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