1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
4 */
5
6 #include <string.h>
7 #include <stdlib.h>
8 #include <inttypes.h>
9 #include <sys/mman.h>
10
11 #include "builtin.h"
12 #include "cfi.h"
13 #include "arch.h"
14 #include "check.h"
15 #include "special.h"
16 #include "warn.h"
17 #include "arch_elf.h"
18
19 #include <linux/objtool.h>
20 #include <linux/hashtable.h>
21 #include <linux/kernel.h>
22 #include <linux/static_call_types.h>
23
24 struct alternative {
25 struct list_head list;
26 struct instruction *insn;
27 bool skip_orig;
28 };
29
30 static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache;
31
32 static struct cfi_init_state initial_func_cfi;
33 static struct cfi_state init_cfi;
34 static struct cfi_state func_cfi;
35
find_insn(struct objtool_file * file,struct section * sec,unsigned long offset)36 struct instruction *find_insn(struct objtool_file *file,
37 struct section *sec, unsigned long offset)
38 {
39 struct instruction *insn;
40
41 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
42 if (insn->sec == sec && insn->offset == offset)
43 return insn;
44 }
45
46 return NULL;
47 }
48
next_insn_same_sec(struct objtool_file * file,struct instruction * insn)49 static struct instruction *next_insn_same_sec(struct objtool_file *file,
50 struct instruction *insn)
51 {
52 struct instruction *next = list_next_entry(insn, list);
53
54 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
55 return NULL;
56
57 return next;
58 }
59
next_insn_same_func(struct objtool_file * file,struct instruction * insn)60 static struct instruction *next_insn_same_func(struct objtool_file *file,
61 struct instruction *insn)
62 {
63 struct instruction *next = list_next_entry(insn, list);
64 struct symbol *func = insn->func;
65
66 if (!func)
67 return NULL;
68
69 if (&next->list != &file->insn_list && next->func == func)
70 return next;
71
72 /* Check if we're already in the subfunction: */
73 if (func == func->cfunc)
74 return NULL;
75
76 /* Move to the subfunction: */
77 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
78 }
79
prev_insn_same_sym(struct objtool_file * file,struct instruction * insn)80 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
81 struct instruction *insn)
82 {
83 struct instruction *prev = list_prev_entry(insn, list);
84
85 if (&prev->list != &file->insn_list && prev->func == insn->func)
86 return prev;
87
88 return NULL;
89 }
90
91 #define func_for_each_insn(file, func, insn) \
92 for (insn = find_insn(file, func->sec, func->offset); \
93 insn; \
94 insn = next_insn_same_func(file, insn))
95
96 #define sym_for_each_insn(file, sym, insn) \
97 for (insn = find_insn(file, sym->sec, sym->offset); \
98 insn && &insn->list != &file->insn_list && \
99 insn->sec == sym->sec && \
100 insn->offset < sym->offset + sym->len; \
101 insn = list_next_entry(insn, list))
102
103 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
104 for (insn = list_prev_entry(insn, list); \
105 &insn->list != &file->insn_list && \
106 insn->sec == sym->sec && insn->offset >= sym->offset; \
107 insn = list_prev_entry(insn, list))
108
109 #define sec_for_each_insn_from(file, insn) \
110 for (; insn; insn = next_insn_same_sec(file, insn))
111
112 #define sec_for_each_insn_continue(file, insn) \
113 for (insn = next_insn_same_sec(file, insn); insn; \
114 insn = next_insn_same_sec(file, insn))
115
is_jump_table_jump(struct instruction * insn)116 static bool is_jump_table_jump(struct instruction *insn)
117 {
118 struct alt_group *alt_group = insn->alt_group;
119
120 if (insn->jump_table)
121 return true;
122
123 /* Retpoline alternative for a jump table? */
124 return alt_group && alt_group->orig_group &&
125 alt_group->orig_group->first_insn->jump_table;
126 }
127
is_sibling_call(struct instruction * insn)128 static bool is_sibling_call(struct instruction *insn)
129 {
130 /*
131 * Assume only ELF functions can make sibling calls. This ensures
132 * sibling call detection consistency between vmlinux.o and individual
133 * objects.
134 */
135 if (!insn->func)
136 return false;
137
138 /* An indirect jump is either a sibling call or a jump to a table. */
139 if (insn->type == INSN_JUMP_DYNAMIC)
140 return !is_jump_table_jump(insn);
141
142 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
143 return (is_static_jump(insn) && insn->call_dest);
144 }
145
146 /*
147 * This checks to see if the given function is a "noreturn" function.
148 *
149 * For global functions which are outside the scope of this object file, we
150 * have to keep a manual list of them.
151 *
152 * For local functions, we have to detect them manually by simply looking for
153 * the lack of a return instruction.
154 */
__dead_end_function(struct objtool_file * file,struct symbol * func,int recursion)155 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
156 int recursion)
157 {
158 int i;
159 struct instruction *insn;
160 bool empty = true;
161
162 /*
163 * Unfortunately these have to be hard coded because the noreturn
164 * attribute isn't provided in ELF data.
165 */
166 static const char * const global_noreturns[] = {
167 "__stack_chk_fail",
168 "panic",
169 "do_exit",
170 "do_task_dead",
171 "__module_put_and_exit",
172 "complete_and_exit",
173 "__reiserfs_panic",
174 "lbug_with_loc",
175 "fortify_panic",
176 "usercopy_abort",
177 "machine_real_restart",
178 "rewind_stack_do_exit",
179 "kunit_try_catch_throw",
180 "xen_start_kernel",
181 "cpu_bringup_and_idle",
182 };
183
184 if (!func)
185 return false;
186
187 if (func->bind == STB_WEAK)
188 return false;
189
190 if (func->bind == STB_GLOBAL)
191 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
192 if (!strcmp(func->name, global_noreturns[i]))
193 return true;
194
195 if (!func->len)
196 return false;
197
198 insn = find_insn(file, func->sec, func->offset);
199 if (!insn || !insn->func)
200 return false;
201
202 func_for_each_insn(file, func, insn) {
203 empty = false;
204
205 if (insn->type == INSN_RETURN)
206 return false;
207 }
208
209 if (empty)
210 return false;
211
212 /*
213 * A function can have a sibling call instead of a return. In that
214 * case, the function's dead-end status depends on whether the target
215 * of the sibling call returns.
216 */
217 func_for_each_insn(file, func, insn) {
218 if (is_sibling_call(insn)) {
219 struct instruction *dest = insn->jump_dest;
220
221 if (!dest)
222 /* sibling call to another file */
223 return false;
224
225 /* local sibling call */
226 if (recursion == 5) {
227 /*
228 * Infinite recursion: two functions have
229 * sibling calls to each other. This is a very
230 * rare case. It means they aren't dead ends.
231 */
232 return false;
233 }
234
235 return __dead_end_function(file, dest->func, recursion+1);
236 }
237 }
238
239 return true;
240 }
241
dead_end_function(struct objtool_file * file,struct symbol * func)242 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
243 {
244 return __dead_end_function(file, func, 0);
245 }
246
init_cfi_state(struct cfi_state * cfi)247 static void init_cfi_state(struct cfi_state *cfi)
248 {
249 int i;
250
251 for (i = 0; i < CFI_NUM_REGS; i++) {
252 cfi->regs[i].base = CFI_UNDEFINED;
253 cfi->vals[i].base = CFI_UNDEFINED;
254 }
255 cfi->cfa.base = CFI_UNDEFINED;
256 cfi->drap_reg = CFI_UNDEFINED;
257 cfi->drap_offset = -1;
258 }
259
init_insn_state(struct insn_state * state,struct section * sec)260 static void init_insn_state(struct insn_state *state, struct section *sec)
261 {
262 memset(state, 0, sizeof(*state));
263 init_cfi_state(&state->cfi);
264
265 /*
266 * We need the full vmlinux for noinstr validation, otherwise we can
267 * not correctly determine insn->call_dest->sec (external symbols do
268 * not have a section).
269 */
270 if (vmlinux && sec)
271 state->noinstr = sec->noinstr;
272 }
273
cfi_alloc(void)274 static struct cfi_state *cfi_alloc(void)
275 {
276 struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
277 if (!cfi) {
278 WARN("calloc failed");
279 exit(1);
280 }
281 nr_cfi++;
282 return cfi;
283 }
284
285 static int cfi_bits;
286 static struct hlist_head *cfi_hash;
287
cficmp(struct cfi_state * cfi1,struct cfi_state * cfi2)288 static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
289 {
290 return memcmp((void *)cfi1 + sizeof(cfi1->hash),
291 (void *)cfi2 + sizeof(cfi2->hash),
292 sizeof(struct cfi_state) - sizeof(struct hlist_node));
293 }
294
cfi_key(struct cfi_state * cfi)295 static inline u32 cfi_key(struct cfi_state *cfi)
296 {
297 return jhash((void *)cfi + sizeof(cfi->hash),
298 sizeof(*cfi) - sizeof(cfi->hash), 0);
299 }
300
cfi_hash_find_or_add(struct cfi_state * cfi)301 static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
302 {
303 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
304 struct cfi_state *obj;
305
306 hlist_for_each_entry(obj, head, hash) {
307 if (!cficmp(cfi, obj)) {
308 nr_cfi_cache++;
309 return obj;
310 }
311 }
312
313 obj = cfi_alloc();
314 *obj = *cfi;
315 hlist_add_head(&obj->hash, head);
316
317 return obj;
318 }
319
cfi_hash_add(struct cfi_state * cfi)320 static void cfi_hash_add(struct cfi_state *cfi)
321 {
322 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
323
324 hlist_add_head(&cfi->hash, head);
325 }
326
cfi_hash_alloc(void)327 static void *cfi_hash_alloc(void)
328 {
329 cfi_bits = vmlinux ? ELF_HASH_BITS - 3 : 13;
330 cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits,
331 PROT_READ|PROT_WRITE,
332 MAP_PRIVATE|MAP_ANON, -1, 0);
333 if (cfi_hash == (void *)-1L) {
334 WARN("mmap fail cfi_hash");
335 cfi_hash = NULL;
336 } else if (stats) {
337 printf("cfi_bits: %d\n", cfi_bits);
338 }
339
340 return cfi_hash;
341 }
342
343 static unsigned long nr_insns;
344 static unsigned long nr_insns_visited;
345
346 /*
347 * Call the arch-specific instruction decoder for all the instructions and add
348 * them to the global instruction list.
349 */
decode_instructions(struct objtool_file * file)350 static int decode_instructions(struct objtool_file *file)
351 {
352 struct section *sec;
353 struct symbol *func;
354 unsigned long offset;
355 struct instruction *insn;
356 int ret;
357
358 for_each_sec(file, sec) {
359
360 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
361 continue;
362
363 if (strcmp(sec->name, ".altinstr_replacement") &&
364 strcmp(sec->name, ".altinstr_aux") &&
365 strncmp(sec->name, ".discard.", 9))
366 sec->text = true;
367
368 if (!strcmp(sec->name, ".noinstr.text") ||
369 !strcmp(sec->name, ".entry.text") ||
370 !strncmp(sec->name, ".text.__x86.", 12))
371 sec->noinstr = true;
372
373 for (offset = 0; offset < sec->len; offset += insn->len) {
374 insn = malloc(sizeof(*insn));
375 if (!insn) {
376 WARN("malloc failed");
377 return -1;
378 }
379 memset(insn, 0, sizeof(*insn));
380 INIT_LIST_HEAD(&insn->alts);
381 INIT_LIST_HEAD(&insn->stack_ops);
382
383 insn->sec = sec;
384 insn->offset = offset;
385
386 ret = arch_decode_instruction(file->elf, sec, offset,
387 sec->len - offset,
388 &insn->len, &insn->type,
389 &insn->immediate,
390 &insn->stack_ops);
391 if (ret)
392 goto err;
393
394 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
395 list_add_tail(&insn->list, &file->insn_list);
396 nr_insns++;
397 }
398
399 list_for_each_entry(func, &sec->symbol_list, list) {
400 if (func->type != STT_FUNC || func->alias != func)
401 continue;
402
403 if (!find_insn(file, sec, func->offset)) {
404 WARN("%s(): can't find starting instruction",
405 func->name);
406 return -1;
407 }
408
409 sym_for_each_insn(file, func, insn)
410 insn->func = func;
411 }
412 }
413
414 if (stats)
415 printf("nr_insns: %lu\n", nr_insns);
416
417 return 0;
418
419 err:
420 free(insn);
421 return ret;
422 }
423
find_last_insn(struct objtool_file * file,struct section * sec)424 static struct instruction *find_last_insn(struct objtool_file *file,
425 struct section *sec)
426 {
427 struct instruction *insn = NULL;
428 unsigned int offset;
429 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
430
431 for (offset = sec->len - 1; offset >= end && !insn; offset--)
432 insn = find_insn(file, sec, offset);
433
434 return insn;
435 }
436
437 /*
438 * Mark "ud2" instructions and manually annotated dead ends.
439 */
add_dead_ends(struct objtool_file * file)440 static int add_dead_ends(struct objtool_file *file)
441 {
442 struct section *sec;
443 struct reloc *reloc;
444 struct instruction *insn;
445
446 /*
447 * By default, "ud2" is a dead end unless otherwise annotated, because
448 * GCC 7 inserts it for certain divide-by-zero cases.
449 */
450 for_each_insn(file, insn)
451 if (insn->type == INSN_BUG)
452 insn->dead_end = true;
453
454 /*
455 * Check for manually annotated dead ends.
456 */
457 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
458 if (!sec)
459 goto reachable;
460
461 list_for_each_entry(reloc, &sec->reloc_list, list) {
462 if (reloc->sym->type != STT_SECTION) {
463 WARN("unexpected relocation symbol type in %s", sec->name);
464 return -1;
465 }
466 insn = find_insn(file, reloc->sym->sec, reloc->addend);
467 if (insn)
468 insn = list_prev_entry(insn, list);
469 else if (reloc->addend == reloc->sym->sec->len) {
470 insn = find_last_insn(file, reloc->sym->sec);
471 if (!insn) {
472 WARN("can't find unreachable insn at %s+0x%" PRIx64,
473 reloc->sym->sec->name, reloc->addend);
474 return -1;
475 }
476 } else {
477 WARN("can't find unreachable insn at %s+0x%" PRIx64,
478 reloc->sym->sec->name, reloc->addend);
479 return -1;
480 }
481
482 insn->dead_end = true;
483 }
484
485 reachable:
486 /*
487 * These manually annotated reachable checks are needed for GCC 4.4,
488 * where the Linux unreachable() macro isn't supported. In that case
489 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
490 * not a dead end.
491 */
492 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
493 if (!sec)
494 return 0;
495
496 list_for_each_entry(reloc, &sec->reloc_list, list) {
497 if (reloc->sym->type != STT_SECTION) {
498 WARN("unexpected relocation symbol type in %s", sec->name);
499 return -1;
500 }
501 insn = find_insn(file, reloc->sym->sec, reloc->addend);
502 if (insn)
503 insn = list_prev_entry(insn, list);
504 else if (reloc->addend == reloc->sym->sec->len) {
505 insn = find_last_insn(file, reloc->sym->sec);
506 if (!insn) {
507 WARN("can't find reachable insn at %s+0x%" PRIx64,
508 reloc->sym->sec->name, reloc->addend);
509 return -1;
510 }
511 } else {
512 WARN("can't find reachable insn at %s+0x%" PRIx64,
513 reloc->sym->sec->name, reloc->addend);
514 return -1;
515 }
516
517 insn->dead_end = false;
518 }
519
520 return 0;
521 }
522
create_static_call_sections(struct objtool_file * file)523 static int create_static_call_sections(struct objtool_file *file)
524 {
525 struct section *sec;
526 struct static_call_site *site;
527 struct instruction *insn;
528 struct symbol *key_sym;
529 char *key_name, *tmp;
530 int idx;
531
532 sec = find_section_by_name(file->elf, ".static_call_sites");
533 if (sec) {
534 INIT_LIST_HEAD(&file->static_call_list);
535 WARN("file already has .static_call_sites section, skipping");
536 return 0;
537 }
538
539 if (list_empty(&file->static_call_list))
540 return 0;
541
542 idx = 0;
543 list_for_each_entry(insn, &file->static_call_list, call_node)
544 idx++;
545
546 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
547 sizeof(struct static_call_site), idx);
548 if (!sec)
549 return -1;
550
551 idx = 0;
552 list_for_each_entry(insn, &file->static_call_list, call_node) {
553
554 site = (struct static_call_site *)sec->data->d_buf + idx;
555 memset(site, 0, sizeof(struct static_call_site));
556
557 /* populate reloc for 'addr' */
558 if (elf_add_reloc_to_insn(file->elf, sec,
559 idx * sizeof(struct static_call_site),
560 R_X86_64_PC32,
561 insn->sec, insn->offset))
562 return -1;
563
564 /* find key symbol */
565 key_name = strdup(insn->call_dest->name);
566 if (!key_name) {
567 perror("strdup");
568 return -1;
569 }
570 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
571 STATIC_CALL_TRAMP_PREFIX_LEN)) {
572 WARN("static_call: trampoline name malformed: %s", key_name);
573 return -1;
574 }
575 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
576 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
577
578 key_sym = find_symbol_by_name(file->elf, tmp);
579 if (!key_sym) {
580 if (!module) {
581 WARN("static_call: can't find static_call_key symbol: %s", tmp);
582 return -1;
583 }
584
585 /*
586 * For modules(), the key might not be exported, which
587 * means the module can make static calls but isn't
588 * allowed to change them.
589 *
590 * In that case we temporarily set the key to be the
591 * trampoline address. This is fixed up in
592 * static_call_add_module().
593 */
594 key_sym = insn->call_dest;
595 }
596 free(key_name);
597
598 /* populate reloc for 'key' */
599 if (elf_add_reloc(file->elf, sec,
600 idx * sizeof(struct static_call_site) + 4,
601 R_X86_64_PC32, key_sym,
602 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
603 return -1;
604
605 idx++;
606 }
607
608 return 0;
609 }
610
create_retpoline_sites_sections(struct objtool_file * file)611 static int create_retpoline_sites_sections(struct objtool_file *file)
612 {
613 struct instruction *insn;
614 struct section *sec;
615 int idx;
616
617 sec = find_section_by_name(file->elf, ".retpoline_sites");
618 if (sec) {
619 WARN("file already has .retpoline_sites, skipping");
620 return 0;
621 }
622
623 idx = 0;
624 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
625 idx++;
626
627 if (!idx)
628 return 0;
629
630 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
631 sizeof(int), idx);
632 if (!sec) {
633 WARN("elf_create_section: .retpoline_sites");
634 return -1;
635 }
636
637 idx = 0;
638 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
639
640 int *site = (int *)sec->data->d_buf + idx;
641 *site = 0;
642
643 if (elf_add_reloc_to_insn(file->elf, sec,
644 idx * sizeof(int),
645 R_X86_64_PC32,
646 insn->sec, insn->offset)) {
647 WARN("elf_add_reloc_to_insn: .retpoline_sites");
648 return -1;
649 }
650
651 idx++;
652 }
653
654 return 0;
655 }
656
create_return_sites_sections(struct objtool_file * file)657 static int create_return_sites_sections(struct objtool_file *file)
658 {
659 struct instruction *insn;
660 struct section *sec;
661 int idx;
662
663 sec = find_section_by_name(file->elf, ".return_sites");
664 if (sec) {
665 WARN("file already has .return_sites, skipping");
666 return 0;
667 }
668
669 idx = 0;
670 list_for_each_entry(insn, &file->return_thunk_list, call_node)
671 idx++;
672
673 if (!idx)
674 return 0;
675
676 sec = elf_create_section(file->elf, ".return_sites", 0,
677 sizeof(int), idx);
678 if (!sec) {
679 WARN("elf_create_section: .return_sites");
680 return -1;
681 }
682
683 idx = 0;
684 list_for_each_entry(insn, &file->return_thunk_list, call_node) {
685
686 int *site = (int *)sec->data->d_buf + idx;
687 *site = 0;
688
689 if (elf_add_reloc_to_insn(file->elf, sec,
690 idx * sizeof(int),
691 R_X86_64_PC32,
692 insn->sec, insn->offset)) {
693 WARN("elf_add_reloc_to_insn: .return_sites");
694 return -1;
695 }
696
697 idx++;
698 }
699
700 return 0;
701 }
702
703 /*
704 * Warnings shouldn't be reported for ignored functions.
705 */
add_ignores(struct objtool_file * file)706 static void add_ignores(struct objtool_file *file)
707 {
708 struct instruction *insn;
709 struct section *sec;
710 struct symbol *func;
711 struct reloc *reloc;
712
713 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
714 if (!sec)
715 return;
716
717 list_for_each_entry(reloc, &sec->reloc_list, list) {
718 switch (reloc->sym->type) {
719 case STT_FUNC:
720 func = reloc->sym;
721 break;
722
723 case STT_SECTION:
724 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
725 if (!func)
726 continue;
727 break;
728
729 default:
730 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
731 continue;
732 }
733
734 func_for_each_insn(file, func, insn)
735 insn->ignore = true;
736 }
737 }
738
739 /*
740 * This is a whitelist of functions that is allowed to be called with AC set.
741 * The list is meant to be minimal and only contains compiler instrumentation
742 * ABI and a few functions used to implement *_{to,from}_user() functions.
743 *
744 * These functions must not directly change AC, but may PUSHF/POPF.
745 */
746 static const char *uaccess_safe_builtin[] = {
747 /* KASAN */
748 "kasan_report",
749 "check_memory_region",
750 /* KASAN out-of-line */
751 "__asan_loadN_noabort",
752 "__asan_load1_noabort",
753 "__asan_load2_noabort",
754 "__asan_load4_noabort",
755 "__asan_load8_noabort",
756 "__asan_load16_noabort",
757 "__asan_storeN_noabort",
758 "__asan_store1_noabort",
759 "__asan_store2_noabort",
760 "__asan_store4_noabort",
761 "__asan_store8_noabort",
762 "__asan_store16_noabort",
763 "__kasan_check_read",
764 "__kasan_check_write",
765 /* KASAN in-line */
766 "__asan_report_load_n_noabort",
767 "__asan_report_load1_noabort",
768 "__asan_report_load2_noabort",
769 "__asan_report_load4_noabort",
770 "__asan_report_load8_noabort",
771 "__asan_report_load16_noabort",
772 "__asan_report_store_n_noabort",
773 "__asan_report_store1_noabort",
774 "__asan_report_store2_noabort",
775 "__asan_report_store4_noabort",
776 "__asan_report_store8_noabort",
777 "__asan_report_store16_noabort",
778 /* KCSAN */
779 "__kcsan_check_access",
780 "kcsan_found_watchpoint",
781 "kcsan_setup_watchpoint",
782 "kcsan_check_scoped_accesses",
783 "kcsan_disable_current",
784 "kcsan_enable_current_nowarn",
785 /* KCSAN/TSAN */
786 "__tsan_func_entry",
787 "__tsan_func_exit",
788 "__tsan_read_range",
789 "__tsan_write_range",
790 "__tsan_read1",
791 "__tsan_read2",
792 "__tsan_read4",
793 "__tsan_read8",
794 "__tsan_read16",
795 "__tsan_write1",
796 "__tsan_write2",
797 "__tsan_write4",
798 "__tsan_write8",
799 "__tsan_write16",
800 "__tsan_read_write1",
801 "__tsan_read_write2",
802 "__tsan_read_write4",
803 "__tsan_read_write8",
804 "__tsan_read_write16",
805 "__tsan_volatile_read1",
806 "__tsan_volatile_read2",
807 "__tsan_volatile_read4",
808 "__tsan_volatile_read8",
809 "__tsan_volatile_read16",
810 "__tsan_volatile_write1",
811 "__tsan_volatile_write2",
812 "__tsan_volatile_write4",
813 "__tsan_volatile_write8",
814 "__tsan_volatile_write16",
815 "__tsan_atomic8_load",
816 "__tsan_atomic16_load",
817 "__tsan_atomic32_load",
818 "__tsan_atomic64_load",
819 "__tsan_atomic8_store",
820 "__tsan_atomic16_store",
821 "__tsan_atomic32_store",
822 "__tsan_atomic64_store",
823 "__tsan_atomic8_exchange",
824 "__tsan_atomic16_exchange",
825 "__tsan_atomic32_exchange",
826 "__tsan_atomic64_exchange",
827 "__tsan_atomic8_fetch_add",
828 "__tsan_atomic16_fetch_add",
829 "__tsan_atomic32_fetch_add",
830 "__tsan_atomic64_fetch_add",
831 "__tsan_atomic8_fetch_sub",
832 "__tsan_atomic16_fetch_sub",
833 "__tsan_atomic32_fetch_sub",
834 "__tsan_atomic64_fetch_sub",
835 "__tsan_atomic8_fetch_and",
836 "__tsan_atomic16_fetch_and",
837 "__tsan_atomic32_fetch_and",
838 "__tsan_atomic64_fetch_and",
839 "__tsan_atomic8_fetch_or",
840 "__tsan_atomic16_fetch_or",
841 "__tsan_atomic32_fetch_or",
842 "__tsan_atomic64_fetch_or",
843 "__tsan_atomic8_fetch_xor",
844 "__tsan_atomic16_fetch_xor",
845 "__tsan_atomic32_fetch_xor",
846 "__tsan_atomic64_fetch_xor",
847 "__tsan_atomic8_fetch_nand",
848 "__tsan_atomic16_fetch_nand",
849 "__tsan_atomic32_fetch_nand",
850 "__tsan_atomic64_fetch_nand",
851 "__tsan_atomic8_compare_exchange_strong",
852 "__tsan_atomic16_compare_exchange_strong",
853 "__tsan_atomic32_compare_exchange_strong",
854 "__tsan_atomic64_compare_exchange_strong",
855 "__tsan_atomic8_compare_exchange_weak",
856 "__tsan_atomic16_compare_exchange_weak",
857 "__tsan_atomic32_compare_exchange_weak",
858 "__tsan_atomic64_compare_exchange_weak",
859 "__tsan_atomic8_compare_exchange_val",
860 "__tsan_atomic16_compare_exchange_val",
861 "__tsan_atomic32_compare_exchange_val",
862 "__tsan_atomic64_compare_exchange_val",
863 "__tsan_atomic_thread_fence",
864 "__tsan_atomic_signal_fence",
865 /* KCOV */
866 "write_comp_data",
867 "check_kcov_mode",
868 "__sanitizer_cov_trace_pc",
869 "__sanitizer_cov_trace_const_cmp1",
870 "__sanitizer_cov_trace_const_cmp2",
871 "__sanitizer_cov_trace_const_cmp4",
872 "__sanitizer_cov_trace_const_cmp8",
873 "__sanitizer_cov_trace_cmp1",
874 "__sanitizer_cov_trace_cmp2",
875 "__sanitizer_cov_trace_cmp4",
876 "__sanitizer_cov_trace_cmp8",
877 "__sanitizer_cov_trace_switch",
878 /* UBSAN */
879 "ubsan_type_mismatch_common",
880 "__ubsan_handle_type_mismatch",
881 "__ubsan_handle_type_mismatch_v1",
882 "__ubsan_handle_shift_out_of_bounds",
883 /* misc */
884 "csum_partial_copy_generic",
885 "copy_mc_fragile",
886 "copy_mc_fragile_handle_tail",
887 "copy_mc_enhanced_fast_string",
888 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
889 NULL
890 };
891
add_uaccess_safe(struct objtool_file * file)892 static void add_uaccess_safe(struct objtool_file *file)
893 {
894 struct symbol *func;
895 const char **name;
896
897 if (!uaccess)
898 return;
899
900 for (name = uaccess_safe_builtin; *name; name++) {
901 func = find_symbol_by_name(file->elf, *name);
902 if (!func)
903 continue;
904
905 func->uaccess_safe = true;
906 }
907 }
908
909 /*
910 * FIXME: For now, just ignore any alternatives which add retpolines. This is
911 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
912 * But it at least allows objtool to understand the control flow *around* the
913 * retpoline.
914 */
add_ignore_alternatives(struct objtool_file * file)915 static int add_ignore_alternatives(struct objtool_file *file)
916 {
917 struct section *sec;
918 struct reloc *reloc;
919 struct instruction *insn;
920
921 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
922 if (!sec)
923 return 0;
924
925 list_for_each_entry(reloc, &sec->reloc_list, list) {
926 if (reloc->sym->type != STT_SECTION) {
927 WARN("unexpected relocation symbol type in %s", sec->name);
928 return -1;
929 }
930
931 insn = find_insn(file, reloc->sym->sec, reloc->addend);
932 if (!insn) {
933 WARN("bad .discard.ignore_alts entry");
934 return -1;
935 }
936
937 insn->ignore_alts = true;
938 }
939
940 return 0;
941 }
942
arch_is_retpoline(struct symbol * sym)943 __weak bool arch_is_retpoline(struct symbol *sym)
944 {
945 return false;
946 }
947
arch_is_rethunk(struct symbol * sym)948 __weak bool arch_is_rethunk(struct symbol *sym)
949 {
950 return false;
951 }
952
953 #define NEGATIVE_RELOC ((void *)-1L)
954
insn_reloc(struct objtool_file * file,struct instruction * insn)955 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
956 {
957 if (insn->reloc == NEGATIVE_RELOC)
958 return NULL;
959
960 if (!insn->reloc) {
961 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
962 insn->offset, insn->len);
963 if (!insn->reloc) {
964 insn->reloc = NEGATIVE_RELOC;
965 return NULL;
966 }
967 }
968
969 return insn->reloc;
970 }
971
remove_insn_ops(struct instruction * insn)972 static void remove_insn_ops(struct instruction *insn)
973 {
974 struct stack_op *op, *tmp;
975
976 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
977 list_del(&op->list);
978 free(op);
979 }
980 }
981
annotate_call_site(struct objtool_file * file,struct instruction * insn,bool sibling)982 static void annotate_call_site(struct objtool_file *file,
983 struct instruction *insn, bool sibling)
984 {
985 struct reloc *reloc = insn_reloc(file, insn);
986 struct symbol *sym = insn->call_dest;
987
988 if (!sym)
989 sym = reloc->sym;
990
991 /*
992 * Alternative replacement code is just template code which is
993 * sometimes copied to the original instruction. For now, don't
994 * annotate it. (In the future we might consider annotating the
995 * original instruction if/when it ever makes sense to do so.)
996 */
997 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
998 return;
999
1000 if (sym->static_call_tramp) {
1001 list_add_tail(&insn->call_node, &file->static_call_list);
1002 return;
1003 }
1004
1005 if (sym->retpoline_thunk) {
1006 list_add_tail(&insn->call_node, &file->retpoline_call_list);
1007 return;
1008 }
1009
1010 /*
1011 * Many compilers cannot disable KCOV with a function attribute
1012 * so they need a little help, NOP out any KCOV calls from noinstr
1013 * text.
1014 */
1015 if (insn->sec->noinstr && sym->kcov) {
1016 if (reloc) {
1017 reloc->type = R_NONE;
1018 elf_write_reloc(file->elf, reloc);
1019 }
1020
1021 elf_write_insn(file->elf, insn->sec,
1022 insn->offset, insn->len,
1023 sibling ? arch_ret_insn(insn->len)
1024 : arch_nop_insn(insn->len));
1025
1026 insn->type = sibling ? INSN_RETURN : INSN_NOP;
1027
1028 if (sibling) {
1029 /*
1030 * We've replaced the tail-call JMP insn by two new
1031 * insn: RET; INT3, except we only have a single struct
1032 * insn here. Mark it retpoline_safe to avoid the SLS
1033 * warning, instead of adding another insn.
1034 */
1035 insn->retpoline_safe = true;
1036 }
1037
1038 return;
1039 }
1040 }
1041
add_call_dest(struct objtool_file * file,struct instruction * insn,struct symbol * dest,bool sibling)1042 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1043 struct symbol *dest, bool sibling)
1044 {
1045 insn->call_dest = dest;
1046 if (!dest)
1047 return;
1048
1049 /*
1050 * Whatever stack impact regular CALLs have, should be undone
1051 * by the RETURN of the called function.
1052 *
1053 * Annotated intra-function calls retain the stack_ops but
1054 * are converted to JUMP, see read_intra_function_calls().
1055 */
1056 remove_insn_ops(insn);
1057
1058 annotate_call_site(file, insn, sibling);
1059 }
1060
add_retpoline_call(struct objtool_file * file,struct instruction * insn)1061 static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1062 {
1063 /*
1064 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1065 * so convert them accordingly.
1066 */
1067 switch (insn->type) {
1068 case INSN_CALL:
1069 insn->type = INSN_CALL_DYNAMIC;
1070 break;
1071 case INSN_JUMP_UNCONDITIONAL:
1072 insn->type = INSN_JUMP_DYNAMIC;
1073 break;
1074 case INSN_JUMP_CONDITIONAL:
1075 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1076 break;
1077 default:
1078 return;
1079 }
1080
1081 insn->retpoline_safe = true;
1082
1083 /*
1084 * Whatever stack impact regular CALLs have, should be undone
1085 * by the RETURN of the called function.
1086 *
1087 * Annotated intra-function calls retain the stack_ops but
1088 * are converted to JUMP, see read_intra_function_calls().
1089 */
1090 remove_insn_ops(insn);
1091
1092 annotate_call_site(file, insn, false);
1093 }
1094
add_return_call(struct objtool_file * file,struct instruction * insn,bool add)1095 static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add)
1096 {
1097 /*
1098 * Return thunk tail calls are really just returns in disguise,
1099 * so convert them accordingly.
1100 */
1101 insn->type = INSN_RETURN;
1102 insn->retpoline_safe = true;
1103
1104 /* Skip the non-text sections, specially .discard ones */
1105 if (add && insn->sec->text)
1106 list_add_tail(&insn->call_node, &file->return_thunk_list);
1107 }
1108
1109 /*
1110 * Find the destination instructions for all jumps.
1111 */
add_jump_destinations(struct objtool_file * file)1112 static int add_jump_destinations(struct objtool_file *file)
1113 {
1114 struct instruction *insn;
1115 struct reloc *reloc;
1116 struct section *dest_sec;
1117 unsigned long dest_off;
1118
1119 for_each_insn(file, insn) {
1120 if (!is_static_jump(insn))
1121 continue;
1122
1123 reloc = insn_reloc(file, insn);
1124 if (!reloc) {
1125 dest_sec = insn->sec;
1126 dest_off = arch_jump_destination(insn);
1127 } else if (reloc->sym->type == STT_SECTION) {
1128 dest_sec = reloc->sym->sec;
1129 dest_off = arch_dest_reloc_offset(reloc->addend);
1130 } else if (reloc->sym->retpoline_thunk) {
1131 add_retpoline_call(file, insn);
1132 continue;
1133 } else if (reloc->sym->return_thunk) {
1134 add_return_call(file, insn, true);
1135 continue;
1136 } else if (insn->func) {
1137 /* internal or external sibling call (with reloc) */
1138 add_call_dest(file, insn, reloc->sym, true);
1139 continue;
1140 } else if (reloc->sym->sec->idx) {
1141 dest_sec = reloc->sym->sec;
1142 dest_off = reloc->sym->sym.st_value +
1143 arch_dest_reloc_offset(reloc->addend);
1144 } else {
1145 /* non-func asm code jumping to another file */
1146 continue;
1147 }
1148
1149 insn->jump_dest = find_insn(file, dest_sec, dest_off);
1150 if (!insn->jump_dest) {
1151 struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off);
1152
1153 /*
1154 * This is a special case where an alt instruction
1155 * jumps past the end of the section. These are
1156 * handled later in handle_group_alt().
1157 */
1158 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1159 continue;
1160
1161 /*
1162 * This is a special case for zen_untrain_ret().
1163 * It jumps to __x86_return_thunk(), but objtool
1164 * can't find the thunk's starting RET
1165 * instruction, because the RET is also in the
1166 * middle of another instruction. Objtool only
1167 * knows about the outer instruction.
1168 */
1169 if (sym && sym->return_thunk) {
1170 add_return_call(file, insn, false);
1171 continue;
1172 }
1173
1174 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1175 insn->sec, insn->offset, dest_sec->name,
1176 dest_off);
1177 return -1;
1178 }
1179
1180 /*
1181 * Cross-function jump.
1182 */
1183 if (insn->func && insn->jump_dest->func &&
1184 insn->func != insn->jump_dest->func) {
1185
1186 /*
1187 * For GCC 8+, create parent/child links for any cold
1188 * subfunctions. This is _mostly_ redundant with a
1189 * similar initialization in read_symbols().
1190 *
1191 * If a function has aliases, we want the *first* such
1192 * function in the symbol table to be the subfunction's
1193 * parent. In that case we overwrite the
1194 * initialization done in read_symbols().
1195 *
1196 * However this code can't completely replace the
1197 * read_symbols() code because this doesn't detect the
1198 * case where the parent function's only reference to a
1199 * subfunction is through a jump table.
1200 */
1201 if (!strstr(insn->func->name, ".cold") &&
1202 strstr(insn->jump_dest->func->name, ".cold")) {
1203 insn->func->cfunc = insn->jump_dest->func;
1204 insn->jump_dest->func->pfunc = insn->func;
1205
1206 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
1207 insn->jump_dest->offset == insn->jump_dest->func->offset) {
1208 /* internal sibling call (without reloc) */
1209 add_call_dest(file, insn, insn->jump_dest->func, true);
1210 }
1211 }
1212 }
1213
1214 return 0;
1215 }
1216
find_call_destination(struct section * sec,unsigned long offset)1217 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1218 {
1219 struct symbol *call_dest;
1220
1221 call_dest = find_func_by_offset(sec, offset);
1222 if (!call_dest)
1223 call_dest = find_symbol_by_offset(sec, offset);
1224
1225 return call_dest;
1226 }
1227
1228 /*
1229 * Find the destination instructions for all calls.
1230 */
add_call_destinations(struct objtool_file * file)1231 static int add_call_destinations(struct objtool_file *file)
1232 {
1233 struct instruction *insn;
1234 unsigned long dest_off;
1235 struct symbol *dest;
1236 struct reloc *reloc;
1237
1238 for_each_insn(file, insn) {
1239 if (insn->type != INSN_CALL)
1240 continue;
1241
1242 reloc = insn_reloc(file, insn);
1243 if (!reloc) {
1244 dest_off = arch_jump_destination(insn);
1245 dest = find_call_destination(insn->sec, dest_off);
1246
1247 add_call_dest(file, insn, dest, false);
1248
1249 if (insn->ignore)
1250 continue;
1251
1252 if (!insn->call_dest) {
1253 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1254 return -1;
1255 }
1256
1257 if (insn->func && insn->call_dest->type != STT_FUNC) {
1258 WARN_FUNC("unsupported call to non-function",
1259 insn->sec, insn->offset);
1260 return -1;
1261 }
1262
1263 } else if (reloc->sym->type == STT_SECTION) {
1264 dest_off = arch_dest_reloc_offset(reloc->addend);
1265 dest = find_call_destination(reloc->sym->sec, dest_off);
1266 if (!dest) {
1267 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1268 insn->sec, insn->offset,
1269 reloc->sym->sec->name,
1270 dest_off);
1271 return -1;
1272 }
1273
1274 add_call_dest(file, insn, dest, false);
1275
1276 } else if (reloc->sym->retpoline_thunk) {
1277 add_retpoline_call(file, insn);
1278
1279 } else
1280 add_call_dest(file, insn, reloc->sym, false);
1281 }
1282
1283 return 0;
1284 }
1285
1286 /*
1287 * The .alternatives section requires some extra special care over and above
1288 * other special sections because alternatives are patched in place.
1289 */
handle_group_alt(struct objtool_file * file,struct special_alt * special_alt,struct instruction * orig_insn,struct instruction ** new_insn)1290 static int handle_group_alt(struct objtool_file *file,
1291 struct special_alt *special_alt,
1292 struct instruction *orig_insn,
1293 struct instruction **new_insn)
1294 {
1295 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1296 struct alt_group *orig_alt_group, *new_alt_group;
1297 unsigned long dest_off;
1298
1299
1300 orig_alt_group = malloc(sizeof(*orig_alt_group));
1301 if (!orig_alt_group) {
1302 WARN("malloc failed");
1303 return -1;
1304 }
1305 orig_alt_group->cfi = calloc(special_alt->orig_len,
1306 sizeof(struct cfi_state *));
1307 if (!orig_alt_group->cfi) {
1308 WARN("calloc failed");
1309 return -1;
1310 }
1311
1312 last_orig_insn = NULL;
1313 insn = orig_insn;
1314 sec_for_each_insn_from(file, insn) {
1315 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1316 break;
1317
1318 insn->alt_group = orig_alt_group;
1319 last_orig_insn = insn;
1320 }
1321 orig_alt_group->orig_group = NULL;
1322 orig_alt_group->first_insn = orig_insn;
1323 orig_alt_group->last_insn = last_orig_insn;
1324
1325
1326 new_alt_group = malloc(sizeof(*new_alt_group));
1327 if (!new_alt_group) {
1328 WARN("malloc failed");
1329 return -1;
1330 }
1331
1332 if (special_alt->new_len < special_alt->orig_len) {
1333 /*
1334 * Insert a fake nop at the end to make the replacement
1335 * alt_group the same size as the original. This is needed to
1336 * allow propagate_alt_cfi() to do its magic. When the last
1337 * instruction affects the stack, the instruction after it (the
1338 * nop) will propagate the new state to the shared CFI array.
1339 */
1340 nop = malloc(sizeof(*nop));
1341 if (!nop) {
1342 WARN("malloc failed");
1343 return -1;
1344 }
1345 memset(nop, 0, sizeof(*nop));
1346 INIT_LIST_HEAD(&nop->alts);
1347 INIT_LIST_HEAD(&nop->stack_ops);
1348
1349 nop->sec = special_alt->new_sec;
1350 nop->offset = special_alt->new_off + special_alt->new_len;
1351 nop->len = special_alt->orig_len - special_alt->new_len;
1352 nop->type = INSN_NOP;
1353 nop->func = orig_insn->func;
1354 nop->alt_group = new_alt_group;
1355 nop->ignore = orig_insn->ignore_alts;
1356 }
1357
1358 if (!special_alt->new_len) {
1359 *new_insn = nop;
1360 goto end;
1361 }
1362
1363 insn = *new_insn;
1364 sec_for_each_insn_from(file, insn) {
1365 struct reloc *alt_reloc;
1366
1367 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1368 break;
1369
1370 last_new_insn = insn;
1371
1372 insn->ignore = orig_insn->ignore_alts;
1373 insn->func = orig_insn->func;
1374 insn->alt_group = new_alt_group;
1375
1376 /*
1377 * Since alternative replacement code is copy/pasted by the
1378 * kernel after applying relocations, generally such code can't
1379 * have relative-address relocation references to outside the
1380 * .altinstr_replacement section, unless the arch's
1381 * alternatives code can adjust the relative offsets
1382 * accordingly.
1383 */
1384 alt_reloc = insn_reloc(file, insn);
1385 if (alt_reloc &&
1386 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1387
1388 WARN_FUNC("unsupported relocation in alternatives section",
1389 insn->sec, insn->offset);
1390 return -1;
1391 }
1392
1393 if (!is_static_jump(insn))
1394 continue;
1395
1396 if (!insn->immediate)
1397 continue;
1398
1399 dest_off = arch_jump_destination(insn);
1400 if (dest_off == special_alt->new_off + special_alt->new_len)
1401 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1402
1403 if (!insn->jump_dest) {
1404 WARN_FUNC("can't find alternative jump destination",
1405 insn->sec, insn->offset);
1406 return -1;
1407 }
1408 }
1409
1410 if (!last_new_insn) {
1411 WARN_FUNC("can't find last new alternative instruction",
1412 special_alt->new_sec, special_alt->new_off);
1413 return -1;
1414 }
1415
1416 if (nop)
1417 list_add(&nop->list, &last_new_insn->list);
1418 end:
1419 new_alt_group->orig_group = orig_alt_group;
1420 new_alt_group->first_insn = *new_insn;
1421 new_alt_group->last_insn = nop ? : last_new_insn;
1422 new_alt_group->cfi = orig_alt_group->cfi;
1423 return 0;
1424 }
1425
1426 /*
1427 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1428 * If the original instruction is a jump, make the alt entry an effective nop
1429 * by just skipping the original instruction.
1430 */
handle_jump_alt(struct objtool_file * file,struct special_alt * special_alt,struct instruction * orig_insn,struct instruction ** new_insn)1431 static int handle_jump_alt(struct objtool_file *file,
1432 struct special_alt *special_alt,
1433 struct instruction *orig_insn,
1434 struct instruction **new_insn)
1435 {
1436 if (orig_insn->type == INSN_NOP)
1437 return 0;
1438
1439 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1440 WARN_FUNC("unsupported instruction at jump label",
1441 orig_insn->sec, orig_insn->offset);
1442 return -1;
1443 }
1444
1445 *new_insn = list_next_entry(orig_insn, list);
1446 return 0;
1447 }
1448
1449 /*
1450 * Read all the special sections which have alternate instructions which can be
1451 * patched in or redirected to at runtime. Each instruction having alternate
1452 * instruction(s) has them added to its insn->alts list, which will be
1453 * traversed in validate_branch().
1454 */
add_special_section_alts(struct objtool_file * file)1455 static int add_special_section_alts(struct objtool_file *file)
1456 {
1457 struct list_head special_alts;
1458 struct instruction *orig_insn, *new_insn;
1459 struct special_alt *special_alt, *tmp;
1460 struct alternative *alt;
1461 int ret;
1462
1463 ret = special_get_alts(file->elf, &special_alts);
1464 if (ret)
1465 return ret;
1466
1467 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1468
1469 orig_insn = find_insn(file, special_alt->orig_sec,
1470 special_alt->orig_off);
1471 if (!orig_insn) {
1472 WARN_FUNC("special: can't find orig instruction",
1473 special_alt->orig_sec, special_alt->orig_off);
1474 ret = -1;
1475 goto out;
1476 }
1477
1478 new_insn = NULL;
1479 if (!special_alt->group || special_alt->new_len) {
1480 new_insn = find_insn(file, special_alt->new_sec,
1481 special_alt->new_off);
1482 if (!new_insn) {
1483 WARN_FUNC("special: can't find new instruction",
1484 special_alt->new_sec,
1485 special_alt->new_off);
1486 ret = -1;
1487 goto out;
1488 }
1489 }
1490
1491 if (special_alt->group) {
1492 if (!special_alt->orig_len) {
1493 WARN_FUNC("empty alternative entry",
1494 orig_insn->sec, orig_insn->offset);
1495 continue;
1496 }
1497
1498 ret = handle_group_alt(file, special_alt, orig_insn,
1499 &new_insn);
1500 if (ret)
1501 goto out;
1502 } else if (special_alt->jump_or_nop) {
1503 ret = handle_jump_alt(file, special_alt, orig_insn,
1504 &new_insn);
1505 if (ret)
1506 goto out;
1507 }
1508
1509 alt = malloc(sizeof(*alt));
1510 if (!alt) {
1511 WARN("malloc failed");
1512 ret = -1;
1513 goto out;
1514 }
1515
1516 alt->insn = new_insn;
1517 alt->skip_orig = special_alt->skip_orig;
1518 orig_insn->ignore_alts |= special_alt->skip_alt;
1519 list_add_tail(&alt->list, &orig_insn->alts);
1520
1521 list_del(&special_alt->list);
1522 free(special_alt);
1523 }
1524
1525 out:
1526 return ret;
1527 }
1528
add_jump_table(struct objtool_file * file,struct instruction * insn,struct reloc * table)1529 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1530 struct reloc *table)
1531 {
1532 struct reloc *reloc = table;
1533 struct instruction *dest_insn;
1534 struct alternative *alt;
1535 struct symbol *pfunc = insn->func->pfunc;
1536 unsigned int prev_offset = 0;
1537
1538 /*
1539 * Each @reloc is a switch table relocation which points to the target
1540 * instruction.
1541 */
1542 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1543
1544 /* Check for the end of the table: */
1545 if (reloc != table && reloc->jump_table_start)
1546 break;
1547
1548 /* Make sure the table entries are consecutive: */
1549 if (prev_offset && reloc->offset != prev_offset + 8)
1550 break;
1551
1552 /* Detect function pointers from contiguous objects: */
1553 if (reloc->sym->sec == pfunc->sec &&
1554 reloc->addend == pfunc->offset)
1555 break;
1556
1557 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1558 if (!dest_insn)
1559 break;
1560
1561 /* Make sure the destination is in the same function: */
1562 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1563 break;
1564
1565 alt = malloc(sizeof(*alt));
1566 if (!alt) {
1567 WARN("malloc failed");
1568 return -1;
1569 }
1570
1571 alt->insn = dest_insn;
1572 list_add_tail(&alt->list, &insn->alts);
1573 prev_offset = reloc->offset;
1574 }
1575
1576 if (!prev_offset) {
1577 WARN_FUNC("can't find switch jump table",
1578 insn->sec, insn->offset);
1579 return -1;
1580 }
1581
1582 return 0;
1583 }
1584
1585 /*
1586 * find_jump_table() - Given a dynamic jump, find the switch jump table
1587 * associated with it.
1588 */
find_jump_table(struct objtool_file * file,struct symbol * func,struct instruction * insn)1589 static struct reloc *find_jump_table(struct objtool_file *file,
1590 struct symbol *func,
1591 struct instruction *insn)
1592 {
1593 struct reloc *table_reloc;
1594 struct instruction *dest_insn, *orig_insn = insn;
1595
1596 /*
1597 * Backward search using the @first_jump_src links, these help avoid
1598 * much of the 'in between' code. Which avoids us getting confused by
1599 * it.
1600 */
1601 for (;
1602 insn && insn->func && insn->func->pfunc == func;
1603 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1604
1605 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1606 break;
1607
1608 /* allow small jumps within the range */
1609 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1610 insn->jump_dest &&
1611 (insn->jump_dest->offset <= insn->offset ||
1612 insn->jump_dest->offset > orig_insn->offset))
1613 break;
1614
1615 table_reloc = arch_find_switch_table(file, insn);
1616 if (!table_reloc)
1617 continue;
1618 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1619 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1620 continue;
1621
1622 return table_reloc;
1623 }
1624
1625 return NULL;
1626 }
1627
1628 /*
1629 * First pass: Mark the head of each jump table so that in the next pass,
1630 * we know when a given jump table ends and the next one starts.
1631 */
mark_func_jump_tables(struct objtool_file * file,struct symbol * func)1632 static void mark_func_jump_tables(struct objtool_file *file,
1633 struct symbol *func)
1634 {
1635 struct instruction *insn, *last = NULL;
1636 struct reloc *reloc;
1637
1638 func_for_each_insn(file, func, insn) {
1639 if (!last)
1640 last = insn;
1641
1642 /*
1643 * Store back-pointers for unconditional forward jumps such
1644 * that find_jump_table() can back-track using those and
1645 * avoid some potentially confusing code.
1646 */
1647 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1648 insn->offset > last->offset &&
1649 insn->jump_dest->offset > insn->offset &&
1650 !insn->jump_dest->first_jump_src) {
1651
1652 insn->jump_dest->first_jump_src = insn;
1653 last = insn->jump_dest;
1654 }
1655
1656 if (insn->type != INSN_JUMP_DYNAMIC)
1657 continue;
1658
1659 reloc = find_jump_table(file, func, insn);
1660 if (reloc) {
1661 reloc->jump_table_start = true;
1662 insn->jump_table = reloc;
1663 }
1664 }
1665 }
1666
add_func_jump_tables(struct objtool_file * file,struct symbol * func)1667 static int add_func_jump_tables(struct objtool_file *file,
1668 struct symbol *func)
1669 {
1670 struct instruction *insn;
1671 int ret;
1672
1673 func_for_each_insn(file, func, insn) {
1674 if (!insn->jump_table)
1675 continue;
1676
1677 ret = add_jump_table(file, insn, insn->jump_table);
1678 if (ret)
1679 return ret;
1680 }
1681
1682 return 0;
1683 }
1684
1685 /*
1686 * For some switch statements, gcc generates a jump table in the .rodata
1687 * section which contains a list of addresses within the function to jump to.
1688 * This finds these jump tables and adds them to the insn->alts lists.
1689 */
add_jump_table_alts(struct objtool_file * file)1690 static int add_jump_table_alts(struct objtool_file *file)
1691 {
1692 struct section *sec;
1693 struct symbol *func;
1694 int ret;
1695
1696 if (!file->rodata)
1697 return 0;
1698
1699 for_each_sec(file, sec) {
1700 list_for_each_entry(func, &sec->symbol_list, list) {
1701 if (func->type != STT_FUNC)
1702 continue;
1703
1704 mark_func_jump_tables(file, func);
1705 ret = add_func_jump_tables(file, func);
1706 if (ret)
1707 return ret;
1708 }
1709 }
1710
1711 return 0;
1712 }
1713
set_func_state(struct cfi_state * state)1714 static void set_func_state(struct cfi_state *state)
1715 {
1716 state->cfa = initial_func_cfi.cfa;
1717 memcpy(&state->regs, &initial_func_cfi.regs,
1718 CFI_NUM_REGS * sizeof(struct cfi_reg));
1719 state->stack_size = initial_func_cfi.cfa.offset;
1720 }
1721
read_unwind_hints(struct objtool_file * file)1722 static int read_unwind_hints(struct objtool_file *file)
1723 {
1724 struct cfi_state cfi = init_cfi;
1725 struct section *sec, *relocsec;
1726 struct unwind_hint *hint;
1727 struct instruction *insn;
1728 struct reloc *reloc;
1729 int i;
1730
1731 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1732 if (!sec)
1733 return 0;
1734
1735 relocsec = sec->reloc;
1736 if (!relocsec) {
1737 WARN("missing .rela.discard.unwind_hints section");
1738 return -1;
1739 }
1740
1741 if (sec->len % sizeof(struct unwind_hint)) {
1742 WARN("struct unwind_hint size mismatch");
1743 return -1;
1744 }
1745
1746 file->hints = true;
1747
1748 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1749 hint = (struct unwind_hint *)sec->data->d_buf + i;
1750
1751 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1752 if (!reloc) {
1753 WARN("can't find reloc for unwind_hints[%d]", i);
1754 return -1;
1755 }
1756
1757 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1758 if (!insn) {
1759 WARN("can't find insn for unwind_hints[%d]", i);
1760 return -1;
1761 }
1762
1763 insn->hint = true;
1764
1765 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1766 insn->hint = false;
1767 insn->save = true;
1768 continue;
1769 }
1770
1771 if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1772 insn->restore = true;
1773 continue;
1774 }
1775
1776 if (hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) {
1777 struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset);
1778
1779 if (sym && sym->bind == STB_GLOBAL) {
1780 insn->entry = 1;
1781 }
1782 }
1783
1784 if (hint->type == UNWIND_HINT_TYPE_ENTRY) {
1785 hint->type = UNWIND_HINT_TYPE_CALL;
1786 insn->entry = 1;
1787 }
1788
1789 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1790 insn->cfi = &func_cfi;
1791 continue;
1792 }
1793
1794 if (insn->cfi)
1795 cfi = *(insn->cfi);
1796
1797 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
1798 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1799 insn->sec, insn->offset, hint->sp_reg);
1800 return -1;
1801 }
1802
1803 cfi.cfa.offset = hint->sp_offset;
1804 cfi.type = hint->type;
1805 cfi.end = hint->end;
1806
1807 insn->cfi = cfi_hash_find_or_add(&cfi);
1808 }
1809
1810 return 0;
1811 }
1812
read_retpoline_hints(struct objtool_file * file)1813 static int read_retpoline_hints(struct objtool_file *file)
1814 {
1815 struct section *sec;
1816 struct instruction *insn;
1817 struct reloc *reloc;
1818
1819 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1820 if (!sec)
1821 return 0;
1822
1823 list_for_each_entry(reloc, &sec->reloc_list, list) {
1824 if (reloc->sym->type != STT_SECTION) {
1825 WARN("unexpected relocation symbol type in %s", sec->name);
1826 return -1;
1827 }
1828
1829 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1830 if (!insn) {
1831 WARN("bad .discard.retpoline_safe entry");
1832 return -1;
1833 }
1834
1835 if (insn->type != INSN_JUMP_DYNAMIC &&
1836 insn->type != INSN_CALL_DYNAMIC &&
1837 insn->type != INSN_RETURN &&
1838 insn->type != INSN_NOP) {
1839 WARN_FUNC("retpoline_safe hint not an indirect jump/call/ret/nop",
1840 insn->sec, insn->offset);
1841 return -1;
1842 }
1843
1844 insn->retpoline_safe = true;
1845 }
1846
1847 return 0;
1848 }
1849
read_instr_hints(struct objtool_file * file)1850 static int read_instr_hints(struct objtool_file *file)
1851 {
1852 struct section *sec;
1853 struct instruction *insn;
1854 struct reloc *reloc;
1855
1856 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1857 if (!sec)
1858 return 0;
1859
1860 list_for_each_entry(reloc, &sec->reloc_list, list) {
1861 if (reloc->sym->type != STT_SECTION) {
1862 WARN("unexpected relocation symbol type in %s", sec->name);
1863 return -1;
1864 }
1865
1866 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1867 if (!insn) {
1868 WARN("bad .discard.instr_end entry");
1869 return -1;
1870 }
1871
1872 insn->instr--;
1873 }
1874
1875 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1876 if (!sec)
1877 return 0;
1878
1879 list_for_each_entry(reloc, &sec->reloc_list, list) {
1880 if (reloc->sym->type != STT_SECTION) {
1881 WARN("unexpected relocation symbol type in %s", sec->name);
1882 return -1;
1883 }
1884
1885 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1886 if (!insn) {
1887 WARN("bad .discard.instr_begin entry");
1888 return -1;
1889 }
1890
1891 insn->instr++;
1892 }
1893
1894 return 0;
1895 }
1896
read_intra_function_calls(struct objtool_file * file)1897 static int read_intra_function_calls(struct objtool_file *file)
1898 {
1899 struct instruction *insn;
1900 struct section *sec;
1901 struct reloc *reloc;
1902
1903 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1904 if (!sec)
1905 return 0;
1906
1907 list_for_each_entry(reloc, &sec->reloc_list, list) {
1908 unsigned long dest_off;
1909
1910 if (reloc->sym->type != STT_SECTION) {
1911 WARN("unexpected relocation symbol type in %s",
1912 sec->name);
1913 return -1;
1914 }
1915
1916 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1917 if (!insn) {
1918 WARN("bad .discard.intra_function_call entry");
1919 return -1;
1920 }
1921
1922 if (insn->type != INSN_CALL) {
1923 WARN_FUNC("intra_function_call not a direct call",
1924 insn->sec, insn->offset);
1925 return -1;
1926 }
1927
1928 /*
1929 * Treat intra-function CALLs as JMPs, but with a stack_op.
1930 * See add_call_destinations(), which strips stack_ops from
1931 * normal CALLs.
1932 */
1933 insn->type = INSN_JUMP_UNCONDITIONAL;
1934
1935 dest_off = insn->offset + insn->len + insn->immediate;
1936 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1937 if (!insn->jump_dest) {
1938 WARN_FUNC("can't find call dest at %s+0x%lx",
1939 insn->sec, insn->offset,
1940 insn->sec->name, dest_off);
1941 return -1;
1942 }
1943 }
1944
1945 return 0;
1946 }
1947
classify_symbols(struct objtool_file * file)1948 static int classify_symbols(struct objtool_file *file)
1949 {
1950 struct section *sec;
1951 struct symbol *func;
1952
1953 for_each_sec(file, sec) {
1954 list_for_each_entry(func, &sec->symbol_list, list) {
1955 if (func->bind != STB_GLOBAL)
1956 continue;
1957
1958 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1959 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1960 func->static_call_tramp = true;
1961
1962 if (arch_is_retpoline(func))
1963 func->retpoline_thunk = true;
1964
1965 if (arch_is_rethunk(func))
1966 func->return_thunk = true;
1967
1968 if (!strcmp(func->name, "__fentry__"))
1969 func->fentry = true;
1970
1971 if (!strncmp(func->name, "__sanitizer_cov_", 16))
1972 func->kcov = true;
1973 }
1974 }
1975
1976 return 0;
1977 }
1978
mark_rodata(struct objtool_file * file)1979 static void mark_rodata(struct objtool_file *file)
1980 {
1981 struct section *sec;
1982 bool found = false;
1983
1984 /*
1985 * Search for the following rodata sections, each of which can
1986 * potentially contain jump tables:
1987 *
1988 * - .rodata: can contain GCC switch tables
1989 * - .rodata.<func>: same, if -fdata-sections is being used
1990 * - .rodata..c_jump_table: contains C annotated jump tables
1991 *
1992 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1993 */
1994 for_each_sec(file, sec) {
1995 if (!strncmp(sec->name, ".rodata", 7) &&
1996 !strstr(sec->name, ".str1.")) {
1997 sec->rodata = true;
1998 found = true;
1999 }
2000 }
2001
2002 file->rodata = found;
2003 }
2004
decode_sections(struct objtool_file * file)2005 static int decode_sections(struct objtool_file *file)
2006 {
2007 int ret;
2008
2009 mark_rodata(file);
2010
2011 ret = decode_instructions(file);
2012 if (ret)
2013 return ret;
2014
2015 ret = add_dead_ends(file);
2016 if (ret)
2017 return ret;
2018
2019 add_ignores(file);
2020 add_uaccess_safe(file);
2021
2022 ret = add_ignore_alternatives(file);
2023 if (ret)
2024 return ret;
2025
2026 /*
2027 * Must be before add_{jump_call}_destination.
2028 */
2029 ret = classify_symbols(file);
2030 if (ret)
2031 return ret;
2032
2033 /*
2034 * Must be before add_special_section_alts() as that depends on
2035 * jump_dest being set.
2036 */
2037 ret = add_jump_destinations(file);
2038 if (ret)
2039 return ret;
2040
2041 ret = add_special_section_alts(file);
2042 if (ret)
2043 return ret;
2044
2045 /*
2046 * Must be before add_call_destination(); it changes INSN_CALL to
2047 * INSN_JUMP.
2048 */
2049 ret = read_intra_function_calls(file);
2050 if (ret)
2051 return ret;
2052
2053 ret = add_call_destinations(file);
2054 if (ret)
2055 return ret;
2056
2057 ret = add_jump_table_alts(file);
2058 if (ret)
2059 return ret;
2060
2061 ret = read_unwind_hints(file);
2062 if (ret)
2063 return ret;
2064
2065 ret = read_retpoline_hints(file);
2066 if (ret)
2067 return ret;
2068
2069 ret = read_instr_hints(file);
2070 if (ret)
2071 return ret;
2072
2073 return 0;
2074 }
2075
is_fentry_call(struct instruction * insn)2076 static bool is_fentry_call(struct instruction *insn)
2077 {
2078 if (insn->type == INSN_CALL &&
2079 insn->call_dest &&
2080 insn->call_dest->fentry)
2081 return true;
2082
2083 return false;
2084 }
2085
has_modified_stack_frame(struct instruction * insn,struct insn_state * state)2086 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
2087 {
2088 struct cfi_state *cfi = &state->cfi;
2089 int i;
2090
2091 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
2092 return true;
2093
2094 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
2095 return true;
2096
2097 if (cfi->stack_size != initial_func_cfi.cfa.offset)
2098 return true;
2099
2100 for (i = 0; i < CFI_NUM_REGS; i++) {
2101 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2102 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
2103 return true;
2104 }
2105
2106 return false;
2107 }
2108
has_valid_stack_frame(struct insn_state * state)2109 static bool has_valid_stack_frame(struct insn_state *state)
2110 {
2111 struct cfi_state *cfi = &state->cfi;
2112
2113 if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
2114 cfi->regs[CFI_BP].offset == -16)
2115 return true;
2116
2117 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
2118 return true;
2119
2120 return false;
2121 }
2122
update_cfi_state_regs(struct instruction * insn,struct cfi_state * cfi,struct stack_op * op)2123 static int update_cfi_state_regs(struct instruction *insn,
2124 struct cfi_state *cfi,
2125 struct stack_op *op)
2126 {
2127 struct cfi_reg *cfa = &cfi->cfa;
2128
2129 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
2130 return 0;
2131
2132 /* push */
2133 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
2134 cfa->offset += 8;
2135
2136 /* pop */
2137 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
2138 cfa->offset -= 8;
2139
2140 /* add immediate to sp */
2141 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2142 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2143 cfa->offset -= op->src.offset;
2144
2145 return 0;
2146 }
2147
save_reg(struct cfi_state * cfi,unsigned char reg,int base,int offset)2148 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2149 {
2150 if (arch_callee_saved_reg(reg) &&
2151 cfi->regs[reg].base == CFI_UNDEFINED) {
2152 cfi->regs[reg].base = base;
2153 cfi->regs[reg].offset = offset;
2154 }
2155 }
2156
restore_reg(struct cfi_state * cfi,unsigned char reg)2157 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2158 {
2159 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2160 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2161 }
2162
2163 /*
2164 * A note about DRAP stack alignment:
2165 *
2166 * GCC has the concept of a DRAP register, which is used to help keep track of
2167 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2168 * register. The typical DRAP pattern is:
2169 *
2170 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2171 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2172 * 41 ff 72 f8 pushq -0x8(%r10)
2173 * 55 push %rbp
2174 * 48 89 e5 mov %rsp,%rbp
2175 * (more pushes)
2176 * 41 52 push %r10
2177 * ...
2178 * 41 5a pop %r10
2179 * (more pops)
2180 * 5d pop %rbp
2181 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2182 * c3 retq
2183 *
2184 * There are some variations in the epilogues, like:
2185 *
2186 * 5b pop %rbx
2187 * 41 5a pop %r10
2188 * 41 5c pop %r12
2189 * 41 5d pop %r13
2190 * 41 5e pop %r14
2191 * c9 leaveq
2192 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2193 * c3 retq
2194 *
2195 * and:
2196 *
2197 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2198 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2199 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2200 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2201 * c9 leaveq
2202 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2203 * c3 retq
2204 *
2205 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2206 * restored beforehand:
2207 *
2208 * 41 55 push %r13
2209 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2210 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2211 * ...
2212 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2213 * 41 5d pop %r13
2214 * c3 retq
2215 */
update_cfi_state(struct instruction * insn,struct cfi_state * cfi,struct stack_op * op)2216 static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
2217 struct stack_op *op)
2218 {
2219 struct cfi_reg *cfa = &cfi->cfa;
2220 struct cfi_reg *regs = cfi->regs;
2221
2222 /* stack operations don't make sense with an undefined CFA */
2223 if (cfa->base == CFI_UNDEFINED) {
2224 if (insn->func) {
2225 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2226 return -1;
2227 }
2228 return 0;
2229 }
2230
2231 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2232 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2233 return update_cfi_state_regs(insn, cfi, op);
2234
2235 switch (op->dest.type) {
2236
2237 case OP_DEST_REG:
2238 switch (op->src.type) {
2239
2240 case OP_SRC_REG:
2241 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2242 cfa->base == CFI_SP &&
2243 regs[CFI_BP].base == CFI_CFA &&
2244 regs[CFI_BP].offset == -cfa->offset) {
2245
2246 /* mov %rsp, %rbp */
2247 cfa->base = op->dest.reg;
2248 cfi->bp_scratch = false;
2249 }
2250
2251 else if (op->src.reg == CFI_SP &&
2252 op->dest.reg == CFI_BP && cfi->drap) {
2253
2254 /* drap: mov %rsp, %rbp */
2255 regs[CFI_BP].base = CFI_BP;
2256 regs[CFI_BP].offset = -cfi->stack_size;
2257 cfi->bp_scratch = false;
2258 }
2259
2260 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2261
2262 /*
2263 * mov %rsp, %reg
2264 *
2265 * This is needed for the rare case where GCC
2266 * does:
2267 *
2268 * mov %rsp, %rax
2269 * ...
2270 * mov %rax, %rsp
2271 */
2272 cfi->vals[op->dest.reg].base = CFI_CFA;
2273 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2274 }
2275
2276 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2277 cfa->base == CFI_BP) {
2278
2279 /*
2280 * mov %rbp, %rsp
2281 *
2282 * Restore the original stack pointer (Clang).
2283 */
2284 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2285 }
2286
2287 else if (op->dest.reg == cfa->base) {
2288
2289 /* mov %reg, %rsp */
2290 if (cfa->base == CFI_SP &&
2291 cfi->vals[op->src.reg].base == CFI_CFA) {
2292
2293 /*
2294 * This is needed for the rare case
2295 * where GCC does something dumb like:
2296 *
2297 * lea 0x8(%rsp), %rcx
2298 * ...
2299 * mov %rcx, %rsp
2300 */
2301 cfa->offset = -cfi->vals[op->src.reg].offset;
2302 cfi->stack_size = cfa->offset;
2303
2304 } else {
2305 cfa->base = CFI_UNDEFINED;
2306 cfa->offset = 0;
2307 }
2308 }
2309
2310 break;
2311
2312 case OP_SRC_ADD:
2313 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2314
2315 /* add imm, %rsp */
2316 cfi->stack_size -= op->src.offset;
2317 if (cfa->base == CFI_SP)
2318 cfa->offset -= op->src.offset;
2319 break;
2320 }
2321
2322 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2323
2324 /* lea disp(%rbp), %rsp */
2325 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2326 break;
2327 }
2328
2329 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2330
2331 /* drap: lea disp(%rsp), %drap */
2332 cfi->drap_reg = op->dest.reg;
2333
2334 /*
2335 * lea disp(%rsp), %reg
2336 *
2337 * This is needed for the rare case where GCC
2338 * does something dumb like:
2339 *
2340 * lea 0x8(%rsp), %rcx
2341 * ...
2342 * mov %rcx, %rsp
2343 */
2344 cfi->vals[op->dest.reg].base = CFI_CFA;
2345 cfi->vals[op->dest.reg].offset = \
2346 -cfi->stack_size + op->src.offset;
2347
2348 break;
2349 }
2350
2351 if (cfi->drap && op->dest.reg == CFI_SP &&
2352 op->src.reg == cfi->drap_reg) {
2353
2354 /* drap: lea disp(%drap), %rsp */
2355 cfa->base = CFI_SP;
2356 cfa->offset = cfi->stack_size = -op->src.offset;
2357 cfi->drap_reg = CFI_UNDEFINED;
2358 cfi->drap = false;
2359 break;
2360 }
2361
2362 if (op->dest.reg == cfi->cfa.base) {
2363 WARN_FUNC("unsupported stack register modification",
2364 insn->sec, insn->offset);
2365 return -1;
2366 }
2367
2368 break;
2369
2370 case OP_SRC_AND:
2371 if (op->dest.reg != CFI_SP ||
2372 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2373 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2374 WARN_FUNC("unsupported stack pointer realignment",
2375 insn->sec, insn->offset);
2376 return -1;
2377 }
2378
2379 if (cfi->drap_reg != CFI_UNDEFINED) {
2380 /* drap: and imm, %rsp */
2381 cfa->base = cfi->drap_reg;
2382 cfa->offset = cfi->stack_size = 0;
2383 cfi->drap = true;
2384 }
2385
2386 /*
2387 * Older versions of GCC (4.8ish) realign the stack
2388 * without DRAP, with a frame pointer.
2389 */
2390
2391 break;
2392
2393 case OP_SRC_POP:
2394 case OP_SRC_POPF:
2395 if (!cfi->drap && op->dest.reg == cfa->base) {
2396
2397 /* pop %rbp */
2398 cfa->base = CFI_SP;
2399 }
2400
2401 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2402 op->dest.reg == cfi->drap_reg &&
2403 cfi->drap_offset == -cfi->stack_size) {
2404
2405 /* drap: pop %drap */
2406 cfa->base = cfi->drap_reg;
2407 cfa->offset = 0;
2408 cfi->drap_offset = -1;
2409
2410 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
2411
2412 /* pop %reg */
2413 restore_reg(cfi, op->dest.reg);
2414 }
2415
2416 cfi->stack_size -= 8;
2417 if (cfa->base == CFI_SP)
2418 cfa->offset -= 8;
2419
2420 break;
2421
2422 case OP_SRC_REG_INDIRECT:
2423 if (cfi->drap && op->src.reg == CFI_BP &&
2424 op->src.offset == cfi->drap_offset) {
2425
2426 /* drap: mov disp(%rbp), %drap */
2427 cfa->base = cfi->drap_reg;
2428 cfa->offset = 0;
2429 cfi->drap_offset = -1;
2430 }
2431
2432 if (cfi->drap && op->src.reg == CFI_BP &&
2433 op->src.offset == regs[op->dest.reg].offset) {
2434
2435 /* drap: mov disp(%rbp), %reg */
2436 restore_reg(cfi, op->dest.reg);
2437
2438 } else if (op->src.reg == cfa->base &&
2439 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2440
2441 /* mov disp(%rbp), %reg */
2442 /* mov disp(%rsp), %reg */
2443 restore_reg(cfi, op->dest.reg);
2444 }
2445
2446 break;
2447
2448 default:
2449 WARN_FUNC("unknown stack-related instruction",
2450 insn->sec, insn->offset);
2451 return -1;
2452 }
2453
2454 break;
2455
2456 case OP_DEST_PUSH:
2457 case OP_DEST_PUSHF:
2458 cfi->stack_size += 8;
2459 if (cfa->base == CFI_SP)
2460 cfa->offset += 8;
2461
2462 if (op->src.type != OP_SRC_REG)
2463 break;
2464
2465 if (cfi->drap) {
2466 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2467
2468 /* drap: push %drap */
2469 cfa->base = CFI_BP_INDIRECT;
2470 cfa->offset = -cfi->stack_size;
2471
2472 /* save drap so we know when to restore it */
2473 cfi->drap_offset = -cfi->stack_size;
2474
2475 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2476
2477 /* drap: push %rbp */
2478 cfi->stack_size = 0;
2479
2480 } else {
2481
2482 /* drap: push %reg */
2483 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2484 }
2485
2486 } else {
2487
2488 /* push %reg */
2489 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2490 }
2491
2492 /* detect when asm code uses rbp as a scratch register */
2493 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2494 cfa->base != CFI_BP)
2495 cfi->bp_scratch = true;
2496 break;
2497
2498 case OP_DEST_REG_INDIRECT:
2499
2500 if (cfi->drap) {
2501 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2502
2503 /* drap: mov %drap, disp(%rbp) */
2504 cfa->base = CFI_BP_INDIRECT;
2505 cfa->offset = op->dest.offset;
2506
2507 /* save drap offset so we know when to restore it */
2508 cfi->drap_offset = op->dest.offset;
2509 } else {
2510
2511 /* drap: mov reg, disp(%rbp) */
2512 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2513 }
2514
2515 } else if (op->dest.reg == cfa->base) {
2516
2517 /* mov reg, disp(%rbp) */
2518 /* mov reg, disp(%rsp) */
2519 save_reg(cfi, op->src.reg, CFI_CFA,
2520 op->dest.offset - cfi->cfa.offset);
2521 }
2522
2523 break;
2524
2525 case OP_DEST_LEAVE:
2526 if ((!cfi->drap && cfa->base != CFI_BP) ||
2527 (cfi->drap && cfa->base != cfi->drap_reg)) {
2528 WARN_FUNC("leave instruction with modified stack frame",
2529 insn->sec, insn->offset);
2530 return -1;
2531 }
2532
2533 /* leave (mov %rbp, %rsp; pop %rbp) */
2534
2535 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2536 restore_reg(cfi, CFI_BP);
2537
2538 if (!cfi->drap) {
2539 cfa->base = CFI_SP;
2540 cfa->offset -= 8;
2541 }
2542
2543 break;
2544
2545 case OP_DEST_MEM:
2546 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2547 WARN_FUNC("unknown stack-related memory operation",
2548 insn->sec, insn->offset);
2549 return -1;
2550 }
2551
2552 /* pop mem */
2553 cfi->stack_size -= 8;
2554 if (cfa->base == CFI_SP)
2555 cfa->offset -= 8;
2556
2557 break;
2558
2559 default:
2560 WARN_FUNC("unknown stack-related instruction",
2561 insn->sec, insn->offset);
2562 return -1;
2563 }
2564
2565 return 0;
2566 }
2567
2568 /*
2569 * The stack layouts of alternatives instructions can sometimes diverge when
2570 * they have stack modifications. That's fine as long as the potential stack
2571 * layouts don't conflict at any given potential instruction boundary.
2572 *
2573 * Flatten the CFIs of the different alternative code streams (both original
2574 * and replacement) into a single shared CFI array which can be used to detect
2575 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2576 */
propagate_alt_cfi(struct objtool_file * file,struct instruction * insn)2577 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2578 {
2579 struct cfi_state **alt_cfi;
2580 int group_off;
2581
2582 if (!insn->alt_group)
2583 return 0;
2584
2585 if (!insn->cfi) {
2586 WARN("CFI missing");
2587 return -1;
2588 }
2589
2590 alt_cfi = insn->alt_group->cfi;
2591 group_off = insn->offset - insn->alt_group->first_insn->offset;
2592
2593 if (!alt_cfi[group_off]) {
2594 alt_cfi[group_off] = insn->cfi;
2595 } else {
2596 if (cficmp(alt_cfi[group_off], insn->cfi)) {
2597 WARN_FUNC("stack layout conflict in alternatives",
2598 insn->sec, insn->offset);
2599 return -1;
2600 }
2601 }
2602
2603 return 0;
2604 }
2605
handle_insn_ops(struct instruction * insn,struct insn_state * state)2606 static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2607 {
2608 struct stack_op *op;
2609
2610 list_for_each_entry(op, &insn->stack_ops, list) {
2611
2612 if (update_cfi_state(insn, &state->cfi, op))
2613 return 1;
2614
2615 if (op->dest.type == OP_DEST_PUSHF) {
2616 if (!state->uaccess_stack) {
2617 state->uaccess_stack = 1;
2618 } else if (state->uaccess_stack >> 31) {
2619 WARN_FUNC("PUSHF stack exhausted",
2620 insn->sec, insn->offset);
2621 return 1;
2622 }
2623 state->uaccess_stack <<= 1;
2624 state->uaccess_stack |= state->uaccess;
2625 }
2626
2627 if (op->src.type == OP_SRC_POPF) {
2628 if (state->uaccess_stack) {
2629 state->uaccess = state->uaccess_stack & 1;
2630 state->uaccess_stack >>= 1;
2631 if (state->uaccess_stack == 1)
2632 state->uaccess_stack = 0;
2633 }
2634 }
2635 }
2636
2637 return 0;
2638 }
2639
insn_cfi_match(struct instruction * insn,struct cfi_state * cfi2)2640 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2641 {
2642 struct cfi_state *cfi1 = insn->cfi;
2643 int i;
2644
2645 if (!cfi1) {
2646 WARN("CFI missing");
2647 return false;
2648 }
2649
2650 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2651
2652 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2653 insn->sec, insn->offset,
2654 cfi1->cfa.base, cfi1->cfa.offset,
2655 cfi2->cfa.base, cfi2->cfa.offset);
2656
2657 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2658 for (i = 0; i < CFI_NUM_REGS; i++) {
2659 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2660 sizeof(struct cfi_reg)))
2661 continue;
2662
2663 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2664 insn->sec, insn->offset,
2665 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2666 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2667 break;
2668 }
2669
2670 } else if (cfi1->type != cfi2->type) {
2671
2672 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2673 insn->sec, insn->offset, cfi1->type, cfi2->type);
2674
2675 } else if (cfi1->drap != cfi2->drap ||
2676 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2677 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2678
2679 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2680 insn->sec, insn->offset,
2681 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2682 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2683
2684 } else
2685 return true;
2686
2687 return false;
2688 }
2689
func_uaccess_safe(struct symbol * func)2690 static inline bool func_uaccess_safe(struct symbol *func)
2691 {
2692 if (func)
2693 return func->uaccess_safe;
2694
2695 return false;
2696 }
2697
call_dest_name(struct instruction * insn)2698 static inline const char *call_dest_name(struct instruction *insn)
2699 {
2700 if (insn->call_dest)
2701 return insn->call_dest->name;
2702
2703 return "{dynamic}";
2704 }
2705
noinstr_call_dest(struct symbol * func)2706 static inline bool noinstr_call_dest(struct symbol *func)
2707 {
2708 /*
2709 * We can't deal with indirect function calls at present;
2710 * assume they're instrumented.
2711 */
2712 if (!func)
2713 return false;
2714
2715 /*
2716 * If the symbol is from a noinstr section; we good.
2717 */
2718 if (func->sec->noinstr)
2719 return true;
2720
2721 /*
2722 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2723 * something 'BAD' happened. At the risk of taking the machine down,
2724 * let them proceed to get the message out.
2725 */
2726 if (!strncmp(func->name, "__ubsan_handle_", 15))
2727 return true;
2728
2729 return false;
2730 }
2731
validate_call(struct instruction * insn,struct insn_state * state)2732 static int validate_call(struct instruction *insn, struct insn_state *state)
2733 {
2734 if (state->noinstr && state->instr <= 0 &&
2735 !noinstr_call_dest(insn->call_dest)) {
2736 WARN_FUNC("call to %s() leaves .noinstr.text section",
2737 insn->sec, insn->offset, call_dest_name(insn));
2738 return 1;
2739 }
2740
2741 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2742 WARN_FUNC("call to %s() with UACCESS enabled",
2743 insn->sec, insn->offset, call_dest_name(insn));
2744 return 1;
2745 }
2746
2747 if (state->df) {
2748 WARN_FUNC("call to %s() with DF set",
2749 insn->sec, insn->offset, call_dest_name(insn));
2750 return 1;
2751 }
2752
2753 return 0;
2754 }
2755
validate_sibling_call(struct instruction * insn,struct insn_state * state)2756 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2757 {
2758 if (has_modified_stack_frame(insn, state)) {
2759 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2760 insn->sec, insn->offset);
2761 return 1;
2762 }
2763
2764 return validate_call(insn, state);
2765 }
2766
validate_return(struct symbol * func,struct instruction * insn,struct insn_state * state)2767 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2768 {
2769 if (state->noinstr && state->instr > 0) {
2770 WARN_FUNC("return with instrumentation enabled",
2771 insn->sec, insn->offset);
2772 return 1;
2773 }
2774
2775 if (state->uaccess && !func_uaccess_safe(func)) {
2776 WARN_FUNC("return with UACCESS enabled",
2777 insn->sec, insn->offset);
2778 return 1;
2779 }
2780
2781 if (!state->uaccess && func_uaccess_safe(func)) {
2782 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2783 insn->sec, insn->offset);
2784 return 1;
2785 }
2786
2787 if (state->df) {
2788 WARN_FUNC("return with DF set",
2789 insn->sec, insn->offset);
2790 return 1;
2791 }
2792
2793 if (func && has_modified_stack_frame(insn, state)) {
2794 WARN_FUNC("return with modified stack frame",
2795 insn->sec, insn->offset);
2796 return 1;
2797 }
2798
2799 if (state->cfi.bp_scratch) {
2800 WARN_FUNC("BP used as a scratch register",
2801 insn->sec, insn->offset);
2802 return 1;
2803 }
2804
2805 return 0;
2806 }
2807
next_insn_to_validate(struct objtool_file * file,struct instruction * insn)2808 static struct instruction *next_insn_to_validate(struct objtool_file *file,
2809 struct instruction *insn)
2810 {
2811 struct alt_group *alt_group = insn->alt_group;
2812
2813 /*
2814 * Simulate the fact that alternatives are patched in-place. When the
2815 * end of a replacement alt_group is reached, redirect objtool flow to
2816 * the end of the original alt_group.
2817 */
2818 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2819 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2820
2821 return next_insn_same_sec(file, insn);
2822 }
2823
2824 /*
2825 * Follow the branch starting at the given instruction, and recursively follow
2826 * any other branches (jumps). Meanwhile, track the frame pointer state at
2827 * each instruction and validate all the rules described in
2828 * tools/objtool/Documentation/stack-validation.txt.
2829 */
validate_branch(struct objtool_file * file,struct symbol * func,struct instruction * insn,struct insn_state state)2830 static int validate_branch(struct objtool_file *file, struct symbol *func,
2831 struct instruction *insn, struct insn_state state)
2832 {
2833 struct alternative *alt;
2834 struct instruction *next_insn, *prev_insn = NULL;
2835 struct section *sec;
2836 u8 visited;
2837 int ret;
2838
2839 sec = insn->sec;
2840
2841 while (1) {
2842 next_insn = next_insn_to_validate(file, insn);
2843
2844 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2845 WARN("%s() falls through to next function %s()",
2846 func->name, insn->func->name);
2847 return 1;
2848 }
2849
2850 if (func && insn->ignore) {
2851 WARN_FUNC("BUG: why am I validating an ignored function?",
2852 sec, insn->offset);
2853 return 1;
2854 }
2855
2856 visited = VISITED_BRANCH << state.uaccess;
2857 if (insn->visited & VISITED_BRANCH_MASK) {
2858 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2859 return 1;
2860
2861 if (insn->visited & visited)
2862 return 0;
2863 } else {
2864 nr_insns_visited++;
2865 }
2866
2867 if (state.noinstr)
2868 state.instr += insn->instr;
2869
2870 if (insn->hint) {
2871 if (insn->restore) {
2872 struct instruction *save_insn, *i;
2873
2874 i = insn;
2875 save_insn = NULL;
2876
2877 sym_for_each_insn_continue_reverse(file, func, i) {
2878 if (i->save) {
2879 save_insn = i;
2880 break;
2881 }
2882 }
2883
2884 if (!save_insn) {
2885 WARN_FUNC("no corresponding CFI save for CFI restore",
2886 sec, insn->offset);
2887 return 1;
2888 }
2889
2890 if (!save_insn->visited) {
2891 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
2892 sec, insn->offset);
2893 return 1;
2894 }
2895
2896 insn->cfi = save_insn->cfi;
2897 nr_cfi_reused++;
2898 }
2899
2900 state.cfi = *insn->cfi;
2901 } else {
2902 /* XXX track if we actually changed state.cfi */
2903
2904 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
2905 insn->cfi = prev_insn->cfi;
2906 nr_cfi_reused++;
2907 } else {
2908 insn->cfi = cfi_hash_find_or_add(&state.cfi);
2909 }
2910 }
2911
2912 insn->visited |= visited;
2913
2914 if (propagate_alt_cfi(file, insn))
2915 return 1;
2916
2917 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2918 bool skip_orig = false;
2919
2920 list_for_each_entry(alt, &insn->alts, list) {
2921 if (alt->skip_orig)
2922 skip_orig = true;
2923
2924 ret = validate_branch(file, func, alt->insn, state);
2925 if (ret) {
2926 if (backtrace)
2927 BT_FUNC("(alt)", insn);
2928 return ret;
2929 }
2930 }
2931
2932 if (skip_orig)
2933 return 0;
2934 }
2935
2936 if (handle_insn_ops(insn, &state))
2937 return 1;
2938
2939 switch (insn->type) {
2940
2941 case INSN_RETURN:
2942 if (sls && !insn->retpoline_safe &&
2943 next_insn && next_insn->type != INSN_TRAP) {
2944 WARN_FUNC("missing int3 after ret",
2945 insn->sec, insn->offset);
2946 }
2947 return validate_return(func, insn, &state);
2948
2949 case INSN_CALL:
2950 case INSN_CALL_DYNAMIC:
2951 ret = validate_call(insn, &state);
2952 if (ret)
2953 return ret;
2954
2955 if (!no_fp && func && !is_fentry_call(insn) &&
2956 !has_valid_stack_frame(&state)) {
2957 WARN_FUNC("call without frame pointer save/setup",
2958 sec, insn->offset);
2959 return 1;
2960 }
2961
2962 if (dead_end_function(file, insn->call_dest))
2963 return 0;
2964
2965 break;
2966
2967 case INSN_JUMP_CONDITIONAL:
2968 case INSN_JUMP_UNCONDITIONAL:
2969 if (is_sibling_call(insn)) {
2970 ret = validate_sibling_call(insn, &state);
2971 if (ret)
2972 return ret;
2973
2974 } else if (insn->jump_dest) {
2975 ret = validate_branch(file, func,
2976 insn->jump_dest, state);
2977 if (ret) {
2978 if (backtrace)
2979 BT_FUNC("(branch)", insn);
2980 return ret;
2981 }
2982 }
2983
2984 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2985 return 0;
2986
2987 break;
2988
2989 case INSN_JUMP_DYNAMIC:
2990 if (sls && !insn->retpoline_safe &&
2991 next_insn && next_insn->type != INSN_TRAP) {
2992 WARN_FUNC("missing int3 after indirect jump",
2993 insn->sec, insn->offset);
2994 }
2995
2996 /* fallthrough */
2997 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2998 if (is_sibling_call(insn)) {
2999 ret = validate_sibling_call(insn, &state);
3000 if (ret)
3001 return ret;
3002 }
3003
3004 if (insn->type == INSN_JUMP_DYNAMIC)
3005 return 0;
3006
3007 break;
3008
3009 case INSN_CONTEXT_SWITCH:
3010 if (func && (!next_insn || !next_insn->hint)) {
3011 WARN_FUNC("unsupported instruction in callable function",
3012 sec, insn->offset);
3013 return 1;
3014 }
3015 return 0;
3016
3017 case INSN_STAC:
3018 if (state.uaccess) {
3019 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
3020 return 1;
3021 }
3022
3023 state.uaccess = true;
3024 break;
3025
3026 case INSN_CLAC:
3027 if (!state.uaccess && func) {
3028 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
3029 return 1;
3030 }
3031
3032 if (func_uaccess_safe(func) && !state.uaccess_stack) {
3033 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
3034 return 1;
3035 }
3036
3037 state.uaccess = false;
3038 break;
3039
3040 case INSN_STD:
3041 if (state.df) {
3042 WARN_FUNC("recursive STD", sec, insn->offset);
3043 return 1;
3044 }
3045
3046 state.df = true;
3047 break;
3048
3049 case INSN_CLD:
3050 if (!state.df && func) {
3051 WARN_FUNC("redundant CLD", sec, insn->offset);
3052 return 1;
3053 }
3054
3055 state.df = false;
3056 break;
3057
3058 default:
3059 break;
3060 }
3061
3062 if (insn->dead_end)
3063 return 0;
3064
3065 if (!next_insn) {
3066 if (state.cfi.cfa.base == CFI_UNDEFINED)
3067 return 0;
3068 WARN("%s: unexpected end of section", sec->name);
3069 return 1;
3070 }
3071
3072 prev_insn = insn;
3073 insn = next_insn;
3074 }
3075
3076 return 0;
3077 }
3078
validate_unwind_hints(struct objtool_file * file,struct section * sec)3079 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
3080 {
3081 struct instruction *insn;
3082 struct insn_state state;
3083 int ret, warnings = 0;
3084
3085 if (!file->hints)
3086 return 0;
3087
3088 init_insn_state(&state, sec);
3089
3090 if (sec) {
3091 insn = find_insn(file, sec, 0);
3092 if (!insn)
3093 return 0;
3094 } else {
3095 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
3096 }
3097
3098 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
3099 if (insn->hint && !insn->visited) {
3100 ret = validate_branch(file, insn->func, insn, state);
3101 if (ret && backtrace)
3102 BT_FUNC("<=== (hint)", insn);
3103 warnings += ret;
3104 }
3105
3106 insn = list_next_entry(insn, list);
3107 }
3108
3109 return warnings;
3110 }
3111
3112 /*
3113 * Validate rethunk entry constraint: must untrain RET before the first RET.
3114 *
3115 * Follow every branch (intra-function) and ensure ANNOTATE_UNRET_END comes
3116 * before an actual RET instruction.
3117 */
validate_entry(struct objtool_file * file,struct instruction * insn)3118 static int validate_entry(struct objtool_file *file, struct instruction *insn)
3119 {
3120 struct instruction *next, *dest;
3121 int ret, warnings = 0;
3122
3123 for (;;) {
3124 next = next_insn_to_validate(file, insn);
3125
3126 if (insn->visited & VISITED_ENTRY)
3127 return 0;
3128
3129 insn->visited |= VISITED_ENTRY;
3130
3131 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
3132 struct alternative *alt;
3133 bool skip_orig = false;
3134
3135 list_for_each_entry(alt, &insn->alts, list) {
3136 if (alt->skip_orig)
3137 skip_orig = true;
3138
3139 ret = validate_entry(file, alt->insn);
3140 if (ret) {
3141 if (backtrace)
3142 BT_FUNC("(alt)", insn);
3143 return ret;
3144 }
3145 }
3146
3147 if (skip_orig)
3148 return 0;
3149 }
3150
3151 switch (insn->type) {
3152
3153 case INSN_CALL_DYNAMIC:
3154 case INSN_JUMP_DYNAMIC:
3155 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3156 WARN_FUNC("early indirect call", insn->sec, insn->offset);
3157 return 1;
3158
3159 case INSN_JUMP_UNCONDITIONAL:
3160 case INSN_JUMP_CONDITIONAL:
3161 if (!is_sibling_call(insn)) {
3162 if (!insn->jump_dest) {
3163 WARN_FUNC("unresolved jump target after linking?!?",
3164 insn->sec, insn->offset);
3165 return -1;
3166 }
3167 ret = validate_entry(file, insn->jump_dest);
3168 if (ret) {
3169 if (backtrace) {
3170 BT_FUNC("(branch%s)", insn,
3171 insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : "");
3172 }
3173 return ret;
3174 }
3175
3176 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3177 return 0;
3178
3179 break;
3180 }
3181
3182 /* fallthrough */
3183 case INSN_CALL:
3184 dest = find_insn(file, insn->call_dest->sec,
3185 insn->call_dest->offset);
3186 if (!dest) {
3187 WARN("Unresolved function after linking!?: %s",
3188 insn->call_dest->name);
3189 return -1;
3190 }
3191
3192 ret = validate_entry(file, dest);
3193 if (ret) {
3194 if (backtrace)
3195 BT_FUNC("(call)", insn);
3196 return ret;
3197 }
3198 /*
3199 * If a call returns without error, it must have seen UNTRAIN_RET.
3200 * Therefore any non-error return is a success.
3201 */
3202 return 0;
3203
3204 case INSN_RETURN:
3205 WARN_FUNC("RET before UNTRAIN", insn->sec, insn->offset);
3206 return 1;
3207
3208 case INSN_NOP:
3209 if (insn->retpoline_safe)
3210 return 0;
3211 break;
3212
3213 default:
3214 break;
3215 }
3216
3217 if (!next) {
3218 WARN_FUNC("teh end!", insn->sec, insn->offset);
3219 return -1;
3220 }
3221 insn = next;
3222 }
3223
3224 return warnings;
3225 }
3226
3227 /*
3228 * Validate that all branches starting at 'insn->entry' encounter UNRET_END
3229 * before RET.
3230 */
validate_unret(struct objtool_file * file)3231 static int validate_unret(struct objtool_file *file)
3232 {
3233 struct instruction *insn;
3234 int ret, warnings = 0;
3235
3236 for_each_insn(file, insn) {
3237 if (!insn->entry)
3238 continue;
3239
3240 ret = validate_entry(file, insn);
3241 if (ret < 0) {
3242 WARN_FUNC("Failed UNRET validation", insn->sec, insn->offset);
3243 return ret;
3244 }
3245 warnings += ret;
3246 }
3247
3248 return warnings;
3249 }
3250
validate_retpoline(struct objtool_file * file)3251 static int validate_retpoline(struct objtool_file *file)
3252 {
3253 struct instruction *insn;
3254 int warnings = 0;
3255
3256 for_each_insn(file, insn) {
3257 if (insn->type != INSN_JUMP_DYNAMIC &&
3258 insn->type != INSN_CALL_DYNAMIC &&
3259 insn->type != INSN_RETURN)
3260 continue;
3261
3262 if (insn->retpoline_safe)
3263 continue;
3264
3265 /*
3266 * .init.text code is ran before userspace and thus doesn't
3267 * strictly need retpolines, except for modules which are
3268 * loaded late, they very much do need retpoline in their
3269 * .init.text
3270 */
3271 if (!strcmp(insn->sec->name, ".init.text") && !module)
3272 continue;
3273
3274 if (insn->type == INSN_RETURN) {
3275 if (rethunk) {
3276 WARN_FUNC("'naked' return found in RETHUNK build",
3277 insn->sec, insn->offset);
3278 } else
3279 continue;
3280 } else {
3281 WARN_FUNC("indirect %s found in RETPOLINE build",
3282 insn->sec, insn->offset,
3283 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3284 }
3285
3286 warnings++;
3287 }
3288
3289 return warnings;
3290 }
3291
is_kasan_insn(struct instruction * insn)3292 static bool is_kasan_insn(struct instruction *insn)
3293 {
3294 return (insn->type == INSN_CALL &&
3295 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3296 }
3297
is_ubsan_insn(struct instruction * insn)3298 static bool is_ubsan_insn(struct instruction *insn)
3299 {
3300 return (insn->type == INSN_CALL &&
3301 !strcmp(insn->call_dest->name,
3302 "__ubsan_handle_builtin_unreachable"));
3303 }
3304
ignore_unreachable_insn(struct objtool_file * file,struct instruction * insn)3305 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
3306 {
3307 int i;
3308 struct instruction *prev_insn;
3309
3310 if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP)
3311 return true;
3312
3313 /*
3314 * Ignore any unused exceptions. This can happen when a whitelisted
3315 * function has an exception table entry.
3316 *
3317 * Also ignore alternative replacement instructions. This can happen
3318 * when a whitelisted function uses one of the ALTERNATIVE macros.
3319 */
3320 if (!strcmp(insn->sec->name, ".fixup") ||
3321 !strcmp(insn->sec->name, ".altinstr_replacement") ||
3322 !strcmp(insn->sec->name, ".altinstr_aux"))
3323 return true;
3324
3325 if (!insn->func)
3326 return false;
3327
3328 /*
3329 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3330 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3331 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3332 * (or occasionally a JMP to UD2).
3333 *
3334 * It may also insert a UD2 after calling a __noreturn function.
3335 */
3336 prev_insn = list_prev_entry(insn, list);
3337 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
3338 (insn->type == INSN_BUG ||
3339 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3340 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3341 return true;
3342
3343 /*
3344 * Check if this (or a subsequent) instruction is related to
3345 * CONFIG_UBSAN or CONFIG_KASAN.
3346 *
3347 * End the search at 5 instructions to avoid going into the weeds.
3348 */
3349 for (i = 0; i < 5; i++) {
3350
3351 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3352 return true;
3353
3354 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3355 if (insn->jump_dest &&
3356 insn->jump_dest->func == insn->func) {
3357 insn = insn->jump_dest;
3358 continue;
3359 }
3360
3361 break;
3362 }
3363
3364 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3365 break;
3366
3367 insn = list_next_entry(insn, list);
3368 }
3369
3370 return false;
3371 }
3372
validate_symbol(struct objtool_file * file,struct section * sec,struct symbol * sym,struct insn_state * state)3373 static int validate_symbol(struct objtool_file *file, struct section *sec,
3374 struct symbol *sym, struct insn_state *state)
3375 {
3376 struct instruction *insn;
3377 int ret;
3378
3379 if (!sym->len) {
3380 WARN("%s() is missing an ELF size annotation", sym->name);
3381 return 1;
3382 }
3383
3384 if (sym->pfunc != sym || sym->alias != sym)
3385 return 0;
3386
3387 insn = find_insn(file, sec, sym->offset);
3388 if (!insn || insn->ignore || insn->visited)
3389 return 0;
3390
3391 state->uaccess = sym->uaccess_safe;
3392
3393 ret = validate_branch(file, insn->func, insn, *state);
3394 if (ret && backtrace)
3395 BT_FUNC("<=== (sym)", insn);
3396 return ret;
3397 }
3398
validate_section(struct objtool_file * file,struct section * sec)3399 static int validate_section(struct objtool_file *file, struct section *sec)
3400 {
3401 struct insn_state state;
3402 struct symbol *func;
3403 int warnings = 0;
3404
3405 list_for_each_entry(func, &sec->symbol_list, list) {
3406 if (func->type != STT_FUNC)
3407 continue;
3408
3409 init_insn_state(&state, sec);
3410 set_func_state(&state.cfi);
3411
3412 warnings += validate_symbol(file, sec, func, &state);
3413 }
3414
3415 return warnings;
3416 }
3417
validate_vmlinux_functions(struct objtool_file * file)3418 static int validate_vmlinux_functions(struct objtool_file *file)
3419 {
3420 struct section *sec;
3421 int warnings = 0;
3422
3423 sec = find_section_by_name(file->elf, ".noinstr.text");
3424 if (sec) {
3425 warnings += validate_section(file, sec);
3426 warnings += validate_unwind_hints(file, sec);
3427 }
3428
3429 sec = find_section_by_name(file->elf, ".entry.text");
3430 if (sec) {
3431 warnings += validate_section(file, sec);
3432 warnings += validate_unwind_hints(file, sec);
3433 }
3434
3435 return warnings;
3436 }
3437
validate_functions(struct objtool_file * file)3438 static int validate_functions(struct objtool_file *file)
3439 {
3440 struct section *sec;
3441 int warnings = 0;
3442
3443 for_each_sec(file, sec) {
3444 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3445 continue;
3446
3447 warnings += validate_section(file, sec);
3448 }
3449
3450 return warnings;
3451 }
3452
validate_reachable_instructions(struct objtool_file * file)3453 static int validate_reachable_instructions(struct objtool_file *file)
3454 {
3455 struct instruction *insn;
3456
3457 if (file->ignore_unreachables)
3458 return 0;
3459
3460 for_each_insn(file, insn) {
3461 if (insn->visited || ignore_unreachable_insn(file, insn))
3462 continue;
3463
3464 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3465 return 1;
3466 }
3467
3468 return 0;
3469 }
3470
check(struct objtool_file * file)3471 int check(struct objtool_file *file)
3472 {
3473 int ret, warnings = 0;
3474
3475 arch_initial_func_cfi_state(&initial_func_cfi);
3476 init_cfi_state(&init_cfi);
3477 init_cfi_state(&func_cfi);
3478 set_func_state(&func_cfi);
3479
3480 if (!cfi_hash_alloc())
3481 goto out;
3482
3483 cfi_hash_add(&init_cfi);
3484 cfi_hash_add(&func_cfi);
3485
3486 ret = decode_sections(file);
3487 if (ret < 0)
3488 goto out;
3489
3490 warnings += ret;
3491
3492 if (list_empty(&file->insn_list))
3493 goto out;
3494
3495 if (vmlinux && !validate_dup) {
3496 ret = validate_vmlinux_functions(file);
3497 if (ret < 0)
3498 goto out;
3499
3500 warnings += ret;
3501 goto out;
3502 }
3503
3504 if (retpoline) {
3505 ret = validate_retpoline(file);
3506 if (ret < 0)
3507 return ret;
3508 warnings += ret;
3509 }
3510
3511 ret = validate_functions(file);
3512 if (ret < 0)
3513 goto out;
3514 warnings += ret;
3515
3516 ret = validate_unwind_hints(file, NULL);
3517 if (ret < 0)
3518 goto out;
3519 warnings += ret;
3520
3521 if (unret) {
3522 /*
3523 * Must be after validate_branch() and friends, it plays
3524 * further games with insn->visited.
3525 */
3526 ret = validate_unret(file);
3527 if (ret < 0)
3528 return ret;
3529 warnings += ret;
3530 }
3531
3532 if (!warnings) {
3533 ret = validate_reachable_instructions(file);
3534 if (ret < 0)
3535 goto out;
3536 warnings += ret;
3537 }
3538
3539 ret = create_static_call_sections(file);
3540 if (ret < 0)
3541 goto out;
3542 warnings += ret;
3543
3544 if (retpoline) {
3545 ret = create_retpoline_sites_sections(file);
3546 if (ret < 0)
3547 goto out;
3548 warnings += ret;
3549 }
3550
3551 if (rethunk) {
3552 ret = create_return_sites_sections(file);
3553 if (ret < 0)
3554 goto out;
3555 warnings += ret;
3556 }
3557
3558 if (stats) {
3559 printf("nr_insns_visited: %ld\n", nr_insns_visited);
3560 printf("nr_cfi: %ld\n", nr_cfi);
3561 printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
3562 printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
3563 }
3564
3565 out:
3566 /*
3567 * For now, don't fail the kernel build on fatal warnings. These
3568 * errors are still fairly common due to the growing matrix of
3569 * supported toolchains and their recent pace of change.
3570 */
3571 return 0;
3572 }
3573