Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 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 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 30 | struct alternative { |
| 31 | struct list_head list; |
| 32 | struct instruction *insn; |
| 33 | }; |
| 34 | |
| 35 | const char *objname; |
Josh Poimboeuf | 867ac9d | 2017-07-24 18:34:14 -0500 | [diff] [blame] | 36 | static bool no_fp; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 37 | struct cfi_state initial_func_cfi; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 38 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 39 | struct instruction *find_insn(struct objtool_file *file, |
| 40 | struct section *sec, unsigned long offset) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 41 | { |
| 42 | struct instruction *insn; |
| 43 | |
| 44 | hash_for_each_possible(file->insn_hash, insn, hash, offset) |
| 45 | if (insn->sec == sec && insn->offset == offset) |
| 46 | return insn; |
| 47 | |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | static struct instruction *next_insn_same_sec(struct objtool_file *file, |
| 52 | struct instruction *insn) |
| 53 | { |
| 54 | struct instruction *next = list_next_entry(insn, list); |
| 55 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 56 | if (!next || &next->list == &file->insn_list || next->sec != insn->sec) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 57 | return NULL; |
| 58 | |
| 59 | return next; |
| 60 | } |
| 61 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 62 | #define func_for_each_insn(file, func, insn) \ |
| 63 | for (insn = find_insn(file, func->sec, func->offset); \ |
| 64 | insn && &insn->list != &file->insn_list && \ |
| 65 | insn->sec == func->sec && \ |
| 66 | insn->offset < func->offset + func->len; \ |
| 67 | insn = list_next_entry(insn, list)) |
| 68 | |
| 69 | #define func_for_each_insn_continue_reverse(file, func, insn) \ |
| 70 | for (insn = list_prev_entry(insn, list); \ |
| 71 | &insn->list != &file->insn_list && \ |
| 72 | insn->sec == func->sec && insn->offset >= func->offset; \ |
| 73 | insn = list_prev_entry(insn, list)) |
| 74 | |
| 75 | #define sec_for_each_insn_from(file, insn) \ |
| 76 | for (; insn; insn = next_insn_same_sec(file, insn)) |
| 77 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 78 | #define sec_for_each_insn_continue(file, insn) \ |
| 79 | for (insn = next_insn_same_sec(file, insn); insn; \ |
| 80 | insn = next_insn_same_sec(file, insn)) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * Check if the function has been manually whitelisted with the |
| 84 | * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted |
| 85 | * due to its use of a context switching instruction. |
| 86 | */ |
| 87 | static bool ignore_func(struct objtool_file *file, struct symbol *func) |
| 88 | { |
| 89 | struct rela *rela; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 90 | |
| 91 | /* check for STACK_FRAME_NON_STANDARD */ |
| 92 | if (file->whitelist && file->whitelist->rela) |
| 93 | list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) { |
| 94 | if (rela->sym->type == STT_SECTION && |
| 95 | rela->sym->sec == func->sec && |
| 96 | rela->addend == func->offset) |
| 97 | return true; |
| 98 | if (rela->sym->type == STT_FUNC && rela->sym == func) |
| 99 | return true; |
| 100 | } |
| 101 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * This checks to see if the given function is a "noreturn" function. |
| 107 | * |
| 108 | * For global functions which are outside the scope of this object file, we |
| 109 | * have to keep a manual list of them. |
| 110 | * |
| 111 | * For local functions, we have to detect them manually by simply looking for |
| 112 | * the lack of a return instruction. |
| 113 | * |
| 114 | * Returns: |
| 115 | * -1: error |
| 116 | * 0: no dead end |
| 117 | * 1: dead end |
| 118 | */ |
| 119 | static int __dead_end_function(struct objtool_file *file, struct symbol *func, |
| 120 | int recursion) |
| 121 | { |
| 122 | int i; |
| 123 | struct instruction *insn; |
| 124 | bool empty = true; |
| 125 | |
| 126 | /* |
| 127 | * Unfortunately these have to be hard coded because the noreturn |
| 128 | * attribute isn't provided in ELF data. |
| 129 | */ |
| 130 | static const char * const global_noreturns[] = { |
| 131 | "__stack_chk_fail", |
| 132 | "panic", |
| 133 | "do_exit", |
| 134 | "do_task_dead", |
| 135 | "__module_put_and_exit", |
| 136 | "complete_and_exit", |
| 137 | "kvm_spurious_fault", |
| 138 | "__reiserfs_panic", |
| 139 | "lbug_with_loc", |
| 140 | "fortify_panic", |
| 141 | }; |
| 142 | |
| 143 | if (func->bind == STB_WEAK) |
| 144 | return 0; |
| 145 | |
| 146 | if (func->bind == STB_GLOBAL) |
| 147 | for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) |
| 148 | if (!strcmp(func->name, global_noreturns[i])) |
| 149 | return 1; |
| 150 | |
| 151 | if (!func->sec) |
| 152 | return 0; |
| 153 | |
| 154 | func_for_each_insn(file, func, insn) { |
| 155 | empty = false; |
| 156 | |
| 157 | if (insn->type == INSN_RETURN) |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | if (empty) |
| 162 | return 0; |
| 163 | |
| 164 | /* |
| 165 | * A function can have a sibling call instead of a return. In that |
| 166 | * case, the function's dead-end status depends on whether the target |
| 167 | * of the sibling call returns. |
| 168 | */ |
| 169 | func_for_each_insn(file, func, insn) { |
| 170 | if (insn->sec != func->sec || |
| 171 | insn->offset >= func->offset + func->len) |
| 172 | break; |
| 173 | |
| 174 | if (insn->type == INSN_JUMP_UNCONDITIONAL) { |
| 175 | struct instruction *dest = insn->jump_dest; |
| 176 | struct symbol *dest_func; |
| 177 | |
| 178 | if (!dest) |
| 179 | /* sibling call to another file */ |
| 180 | return 0; |
| 181 | |
| 182 | if (dest->sec != func->sec || |
| 183 | dest->offset < func->offset || |
| 184 | dest->offset >= func->offset + func->len) { |
| 185 | /* local sibling call */ |
| 186 | dest_func = find_symbol_by_offset(dest->sec, |
| 187 | dest->offset); |
| 188 | if (!dest_func) |
| 189 | continue; |
| 190 | |
| 191 | if (recursion == 5) { |
| 192 | WARN_FUNC("infinite recursion (objtool bug!)", |
| 193 | dest->sec, dest->offset); |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | return __dead_end_function(file, dest_func, |
| 198 | recursion + 1); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts)) |
| 203 | /* sibling call */ |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | static int dead_end_function(struct objtool_file *file, struct symbol *func) |
| 211 | { |
| 212 | return __dead_end_function(file, func, 0); |
| 213 | } |
| 214 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 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; |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 221 | for (i = 0; i < CFI_NUM_REGS; i++) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 222 | state->regs[i].base = CFI_UNDEFINED; |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 223 | state->vals[i].base = CFI_UNDEFINED; |
| 224 | } |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 225 | state->drap_reg = CFI_UNDEFINED; |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 226 | state->drap_offset = -1; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 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 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 241 | for_each_sec(file, sec) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 242 | |
| 243 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) |
| 244 | continue; |
| 245 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 246 | if (strcmp(sec->name, ".altinstr_replacement") && |
| 247 | strcmp(sec->name, ".altinstr_aux") && |
| 248 | strncmp(sec->name, ".discard.", 9)) |
| 249 | sec->text = true; |
| 250 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 251 | for (offset = 0; offset < sec->len; offset += insn->len) { |
| 252 | insn = malloc(sizeof(*insn)); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 253 | if (!insn) { |
| 254 | WARN("malloc failed"); |
| 255 | return -1; |
| 256 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 257 | memset(insn, 0, sizeof(*insn)); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 258 | INIT_LIST_HEAD(&insn->alts); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 259 | clear_insn_state(&insn->state); |
| 260 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 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, |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 267 | &insn->immediate, |
| 268 | &insn->stack_op); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 269 | if (ret) |
Kamalesh Babulal | b703798 | 2017-10-19 11:27:24 -0500 | [diff] [blame] | 270 | goto err; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 271 | |
| 272 | if (!insn->type || insn->type > INSN_LAST) { |
| 273 | WARN_FUNC("invalid instruction type %d", |
| 274 | insn->sec, insn->offset, insn->type); |
Kamalesh Babulal | b703798 | 2017-10-19 11:27:24 -0500 | [diff] [blame] | 275 | ret = -1; |
| 276 | goto err; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | hash_add(file->insn_hash, &insn->hash, insn->offset); |
| 280 | list_add_tail(&insn->list, &file->insn_list); |
| 281 | } |
| 282 | |
| 283 | list_for_each_entry(func, &sec->symbol_list, list) { |
| 284 | if (func->type != STT_FUNC) |
| 285 | continue; |
| 286 | |
| 287 | if (!find_insn(file, sec, func->offset)) { |
| 288 | WARN("%s(): can't find starting instruction", |
| 289 | func->name); |
| 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | func_for_each_insn(file, func, insn) |
| 294 | if (!insn->func) |
| 295 | insn->func = func; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return 0; |
Kamalesh Babulal | b703798 | 2017-10-19 11:27:24 -0500 | [diff] [blame] | 300 | |
| 301 | err: |
| 302 | free(insn); |
| 303 | return ret; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* |
Josh Poimboeuf | 649ea4d | 2017-07-27 15:56:53 -0500 | [diff] [blame] | 307 | * Mark "ud2" instructions and manually annotated dead ends. |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 308 | */ |
| 309 | static int add_dead_ends(struct objtool_file *file) |
| 310 | { |
| 311 | struct section *sec; |
| 312 | struct rela *rela; |
| 313 | struct instruction *insn; |
| 314 | bool found; |
| 315 | |
Josh Poimboeuf | 649ea4d | 2017-07-27 15:56:53 -0500 | [diff] [blame] | 316 | /* |
| 317 | * By default, "ud2" is a dead end unless otherwise annotated, because |
| 318 | * GCC 7 inserts it for certain divide-by-zero cases. |
| 319 | */ |
| 320 | for_each_insn(file, insn) |
| 321 | if (insn->type == INSN_BUG) |
| 322 | insn->dead_end = true; |
| 323 | |
| 324 | /* |
| 325 | * Check for manually annotated dead ends. |
| 326 | */ |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 327 | sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); |
| 328 | if (!sec) |
Josh Poimboeuf | 649ea4d | 2017-07-27 15:56:53 -0500 | [diff] [blame] | 329 | goto reachable; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 330 | |
| 331 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 332 | if (rela->sym->type != STT_SECTION) { |
| 333 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 334 | return -1; |
| 335 | } |
| 336 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 337 | if (insn) |
| 338 | insn = list_prev_entry(insn, list); |
| 339 | else if (rela->addend == rela->sym->sec->len) { |
| 340 | found = false; |
| 341 | list_for_each_entry_reverse(insn, &file->insn_list, list) { |
| 342 | if (insn->sec == rela->sym->sec) { |
| 343 | found = true; |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if (!found) { |
| 349 | WARN("can't find unreachable insn at %s+0x%x", |
| 350 | rela->sym->sec->name, rela->addend); |
| 351 | return -1; |
| 352 | } |
| 353 | } else { |
| 354 | WARN("can't find unreachable insn at %s+0x%x", |
| 355 | rela->sym->sec->name, rela->addend); |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | insn->dead_end = true; |
| 360 | } |
| 361 | |
Josh Poimboeuf | 649ea4d | 2017-07-27 15:56:53 -0500 | [diff] [blame] | 362 | reachable: |
| 363 | /* |
| 364 | * These manually annotated reachable checks are needed for GCC 4.4, |
| 365 | * where the Linux unreachable() macro isn't supported. In that case |
| 366 | * GCC doesn't know the "ud2" is fatal, so it generates code as if it's |
| 367 | * not a dead end. |
| 368 | */ |
| 369 | sec = find_section_by_name(file->elf, ".rela.discard.reachable"); |
| 370 | if (!sec) |
| 371 | return 0; |
| 372 | |
| 373 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 374 | if (rela->sym->type != STT_SECTION) { |
| 375 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 376 | return -1; |
| 377 | } |
| 378 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 379 | if (insn) |
| 380 | insn = list_prev_entry(insn, list); |
| 381 | else if (rela->addend == rela->sym->sec->len) { |
| 382 | found = false; |
| 383 | list_for_each_entry_reverse(insn, &file->insn_list, list) { |
| 384 | if (insn->sec == rela->sym->sec) { |
| 385 | found = true; |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if (!found) { |
| 391 | WARN("can't find reachable insn at %s+0x%x", |
| 392 | rela->sym->sec->name, rela->addend); |
| 393 | return -1; |
| 394 | } |
| 395 | } else { |
| 396 | WARN("can't find reachable insn at %s+0x%x", |
| 397 | rela->sym->sec->name, rela->addend); |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | insn->dead_end = false; |
| 402 | } |
| 403 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Warnings shouldn't be reported for ignored functions. |
| 409 | */ |
| 410 | static void add_ignores(struct objtool_file *file) |
| 411 | { |
| 412 | struct instruction *insn; |
| 413 | struct section *sec; |
| 414 | struct symbol *func; |
| 415 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 416 | for_each_sec(file, sec) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 417 | list_for_each_entry(func, &sec->symbol_list, list) { |
| 418 | if (func->type != STT_FUNC) |
| 419 | continue; |
| 420 | |
| 421 | if (!ignore_func(file, func)) |
| 422 | continue; |
| 423 | |
| 424 | func_for_each_insn(file, func, insn) |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 425 | insn->ignore = true; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /* |
Josh Poimboeuf | 258c760 | 2018-01-11 21:46:24 +0000 | [diff] [blame] | 431 | * FIXME: For now, just ignore any alternatives which add retpolines. This is |
| 432 | * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline. |
| 433 | * But it at least allows objtool to understand the control flow *around* the |
| 434 | * retpoline. |
| 435 | */ |
| 436 | static int add_nospec_ignores(struct objtool_file *file) |
| 437 | { |
| 438 | struct section *sec; |
| 439 | struct rela *rela; |
| 440 | struct instruction *insn; |
| 441 | |
| 442 | sec = find_section_by_name(file->elf, ".rela.discard.nospec"); |
| 443 | if (!sec) |
| 444 | return 0; |
| 445 | |
| 446 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 447 | if (rela->sym->type != STT_SECTION) { |
| 448 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 453 | if (!insn) { |
| 454 | WARN("bad .discard.nospec entry"); |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | insn->ignore_alts = true; |
| 459 | } |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | /* |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 465 | * Find the destination instructions for all jumps. |
| 466 | */ |
| 467 | static int add_jump_destinations(struct objtool_file *file) |
| 468 | { |
| 469 | struct instruction *insn; |
| 470 | struct rela *rela; |
| 471 | struct section *dest_sec; |
| 472 | unsigned long dest_off; |
| 473 | |
| 474 | for_each_insn(file, insn) { |
| 475 | if (insn->type != INSN_JUMP_CONDITIONAL && |
| 476 | insn->type != INSN_JUMP_UNCONDITIONAL) |
| 477 | continue; |
| 478 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 479 | if (insn->ignore) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 480 | continue; |
| 481 | |
| 482 | rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 483 | insn->len); |
| 484 | if (!rela) { |
| 485 | dest_sec = insn->sec; |
| 486 | dest_off = insn->offset + insn->len + insn->immediate; |
| 487 | } else if (rela->sym->type == STT_SECTION) { |
| 488 | dest_sec = rela->sym->sec; |
| 489 | dest_off = rela->addend + 4; |
| 490 | } else if (rela->sym->sec->idx) { |
| 491 | dest_sec = rela->sym->sec; |
| 492 | dest_off = rela->sym->sym.st_value + rela->addend + 4; |
Josh Poimboeuf | 39b7353 | 2018-01-11 21:46:23 +0000 | [diff] [blame] | 493 | } else if (strstr(rela->sym->name, "_indirect_thunk_")) { |
| 494 | /* |
| 495 | * Retpoline jumps are really dynamic jumps in |
| 496 | * disguise, so convert them accordingly. |
| 497 | */ |
| 498 | insn->type = INSN_JUMP_DYNAMIC; |
| 499 | continue; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 500 | } else { |
| 501 | /* sibling call */ |
| 502 | insn->jump_dest = 0; |
| 503 | continue; |
| 504 | } |
| 505 | |
| 506 | insn->jump_dest = find_insn(file, dest_sec, dest_off); |
| 507 | if (!insn->jump_dest) { |
| 508 | |
| 509 | /* |
| 510 | * This is a special case where an alt instruction |
| 511 | * jumps past the end of the section. These are |
| 512 | * handled later in handle_group_alt(). |
| 513 | */ |
| 514 | if (!strcmp(insn->sec->name, ".altinstr_replacement")) |
| 515 | continue; |
| 516 | |
| 517 | WARN_FUNC("can't find jump dest instruction at %s+0x%lx", |
| 518 | insn->sec, insn->offset, dest_sec->name, |
| 519 | dest_off); |
| 520 | return -1; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Find the destination instructions for all calls. |
| 529 | */ |
| 530 | static int add_call_destinations(struct objtool_file *file) |
| 531 | { |
| 532 | struct instruction *insn; |
| 533 | unsigned long dest_off; |
| 534 | struct rela *rela; |
| 535 | |
| 536 | for_each_insn(file, insn) { |
| 537 | if (insn->type != INSN_CALL) |
| 538 | continue; |
| 539 | |
| 540 | rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 541 | insn->len); |
| 542 | if (!rela) { |
| 543 | dest_off = insn->offset + insn->len + insn->immediate; |
| 544 | insn->call_dest = find_symbol_by_offset(insn->sec, |
| 545 | dest_off); |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame^] | 546 | |
| 547 | if (!insn->call_dest && !insn->ignore) { |
| 548 | WARN_FUNC("unsupported intra-function call", |
| 549 | insn->sec, insn->offset); |
| 550 | WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE."); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 551 | return -1; |
| 552 | } |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame^] | 553 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 554 | } else if (rela->sym->type == STT_SECTION) { |
| 555 | insn->call_dest = find_symbol_by_offset(rela->sym->sec, |
| 556 | rela->addend+4); |
| 557 | if (!insn->call_dest || |
| 558 | insn->call_dest->type != STT_FUNC) { |
| 559 | WARN_FUNC("can't find call dest symbol at %s+0x%x", |
| 560 | insn->sec, insn->offset, |
| 561 | rela->sym->sec->name, |
| 562 | rela->addend + 4); |
| 563 | return -1; |
| 564 | } |
| 565 | } else |
| 566 | insn->call_dest = rela->sym; |
| 567 | } |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * The .alternatives section requires some extra special care, over and above |
| 574 | * what other special sections require: |
| 575 | * |
| 576 | * 1. Because alternatives are patched in-place, we need to insert a fake jump |
| 577 | * instruction at the end so that validate_branch() skips all the original |
| 578 | * replaced instructions when validating the new instruction path. |
| 579 | * |
| 580 | * 2. An added wrinkle is that the new instruction length might be zero. In |
| 581 | * that case the old instructions are replaced with noops. We simulate that |
| 582 | * by creating a fake jump as the only new instruction. |
| 583 | * |
| 584 | * 3. In some cases, the alternative section includes an instruction which |
| 585 | * conditionally jumps to the _end_ of the entry. We have to modify these |
| 586 | * jumps' destinations to point back to .text rather than the end of the |
| 587 | * entry in .altinstr_replacement. |
| 588 | * |
| 589 | * 4. It has been requested that we don't validate the !POPCNT feature path |
| 590 | * which is a "very very small percentage of machines". |
| 591 | */ |
| 592 | static int handle_group_alt(struct objtool_file *file, |
| 593 | struct special_alt *special_alt, |
| 594 | struct instruction *orig_insn, |
| 595 | struct instruction **new_insn) |
| 596 | { |
| 597 | struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump; |
| 598 | unsigned long dest_off; |
| 599 | |
| 600 | last_orig_insn = NULL; |
| 601 | insn = orig_insn; |
| 602 | sec_for_each_insn_from(file, insn) { |
| 603 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) |
| 604 | break; |
| 605 | |
| 606 | if (special_alt->skip_orig) |
| 607 | insn->type = INSN_NOP; |
| 608 | |
| 609 | insn->alt_group = true; |
| 610 | last_orig_insn = insn; |
| 611 | } |
| 612 | |
| 613 | if (!next_insn_same_sec(file, last_orig_insn)) { |
| 614 | WARN("%s: don't know how to handle alternatives at end of section", |
| 615 | special_alt->orig_sec->name); |
| 616 | return -1; |
| 617 | } |
| 618 | |
| 619 | fake_jump = malloc(sizeof(*fake_jump)); |
| 620 | if (!fake_jump) { |
| 621 | WARN("malloc failed"); |
| 622 | return -1; |
| 623 | } |
| 624 | memset(fake_jump, 0, sizeof(*fake_jump)); |
| 625 | INIT_LIST_HEAD(&fake_jump->alts); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 626 | clear_insn_state(&fake_jump->state); |
| 627 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 628 | fake_jump->sec = special_alt->new_sec; |
| 629 | fake_jump->offset = -1; |
| 630 | fake_jump->type = INSN_JUMP_UNCONDITIONAL; |
| 631 | fake_jump->jump_dest = list_next_entry(last_orig_insn, list); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 632 | fake_jump->ignore = true; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 633 | |
| 634 | if (!special_alt->new_len) { |
| 635 | *new_insn = fake_jump; |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | last_new_insn = NULL; |
| 640 | insn = *new_insn; |
| 641 | sec_for_each_insn_from(file, insn) { |
| 642 | if (insn->offset >= special_alt->new_off + special_alt->new_len) |
| 643 | break; |
| 644 | |
| 645 | last_new_insn = insn; |
| 646 | |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame^] | 647 | insn->ignore = orig_insn->ignore_alts; |
| 648 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 649 | if (insn->type != INSN_JUMP_CONDITIONAL && |
| 650 | insn->type != INSN_JUMP_UNCONDITIONAL) |
| 651 | continue; |
| 652 | |
| 653 | if (!insn->immediate) |
| 654 | continue; |
| 655 | |
| 656 | dest_off = insn->offset + insn->len + insn->immediate; |
| 657 | if (dest_off == special_alt->new_off + special_alt->new_len) |
| 658 | insn->jump_dest = fake_jump; |
| 659 | |
| 660 | if (!insn->jump_dest) { |
| 661 | WARN_FUNC("can't find alternative jump destination", |
| 662 | insn->sec, insn->offset); |
| 663 | return -1; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | if (!last_new_insn) { |
| 668 | WARN_FUNC("can't find last new alternative instruction", |
| 669 | special_alt->new_sec, special_alt->new_off); |
| 670 | return -1; |
| 671 | } |
| 672 | |
| 673 | list_add(&fake_jump->list, &last_new_insn->list); |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * A jump table entry can either convert a nop to a jump or a jump to a nop. |
| 680 | * If the original instruction is a jump, make the alt entry an effective nop |
| 681 | * by just skipping the original instruction. |
| 682 | */ |
| 683 | static int handle_jump_alt(struct objtool_file *file, |
| 684 | struct special_alt *special_alt, |
| 685 | struct instruction *orig_insn, |
| 686 | struct instruction **new_insn) |
| 687 | { |
| 688 | if (orig_insn->type == INSN_NOP) |
| 689 | return 0; |
| 690 | |
| 691 | if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { |
| 692 | WARN_FUNC("unsupported instruction at jump label", |
| 693 | orig_insn->sec, orig_insn->offset); |
| 694 | return -1; |
| 695 | } |
| 696 | |
| 697 | *new_insn = list_next_entry(orig_insn, list); |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | * Read all the special sections which have alternate instructions which can be |
| 703 | * patched in or redirected to at runtime. Each instruction having alternate |
| 704 | * instruction(s) has them added to its insn->alts list, which will be |
| 705 | * traversed in validate_branch(). |
| 706 | */ |
| 707 | static int add_special_section_alts(struct objtool_file *file) |
| 708 | { |
| 709 | struct list_head special_alts; |
| 710 | struct instruction *orig_insn, *new_insn; |
| 711 | struct special_alt *special_alt, *tmp; |
| 712 | struct alternative *alt; |
| 713 | int ret; |
| 714 | |
| 715 | ret = special_get_alts(file->elf, &special_alts); |
| 716 | if (ret) |
| 717 | return ret; |
| 718 | |
| 719 | list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 720 | |
| 721 | orig_insn = find_insn(file, special_alt->orig_sec, |
| 722 | special_alt->orig_off); |
| 723 | if (!orig_insn) { |
| 724 | WARN_FUNC("special: can't find orig instruction", |
| 725 | special_alt->orig_sec, special_alt->orig_off); |
| 726 | ret = -1; |
| 727 | goto out; |
| 728 | } |
| 729 | |
| 730 | new_insn = NULL; |
| 731 | if (!special_alt->group || special_alt->new_len) { |
| 732 | new_insn = find_insn(file, special_alt->new_sec, |
| 733 | special_alt->new_off); |
| 734 | if (!new_insn) { |
| 735 | WARN_FUNC("special: can't find new instruction", |
| 736 | special_alt->new_sec, |
| 737 | special_alt->new_off); |
| 738 | ret = -1; |
| 739 | goto out; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | if (special_alt->group) { |
| 744 | ret = handle_group_alt(file, special_alt, orig_insn, |
| 745 | &new_insn); |
| 746 | if (ret) |
| 747 | goto out; |
| 748 | } else if (special_alt->jump_or_nop) { |
| 749 | ret = handle_jump_alt(file, special_alt, orig_insn, |
| 750 | &new_insn); |
| 751 | if (ret) |
| 752 | goto out; |
| 753 | } |
| 754 | |
Josh Poimboeuf | 258c760 | 2018-01-11 21:46:24 +0000 | [diff] [blame] | 755 | alt = malloc(sizeof(*alt)); |
| 756 | if (!alt) { |
| 757 | WARN("malloc failed"); |
| 758 | ret = -1; |
| 759 | goto out; |
| 760 | } |
| 761 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 762 | alt->insn = new_insn; |
| 763 | list_add_tail(&alt->list, &orig_insn->alts); |
| 764 | |
| 765 | list_del(&special_alt->list); |
| 766 | free(special_alt); |
| 767 | } |
| 768 | |
| 769 | out: |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | static int add_switch_table(struct objtool_file *file, struct symbol *func, |
| 774 | struct instruction *insn, struct rela *table, |
| 775 | struct rela *next_table) |
| 776 | { |
| 777 | struct rela *rela = table; |
| 778 | struct instruction *alt_insn; |
| 779 | struct alternative *alt; |
| 780 | |
| 781 | list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) { |
| 782 | if (rela == next_table) |
| 783 | break; |
| 784 | |
| 785 | if (rela->sym->sec != insn->sec || |
| 786 | rela->addend <= func->offset || |
| 787 | rela->addend >= func->offset + func->len) |
| 788 | break; |
| 789 | |
| 790 | alt_insn = find_insn(file, insn->sec, rela->addend); |
| 791 | if (!alt_insn) { |
| 792 | WARN("%s: can't find instruction at %s+0x%x", |
| 793 | file->rodata->rela->name, insn->sec->name, |
| 794 | rela->addend); |
| 795 | return -1; |
| 796 | } |
| 797 | |
| 798 | alt = malloc(sizeof(*alt)); |
| 799 | if (!alt) { |
| 800 | WARN("malloc failed"); |
| 801 | return -1; |
| 802 | } |
| 803 | |
| 804 | alt->insn = alt_insn; |
| 805 | list_add_tail(&alt->list, &insn->alts); |
| 806 | } |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | * find_switch_table() - Given a dynamic jump, find the switch jump table in |
| 813 | * .rodata associated with it. |
| 814 | * |
| 815 | * There are 3 basic patterns: |
| 816 | * |
| 817 | * 1. jmpq *[rodata addr](,%reg,8) |
| 818 | * |
| 819 | * This is the most common case by far. It jumps to an address in a simple |
| 820 | * jump table which is stored in .rodata. |
| 821 | * |
| 822 | * 2. jmpq *[rodata addr](%rip) |
| 823 | * |
| 824 | * This is caused by a rare GCC quirk, currently only seen in three driver |
| 825 | * functions in the kernel, only with certain obscure non-distro configs. |
| 826 | * |
| 827 | * As part of an optimization, GCC makes a copy of an existing switch jump |
| 828 | * table, modifies it, and then hard-codes the jump (albeit with an indirect |
| 829 | * jump) to use a single entry in the table. The rest of the jump table and |
| 830 | * some of its jump targets remain as dead code. |
| 831 | * |
| 832 | * In such a case we can just crudely ignore all unreachable instruction |
| 833 | * warnings for the entire object file. Ideally we would just ignore them |
| 834 | * for the function, but that would require redesigning the code quite a |
| 835 | * bit. And honestly that's just not worth doing: unreachable instruction |
| 836 | * warnings are of questionable value anyway, and this is such a rare issue. |
| 837 | * |
| 838 | * 3. mov [rodata addr],%reg1 |
| 839 | * ... some instructions ... |
| 840 | * jmpq *(%reg1,%reg2,8) |
| 841 | * |
| 842 | * This is a fairly uncommon pattern which is new for GCC 6. As of this |
| 843 | * writing, there are 11 occurrences of it in the allmodconfig kernel. |
| 844 | * |
| 845 | * TODO: Once we have DWARF CFI and smarter instruction decoding logic, |
| 846 | * ensure the same register is used in the mov and jump instructions. |
| 847 | */ |
| 848 | static struct rela *find_switch_table(struct objtool_file *file, |
| 849 | struct symbol *func, |
| 850 | struct instruction *insn) |
| 851 | { |
| 852 | struct rela *text_rela, *rodata_rela; |
| 853 | struct instruction *orig_insn = insn; |
| 854 | |
| 855 | text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len); |
| 856 | if (text_rela && text_rela->sym == file->rodata->sym) { |
| 857 | /* case 1 */ |
| 858 | rodata_rela = find_rela_by_dest(file->rodata, |
| 859 | text_rela->addend); |
| 860 | if (rodata_rela) |
| 861 | return rodata_rela; |
| 862 | |
| 863 | /* case 2 */ |
| 864 | rodata_rela = find_rela_by_dest(file->rodata, |
| 865 | text_rela->addend + 4); |
| 866 | if (!rodata_rela) |
| 867 | return NULL; |
| 868 | file->ignore_unreachables = true; |
| 869 | return rodata_rela; |
| 870 | } |
| 871 | |
| 872 | /* case 3 */ |
| 873 | func_for_each_insn_continue_reverse(file, func, insn) { |
| 874 | if (insn->type == INSN_JUMP_DYNAMIC) |
| 875 | break; |
| 876 | |
| 877 | /* allow small jumps within the range */ |
| 878 | if (insn->type == INSN_JUMP_UNCONDITIONAL && |
| 879 | insn->jump_dest && |
| 880 | (insn->jump_dest->offset <= insn->offset || |
| 881 | insn->jump_dest->offset > orig_insn->offset)) |
| 882 | break; |
| 883 | |
| 884 | /* look for a relocation which references .rodata */ |
| 885 | text_rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 886 | insn->len); |
| 887 | if (!text_rela || text_rela->sym != file->rodata->sym) |
| 888 | continue; |
| 889 | |
| 890 | /* |
| 891 | * Make sure the .rodata address isn't associated with a |
| 892 | * symbol. gcc jump tables are anonymous data. |
| 893 | */ |
| 894 | if (find_symbol_containing(file->rodata, text_rela->addend)) |
| 895 | continue; |
| 896 | |
| 897 | return find_rela_by_dest(file->rodata, text_rela->addend); |
| 898 | } |
| 899 | |
| 900 | return NULL; |
| 901 | } |
| 902 | |
| 903 | static int add_func_switch_tables(struct objtool_file *file, |
| 904 | struct symbol *func) |
| 905 | { |
| 906 | struct instruction *insn, *prev_jump = NULL; |
| 907 | struct rela *rela, *prev_rela = NULL; |
| 908 | int ret; |
| 909 | |
| 910 | func_for_each_insn(file, func, insn) { |
| 911 | if (insn->type != INSN_JUMP_DYNAMIC) |
| 912 | continue; |
| 913 | |
| 914 | rela = find_switch_table(file, func, insn); |
| 915 | if (!rela) |
| 916 | continue; |
| 917 | |
| 918 | /* |
| 919 | * We found a switch table, but we don't know yet how big it |
| 920 | * is. Don't add it until we reach the end of the function or |
| 921 | * the beginning of another switch table in the same function. |
| 922 | */ |
| 923 | if (prev_jump) { |
| 924 | ret = add_switch_table(file, func, prev_jump, prev_rela, |
| 925 | rela); |
| 926 | if (ret) |
| 927 | return ret; |
| 928 | } |
| 929 | |
| 930 | prev_jump = insn; |
| 931 | prev_rela = rela; |
| 932 | } |
| 933 | |
| 934 | if (prev_jump) { |
| 935 | ret = add_switch_table(file, func, prev_jump, prev_rela, NULL); |
| 936 | if (ret) |
| 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * For some switch statements, gcc generates a jump table in the .rodata |
| 945 | * section which contains a list of addresses within the function to jump to. |
| 946 | * This finds these jump tables and adds them to the insn->alts lists. |
| 947 | */ |
| 948 | static int add_switch_table_alts(struct objtool_file *file) |
| 949 | { |
| 950 | struct section *sec; |
| 951 | struct symbol *func; |
| 952 | int ret; |
| 953 | |
| 954 | if (!file->rodata || !file->rodata->rela) |
| 955 | return 0; |
| 956 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 957 | for_each_sec(file, sec) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 958 | list_for_each_entry(func, &sec->symbol_list, list) { |
| 959 | if (func->type != STT_FUNC) |
| 960 | continue; |
| 961 | |
| 962 | ret = add_func_switch_tables(file, func); |
| 963 | if (ret) |
| 964 | return ret; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 971 | static int read_unwind_hints(struct objtool_file *file) |
| 972 | { |
| 973 | struct section *sec, *relasec; |
| 974 | struct rela *rela; |
| 975 | struct unwind_hint *hint; |
| 976 | struct instruction *insn; |
| 977 | struct cfi_reg *cfa; |
| 978 | int i; |
| 979 | |
| 980 | sec = find_section_by_name(file->elf, ".discard.unwind_hints"); |
| 981 | if (!sec) |
| 982 | return 0; |
| 983 | |
| 984 | relasec = sec->rela; |
| 985 | if (!relasec) { |
| 986 | WARN("missing .rela.discard.unwind_hints section"); |
| 987 | return -1; |
| 988 | } |
| 989 | |
| 990 | if (sec->len % sizeof(struct unwind_hint)) { |
| 991 | WARN("struct unwind_hint size mismatch"); |
| 992 | return -1; |
| 993 | } |
| 994 | |
| 995 | file->hints = true; |
| 996 | |
| 997 | for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) { |
| 998 | hint = (struct unwind_hint *)sec->data->d_buf + i; |
| 999 | |
| 1000 | rela = find_rela_by_dest(sec, i * sizeof(*hint)); |
| 1001 | if (!rela) { |
| 1002 | WARN("can't find rela for unwind_hints[%d]", i); |
| 1003 | return -1; |
| 1004 | } |
| 1005 | |
| 1006 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 1007 | if (!insn) { |
| 1008 | WARN("can't find insn for unwind_hints[%d]", i); |
| 1009 | return -1; |
| 1010 | } |
| 1011 | |
| 1012 | cfa = &insn->state.cfa; |
| 1013 | |
| 1014 | if (hint->type == UNWIND_HINT_TYPE_SAVE) { |
| 1015 | insn->save = true; |
| 1016 | continue; |
| 1017 | |
| 1018 | } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) { |
| 1019 | insn->restore = true; |
| 1020 | insn->hint = true; |
| 1021 | continue; |
| 1022 | } |
| 1023 | |
| 1024 | insn->hint = true; |
| 1025 | |
| 1026 | switch (hint->sp_reg) { |
| 1027 | case ORC_REG_UNDEFINED: |
| 1028 | cfa->base = CFI_UNDEFINED; |
| 1029 | break; |
| 1030 | case ORC_REG_SP: |
| 1031 | cfa->base = CFI_SP; |
| 1032 | break; |
| 1033 | case ORC_REG_BP: |
| 1034 | cfa->base = CFI_BP; |
| 1035 | break; |
| 1036 | case ORC_REG_SP_INDIRECT: |
| 1037 | cfa->base = CFI_SP_INDIRECT; |
| 1038 | break; |
| 1039 | case ORC_REG_R10: |
| 1040 | cfa->base = CFI_R10; |
| 1041 | break; |
| 1042 | case ORC_REG_R13: |
| 1043 | cfa->base = CFI_R13; |
| 1044 | break; |
| 1045 | case ORC_REG_DI: |
| 1046 | cfa->base = CFI_DI; |
| 1047 | break; |
| 1048 | case ORC_REG_DX: |
| 1049 | cfa->base = CFI_DX; |
| 1050 | break; |
| 1051 | default: |
| 1052 | WARN_FUNC("unsupported unwind_hint sp base reg %d", |
| 1053 | insn->sec, insn->offset, hint->sp_reg); |
| 1054 | return -1; |
| 1055 | } |
| 1056 | |
| 1057 | cfa->offset = hint->sp_offset; |
| 1058 | insn->state.type = hint->type; |
| 1059 | } |
| 1060 | |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1064 | static int decode_sections(struct objtool_file *file) |
| 1065 | { |
| 1066 | int ret; |
| 1067 | |
| 1068 | ret = decode_instructions(file); |
| 1069 | if (ret) |
| 1070 | return ret; |
| 1071 | |
| 1072 | ret = add_dead_ends(file); |
| 1073 | if (ret) |
| 1074 | return ret; |
| 1075 | |
| 1076 | add_ignores(file); |
| 1077 | |
Josh Poimboeuf | 258c760 | 2018-01-11 21:46:24 +0000 | [diff] [blame] | 1078 | ret = add_nospec_ignores(file); |
| 1079 | if (ret) |
| 1080 | return ret; |
| 1081 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1082 | ret = add_jump_destinations(file); |
| 1083 | if (ret) |
| 1084 | return ret; |
| 1085 | |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame^] | 1086 | ret = add_special_section_alts(file); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1087 | if (ret) |
| 1088 | return ret; |
| 1089 | |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame^] | 1090 | ret = add_call_destinations(file); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1091 | if (ret) |
| 1092 | return ret; |
| 1093 | |
| 1094 | ret = add_switch_table_alts(file); |
| 1095 | if (ret) |
| 1096 | return ret; |
| 1097 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1098 | ret = read_unwind_hints(file); |
| 1099 | if (ret) |
| 1100 | return ret; |
| 1101 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | static bool is_fentry_call(struct instruction *insn) |
| 1106 | { |
| 1107 | if (insn->type == INSN_CALL && |
| 1108 | insn->call_dest->type == STT_NOTYPE && |
| 1109 | !strcmp(insn->call_dest->name, "__fentry__")) |
| 1110 | return true; |
| 1111 | |
| 1112 | return false; |
| 1113 | } |
| 1114 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1115 | static bool has_modified_stack_frame(struct insn_state *state) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1116 | { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1117 | int i; |
| 1118 | |
| 1119 | if (state->cfa.base != initial_func_cfi.cfa.base || |
| 1120 | state->cfa.offset != initial_func_cfi.cfa.offset || |
| 1121 | state->stack_size != initial_func_cfi.cfa.offset || |
| 1122 | state->drap) |
| 1123 | return true; |
| 1124 | |
| 1125 | for (i = 0; i < CFI_NUM_REGS; i++) |
| 1126 | if (state->regs[i].base != initial_func_cfi.regs[i].base || |
| 1127 | state->regs[i].offset != initial_func_cfi.regs[i].offset) |
| 1128 | return true; |
| 1129 | |
| 1130 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1131 | } |
| 1132 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1133 | static bool has_valid_stack_frame(struct insn_state *state) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1134 | { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1135 | if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA && |
| 1136 | state->regs[CFI_BP].offset == -16) |
| 1137 | return true; |
| 1138 | |
| 1139 | if (state->drap && state->regs[CFI_BP].base == CFI_BP) |
| 1140 | return true; |
| 1141 | |
| 1142 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1143 | } |
| 1144 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 1145 | static int update_insn_state_regs(struct instruction *insn, struct insn_state *state) |
| 1146 | { |
| 1147 | struct cfi_reg *cfa = &state->cfa; |
| 1148 | struct stack_op *op = &insn->stack_op; |
| 1149 | |
| 1150 | if (cfa->base != CFI_SP) |
| 1151 | return 0; |
| 1152 | |
| 1153 | /* push */ |
| 1154 | if (op->dest.type == OP_DEST_PUSH) |
| 1155 | cfa->offset += 8; |
| 1156 | |
| 1157 | /* pop */ |
| 1158 | if (op->src.type == OP_SRC_POP) |
| 1159 | cfa->offset -= 8; |
| 1160 | |
| 1161 | /* add immediate to sp */ |
| 1162 | if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD && |
| 1163 | op->dest.reg == CFI_SP && op->src.reg == CFI_SP) |
| 1164 | cfa->offset -= op->src.offset; |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1169 | static void save_reg(struct insn_state *state, unsigned char reg, int base, |
| 1170 | int offset) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1171 | { |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1172 | if (arch_callee_saved_reg(reg) && |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1173 | state->regs[reg].base == CFI_UNDEFINED) { |
| 1174 | state->regs[reg].base = base; |
| 1175 | state->regs[reg].offset = offset; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | static void restore_reg(struct insn_state *state, unsigned char reg) |
| 1180 | { |
| 1181 | state->regs[reg].base = CFI_UNDEFINED; |
| 1182 | state->regs[reg].offset = 0; |
| 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * A note about DRAP stack alignment: |
| 1187 | * |
| 1188 | * GCC has the concept of a DRAP register, which is used to help keep track of |
| 1189 | * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP |
| 1190 | * register. The typical DRAP pattern is: |
| 1191 | * |
| 1192 | * 4c 8d 54 24 08 lea 0x8(%rsp),%r10 |
| 1193 | * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp |
| 1194 | * 41 ff 72 f8 pushq -0x8(%r10) |
| 1195 | * 55 push %rbp |
| 1196 | * 48 89 e5 mov %rsp,%rbp |
| 1197 | * (more pushes) |
| 1198 | * 41 52 push %r10 |
| 1199 | * ... |
| 1200 | * 41 5a pop %r10 |
| 1201 | * (more pops) |
| 1202 | * 5d pop %rbp |
| 1203 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 1204 | * c3 retq |
| 1205 | * |
| 1206 | * There are some variations in the epilogues, like: |
| 1207 | * |
| 1208 | * 5b pop %rbx |
| 1209 | * 41 5a pop %r10 |
| 1210 | * 41 5c pop %r12 |
| 1211 | * 41 5d pop %r13 |
| 1212 | * 41 5e pop %r14 |
| 1213 | * c9 leaveq |
| 1214 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 1215 | * c3 retq |
| 1216 | * |
| 1217 | * and: |
| 1218 | * |
| 1219 | * 4c 8b 55 e8 mov -0x18(%rbp),%r10 |
| 1220 | * 48 8b 5d e0 mov -0x20(%rbp),%rbx |
| 1221 | * 4c 8b 65 f0 mov -0x10(%rbp),%r12 |
| 1222 | * 4c 8b 6d f8 mov -0x8(%rbp),%r13 |
| 1223 | * c9 leaveq |
| 1224 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 1225 | * c3 retq |
| 1226 | * |
| 1227 | * Sometimes r13 is used as the DRAP register, in which case it's saved and |
| 1228 | * restored beforehand: |
| 1229 | * |
| 1230 | * 41 55 push %r13 |
| 1231 | * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13 |
| 1232 | * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp |
| 1233 | * ... |
| 1234 | * 49 8d 65 f0 lea -0x10(%r13),%rsp |
| 1235 | * 41 5d pop %r13 |
| 1236 | * c3 retq |
| 1237 | */ |
| 1238 | static int update_insn_state(struct instruction *insn, struct insn_state *state) |
| 1239 | { |
| 1240 | struct stack_op *op = &insn->stack_op; |
| 1241 | struct cfi_reg *cfa = &state->cfa; |
| 1242 | struct cfi_reg *regs = state->regs; |
| 1243 | |
| 1244 | /* stack operations don't make sense with an undefined CFA */ |
| 1245 | if (cfa->base == CFI_UNDEFINED) { |
| 1246 | if (insn->func) { |
| 1247 | WARN_FUNC("undefined stack state", insn->sec, insn->offset); |
| 1248 | return -1; |
| 1249 | } |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 1253 | if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET) |
| 1254 | return update_insn_state_regs(insn, state); |
| 1255 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1256 | switch (op->dest.type) { |
| 1257 | |
| 1258 | case OP_DEST_REG: |
| 1259 | switch (op->src.type) { |
| 1260 | |
| 1261 | case OP_SRC_REG: |
Josh Poimboeuf | 0d0970e | 2017-09-20 16:24:32 -0500 | [diff] [blame] | 1262 | if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP && |
| 1263 | cfa->base == CFI_SP && |
| 1264 | regs[CFI_BP].base == CFI_CFA && |
| 1265 | regs[CFI_BP].offset == -cfa->offset) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1266 | |
Josh Poimboeuf | 0d0970e | 2017-09-20 16:24:32 -0500 | [diff] [blame] | 1267 | /* mov %rsp, %rbp */ |
| 1268 | cfa->base = op->dest.reg; |
| 1269 | state->bp_scratch = false; |
| 1270 | } |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1271 | |
Josh Poimboeuf | 0d0970e | 2017-09-20 16:24:32 -0500 | [diff] [blame] | 1272 | else if (op->src.reg == CFI_SP && |
| 1273 | op->dest.reg == CFI_BP && state->drap) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1274 | |
Josh Poimboeuf | 0d0970e | 2017-09-20 16:24:32 -0500 | [diff] [blame] | 1275 | /* drap: mov %rsp, %rbp */ |
| 1276 | regs[CFI_BP].base = CFI_BP; |
| 1277 | regs[CFI_BP].offset = -state->stack_size; |
| 1278 | state->bp_scratch = false; |
| 1279 | } |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 1280 | |
Josh Poimboeuf | 0d0970e | 2017-09-20 16:24:32 -0500 | [diff] [blame] | 1281 | else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
| 1282 | |
| 1283 | /* |
| 1284 | * mov %rsp, %reg |
| 1285 | * |
| 1286 | * This is needed for the rare case where GCC |
| 1287 | * does: |
| 1288 | * |
| 1289 | * mov %rsp, %rax |
| 1290 | * ... |
| 1291 | * mov %rax, %rsp |
| 1292 | */ |
| 1293 | state->vals[op->dest.reg].base = CFI_CFA; |
| 1294 | state->vals[op->dest.reg].offset = -state->stack_size; |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | else if (op->dest.reg == cfa->base) { |
| 1298 | |
| 1299 | /* mov %reg, %rsp */ |
| 1300 | if (cfa->base == CFI_SP && |
| 1301 | state->vals[op->src.reg].base == CFI_CFA) { |
| 1302 | |
| 1303 | /* |
| 1304 | * This is needed for the rare case |
| 1305 | * where GCC does something dumb like: |
| 1306 | * |
| 1307 | * lea 0x8(%rsp), %rcx |
| 1308 | * ... |
| 1309 | * mov %rcx, %rsp |
| 1310 | */ |
| 1311 | cfa->offset = -state->vals[op->src.reg].offset; |
| 1312 | state->stack_size = cfa->offset; |
| 1313 | |
| 1314 | } else { |
| 1315 | cfa->base = CFI_UNDEFINED; |
| 1316 | cfa->offset = 0; |
| 1317 | } |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | break; |
| 1321 | |
| 1322 | case OP_SRC_ADD: |
| 1323 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) { |
| 1324 | |
| 1325 | /* add imm, %rsp */ |
| 1326 | state->stack_size -= op->src.offset; |
| 1327 | if (cfa->base == CFI_SP) |
| 1328 | cfa->offset -= op->src.offset; |
| 1329 | break; |
| 1330 | } |
| 1331 | |
| 1332 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) { |
| 1333 | |
| 1334 | /* lea disp(%rbp), %rsp */ |
| 1335 | state->stack_size = -(op->src.offset + regs[CFI_BP].offset); |
| 1336 | break; |
| 1337 | } |
| 1338 | |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 1339 | if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1340 | |
| 1341 | /* drap: lea disp(%rsp), %drap */ |
| 1342 | state->drap_reg = op->dest.reg; |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 1343 | |
| 1344 | /* |
| 1345 | * lea disp(%rsp), %reg |
| 1346 | * |
| 1347 | * This is needed for the rare case where GCC |
| 1348 | * does something dumb like: |
| 1349 | * |
| 1350 | * lea 0x8(%rsp), %rcx |
| 1351 | * ... |
| 1352 | * mov %rcx, %rsp |
| 1353 | */ |
| 1354 | state->vals[op->dest.reg].base = CFI_CFA; |
| 1355 | state->vals[op->dest.reg].offset = \ |
| 1356 | -state->stack_size + op->src.offset; |
| 1357 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1358 | break; |
| 1359 | } |
| 1360 | |
| 1361 | if (state->drap && op->dest.reg == CFI_SP && |
| 1362 | op->src.reg == state->drap_reg) { |
| 1363 | |
| 1364 | /* drap: lea disp(%drap), %rsp */ |
| 1365 | cfa->base = CFI_SP; |
| 1366 | cfa->offset = state->stack_size = -op->src.offset; |
| 1367 | state->drap_reg = CFI_UNDEFINED; |
| 1368 | state->drap = false; |
| 1369 | break; |
| 1370 | } |
| 1371 | |
| 1372 | if (op->dest.reg == state->cfa.base) { |
| 1373 | WARN_FUNC("unsupported stack register modification", |
| 1374 | insn->sec, insn->offset); |
| 1375 | return -1; |
| 1376 | } |
| 1377 | |
| 1378 | break; |
| 1379 | |
| 1380 | case OP_SRC_AND: |
| 1381 | if (op->dest.reg != CFI_SP || |
| 1382 | (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) || |
| 1383 | (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) { |
| 1384 | WARN_FUNC("unsupported stack pointer realignment", |
| 1385 | insn->sec, insn->offset); |
| 1386 | return -1; |
| 1387 | } |
| 1388 | |
| 1389 | if (state->drap_reg != CFI_UNDEFINED) { |
| 1390 | /* drap: and imm, %rsp */ |
| 1391 | cfa->base = state->drap_reg; |
| 1392 | cfa->offset = state->stack_size = 0; |
| 1393 | state->drap = true; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | /* |
| 1397 | * Older versions of GCC (4.8ish) realign the stack |
| 1398 | * without DRAP, with a frame pointer. |
| 1399 | */ |
| 1400 | |
| 1401 | break; |
| 1402 | |
| 1403 | case OP_SRC_POP: |
| 1404 | if (!state->drap && op->dest.type == OP_DEST_REG && |
| 1405 | op->dest.reg == cfa->base) { |
| 1406 | |
| 1407 | /* pop %rbp */ |
| 1408 | cfa->base = CFI_SP; |
| 1409 | } |
| 1410 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1411 | if (state->drap && cfa->base == CFI_BP_INDIRECT && |
| 1412 | op->dest.type == OP_DEST_REG && |
| 1413 | op->dest.reg == state->drap_reg && |
| 1414 | state->drap_offset == -state->stack_size) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1415 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1416 | /* drap: pop %drap */ |
| 1417 | cfa->base = state->drap_reg; |
| 1418 | cfa->offset = 0; |
| 1419 | state->drap_offset = -1; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1420 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1421 | } else if (regs[op->dest.reg].offset == -state->stack_size) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1422 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1423 | /* pop %reg */ |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1424 | restore_reg(state, op->dest.reg); |
| 1425 | } |
| 1426 | |
| 1427 | state->stack_size -= 8; |
| 1428 | if (cfa->base == CFI_SP) |
| 1429 | cfa->offset -= 8; |
| 1430 | |
| 1431 | break; |
| 1432 | |
| 1433 | case OP_SRC_REG_INDIRECT: |
| 1434 | if (state->drap && op->src.reg == CFI_BP && |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1435 | op->src.offset == state->drap_offset) { |
| 1436 | |
| 1437 | /* drap: mov disp(%rbp), %drap */ |
| 1438 | cfa->base = state->drap_reg; |
| 1439 | cfa->offset = 0; |
| 1440 | state->drap_offset = -1; |
| 1441 | } |
| 1442 | |
| 1443 | if (state->drap && op->src.reg == CFI_BP && |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1444 | op->src.offset == regs[op->dest.reg].offset) { |
| 1445 | |
| 1446 | /* drap: mov disp(%rbp), %reg */ |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1447 | restore_reg(state, op->dest.reg); |
| 1448 | |
| 1449 | } else if (op->src.reg == cfa->base && |
| 1450 | op->src.offset == regs[op->dest.reg].offset + cfa->offset) { |
| 1451 | |
| 1452 | /* mov disp(%rbp), %reg */ |
| 1453 | /* mov disp(%rsp), %reg */ |
| 1454 | restore_reg(state, op->dest.reg); |
| 1455 | } |
| 1456 | |
| 1457 | break; |
| 1458 | |
| 1459 | default: |
| 1460 | WARN_FUNC("unknown stack-related instruction", |
| 1461 | insn->sec, insn->offset); |
| 1462 | return -1; |
| 1463 | } |
| 1464 | |
| 1465 | break; |
| 1466 | |
| 1467 | case OP_DEST_PUSH: |
| 1468 | state->stack_size += 8; |
| 1469 | if (cfa->base == CFI_SP) |
| 1470 | cfa->offset += 8; |
| 1471 | |
| 1472 | if (op->src.type != OP_SRC_REG) |
| 1473 | break; |
| 1474 | |
| 1475 | if (state->drap) { |
| 1476 | if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) { |
| 1477 | |
| 1478 | /* drap: push %drap */ |
| 1479 | cfa->base = CFI_BP_INDIRECT; |
| 1480 | cfa->offset = -state->stack_size; |
| 1481 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1482 | /* save drap so we know when to restore it */ |
| 1483 | state->drap_offset = -state->stack_size; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1484 | |
| 1485 | } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) { |
| 1486 | |
| 1487 | /* drap: push %rbp */ |
| 1488 | state->stack_size = 0; |
| 1489 | |
| 1490 | } else if (regs[op->src.reg].base == CFI_UNDEFINED) { |
| 1491 | |
| 1492 | /* drap: push %reg */ |
| 1493 | save_reg(state, op->src.reg, CFI_BP, -state->stack_size); |
| 1494 | } |
| 1495 | |
| 1496 | } else { |
| 1497 | |
| 1498 | /* push %reg */ |
| 1499 | save_reg(state, op->src.reg, CFI_CFA, -state->stack_size); |
| 1500 | } |
| 1501 | |
| 1502 | /* detect when asm code uses rbp as a scratch register */ |
Josh Poimboeuf | 867ac9d | 2017-07-24 18:34:14 -0500 | [diff] [blame] | 1503 | if (!no_fp && insn->func && op->src.reg == CFI_BP && |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1504 | cfa->base != CFI_BP) |
| 1505 | state->bp_scratch = true; |
| 1506 | break; |
| 1507 | |
| 1508 | case OP_DEST_REG_INDIRECT: |
| 1509 | |
| 1510 | if (state->drap) { |
| 1511 | if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) { |
| 1512 | |
| 1513 | /* drap: mov %drap, disp(%rbp) */ |
| 1514 | cfa->base = CFI_BP_INDIRECT; |
| 1515 | cfa->offset = op->dest.offset; |
| 1516 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1517 | /* save drap offset so we know when to restore it */ |
| 1518 | state->drap_offset = op->dest.offset; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | else if (regs[op->src.reg].base == CFI_UNDEFINED) { |
| 1522 | |
| 1523 | /* drap: mov reg, disp(%rbp) */ |
| 1524 | save_reg(state, op->src.reg, CFI_BP, op->dest.offset); |
| 1525 | } |
| 1526 | |
| 1527 | } else if (op->dest.reg == cfa->base) { |
| 1528 | |
| 1529 | /* mov reg, disp(%rbp) */ |
| 1530 | /* mov reg, disp(%rsp) */ |
| 1531 | save_reg(state, op->src.reg, CFI_CFA, |
| 1532 | op->dest.offset - state->cfa.offset); |
| 1533 | } |
| 1534 | |
| 1535 | break; |
| 1536 | |
| 1537 | case OP_DEST_LEAVE: |
| 1538 | if ((!state->drap && cfa->base != CFI_BP) || |
| 1539 | (state->drap && cfa->base != state->drap_reg)) { |
| 1540 | WARN_FUNC("leave instruction with modified stack frame", |
| 1541 | insn->sec, insn->offset); |
| 1542 | return -1; |
| 1543 | } |
| 1544 | |
| 1545 | /* leave (mov %rbp, %rsp; pop %rbp) */ |
| 1546 | |
| 1547 | state->stack_size = -state->regs[CFI_BP].offset - 8; |
| 1548 | restore_reg(state, CFI_BP); |
| 1549 | |
| 1550 | if (!state->drap) { |
| 1551 | cfa->base = CFI_SP; |
| 1552 | cfa->offset -= 8; |
| 1553 | } |
| 1554 | |
| 1555 | break; |
| 1556 | |
| 1557 | case OP_DEST_MEM: |
| 1558 | if (op->src.type != OP_SRC_POP) { |
| 1559 | WARN_FUNC("unknown stack-related memory operation", |
| 1560 | insn->sec, insn->offset); |
| 1561 | return -1; |
| 1562 | } |
| 1563 | |
| 1564 | /* pop mem */ |
| 1565 | state->stack_size -= 8; |
| 1566 | if (cfa->base == CFI_SP) |
| 1567 | cfa->offset -= 8; |
| 1568 | |
| 1569 | break; |
| 1570 | |
| 1571 | default: |
| 1572 | WARN_FUNC("unknown stack-related instruction", |
| 1573 | insn->sec, insn->offset); |
| 1574 | return -1; |
| 1575 | } |
| 1576 | |
| 1577 | return 0; |
| 1578 | } |
| 1579 | |
| 1580 | static bool insn_state_match(struct instruction *insn, struct insn_state *state) |
| 1581 | { |
| 1582 | struct insn_state *state1 = &insn->state, *state2 = state; |
| 1583 | int i; |
| 1584 | |
| 1585 | if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) { |
| 1586 | WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d", |
| 1587 | insn->sec, insn->offset, |
| 1588 | state1->cfa.base, state1->cfa.offset, |
| 1589 | state2->cfa.base, state2->cfa.offset); |
| 1590 | |
| 1591 | } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) { |
| 1592 | for (i = 0; i < CFI_NUM_REGS; i++) { |
| 1593 | if (!memcmp(&state1->regs[i], &state2->regs[i], |
| 1594 | sizeof(struct cfi_reg))) |
| 1595 | continue; |
| 1596 | |
| 1597 | WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d", |
| 1598 | insn->sec, insn->offset, |
| 1599 | i, state1->regs[i].base, state1->regs[i].offset, |
| 1600 | i, state2->regs[i].base, state2->regs[i].offset); |
| 1601 | break; |
| 1602 | } |
| 1603 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 1604 | } else if (state1->type != state2->type) { |
| 1605 | WARN_FUNC("stack state mismatch: type1=%d type2=%d", |
| 1606 | insn->sec, insn->offset, state1->type, state2->type); |
| 1607 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1608 | } else if (state1->drap != state2->drap || |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1609 | (state1->drap && state1->drap_reg != state2->drap_reg) || |
| 1610 | (state1->drap && state1->drap_offset != state2->drap_offset)) { |
| 1611 | WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)", |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1612 | insn->sec, insn->offset, |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1613 | state1->drap, state1->drap_reg, state1->drap_offset, |
| 1614 | state2->drap, state2->drap_reg, state2->drap_offset); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1615 | |
| 1616 | } else |
| 1617 | return true; |
| 1618 | |
| 1619 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | /* |
| 1623 | * Follow the branch starting at the given instruction, and recursively follow |
| 1624 | * any other branches (jumps). Meanwhile, track the frame pointer state at |
| 1625 | * each instruction and validate all the rules described in |
| 1626 | * tools/objtool/Documentation/stack-validation.txt. |
| 1627 | */ |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1628 | static int validate_branch(struct objtool_file *file, struct instruction *first, |
| 1629 | struct insn_state state) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1630 | { |
| 1631 | struct alternative *alt; |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1632 | struct instruction *insn, *next_insn; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1633 | struct section *sec; |
| 1634 | struct symbol *func = NULL; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1635 | int ret; |
| 1636 | |
| 1637 | insn = first; |
| 1638 | sec = insn->sec; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1639 | |
| 1640 | if (insn->alt_group && list_empty(&insn->alts)) { |
| 1641 | WARN_FUNC("don't know how to handle branch to middle of alternative instruction group", |
| 1642 | sec, insn->offset); |
Josh Poimboeuf | 12b2572 | 2017-08-10 16:37:25 -0500 | [diff] [blame] | 1643 | return 1; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | while (1) { |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1647 | next_insn = next_insn_same_sec(file, insn); |
| 1648 | |
Josh Poimboeuf | ee97638 | 2017-08-11 12:24:15 -0500 | [diff] [blame] | 1649 | |
| 1650 | if (file->c_file && func && insn->func && func != insn->func) { |
| 1651 | WARN("%s() falls through to next function %s()", |
| 1652 | func->name, insn->func->name); |
| 1653 | return 1; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1654 | } |
| 1655 | |
Josh Poimboeuf | ee97638 | 2017-08-11 12:24:15 -0500 | [diff] [blame] | 1656 | if (insn->func) |
| 1657 | func = insn->func; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1658 | |
Josh Poimboeuf | 4855022 | 2017-07-07 09:19:42 -0500 | [diff] [blame] | 1659 | if (func && insn->ignore) { |
| 1660 | WARN_FUNC("BUG: why am I validating an ignored function?", |
| 1661 | sec, insn->offset); |
Josh Poimboeuf | 12b2572 | 2017-08-10 16:37:25 -0500 | [diff] [blame] | 1662 | return 1; |
Josh Poimboeuf | 4855022 | 2017-07-07 09:19:42 -0500 | [diff] [blame] | 1663 | } |
| 1664 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1665 | if (insn->visited) { |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1666 | if (!insn->hint && !insn_state_match(insn, &state)) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1667 | return 1; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1668 | |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1672 | if (insn->hint) { |
| 1673 | if (insn->restore) { |
| 1674 | struct instruction *save_insn, *i; |
| 1675 | |
| 1676 | i = insn; |
| 1677 | save_insn = NULL; |
| 1678 | func_for_each_insn_continue_reverse(file, func, i) { |
| 1679 | if (i->save) { |
| 1680 | save_insn = i; |
| 1681 | break; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | if (!save_insn) { |
| 1686 | WARN_FUNC("no corresponding CFI save for CFI restore", |
| 1687 | sec, insn->offset); |
| 1688 | return 1; |
| 1689 | } |
| 1690 | |
| 1691 | if (!save_insn->visited) { |
| 1692 | /* |
| 1693 | * Oops, no state to copy yet. |
| 1694 | * Hopefully we can reach this |
| 1695 | * instruction from another branch |
| 1696 | * after the save insn has been |
| 1697 | * visited. |
| 1698 | */ |
| 1699 | if (insn == first) |
| 1700 | return 0; |
| 1701 | |
| 1702 | WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo", |
| 1703 | sec, insn->offset); |
| 1704 | return 1; |
| 1705 | } |
| 1706 | |
| 1707 | insn->state = save_insn->state; |
| 1708 | } |
| 1709 | |
| 1710 | state = insn->state; |
| 1711 | |
| 1712 | } else |
| 1713 | insn->state = state; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1714 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1715 | insn->visited = true; |
| 1716 | |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame^] | 1717 | if (!insn->ignore_alts) { |
| 1718 | list_for_each_entry(alt, &insn->alts, list) { |
| 1719 | ret = validate_branch(file, alt->insn, state); |
| 1720 | if (ret) |
| 1721 | return 1; |
| 1722 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | switch (insn->type) { |
| 1726 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1727 | case INSN_RETURN: |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1728 | if (func && has_modified_stack_frame(&state)) { |
| 1729 | WARN_FUNC("return with modified stack frame", |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1730 | sec, insn->offset); |
| 1731 | return 1; |
| 1732 | } |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1733 | |
| 1734 | if (state.bp_scratch) { |
| 1735 | WARN("%s uses BP as a scratch register", |
| 1736 | insn->func->name); |
| 1737 | return 1; |
| 1738 | } |
| 1739 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1740 | return 0; |
| 1741 | |
| 1742 | case INSN_CALL: |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1743 | if (is_fentry_call(insn)) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1744 | break; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1745 | |
| 1746 | ret = dead_end_function(file, insn->call_dest); |
| 1747 | if (ret == 1) |
| 1748 | return 0; |
| 1749 | if (ret == -1) |
| 1750 | return 1; |
| 1751 | |
| 1752 | /* fallthrough */ |
| 1753 | case INSN_CALL_DYNAMIC: |
Josh Poimboeuf | 867ac9d | 2017-07-24 18:34:14 -0500 | [diff] [blame] | 1754 | if (!no_fp && func && !has_valid_stack_frame(&state)) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1755 | WARN_FUNC("call without frame pointer save/setup", |
| 1756 | sec, insn->offset); |
| 1757 | return 1; |
| 1758 | } |
| 1759 | break; |
| 1760 | |
| 1761 | case INSN_JUMP_CONDITIONAL: |
| 1762 | case INSN_JUMP_UNCONDITIONAL: |
Josh Poimboeuf | 4855022 | 2017-07-07 09:19:42 -0500 | [diff] [blame] | 1763 | if (insn->jump_dest && |
| 1764 | (!func || !insn->jump_dest->func || |
| 1765 | func == insn->jump_dest->func)) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1766 | ret = validate_branch(file, insn->jump_dest, |
| 1767 | state); |
| 1768 | if (ret) |
| 1769 | return 1; |
Josh Poimboeuf | 4855022 | 2017-07-07 09:19:42 -0500 | [diff] [blame] | 1770 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1771 | } else if (func && has_modified_stack_frame(&state)) { |
| 1772 | WARN_FUNC("sibling call from callable instruction with modified stack frame", |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1773 | sec, insn->offset); |
| 1774 | return 1; |
Josh Poimboeuf | 4855022 | 2017-07-07 09:19:42 -0500 | [diff] [blame] | 1775 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1776 | |
| 1777 | if (insn->type == INSN_JUMP_UNCONDITIONAL) |
| 1778 | return 0; |
| 1779 | |
| 1780 | break; |
| 1781 | |
| 1782 | case INSN_JUMP_DYNAMIC: |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1783 | if (func && list_empty(&insn->alts) && |
| 1784 | has_modified_stack_frame(&state)) { |
| 1785 | WARN_FUNC("sibling call from callable instruction with modified stack frame", |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1786 | sec, insn->offset); |
| 1787 | return 1; |
| 1788 | } |
| 1789 | |
| 1790 | return 0; |
| 1791 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1792 | case INSN_CONTEXT_SWITCH: |
| 1793 | if (func && (!next_insn || !next_insn->hint)) { |
| 1794 | WARN_FUNC("unsupported instruction in callable function", |
| 1795 | sec, insn->offset); |
| 1796 | return 1; |
| 1797 | } |
| 1798 | return 0; |
| 1799 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1800 | case INSN_STACK: |
| 1801 | if (update_insn_state(insn, &state)) |
Josh Poimboeuf | 12b2572 | 2017-08-10 16:37:25 -0500 | [diff] [blame] | 1802 | return 1; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1803 | |
| 1804 | break; |
| 1805 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1806 | default: |
| 1807 | break; |
| 1808 | } |
| 1809 | |
| 1810 | if (insn->dead_end) |
| 1811 | return 0; |
| 1812 | |
Josh Poimboeuf | 00d9618 | 2017-09-18 21:43:30 -0500 | [diff] [blame] | 1813 | if (!next_insn) { |
| 1814 | if (state.cfa.base == CFI_UNDEFINED) |
| 1815 | return 0; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1816 | WARN("%s: unexpected end of section", sec->name); |
| 1817 | return 1; |
| 1818 | } |
Josh Poimboeuf | 00d9618 | 2017-09-18 21:43:30 -0500 | [diff] [blame] | 1819 | |
| 1820 | insn = next_insn; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1826 | static int validate_unwind_hints(struct objtool_file *file) |
| 1827 | { |
| 1828 | struct instruction *insn; |
| 1829 | int ret, warnings = 0; |
| 1830 | struct insn_state state; |
| 1831 | |
| 1832 | if (!file->hints) |
| 1833 | return 0; |
| 1834 | |
| 1835 | clear_insn_state(&state); |
| 1836 | |
| 1837 | for_each_insn(file, insn) { |
| 1838 | if (insn->hint && !insn->visited) { |
| 1839 | ret = validate_branch(file, insn, state); |
| 1840 | warnings += ret; |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | return warnings; |
| 1845 | } |
| 1846 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1847 | static bool is_kasan_insn(struct instruction *insn) |
| 1848 | { |
| 1849 | return (insn->type == INSN_CALL && |
| 1850 | !strcmp(insn->call_dest->name, "__asan_handle_no_return")); |
| 1851 | } |
| 1852 | |
| 1853 | static bool is_ubsan_insn(struct instruction *insn) |
| 1854 | { |
| 1855 | return (insn->type == INSN_CALL && |
| 1856 | !strcmp(insn->call_dest->name, |
| 1857 | "__ubsan_handle_builtin_unreachable")); |
| 1858 | } |
| 1859 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1860 | static bool ignore_unreachable_insn(struct instruction *insn) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1861 | { |
| 1862 | int i; |
| 1863 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1864 | if (insn->ignore || insn->type == INSN_NOP) |
| 1865 | return true; |
| 1866 | |
| 1867 | /* |
| 1868 | * Ignore any unused exceptions. This can happen when a whitelisted |
| 1869 | * function has an exception table entry. |
Josh Poimboeuf | 0e2bb2b | 2017-07-27 15:56:54 -0500 | [diff] [blame] | 1870 | * |
| 1871 | * Also ignore alternative replacement instructions. This can happen |
| 1872 | * when a whitelisted function uses one of the ALTERNATIVE macros. |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1873 | */ |
Josh Poimboeuf | 0e2bb2b | 2017-07-27 15:56:54 -0500 | [diff] [blame] | 1874 | if (!strcmp(insn->sec->name, ".fixup") || |
| 1875 | !strcmp(insn->sec->name, ".altinstr_replacement") || |
| 1876 | !strcmp(insn->sec->name, ".altinstr_aux")) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1877 | return true; |
| 1878 | |
| 1879 | /* |
| 1880 | * Check if this (or a subsequent) instruction is related to |
| 1881 | * CONFIG_UBSAN or CONFIG_KASAN. |
| 1882 | * |
| 1883 | * End the search at 5 instructions to avoid going into the weeds. |
| 1884 | */ |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1885 | if (!insn->func) |
| 1886 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1887 | for (i = 0; i < 5; i++) { |
| 1888 | |
| 1889 | if (is_kasan_insn(insn) || is_ubsan_insn(insn)) |
| 1890 | return true; |
| 1891 | |
| 1892 | if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) { |
| 1893 | insn = insn->jump_dest; |
| 1894 | continue; |
| 1895 | } |
| 1896 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1897 | if (insn->offset + insn->len >= insn->func->offset + insn->func->len) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1898 | break; |
| 1899 | insn = list_next_entry(insn, list); |
| 1900 | } |
| 1901 | |
| 1902 | return false; |
| 1903 | } |
| 1904 | |
| 1905 | static int validate_functions(struct objtool_file *file) |
| 1906 | { |
| 1907 | struct section *sec; |
| 1908 | struct symbol *func; |
| 1909 | struct instruction *insn; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1910 | struct insn_state state; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1911 | int ret, warnings = 0; |
| 1912 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1913 | clear_insn_state(&state); |
| 1914 | |
| 1915 | state.cfa = initial_func_cfi.cfa; |
| 1916 | memcpy(&state.regs, &initial_func_cfi.regs, |
| 1917 | CFI_NUM_REGS * sizeof(struct cfi_reg)); |
| 1918 | state.stack_size = initial_func_cfi.cfa.offset; |
| 1919 | |
| 1920 | for_each_sec(file, sec) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1921 | list_for_each_entry(func, &sec->symbol_list, list) { |
| 1922 | if (func->type != STT_FUNC) |
| 1923 | continue; |
| 1924 | |
| 1925 | insn = find_insn(file, sec, func->offset); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1926 | if (!insn || insn->ignore) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1927 | continue; |
| 1928 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1929 | ret = validate_branch(file, insn, state); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1930 | warnings += ret; |
| 1931 | } |
| 1932 | } |
| 1933 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1934 | return warnings; |
| 1935 | } |
| 1936 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1937 | static int validate_reachable_instructions(struct objtool_file *file) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1938 | { |
| 1939 | struct instruction *insn; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1940 | |
| 1941 | if (file->ignore_unreachables) |
| 1942 | return 0; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1943 | |
| 1944 | for_each_insn(file, insn) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1945 | if (insn->visited || ignore_unreachable_insn(insn)) |
| 1946 | continue; |
| 1947 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1948 | WARN_FUNC("unreachable instruction", insn->sec, insn->offset); |
| 1949 | return 1; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1950 | } |
| 1951 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1952 | return 0; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | static void cleanup(struct objtool_file *file) |
| 1956 | { |
| 1957 | struct instruction *insn, *tmpinsn; |
| 1958 | struct alternative *alt, *tmpalt; |
| 1959 | |
| 1960 | list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) { |
| 1961 | list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) { |
| 1962 | list_del(&alt->list); |
| 1963 | free(alt); |
| 1964 | } |
| 1965 | list_del(&insn->list); |
| 1966 | hash_del(&insn->hash); |
| 1967 | free(insn); |
| 1968 | } |
| 1969 | elf_close(file->elf); |
| 1970 | } |
| 1971 | |
Josh Poimboeuf | 867ac9d | 2017-07-24 18:34:14 -0500 | [diff] [blame] | 1972 | int check(const char *_objname, bool _no_fp, bool no_unreachable, bool orc) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1973 | { |
| 1974 | struct objtool_file file; |
| 1975 | int ret, warnings = 0; |
| 1976 | |
| 1977 | objname = _objname; |
Josh Poimboeuf | 867ac9d | 2017-07-24 18:34:14 -0500 | [diff] [blame] | 1978 | no_fp = _no_fp; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1979 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 1980 | file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1981 | if (!file.elf) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1982 | return 1; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1983 | |
| 1984 | INIT_LIST_HEAD(&file.insn_list); |
| 1985 | hash_init(file.insn_hash); |
| 1986 | file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard"); |
| 1987 | file.rodata = find_section_by_name(file.elf, ".rodata"); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1988 | file.c_file = find_section_by_name(file.elf, ".comment"); |
Josh Poimboeuf | 867ac9d | 2017-07-24 18:34:14 -0500 | [diff] [blame] | 1989 | file.ignore_unreachables = no_unreachable; |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1990 | file.hints = false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1991 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1992 | arch_initial_func_cfi_state(&initial_func_cfi); |
| 1993 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1994 | ret = decode_sections(&file); |
| 1995 | if (ret < 0) |
| 1996 | goto out; |
| 1997 | warnings += ret; |
| 1998 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1999 | if (list_empty(&file.insn_list)) |
| 2000 | goto out; |
| 2001 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2002 | ret = validate_functions(&file); |
| 2003 | if (ret < 0) |
| 2004 | goto out; |
| 2005 | warnings += ret; |
| 2006 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 2007 | ret = validate_unwind_hints(&file); |
| 2008 | if (ret < 0) |
| 2009 | goto out; |
| 2010 | warnings += ret; |
| 2011 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2012 | if (!warnings) { |
| 2013 | ret = validate_reachable_instructions(&file); |
| 2014 | if (ret < 0) |
| 2015 | goto out; |
| 2016 | warnings += ret; |
| 2017 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2018 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 2019 | if (orc) { |
| 2020 | ret = create_orc(&file); |
| 2021 | if (ret < 0) |
| 2022 | goto out; |
| 2023 | |
| 2024 | ret = create_orc_sections(&file); |
| 2025 | if (ret < 0) |
| 2026 | goto out; |
| 2027 | |
| 2028 | ret = elf_write(file.elf); |
| 2029 | if (ret < 0) |
| 2030 | goto out; |
| 2031 | } |
| 2032 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2033 | out: |
| 2034 | cleanup(&file); |
| 2035 | |
| 2036 | /* ignore warnings for now until we get all the code cleaned up */ |
| 2037 | if (ret || warnings) |
| 2038 | return 0; |
| 2039 | return 0; |
| 2040 | } |