]> git.ipfire.org Git - thirdparty/linux.git/blame - tools/objtool/check.c
objtool: Ignore exc_double_fault() __noreturn warnings
[thirdparty/linux.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>
22682a07 8#include <inttypes.h>
8b946cc3 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
f7515d9f 20#include <linux/objtool_types.h>
dcc914f4
JP
21#include <linux/hashtable.h>
22#include <linux/kernel.h>
1e7e4788 23#include <linux/static_call_types.h>
dcc914f4 24
dcc914f4 25struct alternative {
d5406654 26 struct alternative *next;
dcc914f4 27 struct instruction *insn;
764eef4b 28 bool skip_orig;
dcc914f4
JP
29};
30
8b946cc3
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
1c34496e
PZ
50struct instruction *next_insn_same_sec(struct objtool_file *file,
51 struct instruction *insn)
dcc914f4 52{
1c34496e
PZ
53 if (insn->idx == INSN_CHUNK_MAX)
54 return find_insn(file, insn->sec, insn->offset + insn->len);
dcc914f4 55
1c34496e
PZ
56 insn++;
57 if (!insn->len)
dcc914f4
JP
58 return NULL;
59
1c34496e 60 return insn;
dcc914f4
JP
61}
62
13810435
JP
63static struct instruction *next_insn_same_func(struct objtool_file *file,
64 struct instruction *insn)
65{
1c34496e 66 struct instruction *next = next_insn_same_sec(file, insn);
dbcdbdfd 67 struct symbol *func = insn_func(insn);
13810435
JP
68
69 if (!func)
70 return NULL;
71
1c34496e 72 if (next && insn_func(next) == func)
13810435
JP
73 return next;
74
75 /* Check if we're already in the subfunction: */
76 if (func == func->cfunc)
77 return NULL;
78
79 /* Move to the subfunction: */
80 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
81}
82
1c34496e
PZ
83static struct instruction *prev_insn_same_sec(struct objtool_file *file,
84 struct instruction *insn)
85{
86 if (insn->idx == 0) {
87 if (insn->prev_len)
88 return find_insn(file, insn->sec, insn->offset - insn->prev_len);
89 return NULL;
90 }
91
92 return insn - 1;
93}
94
1119d265 95static struct instruction *prev_insn_same_sym(struct objtool_file *file,
1c34496e 96 struct instruction *insn)
1119d265 97{
1c34496e 98 struct instruction *prev = prev_insn_same_sec(file, insn);
1119d265 99
1c34496e 100 if (prev && insn_func(prev) == insn_func(insn))
1119d265
JP
101 return prev;
102
103 return NULL;
104}
105
1c34496e
PZ
106#define for_each_insn(file, insn) \
107 for (struct section *__sec, *__fake = (struct section *)1; \
108 __fake; __fake = NULL) \
109 for_each_sec(file, __sec) \
110 sec_for_each_insn(file, __sec, insn)
111
f0f70adb 112#define func_for_each_insn(file, func, insn) \
13810435
JP
113 for (insn = find_insn(file, func->sec, func->offset); \
114 insn; \
115 insn = next_insn_same_func(file, insn))
116
dbf4aeb0
PZ
117#define sym_for_each_insn(file, sym, insn) \
118 for (insn = find_insn(file, sym->sec, sym->offset); \
1c34496e
PZ
119 insn && insn->offset < sym->offset + sym->len; \
120 insn = next_insn_same_sec(file, insn))
dcc914f4 121
dbf4aeb0 122#define sym_for_each_insn_continue_reverse(file, sym, insn) \
1c34496e
PZ
123 for (insn = prev_insn_same_sec(file, insn); \
124 insn && insn->offset >= sym->offset; \
125 insn = prev_insn_same_sec(file, insn))
dcc914f4
JP
126
127#define sec_for_each_insn_from(file, insn) \
128 for (; insn; insn = next_insn_same_sec(file, insn))
129
baa41469
JP
130#define sec_for_each_insn_continue(file, insn) \
131 for (insn = next_insn_same_sec(file, insn); insn; \
132 insn = next_insn_same_sec(file, insn))
dcc914f4 133
c6f5dc28
PZ
134static inline struct symbol *insn_call_dest(struct instruction *insn)
135{
136 if (insn->type == INSN_JUMP_DYNAMIC ||
137 insn->type == INSN_CALL_DYNAMIC)
138 return NULL;
139
140 return insn->_call_dest;
141}
142
143static inline struct reloc *insn_jump_table(struct instruction *insn)
144{
145 if (insn->type == INSN_JUMP_DYNAMIC ||
146 insn->type == INSN_CALL_DYNAMIC)
147 return insn->_jump_table;
148
149 return NULL;
150}
151
99033461
JP
152static bool is_jump_table_jump(struct instruction *insn)
153{
154 struct alt_group *alt_group = insn->alt_group;
155
c6f5dc28 156 if (insn_jump_table(insn))
99033461
JP
157 return true;
158
159 /* Retpoline alternative for a jump table? */
160 return alt_group && alt_group->orig_group &&
c6f5dc28 161 insn_jump_table(alt_group->orig_group->first_insn);
99033461
JP
162}
163
0c1ddd33
JP
164static bool is_sibling_call(struct instruction *insn)
165{
ecf11ba4 166 /*
5a9c361a 167 * Assume only STT_FUNC calls have jump-tables.
ecf11ba4 168 */
5a9c361a
PZ
169 if (insn_func(insn)) {
170 /* An indirect jump is either a sibling call or a jump to a table. */
171 if (insn->type == INSN_JUMP_DYNAMIC)
172 return !is_jump_table_jump(insn);
173 }
0c1ddd33 174
c6f5dc28
PZ
175 /* add_jump_destinations() sets insn_call_dest(insn) for sibling calls. */
176 return (is_static_jump(insn) && insn_call_dest(insn));
0c1ddd33
JP
177}
178
dcc914f4
JP
179/*
180 * This checks to see if the given function is a "noreturn" function.
181 *
182 * For global functions which are outside the scope of this object file, we
183 * have to keep a manual list of them.
184 *
185 * For local functions, we have to detect them manually by simply looking for
186 * the lack of a return instruction.
dcc914f4 187 */
8e25c9f8
JP
188static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
189 int recursion)
dcc914f4
JP
190{
191 int i;
192 struct instruction *insn;
193 bool empty = true;
194
195 /*
196 * Unfortunately these have to be hard coded because the noreturn
c93c296f 197 * attribute isn't provided in ELF data. Keep 'em sorted.
dcc914f4
JP
198 */
199 static const char * const global_noreturns[] = {
c93c296f
BP
200 "__invalid_creds",
201 "__module_put_and_kthread_exit",
202 "__reiserfs_panic",
dcc914f4 203 "__stack_chk_fail",
c93c296f 204 "__ubsan_handle_builtin_unreachable",
9ea7e6b6 205 "arch_call_rest_init",
071c44e4 206 "arch_cpu_idle_dead",
f3724631 207 "btrfs_assertfail",
c93c296f
BP
208 "cpu_bringup_and_idle",
209 "cpu_startup_entry",
dcc914f4 210 "do_exit",
c93c296f 211 "do_group_exit",
dcc914f4 212 "do_task_dead",
c93c296f
BP
213 "ex_handler_msr_mce",
214 "fortify_panic",
52668bad 215 "hlt_play_dead",
611d4c71 216 "hv_ghcb_terminate",
cead1855 217 "kthread_complete_and_exit",
c93c296f
BP
218 "kthread_exit",
219 "kunit_try_catch_throw",
dcc914f4 220 "lbug_with_loc",
684fb246 221 "machine_real_restart",
c93c296f 222 "make_task_dead",
6e36a56a 223 "mpt_halt_firmware",
27dea14c 224 "nmi_panic_self_stop",
c93c296f 225 "panic",
7412a60d 226 "panic_smp_self_stop",
9ea7e6b6 227 "rest_init",
52668bad 228 "resume_play_dead",
1fb466df 229 "rewind_stack_and_make_dead",
c93c296f
BP
230 "sev_es_terminate",
231 "snp_abort",
25a6917c 232 "start_kernel",
f9cdf7ca 233 "stop_this_cpu",
c93c296f 234 "usercopy_abort",
4208d2d7
JP
235 "x86_64_start_kernel",
236 "x86_64_start_reservations",
f697cb00 237 "xen_cpu_bringup_again",
c93c296f 238 "xen_start_kernel",
dcc914f4
JP
239 };
240
c9bab22b
JP
241 if (!func)
242 return false;
243
1c47c875 244 if (func->bind == STB_GLOBAL || func->bind == STB_WEAK)
dcc914f4
JP
245 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
246 if (!strcmp(func->name, global_noreturns[i]))
8e25c9f8 247 return true;
dcc914f4 248
1c47c875
JP
249 if (func->bind == STB_WEAK)
250 return false;
251
13810435 252 if (!func->len)
8e25c9f8 253 return false;
dcc914f4 254
13810435 255 insn = find_insn(file, func->sec, func->offset);
5f6e430f 256 if (!insn || !insn_func(insn))
8e25c9f8 257 return false;
13810435 258
f0f70adb 259 func_for_each_insn(file, func, insn) {
dcc914f4
JP
260 empty = false;
261
262 if (insn->type == INSN_RETURN)
8e25c9f8 263 return false;
dcc914f4
JP
264 }
265
266 if (empty)
8e25c9f8 267 return false;
dcc914f4
JP
268
269 /*
270 * A function can have a sibling call instead of a return. In that
271 * case, the function's dead-end status depends on whether the target
272 * of the sibling call returns.
273 */
f0f70adb 274 func_for_each_insn(file, func, insn) {
0c1ddd33 275 if (is_sibling_call(insn)) {
dcc914f4 276 struct instruction *dest = insn->jump_dest;
dcc914f4
JP
277
278 if (!dest)
279 /* sibling call to another file */
8e25c9f8 280 return false;
dcc914f4 281
0c1ddd33
JP
282 /* local sibling call */
283 if (recursion == 5) {
284 /*
285 * Infinite recursion: two functions have
286 * sibling calls to each other. This is a very
287 * rare case. It means they aren't dead ends.
288 */
289 return false;
dcc914f4 290 }
dcc914f4 291
dbcdbdfd 292 return __dead_end_function(file, insn_func(dest), recursion+1);
0c1ddd33 293 }
dcc914f4
JP
294 }
295
8e25c9f8 296 return true;
dcc914f4
JP
297}
298
8e25c9f8 299static bool dead_end_function(struct objtool_file *file, struct symbol *func)
dcc914f4
JP
300{
301 return __dead_end_function(file, func, 0);
302}
303
e7c0219b 304static void init_cfi_state(struct cfi_state *cfi)
baa41469
JP
305{
306 int i;
307
dd88a0a0 308 for (i = 0; i < CFI_NUM_REGS; i++) {
e7c0219b
PZ
309 cfi->regs[i].base = CFI_UNDEFINED;
310 cfi->vals[i].base = CFI_UNDEFINED;
dd88a0a0 311 }
e7c0219b
PZ
312 cfi->cfa.base = CFI_UNDEFINED;
313 cfi->drap_reg = CFI_UNDEFINED;
314 cfi->drap_offset = -1;
315}
316
753da417
JP
317static void init_insn_state(struct objtool_file *file, struct insn_state *state,
318 struct section *sec)
e7c0219b
PZ
319{
320 memset(state, 0, sizeof(*state));
321 init_cfi_state(&state->cfi);
932f8e98
PZ
322
323 /*
324 * We need the full vmlinux for noinstr validation, otherwise we can
c6f5dc28
PZ
325 * not correctly determine insn_call_dest(insn)->sec (external symbols
326 * do not have a section).
932f8e98 327 */
753da417 328 if (opts.link && opts.noinstr && sec)
932f8e98 329 state->noinstr = sec->noinstr;
baa41469
JP
330}
331
8b946cc3
PZ
332static struct cfi_state *cfi_alloc(void)
333{
334 struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
335 if (!cfi) {
336 WARN("calloc failed");
337 exit(1);
338 }
339 nr_cfi++;
340 return cfi;
341}
342
343static int cfi_bits;
344static struct hlist_head *cfi_hash;
345
346static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
347{
348 return memcmp((void *)cfi1 + sizeof(cfi1->hash),
349 (void *)cfi2 + sizeof(cfi2->hash),
350 sizeof(struct cfi_state) - sizeof(struct hlist_node));
351}
352
353static inline u32 cfi_key(struct cfi_state *cfi)
354{
355 return jhash((void *)cfi + sizeof(cfi->hash),
356 sizeof(*cfi) - sizeof(cfi->hash), 0);
357}
358
359static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
360{
361 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
362 struct cfi_state *obj;
363
364 hlist_for_each_entry(obj, head, hash) {
365 if (!cficmp(cfi, obj)) {
366 nr_cfi_cache++;
367 return obj;
368 }
369 }
370
371 obj = cfi_alloc();
372 *obj = *cfi;
373 hlist_add_head(&obj->hash, head);
374
375 return obj;
376}
377
378static void cfi_hash_add(struct cfi_state *cfi)
379{
380 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
381
382 hlist_add_head(&cfi->hash, head);
383}
384
385static void *cfi_hash_alloc(unsigned long size)
386{
387 cfi_bits = max(10, ilog2(size));
388 cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits,
389 PROT_READ|PROT_WRITE,
390 MAP_PRIVATE|MAP_ANON, -1, 0);
391 if (cfi_hash == (void *)-1L) {
392 WARN("mmap fail cfi_hash");
393 cfi_hash = NULL;
2daf7fab 394 } else if (opts.stats) {
8b946cc3
PZ
395 printf("cfi_bits: %d\n", cfi_bits);
396 }
397
398 return cfi_hash;
399}
400
401static unsigned long nr_insns;
402static unsigned long nr_insns_visited;
403
dcc914f4
JP
404/*
405 * Call the arch-specific instruction decoder for all the instructions and add
406 * them to the global instruction list.
407 */
408static int decode_instructions(struct objtool_file *file)
409{
410 struct section *sec;
411 struct symbol *func;
412 unsigned long offset;
413 struct instruction *insn;
414 int ret;
415
baa41469 416 for_each_sec(file, sec) {
1c34496e
PZ
417 struct instruction *insns = NULL;
418 u8 prev_len = 0;
419 u8 idx = 0;
dcc914f4
JP
420
421 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
422 continue;
423
627fce14
JP
424 if (strcmp(sec->name, ".altinstr_replacement") &&
425 strcmp(sec->name, ".altinstr_aux") &&
426 strncmp(sec->name, ".discard.", 9))
427 sec->text = true;
428
0cc9ac8d 429 if (!strcmp(sec->name, ".noinstr.text") ||
951ddecf 430 !strcmp(sec->name, ".entry.text") ||
2b5a0e42 431 !strcmp(sec->name, ".cpuidle.text") ||
951ddecf 432 !strncmp(sec->name, ".text.__x86.", 12))
c4a33939
PZ
433 sec->noinstr = true;
434
6644ee84
PZ
435 /*
436 * .init.text code is ran before userspace and thus doesn't
437 * strictly need retpolines, except for modules which are
438 * loaded late, they very much do need retpoline in their
439 * .init.text
440 */
441 if (!strcmp(sec->name, ".init.text") && !opts.module)
442 sec->init = true;
443
fe255fe6 444 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
1c34496e
PZ
445 if (!insns || idx == INSN_CHUNK_MAX) {
446 insns = calloc(sizeof(*insn), INSN_CHUNK_SIZE);
447 if (!insns) {
448 WARN("malloc failed");
449 return -1;
450 }
451 idx = 0;
452 } else {
453 idx++;
baa41469 454 }
1c34496e
PZ
455 insn = &insns[idx];
456 insn->idx = idx;
baa41469 457
1c34496e 458 INIT_LIST_HEAD(&insn->call_node);
dcc914f4
JP
459 insn->sec = sec;
460 insn->offset = offset;
1c34496e 461 insn->prev_len = prev_len;
dcc914f4 462
db2b0c5d 463 ret = arch_decode_instruction(file, sec, offset,
fe255fe6 464 sec->sh.sh_size - offset,
20a55463 465 insn);
dcc914f4 466 if (ret)
1c34496e
PZ
467 return ret;
468
469 prev_len = insn->len;
dcc914f4 470
0e5b613b
PZ
471 /*
472 * By default, "ud2" is a dead end unless otherwise
473 * annotated, because GCC 7 inserts it for certain
474 * divide-by-zero cases.
475 */
476 if (insn->type == INSN_BUG)
477 insn->dead_end = true;
478
87ecb582 479 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
1e11f3fd 480 nr_insns++;
dcc914f4
JP
481 }
482
1c34496e
PZ
483// printf("%s: last chunk used: %d\n", sec->name, (int)idx);
484
9290e772 485 sec_for_each_sym(sec, func) {
dbcdbdfd
PZ
486 if (func->type != STT_NOTYPE && func->type != STT_FUNC)
487 continue;
488
cad90e53
NP
489 if (func->offset == sec->sh.sh_size) {
490 /* Heuristic: likely an "end" symbol */
491 if (func->type == STT_NOTYPE)
492 continue;
493 WARN("%s(): STT_FUNC at end of section",
494 func->name);
495 return -1;
496 }
497
dbcdbdfd 498 if (func->return_thunk || func->alias != func)
dcc914f4
JP
499 continue;
500
501 if (!find_insn(file, sec, func->offset)) {
502 WARN("%s(): can't find starting instruction",
503 func->name);
504 return -1;
505 }
506
08f87a93 507 sym_for_each_insn(file, func, insn) {
dbcdbdfd
PZ
508 insn->sym = func;
509 if (func->type == STT_FUNC &&
510 insn->type == INSN_ENDBR &&
511 list_empty(&insn->call_node)) {
512 if (insn->offset == func->offset) {
89bc853e 513 list_add_tail(&insn->call_node, &file->endbr_list);
08f87a93
PZ
514 file->nr_endbr++;
515 } else {
516 file->nr_endbr_int++;
517 }
518 }
519 }
dcc914f4
JP
520 }
521 }
522
2daf7fab 523 if (opts.stats)
1e11f3fd
PZ
524 printf("nr_insns: %lu\n", nr_insns);
525
dcc914f4
JP
526 return 0;
527}
528
db2b0c5d
PZ
529/*
530 * Read the pv_ops[] .data table to find the static initialized values.
531 */
532static int add_pv_ops(struct objtool_file *file, const char *symname)
533{
534 struct symbol *sym, *func;
535 unsigned long off, end;
536 struct reloc *rel;
537 int idx;
538
539 sym = find_symbol_by_name(file->elf, symname);
540 if (!sym)
541 return 0;
542
543 off = sym->offset;
544 end = off + sym->len;
545 for (;;) {
546 rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off);
547 if (!rel)
548 break;
549
550 func = rel->sym;
551 if (func->type == STT_SECTION)
552 func = find_symbol_by_offset(rel->sym->sec, rel->addend);
553
554 idx = (rel->offset - sym->offset) / sizeof(unsigned long);
555
556 objtool_pv_add(file, idx, func);
557
558 off = rel->offset + 1;
559 if (off > end)
560 break;
561 }
562
563 return 0;
564}
565
566/*
567 * Allocate and initialize file->pv_ops[].
568 */
569static int init_pv_ops(struct objtool_file *file)
570{
571 static const char *pv_ops_tables[] = {
572 "pv_ops",
573 "xen_cpu_ops",
574 "xen_irq_ops",
575 "xen_mmu_ops",
576 NULL,
577 };
578 const char *pv_ops;
579 struct symbol *sym;
580 int idx, nr;
581
2daf7fab 582 if (!opts.noinstr)
db2b0c5d
PZ
583 return 0;
584
585 file->pv_ops = NULL;
586
587 sym = find_symbol_by_name(file->elf, "pv_ops");
588 if (!sym)
589 return 0;
590
591 nr = sym->len / sizeof(unsigned long);
592 file->pv_ops = calloc(sizeof(struct pv_state), nr);
593 if (!file->pv_ops)
594 return -1;
595
596 for (idx = 0; idx < nr; idx++)
597 INIT_LIST_HEAD(&file->pv_ops[idx].targets);
598
599 for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++)
600 add_pv_ops(file, pv_ops);
601
602 return 0;
603}
604
6b5dd716
ST
605static struct instruction *find_last_insn(struct objtool_file *file,
606 struct section *sec)
607{
608 struct instruction *insn = NULL;
609 unsigned int offset;
fe255fe6 610 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
6b5dd716 611
fe255fe6 612 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
6b5dd716
ST
613 insn = find_insn(file, sec, offset);
614
615 return insn;
616}
617
dcc914f4 618/*
649ea4d5 619 * Mark "ud2" instructions and manually annotated dead ends.
dcc914f4
JP
620 */
621static int add_dead_ends(struct objtool_file *file)
622{
623 struct section *sec;
f1974222 624 struct reloc *reloc;
dcc914f4 625 struct instruction *insn;
dcc914f4 626
649ea4d5
JP
627 /*
628 * Check for manually annotated dead ends.
629 */
dcc914f4
JP
630 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
631 if (!sec)
649ea4d5 632 goto reachable;
dcc914f4 633
f1974222
MH
634 list_for_each_entry(reloc, &sec->reloc_list, list) {
635 if (reloc->sym->type != STT_SECTION) {
dcc914f4
JP
636 WARN("unexpected relocation symbol type in %s", sec->name);
637 return -1;
638 }
f1974222 639 insn = find_insn(file, reloc->sym->sec, reloc->addend);
dcc914f4 640 if (insn)
1c34496e 641 insn = prev_insn_same_sec(file, insn);
fe255fe6 642 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
f1974222 643 insn = find_last_insn(file, reloc->sym->sec);
6b5dd716 644 if (!insn) {
22682a07 645 WARN("can't find unreachable insn at %s+0x%" PRIx64,
f1974222 646 reloc->sym->sec->name, reloc->addend);
dcc914f4
JP
647 return -1;
648 }
649 } else {
22682a07 650 WARN("can't find unreachable insn at %s+0x%" PRIx64,
f1974222 651 reloc->sym->sec->name, reloc->addend);
dcc914f4
JP
652 return -1;
653 }
654
655 insn->dead_end = true;
656 }
657
649ea4d5
JP
658reachable:
659 /*
660 * These manually annotated reachable checks are needed for GCC 4.4,
661 * where the Linux unreachable() macro isn't supported. In that case
662 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
663 * not a dead end.
664 */
665 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
666 if (!sec)
667 return 0;
668
f1974222
MH
669 list_for_each_entry(reloc, &sec->reloc_list, list) {
670 if (reloc->sym->type != STT_SECTION) {
649ea4d5
JP
671 WARN("unexpected relocation symbol type in %s", sec->name);
672 return -1;
673 }
f1974222 674 insn = find_insn(file, reloc->sym->sec, reloc->addend);
649ea4d5 675 if (insn)
1c34496e 676 insn = prev_insn_same_sec(file, insn);
fe255fe6 677 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
f1974222 678 insn = find_last_insn(file, reloc->sym->sec);
6b5dd716 679 if (!insn) {
22682a07 680 WARN("can't find reachable insn at %s+0x%" PRIx64,
f1974222 681 reloc->sym->sec->name, reloc->addend);
649ea4d5
JP
682 return -1;
683 }
684 } else {
22682a07 685 WARN("can't find reachable insn at %s+0x%" PRIx64,
f1974222 686 reloc->sym->sec->name, reloc->addend);
649ea4d5
JP
687 return -1;
688 }
689
690 insn->dead_end = false;
691 }
692
dcc914f4
JP
693 return 0;
694}
695
1e7e4788
JP
696static int create_static_call_sections(struct objtool_file *file)
697{
ef47cc01 698 struct section *sec;
1e7e4788
JP
699 struct static_call_site *site;
700 struct instruction *insn;
701 struct symbol *key_sym;
702 char *key_name, *tmp;
703 int idx;
704
705 sec = find_section_by_name(file->elf, ".static_call_sites");
706 if (sec) {
707 INIT_LIST_HEAD(&file->static_call_list);
708 WARN("file already has .static_call_sites section, skipping");
709 return 0;
710 }
711
712 if (list_empty(&file->static_call_list))
713 return 0;
714
715 idx = 0;
43d5430a 716 list_for_each_entry(insn, &file->static_call_list, call_node)
1e7e4788
JP
717 idx++;
718
719 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
720 sizeof(struct static_call_site), idx);
721 if (!sec)
722 return -1;
723
1e7e4788 724 idx = 0;
43d5430a 725 list_for_each_entry(insn, &file->static_call_list, call_node) {
1e7e4788
JP
726
727 site = (struct static_call_site *)sec->data->d_buf + idx;
728 memset(site, 0, sizeof(struct static_call_site));
729
730 /* populate reloc for 'addr' */
ef47cc01
PZ
731 if (elf_add_reloc_to_insn(file->elf, sec,
732 idx * sizeof(struct static_call_site),
733 R_X86_64_PC32,
734 insn->sec, insn->offset))
44f6a7c0 735 return -1;
1e7e4788
JP
736
737 /* find key symbol */
c6f5dc28 738 key_name = strdup(insn_call_dest(insn)->name);
1e7e4788
JP
739 if (!key_name) {
740 perror("strdup");
741 return -1;
742 }
743 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
744 STATIC_CALL_TRAMP_PREFIX_LEN)) {
745 WARN("static_call: trampoline name malformed: %s", key_name);
3da73f10 746 free(key_name);
1e7e4788
JP
747 return -1;
748 }
749 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
750 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
751
752 key_sym = find_symbol_by_name(file->elf, tmp);
753 if (!key_sym) {
2daf7fab 754 if (!opts.module) {
73f44fe1 755 WARN("static_call: can't find static_call_key symbol: %s", tmp);
3da73f10 756 free(key_name);
73f44fe1
JP
757 return -1;
758 }
759
760 /*
761 * For modules(), the key might not be exported, which
762 * means the module can make static calls but isn't
763 * allowed to change them.
764 *
765 * In that case we temporarily set the key to be the
766 * trampoline address. This is fixed up in
767 * static_call_add_module().
768 */
c6f5dc28 769 key_sym = insn_call_dest(insn);
1e7e4788
JP
770 }
771 free(key_name);
772
773 /* populate reloc for 'key' */
ef47cc01
PZ
774 if (elf_add_reloc(file->elf, sec,
775 idx * sizeof(struct static_call_site) + 4,
776 R_X86_64_PC32, key_sym,
777 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
1e7e4788 778 return -1;
1e7e4788
JP
779
780 idx++;
781 }
782
1e7e4788
JP
783 return 0;
784}
785
134ab5bd
PZ
786static int create_retpoline_sites_sections(struct objtool_file *file)
787{
788 struct instruction *insn;
789 struct section *sec;
790 int idx;
791
792 sec = find_section_by_name(file->elf, ".retpoline_sites");
793 if (sec) {
794 WARN("file already has .retpoline_sites, skipping");
795 return 0;
796 }
797
798 idx = 0;
799 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
800 idx++;
801
802 if (!idx)
803 return 0;
804
805 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
806 sizeof(int), idx);
807 if (!sec) {
808 WARN("elf_create_section: .retpoline_sites");
809 return -1;
810 }
811
812 idx = 0;
813 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
814
815 int *site = (int *)sec->data->d_buf + idx;
816 *site = 0;
817
818 if (elf_add_reloc_to_insn(file->elf, sec,
819 idx * sizeof(int),
820 R_X86_64_PC32,
821 insn->sec, insn->offset)) {
822 WARN("elf_add_reloc_to_insn: .retpoline_sites");
823 return -1;
824 }
825
826 idx++;
827 }
828
829 return 0;
830}
831
d9e9d230
PZ
832static int create_return_sites_sections(struct objtool_file *file)
833{
834 struct instruction *insn;
835 struct section *sec;
836 int idx;
837
838 sec = find_section_by_name(file->elf, ".return_sites");
839 if (sec) {
840 WARN("file already has .return_sites, skipping");
841 return 0;
842 }
843
844 idx = 0;
845 list_for_each_entry(insn, &file->return_thunk_list, call_node)
846 idx++;
847
848 if (!idx)
849 return 0;
850
851 sec = elf_create_section(file->elf, ".return_sites", 0,
852 sizeof(int), idx);
853 if (!sec) {
854 WARN("elf_create_section: .return_sites");
855 return -1;
856 }
857
858 idx = 0;
859 list_for_each_entry(insn, &file->return_thunk_list, call_node) {
860
861 int *site = (int *)sec->data->d_buf + idx;
862 *site = 0;
863
864 if (elf_add_reloc_to_insn(file->elf, sec,
865 idx * sizeof(int),
866 R_X86_64_PC32,
867 insn->sec, insn->offset)) {
868 WARN("elf_add_reloc_to_insn: .return_sites");
869 return -1;
870 }
871
872 idx++;
873 }
874
875 return 0;
876}
877
89bc853e
PZ
878static int create_ibt_endbr_seal_sections(struct objtool_file *file)
879{
880 struct instruction *insn;
881 struct section *sec;
882 int idx;
883
884 sec = find_section_by_name(file->elf, ".ibt_endbr_seal");
885 if (sec) {
886 WARN("file already has .ibt_endbr_seal, skipping");
887 return 0;
888 }
889
890 idx = 0;
891 list_for_each_entry(insn, &file->endbr_list, call_node)
892 idx++;
893
2daf7fab 894 if (opts.stats) {
89bc853e
PZ
895 printf("ibt: ENDBR at function start: %d\n", file->nr_endbr);
896 printf("ibt: ENDBR inside functions: %d\n", file->nr_endbr_int);
897 printf("ibt: superfluous ENDBR: %d\n", idx);
898 }
899
900 if (!idx)
901 return 0;
902
903 sec = elf_create_section(file->elf, ".ibt_endbr_seal", 0,
904 sizeof(int), idx);
905 if (!sec) {
906 WARN("elf_create_section: .ibt_endbr_seal");
907 return -1;
908 }
909
910 idx = 0;
911 list_for_each_entry(insn, &file->endbr_list, call_node) {
912
913 int *site = (int *)sec->data->d_buf + idx;
03d7a105 914 struct symbol *sym = insn->sym;
89bc853e
PZ
915 *site = 0;
916
03d7a105
MK
917 if (opts.module && sym && sym->type == STT_FUNC &&
918 insn->offset == sym->offset &&
919 (!strcmp(sym->name, "init_module") ||
920 !strcmp(sym->name, "cleanup_module")))
921 WARN("%s(): not an indirect call target", sym->name);
922
89bc853e
PZ
923 if (elf_add_reloc_to_insn(file->elf, sec,
924 idx * sizeof(int),
925 R_X86_64_PC32,
926 insn->sec, insn->offset)) {
927 WARN("elf_add_reloc_to_insn: .ibt_endbr_seal");
928 return -1;
929 }
930
931 idx++;
932 }
933
934 return 0;
935}
936
9a479f76
PZ
937static int create_cfi_sections(struct objtool_file *file)
938{
9290e772 939 struct section *sec;
9a479f76
PZ
940 struct symbol *sym;
941 unsigned int *loc;
942 int idx;
943
944 sec = find_section_by_name(file->elf, ".cfi_sites");
945 if (sec) {
946 INIT_LIST_HEAD(&file->call_list);
947 WARN("file already has .cfi_sites section, skipping");
948 return 0;
949 }
950
951 idx = 0;
9290e772
JP
952 for_each_sym(file, sym) {
953 if (sym->type != STT_FUNC)
9a479f76
PZ
954 continue;
955
9290e772
JP
956 if (strncmp(sym->name, "__cfi_", 6))
957 continue;
9a479f76 958
9290e772 959 idx++;
9a479f76
PZ
960 }
961
962 sec = elf_create_section(file->elf, ".cfi_sites", 0, sizeof(unsigned int), idx);
963 if (!sec)
964 return -1;
965
966 idx = 0;
9290e772
JP
967 for_each_sym(file, sym) {
968 if (sym->type != STT_FUNC)
9a479f76
PZ
969 continue;
970
9290e772
JP
971 if (strncmp(sym->name, "__cfi_", 6))
972 continue;
9a479f76 973
9290e772
JP
974 loc = (unsigned int *)sec->data->d_buf + idx;
975 memset(loc, 0, sizeof(unsigned int));
9a479f76 976
9290e772
JP
977 if (elf_add_reloc_to_insn(file->elf, sec,
978 idx * sizeof(unsigned int),
979 R_X86_64_PC32,
980 sym->sec, sym->offset))
981 return -1;
9a479f76 982
9290e772 983 idx++;
9a479f76
PZ
984 }
985
986 return 0;
987}
988
99d00215
PZ
989static int create_mcount_loc_sections(struct objtool_file *file)
990{
86ea7f36 991 int addrsize = elf_class_addrsize(file->elf);
99d00215 992 struct instruction *insn;
86ea7f36 993 struct section *sec;
99d00215
PZ
994 int idx;
995
996 sec = find_section_by_name(file->elf, "__mcount_loc");
997 if (sec) {
998 INIT_LIST_HEAD(&file->mcount_loc_list);
999 WARN("file already has __mcount_loc section, skipping");
1000 return 0;
1001 }
1002
1003 if (list_empty(&file->mcount_loc_list))
1004 return 0;
1005
1006 idx = 0;
c509331b 1007 list_for_each_entry(insn, &file->mcount_loc_list, call_node)
99d00215
PZ
1008 idx++;
1009
86ea7f36 1010 sec = elf_create_section(file->elf, "__mcount_loc", 0, addrsize, idx);
99d00215
PZ
1011 if (!sec)
1012 return -1;
1013
86ea7f36
CL
1014 sec->sh.sh_addralign = addrsize;
1015
99d00215 1016 idx = 0;
c509331b 1017 list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
86ea7f36 1018 void *loc;
99d00215 1019
86ea7f36
CL
1020 loc = sec->data->d_buf + idx;
1021 memset(loc, 0, addrsize);
99d00215 1022
86ea7f36 1023 if (elf_add_reloc_to_insn(file->elf, sec, idx,
c1449735 1024 addrsize == sizeof(u64) ? R_ABS64 : R_ABS32,
ef47cc01 1025 insn->sec, insn->offset))
99d00215 1026 return -1;
99d00215 1027
86ea7f36 1028 idx += addrsize;
99d00215
PZ
1029 }
1030
99d00215
PZ
1031 return 0;
1032}
1033
00abd384
PZ
1034static int create_direct_call_sections(struct objtool_file *file)
1035{
1036 struct instruction *insn;
1037 struct section *sec;
1038 unsigned int *loc;
1039 int idx;
1040
1041 sec = find_section_by_name(file->elf, ".call_sites");
1042 if (sec) {
1043 INIT_LIST_HEAD(&file->call_list);
1044 WARN("file already has .call_sites section, skipping");
1045 return 0;
1046 }
1047
1048 if (list_empty(&file->call_list))
1049 return 0;
1050
1051 idx = 0;
1052 list_for_each_entry(insn, &file->call_list, call_node)
1053 idx++;
1054
1055 sec = elf_create_section(file->elf, ".call_sites", 0, sizeof(unsigned int), idx);
1056 if (!sec)
1057 return -1;
1058
1059 idx = 0;
1060 list_for_each_entry(insn, &file->call_list, call_node) {
1061
1062 loc = (unsigned int *)sec->data->d_buf + idx;
1063 memset(loc, 0, sizeof(unsigned int));
1064
1065 if (elf_add_reloc_to_insn(file->elf, sec,
1066 idx * sizeof(unsigned int),
1067 R_X86_64_PC32,
1068 insn->sec, insn->offset))
1069 return -1;
1070
1071 idx++;
1072 }
1073
1074 return 0;
1075}
1076
dcc914f4
JP
1077/*
1078 * Warnings shouldn't be reported for ignored functions.
1079 */
1080static void add_ignores(struct objtool_file *file)
1081{
1082 struct instruction *insn;
1083 struct section *sec;
1084 struct symbol *func;
f1974222 1085 struct reloc *reloc;
dcc914f4 1086
aaf5c623
PZ
1087 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
1088 if (!sec)
1089 return;
dcc914f4 1090
f1974222
MH
1091 list_for_each_entry(reloc, &sec->reloc_list, list) {
1092 switch (reloc->sym->type) {
aaf5c623 1093 case STT_FUNC:
f1974222 1094 func = reloc->sym;
aaf5c623
PZ
1095 break;
1096
1097 case STT_SECTION:
f1974222 1098 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
7acfe531 1099 if (!func)
dcc914f4 1100 continue;
aaf5c623 1101 break;
dcc914f4 1102
aaf5c623 1103 default:
f1974222 1104 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
aaf5c623 1105 continue;
dcc914f4 1106 }
aaf5c623 1107
f0f70adb 1108 func_for_each_insn(file, func, insn)
aaf5c623 1109 insn->ignore = true;
dcc914f4
JP
1110 }
1111}
1112
ea24213d
PZ
1113/*
1114 * This is a whitelist of functions that is allowed to be called with AC set.
1115 * The list is meant to be minimal and only contains compiler instrumentation
1116 * ABI and a few functions used to implement *_{to,from}_user() functions.
1117 *
1118 * These functions must not directly change AC, but may PUSHF/POPF.
1119 */
1120static const char *uaccess_safe_builtin[] = {
1121 /* KASAN */
1122 "kasan_report",
f00748bf 1123 "kasan_check_range",
ea24213d
PZ
1124 /* KASAN out-of-line */
1125 "__asan_loadN_noabort",
1126 "__asan_load1_noabort",
1127 "__asan_load2_noabort",
1128 "__asan_load4_noabort",
1129 "__asan_load8_noabort",
1130 "__asan_load16_noabort",
1131 "__asan_storeN_noabort",
1132 "__asan_store1_noabort",
1133 "__asan_store2_noabort",
1134 "__asan_store4_noabort",
1135 "__asan_store8_noabort",
1136 "__asan_store16_noabort",
b0b8e56b
JH
1137 "__kasan_check_read",
1138 "__kasan_check_write",
ea24213d
PZ
1139 /* KASAN in-line */
1140 "__asan_report_load_n_noabort",
1141 "__asan_report_load1_noabort",
1142 "__asan_report_load2_noabort",
1143 "__asan_report_load4_noabort",
1144 "__asan_report_load8_noabort",
1145 "__asan_report_load16_noabort",
1146 "__asan_report_store_n_noabort",
1147 "__asan_report_store1_noabort",
1148 "__asan_report_store2_noabort",
1149 "__asan_report_store4_noabort",
1150 "__asan_report_store8_noabort",
1151 "__asan_report_store16_noabort",
5f5c9712 1152 /* KCSAN */
9967683c 1153 "__kcsan_check_access",
0525bd82
ME
1154 "__kcsan_mb",
1155 "__kcsan_wmb",
1156 "__kcsan_rmb",
1157 "__kcsan_release",
5f5c9712
ME
1158 "kcsan_found_watchpoint",
1159 "kcsan_setup_watchpoint",
9967683c 1160 "kcsan_check_scoped_accesses",
50a19ad4
ME
1161 "kcsan_disable_current",
1162 "kcsan_enable_current_nowarn",
5f5c9712
ME
1163 /* KCSAN/TSAN */
1164 "__tsan_func_entry",
1165 "__tsan_func_exit",
1166 "__tsan_read_range",
1167 "__tsan_write_range",
1168 "__tsan_read1",
1169 "__tsan_read2",
1170 "__tsan_read4",
1171 "__tsan_read8",
1172 "__tsan_read16",
1173 "__tsan_write1",
1174 "__tsan_write2",
1175 "__tsan_write4",
1176 "__tsan_write8",
1177 "__tsan_write16",
a81b3759
ME
1178 "__tsan_read_write1",
1179 "__tsan_read_write2",
1180 "__tsan_read_write4",
1181 "__tsan_read_write8",
1182 "__tsan_read_write16",
63646fcb
ME
1183 "__tsan_volatile_read1",
1184 "__tsan_volatile_read2",
1185 "__tsan_volatile_read4",
1186 "__tsan_volatile_read8",
1187 "__tsan_volatile_read16",
1188 "__tsan_volatile_write1",
1189 "__tsan_volatile_write2",
1190 "__tsan_volatile_write4",
1191 "__tsan_volatile_write8",
1192 "__tsan_volatile_write16",
883957b1
ME
1193 "__tsan_atomic8_load",
1194 "__tsan_atomic16_load",
1195 "__tsan_atomic32_load",
1196 "__tsan_atomic64_load",
1197 "__tsan_atomic8_store",
1198 "__tsan_atomic16_store",
1199 "__tsan_atomic32_store",
1200 "__tsan_atomic64_store",
1201 "__tsan_atomic8_exchange",
1202 "__tsan_atomic16_exchange",
1203 "__tsan_atomic32_exchange",
1204 "__tsan_atomic64_exchange",
1205 "__tsan_atomic8_fetch_add",
1206 "__tsan_atomic16_fetch_add",
1207 "__tsan_atomic32_fetch_add",
1208 "__tsan_atomic64_fetch_add",
1209 "__tsan_atomic8_fetch_sub",
1210 "__tsan_atomic16_fetch_sub",
1211 "__tsan_atomic32_fetch_sub",
1212 "__tsan_atomic64_fetch_sub",
1213 "__tsan_atomic8_fetch_and",
1214 "__tsan_atomic16_fetch_and",
1215 "__tsan_atomic32_fetch_and",
1216 "__tsan_atomic64_fetch_and",
1217 "__tsan_atomic8_fetch_or",
1218 "__tsan_atomic16_fetch_or",
1219 "__tsan_atomic32_fetch_or",
1220 "__tsan_atomic64_fetch_or",
1221 "__tsan_atomic8_fetch_xor",
1222 "__tsan_atomic16_fetch_xor",
1223 "__tsan_atomic32_fetch_xor",
1224 "__tsan_atomic64_fetch_xor",
1225 "__tsan_atomic8_fetch_nand",
1226 "__tsan_atomic16_fetch_nand",
1227 "__tsan_atomic32_fetch_nand",
1228 "__tsan_atomic64_fetch_nand",
1229 "__tsan_atomic8_compare_exchange_strong",
1230 "__tsan_atomic16_compare_exchange_strong",
1231 "__tsan_atomic32_compare_exchange_strong",
1232 "__tsan_atomic64_compare_exchange_strong",
1233 "__tsan_atomic8_compare_exchange_weak",
1234 "__tsan_atomic16_compare_exchange_weak",
1235 "__tsan_atomic32_compare_exchange_weak",
1236 "__tsan_atomic64_compare_exchange_weak",
1237 "__tsan_atomic8_compare_exchange_val",
1238 "__tsan_atomic16_compare_exchange_val",
1239 "__tsan_atomic32_compare_exchange_val",
1240 "__tsan_atomic64_compare_exchange_val",
1241 "__tsan_atomic_thread_fence",
1242 "__tsan_atomic_signal_fence",
d5d46924
AB
1243 "__tsan_unaligned_read16",
1244 "__tsan_unaligned_write16",
ea24213d
PZ
1245 /* KCOV */
1246 "write_comp_data",
ae033f08 1247 "check_kcov_mode",
ea24213d
PZ
1248 "__sanitizer_cov_trace_pc",
1249 "__sanitizer_cov_trace_const_cmp1",
1250 "__sanitizer_cov_trace_const_cmp2",
1251 "__sanitizer_cov_trace_const_cmp4",
1252 "__sanitizer_cov_trace_const_cmp8",
1253 "__sanitizer_cov_trace_cmp1",
1254 "__sanitizer_cov_trace_cmp2",
1255 "__sanitizer_cov_trace_cmp4",
1256 "__sanitizer_cov_trace_cmp8",
36b1c700 1257 "__sanitizer_cov_trace_switch",
40b22c9d
AP
1258 /* KMSAN */
1259 "kmsan_copy_to_user",
1260 "kmsan_report",
1261 "kmsan_unpoison_entry_regs",
1262 "kmsan_unpoison_memory",
1263 "__msan_chain_origin",
1264 "__msan_get_context_state",
1265 "__msan_instrument_asm_store",
1266 "__msan_metadata_ptr_for_load_1",
1267 "__msan_metadata_ptr_for_load_2",
1268 "__msan_metadata_ptr_for_load_4",
1269 "__msan_metadata_ptr_for_load_8",
1270 "__msan_metadata_ptr_for_load_n",
1271 "__msan_metadata_ptr_for_store_1",
1272 "__msan_metadata_ptr_for_store_2",
1273 "__msan_metadata_ptr_for_store_4",
1274 "__msan_metadata_ptr_for_store_8",
1275 "__msan_metadata_ptr_for_store_n",
1276 "__msan_poison_alloca",
1277 "__msan_warning",
ea24213d
PZ
1278 /* UBSAN */
1279 "ubsan_type_mismatch_common",
1280 "__ubsan_handle_type_mismatch",
1281 "__ubsan_handle_type_mismatch_v1",
9a50dcaf 1282 "__ubsan_handle_shift_out_of_bounds",
f18b0d7e 1283 "__ubsan_handle_load_invalid_value",
7f530fba
JP
1284 /* STACKLEAK */
1285 "stackleak_track_stack",
ea24213d
PZ
1286 /* misc */
1287 "csum_partial_copy_generic",
ec6347bb
DW
1288 "copy_mc_fragile",
1289 "copy_mc_fragile_handle_tail",
5da8e4a6 1290 "copy_mc_enhanced_fast_string",
ea24213d 1291 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
8c9b6a88 1292 "rep_stos_alternative",
427fda2c 1293 "rep_movs_alternative",
3639a535 1294 "__copy_user_nocache",
ea24213d
PZ
1295 NULL
1296};
1297
1298static void add_uaccess_safe(struct objtool_file *file)
1299{
1300 struct symbol *func;
1301 const char **name;
1302
2daf7fab 1303 if (!opts.uaccess)
ea24213d
PZ
1304 return;
1305
1306 for (name = uaccess_safe_builtin; *name; name++) {
1307 func = find_symbol_by_name(file->elf, *name);
1308 if (!func)
1309 continue;
1310
e10cd8fe 1311 func->uaccess_safe = true;
dcc914f4
JP
1312 }
1313}
1314
258c7605
JP
1315/*
1316 * FIXME: For now, just ignore any alternatives which add retpolines. This is
1317 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
1318 * But it at least allows objtool to understand the control flow *around* the
1319 * retpoline.
1320 */
ff05ab23 1321static int add_ignore_alternatives(struct objtool_file *file)
258c7605
JP
1322{
1323 struct section *sec;
f1974222 1324 struct reloc *reloc;
258c7605
JP
1325 struct instruction *insn;
1326
ff05ab23 1327 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
258c7605
JP
1328 if (!sec)
1329 return 0;
1330
f1974222
MH
1331 list_for_each_entry(reloc, &sec->reloc_list, list) {
1332 if (reloc->sym->type != STT_SECTION) {
258c7605
JP
1333 WARN("unexpected relocation symbol type in %s", sec->name);
1334 return -1;
1335 }
1336
f1974222 1337 insn = find_insn(file, reloc->sym->sec, reloc->addend);
258c7605 1338 if (!insn) {
ff05ab23 1339 WARN("bad .discard.ignore_alts entry");
258c7605
JP
1340 return -1;
1341 }
1342
1343 insn->ignore_alts = true;
1344 }
1345
1346 return 0;
1347}
1348
530b4ddd
PZ
1349__weak bool arch_is_retpoline(struct symbol *sym)
1350{
1351 return false;
1352}
1353
d9e9d230
PZ
1354__weak bool arch_is_rethunk(struct symbol *sym)
1355{
1356 return false;
1357}
1358
7bd2a600
PZ
1359static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
1360{
0932dbe1
PZ
1361 struct reloc *reloc;
1362
1363 if (insn->no_reloc)
7bd2a600
PZ
1364 return NULL;
1365
0932dbe1
PZ
1366 if (!file)
1367 return NULL;
db2b0c5d 1368
0932dbe1
PZ
1369 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1370 insn->offset, insn->len);
1371 if (!reloc) {
1372 insn->no_reloc = 1;
1373 return NULL;
7bd2a600
PZ
1374 }
1375
0932dbe1 1376 return reloc;
7bd2a600
PZ
1377}
1378
f56dae88
PZ
1379static void remove_insn_ops(struct instruction *insn)
1380{
3ee88df1 1381 struct stack_op *op, *next;
f56dae88 1382
3ee88df1
PZ
1383 for (op = insn->stack_ops; op; op = next) {
1384 next = op->next;
f56dae88
PZ
1385 free(op);
1386 }
3ee88df1 1387 insn->stack_ops = NULL;
f56dae88
PZ
1388}
1389
dd003ede
PZ
1390static void annotate_call_site(struct objtool_file *file,
1391 struct instruction *insn, bool sibling)
f56dae88
PZ
1392{
1393 struct reloc *reloc = insn_reloc(file, insn);
c6f5dc28 1394 struct symbol *sym = insn_call_dest(insn);
f56dae88 1395
dd003ede
PZ
1396 if (!sym)
1397 sym = reloc->sym;
1398
1399 /*
1400 * Alternative replacement code is just template code which is
1401 * sometimes copied to the original instruction. For now, don't
1402 * annotate it. (In the future we might consider annotating the
1403 * original instruction if/when it ever makes sense to do so.)
1404 */
1405 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
f56dae88
PZ
1406 return;
1407
dd003ede
PZ
1408 if (sym->static_call_tramp) {
1409 list_add_tail(&insn->call_node, &file->static_call_list);
1410 return;
f56dae88
PZ
1411 }
1412
134ab5bd
PZ
1413 if (sym->retpoline_thunk) {
1414 list_add_tail(&insn->call_node, &file->retpoline_call_list);
1415 return;
1416 }
1417
f56dae88 1418 /*
05098119
ME
1419 * Many compilers cannot disable KCOV or sanitizer calls with a function
1420 * attribute so they need a little help, NOP out any such calls from
1421 * noinstr text.
f56dae88 1422 */
22102f45 1423 if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) {
f56dae88
PZ
1424 if (reloc) {
1425 reloc->type = R_NONE;
1426 elf_write_reloc(file->elf, reloc);
1427 }
1428
1429 elf_write_insn(file->elf, insn->sec,
1430 insn->offset, insn->len,
1431 sibling ? arch_ret_insn(insn->len)
1432 : arch_nop_insn(insn->len));
1433
1434 insn->type = sibling ? INSN_RETURN : INSN_NOP;
7a53f408
PZ
1435
1436 if (sibling) {
1437 /*
1438 * We've replaced the tail-call JMP insn by two new
1439 * insn: RET; INT3, except we only have a single struct
1440 * insn here. Mark it retpoline_safe to avoid the SLS
1441 * warning, instead of adding another insn.
1442 */
1443 insn->retpoline_safe = true;
1444 }
1445
dd003ede 1446 return;
f56dae88
PZ
1447 }
1448
2daf7fab 1449 if (opts.mcount && sym->fentry) {
f56dae88 1450 if (sibling)
246b2c85 1451 WARN_INSN(insn, "tail call to __fentry__ !?!?");
280981d6
SV
1452 if (opts.mnop) {
1453 if (reloc) {
1454 reloc->type = R_NONE;
1455 elf_write_reloc(file->elf, reloc);
1456 }
f56dae88 1457
280981d6
SV
1458 elf_write_insn(file->elf, insn->sec,
1459 insn->offset, insn->len,
1460 arch_nop_insn(insn->len));
f56dae88 1461
280981d6
SV
1462 insn->type = INSN_NOP;
1463 }
f56dae88 1464
c509331b 1465 list_add_tail(&insn->call_node, &file->mcount_loc_list);
dd003ede 1466 return;
f56dae88 1467 }
0e5b613b 1468
00abd384
PZ
1469 if (insn->type == INSN_CALL && !insn->sec->init)
1470 list_add_tail(&insn->call_node, &file->call_list);
1471
0e5b613b
PZ
1472 if (!sibling && dead_end_function(file, sym))
1473 insn->dead_end = true;
dd003ede
PZ
1474}
1475
1476static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1477 struct symbol *dest, bool sibling)
1478{
c6f5dc28 1479 insn->_call_dest = dest;
dd003ede
PZ
1480 if (!dest)
1481 return;
f56dae88
PZ
1482
1483 /*
1484 * Whatever stack impact regular CALLs have, should be undone
1485 * by the RETURN of the called function.
1486 *
1487 * Annotated intra-function calls retain the stack_ops but
1488 * are converted to JUMP, see read_intra_function_calls().
1489 */
1490 remove_insn_ops(insn);
dd003ede
PZ
1491
1492 annotate_call_site(file, insn, sibling);
f56dae88
PZ
1493}
1494
134ab5bd
PZ
1495static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1496{
1497 /*
1498 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1499 * so convert them accordingly.
1500 */
1501 switch (insn->type) {
1502 case INSN_CALL:
1503 insn->type = INSN_CALL_DYNAMIC;
1504 break;
1505 case INSN_JUMP_UNCONDITIONAL:
1506 insn->type = INSN_JUMP_DYNAMIC;
1507 break;
1508 case INSN_JUMP_CONDITIONAL:
1509 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1510 break;
1511 default:
1512 return;
1513 }
1514
1515 insn->retpoline_safe = true;
1516
1517 /*
1518 * Whatever stack impact regular CALLs have, should be undone
1519 * by the RETURN of the called function.
1520 *
1521 * Annotated intra-function calls retain the stack_ops but
1522 * are converted to JUMP, see read_intra_function_calls().
1523 */
1524 remove_insn_ops(insn);
1525
1526 annotate_call_site(file, insn, false);
1527}
08f87a93 1528
a149180f 1529static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add)
d9e9d230
PZ
1530{
1531 /*
1532 * Return thunk tail calls are really just returns in disguise,
1533 * so convert them accordingly.
1534 */
1535 insn->type = INSN_RETURN;
1536 insn->retpoline_safe = true;
1537
a149180f
PZ
1538 if (add)
1539 list_add_tail(&insn->call_node, &file->return_thunk_list);
d9e9d230
PZ
1540}
1541
5a9c361a
PZ
1542static bool is_first_func_insn(struct objtool_file *file,
1543 struct instruction *insn, struct symbol *sym)
08f87a93 1544{
5a9c361a 1545 if (insn->offset == sym->offset)
d139bca4
PZ
1546 return true;
1547
5a9c361a 1548 /* Allow direct CALL/JMP past ENDBR */
2daf7fab 1549 if (opts.ibt) {
d139bca4
PZ
1550 struct instruction *prev = prev_insn_same_sym(file, insn);
1551
1552 if (prev && prev->type == INSN_ENDBR &&
5a9c361a 1553 insn->offset == sym->offset + prev->len)
d139bca4
PZ
1554 return true;
1555 }
1556
1557 return false;
08f87a93
PZ
1558}
1559
5a9c361a
PZ
1560/*
1561 * A sibling call is a tail-call to another symbol -- to differentiate from a
1562 * recursive tail-call which is to the same symbol.
1563 */
1564static bool jump_is_sibling_call(struct objtool_file *file,
1565 struct instruction *from, struct instruction *to)
1566{
1567 struct symbol *fs = from->sym;
1568 struct symbol *ts = to->sym;
1569
1570 /* Not a sibling call if from/to a symbol hole */
1571 if (!fs || !ts)
1572 return false;
1573
1574 /* Not a sibling call if not targeting the start of a symbol. */
1575 if (!is_first_func_insn(file, to, ts))
1576 return false;
1577
1578 /* Disallow sibling calls into STT_NOTYPE */
1579 if (ts->type == STT_NOTYPE)
1580 return false;
1581
1582 /* Must not be self to be a sibling */
1583 return fs->pfunc != ts->pfunc;
1584}
1585
dcc914f4
JP
1586/*
1587 * Find the destination instructions for all jumps.
1588 */
1589static int add_jump_destinations(struct objtool_file *file)
1590{
26ff6041 1591 struct instruction *insn, *jump_dest;
f1974222 1592 struct reloc *reloc;
dcc914f4
JP
1593 struct section *dest_sec;
1594 unsigned long dest_off;
1595
1596 for_each_insn(file, insn) {
34c861e8
JP
1597 if (insn->jump_dest) {
1598 /*
1599 * handle_group_alt() may have previously set
1600 * 'jump_dest' for some alternatives.
1601 */
1602 continue;
1603 }
a2296140 1604 if (!is_static_jump(insn))
dcc914f4
JP
1605 continue;
1606
7bd2a600 1607 reloc = insn_reloc(file, insn);
f1974222 1608 if (!reloc) {
dcc914f4 1609 dest_sec = insn->sec;
bfb08f22 1610 dest_off = arch_jump_destination(insn);
f1974222
MH
1611 } else if (reloc->sym->type == STT_SECTION) {
1612 dest_sec = reloc->sym->sec;
1613 dest_off = arch_dest_reloc_offset(reloc->addend);
1739c66e 1614 } else if (reloc->sym->retpoline_thunk) {
134ab5bd 1615 add_retpoline_call(file, insn);
39b73533 1616 continue;
d9e9d230 1617 } else if (reloc->sym->return_thunk) {
a149180f 1618 add_return_call(file, insn, true);
d9e9d230 1619 continue;
dbcdbdfd 1620 } else if (insn_func(insn)) {
26ff6041
JP
1621 /*
1622 * External sibling call or internal sibling call with
1623 * STT_FUNC reloc.
1624 */
f56dae88 1625 add_call_dest(file, insn, reloc->sym, true);
dcc914f4 1626 continue;
ecf11ba4
JP
1627 } else if (reloc->sym->sec->idx) {
1628 dest_sec = reloc->sym->sec;
1629 dest_off = reloc->sym->sym.st_value +
1630 arch_dest_reloc_offset(reloc->addend);
1631 } else {
1632 /* non-func asm code jumping to another file */
1633 continue;
dcc914f4
JP
1634 }
1635
26ff6041
JP
1636 jump_dest = find_insn(file, dest_sec, dest_off);
1637 if (!jump_dest) {
a149180f
PZ
1638 struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off);
1639
1640 /*
1641 * This is a special case for zen_untrain_ret().
1642 * It jumps to __x86_return_thunk(), but objtool
1643 * can't find the thunk's starting RET
1644 * instruction, because the RET is also in the
1645 * middle of another instruction. Objtool only
1646 * knows about the outer instruction.
1647 */
1648 if (sym && sym->return_thunk) {
1649 add_return_call(file, insn, false);
1650 continue;
1651 }
1652
246b2c85
JP
1653 WARN_INSN(insn, "can't find jump dest instruction at %s+0x%lx",
1654 dest_sec->name, dest_off);
dcc914f4
JP
1655 return -1;
1656 }
cd77849a
JP
1657
1658 /*
54262aa2 1659 * Cross-function jump.
cd77849a 1660 */
dbcdbdfd
PZ
1661 if (insn_func(insn) && insn_func(jump_dest) &&
1662 insn_func(insn) != insn_func(jump_dest)) {
54262aa2
PZ
1663
1664 /*
1665 * For GCC 8+, create parent/child links for any cold
1666 * subfunctions. This is _mostly_ redundant with a
1667 * similar initialization in read_symbols().
1668 *
1669 * If a function has aliases, we want the *first* such
1670 * function in the symbol table to be the subfunction's
1671 * parent. In that case we overwrite the
1672 * initialization done in read_symbols().
1673 *
1674 * However this code can't completely replace the
1675 * read_symbols() code because this doesn't detect the
1676 * case where the parent function's only reference to a
e7c2bc37 1677 * subfunction is through a jump table.
54262aa2 1678 */
dbcdbdfd
PZ
1679 if (!strstr(insn_func(insn)->name, ".cold") &&
1680 strstr(insn_func(jump_dest)->name, ".cold")) {
1681 insn_func(insn)->cfunc = insn_func(jump_dest);
1682 insn_func(jump_dest)->pfunc = insn_func(insn);
54262aa2 1683 }
cd77849a 1684 }
26ff6041 1685
5a9c361a
PZ
1686 if (jump_is_sibling_call(file, insn, jump_dest)) {
1687 /*
1688 * Internal sibling call without reloc or with
1689 * STT_SECTION reloc.
1690 */
1691 add_call_dest(file, insn, insn_func(jump_dest), true);
1692 continue;
1693 }
1694
26ff6041 1695 insn->jump_dest = jump_dest;
dcc914f4
JP
1696 }
1697
1698 return 0;
1699}
1700
2b232a22
JT
1701static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1702{
1703 struct symbol *call_dest;
1704
1705 call_dest = find_func_by_offset(sec, offset);
1706 if (!call_dest)
1707 call_dest = find_symbol_by_offset(sec, offset);
1708
1709 return call_dest;
1710}
1711
dcc914f4
JP
1712/*
1713 * Find the destination instructions for all calls.
1714 */
1715static int add_call_destinations(struct objtool_file *file)
1716{
1717 struct instruction *insn;
1718 unsigned long dest_off;
f56dae88 1719 struct symbol *dest;
f1974222 1720 struct reloc *reloc;
dcc914f4
JP
1721
1722 for_each_insn(file, insn) {
1723 if (insn->type != INSN_CALL)
1724 continue;
1725
7bd2a600 1726 reloc = insn_reloc(file, insn);
f1974222 1727 if (!reloc) {
bfb08f22 1728 dest_off = arch_jump_destination(insn);
f56dae88
PZ
1729 dest = find_call_destination(insn->sec, dest_off);
1730
1731 add_call_dest(file, insn, dest, false);
a845c7cf 1732
7acfe531
JP
1733 if (insn->ignore)
1734 continue;
1735
c6f5dc28 1736 if (!insn_call_dest(insn)) {
246b2c85 1737 WARN_INSN(insn, "unannotated intra-function call");
dcc914f4
JP
1738 return -1;
1739 }
a845c7cf 1740
c6f5dc28 1741 if (insn_func(insn) && insn_call_dest(insn)->type != STT_FUNC) {
246b2c85 1742 WARN_INSN(insn, "unsupported call to non-function");
7acfe531
JP
1743 return -1;
1744 }
1745
f1974222
MH
1746 } else if (reloc->sym->type == STT_SECTION) {
1747 dest_off = arch_dest_reloc_offset(reloc->addend);
f56dae88
PZ
1748 dest = find_call_destination(reloc->sym->sec, dest_off);
1749 if (!dest) {
246b2c85
JP
1750 WARN_INSN(insn, "can't find call dest symbol at %s+0x%lx",
1751 reloc->sym->sec->name, dest_off);
dcc914f4
JP
1752 return -1;
1753 }
bcb1b6ff 1754
f56dae88
PZ
1755 add_call_dest(file, insn, dest, false);
1756
1739c66e 1757 } else if (reloc->sym->retpoline_thunk) {
134ab5bd 1758 add_retpoline_call(file, insn);
bcb1b6ff 1759
dcc914f4 1760 } else
f56dae88 1761 add_call_dest(file, insn, reloc->sym, false);
dcc914f4
JP
1762 }
1763
1764 return 0;
1765}
1766
1767/*
c9c324dc
JP
1768 * The .alternatives section requires some extra special care over and above
1769 * other special sections because alternatives are patched in place.
dcc914f4
JP
1770 */
1771static int handle_group_alt(struct objtool_file *file,
1772 struct special_alt *special_alt,
1773 struct instruction *orig_insn,
1774 struct instruction **new_insn)
1775{
a706bb08 1776 struct instruction *last_new_insn = NULL, *insn, *nop = NULL;
b23cc71c 1777 struct alt_group *orig_alt_group, *new_alt_group;
dcc914f4
JP
1778 unsigned long dest_off;
1779
a706bb08 1780 orig_alt_group = orig_insn->alt_group;
b23cc71c 1781 if (!orig_alt_group) {
a706bb08 1782 struct instruction *last_orig_insn = NULL;
c9c324dc 1783
a706bb08
PZ
1784 orig_alt_group = malloc(sizeof(*orig_alt_group));
1785 if (!orig_alt_group) {
1786 WARN("malloc failed");
1787 return -1;
1788 }
1789 orig_alt_group->cfi = calloc(special_alt->orig_len,
1790 sizeof(struct cfi_state *));
1791 if (!orig_alt_group->cfi) {
1792 WARN("calloc failed");
1793 return -1;
1794 }
dcc914f4 1795
a706bb08
PZ
1796 insn = orig_insn;
1797 sec_for_each_insn_from(file, insn) {
1798 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1799 break;
dcc914f4 1800
a706bb08
PZ
1801 insn->alt_group = orig_alt_group;
1802 last_orig_insn = insn;
1803 }
1804 orig_alt_group->orig_group = NULL;
1805 orig_alt_group->first_insn = orig_insn;
1806 orig_alt_group->last_insn = last_orig_insn;
1c34496e 1807 orig_alt_group->nop = NULL;
a706bb08
PZ
1808 } else {
1809 if (orig_alt_group->last_insn->offset + orig_alt_group->last_insn->len -
1810 orig_alt_group->first_insn->offset != special_alt->orig_len) {
246b2c85 1811 WARN_INSN(orig_insn, "weirdly overlapping alternative! %ld != %d",
a706bb08
PZ
1812 orig_alt_group->last_insn->offset +
1813 orig_alt_group->last_insn->len -
1814 orig_alt_group->first_insn->offset,
1815 special_alt->orig_len);
1816 return -1;
1817 }
1818 }
17bc3391 1819
c9c324dc
JP
1820 new_alt_group = malloc(sizeof(*new_alt_group));
1821 if (!new_alt_group) {
1822 WARN("malloc failed");
1823 return -1;
dcc914f4 1824 }
dcc914f4 1825
c9c324dc
JP
1826 if (special_alt->new_len < special_alt->orig_len) {
1827 /*
1828 * Insert a fake nop at the end to make the replacement
1829 * alt_group the same size as the original. This is needed to
1830 * allow propagate_alt_cfi() to do its magic. When the last
1831 * instruction affects the stack, the instruction after it (the
1832 * nop) will propagate the new state to the shared CFI array.
1833 */
1834 nop = malloc(sizeof(*nop));
1835 if (!nop) {
1836 WARN("malloc failed");
17bc3391
JP
1837 return -1;
1838 }
c9c324dc 1839 memset(nop, 0, sizeof(*nop));
c9c324dc
JP
1840
1841 nop->sec = special_alt->new_sec;
1842 nop->offset = special_alt->new_off + special_alt->new_len;
1843 nop->len = special_alt->orig_len - special_alt->new_len;
1844 nop->type = INSN_NOP;
dbcdbdfd 1845 nop->sym = orig_insn->sym;
c9c324dc
JP
1846 nop->alt_group = new_alt_group;
1847 nop->ignore = orig_insn->ignore_alts;
dcc914f4 1848 }
17bc3391 1849
c9c324dc
JP
1850 if (!special_alt->new_len) {
1851 *new_insn = nop;
1852 goto end;
dcc914f4
JP
1853 }
1854
dcc914f4
JP
1855 insn = *new_insn;
1856 sec_for_each_insn_from(file, insn) {
45245f51
JT
1857 struct reloc *alt_reloc;
1858
dcc914f4
JP
1859 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1860 break;
1861
1862 last_new_insn = insn;
1863
a845c7cf 1864 insn->ignore = orig_insn->ignore_alts;
dbcdbdfd 1865 insn->sym = orig_insn->sym;
b23cc71c 1866 insn->alt_group = new_alt_group;
a845c7cf 1867
dc419723
JP
1868 /*
1869 * Since alternative replacement code is copy/pasted by the
1870 * kernel after applying relocations, generally such code can't
1871 * have relative-address relocation references to outside the
1872 * .altinstr_replacement section, unless the arch's
1873 * alternatives code can adjust the relative offsets
1874 * accordingly.
dc419723 1875 */
7bd2a600 1876 alt_reloc = insn_reloc(file, insn);
61c6065e 1877 if (alt_reloc && arch_pc_relative_reloc(alt_reloc) &&
45245f51 1878 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
dc419723 1879
246b2c85 1880 WARN_INSN(insn, "unsupported relocation in alternatives section");
dc419723
JP
1881 return -1;
1882 }
1883
a2296140 1884 if (!is_static_jump(insn))
dcc914f4
JP
1885 continue;
1886
1887 if (!insn->immediate)
1888 continue;
1889
bfb08f22 1890 dest_off = arch_jump_destination(insn);
34c861e8 1891 if (dest_off == special_alt->new_off + special_alt->new_len) {
a706bb08 1892 insn->jump_dest = next_insn_same_sec(file, orig_alt_group->last_insn);
34c861e8 1893 if (!insn->jump_dest) {
246b2c85 1894 WARN_INSN(insn, "can't find alternative jump destination");
34c861e8
JP
1895 return -1;
1896 }
dcc914f4
JP
1897 }
1898 }
1899
1900 if (!last_new_insn) {
1901 WARN_FUNC("can't find last new alternative instruction",
1902 special_alt->new_sec, special_alt->new_off);
1903 return -1;
1904 }
1905
c9c324dc 1906end:
b23cc71c
JP
1907 new_alt_group->orig_group = orig_alt_group;
1908 new_alt_group->first_insn = *new_insn;
1c34496e
PZ
1909 new_alt_group->last_insn = last_new_insn;
1910 new_alt_group->nop = nop;
c9c324dc 1911 new_alt_group->cfi = orig_alt_group->cfi;
dcc914f4
JP
1912 return 0;
1913}
1914
1915/*
1916 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1917 * If the original instruction is a jump, make the alt entry an effective nop
1918 * by just skipping the original instruction.
1919 */
1920static int handle_jump_alt(struct objtool_file *file,
1921 struct special_alt *special_alt,
1922 struct instruction *orig_insn,
1923 struct instruction **new_insn)
1924{
48001d26
PZ
1925 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1926 orig_insn->type != INSN_NOP) {
e2d9494b 1927
246b2c85 1928 WARN_INSN(orig_insn, "unsupported instruction at jump label");
dcc914f4
JP
1929 return -1;
1930 }
1931
4ab7674f 1932 if (opts.hack_jump_label && special_alt->key_addend & 2) {
6d37b83c
PZ
1933 struct reloc *reloc = insn_reloc(file, orig_insn);
1934
1935 if (reloc) {
1936 reloc->type = R_NONE;
1937 elf_write_reloc(file->elf, reloc);
1938 }
1939 elf_write_insn(file->elf, orig_insn->sec,
1940 orig_insn->offset, orig_insn->len,
1941 arch_nop_insn(orig_insn->len));
1942 orig_insn->type = INSN_NOP;
48001d26
PZ
1943 }
1944
1945 if (orig_insn->type == INSN_NOP) {
1946 if (orig_insn->len == 2)
1947 file->jl_nop_short++;
1948 else
1949 file->jl_nop_long++;
1950
1951 return 0;
6d37b83c
PZ
1952 }
1953
e2d9494b
PZ
1954 if (orig_insn->len == 2)
1955 file->jl_short++;
1956 else
1957 file->jl_long++;
1958
1c34496e 1959 *new_insn = next_insn_same_sec(file, orig_insn);
dcc914f4
JP
1960 return 0;
1961}
1962
1963/*
1964 * Read all the special sections which have alternate instructions which can be
1965 * patched in or redirected to at runtime. Each instruction having alternate
1966 * instruction(s) has them added to its insn->alts list, which will be
1967 * traversed in validate_branch().
1968 */
1969static int add_special_section_alts(struct objtool_file *file)
1970{
1971 struct list_head special_alts;
1972 struct instruction *orig_insn, *new_insn;
1973 struct special_alt *special_alt, *tmp;
1974 struct alternative *alt;
1975 int ret;
1976
1977 ret = special_get_alts(file->elf, &special_alts);
1978 if (ret)
1979 return ret;
1980
1981 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
dcc914f4
JP
1982
1983 orig_insn = find_insn(file, special_alt->orig_sec,
1984 special_alt->orig_off);
1985 if (!orig_insn) {
1986 WARN_FUNC("special: can't find orig instruction",
1987 special_alt->orig_sec, special_alt->orig_off);
1988 ret = -1;
1989 goto out;
1990 }
1991
1992 new_insn = NULL;
1993 if (!special_alt->group || special_alt->new_len) {
1994 new_insn = find_insn(file, special_alt->new_sec,
1995 special_alt->new_off);
1996 if (!new_insn) {
1997 WARN_FUNC("special: can't find new instruction",
1998 special_alt->new_sec,
1999 special_alt->new_off);
2000 ret = -1;
2001 goto out;
2002 }
2003 }
2004
2005 if (special_alt->group) {
7170cf47 2006 if (!special_alt->orig_len) {
246b2c85 2007 WARN_INSN(orig_insn, "empty alternative entry");
7170cf47
JT
2008 continue;
2009 }
2010
dcc914f4
JP
2011 ret = handle_group_alt(file, special_alt, orig_insn,
2012 &new_insn);
2013 if (ret)
2014 goto out;
2015 } else if (special_alt->jump_or_nop) {
2016 ret = handle_jump_alt(file, special_alt, orig_insn,
2017 &new_insn);
2018 if (ret)
2019 goto out;
2020 }
2021
258c7605
JP
2022 alt = malloc(sizeof(*alt));
2023 if (!alt) {
2024 WARN("malloc failed");
2025 ret = -1;
2026 goto out;
2027 }
2028
dcc914f4 2029 alt->insn = new_insn;
764eef4b 2030 alt->skip_orig = special_alt->skip_orig;
ea24213d 2031 orig_insn->ignore_alts |= special_alt->skip_alt;
d5406654
PZ
2032 alt->next = orig_insn->alts;
2033 orig_insn->alts = alt;
dcc914f4
JP
2034
2035 list_del(&special_alt->list);
2036 free(special_alt);
2037 }
2038
2daf7fab 2039 if (opts.stats) {
e2d9494b
PZ
2040 printf("jl\\\tNOP\tJMP\n");
2041 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
2042 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
2043 }
2044
dcc914f4
JP
2045out:
2046 return ret;
2047}
2048
e7c2bc37 2049static int add_jump_table(struct objtool_file *file, struct instruction *insn,
f1974222 2050 struct reloc *table)
dcc914f4 2051{
f1974222 2052 struct reloc *reloc = table;
e7c2bc37 2053 struct instruction *dest_insn;
dcc914f4 2054 struct alternative *alt;
dbcdbdfd 2055 struct symbol *pfunc = insn_func(insn)->pfunc;
fd35c88b 2056 unsigned int prev_offset = 0;
dcc914f4 2057
e7c2bc37 2058 /*
f1974222 2059 * Each @reloc is a switch table relocation which points to the target
e7c2bc37
JP
2060 * instruction.
2061 */
f1974222 2062 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
bd98c813
JH
2063
2064 /* Check for the end of the table: */
f1974222 2065 if (reloc != table && reloc->jump_table_start)
dcc914f4
JP
2066 break;
2067
e7c2bc37 2068 /* Make sure the table entries are consecutive: */
f1974222 2069 if (prev_offset && reloc->offset != prev_offset + 8)
fd35c88b
JP
2070 break;
2071
2072 /* Detect function pointers from contiguous objects: */
f1974222
MH
2073 if (reloc->sym->sec == pfunc->sec &&
2074 reloc->addend == pfunc->offset)
fd35c88b
JP
2075 break;
2076
f1974222 2077 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
e7c2bc37 2078 if (!dest_insn)
dcc914f4
JP
2079 break;
2080
e7c2bc37 2081 /* Make sure the destination is in the same function: */
dbcdbdfd 2082 if (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc)
13810435 2083 break;
dcc914f4
JP
2084
2085 alt = malloc(sizeof(*alt));
2086 if (!alt) {
2087 WARN("malloc failed");
2088 return -1;
2089 }
2090
e7c2bc37 2091 alt->insn = dest_insn;
d5406654
PZ
2092 alt->next = insn->alts;
2093 insn->alts = alt;
f1974222 2094 prev_offset = reloc->offset;
fd35c88b
JP
2095 }
2096
2097 if (!prev_offset) {
246b2c85 2098 WARN_INSN(insn, "can't find switch jump table");
fd35c88b 2099 return -1;
dcc914f4
JP
2100 }
2101
2102 return 0;
2103}
2104
2105/*
d871f7b5
RG
2106 * find_jump_table() - Given a dynamic jump, find the switch jump table
2107 * associated with it.
dcc914f4 2108 */
f1974222 2109static struct reloc *find_jump_table(struct objtool_file *file,
dcc914f4
JP
2110 struct symbol *func,
2111 struct instruction *insn)
2112{
d871f7b5 2113 struct reloc *table_reloc;
113d4bc9 2114 struct instruction *dest_insn, *orig_insn = insn;
dcc914f4 2115
99ce7962
PZ
2116 /*
2117 * Backward search using the @first_jump_src links, these help avoid
2118 * much of the 'in between' code. Which avoids us getting confused by
2119 * it.
2120 */
7dec80cc 2121 for (;
dbcdbdfd 2122 insn && insn_func(insn) && insn_func(insn)->pfunc == func;
1119d265 2123 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
99ce7962 2124
7dec80cc 2125 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
dcc914f4
JP
2126 break;
2127
2128 /* allow small jumps within the range */
2129 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
2130 insn->jump_dest &&
2131 (insn->jump_dest->offset <= insn->offset ||
2132 insn->jump_dest->offset > orig_insn->offset))
2133 break;
2134
d871f7b5 2135 table_reloc = arch_find_switch_table(file, insn);
f1974222 2136 if (!table_reloc)
e7c2bc37 2137 continue;
f1974222 2138 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
dbcdbdfd 2139 if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func)
113d4bc9 2140 continue;
7dec80cc 2141
f1974222 2142 return table_reloc;
dcc914f4
JP
2143 }
2144
2145 return NULL;
2146}
2147
bd98c813
JH
2148/*
2149 * First pass: Mark the head of each jump table so that in the next pass,
2150 * we know when a given jump table ends and the next one starts.
2151 */
2152static void mark_func_jump_tables(struct objtool_file *file,
2153 struct symbol *func)
dcc914f4 2154{
bd98c813 2155 struct instruction *insn, *last = NULL;
f1974222 2156 struct reloc *reloc;
dcc914f4 2157
f0f70adb 2158 func_for_each_insn(file, func, insn) {
99ce7962
PZ
2159 if (!last)
2160 last = insn;
2161
2162 /*
2163 * Store back-pointers for unconditional forward jumps such
e7c2bc37 2164 * that find_jump_table() can back-track using those and
99ce7962
PZ
2165 * avoid some potentially confusing code.
2166 */
2167 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
2168 insn->offset > last->offset &&
2169 insn->jump_dest->offset > insn->offset &&
2170 !insn->jump_dest->first_jump_src) {
2171
2172 insn->jump_dest->first_jump_src = insn;
2173 last = insn->jump_dest;
2174 }
2175
dcc914f4
JP
2176 if (insn->type != INSN_JUMP_DYNAMIC)
2177 continue;
2178
f1974222
MH
2179 reloc = find_jump_table(file, func, insn);
2180 if (reloc) {
2181 reloc->jump_table_start = true;
c6f5dc28 2182 insn->_jump_table = reloc;
dcc914f4 2183 }
dcc914f4 2184 }
bd98c813
JH
2185}
2186
2187static int add_func_jump_tables(struct objtool_file *file,
2188 struct symbol *func)
2189{
2190 struct instruction *insn;
2191 int ret;
2192
f0f70adb 2193 func_for_each_insn(file, func, insn) {
c6f5dc28 2194 if (!insn_jump_table(insn))
bd98c813 2195 continue;
dcc914f4 2196
c6f5dc28 2197 ret = add_jump_table(file, insn, insn_jump_table(insn));
dcc914f4
JP
2198 if (ret)
2199 return ret;
2200 }
2201
2202 return 0;
2203}
2204
2205/*
2206 * For some switch statements, gcc generates a jump table in the .rodata
2207 * section which contains a list of addresses within the function to jump to.
2208 * This finds these jump tables and adds them to the insn->alts lists.
2209 */
e7c2bc37 2210static int add_jump_table_alts(struct objtool_file *file)
dcc914f4 2211{
dcc914f4
JP
2212 struct symbol *func;
2213 int ret;
2214
4a60aa05 2215 if (!file->rodata)
dcc914f4
JP
2216 return 0;
2217
9290e772
JP
2218 for_each_sym(file, func) {
2219 if (func->type != STT_FUNC)
2220 continue;
dcc914f4 2221
9290e772
JP
2222 mark_func_jump_tables(file, func);
2223 ret = add_func_jump_tables(file, func);
2224 if (ret)
2225 return ret;
dcc914f4
JP
2226 }
2227
2228 return 0;
2229}
2230
b735bd3e
JP
2231static void set_func_state(struct cfi_state *state)
2232{
2233 state->cfa = initial_func_cfi.cfa;
2234 memcpy(&state->regs, &initial_func_cfi.regs,
2235 CFI_NUM_REGS * sizeof(struct cfi_reg));
2236 state->stack_size = initial_func_cfi.cfa.offset;
fb799447 2237 state->type = UNWIND_HINT_TYPE_CALL;
b735bd3e
JP
2238}
2239
39358a03
JP
2240static int read_unwind_hints(struct objtool_file *file)
2241{
8b946cc3 2242 struct cfi_state cfi = init_cfi;
f1974222 2243 struct section *sec, *relocsec;
39358a03
JP
2244 struct unwind_hint *hint;
2245 struct instruction *insn;
8b946cc3 2246 struct reloc *reloc;
39358a03
JP
2247 int i;
2248
2249 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
2250 if (!sec)
2251 return 0;
2252
f1974222
MH
2253 relocsec = sec->reloc;
2254 if (!relocsec) {
39358a03
JP
2255 WARN("missing .rela.discard.unwind_hints section");
2256 return -1;
2257 }
2258
fe255fe6 2259 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
39358a03
JP
2260 WARN("struct unwind_hint size mismatch");
2261 return -1;
2262 }
2263
2264 file->hints = true;
2265
fe255fe6 2266 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
39358a03
JP
2267 hint = (struct unwind_hint *)sec->data->d_buf + i;
2268
f1974222
MH
2269 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
2270 if (!reloc) {
2271 WARN("can't find reloc for unwind_hints[%d]", i);
39358a03
JP
2272 return -1;
2273 }
2274
f1974222 2275 insn = find_insn(file, reloc->sym->sec, reloc->addend);
39358a03
JP
2276 if (!insn) {
2277 WARN("can't find insn for unwind_hints[%d]", i);
2278 return -1;
2279 }
2280
b735bd3e 2281 insn->hint = true;
39358a03 2282
8faea26e
JP
2283 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
2284 insn->hint = false;
2285 insn->save = true;
2286 continue;
2287 }
2288
2289 if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
2290 insn->restore = true;
2291 continue;
2292 }
2293
a09a6e23 2294 if (hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) {
08f87a93
PZ
2295 struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset);
2296
a09a6e23
PZ
2297 if (sym && sym->bind == STB_GLOBAL) {
2298 if (opts.ibt && insn->type != INSN_ENDBR && !insn->noendbr) {
246b2c85 2299 WARN_INSN(insn, "UNWIND_HINT_IRET_REGS without ENDBR");
a09a6e23 2300 }
08f87a93
PZ
2301 }
2302 }
2303
b735bd3e 2304 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
8b946cc3 2305 insn->cfi = &func_cfi;
39358a03
JP
2306 continue;
2307 }
2308
8b946cc3
PZ
2309 if (insn->cfi)
2310 cfi = *(insn->cfi);
2311
2312 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
246b2c85 2313 WARN_INSN(insn, "unsupported unwind_hint sp base reg %d", hint->sp_reg);
39358a03
JP
2314 return -1;
2315 }
2316
0646c28b 2317 cfi.cfa.offset = bswap_if_needed(file->elf, hint->sp_offset);
8b946cc3 2318 cfi.type = hint->type;
00c8f01c 2319 cfi.signal = hint->signal;
8b946cc3
PZ
2320
2321 insn->cfi = cfi_hash_find_or_add(&cfi);
39358a03
JP
2322 }
2323
2324 return 0;
2325}
2326
96db4a98
PZ
2327static int read_noendbr_hints(struct objtool_file *file)
2328{
2329 struct section *sec;
2330 struct instruction *insn;
2331 struct reloc *reloc;
2332
2333 sec = find_section_by_name(file->elf, ".rela.discard.noendbr");
2334 if (!sec)
2335 return 0;
2336
2337 list_for_each_entry(reloc, &sec->reloc_list, list) {
2338 insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend);
2339 if (!insn) {
2340 WARN("bad .discard.noendbr entry");
2341 return -1;
2342 }
2343
2344 insn->noendbr = 1;
2345 }
2346
2347 return 0;
2348}
2349
b5bc2231
PZ
2350static int read_retpoline_hints(struct objtool_file *file)
2351{
63474dc4 2352 struct section *sec;
b5bc2231 2353 struct instruction *insn;
f1974222 2354 struct reloc *reloc;
b5bc2231 2355
63474dc4 2356 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
b5bc2231
PZ
2357 if (!sec)
2358 return 0;
2359
f1974222
MH
2360 list_for_each_entry(reloc, &sec->reloc_list, list) {
2361 if (reloc->sym->type != STT_SECTION) {
63474dc4 2362 WARN("unexpected relocation symbol type in %s", sec->name);
b5bc2231
PZ
2363 return -1;
2364 }
2365
f1974222 2366 insn = find_insn(file, reloc->sym->sec, reloc->addend);
b5bc2231 2367 if (!insn) {
63474dc4 2368 WARN("bad .discard.retpoline_safe entry");
b5bc2231
PZ
2369 return -1;
2370 }
2371
2372 if (insn->type != INSN_JUMP_DYNAMIC &&
9bb2ec60 2373 insn->type != INSN_CALL_DYNAMIC &&
a09a6e23
PZ
2374 insn->type != INSN_RETURN &&
2375 insn->type != INSN_NOP) {
246b2c85 2376 WARN_INSN(insn, "retpoline_safe hint not an indirect jump/call/ret/nop");
b5bc2231
PZ
2377 return -1;
2378 }
2379
2380 insn->retpoline_safe = true;
2381 }
2382
2383 return 0;
2384}
2385
c4a33939
PZ
2386static int read_instr_hints(struct objtool_file *file)
2387{
2388 struct section *sec;
2389 struct instruction *insn;
f1974222 2390 struct reloc *reloc;
c4a33939
PZ
2391
2392 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
2393 if (!sec)
2394 return 0;
2395
f1974222
MH
2396 list_for_each_entry(reloc, &sec->reloc_list, list) {
2397 if (reloc->sym->type != STT_SECTION) {
c4a33939
PZ
2398 WARN("unexpected relocation symbol type in %s", sec->name);
2399 return -1;
2400 }
2401
f1974222 2402 insn = find_insn(file, reloc->sym->sec, reloc->addend);
c4a33939
PZ
2403 if (!insn) {
2404 WARN("bad .discard.instr_end entry");
2405 return -1;
2406 }
2407
2408 insn->instr--;
2409 }
2410
2411 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
2412 if (!sec)
2413 return 0;
2414
f1974222
MH
2415 list_for_each_entry(reloc, &sec->reloc_list, list) {
2416 if (reloc->sym->type != STT_SECTION) {
c4a33939
PZ
2417 WARN("unexpected relocation symbol type in %s", sec->name);
2418 return -1;
2419 }
2420
f1974222 2421 insn = find_insn(file, reloc->sym->sec, reloc->addend);
c4a33939
PZ
2422 if (!insn) {
2423 WARN("bad .discard.instr_begin entry");
2424 return -1;
2425 }
2426
2427 insn->instr++;
2428 }
2429
2430 return 0;
2431}
2432
4708ea14
JP
2433static int read_validate_unret_hints(struct objtool_file *file)
2434{
2435 struct section *sec;
2436 struct instruction *insn;
2437 struct reloc *reloc;
2438
2439 sec = find_section_by_name(file->elf, ".rela.discard.validate_unret");
2440 if (!sec)
2441 return 0;
2442
2443 list_for_each_entry(reloc, &sec->reloc_list, list) {
2444 if (reloc->sym->type != STT_SECTION) {
2445 WARN("unexpected relocation symbol type in %s", sec->name);
2446 return -1;
2447 }
2448
2449 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2450 if (!insn) {
2451 WARN("bad .discard.instr_end entry");
2452 return -1;
2453 }
2454 insn->unret = 1;
2455 }
2456
2457 return 0;
2458}
2459
2460
8aa8eb2a
AC
2461static int read_intra_function_calls(struct objtool_file *file)
2462{
2463 struct instruction *insn;
2464 struct section *sec;
f1974222 2465 struct reloc *reloc;
8aa8eb2a
AC
2466
2467 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
2468 if (!sec)
2469 return 0;
2470
f1974222 2471 list_for_each_entry(reloc, &sec->reloc_list, list) {
8aa8eb2a
AC
2472 unsigned long dest_off;
2473
f1974222 2474 if (reloc->sym->type != STT_SECTION) {
8aa8eb2a
AC
2475 WARN("unexpected relocation symbol type in %s",
2476 sec->name);
2477 return -1;
2478 }
2479
f1974222 2480 insn = find_insn(file, reloc->sym->sec, reloc->addend);
8aa8eb2a
AC
2481 if (!insn) {
2482 WARN("bad .discard.intra_function_call entry");
2483 return -1;
2484 }
2485
2486 if (insn->type != INSN_CALL) {
246b2c85 2487 WARN_INSN(insn, "intra_function_call not a direct call");
8aa8eb2a
AC
2488 return -1;
2489 }
2490
2491 /*
2492 * Treat intra-function CALLs as JMPs, but with a stack_op.
2493 * See add_call_destinations(), which strips stack_ops from
2494 * normal CALLs.
2495 */
2496 insn->type = INSN_JUMP_UNCONDITIONAL;
2497
7b3e3186 2498 dest_off = arch_jump_destination(insn);
8aa8eb2a
AC
2499 insn->jump_dest = find_insn(file, insn->sec, dest_off);
2500 if (!insn->jump_dest) {
246b2c85 2501 WARN_INSN(insn, "can't find call dest at %s+0x%lx",
8aa8eb2a
AC
2502 insn->sec->name, dest_off);
2503 return -1;
2504 }
2505 }
2506
2507 return 0;
2508}
2509
05098119
ME
2510/*
2511 * Return true if name matches an instrumentation function, where calls to that
2512 * function from noinstr code can safely be removed, but compilers won't do so.
2513 */
2514static bool is_profiling_func(const char *name)
2515{
2516 /*
2517 * Many compilers cannot disable KCOV with a function attribute.
2518 */
2519 if (!strncmp(name, "__sanitizer_cov_", 16))
2520 return true;
2521
2522 /*
2523 * Some compilers currently do not remove __tsan_func_entry/exit nor
2524 * __tsan_atomic_signal_fence (used for barrier instrumentation) with
2525 * the __no_sanitize_thread attribute, remove them. Once the kernel's
2526 * minimum Clang version is 14.0, this can be removed.
2527 */
2528 if (!strncmp(name, "__tsan_func_", 12) ||
2529 !strcmp(name, "__tsan_atomic_signal_fence"))
2530 return true;
2531
2532 return false;
2533}
2534
1739c66e 2535static int classify_symbols(struct objtool_file *file)
1e7e4788 2536{
1e7e4788
JP
2537 struct symbol *func;
2538
9290e772
JP
2539 for_each_sym(file, func) {
2540 if (func->bind != STB_GLOBAL)
2541 continue;
1739c66e 2542
9290e772
JP
2543 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
2544 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2545 func->static_call_tramp = true;
1739c66e 2546
9290e772
JP
2547 if (arch_is_retpoline(func))
2548 func->retpoline_thunk = true;
1739c66e 2549
9290e772
JP
2550 if (arch_is_rethunk(func))
2551 func->return_thunk = true;
d9e9d230 2552
9290e772
JP
2553 if (arch_ftrace_match(func->name))
2554 func->fentry = true;
1739c66e 2555
9290e772
JP
2556 if (is_profiling_func(func->name))
2557 func->profiling_func = true;
1e7e4788
JP
2558 }
2559
2560 return 0;
2561}
2562
4a60aa05
AX
2563static void mark_rodata(struct objtool_file *file)
2564{
2565 struct section *sec;
2566 bool found = false;
2567
2568 /*
87b512de
JP
2569 * Search for the following rodata sections, each of which can
2570 * potentially contain jump tables:
2571 *
2572 * - .rodata: can contain GCC switch tables
2573 * - .rodata.<func>: same, if -fdata-sections is being used
2574 * - .rodata..c_jump_table: contains C annotated jump tables
2575 *
2576 * .rodata.str1.* sections are ignored; they don't contain jump tables.
4a60aa05
AX
2577 */
2578 for_each_sec(file, sec) {
1ee44470
MS
2579 if (!strncmp(sec->name, ".rodata", 7) &&
2580 !strstr(sec->name, ".str1.")) {
4a60aa05
AX
2581 sec->rodata = true;
2582 found = true;
2583 }
2584 }
2585
2586 file->rodata = found;
2587}
2588
dcc914f4
JP
2589static int decode_sections(struct objtool_file *file)
2590{
2591 int ret;
2592
4a60aa05
AX
2593 mark_rodata(file);
2594
db2b0c5d
PZ
2595 ret = init_pv_ops(file);
2596 if (ret)
2597 return ret;
2598
dbcdbdfd
PZ
2599 /*
2600 * Must be before add_{jump_call}_destination.
2601 */
2602 ret = classify_symbols(file);
2603 if (ret)
2604 return ret;
2605
dcc914f4
JP
2606 ret = decode_instructions(file);
2607 if (ret)
2608 return ret;
2609
dcc914f4 2610 add_ignores(file);
ea24213d 2611 add_uaccess_safe(file);
dcc914f4 2612
ff05ab23 2613 ret = add_ignore_alternatives(file);
258c7605
JP
2614 if (ret)
2615 return ret;
2616
08f87a93
PZ
2617 /*
2618 * Must be before read_unwind_hints() since that needs insn->noendbr.
2619 */
96db4a98
PZ
2620 ret = read_noendbr_hints(file);
2621 if (ret)
2622 return ret;
2623
43d5430a 2624 /*
34c861e8
JP
2625 * Must be before add_jump_destinations(), which depends on 'func'
2626 * being set for alternatives, to enable proper sibling call detection.
43d5430a 2627 */
de6fbced
SV
2628 if (opts.stackval || opts.orc || opts.uaccess || opts.noinstr) {
2629 ret = add_special_section_alts(file);
2630 if (ret)
2631 return ret;
2632 }
dcc914f4 2633
34c861e8 2634 ret = add_jump_destinations(file);
dcc914f4
JP
2635 if (ret)
2636 return ret;
2637
a958c4fe
PZ
2638 /*
2639 * Must be before add_call_destination(); it changes INSN_CALL to
2640 * INSN_JUMP.
2641 */
8aa8eb2a
AC
2642 ret = read_intra_function_calls(file);
2643 if (ret)
2644 return ret;
2645
a845c7cf 2646 ret = add_call_destinations(file);
dcc914f4
JP
2647 if (ret)
2648 return ret;
2649
0e5b613b
PZ
2650 /*
2651 * Must be after add_call_destinations() such that it can override
2652 * dead_end_function() marks.
2653 */
2654 ret = add_dead_ends(file);
2655 if (ret)
2656 return ret;
2657
e7c2bc37 2658 ret = add_jump_table_alts(file);
dcc914f4
JP
2659 if (ret)
2660 return ret;
2661
39358a03
JP
2662 ret = read_unwind_hints(file);
2663 if (ret)
2664 return ret;
2665
b5bc2231
PZ
2666 ret = read_retpoline_hints(file);
2667 if (ret)
2668 return ret;
2669
c4a33939
PZ
2670 ret = read_instr_hints(file);
2671 if (ret)
2672 return ret;
2673
4708ea14
JP
2674 ret = read_validate_unret_hints(file);
2675 if (ret)
2676 return ret;
2677
dcc914f4
JP
2678 return 0;
2679}
2680
2681static bool is_fentry_call(struct instruction *insn)
2682{
1739c66e 2683 if (insn->type == INSN_CALL &&
c6f5dc28
PZ
2684 insn_call_dest(insn) &&
2685 insn_call_dest(insn)->fentry)
dcc914f4
JP
2686 return true;
2687
2688 return false;
2689}
2690
e25eea89 2691static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
dcc914f4 2692{
e7c0219b 2693 struct cfi_state *cfi = &state->cfi;
baa41469
JP
2694 int i;
2695
e7c0219b 2696 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
e25eea89
PZ
2697 return true;
2698
b735bd3e 2699 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
baa41469
JP
2700 return true;
2701
b735bd3e 2702 if (cfi->stack_size != initial_func_cfi.cfa.offset)
e25eea89
PZ
2703 return true;
2704
2705 for (i = 0; i < CFI_NUM_REGS; i++) {
e7c0219b
PZ
2706 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2707 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
baa41469 2708 return true;
e25eea89 2709 }
baa41469
JP
2710
2711 return false;
2712}
2713
fb084fde
JT
2714static bool check_reg_frame_pos(const struct cfi_reg *reg,
2715 int expected_offset)
2716{
2717 return reg->base == CFI_CFA &&
2718 reg->offset == expected_offset;
2719}
2720
baa41469
JP
2721static bool has_valid_stack_frame(struct insn_state *state)
2722{
e7c0219b
PZ
2723 struct cfi_state *cfi = &state->cfi;
2724
fb084fde
JT
2725 if (cfi->cfa.base == CFI_BP &&
2726 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2727 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
baa41469
JP
2728 return true;
2729
e7c0219b 2730 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
baa41469
JP
2731 return true;
2732
2733 return false;
dcc914f4
JP
2734}
2735
e7c0219b
PZ
2736static int update_cfi_state_regs(struct instruction *insn,
2737 struct cfi_state *cfi,
65ea47dc 2738 struct stack_op *op)
627fce14 2739{
e7c0219b 2740 struct cfi_reg *cfa = &cfi->cfa;
627fce14 2741
d8dd25a4 2742 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
627fce14
JP
2743 return 0;
2744
2745 /* push */
ea24213d 2746 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
627fce14
JP
2747 cfa->offset += 8;
2748
2749 /* pop */
ea24213d 2750 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
627fce14
JP
2751 cfa->offset -= 8;
2752
2753 /* add immediate to sp */
2754 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2755 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2756 cfa->offset -= op->src.offset;
2757
2758 return 0;
2759}
2760
e7c0219b 2761static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
dcc914f4 2762{
bf4d1a83 2763 if (arch_callee_saved_reg(reg) &&
e7c0219b
PZ
2764 cfi->regs[reg].base == CFI_UNDEFINED) {
2765 cfi->regs[reg].base = base;
2766 cfi->regs[reg].offset = offset;
baa41469 2767 }
dcc914f4
JP
2768}
2769
e7c0219b 2770static void restore_reg(struct cfi_state *cfi, unsigned char reg)
dcc914f4 2771{
e7c0219b
PZ
2772 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2773 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
baa41469
JP
2774}
2775
2776/*
2777 * A note about DRAP stack alignment:
2778 *
2779 * GCC has the concept of a DRAP register, which is used to help keep track of
2780 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2781 * register. The typical DRAP pattern is:
2782 *
2783 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2784 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2785 * 41 ff 72 f8 pushq -0x8(%r10)
2786 * 55 push %rbp
2787 * 48 89 e5 mov %rsp,%rbp
2788 * (more pushes)
2789 * 41 52 push %r10
2790 * ...
2791 * 41 5a pop %r10
2792 * (more pops)
2793 * 5d pop %rbp
2794 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2795 * c3 retq
2796 *
2797 * There are some variations in the epilogues, like:
2798 *
2799 * 5b pop %rbx
2800 * 41 5a pop %r10
2801 * 41 5c pop %r12
2802 * 41 5d pop %r13
2803 * 41 5e pop %r14
2804 * c9 leaveq
2805 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2806 * c3 retq
2807 *
2808 * and:
2809 *
2810 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2811 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2812 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2813 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2814 * c9 leaveq
2815 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2816 * c3 retq
2817 *
2818 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2819 * restored beforehand:
2820 *
2821 * 41 55 push %r13
2822 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2823 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2824 * ...
2825 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2826 * 41 5d pop %r13
2827 * c3 retq
2828 */
d54dba41
PZ
2829static int update_cfi_state(struct instruction *insn,
2830 struct instruction *next_insn,
2831 struct cfi_state *cfi, struct stack_op *op)
baa41469 2832{
e7c0219b
PZ
2833 struct cfi_reg *cfa = &cfi->cfa;
2834 struct cfi_reg *regs = cfi->regs;
baa41469
JP
2835
2836 /* stack operations don't make sense with an undefined CFA */
2837 if (cfa->base == CFI_UNDEFINED) {
dbcdbdfd 2838 if (insn_func(insn)) {
246b2c85 2839 WARN_INSN(insn, "undefined stack state");
baa41469
JP
2840 return -1;
2841 }
2842 return 0;
2843 }
2844
ee819aed
JT
2845 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2846 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
e7c0219b 2847 return update_cfi_state_regs(insn, cfi, op);
627fce14 2848
baa41469
JP
2849 switch (op->dest.type) {
2850
2851 case OP_DEST_REG:
2852 switch (op->src.type) {
2853
2854 case OP_SRC_REG:
0d0970ee
JP
2855 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2856 cfa->base == CFI_SP &&
fb084fde 2857 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset)) {
0d0970ee
JP
2858
2859 /* mov %rsp, %rbp */
2860 cfa->base = op->dest.reg;
e7c0219b 2861 cfi->bp_scratch = false;
0d0970ee 2862 }
dd88a0a0 2863
0d0970ee 2864 else if (op->src.reg == CFI_SP &&
e7c0219b 2865 op->dest.reg == CFI_BP && cfi->drap) {
dd88a0a0 2866
0d0970ee
JP
2867 /* drap: mov %rsp, %rbp */
2868 regs[CFI_BP].base = CFI_BP;
e7c0219b
PZ
2869 regs[CFI_BP].offset = -cfi->stack_size;
2870 cfi->bp_scratch = false;
0d0970ee 2871 }
dd88a0a0 2872
0d0970ee
JP
2873 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2874
2875 /*
2876 * mov %rsp, %reg
2877 *
2878 * This is needed for the rare case where GCC
2879 * does:
2880 *
2881 * mov %rsp, %rax
2882 * ...
2883 * mov %rax, %rsp
2884 */
e7c0219b
PZ
2885 cfi->vals[op->dest.reg].base = CFI_CFA;
2886 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
dd88a0a0
JP
2887 }
2888
3c1f0583 2889 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
ffc7e74f 2890 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
3c1f0583
JP
2891
2892 /*
2893 * mov %rbp, %rsp
2894 *
2895 * Restore the original stack pointer (Clang).
2896 */
e7c0219b 2897 cfi->stack_size = -cfi->regs[CFI_BP].offset;
3c1f0583
JP
2898 }
2899
dd88a0a0
JP
2900 else if (op->dest.reg == cfa->base) {
2901
2902 /* mov %reg, %rsp */
2903 if (cfa->base == CFI_SP &&
e7c0219b 2904 cfi->vals[op->src.reg].base == CFI_CFA) {
dd88a0a0
JP
2905
2906 /*
2907 * This is needed for the rare case
2908 * where GCC does something dumb like:
2909 *
2910 * lea 0x8(%rsp), %rcx
2911 * ...
2912 * mov %rcx, %rsp
2913 */
e7c0219b
PZ
2914 cfa->offset = -cfi->vals[op->src.reg].offset;
2915 cfi->stack_size = cfa->offset;
dd88a0a0 2916
aafeb14e
PZ
2917 } else if (cfa->base == CFI_SP &&
2918 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2919 cfi->vals[op->src.reg].offset == cfa->offset) {
2920
2921 /*
2922 * Stack swizzle:
2923 *
2924 * 1: mov %rsp, (%[tos])
2925 * 2: mov %[tos], %rsp
2926 * ...
2927 * 3: pop %rsp
2928 *
2929 * Where:
2930 *
2931 * 1 - places a pointer to the previous
2932 * stack at the Top-of-Stack of the
2933 * new stack.
2934 *
2935 * 2 - switches to the new stack.
2936 *
2937 * 3 - pops the Top-of-Stack to restore
2938 * the original stack.
2939 *
2940 * Note: we set base to SP_INDIRECT
2941 * here and preserve offset. Therefore
2942 * when the unwinder reaches ToS it
2943 * will dereference SP and then add the
2944 * offset to find the next frame, IOW:
2945 * (%rsp) + offset.
2946 */
2947 cfa->base = CFI_SP_INDIRECT;
2948
dd88a0a0
JP
2949 } else {
2950 cfa->base = CFI_UNDEFINED;
2951 cfa->offset = 0;
2952 }
baa41469
JP
2953 }
2954
724c8a23
PZ
2955 else if (op->dest.reg == CFI_SP &&
2956 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2957 cfi->vals[op->src.reg].offset == cfa->offset) {
2958
2959 /*
2960 * The same stack swizzle case 2) as above. But
2961 * because we can't change cfa->base, case 3)
2962 * will become a regular POP. Pretend we're a
2963 * PUSH so things don't go unbalanced.
2964 */
2965 cfi->stack_size += 8;
2966 }
2967
2968
baa41469
JP
2969 break;
2970
2971 case OP_SRC_ADD:
2972 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2973
2974 /* add imm, %rsp */
e7c0219b 2975 cfi->stack_size -= op->src.offset;
baa41469
JP
2976 if (cfa->base == CFI_SP)
2977 cfa->offset -= op->src.offset;
2978 break;
2979 }
2980
2981 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2982
2983 /* lea disp(%rbp), %rsp */
e7c0219b 2984 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
baa41469
JP
2985 break;
2986 }
2987
dd88a0a0 2988 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
baa41469
JP
2989
2990 /* drap: lea disp(%rsp), %drap */
e7c0219b 2991 cfi->drap_reg = op->dest.reg;
dd88a0a0
JP
2992
2993 /*
2994 * lea disp(%rsp), %reg
2995 *
2996 * This is needed for the rare case where GCC
2997 * does something dumb like:
2998 *
2999 * lea 0x8(%rsp), %rcx
3000 * ...
3001 * mov %rcx, %rsp
3002 */
e7c0219b
PZ
3003 cfi->vals[op->dest.reg].base = CFI_CFA;
3004 cfi->vals[op->dest.reg].offset = \
3005 -cfi->stack_size + op->src.offset;
dd88a0a0 3006
baa41469
JP
3007 break;
3008 }
3009
e7c0219b
PZ
3010 if (cfi->drap && op->dest.reg == CFI_SP &&
3011 op->src.reg == cfi->drap_reg) {
baa41469
JP
3012
3013 /* drap: lea disp(%drap), %rsp */
3014 cfa->base = CFI_SP;
e7c0219b
PZ
3015 cfa->offset = cfi->stack_size = -op->src.offset;
3016 cfi->drap_reg = CFI_UNDEFINED;
3017 cfi->drap = false;
baa41469
JP
3018 break;
3019 }
3020
d54dba41 3021 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
246b2c85 3022 WARN_INSN(insn, "unsupported stack register modification");
baa41469
JP
3023 return -1;
3024 }
3025
3026 break;
3027
3028 case OP_SRC_AND:
3029 if (op->dest.reg != CFI_SP ||
e7c0219b
PZ
3030 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
3031 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
246b2c85 3032 WARN_INSN(insn, "unsupported stack pointer realignment");
baa41469
JP
3033 return -1;
3034 }
3035
e7c0219b 3036 if (cfi->drap_reg != CFI_UNDEFINED) {
baa41469 3037 /* drap: and imm, %rsp */
e7c0219b
PZ
3038 cfa->base = cfi->drap_reg;
3039 cfa->offset = cfi->stack_size = 0;
3040 cfi->drap = true;
baa41469
JP
3041 }
3042
3043 /*
3044 * Older versions of GCC (4.8ish) realign the stack
3045 * without DRAP, with a frame pointer.
3046 */
3047
3048 break;
3049
3050 case OP_SRC_POP:
ea24213d 3051 case OP_SRC_POPF:
aafeb14e
PZ
3052 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
3053
3054 /* pop %rsp; # restore from a stack swizzle */
3055 cfa->base = CFI_SP;
3056 break;
3057 }
3058
e7c0219b 3059 if (!cfi->drap && op->dest.reg == cfa->base) {
baa41469
JP
3060
3061 /* pop %rbp */
3062 cfa->base = CFI_SP;
3063 }
3064
e7c0219b
PZ
3065 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
3066 op->dest.reg == cfi->drap_reg &&
3067 cfi->drap_offset == -cfi->stack_size) {
baa41469 3068
bf4d1a83 3069 /* drap: pop %drap */
e7c0219b 3070 cfa->base = cfi->drap_reg;
bf4d1a83 3071 cfa->offset = 0;
e7c0219b 3072 cfi->drap_offset = -1;
baa41469 3073
ffc7e74f 3074 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
baa41469 3075
bf4d1a83 3076 /* pop %reg */
e7c0219b 3077 restore_reg(cfi, op->dest.reg);
baa41469
JP
3078 }
3079
e7c0219b 3080 cfi->stack_size -= 8;
baa41469
JP
3081 if (cfa->base == CFI_SP)
3082 cfa->offset -= 8;
3083
3084 break;
3085
3086 case OP_SRC_REG_INDIRECT:
201ef5a9
JT
3087 if (!cfi->drap && op->dest.reg == cfa->base &&
3088 op->dest.reg == CFI_BP) {
3089
3090 /* mov disp(%rsp), %rbp */
3091 cfa->base = CFI_SP;
3092 cfa->offset = cfi->stack_size;
3093 }
3094
e7c0219b
PZ
3095 if (cfi->drap && op->src.reg == CFI_BP &&
3096 op->src.offset == cfi->drap_offset) {
bf4d1a83
JP
3097
3098 /* drap: mov disp(%rbp), %drap */
e7c0219b 3099 cfa->base = cfi->drap_reg;
bf4d1a83 3100 cfa->offset = 0;
e7c0219b 3101 cfi->drap_offset = -1;
bf4d1a83
JP
3102 }
3103
e7c0219b 3104 if (cfi->drap && op->src.reg == CFI_BP &&
baa41469
JP
3105 op->src.offset == regs[op->dest.reg].offset) {
3106
3107 /* drap: mov disp(%rbp), %reg */
e7c0219b 3108 restore_reg(cfi, op->dest.reg);
baa41469
JP
3109
3110 } else if (op->src.reg == cfa->base &&
3111 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
3112
3113 /* mov disp(%rbp), %reg */
3114 /* mov disp(%rsp), %reg */
e7c0219b 3115 restore_reg(cfi, op->dest.reg);
201ef5a9
JT
3116
3117 } else if (op->src.reg == CFI_SP &&
3118 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
3119
3120 /* mov disp(%rsp), %reg */
3121 restore_reg(cfi, op->dest.reg);
baa41469
JP
3122 }
3123
3124 break;
3125
3126 default:
246b2c85 3127 WARN_INSN(insn, "unknown stack-related instruction");
baa41469
JP
3128 return -1;
3129 }
3130
3131 break;
3132
3133 case OP_DEST_PUSH:
ea24213d 3134 case OP_DEST_PUSHF:
e7c0219b 3135 cfi->stack_size += 8;
baa41469
JP
3136 if (cfa->base == CFI_SP)
3137 cfa->offset += 8;
3138
3139 if (op->src.type != OP_SRC_REG)
3140 break;
3141
e7c0219b
PZ
3142 if (cfi->drap) {
3143 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
baa41469
JP
3144
3145 /* drap: push %drap */
3146 cfa->base = CFI_BP_INDIRECT;
e7c0219b 3147 cfa->offset = -cfi->stack_size;
baa41469 3148
bf4d1a83 3149 /* save drap so we know when to restore it */
e7c0219b 3150 cfi->drap_offset = -cfi->stack_size;
baa41469 3151
e7c0219b 3152 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
baa41469
JP
3153
3154 /* drap: push %rbp */
e7c0219b 3155 cfi->stack_size = 0;
baa41469 3156
f4f80398 3157 } else {
baa41469
JP
3158
3159 /* drap: push %reg */
e7c0219b 3160 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
baa41469
JP
3161 }
3162
3163 } else {
3164
3165 /* push %reg */
e7c0219b 3166 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
baa41469
JP
3167 }
3168
3169 /* detect when asm code uses rbp as a scratch register */
dbcdbdfd 3170 if (opts.stackval && insn_func(insn) && op->src.reg == CFI_BP &&
baa41469 3171 cfa->base != CFI_BP)
e7c0219b 3172 cfi->bp_scratch = true;
baa41469
JP
3173 break;
3174
3175 case OP_DEST_REG_INDIRECT:
3176
e7c0219b
PZ
3177 if (cfi->drap) {
3178 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
baa41469
JP
3179
3180 /* drap: mov %drap, disp(%rbp) */
3181 cfa->base = CFI_BP_INDIRECT;
3182 cfa->offset = op->dest.offset;
3183
bf4d1a83 3184 /* save drap offset so we know when to restore it */
e7c0219b 3185 cfi->drap_offset = op->dest.offset;
f4f80398 3186 } else {
baa41469
JP
3187
3188 /* drap: mov reg, disp(%rbp) */
e7c0219b 3189 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
baa41469
JP
3190 }
3191
3192 } else if (op->dest.reg == cfa->base) {
3193
3194 /* mov reg, disp(%rbp) */
3195 /* mov reg, disp(%rsp) */
e7c0219b
PZ
3196 save_reg(cfi, op->src.reg, CFI_CFA,
3197 op->dest.offset - cfi->cfa.offset);
201ef5a9
JT
3198
3199 } else if (op->dest.reg == CFI_SP) {
3200
3201 /* mov reg, disp(%rsp) */
3202 save_reg(cfi, op->src.reg, CFI_CFA,
3203 op->dest.offset - cfi->stack_size);
aafeb14e
PZ
3204
3205 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
3206
3207 /* mov %rsp, (%reg); # setup a stack swizzle. */
3208 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
3209 cfi->vals[op->dest.reg].offset = cfa->offset;
baa41469
JP
3210 }
3211
3212 break;
3213
baa41469 3214 case OP_DEST_MEM:
ea24213d 3215 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
246b2c85 3216 WARN_INSN(insn, "unknown stack-related memory operation");
baa41469
JP
3217 return -1;
3218 }
3219
3220 /* pop mem */
e7c0219b 3221 cfi->stack_size -= 8;
baa41469
JP
3222 if (cfa->base == CFI_SP)
3223 cfa->offset -= 8;
3224
3225 break;
3226
3227 default:
246b2c85 3228 WARN_INSN(insn, "unknown stack-related instruction");
baa41469
JP
3229 return -1;
3230 }
3231
3232 return 0;
3233}
3234
c9c324dc
JP
3235/*
3236 * The stack layouts of alternatives instructions can sometimes diverge when
3237 * they have stack modifications. That's fine as long as the potential stack
3238 * layouts don't conflict at any given potential instruction boundary.
3239 *
3240 * Flatten the CFIs of the different alternative code streams (both original
3241 * and replacement) into a single shared CFI array which can be used to detect
3242 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
3243 */
3244static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
65ea47dc 3245{
c9c324dc
JP
3246 struct cfi_state **alt_cfi;
3247 int group_off;
65ea47dc 3248
c9c324dc
JP
3249 if (!insn->alt_group)
3250 return 0;
65ea47dc 3251
8b946cc3
PZ
3252 if (!insn->cfi) {
3253 WARN("CFI missing");
3254 return -1;
3255 }
3256
c9c324dc
JP
3257 alt_cfi = insn->alt_group->cfi;
3258 group_off = insn->offset - insn->alt_group->first_insn->offset;
65ea47dc 3259
c9c324dc 3260 if (!alt_cfi[group_off]) {
8b946cc3 3261 alt_cfi[group_off] = insn->cfi;
c9c324dc 3262 } else {
8b946cc3 3263 if (cficmp(alt_cfi[group_off], insn->cfi)) {
a706bb08
PZ
3264 struct alt_group *orig_group = insn->alt_group->orig_group ?: insn->alt_group;
3265 struct instruction *orig = orig_group->first_insn;
3266 char *where = offstr(insn->sec, insn->offset);
246b2c85 3267 WARN_INSN(orig, "stack layout conflict in alternatives: %s", where);
a706bb08 3268 free(where);
ab3852ab
PZ
3269 return -1;
3270 }
c9c324dc
JP
3271 }
3272
3273 return 0;
3274}
3275
d54dba41
PZ
3276static int handle_insn_ops(struct instruction *insn,
3277 struct instruction *next_insn,
3278 struct insn_state *state)
c9c324dc
JP
3279{
3280 struct stack_op *op;
3281
3ee88df1 3282 for (op = insn->stack_ops; op; op = op->next) {
c9c324dc 3283
d54dba41 3284 if (update_cfi_state(insn, next_insn, &state->cfi, op))
c9c324dc 3285 return 1;
ab3852ab 3286
ba08abca
PZ
3287 if (!insn->alt_group)
3288 continue;
3289
65ea47dc
JT
3290 if (op->dest.type == OP_DEST_PUSHF) {
3291 if (!state->uaccess_stack) {
3292 state->uaccess_stack = 1;
3293 } else if (state->uaccess_stack >> 31) {
246b2c85 3294 WARN_INSN(insn, "PUSHF stack exhausted");
65ea47dc
JT
3295 return 1;
3296 }
3297 state->uaccess_stack <<= 1;
3298 state->uaccess_stack |= state->uaccess;
3299 }
3300
3301 if (op->src.type == OP_SRC_POPF) {
3302 if (state->uaccess_stack) {
3303 state->uaccess = state->uaccess_stack & 1;
3304 state->uaccess_stack >>= 1;
3305 if (state->uaccess_stack == 1)
3306 state->uaccess_stack = 0;
3307 }
3308 }
3309 }
3310
3311 return 0;
3312}
3313
e7c0219b 3314static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
baa41469 3315{
8b946cc3 3316 struct cfi_state *cfi1 = insn->cfi;
baa41469
JP
3317 int i;
3318
8b946cc3
PZ
3319 if (!cfi1) {
3320 WARN("CFI missing");
3321 return false;
3322 }
3323
e7c0219b
PZ
3324 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
3325
246b2c85 3326 WARN_INSN(insn, "stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
e7c0219b
PZ
3327 cfi1->cfa.base, cfi1->cfa.offset,
3328 cfi2->cfa.base, cfi2->cfa.offset);
baa41469 3329
e7c0219b 3330 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
baa41469 3331 for (i = 0; i < CFI_NUM_REGS; i++) {
e7c0219b 3332 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
baa41469
JP
3333 sizeof(struct cfi_reg)))
3334 continue;
3335
246b2c85 3336 WARN_INSN(insn, "stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
e7c0219b
PZ
3337 i, cfi1->regs[i].base, cfi1->regs[i].offset,
3338 i, cfi2->regs[i].base, cfi2->regs[i].offset);
baa41469
JP
3339 break;
3340 }
3341
e7c0219b
PZ
3342 } else if (cfi1->type != cfi2->type) {
3343
246b2c85
JP
3344 WARN_INSN(insn, "stack state mismatch: type1=%d type2=%d",
3345 cfi1->type, cfi2->type);
e7c0219b
PZ
3346
3347 } else if (cfi1->drap != cfi2->drap ||
3348 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
3349 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
627fce14 3350
246b2c85 3351 WARN_INSN(insn, "stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
e7c0219b
PZ
3352 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
3353 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
baa41469
JP
3354
3355 } else
3356 return true;
3357
3358 return false;
dcc914f4
JP
3359}
3360
ea24213d
PZ
3361static inline bool func_uaccess_safe(struct symbol *func)
3362{
3363 if (func)
e10cd8fe 3364 return func->uaccess_safe;
ea24213d
PZ
3365
3366 return false;
3367}
3368
0c1ddd33 3369static inline const char *call_dest_name(struct instruction *insn)
ea24213d 3370{
82880283 3371 static char pvname[19];
db2b0c5d
PZ
3372 struct reloc *rel;
3373 int idx;
3374
c6f5dc28
PZ
3375 if (insn_call_dest(insn))
3376 return insn_call_dest(insn)->name;
ea24213d 3377
db2b0c5d
PZ
3378 rel = insn_reloc(NULL, insn);
3379 if (rel && !strcmp(rel->sym->name, "pv_ops")) {
3380 idx = (rel->addend / sizeof(void *));
3381 snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
3382 return pvname;
3383 }
3384
ea24213d
PZ
3385 return "{dynamic}";
3386}
3387
db2b0c5d
PZ
3388static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
3389{
3390 struct symbol *target;
3391 struct reloc *rel;
3392 int idx;
3393
3394 rel = insn_reloc(file, insn);
3395 if (!rel || strcmp(rel->sym->name, "pv_ops"))
3396 return false;
3397
3398 idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *));
3399
3400 if (file->pv_ops[idx].clean)
3401 return true;
3402
3403 file->pv_ops[idx].clean = true;
3404
3405 list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) {
3406 if (!target->sec->noinstr) {
3407 WARN("pv_ops[%d]: %s", idx, target->name);
3408 file->pv_ops[idx].clean = false;
3409 }
3410 }
3411
3412 return file->pv_ops[idx].clean;
3413}
3414
3415static inline bool noinstr_call_dest(struct objtool_file *file,
3416 struct instruction *insn,
3417 struct symbol *func)
6b643a07
PZ
3418{
3419 /*
3420 * We can't deal with indirect function calls at present;
3421 * assume they're instrumented.
3422 */
db2b0c5d
PZ
3423 if (!func) {
3424 if (file->pv_ops)
3425 return pv_call_dest(file, insn);
3426
6b643a07 3427 return false;
db2b0c5d 3428 }
6b643a07
PZ
3429
3430 /*
3431 * If the symbol is from a noinstr section; we good.
3432 */
3433 if (func->sec->noinstr)
3434 return true;
3435
2b5a0e42
PZ
3436 /*
3437 * If the symbol is a static_call trampoline, we can't tell.
3438 */
3439 if (func->static_call_tramp)
3440 return true;
3441
6b643a07
PZ
3442 /*
3443 * The __ubsan_handle_*() calls are like WARN(), they only happen when
3444 * something 'BAD' happened. At the risk of taking the machine down,
3445 * let them proceed to get the message out.
3446 */
3447 if (!strncmp(func->name, "__ubsan_handle_", 15))
3448 return true;
3449
3450 return false;
3451}
3452
db2b0c5d
PZ
3453static int validate_call(struct objtool_file *file,
3454 struct instruction *insn,
3455 struct insn_state *state)
ea24213d 3456{
c4a33939 3457 if (state->noinstr && state->instr <= 0 &&
c6f5dc28 3458 !noinstr_call_dest(file, insn, insn_call_dest(insn))) {
246b2c85 3459 WARN_INSN(insn, "call to %s() leaves .noinstr.text section", call_dest_name(insn));
c4a33939
PZ
3460 return 1;
3461 }
3462
c6f5dc28 3463 if (state->uaccess && !func_uaccess_safe(insn_call_dest(insn))) {
246b2c85 3464 WARN_INSN(insn, "call to %s() with UACCESS enabled", call_dest_name(insn));
ea24213d
PZ
3465 return 1;
3466 }
3467
2f0f9e9a 3468 if (state->df) {
246b2c85 3469 WARN_INSN(insn, "call to %s() with DF set", call_dest_name(insn));
2f0f9e9a
PZ
3470 return 1;
3471 }
3472
ea24213d
PZ
3473 return 0;
3474}
3475
db2b0c5d
PZ
3476static int validate_sibling_call(struct objtool_file *file,
3477 struct instruction *insn,
3478 struct insn_state *state)
54262aa2 3479{
5a9c361a 3480 if (insn_func(insn) && has_modified_stack_frame(insn, state)) {
246b2c85 3481 WARN_INSN(insn, "sibling call from callable instruction with modified stack frame");
54262aa2
PZ
3482 return 1;
3483 }
3484
db2b0c5d 3485 return validate_call(file, insn, state);
54262aa2
PZ
3486}
3487
a92e92d1
PZ
3488static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
3489{
c4a33939 3490 if (state->noinstr && state->instr > 0) {
246b2c85 3491 WARN_INSN(insn, "return with instrumentation enabled");
c4a33939
PZ
3492 return 1;
3493 }
3494
a92e92d1 3495 if (state->uaccess && !func_uaccess_safe(func)) {
246b2c85 3496 WARN_INSN(insn, "return with UACCESS enabled");
a92e92d1
PZ
3497 return 1;
3498 }
3499
3500 if (!state->uaccess && func_uaccess_safe(func)) {
246b2c85 3501 WARN_INSN(insn, "return with UACCESS disabled from a UACCESS-safe function");
a92e92d1
PZ
3502 return 1;
3503 }
3504
3505 if (state->df) {
246b2c85 3506 WARN_INSN(insn, "return with DF set");
a92e92d1
PZ
3507 return 1;
3508 }
3509
e25eea89 3510 if (func && has_modified_stack_frame(insn, state)) {
246b2c85 3511 WARN_INSN(insn, "return with modified stack frame");
a92e92d1
PZ
3512 return 1;
3513 }
3514
e7c0219b 3515 if (state->cfi.bp_scratch) {
246b2c85 3516 WARN_INSN(insn, "BP used as a scratch register");
a92e92d1
PZ
3517 return 1;
3518 }
3519
3520 return 0;
3521}
3522
c9c324dc
JP
3523static struct instruction *next_insn_to_validate(struct objtool_file *file,
3524 struct instruction *insn)
7117f16b 3525{
b23cc71c 3526 struct alt_group *alt_group = insn->alt_group;
7117f16b 3527
c9c324dc
JP
3528 /*
3529 * Simulate the fact that alternatives are patched in-place. When the
3530 * end of a replacement alt_group is reached, redirect objtool flow to
3531 * the end of the original alt_group.
1c34496e
PZ
3532 *
3533 * insn->alts->insn -> alt_group->first_insn
3534 * ...
3535 * alt_group->last_insn
3536 * [alt_group->nop] -> next(orig_group->last_insn)
c9c324dc 3537 */
1c34496e
PZ
3538 if (alt_group) {
3539 if (alt_group->nop) {
3540 /* ->nop implies ->orig_group */
3541 if (insn == alt_group->last_insn)
3542 return alt_group->nop;
3543 if (insn == alt_group->nop)
3544 goto next_orig;
3545 }
3546 if (insn == alt_group->last_insn && alt_group->orig_group)
3547 goto next_orig;
3548 }
c9c324dc
JP
3549
3550 return next_insn_same_sec(file, insn);
1c34496e
PZ
3551
3552next_orig:
3553 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
7117f16b
PZ
3554}
3555
dcc914f4
JP
3556/*
3557 * Follow the branch starting at the given instruction, and recursively follow
3558 * any other branches (jumps). Meanwhile, track the frame pointer state at
3559 * each instruction and validate all the rules described in
d6a21f2d 3560 * tools/objtool/Documentation/objtool.txt.
dcc914f4 3561 */
c705cecc 3562static int validate_branch(struct objtool_file *file, struct symbol *func,
b7460462 3563 struct instruction *insn, struct insn_state state)
dcc914f4
JP
3564{
3565 struct alternative *alt;
8b946cc3 3566 struct instruction *next_insn, *prev_insn = NULL;
dcc914f4 3567 struct section *sec;
882a0db9 3568 u8 visited;
dcc914f4
JP
3569 int ret;
3570
dcc914f4 3571 sec = insn->sec;
dcc914f4 3572
dcc914f4 3573 while (1) {
c9c324dc 3574 next_insn = next_insn_to_validate(file, insn);
39358a03 3575
dbcdbdfd 3576 if (func && insn_func(insn) && func != insn_func(insn)->pfunc) {
3c68a92d 3577 /* Ignore KCFI type preambles, which always fall through */
9f2899fe
PZ
3578 if (!strncmp(func->name, "__cfi_", 6) ||
3579 !strncmp(func->name, "__pfx_", 6))
3c68a92d
ST
3580 return 0;
3581
ee97638b 3582 WARN("%s() falls through to next function %s()",
dbcdbdfd 3583 func->name, insn_func(insn)->name);
ee97638b 3584 return 1;
dcc914f4
JP
3585 }
3586
4855022a 3587 if (func && insn->ignore) {
246b2c85 3588 WARN_INSN(insn, "BUG: why am I validating an ignored function?");
12b25729 3589 return 1;
4855022a
JP
3590 }
3591
a09a6e23
PZ
3592 visited = VISITED_BRANCH << state.uaccess;
3593 if (insn->visited & VISITED_BRANCH_MASK) {
e7c0219b 3594 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
dcc914f4 3595 return 1;
dcc914f4 3596
882a0db9 3597 if (insn->visited & visited)
ea24213d 3598 return 0;
8b946cc3
PZ
3599 } else {
3600 nr_insns_visited++;
dcc914f4
JP
3601 }
3602
c4a33939
PZ
3603 if (state.noinstr)
3604 state.instr += insn->instr;
3605
8b946cc3 3606 if (insn->hint) {
8faea26e
JP
3607 if (insn->restore) {
3608 struct instruction *save_insn, *i;
3609
3610 i = insn;
3611 save_insn = NULL;
3612
3613 sym_for_each_insn_continue_reverse(file, func, i) {
3614 if (i->save) {
3615 save_insn = i;
3616 break;
3617 }
3618 }
3619
3620 if (!save_insn) {
246b2c85 3621 WARN_INSN(insn, "no corresponding CFI save for CFI restore");
8faea26e
JP
3622 return 1;
3623 }
3624
3625 if (!save_insn->visited) {
246b2c85 3626 WARN_INSN(insn, "objtool isn't smart enough to handle this CFI save/restore combo");
8faea26e
JP
3627 return 1;
3628 }
3629
3630 insn->cfi = save_insn->cfi;
3631 nr_cfi_reused++;
3632 }
3633
8b946cc3
PZ
3634 state.cfi = *insn->cfi;
3635 } else {
3636 /* XXX track if we actually changed state.cfi */
3637
3638 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
3639 insn->cfi = prev_insn->cfi;
3640 nr_cfi_reused++;
3641 } else {
3642 insn->cfi = cfi_hash_find_or_add(&state.cfi);
3643 }
3644 }
dcc914f4 3645
882a0db9 3646 insn->visited |= visited;
baa41469 3647
c9c324dc
JP
3648 if (propagate_alt_cfi(file, insn))
3649 return 1;
3650
d5406654 3651 if (!insn->ignore_alts && insn->alts) {
764eef4b
PZ
3652 bool skip_orig = false;
3653
d5406654 3654 for (alt = insn->alts; alt; alt = alt->next) {
764eef4b
PZ
3655 if (alt->skip_orig)
3656 skip_orig = true;
3657
c705cecc 3658 ret = validate_branch(file, func, alt->insn, state);
7697eee3 3659 if (ret) {
ced23d2e 3660 BT_INSN(insn, "(alt)");
7697eee3
PZ
3661 return ret;
3662 }
a845c7cf 3663 }
764eef4b
PZ
3664
3665 if (skip_orig)
3666 return 0;
dcc914f4
JP
3667 }
3668
d54dba41 3669 if (handle_insn_ops(insn, next_insn, &state))
60041bcd
PZ
3670 return 1;
3671
dcc914f4
JP
3672 switch (insn->type) {
3673
dcc914f4 3674 case INSN_RETURN:
a92e92d1 3675 return validate_return(func, insn, &state);
dcc914f4
JP
3676
3677 case INSN_CALL:
ea24213d 3678 case INSN_CALL_DYNAMIC:
db2b0c5d 3679 ret = validate_call(file, insn, &state);
ea24213d
PZ
3680 if (ret)
3681 return ret;
dcc914f4 3682
72064474 3683 if (opts.stackval && func && !is_fentry_call(insn) &&
c9bab22b 3684 !has_valid_stack_frame(&state)) {
246b2c85 3685 WARN_INSN(insn, "call without frame pointer save/setup");
dcc914f4
JP
3686 return 1;
3687 }
c9bab22b 3688
0e5b613b 3689 if (insn->dead_end)
c9bab22b
JP
3690 return 0;
3691
dcc914f4
JP
3692 break;
3693
3694 case INSN_JUMP_CONDITIONAL:
3695 case INSN_JUMP_UNCONDITIONAL:
ecf11ba4 3696 if (is_sibling_call(insn)) {
db2b0c5d 3697 ret = validate_sibling_call(file, insn, &state);
dcc914f4 3698 if (ret)
54262aa2 3699 return ret;
4855022a 3700
0c1ddd33 3701 } else if (insn->jump_dest) {
c705cecc
JP
3702 ret = validate_branch(file, func,
3703 insn->jump_dest, state);
7697eee3 3704 if (ret) {
ced23d2e 3705 BT_INSN(insn, "(branch)");
7697eee3
PZ
3706 return ret;
3707 }
4855022a 3708 }
dcc914f4
JP
3709
3710 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3711 return 0;
3712
3713 break;
3714
3715 case INSN_JUMP_DYNAMIC:
b68b9907 3716 case INSN_JUMP_DYNAMIC_CONDITIONAL:
ecf11ba4 3717 if (is_sibling_call(insn)) {
db2b0c5d 3718 ret = validate_sibling_call(file, insn, &state);
54262aa2
PZ
3719 if (ret)
3720 return ret;
dcc914f4
JP
3721 }
3722
b68b9907
JP
3723 if (insn->type == INSN_JUMP_DYNAMIC)
3724 return 0;
3725
3726 break;
dcc914f4 3727
39358a03
JP
3728 case INSN_CONTEXT_SWITCH:
3729 if (func && (!next_insn || !next_insn->hint)) {
246b2c85 3730 WARN_INSN(insn, "unsupported instruction in callable function");
39358a03
JP
3731 return 1;
3732 }
3733 return 0;
3734
ea24213d
PZ
3735 case INSN_STAC:
3736 if (state.uaccess) {
246b2c85 3737 WARN_INSN(insn, "recursive UACCESS enable");
ea24213d
PZ
3738 return 1;
3739 }
3740
3741 state.uaccess = true;
3742 break;
3743
3744 case INSN_CLAC:
c705cecc 3745 if (!state.uaccess && func) {
246b2c85 3746 WARN_INSN(insn, "redundant UACCESS disable");
ea24213d
PZ
3747 return 1;
3748 }
3749
3750 if (func_uaccess_safe(func) && !state.uaccess_stack) {
246b2c85 3751 WARN_INSN(insn, "UACCESS-safe disables UACCESS");
ea24213d
PZ
3752 return 1;
3753 }
3754
3755 state.uaccess = false;
baa41469
JP
3756 break;
3757
2f0f9e9a 3758 case INSN_STD:
6f567c93 3759 if (state.df) {
246b2c85 3760 WARN_INSN(insn, "recursive STD");
6f567c93
JP
3761 return 1;
3762 }
2f0f9e9a
PZ
3763
3764 state.df = true;
3765 break;
3766
3767 case INSN_CLD:
6f567c93 3768 if (!state.df && func) {
246b2c85 3769 WARN_INSN(insn, "redundant CLD");
6f567c93
JP
3770 return 1;
3771 }
2f0f9e9a
PZ
3772
3773 state.df = false;
baa41469
JP
3774 break;
3775
dcc914f4
JP
3776 default:
3777 break;
3778 }
3779
3780 if (insn->dead_end)
3781 return 0;
3782
00d96180 3783 if (!next_insn) {
e7c0219b 3784 if (state.cfi.cfa.base == CFI_UNDEFINED)
00d96180 3785 return 0;
dcc914f4
JP
3786 WARN("%s: unexpected end of section", sec->name);
3787 return 1;
3788 }
00d96180 3789
8b946cc3 3790 prev_insn = insn;
00d96180 3791 insn = next_insn;
dcc914f4
JP
3792 }
3793
3794 return 0;
3795}
3796
1c34496e
PZ
3797static int validate_unwind_hint(struct objtool_file *file,
3798 struct instruction *insn,
3799 struct insn_state *state)
3800{
3801 if (insn->hint && !insn->visited && !insn->ignore) {
3802 int ret = validate_branch(file, insn_func(insn), insn, *state);
ced23d2e
JP
3803 if (ret)
3804 BT_INSN(insn, "<=== (hint)");
1c34496e
PZ
3805 return ret;
3806 }
3807
3808 return 0;
3809}
3810
932f8e98 3811static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
39358a03
JP
3812{
3813 struct instruction *insn;
39358a03 3814 struct insn_state state;
1c34496e 3815 int warnings = 0;
39358a03
JP
3816
3817 if (!file->hints)
3818 return 0;
3819
753da417 3820 init_insn_state(file, &state, sec);
39358a03 3821
932f8e98 3822 if (sec) {
1c34496e
PZ
3823 sec_for_each_insn(file, sec, insn)
3824 warnings += validate_unwind_hint(file, insn, &state);
932f8e98 3825 } else {
1c34496e
PZ
3826 for_each_insn(file, insn)
3827 warnings += validate_unwind_hint(file, insn, &state);
39358a03
JP
3828 }
3829
3830 return warnings;
3831}
3832
a09a6e23
PZ
3833/*
3834 * Validate rethunk entry constraint: must untrain RET before the first RET.
3835 *
4708ea14 3836 * Follow every branch (intra-function) and ensure VALIDATE_UNRET_END comes
a09a6e23
PZ
3837 * before an actual RET instruction.
3838 */
4708ea14 3839static int validate_unret(struct objtool_file *file, struct instruction *insn)
a09a6e23
PZ
3840{
3841 struct instruction *next, *dest;
3842 int ret, warnings = 0;
3843
3844 for (;;) {
3845 next = next_insn_to_validate(file, insn);
3846
4708ea14 3847 if (insn->visited & VISITED_UNRET)
a09a6e23
PZ
3848 return 0;
3849
4708ea14 3850 insn->visited |= VISITED_UNRET;
a09a6e23 3851
d5406654 3852 if (!insn->ignore_alts && insn->alts) {
a09a6e23
PZ
3853 struct alternative *alt;
3854 bool skip_orig = false;
3855
d5406654 3856 for (alt = insn->alts; alt; alt = alt->next) {
a09a6e23
PZ
3857 if (alt->skip_orig)
3858 skip_orig = true;
3859
4708ea14 3860 ret = validate_unret(file, alt->insn);
a09a6e23 3861 if (ret) {
ced23d2e 3862 BT_INSN(insn, "(alt)");
a09a6e23
PZ
3863 return ret;
3864 }
3865 }
3866
3867 if (skip_orig)
3868 return 0;
3869 }
3870
3871 switch (insn->type) {
3872
3873 case INSN_CALL_DYNAMIC:
3874 case INSN_JUMP_DYNAMIC:
3875 case INSN_JUMP_DYNAMIC_CONDITIONAL:
246b2c85 3876 WARN_INSN(insn, "early indirect call");
a09a6e23
PZ
3877 return 1;
3878
3879 case INSN_JUMP_UNCONDITIONAL:
3880 case INSN_JUMP_CONDITIONAL:
3881 if (!is_sibling_call(insn)) {
3882 if (!insn->jump_dest) {
246b2c85 3883 WARN_INSN(insn, "unresolved jump target after linking?!?");
a09a6e23
PZ
3884 return -1;
3885 }
4708ea14 3886 ret = validate_unret(file, insn->jump_dest);
a09a6e23 3887 if (ret) {
ced23d2e
JP
3888 BT_INSN(insn, "(branch%s)",
3889 insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : "");
a09a6e23
PZ
3890 return ret;
3891 }
3892
3893 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3894 return 0;
3895
3896 break;
3897 }
3898
3899 /* fallthrough */
3900 case INSN_CALL:
c6f5dc28
PZ
3901 dest = find_insn(file, insn_call_dest(insn)->sec,
3902 insn_call_dest(insn)->offset);
a09a6e23
PZ
3903 if (!dest) {
3904 WARN("Unresolved function after linking!?: %s",
c6f5dc28 3905 insn_call_dest(insn)->name);
a09a6e23
PZ
3906 return -1;
3907 }
3908
4708ea14 3909 ret = validate_unret(file, dest);
a09a6e23 3910 if (ret) {
ced23d2e 3911 BT_INSN(insn, "(call)");
a09a6e23
PZ
3912 return ret;
3913 }
3914 /*
3915 * If a call returns without error, it must have seen UNTRAIN_RET.
3916 * Therefore any non-error return is a success.
3917 */
3918 return 0;
3919
3920 case INSN_RETURN:
246b2c85 3921 WARN_INSN(insn, "RET before UNTRAIN");
a09a6e23
PZ
3922 return 1;
3923
3924 case INSN_NOP:
3925 if (insn->retpoline_safe)
3926 return 0;
3927 break;
3928
3929 default:
3930 break;
3931 }
3932
3933 if (!next) {
246b2c85 3934 WARN_INSN(insn, "teh end!");
a09a6e23
PZ
3935 return -1;
3936 }
3937 insn = next;
3938 }
3939
3940 return warnings;
3941}
3942
3943/*
4708ea14
JP
3944 * Validate that all branches starting at VALIDATE_UNRET_BEGIN encounter
3945 * VALIDATE_UNRET_END before RET.
a09a6e23 3946 */
4708ea14 3947static int validate_unrets(struct objtool_file *file)
a09a6e23
PZ
3948{
3949 struct instruction *insn;
3950 int ret, warnings = 0;
3951
3952 for_each_insn(file, insn) {
4708ea14 3953 if (!insn->unret)
a09a6e23
PZ
3954 continue;
3955
4708ea14 3956 ret = validate_unret(file, insn);
a09a6e23 3957 if (ret < 0) {
246b2c85 3958 WARN_INSN(insn, "Failed UNRET validation");
a09a6e23
PZ
3959 return ret;
3960 }
3961 warnings += ret;
3962 }
3963
3964 return warnings;
3965}
3966
b5bc2231
PZ
3967static int validate_retpoline(struct objtool_file *file)
3968{
3969 struct instruction *insn;
3970 int warnings = 0;
3971
3972 for_each_insn(file, insn) {
3973 if (insn->type != INSN_JUMP_DYNAMIC &&
9bb2ec60
PZ
3974 insn->type != INSN_CALL_DYNAMIC &&
3975 insn->type != INSN_RETURN)
b5bc2231
PZ
3976 continue;
3977
3978 if (insn->retpoline_safe)
3979 continue;
3980
6644ee84 3981 if (insn->sec->init)
ca41b97e
PZ
3982 continue;
3983
9bb2ec60 3984 if (insn->type == INSN_RETURN) {
f43b9876 3985 if (opts.rethunk) {
246b2c85 3986 WARN_INSN(insn, "'naked' return found in RETHUNK build");
f43b9876
PZ
3987 } else
3988 continue;
9bb2ec60 3989 } else {
246b2c85 3990 WARN_INSN(insn, "indirect %s found in RETPOLINE build",
9bb2ec60
PZ
3991 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3992 }
b5bc2231
PZ
3993
3994 warnings++;
3995 }
3996
3997 return warnings;
3998}
3999
dcc914f4
JP
4000static bool is_kasan_insn(struct instruction *insn)
4001{
4002 return (insn->type == INSN_CALL &&
c6f5dc28 4003 !strcmp(insn_call_dest(insn)->name, "__asan_handle_no_return"));
dcc914f4
JP
4004}
4005
4006static bool is_ubsan_insn(struct instruction *insn)
4007{
4008 return (insn->type == INSN_CALL &&
c6f5dc28 4009 !strcmp(insn_call_dest(insn)->name,
dcc914f4
JP
4010 "__ubsan_handle_builtin_unreachable"));
4011}
4012
14db1f0a 4013static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
dcc914f4
JP
4014{
4015 int i;
14db1f0a 4016 struct instruction *prev_insn;
dcc914f4 4017
1ffbe4e9 4018 if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP)
baa41469
JP
4019 return true;
4020
4021 /*
82a8954a 4022 * Ignore alternative replacement instructions. This can happen
0e2bb2bc 4023 * when a whitelisted function uses one of the ALTERNATIVE macros.
baa41469 4024 */
82a8954a 4025 if (!strcmp(insn->sec->name, ".altinstr_replacement") ||
0e2bb2bc 4026 !strcmp(insn->sec->name, ".altinstr_aux"))
dcc914f4
JP
4027 return true;
4028
4adb2368 4029 /*
753da417 4030 * Whole archive runs might encounter dead code from weak symbols.
4adb2368
PZ
4031 * This is where the linker will have dropped the weak symbol in
4032 * favour of a regular symbol, but leaves the code in place.
4033 *
4034 * In this case we'll find a piece of code (whole function) that is not
4035 * covered by a !section symbol. Ignore them.
4036 */
dbcdbdfd 4037 if (opts.link && !insn_func(insn)) {
4adb2368
PZ
4038 int size = find_symbol_hole_containing(insn->sec, insn->offset);
4039 unsigned long end = insn->offset + size;
4040
4041 if (!size) /* not a hole */
4042 return false;
4043
4044 if (size < 0) /* hole until the end */
4045 return true;
4046
4047 sec_for_each_insn_continue(file, insn) {
4048 /*
4049 * If we reach a visited instruction at or before the
4050 * end of the hole, ignore the unreachable.
4051 */
4052 if (insn->visited)
4053 return true;
4054
4055 if (insn->offset >= end)
4056 break;
4057
4058 /*
4059 * If this hole jumps to a .cold function, mark it ignore too.
4060 */
dbcdbdfd
PZ
4061 if (insn->jump_dest && insn_func(insn->jump_dest) &&
4062 strstr(insn_func(insn->jump_dest)->name, ".cold")) {
4adb2368 4063 struct instruction *dest = insn->jump_dest;
dbcdbdfd 4064 func_for_each_insn(file, insn_func(dest), dest)
4adb2368
PZ
4065 dest->ignore = true;
4066 }
4067 }
4068
4069 return false;
4070 }
4071
dbcdbdfd 4072 if (!insn_func(insn))
bd841d61
JP
4073 return false;
4074
dbcdbdfd 4075 if (insn_func(insn)->static_call_tramp)
2105a927
PZ
4076 return true;
4077
bd841d61
JP
4078 /*
4079 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
4080 * __builtin_unreachable(). The BUG() macro has an unreachable() after
4081 * the UD2, which causes GCC's undefined trap logic to emit another UD2
4082 * (or occasionally a JMP to UD2).
14db1f0a
IH
4083 *
4084 * It may also insert a UD2 after calling a __noreturn function.
bd841d61 4085 */
1c34496e 4086 prev_insn = prev_insn_same_sec(file, insn);
6126ed5d 4087 if (prev_insn->dead_end &&
bd841d61
JP
4088 (insn->type == INSN_BUG ||
4089 (insn->type == INSN_JUMP_UNCONDITIONAL &&
4090 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
4091 return true;
4092
dcc914f4
JP
4093 /*
4094 * Check if this (or a subsequent) instruction is related to
4095 * CONFIG_UBSAN or CONFIG_KASAN.
4096 *
4097 * End the search at 5 instructions to avoid going into the weeds.
4098 */
4099 for (i = 0; i < 5; i++) {
4100
4101 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
4102 return true;
4103
fe24e271
JP
4104 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
4105 if (insn->jump_dest &&
dbcdbdfd 4106 insn_func(insn->jump_dest) == insn_func(insn)) {
fe24e271
JP
4107 insn = insn->jump_dest;
4108 continue;
4109 }
4110
4111 break;
dcc914f4
JP
4112 }
4113
dbcdbdfd 4114 if (insn->offset + insn->len >= insn_func(insn)->offset + insn_func(insn)->len)
dcc914f4 4115 break;
fe24e271 4116
1c34496e 4117 insn = next_insn_same_sec(file, insn);
dcc914f4
JP
4118 }
4119
4120 return false;
4121}
4122
bd456a1b 4123static int add_prefix_symbol(struct objtool_file *file, struct symbol *func)
9f2899fe 4124{
bd456a1b 4125 struct instruction *insn, *prev;
5743654f 4126 struct cfi_state *cfi;
9f2899fe 4127
bd456a1b
JP
4128 insn = find_insn(file, func->sec, func->offset);
4129 if (!insn)
4130 return -1;
9f2899fe 4131
bd456a1b
JP
4132 for (prev = prev_insn_same_sec(file, insn);
4133 prev;
4134 prev = prev_insn_same_sec(file, prev)) {
4135 u64 offset;
9f2899fe
PZ
4136
4137 if (prev->type != INSN_NOP)
bd456a1b 4138 return -1;
9f2899fe
PZ
4139
4140 offset = func->offset - prev->offset;
bd456a1b
JP
4141
4142 if (offset > opts.prefix)
4143 return -1;
4144
4145 if (offset < opts.prefix)
4146 continue;
4147
4148 elf_create_prefix_symbol(file->elf, func, opts.prefix);
4149 break;
9f2899fe
PZ
4150 }
4151
bd456a1b
JP
4152 if (!prev)
4153 return -1;
4154
5743654f
JP
4155 if (!insn->cfi) {
4156 /*
4157 * This can happen if stack validation isn't enabled or the
4158 * function is annotated with STACK_FRAME_NON_STANDARD.
4159 */
4160 return 0;
4161 }
4162
4163 /* Propagate insn->cfi to the prefix code */
4164 cfi = cfi_hash_find_or_add(insn->cfi);
4165 for (; prev != insn; prev = next_insn_same_sec(file, prev))
4166 prev->cfi = cfi;
4167
9f2899fe
PZ
4168 return 0;
4169}
4170
bd456a1b
JP
4171static int add_prefix_symbols(struct objtool_file *file)
4172{
4173 struct section *sec;
4174 struct symbol *func;
4175 int warnings = 0;
4176
4177 for_each_sec(file, sec) {
4178 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
4179 continue;
4180
4181 sec_for_each_sym(sec, func) {
4182 if (func->type != STT_FUNC)
4183 continue;
4184
4185 add_prefix_symbol(file, func);
4186 }
4187 }
4188
4189 return warnings;
4190}
4191
4b5e2e7f
PZ
4192static int validate_symbol(struct objtool_file *file, struct section *sec,
4193 struct symbol *sym, struct insn_state *state)
dcc914f4 4194{
dcc914f4 4195 struct instruction *insn;
4b5e2e7f
PZ
4196 int ret;
4197
4198 if (!sym->len) {
4199 WARN("%s() is missing an ELF size annotation", sym->name);
4200 return 1;
4201 }
4202
4203 if (sym->pfunc != sym || sym->alias != sym)
4204 return 0;
4205
4206 insn = find_insn(file, sec, sym->offset);
4207 if (!insn || insn->ignore || insn->visited)
4208 return 0;
4209
4210 state->uaccess = sym->uaccess_safe;
4211
dbcdbdfd 4212 ret = validate_branch(file, insn_func(insn), insn, *state);
ced23d2e
JP
4213 if (ret)
4214 BT_INSN(insn, "<=== (sym)");
4b5e2e7f
PZ
4215 return ret;
4216}
4217
4218static int validate_section(struct objtool_file *file, struct section *sec)
4219{
baa41469 4220 struct insn_state state;
4b5e2e7f
PZ
4221 struct symbol *func;
4222 int warnings = 0;
dcc914f4 4223
9290e772 4224 sec_for_each_sym(sec, func) {
350994bf
PZ
4225 if (func->type != STT_FUNC)
4226 continue;
e10cd8fe 4227
753da417 4228 init_insn_state(file, &state, sec);
b735bd3e 4229 set_func_state(&state.cfi);
0699e551 4230
4b5e2e7f 4231 warnings += validate_symbol(file, sec, func, &state);
dcc914f4
JP
4232 }
4233
dcc914f4
JP
4234 return warnings;
4235}
4236
753da417 4237static int validate_noinstr_sections(struct objtool_file *file)
c4a33939
PZ
4238{
4239 struct section *sec;
932f8e98 4240 int warnings = 0;
c4a33939
PZ
4241
4242 sec = find_section_by_name(file->elf, ".noinstr.text");
0cc9ac8d
TG
4243 if (sec) {
4244 warnings += validate_section(file, sec);
4245 warnings += validate_unwind_hints(file, sec);
4246 }
c4a33939 4247
0cc9ac8d
TG
4248 sec = find_section_by_name(file->elf, ".entry.text");
4249 if (sec) {
4250 warnings += validate_section(file, sec);
4251 warnings += validate_unwind_hints(file, sec);
4252 }
932f8e98 4253
2b5a0e42
PZ
4254 sec = find_section_by_name(file->elf, ".cpuidle.text");
4255 if (sec) {
4256 warnings += validate_section(file, sec);
4257 warnings += validate_unwind_hints(file, sec);
4258 }
4259
932f8e98 4260 return warnings;
c4a33939
PZ
4261}
4262
350994bf
PZ
4263static int validate_functions(struct objtool_file *file)
4264{
4265 struct section *sec;
4266 int warnings = 0;
4267
da837bd6
PZ
4268 for_each_sec(file, sec) {
4269 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
4270 continue;
4271
350994bf 4272 warnings += validate_section(file, sec);
da837bd6 4273 }
350994bf
PZ
4274
4275 return warnings;
4276}
4277
3c6f9f77 4278static void mark_endbr_used(struct instruction *insn)
08f87a93 4279{
3c6f9f77
JP
4280 if (!list_empty(&insn->call_node))
4281 list_del_init(&insn->call_node);
4282}
4283
08ef8c40
PZ
4284static bool noendbr_range(struct objtool_file *file, struct instruction *insn)
4285{
4286 struct symbol *sym = find_symbol_containing(insn->sec, insn->offset-1);
4287 struct instruction *first;
4288
4289 if (!sym)
4290 return false;
4291
4292 first = find_insn(file, sym->sec, sym->offset);
4293 if (!first)
4294 return false;
4295
4296 if (first->type != INSN_ENDBR && !first->noendbr)
4297 return false;
4298
4299 return insn->offset == sym->offset + sym->len;
4300}
4301
3c6f9f77
JP
4302static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn)
4303{
4304 struct instruction *dest;
08f87a93 4305 struct reloc *reloc;
3c6f9f77
JP
4306 unsigned long off;
4307 int warnings = 0;
08f87a93 4308
3c6f9f77
JP
4309 /*
4310 * Looking for function pointer load relocations. Ignore
4311 * direct/indirect branches:
4312 */
4313 switch (insn->type) {
4314 case INSN_CALL:
4315 case INSN_CALL_DYNAMIC:
4316 case INSN_JUMP_CONDITIONAL:
4317 case INSN_JUMP_UNCONDITIONAL:
4318 case INSN_JUMP_DYNAMIC:
4319 case INSN_JUMP_DYNAMIC_CONDITIONAL:
4320 case INSN_RETURN:
4321 case INSN_NOP:
4322 return 0;
4323 default:
4324 break;
4325 }
08f87a93 4326
3c6f9f77
JP
4327 for (reloc = insn_reloc(file, insn);
4328 reloc;
4329 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
4330 reloc->offset + 1,
4331 (insn->offset + insn->len) - (reloc->offset + 1))) {
08f87a93 4332
3c6f9f77
JP
4333 /*
4334 * static_call_update() references the trampoline, which
4335 * doesn't have (or need) ENDBR. Skip warning in that case.
4336 */
4337 if (reloc->sym->static_call_tramp)
08f87a93
PZ
4338 continue;
4339
3c6f9f77
JP
4340 off = reloc->sym->offset;
4341 if (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32)
4342 off += arch_dest_reloc_offset(reloc->addend);
4343 else
4344 off += reloc->addend;
4345
4346 dest = find_insn(file, reloc->sym->sec, off);
4347 if (!dest)
08f87a93
PZ
4348 continue;
4349
3c6f9f77
JP
4350 if (dest->type == INSN_ENDBR) {
4351 mark_endbr_used(dest);
08f87a93 4352 continue;
3c6f9f77 4353 }
08f87a93 4354
dbcdbdfd 4355 if (insn_func(dest) && insn_func(dest) == insn_func(insn)) {
3c6f9f77
JP
4356 /*
4357 * Anything from->to self is either _THIS_IP_ or
4358 * IRET-to-self.
4359 *
4360 * There is no sane way to annotate _THIS_IP_ since the
4361 * compiler treats the relocation as a constant and is
4362 * happy to fold in offsets, skewing any annotation we
4363 * do, leading to vast amounts of false-positives.
4364 *
4365 * There's also compiler generated _THIS_IP_ through
4366 * KCOV and such which we have no hope of annotating.
4367 *
4368 * As such, blanket accept self-references without
4369 * issue.
4370 */
08f87a93 4371 continue;
3c6f9f77 4372 }
08f87a93 4373
08ef8c40
PZ
4374 /*
4375 * Accept anything ANNOTATE_NOENDBR.
4376 */
3c6f9f77 4377 if (dest->noendbr)
08f87a93
PZ
4378 continue;
4379
08ef8c40
PZ
4380 /*
4381 * Accept if this is the instruction after a symbol
4382 * that is (no)endbr -- typical code-range usage.
4383 */
4384 if (noendbr_range(file, dest))
4385 continue;
4386
246b2c85 4387 WARN_INSN(insn, "relocation to !ENDBR: %s", offstr(dest->sec, dest->offset));
3c6f9f77
JP
4388
4389 warnings++;
4390 }
4391
4392 return warnings;
4393}
4394
4395static int validate_ibt_data_reloc(struct objtool_file *file,
4396 struct reloc *reloc)
4397{
4398 struct instruction *dest;
4399
4400 dest = find_insn(file, reloc->sym->sec,
4401 reloc->sym->offset + reloc->addend);
4402 if (!dest)
4403 return 0;
4404
4405 if (dest->type == INSN_ENDBR) {
4406 mark_endbr_used(dest);
4407 return 0;
4408 }
4409
4410 if (dest->noendbr)
4411 return 0;
4412
4413 WARN_FUNC("data relocation to !ENDBR: %s",
4414 reloc->sec->base, reloc->offset,
4415 offstr(dest->sec, dest->offset));
4416
4417 return 1;
4418}
4419
4420/*
4421 * Validate IBT rules and remove used ENDBR instructions from the seal list.
4422 * Unused ENDBR instructions will be annotated for sealing (i.e., replaced with
4423 * NOPs) later, in create_ibt_endbr_seal_sections().
4424 */
4425static int validate_ibt(struct objtool_file *file)
4426{
4427 struct section *sec;
4428 struct reloc *reloc;
4429 struct instruction *insn;
4430 int warnings = 0;
4431
4432 for_each_insn(file, insn)
4433 warnings += validate_ibt_insn(file, insn);
4434
4435 for_each_sec(file, sec) {
4436
4437 /* Already done by validate_ibt_insn() */
4438 if (sec->sh.sh_flags & SHF_EXECINSTR)
08f87a93
PZ
4439 continue;
4440
3c6f9f77
JP
4441 if (!sec->reloc)
4442 continue;
08f87a93 4443
3c6f9f77
JP
4444 /*
4445 * These sections can reference text addresses, but not with
4446 * the intent to indirect branch to them.
4447 */
e27e5bea
JP
4448 if ((!strncmp(sec->name, ".discard", 8) &&
4449 strcmp(sec->name, ".discard.ibt_endbr_noseal")) ||
3c6f9f77
JP
4450 !strncmp(sec->name, ".debug", 6) ||
4451 !strcmp(sec->name, ".altinstructions") ||
4452 !strcmp(sec->name, ".ibt_endbr_seal") ||
4453 !strcmp(sec->name, ".orc_unwind_ip") ||
4454 !strcmp(sec->name, ".parainstructions") ||
4455 !strcmp(sec->name, ".retpoline_sites") ||
4456 !strcmp(sec->name, ".smp_locks") ||
4457 !strcmp(sec->name, ".static_call_sites") ||
4458 !strcmp(sec->name, "_error_injection_whitelist") ||
4459 !strcmp(sec->name, "_kprobe_blacklist") ||
4460 !strcmp(sec->name, "__bug_table") ||
4461 !strcmp(sec->name, "__ex_table") ||
4462 !strcmp(sec->name, "__jump_table") ||
3c68a92d 4463 !strcmp(sec->name, "__mcount_loc") ||
0326074f 4464 !strcmp(sec->name, ".kcfi_traps") ||
9440155c 4465 strstr(sec->name, "__patchable_function_entries"))
3c6f9f77 4466 continue;
08f87a93 4467
3c6f9f77
JP
4468 list_for_each_entry(reloc, &sec->reloc->reloc_list, list)
4469 warnings += validate_ibt_data_reloc(file, reloc);
08f87a93
PZ
4470 }
4471
3c6f9f77 4472 return warnings;
08f87a93
PZ
4473}
4474
c2bdd61c
JP
4475static int validate_sls(struct objtool_file *file)
4476{
4477 struct instruction *insn, *next_insn;
4478 int warnings = 0;
4479
4480 for_each_insn(file, insn) {
4481 next_insn = next_insn_same_sec(file, insn);
4482
4483 if (insn->retpoline_safe)
4484 continue;
4485
4486 switch (insn->type) {
4487 case INSN_RETURN:
4488 if (!next_insn || next_insn->type != INSN_TRAP) {
246b2c85 4489 WARN_INSN(insn, "missing int3 after ret");
c2bdd61c
JP
4490 warnings++;
4491 }
4492
4493 break;
4494 case INSN_JUMP_DYNAMIC:
4495 if (!next_insn || next_insn->type != INSN_TRAP) {
246b2c85 4496 WARN_INSN(insn, "missing int3 after indirect jump");
c2bdd61c
JP
4497 warnings++;
4498 }
4499 break;
4500 default:
4501 break;
4502 }
4503 }
4504
4505 return warnings;
4506}
4507
55eeab2a
JP
4508static bool ignore_noreturn_call(struct instruction *insn)
4509{
4510 struct symbol *call_dest = insn_call_dest(insn);
4511
4512 /*
4513 * FIXME: hack, we need a real noreturn solution
4514 *
4515 * Problem is, exc_double_fault() may or may not return, depending on
4516 * whether CONFIG_X86_ESPFIX64 is set. But objtool has no visibility
4517 * to the kernel config.
4518 *
4519 * Other potential ways to fix it:
4520 *
4521 * - have compiler communicate __noreturn functions somehow
4522 * - remove CONFIG_X86_ESPFIX64
4523 * - read the .config file
4524 * - add a cmdline option
4525 * - create a generic objtool annotation format (vs a bunch of custom
4526 * formats) and annotate it
4527 */
4528 if (!strcmp(call_dest->name, "exc_double_fault")) {
4529 /* prevent further unreachable warnings for the caller */
4530 insn->sym->warned = 1;
4531 return true;
4532 }
4533
4534 return false;
4535}
4536
baa41469 4537static int validate_reachable_instructions(struct objtool_file *file)
dcc914f4 4538{
fedb724c
JP
4539 struct instruction *insn, *prev_insn;
4540 struct symbol *call_dest;
5e3992fe 4541 int warnings = 0;
baa41469
JP
4542
4543 if (file->ignore_unreachables)
4544 return 0;
dcc914f4
JP
4545
4546 for_each_insn(file, insn) {
14db1f0a 4547 if (insn->visited || ignore_unreachable_insn(file, insn))
baa41469
JP
4548 continue;
4549
fedb724c
JP
4550 prev_insn = prev_insn_same_sec(file, insn);
4551 if (prev_insn && prev_insn->dead_end) {
4552 call_dest = insn_call_dest(prev_insn);
55eeab2a 4553 if (call_dest && !ignore_noreturn_call(prev_insn)) {
fedb724c
JP
4554 WARN_INSN(insn, "%s() is missing a __noreturn annotation",
4555 call_dest->name);
4556 warnings++;
4557 continue;
4558 }
4559 }
4560
246b2c85 4561 WARN_INSN(insn, "unreachable instruction");
5e3992fe 4562 warnings++;
dcc914f4
JP
4563 }
4564
5e3992fe 4565 return warnings;
dcc914f4
JP
4566}
4567
ca653464
JP
4568/* 'funcs' is a space-separated list of function names */
4569static int disas_funcs(const char *funcs)
4570{
4571 const char *objdump_str, *cross_compile;
4572 int size, ret;
4573 char *cmd;
4574
4575 cross_compile = getenv("CROSS_COMPILE");
4576
4577 objdump_str = "%sobjdump -wdr %s | gawk -M -v _funcs='%s' '"
4578 "BEGIN { split(_funcs, funcs); }"
4579 "/^$/ { func_match = 0; }"
4580 "/<.*>:/ { "
4581 "f = gensub(/.*<(.*)>:/, \"\\\\1\", 1);"
4582 "for (i in funcs) {"
4583 "if (funcs[i] == f) {"
4584 "func_match = 1;"
4585 "base = strtonum(\"0x\" $1);"
4586 "break;"
4587 "}"
4588 "}"
4589 "}"
4590 "{"
4591 "if (func_match) {"
4592 "addr = strtonum(\"0x\" $1);"
4593 "printf(\"%%04x \", addr - base);"
4594 "print;"
4595 "}"
4596 "}' 1>&2";
4597
4598 /* fake snprintf() to calculate the size */
4599 size = snprintf(NULL, 0, objdump_str, cross_compile, objname, funcs) + 1;
4600 if (size <= 0) {
4601 WARN("objdump string size calculation failed");
4602 return -1;
4603 }
4604
4605 cmd = malloc(size);
4606
4607 /* real snprintf() */
4608 snprintf(cmd, size, objdump_str, cross_compile, objname, funcs);
4609 ret = system(cmd);
4610 if (ret) {
4611 WARN("disassembly failed: %d", ret);
4612 return -1;
4613 }
4614
4615 return 0;
4616}
4617
4618static int disas_warned_funcs(struct objtool_file *file)
4619{
4620 struct symbol *sym;
4621 char *funcs = NULL, *tmp;
4622
4623 for_each_sym(file, sym) {
4624 if (sym->warned) {
4625 if (!funcs) {
4626 funcs = malloc(strlen(sym->name) + 1);
4627 strcpy(funcs, sym->name);
4628 } else {
4629 tmp = malloc(strlen(funcs) + strlen(sym->name) + 2);
4630 sprintf(tmp, "%s %s", funcs, sym->name);
4631 free(funcs);
4632 funcs = tmp;
4633 }
4634 }
4635 }
4636
4637 if (funcs)
4638 disas_funcs(funcs);
4639
4640 return 0;
4641}
4642
d44becb9 4643int check(struct objtool_file *file)
dcc914f4 4644{
dcc914f4
JP
4645 int ret, warnings = 0;
4646
baa41469 4647 arch_initial_func_cfi_state(&initial_func_cfi);
8b946cc3
PZ
4648 init_cfi_state(&init_cfi);
4649 init_cfi_state(&func_cfi);
4650 set_func_state(&func_cfi);
4651
4652 if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
4653 goto out;
4654
4655 cfi_hash_add(&init_cfi);
4656 cfi_hash_add(&func_cfi);
baa41469 4657
6545eb03 4658 ret = decode_sections(file);
dcc914f4
JP
4659 if (ret < 0)
4660 goto out;
8b946cc3 4661
dcc914f4
JP
4662 warnings += ret;
4663
1c34496e 4664 if (!nr_insns)
dcc914f4 4665 goto out;
dcc914f4 4666
2daf7fab 4667 if (opts.retpoline) {
6545eb03 4668 ret = validate_retpoline(file);
b5bc2231
PZ
4669 if (ret < 0)
4670 return ret;
4671 warnings += ret;
4672 }
4673
c2bdd61c 4674 if (opts.stackval || opts.orc || opts.uaccess) {
7dce6204
JP
4675 ret = validate_functions(file);
4676 if (ret < 0)
4677 goto out;
4678 warnings += ret;
39358a03 4679
7dce6204 4680 ret = validate_unwind_hints(file, NULL);
08f87a93
PZ
4681 if (ret < 0)
4682 goto out;
4683 warnings += ret;
7dce6204
JP
4684
4685 if (!warnings) {
4686 ret = validate_reachable_instructions(file);
4687 if (ret < 0)
4688 goto out;
4689 warnings += ret;
4690 }
753da417
JP
4691
4692 } else if (opts.noinstr) {
4693 ret = validate_noinstr_sections(file);
4694 if (ret < 0)
4695 goto out;
4696 warnings += ret;
08f87a93
PZ
4697 }
4698
a09a6e23
PZ
4699 if (opts.unret) {
4700 /*
4701 * Must be after validate_branch() and friends, it plays
4702 * further games with insn->visited.
4703 */
4708ea14 4704 ret = validate_unrets(file);
a09a6e23
PZ
4705 if (ret < 0)
4706 return ret;
4707 warnings += ret;
4708 }
4709
7dce6204
JP
4710 if (opts.ibt) {
4711 ret = validate_ibt(file);
baa41469
JP
4712 if (ret < 0)
4713 goto out;
c2bdd61c
JP
4714 warnings += ret;
4715 }
4716
4717 if (opts.sls) {
4718 ret = validate_sls(file);
4719 if (ret < 0)
4720 goto out;
baa41469
JP
4721 warnings += ret;
4722 }
4723
26e17689
JP
4724 if (opts.static_call) {
4725 ret = create_static_call_sections(file);
4726 if (ret < 0)
4727 goto out;
4728 warnings += ret;
4729 }
1e7e4788 4730
2daf7fab 4731 if (opts.retpoline) {
134ab5bd
PZ
4732 ret = create_retpoline_sites_sections(file);
4733 if (ret < 0)
4734 goto out;
4735 warnings += ret;
4736 }
4737
9a479f76
PZ
4738 if (opts.cfi) {
4739 ret = create_cfi_sections(file);
4740 if (ret < 0)
4741 goto out;
4742 warnings += ret;
4743 }
4744
f43b9876 4745 if (opts.rethunk) {
d9e9d230
PZ
4746 ret = create_return_sites_sections(file);
4747 if (ret < 0)
4748 goto out;
4749 warnings += ret;
00abd384 4750
0c0a6d89
PZ
4751 if (opts.hack_skylake) {
4752 ret = create_direct_call_sections(file);
4753 if (ret < 0)
4754 goto out;
4755 warnings += ret;
4756 }
134ab5bd
PZ
4757 }
4758
2daf7fab 4759 if (opts.mcount) {
99d00215
PZ
4760 ret = create_mcount_loc_sections(file);
4761 if (ret < 0)
4762 goto out;
4763 warnings += ret;
4764 }
4765
bd456a1b
JP
4766 if (opts.prefix) {
4767 ret = add_prefix_symbols(file);
4768 if (ret < 0)
4769 return ret;
4770 warnings += ret;
4771 }
4772
2daf7fab 4773 if (opts.ibt) {
89bc853e
PZ
4774 ret = create_ibt_endbr_seal_sections(file);
4775 if (ret < 0)
4776 goto out;
4777 warnings += ret;
4778 }
4779
1c34496e 4780 if (opts.orc && nr_insns) {
b51277eb
JP
4781 ret = orc_create(file);
4782 if (ret < 0)
4783 goto out;
4784 warnings += ret;
4785 }
4786
ca653464
JP
4787 if (opts.verbose)
4788 disas_warned_funcs(file);
b51277eb 4789
2daf7fab 4790 if (opts.stats) {
8b946cc3
PZ
4791 printf("nr_insns_visited: %ld\n", nr_insns_visited);
4792 printf("nr_cfi: %ld\n", nr_cfi);
4793 printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
4794 printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
4795 }
4796
dcc914f4 4797out:
655cf865
JP
4798 /*
4799 * For now, don't fail the kernel build on fatal warnings. These
4800 * errors are still fairly common due to the growing matrix of
4801 * supported toolchains and their recent pace of change.
4802 */
dcc914f4
JP
4803 return 0;
4804}