blob: f4bbce838433f96776d73aacb995812f7f069928 [file] [log] [blame]
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001/*
2 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <string.h>
19#include <stdlib.h>
20
Peter Zijlstra43a45252018-01-16 17:16:32 +010021#include "builtin.h"
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050022#include "check.h"
23#include "elf.h"
24#include "special.h"
25#include "arch.h"
26#include "warn.h"
27
28#include <linux/hashtable.h>
29#include <linux/kernel.h>
30
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050031struct alternative {
32 struct list_head list;
33 struct instruction *insn;
34};
35
36const char *objname;
Josh Poimboeufbaa41462017-06-28 10:11:07 -050037struct cfi_state initial_func_cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050038
Josh Poimboeuf627fce12017-07-11 10:33:42 -050039struct instruction *find_insn(struct objtool_file *file,
40 struct section *sec, unsigned long offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050041{
42 struct instruction *insn;
43
44 hash_for_each_possible(file->insn_hash, insn, hash, offset)
45 if (insn->sec == sec && insn->offset == offset)
46 return insn;
47
48 return NULL;
49}
50
51static struct instruction *next_insn_same_sec(struct objtool_file *file,
52 struct instruction *insn)
53{
54 struct instruction *next = list_next_entry(insn, list);
55
Josh Poimboeufbaa41462017-06-28 10:11:07 -050056 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050057 return NULL;
58
59 return next;
60}
61
Josh 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 Poimboeufbaa41462017-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 Poimboeufdcc914f2017-06-28 10:11:05 -0500167 };
168
169 if (func->bind == STB_WEAK)
170 return 0;
171
172 if (func->bind == STB_GLOBAL)
173 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
174 if (!strcmp(func->name, global_noreturns[i]))
175 return 1;
176
Josh Poimboeuf13810432018-05-09 22:39:15 -0500177 if (!func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500178 return 0;
179
Josh Poimboeuf13810432018-05-09 22:39:15 -0500180 insn = find_insn(file, func->sec, func->offset);
181 if (!insn->func)
182 return 0;
183
184 func_for_each_insn_all(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500185 empty = false;
186
187 if (insn->type == INSN_RETURN)
188 return 0;
189 }
190
191 if (empty)
192 return 0;
193
194 /*
195 * A function can have a sibling call instead of a return. In that
196 * case, the function's dead-end status depends on whether the target
197 * of the sibling call returns.
198 */
Josh Poimboeuf13810432018-05-09 22:39:15 -0500199 func_for_each_insn_all(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500200 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
201 struct instruction *dest = insn->jump_dest;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500202
203 if (!dest)
204 /* sibling call to another file */
205 return 0;
206
Josh Poimboeuf13810432018-05-09 22:39:15 -0500207 if (dest->func && dest->func->pfunc != insn->func->pfunc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500208
Josh Poimboeuf13810432018-05-09 22:39:15 -0500209 /* local sibling call */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500210 if (recursion == 5) {
Josh Poimboeuf0afd0d92018-05-09 22:39:14 -0500211 /*
212 * Infinite recursion: two functions
213 * have sibling calls to each other.
214 * This is a very rare case. It means
215 * they aren't dead ends.
216 */
217 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500218 }
219
Josh Poimboeuf13810432018-05-09 22:39:15 -0500220 return __dead_end_function(file, dest->func,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500221 recursion + 1);
222 }
223 }
224
225 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
226 /* sibling call */
227 return 0;
228 }
229
230 return 1;
231}
232
233static int dead_end_function(struct objtool_file *file, struct symbol *func)
234{
235 return __dead_end_function(file, func, 0);
236}
237
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500238static void clear_insn_state(struct insn_state *state)
239{
240 int i;
241
242 memset(state, 0, sizeof(*state));
243 state->cfa.base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500244 for (i = 0; i < CFI_NUM_REGS; i++) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500245 state->regs[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500246 state->vals[i].base = CFI_UNDEFINED;
247 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500248 state->drap_reg = CFI_UNDEFINED;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -0500249 state->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500250}
251
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500252/*
253 * Call the arch-specific instruction decoder for all the instructions and add
254 * them to the global instruction list.
255 */
256static int decode_instructions(struct objtool_file *file)
257{
258 struct section *sec;
259 struct symbol *func;
260 unsigned long offset;
261 struct instruction *insn;
262 int ret;
263
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500264 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500265
266 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
267 continue;
268
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500269 if (strcmp(sec->name, ".altinstr_replacement") &&
270 strcmp(sec->name, ".altinstr_aux") &&
271 strncmp(sec->name, ".discard.", 9))
272 sec->text = true;
273
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500274 for (offset = 0; offset < sec->len; offset += insn->len) {
275 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500276 if (!insn) {
277 WARN("malloc failed");
278 return -1;
279 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500280 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500281 INIT_LIST_HEAD(&insn->alts);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500282 clear_insn_state(&insn->state);
283
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500284 insn->sec = sec;
285 insn->offset = offset;
286
287 ret = arch_decode_instruction(file->elf, sec, offset,
288 sec->len - offset,
289 &insn->len, &insn->type,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500290 &insn->immediate,
291 &insn->stack_op);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500292 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500293 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500294
295 if (!insn->type || insn->type > INSN_LAST) {
296 WARN_FUNC("invalid instruction type %d",
297 insn->sec, insn->offset, insn->type);
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500298 ret = -1;
299 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500300 }
301
302 hash_add(file->insn_hash, &insn->hash, insn->offset);
303 list_add_tail(&insn->list, &file->insn_list);
304 }
305
306 list_for_each_entry(func, &sec->symbol_list, list) {
307 if (func->type != STT_FUNC)
308 continue;
309
310 if (!find_insn(file, sec, func->offset)) {
311 WARN("%s(): can't find starting instruction",
312 func->name);
313 return -1;
314 }
315
316 func_for_each_insn(file, func, insn)
317 if (!insn->func)
318 insn->func = func;
319 }
320 }
321
322 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500323
324err:
325 free(insn);
326 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500327}
328
329/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500330 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500331 */
332static int add_dead_ends(struct objtool_file *file)
333{
334 struct section *sec;
335 struct rela *rela;
336 struct instruction *insn;
337 bool found;
338
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500339 /*
340 * By default, "ud2" is a dead end unless otherwise annotated, because
341 * GCC 7 inserts it for certain divide-by-zero cases.
342 */
343 for_each_insn(file, insn)
344 if (insn->type == INSN_BUG)
345 insn->dead_end = true;
346
347 /*
348 * Check for manually annotated dead ends.
349 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500350 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
351 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500352 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500353
354 list_for_each_entry(rela, &sec->rela_list, list) {
355 if (rela->sym->type != STT_SECTION) {
356 WARN("unexpected relocation symbol type in %s", sec->name);
357 return -1;
358 }
359 insn = find_insn(file, rela->sym->sec, rela->addend);
360 if (insn)
361 insn = list_prev_entry(insn, list);
362 else if (rela->addend == rela->sym->sec->len) {
363 found = false;
364 list_for_each_entry_reverse(insn, &file->insn_list, list) {
365 if (insn->sec == rela->sym->sec) {
366 found = true;
367 break;
368 }
369 }
370
371 if (!found) {
372 WARN("can't find unreachable insn at %s+0x%x",
373 rela->sym->sec->name, rela->addend);
374 return -1;
375 }
376 } else {
377 WARN("can't find unreachable insn at %s+0x%x",
378 rela->sym->sec->name, rela->addend);
379 return -1;
380 }
381
382 insn->dead_end = true;
383 }
384
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500385reachable:
386 /*
387 * These manually annotated reachable checks are needed for GCC 4.4,
388 * where the Linux unreachable() macro isn't supported. In that case
389 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
390 * not a dead end.
391 */
392 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
393 if (!sec)
394 return 0;
395
396 list_for_each_entry(rela, &sec->rela_list, list) {
397 if (rela->sym->type != STT_SECTION) {
398 WARN("unexpected relocation symbol type in %s", sec->name);
399 return -1;
400 }
401 insn = find_insn(file, rela->sym->sec, rela->addend);
402 if (insn)
403 insn = list_prev_entry(insn, list);
404 else if (rela->addend == rela->sym->sec->len) {
405 found = false;
406 list_for_each_entry_reverse(insn, &file->insn_list, list) {
407 if (insn->sec == rela->sym->sec) {
408 found = true;
409 break;
410 }
411 }
412
413 if (!found) {
414 WARN("can't find reachable insn at %s+0x%x",
415 rela->sym->sec->name, rela->addend);
416 return -1;
417 }
418 } else {
419 WARN("can't find reachable insn at %s+0x%x",
420 rela->sym->sec->name, rela->addend);
421 return -1;
422 }
423
424 insn->dead_end = false;
425 }
426
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500427 return 0;
428}
429
430/*
431 * Warnings shouldn't be reported for ignored functions.
432 */
433static void add_ignores(struct objtool_file *file)
434{
435 struct instruction *insn;
436 struct section *sec;
437 struct symbol *func;
438
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500439 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500440 list_for_each_entry(func, &sec->symbol_list, list) {
441 if (func->type != STT_FUNC)
442 continue;
443
444 if (!ignore_func(file, func))
445 continue;
446
Josh Poimboeuf13810432018-05-09 22:39:15 -0500447 func_for_each_insn_all(file, func, insn)
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500448 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500449 }
450 }
451}
452
453/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000454 * FIXME: For now, just ignore any alternatives which add retpolines. This is
455 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
456 * But it at least allows objtool to understand the control flow *around* the
457 * retpoline.
458 */
459static int add_nospec_ignores(struct objtool_file *file)
460{
461 struct section *sec;
462 struct rela *rela;
463 struct instruction *insn;
464
465 sec = find_section_by_name(file->elf, ".rela.discard.nospec");
466 if (!sec)
467 return 0;
468
469 list_for_each_entry(rela, &sec->rela_list, list) {
470 if (rela->sym->type != STT_SECTION) {
471 WARN("unexpected relocation symbol type in %s", sec->name);
472 return -1;
473 }
474
475 insn = find_insn(file, rela->sym->sec, rela->addend);
476 if (!insn) {
477 WARN("bad .discard.nospec entry");
478 return -1;
479 }
480
481 insn->ignore_alts = true;
482 }
483
484 return 0;
485}
486
487/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500488 * Find the destination instructions for all jumps.
489 */
490static int add_jump_destinations(struct objtool_file *file)
491{
492 struct instruction *insn;
493 struct rela *rela;
494 struct section *dest_sec;
495 unsigned long dest_off;
496
497 for_each_insn(file, insn) {
498 if (insn->type != INSN_JUMP_CONDITIONAL &&
499 insn->type != INSN_JUMP_UNCONDITIONAL)
500 continue;
501
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500502 if (insn->ignore)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500503 continue;
504
505 rela = find_rela_by_dest_range(insn->sec, insn->offset,
506 insn->len);
507 if (!rela) {
508 dest_sec = insn->sec;
509 dest_off = insn->offset + insn->len + insn->immediate;
510 } else if (rela->sym->type == STT_SECTION) {
511 dest_sec = rela->sym->sec;
512 dest_off = rela->addend + 4;
513 } else if (rela->sym->sec->idx) {
514 dest_sec = rela->sym->sec;
515 dest_off = rela->sym->sym.st_value + rela->addend + 4;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000516 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
517 /*
518 * Retpoline jumps are really dynamic jumps in
519 * disguise, so convert them accordingly.
520 */
521 insn->type = INSN_JUMP_DYNAMIC;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100522 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000523 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500524 } else {
525 /* sibling call */
526 insn->jump_dest = 0;
527 continue;
528 }
529
530 insn->jump_dest = find_insn(file, dest_sec, dest_off);
531 if (!insn->jump_dest) {
532
533 /*
534 * This is a special case where an alt instruction
535 * jumps past the end of the section. These are
536 * handled later in handle_group_alt().
537 */
538 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
539 continue;
540
541 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
542 insn->sec, insn->offset, dest_sec->name,
543 dest_off);
544 return -1;
545 }
546 }
547
548 return 0;
549}
550
551/*
552 * Find the destination instructions for all calls.
553 */
554static int add_call_destinations(struct objtool_file *file)
555{
556 struct instruction *insn;
557 unsigned long dest_off;
558 struct rela *rela;
559
560 for_each_insn(file, insn) {
561 if (insn->type != INSN_CALL)
562 continue;
563
564 rela = find_rela_by_dest_range(insn->sec, insn->offset,
565 insn->len);
566 if (!rela) {
567 dest_off = insn->offset + insn->len + insn->immediate;
568 insn->call_dest = find_symbol_by_offset(insn->sec,
569 dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600570
571 if (!insn->call_dest && !insn->ignore) {
572 WARN_FUNC("unsupported intra-function call",
573 insn->sec, insn->offset);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100574 if (retpoline)
575 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 -0500576 return -1;
577 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600578
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500579 } else if (rela->sym->type == STT_SECTION) {
580 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
581 rela->addend+4);
582 if (!insn->call_dest ||
583 insn->call_dest->type != STT_FUNC) {
584 WARN_FUNC("can't find call dest symbol at %s+0x%x",
585 insn->sec, insn->offset,
586 rela->sym->sec->name,
587 rela->addend + 4);
588 return -1;
589 }
590 } else
591 insn->call_dest = rela->sym;
592 }
593
594 return 0;
595}
596
597/*
598 * The .alternatives section requires some extra special care, over and above
599 * what other special sections require:
600 *
601 * 1. Because alternatives are patched in-place, we need to insert a fake jump
602 * instruction at the end so that validate_branch() skips all the original
603 * replaced instructions when validating the new instruction path.
604 *
605 * 2. An added wrinkle is that the new instruction length might be zero. In
606 * that case the old instructions are replaced with noops. We simulate that
607 * by creating a fake jump as the only new instruction.
608 *
609 * 3. In some cases, the alternative section includes an instruction which
610 * conditionally jumps to the _end_ of the entry. We have to modify these
611 * jumps' destinations to point back to .text rather than the end of the
612 * entry in .altinstr_replacement.
613 *
614 * 4. It has been requested that we don't validate the !POPCNT feature path
615 * which is a "very very small percentage of machines".
616 */
617static int handle_group_alt(struct objtool_file *file,
618 struct special_alt *special_alt,
619 struct instruction *orig_insn,
620 struct instruction **new_insn)
621{
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600622 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500623 unsigned long dest_off;
624
625 last_orig_insn = NULL;
626 insn = orig_insn;
627 sec_for_each_insn_from(file, insn) {
628 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
629 break;
630
631 if (special_alt->skip_orig)
632 insn->type = INSN_NOP;
633
634 insn->alt_group = true;
635 last_orig_insn = insn;
636 }
637
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600638 if (next_insn_same_sec(file, last_orig_insn)) {
639 fake_jump = malloc(sizeof(*fake_jump));
640 if (!fake_jump) {
641 WARN("malloc failed");
642 return -1;
643 }
644 memset(fake_jump, 0, sizeof(*fake_jump));
645 INIT_LIST_HEAD(&fake_jump->alts);
646 clear_insn_state(&fake_jump->state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500647
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600648 fake_jump->sec = special_alt->new_sec;
649 fake_jump->offset = -1;
650 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
651 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
652 fake_jump->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500653 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500654
655 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600656 if (!fake_jump) {
657 WARN("%s: empty alternative at end of section",
658 special_alt->orig_sec->name);
659 return -1;
660 }
661
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500662 *new_insn = fake_jump;
663 return 0;
664 }
665
666 last_new_insn = NULL;
667 insn = *new_insn;
668 sec_for_each_insn_from(file, insn) {
669 if (insn->offset >= special_alt->new_off + special_alt->new_len)
670 break;
671
672 last_new_insn = insn;
673
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600674 insn->ignore = orig_insn->ignore_alts;
675
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500676 if (insn->type != INSN_JUMP_CONDITIONAL &&
677 insn->type != INSN_JUMP_UNCONDITIONAL)
678 continue;
679
680 if (!insn->immediate)
681 continue;
682
683 dest_off = insn->offset + insn->len + insn->immediate;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600684 if (dest_off == special_alt->new_off + special_alt->new_len) {
685 if (!fake_jump) {
686 WARN("%s: alternative jump to end of section",
687 special_alt->orig_sec->name);
688 return -1;
689 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500690 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600691 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500692
693 if (!insn->jump_dest) {
694 WARN_FUNC("can't find alternative jump destination",
695 insn->sec, insn->offset);
696 return -1;
697 }
698 }
699
700 if (!last_new_insn) {
701 WARN_FUNC("can't find last new alternative instruction",
702 special_alt->new_sec, special_alt->new_off);
703 return -1;
704 }
705
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600706 if (fake_jump)
707 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500708
709 return 0;
710}
711
712/*
713 * A jump table entry can either convert a nop to a jump or a jump to a nop.
714 * If the original instruction is a jump, make the alt entry an effective nop
715 * by just skipping the original instruction.
716 */
717static int handle_jump_alt(struct objtool_file *file,
718 struct special_alt *special_alt,
719 struct instruction *orig_insn,
720 struct instruction **new_insn)
721{
722 if (orig_insn->type == INSN_NOP)
723 return 0;
724
725 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
726 WARN_FUNC("unsupported instruction at jump label",
727 orig_insn->sec, orig_insn->offset);
728 return -1;
729 }
730
731 *new_insn = list_next_entry(orig_insn, list);
732 return 0;
733}
734
735/*
736 * Read all the special sections which have alternate instructions which can be
737 * patched in or redirected to at runtime. Each instruction having alternate
738 * instruction(s) has them added to its insn->alts list, which will be
739 * traversed in validate_branch().
740 */
741static int add_special_section_alts(struct objtool_file *file)
742{
743 struct list_head special_alts;
744 struct instruction *orig_insn, *new_insn;
745 struct special_alt *special_alt, *tmp;
746 struct alternative *alt;
747 int ret;
748
749 ret = special_get_alts(file->elf, &special_alts);
750 if (ret)
751 return ret;
752
753 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500754
755 orig_insn = find_insn(file, special_alt->orig_sec,
756 special_alt->orig_off);
757 if (!orig_insn) {
758 WARN_FUNC("special: can't find orig instruction",
759 special_alt->orig_sec, special_alt->orig_off);
760 ret = -1;
761 goto out;
762 }
763
764 new_insn = NULL;
765 if (!special_alt->group || special_alt->new_len) {
766 new_insn = find_insn(file, special_alt->new_sec,
767 special_alt->new_off);
768 if (!new_insn) {
769 WARN_FUNC("special: can't find new instruction",
770 special_alt->new_sec,
771 special_alt->new_off);
772 ret = -1;
773 goto out;
774 }
775 }
776
777 if (special_alt->group) {
778 ret = handle_group_alt(file, special_alt, orig_insn,
779 &new_insn);
780 if (ret)
781 goto out;
782 } else if (special_alt->jump_or_nop) {
783 ret = handle_jump_alt(file, special_alt, orig_insn,
784 &new_insn);
785 if (ret)
786 goto out;
787 }
788
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000789 alt = malloc(sizeof(*alt));
790 if (!alt) {
791 WARN("malloc failed");
792 ret = -1;
793 goto out;
794 }
795
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500796 alt->insn = new_insn;
797 list_add_tail(&alt->list, &orig_insn->alts);
798
799 list_del(&special_alt->list);
800 free(special_alt);
801 }
802
803out:
804 return ret;
805}
806
Josh Poimboeuf13810432018-05-09 22:39:15 -0500807static int add_switch_table(struct objtool_file *file, struct instruction *insn,
808 struct rela *table, struct rela *next_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500809{
810 struct rela *rela = table;
811 struct instruction *alt_insn;
812 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500813 struct symbol *pfunc = insn->func->pfunc;
814 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500815
816 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
817 if (rela == next_table)
818 break;
819
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500820 /* Make sure the switch table entries are consecutive: */
821 if (prev_offset && rela->offset != prev_offset + 8)
822 break;
823
824 /* Detect function pointers from contiguous objects: */
825 if (rela->sym->sec == pfunc->sec &&
826 rela->addend == pfunc->offset)
827 break;
828
Josh Poimboeuf13810432018-05-09 22:39:15 -0500829 alt_insn = find_insn(file, rela->sym->sec, rela->addend);
830 if (!alt_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500831 break;
832
Josh Poimboeuf13810432018-05-09 22:39:15 -0500833 /* Make sure the jmp dest is in the function or subfunction: */
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500834 if (alt_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -0500835 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500836
837 alt = malloc(sizeof(*alt));
838 if (!alt) {
839 WARN("malloc failed");
840 return -1;
841 }
842
843 alt->insn = alt_insn;
844 list_add_tail(&alt->list, &insn->alts);
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500845 prev_offset = rela->offset;
846 }
847
848 if (!prev_offset) {
849 WARN_FUNC("can't find switch jump table",
850 insn->sec, insn->offset);
851 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500852 }
853
854 return 0;
855}
856
857/*
858 * find_switch_table() - Given a dynamic jump, find the switch jump table in
859 * .rodata associated with it.
860 *
861 * There are 3 basic patterns:
862 *
863 * 1. jmpq *[rodata addr](,%reg,8)
864 *
865 * This is the most common case by far. It jumps to an address in a simple
866 * jump table which is stored in .rodata.
867 *
868 * 2. jmpq *[rodata addr](%rip)
869 *
870 * This is caused by a rare GCC quirk, currently only seen in three driver
871 * functions in the kernel, only with certain obscure non-distro configs.
872 *
873 * As part of an optimization, GCC makes a copy of an existing switch jump
874 * table, modifies it, and then hard-codes the jump (albeit with an indirect
875 * jump) to use a single entry in the table. The rest of the jump table and
876 * some of its jump targets remain as dead code.
877 *
878 * In such a case we can just crudely ignore all unreachable instruction
879 * warnings for the entire object file. Ideally we would just ignore them
880 * for the function, but that would require redesigning the code quite a
881 * bit. And honestly that's just not worth doing: unreachable instruction
882 * warnings are of questionable value anyway, and this is such a rare issue.
883 *
884 * 3. mov [rodata addr],%reg1
885 * ... some instructions ...
886 * jmpq *(%reg1,%reg2,8)
887 *
888 * This is a fairly uncommon pattern which is new for GCC 6. As of this
889 * writing, there are 11 occurrences of it in the allmodconfig kernel.
890 *
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100891 * As of GCC 7 there are quite a few more of these and the 'in between' code
892 * is significant. Esp. with KASAN enabled some of the code between the mov
893 * and jmpq uses .rodata itself, which can confuse things.
894 *
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500895 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
896 * ensure the same register is used in the mov and jump instructions.
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100897 *
898 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500899 */
900static struct rela *find_switch_table(struct objtool_file *file,
901 struct symbol *func,
902 struct instruction *insn)
903{
904 struct rela *text_rela, *rodata_rela;
905 struct instruction *orig_insn = insn;
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -0500906 unsigned long table_offset;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500907
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -0500908 /* case 1 & 2 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500909 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
Josh Poimboeuffd35c882018-05-10 17:48:49 -0500910 if (text_rela && text_rela->sym == file->rodata->sym &&
911 !find_symbol_containing(file->rodata, text_rela->addend)) {
912
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -0500913 table_offset = text_rela->addend;
914 if (text_rela->type == R_X86_64_PC32) {
915 /* case 2 */
916 table_offset += 4;
917 file->ignore_unreachables = true;
918 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500919
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -0500920 rodata_rela = find_rela_by_dest(file->rodata, table_offset);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500921 if (!rodata_rela)
922 return NULL;
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100923
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500924 return rodata_rela;
925 }
926
927 /* case 3 */
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100928 /*
929 * Backward search using the @first_jump_src links, these help avoid
930 * much of the 'in between' code. Which avoids us getting confused by
931 * it.
932 */
933 for (insn = list_prev_entry(insn, list);
934
935 &insn->list != &file->insn_list &&
936 insn->sec == func->sec &&
937 insn->offset >= func->offset;
938
939 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
940
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500941 if (insn->type == INSN_JUMP_DYNAMIC)
942 break;
943
944 /* allow small jumps within the range */
945 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
946 insn->jump_dest &&
947 (insn->jump_dest->offset <= insn->offset ||
948 insn->jump_dest->offset > orig_insn->offset))
949 break;
950
951 /* look for a relocation which references .rodata */
952 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
953 insn->len);
954 if (!text_rela || text_rela->sym != file->rodata->sym)
955 continue;
956
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -0500957 table_offset = text_rela->addend;
958 if (text_rela->type == R_X86_64_PC32)
959 table_offset += 4;
960
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500961 /*
962 * Make sure the .rodata address isn't associated with a
963 * symbol. gcc jump tables are anonymous data.
964 */
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -0500965 if (find_symbol_containing(file->rodata, table_offset))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500966 continue;
967
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -0500968 /* mov [rodata addr], %reg */
969 rodata_rela = find_rela_by_dest(file->rodata, table_offset);
970 if (rodata_rela)
971 return rodata_rela;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500972 }
973
974 return NULL;
975}
976
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100977
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500978static int add_func_switch_tables(struct objtool_file *file,
979 struct symbol *func)
980{
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100981 struct instruction *insn, *last = NULL, *prev_jump = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500982 struct rela *rela, *prev_rela = NULL;
983 int ret;
984
Josh Poimboeuf13810432018-05-09 22:39:15 -0500985 func_for_each_insn_all(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +0100986 if (!last)
987 last = insn;
988
989 /*
990 * Store back-pointers for unconditional forward jumps such
991 * that find_switch_table() can back-track using those and
992 * avoid some potentially confusing code.
993 */
994 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
995 insn->offset > last->offset &&
996 insn->jump_dest->offset > insn->offset &&
997 !insn->jump_dest->first_jump_src) {
998
999 insn->jump_dest->first_jump_src = insn;
1000 last = insn->jump_dest;
1001 }
1002
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001003 if (insn->type != INSN_JUMP_DYNAMIC)
1004 continue;
1005
1006 rela = find_switch_table(file, func, insn);
1007 if (!rela)
1008 continue;
1009
1010 /*
1011 * We found a switch table, but we don't know yet how big it
1012 * is. Don't add it until we reach the end of the function or
1013 * the beginning of another switch table in the same function.
1014 */
1015 if (prev_jump) {
Josh Poimboeuf13810432018-05-09 22:39:15 -05001016 ret = add_switch_table(file, prev_jump, prev_rela, rela);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001017 if (ret)
1018 return ret;
1019 }
1020
1021 prev_jump = insn;
1022 prev_rela = rela;
1023 }
1024
1025 if (prev_jump) {
Josh Poimboeuf13810432018-05-09 22:39:15 -05001026 ret = add_switch_table(file, prev_jump, prev_rela, NULL);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001027 if (ret)
1028 return ret;
1029 }
1030
1031 return 0;
1032}
1033
1034/*
1035 * For some switch statements, gcc generates a jump table in the .rodata
1036 * section which contains a list of addresses within the function to jump to.
1037 * This finds these jump tables and adds them to the insn->alts lists.
1038 */
1039static int add_switch_table_alts(struct objtool_file *file)
1040{
1041 struct section *sec;
1042 struct symbol *func;
1043 int ret;
1044
1045 if (!file->rodata || !file->rodata->rela)
1046 return 0;
1047
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001048 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001049 list_for_each_entry(func, &sec->symbol_list, list) {
1050 if (func->type != STT_FUNC)
1051 continue;
1052
1053 ret = add_func_switch_tables(file, func);
1054 if (ret)
1055 return ret;
1056 }
1057 }
1058
1059 return 0;
1060}
1061
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001062static int read_unwind_hints(struct objtool_file *file)
1063{
1064 struct section *sec, *relasec;
1065 struct rela *rela;
1066 struct unwind_hint *hint;
1067 struct instruction *insn;
1068 struct cfi_reg *cfa;
1069 int i;
1070
1071 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1072 if (!sec)
1073 return 0;
1074
1075 relasec = sec->rela;
1076 if (!relasec) {
1077 WARN("missing .rela.discard.unwind_hints section");
1078 return -1;
1079 }
1080
1081 if (sec->len % sizeof(struct unwind_hint)) {
1082 WARN("struct unwind_hint size mismatch");
1083 return -1;
1084 }
1085
1086 file->hints = true;
1087
1088 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1089 hint = (struct unwind_hint *)sec->data->d_buf + i;
1090
1091 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1092 if (!rela) {
1093 WARN("can't find rela for unwind_hints[%d]", i);
1094 return -1;
1095 }
1096
1097 insn = find_insn(file, rela->sym->sec, rela->addend);
1098 if (!insn) {
1099 WARN("can't find insn for unwind_hints[%d]", i);
1100 return -1;
1101 }
1102
1103 cfa = &insn->state.cfa;
1104
1105 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1106 insn->save = true;
1107 continue;
1108
1109 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1110 insn->restore = true;
1111 insn->hint = true;
1112 continue;
1113 }
1114
1115 insn->hint = true;
1116
1117 switch (hint->sp_reg) {
1118 case ORC_REG_UNDEFINED:
1119 cfa->base = CFI_UNDEFINED;
1120 break;
1121 case ORC_REG_SP:
1122 cfa->base = CFI_SP;
1123 break;
1124 case ORC_REG_BP:
1125 cfa->base = CFI_BP;
1126 break;
1127 case ORC_REG_SP_INDIRECT:
1128 cfa->base = CFI_SP_INDIRECT;
1129 break;
1130 case ORC_REG_R10:
1131 cfa->base = CFI_R10;
1132 break;
1133 case ORC_REG_R13:
1134 cfa->base = CFI_R13;
1135 break;
1136 case ORC_REG_DI:
1137 cfa->base = CFI_DI;
1138 break;
1139 case ORC_REG_DX:
1140 cfa->base = CFI_DX;
1141 break;
1142 default:
1143 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1144 insn->sec, insn->offset, hint->sp_reg);
1145 return -1;
1146 }
1147
1148 cfa->offset = hint->sp_offset;
1149 insn->state.type = hint->type;
1150 }
1151
1152 return 0;
1153}
1154
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001155static int read_retpoline_hints(struct objtool_file *file)
1156{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001157 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001158 struct instruction *insn;
1159 struct rela *rela;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001160
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001161 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001162 if (!sec)
1163 return 0;
1164
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001165 list_for_each_entry(rela, &sec->rela_list, list) {
1166 if (rela->sym->type != STT_SECTION) {
1167 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001168 return -1;
1169 }
1170
1171 insn = find_insn(file, rela->sym->sec, rela->addend);
1172 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001173 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001174 return -1;
1175 }
1176
1177 if (insn->type != INSN_JUMP_DYNAMIC &&
1178 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001179 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001180 insn->sec, insn->offset);
1181 return -1;
1182 }
1183
1184 insn->retpoline_safe = true;
1185 }
1186
1187 return 0;
1188}
1189
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001190static int decode_sections(struct objtool_file *file)
1191{
1192 int ret;
1193
1194 ret = decode_instructions(file);
1195 if (ret)
1196 return ret;
1197
1198 ret = add_dead_ends(file);
1199 if (ret)
1200 return ret;
1201
1202 add_ignores(file);
1203
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001204 ret = add_nospec_ignores(file);
1205 if (ret)
1206 return ret;
1207
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001208 ret = add_jump_destinations(file);
1209 if (ret)
1210 return ret;
1211
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001212 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001213 if (ret)
1214 return ret;
1215
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001216 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001217 if (ret)
1218 return ret;
1219
1220 ret = add_switch_table_alts(file);
1221 if (ret)
1222 return ret;
1223
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001224 ret = read_unwind_hints(file);
1225 if (ret)
1226 return ret;
1227
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001228 ret = read_retpoline_hints(file);
1229 if (ret)
1230 return ret;
1231
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001232 return 0;
1233}
1234
1235static bool is_fentry_call(struct instruction *insn)
1236{
1237 if (insn->type == INSN_CALL &&
1238 insn->call_dest->type == STT_NOTYPE &&
1239 !strcmp(insn->call_dest->name, "__fentry__"))
1240 return true;
1241
1242 return false;
1243}
1244
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001245static bool has_modified_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001246{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001247 int i;
1248
1249 if (state->cfa.base != initial_func_cfi.cfa.base ||
1250 state->cfa.offset != initial_func_cfi.cfa.offset ||
1251 state->stack_size != initial_func_cfi.cfa.offset ||
1252 state->drap)
1253 return true;
1254
1255 for (i = 0; i < CFI_NUM_REGS; i++)
1256 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1257 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1258 return true;
1259
1260 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001261}
1262
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001263static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001264{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001265 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1266 state->regs[CFI_BP].offset == -16)
1267 return true;
1268
1269 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1270 return true;
1271
1272 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001273}
1274
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001275static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1276{
1277 struct cfi_reg *cfa = &state->cfa;
1278 struct stack_op *op = &insn->stack_op;
1279
1280 if (cfa->base != CFI_SP)
1281 return 0;
1282
1283 /* push */
1284 if (op->dest.type == OP_DEST_PUSH)
1285 cfa->offset += 8;
1286
1287 /* pop */
1288 if (op->src.type == OP_SRC_POP)
1289 cfa->offset -= 8;
1290
1291 /* add immediate to sp */
1292 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1293 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1294 cfa->offset -= op->src.offset;
1295
1296 return 0;
1297}
1298
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001299static void save_reg(struct insn_state *state, unsigned char reg, int base,
1300 int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001301{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001302 if (arch_callee_saved_reg(reg) &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001303 state->regs[reg].base == CFI_UNDEFINED) {
1304 state->regs[reg].base = base;
1305 state->regs[reg].offset = offset;
1306 }
1307}
1308
1309static void restore_reg(struct insn_state *state, unsigned char reg)
1310{
1311 state->regs[reg].base = CFI_UNDEFINED;
1312 state->regs[reg].offset = 0;
1313}
1314
1315/*
1316 * A note about DRAP stack alignment:
1317 *
1318 * GCC has the concept of a DRAP register, which is used to help keep track of
1319 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1320 * register. The typical DRAP pattern is:
1321 *
1322 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1323 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1324 * 41 ff 72 f8 pushq -0x8(%r10)
1325 * 55 push %rbp
1326 * 48 89 e5 mov %rsp,%rbp
1327 * (more pushes)
1328 * 41 52 push %r10
1329 * ...
1330 * 41 5a pop %r10
1331 * (more pops)
1332 * 5d pop %rbp
1333 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1334 * c3 retq
1335 *
1336 * There are some variations in the epilogues, like:
1337 *
1338 * 5b pop %rbx
1339 * 41 5a pop %r10
1340 * 41 5c pop %r12
1341 * 41 5d pop %r13
1342 * 41 5e pop %r14
1343 * c9 leaveq
1344 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1345 * c3 retq
1346 *
1347 * and:
1348 *
1349 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1350 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1351 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1352 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1353 * c9 leaveq
1354 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1355 * c3 retq
1356 *
1357 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1358 * restored beforehand:
1359 *
1360 * 41 55 push %r13
1361 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1362 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1363 * ...
1364 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1365 * 41 5d pop %r13
1366 * c3 retq
1367 */
1368static int update_insn_state(struct instruction *insn, struct insn_state *state)
1369{
1370 struct stack_op *op = &insn->stack_op;
1371 struct cfi_reg *cfa = &state->cfa;
1372 struct cfi_reg *regs = state->regs;
1373
1374 /* stack operations don't make sense with an undefined CFA */
1375 if (cfa->base == CFI_UNDEFINED) {
1376 if (insn->func) {
1377 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1378 return -1;
1379 }
1380 return 0;
1381 }
1382
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001383 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1384 return update_insn_state_regs(insn, state);
1385
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001386 switch (op->dest.type) {
1387
1388 case OP_DEST_REG:
1389 switch (op->src.type) {
1390
1391 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001392 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1393 cfa->base == CFI_SP &&
1394 regs[CFI_BP].base == CFI_CFA &&
1395 regs[CFI_BP].offset == -cfa->offset) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001396
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001397 /* mov %rsp, %rbp */
1398 cfa->base = op->dest.reg;
1399 state->bp_scratch = false;
1400 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001401
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001402 else if (op->src.reg == CFI_SP &&
1403 op->dest.reg == CFI_BP && state->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001404
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001405 /* drap: mov %rsp, %rbp */
1406 regs[CFI_BP].base = CFI_BP;
1407 regs[CFI_BP].offset = -state->stack_size;
1408 state->bp_scratch = false;
1409 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001410
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001411 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1412
1413 /*
1414 * mov %rsp, %reg
1415 *
1416 * This is needed for the rare case where GCC
1417 * does:
1418 *
1419 * mov %rsp, %rax
1420 * ...
1421 * mov %rax, %rsp
1422 */
1423 state->vals[op->dest.reg].base = CFI_CFA;
1424 state->vals[op->dest.reg].offset = -state->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001425 }
1426
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001427 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1428 cfa->base == CFI_BP) {
1429
1430 /*
1431 * mov %rbp, %rsp
1432 *
1433 * Restore the original stack pointer (Clang).
1434 */
1435 state->stack_size = -state->regs[CFI_BP].offset;
1436 }
1437
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001438 else if (op->dest.reg == cfa->base) {
1439
1440 /* mov %reg, %rsp */
1441 if (cfa->base == CFI_SP &&
1442 state->vals[op->src.reg].base == CFI_CFA) {
1443
1444 /*
1445 * This is needed for the rare case
1446 * where GCC does something dumb like:
1447 *
1448 * lea 0x8(%rsp), %rcx
1449 * ...
1450 * mov %rcx, %rsp
1451 */
1452 cfa->offset = -state->vals[op->src.reg].offset;
1453 state->stack_size = cfa->offset;
1454
1455 } else {
1456 cfa->base = CFI_UNDEFINED;
1457 cfa->offset = 0;
1458 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001459 }
1460
1461 break;
1462
1463 case OP_SRC_ADD:
1464 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1465
1466 /* add imm, %rsp */
1467 state->stack_size -= op->src.offset;
1468 if (cfa->base == CFI_SP)
1469 cfa->offset -= op->src.offset;
1470 break;
1471 }
1472
1473 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1474
1475 /* lea disp(%rbp), %rsp */
1476 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1477 break;
1478 }
1479
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001480 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001481
1482 /* drap: lea disp(%rsp), %drap */
1483 state->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001484
1485 /*
1486 * lea disp(%rsp), %reg
1487 *
1488 * This is needed for the rare case where GCC
1489 * does something dumb like:
1490 *
1491 * lea 0x8(%rsp), %rcx
1492 * ...
1493 * mov %rcx, %rsp
1494 */
1495 state->vals[op->dest.reg].base = CFI_CFA;
1496 state->vals[op->dest.reg].offset = \
1497 -state->stack_size + op->src.offset;
1498
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001499 break;
1500 }
1501
1502 if (state->drap && op->dest.reg == CFI_SP &&
1503 op->src.reg == state->drap_reg) {
1504
1505 /* drap: lea disp(%drap), %rsp */
1506 cfa->base = CFI_SP;
1507 cfa->offset = state->stack_size = -op->src.offset;
1508 state->drap_reg = CFI_UNDEFINED;
1509 state->drap = false;
1510 break;
1511 }
1512
1513 if (op->dest.reg == state->cfa.base) {
1514 WARN_FUNC("unsupported stack register modification",
1515 insn->sec, insn->offset);
1516 return -1;
1517 }
1518
1519 break;
1520
1521 case OP_SRC_AND:
1522 if (op->dest.reg != CFI_SP ||
1523 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1524 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1525 WARN_FUNC("unsupported stack pointer realignment",
1526 insn->sec, insn->offset);
1527 return -1;
1528 }
1529
1530 if (state->drap_reg != CFI_UNDEFINED) {
1531 /* drap: and imm, %rsp */
1532 cfa->base = state->drap_reg;
1533 cfa->offset = state->stack_size = 0;
1534 state->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001535 }
1536
1537 /*
1538 * Older versions of GCC (4.8ish) realign the stack
1539 * without DRAP, with a frame pointer.
1540 */
1541
1542 break;
1543
1544 case OP_SRC_POP:
1545 if (!state->drap && op->dest.type == OP_DEST_REG &&
1546 op->dest.reg == cfa->base) {
1547
1548 /* pop %rbp */
1549 cfa->base = CFI_SP;
1550 }
1551
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001552 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1553 op->dest.type == OP_DEST_REG &&
1554 op->dest.reg == state->drap_reg &&
1555 state->drap_offset == -state->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001556
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001557 /* drap: pop %drap */
1558 cfa->base = state->drap_reg;
1559 cfa->offset = 0;
1560 state->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001561
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001562 } else if (regs[op->dest.reg].offset == -state->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001563
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001564 /* pop %reg */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001565 restore_reg(state, op->dest.reg);
1566 }
1567
1568 state->stack_size -= 8;
1569 if (cfa->base == CFI_SP)
1570 cfa->offset -= 8;
1571
1572 break;
1573
1574 case OP_SRC_REG_INDIRECT:
1575 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001576 op->src.offset == state->drap_offset) {
1577
1578 /* drap: mov disp(%rbp), %drap */
1579 cfa->base = state->drap_reg;
1580 cfa->offset = 0;
1581 state->drap_offset = -1;
1582 }
1583
1584 if (state->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001585 op->src.offset == regs[op->dest.reg].offset) {
1586
1587 /* drap: mov disp(%rbp), %reg */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001588 restore_reg(state, op->dest.reg);
1589
1590 } else if (op->src.reg == cfa->base &&
1591 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1592
1593 /* mov disp(%rbp), %reg */
1594 /* mov disp(%rsp), %reg */
1595 restore_reg(state, op->dest.reg);
1596 }
1597
1598 break;
1599
1600 default:
1601 WARN_FUNC("unknown stack-related instruction",
1602 insn->sec, insn->offset);
1603 return -1;
1604 }
1605
1606 break;
1607
1608 case OP_DEST_PUSH:
1609 state->stack_size += 8;
1610 if (cfa->base == CFI_SP)
1611 cfa->offset += 8;
1612
1613 if (op->src.type != OP_SRC_REG)
1614 break;
1615
1616 if (state->drap) {
1617 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1618
1619 /* drap: push %drap */
1620 cfa->base = CFI_BP_INDIRECT;
1621 cfa->offset = -state->stack_size;
1622
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001623 /* save drap so we know when to restore it */
1624 state->drap_offset = -state->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001625
1626 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1627
1628 /* drap: push %rbp */
1629 state->stack_size = 0;
1630
1631 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1632
1633 /* drap: push %reg */
1634 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1635 }
1636
1637 } else {
1638
1639 /* push %reg */
1640 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1641 }
1642
1643 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001644 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001645 cfa->base != CFI_BP)
1646 state->bp_scratch = true;
1647 break;
1648
1649 case OP_DEST_REG_INDIRECT:
1650
1651 if (state->drap) {
1652 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1653
1654 /* drap: mov %drap, disp(%rbp) */
1655 cfa->base = CFI_BP_INDIRECT;
1656 cfa->offset = op->dest.offset;
1657
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001658 /* save drap offset so we know when to restore it */
1659 state->drap_offset = op->dest.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001660 }
1661
1662 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1663
1664 /* drap: mov reg, disp(%rbp) */
1665 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1666 }
1667
1668 } else if (op->dest.reg == cfa->base) {
1669
1670 /* mov reg, disp(%rbp) */
1671 /* mov reg, disp(%rsp) */
1672 save_reg(state, op->src.reg, CFI_CFA,
1673 op->dest.offset - state->cfa.offset);
1674 }
1675
1676 break;
1677
1678 case OP_DEST_LEAVE:
1679 if ((!state->drap && cfa->base != CFI_BP) ||
1680 (state->drap && cfa->base != state->drap_reg)) {
1681 WARN_FUNC("leave instruction with modified stack frame",
1682 insn->sec, insn->offset);
1683 return -1;
1684 }
1685
1686 /* leave (mov %rbp, %rsp; pop %rbp) */
1687
1688 state->stack_size = -state->regs[CFI_BP].offset - 8;
1689 restore_reg(state, CFI_BP);
1690
1691 if (!state->drap) {
1692 cfa->base = CFI_SP;
1693 cfa->offset -= 8;
1694 }
1695
1696 break;
1697
1698 case OP_DEST_MEM:
1699 if (op->src.type != OP_SRC_POP) {
1700 WARN_FUNC("unknown stack-related memory operation",
1701 insn->sec, insn->offset);
1702 return -1;
1703 }
1704
1705 /* pop mem */
1706 state->stack_size -= 8;
1707 if (cfa->base == CFI_SP)
1708 cfa->offset -= 8;
1709
1710 break;
1711
1712 default:
1713 WARN_FUNC("unknown stack-related instruction",
1714 insn->sec, insn->offset);
1715 return -1;
1716 }
1717
1718 return 0;
1719}
1720
1721static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1722{
1723 struct insn_state *state1 = &insn->state, *state2 = state;
1724 int i;
1725
1726 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1727 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1728 insn->sec, insn->offset,
1729 state1->cfa.base, state1->cfa.offset,
1730 state2->cfa.base, state2->cfa.offset);
1731
1732 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1733 for (i = 0; i < CFI_NUM_REGS; i++) {
1734 if (!memcmp(&state1->regs[i], &state2->regs[i],
1735 sizeof(struct cfi_reg)))
1736 continue;
1737
1738 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1739 insn->sec, insn->offset,
1740 i, state1->regs[i].base, state1->regs[i].offset,
1741 i, state2->regs[i].base, state2->regs[i].offset);
1742 break;
1743 }
1744
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001745 } else if (state1->type != state2->type) {
1746 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1747 insn->sec, insn->offset, state1->type, state2->type);
1748
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001749 } else if (state1->drap != state2->drap ||
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001750 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1751 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1752 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001753 insn->sec, insn->offset,
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001754 state1->drap, state1->drap_reg, state1->drap_offset,
1755 state2->drap, state2->drap_reg, state2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001756
1757 } else
1758 return true;
1759
1760 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001761}
1762
1763/*
1764 * Follow the branch starting at the given instruction, and recursively follow
1765 * any other branches (jumps). Meanwhile, track the frame pointer state at
1766 * each instruction and validate all the rules described in
1767 * tools/objtool/Documentation/stack-validation.txt.
1768 */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001769static int validate_branch(struct objtool_file *file, struct instruction *first,
1770 struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001771{
1772 struct alternative *alt;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001773 struct instruction *insn, *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001774 struct section *sec;
1775 struct symbol *func = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001776 int ret;
1777
1778 insn = first;
1779 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001780
1781 if (insn->alt_group && list_empty(&insn->alts)) {
1782 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1783 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001784 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001785 }
1786
1787 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001788 next_insn = next_insn_same_sec(file, insn);
1789
Josh Poimboeuf13810432018-05-09 22:39:15 -05001790 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05001791 WARN("%s() falls through to next function %s()",
1792 func->name, insn->func->name);
1793 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001794 }
1795
Josh Poimboeuf13810432018-05-09 22:39:15 -05001796 func = insn->func ? insn->func->pfunc : NULL;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001797
Josh Poimboeuf48550222017-07-07 09:19:42 -05001798 if (func && insn->ignore) {
1799 WARN_FUNC("BUG: why am I validating an ignored function?",
1800 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001801 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001802 }
1803
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001804 if (insn->visited) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001805 if (!insn->hint && !insn_state_match(insn, &state))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001806 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001807
1808 return 0;
1809 }
1810
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001811 if (insn->hint) {
1812 if (insn->restore) {
1813 struct instruction *save_insn, *i;
1814
1815 i = insn;
1816 save_insn = NULL;
Josh Poimboeuf13810432018-05-09 22:39:15 -05001817 func_for_each_insn_continue_reverse(file, insn->func, i) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001818 if (i->save) {
1819 save_insn = i;
1820 break;
1821 }
1822 }
1823
1824 if (!save_insn) {
1825 WARN_FUNC("no corresponding CFI save for CFI restore",
1826 sec, insn->offset);
1827 return 1;
1828 }
1829
1830 if (!save_insn->visited) {
1831 /*
1832 * Oops, no state to copy yet.
1833 * Hopefully we can reach this
1834 * instruction from another branch
1835 * after the save insn has been
1836 * visited.
1837 */
1838 if (insn == first)
1839 return 0;
1840
1841 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1842 sec, insn->offset);
1843 return 1;
1844 }
1845
1846 insn->state = save_insn->state;
1847 }
1848
1849 state = insn->state;
1850
1851 } else
1852 insn->state = state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001853
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001854 insn->visited = true;
1855
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001856 if (!insn->ignore_alts) {
1857 list_for_each_entry(alt, &insn->alts, list) {
1858 ret = validate_branch(file, alt->insn, state);
1859 if (ret)
1860 return 1;
1861 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001862 }
1863
1864 switch (insn->type) {
1865
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001866 case INSN_RETURN:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001867 if (func && has_modified_stack_frame(&state)) {
1868 WARN_FUNC("return with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001869 sec, insn->offset);
1870 return 1;
1871 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001872
1873 if (state.bp_scratch) {
1874 WARN("%s uses BP as a scratch register",
1875 insn->func->name);
1876 return 1;
1877 }
1878
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001879 return 0;
1880
1881 case INSN_CALL:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001882 if (is_fentry_call(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001883 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001884
1885 ret = dead_end_function(file, insn->call_dest);
1886 if (ret == 1)
1887 return 0;
1888 if (ret == -1)
1889 return 1;
1890
1891 /* fallthrough */
1892 case INSN_CALL_DYNAMIC:
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05001893 if (!no_fp && func && !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001894 WARN_FUNC("call without frame pointer save/setup",
1895 sec, insn->offset);
1896 return 1;
1897 }
1898 break;
1899
1900 case INSN_JUMP_CONDITIONAL:
1901 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf48550222017-07-07 09:19:42 -05001902 if (insn->jump_dest &&
1903 (!func || !insn->jump_dest->func ||
Josh Poimboeuf13810432018-05-09 22:39:15 -05001904 insn->jump_dest->func->pfunc == func)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001905 ret = validate_branch(file, insn->jump_dest,
1906 state);
1907 if (ret)
1908 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001909
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001910 } else if (func && has_modified_stack_frame(&state)) {
1911 WARN_FUNC("sibling call from callable instruction with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001912 sec, insn->offset);
1913 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05001914 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001915
1916 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1917 return 0;
1918
1919 break;
1920
1921 case INSN_JUMP_DYNAMIC:
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001922 if (func && list_empty(&insn->alts) &&
1923 has_modified_stack_frame(&state)) {
1924 WARN_FUNC("sibling call from callable instruction with modified stack frame",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001925 sec, insn->offset);
1926 return 1;
1927 }
1928
1929 return 0;
1930
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001931 case INSN_CONTEXT_SWITCH:
1932 if (func && (!next_insn || !next_insn->hint)) {
1933 WARN_FUNC("unsupported instruction in callable function",
1934 sec, insn->offset);
1935 return 1;
1936 }
1937 return 0;
1938
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001939 case INSN_STACK:
1940 if (update_insn_state(insn, &state))
Josh Poimboeuf12b25722017-08-10 16:37:25 -05001941 return 1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001942
1943 break;
1944
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001945 default:
1946 break;
1947 }
1948
1949 if (insn->dead_end)
1950 return 0;
1951
Josh Poimboeuf00d96182017-09-18 21:43:30 -05001952 if (!next_insn) {
1953 if (state.cfa.base == CFI_UNDEFINED)
1954 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001955 WARN("%s: unexpected end of section", sec->name);
1956 return 1;
1957 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05001958
1959 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001960 }
1961
1962 return 0;
1963}
1964
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001965static int validate_unwind_hints(struct objtool_file *file)
1966{
1967 struct instruction *insn;
1968 int ret, warnings = 0;
1969 struct insn_state state;
1970
1971 if (!file->hints)
1972 return 0;
1973
1974 clear_insn_state(&state);
1975
1976 for_each_insn(file, insn) {
1977 if (insn->hint && !insn->visited) {
1978 ret = validate_branch(file, insn, state);
1979 warnings += ret;
1980 }
1981 }
1982
1983 return warnings;
1984}
1985
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001986static int validate_retpoline(struct objtool_file *file)
1987{
1988 struct instruction *insn;
1989 int warnings = 0;
1990
1991 for_each_insn(file, insn) {
1992 if (insn->type != INSN_JUMP_DYNAMIC &&
1993 insn->type != INSN_CALL_DYNAMIC)
1994 continue;
1995
1996 if (insn->retpoline_safe)
1997 continue;
1998
Peter Zijlstraca41b972018-01-31 10:18:28 +01001999 /*
2000 * .init.text code is ran before userspace and thus doesn't
2001 * strictly need retpolines, except for modules which are
2002 * loaded late, they very much do need retpoline in their
2003 * .init.text
2004 */
2005 if (!strcmp(insn->sec->name, ".init.text") && !module)
2006 continue;
2007
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002008 WARN_FUNC("indirect %s found in RETPOLINE build",
2009 insn->sec, insn->offset,
2010 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2011
2012 warnings++;
2013 }
2014
2015 return warnings;
2016}
2017
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002018static bool is_kasan_insn(struct instruction *insn)
2019{
2020 return (insn->type == INSN_CALL &&
2021 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2022}
2023
2024static bool is_ubsan_insn(struct instruction *insn)
2025{
2026 return (insn->type == INSN_CALL &&
2027 !strcmp(insn->call_dest->name,
2028 "__ubsan_handle_builtin_unreachable"));
2029}
2030
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002031static bool ignore_unreachable_insn(struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002032{
2033 int i;
2034
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002035 if (insn->ignore || insn->type == INSN_NOP)
2036 return true;
2037
2038 /*
2039 * Ignore any unused exceptions. This can happen when a whitelisted
2040 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002041 *
2042 * Also ignore alternative replacement instructions. This can happen
2043 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002044 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002045 if (!strcmp(insn->sec->name, ".fixup") ||
2046 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2047 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002048 return true;
2049
2050 /*
2051 * Check if this (or a subsequent) instruction is related to
2052 * CONFIG_UBSAN or CONFIG_KASAN.
2053 *
2054 * End the search at 5 instructions to avoid going into the weeds.
2055 */
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002056 if (!insn->func)
2057 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002058 for (i = 0; i < 5; i++) {
2059
2060 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2061 return true;
2062
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002063 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2064 if (insn->jump_dest &&
2065 insn->jump_dest->func == insn->func) {
2066 insn = insn->jump_dest;
2067 continue;
2068 }
2069
2070 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002071 }
2072
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002073 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002074 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002075
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002076 insn = list_next_entry(insn, list);
2077 }
2078
2079 return false;
2080}
2081
2082static int validate_functions(struct objtool_file *file)
2083{
2084 struct section *sec;
2085 struct symbol *func;
2086 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002087 struct insn_state state;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002088 int ret, warnings = 0;
2089
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002090 clear_insn_state(&state);
2091
2092 state.cfa = initial_func_cfi.cfa;
2093 memcpy(&state.regs, &initial_func_cfi.regs,
2094 CFI_NUM_REGS * sizeof(struct cfi_reg));
2095 state.stack_size = initial_func_cfi.cfa.offset;
2096
2097 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002098 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf13810432018-05-09 22:39:15 -05002099 if (func->type != STT_FUNC || func->pfunc != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002100 continue;
2101
2102 insn = find_insn(file, sec, func->offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002103 if (!insn || insn->ignore)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002104 continue;
2105
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002106 ret = validate_branch(file, insn, state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002107 warnings += ret;
2108 }
2109 }
2110
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002111 return warnings;
2112}
2113
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002114static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002115{
2116 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002117
2118 if (file->ignore_unreachables)
2119 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002120
2121 for_each_insn(file, insn) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002122 if (insn->visited || ignore_unreachable_insn(insn))
2123 continue;
2124
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002125 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2126 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002127 }
2128
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002129 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002130}
2131
2132static void cleanup(struct objtool_file *file)
2133{
2134 struct instruction *insn, *tmpinsn;
2135 struct alternative *alt, *tmpalt;
2136
2137 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2138 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2139 list_del(&alt->list);
2140 free(alt);
2141 }
2142 list_del(&insn->list);
2143 hash_del(&insn->hash);
2144 free(insn);
2145 }
2146 elf_close(file->elf);
2147}
2148
Peter Zijlstra43a45252018-01-16 17:16:32 +01002149int check(const char *_objname, bool orc)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002150{
2151 struct objtool_file file;
2152 int ret, warnings = 0;
2153
2154 objname = _objname;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002155
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002156 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002157 if (!file.elf)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002158 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002159
2160 INIT_LIST_HEAD(&file.insn_list);
2161 hash_init(file.insn_hash);
2162 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
2163 file.rodata = find_section_by_name(file.elf, ".rodata");
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002164 file.c_file = find_section_by_name(file.elf, ".comment");
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002165 file.ignore_unreachables = no_unreachable;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002166 file.hints = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002167
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002168 arch_initial_func_cfi_state(&initial_func_cfi);
2169
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002170 ret = decode_sections(&file);
2171 if (ret < 0)
2172 goto out;
2173 warnings += ret;
2174
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002175 if (list_empty(&file.insn_list))
2176 goto out;
2177
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002178 if (retpoline) {
2179 ret = validate_retpoline(&file);
2180 if (ret < 0)
2181 return ret;
2182 warnings += ret;
2183 }
2184
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002185 ret = validate_functions(&file);
2186 if (ret < 0)
2187 goto out;
2188 warnings += ret;
2189
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002190 ret = validate_unwind_hints(&file);
2191 if (ret < 0)
2192 goto out;
2193 warnings += ret;
2194
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002195 if (!warnings) {
2196 ret = validate_reachable_instructions(&file);
2197 if (ret < 0)
2198 goto out;
2199 warnings += ret;
2200 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002201
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002202 if (orc) {
2203 ret = create_orc(&file);
2204 if (ret < 0)
2205 goto out;
2206
2207 ret = create_orc_sections(&file);
2208 if (ret < 0)
2209 goto out;
2210
2211 ret = elf_write(file.elf);
2212 if (ret < 0)
2213 goto out;
2214 }
2215
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002216out:
2217 cleanup(&file);
2218
2219 /* ignore warnings for now until we get all the code cleaned up */
2220 if (ret || warnings)
2221 return 0;
2222 return 0;
2223}