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