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