blob: 2299eae155ace85210cd70c74f731d2835e8e7c9 [file] [log] [blame]
Josh Poimboeuf24ac7a42017-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
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +020021#include "builtin.h"
Josh Poimboeuf24ac7a42017-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>
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +020029#include <linux/kernel.h>
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -050030
31struct alternative {
32 struct list_head list;
33 struct instruction *insn;
34};
35
36const char *objname;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +020037struct cfi_state initial_func_cfi;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -050038
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +020039struct instruction *find_insn(struct objtool_file *file,
40 struct section *sec, unsigned long offset)
Josh Poimboeuf24ac7a42017-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
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +020056 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -050057 return NULL;
58
59 return next;
60}
61
Josh Poimboeuf1f7f88a2018-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 Poimboeuf24ac7a42017-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
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200103#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 Poimboeuf24ac7a42017-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 Poimboeuf24ac7a42017-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 Poimboeuf24ac7a42017-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",
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200164 "lbug_with_loc",
165 "fortify_panic",
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500166 };
167
168 if (func->bind == STB_WEAK)
169 return 0;
170
171 if (func->bind == STB_GLOBAL)
172 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
173 if (!strcmp(func->name, global_noreturns[i]))
174 return 1;
175
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500176 if (!func->len)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500177 return 0;
178
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500179 insn = find_insn(file, func->sec, func->offset);
180 if (!insn->func)
181 return 0;
182
183 func_for_each_insn_all(file, func, insn) {
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500184 empty = false;
185
186 if (insn->type == INSN_RETURN)
187 return 0;
188 }
189
190 if (empty)
191 return 0;
192
193 /*
194 * A function can have a sibling call instead of a return. In that
195 * case, the function's dead-end status depends on whether the target
196 * of the sibling call returns.
197 */
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500198 func_for_each_insn_all(file, func, insn) {
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500199 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
200 struct instruction *dest = insn->jump_dest;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500201
202 if (!dest)
203 /* sibling call to another file */
204 return 0;
205
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500206 if (dest->func && dest->func->pfunc != insn->func->pfunc) {
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500207
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500208 /* local sibling call */
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500209 if (recursion == 5) {
210 WARN_FUNC("infinite recursion (objtool bug!)",
211 dest->sec, dest->offset);
212 return -1;
213 }
214
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500215 return __dead_end_function(file, dest->func,
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500216 recursion + 1);
217 }
218 }
219
220 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
221 /* sibling call */
222 return 0;
223 }
224
225 return 1;
226}
227
228static int dead_end_function(struct objtool_file *file, struct symbol *func)
229{
230 return __dead_end_function(file, func, 0);
231}
232
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200233static void clear_insn_state(struct insn_state *state)
234{
235 int i;
236
237 memset(state, 0, sizeof(*state));
238 state->cfa.base = CFI_UNDEFINED;
239 for (i = 0; i < CFI_NUM_REGS; i++) {
240 state->regs[i].base = CFI_UNDEFINED;
241 state->vals[i].base = CFI_UNDEFINED;
242 }
243 state->drap_reg = CFI_UNDEFINED;
244 state->drap_offset = -1;
245}
246
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500247/*
248 * Call the arch-specific instruction decoder for all the instructions and add
249 * them to the global instruction list.
250 */
251static int decode_instructions(struct objtool_file *file)
252{
253 struct section *sec;
254 struct symbol *func;
255 unsigned long offset;
256 struct instruction *insn;
257 int ret;
258
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200259 for_each_sec(file, sec) {
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500260
261 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
262 continue;
263
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200264 if (strcmp(sec->name, ".altinstr_replacement") &&
265 strcmp(sec->name, ".altinstr_aux") &&
266 strncmp(sec->name, ".discard.", 9))
267 sec->text = true;
268
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500269 for (offset = 0; offset < sec->len; offset += insn->len) {
270 insn = malloc(sizeof(*insn));
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200271 if (!insn) {
272 WARN("malloc failed");
273 return -1;
274 }
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500275 memset(insn, 0, sizeof(*insn));
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500276 INIT_LIST_HEAD(&insn->alts);
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200277 clear_insn_state(&insn->state);
278
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500279 insn->sec = sec;
280 insn->offset = offset;
281
282 ret = arch_decode_instruction(file->elf, sec, offset,
283 sec->len - offset,
284 &insn->len, &insn->type,
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200285 &insn->immediate,
286 &insn->stack_op);
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500287 if (ret)
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200288 goto err;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500289
290 if (!insn->type || insn->type > INSN_LAST) {
291 WARN_FUNC("invalid instruction type %d",
292 insn->sec, insn->offset, insn->type);
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200293 ret = -1;
294 goto err;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500295 }
296
297 hash_add(file->insn_hash, &insn->hash, insn->offset);
298 list_add_tail(&insn->list, &file->insn_list);
299 }
300
301 list_for_each_entry(func, &sec->symbol_list, list) {
302 if (func->type != STT_FUNC)
303 continue;
304
305 if (!find_insn(file, sec, func->offset)) {
306 WARN("%s(): can't find starting instruction",
307 func->name);
308 return -1;
309 }
310
311 func_for_each_insn(file, func, insn)
312 if (!insn->func)
313 insn->func = func;
314 }
315 }
316
317 return 0;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200318
319err:
320 free(insn);
321 return ret;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500322}
323
324/*
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200325 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500326 */
327static int add_dead_ends(struct objtool_file *file)
328{
329 struct section *sec;
330 struct rela *rela;
331 struct instruction *insn;
332 bool found;
333
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200334 /*
335 * By default, "ud2" is a dead end unless otherwise annotated, because
336 * GCC 7 inserts it for certain divide-by-zero cases.
337 */
338 for_each_insn(file, insn)
339 if (insn->type == INSN_BUG)
340 insn->dead_end = true;
341
342 /*
343 * Check for manually annotated dead ends.
344 */
345 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500346 if (!sec)
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200347 goto reachable;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500348
349 list_for_each_entry(rela, &sec->rela_list, list) {
350 if (rela->sym->type != STT_SECTION) {
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200351 WARN("unexpected relocation symbol type in %s", sec->name);
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500352 return -1;
353 }
354 insn = find_insn(file, rela->sym->sec, rela->addend);
355 if (insn)
356 insn = list_prev_entry(insn, list);
357 else if (rela->addend == rela->sym->sec->len) {
358 found = false;
359 list_for_each_entry_reverse(insn, &file->insn_list, list) {
360 if (insn->sec == rela->sym->sec) {
361 found = true;
362 break;
363 }
364 }
365
366 if (!found) {
367 WARN("can't find unreachable insn at %s+0x%x",
368 rela->sym->sec->name, rela->addend);
369 return -1;
370 }
371 } else {
372 WARN("can't find unreachable insn at %s+0x%x",
373 rela->sym->sec->name, rela->addend);
374 return -1;
375 }
376
377 insn->dead_end = true;
378 }
379
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200380reachable:
381 /*
382 * These manually annotated reachable checks are needed for GCC 4.4,
383 * where the Linux unreachable() macro isn't supported. In that case
384 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
385 * not a dead end.
386 */
387 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
388 if (!sec)
389 return 0;
390
391 list_for_each_entry(rela, &sec->rela_list, list) {
392 if (rela->sym->type != STT_SECTION) {
393 WARN("unexpected relocation symbol type in %s", sec->name);
394 return -1;
395 }
396 insn = find_insn(file, rela->sym->sec, rela->addend);
397 if (insn)
398 insn = list_prev_entry(insn, list);
399 else if (rela->addend == rela->sym->sec->len) {
400 found = false;
401 list_for_each_entry_reverse(insn, &file->insn_list, list) {
402 if (insn->sec == rela->sym->sec) {
403 found = true;
404 break;
405 }
406 }
407
408 if (!found) {
409 WARN("can't find reachable insn at %s+0x%x",
410 rela->sym->sec->name, rela->addend);
411 return -1;
412 }
413 } else {
414 WARN("can't find reachable insn at %s+0x%x",
415 rela->sym->sec->name, rela->addend);
416 return -1;
417 }
418
419 insn->dead_end = false;
420 }
421
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500422 return 0;
423}
424
425/*
426 * Warnings shouldn't be reported for ignored functions.
427 */
428static void add_ignores(struct objtool_file *file)
429{
430 struct instruction *insn;
431 struct section *sec;
432 struct symbol *func;
433
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200434 for_each_sec(file, sec) {
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500435 list_for_each_entry(func, &sec->symbol_list, list) {
436 if (func->type != STT_FUNC)
437 continue;
438
439 if (!ignore_func(file, func))
440 continue;
441
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500442 func_for_each_insn_all(file, func, insn)
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200443 insn->ignore = true;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500444 }
445 }
446}
447
448/*
449 * FIXME: For now, just ignore any alternatives which add retpolines. This is
450 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
451 * But it at least allows objtool to understand the control flow *around* the
452 * retpoline.
453 */
454static int add_nospec_ignores(struct objtool_file *file)
455{
456 struct section *sec;
457 struct rela *rela;
458 struct instruction *insn;
459
460 sec = find_section_by_name(file->elf, ".rela.discard.nospec");
461 if (!sec)
462 return 0;
463
464 list_for_each_entry(rela, &sec->rela_list, list) {
465 if (rela->sym->type != STT_SECTION) {
466 WARN("unexpected relocation symbol type in %s", sec->name);
467 return -1;
468 }
469
470 insn = find_insn(file, rela->sym->sec, rela->addend);
471 if (!insn) {
472 WARN("bad .discard.nospec entry");
473 return -1;
474 }
475
476 insn->ignore_alts = true;
477 }
478
479 return 0;
480}
481
482/*
483 * Find the destination instructions for all jumps.
484 */
485static int add_jump_destinations(struct objtool_file *file)
486{
487 struct instruction *insn;
488 struct rela *rela;
489 struct section *dest_sec;
490 unsigned long dest_off;
491
492 for_each_insn(file, insn) {
493 if (insn->type != INSN_JUMP_CONDITIONAL &&
494 insn->type != INSN_JUMP_UNCONDITIONAL)
495 continue;
496
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200497 if (insn->ignore)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500498 continue;
499
500 rela = find_rela_by_dest_range(insn->sec, insn->offset,
501 insn->len);
502 if (!rela) {
503 dest_sec = insn->sec;
504 dest_off = insn->offset + insn->len + insn->immediate;
505 } else if (rela->sym->type == STT_SECTION) {
506 dest_sec = rela->sym->sec;
507 dest_off = rela->addend + 4;
508 } else if (rela->sym->sec->idx) {
509 dest_sec = rela->sym->sec;
510 dest_off = rela->sym->sym.st_value + rela->addend + 4;
511 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
512 /*
513 * Retpoline jumps are really dynamic jumps in
514 * disguise, so convert them accordingly.
515 */
516 insn->type = INSN_JUMP_DYNAMIC;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200517 insn->retpoline_safe = true;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500518 continue;
519 } else {
520 /* sibling call */
521 insn->jump_dest = 0;
522 continue;
523 }
524
525 insn->jump_dest = find_insn(file, dest_sec, dest_off);
526 if (!insn->jump_dest) {
527
528 /*
529 * This is a special case where an alt instruction
530 * jumps past the end of the section. These are
531 * handled later in handle_group_alt().
532 */
533 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
534 continue;
535
536 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
537 insn->sec, insn->offset, dest_sec->name,
538 dest_off);
539 return -1;
540 }
541 }
542
543 return 0;
544}
545
546/*
547 * Find the destination instructions for all calls.
548 */
549static int add_call_destinations(struct objtool_file *file)
550{
551 struct instruction *insn;
552 unsigned long dest_off;
553 struct rela *rela;
554
555 for_each_insn(file, insn) {
556 if (insn->type != INSN_CALL)
557 continue;
558
559 rela = find_rela_by_dest_range(insn->sec, insn->offset,
560 insn->len);
561 if (!rela) {
562 dest_off = insn->offset + insn->len + insn->immediate;
563 insn->call_dest = find_symbol_by_offset(insn->sec,
564 dest_off);
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200565
566 if (!insn->call_dest && !insn->ignore) {
567 WARN_FUNC("unsupported intra-function call",
568 insn->sec, insn->offset);
569 if (retpoline)
570 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500571 return -1;
572 }
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200573
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500574 } else if (rela->sym->type == STT_SECTION) {
575 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
576 rela->addend+4);
577 if (!insn->call_dest ||
578 insn->call_dest->type != STT_FUNC) {
579 WARN_FUNC("can't find call dest symbol at %s+0x%x",
580 insn->sec, insn->offset,
581 rela->sym->sec->name,
582 rela->addend + 4);
583 return -1;
584 }
585 } else
586 insn->call_dest = rela->sym;
587 }
588
589 return 0;
590}
591
592/*
593 * The .alternatives section requires some extra special care, over and above
594 * what other special sections require:
595 *
596 * 1. Because alternatives are patched in-place, we need to insert a fake jump
597 * instruction at the end so that validate_branch() skips all the original
598 * replaced instructions when validating the new instruction path.
599 *
600 * 2. An added wrinkle is that the new instruction length might be zero. In
601 * that case the old instructions are replaced with noops. We simulate that
602 * by creating a fake jump as the only new instruction.
603 *
604 * 3. In some cases, the alternative section includes an instruction which
605 * conditionally jumps to the _end_ of the entry. We have to modify these
606 * jumps' destinations to point back to .text rather than the end of the
607 * entry in .altinstr_replacement.
608 *
609 * 4. It has been requested that we don't validate the !POPCNT feature path
610 * which is a "very very small percentage of machines".
611 */
612static int handle_group_alt(struct objtool_file *file,
613 struct special_alt *special_alt,
614 struct instruction *orig_insn,
615 struct instruction **new_insn)
616{
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200617 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500618 unsigned long dest_off;
619
620 last_orig_insn = NULL;
621 insn = orig_insn;
622 sec_for_each_insn_from(file, insn) {
623 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
624 break;
625
626 if (special_alt->skip_orig)
627 insn->type = INSN_NOP;
628
629 insn->alt_group = true;
630 last_orig_insn = insn;
631 }
632
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200633 if (next_insn_same_sec(file, last_orig_insn)) {
634 fake_jump = malloc(sizeof(*fake_jump));
635 if (!fake_jump) {
636 WARN("malloc failed");
637 return -1;
638 }
639 memset(fake_jump, 0, sizeof(*fake_jump));
640 INIT_LIST_HEAD(&fake_jump->alts);
641 clear_insn_state(&fake_jump->state);
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500642
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200643 fake_jump->sec = special_alt->new_sec;
644 fake_jump->offset = -1;
645 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
646 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
647 fake_jump->ignore = true;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500648 }
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500649
650 if (!special_alt->new_len) {
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200651 if (!fake_jump) {
652 WARN("%s: empty alternative at end of section",
653 special_alt->orig_sec->name);
654 return -1;
655 }
656
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500657 *new_insn = fake_jump;
658 return 0;
659 }
660
661 last_new_insn = NULL;
662 insn = *new_insn;
663 sec_for_each_insn_from(file, insn) {
664 if (insn->offset >= special_alt->new_off + special_alt->new_len)
665 break;
666
667 last_new_insn = insn;
668
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200669 insn->ignore = orig_insn->ignore_alts;
670
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500671 if (insn->type != INSN_JUMP_CONDITIONAL &&
672 insn->type != INSN_JUMP_UNCONDITIONAL)
673 continue;
674
675 if (!insn->immediate)
676 continue;
677
678 dest_off = insn->offset + insn->len + insn->immediate;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200679 if (dest_off == special_alt->new_off + special_alt->new_len) {
680 if (!fake_jump) {
681 WARN("%s: alternative jump to end of section",
682 special_alt->orig_sec->name);
683 return -1;
684 }
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500685 insn->jump_dest = fake_jump;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200686 }
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500687
688 if (!insn->jump_dest) {
689 WARN_FUNC("can't find alternative jump destination",
690 insn->sec, insn->offset);
691 return -1;
692 }
693 }
694
695 if (!last_new_insn) {
696 WARN_FUNC("can't find last new alternative instruction",
697 special_alt->new_sec, special_alt->new_off);
698 return -1;
699 }
700
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200701 if (fake_jump)
702 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500703
704 return 0;
705}
706
707/*
708 * A jump table entry can either convert a nop to a jump or a jump to a nop.
709 * If the original instruction is a jump, make the alt entry an effective nop
710 * by just skipping the original instruction.
711 */
712static int handle_jump_alt(struct objtool_file *file,
713 struct special_alt *special_alt,
714 struct instruction *orig_insn,
715 struct instruction **new_insn)
716{
717 if (orig_insn->type == INSN_NOP)
718 return 0;
719
720 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
721 WARN_FUNC("unsupported instruction at jump label",
722 orig_insn->sec, orig_insn->offset);
723 return -1;
724 }
725
726 *new_insn = list_next_entry(orig_insn, list);
727 return 0;
728}
729
730/*
731 * Read all the special sections which have alternate instructions which can be
732 * patched in or redirected to at runtime. Each instruction having alternate
733 * instruction(s) has them added to its insn->alts list, which will be
734 * traversed in validate_branch().
735 */
736static int add_special_section_alts(struct objtool_file *file)
737{
738 struct list_head special_alts;
739 struct instruction *orig_insn, *new_insn;
740 struct special_alt *special_alt, *tmp;
741 struct alternative *alt;
742 int ret;
743
744 ret = special_get_alts(file->elf, &special_alts);
745 if (ret)
746 return ret;
747
748 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200749
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500750 orig_insn = find_insn(file, special_alt->orig_sec,
751 special_alt->orig_off);
752 if (!orig_insn) {
753 WARN_FUNC("special: can't find orig instruction",
754 special_alt->orig_sec, special_alt->orig_off);
755 ret = -1;
756 goto out;
757 }
758
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500759 new_insn = NULL;
760 if (!special_alt->group || special_alt->new_len) {
761 new_insn = find_insn(file, special_alt->new_sec,
762 special_alt->new_off);
763 if (!new_insn) {
764 WARN_FUNC("special: can't find new instruction",
765 special_alt->new_sec,
766 special_alt->new_off);
767 ret = -1;
768 goto out;
769 }
770 }
771
772 if (special_alt->group) {
773 ret = handle_group_alt(file, special_alt, orig_insn,
774 &new_insn);
775 if (ret)
776 goto out;
777 } else if (special_alt->jump_or_nop) {
778 ret = handle_jump_alt(file, special_alt, orig_insn,
779 &new_insn);
780 if (ret)
781 goto out;
782 }
783
784 alt = malloc(sizeof(*alt));
785 if (!alt) {
786 WARN("malloc failed");
787 ret = -1;
788 goto out;
789 }
790
791 alt->insn = new_insn;
792 list_add_tail(&alt->list, &orig_insn->alts);
793
794 list_del(&special_alt->list);
795 free(special_alt);
796 }
797
798out:
799 return ret;
800}
801
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500802static int add_switch_table(struct objtool_file *file, struct instruction *insn,
803 struct rela *table, struct rela *next_table)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500804{
805 struct rela *rela = table;
806 struct instruction *alt_insn;
807 struct alternative *alt;
Josh Poimboeuf7cd91852018-05-10 17:48:49 -0500808 struct symbol *pfunc = insn->func->pfunc;
809 unsigned int prev_offset = 0;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500810
811 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
812 if (rela == next_table)
813 break;
814
Josh Poimboeuf7cd91852018-05-10 17:48:49 -0500815 /* Make sure the switch table entries are consecutive: */
816 if (prev_offset && rela->offset != prev_offset + 8)
817 break;
818
819 /* Detect function pointers from contiguous objects: */
820 if (rela->sym->sec == pfunc->sec &&
821 rela->addend == pfunc->offset)
822 break;
823
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500824 alt_insn = find_insn(file, rela->sym->sec, rela->addend);
825 if (!alt_insn)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500826 break;
827
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500828 /* Make sure the jmp dest is in the function or subfunction: */
Josh Poimboeuf7cd91852018-05-10 17:48:49 -0500829 if (alt_insn->func->pfunc != pfunc)
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500830 break;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500831
832 alt = malloc(sizeof(*alt));
833 if (!alt) {
834 WARN("malloc failed");
835 return -1;
836 }
837
838 alt->insn = alt_insn;
839 list_add_tail(&alt->list, &insn->alts);
Josh Poimboeuf7cd91852018-05-10 17:48:49 -0500840 prev_offset = rela->offset;
841 }
842
843 if (!prev_offset) {
844 WARN_FUNC("can't find switch jump table",
845 insn->sec, insn->offset);
846 return -1;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500847 }
848
849 return 0;
850}
851
852/*
853 * find_switch_table() - Given a dynamic jump, find the switch jump table in
854 * .rodata associated with it.
855 *
856 * There are 3 basic patterns:
857 *
858 * 1. jmpq *[rodata addr](,%reg,8)
859 *
860 * This is the most common case by far. It jumps to an address in a simple
861 * jump table which is stored in .rodata.
862 *
863 * 2. jmpq *[rodata addr](%rip)
864 *
865 * This is caused by a rare GCC quirk, currently only seen in three driver
866 * functions in the kernel, only with certain obscure non-distro configs.
867 *
868 * As part of an optimization, GCC makes a copy of an existing switch jump
869 * table, modifies it, and then hard-codes the jump (albeit with an indirect
870 * jump) to use a single entry in the table. The rest of the jump table and
871 * some of its jump targets remain as dead code.
872 *
873 * In such a case we can just crudely ignore all unreachable instruction
874 * warnings for the entire object file. Ideally we would just ignore them
875 * for the function, but that would require redesigning the code quite a
876 * bit. And honestly that's just not worth doing: unreachable instruction
877 * warnings are of questionable value anyway, and this is such a rare issue.
878 *
879 * 3. mov [rodata addr],%reg1
880 * ... some instructions ...
881 * jmpq *(%reg1,%reg2,8)
882 *
883 * This is a fairly uncommon pattern which is new for GCC 6. As of this
884 * writing, there are 11 occurrences of it in the allmodconfig kernel.
885 *
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200886 * As of GCC 7 there are quite a few more of these and the 'in between' code
887 * is significant. Esp. with KASAN enabled some of the code between the mov
888 * and jmpq uses .rodata itself, which can confuse things.
889 *
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500890 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
891 * ensure the same register is used in the mov and jump instructions.
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200892 *
893 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500894 */
895static struct rela *find_switch_table(struct objtool_file *file,
896 struct symbol *func,
897 struct instruction *insn)
898{
899 struct rela *text_rela, *rodata_rela;
900 struct instruction *orig_insn = insn;
Josh Poimboeuf48dc5372018-05-14 08:53:24 -0500901 unsigned long table_offset;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500902
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200903 /*
904 * Backward search using the @first_jump_src links, these help avoid
905 * much of the 'in between' code. Which avoids us getting confused by
906 * it.
907 */
Josh Poimboeuf603a2cd2018-05-18 15:10:34 -0500908 for (;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200909 &insn->list != &file->insn_list &&
910 insn->sec == func->sec &&
911 insn->offset >= func->offset;
912
913 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
914
Josh Poimboeuf603a2cd2018-05-18 15:10:34 -0500915 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500916 break;
917
918 /* allow small jumps within the range */
919 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
920 insn->jump_dest &&
921 (insn->jump_dest->offset <= insn->offset ||
922 insn->jump_dest->offset > orig_insn->offset))
923 break;
924
925 /* look for a relocation which references .rodata */
926 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
927 insn->len);
928 if (!text_rela || text_rela->sym != file->rodata->sym)
929 continue;
930
Josh Poimboeuf48dc5372018-05-14 08:53:24 -0500931 table_offset = text_rela->addend;
932 if (text_rela->type == R_X86_64_PC32)
933 table_offset += 4;
934
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500935 /*
936 * Make sure the .rodata address isn't associated with a
937 * symbol. gcc jump tables are anonymous data.
938 */
Josh Poimboeuf48dc5372018-05-14 08:53:24 -0500939 if (find_symbol_containing(file->rodata, table_offset))
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500940 continue;
941
Josh Poimboeuf48dc5372018-05-14 08:53:24 -0500942 rodata_rela = find_rela_by_dest(file->rodata, table_offset);
Josh Poimboeuf603a2cd2018-05-18 15:10:34 -0500943 if (rodata_rela) {
944 /*
945 * Use of RIP-relative switch jumps is quite rare, and
946 * indicates a rare GCC quirk/bug which can leave dead
947 * code behind.
948 */
949 if (text_rela->type == R_X86_64_PC32)
950 file->ignore_unreachables = true;
951
Josh Poimboeuf48dc5372018-05-14 08:53:24 -0500952 return rodata_rela;
Josh Poimboeuf603a2cd2018-05-18 15:10:34 -0500953 }
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500954 }
955
956 return NULL;
957}
958
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200959
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500960static int add_func_switch_tables(struct objtool_file *file,
961 struct symbol *func)
962{
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200963 struct instruction *insn, *last = NULL, *prev_jump = NULL;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500964 struct rela *rela, *prev_rela = NULL;
965 int ret;
966
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500967 func_for_each_insn_all(file, func, insn) {
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +0200968 if (!last)
969 last = insn;
970
971 /*
972 * Store back-pointers for unconditional forward jumps such
973 * that find_switch_table() can back-track using those and
974 * avoid some potentially confusing code.
975 */
976 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
977 insn->offset > last->offset &&
978 insn->jump_dest->offset > insn->offset &&
979 !insn->jump_dest->first_jump_src) {
980
981 insn->jump_dest->first_jump_src = insn;
982 last = insn->jump_dest;
983 }
984
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500985 if (insn->type != INSN_JUMP_DYNAMIC)
986 continue;
987
988 rela = find_switch_table(file, func, insn);
989 if (!rela)
990 continue;
991
992 /*
993 * We found a switch table, but we don't know yet how big it
994 * is. Don't add it until we reach the end of the function or
995 * the beginning of another switch table in the same function.
996 */
997 if (prev_jump) {
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -0500998 ret = add_switch_table(file, prev_jump, prev_rela, rela);
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -0500999 if (ret)
1000 return ret;
1001 }
1002
1003 prev_jump = insn;
1004 prev_rela = rela;
1005 }
1006
1007 if (prev_jump) {
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -05001008 ret = add_switch_table(file, prev_jump, prev_rela, NULL);
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001009 if (ret)
1010 return ret;
1011 }
1012
1013 return 0;
1014}
1015
1016/*
1017 * For some switch statements, gcc generates a jump table in the .rodata
1018 * section which contains a list of addresses within the function to jump to.
1019 * This finds these jump tables and adds them to the insn->alts lists.
1020 */
1021static int add_switch_table_alts(struct objtool_file *file)
1022{
1023 struct section *sec;
1024 struct symbol *func;
1025 int ret;
1026
1027 if (!file->rodata || !file->rodata->rela)
1028 return 0;
1029
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001030 for_each_sec(file, sec) {
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001031 list_for_each_entry(func, &sec->symbol_list, list) {
1032 if (func->type != STT_FUNC)
1033 continue;
1034
1035 ret = add_func_switch_tables(file, func);
1036 if (ret)
1037 return ret;
1038 }
1039 }
1040
1041 return 0;
1042}
1043
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001044static int read_unwind_hints(struct objtool_file *file)
1045{
1046 struct section *sec, *relasec;
1047 struct rela *rela;
1048 struct unwind_hint *hint;
1049 struct instruction *insn;
1050 struct cfi_reg *cfa;
1051 int i;
1052
1053 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1054 if (!sec)
1055 return 0;
1056
1057 relasec = sec->rela;
1058 if (!relasec) {
1059 WARN("missing .rela.discard.unwind_hints section");
1060 return -1;
1061 }
1062
1063 if (sec->len % sizeof(struct unwind_hint)) {
1064 WARN("struct unwind_hint size mismatch");
1065 return -1;
1066 }
1067
1068 file->hints = true;
1069
1070 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1071 hint = (struct unwind_hint *)sec->data->d_buf + i;
1072
1073 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1074 if (!rela) {
1075 WARN("can't find rela for unwind_hints[%d]", i);
1076 return -1;
1077 }
1078
1079 insn = find_insn(file, rela->sym->sec, rela->addend);
1080 if (!insn) {
1081 WARN("can't find insn for unwind_hints[%d]", i);
1082 return -1;
1083 }
1084
1085 cfa = &insn->state.cfa;
1086
1087 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1088 insn->save = true;
1089 continue;
1090
1091 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1092 insn->restore = true;
1093 insn->hint = true;
1094 continue;
1095 }
1096
1097 insn->hint = true;
1098
1099 switch (hint->sp_reg) {
1100 case ORC_REG_UNDEFINED:
1101 cfa->base = CFI_UNDEFINED;
1102 break;
1103 case ORC_REG_SP:
1104 cfa->base = CFI_SP;
1105 break;
1106 case ORC_REG_BP:
1107 cfa->base = CFI_BP;
1108 break;
1109 case ORC_REG_SP_INDIRECT:
1110 cfa->base = CFI_SP_INDIRECT;
1111 break;
1112 case ORC_REG_R10:
1113 cfa->base = CFI_R10;
1114 break;
1115 case ORC_REG_R13:
1116 cfa->base = CFI_R13;
1117 break;
1118 case ORC_REG_DI:
1119 cfa->base = CFI_DI;
1120 break;
1121 case ORC_REG_DX:
1122 cfa->base = CFI_DX;
1123 break;
1124 default:
1125 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1126 insn->sec, insn->offset, hint->sp_reg);
1127 return -1;
1128 }
1129
1130 cfa->offset = hint->sp_offset;
1131 insn->state.type = hint->type;
1132 }
1133
1134 return 0;
1135}
1136
1137static int read_retpoline_hints(struct objtool_file *file)
1138{
1139 struct section *sec;
1140 struct instruction *insn;
1141 struct rela *rela;
1142
1143 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1144 if (!sec)
1145 return 0;
1146
1147 list_for_each_entry(rela, &sec->rela_list, list) {
1148 if (rela->sym->type != STT_SECTION) {
1149 WARN("unexpected relocation symbol type in %s", sec->name);
1150 return -1;
1151 }
1152
1153 insn = find_insn(file, rela->sym->sec, rela->addend);
1154 if (!insn) {
1155 WARN("bad .discard.retpoline_safe entry");
1156 return -1;
1157 }
1158
1159 if (insn->type != INSN_JUMP_DYNAMIC &&
1160 insn->type != INSN_CALL_DYNAMIC) {
1161 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1162 insn->sec, insn->offset);
1163 return -1;
1164 }
1165
1166 insn->retpoline_safe = true;
1167 }
1168
1169 return 0;
1170}
1171
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001172static int decode_sections(struct objtool_file *file)
1173{
1174 int ret;
1175
1176 ret = decode_instructions(file);
1177 if (ret)
1178 return ret;
1179
1180 ret = add_dead_ends(file);
1181 if (ret)
1182 return ret;
1183
1184 add_ignores(file);
1185
1186 ret = add_nospec_ignores(file);
1187 if (ret)
1188 return ret;
1189
1190 ret = add_jump_destinations(file);
1191 if (ret)
1192 return ret;
1193
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001194 ret = add_special_section_alts(file);
1195 if (ret)
1196 return ret;
1197
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001198 ret = add_call_destinations(file);
1199 if (ret)
1200 return ret;
1201
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001202 ret = add_switch_table_alts(file);
1203 if (ret)
1204 return ret;
1205
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001206 ret = read_unwind_hints(file);
1207 if (ret)
1208 return ret;
1209
1210 ret = read_retpoline_hints(file);
1211 if (ret)
1212 return ret;
1213
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001214 return 0;
1215}
1216
1217static bool is_fentry_call(struct instruction *insn)
1218{
1219 if (insn->type == INSN_CALL &&
1220 insn->call_dest->type == STT_NOTYPE &&
1221 !strcmp(insn->call_dest->name, "__fentry__"))
1222 return true;
1223
1224 return false;
1225}
1226
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001227static bool has_modified_stack_frame(struct insn_state *state)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001228{
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001229 int i;
1230
1231 if (state->cfa.base != initial_func_cfi.cfa.base ||
1232 state->cfa.offset != initial_func_cfi.cfa.offset ||
1233 state->stack_size != initial_func_cfi.cfa.offset ||
1234 state->drap)
1235 return true;
1236
1237 for (i = 0; i < CFI_NUM_REGS; i++)
1238 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1239 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1240 return true;
1241
1242 return false;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001243}
1244
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001245static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001246{
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001247 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1248 state->regs[CFI_BP].offset == -16)
1249 return true;
1250
1251 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1252 return true;
1253
1254 return false;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001255}
1256
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001257static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001258{
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001259 struct cfi_reg *cfa = &state->cfa;
1260 struct stack_op *op = &insn->stack_op;
1261
1262 if (cfa->base != CFI_SP)
1263 return 0;
1264
1265 /* push */
1266 if (op->dest.type == OP_DEST_PUSH)
1267 cfa->offset += 8;
1268
1269 /* pop */
1270 if (op->src.type == OP_SRC_POP)
1271 cfa->offset -= 8;
1272
1273 /* add immediate to sp */
1274 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1275 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1276 cfa->offset -= op->src.offset;
1277
1278 return 0;
1279}
1280
1281static void save_reg(struct insn_state *state, unsigned char reg, int base,
1282 int offset)
1283{
1284 if (arch_callee_saved_reg(reg) &&
1285 state->regs[reg].base == CFI_UNDEFINED) {
1286 state->regs[reg].base = base;
1287 state->regs[reg].offset = offset;
1288 }
1289}
1290
1291static void restore_reg(struct insn_state *state, unsigned char reg)
1292{
1293 state->regs[reg].base = CFI_UNDEFINED;
1294 state->regs[reg].offset = 0;
1295}
1296
1297/*
1298 * A note about DRAP stack alignment:
1299 *
1300 * GCC has the concept of a DRAP register, which is used to help keep track of
1301 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1302 * register. The typical DRAP pattern is:
1303 *
1304 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1305 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1306 * 41 ff 72 f8 pushq -0x8(%r10)
1307 * 55 push %rbp
1308 * 48 89 e5 mov %rsp,%rbp
1309 * (more pushes)
1310 * 41 52 push %r10
1311 * ...
1312 * 41 5a pop %r10
1313 * (more pops)
1314 * 5d pop %rbp
1315 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1316 * c3 retq
1317 *
1318 * There are some variations in the epilogues, like:
1319 *
1320 * 5b pop %rbx
1321 * 41 5a pop %r10
1322 * 41 5c pop %r12
1323 * 41 5d pop %r13
1324 * 41 5e pop %r14
1325 * c9 leaveq
1326 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1327 * c3 retq
1328 *
1329 * and:
1330 *
1331 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1332 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1333 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1334 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1335 * c9 leaveq
1336 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1337 * c3 retq
1338 *
1339 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1340 * restored beforehand:
1341 *
1342 * 41 55 push %r13
1343 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1344 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1345 * ...
1346 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1347 * 41 5d pop %r13
1348 * c3 retq
1349 */
1350static int update_insn_state(struct instruction *insn, struct insn_state *state)
1351{
1352 struct stack_op *op = &insn->stack_op;
1353 struct cfi_reg *cfa = &state->cfa;
1354 struct cfi_reg *regs = state->regs;
1355
1356 /* stack operations don't make sense with an undefined CFA */
1357 if (cfa->base == CFI_UNDEFINED) {
1358 if (insn->func) {
1359 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1360 return -1;
1361 }
1362 return 0;
1363 }
1364
1365 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1366 return update_insn_state_regs(insn, state);
1367
1368 switch (op->dest.type) {
1369
1370 case OP_DEST_REG:
1371 switch (op->src.type) {
1372
1373 case OP_SRC_REG:
1374 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1375 cfa->base == CFI_SP &&
1376 regs[CFI_BP].base == CFI_CFA &&
1377 regs[CFI_BP].offset == -cfa->offset) {
1378
1379 /* mov %rsp, %rbp */
1380 cfa->base = op->dest.reg;
1381 state->bp_scratch = false;
1382 }
1383
1384 else if (op->src.reg == CFI_SP &&
1385 op->dest.reg == CFI_BP && state->drap) {
1386
1387 /* drap: mov %rsp, %rbp */
1388 regs[CFI_BP].base = CFI_BP;
1389 regs[CFI_BP].offset = -state->stack_size;
1390 state->bp_scratch = false;
1391 }
1392
1393 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1394
1395 /*
1396 * mov %rsp, %reg
1397 *
1398 * This is needed for the rare case where GCC
1399 * does:
1400 *
1401 * mov %rsp, %rax
1402 * ...
1403 * mov %rax, %rsp
1404 */
1405 state->vals[op->dest.reg].base = CFI_CFA;
1406 state->vals[op->dest.reg].offset = -state->stack_size;
1407 }
1408
1409 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1410 cfa->base == CFI_BP) {
1411
1412 /*
1413 * mov %rbp, %rsp
1414 *
1415 * Restore the original stack pointer (Clang).
1416 */
1417 state->stack_size = -state->regs[CFI_BP].offset;
1418 }
1419
1420 else if (op->dest.reg == cfa->base) {
1421
1422 /* mov %reg, %rsp */
1423 if (cfa->base == CFI_SP &&
1424 state->vals[op->src.reg].base == CFI_CFA) {
1425
1426 /*
1427 * This is needed for the rare case
1428 * where GCC does something dumb like:
1429 *
1430 * lea 0x8(%rsp), %rcx
1431 * ...
1432 * mov %rcx, %rsp
1433 */
1434 cfa->offset = -state->vals[op->src.reg].offset;
1435 state->stack_size = cfa->offset;
1436
1437 } else {
1438 cfa->base = CFI_UNDEFINED;
1439 cfa->offset = 0;
1440 }
1441 }
1442
1443 break;
1444
1445 case OP_SRC_ADD:
1446 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1447
1448 /* add imm, %rsp */
1449 state->stack_size -= op->src.offset;
1450 if (cfa->base == CFI_SP)
1451 cfa->offset -= op->src.offset;
1452 break;
1453 }
1454
1455 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1456
1457 /* lea disp(%rbp), %rsp */
1458 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1459 break;
1460 }
1461
1462 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1463
1464 /* drap: lea disp(%rsp), %drap */
1465 state->drap_reg = op->dest.reg;
1466
1467 /*
1468 * lea disp(%rsp), %reg
1469 *
1470 * This is needed for the rare case where GCC
1471 * does something dumb like:
1472 *
1473 * lea 0x8(%rsp), %rcx
1474 * ...
1475 * mov %rcx, %rsp
1476 */
1477 state->vals[op->dest.reg].base = CFI_CFA;
1478 state->vals[op->dest.reg].offset = \
1479 -state->stack_size + op->src.offset;
1480
1481 break;
1482 }
1483
1484 if (state->drap && op->dest.reg == CFI_SP &&
1485 op->src.reg == state->drap_reg) {
1486
1487 /* drap: lea disp(%drap), %rsp */
1488 cfa->base = CFI_SP;
1489 cfa->offset = state->stack_size = -op->src.offset;
1490 state->drap_reg = CFI_UNDEFINED;
1491 state->drap = false;
1492 break;
1493 }
1494
1495 if (op->dest.reg == state->cfa.base) {
1496 WARN_FUNC("unsupported stack register modification",
1497 insn->sec, insn->offset);
1498 return -1;
1499 }
1500
1501 break;
1502
1503 case OP_SRC_AND:
1504 if (op->dest.reg != CFI_SP ||
1505 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1506 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1507 WARN_FUNC("unsupported stack pointer realignment",
1508 insn->sec, insn->offset);
1509 return -1;
1510 }
1511
1512 if (state->drap_reg != CFI_UNDEFINED) {
1513 /* drap: and imm, %rsp */
1514 cfa->base = state->drap_reg;
1515 cfa->offset = state->stack_size = 0;
1516 state->drap = true;
1517 }
1518
1519 /*
1520 * Older versions of GCC (4.8ish) realign the stack
1521 * without DRAP, with a frame pointer.
1522 */
1523
1524 break;
1525
1526 case OP_SRC_POP:
1527 if (!state->drap && op->dest.type == OP_DEST_REG &&
1528 op->dest.reg == cfa->base) {
1529
1530 /* pop %rbp */
1531 cfa->base = CFI_SP;
1532 }
1533
1534 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1535 op->dest.type == OP_DEST_REG &&
1536 op->dest.reg == state->drap_reg &&
1537 state->drap_offset == -state->stack_size) {
1538
1539 /* drap: pop %drap */
1540 cfa->base = state->drap_reg;
1541 cfa->offset = 0;
1542 state->drap_offset = -1;
1543
1544 } else if (regs[op->dest.reg].offset == -state->stack_size) {
1545
1546 /* pop %reg */
1547 restore_reg(state, op->dest.reg);
1548 }
1549
1550 state->stack_size -= 8;
1551 if (cfa->base == CFI_SP)
1552 cfa->offset -= 8;
1553
1554 break;
1555
1556 case OP_SRC_REG_INDIRECT:
1557 if (state->drap && op->src.reg == CFI_BP &&
1558 op->src.offset == state->drap_offset) {
1559
1560 /* drap: mov disp(%rbp), %drap */
1561 cfa->base = state->drap_reg;
1562 cfa->offset = 0;
1563 state->drap_offset = -1;
1564 }
1565
1566 if (state->drap && op->src.reg == CFI_BP &&
1567 op->src.offset == regs[op->dest.reg].offset) {
1568
1569 /* drap: mov disp(%rbp), %reg */
1570 restore_reg(state, op->dest.reg);
1571
1572 } else if (op->src.reg == cfa->base &&
1573 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1574
1575 /* mov disp(%rbp), %reg */
1576 /* mov disp(%rsp), %reg */
1577 restore_reg(state, op->dest.reg);
1578 }
1579
1580 break;
1581
1582 default:
1583 WARN_FUNC("unknown stack-related instruction",
1584 insn->sec, insn->offset);
1585 return -1;
1586 }
1587
1588 break;
1589
1590 case OP_DEST_PUSH:
1591 state->stack_size += 8;
1592 if (cfa->base == CFI_SP)
1593 cfa->offset += 8;
1594
1595 if (op->src.type != OP_SRC_REG)
1596 break;
1597
1598 if (state->drap) {
1599 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1600
1601 /* drap: push %drap */
1602 cfa->base = CFI_BP_INDIRECT;
1603 cfa->offset = -state->stack_size;
1604
1605 /* save drap so we know when to restore it */
1606 state->drap_offset = -state->stack_size;
1607
1608 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1609
1610 /* drap: push %rbp */
1611 state->stack_size = 0;
1612
1613 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1614
1615 /* drap: push %reg */
1616 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1617 }
1618
1619 } else {
1620
1621 /* push %reg */
1622 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1623 }
1624
1625 /* detect when asm code uses rbp as a scratch register */
1626 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1627 cfa->base != CFI_BP)
1628 state->bp_scratch = true;
1629 break;
1630
1631 case OP_DEST_REG_INDIRECT:
1632
1633 if (state->drap) {
1634 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1635
1636 /* drap: mov %drap, disp(%rbp) */
1637 cfa->base = CFI_BP_INDIRECT;
1638 cfa->offset = op->dest.offset;
1639
1640 /* save drap offset so we know when to restore it */
1641 state->drap_offset = op->dest.offset;
1642 }
1643
1644 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1645
1646 /* drap: mov reg, disp(%rbp) */
1647 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1648 }
1649
1650 } else if (op->dest.reg == cfa->base) {
1651
1652 /* mov reg, disp(%rbp) */
1653 /* mov reg, disp(%rsp) */
1654 save_reg(state, op->src.reg, CFI_CFA,
1655 op->dest.offset - state->cfa.offset);
1656 }
1657
1658 break;
1659
1660 case OP_DEST_LEAVE:
1661 if ((!state->drap && cfa->base != CFI_BP) ||
1662 (state->drap && cfa->base != state->drap_reg)) {
1663 WARN_FUNC("leave instruction with modified stack frame",
1664 insn->sec, insn->offset);
1665 return -1;
1666 }
1667
1668 /* leave (mov %rbp, %rsp; pop %rbp) */
1669
1670 state->stack_size = -state->regs[CFI_BP].offset - 8;
1671 restore_reg(state, CFI_BP);
1672
1673 if (!state->drap) {
1674 cfa->base = CFI_SP;
1675 cfa->offset -= 8;
1676 }
1677
1678 break;
1679
1680 case OP_DEST_MEM:
1681 if (op->src.type != OP_SRC_POP) {
1682 WARN_FUNC("unknown stack-related memory operation",
1683 insn->sec, insn->offset);
1684 return -1;
1685 }
1686
1687 /* pop mem */
1688 state->stack_size -= 8;
1689 if (cfa->base == CFI_SP)
1690 cfa->offset -= 8;
1691
1692 break;
1693
1694 default:
1695 WARN_FUNC("unknown stack-related instruction",
1696 insn->sec, insn->offset);
1697 return -1;
1698 }
1699
1700 return 0;
1701}
1702
1703static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1704{
1705 struct insn_state *state1 = &insn->state, *state2 = state;
1706 int i;
1707
1708 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1709 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1710 insn->sec, insn->offset,
1711 state1->cfa.base, state1->cfa.offset,
1712 state2->cfa.base, state2->cfa.offset);
1713
1714 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1715 for (i = 0; i < CFI_NUM_REGS; i++) {
1716 if (!memcmp(&state1->regs[i], &state2->regs[i],
1717 sizeof(struct cfi_reg)))
1718 continue;
1719
1720 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1721 insn->sec, insn->offset,
1722 i, state1->regs[i].base, state1->regs[i].offset,
1723 i, state2->regs[i].base, state2->regs[i].offset);
1724 break;
1725 }
1726
1727 } else if (state1->type != state2->type) {
1728 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1729 insn->sec, insn->offset, state1->type, state2->type);
1730
1731 } else if (state1->drap != state2->drap ||
1732 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1733 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1734 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1735 insn->sec, insn->offset,
1736 state1->drap, state1->drap_reg, state1->drap_offset,
1737 state2->drap, state2->drap_reg, state2->drap_offset);
1738
1739 } else
1740 return true;
1741
1742 return false;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001743}
1744
1745/*
1746 * Follow the branch starting at the given instruction, and recursively follow
1747 * any other branches (jumps). Meanwhile, track the frame pointer state at
1748 * each instruction and validate all the rules described in
1749 * tools/objtool/Documentation/stack-validation.txt.
1750 */
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001751static int validate_branch(struct objtool_file *file, struct instruction *first,
1752 struct insn_state state)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001753{
1754 struct alternative *alt;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001755 struct instruction *insn, *next_insn;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001756 struct section *sec;
1757 struct symbol *func = NULL;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001758 int ret;
1759
1760 insn = first;
1761 sec = insn->sec;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001762
1763 if (insn->alt_group && list_empty(&insn->alts)) {
1764 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1765 sec, insn->offset);
1766 return 1;
1767 }
1768
1769 while (1) {
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001770 next_insn = next_insn_same_sec(file, insn);
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001771
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -05001772 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001773 WARN("%s() falls through to next function %s()",
1774 func->name, insn->func->name);
1775 return 1;
1776 }
1777
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -05001778 func = insn->func ? insn->func->pfunc : NULL;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001779
1780 if (func && insn->ignore) {
1781 WARN_FUNC("BUG: why am I validating an ignored function?",
1782 sec, insn->offset);
1783 return 1;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001784 }
1785
1786 if (insn->visited) {
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001787 if (!insn->hint && !insn_state_match(insn, &state))
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001788 return 1;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001789
1790 return 0;
1791 }
1792
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001793 if (insn->hint) {
1794 if (insn->restore) {
1795 struct instruction *save_insn, *i;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001796
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001797 i = insn;
1798 save_insn = NULL;
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -05001799 func_for_each_insn_continue_reverse(file, insn->func, i) {
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001800 if (i->save) {
1801 save_insn = i;
1802 break;
1803 }
1804 }
1805
1806 if (!save_insn) {
1807 WARN_FUNC("no corresponding CFI save for CFI restore",
1808 sec, insn->offset);
1809 return 1;
1810 }
1811
1812 if (!save_insn->visited) {
1813 /*
1814 * Oops, no state to copy yet.
1815 * Hopefully we can reach this
1816 * instruction from another branch
1817 * after the save insn has been
1818 * visited.
1819 */
1820 if (insn == first)
1821 return 0;
1822
1823 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1824 sec, insn->offset);
1825 return 1;
1826 }
1827
1828 insn->state = save_insn->state;
1829 }
1830
1831 state = insn->state;
1832
1833 } else
1834 insn->state = state;
1835
1836 insn->visited = true;
1837
1838 if (!insn->ignore_alts) {
1839 list_for_each_entry(alt, &insn->alts, list) {
1840 ret = validate_branch(file, alt->insn, state);
1841 if (ret)
1842 return 1;
1843 }
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001844 }
1845
1846 switch (insn->type) {
1847
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001848 case INSN_RETURN:
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001849 if (func && has_modified_stack_frame(&state)) {
1850 WARN_FUNC("return with modified stack frame",
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001851 sec, insn->offset);
1852 return 1;
1853 }
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001854
1855 if (state.bp_scratch) {
1856 WARN("%s uses BP as a scratch register",
1857 insn->func->name);
1858 return 1;
1859 }
1860
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001861 return 0;
1862
1863 case INSN_CALL:
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001864 if (is_fentry_call(insn))
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001865 break;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001866
1867 ret = dead_end_function(file, insn->call_dest);
1868 if (ret == 1)
1869 return 0;
1870 if (ret == -1)
1871 return 1;
1872
1873 /* fallthrough */
1874 case INSN_CALL_DYNAMIC:
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001875 if (!no_fp && func && !has_valid_stack_frame(&state)) {
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001876 WARN_FUNC("call without frame pointer save/setup",
1877 sec, insn->offset);
1878 return 1;
1879 }
1880 break;
1881
1882 case INSN_JUMP_CONDITIONAL:
1883 case INSN_JUMP_UNCONDITIONAL:
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001884 if (insn->jump_dest &&
1885 (!func || !insn->jump_dest->func ||
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -05001886 insn->jump_dest->func->pfunc == func)) {
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001887 ret = validate_branch(file, insn->jump_dest,
1888 state);
1889 if (ret)
1890 return 1;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001891
1892 } else if (func && has_modified_stack_frame(&state)) {
1893 WARN_FUNC("sibling call from callable instruction with modified stack frame",
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001894 sec, insn->offset);
1895 return 1;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001896 }
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001897
1898 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1899 return 0;
1900
1901 break;
1902
1903 case INSN_JUMP_DYNAMIC:
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001904 if (func && list_empty(&insn->alts) &&
1905 has_modified_stack_frame(&state)) {
1906 WARN_FUNC("sibling call from callable instruction with modified stack frame",
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001907 sec, insn->offset);
1908 return 1;
1909 }
1910
1911 return 0;
1912
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001913 case INSN_CONTEXT_SWITCH:
1914 if (func && (!next_insn || !next_insn->hint)) {
1915 WARN_FUNC("unsupported instruction in callable function",
1916 sec, insn->offset);
1917 return 1;
1918 }
1919 return 0;
1920
1921 case INSN_STACK:
1922 if (update_insn_state(insn, &state))
1923 return 1;
1924
1925 break;
1926
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001927 default:
1928 break;
1929 }
1930
1931 if (insn->dead_end)
1932 return 0;
1933
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001934 if (!next_insn) {
1935 if (state.cfa.base == CFI_UNDEFINED)
1936 return 0;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001937 WARN("%s: unexpected end of section", sec->name);
1938 return 1;
1939 }
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001940
1941 insn = next_insn;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05001942 }
1943
1944 return 0;
1945}
1946
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02001947static int validate_unwind_hints(struct objtool_file *file)
1948{
1949 struct instruction *insn;
1950 int ret, warnings = 0;
1951 struct insn_state state;
1952
1953 if (!file->hints)
1954 return 0;
1955
1956 clear_insn_state(&state);
1957
1958 for_each_insn(file, insn) {
1959 if (insn->hint && !insn->visited) {
1960 ret = validate_branch(file, insn, state);
1961 warnings += ret;
1962 }
1963 }
1964
1965 return warnings;
1966}
1967
1968static int validate_retpoline(struct objtool_file *file)
1969{
1970 struct instruction *insn;
1971 int warnings = 0;
1972
1973 for_each_insn(file, insn) {
1974 if (insn->type != INSN_JUMP_DYNAMIC &&
1975 insn->type != INSN_CALL_DYNAMIC)
1976 continue;
1977
1978 if (insn->retpoline_safe)
1979 continue;
1980
1981 /*
1982 * .init.text code is ran before userspace and thus doesn't
1983 * strictly need retpolines, except for modules which are
1984 * loaded late, they very much do need retpoline in their
1985 * .init.text
1986 */
1987 if (!strcmp(insn->sec->name, ".init.text") && !module)
1988 continue;
1989
1990 WARN_FUNC("indirect %s found in RETPOLINE build",
1991 insn->sec, insn->offset,
1992 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
1993
1994 warnings++;
1995 }
1996
1997 return warnings;
1998}
1999
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002000static bool is_kasan_insn(struct instruction *insn)
2001{
2002 return (insn->type == INSN_CALL &&
2003 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2004}
2005
2006static bool is_ubsan_insn(struct instruction *insn)
2007{
2008 return (insn->type == INSN_CALL &&
2009 !strcmp(insn->call_dest->name,
2010 "__ubsan_handle_builtin_unreachable"));
2011}
2012
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002013static bool ignore_unreachable_insn(struct instruction *insn)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002014{
2015 int i;
2016
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002017 if (insn->ignore || insn->type == INSN_NOP)
2018 return true;
2019
2020 /*
2021 * Ignore any unused exceptions. This can happen when a whitelisted
2022 * function has an exception table entry.
2023 *
2024 * Also ignore alternative replacement instructions. This can happen
2025 * when a whitelisted function uses one of the ALTERNATIVE macros.
2026 */
2027 if (!strcmp(insn->sec->name, ".fixup") ||
2028 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2029 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002030 return true;
2031
2032 /*
2033 * Check if this (or a subsequent) instruction is related to
2034 * CONFIG_UBSAN or CONFIG_KASAN.
2035 *
2036 * End the search at 5 instructions to avoid going into the weeds.
2037 */
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002038 if (!insn->func)
2039 return false;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002040 for (i = 0; i < 5; i++) {
2041
2042 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2043 return true;
2044
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002045 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2046 if (insn->jump_dest &&
2047 insn->jump_dest->func == insn->func) {
2048 insn = insn->jump_dest;
2049 continue;
2050 }
2051
2052 break;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002053 }
2054
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002055 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002056 break;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002057
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002058 insn = list_next_entry(insn, list);
2059 }
2060
2061 return false;
2062}
2063
2064static int validate_functions(struct objtool_file *file)
2065{
2066 struct section *sec;
2067 struct symbol *func;
2068 struct instruction *insn;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002069 struct insn_state state;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002070 int ret, warnings = 0;
2071
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002072 clear_insn_state(&state);
2073
2074 state.cfa = initial_func_cfi.cfa;
2075 memcpy(&state.regs, &initial_func_cfi.regs,
2076 CFI_NUM_REGS * sizeof(struct cfi_reg));
2077 state.stack_size = initial_func_cfi.cfa.offset;
2078
2079 for_each_sec(file, sec) {
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002080 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf1f7f88a2018-05-09 22:39:15 -05002081 if (func->type != STT_FUNC || func->pfunc != func)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002082 continue;
2083
2084 insn = find_insn(file, sec, func->offset);
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002085 if (!insn || insn->ignore)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002086 continue;
2087
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002088 ret = validate_branch(file, insn, state);
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002089 warnings += ret;
2090 }
2091 }
2092
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002093 return warnings;
2094}
2095
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002096static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002097{
2098 struct instruction *insn;
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002099
2100 if (file->ignore_unreachables)
2101 return 0;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002102
2103 for_each_insn(file, insn) {
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002104 if (insn->visited || ignore_unreachable_insn(insn))
2105 continue;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002106
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002107 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2108 return 1;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002109 }
2110
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002111 return 0;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002112}
2113
2114static void cleanup(struct objtool_file *file)
2115{
2116 struct instruction *insn, *tmpinsn;
2117 struct alternative *alt, *tmpalt;
2118
2119 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2120 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2121 list_del(&alt->list);
2122 free(alt);
2123 }
2124 list_del(&insn->list);
2125 hash_del(&insn->hash);
2126 free(insn);
2127 }
2128 elf_close(file->elf);
2129}
2130
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002131int check(const char *_objname, bool orc)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002132{
2133 struct objtool_file file;
2134 int ret, warnings = 0;
2135
2136 objname = _objname;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002137
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002138 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
2139 if (!file.elf)
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002140 return 1;
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002141
2142 INIT_LIST_HEAD(&file.insn_list);
2143 hash_init(file.insn_hash);
2144 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
2145 file.rodata = find_section_by_name(file.elf, ".rodata");
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002146 file.c_file = find_section_by_name(file.elf, ".comment");
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002147 file.ignore_unreachables = no_unreachable;
2148 file.hints = false;
2149
2150 arch_initial_func_cfi_state(&initial_func_cfi);
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002151
2152 ret = decode_sections(&file);
2153 if (ret < 0)
2154 goto out;
2155 warnings += ret;
2156
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002157 if (list_empty(&file.insn_list))
2158 goto out;
2159
2160 if (retpoline) {
2161 ret = validate_retpoline(&file);
2162 if (ret < 0)
2163 return ret;
2164 warnings += ret;
2165 }
2166
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002167 ret = validate_functions(&file);
2168 if (ret < 0)
2169 goto out;
2170 warnings += ret;
2171
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002172 ret = validate_unwind_hints(&file);
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002173 if (ret < 0)
2174 goto out;
2175 warnings += ret;
2176
Greg Kroah-Hartmanb790b4f2018-06-03 12:35:15 +02002177 if (!warnings) {
2178 ret = validate_reachable_instructions(&file);
2179 if (ret < 0)
2180 goto out;
2181 warnings += ret;
2182 }
2183
2184 if (orc) {
2185 ret = create_orc(&file);
2186 if (ret < 0)
2187 goto out;
2188
2189 ret = create_orc_sections(&file);
2190 if (ret < 0)
2191 goto out;
2192
2193 ret = elf_write(file.elf);
2194 if (ret < 0)
2195 goto out;
2196 }
2197
Josh Poimboeuf24ac7a42017-06-28 10:11:05 -05002198out:
2199 cleanup(&file);
2200
2201 /* ignore warnings for now until we get all the code cleaned up */
2202 if (ret || warnings)
2203 return 0;
2204 return 0;
2205}