]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - tools/objtool/check.c
x86: Clarify/fix no-op barriers for text_poke_bp()
[mirror_ubuntu-jammy-kernel.git] / tools / objtool / check.c
CommitLineData
dcc914f4
JP
1/*
2 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <string.h>
19#include <stdlib.h>
20
21#include "check.h"
22#include "elf.h"
23#include "special.h"
24#include "arch.h"
25#include "warn.h"
26
27#include <linux/hashtable.h>
28#include <linux/kernel.h>
29
dcc914f4
JP
30struct alternative {
31 struct list_head list;
32 struct instruction *insn;
33};
34
35const char *objname;
867ac9d7 36static bool no_fp;
baa41469 37struct cfi_state initial_func_cfi;
dcc914f4 38
627fce14
JP
39struct instruction *find_insn(struct objtool_file *file,
40 struct section *sec, unsigned long offset)
dcc914f4
JP
41{
42 struct instruction *insn;
43
44 hash_for_each_possible(file->insn_hash, insn, hash, offset)
45 if (insn->sec == sec && insn->offset == offset)
46 return insn;
47
48 return NULL;
49}
50
51static struct instruction *next_insn_same_sec(struct objtool_file *file,
52 struct instruction *insn)
53{
54 struct instruction *next = list_next_entry(insn, list);
55
baa41469 56 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
dcc914f4
JP
57 return NULL;
58
59 return next;
60}
61
dcc914f4
JP
62#define func_for_each_insn(file, func, insn) \
63 for (insn = find_insn(file, func->sec, func->offset); \
64 insn && &insn->list != &file->insn_list && \
65 insn->sec == func->sec && \
66 insn->offset < func->offset + func->len; \
67 insn = list_next_entry(insn, list))
68
69#define func_for_each_insn_continue_reverse(file, func, insn) \
70 for (insn = list_prev_entry(insn, list); \
71 &insn->list != &file->insn_list && \
72 insn->sec == func->sec && insn->offset >= func->offset; \
73 insn = list_prev_entry(insn, list))
74
75#define sec_for_each_insn_from(file, insn) \
76 for (; insn; insn = next_insn_same_sec(file, insn))
77
baa41469
JP
78#define sec_for_each_insn_continue(file, insn) \
79 for (insn = next_insn_same_sec(file, insn); insn; \
80 insn = next_insn_same_sec(file, insn))
dcc914f4
JP
81
82/*
83 * Check if the function has been manually whitelisted with the
84 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
85 * due to its use of a context switching instruction.
86 */
87static bool ignore_func(struct objtool_file *file, struct symbol *func)
88{
89 struct rela *rela;
dcc914f4
JP
90
91 /* check for STACK_FRAME_NON_STANDARD */
92 if (file->whitelist && file->whitelist->rela)
93 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) {
94 if (rela->sym->type == STT_SECTION &&
95 rela->sym->sec == func->sec &&
96 rela->addend == func->offset)
97 return true;
98 if (rela->sym->type == STT_FUNC && rela->sym == func)
99 return true;
100 }
101
dcc914f4
JP
102 return false;
103}
104
105/*
106 * This checks to see if the given function is a "noreturn" function.
107 *
108 * For global functions which are outside the scope of this object file, we
109 * have to keep a manual list of them.
110 *
111 * For local functions, we have to detect them manually by simply looking for
112 * the lack of a return instruction.
113 *
114 * Returns:
115 * -1: error
116 * 0: no dead end
117 * 1: dead end
118 */
119static int __dead_end_function(struct objtool_file *file, struct symbol *func,
120 int recursion)
121{
122 int i;
123 struct instruction *insn;
124 bool empty = true;
125
126 /*
127 * Unfortunately these have to be hard coded because the noreturn
128 * attribute isn't provided in ELF data.
129 */
130 static const char * const global_noreturns[] = {
131 "__stack_chk_fail",
132 "panic",
133 "do_exit",
134 "do_task_dead",
135 "__module_put_and_exit",
136 "complete_and_exit",
137 "kvm_spurious_fault",
138 "__reiserfs_panic",
139 "lbug_with_loc",
140 "fortify_panic",
141 };
142
143 if (func->bind == STB_WEAK)
144 return 0;
145
146 if (func->bind == STB_GLOBAL)
147 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
148 if (!strcmp(func->name, global_noreturns[i]))
149 return 1;
150
151 if (!func->sec)
152 return 0;
153
154 func_for_each_insn(file, func, insn) {
155 empty = false;
156
157 if (insn->type == INSN_RETURN)
158 return 0;
159 }
160
161 if (empty)
162 return 0;
163
164 /*
165 * A function can have a sibling call instead of a return. In that
166 * case, the function's dead-end status depends on whether the target
167 * of the sibling call returns.
168 */
169 func_for_each_insn(file, func, insn) {
170 if (insn->sec != func->sec ||
171 insn->offset >= func->offset + func->len)
172 break;
173
174 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
175 struct instruction *dest = insn->jump_dest;
176 struct symbol *dest_func;
177
178 if (!dest)
179 /* sibling call to another file */
180 return 0;
181
182 if (dest->sec != func->sec ||
183 dest->offset < func->offset ||
184 dest->offset >= func->offset + func->len) {
185 /* local sibling call */
186 dest_func = find_symbol_by_offset(dest->sec,
187 dest->offset);
188 if (!dest_func)
189 continue;
190
191 if (recursion == 5) {
192 WARN_FUNC("infinite recursion (objtool bug!)",
193 dest->sec, dest->offset);
194 return -1;
195 }
196
197 return __dead_end_function(file, dest_func,
198 recursion + 1);
199 }
200 }
201
202 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
203 /* sibling call */
204 return 0;
205 }
206
207 return 1;
208}
209
210static int dead_end_function(struct objtool_file *file, struct symbol *func)
211{
212 return __dead_end_function(file, func, 0);
213}
214
baa41469
JP
215static void clear_insn_state(struct insn_state *state)
216{
217 int i;
218
219 memset(state, 0, sizeof(*state));
220 state->cfa.base = CFI_UNDEFINED;
221 for (i = 0; i < CFI_NUM_REGS; i++)
222 state->regs[i].base = CFI_UNDEFINED;
223 state->drap_reg = CFI_UNDEFINED;
224}
225
dcc914f4
JP
226/*
227 * Call the arch-specific instruction decoder for all the instructions and add
228 * them to the global instruction list.
229 */
230static int decode_instructions(struct objtool_file *file)
231{
232 struct section *sec;
233 struct symbol *func;
234 unsigned long offset;
235 struct instruction *insn;
236 int ret;
237
baa41469 238 for_each_sec(file, sec) {
dcc914f4
JP
239
240 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
241 continue;
242
627fce14
JP
243 if (strcmp(sec->name, ".altinstr_replacement") &&
244 strcmp(sec->name, ".altinstr_aux") &&
245 strncmp(sec->name, ".discard.", 9))
246 sec->text = true;
247
dcc914f4
JP
248 for (offset = 0; offset < sec->len; offset += insn->len) {
249 insn = malloc(sizeof(*insn));
baa41469
JP
250 if (!insn) {
251 WARN("malloc failed");
252 return -1;
253 }
dcc914f4 254 memset(insn, 0, sizeof(*insn));
dcc914f4 255 INIT_LIST_HEAD(&insn->alts);
baa41469
JP
256 clear_insn_state(&insn->state);
257
dcc914f4
JP
258 insn->sec = sec;
259 insn->offset = offset;
260
261 ret = arch_decode_instruction(file->elf, sec, offset,
262 sec->len - offset,
263 &insn->len, &insn->type,
baa41469
JP
264 &insn->immediate,
265 &insn->stack_op);
dcc914f4
JP
266 if (ret)
267 return ret;
268
269 if (!insn->type || insn->type > INSN_LAST) {
270 WARN_FUNC("invalid instruction type %d",
271 insn->sec, insn->offset, insn->type);
272 return -1;
273 }
274
275 hash_add(file->insn_hash, &insn->hash, insn->offset);
276 list_add_tail(&insn->list, &file->insn_list);
277 }
278
279 list_for_each_entry(func, &sec->symbol_list, list) {
280 if (func->type != STT_FUNC)
281 continue;
282
283 if (!find_insn(file, sec, func->offset)) {
284 WARN("%s(): can't find starting instruction",
285 func->name);
286 return -1;
287 }
288
289 func_for_each_insn(file, func, insn)
290 if (!insn->func)
291 insn->func = func;
292 }
293 }
294
295 return 0;
296}
297
298/*
649ea4d5 299 * Mark "ud2" instructions and manually annotated dead ends.
dcc914f4
JP
300 */
301static int add_dead_ends(struct objtool_file *file)
302{
303 struct section *sec;
304 struct rela *rela;
305 struct instruction *insn;
306 bool found;
307
649ea4d5
JP
308 /*
309 * By default, "ud2" is a dead end unless otherwise annotated, because
310 * GCC 7 inserts it for certain divide-by-zero cases.
311 */
312 for_each_insn(file, insn)
313 if (insn->type == INSN_BUG)
314 insn->dead_end = true;
315
316 /*
317 * Check for manually annotated dead ends.
318 */
dcc914f4
JP
319 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
320 if (!sec)
649ea4d5 321 goto reachable;
dcc914f4
JP
322
323 list_for_each_entry(rela, &sec->rela_list, list) {
324 if (rela->sym->type != STT_SECTION) {
325 WARN("unexpected relocation symbol type in %s", sec->name);
326 return -1;
327 }
328 insn = find_insn(file, rela->sym->sec, rela->addend);
329 if (insn)
330 insn = list_prev_entry(insn, list);
331 else if (rela->addend == rela->sym->sec->len) {
332 found = false;
333 list_for_each_entry_reverse(insn, &file->insn_list, list) {
334 if (insn->sec == rela->sym->sec) {
335 found = true;
336 break;
337 }
338 }
339
340 if (!found) {
341 WARN("can't find unreachable insn at %s+0x%x",
342 rela->sym->sec->name, rela->addend);
343 return -1;
344 }
345 } else {
346 WARN("can't find unreachable insn at %s+0x%x",
347 rela->sym->sec->name, rela->addend);
348 return -1;
349 }
350
351 insn->dead_end = true;
352 }
353
649ea4d5
JP
354reachable:
355 /*
356 * These manually annotated reachable checks are needed for GCC 4.4,
357 * where the Linux unreachable() macro isn't supported. In that case
358 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
359 * not a dead end.
360 */
361 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
362 if (!sec)
363 return 0;
364
365 list_for_each_entry(rela, &sec->rela_list, list) {
366 if (rela->sym->type != STT_SECTION) {
367 WARN("unexpected relocation symbol type in %s", sec->name);
368 return -1;
369 }
370 insn = find_insn(file, rela->sym->sec, rela->addend);
371 if (insn)
372 insn = list_prev_entry(insn, list);
373 else if (rela->addend == rela->sym->sec->len) {
374 found = false;
375 list_for_each_entry_reverse(insn, &file->insn_list, list) {
376 if (insn->sec == rela->sym->sec) {
377 found = true;
378 break;
379 }
380 }
381
382 if (!found) {
383 WARN("can't find reachable insn at %s+0x%x",
384 rela->sym->sec->name, rela->addend);
385 return -1;
386 }
387 } else {
388 WARN("can't find reachable insn at %s+0x%x",
389 rela->sym->sec->name, rela->addend);
390 return -1;
391 }
392
393 insn->dead_end = false;
394 }
395
dcc914f4
JP
396 return 0;
397}
398
399/*
400 * Warnings shouldn't be reported for ignored functions.
401 */
402static void add_ignores(struct objtool_file *file)
403{
404 struct instruction *insn;
405 struct section *sec;
406 struct symbol *func;
407
baa41469 408 for_each_sec(file, sec) {
dcc914f4
JP
409 list_for_each_entry(func, &sec->symbol_list, list) {
410 if (func->type != STT_FUNC)
411 continue;
412
413 if (!ignore_func(file, func))
414 continue;
415
416 func_for_each_insn(file, func, insn)
baa41469 417 insn->ignore = true;
dcc914f4
JP
418 }
419 }
420}
421
422/*
423 * Find the destination instructions for all jumps.
424 */
425static int add_jump_destinations(struct objtool_file *file)
426{
427 struct instruction *insn;
428 struct rela *rela;
429 struct section *dest_sec;
430 unsigned long dest_off;
431
432 for_each_insn(file, insn) {
433 if (insn->type != INSN_JUMP_CONDITIONAL &&
434 insn->type != INSN_JUMP_UNCONDITIONAL)
435 continue;
436
baa41469 437 if (insn->ignore)
dcc914f4
JP
438 continue;
439
440 rela = find_rela_by_dest_range(insn->sec, insn->offset,
441 insn->len);
442 if (!rela) {
443 dest_sec = insn->sec;
444 dest_off = insn->offset + insn->len + insn->immediate;
445 } else if (rela->sym->type == STT_SECTION) {
446 dest_sec = rela->sym->sec;
447 dest_off = rela->addend + 4;
448 } else if (rela->sym->sec->idx) {
449 dest_sec = rela->sym->sec;
450 dest_off = rela->sym->sym.st_value + rela->addend + 4;
451 } else {
452 /* sibling call */
453 insn->jump_dest = 0;
454 continue;
455 }
456
457 insn->jump_dest = find_insn(file, dest_sec, dest_off);
458 if (!insn->jump_dest) {
459
460 /*
461 * This is a special case where an alt instruction
462 * jumps past the end of the section. These are
463 * handled later in handle_group_alt().
464 */
465 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
466 continue;
467
468 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
469 insn->sec, insn->offset, dest_sec->name,
470 dest_off);
471 return -1;
472 }
473 }
474
475 return 0;
476}
477
478/*
479 * Find the destination instructions for all calls.
480 */
481static int add_call_destinations(struct objtool_file *file)
482{
483 struct instruction *insn;
484 unsigned long dest_off;
485 struct rela *rela;
486
487 for_each_insn(file, insn) {
488 if (insn->type != INSN_CALL)
489 continue;
490
491 rela = find_rela_by_dest_range(insn->sec, insn->offset,
492 insn->len);
493 if (!rela) {
494 dest_off = insn->offset + insn->len + insn->immediate;
495 insn->call_dest = find_symbol_by_offset(insn->sec,
496 dest_off);
497 if (!insn->call_dest) {
498 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
499 insn->sec, insn->offset, dest_off);
500 return -1;
501 }
502 } else if (rela->sym->type == STT_SECTION) {
503 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
504 rela->addend+4);
505 if (!insn->call_dest ||
506 insn->call_dest->type != STT_FUNC) {
507 WARN_FUNC("can't find call dest symbol at %s+0x%x",
508 insn->sec, insn->offset,
509 rela->sym->sec->name,
510 rela->addend + 4);
511 return -1;
512 }
513 } else
514 insn->call_dest = rela->sym;
515 }
516
517 return 0;
518}
519
520/*
521 * The .alternatives section requires some extra special care, over and above
522 * what other special sections require:
523 *
524 * 1. Because alternatives are patched in-place, we need to insert a fake jump
525 * instruction at the end so that validate_branch() skips all the original
526 * replaced instructions when validating the new instruction path.
527 *
528 * 2. An added wrinkle is that the new instruction length might be zero. In
529 * that case the old instructions are replaced with noops. We simulate that
530 * by creating a fake jump as the only new instruction.
531 *
532 * 3. In some cases, the alternative section includes an instruction which
533 * conditionally jumps to the _end_ of the entry. We have to modify these
534 * jumps' destinations to point back to .text rather than the end of the
535 * entry in .altinstr_replacement.
536 *
537 * 4. It has been requested that we don't validate the !POPCNT feature path
538 * which is a "very very small percentage of machines".
539 */
540static int handle_group_alt(struct objtool_file *file,
541 struct special_alt *special_alt,
542 struct instruction *orig_insn,
543 struct instruction **new_insn)
544{
545 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump;
546 unsigned long dest_off;
547
548 last_orig_insn = NULL;
549 insn = orig_insn;
550 sec_for_each_insn_from(file, insn) {
551 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
552 break;
553
554 if (special_alt->skip_orig)
555 insn->type = INSN_NOP;
556
557 insn->alt_group = true;
558 last_orig_insn = insn;
559 }
560
561 if (!next_insn_same_sec(file, last_orig_insn)) {
562 WARN("%s: don't know how to handle alternatives at end of section",
563 special_alt->orig_sec->name);
564 return -1;
565 }
566
567 fake_jump = malloc(sizeof(*fake_jump));
568 if (!fake_jump) {
569 WARN("malloc failed");
570 return -1;
571 }
572 memset(fake_jump, 0, sizeof(*fake_jump));
573 INIT_LIST_HEAD(&fake_jump->alts);
baa41469
JP
574 clear_insn_state(&fake_jump->state);
575
dcc914f4
JP
576 fake_jump->sec = special_alt->new_sec;
577 fake_jump->offset = -1;
578 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
579 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
baa41469 580 fake_jump->ignore = true;
dcc914f4
JP
581
582 if (!special_alt->new_len) {
583 *new_insn = fake_jump;
584 return 0;
585 }
586
587 last_new_insn = NULL;
588 insn = *new_insn;
589 sec_for_each_insn_from(file, insn) {
590 if (insn->offset >= special_alt->new_off + special_alt->new_len)
591 break;
592
593 last_new_insn = insn;
594
595 if (insn->type != INSN_JUMP_CONDITIONAL &&
596 insn->type != INSN_JUMP_UNCONDITIONAL)
597 continue;
598
599 if (!insn->immediate)
600 continue;
601
602 dest_off = insn->offset + insn->len + insn->immediate;
603 if (dest_off == special_alt->new_off + special_alt->new_len)
604 insn->jump_dest = fake_jump;
605
606 if (!insn->jump_dest) {
607 WARN_FUNC("can't find alternative jump destination",
608 insn->sec, insn->offset);
609 return -1;
610 }
611 }
612
613 if (!last_new_insn) {
614 WARN_FUNC("can't find last new alternative instruction",
615 special_alt->new_sec, special_alt->new_off);
616 return -1;
617 }
618
619 list_add(&fake_jump->list, &last_new_insn->list);
620
621 return 0;
622}
623
624/*
625 * A jump table entry can either convert a nop to a jump or a jump to a nop.
626 * If the original instruction is a jump, make the alt entry an effective nop
627 * by just skipping the original instruction.
628 */
629static int handle_jump_alt(struct objtool_file *file,
630 struct special_alt *special_alt,
631 struct instruction *orig_insn,
632 struct instruction **new_insn)
633{
634 if (orig_insn->type == INSN_NOP)
635 return 0;
636
637 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
638 WARN_FUNC("unsupported instruction at jump label",
639 orig_insn->sec, orig_insn->offset);
640 return -1;
641 }
642
643 *new_insn = list_next_entry(orig_insn, list);
644 return 0;
645}
646
647/*
648 * Read all the special sections which have alternate instructions which can be
649 * patched in or redirected to at runtime. Each instruction having alternate
650 * instruction(s) has them added to its insn->alts list, which will be
651 * traversed in validate_branch().
652 */
653static int add_special_section_alts(struct objtool_file *file)
654{
655 struct list_head special_alts;
656 struct instruction *orig_insn, *new_insn;
657 struct special_alt *special_alt, *tmp;
658 struct alternative *alt;
659 int ret;
660
661 ret = special_get_alts(file->elf, &special_alts);
662 if (ret)
663 return ret;
664
665 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
666 alt = malloc(sizeof(*alt));
667 if (!alt) {
668 WARN("malloc failed");
669 ret = -1;
670 goto out;
671 }
672
673 orig_insn = find_insn(file, special_alt->orig_sec,
674 special_alt->orig_off);
675 if (!orig_insn) {
676 WARN_FUNC("special: can't find orig instruction",
677 special_alt->orig_sec, special_alt->orig_off);
678 ret = -1;
679 goto out;
680 }
681
682 new_insn = NULL;
683 if (!special_alt->group || special_alt->new_len) {
684 new_insn = find_insn(file, special_alt->new_sec,
685 special_alt->new_off);
686 if (!new_insn) {
687 WARN_FUNC("special: can't find new instruction",
688 special_alt->new_sec,
689 special_alt->new_off);
690 ret = -1;
691 goto out;
692 }
693 }
694
695 if (special_alt->group) {
696 ret = handle_group_alt(file, special_alt, orig_insn,
697 &new_insn);
698 if (ret)
699 goto out;
700 } else if (special_alt->jump_or_nop) {
701 ret = handle_jump_alt(file, special_alt, orig_insn,
702 &new_insn);
703 if (ret)
704 goto out;
705 }
706
707 alt->insn = new_insn;
708 list_add_tail(&alt->list, &orig_insn->alts);
709
710 list_del(&special_alt->list);
711 free(special_alt);
712 }
713
714out:
715 return ret;
716}
717
718static int add_switch_table(struct objtool_file *file, struct symbol *func,
719 struct instruction *insn, struct rela *table,
720 struct rela *next_table)
721{
722 struct rela *rela = table;
723 struct instruction *alt_insn;
724 struct alternative *alt;
725
726 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
727 if (rela == next_table)
728 break;
729
730 if (rela->sym->sec != insn->sec ||
731 rela->addend <= func->offset ||
732 rela->addend >= func->offset + func->len)
733 break;
734
735 alt_insn = find_insn(file, insn->sec, rela->addend);
736 if (!alt_insn) {
737 WARN("%s: can't find instruction at %s+0x%x",
738 file->rodata->rela->name, insn->sec->name,
739 rela->addend);
740 return -1;
741 }
742
743 alt = malloc(sizeof(*alt));
744 if (!alt) {
745 WARN("malloc failed");
746 return -1;
747 }
748
749 alt->insn = alt_insn;
750 list_add_tail(&alt->list, &insn->alts);
751 }
752
753 return 0;
754}
755
756/*
757 * find_switch_table() - Given a dynamic jump, find the switch jump table in
758 * .rodata associated with it.
759 *
760 * There are 3 basic patterns:
761 *
762 * 1. jmpq *[rodata addr](,%reg,8)
763 *
764 * This is the most common case by far. It jumps to an address in a simple
765 * jump table which is stored in .rodata.
766 *
767 * 2. jmpq *[rodata addr](%rip)
768 *
769 * This is caused by a rare GCC quirk, currently only seen in three driver
770 * functions in the kernel, only with certain obscure non-distro configs.
771 *
772 * As part of an optimization, GCC makes a copy of an existing switch jump
773 * table, modifies it, and then hard-codes the jump (albeit with an indirect
774 * jump) to use a single entry in the table. The rest of the jump table and
775 * some of its jump targets remain as dead code.
776 *
777 * In such a case we can just crudely ignore all unreachable instruction
778 * warnings for the entire object file. Ideally we would just ignore them
779 * for the function, but that would require redesigning the code quite a
780 * bit. And honestly that's just not worth doing: unreachable instruction
781 * warnings are of questionable value anyway, and this is such a rare issue.
782 *
783 * 3. mov [rodata addr],%reg1
784 * ... some instructions ...
785 * jmpq *(%reg1,%reg2,8)
786 *
787 * This is a fairly uncommon pattern which is new for GCC 6. As of this
788 * writing, there are 11 occurrences of it in the allmodconfig kernel.
789 *
790 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
791 * ensure the same register is used in the mov and jump instructions.
792 */
793static struct rela *find_switch_table(struct objtool_file *file,
794 struct symbol *func,
795 struct instruction *insn)
796{
797 struct rela *text_rela, *rodata_rela;
798 struct instruction *orig_insn = insn;
799
800 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
801 if (text_rela && text_rela->sym == file->rodata->sym) {
802 /* case 1 */
803 rodata_rela = find_rela_by_dest(file->rodata,
804 text_rela->addend);
805 if (rodata_rela)
806 return rodata_rela;
807
808 /* case 2 */
809 rodata_rela = find_rela_by_dest(file->rodata,
810 text_rela->addend + 4);
811 if (!rodata_rela)
812 return NULL;
813 file->ignore_unreachables = true;
814 return rodata_rela;
815 }
816
817 /* case 3 */
818 func_for_each_insn_continue_reverse(file, func, insn) {
819 if (insn->type == INSN_JUMP_DYNAMIC)
820 break;
821
822 /* allow small jumps within the range */
823 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
824 insn->jump_dest &&
825 (insn->jump_dest->offset <= insn->offset ||
826 insn->jump_dest->offset > orig_insn->offset))
827 break;
828
829 /* look for a relocation which references .rodata */
830 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
831 insn->len);
832 if (!text_rela || text_rela->sym != file->rodata->sym)
833 continue;
834
835 /*
836 * Make sure the .rodata address isn't associated with a
837 * symbol. gcc jump tables are anonymous data.
838 */
839 if (find_symbol_containing(file->rodata, text_rela->addend))
840 continue;
841
842 return find_rela_by_dest(file->rodata, text_rela->addend);
843 }
844
845 return NULL;
846}
847
848static int add_func_switch_tables(struct objtool_file *file,
849 struct symbol *func)
850{
851 struct instruction *insn, *prev_jump = NULL;
852 struct rela *rela, *prev_rela = NULL;
853 int ret;
854
855 func_for_each_insn(file, func, insn) {
856 if (insn->type != INSN_JUMP_DYNAMIC)
857 continue;
858
859 rela = find_switch_table(file, func, insn);
860 if (!rela)
861 continue;
862
863 /*
864 * We found a switch table, but we don't know yet how big it
865 * is. Don't add it until we reach the end of the function or
866 * the beginning of another switch table in the same function.
867 */
868 if (prev_jump) {
869 ret = add_switch_table(file, func, prev_jump, prev_rela,
870 rela);
871 if (ret)
872 return ret;
873 }
874
875 prev_jump = insn;
876 prev_rela = rela;
877 }
878
879 if (prev_jump) {
880 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
881 if (ret)
882 return ret;
883 }
884
885 return 0;
886}
887
888/*
889 * For some switch statements, gcc generates a jump table in the .rodata
890 * section which contains a list of addresses within the function to jump to.
891 * This finds these jump tables and adds them to the insn->alts lists.
892 */
893static int add_switch_table_alts(struct objtool_file *file)
894{
895 struct section *sec;
896 struct symbol *func;
897 int ret;
898
899 if (!file->rodata || !file->rodata->rela)
900 return 0;
901
baa41469 902 for_each_sec(file, sec) {
dcc914f4
JP
903 list_for_each_entry(func, &sec->symbol_list, list) {
904 if (func->type != STT_FUNC)
905 continue;
906
907 ret = add_func_switch_tables(file, func);
908 if (ret)
909 return ret;
910 }
911 }
912
913 return 0;
914}
915
39358a03
JP
916static int read_unwind_hints(struct objtool_file *file)
917{
918 struct section *sec, *relasec;
919 struct rela *rela;
920 struct unwind_hint *hint;
921 struct instruction *insn;
922 struct cfi_reg *cfa;
923 int i;
924
925 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
926 if (!sec)
927 return 0;
928
929 relasec = sec->rela;
930 if (!relasec) {
931 WARN("missing .rela.discard.unwind_hints section");
932 return -1;
933 }
934
935 if (sec->len % sizeof(struct unwind_hint)) {
936 WARN("struct unwind_hint size mismatch");
937 return -1;
938 }
939
940 file->hints = true;
941
942 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
943 hint = (struct unwind_hint *)sec->data->d_buf + i;
944
945 rela = find_rela_by_dest(sec, i * sizeof(*hint));
946 if (!rela) {
947 WARN("can't find rela for unwind_hints[%d]", i);
948 return -1;
949 }
950
951 insn = find_insn(file, rela->sym->sec, rela->addend);
952 if (!insn) {
953 WARN("can't find insn for unwind_hints[%d]", i);
954 return -1;
955 }
956
957 cfa = &insn->state.cfa;
958
959 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
960 insn->save = true;
961 continue;
962
963 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
964 insn->restore = true;
965 insn->hint = true;
966 continue;
967 }
968
969 insn->hint = true;
970
971 switch (hint->sp_reg) {
972 case ORC_REG_UNDEFINED:
973 cfa->base = CFI_UNDEFINED;
974 break;
975 case ORC_REG_SP:
976 cfa->base = CFI_SP;
977 break;
978 case ORC_REG_BP:
979 cfa->base = CFI_BP;
980 break;
981 case ORC_REG_SP_INDIRECT:
982 cfa->base = CFI_SP_INDIRECT;
983 break;
984 case ORC_REG_R10:
985 cfa->base = CFI_R10;
986 break;
987 case ORC_REG_R13:
988 cfa->base = CFI_R13;
989 break;
990 case ORC_REG_DI:
991 cfa->base = CFI_DI;
992 break;
993 case ORC_REG_DX:
994 cfa->base = CFI_DX;
995 break;
996 default:
997 WARN_FUNC("unsupported unwind_hint sp base reg %d",
998 insn->sec, insn->offset, hint->sp_reg);
999 return -1;
1000 }
1001
1002 cfa->offset = hint->sp_offset;
1003 insn->state.type = hint->type;
1004 }
1005
1006 return 0;
1007}
1008
dcc914f4
JP
1009static int decode_sections(struct objtool_file *file)
1010{
1011 int ret;
1012
1013 ret = decode_instructions(file);
1014 if (ret)
1015 return ret;
1016
1017 ret = add_dead_ends(file);
1018 if (ret)
1019 return ret;
1020
1021 add_ignores(file);
1022
1023 ret = add_jump_destinations(file);
1024 if (ret)
1025 return ret;
1026
1027 ret = add_call_destinations(file);
1028 if (ret)
1029 return ret;
1030
1031 ret = add_special_section_alts(file);
1032 if (ret)
1033 return ret;
1034
1035 ret = add_switch_table_alts(file);
1036 if (ret)
1037 return ret;
1038
39358a03
JP
1039 ret = read_unwind_hints(file);
1040 if (ret)
1041 return ret;
1042
dcc914f4
JP
1043 return 0;
1044}
1045
1046static bool is_fentry_call(struct instruction *insn)
1047{
1048 if (insn->type == INSN_CALL &&
1049 insn->call_dest->type == STT_NOTYPE &&
1050 !strcmp(insn->call_dest->name, "__fentry__"))
1051 return true;
1052
1053 return false;
1054}
1055
baa41469 1056static bool has_modified_stack_frame(struct insn_state *state)
dcc914f4 1057{
baa41469
JP
1058 int i;
1059
1060 if (state->cfa.base != initial_func_cfi.cfa.base ||
1061 state->cfa.offset != initial_func_cfi.cfa.offset ||
1062 state->stack_size != initial_func_cfi.cfa.offset ||
1063 state->drap)
1064 return true;
1065
1066 for (i = 0; i < CFI_NUM_REGS; i++)
1067 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1068 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1069 return true;
1070
1071 return false;
1072}
1073
1074static bool has_valid_stack_frame(struct insn_state *state)
1075{
1076 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1077 state->regs[CFI_BP].offset == -16)
1078 return true;
1079
1080 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1081 return true;
1082
1083 return false;
dcc914f4
JP
1084}
1085
627fce14
JP
1086static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1087{
1088 struct cfi_reg *cfa = &state->cfa;
1089 struct stack_op *op = &insn->stack_op;
1090
1091 if (cfa->base != CFI_SP)
1092 return 0;
1093
1094 /* push */
1095 if (op->dest.type == OP_DEST_PUSH)
1096 cfa->offset += 8;
1097
1098 /* pop */
1099 if (op->src.type == OP_SRC_POP)
1100 cfa->offset -= 8;
1101
1102 /* add immediate to sp */
1103 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1104 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1105 cfa->offset -= op->src.offset;
1106
1107 return 0;
1108}
1109
baa41469
JP
1110static void save_reg(struct insn_state *state, unsigned char reg, int base,
1111 int offset)
dcc914f4 1112{
baa41469
JP
1113 if ((arch_callee_saved_reg(reg) ||
1114 (state->drap && reg == state->drap_reg)) &&
1115 state->regs[reg].base == CFI_UNDEFINED) {
1116 state->regs[reg].base = base;
1117 state->regs[reg].offset = offset;
1118 }
dcc914f4
JP
1119}
1120
baa41469 1121static void restore_reg(struct insn_state *state, unsigned char reg)
dcc914f4 1122{
baa41469
JP
1123 state->regs[reg].base = CFI_UNDEFINED;
1124 state->regs[reg].offset = 0;
1125}
1126
1127/*
1128 * A note about DRAP stack alignment:
1129 *
1130 * GCC has the concept of a DRAP register, which is used to help keep track of
1131 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1132 * register. The typical DRAP pattern is:
1133 *
1134 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1135 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1136 * 41 ff 72 f8 pushq -0x8(%r10)
1137 * 55 push %rbp
1138 * 48 89 e5 mov %rsp,%rbp
1139 * (more pushes)
1140 * 41 52 push %r10
1141 * ...
1142 * 41 5a pop %r10
1143 * (more pops)
1144 * 5d pop %rbp
1145 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1146 * c3 retq
1147 *
1148 * There are some variations in the epilogues, like:
1149 *
1150 * 5b pop %rbx
1151 * 41 5a pop %r10
1152 * 41 5c pop %r12
1153 * 41 5d pop %r13
1154 * 41 5e pop %r14
1155 * c9 leaveq
1156 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1157 * c3 retq
1158 *
1159 * and:
1160 *
1161 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1162 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1163 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1164 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1165 * c9 leaveq
1166 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1167 * c3 retq
1168 *
1169 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1170 * restored beforehand:
1171 *
1172 * 41 55 push %r13
1173 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1174 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1175 * ...
1176 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1177 * 41 5d pop %r13
1178 * c3 retq
1179 */
1180static int update_insn_state(struct instruction *insn, struct insn_state *state)
1181{
1182 struct stack_op *op = &insn->stack_op;
1183 struct cfi_reg *cfa = &state->cfa;
1184 struct cfi_reg *regs = state->regs;
1185
1186 /* stack operations don't make sense with an undefined CFA */
1187 if (cfa->base == CFI_UNDEFINED) {
1188 if (insn->func) {
1189 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1190 return -1;
1191 }
1192 return 0;
1193 }
1194
627fce14
JP
1195 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1196 return update_insn_state_regs(insn, state);
1197
baa41469
JP
1198 switch (op->dest.type) {
1199
1200 case OP_DEST_REG:
1201 switch (op->src.type) {
1202
1203 case OP_SRC_REG:
1204 if (cfa->base == op->src.reg && cfa->base == CFI_SP &&
1205 op->dest.reg == CFI_BP && regs[CFI_BP].base == CFI_CFA &&
1206 regs[CFI_BP].offset == -cfa->offset) {
1207
1208 /* mov %rsp, %rbp */
1209 cfa->base = op->dest.reg;
1210 state->bp_scratch = false;
1211 } else if (state->drap) {
1212
1213 /* drap: mov %rsp, %rbp */
1214 regs[CFI_BP].base = CFI_BP;
1215 regs[CFI_BP].offset = -state->stack_size;
1216 state->bp_scratch = false;
867ac9d7 1217 } else if (!no_fp) {
baa41469
JP
1218
1219 WARN_FUNC("unknown stack-related register move",
1220 insn->sec, insn->offset);
1221 return -1;
1222 }
1223
1224 break;
1225
1226 case OP_SRC_ADD:
1227 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1228
1229 /* add imm, %rsp */
1230 state->stack_size -= op->src.offset;
1231 if (cfa->base == CFI_SP)
1232 cfa->offset -= op->src.offset;
1233 break;
1234 }
1235
1236 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1237
1238 /* lea disp(%rbp), %rsp */
1239 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1240 break;
1241 }
1242
1243 if (op->dest.reg != CFI_BP && op->src.reg == CFI_SP &&
1244 cfa->base == CFI_SP) {
1245
1246 /* drap: lea disp(%rsp), %drap */
1247 state->drap_reg = op->dest.reg;
1248 break;
1249 }
1250
1251 if (state->drap && op->dest.reg == CFI_SP &&
1252 op->src.reg == state->drap_reg) {
1253
1254 /* drap: lea disp(%drap), %rsp */
1255 cfa->base = CFI_SP;
1256 cfa->offset = state->stack_size = -op->src.offset;
1257 state->drap_reg = CFI_UNDEFINED;
1258 state->drap = false;
1259 break;
1260 }
1261
1262 if (op->dest.reg == state->cfa.base) {
1263 WARN_FUNC("unsupported stack register modification",
1264 insn->sec, insn->offset);
1265 return -1;
1266 }
1267
1268 break;
1269
1270 case OP_SRC_AND:
1271 if (op->dest.reg != CFI_SP ||
1272 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1273 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1274 WARN_FUNC("unsupported stack pointer realignment",
1275 insn->sec, insn->offset);
1276 return -1;
1277 }
1278
1279 if (state->drap_reg != CFI_UNDEFINED) {
1280 /* drap: and imm, %rsp */
1281 cfa->base = state->drap_reg;
1282 cfa->offset = state->stack_size = 0;
1283 state->drap = true;
1284
1285 }
1286
1287 /*
1288 * Older versions of GCC (4.8ish) realign the stack
1289 * without DRAP, with a frame pointer.
1290 */
1291
1292 break;
1293
1294 case OP_SRC_POP:
1295 if (!state->drap && op->dest.type == OP_DEST_REG &&
1296 op->dest.reg == cfa->base) {
1297
1298 /* pop %rbp */
1299 cfa->base = CFI_SP;
1300 }
1301
1302 if (regs[op->dest.reg].offset == -state->stack_size) {
1303
1304 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1305 op->dest.type == OP_DEST_REG &&
1306 op->dest.reg == state->drap_reg) {
1307
1308 /* drap: pop %drap */
1309 cfa->base = state->drap_reg;
1310 cfa->offset = 0;
1311 }
1312
1313 restore_reg(state, op->dest.reg);
1314 }
1315
1316 state->stack_size -= 8;
1317 if (cfa->base == CFI_SP)
1318 cfa->offset -= 8;
1319
1320 break;
1321
1322 case OP_SRC_REG_INDIRECT:
1323 if (state->drap && op->src.reg == CFI_BP &&
1324 op->src.offset == regs[op->dest.reg].offset) {
1325
1326 /* drap: mov disp(%rbp), %reg */
1327 if (op->dest.reg == state->drap_reg) {
1328 cfa->base = state->drap_reg;
1329 cfa->offset = 0;
1330 }
1331
1332 restore_reg(state, op->dest.reg);
1333
1334 } else if (op->src.reg == cfa->base &&
1335 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1336
1337 /* mov disp(%rbp), %reg */
1338 /* mov disp(%rsp), %reg */
1339 restore_reg(state, op->dest.reg);
1340 }
1341
1342 break;
1343
1344 default:
1345 WARN_FUNC("unknown stack-related instruction",
1346 insn->sec, insn->offset);
1347 return -1;
1348 }
1349
1350 break;
1351
1352 case OP_DEST_PUSH:
1353 state->stack_size += 8;
1354 if (cfa->base == CFI_SP)
1355 cfa->offset += 8;
1356
1357 if (op->src.type != OP_SRC_REG)
1358 break;
1359
1360 if (state->drap) {
1361 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1362
1363 /* drap: push %drap */
1364 cfa->base = CFI_BP_INDIRECT;
1365 cfa->offset = -state->stack_size;
1366
1367 /* save drap so we know when to undefine it */
1368 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1369
1370 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1371
1372 /* drap: push %rbp */
1373 state->stack_size = 0;
1374
1375 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1376
1377 /* drap: push %reg */
1378 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1379 }
1380
1381 } else {
1382
1383 /* push %reg */
1384 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1385 }
1386
1387 /* detect when asm code uses rbp as a scratch register */
867ac9d7 1388 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
baa41469
JP
1389 cfa->base != CFI_BP)
1390 state->bp_scratch = true;
1391 break;
1392
1393 case OP_DEST_REG_INDIRECT:
1394
1395 if (state->drap) {
1396 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1397
1398 /* drap: mov %drap, disp(%rbp) */
1399 cfa->base = CFI_BP_INDIRECT;
1400 cfa->offset = op->dest.offset;
1401
1402 /* save drap so we know when to undefine it */
1403 save_reg(state, op->src.reg, CFI_CFA, op->dest.offset);
1404 }
1405
1406 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1407
1408 /* drap: mov reg, disp(%rbp) */
1409 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1410 }
1411
1412 } else if (op->dest.reg == cfa->base) {
1413
1414 /* mov reg, disp(%rbp) */
1415 /* mov reg, disp(%rsp) */
1416 save_reg(state, op->src.reg, CFI_CFA,
1417 op->dest.offset - state->cfa.offset);
1418 }
1419
1420 break;
1421
1422 case OP_DEST_LEAVE:
1423 if ((!state->drap && cfa->base != CFI_BP) ||
1424 (state->drap && cfa->base != state->drap_reg)) {
1425 WARN_FUNC("leave instruction with modified stack frame",
1426 insn->sec, insn->offset);
1427 return -1;
1428 }
1429
1430 /* leave (mov %rbp, %rsp; pop %rbp) */
1431
1432 state->stack_size = -state->regs[CFI_BP].offset - 8;
1433 restore_reg(state, CFI_BP);
1434
1435 if (!state->drap) {
1436 cfa->base = CFI_SP;
1437 cfa->offset -= 8;
1438 }
1439
1440 break;
1441
1442 case OP_DEST_MEM:
1443 if (op->src.type != OP_SRC_POP) {
1444 WARN_FUNC("unknown stack-related memory operation",
1445 insn->sec, insn->offset);
1446 return -1;
1447 }
1448
1449 /* pop mem */
1450 state->stack_size -= 8;
1451 if (cfa->base == CFI_SP)
1452 cfa->offset -= 8;
1453
1454 break;
1455
1456 default:
1457 WARN_FUNC("unknown stack-related instruction",
1458 insn->sec, insn->offset);
1459 return -1;
1460 }
1461
1462 return 0;
1463}
1464
1465static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1466{
1467 struct insn_state *state1 = &insn->state, *state2 = state;
1468 int i;
1469
1470 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1471 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1472 insn->sec, insn->offset,
1473 state1->cfa.base, state1->cfa.offset,
1474 state2->cfa.base, state2->cfa.offset);
1475
1476 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1477 for (i = 0; i < CFI_NUM_REGS; i++) {
1478 if (!memcmp(&state1->regs[i], &state2->regs[i],
1479 sizeof(struct cfi_reg)))
1480 continue;
1481
1482 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1483 insn->sec, insn->offset,
1484 i, state1->regs[i].base, state1->regs[i].offset,
1485 i, state2->regs[i].base, state2->regs[i].offset);
1486 break;
1487 }
1488
627fce14
JP
1489 } else if (state1->type != state2->type) {
1490 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1491 insn->sec, insn->offset, state1->type, state2->type);
1492
baa41469
JP
1493 } else if (state1->drap != state2->drap ||
1494 (state1->drap && state1->drap_reg != state2->drap_reg)) {
1495 WARN_FUNC("stack state mismatch: drap1=%d(%d) drap2=%d(%d)",
1496 insn->sec, insn->offset,
1497 state1->drap, state1->drap_reg,
1498 state2->drap, state2->drap_reg);
1499
1500 } else
1501 return true;
1502
1503 return false;
dcc914f4
JP
1504}
1505
1506/*
1507 * Follow the branch starting at the given instruction, and recursively follow
1508 * any other branches (jumps). Meanwhile, track the frame pointer state at
1509 * each instruction and validate all the rules described in
1510 * tools/objtool/Documentation/stack-validation.txt.
1511 */
baa41469
JP
1512static int validate_branch(struct objtool_file *file, struct instruction *first,
1513 struct insn_state state)
dcc914f4
JP
1514{
1515 struct alternative *alt;
39358a03 1516 struct instruction *insn, *next_insn;
dcc914f4
JP
1517 struct section *sec;
1518 struct symbol *func = NULL;
dcc914f4
JP
1519 int ret;
1520
1521 insn = first;
1522 sec = insn->sec;
dcc914f4
JP
1523
1524 if (insn->alt_group && list_empty(&insn->alts)) {
1525 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1526 sec, insn->offset);
baa41469 1527 return -1;
dcc914f4
JP
1528 }
1529
1530 while (1) {
39358a03
JP
1531 next_insn = next_insn_same_sec(file, insn);
1532
dcc914f4
JP
1533 if (file->c_file && insn->func) {
1534 if (func && func != insn->func) {
1535 WARN("%s() falls through to next function %s()",
1536 func->name, insn->func->name);
1537 return 1;
1538 }
dcc914f4
JP
1539 }
1540
baa41469
JP
1541 func = insn->func;
1542
4855022a
JP
1543 if (func && insn->ignore) {
1544 WARN_FUNC("BUG: why am I validating an ignored function?",
1545 sec, insn->offset);
1546 return -1;
1547 }
1548
dcc914f4 1549 if (insn->visited) {
39358a03 1550 if (!insn->hint && !insn_state_match(insn, &state))
dcc914f4 1551 return 1;
dcc914f4
JP
1552
1553 return 0;
1554 }
1555
39358a03
JP
1556 if (insn->hint) {
1557 if (insn->restore) {
1558 struct instruction *save_insn, *i;
1559
1560 i = insn;
1561 save_insn = NULL;
1562 func_for_each_insn_continue_reverse(file, func, i) {
1563 if (i->save) {
1564 save_insn = i;
1565 break;
1566 }
1567 }
1568
1569 if (!save_insn) {
1570 WARN_FUNC("no corresponding CFI save for CFI restore",
1571 sec, insn->offset);
1572 return 1;
1573 }
1574
1575 if (!save_insn->visited) {
1576 /*
1577 * Oops, no state to copy yet.
1578 * Hopefully we can reach this
1579 * instruction from another branch
1580 * after the save insn has been
1581 * visited.
1582 */
1583 if (insn == first)
1584 return 0;
1585
1586 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1587 sec, insn->offset);
1588 return 1;
1589 }
1590
1591 insn->state = save_insn->state;
1592 }
1593
1594 state = insn->state;
1595
1596 } else
1597 insn->state = state;
dcc914f4 1598
baa41469
JP
1599 insn->visited = true;
1600
dcc914f4
JP
1601 list_for_each_entry(alt, &insn->alts, list) {
1602 ret = validate_branch(file, alt->insn, state);
1603 if (ret)
1604 return 1;
1605 }
1606
1607 switch (insn->type) {
1608
dcc914f4 1609 case INSN_RETURN:
baa41469
JP
1610 if (func && has_modified_stack_frame(&state)) {
1611 WARN_FUNC("return with modified stack frame",
dcc914f4
JP
1612 sec, insn->offset);
1613 return 1;
1614 }
baa41469
JP
1615
1616 if (state.bp_scratch) {
1617 WARN("%s uses BP as a scratch register",
1618 insn->func->name);
1619 return 1;
1620 }
1621
dcc914f4
JP
1622 return 0;
1623
1624 case INSN_CALL:
baa41469 1625 if (is_fentry_call(insn))
dcc914f4 1626 break;
dcc914f4
JP
1627
1628 ret = dead_end_function(file, insn->call_dest);
1629 if (ret == 1)
1630 return 0;
1631 if (ret == -1)
1632 return 1;
1633
1634 /* fallthrough */
1635 case INSN_CALL_DYNAMIC:
867ac9d7 1636 if (!no_fp && func && !has_valid_stack_frame(&state)) {
dcc914f4
JP
1637 WARN_FUNC("call without frame pointer save/setup",
1638 sec, insn->offset);
1639 return 1;
1640 }
1641 break;
1642
1643 case INSN_JUMP_CONDITIONAL:
1644 case INSN_JUMP_UNCONDITIONAL:
4855022a
JP
1645 if (insn->jump_dest &&
1646 (!func || !insn->jump_dest->func ||
1647 func == insn->jump_dest->func)) {
dcc914f4
JP
1648 ret = validate_branch(file, insn->jump_dest,
1649 state);
1650 if (ret)
1651 return 1;
4855022a 1652
baa41469
JP
1653 } else if (func && has_modified_stack_frame(&state)) {
1654 WARN_FUNC("sibling call from callable instruction with modified stack frame",
dcc914f4
JP
1655 sec, insn->offset);
1656 return 1;
4855022a 1657 }
dcc914f4
JP
1658
1659 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1660 return 0;
1661
1662 break;
1663
1664 case INSN_JUMP_DYNAMIC:
baa41469
JP
1665 if (func && list_empty(&insn->alts) &&
1666 has_modified_stack_frame(&state)) {
1667 WARN_FUNC("sibling call from callable instruction with modified stack frame",
dcc914f4
JP
1668 sec, insn->offset);
1669 return 1;
1670 }
1671
1672 return 0;
1673
39358a03
JP
1674 case INSN_CONTEXT_SWITCH:
1675 if (func && (!next_insn || !next_insn->hint)) {
1676 WARN_FUNC("unsupported instruction in callable function",
1677 sec, insn->offset);
1678 return 1;
1679 }
1680 return 0;
1681
baa41469
JP
1682 case INSN_STACK:
1683 if (update_insn_state(insn, &state))
1684 return -1;
1685
1686 break;
1687
dcc914f4
JP
1688 default:
1689 break;
1690 }
1691
1692 if (insn->dead_end)
1693 return 0;
1694
39358a03 1695 insn = next_insn;
dcc914f4
JP
1696 if (!insn) {
1697 WARN("%s: unexpected end of section", sec->name);
1698 return 1;
1699 }
1700 }
1701
1702 return 0;
1703}
1704
39358a03
JP
1705static int validate_unwind_hints(struct objtool_file *file)
1706{
1707 struct instruction *insn;
1708 int ret, warnings = 0;
1709 struct insn_state state;
1710
1711 if (!file->hints)
1712 return 0;
1713
1714 clear_insn_state(&state);
1715
1716 for_each_insn(file, insn) {
1717 if (insn->hint && !insn->visited) {
1718 ret = validate_branch(file, insn, state);
1719 warnings += ret;
1720 }
1721 }
1722
1723 return warnings;
1724}
1725
dcc914f4
JP
1726static bool is_kasan_insn(struct instruction *insn)
1727{
1728 return (insn->type == INSN_CALL &&
1729 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1730}
1731
1732static bool is_ubsan_insn(struct instruction *insn)
1733{
1734 return (insn->type == INSN_CALL &&
1735 !strcmp(insn->call_dest->name,
1736 "__ubsan_handle_builtin_unreachable"));
1737}
1738
baa41469 1739static bool ignore_unreachable_insn(struct instruction *insn)
dcc914f4
JP
1740{
1741 int i;
1742
baa41469
JP
1743 if (insn->ignore || insn->type == INSN_NOP)
1744 return true;
1745
1746 /*
1747 * Ignore any unused exceptions. This can happen when a whitelisted
1748 * function has an exception table entry.
0e2bb2bc
JP
1749 *
1750 * Also ignore alternative replacement instructions. This can happen
1751 * when a whitelisted function uses one of the ALTERNATIVE macros.
baa41469 1752 */
0e2bb2bc
JP
1753 if (!strcmp(insn->sec->name, ".fixup") ||
1754 !strcmp(insn->sec->name, ".altinstr_replacement") ||
1755 !strcmp(insn->sec->name, ".altinstr_aux"))
dcc914f4
JP
1756 return true;
1757
1758 /*
1759 * Check if this (or a subsequent) instruction is related to
1760 * CONFIG_UBSAN or CONFIG_KASAN.
1761 *
1762 * End the search at 5 instructions to avoid going into the weeds.
1763 */
baa41469
JP
1764 if (!insn->func)
1765 return false;
dcc914f4
JP
1766 for (i = 0; i < 5; i++) {
1767
1768 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
1769 return true;
1770
1771 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
1772 insn = insn->jump_dest;
1773 continue;
1774 }
1775
baa41469 1776 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
dcc914f4
JP
1777 break;
1778 insn = list_next_entry(insn, list);
1779 }
1780
1781 return false;
1782}
1783
1784static int validate_functions(struct objtool_file *file)
1785{
1786 struct section *sec;
1787 struct symbol *func;
1788 struct instruction *insn;
baa41469 1789 struct insn_state state;
dcc914f4
JP
1790 int ret, warnings = 0;
1791
baa41469
JP
1792 clear_insn_state(&state);
1793
1794 state.cfa = initial_func_cfi.cfa;
1795 memcpy(&state.regs, &initial_func_cfi.regs,
1796 CFI_NUM_REGS * sizeof(struct cfi_reg));
1797 state.stack_size = initial_func_cfi.cfa.offset;
1798
1799 for_each_sec(file, sec) {
dcc914f4
JP
1800 list_for_each_entry(func, &sec->symbol_list, list) {
1801 if (func->type != STT_FUNC)
1802 continue;
1803
1804 insn = find_insn(file, sec, func->offset);
baa41469 1805 if (!insn || insn->ignore)
dcc914f4
JP
1806 continue;
1807
baa41469 1808 ret = validate_branch(file, insn, state);
dcc914f4
JP
1809 warnings += ret;
1810 }
1811 }
1812
dcc914f4
JP
1813 return warnings;
1814}
1815
baa41469 1816static int validate_reachable_instructions(struct objtool_file *file)
dcc914f4
JP
1817{
1818 struct instruction *insn;
baa41469
JP
1819
1820 if (file->ignore_unreachables)
1821 return 0;
dcc914f4
JP
1822
1823 for_each_insn(file, insn) {
baa41469
JP
1824 if (insn->visited || ignore_unreachable_insn(insn))
1825 continue;
1826
baa41469
JP
1827 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
1828 return 1;
dcc914f4
JP
1829 }
1830
baa41469 1831 return 0;
dcc914f4
JP
1832}
1833
1834static void cleanup(struct objtool_file *file)
1835{
1836 struct instruction *insn, *tmpinsn;
1837 struct alternative *alt, *tmpalt;
1838
1839 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
1840 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
1841 list_del(&alt->list);
1842 free(alt);
1843 }
1844 list_del(&insn->list);
1845 hash_del(&insn->hash);
1846 free(insn);
1847 }
1848 elf_close(file->elf);
1849}
1850
867ac9d7 1851int check(const char *_objname, bool _no_fp, bool no_unreachable, bool orc)
dcc914f4
JP
1852{
1853 struct objtool_file file;
1854 int ret, warnings = 0;
1855
1856 objname = _objname;
867ac9d7 1857 no_fp = _no_fp;
dcc914f4 1858
627fce14 1859 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
baa41469 1860 if (!file.elf)
dcc914f4 1861 return 1;
dcc914f4
JP
1862
1863 INIT_LIST_HEAD(&file.insn_list);
1864 hash_init(file.insn_hash);
1865 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
1866 file.rodata = find_section_by_name(file.elf, ".rodata");
dcc914f4 1867 file.c_file = find_section_by_name(file.elf, ".comment");
867ac9d7 1868 file.ignore_unreachables = no_unreachable;
39358a03 1869 file.hints = false;
dcc914f4 1870
baa41469
JP
1871 arch_initial_func_cfi_state(&initial_func_cfi);
1872
dcc914f4
JP
1873 ret = decode_sections(&file);
1874 if (ret < 0)
1875 goto out;
1876 warnings += ret;
1877
baa41469 1878 if (list_empty(&file.insn_list))
dcc914f4 1879 goto out;
dcc914f4 1880
baa41469 1881 ret = validate_functions(&file);
dcc914f4
JP
1882 if (ret < 0)
1883 goto out;
1884 warnings += ret;
1885
39358a03
JP
1886 ret = validate_unwind_hints(&file);
1887 if (ret < 0)
1888 goto out;
1889 warnings += ret;
1890
baa41469
JP
1891 if (!warnings) {
1892 ret = validate_reachable_instructions(&file);
1893 if (ret < 0)
1894 goto out;
1895 warnings += ret;
1896 }
1897
627fce14
JP
1898 if (orc) {
1899 ret = create_orc(&file);
1900 if (ret < 0)
1901 goto out;
1902
1903 ret = create_orc_sections(&file);
1904 if (ret < 0)
1905 goto out;
1906
1907 ret = elf_write(file.elf);
1908 if (ret < 0)
1909 goto out;
1910 }
1911
dcc914f4
JP
1912out:
1913 cleanup(&file);
1914
1915 /* ignore warnings for now until we get all the code cleaned up */
1916 if (ret || warnings)
1917 return 0;
1918 return 0;
1919}