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