b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> |
| 4 | */ |
| 5 | |
| 6 | #include <string.h> |
| 7 | #include <stdlib.h> |
| 8 | |
| 9 | #include "builtin.h" |
| 10 | #include "check.h" |
| 11 | #include "elf.h" |
| 12 | #include "special.h" |
| 13 | #include "arch.h" |
| 14 | #include "warn.h" |
| 15 | |
| 16 | #include <linux/hashtable.h> |
| 17 | #include <linux/kernel.h> |
| 18 | |
| 19 | #define FAKE_JUMP_OFFSET -1 |
| 20 | |
| 21 | #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table" |
| 22 | |
| 23 | struct alternative { |
| 24 | struct list_head list; |
| 25 | struct instruction *insn; |
| 26 | bool skip_orig; |
| 27 | }; |
| 28 | |
| 29 | const char *objname; |
| 30 | struct cfi_state initial_func_cfi; |
| 31 | |
| 32 | struct instruction *find_insn(struct objtool_file *file, |
| 33 | struct section *sec, unsigned long offset) |
| 34 | { |
| 35 | struct instruction *insn; |
| 36 | |
| 37 | hash_for_each_possible(file->insn_hash, insn, hash, offset) |
| 38 | if (insn->sec == sec && insn->offset == offset) |
| 39 | return insn; |
| 40 | |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | static struct instruction *next_insn_same_sec(struct objtool_file *file, |
| 45 | struct instruction *insn) |
| 46 | { |
| 47 | struct instruction *next = list_next_entry(insn, list); |
| 48 | |
| 49 | if (!next || &next->list == &file->insn_list || next->sec != insn->sec) |
| 50 | return NULL; |
| 51 | |
| 52 | return next; |
| 53 | } |
| 54 | |
| 55 | static struct instruction *next_insn_same_func(struct objtool_file *file, |
| 56 | struct instruction *insn) |
| 57 | { |
| 58 | struct instruction *next = list_next_entry(insn, list); |
| 59 | struct symbol *func = insn->func; |
| 60 | |
| 61 | if (!func) |
| 62 | return NULL; |
| 63 | |
| 64 | if (&next->list != &file->insn_list && next->func == func) |
| 65 | return next; |
| 66 | |
| 67 | /* Check if we're already in the subfunction: */ |
| 68 | if (func == func->cfunc) |
| 69 | return NULL; |
| 70 | |
| 71 | /* Move to the subfunction: */ |
| 72 | return find_insn(file, func->cfunc->sec, func->cfunc->offset); |
| 73 | } |
| 74 | |
| 75 | #define func_for_each_insn_all(file, func, insn) \ |
| 76 | for (insn = find_insn(file, func->sec, func->offset); \ |
| 77 | insn; \ |
| 78 | insn = next_insn_same_func(file, insn)) |
| 79 | |
| 80 | #define func_for_each_insn(file, func, insn) \ |
| 81 | for (insn = find_insn(file, func->sec, func->offset); \ |
| 82 | insn && &insn->list != &file->insn_list && \ |
| 83 | insn->sec == func->sec && \ |
| 84 | insn->offset < func->offset + func->len; \ |
| 85 | insn = list_next_entry(insn, list)) |
| 86 | |
| 87 | #define func_for_each_insn_continue_reverse(file, func, insn) \ |
| 88 | for (insn = list_prev_entry(insn, list); \ |
| 89 | &insn->list != &file->insn_list && \ |
| 90 | insn->sec == func->sec && insn->offset >= func->offset; \ |
| 91 | insn = list_prev_entry(insn, list)) |
| 92 | |
| 93 | #define sec_for_each_insn_from(file, insn) \ |
| 94 | for (; insn; insn = next_insn_same_sec(file, insn)) |
| 95 | |
| 96 | #define sec_for_each_insn_continue(file, insn) \ |
| 97 | for (insn = next_insn_same_sec(file, insn); insn; \ |
| 98 | insn = next_insn_same_sec(file, insn)) |
| 99 | |
| 100 | static bool is_sibling_call(struct instruction *insn) |
| 101 | { |
| 102 | /* An indirect jump is either a sibling call or a jump to a table. */ |
| 103 | if (insn->type == INSN_JUMP_DYNAMIC) |
| 104 | return list_empty(&insn->alts); |
| 105 | |
| 106 | if (insn->type != INSN_JUMP_CONDITIONAL && |
| 107 | insn->type != INSN_JUMP_UNCONDITIONAL) |
| 108 | return false; |
| 109 | |
| 110 | /* add_jump_destinations() sets insn->call_dest for sibling calls. */ |
| 111 | return !!insn->call_dest; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * This checks to see if the given function is a "noreturn" function. |
| 116 | * |
| 117 | * For global functions which are outside the scope of this object file, we |
| 118 | * have to keep a manual list of them. |
| 119 | * |
| 120 | * For local functions, we have to detect them manually by simply looking for |
| 121 | * the lack of a return instruction. |
| 122 | */ |
| 123 | static bool __dead_end_function(struct objtool_file *file, struct symbol *func, |
| 124 | int recursion) |
| 125 | { |
| 126 | int i; |
| 127 | struct instruction *insn; |
| 128 | bool empty = true; |
| 129 | |
| 130 | /* |
| 131 | * Unfortunately these have to be hard coded because the noreturn |
| 132 | * attribute isn't provided in ELF data. |
| 133 | */ |
| 134 | static const char * const global_noreturns[] = { |
| 135 | "__stack_chk_fail", |
| 136 | "panic", |
| 137 | "do_exit", |
| 138 | "do_task_dead", |
| 139 | "make_task_dead", |
| 140 | "__module_put_and_exit", |
| 141 | "complete_and_exit", |
| 142 | "__reiserfs_panic", |
| 143 | "lbug_with_loc", |
| 144 | "fortify_panic", |
| 145 | "usercopy_abort", |
| 146 | "machine_real_restart", |
| 147 | "rewind_stack_and_make_dead", |
| 148 | "kunit_try_catch_throw", |
| 149 | "cpu_bringup_and_idle", |
| 150 | }; |
| 151 | |
| 152 | if (!func) |
| 153 | return false; |
| 154 | |
| 155 | if (func->bind == STB_WEAK) |
| 156 | return false; |
| 157 | |
| 158 | if (func->bind == STB_GLOBAL) |
| 159 | for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) |
| 160 | if (!strcmp(func->name, global_noreturns[i])) |
| 161 | return true; |
| 162 | |
| 163 | if (!func->len) |
| 164 | return false; |
| 165 | |
| 166 | insn = find_insn(file, func->sec, func->offset); |
| 167 | if (!insn || !insn->func) |
| 168 | return false; |
| 169 | |
| 170 | func_for_each_insn_all(file, func, insn) { |
| 171 | empty = false; |
| 172 | |
| 173 | if (insn->type == INSN_RETURN) |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | if (empty) |
| 178 | return false; |
| 179 | |
| 180 | /* |
| 181 | * A function can have a sibling call instead of a return. In that |
| 182 | * case, the function's dead-end status depends on whether the target |
| 183 | * of the sibling call returns. |
| 184 | */ |
| 185 | func_for_each_insn_all(file, func, insn) { |
| 186 | if (is_sibling_call(insn)) { |
| 187 | struct instruction *dest = insn->jump_dest; |
| 188 | |
| 189 | if (!dest) |
| 190 | /* sibling call to another file */ |
| 191 | return false; |
| 192 | |
| 193 | /* local sibling call */ |
| 194 | if (recursion == 5) { |
| 195 | /* |
| 196 | * Infinite recursion: two functions have |
| 197 | * sibling calls to each other. This is a very |
| 198 | * rare case. It means they aren't dead ends. |
| 199 | */ |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | return __dead_end_function(file, dest->func, recursion+1); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | static bool dead_end_function(struct objtool_file *file, struct symbol *func) |
| 211 | { |
| 212 | return __dead_end_function(file, func, 0); |
| 213 | } |
| 214 | |
| 215 | static void clear_insn_state(struct insn_state *state) |
| 216 | { |
| 217 | int i; |
| 218 | |
| 219 | memset(state, 0, sizeof(*state)); |
| 220 | state->cfa.base = CFI_UNDEFINED; |
| 221 | for (i = 0; i < CFI_NUM_REGS; i++) { |
| 222 | state->regs[i].base = CFI_UNDEFINED; |
| 223 | state->vals[i].base = CFI_UNDEFINED; |
| 224 | } |
| 225 | state->drap_reg = CFI_UNDEFINED; |
| 226 | state->drap_offset = -1; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Call the arch-specific instruction decoder for all the instructions and add |
| 231 | * them to the global instruction list. |
| 232 | */ |
| 233 | static int decode_instructions(struct objtool_file *file) |
| 234 | { |
| 235 | struct section *sec; |
| 236 | struct symbol *func; |
| 237 | unsigned long offset; |
| 238 | struct instruction *insn; |
| 239 | int ret; |
| 240 | |
| 241 | for_each_sec(file, sec) { |
| 242 | |
| 243 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) |
| 244 | continue; |
| 245 | |
| 246 | if (strcmp(sec->name, ".altinstr_replacement") && |
| 247 | strcmp(sec->name, ".altinstr_aux") && |
| 248 | strncmp(sec->name, ".discard.", 9)) |
| 249 | sec->text = true; |
| 250 | |
| 251 | for (offset = 0; offset < sec->len; offset += insn->len) { |
| 252 | insn = malloc(sizeof(*insn)); |
| 253 | if (!insn) { |
| 254 | WARN("malloc failed"); |
| 255 | return -1; |
| 256 | } |
| 257 | memset(insn, 0, sizeof(*insn)); |
| 258 | INIT_LIST_HEAD(&insn->alts); |
| 259 | clear_insn_state(&insn->state); |
| 260 | |
| 261 | insn->sec = sec; |
| 262 | insn->offset = offset; |
| 263 | |
| 264 | ret = arch_decode_instruction(file->elf, sec, offset, |
| 265 | sec->len - offset, |
| 266 | &insn->len, &insn->type, |
| 267 | &insn->immediate, |
| 268 | &insn->stack_op); |
| 269 | if (ret) |
| 270 | goto err; |
| 271 | |
| 272 | hash_add(file->insn_hash, &insn->hash, insn->offset); |
| 273 | list_add_tail(&insn->list, &file->insn_list); |
| 274 | } |
| 275 | |
| 276 | list_for_each_entry(func, &sec->symbol_list, list) { |
| 277 | if (func->type != STT_FUNC || func->alias != func) |
| 278 | continue; |
| 279 | |
| 280 | if (!find_insn(file, sec, func->offset)) { |
| 281 | WARN("%s(): can't find starting instruction", |
| 282 | func->name); |
| 283 | return -1; |
| 284 | } |
| 285 | |
| 286 | func_for_each_insn(file, func, insn) |
| 287 | insn->func = func; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | |
| 293 | err: |
| 294 | free(insn); |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Mark "ud2" instructions and manually annotated 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 | /* |
| 309 | * By default, "ud2" is a dead end unless otherwise annotated, because |
| 310 | * GCC 7 inserts it for certain divide-by-zero cases. |
| 311 | */ |
| 312 | for_each_insn(file, insn) |
| 313 | if (insn->type == INSN_BUG) |
| 314 | insn->dead_end = true; |
| 315 | |
| 316 | /* |
| 317 | * Check for manually annotated dead ends. |
| 318 | */ |
| 319 | sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); |
| 320 | if (!sec) |
| 321 | goto reachable; |
| 322 | |
| 323 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 324 | if (rela->sym->type != STT_SECTION) { |
| 325 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 326 | return -1; |
| 327 | } |
| 328 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 329 | if (insn) |
| 330 | insn = list_prev_entry(insn, list); |
| 331 | else if (rela->addend == rela->sym->sec->len) { |
| 332 | found = false; |
| 333 | list_for_each_entry_reverse(insn, &file->insn_list, list) { |
| 334 | if (insn->sec == rela->sym->sec) { |
| 335 | found = true; |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | if (!found) { |
| 341 | WARN("can't find unreachable insn at %s+0x%x", |
| 342 | rela->sym->sec->name, rela->addend); |
| 343 | return -1; |
| 344 | } |
| 345 | } else { |
| 346 | WARN("can't find unreachable insn at %s+0x%x", |
| 347 | rela->sym->sec->name, rela->addend); |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | insn->dead_end = true; |
| 352 | } |
| 353 | |
| 354 | reachable: |
| 355 | /* |
| 356 | * These manually annotated reachable checks are needed for GCC 4.4, |
| 357 | * where the Linux unreachable() macro isn't supported. In that case |
| 358 | * GCC doesn't know the "ud2" is fatal, so it generates code as if it's |
| 359 | * not a dead end. |
| 360 | */ |
| 361 | sec = find_section_by_name(file->elf, ".rela.discard.reachable"); |
| 362 | if (!sec) |
| 363 | return 0; |
| 364 | |
| 365 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 366 | if (rela->sym->type != STT_SECTION) { |
| 367 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 368 | return -1; |
| 369 | } |
| 370 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 371 | if (insn) |
| 372 | insn = list_prev_entry(insn, list); |
| 373 | else if (rela->addend == rela->sym->sec->len) { |
| 374 | found = false; |
| 375 | list_for_each_entry_reverse(insn, &file->insn_list, list) { |
| 376 | if (insn->sec == rela->sym->sec) { |
| 377 | found = true; |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (!found) { |
| 383 | WARN("can't find reachable insn at %s+0x%x", |
| 384 | rela->sym->sec->name, rela->addend); |
| 385 | return -1; |
| 386 | } |
| 387 | } else { |
| 388 | WARN("can't find reachable insn at %s+0x%x", |
| 389 | rela->sym->sec->name, rela->addend); |
| 390 | return -1; |
| 391 | } |
| 392 | |
| 393 | insn->dead_end = false; |
| 394 | } |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Warnings shouldn't be reported for ignored functions. |
| 401 | */ |
| 402 | static void add_ignores(struct objtool_file *file) |
| 403 | { |
| 404 | struct instruction *insn; |
| 405 | struct section *sec; |
| 406 | struct symbol *func; |
| 407 | struct rela *rela; |
| 408 | |
| 409 | sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); |
| 410 | if (!sec) |
| 411 | return; |
| 412 | |
| 413 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 414 | switch (rela->sym->type) { |
| 415 | case STT_FUNC: |
| 416 | func = rela->sym; |
| 417 | break; |
| 418 | |
| 419 | case STT_SECTION: |
| 420 | func = find_symbol_by_offset(rela->sym->sec, rela->addend); |
| 421 | if (!func || func->type != STT_FUNC) |
| 422 | continue; |
| 423 | break; |
| 424 | |
| 425 | default: |
| 426 | WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type); |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | func_for_each_insn_all(file, func, insn) |
| 431 | insn->ignore = true; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * This is a whitelist of functions that is allowed to be called with AC set. |
| 437 | * The list is meant to be minimal and only contains compiler instrumentation |
| 438 | * ABI and a few functions used to implement *_{to,from}_user() functions. |
| 439 | * |
| 440 | * These functions must not directly change AC, but may PUSHF/POPF. |
| 441 | */ |
| 442 | static const char *uaccess_safe_builtin[] = { |
| 443 | /* KASAN */ |
| 444 | "kasan_report", |
| 445 | "check_memory_region", |
| 446 | /* KASAN out-of-line */ |
| 447 | "__asan_loadN_noabort", |
| 448 | "__asan_load1_noabort", |
| 449 | "__asan_load2_noabort", |
| 450 | "__asan_load4_noabort", |
| 451 | "__asan_load8_noabort", |
| 452 | "__asan_load16_noabort", |
| 453 | "__asan_storeN_noabort", |
| 454 | "__asan_store1_noabort", |
| 455 | "__asan_store2_noabort", |
| 456 | "__asan_store4_noabort", |
| 457 | "__asan_store8_noabort", |
| 458 | "__asan_store16_noabort", |
| 459 | /* KASAN in-line */ |
| 460 | "__asan_report_load_n_noabort", |
| 461 | "__asan_report_load1_noabort", |
| 462 | "__asan_report_load2_noabort", |
| 463 | "__asan_report_load4_noabort", |
| 464 | "__asan_report_load8_noabort", |
| 465 | "__asan_report_load16_noabort", |
| 466 | "__asan_report_store_n_noabort", |
| 467 | "__asan_report_store1_noabort", |
| 468 | "__asan_report_store2_noabort", |
| 469 | "__asan_report_store4_noabort", |
| 470 | "__asan_report_store8_noabort", |
| 471 | "__asan_report_store16_noabort", |
| 472 | /* KCOV */ |
| 473 | "write_comp_data", |
| 474 | "__sanitizer_cov_trace_pc", |
| 475 | "__sanitizer_cov_trace_const_cmp1", |
| 476 | "__sanitizer_cov_trace_const_cmp2", |
| 477 | "__sanitizer_cov_trace_const_cmp4", |
| 478 | "__sanitizer_cov_trace_const_cmp8", |
| 479 | "__sanitizer_cov_trace_cmp1", |
| 480 | "__sanitizer_cov_trace_cmp2", |
| 481 | "__sanitizer_cov_trace_cmp4", |
| 482 | "__sanitizer_cov_trace_cmp8", |
| 483 | /* UBSAN */ |
| 484 | "ubsan_type_mismatch_common", |
| 485 | "__ubsan_handle_type_mismatch", |
| 486 | "__ubsan_handle_type_mismatch_v1", |
| 487 | "__ubsan_handle_shift_out_of_bounds", |
| 488 | /* misc */ |
| 489 | "csum_partial_copy_generic", |
| 490 | "__memcpy_mcsafe", |
| 491 | "mcsafe_handle_tail", |
| 492 | "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */ |
| 493 | NULL |
| 494 | }; |
| 495 | |
| 496 | static void add_uaccess_safe(struct objtool_file *file) |
| 497 | { |
| 498 | struct symbol *func; |
| 499 | const char **name; |
| 500 | |
| 501 | if (!uaccess) |
| 502 | return; |
| 503 | |
| 504 | for (name = uaccess_safe_builtin; *name; name++) { |
| 505 | func = find_symbol_by_name(file->elf, *name); |
| 506 | if (!func) |
| 507 | continue; |
| 508 | |
| 509 | func->uaccess_safe = true; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * FIXME: For now, just ignore any alternatives which add retpolines. This is |
| 515 | * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline. |
| 516 | * But it at least allows objtool to understand the control flow *around* the |
| 517 | * retpoline. |
| 518 | */ |
| 519 | static int add_ignore_alternatives(struct objtool_file *file) |
| 520 | { |
| 521 | struct section *sec; |
| 522 | struct rela *rela; |
| 523 | struct instruction *insn; |
| 524 | |
| 525 | sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts"); |
| 526 | if (!sec) |
| 527 | return 0; |
| 528 | |
| 529 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 530 | if (rela->sym->type != STT_SECTION) { |
| 531 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 532 | return -1; |
| 533 | } |
| 534 | |
| 535 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 536 | if (!insn) { |
| 537 | WARN("bad .discard.ignore_alts entry"); |
| 538 | return -1; |
| 539 | } |
| 540 | |
| 541 | insn->ignore_alts = true; |
| 542 | } |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Find the destination instructions for all jumps. |
| 549 | */ |
| 550 | static int add_jump_destinations(struct objtool_file *file) |
| 551 | { |
| 552 | struct instruction *insn; |
| 553 | struct rela *rela; |
| 554 | struct section *dest_sec; |
| 555 | unsigned long dest_off; |
| 556 | |
| 557 | for_each_insn(file, insn) { |
| 558 | if (insn->type != INSN_JUMP_CONDITIONAL && |
| 559 | insn->type != INSN_JUMP_UNCONDITIONAL) |
| 560 | continue; |
| 561 | |
| 562 | if (insn->offset == FAKE_JUMP_OFFSET) |
| 563 | continue; |
| 564 | |
| 565 | rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 566 | insn->len); |
| 567 | if (!rela) { |
| 568 | dest_sec = insn->sec; |
| 569 | dest_off = insn->offset + insn->len + insn->immediate; |
| 570 | } else if (rela->sym->type == STT_SECTION) { |
| 571 | dest_sec = rela->sym->sec; |
| 572 | dest_off = rela->addend + 4; |
| 573 | } else if (rela->sym->sec->idx) { |
| 574 | dest_sec = rela->sym->sec; |
| 575 | dest_off = rela->sym->sym.st_value + rela->addend + 4; |
| 576 | } else if (strstr(rela->sym->name, "_indirect_thunk_")) { |
| 577 | /* |
| 578 | * Retpoline jumps are really dynamic jumps in |
| 579 | * disguise, so convert them accordingly. |
| 580 | */ |
| 581 | if (insn->type == INSN_JUMP_UNCONDITIONAL) |
| 582 | insn->type = INSN_JUMP_DYNAMIC; |
| 583 | else |
| 584 | insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL; |
| 585 | |
| 586 | insn->retpoline_safe = true; |
| 587 | continue; |
| 588 | } else { |
| 589 | /* external sibling call */ |
| 590 | insn->call_dest = rela->sym; |
| 591 | continue; |
| 592 | } |
| 593 | |
| 594 | insn->jump_dest = find_insn(file, dest_sec, dest_off); |
| 595 | if (!insn->jump_dest) { |
| 596 | |
| 597 | /* |
| 598 | * This is a special case where an alt instruction |
| 599 | * jumps past the end of the section. These are |
| 600 | * handled later in handle_group_alt(). |
| 601 | */ |
| 602 | if (!strcmp(insn->sec->name, ".altinstr_replacement")) |
| 603 | continue; |
| 604 | |
| 605 | WARN_FUNC("can't find jump dest instruction at %s+0x%lx", |
| 606 | insn->sec, insn->offset, dest_sec->name, |
| 607 | dest_off); |
| 608 | return -1; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Cross-function jump. |
| 613 | */ |
| 614 | if (insn->func && insn->jump_dest->func && |
| 615 | insn->func != insn->jump_dest->func) { |
| 616 | |
| 617 | /* |
| 618 | * For GCC 8+, create parent/child links for any cold |
| 619 | * subfunctions. This is _mostly_ redundant with a |
| 620 | * similar initialization in read_symbols(). |
| 621 | * |
| 622 | * If a function has aliases, we want the *first* such |
| 623 | * function in the symbol table to be the subfunction's |
| 624 | * parent. In that case we overwrite the |
| 625 | * initialization done in read_symbols(). |
| 626 | * |
| 627 | * However this code can't completely replace the |
| 628 | * read_symbols() code because this doesn't detect the |
| 629 | * case where the parent function's only reference to a |
| 630 | * subfunction is through a jump table. |
| 631 | */ |
| 632 | if (!strstr(insn->func->name, ".cold") && |
| 633 | strstr(insn->jump_dest->func->name, ".cold")) { |
| 634 | insn->func->cfunc = insn->jump_dest->func; |
| 635 | insn->jump_dest->func->pfunc = insn->func; |
| 636 | |
| 637 | } else if (insn->jump_dest->func->pfunc != insn->func->pfunc && |
| 638 | insn->jump_dest->offset == insn->jump_dest->func->offset) { |
| 639 | |
| 640 | /* internal sibling call */ |
| 641 | insn->call_dest = insn->jump_dest->func; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | /* |
| 651 | * Find the destination instructions for all calls. |
| 652 | */ |
| 653 | static int add_call_destinations(struct objtool_file *file) |
| 654 | { |
| 655 | struct instruction *insn; |
| 656 | unsigned long dest_off; |
| 657 | struct rela *rela; |
| 658 | |
| 659 | for_each_insn(file, insn) { |
| 660 | if (insn->type != INSN_CALL) |
| 661 | continue; |
| 662 | |
| 663 | rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 664 | insn->len); |
| 665 | if (!rela) { |
| 666 | dest_off = insn->offset + insn->len + insn->immediate; |
| 667 | insn->call_dest = find_symbol_by_offset(insn->sec, |
| 668 | dest_off); |
| 669 | |
| 670 | if (!insn->call_dest && !insn->ignore) { |
| 671 | WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset); |
| 672 | return -1; |
| 673 | } |
| 674 | |
| 675 | } else if (rela->sym->type == STT_SECTION) { |
| 676 | insn->call_dest = find_symbol_by_offset(rela->sym->sec, |
| 677 | rela->addend+4); |
| 678 | if (!insn->call_dest || |
| 679 | insn->call_dest->type != STT_FUNC) { |
| 680 | WARN_FUNC("can't find call dest symbol at %s+0x%x", |
| 681 | insn->sec, insn->offset, |
| 682 | rela->sym->sec->name, |
| 683 | rela->addend + 4); |
| 684 | return -1; |
| 685 | } |
| 686 | } else |
| 687 | insn->call_dest = rela->sym; |
| 688 | } |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * The .alternatives section requires some extra special care, over and above |
| 695 | * what other special sections require: |
| 696 | * |
| 697 | * 1. Because alternatives are patched in-place, we need to insert a fake jump |
| 698 | * instruction at the end so that validate_branch() skips all the original |
| 699 | * replaced instructions when validating the new instruction path. |
| 700 | * |
| 701 | * 2. An added wrinkle is that the new instruction length might be zero. In |
| 702 | * that case the old instructions are replaced with noops. We simulate that |
| 703 | * by creating a fake jump as the only new instruction. |
| 704 | * |
| 705 | * 3. In some cases, the alternative section includes an instruction which |
| 706 | * conditionally jumps to the _end_ of the entry. We have to modify these |
| 707 | * jumps' destinations to point back to .text rather than the end of the |
| 708 | * entry in .altinstr_replacement. |
| 709 | */ |
| 710 | static int handle_group_alt(struct objtool_file *file, |
| 711 | struct special_alt *special_alt, |
| 712 | struct instruction *orig_insn, |
| 713 | struct instruction **new_insn) |
| 714 | { |
| 715 | struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL; |
| 716 | unsigned long dest_off; |
| 717 | |
| 718 | last_orig_insn = NULL; |
| 719 | insn = orig_insn; |
| 720 | sec_for_each_insn_from(file, insn) { |
| 721 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) |
| 722 | break; |
| 723 | |
| 724 | insn->alt_group = true; |
| 725 | last_orig_insn = insn; |
| 726 | } |
| 727 | |
| 728 | if (next_insn_same_sec(file, last_orig_insn)) { |
| 729 | fake_jump = malloc(sizeof(*fake_jump)); |
| 730 | if (!fake_jump) { |
| 731 | WARN("malloc failed"); |
| 732 | return -1; |
| 733 | } |
| 734 | memset(fake_jump, 0, sizeof(*fake_jump)); |
| 735 | INIT_LIST_HEAD(&fake_jump->alts); |
| 736 | clear_insn_state(&fake_jump->state); |
| 737 | |
| 738 | fake_jump->sec = special_alt->new_sec; |
| 739 | fake_jump->offset = FAKE_JUMP_OFFSET; |
| 740 | fake_jump->type = INSN_JUMP_UNCONDITIONAL; |
| 741 | fake_jump->jump_dest = list_next_entry(last_orig_insn, list); |
| 742 | fake_jump->func = orig_insn->func; |
| 743 | } |
| 744 | |
| 745 | if (!special_alt->new_len) { |
| 746 | if (!fake_jump) { |
| 747 | WARN("%s: empty alternative at end of section", |
| 748 | special_alt->orig_sec->name); |
| 749 | return -1; |
| 750 | } |
| 751 | |
| 752 | *new_insn = fake_jump; |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | last_new_insn = NULL; |
| 757 | insn = *new_insn; |
| 758 | sec_for_each_insn_from(file, insn) { |
| 759 | if (insn->offset >= special_alt->new_off + special_alt->new_len) |
| 760 | break; |
| 761 | |
| 762 | last_new_insn = insn; |
| 763 | |
| 764 | insn->ignore = orig_insn->ignore_alts; |
| 765 | insn->func = orig_insn->func; |
| 766 | |
| 767 | if (insn->type != INSN_JUMP_CONDITIONAL && |
| 768 | insn->type != INSN_JUMP_UNCONDITIONAL) |
| 769 | continue; |
| 770 | |
| 771 | if (!insn->immediate) |
| 772 | continue; |
| 773 | |
| 774 | dest_off = insn->offset + insn->len + insn->immediate; |
| 775 | if (dest_off == special_alt->new_off + special_alt->new_len) { |
| 776 | if (!fake_jump) { |
| 777 | WARN("%s: alternative jump to end of section", |
| 778 | special_alt->orig_sec->name); |
| 779 | return -1; |
| 780 | } |
| 781 | insn->jump_dest = fake_jump; |
| 782 | } |
| 783 | |
| 784 | if (!insn->jump_dest) { |
| 785 | WARN_FUNC("can't find alternative jump destination", |
| 786 | insn->sec, insn->offset); |
| 787 | return -1; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | if (!last_new_insn) { |
| 792 | WARN_FUNC("can't find last new alternative instruction", |
| 793 | special_alt->new_sec, special_alt->new_off); |
| 794 | return -1; |
| 795 | } |
| 796 | |
| 797 | if (fake_jump) |
| 798 | list_add(&fake_jump->list, &last_new_insn->list); |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * A jump table entry can either convert a nop to a jump or a jump to a nop. |
| 805 | * If the original instruction is a jump, make the alt entry an effective nop |
| 806 | * by just skipping the original instruction. |
| 807 | */ |
| 808 | static int handle_jump_alt(struct objtool_file *file, |
| 809 | struct special_alt *special_alt, |
| 810 | struct instruction *orig_insn, |
| 811 | struct instruction **new_insn) |
| 812 | { |
| 813 | if (orig_insn->type == INSN_NOP) |
| 814 | return 0; |
| 815 | |
| 816 | if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { |
| 817 | WARN_FUNC("unsupported instruction at jump label", |
| 818 | orig_insn->sec, orig_insn->offset); |
| 819 | return -1; |
| 820 | } |
| 821 | |
| 822 | *new_insn = list_next_entry(orig_insn, list); |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Read all the special sections which have alternate instructions which can be |
| 828 | * patched in or redirected to at runtime. Each instruction having alternate |
| 829 | * instruction(s) has them added to its insn->alts list, which will be |
| 830 | * traversed in validate_branch(). |
| 831 | */ |
| 832 | static int add_special_section_alts(struct objtool_file *file) |
| 833 | { |
| 834 | struct list_head special_alts; |
| 835 | struct instruction *orig_insn, *new_insn; |
| 836 | struct special_alt *special_alt, *tmp; |
| 837 | struct alternative *alt; |
| 838 | int ret; |
| 839 | |
| 840 | ret = special_get_alts(file->elf, &special_alts); |
| 841 | if (ret) |
| 842 | return ret; |
| 843 | |
| 844 | list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { |
| 845 | |
| 846 | orig_insn = find_insn(file, special_alt->orig_sec, |
| 847 | special_alt->orig_off); |
| 848 | if (!orig_insn) { |
| 849 | WARN_FUNC("special: can't find orig instruction", |
| 850 | special_alt->orig_sec, special_alt->orig_off); |
| 851 | ret = -1; |
| 852 | goto out; |
| 853 | } |
| 854 | |
| 855 | new_insn = NULL; |
| 856 | if (!special_alt->group || special_alt->new_len) { |
| 857 | new_insn = find_insn(file, special_alt->new_sec, |
| 858 | special_alt->new_off); |
| 859 | if (!new_insn) { |
| 860 | WARN_FUNC("special: can't find new instruction", |
| 861 | special_alt->new_sec, |
| 862 | special_alt->new_off); |
| 863 | ret = -1; |
| 864 | goto out; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | if (special_alt->group) { |
| 869 | if (!special_alt->orig_len) { |
| 870 | WARN_FUNC("empty alternative entry", |
| 871 | orig_insn->sec, orig_insn->offset); |
| 872 | continue; |
| 873 | } |
| 874 | |
| 875 | ret = handle_group_alt(file, special_alt, orig_insn, |
| 876 | &new_insn); |
| 877 | if (ret) |
| 878 | goto out; |
| 879 | } else if (special_alt->jump_or_nop) { |
| 880 | ret = handle_jump_alt(file, special_alt, orig_insn, |
| 881 | &new_insn); |
| 882 | if (ret) |
| 883 | goto out; |
| 884 | } |
| 885 | |
| 886 | alt = malloc(sizeof(*alt)); |
| 887 | if (!alt) { |
| 888 | WARN("malloc failed"); |
| 889 | ret = -1; |
| 890 | goto out; |
| 891 | } |
| 892 | |
| 893 | alt->insn = new_insn; |
| 894 | alt->skip_orig = special_alt->skip_orig; |
| 895 | orig_insn->ignore_alts |= special_alt->skip_alt; |
| 896 | list_add_tail(&alt->list, &orig_insn->alts); |
| 897 | |
| 898 | list_del(&special_alt->list); |
| 899 | free(special_alt); |
| 900 | } |
| 901 | |
| 902 | out: |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | static int add_jump_table(struct objtool_file *file, struct instruction *insn, |
| 907 | struct rela *table) |
| 908 | { |
| 909 | struct rela *rela = table; |
| 910 | struct instruction *dest_insn; |
| 911 | struct alternative *alt; |
| 912 | struct symbol *pfunc = insn->func->pfunc; |
| 913 | unsigned int prev_offset = 0; |
| 914 | |
| 915 | /* |
| 916 | * Each @rela is a switch table relocation which points to the target |
| 917 | * instruction. |
| 918 | */ |
| 919 | list_for_each_entry_from(rela, &table->sec->rela_list, list) { |
| 920 | |
| 921 | /* Check for the end of the table: */ |
| 922 | if (rela != table && rela->jump_table_start) |
| 923 | break; |
| 924 | |
| 925 | /* Make sure the table entries are consecutive: */ |
| 926 | if (prev_offset && rela->offset != prev_offset + 8) |
| 927 | break; |
| 928 | |
| 929 | /* Detect function pointers from contiguous objects: */ |
| 930 | if (rela->sym->sec == pfunc->sec && |
| 931 | rela->addend == pfunc->offset) |
| 932 | break; |
| 933 | |
| 934 | dest_insn = find_insn(file, rela->sym->sec, rela->addend); |
| 935 | if (!dest_insn) |
| 936 | break; |
| 937 | |
| 938 | /* Make sure the destination is in the same function: */ |
| 939 | if (!dest_insn->func || dest_insn->func->pfunc != pfunc) |
| 940 | break; |
| 941 | |
| 942 | alt = malloc(sizeof(*alt)); |
| 943 | if (!alt) { |
| 944 | WARN("malloc failed"); |
| 945 | return -1; |
| 946 | } |
| 947 | |
| 948 | alt->insn = dest_insn; |
| 949 | list_add_tail(&alt->list, &insn->alts); |
| 950 | prev_offset = rela->offset; |
| 951 | } |
| 952 | |
| 953 | if (!prev_offset) { |
| 954 | WARN_FUNC("can't find switch jump table", |
| 955 | insn->sec, insn->offset); |
| 956 | return -1; |
| 957 | } |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | * find_jump_table() - Given a dynamic jump, find the switch jump table in |
| 964 | * .rodata associated with it. |
| 965 | * |
| 966 | * There are 3 basic patterns: |
| 967 | * |
| 968 | * 1. jmpq *[rodata addr](,%reg,8) |
| 969 | * |
| 970 | * This is the most common case by far. It jumps to an address in a simple |
| 971 | * jump table which is stored in .rodata. |
| 972 | * |
| 973 | * 2. jmpq *[rodata addr](%rip) |
| 974 | * |
| 975 | * This is caused by a rare GCC quirk, currently only seen in three driver |
| 976 | * functions in the kernel, only with certain obscure non-distro configs. |
| 977 | * |
| 978 | * As part of an optimization, GCC makes a copy of an existing switch jump |
| 979 | * table, modifies it, and then hard-codes the jump (albeit with an indirect |
| 980 | * jump) to use a single entry in the table. The rest of the jump table and |
| 981 | * some of its jump targets remain as dead code. |
| 982 | * |
| 983 | * In such a case we can just crudely ignore all unreachable instruction |
| 984 | * warnings for the entire object file. Ideally we would just ignore them |
| 985 | * for the function, but that would require redesigning the code quite a |
| 986 | * bit. And honestly that's just not worth doing: unreachable instruction |
| 987 | * warnings are of questionable value anyway, and this is such a rare issue. |
| 988 | * |
| 989 | * 3. mov [rodata addr],%reg1 |
| 990 | * ... some instructions ... |
| 991 | * jmpq *(%reg1,%reg2,8) |
| 992 | * |
| 993 | * This is a fairly uncommon pattern which is new for GCC 6. As of this |
| 994 | * writing, there are 11 occurrences of it in the allmodconfig kernel. |
| 995 | * |
| 996 | * As of GCC 7 there are quite a few more of these and the 'in between' code |
| 997 | * is significant. Esp. with KASAN enabled some of the code between the mov |
| 998 | * and jmpq uses .rodata itself, which can confuse things. |
| 999 | * |
| 1000 | * TODO: Once we have DWARF CFI and smarter instruction decoding logic, |
| 1001 | * ensure the same register is used in the mov and jump instructions. |
| 1002 | * |
| 1003 | * NOTE: RETPOLINE made it harder still to decode dynamic jumps. |
| 1004 | */ |
| 1005 | static struct rela *find_jump_table(struct objtool_file *file, |
| 1006 | struct symbol *func, |
| 1007 | struct instruction *insn) |
| 1008 | { |
| 1009 | struct rela *text_rela, *table_rela; |
| 1010 | struct instruction *orig_insn = insn; |
| 1011 | struct section *table_sec; |
| 1012 | unsigned long table_offset; |
| 1013 | |
| 1014 | /* |
| 1015 | * Backward search using the @first_jump_src links, these help avoid |
| 1016 | * much of the 'in between' code. Which avoids us getting confused by |
| 1017 | * it. |
| 1018 | */ |
| 1019 | for (; |
| 1020 | &insn->list != &file->insn_list && insn->func && insn->func->pfunc == func; |
| 1021 | insn = insn->first_jump_src ?: list_prev_entry(insn, list)) { |
| 1022 | |
| 1023 | if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC) |
| 1024 | break; |
| 1025 | |
| 1026 | /* allow small jumps within the range */ |
| 1027 | if (insn->type == INSN_JUMP_UNCONDITIONAL && |
| 1028 | insn->jump_dest && |
| 1029 | (insn->jump_dest->offset <= insn->offset || |
| 1030 | insn->jump_dest->offset > orig_insn->offset)) |
| 1031 | break; |
| 1032 | |
| 1033 | /* look for a relocation which references .rodata */ |
| 1034 | text_rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 1035 | insn->len); |
| 1036 | if (!text_rela || text_rela->sym->type != STT_SECTION || |
| 1037 | !text_rela->sym->sec->rodata) |
| 1038 | continue; |
| 1039 | |
| 1040 | table_offset = text_rela->addend; |
| 1041 | table_sec = text_rela->sym->sec; |
| 1042 | |
| 1043 | if (text_rela->type == R_X86_64_PC32) |
| 1044 | table_offset += 4; |
| 1045 | |
| 1046 | /* |
| 1047 | * Make sure the .rodata address isn't associated with a |
| 1048 | * symbol. GCC jump tables are anonymous data. |
| 1049 | * |
| 1050 | * Also support C jump tables which are in the same format as |
| 1051 | * switch jump tables. For objtool to recognize them, they |
| 1052 | * need to be placed in the C_JUMP_TABLE_SECTION section. They |
| 1053 | * have symbols associated with them. |
| 1054 | */ |
| 1055 | if (find_symbol_containing(table_sec, table_offset) && |
| 1056 | strcmp(table_sec->name, C_JUMP_TABLE_SECTION)) |
| 1057 | continue; |
| 1058 | |
| 1059 | /* Each table entry has a rela associated with it. */ |
| 1060 | table_rela = find_rela_by_dest(table_sec, table_offset); |
| 1061 | if (!table_rela) |
| 1062 | continue; |
| 1063 | |
| 1064 | /* |
| 1065 | * Use of RIP-relative switch jumps is quite rare, and |
| 1066 | * indicates a rare GCC quirk/bug which can leave dead code |
| 1067 | * behind. |
| 1068 | */ |
| 1069 | if (text_rela->type == R_X86_64_PC32) |
| 1070 | file->ignore_unreachables = true; |
| 1071 | |
| 1072 | return table_rela; |
| 1073 | } |
| 1074 | |
| 1075 | return NULL; |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * First pass: Mark the head of each jump table so that in the next pass, |
| 1080 | * we know when a given jump table ends and the next one starts. |
| 1081 | */ |
| 1082 | static void mark_func_jump_tables(struct objtool_file *file, |
| 1083 | struct symbol *func) |
| 1084 | { |
| 1085 | struct instruction *insn, *last = NULL; |
| 1086 | struct rela *rela; |
| 1087 | |
| 1088 | func_for_each_insn_all(file, func, insn) { |
| 1089 | if (!last) |
| 1090 | last = insn; |
| 1091 | |
| 1092 | /* |
| 1093 | * Store back-pointers for unconditional forward jumps such |
| 1094 | * that find_jump_table() can back-track using those and |
| 1095 | * avoid some potentially confusing code. |
| 1096 | */ |
| 1097 | if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest && |
| 1098 | insn->offset > last->offset && |
| 1099 | insn->jump_dest->offset > insn->offset && |
| 1100 | !insn->jump_dest->first_jump_src) { |
| 1101 | |
| 1102 | insn->jump_dest->first_jump_src = insn; |
| 1103 | last = insn->jump_dest; |
| 1104 | } |
| 1105 | |
| 1106 | if (insn->type != INSN_JUMP_DYNAMIC) |
| 1107 | continue; |
| 1108 | |
| 1109 | rela = find_jump_table(file, func, insn); |
| 1110 | if (rela) { |
| 1111 | rela->jump_table_start = true; |
| 1112 | insn->jump_table = rela; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | static int add_func_jump_tables(struct objtool_file *file, |
| 1118 | struct symbol *func) |
| 1119 | { |
| 1120 | struct instruction *insn; |
| 1121 | int ret; |
| 1122 | |
| 1123 | func_for_each_insn_all(file, func, insn) { |
| 1124 | if (!insn->jump_table) |
| 1125 | continue; |
| 1126 | |
| 1127 | ret = add_jump_table(file, insn, insn->jump_table); |
| 1128 | if (ret) |
| 1129 | return ret; |
| 1130 | } |
| 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | /* |
| 1136 | * For some switch statements, gcc generates a jump table in the .rodata |
| 1137 | * section which contains a list of addresses within the function to jump to. |
| 1138 | * This finds these jump tables and adds them to the insn->alts lists. |
| 1139 | */ |
| 1140 | static int add_jump_table_alts(struct objtool_file *file) |
| 1141 | { |
| 1142 | struct section *sec; |
| 1143 | struct symbol *func; |
| 1144 | int ret; |
| 1145 | |
| 1146 | if (!file->rodata) |
| 1147 | return 0; |
| 1148 | |
| 1149 | for_each_sec(file, sec) { |
| 1150 | list_for_each_entry(func, &sec->symbol_list, list) { |
| 1151 | if (func->type != STT_FUNC) |
| 1152 | continue; |
| 1153 | |
| 1154 | mark_func_jump_tables(file, func); |
| 1155 | ret = add_func_jump_tables(file, func); |
| 1156 | if (ret) |
| 1157 | return ret; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | static int read_unwind_hints(struct objtool_file *file) |
| 1165 | { |
| 1166 | struct section *sec, *relasec; |
| 1167 | struct rela *rela; |
| 1168 | struct unwind_hint *hint; |
| 1169 | struct instruction *insn; |
| 1170 | struct cfi_reg *cfa; |
| 1171 | int i; |
| 1172 | |
| 1173 | sec = find_section_by_name(file->elf, ".discard.unwind_hints"); |
| 1174 | if (!sec) |
| 1175 | return 0; |
| 1176 | |
| 1177 | relasec = sec->rela; |
| 1178 | if (!relasec) { |
| 1179 | WARN("missing .rela.discard.unwind_hints section"); |
| 1180 | return -1; |
| 1181 | } |
| 1182 | |
| 1183 | if (sec->len % sizeof(struct unwind_hint)) { |
| 1184 | WARN("struct unwind_hint size mismatch"); |
| 1185 | return -1; |
| 1186 | } |
| 1187 | |
| 1188 | file->hints = true; |
| 1189 | |
| 1190 | for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) { |
| 1191 | hint = (struct unwind_hint *)sec->data->d_buf + i; |
| 1192 | |
| 1193 | rela = find_rela_by_dest(sec, i * sizeof(*hint)); |
| 1194 | if (!rela) { |
| 1195 | WARN("can't find rela for unwind_hints[%d]", i); |
| 1196 | return -1; |
| 1197 | } |
| 1198 | |
| 1199 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 1200 | if (!insn) { |
| 1201 | WARN("can't find insn for unwind_hints[%d]", i); |
| 1202 | return -1; |
| 1203 | } |
| 1204 | |
| 1205 | cfa = &insn->state.cfa; |
| 1206 | |
| 1207 | if (hint->type == UNWIND_HINT_TYPE_SAVE) { |
| 1208 | insn->save = true; |
| 1209 | continue; |
| 1210 | |
| 1211 | } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) { |
| 1212 | insn->restore = true; |
| 1213 | insn->hint = true; |
| 1214 | continue; |
| 1215 | } |
| 1216 | |
| 1217 | insn->hint = true; |
| 1218 | |
| 1219 | switch (hint->sp_reg) { |
| 1220 | case ORC_REG_UNDEFINED: |
| 1221 | cfa->base = CFI_UNDEFINED; |
| 1222 | break; |
| 1223 | case ORC_REG_SP: |
| 1224 | cfa->base = CFI_SP; |
| 1225 | break; |
| 1226 | case ORC_REG_BP: |
| 1227 | cfa->base = CFI_BP; |
| 1228 | break; |
| 1229 | case ORC_REG_SP_INDIRECT: |
| 1230 | cfa->base = CFI_SP_INDIRECT; |
| 1231 | break; |
| 1232 | case ORC_REG_R10: |
| 1233 | cfa->base = CFI_R10; |
| 1234 | break; |
| 1235 | case ORC_REG_R13: |
| 1236 | cfa->base = CFI_R13; |
| 1237 | break; |
| 1238 | case ORC_REG_DI: |
| 1239 | cfa->base = CFI_DI; |
| 1240 | break; |
| 1241 | case ORC_REG_DX: |
| 1242 | cfa->base = CFI_DX; |
| 1243 | break; |
| 1244 | default: |
| 1245 | WARN_FUNC("unsupported unwind_hint sp base reg %d", |
| 1246 | insn->sec, insn->offset, hint->sp_reg); |
| 1247 | return -1; |
| 1248 | } |
| 1249 | |
| 1250 | cfa->offset = hint->sp_offset; |
| 1251 | insn->state.type = hint->type; |
| 1252 | insn->state.end = hint->end; |
| 1253 | } |
| 1254 | |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | static int read_retpoline_hints(struct objtool_file *file) |
| 1259 | { |
| 1260 | struct section *sec; |
| 1261 | struct instruction *insn; |
| 1262 | struct rela *rela; |
| 1263 | |
| 1264 | sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); |
| 1265 | if (!sec) |
| 1266 | return 0; |
| 1267 | |
| 1268 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 1269 | if (rela->sym->type != STT_SECTION) { |
| 1270 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 1271 | return -1; |
| 1272 | } |
| 1273 | |
| 1274 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 1275 | if (!insn) { |
| 1276 | WARN("bad .discard.retpoline_safe entry"); |
| 1277 | return -1; |
| 1278 | } |
| 1279 | |
| 1280 | if (insn->type != INSN_JUMP_DYNAMIC && |
| 1281 | insn->type != INSN_CALL_DYNAMIC) { |
| 1282 | WARN_FUNC("retpoline_safe hint not an indirect jump/call", |
| 1283 | insn->sec, insn->offset); |
| 1284 | return -1; |
| 1285 | } |
| 1286 | |
| 1287 | insn->retpoline_safe = true; |
| 1288 | } |
| 1289 | |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | |
| 1294 | static int read_intra_function_calls(struct objtool_file *file) |
| 1295 | { |
| 1296 | struct instruction *insn; |
| 1297 | struct section *sec; |
| 1298 | struct rela *rela; |
| 1299 | |
| 1300 | sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls"); |
| 1301 | if (!sec) |
| 1302 | return 0; |
| 1303 | |
| 1304 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 1305 | unsigned long dest_off; |
| 1306 | |
| 1307 | if (rela->sym->type != STT_SECTION) { |
| 1308 | WARN("unexpected relocation symbol type in %s", |
| 1309 | sec->name); |
| 1310 | return -1; |
| 1311 | } |
| 1312 | |
| 1313 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 1314 | if (!insn) { |
| 1315 | WARN("bad .discard.intra_function_call entry"); |
| 1316 | return -1; |
| 1317 | } |
| 1318 | |
| 1319 | if (insn->type != INSN_CALL) { |
| 1320 | WARN_FUNC("intra_function_call not a direct call", |
| 1321 | insn->sec, insn->offset); |
| 1322 | return -1; |
| 1323 | } |
| 1324 | |
| 1325 | /* |
| 1326 | * Treat intra-function CALLs as JMPs, but with a stack_op. |
| 1327 | * See add_call_destinations(), which strips stack_ops from |
| 1328 | * normal CALLs. |
| 1329 | */ |
| 1330 | insn->type = INSN_JUMP_UNCONDITIONAL; |
| 1331 | |
| 1332 | dest_off = insn->offset + insn->len + insn->immediate; |
| 1333 | insn->jump_dest = find_insn(file, insn->sec, dest_off); |
| 1334 | if (!insn->jump_dest) { |
| 1335 | WARN_FUNC("can't find call dest at %s+0x%lx", |
| 1336 | insn->sec, insn->offset, |
| 1337 | insn->sec->name, dest_off); |
| 1338 | return -1; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | static void mark_rodata(struct objtool_file *file) |
| 1346 | { |
| 1347 | struct section *sec; |
| 1348 | bool found = false; |
| 1349 | |
| 1350 | /* |
| 1351 | * Search for the following rodata sections, each of which can |
| 1352 | * potentially contain jump tables: |
| 1353 | * |
| 1354 | * - .rodata: can contain GCC switch tables |
| 1355 | * - .rodata.<func>: same, if -fdata-sections is being used |
| 1356 | * - .rodata..c_jump_table: contains C annotated jump tables |
| 1357 | * |
| 1358 | * .rodata.str1.* sections are ignored; they don't contain jump tables. |
| 1359 | */ |
| 1360 | for_each_sec(file, sec) { |
| 1361 | if ((!strncmp(sec->name, ".rodata", 7) && !strstr(sec->name, ".str1.")) || |
| 1362 | !strcmp(sec->name, C_JUMP_TABLE_SECTION)) { |
| 1363 | sec->rodata = true; |
| 1364 | found = true; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | file->rodata = found; |
| 1369 | } |
| 1370 | |
| 1371 | static int decode_sections(struct objtool_file *file) |
| 1372 | { |
| 1373 | int ret; |
| 1374 | |
| 1375 | mark_rodata(file); |
| 1376 | |
| 1377 | ret = decode_instructions(file); |
| 1378 | if (ret) |
| 1379 | return ret; |
| 1380 | |
| 1381 | ret = add_dead_ends(file); |
| 1382 | if (ret) |
| 1383 | return ret; |
| 1384 | |
| 1385 | add_ignores(file); |
| 1386 | add_uaccess_safe(file); |
| 1387 | |
| 1388 | ret = add_ignore_alternatives(file); |
| 1389 | if (ret) |
| 1390 | return ret; |
| 1391 | |
| 1392 | ret = add_jump_destinations(file); |
| 1393 | if (ret) |
| 1394 | return ret; |
| 1395 | |
| 1396 | ret = add_special_section_alts(file); |
| 1397 | if (ret) |
| 1398 | return ret; |
| 1399 | |
| 1400 | ret = read_intra_function_calls(file); |
| 1401 | if (ret) |
| 1402 | return ret; |
| 1403 | |
| 1404 | ret = add_call_destinations(file); |
| 1405 | if (ret) |
| 1406 | return ret; |
| 1407 | |
| 1408 | ret = add_jump_table_alts(file); |
| 1409 | if (ret) |
| 1410 | return ret; |
| 1411 | |
| 1412 | ret = read_unwind_hints(file); |
| 1413 | if (ret) |
| 1414 | return ret; |
| 1415 | |
| 1416 | ret = read_retpoline_hints(file); |
| 1417 | if (ret) |
| 1418 | return ret; |
| 1419 | |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | static bool is_fentry_call(struct instruction *insn) |
| 1424 | { |
| 1425 | if (insn->type == INSN_CALL && insn->call_dest && |
| 1426 | insn->call_dest->type == STT_NOTYPE && |
| 1427 | !strcmp(insn->call_dest->name, "__fentry__")) |
| 1428 | return true; |
| 1429 | |
| 1430 | return false; |
| 1431 | } |
| 1432 | |
| 1433 | static bool has_modified_stack_frame(struct insn_state *state) |
| 1434 | { |
| 1435 | int i; |
| 1436 | |
| 1437 | if (state->cfa.base != initial_func_cfi.cfa.base || |
| 1438 | state->cfa.offset != initial_func_cfi.cfa.offset || |
| 1439 | state->stack_size != initial_func_cfi.cfa.offset || |
| 1440 | state->drap) |
| 1441 | return true; |
| 1442 | |
| 1443 | for (i = 0; i < CFI_NUM_REGS; i++) |
| 1444 | if (state->regs[i].base != initial_func_cfi.regs[i].base || |
| 1445 | state->regs[i].offset != initial_func_cfi.regs[i].offset) |
| 1446 | return true; |
| 1447 | |
| 1448 | return false; |
| 1449 | } |
| 1450 | |
| 1451 | static bool has_valid_stack_frame(struct insn_state *state) |
| 1452 | { |
| 1453 | if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA && |
| 1454 | state->regs[CFI_BP].offset == -16) |
| 1455 | return true; |
| 1456 | |
| 1457 | if (state->drap && state->regs[CFI_BP].base == CFI_BP) |
| 1458 | return true; |
| 1459 | |
| 1460 | return false; |
| 1461 | } |
| 1462 | |
| 1463 | static int update_insn_state_regs(struct instruction *insn, struct insn_state *state) |
| 1464 | { |
| 1465 | struct cfi_reg *cfa = &state->cfa; |
| 1466 | struct stack_op *op = &insn->stack_op; |
| 1467 | |
| 1468 | if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT) |
| 1469 | return 0; |
| 1470 | |
| 1471 | /* push */ |
| 1472 | if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF) |
| 1473 | cfa->offset += 8; |
| 1474 | |
| 1475 | /* pop */ |
| 1476 | if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF) |
| 1477 | cfa->offset -= 8; |
| 1478 | |
| 1479 | /* add immediate to sp */ |
| 1480 | if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD && |
| 1481 | op->dest.reg == CFI_SP && op->src.reg == CFI_SP) |
| 1482 | cfa->offset -= op->src.offset; |
| 1483 | |
| 1484 | return 0; |
| 1485 | } |
| 1486 | |
| 1487 | static void save_reg(struct insn_state *state, unsigned char reg, int base, |
| 1488 | int offset) |
| 1489 | { |
| 1490 | if (arch_callee_saved_reg(reg) && |
| 1491 | state->regs[reg].base == CFI_UNDEFINED) { |
| 1492 | state->regs[reg].base = base; |
| 1493 | state->regs[reg].offset = offset; |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | static void restore_reg(struct insn_state *state, unsigned char reg) |
| 1498 | { |
| 1499 | state->regs[reg].base = CFI_UNDEFINED; |
| 1500 | state->regs[reg].offset = 0; |
| 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | * A note about DRAP stack alignment: |
| 1505 | * |
| 1506 | * GCC has the concept of a DRAP register, which is used to help keep track of |
| 1507 | * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP |
| 1508 | * register. The typical DRAP pattern is: |
| 1509 | * |
| 1510 | * 4c 8d 54 24 08 lea 0x8(%rsp),%r10 |
| 1511 | * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp |
| 1512 | * 41 ff 72 f8 pushq -0x8(%r10) |
| 1513 | * 55 push %rbp |
| 1514 | * 48 89 e5 mov %rsp,%rbp |
| 1515 | * (more pushes) |
| 1516 | * 41 52 push %r10 |
| 1517 | * ... |
| 1518 | * 41 5a pop %r10 |
| 1519 | * (more pops) |
| 1520 | * 5d pop %rbp |
| 1521 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 1522 | * c3 retq |
| 1523 | * |
| 1524 | * There are some variations in the epilogues, like: |
| 1525 | * |
| 1526 | * 5b pop %rbx |
| 1527 | * 41 5a pop %r10 |
| 1528 | * 41 5c pop %r12 |
| 1529 | * 41 5d pop %r13 |
| 1530 | * 41 5e pop %r14 |
| 1531 | * c9 leaveq |
| 1532 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 1533 | * c3 retq |
| 1534 | * |
| 1535 | * and: |
| 1536 | * |
| 1537 | * 4c 8b 55 e8 mov -0x18(%rbp),%r10 |
| 1538 | * 48 8b 5d e0 mov -0x20(%rbp),%rbx |
| 1539 | * 4c 8b 65 f0 mov -0x10(%rbp),%r12 |
| 1540 | * 4c 8b 6d f8 mov -0x8(%rbp),%r13 |
| 1541 | * c9 leaveq |
| 1542 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 1543 | * c3 retq |
| 1544 | * |
| 1545 | * Sometimes r13 is used as the DRAP register, in which case it's saved and |
| 1546 | * restored beforehand: |
| 1547 | * |
| 1548 | * 41 55 push %r13 |
| 1549 | * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13 |
| 1550 | * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp |
| 1551 | * ... |
| 1552 | * 49 8d 65 f0 lea -0x10(%r13),%rsp |
| 1553 | * 41 5d pop %r13 |
| 1554 | * c3 retq |
| 1555 | */ |
| 1556 | static int update_insn_state(struct instruction *insn, struct insn_state *state) |
| 1557 | { |
| 1558 | struct stack_op *op = &insn->stack_op; |
| 1559 | struct cfi_reg *cfa = &state->cfa; |
| 1560 | struct cfi_reg *regs = state->regs; |
| 1561 | |
| 1562 | /* stack operations don't make sense with an undefined CFA */ |
| 1563 | if (cfa->base == CFI_UNDEFINED) { |
| 1564 | if (insn->func) { |
| 1565 | WARN_FUNC("undefined stack state", insn->sec, insn->offset); |
| 1566 | return -1; |
| 1567 | } |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
| 1571 | if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET) |
| 1572 | return update_insn_state_regs(insn, state); |
| 1573 | |
| 1574 | switch (op->dest.type) { |
| 1575 | |
| 1576 | case OP_DEST_REG: |
| 1577 | switch (op->src.type) { |
| 1578 | |
| 1579 | case OP_SRC_REG: |
| 1580 | if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP && |
| 1581 | cfa->base == CFI_SP && |
| 1582 | regs[CFI_BP].base == CFI_CFA && |
| 1583 | regs[CFI_BP].offset == -cfa->offset) { |
| 1584 | |
| 1585 | /* mov %rsp, %rbp */ |
| 1586 | cfa->base = op->dest.reg; |
| 1587 | state->bp_scratch = false; |
| 1588 | } |
| 1589 | |
| 1590 | else if (op->src.reg == CFI_SP && |
| 1591 | op->dest.reg == CFI_BP && state->drap) { |
| 1592 | |
| 1593 | /* drap: mov %rsp, %rbp */ |
| 1594 | regs[CFI_BP].base = CFI_BP; |
| 1595 | regs[CFI_BP].offset = -state->stack_size; |
| 1596 | state->bp_scratch = false; |
| 1597 | } |
| 1598 | |
| 1599 | else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
| 1600 | |
| 1601 | /* |
| 1602 | * mov %rsp, %reg |
| 1603 | * |
| 1604 | * This is needed for the rare case where GCC |
| 1605 | * does: |
| 1606 | * |
| 1607 | * mov %rsp, %rax |
| 1608 | * ... |
| 1609 | * mov %rax, %rsp |
| 1610 | */ |
| 1611 | state->vals[op->dest.reg].base = CFI_CFA; |
| 1612 | state->vals[op->dest.reg].offset = -state->stack_size; |
| 1613 | } |
| 1614 | |
| 1615 | else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP && |
| 1616 | cfa->base == CFI_BP) { |
| 1617 | |
| 1618 | /* |
| 1619 | * mov %rbp, %rsp |
| 1620 | * |
| 1621 | * Restore the original stack pointer (Clang). |
| 1622 | */ |
| 1623 | state->stack_size = -state->regs[CFI_BP].offset; |
| 1624 | } |
| 1625 | |
| 1626 | else if (op->dest.reg == cfa->base) { |
| 1627 | |
| 1628 | /* mov %reg, %rsp */ |
| 1629 | if (cfa->base == CFI_SP && |
| 1630 | state->vals[op->src.reg].base == CFI_CFA) { |
| 1631 | |
| 1632 | /* |
| 1633 | * This is needed for the rare case |
| 1634 | * where GCC does something dumb like: |
| 1635 | * |
| 1636 | * lea 0x8(%rsp), %rcx |
| 1637 | * ... |
| 1638 | * mov %rcx, %rsp |
| 1639 | */ |
| 1640 | cfa->offset = -state->vals[op->src.reg].offset; |
| 1641 | state->stack_size = cfa->offset; |
| 1642 | |
| 1643 | } else { |
| 1644 | cfa->base = CFI_UNDEFINED; |
| 1645 | cfa->offset = 0; |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | break; |
| 1650 | |
| 1651 | case OP_SRC_ADD: |
| 1652 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) { |
| 1653 | |
| 1654 | /* add imm, %rsp */ |
| 1655 | state->stack_size -= op->src.offset; |
| 1656 | if (cfa->base == CFI_SP) |
| 1657 | cfa->offset -= op->src.offset; |
| 1658 | break; |
| 1659 | } |
| 1660 | |
| 1661 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) { |
| 1662 | |
| 1663 | /* lea disp(%rbp), %rsp */ |
| 1664 | state->stack_size = -(op->src.offset + regs[CFI_BP].offset); |
| 1665 | break; |
| 1666 | } |
| 1667 | |
| 1668 | if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
| 1669 | |
| 1670 | /* drap: lea disp(%rsp), %drap */ |
| 1671 | state->drap_reg = op->dest.reg; |
| 1672 | |
| 1673 | /* |
| 1674 | * lea disp(%rsp), %reg |
| 1675 | * |
| 1676 | * This is needed for the rare case where GCC |
| 1677 | * does something dumb like: |
| 1678 | * |
| 1679 | * lea 0x8(%rsp), %rcx |
| 1680 | * ... |
| 1681 | * mov %rcx, %rsp |
| 1682 | */ |
| 1683 | state->vals[op->dest.reg].base = CFI_CFA; |
| 1684 | state->vals[op->dest.reg].offset = \ |
| 1685 | -state->stack_size + op->src.offset; |
| 1686 | |
| 1687 | break; |
| 1688 | } |
| 1689 | |
| 1690 | if (state->drap && op->dest.reg == CFI_SP && |
| 1691 | op->src.reg == state->drap_reg) { |
| 1692 | |
| 1693 | /* drap: lea disp(%drap), %rsp */ |
| 1694 | cfa->base = CFI_SP; |
| 1695 | cfa->offset = state->stack_size = -op->src.offset; |
| 1696 | state->drap_reg = CFI_UNDEFINED; |
| 1697 | state->drap = false; |
| 1698 | break; |
| 1699 | } |
| 1700 | |
| 1701 | if (op->dest.reg == state->cfa.base) { |
| 1702 | WARN_FUNC("unsupported stack register modification", |
| 1703 | insn->sec, insn->offset); |
| 1704 | return -1; |
| 1705 | } |
| 1706 | |
| 1707 | break; |
| 1708 | |
| 1709 | case OP_SRC_AND: |
| 1710 | if (op->dest.reg != CFI_SP || |
| 1711 | (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) || |
| 1712 | (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) { |
| 1713 | WARN_FUNC("unsupported stack pointer realignment", |
| 1714 | insn->sec, insn->offset); |
| 1715 | return -1; |
| 1716 | } |
| 1717 | |
| 1718 | if (state->drap_reg != CFI_UNDEFINED) { |
| 1719 | /* drap: and imm, %rsp */ |
| 1720 | cfa->base = state->drap_reg; |
| 1721 | cfa->offset = state->stack_size = 0; |
| 1722 | state->drap = true; |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * Older versions of GCC (4.8ish) realign the stack |
| 1727 | * without DRAP, with a frame pointer. |
| 1728 | */ |
| 1729 | |
| 1730 | break; |
| 1731 | |
| 1732 | case OP_SRC_POP: |
| 1733 | case OP_SRC_POPF: |
| 1734 | if (!state->drap && op->dest.type == OP_DEST_REG && |
| 1735 | op->dest.reg == cfa->base) { |
| 1736 | |
| 1737 | /* pop %rbp */ |
| 1738 | cfa->base = CFI_SP; |
| 1739 | } |
| 1740 | |
| 1741 | if (state->drap && cfa->base == CFI_BP_INDIRECT && |
| 1742 | op->dest.type == OP_DEST_REG && |
| 1743 | op->dest.reg == state->drap_reg && |
| 1744 | state->drap_offset == -state->stack_size) { |
| 1745 | |
| 1746 | /* drap: pop %drap */ |
| 1747 | cfa->base = state->drap_reg; |
| 1748 | cfa->offset = 0; |
| 1749 | state->drap_offset = -1; |
| 1750 | |
| 1751 | } else if (regs[op->dest.reg].offset == -state->stack_size) { |
| 1752 | |
| 1753 | /* pop %reg */ |
| 1754 | restore_reg(state, op->dest.reg); |
| 1755 | } |
| 1756 | |
| 1757 | state->stack_size -= 8; |
| 1758 | if (cfa->base == CFI_SP) |
| 1759 | cfa->offset -= 8; |
| 1760 | |
| 1761 | break; |
| 1762 | |
| 1763 | case OP_SRC_REG_INDIRECT: |
| 1764 | if (state->drap && op->src.reg == CFI_BP && |
| 1765 | op->src.offset == state->drap_offset) { |
| 1766 | |
| 1767 | /* drap: mov disp(%rbp), %drap */ |
| 1768 | cfa->base = state->drap_reg; |
| 1769 | cfa->offset = 0; |
| 1770 | state->drap_offset = -1; |
| 1771 | } |
| 1772 | |
| 1773 | if (state->drap && op->src.reg == CFI_BP && |
| 1774 | op->src.offset == regs[op->dest.reg].offset) { |
| 1775 | |
| 1776 | /* drap: mov disp(%rbp), %reg */ |
| 1777 | restore_reg(state, op->dest.reg); |
| 1778 | |
| 1779 | } else if (op->src.reg == cfa->base && |
| 1780 | op->src.offset == regs[op->dest.reg].offset + cfa->offset) { |
| 1781 | |
| 1782 | /* mov disp(%rbp), %reg */ |
| 1783 | /* mov disp(%rsp), %reg */ |
| 1784 | restore_reg(state, op->dest.reg); |
| 1785 | } |
| 1786 | |
| 1787 | break; |
| 1788 | |
| 1789 | default: |
| 1790 | WARN_FUNC("unknown stack-related instruction", |
| 1791 | insn->sec, insn->offset); |
| 1792 | return -1; |
| 1793 | } |
| 1794 | |
| 1795 | break; |
| 1796 | |
| 1797 | case OP_DEST_PUSH: |
| 1798 | case OP_DEST_PUSHF: |
| 1799 | state->stack_size += 8; |
| 1800 | if (cfa->base == CFI_SP) |
| 1801 | cfa->offset += 8; |
| 1802 | |
| 1803 | if (op->src.type != OP_SRC_REG) |
| 1804 | break; |
| 1805 | |
| 1806 | if (state->drap) { |
| 1807 | if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) { |
| 1808 | |
| 1809 | /* drap: push %drap */ |
| 1810 | cfa->base = CFI_BP_INDIRECT; |
| 1811 | cfa->offset = -state->stack_size; |
| 1812 | |
| 1813 | /* save drap so we know when to restore it */ |
| 1814 | state->drap_offset = -state->stack_size; |
| 1815 | |
| 1816 | } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) { |
| 1817 | |
| 1818 | /* drap: push %rbp */ |
| 1819 | state->stack_size = 0; |
| 1820 | |
| 1821 | } else if (regs[op->src.reg].base == CFI_UNDEFINED) { |
| 1822 | |
| 1823 | /* drap: push %reg */ |
| 1824 | save_reg(state, op->src.reg, CFI_BP, -state->stack_size); |
| 1825 | } |
| 1826 | |
| 1827 | } else { |
| 1828 | |
| 1829 | /* push %reg */ |
| 1830 | save_reg(state, op->src.reg, CFI_CFA, -state->stack_size); |
| 1831 | } |
| 1832 | |
| 1833 | /* detect when asm code uses rbp as a scratch register */ |
| 1834 | if (!no_fp && insn->func && op->src.reg == CFI_BP && |
| 1835 | cfa->base != CFI_BP) |
| 1836 | state->bp_scratch = true; |
| 1837 | break; |
| 1838 | |
| 1839 | case OP_DEST_REG_INDIRECT: |
| 1840 | |
| 1841 | if (state->drap) { |
| 1842 | if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) { |
| 1843 | |
| 1844 | /* drap: mov %drap, disp(%rbp) */ |
| 1845 | cfa->base = CFI_BP_INDIRECT; |
| 1846 | cfa->offset = op->dest.offset; |
| 1847 | |
| 1848 | /* save drap offset so we know when to restore it */ |
| 1849 | state->drap_offset = op->dest.offset; |
| 1850 | } |
| 1851 | |
| 1852 | else if (regs[op->src.reg].base == CFI_UNDEFINED) { |
| 1853 | |
| 1854 | /* drap: mov reg, disp(%rbp) */ |
| 1855 | save_reg(state, op->src.reg, CFI_BP, op->dest.offset); |
| 1856 | } |
| 1857 | |
| 1858 | } else if (op->dest.reg == cfa->base) { |
| 1859 | |
| 1860 | /* mov reg, disp(%rbp) */ |
| 1861 | /* mov reg, disp(%rsp) */ |
| 1862 | save_reg(state, op->src.reg, CFI_CFA, |
| 1863 | op->dest.offset - state->cfa.offset); |
| 1864 | } |
| 1865 | |
| 1866 | break; |
| 1867 | |
| 1868 | case OP_DEST_LEAVE: |
| 1869 | if ((!state->drap && cfa->base != CFI_BP) || |
| 1870 | (state->drap && cfa->base != state->drap_reg)) { |
| 1871 | WARN_FUNC("leave instruction with modified stack frame", |
| 1872 | insn->sec, insn->offset); |
| 1873 | return -1; |
| 1874 | } |
| 1875 | |
| 1876 | /* leave (mov %rbp, %rsp; pop %rbp) */ |
| 1877 | |
| 1878 | state->stack_size = -state->regs[CFI_BP].offset - 8; |
| 1879 | restore_reg(state, CFI_BP); |
| 1880 | |
| 1881 | if (!state->drap) { |
| 1882 | cfa->base = CFI_SP; |
| 1883 | cfa->offset -= 8; |
| 1884 | } |
| 1885 | |
| 1886 | break; |
| 1887 | |
| 1888 | case OP_DEST_MEM: |
| 1889 | if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) { |
| 1890 | WARN_FUNC("unknown stack-related memory operation", |
| 1891 | insn->sec, insn->offset); |
| 1892 | return -1; |
| 1893 | } |
| 1894 | |
| 1895 | /* pop mem */ |
| 1896 | state->stack_size -= 8; |
| 1897 | if (cfa->base == CFI_SP) |
| 1898 | cfa->offset -= 8; |
| 1899 | |
| 1900 | break; |
| 1901 | |
| 1902 | default: |
| 1903 | WARN_FUNC("unknown stack-related instruction", |
| 1904 | insn->sec, insn->offset); |
| 1905 | return -1; |
| 1906 | } |
| 1907 | |
| 1908 | return 0; |
| 1909 | } |
| 1910 | |
| 1911 | static bool insn_state_match(struct instruction *insn, struct insn_state *state) |
| 1912 | { |
| 1913 | struct insn_state *state1 = &insn->state, *state2 = state; |
| 1914 | int i; |
| 1915 | |
| 1916 | if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) { |
| 1917 | WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d", |
| 1918 | insn->sec, insn->offset, |
| 1919 | state1->cfa.base, state1->cfa.offset, |
| 1920 | state2->cfa.base, state2->cfa.offset); |
| 1921 | |
| 1922 | } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) { |
| 1923 | for (i = 0; i < CFI_NUM_REGS; i++) { |
| 1924 | if (!memcmp(&state1->regs[i], &state2->regs[i], |
| 1925 | sizeof(struct cfi_reg))) |
| 1926 | continue; |
| 1927 | |
| 1928 | WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d", |
| 1929 | insn->sec, insn->offset, |
| 1930 | i, state1->regs[i].base, state1->regs[i].offset, |
| 1931 | i, state2->regs[i].base, state2->regs[i].offset); |
| 1932 | break; |
| 1933 | } |
| 1934 | |
| 1935 | } else if (state1->type != state2->type) { |
| 1936 | WARN_FUNC("stack state mismatch: type1=%d type2=%d", |
| 1937 | insn->sec, insn->offset, state1->type, state2->type); |
| 1938 | |
| 1939 | } else if (state1->drap != state2->drap || |
| 1940 | (state1->drap && state1->drap_reg != state2->drap_reg) || |
| 1941 | (state1->drap && state1->drap_offset != state2->drap_offset)) { |
| 1942 | WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)", |
| 1943 | insn->sec, insn->offset, |
| 1944 | state1->drap, state1->drap_reg, state1->drap_offset, |
| 1945 | state2->drap, state2->drap_reg, state2->drap_offset); |
| 1946 | |
| 1947 | } else |
| 1948 | return true; |
| 1949 | |
| 1950 | return false; |
| 1951 | } |
| 1952 | |
| 1953 | static inline bool func_uaccess_safe(struct symbol *func) |
| 1954 | { |
| 1955 | if (func) |
| 1956 | return func->uaccess_safe; |
| 1957 | |
| 1958 | return false; |
| 1959 | } |
| 1960 | |
| 1961 | static inline const char *call_dest_name(struct instruction *insn) |
| 1962 | { |
| 1963 | if (insn->call_dest) |
| 1964 | return insn->call_dest->name; |
| 1965 | |
| 1966 | return "{dynamic}"; |
| 1967 | } |
| 1968 | |
| 1969 | static int validate_call(struct instruction *insn, struct insn_state *state) |
| 1970 | { |
| 1971 | if (state->uaccess && !func_uaccess_safe(insn->call_dest)) { |
| 1972 | WARN_FUNC("call to %s() with UACCESS enabled", |
| 1973 | insn->sec, insn->offset, call_dest_name(insn)); |
| 1974 | return 1; |
| 1975 | } |
| 1976 | |
| 1977 | if (state->df) { |
| 1978 | WARN_FUNC("call to %s() with DF set", |
| 1979 | insn->sec, insn->offset, call_dest_name(insn)); |
| 1980 | return 1; |
| 1981 | } |
| 1982 | |
| 1983 | return 0; |
| 1984 | } |
| 1985 | |
| 1986 | static int validate_sibling_call(struct instruction *insn, struct insn_state *state) |
| 1987 | { |
| 1988 | if (has_modified_stack_frame(state)) { |
| 1989 | WARN_FUNC("sibling call from callable instruction with modified stack frame", |
| 1990 | insn->sec, insn->offset); |
| 1991 | return 1; |
| 1992 | } |
| 1993 | |
| 1994 | return validate_call(insn, state); |
| 1995 | } |
| 1996 | |
| 1997 | /* |
| 1998 | * Follow the branch starting at the given instruction, and recursively follow |
| 1999 | * any other branches (jumps). Meanwhile, track the frame pointer state at |
| 2000 | * each instruction and validate all the rules described in |
| 2001 | * tools/objtool/Documentation/stack-validation.txt. |
| 2002 | */ |
| 2003 | static int validate_branch(struct objtool_file *file, struct symbol *func, |
| 2004 | struct instruction *first, struct insn_state state) |
| 2005 | { |
| 2006 | struct alternative *alt; |
| 2007 | struct instruction *insn, *next_insn; |
| 2008 | struct section *sec; |
| 2009 | u8 visited; |
| 2010 | int ret; |
| 2011 | |
| 2012 | insn = first; |
| 2013 | sec = insn->sec; |
| 2014 | |
| 2015 | if (insn->alt_group && list_empty(&insn->alts)) { |
| 2016 | WARN_FUNC("don't know how to handle branch to middle of alternative instruction group", |
| 2017 | sec, insn->offset); |
| 2018 | return 1; |
| 2019 | } |
| 2020 | |
| 2021 | while (1) { |
| 2022 | next_insn = next_insn_same_sec(file, insn); |
| 2023 | |
| 2024 | if (file->c_file && func && insn->func && func != insn->func->pfunc) { |
| 2025 | WARN("%s() falls through to next function %s()", |
| 2026 | func->name, insn->func->name); |
| 2027 | return 1; |
| 2028 | } |
| 2029 | |
| 2030 | if (func && insn->ignore) { |
| 2031 | WARN_FUNC("BUG: why am I validating an ignored function?", |
| 2032 | sec, insn->offset); |
| 2033 | return 1; |
| 2034 | } |
| 2035 | |
| 2036 | visited = 1 << state.uaccess; |
| 2037 | if (insn->visited) { |
| 2038 | if (!insn->hint && !insn_state_match(insn, &state)) |
| 2039 | return 1; |
| 2040 | |
| 2041 | if (insn->visited & visited) |
| 2042 | return 0; |
| 2043 | } |
| 2044 | |
| 2045 | if (insn->hint) { |
| 2046 | if (insn->restore) { |
| 2047 | struct instruction *save_insn, *i; |
| 2048 | |
| 2049 | i = insn; |
| 2050 | save_insn = NULL; |
| 2051 | func_for_each_insn_continue_reverse(file, func, i) { |
| 2052 | if (i->save) { |
| 2053 | save_insn = i; |
| 2054 | break; |
| 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | if (!save_insn) { |
| 2059 | WARN_FUNC("no corresponding CFI save for CFI restore", |
| 2060 | sec, insn->offset); |
| 2061 | return 1; |
| 2062 | } |
| 2063 | |
| 2064 | if (!save_insn->visited) { |
| 2065 | /* |
| 2066 | * Oops, no state to copy yet. |
| 2067 | * Hopefully we can reach this |
| 2068 | * instruction from another branch |
| 2069 | * after the save insn has been |
| 2070 | * visited. |
| 2071 | */ |
| 2072 | if (insn == first) |
| 2073 | return 0; |
| 2074 | |
| 2075 | WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo", |
| 2076 | sec, insn->offset); |
| 2077 | return 1; |
| 2078 | } |
| 2079 | |
| 2080 | insn->state = save_insn->state; |
| 2081 | } |
| 2082 | |
| 2083 | state = insn->state; |
| 2084 | |
| 2085 | } else |
| 2086 | insn->state = state; |
| 2087 | |
| 2088 | insn->visited |= visited; |
| 2089 | |
| 2090 | if (!insn->ignore_alts) { |
| 2091 | bool skip_orig = false; |
| 2092 | |
| 2093 | list_for_each_entry(alt, &insn->alts, list) { |
| 2094 | if (alt->skip_orig) |
| 2095 | skip_orig = true; |
| 2096 | |
| 2097 | ret = validate_branch(file, func, alt->insn, state); |
| 2098 | if (ret) { |
| 2099 | if (backtrace) |
| 2100 | BT_FUNC("(alt)", insn); |
| 2101 | return ret; |
| 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | if (skip_orig) |
| 2106 | return 0; |
| 2107 | } |
| 2108 | |
| 2109 | switch (insn->type) { |
| 2110 | |
| 2111 | case INSN_RETURN: |
| 2112 | if (state.uaccess && !func_uaccess_safe(func)) { |
| 2113 | WARN_FUNC("return with UACCESS enabled", sec, insn->offset); |
| 2114 | return 1; |
| 2115 | } |
| 2116 | |
| 2117 | if (!state.uaccess && func_uaccess_safe(func)) { |
| 2118 | WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", sec, insn->offset); |
| 2119 | return 1; |
| 2120 | } |
| 2121 | |
| 2122 | if (state.df) { |
| 2123 | WARN_FUNC("return with DF set", sec, insn->offset); |
| 2124 | return 1; |
| 2125 | } |
| 2126 | |
| 2127 | if (func && has_modified_stack_frame(&state)) { |
| 2128 | WARN_FUNC("return with modified stack frame", |
| 2129 | sec, insn->offset); |
| 2130 | return 1; |
| 2131 | } |
| 2132 | |
| 2133 | if (state.bp_scratch) { |
| 2134 | WARN("%s uses BP as a scratch register", |
| 2135 | func->name); |
| 2136 | return 1; |
| 2137 | } |
| 2138 | |
| 2139 | return 0; |
| 2140 | |
| 2141 | case INSN_CALL: |
| 2142 | case INSN_CALL_DYNAMIC: |
| 2143 | ret = validate_call(insn, &state); |
| 2144 | if (ret) |
| 2145 | return ret; |
| 2146 | |
| 2147 | if (!no_fp && func && !is_fentry_call(insn) && |
| 2148 | !has_valid_stack_frame(&state)) { |
| 2149 | WARN_FUNC("call without frame pointer save/setup", |
| 2150 | sec, insn->offset); |
| 2151 | return 1; |
| 2152 | } |
| 2153 | |
| 2154 | if (dead_end_function(file, insn->call_dest)) |
| 2155 | return 0; |
| 2156 | |
| 2157 | break; |
| 2158 | |
| 2159 | case INSN_JUMP_CONDITIONAL: |
| 2160 | case INSN_JUMP_UNCONDITIONAL: |
| 2161 | if (func && is_sibling_call(insn)) { |
| 2162 | ret = validate_sibling_call(insn, &state); |
| 2163 | if (ret) |
| 2164 | return ret; |
| 2165 | |
| 2166 | } else if (insn->jump_dest) { |
| 2167 | ret = validate_branch(file, func, |
| 2168 | insn->jump_dest, state); |
| 2169 | if (ret) { |
| 2170 | if (backtrace) |
| 2171 | BT_FUNC("(branch)", insn); |
| 2172 | return ret; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | if (insn->type == INSN_JUMP_UNCONDITIONAL) |
| 2177 | return 0; |
| 2178 | |
| 2179 | break; |
| 2180 | |
| 2181 | case INSN_JUMP_DYNAMIC: |
| 2182 | case INSN_JUMP_DYNAMIC_CONDITIONAL: |
| 2183 | if (func && is_sibling_call(insn)) { |
| 2184 | ret = validate_sibling_call(insn, &state); |
| 2185 | if (ret) |
| 2186 | return ret; |
| 2187 | } |
| 2188 | |
| 2189 | if (insn->type == INSN_JUMP_DYNAMIC) |
| 2190 | return 0; |
| 2191 | |
| 2192 | break; |
| 2193 | |
| 2194 | case INSN_CONTEXT_SWITCH: |
| 2195 | if (func && (!next_insn || !next_insn->hint)) { |
| 2196 | WARN_FUNC("unsupported instruction in callable function", |
| 2197 | sec, insn->offset); |
| 2198 | return 1; |
| 2199 | } |
| 2200 | return 0; |
| 2201 | |
| 2202 | case INSN_STACK: |
| 2203 | if (update_insn_state(insn, &state)) |
| 2204 | return 1; |
| 2205 | |
| 2206 | if (insn->stack_op.dest.type == OP_DEST_PUSHF) { |
| 2207 | if (!state.uaccess_stack) { |
| 2208 | state.uaccess_stack = 1; |
| 2209 | } else if (state.uaccess_stack >> 31) { |
| 2210 | WARN_FUNC("PUSHF stack exhausted", sec, insn->offset); |
| 2211 | return 1; |
| 2212 | } |
| 2213 | state.uaccess_stack <<= 1; |
| 2214 | state.uaccess_stack |= state.uaccess; |
| 2215 | } |
| 2216 | |
| 2217 | if (insn->stack_op.src.type == OP_SRC_POPF) { |
| 2218 | if (state.uaccess_stack) { |
| 2219 | state.uaccess = state.uaccess_stack & 1; |
| 2220 | state.uaccess_stack >>= 1; |
| 2221 | if (state.uaccess_stack == 1) |
| 2222 | state.uaccess_stack = 0; |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | break; |
| 2227 | |
| 2228 | case INSN_STAC: |
| 2229 | if (state.uaccess) { |
| 2230 | WARN_FUNC("recursive UACCESS enable", sec, insn->offset); |
| 2231 | return 1; |
| 2232 | } |
| 2233 | |
| 2234 | state.uaccess = true; |
| 2235 | break; |
| 2236 | |
| 2237 | case INSN_CLAC: |
| 2238 | if (!state.uaccess && func) { |
| 2239 | WARN_FUNC("redundant UACCESS disable", sec, insn->offset); |
| 2240 | return 1; |
| 2241 | } |
| 2242 | |
| 2243 | if (func_uaccess_safe(func) && !state.uaccess_stack) { |
| 2244 | WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset); |
| 2245 | return 1; |
| 2246 | } |
| 2247 | |
| 2248 | state.uaccess = false; |
| 2249 | break; |
| 2250 | |
| 2251 | case INSN_STD: |
| 2252 | if (state.df) { |
| 2253 | WARN_FUNC("recursive STD", sec, insn->offset); |
| 2254 | return 1; |
| 2255 | } |
| 2256 | |
| 2257 | state.df = true; |
| 2258 | break; |
| 2259 | |
| 2260 | case INSN_CLD: |
| 2261 | if (!state.df && func) { |
| 2262 | WARN_FUNC("redundant CLD", sec, insn->offset); |
| 2263 | return 1; |
| 2264 | } |
| 2265 | |
| 2266 | state.df = false; |
| 2267 | break; |
| 2268 | |
| 2269 | default: |
| 2270 | break; |
| 2271 | } |
| 2272 | |
| 2273 | if (insn->dead_end) |
| 2274 | return 0; |
| 2275 | |
| 2276 | if (!next_insn) { |
| 2277 | if (state.cfa.base == CFI_UNDEFINED) |
| 2278 | return 0; |
| 2279 | WARN("%s: unexpected end of section", sec->name); |
| 2280 | return 1; |
| 2281 | } |
| 2282 | |
| 2283 | insn = next_insn; |
| 2284 | } |
| 2285 | |
| 2286 | return 0; |
| 2287 | } |
| 2288 | |
| 2289 | static int validate_unwind_hints(struct objtool_file *file) |
| 2290 | { |
| 2291 | struct instruction *insn; |
| 2292 | int ret, warnings = 0; |
| 2293 | struct insn_state state; |
| 2294 | |
| 2295 | if (!file->hints) |
| 2296 | return 0; |
| 2297 | |
| 2298 | clear_insn_state(&state); |
| 2299 | |
| 2300 | for_each_insn(file, insn) { |
| 2301 | if (insn->hint && !insn->visited) { |
| 2302 | ret = validate_branch(file, insn->func, insn, state); |
| 2303 | if (ret && backtrace) |
| 2304 | BT_FUNC("<=== (hint)", insn); |
| 2305 | warnings += ret; |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | return warnings; |
| 2310 | } |
| 2311 | |
| 2312 | static int validate_retpoline(struct objtool_file *file) |
| 2313 | { |
| 2314 | struct instruction *insn; |
| 2315 | int warnings = 0; |
| 2316 | |
| 2317 | for_each_insn(file, insn) { |
| 2318 | if (insn->type != INSN_JUMP_DYNAMIC && |
| 2319 | insn->type != INSN_CALL_DYNAMIC) |
| 2320 | continue; |
| 2321 | |
| 2322 | if (insn->retpoline_safe) |
| 2323 | continue; |
| 2324 | |
| 2325 | /* |
| 2326 | * .init.text code is ran before userspace and thus doesn't |
| 2327 | * strictly need retpolines, except for modules which are |
| 2328 | * loaded late, they very much do need retpoline in their |
| 2329 | * .init.text |
| 2330 | */ |
| 2331 | if (!strcmp(insn->sec->name, ".init.text") && !module) |
| 2332 | continue; |
| 2333 | |
| 2334 | WARN_FUNC("indirect %s found in RETPOLINE build", |
| 2335 | insn->sec, insn->offset, |
| 2336 | insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call"); |
| 2337 | |
| 2338 | warnings++; |
| 2339 | } |
| 2340 | |
| 2341 | return warnings; |
| 2342 | } |
| 2343 | |
| 2344 | static bool is_kasan_insn(struct instruction *insn) |
| 2345 | { |
| 2346 | return (insn->type == INSN_CALL && |
| 2347 | !strcmp(insn->call_dest->name, "__asan_handle_no_return")); |
| 2348 | } |
| 2349 | |
| 2350 | static bool is_ubsan_insn(struct instruction *insn) |
| 2351 | { |
| 2352 | return (insn->type == INSN_CALL && |
| 2353 | !strcmp(insn->call_dest->name, |
| 2354 | "__ubsan_handle_builtin_unreachable")); |
| 2355 | } |
| 2356 | |
| 2357 | static bool ignore_unreachable_insn(struct instruction *insn) |
| 2358 | { |
| 2359 | int i; |
| 2360 | |
| 2361 | if (insn->ignore || insn->type == INSN_NOP) |
| 2362 | return true; |
| 2363 | |
| 2364 | /* |
| 2365 | * Ignore any unused exceptions. This can happen when a whitelisted |
| 2366 | * function has an exception table entry. |
| 2367 | * |
| 2368 | * Also ignore alternative replacement instructions. This can happen |
| 2369 | * when a whitelisted function uses one of the ALTERNATIVE macros. |
| 2370 | */ |
| 2371 | if (!strcmp(insn->sec->name, ".fixup") || |
| 2372 | !strcmp(insn->sec->name, ".altinstr_replacement") || |
| 2373 | !strcmp(insn->sec->name, ".altinstr_aux")) |
| 2374 | return true; |
| 2375 | |
| 2376 | if (!insn->func) |
| 2377 | return false; |
| 2378 | |
| 2379 | /* |
| 2380 | * CONFIG_UBSAN_TRAP inserts a UD2 when it sees |
| 2381 | * __builtin_unreachable(). The BUG() macro has an unreachable() after |
| 2382 | * the UD2, which causes GCC's undefined trap logic to emit another UD2 |
| 2383 | * (or occasionally a JMP to UD2). |
| 2384 | */ |
| 2385 | if (list_prev_entry(insn, list)->dead_end && |
| 2386 | (insn->type == INSN_BUG || |
| 2387 | (insn->type == INSN_JUMP_UNCONDITIONAL && |
| 2388 | insn->jump_dest && insn->jump_dest->type == INSN_BUG))) |
| 2389 | return true; |
| 2390 | |
| 2391 | /* |
| 2392 | * Check if this (or a subsequent) instruction is related to |
| 2393 | * CONFIG_UBSAN or CONFIG_KASAN. |
| 2394 | * |
| 2395 | * End the search at 5 instructions to avoid going into the weeds. |
| 2396 | */ |
| 2397 | for (i = 0; i < 5; i++) { |
| 2398 | |
| 2399 | if (is_kasan_insn(insn) || is_ubsan_insn(insn)) |
| 2400 | return true; |
| 2401 | |
| 2402 | if (insn->type == INSN_JUMP_UNCONDITIONAL) { |
| 2403 | if (insn->jump_dest && |
| 2404 | insn->jump_dest->func == insn->func) { |
| 2405 | insn = insn->jump_dest; |
| 2406 | continue; |
| 2407 | } |
| 2408 | |
| 2409 | break; |
| 2410 | } |
| 2411 | |
| 2412 | if (insn->offset + insn->len >= insn->func->offset + insn->func->len) |
| 2413 | break; |
| 2414 | |
| 2415 | insn = list_next_entry(insn, list); |
| 2416 | } |
| 2417 | |
| 2418 | return false; |
| 2419 | } |
| 2420 | |
| 2421 | static int validate_functions(struct objtool_file *file) |
| 2422 | { |
| 2423 | struct section *sec; |
| 2424 | struct symbol *func; |
| 2425 | struct instruction *insn; |
| 2426 | struct insn_state state; |
| 2427 | int ret, warnings = 0; |
| 2428 | |
| 2429 | clear_insn_state(&state); |
| 2430 | |
| 2431 | state.cfa = initial_func_cfi.cfa; |
| 2432 | memcpy(&state.regs, &initial_func_cfi.regs, |
| 2433 | CFI_NUM_REGS * sizeof(struct cfi_reg)); |
| 2434 | state.stack_size = initial_func_cfi.cfa.offset; |
| 2435 | |
| 2436 | for_each_sec(file, sec) { |
| 2437 | list_for_each_entry(func, &sec->symbol_list, list) { |
| 2438 | if (func->type != STT_FUNC) |
| 2439 | continue; |
| 2440 | |
| 2441 | if (!func->len) { |
| 2442 | WARN("%s() is missing an ELF size annotation", |
| 2443 | func->name); |
| 2444 | warnings++; |
| 2445 | } |
| 2446 | |
| 2447 | if (func->pfunc != func || func->alias != func) |
| 2448 | continue; |
| 2449 | |
| 2450 | insn = find_insn(file, sec, func->offset); |
| 2451 | if (!insn || insn->ignore || insn->visited) |
| 2452 | continue; |
| 2453 | |
| 2454 | state.uaccess = func->uaccess_safe; |
| 2455 | |
| 2456 | ret = validate_branch(file, func, insn, state); |
| 2457 | if (ret && backtrace) |
| 2458 | BT_FUNC("<=== (func)", insn); |
| 2459 | warnings += ret; |
| 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | return warnings; |
| 2464 | } |
| 2465 | |
| 2466 | static int validate_reachable_instructions(struct objtool_file *file) |
| 2467 | { |
| 2468 | struct instruction *insn; |
| 2469 | |
| 2470 | if (file->ignore_unreachables) |
| 2471 | return 0; |
| 2472 | |
| 2473 | for_each_insn(file, insn) { |
| 2474 | if (insn->visited || ignore_unreachable_insn(insn)) |
| 2475 | continue; |
| 2476 | |
| 2477 | WARN_FUNC("unreachable instruction", insn->sec, insn->offset); |
| 2478 | return 1; |
| 2479 | } |
| 2480 | |
| 2481 | return 0; |
| 2482 | } |
| 2483 | |
| 2484 | static void cleanup(struct objtool_file *file) |
| 2485 | { |
| 2486 | struct instruction *insn, *tmpinsn; |
| 2487 | struct alternative *alt, *tmpalt; |
| 2488 | |
| 2489 | list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) { |
| 2490 | list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) { |
| 2491 | list_del(&alt->list); |
| 2492 | free(alt); |
| 2493 | } |
| 2494 | list_del(&insn->list); |
| 2495 | hash_del(&insn->hash); |
| 2496 | free(insn); |
| 2497 | } |
| 2498 | elf_close(file->elf); |
| 2499 | } |
| 2500 | |
| 2501 | static struct objtool_file file; |
| 2502 | |
| 2503 | int check(const char *_objname, bool orc) |
| 2504 | { |
| 2505 | int ret, warnings = 0; |
| 2506 | |
| 2507 | objname = _objname; |
| 2508 | |
| 2509 | file.elf = elf_read(objname, orc ? O_RDWR : O_RDONLY); |
| 2510 | if (!file.elf) |
| 2511 | return 1; |
| 2512 | |
| 2513 | INIT_LIST_HEAD(&file.insn_list); |
| 2514 | hash_init(file.insn_hash); |
| 2515 | file.c_file = find_section_by_name(file.elf, ".comment"); |
| 2516 | file.ignore_unreachables = no_unreachable; |
| 2517 | file.hints = false; |
| 2518 | |
| 2519 | arch_initial_func_cfi_state(&initial_func_cfi); |
| 2520 | |
| 2521 | ret = decode_sections(&file); |
| 2522 | if (ret < 0) |
| 2523 | goto out; |
| 2524 | warnings += ret; |
| 2525 | |
| 2526 | if (list_empty(&file.insn_list)) |
| 2527 | goto out; |
| 2528 | |
| 2529 | if (retpoline) { |
| 2530 | ret = validate_retpoline(&file); |
| 2531 | if (ret < 0) |
| 2532 | return ret; |
| 2533 | warnings += ret; |
| 2534 | } |
| 2535 | |
| 2536 | ret = validate_functions(&file); |
| 2537 | if (ret < 0) |
| 2538 | goto out; |
| 2539 | warnings += ret; |
| 2540 | |
| 2541 | ret = validate_unwind_hints(&file); |
| 2542 | if (ret < 0) |
| 2543 | goto out; |
| 2544 | warnings += ret; |
| 2545 | |
| 2546 | if (!warnings) { |
| 2547 | ret = validate_reachable_instructions(&file); |
| 2548 | if (ret < 0) |
| 2549 | goto out; |
| 2550 | warnings += ret; |
| 2551 | } |
| 2552 | |
| 2553 | if (orc) { |
| 2554 | ret = create_orc(&file); |
| 2555 | if (ret < 0) |
| 2556 | goto out; |
| 2557 | |
| 2558 | ret = create_orc_sections(&file); |
| 2559 | if (ret < 0) |
| 2560 | goto out; |
| 2561 | |
| 2562 | ret = elf_write(file.elf); |
| 2563 | if (ret < 0) |
| 2564 | goto out; |
| 2565 | } |
| 2566 | |
| 2567 | out: |
| 2568 | cleanup(&file); |
| 2569 | |
| 2570 | /* ignore warnings for now until we get all the code cleaned up */ |
| 2571 | if (ret || warnings) |
| 2572 | return 0; |
| 2573 | return 0; |
| 2574 | } |