xref: /linux/tools/objtool/builtin-check.c (revision 058443934524590d5537a80f490267cc95a61c05)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
4  */
5 
6 #include <subcmd/parse-options.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <objtool/builtin.h>
10 #include <objtool/objtool.h>
11 
12 #define ERROR(format, ...)				\
13 	fprintf(stderr,					\
14 		"error: objtool: " format "\n",		\
15 		##__VA_ARGS__)
16 
17 struct opts opts;
18 
19 static const char * const check_usage[] = {
20 	"objtool <actions> [<options>] file.o",
21 	NULL,
22 };
23 
24 static const char * const env_usage[] = {
25 	"OBJTOOL_ARGS=\"<options>\"",
26 	NULL,
27 };
28 
29 static int parse_dump(const struct option *opt, const char *str, int unset)
30 {
31 	if (!str || !strcmp(str, "orc")) {
32 		opts.dump_orc = true;
33 		return 0;
34 	}
35 
36 	return -1;
37 }
38 
39 static int parse_hacks(const struct option *opt, const char *str, int unset)
40 {
41 	bool found = false;
42 
43 	/*
44 	 * Use strstr() as a lazy method of checking for comma-separated
45 	 * options.
46 	 *
47 	 * No string provided == enable all options.
48 	 */
49 
50 	if (!str || strstr(str, "jump_label")) {
51 		opts.hack_jump_label = true;
52 		found = true;
53 	}
54 
55 	if (!str || strstr(str, "noinstr")) {
56 		opts.hack_noinstr = true;
57 		found = true;
58 	}
59 
60 	return found ? 0 : -1;
61 }
62 
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),
78 
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"),
88 
89 	OPT_END(),
90 };
91 
92 int cmd_parse_options(int argc, const char **argv, const char * const usage[])
93 {
94 	const char *envv[16] = { };
95 	char *env;
96 	int envc;
97 
98 	env = getenv("OBJTOOL_ARGS");
99 	if (env) {
100 		envv[0] = "OBJTOOL_ARGS";
101 		for (envc = 1; envc < ARRAY_SIZE(envv); ) {
102 			envv[envc++] = env;
103 			env = strchr(env, ' ');
104 			if (!env)
105 				break;
106 			*env = '\0';
107 			env++;
108 		}
109 
110 		parse_options(envc, envv, check_options, env_usage, 0);
111 	}
112 
113 	argc = parse_options(argc, argv, check_options, usage, 0);
114 	if (argc != 1)
115 		usage_with_options(usage, check_options);
116 	return argc;
117 }
118 
119 static bool opts_valid(void)
120 {
121 	if (opts.hack_jump_label	||
122 	    opts.hack_noinstr		||
123 	    opts.ibt			||
124 	    opts.mcount			||
125 	    opts.noinstr		||
126 	    opts.orc			||
127 	    opts.retpoline		||
128 	    opts.rethunk		||
129 	    opts.sls			||
130 	    opts.stackval		||
131 	    opts.static_call		||
132 	    opts.uaccess) {
133 		if (opts.dump_orc) {
134 			ERROR("--dump can't be combined with other options");
135 			return false;
136 		}
137 
138 		return true;
139 	}
140 
141 	if (opts.unret && !opts.rethunk) {
142 		ERROR("--unret requires --rethunk");
143 		return false;
144 	}
145 
146 	if (opts.dump_orc)
147 		return true;
148 
149 	ERROR("At least one command required");
150 	return false;
151 }
152 
153 static bool link_opts_valid(struct objtool_file *file)
154 {
155 	if (opts.link)
156 		return true;
157 
158 	if (has_multiple_files(file->elf)) {
159 		ERROR("Linked object detected, forcing --link");
160 		opts.link = true;
161 		return true;
162 	}
163 
164 	if (opts.noinstr) {
165 		ERROR("--noinstr requires --link");
166 		return false;
167 	}
168 
169 	if (opts.ibt) {
170 		ERROR("--ibt requires --link");
171 		return false;
172 	}
173 
174 	if (opts.unret) {
175 		ERROR("--unret requires --link");
176 		return false;
177 	}
178 
179 	return true;
180 }
181 
182 int objtool_run(int argc, const char **argv)
183 {
184 	const char *objname;
185 	struct objtool_file *file;
186 	int ret;
187 
188 	argc = cmd_parse_options(argc, argv, check_usage);
189 	objname = argv[0];
190 
191 	if (!opts_valid())
192 		return 1;
193 
194 	if (opts.dump_orc)
195 		return orc_dump(objname);
196 
197 	file = objtool_open_read(objname);
198 	if (!file)
199 		return 1;
200 
201 	if (!link_opts_valid(file))
202 		return 1;
203 
204 	ret = check(file);
205 	if (ret)
206 		return ret;
207 
208 	if (file->elf->changed)
209 		return elf_write(file->elf);
210 
211 	return 0;
212 }
213