xref: /linux/tools/objtool/check.c (revision 791d3ef2e11100449837dc0b6fe884e60ca3a484)
1 /*
2  * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <string.h>
19 #include <stdlib.h>
20 
21 #include "builtin.h"
22 #include "check.h"
23 #include "elf.h"
24 #include "special.h"
25 #include "arch.h"
26 #include "warn.h"
27 
28 #include <linux/hashtable.h>
29 #include <linux/kernel.h>
30 
31 struct alternative {
32 	struct list_head list;
33 	struct instruction *insn;
34 };
35 
36 const char *objname;
37 struct cfi_state initial_func_cfi;
38 
39 struct instruction *find_insn(struct objtool_file *file,
40 			      struct section *sec, unsigned long offset)
41 {
42 	struct instruction *insn;
43 
44 	hash_for_each_possible(file->insn_hash, insn, hash, offset)
45 		if (insn->sec == sec && insn->offset == offset)
46 			return insn;
47 
48 	return NULL;
49 }
50 
51 static struct instruction *next_insn_same_sec(struct objtool_file *file,
52 					      struct instruction *insn)
53 {
54 	struct instruction *next = list_next_entry(insn, list);
55 
56 	if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
57 		return NULL;
58 
59 	return next;
60 }
61 
62 static struct instruction *next_insn_same_func(struct objtool_file *file,
63 					       struct instruction *insn)
64 {
65 	struct instruction *next = list_next_entry(insn, list);
66 	struct symbol *func = insn->func;
67 
68 	if (!func)
69 		return NULL;
70 
71 	if (&next->list != &file->insn_list && next->func == func)
72 		return next;
73 
74 	/* Check if we're already in the subfunction: */
75 	if (func == func->cfunc)
76 		return NULL;
77 
78 	/* Move to the subfunction: */
79 	return find_insn(file, func->cfunc->sec, func->cfunc->offset);
80 }
81 
82 #define func_for_each_insn_all(file, func, insn)			\
83 	for (insn = find_insn(file, func->sec, func->offset);		\
84 	     insn;							\
85 	     insn = next_insn_same_func(file, insn))
86 
87 #define func_for_each_insn(file, func, insn)				\
88 	for (insn = find_insn(file, func->sec, func->offset);		\
89 	     insn && &insn->list != &file->insn_list &&			\
90 		insn->sec == func->sec &&				\
91 		insn->offset < func->offset + func->len;		\
92 	     insn = list_next_entry(insn, list))
93 
94 #define func_for_each_insn_continue_reverse(file, func, insn)		\
95 	for (insn = list_prev_entry(insn, list);			\
96 	     &insn->list != &file->insn_list &&				\
97 		insn->sec == func->sec && insn->offset >= func->offset;	\
98 	     insn = list_prev_entry(insn, list))
99 
100 #define sec_for_each_insn_from(file, insn)				\
101 	for (; insn; insn = next_insn_same_sec(file, insn))
102 
103 #define sec_for_each_insn_continue(file, insn)				\
104 	for (insn = next_insn_same_sec(file, insn); insn;		\
105 	     insn = next_insn_same_sec(file, insn))
106 
107 /*
108  * Check if the function has been manually whitelisted with the
109  * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
110  * due to its use of a context switching instruction.
111  */
112 static bool ignore_func(struct objtool_file *file, struct symbol *func)
113 {
114 	struct rela *rela;
115 
116 	/* check for STACK_FRAME_NON_STANDARD */
117 	if (file->whitelist && file->whitelist->rela)
118 		list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) {
119 			if (rela->sym->type == STT_SECTION &&
120 			    rela->sym->sec == func->sec &&
121 			    rela->addend == func->offset)
122 				return true;
123 			if (rela->sym->type == STT_FUNC && rela->sym == func)
124 				return true;
125 		}
126 
127 	return false;
128 }
129 
130 /*
131  * This checks to see if the given function is a "noreturn" function.
132  *
133  * For global functions which are outside the scope of this object file, we
134  * have to keep a manual list of them.
135  *
136  * For local functions, we have to detect them manually by simply looking for
137  * the lack of a return instruction.
138  *
139  * Returns:
140  *  -1: error
141  *   0: no dead end
142  *   1: dead end
143  */
144 static int __dead_end_function(struct objtool_file *file, struct symbol *func,
145 			       int recursion)
146 {
147 	int i;
148 	struct instruction *insn;
149 	bool empty = true;
150 
151 	/*
152 	 * Unfortunately these have to be hard coded because the noreturn
153 	 * attribute isn't provided in ELF data.
154 	 */
155 	static const char * const global_noreturns[] = {
156 		"__stack_chk_fail",
157 		"panic",
158 		"do_exit",
159 		"do_task_dead",
160 		"__module_put_and_exit",
161 		"complete_and_exit",
162 		"kvm_spurious_fault",
163 		"__reiserfs_panic",
164 		"lbug_with_loc",
165 		"fortify_panic",
166 		"usercopy_abort",
167 		"machine_real_restart",
168 	};
169 
170 	if (func->bind == STB_WEAK)
171 		return 0;
172 
173 	if (func->bind == STB_GLOBAL)
174 		for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
175 			if (!strcmp(func->name, global_noreturns[i]))
176 				return 1;
177 
178 	if (!func->len)
179 		return 0;
180 
181 	insn = find_insn(file, func->sec, func->offset);
182 	if (!insn->func)
183 		return 0;
184 
185 	func_for_each_insn_all(file, func, insn) {
186 		empty = false;
187 
188 		if (insn->type == INSN_RETURN)
189 			return 0;
190 	}
191 
192 	if (empty)
193 		return 0;
194 
195 	/*
196 	 * A function can have a sibling call instead of a return.  In that
197 	 * case, the function's dead-end status depends on whether the target
198 	 * of the sibling call returns.
199 	 */
200 	func_for_each_insn_all(file, func, insn) {
201 		if (insn->type == INSN_JUMP_UNCONDITIONAL) {
202 			struct instruction *dest = insn->jump_dest;
203 
204 			if (!dest)
205 				/* sibling call to another file */
206 				return 0;
207 
208 			if (dest->func && dest->func->pfunc != insn->func->pfunc) {
209 
210 				/* local sibling call */
211 				if (recursion == 5) {
212 					/*
213 					 * Infinite recursion: two functions
214 					 * have sibling calls to each other.
215 					 * This is a very rare case.  It means
216 					 * they aren't dead ends.
217 					 */
218 					return 0;
219 				}
220 
221 				return __dead_end_function(file, dest->func,
222 							   recursion + 1);
223 			}
224 		}
225 
226 		if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
227 			/* sibling call */
228 			return 0;
229 	}
230 
231 	return 1;
232 }
233 
234 static int dead_end_function(struct objtool_file *file, struct symbol *func)
235 {
236 	return __dead_end_function(file, func, 0);
237 }
238 
239 static void clear_insn_state(struct insn_state *state)
240 {
241 	int i;
242 
243 	memset(state, 0, sizeof(*state));
244 	state->cfa.base = CFI_UNDEFINED;
245 	for (i = 0; i < CFI_NUM_REGS; i++) {
246 		state->regs[i].base = CFI_UNDEFINED;
247 		state->vals[i].base = CFI_UNDEFINED;
248 	}
249 	state->drap_reg = CFI_UNDEFINED;
250 	state->drap_offset = -1;
251 }
252 
253 /*
254  * Call the arch-specific instruction decoder for all the instructions and add
255  * them to the global instruction list.
256  */
257 static int decode_instructions(struct objtool_file *file)
258 {
259 	struct section *sec;
260 	struct symbol *func;
261 	unsigned long offset;
262 	struct instruction *insn;
263 	int ret;
264 
265 	for_each_sec(file, sec) {
266 
267 		if (!(sec->sh.sh_flags & SHF_EXECINSTR))
268 			continue;
269 
270 		if (strcmp(sec->name, ".altinstr_replacement") &&
271 		    strcmp(sec->name, ".altinstr_aux") &&
272 		    strncmp(sec->name, ".discard.", 9))
273 			sec->text = true;
274 
275 		for (offset = 0; offset < sec->len; offset += insn->len) {
276 			insn = malloc(sizeof(*insn));
277 			if (!insn) {
278 				WARN("malloc failed");
279 				return -1;
280 			}
281 			memset(insn, 0, sizeof(*insn));
282 			INIT_LIST_HEAD(&insn->alts);
283 			clear_insn_state(&insn->state);
284 
285 			insn->sec = sec;
286 			insn->offset = offset;
287 
288 			ret = arch_decode_instruction(file->elf, sec, offset,
289 						      sec->len - offset,
290 						      &insn->len, &insn->type,
291 						      &insn->immediate,
292 						      &insn->stack_op);
293 			if (ret)
294 				goto err;
295 
296 			if (!insn->type || insn->type > INSN_LAST) {
297 				WARN_FUNC("invalid instruction type %d",
298 					  insn->sec, insn->offset, insn->type);
299 				ret = -1;
300 				goto err;
301 			}
302 
303 			hash_add(file->insn_hash, &insn->hash, insn->offset);
304 			list_add_tail(&insn->list, &file->insn_list);
305 		}
306 
307 		list_for_each_entry(func, &sec->symbol_list, list) {
308 			if (func->type != STT_FUNC)
309 				continue;
310 
311 			if (!find_insn(file, sec, func->offset)) {
312 				WARN("%s(): can't find starting instruction",
313 				     func->name);
314 				return -1;
315 			}
316 
317 			func_for_each_insn(file, func, insn)
318 				if (!insn->func)
319 					insn->func = func;
320 		}
321 	}
322 
323 	return 0;
324 
325 err:
326 	free(insn);
327 	return ret;
328 }
329 
330 /*
331  * Mark "ud2" instructions and manually annotated dead ends.
332  */
333 static int add_dead_ends(struct objtool_file *file)
334 {
335 	struct section *sec;
336 	struct rela *rela;
337 	struct instruction *insn;
338 	bool found;
339 
340 	/*
341 	 * By default, "ud2" is a dead end unless otherwise annotated, because
342 	 * GCC 7 inserts it for certain divide-by-zero cases.
343 	 */
344 	for_each_insn(file, insn)
345 		if (insn->type == INSN_BUG)
346 			insn->dead_end = true;
347 
348 	/*
349 	 * Check for manually annotated dead ends.
350 	 */
351 	sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
352 	if (!sec)
353 		goto reachable;
354 
355 	list_for_each_entry(rela, &sec->rela_list, list) {
356 		if (rela->sym->type != STT_SECTION) {
357 			WARN("unexpected relocation symbol type in %s", sec->name);
358 			return -1;
359 		}
360 		insn = find_insn(file, rela->sym->sec, rela->addend);
361 		if (insn)
362 			insn = list_prev_entry(insn, list);
363 		else if (rela->addend == rela->sym->sec->len) {
364 			found = false;
365 			list_for_each_entry_reverse(insn, &file->insn_list, list) {
366 				if (insn->sec == rela->sym->sec) {
367 					found = true;
368 					break;
369 				}
370 			}
371 
372 			if (!found) {
373 				WARN("can't find unreachable insn at %s+0x%x",
374 				     rela->sym->sec->name, rela->addend);
375 				return -1;
376 			}
377 		} else {
378 			WARN("can't find unreachable insn at %s+0x%x",
379 			     rela->sym->sec->name, rela->addend);
380 			return -1;
381 		}
382 
383 		insn->dead_end = true;
384 	}
385 
386 reachable:
387 	/*
388 	 * These manually annotated reachable checks are needed for GCC 4.4,
389 	 * where the Linux unreachable() macro isn't supported.  In that case
390 	 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
391 	 * not a dead end.
392 	 */
393 	sec = find_section_by_name(file->elf, ".rela.discard.reachable");
394 	if (!sec)
395 		return 0;
396 
397 	list_for_each_entry(rela, &sec->rela_list, list) {
398 		if (rela->sym->type != STT_SECTION) {
399 			WARN("unexpected relocation symbol type in %s", sec->name);
400 			return -1;
401 		}
402 		insn = find_insn(file, rela->sym->sec, rela->addend);
403 		if (insn)
404 			insn = list_prev_entry(insn, list);
405 		else if (rela->addend == rela->sym->sec->len) {
406 			found = false;
407 			list_for_each_entry_reverse(insn, &file->insn_list, list) {
408 				if (insn->sec == rela->sym->sec) {
409 					found = true;
410 					break;
411 				}
412 			}
413 
414 			if (!found) {
415 				WARN("can't find reachable insn at %s+0x%x",
416 				     rela->sym->sec->name, rela->addend);
417 				return -1;
418 			}
419 		} else {
420 			WARN("can't find reachable insn at %s+0x%x",
421 			     rela->sym->sec->name, rela->addend);
422 			return -1;
423 		}
424 
425 		insn->dead_end = false;
426 	}
427 
428 	return 0;
429 }
430 
431 /*
432  * Warnings shouldn't be reported for ignored functions.
433  */
434 static void add_ignores(struct objtool_file *file)
435 {
436 	struct instruction *insn;
437 	struct section *sec;
438 	struct symbol *func;
439 
440 	for_each_sec(file, sec) {
441 		list_for_each_entry(func, &sec->symbol_list, list) {
442 			if (func->type != STT_FUNC)
443 				continue;
444 
445 			if (!ignore_func(file, func))
446 				continue;
447 
448 			func_for_each_insn_all(file, func, insn)
449 				insn->ignore = true;
450 		}
451 	}
452 }
453 
454 /*
455  * FIXME: For now, just ignore any alternatives which add retpolines.  This is
456  * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
457  * But it at least allows objtool to understand the control flow *around* the
458  * retpoline.
459  */
460 static int add_nospec_ignores(struct objtool_file *file)
461 {
462 	struct section *sec;
463 	struct rela *rela;
464 	struct instruction *insn;
465 
466 	sec = find_section_by_name(file->elf, ".rela.discard.nospec");
467 	if (!sec)
468 		return 0;
469 
470 	list_for_each_entry(rela, &sec->rela_list, list) {
471 		if (rela->sym->type != STT_SECTION) {
472 			WARN("unexpected relocation symbol type in %s", sec->name);
473 			return -1;
474 		}
475 
476 		insn = find_insn(file, rela->sym->sec, rela->addend);
477 		if (!insn) {
478 			WARN("bad .discard.nospec entry");
479 			return -1;
480 		}
481 
482 		insn->ignore_alts = true;
483 	}
484 
485 	return 0;
486 }
487 
488 /*
489  * Find the destination instructions for all jumps.
490  */
491 static int add_jump_destinations(struct objtool_file *file)
492 {
493 	struct instruction *insn;
494 	struct rela *rela;
495 	struct section *dest_sec;
496 	unsigned long dest_off;
497 
498 	for_each_insn(file, insn) {
499 		if (insn->type != INSN_JUMP_CONDITIONAL &&
500 		    insn->type != INSN_JUMP_UNCONDITIONAL)
501 			continue;
502 
503 		if (insn->ignore)
504 			continue;
505 
506 		rela = find_rela_by_dest_range(insn->sec, insn->offset,
507 					       insn->len);
508 		if (!rela) {
509 			dest_sec = insn->sec;
510 			dest_off = insn->offset + insn->len + insn->immediate;
511 		} else if (rela->sym->type == STT_SECTION) {
512 			dest_sec = rela->sym->sec;
513 			dest_off = rela->addend + 4;
514 		} else if (rela->sym->sec->idx) {
515 			dest_sec = rela->sym->sec;
516 			dest_off = rela->sym->sym.st_value + rela->addend + 4;
517 		} else if (strstr(rela->sym->name, "_indirect_thunk_")) {
518 			/*
519 			 * Retpoline jumps are really dynamic jumps in
520 			 * disguise, so convert them accordingly.
521 			 */
522 			insn->type = INSN_JUMP_DYNAMIC;
523 			insn->retpoline_safe = true;
524 			continue;
525 		} else {
526 			/* sibling call */
527 			insn->jump_dest = 0;
528 			continue;
529 		}
530 
531 		insn->jump_dest = find_insn(file, dest_sec, dest_off);
532 		if (!insn->jump_dest) {
533 
534 			/*
535 			 * This is a special case where an alt instruction
536 			 * jumps past the end of the section.  These are
537 			 * handled later in handle_group_alt().
538 			 */
539 			if (!strcmp(insn->sec->name, ".altinstr_replacement"))
540 				continue;
541 
542 			WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
543 				  insn->sec, insn->offset, dest_sec->name,
544 				  dest_off);
545 			return -1;
546 		}
547 
548 		/*
549 		 * For GCC 8+, create parent/child links for any cold
550 		 * subfunctions.  This is _mostly_ redundant with a similar
551 		 * initialization in read_symbols().
552 		 *
553 		 * If a function has aliases, we want the *first* such function
554 		 * in the symbol table to be the subfunction's parent.  In that
555 		 * case we overwrite the initialization done in read_symbols().
556 		 *
557 		 * However this code can't completely replace the
558 		 * read_symbols() code because this doesn't detect the case
559 		 * where the parent function's only reference to a subfunction
560 		 * is through a switch table.
561 		 */
562 		if (insn->func && insn->jump_dest->func &&
563 		    insn->func != insn->jump_dest->func &&
564 		    !strstr(insn->func->name, ".cold.") &&
565 		    strstr(insn->jump_dest->func->name, ".cold.")) {
566 			insn->func->cfunc = insn->jump_dest->func;
567 			insn->jump_dest->func->pfunc = insn->func;
568 		}
569 	}
570 
571 	return 0;
572 }
573 
574 /*
575  * Find the destination instructions for all calls.
576  */
577 static int add_call_destinations(struct objtool_file *file)
578 {
579 	struct instruction *insn;
580 	unsigned long dest_off;
581 	struct rela *rela;
582 
583 	for_each_insn(file, insn) {
584 		if (insn->type != INSN_CALL)
585 			continue;
586 
587 		rela = find_rela_by_dest_range(insn->sec, insn->offset,
588 					       insn->len);
589 		if (!rela) {
590 			dest_off = insn->offset + insn->len + insn->immediate;
591 			insn->call_dest = find_symbol_by_offset(insn->sec,
592 								dest_off);
593 
594 			if (!insn->call_dest && !insn->ignore) {
595 				WARN_FUNC("unsupported intra-function call",
596 					  insn->sec, insn->offset);
597 				if (retpoline)
598 					WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
599 				return -1;
600 			}
601 
602 		} else if (rela->sym->type == STT_SECTION) {
603 			insn->call_dest = find_symbol_by_offset(rela->sym->sec,
604 								rela->addend+4);
605 			if (!insn->call_dest ||
606 			    insn->call_dest->type != STT_FUNC) {
607 				WARN_FUNC("can't find call dest symbol at %s+0x%x",
608 					  insn->sec, insn->offset,
609 					  rela->sym->sec->name,
610 					  rela->addend + 4);
611 				return -1;
612 			}
613 		} else
614 			insn->call_dest = rela->sym;
615 	}
616 
617 	return 0;
618 }
619 
620 /*
621  * The .alternatives section requires some extra special care, over and above
622  * what other special sections require:
623  *
624  * 1. Because alternatives are patched in-place, we need to insert a fake jump
625  *    instruction at the end so that validate_branch() skips all the original
626  *    replaced instructions when validating the new instruction path.
627  *
628  * 2. An added wrinkle is that the new instruction length might be zero.  In
629  *    that case the old instructions are replaced with noops.  We simulate that
630  *    by creating a fake jump as the only new instruction.
631  *
632  * 3. In some cases, the alternative section includes an instruction which
633  *    conditionally jumps to the _end_ of the entry.  We have to modify these
634  *    jumps' destinations to point back to .text rather than the end of the
635  *    entry in .altinstr_replacement.
636  *
637  * 4. It has been requested that we don't validate the !POPCNT feature path
638  *    which is a "very very small percentage of machines".
639  */
640 static int handle_group_alt(struct objtool_file *file,
641 			    struct special_alt *special_alt,
642 			    struct instruction *orig_insn,
643 			    struct instruction **new_insn)
644 {
645 	struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
646 	unsigned long dest_off;
647 
648 	last_orig_insn = NULL;
649 	insn = orig_insn;
650 	sec_for_each_insn_from(file, insn) {
651 		if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
652 			break;
653 
654 		if (special_alt->skip_orig)
655 			insn->type = INSN_NOP;
656 
657 		insn->alt_group = true;
658 		last_orig_insn = insn;
659 	}
660 
661 	if (next_insn_same_sec(file, last_orig_insn)) {
662 		fake_jump = malloc(sizeof(*fake_jump));
663 		if (!fake_jump) {
664 			WARN("malloc failed");
665 			return -1;
666 		}
667 		memset(fake_jump, 0, sizeof(*fake_jump));
668 		INIT_LIST_HEAD(&fake_jump->alts);
669 		clear_insn_state(&fake_jump->state);
670 
671 		fake_jump->sec = special_alt->new_sec;
672 		fake_jump->offset = -1;
673 		fake_jump->type = INSN_JUMP_UNCONDITIONAL;
674 		fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
675 		fake_jump->ignore = true;
676 	}
677 
678 	if (!special_alt->new_len) {
679 		if (!fake_jump) {
680 			WARN("%s: empty alternative at end of section",
681 			     special_alt->orig_sec->name);
682 			return -1;
683 		}
684 
685 		*new_insn = fake_jump;
686 		return 0;
687 	}
688 
689 	last_new_insn = NULL;
690 	insn = *new_insn;
691 	sec_for_each_insn_from(file, insn) {
692 		if (insn->offset >= special_alt->new_off + special_alt->new_len)
693 			break;
694 
695 		last_new_insn = insn;
696 
697 		insn->ignore = orig_insn->ignore_alts;
698 
699 		if (insn->type != INSN_JUMP_CONDITIONAL &&
700 		    insn->type != INSN_JUMP_UNCONDITIONAL)
701 			continue;
702 
703 		if (!insn->immediate)
704 			continue;
705 
706 		dest_off = insn->offset + insn->len + insn->immediate;
707 		if (dest_off == special_alt->new_off + special_alt->new_len) {
708 			if (!fake_jump) {
709 				WARN("%s: alternative jump to end of section",
710 				     special_alt->orig_sec->name);
711 				return -1;
712 			}
713 			insn->jump_dest = fake_jump;
714 		}
715 
716 		if (!insn->jump_dest) {
717 			WARN_FUNC("can't find alternative jump destination",
718 				  insn->sec, insn->offset);
719 			return -1;
720 		}
721 	}
722 
723 	if (!last_new_insn) {
724 		WARN_FUNC("can't find last new alternative instruction",
725 			  special_alt->new_sec, special_alt->new_off);
726 		return -1;
727 	}
728 
729 	if (fake_jump)
730 		list_add(&fake_jump->list, &last_new_insn->list);
731 
732 	return 0;
733 }
734 
735 /*
736  * A jump table entry can either convert a nop to a jump or a jump to a nop.
737  * If the original instruction is a jump, make the alt entry an effective nop
738  * by just skipping the original instruction.
739  */
740 static int handle_jump_alt(struct objtool_file *file,
741 			   struct special_alt *special_alt,
742 			   struct instruction *orig_insn,
743 			   struct instruction **new_insn)
744 {
745 	if (orig_insn->type == INSN_NOP)
746 		return 0;
747 
748 	if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
749 		WARN_FUNC("unsupported instruction at jump label",
750 			  orig_insn->sec, orig_insn->offset);
751 		return -1;
752 	}
753 
754 	*new_insn = list_next_entry(orig_insn, list);
755 	return 0;
756 }
757 
758 /*
759  * Read all the special sections which have alternate instructions which can be
760  * patched in or redirected to at runtime.  Each instruction having alternate
761  * instruction(s) has them added to its insn->alts list, which will be
762  * traversed in validate_branch().
763  */
764 static int add_special_section_alts(struct objtool_file *file)
765 {
766 	struct list_head special_alts;
767 	struct instruction *orig_insn, *new_insn;
768 	struct special_alt *special_alt, *tmp;
769 	struct alternative *alt;
770 	int ret;
771 
772 	ret = special_get_alts(file->elf, &special_alts);
773 	if (ret)
774 		return ret;
775 
776 	list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
777 
778 		orig_insn = find_insn(file, special_alt->orig_sec,
779 				      special_alt->orig_off);
780 		if (!orig_insn) {
781 			WARN_FUNC("special: can't find orig instruction",
782 				  special_alt->orig_sec, special_alt->orig_off);
783 			ret = -1;
784 			goto out;
785 		}
786 
787 		new_insn = NULL;
788 		if (!special_alt->group || special_alt->new_len) {
789 			new_insn = find_insn(file, special_alt->new_sec,
790 					     special_alt->new_off);
791 			if (!new_insn) {
792 				WARN_FUNC("special: can't find new instruction",
793 					  special_alt->new_sec,
794 					  special_alt->new_off);
795 				ret = -1;
796 				goto out;
797 			}
798 		}
799 
800 		if (special_alt->group) {
801 			ret = handle_group_alt(file, special_alt, orig_insn,
802 					       &new_insn);
803 			if (ret)
804 				goto out;
805 		} else if (special_alt->jump_or_nop) {
806 			ret = handle_jump_alt(file, special_alt, orig_insn,
807 					      &new_insn);
808 			if (ret)
809 				goto out;
810 		}
811 
812 		alt = malloc(sizeof(*alt));
813 		if (!alt) {
814 			WARN("malloc failed");
815 			ret = -1;
816 			goto out;
817 		}
818 
819 		alt->insn = new_insn;
820 		list_add_tail(&alt->list, &orig_insn->alts);
821 
822 		list_del(&special_alt->list);
823 		free(special_alt);
824 	}
825 
826 out:
827 	return ret;
828 }
829 
830 static int add_switch_table(struct objtool_file *file, struct instruction *insn,
831 			    struct rela *table, struct rela *next_table)
832 {
833 	struct rela *rela = table;
834 	struct instruction *alt_insn;
835 	struct alternative *alt;
836 	struct symbol *pfunc = insn->func->pfunc;
837 	unsigned int prev_offset = 0;
838 
839 	list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
840 		if (rela == next_table)
841 			break;
842 
843 		/* Make sure the switch table entries are consecutive: */
844 		if (prev_offset && rela->offset != prev_offset + 8)
845 			break;
846 
847 		/* Detect function pointers from contiguous objects: */
848 		if (rela->sym->sec == pfunc->sec &&
849 		    rela->addend == pfunc->offset)
850 			break;
851 
852 		alt_insn = find_insn(file, rela->sym->sec, rela->addend);
853 		if (!alt_insn)
854 			break;
855 
856 		/* Make sure the jmp dest is in the function or subfunction: */
857 		if (alt_insn->func->pfunc != pfunc)
858 			break;
859 
860 		alt = malloc(sizeof(*alt));
861 		if (!alt) {
862 			WARN("malloc failed");
863 			return -1;
864 		}
865 
866 		alt->insn = alt_insn;
867 		list_add_tail(&alt->list, &insn->alts);
868 		prev_offset = rela->offset;
869 	}
870 
871 	if (!prev_offset) {
872 		WARN_FUNC("can't find switch jump table",
873 			  insn->sec, insn->offset);
874 		return -1;
875 	}
876 
877 	return 0;
878 }
879 
880 /*
881  * find_switch_table() - Given a dynamic jump, find the switch jump table in
882  * .rodata associated with it.
883  *
884  * There are 3 basic patterns:
885  *
886  * 1. jmpq *[rodata addr](,%reg,8)
887  *
888  *    This is the most common case by far.  It jumps to an address in a simple
889  *    jump table which is stored in .rodata.
890  *
891  * 2. jmpq *[rodata addr](%rip)
892  *
893  *    This is caused by a rare GCC quirk, currently only seen in three driver
894  *    functions in the kernel, only with certain obscure non-distro configs.
895  *
896  *    As part of an optimization, GCC makes a copy of an existing switch jump
897  *    table, modifies it, and then hard-codes the jump (albeit with an indirect
898  *    jump) to use a single entry in the table.  The rest of the jump table and
899  *    some of its jump targets remain as dead code.
900  *
901  *    In such a case we can just crudely ignore all unreachable instruction
902  *    warnings for the entire object file.  Ideally we would just ignore them
903  *    for the function, but that would require redesigning the code quite a
904  *    bit.  And honestly that's just not worth doing: unreachable instruction
905  *    warnings are of questionable value anyway, and this is such a rare issue.
906  *
907  * 3. mov [rodata addr],%reg1
908  *    ... some instructions ...
909  *    jmpq *(%reg1,%reg2,8)
910  *
911  *    This is a fairly uncommon pattern which is new for GCC 6.  As of this
912  *    writing, there are 11 occurrences of it in the allmodconfig kernel.
913  *
914  *    As of GCC 7 there are quite a few more of these and the 'in between' code
915  *    is significant. Esp. with KASAN enabled some of the code between the mov
916  *    and jmpq uses .rodata itself, which can confuse things.
917  *
918  *    TODO: Once we have DWARF CFI and smarter instruction decoding logic,
919  *    ensure the same register is used in the mov and jump instructions.
920  *
921  *    NOTE: RETPOLINE made it harder still to decode dynamic jumps.
922  */
923 static struct rela *find_switch_table(struct objtool_file *file,
924 				      struct symbol *func,
925 				      struct instruction *insn)
926 {
927 	struct rela *text_rela, *rodata_rela;
928 	struct instruction *orig_insn = insn;
929 	unsigned long table_offset;
930 
931 	/*
932 	 * Backward search using the @first_jump_src links, these help avoid
933 	 * much of the 'in between' code. Which avoids us getting confused by
934 	 * it.
935 	 */
936 	for (;
937 	     &insn->list != &file->insn_list &&
938 	     insn->sec == func->sec &&
939 	     insn->offset >= func->offset;
940 
941 	     insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
942 
943 		if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
944 			break;
945 
946 		/* allow small jumps within the range */
947 		if (insn->type == INSN_JUMP_UNCONDITIONAL &&
948 		    insn->jump_dest &&
949 		    (insn->jump_dest->offset <= insn->offset ||
950 		     insn->jump_dest->offset > orig_insn->offset))
951 		    break;
952 
953 		/* look for a relocation which references .rodata */
954 		text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
955 						    insn->len);
956 		if (!text_rela || text_rela->sym != file->rodata->sym)
957 			continue;
958 
959 		table_offset = text_rela->addend;
960 		if (text_rela->type == R_X86_64_PC32)
961 			table_offset += 4;
962 
963 		/*
964 		 * Make sure the .rodata address isn't associated with a
965 		 * symbol.  gcc jump tables are anonymous data.
966 		 */
967 		if (find_symbol_containing(file->rodata, table_offset))
968 			continue;
969 
970 		rodata_rela = find_rela_by_dest(file->rodata, table_offset);
971 		if (rodata_rela) {
972 			/*
973 			 * Use of RIP-relative switch jumps is quite rare, and
974 			 * indicates a rare GCC quirk/bug which can leave dead
975 			 * code behind.
976 			 */
977 			if (text_rela->type == R_X86_64_PC32)
978 				file->ignore_unreachables = true;
979 
980 			return rodata_rela;
981 		}
982 	}
983 
984 	return NULL;
985 }
986 
987 
988 static int add_func_switch_tables(struct objtool_file *file,
989 				  struct symbol *func)
990 {
991 	struct instruction *insn, *last = NULL, *prev_jump = NULL;
992 	struct rela *rela, *prev_rela = NULL;
993 	int ret;
994 
995 	func_for_each_insn_all(file, func, insn) {
996 		if (!last)
997 			last = insn;
998 
999 		/*
1000 		 * Store back-pointers for unconditional forward jumps such
1001 		 * that find_switch_table() can back-track using those and
1002 		 * avoid some potentially confusing code.
1003 		 */
1004 		if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1005 		    insn->offset > last->offset &&
1006 		    insn->jump_dest->offset > insn->offset &&
1007 		    !insn->jump_dest->first_jump_src) {
1008 
1009 			insn->jump_dest->first_jump_src = insn;
1010 			last = insn->jump_dest;
1011 		}
1012 
1013 		if (insn->type != INSN_JUMP_DYNAMIC)
1014 			continue;
1015 
1016 		rela = find_switch_table(file, func, insn);
1017 		if (!rela)
1018 			continue;
1019 
1020 		/*
1021 		 * We found a switch table, but we don't know yet how big it
1022 		 * is.  Don't add it until we reach the end of the function or
1023 		 * the beginning of another switch table in the same function.
1024 		 */
1025 		if (prev_jump) {
1026 			ret = add_switch_table(file, prev_jump, prev_rela, rela);
1027 			if (ret)
1028 				return ret;
1029 		}
1030 
1031 		prev_jump = insn;
1032 		prev_rela = rela;
1033 	}
1034 
1035 	if (prev_jump) {
1036 		ret = add_switch_table(file, prev_jump, prev_rela, NULL);
1037 		if (ret)
1038 			return ret;
1039 	}
1040 
1041 	return 0;
1042 }
1043 
1044 /*
1045  * For some switch statements, gcc generates a jump table in the .rodata
1046  * section which contains a list of addresses within the function to jump to.
1047  * This finds these jump tables and adds them to the insn->alts lists.
1048  */
1049 static int add_switch_table_alts(struct objtool_file *file)
1050 {
1051 	struct section *sec;
1052 	struct symbol *func;
1053 	int ret;
1054 
1055 	if (!file->rodata || !file->rodata->rela)
1056 		return 0;
1057 
1058 	for_each_sec(file, sec) {
1059 		list_for_each_entry(func, &sec->symbol_list, list) {
1060 			if (func->type != STT_FUNC)
1061 				continue;
1062 
1063 			ret = add_func_switch_tables(file, func);
1064 			if (ret)
1065 				return ret;
1066 		}
1067 	}
1068 
1069 	return 0;
1070 }
1071 
1072 static int read_unwind_hints(struct objtool_file *file)
1073 {
1074 	struct section *sec, *relasec;
1075 	struct rela *rela;
1076 	struct unwind_hint *hint;
1077 	struct instruction *insn;
1078 	struct cfi_reg *cfa;
1079 	int i;
1080 
1081 	sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1082 	if (!sec)
1083 		return 0;
1084 
1085 	relasec = sec->rela;
1086 	if (!relasec) {
1087 		WARN("missing .rela.discard.unwind_hints section");
1088 		return -1;
1089 	}
1090 
1091 	if (sec->len % sizeof(struct unwind_hint)) {
1092 		WARN("struct unwind_hint size mismatch");
1093 		return -1;
1094 	}
1095 
1096 	file->hints = true;
1097 
1098 	for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1099 		hint = (struct unwind_hint *)sec->data->d_buf + i;
1100 
1101 		rela = find_rela_by_dest(sec, i * sizeof(*hint));
1102 		if (!rela) {
1103 			WARN("can't find rela for unwind_hints[%d]", i);
1104 			return -1;
1105 		}
1106 
1107 		insn = find_insn(file, rela->sym->sec, rela->addend);
1108 		if (!insn) {
1109 			WARN("can't find insn for unwind_hints[%d]", i);
1110 			return -1;
1111 		}
1112 
1113 		cfa = &insn->state.cfa;
1114 
1115 		if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1116 			insn->save = true;
1117 			continue;
1118 
1119 		} else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1120 			insn->restore = true;
1121 			insn->hint = true;
1122 			continue;
1123 		}
1124 
1125 		insn->hint = true;
1126 
1127 		switch (hint->sp_reg) {
1128 		case ORC_REG_UNDEFINED:
1129 			cfa->base = CFI_UNDEFINED;
1130 			break;
1131 		case ORC_REG_SP:
1132 			cfa->base = CFI_SP;
1133 			break;
1134 		case ORC_REG_BP:
1135 			cfa->base = CFI_BP;
1136 			break;
1137 		case ORC_REG_SP_INDIRECT:
1138 			cfa->base = CFI_SP_INDIRECT;
1139 			break;
1140 		case ORC_REG_R10:
1141 			cfa->base = CFI_R10;
1142 			break;
1143 		case ORC_REG_R13:
1144 			cfa->base = CFI_R13;
1145 			break;
1146 		case ORC_REG_DI:
1147 			cfa->base = CFI_DI;
1148 			break;
1149 		case ORC_REG_DX:
1150 			cfa->base = CFI_DX;
1151 			break;
1152 		default:
1153 			WARN_FUNC("unsupported unwind_hint sp base reg %d",
1154 				  insn->sec, insn->offset, hint->sp_reg);
1155 			return -1;
1156 		}
1157 
1158 		cfa->offset = hint->sp_offset;
1159 		insn->state.type = hint->type;
1160 	}
1161 
1162 	return 0;
1163 }
1164 
1165 static int read_retpoline_hints(struct objtool_file *file)
1166 {
1167 	struct section *sec;
1168 	struct instruction *insn;
1169 	struct rela *rela;
1170 
1171 	sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1172 	if (!sec)
1173 		return 0;
1174 
1175 	list_for_each_entry(rela, &sec->rela_list, list) {
1176 		if (rela->sym->type != STT_SECTION) {
1177 			WARN("unexpected relocation symbol type in %s", sec->name);
1178 			return -1;
1179 		}
1180 
1181 		insn = find_insn(file, rela->sym->sec, rela->addend);
1182 		if (!insn) {
1183 			WARN("bad .discard.retpoline_safe entry");
1184 			return -1;
1185 		}
1186 
1187 		if (insn->type != INSN_JUMP_DYNAMIC &&
1188 		    insn->type != INSN_CALL_DYNAMIC) {
1189 			WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1190 				  insn->sec, insn->offset);
1191 			return -1;
1192 		}
1193 
1194 		insn->retpoline_safe = true;
1195 	}
1196 
1197 	return 0;
1198 }
1199 
1200 static int decode_sections(struct objtool_file *file)
1201 {
1202 	int ret;
1203 
1204 	ret = decode_instructions(file);
1205 	if (ret)
1206 		return ret;
1207 
1208 	ret = add_dead_ends(file);
1209 	if (ret)
1210 		return ret;
1211 
1212 	add_ignores(file);
1213 
1214 	ret = add_nospec_ignores(file);
1215 	if (ret)
1216 		return ret;
1217 
1218 	ret = add_jump_destinations(file);
1219 	if (ret)
1220 		return ret;
1221 
1222 	ret = add_special_section_alts(file);
1223 	if (ret)
1224 		return ret;
1225 
1226 	ret = add_call_destinations(file);
1227 	if (ret)
1228 		return ret;
1229 
1230 	ret = add_switch_table_alts(file);
1231 	if (ret)
1232 		return ret;
1233 
1234 	ret = read_unwind_hints(file);
1235 	if (ret)
1236 		return ret;
1237 
1238 	ret = read_retpoline_hints(file);
1239 	if (ret)
1240 		return ret;
1241 
1242 	return 0;
1243 }
1244 
1245 static bool is_fentry_call(struct instruction *insn)
1246 {
1247 	if (insn->type == INSN_CALL &&
1248 	    insn->call_dest->type == STT_NOTYPE &&
1249 	    !strcmp(insn->call_dest->name, "__fentry__"))
1250 		return true;
1251 
1252 	return false;
1253 }
1254 
1255 static bool has_modified_stack_frame(struct insn_state *state)
1256 {
1257 	int i;
1258 
1259 	if (state->cfa.base != initial_func_cfi.cfa.base ||
1260 	    state->cfa.offset != initial_func_cfi.cfa.offset ||
1261 	    state->stack_size != initial_func_cfi.cfa.offset ||
1262 	    state->drap)
1263 		return true;
1264 
1265 	for (i = 0; i < CFI_NUM_REGS; i++)
1266 		if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1267 		    state->regs[i].offset != initial_func_cfi.regs[i].offset)
1268 			return true;
1269 
1270 	return false;
1271 }
1272 
1273 static bool has_valid_stack_frame(struct insn_state *state)
1274 {
1275 	if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1276 	    state->regs[CFI_BP].offset == -16)
1277 		return true;
1278 
1279 	if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1280 		return true;
1281 
1282 	return false;
1283 }
1284 
1285 static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1286 {
1287 	struct cfi_reg *cfa = &state->cfa;
1288 	struct stack_op *op = &insn->stack_op;
1289 
1290 	if (cfa->base != CFI_SP)
1291 		return 0;
1292 
1293 	/* push */
1294 	if (op->dest.type == OP_DEST_PUSH)
1295 		cfa->offset += 8;
1296 
1297 	/* pop */
1298 	if (op->src.type == OP_SRC_POP)
1299 		cfa->offset -= 8;
1300 
1301 	/* add immediate to sp */
1302 	if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1303 	    op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1304 		cfa->offset -= op->src.offset;
1305 
1306 	return 0;
1307 }
1308 
1309 static void save_reg(struct insn_state *state, unsigned char reg, int base,
1310 		     int offset)
1311 {
1312 	if (arch_callee_saved_reg(reg) &&
1313 	    state->regs[reg].base == CFI_UNDEFINED) {
1314 		state->regs[reg].base = base;
1315 		state->regs[reg].offset = offset;
1316 	}
1317 }
1318 
1319 static void restore_reg(struct insn_state *state, unsigned char reg)
1320 {
1321 	state->regs[reg].base = CFI_UNDEFINED;
1322 	state->regs[reg].offset = 0;
1323 }
1324 
1325 /*
1326  * A note about DRAP stack alignment:
1327  *
1328  * GCC has the concept of a DRAP register, which is used to help keep track of
1329  * the stack pointer when aligning the stack.  r10 or r13 is used as the DRAP
1330  * register.  The typical DRAP pattern is:
1331  *
1332  *   4c 8d 54 24 08		lea    0x8(%rsp),%r10
1333  *   48 83 e4 c0		and    $0xffffffffffffffc0,%rsp
1334  *   41 ff 72 f8		pushq  -0x8(%r10)
1335  *   55				push   %rbp
1336  *   48 89 e5			mov    %rsp,%rbp
1337  *				(more pushes)
1338  *   41 52			push   %r10
1339  *				...
1340  *   41 5a			pop    %r10
1341  *				(more pops)
1342  *   5d				pop    %rbp
1343  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
1344  *   c3				retq
1345  *
1346  * There are some variations in the epilogues, like:
1347  *
1348  *   5b				pop    %rbx
1349  *   41 5a			pop    %r10
1350  *   41 5c			pop    %r12
1351  *   41 5d			pop    %r13
1352  *   41 5e			pop    %r14
1353  *   c9				leaveq
1354  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
1355  *   c3				retq
1356  *
1357  * and:
1358  *
1359  *   4c 8b 55 e8		mov    -0x18(%rbp),%r10
1360  *   48 8b 5d e0		mov    -0x20(%rbp),%rbx
1361  *   4c 8b 65 f0		mov    -0x10(%rbp),%r12
1362  *   4c 8b 6d f8		mov    -0x8(%rbp),%r13
1363  *   c9				leaveq
1364  *   49 8d 62 f8		lea    -0x8(%r10),%rsp
1365  *   c3				retq
1366  *
1367  * Sometimes r13 is used as the DRAP register, in which case it's saved and
1368  * restored beforehand:
1369  *
1370  *   41 55			push   %r13
1371  *   4c 8d 6c 24 10		lea    0x10(%rsp),%r13
1372  *   48 83 e4 f0		and    $0xfffffffffffffff0,%rsp
1373  *				...
1374  *   49 8d 65 f0		lea    -0x10(%r13),%rsp
1375  *   41 5d			pop    %r13
1376  *   c3				retq
1377  */
1378 static int update_insn_state(struct instruction *insn, struct insn_state *state)
1379 {
1380 	struct stack_op *op = &insn->stack_op;
1381 	struct cfi_reg *cfa = &state->cfa;
1382 	struct cfi_reg *regs = state->regs;
1383 
1384 	/* stack operations don't make sense with an undefined CFA */
1385 	if (cfa->base == CFI_UNDEFINED) {
1386 		if (insn->func) {
1387 			WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1388 			return -1;
1389 		}
1390 		return 0;
1391 	}
1392 
1393 	if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1394 		return update_insn_state_regs(insn, state);
1395 
1396 	switch (op->dest.type) {
1397 
1398 	case OP_DEST_REG:
1399 		switch (op->src.type) {
1400 
1401 		case OP_SRC_REG:
1402 			if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1403 			    cfa->base == CFI_SP &&
1404 			    regs[CFI_BP].base == CFI_CFA &&
1405 			    regs[CFI_BP].offset == -cfa->offset) {
1406 
1407 				/* mov %rsp, %rbp */
1408 				cfa->base = op->dest.reg;
1409 				state->bp_scratch = false;
1410 			}
1411 
1412 			else if (op->src.reg == CFI_SP &&
1413 				 op->dest.reg == CFI_BP && state->drap) {
1414 
1415 				/* drap: mov %rsp, %rbp */
1416 				regs[CFI_BP].base = CFI_BP;
1417 				regs[CFI_BP].offset = -state->stack_size;
1418 				state->bp_scratch = false;
1419 			}
1420 
1421 			else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1422 
1423 				/*
1424 				 * mov %rsp, %reg
1425 				 *
1426 				 * This is needed for the rare case where GCC
1427 				 * does:
1428 				 *
1429 				 *   mov    %rsp, %rax
1430 				 *   ...
1431 				 *   mov    %rax, %rsp
1432 				 */
1433 				state->vals[op->dest.reg].base = CFI_CFA;
1434 				state->vals[op->dest.reg].offset = -state->stack_size;
1435 			}
1436 
1437 			else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1438 				 cfa->base == CFI_BP) {
1439 
1440 				/*
1441 				 * mov %rbp, %rsp
1442 				 *
1443 				 * Restore the original stack pointer (Clang).
1444 				 */
1445 				state->stack_size = -state->regs[CFI_BP].offset;
1446 			}
1447 
1448 			else if (op->dest.reg == cfa->base) {
1449 
1450 				/* mov %reg, %rsp */
1451 				if (cfa->base == CFI_SP &&
1452 				    state->vals[op->src.reg].base == CFI_CFA) {
1453 
1454 					/*
1455 					 * This is needed for the rare case
1456 					 * where GCC does something dumb like:
1457 					 *
1458 					 *   lea    0x8(%rsp), %rcx
1459 					 *   ...
1460 					 *   mov    %rcx, %rsp
1461 					 */
1462 					cfa->offset = -state->vals[op->src.reg].offset;
1463 					state->stack_size = cfa->offset;
1464 
1465 				} else {
1466 					cfa->base = CFI_UNDEFINED;
1467 					cfa->offset = 0;
1468 				}
1469 			}
1470 
1471 			break;
1472 
1473 		case OP_SRC_ADD:
1474 			if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1475 
1476 				/* add imm, %rsp */
1477 				state->stack_size -= op->src.offset;
1478 				if (cfa->base == CFI_SP)
1479 					cfa->offset -= op->src.offset;
1480 				break;
1481 			}
1482 
1483 			if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1484 
1485 				/* lea disp(%rbp), %rsp */
1486 				state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1487 				break;
1488 			}
1489 
1490 			if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1491 
1492 				/* drap: lea disp(%rsp), %drap */
1493 				state->drap_reg = op->dest.reg;
1494 
1495 				/*
1496 				 * lea disp(%rsp), %reg
1497 				 *
1498 				 * This is needed for the rare case where GCC
1499 				 * does something dumb like:
1500 				 *
1501 				 *   lea    0x8(%rsp), %rcx
1502 				 *   ...
1503 				 *   mov    %rcx, %rsp
1504 				 */
1505 				state->vals[op->dest.reg].base = CFI_CFA;
1506 				state->vals[op->dest.reg].offset = \
1507 					-state->stack_size + op->src.offset;
1508 
1509 				break;
1510 			}
1511 
1512 			if (state->drap && op->dest.reg == CFI_SP &&
1513 			    op->src.reg == state->drap_reg) {
1514 
1515 				 /* drap: lea disp(%drap), %rsp */
1516 				cfa->base = CFI_SP;
1517 				cfa->offset = state->stack_size = -op->src.offset;
1518 				state->drap_reg = CFI_UNDEFINED;
1519 				state->drap = false;
1520 				break;
1521 			}
1522 
1523 			if (op->dest.reg == state->cfa.base) {
1524 				WARN_FUNC("unsupported stack register modification",
1525 					  insn->sec, insn->offset);
1526 				return -1;
1527 			}
1528 
1529 			break;
1530 
1531 		case OP_SRC_AND:
1532 			if (op->dest.reg != CFI_SP ||
1533 			    (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1534 			    (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1535 				WARN_FUNC("unsupported stack pointer realignment",
1536 					  insn->sec, insn->offset);
1537 				return -1;
1538 			}
1539 
1540 			if (state->drap_reg != CFI_UNDEFINED) {
1541 				/* drap: and imm, %rsp */
1542 				cfa->base = state->drap_reg;
1543 				cfa->offset = state->stack_size = 0;
1544 				state->drap = true;
1545 			}
1546 
1547 			/*
1548 			 * Older versions of GCC (4.8ish) realign the stack
1549 			 * without DRAP, with a frame pointer.
1550 			 */
1551 
1552 			break;
1553 
1554 		case OP_SRC_POP:
1555 			if (!state->drap && op->dest.type == OP_DEST_REG &&
1556 			    op->dest.reg == cfa->base) {
1557 
1558 				/* pop %rbp */
1559 				cfa->base = CFI_SP;
1560 			}
1561 
1562 			if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1563 			    op->dest.type == OP_DEST_REG &&
1564 			    op->dest.reg == state->drap_reg &&
1565 			    state->drap_offset == -state->stack_size) {
1566 
1567 				/* drap: pop %drap */
1568 				cfa->base = state->drap_reg;
1569 				cfa->offset = 0;
1570 				state->drap_offset = -1;
1571 
1572 			} else if (regs[op->dest.reg].offset == -state->stack_size) {
1573 
1574 				/* pop %reg */
1575 				restore_reg(state, op->dest.reg);
1576 			}
1577 
1578 			state->stack_size -= 8;
1579 			if (cfa->base == CFI_SP)
1580 				cfa->offset -= 8;
1581 
1582 			break;
1583 
1584 		case OP_SRC_REG_INDIRECT:
1585 			if (state->drap && op->src.reg == CFI_BP &&
1586 			    op->src.offset == state->drap_offset) {
1587 
1588 				/* drap: mov disp(%rbp), %drap */
1589 				cfa->base = state->drap_reg;
1590 				cfa->offset = 0;
1591 				state->drap_offset = -1;
1592 			}
1593 
1594 			if (state->drap && op->src.reg == CFI_BP &&
1595 			    op->src.offset == regs[op->dest.reg].offset) {
1596 
1597 				/* drap: mov disp(%rbp), %reg */
1598 				restore_reg(state, op->dest.reg);
1599 
1600 			} else if (op->src.reg == cfa->base &&
1601 			    op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1602 
1603 				/* mov disp(%rbp), %reg */
1604 				/* mov disp(%rsp), %reg */
1605 				restore_reg(state, op->dest.reg);
1606 			}
1607 
1608 			break;
1609 
1610 		default:
1611 			WARN_FUNC("unknown stack-related instruction",
1612 				  insn->sec, insn->offset);
1613 			return -1;
1614 		}
1615 
1616 		break;
1617 
1618 	case OP_DEST_PUSH:
1619 		state->stack_size += 8;
1620 		if (cfa->base == CFI_SP)
1621 			cfa->offset += 8;
1622 
1623 		if (op->src.type != OP_SRC_REG)
1624 			break;
1625 
1626 		if (state->drap) {
1627 			if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1628 
1629 				/* drap: push %drap */
1630 				cfa->base = CFI_BP_INDIRECT;
1631 				cfa->offset = -state->stack_size;
1632 
1633 				/* save drap so we know when to restore it */
1634 				state->drap_offset = -state->stack_size;
1635 
1636 			} else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1637 
1638 				/* drap: push %rbp */
1639 				state->stack_size = 0;
1640 
1641 			} else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1642 
1643 				/* drap: push %reg */
1644 				save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1645 			}
1646 
1647 		} else {
1648 
1649 			/* push %reg */
1650 			save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1651 		}
1652 
1653 		/* detect when asm code uses rbp as a scratch register */
1654 		if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1655 		    cfa->base != CFI_BP)
1656 			state->bp_scratch = true;
1657 		break;
1658 
1659 	case OP_DEST_REG_INDIRECT:
1660 
1661 		if (state->drap) {
1662 			if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1663 
1664 				/* drap: mov %drap, disp(%rbp) */
1665 				cfa->base = CFI_BP_INDIRECT;
1666 				cfa->offset = op->dest.offset;
1667 
1668 				/* save drap offset so we know when to restore it */
1669 				state->drap_offset = op->dest.offset;
1670 			}
1671 
1672 			else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1673 
1674 				/* drap: mov reg, disp(%rbp) */
1675 				save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1676 			}
1677 
1678 		} else if (op->dest.reg == cfa->base) {
1679 
1680 			/* mov reg, disp(%rbp) */
1681 			/* mov reg, disp(%rsp) */
1682 			save_reg(state, op->src.reg, CFI_CFA,
1683 				 op->dest.offset - state->cfa.offset);
1684 		}
1685 
1686 		break;
1687 
1688 	case OP_DEST_LEAVE:
1689 		if ((!state->drap && cfa->base != CFI_BP) ||
1690 		    (state->drap && cfa->base != state->drap_reg)) {
1691 			WARN_FUNC("leave instruction with modified stack frame",
1692 				  insn->sec, insn->offset);
1693 			return -1;
1694 		}
1695 
1696 		/* leave (mov %rbp, %rsp; pop %rbp) */
1697 
1698 		state->stack_size = -state->regs[CFI_BP].offset - 8;
1699 		restore_reg(state, CFI_BP);
1700 
1701 		if (!state->drap) {
1702 			cfa->base = CFI_SP;
1703 			cfa->offset -= 8;
1704 		}
1705 
1706 		break;
1707 
1708 	case OP_DEST_MEM:
1709 		if (op->src.type != OP_SRC_POP) {
1710 			WARN_FUNC("unknown stack-related memory operation",
1711 				  insn->sec, insn->offset);
1712 			return -1;
1713 		}
1714 
1715 		/* pop mem */
1716 		state->stack_size -= 8;
1717 		if (cfa->base == CFI_SP)
1718 			cfa->offset -= 8;
1719 
1720 		break;
1721 
1722 	default:
1723 		WARN_FUNC("unknown stack-related instruction",
1724 			  insn->sec, insn->offset);
1725 		return -1;
1726 	}
1727 
1728 	return 0;
1729 }
1730 
1731 static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1732 {
1733 	struct insn_state *state1 = &insn->state, *state2 = state;
1734 	int i;
1735 
1736 	if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1737 		WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1738 			  insn->sec, insn->offset,
1739 			  state1->cfa.base, state1->cfa.offset,
1740 			  state2->cfa.base, state2->cfa.offset);
1741 
1742 	} else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1743 		for (i = 0; i < CFI_NUM_REGS; i++) {
1744 			if (!memcmp(&state1->regs[i], &state2->regs[i],
1745 				    sizeof(struct cfi_reg)))
1746 				continue;
1747 
1748 			WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1749 				  insn->sec, insn->offset,
1750 				  i, state1->regs[i].base, state1->regs[i].offset,
1751 				  i, state2->regs[i].base, state2->regs[i].offset);
1752 			break;
1753 		}
1754 
1755 	} else if (state1->type != state2->type) {
1756 		WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1757 			  insn->sec, insn->offset, state1->type, state2->type);
1758 
1759 	} else if (state1->drap != state2->drap ||
1760 		 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1761 		 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1762 		WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1763 			  insn->sec, insn->offset,
1764 			  state1->drap, state1->drap_reg, state1->drap_offset,
1765 			  state2->drap, state2->drap_reg, state2->drap_offset);
1766 
1767 	} else
1768 		return true;
1769 
1770 	return false;
1771 }
1772 
1773 /*
1774  * Follow the branch starting at the given instruction, and recursively follow
1775  * any other branches (jumps).  Meanwhile, track the frame pointer state at
1776  * each instruction and validate all the rules described in
1777  * tools/objtool/Documentation/stack-validation.txt.
1778  */
1779 static int validate_branch(struct objtool_file *file, struct instruction *first,
1780 			   struct insn_state state)
1781 {
1782 	struct alternative *alt;
1783 	struct instruction *insn, *next_insn;
1784 	struct section *sec;
1785 	struct symbol *func = NULL;
1786 	int ret;
1787 
1788 	insn = first;
1789 	sec = insn->sec;
1790 
1791 	if (insn->alt_group && list_empty(&insn->alts)) {
1792 		WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1793 			  sec, insn->offset);
1794 		return 1;
1795 	}
1796 
1797 	while (1) {
1798 		next_insn = next_insn_same_sec(file, insn);
1799 
1800 		if (file->c_file && func && insn->func && func != insn->func->pfunc) {
1801 			WARN("%s() falls through to next function %s()",
1802 			     func->name, insn->func->name);
1803 			return 1;
1804 		}
1805 
1806 		func = insn->func ? insn->func->pfunc : NULL;
1807 
1808 		if (func && insn->ignore) {
1809 			WARN_FUNC("BUG: why am I validating an ignored function?",
1810 				  sec, insn->offset);
1811 			return 1;
1812 		}
1813 
1814 		if (insn->visited) {
1815 			if (!insn->hint && !insn_state_match(insn, &state))
1816 				return 1;
1817 
1818 			return 0;
1819 		}
1820 
1821 		if (insn->hint) {
1822 			if (insn->restore) {
1823 				struct instruction *save_insn, *i;
1824 
1825 				i = insn;
1826 				save_insn = NULL;
1827 				func_for_each_insn_continue_reverse(file, insn->func, i) {
1828 					if (i->save) {
1829 						save_insn = i;
1830 						break;
1831 					}
1832 				}
1833 
1834 				if (!save_insn) {
1835 					WARN_FUNC("no corresponding CFI save for CFI restore",
1836 						  sec, insn->offset);
1837 					return 1;
1838 				}
1839 
1840 				if (!save_insn->visited) {
1841 					/*
1842 					 * Oops, no state to copy yet.
1843 					 * Hopefully we can reach this
1844 					 * instruction from another branch
1845 					 * after the save insn has been
1846 					 * visited.
1847 					 */
1848 					if (insn == first)
1849 						return 0;
1850 
1851 					WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1852 						  sec, insn->offset);
1853 					return 1;
1854 				}
1855 
1856 				insn->state = save_insn->state;
1857 			}
1858 
1859 			state = insn->state;
1860 
1861 		} else
1862 			insn->state = state;
1863 
1864 		insn->visited = true;
1865 
1866 		if (!insn->ignore_alts) {
1867 			list_for_each_entry(alt, &insn->alts, list) {
1868 				ret = validate_branch(file, alt->insn, state);
1869 				if (ret)
1870 					return 1;
1871 			}
1872 		}
1873 
1874 		switch (insn->type) {
1875 
1876 		case INSN_RETURN:
1877 			if (func && has_modified_stack_frame(&state)) {
1878 				WARN_FUNC("return with modified stack frame",
1879 					  sec, insn->offset);
1880 				return 1;
1881 			}
1882 
1883 			if (state.bp_scratch) {
1884 				WARN("%s uses BP as a scratch register",
1885 				     insn->func->name);
1886 				return 1;
1887 			}
1888 
1889 			return 0;
1890 
1891 		case INSN_CALL:
1892 			if (is_fentry_call(insn))
1893 				break;
1894 
1895 			ret = dead_end_function(file, insn->call_dest);
1896 			if (ret == 1)
1897 				return 0;
1898 			if (ret == -1)
1899 				return 1;
1900 
1901 			/* fallthrough */
1902 		case INSN_CALL_DYNAMIC:
1903 			if (!no_fp && func && !has_valid_stack_frame(&state)) {
1904 				WARN_FUNC("call without frame pointer save/setup",
1905 					  sec, insn->offset);
1906 				return 1;
1907 			}
1908 			break;
1909 
1910 		case INSN_JUMP_CONDITIONAL:
1911 		case INSN_JUMP_UNCONDITIONAL:
1912 			if (insn->jump_dest &&
1913 			    (!func || !insn->jump_dest->func ||
1914 			     insn->jump_dest->func->pfunc == func)) {
1915 				ret = validate_branch(file, insn->jump_dest,
1916 						      state);
1917 				if (ret)
1918 					return 1;
1919 
1920 			} else if (func && has_modified_stack_frame(&state)) {
1921 				WARN_FUNC("sibling call from callable instruction with modified stack frame",
1922 					  sec, insn->offset);
1923 				return 1;
1924 			}
1925 
1926 			if (insn->type == INSN_JUMP_UNCONDITIONAL)
1927 				return 0;
1928 
1929 			break;
1930 
1931 		case INSN_JUMP_DYNAMIC:
1932 			if (func && list_empty(&insn->alts) &&
1933 			    has_modified_stack_frame(&state)) {
1934 				WARN_FUNC("sibling call from callable instruction with modified stack frame",
1935 					  sec, insn->offset);
1936 				return 1;
1937 			}
1938 
1939 			return 0;
1940 
1941 		case INSN_CONTEXT_SWITCH:
1942 			if (func && (!next_insn || !next_insn->hint)) {
1943 				WARN_FUNC("unsupported instruction in callable function",
1944 					  sec, insn->offset);
1945 				return 1;
1946 			}
1947 			return 0;
1948 
1949 		case INSN_STACK:
1950 			if (update_insn_state(insn, &state))
1951 				return 1;
1952 
1953 			break;
1954 
1955 		default:
1956 			break;
1957 		}
1958 
1959 		if (insn->dead_end)
1960 			return 0;
1961 
1962 		if (!next_insn) {
1963 			if (state.cfa.base == CFI_UNDEFINED)
1964 				return 0;
1965 			WARN("%s: unexpected end of section", sec->name);
1966 			return 1;
1967 		}
1968 
1969 		insn = next_insn;
1970 	}
1971 
1972 	return 0;
1973 }
1974 
1975 static int validate_unwind_hints(struct objtool_file *file)
1976 {
1977 	struct instruction *insn;
1978 	int ret, warnings = 0;
1979 	struct insn_state state;
1980 
1981 	if (!file->hints)
1982 		return 0;
1983 
1984 	clear_insn_state(&state);
1985 
1986 	for_each_insn(file, insn) {
1987 		if (insn->hint && !insn->visited) {
1988 			ret = validate_branch(file, insn, state);
1989 			warnings += ret;
1990 		}
1991 	}
1992 
1993 	return warnings;
1994 }
1995 
1996 static int validate_retpoline(struct objtool_file *file)
1997 {
1998 	struct instruction *insn;
1999 	int warnings = 0;
2000 
2001 	for_each_insn(file, insn) {
2002 		if (insn->type != INSN_JUMP_DYNAMIC &&
2003 		    insn->type != INSN_CALL_DYNAMIC)
2004 			continue;
2005 
2006 		if (insn->retpoline_safe)
2007 			continue;
2008 
2009 		/*
2010 		 * .init.text code is ran before userspace and thus doesn't
2011 		 * strictly need retpolines, except for modules which are
2012 		 * loaded late, they very much do need retpoline in their
2013 		 * .init.text
2014 		 */
2015 		if (!strcmp(insn->sec->name, ".init.text") && !module)
2016 			continue;
2017 
2018 		WARN_FUNC("indirect %s found in RETPOLINE build",
2019 			  insn->sec, insn->offset,
2020 			  insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2021 
2022 		warnings++;
2023 	}
2024 
2025 	return warnings;
2026 }
2027 
2028 static bool is_kasan_insn(struct instruction *insn)
2029 {
2030 	return (insn->type == INSN_CALL &&
2031 		!strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2032 }
2033 
2034 static bool is_ubsan_insn(struct instruction *insn)
2035 {
2036 	return (insn->type == INSN_CALL &&
2037 		!strcmp(insn->call_dest->name,
2038 			"__ubsan_handle_builtin_unreachable"));
2039 }
2040 
2041 static bool ignore_unreachable_insn(struct instruction *insn)
2042 {
2043 	int i;
2044 
2045 	if (insn->ignore || insn->type == INSN_NOP)
2046 		return true;
2047 
2048 	/*
2049 	 * Ignore any unused exceptions.  This can happen when a whitelisted
2050 	 * function has an exception table entry.
2051 	 *
2052 	 * Also ignore alternative replacement instructions.  This can happen
2053 	 * when a whitelisted function uses one of the ALTERNATIVE macros.
2054 	 */
2055 	if (!strcmp(insn->sec->name, ".fixup") ||
2056 	    !strcmp(insn->sec->name, ".altinstr_replacement") ||
2057 	    !strcmp(insn->sec->name, ".altinstr_aux"))
2058 		return true;
2059 
2060 	/*
2061 	 * Check if this (or a subsequent) instruction is related to
2062 	 * CONFIG_UBSAN or CONFIG_KASAN.
2063 	 *
2064 	 * End the search at 5 instructions to avoid going into the weeds.
2065 	 */
2066 	if (!insn->func)
2067 		return false;
2068 	for (i = 0; i < 5; i++) {
2069 
2070 		if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2071 			return true;
2072 
2073 		if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2074 			if (insn->jump_dest &&
2075 			    insn->jump_dest->func == insn->func) {
2076 				insn = insn->jump_dest;
2077 				continue;
2078 			}
2079 
2080 			break;
2081 		}
2082 
2083 		if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2084 			break;
2085 
2086 		insn = list_next_entry(insn, list);
2087 	}
2088 
2089 	return false;
2090 }
2091 
2092 static int validate_functions(struct objtool_file *file)
2093 {
2094 	struct section *sec;
2095 	struct symbol *func;
2096 	struct instruction *insn;
2097 	struct insn_state state;
2098 	int ret, warnings = 0;
2099 
2100 	clear_insn_state(&state);
2101 
2102 	state.cfa = initial_func_cfi.cfa;
2103 	memcpy(&state.regs, &initial_func_cfi.regs,
2104 	       CFI_NUM_REGS * sizeof(struct cfi_reg));
2105 	state.stack_size = initial_func_cfi.cfa.offset;
2106 
2107 	for_each_sec(file, sec) {
2108 		list_for_each_entry(func, &sec->symbol_list, list) {
2109 			if (func->type != STT_FUNC || func->pfunc != func)
2110 				continue;
2111 
2112 			insn = find_insn(file, sec, func->offset);
2113 			if (!insn || insn->ignore)
2114 				continue;
2115 
2116 			ret = validate_branch(file, insn, state);
2117 			warnings += ret;
2118 		}
2119 	}
2120 
2121 	return warnings;
2122 }
2123 
2124 static int validate_reachable_instructions(struct objtool_file *file)
2125 {
2126 	struct instruction *insn;
2127 
2128 	if (file->ignore_unreachables)
2129 		return 0;
2130 
2131 	for_each_insn(file, insn) {
2132 		if (insn->visited || ignore_unreachable_insn(insn))
2133 			continue;
2134 
2135 		WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2136 		return 1;
2137 	}
2138 
2139 	return 0;
2140 }
2141 
2142 static void cleanup(struct objtool_file *file)
2143 {
2144 	struct instruction *insn, *tmpinsn;
2145 	struct alternative *alt, *tmpalt;
2146 
2147 	list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2148 		list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2149 			list_del(&alt->list);
2150 			free(alt);
2151 		}
2152 		list_del(&insn->list);
2153 		hash_del(&insn->hash);
2154 		free(insn);
2155 	}
2156 	elf_close(file->elf);
2157 }
2158 
2159 int check(const char *_objname, bool orc)
2160 {
2161 	struct objtool_file file;
2162 	int ret, warnings = 0;
2163 
2164 	objname = _objname;
2165 
2166 	file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
2167 	if (!file.elf)
2168 		return 1;
2169 
2170 	INIT_LIST_HEAD(&file.insn_list);
2171 	hash_init(file.insn_hash);
2172 	file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
2173 	file.rodata = find_section_by_name(file.elf, ".rodata");
2174 	file.c_file = find_section_by_name(file.elf, ".comment");
2175 	file.ignore_unreachables = no_unreachable;
2176 	file.hints = false;
2177 
2178 	arch_initial_func_cfi_state(&initial_func_cfi);
2179 
2180 	ret = decode_sections(&file);
2181 	if (ret < 0)
2182 		goto out;
2183 	warnings += ret;
2184 
2185 	if (list_empty(&file.insn_list))
2186 		goto out;
2187 
2188 	if (retpoline) {
2189 		ret = validate_retpoline(&file);
2190 		if (ret < 0)
2191 			return ret;
2192 		warnings += ret;
2193 	}
2194 
2195 	ret = validate_functions(&file);
2196 	if (ret < 0)
2197 		goto out;
2198 	warnings += ret;
2199 
2200 	ret = validate_unwind_hints(&file);
2201 	if (ret < 0)
2202 		goto out;
2203 	warnings += ret;
2204 
2205 	if (!warnings) {
2206 		ret = validate_reachable_instructions(&file);
2207 		if (ret < 0)
2208 			goto out;
2209 		warnings += ret;
2210 	}
2211 
2212 	if (orc) {
2213 		ret = create_orc(&file);
2214 		if (ret < 0)
2215 			goto out;
2216 
2217 		ret = create_orc_sections(&file);
2218 		if (ret < 0)
2219 			goto out;
2220 
2221 		ret = elf_write(file.elf);
2222 		if (ret < 0)
2223 			goto out;
2224 	}
2225 
2226 out:
2227 	cleanup(&file);
2228 
2229 	/* ignore warnings for now until we get all the code cleaned up */
2230 	if (ret || warnings)
2231 		return 0;
2232 	return 0;
2233 }
2234