Commit | Line | Data |
---|---|---|
1ccea77e | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
dcc914f4 JP |
2 | /* |
3 | * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> | |
dcc914f4 JP |
4 | */ |
5 | ||
6 | #include <string.h> | |
7 | #include <stdlib.h> | |
8 | ||
43a4525f | 9 | #include "builtin.h" |
0decf1f8 MH |
10 | #include "cfi.h" |
11 | #include "arch.h" | |
dcc914f4 | 12 | #include "check.h" |
dcc914f4 | 13 | #include "special.h" |
dcc914f4 | 14 | #include "warn.h" |
0f1441b4 | 15 | #include "arch_elf.h" |
dcc914f4 JP |
16 | |
17 | #include <linux/hashtable.h> | |
18 | #include <linux/kernel.h> | |
19 | ||
e6da9567 JP |
20 | #define FAKE_JUMP_OFFSET -1 |
21 | ||
87b512de JP |
22 | #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table" |
23 | ||
dcc914f4 JP |
24 | struct alternative { |
25 | struct list_head list; | |
26 | struct instruction *insn; | |
764eef4b | 27 | bool skip_orig; |
dcc914f4 JP |
28 | }; |
29 | ||
30 | const char *objname; | |
a3608f59 | 31 | struct cfi_init_state initial_func_cfi; |
dcc914f4 | 32 | |
627fce14 JP |
33 | struct instruction *find_insn(struct objtool_file *file, |
34 | struct section *sec, unsigned long offset) | |
dcc914f4 JP |
35 | { |
36 | struct instruction *insn; | |
37 | ||
87ecb582 | 38 | hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) { |
dcc914f4 JP |
39 | if (insn->sec == sec && insn->offset == offset) |
40 | return insn; | |
87ecb582 | 41 | } |
dcc914f4 JP |
42 | |
43 | return NULL; | |
44 | } | |
45 | ||
46 | static struct instruction *next_insn_same_sec(struct objtool_file *file, | |
47 | struct instruction *insn) | |
48 | { | |
49 | struct instruction *next = list_next_entry(insn, list); | |
50 | ||
baa41469 | 51 | if (!next || &next->list == &file->insn_list || next->sec != insn->sec) |
dcc914f4 JP |
52 | return NULL; |
53 | ||
54 | return next; | |
55 | } | |
56 | ||
13810435 JP |
57 | static struct instruction *next_insn_same_func(struct objtool_file *file, |
58 | struct instruction *insn) | |
59 | { | |
60 | struct instruction *next = list_next_entry(insn, list); | |
61 | struct symbol *func = insn->func; | |
62 | ||
63 | if (!func) | |
64 | return NULL; | |
65 | ||
66 | if (&next->list != &file->insn_list && next->func == func) | |
67 | return next; | |
68 | ||
69 | /* Check if we're already in the subfunction: */ | |
70 | if (func == func->cfunc) | |
71 | return NULL; | |
72 | ||
73 | /* Move to the subfunction: */ | |
74 | return find_insn(file, func->cfunc->sec, func->cfunc->offset); | |
75 | } | |
76 | ||
1119d265 JP |
77 | static struct instruction *prev_insn_same_sym(struct objtool_file *file, |
78 | struct instruction *insn) | |
79 | { | |
80 | struct instruction *prev = list_prev_entry(insn, list); | |
81 | ||
82 | if (&prev->list != &file->insn_list && prev->func == insn->func) | |
83 | return prev; | |
84 | ||
85 | return NULL; | |
86 | } | |
87 | ||
f0f70adb | 88 | #define func_for_each_insn(file, func, insn) \ |
13810435 JP |
89 | for (insn = find_insn(file, func->sec, func->offset); \ |
90 | insn; \ | |
91 | insn = next_insn_same_func(file, insn)) | |
92 | ||
dbf4aeb0 PZ |
93 | #define sym_for_each_insn(file, sym, insn) \ |
94 | for (insn = find_insn(file, sym->sec, sym->offset); \ | |
dcc914f4 | 95 | insn && &insn->list != &file->insn_list && \ |
dbf4aeb0 PZ |
96 | insn->sec == sym->sec && \ |
97 | insn->offset < sym->offset + sym->len; \ | |
dcc914f4 JP |
98 | insn = list_next_entry(insn, list)) |
99 | ||
dbf4aeb0 | 100 | #define sym_for_each_insn_continue_reverse(file, sym, insn) \ |
dcc914f4 JP |
101 | for (insn = list_prev_entry(insn, list); \ |
102 | &insn->list != &file->insn_list && \ | |
dbf4aeb0 | 103 | insn->sec == sym->sec && insn->offset >= sym->offset; \ |
dcc914f4 JP |
104 | insn = list_prev_entry(insn, list)) |
105 | ||
106 | #define sec_for_each_insn_from(file, insn) \ | |
107 | for (; insn; insn = next_insn_same_sec(file, insn)) | |
108 | ||
baa41469 JP |
109 | #define sec_for_each_insn_continue(file, insn) \ |
110 | for (insn = next_insn_same_sec(file, insn); insn; \ | |
111 | insn = next_insn_same_sec(file, insn)) | |
dcc914f4 | 112 | |
a2296140 JP |
113 | static bool is_static_jump(struct instruction *insn) |
114 | { | |
115 | return insn->type == INSN_JUMP_CONDITIONAL || | |
116 | insn->type == INSN_JUMP_UNCONDITIONAL; | |
117 | } | |
118 | ||
0c1ddd33 JP |
119 | static bool is_sibling_call(struct instruction *insn) |
120 | { | |
121 | /* An indirect jump is either a sibling call or a jump to a table. */ | |
122 | if (insn->type == INSN_JUMP_DYNAMIC) | |
123 | return list_empty(&insn->alts); | |
124 | ||
a2296140 | 125 | if (!is_static_jump(insn)) |
0c1ddd33 JP |
126 | return false; |
127 | ||
128 | /* add_jump_destinations() sets insn->call_dest for sibling calls. */ | |
129 | return !!insn->call_dest; | |
130 | } | |
131 | ||
dcc914f4 JP |
132 | /* |
133 | * This checks to see if the given function is a "noreturn" function. | |
134 | * | |
135 | * For global functions which are outside the scope of this object file, we | |
136 | * have to keep a manual list of them. | |
137 | * | |
138 | * For local functions, we have to detect them manually by simply looking for | |
139 | * the lack of a return instruction. | |
dcc914f4 | 140 | */ |
8e25c9f8 JP |
141 | static bool __dead_end_function(struct objtool_file *file, struct symbol *func, |
142 | int recursion) | |
dcc914f4 JP |
143 | { |
144 | int i; | |
145 | struct instruction *insn; | |
146 | bool empty = true; | |
147 | ||
148 | /* | |
149 | * Unfortunately these have to be hard coded because the noreturn | |
150 | * attribute isn't provided in ELF data. | |
151 | */ | |
152 | static const char * const global_noreturns[] = { | |
153 | "__stack_chk_fail", | |
154 | "panic", | |
155 | "do_exit", | |
156 | "do_task_dead", | |
157 | "__module_put_and_exit", | |
158 | "complete_and_exit", | |
dcc914f4 JP |
159 | "__reiserfs_panic", |
160 | "lbug_with_loc", | |
161 | "fortify_panic", | |
b394d468 | 162 | "usercopy_abort", |
684fb246 | 163 | "machine_real_restart", |
4fa5ecda | 164 | "rewind_stack_do_exit", |
33adf80f | 165 | "kunit_try_catch_throw", |
dcc914f4 JP |
166 | }; |
167 | ||
c9bab22b JP |
168 | if (!func) |
169 | return false; | |
170 | ||
dcc914f4 | 171 | if (func->bind == STB_WEAK) |
8e25c9f8 | 172 | return false; |
dcc914f4 JP |
173 | |
174 | if (func->bind == STB_GLOBAL) | |
175 | for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) | |
176 | if (!strcmp(func->name, global_noreturns[i])) | |
8e25c9f8 | 177 | return true; |
dcc914f4 | 178 | |
13810435 | 179 | if (!func->len) |
8e25c9f8 | 180 | return false; |
dcc914f4 | 181 | |
13810435 JP |
182 | insn = find_insn(file, func->sec, func->offset); |
183 | if (!insn->func) | |
8e25c9f8 | 184 | return false; |
13810435 | 185 | |
f0f70adb | 186 | func_for_each_insn(file, func, insn) { |
dcc914f4 JP |
187 | empty = false; |
188 | ||
189 | if (insn->type == INSN_RETURN) | |
8e25c9f8 | 190 | return false; |
dcc914f4 JP |
191 | } |
192 | ||
193 | if (empty) | |
8e25c9f8 | 194 | return false; |
dcc914f4 JP |
195 | |
196 | /* | |
197 | * A function can have a sibling call instead of a return. In that | |
198 | * case, the function's dead-end status depends on whether the target | |
199 | * of the sibling call returns. | |
200 | */ | |
f0f70adb | 201 | func_for_each_insn(file, func, insn) { |
0c1ddd33 | 202 | if (is_sibling_call(insn)) { |
dcc914f4 | 203 | struct instruction *dest = insn->jump_dest; |
dcc914f4 JP |
204 | |
205 | if (!dest) | |
206 | /* sibling call to another file */ | |
8e25c9f8 | 207 | return false; |
dcc914f4 | 208 | |
0c1ddd33 JP |
209 | /* local sibling call */ |
210 | if (recursion == 5) { | |
211 | /* | |
212 | * Infinite recursion: two functions have | |
213 | * sibling calls to each other. This is a very | |
214 | * rare case. It means they aren't dead ends. | |
215 | */ | |
216 | return false; | |
dcc914f4 | 217 | } |
dcc914f4 | 218 | |
0c1ddd33 JP |
219 | return __dead_end_function(file, dest->func, recursion+1); |
220 | } | |
dcc914f4 JP |
221 | } |
222 | ||
8e25c9f8 | 223 | return true; |
dcc914f4 JP |
224 | } |
225 | ||
8e25c9f8 | 226 | static bool dead_end_function(struct objtool_file *file, struct symbol *func) |
dcc914f4 JP |
227 | { |
228 | return __dead_end_function(file, func, 0); | |
229 | } | |
230 | ||
e7c0219b | 231 | static void init_cfi_state(struct cfi_state *cfi) |
baa41469 JP |
232 | { |
233 | int i; | |
234 | ||
dd88a0a0 | 235 | for (i = 0; i < CFI_NUM_REGS; i++) { |
e7c0219b PZ |
236 | cfi->regs[i].base = CFI_UNDEFINED; |
237 | cfi->vals[i].base = CFI_UNDEFINED; | |
dd88a0a0 | 238 | } |
e7c0219b PZ |
239 | cfi->cfa.base = CFI_UNDEFINED; |
240 | cfi->drap_reg = CFI_UNDEFINED; | |
241 | cfi->drap_offset = -1; | |
242 | } | |
243 | ||
932f8e98 | 244 | static void init_insn_state(struct insn_state *state, struct section *sec) |
e7c0219b PZ |
245 | { |
246 | memset(state, 0, sizeof(*state)); | |
247 | init_cfi_state(&state->cfi); | |
932f8e98 PZ |
248 | |
249 | /* | |
250 | * We need the full vmlinux for noinstr validation, otherwise we can | |
251 | * not correctly determine insn->call_dest->sec (external symbols do | |
252 | * not have a section). | |
253 | */ | |
254 | if (vmlinux && sec) | |
255 | state->noinstr = sec->noinstr; | |
baa41469 JP |
256 | } |
257 | ||
dcc914f4 JP |
258 | /* |
259 | * Call the arch-specific instruction decoder for all the instructions and add | |
260 | * them to the global instruction list. | |
261 | */ | |
262 | static int decode_instructions(struct objtool_file *file) | |
263 | { | |
264 | struct section *sec; | |
265 | struct symbol *func; | |
266 | unsigned long offset; | |
267 | struct instruction *insn; | |
1e11f3fd | 268 | unsigned long nr_insns = 0; |
dcc914f4 JP |
269 | int ret; |
270 | ||
baa41469 | 271 | for_each_sec(file, sec) { |
dcc914f4 JP |
272 | |
273 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) | |
274 | continue; | |
275 | ||
627fce14 JP |
276 | if (strcmp(sec->name, ".altinstr_replacement") && |
277 | strcmp(sec->name, ".altinstr_aux") && | |
278 | strncmp(sec->name, ".discard.", 9)) | |
279 | sec->text = true; | |
280 | ||
0cc9ac8d TG |
281 | if (!strcmp(sec->name, ".noinstr.text") || |
282 | !strcmp(sec->name, ".entry.text")) | |
c4a33939 PZ |
283 | sec->noinstr = true; |
284 | ||
dcc914f4 JP |
285 | for (offset = 0; offset < sec->len; offset += insn->len) { |
286 | insn = malloc(sizeof(*insn)); | |
baa41469 JP |
287 | if (!insn) { |
288 | WARN("malloc failed"); | |
289 | return -1; | |
290 | } | |
dcc914f4 | 291 | memset(insn, 0, sizeof(*insn)); |
dcc914f4 | 292 | INIT_LIST_HEAD(&insn->alts); |
65ea47dc | 293 | INIT_LIST_HEAD(&insn->stack_ops); |
e7c0219b | 294 | init_cfi_state(&insn->cfi); |
baa41469 | 295 | |
dcc914f4 JP |
296 | insn->sec = sec; |
297 | insn->offset = offset; | |
298 | ||
299 | ret = arch_decode_instruction(file->elf, sec, offset, | |
300 | sec->len - offset, | |
301 | &insn->len, &insn->type, | |
baa41469 | 302 | &insn->immediate, |
65ea47dc | 303 | &insn->stack_ops); |
dcc914f4 | 304 | if (ret) |
b7037983 | 305 | goto err; |
dcc914f4 | 306 | |
87ecb582 | 307 | hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset)); |
dcc914f4 | 308 | list_add_tail(&insn->list, &file->insn_list); |
1e11f3fd | 309 | nr_insns++; |
dcc914f4 JP |
310 | } |
311 | ||
312 | list_for_each_entry(func, &sec->symbol_list, list) { | |
e10cd8fe | 313 | if (func->type != STT_FUNC || func->alias != func) |
dcc914f4 JP |
314 | continue; |
315 | ||
316 | if (!find_insn(file, sec, func->offset)) { | |
317 | WARN("%s(): can't find starting instruction", | |
318 | func->name); | |
319 | return -1; | |
320 | } | |
321 | ||
dbf4aeb0 | 322 | sym_for_each_insn(file, func, insn) |
e10cd8fe | 323 | insn->func = func; |
dcc914f4 JP |
324 | } |
325 | } | |
326 | ||
1e11f3fd PZ |
327 | if (stats) |
328 | printf("nr_insns: %lu\n", nr_insns); | |
329 | ||
dcc914f4 | 330 | return 0; |
b7037983 KB |
331 | |
332 | err: | |
333 | free(insn); | |
334 | return ret; | |
dcc914f4 JP |
335 | } |
336 | ||
6b5dd716 ST |
337 | static struct instruction *find_last_insn(struct objtool_file *file, |
338 | struct section *sec) | |
339 | { | |
340 | struct instruction *insn = NULL; | |
341 | unsigned int offset; | |
342 | unsigned int end = (sec->len > 10) ? sec->len - 10 : 0; | |
343 | ||
344 | for (offset = sec->len - 1; offset >= end && !insn; offset--) | |
345 | insn = find_insn(file, sec, offset); | |
346 | ||
347 | return insn; | |
348 | } | |
349 | ||
dcc914f4 | 350 | /* |
649ea4d5 | 351 | * Mark "ud2" instructions and manually annotated dead ends. |
dcc914f4 JP |
352 | */ |
353 | static int add_dead_ends(struct objtool_file *file) | |
354 | { | |
355 | struct section *sec; | |
f1974222 | 356 | struct reloc *reloc; |
dcc914f4 | 357 | struct instruction *insn; |
dcc914f4 | 358 | |
649ea4d5 JP |
359 | /* |
360 | * By default, "ud2" is a dead end unless otherwise annotated, because | |
361 | * GCC 7 inserts it for certain divide-by-zero cases. | |
362 | */ | |
363 | for_each_insn(file, insn) | |
364 | if (insn->type == INSN_BUG) | |
365 | insn->dead_end = true; | |
366 | ||
367 | /* | |
368 | * Check for manually annotated dead ends. | |
369 | */ | |
dcc914f4 JP |
370 | sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); |
371 | if (!sec) | |
649ea4d5 | 372 | goto reachable; |
dcc914f4 | 373 | |
f1974222 MH |
374 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
375 | if (reloc->sym->type != STT_SECTION) { | |
dcc914f4 JP |
376 | WARN("unexpected relocation symbol type in %s", sec->name); |
377 | return -1; | |
378 | } | |
f1974222 | 379 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
dcc914f4 JP |
380 | if (insn) |
381 | insn = list_prev_entry(insn, list); | |
f1974222 MH |
382 | else if (reloc->addend == reloc->sym->sec->len) { |
383 | insn = find_last_insn(file, reloc->sym->sec); | |
6b5dd716 | 384 | if (!insn) { |
dcc914f4 | 385 | WARN("can't find unreachable insn at %s+0x%x", |
f1974222 | 386 | reloc->sym->sec->name, reloc->addend); |
dcc914f4 JP |
387 | return -1; |
388 | } | |
389 | } else { | |
390 | WARN("can't find unreachable insn at %s+0x%x", | |
f1974222 | 391 | reloc->sym->sec->name, reloc->addend); |
dcc914f4 JP |
392 | return -1; |
393 | } | |
394 | ||
395 | insn->dead_end = true; | |
396 | } | |
397 | ||
649ea4d5 JP |
398 | reachable: |
399 | /* | |
400 | * These manually annotated reachable checks are needed for GCC 4.4, | |
401 | * where the Linux unreachable() macro isn't supported. In that case | |
402 | * GCC doesn't know the "ud2" is fatal, so it generates code as if it's | |
403 | * not a dead end. | |
404 | */ | |
405 | sec = find_section_by_name(file->elf, ".rela.discard.reachable"); | |
406 | if (!sec) | |
407 | return 0; | |
408 | ||
f1974222 MH |
409 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
410 | if (reloc->sym->type != STT_SECTION) { | |
649ea4d5 JP |
411 | WARN("unexpected relocation symbol type in %s", sec->name); |
412 | return -1; | |
413 | } | |
f1974222 | 414 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
649ea4d5 JP |
415 | if (insn) |
416 | insn = list_prev_entry(insn, list); | |
f1974222 MH |
417 | else if (reloc->addend == reloc->sym->sec->len) { |
418 | insn = find_last_insn(file, reloc->sym->sec); | |
6b5dd716 | 419 | if (!insn) { |
649ea4d5 | 420 | WARN("can't find reachable insn at %s+0x%x", |
f1974222 | 421 | reloc->sym->sec->name, reloc->addend); |
649ea4d5 JP |
422 | return -1; |
423 | } | |
424 | } else { | |
425 | WARN("can't find reachable insn at %s+0x%x", | |
f1974222 | 426 | reloc->sym->sec->name, reloc->addend); |
649ea4d5 JP |
427 | return -1; |
428 | } | |
429 | ||
430 | insn->dead_end = false; | |
431 | } | |
432 | ||
dcc914f4 JP |
433 | return 0; |
434 | } | |
435 | ||
436 | /* | |
437 | * Warnings shouldn't be reported for ignored functions. | |
438 | */ | |
439 | static void add_ignores(struct objtool_file *file) | |
440 | { | |
441 | struct instruction *insn; | |
442 | struct section *sec; | |
443 | struct symbol *func; | |
f1974222 | 444 | struct reloc *reloc; |
dcc914f4 | 445 | |
aaf5c623 PZ |
446 | sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); |
447 | if (!sec) | |
448 | return; | |
dcc914f4 | 449 | |
f1974222 MH |
450 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
451 | switch (reloc->sym->type) { | |
aaf5c623 | 452 | case STT_FUNC: |
f1974222 | 453 | func = reloc->sym; |
aaf5c623 PZ |
454 | break; |
455 | ||
456 | case STT_SECTION: | |
f1974222 | 457 | func = find_func_by_offset(reloc->sym->sec, reloc->addend); |
7acfe531 | 458 | if (!func) |
dcc914f4 | 459 | continue; |
aaf5c623 | 460 | break; |
dcc914f4 | 461 | |
aaf5c623 | 462 | default: |
f1974222 | 463 | WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type); |
aaf5c623 | 464 | continue; |
dcc914f4 | 465 | } |
aaf5c623 | 466 | |
f0f70adb | 467 | func_for_each_insn(file, func, insn) |
aaf5c623 | 468 | insn->ignore = true; |
dcc914f4 JP |
469 | } |
470 | } | |
471 | ||
ea24213d PZ |
472 | /* |
473 | * This is a whitelist of functions that is allowed to be called with AC set. | |
474 | * The list is meant to be minimal and only contains compiler instrumentation | |
475 | * ABI and a few functions used to implement *_{to,from}_user() functions. | |
476 | * | |
477 | * These functions must not directly change AC, but may PUSHF/POPF. | |
478 | */ | |
479 | static const char *uaccess_safe_builtin[] = { | |
480 | /* KASAN */ | |
481 | "kasan_report", | |
482 | "check_memory_region", | |
483 | /* KASAN out-of-line */ | |
484 | "__asan_loadN_noabort", | |
485 | "__asan_load1_noabort", | |
486 | "__asan_load2_noabort", | |
487 | "__asan_load4_noabort", | |
488 | "__asan_load8_noabort", | |
489 | "__asan_load16_noabort", | |
490 | "__asan_storeN_noabort", | |
491 | "__asan_store1_noabort", | |
492 | "__asan_store2_noabort", | |
493 | "__asan_store4_noabort", | |
494 | "__asan_store8_noabort", | |
495 | "__asan_store16_noabort", | |
496 | /* KASAN in-line */ | |
497 | "__asan_report_load_n_noabort", | |
498 | "__asan_report_load1_noabort", | |
499 | "__asan_report_load2_noabort", | |
500 | "__asan_report_load4_noabort", | |
501 | "__asan_report_load8_noabort", | |
502 | "__asan_report_load16_noabort", | |
503 | "__asan_report_store_n_noabort", | |
504 | "__asan_report_store1_noabort", | |
505 | "__asan_report_store2_noabort", | |
506 | "__asan_report_store4_noabort", | |
507 | "__asan_report_store8_noabort", | |
508 | "__asan_report_store16_noabort", | |
5f5c9712 | 509 | /* KCSAN */ |
9967683c | 510 | "__kcsan_check_access", |
5f5c9712 ME |
511 | "kcsan_found_watchpoint", |
512 | "kcsan_setup_watchpoint", | |
9967683c | 513 | "kcsan_check_scoped_accesses", |
50a19ad4 ME |
514 | "kcsan_disable_current", |
515 | "kcsan_enable_current_nowarn", | |
5f5c9712 ME |
516 | /* KCSAN/TSAN */ |
517 | "__tsan_func_entry", | |
518 | "__tsan_func_exit", | |
519 | "__tsan_read_range", | |
520 | "__tsan_write_range", | |
521 | "__tsan_read1", | |
522 | "__tsan_read2", | |
523 | "__tsan_read4", | |
524 | "__tsan_read8", | |
525 | "__tsan_read16", | |
526 | "__tsan_write1", | |
527 | "__tsan_write2", | |
528 | "__tsan_write4", | |
529 | "__tsan_write8", | |
530 | "__tsan_write16", | |
883957b1 ME |
531 | "__tsan_atomic8_load", |
532 | "__tsan_atomic16_load", | |
533 | "__tsan_atomic32_load", | |
534 | "__tsan_atomic64_load", | |
535 | "__tsan_atomic8_store", | |
536 | "__tsan_atomic16_store", | |
537 | "__tsan_atomic32_store", | |
538 | "__tsan_atomic64_store", | |
539 | "__tsan_atomic8_exchange", | |
540 | "__tsan_atomic16_exchange", | |
541 | "__tsan_atomic32_exchange", | |
542 | "__tsan_atomic64_exchange", | |
543 | "__tsan_atomic8_fetch_add", | |
544 | "__tsan_atomic16_fetch_add", | |
545 | "__tsan_atomic32_fetch_add", | |
546 | "__tsan_atomic64_fetch_add", | |
547 | "__tsan_atomic8_fetch_sub", | |
548 | "__tsan_atomic16_fetch_sub", | |
549 | "__tsan_atomic32_fetch_sub", | |
550 | "__tsan_atomic64_fetch_sub", | |
551 | "__tsan_atomic8_fetch_and", | |
552 | "__tsan_atomic16_fetch_and", | |
553 | "__tsan_atomic32_fetch_and", | |
554 | "__tsan_atomic64_fetch_and", | |
555 | "__tsan_atomic8_fetch_or", | |
556 | "__tsan_atomic16_fetch_or", | |
557 | "__tsan_atomic32_fetch_or", | |
558 | "__tsan_atomic64_fetch_or", | |
559 | "__tsan_atomic8_fetch_xor", | |
560 | "__tsan_atomic16_fetch_xor", | |
561 | "__tsan_atomic32_fetch_xor", | |
562 | "__tsan_atomic64_fetch_xor", | |
563 | "__tsan_atomic8_fetch_nand", | |
564 | "__tsan_atomic16_fetch_nand", | |
565 | "__tsan_atomic32_fetch_nand", | |
566 | "__tsan_atomic64_fetch_nand", | |
567 | "__tsan_atomic8_compare_exchange_strong", | |
568 | "__tsan_atomic16_compare_exchange_strong", | |
569 | "__tsan_atomic32_compare_exchange_strong", | |
570 | "__tsan_atomic64_compare_exchange_strong", | |
571 | "__tsan_atomic8_compare_exchange_weak", | |
572 | "__tsan_atomic16_compare_exchange_weak", | |
573 | "__tsan_atomic32_compare_exchange_weak", | |
574 | "__tsan_atomic64_compare_exchange_weak", | |
575 | "__tsan_atomic8_compare_exchange_val", | |
576 | "__tsan_atomic16_compare_exchange_val", | |
577 | "__tsan_atomic32_compare_exchange_val", | |
578 | "__tsan_atomic64_compare_exchange_val", | |
579 | "__tsan_atomic_thread_fence", | |
580 | "__tsan_atomic_signal_fence", | |
ea24213d PZ |
581 | /* KCOV */ |
582 | "write_comp_data", | |
ae033f08 | 583 | "check_kcov_mode", |
ea24213d PZ |
584 | "__sanitizer_cov_trace_pc", |
585 | "__sanitizer_cov_trace_const_cmp1", | |
586 | "__sanitizer_cov_trace_const_cmp2", | |
587 | "__sanitizer_cov_trace_const_cmp4", | |
588 | "__sanitizer_cov_trace_const_cmp8", | |
589 | "__sanitizer_cov_trace_cmp1", | |
590 | "__sanitizer_cov_trace_cmp2", | |
591 | "__sanitizer_cov_trace_cmp4", | |
592 | "__sanitizer_cov_trace_cmp8", | |
36b1c700 | 593 | "__sanitizer_cov_trace_switch", |
ea24213d PZ |
594 | /* UBSAN */ |
595 | "ubsan_type_mismatch_common", | |
596 | "__ubsan_handle_type_mismatch", | |
597 | "__ubsan_handle_type_mismatch_v1", | |
9a50dcaf | 598 | "__ubsan_handle_shift_out_of_bounds", |
ea24213d PZ |
599 | /* misc */ |
600 | "csum_partial_copy_generic", | |
601 | "__memcpy_mcsafe", | |
a7e47f26 | 602 | "mcsafe_handle_tail", |
ea24213d PZ |
603 | "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */ |
604 | NULL | |
605 | }; | |
606 | ||
607 | static void add_uaccess_safe(struct objtool_file *file) | |
608 | { | |
609 | struct symbol *func; | |
610 | const char **name; | |
611 | ||
612 | if (!uaccess) | |
613 | return; | |
614 | ||
615 | for (name = uaccess_safe_builtin; *name; name++) { | |
616 | func = find_symbol_by_name(file->elf, *name); | |
617 | if (!func) | |
618 | continue; | |
619 | ||
e10cd8fe | 620 | func->uaccess_safe = true; |
dcc914f4 JP |
621 | } |
622 | } | |
623 | ||
258c7605 JP |
624 | /* |
625 | * FIXME: For now, just ignore any alternatives which add retpolines. This is | |
626 | * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline. | |
627 | * But it at least allows objtool to understand the control flow *around* the | |
628 | * retpoline. | |
629 | */ | |
ff05ab23 | 630 | static int add_ignore_alternatives(struct objtool_file *file) |
258c7605 JP |
631 | { |
632 | struct section *sec; | |
f1974222 | 633 | struct reloc *reloc; |
258c7605 JP |
634 | struct instruction *insn; |
635 | ||
ff05ab23 | 636 | sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts"); |
258c7605 JP |
637 | if (!sec) |
638 | return 0; | |
639 | ||
f1974222 MH |
640 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
641 | if (reloc->sym->type != STT_SECTION) { | |
258c7605 JP |
642 | WARN("unexpected relocation symbol type in %s", sec->name); |
643 | return -1; | |
644 | } | |
645 | ||
f1974222 | 646 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
258c7605 | 647 | if (!insn) { |
ff05ab23 | 648 | WARN("bad .discard.ignore_alts entry"); |
258c7605 JP |
649 | return -1; |
650 | } | |
651 | ||
652 | insn->ignore_alts = true; | |
653 | } | |
654 | ||
655 | return 0; | |
656 | } | |
657 | ||
dcc914f4 JP |
658 | /* |
659 | * Find the destination instructions for all jumps. | |
660 | */ | |
661 | static int add_jump_destinations(struct objtool_file *file) | |
662 | { | |
663 | struct instruction *insn; | |
f1974222 | 664 | struct reloc *reloc; |
dcc914f4 JP |
665 | struct section *dest_sec; |
666 | unsigned long dest_off; | |
667 | ||
668 | for_each_insn(file, insn) { | |
a2296140 | 669 | if (!is_static_jump(insn)) |
dcc914f4 JP |
670 | continue; |
671 | ||
e6da9567 | 672 | if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET) |
dcc914f4 JP |
673 | continue; |
674 | ||
f1974222 | 675 | reloc = find_reloc_by_dest_range(file->elf, insn->sec, |
8b5fa6bc | 676 | insn->offset, insn->len); |
f1974222 | 677 | if (!reloc) { |
dcc914f4 | 678 | dest_sec = insn->sec; |
bfb08f22 | 679 | dest_off = arch_jump_destination(insn); |
f1974222 MH |
680 | } else if (reloc->sym->type == STT_SECTION) { |
681 | dest_sec = reloc->sym->sec; | |
682 | dest_off = arch_dest_reloc_offset(reloc->addend); | |
683 | } else if (reloc->sym->sec->idx) { | |
684 | dest_sec = reloc->sym->sec; | |
685 | dest_off = reloc->sym->sym.st_value + | |
686 | arch_dest_reloc_offset(reloc->addend); | |
687 | } else if (strstr(reloc->sym->name, "_indirect_thunk_")) { | |
39b73533 JP |
688 | /* |
689 | * Retpoline jumps are really dynamic jumps in | |
690 | * disguise, so convert them accordingly. | |
691 | */ | |
b68b9907 JP |
692 | if (insn->type == INSN_JUMP_UNCONDITIONAL) |
693 | insn->type = INSN_JUMP_DYNAMIC; | |
694 | else | |
695 | insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL; | |
696 | ||
b5bc2231 | 697 | insn->retpoline_safe = true; |
39b73533 | 698 | continue; |
dcc914f4 | 699 | } else { |
0c1ddd33 | 700 | /* external sibling call */ |
f1974222 | 701 | insn->call_dest = reloc->sym; |
dcc914f4 JP |
702 | continue; |
703 | } | |
704 | ||
705 | insn->jump_dest = find_insn(file, dest_sec, dest_off); | |
706 | if (!insn->jump_dest) { | |
707 | ||
708 | /* | |
709 | * This is a special case where an alt instruction | |
710 | * jumps past the end of the section. These are | |
711 | * handled later in handle_group_alt(). | |
712 | */ | |
713 | if (!strcmp(insn->sec->name, ".altinstr_replacement")) | |
714 | continue; | |
715 | ||
716 | WARN_FUNC("can't find jump dest instruction at %s+0x%lx", | |
717 | insn->sec, insn->offset, dest_sec->name, | |
718 | dest_off); | |
719 | return -1; | |
720 | } | |
cd77849a JP |
721 | |
722 | /* | |
54262aa2 | 723 | * Cross-function jump. |
cd77849a JP |
724 | */ |
725 | if (insn->func && insn->jump_dest->func && | |
54262aa2 PZ |
726 | insn->func != insn->jump_dest->func) { |
727 | ||
728 | /* | |
729 | * For GCC 8+, create parent/child links for any cold | |
730 | * subfunctions. This is _mostly_ redundant with a | |
731 | * similar initialization in read_symbols(). | |
732 | * | |
733 | * If a function has aliases, we want the *first* such | |
734 | * function in the symbol table to be the subfunction's | |
735 | * parent. In that case we overwrite the | |
736 | * initialization done in read_symbols(). | |
737 | * | |
738 | * However this code can't completely replace the | |
739 | * read_symbols() code because this doesn't detect the | |
740 | * case where the parent function's only reference to a | |
e7c2bc37 | 741 | * subfunction is through a jump table. |
54262aa2 PZ |
742 | */ |
743 | if (!strstr(insn->func->name, ".cold.") && | |
744 | strstr(insn->jump_dest->func->name, ".cold.")) { | |
745 | insn->func->cfunc = insn->jump_dest->func; | |
746 | insn->jump_dest->func->pfunc = insn->func; | |
747 | ||
748 | } else if (insn->jump_dest->func->pfunc != insn->func->pfunc && | |
749 | insn->jump_dest->offset == insn->jump_dest->func->offset) { | |
750 | ||
0c1ddd33 | 751 | /* internal sibling call */ |
54262aa2 | 752 | insn->call_dest = insn->jump_dest->func; |
54262aa2 | 753 | } |
cd77849a | 754 | } |
dcc914f4 JP |
755 | } |
756 | ||
757 | return 0; | |
758 | } | |
759 | ||
8aa8eb2a AC |
760 | static void remove_insn_ops(struct instruction *insn) |
761 | { | |
762 | struct stack_op *op, *tmp; | |
763 | ||
764 | list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) { | |
765 | list_del(&op->list); | |
766 | free(op); | |
767 | } | |
768 | } | |
769 | ||
dcc914f4 JP |
770 | /* |
771 | * Find the destination instructions for all calls. | |
772 | */ | |
773 | static int add_call_destinations(struct objtool_file *file) | |
774 | { | |
775 | struct instruction *insn; | |
776 | unsigned long dest_off; | |
f1974222 | 777 | struct reloc *reloc; |
dcc914f4 JP |
778 | |
779 | for_each_insn(file, insn) { | |
780 | if (insn->type != INSN_CALL) | |
781 | continue; | |
782 | ||
f1974222 | 783 | reloc = find_reloc_by_dest_range(file->elf, insn->sec, |
8b5fa6bc | 784 | insn->offset, insn->len); |
f1974222 | 785 | if (!reloc) { |
bfb08f22 | 786 | dest_off = arch_jump_destination(insn); |
7acfe531 JP |
787 | insn->call_dest = find_func_by_offset(insn->sec, dest_off); |
788 | if (!insn->call_dest) | |
789 | insn->call_dest = find_symbol_by_offset(insn->sec, dest_off); | |
a845c7cf | 790 | |
7acfe531 JP |
791 | if (insn->ignore) |
792 | continue; | |
793 | ||
794 | if (!insn->call_dest) { | |
8aa8eb2a | 795 | WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset); |
dcc914f4 JP |
796 | return -1; |
797 | } | |
a845c7cf | 798 | |
7acfe531 JP |
799 | if (insn->func && insn->call_dest->type != STT_FUNC) { |
800 | WARN_FUNC("unsupported call to non-function", | |
801 | insn->sec, insn->offset); | |
802 | return -1; | |
803 | } | |
804 | ||
f1974222 MH |
805 | } else if (reloc->sym->type == STT_SECTION) { |
806 | dest_off = arch_dest_reloc_offset(reloc->addend); | |
807 | insn->call_dest = find_func_by_offset(reloc->sym->sec, | |
bfb08f22 | 808 | dest_off); |
7acfe531 | 809 | if (!insn->call_dest) { |
bfb08f22 | 810 | WARN_FUNC("can't find call dest symbol at %s+0x%lx", |
dcc914f4 | 811 | insn->sec, insn->offset, |
f1974222 | 812 | reloc->sym->sec->name, |
bfb08f22 | 813 | dest_off); |
dcc914f4 JP |
814 | return -1; |
815 | } | |
816 | } else | |
f1974222 | 817 | insn->call_dest = reloc->sym; |
8aa8eb2a | 818 | |
0f1441b4 PZ |
819 | /* |
820 | * Many compilers cannot disable KCOV with a function attribute | |
821 | * so they need a little help, NOP out any KCOV calls from noinstr | |
822 | * text. | |
823 | */ | |
824 | if (insn->sec->noinstr && | |
825 | !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) { | |
d832c005 PZ |
826 | if (reloc) { |
827 | reloc->type = R_NONE; | |
828 | elf_write_reloc(file->elf, reloc); | |
0f1441b4 PZ |
829 | } |
830 | ||
831 | elf_write_insn(file->elf, insn->sec, | |
832 | insn->offset, insn->len, | |
833 | arch_nop_insn(insn->len)); | |
834 | insn->type = INSN_NOP; | |
835 | } | |
836 | ||
8aa8eb2a AC |
837 | /* |
838 | * Whatever stack impact regular CALLs have, should be undone | |
839 | * by the RETURN of the called function. | |
840 | * | |
841 | * Annotated intra-function calls retain the stack_ops but | |
842 | * are converted to JUMP, see read_intra_function_calls(). | |
843 | */ | |
844 | remove_insn_ops(insn); | |
dcc914f4 JP |
845 | } |
846 | ||
847 | return 0; | |
848 | } | |
849 | ||
850 | /* | |
851 | * The .alternatives section requires some extra special care, over and above | |
852 | * what other special sections require: | |
853 | * | |
854 | * 1. Because alternatives are patched in-place, we need to insert a fake jump | |
855 | * instruction at the end so that validate_branch() skips all the original | |
856 | * replaced instructions when validating the new instruction path. | |
857 | * | |
858 | * 2. An added wrinkle is that the new instruction length might be zero. In | |
859 | * that case the old instructions are replaced with noops. We simulate that | |
860 | * by creating a fake jump as the only new instruction. | |
861 | * | |
862 | * 3. In some cases, the alternative section includes an instruction which | |
863 | * conditionally jumps to the _end_ of the entry. We have to modify these | |
864 | * jumps' destinations to point back to .text rather than the end of the | |
865 | * entry in .altinstr_replacement. | |
dcc914f4 JP |
866 | */ |
867 | static int handle_group_alt(struct objtool_file *file, | |
868 | struct special_alt *special_alt, | |
869 | struct instruction *orig_insn, | |
870 | struct instruction **new_insn) | |
871 | { | |
13fab06d | 872 | static unsigned int alt_group_next_index = 1; |
17bc3391 | 873 | struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL; |
13fab06d | 874 | unsigned int alt_group = alt_group_next_index++; |
dcc914f4 JP |
875 | unsigned long dest_off; |
876 | ||
877 | last_orig_insn = NULL; | |
878 | insn = orig_insn; | |
879 | sec_for_each_insn_from(file, insn) { | |
880 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) | |
881 | break; | |
882 | ||
13fab06d | 883 | insn->alt_group = alt_group; |
dcc914f4 JP |
884 | last_orig_insn = insn; |
885 | } | |
886 | ||
17bc3391 JP |
887 | if (next_insn_same_sec(file, last_orig_insn)) { |
888 | fake_jump = malloc(sizeof(*fake_jump)); | |
889 | if (!fake_jump) { | |
890 | WARN("malloc failed"); | |
891 | return -1; | |
892 | } | |
893 | memset(fake_jump, 0, sizeof(*fake_jump)); | |
894 | INIT_LIST_HEAD(&fake_jump->alts); | |
65ea47dc | 895 | INIT_LIST_HEAD(&fake_jump->stack_ops); |
e7c0219b | 896 | init_cfi_state(&fake_jump->cfi); |
17bc3391 JP |
897 | |
898 | fake_jump->sec = special_alt->new_sec; | |
e6da9567 | 899 | fake_jump->offset = FAKE_JUMP_OFFSET; |
17bc3391 JP |
900 | fake_jump->type = INSN_JUMP_UNCONDITIONAL; |
901 | fake_jump->jump_dest = list_next_entry(last_orig_insn, list); | |
e6da9567 | 902 | fake_jump->func = orig_insn->func; |
dcc914f4 | 903 | } |
dcc914f4 JP |
904 | |
905 | if (!special_alt->new_len) { | |
17bc3391 JP |
906 | if (!fake_jump) { |
907 | WARN("%s: empty alternative at end of section", | |
908 | special_alt->orig_sec->name); | |
909 | return -1; | |
910 | } | |
911 | ||
dcc914f4 JP |
912 | *new_insn = fake_jump; |
913 | return 0; | |
914 | } | |
915 | ||
916 | last_new_insn = NULL; | |
13fab06d | 917 | alt_group = alt_group_next_index++; |
dcc914f4 JP |
918 | insn = *new_insn; |
919 | sec_for_each_insn_from(file, insn) { | |
920 | if (insn->offset >= special_alt->new_off + special_alt->new_len) | |
921 | break; | |
922 | ||
923 | last_new_insn = insn; | |
924 | ||
a845c7cf | 925 | insn->ignore = orig_insn->ignore_alts; |
a4d09dde | 926 | insn->func = orig_insn->func; |
13fab06d | 927 | insn->alt_group = alt_group; |
a845c7cf | 928 | |
dc419723 JP |
929 | /* |
930 | * Since alternative replacement code is copy/pasted by the | |
931 | * kernel after applying relocations, generally such code can't | |
932 | * have relative-address relocation references to outside the | |
933 | * .altinstr_replacement section, unless the arch's | |
934 | * alternatives code can adjust the relative offsets | |
935 | * accordingly. | |
936 | * | |
937 | * The x86 alternatives code adjusts the offsets only when it | |
938 | * encounters a branch instruction at the very beginning of the | |
939 | * replacement group. | |
940 | */ | |
941 | if ((insn->offset != special_alt->new_off || | |
942 | (insn->type != INSN_CALL && !is_static_jump(insn))) && | |
f1974222 | 943 | find_reloc_by_dest_range(file->elf, insn->sec, insn->offset, insn->len)) { |
dc419723 JP |
944 | |
945 | WARN_FUNC("unsupported relocation in alternatives section", | |
946 | insn->sec, insn->offset); | |
947 | return -1; | |
948 | } | |
949 | ||
a2296140 | 950 | if (!is_static_jump(insn)) |
dcc914f4 JP |
951 | continue; |
952 | ||
953 | if (!insn->immediate) | |
954 | continue; | |
955 | ||
bfb08f22 | 956 | dest_off = arch_jump_destination(insn); |
17bc3391 JP |
957 | if (dest_off == special_alt->new_off + special_alt->new_len) { |
958 | if (!fake_jump) { | |
959 | WARN("%s: alternative jump to end of section", | |
960 | special_alt->orig_sec->name); | |
961 | return -1; | |
962 | } | |
dcc914f4 | 963 | insn->jump_dest = fake_jump; |
17bc3391 | 964 | } |
dcc914f4 JP |
965 | |
966 | if (!insn->jump_dest) { | |
967 | WARN_FUNC("can't find alternative jump destination", | |
968 | insn->sec, insn->offset); | |
969 | return -1; | |
970 | } | |
971 | } | |
972 | ||
973 | if (!last_new_insn) { | |
974 | WARN_FUNC("can't find last new alternative instruction", | |
975 | special_alt->new_sec, special_alt->new_off); | |
976 | return -1; | |
977 | } | |
978 | ||
17bc3391 JP |
979 | if (fake_jump) |
980 | list_add(&fake_jump->list, &last_new_insn->list); | |
dcc914f4 JP |
981 | |
982 | return 0; | |
983 | } | |
984 | ||
985 | /* | |
986 | * A jump table entry can either convert a nop to a jump or a jump to a nop. | |
987 | * If the original instruction is a jump, make the alt entry an effective nop | |
988 | * by just skipping the original instruction. | |
989 | */ | |
990 | static int handle_jump_alt(struct objtool_file *file, | |
991 | struct special_alt *special_alt, | |
992 | struct instruction *orig_insn, | |
993 | struct instruction **new_insn) | |
994 | { | |
995 | if (orig_insn->type == INSN_NOP) | |
996 | return 0; | |
997 | ||
998 | if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { | |
999 | WARN_FUNC("unsupported instruction at jump label", | |
1000 | orig_insn->sec, orig_insn->offset); | |
1001 | return -1; | |
1002 | } | |
1003 | ||
1004 | *new_insn = list_next_entry(orig_insn, list); | |
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | /* | |
1009 | * Read all the special sections which have alternate instructions which can be | |
1010 | * patched in or redirected to at runtime. Each instruction having alternate | |
1011 | * instruction(s) has them added to its insn->alts list, which will be | |
1012 | * traversed in validate_branch(). | |
1013 | */ | |
1014 | static int add_special_section_alts(struct objtool_file *file) | |
1015 | { | |
1016 | struct list_head special_alts; | |
1017 | struct instruction *orig_insn, *new_insn; | |
1018 | struct special_alt *special_alt, *tmp; | |
1019 | struct alternative *alt; | |
1020 | int ret; | |
1021 | ||
1022 | ret = special_get_alts(file->elf, &special_alts); | |
1023 | if (ret) | |
1024 | return ret; | |
1025 | ||
1026 | list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { | |
dcc914f4 JP |
1027 | |
1028 | orig_insn = find_insn(file, special_alt->orig_sec, | |
1029 | special_alt->orig_off); | |
1030 | if (!orig_insn) { | |
1031 | WARN_FUNC("special: can't find orig instruction", | |
1032 | special_alt->orig_sec, special_alt->orig_off); | |
1033 | ret = -1; | |
1034 | goto out; | |
1035 | } | |
1036 | ||
1037 | new_insn = NULL; | |
1038 | if (!special_alt->group || special_alt->new_len) { | |
1039 | new_insn = find_insn(file, special_alt->new_sec, | |
1040 | special_alt->new_off); | |
1041 | if (!new_insn) { | |
1042 | WARN_FUNC("special: can't find new instruction", | |
1043 | special_alt->new_sec, | |
1044 | special_alt->new_off); | |
1045 | ret = -1; | |
1046 | goto out; | |
1047 | } | |
1048 | } | |
1049 | ||
1050 | if (special_alt->group) { | |
7170cf47 JT |
1051 | if (!special_alt->orig_len) { |
1052 | WARN_FUNC("empty alternative entry", | |
1053 | orig_insn->sec, orig_insn->offset); | |
1054 | continue; | |
1055 | } | |
1056 | ||
dcc914f4 JP |
1057 | ret = handle_group_alt(file, special_alt, orig_insn, |
1058 | &new_insn); | |
1059 | if (ret) | |
1060 | goto out; | |
1061 | } else if (special_alt->jump_or_nop) { | |
1062 | ret = handle_jump_alt(file, special_alt, orig_insn, | |
1063 | &new_insn); | |
1064 | if (ret) | |
1065 | goto out; | |
1066 | } | |
1067 | ||
258c7605 JP |
1068 | alt = malloc(sizeof(*alt)); |
1069 | if (!alt) { | |
1070 | WARN("malloc failed"); | |
1071 | ret = -1; | |
1072 | goto out; | |
1073 | } | |
1074 | ||
dcc914f4 | 1075 | alt->insn = new_insn; |
764eef4b | 1076 | alt->skip_orig = special_alt->skip_orig; |
ea24213d | 1077 | orig_insn->ignore_alts |= special_alt->skip_alt; |
dcc914f4 JP |
1078 | list_add_tail(&alt->list, &orig_insn->alts); |
1079 | ||
1080 | list_del(&special_alt->list); | |
1081 | free(special_alt); | |
1082 | } | |
1083 | ||
1084 | out: | |
1085 | return ret; | |
1086 | } | |
1087 | ||
e7c2bc37 | 1088 | static int add_jump_table(struct objtool_file *file, struct instruction *insn, |
f1974222 | 1089 | struct reloc *table) |
dcc914f4 | 1090 | { |
f1974222 | 1091 | struct reloc *reloc = table; |
e7c2bc37 | 1092 | struct instruction *dest_insn; |
dcc914f4 | 1093 | struct alternative *alt; |
fd35c88b JP |
1094 | struct symbol *pfunc = insn->func->pfunc; |
1095 | unsigned int prev_offset = 0; | |
dcc914f4 | 1096 | |
e7c2bc37 | 1097 | /* |
f1974222 | 1098 | * Each @reloc is a switch table relocation which points to the target |
e7c2bc37 JP |
1099 | * instruction. |
1100 | */ | |
f1974222 | 1101 | list_for_each_entry_from(reloc, &table->sec->reloc_list, list) { |
bd98c813 JH |
1102 | |
1103 | /* Check for the end of the table: */ | |
f1974222 | 1104 | if (reloc != table && reloc->jump_table_start) |
dcc914f4 JP |
1105 | break; |
1106 | ||
e7c2bc37 | 1107 | /* Make sure the table entries are consecutive: */ |
f1974222 | 1108 | if (prev_offset && reloc->offset != prev_offset + 8) |
fd35c88b JP |
1109 | break; |
1110 | ||
1111 | /* Detect function pointers from contiguous objects: */ | |
f1974222 MH |
1112 | if (reloc->sym->sec == pfunc->sec && |
1113 | reloc->addend == pfunc->offset) | |
fd35c88b JP |
1114 | break; |
1115 | ||
f1974222 | 1116 | dest_insn = find_insn(file, reloc->sym->sec, reloc->addend); |
e7c2bc37 | 1117 | if (!dest_insn) |
dcc914f4 JP |
1118 | break; |
1119 | ||
e7c2bc37 | 1120 | /* Make sure the destination is in the same function: */ |
e65050b9 | 1121 | if (!dest_insn->func || dest_insn->func->pfunc != pfunc) |
13810435 | 1122 | break; |
dcc914f4 JP |
1123 | |
1124 | alt = malloc(sizeof(*alt)); | |
1125 | if (!alt) { | |
1126 | WARN("malloc failed"); | |
1127 | return -1; | |
1128 | } | |
1129 | ||
e7c2bc37 | 1130 | alt->insn = dest_insn; |
dcc914f4 | 1131 | list_add_tail(&alt->list, &insn->alts); |
f1974222 | 1132 | prev_offset = reloc->offset; |
fd35c88b JP |
1133 | } |
1134 | ||
1135 | if (!prev_offset) { | |
1136 | WARN_FUNC("can't find switch jump table", | |
1137 | insn->sec, insn->offset); | |
1138 | return -1; | |
dcc914f4 JP |
1139 | } |
1140 | ||
1141 | return 0; | |
1142 | } | |
1143 | ||
1144 | /* | |
e7c2bc37 | 1145 | * find_jump_table() - Given a dynamic jump, find the switch jump table in |
dcc914f4 JP |
1146 | * .rodata associated with it. |
1147 | * | |
1148 | * There are 3 basic patterns: | |
1149 | * | |
1150 | * 1. jmpq *[rodata addr](,%reg,8) | |
1151 | * | |
1152 | * This is the most common case by far. It jumps to an address in a simple | |
1153 | * jump table which is stored in .rodata. | |
1154 | * | |
1155 | * 2. jmpq *[rodata addr](%rip) | |
1156 | * | |
1157 | * This is caused by a rare GCC quirk, currently only seen in three driver | |
1158 | * functions in the kernel, only with certain obscure non-distro configs. | |
1159 | * | |
1160 | * As part of an optimization, GCC makes a copy of an existing switch jump | |
1161 | * table, modifies it, and then hard-codes the jump (albeit with an indirect | |
1162 | * jump) to use a single entry in the table. The rest of the jump table and | |
1163 | * some of its jump targets remain as dead code. | |
1164 | * | |
1165 | * In such a case we can just crudely ignore all unreachable instruction | |
1166 | * warnings for the entire object file. Ideally we would just ignore them | |
1167 | * for the function, but that would require redesigning the code quite a | |
1168 | * bit. And honestly that's just not worth doing: unreachable instruction | |
1169 | * warnings are of questionable value anyway, and this is such a rare issue. | |
1170 | * | |
1171 | * 3. mov [rodata addr],%reg1 | |
1172 | * ... some instructions ... | |
1173 | * jmpq *(%reg1,%reg2,8) | |
1174 | * | |
1175 | * This is a fairly uncommon pattern which is new for GCC 6. As of this | |
1176 | * writing, there are 11 occurrences of it in the allmodconfig kernel. | |
1177 | * | |
99ce7962 PZ |
1178 | * As of GCC 7 there are quite a few more of these and the 'in between' code |
1179 | * is significant. Esp. with KASAN enabled some of the code between the mov | |
1180 | * and jmpq uses .rodata itself, which can confuse things. | |
1181 | * | |
dcc914f4 JP |
1182 | * TODO: Once we have DWARF CFI and smarter instruction decoding logic, |
1183 | * ensure the same register is used in the mov and jump instructions. | |
99ce7962 PZ |
1184 | * |
1185 | * NOTE: RETPOLINE made it harder still to decode dynamic jumps. | |
dcc914f4 | 1186 | */ |
f1974222 | 1187 | static struct reloc *find_jump_table(struct objtool_file *file, |
dcc914f4 JP |
1188 | struct symbol *func, |
1189 | struct instruction *insn) | |
1190 | { | |
f1974222 | 1191 | struct reloc *text_reloc, *table_reloc; |
113d4bc9 | 1192 | struct instruction *dest_insn, *orig_insn = insn; |
e7c2bc37 | 1193 | struct section *table_sec; |
6f5ec299 | 1194 | unsigned long table_offset; |
dcc914f4 | 1195 | |
99ce7962 PZ |
1196 | /* |
1197 | * Backward search using the @first_jump_src links, these help avoid | |
1198 | * much of the 'in between' code. Which avoids us getting confused by | |
1199 | * it. | |
1200 | */ | |
7dec80cc | 1201 | for (; |
1119d265 JP |
1202 | insn && insn->func && insn->func->pfunc == func; |
1203 | insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) { | |
99ce7962 | 1204 | |
7dec80cc | 1205 | if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC) |
dcc914f4 JP |
1206 | break; |
1207 | ||
1208 | /* allow small jumps within the range */ | |
1209 | if (insn->type == INSN_JUMP_UNCONDITIONAL && | |
1210 | insn->jump_dest && | |
1211 | (insn->jump_dest->offset <= insn->offset || | |
1212 | insn->jump_dest->offset > orig_insn->offset)) | |
1213 | break; | |
1214 | ||
1215 | /* look for a relocation which references .rodata */ | |
f1974222 | 1216 | text_reloc = find_reloc_by_dest_range(file->elf, insn->sec, |
8b5fa6bc | 1217 | insn->offset, insn->len); |
f1974222 MH |
1218 | if (!text_reloc || text_reloc->sym->type != STT_SECTION || |
1219 | !text_reloc->sym->sec->rodata) | |
dcc914f4 JP |
1220 | continue; |
1221 | ||
f1974222 MH |
1222 | table_offset = text_reloc->addend; |
1223 | table_sec = text_reloc->sym->sec; | |
4a60aa05 | 1224 | |
f1974222 | 1225 | if (text_reloc->type == R_X86_64_PC32) |
6f5ec299 JP |
1226 | table_offset += 4; |
1227 | ||
dcc914f4 JP |
1228 | /* |
1229 | * Make sure the .rodata address isn't associated with a | |
87b512de JP |
1230 | * symbol. GCC jump tables are anonymous data. |
1231 | * | |
1232 | * Also support C jump tables which are in the same format as | |
1233 | * switch jump tables. For objtool to recognize them, they | |
1234 | * need to be placed in the C_JUMP_TABLE_SECTION section. They | |
1235 | * have symbols associated with them. | |
dcc914f4 | 1236 | */ |
e7c2bc37 JP |
1237 | if (find_symbol_containing(table_sec, table_offset) && |
1238 | strcmp(table_sec->name, C_JUMP_TABLE_SECTION)) | |
1402fd8e JP |
1239 | continue; |
1240 | ||
113d4bc9 | 1241 | /* |
f1974222 | 1242 | * Each table entry has a reloc associated with it. The reloc |
113d4bc9 JP |
1243 | * should reference text in the same function as the original |
1244 | * instruction. | |
1245 | */ | |
f1974222 MH |
1246 | table_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset); |
1247 | if (!table_reloc) | |
e7c2bc37 | 1248 | continue; |
f1974222 | 1249 | dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend); |
113d4bc9 JP |
1250 | if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func) |
1251 | continue; | |
7dec80cc | 1252 | |
e7c2bc37 JP |
1253 | /* |
1254 | * Use of RIP-relative switch jumps is quite rare, and | |
1255 | * indicates a rare GCC quirk/bug which can leave dead code | |
1256 | * behind. | |
1257 | */ | |
f1974222 | 1258 | if (text_reloc->type == R_X86_64_PC32) |
e7c2bc37 JP |
1259 | file->ignore_unreachables = true; |
1260 | ||
f1974222 | 1261 | return table_reloc; |
dcc914f4 JP |
1262 | } |
1263 | ||
1264 | return NULL; | |
1265 | } | |
1266 | ||
bd98c813 JH |
1267 | /* |
1268 | * First pass: Mark the head of each jump table so that in the next pass, | |
1269 | * we know when a given jump table ends and the next one starts. | |
1270 | */ | |
1271 | static void mark_func_jump_tables(struct objtool_file *file, | |
1272 | struct symbol *func) | |
dcc914f4 | 1273 | { |
bd98c813 | 1274 | struct instruction *insn, *last = NULL; |
f1974222 | 1275 | struct reloc *reloc; |
dcc914f4 | 1276 | |
f0f70adb | 1277 | func_for_each_insn(file, func, insn) { |
99ce7962 PZ |
1278 | if (!last) |
1279 | last = insn; | |
1280 | ||
1281 | /* | |
1282 | * Store back-pointers for unconditional forward jumps such | |
e7c2bc37 | 1283 | * that find_jump_table() can back-track using those and |
99ce7962 PZ |
1284 | * avoid some potentially confusing code. |
1285 | */ | |
1286 | if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest && | |
1287 | insn->offset > last->offset && | |
1288 | insn->jump_dest->offset > insn->offset && | |
1289 | !insn->jump_dest->first_jump_src) { | |
1290 | ||
1291 | insn->jump_dest->first_jump_src = insn; | |
1292 | last = insn->jump_dest; | |
1293 | } | |
1294 | ||
dcc914f4 JP |
1295 | if (insn->type != INSN_JUMP_DYNAMIC) |
1296 | continue; | |
1297 | ||
f1974222 MH |
1298 | reloc = find_jump_table(file, func, insn); |
1299 | if (reloc) { | |
1300 | reloc->jump_table_start = true; | |
1301 | insn->jump_table = reloc; | |
dcc914f4 | 1302 | } |
dcc914f4 | 1303 | } |
bd98c813 JH |
1304 | } |
1305 | ||
1306 | static int add_func_jump_tables(struct objtool_file *file, | |
1307 | struct symbol *func) | |
1308 | { | |
1309 | struct instruction *insn; | |
1310 | int ret; | |
1311 | ||
f0f70adb | 1312 | func_for_each_insn(file, func, insn) { |
bd98c813 JH |
1313 | if (!insn->jump_table) |
1314 | continue; | |
dcc914f4 | 1315 | |
bd98c813 | 1316 | ret = add_jump_table(file, insn, insn->jump_table); |
dcc914f4 JP |
1317 | if (ret) |
1318 | return ret; | |
1319 | } | |
1320 | ||
1321 | return 0; | |
1322 | } | |
1323 | ||
1324 | /* | |
1325 | * For some switch statements, gcc generates a jump table in the .rodata | |
1326 | * section which contains a list of addresses within the function to jump to. | |
1327 | * This finds these jump tables and adds them to the insn->alts lists. | |
1328 | */ | |
e7c2bc37 | 1329 | static int add_jump_table_alts(struct objtool_file *file) |
dcc914f4 JP |
1330 | { |
1331 | struct section *sec; | |
1332 | struct symbol *func; | |
1333 | int ret; | |
1334 | ||
4a60aa05 | 1335 | if (!file->rodata) |
dcc914f4 JP |
1336 | return 0; |
1337 | ||
baa41469 | 1338 | for_each_sec(file, sec) { |
dcc914f4 JP |
1339 | list_for_each_entry(func, &sec->symbol_list, list) { |
1340 | if (func->type != STT_FUNC) | |
1341 | continue; | |
1342 | ||
bd98c813 | 1343 | mark_func_jump_tables(file, func); |
e7c2bc37 | 1344 | ret = add_func_jump_tables(file, func); |
dcc914f4 JP |
1345 | if (ret) |
1346 | return ret; | |
1347 | } | |
1348 | } | |
1349 | ||
1350 | return 0; | |
1351 | } | |
1352 | ||
39358a03 JP |
1353 | static int read_unwind_hints(struct objtool_file *file) |
1354 | { | |
f1974222 MH |
1355 | struct section *sec, *relocsec; |
1356 | struct reloc *reloc; | |
39358a03 JP |
1357 | struct unwind_hint *hint; |
1358 | struct instruction *insn; | |
1359 | struct cfi_reg *cfa; | |
1360 | int i; | |
1361 | ||
1362 | sec = find_section_by_name(file->elf, ".discard.unwind_hints"); | |
1363 | if (!sec) | |
1364 | return 0; | |
1365 | ||
f1974222 MH |
1366 | relocsec = sec->reloc; |
1367 | if (!relocsec) { | |
39358a03 JP |
1368 | WARN("missing .rela.discard.unwind_hints section"); |
1369 | return -1; | |
1370 | } | |
1371 | ||
1372 | if (sec->len % sizeof(struct unwind_hint)) { | |
1373 | WARN("struct unwind_hint size mismatch"); | |
1374 | return -1; | |
1375 | } | |
1376 | ||
1377 | file->hints = true; | |
1378 | ||
1379 | for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) { | |
1380 | hint = (struct unwind_hint *)sec->data->d_buf + i; | |
1381 | ||
f1974222 MH |
1382 | reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint)); |
1383 | if (!reloc) { | |
1384 | WARN("can't find reloc for unwind_hints[%d]", i); | |
39358a03 JP |
1385 | return -1; |
1386 | } | |
1387 | ||
f1974222 | 1388 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
39358a03 JP |
1389 | if (!insn) { |
1390 | WARN("can't find insn for unwind_hints[%d]", i); | |
1391 | return -1; | |
1392 | } | |
1393 | ||
e7c0219b | 1394 | cfa = &insn->cfi.cfa; |
39358a03 | 1395 | |
c536ed2f | 1396 | if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) { |
e25eea89 | 1397 | insn->ret_offset = hint->sp_offset; |
39358a03 JP |
1398 | continue; |
1399 | } | |
1400 | ||
1401 | insn->hint = true; | |
1402 | ||
1403 | switch (hint->sp_reg) { | |
1404 | case ORC_REG_UNDEFINED: | |
1405 | cfa->base = CFI_UNDEFINED; | |
1406 | break; | |
1407 | case ORC_REG_SP: | |
1408 | cfa->base = CFI_SP; | |
1409 | break; | |
1410 | case ORC_REG_BP: | |
1411 | cfa->base = CFI_BP; | |
1412 | break; | |
1413 | case ORC_REG_SP_INDIRECT: | |
1414 | cfa->base = CFI_SP_INDIRECT; | |
1415 | break; | |
1416 | case ORC_REG_R10: | |
1417 | cfa->base = CFI_R10; | |
1418 | break; | |
1419 | case ORC_REG_R13: | |
1420 | cfa->base = CFI_R13; | |
1421 | break; | |
1422 | case ORC_REG_DI: | |
1423 | cfa->base = CFI_DI; | |
1424 | break; | |
1425 | case ORC_REG_DX: | |
1426 | cfa->base = CFI_DX; | |
1427 | break; | |
1428 | default: | |
1429 | WARN_FUNC("unsupported unwind_hint sp base reg %d", | |
1430 | insn->sec, insn->offset, hint->sp_reg); | |
1431 | return -1; | |
1432 | } | |
1433 | ||
1434 | cfa->offset = hint->sp_offset; | |
e7c0219b PZ |
1435 | insn->cfi.type = hint->type; |
1436 | insn->cfi.end = hint->end; | |
39358a03 JP |
1437 | } |
1438 | ||
1439 | return 0; | |
1440 | } | |
1441 | ||
b5bc2231 PZ |
1442 | static int read_retpoline_hints(struct objtool_file *file) |
1443 | { | |
63474dc4 | 1444 | struct section *sec; |
b5bc2231 | 1445 | struct instruction *insn; |
f1974222 | 1446 | struct reloc *reloc; |
b5bc2231 | 1447 | |
63474dc4 | 1448 | sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); |
b5bc2231 PZ |
1449 | if (!sec) |
1450 | return 0; | |
1451 | ||
f1974222 MH |
1452 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
1453 | if (reloc->sym->type != STT_SECTION) { | |
63474dc4 | 1454 | WARN("unexpected relocation symbol type in %s", sec->name); |
b5bc2231 PZ |
1455 | return -1; |
1456 | } | |
1457 | ||
f1974222 | 1458 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
b5bc2231 | 1459 | if (!insn) { |
63474dc4 | 1460 | WARN("bad .discard.retpoline_safe entry"); |
b5bc2231 PZ |
1461 | return -1; |
1462 | } | |
1463 | ||
1464 | if (insn->type != INSN_JUMP_DYNAMIC && | |
1465 | insn->type != INSN_CALL_DYNAMIC) { | |
63474dc4 | 1466 | WARN_FUNC("retpoline_safe hint not an indirect jump/call", |
b5bc2231 PZ |
1467 | insn->sec, insn->offset); |
1468 | return -1; | |
1469 | } | |
1470 | ||
1471 | insn->retpoline_safe = true; | |
1472 | } | |
1473 | ||
1474 | return 0; | |
1475 | } | |
1476 | ||
c4a33939 PZ |
1477 | static int read_instr_hints(struct objtool_file *file) |
1478 | { | |
1479 | struct section *sec; | |
1480 | struct instruction *insn; | |
f1974222 | 1481 | struct reloc *reloc; |
c4a33939 PZ |
1482 | |
1483 | sec = find_section_by_name(file->elf, ".rela.discard.instr_end"); | |
1484 | if (!sec) | |
1485 | return 0; | |
1486 | ||
f1974222 MH |
1487 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
1488 | if (reloc->sym->type != STT_SECTION) { | |
c4a33939 PZ |
1489 | WARN("unexpected relocation symbol type in %s", sec->name); |
1490 | return -1; | |
1491 | } | |
1492 | ||
f1974222 | 1493 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
c4a33939 PZ |
1494 | if (!insn) { |
1495 | WARN("bad .discard.instr_end entry"); | |
1496 | return -1; | |
1497 | } | |
1498 | ||
1499 | insn->instr--; | |
1500 | } | |
1501 | ||
1502 | sec = find_section_by_name(file->elf, ".rela.discard.instr_begin"); | |
1503 | if (!sec) | |
1504 | return 0; | |
1505 | ||
f1974222 MH |
1506 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
1507 | if (reloc->sym->type != STT_SECTION) { | |
c4a33939 PZ |
1508 | WARN("unexpected relocation symbol type in %s", sec->name); |
1509 | return -1; | |
1510 | } | |
1511 | ||
f1974222 | 1512 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
c4a33939 PZ |
1513 | if (!insn) { |
1514 | WARN("bad .discard.instr_begin entry"); | |
1515 | return -1; | |
1516 | } | |
1517 | ||
1518 | insn->instr++; | |
1519 | } | |
1520 | ||
1521 | return 0; | |
1522 | } | |
1523 | ||
8aa8eb2a AC |
1524 | static int read_intra_function_calls(struct objtool_file *file) |
1525 | { | |
1526 | struct instruction *insn; | |
1527 | struct section *sec; | |
f1974222 | 1528 | struct reloc *reloc; |
8aa8eb2a AC |
1529 | |
1530 | sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls"); | |
1531 | if (!sec) | |
1532 | return 0; | |
1533 | ||
f1974222 | 1534 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
8aa8eb2a AC |
1535 | unsigned long dest_off; |
1536 | ||
f1974222 | 1537 | if (reloc->sym->type != STT_SECTION) { |
8aa8eb2a AC |
1538 | WARN("unexpected relocation symbol type in %s", |
1539 | sec->name); | |
1540 | return -1; | |
1541 | } | |
1542 | ||
f1974222 | 1543 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
8aa8eb2a AC |
1544 | if (!insn) { |
1545 | WARN("bad .discard.intra_function_call entry"); | |
1546 | return -1; | |
1547 | } | |
1548 | ||
1549 | if (insn->type != INSN_CALL) { | |
1550 | WARN_FUNC("intra_function_call not a direct call", | |
1551 | insn->sec, insn->offset); | |
1552 | return -1; | |
1553 | } | |
1554 | ||
1555 | /* | |
1556 | * Treat intra-function CALLs as JMPs, but with a stack_op. | |
1557 | * See add_call_destinations(), which strips stack_ops from | |
1558 | * normal CALLs. | |
1559 | */ | |
1560 | insn->type = INSN_JUMP_UNCONDITIONAL; | |
1561 | ||
1562 | dest_off = insn->offset + insn->len + insn->immediate; | |
1563 | insn->jump_dest = find_insn(file, insn->sec, dest_off); | |
1564 | if (!insn->jump_dest) { | |
1565 | WARN_FUNC("can't find call dest at %s+0x%lx", | |
1566 | insn->sec, insn->offset, | |
1567 | insn->sec->name, dest_off); | |
1568 | return -1; | |
1569 | } | |
1570 | } | |
1571 | ||
1572 | return 0; | |
1573 | } | |
1574 | ||
4a60aa05 AX |
1575 | static void mark_rodata(struct objtool_file *file) |
1576 | { | |
1577 | struct section *sec; | |
1578 | bool found = false; | |
1579 | ||
1580 | /* | |
87b512de JP |
1581 | * Search for the following rodata sections, each of which can |
1582 | * potentially contain jump tables: | |
1583 | * | |
1584 | * - .rodata: can contain GCC switch tables | |
1585 | * - .rodata.<func>: same, if -fdata-sections is being used | |
1586 | * - .rodata..c_jump_table: contains C annotated jump tables | |
1587 | * | |
1588 | * .rodata.str1.* sections are ignored; they don't contain jump tables. | |
4a60aa05 AX |
1589 | */ |
1590 | for_each_sec(file, sec) { | |
1ee44470 MS |
1591 | if (!strncmp(sec->name, ".rodata", 7) && |
1592 | !strstr(sec->name, ".str1.")) { | |
4a60aa05 AX |
1593 | sec->rodata = true; |
1594 | found = true; | |
1595 | } | |
1596 | } | |
1597 | ||
1598 | file->rodata = found; | |
1599 | } | |
1600 | ||
dcc914f4 JP |
1601 | static int decode_sections(struct objtool_file *file) |
1602 | { | |
1603 | int ret; | |
1604 | ||
4a60aa05 AX |
1605 | mark_rodata(file); |
1606 | ||
dcc914f4 JP |
1607 | ret = decode_instructions(file); |
1608 | if (ret) | |
1609 | return ret; | |
1610 | ||
1611 | ret = add_dead_ends(file); | |
1612 | if (ret) | |
1613 | return ret; | |
1614 | ||
1615 | add_ignores(file); | |
ea24213d | 1616 | add_uaccess_safe(file); |
dcc914f4 | 1617 | |
ff05ab23 | 1618 | ret = add_ignore_alternatives(file); |
258c7605 JP |
1619 | if (ret) |
1620 | return ret; | |
1621 | ||
dcc914f4 JP |
1622 | ret = add_jump_destinations(file); |
1623 | if (ret) | |
1624 | return ret; | |
1625 | ||
a845c7cf | 1626 | ret = add_special_section_alts(file); |
dcc914f4 JP |
1627 | if (ret) |
1628 | return ret; | |
1629 | ||
8aa8eb2a AC |
1630 | ret = read_intra_function_calls(file); |
1631 | if (ret) | |
1632 | return ret; | |
1633 | ||
a845c7cf | 1634 | ret = add_call_destinations(file); |
dcc914f4 JP |
1635 | if (ret) |
1636 | return ret; | |
1637 | ||
e7c2bc37 | 1638 | ret = add_jump_table_alts(file); |
dcc914f4 JP |
1639 | if (ret) |
1640 | return ret; | |
1641 | ||
39358a03 JP |
1642 | ret = read_unwind_hints(file); |
1643 | if (ret) | |
1644 | return ret; | |
1645 | ||
b5bc2231 PZ |
1646 | ret = read_retpoline_hints(file); |
1647 | if (ret) | |
1648 | return ret; | |
1649 | ||
c4a33939 PZ |
1650 | ret = read_instr_hints(file); |
1651 | if (ret) | |
1652 | return ret; | |
1653 | ||
dcc914f4 JP |
1654 | return 0; |
1655 | } | |
1656 | ||
1657 | static bool is_fentry_call(struct instruction *insn) | |
1658 | { | |
87cf61fe | 1659 | if (insn->type == INSN_CALL && insn->call_dest && |
dcc914f4 JP |
1660 | insn->call_dest->type == STT_NOTYPE && |
1661 | !strcmp(insn->call_dest->name, "__fentry__")) | |
1662 | return true; | |
1663 | ||
1664 | return false; | |
1665 | } | |
1666 | ||
e25eea89 | 1667 | static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state) |
dcc914f4 | 1668 | { |
e25eea89 | 1669 | u8 ret_offset = insn->ret_offset; |
e7c0219b | 1670 | struct cfi_state *cfi = &state->cfi; |
baa41469 JP |
1671 | int i; |
1672 | ||
e7c0219b | 1673 | if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap) |
e25eea89 PZ |
1674 | return true; |
1675 | ||
e7c0219b | 1676 | if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset) |
baa41469 JP |
1677 | return true; |
1678 | ||
e7c0219b | 1679 | if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset) |
e25eea89 PZ |
1680 | return true; |
1681 | ||
c721b3f8 AC |
1682 | /* |
1683 | * If there is a ret offset hint then don't check registers | |
1684 | * because a callee-saved register might have been pushed on | |
1685 | * the stack. | |
1686 | */ | |
1687 | if (ret_offset) | |
1688 | return false; | |
1689 | ||
e25eea89 | 1690 | for (i = 0; i < CFI_NUM_REGS; i++) { |
e7c0219b PZ |
1691 | if (cfi->regs[i].base != initial_func_cfi.regs[i].base || |
1692 | cfi->regs[i].offset != initial_func_cfi.regs[i].offset) | |
baa41469 | 1693 | return true; |
e25eea89 | 1694 | } |
baa41469 JP |
1695 | |
1696 | return false; | |
1697 | } | |
1698 | ||
1699 | static bool has_valid_stack_frame(struct insn_state *state) | |
1700 | { | |
e7c0219b PZ |
1701 | struct cfi_state *cfi = &state->cfi; |
1702 | ||
1703 | if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA && | |
1704 | cfi->regs[CFI_BP].offset == -16) | |
baa41469 JP |
1705 | return true; |
1706 | ||
e7c0219b | 1707 | if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP) |
baa41469 JP |
1708 | return true; |
1709 | ||
1710 | return false; | |
dcc914f4 JP |
1711 | } |
1712 | ||
e7c0219b PZ |
1713 | static int update_cfi_state_regs(struct instruction *insn, |
1714 | struct cfi_state *cfi, | |
65ea47dc | 1715 | struct stack_op *op) |
627fce14 | 1716 | { |
e7c0219b | 1717 | struct cfi_reg *cfa = &cfi->cfa; |
627fce14 | 1718 | |
d8dd25a4 | 1719 | if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT) |
627fce14 JP |
1720 | return 0; |
1721 | ||
1722 | /* push */ | |
ea24213d | 1723 | if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF) |
627fce14 JP |
1724 | cfa->offset += 8; |
1725 | ||
1726 | /* pop */ | |
ea24213d | 1727 | if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF) |
627fce14 JP |
1728 | cfa->offset -= 8; |
1729 | ||
1730 | /* add immediate to sp */ | |
1731 | if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD && | |
1732 | op->dest.reg == CFI_SP && op->src.reg == CFI_SP) | |
1733 | cfa->offset -= op->src.offset; | |
1734 | ||
1735 | return 0; | |
1736 | } | |
1737 | ||
e7c0219b | 1738 | static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset) |
dcc914f4 | 1739 | { |
bf4d1a83 | 1740 | if (arch_callee_saved_reg(reg) && |
e7c0219b PZ |
1741 | cfi->regs[reg].base == CFI_UNDEFINED) { |
1742 | cfi->regs[reg].base = base; | |
1743 | cfi->regs[reg].offset = offset; | |
baa41469 | 1744 | } |
dcc914f4 JP |
1745 | } |
1746 | ||
e7c0219b | 1747 | static void restore_reg(struct cfi_state *cfi, unsigned char reg) |
dcc914f4 | 1748 | { |
e7c0219b PZ |
1749 | cfi->regs[reg].base = initial_func_cfi.regs[reg].base; |
1750 | cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset; | |
baa41469 JP |
1751 | } |
1752 | ||
1753 | /* | |
1754 | * A note about DRAP stack alignment: | |
1755 | * | |
1756 | * GCC has the concept of a DRAP register, which is used to help keep track of | |
1757 | * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP | |
1758 | * register. The typical DRAP pattern is: | |
1759 | * | |
1760 | * 4c 8d 54 24 08 lea 0x8(%rsp),%r10 | |
1761 | * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp | |
1762 | * 41 ff 72 f8 pushq -0x8(%r10) | |
1763 | * 55 push %rbp | |
1764 | * 48 89 e5 mov %rsp,%rbp | |
1765 | * (more pushes) | |
1766 | * 41 52 push %r10 | |
1767 | * ... | |
1768 | * 41 5a pop %r10 | |
1769 | * (more pops) | |
1770 | * 5d pop %rbp | |
1771 | * 49 8d 62 f8 lea -0x8(%r10),%rsp | |
1772 | * c3 retq | |
1773 | * | |
1774 | * There are some variations in the epilogues, like: | |
1775 | * | |
1776 | * 5b pop %rbx | |
1777 | * 41 5a pop %r10 | |
1778 | * 41 5c pop %r12 | |
1779 | * 41 5d pop %r13 | |
1780 | * 41 5e pop %r14 | |
1781 | * c9 leaveq | |
1782 | * 49 8d 62 f8 lea -0x8(%r10),%rsp | |
1783 | * c3 retq | |
1784 | * | |
1785 | * and: | |
1786 | * | |
1787 | * 4c 8b 55 e8 mov -0x18(%rbp),%r10 | |
1788 | * 48 8b 5d e0 mov -0x20(%rbp),%rbx | |
1789 | * 4c 8b 65 f0 mov -0x10(%rbp),%r12 | |
1790 | * 4c 8b 6d f8 mov -0x8(%rbp),%r13 | |
1791 | * c9 leaveq | |
1792 | * 49 8d 62 f8 lea -0x8(%r10),%rsp | |
1793 | * c3 retq | |
1794 | * | |
1795 | * Sometimes r13 is used as the DRAP register, in which case it's saved and | |
1796 | * restored beforehand: | |
1797 | * | |
1798 | * 41 55 push %r13 | |
1799 | * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13 | |
1800 | * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp | |
1801 | * ... | |
1802 | * 49 8d 65 f0 lea -0x10(%r13),%rsp | |
1803 | * 41 5d pop %r13 | |
1804 | * c3 retq | |
1805 | */ | |
e7c0219b | 1806 | static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi, |
65ea47dc | 1807 | struct stack_op *op) |
baa41469 | 1808 | { |
e7c0219b PZ |
1809 | struct cfi_reg *cfa = &cfi->cfa; |
1810 | struct cfi_reg *regs = cfi->regs; | |
baa41469 JP |
1811 | |
1812 | /* stack operations don't make sense with an undefined CFA */ | |
1813 | if (cfa->base == CFI_UNDEFINED) { | |
1814 | if (insn->func) { | |
1815 | WARN_FUNC("undefined stack state", insn->sec, insn->offset); | |
1816 | return -1; | |
1817 | } | |
1818 | return 0; | |
1819 | } | |
1820 | ||
e7c0219b PZ |
1821 | if (cfi->type == ORC_TYPE_REGS || cfi->type == ORC_TYPE_REGS_IRET) |
1822 | return update_cfi_state_regs(insn, cfi, op); | |
627fce14 | 1823 | |
baa41469 JP |
1824 | switch (op->dest.type) { |
1825 | ||
1826 | case OP_DEST_REG: | |
1827 | switch (op->src.type) { | |
1828 | ||
1829 | case OP_SRC_REG: | |
0d0970ee JP |
1830 | if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP && |
1831 | cfa->base == CFI_SP && | |
1832 | regs[CFI_BP].base == CFI_CFA && | |
1833 | regs[CFI_BP].offset == -cfa->offset) { | |
1834 | ||
1835 | /* mov %rsp, %rbp */ | |
1836 | cfa->base = op->dest.reg; | |
e7c0219b | 1837 | cfi->bp_scratch = false; |
0d0970ee | 1838 | } |
dd88a0a0 | 1839 | |
0d0970ee | 1840 | else if (op->src.reg == CFI_SP && |
e7c0219b | 1841 | op->dest.reg == CFI_BP && cfi->drap) { |
dd88a0a0 | 1842 | |
0d0970ee JP |
1843 | /* drap: mov %rsp, %rbp */ |
1844 | regs[CFI_BP].base = CFI_BP; | |
e7c0219b PZ |
1845 | regs[CFI_BP].offset = -cfi->stack_size; |
1846 | cfi->bp_scratch = false; | |
0d0970ee | 1847 | } |
dd88a0a0 | 1848 | |
0d0970ee JP |
1849 | else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
1850 | ||
1851 | /* | |
1852 | * mov %rsp, %reg | |
1853 | * | |
1854 | * This is needed for the rare case where GCC | |
1855 | * does: | |
1856 | * | |
1857 | * mov %rsp, %rax | |
1858 | * ... | |
1859 | * mov %rax, %rsp | |
1860 | */ | |
e7c0219b PZ |
1861 | cfi->vals[op->dest.reg].base = CFI_CFA; |
1862 | cfi->vals[op->dest.reg].offset = -cfi->stack_size; | |
dd88a0a0 JP |
1863 | } |
1864 | ||
3c1f0583 JP |
1865 | else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP && |
1866 | cfa->base == CFI_BP) { | |
1867 | ||
1868 | /* | |
1869 | * mov %rbp, %rsp | |
1870 | * | |
1871 | * Restore the original stack pointer (Clang). | |
1872 | */ | |
e7c0219b | 1873 | cfi->stack_size = -cfi->regs[CFI_BP].offset; |
3c1f0583 JP |
1874 | } |
1875 | ||
dd88a0a0 JP |
1876 | else if (op->dest.reg == cfa->base) { |
1877 | ||
1878 | /* mov %reg, %rsp */ | |
1879 | if (cfa->base == CFI_SP && | |
e7c0219b | 1880 | cfi->vals[op->src.reg].base == CFI_CFA) { |
dd88a0a0 JP |
1881 | |
1882 | /* | |
1883 | * This is needed for the rare case | |
1884 | * where GCC does something dumb like: | |
1885 | * | |
1886 | * lea 0x8(%rsp), %rcx | |
1887 | * ... | |
1888 | * mov %rcx, %rsp | |
1889 | */ | |
e7c0219b PZ |
1890 | cfa->offset = -cfi->vals[op->src.reg].offset; |
1891 | cfi->stack_size = cfa->offset; | |
dd88a0a0 JP |
1892 | |
1893 | } else { | |
1894 | cfa->base = CFI_UNDEFINED; | |
1895 | cfa->offset = 0; | |
1896 | } | |
baa41469 JP |
1897 | } |
1898 | ||
1899 | break; | |
1900 | ||
1901 | case OP_SRC_ADD: | |
1902 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) { | |
1903 | ||
1904 | /* add imm, %rsp */ | |
e7c0219b | 1905 | cfi->stack_size -= op->src.offset; |
baa41469 JP |
1906 | if (cfa->base == CFI_SP) |
1907 | cfa->offset -= op->src.offset; | |
1908 | break; | |
1909 | } | |
1910 | ||
1911 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) { | |
1912 | ||
1913 | /* lea disp(%rbp), %rsp */ | |
e7c0219b | 1914 | cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset); |
baa41469 JP |
1915 | break; |
1916 | } | |
1917 | ||
dd88a0a0 | 1918 | if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
baa41469 JP |
1919 | |
1920 | /* drap: lea disp(%rsp), %drap */ | |
e7c0219b | 1921 | cfi->drap_reg = op->dest.reg; |
dd88a0a0 JP |
1922 | |
1923 | /* | |
1924 | * lea disp(%rsp), %reg | |
1925 | * | |
1926 | * This is needed for the rare case where GCC | |
1927 | * does something dumb like: | |
1928 | * | |
1929 | * lea 0x8(%rsp), %rcx | |
1930 | * ... | |
1931 | * mov %rcx, %rsp | |
1932 | */ | |
e7c0219b PZ |
1933 | cfi->vals[op->dest.reg].base = CFI_CFA; |
1934 | cfi->vals[op->dest.reg].offset = \ | |
1935 | -cfi->stack_size + op->src.offset; | |
dd88a0a0 | 1936 | |
baa41469 JP |
1937 | break; |
1938 | } | |
1939 | ||
e7c0219b PZ |
1940 | if (cfi->drap && op->dest.reg == CFI_SP && |
1941 | op->src.reg == cfi->drap_reg) { | |
baa41469 JP |
1942 | |
1943 | /* drap: lea disp(%drap), %rsp */ | |
1944 | cfa->base = CFI_SP; | |
e7c0219b PZ |
1945 | cfa->offset = cfi->stack_size = -op->src.offset; |
1946 | cfi->drap_reg = CFI_UNDEFINED; | |
1947 | cfi->drap = false; | |
baa41469 JP |
1948 | break; |
1949 | } | |
1950 | ||
e7c0219b | 1951 | if (op->dest.reg == cfi->cfa.base) { |
baa41469 JP |
1952 | WARN_FUNC("unsupported stack register modification", |
1953 | insn->sec, insn->offset); | |
1954 | return -1; | |
1955 | } | |
1956 | ||
1957 | break; | |
1958 | ||
1959 | case OP_SRC_AND: | |
1960 | if (op->dest.reg != CFI_SP || | |
e7c0219b PZ |
1961 | (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) || |
1962 | (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) { | |
baa41469 JP |
1963 | WARN_FUNC("unsupported stack pointer realignment", |
1964 | insn->sec, insn->offset); | |
1965 | return -1; | |
1966 | } | |
1967 | ||
e7c0219b | 1968 | if (cfi->drap_reg != CFI_UNDEFINED) { |
baa41469 | 1969 | /* drap: and imm, %rsp */ |
e7c0219b PZ |
1970 | cfa->base = cfi->drap_reg; |
1971 | cfa->offset = cfi->stack_size = 0; | |
1972 | cfi->drap = true; | |
baa41469 JP |
1973 | } |
1974 | ||
1975 | /* | |
1976 | * Older versions of GCC (4.8ish) realign the stack | |
1977 | * without DRAP, with a frame pointer. | |
1978 | */ | |
1979 | ||
1980 | break; | |
1981 | ||
1982 | case OP_SRC_POP: | |
ea24213d | 1983 | case OP_SRC_POPF: |
e7c0219b | 1984 | if (!cfi->drap && op->dest.reg == cfa->base) { |
baa41469 JP |
1985 | |
1986 | /* pop %rbp */ | |
1987 | cfa->base = CFI_SP; | |
1988 | } | |
1989 | ||
e7c0219b PZ |
1990 | if (cfi->drap && cfa->base == CFI_BP_INDIRECT && |
1991 | op->dest.reg == cfi->drap_reg && | |
1992 | cfi->drap_offset == -cfi->stack_size) { | |
baa41469 | 1993 | |
bf4d1a83 | 1994 | /* drap: pop %drap */ |
e7c0219b | 1995 | cfa->base = cfi->drap_reg; |
bf4d1a83 | 1996 | cfa->offset = 0; |
e7c0219b | 1997 | cfi->drap_offset = -1; |
baa41469 | 1998 | |
e7c0219b | 1999 | } else if (regs[op->dest.reg].offset == -cfi->stack_size) { |
baa41469 | 2000 | |
bf4d1a83 | 2001 | /* pop %reg */ |
e7c0219b | 2002 | restore_reg(cfi, op->dest.reg); |
baa41469 JP |
2003 | } |
2004 | ||
e7c0219b | 2005 | cfi->stack_size -= 8; |
baa41469 JP |
2006 | if (cfa->base == CFI_SP) |
2007 | cfa->offset -= 8; | |
2008 | ||
2009 | break; | |
2010 | ||
2011 | case OP_SRC_REG_INDIRECT: | |
e7c0219b PZ |
2012 | if (cfi->drap && op->src.reg == CFI_BP && |
2013 | op->src.offset == cfi->drap_offset) { | |
bf4d1a83 JP |
2014 | |
2015 | /* drap: mov disp(%rbp), %drap */ | |
e7c0219b | 2016 | cfa->base = cfi->drap_reg; |
bf4d1a83 | 2017 | cfa->offset = 0; |
e7c0219b | 2018 | cfi->drap_offset = -1; |
bf4d1a83 JP |
2019 | } |
2020 | ||
e7c0219b | 2021 | if (cfi->drap && op->src.reg == CFI_BP && |
baa41469 JP |
2022 | op->src.offset == regs[op->dest.reg].offset) { |
2023 | ||
2024 | /* drap: mov disp(%rbp), %reg */ | |
e7c0219b | 2025 | restore_reg(cfi, op->dest.reg); |
baa41469 JP |
2026 | |
2027 | } else if (op->src.reg == cfa->base && | |
2028 | op->src.offset == regs[op->dest.reg].offset + cfa->offset) { | |
2029 | ||
2030 | /* mov disp(%rbp), %reg */ | |
2031 | /* mov disp(%rsp), %reg */ | |
e7c0219b | 2032 | restore_reg(cfi, op->dest.reg); |
baa41469 JP |
2033 | } |
2034 | ||
2035 | break; | |
2036 | ||
2037 | default: | |
2038 | WARN_FUNC("unknown stack-related instruction", | |
2039 | insn->sec, insn->offset); | |
2040 | return -1; | |
2041 | } | |
2042 | ||
2043 | break; | |
2044 | ||
2045 | case OP_DEST_PUSH: | |
ea24213d | 2046 | case OP_DEST_PUSHF: |
e7c0219b | 2047 | cfi->stack_size += 8; |
baa41469 JP |
2048 | if (cfa->base == CFI_SP) |
2049 | cfa->offset += 8; | |
2050 | ||
2051 | if (op->src.type != OP_SRC_REG) | |
2052 | break; | |
2053 | ||
e7c0219b PZ |
2054 | if (cfi->drap) { |
2055 | if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { | |
baa41469 JP |
2056 | |
2057 | /* drap: push %drap */ | |
2058 | cfa->base = CFI_BP_INDIRECT; | |
e7c0219b | 2059 | cfa->offset = -cfi->stack_size; |
baa41469 | 2060 | |
bf4d1a83 | 2061 | /* save drap so we know when to restore it */ |
e7c0219b | 2062 | cfi->drap_offset = -cfi->stack_size; |
baa41469 | 2063 | |
e7c0219b | 2064 | } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) { |
baa41469 JP |
2065 | |
2066 | /* drap: push %rbp */ | |
e7c0219b | 2067 | cfi->stack_size = 0; |
baa41469 JP |
2068 | |
2069 | } else if (regs[op->src.reg].base == CFI_UNDEFINED) { | |
2070 | ||
2071 | /* drap: push %reg */ | |
e7c0219b | 2072 | save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size); |
baa41469 JP |
2073 | } |
2074 | ||
2075 | } else { | |
2076 | ||
2077 | /* push %reg */ | |
e7c0219b | 2078 | save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size); |
baa41469 JP |
2079 | } |
2080 | ||
2081 | /* detect when asm code uses rbp as a scratch register */ | |
867ac9d7 | 2082 | if (!no_fp && insn->func && op->src.reg == CFI_BP && |
baa41469 | 2083 | cfa->base != CFI_BP) |
e7c0219b | 2084 | cfi->bp_scratch = true; |
baa41469 JP |
2085 | break; |
2086 | ||
2087 | case OP_DEST_REG_INDIRECT: | |
2088 | ||
e7c0219b PZ |
2089 | if (cfi->drap) { |
2090 | if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { | |
baa41469 JP |
2091 | |
2092 | /* drap: mov %drap, disp(%rbp) */ | |
2093 | cfa->base = CFI_BP_INDIRECT; | |
2094 | cfa->offset = op->dest.offset; | |
2095 | ||
bf4d1a83 | 2096 | /* save drap offset so we know when to restore it */ |
e7c0219b | 2097 | cfi->drap_offset = op->dest.offset; |
baa41469 JP |
2098 | } |
2099 | ||
2100 | else if (regs[op->src.reg].base == CFI_UNDEFINED) { | |
2101 | ||
2102 | /* drap: mov reg, disp(%rbp) */ | |
e7c0219b | 2103 | save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset); |
baa41469 JP |
2104 | } |
2105 | ||
2106 | } else if (op->dest.reg == cfa->base) { | |
2107 | ||
2108 | /* mov reg, disp(%rbp) */ | |
2109 | /* mov reg, disp(%rsp) */ | |
e7c0219b PZ |
2110 | save_reg(cfi, op->src.reg, CFI_CFA, |
2111 | op->dest.offset - cfi->cfa.offset); | |
baa41469 JP |
2112 | } |
2113 | ||
2114 | break; | |
2115 | ||
2116 | case OP_DEST_LEAVE: | |
e7c0219b PZ |
2117 | if ((!cfi->drap && cfa->base != CFI_BP) || |
2118 | (cfi->drap && cfa->base != cfi->drap_reg)) { | |
baa41469 JP |
2119 | WARN_FUNC("leave instruction with modified stack frame", |
2120 | insn->sec, insn->offset); | |
2121 | return -1; | |
2122 | } | |
2123 | ||
2124 | /* leave (mov %rbp, %rsp; pop %rbp) */ | |
2125 | ||
e7c0219b PZ |
2126 | cfi->stack_size = -cfi->regs[CFI_BP].offset - 8; |
2127 | restore_reg(cfi, CFI_BP); | |
baa41469 | 2128 | |
e7c0219b | 2129 | if (!cfi->drap) { |
baa41469 JP |
2130 | cfa->base = CFI_SP; |
2131 | cfa->offset -= 8; | |
2132 | } | |
2133 | ||
2134 | break; | |
2135 | ||
2136 | case OP_DEST_MEM: | |
ea24213d | 2137 | if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) { |
baa41469 JP |
2138 | WARN_FUNC("unknown stack-related memory operation", |
2139 | insn->sec, insn->offset); | |
2140 | return -1; | |
2141 | } | |
2142 | ||
2143 | /* pop mem */ | |
e7c0219b | 2144 | cfi->stack_size -= 8; |
baa41469 JP |
2145 | if (cfa->base == CFI_SP) |
2146 | cfa->offset -= 8; | |
2147 | ||
2148 | break; | |
2149 | ||
2150 | default: | |
2151 | WARN_FUNC("unknown stack-related instruction", | |
2152 | insn->sec, insn->offset); | |
2153 | return -1; | |
2154 | } | |
2155 | ||
2156 | return 0; | |
2157 | } | |
2158 | ||
65ea47dc JT |
2159 | static int handle_insn_ops(struct instruction *insn, struct insn_state *state) |
2160 | { | |
2161 | struct stack_op *op; | |
2162 | ||
2163 | list_for_each_entry(op, &insn->stack_ops, list) { | |
ab3852ab | 2164 | struct cfi_state old_cfi = state->cfi; |
65ea47dc JT |
2165 | int res; |
2166 | ||
e7c0219b | 2167 | res = update_cfi_state(insn, &state->cfi, op); |
65ea47dc JT |
2168 | if (res) |
2169 | return res; | |
2170 | ||
ab3852ab PZ |
2171 | if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) { |
2172 | WARN_FUNC("alternative modifies stack", insn->sec, insn->offset); | |
2173 | return -1; | |
2174 | } | |
2175 | ||
65ea47dc JT |
2176 | if (op->dest.type == OP_DEST_PUSHF) { |
2177 | if (!state->uaccess_stack) { | |
2178 | state->uaccess_stack = 1; | |
2179 | } else if (state->uaccess_stack >> 31) { | |
2180 | WARN_FUNC("PUSHF stack exhausted", | |
2181 | insn->sec, insn->offset); | |
2182 | return 1; | |
2183 | } | |
2184 | state->uaccess_stack <<= 1; | |
2185 | state->uaccess_stack |= state->uaccess; | |
2186 | } | |
2187 | ||
2188 | if (op->src.type == OP_SRC_POPF) { | |
2189 | if (state->uaccess_stack) { | |
2190 | state->uaccess = state->uaccess_stack & 1; | |
2191 | state->uaccess_stack >>= 1; | |
2192 | if (state->uaccess_stack == 1) | |
2193 | state->uaccess_stack = 0; | |
2194 | } | |
2195 | } | |
2196 | } | |
2197 | ||
2198 | return 0; | |
2199 | } | |
2200 | ||
e7c0219b | 2201 | static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2) |
baa41469 | 2202 | { |
e7c0219b | 2203 | struct cfi_state *cfi1 = &insn->cfi; |
baa41469 JP |
2204 | int i; |
2205 | ||
e7c0219b PZ |
2206 | if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) { |
2207 | ||
baa41469 JP |
2208 | WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d", |
2209 | insn->sec, insn->offset, | |
e7c0219b PZ |
2210 | cfi1->cfa.base, cfi1->cfa.offset, |
2211 | cfi2->cfa.base, cfi2->cfa.offset); | |
baa41469 | 2212 | |
e7c0219b | 2213 | } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) { |
baa41469 | 2214 | for (i = 0; i < CFI_NUM_REGS; i++) { |
e7c0219b | 2215 | if (!memcmp(&cfi1->regs[i], &cfi2->regs[i], |
baa41469 JP |
2216 | sizeof(struct cfi_reg))) |
2217 | continue; | |
2218 | ||
2219 | WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d", | |
2220 | insn->sec, insn->offset, | |
e7c0219b PZ |
2221 | i, cfi1->regs[i].base, cfi1->regs[i].offset, |
2222 | i, cfi2->regs[i].base, cfi2->regs[i].offset); | |
baa41469 JP |
2223 | break; |
2224 | } | |
2225 | ||
e7c0219b PZ |
2226 | } else if (cfi1->type != cfi2->type) { |
2227 | ||
627fce14 | 2228 | WARN_FUNC("stack state mismatch: type1=%d type2=%d", |
e7c0219b PZ |
2229 | insn->sec, insn->offset, cfi1->type, cfi2->type); |
2230 | ||
2231 | } else if (cfi1->drap != cfi2->drap || | |
2232 | (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) || | |
2233 | (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) { | |
627fce14 | 2234 | |
bf4d1a83 | 2235 | WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)", |
baa41469 | 2236 | insn->sec, insn->offset, |
e7c0219b PZ |
2237 | cfi1->drap, cfi1->drap_reg, cfi1->drap_offset, |
2238 | cfi2->drap, cfi2->drap_reg, cfi2->drap_offset); | |
baa41469 JP |
2239 | |
2240 | } else | |
2241 | return true; | |
2242 | ||
2243 | return false; | |
dcc914f4 JP |
2244 | } |
2245 | ||
ea24213d PZ |
2246 | static inline bool func_uaccess_safe(struct symbol *func) |
2247 | { | |
2248 | if (func) | |
e10cd8fe | 2249 | return func->uaccess_safe; |
ea24213d PZ |
2250 | |
2251 | return false; | |
2252 | } | |
2253 | ||
0c1ddd33 | 2254 | static inline const char *call_dest_name(struct instruction *insn) |
ea24213d PZ |
2255 | { |
2256 | if (insn->call_dest) | |
2257 | return insn->call_dest->name; | |
2258 | ||
2259 | return "{dynamic}"; | |
2260 | } | |
2261 | ||
6b643a07 PZ |
2262 | static inline bool noinstr_call_dest(struct symbol *func) |
2263 | { | |
2264 | /* | |
2265 | * We can't deal with indirect function calls at present; | |
2266 | * assume they're instrumented. | |
2267 | */ | |
2268 | if (!func) | |
2269 | return false; | |
2270 | ||
2271 | /* | |
2272 | * If the symbol is from a noinstr section; we good. | |
2273 | */ | |
2274 | if (func->sec->noinstr) | |
2275 | return true; | |
2276 | ||
2277 | /* | |
2278 | * The __ubsan_handle_*() calls are like WARN(), they only happen when | |
2279 | * something 'BAD' happened. At the risk of taking the machine down, | |
2280 | * let them proceed to get the message out. | |
2281 | */ | |
2282 | if (!strncmp(func->name, "__ubsan_handle_", 15)) | |
2283 | return true; | |
2284 | ||
2285 | return false; | |
2286 | } | |
2287 | ||
ea24213d PZ |
2288 | static int validate_call(struct instruction *insn, struct insn_state *state) |
2289 | { | |
c4a33939 | 2290 | if (state->noinstr && state->instr <= 0 && |
6b643a07 | 2291 | !noinstr_call_dest(insn->call_dest)) { |
c4a33939 PZ |
2292 | WARN_FUNC("call to %s() leaves .noinstr.text section", |
2293 | insn->sec, insn->offset, call_dest_name(insn)); | |
2294 | return 1; | |
2295 | } | |
2296 | ||
ea24213d PZ |
2297 | if (state->uaccess && !func_uaccess_safe(insn->call_dest)) { |
2298 | WARN_FUNC("call to %s() with UACCESS enabled", | |
0c1ddd33 | 2299 | insn->sec, insn->offset, call_dest_name(insn)); |
ea24213d PZ |
2300 | return 1; |
2301 | } | |
2302 | ||
2f0f9e9a PZ |
2303 | if (state->df) { |
2304 | WARN_FUNC("call to %s() with DF set", | |
0c1ddd33 | 2305 | insn->sec, insn->offset, call_dest_name(insn)); |
2f0f9e9a PZ |
2306 | return 1; |
2307 | } | |
2308 | ||
ea24213d PZ |
2309 | return 0; |
2310 | } | |
2311 | ||
54262aa2 PZ |
2312 | static int validate_sibling_call(struct instruction *insn, struct insn_state *state) |
2313 | { | |
e25eea89 | 2314 | if (has_modified_stack_frame(insn, state)) { |
54262aa2 PZ |
2315 | WARN_FUNC("sibling call from callable instruction with modified stack frame", |
2316 | insn->sec, insn->offset); | |
2317 | return 1; | |
2318 | } | |
2319 | ||
ea24213d | 2320 | return validate_call(insn, state); |
54262aa2 PZ |
2321 | } |
2322 | ||
a92e92d1 PZ |
2323 | static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state) |
2324 | { | |
c4a33939 PZ |
2325 | if (state->noinstr && state->instr > 0) { |
2326 | WARN_FUNC("return with instrumentation enabled", | |
2327 | insn->sec, insn->offset); | |
2328 | return 1; | |
2329 | } | |
2330 | ||
a92e92d1 PZ |
2331 | if (state->uaccess && !func_uaccess_safe(func)) { |
2332 | WARN_FUNC("return with UACCESS enabled", | |
2333 | insn->sec, insn->offset); | |
2334 | return 1; | |
2335 | } | |
2336 | ||
2337 | if (!state->uaccess && func_uaccess_safe(func)) { | |
2338 | WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", | |
2339 | insn->sec, insn->offset); | |
2340 | return 1; | |
2341 | } | |
2342 | ||
2343 | if (state->df) { | |
2344 | WARN_FUNC("return with DF set", | |
2345 | insn->sec, insn->offset); | |
2346 | return 1; | |
2347 | } | |
2348 | ||
e25eea89 | 2349 | if (func && has_modified_stack_frame(insn, state)) { |
a92e92d1 PZ |
2350 | WARN_FUNC("return with modified stack frame", |
2351 | insn->sec, insn->offset); | |
2352 | return 1; | |
2353 | } | |
2354 | ||
e7c0219b | 2355 | if (state->cfi.bp_scratch) { |
b2966952 JP |
2356 | WARN_FUNC("BP used as a scratch register", |
2357 | insn->sec, insn->offset); | |
a92e92d1 PZ |
2358 | return 1; |
2359 | } | |
2360 | ||
2361 | return 0; | |
2362 | } | |
2363 | ||
7117f16b PZ |
2364 | /* |
2365 | * Alternatives should not contain any ORC entries, this in turn means they | |
2366 | * should not contain any CFI ops, which implies all instructions should have | |
2367 | * the same same CFI state. | |
2368 | * | |
2369 | * It is possible to constuct alternatives that have unreachable holes that go | |
2370 | * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED | |
2371 | * states which then results in ORC entries, which we just said we didn't want. | |
2372 | * | |
2373 | * Avoid them by copying the CFI entry of the first instruction into the whole | |
2374 | * alternative. | |
2375 | */ | |
2376 | static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn) | |
2377 | { | |
2378 | struct instruction *first_insn = insn; | |
2379 | int alt_group = insn->alt_group; | |
2380 | ||
2381 | sec_for_each_insn_continue(file, insn) { | |
2382 | if (insn->alt_group != alt_group) | |
2383 | break; | |
2384 | insn->cfi = first_insn->cfi; | |
2385 | } | |
2386 | } | |
2387 | ||
dcc914f4 JP |
2388 | /* |
2389 | * Follow the branch starting at the given instruction, and recursively follow | |
2390 | * any other branches (jumps). Meanwhile, track the frame pointer state at | |
2391 | * each instruction and validate all the rules described in | |
2392 | * tools/objtool/Documentation/stack-validation.txt. | |
2393 | */ | |
c705cecc | 2394 | static int validate_branch(struct objtool_file *file, struct symbol *func, |
b7460462 | 2395 | struct instruction *insn, struct insn_state state) |
dcc914f4 JP |
2396 | { |
2397 | struct alternative *alt; | |
b7460462 | 2398 | struct instruction *next_insn; |
dcc914f4 | 2399 | struct section *sec; |
882a0db9 | 2400 | u8 visited; |
dcc914f4 JP |
2401 | int ret; |
2402 | ||
dcc914f4 | 2403 | sec = insn->sec; |
dcc914f4 | 2404 | |
dcc914f4 | 2405 | while (1) { |
39358a03 JP |
2406 | next_insn = next_insn_same_sec(file, insn); |
2407 | ||
13810435 | 2408 | if (file->c_file && func && insn->func && func != insn->func->pfunc) { |
ee97638b JP |
2409 | WARN("%s() falls through to next function %s()", |
2410 | func->name, insn->func->name); | |
2411 | return 1; | |
dcc914f4 JP |
2412 | } |
2413 | ||
4855022a JP |
2414 | if (func && insn->ignore) { |
2415 | WARN_FUNC("BUG: why am I validating an ignored function?", | |
2416 | sec, insn->offset); | |
12b25729 | 2417 | return 1; |
4855022a JP |
2418 | } |
2419 | ||
882a0db9 | 2420 | visited = 1 << state.uaccess; |
dcc914f4 | 2421 | if (insn->visited) { |
e7c0219b | 2422 | if (!insn->hint && !insn_cfi_match(insn, &state.cfi)) |
dcc914f4 | 2423 | return 1; |
dcc914f4 | 2424 | |
882a0db9 | 2425 | if (insn->visited & visited) |
ea24213d | 2426 | return 0; |
dcc914f4 JP |
2427 | } |
2428 | ||
c4a33939 PZ |
2429 | if (state.noinstr) |
2430 | state.instr += insn->instr; | |
2431 | ||
c536ed2f | 2432 | if (insn->hint) |
e7c0219b | 2433 | state.cfi = insn->cfi; |
c536ed2f | 2434 | else |
e7c0219b | 2435 | insn->cfi = state.cfi; |
dcc914f4 | 2436 | |
882a0db9 | 2437 | insn->visited |= visited; |
baa41469 | 2438 | |
7117f16b | 2439 | if (!insn->ignore_alts && !list_empty(&insn->alts)) { |
764eef4b PZ |
2440 | bool skip_orig = false; |
2441 | ||
a845c7cf | 2442 | list_for_each_entry(alt, &insn->alts, list) { |
764eef4b PZ |
2443 | if (alt->skip_orig) |
2444 | skip_orig = true; | |
2445 | ||
c705cecc | 2446 | ret = validate_branch(file, func, alt->insn, state); |
7697eee3 PZ |
2447 | if (ret) { |
2448 | if (backtrace) | |
2449 | BT_FUNC("(alt)", insn); | |
2450 | return ret; | |
2451 | } | |
a845c7cf | 2452 | } |
764eef4b | 2453 | |
7117f16b PZ |
2454 | if (insn->alt_group) |
2455 | fill_alternative_cfi(file, insn); | |
2456 | ||
764eef4b PZ |
2457 | if (skip_orig) |
2458 | return 0; | |
dcc914f4 JP |
2459 | } |
2460 | ||
60041bcd PZ |
2461 | if (handle_insn_ops(insn, &state)) |
2462 | return 1; | |
2463 | ||
dcc914f4 JP |
2464 | switch (insn->type) { |
2465 | ||
dcc914f4 | 2466 | case INSN_RETURN: |
a92e92d1 | 2467 | return validate_return(func, insn, &state); |
dcc914f4 JP |
2468 | |
2469 | case INSN_CALL: | |
ea24213d PZ |
2470 | case INSN_CALL_DYNAMIC: |
2471 | ret = validate_call(insn, &state); | |
2472 | if (ret) | |
2473 | return ret; | |
dcc914f4 | 2474 | |
c9bab22b JP |
2475 | if (!no_fp && func && !is_fentry_call(insn) && |
2476 | !has_valid_stack_frame(&state)) { | |
dcc914f4 JP |
2477 | WARN_FUNC("call without frame pointer save/setup", |
2478 | sec, insn->offset); | |
2479 | return 1; | |
2480 | } | |
c9bab22b JP |
2481 | |
2482 | if (dead_end_function(file, insn->call_dest)) | |
2483 | return 0; | |
2484 | ||
dcc914f4 JP |
2485 | break; |
2486 | ||
2487 | case INSN_JUMP_CONDITIONAL: | |
2488 | case INSN_JUMP_UNCONDITIONAL: | |
0c1ddd33 | 2489 | if (func && is_sibling_call(insn)) { |
54262aa2 | 2490 | ret = validate_sibling_call(insn, &state); |
dcc914f4 | 2491 | if (ret) |
54262aa2 | 2492 | return ret; |
4855022a | 2493 | |
0c1ddd33 | 2494 | } else if (insn->jump_dest) { |
c705cecc JP |
2495 | ret = validate_branch(file, func, |
2496 | insn->jump_dest, state); | |
7697eee3 PZ |
2497 | if (ret) { |
2498 | if (backtrace) | |
2499 | BT_FUNC("(branch)", insn); | |
2500 | return ret; | |
2501 | } | |
4855022a | 2502 | } |
dcc914f4 JP |
2503 | |
2504 | if (insn->type == INSN_JUMP_UNCONDITIONAL) | |
2505 | return 0; | |
2506 | ||
2507 | break; | |
2508 | ||
2509 | case INSN_JUMP_DYNAMIC: | |
b68b9907 | 2510 | case INSN_JUMP_DYNAMIC_CONDITIONAL: |
0c1ddd33 | 2511 | if (func && is_sibling_call(insn)) { |
54262aa2 PZ |
2512 | ret = validate_sibling_call(insn, &state); |
2513 | if (ret) | |
2514 | return ret; | |
dcc914f4 JP |
2515 | } |
2516 | ||
b68b9907 JP |
2517 | if (insn->type == INSN_JUMP_DYNAMIC) |
2518 | return 0; | |
2519 | ||
2520 | break; | |
dcc914f4 | 2521 | |
39358a03 JP |
2522 | case INSN_CONTEXT_SWITCH: |
2523 | if (func && (!next_insn || !next_insn->hint)) { | |
2524 | WARN_FUNC("unsupported instruction in callable function", | |
2525 | sec, insn->offset); | |
2526 | return 1; | |
2527 | } | |
2528 | return 0; | |
2529 | ||
ea24213d PZ |
2530 | case INSN_STAC: |
2531 | if (state.uaccess) { | |
2532 | WARN_FUNC("recursive UACCESS enable", sec, insn->offset); | |
2533 | return 1; | |
2534 | } | |
2535 | ||
2536 | state.uaccess = true; | |
2537 | break; | |
2538 | ||
2539 | case INSN_CLAC: | |
c705cecc | 2540 | if (!state.uaccess && func) { |
ea24213d PZ |
2541 | WARN_FUNC("redundant UACCESS disable", sec, insn->offset); |
2542 | return 1; | |
2543 | } | |
2544 | ||
2545 | if (func_uaccess_safe(func) && !state.uaccess_stack) { | |
2546 | WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset); | |
2547 | return 1; | |
2548 | } | |
2549 | ||
2550 | state.uaccess = false; | |
baa41469 JP |
2551 | break; |
2552 | ||
2f0f9e9a PZ |
2553 | case INSN_STD: |
2554 | if (state.df) | |
2555 | WARN_FUNC("recursive STD", sec, insn->offset); | |
2556 | ||
2557 | state.df = true; | |
2558 | break; | |
2559 | ||
2560 | case INSN_CLD: | |
c705cecc | 2561 | if (!state.df && func) |
2f0f9e9a PZ |
2562 | WARN_FUNC("redundant CLD", sec, insn->offset); |
2563 | ||
2564 | state.df = false; | |
baa41469 JP |
2565 | break; |
2566 | ||
dcc914f4 JP |
2567 | default: |
2568 | break; | |
2569 | } | |
2570 | ||
2571 | if (insn->dead_end) | |
2572 | return 0; | |
2573 | ||
00d96180 | 2574 | if (!next_insn) { |
e7c0219b | 2575 | if (state.cfi.cfa.base == CFI_UNDEFINED) |
00d96180 | 2576 | return 0; |
dcc914f4 JP |
2577 | WARN("%s: unexpected end of section", sec->name); |
2578 | return 1; | |
2579 | } | |
00d96180 JP |
2580 | |
2581 | insn = next_insn; | |
dcc914f4 JP |
2582 | } |
2583 | ||
2584 | return 0; | |
2585 | } | |
2586 | ||
932f8e98 | 2587 | static int validate_unwind_hints(struct objtool_file *file, struct section *sec) |
39358a03 JP |
2588 | { |
2589 | struct instruction *insn; | |
39358a03 | 2590 | struct insn_state state; |
932f8e98 | 2591 | int ret, warnings = 0; |
39358a03 JP |
2592 | |
2593 | if (!file->hints) | |
2594 | return 0; | |
2595 | ||
932f8e98 | 2596 | init_insn_state(&state, sec); |
39358a03 | 2597 | |
932f8e98 PZ |
2598 | if (sec) { |
2599 | insn = find_insn(file, sec, 0); | |
2600 | if (!insn) | |
2601 | return 0; | |
2602 | } else { | |
2603 | insn = list_first_entry(&file->insn_list, typeof(*insn), list); | |
2604 | } | |
2605 | ||
2606 | while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) { | |
39358a03 | 2607 | if (insn->hint && !insn->visited) { |
c705cecc | 2608 | ret = validate_branch(file, insn->func, insn, state); |
7697eee3 PZ |
2609 | if (ret && backtrace) |
2610 | BT_FUNC("<=== (hint)", insn); | |
39358a03 JP |
2611 | warnings += ret; |
2612 | } | |
932f8e98 PZ |
2613 | |
2614 | insn = list_next_entry(insn, list); | |
39358a03 JP |
2615 | } |
2616 | ||
2617 | return warnings; | |
2618 | } | |
2619 | ||
b5bc2231 PZ |
2620 | static int validate_retpoline(struct objtool_file *file) |
2621 | { | |
2622 | struct instruction *insn; | |
2623 | int warnings = 0; | |
2624 | ||
2625 | for_each_insn(file, insn) { | |
2626 | if (insn->type != INSN_JUMP_DYNAMIC && | |
2627 | insn->type != INSN_CALL_DYNAMIC) | |
2628 | continue; | |
2629 | ||
2630 | if (insn->retpoline_safe) | |
2631 | continue; | |
2632 | ||
ca41b97e PZ |
2633 | /* |
2634 | * .init.text code is ran before userspace and thus doesn't | |
2635 | * strictly need retpolines, except for modules which are | |
2636 | * loaded late, they very much do need retpoline in their | |
2637 | * .init.text | |
2638 | */ | |
2639 | if (!strcmp(insn->sec->name, ".init.text") && !module) | |
2640 | continue; | |
2641 | ||
b5bc2231 PZ |
2642 | WARN_FUNC("indirect %s found in RETPOLINE build", |
2643 | insn->sec, insn->offset, | |
2644 | insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call"); | |
2645 | ||
2646 | warnings++; | |
2647 | } | |
2648 | ||
2649 | return warnings; | |
2650 | } | |
2651 | ||
dcc914f4 JP |
2652 | static bool is_kasan_insn(struct instruction *insn) |
2653 | { | |
2654 | return (insn->type == INSN_CALL && | |
2655 | !strcmp(insn->call_dest->name, "__asan_handle_no_return")); | |
2656 | } | |
2657 | ||
2658 | static bool is_ubsan_insn(struct instruction *insn) | |
2659 | { | |
2660 | return (insn->type == INSN_CALL && | |
2661 | !strcmp(insn->call_dest->name, | |
2662 | "__ubsan_handle_builtin_unreachable")); | |
2663 | } | |
2664 | ||
baa41469 | 2665 | static bool ignore_unreachable_insn(struct instruction *insn) |
dcc914f4 JP |
2666 | { |
2667 | int i; | |
2668 | ||
baa41469 JP |
2669 | if (insn->ignore || insn->type == INSN_NOP) |
2670 | return true; | |
2671 | ||
2672 | /* | |
2673 | * Ignore any unused exceptions. This can happen when a whitelisted | |
2674 | * function has an exception table entry. | |
0e2bb2bc JP |
2675 | * |
2676 | * Also ignore alternative replacement instructions. This can happen | |
2677 | * when a whitelisted function uses one of the ALTERNATIVE macros. | |
baa41469 | 2678 | */ |
0e2bb2bc JP |
2679 | if (!strcmp(insn->sec->name, ".fixup") || |
2680 | !strcmp(insn->sec->name, ".altinstr_replacement") || | |
2681 | !strcmp(insn->sec->name, ".altinstr_aux")) | |
dcc914f4 JP |
2682 | return true; |
2683 | ||
bd841d61 JP |
2684 | if (!insn->func) |
2685 | return false; | |
2686 | ||
2687 | /* | |
2688 | * CONFIG_UBSAN_TRAP inserts a UD2 when it sees | |
2689 | * __builtin_unreachable(). The BUG() macro has an unreachable() after | |
2690 | * the UD2, which causes GCC's undefined trap logic to emit another UD2 | |
2691 | * (or occasionally a JMP to UD2). | |
2692 | */ | |
2693 | if (list_prev_entry(insn, list)->dead_end && | |
2694 | (insn->type == INSN_BUG || | |
2695 | (insn->type == INSN_JUMP_UNCONDITIONAL && | |
2696 | insn->jump_dest && insn->jump_dest->type == INSN_BUG))) | |
2697 | return true; | |
2698 | ||
dcc914f4 JP |
2699 | /* |
2700 | * Check if this (or a subsequent) instruction is related to | |
2701 | * CONFIG_UBSAN or CONFIG_KASAN. | |
2702 | * | |
2703 | * End the search at 5 instructions to avoid going into the weeds. | |
2704 | */ | |
2705 | for (i = 0; i < 5; i++) { | |
2706 | ||
2707 | if (is_kasan_insn(insn) || is_ubsan_insn(insn)) | |
2708 | return true; | |
2709 | ||
fe24e271 JP |
2710 | if (insn->type == INSN_JUMP_UNCONDITIONAL) { |
2711 | if (insn->jump_dest && | |
2712 | insn->jump_dest->func == insn->func) { | |
2713 | insn = insn->jump_dest; | |
2714 | continue; | |
2715 | } | |
2716 | ||
2717 | break; | |
dcc914f4 JP |
2718 | } |
2719 | ||
baa41469 | 2720 | if (insn->offset + insn->len >= insn->func->offset + insn->func->len) |
dcc914f4 | 2721 | break; |
fe24e271 | 2722 | |
dcc914f4 JP |
2723 | insn = list_next_entry(insn, list); |
2724 | } | |
2725 | ||
2726 | return false; | |
2727 | } | |
2728 | ||
4b5e2e7f PZ |
2729 | static int validate_symbol(struct objtool_file *file, struct section *sec, |
2730 | struct symbol *sym, struct insn_state *state) | |
dcc914f4 | 2731 | { |
dcc914f4 | 2732 | struct instruction *insn; |
4b5e2e7f PZ |
2733 | int ret; |
2734 | ||
2735 | if (!sym->len) { | |
2736 | WARN("%s() is missing an ELF size annotation", sym->name); | |
2737 | return 1; | |
2738 | } | |
2739 | ||
2740 | if (sym->pfunc != sym || sym->alias != sym) | |
2741 | return 0; | |
2742 | ||
2743 | insn = find_insn(file, sec, sym->offset); | |
2744 | if (!insn || insn->ignore || insn->visited) | |
2745 | return 0; | |
2746 | ||
2747 | state->uaccess = sym->uaccess_safe; | |
2748 | ||
2749 | ret = validate_branch(file, insn->func, insn, *state); | |
2750 | if (ret && backtrace) | |
2751 | BT_FUNC("<=== (sym)", insn); | |
2752 | return ret; | |
2753 | } | |
2754 | ||
2755 | static int validate_section(struct objtool_file *file, struct section *sec) | |
2756 | { | |
baa41469 | 2757 | struct insn_state state; |
4b5e2e7f PZ |
2758 | struct symbol *func; |
2759 | int warnings = 0; | |
dcc914f4 | 2760 | |
350994bf PZ |
2761 | list_for_each_entry(func, &sec->symbol_list, list) { |
2762 | if (func->type != STT_FUNC) | |
2763 | continue; | |
e10cd8fe | 2764 | |
932f8e98 | 2765 | init_insn_state(&state, sec); |
e7c0219b PZ |
2766 | state.cfi.cfa = initial_func_cfi.cfa; |
2767 | memcpy(&state.cfi.regs, &initial_func_cfi.regs, | |
0699e551 | 2768 | CFI_NUM_REGS * sizeof(struct cfi_reg)); |
e7c0219b | 2769 | state.cfi.stack_size = initial_func_cfi.cfa.offset; |
0699e551 | 2770 | |
4b5e2e7f | 2771 | warnings += validate_symbol(file, sec, func, &state); |
dcc914f4 JP |
2772 | } |
2773 | ||
dcc914f4 JP |
2774 | return warnings; |
2775 | } | |
2776 | ||
c4a33939 PZ |
2777 | static int validate_vmlinux_functions(struct objtool_file *file) |
2778 | { | |
2779 | struct section *sec; | |
932f8e98 | 2780 | int warnings = 0; |
c4a33939 PZ |
2781 | |
2782 | sec = find_section_by_name(file->elf, ".noinstr.text"); | |
0cc9ac8d TG |
2783 | if (sec) { |
2784 | warnings += validate_section(file, sec); | |
2785 | warnings += validate_unwind_hints(file, sec); | |
2786 | } | |
c4a33939 | 2787 | |
0cc9ac8d TG |
2788 | sec = find_section_by_name(file->elf, ".entry.text"); |
2789 | if (sec) { | |
2790 | warnings += validate_section(file, sec); | |
2791 | warnings += validate_unwind_hints(file, sec); | |
2792 | } | |
932f8e98 PZ |
2793 | |
2794 | return warnings; | |
c4a33939 PZ |
2795 | } |
2796 | ||
350994bf PZ |
2797 | static int validate_functions(struct objtool_file *file) |
2798 | { | |
2799 | struct section *sec; | |
2800 | int warnings = 0; | |
2801 | ||
da837bd6 PZ |
2802 | for_each_sec(file, sec) { |
2803 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) | |
2804 | continue; | |
2805 | ||
350994bf | 2806 | warnings += validate_section(file, sec); |
da837bd6 | 2807 | } |
350994bf PZ |
2808 | |
2809 | return warnings; | |
2810 | } | |
2811 | ||
baa41469 | 2812 | static int validate_reachable_instructions(struct objtool_file *file) |
dcc914f4 JP |
2813 | { |
2814 | struct instruction *insn; | |
baa41469 JP |
2815 | |
2816 | if (file->ignore_unreachables) | |
2817 | return 0; | |
dcc914f4 JP |
2818 | |
2819 | for_each_insn(file, insn) { | |
baa41469 JP |
2820 | if (insn->visited || ignore_unreachable_insn(insn)) |
2821 | continue; | |
2822 | ||
baa41469 JP |
2823 | WARN_FUNC("unreachable instruction", insn->sec, insn->offset); |
2824 | return 1; | |
dcc914f4 JP |
2825 | } |
2826 | ||
baa41469 | 2827 | return 0; |
dcc914f4 JP |
2828 | } |
2829 | ||
0c671812 JP |
2830 | static struct objtool_file file; |
2831 | ||
43a4525f | 2832 | int check(const char *_objname, bool orc) |
dcc914f4 | 2833 | { |
dcc914f4 JP |
2834 | int ret, warnings = 0; |
2835 | ||
2836 | objname = _objname; | |
dcc914f4 | 2837 | |
2b10be23 | 2838 | file.elf = elf_open_read(objname, O_RDWR); |
baa41469 | 2839 | if (!file.elf) |
dcc914f4 | 2840 | return 1; |
dcc914f4 JP |
2841 | |
2842 | INIT_LIST_HEAD(&file.insn_list); | |
2843 | hash_init(file.insn_hash); | |
734d099b | 2844 | file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment"); |
867ac9d7 | 2845 | file.ignore_unreachables = no_unreachable; |
39358a03 | 2846 | file.hints = false; |
dcc914f4 | 2847 | |
baa41469 JP |
2848 | arch_initial_func_cfi_state(&initial_func_cfi); |
2849 | ||
dcc914f4 JP |
2850 | ret = decode_sections(&file); |
2851 | if (ret < 0) | |
2852 | goto out; | |
2853 | warnings += ret; | |
2854 | ||
baa41469 | 2855 | if (list_empty(&file.insn_list)) |
dcc914f4 | 2856 | goto out; |
dcc914f4 | 2857 | |
c4a33939 PZ |
2858 | if (vmlinux && !validate_dup) { |
2859 | ret = validate_vmlinux_functions(&file); | |
2860 | if (ret < 0) | |
2861 | goto out; | |
2862 | ||
2863 | warnings += ret; | |
2864 | goto out; | |
2865 | } | |
2866 | ||
b5bc2231 PZ |
2867 | if (retpoline) { |
2868 | ret = validate_retpoline(&file); | |
2869 | if (ret < 0) | |
2870 | return ret; | |
2871 | warnings += ret; | |
2872 | } | |
2873 | ||
baa41469 | 2874 | ret = validate_functions(&file); |
dcc914f4 JP |
2875 | if (ret < 0) |
2876 | goto out; | |
2877 | warnings += ret; | |
2878 | ||
932f8e98 | 2879 | ret = validate_unwind_hints(&file, NULL); |
39358a03 JP |
2880 | if (ret < 0) |
2881 | goto out; | |
2882 | warnings += ret; | |
2883 | ||
baa41469 JP |
2884 | if (!warnings) { |
2885 | ret = validate_reachable_instructions(&file); | |
2886 | if (ret < 0) | |
2887 | goto out; | |
2888 | warnings += ret; | |
2889 | } | |
2890 | ||
627fce14 JP |
2891 | if (orc) { |
2892 | ret = create_orc(&file); | |
2893 | if (ret < 0) | |
2894 | goto out; | |
2895 | ||
2896 | ret = create_orc_sections(&file); | |
2897 | if (ret < 0) | |
2898 | goto out; | |
2b10be23 | 2899 | } |
627fce14 | 2900 | |
2b10be23 | 2901 | if (file.elf->changed) { |
627fce14 JP |
2902 | ret = elf_write(file.elf); |
2903 | if (ret < 0) | |
2904 | goto out; | |
2905 | } | |
2906 | ||
dcc914f4 | 2907 | out: |
644592d3 JP |
2908 | if (ret < 0) { |
2909 | /* | |
2910 | * Fatal error. The binary is corrupt or otherwise broken in | |
2911 | * some way, or objtool itself is broken. Fail the kernel | |
2912 | * build. | |
2913 | */ | |
2914 | return ret; | |
2915 | } | |
2916 | ||
dcc914f4 JP |
2917 | return 0; |
2918 | } |