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