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