blob: 950d0f62d22b37f208768e361465bf3ef38050b6 [file] [log] [blame]
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001/*
2 * Copyright (C) 2015-2017 Josh Poimboeuf <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <string.h>
19#include <stdlib.h>
20
Peter Zijlstra43a45252018-01-16 17:16:32 +010021#include "builtin.h"
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050022#include "check.h"
23#include "elf.h"
24#include "special.h"
25#include "arch.h"
26#include "warn.h"
27
28#include <linux/hashtable.h>
29#include <linux/kernel.h>
30
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050031struct alternative {
32 struct list_head list;
33 struct instruction *insn;
34};
35
36const char *objname;
Josh Poimboeufbaa414692017-06-28 10:11:07 -050037struct cfi_state initial_func_cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050038
Josh Poimboeuf627fce12017-07-11 10:33:42 -050039struct instruction *find_insn(struct objtool_file *file,
40 struct section *sec, unsigned long offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050041{
42 struct instruction *insn;
43
44 hash_for_each_possible(file->insn_hash, insn, hash, offset)
45 if (insn->sec == sec && insn->offset == offset)
46 return insn;
47
48 return NULL;
49}
50
51static struct instruction *next_insn_same_sec(struct objtool_file *file,
52 struct instruction *insn)
53{
54 struct instruction *next = list_next_entry(insn, list);
55
Josh Poimboeufbaa414692017-06-28 10:11:07 -050056 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050057 return NULL;
58
59 return next;
60}
61
Josh Poimboeuf13810432018-05-09 22:39:15 -050062static struct instruction *next_insn_same_func(struct objtool_file *file,
63 struct instruction *insn)
64{
65 struct instruction *next = list_next_entry(insn, list);
66 struct symbol *func = insn->func;
67
68 if (!func)
69 return NULL;
70
71 if (&next->list != &file->insn_list && next->func == func)
72 return next;
73
74 /* Check if we're already in the subfunction: */
75 if (func == func->cfunc)
76 return NULL;
77
78 /* Move to the subfunction: */
79 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
80}
81
82#define func_for_each_insn_all(file, func, insn) \
83 for (insn = find_insn(file, func->sec, func->offset); \
84 insn; \
85 insn = next_insn_same_func(file, insn))
86
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050087#define func_for_each_insn(file, func, insn) \
88 for (insn = find_insn(file, func->sec, func->offset); \
89 insn && &insn->list != &file->insn_list && \
90 insn->sec == func->sec && \
91 insn->offset < func->offset + func->len; \
92 insn = list_next_entry(insn, list))
93
94#define func_for_each_insn_continue_reverse(file, func, insn) \
95 for (insn = list_prev_entry(insn, list); \
96 &insn->list != &file->insn_list && \
97 insn->sec == func->sec && insn->offset >= func->offset; \
98 insn = list_prev_entry(insn, list))
99
100#define sec_for_each_insn_from(file, insn) \
101 for (; insn; insn = next_insn_same_sec(file, insn))
102
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500103#define sec_for_each_insn_continue(file, insn) \
104 for (insn = next_insn_same_sec(file, insn); insn; \
105 insn = next_insn_same_sec(file, insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500106
107/*
108 * Check if the function has been manually whitelisted with the
109 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
110 * due to its use of a context switching instruction.
111 */
112static bool ignore_func(struct objtool_file *file, struct symbol *func)
113{
114 struct rela *rela;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500115
116 /* check for STACK_FRAME_NON_STANDARD */
117 if (file->whitelist && file->whitelist->rela)
118 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) {
119 if (rela->sym->type == STT_SECTION &&
120 rela->sym->sec == func->sec &&
121 rela->addend == func->offset)
122 return true;
123 if (rela->sym->type == STT_FUNC && rela->sym == func)
124 return true;
125 }
126
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500127 return false;
128}
129
130/*
131 * This checks to see if the given function is a "noreturn" function.
132 *
133 * For global functions which are outside the scope of this object file, we
134 * have to keep a manual list of them.
135 *
136 * For local functions, we have to detect them manually by simply looking for
137 * the lack of a return instruction.
138 *
139 * Returns:
140 * -1: error
141 * 0: no dead end
142 * 1: dead end
143 */
144static int __dead_end_function(struct objtool_file *file, struct symbol *func,
145 int recursion)
146{
147 int i;
148 struct instruction *insn;
149 bool empty = true;
150
151 /*
152 * Unfortunately these have to be hard coded because the noreturn
153 * attribute isn't provided in ELF data.
154 */
155 static const char * const global_noreturns[] = {
156 "__stack_chk_fail",
157 "panic",
158 "do_exit",
159 "do_task_dead",
160 "__module_put_and_exit",
161 "complete_and_exit",
162 "kvm_spurious_fault",
163 "__reiserfs_panic",
164 "lbug_with_loc",
165 "fortify_panic",
Kees Cookb394d462018-01-10 14:22:38 -0800166 "usercopy_abort",
Josh Poimboeuf684fb242018-06-19 10:47:50 -0500167 "machine_real_restart",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500168 };
169
170 if (func->bind == STB_WEAK)
171 return 0;
172
173 if (func->bind == STB_GLOBAL)
174 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
175 if (!strcmp(func->name, global_noreturns[i]))
176 return 1;
177
Josh Poimboeuf13810432018-05-09 22:39:15 -0500178 if (!func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500179 return 0;
180
Josh Poimboeuf13810432018-05-09 22:39:15 -0500181 insn = find_insn(file, func->sec, func->offset);
182 if (!insn->func)
183 return 0;
184
185 func_for_each_insn_all(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500186 empty = false;
187
188 if (insn->type == INSN_RETURN)
189 return 0;
190 }
191
192 if (empty)
193 return 0;
194
195 /*
196 * A function can have a sibling call instead of a return. In that
197 * case, the function's dead-end status depends on whether the target
198 * of the sibling call returns.
199 */
Josh Poimboeuf13810432018-05-09 22:39:15 -0500200 func_for_each_insn_all(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500201 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
202 struct instruction *dest = insn->jump_dest;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500203
204 if (!dest)
205 /* sibling call to another file */
206 return 0;
207
Josh Poimboeuf13810432018-05-09 22:39:15 -0500208 if (dest->func && dest->func->pfunc != insn->func->pfunc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500209
Josh Poimboeuf13810432018-05-09 22:39:15 -0500210 /* local sibling call */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500211 if (recursion == 5) {
Josh Poimboeuf0afd0d9e02018-05-09 22:39:14 -0500212 /*
213 * Infinite recursion: two functions
214 * have sibling calls to each other.
215 * This is a very rare case. It means
216 * they aren't dead ends.
217 */
218 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500219 }
220
Josh Poimboeuf13810432018-05-09 22:39:15 -0500221 return __dead_end_function(file, dest->func,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500222 recursion + 1);
223 }
224 }
225
226 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
227 /* sibling call */
228 return 0;
229 }
230
231 return 1;
232}
233
234static int dead_end_function(struct objtool_file *file, struct symbol *func)
235{
236 return __dead_end_function(file, func, 0);
237}
238
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500239static void clear_insn_state(struct insn_state *state)
240{
241 int i;
242
243 memset(state, 0, sizeof(*state));
244 state->cfa.base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500245 for (i = 0; i < CFI_NUM_REGS; i++) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500246 state->regs[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500247 state->vals[i].base = CFI_UNDEFINED;
248 }
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500249 state->drap_reg = CFI_UNDEFINED;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -0500250 state->drap_offset = -1;
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500251}
252
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500253/*
254 * Call the arch-specific instruction decoder for all the instructions and add
255 * them to the global instruction list.
256 */
257static int decode_instructions(struct objtool_file *file)
258{
259 struct section *sec;
260 struct symbol *func;
261 unsigned long offset;
262 struct instruction *insn;
263 int ret;
264
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500265 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500266
267 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
268 continue;
269
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500270 if (strcmp(sec->name, ".altinstr_replacement") &&
271 strcmp(sec->name, ".altinstr_aux") &&
272 strncmp(sec->name, ".discard.", 9))
273 sec->text = true;
274
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500275 for (offset = 0; offset < sec->len; offset += insn->len) {
276 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500277 if (!insn) {
278 WARN("malloc failed");
279 return -1;
280 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500281 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500282 INIT_LIST_HEAD(&insn->alts);
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500283 clear_insn_state(&insn->state);
284
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500285 insn->sec = sec;
286 insn->offset = offset;
287
288 ret = arch_decode_instruction(file->elf, sec, offset,
289 sec->len - offset,
290 &insn->len, &insn->type,
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500291 &insn->immediate,
292 &insn->stack_op);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500293 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500294 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500295
296 if (!insn->type || insn->type > INSN_LAST) {
297 WARN_FUNC("invalid instruction type %d",
298 insn->sec, insn->offset, insn->type);
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500299 ret = -1;
300 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500301 }
302
303 hash_add(file->insn_hash, &insn->hash, insn->offset);
304 list_add_tail(&insn->list, &file->insn_list);
305 }
306
307 list_for_each_entry(func, &sec->symbol_list, list) {
308 if (func->type != STT_FUNC)
309 continue;
310
311 if (!find_insn(file, sec, func->offset)) {
312 WARN("%s(): can't find starting instruction",
313 func->name);
314 return -1;
315 }
316
317 func_for_each_insn(file, func, insn)
318 if (!insn->func)
319 insn->func = func;
320 }
321 }
322
323 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500324
325err:
326 free(insn);
327 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500328}
329
330/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500331 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500332 */
333static int add_dead_ends(struct objtool_file *file)
334{
335 struct section *sec;
336 struct rela *rela;
337 struct instruction *insn;
338 bool found;
339
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500340 /*
341 * By default, "ud2" is a dead end unless otherwise annotated, because
342 * GCC 7 inserts it for certain divide-by-zero cases.
343 */
344 for_each_insn(file, insn)
345 if (insn->type == INSN_BUG)
346 insn->dead_end = true;
347
348 /*
349 * Check for manually annotated dead ends.
350 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500351 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
352 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500353 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500354
355 list_for_each_entry(rela, &sec->rela_list, list) {
356 if (rela->sym->type != STT_SECTION) {
357 WARN("unexpected relocation symbol type in %s", sec->name);
358 return -1;
359 }
360 insn = find_insn(file, rela->sym->sec, rela->addend);
361 if (insn)
362 insn = list_prev_entry(insn, list);
363 else if (rela->addend == rela->sym->sec->len) {
364 found = false;
365 list_for_each_entry_reverse(insn, &file->insn_list, list) {
366 if (insn->sec == rela->sym->sec) {
367 found = true;
368 break;
369 }
370 }
371
372 if (!found) {
373 WARN("can't find unreachable insn at %s+0x%x",
374 rela->sym->sec->name, rela->addend);
375 return -1;
376 }
377 } else {
378 WARN("can't find unreachable insn at %s+0x%x",
379 rela->sym->sec->name, rela->addend);
380 return -1;
381 }
382
383 insn->dead_end = true;
384 }
385
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500386reachable:
387 /*
388 * These manually annotated reachable checks are needed for GCC 4.4,
389 * where the Linux unreachable() macro isn't supported. In that case
390 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
391 * not a dead end.
392 */
393 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
394 if (!sec)
395 return 0;
396
397 list_for_each_entry(rela, &sec->rela_list, list) {
398 if (rela->sym->type != STT_SECTION) {
399 WARN("unexpected relocation symbol type in %s", sec->name);
400 return -1;
401 }
402 insn = find_insn(file, rela->sym->sec, rela->addend);
403 if (insn)
404 insn = list_prev_entry(insn, list);
405 else if (rela->addend == rela->sym->sec->len) {
406 found = false;
407 list_for_each_entry_reverse(insn, &file->insn_list, list) {
408 if (insn->sec == rela->sym->sec) {
409 found = true;
410 break;
411 }
412 }
413
414 if (!found) {
415 WARN("can't find reachable insn at %s+0x%x",
416 rela->sym->sec->name, rela->addend);
417 return -1;
418 }
419 } else {
420 WARN("can't find reachable insn at %s+0x%x",
421 rela->sym->sec->name, rela->addend);
422 return -1;
423 }
424
425 insn->dead_end = false;
426 }
427
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500428 return 0;
429}
430
431/*
432 * Warnings shouldn't be reported for ignored functions.
433 */
434static void add_ignores(struct objtool_file *file)
435{
436 struct instruction *insn;
437 struct section *sec;
438 struct symbol *func;
439
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500440 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500441 list_for_each_entry(func, &sec->symbol_list, list) {
442 if (func->type != STT_FUNC)
443 continue;
444
445 if (!ignore_func(file, func))
446 continue;
447
Josh Poimboeuf13810432018-05-09 22:39:15 -0500448 func_for_each_insn_all(file, func, insn)
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500449 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500450 }
451 }
452}
453
454/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000455 * FIXME: For now, just ignore any alternatives which add retpolines. This is
456 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
457 * But it at least allows objtool to understand the control flow *around* the
458 * retpoline.
459 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100460static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000461{
462 struct section *sec;
463 struct rela *rela;
464 struct instruction *insn;
465
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100466 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000467 if (!sec)
468 return 0;
469
470 list_for_each_entry(rela, &sec->rela_list, list) {
471 if (rela->sym->type != STT_SECTION) {
472 WARN("unexpected relocation symbol type in %s", sec->name);
473 return -1;
474 }
475
476 insn = find_insn(file, rela->sym->sec, rela->addend);
477 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100478 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000479 return -1;
480 }
481
482 insn->ignore_alts = true;
483 }
484
485 return 0;
486}
487
488/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500489 * Find the destination instructions for all jumps.
490 */
491static int add_jump_destinations(struct objtool_file *file)
492{
493 struct instruction *insn;
494 struct rela *rela;
495 struct section *dest_sec;
496 unsigned long dest_off;
497
498 for_each_insn(file, insn) {
499 if (insn->type != INSN_JUMP_CONDITIONAL &&
500 insn->type != INSN_JUMP_UNCONDITIONAL)
501 continue;
502
Josh Poimboeufbaa414692017-06-28 10:11:07 -0500503 if (insn->ignore)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500504 continue;
505
506 rela = find_rela_by_dest_range(insn->sec, insn->offset,
507 insn->len);
508 if (!rela) {
509 dest_sec = insn->sec;
510 dest_off = insn->offset + insn->len + insn->immediate;
511 } else if (rela->sym->type == STT_SECTION) {
512 dest_sec = rela->sym->sec;
513 dest_off = rela->addend + 4;
514 } else if (rela->sym->sec->idx) {
515 dest_sec = rela->sym->sec;
516 dest_off = rela->sym->sym.st_value + rela->addend + 4;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000517 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
518 /*
519 * Retpoline jumps are really dynamic jumps in
520 * disguise, so convert them accordingly.
521 */
522 insn->type = INSN_JUMP_DYNAMIC;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100523 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000524 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500525 } else {
526 /* sibling call */
527 insn->jump_dest = 0;
528 continue;
529 }
530
531 insn->jump_dest = find_insn(file, dest_sec, dest_off);
532 if (!insn->jump_dest) {
533
534 /*
535 * This is a special case where an alt instruction
536 * jumps past the end of the section. These are
537 * handled later in handle_group_alt().
538 */
539 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
540 continue;
541
542 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
543 insn->sec, insn->offset, dest_sec->name,
544 dest_off);
545 return -1;
546 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500547
548 /*
549 * For GCC 8+, create parent/child links for any cold
550 * subfunctions. This is _mostly_ redundant with a similar
551 * initialization in read_symbols().
552 *
553 * If a function has aliases, we want the *first* such function
554 * in the symbol table to be the subfunction's parent. In that
555 * case we overwrite the initialization done in read_symbols().
556 *
557 * However this code can't completely replace the
558 * read_symbols() code because this doesn't detect the case
559 * where the parent function's only reference to a subfunction
560 * is through a switch table.
561 */
562 if (insn->func && insn->jump_dest->func &&
563 insn->func != insn->jump_dest->func &&
564 !strstr(insn->func->name, ".cold.") &&
565 strstr(insn->jump_dest->func->name, ".cold.")) {
566 insn->func->cfunc = insn->jump_dest->func;
567 insn->jump_dest->func->pfunc = insn->func;
568 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500569 }
570
571 return 0;
572}
573
574/*
575 * Find the destination instructions for all calls.
576 */
577static int add_call_destinations(struct objtool_file *file)
578{
579 struct instruction *insn;
580 unsigned long dest_off;
581 struct rela *rela;
582
583 for_each_insn(file, insn) {
584 if (insn->type != INSN_CALL)
585 continue;
586
587 rela = find_rela_by_dest_range(insn->sec, insn->offset,
588 insn->len);
589 if (!rela) {
590 dest_off = insn->offset + insn->len + insn->immediate;
591 insn->call_dest = find_symbol_by_offset(insn->sec,
592 dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600593
594 if (!insn->call_dest && !insn->ignore) {
595 WARN_FUNC("unsupported intra-function call",
596 insn->sec, insn->offset);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100597 if (retpoline)
598 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 -0500599 return -1;
600 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600601
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500602 } else if (rela->sym->type == STT_SECTION) {
603 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
604 rela->addend+4);
605 if (!insn->call_dest ||
606 insn->call_dest->type != STT_FUNC) {
607 WARN_FUNC("can't find call dest symbol at %s+0x%x",
608 insn->sec, insn->offset,
609 rela->sym->sec->name,
610 rela->addend + 4);
611 return -1;
612 }
613 } else
614 insn->call_dest = rela->sym;
615 }
616
617 return 0;
618}
619
620/*
621 * The .alternatives section requires some extra special care, over and above
622 * what other special sections require:
623 *
624 * 1. Because alternatives are patched in-place, we need to insert a fake jump
625 * instruction at the end so that validate_branch() skips all the original
626 * replaced instructions when validating the new instruction path.
627 *
628 * 2. An added wrinkle is that the new instruction length might be zero. In
629 * that case the old instructions are replaced with noops. We simulate that
630 * by creating a fake jump as the only new instruction.
631 *
632 * 3. In some cases, the alternative section includes an instruction which
633 * conditionally jumps to the _end_ of the entry. We have to modify these
634 * jumps' destinations to point back to .text rather than the end of the
635 * entry in .altinstr_replacement.
636 *
637 * 4. It has been requested that we don't validate the !POPCNT feature path
638 * which is a "very very small percentage of machines".
639 */
640static int handle_group_alt(struct objtool_file *file,
641 struct special_alt *special_alt,
642 struct instruction *orig_insn,
643 struct instruction **new_insn)
644{
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600645 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500646 unsigned long dest_off;
647
648 last_orig_insn = NULL;
649 insn = orig_insn;
650 sec_for_each_insn_from(file, insn) {
651 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
652 break;
653
654 if (special_alt->skip_orig)
655 insn->type = INSN_NOP;
656
657 insn->alt_group = true;
658 last_orig_insn = insn;
659 }
660
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600661 if (next_insn_same_sec(file, last_orig_insn)) {
662 fake_jump = malloc(sizeof(*fake_jump));
663 if (!fake_jump) {
664 WARN("malloc failed");
665 return -1;
666 }
667 memset(fake_jump, 0, sizeof(*fake_jump));
668 INIT_LIST_HEAD(&fake_jump->alts);
669 clear_insn_state(&fake_jump->state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500670
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600671 fake_jump->sec = special_alt->new_sec;
672 fake_jump->offset = -1;
673 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
674 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
675 fake_jump->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500676 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500677
678 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600679 if (!fake_jump) {
680 WARN("%s: empty alternative at end of section",
681 special_alt->orig_sec->name);
682 return -1;
683 }
684
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500685 *new_insn = fake_jump;
686 return 0;
687 }
688
689 last_new_insn = NULL;
690 insn = *new_insn;
691 sec_for_each_insn_from(file, insn) {
692 if (insn->offset >= special_alt->new_off + special_alt->new_len)
693 break;
694
695 last_new_insn = insn;
696
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600697 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +0100698 insn->func = orig_insn->func;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600699
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500700 if (insn->type != INSN_JUMP_CONDITIONAL &&
701 insn->type != INSN_JUMP_UNCONDITIONAL)
702 continue;
703
704 if (!insn->immediate)
705 continue;
706
707 dest_off = insn->offset + insn->len + insn->immediate;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600708 if (dest_off == special_alt->new_off + special_alt->new_len) {
709 if (!fake_jump) {
710 WARN("%s: alternative jump to end of section",
711 special_alt->orig_sec->name);
712 return -1;
713 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500714 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600715 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500716
717 if (!insn->jump_dest) {
718 WARN_FUNC("can't find alternative jump destination",
719 insn->sec, insn->offset);
720 return -1;
721 }
722 }
723
724 if (!last_new_insn) {
725 WARN_FUNC("can't find last new alternative instruction",
726 special_alt->new_sec, special_alt->new_off);
727 return -1;
728 }
729
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600730 if (fake_jump)
731 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500732
733 return 0;
734}
735
736/*
737 * A jump table entry can either convert a nop to a jump or a jump to a nop.
738 * If the original instruction is a jump, make the alt entry an effective nop
739 * by just skipping the original instruction.
740 */
741static int handle_jump_alt(struct objtool_file *file,
742 struct special_alt *special_alt,
743 struct instruction *orig_insn,
744 struct instruction **new_insn)
745{
746 if (orig_insn->type == INSN_NOP)
747 return 0;
748
749 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
750 WARN_FUNC("unsupported instruction at jump label",
751 orig_insn->sec, orig_insn->offset);
752 return -1;
753 }
754
755 *new_insn = list_next_entry(orig_insn, list);
756 return 0;
757}
758
759/*
760 * Read all the special sections which have alternate instructions which can be
761 * patched in or redirected to at runtime. Each instruction having alternate
762 * instruction(s) has them added to its insn->alts list, which will be
763 * traversed in validate_branch().
764 */
765static int add_special_section_alts(struct objtool_file *file)
766{
767 struct list_head special_alts;
768 struct instruction *orig_insn, *new_insn;
769 struct special_alt *special_alt, *tmp;
770 struct alternative *alt;
771 int ret;
772
773 ret = special_get_alts(file->elf, &special_alts);
774 if (ret)
775 return ret;
776
777 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500778
779 orig_insn = find_insn(file, special_alt->orig_sec,
780 special_alt->orig_off);
781 if (!orig_insn) {
782 WARN_FUNC("special: can't find orig instruction",
783 special_alt->orig_sec, special_alt->orig_off);
784 ret = -1;
785 goto out;
786 }
787
788 new_insn = NULL;
789 if (!special_alt->group || special_alt->new_len) {
790 new_insn = find_insn(file, special_alt->new_sec,
791 special_alt->new_off);
792 if (!new_insn) {
793 WARN_FUNC("special: can't find new instruction",
794 special_alt->new_sec,
795 special_alt->new_off);
796 ret = -1;
797 goto out;
798 }
799 }
800
801 if (special_alt->group) {
802 ret = handle_group_alt(file, special_alt, orig_insn,
803 &new_insn);
804 if (ret)
805 goto out;
806 } else if (special_alt->jump_or_nop) {
807 ret = handle_jump_alt(file, special_alt, orig_insn,
808 &new_insn);
809 if (ret)
810 goto out;
811 }
812
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000813 alt = malloc(sizeof(*alt));
814 if (!alt) {
815 WARN("malloc failed");
816 ret = -1;
817 goto out;
818 }
819
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500820 alt->insn = new_insn;
821 list_add_tail(&alt->list, &orig_insn->alts);
822
823 list_del(&special_alt->list);
824 free(special_alt);
825 }
826
827out:
828 return ret;
829}
830
Josh Poimboeuf13810432018-05-09 22:39:15 -0500831static int add_switch_table(struct objtool_file *file, struct instruction *insn,
832 struct rela *table, struct rela *next_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500833{
834 struct rela *rela = table;
835 struct instruction *alt_insn;
836 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500837 struct symbol *pfunc = insn->func->pfunc;
838 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500839
Allan Xavier4a60aa02018-09-07 08:12:01 -0500840 list_for_each_entry_from(rela, &table->rela_sec->rela_list, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500841 if (rela == next_table)
842 break;
843
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500844 /* Make sure the switch table entries are consecutive: */
845 if (prev_offset && rela->offset != prev_offset + 8)
846 break;
847
848 /* Detect function pointers from contiguous objects: */
849 if (rela->sym->sec == pfunc->sec &&
850 rela->addend == pfunc->offset)
851 break;
852
Josh Poimboeuf13810432018-05-09 22:39:15 -0500853 alt_insn = find_insn(file, rela->sym->sec, rela->addend);
854 if (!alt_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500855 break;
856
Josh Poimboeuf13810432018-05-09 22:39:15 -0500857 /* Make sure the jmp dest is in the function or subfunction: */
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500858 if (alt_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -0500859 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500860
861 alt = malloc(sizeof(*alt));
862 if (!alt) {
863 WARN("malloc failed");
864 return -1;
865 }
866
867 alt->insn = alt_insn;
868 list_add_tail(&alt->list, &insn->alts);
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500869 prev_offset = rela->offset;
870 }
871
872 if (!prev_offset) {
873 WARN_FUNC("can't find switch jump table",
874 insn->sec, insn->offset);
875 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500876 }
877
878 return 0;
879}
880
881/*
882 * find_switch_table() - Given a dynamic jump, find the switch jump table in
883 * .rodata associated with it.
884 *
885 * There are 3 basic patterns:
886 *
887 * 1. jmpq *[rodata addr](,%reg,8)
888 *
889 * This is the most common case by far. It jumps to an address in a simple
890 * jump table which is stored in .rodata.
891 *
892 * 2. jmpq *[rodata addr](%rip)
893 *
894 * This is caused by a rare GCC quirk, currently only seen in three driver
895 * functions in the kernel, only with certain obscure non-distro configs.
896 *
897 * As part of an optimization, GCC makes a copy of an existing switch jump
898 * table, modifies it, and then hard-codes the jump (albeit with an indirect
899 * jump) to use a single entry in the table. The rest of the jump table and
900 * some of its jump targets remain as dead code.
901 *
902 * In such a case we can just crudely ignore all unreachable instruction
903 * warnings for the entire object file. Ideally we would just ignore them
904 * for the function, but that would require redesigning the code quite a
905 * bit. And honestly that's just not worth doing: unreachable instruction
906 * warnings are of questionable value anyway, and this is such a rare issue.
907 *
908 * 3. mov [rodata addr],%reg1
909 * ... some instructions ...
910 * jmpq *(%reg1,%reg2,8)
911 *
912 * This is a fairly uncommon pattern which is new for GCC 6. As of this
913 * writing, there are 11 occurrences of it in the allmodconfig kernel.
914 *
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100915 * As of GCC 7 there are quite a few more of these and the 'in between' code
916 * is significant. Esp. with KASAN enabled some of the code between the mov
917 * and jmpq uses .rodata itself, which can confuse things.
918 *
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500919 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
920 * ensure the same register is used in the mov and jump instructions.
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100921 *
922 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500923 */
924static struct rela *find_switch_table(struct objtool_file *file,
925 struct symbol *func,
926 struct instruction *insn)
927{
928 struct rela *text_rela, *rodata_rela;
929 struct instruction *orig_insn = insn;
Allan Xavier4a60aa02018-09-07 08:12:01 -0500930 struct section *rodata_sec;
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -0500931 unsigned long table_offset;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500932
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100933 /*
934 * Backward search using the @first_jump_src links, these help avoid
935 * much of the 'in between' code. Which avoids us getting confused by
936 * it.
937 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -0500938 for (;
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100939 &insn->list != &file->insn_list &&
940 insn->sec == func->sec &&
941 insn->offset >= func->offset;
942
943 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
944
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -0500945 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500946 break;
947
948 /* allow small jumps within the range */
949 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
950 insn->jump_dest &&
951 (insn->jump_dest->offset <= insn->offset ||
952 insn->jump_dest->offset > orig_insn->offset))
953 break;
954
955 /* look for a relocation which references .rodata */
956 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
957 insn->len);
Allan Xavier4a60aa02018-09-07 08:12:01 -0500958 if (!text_rela || text_rela->sym->type != STT_SECTION ||
959 !text_rela->sym->sec->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500960 continue;
961
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -0500962 table_offset = text_rela->addend;
Allan Xavier4a60aa02018-09-07 08:12:01 -0500963 rodata_sec = text_rela->sym->sec;
964
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -0500965 if (text_rela->type == R_X86_64_PC32)
966 table_offset += 4;
967
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500968 /*
969 * Make sure the .rodata address isn't associated with a
970 * symbol. gcc jump tables are anonymous data.
971 */
Allan Xavier4a60aa02018-09-07 08:12:01 -0500972 if (find_symbol_containing(rodata_sec, table_offset))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500973 continue;
974
Allan Xavier4a60aa02018-09-07 08:12:01 -0500975 rodata_rela = find_rela_by_dest(rodata_sec, table_offset);
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -0500976 if (rodata_rela) {
977 /*
978 * Use of RIP-relative switch jumps is quite rare, and
979 * indicates a rare GCC quirk/bug which can leave dead
980 * code behind.
981 */
982 if (text_rela->type == R_X86_64_PC32)
983 file->ignore_unreachables = true;
984
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -0500985 return rodata_rela;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -0500986 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500987 }
988
989 return NULL;
990}
991
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100992
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500993static int add_func_switch_tables(struct objtool_file *file,
994 struct symbol *func)
995{
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100996 struct instruction *insn, *last = NULL, *prev_jump = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500997 struct rela *rela, *prev_rela = NULL;
998 int ret;
999
Josh Poimboeuf13810432018-05-09 22:39:15 -05001000 func_for_each_insn_all(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001001 if (!last)
1002 last = insn;
1003
1004 /*
1005 * Store back-pointers for unconditional forward jumps such
1006 * that find_switch_table() can back-track using those and
1007 * avoid some potentially confusing code.
1008 */
1009 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1010 insn->offset > last->offset &&
1011 insn->jump_dest->offset > insn->offset &&
1012 !insn->jump_dest->first_jump_src) {
1013
1014 insn->jump_dest->first_jump_src = insn;
1015 last = insn->jump_dest;
1016 }
1017
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001018 if (insn->type != INSN_JUMP_DYNAMIC)
1019 continue;
1020
1021 rela = find_switch_table(file, func, insn);
1022 if (!rela)
1023 continue;
1024
1025 /*
1026 * We found a switch table, but we don't know yet how big it
1027 * is. Don't add it until we reach the end of the function or
1028 * the beginning of another switch table in the same function.
1029 */
1030 if (prev_jump) {
Josh Poimboeuf13810432018-05-09 22:39:15 -05001031 ret = add_switch_table(file, prev_jump, prev_rela, rela);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001032 if (ret)
1033 return ret;
1034 }
1035
1036 prev_jump = insn;
1037 prev_rela = rela;
1038 }
1039
1040 if (prev_jump) {
Josh Poimboeuf13810432018-05-09 22:39:15 -05001041 ret = add_switch_table(file, prev_jump, prev_rela, NULL);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001042 if (ret)
1043 return ret;
1044 }
1045
1046 return 0;
1047}
1048
1049/*
1050 * For some switch statements, gcc generates a jump table in the .rodata
1051 * section which contains a list of addresses within the function to jump to.
1052 * This finds these jump tables and adds them to the insn->alts lists.
1053 */
1054static int add_switch_table_alts(struct objtool_file *file)
1055{
1056 struct section *sec;
1057 struct symbol *func;
1058 int ret;
1059
Allan Xavier4a60aa02018-09-07 08:12:01 -05001060 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001061 return 0;
1062
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001063 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001064 list_for_each_entry(func, &sec->symbol_list, list) {
1065 if (func->type != STT_FUNC)
1066 continue;
1067
1068 ret = add_func_switch_tables(file, func);
1069 if (ret)
1070 return ret;
1071 }
1072 }
1073
1074 return 0;
1075}
1076
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001077static int read_unwind_hints(struct objtool_file *file)
1078{
1079 struct section *sec, *relasec;
1080 struct rela *rela;
1081 struct unwind_hint *hint;
1082 struct instruction *insn;
1083 struct cfi_reg *cfa;
1084 int i;
1085
1086 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1087 if (!sec)
1088 return 0;
1089
1090 relasec = sec->rela;
1091 if (!relasec) {
1092 WARN("missing .rela.discard.unwind_hints section");
1093 return -1;
1094 }
1095
1096 if (sec->len % sizeof(struct unwind_hint)) {
1097 WARN("struct unwind_hint size mismatch");
1098 return -1;
1099 }
1100
1101 file->hints = true;
1102
1103 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1104 hint = (struct unwind_hint *)sec->data->d_buf + i;
1105
1106 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1107 if (!rela) {
1108 WARN("can't find rela for unwind_hints[%d]", i);
1109 return -1;
1110 }
1111
1112 insn = find_insn(file, rela->sym->sec, rela->addend);
1113 if (!insn) {
1114 WARN("can't find insn for unwind_hints[%d]", i);
1115 return -1;
1116 }
1117
1118 cfa = &insn->state.cfa;
1119
1120 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1121 insn->save = true;
1122 continue;
1123
1124 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1125 insn->restore = true;
1126 insn->hint = true;
1127 continue;
1128 }
1129
1130 insn->hint = true;
1131
1132 switch (hint->sp_reg) {
1133 case ORC_REG_UNDEFINED:
1134 cfa->base = CFI_UNDEFINED;
1135 break;
1136 case ORC_REG_SP:
1137 cfa->base = CFI_SP;
1138 break;
1139 case ORC_REG_BP:
1140 cfa->base = CFI_BP;
1141 break;
1142 case ORC_REG_SP_INDIRECT:
1143 cfa->base = CFI_SP_INDIRECT;
1144 break;
1145 case ORC_REG_R10:
1146 cfa->base = CFI_R10;
1147 break;
1148 case ORC_REG_R13:
1149 cfa->base = CFI_R13;
1150 break;
1151 case ORC_REG_DI:
1152 cfa->base = CFI_DI;
1153 break;
1154 case ORC_REG_DX:
1155 cfa->base = CFI_DX;
1156 break;
1157 default:
1158 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1159 insn->sec, insn->offset, hint->sp_reg);
1160 return -1;
1161 }
1162
1163 cfa->offset = hint->sp_offset;
1164 insn->state.type = hint->type;
Josh Poimboeufd31a5802018-05-18 08:47:12 +02001165 insn->state.end = hint->end;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001166 }
1167
1168 return 0;
1169}
1170
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001171static int read_retpoline_hints(struct objtool_file *file)
1172{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001173 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001174 struct instruction *insn;
1175 struct rela *rela;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001176
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001177 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001178 if (!sec)
1179 return 0;
1180
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001181 list_for_each_entry(rela, &sec->rela_list, list) {
1182 if (rela->sym->type != STT_SECTION) {
1183 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001184 return -1;
1185 }
1186
1187 insn = find_insn(file, rela->sym->sec, rela->addend);
1188 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001189 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001190 return -1;
1191 }
1192
1193 if (insn->type != INSN_JUMP_DYNAMIC &&
1194 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001195 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001196 insn->sec, insn->offset);
1197 return -1;
1198 }
1199
1200 insn->retpoline_safe = true;
1201 }
1202
1203 return 0;
1204}
1205
Allan Xavier4a60aa02018-09-07 08:12:01 -05001206static void mark_rodata(struct objtool_file *file)
1207{
1208 struct section *sec;
1209 bool found = false;
1210
1211 /*
1212 * This searches for the .rodata section or multiple .rodata.func_name
1213 * sections if -fdata-sections is being used. The .str.1.1 and .str.1.8
1214 * rodata sections are ignored as they don't contain jump tables.
1215 */
1216 for_each_sec(file, sec) {
1217 if (!strncmp(sec->name, ".rodata", 7) &&
1218 !strstr(sec->name, ".str1.")) {
1219 sec->rodata = true;
1220 found = true;
1221 }
1222 }
1223
1224 file->rodata = found;
1225}
1226
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001227static int decode_sections(struct objtool_file *file)
1228{
1229 int ret;
1230
Allan Xavier4a60aa02018-09-07 08:12:01 -05001231 mark_rodata(file);
1232
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001233 ret = decode_instructions(file);
1234 if (ret)
1235 return ret;
1236
1237 ret = add_dead_ends(file);
1238 if (ret)
1239 return ret;
1240
1241 add_ignores(file);
1242
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001243 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001244 if (ret)
1245 return ret;
1246
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001247 ret = add_jump_destinations(file);
1248 if (ret)
1249 return ret;
1250
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001251 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001252 if (ret)
1253 return ret;
1254
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001255 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001256 if (ret)
1257 return ret;
1258
1259 ret = add_switch_table_alts(file);
1260 if (ret)
1261 return ret;
1262
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001263 ret = read_unwind_hints(file);
1264 if (ret)
1265 return ret;
1266
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001267 ret = read_retpoline_hints(file);
1268 if (ret)
1269 return ret;
1270
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001271 return 0;
1272}
1273
1274static bool is_fentry_call(struct instruction *insn)
1275{
1276 if (insn->type == INSN_CALL &&
1277 insn->call_dest->type == STT_NOTYPE &&
1278 !strcmp(insn->call_dest->name, "__fentry__"))
1279 return true;
1280
1281 return false;
1282}
1283
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001284static bool has_modified_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001285{
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001286 int i;
1287
1288 if (state->cfa.base != initial_func_cfi.cfa.base ||
1289 state->cfa.offset != initial_func_cfi.cfa.offset ||
1290 state->stack_size != initial_func_cfi.cfa.offset ||
1291 state->drap)
1292 return true;
1293
1294 for (i = 0; i < CFI_NUM_REGS; i++)
1295 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1296 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1297 return true;
1298
1299 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001300}
1301
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001302static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001303{
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001304 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1305 state->regs[CFI_BP].offset == -16)
1306 return true;
1307
1308 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1309 return true;
1310
1311 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001312}
1313
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001314static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1315{
1316 struct cfi_reg *cfa = &state->cfa;
1317 struct stack_op *op = &insn->stack_op;
1318
1319 if (cfa->base != CFI_SP)
1320 return 0;
1321
1322 /* push */
1323 if (op->dest.type == OP_DEST_PUSH)
1324 cfa->offset += 8;
1325
1326 /* pop */
1327 if (op->src.type == OP_SRC_POP)
1328 cfa->offset -= 8;
1329
1330 /* add immediate to sp */
1331 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1332 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1333 cfa->offset -= op->src.offset;
1334
1335 return 0;
1336}
1337
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001338static void save_reg(struct insn_state *state, unsigned char reg, int base,
1339 int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001340{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001341 if (arch_callee_saved_reg(reg) &&
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001342 state->regs[reg].base == CFI_UNDEFINED) {
1343 state->regs[reg].base = base;
1344 state->regs[reg].offset = offset;
1345 }
1346}
1347
1348static void restore_reg(struct insn_state *state, unsigned char reg)
1349{
1350 state->regs[reg].base = CFI_UNDEFINED;
1351 state->regs[reg].offset = 0;
1352}
1353
1354/*
1355 * A note about DRAP stack alignment:
1356 *
1357 * GCC has the concept of a DRAP register, which is used to help keep track of
1358 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1359 * register. The typical DRAP pattern is:
1360 *
1361 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1362 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1363 * 41 ff 72 f8 pushq -0x8(%r10)
1364 * 55 push %rbp
1365 * 48 89 e5 mov %rsp,%rbp
1366 * (more pushes)
1367 * 41 52 push %r10
1368 * ...
1369 * 41 5a pop %r10
1370 * (more pops)
1371 * 5d pop %rbp
1372 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1373 * c3 retq
1374 *
1375 * There are some variations in the epilogues, like:
1376 *
1377 * 5b pop %rbx
1378 * 41 5a pop %r10
1379 * 41 5c pop %r12
1380 * 41 5d pop %r13
1381 * 41 5e pop %r14
1382 * c9 leaveq
1383 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1384 * c3 retq
1385 *
1386 * and:
1387 *
1388 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1389 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1390 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1391 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1392 * c9 leaveq
1393 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1394 * c3 retq
1395 *
1396 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1397 * restored beforehand:
1398 *
1399 * 41 55 push %r13
1400 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1401 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1402 * ...
1403 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1404 * 41 5d pop %r13
1405 * c3 retq
1406 */
1407static int update_insn_state(struct instruction *insn, struct insn_state *state)
1408{
1409 struct stack_op *op = &insn->stack_op;
1410 struct cfi_reg *cfa = &state->cfa;
1411 struct cfi_reg *regs = state->regs;
1412
1413 /* stack operations don't make sense with an undefined CFA */
1414 if (cfa->base == CFI_UNDEFINED) {
1415 if (insn->func) {
1416 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1417 return -1;
1418 }
1419 return 0;
1420 }
1421
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001422 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1423 return update_insn_state_regs(insn, state);
1424
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001425 switch (op->dest.type) {
1426
1427 case OP_DEST_REG:
1428 switch (op->src.type) {
1429
1430 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001431 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1432 cfa->base == CFI_SP &&
1433 regs[CFI_BP].base == CFI_CFA &&
1434 regs[CFI_BP].offset == -cfa->offset) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001435
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001436 /* mov %rsp, %rbp */
1437 cfa->base = op->dest.reg;
1438 state->bp_scratch = false;
1439 }
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001440
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001441 else if (op->src.reg == CFI_SP &&
1442 op->dest.reg == CFI_BP && state->drap) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001443
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001444 /* drap: mov %rsp, %rbp */
1445 regs[CFI_BP].base = CFI_BP;
1446 regs[CFI_BP].offset = -state->stack_size;
1447 state->bp_scratch = false;
1448 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001449
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001450 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1451
1452 /*
1453 * mov %rsp, %reg
1454 *
1455 * This is needed for the rare case where GCC
1456 * does:
1457 *
1458 * mov %rsp, %rax
1459 * ...
1460 * mov %rax, %rsp
1461 */
1462 state->vals[op->dest.reg].base = CFI_CFA;
1463 state->vals[op->dest.reg].offset = -state->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001464 }
1465
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001466 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1467 cfa->base == CFI_BP) {
1468
1469 /*
1470 * mov %rbp, %rsp
1471 *
1472 * Restore the original stack pointer (Clang).
1473 */
1474 state->stack_size = -state->regs[CFI_BP].offset;
1475 }
1476
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001477 else if (op->dest.reg == cfa->base) {
1478
1479 /* mov %reg, %rsp */
1480 if (cfa->base == CFI_SP &&
1481 state->vals[op->src.reg].base == CFI_CFA) {
1482
1483 /*
1484 * This is needed for the rare case
1485 * where GCC does something dumb like:
1486 *
1487 * lea 0x8(%rsp), %rcx
1488 * ...
1489 * mov %rcx, %rsp
1490 */
1491 cfa->offset = -state->vals[op->src.reg].offset;
1492 state->stack_size = cfa->offset;
1493
1494 } else {
1495 cfa->base = CFI_UNDEFINED;
1496 cfa->offset = 0;
1497 }
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001498 }
1499
1500 break;
1501
1502 case OP_SRC_ADD:
1503 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1504
1505 /* add imm, %rsp */
1506 state->stack_size -= op->src.offset;
1507 if (cfa->base == CFI_SP)
1508 cfa->offset -= op->src.offset;
1509 break;
1510 }
1511
1512 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1513
1514 /* lea disp(%rbp), %rsp */
1515 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1516 break;
1517 }
1518
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001519 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001520
1521 /* drap: lea disp(%rsp), %drap */
1522 state->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001523
1524 /*
1525 * lea disp(%rsp), %reg
1526 *
1527 * This is needed for the rare case where GCC
1528 * does something dumb like:
1529 *
1530 * lea 0x8(%rsp), %rcx
1531 * ...
1532 * mov %rcx, %rsp
1533 */
1534 state->vals[op->dest.reg].base = CFI_CFA;
1535 state->vals[op->dest.reg].offset = \
1536 -state->stack_size + op->src.offset;
1537
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001538 break;
1539 }
1540
1541 if (state->drap && op->dest.reg == CFI_SP &&
1542 op->src.reg == state->drap_reg) {
1543
1544 /* drap: lea disp(%drap), %rsp */
1545 cfa->base = CFI_SP;
1546 cfa->offset = state->stack_size = -op->src.offset;
1547 state->drap_reg = CFI_UNDEFINED;
1548 state->drap = false;
1549 break;
1550 }
1551
1552 if (op->dest.reg == state->cfa.base) {
1553 WARN_FUNC("unsupported stack register modification",
1554 insn->sec, insn->offset);
1555 return -1;
1556 }
1557
1558 break;
1559
1560 case OP_SRC_AND:
1561 if (op->dest.reg != CFI_SP ||
1562 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1563 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1564 WARN_FUNC("unsupported stack pointer realignment",
1565 insn->sec, insn->offset);
1566 return -1;
1567 }
1568
1569 if (state->drap_reg != CFI_UNDEFINED) {
1570 /* drap: and imm, %rsp */
1571 cfa->base = state->drap_reg;
1572 cfa->offset = state->stack_size = 0;
1573 state->drap = true;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001574 }
1575
1576 /*
1577 * Older versions of GCC (4.8ish) realign the stack
1578 * without DRAP, with a frame pointer.
1579 */
1580
1581 break;
1582
1583 case OP_SRC_POP:
1584 if (!state->drap && op->dest.type == OP_DEST_REG &&
1585 op->dest.reg == cfa->base) {
1586
1587 /* pop %rbp */
1588 cfa->base = CFI_SP;
1589 }
1590
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001591 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1592 op->dest.type == OP_DEST_REG &&
1593 op->dest.reg == state->drap_reg &&
1594 state->drap_offset == -state->stack_size) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001595
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001596 /* drap: pop %drap */
1597 cfa->base = state->drap_reg;
1598 cfa->offset = 0;
1599 state->drap_offset = -1;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001600
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001601 } else if (regs[op->dest.reg].offset == -state->stack_size) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001602
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001603 /* pop %reg */
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001604 restore_reg(state, op->dest.reg);
1605 }
1606
1607 state->stack_size -= 8;
1608 if (cfa->base == CFI_SP)
1609 cfa->offset -= 8;
1610
1611 break;
1612
1613 case OP_SRC_REG_INDIRECT:
1614 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001615 op->src.offset == state->drap_offset) {
1616
1617 /* drap: mov disp(%rbp), %drap */
1618 cfa->base = state->drap_reg;
1619 cfa->offset = 0;
1620 state->drap_offset = -1;
1621 }
1622
1623 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001624 op->src.offset == regs[op->dest.reg].offset) {
1625
1626 /* drap: mov disp(%rbp), %reg */
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001627 restore_reg(state, op->dest.reg);
1628
1629 } else if (op->src.reg == cfa->base &&
1630 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1631
1632 /* mov disp(%rbp), %reg */
1633 /* mov disp(%rsp), %reg */
1634 restore_reg(state, op->dest.reg);
1635 }
1636
1637 break;
1638
1639 default:
1640 WARN_FUNC("unknown stack-related instruction",
1641 insn->sec, insn->offset);
1642 return -1;
1643 }
1644
1645 break;
1646
1647 case OP_DEST_PUSH:
1648 state->stack_size += 8;
1649 if (cfa->base == CFI_SP)
1650 cfa->offset += 8;
1651
1652 if (op->src.type != OP_SRC_REG)
1653 break;
1654
1655 if (state->drap) {
1656 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1657
1658 /* drap: push %drap */
1659 cfa->base = CFI_BP_INDIRECT;
1660 cfa->offset = -state->stack_size;
1661
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001662 /* save drap so we know when to restore it */
1663 state->drap_offset = -state->stack_size;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001664
1665 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1666
1667 /* drap: push %rbp */
1668 state->stack_size = 0;
1669
1670 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1671
1672 /* drap: push %reg */
1673 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1674 }
1675
1676 } else {
1677
1678 /* push %reg */
1679 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1680 }
1681
1682 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001683 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001684 cfa->base != CFI_BP)
1685 state->bp_scratch = true;
1686 break;
1687
1688 case OP_DEST_REG_INDIRECT:
1689
1690 if (state->drap) {
1691 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1692
1693 /* drap: mov %drap, disp(%rbp) */
1694 cfa->base = CFI_BP_INDIRECT;
1695 cfa->offset = op->dest.offset;
1696
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001697 /* save drap offset so we know when to restore it */
1698 state->drap_offset = op->dest.offset;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001699 }
1700
1701 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1702
1703 /* drap: mov reg, disp(%rbp) */
1704 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1705 }
1706
1707 } else if (op->dest.reg == cfa->base) {
1708
1709 /* mov reg, disp(%rbp) */
1710 /* mov reg, disp(%rsp) */
1711 save_reg(state, op->src.reg, CFI_CFA,
1712 op->dest.offset - state->cfa.offset);
1713 }
1714
1715 break;
1716
1717 case OP_DEST_LEAVE:
1718 if ((!state->drap && cfa->base != CFI_BP) ||
1719 (state->drap && cfa->base != state->drap_reg)) {
1720 WARN_FUNC("leave instruction with modified stack frame",
1721 insn->sec, insn->offset);
1722 return -1;
1723 }
1724
1725 /* leave (mov %rbp, %rsp; pop %rbp) */
1726
1727 state->stack_size = -state->regs[CFI_BP].offset - 8;
1728 restore_reg(state, CFI_BP);
1729
1730 if (!state->drap) {
1731 cfa->base = CFI_SP;
1732 cfa->offset -= 8;
1733 }
1734
1735 break;
1736
1737 case OP_DEST_MEM:
1738 if (op->src.type != OP_SRC_POP) {
1739 WARN_FUNC("unknown stack-related memory operation",
1740 insn->sec, insn->offset);
1741 return -1;
1742 }
1743
1744 /* pop mem */
1745 state->stack_size -= 8;
1746 if (cfa->base == CFI_SP)
1747 cfa->offset -= 8;
1748
1749 break;
1750
1751 default:
1752 WARN_FUNC("unknown stack-related instruction",
1753 insn->sec, insn->offset);
1754 return -1;
1755 }
1756
1757 return 0;
1758}
1759
1760static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1761{
1762 struct insn_state *state1 = &insn->state, *state2 = state;
1763 int i;
1764
1765 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1766 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1767 insn->sec, insn->offset,
1768 state1->cfa.base, state1->cfa.offset,
1769 state2->cfa.base, state2->cfa.offset);
1770
1771 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1772 for (i = 0; i < CFI_NUM_REGS; i++) {
1773 if (!memcmp(&state1->regs[i], &state2->regs[i],
1774 sizeof(struct cfi_reg)))
1775 continue;
1776
1777 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1778 insn->sec, insn->offset,
1779 i, state1->regs[i].base, state1->regs[i].offset,
1780 i, state2->regs[i].base, state2->regs[i].offset);
1781 break;
1782 }
1783
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001784 } else if (state1->type != state2->type) {
1785 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1786 insn->sec, insn->offset, state1->type, state2->type);
1787
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001788 } else if (state1->drap != state2->drap ||
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001789 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1790 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1791 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001792 insn->sec, insn->offset,
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001793 state1->drap, state1->drap_reg, state1->drap_offset,
1794 state2->drap, state2->drap_reg, state2->drap_offset);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001795
1796 } else
1797 return true;
1798
1799 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001800}
1801
1802/*
1803 * Follow the branch starting at the given instruction, and recursively follow
1804 * any other branches (jumps). Meanwhile, track the frame pointer state at
1805 * each instruction and validate all the rules described in
1806 * tools/objtool/Documentation/stack-validation.txt.
1807 */
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001808static int validate_branch(struct objtool_file *file, struct instruction *first,
1809 struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001810{
1811 struct alternative *alt;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001812 struct instruction *insn, *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001813 struct section *sec;
1814 struct symbol *func = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001815 int ret;
1816
1817 insn = first;
1818 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001819
1820 if (insn->alt_group && list_empty(&insn->alts)) {
1821 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1822 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001823 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001824 }
1825
1826 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001827 next_insn = next_insn_same_sec(file, insn);
1828
Josh Poimboeuf13810432018-05-09 22:39:15 -05001829 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05001830 WARN("%s() falls through to next function %s()",
1831 func->name, insn->func->name);
1832 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001833 }
1834
Josh Poimboeuf13810432018-05-09 22:39:15 -05001835 func = insn->func ? insn->func->pfunc : NULL;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001836
Josh Poimboeuf48550222017-07-07 09:19:42 -05001837 if (func && insn->ignore) {
1838 WARN_FUNC("BUG: why am I validating an ignored function?",
1839 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001840 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001841 }
1842
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001843 if (insn->visited) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001844 if (!insn->hint && !insn_state_match(insn, &state))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001845 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001846
1847 return 0;
1848 }
1849
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001850 if (insn->hint) {
1851 if (insn->restore) {
1852 struct instruction *save_insn, *i;
1853
1854 i = insn;
1855 save_insn = NULL;
Josh Poimboeuf13810432018-05-09 22:39:15 -05001856 func_for_each_insn_continue_reverse(file, insn->func, i) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001857 if (i->save) {
1858 save_insn = i;
1859 break;
1860 }
1861 }
1862
1863 if (!save_insn) {
1864 WARN_FUNC("no corresponding CFI save for CFI restore",
1865 sec, insn->offset);
1866 return 1;
1867 }
1868
1869 if (!save_insn->visited) {
1870 /*
1871 * Oops, no state to copy yet.
1872 * Hopefully we can reach this
1873 * instruction from another branch
1874 * after the save insn has been
1875 * visited.
1876 */
1877 if (insn == first)
1878 return 0;
1879
1880 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1881 sec, insn->offset);
1882 return 1;
1883 }
1884
1885 insn->state = save_insn->state;
1886 }
1887
1888 state = insn->state;
1889
1890 } else
1891 insn->state = state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001892
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001893 insn->visited = true;
1894
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001895 if (!insn->ignore_alts) {
1896 list_for_each_entry(alt, &insn->alts, list) {
1897 ret = validate_branch(file, alt->insn, state);
1898 if (ret)
1899 return 1;
1900 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001901 }
1902
1903 switch (insn->type) {
1904
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001905 case INSN_RETURN:
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001906 if (func && has_modified_stack_frame(&state)) {
1907 WARN_FUNC("return with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001908 sec, insn->offset);
1909 return 1;
1910 }
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001911
1912 if (state.bp_scratch) {
1913 WARN("%s uses BP as a scratch register",
1914 insn->func->name);
1915 return 1;
1916 }
1917
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001918 return 0;
1919
1920 case INSN_CALL:
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001921 if (is_fentry_call(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001922 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001923
1924 ret = dead_end_function(file, insn->call_dest);
1925 if (ret == 1)
1926 return 0;
1927 if (ret == -1)
1928 return 1;
1929
1930 /* fallthrough */
1931 case INSN_CALL_DYNAMIC:
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001932 if (!no_fp && func && !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001933 WARN_FUNC("call without frame pointer save/setup",
1934 sec, insn->offset);
1935 return 1;
1936 }
1937 break;
1938
1939 case INSN_JUMP_CONDITIONAL:
1940 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf48550222017-07-07 09:19:42 -05001941 if (insn->jump_dest &&
1942 (!func || !insn->jump_dest->func ||
Josh Poimboeuf13810432018-05-09 22:39:15 -05001943 insn->jump_dest->func->pfunc == func)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001944 ret = validate_branch(file, insn->jump_dest,
1945 state);
1946 if (ret)
1947 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001948
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001949 } else if (func && has_modified_stack_frame(&state)) {
1950 WARN_FUNC("sibling call from callable instruction with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001951 sec, insn->offset);
1952 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001953 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001954
1955 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1956 return 0;
1957
1958 break;
1959
1960 case INSN_JUMP_DYNAMIC:
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001961 if (func && list_empty(&insn->alts) &&
1962 has_modified_stack_frame(&state)) {
1963 WARN_FUNC("sibling call from callable instruction with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001964 sec, insn->offset);
1965 return 1;
1966 }
1967
1968 return 0;
1969
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001970 case INSN_CONTEXT_SWITCH:
1971 if (func && (!next_insn || !next_insn->hint)) {
1972 WARN_FUNC("unsupported instruction in callable function",
1973 sec, insn->offset);
1974 return 1;
1975 }
1976 return 0;
1977
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001978 case INSN_STACK:
1979 if (update_insn_state(insn, &state))
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001980 return 1;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001981
1982 break;
1983
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001984 default:
1985 break;
1986 }
1987
1988 if (insn->dead_end)
1989 return 0;
1990
Josh Poimboeuf00d96182017-09-18 21:43:30 -05001991 if (!next_insn) {
1992 if (state.cfa.base == CFI_UNDEFINED)
1993 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001994 WARN("%s: unexpected end of section", sec->name);
1995 return 1;
1996 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05001997
1998 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001999 }
2000
2001 return 0;
2002}
2003
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002004static int validate_unwind_hints(struct objtool_file *file)
2005{
2006 struct instruction *insn;
2007 int ret, warnings = 0;
2008 struct insn_state state;
2009
2010 if (!file->hints)
2011 return 0;
2012
2013 clear_insn_state(&state);
2014
2015 for_each_insn(file, insn) {
2016 if (insn->hint && !insn->visited) {
2017 ret = validate_branch(file, insn, state);
2018 warnings += ret;
2019 }
2020 }
2021
2022 return warnings;
2023}
2024
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002025static int validate_retpoline(struct objtool_file *file)
2026{
2027 struct instruction *insn;
2028 int warnings = 0;
2029
2030 for_each_insn(file, insn) {
2031 if (insn->type != INSN_JUMP_DYNAMIC &&
2032 insn->type != INSN_CALL_DYNAMIC)
2033 continue;
2034
2035 if (insn->retpoline_safe)
2036 continue;
2037
Peter Zijlstraca41b972018-01-31 10:18:28 +01002038 /*
2039 * .init.text code is ran before userspace and thus doesn't
2040 * strictly need retpolines, except for modules which are
2041 * loaded late, they very much do need retpoline in their
2042 * .init.text
2043 */
2044 if (!strcmp(insn->sec->name, ".init.text") && !module)
2045 continue;
2046
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002047 WARN_FUNC("indirect %s found in RETPOLINE build",
2048 insn->sec, insn->offset,
2049 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2050
2051 warnings++;
2052 }
2053
2054 return warnings;
2055}
2056
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002057static bool is_kasan_insn(struct instruction *insn)
2058{
2059 return (insn->type == INSN_CALL &&
2060 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2061}
2062
2063static bool is_ubsan_insn(struct instruction *insn)
2064{
2065 return (insn->type == INSN_CALL &&
2066 !strcmp(insn->call_dest->name,
2067 "__ubsan_handle_builtin_unreachable"));
2068}
2069
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002070static bool ignore_unreachable_insn(struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002071{
2072 int i;
2073
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002074 if (insn->ignore || insn->type == INSN_NOP)
2075 return true;
2076
2077 /*
2078 * Ignore any unused exceptions. This can happen when a whitelisted
2079 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002080 *
2081 * Also ignore alternative replacement instructions. This can happen
2082 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002083 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002084 if (!strcmp(insn->sec->name, ".fixup") ||
2085 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2086 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002087 return true;
2088
2089 /*
2090 * Check if this (or a subsequent) instruction is related to
2091 * CONFIG_UBSAN or CONFIG_KASAN.
2092 *
2093 * End the search at 5 instructions to avoid going into the weeds.
2094 */
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002095 if (!insn->func)
2096 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002097 for (i = 0; i < 5; i++) {
2098
2099 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2100 return true;
2101
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002102 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2103 if (insn->jump_dest &&
2104 insn->jump_dest->func == insn->func) {
2105 insn = insn->jump_dest;
2106 continue;
2107 }
2108
2109 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002110 }
2111
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002112 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002113 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002114
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002115 insn = list_next_entry(insn, list);
2116 }
2117
2118 return false;
2119}
2120
2121static int validate_functions(struct objtool_file *file)
2122{
2123 struct section *sec;
2124 struct symbol *func;
2125 struct instruction *insn;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002126 struct insn_state state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002127 int ret, warnings = 0;
2128
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002129 clear_insn_state(&state);
2130
2131 state.cfa = initial_func_cfi.cfa;
2132 memcpy(&state.regs, &initial_func_cfi.regs,
2133 CFI_NUM_REGS * sizeof(struct cfi_reg));
2134 state.stack_size = initial_func_cfi.cfa.offset;
2135
2136 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002137 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf13810432018-05-09 22:39:15 -05002138 if (func->type != STT_FUNC || func->pfunc != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002139 continue;
2140
2141 insn = find_insn(file, sec, func->offset);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002142 if (!insn || insn->ignore)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002143 continue;
2144
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002145 ret = validate_branch(file, insn, state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002146 warnings += ret;
2147 }
2148 }
2149
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002150 return warnings;
2151}
2152
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002153static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002154{
2155 struct instruction *insn;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002156
2157 if (file->ignore_unreachables)
2158 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002159
2160 for_each_insn(file, insn) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002161 if (insn->visited || ignore_unreachable_insn(insn))
2162 continue;
2163
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002164 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2165 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002166 }
2167
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002168 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002169}
2170
2171static void cleanup(struct objtool_file *file)
2172{
2173 struct instruction *insn, *tmpinsn;
2174 struct alternative *alt, *tmpalt;
2175
2176 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2177 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2178 list_del(&alt->list);
2179 free(alt);
2180 }
2181 list_del(&insn->list);
2182 hash_del(&insn->hash);
2183 free(insn);
2184 }
2185 elf_close(file->elf);
2186}
2187
Josh Poimboeuf0c671812019-03-18 19:09:38 -05002188static struct objtool_file file;
2189
Peter Zijlstra43a45252018-01-16 17:16:32 +01002190int check(const char *_objname, bool orc)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002191{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002192 int ret, warnings = 0;
2193
2194 objname = _objname;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002195
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002196 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002197 if (!file.elf)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002198 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002199
2200 INIT_LIST_HEAD(&file.insn_list);
2201 hash_init(file.insn_hash);
2202 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002203 file.c_file = find_section_by_name(file.elf, ".comment");
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002204 file.ignore_unreachables = no_unreachable;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002205 file.hints = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002206
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002207 arch_initial_func_cfi_state(&initial_func_cfi);
2208
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002209 ret = decode_sections(&file);
2210 if (ret < 0)
2211 goto out;
2212 warnings += ret;
2213
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002214 if (list_empty(&file.insn_list))
2215 goto out;
2216
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002217 if (retpoline) {
2218 ret = validate_retpoline(&file);
2219 if (ret < 0)
2220 return ret;
2221 warnings += ret;
2222 }
2223
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002224 ret = validate_functions(&file);
2225 if (ret < 0)
2226 goto out;
2227 warnings += ret;
2228
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002229 ret = validate_unwind_hints(&file);
2230 if (ret < 0)
2231 goto out;
2232 warnings += ret;
2233
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002234 if (!warnings) {
2235 ret = validate_reachable_instructions(&file);
2236 if (ret < 0)
2237 goto out;
2238 warnings += ret;
2239 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002240
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002241 if (orc) {
2242 ret = create_orc(&file);
2243 if (ret < 0)
2244 goto out;
2245
2246 ret = create_orc_sections(&file);
2247 if (ret < 0)
2248 goto out;
2249
2250 ret = elf_write(file.elf);
2251 if (ret < 0)
2252 goto out;
2253 }
2254
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002255out:
2256 cleanup(&file);
2257
2258 /* ignore warnings for now until we get all the code cleaned up */
2259 if (ret || warnings)
2260 return 0;
2261 return 0;
2262}