Skip to content

Commit 6150d9d

Browse files
committed
objtool: Add --output option
JIRA: https://issues.redhat.com/browse/RHEL-85302 Conflicts: tools/objtool/builtin-check.c - Update !mnop_opts_valid() and !link_opts_valid() error paths as cs-9 doesn't have upstream acc8c6a ("objtool: Consolidate option validation"). tools/objtool/orc_dump.c - Diff context deltas from various missing commits. commit 5a40603 Author: Josh Poimboeuf <jpoimboe@kernel.org> Date: Fri Mar 14 12:29:07 2025 -0700 objtool: Add --output option Add option to allow writing the changed binary to a separate file rather than changing it in place. Libelf makes this suprisingly hard, so take the easy way out and just copy the file before editing it. Also steal the -o short option from --orc. Nobody will notice ;-) Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/r/0da308d42d82b3bbed16a31a72d6bde52afcd6bd.1741975349.git.jpoimboe@kernel.org Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
1 parent b103994 commit 6150d9d

File tree

5 files changed

+91
-37
lines changed

5 files changed

+91
-37
lines changed

tools/objtool/builtin-check.c

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include <subcmd/parse-options.h>
77
#include <string.h>
88
#include <stdlib.h>
9+
#include <fcntl.h>
10+
#include <unistd.h>
11+
#include <sys/stat.h>
12+
#include <sys/sendfile.h>
913
#include <objtool/builtin.h>
1014
#include <objtool/objtool.h>
1115

@@ -14,6 +18,8 @@
1418
"error: objtool: " format "\n", \
1519
##__VA_ARGS__)
1620

21+
const char *objname;
22+
1723
struct opts opts;
1824

1925
static const char * const check_usage[] = {
@@ -71,7 +77,7 @@ const struct option check_options[] = {
7177
OPT_BOOLEAN('i', "ibt", &opts.ibt, "validate and annotate IBT"),
7278
OPT_BOOLEAN('m', "mcount", &opts.mcount, "annotate mcount/fentry calls for ftrace"),
7379
OPT_BOOLEAN('n', "noinstr", &opts.noinstr, "validate noinstr rules"),
74-
OPT_BOOLEAN('o', "orc", &opts.orc, "generate ORC metadata"),
80+
OPT_BOOLEAN(0, "orc", &opts.orc, "generate ORC metadata"),
7581
OPT_BOOLEAN('r', "retpoline", &opts.retpoline, "validate and annotate retpoline usage"),
7682
OPT_BOOLEAN(0, "rethunk", &opts.rethunk, "validate and annotate rethunk usage"),
7783
OPT_BOOLEAN(0, "unret", &opts.unret, "validate entry unret placement"),
@@ -83,15 +89,16 @@ const struct option check_options[] = {
8389
OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),
8490

8591
OPT_GROUP("Options:"),
86-
OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
87-
OPT_BOOLEAN(0, "backup", &opts.backup, "create .orig files before modification"),
88-
OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
89-
OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
90-
OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
91-
OPT_BOOLEAN(0, "mnop", &opts.mnop, "nop out mcount call sites"),
92-
OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
93-
OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
94-
OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
92+
OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
93+
OPT_BOOLEAN(0, "backup", &opts.backup, "create .orig files before modification"),
94+
OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
95+
OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
96+
OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
97+
OPT_BOOLEAN(0, "mnop", &opts.mnop, "nop out mcount call sites"),
98+
OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
99+
OPT_STRING('o', "output", &opts.output, "file", "output file name"),
100+
OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
101+
OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
95102

96103
OPT_END(),
97104
};
@@ -190,37 +197,94 @@ static bool link_opts_valid(struct objtool_file *file)
190197
return true;
191198
}
192199

