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