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