Commit | Line | Data |
---|---|---|
1ccea77e | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
dcc914f4 JP |
2 | /* |
3 | * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> | |
dcc914f4 JP |
4 | */ |
5 | ||
6 | #include <string.h> | |
7 | #include <stdlib.h> | |
22682a07 | 8 | #include <inttypes.h> |
8b946cc3 | 9 | #include <sys/mman.h> |
dcc914f4 | 10 | |
7786032e VG |
11 | #include <arch/elf.h> |
12 | #include <objtool/builtin.h> | |
13 | #include <objtool/cfi.h> | |
14 | #include <objtool/arch.h> | |
15 | #include <objtool/check.h> | |
16 | #include <objtool/special.h> | |
17 | #include <objtool/warn.h> | |
18 | #include <objtool/endianness.h> | |
dcc914f4 | 19 | |
ee819aed | 20 | #include <linux/objtool.h> |
dcc914f4 JP |
21 | #include <linux/hashtable.h> |
22 | #include <linux/kernel.h> | |
1e7e4788 | 23 | #include <linux/static_call_types.h> |
dcc914f4 | 24 | |
dcc914f4 | 25 | struct alternative { |
d5406654 | 26 | struct alternative *next; |
dcc914f4 | 27 | struct instruction *insn; |
764eef4b | 28 | bool skip_orig; |
dcc914f4 JP |
29 | }; |
30 | ||
8b946cc3 PZ |
31 | static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache; |
32 | ||
33 | static struct cfi_init_state initial_func_cfi; | |
34 | static struct cfi_state init_cfi; | |
35 | static struct cfi_state func_cfi; | |
dcc914f4 | 36 | |
627fce14 JP |
37 | struct instruction *find_insn(struct objtool_file *file, |
38 | struct section *sec, unsigned long offset) | |
dcc914f4 JP |
39 | { |
40 | struct instruction *insn; | |
41 | ||
87ecb582 | 42 | hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) { |
dcc914f4 JP |
43 | if (insn->sec == sec && insn->offset == offset) |
44 | return insn; | |
87ecb582 | 45 | } |
dcc914f4 JP |
46 | |
47 | return NULL; | |
48 | } | |
49 | ||
1c34496e PZ |
50 | struct instruction *next_insn_same_sec(struct objtool_file *file, |
51 | struct instruction *insn) | |
dcc914f4 | 52 | { |
1c34496e PZ |
53 | if (insn->idx == INSN_CHUNK_MAX) |
54 | return find_insn(file, insn->sec, insn->offset + insn->len); | |
dcc914f4 | 55 | |
1c34496e PZ |
56 | insn++; |
57 | if (!insn->len) | |
dcc914f4 JP |
58 | return NULL; |
59 | ||
1c34496e | 60 | return insn; |
dcc914f4 JP |
61 | } |
62 | ||
13810435 JP |
63 | static struct instruction *next_insn_same_func(struct objtool_file *file, |
64 | struct instruction *insn) | |
65 | { | |
1c34496e | 66 | struct instruction *next = next_insn_same_sec(file, insn); |
dbcdbdfd | 67 | struct symbol *func = insn_func(insn); |
13810435 JP |
68 | |
69 | if (!func) | |
70 | return NULL; | |
71 | ||
1c34496e | 72 | if (next && insn_func(next) == func) |
13810435 JP |
73 | return next; |
74 | ||
75 | /* Check if we're already in the subfunction: */ | |
76 | if (func == func->cfunc) | |
77 | return NULL; | |
78 | ||
79 | /* Move to the subfunction: */ | |
80 | return find_insn(file, func->cfunc->sec, func->cfunc->offset); | |
81 | } | |
82 | ||
1c34496e PZ |
83 | static struct instruction *prev_insn_same_sec(struct objtool_file *file, |
84 | struct instruction *insn) | |
85 | { | |
86 | if (insn->idx == 0) { | |
87 | if (insn->prev_len) | |
88 | return find_insn(file, insn->sec, insn->offset - insn->prev_len); | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | return insn - 1; | |
93 | } | |
94 | ||
1119d265 | 95 | static struct instruction *prev_insn_same_sym(struct objtool_file *file, |
1c34496e | 96 | struct instruction *insn) |
1119d265 | 97 | { |
1c34496e | 98 | struct instruction *prev = prev_insn_same_sec(file, insn); |
1119d265 | 99 | |
1c34496e | 100 | if (prev && insn_func(prev) == insn_func(insn)) |
1119d265 JP |
101 | return prev; |
102 | ||
103 | return NULL; | |
104 | } | |
105 | ||
1c34496e PZ |
106 | #define for_each_insn(file, insn) \ |
107 | for (struct section *__sec, *__fake = (struct section *)1; \ | |
108 | __fake; __fake = NULL) \ | |
109 | for_each_sec(file, __sec) \ | |
110 | sec_for_each_insn(file, __sec, insn) | |
111 | ||
f0f70adb | 112 | #define func_for_each_insn(file, func, insn) \ |
13810435 JP |
113 | for (insn = find_insn(file, func->sec, func->offset); \ |
114 | insn; \ | |
115 | insn = next_insn_same_func(file, insn)) | |
116 | ||
dbf4aeb0 PZ |
117 | #define sym_for_each_insn(file, sym, insn) \ |
118 | for (insn = find_insn(file, sym->sec, sym->offset); \ | |
1c34496e PZ |
119 | insn && insn->offset < sym->offset + sym->len; \ |
120 | insn = next_insn_same_sec(file, insn)) | |
dcc914f4 | 121 | |
dbf4aeb0 | 122 | #define sym_for_each_insn_continue_reverse(file, sym, insn) \ |
1c34496e PZ |
123 | for (insn = prev_insn_same_sec(file, insn); \ |
124 | insn && insn->offset >= sym->offset; \ | |
125 | insn = prev_insn_same_sec(file, insn)) | |
dcc914f4 JP |
126 | |
127 | #define sec_for_each_insn_from(file, insn) \ | |
128 | for (; insn; insn = next_insn_same_sec(file, insn)) | |
129 | ||
baa41469 JP |
130 | #define sec_for_each_insn_continue(file, insn) \ |
131 | for (insn = next_insn_same_sec(file, insn); insn; \ | |
132 | insn = next_insn_same_sec(file, insn)) | |
dcc914f4 | 133 | |
c6f5dc28 PZ |
134 | static inline struct symbol *insn_call_dest(struct instruction *insn) |
135 | { | |
136 | if (insn->type == INSN_JUMP_DYNAMIC || | |
137 | insn->type == INSN_CALL_DYNAMIC) | |
138 | return NULL; | |
139 | ||
140 | return insn->_call_dest; | |
141 | } | |
142 | ||
143 | static inline struct reloc *insn_jump_table(struct instruction *insn) | |
144 | { | |
145 | if (insn->type == INSN_JUMP_DYNAMIC || | |
146 | insn->type == INSN_CALL_DYNAMIC) | |
147 | return insn->_jump_table; | |
148 | ||
149 | return NULL; | |
150 | } | |
151 | ||
99033461 JP |
152 | static bool is_jump_table_jump(struct instruction *insn) |
153 | { | |
154 | struct alt_group *alt_group = insn->alt_group; | |
155 | ||
c6f5dc28 | 156 | if (insn_jump_table(insn)) |
99033461 JP |
157 | return true; |
158 | ||
159 | /* Retpoline alternative for a jump table? */ | |
160 | return alt_group && alt_group->orig_group && | |
c6f5dc28 | 161 | insn_jump_table(alt_group->orig_group->first_insn); |
99033461 JP |
162 | } |
163 | ||
0c1ddd33 JP |
164 | static bool is_sibling_call(struct instruction *insn) |
165 | { | |
ecf11ba4 | 166 | /* |
5a9c361a | 167 | * Assume only STT_FUNC calls have jump-tables. |
ecf11ba4 | 168 | */ |
5a9c361a PZ |
169 | if (insn_func(insn)) { |
170 | /* An indirect jump is either a sibling call or a jump to a table. */ | |
171 | if (insn->type == INSN_JUMP_DYNAMIC) | |
172 | return !is_jump_table_jump(insn); | |
173 | } | |
0c1ddd33 | 174 | |
c6f5dc28 PZ |
175 | /* add_jump_destinations() sets insn_call_dest(insn) for sibling calls. */ |
176 | return (is_static_jump(insn) && insn_call_dest(insn)); | |
0c1ddd33 JP |
177 | } |
178 | ||
dcc914f4 JP |
179 | /* |
180 | * This checks to see if the given function is a "noreturn" function. | |
181 | * | |
182 | * For global functions which are outside the scope of this object file, we | |
183 | * have to keep a manual list of them. | |
184 | * | |
185 | * For local functions, we have to detect them manually by simply looking for | |
186 | * the lack of a return instruction. | |
dcc914f4 | 187 | */ |
8e25c9f8 JP |
188 | static bool __dead_end_function(struct objtool_file *file, struct symbol *func, |
189 | int recursion) | |
dcc914f4 JP |
190 | { |
191 | int i; | |
192 | struct instruction *insn; | |
193 | bool empty = true; | |
194 | ||
195 | /* | |
196 | * Unfortunately these have to be hard coded because the noreturn | |
c93c296f | 197 | * attribute isn't provided in ELF data. Keep 'em sorted. |
dcc914f4 JP |
198 | */ |
199 | static const char * const global_noreturns[] = { | |
c93c296f BP |
200 | "__invalid_creds", |
201 | "__module_put_and_kthread_exit", | |
202 | "__reiserfs_panic", | |
dcc914f4 | 203 | "__stack_chk_fail", |
c93c296f BP |
204 | "__ubsan_handle_builtin_unreachable", |
205 | "cpu_bringup_and_idle", | |
206 | "cpu_startup_entry", | |
dcc914f4 | 207 | "do_exit", |
c93c296f | 208 | "do_group_exit", |
dcc914f4 | 209 | "do_task_dead", |
c93c296f BP |
210 | "ex_handler_msr_mce", |
211 | "fortify_panic", | |
cead1855 | 212 | "kthread_complete_and_exit", |
c93c296f BP |
213 | "kthread_exit", |
214 | "kunit_try_catch_throw", | |
dcc914f4 | 215 | "lbug_with_loc", |
684fb246 | 216 | "machine_real_restart", |
c93c296f BP |
217 | "make_task_dead", |
218 | "panic", | |
1fb466df | 219 | "rewind_stack_and_make_dead", |
c93c296f BP |
220 | "sev_es_terminate", |
221 | "snp_abort", | |
f9cdf7ca | 222 | "stop_this_cpu", |
c93c296f | 223 | "usercopy_abort", |
f697cb00 | 224 | "xen_cpu_bringup_again", |
c93c296f | 225 | "xen_start_kernel", |
dcc914f4 JP |
226 | }; |
227 | ||
c9bab22b JP |
228 | if (!func) |
229 | return false; | |
230 | ||
dcc914f4 | 231 | if (func->bind == STB_WEAK) |
8e25c9f8 | 232 | return false; |
dcc914f4 JP |
233 | |
234 | if (func->bind == STB_GLOBAL) | |
235 | for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) | |
236 | if (!strcmp(func->name, global_noreturns[i])) | |
8e25c9f8 | 237 | return true; |
dcc914f4 | 238 | |
13810435 | 239 | if (!func->len) |
8e25c9f8 | 240 | return false; |
dcc914f4 | 241 | |
13810435 | 242 | insn = find_insn(file, func->sec, func->offset); |
5f6e430f | 243 | if (!insn || !insn_func(insn)) |
8e25c9f8 | 244 | return false; |
13810435 | 245 | |
f0f70adb | 246 | func_for_each_insn(file, func, insn) { |
dcc914f4 JP |
247 | empty = false; |
248 | ||
249 | if (insn->type == INSN_RETURN) | |
8e25c9f8 | 250 | return false; |
dcc914f4 JP |
251 | } |
252 | ||
253 | if (empty) | |
8e25c9f8 | 254 | return false; |
dcc914f4 JP |
255 | |
256 | /* | |
257 | * A function can have a sibling call instead of a return. In that | |
258 | * case, the function's dead-end status depends on whether the target | |
259 | * of the sibling call returns. | |
260 | */ | |
f0f70adb | 261 | func_for_each_insn(file, func, insn) { |
0c1ddd33 | 262 | if (is_sibling_call(insn)) { |
dcc914f4 | 263 | struct instruction *dest = insn->jump_dest; |
dcc914f4 JP |
264 | |
265 | if (!dest) | |
266 | /* sibling call to another file */ | |
8e25c9f8 | 267 | return false; |
dcc914f4 | 268 | |
0c1ddd33 JP |
269 | /* local sibling call */ |
270 | if (recursion == 5) { | |
271 | /* | |
272 | * Infinite recursion: two functions have | |
273 | * sibling calls to each other. This is a very | |
274 | * rare case. It means they aren't dead ends. | |
275 | */ | |
276 | return false; | |
dcc914f4 | 277 | } |
dcc914f4 | 278 | |
dbcdbdfd | 279 | return __dead_end_function(file, insn_func(dest), recursion+1); |
0c1ddd33 | 280 | } |
dcc914f4 JP |
281 | } |
282 | ||
8e25c9f8 | 283 | return true; |
dcc914f4 JP |
284 | } |
285 | ||
8e25c9f8 | 286 | static bool dead_end_function(struct objtool_file *file, struct symbol *func) |
dcc914f4 JP |
287 | { |
288 | return __dead_end_function(file, func, 0); | |
289 | } | |
290 | ||
e7c0219b | 291 | static void init_cfi_state(struct cfi_state *cfi) |
baa41469 JP |
292 | { |
293 | int i; | |
294 | ||
dd88a0a0 | 295 | for (i = 0; i < CFI_NUM_REGS; i++) { |
e7c0219b PZ |
296 | cfi->regs[i].base = CFI_UNDEFINED; |
297 | cfi->vals[i].base = CFI_UNDEFINED; | |
dd88a0a0 | 298 | } |
e7c0219b PZ |
299 | cfi->cfa.base = CFI_UNDEFINED; |
300 | cfi->drap_reg = CFI_UNDEFINED; | |
301 | cfi->drap_offset = -1; | |
302 | } | |
303 | ||
753da417 JP |
304 | static void init_insn_state(struct objtool_file *file, struct insn_state *state, |
305 | struct section *sec) | |
e7c0219b PZ |
306 | { |
307 | memset(state, 0, sizeof(*state)); | |
308 | init_cfi_state(&state->cfi); | |
932f8e98 PZ |
309 | |
310 | /* | |
311 | * We need the full vmlinux for noinstr validation, otherwise we can | |
c6f5dc28 PZ |
312 | * not correctly determine insn_call_dest(insn)->sec (external symbols |
313 | * do not have a section). | |
932f8e98 | 314 | */ |
753da417 | 315 | if (opts.link && opts.noinstr && sec) |
932f8e98 | 316 | state->noinstr = sec->noinstr; |
baa41469 JP |
317 | } |
318 | ||
8b946cc3 PZ |
319 | static struct cfi_state *cfi_alloc(void) |
320 | { | |
321 | struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1); | |
322 | if (!cfi) { | |
323 | WARN("calloc failed"); | |
324 | exit(1); | |
325 | } | |
326 | nr_cfi++; | |
327 | return cfi; | |
328 | } | |
329 | ||
330 | static int cfi_bits; | |
331 | static struct hlist_head *cfi_hash; | |
332 | ||
333 | static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2) | |
334 | { | |
335 | return memcmp((void *)cfi1 + sizeof(cfi1->hash), | |
336 | (void *)cfi2 + sizeof(cfi2->hash), | |
337 | sizeof(struct cfi_state) - sizeof(struct hlist_node)); | |
338 | } | |
339 | ||
340 | static inline u32 cfi_key(struct cfi_state *cfi) | |
341 | { | |
342 | return jhash((void *)cfi + sizeof(cfi->hash), | |
343 | sizeof(*cfi) - sizeof(cfi->hash), 0); | |
344 | } | |
345 | ||
346 | static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi) | |
347 | { | |
348 | struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; | |
349 | struct cfi_state *obj; | |
350 | ||
351 | hlist_for_each_entry(obj, head, hash) { | |
352 | if (!cficmp(cfi, obj)) { | |
353 | nr_cfi_cache++; | |
354 | return obj; | |
355 | } | |
356 | } | |
357 | ||
358 | obj = cfi_alloc(); | |
359 | *obj = *cfi; | |
360 | hlist_add_head(&obj->hash, head); | |
361 | ||
362 | return obj; | |
363 | } | |
364 | ||
365 | static void cfi_hash_add(struct cfi_state *cfi) | |
366 | { | |
367 | struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; | |
368 | ||
369 | hlist_add_head(&cfi->hash, head); | |
370 | } | |
371 | ||
372 | static void *cfi_hash_alloc(unsigned long size) | |
373 | { | |
374 | cfi_bits = max(10, ilog2(size)); | |
375 | cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits, | |
376 | PROT_READ|PROT_WRITE, | |
377 | MAP_PRIVATE|MAP_ANON, -1, 0); | |
378 | if (cfi_hash == (void *)-1L) { | |
379 | WARN("mmap fail cfi_hash"); | |
380 | cfi_hash = NULL; | |
2daf7fab | 381 | } else if (opts.stats) { |
8b946cc3 PZ |
382 | printf("cfi_bits: %d\n", cfi_bits); |
383 | } | |
384 | ||
385 | return cfi_hash; | |
386 | } | |
387 | ||
388 | static unsigned long nr_insns; | |
389 | static unsigned long nr_insns_visited; | |
390 | ||
dcc914f4 JP |
391 | /* |
392 | * Call the arch-specific instruction decoder for all the instructions and add | |
393 | * them to the global instruction list. | |
394 | */ | |
395 | static int decode_instructions(struct objtool_file *file) | |
396 | { | |
397 | struct section *sec; | |
398 | struct symbol *func; | |
399 | unsigned long offset; | |
400 | struct instruction *insn; | |
401 | int ret; | |
402 | ||
baa41469 | 403 | for_each_sec(file, sec) { |
1c34496e PZ |
404 | struct instruction *insns = NULL; |
405 | u8 prev_len = 0; | |
406 | u8 idx = 0; | |
dcc914f4 JP |
407 | |
408 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) | |
409 | continue; | |
410 | ||
627fce14 JP |
411 | if (strcmp(sec->name, ".altinstr_replacement") && |
412 | strcmp(sec->name, ".altinstr_aux") && | |
413 | strncmp(sec->name, ".discard.", 9)) | |
414 | sec->text = true; | |
415 | ||
0cc9ac8d | 416 | if (!strcmp(sec->name, ".noinstr.text") || |
951ddecf | 417 | !strcmp(sec->name, ".entry.text") || |
2b5a0e42 | 418 | !strcmp(sec->name, ".cpuidle.text") || |
951ddecf | 419 | !strncmp(sec->name, ".text.__x86.", 12)) |
c4a33939 PZ |
420 | sec->noinstr = true; |
421 | ||
6644ee84 PZ |
422 | /* |
423 | * .init.text code is ran before userspace and thus doesn't | |
424 | * strictly need retpolines, except for modules which are | |
425 | * loaded late, they very much do need retpoline in their | |
426 | * .init.text | |
427 | */ | |
428 | if (!strcmp(sec->name, ".init.text") && !opts.module) | |
429 | sec->init = true; | |
430 | ||
fe255fe6 | 431 | for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) { |
1c34496e PZ |
432 | if (!insns || idx == INSN_CHUNK_MAX) { |
433 | insns = calloc(sizeof(*insn), INSN_CHUNK_SIZE); | |
434 | if (!insns) { | |
435 | WARN("malloc failed"); | |
436 | return -1; | |
437 | } | |
438 | idx = 0; | |
439 | } else { | |
440 | idx++; | |
baa41469 | 441 | } |
1c34496e PZ |
442 | insn = &insns[idx]; |
443 | insn->idx = idx; | |
baa41469 | 444 | |
1c34496e | 445 | INIT_LIST_HEAD(&insn->call_node); |
dcc914f4 JP |
446 | insn->sec = sec; |
447 | insn->offset = offset; | |
1c34496e | 448 | insn->prev_len = prev_len; |
dcc914f4 | 449 | |
db2b0c5d | 450 | ret = arch_decode_instruction(file, sec, offset, |
fe255fe6 | 451 | sec->sh.sh_size - offset, |
20a55463 | 452 | insn); |
dcc914f4 | 453 | if (ret) |
1c34496e PZ |
454 | return ret; |
455 | ||
456 | prev_len = insn->len; | |
dcc914f4 | 457 | |
0e5b613b PZ |
458 | /* |
459 | * By default, "ud2" is a dead end unless otherwise | |
460 | * annotated, because GCC 7 inserts it for certain | |
461 | * divide-by-zero cases. | |
462 | */ | |
463 | if (insn->type == INSN_BUG) | |
464 | insn->dead_end = true; | |
465 | ||
87ecb582 | 466 | hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset)); |
1e11f3fd | 467 | nr_insns++; |
dcc914f4 JP |
468 | } |
469 | ||
1c34496e PZ |
470 | // printf("%s: last chunk used: %d\n", sec->name, (int)idx); |
471 | ||
dcc914f4 | 472 | list_for_each_entry(func, &sec->symbol_list, list) { |
dbcdbdfd PZ |
473 | if (func->type != STT_NOTYPE && func->type != STT_FUNC) |
474 | continue; | |
475 | ||
cad90e53 NP |
476 | if (func->offset == sec->sh.sh_size) { |
477 | /* Heuristic: likely an "end" symbol */ | |
478 | if (func->type == STT_NOTYPE) | |
479 | continue; | |
480 | WARN("%s(): STT_FUNC at end of section", | |
481 | func->name); | |
482 | return -1; | |
483 | } | |
484 | ||
dbcdbdfd | 485 | if (func->return_thunk || func->alias != func) |
dcc914f4 JP |
486 | continue; |
487 | ||
488 | if (!find_insn(file, sec, func->offset)) { | |
489 | WARN("%s(): can't find starting instruction", | |
490 | func->name); | |
491 | return -1; | |
492 | } | |
493 | ||
08f87a93 | 494 | sym_for_each_insn(file, func, insn) { |
dbcdbdfd PZ |
495 | insn->sym = func; |
496 | if (func->type == STT_FUNC && | |
497 | insn->type == INSN_ENDBR && | |
498 | list_empty(&insn->call_node)) { | |
499 | if (insn->offset == func->offset) { | |
89bc853e | 500 | list_add_tail(&insn->call_node, &file->endbr_list); |
08f87a93 PZ |
501 | file->nr_endbr++; |
502 | } else { | |
503 | file->nr_endbr_int++; | |
504 | } | |
505 | } | |
506 | } | |
dcc914f4 JP |
507 | } |
508 | } | |
509 | ||
2daf7fab | 510 | if (opts.stats) |
1e11f3fd PZ |
511 | printf("nr_insns: %lu\n", nr_insns); |
512 | ||
dcc914f4 JP |
513 | return 0; |
514 | } | |
515 | ||
db2b0c5d PZ |
516 | /* |
517 | * Read the pv_ops[] .data table to find the static initialized values. | |
518 | */ | |
519 | static int add_pv_ops(struct objtool_file *file, const char *symname) | |
520 | { | |
521 | struct symbol *sym, *func; | |
522 | unsigned long off, end; | |
523 | struct reloc *rel; | |
524 | int idx; | |
525 | ||
526 | sym = find_symbol_by_name(file->elf, symname); | |
527 | if (!sym) | |
528 | return 0; | |
529 | ||
530 | off = sym->offset; | |
531 | end = off + sym->len; | |
532 | for (;;) { | |
533 | rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off); | |
534 | if (!rel) | |
535 | break; | |
536 | ||
537 | func = rel->sym; | |
538 | if (func->type == STT_SECTION) | |
539 | func = find_symbol_by_offset(rel->sym->sec, rel->addend); | |
540 | ||
541 | idx = (rel->offset - sym->offset) / sizeof(unsigned long); | |
542 | ||
543 | objtool_pv_add(file, idx, func); | |
544 | ||
545 | off = rel->offset + 1; | |
546 | if (off > end) | |
547 | break; | |
548 | } | |
549 | ||
550 | return 0; | |
551 | } | |
552 | ||
553 | /* | |
554 | * Allocate and initialize file->pv_ops[]. | |
555 | */ | |
556 | static int init_pv_ops(struct objtool_file *file) | |
557 | { | |
558 | static const char *pv_ops_tables[] = { | |
559 | "pv_ops", | |
560 | "xen_cpu_ops", | |
561 | "xen_irq_ops", | |
562 | "xen_mmu_ops", | |
563 | NULL, | |
564 | }; | |
565 | const char *pv_ops; | |
566 | struct symbol *sym; | |
567 | int idx, nr; | |
568 | ||
2daf7fab | 569 | if (!opts.noinstr) |
db2b0c5d PZ |
570 | return 0; |
571 | ||
572 | file->pv_ops = NULL; | |
573 | ||
574 | sym = find_symbol_by_name(file->elf, "pv_ops"); | |
575 | if (!sym) | |
576 | return 0; | |
577 | ||
578 | nr = sym->len / sizeof(unsigned long); | |
579 | file->pv_ops = calloc(sizeof(struct pv_state), nr); | |
580 | if (!file->pv_ops) | |
581 | return -1; | |
582 | ||
583 | for (idx = 0; idx < nr; idx++) | |
584 | INIT_LIST_HEAD(&file->pv_ops[idx].targets); | |
585 | ||
586 | for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++) | |
587 | add_pv_ops(file, pv_ops); | |
588 | ||
589 | return 0; | |
590 | } | |
591 | ||
6b5dd716 ST |
592 | static struct instruction *find_last_insn(struct objtool_file *file, |
593 | struct section *sec) | |
594 | { | |
595 | struct instruction *insn = NULL; | |
596 | unsigned int offset; | |
fe255fe6 | 597 | unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0; |
6b5dd716 | 598 | |
fe255fe6 | 599 | for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--) |
6b5dd716 ST |
600 | insn = find_insn(file, sec, offset); |
601 | ||
602 | return insn; | |
603 | } | |
604 | ||
dcc914f4 | 605 | /* |
649ea4d5 | 606 | * Mark "ud2" instructions and manually annotated dead ends. |
dcc914f4 JP |
607 | */ |
608 | static int add_dead_ends(struct objtool_file *file) | |
609 | { | |
610 | struct section *sec; | |
f1974222 | 611 | struct reloc *reloc; |
dcc914f4 | 612 | struct instruction *insn; |
dcc914f4 | 613 | |
649ea4d5 JP |
614 | /* |
615 | * Check for manually annotated dead ends. | |
616 | */ | |
dcc914f4 JP |
617 | sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); |
618 | if (!sec) | |
649ea4d5 | 619 | goto reachable; |
dcc914f4 | 620 | |
f1974222 MH |
621 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
622 | if (reloc->sym->type != STT_SECTION) { | |
dcc914f4 JP |
623 | WARN("unexpected relocation symbol type in %s", sec->name); |
624 | return -1; | |
625 | } | |
f1974222 | 626 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
dcc914f4 | 627 | if (insn) |
1c34496e | 628 | insn = prev_insn_same_sec(file, insn); |
fe255fe6 | 629 | else if (reloc->addend == reloc->sym->sec->sh.sh_size) { |
f1974222 | 630 | insn = find_last_insn(file, reloc->sym->sec); |
6b5dd716 | 631 | if (!insn) { |
22682a07 | 632 | WARN("can't find unreachable insn at %s+0x%" PRIx64, |
f1974222 | 633 | reloc->sym->sec->name, reloc->addend); |
dcc914f4 JP |
634 | return -1; |
635 | } | |
636 | } else { | |
22682a07 | 637 | WARN("can't find unreachable insn at %s+0x%" PRIx64, |
f1974222 | 638 | reloc->sym->sec->name, reloc->addend); |
dcc914f4 JP |
639 | return -1; |
640 | } | |
641 | ||
642 | insn->dead_end = true; | |
643 | } | |
644 | ||
649ea4d5 JP |
645 | reachable: |
646 | /* | |
647 | * These manually annotated reachable checks are needed for GCC 4.4, | |
648 | * where the Linux unreachable() macro isn't supported. In that case | |
649 | * GCC doesn't know the "ud2" is fatal, so it generates code as if it's | |
650 | * not a dead end. | |
651 | */ | |
652 | sec = find_section_by_name(file->elf, ".rela.discard.reachable"); | |
653 | if (!sec) | |
654 | return 0; | |
655 | ||
f1974222 MH |
656 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
657 | if (reloc->sym->type != STT_SECTION) { | |
649ea4d5 JP |
658 | WARN("unexpected relocation symbol type in %s", sec->name); |
659 | return -1; | |
660 | } | |
f1974222 | 661 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
649ea4d5 | 662 | if (insn) |
1c34496e | 663 | insn = prev_insn_same_sec(file, insn); |
fe255fe6 | 664 | else if (reloc->addend == reloc->sym->sec->sh.sh_size) { |
f1974222 | 665 | insn = find_last_insn(file, reloc->sym->sec); |
6b5dd716 | 666 | if (!insn) { |
22682a07 | 667 | WARN("can't find reachable insn at %s+0x%" PRIx64, |
f1974222 | 668 | reloc->sym->sec->name, reloc->addend); |
649ea4d5 JP |
669 | return -1; |
670 | } | |
671 | } else { | |
22682a07 | 672 | WARN("can't find reachable insn at %s+0x%" PRIx64, |
f1974222 | 673 | reloc->sym->sec->name, reloc->addend); |
649ea4d5 JP |
674 | return -1; |
675 | } | |
676 | ||
677 | insn->dead_end = false; | |
678 | } | |
679 | ||
dcc914f4 JP |
680 | return 0; |
681 | } | |
682 | ||
1e7e4788 JP |
683 | static int create_static_call_sections(struct objtool_file *file) |
684 | { | |
ef47cc01 | 685 | struct section *sec; |
1e7e4788 JP |
686 | struct static_call_site *site; |
687 | struct instruction *insn; | |
688 | struct symbol *key_sym; | |
689 | char *key_name, *tmp; | |
690 | int idx; | |
691 | ||
692 | sec = find_section_by_name(file->elf, ".static_call_sites"); | |
693 | if (sec) { | |
694 | INIT_LIST_HEAD(&file->static_call_list); | |
695 | WARN("file already has .static_call_sites section, skipping"); | |
696 | return 0; | |
697 | } | |
698 | ||
699 | if (list_empty(&file->static_call_list)) | |
700 | return 0; | |
701 | ||
702 | idx = 0; | |
43d5430a | 703 | list_for_each_entry(insn, &file->static_call_list, call_node) |
1e7e4788 JP |
704 | idx++; |
705 | ||
706 | sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE, | |
707 | sizeof(struct static_call_site), idx); | |
708 | if (!sec) | |
709 | return -1; | |
710 | ||
1e7e4788 | 711 | idx = 0; |
43d5430a | 712 | list_for_each_entry(insn, &file->static_call_list, call_node) { |
1e7e4788 JP |
713 | |
714 | site = (struct static_call_site *)sec->data->d_buf + idx; | |
715 | memset(site, 0, sizeof(struct static_call_site)); | |
716 | ||
717 | /* populate reloc for 'addr' */ | |
ef47cc01 PZ |
718 | if (elf_add_reloc_to_insn(file->elf, sec, |
719 | idx * sizeof(struct static_call_site), | |
720 | R_X86_64_PC32, | |
721 | insn->sec, insn->offset)) | |
44f6a7c0 | 722 | return -1; |
1e7e4788 JP |
723 | |
724 | /* find key symbol */ | |
c6f5dc28 | 725 | key_name = strdup(insn_call_dest(insn)->name); |
1e7e4788 JP |
726 | if (!key_name) { |
727 | perror("strdup"); | |
728 | return -1; | |
729 | } | |
730 | if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR, | |
731 | STATIC_CALL_TRAMP_PREFIX_LEN)) { | |
732 | WARN("static_call: trampoline name malformed: %s", key_name); | |
3da73f10 | 733 | free(key_name); |
1e7e4788 JP |
734 | return -1; |
735 | } | |
736 | tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN; | |
737 | memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN); | |
738 | ||
739 | key_sym = find_symbol_by_name(file->elf, tmp); | |
740 | if (!key_sym) { | |
2daf7fab | 741 | if (!opts.module) { |
73f44fe1 | 742 | WARN("static_call: can't find static_call_key symbol: %s", tmp); |
3da73f10 | 743 | free(key_name); |
73f44fe1 JP |
744 | return -1; |
745 | } | |
746 | ||
747 | /* | |
748 | * For modules(), the key might not be exported, which | |
749 | * means the module can make static calls but isn't | |
750 | * allowed to change them. | |
751 | * | |
752 | * In that case we temporarily set the key to be the | |
753 | * trampoline address. This is fixed up in | |
754 | * static_call_add_module(). | |
755 | */ | |
c6f5dc28 | 756 | key_sym = insn_call_dest(insn); |
1e7e4788 JP |
757 | } |
758 | free(key_name); | |
759 | ||
760 | /* populate reloc for 'key' */ | |
ef47cc01 PZ |
761 | if (elf_add_reloc(file->elf, sec, |
762 | idx * sizeof(struct static_call_site) + 4, | |
763 | R_X86_64_PC32, key_sym, | |
764 | is_sibling_call(insn) * STATIC_CALL_SITE_TAIL)) | |
1e7e4788 | 765 | return -1; |
1e7e4788 JP |
766 | |
767 | idx++; | |
768 | } | |
769 | ||
1e7e4788 JP |
770 | return 0; |
771 | } | |
772 | ||
134ab5bd PZ |
773 | static int create_retpoline_sites_sections(struct objtool_file *file) |
774 | { | |
775 | struct instruction *insn; | |
776 | struct section *sec; | |
777 | int idx; | |
778 | ||
779 | sec = find_section_by_name(file->elf, ".retpoline_sites"); | |
780 | if (sec) { | |
781 | WARN("file already has .retpoline_sites, skipping"); | |
782 | return 0; | |
783 | } | |
784 | ||
785 | idx = 0; | |
786 | list_for_each_entry(insn, &file->retpoline_call_list, call_node) | |
787 | idx++; | |
788 | ||
789 | if (!idx) | |
790 | return 0; | |
791 | ||
792 | sec = elf_create_section(file->elf, ".retpoline_sites", 0, | |
793 | sizeof(int), idx); | |
794 | if (!sec) { | |
795 | WARN("elf_create_section: .retpoline_sites"); | |
796 | return -1; | |
797 | } | |
798 | ||
799 | idx = 0; | |
800 | list_for_each_entry(insn, &file->retpoline_call_list, call_node) { | |
801 | ||
802 | int *site = (int *)sec->data->d_buf + idx; | |
803 | *site = 0; | |
804 | ||
805 | if (elf_add_reloc_to_insn(file->elf, sec, | |
806 | idx * sizeof(int), | |
807 | R_X86_64_PC32, | |
808 | insn->sec, insn->offset)) { | |
809 | WARN("elf_add_reloc_to_insn: .retpoline_sites"); | |
810 | return -1; | |
811 | } | |
812 | ||
813 | idx++; | |
814 | } | |
815 | ||
816 | return 0; | |
817 | } | |
818 | ||
d9e9d230 PZ |
819 | static int create_return_sites_sections(struct objtool_file *file) |
820 | { | |
821 | struct instruction *insn; | |
822 | struct section *sec; | |
823 | int idx; | |
824 | ||
825 | sec = find_section_by_name(file->elf, ".return_sites"); | |
826 | if (sec) { | |
827 | WARN("file already has .return_sites, skipping"); | |
828 | return 0; | |
829 | } | |
830 | ||
831 | idx = 0; | |
832 | list_for_each_entry(insn, &file->return_thunk_list, call_node) | |
833 | idx++; | |
834 | ||
835 | if (!idx) | |
836 | return 0; | |
837 | ||
838 | sec = elf_create_section(file->elf, ".return_sites", 0, | |
839 | sizeof(int), idx); | |
840 | if (!sec) { | |
841 | WARN("elf_create_section: .return_sites"); | |
842 | return -1; | |
843 | } | |
844 | ||
845 | idx = 0; | |
846 | list_for_each_entry(insn, &file->return_thunk_list, call_node) { | |
847 | ||
848 | int *site = (int *)sec->data->d_buf + idx; | |
849 | *site = 0; | |
850 | ||
851 | if (elf_add_reloc_to_insn(file->elf, sec, | |
852 | idx * sizeof(int), | |
853 | R_X86_64_PC32, | |
854 | insn->sec, insn->offset)) { | |
855 | WARN("elf_add_reloc_to_insn: .return_sites"); | |
856 | return -1; | |
857 | } | |
858 | ||
859 | idx++; | |
860 | } | |
861 | ||
862 | return 0; | |
863 | } | |
864 | ||
89bc853e PZ |
865 | static int create_ibt_endbr_seal_sections(struct objtool_file *file) |
866 | { | |
867 | struct instruction *insn; | |
868 | struct section *sec; | |
869 | int idx; | |
870 | ||
871 | sec = find_section_by_name(file->elf, ".ibt_endbr_seal"); | |
872 | if (sec) { | |
873 | WARN("file already has .ibt_endbr_seal, skipping"); | |
874 | return 0; | |
875 | } | |
876 | ||
877 | idx = 0; | |
878 | list_for_each_entry(insn, &file->endbr_list, call_node) | |
879 | idx++; | |
880 | ||
2daf7fab | 881 | if (opts.stats) { |
89bc853e PZ |
882 | printf("ibt: ENDBR at function start: %d\n", file->nr_endbr); |
883 | printf("ibt: ENDBR inside functions: %d\n", file->nr_endbr_int); | |
884 | printf("ibt: superfluous ENDBR: %d\n", idx); | |
885 | } | |
886 | ||
887 | if (!idx) | |
888 | return 0; | |
889 | ||
890 | sec = elf_create_section(file->elf, ".ibt_endbr_seal", 0, | |
891 | sizeof(int), idx); | |
892 | if (!sec) { | |
893 | WARN("elf_create_section: .ibt_endbr_seal"); | |
894 | return -1; | |
895 | } | |
896 | ||
897 | idx = 0; | |
898 | list_for_each_entry(insn, &file->endbr_list, call_node) { | |
899 | ||
900 | int *site = (int *)sec->data->d_buf + idx; | |
03d7a105 | 901 | struct symbol *sym = insn->sym; |
89bc853e PZ |
902 | *site = 0; |
903 | ||
03d7a105 MK |
904 | if (opts.module && sym && sym->type == STT_FUNC && |
905 | insn->offset == sym->offset && | |
906 | (!strcmp(sym->name, "init_module") || | |
907 | !strcmp(sym->name, "cleanup_module"))) | |
908 | WARN("%s(): not an indirect call target", sym->name); | |
909 | ||
89bc853e PZ |
910 | if (elf_add_reloc_to_insn(file->elf, sec, |
911 | idx * sizeof(int), | |
912 | R_X86_64_PC32, | |
913 | insn->sec, insn->offset)) { | |
914 | WARN("elf_add_reloc_to_insn: .ibt_endbr_seal"); | |
915 | return -1; | |
916 | } | |
917 | ||
918 | idx++; | |
919 | } | |
920 | ||
921 | return 0; | |
922 | } | |
923 | ||
9a479f76 PZ |
924 | static int create_cfi_sections(struct objtool_file *file) |
925 | { | |
926 | struct section *sec, *s; | |
927 | struct symbol *sym; | |
928 | unsigned int *loc; | |
929 | int idx; | |
930 | ||
931 | sec = find_section_by_name(file->elf, ".cfi_sites"); | |
932 | if (sec) { | |
933 | INIT_LIST_HEAD(&file->call_list); | |
934 | WARN("file already has .cfi_sites section, skipping"); | |
935 | return 0; | |
936 | } | |
937 | ||
938 | idx = 0; | |
939 | for_each_sec(file, s) { | |
940 | if (!s->text) | |
941 | continue; | |
942 | ||
943 | list_for_each_entry(sym, &s->symbol_list, list) { | |
944 | if (sym->type != STT_FUNC) | |
945 | continue; | |
946 | ||
947 | if (strncmp(sym->name, "__cfi_", 6)) | |
948 | continue; | |
949 | ||
950 | idx++; | |
951 | } | |
952 | } | |
953 | ||
954 | sec = elf_create_section(file->elf, ".cfi_sites", 0, sizeof(unsigned int), idx); | |
955 | if (!sec) | |
956 | return -1; | |
957 | ||
958 | idx = 0; | |
959 | for_each_sec(file, s) { | |
960 | if (!s->text) | |
961 | continue; | |
962 | ||
963 | list_for_each_entry(sym, &s->symbol_list, list) { | |
964 | if (sym->type != STT_FUNC) | |
965 | continue; | |
966 | ||
967 | if (strncmp(sym->name, "__cfi_", 6)) | |
968 | continue; | |
969 | ||
970 | loc = (unsigned int *)sec->data->d_buf + idx; | |
971 | memset(loc, 0, sizeof(unsigned int)); | |
972 | ||
973 | if (elf_add_reloc_to_insn(file->elf, sec, | |
974 | idx * sizeof(unsigned int), | |
975 | R_X86_64_PC32, | |
976 | s, sym->offset)) | |
977 | return -1; | |
978 | ||
979 | idx++; | |
980 | } | |
981 | } | |
982 | ||
983 | return 0; | |
984 | } | |
985 | ||
99d00215 PZ |
986 | static int create_mcount_loc_sections(struct objtool_file *file) |
987 | { | |
86ea7f36 | 988 | int addrsize = elf_class_addrsize(file->elf); |
99d00215 | 989 | struct instruction *insn; |
86ea7f36 | 990 | struct section *sec; |
99d00215 PZ |
991 | int idx; |
992 | ||
993 | sec = find_section_by_name(file->elf, "__mcount_loc"); | |
994 | if (sec) { | |
995 | INIT_LIST_HEAD(&file->mcount_loc_list); | |
996 | WARN("file already has __mcount_loc section, skipping"); | |
997 | return 0; | |
998 | } | |
999 | ||
1000 | if (list_empty(&file->mcount_loc_list)) | |
1001 | return 0; | |
1002 | ||
1003 | idx = 0; | |
c509331b | 1004 | list_for_each_entry(insn, &file->mcount_loc_list, call_node) |
99d00215 PZ |
1005 | idx++; |
1006 | ||
86ea7f36 | 1007 | sec = elf_create_section(file->elf, "__mcount_loc", 0, addrsize, idx); |
99d00215 PZ |
1008 | if (!sec) |
1009 | return -1; | |
1010 | ||
86ea7f36 CL |
1011 | sec->sh.sh_addralign = addrsize; |
1012 | ||
99d00215 | 1013 | idx = 0; |
c509331b | 1014 | list_for_each_entry(insn, &file->mcount_loc_list, call_node) { |
86ea7f36 | 1015 | void *loc; |
99d00215 | 1016 | |
86ea7f36 CL |
1017 | loc = sec->data->d_buf + idx; |
1018 | memset(loc, 0, addrsize); | |
99d00215 | 1019 | |
86ea7f36 | 1020 | if (elf_add_reloc_to_insn(file->elf, sec, idx, |
c1449735 | 1021 | addrsize == sizeof(u64) ? R_ABS64 : R_ABS32, |
ef47cc01 | 1022 | insn->sec, insn->offset)) |
99d00215 | 1023 | return -1; |
99d00215 | 1024 | |
86ea7f36 | 1025 | idx += addrsize; |
99d00215 PZ |
1026 | } |
1027 | ||
99d00215 PZ |
1028 | return 0; |
1029 | } | |
1030 | ||
00abd384 PZ |
1031 | static int create_direct_call_sections(struct objtool_file *file) |
1032 | { | |
1033 | struct instruction *insn; | |
1034 | struct section *sec; | |
1035 | unsigned int *loc; | |
1036 | int idx; | |
1037 | ||
1038 | sec = find_section_by_name(file->elf, ".call_sites"); | |
1039 | if (sec) { | |
1040 | INIT_LIST_HEAD(&file->call_list); | |
1041 | WARN("file already has .call_sites section, skipping"); | |
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | if (list_empty(&file->call_list)) | |
1046 | return 0; | |
1047 | ||
1048 | idx = 0; | |
1049 | list_for_each_entry(insn, &file->call_list, call_node) | |
1050 | idx++; | |
1051 | ||
1052 | sec = elf_create_section(file->elf, ".call_sites", 0, sizeof(unsigned int), idx); | |
1053 | if (!sec) | |
1054 | return -1; | |
1055 | ||
1056 | idx = 0; | |
1057 | list_for_each_entry(insn, &file->call_list, call_node) { | |
1058 | ||
1059 | loc = (unsigned int *)sec->data->d_buf + idx; | |
1060 | memset(loc, 0, sizeof(unsigned int)); | |
1061 | ||
1062 | if (elf_add_reloc_to_insn(file->elf, sec, | |
1063 | idx * sizeof(unsigned int), | |
1064 | R_X86_64_PC32, | |
1065 | insn->sec, insn->offset)) | |
1066 | return -1; | |
1067 | ||
1068 | idx++; | |
1069 | } | |
1070 | ||
1071 | return 0; | |
1072 | } | |
1073 | ||
dcc914f4 JP |
1074 | /* |
1075 | * Warnings shouldn't be reported for ignored functions. | |
1076 | */ | |
1077 | static void add_ignores(struct objtool_file *file) | |
1078 | { | |
1079 | struct instruction *insn; | |
1080 | struct section *sec; | |
1081 | struct symbol *func; | |
f1974222 | 1082 | struct reloc *reloc; |
dcc914f4 | 1083 | |
aaf5c623 PZ |
1084 | sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); |
1085 | if (!sec) | |
1086 | return; | |
dcc914f4 | 1087 | |
f1974222 MH |
1088 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
1089 | switch (reloc->sym->type) { | |
aaf5c623 | 1090 | case STT_FUNC: |
f1974222 | 1091 | func = reloc->sym; |
aaf5c623 PZ |
1092 | break; |
1093 | ||
1094 | case STT_SECTION: | |
f1974222 | 1095 | func = find_func_by_offset(reloc->sym->sec, reloc->addend); |
7acfe531 | 1096 | if (!func) |
dcc914f4 | 1097 | continue; |
aaf5c623 | 1098 | break; |
dcc914f4 | 1099 | |
aaf5c623 | 1100 | default: |
f1974222 | 1101 | WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type); |
aaf5c623 | 1102 | continue; |
dcc914f4 | 1103 | } |
aaf5c623 | 1104 | |
f0f70adb | 1105 | func_for_each_insn(file, func, insn) |
aaf5c623 | 1106 | insn->ignore = true; |
dcc914f4 JP |
1107 | } |
1108 | } | |
1109 | ||
ea24213d PZ |
1110 | /* |
1111 | * This is a whitelist of functions that is allowed to be called with AC set. | |
1112 | * The list is meant to be minimal and only contains compiler instrumentation | |
1113 | * ABI and a few functions used to implement *_{to,from}_user() functions. | |
1114 | * | |
1115 | * These functions must not directly change AC, but may PUSHF/POPF. | |
1116 | */ | |
1117 | static const char *uaccess_safe_builtin[] = { | |
1118 | /* KASAN */ | |
1119 | "kasan_report", | |
f00748bf | 1120 | "kasan_check_range", |
ea24213d PZ |
1121 | /* KASAN out-of-line */ |
1122 | "__asan_loadN_noabort", | |
1123 | "__asan_load1_noabort", | |
1124 | "__asan_load2_noabort", | |
1125 | "__asan_load4_noabort", | |
1126 | "__asan_load8_noabort", | |
1127 | "__asan_load16_noabort", | |
1128 | "__asan_storeN_noabort", | |
1129 | "__asan_store1_noabort", | |
1130 | "__asan_store2_noabort", | |
1131 | "__asan_store4_noabort", | |
1132 | "__asan_store8_noabort", | |
1133 | "__asan_store16_noabort", | |
b0b8e56b JH |
1134 | "__kasan_check_read", |
1135 | "__kasan_check_write", | |
ea24213d PZ |
1136 | /* KASAN in-line */ |
1137 | "__asan_report_load_n_noabort", | |
1138 | "__asan_report_load1_noabort", | |
1139 | "__asan_report_load2_noabort", | |
1140 | "__asan_report_load4_noabort", | |
1141 | "__asan_report_load8_noabort", | |
1142 | "__asan_report_load16_noabort", | |
1143 | "__asan_report_store_n_noabort", | |
1144 | "__asan_report_store1_noabort", | |
1145 | "__asan_report_store2_noabort", | |
1146 | "__asan_report_store4_noabort", | |
1147 | "__asan_report_store8_noabort", | |
1148 | "__asan_report_store16_noabort", | |
5f5c9712 | 1149 | /* KCSAN */ |
9967683c | 1150 | "__kcsan_check_access", |
0525bd82 ME |
1151 | "__kcsan_mb", |
1152 | "__kcsan_wmb", | |
1153 | "__kcsan_rmb", | |
1154 | "__kcsan_release", | |
5f5c9712 ME |
1155 | "kcsan_found_watchpoint", |
1156 | "kcsan_setup_watchpoint", | |
9967683c | 1157 | "kcsan_check_scoped_accesses", |
50a19ad4 ME |
1158 | "kcsan_disable_current", |
1159 | "kcsan_enable_current_nowarn", | |
5f5c9712 ME |
1160 | /* KCSAN/TSAN */ |
1161 | "__tsan_func_entry", | |
1162 | "__tsan_func_exit", | |
1163 | "__tsan_read_range", | |
1164 | "__tsan_write_range", | |
1165 | "__tsan_read1", | |
1166 | "__tsan_read2", | |
1167 | "__tsan_read4", | |
1168 | "__tsan_read8", | |
1169 | "__tsan_read16", | |
1170 | "__tsan_write1", | |
1171 | "__tsan_write2", | |
1172 | "__tsan_write4", | |
1173 | "__tsan_write8", | |
1174 | "__tsan_write16", | |
a81b3759 ME |
1175 | "__tsan_read_write1", |
1176 | "__tsan_read_write2", | |
1177 | "__tsan_read_write4", | |
1178 | "__tsan_read_write8", | |
1179 | "__tsan_read_write16", | |
63646fcb ME |
1180 | "__tsan_volatile_read1", |
1181 | "__tsan_volatile_read2", | |
1182 | "__tsan_volatile_read4", | |
1183 | "__tsan_volatile_read8", | |
1184 | "__tsan_volatile_read16", | |
1185 | "__tsan_volatile_write1", | |
1186 | "__tsan_volatile_write2", | |
1187 | "__tsan_volatile_write4", | |
1188 | "__tsan_volatile_write8", | |
1189 | "__tsan_volatile_write16", | |
883957b1 ME |
1190 | "__tsan_atomic8_load", |
1191 | "__tsan_atomic16_load", | |
1192 | "__tsan_atomic32_load", | |
1193 | "__tsan_atomic64_load", | |
1194 | "__tsan_atomic8_store", | |
1195 | "__tsan_atomic16_store", | |
1196 | "__tsan_atomic32_store", | |
1197 | "__tsan_atomic64_store", | |
1198 | "__tsan_atomic8_exchange", | |
1199 | "__tsan_atomic16_exchange", | |
1200 | "__tsan_atomic32_exchange", | |
1201 | "__tsan_atomic64_exchange", | |
1202 | "__tsan_atomic8_fetch_add", | |
1203 | "__tsan_atomic16_fetch_add", | |
1204 | "__tsan_atomic32_fetch_add", | |
1205 | "__tsan_atomic64_fetch_add", | |
1206 | "__tsan_atomic8_fetch_sub", | |
1207 | "__tsan_atomic16_fetch_sub", | |
1208 | "__tsan_atomic32_fetch_sub", | |
1209 | "__tsan_atomic64_fetch_sub", | |
1210 | "__tsan_atomic8_fetch_and", | |
1211 | "__tsan_atomic16_fetch_and", | |
1212 | "__tsan_atomic32_fetch_and", | |
1213 | "__tsan_atomic64_fetch_and", | |
1214 | "__tsan_atomic8_fetch_or", | |
1215 | "__tsan_atomic16_fetch_or", | |
1216 | "__tsan_atomic32_fetch_or", | |
1217 | "__tsan_atomic64_fetch_or", | |
1218 | "__tsan_atomic8_fetch_xor", | |
1219 | "__tsan_atomic16_fetch_xor", | |
1220 | "__tsan_atomic32_fetch_xor", | |
1221 | "__tsan_atomic64_fetch_xor", | |
1222 | "__tsan_atomic8_fetch_nand", | |
1223 | "__tsan_atomic16_fetch_nand", | |
1224 | "__tsan_atomic32_fetch_nand", | |
1225 | "__tsan_atomic64_fetch_nand", | |
1226 | "__tsan_atomic8_compare_exchange_strong", | |
1227 | "__tsan_atomic16_compare_exchange_strong", | |
1228 | "__tsan_atomic32_compare_exchange_strong", | |
1229 | "__tsan_atomic64_compare_exchange_strong", | |
1230 | "__tsan_atomic8_compare_exchange_weak", | |
1231 | "__tsan_atomic16_compare_exchange_weak", | |
1232 | "__tsan_atomic32_compare_exchange_weak", | |
1233 | "__tsan_atomic64_compare_exchange_weak", | |
1234 | "__tsan_atomic8_compare_exchange_val", | |
1235 | "__tsan_atomic16_compare_exchange_val", | |
1236 | "__tsan_atomic32_compare_exchange_val", | |
1237 | "__tsan_atomic64_compare_exchange_val", | |
1238 | "__tsan_atomic_thread_fence", | |
1239 | "__tsan_atomic_signal_fence", | |
d5d46924 AB |
1240 | "__tsan_unaligned_read16", |
1241 | "__tsan_unaligned_write16", | |
ea24213d PZ |
1242 | /* KCOV */ |
1243 | "write_comp_data", | |
ae033f08 | 1244 | "check_kcov_mode", |
ea24213d PZ |
1245 | "__sanitizer_cov_trace_pc", |
1246 | "__sanitizer_cov_trace_const_cmp1", | |
1247 | "__sanitizer_cov_trace_const_cmp2", | |
1248 | "__sanitizer_cov_trace_const_cmp4", | |
1249 | "__sanitizer_cov_trace_const_cmp8", | |
1250 | "__sanitizer_cov_trace_cmp1", | |
1251 | "__sanitizer_cov_trace_cmp2", | |
1252 | "__sanitizer_cov_trace_cmp4", | |
1253 | "__sanitizer_cov_trace_cmp8", | |
36b1c700 | 1254 | "__sanitizer_cov_trace_switch", |
40b22c9d AP |
1255 | /* KMSAN */ |
1256 | "kmsan_copy_to_user", | |
1257 | "kmsan_report", | |
1258 | "kmsan_unpoison_entry_regs", | |
1259 | "kmsan_unpoison_memory", | |
1260 | "__msan_chain_origin", | |
1261 | "__msan_get_context_state", | |
1262 | "__msan_instrument_asm_store", | |
1263 | "__msan_metadata_ptr_for_load_1", | |
1264 | "__msan_metadata_ptr_for_load_2", | |
1265 | "__msan_metadata_ptr_for_load_4", | |
1266 | "__msan_metadata_ptr_for_load_8", | |
1267 | "__msan_metadata_ptr_for_load_n", | |
1268 | "__msan_metadata_ptr_for_store_1", | |
1269 | "__msan_metadata_ptr_for_store_2", | |
1270 | "__msan_metadata_ptr_for_store_4", | |
1271 | "__msan_metadata_ptr_for_store_8", | |
1272 | "__msan_metadata_ptr_for_store_n", | |
1273 | "__msan_poison_alloca", | |
1274 | "__msan_warning", | |
ea24213d PZ |
1275 | /* UBSAN */ |
1276 | "ubsan_type_mismatch_common", | |
1277 | "__ubsan_handle_type_mismatch", | |
1278 | "__ubsan_handle_type_mismatch_v1", | |
9a50dcaf | 1279 | "__ubsan_handle_shift_out_of_bounds", |
f18b0d7e | 1280 | "__ubsan_handle_load_invalid_value", |
ea24213d PZ |
1281 | /* misc */ |
1282 | "csum_partial_copy_generic", | |
ec6347bb DW |
1283 | "copy_mc_fragile", |
1284 | "copy_mc_fragile_handle_tail", | |
5da8e4a6 | 1285 | "copy_mc_enhanced_fast_string", |
ea24213d | 1286 | "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */ |
0db7058e | 1287 | "clear_user_original", |
ea24213d PZ |
1288 | NULL |
1289 | }; | |
1290 | ||
1291 | static void add_uaccess_safe(struct objtool_file *file) | |
1292 | { | |
1293 | struct symbol *func; | |
1294 | const char **name; | |
1295 | ||
2daf7fab | 1296 | if (!opts.uaccess) |
ea24213d PZ |
1297 | return; |
1298 | ||
1299 | for (name = uaccess_safe_builtin; *name; name++) { | |
1300 | func = find_symbol_by_name(file->elf, *name); | |
1301 | if (!func) | |
1302 | continue; | |
1303 | ||
e10cd8fe | 1304 | func->uaccess_safe = true; |
dcc914f4 JP |
1305 | } |
1306 | } | |
1307 | ||
258c7605 JP |
1308 | /* |
1309 | * FIXME: For now, just ignore any alternatives which add retpolines. This is | |
1310 | * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline. | |
1311 | * But it at least allows objtool to understand the control flow *around* the | |
1312 | * retpoline. | |
1313 | */ | |
ff05ab23 | 1314 | static int add_ignore_alternatives(struct objtool_file *file) |
258c7605 JP |
1315 | { |
1316 | struct section *sec; | |
f1974222 | 1317 | struct reloc *reloc; |
258c7605 JP |
1318 | struct instruction *insn; |
1319 | ||
ff05ab23 | 1320 | sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts"); |
258c7605 JP |
1321 | if (!sec) |
1322 | return 0; | |
1323 | ||
f1974222 MH |
1324 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
1325 | if (reloc->sym->type != STT_SECTION) { | |
258c7605 JP |
1326 | WARN("unexpected relocation symbol type in %s", sec->name); |
1327 | return -1; | |
1328 | } | |
1329 | ||
f1974222 | 1330 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
258c7605 | 1331 | if (!insn) { |
ff05ab23 | 1332 | WARN("bad .discard.ignore_alts entry"); |
258c7605 JP |
1333 | return -1; |
1334 | } | |
1335 | ||
1336 | insn->ignore_alts = true; | |
1337 | } | |
1338 | ||
1339 | return 0; | |
1340 | } | |
1341 | ||
530b4ddd PZ |
1342 | __weak bool arch_is_retpoline(struct symbol *sym) |
1343 | { | |
1344 | return false; | |
1345 | } | |
1346 | ||
d9e9d230 PZ |
1347 | __weak bool arch_is_rethunk(struct symbol *sym) |
1348 | { | |
1349 | return false; | |
1350 | } | |
1351 | ||
7bd2a600 PZ |
1352 | static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn) |
1353 | { | |
0932dbe1 PZ |
1354 | struct reloc *reloc; |
1355 | ||
1356 | if (insn->no_reloc) | |
7bd2a600 PZ |
1357 | return NULL; |
1358 | ||
0932dbe1 PZ |
1359 | if (!file) |
1360 | return NULL; | |
db2b0c5d | 1361 | |
0932dbe1 PZ |
1362 | reloc = find_reloc_by_dest_range(file->elf, insn->sec, |
1363 | insn->offset, insn->len); | |
1364 | if (!reloc) { | |
1365 | insn->no_reloc = 1; | |
1366 | return NULL; | |
7bd2a600 PZ |
1367 | } |
1368 | ||
0932dbe1 | 1369 | return reloc; |
7bd2a600 PZ |
1370 | } |
1371 | ||
f56dae88 PZ |
1372 | static void remove_insn_ops(struct instruction *insn) |
1373 | { | |
3ee88df1 | 1374 | struct stack_op *op, *next; |
f56dae88 | 1375 | |
3ee88df1 PZ |
1376 | for (op = insn->stack_ops; op; op = next) { |
1377 | next = op->next; | |
f56dae88 PZ |
1378 | free(op); |
1379 | } | |
3ee88df1 | 1380 | insn->stack_ops = NULL; |
f56dae88 PZ |
1381 | } |
1382 | ||
dd003ede PZ |
1383 | static void annotate_call_site(struct objtool_file *file, |
1384 | struct instruction *insn, bool sibling) | |
f56dae88 PZ |
1385 | { |
1386 | struct reloc *reloc = insn_reloc(file, insn); | |
c6f5dc28 | 1387 | struct symbol *sym = insn_call_dest(insn); |
f56dae88 | 1388 | |
dd003ede PZ |
1389 | if (!sym) |
1390 | sym = reloc->sym; | |
1391 | ||
1392 | /* | |
1393 | * Alternative replacement code is just template code which is | |
1394 | * sometimes copied to the original instruction. For now, don't | |
1395 | * annotate it. (In the future we might consider annotating the | |
1396 | * original instruction if/when it ever makes sense to do so.) | |
1397 | */ | |
1398 | if (!strcmp(insn->sec->name, ".altinstr_replacement")) | |
f56dae88 PZ |
1399 | return; |
1400 | ||
dd003ede PZ |
1401 | if (sym->static_call_tramp) { |
1402 | list_add_tail(&insn->call_node, &file->static_call_list); | |
1403 | return; | |
f56dae88 PZ |
1404 | } |
1405 | ||
134ab5bd PZ |
1406 | if (sym->retpoline_thunk) { |
1407 | list_add_tail(&insn->call_node, &file->retpoline_call_list); | |
1408 | return; | |
1409 | } | |
1410 | ||
f56dae88 | 1411 | /* |
05098119 ME |
1412 | * Many compilers cannot disable KCOV or sanitizer calls with a function |
1413 | * attribute so they need a little help, NOP out any such calls from | |
1414 | * noinstr text. | |
f56dae88 | 1415 | */ |
22102f45 | 1416 | if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) { |
f56dae88 PZ |
1417 | if (reloc) { |
1418 | reloc->type = R_NONE; | |
1419 | elf_write_reloc(file->elf, reloc); | |
1420 | } | |
1421 | ||
1422 | elf_write_insn(file->elf, insn->sec, | |
1423 | insn->offset, insn->len, | |
1424 | sibling ? arch_ret_insn(insn->len) | |
1425 | : arch_nop_insn(insn->len)); | |
1426 | ||
1427 | insn->type = sibling ? INSN_RETURN : INSN_NOP; | |
7a53f408 PZ |
1428 | |
1429 | if (sibling) { | |
1430 | /* | |
1431 | * We've replaced the tail-call JMP insn by two new | |
1432 | * insn: RET; INT3, except we only have a single struct | |
1433 | * insn here. Mark it retpoline_safe to avoid the SLS | |
1434 | * warning, instead of adding another insn. | |
1435 | */ | |
1436 | insn->retpoline_safe = true; | |
1437 | } | |
1438 | ||
dd003ede | 1439 | return; |
f56dae88 PZ |
1440 | } |
1441 | ||
2daf7fab | 1442 | if (opts.mcount && sym->fentry) { |
f56dae88 PZ |
1443 | if (sibling) |
1444 | WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset); | |
280981d6 SV |
1445 | if (opts.mnop) { |
1446 | if (reloc) { | |
1447 | reloc->type = R_NONE; | |
1448 | elf_write_reloc(file->elf, reloc); | |
1449 | } | |
f56dae88 | 1450 | |
280981d6 SV |
1451 | elf_write_insn(file->elf, insn->sec, |
1452 | insn->offset, insn->len, | |
1453 | arch_nop_insn(insn->len)); | |
f56dae88 | 1454 | |
280981d6 SV |
1455 | insn->type = INSN_NOP; |
1456 | } | |
f56dae88 | 1457 | |
c509331b | 1458 | list_add_tail(&insn->call_node, &file->mcount_loc_list); |
dd003ede | 1459 | return; |
f56dae88 | 1460 | } |
0e5b613b | 1461 | |
00abd384 PZ |
1462 | if (insn->type == INSN_CALL && !insn->sec->init) |
1463 | list_add_tail(&insn->call_node, &file->call_list); | |
1464 | ||
0e5b613b PZ |
1465 | if (!sibling && dead_end_function(file, sym)) |
1466 | insn->dead_end = true; | |
dd003ede PZ |
1467 | } |
1468 | ||
1469 | static void add_call_dest(struct objtool_file *file, struct instruction *insn, | |
1470 | struct symbol *dest, bool sibling) | |
1471 | { | |
c6f5dc28 | 1472 | insn->_call_dest = dest; |
dd003ede PZ |
1473 | if (!dest) |
1474 | return; | |
f56dae88 PZ |
1475 | |
1476 | /* | |
1477 | * Whatever stack impact regular CALLs have, should be undone | |
1478 | * by the RETURN of the called function. | |
1479 | * | |
1480 | * Annotated intra-function calls retain the stack_ops but | |
1481 | * are converted to JUMP, see read_intra_function_calls(). | |
1482 | */ | |
1483 | remove_insn_ops(insn); | |
dd003ede PZ |
1484 | |
1485 | annotate_call_site(file, insn, sibling); | |
f56dae88 PZ |
1486 | } |
1487 | ||
134ab5bd PZ |
1488 | static void add_retpoline_call(struct objtool_file *file, struct instruction *insn) |
1489 | { | |
1490 | /* | |
1491 | * Retpoline calls/jumps are really dynamic calls/jumps in disguise, | |
1492 | * so convert them accordingly. | |
1493 | */ | |
1494 | switch (insn->type) { | |
1495 | case INSN_CALL: | |
1496 | insn->type = INSN_CALL_DYNAMIC; | |
1497 | break; | |
1498 | case INSN_JUMP_UNCONDITIONAL: | |
1499 | insn->type = INSN_JUMP_DYNAMIC; | |
1500 | break; | |
1501 | case INSN_JUMP_CONDITIONAL: | |
1502 | insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL; | |
1503 | break; | |
1504 | default: | |
1505 | return; | |
1506 | } | |
1507 | ||
1508 | insn->retpoline_safe = true; | |
1509 | ||
1510 | /* | |
1511 | * Whatever stack impact regular CALLs have, should be undone | |
1512 | * by the RETURN of the called function. | |
1513 | * | |
1514 | * Annotated intra-function calls retain the stack_ops but | |
1515 | * are converted to JUMP, see read_intra_function_calls(). | |
1516 | */ | |
1517 | remove_insn_ops(insn); | |
1518 | ||
1519 | annotate_call_site(file, insn, false); | |
1520 | } | |
08f87a93 | 1521 | |
a149180f | 1522 | static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add) |
d9e9d230 PZ |
1523 | { |
1524 | /* | |
1525 | * Return thunk tail calls are really just returns in disguise, | |
1526 | * so convert them accordingly. | |
1527 | */ | |
1528 | insn->type = INSN_RETURN; | |
1529 | insn->retpoline_safe = true; | |
1530 | ||
a149180f PZ |
1531 | if (add) |
1532 | list_add_tail(&insn->call_node, &file->return_thunk_list); | |
d9e9d230 PZ |
1533 | } |
1534 | ||
5a9c361a PZ |
1535 | static bool is_first_func_insn(struct objtool_file *file, |
1536 | struct instruction *insn, struct symbol *sym) | |
08f87a93 | 1537 | { |
5a9c361a | 1538 | if (insn->offset == sym->offset) |
d139bca4 PZ |
1539 | return true; |
1540 | ||
5a9c361a | 1541 | /* Allow direct CALL/JMP past ENDBR */ |
2daf7fab | 1542 | if (opts.ibt) { |
d139bca4 PZ |
1543 | struct instruction *prev = prev_insn_same_sym(file, insn); |
1544 | ||
1545 | if (prev && prev->type == INSN_ENDBR && | |
5a9c361a | 1546 | insn->offset == sym->offset + prev->len) |
d139bca4 PZ |
1547 | return true; |
1548 | } | |
1549 | ||
1550 | return false; | |
08f87a93 PZ |
1551 | } |
1552 | ||
5a9c361a PZ |
1553 | /* |
1554 | * A sibling call is a tail-call to another symbol -- to differentiate from a | |
1555 | * recursive tail-call which is to the same symbol. | |
1556 | */ | |
1557 | static bool jump_is_sibling_call(struct objtool_file *file, | |
1558 | struct instruction *from, struct instruction *to) | |
1559 | { | |
1560 | struct symbol *fs = from->sym; | |
1561 | struct symbol *ts = to->sym; | |
1562 | ||
1563 | /* Not a sibling call if from/to a symbol hole */ | |
1564 | if (!fs || !ts) | |
1565 | return false; | |
1566 | ||
1567 | /* Not a sibling call if not targeting the start of a symbol. */ | |
1568 | if (!is_first_func_insn(file, to, ts)) | |
1569 | return false; | |
1570 | ||
1571 | /* Disallow sibling calls into STT_NOTYPE */ | |
1572 | if (ts->type == STT_NOTYPE) | |
1573 | return false; | |
1574 | ||
1575 | /* Must not be self to be a sibling */ | |
1576 | return fs->pfunc != ts->pfunc; | |
1577 | } | |
1578 | ||
dcc914f4 JP |
1579 | /* |
1580 | * Find the destination instructions for all jumps. | |
1581 | */ | |
1582 | static int add_jump_destinations(struct objtool_file *file) | |
1583 | { | |
26ff6041 | 1584 | struct instruction *insn, *jump_dest; |
f1974222 | 1585 | struct reloc *reloc; |
dcc914f4 JP |
1586 | struct section *dest_sec; |
1587 | unsigned long dest_off; | |
1588 | ||
1589 | for_each_insn(file, insn) { | |
34c861e8 JP |
1590 | if (insn->jump_dest) { |
1591 | /* | |
1592 | * handle_group_alt() may have previously set | |
1593 | * 'jump_dest' for some alternatives. | |
1594 | */ | |
1595 | continue; | |
1596 | } | |
a2296140 | 1597 | if (!is_static_jump(insn)) |
dcc914f4 JP |
1598 | continue; |
1599 | ||
7bd2a600 | 1600 | reloc = insn_reloc(file, insn); |
f1974222 | 1601 | if (!reloc) { |
dcc914f4 | 1602 | dest_sec = insn->sec; |
bfb08f22 | 1603 | dest_off = arch_jump_destination(insn); |
f1974222 MH |
1604 | } else if (reloc->sym->type == STT_SECTION) { |
1605 | dest_sec = reloc->sym->sec; | |
1606 | dest_off = arch_dest_reloc_offset(reloc->addend); | |
1739c66e | 1607 | } else if (reloc->sym->retpoline_thunk) { |
134ab5bd | 1608 | add_retpoline_call(file, insn); |
39b73533 | 1609 | continue; |
d9e9d230 | 1610 | } else if (reloc->sym->return_thunk) { |
a149180f | 1611 | add_return_call(file, insn, true); |
d9e9d230 | 1612 | continue; |
dbcdbdfd | 1613 | } else if (insn_func(insn)) { |
26ff6041 JP |
1614 | /* |
1615 | * External sibling call or internal sibling call with | |
1616 | * STT_FUNC reloc. | |
1617 | */ | |
f56dae88 | 1618 | add_call_dest(file, insn, reloc->sym, true); |
dcc914f4 | 1619 | continue; |
ecf11ba4 JP |
1620 | } else if (reloc->sym->sec->idx) { |
1621 | dest_sec = reloc->sym->sec; | |
1622 | dest_off = reloc->sym->sym.st_value + | |
1623 | arch_dest_reloc_offset(reloc->addend); | |
1624 | } else { | |
1625 | /* non-func asm code jumping to another file */ | |
1626 | continue; | |
dcc914f4 JP |
1627 | } |
1628 | ||
26ff6041 JP |
1629 | jump_dest = find_insn(file, dest_sec, dest_off); |
1630 | if (!jump_dest) { | |
a149180f PZ |
1631 | struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off); |
1632 | ||
1633 | /* | |
1634 | * This is a special case for zen_untrain_ret(). | |
1635 | * It jumps to __x86_return_thunk(), but objtool | |
1636 | * can't find the thunk's starting RET | |
1637 | * instruction, because the RET is also in the | |
1638 | * middle of another instruction. Objtool only | |
1639 | * knows about the outer instruction. | |
1640 | */ | |
1641 | if (sym && sym->return_thunk) { | |
1642 | add_return_call(file, insn, false); | |
1643 | continue; | |
1644 | } | |
1645 | ||
dcc914f4 JP |
1646 | WARN_FUNC("can't find jump dest instruction at %s+0x%lx", |
1647 | insn->sec, insn->offset, dest_sec->name, | |
1648 | dest_off); | |
1649 | return -1; | |
1650 | } | |
cd77849a JP |
1651 | |
1652 | /* | |
54262aa2 | 1653 | * Cross-function jump. |
cd77849a | 1654 | */ |
dbcdbdfd PZ |
1655 | if (insn_func(insn) && insn_func(jump_dest) && |
1656 | insn_func(insn) != insn_func(jump_dest)) { | |
54262aa2 PZ |
1657 | |
1658 | /* | |
1659 | * For GCC 8+, create parent/child links for any cold | |
1660 | * subfunctions. This is _mostly_ redundant with a | |
1661 | * similar initialization in read_symbols(). | |
1662 | * | |
1663 | * If a function has aliases, we want the *first* such | |
1664 | * function in the symbol table to be the subfunction's | |
1665 | * parent. In that case we overwrite the | |
1666 | * initialization done in read_symbols(). | |
1667 | * | |
1668 | * However this code can't completely replace the | |
1669 | * read_symbols() code because this doesn't detect the | |
1670 | * case where the parent function's only reference to a | |
e7c2bc37 | 1671 | * subfunction is through a jump table. |
54262aa2 | 1672 | */ |
dbcdbdfd PZ |
1673 | if (!strstr(insn_func(insn)->name, ".cold") && |
1674 | strstr(insn_func(jump_dest)->name, ".cold")) { | |
1675 | insn_func(insn)->cfunc = insn_func(jump_dest); | |
1676 | insn_func(jump_dest)->pfunc = insn_func(insn); | |
54262aa2 | 1677 | } |
cd77849a | 1678 | } |
26ff6041 | 1679 | |
5a9c361a PZ |
1680 | if (jump_is_sibling_call(file, insn, jump_dest)) { |
1681 | /* | |
1682 | * Internal sibling call without reloc or with | |
1683 | * STT_SECTION reloc. | |
1684 | */ | |
1685 | add_call_dest(file, insn, insn_func(jump_dest), true); | |
1686 | continue; | |
1687 | } | |
1688 | ||
26ff6041 | 1689 | insn->jump_dest = jump_dest; |
dcc914f4 JP |
1690 | } |
1691 | ||
1692 | return 0; | |
1693 | } | |
1694 | ||
2b232a22 JT |
1695 | static struct symbol *find_call_destination(struct section *sec, unsigned long offset) |
1696 | { | |
1697 | struct symbol *call_dest; | |
1698 | ||
1699 | call_dest = find_func_by_offset(sec, offset); | |
1700 | if (!call_dest) | |
1701 | call_dest = find_symbol_by_offset(sec, offset); | |
1702 | ||
1703 | return call_dest; | |
1704 | } | |
1705 | ||
dcc914f4 JP |
1706 | /* |
1707 | * Find the destination instructions for all calls. | |
1708 | */ | |
1709 | static int add_call_destinations(struct objtool_file *file) | |
1710 | { | |
1711 | struct instruction *insn; | |
1712 | unsigned long dest_off; | |
f56dae88 | 1713 | struct symbol *dest; |
f1974222 | 1714 | struct reloc *reloc; |
dcc914f4 JP |
1715 | |
1716 | for_each_insn(file, insn) { | |
1717 | if (insn->type != INSN_CALL) | |
1718 | continue; | |
1719 | ||
7bd2a600 | 1720 | reloc = insn_reloc(file, insn); |
f1974222 | 1721 | if (!reloc) { |
bfb08f22 | 1722 | dest_off = arch_jump_destination(insn); |
f56dae88 PZ |
1723 | dest = find_call_destination(insn->sec, dest_off); |
1724 | ||
1725 | add_call_dest(file, insn, dest, false); | |
a845c7cf | 1726 | |
7acfe531 JP |
1727 | if (insn->ignore) |
1728 | continue; | |
1729 | ||
c6f5dc28 | 1730 | if (!insn_call_dest(insn)) { |
8aa8eb2a | 1731 | WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset); |
dcc914f4 JP |
1732 | return -1; |
1733 | } | |
a845c7cf | 1734 | |
c6f5dc28 | 1735 | if (insn_func(insn) && insn_call_dest(insn)->type != STT_FUNC) { |
7acfe531 JP |
1736 | WARN_FUNC("unsupported call to non-function", |
1737 | insn->sec, insn->offset); | |
1738 | return -1; | |
1739 | } | |
1740 | ||
f1974222 MH |
1741 | } else if (reloc->sym->type == STT_SECTION) { |
1742 | dest_off = arch_dest_reloc_offset(reloc->addend); | |
f56dae88 PZ |
1743 | dest = find_call_destination(reloc->sym->sec, dest_off); |
1744 | if (!dest) { | |
bfb08f22 | 1745 | WARN_FUNC("can't find call dest symbol at %s+0x%lx", |
dcc914f4 | 1746 | insn->sec, insn->offset, |
f1974222 | 1747 | reloc->sym->sec->name, |
bfb08f22 | 1748 | dest_off); |
dcc914f4 JP |
1749 | return -1; |
1750 | } | |
bcb1b6ff | 1751 | |
f56dae88 PZ |
1752 | add_call_dest(file, insn, dest, false); |
1753 | ||
1739c66e | 1754 | } else if (reloc->sym->retpoline_thunk) { |
134ab5bd | 1755 | add_retpoline_call(file, insn); |
bcb1b6ff | 1756 | |
dcc914f4 | 1757 | } else |
f56dae88 | 1758 | add_call_dest(file, insn, reloc->sym, false); |
dcc914f4 JP |
1759 | } |
1760 | ||
1761 | return 0; | |
1762 | } | |
1763 | ||
1764 | /* | |
c9c324dc JP |
1765 | * The .alternatives section requires some extra special care over and above |
1766 | * other special sections because alternatives are patched in place. | |
dcc914f4 JP |
1767 | */ |
1768 | static int handle_group_alt(struct objtool_file *file, | |
1769 | struct special_alt *special_alt, | |
1770 | struct instruction *orig_insn, | |
1771 | struct instruction **new_insn) | |
1772 | { | |
a706bb08 | 1773 | struct instruction *last_new_insn = NULL, *insn, *nop = NULL; |
b23cc71c | 1774 | struct alt_group *orig_alt_group, *new_alt_group; |
dcc914f4 JP |
1775 | unsigned long dest_off; |
1776 | ||
a706bb08 | 1777 | orig_alt_group = orig_insn->alt_group; |
b23cc71c | 1778 | if (!orig_alt_group) { |
a706bb08 | 1779 | struct instruction *last_orig_insn = NULL; |
c9c324dc | 1780 | |
a706bb08 PZ |
1781 | orig_alt_group = malloc(sizeof(*orig_alt_group)); |
1782 | if (!orig_alt_group) { | |
1783 | WARN("malloc failed"); | |
1784 | return -1; | |
1785 | } | |
1786 | orig_alt_group->cfi = calloc(special_alt->orig_len, | |
1787 | sizeof(struct cfi_state *)); | |
1788 | if (!orig_alt_group->cfi) { | |
1789 | WARN("calloc failed"); | |
1790 | return -1; | |
1791 | } | |
dcc914f4 | 1792 | |
a706bb08 PZ |
1793 | insn = orig_insn; |
1794 | sec_for_each_insn_from(file, insn) { | |
1795 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) | |
1796 | break; | |
dcc914f4 | 1797 | |
a706bb08 PZ |
1798 | insn->alt_group = orig_alt_group; |
1799 | last_orig_insn = insn; | |
1800 | } | |
1801 | orig_alt_group->orig_group = NULL; | |
1802 | orig_alt_group->first_insn = orig_insn; | |
1803 | orig_alt_group->last_insn = last_orig_insn; | |
1c34496e | 1804 | orig_alt_group->nop = NULL; |
a706bb08 PZ |
1805 | } else { |
1806 | if (orig_alt_group->last_insn->offset + orig_alt_group->last_insn->len - | |
1807 | orig_alt_group->first_insn->offset != special_alt->orig_len) { | |
1808 | WARN_FUNC("weirdly overlapping alternative! %ld != %d", | |
1809 | orig_insn->sec, orig_insn->offset, | |
1810 | orig_alt_group->last_insn->offset + | |
1811 | orig_alt_group->last_insn->len - | |
1812 | orig_alt_group->first_insn->offset, | |
1813 | special_alt->orig_len); | |
1814 | return -1; | |
1815 | } | |
1816 | } | |
17bc3391 | 1817 | |
c9c324dc JP |
1818 | new_alt_group = malloc(sizeof(*new_alt_group)); |
1819 | if (!new_alt_group) { | |
1820 | WARN("malloc failed"); | |
1821 | return -1; | |
dcc914f4 | 1822 | } |
dcc914f4 | 1823 | |
c9c324dc JP |
1824 | if (special_alt->new_len < special_alt->orig_len) { |
1825 | /* | |
1826 | * Insert a fake nop at the end to make the replacement | |
1827 | * alt_group the same size as the original. This is needed to | |
1828 | * allow propagate_alt_cfi() to do its magic. When the last | |
1829 | * instruction affects the stack, the instruction after it (the | |
1830 | * nop) will propagate the new state to the shared CFI array. | |
1831 | */ | |
1832 | nop = malloc(sizeof(*nop)); | |
1833 | if (!nop) { | |
1834 | WARN("malloc failed"); | |
17bc3391 JP |
1835 | return -1; |
1836 | } | |
c9c324dc | 1837 | memset(nop, 0, sizeof(*nop)); |
c9c324dc JP |
1838 | |
1839 | nop->sec = special_alt->new_sec; | |
1840 | nop->offset = special_alt->new_off + special_alt->new_len; | |
1841 | nop->len = special_alt->orig_len - special_alt->new_len; | |
1842 | nop->type = INSN_NOP; | |
dbcdbdfd | 1843 | nop->sym = orig_insn->sym; |
c9c324dc JP |
1844 | nop->alt_group = new_alt_group; |
1845 | nop->ignore = orig_insn->ignore_alts; | |
dcc914f4 | 1846 | } |
17bc3391 | 1847 | |
c9c324dc JP |
1848 | if (!special_alt->new_len) { |
1849 | *new_insn = nop; | |
1850 | goto end; | |
dcc914f4 JP |
1851 | } |
1852 | ||
dcc914f4 JP |
1853 | insn = *new_insn; |
1854 | sec_for_each_insn_from(file, insn) { | |
45245f51 JT |
1855 | struct reloc *alt_reloc; |
1856 | ||
dcc914f4 JP |
1857 | if (insn->offset >= special_alt->new_off + special_alt->new_len) |
1858 | break; | |
1859 | ||
1860 | last_new_insn = insn; | |
1861 | ||
a845c7cf | 1862 | insn->ignore = orig_insn->ignore_alts; |
dbcdbdfd | 1863 | insn->sym = orig_insn->sym; |
b23cc71c | 1864 | insn->alt_group = new_alt_group; |
a845c7cf | 1865 | |
dc419723 JP |
1866 | /* |
1867 | * Since alternative replacement code is copy/pasted by the | |
1868 | * kernel after applying relocations, generally such code can't | |
1869 | * have relative-address relocation references to outside the | |
1870 | * .altinstr_replacement section, unless the arch's | |
1871 | * alternatives code can adjust the relative offsets | |
1872 | * accordingly. | |
dc419723 | 1873 | */ |
7bd2a600 | 1874 | alt_reloc = insn_reloc(file, insn); |
61c6065e | 1875 | if (alt_reloc && arch_pc_relative_reloc(alt_reloc) && |
45245f51 | 1876 | !arch_support_alt_relocation(special_alt, insn, alt_reloc)) { |
dc419723 JP |
1877 | |
1878 | WARN_FUNC("unsupported relocation in alternatives section", | |
1879 | insn->sec, insn->offset); | |
1880 | return -1; | |
1881 | } | |
1882 | ||
a2296140 | 1883 | if (!is_static_jump(insn)) |
dcc914f4 JP |
1884 | continue; |
1885 | ||
1886 | if (!insn->immediate) | |
1887 | continue; | |
1888 | ||
bfb08f22 | 1889 | dest_off = arch_jump_destination(insn); |
34c861e8 | 1890 | if (dest_off == special_alt->new_off + special_alt->new_len) { |
a706bb08 | 1891 | insn->jump_dest = next_insn_same_sec(file, orig_alt_group->last_insn); |
34c861e8 JP |
1892 | if (!insn->jump_dest) { |
1893 | WARN_FUNC("can't find alternative jump destination", | |
1894 | insn->sec, insn->offset); | |
1895 | return -1; | |
1896 | } | |
dcc914f4 JP |
1897 | } |
1898 | } | |
1899 | ||
1900 | if (!last_new_insn) { | |
1901 | WARN_FUNC("can't find last new alternative instruction", | |
1902 | special_alt->new_sec, special_alt->new_off); | |
1903 | return -1; | |
1904 | } | |
1905 | ||
c9c324dc | 1906 | end: |
b23cc71c JP |
1907 | new_alt_group->orig_group = orig_alt_group; |
1908 | new_alt_group->first_insn = *new_insn; | |
1c34496e PZ |
1909 | new_alt_group->last_insn = last_new_insn; |
1910 | new_alt_group->nop = nop; | |
c9c324dc | 1911 | new_alt_group->cfi = orig_alt_group->cfi; |
dcc914f4 JP |
1912 | return 0; |
1913 | } | |
1914 | ||
1915 | /* | |
1916 | * A jump table entry can either convert a nop to a jump or a jump to a nop. | |
1917 | * If the original instruction is a jump, make the alt entry an effective nop | |
1918 | * by just skipping the original instruction. | |
1919 | */ | |
1920 | static int handle_jump_alt(struct objtool_file *file, | |
1921 | struct special_alt *special_alt, | |
1922 | struct instruction *orig_insn, | |
1923 | struct instruction **new_insn) | |
1924 | { | |
48001d26 PZ |
1925 | if (orig_insn->type != INSN_JUMP_UNCONDITIONAL && |
1926 | orig_insn->type != INSN_NOP) { | |
e2d9494b | 1927 | |
dcc914f4 JP |
1928 | WARN_FUNC("unsupported instruction at jump label", |
1929 | orig_insn->sec, orig_insn->offset); | |
1930 | return -1; | |
1931 | } | |
1932 | ||
4ab7674f | 1933 | if (opts.hack_jump_label && special_alt->key_addend & 2) { |
6d37b83c PZ |
1934 | struct reloc *reloc = insn_reloc(file, orig_insn); |
1935 | ||
1936 | if (reloc) { | |
1937 | reloc->type = R_NONE; | |
1938 | elf_write_reloc(file->elf, reloc); | |
1939 | } | |
1940 | elf_write_insn(file->elf, orig_insn->sec, | |
1941 | orig_insn->offset, orig_insn->len, | |
1942 | arch_nop_insn(orig_insn->len)); | |
1943 | orig_insn->type = INSN_NOP; | |
48001d26 PZ |
1944 | } |
1945 | ||
1946 | if (orig_insn->type == INSN_NOP) { | |
1947 | if (orig_insn->len == 2) | |
1948 | file->jl_nop_short++; | |
1949 | else | |
1950 | file->jl_nop_long++; | |
1951 | ||
1952 | return 0; | |
6d37b83c PZ |
1953 | } |
1954 | ||
e2d9494b PZ |
1955 | if (orig_insn->len == 2) |
1956 | file->jl_short++; | |
1957 | else | |
1958 | file->jl_long++; | |
1959 | ||
1c34496e | 1960 | *new_insn = next_insn_same_sec(file, orig_insn); |
dcc914f4 JP |
1961 | return 0; |
1962 | } | |
1963 | ||
1964 | /* | |
1965 | * Read all the special sections which have alternate instructions which can be | |
1966 | * patched in or redirected to at runtime. Each instruction having alternate | |
1967 | * instruction(s) has them added to its insn->alts list, which will be | |
1968 | * traversed in validate_branch(). | |
1969 | */ | |
1970 | static int add_special_section_alts(struct objtool_file *file) | |
1971 | { | |
1972 | struct list_head special_alts; | |
1973 | struct instruction *orig_insn, *new_insn; | |
1974 | struct special_alt *special_alt, *tmp; | |
1975 | struct alternative *alt; | |
1976 | int ret; | |
1977 | ||
1978 | ret = special_get_alts(file->elf, &special_alts); | |
1979 | if (ret) | |
1980 | return ret; | |
1981 | ||
1982 | list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { | |
dcc914f4 JP |
1983 | |
1984 | orig_insn = find_insn(file, special_alt->orig_sec, | |
1985 | special_alt->orig_off); | |
1986 | if (!orig_insn) { | |
1987 | WARN_FUNC("special: can't find orig instruction", | |
1988 | special_alt->orig_sec, special_alt->orig_off); | |
1989 | ret = -1; | |
1990 | goto out; | |
1991 | } | |
1992 | ||
1993 | new_insn = NULL; | |
1994 | if (!special_alt->group || special_alt->new_len) { | |
1995 | new_insn = find_insn(file, special_alt->new_sec, | |
1996 | special_alt->new_off); | |
1997 | if (!new_insn) { | |
1998 | WARN_FUNC("special: can't find new instruction", | |
1999 | special_alt->new_sec, | |
2000 | special_alt->new_off); | |
2001 | ret = -1; | |
2002 | goto out; | |
2003 | } | |
2004 | } | |
2005 | ||
2006 | if (special_alt->group) { | |
7170cf47 JT |
2007 | if (!special_alt->orig_len) { |
2008 | WARN_FUNC("empty alternative entry", | |
2009 | orig_insn->sec, orig_insn->offset); | |
2010 | continue; | |
2011 | } | |
2012 | ||
dcc914f4 JP |
2013 | ret = handle_group_alt(file, special_alt, orig_insn, |
2014 | &new_insn); | |
2015 | if (ret) | |
2016 | goto out; | |
2017 | } else if (special_alt->jump_or_nop) { | |
2018 | ret = handle_jump_alt(file, special_alt, orig_insn, | |
2019 | &new_insn); | |
2020 | if (ret) | |
2021 | goto out; | |
2022 | } | |
2023 | ||
258c7605 JP |
2024 | alt = malloc(sizeof(*alt)); |
2025 | if (!alt) { | |
2026 | WARN("malloc failed"); | |
2027 | ret = -1; | |
2028 | goto out; | |
2029 | } | |
2030 | ||
dcc914f4 | 2031 | alt->insn = new_insn; |
764eef4b | 2032 | alt->skip_orig = special_alt->skip_orig; |
ea24213d | 2033 | orig_insn->ignore_alts |= special_alt->skip_alt; |
d5406654 PZ |
2034 | alt->next = orig_insn->alts; |
2035 | orig_insn->alts = alt; | |
dcc914f4 JP |
2036 | |
2037 | list_del(&special_alt->list); | |
2038 | free(special_alt); | |
2039 | } | |
2040 | ||
2daf7fab | 2041 | if (opts.stats) { |
e2d9494b PZ |
2042 | printf("jl\\\tNOP\tJMP\n"); |
2043 | printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short); | |
2044 | printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long); | |
2045 | } | |
2046 | ||
dcc914f4 JP |
2047 | out: |
2048 | return ret; | |
2049 | } | |
2050 | ||
e7c2bc37 | 2051 | static int add_jump_table(struct objtool_file *file, struct instruction *insn, |
f1974222 | 2052 | struct reloc *table) |
dcc914f4 | 2053 | { |
f1974222 | 2054 | struct reloc *reloc = table; |
e7c2bc37 | 2055 | struct instruction *dest_insn; |
dcc914f4 | 2056 | struct alternative *alt; |
dbcdbdfd | 2057 | struct symbol *pfunc = insn_func(insn)->pfunc; |
fd35c88b | 2058 | unsigned int prev_offset = 0; |
dcc914f4 | 2059 | |
e7c2bc37 | 2060 | /* |
f1974222 | 2061 | * Each @reloc is a switch table relocation which points to the target |
e7c2bc37 JP |
2062 | * instruction. |
2063 | */ | |
f1974222 | 2064 | list_for_each_entry_from(reloc, &table->sec->reloc_list, list) { |
bd98c813 JH |
2065 | |
2066 | /* Check for the end of the table: */ | |
f1974222 | 2067 | if (reloc != table && reloc->jump_table_start) |
dcc914f4 JP |
2068 | break; |
2069 | ||
e7c2bc37 | 2070 | /* Make sure the table entries are consecutive: */ |
f1974222 | 2071 | if (prev_offset && reloc->offset != prev_offset + 8) |
fd35c88b JP |
2072 | break; |
2073 | ||
2074 | /* Detect function pointers from contiguous objects: */ | |
f1974222 MH |
2075 | if (reloc->sym->sec == pfunc->sec && |
2076 | reloc->addend == pfunc->offset) | |
fd35c88b JP |
2077 | break; |
2078 | ||
f1974222 | 2079 | dest_insn = find_insn(file, reloc->sym->sec, reloc->addend); |
e7c2bc37 | 2080 | if (!dest_insn) |
dcc914f4 JP |
2081 | break; |
2082 | ||
e7c2bc37 | 2083 | /* Make sure the destination is in the same function: */ |
dbcdbdfd | 2084 | if (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc) |
13810435 | 2085 | break; |
dcc914f4 JP |
2086 | |
2087 | alt = malloc(sizeof(*alt)); | |
2088 | if (!alt) { | |
2089 | WARN("malloc failed"); | |
2090 | return -1; | |
2091 | } | |
2092 | ||
e7c2bc37 | 2093 | alt->insn = dest_insn; |
d5406654 PZ |
2094 | alt->next = insn->alts; |
2095 | insn->alts = alt; | |
f1974222 | 2096 | prev_offset = reloc->offset; |
fd35c88b JP |
2097 | } |
2098 | ||
2099 | if (!prev_offset) { | |
2100 | WARN_FUNC("can't find switch jump table", | |
2101 | insn->sec, insn->offset); | |
2102 | return -1; | |
dcc914f4 JP |
2103 | } |
2104 | ||
2105 | return 0; | |
2106 | } | |
2107 | ||
2108 | /* | |
d871f7b5 RG |
2109 | * find_jump_table() - Given a dynamic jump, find the switch jump table |
2110 | * associated with it. | |
dcc914f4 | 2111 | */ |
f1974222 | 2112 | static struct reloc *find_jump_table(struct objtool_file *file, |
dcc914f4 JP |
2113 | struct symbol *func, |
2114 | struct instruction *insn) | |
2115 | { | |
d871f7b5 | 2116 | struct reloc *table_reloc; |
113d4bc9 | 2117 | struct instruction *dest_insn, *orig_insn = insn; |
dcc914f4 | 2118 | |
99ce7962 PZ |
2119 | /* |
2120 | * Backward search using the @first_jump_src links, these help avoid | |
2121 | * much of the 'in between' code. Which avoids us getting confused by | |
2122 | * it. | |
2123 | */ | |
7dec80cc | 2124 | for (; |
dbcdbdfd | 2125 | insn && insn_func(insn) && insn_func(insn)->pfunc == func; |
1119d265 | 2126 | insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) { |
99ce7962 | 2127 | |
7dec80cc | 2128 | if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC) |
dcc914f4 JP |
2129 | break; |
2130 | ||
2131 | /* allow small jumps within the range */ | |
2132 | if (insn->type == INSN_JUMP_UNCONDITIONAL && | |
2133 | insn->jump_dest && | |
2134 | (insn->jump_dest->offset <= insn->offset || | |
2135 | insn->jump_dest->offset > orig_insn->offset)) | |
2136 | break; | |
2137 | ||
d871f7b5 | 2138 | table_reloc = arch_find_switch_table(file, insn); |
f1974222 | 2139 | if (!table_reloc) |
e7c2bc37 | 2140 | continue; |
f1974222 | 2141 | dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend); |
dbcdbdfd | 2142 | if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func) |
113d4bc9 | 2143 | continue; |
7dec80cc | 2144 | |
f1974222 | 2145 | return table_reloc; |
dcc914f4 JP |
2146 | } |
2147 | ||
2148 | return NULL; | |
2149 | } | |
2150 | ||
bd98c813 JH |
2151 | /* |
2152 | * First pass: Mark the head of each jump table so that in the next pass, | |
2153 | * we know when a given jump table ends and the next one starts. | |
2154 | */ | |
2155 | static void mark_func_jump_tables(struct objtool_file *file, | |
2156 | struct symbol *func) | |
dcc914f4 | 2157 | { |
bd98c813 | 2158 | struct instruction *insn, *last = NULL; |
f1974222 | 2159 | struct reloc *reloc; |
dcc914f4 | 2160 | |
f0f70adb | 2161 | func_for_each_insn(file, func, insn) { |
99ce7962 PZ |
2162 | if (!last) |
2163 | last = insn; | |
2164 | ||
2165 | /* | |
2166 | * Store back-pointers for unconditional forward jumps such | |
e7c2bc37 | 2167 | * that find_jump_table() can back-track using those and |
99ce7962 PZ |
2168 | * avoid some potentially confusing code. |
2169 | */ | |
2170 | if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest && | |
2171 | insn->offset > last->offset && | |
2172 | insn->jump_dest->offset > insn->offset && | |
2173 | !insn->jump_dest->first_jump_src) { | |
2174 | ||
2175 | insn->jump_dest->first_jump_src = insn; | |
2176 | last = insn->jump_dest; | |
2177 | } | |
2178 | ||
dcc914f4 JP |
2179 | if (insn->type != INSN_JUMP_DYNAMIC) |
2180 | continue; | |
2181 | ||
f1974222 MH |
2182 | reloc = find_jump_table(file, func, insn); |
2183 | if (reloc) { | |
2184 | reloc->jump_table_start = true; | |
c6f5dc28 | 2185 | insn->_jump_table = reloc; |
dcc914f4 | 2186 | } |
dcc914f4 | 2187 | } |
bd98c813 JH |
2188 | } |
2189 | ||
2190 | static int add_func_jump_tables(struct objtool_file *file, | |
2191 | struct symbol *func) | |
2192 | { | |
2193 | struct instruction *insn; | |
2194 | int ret; | |
2195 | ||
f0f70adb | 2196 | func_for_each_insn(file, func, insn) { |
c6f5dc28 | 2197 | if (!insn_jump_table(insn)) |
bd98c813 | 2198 | continue; |
dcc914f4 | 2199 | |
c6f5dc28 | 2200 | ret = add_jump_table(file, insn, insn_jump_table(insn)); |
dcc914f4 JP |
2201 | if (ret) |
2202 | return ret; | |
2203 | } | |
2204 | ||
2205 | return 0; | |
2206 | } | |
2207 | ||
2208 | /* | |
2209 | * For some switch statements, gcc generates a jump table in the .rodata | |
2210 | * section which contains a list of addresses within the function to jump to. | |
2211 | * This finds these jump tables and adds them to the insn->alts lists. | |
2212 | */ | |
e7c2bc37 | 2213 | static int add_jump_table_alts(struct objtool_file *file) |
dcc914f4 JP |
2214 | { |
2215 | struct section *sec; | |
2216 | struct symbol *func; | |
2217 | int ret; | |
2218 | ||
4a60aa05 | 2219 | if (!file->rodata) |
dcc914f4 JP |
2220 | return 0; |
2221 | ||
baa41469 | 2222 | for_each_sec(file, sec) { |
dcc914f4 JP |
2223 | list_for_each_entry(func, &sec->symbol_list, list) { |
2224 | if (func->type != STT_FUNC) | |
2225 | continue; | |
2226 | ||
bd98c813 | 2227 | mark_func_jump_tables(file, func); |
e7c2bc37 | 2228 | ret = add_func_jump_tables(file, func); |
dcc914f4 JP |
2229 | if (ret) |
2230 | return ret; | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | return 0; | |
2235 | } | |
2236 | ||
b735bd3e JP |
2237 | static void set_func_state(struct cfi_state *state) |
2238 | { | |
2239 | state->cfa = initial_func_cfi.cfa; | |
2240 | memcpy(&state->regs, &initial_func_cfi.regs, | |
2241 | CFI_NUM_REGS * sizeof(struct cfi_reg)); | |
2242 | state->stack_size = initial_func_cfi.cfa.offset; | |
2243 | } | |
2244 | ||
39358a03 JP |
2245 | static int read_unwind_hints(struct objtool_file *file) |
2246 | { | |
8b946cc3 | 2247 | struct cfi_state cfi = init_cfi; |
f1974222 | 2248 | struct section *sec, *relocsec; |
39358a03 JP |
2249 | struct unwind_hint *hint; |
2250 | struct instruction *insn; | |
8b946cc3 | 2251 | struct reloc *reloc; |
39358a03 JP |
2252 | int i; |
2253 | ||
2254 | sec = find_section_by_name(file->elf, ".discard.unwind_hints"); | |
2255 | if (!sec) | |
2256 | return 0; | |
2257 | ||
f1974222 MH |
2258 | relocsec = sec->reloc; |
2259 | if (!relocsec) { | |
39358a03 JP |
2260 | WARN("missing .rela.discard.unwind_hints section"); |
2261 | return -1; | |
2262 | } | |
2263 | ||
fe255fe6 | 2264 | if (sec->sh.sh_size % sizeof(struct unwind_hint)) { |
39358a03 JP |
2265 | WARN("struct unwind_hint size mismatch"); |
2266 | return -1; | |
2267 | } | |
2268 | ||
2269 | file->hints = true; | |
2270 | ||
fe255fe6 | 2271 | for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) { |
39358a03 JP |
2272 | hint = (struct unwind_hint *)sec->data->d_buf + i; |
2273 | ||
f1974222 MH |
2274 | reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint)); |
2275 | if (!reloc) { | |
2276 | WARN("can't find reloc for unwind_hints[%d]", i); | |
39358a03 JP |
2277 | return -1; |
2278 | } | |
2279 | ||
f1974222 | 2280 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
39358a03 JP |
2281 | if (!insn) { |
2282 | WARN("can't find insn for unwind_hints[%d]", i); | |
2283 | return -1; | |
2284 | } | |
2285 | ||
b735bd3e | 2286 | insn->hint = true; |
39358a03 | 2287 | |
8faea26e JP |
2288 | if (hint->type == UNWIND_HINT_TYPE_SAVE) { |
2289 | insn->hint = false; | |
2290 | insn->save = true; | |
2291 | continue; | |
2292 | } | |
2293 | ||
2294 | if (hint->type == UNWIND_HINT_TYPE_RESTORE) { | |
2295 | insn->restore = true; | |
2296 | continue; | |
2297 | } | |
2298 | ||
a09a6e23 | 2299 | if (hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) { |
08f87a93 PZ |
2300 | struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset); |
2301 | ||
a09a6e23 PZ |
2302 | if (sym && sym->bind == STB_GLOBAL) { |
2303 | if (opts.ibt && insn->type != INSN_ENDBR && !insn->noendbr) { | |
2304 | WARN_FUNC("UNWIND_HINT_IRET_REGS without ENDBR", | |
2305 | insn->sec, insn->offset); | |
2306 | } | |
2307 | ||
2308 | insn->entry = 1; | |
08f87a93 PZ |
2309 | } |
2310 | } | |
2311 | ||
a09a6e23 PZ |
2312 | if (hint->type == UNWIND_HINT_TYPE_ENTRY) { |
2313 | hint->type = UNWIND_HINT_TYPE_CALL; | |
2314 | insn->entry = 1; | |
2315 | } | |
2316 | ||
b735bd3e | 2317 | if (hint->type == UNWIND_HINT_TYPE_FUNC) { |
8b946cc3 | 2318 | insn->cfi = &func_cfi; |
39358a03 JP |
2319 | continue; |
2320 | } | |
2321 | ||
8b946cc3 PZ |
2322 | if (insn->cfi) |
2323 | cfi = *(insn->cfi); | |
2324 | ||
2325 | if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) { | |
39358a03 JP |
2326 | WARN_FUNC("unsupported unwind_hint sp base reg %d", |
2327 | insn->sec, insn->offset, hint->sp_reg); | |
2328 | return -1; | |
2329 | } | |
2330 | ||
0646c28b | 2331 | cfi.cfa.offset = bswap_if_needed(file->elf, hint->sp_offset); |
8b946cc3 | 2332 | cfi.type = hint->type; |
00c8f01c | 2333 | cfi.signal = hint->signal; |
8b946cc3 PZ |
2334 | cfi.end = hint->end; |
2335 | ||
2336 | insn->cfi = cfi_hash_find_or_add(&cfi); | |
39358a03 JP |
2337 | } |
2338 | ||
2339 | return 0; | |
2340 | } | |
2341 | ||
96db4a98 PZ |
2342 | static int read_noendbr_hints(struct objtool_file *file) |
2343 | { | |
2344 | struct section *sec; | |
2345 | struct instruction *insn; | |
2346 | struct reloc *reloc; | |
2347 | ||
2348 | sec = find_section_by_name(file->elf, ".rela.discard.noendbr"); | |
2349 | if (!sec) | |
2350 | return 0; | |
2351 | ||
2352 | list_for_each_entry(reloc, &sec->reloc_list, list) { | |
2353 | insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend); | |
2354 | if (!insn) { | |
2355 | WARN("bad .discard.noendbr entry"); | |
2356 | return -1; | |
2357 | } | |
2358 | ||
2359 | insn->noendbr = 1; | |
2360 | } | |
2361 | ||
2362 | return 0; | |
2363 | } | |
2364 | ||
b5bc2231 PZ |
2365 | static int read_retpoline_hints(struct objtool_file *file) |
2366 | { | |
63474dc4 | 2367 | struct section *sec; |
b5bc2231 | 2368 | struct instruction *insn; |
f1974222 | 2369 | struct reloc *reloc; |
b5bc2231 | 2370 | |
63474dc4 | 2371 | sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); |
b5bc2231 PZ |
2372 | if (!sec) |
2373 | return 0; | |
2374 | ||
f1974222 MH |
2375 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
2376 | if (reloc->sym->type != STT_SECTION) { | |
63474dc4 | 2377 | WARN("unexpected relocation symbol type in %s", sec->name); |
b5bc2231 PZ |
2378 | return -1; |
2379 | } | |
2380 | ||
f1974222 | 2381 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
b5bc2231 | 2382 | if (!insn) { |
63474dc4 | 2383 | WARN("bad .discard.retpoline_safe entry"); |
b5bc2231 PZ |
2384 | return -1; |
2385 | } | |
2386 | ||
2387 | if (insn->type != INSN_JUMP_DYNAMIC && | |
9bb2ec60 | 2388 | insn->type != INSN_CALL_DYNAMIC && |
a09a6e23 PZ |
2389 | insn->type != INSN_RETURN && |
2390 | insn->type != INSN_NOP) { | |
2391 | WARN_FUNC("retpoline_safe hint not an indirect jump/call/ret/nop", | |
b5bc2231 PZ |
2392 | insn->sec, insn->offset); |
2393 | return -1; | |
2394 | } | |
2395 | ||
2396 | insn->retpoline_safe = true; | |
2397 | } | |
2398 | ||
2399 | return 0; | |
2400 | } | |
2401 | ||
c4a33939 PZ |
2402 | static int read_instr_hints(struct objtool_file *file) |
2403 | { | |
2404 | struct section *sec; | |
2405 | struct instruction *insn; | |
f1974222 | 2406 | struct reloc *reloc; |
c4a33939 PZ |
2407 | |
2408 | sec = find_section_by_name(file->elf, ".rela.discard.instr_end"); | |
2409 | if (!sec) | |
2410 | return 0; | |
2411 | ||
f1974222 MH |
2412 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
2413 | if (reloc->sym->type != STT_SECTION) { | |
c4a33939 PZ |
2414 | WARN("unexpected relocation symbol type in %s", sec->name); |
2415 | return -1; | |
2416 | } | |
2417 | ||
f1974222 | 2418 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
c4a33939 PZ |
2419 | if (!insn) { |
2420 | WARN("bad .discard.instr_end entry"); | |
2421 | return -1; | |
2422 | } | |
2423 | ||
2424 | insn->instr--; | |
2425 | } | |
2426 | ||
2427 | sec = find_section_by_name(file->elf, ".rela.discard.instr_begin"); | |
2428 | if (!sec) | |
2429 | return 0; | |
2430 | ||
f1974222 MH |
2431 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
2432 | if (reloc->sym->type != STT_SECTION) { | |
c4a33939 PZ |
2433 | WARN("unexpected relocation symbol type in %s", sec->name); |
2434 | return -1; | |
2435 | } | |
2436 | ||
f1974222 | 2437 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
c4a33939 PZ |
2438 | if (!insn) { |
2439 | WARN("bad .discard.instr_begin entry"); | |
2440 | return -1; | |
2441 | } | |
2442 | ||
2443 | insn->instr++; | |
2444 | } | |
2445 | ||
2446 | return 0; | |
2447 | } | |
2448 | ||
8aa8eb2a AC |
2449 | static int read_intra_function_calls(struct objtool_file *file) |
2450 | { | |
2451 | struct instruction *insn; | |
2452 | struct section *sec; | |
f1974222 | 2453 | struct reloc *reloc; |
8aa8eb2a AC |
2454 | |
2455 | sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls"); | |
2456 | if (!sec) | |
2457 | return 0; | |
2458 | ||
f1974222 | 2459 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
8aa8eb2a AC |
2460 | unsigned long dest_off; |
2461 | ||
f1974222 | 2462 | if (reloc->sym->type != STT_SECTION) { |
8aa8eb2a AC |
2463 | WARN("unexpected relocation symbol type in %s", |
2464 | sec->name); | |
2465 | return -1; | |
2466 | } | |
2467 | ||
f1974222 | 2468 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
8aa8eb2a AC |
2469 | if (!insn) { |
2470 | WARN("bad .discard.intra_function_call entry"); | |
2471 | return -1; | |
2472 | } | |
2473 | ||
2474 | if (insn->type != INSN_CALL) { | |
2475 | WARN_FUNC("intra_function_call not a direct call", | |
2476 | insn->sec, insn->offset); | |
2477 | return -1; | |
2478 | } | |
2479 | ||
2480 | /* | |
2481 | * Treat intra-function CALLs as JMPs, but with a stack_op. | |
2482 | * See add_call_destinations(), which strips stack_ops from | |
2483 | * normal CALLs. | |
2484 | */ | |
2485 | insn->type = INSN_JUMP_UNCONDITIONAL; | |
2486 | ||
7b3e3186 | 2487 | dest_off = arch_jump_destination(insn); |
8aa8eb2a AC |
2488 | insn->jump_dest = find_insn(file, insn->sec, dest_off); |
2489 | if (!insn->jump_dest) { | |
2490 | WARN_FUNC("can't find call dest at %s+0x%lx", | |
2491 | insn->sec, insn->offset, | |
2492 | insn->sec->name, dest_off); | |
2493 | return -1; | |
2494 | } | |
2495 | } | |
2496 | ||
2497 | return 0; | |
2498 | } | |
2499 | ||
05098119 ME |
2500 | /* |
2501 | * Return true if name matches an instrumentation function, where calls to that | |
2502 | * function from noinstr code can safely be removed, but compilers won't do so. | |
2503 | */ | |
2504 | static bool is_profiling_func(const char *name) | |
2505 | { | |
2506 | /* | |
2507 | * Many compilers cannot disable KCOV with a function attribute. | |
2508 | */ | |
2509 | if (!strncmp(name, "__sanitizer_cov_", 16)) | |
2510 | return true; | |
2511 | ||
2512 | /* | |
2513 | * Some compilers currently do not remove __tsan_func_entry/exit nor | |
2514 | * __tsan_atomic_signal_fence (used for barrier instrumentation) with | |
2515 | * the __no_sanitize_thread attribute, remove them. Once the kernel's | |
2516 | * minimum Clang version is 14.0, this can be removed. | |
2517 | */ | |
2518 | if (!strncmp(name, "__tsan_func_", 12) || | |
2519 | !strcmp(name, "__tsan_atomic_signal_fence")) | |
2520 | return true; | |
2521 | ||
2522 | return false; | |
2523 | } | |
2524 | ||
1739c66e | 2525 | static int classify_symbols(struct objtool_file *file) |
1e7e4788 JP |
2526 | { |
2527 | struct section *sec; | |
2528 | struct symbol *func; | |
2529 | ||
2530 | for_each_sec(file, sec) { | |
2531 | list_for_each_entry(func, &sec->symbol_list, list) { | |
1739c66e PZ |
2532 | if (func->bind != STB_GLOBAL) |
2533 | continue; | |
2534 | ||
2535 | if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR, | |
1e7e4788 JP |
2536 | strlen(STATIC_CALL_TRAMP_PREFIX_STR))) |
2537 | func->static_call_tramp = true; | |
1739c66e PZ |
2538 | |
2539 | if (arch_is_retpoline(func)) | |
2540 | func->retpoline_thunk = true; | |
2541 | ||
d9e9d230 PZ |
2542 | if (arch_is_rethunk(func)) |
2543 | func->return_thunk = true; | |
2544 | ||
4ca993d4 | 2545 | if (arch_ftrace_match(func->name)) |
1739c66e PZ |
2546 | func->fentry = true; |
2547 | ||
05098119 ME |
2548 | if (is_profiling_func(func->name)) |
2549 | func->profiling_func = true; | |
1e7e4788 JP |
2550 | } |
2551 | } | |
2552 | ||
2553 | return 0; | |
2554 | } | |
2555 | ||
4a60aa05 AX |
2556 | static void mark_rodata(struct objtool_file *file) |
2557 | { | |
2558 | struct section *sec; | |
2559 | bool found = false; | |
2560 | ||
2561 | /* | |
87b512de JP |
2562 | * Search for the following rodata sections, each of which can |
2563 | * potentially contain jump tables: | |
2564 | * | |
2565 | * - .rodata: can contain GCC switch tables | |
2566 | * - .rodata.<func>: same, if -fdata-sections is being used | |
2567 | * - .rodata..c_jump_table: contains C annotated jump tables | |
2568 | * | |
2569 | * .rodata.str1.* sections are ignored; they don't contain jump tables. | |
4a60aa05 AX |
2570 | */ |
2571 | for_each_sec(file, sec) { | |
1ee44470 MS |
2572 | if (!strncmp(sec->name, ".rodata", 7) && |
2573 | !strstr(sec->name, ".str1.")) { | |
4a60aa05 AX |
2574 | sec->rodata = true; |
2575 | found = true; | |
2576 | } | |
2577 | } | |
2578 | ||
2579 | file->rodata = found; | |
2580 | } | |
2581 | ||
dcc914f4 JP |
2582 | static int decode_sections(struct objtool_file *file) |
2583 | { | |
2584 | int ret; | |
2585 | ||
4a60aa05 AX |
2586 | mark_rodata(file); |
2587 | ||
db2b0c5d PZ |
2588 | ret = init_pv_ops(file); |
2589 | if (ret) | |
2590 | return ret; | |
2591 | ||
dbcdbdfd PZ |
2592 | /* |
2593 | * Must be before add_{jump_call}_destination. | |
2594 | */ | |
2595 | ret = classify_symbols(file); | |
2596 | if (ret) | |
2597 | return ret; | |
2598 | ||
dcc914f4 JP |
2599 | ret = decode_instructions(file); |
2600 | if (ret) | |
2601 | return ret; | |
2602 | ||
dcc914f4 | 2603 | add_ignores(file); |
ea24213d | 2604 | add_uaccess_safe(file); |
dcc914f4 | 2605 | |
ff05ab23 | 2606 | ret = add_ignore_alternatives(file); |
258c7605 JP |
2607 | if (ret) |
2608 | return ret; | |
2609 | ||
08f87a93 PZ |
2610 | /* |
2611 | * Must be before read_unwind_hints() since that needs insn->noendbr. | |
2612 | */ | |
96db4a98 PZ |
2613 | ret = read_noendbr_hints(file); |
2614 | if (ret) | |
2615 | return ret; | |
2616 | ||
43d5430a | 2617 | /* |
34c861e8 JP |
2618 | * Must be before add_jump_destinations(), which depends on 'func' |
2619 | * being set for alternatives, to enable proper sibling call detection. | |
43d5430a | 2620 | */ |
de6fbced SV |
2621 | if (opts.stackval || opts.orc || opts.uaccess || opts.noinstr) { |
2622 | ret = add_special_section_alts(file); | |
2623 | if (ret) | |
2624 | return ret; | |
2625 | } | |
dcc914f4 | 2626 | |
34c861e8 | 2627 | ret = add_jump_destinations(file); |
dcc914f4 JP |
2628 | if (ret) |
2629 | return ret; | |
2630 | ||
a958c4fe PZ |
2631 | /* |
2632 | * Must be before add_call_destination(); it changes INSN_CALL to | |
2633 | * INSN_JUMP. | |
2634 | */ | |
8aa8eb2a AC |
2635 | ret = read_intra_function_calls(file); |
2636 | if (ret) | |
2637 | return ret; | |
2638 | ||
a845c7cf | 2639 | ret = add_call_destinations(file); |
dcc914f4 JP |
2640 | if (ret) |
2641 | return ret; | |
2642 | ||
0e5b613b PZ |
2643 | /* |
2644 | * Must be after add_call_destinations() such that it can override | |
2645 | * dead_end_function() marks. | |
2646 | */ | |
2647 | ret = add_dead_ends(file); | |
2648 | if (ret) | |
2649 | return ret; | |
2650 | ||
e7c2bc37 | 2651 | ret = add_jump_table_alts(file); |
dcc914f4 JP |
2652 | if (ret) |
2653 | return ret; | |
2654 | ||
39358a03 JP |
2655 | ret = read_unwind_hints(file); |
2656 | if (ret) | |
2657 | return ret; | |
2658 | ||
b5bc2231 PZ |
2659 | ret = read_retpoline_hints(file); |
2660 | if (ret) | |
2661 | return ret; | |
2662 | ||
c4a33939 PZ |
2663 | ret = read_instr_hints(file); |
2664 | if (ret) | |
2665 | return ret; | |
2666 | ||
dcc914f4 JP |
2667 | return 0; |
2668 | } | |
2669 | ||
2670 | static bool is_fentry_call(struct instruction *insn) | |
2671 | { | |
1739c66e | 2672 | if (insn->type == INSN_CALL && |
c6f5dc28 PZ |
2673 | insn_call_dest(insn) && |
2674 | insn_call_dest(insn)->fentry) | |
dcc914f4 JP |
2675 | return true; |
2676 | ||
2677 | return false; | |
2678 | } | |
2679 | ||
e25eea89 | 2680 | static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state) |
dcc914f4 | 2681 | { |
e7c0219b | 2682 | struct cfi_state *cfi = &state->cfi; |
baa41469 JP |
2683 | int i; |
2684 | ||
e7c0219b | 2685 | if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap) |
e25eea89 PZ |
2686 | return true; |
2687 | ||
b735bd3e | 2688 | if (cfi->cfa.offset != initial_func_cfi.cfa.offset) |
baa41469 JP |
2689 | return true; |
2690 | ||
b735bd3e | 2691 | if (cfi->stack_size != initial_func_cfi.cfa.offset) |
e25eea89 PZ |
2692 | return true; |
2693 | ||
2694 | for (i = 0; i < CFI_NUM_REGS; i++) { | |
e7c0219b PZ |
2695 | if (cfi->regs[i].base != initial_func_cfi.regs[i].base || |
2696 | cfi->regs[i].offset != initial_func_cfi.regs[i].offset) | |
baa41469 | 2697 | return true; |
e25eea89 | 2698 | } |
baa41469 JP |
2699 | |
2700 | return false; | |
2701 | } | |
2702 | ||
fb084fde JT |
2703 | static bool check_reg_frame_pos(const struct cfi_reg *reg, |
2704 | int expected_offset) | |
2705 | { | |
2706 | return reg->base == CFI_CFA && | |
2707 | reg->offset == expected_offset; | |
2708 | } | |
2709 | ||
baa41469 JP |
2710 | static bool has_valid_stack_frame(struct insn_state *state) |
2711 | { | |
e7c0219b PZ |
2712 | struct cfi_state *cfi = &state->cfi; |
2713 | ||
fb084fde JT |
2714 | if (cfi->cfa.base == CFI_BP && |
2715 | check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) && | |
2716 | check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8)) | |
baa41469 JP |
2717 | return true; |
2718 | ||
e7c0219b | 2719 | if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP) |
baa41469 JP |
2720 | return true; |
2721 | ||
2722 | return false; | |
dcc914f4 JP |
2723 | } |
2724 | ||
e7c0219b PZ |
2725 | static int update_cfi_state_regs(struct instruction *insn, |
2726 | struct cfi_state *cfi, | |
65ea47dc | 2727 | struct stack_op *op) |
627fce14 | 2728 | { |
e7c0219b | 2729 | struct cfi_reg *cfa = &cfi->cfa; |
627fce14 | 2730 | |
d8dd25a4 | 2731 | if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT) |
627fce14 JP |
2732 | return 0; |
2733 | ||
2734 | /* push */ | |
ea24213d | 2735 | if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF) |
627fce14 JP |
2736 | cfa->offset += 8; |
2737 | ||
2738 | /* pop */ | |
ea24213d | 2739 | if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF) |
627fce14 JP |
2740 | cfa->offset -= 8; |
2741 | ||
2742 | /* add immediate to sp */ | |
2743 | if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD && | |
2744 | op->dest.reg == CFI_SP && op->src.reg == CFI_SP) | |
2745 | cfa->offset -= op->src.offset; | |
2746 | ||
2747 | return 0; | |
2748 | } | |
2749 | ||
e7c0219b | 2750 | static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset) |
dcc914f4 | 2751 | { |
bf4d1a83 | 2752 | if (arch_callee_saved_reg(reg) && |
e7c0219b PZ |
2753 | cfi->regs[reg].base == CFI_UNDEFINED) { |
2754 | cfi->regs[reg].base = base; | |
2755 | cfi->regs[reg].offset = offset; | |
baa41469 | 2756 | } |
dcc914f4 JP |
2757 | } |
2758 | ||
e7c0219b | 2759 | static void restore_reg(struct cfi_state *cfi, unsigned char reg) |
dcc914f4 | 2760 | { |
e7c0219b PZ |
2761 | cfi->regs[reg].base = initial_func_cfi.regs[reg].base; |
2762 | cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset; | |
baa41469 JP |
2763 | } |
2764 | ||
2765 | /* | |
2766 | * A note about DRAP stack alignment: | |
2767 | * | |
2768 | * GCC has the concept of a DRAP register, which is used to help keep track of | |
2769 | * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP | |
2770 | * register. The typical DRAP pattern is: | |
2771 | * | |
2772 | * 4c 8d 54 24 08 lea 0x8(%rsp),%r10 | |
2773 | * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp | |
2774 | * 41 ff 72 f8 pushq -0x8(%r10) | |
2775 | * 55 push %rbp | |
2776 | * 48 89 e5 mov %rsp,%rbp | |
2777 | * (more pushes) | |
2778 | * 41 52 push %r10 | |
2779 | * ... | |
2780 | * 41 5a pop %r10 | |
2781 | * (more pops) | |
2782 | * 5d pop %rbp | |
2783 | * 49 8d 62 f8 lea -0x8(%r10),%rsp | |
2784 | * c3 retq | |
2785 | * | |
2786 | * There are some variations in the epilogues, like: | |
2787 | * | |
2788 | * 5b pop %rbx | |
2789 | * 41 5a pop %r10 | |
2790 | * 41 5c pop %r12 | |
2791 | * 41 5d pop %r13 | |
2792 | * 41 5e pop %r14 | |
2793 | * c9 leaveq | |
2794 | * 49 8d 62 f8 lea -0x8(%r10),%rsp | |
2795 | * c3 retq | |
2796 | * | |
2797 | * and: | |
2798 | * | |
2799 | * 4c 8b 55 e8 mov -0x18(%rbp),%r10 | |
2800 | * 48 8b 5d e0 mov -0x20(%rbp),%rbx | |
2801 | * 4c 8b 65 f0 mov -0x10(%rbp),%r12 | |
2802 | * 4c 8b 6d f8 mov -0x8(%rbp),%r13 | |
2803 | * c9 leaveq | |
2804 | * 49 8d 62 f8 lea -0x8(%r10),%rsp | |
2805 | * c3 retq | |
2806 | * | |
2807 | * Sometimes r13 is used as the DRAP register, in which case it's saved and | |
2808 | * restored beforehand: | |
2809 | * | |
2810 | * 41 55 push %r13 | |
2811 | * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13 | |
2812 | * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp | |
2813 | * ... | |
2814 | * 49 8d 65 f0 lea -0x10(%r13),%rsp | |
2815 | * 41 5d pop %r13 | |
2816 | * c3 retq | |
2817 | */ | |
d54dba41 PZ |
2818 | static int update_cfi_state(struct instruction *insn, |
2819 | struct instruction *next_insn, | |
2820 | struct cfi_state *cfi, struct stack_op *op) | |
baa41469 | 2821 | { |
e7c0219b PZ |
2822 | struct cfi_reg *cfa = &cfi->cfa; |
2823 | struct cfi_reg *regs = cfi->regs; | |
baa41469 JP |
2824 | |
2825 | /* stack operations don't make sense with an undefined CFA */ | |
2826 | if (cfa->base == CFI_UNDEFINED) { | |
dbcdbdfd | 2827 | if (insn_func(insn)) { |
baa41469 JP |
2828 | WARN_FUNC("undefined stack state", insn->sec, insn->offset); |
2829 | return -1; | |
2830 | } | |
2831 | return 0; | |
2832 | } | |
2833 | ||
ee819aed JT |
2834 | if (cfi->type == UNWIND_HINT_TYPE_REGS || |
2835 | cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL) | |
e7c0219b | 2836 | return update_cfi_state_regs(insn, cfi, op); |
627fce14 | 2837 | |
baa41469 JP |
2838 | switch (op->dest.type) { |
2839 | ||
2840 | case OP_DEST_REG: | |
2841 | switch (op->src.type) { | |
2842 | ||
2843 | case OP_SRC_REG: | |
0d0970ee JP |
2844 | if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP && |
2845 | cfa->base == CFI_SP && | |
fb084fde | 2846 | check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) { |
0d0970ee JP |
2847 | |
2848 | /* mov %rsp, %rbp */ | |
2849 | cfa->base = op->dest.reg; | |
e7c0219b | 2850 | cfi->bp_scratch = false; |
0d0970ee | 2851 | } |
dd88a0a0 | 2852 | |
0d0970ee | 2853 | else if (op->src.reg == CFI_SP && |
e7c0219b | 2854 | op->dest.reg == CFI_BP && cfi->drap) { |
dd88a0a0 | 2855 | |
0d0970ee JP |
2856 | /* drap: mov %rsp, %rbp */ |
2857 | regs[CFI_BP].base = CFI_BP; | |
e7c0219b PZ |
2858 | regs[CFI_BP].offset = -cfi->stack_size; |
2859 | cfi->bp_scratch = false; | |
0d0970ee | 2860 | } |
dd88a0a0 | 2861 | |
0d0970ee JP |
2862 | else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
2863 | ||
2864 | /* | |
2865 | * mov %rsp, %reg | |
2866 | * | |
2867 | * This is needed for the rare case where GCC | |
2868 | * does: | |
2869 | * | |
2870 | * mov %rsp, %rax | |
2871 | * ... | |
2872 | * mov %rax, %rsp | |
2873 | */ | |
e7c0219b PZ |
2874 | cfi->vals[op->dest.reg].base = CFI_CFA; |
2875 | cfi->vals[op->dest.reg].offset = -cfi->stack_size; | |
dd88a0a0 JP |
2876 | } |
2877 | ||
3c1f0583 | 2878 | else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP && |
ffc7e74f | 2879 | (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) { |
3c1f0583 JP |
2880 | |
2881 | /* | |
2882 | * mov %rbp, %rsp | |
2883 | * | |
2884 | * Restore the original stack pointer (Clang). | |
2885 | */ | |
e7c0219b | 2886 | cfi->stack_size = -cfi->regs[CFI_BP].offset; |
3c1f0583 JP |
2887 | } |
2888 | ||
dd88a0a0 JP |
2889 | else if (op->dest.reg == cfa->base) { |
2890 | ||
2891 | /* mov %reg, %rsp */ | |
2892 | if (cfa->base == CFI_SP && | |
e7c0219b | 2893 | cfi->vals[op->src.reg].base == CFI_CFA) { |
dd88a0a0 JP |
2894 | |
2895 | /* | |
2896 | * This is needed for the rare case | |
2897 | * where GCC does something dumb like: | |
2898 | * | |
2899 | * lea 0x8(%rsp), %rcx | |
2900 | * ... | |
2901 | * mov %rcx, %rsp | |
2902 | */ | |
e7c0219b PZ |
2903 | cfa->offset = -cfi->vals[op->src.reg].offset; |
2904 | cfi->stack_size = cfa->offset; | |
dd88a0a0 | 2905 | |
aafeb14e PZ |
2906 | } else if (cfa->base == CFI_SP && |
2907 | cfi->vals[op->src.reg].base == CFI_SP_INDIRECT && | |
2908 | cfi->vals[op->src.reg].offset == cfa->offset) { | |
2909 | ||
2910 | /* | |
2911 | * Stack swizzle: | |
2912 | * | |
2913 | * 1: mov %rsp, (%[tos]) | |
2914 | * 2: mov %[tos], %rsp | |
2915 | * ... | |
2916 | * 3: pop %rsp | |
2917 | * | |
2918 | * Where: | |
2919 | * | |
2920 | * 1 - places a pointer to the previous | |
2921 | * stack at the Top-of-Stack of the | |
2922 | * new stack. | |
2923 | * | |
2924 | * 2 - switches to the new stack. | |
2925 | * | |
2926 | * 3 - pops the Top-of-Stack to restore | |
2927 | * the original stack. | |
2928 | * | |
2929 | * Note: we set base to SP_INDIRECT | |
2930 | * here and preserve offset. Therefore | |
2931 | * when the unwinder reaches ToS it | |
2932 | * will dereference SP and then add the | |
2933 | * offset to find the next frame, IOW: | |
2934 | * (%rsp) + offset. | |
2935 | */ | |
2936 | cfa->base = CFI_SP_INDIRECT; | |
2937 | ||
dd88a0a0 JP |
2938 | } else { |
2939 | cfa->base = CFI_UNDEFINED; | |
2940 | cfa->offset = 0; | |
2941 | } | |
baa41469 JP |
2942 | } |
2943 | ||
724c8a23 PZ |
2944 | else if (op->dest.reg == CFI_SP && |
2945 | cfi->vals[op->src.reg].base == CFI_SP_INDIRECT && | |
2946 | cfi->vals[op->src.reg].offset == cfa->offset) { | |
2947 | ||
2948 | /* | |
2949 | * The same stack swizzle case 2) as above. But | |
2950 | * because we can't change cfa->base, case 3) | |
2951 | * will become a regular POP. Pretend we're a | |
2952 | * PUSH so things don't go unbalanced. | |
2953 | */ | |
2954 | cfi->stack_size += 8; | |
2955 | } | |
2956 | ||
2957 | ||
baa41469 JP |
2958 | break; |
2959 | ||
2960 | case OP_SRC_ADD: | |
2961 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) { | |
2962 | ||
2963 | /* add imm, %rsp */ | |
e7c0219b | 2964 | cfi->stack_size -= op->src.offset; |
baa41469 JP |
2965 | if (cfa->base == CFI_SP) |
2966 | cfa->offset -= op->src.offset; | |
2967 | break; | |
2968 | } | |
2969 | ||
2970 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) { | |
2971 | ||
2972 | /* lea disp(%rbp), %rsp */ | |
e7c0219b | 2973 | cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset); |
baa41469 JP |
2974 | break; |
2975 | } | |
2976 | ||
468af56a JT |
2977 | if (!cfi->drap && op->src.reg == CFI_SP && |
2978 | op->dest.reg == CFI_BP && cfa->base == CFI_SP && | |
2979 | check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) { | |
2980 | ||
2981 | /* lea disp(%rsp), %rbp */ | |
2982 | cfa->base = CFI_BP; | |
2983 | cfa->offset -= op->src.offset; | |
2984 | cfi->bp_scratch = false; | |
2985 | break; | |
2986 | } | |
2987 | ||
dd88a0a0 | 2988 | if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
baa41469 JP |
2989 | |
2990 | /* drap: lea disp(%rsp), %drap */ | |
e7c0219b | 2991 | cfi->drap_reg = op->dest.reg; |
dd88a0a0 JP |
2992 | |
2993 | /* | |
2994 | * lea disp(%rsp), %reg | |
2995 | * | |
2996 | * This is needed for the rare case where GCC | |
2997 | * does something dumb like: | |
2998 | * | |
2999 | * lea 0x8(%rsp), %rcx | |
3000 | * ... | |
3001 | * mov %rcx, %rsp | |
3002 | */ | |
e7c0219b PZ |
3003 | cfi->vals[op->dest.reg].base = CFI_CFA; |
3004 | cfi->vals[op->dest.reg].offset = \ | |
3005 | -cfi->stack_size + op->src.offset; | |
dd88a0a0 | 3006 | |
baa41469 JP |
3007 | break; |
3008 | } | |
3009 | ||
e7c0219b PZ |
3010 | if (cfi->drap && op->dest.reg == CFI_SP && |
3011 | op->src.reg == cfi->drap_reg) { | |
baa41469 JP |
3012 | |
3013 | /* drap: lea disp(%drap), %rsp */ | |
3014 | cfa->base = CFI_SP; | |
e7c0219b PZ |
3015 | cfa->offset = cfi->stack_size = -op->src.offset; |
3016 | cfi->drap_reg = CFI_UNDEFINED; | |
3017 | cfi->drap = false; | |
baa41469 JP |
3018 | break; |
3019 | } | |
3020 | ||
d54dba41 | 3021 | if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) { |
baa41469 JP |
3022 | WARN_FUNC("unsupported stack register modification", |
3023 | insn->sec, insn->offset); | |
3024 | return -1; | |
3025 | } | |
3026 | ||
3027 | break; | |
3028 | ||
3029 | case OP_SRC_AND: | |
3030 | if (op->dest.reg != CFI_SP || | |
e7c0219b PZ |
3031 | (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) || |
3032 | (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) { | |
baa41469 JP |
3033 | WARN_FUNC("unsupported stack pointer realignment", |
3034 | insn->sec, insn->offset); | |
3035 | return -1; | |
3036 | } | |
3037 | ||
e7c0219b | 3038 | if (cfi->drap_reg != CFI_UNDEFINED) { |
baa41469 | 3039 | /* drap: and imm, %rsp */ |
e7c0219b PZ |
3040 | cfa->base = cfi->drap_reg; |
3041 | cfa->offset = cfi->stack_size = 0; | |
3042 | cfi->drap = true; | |
baa41469 JP |
3043 | } |
3044 | ||
3045 | /* | |
3046 | * Older versions of GCC (4.8ish) realign the stack | |
3047 | * without DRAP, with a frame pointer. | |
3048 | */ | |
3049 | ||
3050 | break; | |
3051 | ||
3052 | case OP_SRC_POP: | |
ea24213d | 3053 | case OP_SRC_POPF: |
aafeb14e PZ |
3054 | if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) { |
3055 | ||
3056 | /* pop %rsp; # restore from a stack swizzle */ | |
3057 | cfa->base = CFI_SP; | |
3058 | break; | |
3059 | } | |
3060 | ||
e7c0219b | 3061 | if (!cfi->drap && op->dest.reg == cfa->base) { |
baa41469 JP |
3062 | |
3063 | /* pop %rbp */ | |
3064 | cfa->base = CFI_SP; | |
3065 | } | |
3066 | ||
e7c0219b PZ |
3067 | if (cfi->drap && cfa->base == CFI_BP_INDIRECT && |
3068 | op->dest.reg == cfi->drap_reg && | |
3069 | cfi->drap_offset == -cfi->stack_size) { | |
baa41469 | 3070 | |
bf4d1a83 | 3071 | /* drap: pop %drap */ |
e7c0219b | 3072 | cfa->base = cfi->drap_reg; |
bf4d1a83 | 3073 | cfa->offset = 0; |
e7c0219b | 3074 | cfi->drap_offset = -1; |
baa41469 | 3075 | |
ffc7e74f | 3076 | } else if (cfi->stack_size == -regs[op->dest.reg].offset) { |
baa41469 | 3077 | |
bf4d1a83 | 3078 | /* pop %reg */ |
e7c0219b | 3079 | restore_reg(cfi, op->dest.reg); |
baa41469 JP |
3080 | } |
3081 | ||
e7c0219b | 3082 | cfi->stack_size -= 8; |
baa41469 JP |
3083 | if (cfa->base == CFI_SP) |
3084 | cfa->offset -= 8; | |
3085 | ||
3086 | break; | |
3087 | ||
3088 | case OP_SRC_REG_INDIRECT: | |
201ef5a9 JT |
3089 | if (!cfi->drap && op->dest.reg == cfa->base && |
3090 | op->dest.reg == CFI_BP) { | |
3091 | ||
3092 | /* mov disp(%rsp), %rbp */ | |
3093 | cfa->base = CFI_SP; | |
3094 | cfa->offset = cfi->stack_size; | |
3095 | } | |
3096 | ||
e7c0219b PZ |
3097 | if (cfi->drap && op->src.reg == CFI_BP && |
3098 | op->src.offset == cfi->drap_offset) { | |
bf4d1a83 JP |
3099 | |
3100 | /* drap: mov disp(%rbp), %drap */ | |
e7c0219b | 3101 | cfa->base = cfi->drap_reg; |
bf4d1a83 | 3102 | cfa->offset = 0; |
e7c0219b | 3103 | cfi->drap_offset = -1; |
bf4d1a83 JP |
3104 | } |
3105 | ||
e7c0219b | 3106 | if (cfi->drap && op->src.reg == CFI_BP && |
baa41469 JP |
3107 | op->src.offset == regs[op->dest.reg].offset) { |
3108 | ||
3109 | /* drap: mov disp(%rbp), %reg */ | |
e7c0219b | 3110 | restore_reg(cfi, op->dest.reg); |
baa41469 JP |
3111 | |
3112 | } else if (op->src.reg == cfa->base && | |
3113 | op->src.offset == regs[op->dest.reg].offset + cfa->offset) { | |
3114 | ||
3115 | /* mov disp(%rbp), %reg */ | |
3116 | /* mov disp(%rsp), %reg */ | |
e7c0219b | 3117 | restore_reg(cfi, op->dest.reg); |
201ef5a9 JT |
3118 | |
3119 | } else if (op->src.reg == CFI_SP && | |
3120 | op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) { | |
3121 | ||
3122 | /* mov disp(%rsp), %reg */ | |
3123 | restore_reg(cfi, op->dest.reg); | |
baa41469 JP |
3124 | } |
3125 | ||
3126 | break; | |
3127 | ||
3128 | default: | |
3129 | WARN_FUNC("unknown stack-related instruction", | |
3130 | insn->sec, insn->offset); | |
3131 | return -1; | |
3132 | } | |
3133 | ||
3134 | break; | |
3135 | ||
3136 | case OP_DEST_PUSH: | |
ea24213d | 3137 | case OP_DEST_PUSHF: |
e7c0219b | 3138 | cfi->stack_size += 8; |
baa41469 JP |
3139 | if (cfa->base == CFI_SP) |
3140 | cfa->offset += 8; | |
3141 | ||
3142 | if (op->src.type != OP_SRC_REG) | |
3143 | break; | |
3144 | ||
e7c0219b PZ |
3145 | if (cfi->drap) { |
3146 | if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { | |
baa41469 JP |
3147 | |
3148 | /* drap: push %drap */ | |
3149 | cfa->base = CFI_BP_INDIRECT; | |
e7c0219b | 3150 | cfa->offset = -cfi->stack_size; |
baa41469 | 3151 | |
bf4d1a83 | 3152 | /* save drap so we know when to restore it */ |
e7c0219b | 3153 | cfi->drap_offset = -cfi->stack_size; |
baa41469 | 3154 | |
e7c0219b | 3155 | } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) { |
baa41469 JP |
3156 | |
3157 | /* drap: push %rbp */ | |
e7c0219b | 3158 | cfi->stack_size = 0; |
baa41469 | 3159 | |
f4f80398 | 3160 | } else { |
baa41469 JP |
3161 | |
3162 | /* drap: push %reg */ | |
e7c0219b | 3163 | save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size); |
baa41469 JP |
3164 | } |
3165 | ||
3166 | } else { | |
3167 | ||
3168 | /* push %reg */ | |
e7c0219b | 3169 | save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size); |
baa41469 JP |
3170 | } |
3171 | ||
3172 | /* detect when asm code uses rbp as a scratch register */ | |
dbcdbdfd | 3173 | if (opts.stackval && insn_func(insn) && op->src.reg == CFI_BP && |
baa41469 | 3174 | cfa->base != CFI_BP) |
e7c0219b | 3175 | cfi->bp_scratch = true; |
baa41469 JP |
3176 | break; |
3177 | ||
3178 | case OP_DEST_REG_INDIRECT: | |
3179 | ||
e7c0219b PZ |
3180 | if (cfi->drap) { |
3181 | if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { | |
baa41469 JP |
3182 | |
3183 | /* drap: mov %drap, disp(%rbp) */ | |
3184 | cfa->base = CFI_BP_INDIRECT; | |
3185 | cfa->offset = op->dest.offset; | |
3186 | ||
bf4d1a83 | 3187 | /* save drap offset so we know when to restore it */ |
e7c0219b | 3188 | cfi->drap_offset = op->dest.offset; |
f4f80398 | 3189 | } else { |
baa41469 JP |
3190 | |
3191 | /* drap: mov reg, disp(%rbp) */ | |
e7c0219b | 3192 | save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset); |
baa41469 JP |
3193 | } |
3194 | ||
3195 | } else if (op->dest.reg == cfa->base) { | |
3196 | ||
3197 | /* mov reg, disp(%rbp) */ | |
3198 | /* mov reg, disp(%rsp) */ | |
e7c0219b PZ |
3199 | save_reg(cfi, op->src.reg, CFI_CFA, |
3200 | op->dest.offset - cfi->cfa.offset); | |
201ef5a9 JT |
3201 | |
3202 | } else if (op->dest.reg == CFI_SP) { | |
3203 | ||
3204 | /* mov reg, disp(%rsp) */ | |
3205 | save_reg(cfi, op->src.reg, CFI_CFA, | |
3206 | op->dest.offset - cfi->stack_size); | |
aafeb14e PZ |
3207 | |
3208 | } else if (op->src.reg == CFI_SP && op->dest.offset == 0) { | |
3209 | ||
3210 | /* mov %rsp, (%reg); # setup a stack swizzle. */ | |
3211 | cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT; | |
3212 | cfi->vals[op->dest.reg].offset = cfa->offset; | |
baa41469 JP |
3213 | } |
3214 | ||
3215 | break; | |
3216 | ||
baa41469 | 3217 | case OP_DEST_MEM: |
ea24213d | 3218 | if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) { |
baa41469 JP |
3219 | WARN_FUNC("unknown stack-related memory operation", |
3220 | insn->sec, insn->offset); | |
3221 | return -1; | |
3222 | } | |
3223 | ||
3224 | /* pop mem */ | |
e7c0219b | 3225 | cfi->stack_size -= 8; |
baa41469 JP |
3226 | if (cfa->base == CFI_SP) |
3227 | cfa->offset -= 8; | |
3228 | ||
3229 | break; | |
3230 | ||
3231 | default: | |
3232 | WARN_FUNC("unknown stack-related instruction", | |
3233 | insn->sec, insn->offset); | |
3234 | return -1; | |
3235 | } | |
3236 | ||
3237 | return 0; | |
3238 | } | |
3239 | ||
c9c324dc JP |
3240 | /* |
3241 | * The stack layouts of alternatives instructions can sometimes diverge when | |
3242 | * they have stack modifications. That's fine as long as the potential stack | |
3243 | * layouts don't conflict at any given potential instruction boundary. | |
3244 | * | |
3245 | * Flatten the CFIs of the different alternative code streams (both original | |
3246 | * and replacement) into a single shared CFI array which can be used to detect | |
3247 | * conflicts and nicely feed a linear array of ORC entries to the unwinder. | |
3248 | */ | |
3249 | static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn) | |
65ea47dc | 3250 | { |
c9c324dc JP |
3251 | struct cfi_state **alt_cfi; |
3252 | int group_off; | |
65ea47dc | 3253 | |
c9c324dc JP |
3254 | if (!insn->alt_group) |
3255 | return 0; | |
65ea47dc | 3256 | |
8b946cc3 PZ |
3257 | if (!insn->cfi) { |
3258 | WARN("CFI missing"); | |
3259 | return -1; | |
3260 | } | |
3261 | ||
c9c324dc JP |
3262 | alt_cfi = insn->alt_group->cfi; |
3263 | group_off = insn->offset - insn->alt_group->first_insn->offset; | |
65ea47dc | 3264 | |
c9c324dc | 3265 | if (!alt_cfi[group_off]) { |
8b946cc3 | 3266 | alt_cfi[group_off] = insn->cfi; |
c9c324dc | 3267 | } else { |
8b946cc3 | 3268 | if (cficmp(alt_cfi[group_off], insn->cfi)) { |
a706bb08 PZ |
3269 | struct alt_group *orig_group = insn->alt_group->orig_group ?: insn->alt_group; |
3270 | struct instruction *orig = orig_group->first_insn; | |
3271 | char *where = offstr(insn->sec, insn->offset); | |
3272 | WARN_FUNC("stack layout conflict in alternatives: %s", | |
3273 | orig->sec, orig->offset, where); | |
3274 | free(where); | |
ab3852ab PZ |
3275 | return -1; |
3276 | } | |
c9c324dc JP |
3277 | } |
3278 | ||
3279 | return 0; | |
3280 | } | |
3281 | ||
d54dba41 PZ |
3282 | static int handle_insn_ops(struct instruction *insn, |
3283 | struct instruction *next_insn, | |
3284 | struct insn_state *state) | |
c9c324dc JP |
3285 | { |
3286 | struct stack_op *op; | |
3287 | ||
3ee88df1 | 3288 | for (op = insn->stack_ops; op; op = op->next) { |
c9c324dc | 3289 | |
d54dba41 | 3290 | if (update_cfi_state(insn, next_insn, &state->cfi, op)) |
c9c324dc | 3291 | return 1; |
ab3852ab | 3292 | |
ba08abca PZ |
3293 | if (!insn->alt_group) |
3294 | continue; | |
3295 | ||
65ea47dc JT |
3296 | if (op->dest.type == OP_DEST_PUSHF) { |
3297 | if (!state->uaccess_stack) { | |
3298 | state->uaccess_stack = 1; | |
3299 | } else if (state->uaccess_stack >> 31) { | |
3300 | WARN_FUNC("PUSHF stack exhausted", | |
3301 | insn->sec, insn->offset); | |
3302 | return 1; | |
3303 | } | |
3304 | state->uaccess_stack <<= 1; | |
3305 | state->uaccess_stack |= state->uaccess; | |
3306 | } | |
3307 | ||
3308 | if (op->src.type == OP_SRC_POPF) { | |
3309 | if (state->uaccess_stack) { | |
3310 | state->uaccess = state->uaccess_stack & 1; | |
3311 | state->uaccess_stack >>= 1; | |
3312 | if (state->uaccess_stack == 1) | |
3313 | state->uaccess_stack = 0; | |
3314 | } | |
3315 | } | |
3316 | } | |
3317 | ||
3318 | return 0; | |
3319 | } | |
3320 | ||
e7c0219b | 3321 | static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2) |
baa41469 | 3322 | { |
8b946cc3 | 3323 | struct cfi_state *cfi1 = insn->cfi; |
baa41469 JP |
3324 | int i; |
3325 | ||
8b946cc3 PZ |
3326 | if (!cfi1) { |
3327 | WARN("CFI missing"); | |
3328 | return false; | |
3329 | } | |
3330 | ||
e7c0219b PZ |
3331 | if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) { |
3332 | ||
baa41469 JP |
3333 | WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d", |
3334 | insn->sec, insn->offset, | |
e7c0219b PZ |
3335 | cfi1->cfa.base, cfi1->cfa.offset, |
3336 | cfi2->cfa.base, cfi2->cfa.offset); | |
baa41469 | 3337 | |
e7c0219b | 3338 | } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) { |
baa41469 | 3339 | for (i = 0; i < CFI_NUM_REGS; i++) { |
e7c0219b | 3340 | if (!memcmp(&cfi1->regs[i], &cfi2->regs[i], |
baa41469 JP |
3341 | sizeof(struct cfi_reg))) |
3342 | continue; | |
3343 | ||
3344 | WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d", | |
3345 | insn->sec, insn->offset, | |
e7c0219b PZ |
3346 | i, cfi1->regs[i].base, cfi1->regs[i].offset, |
3347 | i, cfi2->regs[i].base, cfi2->regs[i].offset); | |
baa41469 JP |
3348 | break; |
3349 | } | |
3350 | ||
e7c0219b PZ |
3351 | } else if (cfi1->type != cfi2->type) { |
3352 | ||
627fce14 | 3353 | WARN_FUNC("stack state mismatch: type1=%d type2=%d", |
e7c0219b PZ |
3354 | insn->sec, insn->offset, cfi1->type, cfi2->type); |
3355 | ||
3356 | } else if (cfi1->drap != cfi2->drap || | |
3357 | (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) || | |
3358 | (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) { | |
627fce14 | 3359 | |
bf4d1a83 | 3360 | WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)", |
baa41469 | 3361 | insn->sec, insn->offset, |
e7c0219b PZ |
3362 | cfi1->drap, cfi1->drap_reg, cfi1->drap_offset, |
3363 | cfi2->drap, cfi2->drap_reg, cfi2->drap_offset); | |
baa41469 JP |
3364 | |
3365 | } else | |
3366 | return true; | |
3367 | ||
3368 | return false; | |
dcc914f4 JP |
3369 | } |
3370 | ||
ea24213d PZ |
3371 | static inline bool func_uaccess_safe(struct symbol *func) |
3372 | { | |
3373 | if (func) | |
e10cd8fe | 3374 | return func->uaccess_safe; |
ea24213d PZ |
3375 | |
3376 | return false; | |
3377 | } | |
3378 | ||
0c1ddd33 | 3379 | static inline const char *call_dest_name(struct instruction *insn) |
ea24213d | 3380 | { |
82880283 | 3381 | static char pvname[19]; |
db2b0c5d PZ |
3382 | struct reloc *rel; |
3383 | int idx; | |
3384 | ||
c6f5dc28 PZ |
3385 | if (insn_call_dest(insn)) |
3386 | return insn_call_dest(insn)->name; | |
ea24213d | 3387 | |
db2b0c5d PZ |
3388 | rel = insn_reloc(NULL, insn); |
3389 | if (rel && !strcmp(rel->sym->name, "pv_ops")) { | |
3390 | idx = (rel->addend / sizeof(void *)); | |
3391 | snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx); | |
3392 | return pvname; | |
3393 | } | |
3394 | ||
ea24213d PZ |
3395 | return "{dynamic}"; |
3396 | } | |
3397 | ||
db2b0c5d PZ |
3398 | static bool pv_call_dest(struct objtool_file *file, struct instruction *insn) |
3399 | { | |
3400 | struct symbol *target; | |
3401 | struct reloc *rel; | |
3402 | int idx; | |
3403 | ||
3404 | rel = insn_reloc(file, insn); | |
3405 | if (!rel || strcmp(rel->sym->name, "pv_ops")) | |
3406 | return false; | |
3407 | ||
3408 | idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *)); | |
3409 | ||
3410 | if (file->pv_ops[idx].clean) | |
3411 | return true; | |
3412 | ||
3413 | file->pv_ops[idx].clean = true; | |
3414 | ||
3415 | list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) { | |
3416 | if (!target->sec->noinstr) { | |
3417 | WARN("pv_ops[%d]: %s", idx, target->name); | |
3418 | file->pv_ops[idx].clean = false; | |
3419 | } | |
3420 | } | |
3421 | ||
3422 | return file->pv_ops[idx].clean; | |
3423 | } | |
3424 | ||
3425 | static inline bool noinstr_call_dest(struct objtool_file *file, | |
3426 | struct instruction *insn, | |
3427 | struct symbol *func) | |
6b643a07 PZ |
3428 | { |
3429 | /* | |
3430 | * We can't deal with indirect function calls at present; | |
3431 | * assume they're instrumented. | |
3432 | */ | |
db2b0c5d PZ |
3433 | if (!func) { |
3434 | if (file->pv_ops) | |
3435 | return pv_call_dest(file, insn); | |
3436 | ||
6b643a07 | 3437 | return false; |
db2b0c5d | 3438 | } |
6b643a07 PZ |
3439 | |
3440 | /* | |
3441 | * If the symbol is from a noinstr section; we good. | |
3442 | */ | |
3443 | if (func->sec->noinstr) | |
3444 | return true; | |
3445 | ||
2b5a0e42 PZ |
3446 | /* |
3447 | * If the symbol is a static_call trampoline, we can't tell. | |
3448 | */ | |
3449 | if (func->static_call_tramp) | |
3450 | return true; | |
3451 | ||
6b643a07 PZ |
3452 | /* |
3453 | * The __ubsan_handle_*() calls are like WARN(), they only happen when | |
3454 | * something 'BAD' happened. At the risk of taking the machine down, | |
3455 | * let them proceed to get the message out. | |
3456 | */ | |
3457 | if (!strncmp(func->name, "__ubsan_handle_", 15)) | |
3458 | return true; | |
3459 | ||
3460 | return false; | |
3461 | } | |
3462 | ||
db2b0c5d PZ |
3463 | static int validate_call(struct objtool_file *file, |
3464 | struct instruction *insn, | |
3465 | struct insn_state *state) | |
ea24213d | 3466 | { |
c4a33939 | 3467 | if (state->noinstr && state->instr <= 0 && |
c6f5dc28 | 3468 | !noinstr_call_dest(file, insn, insn_call_dest(insn))) { |
c4a33939 PZ |
3469 | WARN_FUNC("call to %s() leaves .noinstr.text section", |
3470 | insn->sec, insn->offset, call_dest_name(insn)); | |
3471 | return 1; | |
3472 | } | |
3473 | ||
c6f5dc28 | 3474 | if (state->uaccess && !func_uaccess_safe(insn_call_dest(insn))) { |
ea24213d | 3475 | WARN_FUNC("call to %s() with UACCESS enabled", |
0c1ddd33 | 3476 | insn->sec, insn->offset, call_dest_name(insn)); |
ea24213d PZ |
3477 | return 1; |
3478 | } | |
3479 | ||
2f0f9e9a PZ |
3480 | if (state->df) { |
3481 | WARN_FUNC("call to %s() with DF set", | |
0c1ddd33 | 3482 | insn->sec, insn->offset, call_dest_name(insn)); |
2f0f9e9a PZ |
3483 | return 1; |
3484 | } | |
3485 | ||
ea24213d PZ |
3486 | return 0; |
3487 | } | |
3488 | ||
db2b0c5d PZ |
3489 | static int validate_sibling_call(struct objtool_file *file, |
3490 | struct instruction *insn, | |
3491 | struct insn_state *state) | |
54262aa2 | 3492 | { |
5a9c361a | 3493 | if (insn_func(insn) && has_modified_stack_frame(insn, state)) { |
54262aa2 PZ |
3494 | WARN_FUNC("sibling call from callable instruction with modified stack frame", |
3495 | insn->sec, insn->offset); | |
3496 | return 1; | |
3497 | } | |
3498 | ||
db2b0c5d | 3499 | return validate_call(file, insn, state); |
54262aa2 PZ |
3500 | } |
3501 | ||
a92e92d1 PZ |
3502 | static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state) |
3503 | { | |
c4a33939 PZ |
3504 | if (state->noinstr && state->instr > 0) { |
3505 | WARN_FUNC("return with instrumentation enabled", | |
3506 | insn->sec, insn->offset); | |
3507 | return 1; | |
3508 | } | |
3509 | ||
a92e92d1 PZ |
3510 | if (state->uaccess && !func_uaccess_safe(func)) { |
3511 | WARN_FUNC("return with UACCESS enabled", | |
3512 | insn->sec, insn->offset); | |
3513 | return 1; | |
3514 | } | |
3515 | ||
3516 | if (!state->uaccess && func_uaccess_safe(func)) { | |
3517 | WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", | |
3518 | insn->sec, insn->offset); | |
3519 | return 1; | |
3520 | } | |
3521 | ||
3522 | if (state->df) { | |
3523 | WARN_FUNC("return with DF set", | |
3524 | insn->sec, insn->offset); | |
3525 | return 1; | |
3526 | } | |
3527 | ||
e25eea89 | 3528 | if (func && has_modified_stack_frame(insn, state)) { |
a92e92d1 PZ |
3529 | WARN_FUNC("return with modified stack frame", |
3530 | insn->sec, insn->offset); | |
3531 | return 1; | |
3532 | } | |
3533 | ||
e7c0219b | 3534 | if (state->cfi.bp_scratch) { |
b2966952 JP |
3535 | WARN_FUNC("BP used as a scratch register", |
3536 | insn->sec, insn->offset); | |
a92e92d1 PZ |
3537 | return 1; |
3538 | } | |
3539 | ||
3540 | return 0; | |
3541 | } | |
3542 | ||
c9c324dc JP |
3543 | static struct instruction *next_insn_to_validate(struct objtool_file *file, |
3544 | struct instruction *insn) | |
7117f16b | 3545 | { |
b23cc71c | 3546 | struct alt_group *alt_group = insn->alt_group; |
7117f16b | 3547 | |
c9c324dc JP |
3548 | /* |
3549 | * Simulate the fact that alternatives are patched in-place. When the | |
3550 | * end of a replacement alt_group is reached, redirect objtool flow to | |
3551 | * the end of the original alt_group. | |
1c34496e PZ |
3552 | * |
3553 | * insn->alts->insn -> alt_group->first_insn | |
3554 | * ... | |
3555 | * alt_group->last_insn | |
3556 | * [alt_group->nop] -> next(orig_group->last_insn) | |
c9c324dc | 3557 | */ |
1c34496e PZ |
3558 | if (alt_group) { |
3559 | if (alt_group->nop) { | |
3560 | /* ->nop implies ->orig_group */ | |
3561 | if (insn == alt_group->last_insn) | |
3562 | return alt_group->nop; | |
3563 | if (insn == alt_group->nop) | |
3564 | goto next_orig; | |
3565 | } | |
3566 | if (insn == alt_group->last_insn && alt_group->orig_group) | |
3567 | goto next_orig; | |
3568 | } | |
c9c324dc JP |
3569 | |
3570 | return next_insn_same_sec(file, insn); | |
1c34496e PZ |
3571 | |
3572 | next_orig: | |
3573 | return next_insn_same_sec(file, alt_group->orig_group->last_insn); | |
7117f16b PZ |
3574 | } |
3575 | ||
dcc914f4 JP |
3576 | /* |
3577 | * Follow the branch starting at the given instruction, and recursively follow | |
3578 | * any other branches (jumps). Meanwhile, track the frame pointer state at | |
3579 | * each instruction and validate all the rules described in | |
d6a21f2d | 3580 | * tools/objtool/Documentation/objtool.txt. |
dcc914f4 | 3581 | */ |
c705cecc | 3582 | static int validate_branch(struct objtool_file *file, struct symbol *func, |
b7460462 | 3583 | struct instruction *insn, struct insn_state state) |
dcc914f4 JP |
3584 | { |
3585 | struct alternative *alt; | |
8b946cc3 | 3586 | struct instruction *next_insn, *prev_insn = NULL; |
dcc914f4 | 3587 | struct section *sec; |
882a0db9 | 3588 | u8 visited; |
dcc914f4 JP |
3589 | int ret; |
3590 | ||
dcc914f4 | 3591 | sec = insn->sec; |
dcc914f4 | 3592 | |
dcc914f4 | 3593 | while (1) { |
c9c324dc | 3594 | next_insn = next_insn_to_validate(file, insn); |
39358a03 | 3595 | |
dbcdbdfd | 3596 | if (func && insn_func(insn) && func != insn_func(insn)->pfunc) { |
3c68a92d | 3597 | /* Ignore KCFI type preambles, which always fall through */ |
9f2899fe PZ |
3598 | if (!strncmp(func->name, "__cfi_", 6) || |
3599 | !strncmp(func->name, "__pfx_", 6)) | |
3c68a92d ST |
3600 | return 0; |
3601 | ||
ee97638b | 3602 | WARN("%s() falls through to next function %s()", |
dbcdbdfd | 3603 | func->name, insn_func(insn)->name); |
ee97638b | 3604 | return 1; |
dcc914f4 JP |
3605 | } |
3606 | ||
4855022a JP |
3607 | if (func && insn->ignore) { |
3608 | WARN_FUNC("BUG: why am I validating an ignored function?", | |
3609 | sec, insn->offset); | |
12b25729 | 3610 | return 1; |
4855022a JP |
3611 | } |
3612 | ||
a09a6e23 PZ |
3613 | visited = VISITED_BRANCH << state.uaccess; |
3614 | if (insn->visited & VISITED_BRANCH_MASK) { | |
e7c0219b | 3615 | if (!insn->hint && !insn_cfi_match(insn, &state.cfi)) |
dcc914f4 | 3616 | return 1; |
dcc914f4 | 3617 | |
882a0db9 | 3618 | if (insn->visited & visited) |
ea24213d | 3619 | return 0; |
8b946cc3 PZ |
3620 | } else { |
3621 | nr_insns_visited++; | |
dcc914f4 JP |
3622 | } |
3623 | ||
c4a33939 PZ |
3624 | if (state.noinstr) |
3625 | state.instr += insn->instr; | |
3626 | ||
8b946cc3 | 3627 | if (insn->hint) { |
8faea26e JP |
3628 | if (insn->restore) { |
3629 | struct instruction *save_insn, *i; | |
3630 | ||
3631 | i = insn; | |
3632 | save_insn = NULL; | |
3633 | ||
3634 | sym_for_each_insn_continue_reverse(file, func, i) { | |
3635 | if (i->save) { | |
3636 | save_insn = i; | |
3637 | break; | |
3638 | } | |
3639 | } | |
3640 | ||
3641 | if (!save_insn) { | |
3642 | WARN_FUNC("no corresponding CFI save for CFI restore", | |
3643 | sec, insn->offset); | |
3644 | return 1; | |
3645 | } | |
3646 | ||
3647 | if (!save_insn->visited) { | |
3648 | WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo", | |
3649 | sec, insn->offset); | |
3650 | return 1; | |
3651 | } | |
3652 | ||
3653 | insn->cfi = save_insn->cfi; | |
3654 | nr_cfi_reused++; | |
3655 | } | |
3656 | ||
8b946cc3 PZ |
3657 | state.cfi = *insn->cfi; |
3658 | } else { | |
3659 | /* XXX track if we actually changed state.cfi */ | |
3660 | ||
3661 | if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) { | |
3662 | insn->cfi = prev_insn->cfi; | |
3663 | nr_cfi_reused++; | |
3664 | } else { | |
3665 | insn->cfi = cfi_hash_find_or_add(&state.cfi); | |
3666 | } | |
3667 | } | |
dcc914f4 | 3668 | |
882a0db9 | 3669 | insn->visited |= visited; |
baa41469 | 3670 | |
c9c324dc JP |
3671 | if (propagate_alt_cfi(file, insn)) |
3672 | return 1; | |
3673 | ||
d5406654 | 3674 | if (!insn->ignore_alts && insn->alts) { |
764eef4b PZ |
3675 | bool skip_orig = false; |
3676 | ||
d5406654 | 3677 | for (alt = insn->alts; alt; alt = alt->next) { |
764eef4b PZ |
3678 | if (alt->skip_orig) |
3679 | skip_orig = true; | |
3680 | ||
c705cecc | 3681 | ret = validate_branch(file, func, alt->insn, state); |
7697eee3 | 3682 | if (ret) { |
2daf7fab | 3683 | if (opts.backtrace) |
7697eee3 PZ |
3684 | BT_FUNC("(alt)", insn); |
3685 | return ret; | |
3686 | } | |
a845c7cf | 3687 | } |
764eef4b PZ |
3688 | |
3689 | if (skip_orig) | |
3690 | return 0; | |
dcc914f4 JP |
3691 | } |
3692 | ||
d54dba41 | 3693 | if (handle_insn_ops(insn, next_insn, &state)) |
60041bcd PZ |
3694 | return 1; |
3695 | ||
dcc914f4 JP |
3696 | switch (insn->type) { |
3697 | ||
dcc914f4 | 3698 | case INSN_RETURN: |
a92e92d1 | 3699 | return validate_return(func, insn, &state); |
dcc914f4 JP |
3700 | |
3701 | case INSN_CALL: | |
ea24213d | 3702 | case INSN_CALL_DYNAMIC: |
db2b0c5d | 3703 | ret = validate_call(file, insn, &state); |
ea24213d PZ |
3704 | if (ret) |
3705 | return ret; | |
dcc914f4 | 3706 | |
72064474 | 3707 | if (opts.stackval && func && !is_fentry_call(insn) && |
c9bab22b | 3708 | !has_valid_stack_frame(&state)) { |
dcc914f4 JP |
3709 | WARN_FUNC("call without frame pointer save/setup", |
3710 | sec, insn->offset); | |
3711 | return 1; | |
3712 | } | |
c9bab22b | 3713 | |
0e5b613b | 3714 | if (insn->dead_end) |
c9bab22b JP |
3715 | return 0; |
3716 | ||
dcc914f4 JP |
3717 | break; |
3718 | ||
3719 | case INSN_JUMP_CONDITIONAL: | |
3720 | case INSN_JUMP_UNCONDITIONAL: | |
ecf11ba4 | 3721 | if (is_sibling_call(insn)) { |
db2b0c5d | 3722 | ret = validate_sibling_call(file, insn, &state); |
dcc914f4 | 3723 | if (ret) |
54262aa2 | 3724 | return ret; |
4855022a | 3725 | |
0c1ddd33 | 3726 | } else if (insn->jump_dest) { |
c705cecc JP |
3727 | ret = validate_branch(file, func, |
3728 | insn->jump_dest, state); | |
7697eee3 | 3729 | if (ret) { |
2daf7fab | 3730 | if (opts.backtrace) |
7697eee3 PZ |
3731 | BT_FUNC("(branch)", insn); |
3732 | return ret; | |
3733 | } | |
4855022a | 3734 | } |
dcc914f4 JP |
3735 | |
3736 | if (insn->type == INSN_JUMP_UNCONDITIONAL) | |
3737 | return 0; | |
3738 | ||
3739 | break; | |
3740 | ||
3741 | case INSN_JUMP_DYNAMIC: | |
b68b9907 | 3742 | case INSN_JUMP_DYNAMIC_CONDITIONAL: |
ecf11ba4 | 3743 | if (is_sibling_call(insn)) { |
db2b0c5d | 3744 | ret = validate_sibling_call(file, insn, &state); |
54262aa2 PZ |
3745 | if (ret) |
3746 | return ret; | |
dcc914f4 JP |
3747 | } |
3748 | ||
b68b9907 JP |
3749 | if (insn->type == INSN_JUMP_DYNAMIC) |
3750 | return 0; | |
3751 | ||
3752 | break; | |
dcc914f4 | 3753 | |
39358a03 JP |
3754 | case INSN_CONTEXT_SWITCH: |
3755 | if (func && (!next_insn || !next_insn->hint)) { | |
3756 | WARN_FUNC("unsupported instruction in callable function", | |
3757 | sec, insn->offset); | |
3758 | return 1; | |
3759 | } | |
3760 | return 0; | |
3761 | ||
ea24213d PZ |
3762 | case INSN_STAC: |
3763 | if (state.uaccess) { | |
3764 | WARN_FUNC("recursive UACCESS enable", sec, insn->offset); | |
3765 | return 1; | |
3766 | } | |
3767 | ||
3768 | state.uaccess = true; | |
3769 | break; | |
3770 | ||
3771 | case INSN_CLAC: | |
c705cecc | 3772 | if (!state.uaccess && func) { |
ea24213d PZ |
3773 | WARN_FUNC("redundant UACCESS disable", sec, insn->offset); |
3774 | return 1; | |
3775 | } | |
3776 | ||
3777 | if (func_uaccess_safe(func) && !state.uaccess_stack) { | |
3778 | WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset); | |
3779 | return 1; | |
3780 | } | |
3781 | ||
3782 | state.uaccess = false; | |
baa41469 JP |
3783 | break; |
3784 | ||
2f0f9e9a | 3785 | case INSN_STD: |
6f567c93 | 3786 | if (state.df) { |
2f0f9e9a | 3787 | WARN_FUNC("recursive STD", sec, insn->offset); |
6f567c93 JP |
3788 | return 1; |
3789 | } | |
2f0f9e9a PZ |
3790 | |
3791 | state.df = true; | |
3792 | break; | |
3793 | ||
3794 | case INSN_CLD: | |
6f567c93 | 3795 | if (!state.df && func) { |
2f0f9e9a | 3796 | WARN_FUNC("redundant CLD", sec, insn->offset); |
6f567c93 JP |
3797 | return 1; |
3798 | } | |
2f0f9e9a PZ |
3799 | |
3800 | state.df = false; | |
baa41469 JP |
3801 | break; |
3802 | ||
dcc914f4 JP |
3803 | default: |
3804 | break; | |
3805 | } | |
3806 | ||
3807 | if (insn->dead_end) | |
3808 | return 0; | |
3809 | ||
00d96180 | 3810 | if (!next_insn) { |
e7c0219b | 3811 | if (state.cfi.cfa.base == CFI_UNDEFINED) |
00d96180 | 3812 | return 0; |
dcc914f4 JP |
3813 | WARN("%s: unexpected end of section", sec->name); |
3814 | return 1; | |
3815 | } | |
00d96180 | 3816 | |
8b946cc3 | 3817 | prev_insn = insn; |
00d96180 | 3818 | insn = next_insn; |
dcc914f4 JP |
3819 | } |
3820 | ||
3821 | return 0; | |
3822 | } | |
3823 | ||
1c34496e PZ |
3824 | static int validate_unwind_hint(struct objtool_file *file, |
3825 | struct instruction *insn, | |
3826 | struct insn_state *state) | |
3827 | { | |
3828 | if (insn->hint && !insn->visited && !insn->ignore) { | |
3829 | int ret = validate_branch(file, insn_func(insn), insn, *state); | |
3830 | if (ret && opts.backtrace) | |
3831 | BT_FUNC("<=== (hint)", insn); | |
3832 | return ret; | |
3833 | } | |
3834 | ||
3835 | return 0; | |
3836 | } | |
3837 | ||
932f8e98 | 3838 | static int validate_unwind_hints(struct objtool_file *file, struct section *sec) |
39358a03 JP |
3839 | { |
3840 | struct instruction *insn; | |
39358a03 | 3841 | struct insn_state state; |
1c34496e | 3842 | int warnings = 0; |
39358a03 JP |
3843 | |
3844 | if (!file->hints) | |
3845 | return 0; | |
3846 | ||
753da417 | 3847 | init_insn_state(file, &state, sec); |
39358a03 | 3848 | |
932f8e98 | 3849 | if (sec) { |
1c34496e PZ |
3850 | sec_for_each_insn(file, sec, insn) |
3851 | warnings += validate_unwind_hint(file, insn, &state); | |
932f8e98 | 3852 | } else { |
1c34496e PZ |
3853 | for_each_insn(file, insn) |
3854 | warnings += validate_unwind_hint(file, insn, &state); | |
39358a03 JP |
3855 | } |
3856 | ||
3857 | return warnings; | |
3858 | } | |
3859 | ||
a09a6e23 PZ |
3860 | /* |
3861 | * Validate rethunk entry constraint: must untrain RET before the first RET. | |
3862 | * | |
3863 | * Follow every branch (intra-function) and ensure ANNOTATE_UNRET_END comes | |
3864 | * before an actual RET instruction. | |
3865 | */ | |
3866 | static int validate_entry(struct objtool_file *file, struct instruction *insn) | |
3867 | { | |
3868 | struct instruction *next, *dest; | |
3869 | int ret, warnings = 0; | |
3870 | ||
3871 | for (;;) { | |
3872 | next = next_insn_to_validate(file, insn); | |
3873 | ||
3874 | if (insn->visited & VISITED_ENTRY) | |
3875 | return 0; | |
3876 | ||
3877 | insn->visited |= VISITED_ENTRY; | |
3878 | ||
d5406654 | 3879 | if (!insn->ignore_alts && insn->alts) { |
a09a6e23 PZ |
3880 | struct alternative *alt; |
3881 | bool skip_orig = false; | |
3882 | ||
d5406654 | 3883 | for (alt = insn->alts; alt; alt = alt->next) { |
a09a6e23 PZ |
3884 | if (alt->skip_orig) |
3885 | skip_orig = true; | |
3886 | ||
3887 | ret = validate_entry(file, alt->insn); | |
3888 | if (ret) { | |
3889 | if (opts.backtrace) | |
3890 | BT_FUNC("(alt)", insn); | |
3891 | return ret; | |
3892 | } | |
3893 | } | |
3894 | ||
3895 | if (skip_orig) | |
3896 | return 0; | |
3897 | } | |
3898 | ||
3899 | switch (insn->type) { | |
3900 | ||
3901 | case INSN_CALL_DYNAMIC: | |
3902 | case INSN_JUMP_DYNAMIC: | |
3903 | case INSN_JUMP_DYNAMIC_CONDITIONAL: | |
3904 | WARN_FUNC("early indirect call", insn->sec, insn->offset); | |
3905 | return 1; | |
3906 | ||
3907 | case INSN_JUMP_UNCONDITIONAL: | |
3908 | case INSN_JUMP_CONDITIONAL: | |
3909 | if (!is_sibling_call(insn)) { | |
3910 | if (!insn->jump_dest) { | |
3911 | WARN_FUNC("unresolved jump target after linking?!?", | |
3912 | insn->sec, insn->offset); | |
3913 | return -1; | |
3914 | } | |
3915 | ret = validate_entry(file, insn->jump_dest); | |
3916 | if (ret) { | |
3917 | if (opts.backtrace) { | |
3918 | BT_FUNC("(branch%s)", insn, | |
3919 | insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : ""); | |
3920 | } | |
3921 | return ret; | |
3922 | } | |
3923 | ||
3924 | if (insn->type == INSN_JUMP_UNCONDITIONAL) | |
3925 | return 0; | |
3926 | ||
3927 | break; | |
3928 | } | |
3929 | ||
3930 | /* fallthrough */ | |
3931 | case INSN_CALL: | |
c6f5dc28 PZ |
3932 | dest = find_insn(file, insn_call_dest(insn)->sec, |
3933 | insn_call_dest(insn)->offset); | |
a09a6e23 PZ |
3934 | if (!dest) { |
3935 | WARN("Unresolved function after linking!?: %s", | |
c6f5dc28 | 3936 | insn_call_dest(insn)->name); |
a09a6e23 PZ |
3937 | return -1; |
3938 | } | |
3939 | ||
3940 | ret = validate_entry(file, dest); | |
3941 | if (ret) { | |
3942 | if (opts.backtrace) | |
3943 | BT_FUNC("(call)", insn); | |
3944 | return ret; | |
3945 | } | |
3946 | /* | |
3947 | * If a call returns without error, it must have seen UNTRAIN_RET. | |
3948 | * Therefore any non-error return is a success. | |
3949 | */ | |
3950 | return 0; | |
3951 | ||
3952 | case INSN_RETURN: | |
3953 | WARN_FUNC("RET before UNTRAIN", insn->sec, insn->offset); | |
3954 | return 1; | |
3955 | ||
3956 | case INSN_NOP: | |
3957 | if (insn->retpoline_safe) | |
3958 | return 0; | |
3959 | break; | |
3960 | ||
3961 | default: | |
3962 | break; | |
3963 | } | |
3964 | ||
3965 | if (!next) { | |
3966 | WARN_FUNC("teh end!", insn->sec, insn->offset); | |
3967 | return -1; | |
3968 | } | |
3969 | insn = next; | |
3970 | } | |
3971 | ||
3972 | return warnings; | |
3973 | } | |
3974 | ||
3975 | /* | |
3976 | * Validate that all branches starting at 'insn->entry' encounter UNRET_END | |
3977 | * before RET. | |
3978 | */ | |
3979 | static int validate_unret(struct objtool_file *file) | |
3980 | { | |
3981 | struct instruction *insn; | |
3982 | int ret, warnings = 0; | |
3983 | ||
3984 | for_each_insn(file, insn) { | |
3985 | if (!insn->entry) | |
3986 | continue; | |
3987 | ||
3988 | ret = validate_entry(file, insn); | |
3989 | if (ret < 0) { | |
3990 | WARN_FUNC("Failed UNRET validation", insn->sec, insn->offset); | |
3991 | return ret; | |
3992 | } | |
3993 | warnings += ret; | |
3994 | } | |
3995 | ||
3996 | return warnings; | |
3997 | } | |
3998 | ||
b5bc2231 PZ |
3999 | static int validate_retpoline(struct objtool_file *file) |
4000 | { | |
4001 | struct instruction *insn; | |
4002 | int warnings = 0; | |
4003 | ||
4004 | for_each_insn(file, insn) { | |
4005 | if (insn->type != INSN_JUMP_DYNAMIC && | |
9bb2ec60 PZ |
4006 | insn->type != INSN_CALL_DYNAMIC && |
4007 | insn->type != INSN_RETURN) | |
b5bc2231 PZ |
4008 | continue; |
4009 | ||
4010 | if (insn->retpoline_safe) | |
4011 | continue; | |
4012 | ||
6644ee84 | 4013 | if (insn->sec->init) |
ca41b97e PZ |
4014 | continue; |
4015 | ||
9bb2ec60 | 4016 | if (insn->type == INSN_RETURN) { |
f43b9876 PZ |
4017 | if (opts.rethunk) { |
4018 | WARN_FUNC("'naked' return found in RETHUNK build", | |
4019 | insn->sec, insn->offset); | |
4020 | } else | |
4021 | continue; | |
9bb2ec60 PZ |
4022 | } else { |
4023 | WARN_FUNC("indirect %s found in RETPOLINE build", | |
4024 | insn->sec, insn->offset, | |
4025 | insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call"); | |
4026 | } | |
b5bc2231 PZ |
4027 | |
4028 | warnings++; | |
4029 | } | |
4030 | ||
4031 | return warnings; | |
4032 | } | |
4033 | ||
dcc914f4 JP |
4034 | static bool is_kasan_insn(struct instruction *insn) |
4035 | { | |
4036 | return (insn->type == INSN_CALL && | |
c6f5dc28 | 4037 | !strcmp(insn_call_dest(insn)->name, "__asan_handle_no_return")); |
dcc914f4 JP |
4038 | } |
4039 | ||
4040 | static bool is_ubsan_insn(struct instruction *insn) | |
4041 | { | |
4042 | return (insn->type == INSN_CALL && | |
c6f5dc28 | 4043 | !strcmp(insn_call_dest(insn)->name, |
dcc914f4 JP |
4044 | "__ubsan_handle_builtin_unreachable")); |
4045 | } | |
4046 | ||
14db1f0a | 4047 | static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn) |
dcc914f4 JP |
4048 | { |
4049 | int i; | |
14db1f0a | 4050 | struct instruction *prev_insn; |
dcc914f4 | 4051 | |
1ffbe4e9 | 4052 | if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP) |
baa41469 JP |
4053 | return true; |
4054 | ||
4055 | /* | |
82a8954a | 4056 | * Ignore alternative replacement instructions. This can happen |
0e2bb2bc | 4057 | * when a whitelisted function uses one of the ALTERNATIVE macros. |
baa41469 | 4058 | */ |
82a8954a | 4059 | if (!strcmp(insn->sec->name, ".altinstr_replacement") || |
0e2bb2bc | 4060 | !strcmp(insn->sec->name, ".altinstr_aux")) |
dcc914f4 JP |
4061 | return true; |
4062 | ||
4adb2368 | 4063 | /* |
753da417 | 4064 | * Whole archive runs might encounter dead code from weak symbols. |
4adb2368 PZ |
4065 | * This is where the linker will have dropped the weak symbol in |
4066 | * favour of a regular symbol, but leaves the code in place. | |
4067 | * | |
4068 | * In this case we'll find a piece of code (whole function) that is not | |
4069 | * covered by a !section symbol. Ignore them. | |
4070 | */ | |
dbcdbdfd | 4071 | if (opts.link && !insn_func(insn)) { |
4adb2368 PZ |
4072 | int size = find_symbol_hole_containing(insn->sec, insn->offset); |
4073 | unsigned long end = insn->offset + size; | |
4074 | ||
4075 | if (!size) /* not a hole */ | |
4076 | return false; | |
4077 | ||
4078 | if (size < 0) /* hole until the end */ | |
4079 | return true; | |
4080 | ||
4081 | sec_for_each_insn_continue(file, insn) { | |
4082 | /* | |
4083 | * If we reach a visited instruction at or before the | |
4084 | * end of the hole, ignore the unreachable. | |
4085 | */ | |
4086 | if (insn->visited) | |
4087 | return true; | |
4088 | ||
4089 | if (insn->offset >= end) | |
4090 | break; | |
4091 | ||
4092 | /* | |
4093 | * If this hole jumps to a .cold function, mark it ignore too. | |
4094 | */ | |
dbcdbdfd PZ |
4095 | if (insn->jump_dest && insn_func(insn->jump_dest) && |
4096 | strstr(insn_func(insn->jump_dest)->name, ".cold")) { | |
4adb2368 | 4097 | struct instruction *dest = insn->jump_dest; |
dbcdbdfd | 4098 | func_for_each_insn(file, insn_func(dest), dest) |
4adb2368 PZ |
4099 | dest->ignore = true; |
4100 | } | |
4101 | } | |
4102 | ||
4103 | return false; | |
4104 | } | |
4105 | ||
dbcdbdfd | 4106 | if (!insn_func(insn)) |
bd841d61 JP |
4107 | return false; |
4108 | ||
dbcdbdfd | 4109 | if (insn_func(insn)->static_call_tramp) |
2105a927 PZ |
4110 | return true; |
4111 | ||
bd841d61 JP |
4112 | /* |
4113 | * CONFIG_UBSAN_TRAP inserts a UD2 when it sees | |
4114 | * __builtin_unreachable(). The BUG() macro has an unreachable() after | |
4115 | * the UD2, which causes GCC's undefined trap logic to emit another UD2 | |
4116 | * (or occasionally a JMP to UD2). | |
14db1f0a IH |
4117 | * |
4118 | * It may also insert a UD2 after calling a __noreturn function. | |
bd841d61 | 4119 | */ |
1c34496e | 4120 | prev_insn = prev_insn_same_sec(file, insn); |
c6f5dc28 PZ |
4121 | if ((prev_insn->dead_end || |
4122 | dead_end_function(file, insn_call_dest(prev_insn))) && | |
bd841d61 JP |
4123 | (insn->type == INSN_BUG || |
4124 | (insn->type == INSN_JUMP_UNCONDITIONAL && | |
4125 | insn->jump_dest && insn->jump_dest->type == INSN_BUG))) | |
4126 | return true; | |
4127 | ||
dcc914f4 JP |
4128 | /* |
4129 | * Check if this (or a subsequent) instruction is related to | |
4130 | * CONFIG_UBSAN or CONFIG_KASAN. | |
4131 | * | |
4132 | * End the search at 5 instructions to avoid going into the weeds. | |
4133 | */ | |
4134 | for (i = 0; i < 5; i++) { | |
4135 | ||
4136 | if (is_kasan_insn(insn) || is_ubsan_insn(insn)) | |
4137 | return true; | |
4138 | ||
fe24e271 JP |
4139 | if (insn->type == INSN_JUMP_UNCONDITIONAL) { |
4140 | if (insn->jump_dest && | |
dbcdbdfd | 4141 | insn_func(insn->jump_dest) == insn_func(insn)) { |
fe24e271 JP |
4142 | insn = insn->jump_dest; |
4143 | continue; | |
4144 | } | |
4145 | ||
4146 | break; | |
dcc914f4 JP |
4147 | } |
4148 | ||
dbcdbdfd | 4149 | if (insn->offset + insn->len >= insn_func(insn)->offset + insn_func(insn)->len) |
dcc914f4 | 4150 | break; |
fe24e271 | 4151 | |
1c34496e | 4152 | insn = next_insn_same_sec(file, insn); |
dcc914f4 JP |
4153 | } |
4154 | ||
4155 | return false; | |
4156 | } | |
4157 | ||
9f2899fe PZ |
4158 | static int add_prefix_symbol(struct objtool_file *file, struct symbol *func, |
4159 | struct instruction *insn) | |
4160 | { | |
4161 | if (!opts.prefix) | |
4162 | return 0; | |
4163 | ||
4164 | for (;;) { | |
1c34496e | 4165 | struct instruction *prev = prev_insn_same_sec(file, insn); |
9f2899fe PZ |
4166 | u64 offset; |
4167 | ||
1c34496e | 4168 | if (!prev) |
9f2899fe PZ |
4169 | break; |
4170 | ||
4171 | if (prev->type != INSN_NOP) | |
4172 | break; | |
4173 | ||
4174 | offset = func->offset - prev->offset; | |
4175 | if (offset >= opts.prefix) { | |
023f2340 PZ |
4176 | if (offset == opts.prefix) { |
4177 | /* | |
4178 | * Since the sec->symbol_list is ordered by | |
4179 | * offset (see elf_add_symbol()) the added | |
4180 | * symbol will not be seen by the iteration in | |
4181 | * validate_section(). | |
4182 | * | |
4183 | * Hence the lack of list_for_each_entry_safe() | |
4184 | * there. | |
4185 | * | |
4186 | * The direct concequence is that prefix symbols | |
4187 | * don't get visited (because pointless), except | |
4188 | * for the logic in ignore_unreachable_insn() | |
4189 | * that needs the terminating insn to be visited | |
4190 | * otherwise it will report the hole. | |
4191 | * | |
4192 | * Hence mark the first instruction of the | |
4193 | * prefix symbol as visisted. | |
4194 | */ | |
4195 | prev->visited |= VISITED_BRANCH; | |
9f2899fe | 4196 | elf_create_prefix_symbol(file->elf, func, opts.prefix); |
023f2340 | 4197 | } |
9f2899fe PZ |
4198 | break; |
4199 | } | |
4200 | insn = prev; | |
4201 | } | |
4202 | ||
4203 | return 0; | |
4204 | } | |
4205 | ||
4b5e2e7f PZ |
4206 | static int validate_symbol(struct objtool_file *file, struct section *sec, |
4207 | struct symbol *sym, struct insn_state *state) | |
dcc914f4 | 4208 | { |
dcc914f4 | 4209 | struct instruction *insn; |
4b5e2e7f PZ |
4210 | int ret; |
4211 | ||
4212 | if (!sym->len) { | |
4213 | WARN("%s() is missing an ELF size annotation", sym->name); | |
4214 | return 1; | |
4215 | } | |
4216 | ||
4217 | if (sym->pfunc != sym || sym->alias != sym) | |
4218 | return 0; | |
4219 | ||
4220 | insn = find_insn(file, sec, sym->offset); | |
4221 | if (!insn || insn->ignore || insn->visited) | |
4222 | return 0; | |
4223 | ||
9f2899fe PZ |
4224 | add_prefix_symbol(file, sym, insn); |
4225 | ||
4b5e2e7f PZ |
4226 | state->uaccess = sym->uaccess_safe; |
4227 | ||
dbcdbdfd | 4228 | ret = validate_branch(file, insn_func(insn), insn, *state); |
2daf7fab | 4229 | if (ret && opts.backtrace) |
4b5e2e7f PZ |
4230 | BT_FUNC("<=== (sym)", insn); |
4231 | return ret; | |
4232 | } | |
4233 | ||
4234 | static int validate_section(struct objtool_file *file, struct section *sec) | |
4235 | { | |
baa41469 | 4236 | struct insn_state state; |
4b5e2e7f PZ |
4237 | struct symbol *func; |
4238 | int warnings = 0; | |
dcc914f4 | 4239 | |
350994bf PZ |
4240 | list_for_each_entry(func, &sec->symbol_list, list) { |
4241 | if (func->type != STT_FUNC) | |
4242 | continue; | |
e10cd8fe | 4243 | |
753da417 | 4244 | init_insn_state(file, &state, sec); |
b735bd3e | 4245 | set_func_state(&state.cfi); |
0699e551 | 4246 | |
4b5e2e7f | 4247 | warnings += validate_symbol(file, sec, func, &state); |
dcc914f4 JP |
4248 | } |
4249 | ||
dcc914f4 JP |
4250 | return warnings; |
4251 | } | |
4252 | ||
753da417 | 4253 | static int validate_noinstr_sections(struct objtool_file *file) |
c4a33939 PZ |
4254 | { |
4255 | struct section *sec; | |
932f8e98 | 4256 | int warnings = 0; |
c4a33939 PZ |
4257 | |
4258 | sec = find_section_by_name(file->elf, ".noinstr.text"); | |
0cc9ac8d TG |
4259 | if (sec) { |
4260 | warnings += validate_section(file, sec); | |
4261 | warnings += validate_unwind_hints(file, sec); | |
4262 | } | |
c4a33939 | 4263 | |
0cc9ac8d TG |
4264 | sec = find_section_by_name(file->elf, ".entry.text"); |
4265 | if (sec) { | |
4266 | warnings += validate_section(file, sec); | |
4267 | warnings += validate_unwind_hints(file, sec); | |
4268 | } | |
932f8e98 | 4269 | |
2b5a0e42 PZ |
4270 | sec = find_section_by_name(file->elf, ".cpuidle.text"); |
4271 | if (sec) { | |
4272 | warnings += validate_section(file, sec); | |
4273 | warnings += validate_unwind_hints(file, sec); | |
4274 | } | |
4275 | ||
932f8e98 | 4276 | return warnings; |
c4a33939 PZ |
4277 | } |
4278 | ||
350994bf PZ |
4279 | static int validate_functions(struct objtool_file *file) |
4280 | { | |
4281 | struct section *sec; | |
4282 | int warnings = 0; | |
4283 | ||
da837bd6 PZ |
4284 | for_each_sec(file, sec) { |
4285 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) | |
4286 | continue; | |
4287 | ||
350994bf | 4288 | warnings += validate_section(file, sec); |
da837bd6 | 4289 | } |
350994bf PZ |
4290 | |
4291 | return warnings; | |
4292 | } | |
4293 | ||
3c6f9f77 | 4294 | static void mark_endbr_used(struct instruction *insn) |
08f87a93 | 4295 | { |
3c6f9f77 JP |
4296 | if (!list_empty(&insn->call_node)) |
4297 | list_del_init(&insn->call_node); | |
4298 | } | |
4299 | ||
08ef8c40 PZ |
4300 | static bool noendbr_range(struct objtool_file *file, struct instruction *insn) |
4301 | { | |
4302 | struct symbol *sym = find_symbol_containing(insn->sec, insn->offset-1); | |
4303 | struct instruction *first; | |
4304 | ||
4305 | if (!sym) | |
4306 | return false; | |
4307 | ||
4308 | first = find_insn(file, sym->sec, sym->offset); | |
4309 | if (!first) | |
4310 | return false; | |
4311 | ||
4312 | if (first->type != INSN_ENDBR && !first->noendbr) | |
4313 | return false; | |
4314 | ||
4315 | return insn->offset == sym->offset + sym->len; | |
4316 | } | |
4317 | ||
3c6f9f77 JP |
4318 | static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn) |
4319 | { | |
4320 | struct instruction *dest; | |
08f87a93 | 4321 | struct reloc *reloc; |
3c6f9f77 JP |
4322 | unsigned long off; |
4323 | int warnings = 0; | |
08f87a93 | 4324 | |
3c6f9f77 JP |
4325 | /* |
4326 | * Looking for function pointer load relocations. Ignore | |
4327 | * direct/indirect branches: | |
4328 | */ | |
4329 | switch (insn->type) { | |
4330 | case INSN_CALL: | |
4331 | case INSN_CALL_DYNAMIC: | |
4332 | case INSN_JUMP_CONDITIONAL: | |
4333 | case INSN_JUMP_UNCONDITIONAL: | |
4334 | case INSN_JUMP_DYNAMIC: | |
4335 | case INSN_JUMP_DYNAMIC_CONDITIONAL: | |
4336 | case INSN_RETURN: | |
4337 | case INSN_NOP: | |
4338 | return 0; | |
4339 | default: | |
4340 | break; | |
4341 | } | |
08f87a93 | 4342 | |
3c6f9f77 JP |
4343 | for (reloc = insn_reloc(file, insn); |
4344 | reloc; | |
4345 | reloc = find_reloc_by_dest_range(file->elf, insn->sec, | |
4346 | reloc->offset + 1, | |
4347 | (insn->offset + insn->len) - (reloc->offset + 1))) { | |
08f87a93 | 4348 | |
3c6f9f77 JP |
4349 | /* |
4350 | * static_call_update() references the trampoline, which | |
4351 | * doesn't have (or need) ENDBR. Skip warning in that case. | |
4352 | */ | |
4353 | if (reloc->sym->static_call_tramp) | |
08f87a93 PZ |
4354 | continue; |
4355 | ||
3c6f9f77 JP |
4356 | off = reloc->sym->offset; |
4357 | if (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32) | |
4358 | off += arch_dest_reloc_offset(reloc->addend); | |
4359 | else | |
4360 | off += reloc->addend; | |
4361 | ||
4362 | dest = find_insn(file, reloc->sym->sec, off); | |
4363 | if (!dest) | |
08f87a93 PZ |
4364 | continue; |
4365 | ||
3c6f9f77 JP |
4366 | if (dest->type == INSN_ENDBR) { |
4367 | mark_endbr_used(dest); | |
08f87a93 | 4368 | continue; |
3c6f9f77 | 4369 | } |
08f87a93 | 4370 | |
dbcdbdfd | 4371 | if (insn_func(dest) && insn_func(dest) == insn_func(insn)) { |
3c6f9f77 JP |
4372 | /* |
4373 | * Anything from->to self is either _THIS_IP_ or | |
4374 | * IRET-to-self. | |
4375 | * | |
4376 | * There is no sane way to annotate _THIS_IP_ since the | |
4377 | * compiler treats the relocation as a constant and is | |
4378 | * happy to fold in offsets, skewing any annotation we | |
4379 | * do, leading to vast amounts of false-positives. | |
4380 | * | |
4381 | * There's also compiler generated _THIS_IP_ through | |
4382 | * KCOV and such which we have no hope of annotating. | |
4383 | * | |
4384 | * As such, blanket accept self-references without | |
4385 | * issue. | |
4386 | */ | |
08f87a93 | 4387 | continue; |
3c6f9f77 | 4388 | } |
08f87a93 | 4389 | |
08ef8c40 PZ |
4390 | /* |
4391 | * Accept anything ANNOTATE_NOENDBR. | |
4392 | */ | |
3c6f9f77 | 4393 | if (dest->noendbr) |
08f87a93 PZ |
4394 | continue; |
4395 | ||
08ef8c40 PZ |
4396 | /* |
4397 | * Accept if this is the instruction after a symbol | |
4398 | * that is (no)endbr -- typical code-range usage. | |
4399 | */ | |
4400 | if (noendbr_range(file, dest)) | |
4401 | continue; | |
4402 | ||
3c6f9f77 JP |
4403 | WARN_FUNC("relocation to !ENDBR: %s", |
4404 | insn->sec, insn->offset, | |
4405 | offstr(dest->sec, dest->offset)); | |
4406 | ||
4407 | warnings++; | |
4408 | } | |
4409 | ||
4410 | return warnings; | |
4411 | } | |
4412 | ||
4413 | static int validate_ibt_data_reloc(struct objtool_file *file, | |
4414 | struct reloc *reloc) | |
4415 | { | |
4416 | struct instruction *dest; | |
4417 | ||
4418 | dest = find_insn(file, reloc->sym->sec, | |
4419 | reloc->sym->offset + reloc->addend); | |
4420 | if (!dest) | |
4421 | return 0; | |
4422 | ||
4423 | if (dest->type == INSN_ENDBR) { | |
4424 | mark_endbr_used(dest); | |
4425 | return 0; | |
4426 | } | |
4427 | ||
4428 | if (dest->noendbr) | |
4429 | return 0; | |
4430 | ||
4431 | WARN_FUNC("data relocation to !ENDBR: %s", | |
4432 | reloc->sec->base, reloc->offset, | |
4433 | offstr(dest->sec, dest->offset)); | |
4434 | ||
4435 | return 1; | |
4436 | } | |
4437 | ||
4438 | /* | |
4439 | * Validate IBT rules and remove used ENDBR instructions from the seal list. | |
4440 | * Unused ENDBR instructions will be annotated for sealing (i.e., replaced with | |
4441 | * NOPs) later, in create_ibt_endbr_seal_sections(). | |
4442 | */ | |
4443 | static int validate_ibt(struct objtool_file *file) | |
4444 | { | |
4445 | struct section *sec; | |
4446 | struct reloc *reloc; | |
4447 | struct instruction *insn; | |
4448 | int warnings = 0; | |
4449 | ||
4450 | for_each_insn(file, insn) | |
4451 | warnings += validate_ibt_insn(file, insn); | |
4452 | ||
4453 | for_each_sec(file, sec) { | |
4454 | ||
4455 | /* Already done by validate_ibt_insn() */ | |
4456 | if (sec->sh.sh_flags & SHF_EXECINSTR) | |
08f87a93 PZ |
4457 | continue; |
4458 | ||
3c6f9f77 JP |
4459 | if (!sec->reloc) |
4460 | continue; | |
08f87a93 | 4461 | |
3c6f9f77 JP |
4462 | /* |
4463 | * These sections can reference text addresses, but not with | |
4464 | * the intent to indirect branch to them. | |
4465 | */ | |
e27e5bea JP |
4466 | if ((!strncmp(sec->name, ".discard", 8) && |
4467 | strcmp(sec->name, ".discard.ibt_endbr_noseal")) || | |
3c6f9f77 JP |
4468 | !strncmp(sec->name, ".debug", 6) || |
4469 | !strcmp(sec->name, ".altinstructions") || | |
4470 | !strcmp(sec->name, ".ibt_endbr_seal") || | |
4471 | !strcmp(sec->name, ".orc_unwind_ip") || | |
4472 | !strcmp(sec->name, ".parainstructions") || | |
4473 | !strcmp(sec->name, ".retpoline_sites") || | |
4474 | !strcmp(sec->name, ".smp_locks") || | |
4475 | !strcmp(sec->name, ".static_call_sites") || | |
4476 | !strcmp(sec->name, "_error_injection_whitelist") || | |
4477 | !strcmp(sec->name, "_kprobe_blacklist") || | |
4478 | !strcmp(sec->name, "__bug_table") || | |
4479 | !strcmp(sec->name, "__ex_table") || | |
4480 | !strcmp(sec->name, "__jump_table") || | |
3c68a92d | 4481 | !strcmp(sec->name, "__mcount_loc") || |
0326074f | 4482 | !strcmp(sec->name, ".kcfi_traps") || |
9440155c | 4483 | strstr(sec->name, "__patchable_function_entries")) |
3c6f9f77 | 4484 | continue; |
08f87a93 | 4485 | |
3c6f9f77 JP |
4486 | list_for_each_entry(reloc, &sec->reloc->reloc_list, list) |
4487 | warnings += validate_ibt_data_reloc(file, reloc); | |
08f87a93 PZ |
4488 | } |
4489 | ||
3c6f9f77 | 4490 | return warnings; |
08f87a93 PZ |
4491 | } |
4492 | ||
c2bdd61c JP |
4493 | static int validate_sls(struct objtool_file *file) |
4494 | { | |
4495 | struct instruction *insn, *next_insn; | |
4496 | int warnings = 0; | |
4497 | ||
4498 | for_each_insn(file, insn) { | |
4499 | next_insn = next_insn_same_sec(file, insn); | |
4500 | ||
4501 | if (insn->retpoline_safe) | |
4502 | continue; | |
4503 | ||
4504 | switch (insn->type) { | |
4505 | case INSN_RETURN: | |
4506 | if (!next_insn || next_insn->type != INSN_TRAP) { | |
4507 | WARN_FUNC("missing int3 after ret", | |
4508 | insn->sec, insn->offset); | |
4509 | warnings++; | |
4510 | } | |
4511 | ||
4512 | break; | |
4513 | case INSN_JUMP_DYNAMIC: | |
4514 | if (!next_insn || next_insn->type != INSN_TRAP) { | |
4515 | WARN_FUNC("missing int3 after indirect jump", | |
4516 | insn->sec, insn->offset); | |
4517 | warnings++; | |
4518 | } | |
4519 | break; | |
4520 | default: | |
4521 | break; | |
4522 | } | |
4523 | } | |
4524 | ||
4525 | return warnings; | |
4526 | } | |
4527 | ||
baa41469 | 4528 | static int validate_reachable_instructions(struct objtool_file *file) |
dcc914f4 JP |
4529 | { |
4530 | struct instruction *insn; | |
baa41469 JP |
4531 | |
4532 | if (file->ignore_unreachables) | |
4533 | return 0; | |
dcc914f4 JP |
4534 | |
4535 | for_each_insn(file, insn) { | |
14db1f0a | 4536 | if (insn->visited || ignore_unreachable_insn(file, insn)) |
baa41469 JP |
4537 | continue; |
4538 | ||
baa41469 JP |
4539 | WARN_FUNC("unreachable instruction", insn->sec, insn->offset); |
4540 | return 1; | |
dcc914f4 JP |
4541 | } |
4542 | ||
baa41469 | 4543 | return 0; |
dcc914f4 JP |
4544 | } |
4545 | ||
d44becb9 | 4546 | int check(struct objtool_file *file) |
dcc914f4 | 4547 | { |
dcc914f4 JP |
4548 | int ret, warnings = 0; |
4549 | ||
baa41469 | 4550 | arch_initial_func_cfi_state(&initial_func_cfi); |
8b946cc3 PZ |
4551 | init_cfi_state(&init_cfi); |
4552 | init_cfi_state(&func_cfi); | |
4553 | set_func_state(&func_cfi); | |
4554 | ||
4555 | if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3))) | |
4556 | goto out; | |
4557 | ||
4558 | cfi_hash_add(&init_cfi); | |
4559 | cfi_hash_add(&func_cfi); | |
baa41469 | 4560 | |
6545eb03 | 4561 | ret = decode_sections(file); |
dcc914f4 JP |
4562 | if (ret < 0) |
4563 | goto out; | |
8b946cc3 | 4564 | |
dcc914f4 JP |
4565 | warnings += ret; |
4566 | ||
1c34496e | 4567 | if (!nr_insns) |
dcc914f4 | 4568 | goto out; |
dcc914f4 | 4569 | |
2daf7fab | 4570 | if (opts.retpoline) { |
6545eb03 | 4571 | ret = validate_retpoline(file); |
b5bc2231 PZ |
4572 | if (ret < 0) |
4573 | return ret; | |
4574 | warnings += ret; | |
4575 | } | |
4576 | ||
c2bdd61c | 4577 | if (opts.stackval || opts.orc || opts.uaccess) { |
7dce6204 JP |
4578 | ret = validate_functions(file); |
4579 | if (ret < 0) | |
4580 | goto out; | |
4581 | warnings += ret; | |
39358a03 | 4582 | |
7dce6204 | 4583 | ret = validate_unwind_hints(file, NULL); |
08f87a93 PZ |
4584 | if (ret < 0) |
4585 | goto out; | |
4586 | warnings += ret; | |
7dce6204 JP |
4587 | |
4588 | if (!warnings) { | |
4589 | ret = validate_reachable_instructions(file); | |
4590 | if (ret < 0) | |
4591 | goto out; | |
4592 | warnings += ret; | |
4593 | } | |
753da417 JP |
4594 | |
4595 | } else if (opts.noinstr) { | |
4596 | ret = validate_noinstr_sections(file); | |
4597 | if (ret < 0) | |
4598 | goto out; | |
4599 | warnings += ret; | |
08f87a93 PZ |
4600 | } |
4601 | ||
a09a6e23 PZ |
4602 | if (opts.unret) { |
4603 | /* | |
4604 | * Must be after validate_branch() and friends, it plays | |
4605 | * further games with insn->visited. | |
4606 | */ | |
4607 | ret = validate_unret(file); | |
4608 | if (ret < 0) | |
4609 | return ret; | |
4610 | warnings += ret; | |
4611 | } | |
4612 | ||
7dce6204 JP |
4613 | if (opts.ibt) { |
4614 | ret = validate_ibt(file); | |
baa41469 JP |
4615 | if (ret < 0) |
4616 | goto out; | |
c2bdd61c JP |
4617 | warnings += ret; |
4618 | } | |
4619 | ||
4620 | if (opts.sls) { | |
4621 | ret = validate_sls(file); | |
4622 | if (ret < 0) | |
4623 | goto out; | |
baa41469 JP |
4624 | warnings += ret; |
4625 | } | |
4626 | ||
26e17689 JP |
4627 | if (opts.static_call) { |
4628 | ret = create_static_call_sections(file); | |
4629 | if (ret < 0) | |
4630 | goto out; | |
4631 | warnings += ret; | |
4632 | } | |
1e7e4788 | 4633 | |
2daf7fab | 4634 | if (opts.retpoline) { |
134ab5bd PZ |
4635 | ret = create_retpoline_sites_sections(file); |
4636 | if (ret < 0) | |
4637 | goto out; | |
4638 | warnings += ret; | |
4639 | } | |
4640 | ||
9a479f76 PZ |
4641 | if (opts.cfi) { |
4642 | ret = create_cfi_sections(file); | |
4643 | if (ret < 0) | |
4644 | goto out; | |
4645 | warnings += ret; | |
4646 | } | |
4647 | ||
f43b9876 | 4648 | if (opts.rethunk) { |
d9e9d230 PZ |
4649 | ret = create_return_sites_sections(file); |
4650 | if (ret < 0) | |
4651 | goto out; | |
4652 | warnings += ret; | |
00abd384 | 4653 | |
0c0a6d89 PZ |
4654 | if (opts.hack_skylake) { |
4655 | ret = create_direct_call_sections(file); | |
4656 | if (ret < 0) | |
4657 | goto out; | |
4658 | warnings += ret; | |
4659 | } | |
134ab5bd PZ |
4660 | } |
4661 | ||
2daf7fab | 4662 | if (opts.mcount) { |
99d00215 PZ |
4663 | ret = create_mcount_loc_sections(file); |
4664 | if (ret < 0) | |
4665 | goto out; | |
4666 | warnings += ret; | |
4667 | } | |
4668 | ||
2daf7fab | 4669 | if (opts.ibt) { |
89bc853e PZ |
4670 | ret = create_ibt_endbr_seal_sections(file); |
4671 | if (ret < 0) | |
4672 | goto out; | |
4673 | warnings += ret; | |
4674 | } | |
4675 | ||
1c34496e | 4676 | if (opts.orc && nr_insns) { |
b51277eb JP |
4677 | ret = orc_create(file); |
4678 | if (ret < 0) | |
4679 | goto out; | |
4680 | warnings += ret; | |
4681 | } | |
4682 | ||
4683 | ||
2daf7fab | 4684 | if (opts.stats) { |
8b946cc3 PZ |
4685 | printf("nr_insns_visited: %ld\n", nr_insns_visited); |
4686 | printf("nr_cfi: %ld\n", nr_cfi); | |
4687 | printf("nr_cfi_reused: %ld\n", nr_cfi_reused); | |
4688 | printf("nr_cfi_cache: %ld\n", nr_cfi_cache); | |
4689 | } | |
4690 | ||
dcc914f4 | 4691 | out: |
655cf865 JP |
4692 | /* |
4693 | * For now, don't fail the kernel build on fatal warnings. These | |
4694 | * errors are still fairly common due to the growing matrix of | |
4695 | * supported toolchains and their recent pace of change. | |
4696 | */ | |
dcc914f4 JP |
4697 | return 0; |
4698 | } |