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