Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/objtool/builtin-check.c
29267 views
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
* Copyright (C) 2015-2017 Josh Poimboeuf <[email protected]>
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
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
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_BOOLEAN(0 , "noabs", &opts.noabs, "reject absolute references in allocatable sections"),
91
OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),
92
93
OPT_GROUP("Options:"),
94
OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
95
OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
96
OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
97
OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
98
OPT_BOOLEAN(0, "mnop", &opts.mnop, "nop out mcount call sites"),
99
OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
100
OPT_STRING('o', "output", &opts.output, "file", "output file name"),
101
OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
102
OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
103
OPT_BOOLEAN('v', "verbose", &opts.verbose, "verbose warnings"),
104
OPT_BOOLEAN(0, "Werror", &opts.werror, "return error on warnings"),
105
106
OPT_END(),
107
};
108
109
int cmd_parse_options(int argc, const char **argv, const char * const usage[])
110
{
111
const char *envv[16] = { };
112
char *env;
113
int envc;
114
115
env = getenv("OBJTOOL_ARGS");
116
if (env) {
117
envv[0] = "OBJTOOL_ARGS";
118
for (envc = 1; envc < ARRAY_SIZE(envv); ) {
119
envv[envc++] = env;
120
env = strchr(env, ' ');
121
if (!env)
122
break;
123
*env = '\0';
124
env++;
125
}
126
127
parse_options(envc, envv, check_options, env_usage, 0);
128
}
129
130
env = getenv("OBJTOOL_VERBOSE");
131
if (env && !strcmp(env, "1"))
132
opts.verbose = true;
133
134
argc = parse_options(argc, argv, check_options, usage, 0);
135
if (argc != 1)
136
usage_with_options(usage, check_options);
137
return argc;
138
}
139
140
static bool opts_valid(void)
141
{
142
if (opts.mnop && !opts.mcount) {
143
ERROR("--mnop requires --mcount");
144
return false;
145
}
146
147
if (opts.noinstr && !opts.link) {
148
ERROR("--noinstr requires --link");
149
return false;
150
}
151
152
if (opts.ibt && !opts.link) {
153
ERROR("--ibt requires --link");
154
return false;
155
}
156
157
if (opts.unret && !opts.link) {
158
ERROR("--unret requires --link");
159
return false;
160
}
161
162
if (opts.hack_jump_label ||
163
opts.hack_noinstr ||
164
opts.ibt ||
165
opts.mcount ||
166
opts.noabs ||
167
opts.noinstr ||
168
opts.orc ||
169
opts.retpoline ||
170
opts.rethunk ||
171
opts.sls ||
172
opts.stackval ||
173
opts.static_call ||
174
opts.uaccess) {
175
if (opts.dump_orc) {
176
ERROR("--dump can't be combined with other actions");
177
return false;
178
}
179
180
return true;
181
}
182
183
if (opts.dump_orc)
184
return true;
185
186
ERROR("At least one action required");
187
return false;
188
}
189
190
static int copy_file(const char *src, const char *dst)
191
{
192
size_t to_copy, copied;
193
int dst_fd, src_fd;
194
struct stat stat;
195
off_t offset = 0;
196
197
src_fd = open(src, O_RDONLY);
198
if (src_fd == -1) {
199
ERROR("can't open %s for reading: %s", src, strerror(errno));
200
return 1;
201
}
202
203
dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0400);
204
if (dst_fd == -1) {
205
ERROR("can't open %s for writing: %s", dst, strerror(errno));
206
return 1;
207
}
208
209
if (fstat(src_fd, &stat) == -1) {
210
ERROR_GLIBC("fstat");
211
return 1;
212
}
213
214
if (fchmod(dst_fd, stat.st_mode) == -1) {
215
ERROR_GLIBC("fchmod");
216
return 1;
217
}
218
219
for (to_copy = stat.st_size; to_copy > 0; to_copy -= copied) {
220
copied = sendfile(dst_fd, src_fd, &offset, to_copy);
221
if (copied == -1) {
222
ERROR_GLIBC("sendfile");
223
return 1;
224
}
225
}
226
227
close(dst_fd);
228
close(src_fd);
229
return 0;
230
}
231
232
static void save_argv(int argc, const char **argv)
233
{
234
orig_argv = calloc(argc, sizeof(char *));
235
if (!orig_argv) {
236
ERROR_GLIBC("calloc");
237
exit(1);
238
}
239
240
for (int i = 0; i < argc; i++) {
241
orig_argv[i] = strdup(argv[i]);
242
if (!orig_argv[i]) {
243
ERROR_GLIBC("strdup(%s)", argv[i]);
244
exit(1);
245
}
246
};
247
}
248
249
void print_args(void)
250
{
251
char *backup = NULL;
252
253
if (opts.output || opts.dryrun)
254
goto print;
255
256
/*
257
* Make a backup before kbuild deletes the file so the error
258
* can be recreated without recompiling or relinking.
259
*/
260
backup = malloc(strlen(objname) + strlen(ORIG_SUFFIX) + 1);
261
if (!backup) {
262
ERROR_GLIBC("malloc");
263
goto print;
264
}
265
266
strcpy(backup, objname);
267
strcat(backup, ORIG_SUFFIX);
268
if (copy_file(objname, backup)) {
269
backup = NULL;
270
goto print;
271
}
272
273
print:
274
/*
275
* Print the cmdline args to make it easier to recreate. If '--output'
276
* wasn't used, add it to the printed args with the backup as input.
277
*/
278
fprintf(stderr, "%s", orig_argv[0]);
279
280
for (int i = 1; i < orig_argc; i++) {
281
char *arg = orig_argv[i];
282
283
if (backup && !strcmp(arg, objname))
284
fprintf(stderr, " %s -o %s", backup, objname);
285
else
286
fprintf(stderr, " %s", arg);
287
}
288
289
fprintf(stderr, "\n");
290
}
291
292
int objtool_run(int argc, const char **argv)
293
{
294
struct objtool_file *file;
295
int ret = 0;
296
297
orig_argc = argc;
298
save_argv(argc, argv);
299
300
cmd_parse_options(argc, argv, check_usage);
301
302
if (!opts_valid())
303
return 1;
304
305
objname = argv[0];
306
307
if (opts.dump_orc)
308
return orc_dump(objname);
309
310
if (!opts.dryrun && opts.output) {
311
/* copy original .o file to output file */
312
if (copy_file(objname, opts.output))
313
return 1;
314
315
/* from here on, work directly on the output file */
316
objname = opts.output;
317
}
318
319
file = objtool_open_read(objname);
320
if (!file)
321
return 1;
322
323
if (!opts.link && has_multiple_files(file->elf)) {
324
ERROR("Linked object requires --link");
325
return 1;
326
}
327
328
ret = check(file);
329
if (ret)
330
return ret;
331
332
if (!opts.dryrun && file->elf->changed && elf_write(file->elf))
333
return 1;
334
335
return 0;
336
}
337
338