200+
static int copy_file(const char *src, const char *dst)
201+
{
202+
size_t to_copy, copied;
203+
int dst_fd, src_fd;
204+
struct stat stat;
205+
off_t offset = 0;
206+
207+
src_fd = open(src, O_RDONLY);
208+
if (src_fd == -1) {
209+
ERROR("can't open '%s' for reading", src);
210+
return 1;
211+
}
212+
213+
dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC);
214+
if (dst_fd == -1) {
215+
ERROR("can't open '%s' for writing", dst);
216+
return 1;
217+
}
218+
219+
if (fstat(src_fd, &stat) == -1) {
220+
perror("fstat");
221+
return 1;
222+
}
223+
224+
if (fchmod(dst_fd, stat.st_mode) == -1) {
225+
perror("fchmod");
226+
return 1;
227+
}
228+
229+
for (to_copy = stat.st_size; to_copy > 0; to_copy -= copied) {
230+
copied = sendfile(dst_fd, src_fd, &offset, to_copy);
231+
if (copied == -1) {
232+
perror("sendfile");
233+
return 1;
234+
}
235+
}
236+
237+
close(dst_fd);
238+
close(src_fd);
239+
return 0;
240+
}
241+
193242
int objtool_run(int argc, const char **argv)
194243
{
195-
const char *objname;
196244
struct objtool_file *file;
197245
int ret;
198246

199-
argc = cmd_parse_options(argc, argv, check_usage);
200-
objname = argv[0];
247+
cmd_parse_options(argc, argv, check_usage);
201248

202249
if (!opts_valid())
203250
return 1;
204251

252+
objname = argv[0];
253+
205254
if (opts.dump_orc)
206255
return orc_dump(objname);
207256

257+
if (!opts.dryrun && opts.output) {
258+
/* copy original .o file to output file */
259+
if (copy_file(objname, opts.output))
260+
return 1;
261+
262+
/* from here on, work directly on the output file */
263+
objname = opts.output;
264+
}
265+
208266
file = objtool_open_read(objname);
209267
if (!file)
210-
return 1;
268+
goto err;
211269

212270
if (!mnop_opts_valid())
213-
return 1;
271+
goto err;
214272

215273
if (!link_opts_valid(file))
216-
return 1;
274+
goto err;
217275

218276
ret = check(file);
219277
if (ret)
220-
return ret;
278+
goto err;
221279

222-
if (file->elf->changed)
223-
return elf_write(file->elf);
280+
if (!opts.dryrun && file->elf->changed && elf_write(file->elf))
281+
goto err;
224282

225283
return 0;
284+
285+
err:
286+
if (opts.output)
287+
unlink(opts.output);
288+
289+
return 1;
226290
}

tools/objtool/elf.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,9 +1389,6 @@ int elf_write(struct elf *elf)
13891389
struct section *sec;
13901390
Elf_Scn *s;
13911391

1392-
if (opts.dryrun)
1393-
return 0;
1394-
13951392
/* Update changed relocation sections and section headers: */
13961393
list_for_each_entry(sec, &elf->sections, list) {
13971394
if (sec->truncate)

tools/objtool/include/objtool/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct opts {
3535
bool mnop;
3636
bool module;
3737
bool no_unreachable;
38+
const char *output;
3839
bool sec_address;
3940
bool stats;
4041
};

tools/objtool/objtool.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
bool help;
2020

21-
const char *objname;
2221
static struct objtool_file file;
2322

2423
static bool objtool_create_backup(const char *_objname)
@@ -79,18 +78,14 @@ static bool objtool_create_backup(const char *_objname)
7978
return true;
8079
}
8180

82-
struct objtool_file *objtool_open_read(const char *_objname)
81+
struct objtool_file *objtool_open_read(const char *filename)
8382
{
84-
if (objname) {
85-
if (strcmp(objname, _objname)) {
86-
WARN("won't handle more than one file at a time");
87-
return NULL;
88-
}
89-
return &file;
83+
if (file.elf) {
84+
WARN("won't handle more than one file at a time");
85+
return NULL;
9086
}
91-
objname = _objname;
9287

93-
file.elf = elf_open_read(objname, O_RDWR);
88+
file.elf = elf_open_read(filename, O_RDWR);
9489
if (!file.elf)
9590
return NULL;
9691

tools/objtool/orc_dump.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void print_reg(unsigned int reg, int offset)
6565
printf("%s%+d", reg_name(reg), offset);
6666
}
6767

68-
int orc_dump(const char *_objname)
68+
int orc_dump(const char *filename)
6969
{
7070
int fd, nr_entries, i, *orc_ip = NULL, orc_size = 0;
7171
struct orc_entry *orc = NULL;
@@ -80,12 +80,9 @@ int orc_dump(const char *_objname)
8080
GElf_Sym sym;
8181
Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
8282

83-
84-
objname = _objname;
85-
8683
elf_version(EV_CURRENT);
8784

88-
fd = open(objname, O_RDONLY);
85+
fd = open(filename, O_RDONLY);
8986
if (fd == -1) {
9087
perror("open");
9188
return -1;

0 commit comments

Comments
 (0)