blob: 9cd028aa1509808ec87e566ca018d939e3b9734c [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
21#include "check.h"
22#include "elf.h"
23#include "special.h"
24#include "arch.h"
25#include "warn.h"
26
27#include <linux/hashtable.h>
28#include <linux/kernel.h>
29
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050030struct alternative {
31 struct list_head list;
32 struct instruction *insn;
33};
34
35const char *objname;
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -050036static bool no_fp;
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",
141 };
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
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500215static 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;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500221 for (i = 0; i < CFI_NUM_REGS; i++) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500222 state->regs[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500223 state->vals[i].base = CFI_UNDEFINED;
224 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500225 state->drap_reg = CFI_UNDEFINED;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -0500226 state->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500227}
228
Josh Poimboeufdcc914f2017-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
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500241 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500242
243 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
244 continue;
245
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500246 if (strcmp(sec->name, ".altinstr_replacement") &&
247 strcmp(sec->name, ".altinstr_aux") &&
248 strncmp(sec->name, ".discard.", 9))
249 sec->text = true;
250
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500251 for (offset = 0; offset < sec->len; offset += insn->len) {
252 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500253 if (!insn) {
254 WARN("malloc failed");
255 return -1;
256 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500257 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500258 INIT_LIST_HEAD(&insn->alts);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500259 clear_insn_state(&insn->state);
260
Josh Poimboeufdcc914f2017-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,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500267 &insn->immediate,
268 &insn->stack_op);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500269 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500270 goto err;
Josh Poimboeufdcc914f2017-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);
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500275 ret = -1;
276 goto err;
Josh Poimboeufdcc914f2017-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;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500300
301err:
302 free(insn);
303 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500304}
305
306/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500307 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-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
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500316 /*
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 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500327 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
328 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500329 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500330
331 list_for_each_entry(rela, &sec->rela_list, list) {
332 if (rela->sym->type != STT_SECTION) {
333 WARN("unexpected relocation symbol type in %s", sec->name);
334 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
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500362reachable:
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 Poimboeufdcc914f2017-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
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500416 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-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)
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500425 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500426 }
427 }
428}
429
430/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000431 * 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/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500465 * 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
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500479 if (insn->ignore)
Josh Poimboeufdcc914f2017-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;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000493 } 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;
499 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500500 } else {
501 /* sibling call */
502 insn->jump_dest = 0;
503 continue;
504 }
505
506 insn->jump_dest = find_insn(file, dest_sec, dest_off);
507 if (!insn->jump_dest) {
508
509 /*
510 * This is a special case where an alt instruction
511 * jumps past the end of the section. These are
512 * handled later in handle_group_alt().
513 */
514 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
515 continue;
516
517 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
518 insn->sec, insn->offset, dest_sec->name,
519 dest_off);
520 return -1;
521 }
522 }
523
524 return 0;
525}
526
527/*
528 * Find the destination instructions for all calls.
529 */
530static int add_call_destinations(struct objtool_file *file)
531{
532 struct instruction *insn;
533 unsigned long dest_off;
534 struct rela *rela;
535
536 for_each_insn(file, insn) {
537 if (insn->type != INSN_CALL)
538 continue;
539
540 rela = find_rela_by_dest_range(insn->sec, insn->offset,
541 insn->len);
542 if (!rela) {
543 dest_off = insn->offset + insn->len + insn->immediate;
544 insn->call_dest = find_symbol_by_offset(insn->sec,
545 dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600546
547 if (!insn->call_dest && !insn->ignore) {
548 WARN_FUNC("unsupported intra-function call",
549 insn->sec, insn->offset);
550 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 -0500551 return -1;
552 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600553
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500554 } else if (rela->sym->type == STT_SECTION) {
555 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
556 rela->addend+4);
557 if (!insn->call_dest ||
558 insn->call_dest->type != STT_FUNC) {
559 WARN_FUNC("can't find call dest symbol at %s+0x%x",
560 insn->sec, insn->offset,
561 rela->sym->sec->name,
562 rela->addend + 4);
563 return -1;
564 }
565 } else
566 insn->call_dest = rela->sym;
567 }
568
569 return 0;
570}
571
572/*
573 * The .alternatives section requires some extra special care, over and above
574 * what other special sections require:
575 *
576 * 1. Because alternatives are patched in-place, we need to insert a fake jump
577 * instruction at the end so that validate_branch() skips all the original
578 * replaced instructions when validating the new instruction path.
579 *
580 * 2. An added wrinkle is that the new instruction length might be zero. In
581 * that case the old instructions are replaced with noops. We simulate that
582 * by creating a fake jump as the only new instruction.
583 *
584 * 3. In some cases, the alternative section includes an instruction which
585 * conditionally jumps to the _end_ of the entry. We have to modify these
586 * jumps' destinations to point back to .text rather than the end of the
587 * entry in .altinstr_replacement.
588 *
589 * 4. It has been requested that we don't validate the !POPCNT feature path
590 * which is a "very very small percentage of machines".
591 */
592static int handle_group_alt(struct objtool_file *file,
593 struct special_alt *special_alt,
594 struct instruction *orig_insn,
595 struct instruction **new_insn)
596{
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600597 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500598 unsigned long dest_off;
599
600 last_orig_insn = NULL;
601 insn = orig_insn;
602 sec_for_each_insn_from(file, insn) {
603 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
604 break;
605
606 if (special_alt->skip_orig)
607 insn->type = INSN_NOP;
608
609 insn->alt_group = true;
610 last_orig_insn = insn;
611 }
612
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600613 if (next_insn_same_sec(file, last_orig_insn)) {
614 fake_jump = malloc(sizeof(*fake_jump));
615 if (!fake_jump) {
616 WARN("malloc failed");
617 return -1;
618 }
619 memset(fake_jump, 0, sizeof(*fake_jump));
620 INIT_LIST_HEAD(&fake_jump->alts);
621 clear_insn_state(&fake_jump->state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500622
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600623 fake_jump->sec = special_alt->new_sec;
624 fake_jump->offset = -1;
625 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
626 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
627 fake_jump->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500628 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500629
630 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600631 if (!fake_jump) {
632 WARN("%s: empty alternative at end of section",
633 special_alt->orig_sec->name);
634 return -1;
635 }
636
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500637 *new_insn = fake_jump;
638 return 0;
639 }
640
641 last_new_insn = NULL;
642 insn = *new_insn;
643 sec_for_each_insn_from(file, insn) {
644 if (insn->offset >= special_alt->new_off + special_alt->new_len)
645 break;
646
647 last_new_insn = insn;
648
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600649 insn->ignore = orig_insn->ignore_alts;
650
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500651 if (insn->type != INSN_JUMP_CONDITIONAL &&
652 insn->type != INSN_JUMP_UNCONDITIONAL)
653 continue;
654
655 if (!insn->immediate)
656 continue;
657
658 dest_off = insn->offset + insn->len + insn->immediate;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600659 if (dest_off == special_alt->new_off + special_alt->new_len) {
660 if (!fake_jump) {
661 WARN("%s: alternative jump to end of section",
662 special_alt->orig_sec->name);
663 return -1;
664 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500665 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600666 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500667
668 if (!insn->jump_dest) {
669 WARN_FUNC("can't find alternative jump destination",
670 insn->sec, insn->offset);
671 return -1;
672 }
673 }
674
675 if (!last_new_insn) {
676 WARN_FUNC("can't find last new alternative instruction",
677 special_alt->new_sec, special_alt->new_off);
678 return -1;
679 }
680
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600681 if (fake_jump)
682 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500683
684 return 0;
685}
686
687/*
688 * A jump table entry can either convert a nop to a jump or a jump to a nop.
689 * If the original instruction is a jump, make the alt entry an effective nop
690 * by just skipping the original instruction.
691 */
692static int handle_jump_alt(struct objtool_file *file,
693 struct special_alt *special_alt,
694 struct instruction *orig_insn,
695 struct instruction **new_insn)
696{
697 if (orig_insn->type == INSN_NOP)
698 return 0;
699
700 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
701 WARN_FUNC("unsupported instruction at jump label",
702 orig_insn->sec, orig_insn->offset);
703 return -1;
704 }
705
706 *new_insn = list_next_entry(orig_insn, list);
707 return 0;
708}
709
710/*
711 * Read all the special sections which have alternate instructions which can be
712 * patched in or redirected to at runtime. Each instruction having alternate
713 * instruction(s) has them added to its insn->alts list, which will be
714 * traversed in validate_branch().
715 */
716static int add_special_section_alts(struct objtool_file *file)
717{
718 struct list_head special_alts;
719 struct instruction *orig_insn, *new_insn;
720 struct special_alt *special_alt, *tmp;
721 struct alternative *alt;
722 int ret;
723
724 ret = special_get_alts(file->elf, &special_alts);
725 if (ret)
726 return ret;
727
728 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500729
730 orig_insn = find_insn(file, special_alt->orig_sec,
731 special_alt->orig_off);
732 if (!orig_insn) {
733 WARN_FUNC("special: can't find orig instruction",
734 special_alt->orig_sec, special_alt->orig_off);
735 ret = -1;
736 goto out;
737 }
738
739 new_insn = NULL;
740 if (!special_alt->group || special_alt->new_len) {
741 new_insn = find_insn(file, special_alt->new_sec,
742 special_alt->new_off);
743 if (!new_insn) {
744 WARN_FUNC("special: can't find new instruction",
745 special_alt->new_sec,
746 special_alt->new_off);
747 ret = -1;
748 goto out;
749 }
750 }
751
752 if (special_alt->group) {
753 ret = handle_group_alt(file, special_alt, orig_insn,
754 &new_insn);
755 if (ret)
756 goto out;
757 } else if (special_alt->jump_or_nop) {
758 ret = handle_jump_alt(file, special_alt, orig_insn,
759 &new_insn);
760 if (ret)
761 goto out;
762 }
763
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000764 alt = malloc(sizeof(*alt));
765 if (!alt) {
766 WARN("malloc failed");
767 ret = -1;
768 goto out;
769 }
770
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500771 alt->insn = new_insn;
772 list_add_tail(&alt->list, &orig_insn->alts);
773
774 list_del(&special_alt->list);
775 free(special_alt);
776 }
777
778out:
779 return ret;
780}
781
782static int add_switch_table(struct objtool_file *file, struct symbol *func,
783 struct instruction *insn, struct rela *table,
784 struct rela *next_table)
785{
786 struct rela *rela = table;
787 struct instruction *alt_insn;
788 struct alternative *alt;
789
790 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
791 if (rela == next_table)
792 break;
793
794 if (rela->sym->sec != insn->sec ||
795 rela->addend <= func->offset ||
796 rela->addend >= func->offset + func->len)
797 break;
798
799 alt_insn = find_insn(file, insn->sec, rela->addend);
800 if (!alt_insn) {
801 WARN("%s: can't find instruction at %s+0x%x",
802 file->rodata->rela->name, insn->sec->name,
803 rela->addend);
804 return -1;
805 }
806
807 alt = malloc(sizeof(*alt));
808 if (!alt) {
809 WARN("malloc failed");
810 return -1;
811 }
812
813 alt->insn = alt_insn;
814 list_add_tail(&alt->list, &insn->alts);
815 }
816
817 return 0;
818}
819
820/*
821 * find_switch_table() - Given a dynamic jump, find the switch jump table in
822 * .rodata associated with it.
823 *
824 * There are 3 basic patterns:
825 *
826 * 1. jmpq *[rodata addr](,%reg,8)
827 *
828 * This is the most common case by far. It jumps to an address in a simple
829 * jump table which is stored in .rodata.
830 *
831 * 2. jmpq *[rodata addr](%rip)
832 *
833 * This is caused by a rare GCC quirk, currently only seen in three driver
834 * functions in the kernel, only with certain obscure non-distro configs.
835 *
836 * As part of an optimization, GCC makes a copy of an existing switch jump
837 * table, modifies it, and then hard-codes the jump (albeit with an indirect
838 * jump) to use a single entry in the table. The rest of the jump table and
839 * some of its jump targets remain as dead code.
840 *
841 * In such a case we can just crudely ignore all unreachable instruction
842 * warnings for the entire object file. Ideally we would just ignore them
843 * for the function, but that would require redesigning the code quite a
844 * bit. And honestly that's just not worth doing: unreachable instruction
845 * warnings are of questionable value anyway, and this is such a rare issue.
846 *
847 * 3. mov [rodata addr],%reg1
848 * ... some instructions ...
849 * jmpq *(%reg1,%reg2,8)
850 *
851 * This is a fairly uncommon pattern which is new for GCC 6. As of this
852 * writing, there are 11 occurrences of it in the allmodconfig kernel.
853 *
854 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
855 * ensure the same register is used in the mov and jump instructions.
856 */
857static struct rela *find_switch_table(struct objtool_file *file,
858 struct symbol *func,
859 struct instruction *insn)
860{
861 struct rela *text_rela, *rodata_rela;
862 struct instruction *orig_insn = insn;
863
864 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
865 if (text_rela && text_rela->sym == file->rodata->sym) {
866 /* case 1 */
867 rodata_rela = find_rela_by_dest(file->rodata,
868 text_rela->addend);
869 if (rodata_rela)
870 return rodata_rela;
871
872 /* case 2 */
873 rodata_rela = find_rela_by_dest(file->rodata,
874 text_rela->addend + 4);
875 if (!rodata_rela)
876 return NULL;
877 file->ignore_unreachables = true;
878 return rodata_rela;
879 }
880
881 /* case 3 */
882 func_for_each_insn_continue_reverse(file, func, insn) {
883 if (insn->type == INSN_JUMP_DYNAMIC)
884 break;
885
886 /* allow small jumps within the range */
887 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
888 insn->jump_dest &&
889 (insn->jump_dest->offset <= insn->offset ||
890 insn->jump_dest->offset > orig_insn->offset))
891 break;
892
893 /* look for a relocation which references .rodata */
894 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
895 insn->len);
896 if (!text_rela || text_rela->sym != file->rodata->sym)
897 continue;
898
899 /*
900 * Make sure the .rodata address isn't associated with a
901 * symbol. gcc jump tables are anonymous data.
902 */
903 if (find_symbol_containing(file->rodata, text_rela->addend))
904 continue;
905
906 return find_rela_by_dest(file->rodata, text_rela->addend);
907 }
908
909 return NULL;
910}
911
912static int add_func_switch_tables(struct objtool_file *file,
913 struct symbol *func)
914{
915 struct instruction *insn, *prev_jump = NULL;
916 struct rela *rela, *prev_rela = NULL;
917 int ret;
918
919 func_for_each_insn(file, func, insn) {
920 if (insn->type != INSN_JUMP_DYNAMIC)
921 continue;
922
923 rela = find_switch_table(file, func, insn);
924 if (!rela)
925 continue;
926
927 /*
928 * We found a switch table, but we don't know yet how big it
929 * is. Don't add it until we reach the end of the function or
930 * the beginning of another switch table in the same function.
931 */
932 if (prev_jump) {
933 ret = add_switch_table(file, func, prev_jump, prev_rela,
934 rela);
935 if (ret)
936 return ret;
937 }
938
939 prev_jump = insn;
940 prev_rela = rela;
941 }
942
943 if (prev_jump) {
944 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
945 if (ret)
946 return ret;
947 }
948
949 return 0;
950}
951
952/*
953 * For some switch statements, gcc generates a jump table in the .rodata
954 * section which contains a list of addresses within the function to jump to.
955 * This finds these jump tables and adds them to the insn->alts lists.
956 */
957static int add_switch_table_alts(struct objtool_file *file)
958{
959 struct section *sec;
960 struct symbol *func;
961 int ret;
962
963 if (!file->rodata || !file->rodata->rela)
964 return 0;
965
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500966 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500967 list_for_each_entry(func, &sec->symbol_list, list) {
968 if (func->type != STT_FUNC)
969 continue;
970
971 ret = add_func_switch_tables(file, func);
972 if (ret)
973 return ret;
974 }
975 }
976
977 return 0;
978}
979
Josh Poimboeuf39358a02017-07-11 10:33:43 -0500980static int read_unwind_hints(struct objtool_file *file)
981{
982 struct section *sec, *relasec;
983 struct rela *rela;
984 struct unwind_hint *hint;
985 struct instruction *insn;
986 struct cfi_reg *cfa;
987 int i;
988
989 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
990 if (!sec)
991 return 0;
992
993 relasec = sec->rela;
994 if (!relasec) {
995 WARN("missing .rela.discard.unwind_hints section");
996 return -1;
997 }
998
999 if (sec->len % sizeof(struct unwind_hint)) {
1000 WARN("struct unwind_hint size mismatch");
1001 return -1;
1002 }
1003
1004 file->hints = true;
1005
1006 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1007 hint = (struct unwind_hint *)sec->data->d_buf + i;
1008
1009 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1010 if (!rela) {
1011 WARN("can't find rela for unwind_hints[%d]", i);
1012 return -1;
1013 }
1014
1015 insn = find_insn(file, rela->sym->sec, rela->addend);
1016 if (!insn) {
1017 WARN("can't find insn for unwind_hints[%d]", i);
1018 return -1;
1019 }
1020
1021 cfa = &insn->state.cfa;
1022
1023 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1024 insn->save = true;
1025 continue;
1026
1027 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1028 insn->restore = true;
1029 insn->hint = true;
1030 continue;
1031 }
1032
1033 insn->hint = true;
1034
1035 switch (hint->sp_reg) {
1036 case ORC_REG_UNDEFINED:
1037 cfa->base = CFI_UNDEFINED;
1038 break;
1039 case ORC_REG_SP:
1040 cfa->base = CFI_SP;
1041 break;
1042 case ORC_REG_BP:
1043 cfa->base = CFI_BP;
1044 break;
1045 case ORC_REG_SP_INDIRECT:
1046 cfa->base = CFI_SP_INDIRECT;
1047 break;
1048 case ORC_REG_R10:
1049 cfa->base = CFI_R10;
1050 break;
1051 case ORC_REG_R13:
1052 cfa->base = CFI_R13;
1053 break;
1054 case ORC_REG_DI:
1055 cfa->base = CFI_DI;
1056 break;
1057 case ORC_REG_DX:
1058 cfa->base = CFI_DX;
1059 break;
1060 default:
1061 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1062 insn->sec, insn->offset, hint->sp_reg);
1063 return -1;
1064 }
1065
1066 cfa->offset = hint->sp_offset;
1067 insn->state.type = hint->type;
1068 }
1069
1070 return 0;
1071}
1072
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001073static int decode_sections(struct objtool_file *file)
1074{
1075 int ret;
1076
1077 ret = decode_instructions(file);
1078 if (ret)
1079 return ret;
1080
1081 ret = add_dead_ends(file);
1082 if (ret)
1083 return ret;
1084
1085 add_ignores(file);
1086
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001087 ret = add_nospec_ignores(file);
1088 if (ret)
1089 return ret;
1090
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001091 ret = add_jump_destinations(file);
1092 if (ret)
1093 return ret;
1094
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001095 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001096 if (ret)
1097 return ret;
1098
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001099 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001100 if (ret)
1101 return ret;
1102
1103 ret = add_switch_table_alts(file);
1104 if (ret)
1105 return ret;
1106
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001107 ret = read_unwind_hints(file);
1108 if (ret)
1109 return ret;
1110
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001111 return 0;
1112}
1113
1114static bool is_fentry_call(struct instruction *insn)
1115{
1116 if (insn->type == INSN_CALL &&
1117 insn->call_dest->type == STT_NOTYPE &&
1118 !strcmp(insn->call_dest->name, "__fentry__"))
1119 return true;
1120
1121 return false;
1122}
1123
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001124static bool has_modified_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001125{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001126 int i;
1127
1128 if (state->cfa.base != initial_func_cfi.cfa.base ||
1129 state->cfa.offset != initial_func_cfi.cfa.offset ||
1130 state->stack_size != initial_func_cfi.cfa.offset ||
1131 state->drap)
1132 return true;
1133
1134 for (i = 0; i < CFI_NUM_REGS; i++)
1135 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1136 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1137 return true;
1138
1139 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001140}
1141
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001142static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001143{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001144 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1145 state->regs[CFI_BP].offset == -16)
1146 return true;
1147
1148 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1149 return true;
1150
1151 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001152}
1153
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001154static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1155{
1156 struct cfi_reg *cfa = &state->cfa;
1157 struct stack_op *op = &insn->stack_op;
1158
1159 if (cfa->base != CFI_SP)
1160 return 0;
1161
1162 /* push */
1163 if (op->dest.type == OP_DEST_PUSH)
1164 cfa->offset += 8;
1165
1166 /* pop */
1167 if (op->src.type == OP_SRC_POP)
1168 cfa->offset -= 8;
1169
1170 /* add immediate to sp */
1171 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1172 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1173 cfa->offset -= op->src.offset;
1174
1175 return 0;
1176}
1177
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001178static void save_reg(struct insn_state *state, unsigned char reg, int base,
1179 int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001180{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001181 if (arch_callee_saved_reg(reg) &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001182 state->regs[reg].base == CFI_UNDEFINED) {
1183 state->regs[reg].base = base;
1184 state->regs[reg].offset = offset;
1185 }
1186}
1187
1188static void restore_reg(struct insn_state *state, unsigned char reg)
1189{
1190 state->regs[reg].base = CFI_UNDEFINED;
1191 state->regs[reg].offset = 0;
1192}
1193
1194/*
1195 * A note about DRAP stack alignment:
1196 *
1197 * GCC has the concept of a DRAP register, which is used to help keep track of
1198 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1199 * register. The typical DRAP pattern is:
1200 *
1201 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1202 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1203 * 41 ff 72 f8 pushq -0x8(%r10)
1204 * 55 push %rbp
1205 * 48 89 e5 mov %rsp,%rbp
1206 * (more pushes)
1207 * 41 52 push %r10
1208 * ...
1209 * 41 5a pop %r10
1210 * (more pops)
1211 * 5d pop %rbp
1212 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1213 * c3 retq
1214 *
1215 * There are some variations in the epilogues, like:
1216 *
1217 * 5b pop %rbx
1218 * 41 5a pop %r10
1219 * 41 5c pop %r12
1220 * 41 5d pop %r13
1221 * 41 5e pop %r14
1222 * c9 leaveq
1223 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1224 * c3 retq
1225 *
1226 * and:
1227 *
1228 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1229 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1230 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1231 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1232 * c9 leaveq
1233 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1234 * c3 retq
1235 *
1236 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1237 * restored beforehand:
1238 *
1239 * 41 55 push %r13
1240 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1241 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1242 * ...
1243 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1244 * 41 5d pop %r13
1245 * c3 retq
1246 */
1247static int update_insn_state(struct instruction *insn, struct insn_state *state)
1248{
1249 struct stack_op *op = &insn->stack_op;
1250 struct cfi_reg *cfa = &state->cfa;
1251 struct cfi_reg *regs = state->regs;
1252
1253 /* stack operations don't make sense with an undefined CFA */
1254 if (cfa->base == CFI_UNDEFINED) {
1255 if (insn->func) {
1256 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1257 return -1;
1258 }
1259 return 0;
1260 }
1261
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001262 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1263 return update_insn_state_regs(insn, state);
1264
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001265 switch (op->dest.type) {
1266
1267 case OP_DEST_REG:
1268 switch (op->src.type) {
1269
1270 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001271 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1272 cfa->base == CFI_SP &&
1273 regs[CFI_BP].base == CFI_CFA &&
1274 regs[CFI_BP].offset == -cfa->offset) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001275
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001276 /* mov %rsp, %rbp */
1277 cfa->base = op->dest.reg;
1278 state->bp_scratch = false;
1279 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001280
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001281 else if (op->src.reg == CFI_SP &&
1282 op->dest.reg == CFI_BP && state->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001283
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001284 /* drap: mov %rsp, %rbp */
1285 regs[CFI_BP].base = CFI_BP;
1286 regs[CFI_BP].offset = -state->stack_size;
1287 state->bp_scratch = false;
1288 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001289
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001290 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1291
1292 /*
1293 * mov %rsp, %reg
1294 *
1295 * This is needed for the rare case where GCC
1296 * does:
1297 *
1298 * mov %rsp, %rax
1299 * ...
1300 * mov %rax, %rsp
1301 */
1302 state->vals[op->dest.reg].base = CFI_CFA;
1303 state->vals[op->dest.reg].offset = -state->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001304 }
1305
1306 else if (op->dest.reg == cfa->base) {
1307
1308 /* mov %reg, %rsp */
1309 if (cfa->base == CFI_SP &&
1310 state->vals[op->src.reg].base == CFI_CFA) {
1311
1312 /*
1313 * This is needed for the rare case
1314 * where GCC does something dumb like:
1315 *
1316 * lea 0x8(%rsp), %rcx
1317 * ...
1318 * mov %rcx, %rsp
1319 */
1320 cfa->offset = -state->vals[op->src.reg].offset;
1321 state->stack_size = cfa->offset;
1322
1323 } else {
1324 cfa->base = CFI_UNDEFINED;
1325 cfa->offset = 0;
1326 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001327 }
1328
1329 break;
1330
1331 case OP_SRC_ADD:
1332 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1333
1334 /* add imm, %rsp */
1335 state->stack_size -= op->src.offset;
1336 if (cfa->base == CFI_SP)
1337 cfa->offset -= op->src.offset;
1338 break;
1339 }
1340
1341 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1342
1343 /* lea disp(%rbp), %rsp */
1344 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1345 break;
1346 }
1347
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001348 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001349
1350 /* drap: lea disp(%rsp), %drap */
1351 state->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001352
1353 /*
1354 * lea disp(%rsp), %reg
1355 *
1356 * This is needed for the rare case where GCC
1357 * does something dumb like:
1358 *
1359 * lea 0x8(%rsp), %rcx
1360 * ...
1361 * mov %rcx, %rsp
1362 */
1363 state->vals[op->dest.reg].base = CFI_CFA;
1364 state->vals[op->dest.reg].offset = \
1365 -state->stack_size + op->src.offset;
1366
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001367 break;
1368 }
1369
1370 if (state->drap && op->dest.reg == CFI_SP &&
1371 op->src.reg == state->drap_reg) {
1372
1373 /* drap: lea disp(%drap), %rsp */
1374 cfa->base = CFI_SP;
1375 cfa->offset = state->stack_size = -op->src.offset;
1376 state->drap_reg = CFI_UNDEFINED;
1377 state->drap = false;
1378 break;
1379 }
1380
1381 if (op->dest.reg == state->cfa.base) {
1382 WARN_FUNC("unsupported stack register modification",
1383 insn->sec, insn->offset);
1384 return -1;
1385 }
1386
1387 break;
1388
1389 case OP_SRC_AND:
1390 if (op->dest.reg != CFI_SP ||
1391 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1392 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1393 WARN_FUNC("unsupported stack pointer realignment",
1394 insn->sec, insn->offset);
1395 return -1;
1396 }
1397
1398 if (state->drap_reg != CFI_UNDEFINED) {
1399 /* drap: and imm, %rsp */
1400 cfa->base = state->drap_reg;
1401 cfa->offset = state->stack_size = 0;
1402 state->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001403 }
1404
1405 /*
1406 * Older versions of GCC (4.8ish) realign the stack
1407 * without DRAP, with a frame pointer.
1408 */
1409
1410 break;
1411
1412 case OP_SRC_POP:
1413 if (!state->drap && op->dest.type == OP_DEST_REG &&
1414 op->dest.reg == cfa->base) {
1415
1416 /* pop %rbp */
1417 cfa->base = CFI_SP;
1418 }
1419
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001420 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1421 op->dest.type == OP_DEST_REG &&
1422 op->dest.reg == state->drap_reg &&
1423 state->drap_offset == -state->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001424
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001425 /* drap: pop %drap */
1426 cfa->base = state->drap_reg;
1427 cfa->offset = 0;
1428 state->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001429
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001430 } else if (regs[op->dest.reg].offset == -state->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001431
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001432 /* pop %reg */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001433 restore_reg(state, op->dest.reg);
1434 }
1435
1436 state->stack_size -= 8;
1437 if (cfa->base == CFI_SP)
1438 cfa->offset -= 8;
1439
1440 break;
1441
1442 case OP_SRC_REG_INDIRECT:
1443 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001444 op->src.offset == state->drap_offset) {
1445
1446 /* drap: mov disp(%rbp), %drap */
1447 cfa->base = state->drap_reg;
1448 cfa->offset = 0;
1449 state->drap_offset = -1;
1450 }
1451
1452 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001453 op->src.offset == regs[op->dest.reg].offset) {
1454
1455 /* drap: mov disp(%rbp), %reg */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001456 restore_reg(state, op->dest.reg);
1457
1458 } else if (op->src.reg == cfa->base &&
1459 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1460
1461 /* mov disp(%rbp), %reg */
1462 /* mov disp(%rsp), %reg */
1463 restore_reg(state, op->dest.reg);
1464 }
1465
1466 break;
1467
1468 default:
1469 WARN_FUNC("unknown stack-related instruction",
1470 insn->sec, insn->offset);
1471 return -1;
1472 }
1473
1474 break;
1475
1476 case OP_DEST_PUSH:
1477 state->stack_size += 8;
1478 if (cfa->base == CFI_SP)
1479 cfa->offset += 8;
1480
1481 if (op->src.type != OP_SRC_REG)
1482 break;
1483
1484 if (state->drap) {
1485 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1486
1487 /* drap: push %drap */
1488 cfa->base = CFI_BP_INDIRECT;
1489 cfa->offset = -state->stack_size;
1490
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001491 /* save drap so we know when to restore it */
1492 state->drap_offset = -state->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001493
1494 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1495
1496 /* drap: push %rbp */
1497 state->stack_size = 0;
1498
1499 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1500
1501 /* drap: push %reg */
1502 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1503 }
1504
1505 } else {
1506
1507 /* push %reg */
1508 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1509 }
1510
1511 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001512 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001513 cfa->base != CFI_BP)
1514 state->bp_scratch = true;
1515 break;
1516
1517 case OP_DEST_REG_INDIRECT:
1518
1519 if (state->drap) {
1520 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1521
1522 /* drap: mov %drap, disp(%rbp) */
1523 cfa->base = CFI_BP_INDIRECT;
1524 cfa->offset = op->dest.offset;
1525
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001526 /* save drap offset so we know when to restore it */
1527 state->drap_offset = op->dest.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001528 }
1529
1530 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1531
1532 /* drap: mov reg, disp(%rbp) */
1533 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1534 }
1535
1536 } else if (op->dest.reg == cfa->base) {
1537
1538 /* mov reg, disp(%rbp) */
1539 /* mov reg, disp(%rsp) */
1540 save_reg(state, op->src.reg, CFI_CFA,
1541 op->dest.offset - state->cfa.offset);
1542 }
1543
1544 break;
1545
1546 case OP_DEST_LEAVE:
1547 if ((!state->drap && cfa->base != CFI_BP) ||
1548 (state->drap && cfa->base != state->drap_reg)) {
1549 WARN_FUNC("leave instruction with modified stack frame",
1550 insn->sec, insn->offset);
1551 return -1;
1552 }
1553
1554 /* leave (mov %rbp, %rsp; pop %rbp) */
1555
1556 state->stack_size = -state->regs[CFI_BP].offset - 8;
1557 restore_reg(state, CFI_BP);
1558
1559 if (!state->drap) {
1560 cfa->base = CFI_SP;
1561 cfa->offset -= 8;
1562 }
1563
1564 break;
1565
1566 case OP_DEST_MEM:
1567 if (op->src.type != OP_SRC_POP) {
1568 WARN_FUNC("unknown stack-related memory operation",
1569 insn->sec, insn->offset);
1570 return -1;
1571 }
1572
1573 /* pop mem */
1574 state->stack_size -= 8;
1575 if (cfa->base == CFI_SP)
1576 cfa->offset -= 8;
1577
1578 break;
1579
1580 default:
1581 WARN_FUNC("unknown stack-related instruction",
1582 insn->sec, insn->offset);
1583 return -1;
1584 }
1585
1586 return 0;
1587}
1588
1589static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1590{
1591 struct insn_state *state1 = &insn->state, *state2 = state;
1592 int i;
1593
1594 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1595 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1596 insn->sec, insn->offset,
1597 state1->cfa.base, state1->cfa.offset,
1598 state2->cfa.base, state2->cfa.offset);
1599
1600 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1601 for (i = 0; i < CFI_NUM_REGS; i++) {
1602 if (!memcmp(&state1->regs[i], &state2->regs[i],
1603 sizeof(struct cfi_reg)))
1604 continue;
1605
1606 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1607 insn->sec, insn->offset,
1608 i, state1->regs[i].base, state1->regs[i].offset,
1609 i, state2->regs[i].base, state2->regs[i].offset);
1610 break;
1611 }
1612
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001613 } else if (state1->type != state2->type) {
1614 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1615 insn->sec, insn->offset, state1->type, state2->type);
1616
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001617 } else if (state1->drap != state2->drap ||
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001618 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1619 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1620 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001621 insn->sec, insn->offset,
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001622 state1->drap, state1->drap_reg, state1->drap_offset,
1623 state2->drap, state2->drap_reg, state2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001624
1625 } else
1626 return true;
1627
1628 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001629}
1630
1631/*
1632 * Follow the branch starting at the given instruction, and recursively follow
1633 * any other branches (jumps). Meanwhile, track the frame pointer state at
1634 * each instruction and validate all the rules described in
1635 * tools/objtool/Documentation/stack-validation.txt.
1636 */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001637static int validate_branch(struct objtool_file *file, struct instruction *first,
1638 struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001639{
1640 struct alternative *alt;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001641 struct instruction *insn, *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001642 struct section *sec;
1643 struct symbol *func = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001644 int ret;
1645
1646 insn = first;
1647 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001648
1649 if (insn->alt_group && list_empty(&insn->alts)) {
1650 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1651 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001652 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001653 }
1654
1655 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001656 next_insn = next_insn_same_sec(file, insn);
1657
Josh Poimboeufee976382017-08-11 12:24:15 -05001658
1659 if (file->c_file && func && insn->func && func != insn->func) {
1660 WARN("%s() falls through to next function %s()",
1661 func->name, insn->func->name);
1662 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001663 }
1664
Josh Poimboeufee976382017-08-11 12:24:15 -05001665 if (insn->func)
1666 func = insn->func;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001667
Josh Poimboeuf48550222017-07-07 09:19:42 -05001668 if (func && insn->ignore) {
1669 WARN_FUNC("BUG: why am I validating an ignored function?",
1670 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001671 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001672 }
1673
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001674 if (insn->visited) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001675 if (!insn->hint && !insn_state_match(insn, &state))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001676 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001677
1678 return 0;
1679 }
1680
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001681 if (insn->hint) {
1682 if (insn->restore) {
1683 struct instruction *save_insn, *i;
1684
1685 i = insn;
1686 save_insn = NULL;
1687 func_for_each_insn_continue_reverse(file, func, i) {
1688 if (i->save) {
1689 save_insn = i;
1690 break;
1691 }
1692 }
1693
1694 if (!save_insn) {
1695 WARN_FUNC("no corresponding CFI save for CFI restore",
1696 sec, insn->offset);
1697 return 1;
1698 }
1699
1700 if (!save_insn->visited) {
1701 /*
1702 * Oops, no state to copy yet.
1703 * Hopefully we can reach this
1704 * instruction from another branch
1705 * after the save insn has been
1706 * visited.
1707 */
1708 if (insn == first)
1709 return 0;
1710
1711 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1712 sec, insn->offset);
1713 return 1;
1714 }
1715
1716 insn->state = save_insn->state;
1717 }
1718
1719 state = insn->state;
1720
1721 } else
1722 insn->state = state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001723
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001724 insn->visited = true;
1725
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001726 if (!insn->ignore_alts) {
1727 list_for_each_entry(alt, &insn->alts, list) {
1728 ret = validate_branch(file, alt->insn, state);
1729 if (ret)
1730 return 1;
1731 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001732 }
1733
1734 switch (insn->type) {
1735
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001736 case INSN_RETURN:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001737 if (func && has_modified_stack_frame(&state)) {
1738 WARN_FUNC("return with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001739 sec, insn->offset);
1740 return 1;
1741 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001742
1743 if (state.bp_scratch) {
1744 WARN("%s uses BP as a scratch register",
1745 insn->func->name);
1746 return 1;
1747 }
1748
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001749 return 0;
1750
1751 case INSN_CALL:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001752 if (is_fentry_call(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001753 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001754
1755 ret = dead_end_function(file, insn->call_dest);
1756 if (ret == 1)
1757 return 0;
1758 if (ret == -1)
1759 return 1;
1760
1761 /* fallthrough */
1762 case INSN_CALL_DYNAMIC:
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001763 if (!no_fp && func && !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001764 WARN_FUNC("call without frame pointer save/setup",
1765 sec, insn->offset);
1766 return 1;
1767 }
1768 break;
1769
1770 case INSN_JUMP_CONDITIONAL:
1771 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf48550222017-07-07 09:19:42 -05001772 if (insn->jump_dest &&
1773 (!func || !insn->jump_dest->func ||
1774 func == insn->jump_dest->func)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001775 ret = validate_branch(file, insn->jump_dest,
1776 state);
1777 if (ret)
1778 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001779
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001780 } else if (func && has_modified_stack_frame(&state)) {
1781 WARN_FUNC("sibling call from callable instruction with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001782 sec, insn->offset);
1783 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001784 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001785
1786 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1787 return 0;
1788
1789 break;
1790
1791 case INSN_JUMP_DYNAMIC:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001792 if (func && list_empty(&insn->alts) &&
1793 has_modified_stack_frame(&state)) {
1794 WARN_FUNC("sibling call from callable instruction with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001795 sec, insn->offset);
1796 return 1;
1797 }
1798
1799 return 0;
1800
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001801 case INSN_CONTEXT_SWITCH:
1802 if (func && (!next_insn || !next_insn->hint)) {
1803 WARN_FUNC("unsupported instruction in callable function",
1804 sec, insn->offset);
1805 return 1;
1806 }
1807 return 0;
1808
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001809 case INSN_STACK:
1810 if (update_insn_state(insn, &state))
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001811 return 1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001812
1813 break;
1814
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001815 default:
1816 break;
1817 }
1818
1819 if (insn->dead_end)
1820 return 0;
1821
Josh Poimboeuf00d96182017-09-18 21:43:30 -05001822 if (!next_insn) {
1823 if (state.cfa.base == CFI_UNDEFINED)
1824 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001825 WARN("%s: unexpected end of section", sec->name);
1826 return 1;
1827 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05001828
1829 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001830 }
1831
1832 return 0;
1833}
1834
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001835static int validate_unwind_hints(struct objtool_file *file)
1836{
1837 struct instruction *insn;
1838 int ret, warnings = 0;
1839 struct insn_state state;
1840
1841 if (!file->hints)
1842 return 0;
1843
1844 clear_insn_state(&state);
1845
1846 for_each_insn(file, insn) {
1847 if (insn->hint && !insn->visited) {
1848 ret = validate_branch(file, insn, state);
1849 warnings += ret;
1850 }
1851 }
1852
1853 return warnings;
1854}
1855
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001856static bool is_kasan_insn(struct instruction *insn)
1857{
1858 return (insn->type == INSN_CALL &&
1859 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1860}
1861
1862static bool is_ubsan_insn(struct instruction *insn)
1863{
1864 return (insn->type == INSN_CALL &&
1865 !strcmp(insn->call_dest->name,
1866 "__ubsan_handle_builtin_unreachable"));
1867}
1868
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001869static bool ignore_unreachable_insn(struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001870{
1871 int i;
1872
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001873 if (insn->ignore || insn->type == INSN_NOP)
1874 return true;
1875
1876 /*
1877 * Ignore any unused exceptions. This can happen when a whitelisted
1878 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05001879 *
1880 * Also ignore alternative replacement instructions. This can happen
1881 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001882 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05001883 if (!strcmp(insn->sec->name, ".fixup") ||
1884 !strcmp(insn->sec->name, ".altinstr_replacement") ||
1885 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001886 return true;
1887
1888 /*
1889 * Check if this (or a subsequent) instruction is related to
1890 * CONFIG_UBSAN or CONFIG_KASAN.
1891 *
1892 * End the search at 5 instructions to avoid going into the weeds.
1893 */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001894 if (!insn->func)
1895 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001896 for (i = 0; i < 5; i++) {
1897
1898 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
1899 return true;
1900
1901 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
1902 insn = insn->jump_dest;
1903 continue;
1904 }
1905
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001906 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001907 break;
1908 insn = list_next_entry(insn, list);
1909 }
1910
1911 return false;
1912}
1913
1914static int validate_functions(struct objtool_file *file)
1915{
1916 struct section *sec;
1917 struct symbol *func;
1918 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001919 struct insn_state state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001920 int ret, warnings = 0;
1921
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001922 clear_insn_state(&state);
1923
1924 state.cfa = initial_func_cfi.cfa;
1925 memcpy(&state.regs, &initial_func_cfi.regs,
1926 CFI_NUM_REGS * sizeof(struct cfi_reg));
1927 state.stack_size = initial_func_cfi.cfa.offset;
1928
1929 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001930 list_for_each_entry(func, &sec->symbol_list, list) {
1931 if (func->type != STT_FUNC)
1932 continue;
1933
1934 insn = find_insn(file, sec, func->offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001935 if (!insn || insn->ignore)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001936 continue;
1937
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001938 ret = validate_branch(file, insn, state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001939 warnings += ret;
1940 }
1941 }
1942
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001943 return warnings;
1944}
1945
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001946static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001947{
1948 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001949
1950 if (file->ignore_unreachables)
1951 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001952
1953 for_each_insn(file, insn) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001954 if (insn->visited || ignore_unreachable_insn(insn))
1955 continue;
1956
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001957 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
1958 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001959 }
1960
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001961 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001962}
1963
1964static void cleanup(struct objtool_file *file)
1965{
1966 struct instruction *insn, *tmpinsn;
1967 struct alternative *alt, *tmpalt;
1968
1969 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
1970 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
1971 list_del(&alt->list);
1972 free(alt);
1973 }
1974 list_del(&insn->list);
1975 hash_del(&insn->hash);
1976 free(insn);
1977 }
1978 elf_close(file->elf);
1979}
1980
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001981int check(const char *_objname, bool _no_fp, bool no_unreachable, bool orc)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001982{
1983 struct objtool_file file;
1984 int ret, warnings = 0;
1985
1986 objname = _objname;
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001987 no_fp = _no_fp;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001988
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001989 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001990 if (!file.elf)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001991 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001992
1993 INIT_LIST_HEAD(&file.insn_list);
1994 hash_init(file.insn_hash);
1995 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
1996 file.rodata = find_section_by_name(file.elf, ".rodata");
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001997 file.c_file = find_section_by_name(file.elf, ".comment");
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001998 file.ignore_unreachables = no_unreachable;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001999 file.hints = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002000
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002001 arch_initial_func_cfi_state(&initial_func_cfi);
2002
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002003 ret = decode_sections(&file);
2004 if (ret < 0)
2005 goto out;
2006 warnings += ret;
2007
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002008 if (list_empty(&file.insn_list))
2009 goto out;
2010
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002011 ret = validate_functions(&file);
2012 if (ret < 0)
2013 goto out;
2014 warnings += ret;
2015
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002016 ret = validate_unwind_hints(&file);
2017 if (ret < 0)
2018 goto out;
2019 warnings += ret;
2020
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002021 if (!warnings) {
2022 ret = validate_reachable_instructions(&file);
2023 if (ret < 0)
2024 goto out;
2025 warnings += ret;
2026 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002027
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002028 if (orc) {
2029 ret = create_orc(&file);
2030 if (ret < 0)
2031 goto out;
2032
2033 ret = create_orc_sections(&file);
2034 if (ret < 0)
2035 goto out;
2036
2037 ret = elf_write(file.elf);
2038 if (ret < 0)
2039 goto out;
2040 }
2041
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002042out:
2043 cleanup(&file);
2044
2045 /* ignore warnings for now until we get all the code cleaned up */
2046 if (ret || warnings)
2047 return 0;
2048 return 0;
2049}