blob: 5e5db7b4d77bb92172450340dfb103c94b10962e [file] [log] [blame]
Josh Poimboeufdcc914f2017-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
Peter Zijlstra43a45252018-01-16 17:16:32 +010021#include "builtin.h"
Josh Poimboeufdcc914f2017-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>
29#include <linux/kernel.h>
30
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050031struct alternative {
32 struct list_head list;
33 struct instruction *insn;
34};
35
36const char *objname;
Josh Poimboeufbaa41462017-06-28 10:11:07 -050037struct cfi_state initial_func_cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050038
Josh Poimboeuf627fce12017-07-11 10:33:42 -050039struct instruction *find_insn(struct objtool_file *file,
40 struct section *sec, unsigned long offset)
Josh Poimboeufdcc914f2017-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
Josh Poimboeufbaa41462017-06-28 10:11:07 -050056 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050057 return NULL;
58
59 return next;
60}
61
Josh Poimboeufdcc914f2017-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
Josh Poimboeufbaa41462017-06-28 10:11:07 -050078#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 Poimboeufdcc914f2017-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 Poimboeufdcc914f2017-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 Poimboeufdcc914f2017-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",
139 "lbug_with_loc",
140 "fortify_panic",
Kees Cookb394d462018-01-10 14:22:38 -0800141 "usercopy_abort",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500142 };
143
144 if (func->bind == STB_WEAK)
145 return 0;
146
147 if (func->bind == STB_GLOBAL)
148 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
149 if (!strcmp(func->name, global_noreturns[i]))
150 return 1;
151
152 if (!func->sec)
153 return 0;
154
155 func_for_each_insn(file, func, insn) {
156 empty = false;
157
158 if (insn->type == INSN_RETURN)
159 return 0;
160 }
161
162 if (empty)
163 return 0;
164
165 /*
166 * A function can have a sibling call instead of a return. In that
167 * case, the function's dead-end status depends on whether the target
168 * of the sibling call returns.
169 */
170 func_for_each_insn(file, func, insn) {
171 if (insn->sec != func->sec ||
172 insn->offset >= func->offset + func->len)
173 break;
174
175 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
176 struct instruction *dest = insn->jump_dest;
177 struct symbol *dest_func;
178
179 if (!dest)
180 /* sibling call to another file */
181 return 0;
182
183 if (dest->sec != func->sec ||
184 dest->offset < func->offset ||
185 dest->offset >= func->offset + func->len) {
186 /* local sibling call */
187 dest_func = find_symbol_by_offset(dest->sec,
188 dest->offset);
189 if (!dest_func)
190 continue;
191
192 if (recursion == 5) {
193 WARN_FUNC("infinite recursion (objtool bug!)",
194 dest->sec, dest->offset);
195 return -1;
196 }
197
198 return __dead_end_function(file, dest_func,
199 recursion + 1);
200 }
201 }
202
203 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
204 /* sibling call */
205 return 0;
206 }
207
208 return 1;
209}
210
211static int dead_end_function(struct objtool_file *file, struct symbol *func)
212{
213 return __dead_end_function(file, func, 0);
214}
215
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500216static void clear_insn_state(struct insn_state *state)
217{
218 int i;
219
220 memset(state, 0, sizeof(*state));
221 state->cfa.base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500222 for (i = 0; i < CFI_NUM_REGS; i++) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500223 state->regs[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500224 state->vals[i].base = CFI_UNDEFINED;
225 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500226 state->drap_reg = CFI_UNDEFINED;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -0500227 state->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500228}
229
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500230/*
231 * Call the arch-specific instruction decoder for all the instructions and add
232 * them to the global instruction list.
233 */
234static int decode_instructions(struct objtool_file *file)
235{
236 struct section *sec;
237 struct symbol *func;
238 unsigned long offset;
239 struct instruction *insn;
240 int ret;
241
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500242 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500243
244 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
245 continue;
246
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500247 if (strcmp(sec->name, ".altinstr_replacement") &&
248 strcmp(sec->name, ".altinstr_aux") &&
249 strncmp(sec->name, ".discard.", 9))
250 sec->text = true;
251
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500252 for (offset = 0; offset < sec->len; offset += insn->len) {
253 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500254 if (!insn) {
255 WARN("malloc failed");
256 return -1;
257 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500258 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500259 INIT_LIST_HEAD(&insn->alts);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500260 clear_insn_state(&insn->state);
261
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500262 insn->sec = sec;
263 insn->offset = offset;
264
265 ret = arch_decode_instruction(file->elf, sec, offset,
266 sec->len - offset,
267 &insn->len, &insn->type,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500268 &insn->immediate,
269 &insn->stack_op);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500270 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500271 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500272
273 if (!insn->type || insn->type > INSN_LAST) {
274 WARN_FUNC("invalid instruction type %d",
275 insn->sec, insn->offset, insn->type);
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500276 ret = -1;
277 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500278 }
279
280 hash_add(file->insn_hash, &insn->hash, insn->offset);
281 list_add_tail(&insn->list, &file->insn_list);
282 }
283
284 list_for_each_entry(func, &sec->symbol_list, list) {
285 if (func->type != STT_FUNC)
286 continue;
287
288 if (!find_insn(file, sec, func->offset)) {
289 WARN("%s(): can't find starting instruction",
290 func->name);
291 return -1;
292 }
293
294 func_for_each_insn(file, func, insn)
295 if (!insn->func)
296 insn->func = func;
297 }
298 }
299
300 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500301
302err:
303 free(insn);
304 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500305}
306
307/*
Josh Poimboeuf649ea4d52017-07-27 15:56:53 -0500308 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500309 */
310static int add_dead_ends(struct objtool_file *file)
311{
312 struct section *sec;
313 struct rela *rela;
314 struct instruction *insn;
315 bool found;
316
Josh Poimboeuf649ea4d52017-07-27 15:56:53 -0500317 /*
318 * By default, "ud2" is a dead end unless otherwise annotated, because
319 * GCC 7 inserts it for certain divide-by-zero cases.
320 */
321 for_each_insn(file, insn)
322 if (insn->type == INSN_BUG)
323 insn->dead_end = true;
324
325 /*
326 * Check for manually annotated dead ends.
327 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500328 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
329 if (!sec)
Josh Poimboeuf649ea4d52017-07-27 15:56:53 -0500330 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500331
332 list_for_each_entry(rela, &sec->rela_list, list) {
333 if (rela->sym->type != STT_SECTION) {
334 WARN("unexpected relocation symbol type in %s", sec->name);
335 return -1;
336 }
337 insn = find_insn(file, rela->sym->sec, rela->addend);
338 if (insn)
339 insn = list_prev_entry(insn, list);
340 else if (rela->addend == rela->sym->sec->len) {
341 found = false;
342 list_for_each_entry_reverse(insn, &file->insn_list, list) {
343 if (insn->sec == rela->sym->sec) {
344 found = true;
345 break;
346 }
347 }
348
349 if (!found) {
350 WARN("can't find unreachable insn at %s+0x%x",
351 rela->sym->sec->name, rela->addend);
352 return -1;
353 }
354 } else {
355 WARN("can't find unreachable insn at %s+0x%x",
356 rela->sym->sec->name, rela->addend);
357 return -1;
358 }
359
360 insn->dead_end = true;
361 }
362
Josh Poimboeuf649ea4d52017-07-27 15:56:53 -0500363reachable:
364 /*
365 * These manually annotated reachable checks are needed for GCC 4.4,
366 * where the Linux unreachable() macro isn't supported. In that case
367 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
368 * not a dead end.
369 */
370 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
371 if (!sec)
372 return 0;
373
374 list_for_each_entry(rela, &sec->rela_list, list) {
375 if (rela->sym->type != STT_SECTION) {
376 WARN("unexpected relocation symbol type in %s", sec->name);
377 return -1;
378 }
379 insn = find_insn(file, rela->sym->sec, rela->addend);
380 if (insn)
381 insn = list_prev_entry(insn, list);
382 else if (rela->addend == rela->sym->sec->len) {
383 found = false;
384 list_for_each_entry_reverse(insn, &file->insn_list, list) {
385 if (insn->sec == rela->sym->sec) {
386 found = true;
387 break;
388 }
389 }
390
391 if (!found) {
392 WARN("can't find reachable insn at %s+0x%x",
393 rela->sym->sec->name, rela->addend);
394 return -1;
395 }
396 } else {
397 WARN("can't find reachable insn at %s+0x%x",
398 rela->sym->sec->name, rela->addend);
399 return -1;
400 }
401
402 insn->dead_end = false;
403 }
404
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500405 return 0;
406}
407
408/*
409 * Warnings shouldn't be reported for ignored functions.
410 */
411static void add_ignores(struct objtool_file *file)
412{
413 struct instruction *insn;
414 struct section *sec;
415 struct symbol *func;
416
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500417 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500418 list_for_each_entry(func, &sec->symbol_list, list) {
419 if (func->type != STT_FUNC)
420 continue;
421
422 if (!ignore_func(file, func))
423 continue;
424
425 func_for_each_insn(file, func, insn)
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500426 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500427 }
428 }
429}
430
431/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000432 * FIXME: For now, just ignore any alternatives which add retpolines. This is
433 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
434 * But it at least allows objtool to understand the control flow *around* the
435 * retpoline.
436 */
437static int add_nospec_ignores(struct objtool_file *file)
438{
439 struct section *sec;
440 struct rela *rela;
441 struct instruction *insn;
442
443 sec = find_section_by_name(file->elf, ".rela.discard.nospec");
444 if (!sec)
445 return 0;
446
447 list_for_each_entry(rela, &sec->rela_list, list) {
448 if (rela->sym->type != STT_SECTION) {
449 WARN("unexpected relocation symbol type in %s", sec->name);
450 return -1;
451 }
452
453 insn = find_insn(file, rela->sym->sec, rela->addend);
454 if (!insn) {
455 WARN("bad .discard.nospec entry");
456 return -1;
457 }
458
459 insn->ignore_alts = true;
460 }
461
462 return 0;
463}
464
465/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500466 * Find the destination instructions for all jumps.
467 */
468static int add_jump_destinations(struct objtool_file *file)
469{
470 struct instruction *insn;
471 struct rela *rela;
472 struct section *dest_sec;
473 unsigned long dest_off;
474
475 for_each_insn(file, insn) {
476 if (insn->type != INSN_JUMP_CONDITIONAL &&
477 insn->type != INSN_JUMP_UNCONDITIONAL)
478 continue;
479
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500480 if (insn->ignore)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500481 continue;
482
483 rela = find_rela_by_dest_range(insn->sec, insn->offset,
484 insn->len);
485 if (!rela) {
486 dest_sec = insn->sec;
487 dest_off = insn->offset + insn->len + insn->immediate;
488 } else if (rela->sym->type == STT_SECTION) {
489 dest_sec = rela->sym->sec;
490 dest_off = rela->addend + 4;
491 } else if (rela->sym->sec->idx) {
492 dest_sec = rela->sym->sec;
493 dest_off = rela->sym->sym.st_value + rela->addend + 4;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000494 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
495 /*
496 * Retpoline jumps are really dynamic jumps in
497 * disguise, so convert them accordingly.
498 */
499 insn->type = INSN_JUMP_DYNAMIC;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100500 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000501 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500502 } else {
503 /* sibling call */
504 insn->jump_dest = 0;
505 continue;
506 }
507
508 insn->jump_dest = find_insn(file, dest_sec, dest_off);
509 if (!insn->jump_dest) {
510
511 /*
512 * This is a special case where an alt instruction
513 * jumps past the end of the section. These are
514 * handled later in handle_group_alt().
515 */
516 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
517 continue;
518
519 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
520 insn->sec, insn->offset, dest_sec->name,
521 dest_off);
522 return -1;
523 }
524 }
525
526 return 0;
527}
528
529/*
530 * Find the destination instructions for all calls.
531 */
532static int add_call_destinations(struct objtool_file *file)
533{
534 struct instruction *insn;
535 unsigned long dest_off;
536 struct rela *rela;
537
538 for_each_insn(file, insn) {
539 if (insn->type != INSN_CALL)
540 continue;
541
542 rela = find_rela_by_dest_range(insn->sec, insn->offset,
543 insn->len);
544 if (!rela) {
545 dest_off = insn->offset + insn->len + insn->immediate;
546 insn->call_dest = find_symbol_by_offset(insn->sec,
547 dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600548
549 if (!insn->call_dest && !insn->ignore) {
550 WARN_FUNC("unsupported intra-function call",
551 insn->sec, insn->offset);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100552 if (retpoline)
553 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500554 return -1;
555 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600556
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500557 } else if (rela->sym->type == STT_SECTION) {
558 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
559 rela->addend+4);
560 if (!insn->call_dest ||
561 insn->call_dest->type != STT_FUNC) {
562 WARN_FUNC("can't find call dest symbol at %s+0x%x",
563 insn->sec, insn->offset,
564 rela->sym->sec->name,
565 rela->addend + 4);
566 return -1;
567 }
568 } else
569 insn->call_dest = rela->sym;
570 }
571
572 return 0;
573}
574
575/*
576 * The .alternatives section requires some extra special care, over and above
577 * what other special sections require:
578 *
579 * 1. Because alternatives are patched in-place, we need to insert a fake jump
580 * instruction at the end so that validate_branch() skips all the original
581 * replaced instructions when validating the new instruction path.
582 *
583 * 2. An added wrinkle is that the new instruction length might be zero. In
584 * that case the old instructions are replaced with noops. We simulate that
585 * by creating a fake jump as the only new instruction.
586 *
587 * 3. In some cases, the alternative section includes an instruction which
588 * conditionally jumps to the _end_ of the entry. We have to modify these
589 * jumps' destinations to point back to .text rather than the end of the
590 * entry in .altinstr_replacement.
591 *
592 * 4. It has been requested that we don't validate the !POPCNT feature path
593 * which is a "very very small percentage of machines".
594 */
595static int handle_group_alt(struct objtool_file *file,
596 struct special_alt *special_alt,
597 struct instruction *orig_insn,
598 struct instruction **new_insn)
599{
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600600 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500601 unsigned long dest_off;
602
603 last_orig_insn = NULL;
604 insn = orig_insn;
605 sec_for_each_insn_from(file, insn) {
606 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
607 break;
608
609 if (special_alt->skip_orig)
610 insn->type = INSN_NOP;
611
612 insn->alt_group = true;
613 last_orig_insn = insn;
614 }
615
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600616 if (next_insn_same_sec(file, last_orig_insn)) {
617 fake_jump = malloc(sizeof(*fake_jump));
618 if (!fake_jump) {
619 WARN("malloc failed");
620 return -1;
621 }
622 memset(fake_jump, 0, sizeof(*fake_jump));
623 INIT_LIST_HEAD(&fake_jump->alts);
624 clear_insn_state(&fake_jump->state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500625
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600626 fake_jump->sec = special_alt->new_sec;
627 fake_jump->offset = -1;
628 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
629 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
630 fake_jump->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500631 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500632
633 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600634 if (!fake_jump) {
635 WARN("%s: empty alternative at end of section",
636 special_alt->orig_sec->name);
637 return -1;
638 }
639
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500640 *new_insn = fake_jump;
641 return 0;
642 }
643
644 last_new_insn = NULL;
645 insn = *new_insn;
646 sec_for_each_insn_from(file, insn) {
647 if (insn->offset >= special_alt->new_off + special_alt->new_len)
648 break;
649
650 last_new_insn = insn;
651
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600652 insn->ignore = orig_insn->ignore_alts;
653
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500654 if (insn->type != INSN_JUMP_CONDITIONAL &&
655 insn->type != INSN_JUMP_UNCONDITIONAL)
656 continue;
657
658 if (!insn->immediate)
659 continue;
660
661 dest_off = insn->offset + insn->len + insn->immediate;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600662 if (dest_off == special_alt->new_off + special_alt->new_len) {
663 if (!fake_jump) {
664 WARN("%s: alternative jump to end of section",
665 special_alt->orig_sec->name);
666 return -1;
667 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500668 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600669 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500670
671 if (!insn->jump_dest) {
672 WARN_FUNC("can't find alternative jump destination",
673 insn->sec, insn->offset);
674 return -1;
675 }
676 }
677
678 if (!last_new_insn) {
679 WARN_FUNC("can't find last new alternative instruction",
680 special_alt->new_sec, special_alt->new_off);
681 return -1;
682 }
683
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600684 if (fake_jump)
685 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500686
687 return 0;
688}
689
690/*
691 * A jump table entry can either convert a nop to a jump or a jump to a nop.
692 * If the original instruction is a jump, make the alt entry an effective nop
693 * by just skipping the original instruction.
694 */
695static int handle_jump_alt(struct objtool_file *file,
696 struct special_alt *special_alt,
697 struct instruction *orig_insn,
698 struct instruction **new_insn)
699{
700 if (orig_insn->type == INSN_NOP)
701 return 0;
702
703 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
704 WARN_FUNC("unsupported instruction at jump label",
705 orig_insn->sec, orig_insn->offset);
706 return -1;
707 }
708
709 *new_insn = list_next_entry(orig_insn, list);
710 return 0;
711}
712
713/*
714 * Read all the special sections which have alternate instructions which can be
715 * patched in or redirected to at runtime. Each instruction having alternate
716 * instruction(s) has them added to its insn->alts list, which will be
717 * traversed in validate_branch().
718 */
719static int add_special_section_alts(struct objtool_file *file)
720{
721 struct list_head special_alts;
722 struct instruction *orig_insn, *new_insn;
723 struct special_alt *special_alt, *tmp;
724 struct alternative *alt;
725 int ret;
726
727 ret = special_get_alts(file->elf, &special_alts);
728 if (ret)
729 return ret;
730
731 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500732
733 orig_insn = find_insn(file, special_alt->orig_sec,
734 special_alt->orig_off);
735 if (!orig_insn) {
736 WARN_FUNC("special: can't find orig instruction",
737 special_alt->orig_sec, special_alt->orig_off);
738 ret = -1;
739 goto out;
740 }
741
742 new_insn = NULL;
743 if (!special_alt->group || special_alt->new_len) {
744 new_insn = find_insn(file, special_alt->new_sec,
745 special_alt->new_off);
746 if (!new_insn) {
747 WARN_FUNC("special: can't find new instruction",
748 special_alt->new_sec,
749 special_alt->new_off);
750 ret = -1;
751 goto out;
752 }
753 }
754
755 if (special_alt->group) {
756 ret = handle_group_alt(file, special_alt, orig_insn,
757 &new_insn);
758 if (ret)
759 goto out;
760 } else if (special_alt->jump_or_nop) {
761 ret = handle_jump_alt(file, special_alt, orig_insn,
762 &new_insn);
763 if (ret)
764 goto out;
765 }
766
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000767 alt = malloc(sizeof(*alt));
768 if (!alt) {
769 WARN("malloc failed");
770 ret = -1;
771 goto out;
772 }
773
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500774 alt->insn = new_insn;
775 list_add_tail(&alt->list, &orig_insn->alts);
776
777 list_del(&special_alt->list);
778 free(special_alt);
779 }
780
781out:
782 return ret;
783}
784
785static int add_switch_table(struct objtool_file *file, struct symbol *func,
786 struct instruction *insn, struct rela *table,
787 struct rela *next_table)
788{
789 struct rela *rela = table;
790 struct instruction *alt_insn;
791 struct alternative *alt;
792
793 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
794 if (rela == next_table)
795 break;
796
797 if (rela->sym->sec != insn->sec ||
798 rela->addend <= func->offset ||
799 rela->addend >= func->offset + func->len)
800 break;
801
802 alt_insn = find_insn(file, insn->sec, rela->addend);
803 if (!alt_insn) {
804 WARN("%s: can't find instruction at %s+0x%x",
805 file->rodata->rela->name, insn->sec->name,
806 rela->addend);
807 return -1;
808 }
809
810 alt = malloc(sizeof(*alt));
811 if (!alt) {
812 WARN("malloc failed");
813 return -1;
814 }
815
816 alt->insn = alt_insn;
817 list_add_tail(&alt->list, &insn->alts);
818 }
819
820 return 0;
821}
822
823/*
824 * find_switch_table() - Given a dynamic jump, find the switch jump table in
825 * .rodata associated with it.
826 *
827 * There are 3 basic patterns:
828 *
829 * 1. jmpq *[rodata addr](,%reg,8)
830 *
831 * This is the most common case by far. It jumps to an address in a simple
832 * jump table which is stored in .rodata.
833 *
834 * 2. jmpq *[rodata addr](%rip)
835 *
836 * This is caused by a rare GCC quirk, currently only seen in three driver
837 * functions in the kernel, only with certain obscure non-distro configs.
838 *
839 * As part of an optimization, GCC makes a copy of an existing switch jump
840 * table, modifies it, and then hard-codes the jump (albeit with an indirect
841 * jump) to use a single entry in the table. The rest of the jump table and
842 * some of its jump targets remain as dead code.
843 *
844 * In such a case we can just crudely ignore all unreachable instruction
845 * warnings for the entire object file. Ideally we would just ignore them
846 * for the function, but that would require redesigning the code quite a
847 * bit. And honestly that's just not worth doing: unreachable instruction
848 * warnings are of questionable value anyway, and this is such a rare issue.
849 *
850 * 3. mov [rodata addr],%reg1
851 * ... some instructions ...
852 * jmpq *(%reg1,%reg2,8)
853 *
854 * This is a fairly uncommon pattern which is new for GCC 6. As of this
855 * writing, there are 11 occurrences of it in the allmodconfig kernel.
856 *
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100857 * As of GCC 7 there are quite a few more of these and the 'in between' code
858 * is significant. Esp. with KASAN enabled some of the code between the mov
859 * and jmpq uses .rodata itself, which can confuse things.
860 *
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500861 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
862 * ensure the same register is used in the mov and jump instructions.
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100863 *
864 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500865 */
866static struct rela *find_switch_table(struct objtool_file *file,
867 struct symbol *func,
868 struct instruction *insn)
869{
870 struct rela *text_rela, *rodata_rela;
871 struct instruction *orig_insn = insn;
872
873 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
874 if (text_rela && text_rela->sym == file->rodata->sym) {
875 /* case 1 */
876 rodata_rela = find_rela_by_dest(file->rodata,
877 text_rela->addend);
878 if (rodata_rela)
879 return rodata_rela;
880
881 /* case 2 */
882 rodata_rela = find_rela_by_dest(file->rodata,
883 text_rela->addend + 4);
884 if (!rodata_rela)
885 return NULL;
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100886
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500887 file->ignore_unreachables = true;
888 return rodata_rela;
889 }
890
891 /* case 3 */
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100892 /*
893 * Backward search using the @first_jump_src links, these help avoid
894 * much of the 'in between' code. Which avoids us getting confused by
895 * it.
896 */
897 for (insn = list_prev_entry(insn, list);
898
899 &insn->list != &file->insn_list &&
900 insn->sec == func->sec &&
901 insn->offset >= func->offset;
902
903 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
904
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500905 if (insn->type == INSN_JUMP_DYNAMIC)
906 break;
907
908 /* allow small jumps within the range */
909 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
910 insn->jump_dest &&
911 (insn->jump_dest->offset <= insn->offset ||
912 insn->jump_dest->offset > orig_insn->offset))
913 break;
914
915 /* look for a relocation which references .rodata */
916 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
917 insn->len);
918 if (!text_rela || text_rela->sym != file->rodata->sym)
919 continue;
920
921 /*
922 * Make sure the .rodata address isn't associated with a
923 * symbol. gcc jump tables are anonymous data.
924 */
925 if (find_symbol_containing(file->rodata, text_rela->addend))
926 continue;
927
928 return find_rela_by_dest(file->rodata, text_rela->addend);
929 }
930
931 return NULL;
932}
933
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100934
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500935static int add_func_switch_tables(struct objtool_file *file,
936 struct symbol *func)
937{
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100938 struct instruction *insn, *last = NULL, *prev_jump = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500939 struct rela *rela, *prev_rela = NULL;
940 int ret;
941
942 func_for_each_insn(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100943 if (!last)
944 last = insn;
945
946 /*
947 * Store back-pointers for unconditional forward jumps such
948 * that find_switch_table() can back-track using those and
949 * avoid some potentially confusing code.
950 */
951 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
952 insn->offset > last->offset &&
953 insn->jump_dest->offset > insn->offset &&
954 !insn->jump_dest->first_jump_src) {
955
956 insn->jump_dest->first_jump_src = insn;
957 last = insn->jump_dest;
958 }
959
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500960 if (insn->type != INSN_JUMP_DYNAMIC)
961 continue;
962
963 rela = find_switch_table(file, func, insn);
964 if (!rela)
965 continue;
966
967 /*
968 * We found a switch table, but we don't know yet how big it
969 * is. Don't add it until we reach the end of the function or
970 * the beginning of another switch table in the same function.
971 */
972 if (prev_jump) {
973 ret = add_switch_table(file, func, prev_jump, prev_rela,
974 rela);
975 if (ret)
976 return ret;
977 }
978
979 prev_jump = insn;
980 prev_rela = rela;
981 }
982
983 if (prev_jump) {
984 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
985 if (ret)
986 return ret;
987 }
988
989 return 0;
990}
991
992/*
993 * For some switch statements, gcc generates a jump table in the .rodata
994 * section which contains a list of addresses within the function to jump to.
995 * This finds these jump tables and adds them to the insn->alts lists.
996 */
997static int add_switch_table_alts(struct objtool_file *file)
998{
999 struct section *sec;
1000 struct symbol *func;
1001 int ret;
1002
1003 if (!file->rodata || !file->rodata->rela)
1004 return 0;
1005
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001006 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001007 list_for_each_entry(func, &sec->symbol_list, list) {
1008 if (func->type != STT_FUNC)
1009 continue;
1010
1011 ret = add_func_switch_tables(file, func);
1012 if (ret)
1013 return ret;
1014 }
1015 }
1016
1017 return 0;
1018}
1019
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001020static int read_unwind_hints(struct objtool_file *file)
1021{
1022 struct section *sec, *relasec;
1023 struct rela *rela;
1024 struct unwind_hint *hint;
1025 struct instruction *insn;
1026 struct cfi_reg *cfa;
1027 int i;
1028
1029 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1030 if (!sec)
1031 return 0;
1032
1033 relasec = sec->rela;
1034 if (!relasec) {
1035 WARN("missing .rela.discard.unwind_hints section");
1036 return -1;
1037 }
1038
1039 if (sec->len % sizeof(struct unwind_hint)) {
1040 WARN("struct unwind_hint size mismatch");
1041 return -1;
1042 }
1043
1044 file->hints = true;
1045
1046 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1047 hint = (struct unwind_hint *)sec->data->d_buf + i;
1048
1049 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1050 if (!rela) {
1051 WARN("can't find rela for unwind_hints[%d]", i);
1052 return -1;
1053 }
1054
1055 insn = find_insn(file, rela->sym->sec, rela->addend);
1056 if (!insn) {
1057 WARN("can't find insn for unwind_hints[%d]", i);
1058 return -1;
1059 }
1060
1061 cfa = &insn->state.cfa;
1062
1063 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1064 insn->save = true;
1065 continue;
1066
1067 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1068 insn->restore = true;
1069 insn->hint = true;
1070 continue;
1071 }
1072
1073 insn->hint = true;
1074
1075 switch (hint->sp_reg) {
1076 case ORC_REG_UNDEFINED:
1077 cfa->base = CFI_UNDEFINED;
1078 break;
1079 case ORC_REG_SP:
1080 cfa->base = CFI_SP;
1081 break;
1082 case ORC_REG_BP:
1083 cfa->base = CFI_BP;
1084 break;
1085 case ORC_REG_SP_INDIRECT:
1086 cfa->base = CFI_SP_INDIRECT;
1087 break;
1088 case ORC_REG_R10:
1089 cfa->base = CFI_R10;
1090 break;
1091 case ORC_REG_R13:
1092 cfa->base = CFI_R13;
1093 break;
1094 case ORC_REG_DI:
1095 cfa->base = CFI_DI;
1096 break;
1097 case ORC_REG_DX:
1098 cfa->base = CFI_DX;
1099 break;
1100 default:
1101 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1102 insn->sec, insn->offset, hint->sp_reg);
1103 return -1;
1104 }
1105
1106 cfa->offset = hint->sp_offset;
1107 insn->state.type = hint->type;
1108 }
1109
1110 return 0;
1111}
1112
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001113static int read_retpoline_hints(struct objtool_file *file)
1114{
1115 struct section *sec, *relasec;
1116 struct instruction *insn;
1117 struct rela *rela;
1118 int i;
1119
1120 sec = find_section_by_name(file->elf, ".discard.retpoline_safe");
1121 if (!sec)
1122 return 0;
1123
1124 relasec = sec->rela;
1125 if (!relasec) {
1126 WARN("missing .rela.discard.retpoline_safe section");
1127 return -1;
1128 }
1129
1130 if (sec->len % sizeof(unsigned long)) {
1131 WARN("retpoline_safe size mismatch: %d %ld", sec->len, sizeof(unsigned long));
1132 return -1;
1133 }
1134
1135 for (i = 0; i < sec->len / sizeof(unsigned long); i++) {
1136 rela = find_rela_by_dest(sec, i * sizeof(unsigned long));
1137 if (!rela) {
1138 WARN("can't find rela for retpoline_safe[%d]", i);
1139 return -1;
1140 }
1141
1142 insn = find_insn(file, rela->sym->sec, rela->addend);
1143 if (!insn) {
1144 WARN("can't find insn for retpoline_safe[%d]", i);
1145 return -1;
1146 }
1147
1148 if (insn->type != INSN_JUMP_DYNAMIC &&
1149 insn->type != INSN_CALL_DYNAMIC) {
1150 WARN_FUNC("retpoline_safe hint not a indirect jump/call",
1151 insn->sec, insn->offset);
1152 return -1;
1153 }
1154
1155 insn->retpoline_safe = true;
1156 }
1157
1158 return 0;
1159}
1160
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001161static int decode_sections(struct objtool_file *file)
1162{
1163 int ret;
1164
1165 ret = decode_instructions(file);
1166 if (ret)
1167 return ret;
1168
1169 ret = add_dead_ends(file);
1170 if (ret)
1171 return ret;
1172
1173 add_ignores(file);
1174
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001175 ret = add_nospec_ignores(file);
1176 if (ret)
1177 return ret;
1178
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001179 ret = add_jump_destinations(file);
1180 if (ret)
1181 return ret;
1182
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001183 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001184 if (ret)
1185 return ret;
1186
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001187 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001188 if (ret)
1189 return ret;
1190
1191 ret = add_switch_table_alts(file);
1192 if (ret)
1193 return ret;
1194
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001195 ret = read_unwind_hints(file);
1196 if (ret)
1197 return ret;
1198
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001199 ret = read_retpoline_hints(file);
1200 if (ret)
1201 return ret;
1202
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001203 return 0;
1204}
1205
1206static bool is_fentry_call(struct instruction *insn)
1207{
1208 if (insn->type == INSN_CALL &&
1209 insn->call_dest->type == STT_NOTYPE &&
1210 !strcmp(insn->call_dest->name, "__fentry__"))
1211 return true;
1212
1213 return false;
1214}
1215
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001216static bool has_modified_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001217{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001218 int i;
1219
1220 if (state->cfa.base != initial_func_cfi.cfa.base ||
1221 state->cfa.offset != initial_func_cfi.cfa.offset ||
1222 state->stack_size != initial_func_cfi.cfa.offset ||
1223 state->drap)
1224 return true;
1225
1226 for (i = 0; i < CFI_NUM_REGS; i++)
1227 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1228 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1229 return true;
1230
1231 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001232}
1233
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001234static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001235{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001236 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1237 state->regs[CFI_BP].offset == -16)
1238 return true;
1239
1240 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1241 return true;
1242
1243 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001244}
1245
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001246static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1247{
1248 struct cfi_reg *cfa = &state->cfa;
1249 struct stack_op *op = &insn->stack_op;
1250
1251 if (cfa->base != CFI_SP)
1252 return 0;
1253
1254 /* push */
1255 if (op->dest.type == OP_DEST_PUSH)
1256 cfa->offset += 8;
1257
1258 /* pop */
1259 if (op->src.type == OP_SRC_POP)
1260 cfa->offset -= 8;
1261
1262 /* add immediate to sp */
1263 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1264 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1265 cfa->offset -= op->src.offset;
1266
1267 return 0;
1268}
1269
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001270static void save_reg(struct insn_state *state, unsigned char reg, int base,
1271 int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001272{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001273 if (arch_callee_saved_reg(reg) &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001274 state->regs[reg].base == CFI_UNDEFINED) {
1275 state->regs[reg].base = base;
1276 state->regs[reg].offset = offset;
1277 }
1278}
1279
1280static void restore_reg(struct insn_state *state, unsigned char reg)
1281{
1282 state->regs[reg].base = CFI_UNDEFINED;
1283 state->regs[reg].offset = 0;
1284}
1285
1286/*
1287 * A note about DRAP stack alignment:
1288 *
1289 * GCC has the concept of a DRAP register, which is used to help keep track of
1290 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1291 * register. The typical DRAP pattern is:
1292 *
1293 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1294 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1295 * 41 ff 72 f8 pushq -0x8(%r10)
1296 * 55 push %rbp
1297 * 48 89 e5 mov %rsp,%rbp
1298 * (more pushes)
1299 * 41 52 push %r10
1300 * ...
1301 * 41 5a pop %r10
1302 * (more pops)
1303 * 5d pop %rbp
1304 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1305 * c3 retq
1306 *
1307 * There are some variations in the epilogues, like:
1308 *
1309 * 5b pop %rbx
1310 * 41 5a pop %r10
1311 * 41 5c pop %r12
1312 * 41 5d pop %r13
1313 * 41 5e pop %r14
1314 * c9 leaveq
1315 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1316 * c3 retq
1317 *
1318 * and:
1319 *
1320 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1321 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1322 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1323 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1324 * c9 leaveq
1325 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1326 * c3 retq
1327 *
1328 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1329 * restored beforehand:
1330 *
1331 * 41 55 push %r13
1332 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1333 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1334 * ...
1335 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1336 * 41 5d pop %r13
1337 * c3 retq
1338 */
1339static int update_insn_state(struct instruction *insn, struct insn_state *state)
1340{
1341 struct stack_op *op = &insn->stack_op;
1342 struct cfi_reg *cfa = &state->cfa;
1343 struct cfi_reg *regs = state->regs;
1344
1345 /* stack operations don't make sense with an undefined CFA */
1346 if (cfa->base == CFI_UNDEFINED) {
1347 if (insn->func) {
1348 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1349 return -1;
1350 }
1351 return 0;
1352 }
1353
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001354 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1355 return update_insn_state_regs(insn, state);
1356
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001357 switch (op->dest.type) {
1358
1359 case OP_DEST_REG:
1360 switch (op->src.type) {
1361
1362 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001363 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1364 cfa->base == CFI_SP &&
1365 regs[CFI_BP].base == CFI_CFA &&
1366 regs[CFI_BP].offset == -cfa->offset) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001367
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001368 /* mov %rsp, %rbp */
1369 cfa->base = op->dest.reg;
1370 state->bp_scratch = false;
1371 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001372
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001373 else if (op->src.reg == CFI_SP &&
1374 op->dest.reg == CFI_BP && state->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001375
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001376 /* drap: mov %rsp, %rbp */
1377 regs[CFI_BP].base = CFI_BP;
1378 regs[CFI_BP].offset = -state->stack_size;
1379 state->bp_scratch = false;
1380 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001381
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001382 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1383
1384 /*
1385 * mov %rsp, %reg
1386 *
1387 * This is needed for the rare case where GCC
1388 * does:
1389 *
1390 * mov %rsp, %rax
1391 * ...
1392 * mov %rax, %rsp
1393 */
1394 state->vals[op->dest.reg].base = CFI_CFA;
1395 state->vals[op->dest.reg].offset = -state->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001396 }
1397
1398 else if (op->dest.reg == cfa->base) {
1399
1400 /* mov %reg, %rsp */
1401 if (cfa->base == CFI_SP &&
1402 state->vals[op->src.reg].base == CFI_CFA) {
1403
1404 /*
1405 * This is needed for the rare case
1406 * where GCC does something dumb like:
1407 *
1408 * lea 0x8(%rsp), %rcx
1409 * ...
1410 * mov %rcx, %rsp
1411 */
1412 cfa->offset = -state->vals[op->src.reg].offset;
1413 state->stack_size = cfa->offset;
1414
1415 } else {
1416 cfa->base = CFI_UNDEFINED;
1417 cfa->offset = 0;
1418 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001419 }
1420
1421 break;
1422
1423 case OP_SRC_ADD:
1424 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1425
1426 /* add imm, %rsp */
1427 state->stack_size -= op->src.offset;
1428 if (cfa->base == CFI_SP)
1429 cfa->offset -= op->src.offset;
1430 break;
1431 }
1432
1433 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1434
1435 /* lea disp(%rbp), %rsp */
1436 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1437 break;
1438 }
1439
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001440 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001441
1442 /* drap: lea disp(%rsp), %drap */
1443 state->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001444
1445 /*
1446 * lea disp(%rsp), %reg
1447 *
1448 * This is needed for the rare case where GCC
1449 * does something dumb like:
1450 *
1451 * lea 0x8(%rsp), %rcx
1452 * ...
1453 * mov %rcx, %rsp
1454 */
1455 state->vals[op->dest.reg].base = CFI_CFA;
1456 state->vals[op->dest.reg].offset = \
1457 -state->stack_size + op->src.offset;
1458
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001459 break;
1460 }
1461
1462 if (state->drap && op->dest.reg == CFI_SP &&
1463 op->src.reg == state->drap_reg) {
1464
1465 /* drap: lea disp(%drap), %rsp */
1466 cfa->base = CFI_SP;
1467 cfa->offset = state->stack_size = -op->src.offset;
1468 state->drap_reg = CFI_UNDEFINED;
1469 state->drap = false;
1470 break;
1471 }
1472
1473 if (op->dest.reg == state->cfa.base) {
1474 WARN_FUNC("unsupported stack register modification",
1475 insn->sec, insn->offset);
1476 return -1;
1477 }
1478
1479 break;
1480
1481 case OP_SRC_AND:
1482 if (op->dest.reg != CFI_SP ||
1483 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1484 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1485 WARN_FUNC("unsupported stack pointer realignment",
1486 insn->sec, insn->offset);
1487 return -1;
1488 }
1489
1490 if (state->drap_reg != CFI_UNDEFINED) {
1491 /* drap: and imm, %rsp */
1492 cfa->base = state->drap_reg;
1493 cfa->offset = state->stack_size = 0;
1494 state->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001495 }
1496
1497 /*
1498 * Older versions of GCC (4.8ish) realign the stack
1499 * without DRAP, with a frame pointer.
1500 */
1501
1502 break;
1503
1504 case OP_SRC_POP:
1505 if (!state->drap && op->dest.type == OP_DEST_REG &&
1506 op->dest.reg == cfa->base) {
1507
1508 /* pop %rbp */
1509 cfa->base = CFI_SP;
1510 }
1511
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001512 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1513 op->dest.type == OP_DEST_REG &&
1514 op->dest.reg == state->drap_reg &&
1515 state->drap_offset == -state->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001516
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001517 /* drap: pop %drap */
1518 cfa->base = state->drap_reg;
1519 cfa->offset = 0;
1520 state->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001521
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001522 } else if (regs[op->dest.reg].offset == -state->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001523
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001524 /* pop %reg */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001525 restore_reg(state, op->dest.reg);
1526 }
1527
1528 state->stack_size -= 8;
1529 if (cfa->base == CFI_SP)
1530 cfa->offset -= 8;
1531
1532 break;
1533
1534 case OP_SRC_REG_INDIRECT:
1535 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001536 op->src.offset == state->drap_offset) {
1537
1538 /* drap: mov disp(%rbp), %drap */
1539 cfa->base = state->drap_reg;
1540 cfa->offset = 0;
1541 state->drap_offset = -1;
1542 }
1543
1544 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001545 op->src.offset == regs[op->dest.reg].offset) {
1546
1547 /* drap: mov disp(%rbp), %reg */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001548 restore_reg(state, op->dest.reg);
1549
1550 } else if (op->src.reg == cfa->base &&
1551 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1552
1553 /* mov disp(%rbp), %reg */
1554 /* mov disp(%rsp), %reg */
1555 restore_reg(state, op->dest.reg);
1556 }
1557
1558 break;
1559
1560 default:
1561 WARN_FUNC("unknown stack-related instruction",
1562 insn->sec, insn->offset);
1563 return -1;
1564 }
1565
1566 break;
1567
1568 case OP_DEST_PUSH:
1569 state->stack_size += 8;
1570 if (cfa->base == CFI_SP)
1571 cfa->offset += 8;
1572
1573 if (op->src.type != OP_SRC_REG)
1574 break;
1575
1576 if (state->drap) {
1577 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1578
1579 /* drap: push %drap */
1580 cfa->base = CFI_BP_INDIRECT;
1581 cfa->offset = -state->stack_size;
1582
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001583 /* save drap so we know when to restore it */
1584 state->drap_offset = -state->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001585
1586 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1587
1588 /* drap: push %rbp */
1589 state->stack_size = 0;
1590
1591 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1592
1593 /* drap: push %reg */
1594 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1595 }
1596
1597 } else {
1598
1599 /* push %reg */
1600 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1601 }
1602
1603 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001604 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001605 cfa->base != CFI_BP)
1606 state->bp_scratch = true;
1607 break;
1608
1609 case OP_DEST_REG_INDIRECT:
1610
1611 if (state->drap) {
1612 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1613
1614 /* drap: mov %drap, disp(%rbp) */
1615 cfa->base = CFI_BP_INDIRECT;
1616 cfa->offset = op->dest.offset;
1617
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001618 /* save drap offset so we know when to restore it */
1619 state->drap_offset = op->dest.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001620 }
1621
1622 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1623
1624 /* drap: mov reg, disp(%rbp) */
1625 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1626 }
1627
1628 } else if (op->dest.reg == cfa->base) {
1629
1630 /* mov reg, disp(%rbp) */
1631 /* mov reg, disp(%rsp) */
1632 save_reg(state, op->src.reg, CFI_CFA,
1633 op->dest.offset - state->cfa.offset);
1634 }
1635
1636 break;
1637
1638 case OP_DEST_LEAVE:
1639 if ((!state->drap && cfa->base != CFI_BP) ||
1640 (state->drap && cfa->base != state->drap_reg)) {
1641 WARN_FUNC("leave instruction with modified stack frame",
1642 insn->sec, insn->offset);
1643 return -1;
1644 }
1645
1646 /* leave (mov %rbp, %rsp; pop %rbp) */
1647
1648 state->stack_size = -state->regs[CFI_BP].offset - 8;
1649 restore_reg(state, CFI_BP);
1650
1651 if (!state->drap) {
1652 cfa->base = CFI_SP;
1653 cfa->offset -= 8;
1654 }
1655
1656 break;
1657
1658 case OP_DEST_MEM:
1659 if (op->src.type != OP_SRC_POP) {
1660 WARN_FUNC("unknown stack-related memory operation",
1661 insn->sec, insn->offset);
1662 return -1;
1663 }
1664
1665 /* pop mem */
1666 state->stack_size -= 8;
1667 if (cfa->base == CFI_SP)
1668 cfa->offset -= 8;
1669
1670 break;
1671
1672 default:
1673 WARN_FUNC("unknown stack-related instruction",
1674 insn->sec, insn->offset);
1675 return -1;
1676 }
1677
1678 return 0;
1679}
1680
1681static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1682{
1683 struct insn_state *state1 = &insn->state, *state2 = state;
1684 int i;
1685
1686 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1687 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1688 insn->sec, insn->offset,
1689 state1->cfa.base, state1->cfa.offset,
1690 state2->cfa.base, state2->cfa.offset);
1691
1692 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1693 for (i = 0; i < CFI_NUM_REGS; i++) {
1694 if (!memcmp(&state1->regs[i], &state2->regs[i],
1695 sizeof(struct cfi_reg)))
1696 continue;
1697
1698 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1699 insn->sec, insn->offset,
1700 i, state1->regs[i].base, state1->regs[i].offset,
1701 i, state2->regs[i].base, state2->regs[i].offset);
1702 break;
1703 }
1704
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001705 } else if (state1->type != state2->type) {
1706 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1707 insn->sec, insn->offset, state1->type, state2->type);
1708
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001709 } else if (state1->drap != state2->drap ||
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001710 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1711 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1712 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001713 insn->sec, insn->offset,
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001714 state1->drap, state1->drap_reg, state1->drap_offset,
1715 state2->drap, state2->drap_reg, state2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001716
1717 } else
1718 return true;
1719
1720 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001721}
1722
1723/*
1724 * Follow the branch starting at the given instruction, and recursively follow
1725 * any other branches (jumps). Meanwhile, track the frame pointer state at
1726 * each instruction and validate all the rules described in
1727 * tools/objtool/Documentation/stack-validation.txt.
1728 */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001729static int validate_branch(struct objtool_file *file, struct instruction *first,
1730 struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001731{
1732 struct alternative *alt;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001733 struct instruction *insn, *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001734 struct section *sec;
1735 struct symbol *func = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001736 int ret;
1737
1738 insn = first;
1739 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001740
1741 if (insn->alt_group && list_empty(&insn->alts)) {
1742 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1743 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001744 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001745 }
1746
1747 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001748 next_insn = next_insn_same_sec(file, insn);
1749
Josh Poimboeufee976382017-08-11 12:24:15 -05001750
1751 if (file->c_file && func && insn->func && func != insn->func) {
1752 WARN("%s() falls through to next function %s()",
1753 func->name, insn->func->name);
1754 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001755 }
1756
Josh Poimboeufee976382017-08-11 12:24:15 -05001757 if (insn->func)
1758 func = insn->func;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001759
Josh Poimboeuf48550222017-07-07 09:19:42 -05001760 if (func && insn->ignore) {
1761 WARN_FUNC("BUG: why am I validating an ignored function?",
1762 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001763 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001764 }
1765
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001766 if (insn->visited) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001767 if (!insn->hint && !insn_state_match(insn, &state))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001768 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001769
1770 return 0;
1771 }
1772
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001773 if (insn->hint) {
1774 if (insn->restore) {
1775 struct instruction *save_insn, *i;
1776
1777 i = insn;
1778 save_insn = NULL;
1779 func_for_each_insn_continue_reverse(file, func, i) {
1780 if (i->save) {
1781 save_insn = i;
1782 break;
1783 }
1784 }
1785
1786 if (!save_insn) {
1787 WARN_FUNC("no corresponding CFI save for CFI restore",
1788 sec, insn->offset);
1789 return 1;
1790 }
1791
1792 if (!save_insn->visited) {
1793 /*
1794 * Oops, no state to copy yet.
1795 * Hopefully we can reach this
1796 * instruction from another branch
1797 * after the save insn has been
1798 * visited.
1799 */
1800 if (insn == first)
1801 return 0;
1802
1803 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1804 sec, insn->offset);
1805 return 1;
1806 }
1807
1808 insn->state = save_insn->state;
1809 }
1810
1811 state = insn->state;
1812
1813 } else
1814 insn->state = state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001815
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001816 insn->visited = true;
1817
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001818 if (!insn->ignore_alts) {
1819 list_for_each_entry(alt, &insn->alts, list) {
1820 ret = validate_branch(file, alt->insn, state);
1821 if (ret)
1822 return 1;
1823 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001824 }
1825
1826 switch (insn->type) {
1827
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001828 case INSN_RETURN:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001829 if (func && has_modified_stack_frame(&state)) {
1830 WARN_FUNC("return with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001831 sec, insn->offset);
1832 return 1;
1833 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001834
1835 if (state.bp_scratch) {
1836 WARN("%s uses BP as a scratch register",
1837 insn->func->name);
1838 return 1;
1839 }
1840
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001841 return 0;
1842
1843 case INSN_CALL:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001844 if (is_fentry_call(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001845 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001846
1847 ret = dead_end_function(file, insn->call_dest);
1848 if (ret == 1)
1849 return 0;
1850 if (ret == -1)
1851 return 1;
1852
1853 /* fallthrough */
1854 case INSN_CALL_DYNAMIC:
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001855 if (!no_fp && func && !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001856 WARN_FUNC("call without frame pointer save/setup",
1857 sec, insn->offset);
1858 return 1;
1859 }
1860 break;
1861
1862 case INSN_JUMP_CONDITIONAL:
1863 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf48550222017-07-07 09:19:42 -05001864 if (insn->jump_dest &&
1865 (!func || !insn->jump_dest->func ||
1866 func == insn->jump_dest->func)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001867 ret = validate_branch(file, insn->jump_dest,
1868 state);
1869 if (ret)
1870 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001871
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001872 } else if (func && has_modified_stack_frame(&state)) {
1873 WARN_FUNC("sibling call from callable instruction with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001874 sec, insn->offset);
1875 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001876 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001877
1878 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1879 return 0;
1880
1881 break;
1882
1883 case INSN_JUMP_DYNAMIC:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001884 if (func && list_empty(&insn->alts) &&
1885 has_modified_stack_frame(&state)) {
1886 WARN_FUNC("sibling call from callable instruction with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001887 sec, insn->offset);
1888 return 1;
1889 }
1890
1891 return 0;
1892
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001893 case INSN_CONTEXT_SWITCH:
1894 if (func && (!next_insn || !next_insn->hint)) {
1895 WARN_FUNC("unsupported instruction in callable function",
1896 sec, insn->offset);
1897 return 1;
1898 }
1899 return 0;
1900
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001901 case INSN_STACK:
1902 if (update_insn_state(insn, &state))
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001903 return 1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001904
1905 break;
1906
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001907 default:
1908 break;
1909 }
1910
1911 if (insn->dead_end)
1912 return 0;
1913
Josh Poimboeuf00d96182017-09-18 21:43:30 -05001914 if (!next_insn) {
1915 if (state.cfa.base == CFI_UNDEFINED)
1916 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001917 WARN("%s: unexpected end of section", sec->name);
1918 return 1;
1919 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05001920
1921 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001922 }
1923
1924 return 0;
1925}
1926
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001927static int validate_unwind_hints(struct objtool_file *file)
1928{
1929 struct instruction *insn;
1930 int ret, warnings = 0;
1931 struct insn_state state;
1932
1933 if (!file->hints)
1934 return 0;
1935
1936 clear_insn_state(&state);
1937
1938 for_each_insn(file, insn) {
1939 if (insn->hint && !insn->visited) {
1940 ret = validate_branch(file, insn, state);
1941 warnings += ret;
1942 }
1943 }
1944
1945 return warnings;
1946}
1947
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001948static int validate_retpoline(struct objtool_file *file)
1949{
1950 struct instruction *insn;
1951 int warnings = 0;
1952
1953 for_each_insn(file, insn) {
1954 if (insn->type != INSN_JUMP_DYNAMIC &&
1955 insn->type != INSN_CALL_DYNAMIC)
1956 continue;
1957
1958 if (insn->retpoline_safe)
1959 continue;
1960
1961 WARN_FUNC("indirect %s found in RETPOLINE build",
1962 insn->sec, insn->offset,
1963 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
1964
1965 warnings++;
1966 }
1967
1968 return warnings;
1969}
1970
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001971static bool is_kasan_insn(struct instruction *insn)
1972{
1973 return (insn->type == INSN_CALL &&
1974 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1975}
1976
1977static bool is_ubsan_insn(struct instruction *insn)
1978{
1979 return (insn->type == INSN_CALL &&
1980 !strcmp(insn->call_dest->name,
1981 "__ubsan_handle_builtin_unreachable"));
1982}
1983
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001984static bool ignore_unreachable_insn(struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001985{
1986 int i;
1987
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001988 if (insn->ignore || insn->type == INSN_NOP)
1989 return true;
1990
1991 /*
1992 * Ignore any unused exceptions. This can happen when a whitelisted
1993 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05001994 *
1995 * Also ignore alternative replacement instructions. This can happen
1996 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001997 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05001998 if (!strcmp(insn->sec->name, ".fixup") ||
1999 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2000 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002001 return true;
2002
2003 /*
2004 * Check if this (or a subsequent) instruction is related to
2005 * CONFIG_UBSAN or CONFIG_KASAN.
2006 *
2007 * End the search at 5 instructions to avoid going into the weeds.
2008 */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002009 if (!insn->func)
2010 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002011 for (i = 0; i < 5; i++) {
2012
2013 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2014 return true;
2015
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002016 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2017 if (insn->jump_dest &&
2018 insn->jump_dest->func == insn->func) {
2019 insn = insn->jump_dest;
2020 continue;
2021 }
2022
2023 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002024 }
2025
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002026 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002027 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002028
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002029 insn = list_next_entry(insn, list);
2030 }
2031
2032 return false;
2033}
2034
2035static int validate_functions(struct objtool_file *file)
2036{
2037 struct section *sec;
2038 struct symbol *func;
2039 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002040 struct insn_state state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002041 int ret, warnings = 0;
2042
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002043 clear_insn_state(&state);
2044
2045 state.cfa = initial_func_cfi.cfa;
2046 memcpy(&state.regs, &initial_func_cfi.regs,
2047 CFI_NUM_REGS * sizeof(struct cfi_reg));
2048 state.stack_size = initial_func_cfi.cfa.offset;
2049
2050 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002051 list_for_each_entry(func, &sec->symbol_list, list) {
2052 if (func->type != STT_FUNC)
2053 continue;
2054
2055 insn = find_insn(file, sec, func->offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002056 if (!insn || insn->ignore)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002057 continue;
2058
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002059 ret = validate_branch(file, insn, state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002060 warnings += ret;
2061 }
2062 }
2063
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002064 return warnings;
2065}
2066
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002067static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002068{
2069 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002070
2071 if (file->ignore_unreachables)
2072 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002073
2074 for_each_insn(file, insn) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002075 if (insn->visited || ignore_unreachable_insn(insn))
2076 continue;
2077
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002078 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2079 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002080 }
2081
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002082 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002083}
2084
2085static void cleanup(struct objtool_file *file)
2086{
2087 struct instruction *insn, *tmpinsn;
2088 struct alternative *alt, *tmpalt;
2089
2090 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2091 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2092 list_del(&alt->list);
2093 free(alt);
2094 }
2095 list_del(&insn->list);
2096 hash_del(&insn->hash);
2097 free(insn);
2098 }
2099 elf_close(file->elf);
2100}
2101
Peter Zijlstra43a45252018-01-16 17:16:32 +01002102int check(const char *_objname, bool orc)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002103{
2104 struct objtool_file file;
2105 int ret, warnings = 0;
2106
2107 objname = _objname;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002108
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002109 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002110 if (!file.elf)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002111 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002112
2113 INIT_LIST_HEAD(&file.insn_list);
2114 hash_init(file.insn_hash);
2115 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
2116 file.rodata = find_section_by_name(file.elf, ".rodata");
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002117 file.c_file = find_section_by_name(file.elf, ".comment");
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002118 file.ignore_unreachables = no_unreachable;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002119 file.hints = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002120
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002121 arch_initial_func_cfi_state(&initial_func_cfi);
2122
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002123 ret = decode_sections(&file);
2124 if (ret < 0)
2125 goto out;
2126 warnings += ret;
2127
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002128 if (list_empty(&file.insn_list))
2129 goto out;
2130
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002131 if (retpoline) {
2132 ret = validate_retpoline(&file);
2133 if (ret < 0)
2134 return ret;
2135 warnings += ret;
2136 }
2137
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002138 ret = validate_functions(&file);
2139 if (ret < 0)
2140 goto out;
2141 warnings += ret;
2142
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002143 ret = validate_unwind_hints(&file);
2144 if (ret < 0)
2145 goto out;
2146 warnings += ret;
2147
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002148 if (!warnings) {
2149 ret = validate_reachable_instructions(&file);
2150 if (ret < 0)
2151 goto out;
2152 warnings += ret;
2153 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002154
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002155 if (orc) {
2156 ret = create_orc(&file);
2157 if (ret < 0)
2158 goto out;
2159
2160 ret = create_orc_sections(&file);
2161 if (ret < 0)
2162 goto out;
2163
2164 ret = elf_write(file.elf);
2165 if (ret < 0)
2166 goto out;
2167 }
2168
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002169out:
2170 cleanup(&file);
2171
2172 /* ignore warnings for now until we get all the code cleaned up */
2173 if (ret || warnings)
2174 return 0;
2175 return 0;
2176}