xref: /openbmc/linux/tools/objtool/check.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
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 
find_insn(struct objtool_file * file,struct section * sec,unsigned long offset)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 
next_insn_same_sec(struct objtool_file * file,struct instruction * insn)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 
next_insn_same_func(struct objtool_file * file,struct instruction * insn)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 
prev_insn_same_sec(struct objtool_file * file,struct instruction * insn)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 
prev_insn_same_sym(struct objtool_file * file,struct instruction * insn)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 
insn_call_dest(struct instruction * insn)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 
insn_jump_table(struct instruction * insn)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 
is_jump_table_jump(struct instruction * insn)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 
is_sibling_call(struct instruction * insn)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  */
__dead_end_function(struct objtool_file * file,struct symbol * func,int recursion)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 
dead_end_function(struct objtool_file * file,struct symbol * func)259 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
260 {
261 	return __dead_end_function(file, func, 0);
262 }
263 
init_cfi_state(struct cfi_state * cfi)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 
init_insn_state(struct objtool_file * file,struct insn_state * state,struct section * sec)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 
cfi_alloc(void)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 
cficmp(struct cfi_state * cfi1,struct cfi_state * cfi2)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 
cfi_key(struct cfi_state * cfi)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 
cfi_hash_find_or_add(struct cfi_state * cfi)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 
cfi_hash_add(struct cfi_state * cfi)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 
cfi_hash_alloc(unsigned long size)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  */
decode_instructions(struct objtool_file * file)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.", 13))
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->embedded_insn || 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  */
add_pv_ops(struct objtool_file * file,const char * symname)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  */
init_pv_ops(struct objtool_file * file)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 
find_last_insn(struct objtool_file * file,struct section * sec)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  */
add_dead_ends(struct objtool_file * file)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 
create_static_call_sections(struct objtool_file * file)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 
create_retpoline_sites_sections(struct objtool_file * file)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 
create_return_sites_sections(struct objtool_file * file)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 
create_ibt_endbr_seal_sections(struct objtool_file * file)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 
create_cfi_sections(struct objtool_file * file)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 
create_mcount_loc_sections(struct objtool_file * file)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 
create_direct_call_sections(struct objtool_file * file)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  */
add_ignores(struct objtool_file * file)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 	/* TRACE_BRANCH_PROFILING */
1229 	"ftrace_likely_update",
1230 	/* STACKPROTECTOR */
1231 	"__stack_chk_fail",
1232 	/* misc */
1233 	"csum_partial_copy_generic",
1234 	"copy_mc_fragile",
1235 	"copy_mc_fragile_handle_tail",
1236 	"copy_mc_enhanced_fast_string",
1237 	"rep_stos_alternative",
1238 	"rep_movs_alternative",
1239 	"__copy_user_nocache",
1240 	NULL
1241 };
1242 
add_uaccess_safe(struct objtool_file * file)1243 static void add_uaccess_safe(struct objtool_file *file)
1244 {
1245 	struct symbol *func;
1246 	const char **name;
1247 
1248 	if (!opts.uaccess)
1249 		return;
1250 
1251 	for (name = uaccess_safe_builtin; *name; name++) {
1252 		func = find_symbol_by_name(file->elf, *name);
1253 		if (!func)
1254 			continue;
1255 
1256 		func->uaccess_safe = true;
1257 	}
1258 }
1259 
1260 /*
1261  * FIXME: For now, just ignore any alternatives which add retpolines.  This is
1262  * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
1263  * But it at least allows objtool to understand the control flow *around* the
1264  * retpoline.
1265  */
add_ignore_alternatives(struct objtool_file * file)1266 static int add_ignore_alternatives(struct objtool_file *file)
1267 {
1268 	struct section *rsec;
1269 	struct reloc *reloc;
1270 	struct instruction *insn;
1271 
1272 	rsec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
1273 	if (!rsec)
1274 		return 0;
1275 
1276 	for_each_reloc(rsec, reloc) {
1277 		if (reloc->sym->type != STT_SECTION) {
1278 			WARN("unexpected relocation symbol type in %s", rsec->name);
1279 			return -1;
1280 		}
1281 
1282 		insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
1283 		if (!insn) {
1284 			WARN("bad .discard.ignore_alts entry");
1285 			return -1;
1286 		}
1287 
1288 		insn->ignore_alts = true;
1289 	}
1290 
1291 	return 0;
1292 }
1293 
1294 /*
1295  * Symbols that replace INSN_CALL_DYNAMIC, every (tail) call to such a symbol
1296  * will be added to the .retpoline_sites section.
1297  */
arch_is_retpoline(struct symbol * sym)1298 __weak bool arch_is_retpoline(struct symbol *sym)
1299 {
1300 	return false;
1301 }
1302 
1303 /*
1304  * Symbols that replace INSN_RETURN, every (tail) call to such a symbol
1305  * will be added to the .return_sites section.
1306  */
arch_is_rethunk(struct symbol * sym)1307 __weak bool arch_is_rethunk(struct symbol *sym)
1308 {
1309 	return false;
1310 }
1311 
1312 /*
1313  * Symbols that are embedded inside other instructions, because sometimes crazy
1314  * code exists. These are mostly ignored for validation purposes.
1315  */
arch_is_embedded_insn(struct symbol * sym)1316 __weak bool arch_is_embedded_insn(struct symbol *sym)
1317 {
1318 	return false;
1319 }
1320 
insn_reloc(struct objtool_file * file,struct instruction * insn)1321 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
1322 {
1323 	struct reloc *reloc;
1324 
1325 	if (insn->no_reloc)
1326 		return NULL;
1327 
1328 	if (!file)
1329 		return NULL;
1330 
1331 	reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1332 					 insn->offset, insn->len);
1333 	if (!reloc) {
1334 		insn->no_reloc = 1;
1335 		return NULL;
1336 	}
1337 
1338 	return reloc;
1339 }
1340 
remove_insn_ops(struct instruction * insn)1341 static void remove_insn_ops(struct instruction *insn)
1342 {
1343 	struct stack_op *op, *next;
1344 
1345 	for (op = insn->stack_ops; op; op = next) {
1346 		next = op->next;
1347 		free(op);
1348 	}
1349 	insn->stack_ops = NULL;
1350 }
1351 
annotate_call_site(struct objtool_file * file,struct instruction * insn,bool sibling)1352 static void annotate_call_site(struct objtool_file *file,
1353 			       struct instruction *insn, bool sibling)
1354 {
1355 	struct reloc *reloc = insn_reloc(file, insn);
1356 	struct symbol *sym = insn_call_dest(insn);
1357 
1358 	if (!sym)
1359 		sym = reloc->sym;
1360 
1361 	/*
1362 	 * Alternative replacement code is just template code which is
1363 	 * sometimes copied to the original instruction. For now, don't
1364 	 * annotate it. (In the future we might consider annotating the
1365 	 * original instruction if/when it ever makes sense to do so.)
1366 	 */
1367 	if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1368 		return;
1369 
1370 	if (sym->static_call_tramp) {
1371 		list_add_tail(&insn->call_node, &file->static_call_list);
1372 		return;
1373 	}
1374 
1375 	if (sym->retpoline_thunk) {
1376 		list_add_tail(&insn->call_node, &file->retpoline_call_list);
1377 		return;
1378 	}
1379 
1380 	/*
1381 	 * Many compilers cannot disable KCOV or sanitizer calls with a function
1382 	 * attribute so they need a little help, NOP out any such calls from
1383 	 * noinstr text.
1384 	 */
1385 	if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) {
1386 		if (reloc)
1387 			set_reloc_type(file->elf, reloc, R_NONE);
1388 
1389 		elf_write_insn(file->elf, insn->sec,
1390 			       insn->offset, insn->len,
1391 			       sibling ? arch_ret_insn(insn->len)
1392 			               : arch_nop_insn(insn->len));
1393 
1394 		insn->type = sibling ? INSN_RETURN : INSN_NOP;
1395 
1396 		if (sibling) {
1397 			/*
1398 			 * We've replaced the tail-call JMP insn by two new
1399 			 * insn: RET; INT3, except we only have a single struct
1400 			 * insn here. Mark it retpoline_safe to avoid the SLS
1401 			 * warning, instead of adding another insn.
1402 			 */
1403 			insn->retpoline_safe = true;
1404 		}
1405 
1406 		return;
1407 	}
1408 
1409 	if (opts.mcount && sym->fentry) {
1410 		if (sibling)
1411 			WARN_INSN(insn, "tail call to __fentry__ !?!?");
1412 		if (opts.mnop) {
1413 			if (reloc)
1414 				set_reloc_type(file->elf, reloc, R_NONE);
1415 
1416 			elf_write_insn(file->elf, insn->sec,
1417 				       insn->offset, insn->len,
1418 				       arch_nop_insn(insn->len));
1419 
1420 			insn->type = INSN_NOP;
1421 		}
1422 
1423 		list_add_tail(&insn->call_node, &file->mcount_loc_list);
1424 		return;
1425 	}
1426 
1427 	if (insn->type == INSN_CALL && !insn->sec->init)
1428 		list_add_tail(&insn->call_node, &file->call_list);
1429 
1430 	if (!sibling && dead_end_function(file, sym))
1431 		insn->dead_end = true;
1432 }
1433 
add_call_dest(struct objtool_file * file,struct instruction * insn,struct symbol * dest,bool sibling)1434 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1435 			  struct symbol *dest, bool sibling)
1436 {
1437 	insn->_call_dest = dest;
1438 	if (!dest)
1439 		return;
1440 
1441 	/*
1442 	 * Whatever stack impact regular CALLs have, should be undone
1443 	 * by the RETURN of the called function.
1444 	 *
1445 	 * Annotated intra-function calls retain the stack_ops but
1446 	 * are converted to JUMP, see read_intra_function_calls().
1447 	 */
1448 	remove_insn_ops(insn);
1449 
1450 	annotate_call_site(file, insn, sibling);
1451 }
1452 
add_retpoline_call(struct objtool_file * file,struct instruction * insn)1453 static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1454 {
1455 	/*
1456 	 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1457 	 * so convert them accordingly.
1458 	 */
1459 	switch (insn->type) {
1460 	case INSN_CALL:
1461 		insn->type = INSN_CALL_DYNAMIC;
1462 		break;
1463 	case INSN_JUMP_UNCONDITIONAL:
1464 		insn->type = INSN_JUMP_DYNAMIC;
1465 		break;
1466 	case INSN_JUMP_CONDITIONAL:
1467 		insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1468 		break;
1469 	default:
1470 		return;
1471 	}
1472 
1473 	insn->retpoline_safe = true;
1474 
1475 	/*
1476 	 * Whatever stack impact regular CALLs have, should be undone
1477 	 * by the RETURN of the called function.
1478 	 *
1479 	 * Annotated intra-function calls retain the stack_ops but
1480 	 * are converted to JUMP, see read_intra_function_calls().
1481 	 */
1482 	remove_insn_ops(insn);
1483 
1484 	annotate_call_site(file, insn, false);
1485 }
1486 
add_return_call(struct objtool_file * file,struct instruction * insn,bool add)1487 static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add)
1488 {
1489 	/*
1490 	 * Return thunk tail calls are really just returns in disguise,
1491 	 * so convert them accordingly.
1492 	 */
1493 	insn->type = INSN_RETURN;
1494 	insn->retpoline_safe = true;
1495 
1496 	if (add)
1497 		list_add_tail(&insn->call_node, &file->return_thunk_list);
1498 }
1499 
is_first_func_insn(struct objtool_file * file,struct instruction * insn,struct symbol * sym)1500 static bool is_first_func_insn(struct objtool_file *file,
1501 			       struct instruction *insn, struct symbol *sym)
1502 {
1503 	if (insn->offset == sym->offset)
1504 		return true;
1505 
1506 	/* Allow direct CALL/JMP past ENDBR */
1507 	if (opts.ibt) {
1508 		struct instruction *prev = prev_insn_same_sym(file, insn);
1509 
1510 		if (prev && prev->type == INSN_ENDBR &&
1511 		    insn->offset == sym->offset + prev->len)
1512 			return true;
1513 	}
1514 
1515 	return false;
1516 }
1517 
1518 /*
1519  * A sibling call is a tail-call to another symbol -- to differentiate from a
1520  * recursive tail-call which is to the same symbol.
1521  */
jump_is_sibling_call(struct objtool_file * file,struct instruction * from,struct instruction * to)1522 static bool jump_is_sibling_call(struct objtool_file *file,
1523 				 struct instruction *from, struct instruction *to)
1524 {
1525 	struct symbol *fs = from->sym;
1526 	struct symbol *ts = to->sym;
1527 
1528 	/* Not a sibling call if from/to a symbol hole */
1529 	if (!fs || !ts)
1530 		return false;
1531 
1532 	/* Not a sibling call if not targeting the start of a symbol. */
1533 	if (!is_first_func_insn(file, to, ts))
1534 		return false;
1535 
1536 	/* Disallow sibling calls into STT_NOTYPE */
1537 	if (ts->type == STT_NOTYPE)
1538 		return false;
1539 
1540 	/* Must not be self to be a sibling */
1541 	return fs->pfunc != ts->pfunc;
1542 }
1543 
1544 /*
1545  * Find the destination instructions for all jumps.
1546  */
add_jump_destinations(struct objtool_file * file)1547 static int add_jump_destinations(struct objtool_file *file)
1548 {
1549 	struct instruction *insn, *jump_dest;
1550 	struct reloc *reloc;
1551 	struct section *dest_sec;
1552 	unsigned long dest_off;
1553 
1554 	for_each_insn(file, insn) {
1555 		struct symbol *func = insn_func(insn);
1556 
1557 		if (insn->jump_dest) {
1558 			/*
1559 			 * handle_group_alt() may have previously set
1560 			 * 'jump_dest' for some alternatives.
1561 			 */
1562 			continue;
1563 		}
1564 		if (!is_static_jump(insn))
1565 			continue;
1566 
1567 		reloc = insn_reloc(file, insn);
1568 		if (!reloc) {
1569 			dest_sec = insn->sec;
1570 			dest_off = arch_jump_destination(insn);
1571 		} else if (reloc->sym->type == STT_SECTION) {
1572 			dest_sec = reloc->sym->sec;
1573 			dest_off = arch_dest_reloc_offset(reloc_addend(reloc));
1574 		} else if (reloc->sym->retpoline_thunk) {
1575 			add_retpoline_call(file, insn);
1576 			continue;
1577 		} else if (reloc->sym->return_thunk) {
1578 			add_return_call(file, insn, true);
1579 			continue;
1580 		} else if (func) {
1581 			/*
1582 			 * External sibling call or internal sibling call with
1583 			 * STT_FUNC reloc.
1584 			 */
1585 			add_call_dest(file, insn, reloc->sym, true);
1586 			continue;
1587 		} else if (reloc->sym->sec->idx) {
1588 			dest_sec = reloc->sym->sec;
1589 			dest_off = reloc->sym->sym.st_value +
1590 				   arch_dest_reloc_offset(reloc_addend(reloc));
1591 		} else {
1592 			/* non-func asm code jumping to another file */
1593 			continue;
1594 		}
1595 
1596 		jump_dest = find_insn(file, dest_sec, dest_off);
1597 		if (!jump_dest) {
1598 			struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off);
1599 
1600 			/*
1601 			 * This is a special case for retbleed_untrain_ret().
1602 			 * It jumps to __x86_return_thunk(), but objtool
1603 			 * can't find the thunk's starting RET
1604 			 * instruction, because the RET is also in the
1605 			 * middle of another instruction.  Objtool only
1606 			 * knows about the outer instruction.
1607 			 */
1608 			if (sym && sym->embedded_insn) {
1609 				add_return_call(file, insn, false);
1610 				continue;
1611 			}
1612 
1613 			/*
1614 			 * GCOV/KCOV dead code can jump to the end of the
1615 			 * function/section.
1616 			 */
1617 			if (file->ignore_unreachables && func &&
1618 			    dest_sec == insn->sec &&
1619 			    dest_off == func->offset + func->len)
1620 				continue;
1621 
1622 			WARN_INSN(insn, "can't find jump dest instruction at %s+0x%lx",
1623 				  dest_sec->name, dest_off);
1624 			return -1;
1625 		}
1626 
1627 		/*
1628 		 * Cross-function jump.
1629 		 */
1630 		if (func && insn_func(jump_dest) && func != insn_func(jump_dest)) {
1631 
1632 			/*
1633 			 * For GCC 8+, create parent/child links for any cold
1634 			 * subfunctions.  This is _mostly_ redundant with a
1635 			 * similar initialization in read_symbols().
1636 			 *
1637 			 * If a function has aliases, we want the *first* such
1638 			 * function in the symbol table to be the subfunction's
1639 			 * parent.  In that case we overwrite the
1640 			 * initialization done in read_symbols().
1641 			 *
1642 			 * However this code can't completely replace the
1643 			 * read_symbols() code because this doesn't detect the
1644 			 * case where the parent function's only reference to a
1645 			 * subfunction is through a jump table.
1646 			 */
1647 			if (!strstr(func->name, ".cold") &&
1648 			    strstr(insn_func(jump_dest)->name, ".cold")) {
1649 				func->cfunc = insn_func(jump_dest);
1650 				insn_func(jump_dest)->pfunc = func;
1651 			}
1652 		}
1653 
1654 		if (jump_is_sibling_call(file, insn, jump_dest)) {
1655 			/*
1656 			 * Internal sibling call without reloc or with
1657 			 * STT_SECTION reloc.
1658 			 */
1659 			add_call_dest(file, insn, insn_func(jump_dest), true);
1660 			continue;
1661 		}
1662 
1663 		insn->jump_dest = jump_dest;
1664 	}
1665 
1666 	return 0;
1667 }
1668 
find_call_destination(struct section * sec,unsigned long offset)1669 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1670 {
1671 	struct symbol *call_dest;
1672 
1673 	call_dest = find_func_by_offset(sec, offset);
1674 	if (!call_dest)
1675 		call_dest = find_symbol_by_offset(sec, offset);
1676 
1677 	return call_dest;
1678 }
1679 
1680 /*
1681  * Find the destination instructions for all calls.
1682  */
add_call_destinations(struct objtool_file * file)1683 static int add_call_destinations(struct objtool_file *file)
1684 {
1685 	struct instruction *insn;
1686 	unsigned long dest_off;
1687 	struct symbol *dest;
1688 	struct reloc *reloc;
1689 
1690 	for_each_insn(file, insn) {
1691 		if (insn->type != INSN_CALL)
1692 			continue;
1693 
1694 		reloc = insn_reloc(file, insn);
1695 		if (!reloc) {
1696 			dest_off = arch_jump_destination(insn);
1697 			dest = find_call_destination(insn->sec, dest_off);
1698 
1699 			add_call_dest(file, insn, dest, false);
1700 
1701 			if (insn->ignore)
1702 				continue;
1703 
1704 			if (!insn_call_dest(insn)) {
1705 				WARN_INSN(insn, "unannotated intra-function call");
1706 				return -1;
1707 			}
1708 
1709 			if (insn_func(insn) && insn_call_dest(insn)->type != STT_FUNC) {
1710 				WARN_INSN(insn, "unsupported call to non-function");
1711 				return -1;
1712 			}
1713 
1714 		} else if (reloc->sym->type == STT_SECTION) {
1715 			dest_off = arch_dest_reloc_offset(reloc_addend(reloc));
1716 			dest = find_call_destination(reloc->sym->sec, dest_off);
1717 			if (!dest) {
1718 				WARN_INSN(insn, "can't find call dest symbol at %s+0x%lx",
1719 					  reloc->sym->sec->name, dest_off);
1720 				return -1;
1721 			}
1722 
1723 			add_call_dest(file, insn, dest, false);
1724 
1725 		} else if (reloc->sym->retpoline_thunk) {
1726 			add_retpoline_call(file, insn);
1727 
1728 		} else
1729 			add_call_dest(file, insn, reloc->sym, false);
1730 	}
1731 
1732 	return 0;
1733 }
1734 
1735 /*
1736  * The .alternatives section requires some extra special care over and above
1737  * other special sections because alternatives are patched in place.
1738  */
handle_group_alt(struct objtool_file * file,struct special_alt * special_alt,struct instruction * orig_insn,struct instruction ** new_insn)1739 static int handle_group_alt(struct objtool_file *file,
1740 			    struct special_alt *special_alt,
1741 			    struct instruction *orig_insn,
1742 			    struct instruction **new_insn)
1743 {
1744 	struct instruction *last_new_insn = NULL, *insn, *nop = NULL;
1745 	struct alt_group *orig_alt_group, *new_alt_group;
1746 	unsigned long dest_off;
1747 
1748 	orig_alt_group = orig_insn->alt_group;
1749 	if (!orig_alt_group) {
1750 		struct instruction *last_orig_insn = NULL;
1751 
1752 		orig_alt_group = malloc(sizeof(*orig_alt_group));
1753 		if (!orig_alt_group) {
1754 			WARN("malloc failed");
1755 			return -1;
1756 		}
1757 		orig_alt_group->cfi = calloc(special_alt->orig_len,
1758 					     sizeof(struct cfi_state *));
1759 		if (!orig_alt_group->cfi) {
1760 			WARN("calloc failed");
1761 			return -1;
1762 		}
1763 
1764 		insn = orig_insn;
1765 		sec_for_each_insn_from(file, insn) {
1766 			if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1767 				break;
1768 
1769 			insn->alt_group = orig_alt_group;
1770 			last_orig_insn = insn;
1771 		}
1772 		orig_alt_group->orig_group = NULL;
1773 		orig_alt_group->first_insn = orig_insn;
1774 		orig_alt_group->last_insn = last_orig_insn;
1775 		orig_alt_group->nop = NULL;
1776 	} else {
1777 		if (orig_alt_group->last_insn->offset + orig_alt_group->last_insn->len -
1778 		    orig_alt_group->first_insn->offset != special_alt->orig_len) {
1779 			WARN_INSN(orig_insn, "weirdly overlapping alternative! %ld != %d",
1780 				  orig_alt_group->last_insn->offset +
1781 				  orig_alt_group->last_insn->len -
1782 				  orig_alt_group->first_insn->offset,
1783 				  special_alt->orig_len);
1784 			return -1;
1785 		}
1786 	}
1787 
1788 	new_alt_group = malloc(sizeof(*new_alt_group));
1789 	if (!new_alt_group) {
1790 		WARN("malloc failed");
1791 		return -1;
1792 	}
1793 
1794 	if (special_alt->new_len < special_alt->orig_len) {
1795 		/*
1796 		 * Insert a fake nop at the end to make the replacement
1797 		 * alt_group the same size as the original.  This is needed to
1798 		 * allow propagate_alt_cfi() to do its magic.  When the last
1799 		 * instruction affects the stack, the instruction after it (the
1800 		 * nop) will propagate the new state to the shared CFI array.
1801 		 */
1802 		nop = malloc(sizeof(*nop));
1803 		if (!nop) {
1804 			WARN("malloc failed");
1805 			return -1;
1806 		}
1807 		memset(nop, 0, sizeof(*nop));
1808 
1809 		nop->sec = special_alt->new_sec;
1810 		nop->offset = special_alt->new_off + special_alt->new_len;
1811 		nop->len = special_alt->orig_len - special_alt->new_len;
1812 		nop->type = INSN_NOP;
1813 		nop->sym = orig_insn->sym;
1814 		nop->alt_group = new_alt_group;
1815 		nop->ignore = orig_insn->ignore_alts;
1816 	}
1817 
1818 	if (!special_alt->new_len) {
1819 		*new_insn = nop;
1820 		goto end;
1821 	}
1822 
1823 	insn = *new_insn;
1824 	sec_for_each_insn_from(file, insn) {
1825 		struct reloc *alt_reloc;
1826 
1827 		if (insn->offset >= special_alt->new_off + special_alt->new_len)
1828 			break;
1829 
1830 		last_new_insn = insn;
1831 
1832 		insn->ignore = orig_insn->ignore_alts;
1833 		insn->sym = orig_insn->sym;
1834 		insn->alt_group = new_alt_group;
1835 
1836 		/*
1837 		 * Since alternative replacement code is copy/pasted by the
1838 		 * kernel after applying relocations, generally such code can't
1839 		 * have relative-address relocation references to outside the
1840 		 * .altinstr_replacement section, unless the arch's
1841 		 * alternatives code can adjust the relative offsets
1842 		 * accordingly.
1843 		 */
1844 		alt_reloc = insn_reloc(file, insn);
1845 		if (alt_reloc && arch_pc_relative_reloc(alt_reloc) &&
1846 		    !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1847 
1848 			WARN_INSN(insn, "unsupported relocation in alternatives section");
1849 			return -1;
1850 		}
1851 
1852 		if (!is_static_jump(insn))
1853 			continue;
1854 
1855 		if (!insn->immediate)
1856 			continue;
1857 
1858 		dest_off = arch_jump_destination(insn);
1859 		if (dest_off == special_alt->new_off + special_alt->new_len) {
1860 			insn->jump_dest = next_insn_same_sec(file, orig_alt_group->last_insn);
1861 			if (!insn->jump_dest) {
1862 				WARN_INSN(insn, "can't find alternative jump destination");
1863 				return -1;
1864 			}
1865 		}
1866 	}
1867 
1868 	if (!last_new_insn) {
1869 		WARN_FUNC("can't find last new alternative instruction",
1870 			  special_alt->new_sec, special_alt->new_off);
1871 		return -1;
1872 	}
1873 
1874 end:
1875 	new_alt_group->orig_group = orig_alt_group;
1876 	new_alt_group->first_insn = *new_insn;
1877 	new_alt_group->last_insn = last_new_insn;
1878 	new_alt_group->nop = nop;
1879 	new_alt_group->cfi = orig_alt_group->cfi;
1880 	return 0;
1881 }
1882 
1883 /*
1884  * A jump table entry can either convert a nop to a jump or a jump to a nop.
1885  * If the original instruction is a jump, make the alt entry an effective nop
1886  * by just skipping the original instruction.
1887  */
handle_jump_alt(struct objtool_file * file,struct special_alt * special_alt,struct instruction * orig_insn,struct instruction ** new_insn)1888 static int handle_jump_alt(struct objtool_file *file,
1889 			   struct special_alt *special_alt,
1890 			   struct instruction *orig_insn,
1891 			   struct instruction **new_insn)
1892 {
1893 	if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1894 	    orig_insn->type != INSN_NOP) {
1895 
1896 		WARN_INSN(orig_insn, "unsupported instruction at jump label");
1897 		return -1;
1898 	}
1899 
1900 	if (opts.hack_jump_label && special_alt->key_addend & 2) {
1901 		struct reloc *reloc = insn_reloc(file, orig_insn);
1902 
1903 		if (reloc)
1904 			set_reloc_type(file->elf, reloc, R_NONE);
1905 		elf_write_insn(file->elf, orig_insn->sec,
1906 			       orig_insn->offset, orig_insn->len,
1907 			       arch_nop_insn(orig_insn->len));
1908 		orig_insn->type = INSN_NOP;
1909 	}
1910 
1911 	if (orig_insn->type == INSN_NOP) {
1912 		if (orig_insn->len == 2)
1913 			file->jl_nop_short++;
1914 		else
1915 			file->jl_nop_long++;
1916 
1917 		return 0;
1918 	}
1919 
1920 	if (orig_insn->len == 2)
1921 		file->jl_short++;
1922 	else
1923 		file->jl_long++;
1924 
1925 	*new_insn = next_insn_same_sec(file, orig_insn);
1926 	return 0;
1927 }
1928 
1929 /*
1930  * Read all the special sections which have alternate instructions which can be
1931  * patched in or redirected to at runtime.  Each instruction having alternate
1932  * instruction(s) has them added to its insn->alts list, which will be
1933  * traversed in validate_branch().
1934  */
add_special_section_alts(struct objtool_file * file)1935 static int add_special_section_alts(struct objtool_file *file)
1936 {
1937 	struct list_head special_alts;
1938 	struct instruction *orig_insn, *new_insn;
1939 	struct special_alt *special_alt, *tmp;
1940 	struct alternative *alt;
1941 	int ret;
1942 
1943 	ret = special_get_alts(file->elf, &special_alts);
1944 	if (ret)
1945 		return ret;
1946 
1947 	list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1948 
1949 		orig_insn = find_insn(file, special_alt->orig_sec,
1950 				      special_alt->orig_off);
1951 		if (!orig_insn) {
1952 			WARN_FUNC("special: can't find orig instruction",
1953 				  special_alt->orig_sec, special_alt->orig_off);
1954 			ret = -1;
1955 			goto out;
1956 		}
1957 
1958 		new_insn = NULL;
1959 		if (!special_alt->group || special_alt->new_len) {
1960 			new_insn = find_insn(file, special_alt->new_sec,
1961 					     special_alt->new_off);
1962 			if (!new_insn) {
1963 				WARN_FUNC("special: can't find new instruction",
1964 					  special_alt->new_sec,
1965 					  special_alt->new_off);
1966 				ret = -1;
1967 				goto out;
1968 			}
1969 		}
1970 
1971 		if (special_alt->group) {
1972 			if (!special_alt->orig_len) {
1973 				WARN_INSN(orig_insn, "empty alternative entry");
1974 				continue;
1975 			}
1976 
1977 			ret = handle_group_alt(file, special_alt, orig_insn,
1978 					       &new_insn);
1979 			if (ret)
1980 				goto out;
1981 		} else if (special_alt->jump_or_nop) {
1982 			ret = handle_jump_alt(file, special_alt, orig_insn,
1983 					      &new_insn);
1984 			if (ret)
1985 				goto out;
1986 		}
1987 
1988 		alt = malloc(sizeof(*alt));
1989 		if (!alt) {
1990 			WARN("malloc failed");
1991 			ret = -1;
1992 			goto out;
1993 		}
1994 
1995 		alt->insn = new_insn;
1996 		alt->skip_orig = special_alt->skip_orig;
1997 		orig_insn->ignore_alts |= special_alt->skip_alt;
1998 		alt->next = orig_insn->alts;
1999 		orig_insn->alts = alt;
2000 
2001 		list_del(&special_alt->list);
2002 		free(special_alt);
2003 	}
2004 
2005 	if (opts.stats) {
2006 		printf("jl\\\tNOP\tJMP\n");
2007 		printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
2008 		printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
2009 	}
2010 
2011 out:
2012 	return ret;
2013 }
2014 
add_jump_table(struct objtool_file * file,struct instruction * insn,struct reloc * next_table)2015 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
2016 			  struct reloc *next_table)
2017 {
2018 	struct symbol *pfunc = insn_func(insn)->pfunc;
2019 	struct reloc *table = insn_jump_table(insn);
2020 	struct instruction *dest_insn;
2021 	unsigned int prev_offset = 0;
2022 	struct reloc *reloc = table;
2023 	struct alternative *alt;
2024 
2025 	/*
2026 	 * Each @reloc is a switch table relocation which points to the target
2027 	 * instruction.
2028 	 */
2029 	for_each_reloc_from(table->sec, reloc) {
2030 
2031 		/* Check for the end of the table: */
2032 		if (reloc != table && reloc == next_table)
2033 			break;
2034 
2035 		/* Make sure the table entries are consecutive: */
2036 		if (prev_offset && reloc_offset(reloc) != prev_offset + 8)
2037 			break;
2038 
2039 		/* Detect function pointers from contiguous objects: */
2040 		if (reloc->sym->sec == pfunc->sec &&
2041 		    reloc_addend(reloc) == pfunc->offset)
2042 			break;
2043 
2044 		/*
2045 		 * Clang sometimes leaves dangling unused jump table entries
2046 		 * which point to the end of the function.  Ignore them.
2047 		 */
2048 		if (reloc->sym->sec == pfunc->sec &&
2049 		    reloc_addend(reloc) == pfunc->offset + pfunc->len)
2050 			goto next;
2051 
2052 		dest_insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
2053 		if (!dest_insn)
2054 			break;
2055 
2056 		/* Make sure the destination is in the same function: */
2057 		if (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc)
2058 			break;
2059 
2060 		alt = malloc(sizeof(*alt));
2061 		if (!alt) {
2062 			WARN("malloc failed");
2063 			return -1;
2064 		}
2065 
2066 		alt->insn = dest_insn;
2067 		alt->next = insn->alts;
2068 		insn->alts = alt;
2069 next:
2070 		prev_offset = reloc_offset(reloc);
2071 	}
2072 
2073 	if (!prev_offset) {
2074 		WARN_INSN(insn, "can't find switch jump table");
2075 		return -1;
2076 	}
2077 
2078 	return 0;
2079 }
2080 
2081 /*
2082  * find_jump_table() - Given a dynamic jump, find the switch jump table
2083  * associated with it.
2084  */
find_jump_table(struct objtool_file * file,struct symbol * func,struct instruction * insn)2085 static struct reloc *find_jump_table(struct objtool_file *file,
2086 				      struct symbol *func,
2087 				      struct instruction *insn)
2088 {
2089 	struct reloc *table_reloc;
2090 	struct instruction *dest_insn, *orig_insn = insn;
2091 
2092 	/*
2093 	 * Backward search using the @first_jump_src links, these help avoid
2094 	 * much of the 'in between' code. Which avoids us getting confused by
2095 	 * it.
2096 	 */
2097 	for (;
2098 	     insn && insn_func(insn) && insn_func(insn)->pfunc == func;
2099 	     insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
2100 
2101 		if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
2102 			break;
2103 
2104 		/* allow small jumps within the range */
2105 		if (insn->type == INSN_JUMP_UNCONDITIONAL &&
2106 		    insn->jump_dest &&
2107 		    (insn->jump_dest->offset <= insn->offset ||
2108 		     insn->jump_dest->offset > orig_insn->offset))
2109 		    break;
2110 
2111 		table_reloc = arch_find_switch_table(file, insn);
2112 		if (!table_reloc)
2113 			continue;
2114 		dest_insn = find_insn(file, table_reloc->sym->sec, reloc_addend(table_reloc));
2115 		if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func)
2116 			continue;
2117 
2118 		return table_reloc;
2119 	}
2120 
2121 	return NULL;
2122 }
2123 
2124 /*
2125  * First pass: Mark the head of each jump table so that in the next pass,
2126  * we know when a given jump table ends and the next one starts.
2127  */
mark_func_jump_tables(struct objtool_file * file,struct symbol * func)2128 static void mark_func_jump_tables(struct objtool_file *file,
2129 				    struct symbol *func)
2130 {
2131 	struct instruction *insn, *last = NULL;
2132 	struct reloc *reloc;
2133 
2134 	func_for_each_insn(file, func, insn) {
2135 		if (!last)
2136 			last = insn;
2137 
2138 		/*
2139 		 * Store back-pointers for unconditional forward jumps such
2140 		 * that find_jump_table() can back-track using those and
2141 		 * avoid some potentially confusing code.
2142 		 */
2143 		if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
2144 		    insn->offset > last->offset &&
2145 		    insn->jump_dest->offset > insn->offset &&
2146 		    !insn->jump_dest->first_jump_src) {
2147 
2148 			insn->jump_dest->first_jump_src = insn;
2149 			last = insn->jump_dest;
2150 		}
2151 
2152 		if (insn->type != INSN_JUMP_DYNAMIC)
2153 			continue;
2154 
2155 		reloc = find_jump_table(file, func, insn);
2156 		if (reloc)
2157 			insn->_jump_table = reloc;
2158 	}
2159 }
2160 
add_func_jump_tables(struct objtool_file * file,struct symbol * func)2161 static int add_func_jump_tables(struct objtool_file *file,
2162 				  struct symbol *func)
2163 {
2164 	struct instruction *insn, *insn_t1 = NULL, *insn_t2;
2165 	int ret = 0;
2166 
2167 	func_for_each_insn(file, func, insn) {
2168 		if (!insn_jump_table(insn))
2169 			continue;
2170 
2171 		if (!insn_t1) {
2172 			insn_t1 = insn;
2173 			continue;
2174 		}
2175 
2176 		insn_t2 = insn;
2177 
2178 		ret = add_jump_table(file, insn_t1, insn_jump_table(insn_t2));
2179 		if (ret)
2180 			return ret;
2181 
2182 		insn_t1 = insn_t2;
2183 	}
2184 
2185 	if (insn_t1)
2186 		ret = add_jump_table(file, insn_t1, NULL);
2187 
2188 	return ret;
2189 }
2190 
2191 /*
2192  * For some switch statements, gcc generates a jump table in the .rodata
2193  * section which contains a list of addresses within the function to jump to.
2194  * This finds these jump tables and adds them to the insn->alts lists.
2195  */
add_jump_table_alts(struct objtool_file * file)2196 static int add_jump_table_alts(struct objtool_file *file)
2197 {
2198 	struct symbol *func;
2199 	int ret;
2200 
2201 	if (!file->rodata)
2202 		return 0;
2203 
2204 	for_each_sym(file, func) {
2205 		if (func->type != STT_FUNC)
2206 			continue;
2207 
2208 		mark_func_jump_tables(file, func);
2209 		ret = add_func_jump_tables(file, func);
2210 		if (ret)
2211 			return ret;
2212 	}
2213 
2214 	return 0;
2215 }
2216 
set_func_state(struct cfi_state * state)2217 static void set_func_state(struct cfi_state *state)
2218 {
2219 	state->cfa = initial_func_cfi.cfa;
2220 	memcpy(&state->regs, &initial_func_cfi.regs,
2221 	       CFI_NUM_REGS * sizeof(struct cfi_reg));
2222 	state->stack_size = initial_func_cfi.cfa.offset;
2223 	state->type = UNWIND_HINT_TYPE_CALL;
2224 }
2225 
read_unwind_hints(struct objtool_file * file)2226 static int read_unwind_hints(struct objtool_file *file)
2227 {
2228 	struct cfi_state cfi = init_cfi;
2229 	struct section *sec;
2230 	struct unwind_hint *hint;
2231 	struct instruction *insn;
2232 	struct reloc *reloc;
2233 	int i;
2234 
2235 	sec = find_section_by_name(file->elf, ".discard.unwind_hints");
2236 	if (!sec)
2237 		return 0;
2238 
2239 	if (!sec->rsec) {
2240 		WARN("missing .rela.discard.unwind_hints section");
2241 		return -1;
2242 	}
2243 
2244 	if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
2245 		WARN("struct unwind_hint size mismatch");
2246 		return -1;
2247 	}
2248 
2249 	file->hints = true;
2250 
2251 	for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
2252 		hint = (struct unwind_hint *)sec->data->d_buf + i;
2253 
2254 		reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
2255 		if (!reloc) {
2256 			WARN("can't find reloc for unwind_hints[%d]", i);
2257 			return -1;
2258 		}
2259 
2260 		insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
2261 		if (!insn) {
2262 			WARN("can't find insn for unwind_hints[%d]", i);
2263 			return -1;
2264 		}
2265 
2266 		insn->hint = true;
2267 
2268 		if (hint->type == UNWIND_HINT_TYPE_UNDEFINED) {
2269 			insn->cfi = &force_undefined_cfi;
2270 			continue;
2271 		}
2272 
2273 		if (hint->type == UNWIND_HINT_TYPE_SAVE) {
2274 			insn->hint = false;
2275 			insn->save = true;
2276 			continue;
2277 		}
2278 
2279 		if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
2280 			insn->restore = true;
2281 			continue;
2282 		}
2283 
2284 		if (hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) {
2285 			struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset);
2286 
2287 			if (sym && sym->bind == STB_GLOBAL) {
2288 				if (opts.ibt && insn->type != INSN_ENDBR && !insn->noendbr) {
2289 					WARN_INSN(insn, "UNWIND_HINT_IRET_REGS without ENDBR");
2290 				}
2291 			}
2292 		}
2293 
2294 		if (hint->type == UNWIND_HINT_TYPE_FUNC) {
2295 			insn->cfi = &func_cfi;
2296 			continue;
2297 		}
2298 
2299 		if (insn->cfi)
2300 			cfi = *(insn->cfi);
2301 
2302 		if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
2303 			WARN_INSN(insn, "unsupported unwind_hint sp base reg %d", hint->sp_reg);
2304 			return -1;
2305 		}
2306 
2307 		cfi.cfa.offset = bswap_if_needed(file->elf, hint->sp_offset);
2308 		cfi.type = hint->type;
2309 		cfi.signal = hint->signal;
2310 
2311 		insn->cfi = cfi_hash_find_or_add(&cfi);
2312 	}
2313 
2314 	return 0;
2315 }
2316 
read_noendbr_hints(struct objtool_file * file)2317 static int read_noendbr_hints(struct objtool_file *file)
2318 {
2319 	struct instruction *insn;
2320 	struct section *rsec;
2321 	struct reloc *reloc;
2322 
2323 	rsec = find_section_by_name(file->elf, ".rela.discard.noendbr");
2324 	if (!rsec)
2325 		return 0;
2326 
2327 	for_each_reloc(rsec, reloc) {
2328 		insn = find_insn(file, reloc->sym->sec,
2329 				 reloc->sym->offset + reloc_addend(reloc));
2330 		if (!insn) {
2331 			WARN("bad .discard.noendbr entry");
2332 			return -1;
2333 		}
2334 
2335 		insn->noendbr = 1;
2336 	}
2337 
2338 	return 0;
2339 }
2340 
read_retpoline_hints(struct objtool_file * file)2341 static int read_retpoline_hints(struct objtool_file *file)
2342 {
2343 	struct section *rsec;
2344 	struct instruction *insn;
2345 	struct reloc *reloc;
2346 
2347 	rsec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
2348 	if (!rsec)
2349 		return 0;
2350 
2351 	for_each_reloc(rsec, reloc) {
2352 		if (reloc->sym->type != STT_SECTION) {
2353 			WARN("unexpected relocation symbol type in %s", rsec->name);
2354 			return -1;
2355 		}
2356 
2357 		insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
2358 		if (!insn) {
2359 			WARN("bad .discard.retpoline_safe entry");
2360 			return -1;
2361 		}
2362 
2363 		if (insn->type != INSN_JUMP_DYNAMIC &&
2364 		    insn->type != INSN_CALL_DYNAMIC &&
2365 		    insn->type != INSN_RETURN &&
2366 		    insn->type != INSN_NOP) {
2367 			WARN_INSN(insn, "retpoline_safe hint not an indirect jump/call/ret/nop");
2368 			return -1;
2369 		}
2370 
2371 		insn->retpoline_safe = true;
2372 	}
2373 
2374 	return 0;
2375 }
2376 
read_instr_hints(struct objtool_file * file)2377 static int read_instr_hints(struct objtool_file *file)
2378 {
2379 	struct section *rsec;
2380 	struct instruction *insn;
2381 	struct reloc *reloc;
2382 
2383 	rsec = find_section_by_name(file->elf, ".rela.discard.instr_end");
2384 	if (!rsec)
2385 		return 0;
2386 
2387 	for_each_reloc(rsec, reloc) {
2388 		if (reloc->sym->type != STT_SECTION) {
2389 			WARN("unexpected relocation symbol type in %s", rsec->name);
2390 			return -1;
2391 		}
2392 
2393 		insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
2394 		if (!insn) {
2395 			WARN("bad .discard.instr_end entry");
2396 			return -1;
2397 		}
2398 
2399 		insn->instr--;
2400 	}
2401 
2402 	rsec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
2403 	if (!rsec)
2404 		return 0;
2405 
2406 	for_each_reloc(rsec, reloc) {
2407 		if (reloc->sym->type != STT_SECTION) {
2408 			WARN("unexpected relocation symbol type in %s", rsec->name);
2409 			return -1;
2410 		}
2411 
2412 		insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
2413 		if (!insn) {
2414 			WARN("bad .discard.instr_begin entry");
2415 			return -1;
2416 		}
2417 
2418 		insn->instr++;
2419 	}
2420 
2421 	return 0;
2422 }
2423 
read_validate_unret_hints(struct objtool_file * file)2424 static int read_validate_unret_hints(struct objtool_file *file)
2425 {
2426 	struct section *rsec;
2427 	struct instruction *insn;
2428 	struct reloc *reloc;
2429 
2430 	rsec = find_section_by_name(file->elf, ".rela.discard.validate_unret");
2431 	if (!rsec)
2432 		return 0;
2433 
2434 	for_each_reloc(rsec, reloc) {
2435 		if (reloc->sym->type != STT_SECTION) {
2436 			WARN("unexpected relocation symbol type in %s", rsec->name);
2437 			return -1;
2438 		}
2439 
2440 		insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
2441 		if (!insn) {
2442 			WARN("bad .discard.instr_end entry");
2443 			return -1;
2444 		}
2445 		insn->unret = 1;
2446 	}
2447 
2448 	return 0;
2449 }
2450 
2451 
read_intra_function_calls(struct objtool_file * file)2452 static int read_intra_function_calls(struct objtool_file *file)
2453 {
2454 	struct instruction *insn;
2455 	struct section *rsec;
2456 	struct reloc *reloc;
2457 
2458 	rsec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
2459 	if (!rsec)
2460 		return 0;
2461 
2462 	for_each_reloc(rsec, reloc) {
2463 		unsigned long dest_off;
2464 
2465 		if (reloc->sym->type != STT_SECTION) {
2466 			WARN("unexpected relocation symbol type in %s",
2467 			     rsec->name);
2468 			return -1;
2469 		}
2470 
2471 		insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
2472 		if (!insn) {
2473 			WARN("bad .discard.intra_function_call entry");
2474 			return -1;
2475 		}
2476 
2477 		if (insn->type != INSN_CALL) {
2478 			WARN_INSN(insn, "intra_function_call not a direct call");
2479 			return -1;
2480 		}
2481 
2482 		/*
2483 		 * Treat intra-function CALLs as JMPs, but with a stack_op.
2484 		 * See add_call_destinations(), which strips stack_ops from
2485 		 * normal CALLs.
2486 		 */
2487 		insn->type = INSN_JUMP_UNCONDITIONAL;
2488 
2489 		dest_off = arch_jump_destination(insn);
2490 		insn->jump_dest = find_insn(file, insn->sec, dest_off);
2491 		if (!insn->jump_dest) {
2492 			WARN_INSN(insn, "can't find call dest at %s+0x%lx",
2493 				  insn->sec->name, dest_off);
2494 			return -1;
2495 		}
2496 	}
2497 
2498 	return 0;
2499 }
2500 
2501 /*
2502  * Return true if name matches an instrumentation function, where calls to that
2503  * function from noinstr code can safely be removed, but compilers won't do so.
2504  */
is_profiling_func(const char * name)2505 static bool is_profiling_func(const char *name)
2506 {
2507 	/*
2508 	 * Many compilers cannot disable KCOV with a function attribute.
2509 	 */
2510 	if (!strncmp(name, "__sanitizer_cov_", 16))
2511 		return true;
2512 
2513 	/*
2514 	 * Some compilers currently do not remove __tsan_func_entry/exit nor
2515 	 * __tsan_atomic_signal_fence (used for barrier instrumentation) with
2516 	 * the __no_sanitize_thread attribute, remove them. Once the kernel's
2517 	 * minimum Clang version is 14.0, this can be removed.
2518 	 */
2519 	if (!strncmp(name, "__tsan_func_", 12) ||
2520 	    !strcmp(name, "__tsan_atomic_signal_fence"))
2521 		return true;
2522 
2523 	return false;
2524 }
2525 
classify_symbols(struct objtool_file * file)2526 static int classify_symbols(struct objtool_file *file)
2527 {
2528 	struct symbol *func;
2529 
2530 	for_each_sym(file, func) {
2531 		if (func->bind != STB_GLOBAL)
2532 			continue;
2533 
2534 		if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
2535 			     strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2536 			func->static_call_tramp = true;
2537 
2538 		if (arch_is_retpoline(func))
2539 			func->retpoline_thunk = true;
2540 
2541 		if (arch_is_rethunk(func))
2542 			func->return_thunk = true;
2543 
2544 		if (arch_is_embedded_insn(func))
2545 			func->embedded_insn = true;
2546 
2547 		if (arch_ftrace_match(func->name))
2548 			func->fentry = true;
2549 
2550 		if (is_profiling_func(func->name))
2551 			func->profiling_func = true;
2552 	}
2553 
2554 	return 0;
2555 }
2556 
mark_rodata(struct objtool_file * file)2557 static void mark_rodata(struct objtool_file *file)
2558 {
2559 	struct section *sec;
2560 	bool found = false;
2561 
2562 	/*
2563 	 * Search for the following rodata sections, each of which can
2564 	 * potentially contain jump tables:
2565 	 *
2566 	 * - .rodata: can contain GCC switch tables
2567 	 * - .rodata.<func>: same, if -fdata-sections is being used
2568 	 * - .rodata..c_jump_table: contains C annotated jump tables
2569 	 *
2570 	 * .rodata.str1.* sections are ignored; they don't contain jump tables.
2571 	 */
2572 	for_each_sec(file, sec) {
2573 		if (!strncmp(sec->name, ".rodata", 7) &&
2574 		    !strstr(sec->name, ".str1.")) {
2575 			sec->rodata = true;
2576 			found = true;
2577 		}
2578 	}
2579 
2580 	file->rodata = found;
2581 }
2582 
decode_sections(struct objtool_file * file)2583 static int decode_sections(struct objtool_file *file)
2584 {
2585 	int ret;
2586 
2587 	mark_rodata(file);
2588 
2589 	ret = init_pv_ops(file);
2590 	if (ret)
2591 		return ret;
2592 
2593 	/*
2594 	 * Must be before add_{jump_call}_destination.
2595 	 */
2596 	ret = classify_symbols(file);
2597 	if (ret)
2598 		return ret;
2599 
2600 	ret = decode_instructions(file);
2601 	if (ret)
2602 		return ret;
2603 
2604 	add_ignores(file);
2605 	add_uaccess_safe(file);
2606 
2607 	ret = add_ignore_alternatives(file);
2608 	if (ret)
2609 		return ret;
2610 
2611 	/*
2612 	 * Must be before read_unwind_hints() since that needs insn->noendbr.
2613 	 */
2614 	ret = read_noendbr_hints(file);
2615 	if (ret)
2616 		return ret;
2617 
2618 	/*
2619 	 * Must be before add_jump_destinations(), which depends on 'func'
2620 	 * being set for alternatives, to enable proper sibling call detection.
2621 	 */
2622 	if (opts.stackval || opts.orc || opts.uaccess || opts.noinstr) {
2623 		ret = add_special_section_alts(file);
2624 		if (ret)
2625 			return ret;
2626 	}
2627 
2628 	ret = add_jump_destinations(file);
2629 	if (ret)
2630 		return ret;
2631 
2632 	/*
2633 	 * Must be before add_call_destination(); it changes INSN_CALL to
2634 	 * INSN_JUMP.
2635 	 */
2636 	ret = read_intra_function_calls(file);
2637 	if (ret)
2638 		return ret;
2639 
2640 	ret = add_call_destinations(file);
2641 	if (ret)
2642 		return ret;
2643 
2644 	/*
2645 	 * Must be after add_call_destinations() such that it can override
2646 	 * dead_end_function() marks.
2647 	 */
2648 	ret = add_dead_ends(file);
2649 	if (ret)
2650 		return ret;
2651 
2652 	ret = add_jump_table_alts(file);
2653 	if (ret)
2654 		return ret;
2655 
2656 	ret = read_unwind_hints(file);
2657 	if (ret)
2658 		return ret;
2659 
2660 	ret = read_retpoline_hints(file);
2661 	if (ret)
2662 		return ret;
2663 
2664 	ret = read_instr_hints(file);
2665 	if (ret)
2666 		return ret;
2667 
2668 	ret = read_validate_unret_hints(file);
2669 	if (ret)
2670 		return ret;
2671 
2672 	return 0;
2673 }
2674 
is_special_call(struct instruction * insn)2675 static bool is_special_call(struct instruction *insn)
2676 {
2677 	if (insn->type == INSN_CALL) {
2678 		struct symbol *dest = insn_call_dest(insn);
2679 
2680 		if (!dest)
2681 			return false;
2682 
2683 		if (dest->fentry || dest->embedded_insn)
2684 			return true;
2685 	}
2686 
2687 	return false;
2688 }
2689 
has_modified_stack_frame(struct instruction * insn,struct insn_state * state)2690 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
2691 {
2692 	struct cfi_state *cfi = &state->cfi;
2693 	int i;
2694 
2695 	if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
2696 		return true;
2697 
2698 	if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
2699 		return true;
2700 
2701 	if (cfi->stack_size != initial_func_cfi.cfa.offset)
2702 		return true;
2703 
2704 	for (i = 0; i < CFI_NUM_REGS; i++) {
2705 		if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2706 		    cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
2707 			return true;
2708 	}
2709 
2710 	return false;
2711 }
2712 
check_reg_frame_pos(const struct cfi_reg * reg,int expected_offset)2713 static bool check_reg_frame_pos(const struct cfi_reg *reg,
2714 				int expected_offset)
2715 {
2716 	return reg->base == CFI_CFA &&
2717 	       reg->offset == expected_offset;
2718 }
2719 
has_valid_stack_frame(struct insn_state * state)2720 static bool has_valid_stack_frame(struct insn_state *state)
2721 {
2722 	struct cfi_state *cfi = &state->cfi;
2723 
2724 	if (cfi->cfa.base == CFI_BP &&
2725 	    check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2726 	    check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
2727 		return true;
2728 
2729 	if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
2730 		return true;
2731 
2732 	return false;
2733 }
2734 
update_cfi_state_regs(struct instruction * insn,struct cfi_state * cfi,struct stack_op * op)2735 static int update_cfi_state_regs(struct instruction *insn,
2736 				  struct cfi_state *cfi,
2737 				  struct stack_op *op)
2738 {
2739 	struct cfi_reg *cfa = &cfi->cfa;
2740 
2741 	if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
2742 		return 0;
2743 
2744 	/* push */
2745 	if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
2746 		cfa->offset += 8;
2747 
2748 	/* pop */
2749 	if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
2750 		cfa->offset -= 8;
2751 
2752 	/* add immediate to sp */
2753 	if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2754 	    op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2755 		cfa->offset -= op->src.offset;
2756 
2757 	return 0;
2758 }
2759 
save_reg(struct cfi_state * cfi,unsigned char reg,int base,int offset)2760 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2761 {
2762 	if (arch_callee_saved_reg(reg) &&
2763 	    cfi->regs[reg].base == CFI_UNDEFINED) {
2764 		cfi->regs[reg].base = base;
2765 		cfi->regs[reg].offset = offset;
2766 	}
2767 }
2768 
restore_reg(struct cfi_state * cfi,unsigned char reg)2769 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2770 {
2771 	cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2772 	cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2773 }
2774 
2775 /*
2776  * A note about DRAP stack alignment:
2777  *
2778  * GCC has the concept of a DRAP register, which is used to help keep track of
2779  * the stack pointer when aligning the stack.  r10 or r13 is used as the DRAP
2780  * register.  The typical DRAP pattern is:
2781  *
2782  *   4c 8d 54 24 08		lea    0x8(%rsp),%r10
2783  *   48 83 e4 c0		and    $0xffffffffffffffc0,%rsp
2784  *   41 ff 72 f8		pushq  -0x8(%r10)
2785  *   55				push   %rbp
2786  *   48 89 e5			mov    %rsp,%rbp
2787  *				(more pushes)
2788  *   41 52			push   %r10
2789  *				...
2790  *   41 5a			pop    %r10
2791  *				(more pops)
2792  *   5d				pop    %rbp
2793  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
2794  *   c3				retq
2795  *
2796  * There are some variations in the epilogues, like:
2797  *
2798  *   5b				pop    %rbx
2799  *   41 5a			pop    %r10
2800  *   41 5c			pop    %r12
2801  *   41 5d			pop    %r13
2802  *   41 5e			pop    %r14
2803  *   c9				leaveq
2804  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
2805  *   c3				retq
2806  *
2807  * and:
2808  *
2809  *   4c 8b 55 e8		mov    -0x18(%rbp),%r10
2810  *   48 8b 5d e0		mov    -0x20(%rbp),%rbx
2811  *   4c 8b 65 f0		mov    -0x10(%rbp),%r12
2812  *   4c 8b 6d f8		mov    -0x8(%rbp),%r13
2813  *   c9				leaveq
2814  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
2815  *   c3				retq
2816  *
2817  * Sometimes r13 is used as the DRAP register, in which case it's saved and
2818  * restored beforehand:
2819  *
2820  *   41 55			push   %r13
2821  *   4c 8d 6c 24 10		lea    0x10(%rsp),%r13
2822  *   48 83 e4 f0		and    $0xfffffffffffffff0,%rsp
2823  *				...
2824  *   49 8d 65 f0		lea    -0x10(%r13),%rsp
2825  *   41 5d			pop    %r13
2826  *   c3				retq
2827  */
update_cfi_state(struct instruction * insn,struct instruction * next_insn,struct cfi_state * cfi,struct stack_op * op)2828 static int update_cfi_state(struct instruction *insn,
2829 			    struct instruction *next_insn,
2830 			    struct cfi_state *cfi, struct stack_op *op)
2831 {
2832 	struct cfi_reg *cfa = &cfi->cfa;
2833 	struct cfi_reg *regs = cfi->regs;
2834 
2835 	/* ignore UNWIND_HINT_UNDEFINED regions */
2836 	if (cfi->force_undefined)
2837 		return 0;
2838 
2839 	/* stack operations don't make sense with an undefined CFA */
2840 	if (cfa->base == CFI_UNDEFINED) {
2841 		if (insn_func(insn)) {
2842 			WARN_INSN(insn, "undefined stack state");
2843 			return -1;
2844 		}
2845 		return 0;
2846 	}
2847 
2848 	if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2849 	    cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2850 		return update_cfi_state_regs(insn, cfi, op);
2851 
2852 	switch (op->dest.type) {
2853 
2854 	case OP_DEST_REG:
2855 		switch (op->src.type) {
2856 
2857 		case OP_SRC_REG:
2858 			if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2859 			    cfa->base == CFI_SP &&
2860 			    check_reg_frame_pos(&regs[CFI_BP], -cfa->offset)) {
2861 
2862 				/* mov %rsp, %rbp */
2863 				cfa->base = op->dest.reg;
2864 				cfi->bp_scratch = false;
2865 			}
2866 
2867 			else if (op->src.reg == CFI_SP &&
2868 				 op->dest.reg == CFI_BP && cfi->drap) {
2869 
2870 				/* drap: mov %rsp, %rbp */
2871 				regs[CFI_BP].base = CFI_BP;
2872 				regs[CFI_BP].offset = -cfi->stack_size;
2873 				cfi->bp_scratch = false;
2874 			}
2875 
2876 			else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2877 
2878 				/*
2879 				 * mov %rsp, %reg
2880 				 *
2881 				 * This is needed for the rare case where GCC
2882 				 * does:
2883 				 *
2884 				 *   mov    %rsp, %rax
2885 				 *   ...
2886 				 *   mov    %rax, %rsp
2887 				 */
2888 				cfi->vals[op->dest.reg].base = CFI_CFA;
2889 				cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2890 			}
2891 
2892 			else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2893 				 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2894 
2895 				/*
2896 				 * mov %rbp, %rsp
2897 				 *
2898 				 * Restore the original stack pointer (Clang).
2899 				 */
2900 				cfi->stack_size = -cfi->regs[CFI_BP].offset;
2901 			}
2902 
2903 			else if (op->dest.reg == cfa->base) {
2904 
2905 				/* mov %reg, %rsp */
2906 				if (cfa->base == CFI_SP &&
2907 				    cfi->vals[op->src.reg].base == CFI_CFA) {
2908 
2909 					/*
2910 					 * This is needed for the rare case
2911 					 * where GCC does something dumb like:
2912 					 *
2913 					 *   lea    0x8(%rsp), %rcx
2914 					 *   ...
2915 					 *   mov    %rcx, %rsp
2916 					 */
2917 					cfa->offset = -cfi->vals[op->src.reg].offset;
2918 					cfi->stack_size = cfa->offset;
2919 
2920 				} else if (cfa->base == CFI_SP &&
2921 					   cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2922 					   cfi->vals[op->src.reg].offset == cfa->offset) {
2923 
2924 					/*
2925 					 * Stack swizzle:
2926 					 *
2927 					 * 1: mov %rsp, (%[tos])
2928 					 * 2: mov %[tos], %rsp
2929 					 *    ...
2930 					 * 3: pop %rsp
2931 					 *
2932 					 * Where:
2933 					 *
2934 					 * 1 - places a pointer to the previous
2935 					 *     stack at the Top-of-Stack of the
2936 					 *     new stack.
2937 					 *
2938 					 * 2 - switches to the new stack.
2939 					 *
2940 					 * 3 - pops the Top-of-Stack to restore
2941 					 *     the original stack.
2942 					 *
2943 					 * Note: we set base to SP_INDIRECT
2944 					 * here and preserve offset. Therefore
2945 					 * when the unwinder reaches ToS it
2946 					 * will dereference SP and then add the
2947 					 * offset to find the next frame, IOW:
2948 					 * (%rsp) + offset.
2949 					 */
2950 					cfa->base = CFI_SP_INDIRECT;
2951 
2952 				} else {
2953 					cfa->base = CFI_UNDEFINED;
2954 					cfa->offset = 0;
2955 				}
2956 			}
2957 
2958 			else if (op->dest.reg == CFI_SP &&
2959 				 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2960 				 cfi->vals[op->src.reg].offset == cfa->offset) {
2961 
2962 				/*
2963 				 * The same stack swizzle case 2) as above. But
2964 				 * because we can't change cfa->base, case 3)
2965 				 * will become a regular POP. Pretend we're a
2966 				 * PUSH so things don't go unbalanced.
2967 				 */
2968 				cfi->stack_size += 8;
2969 			}
2970 
2971 
2972 			break;
2973 
2974 		case OP_SRC_ADD:
2975 			if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2976 
2977 				/* add imm, %rsp */
2978 				cfi->stack_size -= op->src.offset;
2979 				if (cfa->base == CFI_SP)
2980 					cfa->offset -= op->src.offset;
2981 				break;
2982 			}
2983 
2984 			if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2985 
2986 				/* lea disp(%rbp), %rsp */
2987 				cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2988 				break;
2989 			}
2990 
2991 			if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2992 
2993 				/* drap: lea disp(%rsp), %drap */
2994 				cfi->drap_reg = op->dest.reg;
2995 
2996 				/*
2997 				 * lea disp(%rsp), %reg
2998 				 *
2999 				 * This is needed for the rare case where GCC
3000 				 * does something dumb like:
3001 				 *
3002 				 *   lea    0x8(%rsp), %rcx
3003 				 *   ...
3004 				 *   mov    %rcx, %rsp
3005 				 */
3006 				cfi->vals[op->dest.reg].base = CFI_CFA;
3007 				cfi->vals[op->dest.reg].offset = \
3008 					-cfi->stack_size + op->src.offset;
3009 
3010 				break;
3011 			}
3012 
3013 			if (cfi->drap && op->dest.reg == CFI_SP &&
3014 			    op->src.reg == cfi->drap_reg) {
3015 
3016 				 /* drap: lea disp(%drap), %rsp */
3017 				cfa->base = CFI_SP;
3018 				cfa->offset = cfi->stack_size = -op->src.offset;
3019 				cfi->drap_reg = CFI_UNDEFINED;
3020 				cfi->drap = false;
3021 				break;
3022 			}
3023 
3024 			if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
3025 				WARN_INSN(insn, "unsupported stack register modification");
3026 				return -1;
3027 			}
3028 
3029 			break;
3030 
3031 		case OP_SRC_AND:
3032 			if (op->dest.reg != CFI_SP ||
3033 			    (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
3034 			    (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
3035 				WARN_INSN(insn, "unsupported stack pointer realignment");
3036 				return -1;
3037 			}
3038 
3039 			if (cfi->drap_reg != CFI_UNDEFINED) {
3040 				/* drap: and imm, %rsp */
3041 				cfa->base = cfi->drap_reg;
3042 				cfa->offset = cfi->stack_size = 0;
3043 				cfi->drap = true;
3044 			}
3045 
3046 			/*
3047 			 * Older versions of GCC (4.8ish) realign the stack
3048 			 * without DRAP, with a frame pointer.
3049 			 */
3050 
3051 			break;
3052 
3053 		case OP_SRC_POP:
3054 		case OP_SRC_POPF:
3055 			if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
3056 
3057 				/* pop %rsp; # restore from a stack swizzle */
3058 				cfa->base = CFI_SP;
3059 				break;
3060 			}
3061 
3062 			if (!cfi->drap && op->dest.reg == cfa->base) {
3063 
3064 				/* pop %rbp */
3065 				cfa->base = CFI_SP;
3066 			}
3067 
3068 			if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
3069 			    op->dest.reg == cfi->drap_reg &&
3070 			    cfi->drap_offset == -cfi->stack_size) {
3071 
3072 				/* drap: pop %drap */
3073 				cfa->base = cfi->drap_reg;
3074 				cfa->offset = 0;
3075 				cfi->drap_offset = -1;
3076 
3077 			} else if (cfi->stack_size == -regs[op->dest.reg].offset) {
3078 
3079 				/* pop %reg */
3080 				restore_reg(cfi, op->dest.reg);
3081 			}
3082 
3083 			cfi->stack_size -= 8;
3084 			if (cfa->base == CFI_SP)
3085 				cfa->offset -= 8;
3086 
3087 			break;
3088 
3089 		case OP_SRC_REG_INDIRECT:
3090 			if (!cfi->drap && op->dest.reg == cfa->base &&
3091 			    op->dest.reg == CFI_BP) {
3092 
3093 				/* mov disp(%rsp), %rbp */
3094 				cfa->base = CFI_SP;
3095 				cfa->offset = cfi->stack_size;
3096 			}
3097 
3098 			if (cfi->drap && op->src.reg == CFI_BP &&
3099 			    op->src.offset == cfi->drap_offset) {
3100 
3101 				/* drap: mov disp(%rbp), %drap */
3102 				cfa->base = cfi->drap_reg;
3103 				cfa->offset = 0;
3104 				cfi->drap_offset = -1;
3105 			}
3106 
3107 			if (cfi->drap && op->src.reg == CFI_BP &&
3108 			    op->src.offset == regs[op->dest.reg].offset) {
3109 
3110 				/* drap: mov disp(%rbp), %reg */
3111 				restore_reg(cfi, op->dest.reg);
3112 
3113 			} else if (op->src.reg == cfa->base &&
3114 			    op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
3115 
3116 				/* mov disp(%rbp), %reg */
3117 				/* mov disp(%rsp), %reg */
3118 				restore_reg(cfi, op->dest.reg);
3119 
3120 			} else if (op->src.reg == CFI_SP &&
3121 				   op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
3122 
3123 				/* mov disp(%rsp), %reg */
3124 				restore_reg(cfi, op->dest.reg);
3125 			}
3126 
3127 			break;
3128 
3129 		default:
3130 			WARN_INSN(insn, "unknown stack-related instruction");
3131 			return -1;
3132 		}
3133 
3134 		break;
3135 
3136 	case OP_DEST_PUSH:
3137 	case OP_DEST_PUSHF:
3138 		cfi->stack_size += 8;
3139 		if (cfa->base == CFI_SP)
3140 			cfa->offset += 8;
3141 
3142 		if (op->src.type != OP_SRC_REG)
3143 			break;
3144 
3145 		if (cfi->drap) {
3146 			if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
3147 
3148 				/* drap: push %drap */
3149 				cfa->base = CFI_BP_INDIRECT;
3150 				cfa->offset = -cfi->stack_size;
3151 
3152 				/* save drap so we know when to restore it */
3153 				cfi->drap_offset = -cfi->stack_size;
3154 
3155 			} else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
3156 
3157 				/* drap: push %rbp */
3158 				cfi->stack_size = 0;
3159 
3160 			} else {
3161 
3162 				/* drap: push %reg */
3163 				save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
3164 			}
3165 
3166 		} else {
3167 
3168 			/* push %reg */
3169 			save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
3170 		}
3171 
3172 		/* detect when asm code uses rbp as a scratch register */
3173 		if (opts.stackval && insn_func(insn) && op->src.reg == CFI_BP &&
3174 		    cfa->base != CFI_BP)
3175 			cfi->bp_scratch = true;
3176 		break;
3177 
3178 	case OP_DEST_REG_INDIRECT:
3179 
3180 		if (cfi->drap) {
3181 			if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
3182 
3183 				/* drap: mov %drap, disp(%rbp) */
3184 				cfa->base = CFI_BP_INDIRECT;
3185 				cfa->offset = op->dest.offset;
3186 
3187 				/* save drap offset so we know when to restore it */
3188 				cfi->drap_offset = op->dest.offset;
3189 			} else {
3190 
3191 				/* drap: mov reg, disp(%rbp) */
3192 				save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
3193 			}
3194 
3195 		} else if (op->dest.reg == cfa->base) {
3196 
3197 			/* mov reg, disp(%rbp) */
3198 			/* mov reg, disp(%rsp) */
3199 			save_reg(cfi, op->src.reg, CFI_CFA,
3200 				 op->dest.offset - cfi->cfa.offset);
3201 
3202 		} else if (op->dest.reg == CFI_SP) {
3203 
3204 			/* mov reg, disp(%rsp) */
3205 			save_reg(cfi, op->src.reg, CFI_CFA,
3206 				 op->dest.offset - cfi->stack_size);
3207 
3208 		} else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
3209 
3210 			/* mov %rsp, (%reg); # setup a stack swizzle. */
3211 			cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
3212 			cfi->vals[op->dest.reg].offset = cfa->offset;
3213 		}
3214 
3215 		break;
3216 
3217 	case OP_DEST_MEM:
3218 		if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
3219 			WARN_INSN(insn, "unknown stack-related memory operation");
3220 			return -1;
3221 		}
3222 
3223 		/* pop mem */
3224 		cfi->stack_size -= 8;
3225 		if (cfa->base == CFI_SP)
3226 			cfa->offset -= 8;
3227 
3228 		break;
3229 
3230 	default:
3231 		WARN_INSN(insn, "unknown stack-related instruction");
3232 		return -1;
3233 	}
3234 
3235 	return 0;
3236 }
3237 
3238 /*
3239  * The stack layouts of alternatives instructions can sometimes diverge when
3240  * they have stack modifications.  That's fine as long as the potential stack
3241  * layouts don't conflict at any given potential instruction boundary.
3242  *
3243  * Flatten the CFIs of the different alternative code streams (both original
3244  * and replacement) into a single shared CFI array which can be used to detect
3245  * conflicts and nicely feed a linear array of ORC entries to the unwinder.
3246  */
propagate_alt_cfi(struct objtool_file * file,struct instruction * insn)3247 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
3248 {
3249 	struct cfi_state **alt_cfi;
3250 	int group_off;
3251 
3252 	if (!insn->alt_group)
3253 		return 0;
3254 
3255 	if (!insn->cfi) {
3256 		WARN("CFI missing");
3257 		return -1;
3258 	}
3259 
3260 	alt_cfi = insn->alt_group->cfi;
3261 	group_off = insn->offset - insn->alt_group->first_insn->offset;
3262 
3263 	if (!alt_cfi[group_off]) {
3264 		alt_cfi[group_off] = insn->cfi;
3265 	} else {
3266 		if (cficmp(alt_cfi[group_off], insn->cfi)) {
3267 			struct alt_group *orig_group = insn->alt_group->orig_group ?: insn->alt_group;
3268 			struct instruction *orig = orig_group->first_insn;
3269 			char *where = offstr(insn->sec, insn->offset);
3270 			WARN_INSN(orig, "stack layout conflict in alternatives: %s", where);
3271 			free(where);
3272 			return -1;
3273 		}
3274 	}
3275 
3276 	return 0;
3277 }
3278 
handle_insn_ops(struct instruction * insn,struct instruction * next_insn,struct insn_state * state)3279 static int handle_insn_ops(struct instruction *insn,
3280 			   struct instruction *next_insn,
3281 			   struct insn_state *state)
3282 {
3283 	struct stack_op *op;
3284 
3285 	for (op = insn->stack_ops; op; op = op->next) {
3286 
3287 		if (update_cfi_state(insn, next_insn, &state->cfi, op))
3288 			return 1;
3289 
3290 		if (!opts.uaccess || !insn->alt_group)
3291 			continue;
3292 
3293 		if (op->dest.type == OP_DEST_PUSHF) {
3294 			if (!state->uaccess_stack) {
3295 				state->uaccess_stack = 1;
3296 			} else if (state->uaccess_stack >> 31) {
3297 				WARN_INSN(insn, "PUSHF stack exhausted");
3298 				return 1;
3299 			}
3300 			state->uaccess_stack <<= 1;
3301 			state->uaccess_stack  |= state->uaccess;
3302 		}
3303 
3304 		if (op->src.type == OP_SRC_POPF) {
3305 			if (state->uaccess_stack) {
3306 				state->uaccess = state->uaccess_stack & 1;
3307 				state->uaccess_stack >>= 1;
3308 				if (state->uaccess_stack == 1)
3309 					state->uaccess_stack = 0;
3310 			}
3311 		}
3312 	}
3313 
3314 	return 0;
3315 }
3316 
insn_cfi_match(struct instruction * insn,struct cfi_state * cfi2)3317 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
3318 {
3319 	struct cfi_state *cfi1 = insn->cfi;
3320 	int i;
3321 
3322 	if (!cfi1) {
3323 		WARN("CFI missing");
3324 		return false;
3325 	}
3326 
3327 	if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
3328 
3329 		WARN_INSN(insn, "stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
3330 			  cfi1->cfa.base, cfi1->cfa.offset,
3331 			  cfi2->cfa.base, cfi2->cfa.offset);
3332 
3333 	} else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
3334 		for (i = 0; i < CFI_NUM_REGS; i++) {
3335 			if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
3336 				    sizeof(struct cfi_reg)))
3337 				continue;
3338 
3339 			WARN_INSN(insn, "stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
3340 				  i, cfi1->regs[i].base, cfi1->regs[i].offset,
3341 				  i, cfi2->regs[i].base, cfi2->regs[i].offset);
3342 			break;
3343 		}
3344 
3345 	} else if (cfi1->type != cfi2->type) {
3346 
3347 		WARN_INSN(insn, "stack state mismatch: type1=%d type2=%d",
3348 			  cfi1->type, cfi2->type);
3349 
3350 	} else if (cfi1->drap != cfi2->drap ||
3351 		   (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
3352 		   (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
3353 
3354 		WARN_INSN(insn, "stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
3355 			  cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
3356 			  cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
3357 
3358 	} else
3359 		return true;
3360 
3361 	return false;
3362 }
3363 
func_uaccess_safe(struct symbol * func)3364 static inline bool func_uaccess_safe(struct symbol *func)
3365 {
3366 	if (func)
3367 		return func->uaccess_safe;
3368 
3369 	return false;
3370 }
3371 
call_dest_name(struct instruction * insn)3372 static inline const char *call_dest_name(struct instruction *insn)
3373 {
3374 	static char pvname[19];
3375 	struct reloc *reloc;
3376 	int idx;
3377 
3378 	if (insn_call_dest(insn))
3379 		return insn_call_dest(insn)->name;
3380 
3381 	reloc = insn_reloc(NULL, insn);
3382 	if (reloc && !strcmp(reloc->sym->name, "pv_ops")) {
3383 		idx = (reloc_addend(reloc) / sizeof(void *));
3384 		snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
3385 		return pvname;
3386 	}
3387 
3388 	return "{dynamic}";
3389 }
3390 
pv_call_dest(struct objtool_file * file,struct instruction * insn)3391 static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
3392 {
3393 	struct symbol *target;
3394 	struct reloc *reloc;
3395 	int idx;
3396 
3397 	reloc = insn_reloc(file, insn);
3398 	if (!reloc || strcmp(reloc->sym->name, "pv_ops"))
3399 		return false;
3400 
3401 	idx = (arch_dest_reloc_offset(reloc_addend(reloc)) / sizeof(void *));
3402 
3403 	if (file->pv_ops[idx].clean)
3404 		return true;
3405 
3406 	file->pv_ops[idx].clean = true;
3407 
3408 	list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) {
3409 		if (!target->sec->noinstr) {
3410 			WARN("pv_ops[%d]: %s", idx, target->name);
3411 			file->pv_ops[idx].clean = false;
3412 		}
3413 	}
3414 
3415 	return file->pv_ops[idx].clean;
3416 }
3417 
noinstr_call_dest(struct objtool_file * file,struct instruction * insn,struct symbol * func)3418 static inline bool noinstr_call_dest(struct objtool_file *file,
3419 				     struct instruction *insn,
3420 				     struct symbol *func)
3421 {
3422 	/*
3423 	 * We can't deal with indirect function calls at present;
3424 	 * assume they're instrumented.
3425 	 */
3426 	if (!func) {
3427 		if (file->pv_ops)
3428 			return pv_call_dest(file, insn);
3429 
3430 		return false;
3431 	}
3432 
3433 	/*
3434 	 * If the symbol is from a noinstr section; we good.
3435 	 */
3436 	if (func->sec->noinstr)
3437 		return true;
3438 
3439 	/*
3440 	 * If the symbol is a static_call trampoline, we can't tell.
3441 	 */
3442 	if (func->static_call_tramp)
3443 		return true;
3444 
3445 	/*
3446 	 * The __ubsan_handle_*() calls are like WARN(), they only happen when
3447 	 * something 'BAD' happened. At the risk of taking the machine down,
3448 	 * let them proceed to get the message out.
3449 	 */
3450 	if (!strncmp(func->name, "__ubsan_handle_", 15))
3451 		return true;
3452 
3453 	return false;
3454 }
3455 
validate_call(struct objtool_file * file,struct instruction * insn,struct insn_state * state)3456 static int validate_call(struct objtool_file *file,
3457 			 struct instruction *insn,
3458 			 struct insn_state *state)
3459 {
3460 	if (state->noinstr && state->instr <= 0 &&
3461 	    !noinstr_call_dest(file, insn, insn_call_dest(insn))) {
3462 		WARN_INSN(insn, "call to %s() leaves .noinstr.text section", call_dest_name(insn));
3463 		return 1;
3464 	}
3465 
3466 	if (state->uaccess && !func_uaccess_safe(insn_call_dest(insn))) {
3467 		WARN_INSN(insn, "call to %s() with UACCESS enabled", call_dest_name(insn));
3468 		return 1;
3469 	}
3470 
3471 	if (state->df) {
3472 		WARN_INSN(insn, "call to %s() with DF set", call_dest_name(insn));
3473 		return 1;
3474 	}
3475 
3476 	return 0;
3477 }
3478 
validate_sibling_call(struct objtool_file * file,struct instruction * insn,struct insn_state * state)3479 static int validate_sibling_call(struct objtool_file *file,
3480 				 struct instruction *insn,
3481 				 struct insn_state *state)
3482 {
3483 	if (insn_func(insn) && has_modified_stack_frame(insn, state)) {
3484 		WARN_INSN(insn, "sibling call from callable instruction with modified stack frame");
3485 		return 1;
3486 	}
3487 
3488 	return validate_call(file, insn, state);
3489 }
3490 
validate_return(struct symbol * func,struct instruction * insn,struct insn_state * state)3491 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
3492 {
3493 	if (state->noinstr && state->instr > 0) {
3494 		WARN_INSN(insn, "return with instrumentation enabled");
3495 		return 1;
3496 	}
3497 
3498 	if (state->uaccess && !func_uaccess_safe(func)) {
3499 		WARN_INSN(insn, "return with UACCESS enabled");
3500 		return 1;
3501 	}
3502 
3503 	if (!state->uaccess && func_uaccess_safe(func)) {
3504 		WARN_INSN(insn, "return with UACCESS disabled from a UACCESS-safe function");
3505 		return 1;
3506 	}
3507 
3508 	if (state->df) {
3509 		WARN_INSN(insn, "return with DF set");
3510 		return 1;
3511 	}
3512 
3513 	if (func && has_modified_stack_frame(insn, state)) {
3514 		WARN_INSN(insn, "return with modified stack frame");
3515 		return 1;
3516 	}
3517 
3518 	if (state->cfi.bp_scratch) {
3519 		WARN_INSN(insn, "BP used as a scratch register");
3520 		return 1;
3521 	}
3522 
3523 	return 0;
3524 }
3525 
next_insn_to_validate(struct objtool_file * file,struct instruction * insn)3526 static struct instruction *next_insn_to_validate(struct objtool_file *file,
3527 						 struct instruction *insn)
3528 {
3529 	struct alt_group *alt_group = insn->alt_group;
3530 
3531 	/*
3532 	 * Simulate the fact that alternatives are patched in-place.  When the
3533 	 * end of a replacement alt_group is reached, redirect objtool flow to
3534 	 * the end of the original alt_group.
3535 	 *
3536 	 * insn->alts->insn -> alt_group->first_insn
3537 	 *		       ...
3538 	 *		       alt_group->last_insn
3539 	 *		       [alt_group->nop]      -> next(orig_group->last_insn)
3540 	 */
3541 	if (alt_group) {
3542 		if (alt_group->nop) {
3543 			/* ->nop implies ->orig_group */
3544 			if (insn == alt_group->last_insn)
3545 				return alt_group->nop;
3546 			if (insn == alt_group->nop)
3547 				goto next_orig;
3548 		}
3549 		if (insn == alt_group->last_insn && alt_group->orig_group)
3550 			goto next_orig;
3551 	}
3552 
3553 	return next_insn_same_sec(file, insn);
3554 
3555 next_orig:
3556 	return next_insn_same_sec(file, alt_group->orig_group->last_insn);
3557 }
3558 
3559 /*
3560  * Follow the branch starting at the given instruction, and recursively follow
3561  * any other branches (jumps).  Meanwhile, track the frame pointer state at
3562  * each instruction and validate all the rules described in
3563  * tools/objtool/Documentation/objtool.txt.
3564  */
validate_branch(struct objtool_file * file,struct symbol * func,struct instruction * insn,struct insn_state state)3565 static int validate_branch(struct objtool_file *file, struct symbol *func,
3566 			   struct instruction *insn, struct insn_state state)
3567 {
3568 	struct alternative *alt;
3569 	struct instruction *next_insn, *prev_insn = NULL;
3570 	struct section *sec;
3571 	u8 visited;
3572 	int ret;
3573 
3574 	sec = insn->sec;
3575 
3576 	while (1) {
3577 		next_insn = next_insn_to_validate(file, insn);
3578 
3579 		if (func && insn_func(insn) && func != insn_func(insn)->pfunc) {
3580 			/* Ignore KCFI type preambles, which always fall through */
3581 			if (!strncmp(func->name, "__cfi_", 6) ||
3582 			    !strncmp(func->name, "__pfx_", 6))
3583 				return 0;
3584 
3585 			if (file->ignore_unreachables)
3586 				return 0;
3587 
3588 			WARN("%s() falls through to next function %s()",
3589 			     func->name, insn_func(insn)->name);
3590 			return 1;
3591 		}
3592 
3593 		if (func && insn->ignore) {
3594 			WARN_INSN(insn, "BUG: why am I validating an ignored function?");
3595 			return 1;
3596 		}
3597 
3598 		visited = VISITED_BRANCH << state.uaccess;
3599 		if (insn->visited & VISITED_BRANCH_MASK) {
3600 			if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
3601 				return 1;
3602 
3603 			if (insn->visited & visited)
3604 				return 0;
3605 		} else {
3606 			nr_insns_visited++;
3607 		}
3608 
3609 		if (state.noinstr)
3610 			state.instr += insn->instr;
3611 
3612 		if (insn->hint) {
3613 			if (insn->restore) {
3614 				struct instruction *save_insn, *i;
3615 
3616 				i = insn;
3617 				save_insn = NULL;
3618 
3619 				sym_for_each_insn_continue_reverse(file, func, i) {
3620 					if (i->save) {
3621 						save_insn = i;
3622 						break;
3623 					}
3624 				}
3625 
3626 				if (!save_insn) {
3627 					WARN_INSN(insn, "no corresponding CFI save for CFI restore");
3628 					return 1;
3629 				}
3630 
3631 				if (!save_insn->visited) {
3632 					/*
3633 					 * If the restore hint insn is at the
3634 					 * beginning of a basic block and was
3635 					 * branched to from elsewhere, and the
3636 					 * save insn hasn't been visited yet,
3637 					 * defer following this branch for now.
3638 					 * It will be seen later via the
3639 					 * straight-line path.
3640 					 */
3641 					if (!prev_insn)
3642 						return 0;
3643 
3644 					WARN_INSN(insn, "objtool isn't smart enough to handle this CFI save/restore combo");
3645 					return 1;
3646 				}
3647 
3648 				insn->cfi = save_insn->cfi;
3649 				nr_cfi_reused++;
3650 			}
3651 
3652 			state.cfi = *insn->cfi;
3653 		} else {
3654 			/* XXX track if we actually changed state.cfi */
3655 
3656 			if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
3657 				insn->cfi = prev_insn->cfi;
3658 				nr_cfi_reused++;
3659 			} else {
3660 				insn->cfi = cfi_hash_find_or_add(&state.cfi);
3661 			}
3662 		}
3663 
3664 		insn->visited |= visited;
3665 
3666 		if (propagate_alt_cfi(file, insn))
3667 			return 1;
3668 
3669 		if (!insn->ignore_alts && insn->alts) {
3670 			bool skip_orig = false;
3671 
3672 			for (alt = insn->alts; alt; alt = alt->next) {
3673 				if (alt->skip_orig)
3674 					skip_orig = true;
3675 
3676 				ret = validate_branch(file, func, alt->insn, state);
3677 				if (ret) {
3678 					BT_INSN(insn, "(alt)");
3679 					return ret;
3680 				}
3681 			}
3682 
3683 			if (skip_orig)
3684 				return 0;
3685 		}
3686 
3687 		if (handle_insn_ops(insn, next_insn, &state))
3688 			return 1;
3689 
3690 		switch (insn->type) {
3691 
3692 		case INSN_RETURN:
3693 			return validate_return(func, insn, &state);
3694 
3695 		case INSN_CALL:
3696 		case INSN_CALL_DYNAMIC:
3697 			ret = validate_call(file, insn, &state);
3698 			if (ret)
3699 				return ret;
3700 
3701 			if (opts.stackval && func && !is_special_call(insn) &&
3702 			    !has_valid_stack_frame(&state)) {
3703 				WARN_INSN(insn, "call without frame pointer save/setup");
3704 				return 1;
3705 			}
3706 
3707 			if (insn->dead_end)
3708 				return 0;
3709 
3710 			break;
3711 
3712 		case INSN_JUMP_CONDITIONAL:
3713 		case INSN_JUMP_UNCONDITIONAL:
3714 			if (is_sibling_call(insn)) {
3715 				ret = validate_sibling_call(file, insn, &state);
3716 				if (ret)
3717 					return ret;
3718 
3719 			} else if (insn->jump_dest) {
3720 				ret = validate_branch(file, func,
3721 						      insn->jump_dest, state);
3722 				if (ret) {
3723 					BT_INSN(insn, "(branch)");
3724 					return ret;
3725 				}
3726 			}
3727 
3728 			if (insn->type == INSN_JUMP_UNCONDITIONAL)
3729 				return 0;
3730 
3731 			break;
3732 
3733 		case INSN_JUMP_DYNAMIC:
3734 		case INSN_JUMP_DYNAMIC_CONDITIONAL:
3735 			if (is_sibling_call(insn)) {
3736 				ret = validate_sibling_call(file, insn, &state);
3737 				if (ret)
3738 					return ret;
3739 			}
3740 
3741 			if (insn->type == INSN_JUMP_DYNAMIC)
3742 				return 0;
3743 
3744 			break;
3745 
3746 		case INSN_CONTEXT_SWITCH:
3747 			if (func) {
3748 				if (!next_insn || !next_insn->hint) {
3749 					WARN_INSN(insn, "unsupported instruction in callable function");
3750 					return 1;
3751 				}
3752 				break;
3753 			}
3754 			return 0;
3755 
3756 		case INSN_STAC:
3757 			if (!opts.uaccess)
3758 				break;
3759 
3760 			if (state.uaccess) {
3761 				WARN_INSN(insn, "recursive UACCESS enable");
3762 				return 1;
3763 			}
3764 
3765 			state.uaccess = true;
3766 			break;
3767 
3768 		case INSN_CLAC:
3769 			if (!opts.uaccess)
3770 				break;
3771 
3772 			if (!state.uaccess && func) {
3773 				WARN_INSN(insn, "redundant UACCESS disable");
3774 				return 1;
3775 			}
3776 
3777 			if (func_uaccess_safe(func) && !state.uaccess_stack) {
3778 				WARN_INSN(insn, "UACCESS-safe disables UACCESS");
3779 				return 1;
3780 			}
3781 
3782 			state.uaccess = false;
3783 			break;
3784 
3785 		case INSN_STD:
3786 			if (state.df) {
3787 				WARN_INSN(insn, "recursive STD");
3788 				return 1;
3789 			}
3790 
3791 			state.df = true;
3792 			break;
3793 
3794 		case INSN_CLD:
3795 			if (!state.df && func) {
3796 				WARN_INSN(insn, "redundant CLD");
3797 				return 1;
3798 			}
3799 
3800 			state.df = false;
3801 			break;
3802 
3803 		default:
3804 			break;
3805 		}
3806 
3807 		if (insn->dead_end)
3808 			return 0;
3809 
3810 		if (!next_insn) {
3811 			if (state.cfi.cfa.base == CFI_UNDEFINED)
3812 				return 0;
3813 			if (file->ignore_unreachables)
3814 				return 0;
3815 
3816 			WARN("%s: unexpected end of section", sec->name);
3817 			return 1;
3818 		}
3819 
3820 		prev_insn = insn;
3821 		insn = next_insn;
3822 	}
3823 
3824 	return 0;
3825 }
3826 
validate_unwind_hint(struct objtool_file * file,struct instruction * insn,struct insn_state * state)3827 static int validate_unwind_hint(struct objtool_file *file,
3828 				  struct instruction *insn,
3829 				  struct insn_state *state)
3830 {
3831 	if (insn->hint && !insn->visited && !insn->ignore) {
3832 		int ret = validate_branch(file, insn_func(insn), insn, *state);
3833 		if (ret)
3834 			BT_INSN(insn, "<=== (hint)");
3835 		return ret;
3836 	}
3837 
3838 	return 0;
3839 }
3840 
validate_unwind_hints(struct objtool_file * file,struct section * sec)3841 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
3842 {
3843 	struct instruction *insn;
3844 	struct insn_state state;
3845 	int warnings = 0;
3846 
3847 	if (!file->hints)
3848 		return 0;
3849 
3850 	init_insn_state(file, &state, sec);
3851 
3852 	if (sec) {
3853 		sec_for_each_insn(file, sec, insn)
3854 			warnings += validate_unwind_hint(file, insn, &state);
3855 	} else {
3856 		for_each_insn(file, insn)
3857 			warnings += validate_unwind_hint(file, insn, &state);
3858 	}
3859 
3860 	return warnings;
3861 }
3862 
3863 /*
3864  * Validate rethunk entry constraint: must untrain RET before the first RET.
3865  *
3866  * Follow every branch (intra-function) and ensure VALIDATE_UNRET_END comes
3867  * before an actual RET instruction.
3868  */
validate_unret(struct objtool_file * file,struct instruction * insn)3869 static int validate_unret(struct objtool_file *file, struct instruction *insn)
3870 {
3871 	struct instruction *next, *dest;
3872 	int ret;
3873 
3874 	for (;;) {
3875 		next = next_insn_to_validate(file, insn);
3876 
3877 		if (insn->visited & VISITED_UNRET)
3878 			return 0;
3879 
3880 		insn->visited |= VISITED_UNRET;
3881 
3882 		if (!insn->ignore_alts && insn->alts) {
3883 			struct alternative *alt;
3884 			bool skip_orig = false;
3885 
3886 			for (alt = insn->alts; alt; alt = alt->next) {
3887 				if (alt->skip_orig)
3888 					skip_orig = true;
3889 
3890 				ret = validate_unret(file, alt->insn);
3891 				if (ret) {
3892 					BT_INSN(insn, "(alt)");
3893 					return ret;
3894 				}
3895 			}
3896 
3897 			if (skip_orig)
3898 				return 0;
3899 		}
3900 
3901 		switch (insn->type) {
3902 
3903 		case INSN_CALL_DYNAMIC:
3904 		case INSN_JUMP_DYNAMIC:
3905 		case INSN_JUMP_DYNAMIC_CONDITIONAL:
3906 			WARN_INSN(insn, "early indirect call");
3907 			return 1;
3908 
3909 		case INSN_JUMP_UNCONDITIONAL:
3910 		case INSN_JUMP_CONDITIONAL:
3911 			if (!is_sibling_call(insn)) {
3912 				if (!insn->jump_dest) {
3913 					WARN_INSN(insn, "unresolved jump target after linking?!?");
3914 					return -1;
3915 				}
3916 				ret = validate_unret(file, insn->jump_dest);
3917 				if (ret) {
3918 					BT_INSN(insn, "(branch%s)",
3919 						insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : "");
3920 					return ret;
3921 				}
3922 
3923 				if (insn->type == INSN_JUMP_UNCONDITIONAL)
3924 					return 0;
3925 
3926 				break;
3927 			}
3928 
3929 			/* fallthrough */
3930 		case INSN_CALL:
3931 			dest = find_insn(file, insn_call_dest(insn)->sec,
3932 					 insn_call_dest(insn)->offset);
3933 			if (!dest) {
3934 				WARN("Unresolved function after linking!?: %s",
3935 				     insn_call_dest(insn)->name);
3936 				return -1;
3937 			}
3938 
3939 			ret = validate_unret(file, dest);
3940 			if (ret) {
3941 				BT_INSN(insn, "(call)");
3942 				return ret;
3943 			}
3944 			/*
3945 			 * If a call returns without error, it must have seen UNTRAIN_RET.
3946 			 * Therefore any non-error return is a success.
3947 			 */
3948 			return 0;
3949 
3950 		case INSN_RETURN:
3951 			WARN_INSN(insn, "RET before UNTRAIN");
3952 			return 1;
3953 
3954 		case INSN_CONTEXT_SWITCH:
3955 			if (insn_func(insn))
3956 				break;
3957 			return 0;
3958 
3959 		case INSN_NOP:
3960 			if (insn->retpoline_safe)
3961 				return 0;
3962 			break;
3963 
3964 		default:
3965 			break;
3966 		}
3967 
3968 		if (insn->dead_end)
3969 			return 0;
3970 
3971 		if (!next) {
3972 			WARN_INSN(insn, "teh end!");
3973 			return -1;
3974 		}
3975 		insn = next;
3976 	}
3977 
3978 	return 0;
3979 }
3980 
3981 /*
3982  * Validate that all branches starting at VALIDATE_UNRET_BEGIN encounter
3983  * VALIDATE_UNRET_END before RET.
3984  */
validate_unrets(struct objtool_file * file)3985 static int validate_unrets(struct objtool_file *file)
3986 {
3987 	struct instruction *insn;
3988 	int ret, warnings = 0;
3989 
3990 	for_each_insn(file, insn) {
3991 		if (!insn->unret)
3992 			continue;
3993 
3994 		ret = validate_unret(file, insn);
3995 		if (ret < 0) {
3996 			WARN_INSN(insn, "Failed UNRET validation");
3997 			return ret;
3998 		}
3999 		warnings += ret;
4000 	}
4001 
4002 	return warnings;
4003 }
4004 
validate_retpoline(struct objtool_file * file)4005 static int validate_retpoline(struct objtool_file *file)
4006 {
4007 	struct instruction *insn;
4008 	int warnings = 0;
4009 
4010 	for_each_insn(file, insn) {
4011 		if (insn->type != INSN_JUMP_DYNAMIC &&
4012 		    insn->type != INSN_CALL_DYNAMIC &&
4013 		    insn->type != INSN_RETURN)
4014 			continue;
4015 
4016 		if (insn->retpoline_safe)
4017 			continue;
4018 
4019 		if (insn->sec->init)
4020 			continue;
4021 
4022 		if (insn->type == INSN_RETURN) {
4023 			if (opts.rethunk) {
4024 				WARN_INSN(insn, "'naked' return found in RETHUNK build");
4025 			} else
4026 				continue;
4027 		} else {
4028 			WARN_INSN(insn, "indirect %s found in RETPOLINE build",
4029 				  insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
4030 		}
4031 
4032 		warnings++;
4033 	}
4034 
4035 	return warnings;
4036 }
4037 
is_kasan_insn(struct instruction * insn)4038 static bool is_kasan_insn(struct instruction *insn)
4039 {
4040 	return (insn->type == INSN_CALL &&
4041 		!strcmp(insn_call_dest(insn)->name, "__asan_handle_no_return"));
4042 }
4043 
is_ubsan_insn(struct instruction * insn)4044 static bool is_ubsan_insn(struct instruction *insn)
4045 {
4046 	return (insn->type == INSN_CALL &&
4047 		!strcmp(insn_call_dest(insn)->name,
4048 			"__ubsan_handle_builtin_unreachable"));
4049 }
4050 
ignore_unreachable_insn(struct objtool_file * file,struct instruction * insn)4051 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
4052 {
4053 	int i;
4054 	struct instruction *prev_insn;
4055 
4056 	if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP)
4057 		return true;
4058 
4059 	/*
4060 	 * Ignore alternative replacement instructions.  This can happen
4061 	 * when a whitelisted function uses one of the ALTERNATIVE macros.
4062 	 */
4063 	if (!strcmp(insn->sec->name, ".altinstr_replacement") ||
4064 	    !strcmp(insn->sec->name, ".altinstr_aux"))
4065 		return true;
4066 
4067 	/*
4068 	 * Whole archive runs might encounter dead code from weak symbols.
4069 	 * This is where the linker will have dropped the weak symbol in
4070 	 * favour of a regular symbol, but leaves the code in place.
4071 	 *
4072 	 * In this case we'll find a piece of code (whole function) that is not
4073 	 * covered by a !section symbol. Ignore them.
4074 	 */
4075 	if (opts.link && !insn_func(insn)) {
4076 		int size = find_symbol_hole_containing(insn->sec, insn->offset);
4077 		unsigned long end = insn->offset + size;
4078 
4079 		if (!size) /* not a hole */
4080 			return false;
4081 
4082 		if (size < 0) /* hole until the end */
4083 			return true;
4084 
4085 		sec_for_each_insn_continue(file, insn) {
4086 			/*
4087 			 * If we reach a visited instruction at or before the
4088 			 * end of the hole, ignore the unreachable.
4089 			 */
4090 			if (insn->visited)
4091 				return true;
4092 
4093 			if (insn->offset >= end)
4094 				break;
4095 
4096 			/*
4097 			 * If this hole jumps to a .cold function, mark it ignore too.
4098 			 */
4099 			if (insn->jump_dest && insn_func(insn->jump_dest) &&
4100 			    strstr(insn_func(insn->jump_dest)->name, ".cold")) {
4101 				struct instruction *dest = insn->jump_dest;
4102 				func_for_each_insn(file, insn_func(dest), dest)
4103 					dest->ignore = true;
4104 			}
4105 		}
4106 
4107 		return false;
4108 	}
4109 
4110 	if (!insn_func(insn))
4111 		return false;
4112 
4113 	if (insn_func(insn)->static_call_tramp)
4114 		return true;
4115 
4116 	/*
4117 	 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
4118 	 * __builtin_unreachable().  The BUG() macro has an unreachable() after
4119 	 * the UD2, which causes GCC's undefined trap logic to emit another UD2
4120 	 * (or occasionally a JMP to UD2).
4121 	 *
4122 	 * It may also insert a UD2 after calling a __noreturn function.
4123 	 */
4124 	prev_insn = prev_insn_same_sec(file, insn);
4125 	if (prev_insn && prev_insn->dead_end &&
4126 	    (insn->type == INSN_BUG ||
4127 	     (insn->type == INSN_JUMP_UNCONDITIONAL &&
4128 	      insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
4129 		return true;
4130 
4131 	/*
4132 	 * Check if this (or a subsequent) instruction is related to
4133 	 * CONFIG_UBSAN or CONFIG_KASAN.
4134 	 *
4135 	 * End the search at 5 instructions to avoid going into the weeds.
4136 	 */
4137 	for (i = 0; i < 5; i++) {
4138 
4139 		if (is_kasan_insn(insn) || is_ubsan_insn(insn))
4140 			return true;
4141 
4142 		if (insn->type == INSN_JUMP_UNCONDITIONAL) {
4143 			if (insn->jump_dest &&
4144 			    insn_func(insn->jump_dest) == insn_func(insn)) {
4145 				insn = insn->jump_dest;
4146 				continue;
4147 			}
4148 
4149 			break;
4150 		}
4151 
4152 		if (insn->offset + insn->len >= insn_func(insn)->offset + insn_func(insn)->len)
4153 			break;
4154 
4155 		insn = next_insn_same_sec(file, insn);
4156 	}
4157 
4158 	return false;
4159 }
4160 
add_prefix_symbol(struct objtool_file * file,struct symbol * func)4161 static int add_prefix_symbol(struct objtool_file *file, struct symbol *func)
4162 {
4163 	struct instruction *insn, *prev;
4164 	struct cfi_state *cfi;
4165 
4166 	insn = find_insn(file, func->sec, func->offset);
4167 	if (!insn)
4168 		return -1;
4169 
4170 	for (prev = prev_insn_same_sec(file, insn);
4171 	     prev;
4172 	     prev = prev_insn_same_sec(file, prev)) {
4173 		u64 offset;
4174 
4175 		if (prev->type != INSN_NOP)
4176 			return -1;
4177 
4178 		offset = func->offset - prev->offset;
4179 
4180 		if (offset > opts.prefix)
4181 			return -1;
4182 
4183 		if (offset < opts.prefix)
4184 			continue;
4185 
4186 		elf_create_prefix_symbol(file->elf, func, opts.prefix);
4187 		break;
4188 	}
4189 
4190 	if (!prev)
4191 		return -1;
4192 
4193 	if (!insn->cfi) {
4194 		/*
4195 		 * This can happen if stack validation isn't enabled or the
4196 		 * function is annotated with STACK_FRAME_NON_STANDARD.
4197 		 */
4198 		return 0;
4199 	}
4200 
4201 	/* Propagate insn->cfi to the prefix code */
4202 	cfi = cfi_hash_find_or_add(insn->cfi);
4203 	for (; prev != insn; prev = next_insn_same_sec(file, prev))
4204 		prev->cfi = cfi;
4205 
4206 	return 0;
4207 }
4208 
add_prefix_symbols(struct objtool_file * file)4209 static int add_prefix_symbols(struct objtool_file *file)
4210 {
4211 	struct section *sec;
4212 	struct symbol *func;
4213 
4214 	for_each_sec(file, sec) {
4215 		if (!(sec->sh.sh_flags & SHF_EXECINSTR))
4216 			continue;
4217 
4218 		sec_for_each_sym(sec, func) {
4219 			if (func->type != STT_FUNC)
4220 				continue;
4221 
4222 			add_prefix_symbol(file, func);
4223 		}
4224 	}
4225 
4226 	return 0;
4227 }
4228 
validate_symbol(struct objtool_file * file,struct section * sec,struct symbol * sym,struct insn_state * state)4229 static int validate_symbol(struct objtool_file *file, struct section *sec,
4230 			   struct symbol *sym, struct insn_state *state)
4231 {
4232 	struct instruction *insn;
4233 	int ret;
4234 
4235 	if (!sym->len) {
4236 		WARN("%s() is missing an ELF size annotation", sym->name);
4237 		return 1;
4238 	}
4239 
4240 	if (sym->pfunc != sym || sym->alias != sym)
4241 		return 0;
4242 
4243 	insn = find_insn(file, sec, sym->offset);
4244 	if (!insn || insn->ignore || insn->visited)
4245 		return 0;
4246 
4247 	if (opts.uaccess)
4248 		state->uaccess = sym->uaccess_safe;
4249 
4250 	ret = validate_branch(file, insn_func(insn), insn, *state);
4251 	if (ret)
4252 		BT_INSN(insn, "<=== (sym)");
4253 	return ret;
4254 }
4255 
validate_section(struct objtool_file * file,struct section * sec)4256 static int validate_section(struct objtool_file *file, struct section *sec)
4257 {
4258 	struct insn_state state;
4259 	struct symbol *func;
4260 	int warnings = 0;
4261 
4262 	sec_for_each_sym(sec, func) {
4263 		if (func->type != STT_FUNC)
4264 			continue;
4265 
4266 		init_insn_state(file, &state, sec);
4267 		set_func_state(&state.cfi);
4268 
4269 		warnings += validate_symbol(file, sec, func, &state);
4270 	}
4271 
4272 	return warnings;
4273 }
4274 
validate_noinstr_sections(struct objtool_file * file)4275 static int validate_noinstr_sections(struct objtool_file *file)
4276 {
4277 	struct section *sec;
4278 	int warnings = 0;
4279 
4280 	sec = find_section_by_name(file->elf, ".noinstr.text");
4281 	if (sec) {
4282 		warnings += validate_section(file, sec);
4283 		warnings += validate_unwind_hints(file, sec);
4284 	}
4285 
4286 	sec = find_section_by_name(file->elf, ".entry.text");
4287 	if (sec) {
4288 		warnings += validate_section(file, sec);
4289 		warnings += validate_unwind_hints(file, sec);
4290 	}
4291 
4292 	sec = find_section_by_name(file->elf, ".cpuidle.text");
4293 	if (sec) {
4294 		warnings += validate_section(file, sec);
4295 		warnings += validate_unwind_hints(file, sec);
4296 	}
4297 
4298 	return warnings;
4299 }
4300 
validate_functions(struct objtool_file * file)4301 static int validate_functions(struct objtool_file *file)
4302 {
4303 	struct section *sec;
4304 	int warnings = 0;
4305 
4306 	for_each_sec(file, sec) {
4307 		if (!(sec->sh.sh_flags & SHF_EXECINSTR))
4308 			continue;
4309 
4310 		warnings += validate_section(file, sec);
4311 	}
4312 
4313 	return warnings;
4314 }
4315 
mark_endbr_used(struct instruction * insn)4316 static void mark_endbr_used(struct instruction *insn)
4317 {
4318 	if (!list_empty(&insn->call_node))
4319 		list_del_init(&insn->call_node);
4320 }
4321 
noendbr_range(struct objtool_file * file,struct instruction * insn)4322 static bool noendbr_range(struct objtool_file *file, struct instruction *insn)
4323 {
4324 	struct symbol *sym = find_symbol_containing(insn->sec, insn->offset-1);
4325 	struct instruction *first;
4326 
4327 	if (!sym)
4328 		return false;
4329 
4330 	first = find_insn(file, sym->sec, sym->offset);
4331 	if (!first)
4332 		return false;
4333 
4334 	if (first->type != INSN_ENDBR && !first->noendbr)
4335 		return false;
4336 
4337 	return insn->offset == sym->offset + sym->len;
4338 }
4339 
validate_ibt_insn(struct objtool_file * file,struct instruction * insn)4340 static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn)
4341 {
4342 	struct instruction *dest;
4343 	struct reloc *reloc;
4344 	unsigned long off;
4345 	int warnings = 0;
4346 
4347 	/*
4348 	 * Looking for function pointer load relocations.  Ignore
4349 	 * direct/indirect branches:
4350 	 */
4351 	switch (insn->type) {
4352 	case INSN_CALL:
4353 	case INSN_CALL_DYNAMIC:
4354 	case INSN_JUMP_CONDITIONAL:
4355 	case INSN_JUMP_UNCONDITIONAL:
4356 	case INSN_JUMP_DYNAMIC:
4357 	case INSN_JUMP_DYNAMIC_CONDITIONAL:
4358 	case INSN_RETURN:
4359 	case INSN_NOP:
4360 		return 0;
4361 	default:
4362 		break;
4363 	}
4364 
4365 	for (reloc = insn_reloc(file, insn);
4366 	     reloc;
4367 	     reloc = find_reloc_by_dest_range(file->elf, insn->sec,
4368 					      reloc_offset(reloc) + 1,
4369 					      (insn->offset + insn->len) - (reloc_offset(reloc) + 1))) {
4370 
4371 		/*
4372 		 * static_call_update() references the trampoline, which
4373 		 * doesn't have (or need) ENDBR.  Skip warning in that case.
4374 		 */
4375 		if (reloc->sym->static_call_tramp)
4376 			continue;
4377 
4378 		off = reloc->sym->offset;
4379 		if (reloc_type(reloc) == R_X86_64_PC32 ||
4380 		    reloc_type(reloc) == R_X86_64_PLT32)
4381 			off += arch_dest_reloc_offset(reloc_addend(reloc));
4382 		else
4383 			off += reloc_addend(reloc);
4384 
4385 		dest = find_insn(file, reloc->sym->sec, off);
4386 		if (!dest)
4387 			continue;
4388 
4389 		if (dest->type == INSN_ENDBR) {
4390 			mark_endbr_used(dest);
4391 			continue;
4392 		}
4393 
4394 		if (insn_func(dest) && insn_func(insn) &&
4395 		    insn_func(dest)->pfunc == insn_func(insn)->pfunc) {
4396 			/*
4397 			 * Anything from->to self is either _THIS_IP_ or
4398 			 * IRET-to-self.
4399 			 *
4400 			 * There is no sane way to annotate _THIS_IP_ since the
4401 			 * compiler treats the relocation as a constant and is
4402 			 * happy to fold in offsets, skewing any annotation we
4403 			 * do, leading to vast amounts of false-positives.
4404 			 *
4405 			 * There's also compiler generated _THIS_IP_ through
4406 			 * KCOV and such which we have no hope of annotating.
4407 			 *
4408 			 * As such, blanket accept self-references without
4409 			 * issue.
4410 			 */
4411 			continue;
4412 		}
4413 
4414 		/*
4415 		 * Accept anything ANNOTATE_NOENDBR.
4416 		 */
4417 		if (dest->noendbr)
4418 			continue;
4419 
4420 		/*
4421 		 * Accept if this is the instruction after a symbol
4422 		 * that is (no)endbr -- typical code-range usage.
4423 		 */
4424 		if (noendbr_range(file, dest))
4425 			continue;
4426 
4427 		WARN_INSN(insn, "relocation to !ENDBR: %s", offstr(dest->sec, dest->offset));
4428 
4429 		warnings++;
4430 	}
4431 
4432 	return warnings;
4433 }
4434 
validate_ibt_data_reloc(struct objtool_file * file,struct reloc * reloc)4435 static int validate_ibt_data_reloc(struct objtool_file *file,
4436 				   struct reloc *reloc)
4437 {
4438 	struct instruction *dest;
4439 
4440 	dest = find_insn(file, reloc->sym->sec,
4441 			 reloc->sym->offset + reloc_addend(reloc));
4442 	if (!dest)
4443 		return 0;
4444 
4445 	if (dest->type == INSN_ENDBR) {
4446 		mark_endbr_used(dest);
4447 		return 0;
4448 	}
4449 
4450 	if (dest->noendbr)
4451 		return 0;
4452 
4453 	WARN_FUNC("data relocation to !ENDBR: %s",
4454 		  reloc->sec->base, reloc_offset(reloc),
4455 		  offstr(dest->sec, dest->offset));
4456 
4457 	return 1;
4458 }
4459 
4460 /*
4461  * Validate IBT rules and remove used ENDBR instructions from the seal list.
4462  * Unused ENDBR instructions will be annotated for sealing (i.e., replaced with
4463  * NOPs) later, in create_ibt_endbr_seal_sections().
4464  */
validate_ibt(struct objtool_file * file)4465 static int validate_ibt(struct objtool_file *file)
4466 {
4467 	struct section *sec;
4468 	struct reloc *reloc;
4469 	struct instruction *insn;
4470 	int warnings = 0;
4471 
4472 	for_each_insn(file, insn)
4473 		warnings += validate_ibt_insn(file, insn);
4474 
4475 	for_each_sec(file, sec) {
4476 
4477 		/* Already done by validate_ibt_insn() */
4478 		if (sec->sh.sh_flags & SHF_EXECINSTR)
4479 			continue;
4480 
4481 		if (!sec->rsec)
4482 			continue;
4483 
4484 		/*
4485 		 * These sections can reference text addresses, but not with
4486 		 * the intent to indirect branch to them.
4487 		 */
4488 		if ((!strncmp(sec->name, ".discard", 8) &&
4489 		     strcmp(sec->name, ".discard.ibt_endbr_noseal"))	||
4490 		    !strncmp(sec->name, ".debug", 6)			||
4491 		    !strcmp(sec->name, ".altinstructions")		||
4492 		    !strcmp(sec->name, ".ibt_endbr_seal")		||
4493 		    !strcmp(sec->name, ".orc_unwind_ip")		||
4494 		    !strcmp(sec->name, ".parainstructions")		||
4495 		    !strcmp(sec->name, ".retpoline_sites")		||
4496 		    !strcmp(sec->name, ".smp_locks")			||
4497 		    !strcmp(sec->name, ".static_call_sites")		||
4498 		    !strcmp(sec->name, "_error_injection_whitelist")	||
4499 		    !strcmp(sec->name, "_kprobe_blacklist")		||
4500 		    !strcmp(sec->name, "__bug_table")			||
4501 		    !strcmp(sec->name, "__ex_table")			||
4502 		    !strcmp(sec->name, "__jump_table")			||
4503 		    !strcmp(sec->name, "__mcount_loc")			||
4504 		    !strcmp(sec->name, ".kcfi_traps")			||
4505 		    strstr(sec->name, "__patchable_function_entries"))
4506 			continue;
4507 
4508 		for_each_reloc(sec->rsec, reloc)
4509 			warnings += validate_ibt_data_reloc(file, reloc);
4510 	}
4511 
4512 	return warnings;
4513 }
4514 
validate_sls(struct objtool_file * file)4515 static int validate_sls(struct objtool_file *file)
4516 {
4517 	struct instruction *insn, *next_insn;
4518 	int warnings = 0;
4519 
4520 	for_each_insn(file, insn) {
4521 		next_insn = next_insn_same_sec(file, insn);
4522 
4523 		if (insn->retpoline_safe)
4524 			continue;
4525 
4526 		switch (insn->type) {
4527 		case INSN_RETURN:
4528 			if (!next_insn || next_insn->type != INSN_TRAP) {
4529 				WARN_INSN(insn, "missing int3 after ret");
4530 				warnings++;
4531 			}
4532 
4533 			break;
4534 		case INSN_JUMP_DYNAMIC:
4535 			if (!next_insn || next_insn->type != INSN_TRAP) {
4536 				WARN_INSN(insn, "missing int3 after indirect jump");
4537 				warnings++;
4538 			}
4539 			break;
4540 		default:
4541 			break;
4542 		}
4543 	}
4544 
4545 	return warnings;
4546 }
4547 
validate_reachable_instructions(struct objtool_file * file)4548 static int validate_reachable_instructions(struct objtool_file *file)
4549 {
4550 	struct instruction *insn, *prev_insn;
4551 	struct symbol *call_dest;
4552 	int warnings = 0;
4553 
4554 	if (file->ignore_unreachables)
4555 		return 0;
4556 
4557 	for_each_insn(file, insn) {
4558 		if (insn->visited || ignore_unreachable_insn(file, insn))
4559 			continue;
4560 
4561 		prev_insn = prev_insn_same_sec(file, insn);
4562 		if (prev_insn && prev_insn->dead_end) {
4563 			call_dest = insn_call_dest(prev_insn);
4564 			if (call_dest) {
4565 				WARN_INSN(insn, "%s() is missing a __noreturn annotation",
4566 					  call_dest->name);
4567 				warnings++;
4568 				continue;
4569 			}
4570 		}
4571 
4572 		WARN_INSN(insn, "unreachable instruction");
4573 		warnings++;
4574 	}
4575 
4576 	return warnings;
4577 }
4578 
4579 /* 'funcs' is a space-separated list of function names */
disas_funcs(const char * funcs)4580 static int disas_funcs(const char *funcs)
4581 {
4582 	const char *objdump_str, *cross_compile;
4583 	int size, ret;
4584 	char *cmd;
4585 
4586 	cross_compile = getenv("CROSS_COMPILE");
4587 	if (!cross_compile)
4588 		cross_compile = "";
4589 
4590 	objdump_str = "%sobjdump -wdr %s | gawk -M -v _funcs='%s' '"
4591 			"BEGIN { split(_funcs, funcs); }"
4592 			"/^$/ { func_match = 0; }"
4593 			"/<.*>:/ { "
4594 				"f = gensub(/.*<(.*)>:/, \"\\\\1\", 1);"
4595 				"for (i in funcs) {"
4596 					"if (funcs[i] == f) {"
4597 						"func_match = 1;"
4598 						"base = strtonum(\"0x\" $1);"
4599 						"break;"
4600 					"}"
4601 				"}"
4602 			"}"
4603 			"{"
4604 				"if (func_match) {"
4605 					"addr = strtonum(\"0x\" $1);"
4606 					"printf(\"%%04x \", addr - base);"
4607 					"print;"
4608 				"}"
4609 			"}' 1>&2";
4610 
4611 	/* fake snprintf() to calculate the size */
4612 	size = snprintf(NULL, 0, objdump_str, cross_compile, objname, funcs) + 1;
4613 	if (size <= 0) {
4614 		WARN("objdump string size calculation failed");
4615 		return -1;
4616 	}
4617 
4618 	cmd = malloc(size);
4619 
4620 	/* real snprintf() */
4621 	snprintf(cmd, size, objdump_str, cross_compile, objname, funcs);
4622 	ret = system(cmd);
4623 	if (ret) {
4624 		WARN("disassembly failed: %d", ret);
4625 		return -1;
4626 	}
4627 
4628 	return 0;
4629 }
4630 
disas_warned_funcs(struct objtool_file * file)4631 static int disas_warned_funcs(struct objtool_file *file)
4632 {
4633 	struct symbol *sym;
4634 	char *funcs = NULL, *tmp;
4635 
4636 	for_each_sym(file, sym) {
4637 		if (sym->warned) {
4638 			if (!funcs) {
4639 				funcs = malloc(strlen(sym->name) + 1);
4640 				strcpy(funcs, sym->name);
4641 			} else {
4642 				tmp = malloc(strlen(funcs) + strlen(sym->name) + 2);
4643 				sprintf(tmp, "%s %s", funcs, sym->name);
4644 				free(funcs);
4645 				funcs = tmp;
4646 			}
4647 		}
4648 	}
4649 
4650 	if (funcs)
4651 		disas_funcs(funcs);
4652 
4653 	return 0;
4654 }
4655 
4656 struct insn_chunk {
4657 	void *addr;
4658 	struct insn_chunk *next;
4659 };
4660 
4661 /*
4662  * Reduce peak RSS usage by freeing insns memory before writing the ELF file,
4663  * which can trigger more allocations for .debug_* sections whose data hasn't
4664  * been read yet.
4665  */
free_insns(struct objtool_file * file)4666 static void free_insns(struct objtool_file *file)
4667 {
4668 	struct instruction *insn;
4669 	struct insn_chunk *chunks = NULL, *chunk;
4670 
4671 	for_each_insn(file, insn) {
4672 		if (!insn->idx) {
4673 			chunk = malloc(sizeof(*chunk));
4674 			chunk->addr = insn;
4675 			chunk->next = chunks;
4676 			chunks = chunk;
4677 		}
4678 	}
4679 
4680 	for (chunk = chunks; chunk; chunk = chunk->next)
4681 		free(chunk->addr);
4682 }
4683 
check(struct objtool_file * file)4684 int check(struct objtool_file *file)
4685 {
4686 	int ret, warnings = 0;
4687 
4688 	arch_initial_func_cfi_state(&initial_func_cfi);
4689 	init_cfi_state(&init_cfi);
4690 	init_cfi_state(&func_cfi);
4691 	set_func_state(&func_cfi);
4692 	init_cfi_state(&force_undefined_cfi);
4693 	force_undefined_cfi.force_undefined = true;
4694 
4695 	if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3))) {
4696 		ret = -1;
4697 		goto out;
4698 	}
4699 
4700 	cfi_hash_add(&init_cfi);
4701 	cfi_hash_add(&func_cfi);
4702 
4703 	ret = decode_sections(file);
4704 	if (ret < 0)
4705 		goto out;
4706 
4707 	warnings += ret;
4708 
4709 	if (!nr_insns)
4710 		goto out;
4711 
4712 	if (opts.retpoline) {
4713 		ret = validate_retpoline(file);
4714 		if (ret < 0)
4715 			goto out;
4716 		warnings += ret;
4717 	}
4718 
4719 	if (opts.stackval || opts.orc || opts.uaccess) {
4720 		ret = validate_functions(file);
4721 		if (ret < 0)
4722 			goto out;
4723 		warnings += ret;
4724 
4725 		ret = validate_unwind_hints(file, NULL);
4726 		if (ret < 0)
4727 			goto out;
4728 		warnings += ret;
4729 
4730 		if (!warnings) {
4731 			ret = validate_reachable_instructions(file);
4732 			if (ret < 0)
4733 				goto out;
4734 			warnings += ret;
4735 		}
4736 
4737 	} else if (opts.noinstr) {
4738 		ret = validate_noinstr_sections(file);
4739 		if (ret < 0)
4740 			goto out;
4741 		warnings += ret;
4742 	}
4743 
4744 	if (opts.unret) {
4745 		/*
4746 		 * Must be after validate_branch() and friends, it plays
4747 		 * further games with insn->visited.
4748 		 */
4749 		ret = validate_unrets(file);
4750 		if (ret < 0)
4751 			goto out;
4752 		warnings += ret;
4753 	}
4754 
4755 	if (opts.ibt) {
4756 		ret = validate_ibt(file);
4757 		if (ret < 0)
4758 			goto out;
4759 		warnings += ret;
4760 	}
4761 
4762 	if (opts.sls) {
4763 		ret = validate_sls(file);
4764 		if (ret < 0)
4765 			goto out;
4766 		warnings += ret;
4767 	}
4768 
4769 	if (opts.static_call) {
4770 		ret = create_static_call_sections(file);
4771 		if (ret < 0)
4772 			goto out;
4773 		warnings += ret;
4774 	}
4775 
4776 	if (opts.retpoline) {
4777 		ret = create_retpoline_sites_sections(file);
4778 		if (ret < 0)
4779 			goto out;
4780 		warnings += ret;
4781 	}
4782 
4783 	if (opts.cfi) {
4784 		ret = create_cfi_sections(file);
4785 		if (ret < 0)
4786 			goto out;
4787 		warnings += ret;
4788 	}
4789 
4790 	if (opts.rethunk) {
4791 		ret = create_return_sites_sections(file);
4792 		if (ret < 0)
4793 			goto out;
4794 		warnings += ret;
4795 
4796 		if (opts.hack_skylake) {
4797 			ret = create_direct_call_sections(file);
4798 			if (ret < 0)
4799 				goto out;
4800 			warnings += ret;
4801 		}
4802 	}
4803 
4804 	if (opts.mcount) {
4805 		ret = create_mcount_loc_sections(file);
4806 		if (ret < 0)
4807 			goto out;
4808 		warnings += ret;
4809 	}
4810 
4811 	if (opts.prefix) {
4812 		ret = add_prefix_symbols(file);
4813 		if (ret < 0)
4814 			goto out;
4815 		warnings += ret;
4816 	}
4817 
4818 	if (opts.ibt) {
4819 		ret = create_ibt_endbr_seal_sections(file);
4820 		if (ret < 0)
4821 			goto out;
4822 		warnings += ret;
4823 	}
4824 
4825 	if (opts.orc && nr_insns) {
4826 		ret = orc_create(file);
4827 		if (ret < 0)
4828 			goto out;
4829 		warnings += ret;
4830 	}
4831 
4832 	free_insns(file);
4833 
4834 	if (opts.verbose)
4835 		disas_warned_funcs(file);
4836 
4837 	if (opts.stats) {
4838 		printf("nr_insns_visited: %ld\n", nr_insns_visited);
4839 		printf("nr_cfi: %ld\n", nr_cfi);
4840 		printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
4841 		printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
4842 	}
4843 
4844 out:
4845 	/*
4846 	 *  For now, don't fail the kernel build on fatal warnings.  These
4847 	 *  errors are still fairly common due to the growing matrix of
4848 	 *  supported toolchains and their recent pace of change.
4849 	 */
4850 	return 0;
4851 }
4852