blob: 38b0517dc49efa3a0cd5dfaea5d3cdf0d7550355 [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;
Peter Zijlstra764eef42019-03-01 11:19:03 +010034 bool skip_orig;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050035};
36
37const char *objname;
Josh Poimboeufbaa41462017-06-28 10:11:07 -050038struct cfi_state initial_func_cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050039
Josh Poimboeuf627fce12017-07-11 10:33:42 -050040struct instruction *find_insn(struct objtool_file *file,
41 struct section *sec, unsigned long offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050042{
43 struct instruction *insn;
44
45 hash_for_each_possible(file->insn_hash, insn, hash, offset)
46 if (insn->sec == sec && insn->offset == offset)
47 return insn;
48
49 return NULL;
50}
51
52static struct instruction *next_insn_same_sec(struct objtool_file *file,
53 struct instruction *insn)
54{
55 struct instruction *next = list_next_entry(insn, list);
56
Josh Poimboeufbaa41462017-06-28 10:11:07 -050057 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050058 return NULL;
59
60 return next;
61}
62
Josh Poimboeuf13810432018-05-09 22:39:15 -050063static struct instruction *next_insn_same_func(struct objtool_file *file,
64 struct instruction *insn)
65{
66 struct instruction *next = list_next_entry(insn, list);
67 struct symbol *func = insn->func;
68
69 if (!func)
70 return NULL;
71
72 if (&next->list != &file->insn_list && next->func == func)
73 return next;
74
75 /* Check if we're already in the subfunction: */
76 if (func == func->cfunc)
77 return NULL;
78
79 /* Move to the subfunction: */
80 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
81}
82
83#define func_for_each_insn_all(file, func, insn) \
84 for (insn = find_insn(file, func->sec, func->offset); \
85 insn; \
86 insn = next_insn_same_func(file, insn))
87
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050088#define func_for_each_insn(file, func, insn) \
89 for (insn = find_insn(file, func->sec, func->offset); \
90 insn && &insn->list != &file->insn_list && \
91 insn->sec == func->sec && \
92 insn->offset < func->offset + func->len; \
93 insn = list_next_entry(insn, list))
94
95#define func_for_each_insn_continue_reverse(file, func, insn) \
96 for (insn = list_prev_entry(insn, list); \
97 &insn->list != &file->insn_list && \
98 insn->sec == func->sec && insn->offset >= func->offset; \
99 insn = list_prev_entry(insn, list))
100
101#define sec_for_each_insn_from(file, insn) \
102 for (; insn; insn = next_insn_same_sec(file, insn))
103
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500104#define sec_for_each_insn_continue(file, insn) \
105 for (insn = next_insn_same_sec(file, insn); insn; \
106 insn = next_insn_same_sec(file, insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500107
108/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500109 * This checks to see if the given function is a "noreturn" function.
110 *
111 * For global functions which are outside the scope of this object file, we
112 * have to keep a manual list of them.
113 *
114 * For local functions, we have to detect them manually by simply looking for
115 * the lack of a return instruction.
116 *
117 * Returns:
118 * -1: error
119 * 0: no dead end
120 * 1: dead end
121 */
122static int __dead_end_function(struct objtool_file *file, struct symbol *func,
123 int recursion)
124{
125 int i;
126 struct instruction *insn;
127 bool empty = true;
128
129 /*
130 * Unfortunately these have to be hard coded because the noreturn
131 * attribute isn't provided in ELF data.
132 */
133 static const char * const global_noreturns[] = {
134 "__stack_chk_fail",
135 "panic",
136 "do_exit",
137 "do_task_dead",
138 "__module_put_and_exit",
139 "complete_and_exit",
140 "kvm_spurious_fault",
141 "__reiserfs_panic",
142 "lbug_with_loc",
143 "fortify_panic",
Kees Cookb394d462018-01-10 14:22:38 -0800144 "usercopy_abort",
Josh Poimboeuf684fb242018-06-19 10:47:50 -0500145 "machine_real_restart",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500146 };
147
148 if (func->bind == STB_WEAK)
149 return 0;
150
151 if (func->bind == STB_GLOBAL)
152 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
153 if (!strcmp(func->name, global_noreturns[i]))
154 return 1;
155
Josh Poimboeuf13810432018-05-09 22:39:15 -0500156 if (!func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500157 return 0;
158
Josh Poimboeuf13810432018-05-09 22:39:15 -0500159 insn = find_insn(file, func->sec, func->offset);
160 if (!insn->func)
161 return 0;
162
163 func_for_each_insn_all(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500164 empty = false;
165
166 if (insn->type == INSN_RETURN)
167 return 0;
168 }
169
170 if (empty)
171 return 0;
172
173 /*
174 * A function can have a sibling call instead of a return. In that
175 * case, the function's dead-end status depends on whether the target
176 * of the sibling call returns.
177 */
Josh Poimboeuf13810432018-05-09 22:39:15 -0500178 func_for_each_insn_all(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500179 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
180 struct instruction *dest = insn->jump_dest;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500181
182 if (!dest)
183 /* sibling call to another file */
184 return 0;
185
Josh Poimboeuf13810432018-05-09 22:39:15 -0500186 if (dest->func && dest->func->pfunc != insn->func->pfunc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500187
Josh Poimboeuf13810432018-05-09 22:39:15 -0500188 /* local sibling call */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500189 if (recursion == 5) {
Josh Poimboeuf0afd0d92018-05-09 22:39:14 -0500190 /*
191 * Infinite recursion: two functions
192 * have sibling calls to each other.
193 * This is a very rare case. It means
194 * they aren't dead ends.
195 */
196 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500197 }
198
Josh Poimboeuf13810432018-05-09 22:39:15 -0500199 return __dead_end_function(file, dest->func,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500200 recursion + 1);
201 }
202 }
203
204 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
205 /* sibling call */
206 return 0;
207 }
208
209 return 1;
210}
211
212static int dead_end_function(struct objtool_file *file, struct symbol *func)
213{
214 return __dead_end_function(file, func, 0);
215}
216
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500217static void clear_insn_state(struct insn_state *state)
218{
219 int i;
220
221 memset(state, 0, sizeof(*state));
222 state->cfa.base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500223 for (i = 0; i < CFI_NUM_REGS; i++) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500224 state->regs[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500225 state->vals[i].base = CFI_UNDEFINED;
226 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500227 state->drap_reg = CFI_UNDEFINED;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -0500228 state->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500229}
230
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500231/*
232 * Call the arch-specific instruction decoder for all the instructions and add
233 * them to the global instruction list.
234 */
235static int decode_instructions(struct objtool_file *file)
236{
237 struct section *sec;
238 struct symbol *func;
239 unsigned long offset;
240 struct instruction *insn;
241 int ret;
242
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500243 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500244
245 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
246 continue;
247
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500248 if (strcmp(sec->name, ".altinstr_replacement") &&
249 strcmp(sec->name, ".altinstr_aux") &&
250 strncmp(sec->name, ".discard.", 9))
251 sec->text = true;
252
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500253 for (offset = 0; offset < sec->len; offset += insn->len) {
254 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500255 if (!insn) {
256 WARN("malloc failed");
257 return -1;
258 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500259 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500260 INIT_LIST_HEAD(&insn->alts);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500261 clear_insn_state(&insn->state);
262
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500263 insn->sec = sec;
264 insn->offset = offset;
265
266 ret = arch_decode_instruction(file->elf, sec, offset,
267 sec->len - offset,
268 &insn->len, &insn->type,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500269 &insn->immediate,
270 &insn->stack_op);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500271 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500272 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500273
274 if (!insn->type || insn->type > INSN_LAST) {
275 WARN_FUNC("invalid instruction type %d",
276 insn->sec, insn->offset, insn->type);
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500277 ret = -1;
278 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500279 }
280
281 hash_add(file->insn_hash, &insn->hash, insn->offset);
282 list_add_tail(&insn->list, &file->insn_list);
283 }
284
285 list_for_each_entry(func, &sec->symbol_list, list) {
286 if (func->type != STT_FUNC)
287 continue;
288
289 if (!find_insn(file, sec, func->offset)) {
290 WARN("%s(): can't find starting instruction",
291 func->name);
292 return -1;
293 }
294
295 func_for_each_insn(file, func, insn)
296 if (!insn->func)
297 insn->func = func;
298 }
299 }
300
301 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500302
303err:
304 free(insn);
305 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500306}
307
308/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500309 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500310 */
311static int add_dead_ends(struct objtool_file *file)
312{
313 struct section *sec;
314 struct rela *rela;
315 struct instruction *insn;
316 bool found;
317
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500318 /*
319 * By default, "ud2" is a dead end unless otherwise annotated, because
320 * GCC 7 inserts it for certain divide-by-zero cases.
321 */
322 for_each_insn(file, insn)
323 if (insn->type == INSN_BUG)
324 insn->dead_end = true;
325
326 /*
327 * Check for manually annotated dead ends.
328 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500329 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
330 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500331 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500332
333 list_for_each_entry(rela, &sec->rela_list, list) {
334 if (rela->sym->type != STT_SECTION) {
335 WARN("unexpected relocation symbol type in %s", sec->name);
336 return -1;
337 }
338 insn = find_insn(file, rela->sym->sec, rela->addend);
339 if (insn)
340 insn = list_prev_entry(insn, list);
341 else if (rela->addend == rela->sym->sec->len) {
342 found = false;
343 list_for_each_entry_reverse(insn, &file->insn_list, list) {
344 if (insn->sec == rela->sym->sec) {
345 found = true;
346 break;
347 }
348 }
349
350 if (!found) {
351 WARN("can't find unreachable insn at %s+0x%x",
352 rela->sym->sec->name, rela->addend);
353 return -1;
354 }
355 } else {
356 WARN("can't find unreachable insn at %s+0x%x",
357 rela->sym->sec->name, rela->addend);
358 return -1;
359 }
360
361 insn->dead_end = true;
362 }
363
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500364reachable:
365 /*
366 * These manually annotated reachable checks are needed for GCC 4.4,
367 * where the Linux unreachable() macro isn't supported. In that case
368 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
369 * not a dead end.
370 */
371 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
372 if (!sec)
373 return 0;
374
375 list_for_each_entry(rela, &sec->rela_list, list) {
376 if (rela->sym->type != STT_SECTION) {
377 WARN("unexpected relocation symbol type in %s", sec->name);
378 return -1;
379 }
380 insn = find_insn(file, rela->sym->sec, rela->addend);
381 if (insn)
382 insn = list_prev_entry(insn, list);
383 else if (rela->addend == rela->sym->sec->len) {
384 found = false;
385 list_for_each_entry_reverse(insn, &file->insn_list, list) {
386 if (insn->sec == rela->sym->sec) {
387 found = true;
388 break;
389 }
390 }
391
392 if (!found) {
393 WARN("can't find reachable insn at %s+0x%x",
394 rela->sym->sec->name, rela->addend);
395 return -1;
396 }
397 } else {
398 WARN("can't find reachable insn at %s+0x%x",
399 rela->sym->sec->name, rela->addend);
400 return -1;
401 }
402
403 insn->dead_end = false;
404 }
405
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500406 return 0;
407}
408
409/*
410 * Warnings shouldn't be reported for ignored functions.
411 */
412static void add_ignores(struct objtool_file *file)
413{
414 struct instruction *insn;
415 struct section *sec;
416 struct symbol *func;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100417 struct rela *rela;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500418
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100419 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
420 if (!sec)
421 return;
422
423 list_for_each_entry(rela, &sec->rela_list, list) {
424 switch (rela->sym->type) {
425 case STT_FUNC:
426 func = rela->sym;
427 break;
428
429 case STT_SECTION:
430 func = find_symbol_by_offset(rela->sym->sec, rela->addend);
431 if (!func || func->type != STT_FUNC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500432 continue;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100433 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500434
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100435 default:
436 WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type);
437 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500438 }
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100439
440 func_for_each_insn_all(file, func, insn)
441 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500442 }
443}
444
445/*
Peter Zijlstraea242132019-02-25 12:50:09 +0100446 * This is a whitelist of functions that is allowed to be called with AC set.
447 * The list is meant to be minimal and only contains compiler instrumentation
448 * ABI and a few functions used to implement *_{to,from}_user() functions.
449 *
450 * These functions must not directly change AC, but may PUSHF/POPF.
451 */
452static const char *uaccess_safe_builtin[] = {
453 /* KASAN */
454 "kasan_report",
455 "check_memory_region",
456 /* KASAN out-of-line */
457 "__asan_loadN_noabort",
458 "__asan_load1_noabort",
459 "__asan_load2_noabort",
460 "__asan_load4_noabort",
461 "__asan_load8_noabort",
462 "__asan_load16_noabort",
463 "__asan_storeN_noabort",
464 "__asan_store1_noabort",
465 "__asan_store2_noabort",
466 "__asan_store4_noabort",
467 "__asan_store8_noabort",
468 "__asan_store16_noabort",
469 /* KASAN in-line */
470 "__asan_report_load_n_noabort",
471 "__asan_report_load1_noabort",
472 "__asan_report_load2_noabort",
473 "__asan_report_load4_noabort",
474 "__asan_report_load8_noabort",
475 "__asan_report_load16_noabort",
476 "__asan_report_store_n_noabort",
477 "__asan_report_store1_noabort",
478 "__asan_report_store2_noabort",
479 "__asan_report_store4_noabort",
480 "__asan_report_store8_noabort",
481 "__asan_report_store16_noabort",
482 /* KCOV */
483 "write_comp_data",
484 "__sanitizer_cov_trace_pc",
485 "__sanitizer_cov_trace_const_cmp1",
486 "__sanitizer_cov_trace_const_cmp2",
487 "__sanitizer_cov_trace_const_cmp4",
488 "__sanitizer_cov_trace_const_cmp8",
489 "__sanitizer_cov_trace_cmp1",
490 "__sanitizer_cov_trace_cmp2",
491 "__sanitizer_cov_trace_cmp4",
492 "__sanitizer_cov_trace_cmp8",
493 /* UBSAN */
494 "ubsan_type_mismatch_common",
495 "__ubsan_handle_type_mismatch",
496 "__ubsan_handle_type_mismatch_v1",
497 /* misc */
498 "csum_partial_copy_generic",
499 "__memcpy_mcsafe",
500 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
501 NULL
502};
503
504static void add_uaccess_safe(struct objtool_file *file)
505{
506 struct symbol *func;
507 const char **name;
508
509 if (!uaccess)
510 return;
511
512 for (name = uaccess_safe_builtin; *name; name++) {
513 func = find_symbol_by_name(file->elf, *name);
514 if (!func)
515 continue;
516
517 func->alias->uaccess_safe = true;
518 }
519}
520
521/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000522 * FIXME: For now, just ignore any alternatives which add retpolines. This is
523 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
524 * But it at least allows objtool to understand the control flow *around* the
525 * retpoline.
526 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100527static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000528{
529 struct section *sec;
530 struct rela *rela;
531 struct instruction *insn;
532
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100533 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000534 if (!sec)
535 return 0;
536
537 list_for_each_entry(rela, &sec->rela_list, list) {
538 if (rela->sym->type != STT_SECTION) {
539 WARN("unexpected relocation symbol type in %s", sec->name);
540 return -1;
541 }
542
543 insn = find_insn(file, rela->sym->sec, rela->addend);
544 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100545 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000546 return -1;
547 }
548
549 insn->ignore_alts = true;
550 }
551
552 return 0;
553}
554
555/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500556 * Find the destination instructions for all jumps.
557 */
558static int add_jump_destinations(struct objtool_file *file)
559{
560 struct instruction *insn;
561 struct rela *rela;
562 struct section *dest_sec;
563 unsigned long dest_off;
564
565 for_each_insn(file, insn) {
566 if (insn->type != INSN_JUMP_CONDITIONAL &&
567 insn->type != INSN_JUMP_UNCONDITIONAL)
568 continue;
569
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500570 if (insn->ignore)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500571 continue;
572
573 rela = find_rela_by_dest_range(insn->sec, insn->offset,
574 insn->len);
575 if (!rela) {
576 dest_sec = insn->sec;
577 dest_off = insn->offset + insn->len + insn->immediate;
578 } else if (rela->sym->type == STT_SECTION) {
579 dest_sec = rela->sym->sec;
580 dest_off = rela->addend + 4;
581 } else if (rela->sym->sec->idx) {
582 dest_sec = rela->sym->sec;
583 dest_off = rela->sym->sym.st_value + rela->addend + 4;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000584 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
585 /*
586 * Retpoline jumps are really dynamic jumps in
587 * disguise, so convert them accordingly.
588 */
589 insn->type = INSN_JUMP_DYNAMIC;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100590 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000591 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500592 } else {
593 /* sibling call */
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100594 insn->call_dest = rela->sym;
595 insn->jump_dest = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500596 continue;
597 }
598
599 insn->jump_dest = find_insn(file, dest_sec, dest_off);
600 if (!insn->jump_dest) {
601
602 /*
603 * This is a special case where an alt instruction
604 * jumps past the end of the section. These are
605 * handled later in handle_group_alt().
606 */
607 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
608 continue;
609
610 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
611 insn->sec, insn->offset, dest_sec->name,
612 dest_off);
613 return -1;
614 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500615
616 /*
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100617 * Cross-function jump.
Josh Poimboeufcd778492018-06-01 07:23:51 -0500618 */
619 if (insn->func && insn->jump_dest->func &&
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100620 insn->func != insn->jump_dest->func) {
621
622 /*
623 * For GCC 8+, create parent/child links for any cold
624 * subfunctions. This is _mostly_ redundant with a
625 * similar initialization in read_symbols().
626 *
627 * If a function has aliases, we want the *first* such
628 * function in the symbol table to be the subfunction's
629 * parent. In that case we overwrite the
630 * initialization done in read_symbols().
631 *
632 * However this code can't completely replace the
633 * read_symbols() code because this doesn't detect the
634 * case where the parent function's only reference to a
635 * subfunction is through a switch table.
636 */
637 if (!strstr(insn->func->name, ".cold.") &&
638 strstr(insn->jump_dest->func->name, ".cold.")) {
639 insn->func->cfunc = insn->jump_dest->func;
640 insn->jump_dest->func->pfunc = insn->func;
641
642 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
643 insn->jump_dest->offset == insn->jump_dest->func->offset) {
644
645 /* sibling class */
646 insn->call_dest = insn->jump_dest->func;
647 insn->jump_dest = NULL;
648 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500649 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500650 }
651
652 return 0;
653}
654
655/*
656 * Find the destination instructions for all calls.
657 */
658static int add_call_destinations(struct objtool_file *file)
659{
660 struct instruction *insn;
661 unsigned long dest_off;
662 struct rela *rela;
663
664 for_each_insn(file, insn) {
665 if (insn->type != INSN_CALL)
666 continue;
667
668 rela = find_rela_by_dest_range(insn->sec, insn->offset,
669 insn->len);
670 if (!rela) {
671 dest_off = insn->offset + insn->len + insn->immediate;
672 insn->call_dest = find_symbol_by_offset(insn->sec,
673 dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600674
675 if (!insn->call_dest && !insn->ignore) {
676 WARN_FUNC("unsupported intra-function call",
677 insn->sec, insn->offset);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100678 if (retpoline)
679 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 -0500680 return -1;
681 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600682
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500683 } else if (rela->sym->type == STT_SECTION) {
684 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
685 rela->addend+4);
686 if (!insn->call_dest ||
687 insn->call_dest->type != STT_FUNC) {
688 WARN_FUNC("can't find call dest symbol at %s+0x%x",
689 insn->sec, insn->offset,
690 rela->sym->sec->name,
691 rela->addend + 4);
692 return -1;
693 }
694 } else
695 insn->call_dest = rela->sym;
696 }
697
698 return 0;
699}
700
701/*
702 * The .alternatives section requires some extra special care, over and above
703 * what other special sections require:
704 *
705 * 1. Because alternatives are patched in-place, we need to insert a fake jump
706 * instruction at the end so that validate_branch() skips all the original
707 * replaced instructions when validating the new instruction path.
708 *
709 * 2. An added wrinkle is that the new instruction length might be zero. In
710 * that case the old instructions are replaced with noops. We simulate that
711 * by creating a fake jump as the only new instruction.
712 *
713 * 3. In some cases, the alternative section includes an instruction which
714 * conditionally jumps to the _end_ of the entry. We have to modify these
715 * jumps' destinations to point back to .text rather than the end of the
716 * entry in .altinstr_replacement.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500717 */
718static int handle_group_alt(struct objtool_file *file,
719 struct special_alt *special_alt,
720 struct instruction *orig_insn,
721 struct instruction **new_insn)
722{
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600723 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500724 unsigned long dest_off;
725
726 last_orig_insn = NULL;
727 insn = orig_insn;
728 sec_for_each_insn_from(file, insn) {
729 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
730 break;
731
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500732 insn->alt_group = true;
733 last_orig_insn = insn;
734 }
735
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600736 if (next_insn_same_sec(file, last_orig_insn)) {
737 fake_jump = malloc(sizeof(*fake_jump));
738 if (!fake_jump) {
739 WARN("malloc failed");
740 return -1;
741 }
742 memset(fake_jump, 0, sizeof(*fake_jump));
743 INIT_LIST_HEAD(&fake_jump->alts);
744 clear_insn_state(&fake_jump->state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500745
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600746 fake_jump->sec = special_alt->new_sec;
747 fake_jump->offset = -1;
748 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
749 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
750 fake_jump->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500751 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500752
753 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600754 if (!fake_jump) {
755 WARN("%s: empty alternative at end of section",
756 special_alt->orig_sec->name);
757 return -1;
758 }
759
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500760 *new_insn = fake_jump;
761 return 0;
762 }
763
764 last_new_insn = NULL;
765 insn = *new_insn;
766 sec_for_each_insn_from(file, insn) {
767 if (insn->offset >= special_alt->new_off + special_alt->new_len)
768 break;
769
770 last_new_insn = insn;
771
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600772 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +0100773 insn->func = orig_insn->func;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600774
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500775 if (insn->type != INSN_JUMP_CONDITIONAL &&
776 insn->type != INSN_JUMP_UNCONDITIONAL)
777 continue;
778
779 if (!insn->immediate)
780 continue;
781
782 dest_off = insn->offset + insn->len + insn->immediate;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600783 if (dest_off == special_alt->new_off + special_alt->new_len) {
784 if (!fake_jump) {
785 WARN("%s: alternative jump to end of section",
786 special_alt->orig_sec->name);
787 return -1;
788 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500789 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600790 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500791
792 if (!insn->jump_dest) {
793 WARN_FUNC("can't find alternative jump destination",
794 insn->sec, insn->offset);
795 return -1;
796 }
797 }
798
799 if (!last_new_insn) {
800 WARN_FUNC("can't find last new alternative instruction",
801 special_alt->new_sec, special_alt->new_off);
802 return -1;
803 }
804
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600805 if (fake_jump)
806 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500807
808 return 0;
809}
810
811/*
812 * A jump table entry can either convert a nop to a jump or a jump to a nop.
813 * If the original instruction is a jump, make the alt entry an effective nop
814 * by just skipping the original instruction.
815 */
816static int handle_jump_alt(struct objtool_file *file,
817 struct special_alt *special_alt,
818 struct instruction *orig_insn,
819 struct instruction **new_insn)
820{
821 if (orig_insn->type == INSN_NOP)
822 return 0;
823
824 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
825 WARN_FUNC("unsupported instruction at jump label",
826 orig_insn->sec, orig_insn->offset);
827 return -1;
828 }
829
830 *new_insn = list_next_entry(orig_insn, list);
831 return 0;
832}
833
834/*
835 * Read all the special sections which have alternate instructions which can be
836 * patched in or redirected to at runtime. Each instruction having alternate
837 * instruction(s) has them added to its insn->alts list, which will be
838 * traversed in validate_branch().
839 */
840static int add_special_section_alts(struct objtool_file *file)
841{
842 struct list_head special_alts;
843 struct instruction *orig_insn, *new_insn;
844 struct special_alt *special_alt, *tmp;
845 struct alternative *alt;
846 int ret;
847
848 ret = special_get_alts(file->elf, &special_alts);
849 if (ret)
850 return ret;
851
852 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500853
854 orig_insn = find_insn(file, special_alt->orig_sec,
855 special_alt->orig_off);
856 if (!orig_insn) {
857 WARN_FUNC("special: can't find orig instruction",
858 special_alt->orig_sec, special_alt->orig_off);
859 ret = -1;
860 goto out;
861 }
862
863 new_insn = NULL;
864 if (!special_alt->group || special_alt->new_len) {
865 new_insn = find_insn(file, special_alt->new_sec,
866 special_alt->new_off);
867 if (!new_insn) {
868 WARN_FUNC("special: can't find new instruction",
869 special_alt->new_sec,
870 special_alt->new_off);
871 ret = -1;
872 goto out;
873 }
874 }
875
876 if (special_alt->group) {
877 ret = handle_group_alt(file, special_alt, orig_insn,
878 &new_insn);
879 if (ret)
880 goto out;
881 } else if (special_alt->jump_or_nop) {
882 ret = handle_jump_alt(file, special_alt, orig_insn,
883 &new_insn);
884 if (ret)
885 goto out;
886 }
887
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000888 alt = malloc(sizeof(*alt));
889 if (!alt) {
890 WARN("malloc failed");
891 ret = -1;
892 goto out;
893 }
894
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500895 alt->insn = new_insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +0100896 alt->skip_orig = special_alt->skip_orig;
Peter Zijlstraea242132019-02-25 12:50:09 +0100897 orig_insn->ignore_alts |= special_alt->skip_alt;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500898 list_add_tail(&alt->list, &orig_insn->alts);
899
900 list_del(&special_alt->list);
901 free(special_alt);
902 }
903
904out:
905 return ret;
906}
907
Josh Poimboeuf13810432018-05-09 22:39:15 -0500908static int add_switch_table(struct objtool_file *file, struct instruction *insn,
909 struct rela *table, struct rela *next_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500910{
911 struct rela *rela = table;
912 struct instruction *alt_insn;
913 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500914 struct symbol *pfunc = insn->func->pfunc;
915 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500916
Allan Xavier4a60aa02018-09-07 08:12:01 -0500917 list_for_each_entry_from(rela, &table->rela_sec->rela_list, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500918 if (rela == next_table)
919 break;
920
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500921 /* Make sure the switch table entries are consecutive: */
922 if (prev_offset && rela->offset != prev_offset + 8)
923 break;
924
925 /* Detect function pointers from contiguous objects: */
926 if (rela->sym->sec == pfunc->sec &&
927 rela->addend == pfunc->offset)
928 break;
929
Josh Poimboeuf13810432018-05-09 22:39:15 -0500930 alt_insn = find_insn(file, rela->sym->sec, rela->addend);
931 if (!alt_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500932 break;
933
Josh Poimboeuf13810432018-05-09 22:39:15 -0500934 /* Make sure the jmp dest is in the function or subfunction: */
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500935 if (alt_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -0500936 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500937
938 alt = malloc(sizeof(*alt));
939 if (!alt) {
940 WARN("malloc failed");
941 return -1;
942 }
943
944 alt->insn = alt_insn;
945 list_add_tail(&alt->list, &insn->alts);
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500946 prev_offset = rela->offset;
947 }
948
949 if (!prev_offset) {
950 WARN_FUNC("can't find switch jump table",
951 insn->sec, insn->offset);
952 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500953 }
954
955 return 0;
956}
957
958/*
959 * find_switch_table() - Given a dynamic jump, find the switch jump table in
960 * .rodata associated with it.
961 *
962 * There are 3 basic patterns:
963 *
964 * 1. jmpq *[rodata addr](,%reg,8)
965 *
966 * This is the most common case by far. It jumps to an address in a simple
967 * jump table which is stored in .rodata.
968 *
969 * 2. jmpq *[rodata addr](%rip)
970 *
971 * This is caused by a rare GCC quirk, currently only seen in three driver
972 * functions in the kernel, only with certain obscure non-distro configs.
973 *
974 * As part of an optimization, GCC makes a copy of an existing switch jump
975 * table, modifies it, and then hard-codes the jump (albeit with an indirect
976 * jump) to use a single entry in the table. The rest of the jump table and
977 * some of its jump targets remain as dead code.
978 *
979 * In such a case we can just crudely ignore all unreachable instruction
980 * warnings for the entire object file. Ideally we would just ignore them
981 * for the function, but that would require redesigning the code quite a
982 * bit. And honestly that's just not worth doing: unreachable instruction
983 * warnings are of questionable value anyway, and this is such a rare issue.
984 *
985 * 3. mov [rodata addr],%reg1
986 * ... some instructions ...
987 * jmpq *(%reg1,%reg2,8)
988 *
989 * This is a fairly uncommon pattern which is new for GCC 6. As of this
990 * writing, there are 11 occurrences of it in the allmodconfig kernel.
991 *
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100992 * As of GCC 7 there are quite a few more of these and the 'in between' code
993 * is significant. Esp. with KASAN enabled some of the code between the mov
994 * and jmpq uses .rodata itself, which can confuse things.
995 *
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500996 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
997 * ensure the same register is used in the mov and jump instructions.
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100998 *
999 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001000 */
1001static struct rela *find_switch_table(struct objtool_file *file,
1002 struct symbol *func,
1003 struct instruction *insn)
1004{
1005 struct rela *text_rela, *rodata_rela;
1006 struct instruction *orig_insn = insn;
Allan Xavier4a60aa02018-09-07 08:12:01 -05001007 struct section *rodata_sec;
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -05001008 unsigned long table_offset;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001009
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001010 /*
1011 * Backward search using the @first_jump_src links, these help avoid
1012 * much of the 'in between' code. Which avoids us getting confused by
1013 * it.
1014 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001015 for (;
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001016 &insn->list != &file->insn_list &&
1017 insn->sec == func->sec &&
1018 insn->offset >= func->offset;
1019
1020 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
1021
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001022 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001023 break;
1024
1025 /* allow small jumps within the range */
1026 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1027 insn->jump_dest &&
1028 (insn->jump_dest->offset <= insn->offset ||
1029 insn->jump_dest->offset > orig_insn->offset))
1030 break;
1031
1032 /* look for a relocation which references .rodata */
1033 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
1034 insn->len);
Allan Xavier4a60aa02018-09-07 08:12:01 -05001035 if (!text_rela || text_rela->sym->type != STT_SECTION ||
1036 !text_rela->sym->sec->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001037 continue;
1038
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -05001039 table_offset = text_rela->addend;
Allan Xavier4a60aa02018-09-07 08:12:01 -05001040 rodata_sec = text_rela->sym->sec;
1041
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -05001042 if (text_rela->type == R_X86_64_PC32)
1043 table_offset += 4;
1044
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001045 /*
1046 * Make sure the .rodata address isn't associated with a
1047 * symbol. gcc jump tables are anonymous data.
1048 */
Allan Xavier4a60aa02018-09-07 08:12:01 -05001049 if (find_symbol_containing(rodata_sec, table_offset))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001050 continue;
1051
Allan Xavier4a60aa02018-09-07 08:12:01 -05001052 rodata_rela = find_rela_by_dest(rodata_sec, table_offset);
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001053 if (rodata_rela) {
1054 /*
1055 * Use of RIP-relative switch jumps is quite rare, and
1056 * indicates a rare GCC quirk/bug which can leave dead
1057 * code behind.
1058 */
1059 if (text_rela->type == R_X86_64_PC32)
1060 file->ignore_unreachables = true;
1061
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -05001062 return rodata_rela;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001063 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001064 }
1065
1066 return NULL;
1067}
1068
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001069
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001070static int add_func_switch_tables(struct objtool_file *file,
1071 struct symbol *func)
1072{
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001073 struct instruction *insn, *last = NULL, *prev_jump = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001074 struct rela *rela, *prev_rela = NULL;
1075 int ret;
1076
Josh Poimboeuf13810432018-05-09 22:39:15 -05001077 func_for_each_insn_all(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001078 if (!last)
1079 last = insn;
1080
1081 /*
1082 * Store back-pointers for unconditional forward jumps such
1083 * that find_switch_table() can back-track using those and
1084 * avoid some potentially confusing code.
1085 */
1086 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1087 insn->offset > last->offset &&
1088 insn->jump_dest->offset > insn->offset &&
1089 !insn->jump_dest->first_jump_src) {
1090
1091 insn->jump_dest->first_jump_src = insn;
1092 last = insn->jump_dest;
1093 }
1094
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001095 if (insn->type != INSN_JUMP_DYNAMIC)
1096 continue;
1097
1098 rela = find_switch_table(file, func, insn);
1099 if (!rela)
1100 continue;
1101
1102 /*
1103 * We found a switch table, but we don't know yet how big it
1104 * is. Don't add it until we reach the end of the function or
1105 * the beginning of another switch table in the same function.
1106 */
1107 if (prev_jump) {
Josh Poimboeuf13810432018-05-09 22:39:15 -05001108 ret = add_switch_table(file, prev_jump, prev_rela, rela);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001109 if (ret)
1110 return ret;
1111 }
1112
1113 prev_jump = insn;
1114 prev_rela = rela;
1115 }
1116
1117 if (prev_jump) {
Josh Poimboeuf13810432018-05-09 22:39:15 -05001118 ret = add_switch_table(file, prev_jump, prev_rela, NULL);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001119 if (ret)
1120 return ret;
1121 }
1122
1123 return 0;
1124}
1125
1126/*
1127 * For some switch statements, gcc generates a jump table in the .rodata
1128 * section which contains a list of addresses within the function to jump to.
1129 * This finds these jump tables and adds them to the insn->alts lists.
1130 */
1131static int add_switch_table_alts(struct objtool_file *file)
1132{
1133 struct section *sec;
1134 struct symbol *func;
1135 int ret;
1136
Allan Xavier4a60aa02018-09-07 08:12:01 -05001137 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001138 return 0;
1139
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001140 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001141 list_for_each_entry(func, &sec->symbol_list, list) {
1142 if (func->type != STT_FUNC)
1143 continue;
1144
1145 ret = add_func_switch_tables(file, func);
1146 if (ret)
1147 return ret;
1148 }
1149 }
1150
1151 return 0;
1152}
1153
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001154static int read_unwind_hints(struct objtool_file *file)
1155{
1156 struct section *sec, *relasec;
1157 struct rela *rela;
1158 struct unwind_hint *hint;
1159 struct instruction *insn;
1160 struct cfi_reg *cfa;
1161 int i;
1162
1163 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1164 if (!sec)
1165 return 0;
1166
1167 relasec = sec->rela;
1168 if (!relasec) {
1169 WARN("missing .rela.discard.unwind_hints section");
1170 return -1;
1171 }
1172
1173 if (sec->len % sizeof(struct unwind_hint)) {
1174 WARN("struct unwind_hint size mismatch");
1175 return -1;
1176 }
1177
1178 file->hints = true;
1179
1180 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1181 hint = (struct unwind_hint *)sec->data->d_buf + i;
1182
1183 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1184 if (!rela) {
1185 WARN("can't find rela for unwind_hints[%d]", i);
1186 return -1;
1187 }
1188
1189 insn = find_insn(file, rela->sym->sec, rela->addend);
1190 if (!insn) {
1191 WARN("can't find insn for unwind_hints[%d]", i);
1192 return -1;
1193 }
1194
1195 cfa = &insn->state.cfa;
1196
1197 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1198 insn->save = true;
1199 continue;
1200
1201 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1202 insn->restore = true;
1203 insn->hint = true;
1204 continue;
1205 }
1206
1207 insn->hint = true;
1208
1209 switch (hint->sp_reg) {
1210 case ORC_REG_UNDEFINED:
1211 cfa->base = CFI_UNDEFINED;
1212 break;
1213 case ORC_REG_SP:
1214 cfa->base = CFI_SP;
1215 break;
1216 case ORC_REG_BP:
1217 cfa->base = CFI_BP;
1218 break;
1219 case ORC_REG_SP_INDIRECT:
1220 cfa->base = CFI_SP_INDIRECT;
1221 break;
1222 case ORC_REG_R10:
1223 cfa->base = CFI_R10;
1224 break;
1225 case ORC_REG_R13:
1226 cfa->base = CFI_R13;
1227 break;
1228 case ORC_REG_DI:
1229 cfa->base = CFI_DI;
1230 break;
1231 case ORC_REG_DX:
1232 cfa->base = CFI_DX;
1233 break;
1234 default:
1235 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1236 insn->sec, insn->offset, hint->sp_reg);
1237 return -1;
1238 }
1239
1240 cfa->offset = hint->sp_offset;
1241 insn->state.type = hint->type;
Josh Poimboeufd31a5802018-05-18 08:47:12 +02001242 insn->state.end = hint->end;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001243 }
1244
1245 return 0;
1246}
1247
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001248static int read_retpoline_hints(struct objtool_file *file)
1249{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001250 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001251 struct instruction *insn;
1252 struct rela *rela;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001253
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001254 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001255 if (!sec)
1256 return 0;
1257
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001258 list_for_each_entry(rela, &sec->rela_list, list) {
1259 if (rela->sym->type != STT_SECTION) {
1260 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001261 return -1;
1262 }
1263
1264 insn = find_insn(file, rela->sym->sec, rela->addend);
1265 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001266 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001267 return -1;
1268 }
1269
1270 if (insn->type != INSN_JUMP_DYNAMIC &&
1271 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001272 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001273 insn->sec, insn->offset);
1274 return -1;
1275 }
1276
1277 insn->retpoline_safe = true;
1278 }
1279
1280 return 0;
1281}
1282
Allan Xavier4a60aa02018-09-07 08:12:01 -05001283static void mark_rodata(struct objtool_file *file)
1284{
1285 struct section *sec;
1286 bool found = false;
1287
1288 /*
1289 * This searches for the .rodata section or multiple .rodata.func_name
1290 * sections if -fdata-sections is being used. The .str.1.1 and .str.1.8
1291 * rodata sections are ignored as they don't contain jump tables.
1292 */
1293 for_each_sec(file, sec) {
1294 if (!strncmp(sec->name, ".rodata", 7) &&
1295 !strstr(sec->name, ".str1.")) {
1296 sec->rodata = true;
1297 found = true;
1298 }
1299 }
1300
1301 file->rodata = found;
1302}
1303
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001304static int decode_sections(struct objtool_file *file)
1305{
1306 int ret;
1307
Allan Xavier4a60aa02018-09-07 08:12:01 -05001308 mark_rodata(file);
1309
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001310 ret = decode_instructions(file);
1311 if (ret)
1312 return ret;
1313
1314 ret = add_dead_ends(file);
1315 if (ret)
1316 return ret;
1317
1318 add_ignores(file);
Peter Zijlstraea242132019-02-25 12:50:09 +01001319 add_uaccess_safe(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001320
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001321 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001322 if (ret)
1323 return ret;
1324
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001325 ret = add_jump_destinations(file);
1326 if (ret)
1327 return ret;
1328
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001329 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001330 if (ret)
1331 return ret;
1332
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001333 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001334 if (ret)
1335 return ret;
1336
1337 ret = add_switch_table_alts(file);
1338 if (ret)
1339 return ret;
1340
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001341 ret = read_unwind_hints(file);
1342 if (ret)
1343 return ret;
1344
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001345 ret = read_retpoline_hints(file);
1346 if (ret)
1347 return ret;
1348
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001349 return 0;
1350}
1351
1352static bool is_fentry_call(struct instruction *insn)
1353{
1354 if (insn->type == INSN_CALL &&
1355 insn->call_dest->type == STT_NOTYPE &&
1356 !strcmp(insn->call_dest->name, "__fentry__"))
1357 return true;
1358
1359 return false;
1360}
1361
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001362static bool has_modified_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001363{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001364 int i;
1365
1366 if (state->cfa.base != initial_func_cfi.cfa.base ||
1367 state->cfa.offset != initial_func_cfi.cfa.offset ||
1368 state->stack_size != initial_func_cfi.cfa.offset ||
1369 state->drap)
1370 return true;
1371
1372 for (i = 0; i < CFI_NUM_REGS; i++)
1373 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1374 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1375 return true;
1376
1377 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001378}
1379
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001380static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001381{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001382 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1383 state->regs[CFI_BP].offset == -16)
1384 return true;
1385
1386 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1387 return true;
1388
1389 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001390}
1391
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001392static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1393{
1394 struct cfi_reg *cfa = &state->cfa;
1395 struct stack_op *op = &insn->stack_op;
1396
1397 if (cfa->base != CFI_SP)
1398 return 0;
1399
1400 /* push */
Peter Zijlstraea242132019-02-25 12:50:09 +01001401 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001402 cfa->offset += 8;
1403
1404 /* pop */
Peter Zijlstraea242132019-02-25 12:50:09 +01001405 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001406 cfa->offset -= 8;
1407
1408 /* add immediate to sp */
1409 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1410 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1411 cfa->offset -= op->src.offset;
1412
1413 return 0;
1414}
1415
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001416static void save_reg(struct insn_state *state, unsigned char reg, int base,
1417 int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001418{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001419 if (arch_callee_saved_reg(reg) &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001420 state->regs[reg].base == CFI_UNDEFINED) {
1421 state->regs[reg].base = base;
1422 state->regs[reg].offset = offset;
1423 }
1424}
1425
1426static void restore_reg(struct insn_state *state, unsigned char reg)
1427{
1428 state->regs[reg].base = CFI_UNDEFINED;
1429 state->regs[reg].offset = 0;
1430}
1431
1432/*
1433 * A note about DRAP stack alignment:
1434 *
1435 * GCC has the concept of a DRAP register, which is used to help keep track of
1436 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1437 * register. The typical DRAP pattern is:
1438 *
1439 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1440 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1441 * 41 ff 72 f8 pushq -0x8(%r10)
1442 * 55 push %rbp
1443 * 48 89 e5 mov %rsp,%rbp
1444 * (more pushes)
1445 * 41 52 push %r10
1446 * ...
1447 * 41 5a pop %r10
1448 * (more pops)
1449 * 5d pop %rbp
1450 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1451 * c3 retq
1452 *
1453 * There are some variations in the epilogues, like:
1454 *
1455 * 5b pop %rbx
1456 * 41 5a pop %r10
1457 * 41 5c pop %r12
1458 * 41 5d pop %r13
1459 * 41 5e pop %r14
1460 * c9 leaveq
1461 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1462 * c3 retq
1463 *
1464 * and:
1465 *
1466 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1467 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1468 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1469 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1470 * c9 leaveq
1471 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1472 * c3 retq
1473 *
1474 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1475 * restored beforehand:
1476 *
1477 * 41 55 push %r13
1478 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1479 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1480 * ...
1481 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1482 * 41 5d pop %r13
1483 * c3 retq
1484 */
1485static int update_insn_state(struct instruction *insn, struct insn_state *state)
1486{
1487 struct stack_op *op = &insn->stack_op;
1488 struct cfi_reg *cfa = &state->cfa;
1489 struct cfi_reg *regs = state->regs;
1490
1491 /* stack operations don't make sense with an undefined CFA */
1492 if (cfa->base == CFI_UNDEFINED) {
1493 if (insn->func) {
1494 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1495 return -1;
1496 }
1497 return 0;
1498 }
1499
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001500 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1501 return update_insn_state_regs(insn, state);
1502
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001503 switch (op->dest.type) {
1504
1505 case OP_DEST_REG:
1506 switch (op->src.type) {
1507
1508 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001509 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1510 cfa->base == CFI_SP &&
1511 regs[CFI_BP].base == CFI_CFA &&
1512 regs[CFI_BP].offset == -cfa->offset) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001513
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001514 /* mov %rsp, %rbp */
1515 cfa->base = op->dest.reg;
1516 state->bp_scratch = false;
1517 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001518
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001519 else if (op->src.reg == CFI_SP &&
1520 op->dest.reg == CFI_BP && state->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001521
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001522 /* drap: mov %rsp, %rbp */
1523 regs[CFI_BP].base = CFI_BP;
1524 regs[CFI_BP].offset = -state->stack_size;
1525 state->bp_scratch = false;
1526 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001527
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001528 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1529
1530 /*
1531 * mov %rsp, %reg
1532 *
1533 * This is needed for the rare case where GCC
1534 * does:
1535 *
1536 * mov %rsp, %rax
1537 * ...
1538 * mov %rax, %rsp
1539 */
1540 state->vals[op->dest.reg].base = CFI_CFA;
1541 state->vals[op->dest.reg].offset = -state->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001542 }
1543
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001544 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1545 cfa->base == CFI_BP) {
1546
1547 /*
1548 * mov %rbp, %rsp
1549 *
1550 * Restore the original stack pointer (Clang).
1551 */
1552 state->stack_size = -state->regs[CFI_BP].offset;
1553 }
1554
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001555 else if (op->dest.reg == cfa->base) {
1556
1557 /* mov %reg, %rsp */
1558 if (cfa->base == CFI_SP &&
1559 state->vals[op->src.reg].base == CFI_CFA) {
1560
1561 /*
1562 * This is needed for the rare case
1563 * where GCC does something dumb like:
1564 *
1565 * lea 0x8(%rsp), %rcx
1566 * ...
1567 * mov %rcx, %rsp
1568 */
1569 cfa->offset = -state->vals[op->src.reg].offset;
1570 state->stack_size = cfa->offset;
1571
1572 } else {
1573 cfa->base = CFI_UNDEFINED;
1574 cfa->offset = 0;
1575 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001576 }
1577
1578 break;
1579
1580 case OP_SRC_ADD:
1581 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1582
1583 /* add imm, %rsp */
1584 state->stack_size -= op->src.offset;
1585 if (cfa->base == CFI_SP)
1586 cfa->offset -= op->src.offset;
1587 break;
1588 }
1589
1590 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1591
1592 /* lea disp(%rbp), %rsp */
1593 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1594 break;
1595 }
1596
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001597 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001598
1599 /* drap: lea disp(%rsp), %drap */
1600 state->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001601
1602 /*
1603 * lea disp(%rsp), %reg
1604 *
1605 * This is needed for the rare case where GCC
1606 * does something dumb like:
1607 *
1608 * lea 0x8(%rsp), %rcx
1609 * ...
1610 * mov %rcx, %rsp
1611 */
1612 state->vals[op->dest.reg].base = CFI_CFA;
1613 state->vals[op->dest.reg].offset = \
1614 -state->stack_size + op->src.offset;
1615
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001616 break;
1617 }
1618
1619 if (state->drap && op->dest.reg == CFI_SP &&
1620 op->src.reg == state->drap_reg) {
1621
1622 /* drap: lea disp(%drap), %rsp */
1623 cfa->base = CFI_SP;
1624 cfa->offset = state->stack_size = -op->src.offset;
1625 state->drap_reg = CFI_UNDEFINED;
1626 state->drap = false;
1627 break;
1628 }
1629
1630 if (op->dest.reg == state->cfa.base) {
1631 WARN_FUNC("unsupported stack register modification",
1632 insn->sec, insn->offset);
1633 return -1;
1634 }
1635
1636 break;
1637
1638 case OP_SRC_AND:
1639 if (op->dest.reg != CFI_SP ||
1640 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1641 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1642 WARN_FUNC("unsupported stack pointer realignment",
1643 insn->sec, insn->offset);
1644 return -1;
1645 }
1646
1647 if (state->drap_reg != CFI_UNDEFINED) {
1648 /* drap: and imm, %rsp */
1649 cfa->base = state->drap_reg;
1650 cfa->offset = state->stack_size = 0;
1651 state->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001652 }
1653
1654 /*
1655 * Older versions of GCC (4.8ish) realign the stack
1656 * without DRAP, with a frame pointer.
1657 */
1658
1659 break;
1660
1661 case OP_SRC_POP:
Peter Zijlstraea242132019-02-25 12:50:09 +01001662 case OP_SRC_POPF:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001663 if (!state->drap && op->dest.type == OP_DEST_REG &&
1664 op->dest.reg == cfa->base) {
1665
1666 /* pop %rbp */
1667 cfa->base = CFI_SP;
1668 }
1669
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001670 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1671 op->dest.type == OP_DEST_REG &&
1672 op->dest.reg == state->drap_reg &&
1673 state->drap_offset == -state->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001674
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001675 /* drap: pop %drap */
1676 cfa->base = state->drap_reg;
1677 cfa->offset = 0;
1678 state->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001679
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001680 } else if (regs[op->dest.reg].offset == -state->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001681
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001682 /* pop %reg */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001683 restore_reg(state, op->dest.reg);
1684 }
1685
1686 state->stack_size -= 8;
1687 if (cfa->base == CFI_SP)
1688 cfa->offset -= 8;
1689
1690 break;
1691
1692 case OP_SRC_REG_INDIRECT:
1693 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001694 op->src.offset == state->drap_offset) {
1695
1696 /* drap: mov disp(%rbp), %drap */
1697 cfa->base = state->drap_reg;
1698 cfa->offset = 0;
1699 state->drap_offset = -1;
1700 }
1701
1702 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001703 op->src.offset == regs[op->dest.reg].offset) {
1704
1705 /* drap: mov disp(%rbp), %reg */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001706 restore_reg(state, op->dest.reg);
1707
1708 } else if (op->src.reg == cfa->base &&
1709 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1710
1711 /* mov disp(%rbp), %reg */
1712 /* mov disp(%rsp), %reg */
1713 restore_reg(state, op->dest.reg);
1714 }
1715
1716 break;
1717
1718 default:
1719 WARN_FUNC("unknown stack-related instruction",
1720 insn->sec, insn->offset);
1721 return -1;
1722 }
1723
1724 break;
1725
1726 case OP_DEST_PUSH:
Peter Zijlstraea242132019-02-25 12:50:09 +01001727 case OP_DEST_PUSHF:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001728 state->stack_size += 8;
1729 if (cfa->base == CFI_SP)
1730 cfa->offset += 8;
1731
1732 if (op->src.type != OP_SRC_REG)
1733 break;
1734
1735 if (state->drap) {
1736 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1737
1738 /* drap: push %drap */
1739 cfa->base = CFI_BP_INDIRECT;
1740 cfa->offset = -state->stack_size;
1741
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001742 /* save drap so we know when to restore it */
1743 state->drap_offset = -state->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001744
1745 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1746
1747 /* drap: push %rbp */
1748 state->stack_size = 0;
1749
1750 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1751
1752 /* drap: push %reg */
1753 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1754 }
1755
1756 } else {
1757
1758 /* push %reg */
1759 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1760 }
1761
1762 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001763 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001764 cfa->base != CFI_BP)
1765 state->bp_scratch = true;
1766 break;
1767
1768 case OP_DEST_REG_INDIRECT:
1769
1770 if (state->drap) {
1771 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1772
1773 /* drap: mov %drap, disp(%rbp) */
1774 cfa->base = CFI_BP_INDIRECT;
1775 cfa->offset = op->dest.offset;
1776
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001777 /* save drap offset so we know when to restore it */
1778 state->drap_offset = op->dest.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001779 }
1780
1781 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1782
1783 /* drap: mov reg, disp(%rbp) */
1784 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1785 }
1786
1787 } else if (op->dest.reg == cfa->base) {
1788
1789 /* mov reg, disp(%rbp) */
1790 /* mov reg, disp(%rsp) */
1791 save_reg(state, op->src.reg, CFI_CFA,
1792 op->dest.offset - state->cfa.offset);
1793 }
1794
1795 break;
1796
1797 case OP_DEST_LEAVE:
1798 if ((!state->drap && cfa->base != CFI_BP) ||
1799 (state->drap && cfa->base != state->drap_reg)) {
1800 WARN_FUNC("leave instruction with modified stack frame",
1801 insn->sec, insn->offset);
1802 return -1;
1803 }
1804
1805 /* leave (mov %rbp, %rsp; pop %rbp) */
1806
1807 state->stack_size = -state->regs[CFI_BP].offset - 8;
1808 restore_reg(state, CFI_BP);
1809
1810 if (!state->drap) {
1811 cfa->base = CFI_SP;
1812 cfa->offset -= 8;
1813 }
1814
1815 break;
1816
1817 case OP_DEST_MEM:
Peter Zijlstraea242132019-02-25 12:50:09 +01001818 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001819 WARN_FUNC("unknown stack-related memory operation",
1820 insn->sec, insn->offset);
1821 return -1;
1822 }
1823
1824 /* pop mem */
1825 state->stack_size -= 8;
1826 if (cfa->base == CFI_SP)
1827 cfa->offset -= 8;
1828
1829 break;
1830
1831 default:
1832 WARN_FUNC("unknown stack-related instruction",
1833 insn->sec, insn->offset);
1834 return -1;
1835 }
1836
1837 return 0;
1838}
1839
1840static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1841{
1842 struct insn_state *state1 = &insn->state, *state2 = state;
1843 int i;
1844
1845 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1846 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1847 insn->sec, insn->offset,
1848 state1->cfa.base, state1->cfa.offset,
1849 state2->cfa.base, state2->cfa.offset);
1850
1851 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1852 for (i = 0; i < CFI_NUM_REGS; i++) {
1853 if (!memcmp(&state1->regs[i], &state2->regs[i],
1854 sizeof(struct cfi_reg)))
1855 continue;
1856
1857 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1858 insn->sec, insn->offset,
1859 i, state1->regs[i].base, state1->regs[i].offset,
1860 i, state2->regs[i].base, state2->regs[i].offset);
1861 break;
1862 }
1863
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001864 } else if (state1->type != state2->type) {
1865 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1866 insn->sec, insn->offset, state1->type, state2->type);
1867
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001868 } else if (state1->drap != state2->drap ||
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001869 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1870 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1871 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001872 insn->sec, insn->offset,
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001873 state1->drap, state1->drap_reg, state1->drap_offset,
1874 state2->drap, state2->drap_reg, state2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001875
1876 } else
1877 return true;
1878
1879 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001880}
1881
Peter Zijlstraea242132019-02-25 12:50:09 +01001882static inline bool func_uaccess_safe(struct symbol *func)
1883{
1884 if (func)
1885 return func->alias->uaccess_safe;
1886
1887 return false;
1888}
1889
1890static inline const char *insn_dest_name(struct instruction *insn)
1891{
1892 if (insn->call_dest)
1893 return insn->call_dest->name;
1894
1895 return "{dynamic}";
1896}
1897
1898static int validate_call(struct instruction *insn, struct insn_state *state)
1899{
1900 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
1901 WARN_FUNC("call to %s() with UACCESS enabled",
1902 insn->sec, insn->offset, insn_dest_name(insn));
1903 return 1;
1904 }
1905
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01001906 if (state->df) {
1907 WARN_FUNC("call to %s() with DF set",
1908 insn->sec, insn->offset, insn_dest_name(insn));
1909 return 1;
1910 }
1911
Peter Zijlstraea242132019-02-25 12:50:09 +01001912 return 0;
1913}
1914
Peter Zijlstra54262aa2019-03-06 12:58:15 +01001915static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
1916{
1917 if (has_modified_stack_frame(state)) {
1918 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1919 insn->sec, insn->offset);
1920 return 1;
1921 }
1922
Peter Zijlstraea242132019-02-25 12:50:09 +01001923 return validate_call(insn, state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01001924}
1925
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001926/*
1927 * Follow the branch starting at the given instruction, and recursively follow
1928 * any other branches (jumps). Meanwhile, track the frame pointer state at
1929 * each instruction and validate all the rules described in
1930 * tools/objtool/Documentation/stack-validation.txt.
1931 */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001932static int validate_branch(struct objtool_file *file, struct instruction *first,
1933 struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001934{
1935 struct alternative *alt;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001936 struct instruction *insn, *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001937 struct section *sec;
1938 struct symbol *func = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001939 int ret;
1940
1941 insn = first;
1942 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001943
1944 if (insn->alt_group && list_empty(&insn->alts)) {
1945 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1946 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001947 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001948 }
1949
1950 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001951 next_insn = next_insn_same_sec(file, insn);
1952
Josh Poimboeuf13810432018-05-09 22:39:15 -05001953 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05001954 WARN("%s() falls through to next function %s()",
1955 func->name, insn->func->name);
1956 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001957 }
1958
Josh Poimboeuf13810432018-05-09 22:39:15 -05001959 func = insn->func ? insn->func->pfunc : NULL;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001960
Josh Poimboeuf48550222017-07-07 09:19:42 -05001961 if (func && insn->ignore) {
1962 WARN_FUNC("BUG: why am I validating an ignored function?",
1963 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001964 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001965 }
1966
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001967 if (insn->visited) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001968 if (!insn->hint && !insn_state_match(insn, &state))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001969 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001970
Peter Zijlstraea242132019-02-25 12:50:09 +01001971 /* If we were here with AC=0, but now have AC=1, go again */
1972 if (insn->state.uaccess || !state.uaccess)
1973 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001974 }
1975
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001976 if (insn->hint) {
1977 if (insn->restore) {
1978 struct instruction *save_insn, *i;
1979
1980 i = insn;
1981 save_insn = NULL;
Josh Poimboeuf13810432018-05-09 22:39:15 -05001982 func_for_each_insn_continue_reverse(file, insn->func, i) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001983 if (i->save) {
1984 save_insn = i;
1985 break;
1986 }
1987 }
1988
1989 if (!save_insn) {
1990 WARN_FUNC("no corresponding CFI save for CFI restore",
1991 sec, insn->offset);
1992 return 1;
1993 }
1994
1995 if (!save_insn->visited) {
1996 /*
1997 * Oops, no state to copy yet.
1998 * Hopefully we can reach this
1999 * instruction from another branch
2000 * after the save insn has been
2001 * visited.
2002 */
2003 if (insn == first)
2004 return 0;
2005
2006 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
2007 sec, insn->offset);
2008 return 1;
2009 }
2010
2011 insn->state = save_insn->state;
2012 }
2013
2014 state = insn->state;
2015
2016 } else
2017 insn->state = state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002018
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002019 insn->visited = true;
2020
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002021 if (!insn->ignore_alts) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002022 bool skip_orig = false;
2023
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002024 list_for_each_entry(alt, &insn->alts, list) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002025 if (alt->skip_orig)
2026 skip_orig = true;
2027
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002028 ret = validate_branch(file, alt->insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002029 if (ret) {
2030 if (backtrace)
2031 BT_FUNC("(alt)", insn);
2032 return ret;
2033 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002034 }
Peter Zijlstra764eef42019-03-01 11:19:03 +01002035
2036 if (skip_orig)
2037 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002038 }
2039
2040 switch (insn->type) {
2041
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002042 case INSN_RETURN:
Peter Zijlstraea242132019-02-25 12:50:09 +01002043 if (state.uaccess && !func_uaccess_safe(func)) {
2044 WARN_FUNC("return with UACCESS enabled", sec, insn->offset);
2045 return 1;
2046 }
2047
2048 if (!state.uaccess && func_uaccess_safe(func)) {
2049 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", sec, insn->offset);
2050 return 1;
2051 }
2052
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002053 if (state.df) {
2054 WARN_FUNC("return with DF set", sec, insn->offset);
2055 return 1;
2056 }
2057
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002058 if (func && has_modified_stack_frame(&state)) {
2059 WARN_FUNC("return with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002060 sec, insn->offset);
2061 return 1;
2062 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002063
2064 if (state.bp_scratch) {
2065 WARN("%s uses BP as a scratch register",
2066 insn->func->name);
2067 return 1;
2068 }
2069
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002070 return 0;
2071
2072 case INSN_CALL:
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002073 case INSN_CALL_DYNAMIC:
Peter Zijlstraea242132019-02-25 12:50:09 +01002074 ret = validate_call(insn, &state);
2075 if (ret)
2076 return ret;
2077
2078 if (insn->type == INSN_CALL) {
2079 if (is_fentry_call(insn))
2080 break;
2081
2082 ret = dead_end_function(file, insn->call_dest);
2083 if (ret == 1)
2084 return 0;
2085 if (ret == -1)
2086 return 1;
2087 }
2088
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002089 if (!no_fp && func && !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002090 WARN_FUNC("call without frame pointer save/setup",
2091 sec, insn->offset);
2092 return 1;
2093 }
2094 break;
2095
2096 case INSN_JUMP_CONDITIONAL:
2097 case INSN_JUMP_UNCONDITIONAL:
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002098 if (func && !insn->jump_dest) {
2099 ret = validate_sibling_call(insn, &state);
2100 if (ret)
2101 return ret;
2102
2103 } else if (insn->jump_dest &&
2104 (!func || !insn->jump_dest->func ||
2105 insn->jump_dest->func->pfunc == func)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002106 ret = validate_branch(file, insn->jump_dest,
2107 state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002108 if (ret) {
2109 if (backtrace)
2110 BT_FUNC("(branch)", insn);
2111 return ret;
2112 }
Josh Poimboeuf48550222017-07-07 09:19:42 -05002113 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002114
2115 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2116 return 0;
2117
2118 break;
2119
2120 case INSN_JUMP_DYNAMIC:
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002121 if (func && list_empty(&insn->alts)) {
2122 ret = validate_sibling_call(insn, &state);
2123 if (ret)
2124 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002125 }
2126
2127 return 0;
2128
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002129 case INSN_CONTEXT_SWITCH:
2130 if (func && (!next_insn || !next_insn->hint)) {
2131 WARN_FUNC("unsupported instruction in callable function",
2132 sec, insn->offset);
2133 return 1;
2134 }
2135 return 0;
2136
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002137 case INSN_STACK:
2138 if (update_insn_state(insn, &state))
Josh Poimboeuf12b25722017-08-10 16:37:25 -05002139 return 1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002140
Peter Zijlstraea242132019-02-25 12:50:09 +01002141 if (insn->stack_op.dest.type == OP_DEST_PUSHF) {
2142 if (!state.uaccess_stack) {
2143 state.uaccess_stack = 1;
2144 } else if (state.uaccess_stack >> 31) {
2145 WARN_FUNC("PUSHF stack exhausted", sec, insn->offset);
2146 return 1;
2147 }
2148 state.uaccess_stack <<= 1;
2149 state.uaccess_stack |= state.uaccess;
2150 }
2151
2152 if (insn->stack_op.src.type == OP_SRC_POPF) {
2153 if (state.uaccess_stack) {
2154 state.uaccess = state.uaccess_stack & 1;
2155 state.uaccess_stack >>= 1;
2156 if (state.uaccess_stack == 1)
2157 state.uaccess_stack = 0;
2158 }
2159 }
2160
2161 break;
2162
2163 case INSN_STAC:
2164 if (state.uaccess) {
2165 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2166 return 1;
2167 }
2168
2169 state.uaccess = true;
2170 break;
2171
2172 case INSN_CLAC:
2173 if (!state.uaccess && insn->func) {
2174 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2175 return 1;
2176 }
2177
2178 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2179 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2180 return 1;
2181 }
2182
2183 state.uaccess = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002184 break;
2185
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002186 case INSN_STD:
2187 if (state.df)
2188 WARN_FUNC("recursive STD", sec, insn->offset);
2189
2190 state.df = true;
2191 break;
2192
2193 case INSN_CLD:
2194 if (!state.df && insn->func)
2195 WARN_FUNC("redundant CLD", sec, insn->offset);
2196
2197 state.df = false;
2198 break;
2199
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002200 default:
2201 break;
2202 }
2203
2204 if (insn->dead_end)
2205 return 0;
2206
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002207 if (!next_insn) {
2208 if (state.cfa.base == CFI_UNDEFINED)
2209 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002210 WARN("%s: unexpected end of section", sec->name);
2211 return 1;
2212 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002213
2214 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002215 }
2216
2217 return 0;
2218}
2219
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002220static int validate_unwind_hints(struct objtool_file *file)
2221{
2222 struct instruction *insn;
2223 int ret, warnings = 0;
2224 struct insn_state state;
2225
2226 if (!file->hints)
2227 return 0;
2228
2229 clear_insn_state(&state);
2230
2231 for_each_insn(file, insn) {
2232 if (insn->hint && !insn->visited) {
2233 ret = validate_branch(file, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002234 if (ret && backtrace)
2235 BT_FUNC("<=== (hint)", insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002236 warnings += ret;
2237 }
2238 }
2239
2240 return warnings;
2241}
2242
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002243static int validate_retpoline(struct objtool_file *file)
2244{
2245 struct instruction *insn;
2246 int warnings = 0;
2247
2248 for_each_insn(file, insn) {
2249 if (insn->type != INSN_JUMP_DYNAMIC &&
2250 insn->type != INSN_CALL_DYNAMIC)
2251 continue;
2252
2253 if (insn->retpoline_safe)
2254 continue;
2255
Peter Zijlstraca41b972018-01-31 10:18:28 +01002256 /*
2257 * .init.text code is ran before userspace and thus doesn't
2258 * strictly need retpolines, except for modules which are
2259 * loaded late, they very much do need retpoline in their
2260 * .init.text
2261 */
2262 if (!strcmp(insn->sec->name, ".init.text") && !module)
2263 continue;
2264
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002265 WARN_FUNC("indirect %s found in RETPOLINE build",
2266 insn->sec, insn->offset,
2267 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2268
2269 warnings++;
2270 }
2271
2272 return warnings;
2273}
2274
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002275static bool is_kasan_insn(struct instruction *insn)
2276{
2277 return (insn->type == INSN_CALL &&
2278 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2279}
2280
2281static bool is_ubsan_insn(struct instruction *insn)
2282{
2283 return (insn->type == INSN_CALL &&
2284 !strcmp(insn->call_dest->name,
2285 "__ubsan_handle_builtin_unreachable"));
2286}
2287
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002288static bool ignore_unreachable_insn(struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002289{
2290 int i;
2291
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002292 if (insn->ignore || insn->type == INSN_NOP)
2293 return true;
2294
2295 /*
2296 * Ignore any unused exceptions. This can happen when a whitelisted
2297 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002298 *
2299 * Also ignore alternative replacement instructions. This can happen
2300 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002301 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002302 if (!strcmp(insn->sec->name, ".fixup") ||
2303 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2304 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002305 return true;
2306
2307 /*
2308 * Check if this (or a subsequent) instruction is related to
2309 * CONFIG_UBSAN or CONFIG_KASAN.
2310 *
2311 * End the search at 5 instructions to avoid going into the weeds.
2312 */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002313 if (!insn->func)
2314 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002315 for (i = 0; i < 5; i++) {
2316
2317 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2318 return true;
2319
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002320 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2321 if (insn->jump_dest &&
2322 insn->jump_dest->func == insn->func) {
2323 insn = insn->jump_dest;
2324 continue;
2325 }
2326
2327 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002328 }
2329
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002330 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002331 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002332
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002333 insn = list_next_entry(insn, list);
2334 }
2335
2336 return false;
2337}
2338
2339static int validate_functions(struct objtool_file *file)
2340{
2341 struct section *sec;
2342 struct symbol *func;
2343 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002344 struct insn_state state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002345 int ret, warnings = 0;
2346
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002347 clear_insn_state(&state);
2348
2349 state.cfa = initial_func_cfi.cfa;
2350 memcpy(&state.regs, &initial_func_cfi.regs,
2351 CFI_NUM_REGS * sizeof(struct cfi_reg));
2352 state.stack_size = initial_func_cfi.cfa.offset;
2353
2354 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002355 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf13810432018-05-09 22:39:15 -05002356 if (func->type != STT_FUNC || func->pfunc != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002357 continue;
2358
2359 insn = find_insn(file, sec, func->offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002360 if (!insn || insn->ignore)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002361 continue;
2362
Peter Zijlstraea242132019-02-25 12:50:09 +01002363 state.uaccess = func->alias->uaccess_safe;
2364
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002365 ret = validate_branch(file, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002366 if (ret && backtrace)
2367 BT_FUNC("<=== (func)", insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002368 warnings += ret;
2369 }
2370 }
2371
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002372 return warnings;
2373}
2374
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002375static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002376{
2377 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002378
2379 if (file->ignore_unreachables)
2380 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002381
2382 for_each_insn(file, insn) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002383 if (insn->visited || ignore_unreachable_insn(insn))
2384 continue;
2385
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002386 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2387 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002388 }
2389
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002390 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002391}
2392
2393static void cleanup(struct objtool_file *file)
2394{
2395 struct instruction *insn, *tmpinsn;
2396 struct alternative *alt, *tmpalt;
2397
2398 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2399 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2400 list_del(&alt->list);
2401 free(alt);
2402 }
2403 list_del(&insn->list);
2404 hash_del(&insn->hash);
2405 free(insn);
2406 }
2407 elf_close(file->elf);
2408}
2409
Josh Poimboeuf0c671812019-03-18 19:09:38 -05002410static struct objtool_file file;
2411
Peter Zijlstra43a45252018-01-16 17:16:32 +01002412int check(const char *_objname, bool orc)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002413{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002414 int ret, warnings = 0;
2415
2416 objname = _objname;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002417
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002418 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002419 if (!file.elf)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002420 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002421
2422 INIT_LIST_HEAD(&file.insn_list);
2423 hash_init(file.insn_hash);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002424 file.c_file = find_section_by_name(file.elf, ".comment");
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002425 file.ignore_unreachables = no_unreachable;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002426 file.hints = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002427
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002428 arch_initial_func_cfi_state(&initial_func_cfi);
2429
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002430 ret = decode_sections(&file);
2431 if (ret < 0)
2432 goto out;
2433 warnings += ret;
2434
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002435 if (list_empty(&file.insn_list))
2436 goto out;
2437
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002438 if (retpoline) {
2439 ret = validate_retpoline(&file);
2440 if (ret < 0)
2441 return ret;
2442 warnings += ret;
2443 }
2444
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002445 ret = validate_functions(&file);
2446 if (ret < 0)
2447 goto out;
2448 warnings += ret;
2449
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002450 ret = validate_unwind_hints(&file);
2451 if (ret < 0)
2452 goto out;
2453 warnings += ret;
2454
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002455 if (!warnings) {
2456 ret = validate_reachable_instructions(&file);
2457 if (ret < 0)
2458 goto out;
2459 warnings += ret;
2460 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002461
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002462 if (orc) {
2463 ret = create_orc(&file);
2464 if (ret < 0)
2465 goto out;
2466
2467 ret = create_orc_sections(&file);
2468 if (ret < 0)
2469 goto out;
2470
2471 ret = elf_write(file.elf);
2472 if (ret < 0)
2473 goto out;
2474 }
2475
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002476out:
2477 cleanup(&file);
2478
2479 /* ignore warnings for now until we get all the code cleaned up */
2480 if (ret || warnings)
2481 return 0;
2482 return 0;
2483}