xref: /linux/tools/objtool/builtin-check.c (revision 785d21ba2f447fb26df4b22f45653763beb767ea)
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 	if (!str || strstr(str, "skylake")) {
61 		opts.hack_skylake = true;
62 		found = true;
63 	}
64 
65 	return found ? 0 : -1;
66 }
67 
68 const struct option check_options[] = {
69 	OPT_GROUP("Actions:"),
70 	OPT_CALLBACK_OPTARG('h', "hacks", NULL, NULL, "jump_label,noinstr,skylake", "patch toolchain bugs/limitations", parse_hacks),
71 	OPT_BOOLEAN('i', "ibt", &opts.ibt, "validate and annotate IBT"),
72 	OPT_BOOLEAN('m', "mcount", &opts.mcount, "annotate mcount/fentry calls for ftrace"),
73 	OPT_BOOLEAN('n', "noinstr", &opts.noinstr, "validate noinstr rules"),
74 	OPT_BOOLEAN('o', "orc", &opts.orc, "generate ORC metadata"),
75 	OPT_BOOLEAN('r', "retpoline", &opts.retpoline, "validate and annotate retpoline usage"),
76 	OPT_BOOLEAN(0,   "rethunk", &opts.rethunk, "validate and annotate rethunk usage"),
77 	OPT_BOOLEAN(0,   "unret", &opts.unret, "validate entry unret placement"),
78 	OPT_INTEGER(0,   "prefix", &opts.prefix, "generate prefix symbols"),
79 	OPT_BOOLEAN('l', "sls", &opts.sls, "validate straight-line-speculation mitigations"),
80 	OPT_BOOLEAN('s', "stackval", &opts.stackval, "validate frame pointer rules"),
81 	OPT_BOOLEAN('t', "static-call", &opts.static_call, "annotate static calls"),
82 	OPT_BOOLEAN('u', "uaccess", &opts.uaccess, "validate uaccess rules for SMAP"),
83 	OPT_BOOLEAN(0  , "cfi", &opts.cfi, "annotate kernel control flow integrity (kCFI) function preambles"),
84 	OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),
85 
86 	OPT_GROUP("Options:"),
87 	OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
88 	OPT_BOOLEAN(0, "backup", &opts.backup, "create .orig files before modification"),
89 	OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
90 	OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
91 	OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
92 	OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
93 	OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
94 	OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
95 
96 	OPT_END(),
97 };
98 
99 int cmd_parse_options(int argc, const char **argv, const char * const usage[])
100 {
101 	const char *envv[16] = { };
102 	char *env;
103 	int envc;
104 
105 	env = getenv("OBJTOOL_ARGS");
106 	if (env) {
107 		envv[0] = "OBJTOOL_ARGS";
108 		for (envc = 1; envc < ARRAY_SIZE(envv); ) {
109 			envv[envc++] = env;
110 			env = strchr(env, ' ');
111 			if (!env)
112 				break;
113 			*env = '\0';
114 			env++;
115 		}
116 
117 		parse_options(envc, envv, check_options, env_usage, 0);
118 	}
119 
120 	argc = parse_options(argc, argv, check_options, usage, 0);
121 	if (argc != 1)
122 		usage_with_options(usage, check_options);
123 	return argc;
124 }
125 
126 static bool opts_valid(void)
127 {
128 	if (opts.hack_jump_label	||
129 	    opts.hack_noinstr		||
130 	    opts.ibt			||
131 	    opts.mcount			||
132 	    opts.noinstr		||
133 	    opts.orc			||
134 	    opts.retpoline		||
135 	    opts.rethunk		||
136 	    opts.sls			||
137 	    opts.stackval		||
138 	    opts.static_call		||
139 	    opts.uaccess) {
140 		if (opts.dump_orc) {
141 			ERROR("--dump can't be combined with other options");
142 			return false;
143 		}
144 
145 		return true;
146 	}
147 
148 	if (opts.unret && !opts.rethunk) {
149 		ERROR("--unret requires --rethunk");
150 		return false;
151 	}
152 
153 	if (opts.dump_orc)
154 		return true;
155 
156 	ERROR("At least one command required");
157 	return false;
158 }
159 
160 static bool link_opts_valid(struct objtool_file *file)
161 {
162 	if (opts.link)
163 		return true;
164 
165 	if (has_multiple_files(file->elf)) {
166 		ERROR("Linked object detected, forcing --link");
167 		opts.link = true;
168 		return true;
169 	}
170 
171 	if (opts.noinstr) {
172 		ERROR("--noinstr requires --link");
173 		return false;
174 	}
175 
176 	if (opts.ibt) {
177 		ERROR("--ibt requires --link");
178 		return false;
179 	}
180 
181 	if (opts.unret) {
182 		ERROR("--unret requires --link");
183 		return false;
184 	}
185 
186 	return true;
187 }
188 
189 int objtool_run(int argc, const char **argv)
190 {
191 	const char *objname;
192 	struct objtool_file *file;
193 	int ret;
194 
195 	argc = cmd_parse_options(argc, argv, check_usage);
196 	objname = argv[0];
197 
198 	if (!opts_valid())
199 		return 1;
200 
201 	if (opts.dump_orc)
202 		return orc_dump(objname);
203 
204 	file = objtool_open_read(objname);
205 	if (!file)
206 		return 1;
207 
208 	if (!link_opts_valid(file))
209 		return 1;
210 
211 	ret = check(file);
212 	if (ret)
213 		return ret;
214 
215 	if (file->elf->changed)
216 		return elf_write(file->elf);
217 
218 	return 0;
219 }
220