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