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