xref: /openbmc/linux/tools/objtool/builtin-check.c (revision a09a6e2399ba0595c3042b3164f3ca68a3cff33e)
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 
604ab7674fSJosh Poimboeuf 	return found ? 0 : -1;
614ab7674fSJosh Poimboeuf }
624ab7674fSJosh Poimboeuf 
63dcc914f4SJosh Poimboeuf const struct option check_options[] = {
642daf7fabSJosh Poimboeuf 	OPT_GROUP("Actions:"),
6522102f45SJosh Poimboeuf 	OPT_CALLBACK_OPTARG('h', "hacks", NULL, NULL, "jump_label,noinstr", "patch toolchain bugs/limitations", parse_hacks),
662daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('i', "ibt", &opts.ibt, "validate and annotate IBT"),
672daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('m', "mcount", &opts.mcount, "annotate mcount/fentry calls for ftrace"),
682daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('n', "noinstr", &opts.noinstr, "validate noinstr rules"),
69b51277ebSJosh Poimboeuf 	OPT_BOOLEAN('o', "orc", &opts.orc, "generate ORC metadata"),
702daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('r', "retpoline", &opts.retpoline, "validate and annotate retpoline usage"),
71*a09a6e23SPeter Zijlstra 	OPT_BOOLEAN(0,   "unret", &opts.unret, "validate entry unret placement"),
722daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('l', "sls", &opts.sls, "validate straight-line-speculation mitigations"),
7372064474SJosh Poimboeuf 	OPT_BOOLEAN('s', "stackval", &opts.stackval, "validate frame pointer rules"),
7426e17689SJosh Poimboeuf 	OPT_BOOLEAN('t', "static-call", &opts.static_call, "annotate static calls"),
752daf7fabSJosh Poimboeuf 	OPT_BOOLEAN('u', "uaccess", &opts.uaccess, "validate uaccess rules for SMAP"),
76b51277ebSJosh Poimboeuf 	OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),
772daf7fabSJosh Poimboeuf 
782daf7fabSJosh Poimboeuf 	OPT_GROUP("Options:"),
792daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
802daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "backup", &opts.backup, "create .orig files before modification"),
812daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
82753da417SJosh Poimboeuf 	OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
832daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
842daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
8599c0beb5SJosh Poimboeuf 	OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
862daf7fabSJosh Poimboeuf 	OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
872daf7fabSJosh Poimboeuf 
88442f04c3SJosh Poimboeuf 	OPT_END(),
89442f04c3SJosh Poimboeuf };
90442f04c3SJosh Poimboeuf 
91a2f605f9SPeter Zijlstra int cmd_parse_options(int argc, const char **argv, const char * const usage[])
92a2f605f9SPeter Zijlstra {
93900b4df3SPeter Zijlstra 	const char *envv[16] = { };
94900b4df3SPeter Zijlstra 	char *env;
95900b4df3SPeter Zijlstra 	int envc;
96900b4df3SPeter Zijlstra 
97900b4df3SPeter Zijlstra 	env = getenv("OBJTOOL_ARGS");
98900b4df3SPeter Zijlstra 	if (env) {
99900b4df3SPeter Zijlstra 		envv[0] = "OBJTOOL_ARGS";
100900b4df3SPeter Zijlstra 		for (envc = 1; envc < ARRAY_SIZE(envv); ) {
101900b4df3SPeter Zijlstra 			envv[envc++] = env;
102900b4df3SPeter Zijlstra 			env = strchr(env, ' ');
103900b4df3SPeter Zijlstra 			if (!env)
104900b4df3SPeter Zijlstra 				break;
105900b4df3SPeter Zijlstra 			*env = '\0';
106900b4df3SPeter Zijlstra 			env++;
107900b4df3SPeter Zijlstra 		}
108900b4df3SPeter Zijlstra 
109900b4df3SPeter Zijlstra 		parse_options(envc, envv, check_options, env_usage, 0);
110900b4df3SPeter Zijlstra 	}
111900b4df3SPeter Zijlstra 
112a2f605f9SPeter Zijlstra 	argc = parse_options(argc, argv, check_options, usage, 0);
113a2f605f9SPeter Zijlstra 	if (argc != 1)
114a2f605f9SPeter Zijlstra 		usage_with_options(usage, check_options);
115a2f605f9SPeter Zijlstra 	return argc;
116a2f605f9SPeter Zijlstra }
117a2f605f9SPeter Zijlstra 
118b51277ebSJosh Poimboeuf static bool opts_valid(void)
119b51277ebSJosh Poimboeuf {
1204ab7674fSJosh Poimboeuf 	if (opts.hack_jump_label	||
12122102f45SJosh Poimboeuf 	    opts.hack_noinstr		||
1224ab7674fSJosh Poimboeuf 	    opts.ibt			||
123b51277ebSJosh Poimboeuf 	    opts.mcount			||
124b51277ebSJosh Poimboeuf 	    opts.noinstr		||
125b51277ebSJosh Poimboeuf 	    opts.orc			||
126b51277ebSJosh Poimboeuf 	    opts.retpoline		||
127b51277ebSJosh Poimboeuf 	    opts.sls			||
1287dce6204SJosh Poimboeuf 	    opts.stackval		||
12926e17689SJosh Poimboeuf 	    opts.static_call		||
130b51277ebSJosh Poimboeuf 	    opts.uaccess) {
131b51277ebSJosh Poimboeuf 		if (opts.dump_orc) {
132753da417SJosh Poimboeuf 			ERROR("--dump can't be combined with other options");
133b51277ebSJosh Poimboeuf 			return false;
134b51277ebSJosh Poimboeuf 		}
135b51277ebSJosh Poimboeuf 
136b51277ebSJosh Poimboeuf 		return true;
137b51277ebSJosh Poimboeuf 	}
138b51277ebSJosh Poimboeuf 
139b51277ebSJosh Poimboeuf 	if (opts.dump_orc)
140b51277ebSJosh Poimboeuf 		return true;
141b51277ebSJosh Poimboeuf 
142753da417SJosh Poimboeuf 	ERROR("At least one command required");
143b51277ebSJosh Poimboeuf 	return false;
144b51277ebSJosh Poimboeuf }
145b51277ebSJosh Poimboeuf 
146753da417SJosh Poimboeuf static bool link_opts_valid(struct objtool_file *file)
147753da417SJosh Poimboeuf {
148753da417SJosh Poimboeuf 	if (opts.link)
149753da417SJosh Poimboeuf 		return true;
150753da417SJosh Poimboeuf 
151753da417SJosh Poimboeuf 	if (has_multiple_files(file->elf)) {
152753da417SJosh Poimboeuf 		ERROR("Linked object detected, forcing --link");
153753da417SJosh Poimboeuf 		opts.link = true;
154753da417SJosh Poimboeuf 		return true;
155753da417SJosh Poimboeuf 	}
156753da417SJosh Poimboeuf 
157753da417SJosh Poimboeuf 	if (opts.noinstr) {
158753da417SJosh Poimboeuf 		ERROR("--noinstr requires --link");
159753da417SJosh Poimboeuf 		return false;
160753da417SJosh Poimboeuf 	}
161753da417SJosh Poimboeuf 
162753da417SJosh Poimboeuf 	if (opts.ibt) {
163753da417SJosh Poimboeuf 		ERROR("--ibt requires --link");
164753da417SJosh Poimboeuf 		return false;
165753da417SJosh Poimboeuf 	}
166753da417SJosh Poimboeuf 
167*a09a6e23SPeter Zijlstra 	if (opts.unret) {
168*a09a6e23SPeter Zijlstra 		ERROR("--unret requires --link");
169*a09a6e23SPeter Zijlstra 		return false;
170*a09a6e23SPeter Zijlstra 	}
171*a09a6e23SPeter Zijlstra 
172753da417SJosh Poimboeuf 	return true;
173753da417SJosh Poimboeuf }
174753da417SJosh Poimboeuf 
175b51277ebSJosh Poimboeuf int objtool_run(int argc, const char **argv)
176dcc914f4SJosh Poimboeuf {
1770e731dbcSSami Tolvanen 	const char *objname;
1786545eb03SJulien Thierry 	struct objtool_file *file;
179d44becb9SJulien Thierry 	int ret;
180dcc914f4SJosh Poimboeuf 
181a2f605f9SPeter Zijlstra 	argc = cmd_parse_options(argc, argv, check_usage);
182442f04c3SJosh Poimboeuf 	objname = argv[0];
183442f04c3SJosh Poimboeuf 
184b51277ebSJosh Poimboeuf 	if (!opts_valid())
185b51277ebSJosh Poimboeuf 		return 1;
186b51277ebSJosh Poimboeuf 
187b51277ebSJosh Poimboeuf 	if (opts.dump_orc)
188b51277ebSJosh Poimboeuf 		return orc_dump(objname);
189b51277ebSJosh Poimboeuf 
1906545eb03SJulien Thierry 	file = objtool_open_read(objname);
1916545eb03SJulien Thierry 	if (!file)
1926545eb03SJulien Thierry 		return 1;
1936545eb03SJulien Thierry 
194753da417SJosh Poimboeuf 	if (!link_opts_valid(file))
195753da417SJosh Poimboeuf 		return 1;
196753da417SJosh Poimboeuf 
197d44becb9SJulien Thierry 	ret = check(file);
198d44becb9SJulien Thierry 	if (ret)
199d44becb9SJulien Thierry 		return ret;
200d44becb9SJulien Thierry 
201d44becb9SJulien Thierry 	if (file->elf->changed)
202d44becb9SJulien Thierry 		return elf_write(file->elf);
203d44becb9SJulien Thierry 
204d44becb9SJulien Thierry 	return 0;
205442f04c3SJosh Poimboeuf }
206