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