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