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