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