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