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