diff options
| author | Josh Poimboeuf <jpoimboe@redhat.com> | 2017-06-28 11:11:05 -0400 |
|---|---|---|
| committer | Ingo Molnar <mingo@kernel.org> | 2017-06-30 04:19:19 -0400 |
| commit | dcc914f44f065ef73685b37e59877a5bb3cb7358 (patch) | |
| tree | 38ec8f7dee26ae770a14dcb7cb8563cd49b81074 /tools/objtool | |
| parent | 4d8a991d460d4fa4829beaffdcba45a217ca0fa7 (diff) | |
objtool: Move checking code to check.c
In preparation for the new 'objtool undwarf generate' command, which
will rely on 'objtool check', move the checking code from
builtin-check.c to check.c where it can be used by other commands.
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Reviewed-by: Jiri Slaby <jslaby@suse.cz>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: live-patching@vger.kernel.org
Link: http://lkml.kernel.org/r/294c5c695fd73c1a5000bbe5960a7c9bec4ee6b4.1498659915.git.jpoimboe@redhat.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'tools/objtool')
| -rw-r--r-- | tools/objtool/Build | 1 | ||||
| -rw-r--r-- | tools/objtool/builtin-check.c | 1281 | ||||
| -rw-r--r-- | tools/objtool/check.c | 1263 | ||||
| -rw-r--r-- | tools/objtool/check.h | 51 |
4 files changed, 1328 insertions, 1268 deletions
diff --git a/tools/objtool/Build b/tools/objtool/Build index d6cdece5e58b..6f2e1987c4d9 100644 --- a/tools/objtool/Build +++ b/tools/objtool/Build | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | objtool-y += arch/$(SRCARCH)/ | 1 | objtool-y += arch/$(SRCARCH)/ |
| 2 | objtool-y += builtin-check.o | 2 | objtool-y += builtin-check.o |
| 3 | objtool-y += check.o | ||
| 3 | objtool-y += elf.o | 4 | objtool-y += elf.o |
| 4 | objtool-y += special.o | 5 | objtool-y += special.o |
| 5 | objtool-y += objtool.o | 6 | objtool-y += objtool.o |
diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c index 5f66697fe1e0..365c34ecab26 100644 --- a/tools/objtool/builtin-check.c +++ b/tools/objtool/builtin-check.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com> | 2 | * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> |
| 3 | * | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or | 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License | 5 | * modify it under the terms of the GNU General Public License |
| @@ -25,1287 +25,32 @@ | |||
| 25 | * For more information, see tools/objtool/Documentation/stack-validation.txt. | 25 | * For more information, see tools/objtool/Documentation/stack-validation.txt. |
| 26 | */ | 26 | */ |
| 27 | 27 | ||
| 28 | #include <string.h> | ||
| 29 | #include <stdlib.h> | ||
| 30 | #include <subcmd/parse-options.h> | 28 | #include <subcmd/parse-options.h> |
| 31 | |||
| 32 | #include "builtin.h" | 29 | #include "builtin.h" |
| 33 | #include "elf.h" | 30 | #include "check.h" |
| 34 | #include "special.h" | ||
| 35 | #include "arch.h" | ||
| 36 | #include "warn.h" | ||
| 37 | |||
| 38 | #include <linux/hashtable.h> | ||
| 39 | #include <linux/kernel.h> | ||
| 40 | |||
| 41 | #define STATE_FP_SAVED 0x1 | ||
| 42 | #define STATE_FP_SETUP 0x2 | ||
| 43 | #define STATE_FENTRY 0x4 | ||
| 44 | |||
| 45 | struct instruction { | ||
| 46 | struct list_head list; | ||
| 47 | struct hlist_node hash; | ||
| 48 | struct section *sec; | ||
| 49 | unsigned long offset; | ||
| 50 | unsigned int len, state; | ||
| 51 | unsigned char type; | ||
| 52 | unsigned long immediate; | ||
| 53 | bool alt_group, visited, dead_end; | ||
| 54 | struct symbol *call_dest; | ||
| 55 | struct instruction *jump_dest; | ||
| 56 | struct list_head alts; | ||
| 57 | struct symbol *func; | ||
| 58 | }; | ||
| 59 | |||
| 60 | struct alternative { | ||
| 61 | struct list_head list; | ||
| 62 | struct instruction *insn; | ||
| 63 | }; | ||
| 64 | |||
| 65 | struct objtool_file { | ||
| 66 | struct elf *elf; | ||
| 67 | struct list_head insn_list; | ||
| 68 | DECLARE_HASHTABLE(insn_hash, 16); | ||
| 69 | struct section *rodata, *whitelist; | ||
| 70 | bool ignore_unreachables, c_file; | ||
| 71 | }; | ||
| 72 | |||
| 73 | const char *objname; | ||
| 74 | static bool nofp; | ||
| 75 | |||
| 76 | static struct instruction *find_insn(struct objtool_file *file, | ||
| 77 | struct section *sec, unsigned long offset) | ||
| 78 | { | ||
| 79 | struct instruction *insn; | ||
| 80 | |||
| 81 | hash_for_each_possible(file->insn_hash, insn, hash, offset) | ||
| 82 | if (insn->sec == sec && insn->offset == offset) | ||
| 83 | return insn; | ||
| 84 | |||
| 85 | return NULL; | ||
| 86 | } | ||
| 87 | |||
| 88 | static struct instruction *next_insn_same_sec(struct objtool_file *file, | ||
| 89 | struct instruction *insn) | ||
| 90 | { | ||
| 91 | struct instruction *next = list_next_entry(insn, list); | ||
| 92 | |||
| 93 | if (&next->list == &file->insn_list || next->sec != insn->sec) | ||
| 94 | return NULL; | ||
| 95 | |||
| 96 | return next; | ||
| 97 | } | ||
| 98 | |||
| 99 | static bool gcov_enabled(struct objtool_file *file) | ||
| 100 | { | ||
| 101 | struct section *sec; | ||
| 102 | struct symbol *sym; | ||
| 103 | |||
| 104 | list_for_each_entry(sec, &file->elf->sections, list) | ||
| 105 | list_for_each_entry(sym, &sec->symbol_list, list) | ||
| 106 | if (!strncmp(sym->name, "__gcov_.", 8)) | ||
| 107 | return true; | ||
| 108 | |||
| 109 | return false; | ||
| 110 | } | ||
| 111 | |||
| 112 | #define for_each_insn(file, insn) \ | ||
| 113 | list_for_each_entry(insn, &file->insn_list, list) | ||
| 114 | |||
| 115 | #define func_for_each_insn(file, func, insn) \ | ||
| 116 | for (insn = find_insn(file, func->sec, func->offset); \ | ||
| 117 | insn && &insn->list != &file->insn_list && \ | ||
| 118 | insn->sec == func->sec && \ | ||
| 119 | insn->offset < func->offset + func->len; \ | ||
| 120 | insn = list_next_entry(insn, list)) | ||
| 121 | |||
| 122 | #define func_for_each_insn_continue_reverse(file, func, insn) \ | ||
| 123 | for (insn = list_prev_entry(insn, list); \ | ||
| 124 | &insn->list != &file->insn_list && \ | ||
| 125 | insn->sec == func->sec && insn->offset >= func->offset; \ | ||
| 126 | insn = list_prev_entry(insn, list)) | ||
| 127 | |||
| 128 | #define sec_for_each_insn_from(file, insn) \ | ||
| 129 | for (; insn; insn = next_insn_same_sec(file, insn)) | ||
| 130 | |||
| 131 | |||
| 132 | /* | ||
| 133 | * Check if the function has been manually whitelisted with the | ||
| 134 | * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted | ||
| 135 | * due to its use of a context switching instruction. | ||
| 136 | */ | ||
| 137 | static bool ignore_func(struct objtool_file *file, struct symbol *func) | ||
| 138 | { | ||
| 139 | struct rela *rela; | ||
| 140 | struct instruction *insn; | ||
| 141 | |||
| 142 | /* check for STACK_FRAME_NON_STANDARD */ | ||
| 143 | if (file->whitelist && file->whitelist->rela) | ||
| 144 | list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) { | ||
| 145 | if (rela->sym->type == STT_SECTION && | ||
| 146 | rela->sym->sec == func->sec && | ||
| 147 | rela->addend == func->offset) | ||
| 148 | return true; | ||
| 149 | if (rela->sym->type == STT_FUNC && rela->sym == func) | ||
| 150 | return true; | ||
| 151 | } | ||
| 152 | |||
| 153 | /* check if it has a context switching instruction */ | ||
| 154 | func_for_each_insn(file, func, insn) | ||
| 155 | if (insn->type == INSN_CONTEXT_SWITCH) | ||
| 156 | return true; | ||
| 157 | |||
| 158 | return false; | ||
| 159 | } | ||
| 160 | |||
| 161 | /* | ||
| 162 | * This checks to see if the given function is a "noreturn" function. | ||
| 163 | * | ||
| 164 | * For global functions which are outside the scope of this object file, we | ||
| 165 | * have to keep a manual list of them. | ||
| 166 | * | ||
| 167 | * For local functions, we have to detect them manually by simply looking for | ||
| 168 | * the lack of a return instruction. | ||
| 169 | * | ||
| 170 | * Returns: | ||
| 171 | * -1: error | ||
| 172 | * 0: no dead end | ||
| 173 | * 1: dead end | ||
| 174 | */ | ||
| 175 | static int __dead_end_function(struct objtool_file *file, struct symbol *func, | ||
| 176 | int recursion) | ||
| 177 | { | ||
| 178 | int i; | ||
| 179 | struct instruction *insn; | ||
| 180 | bool empty = true; | ||
| 181 | |||
| 182 | /* | ||
| 183 | * Unfortunately these have to be hard coded because the noreturn | ||
| 184 | * attribute isn't provided in ELF data. | ||
| 185 | */ | ||
| 186 | static const char * const global_noreturns[] = { | ||
| 187 | "__stack_chk_fail", | ||
| 188 | "panic", | ||
| 189 | "do_exit", | ||
| 190 | "do_task_dead", | ||
| 191 | "__module_put_and_exit", | ||
| 192 | "complete_and_exit", | ||
| 193 | "kvm_spurious_fault", | ||
| 194 | "__reiserfs_panic", | ||
| 195 | "lbug_with_loc", | ||
| 196 | "fortify_panic", | ||
| 197 | }; | ||
| 198 | |||
| 199 | if (func->bind == STB_WEAK) | ||
| 200 | return 0; | ||
| 201 | |||
| 202 | if (func->bind == STB_GLOBAL) | ||
| 203 | for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) | ||
| 204 | if (!strcmp(func->name, global_noreturns[i])) | ||
| 205 | return 1; | ||
| 206 | |||
| 207 | if (!func->sec) | ||
| 208 | return 0; | ||
| 209 | |||
| 210 | func_for_each_insn(file, func, insn) { | ||
| 211 | empty = false; | ||
| 212 | |||
| 213 | if (insn->type == INSN_RETURN) | ||
| 214 | return 0; | ||
| 215 | } | ||
| 216 | |||
| 217 | if (empty) | ||
| 218 | return 0; | ||
| 219 | |||
| 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 | */ | ||
| 225 | func_for_each_insn(file, func, insn) { | ||
| 226 | if (insn->sec != func->sec || | ||
| 227 | insn->offset >= func->offset + func->len) | ||
| 228 | break; | ||
| 229 | |||
| 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 */ | ||
| 236 | return 0; | ||
| 237 | |||
| 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 | |||
| 247 | 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); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts)) | ||
| 259 | /* sibling call */ | ||
| 260 | return 0; | ||
| 261 | } | ||
| 262 | |||
| 263 | return 1; | ||
| 264 | } | ||
| 265 | |||
| 266 | static int dead_end_function(struct objtool_file *file, struct symbol *func) | ||
| 267 | { | ||
| 268 | return __dead_end_function(file, func, 0); | ||
| 269 | } | ||
| 270 | |||
| 271 | /* | ||
| 272 | * Call the arch-specific instruction decoder for all the instructions and add | ||
| 273 | * them to the global instruction list. | ||
| 274 | */ | ||
| 275 | static int decode_instructions(struct objtool_file *file) | ||
| 276 | { | ||
| 277 | struct section *sec; | ||
| 278 | struct symbol *func; | ||
| 279 | unsigned long offset; | ||
| 280 | struct instruction *insn; | ||
| 281 | int ret; | ||
| 282 | |||
| 283 | 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 | |||
| 309 | hash_add(file->insn_hash, &insn->hash, insn->offset); | ||
| 310 | list_add_tail(&insn->list, &file->insn_list); | ||
| 311 | } | ||
| 312 | |||
| 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 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | return 0; | ||
| 330 | } | ||
| 331 | |||
| 332 | /* | ||
| 333 | * Find all uses of the unreachable() macro, which are code path dead ends. | ||
| 334 | */ | ||
| 335 | static 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 | |||
| 342 | sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); | ||
| 343 | if (!sec) | ||
| 344 | return 0; | ||
| 345 | |||
| 346 | list_for_each_entry(rela, &sec->rela_list, list) { | ||
| 347 | if (rela->sym->type != STT_SECTION) { | ||
| 348 | WARN("unexpected relocation symbol type in %s", sec->name); | ||
| 349 | 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 | /* | ||
| 381 | * Warnings shouldn't be reported for ignored functions. | ||
| 382 | */ | ||
| 383 | static void add_ignores(struct objtool_file *file) | ||
| 384 | { | ||
| 385 | struct instruction *insn; | ||
| 386 | struct section *sec; | ||
| 387 | struct symbol *func; | ||
| 388 | |||
| 389 | list_for_each_entry(sec, &file->elf->sections, list) { | ||
| 390 | list_for_each_entry(func, &sec->symbol_list, list) { | ||
| 391 | if (func->type != STT_FUNC) | ||
| 392 | continue; | ||
| 393 | |||
| 394 | if (!ignore_func(file, func)) | ||
| 395 | continue; | ||
| 396 | |||
| 397 | func_for_each_insn(file, func, insn) | ||
| 398 | insn->visited = true; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | } | ||
| 402 | |||
| 403 | /* | ||
| 404 | * Find the destination instructions for all jumps. | ||
| 405 | */ | ||
| 406 | static int add_jump_destinations(struct objtool_file *file) | ||
| 407 | { | ||
| 408 | struct instruction *insn; | ||
| 409 | struct rela *rela; | ||
| 410 | struct section *dest_sec; | ||
| 411 | unsigned long dest_off; | ||
| 412 | |||
| 413 | for_each_insn(file, insn) { | ||
| 414 | 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 | |||
| 439 | insn->jump_dest = find_insn(file, dest_sec, dest_off); | ||
| 440 | 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 | */ | ||
| 463 | static int add_call_destinations(struct objtool_file *file) | ||
| 464 | { | ||
| 465 | struct instruction *insn; | ||
| 466 | unsigned long dest_off; | ||
| 467 | struct rela *rela; | ||
| 468 | |||
| 469 | for_each_insn(file, insn) { | ||
| 470 | 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 | */ | ||
| 522 | static 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; | ||
| 532 | sec_for_each_insn_from(file, insn) { | ||
| 533 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) | ||
| 534 | 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 | |||
| 543 | if (!next_insn_same_sec(file, last_orig_insn)) { | ||
| 544 | 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; | ||
| 568 | sec_for_each_insn_from(file, insn) { | ||
| 569 | if (insn->offset >= special_alt->new_off + special_alt->new_len) | ||
| 570 | 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 | */ | ||
| 608 | static 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 | */ | ||
| 632 | static int add_special_section_alts(struct objtool_file *file) | ||
| 633 | { | ||
| 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 | |||
| 652 | orig_insn = find_insn(file, special_alt->orig_sec, | ||
| 653 | special_alt->orig_off); | ||
| 654 | 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 | 31 | ||
| 661 | new_insn = NULL; | 32 | bool nofp; |
| 662 | if (!special_alt->group || special_alt->new_len) { | ||
| 663 | new_insn = find_insn(file, special_alt->new_sec, | ||
| 664 | special_alt->new_off); | ||
| 665 | 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 | 33 | ||
| 674 | if (special_alt->group) { | 34 | static const char * const check_usage[] = { |
| 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 | |||
| 693 | out: | ||
| 694 | return ret; | ||
| 695 | } | ||
| 696 | |||
| 697 | static 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 | |||
| 735 | /* | ||
| 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 | */ | ||
| 772 | static 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; | ||
| 777 | struct instruction *orig_insn = insn; | ||
| 778 | |||
| 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) { | ||
| 798 | if (insn->type == INSN_JUMP_DYNAMIC) | ||
| 799 | break; | ||
| 800 | |||
| 801 | /* allow small jumps within the range */ | ||
| 802 | if (insn->type == INSN_JUMP_UNCONDITIONAL && | ||
| 803 | insn->jump_dest && | ||
| 804 | (insn->jump_dest->offset <= insn->offset || | ||
| 805 | insn->jump_dest->offset > orig_insn->offset)) | ||
| 806 | break; | ||
| 807 | |||
| 808 | /* look for a relocation which references .rodata */ | ||
| 809 | text_rela = find_rela_by_dest_range(insn->sec, insn->offset, | ||
| 810 | insn->len); | ||
| 811 | 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); | ||
| 822 | } | ||
| 823 | |||
| 824 | return NULL; | ||
| 825 | } | ||
| 826 | |||
| 827 | static int add_func_switch_tables(struct objtool_file *file, | ||
| 828 | struct symbol *func) | ||
| 829 | { | ||
| 830 | struct instruction *insn, *prev_jump = NULL; | ||
| 831 | struct rela *rela, *prev_rela = NULL; | ||
| 832 | int ret; | ||
| 833 | |||
| 834 | func_for_each_insn(file, func, insn) { | ||
| 835 | if (insn->type != INSN_JUMP_DYNAMIC) | ||
| 836 | continue; | ||
| 837 | |||
| 838 | rela = find_switch_table(file, func, insn); | ||
| 839 | if (!rela) | ||
| 840 | 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, | ||
| 849 | rela); | ||
| 850 | if (ret) | ||
| 851 | return ret; | ||
| 852 | } | ||
| 853 | |||
| 854 | prev_jump = insn; | ||
| 855 | prev_rela = rela; | ||
| 856 | } | ||
| 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 | |||
| 867 | /* | ||
| 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 | */ | ||
| 872 | static int add_switch_table_alts(struct objtool_file *file) | ||
| 873 | { | ||
| 874 | struct section *sec; | ||
| 875 | struct symbol *func; | ||
| 876 | int ret; | ||
| 877 | |||
| 878 | if (!file->rodata || !file->rodata->rela) | ||
| 879 | return 0; | ||
| 880 | |||
| 881 | 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; | ||
| 885 | |||
| 886 | ret = add_func_switch_tables(file, func); | ||
| 887 | if (ret) | ||
| 888 | return ret; | ||
| 889 | } | ||
| 890 | } | ||
| 891 | |||
| 892 | return 0; | ||
| 893 | } | ||
| 894 | |||
| 895 | static int decode_sections(struct objtool_file *file) | ||
| 896 | { | ||
| 897 | int ret; | ||
| 898 | |||
| 899 | ret = decode_instructions(file); | ||
| 900 | if (ret) | ||
| 901 | return ret; | ||
| 902 | |||
| 903 | ret = add_dead_ends(file); | ||
| 904 | if (ret) | ||
| 905 | return ret; | ||
| 906 | |||
| 907 | add_ignores(file); | ||
| 908 | |||
| 909 | ret = add_jump_destinations(file); | ||
| 910 | if (ret) | ||
| 911 | return ret; | ||
| 912 | |||
| 913 | ret = add_call_destinations(file); | ||
| 914 | if (ret) | ||
| 915 | return ret; | ||
| 916 | |||
| 917 | ret = add_special_section_alts(file); | ||
| 918 | if (ret) | ||
| 919 | return ret; | ||
| 920 | |||
| 921 | ret = add_switch_table_alts(file); | ||
| 922 | if (ret) | ||
| 923 | return ret; | ||
| 924 | |||
| 925 | return 0; | ||
| 926 | } | ||
| 927 | |||
| 928 | static 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 | |||
| 938 | static bool has_modified_stack_frame(struct instruction *insn) | ||
| 939 | { | ||
| 940 | return (insn->state & STATE_FP_SAVED) || | ||
| 941 | (insn->state & STATE_FP_SETUP); | ||
| 942 | } | ||
| 943 | |||
| 944 | static bool has_valid_stack_frame(struct instruction *insn) | ||
| 945 | { | ||
| 946 | return (insn->state & STATE_FP_SAVED) && | ||
| 947 | (insn->state & STATE_FP_SETUP); | ||
| 948 | } | ||
| 949 | |||
| 950 | static unsigned int frame_state(unsigned long state) | ||
| 951 | { | ||
| 952 | return (state & (STATE_FP_SAVED | STATE_FP_SETUP)); | ||
| 953 | } | ||
| 954 | |||
| 955 | /* | ||
| 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 | */ | ||
| 961 | static 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; | ||
| 967 | struct symbol *func = NULL; | ||
| 968 | unsigned char state; | ||
| 969 | int ret; | ||
| 970 | |||
| 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); | ||
| 978 | return 1; | ||
| 979 | } | ||
| 980 | |||
| 981 | while (1) { | ||
| 982 | 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 | |||
| 992 | if (insn->visited) { | ||
| 993 | if (frame_state(insn->state) != frame_state(state)) { | ||
| 994 | WARN_FUNC("frame pointer state mismatch", | ||
| 995 | sec, insn->offset); | ||
| 996 | return 1; | ||
| 997 | } | ||
| 998 | |||
| 999 | return 0; | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | 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); | ||
| 1007 | if (ret) | ||
| 1008 | return 1; | ||
| 1009 | } | ||
| 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); | ||
| 1018 | return 1; | ||
| 1019 | } | ||
| 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); | ||
| 1029 | return 1; | ||
| 1030 | } | ||
| 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); | ||
| 1048 | return 1; | ||
| 1049 | } | ||
| 1050 | return 0; | ||
| 1051 | |||
| 1052 | case INSN_CALL: | ||
| 1053 | if (is_fentry_call(insn)) { | ||
| 1054 | state |= STATE_FENTRY; | ||
| 1055 | break; | ||
| 1056 | } | ||
| 1057 | |||
| 1058 | ret = dead_end_function(file, insn->call_dest); | ||
| 1059 | if (ret == 1) | ||
| 1060 | return 0; | ||
| 1061 | if (ret == -1) | ||
| 1062 | return 1; | ||
| 1063 | |||
| 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); | ||
| 1069 | return 1; | ||
| 1070 | } | ||
| 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); | ||
| 1078 | if (ret) | ||
| 1079 | return 1; | ||
| 1080 | } else if (has_modified_stack_frame(insn)) { | ||
| 1081 | WARN_FUNC("sibling call from callable instruction with changed frame pointer", | ||
| 1082 | sec, insn->offset); | ||
| 1083 | return 1; | ||
| 1084 | } /* else it's a sibling call */ | ||
| 1085 | |||
| 1086 | if (insn->type == INSN_JUMP_UNCONDITIONAL) | ||
| 1087 | return 0; | ||
| 1088 | |||
| 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); | ||
| 1096 | return 1; | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | return 0; | ||
| 1100 | |||
| 1101 | default: | ||
| 1102 | break; | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | if (insn->dead_end) | ||
| 1106 | return 0; | ||
| 1107 | |||
| 1108 | insn = next_insn_same_sec(file, insn); | ||
| 1109 | if (!insn) { | ||
| 1110 | WARN("%s: unexpected end of section", sec->name); | ||
| 1111 | return 1; | ||
| 1112 | } | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | return 0; | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | static 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 | |||
| 1124 | static 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 | |||
| 1131 | static bool ignore_unreachable_insn(struct symbol *func, | ||
| 1132 | struct instruction *insn) | ||
| 1133 | { | ||
| 1134 | int i; | ||
| 1135 | |||
| 1136 | if (insn->type == INSN_NOP) | ||
| 1137 | return true; | ||
| 1138 | |||
| 1139 | /* | ||
| 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 | |||
| 1155 | if (insn->offset + insn->len >= func->offset + func->len) | ||
| 1156 | break; | ||
| 1157 | insn = list_next_entry(insn, list); | ||
| 1158 | } | ||
| 1159 | |||
| 1160 | return false; | ||
| 1161 | } | ||
| 1162 | |||
| 1163 | static int validate_functions(struct objtool_file *file) | ||
| 1164 | { | ||
| 1165 | struct section *sec; | ||
| 1166 | struct symbol *func; | ||
| 1167 | struct instruction *insn; | ||
| 1168 | int ret, warnings = 0; | ||
| 1169 | |||
| 1170 | list_for_each_entry(sec, &file->elf->sections, list) { | ||
| 1171 | list_for_each_entry(func, &sec->symbol_list, list) { | ||
| 1172 | if (func->type != STT_FUNC) | ||
| 1173 | continue; | ||
| 1174 | |||
| 1175 | insn = find_insn(file, sec, func->offset); | ||
| 1176 | if (!insn) | ||
| 1177 | continue; | ||
| 1178 | |||
| 1179 | ret = validate_branch(file, insn, 0); | ||
| 1180 | warnings += ret; | ||
| 1181 | } | ||
| 1182 | } | ||
| 1183 | |||
| 1184 | list_for_each_entry(sec, &file->elf->sections, list) { | ||
| 1185 | list_for_each_entry(func, &sec->symbol_list, list) { | ||
| 1186 | if (func->type != STT_FUNC) | ||
| 1187 | continue; | ||
| 1188 | |||
| 1189 | func_for_each_insn(file, func, insn) { | ||
| 1190 | if (insn->visited) | ||
| 1191 | continue; | ||
| 1192 | |||
| 1193 | insn->visited = true; | ||
| 1194 | |||
| 1195 | if (file->ignore_unreachables || warnings || | ||
| 1196 | ignore_unreachable_insn(func, insn)) | ||
| 1197 | continue; | ||
| 1198 | |||
| 1199 | /* | ||
| 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 | |||
| 1212 | WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset); | ||
| 1213 | warnings++; | ||
| 1214 | } | ||
| 1215 | } | ||
| 1216 | } | ||
| 1217 | |||
| 1218 | return warnings; | ||
| 1219 | } | ||
| 1220 | |||
| 1221 | static int validate_uncallable_instructions(struct objtool_file *file) | ||
| 1222 | { | ||
| 1223 | struct instruction *insn; | ||
| 1224 | int warnings = 0; | ||
| 1225 | |||
| 1226 | for_each_insn(file, insn) { | ||
| 1227 | 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 | |||
| 1237 | static void cleanup(struct objtool_file *file) | ||
| 1238 | { | ||
| 1239 | struct instruction *insn, *tmpinsn; | ||
| 1240 | struct alternative *alt, *tmpalt; | ||
| 1241 | |||
| 1242 | list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) { | ||
| 1243 | list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) { | ||
| 1244 | list_del(&alt->list); | ||
| 1245 | free(alt); | ||
| 1246 | } | ||
| 1247 | list_del(&insn->list); | ||
| 1248 | hash_del(&insn->hash); | ||
| 1249 | free(insn); | ||
| 1250 | } | ||
| 1251 | elf_close(file->elf); | ||
| 1252 | } | ||
| 1253 | |||
| 1254 | const char * const check_usage[] = { | ||
| 1255 | "objtool check [<options>] file.o", | 35 | "objtool check [<options>] file.o", |
| 1256 | NULL, | 36 | NULL, |
| 1257 | }; | 37 | }; |
| 1258 | 38 | ||
| 39 | const struct option check_options[] = { | ||
| 40 | OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"), | ||
| 41 | OPT_END(), | ||
| 42 | }; | ||
| 43 | |||
| 1259 | int cmd_check(int argc, const char **argv) | 44 | int cmd_check(int argc, const char **argv) |
| 1260 | { | 45 | { |
| 1261 | struct objtool_file file; | 46 | const char *objname; |
| 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 | 47 | ||
| 1269 | argc = parse_options(argc, argv, options, check_usage, 0); | 48 | argc = parse_options(argc, argv, check_options, check_usage, 0); |
| 1270 | 49 | ||
| 1271 | if (argc != 1) | 50 | if (argc != 1) |
| 1272 | usage_with_options(check_usage, options); | 51 | usage_with_options(check_usage, check_options); |
| 1273 | 52 | ||
| 1274 | objname = argv[0]; | 53 | objname = argv[0]; |
| 1275 | 54 | ||
| 1276 | file.elf = elf_open(objname); | 55 | return check(objname, nofp); |
| 1277 | if (!file.elf) { | ||
| 1278 | fprintf(stderr, "error reading elf file %s\n", objname); | ||
| 1279 | return 1; | ||
| 1280 | } | ||
| 1281 | |||
| 1282 | INIT_LIST_HEAD(&file.insn_list); | ||
| 1283 | hash_init(file.insn_hash); | ||
| 1284 | file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard"); | ||
| 1285 | file.rodata = find_section_by_name(file.elf, ".rodata"); | ||
| 1286 | file.ignore_unreachables = false; | ||
| 1287 | file.c_file = find_section_by_name(file.elf, ".comment"); | ||
| 1288 | |||
| 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 | |||
| 1304 | out: | ||
| 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 | } | 56 | } |
diff --git a/tools/objtool/check.c b/tools/objtool/check.c new file mode 100644 index 000000000000..231a36053e07 --- /dev/null +++ b/tools/objtool/check.c | |||
| @@ -0,0 +1,1263 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU General Public License | ||
| 6 | * as published by the Free Software Foundation; either version 2 | ||
| 7 | * of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <string.h> | ||
| 19 | #include <stdlib.h> | ||
| 20 | |||
| 21 | #include "check.h" | ||
| 22 | #include "elf.h" | ||
| 23 | #include "special.h" | ||
| 24 | #include "arch.h" | ||
| 25 | #include "warn.h" | ||
| 26 | |||
| 27 | #include <linux/hashtable.h> | ||
| 28 | #include <linux/kernel.h> | ||
| 29 | |||
| 30 | #define STATE_FP_SAVED 0x1 | ||
| 31 | #define STATE_FP_SETUP 0x2 | ||
| 32 | #define STATE_FENTRY 0x4 | ||
| 33 | |||
| 34 | struct alternative { | ||
| 35 | struct list_head list; | ||
| 36 | struct instruction *insn; | ||
| 37 | }; | ||
| 38 | |||
| 39 | const char *objname; | ||
| 40 | static bool nofp; | ||
| 41 | |||
| 42 | static struct instruction *find_insn(struct objtool_file *file, | ||
| 43 | struct section *sec, unsigned long offset) | ||
| 44 | { | ||
| 45 | struct instruction *insn; | ||
| 46 | |||
| 47 | hash_for_each_possible(file->insn_hash, insn, hash, offset) | ||
| 48 | if (insn->sec == sec && insn->offset == offset) | ||
| 49 | return insn; | ||
| 50 | |||
| 51 | return NULL; | ||
| 52 | } | ||
| 53 | |||
| 54 | static struct instruction *next_insn_same_sec(struct objtool_file *file, | ||
| 55 | struct instruction *insn) | ||
| 56 | { | ||
| 57 | struct instruction *next = list_next_entry(insn, list); | ||
| 58 | |||
| 59 | if (&next->list == &file->insn_list || next->sec != insn->sec) | ||
| 60 | return NULL; | ||
| 61 | |||
| 62 | return next; | ||
| 63 | } | ||
| 64 | |||
| 65 | static bool gcov_enabled(struct objtool_file *file) | ||
| 66 | { | ||
| 67 | struct section *sec; | ||
| 68 | struct symbol *sym; | ||
| 69 | |||
| 70 | list_for_each_entry(sec, &file->elf->sections, list) | ||
| 71 | list_for_each_entry(sym, &sec->symbol_list, list) | ||
| 72 | if (!strncmp(sym->name, "__gcov_.", 8)) | ||
| 73 | return true; | ||
| 74 | |||
| 75 | return false; | ||
| 76 | } | ||
| 77 | |||
| 78 | #define for_each_insn(file, insn) \ | ||
| 79 | list_for_each_entry(insn, &file->insn_list, list) | ||
| 80 | |||
| 81 | #define func_for_each_insn(file, func, insn) \ | ||
| 82 | for (insn = find_insn(file, func->sec, func->offset); \ | ||
| 83 | insn && &insn->list != &file->insn_list && \ | ||
| 84 | insn->sec == func->sec && \ | ||
| 85 | insn->offset < func->offset + func->len; \ | ||
| 86 | insn = list_next_entry(insn, list)) | ||
| 87 | |||
| 88 | #define func_for_each_insn_continue_reverse(file, func, insn) \ | ||
| 89 | for (insn = list_prev_entry(insn, list); \ | ||
| 90 | &insn->list != &file->insn_list && \ | ||
| 91 | insn->sec == func->sec && insn->offset >= func->offset; \ | ||
| 92 | insn = list_prev_entry(insn, list)) | ||
| 93 | |||
| 94 | #define sec_for_each_insn_from(file, insn) \ | ||
| 95 | for (; insn; insn = next_insn_same_sec(file, insn)) | ||
| 96 | |||
| 97 | |||
| 98 | /* | ||
| 99 | * Check if the function has been manually whitelisted with the | ||
| 100 | * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted | ||
| 101 | * due to its use of a context switching instruction. | ||
| 102 | */ | ||
| 103 | static bool ignore_func(struct objtool_file *file, struct symbol *func) | ||
| 104 | { | ||
| 105 | struct rela *rela; | ||
| 106 | struct instruction *insn; | ||
| 107 | |||
| 108 | /* check for STACK_FRAME_NON_STANDARD */ | ||
| 109 | if (file->whitelist && file->whitelist->rela) | ||
| 110 | list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) { | ||
| 111 | if (rela->sym->type == STT_SECTION && | ||
| 112 | rela->sym->sec == func->sec && | ||
| 113 | rela->addend == func->offset) | ||
| 114 | return true; | ||
| 115 | if (rela->sym->type == STT_FUNC && rela->sym == func) | ||
| 116 | return true; | ||
| 117 | } | ||
| 118 | |||
| 119 | /* check if it has a context switching instruction */ | ||
| 120 | func_for_each_insn(file, func, insn) | ||
| 121 | if (insn->type == INSN_CONTEXT_SWITCH) | ||
| 122 | return true; | ||
| 123 | |||
| 124 | return false; | ||
| 125 | } | ||
| 126 | |||
| 127 | /* | ||
| 128 | * This checks to see if the given function is a "noreturn" function. | ||
| 129 | * | ||
| 130 | * For global functions which are outside the scope of this object file, we | ||
| 131 | * have to keep a manual list of them. | ||
| 132 | * | ||
| 133 | * For local functions, we have to detect them manually by simply looking for | ||
| 134 | * the lack of a return instruction. | ||
| 135 | * | ||
| 136 | * Returns: | ||
| 137 | * -1: error | ||
| 138 | * 0: no dead end | ||
| 139 | * 1: dead end | ||
| 140 | */ | ||
| 141 | static int __dead_end_function(struct objtool_file *file, struct symbol *func, | ||
| 142 | int recursion) | ||
| 143 | { | ||
| 144 | int i; | ||
| 145 | struct instruction *insn; | ||
| 146 | bool empty = true; | ||
| 147 | |||
| 148 | /* | ||
| 149 | * Unfortunately these have to be hard coded because the noreturn | ||
| 150 | * attribute isn't provided in ELF data. | ||
| 151 | */ | ||
| 152 | static const char * const global_noreturns[] = { | ||
| 153 | "__stack_chk_fail", | ||
| 154 | "panic", | ||
| 155 | "do_exit", | ||
| 156 | "do_task_dead", | ||
| 157 | "__module_put_and_exit", | ||
| 158 | "complete_and_exit", | ||
| 159 | "kvm_spurious_fault", | ||
| 160 | "__reiserfs_panic", | ||
| 161 | "lbug_with_loc", | ||
| 162 | "fortify_panic", | ||
| 163 | }; | ||
| 164 | |||
| 165 | if (func->bind == STB_WEAK) | ||
| 166 | return 0; | ||
| 167 | |||
| 168 | if (func->bind == STB_GLOBAL) | ||
| 169 | for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) | ||
| 170 | if (!strcmp(func->name, global_noreturns[i])) | ||
| 171 | return 1; | ||
| 172 | |||
| 173 | if (!func->sec) | ||
| 174 | return 0; | ||
| 175 | |||
| 176 | func_for_each_insn(file, func, insn) { | ||
| 177 | empty = false; | ||
| 178 | |||
| 179 | if (insn->type == INSN_RETURN) | ||
| 180 | return 0; | ||
| 181 | } | ||
| 182 | |||
| 183 | if (empty) | ||
| 184 | return 0; | ||
| 185 | |||
| 186 | /* | ||
| 187 | * A function can have a sibling call instead of a return. In that | ||
| 188 | * case, the function's dead-end status depends on whether the target | ||
| 189 | * of the sibling call returns. | ||
| 190 | */ | ||
| 191 | func_for_each_insn(file, func, insn) { | ||
| 192 | if (insn->sec != func->sec || | ||
| 193 | insn->offset >= func->offset + func->len) | ||
| 194 | break; | ||
| 195 | |||
| 196 | if (insn->type == INSN_JUMP_UNCONDITIONAL) { | ||
| 197 | struct instruction *dest = insn->jump_dest; | ||
| 198 | struct symbol *dest_func; | ||
| 199 | |||
| 200 | if (!dest) | ||
| 201 | /* sibling call to another file */ | ||
| 202 | return 0; | ||
| 203 | |||
| 204 | if (dest->sec != func->sec || | ||
| 205 | dest->offset < func->offset || | ||
| 206 | dest->offset >= func->offset + func->len) { | ||
| 207 | /* local sibling call */ | ||
| 208 | dest_func = find_symbol_by_offset(dest->sec, | ||
| 209 | dest->offset); | ||
| 210 | if (!dest_func) | ||
| 211 | continue; | ||
| 212 | |||
| 213 | if (recursion == 5) { | ||
| 214 | WARN_FUNC("infinite recursion (objtool bug!)", | ||
| 215 | dest->sec, dest->offset); | ||
| 216 | return -1; | ||
| 217 | } | ||
| 218 | |||
| 219 | return __dead_end_function(file, dest_func, | ||
| 220 | recursion + 1); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts)) | ||
| 225 | /* sibling call */ | ||
| 226 | return 0; | ||
| 227 | } | ||
| 228 | |||
| 229 | return 1; | ||
| 230 | } | ||
| 231 | |||
| 232 | static int dead_end_function(struct objtool_file *file, struct symbol *func) | ||
| 233 | { | ||
| 234 | return __dead_end_function(file, func, 0); | ||
| 235 | } | ||
| 236 | |||
| 237 | /* | ||
| 238 | * Call the arch-specific instruction decoder for all the instructions and add | ||
| 239 | * them to the global instruction list. | ||
| 240 | */ | ||
| 241 | static int decode_instructions(struct objtool_file *file) | ||
| 242 | { | ||
| 243 | struct section *sec; | ||
| 244 | struct symbol *func; | ||
| 245 | unsigned long offset; | ||
| 246 | struct instruction *insn; | ||
| 247 | int ret; | ||
| 248 | |||
| 249 | list_for_each_entry(sec, &file->elf->sections, list) { | ||
| 250 | |||
| 251 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) | ||
| 252 | continue; | ||
| 253 | |||
| 254 | for (offset = 0; offset < sec->len; offset += insn->len) { | ||
| 255 | insn = malloc(sizeof(*insn)); | ||
| 256 | memset(insn, 0, sizeof(*insn)); | ||
| 257 | |||
| 258 | INIT_LIST_HEAD(&insn->alts); | ||
| 259 | insn->sec = sec; | ||
| 260 | insn->offset = offset; | ||
| 261 | |||
| 262 | ret = arch_decode_instruction(file->elf, sec, offset, | ||
| 263 | sec->len - offset, | ||
| 264 | &insn->len, &insn->type, | ||
| 265 | &insn->immediate); | ||
| 266 | if (ret) | ||
| 267 | return ret; | ||
| 268 | |||
| 269 | if (!insn->type || insn->type > INSN_LAST) { | ||
| 270 | WARN_FUNC("invalid instruction type %d", | ||
| 271 | insn->sec, insn->offset, insn->type); | ||
| 272 | return -1; | ||
| 273 | } | ||
| 274 | |||
| 275 | hash_add(file->insn_hash, &insn->hash, insn->offset); | ||
| 276 | list_add_tail(&insn->list, &file->insn_list); | ||
| 277 | } | ||
| 278 | |||
| 279 | list_for_each_entry(func, &sec->symbol_list, list) { | ||
| 280 | if (func->type != STT_FUNC) | ||
| 281 | continue; | ||
| 282 | |||
| 283 | if (!find_insn(file, sec, func->offset)) { | ||
| 284 | WARN("%s(): can't find starting instruction", | ||
| 285 | func->name); | ||
| 286 | return -1; | ||
| 287 | } | ||
| 288 | |||
| 289 | func_for_each_insn(file, func, insn) | ||
| 290 | if (!insn->func) | ||
| 291 | insn->func = func; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | return 0; | ||
| 296 | } | ||
| 297 | |||
| 298 | /* | ||
| 299 | * Find all uses of the unreachable() macro, which are code path dead ends. | ||
| 300 | */ | ||
| 301 | static int add_dead_ends(struct objtool_file *file) | ||
| 302 | { | ||
| 303 | struct section *sec; | ||
| 304 | struct rela *rela; | ||
| 305 | struct instruction *insn; | ||
| 306 | bool found; | ||
| 307 | |||
| 308 | sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); | ||
| 309 | if (!sec) | ||
| 310 | return 0; | ||
| 311 | |||
| 312 | list_for_each_entry(rela, &sec->rela_list, list) { | ||
| 313 | if (rela->sym->type != STT_SECTION) { | ||
| 314 | WARN("unexpected relocation symbol type in %s", sec->name); | ||
| 315 | return -1; | ||
| 316 | } | ||
| 317 | insn = find_insn(file, rela->sym->sec, rela->addend); | ||
| 318 | if (insn) | ||
| 319 | insn = list_prev_entry(insn, list); | ||
| 320 | else if (rela->addend == rela->sym->sec->len) { | ||
| 321 | found = false; | ||
| 322 | list_for_each_entry_reverse(insn, &file->insn_list, list) { | ||
| 323 | if (insn->sec == rela->sym->sec) { | ||
| 324 | found = true; | ||
| 325 | break; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | if (!found) { | ||
| 330 | WARN("can't find unreachable insn at %s+0x%x", | ||
| 331 | rela->sym->sec->name, rela->addend); | ||
| 332 | return -1; | ||
| 333 | } | ||
| 334 | } else { | ||
| 335 | WARN("can't find unreachable insn at %s+0x%x", | ||
| 336 | rela->sym->sec->name, rela->addend); | ||
| 337 | return -1; | ||
| 338 | } | ||
| 339 | |||
| 340 | insn->dead_end = true; | ||
| 341 | } | ||
| 342 | |||
| 343 | return 0; | ||
| 344 | } | ||
| 345 | |||
| 346 | /* | ||
| 347 | * Warnings shouldn't be reported for ignored functions. | ||
| 348 | */ | ||
| 349 | static void add_ignores(struct objtool_file *file) | ||
| 350 | { | ||
| 351 | struct instruction *insn; | ||
| 352 | struct section *sec; | ||
| 353 | struct symbol *func; | ||
| 354 | |||
| 355 | list_for_each_entry(sec, &file->elf->sections, list) { | ||
| 356 | list_for_each_entry(func, &sec->symbol_list, list) { | ||
| 357 | if (func->type != STT_FUNC) | ||
| 358 | continue; | ||
| 359 | |||
| 360 | if (!ignore_func(file, func)) | ||
| 361 | continue; | ||
| 362 | |||
| 363 | func_for_each_insn(file, func, insn) | ||
| 364 | insn->visited = true; | ||
| 365 | } | ||
| 366 | } | ||
| 367 | } | ||
| 368 | |||
| 369 | /* | ||
| 370 | * Find the destination instructions for all jumps. | ||
| 371 | */ | ||
| 372 | static int add_jump_destinations(struct objtool_file *file) | ||
| 373 | { | ||
| 374 | struct instruction *insn; | ||
| 375 | struct rela *rela; | ||
| 376 | struct section *dest_sec; | ||
| 377 | unsigned long dest_off; | ||
| 378 | |||
| 379 | for_each_insn(file, insn) { | ||
| 380 | if (insn->type != INSN_JUMP_CONDITIONAL && | ||
| 381 | insn->type != INSN_JUMP_UNCONDITIONAL) | ||
| 382 | continue; | ||
| 383 | |||
| 384 | /* skip ignores */ | ||
| 385 | if (insn->visited) | ||
| 386 | continue; | ||
| 387 | |||
| 388 | rela = find_rela_by_dest_range(insn->sec, insn->offset, | ||
| 389 | insn->len); | ||
| 390 | if (!rela) { | ||
| 391 | dest_sec = insn->sec; | ||
| 392 | dest_off = insn->offset + insn->len + insn->immediate; | ||
| 393 | } else if (rela->sym->type == STT_SECTION) { | ||
| 394 | dest_sec = rela->sym->sec; | ||
| 395 | dest_off = rela->addend + 4; | ||
| 396 | } else if (rela->sym->sec->idx) { | ||
| 397 | dest_sec = rela->sym->sec; | ||
| 398 | dest_off = rela->sym->sym.st_value + rela->addend + 4; | ||
| 399 | } else { | ||
| 400 | /* sibling call */ | ||
| 401 | insn->jump_dest = 0; | ||
| 402 | continue; | ||
| 403 | } | ||
| 404 | |||
| 405 | insn->jump_dest = find_insn(file, dest_sec, dest_off); | ||
| 406 | if (!insn->jump_dest) { | ||
| 407 | |||
| 408 | /* | ||
| 409 | * This is a special case where an alt instruction | ||
| 410 | * jumps past the end of the section. These are | ||
| 411 | * handled later in handle_group_alt(). | ||
| 412 | */ | ||
| 413 | if (!strcmp(insn->sec->name, ".altinstr_replacement")) | ||
| 414 | continue; | ||
| 415 | |||
| 416 | WARN_FUNC("can't find jump dest instruction at %s+0x%lx", | ||
| 417 | insn->sec, insn->offset, dest_sec->name, | ||
| 418 | dest_off); | ||
| 419 | return -1; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | return 0; | ||
| 424 | } | ||
| 425 | |||
| 426 | /* | ||
| 427 | * Find the destination instructions for all calls. | ||
| 428 | */ | ||
| 429 | static int add_call_destinations(struct objtool_file *file) | ||
| 430 | { | ||
| 431 | struct instruction *insn; | ||
| 432 | unsigned long dest_off; | ||
| 433 | struct rela *rela; | ||
| 434 | |||
| 435 | for_each_insn(file, insn) { | ||
| 436 | if (insn->type != INSN_CALL) | ||
| 437 | continue; | ||
| 438 | |||
| 439 | rela = find_rela_by_dest_range(insn->sec, insn->offset, | ||
| 440 | insn->len); | ||
| 441 | if (!rela) { | ||
| 442 | dest_off = insn->offset + insn->len + insn->immediate; | ||
| 443 | insn->call_dest = find_symbol_by_offset(insn->sec, | ||
| 444 | dest_off); | ||
| 445 | if (!insn->call_dest) { | ||
| 446 | WARN_FUNC("can't find call dest symbol at offset 0x%lx", | ||
| 447 | insn->sec, insn->offset, dest_off); | ||
| 448 | return -1; | ||
| 449 | } | ||
| 450 | } else if (rela->sym->type == STT_SECTION) { | ||
| 451 | insn->call_dest = find_symbol_by_offset(rela->sym->sec, | ||
| 452 | rela->addend+4); | ||
| 453 | if (!insn->call_dest || | ||
| 454 | insn->call_dest->type != STT_FUNC) { | ||
| 455 | WARN_FUNC("can't find call dest symbol at %s+0x%x", | ||
| 456 | insn->sec, insn->offset, | ||
| 457 | rela->sym->sec->name, | ||
| 458 | rela->addend + 4); | ||
| 459 | return -1; | ||
| 460 | } | ||
| 461 | } else | ||
| 462 | insn->call_dest = rela->sym; | ||
| 463 | } | ||
| 464 | |||
| 465 | return 0; | ||
| 466 | } | ||
| 467 | |||
| 468 | /* | ||
| 469 | * The .alternatives section requires some extra special care, over and above | ||
| 470 | * what other special sections require: | ||
| 471 | * | ||
| 472 | * 1. Because alternatives are patched in-place, we need to insert a fake jump | ||
| 473 | * instruction at the end so that validate_branch() skips all the original | ||
| 474 | * replaced instructions when validating the new instruction path. | ||
| 475 | * | ||
| 476 | * 2. An added wrinkle is that the new instruction length might be zero. In | ||
| 477 | * that case the old instructions are replaced with noops. We simulate that | ||
| 478 | * by creating a fake jump as the only new instruction. | ||
| 479 | * | ||
| 480 | * 3. In some cases, the alternative section includes an instruction which | ||
| 481 | * conditionally jumps to the _end_ of the entry. We have to modify these | ||
| 482 | * jumps' destinations to point back to .text rather than the end of the | ||
| 483 | * entry in .altinstr_replacement. | ||
| 484 | * | ||
| 485 | * 4. It has been requested that we don't validate the !POPCNT feature path | ||
| 486 | * which is a "very very small percentage of machines". | ||
| 487 | */ | ||
| 488 | static int handle_group_alt(struct objtool_file *file, | ||
| 489 | struct special_alt *special_alt, | ||
| 490 | struct instruction *orig_insn, | ||
| 491 | struct instruction **new_insn) | ||
| 492 | { | ||
| 493 | struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump; | ||
| 494 | unsigned long dest_off; | ||
| 495 | |||
| 496 | last_orig_insn = NULL; | ||
| 497 | insn = orig_insn; | ||
| 498 | sec_for_each_insn_from(file, insn) { | ||
| 499 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) | ||
| 500 | break; | ||
| 501 | |||
| 502 | if (special_alt->skip_orig) | ||
| 503 | insn->type = INSN_NOP; | ||
| 504 | |||
| 505 | insn->alt_group = true; | ||
| 506 | last_orig_insn = insn; | ||
| 507 | } | ||
| 508 | |||
| 509 | if (!next_insn_same_sec(file, last_orig_insn)) { | ||
| 510 | WARN("%s: don't know how to handle alternatives at end of section", | ||
| 511 | special_alt->orig_sec->name); | ||
| 512 | return -1; | ||
| 513 | } | ||
| 514 | |||
| 515 | fake_jump = malloc(sizeof(*fake_jump)); | ||
| 516 | if (!fake_jump) { | ||
| 517 | WARN("malloc failed"); | ||
| 518 | return -1; | ||
| 519 | } | ||
| 520 | memset(fake_jump, 0, sizeof(*fake_jump)); | ||
| 521 | INIT_LIST_HEAD(&fake_jump->alts); | ||
| 522 | fake_jump->sec = special_alt->new_sec; | ||
| 523 | fake_jump->offset = -1; | ||
| 524 | fake_jump->type = INSN_JUMP_UNCONDITIONAL; | ||
| 525 | fake_jump->jump_dest = list_next_entry(last_orig_insn, list); | ||
| 526 | |||
| 527 | if (!special_alt->new_len) { | ||
| 528 | *new_insn = fake_jump; | ||
| 529 | return 0; | ||
| 530 | } | ||
| 531 | |||
| 532 | last_new_insn = NULL; | ||
| 533 | insn = *new_insn; | ||
| 534 | sec_for_each_insn_from(file, insn) { | ||
| 535 | if (insn->offset >= special_alt->new_off + special_alt->new_len) | ||
| 536 | break; | ||
| 537 | |||
| 538 | last_new_insn = insn; | ||
| 539 | |||
| 540 | if (insn->type != INSN_JUMP_CONDITIONAL && | ||
| 541 | insn->type != INSN_JUMP_UNCONDITIONAL) | ||
| 542 | continue; | ||
| 543 | |||
| 544 | if (!insn->immediate) | ||
| 545 | continue; | ||
| 546 | |||
| 547 | dest_off = insn->offset + insn->len + insn->immediate; | ||
| 548 | if (dest_off == special_alt->new_off + special_alt->new_len) | ||
| 549 | insn->jump_dest = fake_jump; | ||
| 550 | |||
| 551 | if (!insn->jump_dest) { | ||
| 552 | WARN_FUNC("can't find alternative jump destination", | ||
| 553 | insn->sec, insn->offset); | ||
| 554 | return -1; | ||
| 555 | } | ||
| 556 | } | ||
| 557 | |||
| 558 | if (!last_new_insn) { | ||
| 559 | WARN_FUNC("can't find last new alternative instruction", | ||
| 560 | special_alt->new_sec, special_alt->new_off); | ||
| 561 | return -1; | ||
| 562 | } | ||
| 563 | |||
| 564 | list_add(&fake_jump->list, &last_new_insn->list); | ||
| 565 | |||
| 566 | return 0; | ||
| 567 | } | ||
| 568 | |||
| 569 | /* | ||
| 570 | * A jump table entry can either convert a nop to a jump or a jump to a nop. | ||
| 571 | * If the original instruction is a jump, make the alt entry an effective nop | ||
| 572 | * by just skipping the original instruction. | ||
| 573 | */ | ||
| 574 | static int handle_jump_alt(struct objtool_file *file, | ||
| 575 | struct special_alt *special_alt, | ||
| 576 | struct instruction *orig_insn, | ||
| 577 | struct instruction **new_insn) | ||
| 578 | { | ||
| 579 | if (orig_insn->type == INSN_NOP) | ||
| 580 | return 0; | ||
| 581 | |||
| 582 | if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { | ||
| 583 | WARN_FUNC("unsupported instruction at jump label", | ||
| 584 | orig_insn->sec, orig_insn->offset); | ||
| 585 | return -1; | ||
| 586 | } | ||
| 587 | |||
| 588 | *new_insn = list_next_entry(orig_insn, list); | ||
| 589 | return 0; | ||
| 590 | } | ||
| 591 | |||
| 592 | /* | ||
| 593 | * Read all the special sections which have alternate instructions which can be | ||
| 594 | * patched in or redirected to at runtime. Each instruction having alternate | ||
| 595 | * instruction(s) has them added to its insn->alts list, which will be | ||
| 596 | * traversed in validate_branch(). | ||
| 597 | */ | ||
| 598 | static int add_special_section_alts(struct objtool_file *file) | ||
| 599 | { | ||
| 600 | struct list_head special_alts; | ||
| 601 | struct instruction *orig_insn, *new_insn; | ||
| 602 | struct special_alt *special_alt, *tmp; | ||
| 603 | struct alternative *alt; | ||
| 604 | int ret; | ||
| 605 | |||
| 606 | ret = special_get_alts(file->elf, &special_alts); | ||
| 607 | if (ret) | ||
| 608 | return ret; | ||
| 609 | |||
| 610 | list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { | ||
| 611 | alt = malloc(sizeof(*alt)); | ||
| 612 | if (!alt) { | ||
| 613 | WARN("malloc failed"); | ||
| 614 | ret = -1; | ||
| 615 | goto out; | ||
| 616 | } | ||
| 617 | |||
| 618 | orig_insn = find_insn(file, special_alt->orig_sec, | ||
| 619 | special_alt->orig_off); | ||
| 620 | if (!orig_insn) { | ||
| 621 | WARN_FUNC("special: can't find orig instruction", | ||
| 622 | special_alt->orig_sec, special_alt->orig_off); | ||
| 623 | ret = -1; | ||
| 624 | goto out; | ||
| 625 | } | ||
| 626 | |||
| 627 | new_insn = NULL; | ||
| 628 | if (!special_alt->group || special_alt->new_len) { | ||
| 629 | new_insn = find_insn(file, special_alt->new_sec, | ||
| 630 | special_alt->new_off); | ||
| 631 | if (!new_insn) { | ||
| 632 | WARN_FUNC("special: can't find new instruction", | ||
| 633 | special_alt->new_sec, | ||
| 634 | special_alt->new_off); | ||
| 635 | ret = -1; | ||
| 636 | goto out; | ||
| 637 | } | ||
| 638 | } | ||
| 639 | |||
| 640 | if (special_alt->group) { | ||
| 641 | ret = handle_group_alt(file, special_alt, orig_insn, | ||
| 642 | &new_insn); | ||
| 643 | if (ret) | ||
| 644 | goto out; | ||
| 645 | } else if (special_alt->jump_or_nop) { | ||
| 646 | ret = handle_jump_alt(file, special_alt, orig_insn, | ||
| 647 | &new_insn); | ||
| 648 | if (ret) | ||
| 649 | goto out; | ||
| 650 | } | ||
| 651 | |||
| 652 | alt->insn = new_insn; | ||
| 653 | list_add_tail(&alt->list, &orig_insn->alts); | ||
| 654 | |||
| 655 | list_del(&special_alt->list); | ||
| 656 | free(special_alt); | ||
| 657 | } | ||
| 658 | |||
| 659 | out: | ||
| 660 | return ret; | ||
| 661 | } | ||
| 662 | |||
| 663 | static int add_switch_table(struct objtool_file *file, struct symbol *func, | ||
| 664 | struct instruction *insn, struct rela *table, | ||
| 665 | struct rela *next_table) | ||
| 666 | { | ||
| 667 | struct rela *rela = table; | ||
| 668 | struct instruction *alt_insn; | ||
| 669 | struct alternative *alt; | ||
| 670 | |||
| 671 | list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) { | ||
| 672 | if (rela == next_table) | ||
| 673 | break; | ||
| 674 | |||
| 675 | if (rela->sym->sec != insn->sec || | ||
| 676 | rela->addend <= func->offset || | ||
| 677 | rela->addend >= func->offset + func->len) | ||
| 678 | break; | ||
| 679 | |||
| 680 | alt_insn = find_insn(file, insn->sec, rela->addend); | ||
| 681 | if (!alt_insn) { | ||
| 682 | WARN("%s: can't find instruction at %s+0x%x", | ||
| 683 | file->rodata->rela->name, insn->sec->name, | ||
| 684 | rela->addend); | ||
| 685 | return -1; | ||
| 686 | } | ||
| 687 | |||
| 688 | alt = malloc(sizeof(*alt)); | ||
| 689 | if (!alt) { | ||
| 690 | WARN("malloc failed"); | ||
| 691 | return -1; | ||
| 692 | } | ||
| 693 | |||
| 694 | alt->insn = alt_insn; | ||
| 695 | list_add_tail(&alt->list, &insn->alts); | ||
| 696 | } | ||
| 697 | |||
| 698 | return 0; | ||
| 699 | } | ||
| 700 | |||
| 701 | /* | ||
| 702 | * find_switch_table() - Given a dynamic jump, find the switch jump table in | ||
| 703 | * .rodata associated with it. | ||
| 704 | * | ||
| 705 | * There are 3 basic patterns: | ||
| 706 | * | ||
| 707 | * 1. jmpq *[rodata addr](,%reg,8) | ||
| 708 | * | ||
| 709 | * This is the most common case by far. It jumps to an address in a simple | ||
| 710 | * jump table which is stored in .rodata. | ||
| 711 | * | ||
| 712 | * 2. jmpq *[rodata addr](%rip) | ||
| 713 | * | ||
| 714 | * This is caused by a rare GCC quirk, currently only seen in three driver | ||
| 715 | * functions in the kernel, only with certain obscure non-distro configs. | ||
| 716 | * | ||
| 717 | * As part of an optimization, GCC makes a copy of an existing switch jump | ||
| 718 | * table, modifies it, and then hard-codes the jump (albeit with an indirect | ||
| 719 | * jump) to use a single entry in the table. The rest of the jump table and | ||
| 720 | * some of its jump targets remain as dead code. | ||
| 721 | * | ||
| 722 | * In such a case we can just crudely ignore all unreachable instruction | ||
| 723 | * warnings for the entire object file. Ideally we would just ignore them | ||
| 724 | * for the function, but that would require redesigning the code quite a | ||
| 725 | * bit. And honestly that's just not worth doing: unreachable instruction | ||
| 726 | * warnings are of questionable value anyway, and this is such a rare issue. | ||
| 727 | * | ||
| 728 | * 3. mov [rodata addr],%reg1 | ||
| 729 | * ... some instructions ... | ||
| 730 | * jmpq *(%reg1,%reg2,8) | ||
| 731 | * | ||
| 732 | * This is a fairly uncommon pattern which is new for GCC 6. As of this | ||
| 733 | * writing, there are 11 occurrences of it in the allmodconfig kernel. | ||
| 734 | * | ||
| 735 | * TODO: Once we have DWARF CFI and smarter instruction decoding logic, | ||
| 736 | * ensure the same register is used in the mov and jump instructions. | ||
| 737 | */ | ||
| 738 | static struct rela *find_switch_table(struct objtool_file *file, | ||
| 739 | struct symbol *func, | ||
| 740 | struct instruction *insn) | ||
| 741 | { | ||
| 742 | struct rela *text_rela, *rodata_rela; | ||
| 743 | struct instruction *orig_insn = insn; | ||
| 744 | |||
| 745 | text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len); | ||
| 746 | if (text_rela && text_rela->sym == file->rodata->sym) { | ||
| 747 | /* case 1 */ | ||
| 748 | rodata_rela = find_rela_by_dest(file->rodata, | ||
| 749 | text_rela->addend); | ||
| 750 | if (rodata_rela) | ||
| 751 | return rodata_rela; | ||
| 752 | |||
| 753 | /* case 2 */ | ||
| 754 | rodata_rela = find_rela_by_dest(file->rodata, | ||
| 755 | text_rela->addend + 4); | ||
| 756 | if (!rodata_rela) | ||
| 757 | return NULL; | ||
| 758 | file->ignore_unreachables = true; | ||
| 759 | return rodata_rela; | ||
| 760 | } | ||
| 761 | |||
| 762 | /* case 3 */ | ||
| 763 | func_for_each_insn_continue_reverse(file, func, insn) { | ||
| 764 | if (insn->type == INSN_JUMP_DYNAMIC) | ||
| 765 | break; | ||
| 766 | |||
| 767 | /* allow small jumps within the range */ | ||
| 768 | if (insn->type == INSN_JUMP_UNCONDITIONAL && | ||
| 769 | insn->jump_dest && | ||
| 770 | (insn->jump_dest->offset <= insn->offset || | ||
| 771 | insn->jump_dest->offset > orig_insn->offset)) | ||
| 772 | break; | ||
| 773 | |||
| 774 | /* look for a relocation which references .rodata */ | ||
| 775 | text_rela = find_rela_by_dest_range(insn->sec, insn->offset, | ||
| 776 | insn->len); | ||
| 777 | if (!text_rela || text_rela->sym != file->rodata->sym) | ||
| 778 | continue; | ||
| 779 | |||
| 780 | /* | ||
| 781 | * Make sure the .rodata address isn't associated with a | ||
| 782 | * symbol. gcc jump tables are anonymous data. | ||
| 783 | */ | ||
| 784 | if (find_symbol_containing(file->rodata, text_rela->addend)) | ||
| 785 | continue; | ||
| 786 | |||
| 787 | return find_rela_by_dest(file->rodata, text_rela->addend); | ||
| 788 | } | ||
| 789 | |||
| 790 | return NULL; | ||
| 791 | } | ||
| 792 | |||
| 793 | static int add_func_switch_tables(struct objtool_file *file, | ||
| 794 | struct symbol *func) | ||
| 795 | { | ||
| 796 | struct instruction *insn, *prev_jump = NULL; | ||
| 797 | struct rela *rela, *prev_rela = NULL; | ||
| 798 | int ret; | ||
| 799 | |||
| 800 | func_for_each_insn(file, func, insn) { | ||
| 801 | if (insn->type != INSN_JUMP_DYNAMIC) | ||
| 802 | continue; | ||
| 803 | |||
| 804 | rela = find_switch_table(file, func, insn); | ||
| 805 | if (!rela) | ||
| 806 | continue; | ||
| 807 | |||
| 808 | /* | ||
| 809 | * We found a switch table, but we don't know yet how big it | ||
| 810 | * is. Don't add it until we reach the end of the function or | ||
| 811 | * the beginning of another switch table in the same function. | ||
| 812 | */ | ||
| 813 | if (prev_jump) { | ||
| 814 | ret = add_switch_table(file, func, prev_jump, prev_rela, | ||
| 815 | rela); | ||
| 816 | if (ret) | ||
| 817 | return ret; | ||
| 818 | } | ||
| 819 | |||
| 820 | prev_jump = insn; | ||
| 821 | prev_rela = rela; | ||
| 822 | } | ||
| 823 | |||
| 824 | if (prev_jump) { | ||
| 825 | ret = add_switch_table(file, func, prev_jump, prev_rela, NULL); | ||
| 826 | if (ret) | ||
| 827 | return ret; | ||
| 828 | } | ||
| 829 | |||
| 830 | return 0; | ||
| 831 | } | ||
| 832 | |||
| 833 | /* | ||
| 834 | * For some switch statements, gcc generates a jump table in the .rodata | ||
| 835 | * section which contains a list of addresses within the function to jump to. | ||
| 836 | * This finds these jump tables and adds them to the insn->alts lists. | ||
| 837 | */ | ||
| 838 | static int add_switch_table_alts(struct objtool_file *file) | ||
| 839 | { | ||
| 840 | struct section *sec; | ||
| 841 | struct symbol *func; | ||
| 842 | int ret; | ||
| 843 | |||
| 844 | if (!file->rodata || !file->rodata->rela) | ||
| 845 | return 0; | ||
| 846 | |||
| 847 | list_for_each_entry(sec, &file->elf->sections, list) { | ||
| 848 | list_for_each_entry(func, &sec->symbol_list, list) { | ||
| 849 | if (func->type != STT_FUNC) | ||
| 850 | continue; | ||
| 851 | |||
| 852 | ret = add_func_switch_tables(file, func); | ||
| 853 | if (ret) | ||
| 854 | return ret; | ||
| 855 | } | ||
| 856 | } | ||
| 857 | |||
| 858 | return 0; | ||
| 859 | } | ||
| 860 | |||
| 861 | static int decode_sections(struct objtool_file *file) | ||
| 862 | { | ||
| 863 | int ret; | ||
| 864 | |||
| 865 | ret = decode_instructions(file); | ||
| 866 | if (ret) | ||
| 867 | return ret; | ||
| 868 | |||
| 869 | ret = add_dead_ends(file); | ||
| 870 | if (ret) | ||
| 871 | return ret; | ||
| 872 | |||
| 873 | add_ignores(file); | ||
| 874 | |||
| 875 | ret = add_jump_destinations(file); | ||
| 876 | if (ret) | ||
| 877 | return ret; | ||
| 878 | |||
| 879 | ret = add_call_destinations(file); | ||
| 880 | if (ret) | ||
| 881 | return ret; | ||
| 882 | |||
| 883 | ret = add_special_section_alts(file); | ||
| 884 | if (ret) | ||
| 885 | return ret; | ||
| 886 | |||
| 887 | ret = add_switch_table_alts(file); | ||
| 888 | if (ret) | ||
| 889 | return ret; | ||
| 890 | |||
| 891 | return 0; | ||
| 892 | } | ||
| 893 | |||
| 894 | static bool is_fentry_call(struct instruction *insn) | ||
| 895 | { | ||
| 896 | if (insn->type == INSN_CALL && | ||
| 897 | insn->call_dest->type == STT_NOTYPE && | ||
| 898 | !strcmp(insn->call_dest->name, "__fentry__")) | ||
| 899 | return true; | ||
| 900 | |||
| 901 | return false; | ||
| 902 | } | ||
| 903 | |||
| 904 | static bool has_modified_stack_frame(struct instruction *insn) | ||
| 905 | { | ||
| 906 | return (insn->state & STATE_FP_SAVED) || | ||
| 907 | (insn->state & STATE_FP_SETUP); | ||
| 908 | } | ||
| 909 | |||
| 910 | static bool has_valid_stack_frame(struct instruction *insn) | ||
| 911 | { | ||
| 912 | return (insn->state & STATE_FP_SAVED) && | ||
| 913 | (insn->state & STATE_FP_SETUP); | ||
| 914 | } | ||
| 915 | |||
| 916 | static unsigned int frame_state(unsigned long state) | ||
| 917 | { | ||
| 918 | return (state & (STATE_FP_SAVED | STATE_FP_SETUP)); | ||
| 919 | } | ||
| 920 | |||
| 921 | /* | ||
| 922 | * Follow the branch starting at the given instruction, and recursively follow | ||
| 923 | * any other branches (jumps). Meanwhile, track the frame pointer state at | ||
| 924 | * each instruction and validate all the rules described in | ||
| 925 | * tools/objtool/Documentation/stack-validation.txt. | ||
| 926 | */ | ||
| 927 | static int validate_branch(struct objtool_file *file, | ||
| 928 | struct instruction *first, unsigned char first_state) | ||
| 929 | { | ||
| 930 | struct alternative *alt; | ||
| 931 | struct instruction *insn; | ||
| 932 | struct section *sec; | ||
| 933 | struct symbol *func = NULL; | ||
| 934 | unsigned char state; | ||
| 935 | int ret; | ||
| 936 | |||
| 937 | insn = first; | ||
| 938 | sec = insn->sec; | ||
| 939 | state = first_state; | ||
| 940 | |||
| 941 | if (insn->alt_group && list_empty(&insn->alts)) { | ||
| 942 | WARN_FUNC("don't know how to handle branch to middle of alternative instruction group", | ||
| 943 | sec, insn->offset); | ||
| 944 | return 1; | ||
| 945 | } | ||
| 946 | |||
| 947 | while (1) { | ||
| 948 | if (file->c_file && insn->func) { | ||
| 949 | if (func && func != insn->func) { | ||
| 950 | WARN("%s() falls through to next function %s()", | ||
| 951 | func->name, insn->func->name); | ||
| 952 | return 1; | ||
| 953 | } | ||
| 954 | |||
| 955 | func = insn->func; | ||
| 956 | } | ||
| 957 | |||
| 958 | if (insn->visited) { | ||
| 959 | if (frame_state(insn->state) != frame_state(state)) { | ||
| 960 | WARN_FUNC("frame pointer state mismatch", | ||
| 961 | sec, insn->offset); | ||
| 962 | return 1; | ||
| 963 | } | ||
| 964 | |||
| 965 | return 0; | ||
| 966 | } | ||
| 967 | |||
| 968 | insn->visited = true; | ||
| 969 | insn->state = state; | ||
| 970 | |||
| 971 | list_for_each_entry(alt, &insn->alts, list) { | ||
| 972 | ret = validate_branch(file, alt->insn, state); | ||
| 973 | if (ret) | ||
| 974 | return 1; | ||
| 975 | } | ||
| 976 | |||
| 977 | switch (insn->type) { | ||
| 978 | |||
| 979 | case INSN_FP_SAVE: | ||
| 980 | if (!nofp) { | ||
| 981 | if (state & STATE_FP_SAVED) { | ||
| 982 | WARN_FUNC("duplicate frame pointer save", | ||
| 983 | sec, insn->offset); | ||
| 984 | return 1; | ||
| 985 | } | ||
| 986 | state |= STATE_FP_SAVED; | ||
| 987 | } | ||
| 988 | break; | ||
| 989 | |||
| 990 | case INSN_FP_SETUP: | ||
| 991 | if (!nofp) { | ||
| 992 | if (state & STATE_FP_SETUP) { | ||
| 993 | WARN_FUNC("duplicate frame pointer setup", | ||
| 994 | sec, insn->offset); | ||
| 995 | return 1; | ||
| 996 | } | ||
| 997 | state |= STATE_FP_SETUP; | ||
| 998 | } | ||
| 999 | break; | ||
| 1000 | |||
| 1001 | case INSN_FP_RESTORE: | ||
| 1002 | if (!nofp) { | ||
| 1003 | if (has_valid_stack_frame(insn)) | ||
| 1004 | state &= ~STATE_FP_SETUP; | ||
| 1005 | |||
| 1006 | state &= ~STATE_FP_SAVED; | ||
| 1007 | } | ||
| 1008 | break; | ||
| 1009 | |||
| 1010 | case INSN_RETURN: | ||
| 1011 | if (!nofp && has_modified_stack_frame(insn)) { | ||
| 1012 | WARN_FUNC("return without frame pointer restore", | ||
| 1013 | sec, insn->offset); | ||
| 1014 | return 1; | ||
| 1015 | } | ||
| 1016 | return 0; | ||
| 1017 | |||
| 1018 | case INSN_CALL: | ||
| 1019 | if (is_fentry_call(insn)) { | ||
| 1020 | state |= STATE_FENTRY; | ||
| 1021 | break; | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | ret = dead_end_function(file, insn->call_dest); | ||
| 1025 | if (ret == 1) | ||
| 1026 | return 0; | ||
| 1027 | if (ret == -1) | ||
| 1028 | return 1; | ||
| 1029 | |||
| 1030 | /* fallthrough */ | ||
| 1031 | case INSN_CALL_DYNAMIC: | ||
| 1032 | if (!nofp && !has_valid_stack_frame(insn)) { | ||
| 1033 | WARN_FUNC("call without frame pointer save/setup", | ||
| 1034 | sec, insn->offset); | ||
| 1035 | return 1; | ||
| 1036 | } | ||
| 1037 | break; | ||
| 1038 | |||
| 1039 | case INSN_JUMP_CONDITIONAL: | ||
| 1040 | case INSN_JUMP_UNCONDITIONAL: | ||
| 1041 | if (insn->jump_dest) { | ||
| 1042 | ret = validate_branch(file, insn->jump_dest, | ||
| 1043 | state); | ||
| 1044 | if (ret) | ||
| 1045 | return 1; | ||
| 1046 | } else if (has_modified_stack_frame(insn)) { | ||
| 1047 | WARN_FUNC("sibling call from callable instruction with changed frame pointer", | ||
| 1048 | sec, insn->offset); | ||
| 1049 | return 1; | ||
| 1050 | } /* else it's a sibling call */ | ||
| 1051 | |||
| 1052 | if (insn->type == INSN_JUMP_UNCONDITIONAL) | ||
| 1053 | return 0; | ||
| 1054 | |||
| 1055 | break; | ||
| 1056 | |||
| 1057 | case INSN_JUMP_DYNAMIC: | ||
| 1058 | if (list_empty(&insn->alts) && | ||
| 1059 | has_modified_stack_frame(insn)) { | ||
| 1060 | WARN_FUNC("sibling call from callable instruction with changed frame pointer", | ||
| 1061 | sec, insn->offset); | ||
| 1062 | return 1; | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | return 0; | ||
| 1066 | |||
| 1067 | default: | ||
| 1068 | break; | ||
| 1069 | } | ||
| 1070 | |||
| 1071 | if (insn->dead_end) | ||
| 1072 | return 0; | ||
| 1073 | |||
| 1074 | insn = next_insn_same_sec(file, insn); | ||
| 1075 | if (!insn) { | ||
| 1076 | WARN("%s: unexpected end of section", sec->name); | ||
| 1077 | return 1; | ||
| 1078 | } | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | return 0; | ||
| 1082 | } | ||
| 1083 | |||
| 1084 | static bool is_kasan_insn(struct instruction *insn) | ||
| 1085 | { | ||
| 1086 | return (insn->type == INSN_CALL && | ||
| 1087 | !strcmp(insn->call_dest->name, "__asan_handle_no_return")); | ||
| 1088 | } | ||
| 1089 | |||
| 1090 | static bool is_ubsan_insn(struct instruction *insn) | ||
| 1091 | { | ||
| 1092 | return (insn->type == INSN_CALL && | ||
| 1093 | !strcmp(insn->call_dest->name, | ||
| 1094 | "__ubsan_handle_builtin_unreachable")); | ||
| 1095 | } | ||
| 1096 | |||
| 1097 | static bool ignore_unreachable_insn(struct symbol *func, | ||
| 1098 | struct instruction *insn) | ||
| 1099 | { | ||
| 1100 | int i; | ||
| 1101 | |||
| 1102 | if (insn->type == INSN_NOP) | ||
| 1103 | return true; | ||
| 1104 | |||
| 1105 | /* | ||
| 1106 | * Check if this (or a subsequent) instruction is related to | ||
| 1107 | * CONFIG_UBSAN or CONFIG_KASAN. | ||
| 1108 | * | ||
| 1109 | * End the search at 5 instructions to avoid going into the weeds. | ||
| 1110 | */ | ||
| 1111 | for (i = 0; i < 5; i++) { | ||
| 1112 | |||
| 1113 | if (is_kasan_insn(insn) || is_ubsan_insn(insn)) | ||
| 1114 | return true; | ||
| 1115 | |||
| 1116 | if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) { | ||
| 1117 | insn = insn->jump_dest; | ||
| 1118 | continue; | ||
| 1119 | } | ||
| 1120 | |||
| 1121 | if (insn->offset + insn->len >= func->offset + func->len) | ||
| 1122 | break; | ||
| 1123 | insn = list_next_entry(insn, list); | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | return false; | ||
| 1127 | } | ||
| 1128 | |||
| 1129 | static int validate_functions(struct objtool_file *file) | ||
| 1130 | { | ||
| 1131 | struct section *sec; | ||
| 1132 | struct symbol *func; | ||
| 1133 | struct instruction *insn; | ||
| 1134 | int ret, warnings = 0; | ||
| 1135 | |||
| 1136 | list_for_each_entry(sec, &file->elf->sections, list) { | ||
| 1137 | list_for_each_entry(func, &sec->symbol_list, list) { | ||
| 1138 | if (func->type != STT_FUNC) | ||
| 1139 | continue; | ||
| 1140 | |||
| 1141 | insn = find_insn(file, sec, func->offset); | ||
| 1142 | if (!insn) | ||
| 1143 | continue; | ||
| 1144 | |||
| 1145 | ret = validate_branch(file, insn, 0); | ||
| 1146 | warnings += ret; | ||
| 1147 | } | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | list_for_each_entry(sec, &file->elf->sections, list) { | ||
| 1151 | list_for_each_entry(func, &sec->symbol_list, list) { | ||
| 1152 | if (func->type != STT_FUNC) | ||
| 1153 | continue; | ||
| 1154 | |||
| 1155 | func_for_each_insn(file, func, insn) { | ||
| 1156 | if (insn->visited) | ||
| 1157 | continue; | ||
| 1158 | |||
| 1159 | insn->visited = true; | ||
| 1160 | |||
| 1161 | if (file->ignore_unreachables || warnings || | ||
| 1162 | ignore_unreachable_insn(func, insn)) | ||
| 1163 | continue; | ||
| 1164 | |||
| 1165 | /* | ||
| 1166 | * gcov produces a lot of unreachable | ||
| 1167 | * instructions. If we get an unreachable | ||
| 1168 | * warning and the file has gcov enabled, just | ||
| 1169 | * ignore it, and all other such warnings for | ||
| 1170 | * the file. | ||
| 1171 | */ | ||
| 1172 | if (!file->ignore_unreachables && | ||
| 1173 | gcov_enabled(file)) { | ||
| 1174 | file->ignore_unreachables = true; | ||
| 1175 | continue; | ||
| 1176 | } | ||
| 1177 | |||
| 1178 | WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset); | ||
| 1179 | warnings++; | ||
| 1180 | } | ||
| 1181 | } | ||
| 1182 | } | ||
| 1183 | |||
| 1184 | return warnings; | ||
| 1185 | } | ||
| 1186 | |||
| 1187 | static int validate_uncallable_instructions(struct objtool_file *file) | ||
| 1188 | { | ||
| 1189 | struct instruction *insn; | ||
| 1190 | int warnings = 0; | ||
| 1191 | |||
| 1192 | for_each_insn(file, insn) { | ||
| 1193 | if (!insn->visited && insn->type == INSN_RETURN) { | ||
| 1194 | WARN_FUNC("return instruction outside of a callable function", | ||
| 1195 | insn->sec, insn->offset); | ||
| 1196 | warnings++; | ||
| 1197 | } | ||
| 1198 | } | ||
| 1199 | |||
| 1200 | return warnings; | ||
| 1201 | } | ||
| 1202 | |||
| 1203 | static void cleanup(struct objtool_file *file) | ||
| 1204 | { | ||
| 1205 | struct instruction *insn, *tmpinsn; | ||
| 1206 | struct alternative *alt, *tmpalt; | ||
| 1207 | |||
| 1208 | list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) { | ||
| 1209 | list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) { | ||
| 1210 | list_del(&alt->list); | ||
| 1211 | free(alt); | ||
| 1212 | } | ||
| 1213 | list_del(&insn->list); | ||
| 1214 | hash_del(&insn->hash); | ||
| 1215 | free(insn); | ||
| 1216 | } | ||
| 1217 | elf_close(file->elf); | ||
| 1218 | } | ||
| 1219 | |||
| 1220 | int check(const char *_objname, bool _nofp) | ||
| 1221 | { | ||
| 1222 | struct objtool_file file; | ||
| 1223 | int ret, warnings = 0; | ||
| 1224 | |||
| 1225 | objname = _objname; | ||
| 1226 | nofp = _nofp; | ||
| 1227 | |||
| 1228 | file.elf = elf_open(objname); | ||
| 1229 | if (!file.elf) { | ||
| 1230 | fprintf(stderr, "error reading elf file %s\n", objname); | ||
| 1231 | return 1; | ||
| 1232 | } | ||
| 1233 | |||
| 1234 | INIT_LIST_HEAD(&file.insn_list); | ||
| 1235 | hash_init(file.insn_hash); | ||
| 1236 | file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard"); | ||
| 1237 | file.rodata = find_section_by_name(file.elf, ".rodata"); | ||
| 1238 | file.ignore_unreachables = false; | ||
| 1239 | file.c_file = find_section_by_name(file.elf, ".comment"); | ||
| 1240 | |||
| 1241 | ret = decode_sections(&file); | ||
| 1242 | if (ret < 0) | ||
| 1243 | goto out; | ||
| 1244 | warnings += ret; | ||
| 1245 | |||
| 1246 | ret = validate_functions(&file); | ||
| 1247 | if (ret < 0) | ||
| 1248 | goto out; | ||
| 1249 | warnings += ret; | ||
| 1250 | |||
| 1251 | ret = validate_uncallable_instructions(&file); | ||
| 1252 | if (ret < 0) | ||
| 1253 | goto out; | ||
| 1254 | warnings += ret; | ||
| 1255 | |||
| 1256 | out: | ||
| 1257 | cleanup(&file); | ||
| 1258 | |||
| 1259 | /* ignore warnings for now until we get all the code cleaned up */ | ||
| 1260 | if (ret || warnings) | ||
| 1261 | return 0; | ||
| 1262 | return 0; | ||
| 1263 | } | ||
diff --git a/tools/objtool/check.h b/tools/objtool/check.h new file mode 100644 index 000000000000..c0d2fde6b4ea --- /dev/null +++ b/tools/objtool/check.h | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU General Public License | ||
| 6 | * as published by the Free Software Foundation; either version 2 | ||
| 7 | * of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #ifndef _CHECK_H | ||
| 19 | #define _CHECK_H | ||
| 20 | |||
| 21 | #include <stdbool.h> | ||
| 22 | #include "elf.h" | ||
| 23 | #include "arch.h" | ||
| 24 | #include <linux/hashtable.h> | ||
| 25 | |||
| 26 | struct instruction { | ||
| 27 | struct list_head list; | ||
| 28 | struct hlist_node hash; | ||
| 29 | struct section *sec; | ||
| 30 | unsigned long offset; | ||
| 31 | unsigned int len, state; | ||
| 32 | unsigned char type; | ||
| 33 | unsigned long immediate; | ||
| 34 | bool alt_group, visited, dead_end; | ||
| 35 | struct symbol *call_dest; | ||
| 36 | struct instruction *jump_dest; | ||
| 37 | struct list_head alts; | ||
| 38 | struct symbol *func; | ||
| 39 | }; | ||
| 40 | |||
| 41 | struct objtool_file { | ||
| 42 | struct elf *elf; | ||
| 43 | struct list_head insn_list; | ||
| 44 | DECLARE_HASHTABLE(insn_hash, 16); | ||
| 45 | struct section *rodata, *whitelist; | ||
| 46 | bool ignore_unreachables, c_file; | ||
| 47 | }; | ||
| 48 | |||
| 49 | int check(const char *objname, bool nofp); | ||
| 50 | |||
| 51 | #endif /* _CHECK_H */ | ||
