xref: /openbmc/linux/tools/objtool/builtin-check.c (revision 9f2899fe36a623885d8576604cb582328ad32b3c)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2442f04c3SJosh Poimboeuf /*
3dcc914f4SJosh Poimboeuf  * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
4442f04c3SJosh Poimboeuf  */
5442f04c3SJosh Poimboeuf 
6442f04c3SJosh Poimboeuf #include <subcmd/parse-options.h>
7c4a33939SPeter Zijlstra #include <string.h>
8900b4df3SPeter Zijlstra #include <stdlib.h>
97786032eSVasily Gorbik #include <objtool/builtin.h>
107786032eSVasily Gorbik #include <objtool/objtool.h>
11442f04c3SJosh Poimboeuf 
12753da417SJosh Poimboeuf #define ERROR(format, ...)				\
13753da417SJosh Poimboeuf 	fprintf(stderr,					\
14753da417SJosh Poimboeuf 		"error: objtool: " format "\n",		\
15753da417SJosh Poimboeuf 		##__VA_ARGS__)
16753da417SJosh Poimboeuf 
172daf7fabSJosh Poimboeuf struct opts opts;
18442f04c3SJosh Poimboeuf 
19dcc914f4SJosh Poimboeuf static const char * const check_usage[] = {
20b51277ebSJosh Poimboeuf 	"objtool <actions> [<options>] file.o",
21442f04c3SJosh Poimboeuf 	NULL,
22442f04c3SJosh Poimboeuf };
23442f04c3SJosh Poimboeuf 
24900b4df3SPeter Zijlstra static const char * const env_usage[] = {
25900b4df3SPeter Zijlstra 	"OBJTOOL_ARGS=\"<options>\"",
26900b4df3SPeter Zijlstra 	NULL,
27900b4df3SPeter Zijlstra };
28900b4df3SPeter Zijlstra 
29b51277ebSJosh Poimboeuf static int parse_dump(const struct option *opt, const char *str, int unset)
30b51277ebSJosh Poimboeuf {
31b51277ebSJosh Poimboeuf 	if (!str || !strcmp(str, "orc")) {
32b51277ebSJosh Poimboeuf 		opts.dump_orc = true;
33b51277ebSJosh Poimboeuf 		return 0;
34b51277ebSJosh Poimboeuf 	}
35b51277ebSJosh Poimboeuf 
36b51277ebSJosh Poimboeuf 	return -1;
37b51277ebSJosh Poimboeuf }
38b51277ebSJosh Poimboeuf 
394ab7674fSJosh Poimboeuf static int parse_hacks(const struct option *opt, const char *str, int unset)
404ab7674fSJosh Poimboeuf {
414ab7674fSJosh Poimboeuf 	bool found = false;
424ab7674fSJosh Poimboeuf 
434ab7674fSJosh Poimboeuf 	/*
444ab7674fSJosh Poimboeuf 	 * Use strstr() as a lazy method of checking for comma-separated
454ab7674fSJosh Poimboeuf 	 * options.
464ab7674fSJosh Poimboeuf 	 *
474ab7674fSJosh Poimboeuf 	 * No string provided == enable all options.
484ab7674fSJosh Poimboeuf 	 */
494ab7674fSJosh Poimboeuf 
504ab7674fSJosh Poimboeuf 	if (!str || strstr(str, "jump_label")) {
514ab7674fSJosh Poimboeuf 		opts.hack_jump_label = true;
524ab7674fSJosh Poimboeuf 		found = true;
534ab7674fSJosh Poimboeuf 	}
544ab7674fSJosh Poimboeuf 
5522102f45SJosh Poimboeuf 	if (!str || strstr(str, "noinstr")) {
5622102f45SJosh Poimboeuf 		opts.hack_noinstr = true;
5722102f45SJosh Poimboeuf 		found = true;
5822102f45SJosh Poimboeuf 	}
5922102f45SJosh Poimboeuf 
600c0a6d89SPeter Zijlstra 	if (!str || strstr(str, "skylake")) {
610c0a6d89SPeter Zijlstra 		opts.hack_skylake = true;
620c0a6d89SPeter Zijlstra 		found = true;
630c0a6d89SPeter Zijlstra 	}
640c0a6d89SPeter Zijlstra 
654ab7674fSJosh Poimboeuf 	return found ? 0 : -1;
664ab7674fSJosh Poimboeuf }
674ab7674fSJosh Poimboeuf 
68dcc914f4SJosh Poimboeuf const struct option check_options[] = {
692daf7fabSJosh Poimboeuf 	OPT_GROUP("Actions:"),
700c0a6d89SPeter Zijlstra 	OPT_CALLBACK_OPTARG('h', "hacks", NULL, NULL, "jump_label,noinstr,skylake", "patch toolchain bugs/limitations", parse_hacks),
712daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('i', "ibt", &opts.ibt, "validate and annotate IBT"),
722daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('m', "mcount", &opts.mcount, "annotate mcount/fentry calls for ftrace"),
732daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('n', "noinstr", &opts.noinstr, "validate noinstr rules"),
74b51277ebSJosh Poimboeuf 	OPT_BOOLEAN('o', "orc", &opts.orc, "generate ORC metadata"),
752daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('r', "retpoline", &opts.retpoline, "validate and annotate retpoline usage"),
76f43b9876SPeter Zijlstra 	OPT_BOOLEAN(0,   "rethunk", &opts.rethunk, "validate and annotate rethunk usage"),
77a09a6e23SPeter Zijlstra 	OPT_BOOLEAN(0,   "unret", &opts.unret, "validate entry unret placement"),
78*9f2899feSPeter Zijlstra 	OPT_INTEGER(0,   "prefix", &opts.prefix, "generate prefix symbols"),
792daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('l', "sls", &opts.sls, "validate straight-line-speculation mitigations"),
8072064474SJosh Poimboeuf 	OPT_BOOLEAN('s', "stackval", &opts.stackval, "validate frame pointer rules"),
8126e17689SJosh Poimboeuf 	OPT_BOOLEAN('t', "static-call", &opts.static_call, "annotate static calls"),
822daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('u', "uaccess", &opts.uaccess, "validate uaccess rules for SMAP"),
83b51277ebSJosh Poimboeuf 	OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),
842daf7fabSJosh Poimboeuf 
852daf7fabSJosh Poimboeuf 	OPT_GROUP("Options:"),
862daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
872daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "backup", &opts.backup, "create .orig files before modification"),
882daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
89753da417SJosh Poimboeuf 	OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
902daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
912daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
9299c0beb5SJosh Poimboeuf 	OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
932daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
942daf7fabSJosh Poimboeuf 
95442f04c3SJosh Poimboeuf 	OPT_END(),
96442f04c3SJosh Poimboeuf };
97442f04c3SJosh Poimboeuf 
98a2f605f9SPeter Zijlstra int cmd_parse_options(int argc, const char **argv, const char * const usage[])
99a2f605f9SPeter Zijlstra {
100900b4df3SPeter Zijlstra 	const char *envv[16] = { };
101900b4df3SPeter Zijlstra 	char *env;
102900b4df3SPeter Zijlstra 	int envc;
103900b4df3SPeter Zijlstra 
104900b4df3SPeter Zijlstra 	env = getenv("OBJTOOL_ARGS");
105900b4df3SPeter Zijlstra 	if (env) {
106900b4df3SPeter Zijlstra 		envv[0] = "OBJTOOL_ARGS";
107900b4df3SPeter Zijlstra 		for (envc = 1; envc < ARRAY_SIZE(envv); ) {
108900b4df3SPeter Zijlstra 			envv[envc++] = env;
109900b4df3SPeter Zijlstra 			env = strchr(env, ' ');
110900b4df3SPeter Zijlstra 			if (!env)
111900b4df3SPeter Zijlstra 				break;
112900b4df3SPeter Zijlstra 			*env = '\0';
113900b4df3SPeter Zijlstra 			env++;
114900b4df3SPeter Zijlstra 		}
115900b4df3SPeter Zijlstra 
116900b4df3SPeter Zijlstra 		parse_options(envc, envv, check_options, env_usage, 0);
117900b4df3SPeter Zijlstra 	}
118900b4df3SPeter Zijlstra 
119a2f605f9SPeter Zijlstra 	argc = parse_options(argc, argv, check_options, usage, 0);
120a2f605f9SPeter Zijlstra 	if (argc != 1)
121a2f605f9SPeter Zijlstra 		usage_with_options(usage, check_options);
122a2f605f9SPeter Zijlstra 	return argc;
123a2f605f9SPeter Zijlstra }
124a2f605f9SPeter Zijlstra 
125b51277ebSJosh Poimboeuf static bool opts_valid(void)
126b51277ebSJosh Poimboeuf {
1274ab7674fSJosh Poimboeuf 	if (opts.hack_jump_label	||
12822102f45SJosh Poimboeuf 	    opts.hack_noinstr		||
1294ab7674fSJosh Poimboeuf 	    opts.ibt			||
130b51277ebSJosh Poimboeuf 	    opts.mcount			||
131b51277ebSJosh Poimboeuf 	    opts.noinstr		||
132b51277ebSJosh Poimboeuf 	    opts.orc			||
133b51277ebSJosh Poimboeuf 	    opts.retpoline		||
134f43b9876SPeter Zijlstra 	    opts.rethunk		||
135b51277ebSJosh Poimboeuf 	    opts.sls			||
1367dce6204SJosh Poimboeuf 	    opts.stackval		||
13726e17689SJosh Poimboeuf 	    opts.static_call		||
138b51277ebSJosh Poimboeuf 	    opts.uaccess) {
139b51277ebSJosh Poimboeuf 		if (opts.dump_orc) {
140753da417SJosh Poimboeuf 			ERROR("--dump can't be combined with other options");
141b51277ebSJosh Poimboeuf 			return false;
142b51277ebSJosh Poimboeuf 		}
143b51277ebSJosh Poimboeuf 
144b51277ebSJosh Poimboeuf 		return true;
145b51277ebSJosh Poimboeuf 	}
146b51277ebSJosh Poimboeuf 
147f43b9876SPeter Zijlstra 	if (opts.unret && !opts.rethunk) {
148f43b9876SPeter Zijlstra 		ERROR("--unret requires --rethunk");
149f43b9876SPeter Zijlstra 		return false;
150f43b9876SPeter Zijlstra 	}
151f43b9876SPeter Zijlstra 
152b51277ebSJosh Poimboeuf 	if (opts.dump_orc)
153b51277ebSJosh Poimboeuf 		return true;
154b51277ebSJosh Poimboeuf 
155753da417SJosh Poimboeuf 	ERROR("At least one command required");
156b51277ebSJosh Poimboeuf 	return false;
157b51277ebSJosh Poimboeuf }
158b51277ebSJosh Poimboeuf 
159753da417SJosh Poimboeuf static bool link_opts_valid(struct objtool_file *file)
160753da417SJosh Poimboeuf {
161753da417SJosh Poimboeuf 	if (opts.link)
162753da417SJosh Poimboeuf 		return true;
163753da417SJosh Poimboeuf 
164753da417SJosh Poimboeuf 	if (has_multiple_files(file->elf)) {
165753da417SJosh Poimboeuf 		ERROR("Linked object detected, forcing --link");
166753da417SJosh Poimboeuf 		opts.link = true;
167753da417SJosh Poimboeuf 		return true;
168753da417SJosh Poimboeuf 	}
169753da417SJosh Poimboeuf 
170753da417SJosh Poimboeuf 	if (opts.noinstr) {
171753da417SJosh Poimboeuf 		ERROR("--noinstr requires --link");
172753da417SJosh Poimboeuf 		return false;
173753da417SJosh Poimboeuf 	}
174753da417SJosh Poimboeuf 
175753da417SJosh Poimboeuf 	if (opts.ibt) {
176753da417SJosh Poimboeuf 		ERROR("--ibt requires --link");
177753da417SJosh Poimboeuf 		return false;
178753da417SJosh Poimboeuf 	}
179753da417SJosh Poimboeuf 
180a09a6e23SPeter Zijlstra 	if (opts.unret) {
181a09a6e23SPeter Zijlstra 		ERROR("--unret requires --link");
182a09a6e23SPeter Zijlstra 		return false;
183a09a6e23SPeter Zijlstra 	}
184a09a6e23SPeter Zijlstra 
185753da417SJosh Poimboeuf 	return true;
186753da417SJosh Poimboeuf }
187753da417SJosh Poimboeuf 
188b51277ebSJosh Poimboeuf int objtool_run(int argc, const char **argv)
189dcc914f4SJosh Poimboeuf {
1900e731dbcSSami Tolvanen 	const char *objname;
1916545eb03SJulien Thierry 	struct objtool_file *file;
192d44becb9SJulien Thierry 	int ret;
193dcc914f4SJosh Poimboeuf 
194a2f605f9SPeter Zijlstra 	argc = cmd_parse_options(argc, argv, check_usage);
195442f04c3SJosh Poimboeuf 	objname = argv[0];
196442f04c3SJosh Poimboeuf 
197b51277ebSJosh Poimboeuf 	if (!opts_valid())
198b51277ebSJosh Poimboeuf 		return 1;
199b51277ebSJosh Poimboeuf 
200b51277ebSJosh Poimboeuf 	if (opts.dump_orc)
201b51277ebSJosh Poimboeuf 		return orc_dump(objname);
202b51277ebSJosh Poimboeuf 
2036545eb03SJulien Thierry 	file = objtool_open_read(objname);
2046545eb03SJulien Thierry 	if (!file)
2056545eb03SJulien Thierry 		return 1;
2066545eb03SJulien Thierry 
207753da417SJosh Poimboeuf 	if (!link_opts_valid(file))
208753da417SJosh Poimboeuf 		return 1;
209753da417SJosh Poimboeuf 
210d44becb9SJulien Thierry 	ret = check(file);
211d44becb9SJulien Thierry 	if (ret)
212d44becb9SJulien Thierry 		return ret;
213d44becb9SJulien Thierry 
214d44becb9SJulien Thierry 	if (file->elf->changed)
215d44becb9SJulien Thierry 		return elf_write(file->elf);
216d44becb9SJulien Thierry 
217d44becb9SJulien Thierry 	return 0;
218442f04c3SJosh Poimboeuf }
219