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