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