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