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