]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/objtool/builtin-check.c
Merge branch 'irq-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.git] / tools / objtool / builtin-check.c
1 /*
2  * Copyright (C) 2015 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 /*
19  * objtool check:
20  *
21  * This command analyzes every .o file and ensures the validity of its stack
22  * trace metadata.  It enforces a set of rules on asm code and C inline
23  * assembly code so that stack traces can be reliable.
24  *
25  * For more information, see tools/objtool/Documentation/stack-validation.txt.
26  */
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <subcmd/parse-options.h>
31
32 #include "builtin.h"
33 #include "elf.h"
34 #include "special.h"
35 #include "arch.h"
36 #include "warn.h"
37
38 #include <linux/hashtable.h>
39 #include <linux/kernel.h>
40
41 #define STATE_FP_SAVED          0x1
42 #define STATE_FP_SETUP          0x2
43 #define STATE_FENTRY            0x4
44
45 struct instruction {
46         struct list_head list;
47         struct hlist_node hash;
48         struct section *sec;
49         unsigned long offset;
50         unsigned int len, state;
51         unsigned char type;
52         unsigned long immediate;
53         bool alt_group, visited, dead_end;
54         struct symbol *call_dest;
55         struct instruction *jump_dest;
56         struct list_head alts;
57         struct symbol *func;
58 };
59
60 struct alternative {
61         struct list_head list;
62         struct instruction *insn;
63 };
64
65 struct objtool_file {
66         struct elf *elf;
67         struct list_head insn_list;
68         DECLARE_HASHTABLE(insn_hash, 16);
69         struct section *rodata, *whitelist;
70         bool ignore_unreachables, c_file;
71 };
72
73 const char *objname;
74 static bool nofp;
75
76 static struct instruction *find_insn(struct objtool_file *file,
77                                      struct section *sec, unsigned long offset)
78 {
79         struct instruction *insn;
80
81         hash_for_each_possible(file->insn_hash, insn, hash, offset)
82                 if (insn->sec == sec && insn->offset == offset)
83                         return insn;
84
85         return NULL;
86 }
87
88 static struct instruction *next_insn_same_sec(struct objtool_file *file,
89                                               struct instruction *insn)
90 {
91         struct instruction *next = list_next_entry(insn, list);
92
93         if (&next->list == &file->insn_list || next->sec != insn->sec)
94                 return NULL;
95
96         return next;
97 }
98
99 static bool gcov_enabled(struct objtool_file *file)
100 {
101         struct section *sec;
102         struct symbol *sym;
103
104         list_for_each_entry(sec, &file->elf->sections, list)
105                 list_for_each_entry(sym, &sec->symbol_list, list)
106                         if (!strncmp(sym->name, "__gcov_.", 8))
107                                 return true;
108
109         return false;
110 }
111
112 #define for_each_insn(file, insn)                                       \
113         list_for_each_entry(insn, &file->insn_list, list)
114
115 #define func_for_each_insn(file, func, insn)                            \
116         for (insn = find_insn(file, func->sec, func->offset);           \
117              insn && &insn->list != &file->insn_list &&                 \
118                 insn->sec == func->sec &&                               \
119                 insn->offset < func->offset + func->len;                \
120              insn = list_next_entry(insn, list))
121
122 #define func_for_each_insn_continue_reverse(file, func, insn)           \
123         for (insn = list_prev_entry(insn, list);                        \
124              &insn->list != &file->insn_list &&                         \
125                 insn->sec == func->sec && insn->offset >= func->offset; \
126              insn = list_prev_entry(insn, list))
127
128 #define sec_for_each_insn_from(file, insn)                              \
129         for (; insn; insn = next_insn_same_sec(file, insn))
130
131
132 /*
133  * Check if the function has been manually whitelisted with the
134  * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
135  * due to its use of a context switching instruction.
136  */
137 static bool ignore_func(struct objtool_file *file, struct symbol *func)
138 {
139         struct rela *rela;
140         struct instruction *insn;
141
142         /* check for STACK_FRAME_NON_STANDARD */
143         if (file->whitelist && file->whitelist->rela)
144                 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) {
145                         if (rela->sym->type == STT_SECTION &&
146                             rela->sym->sec == func->sec &&
147                             rela->addend == func->offset)
148                                 return true;
149                         if (rela->sym->type == STT_FUNC && rela->sym == func)
150                                 return true;
151                 }
152
153         /* check if it has a context switching instruction */
154         func_for_each_insn(file, func, insn)
155                 if (insn->type == INSN_CONTEXT_SWITCH)
156                         return true;
157
158         return false;
159 }
160
161 /*
162  * This checks to see if the given function is a "noreturn" function.
163  *
164  * For global functions which are outside the scope of this object file, we
165  * have to keep a manual list of them.
166  *
167  * For local functions, we have to detect them manually by simply looking for
168  * the lack of a return instruction.
169  *
170  * Returns:
171  *  -1: error
172  *   0: no dead end
173  *   1: dead end
174  */
175 static int __dead_end_function(struct objtool_file *file, struct symbol *func,
176                                int recursion)
177 {
178         int i;
179         struct instruction *insn;
180         bool empty = true;
181
182         /*
183          * Unfortunately these have to be hard coded because the noreturn
184          * attribute isn't provided in ELF data.
185          */
186         static const char * const global_noreturns[] = {
187                 "__stack_chk_fail",
188                 "panic",
189                 "do_exit",
190                 "do_task_dead",
191                 "__module_put_and_exit",
192                 "complete_and_exit",
193                 "kvm_spurious_fault",
194                 "__reiserfs_panic",
195                 "lbug_with_loc",
196                 "fortify_panic",
197         };
198
199         if (func->bind == STB_WEAK)
200                 return 0;
201
202         if (func->bind == STB_GLOBAL)
203                 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
204                         if (!strcmp(func->name, global_noreturns[i]))
205                                 return 1;
206
207         if (!func->sec)
208                 return 0;
209
210         func_for_each_insn(file, func, insn) {
211                 empty = false;
212
213                 if (insn->type == INSN_RETURN)
214                         return 0;
215         }
216
217         if (empty)
218                 return 0;
219
220         /*
221          * A function can have a sibling call instead of a return.  In that
222          * case, the function's dead-end status depends on whether the target
223          * of the sibling call returns.
224          */
225         func_for_each_insn(file, func, insn) {
226                 if (insn->sec != func->sec ||
227                     insn->offset >= func->offset + func->len)
228                         break;
229
230                 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
231                         struct instruction *dest = insn->jump_dest;
232                         struct symbol *dest_func;
233
234                         if (!dest)
235                                 /* sibling call to another file */
236                                 return 0;
237
238                         if (dest->sec != func->sec ||
239                             dest->offset < func->offset ||
240                             dest->offset >= func->offset + func->len) {
241                                 /* local sibling call */
242                                 dest_func = find_symbol_by_offset(dest->sec,
243                                                                   dest->offset);
244                                 if (!dest_func)
245                                         continue;
246
247                                 if (recursion == 5) {
248                                         WARN_FUNC("infinite recursion (objtool bug!)",
249                                                   dest->sec, dest->offset);
250                                         return -1;
251                                 }
252
253                                 return __dead_end_function(file, dest_func,
254                                                            recursion + 1);
255                         }
256                 }
257
258                 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
259                         /* sibling call */
260                         return 0;
261         }
262
263         return 1;
264 }
265
266 static int dead_end_function(struct objtool_file *file, struct symbol *func)
267 {
268         return __dead_end_function(file, func, 0);
269 }
270
271 /*
272  * Call the arch-specific instruction decoder for all the instructions and add
273  * them to the global instruction list.
274  */
275 static int decode_instructions(struct objtool_file *file)
276 {
277         struct section *sec;
278         struct symbol *func;
279         unsigned long offset;
280         struct instruction *insn;
281         int ret;
282
283         list_for_each_entry(sec, &file->elf->sections, list) {
284
285                 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
286                         continue;
287
288                 for (offset = 0; offset < sec->len; offset += insn->len) {
289                         insn = malloc(sizeof(*insn));
290                         memset(insn, 0, sizeof(*insn));
291
292                         INIT_LIST_HEAD(&insn->alts);
293                         insn->sec = sec;
294                         insn->offset = offset;
295
296                         ret = arch_decode_instruction(file->elf, sec, offset,
297                                                       sec->len - offset,
298                                                       &insn->len, &insn->type,
299                                                       &insn->immediate);
300                         if (ret)
301                                 return ret;
302
303                         if (!insn->type || insn->type > INSN_LAST) {
304                                 WARN_FUNC("invalid instruction type %d",
305                                           insn->sec, insn->offset, insn->type);
306                                 return -1;
307                         }
308
309                         hash_add(file->insn_hash, &insn->hash, insn->offset);
310                         list_add_tail(&insn->list, &file->insn_list);
311                 }
312
313                 list_for_each_entry(func, &sec->symbol_list, list) {
314                         if (func->type != STT_FUNC)
315                                 continue;
316
317                         if (!find_insn(file, sec, func->offset)) {
318                                 WARN("%s(): can't find starting instruction",
319                                      func->name);
320                                 return -1;
321                         }
322
323                         func_for_each_insn(file, func, insn)
324                                 if (!insn->func)
325                                         insn->func = func;
326                 }
327         }
328
329         return 0;
330 }
331
332 /*
333  * Find all uses of the unreachable() macro, which are code path dead ends.
334  */
335 static int add_dead_ends(struct objtool_file *file)
336 {
337         struct section *sec;
338         struct rela *rela;
339         struct instruction *insn;
340         bool found;
341
342         sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
343         if (!sec)
344                 return 0;
345
346         list_for_each_entry(rela, &sec->rela_list, list) {
347                 if (rela->sym->type != STT_SECTION) {
348                         WARN("unexpected relocation symbol type in %s", sec->name);
349                         return -1;
350                 }
351                 insn = find_insn(file, rela->sym->sec, rela->addend);
352                 if (insn)
353                         insn = list_prev_entry(insn, list);
354                 else if (rela->addend == rela->sym->sec->len) {
355                         found = false;
356                         list_for_each_entry_reverse(insn, &file->insn_list, list) {
357                                 if (insn->sec == rela->sym->sec) {
358                                         found = true;
359                                         break;
360                                 }
361                         }
362
363                         if (!found) {
364                                 WARN("can't find unreachable insn at %s+0x%x",
365                                      rela->sym->sec->name, rela->addend);
366                                 return -1;
367                         }
368                 } else {
369                         WARN("can't find unreachable insn at %s+0x%x",
370                              rela->sym->sec->name, rela->addend);
371                         return -1;
372                 }
373
374                 insn->dead_end = true;
375         }
376
377         return 0;
378 }
379
380 /*
381  * Warnings shouldn't be reported for ignored functions.
382  */
383 static void add_ignores(struct objtool_file *file)
384 {
385         struct instruction *insn;
386         struct section *sec;
387         struct symbol *func;
388
389         list_for_each_entry(sec, &file->elf->sections, list) {
390                 list_for_each_entry(func, &sec->symbol_list, list) {
391                         if (func->type != STT_FUNC)
392                                 continue;
393
394                         if (!ignore_func(file, func))
395                                 continue;
396
397                         func_for_each_insn(file, func, insn)
398                                 insn->visited = true;
399                 }
400         }
401 }
402
403 /*
404  * Find the destination instructions for all jumps.
405  */
406 static int add_jump_destinations(struct objtool_file *file)
407 {
408         struct instruction *insn;
409         struct rela *rela;
410         struct section *dest_sec;
411         unsigned long dest_off;
412
413         for_each_insn(file, insn) {
414                 if (insn->type != INSN_JUMP_CONDITIONAL &&
415                     insn->type != INSN_JUMP_UNCONDITIONAL)
416                         continue;
417
418                 /* skip ignores */
419                 if (insn->visited)
420                         continue;
421
422                 rela = find_rela_by_dest_range(insn->sec, insn->offset,
423                                                insn->len);
424                 if (!rela) {
425                         dest_sec = insn->sec;
426                         dest_off = insn->offset + insn->len + insn->immediate;
427                 } else if (rela->sym->type == STT_SECTION) {
428                         dest_sec = rela->sym->sec;
429                         dest_off = rela->addend + 4;
430                 } else if (rela->sym->sec->idx) {
431                         dest_sec = rela->sym->sec;
432                         dest_off = rela->sym->sym.st_value + rela->addend + 4;
433                 } else {
434                         /* sibling call */
435                         insn->jump_dest = 0;
436                         continue;
437                 }
438
439                 insn->jump_dest = find_insn(file, dest_sec, dest_off);
440                 if (!insn->jump_dest) {
441
442                         /*
443                          * This is a special case where an alt instruction
444                          * jumps past the end of the section.  These are
445                          * handled later in handle_group_alt().
446                          */
447                         if (!strcmp(insn->sec->name, ".altinstr_replacement"))
448                                 continue;
449
450                         WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
451                                   insn->sec, insn->offset, dest_sec->name,
452                                   dest_off);
453                         return -1;
454                 }
455         }
456
457         return 0;
458 }
459
460 /*
461  * Find the destination instructions for all calls.
462  */
463 static int add_call_destinations(struct objtool_file *file)
464 {
465         struct instruction *insn;
466         unsigned long dest_off;
467         struct rela *rela;
468
469         for_each_insn(file, insn) {
470                 if (insn->type != INSN_CALL)
471                         continue;
472
473                 rela = find_rela_by_dest_range(insn->sec, insn->offset,
474                                                insn->len);
475                 if (!rela) {
476                         dest_off = insn->offset + insn->len + insn->immediate;
477                         insn->call_dest = find_symbol_by_offset(insn->sec,
478                                                                 dest_off);
479                         if (!insn->call_dest) {
480                                 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
481                                           insn->sec, insn->offset, dest_off);
482                                 return -1;
483                         }
484                 } else if (rela->sym->type == STT_SECTION) {
485                         insn->call_dest = find_symbol_by_offset(rela->sym->sec,
486                                                                 rela->addend+4);
487                         if (!insn->call_dest ||
488                             insn->call_dest->type != STT_FUNC) {
489                                 WARN_FUNC("can't find call dest symbol at %s+0x%x",
490                                           insn->sec, insn->offset,
491                                           rela->sym->sec->name,
492                                           rela->addend + 4);
493                                 return -1;
494                         }
495                 } else
496                         insn->call_dest = rela->sym;
497         }
498
499         return 0;
500 }
501
502 /*
503  * The .alternatives section requires some extra special care, over and above
504  * what other special sections require:
505  *
506  * 1. Because alternatives are patched in-place, we need to insert a fake jump
507  *    instruction at the end so that validate_branch() skips all the original
508  *    replaced instructions when validating the new instruction path.
509  *
510  * 2. An added wrinkle is that the new instruction length might be zero.  In
511  *    that case the old instructions are replaced with noops.  We simulate that
512  *    by creating a fake jump as the only new instruction.
513  *
514  * 3. In some cases, the alternative section includes an instruction which
515  *    conditionally jumps to the _end_ of the entry.  We have to modify these
516  *    jumps' destinations to point back to .text rather than the end of the
517  *    entry in .altinstr_replacement.
518  *
519  * 4. It has been requested that we don't validate the !POPCNT feature path
520  *    which is a "very very small percentage of machines".
521  */
522 static int handle_group_alt(struct objtool_file *file,
523                             struct special_alt *special_alt,
524                             struct instruction *orig_insn,
525                             struct instruction **new_insn)
526 {
527         struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump;
528         unsigned long dest_off;
529
530         last_orig_insn = NULL;
531         insn = orig_insn;
532         sec_for_each_insn_from(file, insn) {
533                 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
534                         break;
535
536                 if (special_alt->skip_orig)
537                         insn->type = INSN_NOP;
538
539                 insn->alt_group = true;
540                 last_orig_insn = insn;
541         }
542
543         if (!next_insn_same_sec(file, last_orig_insn)) {
544                 WARN("%s: don't know how to handle alternatives at end of section",
545                      special_alt->orig_sec->name);
546                 return -1;
547         }
548
549         fake_jump = malloc(sizeof(*fake_jump));
550         if (!fake_jump) {
551                 WARN("malloc failed");
552                 return -1;
553         }
554         memset(fake_jump, 0, sizeof(*fake_jump));
555         INIT_LIST_HEAD(&fake_jump->alts);
556         fake_jump->sec = special_alt->new_sec;
557         fake_jump->offset = -1;
558         fake_jump->type = INSN_JUMP_UNCONDITIONAL;
559         fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
560
561         if (!special_alt->new_len) {
562                 *new_insn = fake_jump;
563                 return 0;
564         }
565
566         last_new_insn = NULL;
567         insn = *new_insn;
568         sec_for_each_insn_from(file, insn) {
569                 if (insn->offset >= special_alt->new_off + special_alt->new_len)
570                         break;
571
572                 last_new_insn = insn;
573
574                 if (insn->type != INSN_JUMP_CONDITIONAL &&
575                     insn->type != INSN_JUMP_UNCONDITIONAL)
576                         continue;
577
578                 if (!insn->immediate)
579                         continue;
580
581                 dest_off = insn->offset + insn->len + insn->immediate;
582                 if (dest_off == special_alt->new_off + special_alt->new_len)
583                         insn->jump_dest = fake_jump;
584
585                 if (!insn->jump_dest) {
586                         WARN_FUNC("can't find alternative jump destination",
587                                   insn->sec, insn->offset);
588                         return -1;
589                 }
590         }
591
592         if (!last_new_insn) {
593                 WARN_FUNC("can't find last new alternative instruction",
594                           special_alt->new_sec, special_alt->new_off);
595                 return -1;
596         }
597
598         list_add(&fake_jump->list, &last_new_insn->list);
599
600         return 0;
601 }
602
603 /*
604  * A jump table entry can either convert a nop to a jump or a jump to a nop.
605  * If the original instruction is a jump, make the alt entry an effective nop
606  * by just skipping the original instruction.
607  */
608 static int handle_jump_alt(struct objtool_file *file,
609                            struct special_alt *special_alt,
610                            struct instruction *orig_insn,
611                            struct instruction **new_insn)
612 {
613         if (orig_insn->type == INSN_NOP)
614                 return 0;
615
616         if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
617                 WARN_FUNC("unsupported instruction at jump label",
618                           orig_insn->sec, orig_insn->offset);
619                 return -1;
620         }
621
622         *new_insn = list_next_entry(orig_insn, list);
623         return 0;
624 }
625
626 /*
627  * Read all the special sections which have alternate instructions which can be
628  * patched in or redirected to at runtime.  Each instruction having alternate
629  * instruction(s) has them added to its insn->alts list, which will be
630  * traversed in validate_branch().
631  */
632 static int add_special_section_alts(struct objtool_file *file)
633 {
634         struct list_head special_alts;
635         struct instruction *orig_insn, *new_insn;
636         struct special_alt *special_alt, *tmp;
637         struct alternative *alt;
638         int ret;
639
640         ret = special_get_alts(file->elf, &special_alts);
641         if (ret)
642                 return ret;
643
644         list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
645                 alt = malloc(sizeof(*alt));
646                 if (!alt) {
647                         WARN("malloc failed");
648                         ret = -1;
649                         goto out;
650                 }
651
652                 orig_insn = find_insn(file, special_alt->orig_sec,
653                                       special_alt->orig_off);
654                 if (!orig_insn) {
655                         WARN_FUNC("special: can't find orig instruction",
656                                   special_alt->orig_sec, special_alt->orig_off);
657                         ret = -1;
658                         goto out;
659                 }
660
661                 new_insn = NULL;
662                 if (!special_alt->group || special_alt->new_len) {
663                         new_insn = find_insn(file, special_alt->new_sec,
664                                              special_alt->new_off);
665                         if (!new_insn) {
666                                 WARN_FUNC("special: can't find new instruction",
667                                           special_alt->new_sec,
668                                           special_alt->new_off);
669                                 ret = -1;
670                                 goto out;
671                         }
672                 }
673
674                 if (special_alt->group) {
675                         ret = handle_group_alt(file, special_alt, orig_insn,
676                                                &new_insn);
677                         if (ret)
678                                 goto out;
679                 } else if (special_alt->jump_or_nop) {
680                         ret = handle_jump_alt(file, special_alt, orig_insn,
681                                               &new_insn);
682                         if (ret)
683                                 goto out;
684                 }
685
686                 alt->insn = new_insn;
687                 list_add_tail(&alt->list, &orig_insn->alts);
688
689                 list_del(&special_alt->list);
690                 free(special_alt);
691         }
692
693 out:
694         return ret;
695 }
696
697 static int add_switch_table(struct objtool_file *file, struct symbol *func,
698                             struct instruction *insn, struct rela *table,
699                             struct rela *next_table)
700 {
701         struct rela *rela = table;
702         struct instruction *alt_insn;
703         struct alternative *alt;
704
705         list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
706                 if (rela == next_table)
707                         break;
708
709                 if (rela->sym->sec != insn->sec ||
710                     rela->addend <= func->offset ||
711                     rela->addend >= func->offset + func->len)
712                         break;
713
714                 alt_insn = find_insn(file, insn->sec, rela->addend);
715                 if (!alt_insn) {
716                         WARN("%s: can't find instruction at %s+0x%x",
717                              file->rodata->rela->name, insn->sec->name,
718                              rela->addend);
719                         return -1;
720                 }
721
722                 alt = malloc(sizeof(*alt));
723                 if (!alt) {
724                         WARN("malloc failed");
725                         return -1;
726                 }
727
728                 alt->insn = alt_insn;
729                 list_add_tail(&alt->list, &insn->alts);
730         }
731
732         return 0;
733 }
734
735 /*
736  * find_switch_table() - Given a dynamic jump, find the switch jump table in
737  * .rodata associated with it.
738  *
739  * There are 3 basic patterns:
740  *
741  * 1. jmpq *[rodata addr](,%reg,8)
742  *
743  *    This is the most common case by far.  It jumps to an address in a simple
744  *    jump table which is stored in .rodata.
745  *
746  * 2. jmpq *[rodata addr](%rip)
747  *
748  *    This is caused by a rare GCC quirk, currently only seen in three driver
749  *    functions in the kernel, only with certain obscure non-distro configs.
750  *
751  *    As part of an optimization, GCC makes a copy of an existing switch jump
752  *    table, modifies it, and then hard-codes the jump (albeit with an indirect
753  *    jump) to use a single entry in the table.  The rest of the jump table and
754  *    some of its jump targets remain as dead code.
755  *
756  *    In such a case we can just crudely ignore all unreachable instruction
757  *    warnings for the entire object file.  Ideally we would just ignore them
758  *    for the function, but that would require redesigning the code quite a
759  *    bit.  And honestly that's just not worth doing: unreachable instruction
760  *    warnings are of questionable value anyway, and this is such a rare issue.
761  *
762  * 3. mov [rodata addr],%reg1
763  *    ... some instructions ...
764  *    jmpq *(%reg1,%reg2,8)
765  *
766  *    This is a fairly uncommon pattern which is new for GCC 6.  As of this
767  *    writing, there are 11 occurrences of it in the allmodconfig kernel.
768  *
769  *    TODO: Once we have DWARF CFI and smarter instruction decoding logic,
770  *    ensure the same register is used in the mov and jump instructions.
771  */
772 static struct rela *find_switch_table(struct objtool_file *file,
773                                       struct symbol *func,
774                                       struct instruction *insn)
775 {
776         struct rela *text_rela, *rodata_rela;
777         struct instruction *orig_insn = insn;
778
779         text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
780         if (text_rela && text_rela->sym == file->rodata->sym) {
781                 /* case 1 */
782                 rodata_rela = find_rela_by_dest(file->rodata,
783                                                 text_rela->addend);
784                 if (rodata_rela)
785                         return rodata_rela;
786
787                 /* case 2 */
788                 rodata_rela = find_rela_by_dest(file->rodata,
789                                                 text_rela->addend + 4);
790                 if (!rodata_rela)
791                         return NULL;
792                 file->ignore_unreachables = true;
793                 return rodata_rela;
794         }
795
796         /* case 3 */
797         func_for_each_insn_continue_reverse(file, func, insn) {
798                 if (insn->type == INSN_JUMP_DYNAMIC)
799                         break;
800
801                 /* allow small jumps within the range */
802                 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
803                     insn->jump_dest &&
804                     (insn->jump_dest->offset <= insn->offset ||
805                      insn->jump_dest->offset > orig_insn->offset))
806                     break;
807
808                 /* look for a relocation which references .rodata */
809                 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
810                                                     insn->len);
811                 if (!text_rela || text_rela->sym != file->rodata->sym)
812                         continue;
813
814                 /*
815                  * Make sure the .rodata address isn't associated with a
816                  * symbol.  gcc jump tables are anonymous data.
817                  */
818                 if (find_symbol_containing(file->rodata, text_rela->addend))
819                         continue;
820
821                 return find_rela_by_dest(file->rodata, text_rela->addend);
822         }
823
824         return NULL;
825 }
826
827 static int add_func_switch_tables(struct objtool_file *file,
828                                   struct symbol *func)
829 {
830         struct instruction *insn, *prev_jump = NULL;
831         struct rela *rela, *prev_rela = NULL;
832         int ret;
833
834         func_for_each_insn(file, func, insn) {
835                 if (insn->type != INSN_JUMP_DYNAMIC)
836                         continue;
837
838                 rela = find_switch_table(file, func, insn);
839                 if (!rela)
840                         continue;
841
842                 /*
843                  * We found a switch table, but we don't know yet how big it
844                  * is.  Don't add it until we reach the end of the function or
845                  * the beginning of another switch table in the same function.
846                  */
847                 if (prev_jump) {
848                         ret = add_switch_table(file, func, prev_jump, prev_rela,
849                                                rela);
850                         if (ret)
851                                 return ret;
852                 }
853
854                 prev_jump = insn;
855                 prev_rela = rela;
856         }
857
858         if (prev_jump) {
859                 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
860                 if (ret)
861                         return ret;
862         }
863
864         return 0;
865 }
866
867 /*
868  * For some switch statements, gcc generates a jump table in the .rodata
869  * section which contains a list of addresses within the function to jump to.
870  * This finds these jump tables and adds them to the insn->alts lists.
871  */
872 static int add_switch_table_alts(struct objtool_file *file)
873 {
874         struct section *sec;
875         struct symbol *func;
876         int ret;
877
878         if (!file->rodata || !file->rodata->rela)
879                 return 0;
880
881         list_for_each_entry(sec, &file->elf->sections, list) {
882                 list_for_each_entry(func, &sec->symbol_list, list) {
883                         if (func->type != STT_FUNC)
884                                 continue;
885
886                         ret = add_func_switch_tables(file, func);
887                         if (ret)
888                                 return ret;
889                 }
890         }
891
892         return 0;
893 }
894
895 static int decode_sections(struct objtool_file *file)
896 {
897         int ret;
898
899         ret = decode_instructions(file);
900         if (ret)
901                 return ret;
902
903         ret = add_dead_ends(file);
904         if (ret)
905                 return ret;
906
907         add_ignores(file);
908
909         ret = add_jump_destinations(file);
910         if (ret)
911                 return ret;
912
913         ret = add_call_destinations(file);
914         if (ret)
915                 return ret;
916
917         ret = add_special_section_alts(file);
918         if (ret)
919                 return ret;
920
921         ret = add_switch_table_alts(file);
922         if (ret)
923                 return ret;
924
925         return 0;
926 }
927
928 static bool is_fentry_call(struct instruction *insn)
929 {
930         if (insn->type == INSN_CALL &&
931             insn->call_dest->type == STT_NOTYPE &&
932             !strcmp(insn->call_dest->name, "__fentry__"))
933                 return true;
934
935         return false;
936 }
937
938 static bool has_modified_stack_frame(struct instruction *insn)
939 {
940         return (insn->state & STATE_FP_SAVED) ||
941                (insn->state & STATE_FP_SETUP);
942 }
943
944 static bool has_valid_stack_frame(struct instruction *insn)
945 {
946         return (insn->state & STATE_FP_SAVED) &&
947                (insn->state & STATE_FP_SETUP);
948 }
949
950 static unsigned int frame_state(unsigned long state)
951 {
952         return (state & (STATE_FP_SAVED | STATE_FP_SETUP));
953 }
954
955 /*
956  * Follow the branch starting at the given instruction, and recursively follow
957  * any other branches (jumps).  Meanwhile, track the frame pointer state at
958  * each instruction and validate all the rules described in
959  * tools/objtool/Documentation/stack-validation.txt.
960  */
961 static int validate_branch(struct objtool_file *file,
962                            struct instruction *first, unsigned char first_state)
963 {
964         struct alternative *alt;
965         struct instruction *insn;
966         struct section *sec;
967         struct symbol *func = NULL;
968         unsigned char state;
969         int ret;
970
971         insn = first;
972         sec = insn->sec;
973         state = first_state;
974
975         if (insn->alt_group && list_empty(&insn->alts)) {
976                 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
977                           sec, insn->offset);
978                 return 1;
979         }
980
981         while (1) {
982                 if (file->c_file && insn->func) {
983                         if (func && func != insn->func) {
984                                 WARN("%s() falls through to next function %s()",
985                                      func->name, insn->func->name);
986                                 return 1;
987                         }
988
989                         func = insn->func;
990                 }
991
992                 if (insn->visited) {
993                         if (frame_state(insn->state) != frame_state(state)) {
994                                 WARN_FUNC("frame pointer state mismatch",
995                                           sec, insn->offset);
996                                 return 1;
997                         }
998
999                         return 0;
1000                 }
1001
1002                 insn->visited = true;
1003                 insn->state = state;
1004
1005                 list_for_each_entry(alt, &insn->alts, list) {
1006                         ret = validate_branch(file, alt->insn, state);
1007                         if (ret)
1008                                 return 1;
1009                 }
1010
1011                 switch (insn->type) {
1012
1013                 case INSN_FP_SAVE:
1014                         if (!nofp) {
1015                                 if (state & STATE_FP_SAVED) {
1016                                         WARN_FUNC("duplicate frame pointer save",
1017                                                   sec, insn->offset);
1018                                         return 1;
1019                                 }
1020                                 state |= STATE_FP_SAVED;
1021                         }
1022                         break;
1023
1024                 case INSN_FP_SETUP:
1025                         if (!nofp) {
1026                                 if (state & STATE_FP_SETUP) {
1027                                         WARN_FUNC("duplicate frame pointer setup",
1028                                                   sec, insn->offset);
1029                                         return 1;
1030                                 }
1031                                 state |= STATE_FP_SETUP;
1032                         }
1033                         break;
1034
1035                 case INSN_FP_RESTORE:
1036                         if (!nofp) {
1037                                 if (has_valid_stack_frame(insn))
1038                                         state &= ~STATE_FP_SETUP;
1039
1040                                 state &= ~STATE_FP_SAVED;
1041                         }
1042                         break;
1043
1044                 case INSN_RETURN:
1045                         if (!nofp && has_modified_stack_frame(insn)) {
1046                                 WARN_FUNC("return without frame pointer restore",
1047                                           sec, insn->offset);
1048                                 return 1;
1049                         }
1050                         return 0;
1051
1052                 case INSN_CALL:
1053                         if (is_fentry_call(insn)) {
1054                                 state |= STATE_FENTRY;
1055                                 break;
1056                         }
1057
1058                         ret = dead_end_function(file, insn->call_dest);
1059                         if (ret == 1)
1060                                 return 0;
1061                         if (ret == -1)
1062                                 return 1;
1063
1064                         /* fallthrough */
1065                 case INSN_CALL_DYNAMIC:
1066                         if (!nofp && !has_valid_stack_frame(insn)) {
1067                                 WARN_FUNC("call without frame pointer save/setup",
1068                                           sec, insn->offset);
1069                                 return 1;
1070                         }
1071                         break;
1072
1073                 case INSN_JUMP_CONDITIONAL:
1074                 case INSN_JUMP_UNCONDITIONAL:
1075                         if (insn->jump_dest) {
1076                                 ret = validate_branch(file, insn->jump_dest,
1077                                                       state);
1078                                 if (ret)
1079                                         return 1;
1080                         } else if (has_modified_stack_frame(insn)) {
1081                                 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
1082                                           sec, insn->offset);
1083                                 return 1;
1084                         } /* else it's a sibling call */
1085
1086                         if (insn->type == INSN_JUMP_UNCONDITIONAL)
1087                                 return 0;
1088
1089                         break;
1090
1091                 case INSN_JUMP_DYNAMIC:
1092                         if (list_empty(&insn->alts) &&
1093                             has_modified_stack_frame(insn)) {
1094                                 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
1095                                           sec, insn->offset);
1096                                 return 1;
1097                         }
1098
1099                         return 0;
1100
1101                 default:
1102                         break;
1103                 }
1104
1105                 if (insn->dead_end)
1106                         return 0;
1107
1108                 insn = next_insn_same_sec(file, insn);
1109                 if (!insn) {
1110                         WARN("%s: unexpected end of section", sec->name);
1111                         return 1;
1112                 }
1113         }
1114
1115         return 0;
1116 }
1117
1118 static bool is_kasan_insn(struct instruction *insn)
1119 {
1120         return (insn->type == INSN_CALL &&
1121                 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1122 }
1123
1124 static bool is_ubsan_insn(struct instruction *insn)
1125 {
1126         return (insn->type == INSN_CALL &&
1127                 !strcmp(insn->call_dest->name,
1128                         "__ubsan_handle_builtin_unreachable"));
1129 }
1130
1131 static bool ignore_unreachable_insn(struct symbol *func,
1132                                     struct instruction *insn)
1133 {
1134         int i;
1135
1136         if (insn->type == INSN_NOP)
1137                 return true;
1138
1139         /*
1140          * Check if this (or a subsequent) instruction is related to
1141          * CONFIG_UBSAN or CONFIG_KASAN.
1142          *
1143          * End the search at 5 instructions to avoid going into the weeds.
1144          */
1145         for (i = 0; i < 5; i++) {
1146
1147                 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
1148                         return true;
1149
1150                 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
1151                         insn = insn->jump_dest;
1152                         continue;
1153                 }
1154
1155                 if (insn->offset + insn->len >= func->offset + func->len)
1156                         break;
1157                 insn = list_next_entry(insn, list);
1158         }
1159
1160         return false;
1161 }
1162
1163 static int validate_functions(struct objtool_file *file)
1164 {
1165         struct section *sec;
1166         struct symbol *func;
1167         struct instruction *insn;
1168         int ret, warnings = 0;
1169
1170         list_for_each_entry(sec, &file->elf->sections, list) {
1171                 list_for_each_entry(func, &sec->symbol_list, list) {
1172                         if (func->type != STT_FUNC)
1173                                 continue;
1174
1175                         insn = find_insn(file, sec, func->offset);
1176                         if (!insn)
1177                                 continue;
1178
1179                         ret = validate_branch(file, insn, 0);
1180                         warnings += ret;
1181                 }
1182         }
1183
1184         list_for_each_entry(sec, &file->elf->sections, list) {
1185                 list_for_each_entry(func, &sec->symbol_list, list) {
1186                         if (func->type != STT_FUNC)
1187                                 continue;
1188
1189                         func_for_each_insn(file, func, insn) {
1190                                 if (insn->visited)
1191                                         continue;
1192
1193                                 insn->visited = true;
1194
1195                                 if (file->ignore_unreachables || warnings ||
1196                                     ignore_unreachable_insn(func, insn))
1197                                         continue;
1198
1199                                 /*
1200                                  * gcov produces a lot of unreachable
1201                                  * instructions.  If we get an unreachable
1202                                  * warning and the file has gcov enabled, just
1203                                  * ignore it, and all other such warnings for
1204                                  * the file.
1205                                  */
1206                                 if (!file->ignore_unreachables &&
1207                                     gcov_enabled(file)) {
1208                                         file->ignore_unreachables = true;
1209                                         continue;
1210                                 }
1211
1212                                 WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset);
1213                                 warnings++;
1214                         }
1215                 }
1216         }
1217
1218         return warnings;
1219 }
1220
1221 static int validate_uncallable_instructions(struct objtool_file *file)
1222 {
1223         struct instruction *insn;
1224         int warnings = 0;
1225
1226         for_each_insn(file, insn) {
1227                 if (!insn->visited && insn->type == INSN_RETURN) {
1228                         WARN_FUNC("return instruction outside of a callable function",
1229                                   insn->sec, insn->offset);
1230                         warnings++;
1231                 }
1232         }
1233
1234         return warnings;
1235 }
1236
1237 static void cleanup(struct objtool_file *file)
1238 {
1239         struct instruction *insn, *tmpinsn;
1240         struct alternative *alt, *tmpalt;
1241
1242         list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
1243                 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
1244                         list_del(&alt->list);
1245                         free(alt);
1246                 }
1247                 list_del(&insn->list);
1248                 hash_del(&insn->hash);
1249                 free(insn);
1250         }
1251         elf_close(file->elf);
1252 }
1253
1254 const char * const check_usage[] = {
1255         "objtool check [<options>] file.o",
1256         NULL,
1257 };
1258
1259 int cmd_check(int argc, const char **argv)
1260 {
1261         struct objtool_file file;
1262         int ret, warnings = 0;
1263
1264         const struct option options[] = {
1265                 OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"),
1266                 OPT_END(),
1267         };
1268
1269         argc = parse_options(argc, argv, options, check_usage, 0);
1270
1271         if (argc != 1)
1272                 usage_with_options(check_usage, options);
1273
1274         objname = argv[0];
1275
1276         file.elf = elf_open(objname);
1277         if (!file.elf) {
1278                 fprintf(stderr, "error reading elf file %s\n", objname);
1279                 return 1;
1280         }
1281
1282         INIT_LIST_HEAD(&file.insn_list);
1283         hash_init(file.insn_hash);
1284         file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
1285         file.rodata = find_section_by_name(file.elf, ".rodata");
1286         file.ignore_unreachables = false;
1287         file.c_file = find_section_by_name(file.elf, ".comment");
1288
1289         ret = decode_sections(&file);
1290         if (ret < 0)
1291                 goto out;
1292         warnings += ret;
1293
1294         ret = validate_functions(&file);
1295         if (ret < 0)
1296                 goto out;
1297         warnings += ret;
1298
1299         ret = validate_uncallable_instructions(&file);
1300         if (ret < 0)
1301                 goto out;
1302         warnings += ret;
1303
1304 out:
1305         cleanup(&file);
1306
1307         /* ignore warnings for now until we get all the code cleaned up */
1308         if (ret || warnings)
1309                 return 0;
1310         return 0;
1311 }