]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-or-later | |
2 | /* | |
3 | * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> | |
4 | */ | |
5 | ||
6 | #include <string.h> | |
7 | #include <stdlib.h> | |
8 | ||
9 | #include "builtin.h" | |
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 | ||
19 | #define FAKE_JUMP_OFFSET -1 | |
20 | ||
21 | #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table" | |
22 | ||
23 | struct alternative { | |
24 | struct list_head list; | |
25 | struct instruction *insn; | |
26 | bool skip_orig; | |
27 | }; | |
28 | ||
29 | const char *objname; | |
30 | struct cfi_init_state initial_func_cfi; | |
31 | ||
32 | struct instruction *find_insn(struct objtool_file *file, | |
33 | struct section *sec, unsigned long offset) | |
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 | ||
49 | if (!next || &next->list == &file->insn_list || next->sec != insn->sec) | |
50 | return NULL; | |
51 | ||
52 | return next; | |
53 | } | |
54 | ||
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(file, func, insn) \ | |
76 | for (insn = find_insn(file, func->sec, func->offset); \ | |
77 | insn; \ | |
78 | insn = next_insn_same_func(file, insn)) | |
79 | ||
80 | #define sym_for_each_insn(file, sym, insn) \ | |
81 | for (insn = find_insn(file, sym->sec, sym->offset); \ | |
82 | insn && &insn->list != &file->insn_list && \ | |
83 | insn->sec == sym->sec && \ | |
84 | insn->offset < sym->offset + sym->len; \ | |
85 | insn = list_next_entry(insn, list)) | |
86 | ||
87 | #define sym_for_each_insn_continue_reverse(file, sym, insn) \ | |
88 | for (insn = list_prev_entry(insn, list); \ | |
89 | &insn->list != &file->insn_list && \ | |
90 | insn->sec == sym->sec && insn->offset >= sym->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 | ||
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)) | |
99 | ||
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 | ||
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 | ||
112 | if (!is_static_jump(insn)) | |
113 | return false; | |
114 | ||
115 | /* add_jump_destinations() sets insn->call_dest for sibling calls. */ | |
116 | return !!insn->call_dest; | |
117 | } | |
118 | ||
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. | |
127 | */ | |
128 | static bool __dead_end_function(struct objtool_file *file, struct symbol *func, | |
129 | int recursion) | |
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", | |
146 | "__reiserfs_panic", | |
147 | "lbug_with_loc", | |
148 | "fortify_panic", | |
149 | "usercopy_abort", | |
150 | "machine_real_restart", | |
151 | "rewind_stack_do_exit", | |
152 | "kunit_try_catch_throw", | |
153 | }; | |
154 | ||
155 | if (!func) | |
156 | return false; | |
157 | ||
158 | if (func->bind == STB_WEAK) | |
159 | return false; | |
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])) | |
164 | return true; | |
165 | ||
166 | if (!func->len) | |
167 | return false; | |
168 | ||
169 | insn = find_insn(file, func->sec, func->offset); | |
170 | if (!insn->func) | |
171 | return false; | |
172 | ||
173 | func_for_each_insn(file, func, insn) { | |
174 | empty = false; | |
175 | ||
176 | if (insn->type == INSN_RETURN) | |
177 | return false; | |
178 | } | |
179 | ||
180 | if (empty) | |
181 | return false; | |
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 | */ | |
188 | func_for_each_insn(file, func, insn) { | |
189 | if (is_sibling_call(insn)) { | |
190 | struct instruction *dest = insn->jump_dest; | |
191 | ||
192 | if (!dest) | |
193 | /* sibling call to another file */ | |
194 | return false; | |
195 | ||
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; | |
204 | } | |
205 | ||
206 | return __dead_end_function(file, dest->func, recursion+1); | |
207 | } | |
208 | } | |
209 | ||
210 | return true; | |
211 | } | |
212 | ||
213 | static bool dead_end_function(struct objtool_file *file, struct symbol *func) | |
214 | { | |
215 | return __dead_end_function(file, func, 0); | |
216 | } | |
217 | ||
218 | static void init_cfi_state(struct cfi_state *cfi) | |
219 | { | |
220 | int i; | |
221 | ||
222 | for (i = 0; i < CFI_NUM_REGS; i++) { | |
223 | cfi->regs[i].base = CFI_UNDEFINED; | |
224 | cfi->vals[i].base = CFI_UNDEFINED; | |
225 | } | |
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); | |
235 | } | |
236 | ||
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; | |
247 | unsigned long nr_insns = 0; | |
248 | int ret; | |
249 | ||
250 | for_each_sec(file, sec) { | |
251 | ||
252 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) | |
253 | continue; | |
254 | ||
255 | if (strcmp(sec->name, ".altinstr_replacement") && | |
256 | strcmp(sec->name, ".altinstr_aux") && | |
257 | strncmp(sec->name, ".discard.", 9)) | |
258 | sec->text = true; | |
259 | ||
260 | for (offset = 0; offset < sec->len; offset += insn->len) { | |
261 | insn = malloc(sizeof(*insn)); | |
262 | if (!insn) { | |
263 | WARN("malloc failed"); | |
264 | return -1; | |
265 | } | |
266 | memset(insn, 0, sizeof(*insn)); | |
267 | INIT_LIST_HEAD(&insn->alts); | |
268 | INIT_LIST_HEAD(&insn->stack_ops); | |
269 | init_cfi_state(&insn->cfi); | |
270 | ||
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, | |
277 | &insn->immediate, | |
278 | &insn->stack_ops); | |
279 | if (ret) | |
280 | goto err; | |
281 | ||
282 | hash_add(file->insn_hash, &insn->hash, insn->offset); | |
283 | list_add_tail(&insn->list, &file->insn_list); | |
284 | nr_insns++; | |
285 | } | |
286 | ||
287 | list_for_each_entry(func, &sec->symbol_list, list) { | |
288 | if (func->type != STT_FUNC || func->alias != func) | |
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 | ||
297 | sym_for_each_insn(file, func, insn) | |
298 | insn->func = func; | |
299 | } | |
300 | } | |
301 | ||
302 | if (stats) | |
303 | printf("nr_insns: %lu\n", nr_insns); | |
304 | ||
305 | return 0; | |
306 | ||
307 | err: | |
308 | free(insn); | |
309 | return ret; | |
310 | } | |
311 | ||
312 | /* | |
313 | * Mark "ud2" instructions and manually annotated dead ends. | |
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 | ||
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 | */ | |
333 | sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); | |
334 | if (!sec) | |
335 | goto reachable; | |
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 | ||
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 | ||
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; | |
421 | struct rela *rela; | |
422 | ||
423 | sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); | |
424 | if (!sec) | |
425 | return; | |
426 | ||
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: | |
434 | func = find_func_by_offset(rela->sym->sec, rela->addend); | |
435 | if (!func) | |
436 | continue; | |
437 | break; | |
438 | ||
439 | default: | |
440 | WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type); | |
441 | continue; | |
442 | } | |
443 | ||
444 | func_for_each_insn(file, func, insn) | |
445 | insn->ignore = true; | |
446 | } | |
447 | } | |
448 | ||
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", | |
497 | "__sanitizer_cov_trace_switch", | |
498 | /* UBSAN */ | |
499 | "ubsan_type_mismatch_common", | |
500 | "__ubsan_handle_type_mismatch", | |
501 | "__ubsan_handle_type_mismatch_v1", | |
502 | "__ubsan_handle_shift_out_of_bounds", | |
503 | /* misc */ | |
504 | "csum_partial_copy_generic", | |
505 | "__memcpy_mcsafe", | |
506 | "mcsafe_handle_tail", | |
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 | ||
524 | func->uaccess_safe = true; | |
525 | } | |
526 | } | |
527 | ||
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 | */ | |
534 | static int add_ignore_alternatives(struct objtool_file *file) | |
535 | { | |
536 | struct section *sec; | |
537 | struct rela *rela; | |
538 | struct instruction *insn; | |
539 | ||
540 | sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts"); | |
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) { | |
552 | WARN("bad .discard.ignore_alts entry"); | |
553 | return -1; | |
554 | } | |
555 | ||
556 | insn->ignore_alts = true; | |
557 | } | |
558 | ||
559 | return 0; | |
560 | } | |
561 | ||
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) { | |
573 | if (!is_static_jump(insn)) | |
574 | continue; | |
575 | ||
576 | if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET) | |
577 | continue; | |
578 | ||
579 | rela = find_rela_by_dest_range(file->elf, insn->sec, | |
580 | insn->offset, insn->len); | |
581 | if (!rela) { | |
582 | dest_sec = insn->sec; | |
583 | dest_off = arch_jump_destination(insn); | |
584 | } else if (rela->sym->type == STT_SECTION) { | |
585 | dest_sec = rela->sym->sec; | |
586 | dest_off = arch_dest_rela_offset(rela->addend); | |
587 | } else if (rela->sym->sec->idx) { | |
588 | dest_sec = rela->sym->sec; | |
589 | dest_off = rela->sym->sym.st_value + | |
590 | arch_dest_rela_offset(rela->addend); | |
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 | */ | |
596 | if (insn->type == INSN_JUMP_UNCONDITIONAL) | |
597 | insn->type = INSN_JUMP_DYNAMIC; | |
598 | else | |
599 | insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL; | |
600 | ||
601 | insn->retpoline_safe = true; | |
602 | continue; | |
603 | } else { | |
604 | /* external sibling call */ | |
605 | insn->call_dest = rela->sym; | |
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 | } | |
625 | ||
626 | /* | |
627 | * Cross-function jump. | |
628 | */ | |
629 | if (insn->func && insn->jump_dest->func && | |
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 | |
645 | * subfunction is through a jump table. | |
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 | ||
655 | /* internal sibling call */ | |
656 | insn->call_dest = insn->jump_dest->func; | |
657 | } | |
658 | } | |
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 | ||
677 | rela = find_rela_by_dest_range(file->elf, insn->sec, | |
678 | insn->offset, insn->len); | |
679 | if (!rela) { | |
680 | dest_off = arch_jump_destination(insn); | |
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); | |
684 | ||
685 | if (insn->ignore) | |
686 | continue; | |
687 | ||
688 | if (!insn->call_dest) { | |
689 | WARN_FUNC("unsupported intra-function call", | |
690 | insn->sec, insn->offset); | |
691 | if (retpoline) | |
692 | WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE."); | |
693 | return -1; | |
694 | } | |
695 | ||
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 | ||
702 | } else if (rela->sym->type == STT_SECTION) { | |
703 | dest_off = arch_dest_rela_offset(rela->addend); | |
704 | insn->call_dest = find_func_by_offset(rela->sym->sec, | |
705 | dest_off); | |
706 | if (!insn->call_dest) { | |
707 | WARN_FUNC("can't find call dest symbol at %s+0x%lx", | |
708 | insn->sec, insn->offset, | |
709 | rela->sym->sec->name, | |
710 | dest_off); | |
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. | |
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 | { | |
742 | struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL; | |
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 | ||
751 | insn->alt_group = true; | |
752 | last_orig_insn = insn; | |
753 | } | |
754 | ||
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); | |
763 | INIT_LIST_HEAD(&fake_jump->stack_ops); | |
764 | init_cfi_state(&fake_jump->cfi); | |
765 | ||
766 | fake_jump->sec = special_alt->new_sec; | |
767 | fake_jump->offset = FAKE_JUMP_OFFSET; | |
768 | fake_jump->type = INSN_JUMP_UNCONDITIONAL; | |
769 | fake_jump->jump_dest = list_next_entry(last_orig_insn, list); | |
770 | fake_jump->func = orig_insn->func; | |
771 | } | |
772 | ||
773 | if (!special_alt->new_len) { | |
774 | if (!fake_jump) { | |
775 | WARN("%s: empty alternative at end of section", | |
776 | special_alt->orig_sec->name); | |
777 | return -1; | |
778 | } | |
779 | ||
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 | ||
792 | insn->ignore = orig_insn->ignore_alts; | |
793 | insn->func = orig_insn->func; | |
794 | ||
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))) && | |
809 | find_rela_by_dest_range(file->elf, insn->sec, insn->offset, insn->len)) { | |
810 | ||
811 | WARN_FUNC("unsupported relocation in alternatives section", | |
812 | insn->sec, insn->offset); | |
813 | return -1; | |
814 | } | |
815 | ||
816 | if (!is_static_jump(insn)) | |
817 | continue; | |
818 | ||
819 | if (!insn->immediate) | |
820 | continue; | |
821 | ||
822 | dest_off = arch_jump_destination(insn); | |
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 | } | |
829 | insn->jump_dest = fake_jump; | |
830 | } | |
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 | ||
845 | if (fake_jump) | |
846 | list_add(&fake_jump->list, &last_new_insn->list); | |
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) { | |
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) { | |
917 | if (!special_alt->orig_len) { | |
918 | WARN_FUNC("empty alternative entry", | |
919 | orig_insn->sec, orig_insn->offset); | |
920 | continue; | |
921 | } | |
922 | ||
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 | ||
934 | alt = malloc(sizeof(*alt)); | |
935 | if (!alt) { | |
936 | WARN("malloc failed"); | |
937 | ret = -1; | |
938 | goto out; | |
939 | } | |
940 | ||
941 | alt->insn = new_insn; | |
942 | alt->skip_orig = special_alt->skip_orig; | |
943 | orig_insn->ignore_alts |= special_alt->skip_alt; | |
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 | ||
954 | static int add_jump_table(struct objtool_file *file, struct instruction *insn, | |
955 | struct rela *table) | |
956 | { | |
957 | struct rela *rela = table; | |
958 | struct instruction *dest_insn; | |
959 | struct alternative *alt; | |
960 | struct symbol *pfunc = insn->func->pfunc; | |
961 | unsigned int prev_offset = 0; | |
962 | ||
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) { | |
968 | ||
969 | /* Check for the end of the table: */ | |
970 | if (rela != table && rela->jump_table_start) | |
971 | break; | |
972 | ||
973 | /* Make sure the table entries are consecutive: */ | |
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 | ||
982 | dest_insn = find_insn(file, rela->sym->sec, rela->addend); | |
983 | if (!dest_insn) | |
984 | break; | |
985 | ||
986 | /* Make sure the destination is in the same function: */ | |
987 | if (!dest_insn->func || dest_insn->func->pfunc != pfunc) | |
988 | break; | |
989 | ||
990 | alt = malloc(sizeof(*alt)); | |
991 | if (!alt) { | |
992 | WARN("malloc failed"); | |
993 | return -1; | |
994 | } | |
995 | ||
996 | alt->insn = dest_insn; | |
997 | list_add_tail(&alt->list, &insn->alts); | |
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; | |
1005 | } | |
1006 | ||
1007 | return 0; | |
1008 | } | |
1009 | ||
1010 | /* | |
1011 | * find_jump_table() - Given a dynamic jump, find the switch jump table in | |
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 | * | |
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 | * | |
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. | |
1050 | * | |
1051 | * NOTE: RETPOLINE made it harder still to decode dynamic jumps. | |
1052 | */ | |
1053 | static struct rela *find_jump_table(struct objtool_file *file, | |
1054 | struct symbol *func, | |
1055 | struct instruction *insn) | |
1056 | { | |
1057 | struct rela *text_rela, *table_rela; | |
1058 | struct instruction *dest_insn, *orig_insn = insn; | |
1059 | struct section *table_sec; | |
1060 | unsigned long table_offset; | |
1061 | ||
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 | */ | |
1067 | for (; | |
1068 | &insn->list != &file->insn_list && insn->func && insn->func->pfunc == func; | |
1069 | insn = insn->first_jump_src ?: list_prev_entry(insn, list)) { | |
1070 | ||
1071 | if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC) | |
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 */ | |
1082 | text_rela = find_rela_by_dest_range(file->elf, insn->sec, | |
1083 | insn->offset, insn->len); | |
1084 | if (!text_rela || text_rela->sym->type != STT_SECTION || | |
1085 | !text_rela->sym->sec->rodata) | |
1086 | continue; | |
1087 | ||
1088 | table_offset = text_rela->addend; | |
1089 | table_sec = text_rela->sym->sec; | |
1090 | ||
1091 | if (text_rela->type == R_X86_64_PC32) | |
1092 | table_offset += 4; | |
1093 | ||
1094 | /* | |
1095 | * Make sure the .rodata address isn't associated with a | |
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. | |
1102 | */ | |
1103 | if (find_symbol_containing(table_sec, table_offset) && | |
1104 | strcmp(table_sec->name, C_JUMP_TABLE_SECTION)) | |
1105 | continue; | |
1106 | ||
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 | */ | |
1112 | table_rela = find_rela_by_dest(file->elf, table_sec, table_offset); | |
1113 | if (!table_rela) | |
1114 | continue; | |
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; | |
1118 | ||
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; | |
1128 | } | |
1129 | ||
1130 | return NULL; | |
1131 | } | |
1132 | ||
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) | |
1139 | { | |
1140 | struct instruction *insn, *last = NULL; | |
1141 | struct rela *rela; | |
1142 | ||
1143 | func_for_each_insn(file, func, insn) { | |
1144 | if (!last) | |
1145 | last = insn; | |
1146 | ||
1147 | /* | |
1148 | * Store back-pointers for unconditional forward jumps such | |
1149 | * that find_jump_table() can back-track using those and | |
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 | ||
1161 | if (insn->type != INSN_JUMP_DYNAMIC) | |
1162 | continue; | |
1163 | ||
1164 | rela = find_jump_table(file, func, insn); | |
1165 | if (rela) { | |
1166 | rela->jump_table_start = true; | |
1167 | insn->jump_table = rela; | |
1168 | } | |
1169 | } | |
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 | ||
1178 | func_for_each_insn(file, func, insn) { | |
1179 | if (!insn->jump_table) | |
1180 | continue; | |
1181 | ||
1182 | ret = add_jump_table(file, insn, insn->jump_table); | |
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 | */ | |
1195 | static int add_jump_table_alts(struct objtool_file *file) | |
1196 | { | |
1197 | struct section *sec; | |
1198 | struct symbol *func; | |
1199 | int ret; | |
1200 | ||
1201 | if (!file->rodata) | |
1202 | return 0; | |
1203 | ||
1204 | for_each_sec(file, sec) { | |
1205 | list_for_each_entry(func, &sec->symbol_list, list) { | |
1206 | if (func->type != STT_FUNC) | |
1207 | continue; | |
1208 | ||
1209 | mark_func_jump_tables(file, func); | |
1210 | ret = add_func_jump_tables(file, func); | |
1211 | if (ret) | |
1212 | return ret; | |
1213 | } | |
1214 | } | |
1215 | ||
1216 | return 0; | |
1217 | } | |
1218 | ||
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 | ||
1248 | rela = find_rela_by_dest(file->elf, sec, i * sizeof(*hint)); | |
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 | ||
1260 | cfa = &insn->cfi.cfa; | |
1261 | ||
1262 | if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) { | |
1263 | insn->ret_offset = hint->sp_offset; | |
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; | |
1301 | insn->cfi.type = hint->type; | |
1302 | insn->cfi.end = hint->end; | |
1303 | } | |
1304 | ||
1305 | return 0; | |
1306 | } | |
1307 | ||
1308 | static int read_retpoline_hints(struct objtool_file *file) | |
1309 | { | |
1310 | struct section *sec; | |
1311 | struct instruction *insn; | |
1312 | struct rela *rela; | |
1313 | ||
1314 | sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); | |
1315 | if (!sec) | |
1316 | return 0; | |
1317 | ||
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); | |
1321 | return -1; | |
1322 | } | |
1323 | ||
1324 | insn = find_insn(file, rela->sym->sec, rela->addend); | |
1325 | if (!insn) { | |
1326 | WARN("bad .discard.retpoline_safe entry"); | |
1327 | return -1; | |
1328 | } | |
1329 | ||
1330 | if (insn->type != INSN_JUMP_DYNAMIC && | |
1331 | insn->type != INSN_CALL_DYNAMIC) { | |
1332 | WARN_FUNC("retpoline_safe hint not an indirect jump/call", | |
1333 | insn->sec, insn->offset); | |
1334 | return -1; | |
1335 | } | |
1336 | ||
1337 | insn->retpoline_safe = true; | |
1338 | } | |
1339 | ||
1340 | return 0; | |
1341 | } | |
1342 | ||
1343 | static void mark_rodata(struct objtool_file *file) | |
1344 | { | |
1345 | struct section *sec; | |
1346 | bool found = false; | |
1347 | ||
1348 | /* | |
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. | |
1357 | */ | |
1358 | for_each_sec(file, sec) { | |
1359 | if (!strncmp(sec->name, ".rodata", 7) && | |
1360 | !strstr(sec->name, ".str1.")) { | |
1361 | sec->rodata = true; | |
1362 | found = true; | |
1363 | } | |
1364 | } | |
1365 | ||
1366 | file->rodata = found; | |
1367 | } | |
1368 | ||
1369 | static int decode_sections(struct objtool_file *file) | |
1370 | { | |
1371 | int ret; | |
1372 | ||
1373 | mark_rodata(file); | |
1374 | ||
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); | |
1384 | add_uaccess_safe(file); | |
1385 | ||
1386 | ret = add_ignore_alternatives(file); | |
1387 | if (ret) | |
1388 | return ret; | |
1389 | ||
1390 | ret = add_jump_destinations(file); | |
1391 | if (ret) | |
1392 | return ret; | |
1393 | ||
1394 | ret = add_special_section_alts(file); | |
1395 | if (ret) | |
1396 | return ret; | |
1397 | ||
1398 | ret = add_call_destinations(file); | |
1399 | if (ret) | |
1400 | return ret; | |
1401 | ||
1402 | ret = add_jump_table_alts(file); | |
1403 | if (ret) | |
1404 | return ret; | |
1405 | ||
1406 | ret = read_unwind_hints(file); | |
1407 | if (ret) | |
1408 | return ret; | |
1409 | ||
1410 | ret = read_retpoline_hints(file); | |
1411 | if (ret) | |
1412 | return ret; | |
1413 | ||
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 | ||
1427 | static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state) | |
1428 | { | |
1429 | u8 ret_offset = insn->ret_offset; | |
1430 | struct cfi_state *cfi = &state->cfi; | |
1431 | int i; | |
1432 | ||
1433 | if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap) | |
1434 | return true; | |
1435 | ||
1436 | if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset) | |
1437 | return true; | |
1438 | ||
1439 | if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset) | |
1440 | return true; | |
1441 | ||
1442 | for (i = 0; i < CFI_NUM_REGS; i++) { | |
1443 | if (cfi->regs[i].base != initial_func_cfi.regs[i].base || | |
1444 | cfi->regs[i].offset != initial_func_cfi.regs[i].offset) | |
1445 | return true; | |
1446 | } | |
1447 | ||
1448 | return false; | |
1449 | } | |
1450 | ||
1451 | static bool has_valid_stack_frame(struct insn_state *state) | |
1452 | { | |
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) | |
1457 | return true; | |
1458 | ||
1459 | if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP) | |
1460 | return true; | |
1461 | ||
1462 | return false; | |
1463 | } | |
1464 | ||
1465 | static int update_cfi_state_regs(struct instruction *insn, | |
1466 | struct cfi_state *cfi, | |
1467 | struct stack_op *op) | |
1468 | { | |
1469 | struct cfi_reg *cfa = &cfi->cfa; | |
1470 | ||
1471 | if (cfa->base != CFI_SP) | |
1472 | return 0; | |
1473 | ||
1474 | /* push */ | |
1475 | if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF) | |
1476 | cfa->offset += 8; | |
1477 | ||
1478 | /* pop */ | |
1479 | if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF) | |
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 | ||
1490 | static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset) | |
1491 | { | |
1492 | if (arch_callee_saved_reg(reg) && | |
1493 | cfi->regs[reg].base == CFI_UNDEFINED) { | |
1494 | cfi->regs[reg].base = base; | |
1495 | cfi->regs[reg].offset = offset; | |
1496 | } | |
1497 | } | |
1498 | ||
1499 | static void restore_reg(struct cfi_state *cfi, unsigned char reg) | |
1500 | { | |
1501 | cfi->regs[reg].base = initial_func_cfi.regs[reg].base; | |
1502 | cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset; | |
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 | */ | |
1558 | static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi, | |
1559 | struct stack_op *op) | |
1560 | { | |
1561 | struct cfi_reg *cfa = &cfi->cfa; | |
1562 | struct cfi_reg *regs = cfi->regs; | |
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 | ||
1573 | if (cfi->type == ORC_TYPE_REGS || cfi->type == ORC_TYPE_REGS_IRET) | |
1574 | return update_cfi_state_regs(insn, cfi, op); | |
1575 | ||
1576 | switch (op->dest.type) { | |
1577 | ||
1578 | case OP_DEST_REG: | |
1579 | switch (op->src.type) { | |
1580 | ||
1581 | case OP_SRC_REG: | |
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; | |
1589 | cfi->bp_scratch = false; | |
1590 | } | |
1591 | ||
1592 | else if (op->src.reg == CFI_SP && | |
1593 | op->dest.reg == CFI_BP && cfi->drap) { | |
1594 | ||
1595 | /* drap: mov %rsp, %rbp */ | |
1596 | regs[CFI_BP].base = CFI_BP; | |
1597 | regs[CFI_BP].offset = -cfi->stack_size; | |
1598 | cfi->bp_scratch = false; | |
1599 | } | |
1600 | ||
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 | */ | |
1613 | cfi->vals[op->dest.reg].base = CFI_CFA; | |
1614 | cfi->vals[op->dest.reg].offset = -cfi->stack_size; | |
1615 | } | |
1616 | ||
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 | */ | |
1625 | cfi->stack_size = -cfi->regs[CFI_BP].offset; | |
1626 | } | |
1627 | ||
1628 | else if (op->dest.reg == cfa->base) { | |
1629 | ||
1630 | /* mov %reg, %rsp */ | |
1631 | if (cfa->base == CFI_SP && | |
1632 | cfi->vals[op->src.reg].base == CFI_CFA) { | |
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 | */ | |
1642 | cfa->offset = -cfi->vals[op->src.reg].offset; | |
1643 | cfi->stack_size = cfa->offset; | |
1644 | ||
1645 | } else { | |
1646 | cfa->base = CFI_UNDEFINED; | |
1647 | cfa->offset = 0; | |
1648 | } | |
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 */ | |
1657 | cfi->stack_size -= op->src.offset; | |
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 */ | |
1666 | cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset); | |
1667 | break; | |
1668 | } | |
1669 | ||
1670 | if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { | |
1671 | ||
1672 | /* drap: lea disp(%rsp), %drap */ | |
1673 | cfi->drap_reg = op->dest.reg; | |
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 | */ | |
1685 | cfi->vals[op->dest.reg].base = CFI_CFA; | |
1686 | cfi->vals[op->dest.reg].offset = \ | |
1687 | -cfi->stack_size + op->src.offset; | |
1688 | ||
1689 | break; | |
1690 | } | |
1691 | ||
1692 | if (cfi->drap && op->dest.reg == CFI_SP && | |
1693 | op->src.reg == cfi->drap_reg) { | |
1694 | ||
1695 | /* drap: lea disp(%drap), %rsp */ | |
1696 | cfa->base = CFI_SP; | |
1697 | cfa->offset = cfi->stack_size = -op->src.offset; | |
1698 | cfi->drap_reg = CFI_UNDEFINED; | |
1699 | cfi->drap = false; | |
1700 | break; | |
1701 | } | |
1702 | ||
1703 | if (op->dest.reg == cfi->cfa.base) { | |
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 || | |
1713 | (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) || | |
1714 | (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) { | |
1715 | WARN_FUNC("unsupported stack pointer realignment", | |
1716 | insn->sec, insn->offset); | |
1717 | return -1; | |
1718 | } | |
1719 | ||
1720 | if (cfi->drap_reg != CFI_UNDEFINED) { | |
1721 | /* drap: and imm, %rsp */ | |
1722 | cfa->base = cfi->drap_reg; | |
1723 | cfa->offset = cfi->stack_size = 0; | |
1724 | cfi->drap = true; | |
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: | |
1735 | case OP_SRC_POPF: | |
1736 | if (!cfi->drap && op->dest.reg == cfa->base) { | |
1737 | ||
1738 | /* pop %rbp */ | |
1739 | cfa->base = CFI_SP; | |
1740 | } | |
1741 | ||
1742 | if (cfi->drap && cfa->base == CFI_BP_INDIRECT && | |
1743 | op->dest.reg == cfi->drap_reg && | |
1744 | cfi->drap_offset == -cfi->stack_size) { | |
1745 | ||
1746 | /* drap: pop %drap */ | |
1747 | cfa->base = cfi->drap_reg; | |
1748 | cfa->offset = 0; | |
1749 | cfi->drap_offset = -1; | |
1750 | ||
1751 | } else if (regs[op->dest.reg].offset == -cfi->stack_size) { | |
1752 | ||
1753 | /* pop %reg */ | |
1754 | restore_reg(cfi, op->dest.reg); | |
1755 | } | |
1756 | ||
1757 | cfi->stack_size -= 8; | |
1758 | if (cfa->base == CFI_SP) | |
1759 | cfa->offset -= 8; | |
1760 | ||
1761 | break; | |
1762 | ||
1763 | case OP_SRC_REG_INDIRECT: | |
1764 | if (cfi->drap && op->src.reg == CFI_BP && | |
1765 | op->src.offset == cfi->drap_offset) { | |
1766 | ||
1767 | /* drap: mov disp(%rbp), %drap */ | |
1768 | cfa->base = cfi->drap_reg; | |
1769 | cfa->offset = 0; | |
1770 | cfi->drap_offset = -1; | |
1771 | } | |
1772 | ||
1773 | if (cfi->drap && op->src.reg == CFI_BP && | |
1774 | op->src.offset == regs[op->dest.reg].offset) { | |
1775 | ||
1776 | /* drap: mov disp(%rbp), %reg */ | |
1777 | restore_reg(cfi, op->dest.reg); | |
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 */ | |
1784 | restore_reg(cfi, op->dest.reg); | |
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: | |
1798 | case OP_DEST_PUSHF: | |
1799 | cfi->stack_size += 8; | |
1800 | if (cfa->base == CFI_SP) | |
1801 | cfa->offset += 8; | |
1802 | ||
1803 | if (op->src.type != OP_SRC_REG) | |
1804 | break; | |
1805 | ||
1806 | if (cfi->drap) { | |
1807 | if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { | |
1808 | ||
1809 | /* drap: push %drap */ | |
1810 | cfa->base = CFI_BP_INDIRECT; | |
1811 | cfa->offset = -cfi->stack_size; | |
1812 | ||
1813 | /* save drap so we know when to restore it */ | |
1814 | cfi->drap_offset = -cfi->stack_size; | |
1815 | ||
1816 | } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) { | |
1817 | ||
1818 | /* drap: push %rbp */ | |
1819 | cfi->stack_size = 0; | |
1820 | ||
1821 | } else if (regs[op->src.reg].base == CFI_UNDEFINED) { | |
1822 | ||
1823 | /* drap: push %reg */ | |
1824 | save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size); | |
1825 | } | |
1826 | ||
1827 | } else { | |
1828 | ||
1829 | /* push %reg */ | |
1830 | save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size); | |
1831 | } | |
1832 | ||
1833 | /* detect when asm code uses rbp as a scratch register */ | |
1834 | if (!no_fp && insn->func && op->src.reg == CFI_BP && | |
1835 | cfa->base != CFI_BP) | |
1836 | cfi->bp_scratch = true; | |
1837 | break; | |
1838 | ||
1839 | case OP_DEST_REG_INDIRECT: | |
1840 | ||
1841 | if (cfi->drap) { | |
1842 | if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { | |
1843 | ||
1844 | /* drap: mov %drap, disp(%rbp) */ | |
1845 | cfa->base = CFI_BP_INDIRECT; | |
1846 | cfa->offset = op->dest.offset; | |
1847 | ||
1848 | /* save drap offset so we know when to restore it */ | |
1849 | cfi->drap_offset = op->dest.offset; | |
1850 | } | |
1851 | ||
1852 | else if (regs[op->src.reg].base == CFI_UNDEFINED) { | |
1853 | ||
1854 | /* drap: mov reg, disp(%rbp) */ | |
1855 | save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset); | |
1856 | } | |
1857 | ||
1858 | } else if (op->dest.reg == cfa->base) { | |
1859 | ||
1860 | /* mov reg, disp(%rbp) */ | |
1861 | /* mov reg, disp(%rsp) */ | |
1862 | save_reg(cfi, op->src.reg, CFI_CFA, | |
1863 | op->dest.offset - cfi->cfa.offset); | |
1864 | } | |
1865 | ||
1866 | break; | |
1867 | ||
1868 | case OP_DEST_LEAVE: | |
1869 | if ((!cfi->drap && cfa->base != CFI_BP) || | |
1870 | (cfi->drap && cfa->base != cfi->drap_reg)) { | |
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 | ||
1878 | cfi->stack_size = -cfi->regs[CFI_BP].offset - 8; | |
1879 | restore_reg(cfi, CFI_BP); | |
1880 | ||
1881 | if (!cfi->drap) { | |
1882 | cfa->base = CFI_SP; | |
1883 | cfa->offset -= 8; | |
1884 | } | |
1885 | ||
1886 | break; | |
1887 | ||
1888 | case OP_DEST_MEM: | |
1889 | if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) { | |
1890 | WARN_FUNC("unknown stack-related memory operation", | |
1891 | insn->sec, insn->offset); | |
1892 | return -1; | |
1893 | } | |
1894 | ||
1895 | /* pop mem */ | |
1896 | cfi->stack_size -= 8; | |
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 | ||
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 | ||
1918 | res = update_cfi_state(insn, &state->cfi, op); | |
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 | ||
1947 | static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2) | |
1948 | { | |
1949 | struct cfi_state *cfi1 = &insn->cfi; | |
1950 | int i; | |
1951 | ||
1952 | if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) { | |
1953 | ||
1954 | WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d", | |
1955 | insn->sec, insn->offset, | |
1956 | cfi1->cfa.base, cfi1->cfa.offset, | |
1957 | cfi2->cfa.base, cfi2->cfa.offset); | |
1958 | ||
1959 | } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) { | |
1960 | for (i = 0; i < CFI_NUM_REGS; i++) { | |
1961 | if (!memcmp(&cfi1->regs[i], &cfi2->regs[i], | |
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, | |
1967 | i, cfi1->regs[i].base, cfi1->regs[i].offset, | |
1968 | i, cfi2->regs[i].base, cfi2->regs[i].offset); | |
1969 | break; | |
1970 | } | |
1971 | ||
1972 | } else if (cfi1->type != cfi2->type) { | |
1973 | ||
1974 | WARN_FUNC("stack state mismatch: type1=%d type2=%d", | |
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)) { | |
1980 | ||
1981 | WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)", | |
1982 | insn->sec, insn->offset, | |
1983 | cfi1->drap, cfi1->drap_reg, cfi1->drap_offset, | |
1984 | cfi2->drap, cfi2->drap_reg, cfi2->drap_offset); | |
1985 | ||
1986 | } else | |
1987 | return true; | |
1988 | ||
1989 | return false; | |
1990 | } | |
1991 | ||
1992 | static inline bool func_uaccess_safe(struct symbol *func) | |
1993 | { | |
1994 | if (func) | |
1995 | return func->uaccess_safe; | |
1996 | ||
1997 | return false; | |
1998 | } | |
1999 | ||
2000 | static inline const char *call_dest_name(struct instruction *insn) | |
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", | |
2012 | insn->sec, insn->offset, call_dest_name(insn)); | |
2013 | return 1; | |
2014 | } | |
2015 | ||
2016 | if (state->df) { | |
2017 | WARN_FUNC("call to %s() with DF set", | |
2018 | insn->sec, insn->offset, call_dest_name(insn)); | |
2019 | return 1; | |
2020 | } | |
2021 | ||
2022 | return 0; | |
2023 | } | |
2024 | ||
2025 | static int validate_sibling_call(struct instruction *insn, struct insn_state *state) | |
2026 | { | |
2027 | if (has_modified_stack_frame(insn, state)) { | |
2028 | WARN_FUNC("sibling call from callable instruction with modified stack frame", | |
2029 | insn->sec, insn->offset); | |
2030 | return 1; | |
2031 | } | |
2032 | ||
2033 | return validate_call(insn, state); | |
2034 | } | |
2035 | ||
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 | ||
2056 | if (func && has_modified_stack_frame(insn, state)) { | |
2057 | WARN_FUNC("return with modified stack frame", | |
2058 | insn->sec, insn->offset); | |
2059 | return 1; | |
2060 | } | |
2061 | ||
2062 | if (state->cfi.bp_scratch) { | |
2063 | WARN_FUNC("BP used as a scratch register", | |
2064 | insn->sec, insn->offset); | |
2065 | return 1; | |
2066 | } | |
2067 | ||
2068 | return 0; | |
2069 | } | |
2070 | ||
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 | */ | |
2077 | static int validate_branch(struct objtool_file *file, struct symbol *func, | |
2078 | struct instruction *insn, struct insn_state state) | |
2079 | { | |
2080 | struct alternative *alt; | |
2081 | struct instruction *next_insn; | |
2082 | struct section *sec; | |
2083 | u8 visited; | |
2084 | int ret; | |
2085 | ||
2086 | sec = insn->sec; | |
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); | |
2091 | return 1; | |
2092 | } | |
2093 | ||
2094 | while (1) { | |
2095 | next_insn = next_insn_same_sec(file, insn); | |
2096 | ||
2097 | if (file->c_file && func && insn->func && func != insn->func->pfunc) { | |
2098 | WARN("%s() falls through to next function %s()", | |
2099 | func->name, insn->func->name); | |
2100 | return 1; | |
2101 | } | |
2102 | ||
2103 | if (func && insn->ignore) { | |
2104 | WARN_FUNC("BUG: why am I validating an ignored function?", | |
2105 | sec, insn->offset); | |
2106 | return 1; | |
2107 | } | |
2108 | ||
2109 | visited = 1 << state.uaccess; | |
2110 | if (insn->visited) { | |
2111 | if (!insn->hint && !insn_cfi_match(insn, &state.cfi)) | |
2112 | return 1; | |
2113 | ||
2114 | if (insn->visited & visited) | |
2115 | return 0; | |
2116 | } | |
2117 | ||
2118 | if (insn->hint) | |
2119 | state.cfi = insn->cfi; | |
2120 | else | |
2121 | insn->cfi = state.cfi; | |
2122 | ||
2123 | insn->visited |= visited; | |
2124 | ||
2125 | if (!insn->ignore_alts) { | |
2126 | bool skip_orig = false; | |
2127 | ||
2128 | list_for_each_entry(alt, &insn->alts, list) { | |
2129 | if (alt->skip_orig) | |
2130 | skip_orig = true; | |
2131 | ||
2132 | ret = validate_branch(file, func, alt->insn, state); | |
2133 | if (ret) { | |
2134 | if (backtrace) | |
2135 | BT_FUNC("(alt)", insn); | |
2136 | return ret; | |
2137 | } | |
2138 | } | |
2139 | ||
2140 | if (skip_orig) | |
2141 | return 0; | |
2142 | } | |
2143 | ||
2144 | switch (insn->type) { | |
2145 | ||
2146 | case INSN_RETURN: | |
2147 | return validate_return(func, insn, &state); | |
2148 | ||
2149 | case INSN_CALL: | |
2150 | case INSN_CALL_DYNAMIC: | |
2151 | ret = validate_call(insn, &state); | |
2152 | if (ret) | |
2153 | return ret; | |
2154 | ||
2155 | if (!no_fp && func && !is_fentry_call(insn) && | |
2156 | !has_valid_stack_frame(&state)) { | |
2157 | WARN_FUNC("call without frame pointer save/setup", | |
2158 | sec, insn->offset); | |
2159 | return 1; | |
2160 | } | |
2161 | ||
2162 | if (dead_end_function(file, insn->call_dest)) | |
2163 | return 0; | |
2164 | ||
2165 | break; | |
2166 | ||
2167 | case INSN_JUMP_CONDITIONAL: | |
2168 | case INSN_JUMP_UNCONDITIONAL: | |
2169 | if (func && is_sibling_call(insn)) { | |
2170 | ret = validate_sibling_call(insn, &state); | |
2171 | if (ret) | |
2172 | return ret; | |
2173 | ||
2174 | } else if (insn->jump_dest) { | |
2175 | ret = validate_branch(file, func, | |
2176 | insn->jump_dest, state); | |
2177 | if (ret) { | |
2178 | if (backtrace) | |
2179 | BT_FUNC("(branch)", insn); | |
2180 | return ret; | |
2181 | } | |
2182 | } | |
2183 | ||
2184 | if (insn->type == INSN_JUMP_UNCONDITIONAL) | |
2185 | return 0; | |
2186 | ||
2187 | break; | |
2188 | ||
2189 | case INSN_JUMP_DYNAMIC: | |
2190 | case INSN_JUMP_DYNAMIC_CONDITIONAL: | |
2191 | if (func && is_sibling_call(insn)) { | |
2192 | ret = validate_sibling_call(insn, &state); | |
2193 | if (ret) | |
2194 | return ret; | |
2195 | } | |
2196 | ||
2197 | if (insn->type == INSN_JUMP_DYNAMIC) | |
2198 | return 0; | |
2199 | ||
2200 | break; | |
2201 | ||
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 | ||
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 | ||
2224 | case INSN_STACK: | |
2225 | if (handle_insn_ops(insn, &state)) | |
2226 | return 1; | |
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: | |
2239 | if (!state.uaccess && func) { | |
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; | |
2250 | break; | |
2251 | ||
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: | |
2260 | if (!state.df && func) | |
2261 | WARN_FUNC("redundant CLD", sec, insn->offset); | |
2262 | ||
2263 | state.df = false; | |
2264 | break; | |
2265 | ||
2266 | default: | |
2267 | break; | |
2268 | } | |
2269 | ||
2270 | if (insn->dead_end) | |
2271 | return 0; | |
2272 | ||
2273 | if (!next_insn) { | |
2274 | if (state.cfi.cfa.base == CFI_UNDEFINED) | |
2275 | return 0; | |
2276 | WARN("%s: unexpected end of section", sec->name); | |
2277 | return 1; | |
2278 | } | |
2279 | ||
2280 | insn = next_insn; | |
2281 | } | |
2282 | ||
2283 | return 0; | |
2284 | } | |
2285 | ||
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) { | |
2299 | ret = validate_branch(file, insn->func, insn, state); | |
2300 | if (ret && backtrace) | |
2301 | BT_FUNC("<=== (hint)", insn); | |
2302 | warnings += ret; | |
2303 | } | |
2304 | } | |
2305 | ||
2306 | return warnings; | |
2307 | } | |
2308 | ||
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 | ||
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 | ||
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 | ||
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 | ||
2354 | static bool ignore_unreachable_insn(struct instruction *insn) | |
2355 | { | |
2356 | int i; | |
2357 | ||
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. | |
2364 | * | |
2365 | * Also ignore alternative replacement instructions. This can happen | |
2366 | * when a whitelisted function uses one of the ALTERNATIVE macros. | |
2367 | */ | |
2368 | if (!strcmp(insn->sec->name, ".fixup") || | |
2369 | !strcmp(insn->sec->name, ".altinstr_replacement") || | |
2370 | !strcmp(insn->sec->name, ".altinstr_aux")) | |
2371 | return true; | |
2372 | ||
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 | ||
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 | ||
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; | |
2407 | } | |
2408 | ||
2409 | if (insn->offset + insn->len >= insn->func->offset + insn->func->len) | |
2410 | break; | |
2411 | ||
2412 | insn = list_next_entry(insn, list); | |
2413 | } | |
2414 | ||
2415 | return false; | |
2416 | } | |
2417 | ||
2418 | static int validate_section(struct objtool_file *file, struct section *sec) | |
2419 | { | |
2420 | struct symbol *func; | |
2421 | struct instruction *insn; | |
2422 | struct insn_state state; | |
2423 | int ret, warnings = 0; | |
2424 | ||
2425 | list_for_each_entry(func, &sec->symbol_list, list) { | |
2426 | if (func->type != STT_FUNC) | |
2427 | continue; | |
2428 | ||
2429 | if (!func->len) { | |
2430 | WARN("%s() is missing an ELF size annotation", | |
2431 | func->name); | |
2432 | warnings++; | |
2433 | } | |
2434 | ||
2435 | if (func->pfunc != func || func->alias != func) | |
2436 | continue; | |
2437 | ||
2438 | insn = find_insn(file, sec, func->offset); | |
2439 | if (!insn || insn->ignore || insn->visited) | |
2440 | continue; | |
2441 | ||
2442 | clear_insn_state(&state); | |
2443 | state.cfi.cfa = initial_func_cfi.cfa; | |
2444 | memcpy(&state.cfi.regs, &initial_func_cfi.regs, | |
2445 | CFI_NUM_REGS * sizeof(struct cfi_reg)); | |
2446 | state.cfi.stack_size = initial_func_cfi.cfa.offset; | |
2447 | ||
2448 | state.uaccess = func->uaccess_safe; | |
2449 | ||
2450 | ret = validate_branch(file, func, insn, state); | |
2451 | if (ret && backtrace) | |
2452 | BT_FUNC("<=== (func)", insn); | |
2453 | warnings += ret; | |
2454 | } | |
2455 | ||
2456 | return warnings; | |
2457 | } | |
2458 | ||
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 | ||
2470 | static int validate_reachable_instructions(struct objtool_file *file) | |
2471 | { | |
2472 | struct instruction *insn; | |
2473 | ||
2474 | if (file->ignore_unreachables) | |
2475 | return 0; | |
2476 | ||
2477 | for_each_insn(file, insn) { | |
2478 | if (insn->visited || ignore_unreachable_insn(insn)) | |
2479 | continue; | |
2480 | ||
2481 | WARN_FUNC("unreachable instruction", insn->sec, insn->offset); | |
2482 | return 1; | |
2483 | } | |
2484 | ||
2485 | return 0; | |
2486 | } | |
2487 | ||
2488 | static struct objtool_file file; | |
2489 | ||
2490 | int check(const char *_objname, bool orc) | |
2491 | { | |
2492 | int ret, warnings = 0; | |
2493 | ||
2494 | objname = _objname; | |
2495 | ||
2496 | file.elf = elf_read(objname, orc ? O_RDWR : O_RDONLY); | |
2497 | if (!file.elf) | |
2498 | return 1; | |
2499 | ||
2500 | INIT_LIST_HEAD(&file.insn_list); | |
2501 | hash_init(file.insn_hash); | |
2502 | file.c_file = find_section_by_name(file.elf, ".comment"); | |
2503 | file.ignore_unreachables = no_unreachable; | |
2504 | file.hints = false; | |
2505 | ||
2506 | arch_initial_func_cfi_state(&initial_func_cfi); | |
2507 | ||
2508 | ret = decode_sections(&file); | |
2509 | if (ret < 0) | |
2510 | goto out; | |
2511 | warnings += ret; | |
2512 | ||
2513 | if (list_empty(&file.insn_list)) | |
2514 | goto out; | |
2515 | ||
2516 | if (retpoline) { | |
2517 | ret = validate_retpoline(&file); | |
2518 | if (ret < 0) | |
2519 | return ret; | |
2520 | warnings += ret; | |
2521 | } | |
2522 | ||
2523 | ret = validate_functions(&file); | |
2524 | if (ret < 0) | |
2525 | goto out; | |
2526 | warnings += ret; | |
2527 | ||
2528 | ret = validate_unwind_hints(&file); | |
2529 | if (ret < 0) | |
2530 | goto out; | |
2531 | warnings += ret; | |
2532 | ||
2533 | if (!warnings) { | |
2534 | ret = validate_reachable_instructions(&file); | |
2535 | if (ret < 0) | |
2536 | goto out; | |
2537 | warnings += ret; | |
2538 | } | |
2539 | ||
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 | ||
2554 | out: | |
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 | ||
2564 | return 0; | |
2565 | } |