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