blob: 066086dd59a8017e293993a50d2f432d47441cfe [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 Poimboeufd1091c72017-02-21 15:35:32 -060054 bool alt_group, visited, dead_end;
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/*
Josh Poimboeufd1091c72017-02-21 15:35:32 -0600333 * Find all uses of the unreachable() macro, which are code path dead ends.
334 */
335static int add_dead_ends(struct objtool_file *file)
336{
337 struct section *sec;
338 struct rela *rela;
339 struct instruction *insn;
340 bool found;
341
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600342 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
Josh Poimboeufd1091c72017-02-21 15:35:32 -0600343 if (!sec)
344 return 0;
345
346 list_for_each_entry(rela, &sec->rela_list, list) {
347 if (rela->sym->type != STT_SECTION) {
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600348 WARN("unexpected relocation symbol type in %s", sec->name);
Josh Poimboeufd1091c72017-02-21 15:35:32 -0600349 return -1;
350 }
351 insn = find_insn(file, rela->sym->sec, rela->addend);
352 if (insn)
353 insn = list_prev_entry(insn, list);
354 else if (rela->addend == rela->sym->sec->len) {
355 found = false;
356 list_for_each_entry_reverse(insn, &file->insn_list, list) {
357 if (insn->sec == rela->sym->sec) {
358 found = true;
359 break;
360 }
361 }
362
363 if (!found) {
364 WARN("can't find unreachable insn at %s+0x%x",
365 rela->sym->sec->name, rela->addend);
366 return -1;
367 }
368 } else {
369 WARN("can't find unreachable insn at %s+0x%x",
370 rela->sym->sec->name, rela->addend);
371 return -1;
372 }
373
374 insn->dead_end = true;
375 }
376
377 return 0;
378}
379
380/*
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600381 * Warnings shouldn't be reported for ignored functions.
382 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600383static void add_ignores(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600384{
385 struct instruction *insn;
386 struct section *sec;
387 struct symbol *func;
388
389 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -0600390 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600391 if (func->type != STT_FUNC)
392 continue;
393
394 if (!ignore_func(file, func))
395 continue;
396
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600397 func_for_each_insn(file, func, insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600398 insn->visited = true;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600399 }
400 }
401}
402
403/*
404 * Find the destination instructions for all jumps.
405 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600406static int add_jump_destinations(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600407{
408 struct instruction *insn;
409 struct rela *rela;
410 struct section *dest_sec;
411 unsigned long dest_off;
412
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600413 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600414 if (insn->type != INSN_JUMP_CONDITIONAL &&
415 insn->type != INSN_JUMP_UNCONDITIONAL)
416 continue;
417
418 /* skip ignores */
419 if (insn->visited)
420 continue;
421
422 rela = find_rela_by_dest_range(insn->sec, insn->offset,
423 insn->len);
424 if (!rela) {
425 dest_sec = insn->sec;
426 dest_off = insn->offset + insn->len + insn->immediate;
427 } else if (rela->sym->type == STT_SECTION) {
428 dest_sec = rela->sym->sec;
429 dest_off = rela->addend + 4;
430 } else if (rela->sym->sec->idx) {
431 dest_sec = rela->sym->sec;
432 dest_off = rela->sym->sym.st_value + rela->addend + 4;
433 } else {
434 /* sibling call */
435 insn->jump_dest = 0;
436 continue;
437 }
438
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600439 insn->jump_dest = find_insn(file, dest_sec, dest_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600440 if (!insn->jump_dest) {
441
442 /*
443 * This is a special case where an alt instruction
444 * jumps past the end of the section. These are
445 * handled later in handle_group_alt().
446 */
447 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
448 continue;
449
450 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
451 insn->sec, insn->offset, dest_sec->name,
452 dest_off);
453 return -1;
454 }
455 }
456
457 return 0;
458}
459
460/*
461 * Find the destination instructions for all calls.
462 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600463static int add_call_destinations(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600464{
465 struct instruction *insn;
466 unsigned long dest_off;
467 struct rela *rela;
468
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600469 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600470 if (insn->type != INSN_CALL)
471 continue;
472
473 rela = find_rela_by_dest_range(insn->sec, insn->offset,
474 insn->len);
475 if (!rela) {
476 dest_off = insn->offset + insn->len + insn->immediate;
477 insn->call_dest = find_symbol_by_offset(insn->sec,
478 dest_off);
479 if (!insn->call_dest) {
480 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
481 insn->sec, insn->offset, dest_off);
482 return -1;
483 }
484 } 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) {
645 alt = malloc(sizeof(*alt));
646 if (!alt) {
647 WARN("malloc failed");
648 ret = -1;
649 goto out;
650 }
651
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600652 orig_insn = find_insn(file, special_alt->orig_sec,
653 special_alt->orig_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600654 if (!orig_insn) {
655 WARN_FUNC("special: can't find orig instruction",
656 special_alt->orig_sec, special_alt->orig_off);
657 ret = -1;
658 goto out;
659 }
660
661 new_insn = NULL;
662 if (!special_alt->group || special_alt->new_len) {
Josh Poimboeuf74aec052016-03-09 00:06:55 -0600663 new_insn = find_insn(file, special_alt->new_sec,
664 special_alt->new_off);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600665 if (!new_insn) {
666 WARN_FUNC("special: can't find new instruction",
667 special_alt->new_sec,
668 special_alt->new_off);
669 ret = -1;
670 goto out;
671 }
672 }
673
674 if (special_alt->group) {
675 ret = handle_group_alt(file, special_alt, orig_insn,
676 &new_insn);
677 if (ret)
678 goto out;
679 } else if (special_alt->jump_or_nop) {
680 ret = handle_jump_alt(file, special_alt, orig_insn,
681 &new_insn);
682 if (ret)
683 goto out;
684 }
685
686 alt->insn = new_insn;
687 list_add_tail(&alt->list, &orig_insn->alts);
688
689 list_del(&special_alt->list);
690 free(special_alt);
691 }
692
693out:
694 return ret;
695}
696
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600697static int add_switch_table(struct objtool_file *file, struct symbol *func,
698 struct instruction *insn, struct rela *table,
699 struct rela *next_table)
700{
701 struct rela *rela = table;
702 struct instruction *alt_insn;
703 struct alternative *alt;
704
705 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
706 if (rela == next_table)
707 break;
708
709 if (rela->sym->sec != insn->sec ||
710 rela->addend <= func->offset ||
711 rela->addend >= func->offset + func->len)
712 break;
713
714 alt_insn = find_insn(file, insn->sec, rela->addend);
715 if (!alt_insn) {
716 WARN("%s: can't find instruction at %s+0x%x",
717 file->rodata->rela->name, insn->sec->name,
718 rela->addend);
719 return -1;
720 }
721
722 alt = malloc(sizeof(*alt));
723 if (!alt) {
724 WARN("malloc failed");
725 return -1;
726 }
727
728 alt->insn = alt_insn;
729 list_add_tail(&alt->list, &insn->alts);
730 }
731
732 return 0;
733}
734
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500735/*
736 * find_switch_table() - Given a dynamic jump, find the switch jump table in
737 * .rodata associated with it.
738 *
739 * There are 3 basic patterns:
740 *
741 * 1. jmpq *[rodata addr](,%reg,8)
742 *
743 * This is the most common case by far. It jumps to an address in a simple
744 * jump table which is stored in .rodata.
745 *
746 * 2. jmpq *[rodata addr](%rip)
747 *
748 * This is caused by a rare GCC quirk, currently only seen in three driver
749 * functions in the kernel, only with certain obscure non-distro configs.
750 *
751 * As part of an optimization, GCC makes a copy of an existing switch jump
752 * table, modifies it, and then hard-codes the jump (albeit with an indirect
753 * jump) to use a single entry in the table. The rest of the jump table and
754 * some of its jump targets remain as dead code.
755 *
756 * In such a case we can just crudely ignore all unreachable instruction
757 * warnings for the entire object file. Ideally we would just ignore them
758 * for the function, but that would require redesigning the code quite a
759 * bit. And honestly that's just not worth doing: unreachable instruction
760 * warnings are of questionable value anyway, and this is such a rare issue.
761 *
762 * 3. mov [rodata addr],%reg1
763 * ... some instructions ...
764 * jmpq *(%reg1,%reg2,8)
765 *
766 * This is a fairly uncommon pattern which is new for GCC 6. As of this
767 * writing, there are 11 occurrences of it in the allmodconfig kernel.
768 *
769 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
770 * ensure the same register is used in the mov and jump instructions.
771 */
772static struct rela *find_switch_table(struct objtool_file *file,
773 struct symbol *func,
774 struct instruction *insn)
775{
776 struct rela *text_rela, *rodata_rela;
Josh Poimboeuf37327102016-10-13 16:22:52 -0500777 struct instruction *orig_insn = insn;
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500778
779 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
780 if (text_rela && text_rela->sym == file->rodata->sym) {
781 /* case 1 */
782 rodata_rela = find_rela_by_dest(file->rodata,
783 text_rela->addend);
784 if (rodata_rela)
785 return rodata_rela;
786
787 /* case 2 */
788 rodata_rela = find_rela_by_dest(file->rodata,
789 text_rela->addend + 4);
790 if (!rodata_rela)
791 return NULL;
792 file->ignore_unreachables = true;
793 return rodata_rela;
794 }
795
796 /* case 3 */
797 func_for_each_insn_continue_reverse(file, func, insn) {
Josh Poimboeuf37327102016-10-13 16:22:52 -0500798 if (insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500799 break;
800
Josh Poimboeuf37327102016-10-13 16:22:52 -0500801 /* allow small jumps within the range */
802 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
803 insn->jump_dest &&
804 (insn->jump_dest->offset <= insn->offset ||
Josh Poimboeuf56fb2d62016-10-26 10:34:08 -0500805 insn->jump_dest->offset > orig_insn->offset))
Josh Poimboeuf37327102016-10-13 16:22:52 -0500806 break;
807
Josh Poimboeuf5c51f4a2017-03-02 16:57:23 -0600808 /* look for a relocation which references .rodata */
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500809 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
810 insn->len);
Josh Poimboeuf5c51f4a2017-03-02 16:57:23 -0600811 if (!text_rela || text_rela->sym != file->rodata->sym)
812 continue;
813
814 /*
815 * Make sure the .rodata address isn't associated with a
816 * symbol. gcc jump tables are anonymous data.
817 */
818 if (find_symbol_containing(file->rodata, text_rela->addend))
819 continue;
820
821 return find_rela_by_dest(file->rodata, text_rela->addend);
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500822 }
823
824 return NULL;
825}
826
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600827static int add_func_switch_tables(struct objtool_file *file,
828 struct symbol *func)
829{
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500830 struct instruction *insn, *prev_jump = NULL;
831 struct rela *rela, *prev_rela = NULL;
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600832 int ret;
833
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600834 func_for_each_insn(file, func, insn) {
835 if (insn->type != INSN_JUMP_DYNAMIC)
836 continue;
837
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500838 rela = find_switch_table(file, func, insn);
839 if (!rela)
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600840 continue;
841
842 /*
843 * We found a switch table, but we don't know yet how big it
844 * is. Don't add it until we reach the end of the function or
845 * the beginning of another switch table in the same function.
846 */
847 if (prev_jump) {
848 ret = add_switch_table(file, func, prev_jump, prev_rela,
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500849 rela);
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600850 if (ret)
851 return ret;
852 }
853
854 prev_jump = insn;
Josh Poimboeuf6d01f282016-07-28 19:14:58 -0500855 prev_rela = rela;
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600856 }
857
858 if (prev_jump) {
859 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
860 if (ret)
861 return ret;
862 }
863
864 return 0;
865}
866
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600867/*
868 * For some switch statements, gcc generates a jump table in the .rodata
869 * section which contains a list of addresses within the function to jump to.
870 * This finds these jump tables and adds them to the insn->alts lists.
871 */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600872static int add_switch_table_alts(struct objtool_file *file)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600873{
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600874 struct section *sec;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600875 struct symbol *func;
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600876 int ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600877
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600878 if (!file->rodata || !file->rodata->rela)
879 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600880
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600881 list_for_each_entry(sec, &file->elf->sections, list) {
882 list_for_each_entry(func, &sec->symbol_list, list) {
883 if (func->type != STT_FUNC)
884 continue;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600885
Josh Poimboeuf8133fbb2016-03-09 00:06:58 -0600886 ret = add_func_switch_tables(file, func);
887 if (ret)
888 return ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600889 }
890 }
891
892 return 0;
893}
894
895static int decode_sections(struct objtool_file *file)
896{
897 int ret;
898
899 ret = decode_instructions(file);
900 if (ret)
901 return ret;
902
Josh Poimboeufd1091c72017-02-21 15:35:32 -0600903 ret = add_dead_ends(file);
904 if (ret)
905 return ret;
906
Josh Poimboeufa196e172016-03-09 00:06:57 -0600907 add_ignores(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600908
Josh Poimboeufa196e172016-03-09 00:06:57 -0600909 ret = add_jump_destinations(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600910 if (ret)
911 return ret;
912
Josh Poimboeufa196e172016-03-09 00:06:57 -0600913 ret = add_call_destinations(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600914 if (ret)
915 return ret;
916
Josh Poimboeufa196e172016-03-09 00:06:57 -0600917 ret = add_special_section_alts(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600918 if (ret)
919 return ret;
920
Josh Poimboeufa196e172016-03-09 00:06:57 -0600921 ret = add_switch_table_alts(file);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600922 if (ret)
923 return ret;
924
925 return 0;
926}
927
928static bool is_fentry_call(struct instruction *insn)
929{
930 if (insn->type == INSN_CALL &&
931 insn->call_dest->type == STT_NOTYPE &&
932 !strcmp(insn->call_dest->name, "__fentry__"))
933 return true;
934
935 return false;
936}
937
938static bool has_modified_stack_frame(struct instruction *insn)
939{
940 return (insn->state & STATE_FP_SAVED) ||
941 (insn->state & STATE_FP_SETUP);
942}
943
944static bool has_valid_stack_frame(struct instruction *insn)
945{
946 return (insn->state & STATE_FP_SAVED) &&
947 (insn->state & STATE_FP_SETUP);
948}
949
Josh Poimboeufd8d1b2c2016-03-09 00:06:54 -0600950static unsigned int frame_state(unsigned long state)
951{
952 return (state & (STATE_FP_SAVED | STATE_FP_SETUP));
953}
954
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600955/*
956 * Follow the branch starting at the given instruction, and recursively follow
957 * any other branches (jumps). Meanwhile, track the frame pointer state at
958 * each instruction and validate all the rules described in
959 * tools/objtool/Documentation/stack-validation.txt.
960 */
961static int validate_branch(struct objtool_file *file,
962 struct instruction *first, unsigned char first_state)
963{
964 struct alternative *alt;
965 struct instruction *insn;
966 struct section *sec;
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500967 struct symbol *func = NULL;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600968 unsigned char state;
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600969 int ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600970
971 insn = first;
972 sec = insn->sec;
973 state = first_state;
974
975 if (insn->alt_group && list_empty(&insn->alts)) {
976 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
977 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600978 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600979 }
980
981 while (1) {
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500982 if (file->c_file && insn->func) {
983 if (func && func != insn->func) {
984 WARN("%s() falls through to next function %s()",
985 func->name, insn->func->name);
986 return 1;
987 }
988
989 func = insn->func;
990 }
991
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600992 if (insn->visited) {
Josh Poimboeufd8d1b2c2016-03-09 00:06:54 -0600993 if (frame_state(insn->state) != frame_state(state)) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600994 WARN_FUNC("frame pointer state mismatch",
995 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600996 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600997 }
998
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -0600999 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001000 }
1001
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001002 insn->visited = true;
1003 insn->state = state;
1004
1005 list_for_each_entry(alt, &insn->alts, list) {
1006 ret = validate_branch(file, alt->insn, state);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001007 if (ret)
1008 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001009 }
1010
1011 switch (insn->type) {
1012
1013 case INSN_FP_SAVE:
1014 if (!nofp) {
1015 if (state & STATE_FP_SAVED) {
1016 WARN_FUNC("duplicate frame pointer save",
1017 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001018 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001019 }
1020 state |= STATE_FP_SAVED;
1021 }
1022 break;
1023
1024 case INSN_FP_SETUP:
1025 if (!nofp) {
1026 if (state & STATE_FP_SETUP) {
1027 WARN_FUNC("duplicate frame pointer setup",
1028 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001029 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001030 }
1031 state |= STATE_FP_SETUP;
1032 }
1033 break;
1034
1035 case INSN_FP_RESTORE:
1036 if (!nofp) {
1037 if (has_valid_stack_frame(insn))
1038 state &= ~STATE_FP_SETUP;
1039
1040 state &= ~STATE_FP_SAVED;
1041 }
1042 break;
1043
1044 case INSN_RETURN:
1045 if (!nofp && has_modified_stack_frame(insn)) {
1046 WARN_FUNC("return without frame pointer restore",
1047 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001048 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001049 }
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001050 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001051
1052 case INSN_CALL:
1053 if (is_fentry_call(insn)) {
1054 state |= STATE_FENTRY;
1055 break;
1056 }
1057
Josh Poimboeufb1e03242016-03-09 00:06:52 -06001058 ret = dead_end_function(file, insn->call_dest);
1059 if (ret == 1)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001060 return 0;
Josh Poimboeufb1e03242016-03-09 00:06:52 -06001061 if (ret == -1)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001062 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001063
1064 /* fallthrough */
1065 case INSN_CALL_DYNAMIC:
1066 if (!nofp && !has_valid_stack_frame(insn)) {
1067 WARN_FUNC("call without frame pointer save/setup",
1068 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001069 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001070 }
1071 break;
1072
1073 case INSN_JUMP_CONDITIONAL:
1074 case INSN_JUMP_UNCONDITIONAL:
1075 if (insn->jump_dest) {
1076 ret = validate_branch(file, insn->jump_dest,
1077 state);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001078 if (ret)
1079 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001080 } else if (has_modified_stack_frame(insn)) {
1081 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
1082 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001083 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001084 } /* else it's a sibling call */
1085
1086 if (insn->type == INSN_JUMP_UNCONDITIONAL)
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001087 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001088
1089 break;
1090
1091 case INSN_JUMP_DYNAMIC:
1092 if (list_empty(&insn->alts) &&
1093 has_modified_stack_frame(insn)) {
1094 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
1095 sec, insn->offset);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001096 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001097 }
1098
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001099 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001100
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001101 default:
1102 break;
1103 }
1104
Josh Poimboeufd1091c72017-02-21 15:35:32 -06001105 if (insn->dead_end)
1106 return 0;
1107
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001108 insn = next_insn_same_sec(file, insn);
1109 if (!insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001110 WARN("%s: unexpected end of section", sec->name);
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001111 return 1;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001112 }
1113 }
1114
Josh Poimboeuf1bcb58a2016-03-09 00:07:01 -06001115 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001116}
1117
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001118static bool is_kasan_insn(struct instruction *insn)
1119{
1120 return (insn->type == INSN_CALL &&
1121 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1122}
1123
1124static bool is_ubsan_insn(struct instruction *insn)
1125{
1126 return (insn->type == INSN_CALL &&
1127 !strcmp(insn->call_dest->name,
1128 "__ubsan_handle_builtin_unreachable"));
1129}
1130
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001131static bool ignore_unreachable_insn(struct symbol *func,
1132 struct instruction *insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001133{
1134 int i;
1135
1136 if (insn->type == INSN_NOP)
1137 return true;
1138
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001139 /*
1140 * Check if this (or a subsequent) instruction is related to
1141 * CONFIG_UBSAN or CONFIG_KASAN.
1142 *
1143 * End the search at 5 instructions to avoid going into the weeds.
1144 */
1145 for (i = 0; i < 5; i++) {
1146
1147 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
1148 return true;
1149
1150 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
1151 insn = insn->jump_dest;
1152 continue;
1153 }
1154
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001155 if (insn->offset + insn->len >= func->offset + func->len)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001156 break;
1157 insn = list_next_entry(insn, list);
1158 }
1159
1160 return false;
1161}
1162
1163static int validate_functions(struct objtool_file *file)
1164{
1165 struct section *sec;
1166 struct symbol *func;
1167 struct instruction *insn;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001168 int ret, warnings = 0;
1169
1170 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -06001171 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001172 if (func->type != STT_FUNC)
1173 continue;
1174
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001175 insn = find_insn(file, sec, func->offset);
Josh Poimboeufb1547d32016-04-15 09:17:10 -05001176 if (!insn)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001177 continue;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001178
1179 ret = validate_branch(file, insn, 0);
1180 warnings += ret;
1181 }
1182 }
1183
1184 list_for_each_entry(sec, &file->elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -06001185 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001186 if (func->type != STT_FUNC)
1187 continue;
1188
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001189 func_for_each_insn(file, func, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001190 if (insn->visited)
1191 continue;
1192
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001193 insn->visited = true;
Josh Poimboeuf7e578442016-04-14 14:52:24 -05001194
1195 if (file->ignore_unreachables || warnings ||
1196 ignore_unreachable_insn(func, insn))
1197 continue;
1198
Josh Poimboeuf9cfffb12016-10-13 16:22:53 -05001199 /*
1200 * gcov produces a lot of unreachable
1201 * instructions. If we get an unreachable
1202 * warning and the file has gcov enabled, just
1203 * ignore it, and all other such warnings for
1204 * the file.
1205 */
1206 if (!file->ignore_unreachables &&
1207 gcov_enabled(file)) {
1208 file->ignore_unreachables = true;
1209 continue;
1210 }
1211
Josh Poimboeuf7e578442016-04-14 14:52:24 -05001212 WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset);
1213 warnings++;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001214 }
1215 }
1216 }
1217
1218 return warnings;
1219}
1220
1221static int validate_uncallable_instructions(struct objtool_file *file)
1222{
1223 struct instruction *insn;
1224 int warnings = 0;
1225
Josh Poimboeuf74aec052016-03-09 00:06:55 -06001226 for_each_insn(file, insn) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001227 if (!insn->visited && insn->type == INSN_RETURN) {
1228 WARN_FUNC("return instruction outside of a callable function",
1229 insn->sec, insn->offset);
1230 warnings++;
1231 }
1232 }
1233
1234 return warnings;
1235}
1236
1237static void cleanup(struct objtool_file *file)
1238{
1239 struct instruction *insn, *tmpinsn;
1240 struct alternative *alt, *tmpalt;
1241
Josh Poimboeufa196e172016-03-09 00:06:57 -06001242 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001243 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
1244 list_del(&alt->list);
1245 free(alt);
1246 }
1247 list_del(&insn->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -06001248 hash_del(&insn->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001249 free(insn);
1250 }
1251 elf_close(file->elf);
1252}
1253
1254const char * const check_usage[] = {
1255 "objtool check [<options>] file.o",
1256 NULL,
1257};
1258
1259int cmd_check(int argc, const char **argv)
1260{
1261 struct objtool_file file;
1262 int ret, warnings = 0;
1263
1264 const struct option options[] = {
1265 OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"),
1266 OPT_END(),
1267 };
1268
1269 argc = parse_options(argc, argv, options, check_usage, 0);
1270
1271 if (argc != 1)
1272 usage_with_options(check_usage, options);
1273
1274 objname = argv[0];
1275
1276 file.elf = elf_open(objname);
1277 if (!file.elf) {
1278 fprintf(stderr, "error reading elf file %s\n", objname);
1279 return 1;
1280 }
1281
Josh Poimboeufa196e172016-03-09 00:06:57 -06001282 INIT_LIST_HEAD(&file.insn_list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -06001283 hash_init(file.insn_hash);
Josh Poimboeufe390f9a2017-03-01 12:04:44 -06001284 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
Josh Poimboeuf7e578442016-04-14 14:52:24 -05001285 file.rodata = find_section_by_name(file.elf, ".rodata");
1286 file.ignore_unreachables = false;
Josh Poimboeufb1547d32016-04-15 09:17:10 -05001287 file.c_file = find_section_by_name(file.elf, ".comment");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001288
1289 ret = decode_sections(&file);
1290 if (ret < 0)
1291 goto out;
1292 warnings += ret;
1293
1294 ret = validate_functions(&file);
1295 if (ret < 0)
1296 goto out;
1297 warnings += ret;
1298
1299 ret = validate_uncallable_instructions(&file);
1300 if (ret < 0)
1301 goto out;
1302 warnings += ret;
1303
1304out:
1305 cleanup(&file);
1306
1307 /* ignore warnings for now until we get all the code cleaned up */
1308 if (ret || warnings)
1309 return 0;
1310 return 0;
1311}