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