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