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