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 
parse_dump(const struct option * opt,const char * str,int unset)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 
parse_hacks(const struct option * opt,const char * str,int unset)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 static 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, "mnop", &opts.mnop, "nop out mcount call sites"),
93 	OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
94 	OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
95 	OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
96 
97 	OPT_END(),
98 };
99 
cmd_parse_options(int argc,const char ** argv,const char * const usage[])100 int cmd_parse_options(int argc, const char **argv, const char * const usage[])
101 {
102 	const char *envv[16] = { };
103 	char *env;
104 	int envc;
105 
106 	env = getenv("OBJTOOL_ARGS");
107 	if (env) {
108 		envv[0] = "OBJTOOL_ARGS";
109 		for (envc = 1; envc < ARRAY_SIZE(envv); ) {
110 			envv[envc++] = env;
111 			env = strchr(env, ' ');
112 			if (!env)
113 				break;
114 			*env = '\0';
115 			env++;
116 		}
117 
118 		parse_options(envc, envv, check_options, env_usage, 0);
119 	}
120 
121 	argc = parse_options(argc, argv, check_options, usage, 0);
122 	if (argc != 1)
123 		usage_with_options(usage, check_options);
124 	return argc;
125 }
126 
opts_valid(void)127 static bool opts_valid(void)
128 {
129 	if (opts.hack_jump_label	||
130 	    opts.hack_noinstr		||
131 	    opts.ibt			||
132 	    opts.mcount			||
133 	    opts.noinstr		||
134 	    opts.orc			||
135 	    opts.retpoline		||
136 	    opts.rethunk		||
137 	    opts.sls			||
138 	    opts.stackval		||
139 	    opts.static_call		||
140 	    opts.uaccess) {
141 		if (opts.dump_orc) {
142 			ERROR("--dump can't be combined with other options");
143 			return false;
144 		}
145 
146 		return true;
147 	}
148 
149 	if (opts.unret && !opts.rethunk) {
150 		ERROR("--unret requires --rethunk");
151 		return false;
152 	}
153 
154 	if (opts.dump_orc)
155 		return true;
156 
157 	ERROR("At least one command required");
158 	return false;
159 }
160 
mnop_opts_valid(void)161 static bool mnop_opts_valid(void)
162 {
163 	if (opts.mnop && !opts.mcount) {
164 		ERROR("--mnop requires --mcount");
165 		return false;
166 	}
167 
168 	return true;
169 }
170 
link_opts_valid(struct objtool_file * file)171 static bool link_opts_valid(struct objtool_file *file)
172 {
173 	if (opts.link)
174 		return true;
175 
176 	if (has_multiple_files(file->elf)) {
177 		ERROR("Linked object detected, forcing --link");
178 		opts.link = true;
179 		return true;
180 	}
181 
182 	if (opts.noinstr) {
183 		ERROR("--noinstr requires --link");
184 		return false;
185 	}
186 
187 	if (opts.ibt) {
188 		ERROR("--ibt requires --link");
189 		return false;
190 	}
191 
192 	if (opts.unret) {
193 		ERROR("--unret requires --link");
194 		return false;
195 	}
196 
197 	return true;
198 }
199 
objtool_run(int argc,const char ** argv)200 int objtool_run(int argc, const char **argv)
201 {
202 	const char *objname;
203 	struct objtool_file *file;
204 	int ret;
205 
206 	argc = cmd_parse_options(argc, argv, check_usage);
207 	objname = argv[0];
208 
209 	if (!opts_valid())
210 		return 1;
211 
212 	if (opts.dump_orc)
213 		return orc_dump(objname);
214 
215 	file = objtool_open_read(objname);
216 	if (!file)
217 		return 1;
218 
219 	if (!mnop_opts_valid())
220 		return 1;
221 
222 	if (!link_opts_valid(file))
223 		return 1;
224 
225 	ret = check(file);
226 	if (ret)
227 		return ret;
228 
229 	if (file->elf->changed)
230 		return elf_write(file->elf);
231 
232 	return 0;
233 }
234