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