blob: a688a857a7ae88ebde88548e83db19506f948744 [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;
Josh Poimboeuf4d8bd3e2018-01-11 21:46:24 +000054 bool alt_group, visited, ignore_alts;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060055 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
Josh Poimboeuf9cfffb12016-10-13 16:22:53 -0500100static bool gcov_enabled(struct objtool_file *file)
101{
102 struct section *sec;
103 struct symbol *sym;
104
105 list_for_each_entry(sec, &file->elf->sections, list)
106 list_for_each_entry(sym, &sec->symbol_list, list)
107 if (!strncmp(sym->name, "__gcov_.", 8))
108 return true;
109
110 return false;
111}
112
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600113#define for_each_insn(file, insn) \
Josh Poimboeufa196e172016-03-09 00:06:57 -0600114 list_for_each_entry(insn, &file->insn_list, list)
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600115
116#define func_for_each_insn(file, func, insn) \
117 for (insn = find_insn(file, func->sec, func->offset); \
Josh Poimboeufa196e172016-03-09 00:06:57 -0600118 insn && &insn->list != &file->insn_list && \
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600119 insn->sec == func->sec && \
120 insn->offset < func->offset + func->len; \
121 insn = list_next_entry(insn, list))
122
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500123#define func_for_each_insn_continue_reverse(file, func, insn) \
124 for (insn = list_prev_entry(insn, list); \
125 &insn->list != &file->insn_list && \
126 insn->sec == func->sec && insn->offset >= func->offset; \
127 insn = list_prev_entry(insn, list))
128
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600129#define sec_for_each_insn_from(file, insn) \
130 for (; insn; insn = next_insn_same_sec(file, insn))
131
132
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600133/*
134 * Check if the function has been manually whitelisted with the
135 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
136 * due to its use of a context switching instruction.
137 */
138static bool ignore_func(struct objtool_file *file, struct symbol *func)
139{
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600140 struct rela *rela;
141 struct instruction *insn;
142
143 /* check for STACK_FRAME_NON_STANDARD */
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600144 if (file->whitelist && file->whitelist->rela)
Josh Poimboeuf0ea5ad82016-06-15 15:45:58 -0500145 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) {
146 if (rela->sym->type == STT_SECTION &&
147 rela->sym->sec == func->sec &&
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600148 rela->addend == func->offset)
149 return true;
Josh Poimboeuf0ea5ad82016-06-15 15:45:58 -0500150 if (rela->sym->type == STT_FUNC && rela->sym == func)
151 return true;
152 }
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600153
154 /* check if it has a context switching instruction */
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600155 func_for_each_insn(file, func, insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600156 if (insn->type == INSN_CONTEXT_SWITCH)
157 return true;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600158
159 return false;
160}
161
162/*
163 * This checks to see if the given function is a "noreturn" function.
164 *
165 * For global functions which are outside the scope of this object file, we
166 * have to keep a manual list of them.
167 *
168 * For local functions, we have to detect them manually by simply looking for
169 * the lack of a return instruction.
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600170 *
171 * Returns:
172 * -1: error
173 * 0: no dead end
174 * 1: dead end
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600175 */
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600176static int __dead_end_function(struct objtool_file *file, struct symbol *func,
177 int recursion)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600178{
179 int i;
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600180 struct instruction *insn;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600181 bool empty = true;
182
183 /*
184 * Unfortunately these have to be hard coded because the noreturn
185 * attribute isn't provided in ELF data.
186 */
187 static const char * const global_noreturns[] = {
188 "__stack_chk_fail",
189 "panic",
190 "do_exit",
Josh Poimboeufc1fad9e2016-09-22 16:21:25 -0500191 "do_task_dead",
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600192 "__module_put_and_exit",
193 "complete_and_exit",
194 "kvm_spurious_fault",
195 "__reiserfs_panic",
196 "lbug_with_loc"
197 };
198
199 if (func->bind == STB_WEAK)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600200 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600201
202 if (func->bind == STB_GLOBAL)
203 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
204 if (!strcmp(func->name, global_noreturns[i]))
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600205 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600206
207 if (!func->sec)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600208 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600209
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600210 func_for_each_insn(file, func, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600211 empty = false;
212
213 if (insn->type == INSN_RETURN)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600214 return 0;
Josh Poimboeuf81bfafc2016-03-09 00:06:51 -0600215 }
216
217 if (empty)
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600218 return 0;
Josh Poimboeuf81bfafc2016-03-09 00:06:51 -0600219
220 /*
221 * A function can have a sibling call instead of a return. In that
222 * case, the function's dead-end status depends on whether the target
223 * of the sibling call returns.
224 */
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600225 func_for_each_insn(file, func, insn) {
Josh Poimboeuf81bfafc2016-03-09 00:06:51 -0600226 if (insn->sec != func->sec ||
227 insn->offset >= func->offset + func->len)
228 break;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600229
230 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
231 struct instruction *dest = insn->jump_dest;
232 struct symbol *dest_func;
233
234 if (!dest)
235 /* sibling call to another file */
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600236 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600237
238 if (dest->sec != func->sec ||
239 dest->offset < func->offset ||
240 dest->offset >= func->offset + func->len) {
241 /* local sibling call */
242 dest_func = find_symbol_by_offset(dest->sec,
243 dest->offset);
244 if (!dest_func)
245 continue;
246
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600247 if (recursion == 5) {
248 WARN_FUNC("infinite recursion (objtool bug!)",
249 dest->sec, dest->offset);
250 return -1;
251 }
252
253 return __dead_end_function(file, dest_func,
254 recursion + 1);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600255 }
256 }
257
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500258 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600259 /* sibling call */
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600260 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600261 }
262
Josh Poimboeufb1e03242016-03-09 00:06:52 -0600263 return 1;
264}
265
266static int dead_end_function(struct objtool_file *file, struct symbol *func)
267{
268 return __dead_end_function(file, func, 0);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600269}
270
271/*
272 * Call the arch-specific instruction decoder for all the instructions and add
Josh Poimboeufa196e172016-03-09 00:06:57 -0600273 * them to the global instruction list.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600274 */
275static int decode_instructions(struct objtool_file *file)
276{
277 struct section *sec;
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500278 struct symbol *func;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600279 unsigned long offset;
280 struct instruction *insn;
281 int ret;
282
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600283 list_for_each_entry(sec, &file->elf->sections, list) {
284
285 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
286 continue;
287
288 for (offset = 0; offset < sec->len; offset += insn->len) {
289 insn = malloc(sizeof(*insn));
290 memset(insn, 0, sizeof(*insn));
291
292 INIT_LIST_HEAD(&insn->alts);
293 insn->sec = sec;
294 insn->offset = offset;
295
296 ret = arch_decode_instruction(file->elf, sec, offset,
297 sec->len - offset,
298 &insn->len, &insn->type,
299 &insn->immediate);
300 if (ret)
301 return ret;
302
303 if (!insn->type || insn->type > INSN_LAST) {
304 WARN_FUNC("invalid instruction type %d",
305 insn->sec, insn->offset, insn->type);
306 return -1;
307 }
308
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600309 hash_add(file->insn_hash, &insn->hash, insn->offset);
Josh Poimboeufa196e172016-03-09 00:06:57 -0600310 list_add_tail(&insn->list, &file->insn_list);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600311 }
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500312
313 list_for_each_entry(func, &sec->symbol_list, list) {
314 if (func->type != STT_FUNC)
315 continue;
316
317 if (!find_insn(file, sec, func->offset)) {
318 WARN("%s(): can't find starting instruction",
319 func->name);
320 return -1;
321 }
322
323 func_for_each_insn(file, func, insn)
324 if (!insn->func)
325 insn->func = func;
326 }
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600327 }
328
329 return 0;
330}
331
332/*
333 * Warnings shouldn't be reported for ignored functions.
334 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600335static void add_ignores(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600336{
337 struct instruction *insn;
338 struct section *sec;
339 struct symbol *func;
340
341 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -0600342 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600343 if (func->type != STT_FUNC)
344 continue;
345
346 if (!ignore_func(file, func))
347 continue;
348
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600349 func_for_each_insn(file, func, insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600350 insn->visited = true;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600351 }
352 }
353}
354
355/*
Josh Poimboeuf4d8bd3e2018-01-11 21:46:24 +0000356 * FIXME: For now, just ignore any alternatives which add retpolines. This is
357 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
358 * But it at least allows objtool to understand the control flow *around* the
359 * retpoline.
360 */
361static int add_nospec_ignores(struct objtool_file *file)
362{
363 struct section *sec;
364 struct rela *rela;
365 struct instruction *insn;
366
367 sec = find_section_by_name(file->elf, ".rela.discard.nospec");
368 if (!sec)
369 return 0;
370
371 list_for_each_entry(rela, &sec->rela_list, list) {
372 if (rela->sym->type != STT_SECTION) {
373 WARN("unexpected relocation symbol type in %s", sec->name);
374 return -1;
375 }
376
377 insn = find_insn(file, rela->sym->sec, rela->addend);
378 if (!insn) {
379 WARN("bad .discard.nospec entry");
380 return -1;
381 }
382
383 insn->ignore_alts = true;
384 }
385
386 return 0;
387}
388
389/*
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600390 * Find the destination instructions for all jumps.
391 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600392static int add_jump_destinations(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600393{
394 struct instruction *insn;
395 struct rela *rela;
396 struct section *dest_sec;
397 unsigned long dest_off;
398
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600399 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600400 if (insn->type != INSN_JUMP_CONDITIONAL &&
401 insn->type != INSN_JUMP_UNCONDITIONAL)
402 continue;
403
404 /* skip ignores */
405 if (insn->visited)
406 continue;
407
408 rela = find_rela_by_dest_range(insn->sec, insn->offset,
409 insn->len);
410 if (!rela) {
411 dest_sec = insn->sec;
412 dest_off = insn->offset + insn->len + insn->immediate;
413 } else if (rela->sym->type == STT_SECTION) {
414 dest_sec = rela->sym->sec;
415 dest_off = rela->addend + 4;
416 } else if (rela->sym->sec->idx) {
417 dest_sec = rela->sym->sec;
418 dest_off = rela->sym->sym.st_value + rela->addend + 4;
Josh Poimboeuf3adb52ab2018-01-11 21:46:23 +0000419 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
420 /*
421 * Retpoline jumps are really dynamic jumps in
422 * disguise, so convert them accordingly.
423 */
424 insn->type = INSN_JUMP_DYNAMIC;
425 continue;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600426 } else {
427 /* sibling call */
428 insn->jump_dest = 0;
429 continue;
430 }
431
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600432 insn->jump_dest = find_insn(file, dest_sec, dest_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600433 if (!insn->jump_dest) {
434
435 /*
436 * This is a special case where an alt instruction
437 * jumps past the end of the section. These are
438 * handled later in handle_group_alt().
439 */
440 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
441 continue;
442
443 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
444 insn->sec, insn->offset, dest_sec->name,
445 dest_off);
446 return -1;
447 }
448 }
449
450 return 0;
451}
452
453/*
454 * Find the destination instructions for all calls.
455 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600456static int add_call_destinations(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600457{
458 struct instruction *insn;
459 unsigned long dest_off;
460 struct rela *rela;
461
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600462 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600463 if (insn->type != INSN_CALL)
464 continue;
465
466 rela = find_rela_by_dest_range(insn->sec, insn->offset,
467 insn->len);
468 if (!rela) {
469 dest_off = insn->offset + insn->len + insn->immediate;
470 insn->call_dest = find_symbol_by_offset(insn->sec,
471 dest_off);
Josh Poimboeuf4d8bd3e2018-01-11 21:46:24 +0000472 /*
473 * FIXME: Thanks to retpolines, it's now considered
474 * normal for a function to call within itself. So
475 * disable this warning for now.
476 */
477#if 0
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600478 if (!insn->call_dest) {
479 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
480 insn->sec, insn->offset, dest_off);
481 return -1;
482 }
Josh Poimboeuf4d8bd3e2018-01-11 21:46:24 +0000483#endif
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600484 } else if (rela->sym->type == STT_SECTION) {
485 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
486 rela->addend+4);
487 if (!insn->call_dest ||
488 insn->call_dest->type != STT_FUNC) {
489 WARN_FUNC("can't find call dest symbol at %s+0x%x",
490 insn->sec, insn->offset,
491 rela->sym->sec->name,
492 rela->addend + 4);
493 return -1;
494 }
495 } else
496 insn->call_dest = rela->sym;
497 }
498
499 return 0;
500}
501
502/*
503 * The .alternatives section requires some extra special care, over and above
504 * what other special sections require:
505 *
506 * 1. Because alternatives are patched in-place, we need to insert a fake jump
507 * instruction at the end so that validate_branch() skips all the original
508 * replaced instructions when validating the new instruction path.
509 *
510 * 2. An added wrinkle is that the new instruction length might be zero. In
511 * that case the old instructions are replaced with noops. We simulate that
512 * by creating a fake jump as the only new instruction.
513 *
514 * 3. In some cases, the alternative section includes an instruction which
515 * conditionally jumps to the _end_ of the entry. We have to modify these
516 * jumps' destinations to point back to .text rather than the end of the
517 * entry in .altinstr_replacement.
518 *
519 * 4. It has been requested that we don't validate the !POPCNT feature path
520 * which is a "very very small percentage of machines".
521 */
522static int handle_group_alt(struct objtool_file *file,
523 struct special_alt *special_alt,
524 struct instruction *orig_insn,
525 struct instruction **new_insn)
526{
527 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump;
528 unsigned long dest_off;
529
530 last_orig_insn = NULL;
531 insn = orig_insn;
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600532 sec_for_each_insn_from(file, insn) {
533 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600534 break;
535
536 if (special_alt->skip_orig)
537 insn->type = INSN_NOP;
538
539 insn->alt_group = true;
540 last_orig_insn = insn;
541 }
542
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600543 if (!next_insn_same_sec(file, last_orig_insn)) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600544 WARN("%s: don't know how to handle alternatives at end of section",
545 special_alt->orig_sec->name);
546 return -1;
547 }
548
549 fake_jump = malloc(sizeof(*fake_jump));
550 if (!fake_jump) {
551 WARN("malloc failed");
552 return -1;
553 }
554 memset(fake_jump, 0, sizeof(*fake_jump));
555 INIT_LIST_HEAD(&fake_jump->alts);
556 fake_jump->sec = special_alt->new_sec;
557 fake_jump->offset = -1;
558 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
559 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
560
561 if (!special_alt->new_len) {
562 *new_insn = fake_jump;
563 return 0;
564 }
565
566 last_new_insn = NULL;
567 insn = *new_insn;
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600568 sec_for_each_insn_from(file, insn) {
569 if (insn->offset >= special_alt->new_off + special_alt->new_len)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600570 break;
571
572 last_new_insn = insn;
573
574 if (insn->type != INSN_JUMP_CONDITIONAL &&
575 insn->type != INSN_JUMP_UNCONDITIONAL)
576 continue;
577
578 if (!insn->immediate)
579 continue;
580
581 dest_off = insn->offset + insn->len + insn->immediate;
582 if (dest_off == special_alt->new_off + special_alt->new_len)
583 insn->jump_dest = fake_jump;
584
585 if (!insn->jump_dest) {
586 WARN_FUNC("can't find alternative jump destination",
587 insn->sec, insn->offset);
588 return -1;
589 }
590 }
591
592 if (!last_new_insn) {
593 WARN_FUNC("can't find last new alternative instruction",
594 special_alt->new_sec, special_alt->new_off);
595 return -1;
596 }
597
598 list_add(&fake_jump->list, &last_new_insn->list);
599
600 return 0;
601}
602
603/*
604 * A jump table entry can either convert a nop to a jump or a jump to a nop.
605 * If the original instruction is a jump, make the alt entry an effective nop
606 * by just skipping the original instruction.
607 */
608static int handle_jump_alt(struct objtool_file *file,
609 struct special_alt *special_alt,
610 struct instruction *orig_insn,
611 struct instruction **new_insn)
612{
613 if (orig_insn->type == INSN_NOP)
614 return 0;
615
616 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
617 WARN_FUNC("unsupported instruction at jump label",
618 orig_insn->sec, orig_insn->offset);
619 return -1;
620 }
621
622 *new_insn = list_next_entry(orig_insn, list);
623 return 0;
624}
625
626/*
627 * Read all the special sections which have alternate instructions which can be
628 * patched in or redirected to at runtime. Each instruction having alternate
629 * instruction(s) has them added to its insn->alts list, which will be
630 * traversed in validate_branch().
631 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600632static int add_special_section_alts(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600633{
634 struct list_head special_alts;
635 struct instruction *orig_insn, *new_insn;
636 struct special_alt *special_alt, *tmp;
637 struct alternative *alt;
638 int ret;
639
640 ret = special_get_alts(file->elf, &special_alts);
641 if (ret)
642 return ret;
643
644 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600645
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600646 orig_insn = find_insn(file, special_alt->orig_sec,
647 special_alt->orig_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600648 if (!orig_insn) {
649 WARN_FUNC("special: can't find orig instruction",
650 special_alt->orig_sec, special_alt->orig_off);
651 ret = -1;
652 goto out;
653 }
654
Josh Poimboeuf4d8bd3e2018-01-11 21:46:24 +0000655 /* Ignore retpoline alternatives. */
656 if (orig_insn->ignore_alts)
657 continue;
658
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600659 new_insn = NULL;
660 if (!special_alt->group || special_alt->new_len) {
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600661 new_insn = find_insn(file, special_alt->new_sec,
662 special_alt->new_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600663 if (!new_insn) {
664 WARN_FUNC("special: can't find new instruction",
665 special_alt->new_sec,
666 special_alt->new_off);
667 ret = -1;
668 goto out;
669 }
670 }
671
672 if (special_alt->group) {
673 ret = handle_group_alt(file, special_alt, orig_insn,
674 &new_insn);
675 if (ret)
676 goto out;
677 } else if (special_alt->jump_or_nop) {
678 ret = handle_jump_alt(file, special_alt, orig_insn,
679 &new_insn);
680 if (ret)
681 goto out;
682 }
683
Josh Poimboeuf4d8bd3e2018-01-11 21:46:24 +0000684 alt = malloc(sizeof(*alt));
685 if (!alt) {
686 WARN("malloc failed");
687 ret = -1;
688 goto out;
689 }
690
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600691 alt->insn = new_insn;
692 list_add_tail(&alt->list, &orig_insn->alts);
693
694 list_del(&special_alt->list);
695 free(special_alt);
696 }
697
698out:
699 return ret;
700}
701
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600702static int add_switch_table(struct objtool_file *file, struct symbol *func,
703 struct instruction *insn, struct rela *table,
704 struct rela *next_table)
705{
706 struct rela *rela = table;
707 struct instruction *alt_insn;
708 struct alternative *alt;
709
710 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
711 if (rela == next_table)
712 break;
713
714 if (rela->sym->sec != insn->sec ||
715 rela->addend <= func->offset ||
716 rela->addend >= func->offset + func->len)
717 break;
718
719 alt_insn = find_insn(file, insn->sec, rela->addend);
720 if (!alt_insn) {
721 WARN("%s: can't find instruction at %s+0x%x",
722 file->rodata->rela->name, insn->sec->name,
723 rela->addend);
724 return -1;
725 }
726
727 alt = malloc(sizeof(*alt));
728 if (!alt) {
729 WARN("malloc failed");
730 return -1;
731 }
732
733 alt->insn = alt_insn;
734 list_add_tail(&alt->list, &insn->alts);
735 }
736
737 return 0;
738}
739
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500740/*
741 * find_switch_table() - Given a dynamic jump, find the switch jump table in
742 * .rodata associated with it.
743 *
744 * There are 3 basic patterns:
745 *
746 * 1. jmpq *[rodata addr](,%reg,8)
747 *
748 * This is the most common case by far. It jumps to an address in a simple
749 * jump table which is stored in .rodata.
750 *
751 * 2. jmpq *[rodata addr](%rip)
752 *
753 * This is caused by a rare GCC quirk, currently only seen in three driver
754 * functions in the kernel, only with certain obscure non-distro configs.
755 *
756 * As part of an optimization, GCC makes a copy of an existing switch jump
757 * table, modifies it, and then hard-codes the jump (albeit with an indirect
758 * jump) to use a single entry in the table. The rest of the jump table and
759 * some of its jump targets remain as dead code.
760 *
761 * In such a case we can just crudely ignore all unreachable instruction
762 * warnings for the entire object file. Ideally we would just ignore them
763 * for the function, but that would require redesigning the code quite a
764 * bit. And honestly that's just not worth doing: unreachable instruction
765 * warnings are of questionable value anyway, and this is such a rare issue.
766 *
767 * 3. mov [rodata addr],%reg1
768 * ... some instructions ...
769 * jmpq *(%reg1,%reg2,8)
770 *
771 * This is a fairly uncommon pattern which is new for GCC 6. As of this
772 * writing, there are 11 occurrences of it in the allmodconfig kernel.
773 *
774 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
775 * ensure the same register is used in the mov and jump instructions.
776 */
777static struct rela *find_switch_table(struct objtool_file *file,
778 struct symbol *func,
779 struct instruction *insn)
780{
781 struct rela *text_rela, *rodata_rela;
Josh Poimboeuf37327102016-10-13 16:22:52 -0500782 struct instruction *orig_insn = insn;
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500783
784 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
785 if (text_rela && text_rela->sym == file->rodata->sym) {
786 /* case 1 */
787 rodata_rela = find_rela_by_dest(file->rodata,
788 text_rela->addend);
789 if (rodata_rela)
790 return rodata_rela;
791
792 /* case 2 */
793 rodata_rela = find_rela_by_dest(file->rodata,
794 text_rela->addend + 4);
795 if (!rodata_rela)
796 return NULL;
797 file->ignore_unreachables = true;
798 return rodata_rela;
799 }
800
801 /* case 3 */
802 func_for_each_insn_continue_reverse(file, func, insn) {
Josh Poimboeuf37327102016-10-13 16:22:52 -0500803 if (insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500804 break;
805
Josh Poimboeuf37327102016-10-13 16:22:52 -0500806 /* allow small jumps within the range */
807 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
808 insn->jump_dest &&
809 (insn->jump_dest->offset <= insn->offset ||
Josh Poimboeuf56fb2d62016-10-26 10:34:08 -0500810 insn->jump_dest->offset > orig_insn->offset))
Josh Poimboeuf37327102016-10-13 16:22:52 -0500811 break;
812
Josh Poimboeuf3e51ccb2017-03-02 16:57:23 -0600813 /* look for a relocation which references .rodata */
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500814 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
815 insn->len);
Josh Poimboeuf3e51ccb2017-03-02 16:57:23 -0600816 if (!text_rela || text_rela->sym != file->rodata->sym)
817 continue;
818
819 /*
820 * Make sure the .rodata address isn't associated with a
821 * symbol. gcc jump tables are anonymous data.
822 */
823 if (find_symbol_containing(file->rodata, text_rela->addend))
824 continue;
825
826 return find_rela_by_dest(file->rodata, text_rela->addend);
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500827 }
828
829 return NULL;
830}
831
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600832static int add_func_switch_tables(struct objtool_file *file,
833 struct symbol *func)
834{
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500835 struct instruction *insn, *prev_jump = NULL;
836 struct rela *rela, *prev_rela = NULL;
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600837 int ret;
838
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600839 func_for_each_insn(file, func, insn) {
840 if (insn->type != INSN_JUMP_DYNAMIC)
841 continue;
842
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500843 rela = find_switch_table(file, func, insn);
844 if (!rela)
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600845 continue;
846
847 /*
848 * We found a switch table, but we don't know yet how big it
849 * is. Don't add it until we reach the end of the function or
850 * the beginning of another switch table in the same function.
851 */
852 if (prev_jump) {
853 ret = add_switch_table(file, func, prev_jump, prev_rela,
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500854 rela);
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600855 if (ret)
856 return ret;
857 }
858
859 prev_jump = insn;
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500860 prev_rela = rela;
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600861 }
862
863 if (prev_jump) {
864 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
865 if (ret)
866 return ret;
867 }
868
869 return 0;
870}
871
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600872/*
873 * For some switch statements, gcc generates a jump table in the .rodata
874 * section which contains a list of addresses within the function to jump to.
875 * This finds these jump tables and adds them to the insn->alts lists.
876 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600877static int add_switch_table_alts(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600878{
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600879 struct section *sec;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600880 struct symbol *func;
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600881 int ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600882
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600883 if (!file->rodata || !file->rodata->rela)
884 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600885
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600886 list_for_each_entry(sec, &file->elf->sections, list) {
887 list_for_each_entry(func, &sec->symbol_list, list) {
888 if (func->type != STT_FUNC)
889 continue;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600890
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600891 ret = add_func_switch_tables(file, func);
892 if (ret)
893 return ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600894 }
895 }
896
897 return 0;
898}
899
900static int decode_sections(struct objtool_file *file)
901{
902 int ret;
903
904 ret = decode_instructions(file);
905 if (ret)
906 return ret;
907
Josh Poimboeufa196e172016-03-09 00:06:57 -0600908 add_ignores(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600909
Josh Poimboeuf4d8bd3e2018-01-11 21:46:24 +0000910 ret = add_nospec_ignores(file);
911 if (ret)
912 return ret;
913
Josh Poimboeufa196e172016-03-09 00:06:57 -0600914 ret = add_jump_destinations(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600915 if (ret)
916 return ret;
917
Josh Poimboeufa196e172016-03-09 00:06:57 -0600918 ret = add_call_destinations(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600919 if (ret)
920 return ret;
921
Josh Poimboeufa196e172016-03-09 00:06:57 -0600922 ret = add_special_section_alts(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600923 if (ret)
924 return ret;
925
Josh Poimboeufa196e172016-03-09 00:06:57 -0600926 ret = add_switch_table_alts(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600927 if (ret)
928 return ret;
929
930 return 0;
931}
932
933static bool is_fentry_call(struct instruction *insn)
934{
935 if (insn->type == INSN_CALL &&
936 insn->call_dest->type == STT_NOTYPE &&
937 !strcmp(insn->call_dest->name, "__fentry__"))
938 return true;
939
940 return false;
941}
942
943static bool has_modified_stack_frame(struct instruction *insn)
944{
945 return (insn->state & STATE_FP_SAVED) ||
946 (insn->state & STATE_FP_SETUP);
947}
948
949static bool has_valid_stack_frame(struct instruction *insn)
950{
951 return (insn->state & STATE_FP_SAVED) &&
952 (insn->state & STATE_FP_SETUP);
953}
954
Josh Poimboeufd8d1b2c2016-03-09 00:06:54 -0600955static unsigned int frame_state(unsigned long state)
956{
957 return (state & (STATE_FP_SAVED | STATE_FP_SETUP));
958}
959
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600960/*
961 * Follow the branch starting at the given instruction, and recursively follow
962 * any other branches (jumps). Meanwhile, track the frame pointer state at
963 * each instruction and validate all the rules described in
964 * tools/objtool/Documentation/stack-validation.txt.
965 */
966static int validate_branch(struct objtool_file *file,
967 struct instruction *first, unsigned char first_state)
968{
969 struct alternative *alt;
970 struct instruction *insn;
971 struct section *sec;
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500972 struct symbol *func = NULL;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600973 unsigned char state;
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600974 int ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600975
976 insn = first;
977 sec = insn->sec;
978 state = first_state;
979
980 if (insn->alt_group && list_empty(&insn->alts)) {
981 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
982 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600983 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600984 }
985
986 while (1) {
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500987 if (file->c_file && insn->func) {
988 if (func && func != insn->func) {
989 WARN("%s() falls through to next function %s()",
990 func->name, insn->func->name);
991 return 1;
992 }
993
994 func = insn->func;
995 }
996
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600997 if (insn->visited) {
Josh Poimboeufd8d1b2c2016-03-09 00:06:54 -0600998 if (frame_state(insn->state) != frame_state(state)) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600999 WARN_FUNC("frame pointer state mismatch",
1000 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001001 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001002 }
1003
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001004 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001005 }
1006
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001007 insn->visited = true;
1008 insn->state = state;
1009
1010 list_for_each_entry(alt, &insn->alts, list) {
1011 ret = validate_branch(file, alt->insn, state);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001012 if (ret)
1013 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001014 }
1015
1016 switch (insn->type) {
1017
1018 case INSN_FP_SAVE:
1019 if (!nofp) {
1020 if (state & STATE_FP_SAVED) {
1021 WARN_FUNC("duplicate frame pointer save",
1022 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001023 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001024 }
1025 state |= STATE_FP_SAVED;
1026 }
1027 break;
1028
1029 case INSN_FP_SETUP:
1030 if (!nofp) {
1031 if (state & STATE_FP_SETUP) {
1032 WARN_FUNC("duplicate frame pointer setup",
1033 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001034 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001035 }
1036 state |= STATE_FP_SETUP;
1037 }
1038 break;
1039
1040 case INSN_FP_RESTORE:
1041 if (!nofp) {
1042 if (has_valid_stack_frame(insn))
1043 state &= ~STATE_FP_SETUP;
1044
1045 state &= ~STATE_FP_SAVED;
1046 }
1047 break;
1048
1049 case INSN_RETURN:
1050 if (!nofp && has_modified_stack_frame(insn)) {
1051 WARN_FUNC("return without frame pointer restore",
1052 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001053 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001054 }
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001055 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001056
1057 case INSN_CALL:
1058 if (is_fentry_call(insn)) {
1059 state |= STATE_FENTRY;
1060 break;
1061 }
1062
Josh Poimboeufb1e03242016-03-09 00:06:52 -06001063 ret = dead_end_function(file, insn->call_dest);
1064 if (ret == 1)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001065 return 0;
Josh Poimboeufb1e03242016-03-09 00:06:52 -06001066 if (ret == -1)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001067 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001068
1069 /* fallthrough */
1070 case INSN_CALL_DYNAMIC:
1071 if (!nofp && !has_valid_stack_frame(insn)) {
1072 WARN_FUNC("call without frame pointer save/setup",
1073 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001074 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001075 }
1076 break;
1077
1078 case INSN_JUMP_CONDITIONAL:
1079 case INSN_JUMP_UNCONDITIONAL:
1080 if (insn->jump_dest) {
1081 ret = validate_branch(file, insn->jump_dest,
1082 state);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001083 if (ret)
1084 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001085 } else if (has_modified_stack_frame(insn)) {
1086 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
1087 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001088 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001089 } /* else it's a sibling call */
1090
1091 if (insn->type == INSN_JUMP_UNCONDITIONAL)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001092 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001093
1094 break;
1095
1096 case INSN_JUMP_DYNAMIC:
1097 if (list_empty(&insn->alts) &&
1098 has_modified_stack_frame(insn)) {
1099 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
1100 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001101 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001102 }
1103
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001104 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001105
1106 case INSN_BUG:
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001107 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001108
1109 default:
1110 break;
1111 }
1112
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001113 insn = next_insn_same_sec(file, insn);
1114 if (!insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001115 WARN("%s: unexpected end of section", sec->name);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001116 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001117 }
1118 }
1119
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001120 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001121}
1122
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001123static bool is_kasan_insn(struct instruction *insn)
1124{
1125 return (insn->type == INSN_CALL &&
1126 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1127}
1128
1129static bool is_ubsan_insn(struct instruction *insn)
1130{
1131 return (insn->type == INSN_CALL &&
1132 !strcmp(insn->call_dest->name,
1133 "__ubsan_handle_builtin_unreachable"));
1134}
1135
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001136static bool ignore_unreachable_insn(struct symbol *func,
1137 struct instruction *insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001138{
1139 int i;
1140
1141 if (insn->type == INSN_NOP)
1142 return true;
1143
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001144 /*
1145 * Check if this (or a subsequent) instruction is related to
1146 * CONFIG_UBSAN or CONFIG_KASAN.
1147 *
1148 * End the search at 5 instructions to avoid going into the weeds.
1149 */
1150 for (i = 0; i < 5; i++) {
1151
1152 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
1153 return true;
1154
1155 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
1156 insn = insn->jump_dest;
1157 continue;
1158 }
1159
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001160 if (insn->offset + insn->len >= func->offset + func->len)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001161 break;
1162 insn = list_next_entry(insn, list);
1163 }
1164
1165 return false;
1166}
1167
1168static int validate_functions(struct objtool_file *file)
1169{
1170 struct section *sec;
1171 struct symbol *func;
1172 struct instruction *insn;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001173 int ret, warnings = 0;
1174
1175 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -06001176 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001177 if (func->type != STT_FUNC)
1178 continue;
1179
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001180 insn = find_insn(file, sec, func->offset);
Josh Poimboeufb1547d32016-04-15 09:17:10 -05001181 if (!insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001182 continue;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001183
1184 ret = validate_branch(file, insn, 0);
1185 warnings += ret;
1186 }
1187 }
1188
1189 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -06001190 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001191 if (func->type != STT_FUNC)
1192 continue;
1193
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001194 func_for_each_insn(file, func, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001195 if (insn->visited)
1196 continue;
1197
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001198 insn->visited = true;
Josh Poimboeuf7e578442016-04-14 14:52:24 -05001199
1200 if (file->ignore_unreachables || warnings ||
1201 ignore_unreachable_insn(func, insn))
1202 continue;
1203
Josh Poimboeuf9cfffb12016-10-13 16:22:53 -05001204 /*
1205 * gcov produces a lot of unreachable
1206 * instructions. If we get an unreachable
1207 * warning and the file has gcov enabled, just
1208 * ignore it, and all other such warnings for
1209 * the file.
1210 */
1211 if (!file->ignore_unreachables &&
1212 gcov_enabled(file)) {
1213 file->ignore_unreachables = true;
1214 continue;
1215 }
1216
Josh Poimboeuf7e578442016-04-14 14:52:24 -05001217 WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset);
1218 warnings++;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001219 }
1220 }
1221 }
1222
1223 return warnings;
1224}
1225
1226static int validate_uncallable_instructions(struct objtool_file *file)
1227{
1228 struct instruction *insn;
1229 int warnings = 0;
1230
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001231 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001232 if (!insn->visited && insn->type == INSN_RETURN) {
Josh Poimboeuf92e8f202018-01-15 11:00:54 -06001233
1234 /*
1235 * Don't warn about call instructions in unvisited
1236 * retpoline alternatives.
1237 */
1238 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1239 continue;
1240
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001241 WARN_FUNC("return instruction outside of a callable function",
1242 insn->sec, insn->offset);
1243 warnings++;
1244 }
1245 }
1246
1247 return warnings;
1248}
1249
1250static void cleanup(struct objtool_file *file)
1251{
1252 struct instruction *insn, *tmpinsn;
1253 struct alternative *alt, *tmpalt;
1254
Josh Poimboeufa196e172016-03-09 00:06:57 -06001255 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001256 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
1257 list_del(&alt->list);
1258 free(alt);
1259 }
1260 list_del(&insn->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -06001261 hash_del(&insn->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001262 free(insn);
1263 }
1264 elf_close(file->elf);
1265}
1266
1267const char * const check_usage[] = {
1268 "objtool check [<options>] file.o",
1269 NULL,
1270};
1271
1272int cmd_check(int argc, const char **argv)
1273{
1274 struct objtool_file file;
1275 int ret, warnings = 0;
1276
1277 const struct option options[] = {
1278 OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"),
1279 OPT_END(),
1280 };
1281
1282 argc = parse_options(argc, argv, options, check_usage, 0);
1283
1284 if (argc != 1)
1285 usage_with_options(check_usage, options);
1286
1287 objname = argv[0];
1288
1289 file.elf = elf_open(objname);
1290 if (!file.elf) {
1291 fprintf(stderr, "error reading elf file %s\n", objname);
1292 return 1;
1293 }
1294
Josh Poimboeufa196e172016-03-09 00:06:57 -06001295 INIT_LIST_HEAD(&file.insn_list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -06001296 hash_init(file.insn_hash);
Josh Poimboeuf35aee622017-03-01 12:04:44 -06001297 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
Josh Poimboeuf7e578442016-04-14 14:52:24 -05001298 file.rodata = find_section_by_name(file.elf, ".rodata");
1299 file.ignore_unreachables = false;
Josh Poimboeufb1547d32016-04-15 09:17:10 -05001300 file.c_file = find_section_by_name(file.elf, ".comment");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001301
1302 ret = decode_sections(&file);
1303 if (ret < 0)
1304 goto out;
1305 warnings += ret;
1306
1307 ret = validate_functions(&file);
1308 if (ret < 0)
1309 goto out;
1310 warnings += ret;
1311
1312 ret = validate_uncallable_instructions(&file);
1313 if (ret < 0)
1314 goto out;
1315 warnings += ret;
1316
1317out:
1318 cleanup(&file);
1319
1320 /* ignore warnings for now until we get all the code cleaned up */
1321 if (ret || warnings)
1322 return 0;
1323 return 0;
1324}