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