blob: b038de2ccd71a468cfad91965e68c2983fe410e9 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002/*
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05004 */
5
6#include <string.h>
7#include <stdlib.h>
8
Peter Zijlstra43a45252018-01-16 17:16:32 +01009#include "builtin.h"
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050010#include "check.h"
11#include "elf.h"
12#include "special.h"
13#include "arch.h"
14#include "warn.h"
15
16#include <linux/hashtable.h>
17#include <linux/kernel.h>
18
Josh Poimboeufe6da9562019-05-13 12:01:31 -050019#define FAKE_JUMP_OFFSET -1
20
Josh Poimboeuf87b512d2019-06-27 20:50:46 -050021#define C_JUMP_TABLE_SECTION ".rodata..c_jump_table"
22
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050023struct alternative {
24 struct list_head list;
25 struct instruction *insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +010026 bool skip_orig;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050027};
28
29const char *objname;
Josh Poimboeufbaa41462017-06-28 10:11:07 -050030struct cfi_state initial_func_cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050031
Josh Poimboeuf627fce12017-07-11 10:33:42 -050032struct instruction *find_insn(struct objtool_file *file,
33 struct section *sec, unsigned long offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050034{
35 struct instruction *insn;
36
37 hash_for_each_possible(file->insn_hash, insn, hash, offset)
38 if (insn->sec == sec && insn->offset == offset)
39 return insn;
40
41 return NULL;
42}
43
44static struct instruction *next_insn_same_sec(struct objtool_file *file,
45 struct instruction *insn)
46{
47 struct instruction *next = list_next_entry(insn, list);
48
Josh Poimboeufbaa41462017-06-28 10:11:07 -050049 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050050 return NULL;
51
52 return next;
53}
54
Josh Poimboeuf13810432018-05-09 22:39:15 -050055static struct instruction *next_insn_same_func(struct objtool_file *file,
56 struct instruction *insn)
57{
58 struct instruction *next = list_next_entry(insn, list);
59 struct symbol *func = insn->func;
60
61 if (!func)
62 return NULL;
63
64 if (&next->list != &file->insn_list && next->func == func)
65 return next;
66
67 /* Check if we're already in the subfunction: */
68 if (func == func->cfunc)
69 return NULL;
70
71 /* Move to the subfunction: */
72 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
73}
74
75#define func_for_each_insn_all(file, func, insn) \
76 for (insn = find_insn(file, func->sec, func->offset); \
77 insn; \
78 insn = next_insn_same_func(file, insn))
79
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050080#define func_for_each_insn(file, func, insn) \
81 for (insn = find_insn(file, func->sec, func->offset); \
82 insn && &insn->list != &file->insn_list && \
83 insn->sec == func->sec && \
84 insn->offset < func->offset + func->len; \
85 insn = list_next_entry(insn, list))
86
87#define func_for_each_insn_continue_reverse(file, func, insn) \
88 for (insn = list_prev_entry(insn, list); \
89 &insn->list != &file->insn_list && \
90 insn->sec == func->sec && insn->offset >= func->offset; \
91 insn = list_prev_entry(insn, list))
92
93#define sec_for_each_insn_from(file, insn) \
94 for (; insn; insn = next_insn_same_sec(file, insn))
95
Josh Poimboeufbaa41462017-06-28 10:11:07 -050096#define sec_for_each_insn_continue(file, insn) \
97 for (insn = next_insn_same_sec(file, insn); insn; \
98 insn = next_insn_same_sec(file, insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050099
Josh Poimboeufa2296142020-02-10 12:32:39 -0600100static bool is_static_jump(struct instruction *insn)
101{
102 return insn->type == INSN_JUMP_CONDITIONAL ||
103 insn->type == INSN_JUMP_UNCONDITIONAL;
104}
105
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500106static bool is_sibling_call(struct instruction *insn)
107{
108 /* An indirect jump is either a sibling call or a jump to a table. */
109 if (insn->type == INSN_JUMP_DYNAMIC)
110 return list_empty(&insn->alts);
111
Josh Poimboeufa2296142020-02-10 12:32:39 -0600112 if (!is_static_jump(insn))
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500113 return false;
114
115 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
116 return !!insn->call_dest;
117}
118
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500119/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500120 * This checks to see if the given function is a "noreturn" function.
121 *
122 * For global functions which are outside the scope of this object file, we
123 * have to keep a manual list of them.
124 *
125 * For local functions, we have to detect them manually by simply looking for
126 * the lack of a return instruction.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500127 */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500128static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
129 int recursion)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500130{
131 int i;
132 struct instruction *insn;
133 bool empty = true;
134
135 /*
136 * Unfortunately these have to be hard coded because the noreturn
137 * attribute isn't provided in ELF data.
138 */
139 static const char * const global_noreturns[] = {
140 "__stack_chk_fail",
141 "panic",
142 "do_exit",
143 "do_task_dead",
144 "__module_put_and_exit",
145 "complete_and_exit",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500146 "__reiserfs_panic",
147 "lbug_with_loc",
148 "fortify_panic",
Kees Cookb394d462018-01-10 14:22:38 -0800149 "usercopy_abort",
Josh Poimboeuf684fb242018-06-19 10:47:50 -0500150 "machine_real_restart",
Josh Poimboeuf4fa5ecd2019-04-04 12:17:35 -0500151 "rewind_stack_do_exit",
Brendan Higgins33adf802019-09-23 02:02:38 -0700152 "kunit_try_catch_throw",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500153 };
154
Josh Poimboeufc9bab222019-07-17 20:36:51 -0500155 if (!func)
156 return false;
157
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500158 if (func->bind == STB_WEAK)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500159 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500160
161 if (func->bind == STB_GLOBAL)
162 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
163 if (!strcmp(func->name, global_noreturns[i]))
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500164 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500165
Josh Poimboeuf13810432018-05-09 22:39:15 -0500166 if (!func->len)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500167 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500168
Josh Poimboeuf13810432018-05-09 22:39:15 -0500169 insn = find_insn(file, func->sec, func->offset);
170 if (!insn->func)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500171 return false;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500172
173 func_for_each_insn_all(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500174 empty = false;
175
176 if (insn->type == INSN_RETURN)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500177 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500178 }
179
180 if (empty)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500181 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500182
183 /*
184 * A function can have a sibling call instead of a return. In that
185 * case, the function's dead-end status depends on whether the target
186 * of the sibling call returns.
187 */
Josh Poimboeuf13810432018-05-09 22:39:15 -0500188 func_for_each_insn_all(file, func, insn) {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500189 if (is_sibling_call(insn)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500190 struct instruction *dest = insn->jump_dest;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500191
192 if (!dest)
193 /* sibling call to another file */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500194 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500195
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500196 /* local sibling call */
197 if (recursion == 5) {
198 /*
199 * Infinite recursion: two functions have
200 * sibling calls to each other. This is a very
201 * rare case. It means they aren't dead ends.
202 */
203 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500204 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500205
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500206 return __dead_end_function(file, dest->func, recursion+1);
207 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500208 }
209
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500210 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500211}
212
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500213static bool dead_end_function(struct objtool_file *file, struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500214{
215 return __dead_end_function(file, func, 0);
216}
217
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500218static void clear_insn_state(struct insn_state *state)
219{
220 int i;
221
222 memset(state, 0, sizeof(*state));
223 state->cfa.base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500224 for (i = 0; i < CFI_NUM_REGS; i++) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500225 state->regs[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500226 state->vals[i].base = CFI_UNDEFINED;
227 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500228 state->drap_reg = CFI_UNDEFINED;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -0500229 state->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500230}
231
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500232/*
233 * Call the arch-specific instruction decoder for all the instructions and add
234 * them to the global instruction list.
235 */
236static int decode_instructions(struct objtool_file *file)
237{
238 struct section *sec;
239 struct symbol *func;
240 unsigned long offset;
241 struct instruction *insn;
242 int ret;
243
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500244 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500245
246 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
247 continue;
248
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500249 if (strcmp(sec->name, ".altinstr_replacement") &&
250 strcmp(sec->name, ".altinstr_aux") &&
251 strncmp(sec->name, ".discard.", 9))
252 sec->text = true;
253
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500254 for (offset = 0; offset < sec->len; offset += insn->len) {
255 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500256 if (!insn) {
257 WARN("malloc failed");
258 return -1;
259 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500260 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500261 INIT_LIST_HEAD(&insn->alts);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500262 clear_insn_state(&insn->state);
263
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500264 insn->sec = sec;
265 insn->offset = offset;
266
267 ret = arch_decode_instruction(file->elf, sec, offset,
268 sec->len - offset,
269 &insn->len, &insn->type,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500270 &insn->immediate,
271 &insn->stack_op);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500272 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500273 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500274
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500275 hash_add(file->insn_hash, &insn->hash, insn->offset);
276 list_add_tail(&insn->list, &file->insn_list);
277 }
278
279 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500280 if (func->type != STT_FUNC || func->alias != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500281 continue;
282
283 if (!find_insn(file, sec, func->offset)) {
284 WARN("%s(): can't find starting instruction",
285 func->name);
286 return -1;
287 }
288
289 func_for_each_insn(file, func, insn)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500290 insn->func = func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500291 }
292 }
293
294 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500295
296err:
297 free(insn);
298 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500299}
300
301/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500302 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500303 */
304static int add_dead_ends(struct objtool_file *file)
305{
306 struct section *sec;
307 struct rela *rela;
308 struct instruction *insn;
309 bool found;
310
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500311 /*
312 * By default, "ud2" is a dead end unless otherwise annotated, because
313 * GCC 7 inserts it for certain divide-by-zero cases.
314 */
315 for_each_insn(file, insn)
316 if (insn->type == INSN_BUG)
317 insn->dead_end = true;
318
319 /*
320 * Check for manually annotated dead ends.
321 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500322 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
323 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500324 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500325
326 list_for_each_entry(rela, &sec->rela_list, list) {
327 if (rela->sym->type != STT_SECTION) {
328 WARN("unexpected relocation symbol type in %s", sec->name);
329 return -1;
330 }
331 insn = find_insn(file, rela->sym->sec, rela->addend);
332 if (insn)
333 insn = list_prev_entry(insn, list);
334 else if (rela->addend == rela->sym->sec->len) {
335 found = false;
336 list_for_each_entry_reverse(insn, &file->insn_list, list) {
337 if (insn->sec == rela->sym->sec) {
338 found = true;
339 break;
340 }
341 }
342
343 if (!found) {
344 WARN("can't find unreachable insn at %s+0x%x",
345 rela->sym->sec->name, rela->addend);
346 return -1;
347 }
348 } else {
349 WARN("can't find unreachable insn at %s+0x%x",
350 rela->sym->sec->name, rela->addend);
351 return -1;
352 }
353
354 insn->dead_end = true;
355 }
356
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500357reachable:
358 /*
359 * These manually annotated reachable checks are needed for GCC 4.4,
360 * where the Linux unreachable() macro isn't supported. In that case
361 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
362 * not a dead end.
363 */
364 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
365 if (!sec)
366 return 0;
367
368 list_for_each_entry(rela, &sec->rela_list, list) {
369 if (rela->sym->type != STT_SECTION) {
370 WARN("unexpected relocation symbol type in %s", sec->name);
371 return -1;
372 }
373 insn = find_insn(file, rela->sym->sec, rela->addend);
374 if (insn)
375 insn = list_prev_entry(insn, list);
376 else if (rela->addend == rela->sym->sec->len) {
377 found = false;
378 list_for_each_entry_reverse(insn, &file->insn_list, list) {
379 if (insn->sec == rela->sym->sec) {
380 found = true;
381 break;
382 }
383 }
384
385 if (!found) {
386 WARN("can't find reachable insn at %s+0x%x",
387 rela->sym->sec->name, rela->addend);
388 return -1;
389 }
390 } else {
391 WARN("can't find reachable insn at %s+0x%x",
392 rela->sym->sec->name, rela->addend);
393 return -1;
394 }
395
396 insn->dead_end = false;
397 }
398
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500399 return 0;
400}
401
402/*
403 * Warnings shouldn't be reported for ignored functions.
404 */
405static void add_ignores(struct objtool_file *file)
406{
407 struct instruction *insn;
408 struct section *sec;
409 struct symbol *func;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100410 struct rela *rela;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500411
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100412 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
413 if (!sec)
414 return;
415
416 list_for_each_entry(rela, &sec->rela_list, list) {
417 switch (rela->sym->type) {
418 case STT_FUNC:
419 func = rela->sym;
420 break;
421
422 case STT_SECTION:
423 func = find_symbol_by_offset(rela->sym->sec, rela->addend);
424 if (!func || func->type != STT_FUNC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500425 continue;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100426 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500427
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100428 default:
429 WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type);
430 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500431 }
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100432
433 func_for_each_insn_all(file, func, insn)
434 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500435 }
436}
437
438/*
Peter Zijlstraea242132019-02-25 12:50:09 +0100439 * This is a whitelist of functions that is allowed to be called with AC set.
440 * The list is meant to be minimal and only contains compiler instrumentation
441 * ABI and a few functions used to implement *_{to,from}_user() functions.
442 *
443 * These functions must not directly change AC, but may PUSHF/POPF.
444 */
445static const char *uaccess_safe_builtin[] = {
446 /* KASAN */
447 "kasan_report",
448 "check_memory_region",
449 /* KASAN out-of-line */
450 "__asan_loadN_noabort",
451 "__asan_load1_noabort",
452 "__asan_load2_noabort",
453 "__asan_load4_noabort",
454 "__asan_load8_noabort",
455 "__asan_load16_noabort",
456 "__asan_storeN_noabort",
457 "__asan_store1_noabort",
458 "__asan_store2_noabort",
459 "__asan_store4_noabort",
460 "__asan_store8_noabort",
461 "__asan_store16_noabort",
462 /* KASAN in-line */
463 "__asan_report_load_n_noabort",
464 "__asan_report_load1_noabort",
465 "__asan_report_load2_noabort",
466 "__asan_report_load4_noabort",
467 "__asan_report_load8_noabort",
468 "__asan_report_load16_noabort",
469 "__asan_report_store_n_noabort",
470 "__asan_report_store1_noabort",
471 "__asan_report_store2_noabort",
472 "__asan_report_store4_noabort",
473 "__asan_report_store8_noabort",
474 "__asan_report_store16_noabort",
475 /* KCOV */
476 "write_comp_data",
477 "__sanitizer_cov_trace_pc",
478 "__sanitizer_cov_trace_const_cmp1",
479 "__sanitizer_cov_trace_const_cmp2",
480 "__sanitizer_cov_trace_const_cmp4",
481 "__sanitizer_cov_trace_const_cmp8",
482 "__sanitizer_cov_trace_cmp1",
483 "__sanitizer_cov_trace_cmp2",
484 "__sanitizer_cov_trace_cmp4",
485 "__sanitizer_cov_trace_cmp8",
486 /* UBSAN */
487 "ubsan_type_mismatch_common",
488 "__ubsan_handle_type_mismatch",
489 "__ubsan_handle_type_mismatch_v1",
Peter Zijlstra9a50dca2019-10-21 15:11:49 +0200490 "__ubsan_handle_shift_out_of_bounds",
Peter Zijlstraea242132019-02-25 12:50:09 +0100491 /* misc */
492 "csum_partial_copy_generic",
493 "__memcpy_mcsafe",
Josh Poimboeufa7e47f22019-07-17 20:36:46 -0500494 "mcsafe_handle_tail",
Peter Zijlstraea242132019-02-25 12:50:09 +0100495 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
496 NULL
497};
498
499static void add_uaccess_safe(struct objtool_file *file)
500{
501 struct symbol *func;
502 const char **name;
503
504 if (!uaccess)
505 return;
506
507 for (name = uaccess_safe_builtin; *name; name++) {
508 func = find_symbol_by_name(file->elf, *name);
509 if (!func)
510 continue;
511
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500512 func->uaccess_safe = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500513 }
514}
515
516/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000517 * FIXME: For now, just ignore any alternatives which add retpolines. This is
518 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
519 * But it at least allows objtool to understand the control flow *around* the
520 * retpoline.
521 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100522static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000523{
524 struct section *sec;
525 struct rela *rela;
526 struct instruction *insn;
527
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100528 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000529 if (!sec)
530 return 0;
531
532 list_for_each_entry(rela, &sec->rela_list, list) {
533 if (rela->sym->type != STT_SECTION) {
534 WARN("unexpected relocation symbol type in %s", sec->name);
535 return -1;
536 }
537
538 insn = find_insn(file, rela->sym->sec, rela->addend);
539 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100540 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000541 return -1;
542 }
543
544 insn->ignore_alts = true;
545 }
546
547 return 0;
548}
549
550/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500551 * Find the destination instructions for all jumps.
552 */
553static int add_jump_destinations(struct objtool_file *file)
554{
555 struct instruction *insn;
556 struct rela *rela;
557 struct section *dest_sec;
558 unsigned long dest_off;
559
560 for_each_insn(file, insn) {
Josh Poimboeufa2296142020-02-10 12:32:39 -0600561 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500562 continue;
563
Josh Poimboeufe6da9562019-05-13 12:01:31 -0500564 if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500565 continue;
566
567 rela = find_rela_by_dest_range(insn->sec, insn->offset,
568 insn->len);
569 if (!rela) {
570 dest_sec = insn->sec;
571 dest_off = insn->offset + insn->len + insn->immediate;
572 } else if (rela->sym->type == STT_SECTION) {
573 dest_sec = rela->sym->sec;
574 dest_off = rela->addend + 4;
575 } else if (rela->sym->sec->idx) {
576 dest_sec = rela->sym->sec;
577 dest_off = rela->sym->sym.st_value + rela->addend + 4;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000578 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
579 /*
580 * Retpoline jumps are really dynamic jumps in
581 * disguise, so convert them accordingly.
582 */
Josh Poimboeufb68b9902019-07-17 20:36:57 -0500583 if (insn->type == INSN_JUMP_UNCONDITIONAL)
584 insn->type = INSN_JUMP_DYNAMIC;
585 else
586 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
587
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100588 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000589 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500590 } else {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500591 /* external sibling call */
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100592 insn->call_dest = rela->sym;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500593 continue;
594 }
595
596 insn->jump_dest = find_insn(file, dest_sec, dest_off);
597 if (!insn->jump_dest) {
598
599 /*
600 * This is a special case where an alt instruction
601 * jumps past the end of the section. These are
602 * handled later in handle_group_alt().
603 */
604 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
605 continue;
606
607 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
608 insn->sec, insn->offset, dest_sec->name,
609 dest_off);
610 return -1;
611 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500612
613 /*
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100614 * Cross-function jump.
Josh Poimboeufcd778492018-06-01 07:23:51 -0500615 */
616 if (insn->func && insn->jump_dest->func &&
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100617 insn->func != insn->jump_dest->func) {
618
619 /*
620 * For GCC 8+, create parent/child links for any cold
621 * subfunctions. This is _mostly_ redundant with a
622 * similar initialization in read_symbols().
623 *
624 * If a function has aliases, we want the *first* such
625 * function in the symbol table to be the subfunction's
626 * parent. In that case we overwrite the
627 * initialization done in read_symbols().
628 *
629 * However this code can't completely replace the
630 * read_symbols() code because this doesn't detect the
631 * case where the parent function's only reference to a
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500632 * subfunction is through a jump table.
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100633 */
634 if (!strstr(insn->func->name, ".cold.") &&
635 strstr(insn->jump_dest->func->name, ".cold.")) {
636 insn->func->cfunc = insn->jump_dest->func;
637 insn->jump_dest->func->pfunc = insn->func;
638
639 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
640 insn->jump_dest->offset == insn->jump_dest->func->offset) {
641
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500642 /* internal sibling call */
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100643 insn->call_dest = insn->jump_dest->func;
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100644 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500645 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500646 }
647
648 return 0;
649}
650
651/*
652 * Find the destination instructions for all calls.
653 */
654static int add_call_destinations(struct objtool_file *file)
655{
656 struct instruction *insn;
657 unsigned long dest_off;
658 struct rela *rela;
659
660 for_each_insn(file, insn) {
661 if (insn->type != INSN_CALL)
662 continue;
663
664 rela = find_rela_by_dest_range(insn->sec, insn->offset,
665 insn->len);
666 if (!rela) {
667 dest_off = insn->offset + insn->len + insn->immediate;
668 insn->call_dest = find_symbol_by_offset(insn->sec,
669 dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600670
671 if (!insn->call_dest && !insn->ignore) {
672 WARN_FUNC("unsupported intra-function call",
673 insn->sec, insn->offset);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100674 if (retpoline)
675 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 -0500676 return -1;
677 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600678
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500679 } else if (rela->sym->type == STT_SECTION) {
680 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
681 rela->addend+4);
682 if (!insn->call_dest ||
683 insn->call_dest->type != STT_FUNC) {
684 WARN_FUNC("can't find call dest symbol at %s+0x%x",
685 insn->sec, insn->offset,
686 rela->sym->sec->name,
687 rela->addend + 4);
688 return -1;
689 }
690 } else
691 insn->call_dest = rela->sym;
692 }
693
694 return 0;
695}
696
697/*
698 * The .alternatives section requires some extra special care, over and above
699 * what other special sections require:
700 *
701 * 1. Because alternatives are patched in-place, we need to insert a fake jump
702 * instruction at the end so that validate_branch() skips all the original
703 * replaced instructions when validating the new instruction path.
704 *
705 * 2. An added wrinkle is that the new instruction length might be zero. In
706 * that case the old instructions are replaced with noops. We simulate that
707 * by creating a fake jump as the only new instruction.
708 *
709 * 3. In some cases, the alternative section includes an instruction which
710 * conditionally jumps to the _end_ of the entry. We have to modify these
711 * jumps' destinations to point back to .text rather than the end of the
712 * entry in .altinstr_replacement.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500713 */
714static int handle_group_alt(struct objtool_file *file,
715 struct special_alt *special_alt,
716 struct instruction *orig_insn,
717 struct instruction **new_insn)
718{
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600719 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500720 unsigned long dest_off;
721
722 last_orig_insn = NULL;
723 insn = orig_insn;
724 sec_for_each_insn_from(file, insn) {
725 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
726 break;
727
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500728 insn->alt_group = true;
729 last_orig_insn = insn;
730 }
731
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600732 if (next_insn_same_sec(file, last_orig_insn)) {
733 fake_jump = malloc(sizeof(*fake_jump));
734 if (!fake_jump) {
735 WARN("malloc failed");
736 return -1;
737 }
738 memset(fake_jump, 0, sizeof(*fake_jump));
739 INIT_LIST_HEAD(&fake_jump->alts);
740 clear_insn_state(&fake_jump->state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500741
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600742 fake_jump->sec = special_alt->new_sec;
Josh Poimboeufe6da9562019-05-13 12:01:31 -0500743 fake_jump->offset = FAKE_JUMP_OFFSET;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600744 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
745 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
Josh Poimboeufe6da9562019-05-13 12:01:31 -0500746 fake_jump->func = orig_insn->func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500747 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500748
749 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600750 if (!fake_jump) {
751 WARN("%s: empty alternative at end of section",
752 special_alt->orig_sec->name);
753 return -1;
754 }
755
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500756 *new_insn = fake_jump;
757 return 0;
758 }
759
760 last_new_insn = NULL;
761 insn = *new_insn;
762 sec_for_each_insn_from(file, insn) {
763 if (insn->offset >= special_alt->new_off + special_alt->new_len)
764 break;
765
766 last_new_insn = insn;
767
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600768 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +0100769 insn->func = orig_insn->func;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600770
Josh Poimboeufdc419722020-02-10 12:32:40 -0600771 /*
772 * Since alternative replacement code is copy/pasted by the
773 * kernel after applying relocations, generally such code can't
774 * have relative-address relocation references to outside the
775 * .altinstr_replacement section, unless the arch's
776 * alternatives code can adjust the relative offsets
777 * accordingly.
778 *
779 * The x86 alternatives code adjusts the offsets only when it
780 * encounters a branch instruction at the very beginning of the
781 * replacement group.
782 */
783 if ((insn->offset != special_alt->new_off ||
784 (insn->type != INSN_CALL && !is_static_jump(insn))) &&
785 find_rela_by_dest_range(insn->sec, insn->offset, insn->len)) {
786
787 WARN_FUNC("unsupported relocation in alternatives section",
788 insn->sec, insn->offset);
789 return -1;
790 }
791
Josh Poimboeufa2296142020-02-10 12:32:39 -0600792 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500793 continue;
794
795 if (!insn->immediate)
796 continue;
797
798 dest_off = insn->offset + insn->len + insn->immediate;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600799 if (dest_off == special_alt->new_off + special_alt->new_len) {
800 if (!fake_jump) {
801 WARN("%s: alternative jump to end of section",
802 special_alt->orig_sec->name);
803 return -1;
804 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500805 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600806 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500807
808 if (!insn->jump_dest) {
809 WARN_FUNC("can't find alternative jump destination",
810 insn->sec, insn->offset);
811 return -1;
812 }
813 }
814
815 if (!last_new_insn) {
816 WARN_FUNC("can't find last new alternative instruction",
817 special_alt->new_sec, special_alt->new_off);
818 return -1;
819 }
820
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600821 if (fake_jump)
822 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500823
824 return 0;
825}
826
827/*
828 * A jump table entry can either convert a nop to a jump or a jump to a nop.
829 * If the original instruction is a jump, make the alt entry an effective nop
830 * by just skipping the original instruction.
831 */
832static int handle_jump_alt(struct objtool_file *file,
833 struct special_alt *special_alt,
834 struct instruction *orig_insn,
835 struct instruction **new_insn)
836{
837 if (orig_insn->type == INSN_NOP)
838 return 0;
839
840 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
841 WARN_FUNC("unsupported instruction at jump label",
842 orig_insn->sec, orig_insn->offset);
843 return -1;
844 }
845
846 *new_insn = list_next_entry(orig_insn, list);
847 return 0;
848}
849
850/*
851 * Read all the special sections which have alternate instructions which can be
852 * patched in or redirected to at runtime. Each instruction having alternate
853 * instruction(s) has them added to its insn->alts list, which will be
854 * traversed in validate_branch().
855 */
856static int add_special_section_alts(struct objtool_file *file)
857{
858 struct list_head special_alts;
859 struct instruction *orig_insn, *new_insn;
860 struct special_alt *special_alt, *tmp;
861 struct alternative *alt;
862 int ret;
863
864 ret = special_get_alts(file->elf, &special_alts);
865 if (ret)
866 return ret;
867
868 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500869
870 orig_insn = find_insn(file, special_alt->orig_sec,
871 special_alt->orig_off);
872 if (!orig_insn) {
873 WARN_FUNC("special: can't find orig instruction",
874 special_alt->orig_sec, special_alt->orig_off);
875 ret = -1;
876 goto out;
877 }
878
879 new_insn = NULL;
880 if (!special_alt->group || special_alt->new_len) {
881 new_insn = find_insn(file, special_alt->new_sec,
882 special_alt->new_off);
883 if (!new_insn) {
884 WARN_FUNC("special: can't find new instruction",
885 special_alt->new_sec,
886 special_alt->new_off);
887 ret = -1;
888 goto out;
889 }
890 }
891
892 if (special_alt->group) {
893 ret = handle_group_alt(file, special_alt, orig_insn,
894 &new_insn);
895 if (ret)
896 goto out;
897 } else if (special_alt->jump_or_nop) {
898 ret = handle_jump_alt(file, special_alt, orig_insn,
899 &new_insn);
900 if (ret)
901 goto out;
902 }
903
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000904 alt = malloc(sizeof(*alt));
905 if (!alt) {
906 WARN("malloc failed");
907 ret = -1;
908 goto out;
909 }
910
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500911 alt->insn = new_insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +0100912 alt->skip_orig = special_alt->skip_orig;
Peter Zijlstraea242132019-02-25 12:50:09 +0100913 orig_insn->ignore_alts |= special_alt->skip_alt;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500914 list_add_tail(&alt->list, &orig_insn->alts);
915
916 list_del(&special_alt->list);
917 free(special_alt);
918 }
919
920out:
921 return ret;
922}
923
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500924static int add_jump_table(struct objtool_file *file, struct instruction *insn,
Jann Hornbd98c812019-07-17 20:36:54 -0500925 struct rela *table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500926{
927 struct rela *rela = table;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500928 struct instruction *dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500929 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500930 struct symbol *pfunc = insn->func->pfunc;
931 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500932
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500933 /*
934 * Each @rela is a switch table relocation which points to the target
935 * instruction.
936 */
937 list_for_each_entry_from(rela, &table->sec->rela_list, list) {
Jann Hornbd98c812019-07-17 20:36:54 -0500938
939 /* Check for the end of the table: */
940 if (rela != table && rela->jump_table_start)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500941 break;
942
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500943 /* Make sure the table entries are consecutive: */
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500944 if (prev_offset && rela->offset != prev_offset + 8)
945 break;
946
947 /* Detect function pointers from contiguous objects: */
948 if (rela->sym->sec == pfunc->sec &&
949 rela->addend == pfunc->offset)
950 break;
951
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500952 dest_insn = find_insn(file, rela->sym->sec, rela->addend);
953 if (!dest_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500954 break;
955
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500956 /* Make sure the destination is in the same function: */
Josh Poimboeufe65050b2019-07-17 20:36:55 -0500957 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -0500958 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500959
960 alt = malloc(sizeof(*alt));
961 if (!alt) {
962 WARN("malloc failed");
963 return -1;
964 }
965
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500966 alt->insn = dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500967 list_add_tail(&alt->list, &insn->alts);
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500968 prev_offset = rela->offset;
969 }
970
971 if (!prev_offset) {
972 WARN_FUNC("can't find switch jump table",
973 insn->sec, insn->offset);
974 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500975 }
976
977 return 0;
978}
979
980/*
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500981 * find_jump_table() - Given a dynamic jump, find the switch jump table in
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500982 * .rodata associated with it.
983 *
984 * There are 3 basic patterns:
985 *
986 * 1. jmpq *[rodata addr](,%reg,8)
987 *
988 * This is the most common case by far. It jumps to an address in a simple
989 * jump table which is stored in .rodata.
990 *
991 * 2. jmpq *[rodata addr](%rip)
992 *
993 * This is caused by a rare GCC quirk, currently only seen in three driver
994 * functions in the kernel, only with certain obscure non-distro configs.
995 *
996 * As part of an optimization, GCC makes a copy of an existing switch jump
997 * table, modifies it, and then hard-codes the jump (albeit with an indirect
998 * jump) to use a single entry in the table. The rest of the jump table and
999 * some of its jump targets remain as dead code.
1000 *
1001 * In such a case we can just crudely ignore all unreachable instruction
1002 * warnings for the entire object file. Ideally we would just ignore them
1003 * for the function, but that would require redesigning the code quite a
1004 * bit. And honestly that's just not worth doing: unreachable instruction
1005 * warnings are of questionable value anyway, and this is such a rare issue.
1006 *
1007 * 3. mov [rodata addr],%reg1
1008 * ... some instructions ...
1009 * jmpq *(%reg1,%reg2,8)
1010 *
1011 * This is a fairly uncommon pattern which is new for GCC 6. As of this
1012 * writing, there are 11 occurrences of it in the allmodconfig kernel.
1013 *
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001014 * As of GCC 7 there are quite a few more of these and the 'in between' code
1015 * is significant. Esp. with KASAN enabled some of the code between the mov
1016 * and jmpq uses .rodata itself, which can confuse things.
1017 *
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001018 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1019 * ensure the same register is used in the mov and jump instructions.
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001020 *
1021 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001022 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001023static struct rela *find_jump_table(struct objtool_file *file,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001024 struct symbol *func,
1025 struct instruction *insn)
1026{
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001027 struct rela *text_rela, *table_rela;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001028 struct instruction *orig_insn = insn;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001029 struct section *table_sec;
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -05001030 unsigned long table_offset;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001031
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001032 /*
1033 * Backward search using the @first_jump_src links, these help avoid
1034 * much of the 'in between' code. Which avoids us getting confused by
1035 * it.
1036 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001037 for (;
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001038 &insn->list != &file->insn_list &&
1039 insn->sec == func->sec &&
1040 insn->offset >= func->offset;
1041
1042 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
1043
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001044 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001045 break;
1046
1047 /* allow small jumps within the range */
1048 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1049 insn->jump_dest &&
1050 (insn->jump_dest->offset <= insn->offset ||
1051 insn->jump_dest->offset > orig_insn->offset))
1052 break;
1053
1054 /* look for a relocation which references .rodata */
1055 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
1056 insn->len);
Allan Xavier4a60aa02018-09-07 08:12:01 -05001057 if (!text_rela || text_rela->sym->type != STT_SECTION ||
1058 !text_rela->sym->sec->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001059 continue;
1060
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -05001061 table_offset = text_rela->addend;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001062 table_sec = text_rela->sym->sec;
Allan Xavier4a60aa02018-09-07 08:12:01 -05001063
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -05001064 if (text_rela->type == R_X86_64_PC32)
1065 table_offset += 4;
1066
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001067 /*
1068 * Make sure the .rodata address isn't associated with a
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001069 * symbol. GCC jump tables are anonymous data.
1070 *
1071 * Also support C jump tables which are in the same format as
1072 * switch jump tables. For objtool to recognize them, they
1073 * need to be placed in the C_JUMP_TABLE_SECTION section. They
1074 * have symbols associated with them.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001075 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001076 if (find_symbol_containing(table_sec, table_offset) &&
1077 strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001078 continue;
1079
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001080 /* Each table entry has a rela associated with it. */
1081 table_rela = find_rela_by_dest(table_sec, table_offset);
1082 if (!table_rela)
1083 continue;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001084
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001085 /*
1086 * Use of RIP-relative switch jumps is quite rare, and
1087 * indicates a rare GCC quirk/bug which can leave dead code
1088 * behind.
1089 */
1090 if (text_rela->type == R_X86_64_PC32)
1091 file->ignore_unreachables = true;
1092
1093 return table_rela;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001094 }
1095
1096 return NULL;
1097}
1098
Jann Hornbd98c812019-07-17 20:36:54 -05001099/*
1100 * First pass: Mark the head of each jump table so that in the next pass,
1101 * we know when a given jump table ends and the next one starts.
1102 */
1103static void mark_func_jump_tables(struct objtool_file *file,
1104 struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001105{
Jann Hornbd98c812019-07-17 20:36:54 -05001106 struct instruction *insn, *last = NULL;
1107 struct rela *rela;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001108
Josh Poimboeuf13810432018-05-09 22:39:15 -05001109 func_for_each_insn_all(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001110 if (!last)
1111 last = insn;
1112
1113 /*
1114 * Store back-pointers for unconditional forward jumps such
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001115 * that find_jump_table() can back-track using those and
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001116 * avoid some potentially confusing code.
1117 */
1118 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1119 insn->offset > last->offset &&
1120 insn->jump_dest->offset > insn->offset &&
1121 !insn->jump_dest->first_jump_src) {
1122
1123 insn->jump_dest->first_jump_src = insn;
1124 last = insn->jump_dest;
1125 }
1126
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001127 if (insn->type != INSN_JUMP_DYNAMIC)
1128 continue;
1129
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001130 rela = find_jump_table(file, func, insn);
Jann Hornbd98c812019-07-17 20:36:54 -05001131 if (rela) {
1132 rela->jump_table_start = true;
1133 insn->jump_table = rela;
1134 }
1135 }
1136}
1137
1138static int add_func_jump_tables(struct objtool_file *file,
1139 struct symbol *func)
1140{
1141 struct instruction *insn;
1142 int ret;
1143
1144 func_for_each_insn_all(file, func, insn) {
1145 if (!insn->jump_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001146 continue;
1147
Jann Hornbd98c812019-07-17 20:36:54 -05001148 ret = add_jump_table(file, insn, insn->jump_table);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001149 if (ret)
1150 return ret;
1151 }
1152
1153 return 0;
1154}
1155
1156/*
1157 * For some switch statements, gcc generates a jump table in the .rodata
1158 * section which contains a list of addresses within the function to jump to.
1159 * This finds these jump tables and adds them to the insn->alts lists.
1160 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001161static int add_jump_table_alts(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001162{
1163 struct section *sec;
1164 struct symbol *func;
1165 int ret;
1166
Allan Xavier4a60aa02018-09-07 08:12:01 -05001167 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001168 return 0;
1169
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001170 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001171 list_for_each_entry(func, &sec->symbol_list, list) {
1172 if (func->type != STT_FUNC)
1173 continue;
1174
Jann Hornbd98c812019-07-17 20:36:54 -05001175 mark_func_jump_tables(file, func);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001176 ret = add_func_jump_tables(file, func);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001177 if (ret)
1178 return ret;
1179 }
1180 }
1181
1182 return 0;
1183}
1184
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001185static int read_unwind_hints(struct objtool_file *file)
1186{
1187 struct section *sec, *relasec;
1188 struct rela *rela;
1189 struct unwind_hint *hint;
1190 struct instruction *insn;
1191 struct cfi_reg *cfa;
1192 int i;
1193
1194 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1195 if (!sec)
1196 return 0;
1197
1198 relasec = sec->rela;
1199 if (!relasec) {
1200 WARN("missing .rela.discard.unwind_hints section");
1201 return -1;
1202 }
1203
1204 if (sec->len % sizeof(struct unwind_hint)) {
1205 WARN("struct unwind_hint size mismatch");
1206 return -1;
1207 }
1208
1209 file->hints = true;
1210
1211 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1212 hint = (struct unwind_hint *)sec->data->d_buf + i;
1213
1214 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1215 if (!rela) {
1216 WARN("can't find rela for unwind_hints[%d]", i);
1217 return -1;
1218 }
1219
1220 insn = find_insn(file, rela->sym->sec, rela->addend);
1221 if (!insn) {
1222 WARN("can't find insn for unwind_hints[%d]", i);
1223 return -1;
1224 }
1225
1226 cfa = &insn->state.cfa;
1227
1228 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1229 insn->save = true;
1230 continue;
1231
1232 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1233 insn->restore = true;
1234 insn->hint = true;
1235 continue;
1236 }
1237
1238 insn->hint = true;
1239
1240 switch (hint->sp_reg) {
1241 case ORC_REG_UNDEFINED:
1242 cfa->base = CFI_UNDEFINED;
1243 break;
1244 case ORC_REG_SP:
1245 cfa->base = CFI_SP;
1246 break;
1247 case ORC_REG_BP:
1248 cfa->base = CFI_BP;
1249 break;
1250 case ORC_REG_SP_INDIRECT:
1251 cfa->base = CFI_SP_INDIRECT;
1252 break;
1253 case ORC_REG_R10:
1254 cfa->base = CFI_R10;
1255 break;
1256 case ORC_REG_R13:
1257 cfa->base = CFI_R13;
1258 break;
1259 case ORC_REG_DI:
1260 cfa->base = CFI_DI;
1261 break;
1262 case ORC_REG_DX:
1263 cfa->base = CFI_DX;
1264 break;
1265 default:
1266 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1267 insn->sec, insn->offset, hint->sp_reg);
1268 return -1;
1269 }
1270
1271 cfa->offset = hint->sp_offset;
1272 insn->state.type = hint->type;
Josh Poimboeufd31a5802018-05-18 08:47:12 +02001273 insn->state.end = hint->end;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001274 }
1275
1276 return 0;
1277}
1278
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001279static int read_retpoline_hints(struct objtool_file *file)
1280{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001281 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001282 struct instruction *insn;
1283 struct rela *rela;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001284
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001285 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001286 if (!sec)
1287 return 0;
1288
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001289 list_for_each_entry(rela, &sec->rela_list, list) {
1290 if (rela->sym->type != STT_SECTION) {
1291 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001292 return -1;
1293 }
1294
1295 insn = find_insn(file, rela->sym->sec, rela->addend);
1296 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001297 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001298 return -1;
1299 }
1300
1301 if (insn->type != INSN_JUMP_DYNAMIC &&
1302 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001303 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001304 insn->sec, insn->offset);
1305 return -1;
1306 }
1307
1308 insn->retpoline_safe = true;
1309 }
1310
1311 return 0;
1312}
1313
Allan Xavier4a60aa02018-09-07 08:12:01 -05001314static void mark_rodata(struct objtool_file *file)
1315{
1316 struct section *sec;
1317 bool found = false;
1318
1319 /*
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001320 * Search for the following rodata sections, each of which can
1321 * potentially contain jump tables:
1322 *
1323 * - .rodata: can contain GCC switch tables
1324 * - .rodata.<func>: same, if -fdata-sections is being used
1325 * - .rodata..c_jump_table: contains C annotated jump tables
1326 *
1327 * .rodata.str1.* sections are ignored; they don't contain jump tables.
Allan Xavier4a60aa02018-09-07 08:12:01 -05001328 */
1329 for_each_sec(file, sec) {
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001330 if ((!strncmp(sec->name, ".rodata", 7) && !strstr(sec->name, ".str1.")) ||
1331 !strcmp(sec->name, C_JUMP_TABLE_SECTION)) {
Allan Xavier4a60aa02018-09-07 08:12:01 -05001332 sec->rodata = true;
1333 found = true;
1334 }
1335 }
1336
1337 file->rodata = found;
1338}
1339
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001340static int decode_sections(struct objtool_file *file)
1341{
1342 int ret;
1343
Allan Xavier4a60aa02018-09-07 08:12:01 -05001344 mark_rodata(file);
1345
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001346 ret = decode_instructions(file);
1347 if (ret)
1348 return ret;
1349
1350 ret = add_dead_ends(file);
1351 if (ret)
1352 return ret;
1353
1354 add_ignores(file);
Peter Zijlstraea242132019-02-25 12:50:09 +01001355 add_uaccess_safe(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001356
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001357 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001358 if (ret)
1359 return ret;
1360
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001361 ret = add_jump_destinations(file);
1362 if (ret)
1363 return ret;
1364
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001365 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001366 if (ret)
1367 return ret;
1368
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001369 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001370 if (ret)
1371 return ret;
1372
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001373 ret = add_jump_table_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001374 if (ret)
1375 return ret;
1376
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001377 ret = read_unwind_hints(file);
1378 if (ret)
1379 return ret;
1380
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001381 ret = read_retpoline_hints(file);
1382 if (ret)
1383 return ret;
1384
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001385 return 0;
1386}
1387
1388static bool is_fentry_call(struct instruction *insn)
1389{
1390 if (insn->type == INSN_CALL &&
1391 insn->call_dest->type == STT_NOTYPE &&
1392 !strcmp(insn->call_dest->name, "__fentry__"))
1393 return true;
1394
1395 return false;
1396}
1397
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001398static bool has_modified_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001399{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001400 int i;
1401
1402 if (state->cfa.base != initial_func_cfi.cfa.base ||
1403 state->cfa.offset != initial_func_cfi.cfa.offset ||
1404 state->stack_size != initial_func_cfi.cfa.offset ||
1405 state->drap)
1406 return true;
1407
1408 for (i = 0; i < CFI_NUM_REGS; i++)
1409 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1410 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1411 return true;
1412
1413 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001414}
1415
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001416static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001417{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001418 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1419 state->regs[CFI_BP].offset == -16)
1420 return true;
1421
1422 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1423 return true;
1424
1425 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001426}
1427
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001428static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1429{
1430 struct cfi_reg *cfa = &state->cfa;
1431 struct stack_op *op = &insn->stack_op;
1432
1433 if (cfa->base != CFI_SP)
1434 return 0;
1435
1436 /* push */
Peter Zijlstraea242132019-02-25 12:50:09 +01001437 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001438 cfa->offset += 8;
1439
1440 /* pop */
Peter Zijlstraea242132019-02-25 12:50:09 +01001441 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001442 cfa->offset -= 8;
1443
1444 /* add immediate to sp */
1445 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1446 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1447 cfa->offset -= op->src.offset;
1448
1449 return 0;
1450}
1451
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001452static void save_reg(struct insn_state *state, unsigned char reg, int base,
1453 int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001454{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001455 if (arch_callee_saved_reg(reg) &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001456 state->regs[reg].base == CFI_UNDEFINED) {
1457 state->regs[reg].base = base;
1458 state->regs[reg].offset = offset;
1459 }
1460}
1461
1462static void restore_reg(struct insn_state *state, unsigned char reg)
1463{
1464 state->regs[reg].base = CFI_UNDEFINED;
1465 state->regs[reg].offset = 0;
1466}
1467
1468/*
1469 * A note about DRAP stack alignment:
1470 *
1471 * GCC has the concept of a DRAP register, which is used to help keep track of
1472 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1473 * register. The typical DRAP pattern is:
1474 *
1475 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1476 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1477 * 41 ff 72 f8 pushq -0x8(%r10)
1478 * 55 push %rbp
1479 * 48 89 e5 mov %rsp,%rbp
1480 * (more pushes)
1481 * 41 52 push %r10
1482 * ...
1483 * 41 5a pop %r10
1484 * (more pops)
1485 * 5d pop %rbp
1486 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1487 * c3 retq
1488 *
1489 * There are some variations in the epilogues, like:
1490 *
1491 * 5b pop %rbx
1492 * 41 5a pop %r10
1493 * 41 5c pop %r12
1494 * 41 5d pop %r13
1495 * 41 5e pop %r14
1496 * c9 leaveq
1497 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1498 * c3 retq
1499 *
1500 * and:
1501 *
1502 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1503 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1504 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1505 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1506 * c9 leaveq
1507 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1508 * c3 retq
1509 *
1510 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1511 * restored beforehand:
1512 *
1513 * 41 55 push %r13
1514 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1515 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1516 * ...
1517 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1518 * 41 5d pop %r13
1519 * c3 retq
1520 */
1521static int update_insn_state(struct instruction *insn, struct insn_state *state)
1522{
1523 struct stack_op *op = &insn->stack_op;
1524 struct cfi_reg *cfa = &state->cfa;
1525 struct cfi_reg *regs = state->regs;
1526
1527 /* stack operations don't make sense with an undefined CFA */
1528 if (cfa->base == CFI_UNDEFINED) {
1529 if (insn->func) {
1530 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1531 return -1;
1532 }
1533 return 0;
1534 }
1535
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001536 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1537 return update_insn_state_regs(insn, state);
1538
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001539 switch (op->dest.type) {
1540
1541 case OP_DEST_REG:
1542 switch (op->src.type) {
1543
1544 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001545 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1546 cfa->base == CFI_SP &&
1547 regs[CFI_BP].base == CFI_CFA &&
1548 regs[CFI_BP].offset == -cfa->offset) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001549
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001550 /* mov %rsp, %rbp */
1551 cfa->base = op->dest.reg;
1552 state->bp_scratch = false;
1553 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001554
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001555 else if (op->src.reg == CFI_SP &&
1556 op->dest.reg == CFI_BP && state->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001557
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001558 /* drap: mov %rsp, %rbp */
1559 regs[CFI_BP].base = CFI_BP;
1560 regs[CFI_BP].offset = -state->stack_size;
1561 state->bp_scratch = false;
1562 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001563
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001564 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1565
1566 /*
1567 * mov %rsp, %reg
1568 *
1569 * This is needed for the rare case where GCC
1570 * does:
1571 *
1572 * mov %rsp, %rax
1573 * ...
1574 * mov %rax, %rsp
1575 */
1576 state->vals[op->dest.reg].base = CFI_CFA;
1577 state->vals[op->dest.reg].offset = -state->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001578 }
1579
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001580 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1581 cfa->base == CFI_BP) {
1582
1583 /*
1584 * mov %rbp, %rsp
1585 *
1586 * Restore the original stack pointer (Clang).
1587 */
1588 state->stack_size = -state->regs[CFI_BP].offset;
1589 }
1590
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001591 else if (op->dest.reg == cfa->base) {
1592
1593 /* mov %reg, %rsp */
1594 if (cfa->base == CFI_SP &&
1595 state->vals[op->src.reg].base == CFI_CFA) {
1596
1597 /*
1598 * This is needed for the rare case
1599 * where GCC does something dumb like:
1600 *
1601 * lea 0x8(%rsp), %rcx
1602 * ...
1603 * mov %rcx, %rsp
1604 */
1605 cfa->offset = -state->vals[op->src.reg].offset;
1606 state->stack_size = cfa->offset;
1607
1608 } else {
1609 cfa->base = CFI_UNDEFINED;
1610 cfa->offset = 0;
1611 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001612 }
1613
1614 break;
1615
1616 case OP_SRC_ADD:
1617 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1618
1619 /* add imm, %rsp */
1620 state->stack_size -= op->src.offset;
1621 if (cfa->base == CFI_SP)
1622 cfa->offset -= op->src.offset;
1623 break;
1624 }
1625
1626 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1627
1628 /* lea disp(%rbp), %rsp */
1629 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1630 break;
1631 }
1632
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001633 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001634
1635 /* drap: lea disp(%rsp), %drap */
1636 state->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001637
1638 /*
1639 * lea disp(%rsp), %reg
1640 *
1641 * This is needed for the rare case where GCC
1642 * does something dumb like:
1643 *
1644 * lea 0x8(%rsp), %rcx
1645 * ...
1646 * mov %rcx, %rsp
1647 */
1648 state->vals[op->dest.reg].base = CFI_CFA;
1649 state->vals[op->dest.reg].offset = \
1650 -state->stack_size + op->src.offset;
1651
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001652 break;
1653 }
1654
1655 if (state->drap && op->dest.reg == CFI_SP &&
1656 op->src.reg == state->drap_reg) {
1657
1658 /* drap: lea disp(%drap), %rsp */
1659 cfa->base = CFI_SP;
1660 cfa->offset = state->stack_size = -op->src.offset;
1661 state->drap_reg = CFI_UNDEFINED;
1662 state->drap = false;
1663 break;
1664 }
1665
1666 if (op->dest.reg == state->cfa.base) {
1667 WARN_FUNC("unsupported stack register modification",
1668 insn->sec, insn->offset);
1669 return -1;
1670 }
1671
1672 break;
1673
1674 case OP_SRC_AND:
1675 if (op->dest.reg != CFI_SP ||
1676 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1677 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1678 WARN_FUNC("unsupported stack pointer realignment",
1679 insn->sec, insn->offset);
1680 return -1;
1681 }
1682
1683 if (state->drap_reg != CFI_UNDEFINED) {
1684 /* drap: and imm, %rsp */
1685 cfa->base = state->drap_reg;
1686 cfa->offset = state->stack_size = 0;
1687 state->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001688 }
1689
1690 /*
1691 * Older versions of GCC (4.8ish) realign the stack
1692 * without DRAP, with a frame pointer.
1693 */
1694
1695 break;
1696
1697 case OP_SRC_POP:
Peter Zijlstraea242132019-02-25 12:50:09 +01001698 case OP_SRC_POPF:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001699 if (!state->drap && op->dest.type == OP_DEST_REG &&
1700 op->dest.reg == cfa->base) {
1701
1702 /* pop %rbp */
1703 cfa->base = CFI_SP;
1704 }
1705
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001706 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1707 op->dest.type == OP_DEST_REG &&
1708 op->dest.reg == state->drap_reg &&
1709 state->drap_offset == -state->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001710
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001711 /* drap: pop %drap */
1712 cfa->base = state->drap_reg;
1713 cfa->offset = 0;
1714 state->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001715
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001716 } else if (regs[op->dest.reg].offset == -state->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001717
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001718 /* pop %reg */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001719 restore_reg(state, op->dest.reg);
1720 }
1721
1722 state->stack_size -= 8;
1723 if (cfa->base == CFI_SP)
1724 cfa->offset -= 8;
1725
1726 break;
1727
1728 case OP_SRC_REG_INDIRECT:
1729 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001730 op->src.offset == state->drap_offset) {
1731
1732 /* drap: mov disp(%rbp), %drap */
1733 cfa->base = state->drap_reg;
1734 cfa->offset = 0;
1735 state->drap_offset = -1;
1736 }
1737
1738 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001739 op->src.offset == regs[op->dest.reg].offset) {
1740
1741 /* drap: mov disp(%rbp), %reg */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001742 restore_reg(state, op->dest.reg);
1743
1744 } else if (op->src.reg == cfa->base &&
1745 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1746
1747 /* mov disp(%rbp), %reg */
1748 /* mov disp(%rsp), %reg */
1749 restore_reg(state, op->dest.reg);
1750 }
1751
1752 break;
1753
1754 default:
1755 WARN_FUNC("unknown stack-related instruction",
1756 insn->sec, insn->offset);
1757 return -1;
1758 }
1759
1760 break;
1761
1762 case OP_DEST_PUSH:
Peter Zijlstraea242132019-02-25 12:50:09 +01001763 case OP_DEST_PUSHF:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001764 state->stack_size += 8;
1765 if (cfa->base == CFI_SP)
1766 cfa->offset += 8;
1767
1768 if (op->src.type != OP_SRC_REG)
1769 break;
1770
1771 if (state->drap) {
1772 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1773
1774 /* drap: push %drap */
1775 cfa->base = CFI_BP_INDIRECT;
1776 cfa->offset = -state->stack_size;
1777
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001778 /* save drap so we know when to restore it */
1779 state->drap_offset = -state->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001780
1781 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1782
1783 /* drap: push %rbp */
1784 state->stack_size = 0;
1785
1786 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1787
1788 /* drap: push %reg */
1789 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1790 }
1791
1792 } else {
1793
1794 /* push %reg */
1795 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1796 }
1797
1798 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001799 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001800 cfa->base != CFI_BP)
1801 state->bp_scratch = true;
1802 break;
1803
1804 case OP_DEST_REG_INDIRECT:
1805
1806 if (state->drap) {
1807 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1808
1809 /* drap: mov %drap, disp(%rbp) */
1810 cfa->base = CFI_BP_INDIRECT;
1811 cfa->offset = op->dest.offset;
1812
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001813 /* save drap offset so we know when to restore it */
1814 state->drap_offset = op->dest.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001815 }
1816
1817 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1818
1819 /* drap: mov reg, disp(%rbp) */
1820 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1821 }
1822
1823 } else if (op->dest.reg == cfa->base) {
1824
1825 /* mov reg, disp(%rbp) */
1826 /* mov reg, disp(%rsp) */
1827 save_reg(state, op->src.reg, CFI_CFA,
1828 op->dest.offset - state->cfa.offset);
1829 }
1830
1831 break;
1832
1833 case OP_DEST_LEAVE:
1834 if ((!state->drap && cfa->base != CFI_BP) ||
1835 (state->drap && cfa->base != state->drap_reg)) {
1836 WARN_FUNC("leave instruction with modified stack frame",
1837 insn->sec, insn->offset);
1838 return -1;
1839 }
1840
1841 /* leave (mov %rbp, %rsp; pop %rbp) */
1842
1843 state->stack_size = -state->regs[CFI_BP].offset - 8;
1844 restore_reg(state, CFI_BP);
1845
1846 if (!state->drap) {
1847 cfa->base = CFI_SP;
1848 cfa->offset -= 8;
1849 }
1850
1851 break;
1852
1853 case OP_DEST_MEM:
Peter Zijlstraea242132019-02-25 12:50:09 +01001854 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001855 WARN_FUNC("unknown stack-related memory operation",
1856 insn->sec, insn->offset);
1857 return -1;
1858 }
1859
1860 /* pop mem */
1861 state->stack_size -= 8;
1862 if (cfa->base == CFI_SP)
1863 cfa->offset -= 8;
1864
1865 break;
1866
1867 default:
1868 WARN_FUNC("unknown stack-related instruction",
1869 insn->sec, insn->offset);
1870 return -1;
1871 }
1872
1873 return 0;
1874}
1875
1876static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1877{
1878 struct insn_state *state1 = &insn->state, *state2 = state;
1879 int i;
1880
1881 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1882 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1883 insn->sec, insn->offset,
1884 state1->cfa.base, state1->cfa.offset,
1885 state2->cfa.base, state2->cfa.offset);
1886
1887 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1888 for (i = 0; i < CFI_NUM_REGS; i++) {
1889 if (!memcmp(&state1->regs[i], &state2->regs[i],
1890 sizeof(struct cfi_reg)))
1891 continue;
1892
1893 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1894 insn->sec, insn->offset,
1895 i, state1->regs[i].base, state1->regs[i].offset,
1896 i, state2->regs[i].base, state2->regs[i].offset);
1897 break;
1898 }
1899
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001900 } else if (state1->type != state2->type) {
1901 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1902 insn->sec, insn->offset, state1->type, state2->type);
1903
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001904 } else if (state1->drap != state2->drap ||
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001905 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1906 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1907 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001908 insn->sec, insn->offset,
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001909 state1->drap, state1->drap_reg, state1->drap_offset,
1910 state2->drap, state2->drap_reg, state2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001911
1912 } else
1913 return true;
1914
1915 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001916}
1917
Peter Zijlstraea242132019-02-25 12:50:09 +01001918static inline bool func_uaccess_safe(struct symbol *func)
1919{
1920 if (func)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05001921 return func->uaccess_safe;
Peter Zijlstraea242132019-02-25 12:50:09 +01001922
1923 return false;
1924}
1925
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05001926static inline const char *call_dest_name(struct instruction *insn)
Peter Zijlstraea242132019-02-25 12:50:09 +01001927{
1928 if (insn->call_dest)
1929 return insn->call_dest->name;
1930
1931 return "{dynamic}";
1932}
1933
1934static int validate_call(struct instruction *insn, struct insn_state *state)
1935{
1936 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
1937 WARN_FUNC("call to %s() with UACCESS enabled",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05001938 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstraea242132019-02-25 12:50:09 +01001939 return 1;
1940 }
1941
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01001942 if (state->df) {
1943 WARN_FUNC("call to %s() with DF set",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05001944 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01001945 return 1;
1946 }
1947
Peter Zijlstraea242132019-02-25 12:50:09 +01001948 return 0;
1949}
1950
Peter Zijlstra54262aa2019-03-06 12:58:15 +01001951static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
1952{
1953 if (has_modified_stack_frame(state)) {
1954 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1955 insn->sec, insn->offset);
1956 return 1;
1957 }
1958
Peter Zijlstraea242132019-02-25 12:50:09 +01001959 return validate_call(insn, state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01001960}
1961
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001962/*
1963 * Follow the branch starting at the given instruction, and recursively follow
1964 * any other branches (jumps). Meanwhile, track the frame pointer state at
1965 * each instruction and validate all the rules described in
1966 * tools/objtool/Documentation/stack-validation.txt.
1967 */
Josh Poimboeufc705cec2019-07-17 20:36:47 -05001968static int validate_branch(struct objtool_file *file, struct symbol *func,
1969 struct instruction *first, struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001970{
1971 struct alternative *alt;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001972 struct instruction *insn, *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001973 struct section *sec;
Peter Zijlstra882a0db2019-07-24 17:47:26 -05001974 u8 visited;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001975 int ret;
1976
1977 insn = first;
1978 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001979
1980 if (insn->alt_group && list_empty(&insn->alts)) {
1981 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1982 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001983 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001984 }
1985
1986 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001987 next_insn = next_insn_same_sec(file, insn);
1988
Josh Poimboeuf13810432018-05-09 22:39:15 -05001989 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05001990 WARN("%s() falls through to next function %s()",
1991 func->name, insn->func->name);
1992 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001993 }
1994
Josh Poimboeuf48550222017-07-07 09:19:42 -05001995 if (func && insn->ignore) {
1996 WARN_FUNC("BUG: why am I validating an ignored function?",
1997 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001998 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001999 }
2000
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002001 visited = 1 << state.uaccess;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002002 if (insn->visited) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002003 if (!insn->hint && !insn_state_match(insn, &state))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002004 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002005
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002006 if (insn->visited & visited)
Peter Zijlstraea242132019-02-25 12:50:09 +01002007 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002008 }
2009
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002010 if (insn->hint) {
2011 if (insn->restore) {
2012 struct instruction *save_insn, *i;
2013
2014 i = insn;
2015 save_insn = NULL;
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002016 func_for_each_insn_continue_reverse(file, func, i) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002017 if (i->save) {
2018 save_insn = i;
2019 break;
2020 }
2021 }
2022
2023 if (!save_insn) {
2024 WARN_FUNC("no corresponding CFI save for CFI restore",
2025 sec, insn->offset);
2026 return 1;
2027 }
2028
2029 if (!save_insn->visited) {
2030 /*
2031 * Oops, no state to copy yet.
2032 * Hopefully we can reach this
2033 * instruction from another branch
2034 * after the save insn has been
2035 * visited.
2036 */
2037 if (insn == first)
2038 return 0;
2039
2040 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
2041 sec, insn->offset);
2042 return 1;
2043 }
2044
2045 insn->state = save_insn->state;
2046 }
2047
2048 state = insn->state;
2049
2050 } else
2051 insn->state = state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002052
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002053 insn->visited |= visited;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002054
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002055 if (!insn->ignore_alts) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002056 bool skip_orig = false;
2057
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002058 list_for_each_entry(alt, &insn->alts, list) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002059 if (alt->skip_orig)
2060 skip_orig = true;
2061
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002062 ret = validate_branch(file, func, alt->insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002063 if (ret) {
2064 if (backtrace)
2065 BT_FUNC("(alt)", insn);
2066 return ret;
2067 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002068 }
Peter Zijlstra764eef42019-03-01 11:19:03 +01002069
2070 if (skip_orig)
2071 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002072 }
2073
2074 switch (insn->type) {
2075
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002076 case INSN_RETURN:
Peter Zijlstraea242132019-02-25 12:50:09 +01002077 if (state.uaccess && !func_uaccess_safe(func)) {
2078 WARN_FUNC("return with UACCESS enabled", sec, insn->offset);
2079 return 1;
2080 }
2081
2082 if (!state.uaccess && func_uaccess_safe(func)) {
2083 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", sec, insn->offset);
2084 return 1;
2085 }
2086
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002087 if (state.df) {
2088 WARN_FUNC("return with DF set", sec, insn->offset);
2089 return 1;
2090 }
2091
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002092 if (func && has_modified_stack_frame(&state)) {
2093 WARN_FUNC("return with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002094 sec, insn->offset);
2095 return 1;
2096 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002097
2098 if (state.bp_scratch) {
2099 WARN("%s uses BP as a scratch register",
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002100 func->name);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002101 return 1;
2102 }
2103
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002104 return 0;
2105
2106 case INSN_CALL:
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002107 case INSN_CALL_DYNAMIC:
Peter Zijlstraea242132019-02-25 12:50:09 +01002108 ret = validate_call(insn, &state);
2109 if (ret)
2110 return ret;
2111
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002112 if (!no_fp && func && !is_fentry_call(insn) &&
2113 !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002114 WARN_FUNC("call without frame pointer save/setup",
2115 sec, insn->offset);
2116 return 1;
2117 }
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002118
2119 if (dead_end_function(file, insn->call_dest))
2120 return 0;
2121
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002122 break;
2123
2124 case INSN_JUMP_CONDITIONAL:
2125 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002126 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002127 ret = validate_sibling_call(insn, &state);
2128 if (ret)
2129 return ret;
2130
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002131 } else if (insn->jump_dest) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002132 ret = validate_branch(file, func,
2133 insn->jump_dest, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002134 if (ret) {
2135 if (backtrace)
2136 BT_FUNC("(branch)", insn);
2137 return ret;
2138 }
Josh Poimboeuf48550222017-07-07 09:19:42 -05002139 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002140
2141 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2142 return 0;
2143
2144 break;
2145
2146 case INSN_JUMP_DYNAMIC:
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002147 case INSN_JUMP_DYNAMIC_CONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002148 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002149 ret = validate_sibling_call(insn, &state);
2150 if (ret)
2151 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002152 }
2153
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002154 if (insn->type == INSN_JUMP_DYNAMIC)
2155 return 0;
2156
2157 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002158
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002159 case INSN_CONTEXT_SWITCH:
2160 if (func && (!next_insn || !next_insn->hint)) {
2161 WARN_FUNC("unsupported instruction in callable function",
2162 sec, insn->offset);
2163 return 1;
2164 }
2165 return 0;
2166
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002167 case INSN_STACK:
2168 if (update_insn_state(insn, &state))
Josh Poimboeuf12b25722017-08-10 16:37:25 -05002169 return 1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002170
Peter Zijlstraea242132019-02-25 12:50:09 +01002171 if (insn->stack_op.dest.type == OP_DEST_PUSHF) {
2172 if (!state.uaccess_stack) {
2173 state.uaccess_stack = 1;
2174 } else if (state.uaccess_stack >> 31) {
2175 WARN_FUNC("PUSHF stack exhausted", sec, insn->offset);
2176 return 1;
2177 }
2178 state.uaccess_stack <<= 1;
2179 state.uaccess_stack |= state.uaccess;
2180 }
2181
2182 if (insn->stack_op.src.type == OP_SRC_POPF) {
2183 if (state.uaccess_stack) {
2184 state.uaccess = state.uaccess_stack & 1;
2185 state.uaccess_stack >>= 1;
2186 if (state.uaccess_stack == 1)
2187 state.uaccess_stack = 0;
2188 }
2189 }
2190
2191 break;
2192
2193 case INSN_STAC:
2194 if (state.uaccess) {
2195 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2196 return 1;
2197 }
2198
2199 state.uaccess = true;
2200 break;
2201
2202 case INSN_CLAC:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002203 if (!state.uaccess && func) {
Peter Zijlstraea242132019-02-25 12:50:09 +01002204 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2205 return 1;
2206 }
2207
2208 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2209 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2210 return 1;
2211 }
2212
2213 state.uaccess = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002214 break;
2215
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002216 case INSN_STD:
2217 if (state.df)
2218 WARN_FUNC("recursive STD", sec, insn->offset);
2219
2220 state.df = true;
2221 break;
2222
2223 case INSN_CLD:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002224 if (!state.df && func)
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002225 WARN_FUNC("redundant CLD", sec, insn->offset);
2226
2227 state.df = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002228 break;
2229
2230 default:
2231 break;
2232 }
2233
2234 if (insn->dead_end)
2235 return 0;
2236
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002237 if (!next_insn) {
2238 if (state.cfa.base == CFI_UNDEFINED)
2239 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002240 WARN("%s: unexpected end of section", sec->name);
2241 return 1;
2242 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002243
2244 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002245 }
2246
2247 return 0;
2248}
2249
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002250static int validate_unwind_hints(struct objtool_file *file)
2251{
2252 struct instruction *insn;
2253 int ret, warnings = 0;
2254 struct insn_state state;
2255
2256 if (!file->hints)
2257 return 0;
2258
2259 clear_insn_state(&state);
2260
2261 for_each_insn(file, insn) {
2262 if (insn->hint && !insn->visited) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002263 ret = validate_branch(file, insn->func, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002264 if (ret && backtrace)
2265 BT_FUNC("<=== (hint)", insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002266 warnings += ret;
2267 }
2268 }
2269
2270 return warnings;
2271}
2272
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002273static int validate_retpoline(struct objtool_file *file)
2274{
2275 struct instruction *insn;
2276 int warnings = 0;
2277
2278 for_each_insn(file, insn) {
2279 if (insn->type != INSN_JUMP_DYNAMIC &&
2280 insn->type != INSN_CALL_DYNAMIC)
2281 continue;
2282
2283 if (insn->retpoline_safe)
2284 continue;
2285
Peter Zijlstraca41b972018-01-31 10:18:28 +01002286 /*
2287 * .init.text code is ran before userspace and thus doesn't
2288 * strictly need retpolines, except for modules which are
2289 * loaded late, they very much do need retpoline in their
2290 * .init.text
2291 */
2292 if (!strcmp(insn->sec->name, ".init.text") && !module)
2293 continue;
2294
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002295 WARN_FUNC("indirect %s found in RETPOLINE build",
2296 insn->sec, insn->offset,
2297 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2298
2299 warnings++;
2300 }
2301
2302 return warnings;
2303}
2304
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002305static bool is_kasan_insn(struct instruction *insn)
2306{
2307 return (insn->type == INSN_CALL &&
2308 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2309}
2310
2311static bool is_ubsan_insn(struct instruction *insn)
2312{
2313 return (insn->type == INSN_CALL &&
2314 !strcmp(insn->call_dest->name,
2315 "__ubsan_handle_builtin_unreachable"));
2316}
2317
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002318static bool ignore_unreachable_insn(struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002319{
2320 int i;
2321
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002322 if (insn->ignore || insn->type == INSN_NOP)
2323 return true;
2324
2325 /*
2326 * Ignore any unused exceptions. This can happen when a whitelisted
2327 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002328 *
2329 * Also ignore alternative replacement instructions. This can happen
2330 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002331 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002332 if (!strcmp(insn->sec->name, ".fixup") ||
2333 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2334 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002335 return true;
2336
2337 /*
2338 * Check if this (or a subsequent) instruction is related to
2339 * CONFIG_UBSAN or CONFIG_KASAN.
2340 *
2341 * End the search at 5 instructions to avoid going into the weeds.
2342 */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002343 if (!insn->func)
2344 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002345 for (i = 0; i < 5; i++) {
2346
2347 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2348 return true;
2349
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002350 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2351 if (insn->jump_dest &&
2352 insn->jump_dest->func == insn->func) {
2353 insn = insn->jump_dest;
2354 continue;
2355 }
2356
2357 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002358 }
2359
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002360 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002361 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002362
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002363 insn = list_next_entry(insn, list);
2364 }
2365
2366 return false;
2367}
2368
2369static int validate_functions(struct objtool_file *file)
2370{
2371 struct section *sec;
2372 struct symbol *func;
2373 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002374 struct insn_state state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002375 int ret, warnings = 0;
2376
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002377 clear_insn_state(&state);
2378
2379 state.cfa = initial_func_cfi.cfa;
2380 memcpy(&state.regs, &initial_func_cfi.regs,
2381 CFI_NUM_REGS * sizeof(struct cfi_reg));
2382 state.stack_size = initial_func_cfi.cfa.offset;
2383
2384 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002385 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002386 if (func->type != STT_FUNC)
2387 continue;
2388
Josh Poimboeuf61e9b752019-07-17 20:36:49 -05002389 if (!func->len) {
2390 WARN("%s() is missing an ELF size annotation",
2391 func->name);
2392 warnings++;
2393 }
2394
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002395 if (func->pfunc != func || func->alias != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002396 continue;
2397
2398 insn = find_insn(file, sec, func->offset);
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002399 if (!insn || insn->ignore || insn->visited)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002400 continue;
2401
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002402 state.uaccess = func->uaccess_safe;
Peter Zijlstraea242132019-02-25 12:50:09 +01002403
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002404 ret = validate_branch(file, func, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002405 if (ret && backtrace)
2406 BT_FUNC("<=== (func)", insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002407 warnings += ret;
2408 }
2409 }
2410
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002411 return warnings;
2412}
2413
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002414static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002415{
2416 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002417
2418 if (file->ignore_unreachables)
2419 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002420
2421 for_each_insn(file, insn) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002422 if (insn->visited || ignore_unreachable_insn(insn))
2423 continue;
2424
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002425 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2426 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002427 }
2428
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002429 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002430}
2431
2432static void cleanup(struct objtool_file *file)
2433{
2434 struct instruction *insn, *tmpinsn;
2435 struct alternative *alt, *tmpalt;
2436
2437 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2438 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2439 list_del(&alt->list);
2440 free(alt);
2441 }
2442 list_del(&insn->list);
2443 hash_del(&insn->hash);
2444 free(insn);
2445 }
2446 elf_close(file->elf);
2447}
2448
Josh Poimboeuf0c671812019-03-18 19:09:38 -05002449static struct objtool_file file;
2450
Peter Zijlstra43a45252018-01-16 17:16:32 +01002451int check(const char *_objname, bool orc)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002452{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002453 int ret, warnings = 0;
2454
2455 objname = _objname;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002456
Michael Forney8e144792019-07-10 16:20:11 -05002457 file.elf = elf_read(objname, orc ? O_RDWR : O_RDONLY);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002458 if (!file.elf)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002459 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002460
2461 INIT_LIST_HEAD(&file.insn_list);
2462 hash_init(file.insn_hash);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002463 file.c_file = find_section_by_name(file.elf, ".comment");
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002464 file.ignore_unreachables = no_unreachable;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002465 file.hints = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002466
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002467 arch_initial_func_cfi_state(&initial_func_cfi);
2468
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002469 ret = decode_sections(&file);
2470 if (ret < 0)
2471 goto out;
2472 warnings += ret;
2473
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002474 if (list_empty(&file.insn_list))
2475 goto out;
2476
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002477 if (retpoline) {
2478 ret = validate_retpoline(&file);
2479 if (ret < 0)
2480 return ret;
2481 warnings += ret;
2482 }
2483
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002484 ret = validate_functions(&file);
2485 if (ret < 0)
2486 goto out;
2487 warnings += ret;
2488
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002489 ret = validate_unwind_hints(&file);
2490 if (ret < 0)
2491 goto out;
2492 warnings += ret;
2493
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002494 if (!warnings) {
2495 ret = validate_reachable_instructions(&file);
2496 if (ret < 0)
2497 goto out;
2498 warnings += ret;
2499 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002500
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002501 if (orc) {
2502 ret = create_orc(&file);
2503 if (ret < 0)
2504 goto out;
2505
2506 ret = create_orc_sections(&file);
2507 if (ret < 0)
2508 goto out;
2509
2510 ret = elf_write(file.elf);
2511 if (ret < 0)
2512 goto out;
2513 }
2514
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002515out:
2516 cleanup(&file);
2517
Josh Poimboeuf644592d2020-02-10 12:32:38 -06002518 if (ret < 0) {
2519 /*
2520 * Fatal error. The binary is corrupt or otherwise broken in
2521 * some way, or objtool itself is broken. Fail the kernel
2522 * build.
2523 */
2524 return ret;
2525 }
2526
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002527 return 0;
2528}