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 <fcntl.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <sys/stat.h>
13 #include <sys/sendfile.h>
14 #include <objtool/builtin.h>
15 #include <objtool/objtool.h>
16 #include <objtool/warn.h>
17
18 #define ORIG_SUFFIX ".orig"
19
20 int orig_argc;
21 static char **orig_argv;
22 const char *objname;
23 struct opts opts;
24
25 static const char * const check_usage[] = {
26 "objtool <actions> [<options>] file.o",
27 NULL,
28 };
29
30 static const char * const env_usage[] = {
31 "OBJTOOL_ARGS=\"<options>\"",
32 NULL,
33 };
34
parse_dump(const struct option * opt,const char * str,int unset)35 static int parse_dump(const struct option *opt, const char *str, int unset)
36 {
37 if (!str || !strcmp(str, "orc")) {
38 opts.dump_orc = true;
39 return 0;
40 }
41
42 return -1;
43 }
44
parse_hacks(const struct option * opt,const char * str,int unset)45 static int parse_hacks(const struct option *opt, const char *str, int unset)
46 {
47 bool found = false;
48
49 /*
50 * Use strstr() as a lazy method of checking for comma-separated
51 * options.
52 *
53 * No string provided == enable all options.
54 */
55
56 if (!str || strstr(str, "jump_label")) {
57 opts.hack_jump_label = true;
58 found = true;
59 }
60
61 if (!str || strstr(str, "noinstr")) {
62 opts.hack_noinstr = true;
63 found = true;
64 }
65
66 if (!str || strstr(str, "skylake")) {
67 opts.hack_skylake = true;
68 found = true;
69 }
70
71 return found ? 0 : -1;
72 }
73
74 static const struct option check_options[] = {
75 OPT_GROUP("Actions:"),
76 OPT_CALLBACK_OPTARG('h', "hacks", NULL, NULL, "jump_label,noinstr,skylake", "patch toolchain bugs/limitations", parse_hacks),
77 OPT_BOOLEAN('i', "ibt", &opts.ibt, "validate and annotate IBT"),
78 OPT_BOOLEAN('m', "mcount", &opts.mcount, "annotate mcount/fentry calls for ftrace"),
79 OPT_BOOLEAN('n', "noinstr", &opts.noinstr, "validate noinstr rules"),
80 OPT_BOOLEAN(0, "orc", &opts.orc, "generate ORC metadata"),
81 OPT_BOOLEAN('r', "retpoline", &opts.retpoline, "validate and annotate retpoline usage"),
82 OPT_BOOLEAN(0, "rethunk", &opts.rethunk, "validate and annotate rethunk usage"),
83 OPT_BOOLEAN(0, "unret", &opts.unret, "validate entry unret placement"),
84 OPT_INTEGER(0, "prefix", &opts.prefix, "generate prefix symbols"),
85 OPT_BOOLEAN('l', "sls", &opts.sls, "validate straight-line-speculation mitigations"),
86 OPT_BOOLEAN('s', "stackval", &opts.stackval, "validate frame pointer rules"),
87 OPT_BOOLEAN('t', "static-call", &opts.static_call, "annotate static calls"),
88 OPT_BOOLEAN('u', "uaccess", &opts.uaccess, "validate uaccess rules for SMAP"),
89 OPT_BOOLEAN(0 , "cfi", &opts.cfi, "annotate kernel control flow integrity (kCFI) function preambles"),
90 OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),
91
92 OPT_GROUP("Options:"),
93 OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
94 OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
95 OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
96 OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
97 OPT_BOOLEAN(0, "mnop", &opts.mnop, "nop out mcount call sites"),
98 OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
99 OPT_STRING('o', "output", &opts.output, "file", "output file name"),
100 OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
101 OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
102 OPT_BOOLEAN('v', "verbose", &opts.verbose, "verbose warnings"),
103 OPT_BOOLEAN(0, "Werror", &opts.werror, "return error on warnings"),
104
105 OPT_END(),
106 };
107
cmd_parse_options(int argc,const char ** argv,const char * const usage[])108 int cmd_parse_options(int argc, const char **argv, const char * const usage[])
109 {
110 const char *envv[16] = { };
111 char *env;
112 int envc;
113
114 env = getenv("OBJTOOL_ARGS");
115 if (env) {
116 envv[0] = "OBJTOOL_ARGS";
117 for (envc = 1; envc < ARRAY_SIZE(envv); ) {
118 envv[envc++] = env;
119 env = strchr(env, ' ');
120 if (!env)
121 break;
122 *env = '\0';
123 env++;
124 }
125
126 parse_options(envc, envv, check_options, env_usage, 0);
127 }
128
129 env = getenv("OBJTOOL_VERBOSE");
130 if (env && !strcmp(env, "1"))
131 opts.verbose = true;
132
133 argc = parse_options(argc, argv, check_options, usage, 0);
134 if (argc != 1)
135 usage_with_options(usage, check_options);
136 return argc;
137 }
138
opts_valid(void)139 static bool opts_valid(void)
140 {
141 if (opts.mnop && !opts.mcount) {
142 ERROR("--mnop requires --mcount");
143 return false;
144 }
145
146 if (opts.noinstr && !opts.link) {
147 ERROR("--noinstr requires --link");
148 return false;
149 }
150
151 if (opts.ibt && !opts.link) {
152 ERROR("--ibt requires --link");
153 return false;
154 }
155
156 if (opts.unret && !opts.link) {
157 ERROR("--unret requires --link");
158 return false;
159 }
160
161 if (opts.hack_jump_label ||
162 opts.hack_noinstr ||
163 opts.ibt ||
164 opts.mcount ||
165 opts.noinstr ||
166 opts.orc ||
167 opts.retpoline ||
168 opts.rethunk ||
169 opts.sls ||
170 opts.stackval ||
171 opts.static_call ||
172 opts.uaccess) {
173 if (opts.dump_orc) {
174 ERROR("--dump can't be combined with other actions");
175 return false;
176 }
177
178 return true;
179 }
180
181 if (opts.dump_orc)
182 return true;
183
184 ERROR("At least one action required");
185 return false;
186 }
187
copy_file(const char * src,const char * dst)188 static int copy_file(const char *src, const char *dst)
189 {
190 size_t to_copy, copied;
191 int dst_fd, src_fd;
192 struct stat stat;
193 off_t offset = 0;
194
195 src_fd = open(src, O_RDONLY);
196 if (src_fd == -1) {
197 ERROR("can't open %s for reading: %s", src, strerror(errno));
198 return 1;
199 }
200
201 dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0400);
202 if (dst_fd == -1) {
203 ERROR("can't open %s for writing: %s", dst, strerror(errno));
204 return 1;
205 }
206
207 if (fstat(src_fd, &stat) == -1) {
208 ERROR_GLIBC("fstat");
209 return 1;
210 }
211
212 if (fchmod(dst_fd, stat.st_mode) == -1) {
213 ERROR_GLIBC("fchmod");
214 return 1;
215 }
216
217 for (to_copy = stat.st_size; to_copy > 0; to_copy -= copied) {
218 copied = sendfile(dst_fd, src_fd, &offset, to_copy);
219 if (copied == -1) {
220 ERROR_GLIBC("sendfile");
221 return 1;
222 }
223 }
224
225 close(dst_fd);
226 close(src_fd);
227 return 0;
228 }
229
save_argv(int argc,const char ** argv)230 static void save_argv(int argc, const char **argv)
231 {
232 orig_argv = calloc(argc, sizeof(char *));
233 if (!orig_argv) {
234 ERROR_GLIBC("calloc");
235 exit(1);
236 }
237
238 for (int i = 0; i < argc; i++) {
239 orig_argv[i] = strdup(argv[i]);
240 if (!orig_argv[i]) {
241 ERROR_GLIBC("strdup(%s)", argv[i]);
242 exit(1);
243 }
244 };
245 }
246
print_args(void)247 void print_args(void)
248 {
249 char *backup = NULL;
250
251 if (opts.output || opts.dryrun)
252 goto print;
253
254 /*
255 * Make a backup before kbuild deletes the file so the error
256 * can be recreated without recompiling or relinking.
257 */
258 backup = malloc(strlen(objname) + strlen(ORIG_SUFFIX) + 1);
259 if (!backup) {
260 ERROR_GLIBC("malloc");
261 goto print;
262 }
263
264 strcpy(backup, objname);
265 strcat(backup, ORIG_SUFFIX);
266 if (copy_file(objname, backup)) {
267 backup = NULL;
268 goto print;
269 }
270
271 print:
272 /*
273 * Print the cmdline args to make it easier to recreate. If '--output'
274 * wasn't used, add it to the printed args with the backup as input.
275 */
276 fprintf(stderr, "%s", orig_argv[0]);
277
278 for (int i = 1; i < orig_argc; i++) {
279 char *arg = orig_argv[i];
280
281 if (backup && !strcmp(arg, objname))
282 fprintf(stderr, " %s -o %s", backup, objname);
283 else
284 fprintf(stderr, " %s", arg);
285 }
286
287 fprintf(stderr, "\n");
288 }
289
objtool_run(int argc,const char ** argv)290 int objtool_run(int argc, const char **argv)
291 {
292 struct objtool_file *file;
293 int ret = 0;
294
295 orig_argc = argc;
296 save_argv(argc, argv);
297
298 cmd_parse_options(argc, argv, check_usage);
299
300 if (!opts_valid())
301 return 1;
302
303 objname = argv[0];
304
305 if (opts.dump_orc)
306 return orc_dump(objname);
307
308 if (!opts.dryrun && opts.output) {
309 /* copy original .o file to output file */
310 if (copy_file(objname, opts.output))
311 return 1;
312
313 /* from here on, work directly on the output file */
314 objname = opts.output;
315 }
316
317 file = objtool_open_read(objname);
318 if (!file)
319 return 1;
320
321 if (!opts.link && has_multiple_files(file->elf)) {
322 ERROR("Linked object requires --link");
323 return 1;
324 }
325
326 ret = check(file);
327 if (ret)
328 return ret;
329
330 if (!opts.dryrun && file->elf->changed && elf_write(file->elf))
331 return 1;
332
333 return 0;
334 }
335