xref: /linux-6.15/tools/objtool/check.c (revision c8ea0d67)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015-2017 Josh Poimboeuf <[email protected]>
4  */
5 
6 #include <string.h>
7 #include <stdlib.h>
8 
9 #include "builtin.h"
10 #include "cfi.h"
11 #include "arch.h"
12 #include "check.h"
13 #include "special.h"
14 #include "warn.h"
15 #include "arch_elf.h"
16 
17 #include <linux/hashtable.h>
18 #include <linux/kernel.h>
19 #include <linux/static_call_types.h>
20 
21 #define FAKE_JUMP_OFFSET -1
22 
23 #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table"
24 
25 struct alternative {
26 	struct list_head list;
27 	struct instruction *insn;
28 	bool skip_orig;
29 };
30 
31 struct cfi_init_state initial_func_cfi;
32 
33 struct instruction *find_insn(struct objtool_file *file,
34 			      struct section *sec, unsigned long offset)
35 {
36 	struct instruction *insn;
37 
38 	hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
39 		if (insn->sec == sec && insn->offset == offset)
40 			return insn;
41 	}
42 
43 	return NULL;
44 }
45 
46 static struct instruction *next_insn_same_sec(struct objtool_file *file,
47 					      struct instruction *insn)
48 {
49 	struct instruction *next = list_next_entry(insn, list);
50 
51 	if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
52 		return NULL;
53 
54 	return next;
55 }
56 
57 static struct instruction *next_insn_same_func(struct objtool_file *file,
58 					       struct instruction *insn)
59 {
60 	struct instruction *next = list_next_entry(insn, list);
61 	struct symbol *func = insn->func;
62 
63 	if (!func)
64 		return NULL;
65 
66 	if (&next->list != &file->insn_list && next->func == func)
67 		return next;
68 
69 	/* Check if we're already in the subfunction: */
70 	if (func == func->cfunc)
71 		return NULL;
72 
73 	/* Move to the subfunction: */
74 	return find_insn(file, func->cfunc->sec, func->cfunc->offset);
75 }
76 
77 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
78 					       struct instruction *insn)
79 {
80 	struct instruction *prev = list_prev_entry(insn, list);
81 
82 	if (&prev->list != &file->insn_list && prev->func == insn->func)
83 		return prev;
84 
85 	return NULL;
86 }
87 
88 #define func_for_each_insn(file, func, insn)				\
89 	for (insn = find_insn(file, func->sec, func->offset);		\
90 	     insn;							\
91 	     insn = next_insn_same_func(file, insn))
92 
93 #define sym_for_each_insn(file, sym, insn)				\
94 	for (insn = find_insn(file, sym->sec, sym->offset);		\
95 	     insn && &insn->list != &file->insn_list &&			\
96 		insn->sec == sym->sec &&				\
97 		insn->offset < sym->offset + sym->len;			\
98 	     insn = list_next_entry(insn, list))
99 
100 #define sym_for_each_insn_continue_reverse(file, sym, insn)		\
101 	for (insn = list_prev_entry(insn, list);			\
102 	     &insn->list != &file->insn_list &&				\
103 		insn->sec == sym->sec && insn->offset >= sym->offset;	\
104 	     insn = list_prev_entry(insn, list))
105 
106 #define sec_for_each_insn_from(file, insn)				\
107 	for (; insn; insn = next_insn_same_sec(file, insn))
108 
109 #define sec_for_each_insn_continue(file, insn)				\
110 	for (insn = next_insn_same_sec(file, insn); insn;		\
111 	     insn = next_insn_same_sec(file, insn))
112 
113 static bool is_static_jump(struct instruction *insn)
114 {
115 	return insn->type == INSN_JUMP_CONDITIONAL ||
116 	       insn->type == INSN_JUMP_UNCONDITIONAL;
117 }
118 
119 static bool is_sibling_call(struct instruction *insn)
120 {
121 	/* An indirect jump is either a sibling call or a jump to a table. */
122 	if (insn->type == INSN_JUMP_DYNAMIC)
123 		return list_empty(&insn->alts);
124 
125 	if (!is_static_jump(insn))
126 		return false;
127 
128 	/* add_jump_destinations() sets insn->call_dest for sibling calls. */
129 	return !!insn->call_dest;
130 }
131 
132 /*
133  * This checks to see if the given function is a "noreturn" function.
134  *
135  * For global functions which are outside the scope of this object file, we
136  * have to keep a manual list of them.
137  *
138  * For local functions, we have to detect them manually by simply looking for
139  * the lack of a return instruction.
140  */
141 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
142 				int recursion)
143 {
144 	int i;
145 	struct instruction *insn;
146 	bool empty = true;
147 
148 	/*
149 	 * Unfortunately these have to be hard coded because the noreturn
150 	 * attribute isn't provided in ELF data.
151 	 */
152 	static const char * const global_noreturns[] = {
153 		"__stack_chk_fail",
154 		"panic",
155 		"do_exit",
156 		"do_task_dead",
157 		"__module_put_and_exit",
158 		"complete_and_exit",
159 		"__reiserfs_panic",
160 		"lbug_with_loc",
161 		"fortify_panic",
162 		"usercopy_abort",
163 		"machine_real_restart",
164 		"rewind_stack_do_exit",
165 		"kunit_try_catch_throw",
166 	};
167 
168 	if (!func)
169 		return false;
170 
171 	if (func->bind == STB_WEAK)
172 		return false;
173 
174 	if (func->bind == STB_GLOBAL)
175 		for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
176 			if (!strcmp(func->name, global_noreturns[i]))
177 				return true;
178 
179 	if (!func->len)
180 		return false;
181 
182 	insn = find_insn(file, func->sec, func->offset);
183 	if (!insn->func)
184 		return false;
185 
186 	func_for_each_insn(file, func, insn) {
187 		empty = false;
188 
189 		if (insn->type == INSN_RETURN)
190 			return false;
191 	}
192 
193 	if (empty)
194 		return false;
195 
196 	/*
197 	 * A function can have a sibling call instead of a return.  In that
198 	 * case, the function's dead-end status depends on whether the target
199 	 * of the sibling call returns.
200 	 */
201 	func_for_each_insn(file, func, insn) {
202 		if (is_sibling_call(insn)) {
203 			struct instruction *dest = insn->jump_dest;
204 
205 			if (!dest)
206 				/* sibling call to another file */
207 				return false;
208 
209 			/* local sibling call */
210 			if (recursion == 5) {
211 				/*
212 				 * Infinite recursion: two functions have
213 				 * sibling calls to each other.  This is a very
214 				 * rare case.  It means they aren't dead ends.
215 				 */
216 				return false;
217 			}
218 
219 			return __dead_end_function(file, dest->func, recursion+1);
220 		}
221 	}
222 
223 	return true;
224 }
225 
226 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
227 {
228 	return __dead_end_function(file, func, 0);
229 }
230 
231 static void init_cfi_state(struct cfi_state *cfi)
232 {
233 	int i;
234 
235 	for (i = 0; i < CFI_NUM_REGS; i++) {
236 		cfi->regs[i].base = CFI_UNDEFINED;
237 		cfi->vals[i].base = CFI_UNDEFINED;
238 	}
239 	cfi->cfa.base = CFI_UNDEFINED;
240 	cfi->drap_reg = CFI_UNDEFINED;
241 	cfi->drap_offset = -1;
242 }
243 
244 static void init_insn_state(struct insn_state *state, struct section *sec)
245 {
246 	memset(state, 0, sizeof(*state));
247 	init_cfi_state(&state->cfi);
248 
249 	/*
250 	 * We need the full vmlinux for noinstr validation, otherwise we can
251 	 * not correctly determine insn->call_dest->sec (external symbols do
252 	 * not have a section).
253 	 */
254 	if (vmlinux && sec)
255 		state->noinstr = sec->noinstr;
256 }
257 
258 /*
259  * Call the arch-specific instruction decoder for all the instructions and add
260  * them to the global instruction list.
261  */
262 static int decode_instructions(struct objtool_file *file)
263 {
264 	struct section *sec;
265 	struct symbol *func;
266 	unsigned long offset;
267 	struct instruction *insn;
268 	unsigned long nr_insns = 0;
269 	int ret;
270 
271 	for_each_sec(file, sec) {
272 
273 		if (!(sec->sh.sh_flags & SHF_EXECINSTR))
274 			continue;
275 
276 		if (strcmp(sec->name, ".altinstr_replacement") &&
277 		    strcmp(sec->name, ".altinstr_aux") &&
278 		    strncmp(sec->name, ".discard.", 9))
279 			sec->text = true;
280 
281 		if (!strcmp(sec->name, ".noinstr.text") ||
282 		    !strcmp(sec->name, ".entry.text"))
283 			sec->noinstr = true;
284 
285 		for (offset = 0; offset < sec->len; offset += insn->len) {
286 			insn = malloc(sizeof(*insn));
287 			if (!insn) {
288 				WARN("malloc failed");
289 				return -1;
290 			}
291 			memset(insn, 0, sizeof(*insn));
292 			INIT_LIST_HEAD(&insn->alts);
293 			INIT_LIST_HEAD(&insn->stack_ops);
294 			init_cfi_state(&insn->cfi);
295 
296 			insn->sec = sec;
297 			insn->offset = offset;
298 
299 			ret = arch_decode_instruction(file->elf, sec, offset,
300 						      sec->len - offset,
301 						      &insn->len, &insn->type,
302 						      &insn->immediate,
303 						      &insn->stack_ops);
304 			if (ret)
305 				goto err;
306 
307 			hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
308 			list_add_tail(&insn->list, &file->insn_list);
309 			nr_insns++;
310 		}
311 
312 		list_for_each_entry(func, &sec->symbol_list, list) {
313 			if (func->type != STT_FUNC || func->alias != func)
314 				continue;
315 
316 			if (!find_insn(file, sec, func->offset)) {
317 				WARN("%s(): can't find starting instruction",
318 				     func->name);
319 				return -1;
320 			}
321 
322 			sym_for_each_insn(file, func, insn)
323 				insn->func = func;
324 		}
325 	}
326 
327 	if (stats)
328 		printf("nr_insns: %lu\n", nr_insns);
329 
330 	return 0;
331 
332 err:
333 	free(insn);
334 	return ret;
335 }
336 
337 static struct instruction *find_last_insn(struct objtool_file *file,
338 					  struct section *sec)
339 {
340 	struct instruction *insn = NULL;
341 	unsigned int offset;
342 	unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
343 
344 	for (offset = sec->len - 1; offset >= end && !insn; offset--)
345 		insn = find_insn(file, sec, offset);
346 
347 	return insn;
348 }
349 
350 /*
351  * Mark "ud2" instructions and manually annotated dead ends.
352  */
353 static int add_dead_ends(struct objtool_file *file)
354 {
355 	struct section *sec;
356 	struct reloc *reloc;
357 	struct instruction *insn;
358 
359 	/*
360 	 * By default, "ud2" is a dead end unless otherwise annotated, because
361 	 * GCC 7 inserts it for certain divide-by-zero cases.
362 	 */
363 	for_each_insn(file, insn)
364 		if (insn->type == INSN_BUG)
365 			insn->dead_end = true;
366 
367 	/*
368 	 * Check for manually annotated dead ends.
369 	 */
370 	sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
371 	if (!sec)
372 		goto reachable;
373 
374 	list_for_each_entry(reloc, &sec->reloc_list, list) {
375 		if (reloc->sym->type != STT_SECTION) {
376 			WARN("unexpected relocation symbol type in %s", sec->name);
377 			return -1;
378 		}
379 		insn = find_insn(file, reloc->sym->sec, reloc->addend);
380 		if (insn)
381 			insn = list_prev_entry(insn, list);
382 		else if (reloc->addend == reloc->sym->sec->len) {
383 			insn = find_last_insn(file, reloc->sym->sec);
384 			if (!insn) {
385 				WARN("can't find unreachable insn at %s+0x%x",
386 				     reloc->sym->sec->name, reloc->addend);
387 				return -1;
388 			}
389 		} else {
390 			WARN("can't find unreachable insn at %s+0x%x",
391 			     reloc->sym->sec->name, reloc->addend);
392 			return -1;
393 		}
394 
395 		insn->dead_end = true;
396 	}
397 
398 reachable:
399 	/*
400 	 * These manually annotated reachable checks are needed for GCC 4.4,
401 	 * where the Linux unreachable() macro isn't supported.  In that case
402 	 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
403 	 * not a dead end.
404 	 */
405 	sec = find_section_by_name(file->elf, ".rela.discard.reachable");
406 	if (!sec)
407 		return 0;
408 
409 	list_for_each_entry(reloc, &sec->reloc_list, list) {
410 		if (reloc->sym->type != STT_SECTION) {
411 			WARN("unexpected relocation symbol type in %s", sec->name);
412 			return -1;
413 		}
414 		insn = find_insn(file, reloc->sym->sec, reloc->addend);
415 		if (insn)
416 			insn = list_prev_entry(insn, list);
417 		else if (reloc->addend == reloc->sym->sec->len) {
418 			insn = find_last_insn(file, reloc->sym->sec);
419 			if (!insn) {
420 				WARN("can't find reachable insn at %s+0x%x",
421 				     reloc->sym->sec->name, reloc->addend);
422 				return -1;
423 			}
424 		} else {
425 			WARN("can't find reachable insn at %s+0x%x",
426 			     reloc->sym->sec->name, reloc->addend);
427 			return -1;
428 		}
429 
430 		insn->dead_end = false;
431 	}
432 
433 	return 0;
434 }
435 
436 static int create_static_call_sections(struct objtool_file *file)
437 {
438 	struct section *sec, *reloc_sec;
439 	struct reloc *reloc;
440 	struct static_call_site *site;
441 	struct instruction *insn;
442 	struct symbol *key_sym;
443 	char *key_name, *tmp;
444 	int idx;
445 
446 	sec = find_section_by_name(file->elf, ".static_call_sites");
447 	if (sec) {
448 		INIT_LIST_HEAD(&file->static_call_list);
449 		WARN("file already has .static_call_sites section, skipping");
450 		return 0;
451 	}
452 
453 	if (list_empty(&file->static_call_list))
454 		return 0;
455 
456 	idx = 0;
457 	list_for_each_entry(insn, &file->static_call_list, static_call_node)
458 		idx++;
459 
460 	sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
461 				 sizeof(struct static_call_site), idx);
462 	if (!sec)
463 		return -1;
464 
465 	reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
466 	if (!reloc_sec)
467 		return -1;
468 
469 	idx = 0;
470 	list_for_each_entry(insn, &file->static_call_list, static_call_node) {
471 
472 		site = (struct static_call_site *)sec->data->d_buf + idx;
473 		memset(site, 0, sizeof(struct static_call_site));
474 
475 		/* populate reloc for 'addr' */
476 		reloc = malloc(sizeof(*reloc));
477 		if (!reloc) {
478 			perror("malloc");
479 			return -1;
480 		}
481 		memset(reloc, 0, sizeof(*reloc));
482 		reloc->sym = insn->sec->sym;
483 		reloc->addend = insn->offset;
484 		reloc->type = R_X86_64_PC32;
485 		reloc->offset = idx * sizeof(struct static_call_site);
486 		reloc->sec = reloc_sec;
487 		elf_add_reloc(file->elf, reloc);
488 
489 		/* find key symbol */
490 		key_name = strdup(insn->call_dest->name);
491 		if (!key_name) {
492 			perror("strdup");
493 			return -1;
494 		}
495 		if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
496 			    STATIC_CALL_TRAMP_PREFIX_LEN)) {
497 			WARN("static_call: trampoline name malformed: %s", key_name);
498 			return -1;
499 		}
500 		tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
501 		memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
502 
503 		key_sym = find_symbol_by_name(file->elf, tmp);
504 		if (!key_sym) {
505 			WARN("static_call: can't find static_call_key symbol: %s", tmp);
506 			return -1;
507 		}
508 		free(key_name);
509 
510 		/* populate reloc for 'key' */
511 		reloc = malloc(sizeof(*reloc));
512 		if (!reloc) {
513 			perror("malloc");
514 			return -1;
515 		}
516 		memset(reloc, 0, sizeof(*reloc));
517 		reloc->sym = key_sym;
518 		reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
519 		reloc->type = R_X86_64_PC32;
520 		reloc->offset = idx * sizeof(struct static_call_site) + 4;
521 		reloc->sec = reloc_sec;
522 		elf_add_reloc(file->elf, reloc);
523 
524 		idx++;
525 	}
526 
527 	if (elf_rebuild_reloc_section(file->elf, reloc_sec))
528 		return -1;
529 
530 	return 0;
531 }
532 
533 /*
534  * Warnings shouldn't be reported for ignored functions.
535  */
536 static void add_ignores(struct objtool_file *file)
537 {
538 	struct instruction *insn;
539 	struct section *sec;
540 	struct symbol *func;
541 	struct reloc *reloc;
542 
543 	sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
544 	if (!sec)
545 		return;
546 
547 	list_for_each_entry(reloc, &sec->reloc_list, list) {
548 		switch (reloc->sym->type) {
549 		case STT_FUNC:
550 			func = reloc->sym;
551 			break;
552 
553 		case STT_SECTION:
554 			func = find_func_by_offset(reloc->sym->sec, reloc->addend);
555 			if (!func)
556 				continue;
557 			break;
558 
559 		default:
560 			WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
561 			continue;
562 		}
563 
564 		func_for_each_insn(file, func, insn)
565 			insn->ignore = true;
566 	}
567 }
568 
569 /*
570  * This is a whitelist of functions that is allowed to be called with AC set.
571  * The list is meant to be minimal and only contains compiler instrumentation
572  * ABI and a few functions used to implement *_{to,from}_user() functions.
573  *
574  * These functions must not directly change AC, but may PUSHF/POPF.
575  */
576 static const char *uaccess_safe_builtin[] = {
577 	/* KASAN */
578 	"kasan_report",
579 	"check_memory_region",
580 	/* KASAN out-of-line */
581 	"__asan_loadN_noabort",
582 	"__asan_load1_noabort",
583 	"__asan_load2_noabort",
584 	"__asan_load4_noabort",
585 	"__asan_load8_noabort",
586 	"__asan_load16_noabort",
587 	"__asan_storeN_noabort",
588 	"__asan_store1_noabort",
589 	"__asan_store2_noabort",
590 	"__asan_store4_noabort",
591 	"__asan_store8_noabort",
592 	"__asan_store16_noabort",
593 	/* KASAN in-line */
594 	"__asan_report_load_n_noabort",
595 	"__asan_report_load1_noabort",
596 	"__asan_report_load2_noabort",
597 	"__asan_report_load4_noabort",
598 	"__asan_report_load8_noabort",
599 	"__asan_report_load16_noabort",
600 	"__asan_report_store_n_noabort",
601 	"__asan_report_store1_noabort",
602 	"__asan_report_store2_noabort",
603 	"__asan_report_store4_noabort",
604 	"__asan_report_store8_noabort",
605 	"__asan_report_store16_noabort",
606 	/* KCSAN */
607 	"__kcsan_check_access",
608 	"kcsan_found_watchpoint",
609 	"kcsan_setup_watchpoint",
610 	"kcsan_check_scoped_accesses",
611 	"kcsan_disable_current",
612 	"kcsan_enable_current_nowarn",
613 	/* KCSAN/TSAN */
614 	"__tsan_func_entry",
615 	"__tsan_func_exit",
616 	"__tsan_read_range",
617 	"__tsan_write_range",
618 	"__tsan_read1",
619 	"__tsan_read2",
620 	"__tsan_read4",
621 	"__tsan_read8",
622 	"__tsan_read16",
623 	"__tsan_write1",
624 	"__tsan_write2",
625 	"__tsan_write4",
626 	"__tsan_write8",
627 	"__tsan_write16",
628 	/* KCOV */
629 	"write_comp_data",
630 	"check_kcov_mode",
631 	"__sanitizer_cov_trace_pc",
632 	"__sanitizer_cov_trace_const_cmp1",
633 	"__sanitizer_cov_trace_const_cmp2",
634 	"__sanitizer_cov_trace_const_cmp4",
635 	"__sanitizer_cov_trace_const_cmp8",
636 	"__sanitizer_cov_trace_cmp1",
637 	"__sanitizer_cov_trace_cmp2",
638 	"__sanitizer_cov_trace_cmp4",
639 	"__sanitizer_cov_trace_cmp8",
640 	"__sanitizer_cov_trace_switch",
641 	/* UBSAN */
642 	"ubsan_type_mismatch_common",
643 	"__ubsan_handle_type_mismatch",
644 	"__ubsan_handle_type_mismatch_v1",
645 	"__ubsan_handle_shift_out_of_bounds",
646 	/* misc */
647 	"csum_partial_copy_generic",
648 	"__memcpy_mcsafe",
649 	"mcsafe_handle_tail",
650 	"ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
651 	NULL
652 };
653 
654 static void add_uaccess_safe(struct objtool_file *file)
655 {
656 	struct symbol *func;
657 	const char **name;
658 
659 	if (!uaccess)
660 		return;
661 
662 	for (name = uaccess_safe_builtin; *name; name++) {
663 		func = find_symbol_by_name(file->elf, *name);
664 		if (!func)
665 			continue;
666 
667 		func->uaccess_safe = true;
668 	}
669 }
670 
671 /*
672  * FIXME: For now, just ignore any alternatives which add retpolines.  This is
673  * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
674  * But it at least allows objtool to understand the control flow *around* the
675  * retpoline.
676  */
677 static int add_ignore_alternatives(struct objtool_file *file)
678 {
679 	struct section *sec;
680 	struct reloc *reloc;
681 	struct instruction *insn;
682 
683 	sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
684 	if (!sec)
685 		return 0;
686 
687 	list_for_each_entry(reloc, &sec->reloc_list, list) {
688 		if (reloc->sym->type != STT_SECTION) {
689 			WARN("unexpected relocation symbol type in %s", sec->name);
690 			return -1;
691 		}
692 
693 		insn = find_insn(file, reloc->sym->sec, reloc->addend);
694 		if (!insn) {
695 			WARN("bad .discard.ignore_alts entry");
696 			return -1;
697 		}
698 
699 		insn->ignore_alts = true;
700 	}
701 
702 	return 0;
703 }
704 
705 /*
706  * Find the destination instructions for all jumps.
707  */
708 static int add_jump_destinations(struct objtool_file *file)
709 {
710 	struct instruction *insn;
711 	struct reloc *reloc;
712 	struct section *dest_sec;
713 	unsigned long dest_off;
714 
715 	for_each_insn(file, insn) {
716 		if (!is_static_jump(insn))
717 			continue;
718 
719 		if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
720 			continue;
721 
722 		reloc = find_reloc_by_dest_range(file->elf, insn->sec,
723 					       insn->offset, insn->len);
724 		if (!reloc) {
725 			dest_sec = insn->sec;
726 			dest_off = arch_jump_destination(insn);
727 		} else if (reloc->sym->type == STT_SECTION) {
728 			dest_sec = reloc->sym->sec;
729 			dest_off = arch_dest_reloc_offset(reloc->addend);
730 		} else if (reloc->sym->sec->idx) {
731 			dest_sec = reloc->sym->sec;
732 			dest_off = reloc->sym->sym.st_value +
733 				   arch_dest_reloc_offset(reloc->addend);
734 		} else if (strstr(reloc->sym->name, "_indirect_thunk_")) {
735 			/*
736 			 * Retpoline jumps are really dynamic jumps in
737 			 * disguise, so convert them accordingly.
738 			 */
739 			if (insn->type == INSN_JUMP_UNCONDITIONAL)
740 				insn->type = INSN_JUMP_DYNAMIC;
741 			else
742 				insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
743 
744 			insn->retpoline_safe = true;
745 			continue;
746 		} else {
747 			/* external sibling call */
748 			insn->call_dest = reloc->sym;
749 			if (insn->call_dest->static_call_tramp) {
750 				list_add_tail(&insn->static_call_node,
751 					      &file->static_call_list);
752 			}
753 			continue;
754 		}
755 
756 		insn->jump_dest = find_insn(file, dest_sec, dest_off);
757 		if (!insn->jump_dest) {
758 
759 			/*
760 			 * This is a special case where an alt instruction
761 			 * jumps past the end of the section.  These are
762 			 * handled later in handle_group_alt().
763 			 */
764 			if (!strcmp(insn->sec->name, ".altinstr_replacement"))
765 				continue;
766 
767 			WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
768 				  insn->sec, insn->offset, dest_sec->name,
769 				  dest_off);
770 			return -1;
771 		}
772 
773 		/*
774 		 * Cross-function jump.
775 		 */
776 		if (insn->func && insn->jump_dest->func &&
777 		    insn->func != insn->jump_dest->func) {
778 
779 			/*
780 			 * For GCC 8+, create parent/child links for any cold
781 			 * subfunctions.  This is _mostly_ redundant with a
782 			 * similar initialization in read_symbols().
783 			 *
784 			 * If a function has aliases, we want the *first* such
785 			 * function in the symbol table to be the subfunction's
786 			 * parent.  In that case we overwrite the
787 			 * initialization done in read_symbols().
788 			 *
789 			 * However this code can't completely replace the
790 			 * read_symbols() code because this doesn't detect the
791 			 * case where the parent function's only reference to a
792 			 * subfunction is through a jump table.
793 			 */
794 			if (!strstr(insn->func->name, ".cold.") &&
795 			    strstr(insn->jump_dest->func->name, ".cold.")) {
796 				insn->func->cfunc = insn->jump_dest->func;
797 				insn->jump_dest->func->pfunc = insn->func;
798 
799 			} else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
800 				   insn->jump_dest->offset == insn->jump_dest->func->offset) {
801 
802 				/* internal sibling call */
803 				insn->call_dest = insn->jump_dest->func;
804 				if (insn->call_dest->static_call_tramp) {
805 					list_add_tail(&insn->static_call_node,
806 						      &file->static_call_list);
807 				}
808 			}
809 		}
810 	}
811 
812 	return 0;
813 }
814 
815 static void remove_insn_ops(struct instruction *insn)
816 {
817 	struct stack_op *op, *tmp;
818 
819 	list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
820 		list_del(&op->list);
821 		free(op);
822 	}
823 }
824 
825 /*
826  * Find the destination instructions for all calls.
827  */
828 static int add_call_destinations(struct objtool_file *file)
829 {
830 	struct instruction *insn;
831 	unsigned long dest_off;
832 	struct reloc *reloc;
833 
834 	for_each_insn(file, insn) {
835 		if (insn->type != INSN_CALL)
836 			continue;
837 
838 		reloc = find_reloc_by_dest_range(file->elf, insn->sec,
839 					       insn->offset, insn->len);
840 		if (!reloc) {
841 			dest_off = arch_jump_destination(insn);
842 			insn->call_dest = find_func_by_offset(insn->sec, dest_off);
843 			if (!insn->call_dest)
844 				insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
845 
846 			if (insn->ignore)
847 				continue;
848 
849 			if (!insn->call_dest) {
850 				WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
851 				return -1;
852 			}
853 
854 			if (insn->func && insn->call_dest->type != STT_FUNC) {
855 				WARN_FUNC("unsupported call to non-function",
856 					  insn->sec, insn->offset);
857 				return -1;
858 			}
859 
860 		} else if (reloc->sym->type == STT_SECTION) {
861 			dest_off = arch_dest_reloc_offset(reloc->addend);
862 			insn->call_dest = find_func_by_offset(reloc->sym->sec,
863 							      dest_off);
864 			if (!insn->call_dest) {
865 				WARN_FUNC("can't find call dest symbol at %s+0x%lx",
866 					  insn->sec, insn->offset,
867 					  reloc->sym->sec->name,
868 					  dest_off);
869 				return -1;
870 			}
871 		} else
872 			insn->call_dest = reloc->sym;
873 
874 		/*
875 		 * Many compilers cannot disable KCOV with a function attribute
876 		 * so they need a little help, NOP out any KCOV calls from noinstr
877 		 * text.
878 		 */
879 		if (insn->sec->noinstr &&
880 		    !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
881 			if (reloc) {
882 				reloc->type = R_NONE;
883 				elf_write_reloc(file->elf, reloc);
884 			}
885 
886 			elf_write_insn(file->elf, insn->sec,
887 				       insn->offset, insn->len,
888 				       arch_nop_insn(insn->len));
889 			insn->type = INSN_NOP;
890 		}
891 
892 		/*
893 		 * Whatever stack impact regular CALLs have, should be undone
894 		 * by the RETURN of the called function.
895 		 *
896 		 * Annotated intra-function calls retain the stack_ops but
897 		 * are converted to JUMP, see read_intra_function_calls().
898 		 */
899 		remove_insn_ops(insn);
900 	}
901 
902 	return 0;
903 }
904 
905 /*
906  * The .alternatives section requires some extra special care, over and above
907  * what other special sections require:
908  *
909  * 1. Because alternatives are patched in-place, we need to insert a fake jump
910  *    instruction at the end so that validate_branch() skips all the original
911  *    replaced instructions when validating the new instruction path.
912  *
913  * 2. An added wrinkle is that the new instruction length might be zero.  In
914  *    that case the old instructions are replaced with noops.  We simulate that
915  *    by creating a fake jump as the only new instruction.
916  *
917  * 3. In some cases, the alternative section includes an instruction which
918  *    conditionally jumps to the _end_ of the entry.  We have to modify these
919  *    jumps' destinations to point back to .text rather than the end of the
920  *    entry in .altinstr_replacement.
921  */
922 static int handle_group_alt(struct objtool_file *file,
923 			    struct special_alt *special_alt,
924 			    struct instruction *orig_insn,
925 			    struct instruction **new_insn)
926 {
927 	static unsigned int alt_group_next_index = 1;
928 	struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
929 	unsigned int alt_group = alt_group_next_index++;
930 	unsigned long dest_off;
931 
932 	last_orig_insn = NULL;
933 	insn = orig_insn;
934 	sec_for_each_insn_from(file, insn) {
935 		if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
936 			break;
937 
938 		insn->alt_group = alt_group;
939 		last_orig_insn = insn;
940 	}
941 
942 	if (next_insn_same_sec(file, last_orig_insn)) {
943 		fake_jump = malloc(sizeof(*fake_jump));
944 		if (!fake_jump) {
945 			WARN("malloc failed");
946 			return -1;
947 		}
948 		memset(fake_jump, 0, sizeof(*fake_jump));
949 		INIT_LIST_HEAD(&fake_jump->alts);
950 		INIT_LIST_HEAD(&fake_jump->stack_ops);
951 		init_cfi_state(&fake_jump->cfi);
952 
953 		fake_jump->sec = special_alt->new_sec;
954 		fake_jump->offset = FAKE_JUMP_OFFSET;
955 		fake_jump->type = INSN_JUMP_UNCONDITIONAL;
956 		fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
957 		fake_jump->func = orig_insn->func;
958 	}
959 
960 	if (!special_alt->new_len) {
961 		if (!fake_jump) {
962 			WARN("%s: empty alternative at end of section",
963 			     special_alt->orig_sec->name);
964 			return -1;
965 		}
966 
967 		*new_insn = fake_jump;
968 		return 0;
969 	}
970 
971 	last_new_insn = NULL;
972 	alt_group = alt_group_next_index++;
973 	insn = *new_insn;
974 	sec_for_each_insn_from(file, insn) {
975 		if (insn->offset >= special_alt->new_off + special_alt->new_len)
976 			break;
977 
978 		last_new_insn = insn;
979 
980 		insn->ignore = orig_insn->ignore_alts;
981 		insn->func = orig_insn->func;
982 		insn->alt_group = alt_group;
983 
984 		/*
985 		 * Since alternative replacement code is copy/pasted by the
986 		 * kernel after applying relocations, generally such code can't
987 		 * have relative-address relocation references to outside the
988 		 * .altinstr_replacement section, unless the arch's
989 		 * alternatives code can adjust the relative offsets
990 		 * accordingly.
991 		 *
992 		 * The x86 alternatives code adjusts the offsets only when it
993 		 * encounters a branch instruction at the very beginning of the
994 		 * replacement group.
995 		 */
996 		if ((insn->offset != special_alt->new_off ||
997 		    (insn->type != INSN_CALL && !is_static_jump(insn))) &&
998 		    find_reloc_by_dest_range(file->elf, insn->sec, insn->offset, insn->len)) {
999 
1000 			WARN_FUNC("unsupported relocation in alternatives section",
1001 				  insn->sec, insn->offset);
1002 			return -1;
1003 		}
1004 
1005 		if (!is_static_jump(insn))
1006 			continue;
1007 
1008 		if (!insn->immediate)
1009 			continue;
1010 
1011 		dest_off = arch_jump_destination(insn);
1012 		if (dest_off == special_alt->new_off + special_alt->new_len) {
1013 			if (!fake_jump) {
1014 				WARN("%s: alternative jump to end of section",
1015 				     special_alt->orig_sec->name);
1016 				return -1;
1017 			}
1018 			insn->jump_dest = fake_jump;
1019 		}
1020 
1021 		if (!insn->jump_dest) {
1022 			WARN_FUNC("can't find alternative jump destination",
1023 				  insn->sec, insn->offset);
1024 			return -1;
1025 		}
1026 	}
1027 
1028 	if (!last_new_insn) {
1029 		WARN_FUNC("can't find last new alternative instruction",
1030 			  special_alt->new_sec, special_alt->new_off);
1031 		return -1;
1032 	}
1033 
1034 	if (fake_jump)
1035 		list_add(&fake_jump->list, &last_new_insn->list);
1036 
1037 	return 0;
1038 }
1039 
1040 /*
1041  * A jump table entry can either convert a nop to a jump or a jump to a nop.
1042  * If the original instruction is a jump, make the alt entry an effective nop
1043  * by just skipping the original instruction.
1044  */
1045 static int handle_jump_alt(struct objtool_file *file,
1046 			   struct special_alt *special_alt,
1047 			   struct instruction *orig_insn,
1048 			   struct instruction **new_insn)
1049 {
1050 	if (orig_insn->type == INSN_NOP)
1051 		return 0;
1052 
1053 	if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1054 		WARN_FUNC("unsupported instruction at jump label",
1055 			  orig_insn->sec, orig_insn->offset);
1056 		return -1;
1057 	}
1058 
1059 	*new_insn = list_next_entry(orig_insn, list);
1060 	return 0;
1061 }
1062 
1063 /*
1064  * Read all the special sections which have alternate instructions which can be
1065  * patched in or redirected to at runtime.  Each instruction having alternate
1066  * instruction(s) has them added to its insn->alts list, which will be
1067  * traversed in validate_branch().
1068  */
1069 static int add_special_section_alts(struct objtool_file *file)
1070 {
1071 	struct list_head special_alts;
1072 	struct instruction *orig_insn, *new_insn;
1073 	struct special_alt *special_alt, *tmp;
1074 	struct alternative *alt;
1075 	int ret;
1076 
1077 	ret = special_get_alts(file->elf, &special_alts);
1078 	if (ret)
1079 		return ret;
1080 
1081 	list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1082 
1083 		orig_insn = find_insn(file, special_alt->orig_sec,
1084 				      special_alt->orig_off);
1085 		if (!orig_insn) {
1086 			WARN_FUNC("special: can't find orig instruction",
1087 				  special_alt->orig_sec, special_alt->orig_off);
1088 			ret = -1;
1089 			goto out;
1090 		}
1091 
1092 		new_insn = NULL;
1093 		if (!special_alt->group || special_alt->new_len) {
1094 			new_insn = find_insn(file, special_alt->new_sec,
1095 					     special_alt->new_off);
1096 			if (!new_insn) {
1097 				WARN_FUNC("special: can't find new instruction",
1098 					  special_alt->new_sec,
1099 					  special_alt->new_off);
1100 				ret = -1;
1101 				goto out;
1102 			}
1103 		}
1104 
1105 		if (special_alt->group) {
1106 			if (!special_alt->orig_len) {
1107 				WARN_FUNC("empty alternative entry",
1108 					  orig_insn->sec, orig_insn->offset);
1109 				continue;
1110 			}
1111 
1112 			ret = handle_group_alt(file, special_alt, orig_insn,
1113 					       &new_insn);
1114 			if (ret)
1115 				goto out;
1116 		} else if (special_alt->jump_or_nop) {
1117 			ret = handle_jump_alt(file, special_alt, orig_insn,
1118 					      &new_insn);
1119 			if (ret)
1120 				goto out;
1121 		}
1122 
1123 		alt = malloc(sizeof(*alt));
1124 		if (!alt) {
1125 			WARN("malloc failed");
1126 			ret = -1;
1127 			goto out;
1128 		}
1129 
1130 		alt->insn = new_insn;
1131 		alt->skip_orig = special_alt->skip_orig;
1132 		orig_insn->ignore_alts |= special_alt->skip_alt;
1133 		list_add_tail(&alt->list, &orig_insn->alts);
1134 
1135 		list_del(&special_alt->list);
1136 		free(special_alt);
1137 	}
1138 
1139 out:
1140 	return ret;
1141 }
1142 
1143 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1144 			    struct reloc *table)
1145 {
1146 	struct reloc *reloc = table;
1147 	struct instruction *dest_insn;
1148 	struct alternative *alt;
1149 	struct symbol *pfunc = insn->func->pfunc;
1150 	unsigned int prev_offset = 0;
1151 
1152 	/*
1153 	 * Each @reloc is a switch table relocation which points to the target
1154 	 * instruction.
1155 	 */
1156 	list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1157 
1158 		/* Check for the end of the table: */
1159 		if (reloc != table && reloc->jump_table_start)
1160 			break;
1161 
1162 		/* Make sure the table entries are consecutive: */
1163 		if (prev_offset && reloc->offset != prev_offset + 8)
1164 			break;
1165 
1166 		/* Detect function pointers from contiguous objects: */
1167 		if (reloc->sym->sec == pfunc->sec &&
1168 		    reloc->addend == pfunc->offset)
1169 			break;
1170 
1171 		dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1172 		if (!dest_insn)
1173 			break;
1174 
1175 		/* Make sure the destination is in the same function: */
1176 		if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1177 			break;
1178 
1179 		alt = malloc(sizeof(*alt));
1180 		if (!alt) {
1181 			WARN("malloc failed");
1182 			return -1;
1183 		}
1184 
1185 		alt->insn = dest_insn;
1186 		list_add_tail(&alt->list, &insn->alts);
1187 		prev_offset = reloc->offset;
1188 	}
1189 
1190 	if (!prev_offset) {
1191 		WARN_FUNC("can't find switch jump table",
1192 			  insn->sec, insn->offset);
1193 		return -1;
1194 	}
1195 
1196 	return 0;
1197 }
1198 
1199 /*
1200  * find_jump_table() - Given a dynamic jump, find the switch jump table in
1201  * .rodata associated with it.
1202  *
1203  * There are 3 basic patterns:
1204  *
1205  * 1. jmpq *[rodata addr](,%reg,8)
1206  *
1207  *    This is the most common case by far.  It jumps to an address in a simple
1208  *    jump table which is stored in .rodata.
1209  *
1210  * 2. jmpq *[rodata addr](%rip)
1211  *
1212  *    This is caused by a rare GCC quirk, currently only seen in three driver
1213  *    functions in the kernel, only with certain obscure non-distro configs.
1214  *
1215  *    As part of an optimization, GCC makes a copy of an existing switch jump
1216  *    table, modifies it, and then hard-codes the jump (albeit with an indirect
1217  *    jump) to use a single entry in the table.  The rest of the jump table and
1218  *    some of its jump targets remain as dead code.
1219  *
1220  *    In such a case we can just crudely ignore all unreachable instruction
1221  *    warnings for the entire object file.  Ideally we would just ignore them
1222  *    for the function, but that would require redesigning the code quite a
1223  *    bit.  And honestly that's just not worth doing: unreachable instruction
1224  *    warnings are of questionable value anyway, and this is such a rare issue.
1225  *
1226  * 3. mov [rodata addr],%reg1
1227  *    ... some instructions ...
1228  *    jmpq *(%reg1,%reg2,8)
1229  *
1230  *    This is a fairly uncommon pattern which is new for GCC 6.  As of this
1231  *    writing, there are 11 occurrences of it in the allmodconfig kernel.
1232  *
1233  *    As of GCC 7 there are quite a few more of these and the 'in between' code
1234  *    is significant. Esp. with KASAN enabled some of the code between the mov
1235  *    and jmpq uses .rodata itself, which can confuse things.
1236  *
1237  *    TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1238  *    ensure the same register is used in the mov and jump instructions.
1239  *
1240  *    NOTE: RETPOLINE made it harder still to decode dynamic jumps.
1241  */
1242 static struct reloc *find_jump_table(struct objtool_file *file,
1243 				      struct symbol *func,
1244 				      struct instruction *insn)
1245 {
1246 	struct reloc *text_reloc, *table_reloc;
1247 	struct instruction *dest_insn, *orig_insn = insn;
1248 	struct section *table_sec;
1249 	unsigned long table_offset;
1250 
1251 	/*
1252 	 * Backward search using the @first_jump_src links, these help avoid
1253 	 * much of the 'in between' code. Which avoids us getting confused by
1254 	 * it.
1255 	 */
1256 	for (;
1257 	     insn && insn->func && insn->func->pfunc == func;
1258 	     insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1259 
1260 		if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1261 			break;
1262 
1263 		/* allow small jumps within the range */
1264 		if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1265 		    insn->jump_dest &&
1266 		    (insn->jump_dest->offset <= insn->offset ||
1267 		     insn->jump_dest->offset > orig_insn->offset))
1268 		    break;
1269 
1270 		/* look for a relocation which references .rodata */
1271 		text_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1272 						    insn->offset, insn->len);
1273 		if (!text_reloc || text_reloc->sym->type != STT_SECTION ||
1274 		    !text_reloc->sym->sec->rodata)
1275 			continue;
1276 
1277 		table_offset = text_reloc->addend;
1278 		table_sec = text_reloc->sym->sec;
1279 
1280 		if (text_reloc->type == R_X86_64_PC32)
1281 			table_offset += 4;
1282 
1283 		/*
1284 		 * Make sure the .rodata address isn't associated with a
1285 		 * symbol.  GCC jump tables are anonymous data.
1286 		 *
1287 		 * Also support C jump tables which are in the same format as
1288 		 * switch jump tables.  For objtool to recognize them, they
1289 		 * need to be placed in the C_JUMP_TABLE_SECTION section.  They
1290 		 * have symbols associated with them.
1291 		 */
1292 		if (find_symbol_containing(table_sec, table_offset) &&
1293 		    strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
1294 			continue;
1295 
1296 		/*
1297 		 * Each table entry has a reloc associated with it.  The reloc
1298 		 * should reference text in the same function as the original
1299 		 * instruction.
1300 		 */
1301 		table_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset);
1302 		if (!table_reloc)
1303 			continue;
1304 		dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1305 		if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1306 			continue;
1307 
1308 		/*
1309 		 * Use of RIP-relative switch jumps is quite rare, and
1310 		 * indicates a rare GCC quirk/bug which can leave dead code
1311 		 * behind.
1312 		 */
1313 		if (text_reloc->type == R_X86_64_PC32)
1314 			file->ignore_unreachables = true;
1315 
1316 		return table_reloc;
1317 	}
1318 
1319 	return NULL;
1320 }
1321 
1322 /*
1323  * First pass: Mark the head of each jump table so that in the next pass,
1324  * we know when a given jump table ends and the next one starts.
1325  */
1326 static void mark_func_jump_tables(struct objtool_file *file,
1327 				    struct symbol *func)
1328 {
1329 	struct instruction *insn, *last = NULL;
1330 	struct reloc *reloc;
1331 
1332 	func_for_each_insn(file, func, insn) {
1333 		if (!last)
1334 			last = insn;
1335 
1336 		/*
1337 		 * Store back-pointers for unconditional forward jumps such
1338 		 * that find_jump_table() can back-track using those and
1339 		 * avoid some potentially confusing code.
1340 		 */
1341 		if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1342 		    insn->offset > last->offset &&
1343 		    insn->jump_dest->offset > insn->offset &&
1344 		    !insn->jump_dest->first_jump_src) {
1345 
1346 			insn->jump_dest->first_jump_src = insn;
1347 			last = insn->jump_dest;
1348 		}
1349 
1350 		if (insn->type != INSN_JUMP_DYNAMIC)
1351 			continue;
1352 
1353 		reloc = find_jump_table(file, func, insn);
1354 		if (reloc) {
1355 			reloc->jump_table_start = true;
1356 			insn->jump_table = reloc;
1357 		}
1358 	}
1359 }
1360 
1361 static int add_func_jump_tables(struct objtool_file *file,
1362 				  struct symbol *func)
1363 {
1364 	struct instruction *insn;
1365 	int ret;
1366 
1367 	func_for_each_insn(file, func, insn) {
1368 		if (!insn->jump_table)
1369 			continue;
1370 
1371 		ret = add_jump_table(file, insn, insn->jump_table);
1372 		if (ret)
1373 			return ret;
1374 	}
1375 
1376 	return 0;
1377 }
1378 
1379 /*
1380  * For some switch statements, gcc generates a jump table in the .rodata
1381  * section which contains a list of addresses within the function to jump to.
1382  * This finds these jump tables and adds them to the insn->alts lists.
1383  */
1384 static int add_jump_table_alts(struct objtool_file *file)
1385 {
1386 	struct section *sec;
1387 	struct symbol *func;
1388 	int ret;
1389 
1390 	if (!file->rodata)
1391 		return 0;
1392 
1393 	for_each_sec(file, sec) {
1394 		list_for_each_entry(func, &sec->symbol_list, list) {
1395 			if (func->type != STT_FUNC)
1396 				continue;
1397 
1398 			mark_func_jump_tables(file, func);
1399 			ret = add_func_jump_tables(file, func);
1400 			if (ret)
1401 				return ret;
1402 		}
1403 	}
1404 
1405 	return 0;
1406 }
1407 
1408 static int read_unwind_hints(struct objtool_file *file)
1409 {
1410 	struct section *sec, *relocsec;
1411 	struct reloc *reloc;
1412 	struct unwind_hint *hint;
1413 	struct instruction *insn;
1414 	struct cfi_reg *cfa;
1415 	int i;
1416 
1417 	sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1418 	if (!sec)
1419 		return 0;
1420 
1421 	relocsec = sec->reloc;
1422 	if (!relocsec) {
1423 		WARN("missing .rela.discard.unwind_hints section");
1424 		return -1;
1425 	}
1426 
1427 	if (sec->len % sizeof(struct unwind_hint)) {
1428 		WARN("struct unwind_hint size mismatch");
1429 		return -1;
1430 	}
1431 
1432 	file->hints = true;
1433 
1434 	for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1435 		hint = (struct unwind_hint *)sec->data->d_buf + i;
1436 
1437 		reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1438 		if (!reloc) {
1439 			WARN("can't find reloc for unwind_hints[%d]", i);
1440 			return -1;
1441 		}
1442 
1443 		insn = find_insn(file, reloc->sym->sec, reloc->addend);
1444 		if (!insn) {
1445 			WARN("can't find insn for unwind_hints[%d]", i);
1446 			return -1;
1447 		}
1448 
1449 		cfa = &insn->cfi.cfa;
1450 
1451 		if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
1452 			insn->ret_offset = hint->sp_offset;
1453 			continue;
1454 		}
1455 
1456 		insn->hint = true;
1457 
1458 		switch (hint->sp_reg) {
1459 		case ORC_REG_UNDEFINED:
1460 			cfa->base = CFI_UNDEFINED;
1461 			break;
1462 		case ORC_REG_SP:
1463 			cfa->base = CFI_SP;
1464 			break;
1465 		case ORC_REG_BP:
1466 			cfa->base = CFI_BP;
1467 			break;
1468 		case ORC_REG_SP_INDIRECT:
1469 			cfa->base = CFI_SP_INDIRECT;
1470 			break;
1471 		case ORC_REG_R10:
1472 			cfa->base = CFI_R10;
1473 			break;
1474 		case ORC_REG_R13:
1475 			cfa->base = CFI_R13;
1476 			break;
1477 		case ORC_REG_DI:
1478 			cfa->base = CFI_DI;
1479 			break;
1480 		case ORC_REG_DX:
1481 			cfa->base = CFI_DX;
1482 			break;
1483 		default:
1484 			WARN_FUNC("unsupported unwind_hint sp base reg %d",
1485 				  insn->sec, insn->offset, hint->sp_reg);
1486 			return -1;
1487 		}
1488 
1489 		cfa->offset = hint->sp_offset;
1490 		insn->cfi.type = hint->type;
1491 		insn->cfi.end = hint->end;
1492 	}
1493 
1494 	return 0;
1495 }
1496 
1497 static int read_retpoline_hints(struct objtool_file *file)
1498 {
1499 	struct section *sec;
1500 	struct instruction *insn;
1501 	struct reloc *reloc;
1502 
1503 	sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1504 	if (!sec)
1505 		return 0;
1506 
1507 	list_for_each_entry(reloc, &sec->reloc_list, list) {
1508 		if (reloc->sym->type != STT_SECTION) {
1509 			WARN("unexpected relocation symbol type in %s", sec->name);
1510 			return -1;
1511 		}
1512 
1513 		insn = find_insn(file, reloc->sym->sec, reloc->addend);
1514 		if (!insn) {
1515 			WARN("bad .discard.retpoline_safe entry");
1516 			return -1;
1517 		}
1518 
1519 		if (insn->type != INSN_JUMP_DYNAMIC &&
1520 		    insn->type != INSN_CALL_DYNAMIC) {
1521 			WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1522 				  insn->sec, insn->offset);
1523 			return -1;
1524 		}
1525 
1526 		insn->retpoline_safe = true;
1527 	}
1528 
1529 	return 0;
1530 }
1531 
1532 static int read_instr_hints(struct objtool_file *file)
1533 {
1534 	struct section *sec;
1535 	struct instruction *insn;
1536 	struct reloc *reloc;
1537 
1538 	sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1539 	if (!sec)
1540 		return 0;
1541 
1542 	list_for_each_entry(reloc, &sec->reloc_list, list) {
1543 		if (reloc->sym->type != STT_SECTION) {
1544 			WARN("unexpected relocation symbol type in %s", sec->name);
1545 			return -1;
1546 		}
1547 
1548 		insn = find_insn(file, reloc->sym->sec, reloc->addend);
1549 		if (!insn) {
1550 			WARN("bad .discard.instr_end entry");
1551 			return -1;
1552 		}
1553 
1554 		insn->instr--;
1555 	}
1556 
1557 	sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1558 	if (!sec)
1559 		return 0;
1560 
1561 	list_for_each_entry(reloc, &sec->reloc_list, list) {
1562 		if (reloc->sym->type != STT_SECTION) {
1563 			WARN("unexpected relocation symbol type in %s", sec->name);
1564 			return -1;
1565 		}
1566 
1567 		insn = find_insn(file, reloc->sym->sec, reloc->addend);
1568 		if (!insn) {
1569 			WARN("bad .discard.instr_begin entry");
1570 			return -1;
1571 		}
1572 
1573 		insn->instr++;
1574 	}
1575 
1576 	return 0;
1577 }
1578 
1579 static int read_intra_function_calls(struct objtool_file *file)
1580 {
1581 	struct instruction *insn;
1582 	struct section *sec;
1583 	struct reloc *reloc;
1584 
1585 	sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1586 	if (!sec)
1587 		return 0;
1588 
1589 	list_for_each_entry(reloc, &sec->reloc_list, list) {
1590 		unsigned long dest_off;
1591 
1592 		if (reloc->sym->type != STT_SECTION) {
1593 			WARN("unexpected relocation symbol type in %s",
1594 			     sec->name);
1595 			return -1;
1596 		}
1597 
1598 		insn = find_insn(file, reloc->sym->sec, reloc->addend);
1599 		if (!insn) {
1600 			WARN("bad .discard.intra_function_call entry");
1601 			return -1;
1602 		}
1603 
1604 		if (insn->type != INSN_CALL) {
1605 			WARN_FUNC("intra_function_call not a direct call",
1606 				  insn->sec, insn->offset);
1607 			return -1;
1608 		}
1609 
1610 		/*
1611 		 * Treat intra-function CALLs as JMPs, but with a stack_op.
1612 		 * See add_call_destinations(), which strips stack_ops from
1613 		 * normal CALLs.
1614 		 */
1615 		insn->type = INSN_JUMP_UNCONDITIONAL;
1616 
1617 		dest_off = insn->offset + insn->len + insn->immediate;
1618 		insn->jump_dest = find_insn(file, insn->sec, dest_off);
1619 		if (!insn->jump_dest) {
1620 			WARN_FUNC("can't find call dest at %s+0x%lx",
1621 				  insn->sec, insn->offset,
1622 				  insn->sec->name, dest_off);
1623 			return -1;
1624 		}
1625 	}
1626 
1627 	return 0;
1628 }
1629 
1630 static int read_static_call_tramps(struct objtool_file *file)
1631 {
1632 	struct section *sec;
1633 	struct symbol *func;
1634 
1635 	for_each_sec(file, sec) {
1636 		list_for_each_entry(func, &sec->symbol_list, list) {
1637 			if (func->bind == STB_GLOBAL &&
1638 			    !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1639 				     strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1640 				func->static_call_tramp = true;
1641 		}
1642 	}
1643 
1644 	return 0;
1645 }
1646 
1647 static void mark_rodata(struct objtool_file *file)
1648 {
1649 	struct section *sec;
1650 	bool found = false;
1651 
1652 	/*
1653 	 * Search for the following rodata sections, each of which can
1654 	 * potentially contain jump tables:
1655 	 *
1656 	 * - .rodata: can contain GCC switch tables
1657 	 * - .rodata.<func>: same, if -fdata-sections is being used
1658 	 * - .rodata..c_jump_table: contains C annotated jump tables
1659 	 *
1660 	 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1661 	 */
1662 	for_each_sec(file, sec) {
1663 		if (!strncmp(sec->name, ".rodata", 7) &&
1664 		    !strstr(sec->name, ".str1.")) {
1665 			sec->rodata = true;
1666 			found = true;
1667 		}
1668 	}
1669 
1670 	file->rodata = found;
1671 }
1672 
1673 static int decode_sections(struct objtool_file *file)
1674 {
1675 	int ret;
1676 
1677 	mark_rodata(file);
1678 
1679 	ret = decode_instructions(file);
1680 	if (ret)
1681 		return ret;
1682 
1683 	ret = add_dead_ends(file);
1684 	if (ret)
1685 		return ret;
1686 
1687 	add_ignores(file);
1688 	add_uaccess_safe(file);
1689 
1690 	ret = add_ignore_alternatives(file);
1691 	if (ret)
1692 		return ret;
1693 
1694 	ret = read_static_call_tramps(file);
1695 	if (ret)
1696 		return ret;
1697 
1698 	ret = add_jump_destinations(file);
1699 	if (ret)
1700 		return ret;
1701 
1702 	ret = add_special_section_alts(file);
1703 	if (ret)
1704 		return ret;
1705 
1706 	ret = read_intra_function_calls(file);
1707 	if (ret)
1708 		return ret;
1709 
1710 	ret = add_call_destinations(file);
1711 	if (ret)
1712 		return ret;
1713 
1714 	ret = add_jump_table_alts(file);
1715 	if (ret)
1716 		return ret;
1717 
1718 	ret = read_unwind_hints(file);
1719 	if (ret)
1720 		return ret;
1721 
1722 	ret = read_retpoline_hints(file);
1723 	if (ret)
1724 		return ret;
1725 
1726 	ret = read_instr_hints(file);
1727 	if (ret)
1728 		return ret;
1729 
1730 	return 0;
1731 }
1732 
1733 static bool is_fentry_call(struct instruction *insn)
1734 {
1735 	if (insn->type == INSN_CALL && insn->call_dest &&
1736 	    insn->call_dest->type == STT_NOTYPE &&
1737 	    !strcmp(insn->call_dest->name, "__fentry__"))
1738 		return true;
1739 
1740 	return false;
1741 }
1742 
1743 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1744 {
1745 	u8 ret_offset = insn->ret_offset;
1746 	struct cfi_state *cfi = &state->cfi;
1747 	int i;
1748 
1749 	if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1750 		return true;
1751 
1752 	if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
1753 		return true;
1754 
1755 	if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
1756 		return true;
1757 
1758 	/*
1759 	 * If there is a ret offset hint then don't check registers
1760 	 * because a callee-saved register might have been pushed on
1761 	 * the stack.
1762 	 */
1763 	if (ret_offset)
1764 		return false;
1765 
1766 	for (i = 0; i < CFI_NUM_REGS; i++) {
1767 		if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1768 		    cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1769 			return true;
1770 	}
1771 
1772 	return false;
1773 }
1774 
1775 static bool has_valid_stack_frame(struct insn_state *state)
1776 {
1777 	struct cfi_state *cfi = &state->cfi;
1778 
1779 	if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
1780 	    cfi->regs[CFI_BP].offset == -16)
1781 		return true;
1782 
1783 	if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
1784 		return true;
1785 
1786 	return false;
1787 }
1788 
1789 static int update_cfi_state_regs(struct instruction *insn,
1790 				  struct cfi_state *cfi,
1791 				  struct stack_op *op)
1792 {
1793 	struct cfi_reg *cfa = &cfi->cfa;
1794 
1795 	if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1796 		return 0;
1797 
1798 	/* push */
1799 	if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1800 		cfa->offset += 8;
1801 
1802 	/* pop */
1803 	if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1804 		cfa->offset -= 8;
1805 
1806 	/* add immediate to sp */
1807 	if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1808 	    op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1809 		cfa->offset -= op->src.offset;
1810 
1811 	return 0;
1812 }
1813 
1814 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
1815 {
1816 	if (arch_callee_saved_reg(reg) &&
1817 	    cfi->regs[reg].base == CFI_UNDEFINED) {
1818 		cfi->regs[reg].base = base;
1819 		cfi->regs[reg].offset = offset;
1820 	}
1821 }
1822 
1823 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
1824 {
1825 	cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1826 	cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
1827 }
1828 
1829 /*
1830  * A note about DRAP stack alignment:
1831  *
1832  * GCC has the concept of a DRAP register, which is used to help keep track of
1833  * the stack pointer when aligning the stack.  r10 or r13 is used as the DRAP
1834  * register.  The typical DRAP pattern is:
1835  *
1836  *   4c 8d 54 24 08		lea    0x8(%rsp),%r10
1837  *   48 83 e4 c0		and    $0xffffffffffffffc0,%rsp
1838  *   41 ff 72 f8		pushq  -0x8(%r10)
1839  *   55				push   %rbp
1840  *   48 89 e5			mov    %rsp,%rbp
1841  *				(more pushes)
1842  *   41 52			push   %r10
1843  *				...
1844  *   41 5a			pop    %r10
1845  *				(more pops)
1846  *   5d				pop    %rbp
1847  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
1848  *   c3				retq
1849  *
1850  * There are some variations in the epilogues, like:
1851  *
1852  *   5b				pop    %rbx
1853  *   41 5a			pop    %r10
1854  *   41 5c			pop    %r12
1855  *   41 5d			pop    %r13
1856  *   41 5e			pop    %r14
1857  *   c9				leaveq
1858  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
1859  *   c3				retq
1860  *
1861  * and:
1862  *
1863  *   4c 8b 55 e8		mov    -0x18(%rbp),%r10
1864  *   48 8b 5d e0		mov    -0x20(%rbp),%rbx
1865  *   4c 8b 65 f0		mov    -0x10(%rbp),%r12
1866  *   4c 8b 6d f8		mov    -0x8(%rbp),%r13
1867  *   c9				leaveq
1868  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
1869  *   c3				retq
1870  *
1871  * Sometimes r13 is used as the DRAP register, in which case it's saved and
1872  * restored beforehand:
1873  *
1874  *   41 55			push   %r13
1875  *   4c 8d 6c 24 10		lea    0x10(%rsp),%r13
1876  *   48 83 e4 f0		and    $0xfffffffffffffff0,%rsp
1877  *				...
1878  *   49 8d 65 f0		lea    -0x10(%r13),%rsp
1879  *   41 5d			pop    %r13
1880  *   c3				retq
1881  */
1882 static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
1883 			     struct stack_op *op)
1884 {
1885 	struct cfi_reg *cfa = &cfi->cfa;
1886 	struct cfi_reg *regs = cfi->regs;
1887 
1888 	/* stack operations don't make sense with an undefined CFA */
1889 	if (cfa->base == CFI_UNDEFINED) {
1890 		if (insn->func) {
1891 			WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1892 			return -1;
1893 		}
1894 		return 0;
1895 	}
1896 
1897 	if (cfi->type == ORC_TYPE_REGS || cfi->type == ORC_TYPE_REGS_IRET)
1898 		return update_cfi_state_regs(insn, cfi, op);
1899 
1900 	switch (op->dest.type) {
1901 
1902 	case OP_DEST_REG:
1903 		switch (op->src.type) {
1904 
1905 		case OP_SRC_REG:
1906 			if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1907 			    cfa->base == CFI_SP &&
1908 			    regs[CFI_BP].base == CFI_CFA &&
1909 			    regs[CFI_BP].offset == -cfa->offset) {
1910 
1911 				/* mov %rsp, %rbp */
1912 				cfa->base = op->dest.reg;
1913 				cfi->bp_scratch = false;
1914 			}
1915 
1916 			else if (op->src.reg == CFI_SP &&
1917 				 op->dest.reg == CFI_BP && cfi->drap) {
1918 
1919 				/* drap: mov %rsp, %rbp */
1920 				regs[CFI_BP].base = CFI_BP;
1921 				regs[CFI_BP].offset = -cfi->stack_size;
1922 				cfi->bp_scratch = false;
1923 			}
1924 
1925 			else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1926 
1927 				/*
1928 				 * mov %rsp, %reg
1929 				 *
1930 				 * This is needed for the rare case where GCC
1931 				 * does:
1932 				 *
1933 				 *   mov    %rsp, %rax
1934 				 *   ...
1935 				 *   mov    %rax, %rsp
1936 				 */
1937 				cfi->vals[op->dest.reg].base = CFI_CFA;
1938 				cfi->vals[op->dest.reg].offset = -cfi->stack_size;
1939 			}
1940 
1941 			else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1942 				 cfa->base == CFI_BP) {
1943 
1944 				/*
1945 				 * mov %rbp, %rsp
1946 				 *
1947 				 * Restore the original stack pointer (Clang).
1948 				 */
1949 				cfi->stack_size = -cfi->regs[CFI_BP].offset;
1950 			}
1951 
1952 			else if (op->dest.reg == cfa->base) {
1953 
1954 				/* mov %reg, %rsp */
1955 				if (cfa->base == CFI_SP &&
1956 				    cfi->vals[op->src.reg].base == CFI_CFA) {
1957 
1958 					/*
1959 					 * This is needed for the rare case
1960 					 * where GCC does something dumb like:
1961 					 *
1962 					 *   lea    0x8(%rsp), %rcx
1963 					 *   ...
1964 					 *   mov    %rcx, %rsp
1965 					 */
1966 					cfa->offset = -cfi->vals[op->src.reg].offset;
1967 					cfi->stack_size = cfa->offset;
1968 
1969 				} else {
1970 					cfa->base = CFI_UNDEFINED;
1971 					cfa->offset = 0;
1972 				}
1973 			}
1974 
1975 			break;
1976 
1977 		case OP_SRC_ADD:
1978 			if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1979 
1980 				/* add imm, %rsp */
1981 				cfi->stack_size -= op->src.offset;
1982 				if (cfa->base == CFI_SP)
1983 					cfa->offset -= op->src.offset;
1984 				break;
1985 			}
1986 
1987 			if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1988 
1989 				/* lea disp(%rbp), %rsp */
1990 				cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1991 				break;
1992 			}
1993 
1994 			if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1995 
1996 				/* drap: lea disp(%rsp), %drap */
1997 				cfi->drap_reg = op->dest.reg;
1998 
1999 				/*
2000 				 * lea disp(%rsp), %reg
2001 				 *
2002 				 * This is needed for the rare case where GCC
2003 				 * does something dumb like:
2004 				 *
2005 				 *   lea    0x8(%rsp), %rcx
2006 				 *   ...
2007 				 *   mov    %rcx, %rsp
2008 				 */
2009 				cfi->vals[op->dest.reg].base = CFI_CFA;
2010 				cfi->vals[op->dest.reg].offset = \
2011 					-cfi->stack_size + op->src.offset;
2012 
2013 				break;
2014 			}
2015 
2016 			if (cfi->drap && op->dest.reg == CFI_SP &&
2017 			    op->src.reg == cfi->drap_reg) {
2018 
2019 				 /* drap: lea disp(%drap), %rsp */
2020 				cfa->base = CFI_SP;
2021 				cfa->offset = cfi->stack_size = -op->src.offset;
2022 				cfi->drap_reg = CFI_UNDEFINED;
2023 				cfi->drap = false;
2024 				break;
2025 			}
2026 
2027 			if (op->dest.reg == cfi->cfa.base) {
2028 				WARN_FUNC("unsupported stack register modification",
2029 					  insn->sec, insn->offset);
2030 				return -1;
2031 			}
2032 
2033 			break;
2034 
2035 		case OP_SRC_AND:
2036 			if (op->dest.reg != CFI_SP ||
2037 			    (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2038 			    (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2039 				WARN_FUNC("unsupported stack pointer realignment",
2040 					  insn->sec, insn->offset);
2041 				return -1;
2042 			}
2043 
2044 			if (cfi->drap_reg != CFI_UNDEFINED) {
2045 				/* drap: and imm, %rsp */
2046 				cfa->base = cfi->drap_reg;
2047 				cfa->offset = cfi->stack_size = 0;
2048 				cfi->drap = true;
2049 			}
2050 
2051 			/*
2052 			 * Older versions of GCC (4.8ish) realign the stack
2053 			 * without DRAP, with a frame pointer.
2054 			 */
2055 
2056 			break;
2057 
2058 		case OP_SRC_POP:
2059 		case OP_SRC_POPF:
2060 			if (!cfi->drap && op->dest.reg == cfa->base) {
2061 
2062 				/* pop %rbp */
2063 				cfa->base = CFI_SP;
2064 			}
2065 
2066 			if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2067 			    op->dest.reg == cfi->drap_reg &&
2068 			    cfi->drap_offset == -cfi->stack_size) {
2069 
2070 				/* drap: pop %drap */
2071 				cfa->base = cfi->drap_reg;
2072 				cfa->offset = 0;
2073 				cfi->drap_offset = -1;
2074 
2075 			} else if (regs[op->dest.reg].offset == -cfi->stack_size) {
2076 
2077 				/* pop %reg */
2078 				restore_reg(cfi, op->dest.reg);
2079 			}
2080 
2081 			cfi->stack_size -= 8;
2082 			if (cfa->base == CFI_SP)
2083 				cfa->offset -= 8;
2084 
2085 			break;
2086 
2087 		case OP_SRC_REG_INDIRECT:
2088 			if (cfi->drap && op->src.reg == CFI_BP &&
2089 			    op->src.offset == cfi->drap_offset) {
2090 
2091 				/* drap: mov disp(%rbp), %drap */
2092 				cfa->base = cfi->drap_reg;
2093 				cfa->offset = 0;
2094 				cfi->drap_offset = -1;
2095 			}
2096 
2097 			if (cfi->drap && op->src.reg == CFI_BP &&
2098 			    op->src.offset == regs[op->dest.reg].offset) {
2099 
2100 				/* drap: mov disp(%rbp), %reg */
2101 				restore_reg(cfi, op->dest.reg);
2102 
2103 			} else if (op->src.reg == cfa->base &&
2104 			    op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2105 
2106 				/* mov disp(%rbp), %reg */
2107 				/* mov disp(%rsp), %reg */
2108 				restore_reg(cfi, op->dest.reg);
2109 			}
2110 
2111 			break;
2112 
2113 		default:
2114 			WARN_FUNC("unknown stack-related instruction",
2115 				  insn->sec, insn->offset);
2116 			return -1;
2117 		}
2118 
2119 		break;
2120 
2121 	case OP_DEST_PUSH:
2122 	case OP_DEST_PUSHF:
2123 		cfi->stack_size += 8;
2124 		if (cfa->base == CFI_SP)
2125 			cfa->offset += 8;
2126 
2127 		if (op->src.type != OP_SRC_REG)
2128 			break;
2129 
2130 		if (cfi->drap) {
2131 			if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2132 
2133 				/* drap: push %drap */
2134 				cfa->base = CFI_BP_INDIRECT;
2135 				cfa->offset = -cfi->stack_size;
2136 
2137 				/* save drap so we know when to restore it */
2138 				cfi->drap_offset = -cfi->stack_size;
2139 
2140 			} else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2141 
2142 				/* drap: push %rbp */
2143 				cfi->stack_size = 0;
2144 
2145 			} else if (regs[op->src.reg].base == CFI_UNDEFINED) {
2146 
2147 				/* drap: push %reg */
2148 				save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2149 			}
2150 
2151 		} else {
2152 
2153 			/* push %reg */
2154 			save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2155 		}
2156 
2157 		/* detect when asm code uses rbp as a scratch register */
2158 		if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2159 		    cfa->base != CFI_BP)
2160 			cfi->bp_scratch = true;
2161 		break;
2162 
2163 	case OP_DEST_REG_INDIRECT:
2164 
2165 		if (cfi->drap) {
2166 			if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2167 
2168 				/* drap: mov %drap, disp(%rbp) */
2169 				cfa->base = CFI_BP_INDIRECT;
2170 				cfa->offset = op->dest.offset;
2171 
2172 				/* save drap offset so we know when to restore it */
2173 				cfi->drap_offset = op->dest.offset;
2174 			}
2175 
2176 			else if (regs[op->src.reg].base == CFI_UNDEFINED) {
2177 
2178 				/* drap: mov reg, disp(%rbp) */
2179 				save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2180 			}
2181 
2182 		} else if (op->dest.reg == cfa->base) {
2183 
2184 			/* mov reg, disp(%rbp) */
2185 			/* mov reg, disp(%rsp) */
2186 			save_reg(cfi, op->src.reg, CFI_CFA,
2187 				 op->dest.offset - cfi->cfa.offset);
2188 		}
2189 
2190 		break;
2191 
2192 	case OP_DEST_LEAVE:
2193 		if ((!cfi->drap && cfa->base != CFI_BP) ||
2194 		    (cfi->drap && cfa->base != cfi->drap_reg)) {
2195 			WARN_FUNC("leave instruction with modified stack frame",
2196 				  insn->sec, insn->offset);
2197 			return -1;
2198 		}
2199 
2200 		/* leave (mov %rbp, %rsp; pop %rbp) */
2201 
2202 		cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2203 		restore_reg(cfi, CFI_BP);
2204 
2205 		if (!cfi->drap) {
2206 			cfa->base = CFI_SP;
2207 			cfa->offset -= 8;
2208 		}
2209 
2210 		break;
2211 
2212 	case OP_DEST_MEM:
2213 		if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2214 			WARN_FUNC("unknown stack-related memory operation",
2215 				  insn->sec, insn->offset);
2216 			return -1;
2217 		}
2218 
2219 		/* pop mem */
2220 		cfi->stack_size -= 8;
2221 		if (cfa->base == CFI_SP)
2222 			cfa->offset -= 8;
2223 
2224 		break;
2225 
2226 	default:
2227 		WARN_FUNC("unknown stack-related instruction",
2228 			  insn->sec, insn->offset);
2229 		return -1;
2230 	}
2231 
2232 	return 0;
2233 }
2234 
2235 static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2236 {
2237 	struct stack_op *op;
2238 
2239 	list_for_each_entry(op, &insn->stack_ops, list) {
2240 		struct cfi_state old_cfi = state->cfi;
2241 		int res;
2242 
2243 		res = update_cfi_state(insn, &state->cfi, op);
2244 		if (res)
2245 			return res;
2246 
2247 		if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) {
2248 			WARN_FUNC("alternative modifies stack", insn->sec, insn->offset);
2249 			return -1;
2250 		}
2251 
2252 		if (op->dest.type == OP_DEST_PUSHF) {
2253 			if (!state->uaccess_stack) {
2254 				state->uaccess_stack = 1;
2255 			} else if (state->uaccess_stack >> 31) {
2256 				WARN_FUNC("PUSHF stack exhausted",
2257 					  insn->sec, insn->offset);
2258 				return 1;
2259 			}
2260 			state->uaccess_stack <<= 1;
2261 			state->uaccess_stack  |= state->uaccess;
2262 		}
2263 
2264 		if (op->src.type == OP_SRC_POPF) {
2265 			if (state->uaccess_stack) {
2266 				state->uaccess = state->uaccess_stack & 1;
2267 				state->uaccess_stack >>= 1;
2268 				if (state->uaccess_stack == 1)
2269 					state->uaccess_stack = 0;
2270 			}
2271 		}
2272 	}
2273 
2274 	return 0;
2275 }
2276 
2277 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2278 {
2279 	struct cfi_state *cfi1 = &insn->cfi;
2280 	int i;
2281 
2282 	if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2283 
2284 		WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2285 			  insn->sec, insn->offset,
2286 			  cfi1->cfa.base, cfi1->cfa.offset,
2287 			  cfi2->cfa.base, cfi2->cfa.offset);
2288 
2289 	} else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2290 		for (i = 0; i < CFI_NUM_REGS; i++) {
2291 			if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2292 				    sizeof(struct cfi_reg)))
2293 				continue;
2294 
2295 			WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2296 				  insn->sec, insn->offset,
2297 				  i, cfi1->regs[i].base, cfi1->regs[i].offset,
2298 				  i, cfi2->regs[i].base, cfi2->regs[i].offset);
2299 			break;
2300 		}
2301 
2302 	} else if (cfi1->type != cfi2->type) {
2303 
2304 		WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2305 			  insn->sec, insn->offset, cfi1->type, cfi2->type);
2306 
2307 	} else if (cfi1->drap != cfi2->drap ||
2308 		   (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2309 		   (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2310 
2311 		WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2312 			  insn->sec, insn->offset,
2313 			  cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2314 			  cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2315 
2316 	} else
2317 		return true;
2318 
2319 	return false;
2320 }
2321 
2322 static inline bool func_uaccess_safe(struct symbol *func)
2323 {
2324 	if (func)
2325 		return func->uaccess_safe;
2326 
2327 	return false;
2328 }
2329 
2330 static inline const char *call_dest_name(struct instruction *insn)
2331 {
2332 	if (insn->call_dest)
2333 		return insn->call_dest->name;
2334 
2335 	return "{dynamic}";
2336 }
2337 
2338 static inline bool noinstr_call_dest(struct symbol *func)
2339 {
2340 	/*
2341 	 * We can't deal with indirect function calls at present;
2342 	 * assume they're instrumented.
2343 	 */
2344 	if (!func)
2345 		return false;
2346 
2347 	/*
2348 	 * If the symbol is from a noinstr section; we good.
2349 	 */
2350 	if (func->sec->noinstr)
2351 		return true;
2352 
2353 	/*
2354 	 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2355 	 * something 'BAD' happened. At the risk of taking the machine down,
2356 	 * let them proceed to get the message out.
2357 	 */
2358 	if (!strncmp(func->name, "__ubsan_handle_", 15))
2359 		return true;
2360 
2361 	return false;
2362 }
2363 
2364 static int validate_call(struct instruction *insn, struct insn_state *state)
2365 {
2366 	if (state->noinstr && state->instr <= 0 &&
2367 	    !noinstr_call_dest(insn->call_dest)) {
2368 		WARN_FUNC("call to %s() leaves .noinstr.text section",
2369 				insn->sec, insn->offset, call_dest_name(insn));
2370 		return 1;
2371 	}
2372 
2373 	if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2374 		WARN_FUNC("call to %s() with UACCESS enabled",
2375 				insn->sec, insn->offset, call_dest_name(insn));
2376 		return 1;
2377 	}
2378 
2379 	if (state->df) {
2380 		WARN_FUNC("call to %s() with DF set",
2381 				insn->sec, insn->offset, call_dest_name(insn));
2382 		return 1;
2383 	}
2384 
2385 	return 0;
2386 }
2387 
2388 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2389 {
2390 	if (has_modified_stack_frame(insn, state)) {
2391 		WARN_FUNC("sibling call from callable instruction with modified stack frame",
2392 				insn->sec, insn->offset);
2393 		return 1;
2394 	}
2395 
2396 	return validate_call(insn, state);
2397 }
2398 
2399 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2400 {
2401 	if (state->noinstr && state->instr > 0) {
2402 		WARN_FUNC("return with instrumentation enabled",
2403 			  insn->sec, insn->offset);
2404 		return 1;
2405 	}
2406 
2407 	if (state->uaccess && !func_uaccess_safe(func)) {
2408 		WARN_FUNC("return with UACCESS enabled",
2409 			  insn->sec, insn->offset);
2410 		return 1;
2411 	}
2412 
2413 	if (!state->uaccess && func_uaccess_safe(func)) {
2414 		WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2415 			  insn->sec, insn->offset);
2416 		return 1;
2417 	}
2418 
2419 	if (state->df) {
2420 		WARN_FUNC("return with DF set",
2421 			  insn->sec, insn->offset);
2422 		return 1;
2423 	}
2424 
2425 	if (func && has_modified_stack_frame(insn, state)) {
2426 		WARN_FUNC("return with modified stack frame",
2427 			  insn->sec, insn->offset);
2428 		return 1;
2429 	}
2430 
2431 	if (state->cfi.bp_scratch) {
2432 		WARN_FUNC("BP used as a scratch register",
2433 			  insn->sec, insn->offset);
2434 		return 1;
2435 	}
2436 
2437 	return 0;
2438 }
2439 
2440 /*
2441  * Alternatives should not contain any ORC entries, this in turn means they
2442  * should not contain any CFI ops, which implies all instructions should have
2443  * the same same CFI state.
2444  *
2445  * It is possible to constuct alternatives that have unreachable holes that go
2446  * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2447  * states which then results in ORC entries, which we just said we didn't want.
2448  *
2449  * Avoid them by copying the CFI entry of the first instruction into the whole
2450  * alternative.
2451  */
2452 static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn)
2453 {
2454 	struct instruction *first_insn = insn;
2455 	int alt_group = insn->alt_group;
2456 
2457 	sec_for_each_insn_continue(file, insn) {
2458 		if (insn->alt_group != alt_group)
2459 			break;
2460 		insn->cfi = first_insn->cfi;
2461 	}
2462 }
2463 
2464 /*
2465  * Follow the branch starting at the given instruction, and recursively follow
2466  * any other branches (jumps).  Meanwhile, track the frame pointer state at
2467  * each instruction and validate all the rules described in
2468  * tools/objtool/Documentation/stack-validation.txt.
2469  */
2470 static int validate_branch(struct objtool_file *file, struct symbol *func,
2471 			   struct instruction *insn, struct insn_state state)
2472 {
2473 	struct alternative *alt;
2474 	struct instruction *next_insn;
2475 	struct section *sec;
2476 	u8 visited;
2477 	int ret;
2478 
2479 	sec = insn->sec;
2480 
2481 	while (1) {
2482 		next_insn = next_insn_same_sec(file, insn);
2483 
2484 		if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2485 			WARN("%s() falls through to next function %s()",
2486 			     func->name, insn->func->name);
2487 			return 1;
2488 		}
2489 
2490 		if (func && insn->ignore) {
2491 			WARN_FUNC("BUG: why am I validating an ignored function?",
2492 				  sec, insn->offset);
2493 			return 1;
2494 		}
2495 
2496 		visited = 1 << state.uaccess;
2497 		if (insn->visited) {
2498 			if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2499 				return 1;
2500 
2501 			if (insn->visited & visited)
2502 				return 0;
2503 		}
2504 
2505 		if (state.noinstr)
2506 			state.instr += insn->instr;
2507 
2508 		if (insn->hint)
2509 			state.cfi = insn->cfi;
2510 		else
2511 			insn->cfi = state.cfi;
2512 
2513 		insn->visited |= visited;
2514 
2515 		if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2516 			bool skip_orig = false;
2517 
2518 			list_for_each_entry(alt, &insn->alts, list) {
2519 				if (alt->skip_orig)
2520 					skip_orig = true;
2521 
2522 				ret = validate_branch(file, func, alt->insn, state);
2523 				if (ret) {
2524 					if (backtrace)
2525 						BT_FUNC("(alt)", insn);
2526 					return ret;
2527 				}
2528 			}
2529 
2530 			if (insn->alt_group)
2531 				fill_alternative_cfi(file, insn);
2532 
2533 			if (skip_orig)
2534 				return 0;
2535 		}
2536 
2537 		if (handle_insn_ops(insn, &state))
2538 			return 1;
2539 
2540 		switch (insn->type) {
2541 
2542 		case INSN_RETURN:
2543 			return validate_return(func, insn, &state);
2544 
2545 		case INSN_CALL:
2546 		case INSN_CALL_DYNAMIC:
2547 			ret = validate_call(insn, &state);
2548 			if (ret)
2549 				return ret;
2550 
2551 			if (!no_fp && func && !is_fentry_call(insn) &&
2552 			    !has_valid_stack_frame(&state)) {
2553 				WARN_FUNC("call without frame pointer save/setup",
2554 					  sec, insn->offset);
2555 				return 1;
2556 			}
2557 
2558 			if (dead_end_function(file, insn->call_dest))
2559 				return 0;
2560 
2561 			if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) {
2562 				list_add_tail(&insn->static_call_node,
2563 					      &file->static_call_list);
2564 			}
2565 
2566 			break;
2567 
2568 		case INSN_JUMP_CONDITIONAL:
2569 		case INSN_JUMP_UNCONDITIONAL:
2570 			if (func && is_sibling_call(insn)) {
2571 				ret = validate_sibling_call(insn, &state);
2572 				if (ret)
2573 					return ret;
2574 
2575 			} else if (insn->jump_dest) {
2576 				ret = validate_branch(file, func,
2577 						      insn->jump_dest, state);
2578 				if (ret) {
2579 					if (backtrace)
2580 						BT_FUNC("(branch)", insn);
2581 					return ret;
2582 				}
2583 			}
2584 
2585 			if (insn->type == INSN_JUMP_UNCONDITIONAL)
2586 				return 0;
2587 
2588 			break;
2589 
2590 		case INSN_JUMP_DYNAMIC:
2591 		case INSN_JUMP_DYNAMIC_CONDITIONAL:
2592 			if (func && is_sibling_call(insn)) {
2593 				ret = validate_sibling_call(insn, &state);
2594 				if (ret)
2595 					return ret;
2596 			}
2597 
2598 			if (insn->type == INSN_JUMP_DYNAMIC)
2599 				return 0;
2600 
2601 			break;
2602 
2603 		case INSN_CONTEXT_SWITCH:
2604 			if (func && (!next_insn || !next_insn->hint)) {
2605 				WARN_FUNC("unsupported instruction in callable function",
2606 					  sec, insn->offset);
2607 				return 1;
2608 			}
2609 			return 0;
2610 
2611 		case INSN_STAC:
2612 			if (state.uaccess) {
2613 				WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2614 				return 1;
2615 			}
2616 
2617 			state.uaccess = true;
2618 			break;
2619 
2620 		case INSN_CLAC:
2621 			if (!state.uaccess && func) {
2622 				WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2623 				return 1;
2624 			}
2625 
2626 			if (func_uaccess_safe(func) && !state.uaccess_stack) {
2627 				WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2628 				return 1;
2629 			}
2630 
2631 			state.uaccess = false;
2632 			break;
2633 
2634 		case INSN_STD:
2635 			if (state.df)
2636 				WARN_FUNC("recursive STD", sec, insn->offset);
2637 
2638 			state.df = true;
2639 			break;
2640 
2641 		case INSN_CLD:
2642 			if (!state.df && func)
2643 				WARN_FUNC("redundant CLD", sec, insn->offset);
2644 
2645 			state.df = false;
2646 			break;
2647 
2648 		default:
2649 			break;
2650 		}
2651 
2652 		if (insn->dead_end)
2653 			return 0;
2654 
2655 		if (!next_insn) {
2656 			if (state.cfi.cfa.base == CFI_UNDEFINED)
2657 				return 0;
2658 			WARN("%s: unexpected end of section", sec->name);
2659 			return 1;
2660 		}
2661 
2662 		insn = next_insn;
2663 	}
2664 
2665 	return 0;
2666 }
2667 
2668 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2669 {
2670 	struct instruction *insn;
2671 	struct insn_state state;
2672 	int ret, warnings = 0;
2673 
2674 	if (!file->hints)
2675 		return 0;
2676 
2677 	init_insn_state(&state, sec);
2678 
2679 	if (sec) {
2680 		insn = find_insn(file, sec, 0);
2681 		if (!insn)
2682 			return 0;
2683 	} else {
2684 		insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2685 	}
2686 
2687 	while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
2688 		if (insn->hint && !insn->visited) {
2689 			ret = validate_branch(file, insn->func, insn, state);
2690 			if (ret && backtrace)
2691 				BT_FUNC("<=== (hint)", insn);
2692 			warnings += ret;
2693 		}
2694 
2695 		insn = list_next_entry(insn, list);
2696 	}
2697 
2698 	return warnings;
2699 }
2700 
2701 static int validate_retpoline(struct objtool_file *file)
2702 {
2703 	struct instruction *insn;
2704 	int warnings = 0;
2705 
2706 	for_each_insn(file, insn) {
2707 		if (insn->type != INSN_JUMP_DYNAMIC &&
2708 		    insn->type != INSN_CALL_DYNAMIC)
2709 			continue;
2710 
2711 		if (insn->retpoline_safe)
2712 			continue;
2713 
2714 		/*
2715 		 * .init.text code is ran before userspace and thus doesn't
2716 		 * strictly need retpolines, except for modules which are
2717 		 * loaded late, they very much do need retpoline in their
2718 		 * .init.text
2719 		 */
2720 		if (!strcmp(insn->sec->name, ".init.text") && !module)
2721 			continue;
2722 
2723 		WARN_FUNC("indirect %s found in RETPOLINE build",
2724 			  insn->sec, insn->offset,
2725 			  insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2726 
2727 		warnings++;
2728 	}
2729 
2730 	return warnings;
2731 }
2732 
2733 static bool is_kasan_insn(struct instruction *insn)
2734 {
2735 	return (insn->type == INSN_CALL &&
2736 		!strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2737 }
2738 
2739 static bool is_ubsan_insn(struct instruction *insn)
2740 {
2741 	return (insn->type == INSN_CALL &&
2742 		!strcmp(insn->call_dest->name,
2743 			"__ubsan_handle_builtin_unreachable"));
2744 }
2745 
2746 static bool ignore_unreachable_insn(struct instruction *insn)
2747 {
2748 	int i;
2749 
2750 	if (insn->ignore || insn->type == INSN_NOP)
2751 		return true;
2752 
2753 	/*
2754 	 * Ignore any unused exceptions.  This can happen when a whitelisted
2755 	 * function has an exception table entry.
2756 	 *
2757 	 * Also ignore alternative replacement instructions.  This can happen
2758 	 * when a whitelisted function uses one of the ALTERNATIVE macros.
2759 	 */
2760 	if (!strcmp(insn->sec->name, ".fixup") ||
2761 	    !strcmp(insn->sec->name, ".altinstr_replacement") ||
2762 	    !strcmp(insn->sec->name, ".altinstr_aux"))
2763 		return true;
2764 
2765 	if (!insn->func)
2766 		return false;
2767 
2768 	/*
2769 	 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2770 	 * __builtin_unreachable().  The BUG() macro has an unreachable() after
2771 	 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2772 	 * (or occasionally a JMP to UD2).
2773 	 */
2774 	if (list_prev_entry(insn, list)->dead_end &&
2775 	    (insn->type == INSN_BUG ||
2776 	     (insn->type == INSN_JUMP_UNCONDITIONAL &&
2777 	      insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2778 		return true;
2779 
2780 	/*
2781 	 * Check if this (or a subsequent) instruction is related to
2782 	 * CONFIG_UBSAN or CONFIG_KASAN.
2783 	 *
2784 	 * End the search at 5 instructions to avoid going into the weeds.
2785 	 */
2786 	for (i = 0; i < 5; i++) {
2787 
2788 		if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2789 			return true;
2790 
2791 		if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2792 			if (insn->jump_dest &&
2793 			    insn->jump_dest->func == insn->func) {
2794 				insn = insn->jump_dest;
2795 				continue;
2796 			}
2797 
2798 			break;
2799 		}
2800 
2801 		if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2802 			break;
2803 
2804 		insn = list_next_entry(insn, list);
2805 	}
2806 
2807 	return false;
2808 }
2809 
2810 static int validate_symbol(struct objtool_file *file, struct section *sec,
2811 			   struct symbol *sym, struct insn_state *state)
2812 {
2813 	struct instruction *insn;
2814 	int ret;
2815 
2816 	if (!sym->len) {
2817 		WARN("%s() is missing an ELF size annotation", sym->name);
2818 		return 1;
2819 	}
2820 
2821 	if (sym->pfunc != sym || sym->alias != sym)
2822 		return 0;
2823 
2824 	insn = find_insn(file, sec, sym->offset);
2825 	if (!insn || insn->ignore || insn->visited)
2826 		return 0;
2827 
2828 	state->uaccess = sym->uaccess_safe;
2829 
2830 	ret = validate_branch(file, insn->func, insn, *state);
2831 	if (ret && backtrace)
2832 		BT_FUNC("<=== (sym)", insn);
2833 	return ret;
2834 }
2835 
2836 static int validate_section(struct objtool_file *file, struct section *sec)
2837 {
2838 	struct insn_state state;
2839 	struct symbol *func;
2840 	int warnings = 0;
2841 
2842 	list_for_each_entry(func, &sec->symbol_list, list) {
2843 		if (func->type != STT_FUNC)
2844 			continue;
2845 
2846 		init_insn_state(&state, sec);
2847 		state.cfi.cfa = initial_func_cfi.cfa;
2848 		memcpy(&state.cfi.regs, &initial_func_cfi.regs,
2849 		       CFI_NUM_REGS * sizeof(struct cfi_reg));
2850 		state.cfi.stack_size = initial_func_cfi.cfa.offset;
2851 
2852 		warnings += validate_symbol(file, sec, func, &state);
2853 	}
2854 
2855 	return warnings;
2856 }
2857 
2858 static int validate_vmlinux_functions(struct objtool_file *file)
2859 {
2860 	struct section *sec;
2861 	int warnings = 0;
2862 
2863 	sec = find_section_by_name(file->elf, ".noinstr.text");
2864 	if (sec) {
2865 		warnings += validate_section(file, sec);
2866 		warnings += validate_unwind_hints(file, sec);
2867 	}
2868 
2869 	sec = find_section_by_name(file->elf, ".entry.text");
2870 	if (sec) {
2871 		warnings += validate_section(file, sec);
2872 		warnings += validate_unwind_hints(file, sec);
2873 	}
2874 
2875 	return warnings;
2876 }
2877 
2878 static int validate_functions(struct objtool_file *file)
2879 {
2880 	struct section *sec;
2881 	int warnings = 0;
2882 
2883 	for_each_sec(file, sec) {
2884 		if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2885 			continue;
2886 
2887 		warnings += validate_section(file, sec);
2888 	}
2889 
2890 	return warnings;
2891 }
2892 
2893 static int validate_reachable_instructions(struct objtool_file *file)
2894 {
2895 	struct instruction *insn;
2896 
2897 	if (file->ignore_unreachables)
2898 		return 0;
2899 
2900 	for_each_insn(file, insn) {
2901 		if (insn->visited || ignore_unreachable_insn(insn))
2902 			continue;
2903 
2904 		WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2905 		return 1;
2906 	}
2907 
2908 	return 0;
2909 }
2910 
2911 int check(struct objtool_file *file)
2912 {
2913 	int ret, warnings = 0;
2914 
2915 	arch_initial_func_cfi_state(&initial_func_cfi);
2916 
2917 	ret = decode_sections(file);
2918 	if (ret < 0)
2919 		goto out;
2920 	warnings += ret;
2921 
2922 	if (list_empty(&file->insn_list))
2923 		goto out;
2924 
2925 	if (vmlinux && !validate_dup) {
2926 		ret = validate_vmlinux_functions(file);
2927 		if (ret < 0)
2928 			goto out;
2929 
2930 		warnings += ret;
2931 		goto out;
2932 	}
2933 
2934 	if (retpoline) {
2935 		ret = validate_retpoline(file);
2936 		if (ret < 0)
2937 			return ret;
2938 		warnings += ret;
2939 	}
2940 
2941 	ret = validate_functions(file);
2942 	if (ret < 0)
2943 		goto out;
2944 	warnings += ret;
2945 
2946 	ret = validate_unwind_hints(file, NULL);
2947 	if (ret < 0)
2948 		goto out;
2949 	warnings += ret;
2950 
2951 	if (!warnings) {
2952 		ret = validate_reachable_instructions(file);
2953 		if (ret < 0)
2954 			goto out;
2955 		warnings += ret;
2956 	}
2957 
2958 	ret = create_static_call_sections(file);
2959 	if (ret < 0)
2960 		goto out;
2961 	warnings += ret;
2962 
2963 out:
2964 	if (ret < 0) {
2965 		/*
2966 		 *  Fatal error.  The binary is corrupt or otherwise broken in
2967 		 *  some way, or objtool itself is broken.  Fail the kernel
2968 		 *  build.
2969 		 */
2970 		return ret;
2971 	}
2972 
2973 	return 0;
2974 }
2975