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