xref: /linux/tools/objtool/check.c (revision 029223d301620bc4e1086696047b0d5d6eba5edd)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
4  */
5 
6 #define _GNU_SOURCE /* memmem() */
7 #include <fnmatch.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <inttypes.h>
11 #include <sys/mman.h>
12 
13 #include <objtool/builtin.h>
14 #include <objtool/cfi.h>
15 #include <objtool/arch.h>
16 #include <objtool/disas.h>
17 #include <objtool/check.h>
18 #include <objtool/klp.h>
19 #include <objtool/special.h>
20 #include <objtool/trace.h>
21 #include <objtool/warn.h>
22 #include <objtool/util.h>
23 
24 #include <linux/objtool_types.h>
25 #include <linux/hashtable.h>
26 #include <linux/kernel.h>
27 #include <linux/static_call_types.h>
28 #include <linux/string.h>
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 static struct cfi_state force_undefined_cfi;
36 
37 struct disas_context *objtool_disas_ctx;
38 
39 size_t sym_name_max_len;
40 
41 struct instruction *find_insn(struct objtool_file *file,
42 			      struct section *sec, unsigned long offset)
43 {
44 	struct instruction *insn;
45 
46 	hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
47 		if (insn->sec == sec && insn->offset == offset)
48 			return insn;
49 	}
50 
51 	return NULL;
52 }
53 
54 struct instruction *next_insn_same_sec(struct objtool_file *file,
55 				       struct instruction *insn)
56 {
57 	if (insn->idx == INSN_CHUNK_MAX)
58 		return find_insn(file, insn->sec, insn->offset + insn->len);
59 
60 	insn++;
61 	if (!insn->len)
62 		return NULL;
63 
64 	return insn;
65 }
66 
67 struct instruction *next_insn_same_func(struct objtool_file *file,
68 				       struct instruction *insn)
69 {
70 	struct instruction *next = next_insn_same_sec(file, insn);
71 	struct symbol *func = insn_func(insn);
72 
73 	if (!func)
74 		return NULL;
75 
76 	if (next && insn_func(next) == func)
77 		return next;
78 
79 	/* Check if we're already in the subfunction: */
80 	if (func == func->cfunc)
81 		return NULL;
82 
83 	/* Move to the subfunction: */
84 	return find_insn(file, func->cfunc->sec, func->cfunc->offset);
85 }
86 
87 static struct instruction *prev_insn_same_sec(struct objtool_file *file,
88 					      struct instruction *insn)
89 {
90 	if (insn->idx == 0) {
91 		if (insn->prev_len)
92 			return find_insn(file, insn->sec, insn->offset - insn->prev_len);
93 		return NULL;
94 	}
95 
96 	return insn - 1;
97 }
98 
99 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
100 					      struct instruction *insn)
101 {
102 	struct instruction *prev = prev_insn_same_sec(file, insn);
103 
104 	if (prev && insn_func(prev) == insn_func(insn))
105 		return prev;
106 
107 	return NULL;
108 }
109 
110 #define for_each_insn(file, insn)					\
111 	for (struct section *__sec, *__fake = (struct section *)1;	\
112 	     __fake; __fake = NULL)					\
113 		for_each_sec(file->elf, __sec)				\
114 			sec_for_each_insn(file, __sec, insn)
115 
116 
117 #define sym_for_each_insn(file, sym, insn)				\
118 	for (insn = find_insn(file, sym->sec, sym->offset);		\
119 	     insn && insn->offset < sym->offset + sym->len;		\
120 	     insn = next_insn_same_sec(file, insn))
121 
122 #define sym_for_each_insn_continue_reverse(file, sym, insn)		\
123 	for (insn = prev_insn_same_sec(file, insn);			\
124 	     insn && insn->offset >= sym->offset;			\
125 	     insn = prev_insn_same_sec(file, insn))
126 
127 #define sec_for_each_insn_from(file, insn)				\
128 	for (; insn; insn = next_insn_same_sec(file, insn))
129 
130 #define sec_for_each_insn_continue(file, insn)				\
131 	for (insn = next_insn_same_sec(file, insn); insn;		\
132 	     insn = next_insn_same_sec(file, insn))
133 
134 static inline struct reloc *insn_jump_table(struct instruction *insn)
135 {
136 	if (insn->type == INSN_JUMP_DYNAMIC ||
137 	    insn->type == INSN_CALL_DYNAMIC)
138 		return insn->_jump_table;
139 
140 	return NULL;
141 }
142 
143 static inline unsigned long insn_jump_table_size(struct instruction *insn)
144 {
145 	if (insn->type == INSN_JUMP_DYNAMIC ||
146 	    insn->type == INSN_CALL_DYNAMIC)
147 		return insn->_jump_table_size;
148 
149 	return 0;
150 }
151 
152 static bool is_jump_table_jump(struct instruction *insn)
153 {
154 	struct alt_group *alt_group = insn->alt_group;
155 
156 	if (insn_jump_table(insn))
157 		return true;
158 
159 	/* Retpoline alternative for a jump table? */
160 	return alt_group && alt_group->orig_group &&
161 	       insn_jump_table(alt_group->orig_group->first_insn);
162 }
163 
164 static bool is_sibling_call(struct instruction *insn)
165 {
166 	/*
167 	 * Assume only STT_FUNC calls have jump-tables.
168 	 */
169 	if (insn_func(insn)) {
170 		/* An indirect jump is either a sibling call or a jump to a table. */
171 		if (insn->type == INSN_JUMP_DYNAMIC)
172 			return !is_jump_table_jump(insn);
173 	}
174 
175 	/* add_jump_destinations() sets insn_call_dest(insn) for sibling calls. */
176 	return (is_static_jump(insn) && insn_call_dest(insn));
177 }
178 
179 /*
180  * Checks if a function is a Rust "noreturn" one.
181  */
182 static bool is_rust_noreturn(const struct symbol *func)
183 {
184 	/*
185 	 * If it does not start with "_R", then it is not a Rust symbol.
186 	 */
187 	if (strncmp(func->name, "_R", 2))
188 		return false;
189 
190 	/*
191 	 * These are just heuristics -- we do not control the precise symbol
192 	 * name, due to the crate disambiguators (which depend on the compiler)
193 	 * as well as changes to the source code itself between versions (since
194 	 * these come from the Rust standard library).
195 	 */
196 	return str_ends_with(func->name, "_4core3num20from_str_radix_panic")				||
197 	       str_ends_with(func->name, "_4core3num22from_ascii_radix_panic")				||
198 	       str_ends_with(func->name, "_4core5sliceSp15copy_from_slice17len_mismatch_fail")		||
199 	       str_ends_with(func->name, "_4core6option13expect_failed")				||
200 	       str_ends_with(func->name, "_4core6option13unwrap_failed")				||
201 	       str_ends_with(func->name, "_4core6result13unwrap_failed")				||
202 	       str_ends_with(func->name, "_4core9panicking5panic")					||
203 	       str_ends_with(func->name, "_4core9panicking9panic_fmt")					||
204 	       str_ends_with(func->name, "_4core9panicking14panic_explicit")				||
205 	       str_ends_with(func->name, "_4core9panicking14panic_nounwind")				||
206 	       str_ends_with(func->name, "_4core9panicking18panic_bounds_check")			||
207 	       str_ends_with(func->name, "_4core9panicking18panic_nounwind_fmt")			||
208 	       str_ends_with(func->name, "_4core9panicking19assert_failed_inner")			||
209 	       str_ends_with(func->name, "_4core9panicking30panic_null_pointer_dereference")		||
210 	       str_ends_with(func->name, "_4core9panicking32panic_null_reference_constructed")		||
211 	       str_ends_with(func->name, "_4core9panicking36panic_misaligned_pointer_dereference")	||
212 	       str_ends_with(func->name, "_7___rustc17rust_begin_unwind")				||
213 	       strstr(func->name, "_4core9panicking13assert_failed")					||
214 	       strstr(func->name, "_4core9panicking11panic_const24panic_const_")			||
215 	       (strstr(func->name, "_4core5slice5index") &&
216 		strstr(func->name, "slice_") &&
217 		str_ends_with(func->name, "_fail"));
218 }
219 
220 /*
221  * This checks to see if the given function is a "noreturn" function.
222  *
223  * For global functions which are outside the scope of this object file, we
224  * have to keep a manual list of them.
225  *
226  * For local functions, we have to detect them manually by simply looking for
227  * the lack of a return instruction.
228  */
229 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
230 				int recursion)
231 {
232 	int i;
233 	struct instruction *insn;
234 	bool empty = true;
235 
236 #define NORETURN(func) __stringify(func),
237 	static const char * const global_noreturns[] = {
238 #include "noreturns.h"
239 	};
240 #undef NORETURN
241 
242 	if (!func)
243 		return false;
244 
245 	if (!is_local_sym(func)) {
246 		if (is_rust_noreturn(func))
247 			return true;
248 
249 		for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
250 			if (!strcmp(func->name, global_noreturns[i]))
251 				return true;
252 	}
253 
254 	if (is_weak_sym(func))
255 		return false;
256 
257 	if (!func->len)
258 		return false;
259 
260 	insn = find_insn(file, func->sec, func->offset);
261 	if (!insn || !insn_func(insn))
262 		return false;
263 
264 	func_for_each_insn(file, func, insn) {
265 		empty = false;
266 
267 		if (insn->type == INSN_RETURN)
268 			return false;
269 	}
270 
271 	if (empty)
272 		return false;
273 
274 	/*
275 	 * A function can have a sibling call instead of a return.  In that
276 	 * case, the function's dead-end status depends on whether the target
277 	 * of the sibling call returns.
278 	 */
279 	func_for_each_insn(file, func, insn) {
280 		if (is_sibling_call(insn)) {
281 			struct instruction *dest = insn->jump_dest;
282 
283 			if (!dest)
284 				/* sibling call to another file */
285 				return false;
286 
287 			/* local sibling call */
288 			if (recursion == 5) {
289 				/*
290 				 * Infinite recursion: two functions have
291 				 * sibling calls to each other.  This is a very
292 				 * rare case.  It means they aren't dead ends.
293 				 */
294 				return false;
295 			}
296 
297 			return __dead_end_function(file, insn_func(dest), recursion+1);
298 		}
299 	}
300 
301 	return true;
302 }
303 
304 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
305 {
306 	return __dead_end_function(file, func, 0);
307 }
308 
309 static void init_cfi_state(struct cfi_state *cfi)
310 {
311 	int i;
312 
313 	for (i = 0; i < CFI_NUM_REGS; i++) {
314 		cfi->regs[i].base = CFI_UNDEFINED;
315 		cfi->vals[i].base = CFI_UNDEFINED;
316 	}
317 	cfi->cfa.base = CFI_UNDEFINED;
318 	cfi->drap_reg = CFI_UNDEFINED;
319 	cfi->drap_offset = -1;
320 }
321 
322 static void init_insn_state(struct objtool_file *file, struct insn_state *state,
323 			    struct section *sec)
324 {
325 	memset(state, 0, sizeof(*state));
326 	init_cfi_state(&state->cfi);
327 
328 	if (opts.noinstr && sec)
329 		state->noinstr = sec->noinstr;
330 }
331 
332 static struct cfi_state *cfi_alloc(void)
333 {
334 	struct cfi_state *cfi = calloc(1, sizeof(struct cfi_state));
335 	if (!cfi) {
336 		ERROR_GLIBC("calloc");
337 		exit(1);
338 	}
339 	nr_cfi++;
340 	return cfi;
341 }
342 
343 static int cfi_bits;
344 static struct hlist_head *cfi_hash;
345 
346 static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
347 {
348 	return memcmp((void *)cfi1 + sizeof(cfi1->hash),
349 		      (void *)cfi2 + sizeof(cfi2->hash),
350 		      sizeof(struct cfi_state) - sizeof(struct hlist_node));
351 }
352 
353 static inline u32 cfi_key(struct cfi_state *cfi)
354 {
355 	return jhash((void *)cfi + sizeof(cfi->hash),
356 		     sizeof(*cfi) - sizeof(cfi->hash), 0);
357 }
358 
359 static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
360 {
361 	struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
362 	struct cfi_state *obj;
363 
364 	hlist_for_each_entry(obj, head, hash) {
365 		if (!cficmp(cfi, obj)) {
366 			nr_cfi_cache++;
367 			return obj;
368 		}
369 	}
370 
371 	obj = cfi_alloc();
372 	*obj = *cfi;
373 	hlist_add_head(&obj->hash, head);
374 
375 	return obj;
376 }
377 
378 static void cfi_hash_add(struct cfi_state *cfi)
379 {
380 	struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
381 
382 	hlist_add_head(&cfi->hash, head);
383 }
384 
385 static void *cfi_hash_alloc(unsigned long size)
386 {
387 	cfi_bits = max(10, ilog2(size));
388 	cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits,
389 			PROT_READ|PROT_WRITE,
390 			MAP_PRIVATE|MAP_ANON, -1, 0);
391 	if (cfi_hash == (void *)-1L) {
392 		ERROR_GLIBC("mmap fail cfi_hash");
393 		cfi_hash = NULL;
394 	}  else if (opts.stats) {
395 		printf("cfi_bits: %d\n", cfi_bits);
396 	}
397 
398 	return cfi_hash;
399 }
400 
401 static unsigned long nr_insns;
402 static unsigned long nr_insns_visited;
403 
404 /*
405  * Call the arch-specific instruction decoder for all the instructions and add
406  * them to the global instruction list.
407  */
408 static int decode_instructions(struct objtool_file *file)
409 {
410 	struct section *sec;
411 	struct symbol *func;
412 	unsigned long offset;
413 	struct instruction *insn;
414 
415 	for_each_sec(file->elf, sec) {
416 		struct instruction *insns = NULL;
417 		u8 prev_len = 0;
418 		u8 idx = 0;
419 
420 		if (!is_text_sec(sec))
421 			continue;
422 
423 		if (strcmp(sec->name, ".altinstr_replacement") &&
424 		    strcmp(sec->name, ".altinstr_aux") &&
425 		    strncmp(sec->name, ".discard.", 9))
426 			sec->text = true;
427 
428 		if (!strcmp(sec->name, ".noinstr.text") ||
429 		    !strcmp(sec->name, ".entry.text") ||
430 		    !strcmp(sec->name, ".cpuidle.text") ||
431 		    !strncmp(sec->name, ".text..__x86.", 13))
432 			sec->noinstr = true;
433 
434 		/*
435 		 * .init.text code is ran before userspace and thus doesn't
436 		 * strictly need retpolines, except for modules which are
437 		 * loaded late, they very much do need retpoline in their
438 		 * .init.text
439 		 */
440 		if (!strcmp(sec->name, ".init.text") && !opts.module)
441 			sec->init = true;
442 
443 		for (offset = 0; offset < sec_size(sec); offset += insn->len) {
444 			if (!insns || idx == INSN_CHUNK_MAX) {
445 				insns = calloc(INSN_CHUNK_SIZE, sizeof(*insn));
446 				if (!insns) {
447 					ERROR_GLIBC("calloc");
448 					return -1;
449 				}
450 				idx = 0;
451 			} else {
452 				idx++;
453 			}
454 			insn = &insns[idx];
455 			insn->idx = idx;
456 
457 			INIT_LIST_HEAD(&insn->call_node);
458 			insn->sec = sec;
459 			insn->offset = offset;
460 			insn->prev_len = prev_len;
461 
462 			if (arch_decode_instruction(file, sec, offset, sec_size(sec) - offset, insn))
463 				return -1;
464 
465 			prev_len = insn->len;
466 
467 			/*
468 			 * By default, "ud2" is a dead end unless otherwise
469 			 * annotated, because GCC 7 inserts it for certain
470 			 * divide-by-zero cases.
471 			 */
472 			if (insn->type == INSN_BUG)
473 				insn->dead_end = true;
474 
475 			hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
476 			nr_insns++;
477 		}
478 
479 		sec_for_each_sym(sec, func) {
480 			if (!is_notype_sym(func) && !is_func_sym(func))
481 				continue;
482 
483 			if (func->offset == sec_size(sec)) {
484 				/* Heuristic: likely an "end" symbol */
485 				if (is_notype_sym(func))
486 					continue;
487 				ERROR("%s(): STT_FUNC at end of section", func->name);
488 				return -1;
489 			}
490 
491 			if (func->embedded_insn || is_alias_sym(func))
492 				continue;
493 
494 			if (!find_insn(file, sec, func->offset)) {
495 				ERROR("%s(): can't find starting instruction", func->name);
496 				return -1;
497 			}
498 
499 			sym_for_each_insn(file, func, insn) {
500 				insn->_sym = func;
501 				if (is_func_sym(func) &&
502 				    insn->type == INSN_ENDBR &&
503 				    list_empty(&insn->call_node)) {
504 					if (insn->offset == func->offset) {
505 						list_add_tail(&insn->call_node, &file->endbr_list);
506 						file->nr_endbr++;
507 					} else {
508 						file->nr_endbr_int++;
509 					}
510 				}
511 			}
512 		}
513 	}
514 
515 	if (opts.stats)
516 		printf("nr_insns: %lu\n", nr_insns);
517 
518 	return 0;
519 }
520 
521 /*
522  * Known pv_ops*[] arrays.
523  */
524 static struct {
525 	const char *name;
526 	int idx_off;
527 } pv_ops_tables[] = {
528 	{ .name = "pv_ops", },
529 	{ .name = "pv_ops_lock", },
530 	{ .name = NULL, .idx_off = -1 }
531 };
532 
533 /*
534  * Get index offset for a pv_ops* array.
535  */
536 int pv_ops_idx_off(const char *symname)
537 {
538 	int idx;
539 
540 	for (idx = 0; pv_ops_tables[idx].name; idx++) {
541 		if (!strcmp(symname, pv_ops_tables[idx].name))
542 			break;
543 	}
544 
545 	return pv_ops_tables[idx].idx_off;
546 }
547 
548 /*
549  * Read a pv_ops*[] .data table to find the static initialized values.
550  */
551 static int add_pv_ops(struct objtool_file *file, int pv_ops_idx)
552 {
553 	struct symbol *sym, *func;
554 	unsigned long off, end;
555 	struct reloc *reloc;
556 	int idx, idx_off;
557 	const char *symname;
558 
559 	symname = pv_ops_tables[pv_ops_idx].name;
560 	sym = find_symbol_by_name(file->elf, symname);
561 	if (!sym) {
562 		ERROR("Unknown pv_ops array %s", symname);
563 		return -1;
564 	}
565 
566 	off = sym->offset;
567 	end = off + sym->len;
568 	idx_off = pv_ops_tables[pv_ops_idx].idx_off;
569 	if (idx_off < 0) {
570 		ERROR("pv_ops array %s has unknown index offset", symname);
571 		return -1;
572 	}
573 
574 	for (;;) {
575 		reloc = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off);
576 		if (!reloc)
577 			break;
578 
579 		idx = (reloc_offset(reloc) - sym->offset) / sizeof(unsigned long);
580 
581 		func = reloc->sym;
582 		if (is_sec_sym(func))
583 			func = find_symbol_by_offset(reloc->sym->sec,
584 						     reloc_addend(reloc));
585 		if (!func) {
586 			ERROR_FUNC(reloc->sym->sec, reloc_addend(reloc),
587 				   "can't find func at %s[%d]", symname, idx);
588 			return -1;
589 		}
590 
591 		if (objtool_pv_add(file, idx + idx_off, func))
592 			return -1;
593 
594 		off = reloc_offset(reloc) + 1;
595 		if (off > end)
596 			break;
597 	}
598 
599 	return 0;
600 }
601 
602 /*
603  * Allocate and initialize file->pv_ops[].
604  */
605 static int init_pv_ops(struct objtool_file *file)
606 {
607 	struct symbol *sym;
608 	int idx, nr;
609 
610 	if (!opts.noinstr)
611 		return 0;
612 
613 	file->pv_ops = NULL;
614 
615 	nr = 0;
616 	for (idx = 0; pv_ops_tables[idx].name; idx++) {
617 		sym = find_symbol_by_name(file->elf, pv_ops_tables[idx].name);
618 		if (!sym) {
619 			pv_ops_tables[idx].idx_off = -1;
620 			continue;
621 		}
622 		pv_ops_tables[idx].idx_off = nr;
623 		nr += sym->len / sizeof(unsigned long);
624 	}
625 
626 	if (nr == 0)
627 		return 0;
628 
629 	file->pv_ops = calloc(nr, sizeof(struct pv_state));
630 	if (!file->pv_ops) {
631 		ERROR_GLIBC("calloc");
632 		return -1;
633 	}
634 
635 	for (idx = 0; idx < nr; idx++)
636 		INIT_LIST_HEAD(&file->pv_ops[idx].targets);
637 
638 	for (idx = 0; pv_ops_tables[idx].name; idx++) {
639 		if (pv_ops_tables[idx].idx_off < 0)
640 			continue;
641 		if (add_pv_ops(file, idx))
642 			return -1;
643 	}
644 
645 	return 0;
646 }
647 
648 static bool is_livepatch_module(struct objtool_file *file)
649 {
650 	struct section *sec;
651 
652 	if (!opts.module)
653 		return false;
654 
655 	sec = find_section_by_name(file->elf, ".modinfo");
656 	if (!sec)
657 		return false;
658 
659 	return memmem(sec->data->d_buf, sec_size(sec), "\0livepatch=Y", 12);
660 }
661 
662 static int create_static_call_sections(struct objtool_file *file)
663 {
664 	struct static_call_site *site;
665 	struct section *sec;
666 	struct instruction *insn;
667 	struct symbol *key_sym;
668 	char *key_name, *tmp;
669 	int idx;
670 
671 	sec = find_section_by_name(file->elf, ".static_call_sites");
672 	if (sec) {
673 		/*
674 		 * Livepatch modules may have already extracted the static call
675 		 * site entries to take advantage of vmlinux static call
676 		 * privileges.
677 		 */
678 		if (!file->klp)
679 			WARN("file already has .static_call_sites section, skipping");
680 
681 		return 0;
682 	}
683 
684 	if (list_empty(&file->static_call_list))
685 		return 0;
686 
687 	idx = 0;
688 	list_for_each_entry(insn, &file->static_call_list, call_node)
689 		idx++;
690 
691 	sec = elf_create_section_pair(file->elf, ".static_call_sites",
692 				      sizeof(*site), idx, idx * 2);
693 	if (!sec)
694 		return -1;
695 
696 	/* Allow modules to modify the low bits of static_call_site::key */
697 	sec->sh.sh_flags |= SHF_WRITE;
698 
699 	idx = 0;
700 	list_for_each_entry(insn, &file->static_call_list, call_node) {
701 
702 		/* populate reloc for 'addr' */
703 		if (!elf_init_reloc_text_sym(file->elf, sec,
704 					     idx * sizeof(*site), idx * 2,
705 					     insn->sec, insn->offset))
706 			return -1;
707 
708 		/* find key symbol */
709 		key_name = strdup(insn_call_dest(insn)->name);
710 		if (!key_name) {
711 			ERROR_GLIBC("strdup");
712 			return -1;
713 		}
714 		if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
715 			    STATIC_CALL_TRAMP_PREFIX_LEN)) {
716 			ERROR("static_call: trampoline name malformed: %s", key_name);
717 			return -1;
718 		}
719 		tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
720 		memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
721 
722 		key_sym = find_symbol_by_name(file->elf, tmp);
723 		if (!key_sym) {
724 			if (!opts.module) {
725 				ERROR("static_call: can't find static_call_key symbol: %s", tmp);
726 				return -1;
727 			}
728 
729 			/*
730 			 * For modules(), the key might not be exported, which
731 			 * means the module can make static calls but isn't
732 			 * allowed to change them.
733 			 *
734 			 * In that case we temporarily set the key to be the
735 			 * trampoline address.  This is fixed up in
736 			 * static_call_add_module().
737 			 */
738 			key_sym = insn_call_dest(insn);
739 		}
740 
741 		/* populate reloc for 'key' */
742 		if (!elf_init_reloc_data_sym(file->elf, sec,
743 					     idx * sizeof(*site) + 4,
744 					     (idx * 2) + 1, key_sym,
745 					     is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
746 			return -1;
747 
748 		idx++;
749 	}
750 
751 	return 0;
752 }
753 
754 static int create_retpoline_sites_sections(struct objtool_file *file)
755 {
756 	struct instruction *insn;
757 	struct section *sec;
758 	int idx;
759 
760 	sec = find_section_by_name(file->elf, ".retpoline_sites");
761 	if (sec) {
762 		WARN("file already has .retpoline_sites, skipping");
763 		return 0;
764 	}
765 
766 	idx = 0;
767 	list_for_each_entry(insn, &file->retpoline_call_list, call_node)
768 		idx++;
769 
770 	if (!idx)
771 		return 0;
772 
773 	sec = elf_create_section_pair(file->elf, ".retpoline_sites",
774 				      sizeof(int), idx, idx);
775 	if (!sec)
776 		return -1;
777 
778 	idx = 0;
779 	list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
780 
781 		if (!elf_init_reloc_text_sym(file->elf, sec,
782 					     idx * sizeof(int), idx,
783 					     insn->sec, insn->offset))
784 			return -1;
785 
786 		idx++;
787 	}
788 
789 	return 0;
790 }
791 
792 static int create_return_sites_sections(struct objtool_file *file)
793 {
794 	struct instruction *insn;
795 	struct section *sec;
796 	int idx;
797 
798 	sec = find_section_by_name(file->elf, ".return_sites");
799 	if (sec) {
800 		WARN("file already has .return_sites, skipping");
801 		return 0;
802 	}
803 
804 	idx = 0;
805 	list_for_each_entry(insn, &file->return_thunk_list, call_node)
806 		idx++;
807 
808 	if (!idx)
809 		return 0;
810 
811 	sec = elf_create_section_pair(file->elf, ".return_sites",
812 				      sizeof(int), idx, idx);
813 	if (!sec)
814 		return -1;
815 
816 	idx = 0;
817 	list_for_each_entry(insn, &file->return_thunk_list, call_node) {
818 
819 		if (!elf_init_reloc_text_sym(file->elf, sec,
820 					     idx * sizeof(int), idx,
821 					     insn->sec, insn->offset))
822 			return -1;
823 
824 		idx++;
825 	}
826 
827 	return 0;
828 }
829 
830 static int create_ibt_endbr_seal_sections(struct objtool_file *file)
831 {
832 	struct instruction *insn;
833 	struct section *sec;
834 	int idx;
835 
836 	sec = find_section_by_name(file->elf, ".ibt_endbr_seal");
837 	if (sec) {
838 		WARN("file already has .ibt_endbr_seal, skipping");
839 		return 0;
840 	}
841 
842 	idx = 0;
843 	list_for_each_entry(insn, &file->endbr_list, call_node)
844 		idx++;
845 
846 	if (opts.stats) {
847 		printf("ibt: ENDBR at function start: %d\n", file->nr_endbr);
848 		printf("ibt: ENDBR inside functions:  %d\n", file->nr_endbr_int);
849 		printf("ibt: superfluous ENDBR:       %d\n", idx);
850 	}
851 
852 	if (!idx)
853 		return 0;
854 
855 	sec = elf_create_section_pair(file->elf, ".ibt_endbr_seal",
856 				      sizeof(int), idx, idx);
857 	if (!sec)
858 		return -1;
859 
860 	idx = 0;
861 	list_for_each_entry(insn, &file->endbr_list, call_node) {
862 
863 		int *site = (int *)sec->data->d_buf + idx;
864 		struct symbol *func = insn_func(insn);
865 		*site = 0;
866 
867 		if (opts.module && func && insn->offset == func->offset &&
868 		    (!strcmp(func->name, "init_module") ||
869 		     !strcmp(func->name, "cleanup_module"))) {
870 			ERROR("%s(): Magic init_module() function name is deprecated, use module_init(fn) instead",
871 			      func->name);
872 			return -1;
873 		}
874 
875 		if (!elf_init_reloc_text_sym(file->elf, sec,
876 					     idx * sizeof(int), idx,
877 					     insn->sec, insn->offset))
878 			return -1;
879 
880 		idx++;
881 	}
882 
883 	return 0;
884 }
885 
886 /*
887 * Grow __cfi_ symbols to fill the NOP gap between the 'mov <hash>, %rax' and
888 * the start of the function.
889 */
890 static int grow_cfi_symbols(struct objtool_file *file)
891 {
892 	struct symbol *sym;
893 
894 	for_each_sym(file->elf, sym) {
895 		if (!is_func_sym(sym) || !strstarts(sym->name, "__cfi_") ||
896 		    sym->len != 5)
897 			continue;
898 
899 		if (!find_func_by_offset(sym->sec, sym->offset + sym->len + opts.prefix))
900 			continue;
901 
902 		sym->len += opts.prefix;
903 		sym->sym.st_size = sym->len;
904 		if (elf_write_symbol(file->elf, sym))
905 			return -1;
906 	}
907 
908 	return 0;
909 }
910 
911 static int create_cfi_sections(struct objtool_file *file)
912 {
913 	struct section *sec;
914 	struct symbol *sym;
915 	int idx;
916 
917 	sec = find_section_by_name(file->elf, ".cfi_sites");
918 	if (sec) {
919 		WARN("file already has .cfi_sites section, skipping");
920 		return 0;
921 	}
922 
923 	idx = 0;
924 	for_each_sym(file->elf, sym) {
925 		if (!is_func_sym(sym))
926 			continue;
927 
928 		if (strncmp(sym->name, "__cfi_", 6))
929 			continue;
930 
931 		idx++;
932 	}
933 
934 	sec = elf_create_section_pair(file->elf, ".cfi_sites",
935 				      sizeof(unsigned int), idx, idx);
936 	if (!sec)
937 		return -1;
938 
939 	idx = 0;
940 	for_each_sym(file->elf, sym) {
941 		if (!is_func_sym(sym))
942 			continue;
943 
944 		if (strncmp(sym->name, "__cfi_", 6))
945 			continue;
946 
947 		if (!elf_init_reloc_text_sym(file->elf, sec,
948 					     idx * sizeof(unsigned int), idx,
949 					     sym->sec, sym->offset))
950 			return -1;
951 
952 		idx++;
953 	}
954 
955 	return 0;
956 }
957 
958 static int create_mcount_loc_sections(struct objtool_file *file)
959 {
960 	size_t addr_size = elf_addr_size(file->elf);
961 	struct instruction *insn;
962 	struct section *sec;
963 	int idx;
964 
965 	sec = find_section_by_name(file->elf, "__mcount_loc");
966 	if (sec) {
967 		/*
968 		 * Livepatch modules have already extracted their __mcount_loc
969 		 * entries to cover the !CONFIG_FTRACE_MCOUNT_USE_OBJTOOL case.
970 		 */
971 		if (!file->klp)
972 			WARN("file already has __mcount_loc section, skipping");
973 
974 		return 0;
975 	}
976 
977 	if (list_empty(&file->mcount_loc_list))
978 		return 0;
979 
980 	idx = 0;
981 	list_for_each_entry(insn, &file->mcount_loc_list, call_node)
982 		idx++;
983 
984 	sec = elf_create_section_pair(file->elf, "__mcount_loc", addr_size,
985 				      idx, idx);
986 	if (!sec)
987 		return -1;
988 
989 	sec->sh.sh_addralign = addr_size;
990 
991 	idx = 0;
992 	list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
993 
994 		struct reloc *reloc;
995 
996 		reloc = elf_init_reloc_text_sym(file->elf, sec, idx * addr_size, idx,
997 					       insn->sec, insn->offset);
998 		if (!reloc)
999 			return -1;
1000 
1001 		set_reloc_type(file->elf, reloc, addr_size == 8 ? R_ABS64 : R_ABS32);
1002 
1003 		idx++;
1004 	}
1005 
1006 	return 0;
1007 }
1008 
1009 static int create_direct_call_sections(struct objtool_file *file)
1010 {
1011 	struct instruction *insn;
1012 	struct section *sec;
1013 	int idx;
1014 
1015 	sec = find_section_by_name(file->elf, ".call_sites");
1016 	if (sec) {
1017 		WARN("file already has .call_sites section, skipping");
1018 		return 0;
1019 	}
1020 
1021 	if (list_empty(&file->call_list))
1022 		return 0;
1023 
1024 	idx = 0;
1025 	list_for_each_entry(insn, &file->call_list, call_node)
1026 		idx++;
1027 
1028 	sec = elf_create_section_pair(file->elf, ".call_sites",
1029 				      sizeof(unsigned int), idx, idx);
1030 	if (!sec)
1031 		return -1;
1032 
1033 	idx = 0;
1034 	list_for_each_entry(insn, &file->call_list, call_node) {
1035 
1036 		if (!elf_init_reloc_text_sym(file->elf, sec,
1037 					     idx * sizeof(unsigned int), idx,
1038 					     insn->sec, insn->offset))
1039 			return -1;
1040 
1041 		idx++;
1042 	}
1043 
1044 	return 0;
1045 }
1046 
1047 /*
1048  * Warnings shouldn't be reported for ignored functions.
1049  */
1050 static int add_ignores(struct objtool_file *file)
1051 {
1052 	struct section *rsec;
1053 	struct symbol *func;
1054 	struct reloc *reloc;
1055 
1056 	rsec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
1057 	if (!rsec)
1058 		return 0;
1059 
1060 	for_each_reloc(rsec, reloc) {
1061 		switch (reloc->sym->type) {
1062 		case STT_FUNC:
1063 			func = reloc->sym;
1064 			break;
1065 
1066 		case STT_SECTION:
1067 			func = find_func_by_offset(reloc->sym->sec, reloc_addend(reloc));
1068 			if (!func)
1069 				continue;
1070 			break;
1071 
1072 		default:
1073 			ERROR("unexpected relocation symbol type in %s: %d",
1074 			      rsec->name, reloc->sym->type);
1075 			return -1;
1076 		}
1077 
1078 		func->ignore = true;
1079 		if (func->cfunc)
1080 			func->cfunc->ignore = true;
1081 	}
1082 
1083 	return 0;
1084 }
1085 
1086 /*
1087  * This is a whitelist of functions that is allowed to be called with AC set.
1088  * The list is meant to be minimal and only contains compiler instrumentation
1089  * ABI and a few functions used to implement *_{to,from}_user() functions.
1090  *
1091  * These functions must not directly change AC, but may PUSHF/POPF.
1092  */
1093 static const char *uaccess_safe_builtin[] = {
1094 	/* KASAN */
1095 	"kasan_report",
1096 	"kasan_check_range",
1097 	/* KASAN out-of-line */
1098 	"__asan_loadN_noabort",
1099 	"__asan_load1_noabort",
1100 	"__asan_load2_noabort",
1101 	"__asan_load4_noabort",
1102 	"__asan_load8_noabort",
1103 	"__asan_load16_noabort",
1104 	"__asan_storeN_noabort",
1105 	"__asan_store1_noabort",
1106 	"__asan_store2_noabort",
1107 	"__asan_store4_noabort",
1108 	"__asan_store8_noabort",
1109 	"__asan_store16_noabort",
1110 	"__kasan_check_read",
1111 	"__kasan_check_write",
1112 	/* KASAN in-line */
1113 	"__asan_report_load_n_noabort",
1114 	"__asan_report_load1_noabort",
1115 	"__asan_report_load2_noabort",
1116 	"__asan_report_load4_noabort",
1117 	"__asan_report_load8_noabort",
1118 	"__asan_report_load16_noabort",
1119 	"__asan_report_store_n_noabort",
1120 	"__asan_report_store1_noabort",
1121 	"__asan_report_store2_noabort",
1122 	"__asan_report_store4_noabort",
1123 	"__asan_report_store8_noabort",
1124 	"__asan_report_store16_noabort",
1125 	/* KCSAN */
1126 	"__kcsan_check_access",
1127 	"__kcsan_mb",
1128 	"__kcsan_wmb",
1129 	"__kcsan_rmb",
1130 	"__kcsan_release",
1131 	"kcsan_found_watchpoint",
1132 	"kcsan_setup_watchpoint",
1133 	"kcsan_check_scoped_accesses",
1134 	"kcsan_disable_current",
1135 	"kcsan_enable_current_nowarn",
1136 	/* KCSAN/TSAN */
1137 	"__tsan_func_entry",
1138 	"__tsan_func_exit",
1139 	"__tsan_read_range",
1140 	"__tsan_write_range",
1141 	"__tsan_read1",
1142 	"__tsan_read2",
1143 	"__tsan_read4",
1144 	"__tsan_read8",
1145 	"__tsan_read16",
1146 	"__tsan_write1",
1147 	"__tsan_write2",
1148 	"__tsan_write4",
1149 	"__tsan_write8",
1150 	"__tsan_write16",
1151 	"__tsan_read_write1",
1152 	"__tsan_read_write2",
1153 	"__tsan_read_write4",
1154 	"__tsan_read_write8",
1155 	"__tsan_read_write16",
1156 	"__tsan_volatile_read1",
1157 	"__tsan_volatile_read2",
1158 	"__tsan_volatile_read4",
1159 	"__tsan_volatile_read8",
1160 	"__tsan_volatile_read16",
1161 	"__tsan_volatile_write1",
1162 	"__tsan_volatile_write2",
1163 	"__tsan_volatile_write4",
1164 	"__tsan_volatile_write8",
1165 	"__tsan_volatile_write16",
1166 	"__tsan_atomic8_load",
1167 	"__tsan_atomic16_load",
1168 	"__tsan_atomic32_load",
1169 	"__tsan_atomic64_load",
1170 	"__tsan_atomic8_store",
1171 	"__tsan_atomic16_store",
1172 	"__tsan_atomic32_store",
1173 	"__tsan_atomic64_store",
1174 	"__tsan_atomic8_exchange",
1175 	"__tsan_atomic16_exchange",
1176 	"__tsan_atomic32_exchange",
1177 	"__tsan_atomic64_exchange",
1178 	"__tsan_atomic8_fetch_add",
1179 	"__tsan_atomic16_fetch_add",
1180 	"__tsan_atomic32_fetch_add",
1181 	"__tsan_atomic64_fetch_add",
1182 	"__tsan_atomic8_fetch_sub",
1183 	"__tsan_atomic16_fetch_sub",
1184 	"__tsan_atomic32_fetch_sub",
1185 	"__tsan_atomic64_fetch_sub",
1186 	"__tsan_atomic8_fetch_and",
1187 	"__tsan_atomic16_fetch_and",
1188 	"__tsan_atomic32_fetch_and",
1189 	"__tsan_atomic64_fetch_and",
1190 	"__tsan_atomic8_fetch_or",
1191 	"__tsan_atomic16_fetch_or",
1192 	"__tsan_atomic32_fetch_or",
1193 	"__tsan_atomic64_fetch_or",
1194 	"__tsan_atomic8_fetch_xor",
1195 	"__tsan_atomic16_fetch_xor",
1196 	"__tsan_atomic32_fetch_xor",
1197 	"__tsan_atomic64_fetch_xor",
1198 	"__tsan_atomic8_fetch_nand",
1199 	"__tsan_atomic16_fetch_nand",
1200 	"__tsan_atomic32_fetch_nand",
1201 	"__tsan_atomic64_fetch_nand",
1202 	"__tsan_atomic8_compare_exchange_strong",
1203 	"__tsan_atomic16_compare_exchange_strong",
1204 	"__tsan_atomic32_compare_exchange_strong",
1205 	"__tsan_atomic64_compare_exchange_strong",
1206 	"__tsan_atomic8_compare_exchange_weak",
1207 	"__tsan_atomic16_compare_exchange_weak",
1208 	"__tsan_atomic32_compare_exchange_weak",
1209 	"__tsan_atomic64_compare_exchange_weak",
1210 	"__tsan_atomic8_compare_exchange_val",
1211 	"__tsan_atomic16_compare_exchange_val",
1212 	"__tsan_atomic32_compare_exchange_val",
1213 	"__tsan_atomic64_compare_exchange_val",
1214 	"__tsan_atomic_thread_fence",
1215 	"__tsan_atomic_signal_fence",
1216 	"__tsan_unaligned_read16",
1217 	"__tsan_unaligned_write16",
1218 	/* KCOV */
1219 	"write_comp_data",
1220 	"check_kcov_mode",
1221 	"__sanitizer_cov_trace_pc",
1222 	"__sanitizer_cov_trace_const_cmp1",
1223 	"__sanitizer_cov_trace_const_cmp2",
1224 	"__sanitizer_cov_trace_const_cmp4",
1225 	"__sanitizer_cov_trace_const_cmp8",
1226 	"__sanitizer_cov_trace_cmp1",
1227 	"__sanitizer_cov_trace_cmp2",
1228 	"__sanitizer_cov_trace_cmp4",
1229 	"__sanitizer_cov_trace_cmp8",
1230 	"__sanitizer_cov_trace_switch",
1231 	/* KMSAN */
1232 	"kmsan_copy_to_user",
1233 	"kmsan_disable_current",
1234 	"kmsan_enable_current",
1235 	"kmsan_report",
1236 	"kmsan_unpoison_entry_regs",
1237 	"kmsan_unpoison_memory",
1238 	"__msan_chain_origin",
1239 	"__msan_get_context_state",
1240 	"__msan_instrument_asm_store",
1241 	"__msan_metadata_ptr_for_load_1",
1242 	"__msan_metadata_ptr_for_load_2",
1243 	"__msan_metadata_ptr_for_load_4",
1244 	"__msan_metadata_ptr_for_load_8",
1245 	"__msan_metadata_ptr_for_load_n",
1246 	"__msan_metadata_ptr_for_store_1",
1247 	"__msan_metadata_ptr_for_store_2",
1248 	"__msan_metadata_ptr_for_store_4",
1249 	"__msan_metadata_ptr_for_store_8",
1250 	"__msan_metadata_ptr_for_store_n",
1251 	"__msan_poison_alloca",
1252 	"__msan_warning",
1253 	/* UBSAN */
1254 	"ubsan_type_mismatch_common",
1255 	"__ubsan_handle_type_mismatch",
1256 	"__ubsan_handle_type_mismatch_v1",
1257 	"__ubsan_handle_shift_out_of_bounds",
1258 	"__ubsan_handle_load_invalid_value",
1259 	/* KSTACK_ERASE */
1260 	"__sanitizer_cov_stack_depth",
1261 	/* TRACE_BRANCH_PROFILING */
1262 	"ftrace_likely_update",
1263 	/* STACKPROTECTOR */
1264 	"__stack_chk_fail",
1265 	/* misc */
1266 	"csum_partial_copy_generic",
1267 	"copy_mc_fragile",
1268 	"copy_mc_fragile_handle_tail",
1269 	"copy_mc_enhanced_fast_string",
1270 	"rep_stos_alternative",
1271 	"rep_movs_alternative",
1272 	"copy_to_nontemporal",
1273 	NULL
1274 };
1275 
1276 static void add_uaccess_safe(struct objtool_file *file)
1277 {
1278 	struct symbol *func;
1279 	const char **name;
1280 
1281 	if (!opts.uaccess)
1282 		return;
1283 
1284 	for (name = uaccess_safe_builtin; *name; name++) {
1285 		func = find_symbol_by_name(file->elf, *name);
1286 		if (!func)
1287 			continue;
1288 
1289 		func->uaccess_safe = true;
1290 	}
1291 }
1292 
1293 /*
1294  * Symbols that replace INSN_CALL_DYNAMIC, every (tail) call to such a symbol
1295  * will be added to the .retpoline_sites section.
1296  */
1297 __weak bool arch_is_retpoline(struct symbol *sym)
1298 {
1299 	return false;
1300 }
1301 
1302 /*
1303  * Symbols that replace INSN_RETURN, every (tail) call to such a symbol
1304  * will be added to the .return_sites section.
1305  */
1306 __weak bool arch_is_rethunk(struct symbol *sym)
1307 {
1308 	return false;
1309 }
1310 
1311 /*
1312  * Symbols that are embedded inside other instructions, because sometimes crazy
1313  * code exists. These are mostly ignored for validation purposes.
1314  */
1315 __weak bool arch_is_embedded_insn(struct symbol *sym)
1316 {
1317 	return false;
1318 }
1319 
1320 struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
1321 {
1322 	struct reloc *reloc;
1323 
1324 	if (!file || insn->no_reloc || insn->fake)
1325 		return NULL;
1326 
1327 	reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1328 					 insn->offset, insn->len);
1329 	if (!reloc) {
1330 		insn->no_reloc = 1;
1331 		return NULL;
1332 	}
1333 
1334 	return reloc;
1335 }
1336 
1337 static void remove_insn_ops(struct instruction *insn)
1338 {
1339 	struct stack_op *op, *next;
1340 
1341 	for (op = insn->stack_ops; op; op = next) {
1342 		next = op->next;
1343 		free(op);
1344 	}
1345 	insn->stack_ops = NULL;
1346 }
1347 
1348 static int annotate_call_site(struct objtool_file *file,
1349 			       struct instruction *insn, bool sibling)
1350 {
1351 	struct reloc *reloc = insn_reloc(file, insn);
1352 	struct symbol *sym = insn_call_dest(insn);
1353 
1354 	if (!sym)
1355 		sym = reloc->sym;
1356 
1357 	if (sym->static_call_tramp) {
1358 		list_add_tail(&insn->call_node, &file->static_call_list);
1359 		return 0;
1360 	}
1361 
1362 	if (sym->retpoline_thunk) {
1363 		list_add_tail(&insn->call_node, &file->retpoline_call_list);
1364 		return 0;
1365 	}
1366 
1367 	/*
1368 	 * Many compilers cannot disable KCOV or sanitizer calls with a function
1369 	 * attribute so they need a little help, NOP out any such calls from
1370 	 * noinstr text.
1371 	 */
1372 	if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) {
1373 		if (reloc)
1374 			set_reloc_type(file->elf, reloc, R_NONE);
1375 
1376 		if (elf_write_insn(file->elf, insn->sec,
1377 				   insn->offset, insn->len,
1378 				   sibling ? arch_ret_insn(insn->len)
1379 					   : arch_nop_insn(insn->len))) {
1380 			return -1;
1381 		}
1382 
1383 		insn->type = sibling ? INSN_RETURN : INSN_NOP;
1384 
1385 		if (sibling) {
1386 			/*
1387 			 * We've replaced the tail-call JMP insn by two new
1388 			 * insn: RET; INT3, except we only have a single struct
1389 			 * insn here. Mark it retpoline_safe to avoid the SLS
1390 			 * warning, instead of adding another insn.
1391 			 */
1392 			insn->retpoline_safe = true;
1393 		}
1394 
1395 		return 0;
1396 	}
1397 
1398 	if (opts.mcount && sym->fentry) {
1399 		if (sibling)
1400 			WARN_INSN(insn, "tail call to __fentry__ !?!?");
1401 		if (opts.mnop) {
1402 			if (reloc)
1403 				set_reloc_type(file->elf, reloc, R_NONE);
1404 
1405 			if (elf_write_insn(file->elf, insn->sec,
1406 					   insn->offset, insn->len,
1407 					   arch_nop_insn(insn->len))) {
1408 				return -1;
1409 			}
1410 
1411 			insn->type = INSN_NOP;
1412 		}
1413 
1414 		list_add_tail(&insn->call_node, &file->mcount_loc_list);
1415 		return 0;
1416 	}
1417 
1418 	if (insn->type == INSN_CALL && !insn->sec->init &&
1419 	    !insn->_call_dest->embedded_insn)
1420 		list_add_tail(&insn->call_node, &file->call_list);
1421 
1422 	if (!sibling && dead_end_function(file, sym))
1423 		insn->dead_end = true;
1424 
1425 	return 0;
1426 }
1427 
1428 static int add_call_dest(struct objtool_file *file, struct instruction *insn,
1429 			  struct symbol *dest, bool sibling)
1430 {
1431 	insn->_call_dest = dest;
1432 	if (!dest)
1433 		return 0;
1434 
1435 	/*
1436 	 * Whatever stack impact regular CALLs have, should be undone
1437 	 * by the RETURN of the called function.
1438 	 *
1439 	 * Annotated intra-function calls retain the stack_ops but
1440 	 * are converted to JUMP, see read_intra_function_calls().
1441 	 */
1442 	remove_insn_ops(insn);
1443 
1444 	return annotate_call_site(file, insn, sibling);
1445 }
1446 
1447 static int add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1448 {
1449 	/*
1450 	 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1451 	 * so convert them accordingly.
1452 	 */
1453 	switch (insn->type) {
1454 	case INSN_CALL:
1455 		insn->type = INSN_CALL_DYNAMIC;
1456 		break;
1457 	case INSN_JUMP_UNCONDITIONAL:
1458 		insn->type = INSN_JUMP_DYNAMIC;
1459 		break;
1460 	case INSN_JUMP_CONDITIONAL:
1461 		insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1462 		break;
1463 	default:
1464 		return 0;
1465 	}
1466 
1467 	insn->retpoline_safe = true;
1468 
1469 	/*
1470 	 * Whatever stack impact regular CALLs have, should be undone
1471 	 * by the RETURN of the called function.
1472 	 *
1473 	 * Annotated intra-function calls retain the stack_ops but
1474 	 * are converted to JUMP, see read_intra_function_calls().
1475 	 */
1476 	remove_insn_ops(insn);
1477 
1478 	return annotate_call_site(file, insn, false);
1479 }
1480 
1481 static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add)
1482 {
1483 	/*
1484 	 * Return thunk tail calls are really just returns in disguise,
1485 	 * so convert them accordingly.
1486 	 */
1487 	insn->type = INSN_RETURN;
1488 	insn->retpoline_safe = true;
1489 
1490 	if (add)
1491 		list_add_tail(&insn->call_node, &file->return_thunk_list);
1492 }
1493 
1494 static bool is_first_func_insn(struct objtool_file *file,
1495 			       struct instruction *insn)
1496 {
1497 	struct symbol *func = insn_func(insn);
1498 
1499 	if (!func)
1500 		return false;
1501 
1502 	if (insn->offset == func->offset)
1503 		return true;
1504 
1505 	/* Allow direct CALL/JMP past ENDBR */
1506 	if (opts.ibt) {
1507 		struct instruction *prev = prev_insn_same_sym(file, insn);
1508 
1509 		if (prev && prev->type == INSN_ENDBR &&
1510 		    insn->offset == func->offset + prev->len)
1511 			return true;
1512 	}
1513 
1514 	return false;
1515 }
1516 
1517 /*
1518  * Find the destination instructions for all jumps.
1519  */
1520 static int add_jump_destinations(struct objtool_file *file)
1521 {
1522 	struct instruction *insn;
1523 	struct reloc *reloc;
1524 
1525 	for_each_insn(file, insn) {
1526 		struct symbol *func = insn_func(insn);
1527 		struct instruction *dest_insn;
1528 		struct section *dest_sec;
1529 		struct symbol *dest_sym;
1530 		unsigned long dest_off;
1531 
1532 		if (!is_static_jump(insn))
1533 			continue;
1534 
1535 		if (insn->jump_dest) {
1536 			/*
1537 			 * handle_group_alt() may have previously set
1538 			 * 'jump_dest' for some alternatives.
1539 			 */
1540 			continue;
1541 		}
1542 
1543 		reloc = insn_reloc(file, insn);
1544 		if (!reloc) {
1545 			dest_sec = insn->sec;
1546 			dest_off = arch_jump_destination(insn);
1547 			dest_sym = dest_sec->sym;
1548 		} else {
1549 			dest_sym = reloc->sym;
1550 			if (is_undef_sym(dest_sym)) {
1551 				if (dest_sym->retpoline_thunk) {
1552 					if (add_retpoline_call(file, insn))
1553 						return -1;
1554 					continue;
1555 				}
1556 
1557 				if (dest_sym->return_thunk) {
1558 					add_return_call(file, insn, true);
1559 					continue;
1560 				}
1561 
1562 				/* External symbol */
1563 				if (func) {
1564 					/* External sibling call */
1565 					if (add_call_dest(file, insn, dest_sym, true))
1566 						return -1;
1567 					continue;
1568 				}
1569 
1570 				/* Non-func asm code jumping to external symbol */
1571 				continue;
1572 			}
1573 
1574 			dest_sec = dest_sym->sec;
1575 			dest_off = dest_sym->offset + arch_insn_adjusted_addend(insn, reloc);
1576 		}
1577 
1578 		dest_insn = find_insn(file, dest_sec, dest_off);
1579 		if (!dest_insn) {
1580 			struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off);
1581 
1582 			/*
1583 			 * retbleed_untrain_ret() jumps to
1584 			 * __x86_return_thunk(), but objtool can't find
1585 			 * the thunk's starting RET instruction,
1586 			 * because the RET is also in the middle of
1587 			 * another instruction.  Objtool only knows
1588 			 * about the outer instruction.
1589 			 */
1590 			if (sym && sym->embedded_insn) {
1591 				add_return_call(file, insn, false);
1592 				continue;
1593 			}
1594 
1595 			/*
1596 			 * GCOV/KCOV dead code can jump to the end of
1597 			 * the function/section.
1598 			 */
1599 			if (file->ignore_unreachables && func &&
1600 			    dest_sec == insn->sec &&
1601 			    dest_off == func->offset + func->len)
1602 				continue;
1603 
1604 			ERROR_INSN(insn, "can't find jump dest instruction at %s",
1605 				   offstr(dest_sec, dest_off));
1606 			return -1;
1607 		}
1608 
1609 		if (!dest_sym || is_sec_sym(dest_sym)) {
1610 			dest_sym = insn_sym(dest_insn);
1611 			if (!dest_sym)
1612 				goto set_jump_dest;
1613 		}
1614 
1615 		if (dest_sym->retpoline_thunk && dest_insn->offset == dest_sym->offset) {
1616 			if (add_retpoline_call(file, insn))
1617 				return -1;
1618 			continue;
1619 		}
1620 
1621 		if (dest_sym->return_thunk && dest_insn->offset == dest_sym->offset) {
1622 			add_return_call(file, insn, true);
1623 			continue;
1624 		}
1625 
1626 		if (!insn_sym(insn) || insn_sym(insn)->pfunc == dest_sym->pfunc)
1627 			goto set_jump_dest;
1628 
1629 		/*
1630 		 * Internal cross-function jump.
1631 		 */
1632 
1633 		if (is_first_func_insn(file, dest_insn)) {
1634 			/* Internal sibling call */
1635 			if (add_call_dest(file, insn, dest_sym, true))
1636 				return -1;
1637 			continue;
1638 		}
1639 
1640 set_jump_dest:
1641 		insn->jump_dest = dest_insn;
1642 	}
1643 
1644 	return 0;
1645 }
1646 
1647 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1648 {
1649 	struct symbol *call_dest;
1650 
1651 	call_dest = find_func_by_offset(sec, offset);
1652 	if (!call_dest)
1653 		call_dest = find_symbol_by_offset(sec, offset);
1654 
1655 	return call_dest;
1656 }
1657 
1658 /*
1659  * Find the destination instructions for all calls.
1660  */
1661 static int add_call_destinations(struct objtool_file *file)
1662 {
1663 	struct instruction *insn;
1664 	unsigned long dest_off;
1665 	struct symbol *dest;
1666 	struct reloc *reloc;
1667 
1668 	for_each_insn(file, insn) {
1669 		struct symbol *func = insn_func(insn);
1670 		if (insn->type != INSN_CALL)
1671 			continue;
1672 
1673 		reloc = insn_reloc(file, insn);
1674 		if (!reloc) {
1675 			dest_off = arch_jump_destination(insn);
1676 			dest = find_call_destination(insn->sec, dest_off);
1677 
1678 			if (add_call_dest(file, insn, dest, false))
1679 				return -1;
1680 
1681 			if (func && func->ignore)
1682 				continue;
1683 
1684 			if (!insn_call_dest(insn)) {
1685 				ERROR_INSN(insn, "unannotated intra-function call");
1686 				return -1;
1687 			}
1688 
1689 			if (func && !is_func_sym(insn_call_dest(insn))) {
1690 				ERROR_INSN(insn, "unsupported call to non-function");
1691 				return -1;
1692 			}
1693 
1694 		} else if (is_sec_sym(reloc->sym)) {
1695 			dest_off = arch_insn_adjusted_addend(insn, reloc);
1696 			dest = find_call_destination(reloc->sym->sec, dest_off);
1697 			if (!dest) {
1698 				ERROR_INSN(insn, "can't find call dest symbol at %s+0x%lx",
1699 					   reloc->sym->sec->name, dest_off);
1700 				return -1;
1701 			}
1702 
1703 			if (add_call_dest(file, insn, dest, false))
1704 				return -1;
1705 
1706 		} else if (reloc->sym->retpoline_thunk) {
1707 			if (add_retpoline_call(file, insn))
1708 				return -1;
1709 
1710 		} else {
1711 			if (add_call_dest(file, insn, reloc->sym, false))
1712 				return -1;
1713 		}
1714 	}
1715 
1716 	return 0;
1717 }
1718 
1719 /*
1720  * The .alternatives section requires some extra special care over and above
1721  * other special sections because alternatives are patched in place.
1722  */
1723 static int handle_group_alt(struct objtool_file *file,
1724 			    struct special_alt *special_alt,
1725 			    struct instruction *orig_insn,
1726 			    struct instruction **new_insn)
1727 {
1728 	struct instruction *last_new_insn = NULL, *insn, *nop = NULL;
1729 	struct alt_group *orig_alt_group, *new_alt_group;
1730 	unsigned long dest_off;
1731 
1732 	orig_alt_group = orig_insn->alt_group;
1733 	if (!orig_alt_group) {
1734 		struct instruction *last_orig_insn = NULL;
1735 
1736 		orig_alt_group = calloc(1, sizeof(*orig_alt_group));
1737 		if (!orig_alt_group) {
1738 			ERROR_GLIBC("calloc");
1739 			return -1;
1740 		}
1741 		orig_alt_group->cfi = calloc(special_alt->orig_len,
1742 					     sizeof(struct cfi_state *));
1743 		if (!orig_alt_group->cfi) {
1744 			ERROR_GLIBC("calloc");
1745 			return -1;
1746 		}
1747 
1748 		insn = orig_insn;
1749 		sec_for_each_insn_from(file, insn) {
1750 			if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1751 				break;
1752 
1753 			insn->alt_group = orig_alt_group;
1754 			last_orig_insn = insn;
1755 		}
1756 		orig_alt_group->orig_group = NULL;
1757 		orig_alt_group->first_insn = orig_insn;
1758 		orig_alt_group->last_insn = last_orig_insn;
1759 		orig_alt_group->nop = NULL;
1760 		orig_alt_group->ignore = orig_insn->ignore_alts;
1761 		orig_alt_group->feature = 0;
1762 	} else {
1763 		if (orig_alt_group->last_insn->offset + orig_alt_group->last_insn->len -
1764 		    orig_alt_group->first_insn->offset != special_alt->orig_len) {
1765 			ERROR_INSN(orig_insn, "weirdly overlapping alternative! %ld != %d",
1766 				   orig_alt_group->last_insn->offset +
1767 				   orig_alt_group->last_insn->len -
1768 				   orig_alt_group->first_insn->offset,
1769 				   special_alt->orig_len);
1770 			return -1;
1771 		}
1772 	}
1773 
1774 	new_alt_group = calloc(1, sizeof(*new_alt_group));
1775 	if (!new_alt_group) {
1776 		ERROR_GLIBC("calloc");
1777 		return -1;
1778 	}
1779 
1780 	if (special_alt->new_len < special_alt->orig_len) {
1781 		/*
1782 		 * Insert a fake nop at the end to make the replacement
1783 		 * alt_group the same size as the original.  This is needed to
1784 		 * allow propagate_alt_cfi() to do its magic.  When the last
1785 		 * instruction affects the stack, the instruction after it (the
1786 		 * nop) will propagate the new state to the shared CFI array.
1787 		 */
1788 		nop = calloc(1, sizeof(*nop));
1789 		if (!nop) {
1790 			ERROR_GLIBC("calloc");
1791 			return -1;
1792 		}
1793 		memset(nop, 0, sizeof(*nop));
1794 
1795 		nop->sec = special_alt->new_sec;
1796 		nop->offset = special_alt->new_off + special_alt->new_len;
1797 		nop->len = special_alt->orig_len - special_alt->new_len;
1798 		nop->type = INSN_NOP;
1799 		nop->alt_group = new_alt_group;
1800 		nop->fake = 1;
1801 	}
1802 
1803 	if (!special_alt->new_len) {
1804 		*new_insn = nop;
1805 		goto end;
1806 	}
1807 
1808 	insn = *new_insn;
1809 	sec_for_each_insn_from(file, insn) {
1810 		struct reloc *alt_reloc;
1811 
1812 		if (insn->offset >= special_alt->new_off + special_alt->new_len)
1813 			break;
1814 
1815 		last_new_insn = insn;
1816 
1817 		insn->alt_group = new_alt_group;
1818 
1819 		/*
1820 		 * Since alternative replacement code is copy/pasted by the
1821 		 * kernel after applying relocations, generally such code can't
1822 		 * have relative-address relocation references to outside the
1823 		 * .altinstr_replacement section, unless the arch's
1824 		 * alternatives code can adjust the relative offsets
1825 		 * accordingly.
1826 		 */
1827 		alt_reloc = insn_reloc(file, insn);
1828 		if (alt_reloc && arch_pc_relative_reloc(alt_reloc) &&
1829 		    !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1830 
1831 			ERROR_INSN(insn, "unsupported relocation in alternatives section");
1832 			return -1;
1833 		}
1834 
1835 		if (!is_static_jump(insn))
1836 			continue;
1837 
1838 		if (!insn->immediate)
1839 			continue;
1840 
1841 		dest_off = arch_jump_destination(insn);
1842 		if (dest_off == special_alt->new_off + special_alt->new_len) {
1843 			insn->jump_dest = next_insn_same_sec(file, orig_alt_group->last_insn);
1844 			if (!insn->jump_dest) {
1845 				ERROR_INSN(insn, "can't find alternative jump destination");
1846 				return -1;
1847 			}
1848 		}
1849 	}
1850 
1851 	if (!last_new_insn) {
1852 		ERROR_FUNC(special_alt->new_sec, special_alt->new_off,
1853 			   "can't find last new alternative instruction");
1854 		return -1;
1855 	}
1856 
1857 end:
1858 	new_alt_group->orig_group = orig_alt_group;
1859 	new_alt_group->first_insn = *new_insn;
1860 	new_alt_group->last_insn = last_new_insn;
1861 	new_alt_group->nop = nop;
1862 	new_alt_group->ignore = (*new_insn)->ignore_alts;
1863 	new_alt_group->cfi = orig_alt_group->cfi;
1864 	new_alt_group->feature = special_alt->feature;
1865 	return 0;
1866 }
1867 
1868 /*
1869  * A jump table entry can either convert a nop to a jump or a jump to a nop.
1870  * If the original instruction is a jump, make the alt entry an effective nop
1871  * by just skipping the original instruction.
1872  */
1873 static int handle_jump_alt(struct objtool_file *file,
1874 			   struct special_alt *special_alt,
1875 			   struct instruction *orig_insn,
1876 			   struct instruction **new_insn)
1877 {
1878 	if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1879 	    orig_insn->type != INSN_NOP) {
1880 
1881 		ERROR_INSN(orig_insn, "unsupported instruction at jump label");
1882 		return -1;
1883 	}
1884 
1885 	if (opts.hack_jump_label && special_alt->key_addend & 2) {
1886 		struct reloc *reloc = insn_reloc(file, orig_insn);
1887 
1888 		if (reloc)
1889 			set_reloc_type(file->elf, reloc, R_NONE);
1890 
1891 		if (elf_write_insn(file->elf, orig_insn->sec,
1892 				   orig_insn->offset, orig_insn->len,
1893 				   arch_nop_insn(orig_insn->len))) {
1894 			return -1;
1895 		}
1896 
1897 		orig_insn->type = INSN_NOP;
1898 	}
1899 
1900 	if (orig_insn->type == INSN_NOP) {
1901 		if (orig_insn->len == 2)
1902 			file->jl_nop_short++;
1903 		else
1904 			file->jl_nop_long++;
1905 
1906 		return 0;
1907 	}
1908 
1909 	if (orig_insn->len == 2)
1910 		file->jl_short++;
1911 	else
1912 		file->jl_long++;
1913 
1914 	*new_insn = next_insn_same_sec(file, orig_insn);
1915 	return 0;
1916 }
1917 
1918 /*
1919  * Read all the special sections which have alternate instructions which can be
1920  * patched in or redirected to at runtime.  Each instruction having alternate
1921  * instruction(s) has them added to its insn->alts list, which will be
1922  * traversed in validate_branch().
1923  */
1924 static int add_special_section_alts(struct objtool_file *file)
1925 {
1926 	struct list_head special_alts;
1927 	struct instruction *orig_insn, *new_insn;
1928 	struct special_alt *special_alt, *tmp;
1929 	enum alternative_type alt_type;
1930 	struct alternative *alt;
1931 	struct alternative *a;
1932 
1933 	if (special_get_alts(file->elf, &special_alts))
1934 		return -1;
1935 
1936 	list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1937 
1938 		orig_insn = find_insn(file, special_alt->orig_sec,
1939 				      special_alt->orig_off);
1940 		if (!orig_insn) {
1941 			ERROR_FUNC(special_alt->orig_sec, special_alt->orig_off,
1942 				   "special: can't find orig instruction");
1943 			return -1;
1944 		}
1945 
1946 		new_insn = NULL;
1947 		if (!special_alt->group || special_alt->new_len) {
1948 			new_insn = find_insn(file, special_alt->new_sec,
1949 					     special_alt->new_off);
1950 			if (!new_insn) {
1951 				ERROR_FUNC(special_alt->new_sec, special_alt->new_off,
1952 					   "special: can't find new instruction");
1953 				return -1;
1954 			}
1955 		}
1956 
1957 		if (special_alt->group) {
1958 			if (!special_alt->orig_len) {
1959 				ERROR_INSN(orig_insn, "empty alternative entry");
1960 				continue;
1961 			}
1962 
1963 			if (handle_group_alt(file, special_alt, orig_insn, &new_insn))
1964 				return -1;
1965 
1966 			alt_type = ALT_TYPE_INSTRUCTIONS;
1967 
1968 		} else if (special_alt->jump_or_nop) {
1969 			if (handle_jump_alt(file, special_alt, orig_insn, &new_insn))
1970 				return -1;
1971 
1972 			alt_type = ALT_TYPE_JUMP_TABLE;
1973 		} else {
1974 			alt_type = ALT_TYPE_EX_TABLE;
1975 		}
1976 
1977 		alt = calloc(1, sizeof(*alt));
1978 		if (!alt) {
1979 			ERROR_GLIBC("calloc");
1980 			return -1;
1981 		}
1982 
1983 		alt->insn = new_insn;
1984 		alt->type = alt_type;
1985 		alt->next = NULL;
1986 
1987 		/*
1988 		 * Store alternatives in the same order they have been
1989 		 * defined.
1990 		 */
1991 		if (!orig_insn->alts) {
1992 			orig_insn->alts = alt;
1993 		} else {
1994 			for (a = orig_insn->alts; a->next; a = a->next)
1995 				;
1996 			a->next = alt;
1997 		}
1998 
1999 		list_del(&special_alt->list);
2000 		free(special_alt);
2001 	}
2002 
2003 	if (opts.stats) {
2004 		printf("jl\\\tNOP\tJMP\n");
2005 		printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
2006 		printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
2007 	}
2008 
2009 	return 0;
2010 }
2011 
2012 __weak unsigned long arch_jump_table_sym_offset(struct reloc *reloc, struct reloc *table)
2013 {
2014 	return reloc->sym->offset + reloc_addend(reloc);
2015 }
2016 
2017 static int add_jump_table(struct objtool_file *file, struct instruction *insn)
2018 {
2019 	unsigned long table_size = insn_jump_table_size(insn);
2020 	struct symbol *pfunc = insn_func(insn)->pfunc;
2021 	struct reloc *table = insn_jump_table(insn);
2022 	struct instruction *dest_insn;
2023 	unsigned int prev_offset = 0;
2024 	struct reloc *reloc = table;
2025 	struct alternative *alt;
2026 	unsigned long sym_offset;
2027 
2028 	/*
2029 	 * Each @reloc is a switch table relocation which points to the target
2030 	 * instruction.
2031 	 */
2032 	for_each_reloc_from(table->sec, reloc) {
2033 
2034 		/* Check for the end of the table: */
2035 		if (table_size && reloc_offset(reloc) - reloc_offset(table) >= table_size)
2036 			break;
2037 		if (reloc != table && is_jump_table(reloc))
2038 			break;
2039 
2040 		/* Make sure the table entries are consecutive: */
2041 		if (prev_offset && reloc_offset(reloc) != prev_offset + arch_reloc_size(reloc))
2042 			break;
2043 
2044 		sym_offset = arch_jump_table_sym_offset(reloc, table);
2045 
2046 		/* Detect function pointers from contiguous objects: */
2047 		if (reloc->sym->sec == pfunc->sec && sym_offset == pfunc->offset)
2048 			break;
2049 
2050 		/*
2051 		 * Clang sometimes leaves dangling unused jump table entries
2052 		 * which point to the end of the function.  Ignore them.
2053 		 */
2054 		if (reloc->sym->sec == pfunc->sec &&
2055 		    sym_offset == pfunc->offset + pfunc->len)
2056 			goto next;
2057 
2058 		dest_insn = find_insn(file, reloc->sym->sec, sym_offset);
2059 		if (!dest_insn)
2060 			break;
2061 
2062 		/* Make sure the destination is in the same function: */
2063 		if (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc)
2064 			break;
2065 
2066 		alt = calloc(1, sizeof(*alt));
2067 		if (!alt) {
2068 			ERROR_GLIBC("calloc");
2069 			return -1;
2070 		}
2071 
2072 		alt->insn = dest_insn;
2073 		alt->next = insn->alts;
2074 		insn->alts = alt;
2075 next:
2076 		prev_offset = reloc_offset(reloc);
2077 	}
2078 
2079 	if (!prev_offset) {
2080 		ERROR_INSN(insn, "can't find switch jump table");
2081 		return -1;
2082 	}
2083 
2084 	return 0;
2085 }
2086 
2087 /*
2088  * find_jump_table() - Given a dynamic jump, find the switch jump table
2089  * associated with it.
2090  */
2091 static void find_jump_table(struct objtool_file *file, struct symbol *func,
2092 			    struct instruction *insn)
2093 {
2094 	struct reloc *table_reloc;
2095 	struct instruction *dest_insn, *orig_insn = insn;
2096 	unsigned long table_size;
2097 	unsigned long sym_offset;
2098 
2099 	/*
2100 	 * Backward search using the @first_jump_src links, these help avoid
2101 	 * much of the 'in between' code. Which avoids us getting confused by
2102 	 * it.
2103 	 */
2104 	for (;
2105 	     insn && insn_func(insn) && insn_func(insn)->pfunc == func;
2106 	     insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
2107 
2108 		if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
2109 			break;
2110 
2111 		/* allow small jumps within the range */
2112 		if (insn->type == INSN_JUMP_UNCONDITIONAL &&
2113 		    insn->jump_dest &&
2114 		    (insn->jump_dest->offset <= insn->offset ||
2115 		     insn->jump_dest->offset > orig_insn->offset))
2116 			break;
2117 
2118 		table_reloc = arch_find_switch_table(file, insn, &table_size);
2119 		if (!table_reloc)
2120 			continue;
2121 
2122 		sym_offset = table_reloc->sym->offset + reloc_addend(table_reloc);
2123 
2124 		dest_insn = find_insn(file, table_reloc->sym->sec, sym_offset);
2125 		if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func)
2126 			continue;
2127 
2128 		set_jump_table(table_reloc);
2129 		orig_insn->_jump_table = table_reloc;
2130 		orig_insn->_jump_table_size = table_size;
2131 
2132 		break;
2133 	}
2134 }
2135 
2136 /*
2137  * First pass: Mark the head of each jump table so that in the next pass,
2138  * we know when a given jump table ends and the next one starts.
2139  */
2140 static void mark_func_jump_tables(struct objtool_file *file,
2141 				    struct symbol *func)
2142 {
2143 	struct instruction *insn, *last = NULL;
2144 
2145 	func_for_each_insn(file, func, insn) {
2146 		if (!last)
2147 			last = insn;
2148 
2149 		/*
2150 		 * Store back-pointers for forward jumps such
2151 		 * that find_jump_table() can back-track using those and
2152 		 * avoid some potentially confusing code.
2153 		 */
2154 		if (insn->jump_dest &&
2155 		    insn->jump_dest->offset > insn->offset &&
2156 		    !insn->jump_dest->first_jump_src) {
2157 
2158 			insn->jump_dest->first_jump_src = insn;
2159 			last = insn->jump_dest;
2160 		}
2161 
2162 		if (insn->type != INSN_JUMP_DYNAMIC)
2163 			continue;
2164 
2165 		find_jump_table(file, func, insn);
2166 	}
2167 }
2168 
2169 static int add_func_jump_tables(struct objtool_file *file,
2170 				  struct symbol *func)
2171 {
2172 	struct instruction *insn;
2173 
2174 	func_for_each_insn(file, func, insn) {
2175 		if (!insn_jump_table(insn))
2176 			continue;
2177 
2178 		if (add_jump_table(file, insn))
2179 			return -1;
2180 	}
2181 
2182 	return 0;
2183 }
2184 
2185 /*
2186  * For some switch statements, gcc generates a jump table in the .rodata
2187  * section which contains a list of addresses within the function to jump to.
2188  * This finds these jump tables and adds them to the insn->alts lists.
2189  */
2190 static int add_jump_table_alts(struct objtool_file *file)
2191 {
2192 	struct symbol *func;
2193 
2194 	if (!file->rodata)
2195 		return 0;
2196 
2197 	for_each_sym(file->elf, func) {
2198 		if (!is_func_sym(func) || is_alias_sym(func))
2199 			continue;
2200 
2201 		mark_func_jump_tables(file, func);
2202 		if (add_func_jump_tables(file, func))
2203 			return -1;
2204 	}
2205 
2206 	return 0;
2207 }
2208 
2209 static void set_func_state(struct cfi_state *state)
2210 {
2211 	state->cfa = initial_func_cfi.cfa;
2212 	memcpy(&state->regs, &initial_func_cfi.regs,
2213 	       CFI_NUM_REGS * sizeof(struct cfi_reg));
2214 	state->stack_size = initial_func_cfi.cfa.offset;
2215 	state->type = UNWIND_HINT_TYPE_CALL;
2216 }
2217 
2218 static int read_unwind_hints(struct objtool_file *file)
2219 {
2220 	struct cfi_state cfi = init_cfi;
2221 	struct section *sec;
2222 	struct unwind_hint *hint;
2223 	struct instruction *insn;
2224 	struct reloc *reloc;
2225 	unsigned long offset;
2226 	int i;
2227 
2228 	sec = find_section_by_name(file->elf, ".discard.unwind_hints");
2229 	if (!sec)
2230 		return 0;
2231 
2232 	if (!sec->rsec) {
2233 		ERROR("missing .rela.discard.unwind_hints section");
2234 		return -1;
2235 	}
2236 
2237 	if (sec_size(sec) % sizeof(struct unwind_hint)) {
2238 		ERROR("struct unwind_hint size mismatch");
2239 		return -1;
2240 	}
2241 
2242 	file->hints = true;
2243 
2244 	for (i = 0; i < sec_size(sec) / sizeof(struct unwind_hint); i++) {
2245 		hint = (struct unwind_hint *)sec->data->d_buf + i;
2246 
2247 		reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
2248 		if (!reloc) {
2249 			ERROR("can't find reloc for unwind_hints[%d]", i);
2250 			return -1;
2251 		}
2252 
2253 		offset = reloc->sym->offset + reloc_addend(reloc);
2254 
2255 		insn = find_insn(file, reloc->sym->sec, offset);
2256 		if (!insn) {
2257 			ERROR("can't find insn for unwind_hints[%d]", i);
2258 			return -1;
2259 		}
2260 
2261 		insn->hint = true;
2262 
2263 		if (hint->type == UNWIND_HINT_TYPE_UNDEFINED) {
2264 			insn->cfi = &force_undefined_cfi;
2265 			continue;
2266 		}
2267 
2268 		if (hint->type == UNWIND_HINT_TYPE_SAVE) {
2269 			insn->hint = false;
2270 			insn->save = true;
2271 			continue;
2272 		}
2273 
2274 		if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
2275 			insn->restore = true;
2276 			continue;
2277 		}
2278 
2279 		if (hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) {
2280 			struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset);
2281 
2282 			if (sym && is_global_sym(sym)) {
2283 				if (opts.ibt && insn->type != INSN_ENDBR && !insn->noendbr) {
2284 					ERROR_INSN(insn, "UNWIND_HINT_IRET_REGS without ENDBR");
2285 					return -1;
2286 				}
2287 			}
2288 		}
2289 
2290 		if (hint->type == UNWIND_HINT_TYPE_FUNC) {
2291 			insn->cfi = &func_cfi;
2292 			continue;
2293 		}
2294 
2295 		if (insn->cfi)
2296 			cfi = *(insn->cfi);
2297 
2298 		if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
2299 			ERROR_INSN(insn, "unsupported unwind_hint sp base reg %d", hint->sp_reg);
2300 			return -1;
2301 		}
2302 
2303 		cfi.cfa.offset = bswap_if_needed(file->elf, hint->sp_offset);
2304 		cfi.type = hint->type;
2305 		cfi.signal = hint->signal;
2306 
2307 		insn->cfi = cfi_hash_find_or_add(&cfi);
2308 	}
2309 
2310 	return 0;
2311 }
2312 
2313 static int read_annotate(struct objtool_file *file,
2314 			 int (*func)(struct objtool_file *file, int type, struct instruction *insn))
2315 {
2316 	struct section *sec;
2317 	struct instruction *insn;
2318 	struct reloc *reloc;
2319 	uint64_t offset;
2320 	int type;
2321 
2322 	sec = find_section_by_name(file->elf, ".discard.annotate_insn");
2323 	if (!sec)
2324 		return 0;
2325 
2326 	if (!sec->rsec)
2327 		return 0;
2328 
2329 	if (sec->sh.sh_entsize != 8) {
2330 		static bool warned = false;
2331 		if (!warned && opts.verbose) {
2332 			WARN("%s: dodgy linker, sh_entsize != 8", sec->name);
2333 			warned = true;
2334 		}
2335 		sec->sh.sh_entsize = 8;
2336 	}
2337 
2338 	if (sec_num_entries(sec) != sec_num_entries(sec->rsec)) {
2339 		ERROR("bad .discard.annotate_insn section: missing relocs");
2340 		return -1;
2341 	}
2342 
2343 	for_each_reloc(sec->rsec, reloc) {
2344 		type = annotype(file->elf, sec, reloc);
2345 		offset = reloc->sym->offset + reloc_addend(reloc);
2346 		insn = find_insn(file, reloc->sym->sec, offset);
2347 
2348 		if (!insn) {
2349 			ERROR("bad .discard.annotate_insn entry: %d of type %d", reloc_idx(reloc), type);
2350 			return -1;
2351 		}
2352 
2353 		if (func(file, type, insn))
2354 			return -1;
2355 	}
2356 
2357 	return 0;
2358 }
2359 
2360 static int __annotate_early(struct objtool_file *file, int type, struct instruction *insn)
2361 {
2362 	switch (type) {
2363 
2364 	/* Must be before add_special_section_alts() */
2365 	case ANNOTYPE_IGNORE_ALTS:
2366 		insn->ignore_alts = true;
2367 		break;
2368 
2369 	/*
2370 	 * Must be before read_unwind_hints() since that needs insn->noendbr.
2371 	 */
2372 	case ANNOTYPE_NOENDBR:
2373 		insn->noendbr = 1;
2374 		break;
2375 
2376 	default:
2377 		break;
2378 	}
2379 
2380 	return 0;
2381 }
2382 
2383 static int __annotate_ifc(struct objtool_file *file, int type, struct instruction *insn)
2384 {
2385 	unsigned long dest_off;
2386 
2387 	if (type != ANNOTYPE_INTRA_FUNCTION_CALL)
2388 		return 0;
2389 
2390 	if (insn->type != INSN_CALL) {
2391 		ERROR_INSN(insn, "intra_function_call not a direct call");
2392 		return -1;
2393 	}
2394 
2395 	/*
2396 	 * Treat intra-function CALLs as JMPs, but with a stack_op.
2397 	 * See add_call_destinations(), which strips stack_ops from
2398 	 * normal CALLs.
2399 	 */
2400 	insn->type = INSN_JUMP_UNCONDITIONAL;
2401 
2402 	dest_off = arch_jump_destination(insn);
2403 	insn->jump_dest = find_insn(file, insn->sec, dest_off);
2404 	if (!insn->jump_dest) {
2405 		ERROR_INSN(insn, "can't find call dest at %s+0x%lx",
2406 			   insn->sec->name, dest_off);
2407 		return -1;
2408 	}
2409 
2410 	return 0;
2411 }
2412 
2413 static int __annotate_late(struct objtool_file *file, int type, struct instruction *insn)
2414 {
2415 	struct symbol *sym;
2416 
2417 	switch (type) {
2418 	case ANNOTYPE_NOENDBR:
2419 		/* early */
2420 		break;
2421 
2422 	case ANNOTYPE_RETPOLINE_SAFE:
2423 		if (insn->type != INSN_JUMP_DYNAMIC &&
2424 		    insn->type != INSN_CALL_DYNAMIC &&
2425 		    insn->type != INSN_RETURN &&
2426 		    insn->type != INSN_NOP) {
2427 			ERROR_INSN(insn, "retpoline_safe hint not an indirect jump/call/ret/nop");
2428 			return -1;
2429 		}
2430 
2431 		insn->retpoline_safe = true;
2432 		break;
2433 
2434 	case ANNOTYPE_INSTR_BEGIN:
2435 		insn->instr++;
2436 		break;
2437 
2438 	case ANNOTYPE_INSTR_END:
2439 		insn->instr--;
2440 		break;
2441 
2442 	case ANNOTYPE_UNRET_BEGIN:
2443 		insn->unret = 1;
2444 		break;
2445 
2446 	case ANNOTYPE_IGNORE_ALTS:
2447 		/* early */
2448 		break;
2449 
2450 	case ANNOTYPE_INTRA_FUNCTION_CALL:
2451 		/* ifc */
2452 		break;
2453 
2454 	case ANNOTYPE_REACHABLE:
2455 		insn->dead_end = false;
2456 		break;
2457 
2458 	case ANNOTYPE_NOCFI:
2459 		sym = insn_sym(insn);
2460 		if (!sym) {
2461 			ERROR_INSN(insn, "dodgy NOCFI annotation");
2462 			return -1;
2463 		}
2464 		sym->nocfi = 1;
2465 		break;
2466 
2467 	default:
2468 		ERROR_INSN(insn, "Unknown annotation type: %d", type);
2469 		return -1;
2470 	}
2471 
2472 	return 0;
2473 }
2474 
2475 /*
2476  * Return true if name matches an instrumentation function, where calls to that
2477  * function from noinstr code can safely be removed, but compilers won't do so.
2478  */
2479 static bool is_profiling_func(const char *name)
2480 {
2481 	/*
2482 	 * Many compilers cannot disable KCOV with a function attribute.
2483 	 */
2484 	if (!strncmp(name, "__sanitizer_cov_", 16))
2485 		return true;
2486 
2487 	return false;
2488 }
2489 
2490 static int classify_symbols(struct objtool_file *file)
2491 {
2492 	struct symbol *func;
2493 	size_t len;
2494 
2495 	for_each_sym(file->elf, func) {
2496 		if (is_notype_sym(func) && strstarts(func->name, ".L"))
2497 			func->local_label = true;
2498 
2499 		if (!is_global_sym(func))
2500 			continue;
2501 
2502 		if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
2503 			     strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2504 			func->static_call_tramp = true;
2505 
2506 		if (arch_is_retpoline(func))
2507 			func->retpoline_thunk = true;
2508 
2509 		if (arch_is_rethunk(func))
2510 			func->return_thunk = true;
2511 
2512 		if (arch_is_embedded_insn(func))
2513 			func->embedded_insn = true;
2514 
2515 		if (arch_ftrace_match(func->name))
2516 			func->fentry = true;
2517 
2518 		if (is_profiling_func(func->name))
2519 			func->profiling_func = true;
2520 
2521 		len = strlen(func->name);
2522 		if (len > sym_name_max_len)
2523 			sym_name_max_len = len;
2524 	}
2525 
2526 	return 0;
2527 }
2528 
2529 static void mark_rodata(struct objtool_file *file)
2530 {
2531 	struct section *sec;
2532 
2533 	/*
2534 	 * Search for the following rodata sections, each of which can
2535 	 * potentially contain jump tables:
2536 	 *
2537 	 * - .rodata: can contain GCC switch tables
2538 	 * - .rodata.<func>: same, if -fdata-sections is being used
2539 	 * - .data.rel.ro.c_jump_table: contains C annotated jump tables
2540 	 *
2541 	 * .rodata.str1.* sections are ignored; they don't contain jump tables.
2542 	 */
2543 	for_each_sec(file->elf, sec) {
2544 		if (is_rodata_sec(sec)) {
2545 			file->rodata = true;
2546 			return;
2547 		}
2548 	}
2549 }
2550 
2551 static void mark_holes(struct objtool_file *file)
2552 {
2553 	struct instruction *insn;
2554 	bool in_hole = false;
2555 
2556 	if (!opts.link)
2557 		return;
2558 
2559 	/*
2560 	 * Whole archive runs might encounter dead code from weak symbols.
2561 	 * This is where the linker will have dropped the weak symbol in
2562 	 * favour of a regular symbol, but leaves the code in place.
2563 	 */
2564 	for_each_insn(file, insn) {
2565 		if (insn_sym(insn) || !find_symbol_hole_containing(insn->sec, insn->offset)) {
2566 			in_hole = false;
2567 			continue;
2568 		}
2569 
2570 		/* Skip function padding and pfx code */
2571 		if (!in_hole && insn->type == INSN_NOP)
2572 			continue;
2573 
2574 		in_hole = true;
2575 		insn->hole = 1;
2576 
2577 		/*
2578 		 * If this hole jumps to a .cold function, mark it ignore.
2579 		 */
2580 		if (insn->jump_dest) {
2581 			struct symbol *dest_func = insn_func(insn->jump_dest);
2582 
2583 			if (dest_func && is_cold_func(dest_func))
2584 				dest_func->ignore = true;
2585 		}
2586 	}
2587 }
2588 
2589 static bool validate_branch_enabled(void)
2590 {
2591 	return opts.stackval	||
2592 	       opts.orc		||
2593 	       opts.uaccess;
2594 }
2595 
2596 static bool alts_needed(void)
2597 {
2598 	return validate_branch_enabled()	||
2599 	       opts.noinstr			||
2600 	       opts.hack_jump_label		||
2601 	       opts.disas			||
2602 	       opts.checksum;
2603 }
2604 
2605 int decode_file(struct objtool_file *file)
2606 {
2607 	arch_initial_func_cfi_state(&initial_func_cfi);
2608 	init_cfi_state(&init_cfi);
2609 	init_cfi_state(&func_cfi);
2610 	set_func_state(&func_cfi);
2611 	init_cfi_state(&force_undefined_cfi);
2612 	force_undefined_cfi.force_undefined = true;
2613 
2614 	if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
2615 		return -1;
2616 
2617 	cfi_hash_add(&init_cfi);
2618 	cfi_hash_add(&func_cfi);
2619 
2620 	file->klp = is_livepatch_module(file);
2621 
2622 	mark_rodata(file);
2623 
2624 	if (init_pv_ops(file))
2625 		return -1;
2626 
2627 	/*
2628 	 * Must be before add_{jump_call}_destination.
2629 	 */
2630 	if (classify_symbols(file))
2631 		return -1;
2632 
2633 	if (decode_instructions(file))
2634 		return -1;
2635 
2636 	if (add_ignores(file))
2637 		return -1;
2638 
2639 	add_uaccess_safe(file);
2640 
2641 	if (read_annotate(file, __annotate_early))
2642 		return -1;
2643 
2644 	/*
2645 	 * Must be before add_jump_destinations(), which depends on 'func'
2646 	 * being set for alternatives, to enable proper sibling call detection.
2647 	 */
2648 	if (alts_needed()) {
2649 		if (add_special_section_alts(file))
2650 			return -1;
2651 	}
2652 
2653 	if (add_jump_destinations(file))
2654 		return -1;
2655 
2656 	/*
2657 	 * Must be before add_call_destination(); it changes INSN_CALL to
2658 	 * INSN_JUMP.
2659 	 */
2660 	if (read_annotate(file, __annotate_ifc))
2661 		return -1;
2662 
2663 	if (add_call_destinations(file))
2664 		return -1;
2665 
2666 	if (add_jump_table_alts(file))
2667 		return -1;
2668 
2669 	if (read_unwind_hints(file))
2670 		return -1;
2671 
2672 	/* Must be after add_jump_destinations() */
2673 	mark_holes(file);
2674 
2675 	/*
2676 	 * Must be after add_call_destinations() such that it can override
2677 	 * dead_end_function() marks.
2678 	 */
2679 	if (read_annotate(file, __annotate_late))
2680 		return -1;
2681 
2682 	return 0;
2683 }
2684 
2685 static bool is_special_call(struct instruction *insn)
2686 {
2687 	if (insn->type == INSN_CALL) {
2688 		struct symbol *dest = insn_call_dest(insn);
2689 
2690 		if (!dest)
2691 			return false;
2692 
2693 		if (dest->fentry || dest->embedded_insn)
2694 			return true;
2695 	}
2696 
2697 	return false;
2698 }
2699 
2700 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
2701 {
2702 	struct cfi_state *cfi = &state->cfi;
2703 	int i;
2704 
2705 	if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
2706 		return true;
2707 
2708 	if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
2709 		return true;
2710 
2711 	if (cfi->stack_size != initial_func_cfi.cfa.offset)
2712 		return true;
2713 
2714 	for (i = 0; i < CFI_NUM_REGS; i++) {
2715 		if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2716 		    cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
2717 			return true;
2718 	}
2719 
2720 	return false;
2721 }
2722 
2723 static bool check_reg_frame_pos(const struct cfi_reg *reg,
2724 				int expected_offset)
2725 {
2726 	return reg->base == CFI_CFA &&
2727 	       reg->offset == expected_offset;
2728 }
2729 
2730 static bool has_valid_stack_frame(struct insn_state *state)
2731 {
2732 	struct cfi_state *cfi = &state->cfi;
2733 
2734 	if (cfi->cfa.base == CFI_BP &&
2735 	    check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2736 	    check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
2737 		return true;
2738 
2739 	if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
2740 		return true;
2741 
2742 	return false;
2743 }
2744 
2745 static int update_cfi_state_regs(struct instruction *insn,
2746 				  struct cfi_state *cfi,
2747 				  struct stack_op *op)
2748 {
2749 	struct cfi_reg *cfa = &cfi->cfa;
2750 
2751 	if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
2752 		return 0;
2753 
2754 	/* push */
2755 	if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
2756 		cfa->offset += 8;
2757 
2758 	/* pop */
2759 	if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
2760 		cfa->offset -= 8;
2761 
2762 	/* add immediate to sp */
2763 	if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2764 	    op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2765 		cfa->offset -= op->src.offset;
2766 
2767 	return 0;
2768 }
2769 
2770 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2771 {
2772 	if (arch_callee_saved_reg(reg) &&
2773 	    cfi->regs[reg].base == CFI_UNDEFINED) {
2774 		cfi->regs[reg].base = base;
2775 		cfi->regs[reg].offset = offset;
2776 	}
2777 }
2778 
2779 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2780 {
2781 	cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2782 	cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2783 }
2784 
2785 /*
2786  * A note about DRAP stack alignment:
2787  *
2788  * GCC has the concept of a DRAP register, which is used to help keep track of
2789  * the stack pointer when aligning the stack.  r10 or r13 is used as the DRAP
2790  * register.  The typical DRAP pattern is:
2791  *
2792  *   4c 8d 54 24 08		lea    0x8(%rsp),%r10
2793  *   48 83 e4 c0		and    $0xffffffffffffffc0,%rsp
2794  *   41 ff 72 f8		pushq  -0x8(%r10)
2795  *   55				push   %rbp
2796  *   48 89 e5			mov    %rsp,%rbp
2797  *				(more pushes)
2798  *   41 52			push   %r10
2799  *				...
2800  *   41 5a			pop    %r10
2801  *				(more pops)
2802  *   5d				pop    %rbp
2803  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
2804  *   c3				retq
2805  *
2806  * There are some variations in the epilogues, like:
2807  *
2808  *   5b				pop    %rbx
2809  *   41 5a			pop    %r10
2810  *   41 5c			pop    %r12
2811  *   41 5d			pop    %r13
2812  *   41 5e			pop    %r14
2813  *   c9				leaveq
2814  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
2815  *   c3				retq
2816  *
2817  * and:
2818  *
2819  *   4c 8b 55 e8		mov    -0x18(%rbp),%r10
2820  *   48 8b 5d e0		mov    -0x20(%rbp),%rbx
2821  *   4c 8b 65 f0		mov    -0x10(%rbp),%r12
2822  *   4c 8b 6d f8		mov    -0x8(%rbp),%r13
2823  *   c9				leaveq
2824  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
2825  *   c3				retq
2826  *
2827  * Sometimes r13 is used as the DRAP register, in which case it's saved and
2828  * restored beforehand:
2829  *
2830  *   41 55			push   %r13
2831  *   4c 8d 6c 24 10		lea    0x10(%rsp),%r13
2832  *   48 83 e4 f0		and    $0xfffffffffffffff0,%rsp
2833  *				...
2834  *   49 8d 65 f0		lea    -0x10(%r13),%rsp
2835  *   41 5d			pop    %r13
2836  *   c3				retq
2837  */
2838 static int update_cfi_state(struct instruction *insn,
2839 			    struct instruction *next_insn,
2840 			    struct cfi_state *cfi, struct stack_op *op)
2841 {
2842 	struct cfi_reg *cfa = &cfi->cfa;
2843 	struct cfi_reg *regs = cfi->regs;
2844 
2845 	/* ignore UNWIND_HINT_UNDEFINED regions */
2846 	if (cfi->force_undefined)
2847 		return 0;
2848 
2849 	/* stack operations don't make sense with an undefined CFA */
2850 	if (cfa->base == CFI_UNDEFINED) {
2851 		if (insn_func(insn)) {
2852 			WARN_INSN(insn, "undefined stack state");
2853 			return 1;
2854 		}
2855 		return 0;
2856 	}
2857 
2858 	if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2859 	    cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2860 		return update_cfi_state_regs(insn, cfi, op);
2861 
2862 	switch (op->dest.type) {
2863 
2864 	case OP_DEST_REG:
2865 		switch (op->src.type) {
2866 
2867 		case OP_SRC_REG:
2868 			if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2869 			    cfa->base == CFI_SP &&
2870 			    check_reg_frame_pos(&regs[CFI_BP], -cfa->offset)) {
2871 
2872 				/* mov %rsp, %rbp */
2873 				cfa->base = op->dest.reg;
2874 				cfi->bp_scratch = false;
2875 			}
2876 
2877 			else if (op->src.reg == CFI_SP &&
2878 				 op->dest.reg == CFI_BP && cfi->drap) {
2879 
2880 				/* drap: mov %rsp, %rbp */
2881 				regs[CFI_BP].base = CFI_BP;
2882 				regs[CFI_BP].offset = -cfi->stack_size;
2883 				cfi->bp_scratch = false;
2884 			}
2885 
2886 			else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2887 
2888 				/*
2889 				 * mov %rsp, %reg
2890 				 *
2891 				 * This is needed for the rare case where GCC
2892 				 * does:
2893 				 *
2894 				 *   mov    %rsp, %rax
2895 				 *   ...
2896 				 *   mov    %rax, %rsp
2897 				 */
2898 				cfi->vals[op->dest.reg].base = CFI_CFA;
2899 				cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2900 			}
2901 
2902 			else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2903 				 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2904 
2905 				/*
2906 				 * mov %rbp, %rsp
2907 				 *
2908 				 * Restore the original stack pointer (Clang).
2909 				 */
2910 				cfi->stack_size = -cfi->regs[CFI_BP].offset;
2911 			}
2912 
2913 			else if (op->dest.reg == cfa->base) {
2914 
2915 				/* mov %reg, %rsp */
2916 				if (cfa->base == CFI_SP &&
2917 				    cfi->vals[op->src.reg].base == CFI_CFA) {
2918 
2919 					/*
2920 					 * This is needed for the rare case
2921 					 * where GCC does something dumb like:
2922 					 *
2923 					 *   lea    0x8(%rsp), %rcx
2924 					 *   ...
2925 					 *   mov    %rcx, %rsp
2926 					 */
2927 					cfa->offset = -cfi->vals[op->src.reg].offset;
2928 					cfi->stack_size = cfa->offset;
2929 
2930 				} else if (cfa->base == CFI_SP &&
2931 					   cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2932 					   cfi->vals[op->src.reg].offset == cfa->offset) {
2933 
2934 					/*
2935 					 * Stack swizzle:
2936 					 *
2937 					 * 1: mov %rsp, (%[tos])
2938 					 * 2: mov %[tos], %rsp
2939 					 *    ...
2940 					 * 3: pop %rsp
2941 					 *
2942 					 * Where:
2943 					 *
2944 					 * 1 - places a pointer to the previous
2945 					 *     stack at the Top-of-Stack of the
2946 					 *     new stack.
2947 					 *
2948 					 * 2 - switches to the new stack.
2949 					 *
2950 					 * 3 - pops the Top-of-Stack to restore
2951 					 *     the original stack.
2952 					 *
2953 					 * Note: we set base to SP_INDIRECT
2954 					 * here and preserve offset. Therefore
2955 					 * when the unwinder reaches ToS it
2956 					 * will dereference SP and then add the
2957 					 * offset to find the next frame, IOW:
2958 					 * (%rsp) + offset.
2959 					 */
2960 					cfa->base = CFI_SP_INDIRECT;
2961 
2962 				} else {
2963 					cfa->base = CFI_UNDEFINED;
2964 					cfa->offset = 0;
2965 				}
2966 			}
2967 
2968 			else if (op->dest.reg == CFI_SP &&
2969 				 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2970 				 cfi->vals[op->src.reg].offset == cfa->offset) {
2971 
2972 				/*
2973 				 * The same stack swizzle case 2) as above. But
2974 				 * because we can't change cfa->base, case 3)
2975 				 * will become a regular POP. Pretend we're a
2976 				 * PUSH so things don't go unbalanced.
2977 				 */
2978 				cfi->stack_size += 8;
2979 			}
2980 
2981 			else if (cfi->vals[op->src.reg].base == CFI_CFA) {
2982 				/*
2983 				 * Clang RSP musical chairs:
2984 				 *
2985 				 *   mov %rsp, %rdx [handled above]
2986 				 *   ...
2987 				 *   mov %rdx, %rbx [handled here]
2988 				 *   ...
2989 				 *   mov %rbx, %rsp [handled above]
2990 				 */
2991 				cfi->vals[op->dest.reg].base = CFI_CFA;
2992 				cfi->vals[op->dest.reg].offset = cfi->vals[op->src.reg].offset;
2993 			}
2994 
2995 
2996 			break;
2997 
2998 		case OP_SRC_ADD:
2999 			if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
3000 
3001 				/* add imm, %rsp */
3002 				cfi->stack_size -= op->src.offset;
3003 				if (cfa->base == CFI_SP)
3004 					cfa->offset -= op->src.offset;
3005 				break;
3006 			}
3007 
3008 			if (op->dest.reg == CFI_BP && op->src.reg == CFI_SP &&
3009 			    insn_sym(insn)->frame_pointer) {
3010 				/* addi.d fp,sp,imm on LoongArch */
3011 				if (cfa->base == CFI_SP && cfa->offset == op->src.offset) {
3012 					cfa->base = CFI_BP;
3013 					cfa->offset = 0;
3014 				}
3015 				break;
3016 			}
3017 
3018 			if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
3019 				/* addi.d sp,fp,imm on LoongArch */
3020 				if (cfa->base == CFI_BP && cfa->offset == 0) {
3021 					if (insn_sym(insn)->frame_pointer) {
3022 						cfa->base = CFI_SP;
3023 						cfa->offset = -op->src.offset;
3024 					}
3025 				} else {
3026 					/* lea disp(%rbp), %rsp */
3027 					cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
3028 				}
3029 				break;
3030 			}
3031 
3032 			if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
3033 
3034 				/* drap: lea disp(%rsp), %drap */
3035 				cfi->drap_reg = op->dest.reg;
3036 
3037 				/*
3038 				 * lea disp(%rsp), %reg
3039 				 *
3040 				 * This is needed for the rare case where GCC
3041 				 * does something dumb like:
3042 				 *
3043 				 *   lea    0x8(%rsp), %rcx
3044 				 *   ...
3045 				 *   mov    %rcx, %rsp
3046 				 */
3047 				cfi->vals[op->dest.reg].base = CFI_CFA;
3048 				cfi->vals[op->dest.reg].offset = \
3049 					-cfi->stack_size + op->src.offset;
3050 
3051 				break;
3052 			}
3053 
3054 			if (cfi->drap && op->dest.reg == CFI_SP &&
3055 			    op->src.reg == cfi->drap_reg) {
3056 
3057 				 /* drap: lea disp(%drap), %rsp */
3058 				cfa->base = CFI_SP;
3059 				cfa->offset = cfi->stack_size = -op->src.offset;
3060 				cfi->drap_reg = CFI_UNDEFINED;
3061 				cfi->drap = false;
3062 				break;
3063 			}
3064 
3065 			if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
3066 				WARN_INSN(insn, "unsupported stack register modification");
3067 				return -1;
3068 			}
3069 
3070 			break;
3071 
3072 		case OP_SRC_AND:
3073 			if (op->dest.reg != CFI_SP ||
3074 			    (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
3075 			    (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
3076 				WARN_INSN(insn, "unsupported stack pointer realignment");
3077 				return -1;
3078 			}
3079 
3080 			if (cfi->drap_reg != CFI_UNDEFINED) {
3081 				/* drap: and imm, %rsp */
3082 				cfa->base = cfi->drap_reg;
3083 				cfa->offset = cfi->stack_size = 0;
3084 				cfi->drap = true;
3085 			}
3086 
3087 			/*
3088 			 * Older versions of GCC (4.8ish) realign the stack
3089 			 * without DRAP, with a frame pointer.
3090 			 */
3091 
3092 			break;
3093 
3094 		case OP_SRC_POP:
3095 		case OP_SRC_POPF:
3096 			if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
3097 
3098 				/* pop %rsp; # restore from a stack swizzle */
3099 				cfa->base = CFI_SP;
3100 				break;
3101 			}
3102 
3103 			if (!cfi->drap && op->dest.reg == cfa->base) {
3104 
3105 				/* pop %rbp */
3106 				cfa->base = CFI_SP;
3107 			}
3108 
3109 			if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
3110 			    op->dest.reg == cfi->drap_reg &&
3111 			    cfi->drap_offset == -cfi->stack_size) {
3112 
3113 				/* drap: pop %drap */
3114 				cfa->base = cfi->drap_reg;
3115 				cfa->offset = 0;
3116 				cfi->drap_offset = -1;
3117 
3118 			} else if (cfi->stack_size == -regs[op->dest.reg].offset) {
3119 
3120 				/* pop %reg */
3121 				restore_reg(cfi, op->dest.reg);
3122 			}
3123 
3124 			cfi->stack_size -= 8;
3125 			if (cfa->base == CFI_SP)
3126 				cfa->offset -= 8;
3127 
3128 			break;
3129 
3130 		case OP_SRC_REG_INDIRECT:
3131 			if (!cfi->drap && op->dest.reg == cfa->base &&
3132 			    op->dest.reg == CFI_BP) {
3133 
3134 				/* mov disp(%rsp), %rbp */
3135 				cfa->base = CFI_SP;
3136 				cfa->offset = cfi->stack_size;
3137 			}
3138 
3139 			if (cfi->drap && op->src.reg == CFI_BP &&
3140 			    op->src.offset == cfi->drap_offset) {
3141 
3142 				/* drap: mov disp(%rbp), %drap */
3143 				cfa->base = cfi->drap_reg;
3144 				cfa->offset = 0;
3145 				cfi->drap_offset = -1;
3146 			}
3147 
3148 			if (cfi->drap && op->src.reg == CFI_BP &&
3149 			    op->src.offset == regs[op->dest.reg].offset) {
3150 
3151 				/* drap: mov disp(%rbp), %reg */
3152 				restore_reg(cfi, op->dest.reg);
3153 
3154 			} else if (op->src.reg == cfa->base &&
3155 			    op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
3156 
3157 				/* mov disp(%rbp), %reg */
3158 				/* mov disp(%rsp), %reg */
3159 				restore_reg(cfi, op->dest.reg);
3160 
3161 			} else if (op->src.reg == CFI_SP &&
3162 				   op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
3163 
3164 				/* mov disp(%rsp), %reg */
3165 				restore_reg(cfi, op->dest.reg);
3166 			}
3167 
3168 			break;
3169 
3170 		default:
3171 			WARN_INSN(insn, "unknown stack-related instruction");
3172 			return -1;
3173 		}
3174 
3175 		break;
3176 
3177 	case OP_DEST_PUSH:
3178 	case OP_DEST_PUSHF:
3179 		cfi->stack_size += 8;
3180 		if (cfa->base == CFI_SP)
3181 			cfa->offset += 8;
3182 
3183 		if (op->src.type != OP_SRC_REG)
3184 			break;
3185 
3186 		if (cfi->drap) {
3187 			if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
3188 
3189 				/* drap: push %drap */
3190 				cfa->base = CFI_BP_INDIRECT;
3191 				cfa->offset = -cfi->stack_size;
3192 
3193 				/* save drap so we know when to restore it */
3194 				cfi->drap_offset = -cfi->stack_size;
3195 
3196 			} else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
3197 
3198 				/* drap: push %rbp */
3199 				cfi->stack_size = 0;
3200 
3201 			} else {
3202 
3203 				/* drap: push %reg */
3204 				save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
3205 			}
3206 
3207 		} else {
3208 
3209 			/* push %reg */
3210 			save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
3211 		}
3212 
3213 		/* detect when asm code uses rbp as a scratch register */
3214 		if (opts.stackval && insn_func(insn) && op->src.reg == CFI_BP &&
3215 		    cfa->base != CFI_BP)
3216 			cfi->bp_scratch = true;
3217 		break;
3218 
3219 	case OP_DEST_REG_INDIRECT:
3220 
3221 		if (cfi->drap) {
3222 			if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
3223 
3224 				/* drap: mov %drap, disp(%rbp) */
3225 				cfa->base = CFI_BP_INDIRECT;
3226 				cfa->offset = op->dest.offset;
3227 
3228 				/* save drap offset so we know when to restore it */
3229 				cfi->drap_offset = op->dest.offset;
3230 			} else {
3231 
3232 				/* drap: mov reg, disp(%rbp) */
3233 				save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
3234 			}
3235 
3236 		} else if (op->dest.reg == cfa->base) {
3237 
3238 			/* mov reg, disp(%rbp) */
3239 			/* mov reg, disp(%rsp) */
3240 			save_reg(cfi, op->src.reg, CFI_CFA,
3241 				 op->dest.offset - cfi->cfa.offset);
3242 
3243 		} else if (op->dest.reg == CFI_SP) {
3244 
3245 			/* mov reg, disp(%rsp) */
3246 			save_reg(cfi, op->src.reg, CFI_CFA,
3247 				 op->dest.offset - cfi->stack_size);
3248 
3249 		} else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
3250 
3251 			/* mov %rsp, (%reg); # setup a stack swizzle. */
3252 			cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
3253 			cfi->vals[op->dest.reg].offset = cfa->offset;
3254 		}
3255 
3256 		break;
3257 
3258 	case OP_DEST_MEM:
3259 		if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
3260 			WARN_INSN(insn, "unknown stack-related memory operation");
3261 			return -1;
3262 		}
3263 
3264 		/* pop mem */
3265 		cfi->stack_size -= 8;
3266 		if (cfa->base == CFI_SP)
3267 			cfa->offset -= 8;
3268 
3269 		break;
3270 
3271 	default:
3272 		WARN_INSN(insn, "unknown stack-related instruction");
3273 		return -1;
3274 	}
3275 
3276 	return 0;
3277 }
3278 
3279 /*
3280  * The stack layouts of alternatives instructions can sometimes diverge when
3281  * they have stack modifications.  That's fine as long as the potential stack
3282  * layouts don't conflict at any given potential instruction boundary.
3283  *
3284  * Flatten the CFIs of the different alternative code streams (both original
3285  * and replacement) into a single shared CFI array which can be used to detect
3286  * conflicts and nicely feed a linear array of ORC entries to the unwinder.
3287  */
3288 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
3289 {
3290 	struct cfi_state **alt_cfi;
3291 	int group_off;
3292 
3293 	if (!insn->alt_group)
3294 		return 0;
3295 
3296 	if (!insn->cfi) {
3297 		WARN("CFI missing");
3298 		return -1;
3299 	}
3300 
3301 	alt_cfi = insn->alt_group->cfi;
3302 	group_off = insn->offset - insn->alt_group->first_insn->offset;
3303 
3304 	if (!alt_cfi[group_off]) {
3305 		alt_cfi[group_off] = insn->cfi;
3306 	} else {
3307 		if (cficmp(alt_cfi[group_off], insn->cfi)) {
3308 			struct alt_group *orig_group = insn->alt_group->orig_group ?: insn->alt_group;
3309 			struct instruction *orig = orig_group->first_insn;
3310 			WARN_INSN(orig, "stack layout conflict in alternatives: %s",
3311 				  offstr(insn->sec, insn->offset));
3312 			return -1;
3313 		}
3314 	}
3315 
3316 	return 0;
3317 }
3318 
3319 static int noinline handle_insn_ops(struct instruction *insn,
3320 				    struct instruction *next_insn,
3321 				    struct insn_state *state)
3322 {
3323 	struct insn_state prev_state __maybe_unused = *state;
3324 	struct stack_op *op;
3325 	int ret = 0;
3326 
3327 	for (op = insn->stack_ops; op; op = op->next) {
3328 
3329 		ret = update_cfi_state(insn, next_insn, &state->cfi, op);
3330 		if (ret)
3331 			goto done;
3332 
3333 		if (!opts.uaccess || !insn->alt_group)
3334 			continue;
3335 
3336 		if (op->dest.type == OP_DEST_PUSHF) {
3337 			if (!state->uaccess_stack) {
3338 				state->uaccess_stack = 1;
3339 			} else if (state->uaccess_stack >> 31) {
3340 				WARN_INSN(insn, "PUSHF stack exhausted");
3341 				ret = 1;
3342 				goto done;
3343 			}
3344 			state->uaccess_stack <<= 1;
3345 			state->uaccess_stack  |= state->uaccess;
3346 		}
3347 
3348 		if (op->src.type == OP_SRC_POPF) {
3349 			if (state->uaccess_stack) {
3350 				state->uaccess = state->uaccess_stack & 1;
3351 				state->uaccess_stack >>= 1;
3352 				if (state->uaccess_stack == 1)
3353 					state->uaccess_stack = 0;
3354 			}
3355 		}
3356 	}
3357 
3358 done:
3359 	TRACE_INSN_STATE(insn, &prev_state, state);
3360 
3361 	return ret;
3362 }
3363 
3364 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
3365 {
3366 	struct cfi_state *cfi1 = insn->cfi;
3367 	int i;
3368 
3369 	if (!cfi1) {
3370 		WARN("CFI missing");
3371 		return false;
3372 	}
3373 
3374 	if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
3375 
3376 		WARN_INSN(insn, "stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
3377 			  cfi1->cfa.base, cfi1->cfa.offset,
3378 			  cfi2->cfa.base, cfi2->cfa.offset);
3379 		return false;
3380 
3381 	}
3382 
3383 	if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
3384 		for (i = 0; i < CFI_NUM_REGS; i++) {
3385 
3386 			if (!memcmp(&cfi1->regs[i], &cfi2->regs[i], sizeof(struct cfi_reg)))
3387 				continue;
3388 
3389 			WARN_INSN(insn, "stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
3390 				  i, cfi1->regs[i].base, cfi1->regs[i].offset,
3391 				  i, cfi2->regs[i].base, cfi2->regs[i].offset);
3392 		}
3393 		return false;
3394 	}
3395 
3396 	if (cfi1->type != cfi2->type) {
3397 
3398 		WARN_INSN(insn, "stack state mismatch: type1=%d type2=%d",
3399 			  cfi1->type, cfi2->type);
3400 		return false;
3401 	}
3402 
3403 	if (cfi1->drap != cfi2->drap ||
3404 		   (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
3405 		   (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
3406 
3407 		WARN_INSN(insn, "stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
3408 			  cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
3409 			  cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
3410 		return false;
3411 	}
3412 
3413 	return true;
3414 }
3415 
3416 static inline bool func_uaccess_safe(struct symbol *func)
3417 {
3418 	if (func)
3419 		return func->uaccess_safe;
3420 
3421 	return false;
3422 }
3423 
3424 static inline const char *call_dest_name(struct instruction *insn)
3425 {
3426 	static char pvname[19];
3427 	struct reloc *reloc;
3428 	int idx;
3429 
3430 	if (insn_call_dest(insn))
3431 		return insn_call_dest(insn)->name;
3432 
3433 	reloc = insn_reloc(NULL, insn);
3434 	if (reloc && !strcmp(reloc->sym->name, "pv_ops")) {
3435 		idx = (reloc_addend(reloc) / sizeof(void *));
3436 		snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
3437 		return pvname;
3438 	}
3439 
3440 	return "{dynamic}";
3441 }
3442 
3443 static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
3444 {
3445 	struct symbol *target;
3446 	struct reloc *reloc;
3447 	int idx;
3448 
3449 	reloc = insn_reloc(file, insn);
3450 	if (!reloc || strcmp(reloc->sym->name, "pv_ops"))
3451 		return false;
3452 
3453 	idx = arch_insn_adjusted_addend(insn, reloc) / sizeof(void *);
3454 
3455 	if (file->pv_ops[idx].clean)
3456 		return true;
3457 
3458 	file->pv_ops[idx].clean = true;
3459 
3460 	list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) {
3461 		if (!target->sec->noinstr) {
3462 			WARN("pv_ops[%d]: %s", idx, target->name);
3463 			file->pv_ops[idx].clean = false;
3464 		}
3465 	}
3466 
3467 	return file->pv_ops[idx].clean;
3468 }
3469 
3470 static inline bool noinstr_call_dest(struct objtool_file *file,
3471 				     struct instruction *insn,
3472 				     struct symbol *func)
3473 {
3474 	/*
3475 	 * We can't deal with indirect function calls at present;
3476 	 * assume they're instrumented.
3477 	 */
3478 	if (!func) {
3479 		if (file->pv_ops)
3480 			return pv_call_dest(file, insn);
3481 
3482 		return false;
3483 	}
3484 
3485 	/*
3486 	 * If the symbol is from a noinstr section; we good.
3487 	 */
3488 	if (func->sec->noinstr)
3489 		return true;
3490 
3491 	/*
3492 	 * If the symbol is a static_call trampoline, we can't tell.
3493 	 */
3494 	if (func->static_call_tramp)
3495 		return true;
3496 
3497 	/*
3498 	 * The __ubsan_handle_*() calls are like WARN(), they only happen when
3499 	 * something 'BAD' happened. At the risk of taking the machine down,
3500 	 * let them proceed to get the message out.
3501 	 */
3502 	if (!strncmp(func->name, "__ubsan_handle_", 15))
3503 		return true;
3504 
3505 	return false;
3506 }
3507 
3508 static int validate_call(struct objtool_file *file,
3509 			 struct instruction *insn,
3510 			 struct insn_state *state)
3511 {
3512 	if (state->noinstr && state->instr <= 0 &&
3513 	    !noinstr_call_dest(file, insn, insn_call_dest(insn))) {
3514 		WARN_INSN(insn, "call to %s() leaves .noinstr.text section", call_dest_name(insn));
3515 		return 1;
3516 	}
3517 
3518 	if (state->uaccess && !func_uaccess_safe(insn_call_dest(insn))) {
3519 		WARN_INSN(insn, "call to %s() with UACCESS enabled", call_dest_name(insn));
3520 		return 1;
3521 	}
3522 
3523 	if (state->df) {
3524 		WARN_INSN(insn, "call to %s() with DF set", call_dest_name(insn));
3525 		return 1;
3526 	}
3527 
3528 	return 0;
3529 }
3530 
3531 static int validate_sibling_call(struct objtool_file *file,
3532 				 struct instruction *insn,
3533 				 struct insn_state *state)
3534 {
3535 	if (insn_func(insn) && has_modified_stack_frame(insn, state)) {
3536 		WARN_INSN(insn, "sibling call from callable instruction with modified stack frame");
3537 		return 1;
3538 	}
3539 
3540 	return validate_call(file, insn, state);
3541 }
3542 
3543 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
3544 {
3545 	if (state->noinstr && state->instr > 0) {
3546 		WARN_INSN(insn, "return with instrumentation enabled");
3547 		return 1;
3548 	}
3549 
3550 	if (state->uaccess && !func_uaccess_safe(func)) {
3551 		WARN_INSN(insn, "return with UACCESS enabled");
3552 		return 1;
3553 	}
3554 
3555 	if (!state->uaccess && func_uaccess_safe(func)) {
3556 		WARN_INSN(insn, "return with UACCESS disabled from a UACCESS-safe function");
3557 		return 1;
3558 	}
3559 
3560 	if (state->df) {
3561 		WARN_INSN(insn, "return with DF set");
3562 		return 1;
3563 	}
3564 
3565 	if (func && has_modified_stack_frame(insn, state)) {
3566 		WARN_INSN(insn, "return with modified stack frame");
3567 		return 1;
3568 	}
3569 
3570 	if (state->cfi.bp_scratch) {
3571 		WARN_INSN(insn, "BP used as a scratch register");
3572 		return 1;
3573 	}
3574 
3575 	return 0;
3576 }
3577 
3578 static struct instruction *next_insn_to_validate(struct objtool_file *file,
3579 						 struct instruction *insn)
3580 {
3581 	struct alt_group *alt_group = insn->alt_group;
3582 
3583 	/*
3584 	 * Simulate the fact that alternatives are patched in-place.  When the
3585 	 * end of a replacement alt_group is reached, redirect objtool flow to
3586 	 * the end of the original alt_group.
3587 	 *
3588 	 * insn->alts->insn -> alt_group->first_insn
3589 	 *		       ...
3590 	 *		       alt_group->last_insn
3591 	 *		       [alt_group->nop]      -> next(orig_group->last_insn)
3592 	 */
3593 	if (alt_group) {
3594 		if (alt_group->nop) {
3595 			/* ->nop implies ->orig_group */
3596 			if (insn == alt_group->last_insn)
3597 				return alt_group->nop;
3598 			if (insn == alt_group->nop)
3599 				goto next_orig;
3600 		}
3601 		if (insn == alt_group->last_insn && alt_group->orig_group)
3602 			goto next_orig;
3603 	}
3604 
3605 	return next_insn_same_sec(file, insn);
3606 
3607 next_orig:
3608 	return next_insn_same_sec(file, alt_group->orig_group->last_insn);
3609 }
3610 
3611 static bool skip_alt_group(struct instruction *insn)
3612 {
3613 	struct instruction *alt_insn = insn->alts ? insn->alts->insn : NULL;
3614 
3615 	if (!insn->alt_group)
3616 		return false;
3617 
3618 	/* ANNOTATE_IGNORE_ALTERNATIVE */
3619 	if (insn->alt_group->ignore) {
3620 		TRACE_ALT(insn, "alt group ignored");
3621 		return true;
3622 	}
3623 
3624 	/*
3625 	 * For NOP patched with CLAC/STAC, only follow the latter to avoid
3626 	 * impossible code paths combining patched CLAC with unpatched STAC
3627 	 * or vice versa.
3628 	 *
3629 	 * ANNOTATE_IGNORE_ALTERNATIVE could have been used here, but Linus
3630 	 * requested not to do that to avoid hurting .s file readability
3631 	 * around CLAC/STAC alternative sites.
3632 	 */
3633 
3634 	if (!alt_insn)
3635 		return false;
3636 
3637 	/* Don't override ASM_{CLAC,STAC}_UNSAFE */
3638 	if (alt_insn->alt_group && alt_insn->alt_group->ignore)
3639 		return false;
3640 
3641 	return alt_insn->type == INSN_CLAC || alt_insn->type == INSN_STAC;
3642 }
3643 
3644 static int validate_branch(struct objtool_file *file, struct symbol *func,
3645 			   struct instruction *insn, struct insn_state state);
3646 static int do_validate_branch(struct objtool_file *file, struct symbol *func,
3647 			      struct instruction *insn, struct insn_state *state);
3648 
3649 static int validate_insn(struct objtool_file *file, struct symbol *func,
3650 			 struct instruction *insn, struct insn_state *statep,
3651 			 struct instruction *prev_insn, struct instruction *next_insn,
3652 			 bool *dead_end)
3653 {
3654 	char *alt_name __maybe_unused = NULL;
3655 	struct alternative *alt;
3656 	u8 visited;
3657 	int ret;
3658 
3659 	/*
3660 	 * Any returns before the end of this function are effectively dead
3661 	 * ends, i.e. validate_branch() has reached the end of the branch.
3662 	 */
3663 	*dead_end = true;
3664 
3665 	visited = VISITED_BRANCH << statep->uaccess;
3666 	if (insn->visited & VISITED_BRANCH_MASK) {
3667 		if (!insn->hint && !insn_cfi_match(insn, &statep->cfi))
3668 			return 1;
3669 
3670 		if (insn->visited & visited) {
3671 			TRACE_INSN(insn, "already visited");
3672 			return 0;
3673 		}
3674 	} else {
3675 		nr_insns_visited++;
3676 	}
3677 
3678 	if (statep->noinstr)
3679 		statep->instr += insn->instr;
3680 
3681 	if (insn->hint) {
3682 		if (insn->restore) {
3683 			struct instruction *save_insn, *i;
3684 
3685 			i = insn;
3686 			save_insn = NULL;
3687 
3688 			sym_for_each_insn_continue_reverse(file, func, i) {
3689 				if (i->save) {
3690 					save_insn = i;
3691 					break;
3692 				}
3693 			}
3694 
3695 			if (!save_insn) {
3696 				WARN_INSN(insn, "no corresponding CFI save for CFI restore");
3697 				return 1;
3698 			}
3699 
3700 			if (!save_insn->visited) {
3701 				/*
3702 				 * If the restore hint insn is at the
3703 				 * beginning of a basic block and was
3704 				 * branched to from elsewhere, and the
3705 				 * save insn hasn't been visited yet,
3706 				 * defer following this branch for now.
3707 				 * It will be seen later via the
3708 				 * straight-line path.
3709 				 */
3710 				if (!prev_insn) {
3711 					TRACE_INSN(insn, "defer restore");
3712 					return 0;
3713 				}
3714 
3715 				WARN_INSN(insn, "objtool isn't smart enough to handle this CFI save/restore combo");
3716 				return 1;
3717 			}
3718 
3719 			insn->cfi = save_insn->cfi;
3720 			nr_cfi_reused++;
3721 		}
3722 
3723 		statep->cfi = *insn->cfi;
3724 	} else {
3725 		/* XXX track if we actually changed statep->cfi */
3726 
3727 		if (prev_insn && !cficmp(prev_insn->cfi, &statep->cfi)) {
3728 			insn->cfi = prev_insn->cfi;
3729 			nr_cfi_reused++;
3730 		} else {
3731 			insn->cfi = cfi_hash_find_or_add(&statep->cfi);
3732 		}
3733 	}
3734 
3735 	insn->visited |= visited;
3736 
3737 	if (propagate_alt_cfi(file, insn))
3738 		return 1;
3739 
3740 	if (insn->alts) {
3741 		for (alt = insn->alts; alt; alt = alt->next) {
3742 			TRACE_ALT_BEGIN(insn, alt, alt_name);
3743 			ret = validate_branch(file, func, alt->insn, *statep);
3744 			TRACE_ALT_END(insn, alt, alt_name);
3745 			if (ret) {
3746 				BT_INSN(insn, "(alt)");
3747 				return ret;
3748 			}
3749 		}
3750 		TRACE_ALT_INFO_NOADDR(insn, "/ ", "DEFAULT");
3751 	}
3752 
3753 	if (skip_alt_group(insn))
3754 		return 0;
3755 
3756 	if (handle_insn_ops(insn, next_insn, statep))
3757 		return 1;
3758 
3759 	switch (insn->type) {
3760 
3761 	case INSN_RETURN:
3762 		TRACE_INSN(insn, "return");
3763 		return validate_return(func, insn, statep);
3764 
3765 	case INSN_CALL:
3766 	case INSN_CALL_DYNAMIC:
3767 		if (insn->type == INSN_CALL)
3768 			TRACE_INSN(insn, "call");
3769 		else
3770 			TRACE_INSN(insn, "indirect call");
3771 
3772 		ret = validate_call(file, insn, statep);
3773 		if (ret)
3774 			return ret;
3775 
3776 		if (opts.stackval && func && !is_special_call(insn) &&
3777 		    !has_valid_stack_frame(statep)) {
3778 			WARN_INSN(insn, "call without frame pointer save/setup");
3779 			return 1;
3780 		}
3781 
3782 		break;
3783 
3784 	case INSN_JUMP_CONDITIONAL:
3785 	case INSN_JUMP_UNCONDITIONAL:
3786 		if (is_sibling_call(insn)) {
3787 			TRACE_INSN(insn, "sibling call");
3788 			ret = validate_sibling_call(file, insn, statep);
3789 			if (ret)
3790 				return ret;
3791 
3792 		} else if (insn->jump_dest) {
3793 			if (insn->type == INSN_JUMP_UNCONDITIONAL)
3794 				TRACE_INSN(insn, "unconditional jump");
3795 			else
3796 				TRACE_INSN(insn, "jump taken");
3797 
3798 			ret = validate_branch(file, func, insn->jump_dest, *statep);
3799 			if (ret) {
3800 				BT_INSN(insn, "(branch)");
3801 				return ret;
3802 			}
3803 		}
3804 
3805 		if (insn->type == INSN_JUMP_UNCONDITIONAL)
3806 			return 0;
3807 
3808 		TRACE_INSN(insn, "jump not taken");
3809 		break;
3810 
3811 	case INSN_JUMP_DYNAMIC:
3812 	case INSN_JUMP_DYNAMIC_CONDITIONAL:
3813 		TRACE_INSN(insn, "indirect jump");
3814 		if (is_sibling_call(insn)) {
3815 			ret = validate_sibling_call(file, insn, statep);
3816 			if (ret)
3817 				return ret;
3818 		}
3819 
3820 		if (insn->type == INSN_JUMP_DYNAMIC)
3821 			return 0;
3822 
3823 		break;
3824 
3825 	case INSN_SYSCALL:
3826 		TRACE_INSN(insn, "syscall");
3827 		if (func && (!next_insn || !next_insn->hint)) {
3828 			WARN_INSN(insn, "unsupported instruction in callable function");
3829 			return 1;
3830 		}
3831 
3832 		break;
3833 
3834 	case INSN_SYSRET:
3835 		TRACE_INSN(insn, "sysret");
3836 		if (func && (!next_insn || !next_insn->hint)) {
3837 			WARN_INSN(insn, "unsupported instruction in callable function");
3838 			return 1;
3839 		}
3840 
3841 		return 0;
3842 
3843 	case INSN_STAC:
3844 		TRACE_INSN(insn, "stac");
3845 		if (!opts.uaccess)
3846 			break;
3847 
3848 		if (statep->uaccess) {
3849 			WARN_INSN(insn, "recursive UACCESS enable");
3850 			return 1;
3851 		}
3852 
3853 		statep->uaccess = true;
3854 		break;
3855 
3856 	case INSN_CLAC:
3857 		TRACE_INSN(insn, "clac");
3858 		if (!opts.uaccess)
3859 			break;
3860 
3861 		if (!statep->uaccess && func) {
3862 			WARN_INSN(insn, "redundant UACCESS disable");
3863 			return 1;
3864 		}
3865 
3866 		if (func_uaccess_safe(func) && !statep->uaccess_stack) {
3867 			WARN_INSN(insn, "UACCESS-safe disables UACCESS");
3868 			return 1;
3869 		}
3870 
3871 		statep->uaccess = false;
3872 		break;
3873 
3874 	case INSN_STD:
3875 		TRACE_INSN(insn, "std");
3876 		if (statep->df) {
3877 			WARN_INSN(insn, "recursive STD");
3878 			return 1;
3879 		}
3880 
3881 		statep->df = true;
3882 		break;
3883 
3884 	case INSN_CLD:
3885 		TRACE_INSN(insn, "cld");
3886 		if (!statep->df && func) {
3887 			WARN_INSN(insn, "redundant CLD");
3888 			return 1;
3889 		}
3890 
3891 		statep->df = false;
3892 		break;
3893 
3894 	default:
3895 		break;
3896 	}
3897 
3898 	if (insn->dead_end)
3899 		TRACE_INSN(insn, "dead end");
3900 
3901 	*dead_end = insn->dead_end;
3902 	return 0;
3903 }
3904 
3905 /*
3906  * Follow the branch starting at the given instruction, and recursively follow
3907  * any other branches (jumps).  Meanwhile, track the frame pointer state at
3908  * each instruction and validate all the rules described in
3909  * tools/objtool/Documentation/objtool.txt.
3910  */
3911 static int do_validate_branch(struct objtool_file *file, struct symbol *func,
3912 			      struct instruction *insn, struct insn_state *state)
3913 {
3914 	struct instruction *next_insn, *prev_insn = NULL;
3915 	bool dead_end;
3916 	int ret;
3917 
3918 	if (func && func->ignore)
3919 		return 0;
3920 
3921 	do {
3922 		insn->trace = 0;
3923 		next_insn = next_insn_to_validate(file, insn);
3924 
3925 		if (func && insn_func(insn) && func != insn_func(insn)->pfunc) {
3926 			/* Ignore KCFI type preambles, which always fall through */
3927 			if (is_prefix_func(func))
3928 				return 0;
3929 
3930 			if (file->ignore_unreachables)
3931 				return 0;
3932 
3933 			WARN("%s() falls through to next function %s()",
3934 			     func->name, insn_func(insn)->name);
3935 			func->warned = 1;
3936 
3937 			return 1;
3938 		}
3939 
3940 		ret = validate_insn(file, func, insn, state, prev_insn, next_insn,
3941 				    &dead_end);
3942 
3943 		if (!insn->trace) {
3944 			if (ret)
3945 				TRACE_INSN(insn, "warning (%d)", ret);
3946 			else
3947 				TRACE_INSN(insn, NULL);
3948 		}
3949 
3950 		if (!dead_end && !next_insn) {
3951 			if (state->cfi.cfa.base == CFI_UNDEFINED)
3952 				return 0;
3953 			if (file->ignore_unreachables)
3954 				return 0;
3955 
3956 			WARN("%s%sunexpected end of section %s",
3957 			     func ? func->name : "", func ? "(): " : "",
3958 			     insn->sec->name);
3959 			return 1;
3960 		}
3961 
3962 		prev_insn = insn;
3963 		insn = next_insn;
3964 
3965 	} while (!dead_end);
3966 
3967 	return ret;
3968 }
3969 
3970 static int validate_branch(struct objtool_file *file, struct symbol *func,
3971 			   struct instruction *insn, struct insn_state state)
3972 {
3973 	int ret;
3974 
3975 	trace_depth_inc();
3976 	ret = do_validate_branch(file, func, insn, &state);
3977 	trace_depth_dec();
3978 
3979 	return ret;
3980 }
3981 
3982 static int validate_unwind_hint(struct objtool_file *file,
3983 				  struct instruction *insn,
3984 				  struct insn_state *state)
3985 {
3986 	if (insn->hint && !insn->visited) {
3987 		struct symbol *func = insn_func(insn);
3988 		int ret;
3989 
3990 		ret = validate_branch(file, func, insn, *state);
3991 		if (ret)
3992 			BT_INSN(insn, "<=== (hint)");
3993 		return ret;
3994 	}
3995 
3996 	return 0;
3997 }
3998 
3999 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
4000 {
4001 	struct instruction *insn;
4002 	struct insn_state state;
4003 	int warnings = 0;
4004 
4005 	if (!file->hints)
4006 		return 0;
4007 
4008 	init_insn_state(file, &state, sec);
4009 
4010 	if (sec) {
4011 		sec_for_each_insn(file, sec, insn)
4012 			warnings += validate_unwind_hint(file, insn, &state);
4013 	} else {
4014 		for_each_insn(file, insn)
4015 			warnings += validate_unwind_hint(file, insn, &state);
4016 	}
4017 
4018 	return warnings;
4019 }
4020 
4021 /*
4022  * Validate rethunk entry constraint: must untrain RET before the first RET.
4023  *
4024  * Follow every branch (intra-function) and ensure VALIDATE_UNRET_END comes
4025  * before an actual RET instruction.
4026  */
4027 static int validate_unret(struct objtool_file *file, struct instruction *insn)
4028 {
4029 	struct instruction *next, *dest;
4030 	int ret;
4031 
4032 	for (;;) {
4033 		next = next_insn_to_validate(file, insn);
4034 
4035 		if (insn->visited & VISITED_UNRET)
4036 			return 0;
4037 
4038 		insn->visited |= VISITED_UNRET;
4039 
4040 		if (insn->alts) {
4041 			struct alternative *alt;
4042 			for (alt = insn->alts; alt; alt = alt->next) {
4043 				ret = validate_unret(file, alt->insn);
4044 				if (ret) {
4045 					BT_INSN(insn, "(alt)");
4046 					return ret;
4047 				}
4048 			}
4049 		}
4050 
4051 		switch (insn->type) {
4052 
4053 		case INSN_CALL_DYNAMIC:
4054 		case INSN_JUMP_DYNAMIC:
4055 		case INSN_JUMP_DYNAMIC_CONDITIONAL:
4056 			WARN_INSN(insn, "early indirect call");
4057 			return 1;
4058 
4059 		case INSN_JUMP_UNCONDITIONAL:
4060 		case INSN_JUMP_CONDITIONAL:
4061 			if (!is_sibling_call(insn)) {
4062 				if (!insn->jump_dest) {
4063 					WARN_INSN(insn, "unresolved jump target after linking?!?");
4064 					return 1;
4065 				}
4066 				ret = validate_unret(file, insn->jump_dest);
4067 				if (ret) {
4068 					BT_INSN(insn, "(branch%s)",
4069 						insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : "");
4070 					return ret;
4071 				}
4072 
4073 				if (insn->type == INSN_JUMP_UNCONDITIONAL)
4074 					return 0;
4075 
4076 				break;
4077 			}
4078 
4079 			/* fallthrough */
4080 		case INSN_CALL:
4081 			dest = find_insn(file, insn_call_dest(insn)->sec,
4082 					 insn_call_dest(insn)->offset);
4083 			if (!dest) {
4084 				WARN("Unresolved function after linking!?: %s",
4085 				     insn_call_dest(insn)->name);
4086 				return 1;
4087 			}
4088 
4089 			ret = validate_unret(file, dest);
4090 			if (ret) {
4091 				BT_INSN(insn, "(call)");
4092 				return ret;
4093 			}
4094 			/*
4095 			 * If a call returns without error, it must have seen UNTRAIN_RET.
4096 			 * Therefore any non-error return is a success.
4097 			 */
4098 			return 0;
4099 
4100 		case INSN_RETURN:
4101 			WARN_INSN(insn, "RET before UNTRAIN");
4102 			return 1;
4103 
4104 		case INSN_SYSCALL:
4105 			break;
4106 
4107 		case INSN_SYSRET:
4108 			return 0;
4109 
4110 		case INSN_NOP:
4111 			if (insn->retpoline_safe)
4112 				return 0;
4113 			break;
4114 
4115 		default:
4116 			break;
4117 		}
4118 
4119 		if (insn->dead_end)
4120 			return 0;
4121 
4122 		if (!next) {
4123 			WARN_INSN(insn, "teh end!");
4124 			return 1;
4125 		}
4126 		insn = next;
4127 	}
4128 
4129 	return 0;
4130 }
4131 
4132 /*
4133  * Validate that all branches starting at VALIDATE_UNRET_BEGIN encounter
4134  * VALIDATE_UNRET_END before RET.
4135  */
4136 static int validate_unrets(struct objtool_file *file)
4137 {
4138 	struct instruction *insn;
4139 	int warnings = 0;
4140 
4141 	for_each_insn(file, insn) {
4142 		if (!insn->unret)
4143 			continue;
4144 
4145 		warnings += validate_unret(file, insn);
4146 	}
4147 
4148 	return warnings;
4149 }
4150 
4151 static int validate_retpoline(struct objtool_file *file)
4152 {
4153 	struct instruction *insn;
4154 	int warnings = 0;
4155 
4156 	for_each_insn(file, insn) {
4157 		if (insn->type != INSN_JUMP_DYNAMIC &&
4158 		    insn->type != INSN_CALL_DYNAMIC &&
4159 		    insn->type != INSN_RETURN)
4160 			continue;
4161 
4162 		if (insn->retpoline_safe)
4163 			continue;
4164 
4165 		if (insn->sec->init)
4166 			continue;
4167 
4168 		if (insn->type == INSN_RETURN) {
4169 			if (opts.rethunk) {
4170 				WARN_INSN(insn, "'naked' return found in MITIGATION_RETHUNK build");
4171 				warnings++;
4172 			}
4173 			continue;
4174 		}
4175 
4176 		WARN_INSN(insn, "indirect %s found in MITIGATION_RETPOLINE build",
4177 			  insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
4178 		warnings++;
4179 	}
4180 
4181 	if (!opts.cfi)
4182 		return warnings;
4183 
4184 	/*
4185 	 * kCFI call sites look like:
4186 	 *
4187 	 *     movl $(-0x12345678), %r10d
4188 	 *     addl -4(%r11), %r10d
4189 	 *     jz 1f
4190 	 *     ud2
4191 	 *  1: cs call __x86_indirect_thunk_r11
4192 	 *
4193 	 * Verify all indirect calls are kCFI adorned by checking for the
4194 	 * UD2. Notably, doing __nocfi calls to regular (cfi) functions is
4195 	 * broken.
4196 	 */
4197 	list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
4198 		struct symbol *sym = insn_sym(insn);
4199 
4200 		if (sym && (is_notype_sym(sym) ||
4201 			    is_func_sym(sym)) && !sym->nocfi) {
4202 			struct instruction *prev =
4203 				prev_insn_same_sym(file, insn);
4204 
4205 			if (!prev || prev->type != INSN_BUG) {
4206 				WARN_INSN(insn, "no-cfi indirect call!");
4207 				warnings++;
4208 			}
4209 		}
4210 	}
4211 
4212 	return warnings;
4213 }
4214 
4215 static bool is_kasan_insn(struct instruction *insn)
4216 {
4217 	return (insn->type == INSN_CALL &&
4218 		!strcmp(insn_call_dest(insn)->name, "__asan_handle_no_return"));
4219 }
4220 
4221 static bool is_ubsan_insn(struct instruction *insn)
4222 {
4223 	return (insn->type == INSN_CALL &&
4224 		!strcmp(insn_call_dest(insn)->name,
4225 			"__ubsan_handle_builtin_unreachable"));
4226 }
4227 
4228 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
4229 {
4230 	struct symbol *func = insn_func(insn);
4231 	struct instruction *prev_insn;
4232 	int i;
4233 
4234 	if (insn->type == INSN_NOP || insn->type == INSN_TRAP ||
4235 	    insn->hole || (func && func->ignore))
4236 		return true;
4237 
4238 	/*
4239 	 * Ignore alternative replacement instructions.  This can happen
4240 	 * when a whitelisted function uses one of the ALTERNATIVE macros.
4241 	 */
4242 	if (!strcmp(insn->sec->name, ".altinstr_replacement") ||
4243 	    !strcmp(insn->sec->name, ".altinstr_aux"))
4244 		return true;
4245 
4246 	if (!func)
4247 		return false;
4248 
4249 	if (func->static_call_tramp)
4250 		return true;
4251 
4252 	/*
4253 	 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
4254 	 * __builtin_unreachable().  The BUG() macro has an unreachable() after
4255 	 * the UD2, which causes GCC's undefined trap logic to emit another UD2
4256 	 * (or occasionally a JMP to UD2).
4257 	 *
4258 	 * It may also insert a UD2 after calling a __noreturn function.
4259 	 */
4260 	prev_insn = prev_insn_same_sec(file, insn);
4261 	if (prev_insn && prev_insn->dead_end &&
4262 	    (insn->type == INSN_BUG ||
4263 	     (insn->type == INSN_JUMP_UNCONDITIONAL &&
4264 	      insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
4265 		return true;
4266 
4267 	/*
4268 	 * Check if this (or a subsequent) instruction is related to
4269 	 * CONFIG_UBSAN or CONFIG_KASAN.
4270 	 *
4271 	 * End the search at 5 instructions to avoid going into the weeds.
4272 	 */
4273 	for (i = 0; i < 5; i++) {
4274 
4275 		if (is_kasan_insn(insn) || is_ubsan_insn(insn))
4276 			return true;
4277 
4278 		if (insn->type == INSN_JUMP_UNCONDITIONAL) {
4279 			if (insn->jump_dest &&
4280 			    insn_func(insn->jump_dest) == func) {
4281 				insn = insn->jump_dest;
4282 				continue;
4283 			}
4284 
4285 			break;
4286 		}
4287 
4288 		if (insn->offset + insn->len >= func->offset + func->len)
4289 			break;
4290 
4291 		insn = next_insn_same_sec(file, insn);
4292 	}
4293 
4294 	return false;
4295 }
4296 
4297 /*
4298  * For FineIBT or kCFI, a certain number of bytes preceding the function may be
4299  * NOPs.  Those NOPs may be rewritten at runtime and executed, so give them a
4300  * proper function name: __pfx_<func>.
4301  */
4302 static int create_prefix_symbol(struct objtool_file *file, struct symbol *func)
4303 {
4304 	struct instruction *insn, *prev;
4305 	char name[SYM_NAME_LEN];
4306 	struct cfi_state *cfi;
4307 
4308 	if ((strlen(func->name) + sizeof("__pfx_") > SYM_NAME_LEN)) {
4309 		WARN("%s: symbol name too long, can't create __pfx_ symbol",
4310 		      func->name);
4311 		return 0;
4312 	}
4313 
4314 	if (snprintf_check(name, SYM_NAME_LEN, "__pfx_%s", func->name))
4315 		return -1;
4316 
4317 	if (!elf_create_symbol(file->elf, name, func->sec,
4318 			       GELF_ST_BIND(func->sym.st_info),
4319 			       GELF_ST_TYPE(func->sym.st_info),
4320 			       func->offset - opts.prefix, opts.prefix))
4321 		return -1;
4322 
4323 	/* Propagate insn->cfi to the prefix code */
4324 	insn = find_insn(file, func->sec, func->offset);
4325 	if (!insn || !insn->cfi)
4326 		return 0;
4327 
4328 	cfi = cfi_hash_find_or_add(insn->cfi);
4329 	for (prev = find_insn(file, func->sec, func->offset - opts.prefix);
4330 	     prev && prev != insn;
4331 	     prev = next_insn_same_sec(file, prev))
4332 		prev->cfi = cfi;
4333 
4334 	return 0;
4335 }
4336 
4337 static int create_prefix_symbols(struct objtool_file *file)
4338 {
4339 	struct section *pfe_sec;
4340 	struct symbol *func;
4341 	struct reloc *reloc;
4342 
4343 	for_each_sec(file->elf, pfe_sec) {
4344 		if (strcmp(pfe_sec->name, "__patchable_function_entries"))
4345 			continue;
4346 		if (!pfe_sec->rsec)
4347 			continue;
4348 
4349 		for_each_reloc(pfe_sec->rsec, reloc) {
4350 			func = find_func_by_offset(reloc->sym->sec,
4351 						   reloc->sym->offset + reloc_addend(reloc) + opts.prefix);
4352 			if (func && create_prefix_symbol(file, func))
4353 				return -1;
4354 		}
4355 	}
4356 
4357 	return 0;
4358 }
4359 
4360 static int validate_symbol(struct objtool_file *file, struct section *sec,
4361 			   struct symbol *sym, struct insn_state *state)
4362 {
4363 	struct instruction *insn;
4364 	struct symbol *func;
4365 	int ret;
4366 
4367 	if (!sym->len) {
4368 		WARN("%s() is missing an ELF size annotation", sym->name);
4369 		return 1;
4370 	}
4371 
4372 	if (sym->pfunc != sym || is_alias_sym(sym))
4373 		return 0;
4374 
4375 	insn = find_insn(file, sec, sym->offset);
4376 	if (!insn || insn->visited)
4377 		return 0;
4378 
4379 	if (opts.uaccess)
4380 		state->uaccess = sym->uaccess_safe;
4381 
4382 	func = insn_func(insn);
4383 
4384 	if (opts.trace && !fnmatch(opts.trace, sym->name, 0)) {
4385 		trace_enable();
4386 		TRACE("%s: validation begin\n", sym->name);
4387 	}
4388 
4389 	ret = validate_branch(file, func, insn, *state);
4390 	if (ret)
4391 		BT_INSN(insn, "<=== (sym)");
4392 
4393 	TRACE("%s: validation %s\n\n", sym->name, ret ? "failed" : "end");
4394 	trace_disable();
4395 
4396 	return ret;
4397 }
4398 
4399 static int validate_section(struct objtool_file *file, struct section *sec)
4400 {
4401 	struct insn_state state;
4402 	struct symbol *func;
4403 	int warnings = 0;
4404 
4405 	sec_for_each_sym(sec, func) {
4406 		if (!is_func_sym(func))
4407 			continue;
4408 
4409 		init_insn_state(file, &state, sec);
4410 		set_func_state(&state.cfi);
4411 
4412 		warnings += validate_symbol(file, sec, func, &state);
4413 	}
4414 
4415 	return warnings;
4416 }
4417 
4418 static int validate_noinstr_sections(struct objtool_file *file)
4419 {
4420 	struct section *sec;
4421 	int warnings = 0;
4422 
4423 	sec = find_section_by_name(file->elf, ".noinstr.text");
4424 	if (sec) {
4425 		warnings += validate_section(file, sec);
4426 		warnings += validate_unwind_hints(file, sec);
4427 	}
4428 
4429 	sec = find_section_by_name(file->elf, ".entry.text");
4430 	if (sec) {
4431 		warnings += validate_section(file, sec);
4432 		warnings += validate_unwind_hints(file, sec);
4433 	}
4434 
4435 	sec = find_section_by_name(file->elf, ".cpuidle.text");
4436 	if (sec) {
4437 		warnings += validate_section(file, sec);
4438 		warnings += validate_unwind_hints(file, sec);
4439 	}
4440 
4441 	return warnings;
4442 }
4443 
4444 static int validate_functions(struct objtool_file *file)
4445 {
4446 	struct section *sec;
4447 	int warnings = 0;
4448 
4449 	for_each_sec(file->elf, sec) {
4450 		if (!is_text_sec(sec))
4451 			continue;
4452 
4453 		warnings += validate_section(file, sec);
4454 	}
4455 
4456 	return warnings;
4457 }
4458 
4459 static void mark_endbr_used(struct instruction *insn)
4460 {
4461 	if (!list_empty(&insn->call_node))
4462 		list_del_init(&insn->call_node);
4463 }
4464 
4465 static bool noendbr_range(struct objtool_file *file, struct instruction *insn)
4466 {
4467 	struct symbol *sym = find_symbol_containing(insn->sec, insn->offset-1);
4468 	struct instruction *first;
4469 
4470 	if (!sym)
4471 		return false;
4472 
4473 	first = find_insn(file, sym->sec, sym->offset);
4474 	if (!first)
4475 		return false;
4476 
4477 	if (first->type != INSN_ENDBR && !first->noendbr)
4478 		return false;
4479 
4480 	return insn->offset == sym->offset + sym->len;
4481 }
4482 
4483 static int __validate_ibt_insn(struct objtool_file *file, struct instruction *insn,
4484 			       struct instruction *dest)
4485 {
4486 	if (dest->type == INSN_ENDBR) {
4487 		mark_endbr_used(dest);
4488 		return 0;
4489 	}
4490 
4491 	if (insn_func(dest) && insn_func(insn) &&
4492 	    insn_func(dest)->pfunc == insn_func(insn)->pfunc) {
4493 		/*
4494 		 * Anything from->to self is either _THIS_IP_ or
4495 		 * IRET-to-self.
4496 		 *
4497 		 * There is no sane way to annotate _THIS_IP_ since the
4498 		 * compiler treats the relocation as a constant and is
4499 		 * happy to fold in offsets, skewing any annotation we
4500 		 * do, leading to vast amounts of false-positives.
4501 		 *
4502 		 * There's also compiler generated _THIS_IP_ through
4503 		 * KCOV and such which we have no hope of annotating.
4504 		 *
4505 		 * As such, blanket accept self-references without
4506 		 * issue.
4507 		 */
4508 		return 0;
4509 	}
4510 
4511 	/*
4512 	 * Accept anything ANNOTATE_NOENDBR.
4513 	 */
4514 	if (dest->noendbr)
4515 		return 0;
4516 
4517 	/*
4518 	 * Accept if this is the instruction after a symbol
4519 	 * that is (no)endbr -- typical code-range usage.
4520 	 */
4521 	if (noendbr_range(file, dest))
4522 		return 0;
4523 
4524 	WARN_INSN(insn, "relocation to !ENDBR: %s", offstr(dest->sec, dest->offset));
4525 	return 1;
4526 }
4527 
4528 static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn)
4529 {
4530 	struct instruction *dest;
4531 	struct reloc *reloc;
4532 	unsigned long off;
4533 	int warnings = 0;
4534 
4535 	/*
4536 	 * Looking for function pointer load relocations.  Ignore
4537 	 * direct/indirect branches:
4538 	 */
4539 	switch (insn->type) {
4540 
4541 	case INSN_CALL:
4542 	case INSN_CALL_DYNAMIC:
4543 	case INSN_JUMP_CONDITIONAL:
4544 	case INSN_JUMP_UNCONDITIONAL:
4545 	case INSN_JUMP_DYNAMIC:
4546 	case INSN_JUMP_DYNAMIC_CONDITIONAL:
4547 	case INSN_RETURN:
4548 	case INSN_NOP:
4549 		return 0;
4550 
4551 	case INSN_LEA_RIP:
4552 		if (!insn_reloc(file, insn)) {
4553 			/* local function pointer reference without reloc */
4554 
4555 			off = arch_jump_destination(insn);
4556 
4557 			dest = find_insn(file, insn->sec, off);
4558 			if (!dest) {
4559 				WARN_INSN(insn, "corrupt function pointer reference");
4560 				return 1;
4561 			}
4562 
4563 			return __validate_ibt_insn(file, insn, dest);
4564 		}
4565 		break;
4566 
4567 	default:
4568 		break;
4569 	}
4570 
4571 	for (reloc = insn_reloc(file, insn);
4572 	     reloc;
4573 	     reloc = find_reloc_by_dest_range(file->elf, insn->sec,
4574 					      reloc_offset(reloc) + 1,
4575 					      (insn->offset + insn->len) - (reloc_offset(reloc) + 1))) {
4576 
4577 		off = reloc->sym->offset + arch_insn_adjusted_addend(insn, reloc);
4578 
4579 		dest = find_insn(file, reloc->sym->sec, off);
4580 		if (!dest)
4581 			continue;
4582 
4583 		warnings += __validate_ibt_insn(file, insn, dest);
4584 	}
4585 
4586 	return warnings;
4587 }
4588 
4589 static int validate_ibt_data_reloc(struct objtool_file *file,
4590 				   struct reloc *reloc)
4591 {
4592 	struct instruction *dest;
4593 
4594 	dest = find_insn(file, reloc->sym->sec,
4595 			 reloc->sym->offset + reloc_addend(reloc));
4596 	if (!dest)
4597 		return 0;
4598 
4599 	if (dest->type == INSN_ENDBR) {
4600 		mark_endbr_used(dest);
4601 		return 0;
4602 	}
4603 
4604 	if (dest->noendbr)
4605 		return 0;
4606 
4607 	WARN_FUNC(reloc->sec->base, reloc_offset(reloc),
4608 		  "data relocation to !ENDBR: %s", offstr(dest->sec, dest->offset));
4609 
4610 	return 1;
4611 }
4612 
4613 /*
4614  * Validate IBT rules and remove used ENDBR instructions from the seal list.
4615  * Unused ENDBR instructions will be annotated for sealing (i.e., replaced with
4616  * NOPs) later, in create_ibt_endbr_seal_sections().
4617  */
4618 static int validate_ibt(struct objtool_file *file)
4619 {
4620 	struct section *sec;
4621 	struct reloc *reloc;
4622 	struct instruction *insn;
4623 	int warnings = 0;
4624 
4625 	for_each_insn(file, insn)
4626 		warnings += validate_ibt_insn(file, insn);
4627 
4628 	for_each_sec(file->elf, sec) {
4629 
4630 		/* Already done by validate_ibt_insn() */
4631 		if (is_text_sec(sec))
4632 			continue;
4633 
4634 		if (!sec->rsec)
4635 			continue;
4636 
4637 		/*
4638 		 * These sections can reference text addresses, but not with
4639 		 * the intent to indirect branch to them.
4640 		 */
4641 		if ((!strncmp(sec->name, ".discard", 8) &&
4642 		     strcmp(sec->name, ".discard.ibt_endbr_noseal"))	||
4643 		    !strncmp(sec->name, ".debug", 6)			||
4644 		    !strcmp(sec->name, ".altinstructions")		||
4645 		    !strcmp(sec->name, ".ibt_endbr_seal")		||
4646 		    !strcmp(sec->name, ".kcfi_traps")			||
4647 		    !strcmp(sec->name, ".orc_unwind_ip")		||
4648 		    !strcmp(sec->name, ".retpoline_sites")		||
4649 		    !strcmp(sec->name, ".smp_locks")			||
4650 		    !strcmp(sec->name, ".static_call_sites")		||
4651 		    !strcmp(sec->name, "_error_injection_whitelist")	||
4652 		    !strcmp(sec->name, "_kprobe_blacklist")		||
4653 		    !strcmp(sec->name, "__bug_table")			||
4654 		    !strcmp(sec->name, "__ex_table")			||
4655 		    !strcmp(sec->name, "__jump_table")			||
4656 		    !strcmp(sec->name, ".init.klp_funcs")		||
4657 		    !strcmp(sec->name, "__mcount_loc")			||
4658 		    !strcmp(sec->name, ".llvm.call-graph-profile")	||
4659 		    !strcmp(sec->name, ".llvm_bb_addr_map")		||
4660 		    !strcmp(sec->name, "__tracepoints")			||
4661 		    !strcmp(sec->name, ".return_sites")			||
4662 		    !strcmp(sec->name, ".call_sites")			||
4663 		    !strcmp(sec->name, "__patchable_function_entries"))
4664 			continue;
4665 
4666 		for_each_reloc(sec->rsec, reloc)
4667 			warnings += validate_ibt_data_reloc(file, reloc);
4668 	}
4669 
4670 	return warnings;
4671 }
4672 
4673 static int validate_sls(struct objtool_file *file)
4674 {
4675 	struct instruction *insn, *next_insn;
4676 	int warnings = 0;
4677 
4678 	for_each_insn(file, insn) {
4679 		next_insn = next_insn_same_sec(file, insn);
4680 
4681 		if (insn->retpoline_safe)
4682 			continue;
4683 
4684 		switch (insn->type) {
4685 		case INSN_RETURN:
4686 			if (!next_insn || next_insn->type != INSN_TRAP) {
4687 				WARN_INSN(insn, "missing int3 after ret");
4688 				warnings++;
4689 			}
4690 
4691 			break;
4692 		case INSN_JUMP_DYNAMIC:
4693 			if (!next_insn || next_insn->type != INSN_TRAP) {
4694 				WARN_INSN(insn, "missing int3 after indirect jump");
4695 				warnings++;
4696 			}
4697 			break;
4698 		default:
4699 			break;
4700 		}
4701 	}
4702 
4703 	return warnings;
4704 }
4705 
4706 static int validate_reachable_instructions(struct objtool_file *file)
4707 {
4708 	struct instruction *insn, *prev_insn;
4709 	struct symbol *call_dest;
4710 	int warnings = 0;
4711 
4712 	if (file->ignore_unreachables)
4713 		return 0;
4714 
4715 	for_each_insn(file, insn) {
4716 		if (insn->visited || ignore_unreachable_insn(file, insn))
4717 			continue;
4718 
4719 		prev_insn = prev_insn_same_sec(file, insn);
4720 		if (prev_insn && prev_insn->dead_end) {
4721 			call_dest = insn_call_dest(prev_insn);
4722 			if (call_dest) {
4723 				WARN_INSN(insn, "%s() missing __noreturn in .c/.h or NORETURN() in noreturns.h",
4724 					  call_dest->name);
4725 				warnings++;
4726 				continue;
4727 			}
4728 		}
4729 
4730 		WARN_INSN(insn, "unreachable instruction");
4731 		warnings++;
4732 	}
4733 
4734 	return warnings;
4735 }
4736 
4737 __weak bool arch_absolute_reloc(struct elf *elf, struct reloc *reloc)
4738 {
4739 	unsigned int type = reloc_type(reloc);
4740 	size_t sz = elf_addr_size(elf);
4741 
4742 	return (sz == 8) ? (type == R_ABS64) : (type == R_ABS32);
4743 }
4744 
4745 static int check_abs_references(struct objtool_file *file)
4746 {
4747 	struct section *sec;
4748 	struct reloc *reloc;
4749 	int ret = 0;
4750 
4751 	for_each_sec(file->elf, sec) {
4752 		/* absolute references in non-loadable sections are fine */
4753 		if (!(sec->sh.sh_flags & SHF_ALLOC))
4754 			continue;
4755 
4756 		/* section must have an associated .rela section */
4757 		if (!sec->rsec)
4758 			continue;
4759 
4760 		/*
4761 		 * Special case for compiler generated metadata that is not
4762 		 * consumed until after boot.
4763 		 */
4764 		if (!strcmp(sec->name, "__patchable_function_entries"))
4765 			continue;
4766 
4767 		for_each_reloc(sec->rsec, reloc) {
4768 			if (arch_absolute_reloc(file->elf, reloc)) {
4769 				WARN("section %s has absolute relocation at offset 0x%llx",
4770 				     sec->name, (unsigned long long)reloc_offset(reloc));
4771 				ret++;
4772 			}
4773 		}
4774 	}
4775 	return ret;
4776 }
4777 
4778 struct insn_chunk {
4779 	void *addr;
4780 	struct insn_chunk *next;
4781 };
4782 
4783 /*
4784  * Reduce peak RSS usage by freeing insns memory before writing the ELF file,
4785  * which can trigger more allocations for .debug_* sections whose data hasn't
4786  * been read yet.
4787  */
4788 void free_insns(struct objtool_file *file)
4789 {
4790 	struct instruction *insn;
4791 	struct insn_chunk *chunks = NULL, *chunk;
4792 
4793 	for_each_insn(file, insn) {
4794 		if (!insn->idx) {
4795 			chunk = malloc(sizeof(*chunk));
4796 			chunk->addr = insn;
4797 			chunk->next = chunks;
4798 			chunks = chunk;
4799 		}
4800 	}
4801 
4802 	for (chunk = chunks; chunk; chunk = chunk->next)
4803 		free(chunk->addr);
4804 }
4805 
4806 const char *objtool_disas_insn(struct instruction *insn)
4807 {
4808 	struct disas_context *dctx = objtool_disas_ctx;
4809 
4810 	if (!dctx)
4811 		return "";
4812 
4813 	disas_insn(dctx, insn);
4814 	return disas_result(dctx);
4815 }
4816 
4817 int check(struct objtool_file *file)
4818 {
4819 	struct disas_context *disas_ctx = NULL;
4820 	int ret = 0, warnings = 0;
4821 
4822 	/*
4823 	 * Create a disassembly context if we might disassemble any
4824 	 * instruction or function.
4825 	 */
4826 	if (opts.verbose || opts.backtrace || opts.trace || opts.disas) {
4827 		disas_ctx = disas_context_create(file);
4828 		if (!disas_ctx) {
4829 			opts.disas = false;
4830 			opts.trace = false;
4831 		}
4832 		objtool_disas_ctx = disas_ctx;
4833 	}
4834 
4835 	ret = decode_file(file);
4836 	if (ret)
4837 		goto out;
4838 
4839 	if (!nr_insns)
4840 		goto out;
4841 
4842 	if (opts.retpoline)
4843 		warnings += validate_retpoline(file);
4844 
4845 	if (validate_branch_enabled()) {
4846 		int w = 0;
4847 
4848 		w += validate_functions(file);
4849 		w += validate_unwind_hints(file, NULL);
4850 		if (!w)
4851 			w += validate_reachable_instructions(file);
4852 
4853 		warnings += w;
4854 
4855 	} else if (opts.noinstr) {
4856 		warnings += validate_noinstr_sections(file);
4857 	}
4858 
4859 	if (opts.unret) {
4860 		/*
4861 		 * Must be after validate_branch() and friends, it plays
4862 		 * further games with insn->visited.
4863 		 */
4864 		warnings += validate_unrets(file);
4865 	}
4866 
4867 	if (opts.ibt)
4868 		warnings += validate_ibt(file);
4869 
4870 	if (opts.sls)
4871 		warnings += validate_sls(file);
4872 
4873 	if (opts.static_call) {
4874 		ret = create_static_call_sections(file);
4875 		if (ret)
4876 			goto out;
4877 	}
4878 
4879 	if (opts.retpoline) {
4880 		ret = create_retpoline_sites_sections(file);
4881 		if (ret)
4882 			goto out;
4883 	}
4884 
4885 	if (opts.rethunk) {
4886 		ret = create_return_sites_sections(file);
4887 		if (ret)
4888 			goto out;
4889 
4890 		if (opts.hack_skylake) {
4891 			ret = create_direct_call_sections(file);
4892 			if (ret)
4893 				goto out;
4894 		}
4895 	}
4896 
4897 	if (opts.mcount) {
4898 		ret = create_mcount_loc_sections(file);
4899 		if (ret)
4900 			goto out;
4901 	}
4902 
4903 	if (opts.prefix) {
4904 		if (!opts.cfi) {
4905 			ret = create_prefix_symbols(file);
4906 			if (ret)
4907 				goto out;
4908 		} else {
4909 			ret = grow_cfi_symbols(file);
4910 			if (ret)
4911 				goto out;
4912 
4913 			if (opts.fineibt) {
4914 				ret = create_cfi_sections(file);
4915 				if (ret)
4916 					goto out;
4917 			}
4918 		}
4919 	}
4920 
4921 	if (opts.ibt) {
4922 		ret = create_ibt_endbr_seal_sections(file);
4923 		if (ret)
4924 			goto out;
4925 	}
4926 
4927 	if (opts.klp_symids) {
4928 		ret = klp_create_symid_sections(file);
4929 		if (ret)
4930 			goto out;
4931 	}
4932 
4933 	if (opts.noabs)
4934 		warnings += check_abs_references(file);
4935 
4936 	if (opts.orc && nr_insns) {
4937 		ret = orc_create(file);
4938 		if (ret)
4939 			goto out;
4940 	}
4941 
4942 	if (opts.stats) {
4943 		printf("nr_insns_visited: %ld\n", nr_insns_visited);
4944 		printf("nr_cfi: %ld\n", nr_cfi);
4945 		printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
4946 		printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
4947 	}
4948 
4949 out:
4950 	if (ret || warnings) {
4951 		if (opts.werror && warnings)
4952 			ret = 1;
4953 
4954 		if (opts.verbose) {
4955 			if (opts.werror && warnings)
4956 				WARN("%d warning(s) upgraded to errors", warnings);
4957 			disas_warned_funcs(disas_ctx);
4958 		}
4959 	}
4960 
4961 	if (opts.disas)
4962 		disas_funcs(disas_ctx);
4963 
4964 	if (disas_ctx) {
4965 		disas_context_destroy(disas_ctx);
4966 		objtool_disas_ctx = NULL;
4967 	}
4968 
4969 	free_insns(file);
4970 
4971 	if (!ret && !warnings)
4972 		return 0;
4973 
4974 	if (opts.backup && make_backup())
4975 		return 1;
4976 
4977 	return ret;
4978 }
4979