xref: /openbmc/linux/tools/objtool/builtin-check.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
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 
parse_dump(const struct option * opt,const char * str,int unset)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 
parse_hacks(const struct option * opt,const char * str,int unset)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 
68cfd66e81SThomas Weißschuh static 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"),
789f2899feSPeter 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"),
839a479f76SPeter Zijlstra 	OPT_BOOLEAN(0  , "cfi", &opts.cfi, "annotate kernel control flow integrity (kCFI) function preambles"),
84b51277ebSJosh Poimboeuf 	OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),
852daf7fabSJosh Poimboeuf 
862daf7fabSJosh Poimboeuf 	OPT_GROUP("Options:"),
872daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
882daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "backup", &opts.backup, "create .orig files before modification"),
892daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
90753da417SJosh Poimboeuf 	OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
912daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
92280981d6SSathvika Vasireddy 	OPT_BOOLEAN(0, "mnop", &opts.mnop, "nop out mcount call sites"),
932daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
9499c0beb5SJosh Poimboeuf 	OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
952daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
96*ca653464SJosh Poimboeuf 	OPT_BOOLEAN('v', "verbose", &opts.verbose, "verbose warnings"),
972daf7fabSJosh Poimboeuf 
98442f04c3SJosh Poimboeuf 	OPT_END(),
99442f04c3SJosh Poimboeuf };
100442f04c3SJosh Poimboeuf 
cmd_parse_options(int argc,const char ** argv,const char * const usage[])101a2f605f9SPeter Zijlstra int cmd_parse_options(int argc, const char **argv, const char * const usage[])
102a2f605f9SPeter Zijlstra {
103900b4df3SPeter Zijlstra 	const char *envv[16] = { };
104900b4df3SPeter Zijlstra 	char *env;
105900b4df3SPeter Zijlstra 	int envc;
106900b4df3SPeter Zijlstra 
107900b4df3SPeter Zijlstra 	env = getenv("OBJTOOL_ARGS");
108900b4df3SPeter Zijlstra 	if (env) {
109900b4df3SPeter Zijlstra 		envv[0] = "OBJTOOL_ARGS";
110900b4df3SPeter Zijlstra 		for (envc = 1; envc < ARRAY_SIZE(envv); ) {
111900b4df3SPeter Zijlstra 			envv[envc++] = env;
112900b4df3SPeter Zijlstra 			env = strchr(env, ' ');
113900b4df3SPeter Zijlstra 			if (!env)
114900b4df3SPeter Zijlstra 				break;
115900b4df3SPeter Zijlstra 			*env = '\0';
116900b4df3SPeter Zijlstra 			env++;
117900b4df3SPeter Zijlstra 		}
118900b4df3SPeter Zijlstra 
119900b4df3SPeter Zijlstra 		parse_options(envc, envv, check_options, env_usage, 0);
120900b4df3SPeter Zijlstra 	}
121900b4df3SPeter Zijlstra 
122*ca653464SJosh Poimboeuf 	env = getenv("OBJTOOL_VERBOSE");
123*ca653464SJosh Poimboeuf 	if (env && !strcmp(env, "1"))
124*ca653464SJosh Poimboeuf 		opts.verbose = true;
125*ca653464SJosh Poimboeuf 
126a2f605f9SPeter Zijlstra 	argc = parse_options(argc, argv, check_options, usage, 0);
127a2f605f9SPeter Zijlstra 	if (argc != 1)
128a2f605f9SPeter Zijlstra 		usage_with_options(usage, check_options);
129a2f605f9SPeter Zijlstra 	return argc;
130a2f605f9SPeter Zijlstra }
131a2f605f9SPeter Zijlstra 
opts_valid(void)132b51277ebSJosh Poimboeuf static bool opts_valid(void)
133b51277ebSJosh Poimboeuf {
1344ab7674fSJosh Poimboeuf 	if (opts.hack_jump_label	||
13522102f45SJosh Poimboeuf 	    opts.hack_noinstr		||
1364ab7674fSJosh Poimboeuf 	    opts.ibt			||
137b51277ebSJosh Poimboeuf 	    opts.mcount			||
138b51277ebSJosh Poimboeuf 	    opts.noinstr		||
139b51277ebSJosh Poimboeuf 	    opts.orc			||
140b51277ebSJosh Poimboeuf 	    opts.retpoline		||
141f43b9876SPeter Zijlstra 	    opts.rethunk		||
142b51277ebSJosh Poimboeuf 	    opts.sls			||
1437dce6204SJosh Poimboeuf 	    opts.stackval		||
14426e17689SJosh Poimboeuf 	    opts.static_call		||
145b51277ebSJosh Poimboeuf 	    opts.uaccess) {
146b51277ebSJosh Poimboeuf 		if (opts.dump_orc) {
147753da417SJosh Poimboeuf 			ERROR("--dump can't be combined with other options");
148b51277ebSJosh Poimboeuf 			return false;
149b51277ebSJosh Poimboeuf 		}
150b51277ebSJosh Poimboeuf 
151b51277ebSJosh Poimboeuf 		return true;
152b51277ebSJosh Poimboeuf 	}
153b51277ebSJosh Poimboeuf 
154f43b9876SPeter Zijlstra 	if (opts.unret && !opts.rethunk) {
155f43b9876SPeter Zijlstra 		ERROR("--unret requires --rethunk");
156f43b9876SPeter Zijlstra 		return false;
157f43b9876SPeter Zijlstra 	}
158f43b9876SPeter Zijlstra 
159b51277ebSJosh Poimboeuf 	if (opts.dump_orc)
160b51277ebSJosh Poimboeuf 		return true;
161b51277ebSJosh Poimboeuf 
162753da417SJosh Poimboeuf 	ERROR("At least one command required");
163b51277ebSJosh Poimboeuf 	return false;
164b51277ebSJosh Poimboeuf }
165b51277ebSJosh Poimboeuf 
mnop_opts_valid(void)166280981d6SSathvika Vasireddy static bool mnop_opts_valid(void)
167280981d6SSathvika Vasireddy {
168280981d6SSathvika Vasireddy 	if (opts.mnop && !opts.mcount) {
169280981d6SSathvika Vasireddy 		ERROR("--mnop requires --mcount");
170280981d6SSathvika Vasireddy 		return false;
171280981d6SSathvika Vasireddy 	}
172280981d6SSathvika Vasireddy 
173280981d6SSathvika Vasireddy 	return true;
174280981d6SSathvika Vasireddy }
175280981d6SSathvika Vasireddy 
link_opts_valid(struct objtool_file * file)176753da417SJosh Poimboeuf static bool link_opts_valid(struct objtool_file *file)
177753da417SJosh Poimboeuf {
178753da417SJosh Poimboeuf 	if (opts.link)
179753da417SJosh Poimboeuf 		return true;
180753da417SJosh Poimboeuf 
181753da417SJosh Poimboeuf 	if (has_multiple_files(file->elf)) {
182753da417SJosh Poimboeuf 		ERROR("Linked object detected, forcing --link");
183753da417SJosh Poimboeuf 		opts.link = true;
184753da417SJosh Poimboeuf 		return true;
185753da417SJosh Poimboeuf 	}
186753da417SJosh Poimboeuf 
187753da417SJosh Poimboeuf 	if (opts.noinstr) {
188753da417SJosh Poimboeuf 		ERROR("--noinstr requires --link");
189753da417SJosh Poimboeuf 		return false;
190753da417SJosh Poimboeuf 	}
191753da417SJosh Poimboeuf 
192753da417SJosh Poimboeuf 	if (opts.ibt) {
193753da417SJosh Poimboeuf 		ERROR("--ibt requires --link");
194753da417SJosh Poimboeuf 		return false;
195753da417SJosh Poimboeuf 	}
196753da417SJosh Poimboeuf 
197a09a6e23SPeter Zijlstra 	if (opts.unret) {
198a09a6e23SPeter Zijlstra 		ERROR("--unret requires --link");
199a09a6e23SPeter Zijlstra 		return false;
200a09a6e23SPeter Zijlstra 	}
201a09a6e23SPeter Zijlstra 
202753da417SJosh Poimboeuf 	return true;
203753da417SJosh Poimboeuf }
204753da417SJosh Poimboeuf 
objtool_run(int argc,const char ** argv)205b51277ebSJosh Poimboeuf int objtool_run(int argc, const char **argv)
206dcc914f4SJosh Poimboeuf {
2070e731dbcSSami Tolvanen 	const char *objname;
2086545eb03SJulien Thierry 	struct objtool_file *file;
209d44becb9SJulien Thierry 	int ret;
210dcc914f4SJosh Poimboeuf 
211a2f605f9SPeter Zijlstra 	argc = cmd_parse_options(argc, argv, check_usage);
212442f04c3SJosh Poimboeuf 	objname = argv[0];
213442f04c3SJosh Poimboeuf 
214b51277ebSJosh Poimboeuf 	if (!opts_valid())
215b51277ebSJosh Poimboeuf 		return 1;
216b51277ebSJosh Poimboeuf 
217b51277ebSJosh Poimboeuf 	if (opts.dump_orc)
218b51277ebSJosh Poimboeuf 		return orc_dump(objname);
219b51277ebSJosh Poimboeuf 
2206545eb03SJulien Thierry 	file = objtool_open_read(objname);
2216545eb03SJulien Thierry 	if (!file)
2226545eb03SJulien Thierry 		return 1;
2236545eb03SJulien Thierry 
224280981d6SSathvika Vasireddy 	if (!mnop_opts_valid())
225280981d6SSathvika Vasireddy 		return 1;
226280981d6SSathvika Vasireddy 
227753da417SJosh Poimboeuf 	if (!link_opts_valid(file))
228753da417SJosh Poimboeuf 		return 1;
229753da417SJosh Poimboeuf 
230d44becb9SJulien Thierry 	ret = check(file);
231d44becb9SJulien Thierry 	if (ret)
232d44becb9SJulien Thierry 		return ret;
233d44becb9SJulien Thierry 
234d44becb9SJulien Thierry 	if (file->elf->changed)
235d44becb9SJulien Thierry 		return elf_write(file->elf);
236d44becb9SJulien Thierry 
237d44becb9SJulien Thierry 	return 0;
238442f04c3SJosh Poimboeuf }
239