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