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