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