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 return found ? 0 : -1;
63 const struct option check_options[] = {
64 OPT_GROUP("Actions:"),
65 OPT_CALLBACK_OPTARG('h', "hacks", NULL, NULL, "jump_label,noinstr", "patch toolchain bugs/limitations", parse_hacks),
66 OPT_BOOLEAN('i', "ibt", &opts.ibt, "validate and annotate IBT"),
67 OPT_BOOLEAN('m', "mcount", &opts.mcount, "annotate mcount/fentry calls for ftrace"),
68 OPT_BOOLEAN('n', "noinstr", &opts.noinstr, "validate noinstr rules"),
69 OPT_BOOLEAN('o', "orc", &opts.orc, "generate ORC metadata"),
70 OPT_BOOLEAN('r', "retpoline", &opts.retpoline, "validate and annotate retpoline usage"),
71 OPT_BOOLEAN(0, "rethunk", &opts.rethunk, "validate and annotate rethunk usage"),
72 OPT_BOOLEAN(0, "unret", &opts.unret, "validate entry unret placement"),
73 OPT_BOOLEAN('l', "sls", &opts.sls, "validate straight-line-speculation mitigations"),
74 OPT_BOOLEAN('s', "stackval", &opts.stackval, "validate frame pointer rules"),
75 OPT_BOOLEAN('t', "static-call", &opts.static_call, "annotate static calls"),
76 OPT_BOOLEAN('u', "uaccess", &opts.uaccess, "validate uaccess rules for SMAP"),
77 OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),
79 OPT_GROUP("Options:"),
80 OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
81 OPT_BOOLEAN(0, "backup", &opts.backup, "create .orig files before modification"),
82 OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
83 OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
84 OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
85 OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
86 OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
87 OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
92 int cmd_parse_options(int argc, const char **argv, const char * const usage[])
94 const char *envv[16] = { };
98 env = getenv("OBJTOOL_ARGS");
100 envv[0] = "OBJTOOL_ARGS";
101 for (envc = 1; envc < ARRAY_SIZE(envv); ) {
103 env = strchr(env, ' ');
110 parse_options(envc, envv, check_options, env_usage, 0);
113 argc = parse_options(argc, argv, check_options, usage, 0);
115 usage_with_options(usage, check_options);
119 static bool opts_valid(void)
121 if (opts.hack_jump_label ||
134 ERROR("--dump can't be combined with other options");
141 if (opts.unret && !opts.rethunk) {
142 ERROR("--unret requires --rethunk");
149 ERROR("At least one command required");
153 static bool link_opts_valid(struct objtool_file *file)
158 if (has_multiple_files(file->elf)) {
159 ERROR("Linked object detected, forcing --link");
165 ERROR("--noinstr requires --link");
170 ERROR("--ibt requires --link");
175 ERROR("--unret requires --link");
182 int objtool_run(int argc, const char **argv)
185 struct objtool_file *file;
188 argc = cmd_parse_options(argc, argv, check_usage);
195 return orc_dump(objname);
197 file = objtool_open_read(objname);
201 if (!link_opts_valid(file))
208 if (file->elf->changed)
209 return elf_write(file->elf);