1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
6 #include <subcmd/parse-options.h>
9 #include <objtool/builtin.h>
10 #include <objtool/objtool.h>
12 #define ERROR(format, ...) \
14 "error: objtool: " format "\n", \
19 static const char * const check_usage
[] = {
20 "objtool <actions> [<options>] file.o",
24 static const char * const env_usage
[] = {
25 "OBJTOOL_ARGS=\"<options>\"",
29 static int parse_dump(const struct option
*opt
, const char *str
, int unset
)
31 if (!str
|| !strcmp(str
, "orc")) {
39 static int parse_hacks(const struct option
*opt
, const char *str
, int unset
)
44 * Use strstr() as a lazy method of checking for comma-separated
47 * No string provided == enable all options.
50 if (!str
|| strstr(str
, "jump_label")) {
51 opts
.hack_jump_label
= true;
55 if (!str
|| strstr(str
, "noinstr")) {
56 opts
.hack_noinstr
= true;
60 if (!str
|| strstr(str
, "skylake")) {
61 opts
.hack_skylake
= true;
65 return found
? 0 : -1;
68 static const struct option check_options
[] = {
69 OPT_GROUP("Actions:"),
70 OPT_CALLBACK_OPTARG('h', "hacks", NULL
, NULL
, "jump_label,noinstr,skylake", "patch toolchain bugs/limitations", parse_hacks
),
71 OPT_BOOLEAN('i', "ibt", &opts
.ibt
, "validate and annotate IBT"),
72 OPT_BOOLEAN('m', "mcount", &opts
.mcount
, "annotate mcount/fentry calls for ftrace"),
73 OPT_BOOLEAN('n', "noinstr", &opts
.noinstr
, "validate noinstr rules"),
74 OPT_BOOLEAN('o', "orc", &opts
.orc
, "generate ORC metadata"),
75 OPT_BOOLEAN('r', "retpoline", &opts
.retpoline
, "validate and annotate retpoline usage"),
76 OPT_BOOLEAN(0, "rethunk", &opts
.rethunk
, "validate and annotate rethunk usage"),
77 OPT_BOOLEAN(0, "unret", &opts
.unret
, "validate entry unret placement"),
78 OPT_INTEGER(0, "prefix", &opts
.prefix
, "generate prefix symbols"),
79 OPT_BOOLEAN('l', "sls", &opts
.sls
, "validate straight-line-speculation mitigations"),
80 OPT_BOOLEAN('s', "stackval", &opts
.stackval
, "validate frame pointer rules"),
81 OPT_BOOLEAN('t', "static-call", &opts
.static_call
, "annotate static calls"),
82 OPT_BOOLEAN('u', "uaccess", &opts
.uaccess
, "validate uaccess rules for SMAP"),
83 OPT_BOOLEAN(0 , "cfi", &opts
.cfi
, "annotate kernel control flow integrity (kCFI) function preambles"),
84 OPT_CALLBACK_OPTARG(0, "dump", NULL
, NULL
, "orc", "dump metadata", parse_dump
),
86 OPT_GROUP("Options:"),
87 OPT_BOOLEAN(0, "backtrace", &opts
.backtrace
, "unwind on error"),
88 OPT_BOOLEAN(0, "backup", &opts
.backup
, "create .orig files before modification"),
89 OPT_BOOLEAN(0, "dry-run", &opts
.dryrun
, "don't write modifications"),
90 OPT_BOOLEAN(0, "link", &opts
.link
, "object is a linked object"),
91 OPT_BOOLEAN(0, "module", &opts
.module
, "object is part of a kernel module"),
92 OPT_BOOLEAN(0, "mnop", &opts
.mnop
, "nop out mcount call sites"),
93 OPT_BOOLEAN(0, "no-unreachable", &opts
.no_unreachable
, "skip 'unreachable instruction' warnings"),
94 OPT_BOOLEAN(0, "sec-address", &opts
.sec_address
, "print section addresses in warnings"),
95 OPT_BOOLEAN(0, "stats", &opts
.stats
, "print statistics"),
96 OPT_BOOLEAN('v', "verbose", &opts
.verbose
, "verbose warnings"),
101 int cmd_parse_options(int argc
, const char **argv
, const char * const usage
[])
103 const char *envv
[16] = { };
107 env
= getenv("OBJTOOL_ARGS");
109 envv
[0] = "OBJTOOL_ARGS";
110 for (envc
= 1; envc
< ARRAY_SIZE(envv
); ) {
112 env
= strchr(env
, ' ');
119 parse_options(envc
, envv
, check_options
, env_usage
, 0);
122 env
= getenv("OBJTOOL_VERBOSE");
123 if (env
&& !strcmp(env
, "1"))
126 argc
= parse_options(argc
, argv
, check_options
, usage
, 0);
128 usage_with_options(usage
, check_options
);
132 static bool opts_valid(void)
134 if (opts
.hack_jump_label
||
147 ERROR("--dump can't be combined with other actions");
154 if (opts
.unret
&& !opts
.rethunk
) {
155 ERROR("--unret requires --rethunk");
162 ERROR("At least one action required");
166 static bool mnop_opts_valid(void)
168 if (opts
.mnop
&& !opts
.mcount
) {
169 ERROR("--mnop requires --mcount");
176 static bool link_opts_valid(struct objtool_file
*file
)
181 if (has_multiple_files(file
->elf
)) {
182 ERROR("Linked object detected, forcing --link");
188 ERROR("--noinstr requires --link");
193 ERROR("--ibt requires --link");
198 ERROR("--unret requires --link");
205 int objtool_run(int argc
, const char **argv
)
208 struct objtool_file
*file
;
211 argc
= cmd_parse_options(argc
, argv
, check_usage
);
218 return orc_dump(objname
);
220 file
= objtool_open_read(objname
);
224 if (!mnop_opts_valid())
227 if (!link_opts_valid(file
))
234 if (file
->elf
->changed
)
235 return elf_write(file
->elf
);