9122c20f5298d076e3c6fa2a6d4d5fdf6353dcfc
[platform/kernel/linux-starfive.git] / tools / objtool / check.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
4  */
5
6 #include <string.h>
7 #include <stdlib.h>
8
9 #include "builtin.h"
10 #include "check.h"
11 #include "elf.h"
12 #include "special.h"
13 #include "arch.h"
14 #include "warn.h"
15
16 #include <linux/hashtable.h>
17 #include <linux/kernel.h>
18
19 #define FAKE_JUMP_OFFSET -1
20
21 #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table"
22
23 struct alternative {
24         struct list_head list;
25         struct instruction *insn;
26         bool skip_orig;
27 };
28
29 const char *objname;
30 struct cfi_state initial_func_cfi;
31
32 struct instruction *find_insn(struct objtool_file *file,
33                               struct section *sec, unsigned long offset)
34 {
35         struct instruction *insn;
36
37         hash_for_each_possible(file->insn_hash, insn, hash, offset)
38                 if (insn->sec == sec && insn->offset == offset)
39                         return insn;
40
41         return NULL;
42 }
43
44 static struct instruction *next_insn_same_sec(struct objtool_file *file,
45                                               struct instruction *insn)
46 {
47         struct instruction *next = list_next_entry(insn, list);
48
49         if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
50                 return NULL;
51
52         return next;
53 }
54
55 static struct instruction *next_insn_same_func(struct objtool_file *file,
56                                                struct instruction *insn)
57 {
58         struct instruction *next = list_next_entry(insn, list);
59         struct symbol *func = insn->func;
60
61         if (!func)
62                 return NULL;
63
64         if (&next->list != &file->insn_list && next->func == func)
65                 return next;
66
67         /* Check if we're already in the subfunction: */
68         if (func == func->cfunc)
69                 return NULL;
70
71         /* Move to the subfunction: */
72         return find_insn(file, func->cfunc->sec, func->cfunc->offset);
73 }
74
75 #define func_for_each_insn_all(file, func, insn)                        \
76         for (insn = find_insn(file, func->sec, func->offset);           \
77              insn;                                                      \
78              insn = next_insn_same_func(file, insn))
79
80 #define func_for_each_insn(file, func, insn)                            \
81         for (insn = find_insn(file, func->sec, func->offset);           \
82              insn && &insn->list != &file->insn_list &&                 \
83                 insn->sec == func->sec &&                               \
84                 insn->offset < func->offset + func->len;                \
85              insn = list_next_entry(insn, list))
86
87 #define func_for_each_insn_continue_reverse(file, func, insn)           \
88         for (insn = list_prev_entry(insn, list);                        \
89              &insn->list != &file->insn_list &&                         \
90                 insn->sec == func->sec && insn->offset >= func->offset; \
91              insn = list_prev_entry(insn, list))
92
93 #define sec_for_each_insn_from(file, insn)                              \
94         for (; insn; insn = next_insn_same_sec(file, insn))
95
96 #define sec_for_each_insn_continue(file, insn)                          \
97         for (insn = next_insn_same_sec(file, insn); insn;               \
98              insn = next_insn_same_sec(file, insn))
99
100 static bool is_sibling_call(struct instruction *insn)
101 {
102         /* An indirect jump is either a sibling call or a jump to a table. */
103         if (insn->type == INSN_JUMP_DYNAMIC)
104                 return list_empty(&insn->alts);
105
106         if (insn->type != INSN_JUMP_CONDITIONAL &&
107             insn->type != INSN_JUMP_UNCONDITIONAL)
108                 return false;
109
110         /* add_jump_destinations() sets insn->call_dest for sibling calls. */
111         return !!insn->call_dest;
112 }
113
114 /*
115  * This checks to see if the given function is a "noreturn" function.
116  *
117  * For global functions which are outside the scope of this object file, we
118  * have to keep a manual list of them.
119  *
120  * For local functions, we have to detect them manually by simply looking for
121  * the lack of a return instruction.
122  */
123 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
124                                 int recursion)
125 {
126         int i;
127         struct instruction *insn;
128         bool empty = true;
129
130         /*
131          * Unfortunately these have to be hard coded because the noreturn
132          * attribute isn't provided in ELF data.
133          */
134         static const char * const global_noreturns[] = {
135                 "__stack_chk_fail",
136                 "panic",
137                 "do_exit",
138                 "do_task_dead",
139                 "__module_put_and_exit",
140                 "complete_and_exit",
141                 "__reiserfs_panic",
142                 "lbug_with_loc",
143                 "fortify_panic",
144                 "usercopy_abort",
145                 "machine_real_restart",
146                 "rewind_stack_do_exit",
147                 "kunit_try_catch_throw",
148         };
149
150         if (!func)
151                 return false;
152
153         if (func->bind == STB_WEAK)
154                 return false;
155
156         if (func->bind == STB_GLOBAL)
157                 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
158                         if (!strcmp(func->name, global_noreturns[i]))
159                                 return true;
160
161         if (!func->len)
162                 return false;
163
164         insn = find_insn(file, func->sec, func->offset);
165         if (!insn->func)
166                 return false;
167
168         func_for_each_insn_all(file, func, insn) {
169                 empty = false;
170
171                 if (insn->type == INSN_RETURN)
172                         return false;
173         }
174
175         if (empty)
176                 return false;
177
178         /*
179          * A function can have a sibling call instead of a return.  In that
180          * case, the function's dead-end status depends on whether the target
181          * of the sibling call returns.
182          */
183         func_for_each_insn_all(file, func, insn) {
184                 if (is_sibling_call(insn)) {
185                         struct instruction *dest = insn->jump_dest;
186
187                         if (!dest)
188                                 /* sibling call to another file */
189                                 return false;
190
191                         /* local sibling call */
192                         if (recursion == 5) {
193                                 /*
194                                  * Infinite recursion: two functions have
195                                  * sibling calls to each other.  This is a very
196                                  * rare case.  It means they aren't dead ends.
197                                  */
198                                 return false;
199                         }
200
201                         return __dead_end_function(file, dest->func, recursion+1);
202                 }
203         }
204
205         return true;
206 }
207
208 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
209 {
210         return __dead_end_function(file, func, 0);
211 }
212
213 static void clear_insn_state(struct insn_state *state)
214 {
215         int i;
216
217         memset(state, 0, sizeof(*state));
218         state->cfa.base = CFI_UNDEFINED;
219         for (i = 0; i < CFI_NUM_REGS; i++) {
220                 state->regs[i].base = CFI_UNDEFINED;
221                 state->vals[i].base = CFI_UNDEFINED;
222         }
223         state->drap_reg = CFI_UNDEFINED;
224         state->drap_offset = -1;
225 }
226
227 /*
228  * Call the arch-specific instruction decoder for all the instructions and add
229  * them to the global instruction list.
230  */
231 static int decode_instructions(struct objtool_file *file)
232 {
233         struct section *sec;
234         struct symbol *func;
235         unsigned long offset;
236         struct instruction *insn;
237         int ret;
238
239         for_each_sec(file, sec) {
240
241                 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
242                         continue;
243
244                 if (strcmp(sec->name, ".altinstr_replacement") &&
245                     strcmp(sec->name, ".altinstr_aux") &&
246                     strncmp(sec->name, ".discard.", 9))
247                         sec->text = true;
248
249                 for (offset = 0; offset < sec->len; offset += insn->len) {
250                         insn = malloc(sizeof(*insn));
251                         if (!insn) {
252                                 WARN("malloc failed");
253                                 return -1;
254                         }
255                         memset(insn, 0, sizeof(*insn));
256                         INIT_LIST_HEAD(&insn->alts);
257                         clear_insn_state(&insn->state);
258
259                         insn->sec = sec;
260                         insn->offset = offset;
261
262                         ret = arch_decode_instruction(file->elf, sec, offset,
263                                                       sec->len - offset,
264                                                       &insn->len, &insn->type,
265                                                       &insn->immediate,
266                                                       &insn->stack_op);
267                         if (ret)
268                                 goto err;
269
270                         hash_add(file->insn_hash, &insn->hash, insn->offset);
271                         list_add_tail(&insn->list, &file->insn_list);
272                 }
273
274                 list_for_each_entry(func, &sec->symbol_list, list) {
275                         if (func->type != STT_FUNC || func->alias != func)
276                                 continue;
277
278                         if (!find_insn(file, sec, func->offset)) {
279                                 WARN("%s(): can't find starting instruction",
280                                      func->name);
281                                 return -1;
282                         }
283
284                         func_for_each_insn(file, func, insn)
285                                 insn->func = func;
286                 }
287         }
288
289         return 0;
290
291 err:
292         free(insn);
293         return ret;
294 }
295
296 /*
297  * Mark "ud2" instructions and manually annotated dead ends.
298  */
299 static int add_dead_ends(struct objtool_file *file)
300 {
301         struct section *sec;
302         struct rela *rela;
303         struct instruction *insn;
304         bool found;
305
306         /*
307          * By default, "ud2" is a dead end unless otherwise annotated, because
308          * GCC 7 inserts it for certain divide-by-zero cases.
309          */
310         for_each_insn(file, insn)
311                 if (insn->type == INSN_BUG)
312                         insn->dead_end = true;
313
314         /*
315          * Check for manually annotated dead ends.
316          */
317         sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
318         if (!sec)
319                 goto reachable;
320
321         list_for_each_entry(rela, &sec->rela_list, list) {
322                 if (rela->sym->type != STT_SECTION) {
323                         WARN("unexpected relocation symbol type in %s", sec->name);
324                         return -1;
325                 }
326                 insn = find_insn(file, rela->sym->sec, rela->addend);
327                 if (insn)
328                         insn = list_prev_entry(insn, list);
329                 else if (rela->addend == rela->sym->sec->len) {
330                         found = false;
331                         list_for_each_entry_reverse(insn, &file->insn_list, list) {
332                                 if (insn->sec == rela->sym->sec) {
333                                         found = true;
334                                         break;
335                                 }
336                         }
337
338                         if (!found) {
339                                 WARN("can't find unreachable insn at %s+0x%x",
340                                      rela->sym->sec->name, rela->addend);
341                                 return -1;
342                         }
343                 } else {
344                         WARN("can't find unreachable insn at %s+0x%x",
345                              rela->sym->sec->name, rela->addend);
346                         return -1;
347                 }
348
349                 insn->dead_end = true;
350         }
351
352 reachable:
353         /*
354          * These manually annotated reachable checks are needed for GCC 4.4,
355          * where the Linux unreachable() macro isn't supported.  In that case
356          * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
357          * not a dead end.
358          */
359         sec = find_section_by_name(file->elf, ".rela.discard.reachable");
360         if (!sec)
361                 return 0;
362
363         list_for_each_entry(rela, &sec->rela_list, list) {
364                 if (rela->sym->type != STT_SECTION) {
365                         WARN("unexpected relocation symbol type in %s", sec->name);
366                         return -1;
367                 }
368                 insn = find_insn(file, rela->sym->sec, rela->addend);
369                 if (insn)
370                         insn = list_prev_entry(insn, list);
371                 else if (rela->addend == rela->sym->sec->len) {
372                         found = false;
373                         list_for_each_entry_reverse(insn, &file->insn_list, list) {
374                                 if (insn->sec == rela->sym->sec) {
375                                         found = true;
376                                         break;
377                                 }
378                         }
379
380                         if (!found) {
381                                 WARN("can't find reachable insn at %s+0x%x",
382                                      rela->sym->sec->name, rela->addend);
383                                 return -1;
384                         }
385                 } else {
386                         WARN("can't find reachable insn at %s+0x%x",
387                              rela->sym->sec->name, rela->addend);
388                         return -1;
389                 }
390
391                 insn->dead_end = false;
392         }
393
394         return 0;
395 }
396
397 /*
398  * Warnings shouldn't be reported for ignored functions.
399  */
400 static void add_ignores(struct objtool_file *file)
401 {
402         struct instruction *insn;
403         struct section *sec;
404         struct symbol *func;
405         struct rela *rela;
406
407         sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
408         if (!sec)
409                 return;
410
411         list_for_each_entry(rela, &sec->rela_list, list) {
412                 switch (rela->sym->type) {
413                 case STT_FUNC:
414                         func = rela->sym;
415                         break;
416
417                 case STT_SECTION:
418                         func = find_symbol_by_offset(rela->sym->sec, rela->addend);
419                         if (!func || func->type != STT_FUNC)
420                                 continue;
421                         break;
422
423                 default:
424                         WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type);
425                         continue;
426                 }
427
428                 func_for_each_insn_all(file, func, insn)
429                         insn->ignore = true;
430         }
431 }
432
433 /*
434  * This is a whitelist of functions that is allowed to be called with AC set.
435  * The list is meant to be minimal and only contains compiler instrumentation
436  * ABI and a few functions used to implement *_{to,from}_user() functions.
437  *
438  * These functions must not directly change AC, but may PUSHF/POPF.
439  */
440 static const char *uaccess_safe_builtin[] = {
441         /* KASAN */
442         "kasan_report",
443         "check_memory_region",
444         /* KASAN out-of-line */
445         "__asan_loadN_noabort",
446         "__asan_load1_noabort",
447         "__asan_load2_noabort",
448         "__asan_load4_noabort",
449         "__asan_load8_noabort",
450         "__asan_load16_noabort",
451         "__asan_storeN_noabort",
452         "__asan_store1_noabort",
453         "__asan_store2_noabort",
454         "__asan_store4_noabort",
455         "__asan_store8_noabort",
456         "__asan_store16_noabort",
457         /* KASAN in-line */
458         "__asan_report_load_n_noabort",
459         "__asan_report_load1_noabort",
460         "__asan_report_load2_noabort",
461         "__asan_report_load4_noabort",
462         "__asan_report_load8_noabort",
463         "__asan_report_load16_noabort",
464         "__asan_report_store_n_noabort",
465         "__asan_report_store1_noabort",
466         "__asan_report_store2_noabort",
467         "__asan_report_store4_noabort",
468         "__asan_report_store8_noabort",
469         "__asan_report_store16_noabort",
470         /* KCSAN */
471         "__kcsan_check_access",
472         "kcsan_found_watchpoint",
473         "kcsan_setup_watchpoint",
474         "kcsan_check_scoped_accesses",
475         "kcsan_disable_current",
476         "kcsan_enable_current_nowarn",
477         /* KCSAN/TSAN */
478         "__tsan_func_entry",
479         "__tsan_func_exit",
480         "__tsan_read_range",
481         "__tsan_write_range",
482         "__tsan_read1",
483         "__tsan_read2",
484         "__tsan_read4",
485         "__tsan_read8",
486         "__tsan_read16",
487         "__tsan_write1",
488         "__tsan_write2",
489         "__tsan_write4",
490         "__tsan_write8",
491         "__tsan_write16",
492         /* KCOV */
493         "write_comp_data",
494         "__sanitizer_cov_trace_pc",
495         "__sanitizer_cov_trace_const_cmp1",
496         "__sanitizer_cov_trace_const_cmp2",
497         "__sanitizer_cov_trace_const_cmp4",
498         "__sanitizer_cov_trace_const_cmp8",
499         "__sanitizer_cov_trace_cmp1",
500         "__sanitizer_cov_trace_cmp2",
501         "__sanitizer_cov_trace_cmp4",
502         "__sanitizer_cov_trace_cmp8",
503         /* UBSAN */
504         "ubsan_type_mismatch_common",
505         "__ubsan_handle_type_mismatch",
506         "__ubsan_handle_type_mismatch_v1",
507         "__ubsan_handle_shift_out_of_bounds",
508         /* misc */
509         "csum_partial_copy_generic",
510         "__memcpy_mcsafe",
511         "mcsafe_handle_tail",
512         "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
513         NULL
514 };
515
516 static void add_uaccess_safe(struct objtool_file *file)
517 {
518         struct symbol *func;
519         const char **name;
520
521         if (!uaccess)
522                 return;
523
524         for (name = uaccess_safe_builtin; *name; name++) {
525                 func = find_symbol_by_name(file->elf, *name);
526                 if (!func)
527                         continue;
528
529                 func->uaccess_safe = true;
530         }
531 }
532
533 /*
534  * FIXME: For now, just ignore any alternatives which add retpolines.  This is
535  * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
536  * But it at least allows objtool to understand the control flow *around* the
537  * retpoline.
538  */
539 static int add_ignore_alternatives(struct objtool_file *file)
540 {
541         struct section *sec;
542         struct rela *rela;
543         struct instruction *insn;
544
545         sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
546         if (!sec)
547                 return 0;
548
549         list_for_each_entry(rela, &sec->rela_list, list) {
550                 if (rela->sym->type != STT_SECTION) {
551                         WARN("unexpected relocation symbol type in %s", sec->name);
552                         return -1;
553                 }
554
555                 insn = find_insn(file, rela->sym->sec, rela->addend);
556                 if (!insn) {
557                         WARN("bad .discard.ignore_alts entry");
558                         return -1;
559                 }
560
561                 insn->ignore_alts = true;
562         }
563
564         return 0;
565 }
566
567 /*
568  * Find the destination instructions for all jumps.
569  */
570 static int add_jump_destinations(struct objtool_file *file)
571 {
572         struct instruction *insn;
573         struct rela *rela;
574         struct section *dest_sec;
575         unsigned long dest_off;
576
577         for_each_insn(file, insn) {
578                 if (insn->type != INSN_JUMP_CONDITIONAL &&
579                     insn->type != INSN_JUMP_UNCONDITIONAL)
580                         continue;
581
582                 if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
583                         continue;
584
585                 rela = find_rela_by_dest_range(insn->sec, insn->offset,
586                                                insn->len);
587                 if (!rela) {
588                         dest_sec = insn->sec;
589                         dest_off = insn->offset + insn->len + insn->immediate;
590                 } else if (rela->sym->type == STT_SECTION) {
591                         dest_sec = rela->sym->sec;
592                         dest_off = rela->addend + 4;
593                 } else if (rela->sym->sec->idx) {
594                         dest_sec = rela->sym->sec;
595                         dest_off = rela->sym->sym.st_value + rela->addend + 4;
596                 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
597                         /*
598                          * Retpoline jumps are really dynamic jumps in
599                          * disguise, so convert them accordingly.
600                          */
601                         if (insn->type == INSN_JUMP_UNCONDITIONAL)
602                                 insn->type = INSN_JUMP_DYNAMIC;
603                         else
604                                 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
605
606                         insn->retpoline_safe = true;
607                         continue;
608                 } else {
609                         /* external sibling call */
610                         insn->call_dest = rela->sym;
611                         continue;
612                 }
613
614                 insn->jump_dest = find_insn(file, dest_sec, dest_off);
615                 if (!insn->jump_dest) {
616
617                         /*
618                          * This is a special case where an alt instruction
619                          * jumps past the end of the section.  These are
620                          * handled later in handle_group_alt().
621                          */
622                         if (!strcmp(insn->sec->name, ".altinstr_replacement"))
623                                 continue;
624
625                         WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
626                                   insn->sec, insn->offset, dest_sec->name,
627                                   dest_off);
628                         return -1;
629                 }
630
631                 /*
632                  * Cross-function jump.
633                  */
634                 if (insn->func && insn->jump_dest->func &&
635                     insn->func != insn->jump_dest->func) {
636
637                         /*
638                          * For GCC 8+, create parent/child links for any cold
639                          * subfunctions.  This is _mostly_ redundant with a
640                          * similar initialization in read_symbols().
641                          *
642                          * If a function has aliases, we want the *first* such
643                          * function in the symbol table to be the subfunction's
644                          * parent.  In that case we overwrite the
645                          * initialization done in read_symbols().
646                          *
647                          * However this code can't completely replace the
648                          * read_symbols() code because this doesn't detect the
649                          * case where the parent function's only reference to a
650                          * subfunction is through a jump table.
651                          */
652                         if (!strstr(insn->func->name, ".cold.") &&
653                             strstr(insn->jump_dest->func->name, ".cold.")) {
654                                 insn->func->cfunc = insn->jump_dest->func;
655                                 insn->jump_dest->func->pfunc = insn->func;
656
657                         } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
658                                    insn->jump_dest->offset == insn->jump_dest->func->offset) {
659
660                                 /* internal sibling call */
661                                 insn->call_dest = insn->jump_dest->func;
662                         }
663                 }
664         }
665
666         return 0;
667 }
668
669 /*
670  * Find the destination instructions for all calls.
671  */
672 static int add_call_destinations(struct objtool_file *file)
673 {
674         struct instruction *insn;
675         unsigned long dest_off;
676         struct rela *rela;
677
678         for_each_insn(file, insn) {
679                 if (insn->type != INSN_CALL)
680                         continue;
681
682                 rela = find_rela_by_dest_range(insn->sec, insn->offset,
683                                                insn->len);
684                 if (!rela) {
685                         dest_off = insn->offset + insn->len + insn->immediate;
686                         insn->call_dest = find_symbol_by_offset(insn->sec,
687                                                                 dest_off);
688
689                         if (!insn->call_dest && !insn->ignore) {
690                                 WARN_FUNC("unsupported intra-function call",
691                                           insn->sec, insn->offset);
692                                 if (retpoline)
693                                         WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
694                                 return -1;
695                         }
696
697                 } else if (rela->sym->type == STT_SECTION) {
698                         insn->call_dest = find_symbol_by_offset(rela->sym->sec,
699                                                                 rela->addend+4);
700                         if (!insn->call_dest ||
701                             insn->call_dest->type != STT_FUNC) {
702                                 WARN_FUNC("can't find call dest symbol at %s+0x%x",
703                                           insn->sec, insn->offset,
704                                           rela->sym->sec->name,
705                                           rela->addend + 4);
706                                 return -1;
707                         }
708                 } else
709                         insn->call_dest = rela->sym;
710         }
711
712         return 0;
713 }
714
715 /*
716  * The .alternatives section requires some extra special care, over and above
717  * what other special sections require:
718  *
719  * 1. Because alternatives are patched in-place, we need to insert a fake jump
720  *    instruction at the end so that validate_branch() skips all the original
721  *    replaced instructions when validating the new instruction path.
722  *
723  * 2. An added wrinkle is that the new instruction length might be zero.  In
724  *    that case the old instructions are replaced with noops.  We simulate that
725  *    by creating a fake jump as the only new instruction.
726  *
727  * 3. In some cases, the alternative section includes an instruction which
728  *    conditionally jumps to the _end_ of the entry.  We have to modify these
729  *    jumps' destinations to point back to .text rather than the end of the
730  *    entry in .altinstr_replacement.
731  */
732 static int handle_group_alt(struct objtool_file *file,
733                             struct special_alt *special_alt,
734                             struct instruction *orig_insn,
735                             struct instruction **new_insn)
736 {
737         struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
738         unsigned long dest_off;
739
740         last_orig_insn = NULL;
741         insn = orig_insn;
742         sec_for_each_insn_from(file, insn) {
743                 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
744                         break;
745
746                 insn->alt_group = true;
747                 last_orig_insn = insn;
748         }
749
750         if (next_insn_same_sec(file, last_orig_insn)) {
751                 fake_jump = malloc(sizeof(*fake_jump));
752                 if (!fake_jump) {
753                         WARN("malloc failed");
754                         return -1;
755                 }
756                 memset(fake_jump, 0, sizeof(*fake_jump));
757                 INIT_LIST_HEAD(&fake_jump->alts);
758                 clear_insn_state(&fake_jump->state);
759
760                 fake_jump->sec = special_alt->new_sec;
761                 fake_jump->offset = FAKE_JUMP_OFFSET;
762                 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
763                 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
764                 fake_jump->func = orig_insn->func;
765         }
766
767         if (!special_alt->new_len) {
768                 if (!fake_jump) {
769                         WARN("%s: empty alternative at end of section",
770                              special_alt->orig_sec->name);
771                         return -1;
772                 }
773
774                 *new_insn = fake_jump;
775                 return 0;
776         }
777
778         last_new_insn = NULL;
779         insn = *new_insn;
780         sec_for_each_insn_from(file, insn) {
781                 if (insn->offset >= special_alt->new_off + special_alt->new_len)
782                         break;
783
784                 last_new_insn = insn;
785
786                 insn->ignore = orig_insn->ignore_alts;
787                 insn->func = orig_insn->func;
788
789                 if (insn->type != INSN_JUMP_CONDITIONAL &&
790                     insn->type != INSN_JUMP_UNCONDITIONAL)
791                         continue;
792
793                 if (!insn->immediate)
794                         continue;
795
796                 dest_off = insn->offset + insn->len + insn->immediate;
797                 if (dest_off == special_alt->new_off + special_alt->new_len) {
798                         if (!fake_jump) {
799                                 WARN("%s: alternative jump to end of section",
800                                      special_alt->orig_sec->name);
801                                 return -1;
802                         }
803                         insn->jump_dest = fake_jump;
804                 }
805
806                 if (!insn->jump_dest) {
807                         WARN_FUNC("can't find alternative jump destination",
808                                   insn->sec, insn->offset);
809                         return -1;
810                 }
811         }
812
813         if (!last_new_insn) {
814                 WARN_FUNC("can't find last new alternative instruction",
815                           special_alt->new_sec, special_alt->new_off);
816                 return -1;
817         }
818
819         if (fake_jump)
820                 list_add(&fake_jump->list, &last_new_insn->list);
821
822         return 0;
823 }
824
825 /*
826  * A jump table entry can either convert a nop to a jump or a jump to a nop.
827  * If the original instruction is a jump, make the alt entry an effective nop
828  * by just skipping the original instruction.
829  */
830 static int handle_jump_alt(struct objtool_file *file,
831                            struct special_alt *special_alt,
832                            struct instruction *orig_insn,
833                            struct instruction **new_insn)
834 {
835         if (orig_insn->type == INSN_NOP)
836                 return 0;
837
838         if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
839                 WARN_FUNC("unsupported instruction at jump label",
840                           orig_insn->sec, orig_insn->offset);
841                 return -1;
842         }
843
844         *new_insn = list_next_entry(orig_insn, list);
845         return 0;
846 }
847
848 /*
849  * Read all the special sections which have alternate instructions which can be
850  * patched in or redirected to at runtime.  Each instruction having alternate
851  * instruction(s) has them added to its insn->alts list, which will be
852  * traversed in validate_branch().
853  */
854 static int add_special_section_alts(struct objtool_file *file)
855 {
856         struct list_head special_alts;
857         struct instruction *orig_insn, *new_insn;
858         struct special_alt *special_alt, *tmp;
859         struct alternative *alt;
860         int ret;
861
862         ret = special_get_alts(file->elf, &special_alts);
863         if (ret)
864                 return ret;
865
866         list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
867
868                 orig_insn = find_insn(file, special_alt->orig_sec,
869                                       special_alt->orig_off);
870                 if (!orig_insn) {
871                         WARN_FUNC("special: can't find orig instruction",
872                                   special_alt->orig_sec, special_alt->orig_off);
873                         ret = -1;
874                         goto out;
875                 }
876
877                 new_insn = NULL;
878                 if (!special_alt->group || special_alt->new_len) {
879                         new_insn = find_insn(file, special_alt->new_sec,
880                                              special_alt->new_off);
881                         if (!new_insn) {
882                                 WARN_FUNC("special: can't find new instruction",
883                                           special_alt->new_sec,
884                                           special_alt->new_off);
885                                 ret = -1;
886                                 goto out;
887                         }
888                 }
889
890                 if (special_alt->group) {
891                         ret = handle_group_alt(file, special_alt, orig_insn,
892                                                &new_insn);
893                         if (ret)
894                                 goto out;
895                 } else if (special_alt->jump_or_nop) {
896                         ret = handle_jump_alt(file, special_alt, orig_insn,
897                                               &new_insn);
898                         if (ret)
899                                 goto out;
900                 }
901
902                 alt = malloc(sizeof(*alt));
903                 if (!alt) {
904                         WARN("malloc failed");
905                         ret = -1;
906                         goto out;
907                 }
908
909                 alt->insn = new_insn;
910                 alt->skip_orig = special_alt->skip_orig;
911                 orig_insn->ignore_alts |= special_alt->skip_alt;
912                 list_add_tail(&alt->list, &orig_insn->alts);
913
914                 list_del(&special_alt->list);
915                 free(special_alt);
916         }
917
918 out:
919         return ret;
920 }
921
922 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
923                             struct rela *table)
924 {
925         struct rela *rela = table;
926         struct instruction *dest_insn;
927         struct alternative *alt;
928         struct symbol *pfunc = insn->func->pfunc;
929         unsigned int prev_offset = 0;
930
931         /*
932          * Each @rela is a switch table relocation which points to the target
933          * instruction.
934          */
935         list_for_each_entry_from(rela, &table->sec->rela_list, list) {
936
937                 /* Check for the end of the table: */
938                 if (rela != table && rela->jump_table_start)
939                         break;
940
941                 /* Make sure the table entries are consecutive: */
942                 if (prev_offset && rela->offset != prev_offset + 8)
943                         break;
944
945                 /* Detect function pointers from contiguous objects: */
946                 if (rela->sym->sec == pfunc->sec &&
947                     rela->addend == pfunc->offset)
948                         break;
949
950                 dest_insn = find_insn(file, rela->sym->sec, rela->addend);
951                 if (!dest_insn)
952                         break;
953
954                 /* Make sure the destination is in the same function: */
955                 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
956                         break;
957
958                 alt = malloc(sizeof(*alt));
959                 if (!alt) {
960                         WARN("malloc failed");
961                         return -1;
962                 }
963
964                 alt->insn = dest_insn;
965                 list_add_tail(&alt->list, &insn->alts);
966                 prev_offset = rela->offset;
967         }
968
969         if (!prev_offset) {
970                 WARN_FUNC("can't find switch jump table",
971                           insn->sec, insn->offset);
972                 return -1;
973         }
974
975         return 0;
976 }
977
978 /*
979  * find_jump_table() - Given a dynamic jump, find the switch jump table in
980  * .rodata associated with it.
981  *
982  * There are 3 basic patterns:
983  *
984  * 1. jmpq *[rodata addr](,%reg,8)
985  *
986  *    This is the most common case by far.  It jumps to an address in a simple
987  *    jump table which is stored in .rodata.
988  *
989  * 2. jmpq *[rodata addr](%rip)
990  *
991  *    This is caused by a rare GCC quirk, currently only seen in three driver
992  *    functions in the kernel, only with certain obscure non-distro configs.
993  *
994  *    As part of an optimization, GCC makes a copy of an existing switch jump
995  *    table, modifies it, and then hard-codes the jump (albeit with an indirect
996  *    jump) to use a single entry in the table.  The rest of the jump table and
997  *    some of its jump targets remain as dead code.
998  *
999  *    In such a case we can just crudely ignore all unreachable instruction
1000  *    warnings for the entire object file.  Ideally we would just ignore them
1001  *    for the function, but that would require redesigning the code quite a
1002  *    bit.  And honestly that's just not worth doing: unreachable instruction
1003  *    warnings are of questionable value anyway, and this is such a rare issue.
1004  *
1005  * 3. mov [rodata addr],%reg1
1006  *    ... some instructions ...
1007  *    jmpq *(%reg1,%reg2,8)
1008  *
1009  *    This is a fairly uncommon pattern which is new for GCC 6.  As of this
1010  *    writing, there are 11 occurrences of it in the allmodconfig kernel.
1011  *
1012  *    As of GCC 7 there are quite a few more of these and the 'in between' code
1013  *    is significant. Esp. with KASAN enabled some of the code between the mov
1014  *    and jmpq uses .rodata itself, which can confuse things.
1015  *
1016  *    TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1017  *    ensure the same register is used in the mov and jump instructions.
1018  *
1019  *    NOTE: RETPOLINE made it harder still to decode dynamic jumps.
1020  */
1021 static struct rela *find_jump_table(struct objtool_file *file,
1022                                       struct symbol *func,
1023                                       struct instruction *insn)
1024 {
1025         struct rela *text_rela, *table_rela;
1026         struct instruction *orig_insn = insn;
1027         struct section *table_sec;
1028         unsigned long table_offset;
1029
1030         /*
1031          * Backward search using the @first_jump_src links, these help avoid
1032          * much of the 'in between' code. Which avoids us getting confused by
1033          * it.
1034          */
1035         for (;
1036              &insn->list != &file->insn_list &&
1037              insn->sec == func->sec &&
1038              insn->offset >= func->offset;
1039
1040              insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
1041
1042                 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1043                         break;
1044
1045                 /* allow small jumps within the range */
1046                 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1047                     insn->jump_dest &&
1048                     (insn->jump_dest->offset <= insn->offset ||
1049                      insn->jump_dest->offset > orig_insn->offset))
1050                     break;
1051
1052                 /* look for a relocation which references .rodata */
1053                 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
1054                                                     insn->len);
1055                 if (!text_rela || text_rela->sym->type != STT_SECTION ||
1056                     !text_rela->sym->sec->rodata)
1057                         continue;
1058
1059                 table_offset = text_rela->addend;
1060                 table_sec = text_rela->sym->sec;
1061
1062                 if (text_rela->type == R_X86_64_PC32)
1063                         table_offset += 4;
1064
1065                 /*
1066                  * Make sure the .rodata address isn't associated with a
1067                  * symbol.  GCC jump tables are anonymous data.
1068                  *
1069                  * Also support C jump tables which are in the same format as
1070                  * switch jump tables.  For objtool to recognize them, they
1071                  * need to be placed in the C_JUMP_TABLE_SECTION section.  They
1072                  * have symbols associated with them.
1073                  */
1074                 if (find_symbol_containing(table_sec, table_offset) &&
1075                     strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
1076                         continue;
1077
1078                 /* Each table entry has a rela associated with it. */
1079                 table_rela = find_rela_by_dest(table_sec, table_offset);
1080                 if (!table_rela)
1081                         continue;
1082
1083                 /*
1084                  * Use of RIP-relative switch jumps is quite rare, and
1085                  * indicates a rare GCC quirk/bug which can leave dead code
1086                  * behind.
1087                  */
1088                 if (text_rela->type == R_X86_64_PC32)
1089                         file->ignore_unreachables = true;
1090
1091                 return table_rela;
1092         }
1093
1094         return NULL;
1095 }
1096
1097 /*
1098  * First pass: Mark the head of each jump table so that in the next pass,
1099  * we know when a given jump table ends and the next one starts.
1100  */
1101 static void mark_func_jump_tables(struct objtool_file *file,
1102                                     struct symbol *func)
1103 {
1104         struct instruction *insn, *last = NULL;
1105         struct rela *rela;
1106
1107         func_for_each_insn_all(file, func, insn) {
1108                 if (!last)
1109                         last = insn;
1110
1111                 /*
1112                  * Store back-pointers for unconditional forward jumps such
1113                  * that find_jump_table() can back-track using those and
1114                  * avoid some potentially confusing code.
1115                  */
1116                 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1117                     insn->offset > last->offset &&
1118                     insn->jump_dest->offset > insn->offset &&
1119                     !insn->jump_dest->first_jump_src) {
1120
1121                         insn->jump_dest->first_jump_src = insn;
1122                         last = insn->jump_dest;
1123                 }
1124
1125                 if (insn->type != INSN_JUMP_DYNAMIC)
1126                         continue;
1127
1128                 rela = find_jump_table(file, func, insn);
1129                 if (rela) {
1130                         rela->jump_table_start = true;
1131                         insn->jump_table = rela;
1132                 }
1133         }
1134 }
1135
1136 static int add_func_jump_tables(struct objtool_file *file,
1137                                   struct symbol *func)
1138 {
1139         struct instruction *insn;
1140         int ret;
1141
1142         func_for_each_insn_all(file, func, insn) {
1143                 if (!insn->jump_table)
1144                         continue;
1145
1146                 ret = add_jump_table(file, insn, insn->jump_table);
1147                 if (ret)
1148                         return ret;
1149         }
1150
1151         return 0;
1152 }
1153
1154 /*
1155  * For some switch statements, gcc generates a jump table in the .rodata
1156  * section which contains a list of addresses within the function to jump to.
1157  * This finds these jump tables and adds them to the insn->alts lists.
1158  */
1159 static int add_jump_table_alts(struct objtool_file *file)
1160 {
1161         struct section *sec;
1162         struct symbol *func;
1163         int ret;
1164
1165         if (!file->rodata)
1166                 return 0;
1167
1168         for_each_sec(file, sec) {
1169                 list_for_each_entry(func, &sec->symbol_list, list) {
1170                         if (func->type != STT_FUNC)
1171                                 continue;
1172
1173                         mark_func_jump_tables(file, func);
1174                         ret = add_func_jump_tables(file, func);
1175                         if (ret)
1176                                 return ret;
1177                 }
1178         }
1179
1180         return 0;
1181 }
1182
1183 static int read_unwind_hints(struct objtool_file *file)
1184 {
1185         struct section *sec, *relasec;
1186         struct rela *rela;
1187         struct unwind_hint *hint;
1188         struct instruction *insn;
1189         struct cfi_reg *cfa;
1190         int i;
1191
1192         sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1193         if (!sec)
1194                 return 0;
1195
1196         relasec = sec->rela;
1197         if (!relasec) {
1198                 WARN("missing .rela.discard.unwind_hints section");
1199                 return -1;
1200         }
1201
1202         if (sec->len % sizeof(struct unwind_hint)) {
1203                 WARN("struct unwind_hint size mismatch");
1204                 return -1;
1205         }
1206
1207         file->hints = true;
1208
1209         for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1210                 hint = (struct unwind_hint *)sec->data->d_buf + i;
1211
1212                 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1213                 if (!rela) {
1214                         WARN("can't find rela for unwind_hints[%d]", i);
1215                         return -1;
1216                 }
1217
1218                 insn = find_insn(file, rela->sym->sec, rela->addend);
1219                 if (!insn) {
1220                         WARN("can't find insn for unwind_hints[%d]", i);
1221                         return -1;
1222                 }
1223
1224                 cfa = &insn->state.cfa;
1225
1226                 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1227                         insn->save = true;
1228                         continue;
1229
1230                 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1231                         insn->restore = true;
1232                         insn->hint = true;
1233                         continue;
1234                 }
1235
1236                 insn->hint = true;
1237
1238                 switch (hint->sp_reg) {
1239                 case ORC_REG_UNDEFINED:
1240                         cfa->base = CFI_UNDEFINED;
1241                         break;
1242                 case ORC_REG_SP:
1243                         cfa->base = CFI_SP;
1244                         break;
1245                 case ORC_REG_BP:
1246                         cfa->base = CFI_BP;
1247                         break;
1248                 case ORC_REG_SP_INDIRECT:
1249                         cfa->base = CFI_SP_INDIRECT;
1250                         break;
1251                 case ORC_REG_R10:
1252                         cfa->base = CFI_R10;
1253                         break;
1254                 case ORC_REG_R13:
1255                         cfa->base = CFI_R13;
1256                         break;
1257                 case ORC_REG_DI:
1258                         cfa->base = CFI_DI;
1259                         break;
1260                 case ORC_REG_DX:
1261                         cfa->base = CFI_DX;
1262                         break;
1263                 default:
1264                         WARN_FUNC("unsupported unwind_hint sp base reg %d",
1265                                   insn->sec, insn->offset, hint->sp_reg);
1266                         return -1;
1267                 }
1268
1269                 cfa->offset = hint->sp_offset;
1270                 insn->state.type = hint->type;
1271                 insn->state.end = hint->end;
1272         }
1273
1274         return 0;
1275 }
1276
1277 static int read_retpoline_hints(struct objtool_file *file)
1278 {
1279         struct section *sec;
1280         struct instruction *insn;
1281         struct rela *rela;
1282
1283         sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1284         if (!sec)
1285                 return 0;
1286
1287         list_for_each_entry(rela, &sec->rela_list, list) {
1288                 if (rela->sym->type != STT_SECTION) {
1289                         WARN("unexpected relocation symbol type in %s", sec->name);
1290                         return -1;
1291                 }
1292
1293                 insn = find_insn(file, rela->sym->sec, rela->addend);
1294                 if (!insn) {
1295                         WARN("bad .discard.retpoline_safe entry");
1296                         return -1;
1297                 }
1298
1299                 if (insn->type != INSN_JUMP_DYNAMIC &&
1300                     insn->type != INSN_CALL_DYNAMIC) {
1301                         WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1302                                   insn->sec, insn->offset);
1303                         return -1;
1304                 }
1305
1306                 insn->retpoline_safe = true;
1307         }
1308
1309         return 0;
1310 }
1311
1312 static void mark_rodata(struct objtool_file *file)
1313 {
1314         struct section *sec;
1315         bool found = false;
1316
1317         /*
1318          * Search for the following rodata sections, each of which can
1319          * potentially contain jump tables:
1320          *
1321          * - .rodata: can contain GCC switch tables
1322          * - .rodata.<func>: same, if -fdata-sections is being used
1323          * - .rodata..c_jump_table: contains C annotated jump tables
1324          *
1325          * .rodata.str1.* sections are ignored; they don't contain jump tables.
1326          */
1327         for_each_sec(file, sec) {
1328                 if ((!strncmp(sec->name, ".rodata", 7) && !strstr(sec->name, ".str1.")) ||
1329                     !strcmp(sec->name, C_JUMP_TABLE_SECTION)) {
1330                         sec->rodata = true;
1331                         found = true;
1332                 }
1333         }
1334
1335         file->rodata = found;
1336 }
1337
1338 static int decode_sections(struct objtool_file *file)
1339 {
1340         int ret;
1341
1342         mark_rodata(file);
1343
1344         ret = decode_instructions(file);
1345         if (ret)
1346                 return ret;
1347
1348         ret = add_dead_ends(file);
1349         if (ret)
1350                 return ret;
1351
1352         add_ignores(file);
1353         add_uaccess_safe(file);
1354
1355         ret = add_ignore_alternatives(file);
1356         if (ret)
1357                 return ret;
1358
1359         ret = add_jump_destinations(file);
1360         if (ret)
1361                 return ret;
1362
1363         ret = add_special_section_alts(file);
1364         if (ret)
1365                 return ret;
1366
1367         ret = add_call_destinations(file);
1368         if (ret)
1369                 return ret;
1370
1371         ret = add_jump_table_alts(file);
1372         if (ret)
1373                 return ret;
1374
1375         ret = read_unwind_hints(file);
1376         if (ret)
1377                 return ret;
1378
1379         ret = read_retpoline_hints(file);
1380         if (ret)
1381                 return ret;
1382
1383         return 0;
1384 }
1385
1386 static bool is_fentry_call(struct instruction *insn)
1387 {
1388         if (insn->type == INSN_CALL &&
1389             insn->call_dest->type == STT_NOTYPE &&
1390             !strcmp(insn->call_dest->name, "__fentry__"))
1391                 return true;
1392
1393         return false;
1394 }
1395
1396 static bool has_modified_stack_frame(struct insn_state *state)
1397 {
1398         int i;
1399
1400         if (state->cfa.base != initial_func_cfi.cfa.base ||
1401             state->cfa.offset != initial_func_cfi.cfa.offset ||
1402             state->stack_size != initial_func_cfi.cfa.offset ||
1403             state->drap)
1404                 return true;
1405
1406         for (i = 0; i < CFI_NUM_REGS; i++)
1407                 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1408                     state->regs[i].offset != initial_func_cfi.regs[i].offset)
1409                         return true;
1410
1411         return false;
1412 }
1413
1414 static bool has_valid_stack_frame(struct insn_state *state)
1415 {
1416         if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1417             state->regs[CFI_BP].offset == -16)
1418                 return true;
1419
1420         if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1421                 return true;
1422
1423         return false;
1424 }
1425
1426 static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1427 {
1428         struct cfi_reg *cfa = &state->cfa;
1429         struct stack_op *op = &insn->stack_op;
1430
1431         if (cfa->base != CFI_SP)
1432                 return 0;
1433
1434         /* push */
1435         if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1436                 cfa->offset += 8;
1437
1438         /* pop */
1439         if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1440                 cfa->offset -= 8;
1441
1442         /* add immediate to sp */
1443         if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1444             op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1445                 cfa->offset -= op->src.offset;
1446
1447         return 0;
1448 }
1449
1450 static void save_reg(struct insn_state *state, unsigned char reg, int base,
1451                      int offset)
1452 {
1453         if (arch_callee_saved_reg(reg) &&
1454             state->regs[reg].base == CFI_UNDEFINED) {
1455                 state->regs[reg].base = base;
1456                 state->regs[reg].offset = offset;
1457         }
1458 }
1459
1460 static void restore_reg(struct insn_state *state, unsigned char reg)
1461 {
1462         state->regs[reg].base = CFI_UNDEFINED;
1463         state->regs[reg].offset = 0;
1464 }
1465
1466 /*
1467  * A note about DRAP stack alignment:
1468  *
1469  * GCC has the concept of a DRAP register, which is used to help keep track of
1470  * the stack pointer when aligning the stack.  r10 or r13 is used as the DRAP
1471  * register.  The typical DRAP pattern is:
1472  *
1473  *   4c 8d 54 24 08             lea    0x8(%rsp),%r10
1474  *   48 83 e4 c0                and    $0xffffffffffffffc0,%rsp
1475  *   41 ff 72 f8                pushq  -0x8(%r10)
1476  *   55                         push   %rbp
1477  *   48 89 e5                   mov    %rsp,%rbp
1478  *                              (more pushes)
1479  *   41 52                      push   %r10
1480  *                              ...
1481  *   41 5a                      pop    %r10
1482  *                              (more pops)
1483  *   5d                         pop    %rbp
1484  *   49 8d 62 f8                lea    -0x8(%r10),%rsp
1485  *   c3                         retq
1486  *
1487  * There are some variations in the epilogues, like:
1488  *
1489  *   5b                         pop    %rbx
1490  *   41 5a                      pop    %r10
1491  *   41 5c                      pop    %r12
1492  *   41 5d                      pop    %r13
1493  *   41 5e                      pop    %r14
1494  *   c9                         leaveq
1495  *   49 8d 62 f8                lea    -0x8(%r10),%rsp
1496  *   c3                         retq
1497  *
1498  * and:
1499  *
1500  *   4c 8b 55 e8                mov    -0x18(%rbp),%r10
1501  *   48 8b 5d e0                mov    -0x20(%rbp),%rbx
1502  *   4c 8b 65 f0                mov    -0x10(%rbp),%r12
1503  *   4c 8b 6d f8                mov    -0x8(%rbp),%r13
1504  *   c9                         leaveq
1505  *   49 8d 62 f8                lea    -0x8(%r10),%rsp
1506  *   c3                         retq
1507  *
1508  * Sometimes r13 is used as the DRAP register, in which case it's saved and
1509  * restored beforehand:
1510  *
1511  *   41 55                      push   %r13
1512  *   4c 8d 6c 24 10             lea    0x10(%rsp),%r13
1513  *   48 83 e4 f0                and    $0xfffffffffffffff0,%rsp
1514  *                              ...
1515  *   49 8d 65 f0                lea    -0x10(%r13),%rsp
1516  *   41 5d                      pop    %r13
1517  *   c3                         retq
1518  */
1519 static int update_insn_state(struct instruction *insn, struct insn_state *state)
1520 {
1521         struct stack_op *op = &insn->stack_op;
1522         struct cfi_reg *cfa = &state->cfa;
1523         struct cfi_reg *regs = state->regs;
1524
1525         /* stack operations don't make sense with an undefined CFA */
1526         if (cfa->base == CFI_UNDEFINED) {
1527                 if (insn->func) {
1528                         WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1529                         return -1;
1530                 }
1531                 return 0;
1532         }
1533
1534         if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1535                 return update_insn_state_regs(insn, state);
1536
1537         switch (op->dest.type) {
1538
1539         case OP_DEST_REG:
1540                 switch (op->src.type) {
1541
1542                 case OP_SRC_REG:
1543                         if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1544                             cfa->base == CFI_SP &&
1545                             regs[CFI_BP].base == CFI_CFA &&
1546                             regs[CFI_BP].offset == -cfa->offset) {
1547
1548                                 /* mov %rsp, %rbp */
1549                                 cfa->base = op->dest.reg;
1550                                 state->bp_scratch = false;
1551                         }
1552
1553                         else if (op->src.reg == CFI_SP &&
1554                                  op->dest.reg == CFI_BP && state->drap) {
1555
1556                                 /* drap: mov %rsp, %rbp */
1557                                 regs[CFI_BP].base = CFI_BP;
1558                                 regs[CFI_BP].offset = -state->stack_size;
1559                                 state->bp_scratch = false;
1560                         }
1561
1562                         else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1563
1564                                 /*
1565                                  * mov %rsp, %reg
1566                                  *
1567                                  * This is needed for the rare case where GCC
1568                                  * does:
1569                                  *
1570                                  *   mov    %rsp, %rax
1571                                  *   ...
1572                                  *   mov    %rax, %rsp
1573                                  */
1574                                 state->vals[op->dest.reg].base = CFI_CFA;
1575                                 state->vals[op->dest.reg].offset = -state->stack_size;
1576                         }
1577
1578                         else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1579                                  cfa->base == CFI_BP) {
1580
1581                                 /*
1582                                  * mov %rbp, %rsp
1583                                  *
1584                                  * Restore the original stack pointer (Clang).
1585                                  */
1586                                 state->stack_size = -state->regs[CFI_BP].offset;
1587                         }
1588
1589                         else if (op->dest.reg == cfa->base) {
1590
1591                                 /* mov %reg, %rsp */
1592                                 if (cfa->base == CFI_SP &&
1593                                     state->vals[op->src.reg].base == CFI_CFA) {
1594
1595                                         /*
1596                                          * This is needed for the rare case
1597                                          * where GCC does something dumb like:
1598                                          *
1599                                          *   lea    0x8(%rsp), %rcx
1600                                          *   ...
1601                                          *   mov    %rcx, %rsp
1602                                          */
1603                                         cfa->offset = -state->vals[op->src.reg].offset;
1604                                         state->stack_size = cfa->offset;
1605
1606                                 } else {
1607                                         cfa->base = CFI_UNDEFINED;
1608                                         cfa->offset = 0;
1609                                 }
1610                         }
1611
1612                         break;
1613
1614                 case OP_SRC_ADD:
1615                         if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1616
1617                                 /* add imm, %rsp */
1618                                 state->stack_size -= op->src.offset;
1619                                 if (cfa->base == CFI_SP)
1620                                         cfa->offset -= op->src.offset;
1621                                 break;
1622                         }
1623
1624                         if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1625
1626                                 /* lea disp(%rbp), %rsp */
1627                                 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1628                                 break;
1629                         }
1630
1631                         if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1632
1633                                 /* drap: lea disp(%rsp), %drap */
1634                                 state->drap_reg = op->dest.reg;
1635
1636                                 /*
1637                                  * lea disp(%rsp), %reg
1638                                  *
1639                                  * This is needed for the rare case where GCC
1640                                  * does something dumb like:
1641                                  *
1642                                  *   lea    0x8(%rsp), %rcx
1643                                  *   ...
1644                                  *   mov    %rcx, %rsp
1645                                  */
1646                                 state->vals[op->dest.reg].base = CFI_CFA;
1647                                 state->vals[op->dest.reg].offset = \
1648                                         -state->stack_size + op->src.offset;
1649
1650                                 break;
1651                         }
1652
1653                         if (state->drap && op->dest.reg == CFI_SP &&
1654                             op->src.reg == state->drap_reg) {
1655
1656                                  /* drap: lea disp(%drap), %rsp */
1657                                 cfa->base = CFI_SP;
1658                                 cfa->offset = state->stack_size = -op->src.offset;
1659                                 state->drap_reg = CFI_UNDEFINED;
1660                                 state->drap = false;
1661                                 break;
1662                         }
1663
1664                         if (op->dest.reg == state->cfa.base) {
1665                                 WARN_FUNC("unsupported stack register modification",
1666                                           insn->sec, insn->offset);
1667                                 return -1;
1668                         }
1669
1670                         break;
1671
1672                 case OP_SRC_AND:
1673                         if (op->dest.reg != CFI_SP ||
1674                             (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1675                             (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1676                                 WARN_FUNC("unsupported stack pointer realignment",
1677                                           insn->sec, insn->offset);
1678                                 return -1;
1679                         }
1680
1681                         if (state->drap_reg != CFI_UNDEFINED) {
1682                                 /* drap: and imm, %rsp */
1683                                 cfa->base = state->drap_reg;
1684                                 cfa->offset = state->stack_size = 0;
1685                                 state->drap = true;
1686                         }
1687
1688                         /*
1689                          * Older versions of GCC (4.8ish) realign the stack
1690                          * without DRAP, with a frame pointer.
1691                          */
1692
1693                         break;
1694
1695                 case OP_SRC_POP:
1696                 case OP_SRC_POPF:
1697                         if (!state->drap && op->dest.type == OP_DEST_REG &&
1698                             op->dest.reg == cfa->base) {
1699
1700                                 /* pop %rbp */
1701                                 cfa->base = CFI_SP;
1702                         }
1703
1704                         if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1705                             op->dest.type == OP_DEST_REG &&
1706                             op->dest.reg == state->drap_reg &&
1707                             state->drap_offset == -state->stack_size) {
1708
1709                                 /* drap: pop %drap */
1710                                 cfa->base = state->drap_reg;
1711                                 cfa->offset = 0;
1712                                 state->drap_offset = -1;
1713
1714                         } else if (regs[op->dest.reg].offset == -state->stack_size) {
1715
1716                                 /* pop %reg */
1717                                 restore_reg(state, op->dest.reg);
1718                         }
1719
1720                         state->stack_size -= 8;
1721                         if (cfa->base == CFI_SP)
1722                                 cfa->offset -= 8;
1723
1724                         break;
1725
1726                 case OP_SRC_REG_INDIRECT:
1727                         if (state->drap && op->src.reg == CFI_BP &&
1728                             op->src.offset == state->drap_offset) {
1729
1730                                 /* drap: mov disp(%rbp), %drap */
1731                                 cfa->base = state->drap_reg;
1732                                 cfa->offset = 0;
1733                                 state->drap_offset = -1;
1734                         }
1735
1736                         if (state->drap && op->src.reg == CFI_BP &&
1737                             op->src.offset == regs[op->dest.reg].offset) {
1738
1739                                 /* drap: mov disp(%rbp), %reg */
1740                                 restore_reg(state, op->dest.reg);
1741
1742                         } else if (op->src.reg == cfa->base &&
1743                             op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1744
1745                                 /* mov disp(%rbp), %reg */
1746                                 /* mov disp(%rsp), %reg */
1747                                 restore_reg(state, op->dest.reg);
1748                         }
1749
1750                         break;
1751
1752                 default:
1753                         WARN_FUNC("unknown stack-related instruction",
1754                                   insn->sec, insn->offset);
1755                         return -1;
1756                 }
1757
1758                 break;
1759
1760         case OP_DEST_PUSH:
1761         case OP_DEST_PUSHF:
1762                 state->stack_size += 8;
1763                 if (cfa->base == CFI_SP)
1764                         cfa->offset += 8;
1765
1766                 if (op->src.type != OP_SRC_REG)
1767                         break;
1768
1769                 if (state->drap) {
1770                         if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1771
1772                                 /* drap: push %drap */
1773                                 cfa->base = CFI_BP_INDIRECT;
1774                                 cfa->offset = -state->stack_size;
1775
1776                                 /* save drap so we know when to restore it */
1777                                 state->drap_offset = -state->stack_size;
1778
1779                         } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1780
1781                                 /* drap: push %rbp */
1782                                 state->stack_size = 0;
1783
1784                         } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1785
1786                                 /* drap: push %reg */
1787                                 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1788                         }
1789
1790                 } else {
1791
1792                         /* push %reg */
1793                         save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1794                 }
1795
1796                 /* detect when asm code uses rbp as a scratch register */
1797                 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1798                     cfa->base != CFI_BP)
1799                         state->bp_scratch = true;
1800                 break;
1801
1802         case OP_DEST_REG_INDIRECT:
1803
1804                 if (state->drap) {
1805                         if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1806
1807                                 /* drap: mov %drap, disp(%rbp) */
1808                                 cfa->base = CFI_BP_INDIRECT;
1809                                 cfa->offset = op->dest.offset;
1810
1811                                 /* save drap offset so we know when to restore it */
1812                                 state->drap_offset = op->dest.offset;
1813                         }
1814
1815                         else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1816
1817                                 /* drap: mov reg, disp(%rbp) */
1818                                 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1819                         }
1820
1821                 } else if (op->dest.reg == cfa->base) {
1822
1823                         /* mov reg, disp(%rbp) */
1824                         /* mov reg, disp(%rsp) */
1825                         save_reg(state, op->src.reg, CFI_CFA,
1826                                  op->dest.offset - state->cfa.offset);
1827                 }
1828
1829                 break;
1830
1831         case OP_DEST_LEAVE:
1832                 if ((!state->drap && cfa->base != CFI_BP) ||
1833                     (state->drap && cfa->base != state->drap_reg)) {
1834                         WARN_FUNC("leave instruction with modified stack frame",
1835                                   insn->sec, insn->offset);
1836                         return -1;
1837                 }
1838
1839                 /* leave (mov %rbp, %rsp; pop %rbp) */
1840
1841                 state->stack_size = -state->regs[CFI_BP].offset - 8;
1842                 restore_reg(state, CFI_BP);
1843
1844                 if (!state->drap) {
1845                         cfa->base = CFI_SP;
1846                         cfa->offset -= 8;
1847                 }
1848
1849                 break;
1850
1851         case OP_DEST_MEM:
1852                 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
1853                         WARN_FUNC("unknown stack-related memory operation",
1854                                   insn->sec, insn->offset);
1855                         return -1;
1856                 }
1857
1858                 /* pop mem */
1859                 state->stack_size -= 8;
1860                 if (cfa->base == CFI_SP)
1861                         cfa->offset -= 8;
1862
1863                 break;
1864
1865         default:
1866                 WARN_FUNC("unknown stack-related instruction",
1867                           insn->sec, insn->offset);
1868                 return -1;
1869         }
1870
1871         return 0;
1872 }
1873
1874 static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1875 {
1876         struct insn_state *state1 = &insn->state, *state2 = state;
1877         int i;
1878
1879         if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1880                 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1881                           insn->sec, insn->offset,
1882                           state1->cfa.base, state1->cfa.offset,
1883                           state2->cfa.base, state2->cfa.offset);
1884
1885         } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1886                 for (i = 0; i < CFI_NUM_REGS; i++) {
1887                         if (!memcmp(&state1->regs[i], &state2->regs[i],
1888                                     sizeof(struct cfi_reg)))
1889                                 continue;
1890
1891                         WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1892                                   insn->sec, insn->offset,
1893                                   i, state1->regs[i].base, state1->regs[i].offset,
1894                                   i, state2->regs[i].base, state2->regs[i].offset);
1895                         break;
1896                 }
1897
1898         } else if (state1->type != state2->type) {
1899                 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1900                           insn->sec, insn->offset, state1->type, state2->type);
1901
1902         } else if (state1->drap != state2->drap ||
1903                  (state1->drap && state1->drap_reg != state2->drap_reg) ||
1904                  (state1->drap && state1->drap_offset != state2->drap_offset)) {
1905                 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1906                           insn->sec, insn->offset,
1907                           state1->drap, state1->drap_reg, state1->drap_offset,
1908                           state2->drap, state2->drap_reg, state2->drap_offset);
1909
1910         } else
1911                 return true;
1912
1913         return false;
1914 }
1915
1916 static inline bool func_uaccess_safe(struct symbol *func)
1917 {
1918         if (func)
1919                 return func->uaccess_safe;
1920
1921         return false;
1922 }
1923
1924 static inline const char *call_dest_name(struct instruction *insn)
1925 {
1926         if (insn->call_dest)
1927                 return insn->call_dest->name;
1928
1929         return "{dynamic}";
1930 }
1931
1932 static int validate_call(struct instruction *insn, struct insn_state *state)
1933 {
1934         if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
1935                 WARN_FUNC("call to %s() with UACCESS enabled",
1936                                 insn->sec, insn->offset, call_dest_name(insn));
1937                 return 1;
1938         }
1939
1940         if (state->df) {
1941                 WARN_FUNC("call to %s() with DF set",
1942                                 insn->sec, insn->offset, call_dest_name(insn));
1943                 return 1;
1944         }
1945
1946         return 0;
1947 }
1948
1949 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
1950 {
1951         if (has_modified_stack_frame(state)) {
1952                 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1953                                 insn->sec, insn->offset);
1954                 return 1;
1955         }
1956
1957         return validate_call(insn, state);
1958 }
1959
1960 /*
1961  * Follow the branch starting at the given instruction, and recursively follow
1962  * any other branches (jumps).  Meanwhile, track the frame pointer state at
1963  * each instruction and validate all the rules described in
1964  * tools/objtool/Documentation/stack-validation.txt.
1965  */
1966 static int validate_branch(struct objtool_file *file, struct symbol *func,
1967                            struct instruction *first, struct insn_state state)
1968 {
1969         struct alternative *alt;
1970         struct instruction *insn, *next_insn;
1971         struct section *sec;
1972         u8 visited;
1973         int ret;
1974
1975         insn = first;
1976         sec = insn->sec;
1977
1978         if (insn->alt_group && list_empty(&insn->alts)) {
1979                 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1980                           sec, insn->offset);
1981                 return 1;
1982         }
1983
1984         while (1) {
1985                 next_insn = next_insn_same_sec(file, insn);
1986
1987                 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
1988                         WARN("%s() falls through to next function %s()",
1989                              func->name, insn->func->name);
1990                         return 1;
1991                 }
1992
1993                 if (func && insn->ignore) {
1994                         WARN_FUNC("BUG: why am I validating an ignored function?",
1995                                   sec, insn->offset);
1996                         return 1;
1997                 }
1998
1999                 visited = 1 << state.uaccess;
2000                 if (insn->visited) {
2001                         if (!insn->hint && !insn_state_match(insn, &state))
2002                                 return 1;
2003
2004                         if (insn->visited & visited)
2005                                 return 0;
2006                 }
2007
2008                 if (insn->hint) {
2009                         if (insn->restore) {
2010                                 struct instruction *save_insn, *i;
2011
2012                                 i = insn;
2013                                 save_insn = NULL;
2014                                 func_for_each_insn_continue_reverse(file, func, i) {
2015                                         if (i->save) {
2016                                                 save_insn = i;
2017                                                 break;
2018                                         }
2019                                 }
2020
2021                                 if (!save_insn) {
2022                                         WARN_FUNC("no corresponding CFI save for CFI restore",
2023                                                   sec, insn->offset);
2024                                         return 1;
2025                                 }
2026
2027                                 if (!save_insn->visited) {
2028                                         /*
2029                                          * Oops, no state to copy yet.
2030                                          * Hopefully we can reach this
2031                                          * instruction from another branch
2032                                          * after the save insn has been
2033                                          * visited.
2034                                          */
2035                                         if (insn == first)
2036                                                 return 0;
2037
2038                                         WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
2039                                                   sec, insn->offset);
2040                                         return 1;
2041                                 }
2042
2043                                 insn->state = save_insn->state;
2044                         }
2045
2046                         state = insn->state;
2047
2048                 } else
2049                         insn->state = state;
2050
2051                 insn->visited |= visited;
2052
2053                 if (!insn->ignore_alts) {
2054                         bool skip_orig = false;
2055
2056                         list_for_each_entry(alt, &insn->alts, list) {
2057                                 if (alt->skip_orig)
2058                                         skip_orig = true;
2059
2060                                 ret = validate_branch(file, func, alt->insn, state);
2061                                 if (ret) {
2062                                         if (backtrace)
2063                                                 BT_FUNC("(alt)", insn);
2064                                         return ret;
2065                                 }
2066                         }
2067
2068                         if (skip_orig)
2069                                 return 0;
2070                 }
2071
2072                 switch (insn->type) {
2073
2074                 case INSN_RETURN:
2075                         if (state.uaccess && !func_uaccess_safe(func)) {
2076                                 WARN_FUNC("return with UACCESS enabled", sec, insn->offset);
2077                                 return 1;
2078                         }
2079
2080                         if (!state.uaccess && func_uaccess_safe(func)) {
2081                                 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", sec, insn->offset);
2082                                 return 1;
2083                         }
2084
2085                         if (state.df) {
2086                                 WARN_FUNC("return with DF set", sec, insn->offset);
2087                                 return 1;
2088                         }
2089
2090                         if (func && has_modified_stack_frame(&state)) {
2091                                 WARN_FUNC("return with modified stack frame",
2092                                           sec, insn->offset);
2093                                 return 1;
2094                         }
2095
2096                         if (state.bp_scratch) {
2097                                 WARN("%s uses BP as a scratch register",
2098                                      func->name);
2099                                 return 1;
2100                         }
2101
2102                         return 0;
2103
2104                 case INSN_CALL:
2105                 case INSN_CALL_DYNAMIC:
2106                         ret = validate_call(insn, &state);
2107                         if (ret)
2108                                 return ret;
2109
2110                         if (!no_fp && func && !is_fentry_call(insn) &&
2111                             !has_valid_stack_frame(&state)) {
2112                                 WARN_FUNC("call without frame pointer save/setup",
2113                                           sec, insn->offset);
2114                                 return 1;
2115                         }
2116
2117                         if (dead_end_function(file, insn->call_dest))
2118                                 return 0;
2119
2120                         break;
2121
2122                 case INSN_JUMP_CONDITIONAL:
2123                 case INSN_JUMP_UNCONDITIONAL:
2124                         if (func && is_sibling_call(insn)) {
2125                                 ret = validate_sibling_call(insn, &state);
2126                                 if (ret)
2127                                         return ret;
2128
2129                         } else if (insn->jump_dest) {
2130                                 ret = validate_branch(file, func,
2131                                                       insn->jump_dest, state);
2132                                 if (ret) {
2133                                         if (backtrace)
2134                                                 BT_FUNC("(branch)", insn);
2135                                         return ret;
2136                                 }
2137                         }
2138
2139                         if (insn->type == INSN_JUMP_UNCONDITIONAL)
2140                                 return 0;
2141
2142                         break;
2143
2144                 case INSN_JUMP_DYNAMIC:
2145                 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2146                         if (func && is_sibling_call(insn)) {
2147                                 ret = validate_sibling_call(insn, &state);
2148                                 if (ret)
2149                                         return ret;
2150                         }
2151
2152                         if (insn->type == INSN_JUMP_DYNAMIC)
2153                                 return 0;
2154
2155                         break;
2156
2157                 case INSN_CONTEXT_SWITCH:
2158                         if (func && (!next_insn || !next_insn->hint)) {
2159                                 WARN_FUNC("unsupported instruction in callable function",
2160                                           sec, insn->offset);
2161                                 return 1;
2162                         }
2163                         return 0;
2164
2165                 case INSN_STACK:
2166                         if (update_insn_state(insn, &state))
2167                                 return 1;
2168
2169                         if (insn->stack_op.dest.type == OP_DEST_PUSHF) {
2170                                 if (!state.uaccess_stack) {
2171                                         state.uaccess_stack = 1;
2172                                 } else if (state.uaccess_stack >> 31) {
2173                                         WARN_FUNC("PUSHF stack exhausted", sec, insn->offset);
2174                                         return 1;
2175                                 }
2176                                 state.uaccess_stack <<= 1;
2177                                 state.uaccess_stack  |= state.uaccess;
2178                         }
2179
2180                         if (insn->stack_op.src.type == OP_SRC_POPF) {
2181                                 if (state.uaccess_stack) {
2182                                         state.uaccess = state.uaccess_stack & 1;
2183                                         state.uaccess_stack >>= 1;
2184                                         if (state.uaccess_stack == 1)
2185                                                 state.uaccess_stack = 0;
2186                                 }
2187                         }
2188
2189                         break;
2190
2191                 case INSN_STAC:
2192                         if (state.uaccess) {
2193                                 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2194                                 return 1;
2195                         }
2196
2197                         state.uaccess = true;
2198                         break;
2199
2200                 case INSN_CLAC:
2201                         if (!state.uaccess && func) {
2202                                 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2203                                 return 1;
2204                         }
2205
2206                         if (func_uaccess_safe(func) && !state.uaccess_stack) {
2207                                 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2208                                 return 1;
2209                         }
2210
2211                         state.uaccess = false;
2212                         break;
2213
2214                 case INSN_STD:
2215                         if (state.df)
2216                                 WARN_FUNC("recursive STD", sec, insn->offset);
2217
2218                         state.df = true;
2219                         break;
2220
2221                 case INSN_CLD:
2222                         if (!state.df && func)
2223                                 WARN_FUNC("redundant CLD", sec, insn->offset);
2224
2225                         state.df = false;
2226                         break;
2227
2228                 default:
2229                         break;
2230                 }
2231
2232                 if (insn->dead_end)
2233                         return 0;
2234
2235                 if (!next_insn) {
2236                         if (state.cfa.base == CFI_UNDEFINED)
2237                                 return 0;
2238                         WARN("%s: unexpected end of section", sec->name);
2239                         return 1;
2240                 }
2241
2242                 insn = next_insn;
2243         }
2244
2245         return 0;
2246 }
2247
2248 static int validate_unwind_hints(struct objtool_file *file)
2249 {
2250         struct instruction *insn;
2251         int ret, warnings = 0;
2252         struct insn_state state;
2253
2254         if (!file->hints)
2255                 return 0;
2256
2257         clear_insn_state(&state);
2258
2259         for_each_insn(file, insn) {
2260                 if (insn->hint && !insn->visited) {
2261                         ret = validate_branch(file, insn->func, insn, state);
2262                         if (ret && backtrace)
2263                                 BT_FUNC("<=== (hint)", insn);
2264                         warnings += ret;
2265                 }
2266         }
2267
2268         return warnings;
2269 }
2270
2271 static int validate_retpoline(struct objtool_file *file)
2272 {
2273         struct instruction *insn;
2274         int warnings = 0;
2275
2276         for_each_insn(file, insn) {
2277                 if (insn->type != INSN_JUMP_DYNAMIC &&
2278                     insn->type != INSN_CALL_DYNAMIC)
2279                         continue;
2280
2281                 if (insn->retpoline_safe)
2282                         continue;
2283
2284                 /*
2285                  * .init.text code is ran before userspace and thus doesn't
2286                  * strictly need retpolines, except for modules which are
2287                  * loaded late, they very much do need retpoline in their
2288                  * .init.text
2289                  */
2290                 if (!strcmp(insn->sec->name, ".init.text") && !module)
2291                         continue;
2292
2293                 WARN_FUNC("indirect %s found in RETPOLINE build",
2294                           insn->sec, insn->offset,
2295                           insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2296
2297                 warnings++;
2298         }
2299
2300         return warnings;
2301 }
2302
2303 static bool is_kasan_insn(struct instruction *insn)
2304 {
2305         return (insn->type == INSN_CALL &&
2306                 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2307 }
2308
2309 static bool is_ubsan_insn(struct instruction *insn)
2310 {
2311         return (insn->type == INSN_CALL &&
2312                 !strcmp(insn->call_dest->name,
2313                         "__ubsan_handle_builtin_unreachable"));
2314 }
2315
2316 static bool ignore_unreachable_insn(struct instruction *insn)
2317 {
2318         int i;
2319
2320         if (insn->ignore || insn->type == INSN_NOP)
2321                 return true;
2322
2323         /*
2324          * Ignore any unused exceptions.  This can happen when a whitelisted
2325          * function has an exception table entry.
2326          *
2327          * Also ignore alternative replacement instructions.  This can happen
2328          * when a whitelisted function uses one of the ALTERNATIVE macros.
2329          */
2330         if (!strcmp(insn->sec->name, ".fixup") ||
2331             !strcmp(insn->sec->name, ".altinstr_replacement") ||
2332             !strcmp(insn->sec->name, ".altinstr_aux"))
2333                 return true;
2334
2335         /*
2336          * Check if this (or a subsequent) instruction is related to
2337          * CONFIG_UBSAN or CONFIG_KASAN.
2338          *
2339          * End the search at 5 instructions to avoid going into the weeds.
2340          */
2341         if (!insn->func)
2342                 return false;
2343         for (i = 0; i < 5; i++) {
2344
2345                 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2346                         return true;
2347
2348                 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2349                         if (insn->jump_dest &&
2350                             insn->jump_dest->func == insn->func) {
2351                                 insn = insn->jump_dest;
2352                                 continue;
2353                         }
2354
2355                         break;
2356                 }
2357
2358                 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2359                         break;
2360
2361                 insn = list_next_entry(insn, list);
2362         }
2363
2364         return false;
2365 }
2366
2367 static int validate_functions(struct objtool_file *file)
2368 {
2369         struct section *sec;
2370         struct symbol *func;
2371         struct instruction *insn;
2372         struct insn_state state;
2373         int ret, warnings = 0;
2374
2375         clear_insn_state(&state);
2376
2377         state.cfa = initial_func_cfi.cfa;
2378         memcpy(&state.regs, &initial_func_cfi.regs,
2379                CFI_NUM_REGS * sizeof(struct cfi_reg));
2380         state.stack_size = initial_func_cfi.cfa.offset;
2381
2382         for_each_sec(file, sec) {
2383                 list_for_each_entry(func, &sec->symbol_list, list) {
2384                         if (func->type != STT_FUNC)
2385                                 continue;
2386
2387                         if (!func->len) {
2388                                 WARN("%s() is missing an ELF size annotation",
2389                                      func->name);
2390                                 warnings++;
2391                         }
2392
2393                         if (func->pfunc != func || func->alias != func)
2394                                 continue;
2395
2396                         insn = find_insn(file, sec, func->offset);
2397                         if (!insn || insn->ignore || insn->visited)
2398                                 continue;
2399
2400                         state.uaccess = func->uaccess_safe;
2401
2402                         ret = validate_branch(file, func, insn, state);
2403                         if (ret && backtrace)
2404                                 BT_FUNC("<=== (func)", insn);
2405                         warnings += ret;
2406                 }
2407         }
2408
2409         return warnings;
2410 }
2411
2412 static int validate_reachable_instructions(struct objtool_file *file)
2413 {
2414         struct instruction *insn;
2415
2416         if (file->ignore_unreachables)
2417                 return 0;
2418
2419         for_each_insn(file, insn) {
2420                 if (insn->visited || ignore_unreachable_insn(insn))
2421                         continue;
2422
2423                 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2424                 return 1;
2425         }
2426
2427         return 0;
2428 }
2429
2430 static void cleanup(struct objtool_file *file)
2431 {
2432         struct instruction *insn, *tmpinsn;
2433         struct alternative *alt, *tmpalt;
2434
2435         list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2436                 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2437                         list_del(&alt->list);
2438                         free(alt);
2439                 }
2440                 list_del(&insn->list);
2441                 hash_del(&insn->hash);
2442                 free(insn);
2443         }
2444         elf_close(file->elf);
2445 }
2446
2447 static struct objtool_file file;
2448
2449 int check(const char *_objname, bool orc)
2450 {
2451         int ret, warnings = 0;
2452
2453         objname = _objname;
2454
2455         file.elf = elf_read(objname, orc ? O_RDWR : O_RDONLY);
2456         if (!file.elf)
2457                 return 1;
2458
2459         INIT_LIST_HEAD(&file.insn_list);
2460         hash_init(file.insn_hash);
2461         file.c_file = find_section_by_name(file.elf, ".comment");
2462         file.ignore_unreachables = no_unreachable;
2463         file.hints = false;
2464
2465         arch_initial_func_cfi_state(&initial_func_cfi);
2466
2467         ret = decode_sections(&file);
2468         if (ret < 0)
2469                 goto out;
2470         warnings += ret;
2471
2472         if (list_empty(&file.insn_list))
2473                 goto out;
2474
2475         if (retpoline) {
2476                 ret = validate_retpoline(&file);
2477                 if (ret < 0)
2478                         return ret;
2479                 warnings += ret;
2480         }
2481
2482         ret = validate_functions(&file);
2483         if (ret < 0)
2484                 goto out;
2485         warnings += ret;
2486
2487         ret = validate_unwind_hints(&file);
2488         if (ret < 0)
2489                 goto out;
2490         warnings += ret;
2491
2492         if (!warnings) {
2493                 ret = validate_reachable_instructions(&file);
2494                 if (ret < 0)
2495                         goto out;
2496                 warnings += ret;
2497         }
2498
2499         if (orc) {
2500                 ret = create_orc(&file);
2501                 if (ret < 0)
2502                         goto out;
2503
2504                 ret = create_orc_sections(&file);
2505                 if (ret < 0)
2506                         goto out;
2507
2508                 ret = elf_write(file.elf);
2509                 if (ret < 0)
2510                         goto out;
2511         }
2512
2513 out:
2514         cleanup(&file);
2515
2516         /* ignore warnings for now until we get all the code cleaned up */
2517         if (ret || warnings)
2518                 return 0;
2519         return 0;
2520 }