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