blob: bd09d0effef81c72e3e729c0e8d2d7fa5266fbfa [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>
Arnaldo Carvalho de Melod0761e32016-07-07 15:42:33 -030029#include <stdlib.h>
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060030#include <subcmd/parse-options.h>
31
32#include "builtin.h"
33#include "elf.h"
34#include "special.h"
35#include "arch.h"
36#include "warn.h"
37
Josh Poimboeuf042ba732016-03-09 00:07:00 -060038#include <linux/hashtable.h>
39
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060040#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
41
42#define STATE_FP_SAVED 0x1
43#define STATE_FP_SETUP 0x2
44#define STATE_FENTRY 0x4
45
46struct instruction {
47 struct list_head list;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060048 struct hlist_node hash;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060049 struct section *sec;
50 unsigned long offset;
51 unsigned int len, state;
52 unsigned char type;
53 unsigned long immediate;
54 bool alt_group, visited;
55 struct symbol *call_dest;
56 struct instruction *jump_dest;
57 struct list_head alts;
Josh Poimboeufb1547d32016-04-15 09:17:10 -050058 struct symbol *func;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060059};
60
61struct alternative {
62 struct list_head list;
63 struct instruction *insn;
64};
65
66struct objtool_file {
67 struct elf *elf;
Josh Poimboeufa196e172016-03-09 00:06:57 -060068 struct list_head insn_list;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060069 DECLARE_HASHTABLE(insn_hash, 16);
70 struct section *rodata, *whitelist;
Josh Poimboeufb1547d32016-04-15 09:17:10 -050071 bool ignore_unreachables, c_file;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060072};
73
74const char *objname;
75static bool nofp;
76
Josh Poimboeuf74aec052016-03-09 00:06:55 -060077static struct instruction *find_insn(struct objtool_file *file,
78 struct section *sec, unsigned long offset)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060079{
80 struct instruction *insn;
81
Josh Poimboeuf042ba732016-03-09 00:07:00 -060082 hash_for_each_possible(file->insn_hash, insn, hash, offset)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060083 if (insn->sec == sec && insn->offset == offset)
84 return insn;
85
86 return NULL;
87}
88
Josh Poimboeuf74aec052016-03-09 00:06:55 -060089static struct instruction *next_insn_same_sec(struct objtool_file *file,
90 struct instruction *insn)
91{
92 struct instruction *next = list_next_entry(insn, list);
93
Josh Poimboeufa196e172016-03-09 00:06:57 -060094 if (&next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeuf74aec052016-03-09 00:06:55 -060095 return NULL;
96
97 return next;
98}
99
100#define for_each_insn(file, insn) \
Josh Poimboeufa196e172016-03-09 00:06:57 -0600101 list_for_each_entry(insn, &file->insn_list, list)
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600102
103#define func_for_each_insn(file, func, insn) \
104 for (insn = find_insn(file, func->sec, func->offset); \
Josh Poimboeufa196e172016-03-09 00:06:57 -0600105 insn && &insn->list != &file->insn_list && \
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600106 insn->sec == func->sec && \
107 insn->offset < func->offset + func->len; \
108 insn = list_next_entry(insn, list))
109
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500110#define func_for_each_insn_continue_reverse(file, func, insn) \
111 for (insn = list_prev_entry(insn, list); \
112 &insn->list != &file->insn_list && \
113 insn->sec == func->sec && insn->offset >= func->offset; \
114 insn = list_prev_entry(insn, list))
115
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600116#define sec_for_each_insn_from(file, insn) \
117 for (; insn; insn = next_insn_same_sec(file, insn))
118
119
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600120/*
121 * Check if the function has been manually whitelisted with the
122 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
123 * due to its use of a context switching instruction.
124 */
125static bool ignore_func(struct objtool_file *file, struct symbol *func)
126{
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600127 struct rela *rela;
128 struct instruction *insn;
129
130 /* check for STACK_FRAME_NON_STANDARD */
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600131 if (file->whitelist && file->whitelist->rela)
Josh Poimboeuf0ea5ad82016-06-15 15:45:58 -0500132 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) {
133 if (rela->sym->type == STT_SECTION &&
134 rela->sym->sec == func->sec &&
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600135 rela->addend == func->offset)
136 return true;
Josh Poimboeuf0ea5ad82016-06-15 15:45:58 -0500137 if (rela->sym->type == STT_FUNC && rela->sym == func)
138 return true;
139 }
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600140
141 /* check if it has a context switching instruction */
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600142 func_for_each_insn(file, func, insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600143 if (insn->type == INSN_CONTEXT_SWITCH)
144 return true;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600145
146 return false;
147}
148
149/*
150 * This checks to see if the given function is a "noreturn" function.
151 *
152 * For global functions which are outside the scope of this object file, we
153 * have to keep a manual list of them.
154 *
155 * For local functions, we have to detect them manually by simply looking for
156 * the lack of a return instruction.
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600157 *
158 * Returns:
159 * -1: error
160 * 0: no dead end
161 * 1: dead end
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600162 */
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600163static int __dead_end_function(struct objtool_file *file, struct symbol *func,
164 int recursion)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600165{
166 int i;
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600167 struct instruction *insn;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600168 bool empty = true;
169
170 /*
171 * Unfortunately these have to be hard coded because the noreturn
172 * attribute isn't provided in ELF data.
173 */
174 static const char * const global_noreturns[] = {
175 "__stack_chk_fail",
176 "panic",
177 "do_exit",
178 "__module_put_and_exit",
179 "complete_and_exit",
180 "kvm_spurious_fault",
181 "__reiserfs_panic",
182 "lbug_with_loc"
183 };
184
185 if (func->bind == STB_WEAK)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600186 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600187
188 if (func->bind == STB_GLOBAL)
189 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
190 if (!strcmp(func->name, global_noreturns[i]))
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600191 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600192
193 if (!func->sec)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600194 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600195
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600196 func_for_each_insn(file, func, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600197 empty = false;
198
199 if (insn->type == INSN_RETURN)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600200 return 0;
Josh Poimboeuf81bfafc2016-03-09 00:06:51 -0600201 }
202
203 if (empty)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600204 return 0;
Josh Poimboeuf81bfafc2016-03-09 00:06:51 -0600205
206 /*
207 * A function can have a sibling call instead of a return. In that
208 * case, the function's dead-end status depends on whether the target
209 * of the sibling call returns.
210 */
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600211 func_for_each_insn(file, func, insn) {
Josh Poimboeuf81bfafc2016-03-09 00:06:51 -0600212 if (insn->sec != func->sec ||
213 insn->offset >= func->offset + func->len)
214 break;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600215
216 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
217 struct instruction *dest = insn->jump_dest;
218 struct symbol *dest_func;
219
220 if (!dest)
221 /* sibling call to another file */
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600222 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600223
224 if (dest->sec != func->sec ||
225 dest->offset < func->offset ||
226 dest->offset >= func->offset + func->len) {
227 /* local sibling call */
228 dest_func = find_symbol_by_offset(dest->sec,
229 dest->offset);
230 if (!dest_func)
231 continue;
232
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600233 if (recursion == 5) {
234 WARN_FUNC("infinite recursion (objtool bug!)",
235 dest->sec, dest->offset);
236 return -1;
237 }
238
239 return __dead_end_function(file, dest_func,
240 recursion + 1);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600241 }
242 }
243
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500244 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600245 /* sibling call */
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600246 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600247 }
248
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600249 return 1;
250}
251
252static int dead_end_function(struct objtool_file *file, struct symbol *func)
253{
254 return __dead_end_function(file, func, 0);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600255}
256
257/*
258 * Call the arch-specific instruction decoder for all the instructions and add
Josh Poimboeufa196e172016-03-09 00:06:57 -0600259 * them to the global instruction list.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600260 */
261static int decode_instructions(struct objtool_file *file)
262{
263 struct section *sec;
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500264 struct symbol *func;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600265 unsigned long offset;
266 struct instruction *insn;
267 int ret;
268
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600269 list_for_each_entry(sec, &file->elf->sections, list) {
270
271 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
272 continue;
273
274 for (offset = 0; offset < sec->len; offset += insn->len) {
275 insn = malloc(sizeof(*insn));
276 memset(insn, 0, sizeof(*insn));
277
278 INIT_LIST_HEAD(&insn->alts);
279 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,
285 &insn->immediate);
286 if (ret)
287 return ret;
288
289 if (!insn->type || insn->type > INSN_LAST) {
290 WARN_FUNC("invalid instruction type %d",
291 insn->sec, insn->offset, insn->type);
292 return -1;
293 }
294
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600295 hash_add(file->insn_hash, &insn->hash, insn->offset);
Josh Poimboeufa196e172016-03-09 00:06:57 -0600296 list_add_tail(&insn->list, &file->insn_list);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600297 }
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500298
299 list_for_each_entry(func, &sec->symbol_list, list) {
300 if (func->type != STT_FUNC)
301 continue;
302
303 if (!find_insn(file, sec, func->offset)) {
304 WARN("%s(): can't find starting instruction",
305 func->name);
306 return -1;
307 }
308
309 func_for_each_insn(file, func, insn)
310 if (!insn->func)
311 insn->func = func;
312 }
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600313 }
314
315 return 0;
316}
317
318/*
319 * Warnings shouldn't be reported for ignored functions.
320 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600321static void add_ignores(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600322{
323 struct instruction *insn;
324 struct section *sec;
325 struct symbol *func;
326
327 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -0600328 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600329 if (func->type != STT_FUNC)
330 continue;
331
332 if (!ignore_func(file, func))
333 continue;
334
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600335 func_for_each_insn(file, func, insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600336 insn->visited = true;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600337 }
338 }
339}
340
341/*
342 * Find the destination instructions for all jumps.
343 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600344static int add_jump_destinations(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600345{
346 struct instruction *insn;
347 struct rela *rela;
348 struct section *dest_sec;
349 unsigned long dest_off;
350
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600351 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600352 if (insn->type != INSN_JUMP_CONDITIONAL &&
353 insn->type != INSN_JUMP_UNCONDITIONAL)
354 continue;
355
356 /* skip ignores */
357 if (insn->visited)
358 continue;
359
360 rela = find_rela_by_dest_range(insn->sec, insn->offset,
361 insn->len);
362 if (!rela) {
363 dest_sec = insn->sec;
364 dest_off = insn->offset + insn->len + insn->immediate;
365 } else if (rela->sym->type == STT_SECTION) {
366 dest_sec = rela->sym->sec;
367 dest_off = rela->addend + 4;
368 } else if (rela->sym->sec->idx) {
369 dest_sec = rela->sym->sec;
370 dest_off = rela->sym->sym.st_value + rela->addend + 4;
371 } else {
372 /* sibling call */
373 insn->jump_dest = 0;
374 continue;
375 }
376
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600377 insn->jump_dest = find_insn(file, dest_sec, dest_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600378 if (!insn->jump_dest) {
379
380 /*
381 * This is a special case where an alt instruction
382 * jumps past the end of the section. These are
383 * handled later in handle_group_alt().
384 */
385 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
386 continue;
387
388 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
389 insn->sec, insn->offset, dest_sec->name,
390 dest_off);
391 return -1;
392 }
393 }
394
395 return 0;
396}
397
398/*
399 * Find the destination instructions for all calls.
400 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600401static int add_call_destinations(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600402{
403 struct instruction *insn;
404 unsigned long dest_off;
405 struct rela *rela;
406
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600407 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600408 if (insn->type != INSN_CALL)
409 continue;
410
411 rela = find_rela_by_dest_range(insn->sec, insn->offset,
412 insn->len);
413 if (!rela) {
414 dest_off = insn->offset + insn->len + insn->immediate;
415 insn->call_dest = find_symbol_by_offset(insn->sec,
416 dest_off);
417 if (!insn->call_dest) {
418 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
419 insn->sec, insn->offset, dest_off);
420 return -1;
421 }
422 } else if (rela->sym->type == STT_SECTION) {
423 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
424 rela->addend+4);
425 if (!insn->call_dest ||
426 insn->call_dest->type != STT_FUNC) {
427 WARN_FUNC("can't find call dest symbol at %s+0x%x",
428 insn->sec, insn->offset,
429 rela->sym->sec->name,
430 rela->addend + 4);
431 return -1;
432 }
433 } else
434 insn->call_dest = rela->sym;
435 }
436
437 return 0;
438}
439
440/*
441 * The .alternatives section requires some extra special care, over and above
442 * what other special sections require:
443 *
444 * 1. Because alternatives are patched in-place, we need to insert a fake jump
445 * instruction at the end so that validate_branch() skips all the original
446 * replaced instructions when validating the new instruction path.
447 *
448 * 2. An added wrinkle is that the new instruction length might be zero. In
449 * that case the old instructions are replaced with noops. We simulate that
450 * by creating a fake jump as the only new instruction.
451 *
452 * 3. In some cases, the alternative section includes an instruction which
453 * conditionally jumps to the _end_ of the entry. We have to modify these
454 * jumps' destinations to point back to .text rather than the end of the
455 * entry in .altinstr_replacement.
456 *
457 * 4. It has been requested that we don't validate the !POPCNT feature path
458 * which is a "very very small percentage of machines".
459 */
460static int handle_group_alt(struct objtool_file *file,
461 struct special_alt *special_alt,
462 struct instruction *orig_insn,
463 struct instruction **new_insn)
464{
465 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump;
466 unsigned long dest_off;
467
468 last_orig_insn = NULL;
469 insn = orig_insn;
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600470 sec_for_each_insn_from(file, insn) {
471 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600472 break;
473
474 if (special_alt->skip_orig)
475 insn->type = INSN_NOP;
476
477 insn->alt_group = true;
478 last_orig_insn = insn;
479 }
480
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600481 if (!next_insn_same_sec(file, last_orig_insn)) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600482 WARN("%s: don't know how to handle alternatives at end of section",
483 special_alt->orig_sec->name);
484 return -1;
485 }
486
487 fake_jump = malloc(sizeof(*fake_jump));
488 if (!fake_jump) {
489 WARN("malloc failed");
490 return -1;
491 }
492 memset(fake_jump, 0, sizeof(*fake_jump));
493 INIT_LIST_HEAD(&fake_jump->alts);
494 fake_jump->sec = special_alt->new_sec;
495 fake_jump->offset = -1;
496 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
497 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
498
499 if (!special_alt->new_len) {
500 *new_insn = fake_jump;
501 return 0;
502 }
503
504 last_new_insn = NULL;
505 insn = *new_insn;
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600506 sec_for_each_insn_from(file, insn) {
507 if (insn->offset >= special_alt->new_off + special_alt->new_len)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600508 break;
509
510 last_new_insn = insn;
511
512 if (insn->type != INSN_JUMP_CONDITIONAL &&
513 insn->type != INSN_JUMP_UNCONDITIONAL)
514 continue;
515
516 if (!insn->immediate)
517 continue;
518
519 dest_off = insn->offset + insn->len + insn->immediate;
520 if (dest_off == special_alt->new_off + special_alt->new_len)
521 insn->jump_dest = fake_jump;
522
523 if (!insn->jump_dest) {
524 WARN_FUNC("can't find alternative jump destination",
525 insn->sec, insn->offset);
526 return -1;
527 }
528 }
529
530 if (!last_new_insn) {
531 WARN_FUNC("can't find last new alternative instruction",
532 special_alt->new_sec, special_alt->new_off);
533 return -1;
534 }
535
536 list_add(&fake_jump->list, &last_new_insn->list);
537
538 return 0;
539}
540
541/*
542 * A jump table entry can either convert a nop to a jump or a jump to a nop.
543 * If the original instruction is a jump, make the alt entry an effective nop
544 * by just skipping the original instruction.
545 */
546static int handle_jump_alt(struct objtool_file *file,
547 struct special_alt *special_alt,
548 struct instruction *orig_insn,
549 struct instruction **new_insn)
550{
551 if (orig_insn->type == INSN_NOP)
552 return 0;
553
554 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
555 WARN_FUNC("unsupported instruction at jump label",
556 orig_insn->sec, orig_insn->offset);
557 return -1;
558 }
559
560 *new_insn = list_next_entry(orig_insn, list);
561 return 0;
562}
563
564/*
565 * Read all the special sections which have alternate instructions which can be
566 * patched in or redirected to at runtime. Each instruction having alternate
567 * instruction(s) has them added to its insn->alts list, which will be
568 * traversed in validate_branch().
569 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600570static int add_special_section_alts(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600571{
572 struct list_head special_alts;
573 struct instruction *orig_insn, *new_insn;
574 struct special_alt *special_alt, *tmp;
575 struct alternative *alt;
576 int ret;
577
578 ret = special_get_alts(file->elf, &special_alts);
579 if (ret)
580 return ret;
581
582 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
583 alt = malloc(sizeof(*alt));
584 if (!alt) {
585 WARN("malloc failed");
586 ret = -1;
587 goto out;
588 }
589
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600590 orig_insn = find_insn(file, special_alt->orig_sec,
591 special_alt->orig_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600592 if (!orig_insn) {
593 WARN_FUNC("special: can't find orig instruction",
594 special_alt->orig_sec, special_alt->orig_off);
595 ret = -1;
596 goto out;
597 }
598
599 new_insn = NULL;
600 if (!special_alt->group || special_alt->new_len) {
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600601 new_insn = find_insn(file, special_alt->new_sec,
602 special_alt->new_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600603 if (!new_insn) {
604 WARN_FUNC("special: can't find new instruction",
605 special_alt->new_sec,
606 special_alt->new_off);
607 ret = -1;
608 goto out;
609 }
610 }
611
612 if (special_alt->group) {
613 ret = handle_group_alt(file, special_alt, orig_insn,
614 &new_insn);
615 if (ret)
616 goto out;
617 } else if (special_alt->jump_or_nop) {
618 ret = handle_jump_alt(file, special_alt, orig_insn,
619 &new_insn);
620 if (ret)
621 goto out;
622 }
623
624 alt->insn = new_insn;
625 list_add_tail(&alt->list, &orig_insn->alts);
626
627 list_del(&special_alt->list);
628 free(special_alt);
629 }
630
631out:
632 return ret;
633}
634
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600635static int add_switch_table(struct objtool_file *file, struct symbol *func,
636 struct instruction *insn, struct rela *table,
637 struct rela *next_table)
638{
639 struct rela *rela = table;
640 struct instruction *alt_insn;
641 struct alternative *alt;
642
643 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
644 if (rela == next_table)
645 break;
646
647 if (rela->sym->sec != insn->sec ||
648 rela->addend <= func->offset ||
649 rela->addend >= func->offset + func->len)
650 break;
651
652 alt_insn = find_insn(file, insn->sec, rela->addend);
653 if (!alt_insn) {
654 WARN("%s: can't find instruction at %s+0x%x",
655 file->rodata->rela->name, insn->sec->name,
656 rela->addend);
657 return -1;
658 }
659
660 alt = malloc(sizeof(*alt));
661 if (!alt) {
662 WARN("malloc failed");
663 return -1;
664 }
665
666 alt->insn = alt_insn;
667 list_add_tail(&alt->list, &insn->alts);
668 }
669
670 return 0;
671}
672
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500673/*
674 * find_switch_table() - Given a dynamic jump, find the switch jump table in
675 * .rodata associated with it.
676 *
677 * There are 3 basic patterns:
678 *
679 * 1. jmpq *[rodata addr](,%reg,8)
680 *
681 * This is the most common case by far. It jumps to an address in a simple
682 * jump table which is stored in .rodata.
683 *
684 * 2. jmpq *[rodata addr](%rip)
685 *
686 * This is caused by a rare GCC quirk, currently only seen in three driver
687 * functions in the kernel, only with certain obscure non-distro configs.
688 *
689 * As part of an optimization, GCC makes a copy of an existing switch jump
690 * table, modifies it, and then hard-codes the jump (albeit with an indirect
691 * jump) to use a single entry in the table. The rest of the jump table and
692 * some of its jump targets remain as dead code.
693 *
694 * In such a case we can just crudely ignore all unreachable instruction
695 * warnings for the entire object file. Ideally we would just ignore them
696 * for the function, but that would require redesigning the code quite a
697 * bit. And honestly that's just not worth doing: unreachable instruction
698 * warnings are of questionable value anyway, and this is such a rare issue.
699 *
700 * 3. mov [rodata addr],%reg1
701 * ... some instructions ...
702 * jmpq *(%reg1,%reg2,8)
703 *
704 * This is a fairly uncommon pattern which is new for GCC 6. As of this
705 * writing, there are 11 occurrences of it in the allmodconfig kernel.
706 *
707 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
708 * ensure the same register is used in the mov and jump instructions.
709 */
710static struct rela *find_switch_table(struct objtool_file *file,
711 struct symbol *func,
712 struct instruction *insn)
713{
714 struct rela *text_rela, *rodata_rela;
715
716 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
717 if (text_rela && text_rela->sym == file->rodata->sym) {
718 /* case 1 */
719 rodata_rela = find_rela_by_dest(file->rodata,
720 text_rela->addend);
721 if (rodata_rela)
722 return rodata_rela;
723
724 /* case 2 */
725 rodata_rela = find_rela_by_dest(file->rodata,
726 text_rela->addend + 4);
727 if (!rodata_rela)
728 return NULL;
729 file->ignore_unreachables = true;
730 return rodata_rela;
731 }
732
733 /* case 3 */
734 func_for_each_insn_continue_reverse(file, func, insn) {
735 if (insn->type == INSN_JUMP_UNCONDITIONAL ||
736 insn->type == INSN_JUMP_DYNAMIC)
737 break;
738
739 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
740 insn->len);
741 if (text_rela && text_rela->sym == file->rodata->sym)
742 return find_rela_by_dest(file->rodata,
743 text_rela->addend);
744 }
745
746 return NULL;
747}
748
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600749static int add_func_switch_tables(struct objtool_file *file,
750 struct symbol *func)
751{
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500752 struct instruction *insn, *prev_jump = NULL;
753 struct rela *rela, *prev_rela = NULL;
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600754 int ret;
755
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600756 func_for_each_insn(file, func, insn) {
757 if (insn->type != INSN_JUMP_DYNAMIC)
758 continue;
759
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500760 rela = find_switch_table(file, func, insn);
761 if (!rela)
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600762 continue;
763
764 /*
765 * We found a switch table, but we don't know yet how big it
766 * is. Don't add it until we reach the end of the function or
767 * the beginning of another switch table in the same function.
768 */
769 if (prev_jump) {
770 ret = add_switch_table(file, func, prev_jump, prev_rela,
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500771 rela);
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600772 if (ret)
773 return ret;
774 }
775
776 prev_jump = insn;
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500777 prev_rela = rela;
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600778 }
779
780 if (prev_jump) {
781 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
782 if (ret)
783 return ret;
784 }
785
786 return 0;
787}
788
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600789/*
790 * For some switch statements, gcc generates a jump table in the .rodata
791 * section which contains a list of addresses within the function to jump to.
792 * This finds these jump tables and adds them to the insn->alts lists.
793 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600794static int add_switch_table_alts(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600795{
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600796 struct section *sec;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600797 struct symbol *func;
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600798 int ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600799
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600800 if (!file->rodata || !file->rodata->rela)
801 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600802
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600803 list_for_each_entry(sec, &file->elf->sections, list) {
804 list_for_each_entry(func, &sec->symbol_list, list) {
805 if (func->type != STT_FUNC)
806 continue;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600807
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600808 ret = add_func_switch_tables(file, func);
809 if (ret)
810 return ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600811 }
812 }
813
814 return 0;
815}
816
817static int decode_sections(struct objtool_file *file)
818{
819 int ret;
820
821 ret = decode_instructions(file);
822 if (ret)
823 return ret;
824
Josh Poimboeufa196e172016-03-09 00:06:57 -0600825 add_ignores(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600826
Josh Poimboeufa196e172016-03-09 00:06:57 -0600827 ret = add_jump_destinations(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600828 if (ret)
829 return ret;
830
Josh Poimboeufa196e172016-03-09 00:06:57 -0600831 ret = add_call_destinations(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600832 if (ret)
833 return ret;
834
Josh Poimboeufa196e172016-03-09 00:06:57 -0600835 ret = add_special_section_alts(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600836 if (ret)
837 return ret;
838
Josh Poimboeufa196e172016-03-09 00:06:57 -0600839 ret = add_switch_table_alts(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600840 if (ret)
841 return ret;
842
843 return 0;
844}
845
846static bool is_fentry_call(struct instruction *insn)
847{
848 if (insn->type == INSN_CALL &&
849 insn->call_dest->type == STT_NOTYPE &&
850 !strcmp(insn->call_dest->name, "__fentry__"))
851 return true;
852
853 return false;
854}
855
856static bool has_modified_stack_frame(struct instruction *insn)
857{
858 return (insn->state & STATE_FP_SAVED) ||
859 (insn->state & STATE_FP_SETUP);
860}
861
862static bool has_valid_stack_frame(struct instruction *insn)
863{
864 return (insn->state & STATE_FP_SAVED) &&
865 (insn->state & STATE_FP_SETUP);
866}
867
Josh Poimboeufd8d1b2c2016-03-09 00:06:54 -0600868static unsigned int frame_state(unsigned long state)
869{
870 return (state & (STATE_FP_SAVED | STATE_FP_SETUP));
871}
872
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600873/*
874 * Follow the branch starting at the given instruction, and recursively follow
875 * any other branches (jumps). Meanwhile, track the frame pointer state at
876 * each instruction and validate all the rules described in
877 * tools/objtool/Documentation/stack-validation.txt.
878 */
879static int validate_branch(struct objtool_file *file,
880 struct instruction *first, unsigned char first_state)
881{
882 struct alternative *alt;
883 struct instruction *insn;
884 struct section *sec;
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500885 struct symbol *func = NULL;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600886 unsigned char state;
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600887 int ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600888
889 insn = first;
890 sec = insn->sec;
891 state = first_state;
892
893 if (insn->alt_group && list_empty(&insn->alts)) {
894 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
895 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600896 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600897 }
898
899 while (1) {
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500900 if (file->c_file && insn->func) {
901 if (func && func != insn->func) {
902 WARN("%s() falls through to next function %s()",
903 func->name, insn->func->name);
904 return 1;
905 }
906
907 func = insn->func;
908 }
909
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600910 if (insn->visited) {
Josh Poimboeufd8d1b2c2016-03-09 00:06:54 -0600911 if (frame_state(insn->state) != frame_state(state)) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600912 WARN_FUNC("frame pointer state mismatch",
913 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600914 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600915 }
916
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600917 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600918 }
919
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600920 insn->visited = true;
921 insn->state = state;
922
923 list_for_each_entry(alt, &insn->alts, list) {
924 ret = validate_branch(file, alt->insn, state);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600925 if (ret)
926 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600927 }
928
929 switch (insn->type) {
930
931 case INSN_FP_SAVE:
932 if (!nofp) {
933 if (state & STATE_FP_SAVED) {
934 WARN_FUNC("duplicate frame pointer save",
935 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600936 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600937 }
938 state |= STATE_FP_SAVED;
939 }
940 break;
941
942 case INSN_FP_SETUP:
943 if (!nofp) {
944 if (state & STATE_FP_SETUP) {
945 WARN_FUNC("duplicate frame pointer setup",
946 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600947 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600948 }
949 state |= STATE_FP_SETUP;
950 }
951 break;
952
953 case INSN_FP_RESTORE:
954 if (!nofp) {
955 if (has_valid_stack_frame(insn))
956 state &= ~STATE_FP_SETUP;
957
958 state &= ~STATE_FP_SAVED;
959 }
960 break;
961
962 case INSN_RETURN:
963 if (!nofp && has_modified_stack_frame(insn)) {
964 WARN_FUNC("return without frame pointer restore",
965 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600966 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600967 }
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600968 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600969
970 case INSN_CALL:
971 if (is_fentry_call(insn)) {
972 state |= STATE_FENTRY;
973 break;
974 }
975
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600976 ret = dead_end_function(file, insn->call_dest);
977 if (ret == 1)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600978 return 0;
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600979 if (ret == -1)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600980 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600981
982 /* fallthrough */
983 case INSN_CALL_DYNAMIC:
984 if (!nofp && !has_valid_stack_frame(insn)) {
985 WARN_FUNC("call without frame pointer save/setup",
986 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600987 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600988 }
989 break;
990
991 case INSN_JUMP_CONDITIONAL:
992 case INSN_JUMP_UNCONDITIONAL:
993 if (insn->jump_dest) {
994 ret = validate_branch(file, insn->jump_dest,
995 state);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600996 if (ret)
997 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600998 } else if (has_modified_stack_frame(insn)) {
999 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
1000 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001001 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001002 } /* else it's a sibling call */
1003
1004 if (insn->type == INSN_JUMP_UNCONDITIONAL)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001005 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001006
1007 break;
1008
1009 case INSN_JUMP_DYNAMIC:
1010 if (list_empty(&insn->alts) &&
1011 has_modified_stack_frame(insn)) {
1012 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
1013 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001014 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001015 }
1016
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001017 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001018
1019 case INSN_BUG:
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001020 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001021
1022 default:
1023 break;
1024 }
1025
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001026 insn = next_insn_same_sec(file, insn);
1027 if (!insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001028 WARN("%s: unexpected end of section", sec->name);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001029 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001030 }
1031 }
1032
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001033 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001034}
1035
1036static bool is_gcov_insn(struct instruction *insn)
1037{
1038 struct rela *rela;
1039 struct section *sec;
1040 struct symbol *sym;
1041 unsigned long offset;
1042
1043 rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
1044 if (!rela)
1045 return false;
1046
1047 if (rela->sym->type != STT_SECTION)
1048 return false;
1049
1050 sec = rela->sym->sec;
1051 offset = rela->addend + insn->offset + insn->len - rela->offset;
1052
Josh Poimboeufa196e172016-03-09 00:06:57 -06001053 list_for_each_entry(sym, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001054 if (sym->type != STT_OBJECT)
1055 continue;
1056
1057 if (offset >= sym->offset && offset < sym->offset + sym->len)
1058 return (!memcmp(sym->name, "__gcov0.", 8));
1059 }
1060
1061 return false;
1062}
1063
1064static bool is_kasan_insn(struct instruction *insn)
1065{
1066 return (insn->type == INSN_CALL &&
1067 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1068}
1069
1070static bool is_ubsan_insn(struct instruction *insn)
1071{
1072 return (insn->type == INSN_CALL &&
1073 !strcmp(insn->call_dest->name,
1074 "__ubsan_handle_builtin_unreachable"));
1075}
1076
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001077static bool ignore_unreachable_insn(struct symbol *func,
1078 struct instruction *insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001079{
1080 int i;
1081
1082 if (insn->type == INSN_NOP)
1083 return true;
1084
1085 if (is_gcov_insn(insn))
1086 return true;
1087
1088 /*
1089 * Check if this (or a subsequent) instruction is related to
1090 * CONFIG_UBSAN or CONFIG_KASAN.
1091 *
1092 * End the search at 5 instructions to avoid going into the weeds.
1093 */
1094 for (i = 0; i < 5; i++) {
1095
1096 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
1097 return true;
1098
1099 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
1100 insn = insn->jump_dest;
1101 continue;
1102 }
1103
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001104 if (insn->offset + insn->len >= func->offset + func->len)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001105 break;
1106 insn = list_next_entry(insn, list);
1107 }
1108
1109 return false;
1110}
1111
1112static int validate_functions(struct objtool_file *file)
1113{
1114 struct section *sec;
1115 struct symbol *func;
1116 struct instruction *insn;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001117 int ret, warnings = 0;
1118
1119 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -06001120 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001121 if (func->type != STT_FUNC)
1122 continue;
1123
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001124 insn = find_insn(file, sec, func->offset);
Josh Poimboeufb1547d32016-04-15 09:17:10 -05001125 if (!insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001126 continue;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001127
1128 ret = validate_branch(file, insn, 0);
1129 warnings += ret;
1130 }
1131 }
1132
1133 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -06001134 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001135 if (func->type != STT_FUNC)
1136 continue;
1137
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001138 func_for_each_insn(file, func, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001139 if (insn->visited)
1140 continue;
1141
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001142 insn->visited = true;
Josh Poimboeuf7e578442016-04-14 14:52:24 -05001143
1144 if (file->ignore_unreachables || warnings ||
1145 ignore_unreachable_insn(func, insn))
1146 continue;
1147
1148 WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset);
1149 warnings++;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001150 }
1151 }
1152 }
1153
1154 return warnings;
1155}
1156
1157static int validate_uncallable_instructions(struct objtool_file *file)
1158{
1159 struct instruction *insn;
1160 int warnings = 0;
1161
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001162 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001163 if (!insn->visited && insn->type == INSN_RETURN) {
1164 WARN_FUNC("return instruction outside of a callable function",
1165 insn->sec, insn->offset);
1166 warnings++;
1167 }
1168 }
1169
1170 return warnings;
1171}
1172
1173static void cleanup(struct objtool_file *file)
1174{
1175 struct instruction *insn, *tmpinsn;
1176 struct alternative *alt, *tmpalt;
1177
Josh Poimboeufa196e172016-03-09 00:06:57 -06001178 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001179 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
1180 list_del(&alt->list);
1181 free(alt);
1182 }
1183 list_del(&insn->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -06001184 hash_del(&insn->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001185 free(insn);
1186 }
1187 elf_close(file->elf);
1188}
1189
1190const char * const check_usage[] = {
1191 "objtool check [<options>] file.o",
1192 NULL,
1193};
1194
1195int cmd_check(int argc, const char **argv)
1196{
1197 struct objtool_file file;
1198 int ret, warnings = 0;
1199
1200 const struct option options[] = {
1201 OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"),
1202 OPT_END(),
1203 };
1204
1205 argc = parse_options(argc, argv, options, check_usage, 0);
1206
1207 if (argc != 1)
1208 usage_with_options(check_usage, options);
1209
1210 objname = argv[0];
1211
1212 file.elf = elf_open(objname);
1213 if (!file.elf) {
1214 fprintf(stderr, "error reading elf file %s\n", objname);
1215 return 1;
1216 }
1217
Josh Poimboeufa196e172016-03-09 00:06:57 -06001218 INIT_LIST_HEAD(&file.insn_list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -06001219 hash_init(file.insn_hash);
Josh Poimboeuf7e578442016-04-14 14:52:24 -05001220 file.whitelist = find_section_by_name(file.elf, "__func_stack_frame_non_standard");
1221 file.rodata = find_section_by_name(file.elf, ".rodata");
1222 file.ignore_unreachables = false;
Josh Poimboeufb1547d32016-04-15 09:17:10 -05001223 file.c_file = find_section_by_name(file.elf, ".comment");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001224
1225 ret = decode_sections(&file);
1226 if (ret < 0)
1227 goto out;
1228 warnings += ret;
1229
1230 ret = validate_functions(&file);
1231 if (ret < 0)
1232 goto out;
1233 warnings += ret;
1234
1235 ret = validate_uncallable_instructions(&file);
1236 if (ret < 0)
1237 goto out;
1238 warnings += ret;
1239
1240out:
1241 cleanup(&file);
1242
1243 /* ignore warnings for now until we get all the code cleaned up */
1244 if (ret || warnings)
1245 return 0;
1246 return 0;
1247}