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