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" |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 10 | #include "check.h" |
| 11 | #include "elf.h" |
| 12 | #include "special.h" |
| 13 | #include "arch.h" |
| 14 | #include "warn.h" |
| 15 | |
| 16 | #include <linux/hashtable.h> |
| 17 | #include <linux/kernel.h> |
| 18 | |
Josh Poimboeuf | e6da956 | 2019-05-13 12:01:31 -0500 | [diff] [blame] | 19 | #define FAKE_JUMP_OFFSET -1 |
| 20 | |
Josh Poimboeuf | 87b512d | 2019-06-27 20:50:46 -0500 | [diff] [blame] | 21 | #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table" |
| 22 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 23 | struct alternative { |
| 24 | struct list_head list; |
| 25 | struct instruction *insn; |
Peter Zijlstra | 764eef4 | 2019-03-01 11:19:03 +0100 | [diff] [blame] | 26 | bool skip_orig; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | const char *objname; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 30 | struct cfi_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 | |
| 37 | hash_for_each_possible(file->insn_hash, insn, hash, offset) |
| 38 | if (insn->sec == sec && insn->offset == offset) |
| 39 | return insn; |
| 40 | |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | static struct instruction *next_insn_same_sec(struct objtool_file *file, |
| 45 | struct instruction *insn) |
| 46 | { |
| 47 | struct instruction *next = list_next_entry(insn, list); |
| 48 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 49 | if (!next || &next->list == &file->insn_list || next->sec != insn->sec) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 50 | return NULL; |
| 51 | |
| 52 | return next; |
| 53 | } |
| 54 | |
Josh Poimboeuf | 1381043 | 2018-05-09 22:39:15 -0500 | [diff] [blame] | 55 | static struct instruction *next_insn_same_func(struct objtool_file *file, |
| 56 | struct instruction *insn) |
| 57 | { |
| 58 | struct instruction *next = list_next_entry(insn, list); |
| 59 | struct symbol *func = insn->func; |
| 60 | |
| 61 | if (!func) |
| 62 | return NULL; |
| 63 | |
| 64 | if (&next->list != &file->insn_list && next->func == func) |
| 65 | return next; |
| 66 | |
| 67 | /* Check if we're already in the subfunction: */ |
| 68 | if (func == func->cfunc) |
| 69 | return NULL; |
| 70 | |
| 71 | /* Move to the subfunction: */ |
| 72 | return find_insn(file, func->cfunc->sec, func->cfunc->offset); |
| 73 | } |
| 74 | |
| 75 | #define func_for_each_insn_all(file, func, insn) \ |
| 76 | for (insn = find_insn(file, func->sec, func->offset); \ |
| 77 | insn; \ |
| 78 | insn = next_insn_same_func(file, insn)) |
| 79 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 80 | #define func_for_each_insn(file, func, insn) \ |
| 81 | for (insn = find_insn(file, func->sec, func->offset); \ |
| 82 | insn && &insn->list != &file->insn_list && \ |
| 83 | insn->sec == func->sec && \ |
| 84 | insn->offset < func->offset + func->len; \ |
| 85 | insn = list_next_entry(insn, list)) |
| 86 | |
| 87 | #define func_for_each_insn_continue_reverse(file, func, insn) \ |
| 88 | for (insn = list_prev_entry(insn, list); \ |
| 89 | &insn->list != &file->insn_list && \ |
| 90 | insn->sec == func->sec && insn->offset >= func->offset; \ |
| 91 | insn = list_prev_entry(insn, list)) |
| 92 | |
| 93 | #define sec_for_each_insn_from(file, insn) \ |
| 94 | for (; insn; insn = next_insn_same_sec(file, insn)) |
| 95 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 96 | #define sec_for_each_insn_continue(file, insn) \ |
| 97 | for (insn = next_insn_same_sec(file, insn); insn; \ |
| 98 | insn = next_insn_same_sec(file, insn)) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 99 | |
Josh Poimboeuf | a229614 | 2020-02-10 12:32:39 -0600 | [diff] [blame] | 100 | static bool is_static_jump(struct instruction *insn) |
| 101 | { |
| 102 | return insn->type == INSN_JUMP_CONDITIONAL || |
| 103 | insn->type == INSN_JUMP_UNCONDITIONAL; |
| 104 | } |
| 105 | |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 106 | static bool is_sibling_call(struct instruction *insn) |
| 107 | { |
| 108 | /* An indirect jump is either a sibling call or a jump to a table. */ |
| 109 | if (insn->type == INSN_JUMP_DYNAMIC) |
| 110 | return list_empty(&insn->alts); |
| 111 | |
Josh Poimboeuf | a229614 | 2020-02-10 12:32:39 -0600 | [diff] [blame] | 112 | if (!is_static_jump(insn)) |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 113 | return false; |
| 114 | |
| 115 | /* add_jump_destinations() sets insn->call_dest for sibling calls. */ |
| 116 | return !!insn->call_dest; |
| 117 | } |
| 118 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 119 | /* |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 120 | * This checks to see if the given function is a "noreturn" function. |
| 121 | * |
| 122 | * For global functions which are outside the scope of this object file, we |
| 123 | * have to keep a manual list of them. |
| 124 | * |
| 125 | * For local functions, we have to detect them manually by simply looking for |
| 126 | * the lack of a return instruction. |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 127 | */ |
Josh Poimboeuf | 8e25c9f | 2019-07-17 20:36:50 -0500 | [diff] [blame] | 128 | static bool __dead_end_function(struct objtool_file *file, struct symbol *func, |
| 129 | int recursion) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 130 | { |
| 131 | int i; |
| 132 | struct instruction *insn; |
| 133 | bool empty = true; |
| 134 | |
| 135 | /* |
| 136 | * Unfortunately these have to be hard coded because the noreturn |
| 137 | * attribute isn't provided in ELF data. |
| 138 | */ |
| 139 | static const char * const global_noreturns[] = { |
| 140 | "__stack_chk_fail", |
| 141 | "panic", |
| 142 | "do_exit", |
| 143 | "do_task_dead", |
| 144 | "__module_put_and_exit", |
| 145 | "complete_and_exit", |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 146 | "__reiserfs_panic", |
| 147 | "lbug_with_loc", |
| 148 | "fortify_panic", |
Kees Cook | b394d46 | 2018-01-10 14:22:38 -0800 | [diff] [blame] | 149 | "usercopy_abort", |
Josh Poimboeuf | 684fb24 | 2018-06-19 10:47:50 -0500 | [diff] [blame] | 150 | "machine_real_restart", |
Josh Poimboeuf | 4fa5ecd | 2019-04-04 12:17:35 -0500 | [diff] [blame] | 151 | "rewind_stack_do_exit", |
Brendan Higgins | 33adf80 | 2019-09-23 02:02:38 -0700 | [diff] [blame] | 152 | "kunit_try_catch_throw", |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 153 | }; |
| 154 | |
Josh Poimboeuf | c9bab22 | 2019-07-17 20:36:51 -0500 | [diff] [blame] | 155 | if (!func) |
| 156 | return false; |
| 157 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 158 | if (func->bind == STB_WEAK) |
Josh Poimboeuf | 8e25c9f | 2019-07-17 20:36:50 -0500 | [diff] [blame] | 159 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 160 | |
| 161 | if (func->bind == STB_GLOBAL) |
| 162 | for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) |
| 163 | if (!strcmp(func->name, global_noreturns[i])) |
Josh Poimboeuf | 8e25c9f | 2019-07-17 20:36:50 -0500 | [diff] [blame] | 164 | return true; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 165 | |
Josh Poimboeuf | 1381043 | 2018-05-09 22:39:15 -0500 | [diff] [blame] | 166 | if (!func->len) |
Josh Poimboeuf | 8e25c9f | 2019-07-17 20:36:50 -0500 | [diff] [blame] | 167 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 168 | |
Josh Poimboeuf | 1381043 | 2018-05-09 22:39:15 -0500 | [diff] [blame] | 169 | insn = find_insn(file, func->sec, func->offset); |
| 170 | if (!insn->func) |
Josh Poimboeuf | 8e25c9f | 2019-07-17 20:36:50 -0500 | [diff] [blame] | 171 | return false; |
Josh Poimboeuf | 1381043 | 2018-05-09 22:39:15 -0500 | [diff] [blame] | 172 | |
| 173 | func_for_each_insn_all(file, func, insn) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 174 | empty = false; |
| 175 | |
| 176 | if (insn->type == INSN_RETURN) |
Josh Poimboeuf | 8e25c9f | 2019-07-17 20:36:50 -0500 | [diff] [blame] | 177 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | if (empty) |
Josh Poimboeuf | 8e25c9f | 2019-07-17 20:36:50 -0500 | [diff] [blame] | 181 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * A function can have a sibling call instead of a return. In that |
| 185 | * case, the function's dead-end status depends on whether the target |
| 186 | * of the sibling call returns. |
| 187 | */ |
Josh Poimboeuf | 1381043 | 2018-05-09 22:39:15 -0500 | [diff] [blame] | 188 | func_for_each_insn_all(file, func, insn) { |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 189 | if (is_sibling_call(insn)) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 190 | struct instruction *dest = insn->jump_dest; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 191 | |
| 192 | if (!dest) |
| 193 | /* sibling call to another file */ |
Josh Poimboeuf | 8e25c9f | 2019-07-17 20:36:50 -0500 | [diff] [blame] | 194 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 195 | |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 196 | /* local sibling call */ |
| 197 | if (recursion == 5) { |
| 198 | /* |
| 199 | * Infinite recursion: two functions have |
| 200 | * sibling calls to each other. This is a very |
| 201 | * rare case. It means they aren't dead ends. |
| 202 | */ |
| 203 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 204 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 205 | |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 206 | return __dead_end_function(file, dest->func, recursion+1); |
| 207 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Josh Poimboeuf | 8e25c9f | 2019-07-17 20:36:50 -0500 | [diff] [blame] | 210 | return true; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Josh Poimboeuf | 8e25c9f | 2019-07-17 20:36:50 -0500 | [diff] [blame] | 213 | static bool dead_end_function(struct objtool_file *file, struct symbol *func) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 214 | { |
| 215 | return __dead_end_function(file, func, 0); |
| 216 | } |
| 217 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 218 | static void clear_insn_state(struct insn_state *state) |
| 219 | { |
| 220 | int i; |
| 221 | |
| 222 | memset(state, 0, sizeof(*state)); |
| 223 | state->cfa.base = CFI_UNDEFINED; |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 224 | for (i = 0; i < CFI_NUM_REGS; i++) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 225 | state->regs[i].base = CFI_UNDEFINED; |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 226 | state->vals[i].base = CFI_UNDEFINED; |
| 227 | } |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 228 | state->drap_reg = CFI_UNDEFINED; |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 229 | state->drap_offset = -1; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 230 | } |
| 231 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 232 | /* |
| 233 | * Call the arch-specific instruction decoder for all the instructions and add |
| 234 | * them to the global instruction list. |
| 235 | */ |
| 236 | static int decode_instructions(struct objtool_file *file) |
| 237 | { |
| 238 | struct section *sec; |
| 239 | struct symbol *func; |
| 240 | unsigned long offset; |
| 241 | struct instruction *insn; |
| 242 | int ret; |
| 243 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 244 | for_each_sec(file, sec) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 245 | |
| 246 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) |
| 247 | continue; |
| 248 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 249 | if (strcmp(sec->name, ".altinstr_replacement") && |
| 250 | strcmp(sec->name, ".altinstr_aux") && |
| 251 | strncmp(sec->name, ".discard.", 9)) |
| 252 | sec->text = true; |
| 253 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 254 | for (offset = 0; offset < sec->len; offset += insn->len) { |
| 255 | insn = malloc(sizeof(*insn)); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 256 | if (!insn) { |
| 257 | WARN("malloc failed"); |
| 258 | return -1; |
| 259 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 260 | memset(insn, 0, sizeof(*insn)); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 261 | INIT_LIST_HEAD(&insn->alts); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 262 | clear_insn_state(&insn->state); |
| 263 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 264 | insn->sec = sec; |
| 265 | insn->offset = offset; |
| 266 | |
| 267 | ret = arch_decode_instruction(file->elf, sec, offset, |
| 268 | sec->len - offset, |
| 269 | &insn->len, &insn->type, |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 270 | &insn->immediate, |
| 271 | &insn->stack_op); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 272 | if (ret) |
Kamalesh Babulal | b703798 | 2017-10-19 11:27:24 -0500 | [diff] [blame] | 273 | goto err; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 274 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 275 | hash_add(file->insn_hash, &insn->hash, insn->offset); |
| 276 | list_add_tail(&insn->list, &file->insn_list); |
| 277 | } |
| 278 | |
| 279 | list_for_each_entry(func, &sec->symbol_list, list) { |
Josh Poimboeuf | e10cd8f | 2019-07-17 20:36:48 -0500 | [diff] [blame] | 280 | if (func->type != STT_FUNC || func->alias != func) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 281 | continue; |
| 282 | |
| 283 | if (!find_insn(file, sec, func->offset)) { |
| 284 | WARN("%s(): can't find starting instruction", |
| 285 | func->name); |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | func_for_each_insn(file, func, insn) |
Josh Poimboeuf | e10cd8f | 2019-07-17 20:36:48 -0500 | [diff] [blame] | 290 | insn->func = func; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | return 0; |
Kamalesh Babulal | b703798 | 2017-10-19 11:27:24 -0500 | [diff] [blame] | 295 | |
| 296 | err: |
| 297 | free(insn); |
| 298 | return ret; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
Josh Poimboeuf | 649ea4d | 2017-07-27 15:56:53 -0500 | [diff] [blame] | 302 | * Mark "ud2" instructions and manually annotated dead ends. |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 303 | */ |
| 304 | static int add_dead_ends(struct objtool_file *file) |
| 305 | { |
| 306 | struct section *sec; |
| 307 | struct rela *rela; |
| 308 | struct instruction *insn; |
| 309 | bool found; |
| 310 | |
Josh Poimboeuf | 649ea4d | 2017-07-27 15:56:53 -0500 | [diff] [blame] | 311 | /* |
| 312 | * By default, "ud2" is a dead end unless otherwise annotated, because |
| 313 | * GCC 7 inserts it for certain divide-by-zero cases. |
| 314 | */ |
| 315 | for_each_insn(file, insn) |
| 316 | if (insn->type == INSN_BUG) |
| 317 | insn->dead_end = true; |
| 318 | |
| 319 | /* |
| 320 | * Check for manually annotated dead ends. |
| 321 | */ |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 322 | sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); |
| 323 | if (!sec) |
Josh Poimboeuf | 649ea4d | 2017-07-27 15:56:53 -0500 | [diff] [blame] | 324 | goto reachable; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 325 | |
| 326 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 327 | if (rela->sym->type != STT_SECTION) { |
| 328 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 329 | return -1; |
| 330 | } |
| 331 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 332 | if (insn) |
| 333 | insn = list_prev_entry(insn, list); |
| 334 | else if (rela->addend == rela->sym->sec->len) { |
| 335 | found = false; |
| 336 | list_for_each_entry_reverse(insn, &file->insn_list, list) { |
| 337 | if (insn->sec == rela->sym->sec) { |
| 338 | found = true; |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if (!found) { |
| 344 | WARN("can't find unreachable insn at %s+0x%x", |
| 345 | rela->sym->sec->name, rela->addend); |
| 346 | return -1; |
| 347 | } |
| 348 | } else { |
| 349 | WARN("can't find unreachable insn at %s+0x%x", |
| 350 | rela->sym->sec->name, rela->addend); |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | insn->dead_end = true; |
| 355 | } |
| 356 | |
Josh Poimboeuf | 649ea4d | 2017-07-27 15:56:53 -0500 | [diff] [blame] | 357 | reachable: |
| 358 | /* |
| 359 | * These manually annotated reachable checks are needed for GCC 4.4, |
| 360 | * where the Linux unreachable() macro isn't supported. In that case |
| 361 | * GCC doesn't know the "ud2" is fatal, so it generates code as if it's |
| 362 | * not a dead end. |
| 363 | */ |
| 364 | sec = find_section_by_name(file->elf, ".rela.discard.reachable"); |
| 365 | if (!sec) |
| 366 | return 0; |
| 367 | |
| 368 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 369 | if (rela->sym->type != STT_SECTION) { |
| 370 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 371 | return -1; |
| 372 | } |
| 373 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 374 | if (insn) |
| 375 | insn = list_prev_entry(insn, list); |
| 376 | else if (rela->addend == rela->sym->sec->len) { |
| 377 | found = false; |
| 378 | list_for_each_entry_reverse(insn, &file->insn_list, list) { |
| 379 | if (insn->sec == rela->sym->sec) { |
| 380 | found = true; |
| 381 | break; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (!found) { |
| 386 | WARN("can't find reachable insn at %s+0x%x", |
| 387 | rela->sym->sec->name, rela->addend); |
| 388 | return -1; |
| 389 | } |
| 390 | } else { |
| 391 | WARN("can't find reachable insn at %s+0x%x", |
| 392 | rela->sym->sec->name, rela->addend); |
| 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | insn->dead_end = false; |
| 397 | } |
| 398 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Warnings shouldn't be reported for ignored functions. |
| 404 | */ |
| 405 | static void add_ignores(struct objtool_file *file) |
| 406 | { |
| 407 | struct instruction *insn; |
| 408 | struct section *sec; |
| 409 | struct symbol *func; |
Peter Zijlstra | aaf5c62 | 2019-02-27 14:04:13 +0100 | [diff] [blame] | 410 | struct rela *rela; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 411 | |
Peter Zijlstra | aaf5c62 | 2019-02-27 14:04:13 +0100 | [diff] [blame] | 412 | sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); |
| 413 | if (!sec) |
| 414 | return; |
| 415 | |
| 416 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 417 | switch (rela->sym->type) { |
| 418 | case STT_FUNC: |
| 419 | func = rela->sym; |
| 420 | break; |
| 421 | |
| 422 | case STT_SECTION: |
| 423 | func = find_symbol_by_offset(rela->sym->sec, rela->addend); |
| 424 | if (!func || func->type != STT_FUNC) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 425 | continue; |
Peter Zijlstra | aaf5c62 | 2019-02-27 14:04:13 +0100 | [diff] [blame] | 426 | break; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 427 | |
Peter Zijlstra | aaf5c62 | 2019-02-27 14:04:13 +0100 | [diff] [blame] | 428 | default: |
| 429 | WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type); |
| 430 | continue; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 431 | } |
Peter Zijlstra | aaf5c62 | 2019-02-27 14:04:13 +0100 | [diff] [blame] | 432 | |
| 433 | func_for_each_insn_all(file, func, insn) |
| 434 | insn->ignore = true; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | /* |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 439 | * This is a whitelist of functions that is allowed to be called with AC set. |
| 440 | * The list is meant to be minimal and only contains compiler instrumentation |
| 441 | * ABI and a few functions used to implement *_{to,from}_user() functions. |
| 442 | * |
| 443 | * These functions must not directly change AC, but may PUSHF/POPF. |
| 444 | */ |
| 445 | static const char *uaccess_safe_builtin[] = { |
| 446 | /* KASAN */ |
| 447 | "kasan_report", |
| 448 | "check_memory_region", |
| 449 | /* KASAN out-of-line */ |
| 450 | "__asan_loadN_noabort", |
| 451 | "__asan_load1_noabort", |
| 452 | "__asan_load2_noabort", |
| 453 | "__asan_load4_noabort", |
| 454 | "__asan_load8_noabort", |
| 455 | "__asan_load16_noabort", |
| 456 | "__asan_storeN_noabort", |
| 457 | "__asan_store1_noabort", |
| 458 | "__asan_store2_noabort", |
| 459 | "__asan_store4_noabort", |
| 460 | "__asan_store8_noabort", |
| 461 | "__asan_store16_noabort", |
| 462 | /* KASAN in-line */ |
| 463 | "__asan_report_load_n_noabort", |
| 464 | "__asan_report_load1_noabort", |
| 465 | "__asan_report_load2_noabort", |
| 466 | "__asan_report_load4_noabort", |
| 467 | "__asan_report_load8_noabort", |
| 468 | "__asan_report_load16_noabort", |
| 469 | "__asan_report_store_n_noabort", |
| 470 | "__asan_report_store1_noabort", |
| 471 | "__asan_report_store2_noabort", |
| 472 | "__asan_report_store4_noabort", |
| 473 | "__asan_report_store8_noabort", |
| 474 | "__asan_report_store16_noabort", |
| 475 | /* KCOV */ |
| 476 | "write_comp_data", |
| 477 | "__sanitizer_cov_trace_pc", |
| 478 | "__sanitizer_cov_trace_const_cmp1", |
| 479 | "__sanitizer_cov_trace_const_cmp2", |
| 480 | "__sanitizer_cov_trace_const_cmp4", |
| 481 | "__sanitizer_cov_trace_const_cmp8", |
| 482 | "__sanitizer_cov_trace_cmp1", |
| 483 | "__sanitizer_cov_trace_cmp2", |
| 484 | "__sanitizer_cov_trace_cmp4", |
| 485 | "__sanitizer_cov_trace_cmp8", |
| 486 | /* UBSAN */ |
| 487 | "ubsan_type_mismatch_common", |
| 488 | "__ubsan_handle_type_mismatch", |
| 489 | "__ubsan_handle_type_mismatch_v1", |
Peter Zijlstra | 9a50dca | 2019-10-21 15:11:49 +0200 | [diff] [blame] | 490 | "__ubsan_handle_shift_out_of_bounds", |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 491 | /* misc */ |
| 492 | "csum_partial_copy_generic", |
| 493 | "__memcpy_mcsafe", |
Josh Poimboeuf | a7e47f2 | 2019-07-17 20:36:46 -0500 | [diff] [blame] | 494 | "mcsafe_handle_tail", |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 495 | "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */ |
| 496 | NULL |
| 497 | }; |
| 498 | |
| 499 | static void add_uaccess_safe(struct objtool_file *file) |
| 500 | { |
| 501 | struct symbol *func; |
| 502 | const char **name; |
| 503 | |
| 504 | if (!uaccess) |
| 505 | return; |
| 506 | |
| 507 | for (name = uaccess_safe_builtin; *name; name++) { |
| 508 | func = find_symbol_by_name(file->elf, *name); |
| 509 | if (!func) |
| 510 | continue; |
| 511 | |
Josh Poimboeuf | e10cd8f | 2019-07-17 20:36:48 -0500 | [diff] [blame] | 512 | func->uaccess_safe = true; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | /* |
Josh Poimboeuf | 258c760 | 2018-01-11 21:46:24 +0000 | [diff] [blame] | 517 | * FIXME: For now, just ignore any alternatives which add retpolines. This is |
| 518 | * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline. |
| 519 | * But it at least allows objtool to understand the control flow *around* the |
| 520 | * retpoline. |
| 521 | */ |
Peter Zijlstra | ff05ab2 | 2019-03-18 14:33:07 +0100 | [diff] [blame] | 522 | static int add_ignore_alternatives(struct objtool_file *file) |
Josh Poimboeuf | 258c760 | 2018-01-11 21:46:24 +0000 | [diff] [blame] | 523 | { |
| 524 | struct section *sec; |
| 525 | struct rela *rela; |
| 526 | struct instruction *insn; |
| 527 | |
Peter Zijlstra | ff05ab2 | 2019-03-18 14:33:07 +0100 | [diff] [blame] | 528 | sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts"); |
Josh Poimboeuf | 258c760 | 2018-01-11 21:46:24 +0000 | [diff] [blame] | 529 | if (!sec) |
| 530 | return 0; |
| 531 | |
| 532 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 533 | if (rela->sym->type != STT_SECTION) { |
| 534 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 535 | return -1; |
| 536 | } |
| 537 | |
| 538 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 539 | if (!insn) { |
Peter Zijlstra | ff05ab2 | 2019-03-18 14:33:07 +0100 | [diff] [blame] | 540 | WARN("bad .discard.ignore_alts entry"); |
Josh Poimboeuf | 258c760 | 2018-01-11 21:46:24 +0000 | [diff] [blame] | 541 | return -1; |
| 542 | } |
| 543 | |
| 544 | insn->ignore_alts = true; |
| 545 | } |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | /* |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 551 | * Find the destination instructions for all jumps. |
| 552 | */ |
| 553 | static int add_jump_destinations(struct objtool_file *file) |
| 554 | { |
| 555 | struct instruction *insn; |
| 556 | struct rela *rela; |
| 557 | struct section *dest_sec; |
| 558 | unsigned long dest_off; |
| 559 | |
| 560 | for_each_insn(file, insn) { |
Josh Poimboeuf | a229614 | 2020-02-10 12:32:39 -0600 | [diff] [blame] | 561 | if (!is_static_jump(insn)) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 562 | continue; |
| 563 | |
Josh Poimboeuf | e6da956 | 2019-05-13 12:01:31 -0500 | [diff] [blame] | 564 | if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 565 | continue; |
| 566 | |
| 567 | rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 568 | insn->len); |
| 569 | if (!rela) { |
| 570 | dest_sec = insn->sec; |
| 571 | dest_off = insn->offset + insn->len + insn->immediate; |
| 572 | } else if (rela->sym->type == STT_SECTION) { |
| 573 | dest_sec = rela->sym->sec; |
| 574 | dest_off = rela->addend + 4; |
| 575 | } else if (rela->sym->sec->idx) { |
| 576 | dest_sec = rela->sym->sec; |
| 577 | dest_off = rela->sym->sym.st_value + rela->addend + 4; |
Josh Poimboeuf | 39b7353 | 2018-01-11 21:46:23 +0000 | [diff] [blame] | 578 | } else if (strstr(rela->sym->name, "_indirect_thunk_")) { |
| 579 | /* |
| 580 | * Retpoline jumps are really dynamic jumps in |
| 581 | * disguise, so convert them accordingly. |
| 582 | */ |
Josh Poimboeuf | b68b990 | 2019-07-17 20:36:57 -0500 | [diff] [blame] | 583 | if (insn->type == INSN_JUMP_UNCONDITIONAL) |
| 584 | insn->type = INSN_JUMP_DYNAMIC; |
| 585 | else |
| 586 | insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL; |
| 587 | |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 588 | insn->retpoline_safe = true; |
Josh Poimboeuf | 39b7353 | 2018-01-11 21:46:23 +0000 | [diff] [blame] | 589 | continue; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 590 | } else { |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 591 | /* external sibling call */ |
Peter Zijlstra | 54262aa | 2019-03-06 12:58:15 +0100 | [diff] [blame] | 592 | insn->call_dest = rela->sym; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 593 | continue; |
| 594 | } |
| 595 | |
| 596 | insn->jump_dest = find_insn(file, dest_sec, dest_off); |
| 597 | if (!insn->jump_dest) { |
| 598 | |
| 599 | /* |
| 600 | * This is a special case where an alt instruction |
| 601 | * jumps past the end of the section. These are |
| 602 | * handled later in handle_group_alt(). |
| 603 | */ |
| 604 | if (!strcmp(insn->sec->name, ".altinstr_replacement")) |
| 605 | continue; |
| 606 | |
| 607 | WARN_FUNC("can't find jump dest instruction at %s+0x%lx", |
| 608 | insn->sec, insn->offset, dest_sec->name, |
| 609 | dest_off); |
| 610 | return -1; |
| 611 | } |
Josh Poimboeuf | cd77849 | 2018-06-01 07:23:51 -0500 | [diff] [blame] | 612 | |
| 613 | /* |
Peter Zijlstra | 54262aa | 2019-03-06 12:58:15 +0100 | [diff] [blame] | 614 | * Cross-function jump. |
Josh Poimboeuf | cd77849 | 2018-06-01 07:23:51 -0500 | [diff] [blame] | 615 | */ |
| 616 | if (insn->func && insn->jump_dest->func && |
Peter Zijlstra | 54262aa | 2019-03-06 12:58:15 +0100 | [diff] [blame] | 617 | insn->func != insn->jump_dest->func) { |
| 618 | |
| 619 | /* |
| 620 | * For GCC 8+, create parent/child links for any cold |
| 621 | * subfunctions. This is _mostly_ redundant with a |
| 622 | * similar initialization in read_symbols(). |
| 623 | * |
| 624 | * If a function has aliases, we want the *first* such |
| 625 | * function in the symbol table to be the subfunction's |
| 626 | * parent. In that case we overwrite the |
| 627 | * initialization done in read_symbols(). |
| 628 | * |
| 629 | * However this code can't completely replace the |
| 630 | * read_symbols() code because this doesn't detect the |
| 631 | * case where the parent function's only reference to a |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 632 | * subfunction is through a jump table. |
Peter Zijlstra | 54262aa | 2019-03-06 12:58:15 +0100 | [diff] [blame] | 633 | */ |
| 634 | if (!strstr(insn->func->name, ".cold.") && |
| 635 | strstr(insn->jump_dest->func->name, ".cold.")) { |
| 636 | insn->func->cfunc = insn->jump_dest->func; |
| 637 | insn->jump_dest->func->pfunc = insn->func; |
| 638 | |
| 639 | } else if (insn->jump_dest->func->pfunc != insn->func->pfunc && |
| 640 | insn->jump_dest->offset == insn->jump_dest->func->offset) { |
| 641 | |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 642 | /* internal sibling call */ |
Peter Zijlstra | 54262aa | 2019-03-06 12:58:15 +0100 | [diff] [blame] | 643 | insn->call_dest = insn->jump_dest->func; |
Peter Zijlstra | 54262aa | 2019-03-06 12:58:15 +0100 | [diff] [blame] | 644 | } |
Josh Poimboeuf | cd77849 | 2018-06-01 07:23:51 -0500 | [diff] [blame] | 645 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Find the destination instructions for all calls. |
| 653 | */ |
| 654 | static int add_call_destinations(struct objtool_file *file) |
| 655 | { |
| 656 | struct instruction *insn; |
| 657 | unsigned long dest_off; |
| 658 | struct rela *rela; |
| 659 | |
| 660 | for_each_insn(file, insn) { |
| 661 | if (insn->type != INSN_CALL) |
| 662 | continue; |
| 663 | |
| 664 | rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 665 | insn->len); |
| 666 | if (!rela) { |
| 667 | dest_off = insn->offset + insn->len + insn->immediate; |
| 668 | insn->call_dest = find_symbol_by_offset(insn->sec, |
| 669 | dest_off); |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame] | 670 | |
| 671 | if (!insn->call_dest && !insn->ignore) { |
| 672 | WARN_FUNC("unsupported intra-function call", |
| 673 | insn->sec, insn->offset); |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 674 | if (retpoline) |
| 675 | WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE."); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 676 | return -1; |
| 677 | } |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame] | 678 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 679 | } else if (rela->sym->type == STT_SECTION) { |
| 680 | insn->call_dest = find_symbol_by_offset(rela->sym->sec, |
| 681 | rela->addend+4); |
| 682 | if (!insn->call_dest || |
| 683 | insn->call_dest->type != STT_FUNC) { |
| 684 | WARN_FUNC("can't find call dest symbol at %s+0x%x", |
| 685 | insn->sec, insn->offset, |
| 686 | rela->sym->sec->name, |
| 687 | rela->addend + 4); |
| 688 | return -1; |
| 689 | } |
| 690 | } else |
| 691 | insn->call_dest = rela->sym; |
| 692 | } |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * The .alternatives section requires some extra special care, over and above |
| 699 | * what other special sections require: |
| 700 | * |
| 701 | * 1. Because alternatives are patched in-place, we need to insert a fake jump |
| 702 | * instruction at the end so that validate_branch() skips all the original |
| 703 | * replaced instructions when validating the new instruction path. |
| 704 | * |
| 705 | * 2. An added wrinkle is that the new instruction length might be zero. In |
| 706 | * that case the old instructions are replaced with noops. We simulate that |
| 707 | * by creating a fake jump as the only new instruction. |
| 708 | * |
| 709 | * 3. In some cases, the alternative section includes an instruction which |
| 710 | * conditionally jumps to the _end_ of the entry. We have to modify these |
| 711 | * jumps' destinations to point back to .text rather than the end of the |
| 712 | * entry in .altinstr_replacement. |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 713 | */ |
| 714 | static int handle_group_alt(struct objtool_file *file, |
| 715 | struct special_alt *special_alt, |
| 716 | struct instruction *orig_insn, |
| 717 | struct instruction **new_insn) |
| 718 | { |
Josh Poimboeuf | 17bc339 | 2018-01-29 22:00:40 -0600 | [diff] [blame] | 719 | struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 720 | unsigned long dest_off; |
| 721 | |
| 722 | last_orig_insn = NULL; |
| 723 | insn = orig_insn; |
| 724 | sec_for_each_insn_from(file, insn) { |
| 725 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) |
| 726 | break; |
| 727 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 728 | insn->alt_group = true; |
| 729 | last_orig_insn = insn; |
| 730 | } |
| 731 | |
Josh Poimboeuf | 17bc339 | 2018-01-29 22:00:40 -0600 | [diff] [blame] | 732 | if (next_insn_same_sec(file, last_orig_insn)) { |
| 733 | fake_jump = malloc(sizeof(*fake_jump)); |
| 734 | if (!fake_jump) { |
| 735 | WARN("malloc failed"); |
| 736 | return -1; |
| 737 | } |
| 738 | memset(fake_jump, 0, sizeof(*fake_jump)); |
| 739 | INIT_LIST_HEAD(&fake_jump->alts); |
| 740 | clear_insn_state(&fake_jump->state); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 741 | |
Josh Poimboeuf | 17bc339 | 2018-01-29 22:00:40 -0600 | [diff] [blame] | 742 | fake_jump->sec = special_alt->new_sec; |
Josh Poimboeuf | e6da956 | 2019-05-13 12:01:31 -0500 | [diff] [blame] | 743 | fake_jump->offset = FAKE_JUMP_OFFSET; |
Josh Poimboeuf | 17bc339 | 2018-01-29 22:00:40 -0600 | [diff] [blame] | 744 | fake_jump->type = INSN_JUMP_UNCONDITIONAL; |
| 745 | fake_jump->jump_dest = list_next_entry(last_orig_insn, list); |
Josh Poimboeuf | e6da956 | 2019-05-13 12:01:31 -0500 | [diff] [blame] | 746 | fake_jump->func = orig_insn->func; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 747 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 748 | |
| 749 | if (!special_alt->new_len) { |
Josh Poimboeuf | 17bc339 | 2018-01-29 22:00:40 -0600 | [diff] [blame] | 750 | if (!fake_jump) { |
| 751 | WARN("%s: empty alternative at end of section", |
| 752 | special_alt->orig_sec->name); |
| 753 | return -1; |
| 754 | } |
| 755 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 756 | *new_insn = fake_jump; |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | last_new_insn = NULL; |
| 761 | insn = *new_insn; |
| 762 | sec_for_each_insn_from(file, insn) { |
| 763 | if (insn->offset >= special_alt->new_off + special_alt->new_len) |
| 764 | break; |
| 765 | |
| 766 | last_new_insn = insn; |
| 767 | |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame] | 768 | insn->ignore = orig_insn->ignore_alts; |
Peter Zijlstra | a4d09dd | 2019-02-25 10:31:24 +0100 | [diff] [blame] | 769 | insn->func = orig_insn->func; |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame] | 770 | |
Josh Poimboeuf | dc41972 | 2020-02-10 12:32:40 -0600 | [diff] [blame^] | 771 | /* |
| 772 | * Since alternative replacement code is copy/pasted by the |
| 773 | * kernel after applying relocations, generally such code can't |
| 774 | * have relative-address relocation references to outside the |
| 775 | * .altinstr_replacement section, unless the arch's |
| 776 | * alternatives code can adjust the relative offsets |
| 777 | * accordingly. |
| 778 | * |
| 779 | * The x86 alternatives code adjusts the offsets only when it |
| 780 | * encounters a branch instruction at the very beginning of the |
| 781 | * replacement group. |
| 782 | */ |
| 783 | if ((insn->offset != special_alt->new_off || |
| 784 | (insn->type != INSN_CALL && !is_static_jump(insn))) && |
| 785 | find_rela_by_dest_range(insn->sec, insn->offset, insn->len)) { |
| 786 | |
| 787 | WARN_FUNC("unsupported relocation in alternatives section", |
| 788 | insn->sec, insn->offset); |
| 789 | return -1; |
| 790 | } |
| 791 | |
Josh Poimboeuf | a229614 | 2020-02-10 12:32:39 -0600 | [diff] [blame] | 792 | if (!is_static_jump(insn)) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 793 | continue; |
| 794 | |
| 795 | if (!insn->immediate) |
| 796 | continue; |
| 797 | |
| 798 | dest_off = insn->offset + insn->len + insn->immediate; |
Josh Poimboeuf | 17bc339 | 2018-01-29 22:00:40 -0600 | [diff] [blame] | 799 | if (dest_off == special_alt->new_off + special_alt->new_len) { |
| 800 | if (!fake_jump) { |
| 801 | WARN("%s: alternative jump to end of section", |
| 802 | special_alt->orig_sec->name); |
| 803 | return -1; |
| 804 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 805 | insn->jump_dest = fake_jump; |
Josh Poimboeuf | 17bc339 | 2018-01-29 22:00:40 -0600 | [diff] [blame] | 806 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 807 | |
| 808 | if (!insn->jump_dest) { |
| 809 | WARN_FUNC("can't find alternative jump destination", |
| 810 | insn->sec, insn->offset); |
| 811 | return -1; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | if (!last_new_insn) { |
| 816 | WARN_FUNC("can't find last new alternative instruction", |
| 817 | special_alt->new_sec, special_alt->new_off); |
| 818 | return -1; |
| 819 | } |
| 820 | |
Josh Poimboeuf | 17bc339 | 2018-01-29 22:00:40 -0600 | [diff] [blame] | 821 | if (fake_jump) |
| 822 | list_add(&fake_jump->list, &last_new_insn->list); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * A jump table entry can either convert a nop to a jump or a jump to a nop. |
| 829 | * If the original instruction is a jump, make the alt entry an effective nop |
| 830 | * by just skipping the original instruction. |
| 831 | */ |
| 832 | static int handle_jump_alt(struct objtool_file *file, |
| 833 | struct special_alt *special_alt, |
| 834 | struct instruction *orig_insn, |
| 835 | struct instruction **new_insn) |
| 836 | { |
| 837 | if (orig_insn->type == INSN_NOP) |
| 838 | return 0; |
| 839 | |
| 840 | if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { |
| 841 | WARN_FUNC("unsupported instruction at jump label", |
| 842 | orig_insn->sec, orig_insn->offset); |
| 843 | return -1; |
| 844 | } |
| 845 | |
| 846 | *new_insn = list_next_entry(orig_insn, list); |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Read all the special sections which have alternate instructions which can be |
| 852 | * patched in or redirected to at runtime. Each instruction having alternate |
| 853 | * instruction(s) has them added to its insn->alts list, which will be |
| 854 | * traversed in validate_branch(). |
| 855 | */ |
| 856 | static int add_special_section_alts(struct objtool_file *file) |
| 857 | { |
| 858 | struct list_head special_alts; |
| 859 | struct instruction *orig_insn, *new_insn; |
| 860 | struct special_alt *special_alt, *tmp; |
| 861 | struct alternative *alt; |
| 862 | int ret; |
| 863 | |
| 864 | ret = special_get_alts(file->elf, &special_alts); |
| 865 | if (ret) |
| 866 | return ret; |
| 867 | |
| 868 | list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 869 | |
| 870 | orig_insn = find_insn(file, special_alt->orig_sec, |
| 871 | special_alt->orig_off); |
| 872 | if (!orig_insn) { |
| 873 | WARN_FUNC("special: can't find orig instruction", |
| 874 | special_alt->orig_sec, special_alt->orig_off); |
| 875 | ret = -1; |
| 876 | goto out; |
| 877 | } |
| 878 | |
| 879 | new_insn = NULL; |
| 880 | if (!special_alt->group || special_alt->new_len) { |
| 881 | new_insn = find_insn(file, special_alt->new_sec, |
| 882 | special_alt->new_off); |
| 883 | if (!new_insn) { |
| 884 | WARN_FUNC("special: can't find new instruction", |
| 885 | special_alt->new_sec, |
| 886 | special_alt->new_off); |
| 887 | ret = -1; |
| 888 | goto out; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | if (special_alt->group) { |
| 893 | ret = handle_group_alt(file, special_alt, orig_insn, |
| 894 | &new_insn); |
| 895 | if (ret) |
| 896 | goto out; |
| 897 | } else if (special_alt->jump_or_nop) { |
| 898 | ret = handle_jump_alt(file, special_alt, orig_insn, |
| 899 | &new_insn); |
| 900 | if (ret) |
| 901 | goto out; |
| 902 | } |
| 903 | |
Josh Poimboeuf | 258c760 | 2018-01-11 21:46:24 +0000 | [diff] [blame] | 904 | alt = malloc(sizeof(*alt)); |
| 905 | if (!alt) { |
| 906 | WARN("malloc failed"); |
| 907 | ret = -1; |
| 908 | goto out; |
| 909 | } |
| 910 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 911 | alt->insn = new_insn; |
Peter Zijlstra | 764eef4 | 2019-03-01 11:19:03 +0100 | [diff] [blame] | 912 | alt->skip_orig = special_alt->skip_orig; |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 913 | orig_insn->ignore_alts |= special_alt->skip_alt; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 914 | list_add_tail(&alt->list, &orig_insn->alts); |
| 915 | |
| 916 | list_del(&special_alt->list); |
| 917 | free(special_alt); |
| 918 | } |
| 919 | |
| 920 | out: |
| 921 | return ret; |
| 922 | } |
| 923 | |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 924 | static int add_jump_table(struct objtool_file *file, struct instruction *insn, |
Jann Horn | bd98c81 | 2019-07-17 20:36:54 -0500 | [diff] [blame] | 925 | struct rela *table) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 926 | { |
| 927 | struct rela *rela = table; |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 928 | struct instruction *dest_insn; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 929 | struct alternative *alt; |
Josh Poimboeuf | fd35c88 | 2018-05-10 17:48:49 -0500 | [diff] [blame] | 930 | struct symbol *pfunc = insn->func->pfunc; |
| 931 | unsigned int prev_offset = 0; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 932 | |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 933 | /* |
| 934 | * Each @rela is a switch table relocation which points to the target |
| 935 | * instruction. |
| 936 | */ |
| 937 | list_for_each_entry_from(rela, &table->sec->rela_list, list) { |
Jann Horn | bd98c81 | 2019-07-17 20:36:54 -0500 | [diff] [blame] | 938 | |
| 939 | /* Check for the end of the table: */ |
| 940 | if (rela != table && rela->jump_table_start) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 941 | break; |
| 942 | |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 943 | /* Make sure the table entries are consecutive: */ |
Josh Poimboeuf | fd35c88 | 2018-05-10 17:48:49 -0500 | [diff] [blame] | 944 | if (prev_offset && rela->offset != prev_offset + 8) |
| 945 | break; |
| 946 | |
| 947 | /* Detect function pointers from contiguous objects: */ |
| 948 | if (rela->sym->sec == pfunc->sec && |
| 949 | rela->addend == pfunc->offset) |
| 950 | break; |
| 951 | |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 952 | dest_insn = find_insn(file, rela->sym->sec, rela->addend); |
| 953 | if (!dest_insn) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 954 | break; |
| 955 | |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 956 | /* Make sure the destination is in the same function: */ |
Josh Poimboeuf | e65050b | 2019-07-17 20:36:55 -0500 | [diff] [blame] | 957 | if (!dest_insn->func || dest_insn->func->pfunc != pfunc) |
Josh Poimboeuf | 1381043 | 2018-05-09 22:39:15 -0500 | [diff] [blame] | 958 | break; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 959 | |
| 960 | alt = malloc(sizeof(*alt)); |
| 961 | if (!alt) { |
| 962 | WARN("malloc failed"); |
| 963 | return -1; |
| 964 | } |
| 965 | |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 966 | alt->insn = dest_insn; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 967 | list_add_tail(&alt->list, &insn->alts); |
Josh Poimboeuf | fd35c88 | 2018-05-10 17:48:49 -0500 | [diff] [blame] | 968 | prev_offset = rela->offset; |
| 969 | } |
| 970 | |
| 971 | if (!prev_offset) { |
| 972 | WARN_FUNC("can't find switch jump table", |
| 973 | insn->sec, insn->offset); |
| 974 | return -1; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | /* |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 981 | * find_jump_table() - Given a dynamic jump, find the switch jump table in |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 982 | * .rodata associated with it. |
| 983 | * |
| 984 | * There are 3 basic patterns: |
| 985 | * |
| 986 | * 1. jmpq *[rodata addr](,%reg,8) |
| 987 | * |
| 988 | * This is the most common case by far. It jumps to an address in a simple |
| 989 | * jump table which is stored in .rodata. |
| 990 | * |
| 991 | * 2. jmpq *[rodata addr](%rip) |
| 992 | * |
| 993 | * This is caused by a rare GCC quirk, currently only seen in three driver |
| 994 | * functions in the kernel, only with certain obscure non-distro configs. |
| 995 | * |
| 996 | * As part of an optimization, GCC makes a copy of an existing switch jump |
| 997 | * table, modifies it, and then hard-codes the jump (albeit with an indirect |
| 998 | * jump) to use a single entry in the table. The rest of the jump table and |
| 999 | * some of its jump targets remain as dead code. |
| 1000 | * |
| 1001 | * In such a case we can just crudely ignore all unreachable instruction |
| 1002 | * warnings for the entire object file. Ideally we would just ignore them |
| 1003 | * for the function, but that would require redesigning the code quite a |
| 1004 | * bit. And honestly that's just not worth doing: unreachable instruction |
| 1005 | * warnings are of questionable value anyway, and this is such a rare issue. |
| 1006 | * |
| 1007 | * 3. mov [rodata addr],%reg1 |
| 1008 | * ... some instructions ... |
| 1009 | * jmpq *(%reg1,%reg2,8) |
| 1010 | * |
| 1011 | * This is a fairly uncommon pattern which is new for GCC 6. As of this |
| 1012 | * writing, there are 11 occurrences of it in the allmodconfig kernel. |
| 1013 | * |
Peter Zijlstra | 99ce796 | 2018-02-08 14:02:32 +0100 | [diff] [blame] | 1014 | * As of GCC 7 there are quite a few more of these and the 'in between' code |
| 1015 | * is significant. Esp. with KASAN enabled some of the code between the mov |
| 1016 | * and jmpq uses .rodata itself, which can confuse things. |
| 1017 | * |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1018 | * TODO: Once we have DWARF CFI and smarter instruction decoding logic, |
| 1019 | * ensure the same register is used in the mov and jump instructions. |
Peter Zijlstra | 99ce796 | 2018-02-08 14:02:32 +0100 | [diff] [blame] | 1020 | * |
| 1021 | * NOTE: RETPOLINE made it harder still to decode dynamic jumps. |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1022 | */ |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1023 | static struct rela *find_jump_table(struct objtool_file *file, |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1024 | struct symbol *func, |
| 1025 | struct instruction *insn) |
| 1026 | { |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1027 | struct rela *text_rela, *table_rela; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1028 | struct instruction *orig_insn = insn; |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1029 | struct section *table_sec; |
Josh Poimboeuf | 6f5ec29 | 2018-05-14 08:53:24 -0500 | [diff] [blame] | 1030 | unsigned long table_offset; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1031 | |
Peter Zijlstra | 99ce796 | 2018-02-08 14:02:32 +0100 | [diff] [blame] | 1032 | /* |
| 1033 | * Backward search using the @first_jump_src links, these help avoid |
| 1034 | * much of the 'in between' code. Which avoids us getting confused by |
| 1035 | * it. |
| 1036 | */ |
Josh Poimboeuf | 7dec80c | 2018-05-18 15:10:34 -0500 | [diff] [blame] | 1037 | for (; |
Peter Zijlstra | 99ce796 | 2018-02-08 14:02:32 +0100 | [diff] [blame] | 1038 | &insn->list != &file->insn_list && |
| 1039 | insn->sec == func->sec && |
| 1040 | insn->offset >= func->offset; |
| 1041 | |
| 1042 | insn = insn->first_jump_src ?: list_prev_entry(insn, list)) { |
| 1043 | |
Josh Poimboeuf | 7dec80c | 2018-05-18 15:10:34 -0500 | [diff] [blame] | 1044 | if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1045 | break; |
| 1046 | |
| 1047 | /* allow small jumps within the range */ |
| 1048 | if (insn->type == INSN_JUMP_UNCONDITIONAL && |
| 1049 | insn->jump_dest && |
| 1050 | (insn->jump_dest->offset <= insn->offset || |
| 1051 | insn->jump_dest->offset > orig_insn->offset)) |
| 1052 | break; |
| 1053 | |
| 1054 | /* look for a relocation which references .rodata */ |
| 1055 | text_rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 1056 | insn->len); |
Allan Xavier | 4a60aa0 | 2018-09-07 08:12:01 -0500 | [diff] [blame] | 1057 | if (!text_rela || text_rela->sym->type != STT_SECTION || |
| 1058 | !text_rela->sym->sec->rodata) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1059 | continue; |
| 1060 | |
Josh Poimboeuf | 6f5ec29 | 2018-05-14 08:53:24 -0500 | [diff] [blame] | 1061 | table_offset = text_rela->addend; |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1062 | table_sec = text_rela->sym->sec; |
Allan Xavier | 4a60aa0 | 2018-09-07 08:12:01 -0500 | [diff] [blame] | 1063 | |
Josh Poimboeuf | 6f5ec29 | 2018-05-14 08:53:24 -0500 | [diff] [blame] | 1064 | if (text_rela->type == R_X86_64_PC32) |
| 1065 | table_offset += 4; |
| 1066 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1067 | /* |
| 1068 | * Make sure the .rodata address isn't associated with a |
Josh Poimboeuf | 87b512d | 2019-06-27 20:50:46 -0500 | [diff] [blame] | 1069 | * symbol. GCC jump tables are anonymous data. |
| 1070 | * |
| 1071 | * Also support C jump tables which are in the same format as |
| 1072 | * switch jump tables. For objtool to recognize them, they |
| 1073 | * need to be placed in the C_JUMP_TABLE_SECTION section. They |
| 1074 | * have symbols associated with them. |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1075 | */ |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1076 | if (find_symbol_containing(table_sec, table_offset) && |
| 1077 | strcmp(table_sec->name, C_JUMP_TABLE_SECTION)) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1078 | continue; |
| 1079 | |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1080 | /* Each table entry has a rela associated with it. */ |
| 1081 | table_rela = find_rela_by_dest(table_sec, table_offset); |
| 1082 | if (!table_rela) |
| 1083 | continue; |
Josh Poimboeuf | 7dec80c | 2018-05-18 15:10:34 -0500 | [diff] [blame] | 1084 | |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1085 | /* |
| 1086 | * Use of RIP-relative switch jumps is quite rare, and |
| 1087 | * indicates a rare GCC quirk/bug which can leave dead code |
| 1088 | * behind. |
| 1089 | */ |
| 1090 | if (text_rela->type == R_X86_64_PC32) |
| 1091 | file->ignore_unreachables = true; |
| 1092 | |
| 1093 | return table_rela; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | return NULL; |
| 1097 | } |
| 1098 | |
Jann Horn | bd98c81 | 2019-07-17 20:36:54 -0500 | [diff] [blame] | 1099 | /* |
| 1100 | * First pass: Mark the head of each jump table so that in the next pass, |
| 1101 | * we know when a given jump table ends and the next one starts. |
| 1102 | */ |
| 1103 | static void mark_func_jump_tables(struct objtool_file *file, |
| 1104 | struct symbol *func) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1105 | { |
Jann Horn | bd98c81 | 2019-07-17 20:36:54 -0500 | [diff] [blame] | 1106 | struct instruction *insn, *last = NULL; |
| 1107 | struct rela *rela; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1108 | |
Josh Poimboeuf | 1381043 | 2018-05-09 22:39:15 -0500 | [diff] [blame] | 1109 | func_for_each_insn_all(file, func, insn) { |
Peter Zijlstra | 99ce796 | 2018-02-08 14:02:32 +0100 | [diff] [blame] | 1110 | if (!last) |
| 1111 | last = insn; |
| 1112 | |
| 1113 | /* |
| 1114 | * Store back-pointers for unconditional forward jumps such |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1115 | * that find_jump_table() can back-track using those and |
Peter Zijlstra | 99ce796 | 2018-02-08 14:02:32 +0100 | [diff] [blame] | 1116 | * avoid some potentially confusing code. |
| 1117 | */ |
| 1118 | if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest && |
| 1119 | insn->offset > last->offset && |
| 1120 | insn->jump_dest->offset > insn->offset && |
| 1121 | !insn->jump_dest->first_jump_src) { |
| 1122 | |
| 1123 | insn->jump_dest->first_jump_src = insn; |
| 1124 | last = insn->jump_dest; |
| 1125 | } |
| 1126 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1127 | if (insn->type != INSN_JUMP_DYNAMIC) |
| 1128 | continue; |
| 1129 | |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1130 | rela = find_jump_table(file, func, insn); |
Jann Horn | bd98c81 | 2019-07-17 20:36:54 -0500 | [diff] [blame] | 1131 | if (rela) { |
| 1132 | rela->jump_table_start = true; |
| 1133 | insn->jump_table = rela; |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | static int add_func_jump_tables(struct objtool_file *file, |
| 1139 | struct symbol *func) |
| 1140 | { |
| 1141 | struct instruction *insn; |
| 1142 | int ret; |
| 1143 | |
| 1144 | func_for_each_insn_all(file, func, insn) { |
| 1145 | if (!insn->jump_table) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1146 | continue; |
| 1147 | |
Jann Horn | bd98c81 | 2019-07-17 20:36:54 -0500 | [diff] [blame] | 1148 | ret = add_jump_table(file, insn, insn->jump_table); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1149 | if (ret) |
| 1150 | return ret; |
| 1151 | } |
| 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * For some switch statements, gcc generates a jump table in the .rodata |
| 1158 | * section which contains a list of addresses within the function to jump to. |
| 1159 | * This finds these jump tables and adds them to the insn->alts lists. |
| 1160 | */ |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1161 | static int add_jump_table_alts(struct objtool_file *file) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1162 | { |
| 1163 | struct section *sec; |
| 1164 | struct symbol *func; |
| 1165 | int ret; |
| 1166 | |
Allan Xavier | 4a60aa0 | 2018-09-07 08:12:01 -0500 | [diff] [blame] | 1167 | if (!file->rodata) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1168 | return 0; |
| 1169 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1170 | for_each_sec(file, sec) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1171 | list_for_each_entry(func, &sec->symbol_list, list) { |
| 1172 | if (func->type != STT_FUNC) |
| 1173 | continue; |
| 1174 | |
Jann Horn | bd98c81 | 2019-07-17 20:36:54 -0500 | [diff] [blame] | 1175 | mark_func_jump_tables(file, func); |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1176 | ret = add_func_jump_tables(file, func); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1177 | if (ret) |
| 1178 | return ret; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1185 | static int read_unwind_hints(struct objtool_file *file) |
| 1186 | { |
| 1187 | struct section *sec, *relasec; |
| 1188 | struct rela *rela; |
| 1189 | struct unwind_hint *hint; |
| 1190 | struct instruction *insn; |
| 1191 | struct cfi_reg *cfa; |
| 1192 | int i; |
| 1193 | |
| 1194 | sec = find_section_by_name(file->elf, ".discard.unwind_hints"); |
| 1195 | if (!sec) |
| 1196 | return 0; |
| 1197 | |
| 1198 | relasec = sec->rela; |
| 1199 | if (!relasec) { |
| 1200 | WARN("missing .rela.discard.unwind_hints section"); |
| 1201 | return -1; |
| 1202 | } |
| 1203 | |
| 1204 | if (sec->len % sizeof(struct unwind_hint)) { |
| 1205 | WARN("struct unwind_hint size mismatch"); |
| 1206 | return -1; |
| 1207 | } |
| 1208 | |
| 1209 | file->hints = true; |
| 1210 | |
| 1211 | for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) { |
| 1212 | hint = (struct unwind_hint *)sec->data->d_buf + i; |
| 1213 | |
| 1214 | rela = find_rela_by_dest(sec, i * sizeof(*hint)); |
| 1215 | if (!rela) { |
| 1216 | WARN("can't find rela for unwind_hints[%d]", i); |
| 1217 | return -1; |
| 1218 | } |
| 1219 | |
| 1220 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 1221 | if (!insn) { |
| 1222 | WARN("can't find insn for unwind_hints[%d]", i); |
| 1223 | return -1; |
| 1224 | } |
| 1225 | |
| 1226 | cfa = &insn->state.cfa; |
| 1227 | |
| 1228 | if (hint->type == UNWIND_HINT_TYPE_SAVE) { |
| 1229 | insn->save = true; |
| 1230 | continue; |
| 1231 | |
| 1232 | } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) { |
| 1233 | insn->restore = true; |
| 1234 | insn->hint = true; |
| 1235 | continue; |
| 1236 | } |
| 1237 | |
| 1238 | insn->hint = true; |
| 1239 | |
| 1240 | switch (hint->sp_reg) { |
| 1241 | case ORC_REG_UNDEFINED: |
| 1242 | cfa->base = CFI_UNDEFINED; |
| 1243 | break; |
| 1244 | case ORC_REG_SP: |
| 1245 | cfa->base = CFI_SP; |
| 1246 | break; |
| 1247 | case ORC_REG_BP: |
| 1248 | cfa->base = CFI_BP; |
| 1249 | break; |
| 1250 | case ORC_REG_SP_INDIRECT: |
| 1251 | cfa->base = CFI_SP_INDIRECT; |
| 1252 | break; |
| 1253 | case ORC_REG_R10: |
| 1254 | cfa->base = CFI_R10; |
| 1255 | break; |
| 1256 | case ORC_REG_R13: |
| 1257 | cfa->base = CFI_R13; |
| 1258 | break; |
| 1259 | case ORC_REG_DI: |
| 1260 | cfa->base = CFI_DI; |
| 1261 | break; |
| 1262 | case ORC_REG_DX: |
| 1263 | cfa->base = CFI_DX; |
| 1264 | break; |
| 1265 | default: |
| 1266 | WARN_FUNC("unsupported unwind_hint sp base reg %d", |
| 1267 | insn->sec, insn->offset, hint->sp_reg); |
| 1268 | return -1; |
| 1269 | } |
| 1270 | |
| 1271 | cfa->offset = hint->sp_offset; |
| 1272 | insn->state.type = hint->type; |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 1273 | insn->state.end = hint->end; |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | return 0; |
| 1277 | } |
| 1278 | |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 1279 | static int read_retpoline_hints(struct objtool_file *file) |
| 1280 | { |
Josh Poimboeuf | 63474dc | 2018-03-06 17:58:15 -0600 | [diff] [blame] | 1281 | struct section *sec; |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 1282 | struct instruction *insn; |
| 1283 | struct rela *rela; |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 1284 | |
Josh Poimboeuf | 63474dc | 2018-03-06 17:58:15 -0600 | [diff] [blame] | 1285 | sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 1286 | if (!sec) |
| 1287 | return 0; |
| 1288 | |
Josh Poimboeuf | 63474dc | 2018-03-06 17:58:15 -0600 | [diff] [blame] | 1289 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 1290 | if (rela->sym->type != STT_SECTION) { |
| 1291 | WARN("unexpected relocation symbol type in %s", sec->name); |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 1292 | return -1; |
| 1293 | } |
| 1294 | |
| 1295 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 1296 | if (!insn) { |
Josh Poimboeuf | 63474dc | 2018-03-06 17:58:15 -0600 | [diff] [blame] | 1297 | WARN("bad .discard.retpoline_safe entry"); |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 1298 | return -1; |
| 1299 | } |
| 1300 | |
| 1301 | if (insn->type != INSN_JUMP_DYNAMIC && |
| 1302 | insn->type != INSN_CALL_DYNAMIC) { |
Josh Poimboeuf | 63474dc | 2018-03-06 17:58:15 -0600 | [diff] [blame] | 1303 | WARN_FUNC("retpoline_safe hint not an indirect jump/call", |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 1304 | insn->sec, insn->offset); |
| 1305 | return -1; |
| 1306 | } |
| 1307 | |
| 1308 | insn->retpoline_safe = true; |
| 1309 | } |
| 1310 | |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
Allan Xavier | 4a60aa0 | 2018-09-07 08:12:01 -0500 | [diff] [blame] | 1314 | static void mark_rodata(struct objtool_file *file) |
| 1315 | { |
| 1316 | struct section *sec; |
| 1317 | bool found = false; |
| 1318 | |
| 1319 | /* |
Josh Poimboeuf | 87b512d | 2019-06-27 20:50:46 -0500 | [diff] [blame] | 1320 | * Search for the following rodata sections, each of which can |
| 1321 | * potentially contain jump tables: |
| 1322 | * |
| 1323 | * - .rodata: can contain GCC switch tables |
| 1324 | * - .rodata.<func>: same, if -fdata-sections is being used |
| 1325 | * - .rodata..c_jump_table: contains C annotated jump tables |
| 1326 | * |
| 1327 | * .rodata.str1.* sections are ignored; they don't contain jump tables. |
Allan Xavier | 4a60aa0 | 2018-09-07 08:12:01 -0500 | [diff] [blame] | 1328 | */ |
| 1329 | for_each_sec(file, sec) { |
Josh Poimboeuf | 87b512d | 2019-06-27 20:50:46 -0500 | [diff] [blame] | 1330 | if ((!strncmp(sec->name, ".rodata", 7) && !strstr(sec->name, ".str1.")) || |
| 1331 | !strcmp(sec->name, C_JUMP_TABLE_SECTION)) { |
Allan Xavier | 4a60aa0 | 2018-09-07 08:12:01 -0500 | [diff] [blame] | 1332 | sec->rodata = true; |
| 1333 | found = true; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | file->rodata = found; |
| 1338 | } |
| 1339 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1340 | static int decode_sections(struct objtool_file *file) |
| 1341 | { |
| 1342 | int ret; |
| 1343 | |
Allan Xavier | 4a60aa0 | 2018-09-07 08:12:01 -0500 | [diff] [blame] | 1344 | mark_rodata(file); |
| 1345 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1346 | ret = decode_instructions(file); |
| 1347 | if (ret) |
| 1348 | return ret; |
| 1349 | |
| 1350 | ret = add_dead_ends(file); |
| 1351 | if (ret) |
| 1352 | return ret; |
| 1353 | |
| 1354 | add_ignores(file); |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1355 | add_uaccess_safe(file); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1356 | |
Peter Zijlstra | ff05ab2 | 2019-03-18 14:33:07 +0100 | [diff] [blame] | 1357 | ret = add_ignore_alternatives(file); |
Josh Poimboeuf | 258c760 | 2018-01-11 21:46:24 +0000 | [diff] [blame] | 1358 | if (ret) |
| 1359 | return ret; |
| 1360 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1361 | ret = add_jump_destinations(file); |
| 1362 | if (ret) |
| 1363 | return ret; |
| 1364 | |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame] | 1365 | ret = add_special_section_alts(file); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1366 | if (ret) |
| 1367 | return ret; |
| 1368 | |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame] | 1369 | ret = add_call_destinations(file); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1370 | if (ret) |
| 1371 | return ret; |
| 1372 | |
Josh Poimboeuf | e7c2bc3 | 2019-07-17 20:36:53 -0500 | [diff] [blame] | 1373 | ret = add_jump_table_alts(file); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1374 | if (ret) |
| 1375 | return ret; |
| 1376 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1377 | ret = read_unwind_hints(file); |
| 1378 | if (ret) |
| 1379 | return ret; |
| 1380 | |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 1381 | ret = read_retpoline_hints(file); |
| 1382 | if (ret) |
| 1383 | return ret; |
| 1384 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1385 | return 0; |
| 1386 | } |
| 1387 | |
| 1388 | static bool is_fentry_call(struct instruction *insn) |
| 1389 | { |
| 1390 | if (insn->type == INSN_CALL && |
| 1391 | insn->call_dest->type == STT_NOTYPE && |
| 1392 | !strcmp(insn->call_dest->name, "__fentry__")) |
| 1393 | return true; |
| 1394 | |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1398 | static bool has_modified_stack_frame(struct insn_state *state) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1399 | { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1400 | int i; |
| 1401 | |
| 1402 | if (state->cfa.base != initial_func_cfi.cfa.base || |
| 1403 | state->cfa.offset != initial_func_cfi.cfa.offset || |
| 1404 | state->stack_size != initial_func_cfi.cfa.offset || |
| 1405 | state->drap) |
| 1406 | return true; |
| 1407 | |
| 1408 | for (i = 0; i < CFI_NUM_REGS; i++) |
| 1409 | if (state->regs[i].base != initial_func_cfi.regs[i].base || |
| 1410 | state->regs[i].offset != initial_func_cfi.regs[i].offset) |
| 1411 | return true; |
| 1412 | |
| 1413 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1414 | } |
| 1415 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1416 | static bool has_valid_stack_frame(struct insn_state *state) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1417 | { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1418 | if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA && |
| 1419 | state->regs[CFI_BP].offset == -16) |
| 1420 | return true; |
| 1421 | |
| 1422 | if (state->drap && state->regs[CFI_BP].base == CFI_BP) |
| 1423 | return true; |
| 1424 | |
| 1425 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1426 | } |
| 1427 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 1428 | static int update_insn_state_regs(struct instruction *insn, struct insn_state *state) |
| 1429 | { |
| 1430 | struct cfi_reg *cfa = &state->cfa; |
| 1431 | struct stack_op *op = &insn->stack_op; |
| 1432 | |
| 1433 | if (cfa->base != CFI_SP) |
| 1434 | return 0; |
| 1435 | |
| 1436 | /* push */ |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1437 | 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] | 1438 | cfa->offset += 8; |
| 1439 | |
| 1440 | /* pop */ |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1441 | 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] | 1442 | cfa->offset -= 8; |
| 1443 | |
| 1444 | /* add immediate to sp */ |
| 1445 | if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD && |
| 1446 | op->dest.reg == CFI_SP && op->src.reg == CFI_SP) |
| 1447 | cfa->offset -= op->src.offset; |
| 1448 | |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1452 | static void save_reg(struct insn_state *state, unsigned char reg, int base, |
| 1453 | int offset) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1454 | { |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1455 | if (arch_callee_saved_reg(reg) && |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1456 | state->regs[reg].base == CFI_UNDEFINED) { |
| 1457 | state->regs[reg].base = base; |
| 1458 | state->regs[reg].offset = offset; |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | static void restore_reg(struct insn_state *state, unsigned char reg) |
| 1463 | { |
| 1464 | state->regs[reg].base = CFI_UNDEFINED; |
| 1465 | state->regs[reg].offset = 0; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | * A note about DRAP stack alignment: |
| 1470 | * |
| 1471 | * GCC has the concept of a DRAP register, which is used to help keep track of |
| 1472 | * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP |
| 1473 | * register. The typical DRAP pattern is: |
| 1474 | * |
| 1475 | * 4c 8d 54 24 08 lea 0x8(%rsp),%r10 |
| 1476 | * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp |
| 1477 | * 41 ff 72 f8 pushq -0x8(%r10) |
| 1478 | * 55 push %rbp |
| 1479 | * 48 89 e5 mov %rsp,%rbp |
| 1480 | * (more pushes) |
| 1481 | * 41 52 push %r10 |
| 1482 | * ... |
| 1483 | * 41 5a pop %r10 |
| 1484 | * (more pops) |
| 1485 | * 5d pop %rbp |
| 1486 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 1487 | * c3 retq |
| 1488 | * |
| 1489 | * There are some variations in the epilogues, like: |
| 1490 | * |
| 1491 | * 5b pop %rbx |
| 1492 | * 41 5a pop %r10 |
| 1493 | * 41 5c pop %r12 |
| 1494 | * 41 5d pop %r13 |
| 1495 | * 41 5e pop %r14 |
| 1496 | * c9 leaveq |
| 1497 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 1498 | * c3 retq |
| 1499 | * |
| 1500 | * and: |
| 1501 | * |
| 1502 | * 4c 8b 55 e8 mov -0x18(%rbp),%r10 |
| 1503 | * 48 8b 5d e0 mov -0x20(%rbp),%rbx |
| 1504 | * 4c 8b 65 f0 mov -0x10(%rbp),%r12 |
| 1505 | * 4c 8b 6d f8 mov -0x8(%rbp),%r13 |
| 1506 | * c9 leaveq |
| 1507 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 1508 | * c3 retq |
| 1509 | * |
| 1510 | * Sometimes r13 is used as the DRAP register, in which case it's saved and |
| 1511 | * restored beforehand: |
| 1512 | * |
| 1513 | * 41 55 push %r13 |
| 1514 | * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13 |
| 1515 | * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp |
| 1516 | * ... |
| 1517 | * 49 8d 65 f0 lea -0x10(%r13),%rsp |
| 1518 | * 41 5d pop %r13 |
| 1519 | * c3 retq |
| 1520 | */ |
| 1521 | static int update_insn_state(struct instruction *insn, struct insn_state *state) |
| 1522 | { |
| 1523 | struct stack_op *op = &insn->stack_op; |
| 1524 | struct cfi_reg *cfa = &state->cfa; |
| 1525 | struct cfi_reg *regs = state->regs; |
| 1526 | |
| 1527 | /* stack operations don't make sense with an undefined CFA */ |
| 1528 | if (cfa->base == CFI_UNDEFINED) { |
| 1529 | if (insn->func) { |
| 1530 | WARN_FUNC("undefined stack state", insn->sec, insn->offset); |
| 1531 | return -1; |
| 1532 | } |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 1536 | if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET) |
| 1537 | return update_insn_state_regs(insn, state); |
| 1538 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1539 | switch (op->dest.type) { |
| 1540 | |
| 1541 | case OP_DEST_REG: |
| 1542 | switch (op->src.type) { |
| 1543 | |
| 1544 | case OP_SRC_REG: |
Josh Poimboeuf | 0d0970e | 2017-09-20 16:24:32 -0500 | [diff] [blame] | 1545 | if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP && |
| 1546 | cfa->base == CFI_SP && |
| 1547 | regs[CFI_BP].base == CFI_CFA && |
| 1548 | regs[CFI_BP].offset == -cfa->offset) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1549 | |
Josh Poimboeuf | 0d0970e | 2017-09-20 16:24:32 -0500 | [diff] [blame] | 1550 | /* mov %rsp, %rbp */ |
| 1551 | cfa->base = op->dest.reg; |
| 1552 | state->bp_scratch = false; |
| 1553 | } |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1554 | |
Josh Poimboeuf | 0d0970e | 2017-09-20 16:24:32 -0500 | [diff] [blame] | 1555 | else if (op->src.reg == CFI_SP && |
| 1556 | op->dest.reg == CFI_BP && state->drap) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1557 | |
Josh Poimboeuf | 0d0970e | 2017-09-20 16:24:32 -0500 | [diff] [blame] | 1558 | /* drap: mov %rsp, %rbp */ |
| 1559 | regs[CFI_BP].base = CFI_BP; |
| 1560 | regs[CFI_BP].offset = -state->stack_size; |
| 1561 | state->bp_scratch = false; |
| 1562 | } |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 1563 | |
Josh Poimboeuf | 0d0970e | 2017-09-20 16:24:32 -0500 | [diff] [blame] | 1564 | else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
| 1565 | |
| 1566 | /* |
| 1567 | * mov %rsp, %reg |
| 1568 | * |
| 1569 | * This is needed for the rare case where GCC |
| 1570 | * does: |
| 1571 | * |
| 1572 | * mov %rsp, %rax |
| 1573 | * ... |
| 1574 | * mov %rax, %rsp |
| 1575 | */ |
| 1576 | state->vals[op->dest.reg].base = CFI_CFA; |
| 1577 | state->vals[op->dest.reg].offset = -state->stack_size; |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 1578 | } |
| 1579 | |
Josh Poimboeuf | 3c1f058 | 2018-03-22 13:00:37 -0500 | [diff] [blame] | 1580 | else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP && |
| 1581 | cfa->base == CFI_BP) { |
| 1582 | |
| 1583 | /* |
| 1584 | * mov %rbp, %rsp |
| 1585 | * |
| 1586 | * Restore the original stack pointer (Clang). |
| 1587 | */ |
| 1588 | state->stack_size = -state->regs[CFI_BP].offset; |
| 1589 | } |
| 1590 | |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 1591 | else if (op->dest.reg == cfa->base) { |
| 1592 | |
| 1593 | /* mov %reg, %rsp */ |
| 1594 | if (cfa->base == CFI_SP && |
| 1595 | state->vals[op->src.reg].base == CFI_CFA) { |
| 1596 | |
| 1597 | /* |
| 1598 | * This is needed for the rare case |
| 1599 | * where GCC does something dumb like: |
| 1600 | * |
| 1601 | * lea 0x8(%rsp), %rcx |
| 1602 | * ... |
| 1603 | * mov %rcx, %rsp |
| 1604 | */ |
| 1605 | cfa->offset = -state->vals[op->src.reg].offset; |
| 1606 | state->stack_size = cfa->offset; |
| 1607 | |
| 1608 | } else { |
| 1609 | cfa->base = CFI_UNDEFINED; |
| 1610 | cfa->offset = 0; |
| 1611 | } |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | break; |
| 1615 | |
| 1616 | case OP_SRC_ADD: |
| 1617 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) { |
| 1618 | |
| 1619 | /* add imm, %rsp */ |
| 1620 | state->stack_size -= op->src.offset; |
| 1621 | if (cfa->base == CFI_SP) |
| 1622 | cfa->offset -= op->src.offset; |
| 1623 | break; |
| 1624 | } |
| 1625 | |
| 1626 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) { |
| 1627 | |
| 1628 | /* lea disp(%rbp), %rsp */ |
| 1629 | state->stack_size = -(op->src.offset + regs[CFI_BP].offset); |
| 1630 | break; |
| 1631 | } |
| 1632 | |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 1633 | if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1634 | |
| 1635 | /* drap: lea disp(%rsp), %drap */ |
| 1636 | state->drap_reg = op->dest.reg; |
Josh Poimboeuf | dd88a0a | 2017-08-29 12:51:03 -0500 | [diff] [blame] | 1637 | |
| 1638 | /* |
| 1639 | * lea disp(%rsp), %reg |
| 1640 | * |
| 1641 | * This is needed for the rare case where GCC |
| 1642 | * does something dumb like: |
| 1643 | * |
| 1644 | * lea 0x8(%rsp), %rcx |
| 1645 | * ... |
| 1646 | * mov %rcx, %rsp |
| 1647 | */ |
| 1648 | state->vals[op->dest.reg].base = CFI_CFA; |
| 1649 | state->vals[op->dest.reg].offset = \ |
| 1650 | -state->stack_size + op->src.offset; |
| 1651 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1652 | break; |
| 1653 | } |
| 1654 | |
| 1655 | if (state->drap && op->dest.reg == CFI_SP && |
| 1656 | op->src.reg == state->drap_reg) { |
| 1657 | |
| 1658 | /* drap: lea disp(%drap), %rsp */ |
| 1659 | cfa->base = CFI_SP; |
| 1660 | cfa->offset = state->stack_size = -op->src.offset; |
| 1661 | state->drap_reg = CFI_UNDEFINED; |
| 1662 | state->drap = false; |
| 1663 | break; |
| 1664 | } |
| 1665 | |
| 1666 | if (op->dest.reg == state->cfa.base) { |
| 1667 | WARN_FUNC("unsupported stack register modification", |
| 1668 | insn->sec, insn->offset); |
| 1669 | return -1; |
| 1670 | } |
| 1671 | |
| 1672 | break; |
| 1673 | |
| 1674 | case OP_SRC_AND: |
| 1675 | if (op->dest.reg != CFI_SP || |
| 1676 | (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) || |
| 1677 | (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) { |
| 1678 | WARN_FUNC("unsupported stack pointer realignment", |
| 1679 | insn->sec, insn->offset); |
| 1680 | return -1; |
| 1681 | } |
| 1682 | |
| 1683 | if (state->drap_reg != CFI_UNDEFINED) { |
| 1684 | /* drap: and imm, %rsp */ |
| 1685 | cfa->base = state->drap_reg; |
| 1686 | cfa->offset = state->stack_size = 0; |
| 1687 | state->drap = true; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * Older versions of GCC (4.8ish) realign the stack |
| 1692 | * without DRAP, with a frame pointer. |
| 1693 | */ |
| 1694 | |
| 1695 | break; |
| 1696 | |
| 1697 | case OP_SRC_POP: |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1698 | case OP_SRC_POPF: |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1699 | if (!state->drap && op->dest.type == OP_DEST_REG && |
| 1700 | op->dest.reg == cfa->base) { |
| 1701 | |
| 1702 | /* pop %rbp */ |
| 1703 | cfa->base = CFI_SP; |
| 1704 | } |
| 1705 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1706 | if (state->drap && cfa->base == CFI_BP_INDIRECT && |
| 1707 | op->dest.type == OP_DEST_REG && |
| 1708 | op->dest.reg == state->drap_reg && |
| 1709 | state->drap_offset == -state->stack_size) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1710 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1711 | /* drap: pop %drap */ |
| 1712 | cfa->base = state->drap_reg; |
| 1713 | cfa->offset = 0; |
| 1714 | state->drap_offset = -1; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1715 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1716 | } else if (regs[op->dest.reg].offset == -state->stack_size) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1717 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1718 | /* pop %reg */ |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1719 | restore_reg(state, op->dest.reg); |
| 1720 | } |
| 1721 | |
| 1722 | state->stack_size -= 8; |
| 1723 | if (cfa->base == CFI_SP) |
| 1724 | cfa->offset -= 8; |
| 1725 | |
| 1726 | break; |
| 1727 | |
| 1728 | case OP_SRC_REG_INDIRECT: |
| 1729 | if (state->drap && op->src.reg == CFI_BP && |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1730 | op->src.offset == state->drap_offset) { |
| 1731 | |
| 1732 | /* drap: mov disp(%rbp), %drap */ |
| 1733 | cfa->base = state->drap_reg; |
| 1734 | cfa->offset = 0; |
| 1735 | state->drap_offset = -1; |
| 1736 | } |
| 1737 | |
| 1738 | if (state->drap && op->src.reg == CFI_BP && |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1739 | op->src.offset == regs[op->dest.reg].offset) { |
| 1740 | |
| 1741 | /* drap: mov disp(%rbp), %reg */ |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1742 | restore_reg(state, op->dest.reg); |
| 1743 | |
| 1744 | } else if (op->src.reg == cfa->base && |
| 1745 | op->src.offset == regs[op->dest.reg].offset + cfa->offset) { |
| 1746 | |
| 1747 | /* mov disp(%rbp), %reg */ |
| 1748 | /* mov disp(%rsp), %reg */ |
| 1749 | restore_reg(state, op->dest.reg); |
| 1750 | } |
| 1751 | |
| 1752 | break; |
| 1753 | |
| 1754 | default: |
| 1755 | WARN_FUNC("unknown stack-related instruction", |
| 1756 | insn->sec, insn->offset); |
| 1757 | return -1; |
| 1758 | } |
| 1759 | |
| 1760 | break; |
| 1761 | |
| 1762 | case OP_DEST_PUSH: |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1763 | case OP_DEST_PUSHF: |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1764 | state->stack_size += 8; |
| 1765 | if (cfa->base == CFI_SP) |
| 1766 | cfa->offset += 8; |
| 1767 | |
| 1768 | if (op->src.type != OP_SRC_REG) |
| 1769 | break; |
| 1770 | |
| 1771 | if (state->drap) { |
| 1772 | if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) { |
| 1773 | |
| 1774 | /* drap: push %drap */ |
| 1775 | cfa->base = CFI_BP_INDIRECT; |
| 1776 | cfa->offset = -state->stack_size; |
| 1777 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1778 | /* save drap so we know when to restore it */ |
| 1779 | state->drap_offset = -state->stack_size; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1780 | |
| 1781 | } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) { |
| 1782 | |
| 1783 | /* drap: push %rbp */ |
| 1784 | state->stack_size = 0; |
| 1785 | |
| 1786 | } else if (regs[op->src.reg].base == CFI_UNDEFINED) { |
| 1787 | |
| 1788 | /* drap: push %reg */ |
| 1789 | save_reg(state, op->src.reg, CFI_BP, -state->stack_size); |
| 1790 | } |
| 1791 | |
| 1792 | } else { |
| 1793 | |
| 1794 | /* push %reg */ |
| 1795 | save_reg(state, op->src.reg, CFI_CFA, -state->stack_size); |
| 1796 | } |
| 1797 | |
| 1798 | /* detect when asm code uses rbp as a scratch register */ |
Josh Poimboeuf | 867ac9d | 2017-07-24 18:34:14 -0500 | [diff] [blame] | 1799 | if (!no_fp && insn->func && op->src.reg == CFI_BP && |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1800 | cfa->base != CFI_BP) |
| 1801 | state->bp_scratch = true; |
| 1802 | break; |
| 1803 | |
| 1804 | case OP_DEST_REG_INDIRECT: |
| 1805 | |
| 1806 | if (state->drap) { |
| 1807 | if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) { |
| 1808 | |
| 1809 | /* drap: mov %drap, disp(%rbp) */ |
| 1810 | cfa->base = CFI_BP_INDIRECT; |
| 1811 | cfa->offset = op->dest.offset; |
| 1812 | |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1813 | /* save drap offset so we know when to restore it */ |
| 1814 | state->drap_offset = op->dest.offset; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | else if (regs[op->src.reg].base == CFI_UNDEFINED) { |
| 1818 | |
| 1819 | /* drap: mov reg, disp(%rbp) */ |
| 1820 | save_reg(state, op->src.reg, CFI_BP, op->dest.offset); |
| 1821 | } |
| 1822 | |
| 1823 | } else if (op->dest.reg == cfa->base) { |
| 1824 | |
| 1825 | /* mov reg, disp(%rbp) */ |
| 1826 | /* mov reg, disp(%rsp) */ |
| 1827 | save_reg(state, op->src.reg, CFI_CFA, |
| 1828 | op->dest.offset - state->cfa.offset); |
| 1829 | } |
| 1830 | |
| 1831 | break; |
| 1832 | |
| 1833 | case OP_DEST_LEAVE: |
| 1834 | if ((!state->drap && cfa->base != CFI_BP) || |
| 1835 | (state->drap && cfa->base != state->drap_reg)) { |
| 1836 | WARN_FUNC("leave instruction with modified stack frame", |
| 1837 | insn->sec, insn->offset); |
| 1838 | return -1; |
| 1839 | } |
| 1840 | |
| 1841 | /* leave (mov %rbp, %rsp; pop %rbp) */ |
| 1842 | |
| 1843 | state->stack_size = -state->regs[CFI_BP].offset - 8; |
| 1844 | restore_reg(state, CFI_BP); |
| 1845 | |
| 1846 | if (!state->drap) { |
| 1847 | cfa->base = CFI_SP; |
| 1848 | cfa->offset -= 8; |
| 1849 | } |
| 1850 | |
| 1851 | break; |
| 1852 | |
| 1853 | case OP_DEST_MEM: |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1854 | 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] | 1855 | WARN_FUNC("unknown stack-related memory operation", |
| 1856 | insn->sec, insn->offset); |
| 1857 | return -1; |
| 1858 | } |
| 1859 | |
| 1860 | /* pop mem */ |
| 1861 | state->stack_size -= 8; |
| 1862 | if (cfa->base == CFI_SP) |
| 1863 | cfa->offset -= 8; |
| 1864 | |
| 1865 | break; |
| 1866 | |
| 1867 | default: |
| 1868 | WARN_FUNC("unknown stack-related instruction", |
| 1869 | insn->sec, insn->offset); |
| 1870 | return -1; |
| 1871 | } |
| 1872 | |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
| 1876 | static bool insn_state_match(struct instruction *insn, struct insn_state *state) |
| 1877 | { |
| 1878 | struct insn_state *state1 = &insn->state, *state2 = state; |
| 1879 | int i; |
| 1880 | |
| 1881 | if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) { |
| 1882 | WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d", |
| 1883 | insn->sec, insn->offset, |
| 1884 | state1->cfa.base, state1->cfa.offset, |
| 1885 | state2->cfa.base, state2->cfa.offset); |
| 1886 | |
| 1887 | } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) { |
| 1888 | for (i = 0; i < CFI_NUM_REGS; i++) { |
| 1889 | if (!memcmp(&state1->regs[i], &state2->regs[i], |
| 1890 | sizeof(struct cfi_reg))) |
| 1891 | continue; |
| 1892 | |
| 1893 | WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d", |
| 1894 | insn->sec, insn->offset, |
| 1895 | i, state1->regs[i].base, state1->regs[i].offset, |
| 1896 | i, state2->regs[i].base, state2->regs[i].offset); |
| 1897 | break; |
| 1898 | } |
| 1899 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 1900 | } else if (state1->type != state2->type) { |
| 1901 | WARN_FUNC("stack state mismatch: type1=%d type2=%d", |
| 1902 | insn->sec, insn->offset, state1->type, state2->type); |
| 1903 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1904 | } else if (state1->drap != state2->drap || |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1905 | (state1->drap && state1->drap_reg != state2->drap_reg) || |
| 1906 | (state1->drap && state1->drap_offset != state2->drap_offset)) { |
| 1907 | 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] | 1908 | insn->sec, insn->offset, |
Josh Poimboeuf | bf4d1a8 | 2017-08-10 16:37:26 -0500 | [diff] [blame] | 1909 | state1->drap, state1->drap_reg, state1->drap_offset, |
| 1910 | state2->drap, state2->drap_reg, state2->drap_offset); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 1911 | |
| 1912 | } else |
| 1913 | return true; |
| 1914 | |
| 1915 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1916 | } |
| 1917 | |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1918 | static inline bool func_uaccess_safe(struct symbol *func) |
| 1919 | { |
| 1920 | if (func) |
Josh Poimboeuf | e10cd8f | 2019-07-17 20:36:48 -0500 | [diff] [blame] | 1921 | return func->uaccess_safe; |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1922 | |
| 1923 | return false; |
| 1924 | } |
| 1925 | |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 1926 | static inline const char *call_dest_name(struct instruction *insn) |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1927 | { |
| 1928 | if (insn->call_dest) |
| 1929 | return insn->call_dest->name; |
| 1930 | |
| 1931 | return "{dynamic}"; |
| 1932 | } |
| 1933 | |
| 1934 | static int validate_call(struct instruction *insn, struct insn_state *state) |
| 1935 | { |
| 1936 | if (state->uaccess && !func_uaccess_safe(insn->call_dest)) { |
| 1937 | WARN_FUNC("call to %s() with UACCESS enabled", |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 1938 | insn->sec, insn->offset, call_dest_name(insn)); |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1939 | return 1; |
| 1940 | } |
| 1941 | |
Peter Zijlstra | 2f0f9e9 | 2019-02-25 11:10:55 +0100 | [diff] [blame] | 1942 | if (state->df) { |
| 1943 | WARN_FUNC("call to %s() with DF set", |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 1944 | insn->sec, insn->offset, call_dest_name(insn)); |
Peter Zijlstra | 2f0f9e9 | 2019-02-25 11:10:55 +0100 | [diff] [blame] | 1945 | return 1; |
| 1946 | } |
| 1947 | |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1948 | return 0; |
| 1949 | } |
| 1950 | |
Peter Zijlstra | 54262aa | 2019-03-06 12:58:15 +0100 | [diff] [blame] | 1951 | static int validate_sibling_call(struct instruction *insn, struct insn_state *state) |
| 1952 | { |
| 1953 | if (has_modified_stack_frame(state)) { |
| 1954 | WARN_FUNC("sibling call from callable instruction with modified stack frame", |
| 1955 | insn->sec, insn->offset); |
| 1956 | return 1; |
| 1957 | } |
| 1958 | |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 1959 | return validate_call(insn, state); |
Peter Zijlstra | 54262aa | 2019-03-06 12:58:15 +0100 | [diff] [blame] | 1960 | } |
| 1961 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1962 | /* |
| 1963 | * Follow the branch starting at the given instruction, and recursively follow |
| 1964 | * any other branches (jumps). Meanwhile, track the frame pointer state at |
| 1965 | * each instruction and validate all the rules described in |
| 1966 | * tools/objtool/Documentation/stack-validation.txt. |
| 1967 | */ |
Josh Poimboeuf | c705cec | 2019-07-17 20:36:47 -0500 | [diff] [blame] | 1968 | static int validate_branch(struct objtool_file *file, struct symbol *func, |
| 1969 | struct instruction *first, struct insn_state state) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1970 | { |
| 1971 | struct alternative *alt; |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1972 | struct instruction *insn, *next_insn; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1973 | struct section *sec; |
Peter Zijlstra | 882a0db | 2019-07-24 17:47:26 -0500 | [diff] [blame] | 1974 | u8 visited; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1975 | int ret; |
| 1976 | |
| 1977 | insn = first; |
| 1978 | sec = insn->sec; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1979 | |
| 1980 | if (insn->alt_group && list_empty(&insn->alts)) { |
| 1981 | WARN_FUNC("don't know how to handle branch to middle of alternative instruction group", |
| 1982 | sec, insn->offset); |
Josh Poimboeuf | 12b2572 | 2017-08-10 16:37:25 -0500 | [diff] [blame] | 1983 | return 1; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | while (1) { |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 1987 | next_insn = next_insn_same_sec(file, insn); |
| 1988 | |
Josh Poimboeuf | 1381043 | 2018-05-09 22:39:15 -0500 | [diff] [blame] | 1989 | if (file->c_file && func && insn->func && func != insn->func->pfunc) { |
Josh Poimboeuf | ee97638 | 2017-08-11 12:24:15 -0500 | [diff] [blame] | 1990 | WARN("%s() falls through to next function %s()", |
| 1991 | func->name, insn->func->name); |
| 1992 | return 1; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 1993 | } |
| 1994 | |
Josh Poimboeuf | 4855022 | 2017-07-07 09:19:42 -0500 | [diff] [blame] | 1995 | if (func && insn->ignore) { |
| 1996 | WARN_FUNC("BUG: why am I validating an ignored function?", |
| 1997 | sec, insn->offset); |
Josh Poimboeuf | 12b2572 | 2017-08-10 16:37:25 -0500 | [diff] [blame] | 1998 | return 1; |
Josh Poimboeuf | 4855022 | 2017-07-07 09:19:42 -0500 | [diff] [blame] | 1999 | } |
| 2000 | |
Peter Zijlstra | 882a0db | 2019-07-24 17:47:26 -0500 | [diff] [blame] | 2001 | visited = 1 << state.uaccess; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2002 | if (insn->visited) { |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 2003 | if (!insn->hint && !insn_state_match(insn, &state)) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2004 | return 1; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2005 | |
Peter Zijlstra | 882a0db | 2019-07-24 17:47:26 -0500 | [diff] [blame] | 2006 | if (insn->visited & visited) |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 2007 | return 0; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2008 | } |
| 2009 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 2010 | if (insn->hint) { |
| 2011 | if (insn->restore) { |
| 2012 | struct instruction *save_insn, *i; |
| 2013 | |
| 2014 | i = insn; |
| 2015 | save_insn = NULL; |
Josh Poimboeuf | c705cec | 2019-07-17 20:36:47 -0500 | [diff] [blame] | 2016 | func_for_each_insn_continue_reverse(file, func, i) { |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 2017 | if (i->save) { |
| 2018 | save_insn = i; |
| 2019 | break; |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | if (!save_insn) { |
| 2024 | WARN_FUNC("no corresponding CFI save for CFI restore", |
| 2025 | sec, insn->offset); |
| 2026 | return 1; |
| 2027 | } |
| 2028 | |
| 2029 | if (!save_insn->visited) { |
| 2030 | /* |
| 2031 | * Oops, no state to copy yet. |
| 2032 | * Hopefully we can reach this |
| 2033 | * instruction from another branch |
| 2034 | * after the save insn has been |
| 2035 | * visited. |
| 2036 | */ |
| 2037 | if (insn == first) |
| 2038 | return 0; |
| 2039 | |
| 2040 | WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo", |
| 2041 | sec, insn->offset); |
| 2042 | return 1; |
| 2043 | } |
| 2044 | |
| 2045 | insn->state = save_insn->state; |
| 2046 | } |
| 2047 | |
| 2048 | state = insn->state; |
| 2049 | |
| 2050 | } else |
| 2051 | insn->state = state; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2052 | |
Peter Zijlstra | 882a0db | 2019-07-24 17:47:26 -0500 | [diff] [blame] | 2053 | insn->visited |= visited; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2054 | |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame] | 2055 | if (!insn->ignore_alts) { |
Peter Zijlstra | 764eef4 | 2019-03-01 11:19:03 +0100 | [diff] [blame] | 2056 | bool skip_orig = false; |
| 2057 | |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame] | 2058 | list_for_each_entry(alt, &insn->alts, list) { |
Peter Zijlstra | 764eef4 | 2019-03-01 11:19:03 +0100 | [diff] [blame] | 2059 | if (alt->skip_orig) |
| 2060 | skip_orig = true; |
| 2061 | |
Josh Poimboeuf | c705cec | 2019-07-17 20:36:47 -0500 | [diff] [blame] | 2062 | ret = validate_branch(file, func, alt->insn, state); |
Peter Zijlstra | 7697eee | 2019-03-01 11:15:49 +0100 | [diff] [blame] | 2063 | if (ret) { |
| 2064 | if (backtrace) |
| 2065 | BT_FUNC("(alt)", insn); |
| 2066 | return ret; |
| 2067 | } |
Josh Poimboeuf | a845c7c | 2018-01-29 22:00:39 -0600 | [diff] [blame] | 2068 | } |
Peter Zijlstra | 764eef4 | 2019-03-01 11:19:03 +0100 | [diff] [blame] | 2069 | |
| 2070 | if (skip_orig) |
| 2071 | return 0; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | switch (insn->type) { |
| 2075 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2076 | case INSN_RETURN: |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 2077 | if (state.uaccess && !func_uaccess_safe(func)) { |
| 2078 | WARN_FUNC("return with UACCESS enabled", sec, insn->offset); |
| 2079 | return 1; |
| 2080 | } |
| 2081 | |
| 2082 | if (!state.uaccess && func_uaccess_safe(func)) { |
| 2083 | WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", sec, insn->offset); |
| 2084 | return 1; |
| 2085 | } |
| 2086 | |
Peter Zijlstra | 2f0f9e9 | 2019-02-25 11:10:55 +0100 | [diff] [blame] | 2087 | if (state.df) { |
| 2088 | WARN_FUNC("return with DF set", sec, insn->offset); |
| 2089 | return 1; |
| 2090 | } |
| 2091 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2092 | if (func && has_modified_stack_frame(&state)) { |
| 2093 | WARN_FUNC("return with modified stack frame", |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2094 | sec, insn->offset); |
| 2095 | return 1; |
| 2096 | } |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2097 | |
| 2098 | if (state.bp_scratch) { |
| 2099 | WARN("%s uses BP as a scratch register", |
Josh Poimboeuf | c705cec | 2019-07-17 20:36:47 -0500 | [diff] [blame] | 2100 | func->name); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2101 | return 1; |
| 2102 | } |
| 2103 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2104 | return 0; |
| 2105 | |
| 2106 | case INSN_CALL: |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2107 | case INSN_CALL_DYNAMIC: |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 2108 | ret = validate_call(insn, &state); |
| 2109 | if (ret) |
| 2110 | return ret; |
| 2111 | |
Josh Poimboeuf | c9bab22 | 2019-07-17 20:36:51 -0500 | [diff] [blame] | 2112 | if (!no_fp && func && !is_fentry_call(insn) && |
| 2113 | !has_valid_stack_frame(&state)) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2114 | WARN_FUNC("call without frame pointer save/setup", |
| 2115 | sec, insn->offset); |
| 2116 | return 1; |
| 2117 | } |
Josh Poimboeuf | c9bab22 | 2019-07-17 20:36:51 -0500 | [diff] [blame] | 2118 | |
| 2119 | if (dead_end_function(file, insn->call_dest)) |
| 2120 | return 0; |
| 2121 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2122 | break; |
| 2123 | |
| 2124 | case INSN_JUMP_CONDITIONAL: |
| 2125 | case INSN_JUMP_UNCONDITIONAL: |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 2126 | if (func && is_sibling_call(insn)) { |
Peter Zijlstra | 54262aa | 2019-03-06 12:58:15 +0100 | [diff] [blame] | 2127 | ret = validate_sibling_call(insn, &state); |
| 2128 | if (ret) |
| 2129 | return ret; |
| 2130 | |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 2131 | } else if (insn->jump_dest) { |
Josh Poimboeuf | c705cec | 2019-07-17 20:36:47 -0500 | [diff] [blame] | 2132 | ret = validate_branch(file, func, |
| 2133 | insn->jump_dest, state); |
Peter Zijlstra | 7697eee | 2019-03-01 11:15:49 +0100 | [diff] [blame] | 2134 | if (ret) { |
| 2135 | if (backtrace) |
| 2136 | BT_FUNC("(branch)", insn); |
| 2137 | return ret; |
| 2138 | } |
Josh Poimboeuf | 4855022 | 2017-07-07 09:19:42 -0500 | [diff] [blame] | 2139 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2140 | |
| 2141 | if (insn->type == INSN_JUMP_UNCONDITIONAL) |
| 2142 | return 0; |
| 2143 | |
| 2144 | break; |
| 2145 | |
| 2146 | case INSN_JUMP_DYNAMIC: |
Josh Poimboeuf | b68b990 | 2019-07-17 20:36:57 -0500 | [diff] [blame] | 2147 | case INSN_JUMP_DYNAMIC_CONDITIONAL: |
Josh Poimboeuf | 0c1ddd3 | 2019-07-17 20:36:52 -0500 | [diff] [blame] | 2148 | if (func && is_sibling_call(insn)) { |
Peter Zijlstra | 54262aa | 2019-03-06 12:58:15 +0100 | [diff] [blame] | 2149 | ret = validate_sibling_call(insn, &state); |
| 2150 | if (ret) |
| 2151 | return ret; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2152 | } |
| 2153 | |
Josh Poimboeuf | b68b990 | 2019-07-17 20:36:57 -0500 | [diff] [blame] | 2154 | if (insn->type == INSN_JUMP_DYNAMIC) |
| 2155 | return 0; |
| 2156 | |
| 2157 | break; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2158 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 2159 | case INSN_CONTEXT_SWITCH: |
| 2160 | if (func && (!next_insn || !next_insn->hint)) { |
| 2161 | WARN_FUNC("unsupported instruction in callable function", |
| 2162 | sec, insn->offset); |
| 2163 | return 1; |
| 2164 | } |
| 2165 | return 0; |
| 2166 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2167 | case INSN_STACK: |
| 2168 | if (update_insn_state(insn, &state)) |
Josh Poimboeuf | 12b2572 | 2017-08-10 16:37:25 -0500 | [diff] [blame] | 2169 | return 1; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2170 | |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 2171 | if (insn->stack_op.dest.type == OP_DEST_PUSHF) { |
| 2172 | if (!state.uaccess_stack) { |
| 2173 | state.uaccess_stack = 1; |
| 2174 | } else if (state.uaccess_stack >> 31) { |
| 2175 | WARN_FUNC("PUSHF stack exhausted", sec, insn->offset); |
| 2176 | return 1; |
| 2177 | } |
| 2178 | state.uaccess_stack <<= 1; |
| 2179 | state.uaccess_stack |= state.uaccess; |
| 2180 | } |
| 2181 | |
| 2182 | if (insn->stack_op.src.type == OP_SRC_POPF) { |
| 2183 | if (state.uaccess_stack) { |
| 2184 | state.uaccess = state.uaccess_stack & 1; |
| 2185 | state.uaccess_stack >>= 1; |
| 2186 | if (state.uaccess_stack == 1) |
| 2187 | state.uaccess_stack = 0; |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | break; |
| 2192 | |
| 2193 | case INSN_STAC: |
| 2194 | if (state.uaccess) { |
| 2195 | WARN_FUNC("recursive UACCESS enable", sec, insn->offset); |
| 2196 | return 1; |
| 2197 | } |
| 2198 | |
| 2199 | state.uaccess = true; |
| 2200 | break; |
| 2201 | |
| 2202 | case INSN_CLAC: |
Josh Poimboeuf | c705cec | 2019-07-17 20:36:47 -0500 | [diff] [blame] | 2203 | if (!state.uaccess && func) { |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 2204 | WARN_FUNC("redundant UACCESS disable", sec, insn->offset); |
| 2205 | return 1; |
| 2206 | } |
| 2207 | |
| 2208 | if (func_uaccess_safe(func) && !state.uaccess_stack) { |
| 2209 | WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset); |
| 2210 | return 1; |
| 2211 | } |
| 2212 | |
| 2213 | state.uaccess = false; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2214 | break; |
| 2215 | |
Peter Zijlstra | 2f0f9e9 | 2019-02-25 11:10:55 +0100 | [diff] [blame] | 2216 | case INSN_STD: |
| 2217 | if (state.df) |
| 2218 | WARN_FUNC("recursive STD", sec, insn->offset); |
| 2219 | |
| 2220 | state.df = true; |
| 2221 | break; |
| 2222 | |
| 2223 | case INSN_CLD: |
Josh Poimboeuf | c705cec | 2019-07-17 20:36:47 -0500 | [diff] [blame] | 2224 | if (!state.df && func) |
Peter Zijlstra | 2f0f9e9 | 2019-02-25 11:10:55 +0100 | [diff] [blame] | 2225 | WARN_FUNC("redundant CLD", sec, insn->offset); |
| 2226 | |
| 2227 | state.df = false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2228 | break; |
| 2229 | |
| 2230 | default: |
| 2231 | break; |
| 2232 | } |
| 2233 | |
| 2234 | if (insn->dead_end) |
| 2235 | return 0; |
| 2236 | |
Josh Poimboeuf | 00d9618 | 2017-09-18 21:43:30 -0500 | [diff] [blame] | 2237 | if (!next_insn) { |
| 2238 | if (state.cfa.base == CFI_UNDEFINED) |
| 2239 | return 0; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2240 | WARN("%s: unexpected end of section", sec->name); |
| 2241 | return 1; |
| 2242 | } |
Josh Poimboeuf | 00d9618 | 2017-09-18 21:43:30 -0500 | [diff] [blame] | 2243 | |
| 2244 | insn = next_insn; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | return 0; |
| 2248 | } |
| 2249 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 2250 | static int validate_unwind_hints(struct objtool_file *file) |
| 2251 | { |
| 2252 | struct instruction *insn; |
| 2253 | int ret, warnings = 0; |
| 2254 | struct insn_state state; |
| 2255 | |
| 2256 | if (!file->hints) |
| 2257 | return 0; |
| 2258 | |
| 2259 | clear_insn_state(&state); |
| 2260 | |
| 2261 | for_each_insn(file, insn) { |
| 2262 | if (insn->hint && !insn->visited) { |
Josh Poimboeuf | c705cec | 2019-07-17 20:36:47 -0500 | [diff] [blame] | 2263 | ret = validate_branch(file, insn->func, insn, state); |
Peter Zijlstra | 7697eee | 2019-03-01 11:15:49 +0100 | [diff] [blame] | 2264 | if (ret && backtrace) |
| 2265 | BT_FUNC("<=== (hint)", insn); |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 2266 | warnings += ret; |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | return warnings; |
| 2271 | } |
| 2272 | |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 2273 | static int validate_retpoline(struct objtool_file *file) |
| 2274 | { |
| 2275 | struct instruction *insn; |
| 2276 | int warnings = 0; |
| 2277 | |
| 2278 | for_each_insn(file, insn) { |
| 2279 | if (insn->type != INSN_JUMP_DYNAMIC && |
| 2280 | insn->type != INSN_CALL_DYNAMIC) |
| 2281 | continue; |
| 2282 | |
| 2283 | if (insn->retpoline_safe) |
| 2284 | continue; |
| 2285 | |
Peter Zijlstra | ca41b97 | 2018-01-31 10:18:28 +0100 | [diff] [blame] | 2286 | /* |
| 2287 | * .init.text code is ran before userspace and thus doesn't |
| 2288 | * strictly need retpolines, except for modules which are |
| 2289 | * loaded late, they very much do need retpoline in their |
| 2290 | * .init.text |
| 2291 | */ |
| 2292 | if (!strcmp(insn->sec->name, ".init.text") && !module) |
| 2293 | continue; |
| 2294 | |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 2295 | WARN_FUNC("indirect %s found in RETPOLINE build", |
| 2296 | insn->sec, insn->offset, |
| 2297 | insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call"); |
| 2298 | |
| 2299 | warnings++; |
| 2300 | } |
| 2301 | |
| 2302 | return warnings; |
| 2303 | } |
| 2304 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2305 | static bool is_kasan_insn(struct instruction *insn) |
| 2306 | { |
| 2307 | return (insn->type == INSN_CALL && |
| 2308 | !strcmp(insn->call_dest->name, "__asan_handle_no_return")); |
| 2309 | } |
| 2310 | |
| 2311 | static bool is_ubsan_insn(struct instruction *insn) |
| 2312 | { |
| 2313 | return (insn->type == INSN_CALL && |
| 2314 | !strcmp(insn->call_dest->name, |
| 2315 | "__ubsan_handle_builtin_unreachable")); |
| 2316 | } |
| 2317 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2318 | static bool ignore_unreachable_insn(struct instruction *insn) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2319 | { |
| 2320 | int i; |
| 2321 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2322 | if (insn->ignore || insn->type == INSN_NOP) |
| 2323 | return true; |
| 2324 | |
| 2325 | /* |
| 2326 | * Ignore any unused exceptions. This can happen when a whitelisted |
| 2327 | * function has an exception table entry. |
Josh Poimboeuf | 0e2bb2b | 2017-07-27 15:56:54 -0500 | [diff] [blame] | 2328 | * |
| 2329 | * Also ignore alternative replacement instructions. This can happen |
| 2330 | * when a whitelisted function uses one of the ALTERNATIVE macros. |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2331 | */ |
Josh Poimboeuf | 0e2bb2b | 2017-07-27 15:56:54 -0500 | [diff] [blame] | 2332 | if (!strcmp(insn->sec->name, ".fixup") || |
| 2333 | !strcmp(insn->sec->name, ".altinstr_replacement") || |
| 2334 | !strcmp(insn->sec->name, ".altinstr_aux")) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2335 | return true; |
| 2336 | |
| 2337 | /* |
| 2338 | * Check if this (or a subsequent) instruction is related to |
| 2339 | * CONFIG_UBSAN or CONFIG_KASAN. |
| 2340 | * |
| 2341 | * End the search at 5 instructions to avoid going into the weeds. |
| 2342 | */ |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2343 | if (!insn->func) |
| 2344 | return false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2345 | for (i = 0; i < 5; i++) { |
| 2346 | |
| 2347 | if (is_kasan_insn(insn) || is_ubsan_insn(insn)) |
| 2348 | return true; |
| 2349 | |
Josh Poimboeuf | fe24e27 | 2018-02-08 17:09:25 -0600 | [diff] [blame] | 2350 | if (insn->type == INSN_JUMP_UNCONDITIONAL) { |
| 2351 | if (insn->jump_dest && |
| 2352 | insn->jump_dest->func == insn->func) { |
| 2353 | insn = insn->jump_dest; |
| 2354 | continue; |
| 2355 | } |
| 2356 | |
| 2357 | break; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2358 | } |
| 2359 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2360 | if (insn->offset + insn->len >= insn->func->offset + insn->func->len) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2361 | break; |
Josh Poimboeuf | fe24e27 | 2018-02-08 17:09:25 -0600 | [diff] [blame] | 2362 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2363 | insn = list_next_entry(insn, list); |
| 2364 | } |
| 2365 | |
| 2366 | return false; |
| 2367 | } |
| 2368 | |
| 2369 | static int validate_functions(struct objtool_file *file) |
| 2370 | { |
| 2371 | struct section *sec; |
| 2372 | struct symbol *func; |
| 2373 | struct instruction *insn; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2374 | struct insn_state state; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2375 | int ret, warnings = 0; |
| 2376 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2377 | clear_insn_state(&state); |
| 2378 | |
| 2379 | state.cfa = initial_func_cfi.cfa; |
| 2380 | memcpy(&state.regs, &initial_func_cfi.regs, |
| 2381 | CFI_NUM_REGS * sizeof(struct cfi_reg)); |
| 2382 | state.stack_size = initial_func_cfi.cfa.offset; |
| 2383 | |
| 2384 | for_each_sec(file, sec) { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2385 | list_for_each_entry(func, &sec->symbol_list, list) { |
Josh Poimboeuf | e10cd8f | 2019-07-17 20:36:48 -0500 | [diff] [blame] | 2386 | if (func->type != STT_FUNC) |
| 2387 | continue; |
| 2388 | |
Josh Poimboeuf | 61e9b75 | 2019-07-17 20:36:49 -0500 | [diff] [blame] | 2389 | if (!func->len) { |
| 2390 | WARN("%s() is missing an ELF size annotation", |
| 2391 | func->name); |
| 2392 | warnings++; |
| 2393 | } |
| 2394 | |
Josh Poimboeuf | e10cd8f | 2019-07-17 20:36:48 -0500 | [diff] [blame] | 2395 | if (func->pfunc != func || func->alias != func) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2396 | continue; |
| 2397 | |
| 2398 | insn = find_insn(file, sec, func->offset); |
Josh Poimboeuf | c705cec | 2019-07-17 20:36:47 -0500 | [diff] [blame] | 2399 | if (!insn || insn->ignore || insn->visited) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2400 | continue; |
| 2401 | |
Josh Poimboeuf | e10cd8f | 2019-07-17 20:36:48 -0500 | [diff] [blame] | 2402 | state.uaccess = func->uaccess_safe; |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 2403 | |
Josh Poimboeuf | c705cec | 2019-07-17 20:36:47 -0500 | [diff] [blame] | 2404 | ret = validate_branch(file, func, insn, state); |
Peter Zijlstra | 7697eee | 2019-03-01 11:15:49 +0100 | [diff] [blame] | 2405 | if (ret && backtrace) |
| 2406 | BT_FUNC("<=== (func)", insn); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2407 | warnings += ret; |
| 2408 | } |
| 2409 | } |
| 2410 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2411 | return warnings; |
| 2412 | } |
| 2413 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2414 | static int validate_reachable_instructions(struct objtool_file *file) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2415 | { |
| 2416 | struct instruction *insn; |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2417 | |
| 2418 | if (file->ignore_unreachables) |
| 2419 | return 0; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2420 | |
| 2421 | for_each_insn(file, insn) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2422 | if (insn->visited || ignore_unreachable_insn(insn)) |
| 2423 | continue; |
| 2424 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2425 | WARN_FUNC("unreachable instruction", insn->sec, insn->offset); |
| 2426 | return 1; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2427 | } |
| 2428 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2429 | return 0; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | static void cleanup(struct objtool_file *file) |
| 2433 | { |
| 2434 | struct instruction *insn, *tmpinsn; |
| 2435 | struct alternative *alt, *tmpalt; |
| 2436 | |
| 2437 | list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) { |
| 2438 | list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) { |
| 2439 | list_del(&alt->list); |
| 2440 | free(alt); |
| 2441 | } |
| 2442 | list_del(&insn->list); |
| 2443 | hash_del(&insn->hash); |
| 2444 | free(insn); |
| 2445 | } |
| 2446 | elf_close(file->elf); |
| 2447 | } |
| 2448 | |
Josh Poimboeuf | 0c67181 | 2019-03-18 19:09:38 -0500 | [diff] [blame] | 2449 | static struct objtool_file file; |
| 2450 | |
Peter Zijlstra | 43a4525 | 2018-01-16 17:16:32 +0100 | [diff] [blame] | 2451 | int check(const char *_objname, bool orc) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2452 | { |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2453 | int ret, warnings = 0; |
| 2454 | |
| 2455 | objname = _objname; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2456 | |
Michael Forney | 8e14479 | 2019-07-10 16:20:11 -0500 | [diff] [blame] | 2457 | file.elf = elf_read(objname, orc ? O_RDWR : O_RDONLY); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2458 | if (!file.elf) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2459 | return 1; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2460 | |
| 2461 | INIT_LIST_HEAD(&file.insn_list); |
| 2462 | hash_init(file.insn_hash); |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2463 | file.c_file = find_section_by_name(file.elf, ".comment"); |
Josh Poimboeuf | 867ac9d | 2017-07-24 18:34:14 -0500 | [diff] [blame] | 2464 | file.ignore_unreachables = no_unreachable; |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 2465 | file.hints = false; |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2466 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2467 | arch_initial_func_cfi_state(&initial_func_cfi); |
| 2468 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2469 | ret = decode_sections(&file); |
| 2470 | if (ret < 0) |
| 2471 | goto out; |
| 2472 | warnings += ret; |
| 2473 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2474 | if (list_empty(&file.insn_list)) |
| 2475 | goto out; |
| 2476 | |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 2477 | if (retpoline) { |
| 2478 | ret = validate_retpoline(&file); |
| 2479 | if (ret < 0) |
| 2480 | return ret; |
| 2481 | warnings += ret; |
| 2482 | } |
| 2483 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2484 | ret = validate_functions(&file); |
| 2485 | if (ret < 0) |
| 2486 | goto out; |
| 2487 | warnings += ret; |
| 2488 | |
Josh Poimboeuf | 39358a0 | 2017-07-11 10:33:43 -0500 | [diff] [blame] | 2489 | ret = validate_unwind_hints(&file); |
| 2490 | if (ret < 0) |
| 2491 | goto out; |
| 2492 | warnings += ret; |
| 2493 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 2494 | if (!warnings) { |
| 2495 | ret = validate_reachable_instructions(&file); |
| 2496 | if (ret < 0) |
| 2497 | goto out; |
| 2498 | warnings += ret; |
| 2499 | } |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2500 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 2501 | if (orc) { |
| 2502 | ret = create_orc(&file); |
| 2503 | if (ret < 0) |
| 2504 | goto out; |
| 2505 | |
| 2506 | ret = create_orc_sections(&file); |
| 2507 | if (ret < 0) |
| 2508 | goto out; |
| 2509 | |
| 2510 | ret = elf_write(file.elf); |
| 2511 | if (ret < 0) |
| 2512 | goto out; |
| 2513 | } |
| 2514 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2515 | out: |
| 2516 | cleanup(&file); |
| 2517 | |
Josh Poimboeuf | 644592d | 2020-02-10 12:32:38 -0600 | [diff] [blame] | 2518 | if (ret < 0) { |
| 2519 | /* |
| 2520 | * Fatal error. The binary is corrupt or otherwise broken in |
| 2521 | * some way, or objtool itself is broken. Fail the kernel |
| 2522 | * build. |
| 2523 | */ |
| 2524 | return ret; |
| 2525 | } |
| 2526 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 2527 | return 0; |
| 2528 | } |