David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <string.h> |
| 7 | #include <stdlib.h> |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 8 | #include <inttypes.h> |
| 9 | #include <sys/mman.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10 | |
| 11 | #include "builtin.h" |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 12 | #include "cfi.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 13 | #include "arch.h" |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 14 | #include "check.h" |
| 15 | #include "special.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 16 | #include "warn.h" |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 17 | #include "arch_elf.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 18 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 19 | #include <linux/objtool.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 20 | #include <linux/hashtable.h> |
| 21 | #include <linux/kernel.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 22 | #include <linux/static_call_types.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 23 | |
| 24 | struct alternative { |
| 25 | struct list_head list; |
| 26 | struct instruction *insn; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 27 | bool skip_orig; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 30 | static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache; |
| 31 | |
| 32 | static struct cfi_init_state initial_func_cfi; |
| 33 | static struct cfi_state init_cfi; |
| 34 | static struct cfi_state func_cfi; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 35 | |
| 36 | struct instruction *find_insn(struct objtool_file *file, |
| 37 | struct section *sec, unsigned long offset) |
| 38 | { |
| 39 | struct instruction *insn; |
| 40 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 41 | hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 42 | if (insn->sec == sec && insn->offset == offset) |
| 43 | return insn; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 44 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 45 | |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | static struct instruction *next_insn_same_sec(struct objtool_file *file, |
| 50 | struct instruction *insn) |
| 51 | { |
| 52 | struct instruction *next = list_next_entry(insn, list); |
| 53 | |
| 54 | if (!next || &next->list == &file->insn_list || next->sec != insn->sec) |
| 55 | return NULL; |
| 56 | |
| 57 | return next; |
| 58 | } |
| 59 | |
| 60 | static struct instruction *next_insn_same_func(struct objtool_file *file, |
| 61 | struct instruction *insn) |
| 62 | { |
| 63 | struct instruction *next = list_next_entry(insn, list); |
| 64 | struct symbol *func = insn->func; |
| 65 | |
| 66 | if (!func) |
| 67 | return NULL; |
| 68 | |
| 69 | if (&next->list != &file->insn_list && next->func == func) |
| 70 | return next; |
| 71 | |
| 72 | /* Check if we're already in the subfunction: */ |
| 73 | if (func == func->cfunc) |
| 74 | return NULL; |
| 75 | |
| 76 | /* Move to the subfunction: */ |
| 77 | return find_insn(file, func->cfunc->sec, func->cfunc->offset); |
| 78 | } |
| 79 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 80 | static struct instruction *prev_insn_same_sym(struct objtool_file *file, |
| 81 | struct instruction *insn) |
| 82 | { |
| 83 | struct instruction *prev = list_prev_entry(insn, list); |
| 84 | |
| 85 | if (&prev->list != &file->insn_list && prev->func == insn->func) |
| 86 | return prev; |
| 87 | |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | #define func_for_each_insn(file, func, insn) \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 92 | for (insn = find_insn(file, func->sec, func->offset); \ |
| 93 | insn; \ |
| 94 | insn = next_insn_same_func(file, insn)) |
| 95 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 96 | #define sym_for_each_insn(file, sym, insn) \ |
| 97 | for (insn = find_insn(file, sym->sec, sym->offset); \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 98 | insn && &insn->list != &file->insn_list && \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 99 | insn->sec == sym->sec && \ |
| 100 | insn->offset < sym->offset + sym->len; \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 101 | insn = list_next_entry(insn, list)) |
| 102 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 103 | #define sym_for_each_insn_continue_reverse(file, sym, insn) \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 104 | for (insn = list_prev_entry(insn, list); \ |
| 105 | &insn->list != &file->insn_list && \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 106 | insn->sec == sym->sec && insn->offset >= sym->offset; \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 107 | insn = list_prev_entry(insn, list)) |
| 108 | |
| 109 | #define sec_for_each_insn_from(file, insn) \ |
| 110 | for (; insn; insn = next_insn_same_sec(file, insn)) |
| 111 | |
| 112 | #define sec_for_each_insn_continue(file, insn) \ |
| 113 | for (insn = next_insn_same_sec(file, insn); insn; \ |
| 114 | insn = next_insn_same_sec(file, insn)) |
| 115 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 116 | static bool is_jump_table_jump(struct instruction *insn) |
| 117 | { |
| 118 | struct alt_group *alt_group = insn->alt_group; |
| 119 | |
| 120 | if (insn->jump_table) |
| 121 | return true; |
| 122 | |
| 123 | /* Retpoline alternative for a jump table? */ |
| 124 | return alt_group && alt_group->orig_group && |
| 125 | alt_group->orig_group->first_insn->jump_table; |
| 126 | } |
| 127 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 128 | static bool is_sibling_call(struct instruction *insn) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 129 | { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 130 | /* |
| 131 | * Assume only ELF functions can make sibling calls. This ensures |
| 132 | * sibling call detection consistency between vmlinux.o and individual |
| 133 | * objects. |
| 134 | */ |
| 135 | if (!insn->func) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 136 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 137 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 138 | /* An indirect jump is either a sibling call or a jump to a table. */ |
| 139 | if (insn->type == INSN_JUMP_DYNAMIC) |
| 140 | return !is_jump_table_jump(insn); |
| 141 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 142 | /* add_jump_destinations() sets insn->call_dest for sibling calls. */ |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 143 | return (is_static_jump(insn) && insn->call_dest); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | * This checks to see if the given function is a "noreturn" function. |
| 148 | * |
| 149 | * For global functions which are outside the scope of this object file, we |
| 150 | * have to keep a manual list of them. |
| 151 | * |
| 152 | * For local functions, we have to detect them manually by simply looking for |
| 153 | * the lack of a return instruction. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 154 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 155 | static bool __dead_end_function(struct objtool_file *file, struct symbol *func, |
| 156 | int recursion) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 157 | { |
| 158 | int i; |
| 159 | struct instruction *insn; |
| 160 | bool empty = true; |
| 161 | |
| 162 | /* |
| 163 | * Unfortunately these have to be hard coded because the noreturn |
| 164 | * attribute isn't provided in ELF data. |
| 165 | */ |
| 166 | static const char * const global_noreturns[] = { |
| 167 | "__stack_chk_fail", |
| 168 | "panic", |
| 169 | "do_exit", |
| 170 | "do_task_dead", |
| 171 | "__module_put_and_exit", |
| 172 | "complete_and_exit", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 173 | "__reiserfs_panic", |
| 174 | "lbug_with_loc", |
| 175 | "fortify_panic", |
| 176 | "usercopy_abort", |
| 177 | "machine_real_restart", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 178 | "rewind_stack_do_exit", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 179 | "kunit_try_catch_throw", |
| 180 | "xen_start_kernel", |
| 181 | "cpu_bringup_and_idle", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 184 | if (!func) |
| 185 | return false; |
| 186 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 187 | if (func->bind == STB_WEAK) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 188 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 189 | |
| 190 | if (func->bind == STB_GLOBAL) |
| 191 | for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) |
| 192 | if (!strcmp(func->name, global_noreturns[i])) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 193 | return true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 194 | |
| 195 | if (!func->len) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 196 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 197 | |
| 198 | insn = find_insn(file, func->sec, func->offset); |
| 199 | if (!insn->func) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 200 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 201 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 202 | func_for_each_insn(file, func, insn) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 203 | empty = false; |
| 204 | |
| 205 | if (insn->type == INSN_RETURN) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 206 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if (empty) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 210 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * A function can have a sibling call instead of a return. In that |
| 214 | * case, the function's dead-end status depends on whether the target |
| 215 | * of the sibling call returns. |
| 216 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 217 | func_for_each_insn(file, func, insn) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 218 | if (is_sibling_call(insn)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 219 | struct instruction *dest = insn->jump_dest; |
| 220 | |
| 221 | if (!dest) |
| 222 | /* sibling call to another file */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 223 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 224 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 225 | /* local sibling call */ |
| 226 | if (recursion == 5) { |
| 227 | /* |
| 228 | * Infinite recursion: two functions have |
| 229 | * sibling calls to each other. This is a very |
| 230 | * rare case. It means they aren't dead ends. |
| 231 | */ |
| 232 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 233 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 234 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 235 | return __dead_end_function(file, dest->func, recursion+1); |
| 236 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 237 | } |
| 238 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 239 | return true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 240 | } |
| 241 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 242 | static bool dead_end_function(struct objtool_file *file, struct symbol *func) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 243 | { |
| 244 | return __dead_end_function(file, func, 0); |
| 245 | } |
| 246 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 247 | static void init_cfi_state(struct cfi_state *cfi) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 248 | { |
| 249 | int i; |
| 250 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 251 | for (i = 0; i < CFI_NUM_REGS; i++) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 252 | cfi->regs[i].base = CFI_UNDEFINED; |
| 253 | cfi->vals[i].base = CFI_UNDEFINED; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 254 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 255 | cfi->cfa.base = CFI_UNDEFINED; |
| 256 | cfi->drap_reg = CFI_UNDEFINED; |
| 257 | cfi->drap_offset = -1; |
| 258 | } |
| 259 | |
| 260 | static void init_insn_state(struct insn_state *state, struct section *sec) |
| 261 | { |
| 262 | memset(state, 0, sizeof(*state)); |
| 263 | init_cfi_state(&state->cfi); |
| 264 | |
| 265 | /* |
| 266 | * We need the full vmlinux for noinstr validation, otherwise we can |
| 267 | * not correctly determine insn->call_dest->sec (external symbols do |
| 268 | * not have a section). |
| 269 | */ |
| 270 | if (vmlinux && sec) |
| 271 | state->noinstr = sec->noinstr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 274 | static struct cfi_state *cfi_alloc(void) |
| 275 | { |
| 276 | struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1); |
| 277 | if (!cfi) { |
| 278 | WARN("calloc failed"); |
| 279 | exit(1); |
| 280 | } |
| 281 | nr_cfi++; |
| 282 | return cfi; |
| 283 | } |
| 284 | |
| 285 | static int cfi_bits; |
| 286 | static struct hlist_head *cfi_hash; |
| 287 | |
| 288 | static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2) |
| 289 | { |
| 290 | return memcmp((void *)cfi1 + sizeof(cfi1->hash), |
| 291 | (void *)cfi2 + sizeof(cfi2->hash), |
| 292 | sizeof(struct cfi_state) - sizeof(struct hlist_node)); |
| 293 | } |
| 294 | |
| 295 | static inline u32 cfi_key(struct cfi_state *cfi) |
| 296 | { |
| 297 | return jhash((void *)cfi + sizeof(cfi->hash), |
| 298 | sizeof(*cfi) - sizeof(cfi->hash), 0); |
| 299 | } |
| 300 | |
| 301 | static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi) |
| 302 | { |
| 303 | struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; |
| 304 | struct cfi_state *obj; |
| 305 | |
| 306 | hlist_for_each_entry(obj, head, hash) { |
| 307 | if (!cficmp(cfi, obj)) { |
| 308 | nr_cfi_cache++; |
| 309 | return obj; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | obj = cfi_alloc(); |
| 314 | *obj = *cfi; |
| 315 | hlist_add_head(&obj->hash, head); |
| 316 | |
| 317 | return obj; |
| 318 | } |
| 319 | |
| 320 | static void cfi_hash_add(struct cfi_state *cfi) |
| 321 | { |
| 322 | struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; |
| 323 | |
| 324 | hlist_add_head(&cfi->hash, head); |
| 325 | } |
| 326 | |
| 327 | static void *cfi_hash_alloc(void) |
| 328 | { |
| 329 | cfi_bits = vmlinux ? ELF_HASH_BITS - 3 : 13; |
| 330 | cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits, |
| 331 | PROT_READ|PROT_WRITE, |
| 332 | MAP_PRIVATE|MAP_ANON, -1, 0); |
| 333 | if (cfi_hash == (void *)-1L) { |
| 334 | WARN("mmap fail cfi_hash"); |
| 335 | cfi_hash = NULL; |
| 336 | } else if (stats) { |
| 337 | printf("cfi_bits: %d\n", cfi_bits); |
| 338 | } |
| 339 | |
| 340 | return cfi_hash; |
| 341 | } |
| 342 | |
| 343 | static unsigned long nr_insns; |
| 344 | static unsigned long nr_insns_visited; |
| 345 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 346 | /* |
| 347 | * Call the arch-specific instruction decoder for all the instructions and add |
| 348 | * them to the global instruction list. |
| 349 | */ |
| 350 | static int decode_instructions(struct objtool_file *file) |
| 351 | { |
| 352 | struct section *sec; |
| 353 | struct symbol *func; |
| 354 | unsigned long offset; |
| 355 | struct instruction *insn; |
| 356 | int ret; |
| 357 | |
| 358 | for_each_sec(file, sec) { |
| 359 | |
| 360 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) |
| 361 | continue; |
| 362 | |
| 363 | if (strcmp(sec->name, ".altinstr_replacement") && |
| 364 | strcmp(sec->name, ".altinstr_aux") && |
| 365 | strncmp(sec->name, ".discard.", 9)) |
| 366 | sec->text = true; |
| 367 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 368 | if (!strcmp(sec->name, ".noinstr.text") || |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 369 | !strcmp(sec->name, ".entry.text") || |
| 370 | !strncmp(sec->name, ".text.__x86.", 12)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 371 | sec->noinstr = true; |
| 372 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 373 | for (offset = 0; offset < sec->len; offset += insn->len) { |
| 374 | insn = malloc(sizeof(*insn)); |
| 375 | if (!insn) { |
| 376 | WARN("malloc failed"); |
| 377 | return -1; |
| 378 | } |
| 379 | memset(insn, 0, sizeof(*insn)); |
| 380 | INIT_LIST_HEAD(&insn->alts); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 381 | INIT_LIST_HEAD(&insn->stack_ops); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 382 | |
| 383 | insn->sec = sec; |
| 384 | insn->offset = offset; |
| 385 | |
| 386 | ret = arch_decode_instruction(file->elf, sec, offset, |
| 387 | sec->len - offset, |
| 388 | &insn->len, &insn->type, |
| 389 | &insn->immediate, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 390 | &insn->stack_ops); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 391 | if (ret) |
| 392 | goto err; |
| 393 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 394 | hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 395 | list_add_tail(&insn->list, &file->insn_list); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 396 | nr_insns++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | list_for_each_entry(func, &sec->symbol_list, list) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 400 | if (func->type != STT_FUNC || func->alias != func) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 401 | continue; |
| 402 | |
| 403 | if (!find_insn(file, sec, func->offset)) { |
| 404 | WARN("%s(): can't find starting instruction", |
| 405 | func->name); |
| 406 | return -1; |
| 407 | } |
| 408 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 409 | sym_for_each_insn(file, func, insn) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 410 | insn->func = func; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 414 | if (stats) |
| 415 | printf("nr_insns: %lu\n", nr_insns); |
| 416 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 417 | return 0; |
| 418 | |
| 419 | err: |
| 420 | free(insn); |
| 421 | return ret; |
| 422 | } |
| 423 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 424 | static struct instruction *find_last_insn(struct objtool_file *file, |
| 425 | struct section *sec) |
| 426 | { |
| 427 | struct instruction *insn = NULL; |
| 428 | unsigned int offset; |
| 429 | unsigned int end = (sec->len > 10) ? sec->len - 10 : 0; |
| 430 | |
| 431 | for (offset = sec->len - 1; offset >= end && !insn; offset--) |
| 432 | insn = find_insn(file, sec, offset); |
| 433 | |
| 434 | return insn; |
| 435 | } |
| 436 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 437 | /* |
| 438 | * Mark "ud2" instructions and manually annotated dead ends. |
| 439 | */ |
| 440 | static int add_dead_ends(struct objtool_file *file) |
| 441 | { |
| 442 | struct section *sec; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 443 | struct reloc *reloc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 444 | struct instruction *insn; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 445 | |
| 446 | /* |
| 447 | * By default, "ud2" is a dead end unless otherwise annotated, because |
| 448 | * GCC 7 inserts it for certain divide-by-zero cases. |
| 449 | */ |
| 450 | for_each_insn(file, insn) |
| 451 | if (insn->type == INSN_BUG) |
| 452 | insn->dead_end = true; |
| 453 | |
| 454 | /* |
| 455 | * Check for manually annotated dead ends. |
| 456 | */ |
| 457 | sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); |
| 458 | if (!sec) |
| 459 | goto reachable; |
| 460 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 461 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
| 462 | if (reloc->sym->type != STT_SECTION) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 463 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 464 | return -1; |
| 465 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 466 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 467 | if (insn) |
| 468 | insn = list_prev_entry(insn, list); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 469 | else if (reloc->addend == reloc->sym->sec->len) { |
| 470 | insn = find_last_insn(file, reloc->sym->sec); |
| 471 | if (!insn) { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 472 | WARN("can't find unreachable insn at %s+0x%" PRIx64, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 473 | reloc->sym->sec->name, reloc->addend); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 474 | return -1; |
| 475 | } |
| 476 | } else { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 477 | WARN("can't find unreachable insn at %s+0x%" PRIx64, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 478 | reloc->sym->sec->name, reloc->addend); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 479 | return -1; |
| 480 | } |
| 481 | |
| 482 | insn->dead_end = true; |
| 483 | } |
| 484 | |
| 485 | reachable: |
| 486 | /* |
| 487 | * These manually annotated reachable checks are needed for GCC 4.4, |
| 488 | * where the Linux unreachable() macro isn't supported. In that case |
| 489 | * GCC doesn't know the "ud2" is fatal, so it generates code as if it's |
| 490 | * not a dead end. |
| 491 | */ |
| 492 | sec = find_section_by_name(file->elf, ".rela.discard.reachable"); |
| 493 | if (!sec) |
| 494 | return 0; |
| 495 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 496 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
| 497 | if (reloc->sym->type != STT_SECTION) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 498 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 499 | return -1; |
| 500 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 501 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 502 | if (insn) |
| 503 | insn = list_prev_entry(insn, list); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 504 | else if (reloc->addend == reloc->sym->sec->len) { |
| 505 | insn = find_last_insn(file, reloc->sym->sec); |
| 506 | if (!insn) { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 507 | WARN("can't find reachable insn at %s+0x%" PRIx64, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 508 | reloc->sym->sec->name, reloc->addend); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 509 | return -1; |
| 510 | } |
| 511 | } else { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 512 | WARN("can't find reachable insn at %s+0x%" PRIx64, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 513 | reloc->sym->sec->name, reloc->addend); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 514 | return -1; |
| 515 | } |
| 516 | |
| 517 | insn->dead_end = false; |
| 518 | } |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 523 | static int create_static_call_sections(struct objtool_file *file) |
| 524 | { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 525 | struct section *sec; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 526 | struct static_call_site *site; |
| 527 | struct instruction *insn; |
| 528 | struct symbol *key_sym; |
| 529 | char *key_name, *tmp; |
| 530 | int idx; |
| 531 | |
| 532 | sec = find_section_by_name(file->elf, ".static_call_sites"); |
| 533 | if (sec) { |
| 534 | INIT_LIST_HEAD(&file->static_call_list); |
| 535 | WARN("file already has .static_call_sites section, skipping"); |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | if (list_empty(&file->static_call_list)) |
| 540 | return 0; |
| 541 | |
| 542 | idx = 0; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 543 | list_for_each_entry(insn, &file->static_call_list, call_node) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 544 | idx++; |
| 545 | |
| 546 | sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE, |
| 547 | sizeof(struct static_call_site), idx); |
| 548 | if (!sec) |
| 549 | return -1; |
| 550 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 551 | idx = 0; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 552 | list_for_each_entry(insn, &file->static_call_list, call_node) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 553 | |
| 554 | site = (struct static_call_site *)sec->data->d_buf + idx; |
| 555 | memset(site, 0, sizeof(struct static_call_site)); |
| 556 | |
| 557 | /* populate reloc for 'addr' */ |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 558 | if (elf_add_reloc_to_insn(file->elf, sec, |
| 559 | idx * sizeof(struct static_call_site), |
| 560 | R_X86_64_PC32, |
| 561 | insn->sec, insn->offset)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 562 | return -1; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 563 | |
| 564 | /* find key symbol */ |
| 565 | key_name = strdup(insn->call_dest->name); |
| 566 | if (!key_name) { |
| 567 | perror("strdup"); |
| 568 | return -1; |
| 569 | } |
| 570 | if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR, |
| 571 | STATIC_CALL_TRAMP_PREFIX_LEN)) { |
| 572 | WARN("static_call: trampoline name malformed: %s", key_name); |
| 573 | return -1; |
| 574 | } |
| 575 | tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN; |
| 576 | memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN); |
| 577 | |
| 578 | key_sym = find_symbol_by_name(file->elf, tmp); |
| 579 | if (!key_sym) { |
| 580 | if (!module) { |
| 581 | WARN("static_call: can't find static_call_key symbol: %s", tmp); |
| 582 | return -1; |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * For modules(), the key might not be exported, which |
| 587 | * means the module can make static calls but isn't |
| 588 | * allowed to change them. |
| 589 | * |
| 590 | * In that case we temporarily set the key to be the |
| 591 | * trampoline address. This is fixed up in |
| 592 | * static_call_add_module(). |
| 593 | */ |
| 594 | key_sym = insn->call_dest; |
| 595 | } |
| 596 | free(key_name); |
| 597 | |
| 598 | /* populate reloc for 'key' */ |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 599 | if (elf_add_reloc(file->elf, sec, |
| 600 | idx * sizeof(struct static_call_site) + 4, |
| 601 | R_X86_64_PC32, key_sym, |
| 602 | is_sibling_call(insn) * STATIC_CALL_SITE_TAIL)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 603 | return -1; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 604 | |
| 605 | idx++; |
| 606 | } |
| 607 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static int create_retpoline_sites_sections(struct objtool_file *file) |
| 612 | { |
| 613 | struct instruction *insn; |
| 614 | struct section *sec; |
| 615 | int idx; |
| 616 | |
| 617 | sec = find_section_by_name(file->elf, ".retpoline_sites"); |
| 618 | if (sec) { |
| 619 | WARN("file already has .retpoline_sites, skipping"); |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | idx = 0; |
| 624 | list_for_each_entry(insn, &file->retpoline_call_list, call_node) |
| 625 | idx++; |
| 626 | |
| 627 | if (!idx) |
| 628 | return 0; |
| 629 | |
| 630 | sec = elf_create_section(file->elf, ".retpoline_sites", 0, |
| 631 | sizeof(int), idx); |
| 632 | if (!sec) { |
| 633 | WARN("elf_create_section: .retpoline_sites"); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 634 | return -1; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 635 | } |
| 636 | |
| 637 | idx = 0; |
| 638 | list_for_each_entry(insn, &file->retpoline_call_list, call_node) { |
| 639 | |
| 640 | int *site = (int *)sec->data->d_buf + idx; |
| 641 | *site = 0; |
| 642 | |
| 643 | if (elf_add_reloc_to_insn(file->elf, sec, |
| 644 | idx * sizeof(int), |
| 645 | R_X86_64_PC32, |
| 646 | insn->sec, insn->offset)) { |
| 647 | WARN("elf_add_reloc_to_insn: .retpoline_sites"); |
| 648 | return -1; |
| 649 | } |
| 650 | |
| 651 | idx++; |
| 652 | } |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int create_return_sites_sections(struct objtool_file *file) |
| 658 | { |
| 659 | struct instruction *insn; |
| 660 | struct section *sec; |
| 661 | int idx; |
| 662 | |
| 663 | sec = find_section_by_name(file->elf, ".return_sites"); |
| 664 | if (sec) { |
| 665 | WARN("file already has .return_sites, skipping"); |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | idx = 0; |
| 670 | list_for_each_entry(insn, &file->return_thunk_list, call_node) |
| 671 | idx++; |
| 672 | |
| 673 | if (!idx) |
| 674 | return 0; |
| 675 | |
| 676 | sec = elf_create_section(file->elf, ".return_sites", 0, |
| 677 | sizeof(int), idx); |
| 678 | if (!sec) { |
| 679 | WARN("elf_create_section: .return_sites"); |
| 680 | return -1; |
| 681 | } |
| 682 | |
| 683 | idx = 0; |
| 684 | list_for_each_entry(insn, &file->return_thunk_list, call_node) { |
| 685 | |
| 686 | int *site = (int *)sec->data->d_buf + idx; |
| 687 | *site = 0; |
| 688 | |
| 689 | if (elf_add_reloc_to_insn(file->elf, sec, |
| 690 | idx * sizeof(int), |
| 691 | R_X86_64_PC32, |
| 692 | insn->sec, insn->offset)) { |
| 693 | WARN("elf_add_reloc_to_insn: .return_sites"); |
| 694 | return -1; |
| 695 | } |
| 696 | |
| 697 | idx++; |
| 698 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 703 | /* |
| 704 | * Warnings shouldn't be reported for ignored functions. |
| 705 | */ |
| 706 | static void add_ignores(struct objtool_file *file) |
| 707 | { |
| 708 | struct instruction *insn; |
| 709 | struct section *sec; |
| 710 | struct symbol *func; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 711 | struct reloc *reloc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 712 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 713 | sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); |
| 714 | if (!sec) |
| 715 | return; |
| 716 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 717 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
| 718 | switch (reloc->sym->type) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 719 | case STT_FUNC: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 720 | func = reloc->sym; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 721 | break; |
| 722 | |
| 723 | case STT_SECTION: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 724 | func = find_func_by_offset(reloc->sym->sec, reloc->addend); |
| 725 | if (!func) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 726 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 727 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 728 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 729 | default: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 730 | WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 731 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 732 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 733 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 734 | func_for_each_insn(file, func, insn) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 735 | insn->ignore = true; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * This is a whitelist of functions that is allowed to be called with AC set. |
| 741 | * The list is meant to be minimal and only contains compiler instrumentation |
| 742 | * ABI and a few functions used to implement *_{to,from}_user() functions. |
| 743 | * |
| 744 | * These functions must not directly change AC, but may PUSHF/POPF. |
| 745 | */ |
| 746 | static const char *uaccess_safe_builtin[] = { |
| 747 | /* KASAN */ |
| 748 | "kasan_report", |
| 749 | "check_memory_region", |
| 750 | /* KASAN out-of-line */ |
| 751 | "__asan_loadN_noabort", |
| 752 | "__asan_load1_noabort", |
| 753 | "__asan_load2_noabort", |
| 754 | "__asan_load4_noabort", |
| 755 | "__asan_load8_noabort", |
| 756 | "__asan_load16_noabort", |
| 757 | "__asan_storeN_noabort", |
| 758 | "__asan_store1_noabort", |
| 759 | "__asan_store2_noabort", |
| 760 | "__asan_store4_noabort", |
| 761 | "__asan_store8_noabort", |
| 762 | "__asan_store16_noabort", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 763 | "__kasan_check_read", |
| 764 | "__kasan_check_write", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 765 | /* KASAN in-line */ |
| 766 | "__asan_report_load_n_noabort", |
| 767 | "__asan_report_load1_noabort", |
| 768 | "__asan_report_load2_noabort", |
| 769 | "__asan_report_load4_noabort", |
| 770 | "__asan_report_load8_noabort", |
| 771 | "__asan_report_load16_noabort", |
| 772 | "__asan_report_store_n_noabort", |
| 773 | "__asan_report_store1_noabort", |
| 774 | "__asan_report_store2_noabort", |
| 775 | "__asan_report_store4_noabort", |
| 776 | "__asan_report_store8_noabort", |
| 777 | "__asan_report_store16_noabort", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 778 | /* KCSAN */ |
| 779 | "__kcsan_check_access", |
| 780 | "kcsan_found_watchpoint", |
| 781 | "kcsan_setup_watchpoint", |
| 782 | "kcsan_check_scoped_accesses", |
| 783 | "kcsan_disable_current", |
| 784 | "kcsan_enable_current_nowarn", |
| 785 | /* KCSAN/TSAN */ |
| 786 | "__tsan_func_entry", |
| 787 | "__tsan_func_exit", |
| 788 | "__tsan_read_range", |
| 789 | "__tsan_write_range", |
| 790 | "__tsan_read1", |
| 791 | "__tsan_read2", |
| 792 | "__tsan_read4", |
| 793 | "__tsan_read8", |
| 794 | "__tsan_read16", |
| 795 | "__tsan_write1", |
| 796 | "__tsan_write2", |
| 797 | "__tsan_write4", |
| 798 | "__tsan_write8", |
| 799 | "__tsan_write16", |
| 800 | "__tsan_read_write1", |
| 801 | "__tsan_read_write2", |
| 802 | "__tsan_read_write4", |
| 803 | "__tsan_read_write8", |
| 804 | "__tsan_read_write16", |
| 805 | "__tsan_atomic8_load", |
| 806 | "__tsan_atomic16_load", |
| 807 | "__tsan_atomic32_load", |
| 808 | "__tsan_atomic64_load", |
| 809 | "__tsan_atomic8_store", |
| 810 | "__tsan_atomic16_store", |
| 811 | "__tsan_atomic32_store", |
| 812 | "__tsan_atomic64_store", |
| 813 | "__tsan_atomic8_exchange", |
| 814 | "__tsan_atomic16_exchange", |
| 815 | "__tsan_atomic32_exchange", |
| 816 | "__tsan_atomic64_exchange", |
| 817 | "__tsan_atomic8_fetch_add", |
| 818 | "__tsan_atomic16_fetch_add", |
| 819 | "__tsan_atomic32_fetch_add", |
| 820 | "__tsan_atomic64_fetch_add", |
| 821 | "__tsan_atomic8_fetch_sub", |
| 822 | "__tsan_atomic16_fetch_sub", |
| 823 | "__tsan_atomic32_fetch_sub", |
| 824 | "__tsan_atomic64_fetch_sub", |
| 825 | "__tsan_atomic8_fetch_and", |
| 826 | "__tsan_atomic16_fetch_and", |
| 827 | "__tsan_atomic32_fetch_and", |
| 828 | "__tsan_atomic64_fetch_and", |
| 829 | "__tsan_atomic8_fetch_or", |
| 830 | "__tsan_atomic16_fetch_or", |
| 831 | "__tsan_atomic32_fetch_or", |
| 832 | "__tsan_atomic64_fetch_or", |
| 833 | "__tsan_atomic8_fetch_xor", |
| 834 | "__tsan_atomic16_fetch_xor", |
| 835 | "__tsan_atomic32_fetch_xor", |
| 836 | "__tsan_atomic64_fetch_xor", |
| 837 | "__tsan_atomic8_fetch_nand", |
| 838 | "__tsan_atomic16_fetch_nand", |
| 839 | "__tsan_atomic32_fetch_nand", |
| 840 | "__tsan_atomic64_fetch_nand", |
| 841 | "__tsan_atomic8_compare_exchange_strong", |
| 842 | "__tsan_atomic16_compare_exchange_strong", |
| 843 | "__tsan_atomic32_compare_exchange_strong", |
| 844 | "__tsan_atomic64_compare_exchange_strong", |
| 845 | "__tsan_atomic8_compare_exchange_weak", |
| 846 | "__tsan_atomic16_compare_exchange_weak", |
| 847 | "__tsan_atomic32_compare_exchange_weak", |
| 848 | "__tsan_atomic64_compare_exchange_weak", |
| 849 | "__tsan_atomic8_compare_exchange_val", |
| 850 | "__tsan_atomic16_compare_exchange_val", |
| 851 | "__tsan_atomic32_compare_exchange_val", |
| 852 | "__tsan_atomic64_compare_exchange_val", |
| 853 | "__tsan_atomic_thread_fence", |
| 854 | "__tsan_atomic_signal_fence", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 855 | /* KCOV */ |
| 856 | "write_comp_data", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 857 | "check_kcov_mode", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 858 | "__sanitizer_cov_trace_pc", |
| 859 | "__sanitizer_cov_trace_const_cmp1", |
| 860 | "__sanitizer_cov_trace_const_cmp2", |
| 861 | "__sanitizer_cov_trace_const_cmp4", |
| 862 | "__sanitizer_cov_trace_const_cmp8", |
| 863 | "__sanitizer_cov_trace_cmp1", |
| 864 | "__sanitizer_cov_trace_cmp2", |
| 865 | "__sanitizer_cov_trace_cmp4", |
| 866 | "__sanitizer_cov_trace_cmp8", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 867 | "__sanitizer_cov_trace_switch", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 868 | /* UBSAN */ |
| 869 | "ubsan_type_mismatch_common", |
| 870 | "__ubsan_handle_type_mismatch", |
| 871 | "__ubsan_handle_type_mismatch_v1", |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 872 | "__ubsan_handle_shift_out_of_bounds", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 873 | /* misc */ |
| 874 | "csum_partial_copy_generic", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 875 | "copy_mc_fragile", |
| 876 | "copy_mc_fragile_handle_tail", |
| 877 | "copy_mc_enhanced_fast_string", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 878 | "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */ |
| 879 | NULL |
| 880 | }; |
| 881 | |
| 882 | static void add_uaccess_safe(struct objtool_file *file) |
| 883 | { |
| 884 | struct symbol *func; |
| 885 | const char **name; |
| 886 | |
| 887 | if (!uaccess) |
| 888 | return; |
| 889 | |
| 890 | for (name = uaccess_safe_builtin; *name; name++) { |
| 891 | func = find_symbol_by_name(file->elf, *name); |
| 892 | if (!func) |
| 893 | continue; |
| 894 | |
| 895 | func->uaccess_safe = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * FIXME: For now, just ignore any alternatives which add retpolines. This is |
| 901 | * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline. |
| 902 | * But it at least allows objtool to understand the control flow *around* the |
| 903 | * retpoline. |
| 904 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 905 | static int add_ignore_alternatives(struct objtool_file *file) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 906 | { |
| 907 | struct section *sec; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 908 | struct reloc *reloc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 909 | struct instruction *insn; |
| 910 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 911 | sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 912 | if (!sec) |
| 913 | return 0; |
| 914 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 915 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
| 916 | if (reloc->sym->type != STT_SECTION) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 917 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 918 | return -1; |
| 919 | } |
| 920 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 921 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 922 | if (!insn) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 923 | WARN("bad .discard.ignore_alts entry"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 924 | return -1; |
| 925 | } |
| 926 | |
| 927 | insn->ignore_alts = true; |
| 928 | } |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 933 | __weak bool arch_is_retpoline(struct symbol *sym) |
| 934 | { |
| 935 | return false; |
| 936 | } |
| 937 | |
| 938 | __weak bool arch_is_rethunk(struct symbol *sym) |
| 939 | { |
| 940 | return false; |
| 941 | } |
| 942 | |
| 943 | #define NEGATIVE_RELOC ((void *)-1L) |
| 944 | |
| 945 | static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn) |
| 946 | { |
| 947 | if (insn->reloc == NEGATIVE_RELOC) |
| 948 | return NULL; |
| 949 | |
| 950 | if (!insn->reloc) { |
| 951 | insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec, |
| 952 | insn->offset, insn->len); |
| 953 | if (!insn->reloc) { |
| 954 | insn->reloc = NEGATIVE_RELOC; |
| 955 | return NULL; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | return insn->reloc; |
| 960 | } |
| 961 | |
| 962 | static void remove_insn_ops(struct instruction *insn) |
| 963 | { |
| 964 | struct stack_op *op, *tmp; |
| 965 | |
| 966 | list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) { |
| 967 | list_del(&op->list); |
| 968 | free(op); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | static void annotate_call_site(struct objtool_file *file, |
| 973 | struct instruction *insn, bool sibling) |
| 974 | { |
| 975 | struct reloc *reloc = insn_reloc(file, insn); |
| 976 | struct symbol *sym = insn->call_dest; |
| 977 | |
| 978 | if (!sym) |
| 979 | sym = reloc->sym; |
| 980 | |
| 981 | /* |
| 982 | * Alternative replacement code is just template code which is |
| 983 | * sometimes copied to the original instruction. For now, don't |
| 984 | * annotate it. (In the future we might consider annotating the |
| 985 | * original instruction if/when it ever makes sense to do so.) |
| 986 | */ |
| 987 | if (!strcmp(insn->sec->name, ".altinstr_replacement")) |
| 988 | return; |
| 989 | |
| 990 | if (sym->static_call_tramp) { |
| 991 | list_add_tail(&insn->call_node, &file->static_call_list); |
| 992 | return; |
| 993 | } |
| 994 | |
| 995 | if (sym->retpoline_thunk) { |
| 996 | list_add_tail(&insn->call_node, &file->retpoline_call_list); |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * Many compilers cannot disable KCOV with a function attribute |
| 1002 | * so they need a little help, NOP out any KCOV calls from noinstr |
| 1003 | * text. |
| 1004 | */ |
| 1005 | if (insn->sec->noinstr && sym->kcov) { |
| 1006 | if (reloc) { |
| 1007 | reloc->type = R_NONE; |
| 1008 | elf_write_reloc(file->elf, reloc); |
| 1009 | } |
| 1010 | |
| 1011 | elf_write_insn(file->elf, insn->sec, |
| 1012 | insn->offset, insn->len, |
| 1013 | sibling ? arch_ret_insn(insn->len) |
| 1014 | : arch_nop_insn(insn->len)); |
| 1015 | |
| 1016 | insn->type = sibling ? INSN_RETURN : INSN_NOP; |
| 1017 | |
| 1018 | if (sibling) { |
| 1019 | /* |
| 1020 | * We've replaced the tail-call JMP insn by two new |
| 1021 | * insn: RET; INT3, except we only have a single struct |
| 1022 | * insn here. Mark it retpoline_safe to avoid the SLS |
| 1023 | * warning, instead of adding another insn. |
| 1024 | */ |
| 1025 | insn->retpoline_safe = true; |
| 1026 | } |
| 1027 | |
| 1028 | return; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | static void add_call_dest(struct objtool_file *file, struct instruction *insn, |
| 1033 | struct symbol *dest, bool sibling) |
| 1034 | { |
| 1035 | insn->call_dest = dest; |
| 1036 | if (!dest) |
| 1037 | return; |
| 1038 | |
| 1039 | /* |
| 1040 | * Whatever stack impact regular CALLs have, should be undone |
| 1041 | * by the RETURN of the called function. |
| 1042 | * |
| 1043 | * Annotated intra-function calls retain the stack_ops but |
| 1044 | * are converted to JUMP, see read_intra_function_calls(). |
| 1045 | */ |
| 1046 | remove_insn_ops(insn); |
| 1047 | |
| 1048 | annotate_call_site(file, insn, sibling); |
| 1049 | } |
| 1050 | |
| 1051 | static void add_retpoline_call(struct objtool_file *file, struct instruction *insn) |
| 1052 | { |
| 1053 | /* |
| 1054 | * Retpoline calls/jumps are really dynamic calls/jumps in disguise, |
| 1055 | * so convert them accordingly. |
| 1056 | */ |
| 1057 | switch (insn->type) { |
| 1058 | case INSN_CALL: |
| 1059 | insn->type = INSN_CALL_DYNAMIC; |
| 1060 | break; |
| 1061 | case INSN_JUMP_UNCONDITIONAL: |
| 1062 | insn->type = INSN_JUMP_DYNAMIC; |
| 1063 | break; |
| 1064 | case INSN_JUMP_CONDITIONAL: |
| 1065 | insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL; |
| 1066 | break; |
| 1067 | default: |
| 1068 | return; |
| 1069 | } |
| 1070 | |
| 1071 | insn->retpoline_safe = true; |
| 1072 | |
| 1073 | /* |
| 1074 | * Whatever stack impact regular CALLs have, should be undone |
| 1075 | * by the RETURN of the called function. |
| 1076 | * |
| 1077 | * Annotated intra-function calls retain the stack_ops but |
| 1078 | * are converted to JUMP, see read_intra_function_calls(). |
| 1079 | */ |
| 1080 | remove_insn_ops(insn); |
| 1081 | |
| 1082 | annotate_call_site(file, insn, false); |
| 1083 | } |
| 1084 | |
| 1085 | static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add) |
| 1086 | { |
| 1087 | /* |
| 1088 | * Return thunk tail calls are really just returns in disguise, |
| 1089 | * so convert them accordingly. |
| 1090 | */ |
| 1091 | insn->type = INSN_RETURN; |
| 1092 | insn->retpoline_safe = true; |
| 1093 | |
| 1094 | /* Skip the non-text sections, specially .discard ones */ |
| 1095 | if (add && insn->sec->text) |
| 1096 | list_add_tail(&insn->call_node, &file->return_thunk_list); |
| 1097 | } |
| 1098 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1099 | /* |
| 1100 | * Find the destination instructions for all jumps. |
| 1101 | */ |
| 1102 | static int add_jump_destinations(struct objtool_file *file) |
| 1103 | { |
| 1104 | struct instruction *insn; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1105 | struct reloc *reloc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1106 | struct section *dest_sec; |
| 1107 | unsigned long dest_off; |
| 1108 | |
| 1109 | for_each_insn(file, insn) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1110 | if (!is_static_jump(insn)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1111 | continue; |
| 1112 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1113 | reloc = insn_reloc(file, insn); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1114 | if (!reloc) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1115 | dest_sec = insn->sec; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1116 | dest_off = arch_jump_destination(insn); |
| 1117 | } else if (reloc->sym->type == STT_SECTION) { |
| 1118 | dest_sec = reloc->sym->sec; |
| 1119 | dest_off = arch_dest_reloc_offset(reloc->addend); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1120 | } else if (reloc->sym->retpoline_thunk) { |
| 1121 | add_retpoline_call(file, insn); |
| 1122 | continue; |
| 1123 | } else if (reloc->sym->return_thunk) { |
| 1124 | add_return_call(file, insn, true); |
| 1125 | continue; |
| 1126 | } else if (insn->func) { |
| 1127 | /* internal or external sibling call (with reloc) */ |
| 1128 | add_call_dest(file, insn, reloc->sym, true); |
| 1129 | continue; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1130 | } else if (reloc->sym->sec->idx) { |
| 1131 | dest_sec = reloc->sym->sec; |
| 1132 | dest_off = reloc->sym->sym.st_value + |
| 1133 | arch_dest_reloc_offset(reloc->addend); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1134 | } else { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1135 | /* non-func asm code jumping to another file */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1136 | continue; |
| 1137 | } |
| 1138 | |
| 1139 | insn->jump_dest = find_insn(file, dest_sec, dest_off); |
| 1140 | if (!insn->jump_dest) { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1141 | struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1142 | |
| 1143 | /* |
| 1144 | * This is a special case where an alt instruction |
| 1145 | * jumps past the end of the section. These are |
| 1146 | * handled later in handle_group_alt(). |
| 1147 | */ |
| 1148 | if (!strcmp(insn->sec->name, ".altinstr_replacement")) |
| 1149 | continue; |
| 1150 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1151 | /* |
| 1152 | * This is a special case for zen_untrain_ret(). |
| 1153 | * It jumps to __x86_return_thunk(), but objtool |
| 1154 | * can't find the thunk's starting RET |
| 1155 | * instruction, because the RET is also in the |
| 1156 | * middle of another instruction. Objtool only |
| 1157 | * knows about the outer instruction. |
| 1158 | */ |
| 1159 | if (sym && sym->return_thunk) { |
| 1160 | add_return_call(file, insn, false); |
| 1161 | continue; |
| 1162 | } |
| 1163 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1164 | WARN_FUNC("can't find jump dest instruction at %s+0x%lx", |
| 1165 | insn->sec, insn->offset, dest_sec->name, |
| 1166 | dest_off); |
| 1167 | return -1; |
| 1168 | } |
| 1169 | |
| 1170 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1171 | * Cross-function jump. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1172 | */ |
| 1173 | if (insn->func && insn->jump_dest->func && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1174 | insn->func != insn->jump_dest->func) { |
| 1175 | |
| 1176 | /* |
| 1177 | * For GCC 8+, create parent/child links for any cold |
| 1178 | * subfunctions. This is _mostly_ redundant with a |
| 1179 | * similar initialization in read_symbols(). |
| 1180 | * |
| 1181 | * If a function has aliases, we want the *first* such |
| 1182 | * function in the symbol table to be the subfunction's |
| 1183 | * parent. In that case we overwrite the |
| 1184 | * initialization done in read_symbols(). |
| 1185 | * |
| 1186 | * However this code can't completely replace the |
| 1187 | * read_symbols() code because this doesn't detect the |
| 1188 | * case where the parent function's only reference to a |
| 1189 | * subfunction is through a jump table. |
| 1190 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1191 | if (!strstr(insn->func->name, ".cold") && |
| 1192 | strstr(insn->jump_dest->func->name, ".cold")) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1193 | insn->func->cfunc = insn->jump_dest->func; |
| 1194 | insn->jump_dest->func->pfunc = insn->func; |
| 1195 | |
| 1196 | } else if (insn->jump_dest->func->pfunc != insn->func->pfunc && |
| 1197 | insn->jump_dest->offset == insn->jump_dest->func->offset) { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1198 | /* internal sibling call (without reloc) */ |
| 1199 | add_call_dest(file, insn, insn->jump_dest->func, true); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1200 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1207 | static struct symbol *find_call_destination(struct section *sec, unsigned long offset) |
| 1208 | { |
| 1209 | struct symbol *call_dest; |
| 1210 | |
| 1211 | call_dest = find_func_by_offset(sec, offset); |
| 1212 | if (!call_dest) |
| 1213 | call_dest = find_symbol_by_offset(sec, offset); |
| 1214 | |
| 1215 | return call_dest; |
| 1216 | } |
| 1217 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1218 | /* |
| 1219 | * Find the destination instructions for all calls. |
| 1220 | */ |
| 1221 | static int add_call_destinations(struct objtool_file *file) |
| 1222 | { |
| 1223 | struct instruction *insn; |
| 1224 | unsigned long dest_off; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1225 | struct symbol *dest; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1226 | struct reloc *reloc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1227 | |
| 1228 | for_each_insn(file, insn) { |
| 1229 | if (insn->type != INSN_CALL) |
| 1230 | continue; |
| 1231 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1232 | reloc = insn_reloc(file, insn); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1233 | if (!reloc) { |
| 1234 | dest_off = arch_jump_destination(insn); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1235 | dest = find_call_destination(insn->sec, dest_off); |
| 1236 | |
| 1237 | add_call_dest(file, insn, dest, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1238 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1239 | if (insn->ignore) |
| 1240 | continue; |
| 1241 | |
| 1242 | if (!insn->call_dest) { |
| 1243 | WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1244 | return -1; |
| 1245 | } |
| 1246 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1247 | if (insn->func && insn->call_dest->type != STT_FUNC) { |
| 1248 | WARN_FUNC("unsupported call to non-function", |
| 1249 | insn->sec, insn->offset); |
| 1250 | return -1; |
| 1251 | } |
| 1252 | |
| 1253 | } else if (reloc->sym->type == STT_SECTION) { |
| 1254 | dest_off = arch_dest_reloc_offset(reloc->addend); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1255 | dest = find_call_destination(reloc->sym->sec, dest_off); |
| 1256 | if (!dest) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1257 | WARN_FUNC("can't find call dest symbol at %s+0x%lx", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1258 | insn->sec, insn->offset, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1259 | reloc->sym->sec->name, |
| 1260 | dest_off); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1261 | return -1; |
| 1262 | } |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1263 | |
| 1264 | add_call_dest(file, insn, dest, false); |
| 1265 | |
| 1266 | } else if (reloc->sym->retpoline_thunk) { |
| 1267 | add_retpoline_call(file, insn); |
| 1268 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1269 | } else |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1270 | add_call_dest(file, insn, reloc->sym, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | /* |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1277 | * The .alternatives section requires some extra special care over and above |
| 1278 | * other special sections because alternatives are patched in place. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1279 | */ |
| 1280 | static int handle_group_alt(struct objtool_file *file, |
| 1281 | struct special_alt *special_alt, |
| 1282 | struct instruction *orig_insn, |
| 1283 | struct instruction **new_insn) |
| 1284 | { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1285 | struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL; |
| 1286 | struct alt_group *orig_alt_group, *new_alt_group; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1287 | unsigned long dest_off; |
| 1288 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1289 | |
| 1290 | orig_alt_group = malloc(sizeof(*orig_alt_group)); |
| 1291 | if (!orig_alt_group) { |
| 1292 | WARN("malloc failed"); |
| 1293 | return -1; |
| 1294 | } |
| 1295 | orig_alt_group->cfi = calloc(special_alt->orig_len, |
| 1296 | sizeof(struct cfi_state *)); |
| 1297 | if (!orig_alt_group->cfi) { |
| 1298 | WARN("calloc failed"); |
| 1299 | return -1; |
| 1300 | } |
| 1301 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1302 | last_orig_insn = NULL; |
| 1303 | insn = orig_insn; |
| 1304 | sec_for_each_insn_from(file, insn) { |
| 1305 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) |
| 1306 | break; |
| 1307 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1308 | insn->alt_group = orig_alt_group; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1309 | last_orig_insn = insn; |
| 1310 | } |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1311 | orig_alt_group->orig_group = NULL; |
| 1312 | orig_alt_group->first_insn = orig_insn; |
| 1313 | orig_alt_group->last_insn = last_orig_insn; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1314 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1315 | |
| 1316 | new_alt_group = malloc(sizeof(*new_alt_group)); |
| 1317 | if (!new_alt_group) { |
| 1318 | WARN("malloc failed"); |
| 1319 | return -1; |
| 1320 | } |
| 1321 | |
| 1322 | if (special_alt->new_len < special_alt->orig_len) { |
| 1323 | /* |
| 1324 | * Insert a fake nop at the end to make the replacement |
| 1325 | * alt_group the same size as the original. This is needed to |
| 1326 | * allow propagate_alt_cfi() to do its magic. When the last |
| 1327 | * instruction affects the stack, the instruction after it (the |
| 1328 | * nop) will propagate the new state to the shared CFI array. |
| 1329 | */ |
| 1330 | nop = malloc(sizeof(*nop)); |
| 1331 | if (!nop) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1332 | WARN("malloc failed"); |
| 1333 | return -1; |
| 1334 | } |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1335 | memset(nop, 0, sizeof(*nop)); |
| 1336 | INIT_LIST_HEAD(&nop->alts); |
| 1337 | INIT_LIST_HEAD(&nop->stack_ops); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1338 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1339 | nop->sec = special_alt->new_sec; |
| 1340 | nop->offset = special_alt->new_off + special_alt->new_len; |
| 1341 | nop->len = special_alt->orig_len - special_alt->new_len; |
| 1342 | nop->type = INSN_NOP; |
| 1343 | nop->func = orig_insn->func; |
| 1344 | nop->alt_group = new_alt_group; |
| 1345 | nop->ignore = orig_insn->ignore_alts; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | if (!special_alt->new_len) { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1349 | *new_insn = nop; |
| 1350 | goto end; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1353 | insn = *new_insn; |
| 1354 | sec_for_each_insn_from(file, insn) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1355 | struct reloc *alt_reloc; |
| 1356 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1357 | if (insn->offset >= special_alt->new_off + special_alt->new_len) |
| 1358 | break; |
| 1359 | |
| 1360 | last_new_insn = insn; |
| 1361 | |
| 1362 | insn->ignore = orig_insn->ignore_alts; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1363 | insn->func = orig_insn->func; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1364 | insn->alt_group = new_alt_group; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1365 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1366 | /* |
| 1367 | * Since alternative replacement code is copy/pasted by the |
| 1368 | * kernel after applying relocations, generally such code can't |
| 1369 | * have relative-address relocation references to outside the |
| 1370 | * .altinstr_replacement section, unless the arch's |
| 1371 | * alternatives code can adjust the relative offsets |
| 1372 | * accordingly. |
| 1373 | */ |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1374 | alt_reloc = insn_reloc(file, insn); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1375 | if (alt_reloc && |
| 1376 | !arch_support_alt_relocation(special_alt, insn, alt_reloc)) { |
| 1377 | |
| 1378 | WARN_FUNC("unsupported relocation in alternatives section", |
| 1379 | insn->sec, insn->offset); |
| 1380 | return -1; |
| 1381 | } |
| 1382 | |
| 1383 | if (!is_static_jump(insn)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1384 | continue; |
| 1385 | |
| 1386 | if (!insn->immediate) |
| 1387 | continue; |
| 1388 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1389 | dest_off = arch_jump_destination(insn); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1390 | if (dest_off == special_alt->new_off + special_alt->new_len) |
| 1391 | insn->jump_dest = next_insn_same_sec(file, last_orig_insn); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1392 | |
| 1393 | if (!insn->jump_dest) { |
| 1394 | WARN_FUNC("can't find alternative jump destination", |
| 1395 | insn->sec, insn->offset); |
| 1396 | return -1; |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | if (!last_new_insn) { |
| 1401 | WARN_FUNC("can't find last new alternative instruction", |
| 1402 | special_alt->new_sec, special_alt->new_off); |
| 1403 | return -1; |
| 1404 | } |
| 1405 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1406 | if (nop) |
| 1407 | list_add(&nop->list, &last_new_insn->list); |
| 1408 | end: |
| 1409 | new_alt_group->orig_group = orig_alt_group; |
| 1410 | new_alt_group->first_insn = *new_insn; |
| 1411 | new_alt_group->last_insn = nop ? : last_new_insn; |
| 1412 | new_alt_group->cfi = orig_alt_group->cfi; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | /* |
| 1417 | * A jump table entry can either convert a nop to a jump or a jump to a nop. |
| 1418 | * If the original instruction is a jump, make the alt entry an effective nop |
| 1419 | * by just skipping the original instruction. |
| 1420 | */ |
| 1421 | static int handle_jump_alt(struct objtool_file *file, |
| 1422 | struct special_alt *special_alt, |
| 1423 | struct instruction *orig_insn, |
| 1424 | struct instruction **new_insn) |
| 1425 | { |
| 1426 | if (orig_insn->type == INSN_NOP) |
| 1427 | return 0; |
| 1428 | |
| 1429 | if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { |
| 1430 | WARN_FUNC("unsupported instruction at jump label", |
| 1431 | orig_insn->sec, orig_insn->offset); |
| 1432 | return -1; |
| 1433 | } |
| 1434 | |
| 1435 | *new_insn = list_next_entry(orig_insn, list); |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
| 1439 | /* |
| 1440 | * Read all the special sections which have alternate instructions which can be |
| 1441 | * patched in or redirected to at runtime. Each instruction having alternate |
| 1442 | * instruction(s) has them added to its insn->alts list, which will be |
| 1443 | * traversed in validate_branch(). |
| 1444 | */ |
| 1445 | static int add_special_section_alts(struct objtool_file *file) |
| 1446 | { |
| 1447 | struct list_head special_alts; |
| 1448 | struct instruction *orig_insn, *new_insn; |
| 1449 | struct special_alt *special_alt, *tmp; |
| 1450 | struct alternative *alt; |
| 1451 | int ret; |
| 1452 | |
| 1453 | ret = special_get_alts(file->elf, &special_alts); |
| 1454 | if (ret) |
| 1455 | return ret; |
| 1456 | |
| 1457 | list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { |
| 1458 | |
| 1459 | orig_insn = find_insn(file, special_alt->orig_sec, |
| 1460 | special_alt->orig_off); |
| 1461 | if (!orig_insn) { |
| 1462 | WARN_FUNC("special: can't find orig instruction", |
| 1463 | special_alt->orig_sec, special_alt->orig_off); |
| 1464 | ret = -1; |
| 1465 | goto out; |
| 1466 | } |
| 1467 | |
| 1468 | new_insn = NULL; |
| 1469 | if (!special_alt->group || special_alt->new_len) { |
| 1470 | new_insn = find_insn(file, special_alt->new_sec, |
| 1471 | special_alt->new_off); |
| 1472 | if (!new_insn) { |
| 1473 | WARN_FUNC("special: can't find new instruction", |
| 1474 | special_alt->new_sec, |
| 1475 | special_alt->new_off); |
| 1476 | ret = -1; |
| 1477 | goto out; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | if (special_alt->group) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1482 | if (!special_alt->orig_len) { |
| 1483 | WARN_FUNC("empty alternative entry", |
| 1484 | orig_insn->sec, orig_insn->offset); |
| 1485 | continue; |
| 1486 | } |
| 1487 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1488 | ret = handle_group_alt(file, special_alt, orig_insn, |
| 1489 | &new_insn); |
| 1490 | if (ret) |
| 1491 | goto out; |
| 1492 | } else if (special_alt->jump_or_nop) { |
| 1493 | ret = handle_jump_alt(file, special_alt, orig_insn, |
| 1494 | &new_insn); |
| 1495 | if (ret) |
| 1496 | goto out; |
| 1497 | } |
| 1498 | |
| 1499 | alt = malloc(sizeof(*alt)); |
| 1500 | if (!alt) { |
| 1501 | WARN("malloc failed"); |
| 1502 | ret = -1; |
| 1503 | goto out; |
| 1504 | } |
| 1505 | |
| 1506 | alt->insn = new_insn; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1507 | alt->skip_orig = special_alt->skip_orig; |
| 1508 | orig_insn->ignore_alts |= special_alt->skip_alt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1509 | list_add_tail(&alt->list, &orig_insn->alts); |
| 1510 | |
| 1511 | list_del(&special_alt->list); |
| 1512 | free(special_alt); |
| 1513 | } |
| 1514 | |
| 1515 | out: |
| 1516 | return ret; |
| 1517 | } |
| 1518 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1519 | static int add_jump_table(struct objtool_file *file, struct instruction *insn, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1520 | struct reloc *table) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1521 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1522 | struct reloc *reloc = table; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1523 | struct instruction *dest_insn; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1524 | struct alternative *alt; |
| 1525 | struct symbol *pfunc = insn->func->pfunc; |
| 1526 | unsigned int prev_offset = 0; |
| 1527 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1528 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1529 | * Each @reloc is a switch table relocation which points to the target |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1530 | * instruction. |
| 1531 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1532 | list_for_each_entry_from(reloc, &table->sec->reloc_list, list) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1533 | |
| 1534 | /* Check for the end of the table: */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1535 | if (reloc != table && reloc->jump_table_start) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1536 | break; |
| 1537 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1538 | /* Make sure the table entries are consecutive: */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1539 | if (prev_offset && reloc->offset != prev_offset + 8) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1540 | break; |
| 1541 | |
| 1542 | /* Detect function pointers from contiguous objects: */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1543 | if (reloc->sym->sec == pfunc->sec && |
| 1544 | reloc->addend == pfunc->offset) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1545 | break; |
| 1546 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1547 | dest_insn = find_insn(file, reloc->sym->sec, reloc->addend); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1548 | if (!dest_insn) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1549 | break; |
| 1550 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1551 | /* Make sure the destination is in the same function: */ |
| 1552 | if (!dest_insn->func || dest_insn->func->pfunc != pfunc) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1553 | break; |
| 1554 | |
| 1555 | alt = malloc(sizeof(*alt)); |
| 1556 | if (!alt) { |
| 1557 | WARN("malloc failed"); |
| 1558 | return -1; |
| 1559 | } |
| 1560 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1561 | alt->insn = dest_insn; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1562 | list_add_tail(&alt->list, &insn->alts); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1563 | prev_offset = reloc->offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | if (!prev_offset) { |
| 1567 | WARN_FUNC("can't find switch jump table", |
| 1568 | insn->sec, insn->offset); |
| 1569 | return -1; |
| 1570 | } |
| 1571 | |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1576 | * find_jump_table() - Given a dynamic jump, find the switch jump table |
| 1577 | * associated with it. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1578 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1579 | static struct reloc *find_jump_table(struct objtool_file *file, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1580 | struct symbol *func, |
| 1581 | struct instruction *insn) |
| 1582 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1583 | struct reloc *table_reloc; |
| 1584 | struct instruction *dest_insn, *orig_insn = insn; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1585 | |
| 1586 | /* |
| 1587 | * Backward search using the @first_jump_src links, these help avoid |
| 1588 | * much of the 'in between' code. Which avoids us getting confused by |
| 1589 | * it. |
| 1590 | */ |
| 1591 | for (; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1592 | insn && insn->func && insn->func->pfunc == func; |
| 1593 | insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1594 | |
| 1595 | if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC) |
| 1596 | break; |
| 1597 | |
| 1598 | /* allow small jumps within the range */ |
| 1599 | if (insn->type == INSN_JUMP_UNCONDITIONAL && |
| 1600 | insn->jump_dest && |
| 1601 | (insn->jump_dest->offset <= insn->offset || |
| 1602 | insn->jump_dest->offset > orig_insn->offset)) |
| 1603 | break; |
| 1604 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1605 | table_reloc = arch_find_switch_table(file, insn); |
| 1606 | if (!table_reloc) |
| 1607 | continue; |
| 1608 | dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend); |
| 1609 | if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1610 | continue; |
| 1611 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1612 | return table_reloc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | return NULL; |
| 1616 | } |
| 1617 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1618 | /* |
| 1619 | * First pass: Mark the head of each jump table so that in the next pass, |
| 1620 | * we know when a given jump table ends and the next one starts. |
| 1621 | */ |
| 1622 | static void mark_func_jump_tables(struct objtool_file *file, |
| 1623 | struct symbol *func) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1624 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1625 | struct instruction *insn, *last = NULL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1626 | struct reloc *reloc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1627 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1628 | func_for_each_insn(file, func, insn) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1629 | if (!last) |
| 1630 | last = insn; |
| 1631 | |
| 1632 | /* |
| 1633 | * Store back-pointers for unconditional forward jumps such |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1634 | * that find_jump_table() can back-track using those and |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1635 | * avoid some potentially confusing code. |
| 1636 | */ |
| 1637 | if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest && |
| 1638 | insn->offset > last->offset && |
| 1639 | insn->jump_dest->offset > insn->offset && |
| 1640 | !insn->jump_dest->first_jump_src) { |
| 1641 | |
| 1642 | insn->jump_dest->first_jump_src = insn; |
| 1643 | last = insn->jump_dest; |
| 1644 | } |
| 1645 | |
| 1646 | if (insn->type != INSN_JUMP_DYNAMIC) |
| 1647 | continue; |
| 1648 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1649 | reloc = find_jump_table(file, func, insn); |
| 1650 | if (reloc) { |
| 1651 | reloc->jump_table_start = true; |
| 1652 | insn->jump_table = reloc; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | static int add_func_jump_tables(struct objtool_file *file, |
| 1658 | struct symbol *func) |
| 1659 | { |
| 1660 | struct instruction *insn; |
| 1661 | int ret; |
| 1662 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1663 | func_for_each_insn(file, func, insn) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1664 | if (!insn->jump_table) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1665 | continue; |
| 1666 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1667 | ret = add_jump_table(file, insn, insn->jump_table); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1668 | if (ret) |
| 1669 | return ret; |
| 1670 | } |
| 1671 | |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
| 1676 | * For some switch statements, gcc generates a jump table in the .rodata |
| 1677 | * section which contains a list of addresses within the function to jump to. |
| 1678 | * This finds these jump tables and adds them to the insn->alts lists. |
| 1679 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1680 | static int add_jump_table_alts(struct objtool_file *file) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1681 | { |
| 1682 | struct section *sec; |
| 1683 | struct symbol *func; |
| 1684 | int ret; |
| 1685 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1686 | if (!file->rodata) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1687 | return 0; |
| 1688 | |
| 1689 | for_each_sec(file, sec) { |
| 1690 | list_for_each_entry(func, &sec->symbol_list, list) { |
| 1691 | if (func->type != STT_FUNC) |
| 1692 | continue; |
| 1693 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1694 | mark_func_jump_tables(file, func); |
| 1695 | ret = add_func_jump_tables(file, func); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1696 | if (ret) |
| 1697 | return ret; |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | return 0; |
| 1702 | } |
| 1703 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1704 | static void set_func_state(struct cfi_state *state) |
| 1705 | { |
| 1706 | state->cfa = initial_func_cfi.cfa; |
| 1707 | memcpy(&state->regs, &initial_func_cfi.regs, |
| 1708 | CFI_NUM_REGS * sizeof(struct cfi_reg)); |
| 1709 | state->stack_size = initial_func_cfi.cfa.offset; |
| 1710 | } |
| 1711 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1712 | static int read_unwind_hints(struct objtool_file *file) |
| 1713 | { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1714 | struct cfi_state cfi = init_cfi; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1715 | struct section *sec, *relocsec; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1716 | struct unwind_hint *hint; |
| 1717 | struct instruction *insn; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1718 | struct reloc *reloc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1719 | int i; |
| 1720 | |
| 1721 | sec = find_section_by_name(file->elf, ".discard.unwind_hints"); |
| 1722 | if (!sec) |
| 1723 | return 0; |
| 1724 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1725 | relocsec = sec->reloc; |
| 1726 | if (!relocsec) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1727 | WARN("missing .rela.discard.unwind_hints section"); |
| 1728 | return -1; |
| 1729 | } |
| 1730 | |
| 1731 | if (sec->len % sizeof(struct unwind_hint)) { |
| 1732 | WARN("struct unwind_hint size mismatch"); |
| 1733 | return -1; |
| 1734 | } |
| 1735 | |
| 1736 | file->hints = true; |
| 1737 | |
| 1738 | for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) { |
| 1739 | hint = (struct unwind_hint *)sec->data->d_buf + i; |
| 1740 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1741 | reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint)); |
| 1742 | if (!reloc) { |
| 1743 | WARN("can't find reloc for unwind_hints[%d]", i); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1744 | return -1; |
| 1745 | } |
| 1746 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1747 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1748 | if (!insn) { |
| 1749 | WARN("can't find insn for unwind_hints[%d]", i); |
| 1750 | return -1; |
| 1751 | } |
| 1752 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1753 | insn->hint = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1754 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1755 | if (hint->type == UNWIND_HINT_TYPE_SAVE) { |
| 1756 | insn->hint = false; |
| 1757 | insn->save = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1758 | continue; |
| 1759 | } |
| 1760 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1761 | if (hint->type == UNWIND_HINT_TYPE_RESTORE) { |
| 1762 | insn->restore = true; |
| 1763 | continue; |
| 1764 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1765 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1766 | if (hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) { |
| 1767 | struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset); |
| 1768 | |
| 1769 | if (sym && sym->bind == STB_GLOBAL) { |
| 1770 | insn->entry = 1; |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | if (hint->type == UNWIND_HINT_TYPE_ENTRY) { |
| 1775 | hint->type = UNWIND_HINT_TYPE_CALL; |
| 1776 | insn->entry = 1; |
| 1777 | } |
| 1778 | |
| 1779 | if (hint->type == UNWIND_HINT_TYPE_FUNC) { |
| 1780 | insn->cfi = &func_cfi; |
| 1781 | continue; |
| 1782 | } |
| 1783 | |
| 1784 | if (insn->cfi) |
| 1785 | cfi = *(insn->cfi); |
| 1786 | |
| 1787 | if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1788 | WARN_FUNC("unsupported unwind_hint sp base reg %d", |
| 1789 | insn->sec, insn->offset, hint->sp_reg); |
| 1790 | return -1; |
| 1791 | } |
| 1792 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1793 | cfi.cfa.offset = hint->sp_offset; |
| 1794 | cfi.type = hint->type; |
| 1795 | cfi.end = hint->end; |
| 1796 | |
| 1797 | insn->cfi = cfi_hash_find_or_add(&cfi); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | return 0; |
| 1801 | } |
| 1802 | |
| 1803 | static int read_retpoline_hints(struct objtool_file *file) |
| 1804 | { |
| 1805 | struct section *sec; |
| 1806 | struct instruction *insn; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1807 | struct reloc *reloc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1808 | |
| 1809 | sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); |
| 1810 | if (!sec) |
| 1811 | return 0; |
| 1812 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1813 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
| 1814 | if (reloc->sym->type != STT_SECTION) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1815 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 1816 | return -1; |
| 1817 | } |
| 1818 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1819 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1820 | if (!insn) { |
| 1821 | WARN("bad .discard.retpoline_safe entry"); |
| 1822 | return -1; |
| 1823 | } |
| 1824 | |
| 1825 | if (insn->type != INSN_JUMP_DYNAMIC && |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1826 | insn->type != INSN_CALL_DYNAMIC && |
| 1827 | insn->type != INSN_RETURN && |
| 1828 | insn->type != INSN_NOP) { |
| 1829 | WARN_FUNC("retpoline_safe hint not an indirect jump/call/ret/nop", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1830 | insn->sec, insn->offset); |
| 1831 | return -1; |
| 1832 | } |
| 1833 | |
| 1834 | insn->retpoline_safe = true; |
| 1835 | } |
| 1836 | |
| 1837 | return 0; |
| 1838 | } |
| 1839 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1840 | static int read_instr_hints(struct objtool_file *file) |
| 1841 | { |
| 1842 | struct section *sec; |
| 1843 | struct instruction *insn; |
| 1844 | struct reloc *reloc; |
| 1845 | |
| 1846 | sec = find_section_by_name(file->elf, ".rela.discard.instr_end"); |
| 1847 | if (!sec) |
| 1848 | return 0; |
| 1849 | |
| 1850 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
| 1851 | if (reloc->sym->type != STT_SECTION) { |
| 1852 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 1853 | return -1; |
| 1854 | } |
| 1855 | |
| 1856 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
| 1857 | if (!insn) { |
| 1858 | WARN("bad .discard.instr_end entry"); |
| 1859 | return -1; |
| 1860 | } |
| 1861 | |
| 1862 | insn->instr--; |
| 1863 | } |
| 1864 | |
| 1865 | sec = find_section_by_name(file->elf, ".rela.discard.instr_begin"); |
| 1866 | if (!sec) |
| 1867 | return 0; |
| 1868 | |
| 1869 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
| 1870 | if (reloc->sym->type != STT_SECTION) { |
| 1871 | WARN("unexpected relocation symbol type in %s", sec->name); |
| 1872 | return -1; |
| 1873 | } |
| 1874 | |
| 1875 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
| 1876 | if (!insn) { |
| 1877 | WARN("bad .discard.instr_begin entry"); |
| 1878 | return -1; |
| 1879 | } |
| 1880 | |
| 1881 | insn->instr++; |
| 1882 | } |
| 1883 | |
| 1884 | return 0; |
| 1885 | } |
| 1886 | |
| 1887 | static int read_intra_function_calls(struct objtool_file *file) |
| 1888 | { |
| 1889 | struct instruction *insn; |
| 1890 | struct section *sec; |
| 1891 | struct reloc *reloc; |
| 1892 | |
| 1893 | sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls"); |
| 1894 | if (!sec) |
| 1895 | return 0; |
| 1896 | |
| 1897 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
| 1898 | unsigned long dest_off; |
| 1899 | |
| 1900 | if (reloc->sym->type != STT_SECTION) { |
| 1901 | WARN("unexpected relocation symbol type in %s", |
| 1902 | sec->name); |
| 1903 | return -1; |
| 1904 | } |
| 1905 | |
| 1906 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
| 1907 | if (!insn) { |
| 1908 | WARN("bad .discard.intra_function_call entry"); |
| 1909 | return -1; |
| 1910 | } |
| 1911 | |
| 1912 | if (insn->type != INSN_CALL) { |
| 1913 | WARN_FUNC("intra_function_call not a direct call", |
| 1914 | insn->sec, insn->offset); |
| 1915 | return -1; |
| 1916 | } |
| 1917 | |
| 1918 | /* |
| 1919 | * Treat intra-function CALLs as JMPs, but with a stack_op. |
| 1920 | * See add_call_destinations(), which strips stack_ops from |
| 1921 | * normal CALLs. |
| 1922 | */ |
| 1923 | insn->type = INSN_JUMP_UNCONDITIONAL; |
| 1924 | |
| 1925 | dest_off = insn->offset + insn->len + insn->immediate; |
| 1926 | insn->jump_dest = find_insn(file, insn->sec, dest_off); |
| 1927 | if (!insn->jump_dest) { |
| 1928 | WARN_FUNC("can't find call dest at %s+0x%lx", |
| 1929 | insn->sec, insn->offset, |
| 1930 | insn->sec->name, dest_off); |
| 1931 | return -1; |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | return 0; |
| 1936 | } |
| 1937 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1938 | static int classify_symbols(struct objtool_file *file) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1939 | { |
| 1940 | struct section *sec; |
| 1941 | struct symbol *func; |
| 1942 | |
| 1943 | for_each_sec(file, sec) { |
| 1944 | list_for_each_entry(func, &sec->symbol_list, list) { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1945 | if (func->bind != STB_GLOBAL) |
| 1946 | continue; |
| 1947 | |
| 1948 | if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1949 | strlen(STATIC_CALL_TRAMP_PREFIX_STR))) |
| 1950 | func->static_call_tramp = true; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1951 | |
| 1952 | if (arch_is_retpoline(func)) |
| 1953 | func->retpoline_thunk = true; |
| 1954 | |
| 1955 | if (arch_is_rethunk(func)) |
| 1956 | func->return_thunk = true; |
| 1957 | |
| 1958 | if (!strcmp(func->name, "__fentry__")) |
| 1959 | func->fentry = true; |
| 1960 | |
| 1961 | if (!strncmp(func->name, "__sanitizer_cov_", 16)) |
| 1962 | func->kcov = true; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | return 0; |
| 1967 | } |
| 1968 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1969 | static void mark_rodata(struct objtool_file *file) |
| 1970 | { |
| 1971 | struct section *sec; |
| 1972 | bool found = false; |
| 1973 | |
| 1974 | /* |
| 1975 | * Search for the following rodata sections, each of which can |
| 1976 | * potentially contain jump tables: |
| 1977 | * |
| 1978 | * - .rodata: can contain GCC switch tables |
| 1979 | * - .rodata.<func>: same, if -fdata-sections is being used |
| 1980 | * - .rodata..c_jump_table: contains C annotated jump tables |
| 1981 | * |
| 1982 | * .rodata.str1.* sections are ignored; they don't contain jump tables. |
| 1983 | */ |
| 1984 | for_each_sec(file, sec) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1985 | if (!strncmp(sec->name, ".rodata", 7) && |
| 1986 | !strstr(sec->name, ".str1.")) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1987 | sec->rodata = true; |
| 1988 | found = true; |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | file->rodata = found; |
| 1993 | } |
| 1994 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1995 | static int decode_sections(struct objtool_file *file) |
| 1996 | { |
| 1997 | int ret; |
| 1998 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1999 | mark_rodata(file); |
| 2000 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2001 | ret = decode_instructions(file); |
| 2002 | if (ret) |
| 2003 | return ret; |
| 2004 | |
| 2005 | ret = add_dead_ends(file); |
| 2006 | if (ret) |
| 2007 | return ret; |
| 2008 | |
| 2009 | add_ignores(file); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2010 | add_uaccess_safe(file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2011 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2012 | ret = add_ignore_alternatives(file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2013 | if (ret) |
| 2014 | return ret; |
| 2015 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2016 | /* |
| 2017 | * Must be before add_{jump_call}_destination. |
| 2018 | */ |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2019 | ret = classify_symbols(file); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2020 | if (ret) |
| 2021 | return ret; |
| 2022 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2023 | /* |
| 2024 | * Must be before add_special_section_alts() as that depends on |
| 2025 | * jump_dest being set. |
| 2026 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2027 | ret = add_jump_destinations(file); |
| 2028 | if (ret) |
| 2029 | return ret; |
| 2030 | |
| 2031 | ret = add_special_section_alts(file); |
| 2032 | if (ret) |
| 2033 | return ret; |
| 2034 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2035 | /* |
| 2036 | * Must be before add_call_destination(); it changes INSN_CALL to |
| 2037 | * INSN_JUMP. |
| 2038 | */ |
| 2039 | ret = read_intra_function_calls(file); |
| 2040 | if (ret) |
| 2041 | return ret; |
| 2042 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2043 | ret = add_call_destinations(file); |
| 2044 | if (ret) |
| 2045 | return ret; |
| 2046 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2047 | ret = add_jump_table_alts(file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2048 | if (ret) |
| 2049 | return ret; |
| 2050 | |
| 2051 | ret = read_unwind_hints(file); |
| 2052 | if (ret) |
| 2053 | return ret; |
| 2054 | |
| 2055 | ret = read_retpoline_hints(file); |
| 2056 | if (ret) |
| 2057 | return ret; |
| 2058 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2059 | ret = read_instr_hints(file); |
| 2060 | if (ret) |
| 2061 | return ret; |
| 2062 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2063 | return 0; |
| 2064 | } |
| 2065 | |
| 2066 | static bool is_fentry_call(struct instruction *insn) |
| 2067 | { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2068 | if (insn->type == INSN_CALL && |
| 2069 | insn->call_dest && |
| 2070 | insn->call_dest->fentry) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2071 | return true; |
| 2072 | |
| 2073 | return false; |
| 2074 | } |
| 2075 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2076 | static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2077 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2078 | struct cfi_state *cfi = &state->cfi; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2079 | int i; |
| 2080 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2081 | if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2082 | return true; |
| 2083 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2084 | if (cfi->cfa.offset != initial_func_cfi.cfa.offset) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2085 | return true; |
| 2086 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2087 | if (cfi->stack_size != initial_func_cfi.cfa.offset) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2088 | return true; |
| 2089 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2090 | for (i = 0; i < CFI_NUM_REGS; i++) { |
| 2091 | if (cfi->regs[i].base != initial_func_cfi.regs[i].base || |
| 2092 | cfi->regs[i].offset != initial_func_cfi.regs[i].offset) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2093 | return true; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2094 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2095 | |
| 2096 | return false; |
| 2097 | } |
| 2098 | |
| 2099 | static bool has_valid_stack_frame(struct insn_state *state) |
| 2100 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2101 | struct cfi_state *cfi = &state->cfi; |
| 2102 | |
| 2103 | if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA && |
| 2104 | cfi->regs[CFI_BP].offset == -16) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2105 | return true; |
| 2106 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2107 | if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2108 | return true; |
| 2109 | |
| 2110 | return false; |
| 2111 | } |
| 2112 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2113 | static int update_cfi_state_regs(struct instruction *insn, |
| 2114 | struct cfi_state *cfi, |
| 2115 | struct stack_op *op) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2116 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2117 | struct cfi_reg *cfa = &cfi->cfa; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2118 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2119 | if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2120 | return 0; |
| 2121 | |
| 2122 | /* push */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2123 | if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2124 | cfa->offset += 8; |
| 2125 | |
| 2126 | /* pop */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2127 | if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2128 | cfa->offset -= 8; |
| 2129 | |
| 2130 | /* add immediate to sp */ |
| 2131 | if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD && |
| 2132 | op->dest.reg == CFI_SP && op->src.reg == CFI_SP) |
| 2133 | cfa->offset -= op->src.offset; |
| 2134 | |
| 2135 | return 0; |
| 2136 | } |
| 2137 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2138 | static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2139 | { |
| 2140 | if (arch_callee_saved_reg(reg) && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2141 | cfi->regs[reg].base == CFI_UNDEFINED) { |
| 2142 | cfi->regs[reg].base = base; |
| 2143 | cfi->regs[reg].offset = offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2144 | } |
| 2145 | } |
| 2146 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2147 | static void restore_reg(struct cfi_state *cfi, unsigned char reg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2148 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2149 | cfi->regs[reg].base = initial_func_cfi.regs[reg].base; |
| 2150 | cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | /* |
| 2154 | * A note about DRAP stack alignment: |
| 2155 | * |
| 2156 | * GCC has the concept of a DRAP register, which is used to help keep track of |
| 2157 | * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP |
| 2158 | * register. The typical DRAP pattern is: |
| 2159 | * |
| 2160 | * 4c 8d 54 24 08 lea 0x8(%rsp),%r10 |
| 2161 | * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp |
| 2162 | * 41 ff 72 f8 pushq -0x8(%r10) |
| 2163 | * 55 push %rbp |
| 2164 | * 48 89 e5 mov %rsp,%rbp |
| 2165 | * (more pushes) |
| 2166 | * 41 52 push %r10 |
| 2167 | * ... |
| 2168 | * 41 5a pop %r10 |
| 2169 | * (more pops) |
| 2170 | * 5d pop %rbp |
| 2171 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 2172 | * c3 retq |
| 2173 | * |
| 2174 | * There are some variations in the epilogues, like: |
| 2175 | * |
| 2176 | * 5b pop %rbx |
| 2177 | * 41 5a pop %r10 |
| 2178 | * 41 5c pop %r12 |
| 2179 | * 41 5d pop %r13 |
| 2180 | * 41 5e pop %r14 |
| 2181 | * c9 leaveq |
| 2182 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 2183 | * c3 retq |
| 2184 | * |
| 2185 | * and: |
| 2186 | * |
| 2187 | * 4c 8b 55 e8 mov -0x18(%rbp),%r10 |
| 2188 | * 48 8b 5d e0 mov -0x20(%rbp),%rbx |
| 2189 | * 4c 8b 65 f0 mov -0x10(%rbp),%r12 |
| 2190 | * 4c 8b 6d f8 mov -0x8(%rbp),%r13 |
| 2191 | * c9 leaveq |
| 2192 | * 49 8d 62 f8 lea -0x8(%r10),%rsp |
| 2193 | * c3 retq |
| 2194 | * |
| 2195 | * Sometimes r13 is used as the DRAP register, in which case it's saved and |
| 2196 | * restored beforehand: |
| 2197 | * |
| 2198 | * 41 55 push %r13 |
| 2199 | * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13 |
| 2200 | * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp |
| 2201 | * ... |
| 2202 | * 49 8d 65 f0 lea -0x10(%r13),%rsp |
| 2203 | * 41 5d pop %r13 |
| 2204 | * c3 retq |
| 2205 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2206 | static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi, |
| 2207 | struct stack_op *op) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2208 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2209 | struct cfi_reg *cfa = &cfi->cfa; |
| 2210 | struct cfi_reg *regs = cfi->regs; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2211 | |
| 2212 | /* stack operations don't make sense with an undefined CFA */ |
| 2213 | if (cfa->base == CFI_UNDEFINED) { |
| 2214 | if (insn->func) { |
| 2215 | WARN_FUNC("undefined stack state", insn->sec, insn->offset); |
| 2216 | return -1; |
| 2217 | } |
| 2218 | return 0; |
| 2219 | } |
| 2220 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2221 | if (cfi->type == UNWIND_HINT_TYPE_REGS || |
| 2222 | cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL) |
| 2223 | return update_cfi_state_regs(insn, cfi, op); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2224 | |
| 2225 | switch (op->dest.type) { |
| 2226 | |
| 2227 | case OP_DEST_REG: |
| 2228 | switch (op->src.type) { |
| 2229 | |
| 2230 | case OP_SRC_REG: |
| 2231 | if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP && |
| 2232 | cfa->base == CFI_SP && |
| 2233 | regs[CFI_BP].base == CFI_CFA && |
| 2234 | regs[CFI_BP].offset == -cfa->offset) { |
| 2235 | |
| 2236 | /* mov %rsp, %rbp */ |
| 2237 | cfa->base = op->dest.reg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2238 | cfi->bp_scratch = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | else if (op->src.reg == CFI_SP && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2242 | op->dest.reg == CFI_BP && cfi->drap) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2243 | |
| 2244 | /* drap: mov %rsp, %rbp */ |
| 2245 | regs[CFI_BP].base = CFI_BP; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2246 | regs[CFI_BP].offset = -cfi->stack_size; |
| 2247 | cfi->bp_scratch = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
| 2251 | |
| 2252 | /* |
| 2253 | * mov %rsp, %reg |
| 2254 | * |
| 2255 | * This is needed for the rare case where GCC |
| 2256 | * does: |
| 2257 | * |
| 2258 | * mov %rsp, %rax |
| 2259 | * ... |
| 2260 | * mov %rax, %rsp |
| 2261 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2262 | cfi->vals[op->dest.reg].base = CFI_CFA; |
| 2263 | cfi->vals[op->dest.reg].offset = -cfi->stack_size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP && |
| 2267 | cfa->base == CFI_BP) { |
| 2268 | |
| 2269 | /* |
| 2270 | * mov %rbp, %rsp |
| 2271 | * |
| 2272 | * Restore the original stack pointer (Clang). |
| 2273 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2274 | cfi->stack_size = -cfi->regs[CFI_BP].offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | else if (op->dest.reg == cfa->base) { |
| 2278 | |
| 2279 | /* mov %reg, %rsp */ |
| 2280 | if (cfa->base == CFI_SP && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2281 | cfi->vals[op->src.reg].base == CFI_CFA) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2282 | |
| 2283 | /* |
| 2284 | * This is needed for the rare case |
| 2285 | * where GCC does something dumb like: |
| 2286 | * |
| 2287 | * lea 0x8(%rsp), %rcx |
| 2288 | * ... |
| 2289 | * mov %rcx, %rsp |
| 2290 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2291 | cfa->offset = -cfi->vals[op->src.reg].offset; |
| 2292 | cfi->stack_size = cfa->offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2293 | |
| 2294 | } else { |
| 2295 | cfa->base = CFI_UNDEFINED; |
| 2296 | cfa->offset = 0; |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | break; |
| 2301 | |
| 2302 | case OP_SRC_ADD: |
| 2303 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) { |
| 2304 | |
| 2305 | /* add imm, %rsp */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2306 | cfi->stack_size -= op->src.offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2307 | if (cfa->base == CFI_SP) |
| 2308 | cfa->offset -= op->src.offset; |
| 2309 | break; |
| 2310 | } |
| 2311 | |
| 2312 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) { |
| 2313 | |
| 2314 | /* lea disp(%rbp), %rsp */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2315 | cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2316 | break; |
| 2317 | } |
| 2318 | |
| 2319 | if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
| 2320 | |
| 2321 | /* drap: lea disp(%rsp), %drap */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2322 | cfi->drap_reg = op->dest.reg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2323 | |
| 2324 | /* |
| 2325 | * lea disp(%rsp), %reg |
| 2326 | * |
| 2327 | * This is needed for the rare case where GCC |
| 2328 | * does something dumb like: |
| 2329 | * |
| 2330 | * lea 0x8(%rsp), %rcx |
| 2331 | * ... |
| 2332 | * mov %rcx, %rsp |
| 2333 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2334 | cfi->vals[op->dest.reg].base = CFI_CFA; |
| 2335 | cfi->vals[op->dest.reg].offset = \ |
| 2336 | -cfi->stack_size + op->src.offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2337 | |
| 2338 | break; |
| 2339 | } |
| 2340 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2341 | if (cfi->drap && op->dest.reg == CFI_SP && |
| 2342 | op->src.reg == cfi->drap_reg) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2343 | |
| 2344 | /* drap: lea disp(%drap), %rsp */ |
| 2345 | cfa->base = CFI_SP; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2346 | cfa->offset = cfi->stack_size = -op->src.offset; |
| 2347 | cfi->drap_reg = CFI_UNDEFINED; |
| 2348 | cfi->drap = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2349 | break; |
| 2350 | } |
| 2351 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2352 | if (op->dest.reg == cfi->cfa.base) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2353 | WARN_FUNC("unsupported stack register modification", |
| 2354 | insn->sec, insn->offset); |
| 2355 | return -1; |
| 2356 | } |
| 2357 | |
| 2358 | break; |
| 2359 | |
| 2360 | case OP_SRC_AND: |
| 2361 | if (op->dest.reg != CFI_SP || |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2362 | (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) || |
| 2363 | (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2364 | WARN_FUNC("unsupported stack pointer realignment", |
| 2365 | insn->sec, insn->offset); |
| 2366 | return -1; |
| 2367 | } |
| 2368 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2369 | if (cfi->drap_reg != CFI_UNDEFINED) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2370 | /* drap: and imm, %rsp */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2371 | cfa->base = cfi->drap_reg; |
| 2372 | cfa->offset = cfi->stack_size = 0; |
| 2373 | cfi->drap = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | /* |
| 2377 | * Older versions of GCC (4.8ish) realign the stack |
| 2378 | * without DRAP, with a frame pointer. |
| 2379 | */ |
| 2380 | |
| 2381 | break; |
| 2382 | |
| 2383 | case OP_SRC_POP: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2384 | case OP_SRC_POPF: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2385 | if (!cfi->drap && op->dest.reg == cfa->base) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2386 | |
| 2387 | /* pop %rbp */ |
| 2388 | cfa->base = CFI_SP; |
| 2389 | } |
| 2390 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2391 | if (cfi->drap && cfa->base == CFI_BP_INDIRECT && |
| 2392 | op->dest.reg == cfi->drap_reg && |
| 2393 | cfi->drap_offset == -cfi->stack_size) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2394 | |
| 2395 | /* drap: pop %drap */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2396 | cfa->base = cfi->drap_reg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2397 | cfa->offset = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2398 | cfi->drap_offset = -1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2399 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2400 | } else if (regs[op->dest.reg].offset == -cfi->stack_size) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2401 | |
| 2402 | /* pop %reg */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2403 | restore_reg(cfi, op->dest.reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2406 | cfi->stack_size -= 8; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2407 | if (cfa->base == CFI_SP) |
| 2408 | cfa->offset -= 8; |
| 2409 | |
| 2410 | break; |
| 2411 | |
| 2412 | case OP_SRC_REG_INDIRECT: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2413 | if (cfi->drap && op->src.reg == CFI_BP && |
| 2414 | op->src.offset == cfi->drap_offset) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2415 | |
| 2416 | /* drap: mov disp(%rbp), %drap */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2417 | cfa->base = cfi->drap_reg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2418 | cfa->offset = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2419 | cfi->drap_offset = -1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2422 | if (cfi->drap && op->src.reg == CFI_BP && |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2423 | op->src.offset == regs[op->dest.reg].offset) { |
| 2424 | |
| 2425 | /* drap: mov disp(%rbp), %reg */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2426 | restore_reg(cfi, op->dest.reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2427 | |
| 2428 | } else if (op->src.reg == cfa->base && |
| 2429 | op->src.offset == regs[op->dest.reg].offset + cfa->offset) { |
| 2430 | |
| 2431 | /* mov disp(%rbp), %reg */ |
| 2432 | /* mov disp(%rsp), %reg */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2433 | restore_reg(cfi, op->dest.reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | break; |
| 2437 | |
| 2438 | default: |
| 2439 | WARN_FUNC("unknown stack-related instruction", |
| 2440 | insn->sec, insn->offset); |
| 2441 | return -1; |
| 2442 | } |
| 2443 | |
| 2444 | break; |
| 2445 | |
| 2446 | case OP_DEST_PUSH: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2447 | case OP_DEST_PUSHF: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2448 | cfi->stack_size += 8; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2449 | if (cfa->base == CFI_SP) |
| 2450 | cfa->offset += 8; |
| 2451 | |
| 2452 | if (op->src.type != OP_SRC_REG) |
| 2453 | break; |
| 2454 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2455 | if (cfi->drap) { |
| 2456 | if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2457 | |
| 2458 | /* drap: push %drap */ |
| 2459 | cfa->base = CFI_BP_INDIRECT; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2460 | cfa->offset = -cfi->stack_size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2461 | |
| 2462 | /* save drap so we know when to restore it */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2463 | cfi->drap_offset = -cfi->stack_size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2464 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2465 | } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2466 | |
| 2467 | /* drap: push %rbp */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2468 | cfi->stack_size = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2469 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2470 | } else { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2471 | |
| 2472 | /* drap: push %reg */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2473 | save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | } else { |
| 2477 | |
| 2478 | /* push %reg */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2479 | save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | /* detect when asm code uses rbp as a scratch register */ |
| 2483 | if (!no_fp && insn->func && op->src.reg == CFI_BP && |
| 2484 | cfa->base != CFI_BP) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2485 | cfi->bp_scratch = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2486 | break; |
| 2487 | |
| 2488 | case OP_DEST_REG_INDIRECT: |
| 2489 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2490 | if (cfi->drap) { |
| 2491 | if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2492 | |
| 2493 | /* drap: mov %drap, disp(%rbp) */ |
| 2494 | cfa->base = CFI_BP_INDIRECT; |
| 2495 | cfa->offset = op->dest.offset; |
| 2496 | |
| 2497 | /* save drap offset so we know when to restore it */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2498 | cfi->drap_offset = op->dest.offset; |
| 2499 | } else { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2500 | |
| 2501 | /* drap: mov reg, disp(%rbp) */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2502 | save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | } else if (op->dest.reg == cfa->base) { |
| 2506 | |
| 2507 | /* mov reg, disp(%rbp) */ |
| 2508 | /* mov reg, disp(%rsp) */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2509 | save_reg(cfi, op->src.reg, CFI_CFA, |
| 2510 | op->dest.offset - cfi->cfa.offset); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | break; |
| 2514 | |
| 2515 | case OP_DEST_LEAVE: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2516 | if ((!cfi->drap && cfa->base != CFI_BP) || |
| 2517 | (cfi->drap && cfa->base != cfi->drap_reg)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2518 | WARN_FUNC("leave instruction with modified stack frame", |
| 2519 | insn->sec, insn->offset); |
| 2520 | return -1; |
| 2521 | } |
| 2522 | |
| 2523 | /* leave (mov %rbp, %rsp; pop %rbp) */ |
| 2524 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2525 | cfi->stack_size = -cfi->regs[CFI_BP].offset - 8; |
| 2526 | restore_reg(cfi, CFI_BP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2527 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2528 | if (!cfi->drap) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2529 | cfa->base = CFI_SP; |
| 2530 | cfa->offset -= 8; |
| 2531 | } |
| 2532 | |
| 2533 | break; |
| 2534 | |
| 2535 | case OP_DEST_MEM: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2536 | if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2537 | WARN_FUNC("unknown stack-related memory operation", |
| 2538 | insn->sec, insn->offset); |
| 2539 | return -1; |
| 2540 | } |
| 2541 | |
| 2542 | /* pop mem */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2543 | cfi->stack_size -= 8; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2544 | if (cfa->base == CFI_SP) |
| 2545 | cfa->offset -= 8; |
| 2546 | |
| 2547 | break; |
| 2548 | |
| 2549 | default: |
| 2550 | WARN_FUNC("unknown stack-related instruction", |
| 2551 | insn->sec, insn->offset); |
| 2552 | return -1; |
| 2553 | } |
| 2554 | |
| 2555 | return 0; |
| 2556 | } |
| 2557 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2558 | /* |
| 2559 | * The stack layouts of alternatives instructions can sometimes diverge when |
| 2560 | * they have stack modifications. That's fine as long as the potential stack |
| 2561 | * layouts don't conflict at any given potential instruction boundary. |
| 2562 | * |
| 2563 | * Flatten the CFIs of the different alternative code streams (both original |
| 2564 | * and replacement) into a single shared CFI array which can be used to detect |
| 2565 | * conflicts and nicely feed a linear array of ORC entries to the unwinder. |
| 2566 | */ |
| 2567 | static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn) |
| 2568 | { |
| 2569 | struct cfi_state **alt_cfi; |
| 2570 | int group_off; |
| 2571 | |
| 2572 | if (!insn->alt_group) |
| 2573 | return 0; |
| 2574 | |
| 2575 | if (!insn->cfi) { |
| 2576 | WARN("CFI missing"); |
| 2577 | return -1; |
| 2578 | } |
| 2579 | |
| 2580 | alt_cfi = insn->alt_group->cfi; |
| 2581 | group_off = insn->offset - insn->alt_group->first_insn->offset; |
| 2582 | |
| 2583 | if (!alt_cfi[group_off]) { |
| 2584 | alt_cfi[group_off] = insn->cfi; |
| 2585 | } else { |
| 2586 | if (cficmp(alt_cfi[group_off], insn->cfi)) { |
| 2587 | WARN_FUNC("stack layout conflict in alternatives", |
| 2588 | insn->sec, insn->offset); |
| 2589 | return -1; |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | return 0; |
| 2594 | } |
| 2595 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2596 | static int handle_insn_ops(struct instruction *insn, struct insn_state *state) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2597 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2598 | struct stack_op *op; |
| 2599 | |
| 2600 | list_for_each_entry(op, &insn->stack_ops, list) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2601 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2602 | if (update_cfi_state(insn, &state->cfi, op)) |
| 2603 | return 1; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2604 | |
| 2605 | if (op->dest.type == OP_DEST_PUSHF) { |
| 2606 | if (!state->uaccess_stack) { |
| 2607 | state->uaccess_stack = 1; |
| 2608 | } else if (state->uaccess_stack >> 31) { |
| 2609 | WARN_FUNC("PUSHF stack exhausted", |
| 2610 | insn->sec, insn->offset); |
| 2611 | return 1; |
| 2612 | } |
| 2613 | state->uaccess_stack <<= 1; |
| 2614 | state->uaccess_stack |= state->uaccess; |
| 2615 | } |
| 2616 | |
| 2617 | if (op->src.type == OP_SRC_POPF) { |
| 2618 | if (state->uaccess_stack) { |
| 2619 | state->uaccess = state->uaccess_stack & 1; |
| 2620 | state->uaccess_stack >>= 1; |
| 2621 | if (state->uaccess_stack == 1) |
| 2622 | state->uaccess_stack = 0; |
| 2623 | } |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | return 0; |
| 2628 | } |
| 2629 | |
| 2630 | static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2) |
| 2631 | { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2632 | struct cfi_state *cfi1 = insn->cfi; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2633 | int i; |
| 2634 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2635 | if (!cfi1) { |
| 2636 | WARN("CFI missing"); |
| 2637 | return false; |
| 2638 | } |
| 2639 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2640 | if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) { |
| 2641 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2642 | WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d", |
| 2643 | insn->sec, insn->offset, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2644 | cfi1->cfa.base, cfi1->cfa.offset, |
| 2645 | cfi2->cfa.base, cfi2->cfa.offset); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2646 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2647 | } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2648 | for (i = 0; i < CFI_NUM_REGS; i++) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2649 | if (!memcmp(&cfi1->regs[i], &cfi2->regs[i], |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2650 | sizeof(struct cfi_reg))) |
| 2651 | continue; |
| 2652 | |
| 2653 | WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d", |
| 2654 | insn->sec, insn->offset, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2655 | i, cfi1->regs[i].base, cfi1->regs[i].offset, |
| 2656 | i, cfi2->regs[i].base, cfi2->regs[i].offset); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2657 | break; |
| 2658 | } |
| 2659 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2660 | } else if (cfi1->type != cfi2->type) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2661 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2662 | WARN_FUNC("stack state mismatch: type1=%d type2=%d", |
| 2663 | insn->sec, insn->offset, cfi1->type, cfi2->type); |
| 2664 | |
| 2665 | } else if (cfi1->drap != cfi2->drap || |
| 2666 | (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) || |
| 2667 | (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) { |
| 2668 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2669 | WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)", |
| 2670 | insn->sec, insn->offset, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2671 | cfi1->drap, cfi1->drap_reg, cfi1->drap_offset, |
| 2672 | cfi2->drap, cfi2->drap_reg, cfi2->drap_offset); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2673 | |
| 2674 | } else |
| 2675 | return true; |
| 2676 | |
| 2677 | return false; |
| 2678 | } |
| 2679 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2680 | static inline bool func_uaccess_safe(struct symbol *func) |
| 2681 | { |
| 2682 | if (func) |
| 2683 | return func->uaccess_safe; |
| 2684 | |
| 2685 | return false; |
| 2686 | } |
| 2687 | |
| 2688 | static inline const char *call_dest_name(struct instruction *insn) |
| 2689 | { |
| 2690 | if (insn->call_dest) |
| 2691 | return insn->call_dest->name; |
| 2692 | |
| 2693 | return "{dynamic}"; |
| 2694 | } |
| 2695 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2696 | static inline bool noinstr_call_dest(struct symbol *func) |
| 2697 | { |
| 2698 | /* |
| 2699 | * We can't deal with indirect function calls at present; |
| 2700 | * assume they're instrumented. |
| 2701 | */ |
| 2702 | if (!func) |
| 2703 | return false; |
| 2704 | |
| 2705 | /* |
| 2706 | * If the symbol is from a noinstr section; we good. |
| 2707 | */ |
| 2708 | if (func->sec->noinstr) |
| 2709 | return true; |
| 2710 | |
| 2711 | /* |
| 2712 | * The __ubsan_handle_*() calls are like WARN(), they only happen when |
| 2713 | * something 'BAD' happened. At the risk of taking the machine down, |
| 2714 | * let them proceed to get the message out. |
| 2715 | */ |
| 2716 | if (!strncmp(func->name, "__ubsan_handle_", 15)) |
| 2717 | return true; |
| 2718 | |
| 2719 | return false; |
| 2720 | } |
| 2721 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2722 | static int validate_call(struct instruction *insn, struct insn_state *state) |
| 2723 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2724 | if (state->noinstr && state->instr <= 0 && |
| 2725 | !noinstr_call_dest(insn->call_dest)) { |
| 2726 | WARN_FUNC("call to %s() leaves .noinstr.text section", |
| 2727 | insn->sec, insn->offset, call_dest_name(insn)); |
| 2728 | return 1; |
| 2729 | } |
| 2730 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2731 | if (state->uaccess && !func_uaccess_safe(insn->call_dest)) { |
| 2732 | WARN_FUNC("call to %s() with UACCESS enabled", |
| 2733 | insn->sec, insn->offset, call_dest_name(insn)); |
| 2734 | return 1; |
| 2735 | } |
| 2736 | |
| 2737 | if (state->df) { |
| 2738 | WARN_FUNC("call to %s() with DF set", |
| 2739 | insn->sec, insn->offset, call_dest_name(insn)); |
| 2740 | return 1; |
| 2741 | } |
| 2742 | |
| 2743 | return 0; |
| 2744 | } |
| 2745 | |
| 2746 | static int validate_sibling_call(struct instruction *insn, struct insn_state *state) |
| 2747 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2748 | if (has_modified_stack_frame(insn, state)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2749 | WARN_FUNC("sibling call from callable instruction with modified stack frame", |
| 2750 | insn->sec, insn->offset); |
| 2751 | return 1; |
| 2752 | } |
| 2753 | |
| 2754 | return validate_call(insn, state); |
| 2755 | } |
| 2756 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2757 | static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state) |
| 2758 | { |
| 2759 | if (state->noinstr && state->instr > 0) { |
| 2760 | WARN_FUNC("return with instrumentation enabled", |
| 2761 | insn->sec, insn->offset); |
| 2762 | return 1; |
| 2763 | } |
| 2764 | |
| 2765 | if (state->uaccess && !func_uaccess_safe(func)) { |
| 2766 | WARN_FUNC("return with UACCESS enabled", |
| 2767 | insn->sec, insn->offset); |
| 2768 | return 1; |
| 2769 | } |
| 2770 | |
| 2771 | if (!state->uaccess && func_uaccess_safe(func)) { |
| 2772 | WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", |
| 2773 | insn->sec, insn->offset); |
| 2774 | return 1; |
| 2775 | } |
| 2776 | |
| 2777 | if (state->df) { |
| 2778 | WARN_FUNC("return with DF set", |
| 2779 | insn->sec, insn->offset); |
| 2780 | return 1; |
| 2781 | } |
| 2782 | |
| 2783 | if (func && has_modified_stack_frame(insn, state)) { |
| 2784 | WARN_FUNC("return with modified stack frame", |
| 2785 | insn->sec, insn->offset); |
| 2786 | return 1; |
| 2787 | } |
| 2788 | |
| 2789 | if (state->cfi.bp_scratch) { |
| 2790 | WARN_FUNC("BP used as a scratch register", |
| 2791 | insn->sec, insn->offset); |
| 2792 | return 1; |
| 2793 | } |
| 2794 | |
| 2795 | return 0; |
| 2796 | } |
| 2797 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2798 | static struct instruction *next_insn_to_validate(struct objtool_file *file, |
| 2799 | struct instruction *insn) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2800 | { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2801 | struct alt_group *alt_group = insn->alt_group; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2802 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2803 | /* |
| 2804 | * Simulate the fact that alternatives are patched in-place. When the |
| 2805 | * end of a replacement alt_group is reached, redirect objtool flow to |
| 2806 | * the end of the original alt_group. |
| 2807 | */ |
| 2808 | if (alt_group && insn == alt_group->last_insn && alt_group->orig_group) |
| 2809 | return next_insn_same_sec(file, alt_group->orig_group->last_insn); |
| 2810 | |
| 2811 | return next_insn_same_sec(file, insn); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2812 | } |
| 2813 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2814 | /* |
| 2815 | * Follow the branch starting at the given instruction, and recursively follow |
| 2816 | * any other branches (jumps). Meanwhile, track the frame pointer state at |
| 2817 | * each instruction and validate all the rules described in |
| 2818 | * tools/objtool/Documentation/stack-validation.txt. |
| 2819 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2820 | static int validate_branch(struct objtool_file *file, struct symbol *func, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2821 | struct instruction *insn, struct insn_state state) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2822 | { |
| 2823 | struct alternative *alt; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2824 | struct instruction *next_insn, *prev_insn = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2825 | struct section *sec; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2826 | u8 visited; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2827 | int ret; |
| 2828 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2829 | sec = insn->sec; |
| 2830 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2831 | while (1) { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2832 | next_insn = next_insn_to_validate(file, insn); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2833 | |
| 2834 | if (file->c_file && func && insn->func && func != insn->func->pfunc) { |
| 2835 | WARN("%s() falls through to next function %s()", |
| 2836 | func->name, insn->func->name); |
| 2837 | return 1; |
| 2838 | } |
| 2839 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2840 | if (func && insn->ignore) { |
| 2841 | WARN_FUNC("BUG: why am I validating an ignored function?", |
| 2842 | sec, insn->offset); |
| 2843 | return 1; |
| 2844 | } |
| 2845 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2846 | visited = VISITED_BRANCH << state.uaccess; |
| 2847 | if (insn->visited & VISITED_BRANCH_MASK) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2848 | if (!insn->hint && !insn_cfi_match(insn, &state.cfi)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2849 | return 1; |
| 2850 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2851 | if (insn->visited & visited) |
| 2852 | return 0; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2853 | } else { |
| 2854 | nr_insns_visited++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2857 | if (state.noinstr) |
| 2858 | state.instr += insn->instr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2859 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2860 | if (insn->hint) { |
| 2861 | if (insn->restore) { |
| 2862 | struct instruction *save_insn, *i; |
| 2863 | |
| 2864 | i = insn; |
| 2865 | save_insn = NULL; |
| 2866 | |
| 2867 | sym_for_each_insn_continue_reverse(file, func, i) { |
| 2868 | if (i->save) { |
| 2869 | save_insn = i; |
| 2870 | break; |
| 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | if (!save_insn) { |
| 2875 | WARN_FUNC("no corresponding CFI save for CFI restore", |
| 2876 | sec, insn->offset); |
| 2877 | return 1; |
| 2878 | } |
| 2879 | |
| 2880 | if (!save_insn->visited) { |
| 2881 | WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo", |
| 2882 | sec, insn->offset); |
| 2883 | return 1; |
| 2884 | } |
| 2885 | |
| 2886 | insn->cfi = save_insn->cfi; |
| 2887 | nr_cfi_reused++; |
| 2888 | } |
| 2889 | |
| 2890 | state.cfi = *insn->cfi; |
| 2891 | } else { |
| 2892 | /* XXX track if we actually changed state.cfi */ |
| 2893 | |
| 2894 | if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) { |
| 2895 | insn->cfi = prev_insn->cfi; |
| 2896 | nr_cfi_reused++; |
| 2897 | } else { |
| 2898 | insn->cfi = cfi_hash_find_or_add(&state.cfi); |
| 2899 | } |
| 2900 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2901 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2902 | insn->visited |= visited; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2903 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2904 | if (propagate_alt_cfi(file, insn)) |
| 2905 | return 1; |
| 2906 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2907 | if (!insn->ignore_alts && !list_empty(&insn->alts)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2908 | bool skip_orig = false; |
| 2909 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2910 | list_for_each_entry(alt, &insn->alts, list) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2911 | if (alt->skip_orig) |
| 2912 | skip_orig = true; |
| 2913 | |
| 2914 | ret = validate_branch(file, func, alt->insn, state); |
| 2915 | if (ret) { |
| 2916 | if (backtrace) |
| 2917 | BT_FUNC("(alt)", insn); |
| 2918 | return ret; |
| 2919 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2920 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2921 | |
| 2922 | if (skip_orig) |
| 2923 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2926 | if (handle_insn_ops(insn, &state)) |
| 2927 | return 1; |
| 2928 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2929 | switch (insn->type) { |
| 2930 | |
| 2931 | case INSN_RETURN: |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2932 | if (sls && !insn->retpoline_safe && |
| 2933 | next_insn && next_insn->type != INSN_TRAP) { |
| 2934 | WARN_FUNC("missing int3 after ret", |
| 2935 | insn->sec, insn->offset); |
| 2936 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2937 | return validate_return(func, insn, &state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2938 | |
| 2939 | case INSN_CALL: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2940 | case INSN_CALL_DYNAMIC: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2941 | ret = validate_call(insn, &state); |
| 2942 | if (ret) |
| 2943 | return ret; |
| 2944 | |
| 2945 | if (!no_fp && func && !is_fentry_call(insn) && |
| 2946 | !has_valid_stack_frame(&state)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2947 | WARN_FUNC("call without frame pointer save/setup", |
| 2948 | sec, insn->offset); |
| 2949 | return 1; |
| 2950 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2951 | |
| 2952 | if (dead_end_function(file, insn->call_dest)) |
| 2953 | return 0; |
| 2954 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2955 | break; |
| 2956 | |
| 2957 | case INSN_JUMP_CONDITIONAL: |
| 2958 | case INSN_JUMP_UNCONDITIONAL: |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2959 | if (is_sibling_call(insn)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2960 | ret = validate_sibling_call(insn, &state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2961 | if (ret) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2962 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2963 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2964 | } else if (insn->jump_dest) { |
| 2965 | ret = validate_branch(file, func, |
| 2966 | insn->jump_dest, state); |
| 2967 | if (ret) { |
| 2968 | if (backtrace) |
| 2969 | BT_FUNC("(branch)", insn); |
| 2970 | return ret; |
| 2971 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | if (insn->type == INSN_JUMP_UNCONDITIONAL) |
| 2975 | return 0; |
| 2976 | |
| 2977 | break; |
| 2978 | |
| 2979 | case INSN_JUMP_DYNAMIC: |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2980 | if (sls && !insn->retpoline_safe && |
| 2981 | next_insn && next_insn->type != INSN_TRAP) { |
| 2982 | WARN_FUNC("missing int3 after indirect jump", |
| 2983 | insn->sec, insn->offset); |
| 2984 | } |
| 2985 | |
| 2986 | /* fallthrough */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2987 | case INSN_JUMP_DYNAMIC_CONDITIONAL: |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 2988 | if (is_sibling_call(insn)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2989 | ret = validate_sibling_call(insn, &state); |
| 2990 | if (ret) |
| 2991 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2994 | if (insn->type == INSN_JUMP_DYNAMIC) |
| 2995 | return 0; |
| 2996 | |
| 2997 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2998 | |
| 2999 | case INSN_CONTEXT_SWITCH: |
| 3000 | if (func && (!next_insn || !next_insn->hint)) { |
| 3001 | WARN_FUNC("unsupported instruction in callable function", |
| 3002 | sec, insn->offset); |
| 3003 | return 1; |
| 3004 | } |
| 3005 | return 0; |
| 3006 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3007 | case INSN_STAC: |
| 3008 | if (state.uaccess) { |
| 3009 | WARN_FUNC("recursive UACCESS enable", sec, insn->offset); |
| 3010 | return 1; |
| 3011 | } |
| 3012 | |
| 3013 | state.uaccess = true; |
| 3014 | break; |
| 3015 | |
| 3016 | case INSN_CLAC: |
| 3017 | if (!state.uaccess && func) { |
| 3018 | WARN_FUNC("redundant UACCESS disable", sec, insn->offset); |
| 3019 | return 1; |
| 3020 | } |
| 3021 | |
| 3022 | if (func_uaccess_safe(func) && !state.uaccess_stack) { |
| 3023 | WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset); |
| 3024 | return 1; |
| 3025 | } |
| 3026 | |
| 3027 | state.uaccess = false; |
| 3028 | break; |
| 3029 | |
| 3030 | case INSN_STD: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3031 | if (state.df) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3032 | WARN_FUNC("recursive STD", sec, insn->offset); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3033 | return 1; |
| 3034 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3035 | |
| 3036 | state.df = true; |
| 3037 | break; |
| 3038 | |
| 3039 | case INSN_CLD: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3040 | if (!state.df && func) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3041 | WARN_FUNC("redundant CLD", sec, insn->offset); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3042 | return 1; |
| 3043 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3044 | |
| 3045 | state.df = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3046 | break; |
| 3047 | |
| 3048 | default: |
| 3049 | break; |
| 3050 | } |
| 3051 | |
| 3052 | if (insn->dead_end) |
| 3053 | return 0; |
| 3054 | |
| 3055 | if (!next_insn) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3056 | if (state.cfi.cfa.base == CFI_UNDEFINED) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3057 | return 0; |
| 3058 | WARN("%s: unexpected end of section", sec->name); |
| 3059 | return 1; |
| 3060 | } |
| 3061 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 3062 | prev_insn = insn; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3063 | insn = next_insn; |
| 3064 | } |
| 3065 | |
| 3066 | return 0; |
| 3067 | } |
| 3068 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3069 | static int validate_unwind_hints(struct objtool_file *file, struct section *sec) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3070 | { |
| 3071 | struct instruction *insn; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3072 | struct insn_state state; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3073 | int ret, warnings = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3074 | |
| 3075 | if (!file->hints) |
| 3076 | return 0; |
| 3077 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3078 | init_insn_state(&state, sec); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3079 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3080 | if (sec) { |
| 3081 | insn = find_insn(file, sec, 0); |
| 3082 | if (!insn) |
| 3083 | return 0; |
| 3084 | } else { |
| 3085 | insn = list_first_entry(&file->insn_list, typeof(*insn), list); |
| 3086 | } |
| 3087 | |
| 3088 | while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3089 | if (insn->hint && !insn->visited) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3090 | ret = validate_branch(file, insn->func, insn, state); |
| 3091 | if (ret && backtrace) |
| 3092 | BT_FUNC("<=== (hint)", insn); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3093 | warnings += ret; |
| 3094 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3095 | |
| 3096 | insn = list_next_entry(insn, list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | return warnings; |
| 3100 | } |
| 3101 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 3102 | /* |
| 3103 | * Validate rethunk entry constraint: must untrain RET before the first RET. |
| 3104 | * |
| 3105 | * Follow every branch (intra-function) and ensure ANNOTATE_UNRET_END comes |
| 3106 | * before an actual RET instruction. |
| 3107 | */ |
| 3108 | static int validate_entry(struct objtool_file *file, struct instruction *insn) |
| 3109 | { |
| 3110 | struct instruction *next, *dest; |
| 3111 | int ret, warnings = 0; |
| 3112 | |
| 3113 | for (;;) { |
| 3114 | next = next_insn_to_validate(file, insn); |
| 3115 | |
| 3116 | if (insn->visited & VISITED_ENTRY) |
| 3117 | return 0; |
| 3118 | |
| 3119 | insn->visited |= VISITED_ENTRY; |
| 3120 | |
| 3121 | if (!insn->ignore_alts && !list_empty(&insn->alts)) { |
| 3122 | struct alternative *alt; |
| 3123 | bool skip_orig = false; |
| 3124 | |
| 3125 | list_for_each_entry(alt, &insn->alts, list) { |
| 3126 | if (alt->skip_orig) |
| 3127 | skip_orig = true; |
| 3128 | |
| 3129 | ret = validate_entry(file, alt->insn); |
| 3130 | if (ret) { |
| 3131 | if (backtrace) |
| 3132 | BT_FUNC("(alt)", insn); |
| 3133 | return ret; |
| 3134 | } |
| 3135 | } |
| 3136 | |
| 3137 | if (skip_orig) |
| 3138 | return 0; |
| 3139 | } |
| 3140 | |
| 3141 | switch (insn->type) { |
| 3142 | |
| 3143 | case INSN_CALL_DYNAMIC: |
| 3144 | case INSN_JUMP_DYNAMIC: |
| 3145 | case INSN_JUMP_DYNAMIC_CONDITIONAL: |
| 3146 | WARN_FUNC("early indirect call", insn->sec, insn->offset); |
| 3147 | return 1; |
| 3148 | |
| 3149 | case INSN_JUMP_UNCONDITIONAL: |
| 3150 | case INSN_JUMP_CONDITIONAL: |
| 3151 | if (!is_sibling_call(insn)) { |
| 3152 | if (!insn->jump_dest) { |
| 3153 | WARN_FUNC("unresolved jump target after linking?!?", |
| 3154 | insn->sec, insn->offset); |
| 3155 | return -1; |
| 3156 | } |
| 3157 | ret = validate_entry(file, insn->jump_dest); |
| 3158 | if (ret) { |
| 3159 | if (backtrace) { |
| 3160 | BT_FUNC("(branch%s)", insn, |
| 3161 | insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : ""); |
| 3162 | } |
| 3163 | return ret; |
| 3164 | } |
| 3165 | |
| 3166 | if (insn->type == INSN_JUMP_UNCONDITIONAL) |
| 3167 | return 0; |
| 3168 | |
| 3169 | break; |
| 3170 | } |
| 3171 | |
| 3172 | /* fallthrough */ |
| 3173 | case INSN_CALL: |
| 3174 | dest = find_insn(file, insn->call_dest->sec, |
| 3175 | insn->call_dest->offset); |
| 3176 | if (!dest) { |
| 3177 | WARN("Unresolved function after linking!?: %s", |
| 3178 | insn->call_dest->name); |
| 3179 | return -1; |
| 3180 | } |
| 3181 | |
| 3182 | ret = validate_entry(file, dest); |
| 3183 | if (ret) { |
| 3184 | if (backtrace) |
| 3185 | BT_FUNC("(call)", insn); |
| 3186 | return ret; |
| 3187 | } |
| 3188 | /* |
| 3189 | * If a call returns without error, it must have seen UNTRAIN_RET. |
| 3190 | * Therefore any non-error return is a success. |
| 3191 | */ |
| 3192 | return 0; |
| 3193 | |
| 3194 | case INSN_RETURN: |
| 3195 | WARN_FUNC("RET before UNTRAIN", insn->sec, insn->offset); |
| 3196 | return 1; |
| 3197 | |
| 3198 | case INSN_NOP: |
| 3199 | if (insn->retpoline_safe) |
| 3200 | return 0; |
| 3201 | break; |
| 3202 | |
| 3203 | default: |
| 3204 | break; |
| 3205 | } |
| 3206 | |
| 3207 | if (!next) { |
| 3208 | WARN_FUNC("teh end!", insn->sec, insn->offset); |
| 3209 | return -1; |
| 3210 | } |
| 3211 | insn = next; |
| 3212 | } |
| 3213 | |
| 3214 | return warnings; |
| 3215 | } |
| 3216 | |
| 3217 | /* |
| 3218 | * Validate that all branches starting at 'insn->entry' encounter UNRET_END |
| 3219 | * before RET. |
| 3220 | */ |
| 3221 | static int validate_unret(struct objtool_file *file) |
| 3222 | { |
| 3223 | struct instruction *insn; |
| 3224 | int ret, warnings = 0; |
| 3225 | |
| 3226 | for_each_insn(file, insn) { |
| 3227 | if (!insn->entry) |
| 3228 | continue; |
| 3229 | |
| 3230 | ret = validate_entry(file, insn); |
| 3231 | if (ret < 0) { |
| 3232 | WARN_FUNC("Failed UNRET validation", insn->sec, insn->offset); |
| 3233 | return ret; |
| 3234 | } |
| 3235 | warnings += ret; |
| 3236 | } |
| 3237 | |
| 3238 | return warnings; |
| 3239 | } |
| 3240 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3241 | static int validate_retpoline(struct objtool_file *file) |
| 3242 | { |
| 3243 | struct instruction *insn; |
| 3244 | int warnings = 0; |
| 3245 | |
| 3246 | for_each_insn(file, insn) { |
| 3247 | if (insn->type != INSN_JUMP_DYNAMIC && |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 3248 | insn->type != INSN_CALL_DYNAMIC && |
| 3249 | insn->type != INSN_RETURN) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3250 | continue; |
| 3251 | |
| 3252 | if (insn->retpoline_safe) |
| 3253 | continue; |
| 3254 | |
| 3255 | /* |
| 3256 | * .init.text code is ran before userspace and thus doesn't |
| 3257 | * strictly need retpolines, except for modules which are |
| 3258 | * loaded late, they very much do need retpoline in their |
| 3259 | * .init.text |
| 3260 | */ |
| 3261 | if (!strcmp(insn->sec->name, ".init.text") && !module) |
| 3262 | continue; |
| 3263 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 3264 | if (insn->type == INSN_RETURN) { |
| 3265 | if (rethunk) { |
| 3266 | WARN_FUNC("'naked' return found in RETHUNK build", |
| 3267 | insn->sec, insn->offset); |
| 3268 | } else |
| 3269 | continue; |
| 3270 | } else { |
| 3271 | WARN_FUNC("indirect %s found in RETPOLINE build", |
| 3272 | insn->sec, insn->offset, |
| 3273 | insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call"); |
| 3274 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3275 | |
| 3276 | warnings++; |
| 3277 | } |
| 3278 | |
| 3279 | return warnings; |
| 3280 | } |
| 3281 | |
| 3282 | static bool is_kasan_insn(struct instruction *insn) |
| 3283 | { |
| 3284 | return (insn->type == INSN_CALL && |
| 3285 | !strcmp(insn->call_dest->name, "__asan_handle_no_return")); |
| 3286 | } |
| 3287 | |
| 3288 | static bool is_ubsan_insn(struct instruction *insn) |
| 3289 | { |
| 3290 | return (insn->type == INSN_CALL && |
| 3291 | !strcmp(insn->call_dest->name, |
| 3292 | "__ubsan_handle_builtin_unreachable")); |
| 3293 | } |
| 3294 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3295 | static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3296 | { |
| 3297 | int i; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3298 | struct instruction *prev_insn; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3299 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 3300 | if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3301 | return true; |
| 3302 | |
| 3303 | /* |
| 3304 | * Ignore any unused exceptions. This can happen when a whitelisted |
| 3305 | * function has an exception table entry. |
| 3306 | * |
| 3307 | * Also ignore alternative replacement instructions. This can happen |
| 3308 | * when a whitelisted function uses one of the ALTERNATIVE macros. |
| 3309 | */ |
| 3310 | if (!strcmp(insn->sec->name, ".fixup") || |
| 3311 | !strcmp(insn->sec->name, ".altinstr_replacement") || |
| 3312 | !strcmp(insn->sec->name, ".altinstr_aux")) |
| 3313 | return true; |
| 3314 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3315 | if (!insn->func) |
| 3316 | return false; |
| 3317 | |
| 3318 | /* |
| 3319 | * CONFIG_UBSAN_TRAP inserts a UD2 when it sees |
| 3320 | * __builtin_unreachable(). The BUG() macro has an unreachable() after |
| 3321 | * the UD2, which causes GCC's undefined trap logic to emit another UD2 |
| 3322 | * (or occasionally a JMP to UD2). |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3323 | * |
| 3324 | * It may also insert a UD2 after calling a __noreturn function. |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3325 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3326 | prev_insn = list_prev_entry(insn, list); |
| 3327 | if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) && |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3328 | (insn->type == INSN_BUG || |
| 3329 | (insn->type == INSN_JUMP_UNCONDITIONAL && |
| 3330 | insn->jump_dest && insn->jump_dest->type == INSN_BUG))) |
| 3331 | return true; |
| 3332 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3333 | /* |
| 3334 | * Check if this (or a subsequent) instruction is related to |
| 3335 | * CONFIG_UBSAN or CONFIG_KASAN. |
| 3336 | * |
| 3337 | * End the search at 5 instructions to avoid going into the weeds. |
| 3338 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3339 | for (i = 0; i < 5; i++) { |
| 3340 | |
| 3341 | if (is_kasan_insn(insn) || is_ubsan_insn(insn)) |
| 3342 | return true; |
| 3343 | |
| 3344 | if (insn->type == INSN_JUMP_UNCONDITIONAL) { |
| 3345 | if (insn->jump_dest && |
| 3346 | insn->jump_dest->func == insn->func) { |
| 3347 | insn = insn->jump_dest; |
| 3348 | continue; |
| 3349 | } |
| 3350 | |
| 3351 | break; |
| 3352 | } |
| 3353 | |
| 3354 | if (insn->offset + insn->len >= insn->func->offset + insn->func->len) |
| 3355 | break; |
| 3356 | |
| 3357 | insn = list_next_entry(insn, list); |
| 3358 | } |
| 3359 | |
| 3360 | return false; |
| 3361 | } |
| 3362 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3363 | static int validate_symbol(struct objtool_file *file, struct section *sec, |
| 3364 | struct symbol *sym, struct insn_state *state) |
| 3365 | { |
| 3366 | struct instruction *insn; |
| 3367 | int ret; |
| 3368 | |
| 3369 | if (!sym->len) { |
| 3370 | WARN("%s() is missing an ELF size annotation", sym->name); |
| 3371 | return 1; |
| 3372 | } |
| 3373 | |
| 3374 | if (sym->pfunc != sym || sym->alias != sym) |
| 3375 | return 0; |
| 3376 | |
| 3377 | insn = find_insn(file, sec, sym->offset); |
| 3378 | if (!insn || insn->ignore || insn->visited) |
| 3379 | return 0; |
| 3380 | |
| 3381 | state->uaccess = sym->uaccess_safe; |
| 3382 | |
| 3383 | ret = validate_branch(file, insn->func, insn, *state); |
| 3384 | if (ret && backtrace) |
| 3385 | BT_FUNC("<=== (sym)", insn); |
| 3386 | return ret; |
| 3387 | } |
| 3388 | |
| 3389 | static int validate_section(struct objtool_file *file, struct section *sec) |
| 3390 | { |
| 3391 | struct insn_state state; |
| 3392 | struct symbol *func; |
| 3393 | int warnings = 0; |
| 3394 | |
| 3395 | list_for_each_entry(func, &sec->symbol_list, list) { |
| 3396 | if (func->type != STT_FUNC) |
| 3397 | continue; |
| 3398 | |
| 3399 | init_insn_state(&state, sec); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 3400 | set_func_state(&state.cfi); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3401 | |
| 3402 | warnings += validate_symbol(file, sec, func, &state); |
| 3403 | } |
| 3404 | |
| 3405 | return warnings; |
| 3406 | } |
| 3407 | |
| 3408 | static int validate_vmlinux_functions(struct objtool_file *file) |
| 3409 | { |
| 3410 | struct section *sec; |
| 3411 | int warnings = 0; |
| 3412 | |
| 3413 | sec = find_section_by_name(file->elf, ".noinstr.text"); |
| 3414 | if (sec) { |
| 3415 | warnings += validate_section(file, sec); |
| 3416 | warnings += validate_unwind_hints(file, sec); |
| 3417 | } |
| 3418 | |
| 3419 | sec = find_section_by_name(file->elf, ".entry.text"); |
| 3420 | if (sec) { |
| 3421 | warnings += validate_section(file, sec); |
| 3422 | warnings += validate_unwind_hints(file, sec); |
| 3423 | } |
| 3424 | |
| 3425 | return warnings; |
| 3426 | } |
| 3427 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3428 | static int validate_functions(struct objtool_file *file) |
| 3429 | { |
| 3430 | struct section *sec; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3431 | int warnings = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3432 | |
| 3433 | for_each_sec(file, sec) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3434 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) |
| 3435 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3436 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3437 | warnings += validate_section(file, sec); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | return warnings; |
| 3441 | } |
| 3442 | |
| 3443 | static int validate_reachable_instructions(struct objtool_file *file) |
| 3444 | { |
| 3445 | struct instruction *insn; |
| 3446 | |
| 3447 | if (file->ignore_unreachables) |
| 3448 | return 0; |
| 3449 | |
| 3450 | for_each_insn(file, insn) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3451 | if (insn->visited || ignore_unreachable_insn(file, insn)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3452 | continue; |
| 3453 | |
| 3454 | WARN_FUNC("unreachable instruction", insn->sec, insn->offset); |
| 3455 | return 1; |
| 3456 | } |
| 3457 | |
| 3458 | return 0; |
| 3459 | } |
| 3460 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3461 | int check(struct objtool_file *file) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3462 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3463 | int ret, warnings = 0; |
| 3464 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3465 | arch_initial_func_cfi_state(&initial_func_cfi); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 3466 | init_cfi_state(&init_cfi); |
| 3467 | init_cfi_state(&func_cfi); |
| 3468 | set_func_state(&func_cfi); |
| 3469 | |
| 3470 | if (!cfi_hash_alloc()) |
| 3471 | goto out; |
| 3472 | |
| 3473 | cfi_hash_add(&init_cfi); |
| 3474 | cfi_hash_add(&func_cfi); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3475 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3476 | ret = decode_sections(file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3477 | if (ret < 0) |
| 3478 | goto out; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 3479 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3480 | warnings += ret; |
| 3481 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3482 | if (list_empty(&file->insn_list)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3483 | goto out; |
| 3484 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3485 | if (vmlinux && !validate_dup) { |
| 3486 | ret = validate_vmlinux_functions(file); |
| 3487 | if (ret < 0) |
| 3488 | goto out; |
| 3489 | |
| 3490 | warnings += ret; |
| 3491 | goto out; |
| 3492 | } |
| 3493 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3494 | if (retpoline) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3495 | ret = validate_retpoline(file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3496 | if (ret < 0) |
| 3497 | return ret; |
| 3498 | warnings += ret; |
| 3499 | } |
| 3500 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3501 | ret = validate_functions(file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3502 | if (ret < 0) |
| 3503 | goto out; |
| 3504 | warnings += ret; |
| 3505 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3506 | ret = validate_unwind_hints(file, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3507 | if (ret < 0) |
| 3508 | goto out; |
| 3509 | warnings += ret; |
| 3510 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 3511 | if (unret) { |
| 3512 | /* |
| 3513 | * Must be after validate_branch() and friends, it plays |
| 3514 | * further games with insn->visited. |
| 3515 | */ |
| 3516 | ret = validate_unret(file); |
| 3517 | if (ret < 0) |
| 3518 | return ret; |
| 3519 | warnings += ret; |
| 3520 | } |
| 3521 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3522 | if (!warnings) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3523 | ret = validate_reachable_instructions(file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3524 | if (ret < 0) |
| 3525 | goto out; |
| 3526 | warnings += ret; |
| 3527 | } |
| 3528 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3529 | ret = create_static_call_sections(file); |
| 3530 | if (ret < 0) |
| 3531 | goto out; |
| 3532 | warnings += ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3533 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 3534 | if (retpoline) { |
| 3535 | ret = create_retpoline_sites_sections(file); |
| 3536 | if (ret < 0) |
| 3537 | goto out; |
| 3538 | warnings += ret; |
| 3539 | } |
| 3540 | |
| 3541 | if (rethunk) { |
| 3542 | ret = create_return_sites_sections(file); |
| 3543 | if (ret < 0) |
| 3544 | goto out; |
| 3545 | warnings += ret; |
| 3546 | } |
| 3547 | |
| 3548 | if (stats) { |
| 3549 | printf("nr_insns_visited: %ld\n", nr_insns_visited); |
| 3550 | printf("nr_cfi: %ld\n", nr_cfi); |
| 3551 | printf("nr_cfi_reused: %ld\n", nr_cfi_reused); |
| 3552 | printf("nr_cfi_cache: %ld\n", nr_cfi_cache); |
| 3553 | } |
| 3554 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3555 | out: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3556 | /* |
| 3557 | * For now, don't fail the kernel build on fatal warnings. These |
| 3558 | * errors are still fairly common due to the growing matrix of |
| 3559 | * supported toolchains and their recent pace of change. |
| 3560 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3561 | return 0; |
| 3562 | } |