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