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