blob: 157a0f96d64d97fe4ae57bd5d10658229cbf4a0a [file] [log] [blame]
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001/*
2 * Copyright (C) 2015 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/*
19 * objtool check:
20 *
21 * This command analyzes every .o file and ensures the validity of its stack
22 * trace metadata. It enforces a set of rules on asm code and C inline
23 * assembly code so that stack traces can be reliable.
24 *
25 * For more information, see tools/objtool/Documentation/stack-validation.txt.
26 */
27
28#include <string.h>
29#include <subcmd/parse-options.h>
30
31#include "builtin.h"
32#include "elf.h"
33#include "special.h"
34#include "arch.h"
35#include "warn.h"
36
Josh Poimboeuf042ba732016-03-09 00:07:00 -060037#include <linux/hashtable.h>
38
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060039#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
40
41#define STATE_FP_SAVED 0x1
42#define STATE_FP_SETUP 0x2
43#define STATE_FENTRY 0x4
44
45struct instruction {
46 struct list_head list;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060047 struct hlist_node hash;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060048 struct section *sec;
49 unsigned long offset;
50 unsigned int len, state;
51 unsigned char type;
52 unsigned long immediate;
53 bool alt_group, visited;
54 struct symbol *call_dest;
55 struct instruction *jump_dest;
56 struct list_head alts;
57};
58
59struct alternative {
60 struct list_head list;
61 struct instruction *insn;
62};
63
64struct objtool_file {
65 struct elf *elf;
Josh Poimboeufa196e172016-03-09 00:06:57 -060066 struct list_head insn_list;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060067 DECLARE_HASHTABLE(insn_hash, 16);
68 struct section *rodata, *whitelist;
Josh Poimboeuf7e578442016-04-14 14:52:24 -050069 bool ignore_unreachables;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060070};
71
72const char *objname;
73static bool nofp;
74
Josh Poimboeuf74aec052016-03-09 00:06:55 -060075static struct instruction *find_insn(struct objtool_file *file,
76 struct section *sec, unsigned long offset)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060077{
78 struct instruction *insn;
79
Josh Poimboeuf042ba732016-03-09 00:07:00 -060080 hash_for_each_possible(file->insn_hash, insn, hash, offset)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060081 if (insn->sec == sec && insn->offset == offset)
82 return insn;
83
84 return NULL;
85}
86
Josh Poimboeuf74aec052016-03-09 00:06:55 -060087static struct instruction *next_insn_same_sec(struct objtool_file *file,
88 struct instruction *insn)
89{
90 struct instruction *next = list_next_entry(insn, list);
91
Josh Poimboeufa196e172016-03-09 00:06:57 -060092 if (&next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeuf74aec052016-03-09 00:06:55 -060093 return NULL;
94
95 return next;
96}
97
98#define for_each_insn(file, insn) \
Josh Poimboeufa196e172016-03-09 00:06:57 -060099 list_for_each_entry(insn, &file->insn_list, list)
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600100
101#define func_for_each_insn(file, func, insn) \
102 for (insn = find_insn(file, func->sec, func->offset); \
Josh Poimboeufa196e172016-03-09 00:06:57 -0600103 insn && &insn->list != &file->insn_list && \
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600104 insn->sec == func->sec && \
105 insn->offset < func->offset + func->len; \
106 insn = list_next_entry(insn, list))
107
108#define sec_for_each_insn_from(file, insn) \
109 for (; insn; insn = next_insn_same_sec(file, insn))
110
111
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600112/*
113 * Check if the function has been manually whitelisted with the
114 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
115 * due to its use of a context switching instruction.
116 */
117static bool ignore_func(struct objtool_file *file, struct symbol *func)
118{
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600119 struct rela *rela;
120 struct instruction *insn;
121
122 /* check for STACK_FRAME_NON_STANDARD */
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600123 if (file->whitelist && file->whitelist->rela)
124 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600125 if (rela->sym->sec == func->sec &&
126 rela->addend == func->offset)
127 return true;
128
129 /* check if it has a context switching instruction */
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600130 func_for_each_insn(file, func, insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600131 if (insn->type == INSN_CONTEXT_SWITCH)
132 return true;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600133
134 return false;
135}
136
137/*
138 * This checks to see if the given function is a "noreturn" function.
139 *
140 * For global functions which are outside the scope of this object file, we
141 * have to keep a manual list of them.
142 *
143 * For local functions, we have to detect them manually by simply looking for
144 * the lack of a return instruction.
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600145 *
146 * Returns:
147 * -1: error
148 * 0: no dead end
149 * 1: dead end
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600150 */
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600151static int __dead_end_function(struct objtool_file *file, struct symbol *func,
152 int recursion)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600153{
154 int i;
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600155 struct instruction *insn;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600156 bool empty = true;
157
158 /*
159 * Unfortunately these have to be hard coded because the noreturn
160 * attribute isn't provided in ELF data.
161 */
162 static const char * const global_noreturns[] = {
163 "__stack_chk_fail",
164 "panic",
165 "do_exit",
166 "__module_put_and_exit",
167 "complete_and_exit",
168 "kvm_spurious_fault",
169 "__reiserfs_panic",
170 "lbug_with_loc"
171 };
172
173 if (func->bind == STB_WEAK)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600174 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600175
176 if (func->bind == STB_GLOBAL)
177 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
178 if (!strcmp(func->name, global_noreturns[i]))
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600179 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600180
181 if (!func->sec)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600182 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600183
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600184 func_for_each_insn(file, func, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600185 empty = false;
186
187 if (insn->type == INSN_RETURN)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600188 return 0;
Josh Poimboeuf81bfafc2016-03-09 00:06:51 -0600189 }
190
191 if (empty)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600192 return 0;
Josh Poimboeuf81bfafc2016-03-09 00:06:51 -0600193
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 Poimboeuf74aec052016-03-09 00:06:55 -0600199 func_for_each_insn(file, func, insn) {
Josh Poimboeuf81bfafc2016-03-09 00:06:51 -0600200 if (insn->sec != func->sec ||
201 insn->offset >= func->offset + func->len)
202 break;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600203
204 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
205 struct instruction *dest = insn->jump_dest;
206 struct symbol *dest_func;
207
208 if (!dest)
209 /* sibling call to another file */
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600210 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600211
212 if (dest->sec != func->sec ||
213 dest->offset < func->offset ||
214 dest->offset >= func->offset + func->len) {
215 /* local sibling call */
216 dest_func = find_symbol_by_offset(dest->sec,
217 dest->offset);
218 if (!dest_func)
219 continue;
220
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600221 if (recursion == 5) {
222 WARN_FUNC("infinite recursion (objtool bug!)",
223 dest->sec, dest->offset);
224 return -1;
225 }
226
227 return __dead_end_function(file, dest_func,
228 recursion + 1);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600229 }
230 }
231
232 if (insn->type == INSN_JUMP_DYNAMIC)
233 /* sibling call */
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600234 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600235 }
236
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600237 return 1;
238}
239
240static int dead_end_function(struct objtool_file *file, struct symbol *func)
241{
242 return __dead_end_function(file, func, 0);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600243}
244
245/*
246 * Call the arch-specific instruction decoder for all the instructions and add
Josh Poimboeufa196e172016-03-09 00:06:57 -0600247 * them to the global instruction list.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600248 */
249static int decode_instructions(struct objtool_file *file)
250{
251 struct section *sec;
252 unsigned long offset;
253 struct instruction *insn;
254 int ret;
255
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600256 list_for_each_entry(sec, &file->elf->sections, list) {
257
258 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
259 continue;
260
261 for (offset = 0; offset < sec->len; offset += insn->len) {
262 insn = malloc(sizeof(*insn));
263 memset(insn, 0, sizeof(*insn));
264
265 INIT_LIST_HEAD(&insn->alts);
266 insn->sec = sec;
267 insn->offset = offset;
268
269 ret = arch_decode_instruction(file->elf, sec, offset,
270 sec->len - offset,
271 &insn->len, &insn->type,
272 &insn->immediate);
273 if (ret)
274 return ret;
275
276 if (!insn->type || insn->type > INSN_LAST) {
277 WARN_FUNC("invalid instruction type %d",
278 insn->sec, insn->offset, insn->type);
279 return -1;
280 }
281
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600282 hash_add(file->insn_hash, &insn->hash, insn->offset);
Josh Poimboeufa196e172016-03-09 00:06:57 -0600283 list_add_tail(&insn->list, &file->insn_list);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600284 }
285 }
286
287 return 0;
288}
289
290/*
291 * Warnings shouldn't be reported for ignored functions.
292 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600293static void add_ignores(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600294{
295 struct instruction *insn;
296 struct section *sec;
297 struct symbol *func;
298
299 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -0600300 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600301 if (func->type != STT_FUNC)
302 continue;
303
304 if (!ignore_func(file, func))
305 continue;
306
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600307 func_for_each_insn(file, func, insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600308 insn->visited = true;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600309 }
310 }
311}
312
313/*
314 * Find the destination instructions for all jumps.
315 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600316static int add_jump_destinations(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600317{
318 struct instruction *insn;
319 struct rela *rela;
320 struct section *dest_sec;
321 unsigned long dest_off;
322
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600323 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600324 if (insn->type != INSN_JUMP_CONDITIONAL &&
325 insn->type != INSN_JUMP_UNCONDITIONAL)
326 continue;
327
328 /* skip ignores */
329 if (insn->visited)
330 continue;
331
332 rela = find_rela_by_dest_range(insn->sec, insn->offset,
333 insn->len);
334 if (!rela) {
335 dest_sec = insn->sec;
336 dest_off = insn->offset + insn->len + insn->immediate;
337 } else if (rela->sym->type == STT_SECTION) {
338 dest_sec = rela->sym->sec;
339 dest_off = rela->addend + 4;
340 } else if (rela->sym->sec->idx) {
341 dest_sec = rela->sym->sec;
342 dest_off = rela->sym->sym.st_value + rela->addend + 4;
343 } else {
344 /* sibling call */
345 insn->jump_dest = 0;
346 continue;
347 }
348
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600349 insn->jump_dest = find_insn(file, dest_sec, dest_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600350 if (!insn->jump_dest) {
351
352 /*
353 * This is a special case where an alt instruction
354 * jumps past the end of the section. These are
355 * handled later in handle_group_alt().
356 */
357 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
358 continue;
359
360 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
361 insn->sec, insn->offset, dest_sec->name,
362 dest_off);
363 return -1;
364 }
365 }
366
367 return 0;
368}
369
370/*
371 * Find the destination instructions for all calls.
372 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600373static int add_call_destinations(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600374{
375 struct instruction *insn;
376 unsigned long dest_off;
377 struct rela *rela;
378
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600379 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600380 if (insn->type != INSN_CALL)
381 continue;
382
383 rela = find_rela_by_dest_range(insn->sec, insn->offset,
384 insn->len);
385 if (!rela) {
386 dest_off = insn->offset + insn->len + insn->immediate;
387 insn->call_dest = find_symbol_by_offset(insn->sec,
388 dest_off);
389 if (!insn->call_dest) {
390 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
391 insn->sec, insn->offset, dest_off);
392 return -1;
393 }
394 } else if (rela->sym->type == STT_SECTION) {
395 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
396 rela->addend+4);
397 if (!insn->call_dest ||
398 insn->call_dest->type != STT_FUNC) {
399 WARN_FUNC("can't find call dest symbol at %s+0x%x",
400 insn->sec, insn->offset,
401 rela->sym->sec->name,
402 rela->addend + 4);
403 return -1;
404 }
405 } else
406 insn->call_dest = rela->sym;
407 }
408
409 return 0;
410}
411
412/*
413 * The .alternatives section requires some extra special care, over and above
414 * what other special sections require:
415 *
416 * 1. Because alternatives are patched in-place, we need to insert a fake jump
417 * instruction at the end so that validate_branch() skips all the original
418 * replaced instructions when validating the new instruction path.
419 *
420 * 2. An added wrinkle is that the new instruction length might be zero. In
421 * that case the old instructions are replaced with noops. We simulate that
422 * by creating a fake jump as the only new instruction.
423 *
424 * 3. In some cases, the alternative section includes an instruction which
425 * conditionally jumps to the _end_ of the entry. We have to modify these
426 * jumps' destinations to point back to .text rather than the end of the
427 * entry in .altinstr_replacement.
428 *
429 * 4. It has been requested that we don't validate the !POPCNT feature path
430 * which is a "very very small percentage of machines".
431 */
432static int handle_group_alt(struct objtool_file *file,
433 struct special_alt *special_alt,
434 struct instruction *orig_insn,
435 struct instruction **new_insn)
436{
437 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump;
438 unsigned long dest_off;
439
440 last_orig_insn = NULL;
441 insn = orig_insn;
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600442 sec_for_each_insn_from(file, insn) {
443 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600444 break;
445
446 if (special_alt->skip_orig)
447 insn->type = INSN_NOP;
448
449 insn->alt_group = true;
450 last_orig_insn = insn;
451 }
452
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600453 if (!next_insn_same_sec(file, last_orig_insn)) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600454 WARN("%s: don't know how to handle alternatives at end of section",
455 special_alt->orig_sec->name);
456 return -1;
457 }
458
459 fake_jump = malloc(sizeof(*fake_jump));
460 if (!fake_jump) {
461 WARN("malloc failed");
462 return -1;
463 }
464 memset(fake_jump, 0, sizeof(*fake_jump));
465 INIT_LIST_HEAD(&fake_jump->alts);
466 fake_jump->sec = special_alt->new_sec;
467 fake_jump->offset = -1;
468 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
469 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
470
471 if (!special_alt->new_len) {
472 *new_insn = fake_jump;
473 return 0;
474 }
475
476 last_new_insn = NULL;
477 insn = *new_insn;
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600478 sec_for_each_insn_from(file, insn) {
479 if (insn->offset >= special_alt->new_off + special_alt->new_len)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600480 break;
481
482 last_new_insn = insn;
483
484 if (insn->type != INSN_JUMP_CONDITIONAL &&
485 insn->type != INSN_JUMP_UNCONDITIONAL)
486 continue;
487
488 if (!insn->immediate)
489 continue;
490
491 dest_off = insn->offset + insn->len + insn->immediate;
492 if (dest_off == special_alt->new_off + special_alt->new_len)
493 insn->jump_dest = fake_jump;
494
495 if (!insn->jump_dest) {
496 WARN_FUNC("can't find alternative jump destination",
497 insn->sec, insn->offset);
498 return -1;
499 }
500 }
501
502 if (!last_new_insn) {
503 WARN_FUNC("can't find last new alternative instruction",
504 special_alt->new_sec, special_alt->new_off);
505 return -1;
506 }
507
508 list_add(&fake_jump->list, &last_new_insn->list);
509
510 return 0;
511}
512
513/*
514 * A jump table entry can either convert a nop to a jump or a jump to a nop.
515 * If the original instruction is a jump, make the alt entry an effective nop
516 * by just skipping the original instruction.
517 */
518static int handle_jump_alt(struct objtool_file *file,
519 struct special_alt *special_alt,
520 struct instruction *orig_insn,
521 struct instruction **new_insn)
522{
523 if (orig_insn->type == INSN_NOP)
524 return 0;
525
526 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
527 WARN_FUNC("unsupported instruction at jump label",
528 orig_insn->sec, orig_insn->offset);
529 return -1;
530 }
531
532 *new_insn = list_next_entry(orig_insn, list);
533 return 0;
534}
535
536/*
537 * Read all the special sections which have alternate instructions which can be
538 * patched in or redirected to at runtime. Each instruction having alternate
539 * instruction(s) has them added to its insn->alts list, which will be
540 * traversed in validate_branch().
541 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600542static int add_special_section_alts(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600543{
544 struct list_head special_alts;
545 struct instruction *orig_insn, *new_insn;
546 struct special_alt *special_alt, *tmp;
547 struct alternative *alt;
548 int ret;
549
550 ret = special_get_alts(file->elf, &special_alts);
551 if (ret)
552 return ret;
553
554 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
555 alt = malloc(sizeof(*alt));
556 if (!alt) {
557 WARN("malloc failed");
558 ret = -1;
559 goto out;
560 }
561
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600562 orig_insn = find_insn(file, special_alt->orig_sec,
563 special_alt->orig_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600564 if (!orig_insn) {
565 WARN_FUNC("special: can't find orig instruction",
566 special_alt->orig_sec, special_alt->orig_off);
567 ret = -1;
568 goto out;
569 }
570
571 new_insn = NULL;
572 if (!special_alt->group || special_alt->new_len) {
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600573 new_insn = find_insn(file, special_alt->new_sec,
574 special_alt->new_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600575 if (!new_insn) {
576 WARN_FUNC("special: can't find new instruction",
577 special_alt->new_sec,
578 special_alt->new_off);
579 ret = -1;
580 goto out;
581 }
582 }
583
584 if (special_alt->group) {
585 ret = handle_group_alt(file, special_alt, orig_insn,
586 &new_insn);
587 if (ret)
588 goto out;
589 } else if (special_alt->jump_or_nop) {
590 ret = handle_jump_alt(file, special_alt, orig_insn,
591 &new_insn);
592 if (ret)
593 goto out;
594 }
595
596 alt->insn = new_insn;
597 list_add_tail(&alt->list, &orig_insn->alts);
598
599 list_del(&special_alt->list);
600 free(special_alt);
601 }
602
603out:
604 return ret;
605}
606
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600607static int add_switch_table(struct objtool_file *file, struct symbol *func,
608 struct instruction *insn, struct rela *table,
609 struct rela *next_table)
610{
611 struct rela *rela = table;
612 struct instruction *alt_insn;
613 struct alternative *alt;
614
615 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
616 if (rela == next_table)
617 break;
618
619 if (rela->sym->sec != insn->sec ||
620 rela->addend <= func->offset ||
621 rela->addend >= func->offset + func->len)
622 break;
623
624 alt_insn = find_insn(file, insn->sec, rela->addend);
625 if (!alt_insn) {
626 WARN("%s: can't find instruction at %s+0x%x",
627 file->rodata->rela->name, insn->sec->name,
628 rela->addend);
629 return -1;
630 }
631
632 alt = malloc(sizeof(*alt));
633 if (!alt) {
634 WARN("malloc failed");
635 return -1;
636 }
637
638 alt->insn = alt_insn;
639 list_add_tail(&alt->list, &insn->alts);
640 }
641
642 return 0;
643}
644
645static int add_func_switch_tables(struct objtool_file *file,
646 struct symbol *func)
647{
648 struct instruction *insn, *prev_jump;
649 struct rela *text_rela, *rodata_rela, *prev_rela;
650 int ret;
651
652 prev_jump = NULL;
653
654 func_for_each_insn(file, func, insn) {
655 if (insn->type != INSN_JUMP_DYNAMIC)
656 continue;
657
658 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
659 insn->len);
660 if (!text_rela || text_rela->sym != file->rodata->sym)
661 continue;
662
663 /* common case: jmpq *[addr](,%rax,8) */
664 rodata_rela = find_rela_by_dest(file->rodata,
665 text_rela->addend);
666
667 /*
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600668 * rare case: jmpq *[addr](%rip)
Josh Poimboeuf7e578442016-04-14 14:52:24 -0500669 *
670 * This check is for a rare gcc quirk, currently only seen in
671 * three driver functions in the kernel, only with certain
672 * obscure non-distro configs.
673 *
674 * As part of an optimization, gcc makes a copy of an existing
675 * switch jump table, modifies it, and then hard-codes the jump
676 * (albeit with an indirect jump) to use a single entry in the
677 * table. The rest of the jump table and some of its jump
678 * targets remain as dead code.
679 *
680 * In such a case we can just crudely ignore all unreachable
681 * instruction warnings for the entire object file. Ideally we
682 * would just ignore them for the function, but that would
683 * require redesigning the code quite a bit. And honestly
684 * that's just not worth doing: unreachable instruction
685 * warnings are of questionable value anyway, and this is such
686 * a rare issue.
687 *
688 * kbuild reports:
689 * - https://lkml.kernel.org/r/201603231906.LWcVUpxm%25fengguang.wu@intel.com
690 * - https://lkml.kernel.org/r/201603271114.K9i45biy%25fengguang.wu@intel.com
691 * - https://lkml.kernel.org/r/201603291058.zuJ6ben1%25fengguang.wu@intel.com
692 *
693 * gcc bug:
694 * - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70604
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600695 */
Josh Poimboeuf7e578442016-04-14 14:52:24 -0500696 if (!rodata_rela) {
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600697 rodata_rela = find_rela_by_dest(file->rodata,
698 text_rela->addend + 4);
Josh Poimboeuf7e578442016-04-14 14:52:24 -0500699 if (rodata_rela)
700 file->ignore_unreachables = true;
701 }
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600702
703 if (!rodata_rela)
704 continue;
705
706 /*
707 * We found a switch table, but we don't know yet how big it
708 * is. Don't add it until we reach the end of the function or
709 * the beginning of another switch table in the same function.
710 */
711 if (prev_jump) {
712 ret = add_switch_table(file, func, prev_jump, prev_rela,
713 rodata_rela);
714 if (ret)
715 return ret;
716 }
717
718 prev_jump = insn;
719 prev_rela = rodata_rela;
720 }
721
722 if (prev_jump) {
723 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
724 if (ret)
725 return ret;
726 }
727
728 return 0;
729}
730
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600731/*
732 * For some switch statements, gcc generates a jump table in the .rodata
733 * section which contains a list of addresses within the function to jump to.
734 * This finds these jump tables and adds them to the insn->alts lists.
735 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600736static int add_switch_table_alts(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600737{
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600738 struct section *sec;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600739 struct symbol *func;
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600740 int ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600741
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600742 if (!file->rodata || !file->rodata->rela)
743 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600744
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600745 list_for_each_entry(sec, &file->elf->sections, list) {
746 list_for_each_entry(func, &sec->symbol_list, list) {
747 if (func->type != STT_FUNC)
748 continue;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600749
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600750 ret = add_func_switch_tables(file, func);
751 if (ret)
752 return ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600753 }
754 }
755
756 return 0;
757}
758
759static int decode_sections(struct objtool_file *file)
760{
761 int ret;
762
763 ret = decode_instructions(file);
764 if (ret)
765 return ret;
766
Josh Poimboeufa196e172016-03-09 00:06:57 -0600767 add_ignores(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600768
Josh Poimboeufa196e172016-03-09 00:06:57 -0600769 ret = add_jump_destinations(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600770 if (ret)
771 return ret;
772
Josh Poimboeufa196e172016-03-09 00:06:57 -0600773 ret = add_call_destinations(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600774 if (ret)
775 return ret;
776
Josh Poimboeufa196e172016-03-09 00:06:57 -0600777 ret = add_special_section_alts(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600778 if (ret)
779 return ret;
780
Josh Poimboeufa196e172016-03-09 00:06:57 -0600781 ret = add_switch_table_alts(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600782 if (ret)
783 return ret;
784
785 return 0;
786}
787
788static bool is_fentry_call(struct instruction *insn)
789{
790 if (insn->type == INSN_CALL &&
791 insn->call_dest->type == STT_NOTYPE &&
792 !strcmp(insn->call_dest->name, "__fentry__"))
793 return true;
794
795 return false;
796}
797
798static bool has_modified_stack_frame(struct instruction *insn)
799{
800 return (insn->state & STATE_FP_SAVED) ||
801 (insn->state & STATE_FP_SETUP);
802}
803
804static bool has_valid_stack_frame(struct instruction *insn)
805{
806 return (insn->state & STATE_FP_SAVED) &&
807 (insn->state & STATE_FP_SETUP);
808}
809
Josh Poimboeufd8d1b2c2016-03-09 00:06:54 -0600810static unsigned int frame_state(unsigned long state)
811{
812 return (state & (STATE_FP_SAVED | STATE_FP_SETUP));
813}
814
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600815/*
816 * Follow the branch starting at the given instruction, and recursively follow
817 * any other branches (jumps). Meanwhile, track the frame pointer state at
818 * each instruction and validate all the rules described in
819 * tools/objtool/Documentation/stack-validation.txt.
820 */
821static int validate_branch(struct objtool_file *file,
822 struct instruction *first, unsigned char first_state)
823{
824 struct alternative *alt;
825 struct instruction *insn;
826 struct section *sec;
827 unsigned char state;
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600828 int ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600829
830 insn = first;
831 sec = insn->sec;
832 state = first_state;
833
834 if (insn->alt_group && list_empty(&insn->alts)) {
835 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
836 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600837 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600838 }
839
840 while (1) {
841 if (insn->visited) {
Josh Poimboeufd8d1b2c2016-03-09 00:06:54 -0600842 if (frame_state(insn->state) != frame_state(state)) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600843 WARN_FUNC("frame pointer state mismatch",
844 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600845 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600846 }
847
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600848 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600849 }
850
851 /*
852 * Catch a rare case where a noreturn function falls through to
853 * the next function.
854 */
855 if (is_fentry_call(insn) && (state & STATE_FENTRY))
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600856 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600857
858 insn->visited = true;
859 insn->state = state;
860
861 list_for_each_entry(alt, &insn->alts, list) {
862 ret = validate_branch(file, alt->insn, state);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600863 if (ret)
864 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600865 }
866
867 switch (insn->type) {
868
869 case INSN_FP_SAVE:
870 if (!nofp) {
871 if (state & STATE_FP_SAVED) {
872 WARN_FUNC("duplicate frame pointer save",
873 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600874 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600875 }
876 state |= STATE_FP_SAVED;
877 }
878 break;
879
880 case INSN_FP_SETUP:
881 if (!nofp) {
882 if (state & STATE_FP_SETUP) {
883 WARN_FUNC("duplicate frame pointer setup",
884 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600885 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600886 }
887 state |= STATE_FP_SETUP;
888 }
889 break;
890
891 case INSN_FP_RESTORE:
892 if (!nofp) {
893 if (has_valid_stack_frame(insn))
894 state &= ~STATE_FP_SETUP;
895
896 state &= ~STATE_FP_SAVED;
897 }
898 break;
899
900 case INSN_RETURN:
901 if (!nofp && has_modified_stack_frame(insn)) {
902 WARN_FUNC("return without frame pointer restore",
903 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600904 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600905 }
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600906 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600907
908 case INSN_CALL:
909 if (is_fentry_call(insn)) {
910 state |= STATE_FENTRY;
911 break;
912 }
913
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600914 ret = dead_end_function(file, insn->call_dest);
915 if (ret == 1)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600916 return 0;
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600917 if (ret == -1)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600918 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600919
920 /* fallthrough */
921 case INSN_CALL_DYNAMIC:
922 if (!nofp && !has_valid_stack_frame(insn)) {
923 WARN_FUNC("call without frame pointer save/setup",
924 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600925 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600926 }
927 break;
928
929 case INSN_JUMP_CONDITIONAL:
930 case INSN_JUMP_UNCONDITIONAL:
931 if (insn->jump_dest) {
932 ret = validate_branch(file, insn->jump_dest,
933 state);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600934 if (ret)
935 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600936 } else if (has_modified_stack_frame(insn)) {
937 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
938 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600939 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600940 } /* else it's a sibling call */
941
942 if (insn->type == INSN_JUMP_UNCONDITIONAL)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600943 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600944
945 break;
946
947 case INSN_JUMP_DYNAMIC:
948 if (list_empty(&insn->alts) &&
949 has_modified_stack_frame(insn)) {
950 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
951 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600952 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600953 }
954
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600955 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600956
957 case INSN_BUG:
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600958 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600959
960 default:
961 break;
962 }
963
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600964 insn = next_insn_same_sec(file, insn);
965 if (!insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600966 WARN("%s: unexpected end of section", sec->name);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600967 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600968 }
969 }
970
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600971 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600972}
973
974static bool is_gcov_insn(struct instruction *insn)
975{
976 struct rela *rela;
977 struct section *sec;
978 struct symbol *sym;
979 unsigned long offset;
980
981 rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
982 if (!rela)
983 return false;
984
985 if (rela->sym->type != STT_SECTION)
986 return false;
987
988 sec = rela->sym->sec;
989 offset = rela->addend + insn->offset + insn->len - rela->offset;
990
Josh Poimboeufa196e172016-03-09 00:06:57 -0600991 list_for_each_entry(sym, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600992 if (sym->type != STT_OBJECT)
993 continue;
994
995 if (offset >= sym->offset && offset < sym->offset + sym->len)
996 return (!memcmp(sym->name, "__gcov0.", 8));
997 }
998
999 return false;
1000}
1001
1002static bool is_kasan_insn(struct instruction *insn)
1003{
1004 return (insn->type == INSN_CALL &&
1005 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1006}
1007
1008static bool is_ubsan_insn(struct instruction *insn)
1009{
1010 return (insn->type == INSN_CALL &&
1011 !strcmp(insn->call_dest->name,
1012 "__ubsan_handle_builtin_unreachable"));
1013}
1014
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001015static bool ignore_unreachable_insn(struct symbol *func,
1016 struct instruction *insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001017{
1018 int i;
1019
1020 if (insn->type == INSN_NOP)
1021 return true;
1022
1023 if (is_gcov_insn(insn))
1024 return true;
1025
1026 /*
1027 * Check if this (or a subsequent) instruction is related to
1028 * CONFIG_UBSAN or CONFIG_KASAN.
1029 *
1030 * End the search at 5 instructions to avoid going into the weeds.
1031 */
1032 for (i = 0; i < 5; i++) {
1033
1034 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
1035 return true;
1036
1037 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
1038 insn = insn->jump_dest;
1039 continue;
1040 }
1041
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001042 if (insn->offset + insn->len >= func->offset + func->len)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001043 break;
1044 insn = list_next_entry(insn, list);
1045 }
1046
1047 return false;
1048}
1049
1050static int validate_functions(struct objtool_file *file)
1051{
1052 struct section *sec;
1053 struct symbol *func;
1054 struct instruction *insn;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001055 int ret, warnings = 0;
1056
1057 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -06001058 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001059 if (func->type != STT_FUNC)
1060 continue;
1061
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001062 insn = find_insn(file, sec, func->offset);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001063 if (!insn) {
1064 WARN("%s(): can't find starting instruction",
1065 func->name);
1066 warnings++;
1067 continue;
1068 }
1069
1070 ret = validate_branch(file, insn, 0);
1071 warnings += ret;
1072 }
1073 }
1074
1075 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -06001076 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001077 if (func->type != STT_FUNC)
1078 continue;
1079
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001080 func_for_each_insn(file, func, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001081 if (insn->visited)
1082 continue;
1083
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001084 insn->visited = true;
Josh Poimboeuf7e578442016-04-14 14:52:24 -05001085
1086 if (file->ignore_unreachables || warnings ||
1087 ignore_unreachable_insn(func, insn))
1088 continue;
1089
1090 WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset);
1091 warnings++;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001092 }
1093 }
1094 }
1095
1096 return warnings;
1097}
1098
1099static int validate_uncallable_instructions(struct objtool_file *file)
1100{
1101 struct instruction *insn;
1102 int warnings = 0;
1103
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001104 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001105 if (!insn->visited && insn->type == INSN_RETURN) {
1106 WARN_FUNC("return instruction outside of a callable function",
1107 insn->sec, insn->offset);
1108 warnings++;
1109 }
1110 }
1111
1112 return warnings;
1113}
1114
1115static void cleanup(struct objtool_file *file)
1116{
1117 struct instruction *insn, *tmpinsn;
1118 struct alternative *alt, *tmpalt;
1119
Josh Poimboeufa196e172016-03-09 00:06:57 -06001120 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001121 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
1122 list_del(&alt->list);
1123 free(alt);
1124 }
1125 list_del(&insn->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -06001126 hash_del(&insn->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001127 free(insn);
1128 }
1129 elf_close(file->elf);
1130}
1131
1132const char * const check_usage[] = {
1133 "objtool check [<options>] file.o",
1134 NULL,
1135};
1136
1137int cmd_check(int argc, const char **argv)
1138{
1139 struct objtool_file file;
1140 int ret, warnings = 0;
1141
1142 const struct option options[] = {
1143 OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"),
1144 OPT_END(),
1145 };
1146
1147 argc = parse_options(argc, argv, options, check_usage, 0);
1148
1149 if (argc != 1)
1150 usage_with_options(check_usage, options);
1151
1152 objname = argv[0];
1153
1154 file.elf = elf_open(objname);
1155 if (!file.elf) {
1156 fprintf(stderr, "error reading elf file %s\n", objname);
1157 return 1;
1158 }
1159
Josh Poimboeufa196e172016-03-09 00:06:57 -06001160 INIT_LIST_HEAD(&file.insn_list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -06001161 hash_init(file.insn_hash);
Josh Poimboeuf7e578442016-04-14 14:52:24 -05001162 file.whitelist = find_section_by_name(file.elf, "__func_stack_frame_non_standard");
1163 file.rodata = find_section_by_name(file.elf, ".rodata");
1164 file.ignore_unreachables = false;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001165
1166 ret = decode_sections(&file);
1167 if (ret < 0)
1168 goto out;
1169 warnings += ret;
1170
1171 ret = validate_functions(&file);
1172 if (ret < 0)
1173 goto out;
1174 warnings += ret;
1175
1176 ret = validate_uncallable_instructions(&file);
1177 if (ret < 0)
1178 goto out;
1179 warnings += ret;
1180
1181out:
1182 cleanup(&file);
1183
1184 /* ignore warnings for now until we get all the code cleaned up */
1185 if (ret || warnings)
1186 return 0;
1187 return 0;
1188}