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