]>
Commit | Line | Data |
---|---|---|
1ccea77e | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
dcc914f4 JP |
2 | /* |
3 | * Copyright (C) 2015-2017 Josh Poimboeuf <[email protected]> | |
dcc914f4 JP |
4 | */ |
5 | ||
6 | #include <string.h> | |
7 | #include <stdlib.h> | |
8 | ||
43a4525f | 9 | #include "builtin.h" |
0decf1f8 MH |
10 | #include "cfi.h" |
11 | #include "arch.h" | |
dcc914f4 | 12 | #include "check.h" |
dcc914f4 | 13 | #include "special.h" |
dcc914f4 | 14 | #include "warn.h" |
0f1441b4 | 15 | #include "arch_elf.h" |
dcc914f4 | 16 | |
ee819aed | 17 | #include <linux/objtool.h> |
dcc914f4 JP |
18 | #include <linux/hashtable.h> |
19 | #include <linux/kernel.h> | |
1e7e4788 | 20 | #include <linux/static_call_types.h> |
dcc914f4 | 21 | |
e6da9567 JP |
22 | #define FAKE_JUMP_OFFSET -1 |
23 | ||
dcc914f4 JP |
24 | struct alternative { |
25 | struct list_head list; | |
26 | struct instruction *insn; | |
764eef4b | 27 | bool skip_orig; |
dcc914f4 JP |
28 | }; |
29 | ||
a3608f59 | 30 | struct cfi_init_state initial_func_cfi; |
dcc914f4 | 31 | |
627fce14 JP |
32 | struct instruction *find_insn(struct objtool_file *file, |
33 | struct section *sec, unsigned long offset) | |
dcc914f4 JP |
34 | { |
35 | struct instruction *insn; | |
36 | ||
87ecb582 | 37 | hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) { |
dcc914f4 JP |
38 | if (insn->sec == sec && insn->offset == offset) |
39 | return insn; | |
87ecb582 | 40 | } |
dcc914f4 JP |
41 | |
42 | return NULL; | |
43 | } | |
44 | ||
45 | static struct instruction *next_insn_same_sec(struct objtool_file *file, | |
46 | struct instruction *insn) | |
47 | { | |
48 | struct instruction *next = list_next_entry(insn, list); | |
49 | ||
baa41469 | 50 | if (!next || &next->list == &file->insn_list || next->sec != insn->sec) |
dcc914f4 JP |
51 | return NULL; |
52 | ||
53 | return next; | |
54 | } | |
55 | ||
13810435 JP |
56 | static struct instruction *next_insn_same_func(struct objtool_file *file, |
57 | struct instruction *insn) | |
58 | { | |
59 | struct instruction *next = list_next_entry(insn, list); | |
60 | struct symbol *func = insn->func; | |
61 | ||
62 | if (!func) | |
63 | return NULL; | |
64 | ||
65 | if (&next->list != &file->insn_list && next->func == func) | |
66 | return next; | |
67 | ||
68 | /* Check if we're already in the subfunction: */ | |
69 | if (func == func->cfunc) | |
70 | return NULL; | |
71 | ||
72 | /* Move to the subfunction: */ | |
73 | return find_insn(file, func->cfunc->sec, func->cfunc->offset); | |
74 | } | |
75 | ||
1119d265 JP |
76 | static struct instruction *prev_insn_same_sym(struct objtool_file *file, |
77 | struct instruction *insn) | |
78 | { | |
79 | struct instruction *prev = list_prev_entry(insn, list); | |
80 | ||
81 | if (&prev->list != &file->insn_list && prev->func == insn->func) | |
82 | return prev; | |
83 | ||
84 | return NULL; | |
85 | } | |
86 | ||
f0f70adb | 87 | #define func_for_each_insn(file, func, insn) \ |
13810435 JP |
88 | for (insn = find_insn(file, func->sec, func->offset); \ |
89 | insn; \ | |
90 | insn = next_insn_same_func(file, insn)) | |
91 | ||
dbf4aeb0 PZ |
92 | #define sym_for_each_insn(file, sym, insn) \ |
93 | for (insn = find_insn(file, sym->sec, sym->offset); \ | |
dcc914f4 | 94 | insn && &insn->list != &file->insn_list && \ |
dbf4aeb0 PZ |
95 | insn->sec == sym->sec && \ |
96 | insn->offset < sym->offset + sym->len; \ | |
dcc914f4 JP |
97 | insn = list_next_entry(insn, list)) |
98 | ||
dbf4aeb0 | 99 | #define sym_for_each_insn_continue_reverse(file, sym, insn) \ |
dcc914f4 JP |
100 | for (insn = list_prev_entry(insn, list); \ |
101 | &insn->list != &file->insn_list && \ | |
dbf4aeb0 | 102 | insn->sec == sym->sec && insn->offset >= sym->offset; \ |
dcc914f4 JP |
103 | insn = list_prev_entry(insn, list)) |
104 | ||
105 | #define sec_for_each_insn_from(file, insn) \ | |
106 | for (; insn; insn = next_insn_same_sec(file, insn)) | |
107 | ||
baa41469 JP |
108 | #define sec_for_each_insn_continue(file, insn) \ |
109 | for (insn = next_insn_same_sec(file, insn); insn; \ | |
110 | insn = next_insn_same_sec(file, insn)) | |
dcc914f4 | 111 | |
0c1ddd33 JP |
112 | static bool is_sibling_call(struct instruction *insn) |
113 | { | |
114 | /* An indirect jump is either a sibling call or a jump to a table. */ | |
115 | if (insn->type == INSN_JUMP_DYNAMIC) | |
116 | return list_empty(&insn->alts); | |
117 | ||
a2296140 | 118 | if (!is_static_jump(insn)) |
0c1ddd33 JP |
119 | return false; |
120 | ||
121 | /* add_jump_destinations() sets insn->call_dest for sibling calls. */ | |
122 | return !!insn->call_dest; | |
123 | } | |
124 | ||
dcc914f4 JP |
125 | /* |
126 | * This checks to see if the given function is a "noreturn" function. | |
127 | * | |
128 | * For global functions which are outside the scope of this object file, we | |
129 | * have to keep a manual list of them. | |
130 | * | |
131 | * For local functions, we have to detect them manually by simply looking for | |
132 | * the lack of a return instruction. | |
dcc914f4 | 133 | */ |
8e25c9f8 JP |
134 | static bool __dead_end_function(struct objtool_file *file, struct symbol *func, |
135 | int recursion) | |
dcc914f4 JP |
136 | { |
137 | int i; | |
138 | struct instruction *insn; | |
139 | bool empty = true; | |
140 | ||
141 | /* | |
142 | * Unfortunately these have to be hard coded because the noreturn | |
143 | * attribute isn't provided in ELF data. | |
144 | */ | |
145 | static const char * const global_noreturns[] = { | |
146 | "__stack_chk_fail", | |
147 | "panic", | |
148 | "do_exit", | |
149 | "do_task_dead", | |
150 | "__module_put_and_exit", | |
151 | "complete_and_exit", | |
dcc914f4 JP |
152 | "__reiserfs_panic", |
153 | "lbug_with_loc", | |
154 | "fortify_panic", | |
b394d468 | 155 | "usercopy_abort", |
684fb246 | 156 | "machine_real_restart", |
4fa5ecda | 157 | "rewind_stack_do_exit", |
33adf80f | 158 | "kunit_try_catch_throw", |
dcc914f4 JP |
159 | }; |
160 | ||
c9bab22b JP |
161 | if (!func) |
162 | return false; | |
163 | ||
dcc914f4 | 164 | if (func->bind == STB_WEAK) |
8e25c9f8 | 165 | return false; |
dcc914f4 JP |
166 | |
167 | if (func->bind == STB_GLOBAL) | |
168 | for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) | |
169 | if (!strcmp(func->name, global_noreturns[i])) | |
8e25c9f8 | 170 | return true; |
dcc914f4 | 171 | |
13810435 | 172 | if (!func->len) |
8e25c9f8 | 173 | return false; |
dcc914f4 | 174 | |
13810435 JP |
175 | insn = find_insn(file, func->sec, func->offset); |
176 | if (!insn->func) | |
8e25c9f8 | 177 | return false; |
13810435 | 178 | |
f0f70adb | 179 | func_for_each_insn(file, func, insn) { |
dcc914f4 JP |
180 | empty = false; |
181 | ||
182 | if (insn->type == INSN_RETURN) | |
8e25c9f8 | 183 | return false; |
dcc914f4 JP |
184 | } |
185 | ||
186 | if (empty) | |
8e25c9f8 | 187 | return false; |
dcc914f4 JP |
188 | |
189 | /* | |
190 | * A function can have a sibling call instead of a return. In that | |
191 | * case, the function's dead-end status depends on whether the target | |
192 | * of the sibling call returns. | |
193 | */ | |
f0f70adb | 194 | func_for_each_insn(file, func, insn) { |
0c1ddd33 | 195 | if (is_sibling_call(insn)) { |
dcc914f4 | 196 | struct instruction *dest = insn->jump_dest; |
dcc914f4 JP |
197 | |
198 | if (!dest) | |
199 | /* sibling call to another file */ | |
8e25c9f8 | 200 | return false; |
dcc914f4 | 201 | |
0c1ddd33 JP |
202 | /* local sibling call */ |
203 | if (recursion == 5) { | |
204 | /* | |
205 | * Infinite recursion: two functions have | |
206 | * sibling calls to each other. This is a very | |
207 | * rare case. It means they aren't dead ends. | |
208 | */ | |
209 | return false; | |
dcc914f4 | 210 | } |
dcc914f4 | 211 | |
0c1ddd33 JP |
212 | return __dead_end_function(file, dest->func, recursion+1); |
213 | } | |
dcc914f4 JP |
214 | } |
215 | ||
8e25c9f8 | 216 | return true; |
dcc914f4 JP |
217 | } |
218 | ||
8e25c9f8 | 219 | static bool dead_end_function(struct objtool_file *file, struct symbol *func) |
dcc914f4 JP |
220 | { |
221 | return __dead_end_function(file, func, 0); | |
222 | } | |
223 | ||
e7c0219b | 224 | static void init_cfi_state(struct cfi_state *cfi) |
baa41469 JP |
225 | { |
226 | int i; | |
227 | ||
dd88a0a0 | 228 | for (i = 0; i < CFI_NUM_REGS; i++) { |
e7c0219b PZ |
229 | cfi->regs[i].base = CFI_UNDEFINED; |
230 | cfi->vals[i].base = CFI_UNDEFINED; | |
dd88a0a0 | 231 | } |
e7c0219b PZ |
232 | cfi->cfa.base = CFI_UNDEFINED; |
233 | cfi->drap_reg = CFI_UNDEFINED; | |
234 | cfi->drap_offset = -1; | |
235 | } | |
236 | ||
932f8e98 | 237 | static void init_insn_state(struct insn_state *state, struct section *sec) |
e7c0219b PZ |
238 | { |
239 | memset(state, 0, sizeof(*state)); | |
240 | init_cfi_state(&state->cfi); | |
932f8e98 PZ |
241 | |
242 | /* | |
243 | * We need the full vmlinux for noinstr validation, otherwise we can | |
244 | * not correctly determine insn->call_dest->sec (external symbols do | |
245 | * not have a section). | |
246 | */ | |
247 | if (vmlinux && sec) | |
248 | state->noinstr = sec->noinstr; | |
baa41469 JP |
249 | } |
250 | ||
dcc914f4 JP |
251 | /* |
252 | * Call the arch-specific instruction decoder for all the instructions and add | |
253 | * them to the global instruction list. | |
254 | */ | |
255 | static int decode_instructions(struct objtool_file *file) | |
256 | { | |
257 | struct section *sec; | |
258 | struct symbol *func; | |
259 | unsigned long offset; | |
260 | struct instruction *insn; | |
1e11f3fd | 261 | unsigned long nr_insns = 0; |
dcc914f4 JP |
262 | int ret; |
263 | ||
baa41469 | 264 | for_each_sec(file, sec) { |
dcc914f4 JP |
265 | |
266 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) | |
267 | continue; | |
268 | ||
627fce14 JP |
269 | if (strcmp(sec->name, ".altinstr_replacement") && |
270 | strcmp(sec->name, ".altinstr_aux") && | |
271 | strncmp(sec->name, ".discard.", 9)) | |
272 | sec->text = true; | |
273 | ||
0cc9ac8d TG |
274 | if (!strcmp(sec->name, ".noinstr.text") || |
275 | !strcmp(sec->name, ".entry.text")) | |
c4a33939 PZ |
276 | sec->noinstr = true; |
277 | ||
dcc914f4 JP |
278 | for (offset = 0; offset < sec->len; offset += insn->len) { |
279 | insn = malloc(sizeof(*insn)); | |
baa41469 JP |
280 | if (!insn) { |
281 | WARN("malloc failed"); | |
282 | return -1; | |
283 | } | |
dcc914f4 | 284 | memset(insn, 0, sizeof(*insn)); |
dcc914f4 | 285 | INIT_LIST_HEAD(&insn->alts); |
65ea47dc | 286 | INIT_LIST_HEAD(&insn->stack_ops); |
e7c0219b | 287 | init_cfi_state(&insn->cfi); |
baa41469 | 288 | |
dcc914f4 JP |
289 | insn->sec = sec; |
290 | insn->offset = offset; | |
291 | ||
292 | ret = arch_decode_instruction(file->elf, sec, offset, | |
293 | sec->len - offset, | |
294 | &insn->len, &insn->type, | |
baa41469 | 295 | &insn->immediate, |
65ea47dc | 296 | &insn->stack_ops); |
dcc914f4 | 297 | if (ret) |
b7037983 | 298 | goto err; |
dcc914f4 | 299 | |
87ecb582 | 300 | hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset)); |
dcc914f4 | 301 | list_add_tail(&insn->list, &file->insn_list); |
1e11f3fd | 302 | nr_insns++; |
dcc914f4 JP |
303 | } |
304 | ||
305 | list_for_each_entry(func, &sec->symbol_list, list) { | |
e10cd8fe | 306 | if (func->type != STT_FUNC || func->alias != func) |
dcc914f4 JP |
307 | continue; |
308 | ||
309 | if (!find_insn(file, sec, func->offset)) { | |
310 | WARN("%s(): can't find starting instruction", | |
311 | func->name); | |
312 | return -1; | |
313 | } | |
314 | ||
dbf4aeb0 | 315 | sym_for_each_insn(file, func, insn) |
e10cd8fe | 316 | insn->func = func; |
dcc914f4 JP |
317 | } |
318 | } | |
319 | ||
1e11f3fd PZ |
320 | if (stats) |
321 | printf("nr_insns: %lu\n", nr_insns); | |
322 | ||
dcc914f4 | 323 | return 0; |
b7037983 KB |
324 | |
325 | err: | |
326 | free(insn); | |
327 | return ret; | |
dcc914f4 JP |
328 | } |
329 | ||
6b5dd716 ST |
330 | static struct instruction *find_last_insn(struct objtool_file *file, |
331 | struct section *sec) | |
332 | { | |
333 | struct instruction *insn = NULL; | |
334 | unsigned int offset; | |
335 | unsigned int end = (sec->len > 10) ? sec->len - 10 : 0; | |
336 | ||
337 | for (offset = sec->len - 1; offset >= end && !insn; offset--) | |
338 | insn = find_insn(file, sec, offset); | |
339 | ||
340 | return insn; | |
341 | } | |
342 | ||
dcc914f4 | 343 | /* |
649ea4d5 | 344 | * Mark "ud2" instructions and manually annotated dead ends. |
dcc914f4 JP |
345 | */ |
346 | static int add_dead_ends(struct objtool_file *file) | |
347 | { | |
348 | struct section *sec; | |
f1974222 | 349 | struct reloc *reloc; |
dcc914f4 | 350 | struct instruction *insn; |
dcc914f4 | 351 | |
649ea4d5 JP |
352 | /* |
353 | * By default, "ud2" is a dead end unless otherwise annotated, because | |
354 | * GCC 7 inserts it for certain divide-by-zero cases. | |
355 | */ | |
356 | for_each_insn(file, insn) | |
357 | if (insn->type == INSN_BUG) | |
358 | insn->dead_end = true; | |
359 | ||
360 | /* | |
361 | * Check for manually annotated dead ends. | |
362 | */ | |
dcc914f4 JP |
363 | sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); |
364 | if (!sec) | |
649ea4d5 | 365 | goto reachable; |
dcc914f4 | 366 | |
f1974222 MH |
367 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
368 | if (reloc->sym->type != STT_SECTION) { | |
dcc914f4 JP |
369 | WARN("unexpected relocation symbol type in %s", sec->name); |
370 | return -1; | |
371 | } | |
f1974222 | 372 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
dcc914f4 JP |
373 | if (insn) |
374 | insn = list_prev_entry(insn, list); | |
f1974222 MH |
375 | else if (reloc->addend == reloc->sym->sec->len) { |
376 | insn = find_last_insn(file, reloc->sym->sec); | |
6b5dd716 | 377 | if (!insn) { |
dcc914f4 | 378 | WARN("can't find unreachable insn at %s+0x%x", |
f1974222 | 379 | reloc->sym->sec->name, reloc->addend); |
dcc914f4 JP |
380 | return -1; |
381 | } | |
382 | } else { | |
383 | WARN("can't find unreachable insn at %s+0x%x", | |
f1974222 | 384 | reloc->sym->sec->name, reloc->addend); |
dcc914f4 JP |
385 | return -1; |
386 | } | |
387 | ||
388 | insn->dead_end = true; | |
389 | } | |
390 | ||
649ea4d5 JP |
391 | reachable: |
392 | /* | |
393 | * These manually annotated reachable checks are needed for GCC 4.4, | |
394 | * where the Linux unreachable() macro isn't supported. In that case | |
395 | * GCC doesn't know the "ud2" is fatal, so it generates code as if it's | |
396 | * not a dead end. | |
397 | */ | |
398 | sec = find_section_by_name(file->elf, ".rela.discard.reachable"); | |
399 | if (!sec) | |
400 | return 0; | |
401 | ||
f1974222 MH |
402 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
403 | if (reloc->sym->type != STT_SECTION) { | |
649ea4d5 JP |
404 | WARN("unexpected relocation symbol type in %s", sec->name); |
405 | return -1; | |
406 | } | |
f1974222 | 407 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
649ea4d5 JP |
408 | if (insn) |
409 | insn = list_prev_entry(insn, list); | |
f1974222 MH |
410 | else if (reloc->addend == reloc->sym->sec->len) { |
411 | insn = find_last_insn(file, reloc->sym->sec); | |
6b5dd716 | 412 | if (!insn) { |
649ea4d5 | 413 | WARN("can't find reachable insn at %s+0x%x", |
f1974222 | 414 | reloc->sym->sec->name, reloc->addend); |
649ea4d5 JP |
415 | return -1; |
416 | } | |
417 | } else { | |
418 | WARN("can't find reachable insn at %s+0x%x", | |
f1974222 | 419 | reloc->sym->sec->name, reloc->addend); |
649ea4d5 JP |
420 | return -1; |
421 | } | |
422 | ||
423 | insn->dead_end = false; | |
424 | } | |
425 | ||
dcc914f4 JP |
426 | return 0; |
427 | } | |
428 | ||
1e7e4788 JP |
429 | static int create_static_call_sections(struct objtool_file *file) |
430 | { | |
431 | struct section *sec, *reloc_sec; | |
432 | struct reloc *reloc; | |
433 | struct static_call_site *site; | |
434 | struct instruction *insn; | |
435 | struct symbol *key_sym; | |
436 | char *key_name, *tmp; | |
437 | int idx; | |
438 | ||
439 | sec = find_section_by_name(file->elf, ".static_call_sites"); | |
440 | if (sec) { | |
441 | INIT_LIST_HEAD(&file->static_call_list); | |
442 | WARN("file already has .static_call_sites section, skipping"); | |
443 | return 0; | |
444 | } | |
445 | ||
446 | if (list_empty(&file->static_call_list)) | |
447 | return 0; | |
448 | ||
449 | idx = 0; | |
450 | list_for_each_entry(insn, &file->static_call_list, static_call_node) | |
451 | idx++; | |
452 | ||
453 | sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE, | |
454 | sizeof(struct static_call_site), idx); | |
455 | if (!sec) | |
456 | return -1; | |
457 | ||
458 | reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA); | |
459 | if (!reloc_sec) | |
460 | return -1; | |
461 | ||
462 | idx = 0; | |
463 | list_for_each_entry(insn, &file->static_call_list, static_call_node) { | |
464 | ||
465 | site = (struct static_call_site *)sec->data->d_buf + idx; | |
466 | memset(site, 0, sizeof(struct static_call_site)); | |
467 | ||
468 | /* populate reloc for 'addr' */ | |
469 | reloc = malloc(sizeof(*reloc)); | |
470 | if (!reloc) { | |
471 | perror("malloc"); | |
472 | return -1; | |
473 | } | |
474 | memset(reloc, 0, sizeof(*reloc)); | |
475 | reloc->sym = insn->sec->sym; | |
476 | reloc->addend = insn->offset; | |
477 | reloc->type = R_X86_64_PC32; | |
478 | reloc->offset = idx * sizeof(struct static_call_site); | |
479 | reloc->sec = reloc_sec; | |
480 | elf_add_reloc(file->elf, reloc); | |
481 | ||
482 | /* find key symbol */ | |
483 | key_name = strdup(insn->call_dest->name); | |
484 | if (!key_name) { | |
485 | perror("strdup"); | |
486 | return -1; | |
487 | } | |
488 | if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR, | |
489 | STATIC_CALL_TRAMP_PREFIX_LEN)) { | |
490 | WARN("static_call: trampoline name malformed: %s", key_name); | |
491 | return -1; | |
492 | } | |
493 | tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN; | |
494 | memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN); | |
495 | ||
496 | key_sym = find_symbol_by_name(file->elf, tmp); | |
497 | if (!key_sym) { | |
498 | WARN("static_call: can't find static_call_key symbol: %s", tmp); | |
499 | return -1; | |
500 | } | |
501 | free(key_name); | |
502 | ||
503 | /* populate reloc for 'key' */ | |
504 | reloc = malloc(sizeof(*reloc)); | |
505 | if (!reloc) { | |
506 | perror("malloc"); | |
507 | return -1; | |
508 | } | |
509 | memset(reloc, 0, sizeof(*reloc)); | |
510 | reloc->sym = key_sym; | |
5b06fd3b | 511 | reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0; |
1e7e4788 JP |
512 | reloc->type = R_X86_64_PC32; |
513 | reloc->offset = idx * sizeof(struct static_call_site) + 4; | |
514 | reloc->sec = reloc_sec; | |
515 | elf_add_reloc(file->elf, reloc); | |
516 | ||
517 | idx++; | |
518 | } | |
519 | ||
520 | if (elf_rebuild_reloc_section(file->elf, reloc_sec)) | |
521 | return -1; | |
522 | ||
523 | return 0; | |
524 | } | |
525 | ||
dcc914f4 JP |
526 | /* |
527 | * Warnings shouldn't be reported for ignored functions. | |
528 | */ | |
529 | static void add_ignores(struct objtool_file *file) | |
530 | { | |
531 | struct instruction *insn; | |
532 | struct section *sec; | |
533 | struct symbol *func; | |
f1974222 | 534 | struct reloc *reloc; |
dcc914f4 | 535 | |
aaf5c623 PZ |
536 | sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); |
537 | if (!sec) | |
538 | return; | |
dcc914f4 | 539 | |
f1974222 MH |
540 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
541 | switch (reloc->sym->type) { | |
aaf5c623 | 542 | case STT_FUNC: |
f1974222 | 543 | func = reloc->sym; |
aaf5c623 PZ |
544 | break; |
545 | ||
546 | case STT_SECTION: | |
f1974222 | 547 | func = find_func_by_offset(reloc->sym->sec, reloc->addend); |
7acfe531 | 548 | if (!func) |
dcc914f4 | 549 | continue; |
aaf5c623 | 550 | break; |
dcc914f4 | 551 | |
aaf5c623 | 552 | default: |
f1974222 | 553 | WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type); |
aaf5c623 | 554 | continue; |
dcc914f4 | 555 | } |
aaf5c623 | 556 | |
f0f70adb | 557 | func_for_each_insn(file, func, insn) |
aaf5c623 | 558 | insn->ignore = true; |
dcc914f4 JP |
559 | } |
560 | } | |
561 | ||
ea24213d PZ |
562 | /* |
563 | * This is a whitelist of functions that is allowed to be called with AC set. | |
564 | * The list is meant to be minimal and only contains compiler instrumentation | |
565 | * ABI and a few functions used to implement *_{to,from}_user() functions. | |
566 | * | |
567 | * These functions must not directly change AC, but may PUSHF/POPF. | |
568 | */ | |
569 | static const char *uaccess_safe_builtin[] = { | |
570 | /* KASAN */ | |
571 | "kasan_report", | |
572 | "check_memory_region", | |
573 | /* KASAN out-of-line */ | |
574 | "__asan_loadN_noabort", | |
575 | "__asan_load1_noabort", | |
576 | "__asan_load2_noabort", | |
577 | "__asan_load4_noabort", | |
578 | "__asan_load8_noabort", | |
579 | "__asan_load16_noabort", | |
580 | "__asan_storeN_noabort", | |
581 | "__asan_store1_noabort", | |
582 | "__asan_store2_noabort", | |
583 | "__asan_store4_noabort", | |
584 | "__asan_store8_noabort", | |
585 | "__asan_store16_noabort", | |
586 | /* KASAN in-line */ | |
587 | "__asan_report_load_n_noabort", | |
588 | "__asan_report_load1_noabort", | |
589 | "__asan_report_load2_noabort", | |
590 | "__asan_report_load4_noabort", | |
591 | "__asan_report_load8_noabort", | |
592 | "__asan_report_load16_noabort", | |
593 | "__asan_report_store_n_noabort", | |
594 | "__asan_report_store1_noabort", | |
595 | "__asan_report_store2_noabort", | |
596 | "__asan_report_store4_noabort", | |
597 | "__asan_report_store8_noabort", | |
598 | "__asan_report_store16_noabort", | |
5f5c9712 | 599 | /* KCSAN */ |
9967683c | 600 | "__kcsan_check_access", |
5f5c9712 ME |
601 | "kcsan_found_watchpoint", |
602 | "kcsan_setup_watchpoint", | |
9967683c | 603 | "kcsan_check_scoped_accesses", |
50a19ad4 ME |
604 | "kcsan_disable_current", |
605 | "kcsan_enable_current_nowarn", | |
5f5c9712 ME |
606 | /* KCSAN/TSAN */ |
607 | "__tsan_func_entry", | |
608 | "__tsan_func_exit", | |
609 | "__tsan_read_range", | |
610 | "__tsan_write_range", | |
611 | "__tsan_read1", | |
612 | "__tsan_read2", | |
613 | "__tsan_read4", | |
614 | "__tsan_read8", | |
615 | "__tsan_read16", | |
616 | "__tsan_write1", | |
617 | "__tsan_write2", | |
618 | "__tsan_write4", | |
619 | "__tsan_write8", | |
620 | "__tsan_write16", | |
ea24213d PZ |
621 | /* KCOV */ |
622 | "write_comp_data", | |
ae033f08 | 623 | "check_kcov_mode", |
ea24213d PZ |
624 | "__sanitizer_cov_trace_pc", |
625 | "__sanitizer_cov_trace_const_cmp1", | |
626 | "__sanitizer_cov_trace_const_cmp2", | |
627 | "__sanitizer_cov_trace_const_cmp4", | |
628 | "__sanitizer_cov_trace_const_cmp8", | |
629 | "__sanitizer_cov_trace_cmp1", | |
630 | "__sanitizer_cov_trace_cmp2", | |
631 | "__sanitizer_cov_trace_cmp4", | |
632 | "__sanitizer_cov_trace_cmp8", | |
36b1c700 | 633 | "__sanitizer_cov_trace_switch", |
ea24213d PZ |
634 | /* UBSAN */ |
635 | "ubsan_type_mismatch_common", | |
636 | "__ubsan_handle_type_mismatch", | |
637 | "__ubsan_handle_type_mismatch_v1", | |
9a50dcaf | 638 | "__ubsan_handle_shift_out_of_bounds", |
ea24213d PZ |
639 | /* misc */ |
640 | "csum_partial_copy_generic", | |
641 | "__memcpy_mcsafe", | |
a7e47f26 | 642 | "mcsafe_handle_tail", |
ea24213d PZ |
643 | "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */ |
644 | NULL | |
645 | }; | |
646 | ||
647 | static void add_uaccess_safe(struct objtool_file *file) | |
648 | { | |
649 | struct symbol *func; | |
650 | const char **name; | |
651 | ||
652 | if (!uaccess) | |
653 | return; | |
654 | ||
655 | for (name = uaccess_safe_builtin; *name; name++) { | |
656 | func = find_symbol_by_name(file->elf, *name); | |
657 | if (!func) | |
658 | continue; | |
659 | ||
e10cd8fe | 660 | func->uaccess_safe = true; |
dcc914f4 JP |
661 | } |
662 | } | |
663 | ||
258c7605 JP |
664 | /* |
665 | * FIXME: For now, just ignore any alternatives which add retpolines. This is | |
666 | * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline. | |
667 | * But it at least allows objtool to understand the control flow *around* the | |
668 | * retpoline. | |
669 | */ | |
ff05ab23 | 670 | static int add_ignore_alternatives(struct objtool_file *file) |
258c7605 JP |
671 | { |
672 | struct section *sec; | |
f1974222 | 673 | struct reloc *reloc; |
258c7605 JP |
674 | struct instruction *insn; |
675 | ||
ff05ab23 | 676 | sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts"); |
258c7605 JP |
677 | if (!sec) |
678 | return 0; | |
679 | ||
f1974222 MH |
680 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
681 | if (reloc->sym->type != STT_SECTION) { | |
258c7605 JP |
682 | WARN("unexpected relocation symbol type in %s", sec->name); |
683 | return -1; | |
684 | } | |
685 | ||
f1974222 | 686 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
258c7605 | 687 | if (!insn) { |
ff05ab23 | 688 | WARN("bad .discard.ignore_alts entry"); |
258c7605 JP |
689 | return -1; |
690 | } | |
691 | ||
692 | insn->ignore_alts = true; | |
693 | } | |
694 | ||
695 | return 0; | |
696 | } | |
697 | ||
dcc914f4 JP |
698 | /* |
699 | * Find the destination instructions for all jumps. | |
700 | */ | |
701 | static int add_jump_destinations(struct objtool_file *file) | |
702 | { | |
703 | struct instruction *insn; | |
f1974222 | 704 | struct reloc *reloc; |
dcc914f4 JP |
705 | struct section *dest_sec; |
706 | unsigned long dest_off; | |
707 | ||
708 | for_each_insn(file, insn) { | |
a2296140 | 709 | if (!is_static_jump(insn)) |
dcc914f4 JP |
710 | continue; |
711 | ||
e6da9567 | 712 | if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET) |
dcc914f4 JP |
713 | continue; |
714 | ||
f1974222 | 715 | reloc = find_reloc_by_dest_range(file->elf, insn->sec, |
8b5fa6bc | 716 | insn->offset, insn->len); |
f1974222 | 717 | if (!reloc) { |
dcc914f4 | 718 | dest_sec = insn->sec; |
bfb08f22 | 719 | dest_off = arch_jump_destination(insn); |
f1974222 MH |
720 | } else if (reloc->sym->type == STT_SECTION) { |
721 | dest_sec = reloc->sym->sec; | |
722 | dest_off = arch_dest_reloc_offset(reloc->addend); | |
723 | } else if (reloc->sym->sec->idx) { | |
724 | dest_sec = reloc->sym->sec; | |
725 | dest_off = reloc->sym->sym.st_value + | |
726 | arch_dest_reloc_offset(reloc->addend); | |
727 | } else if (strstr(reloc->sym->name, "_indirect_thunk_")) { | |
39b73533 JP |
728 | /* |
729 | * Retpoline jumps are really dynamic jumps in | |
730 | * disguise, so convert them accordingly. | |
731 | */ | |
b68b9907 JP |
732 | if (insn->type == INSN_JUMP_UNCONDITIONAL) |
733 | insn->type = INSN_JUMP_DYNAMIC; | |
734 | else | |
735 | insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL; | |
736 | ||
b5bc2231 | 737 | insn->retpoline_safe = true; |
39b73533 | 738 | continue; |
dcc914f4 | 739 | } else { |
0c1ddd33 | 740 | /* external sibling call */ |
f1974222 | 741 | insn->call_dest = reloc->sym; |
5b06fd3b PZ |
742 | if (insn->call_dest->static_call_tramp) { |
743 | list_add_tail(&insn->static_call_node, | |
744 | &file->static_call_list); | |
745 | } | |
dcc914f4 JP |
746 | continue; |
747 | } | |
748 | ||
749 | insn->jump_dest = find_insn(file, dest_sec, dest_off); | |
750 | if (!insn->jump_dest) { | |
751 | ||
752 | /* | |
753 | * This is a special case where an alt instruction | |
754 | * jumps past the end of the section. These are | |
755 | * handled later in handle_group_alt(). | |
756 | */ | |
757 | if (!strcmp(insn->sec->name, ".altinstr_replacement")) | |
758 | continue; | |
759 | ||
760 | WARN_FUNC("can't find jump dest instruction at %s+0x%lx", | |
761 | insn->sec, insn->offset, dest_sec->name, | |
762 | dest_off); | |
763 | return -1; | |
764 | } | |
cd77849a JP |
765 | |
766 | /* | |
54262aa2 | 767 | * Cross-function jump. |
cd77849a JP |
768 | */ |
769 | if (insn->func && insn->jump_dest->func && | |
54262aa2 PZ |
770 | insn->func != insn->jump_dest->func) { |
771 | ||
772 | /* | |
773 | * For GCC 8+, create parent/child links for any cold | |
774 | * subfunctions. This is _mostly_ redundant with a | |
775 | * similar initialization in read_symbols(). | |
776 | * | |
777 | * If a function has aliases, we want the *first* such | |
778 | * function in the symbol table to be the subfunction's | |
779 | * parent. In that case we overwrite the | |
780 | * initialization done in read_symbols(). | |
781 | * | |
782 | * However this code can't completely replace the | |
783 | * read_symbols() code because this doesn't detect the | |
784 | * case where the parent function's only reference to a | |
e7c2bc37 | 785 | * subfunction is through a jump table. |
54262aa2 PZ |
786 | */ |
787 | if (!strstr(insn->func->name, ".cold.") && | |
788 | strstr(insn->jump_dest->func->name, ".cold.")) { | |
789 | insn->func->cfunc = insn->jump_dest->func; | |
790 | insn->jump_dest->func->pfunc = insn->func; | |
791 | ||
792 | } else if (insn->jump_dest->func->pfunc != insn->func->pfunc && | |
793 | insn->jump_dest->offset == insn->jump_dest->func->offset) { | |
794 | ||
0c1ddd33 | 795 | /* internal sibling call */ |
54262aa2 | 796 | insn->call_dest = insn->jump_dest->func; |
5b06fd3b PZ |
797 | if (insn->call_dest->static_call_tramp) { |
798 | list_add_tail(&insn->static_call_node, | |
799 | &file->static_call_list); | |
800 | } | |
54262aa2 | 801 | } |
cd77849a | 802 | } |
dcc914f4 JP |
803 | } |
804 | ||
805 | return 0; | |
806 | } | |
807 | ||
8aa8eb2a AC |
808 | static void remove_insn_ops(struct instruction *insn) |
809 | { | |
810 | struct stack_op *op, *tmp; | |
811 | ||
812 | list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) { | |
813 | list_del(&op->list); | |
814 | free(op); | |
815 | } | |
816 | } | |
817 | ||
dcc914f4 JP |
818 | /* |
819 | * Find the destination instructions for all calls. | |
820 | */ | |
821 | static int add_call_destinations(struct objtool_file *file) | |
822 | { | |
823 | struct instruction *insn; | |
824 | unsigned long dest_off; | |
f1974222 | 825 | struct reloc *reloc; |
dcc914f4 JP |
826 | |
827 | for_each_insn(file, insn) { | |
828 | if (insn->type != INSN_CALL) | |
829 | continue; | |
830 | ||
f1974222 | 831 | reloc = find_reloc_by_dest_range(file->elf, insn->sec, |
8b5fa6bc | 832 | insn->offset, insn->len); |
f1974222 | 833 | if (!reloc) { |
bfb08f22 | 834 | dest_off = arch_jump_destination(insn); |
7acfe531 JP |
835 | insn->call_dest = find_func_by_offset(insn->sec, dest_off); |
836 | if (!insn->call_dest) | |
837 | insn->call_dest = find_symbol_by_offset(insn->sec, dest_off); | |
a845c7cf | 838 | |
7acfe531 JP |
839 | if (insn->ignore) |
840 | continue; | |
841 | ||
842 | if (!insn->call_dest) { | |
8aa8eb2a | 843 | WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset); |
dcc914f4 JP |
844 | return -1; |
845 | } | |
a845c7cf | 846 | |
7acfe531 JP |
847 | if (insn->func && insn->call_dest->type != STT_FUNC) { |
848 | WARN_FUNC("unsupported call to non-function", | |
849 | insn->sec, insn->offset); | |
850 | return -1; | |
851 | } | |
852 | ||
f1974222 MH |
853 | } else if (reloc->sym->type == STT_SECTION) { |
854 | dest_off = arch_dest_reloc_offset(reloc->addend); | |
855 | insn->call_dest = find_func_by_offset(reloc->sym->sec, | |
bfb08f22 | 856 | dest_off); |
7acfe531 | 857 | if (!insn->call_dest) { |
bfb08f22 | 858 | WARN_FUNC("can't find call dest symbol at %s+0x%lx", |
dcc914f4 | 859 | insn->sec, insn->offset, |
f1974222 | 860 | reloc->sym->sec->name, |
bfb08f22 | 861 | dest_off); |
dcc914f4 JP |
862 | return -1; |
863 | } | |
864 | } else | |
f1974222 | 865 | insn->call_dest = reloc->sym; |
8aa8eb2a | 866 | |
0f1441b4 PZ |
867 | /* |
868 | * Many compilers cannot disable KCOV with a function attribute | |
869 | * so they need a little help, NOP out any KCOV calls from noinstr | |
870 | * text. | |
871 | */ | |
872 | if (insn->sec->noinstr && | |
873 | !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) { | |
d832c005 PZ |
874 | if (reloc) { |
875 | reloc->type = R_NONE; | |
876 | elf_write_reloc(file->elf, reloc); | |
0f1441b4 PZ |
877 | } |
878 | ||
879 | elf_write_insn(file->elf, insn->sec, | |
880 | insn->offset, insn->len, | |
881 | arch_nop_insn(insn->len)); | |
882 | insn->type = INSN_NOP; | |
883 | } | |
884 | ||
8aa8eb2a AC |
885 | /* |
886 | * Whatever stack impact regular CALLs have, should be undone | |
887 | * by the RETURN of the called function. | |
888 | * | |
889 | * Annotated intra-function calls retain the stack_ops but | |
890 | * are converted to JUMP, see read_intra_function_calls(). | |
891 | */ | |
892 | remove_insn_ops(insn); | |
dcc914f4 JP |
893 | } |
894 | ||
895 | return 0; | |
896 | } | |
897 | ||
898 | /* | |
899 | * The .alternatives section requires some extra special care, over and above | |
900 | * what other special sections require: | |
901 | * | |
902 | * 1. Because alternatives are patched in-place, we need to insert a fake jump | |
903 | * instruction at the end so that validate_branch() skips all the original | |
904 | * replaced instructions when validating the new instruction path. | |
905 | * | |
906 | * 2. An added wrinkle is that the new instruction length might be zero. In | |
907 | * that case the old instructions are replaced with noops. We simulate that | |
908 | * by creating a fake jump as the only new instruction. | |
909 | * | |
910 | * 3. In some cases, the alternative section includes an instruction which | |
911 | * conditionally jumps to the _end_ of the entry. We have to modify these | |
912 | * jumps' destinations to point back to .text rather than the end of the | |
913 | * entry in .altinstr_replacement. | |
dcc914f4 JP |
914 | */ |
915 | static int handle_group_alt(struct objtool_file *file, | |
916 | struct special_alt *special_alt, | |
917 | struct instruction *orig_insn, | |
918 | struct instruction **new_insn) | |
919 | { | |
13fab06d | 920 | static unsigned int alt_group_next_index = 1; |
17bc3391 | 921 | struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL; |
13fab06d | 922 | unsigned int alt_group = alt_group_next_index++; |
dcc914f4 JP |
923 | unsigned long dest_off; |
924 | ||
925 | last_orig_insn = NULL; | |
926 | insn = orig_insn; | |
927 | sec_for_each_insn_from(file, insn) { | |
928 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) | |
929 | break; | |
930 | ||
13fab06d | 931 | insn->alt_group = alt_group; |
dcc914f4 JP |
932 | last_orig_insn = insn; |
933 | } | |
934 | ||
17bc3391 JP |
935 | if (next_insn_same_sec(file, last_orig_insn)) { |
936 | fake_jump = malloc(sizeof(*fake_jump)); | |
937 | if (!fake_jump) { | |
938 | WARN("malloc failed"); | |
939 | return -1; | |
940 | } | |
941 | memset(fake_jump, 0, sizeof(*fake_jump)); | |
942 | INIT_LIST_HEAD(&fake_jump->alts); | |
65ea47dc | 943 | INIT_LIST_HEAD(&fake_jump->stack_ops); |
e7c0219b | 944 | init_cfi_state(&fake_jump->cfi); |
17bc3391 JP |
945 | |
946 | fake_jump->sec = special_alt->new_sec; | |
e6da9567 | 947 | fake_jump->offset = FAKE_JUMP_OFFSET; |
17bc3391 JP |
948 | fake_jump->type = INSN_JUMP_UNCONDITIONAL; |
949 | fake_jump->jump_dest = list_next_entry(last_orig_insn, list); | |
e6da9567 | 950 | fake_jump->func = orig_insn->func; |
dcc914f4 | 951 | } |
dcc914f4 JP |
952 | |
953 | if (!special_alt->new_len) { | |
17bc3391 JP |
954 | if (!fake_jump) { |
955 | WARN("%s: empty alternative at end of section", | |
956 | special_alt->orig_sec->name); | |
957 | return -1; | |
958 | } | |
959 | ||
dcc914f4 JP |
960 | *new_insn = fake_jump; |
961 | return 0; | |
962 | } | |
963 | ||
964 | last_new_insn = NULL; | |
13fab06d | 965 | alt_group = alt_group_next_index++; |
dcc914f4 JP |
966 | insn = *new_insn; |
967 | sec_for_each_insn_from(file, insn) { | |
45245f51 JT |
968 | struct reloc *alt_reloc; |
969 | ||
dcc914f4 JP |
970 | if (insn->offset >= special_alt->new_off + special_alt->new_len) |
971 | break; | |
972 | ||
973 | last_new_insn = insn; | |
974 | ||
a845c7cf | 975 | insn->ignore = orig_insn->ignore_alts; |
a4d09dde | 976 | insn->func = orig_insn->func; |
13fab06d | 977 | insn->alt_group = alt_group; |
a845c7cf | 978 | |
dc419723 JP |
979 | /* |
980 | * Since alternative replacement code is copy/pasted by the | |
981 | * kernel after applying relocations, generally such code can't | |
982 | * have relative-address relocation references to outside the | |
983 | * .altinstr_replacement section, unless the arch's | |
984 | * alternatives code can adjust the relative offsets | |
985 | * accordingly. | |
dc419723 | 986 | */ |
45245f51 JT |
987 | alt_reloc = find_reloc_by_dest_range(file->elf, insn->sec, |
988 | insn->offset, insn->len); | |
989 | if (alt_reloc && | |
990 | !arch_support_alt_relocation(special_alt, insn, alt_reloc)) { | |
dc419723 JP |
991 | |
992 | WARN_FUNC("unsupported relocation in alternatives section", | |
993 | insn->sec, insn->offset); | |
994 | return -1; | |
995 | } | |
996 | ||
a2296140 | 997 | if (!is_static_jump(insn)) |
dcc914f4 JP |
998 | continue; |
999 | ||
1000 | if (!insn->immediate) | |
1001 | continue; | |
1002 | ||
bfb08f22 | 1003 | dest_off = arch_jump_destination(insn); |
17bc3391 JP |
1004 | if (dest_off == special_alt->new_off + special_alt->new_len) { |
1005 | if (!fake_jump) { | |
1006 | WARN("%s: alternative jump to end of section", | |
1007 | special_alt->orig_sec->name); | |
1008 | return -1; | |
1009 | } | |
dcc914f4 | 1010 | insn->jump_dest = fake_jump; |
17bc3391 | 1011 | } |
dcc914f4 JP |
1012 | |
1013 | if (!insn->jump_dest) { | |
1014 | WARN_FUNC("can't find alternative jump destination", | |
1015 | insn->sec, insn->offset); | |
1016 | return -1; | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | if (!last_new_insn) { | |
1021 | WARN_FUNC("can't find last new alternative instruction", | |
1022 | special_alt->new_sec, special_alt->new_off); | |
1023 | return -1; | |
1024 | } | |
1025 | ||
17bc3391 JP |
1026 | if (fake_jump) |
1027 | list_add(&fake_jump->list, &last_new_insn->list); | |
dcc914f4 JP |
1028 | |
1029 | return 0; | |
1030 | } | |
1031 | ||
1032 | /* | |
1033 | * A jump table entry can either convert a nop to a jump or a jump to a nop. | |
1034 | * If the original instruction is a jump, make the alt entry an effective nop | |
1035 | * by just skipping the original instruction. | |
1036 | */ | |
1037 | static int handle_jump_alt(struct objtool_file *file, | |
1038 | struct special_alt *special_alt, | |
1039 | struct instruction *orig_insn, | |
1040 | struct instruction **new_insn) | |
1041 | { | |
1042 | if (orig_insn->type == INSN_NOP) | |
1043 | return 0; | |
1044 | ||
1045 | if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { | |
1046 | WARN_FUNC("unsupported instruction at jump label", | |
1047 | orig_insn->sec, orig_insn->offset); | |
1048 | return -1; | |
1049 | } | |
1050 | ||
1051 | *new_insn = list_next_entry(orig_insn, list); | |
1052 | return 0; | |
1053 | } | |
1054 | ||
1055 | /* | |
1056 | * Read all the special sections which have alternate instructions which can be | |
1057 | * patched in or redirected to at runtime. Each instruction having alternate | |
1058 | * instruction(s) has them added to its insn->alts list, which will be | |
1059 | * traversed in validate_branch(). | |
1060 | */ | |
1061 | static int add_special_section_alts(struct objtool_file *file) | |
1062 | { | |
1063 | struct list_head special_alts; | |
1064 | struct instruction *orig_insn, *new_insn; | |
1065 | struct special_alt *special_alt, *tmp; | |
1066 | struct alternative *alt; | |
1067 | int ret; | |
1068 | ||
1069 | ret = special_get_alts(file->elf, &special_alts); | |
1070 | if (ret) | |
1071 | return ret; | |
1072 | ||
1073 | list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { | |
dcc914f4 JP |
1074 | |
1075 | orig_insn = find_insn(file, special_alt->orig_sec, | |
1076 | special_alt->orig_off); | |
1077 | if (!orig_insn) { | |
1078 | WARN_FUNC("special: can't find orig instruction", | |
1079 | special_alt->orig_sec, special_alt->orig_off); | |
1080 | ret = -1; | |
1081 | goto out; | |
1082 | } | |
1083 | ||
1084 | new_insn = NULL; | |
1085 | if (!special_alt->group || special_alt->new_len) { | |
1086 | new_insn = find_insn(file, special_alt->new_sec, | |
1087 | special_alt->new_off); | |
1088 | if (!new_insn) { | |
1089 | WARN_FUNC("special: can't find new instruction", | |
1090 | special_alt->new_sec, | |
1091 | special_alt->new_off); | |
1092 | ret = -1; | |
1093 | goto out; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | if (special_alt->group) { | |
7170cf47 JT |
1098 | if (!special_alt->orig_len) { |
1099 | WARN_FUNC("empty alternative entry", | |
1100 | orig_insn->sec, orig_insn->offset); | |
1101 | continue; | |
1102 | } | |
1103 | ||
dcc914f4 JP |
1104 | ret = handle_group_alt(file, special_alt, orig_insn, |
1105 | &new_insn); | |
1106 | if (ret) | |
1107 | goto out; | |
1108 | } else if (special_alt->jump_or_nop) { | |
1109 | ret = handle_jump_alt(file, special_alt, orig_insn, | |
1110 | &new_insn); | |
1111 | if (ret) | |
1112 | goto out; | |
1113 | } | |
1114 | ||
258c7605 JP |
1115 | alt = malloc(sizeof(*alt)); |
1116 | if (!alt) { | |
1117 | WARN("malloc failed"); | |
1118 | ret = -1; | |
1119 | goto out; | |
1120 | } | |
1121 | ||
dcc914f4 | 1122 | alt->insn = new_insn; |
764eef4b | 1123 | alt->skip_orig = special_alt->skip_orig; |
ea24213d | 1124 | orig_insn->ignore_alts |= special_alt->skip_alt; |
dcc914f4 JP |
1125 | list_add_tail(&alt->list, &orig_insn->alts); |
1126 | ||
1127 | list_del(&special_alt->list); | |
1128 | free(special_alt); | |
1129 | } | |
1130 | ||
1131 | out: | |
1132 | return ret; | |
1133 | } | |
1134 | ||
e7c2bc37 | 1135 | static int add_jump_table(struct objtool_file *file, struct instruction *insn, |
f1974222 | 1136 | struct reloc *table) |
dcc914f4 | 1137 | { |
f1974222 | 1138 | struct reloc *reloc = table; |
e7c2bc37 | 1139 | struct instruction *dest_insn; |
dcc914f4 | 1140 | struct alternative *alt; |
fd35c88b JP |
1141 | struct symbol *pfunc = insn->func->pfunc; |
1142 | unsigned int prev_offset = 0; | |
dcc914f4 | 1143 | |
e7c2bc37 | 1144 | /* |
f1974222 | 1145 | * Each @reloc is a switch table relocation which points to the target |
e7c2bc37 JP |
1146 | * instruction. |
1147 | */ | |
f1974222 | 1148 | list_for_each_entry_from(reloc, &table->sec->reloc_list, list) { |
bd98c813 JH |
1149 | |
1150 | /* Check for the end of the table: */ | |
f1974222 | 1151 | if (reloc != table && reloc->jump_table_start) |
dcc914f4 JP |
1152 | break; |
1153 | ||
e7c2bc37 | 1154 | /* Make sure the table entries are consecutive: */ |
f1974222 | 1155 | if (prev_offset && reloc->offset != prev_offset + 8) |
fd35c88b JP |
1156 | break; |
1157 | ||
1158 | /* Detect function pointers from contiguous objects: */ | |
f1974222 MH |
1159 | if (reloc->sym->sec == pfunc->sec && |
1160 | reloc->addend == pfunc->offset) | |
fd35c88b JP |
1161 | break; |
1162 | ||
f1974222 | 1163 | dest_insn = find_insn(file, reloc->sym->sec, reloc->addend); |
e7c2bc37 | 1164 | if (!dest_insn) |
dcc914f4 JP |
1165 | break; |
1166 | ||
e7c2bc37 | 1167 | /* Make sure the destination is in the same function: */ |
e65050b9 | 1168 | if (!dest_insn->func || dest_insn->func->pfunc != pfunc) |
13810435 | 1169 | break; |
dcc914f4 JP |
1170 | |
1171 | alt = malloc(sizeof(*alt)); | |
1172 | if (!alt) { | |
1173 | WARN("malloc failed"); | |
1174 | return -1; | |
1175 | } | |
1176 | ||
e7c2bc37 | 1177 | alt->insn = dest_insn; |
dcc914f4 | 1178 | list_add_tail(&alt->list, &insn->alts); |
f1974222 | 1179 | prev_offset = reloc->offset; |
fd35c88b JP |
1180 | } |
1181 | ||
1182 | if (!prev_offset) { | |
1183 | WARN_FUNC("can't find switch jump table", | |
1184 | insn->sec, insn->offset); | |
1185 | return -1; | |
dcc914f4 JP |
1186 | } |
1187 | ||
1188 | return 0; | |
1189 | } | |
1190 | ||
1191 | /* | |
d871f7b5 RG |
1192 | * find_jump_table() - Given a dynamic jump, find the switch jump table |
1193 | * associated with it. | |
dcc914f4 | 1194 | */ |
f1974222 | 1195 | static struct reloc *find_jump_table(struct objtool_file *file, |
dcc914f4 JP |
1196 | struct symbol *func, |
1197 | struct instruction *insn) | |
1198 | { | |
d871f7b5 | 1199 | struct reloc *table_reloc; |
113d4bc9 | 1200 | struct instruction *dest_insn, *orig_insn = insn; |
dcc914f4 | 1201 | |
99ce7962 PZ |
1202 | /* |
1203 | * Backward search using the @first_jump_src links, these help avoid | |
1204 | * much of the 'in between' code. Which avoids us getting confused by | |
1205 | * it. | |
1206 | */ | |
7dec80cc | 1207 | for (; |
1119d265 JP |
1208 | insn && insn->func && insn->func->pfunc == func; |
1209 | insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) { | |
99ce7962 | 1210 | |
7dec80cc | 1211 | if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC) |
dcc914f4 JP |
1212 | break; |
1213 | ||
1214 | /* allow small jumps within the range */ | |
1215 | if (insn->type == INSN_JUMP_UNCONDITIONAL && | |
1216 | insn->jump_dest && | |
1217 | (insn->jump_dest->offset <= insn->offset || | |
1218 | insn->jump_dest->offset > orig_insn->offset)) | |
1219 | break; | |
1220 | ||
d871f7b5 | 1221 | table_reloc = arch_find_switch_table(file, insn); |
f1974222 | 1222 | if (!table_reloc) |
e7c2bc37 | 1223 | continue; |
f1974222 | 1224 | dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend); |
113d4bc9 JP |
1225 | if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func) |
1226 | continue; | |
7dec80cc | 1227 | |
f1974222 | 1228 | return table_reloc; |
dcc914f4 JP |
1229 | } |
1230 | ||
1231 | return NULL; | |
1232 | } | |
1233 | ||
bd98c813 JH |
1234 | /* |
1235 | * First pass: Mark the head of each jump table so that in the next pass, | |
1236 | * we know when a given jump table ends and the next one starts. | |
1237 | */ | |
1238 | static void mark_func_jump_tables(struct objtool_file *file, | |
1239 | struct symbol *func) | |
dcc914f4 | 1240 | { |
bd98c813 | 1241 | struct instruction *insn, *last = NULL; |
f1974222 | 1242 | struct reloc *reloc; |
dcc914f4 | 1243 | |
f0f70adb | 1244 | func_for_each_insn(file, func, insn) { |
99ce7962 PZ |
1245 | if (!last) |
1246 | last = insn; | |
1247 | ||
1248 | /* | |
1249 | * Store back-pointers for unconditional forward jumps such | |
e7c2bc37 | 1250 | * that find_jump_table() can back-track using those and |
99ce7962 PZ |
1251 | * avoid some potentially confusing code. |
1252 | */ | |
1253 | if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest && | |
1254 | insn->offset > last->offset && | |
1255 | insn->jump_dest->offset > insn->offset && | |
1256 | !insn->jump_dest->first_jump_src) { | |
1257 | ||
1258 | insn->jump_dest->first_jump_src = insn; | |
1259 | last = insn->jump_dest; | |
1260 | } | |
1261 | ||
dcc914f4 JP |
1262 | if (insn->type != INSN_JUMP_DYNAMIC) |
1263 | continue; | |
1264 | ||
f1974222 MH |
1265 | reloc = find_jump_table(file, func, insn); |
1266 | if (reloc) { | |
1267 | reloc->jump_table_start = true; | |
1268 | insn->jump_table = reloc; | |
dcc914f4 | 1269 | } |
dcc914f4 | 1270 | } |
bd98c813 JH |
1271 | } |
1272 | ||
1273 | static int add_func_jump_tables(struct objtool_file *file, | |
1274 | struct symbol *func) | |
1275 | { | |
1276 | struct instruction *insn; | |
1277 | int ret; | |
1278 | ||
f0f70adb | 1279 | func_for_each_insn(file, func, insn) { |
bd98c813 JH |
1280 | if (!insn->jump_table) |
1281 | continue; | |
dcc914f4 | 1282 | |
bd98c813 | 1283 | ret = add_jump_table(file, insn, insn->jump_table); |
dcc914f4 JP |
1284 | if (ret) |
1285 | return ret; | |
1286 | } | |
1287 | ||
1288 | return 0; | |
1289 | } | |
1290 | ||
1291 | /* | |
1292 | * For some switch statements, gcc generates a jump table in the .rodata | |
1293 | * section which contains a list of addresses within the function to jump to. | |
1294 | * This finds these jump tables and adds them to the insn->alts lists. | |
1295 | */ | |
e7c2bc37 | 1296 | static int add_jump_table_alts(struct objtool_file *file) |
dcc914f4 JP |
1297 | { |
1298 | struct section *sec; | |
1299 | struct symbol *func; | |
1300 | int ret; | |
1301 | ||
4a60aa05 | 1302 | if (!file->rodata) |
dcc914f4 JP |
1303 | return 0; |
1304 | ||
baa41469 | 1305 | for_each_sec(file, sec) { |
dcc914f4 JP |
1306 | list_for_each_entry(func, &sec->symbol_list, list) { |
1307 | if (func->type != STT_FUNC) | |
1308 | continue; | |
1309 | ||
bd98c813 | 1310 | mark_func_jump_tables(file, func); |
e7c2bc37 | 1311 | ret = add_func_jump_tables(file, func); |
dcc914f4 JP |
1312 | if (ret) |
1313 | return ret; | |
1314 | } | |
1315 | } | |
1316 | ||
1317 | return 0; | |
1318 | } | |
1319 | ||
39358a03 JP |
1320 | static int read_unwind_hints(struct objtool_file *file) |
1321 | { | |
f1974222 MH |
1322 | struct section *sec, *relocsec; |
1323 | struct reloc *reloc; | |
39358a03 JP |
1324 | struct unwind_hint *hint; |
1325 | struct instruction *insn; | |
1326 | struct cfi_reg *cfa; | |
1327 | int i; | |
1328 | ||
1329 | sec = find_section_by_name(file->elf, ".discard.unwind_hints"); | |
1330 | if (!sec) | |
1331 | return 0; | |
1332 | ||
f1974222 MH |
1333 | relocsec = sec->reloc; |
1334 | if (!relocsec) { | |
39358a03 JP |
1335 | WARN("missing .rela.discard.unwind_hints section"); |
1336 | return -1; | |
1337 | } | |
1338 | ||
1339 | if (sec->len % sizeof(struct unwind_hint)) { | |
1340 | WARN("struct unwind_hint size mismatch"); | |
1341 | return -1; | |
1342 | } | |
1343 | ||
1344 | file->hints = true; | |
1345 | ||
1346 | for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) { | |
1347 | hint = (struct unwind_hint *)sec->data->d_buf + i; | |
1348 | ||
f1974222 MH |
1349 | reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint)); |
1350 | if (!reloc) { | |
1351 | WARN("can't find reloc for unwind_hints[%d]", i); | |
39358a03 JP |
1352 | return -1; |
1353 | } | |
1354 | ||
f1974222 | 1355 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
39358a03 JP |
1356 | if (!insn) { |
1357 | WARN("can't find insn for unwind_hints[%d]", i); | |
1358 | return -1; | |
1359 | } | |
1360 | ||
e7c0219b | 1361 | cfa = &insn->cfi.cfa; |
39358a03 | 1362 | |
c536ed2f | 1363 | if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) { |
e25eea89 | 1364 | insn->ret_offset = hint->sp_offset; |
39358a03 JP |
1365 | continue; |
1366 | } | |
1367 | ||
1368 | insn->hint = true; | |
1369 | ||
1370 | switch (hint->sp_reg) { | |
1371 | case ORC_REG_UNDEFINED: | |
1372 | cfa->base = CFI_UNDEFINED; | |
1373 | break; | |
1374 | case ORC_REG_SP: | |
1375 | cfa->base = CFI_SP; | |
1376 | break; | |
1377 | case ORC_REG_BP: | |
1378 | cfa->base = CFI_BP; | |
1379 | break; | |
1380 | case ORC_REG_SP_INDIRECT: | |
1381 | cfa->base = CFI_SP_INDIRECT; | |
1382 | break; | |
1383 | case ORC_REG_R10: | |
1384 | cfa->base = CFI_R10; | |
1385 | break; | |
1386 | case ORC_REG_R13: | |
1387 | cfa->base = CFI_R13; | |
1388 | break; | |
1389 | case ORC_REG_DI: | |
1390 | cfa->base = CFI_DI; | |
1391 | break; | |
1392 | case ORC_REG_DX: | |
1393 | cfa->base = CFI_DX; | |
1394 | break; | |
1395 | default: | |
1396 | WARN_FUNC("unsupported unwind_hint sp base reg %d", | |
1397 | insn->sec, insn->offset, hint->sp_reg); | |
1398 | return -1; | |
1399 | } | |
1400 | ||
1401 | cfa->offset = hint->sp_offset; | |
e7c0219b PZ |
1402 | insn->cfi.type = hint->type; |
1403 | insn->cfi.end = hint->end; | |
39358a03 JP |
1404 | } |
1405 | ||
1406 | return 0; | |
1407 | } | |
1408 | ||
b5bc2231 PZ |
1409 | static int read_retpoline_hints(struct objtool_file *file) |
1410 | { | |
63474dc4 | 1411 | struct section *sec; |
b5bc2231 | 1412 | struct instruction *insn; |
f1974222 | 1413 | struct reloc *reloc; |
b5bc2231 | 1414 | |
63474dc4 | 1415 | sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); |
b5bc2231 PZ |
1416 | if (!sec) |
1417 | return 0; | |
1418 | ||
f1974222 MH |
1419 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
1420 | if (reloc->sym->type != STT_SECTION) { | |
63474dc4 | 1421 | WARN("unexpected relocation symbol type in %s", sec->name); |
b5bc2231 PZ |
1422 | return -1; |
1423 | } | |
1424 | ||
f1974222 | 1425 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
b5bc2231 | 1426 | if (!insn) { |
63474dc4 | 1427 | WARN("bad .discard.retpoline_safe entry"); |
b5bc2231 PZ |
1428 | return -1; |
1429 | } | |
1430 | ||
1431 | if (insn->type != INSN_JUMP_DYNAMIC && | |
1432 | insn->type != INSN_CALL_DYNAMIC) { | |
63474dc4 | 1433 | WARN_FUNC("retpoline_safe hint not an indirect jump/call", |
b5bc2231 PZ |
1434 | insn->sec, insn->offset); |
1435 | return -1; | |
1436 | } | |
1437 | ||
1438 | insn->retpoline_safe = true; | |
1439 | } | |
1440 | ||
1441 | return 0; | |
1442 | } | |
1443 | ||
c4a33939 PZ |
1444 | static int read_instr_hints(struct objtool_file *file) |
1445 | { | |
1446 | struct section *sec; | |
1447 | struct instruction *insn; | |
f1974222 | 1448 | struct reloc *reloc; |
c4a33939 PZ |
1449 | |
1450 | sec = find_section_by_name(file->elf, ".rela.discard.instr_end"); | |
1451 | if (!sec) | |
1452 | return 0; | |
1453 | ||
f1974222 MH |
1454 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
1455 | if (reloc->sym->type != STT_SECTION) { | |
c4a33939 PZ |
1456 | WARN("unexpected relocation symbol type in %s", sec->name); |
1457 | return -1; | |
1458 | } | |
1459 | ||
f1974222 | 1460 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
c4a33939 PZ |
1461 | if (!insn) { |
1462 | WARN("bad .discard.instr_end entry"); | |
1463 | return -1; | |
1464 | } | |
1465 | ||
1466 | insn->instr--; | |
1467 | } | |
1468 | ||
1469 | sec = find_section_by_name(file->elf, ".rela.discard.instr_begin"); | |
1470 | if (!sec) | |
1471 | return 0; | |
1472 | ||
f1974222 MH |
1473 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
1474 | if (reloc->sym->type != STT_SECTION) { | |
c4a33939 PZ |
1475 | WARN("unexpected relocation symbol type in %s", sec->name); |
1476 | return -1; | |
1477 | } | |
1478 | ||
f1974222 | 1479 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
c4a33939 PZ |
1480 | if (!insn) { |
1481 | WARN("bad .discard.instr_begin entry"); | |
1482 | return -1; | |
1483 | } | |
1484 | ||
1485 | insn->instr++; | |
1486 | } | |
1487 | ||
1488 | return 0; | |
1489 | } | |
1490 | ||
8aa8eb2a AC |
1491 | static int read_intra_function_calls(struct objtool_file *file) |
1492 | { | |
1493 | struct instruction *insn; | |
1494 | struct section *sec; | |
f1974222 | 1495 | struct reloc *reloc; |
8aa8eb2a AC |
1496 | |
1497 | sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls"); | |
1498 | if (!sec) | |
1499 | return 0; | |
1500 | ||
f1974222 | 1501 | list_for_each_entry(reloc, &sec->reloc_list, list) { |
8aa8eb2a AC |
1502 | unsigned long dest_off; |
1503 | ||
f1974222 | 1504 | if (reloc->sym->type != STT_SECTION) { |
8aa8eb2a AC |
1505 | WARN("unexpected relocation symbol type in %s", |
1506 | sec->name); | |
1507 | return -1; | |
1508 | } | |
1509 | ||
f1974222 | 1510 | insn = find_insn(file, reloc->sym->sec, reloc->addend); |
8aa8eb2a AC |
1511 | if (!insn) { |
1512 | WARN("bad .discard.intra_function_call entry"); | |
1513 | return -1; | |
1514 | } | |
1515 | ||
1516 | if (insn->type != INSN_CALL) { | |
1517 | WARN_FUNC("intra_function_call not a direct call", | |
1518 | insn->sec, insn->offset); | |
1519 | return -1; | |
1520 | } | |
1521 | ||
1522 | /* | |
1523 | * Treat intra-function CALLs as JMPs, but with a stack_op. | |
1524 | * See add_call_destinations(), which strips stack_ops from | |
1525 | * normal CALLs. | |
1526 | */ | |
1527 | insn->type = INSN_JUMP_UNCONDITIONAL; | |
1528 | ||
1529 | dest_off = insn->offset + insn->len + insn->immediate; | |
1530 | insn->jump_dest = find_insn(file, insn->sec, dest_off); | |
1531 | if (!insn->jump_dest) { | |
1532 | WARN_FUNC("can't find call dest at %s+0x%lx", | |
1533 | insn->sec, insn->offset, | |
1534 | insn->sec->name, dest_off); | |
1535 | return -1; | |
1536 | } | |
1537 | } | |
1538 | ||
1539 | return 0; | |
1540 | } | |
1541 | ||
1e7e4788 JP |
1542 | static int read_static_call_tramps(struct objtool_file *file) |
1543 | { | |
1544 | struct section *sec; | |
1545 | struct symbol *func; | |
1546 | ||
1547 | for_each_sec(file, sec) { | |
1548 | list_for_each_entry(func, &sec->symbol_list, list) { | |
1549 | if (func->bind == STB_GLOBAL && | |
1550 | !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR, | |
1551 | strlen(STATIC_CALL_TRAMP_PREFIX_STR))) | |
1552 | func->static_call_tramp = true; | |
1553 | } | |
1554 | } | |
1555 | ||
1556 | return 0; | |
1557 | } | |
1558 | ||
4a60aa05 AX |
1559 | static void mark_rodata(struct objtool_file *file) |
1560 | { | |
1561 | struct section *sec; | |
1562 | bool found = false; | |
1563 | ||
1564 | /* | |
87b512de JP |
1565 | * Search for the following rodata sections, each of which can |
1566 | * potentially contain jump tables: | |
1567 | * | |
1568 | * - .rodata: can contain GCC switch tables | |
1569 | * - .rodata.<func>: same, if -fdata-sections is being used | |
1570 | * - .rodata..c_jump_table: contains C annotated jump tables | |
1571 | * | |
1572 | * .rodata.str1.* sections are ignored; they don't contain jump tables. | |
4a60aa05 AX |
1573 | */ |
1574 | for_each_sec(file, sec) { | |
1ee44470 MS |
1575 | if (!strncmp(sec->name, ".rodata", 7) && |
1576 | !strstr(sec->name, ".str1.")) { | |
4a60aa05 AX |
1577 | sec->rodata = true; |
1578 | found = true; | |
1579 | } | |
1580 | } | |
1581 | ||
1582 | file->rodata = found; | |
1583 | } | |
1584 | ||
dcc914f4 JP |
1585 | static int decode_sections(struct objtool_file *file) |
1586 | { | |
1587 | int ret; | |
1588 | ||
4a60aa05 AX |
1589 | mark_rodata(file); |
1590 | ||
dcc914f4 JP |
1591 | ret = decode_instructions(file); |
1592 | if (ret) | |
1593 | return ret; | |
1594 | ||
1595 | ret = add_dead_ends(file); | |
1596 | if (ret) | |
1597 | return ret; | |
1598 | ||
1599 | add_ignores(file); | |
ea24213d | 1600 | add_uaccess_safe(file); |
dcc914f4 | 1601 | |
ff05ab23 | 1602 | ret = add_ignore_alternatives(file); |
258c7605 JP |
1603 | if (ret) |
1604 | return ret; | |
1605 | ||
5b06fd3b PZ |
1606 | ret = read_static_call_tramps(file); |
1607 | if (ret) | |
1608 | return ret; | |
1609 | ||
dcc914f4 JP |
1610 | ret = add_jump_destinations(file); |
1611 | if (ret) | |
1612 | return ret; | |
1613 | ||
a845c7cf | 1614 | ret = add_special_section_alts(file); |
dcc914f4 JP |
1615 | if (ret) |
1616 | return ret; | |
1617 | ||
8aa8eb2a AC |
1618 | ret = read_intra_function_calls(file); |
1619 | if (ret) | |
1620 | return ret; | |
1621 | ||
a845c7cf | 1622 | ret = add_call_destinations(file); |
dcc914f4 JP |
1623 | if (ret) |
1624 | return ret; | |
1625 | ||
e7c2bc37 | 1626 | ret = add_jump_table_alts(file); |
dcc914f4 JP |
1627 | if (ret) |
1628 | return ret; | |
1629 | ||
39358a03 JP |
1630 | ret = read_unwind_hints(file); |
1631 | if (ret) | |
1632 | return ret; | |
1633 | ||
b5bc2231 PZ |
1634 | ret = read_retpoline_hints(file); |
1635 | if (ret) | |
1636 | return ret; | |
1637 | ||
c4a33939 PZ |
1638 | ret = read_instr_hints(file); |
1639 | if (ret) | |
1640 | return ret; | |
1641 | ||
dcc914f4 JP |
1642 | return 0; |
1643 | } | |
1644 | ||
1645 | static bool is_fentry_call(struct instruction *insn) | |
1646 | { | |
87cf61fe | 1647 | if (insn->type == INSN_CALL && insn->call_dest && |
dcc914f4 JP |
1648 | insn->call_dest->type == STT_NOTYPE && |
1649 | !strcmp(insn->call_dest->name, "__fentry__")) | |
1650 | return true; | |
1651 | ||
1652 | return false; | |
1653 | } | |
1654 | ||
e25eea89 | 1655 | static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state) |
dcc914f4 | 1656 | { |
e25eea89 | 1657 | u8 ret_offset = insn->ret_offset; |
e7c0219b | 1658 | struct cfi_state *cfi = &state->cfi; |
baa41469 JP |
1659 | int i; |
1660 | ||
e7c0219b | 1661 | if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap) |
e25eea89 PZ |
1662 | return true; |
1663 | ||
e7c0219b | 1664 | if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset) |
baa41469 JP |
1665 | return true; |
1666 | ||
e7c0219b | 1667 | if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset) |
e25eea89 PZ |
1668 | return true; |
1669 | ||
c721b3f8 AC |
1670 | /* |
1671 | * If there is a ret offset hint then don't check registers | |
1672 | * because a callee-saved register might have been pushed on | |
1673 | * the stack. | |
1674 | */ | |
1675 | if (ret_offset) | |
1676 | return false; | |
1677 | ||
e25eea89 | 1678 | for (i = 0; i < CFI_NUM_REGS; i++) { |
e7c0219b PZ |
1679 | if (cfi->regs[i].base != initial_func_cfi.regs[i].base || |
1680 | cfi->regs[i].offset != initial_func_cfi.regs[i].offset) | |
baa41469 | 1681 | return true; |
e25eea89 | 1682 | } |
baa41469 JP |
1683 | |
1684 | return false; | |
1685 | } | |
1686 | ||
1687 | static bool has_valid_stack_frame(struct insn_state *state) | |
1688 | { | |
e7c0219b PZ |
1689 | struct cfi_state *cfi = &state->cfi; |
1690 | ||
1691 | if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA && | |
1692 | cfi->regs[CFI_BP].offset == -16) | |
baa41469 JP |
1693 | return true; |
1694 | ||
e7c0219b | 1695 | if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP) |
baa41469 JP |
1696 | return true; |
1697 | ||
1698 | return false; | |
dcc914f4 JP |
1699 | } |
1700 | ||
e7c0219b PZ |
1701 | static int update_cfi_state_regs(struct instruction *insn, |
1702 | struct cfi_state *cfi, | |
65ea47dc | 1703 | struct stack_op *op) |
627fce14 | 1704 | { |
e7c0219b | 1705 | struct cfi_reg *cfa = &cfi->cfa; |
627fce14 | 1706 | |
d8dd25a4 | 1707 | if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT) |
627fce14 JP |
1708 | return 0; |
1709 | ||
1710 | /* push */ | |
ea24213d | 1711 | if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF) |
627fce14 JP |
1712 | cfa->offset += 8; |
1713 | ||
1714 | /* pop */ | |
ea24213d | 1715 | if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF) |
627fce14 JP |
1716 | cfa->offset -= 8; |
1717 | ||
1718 | /* add immediate to sp */ | |
1719 | if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD && | |
1720 | op->dest.reg == CFI_SP && op->src.reg == CFI_SP) | |
1721 | cfa->offset -= op->src.offset; | |
1722 | ||
1723 | return 0; | |
1724 | } | |
1725 | ||
e7c0219b | 1726 | static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset) |
dcc914f4 | 1727 | { |
bf4d1a83 | 1728 | if (arch_callee_saved_reg(reg) && |
e7c0219b PZ |
1729 | cfi->regs[reg].base == CFI_UNDEFINED) { |
1730 | cfi->regs[reg].base = base; | |
1731 | cfi->regs[reg].offset = offset; | |
baa41469 | 1732 | } |
dcc914f4 JP |
1733 | } |
1734 | ||
e7c0219b | 1735 | static void restore_reg(struct cfi_state *cfi, unsigned char reg) |
dcc914f4 | 1736 | { |
e7c0219b PZ |
1737 | cfi->regs[reg].base = initial_func_cfi.regs[reg].base; |
1738 | cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset; | |
baa41469 JP |
1739 | } |
1740 | ||
1741 | /* | |
1742 | * A note about DRAP stack alignment: | |
1743 | * | |
1744 | * GCC has the concept of a DRAP register, which is used to help keep track of | |
1745 | * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP | |
1746 | * register. The typical DRAP pattern is: | |
1747 | * | |
1748 | * 4c 8d 54 24 08 lea 0x8(%rsp),%r10 | |
1749 | * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp | |
1750 | * 41 ff 72 f8 pushq -0x8(%r10) | |
1751 | * 55 push %rbp | |
1752 | * 48 89 e5 mov %rsp,%rbp | |
1753 | * (more pushes) | |
1754 | * 41 52 push %r10 | |
1755 | * ... | |
1756 | * 41 5a pop %r10 | |
1757 | * (more pops) | |
1758 | * 5d pop %rbp | |
1759 | * 49 8d 62 f8 lea -0x8(%r10),%rsp | |
1760 | * c3 retq | |
1761 | * | |
1762 | * There are some variations in the epilogues, like: | |
1763 | * | |
1764 | * 5b pop %rbx | |
1765 | * 41 5a pop %r10 | |
1766 | * 41 5c pop %r12 | |
1767 | * 41 5d pop %r13 | |
1768 | * 41 5e pop %r14 | |
1769 | * c9 leaveq | |
1770 | * 49 8d 62 f8 lea -0x8(%r10),%rsp | |
1771 | * c3 retq | |
1772 | * | |
1773 | * and: | |
1774 | * | |
1775 | * 4c 8b 55 e8 mov -0x18(%rbp),%r10 | |
1776 | * 48 8b 5d e0 mov -0x20(%rbp),%rbx | |
1777 | * 4c 8b 65 f0 mov -0x10(%rbp),%r12 | |
1778 | * 4c 8b 6d f8 mov -0x8(%rbp),%r13 | |
1779 | * c9 leaveq | |
1780 | * 49 8d 62 f8 lea -0x8(%r10),%rsp | |
1781 | * c3 retq | |
1782 | * | |
1783 | * Sometimes r13 is used as the DRAP register, in which case it's saved and | |
1784 | * restored beforehand: | |
1785 | * | |
1786 | * 41 55 push %r13 | |
1787 | * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13 | |
1788 | * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp | |
1789 | * ... | |
1790 | * 49 8d 65 f0 lea -0x10(%r13),%rsp | |
1791 | * 41 5d pop %r13 | |
1792 | * c3 retq | |
1793 | */ | |
e7c0219b | 1794 | static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi, |
65ea47dc | 1795 | struct stack_op *op) |
baa41469 | 1796 | { |
e7c0219b PZ |
1797 | struct cfi_reg *cfa = &cfi->cfa; |
1798 | struct cfi_reg *regs = cfi->regs; | |
baa41469 JP |
1799 | |
1800 | /* stack operations don't make sense with an undefined CFA */ | |
1801 | if (cfa->base == CFI_UNDEFINED) { | |
1802 | if (insn->func) { | |
1803 | WARN_FUNC("undefined stack state", insn->sec, insn->offset); | |
1804 | return -1; | |
1805 | } | |
1806 | return 0; | |
1807 | } | |
1808 | ||
ee819aed JT |
1809 | if (cfi->type == UNWIND_HINT_TYPE_REGS || |
1810 | cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL) | |
e7c0219b | 1811 | return update_cfi_state_regs(insn, cfi, op); |
627fce14 | 1812 | |
baa41469 JP |
1813 | switch (op->dest.type) { |
1814 | ||
1815 | case OP_DEST_REG: | |
1816 | switch (op->src.type) { | |
1817 | ||
1818 | case OP_SRC_REG: | |
0d0970ee JP |
1819 | if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP && |
1820 | cfa->base == CFI_SP && | |
1821 | regs[CFI_BP].base == CFI_CFA && | |
1822 | regs[CFI_BP].offset == -cfa->offset) { | |
1823 | ||
1824 | /* mov %rsp, %rbp */ | |
1825 | cfa->base = op->dest.reg; | |
e7c0219b | 1826 | cfi->bp_scratch = false; |
0d0970ee | 1827 | } |
dd88a0a0 | 1828 | |
0d0970ee | 1829 | else if (op->src.reg == CFI_SP && |
e7c0219b | 1830 | op->dest.reg == CFI_BP && cfi->drap) { |
dd88a0a0 | 1831 | |
0d0970ee JP |
1832 | /* drap: mov %rsp, %rbp */ |
1833 | regs[CFI_BP].base = CFI_BP; | |
e7c0219b PZ |
1834 | regs[CFI_BP].offset = -cfi->stack_size; |
1835 | cfi->bp_scratch = false; | |
0d0970ee | 1836 | } |
dd88a0a0 | 1837 | |
0d0970ee JP |
1838 | else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
1839 | ||
1840 | /* | |
1841 | * mov %rsp, %reg | |
1842 | * | |
1843 | * This is needed for the rare case where GCC | |
1844 | * does: | |
1845 | * | |
1846 | * mov %rsp, %rax | |
1847 | * ... | |
1848 | * mov %rax, %rsp | |
1849 | */ | |
e7c0219b PZ |
1850 | cfi->vals[op->dest.reg].base = CFI_CFA; |
1851 | cfi->vals[op->dest.reg].offset = -cfi->stack_size; | |
dd88a0a0 JP |
1852 | } |
1853 | ||
3c1f0583 JP |
1854 | else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP && |
1855 | cfa->base == CFI_BP) { | |
1856 | ||
1857 | /* | |
1858 | * mov %rbp, %rsp | |
1859 | * | |
1860 | * Restore the original stack pointer (Clang). | |
1861 | */ | |
e7c0219b | 1862 | cfi->stack_size = -cfi->regs[CFI_BP].offset; |
3c1f0583 JP |
1863 | } |
1864 | ||
dd88a0a0 JP |
1865 | else if (op->dest.reg == cfa->base) { |
1866 | ||
1867 | /* mov %reg, %rsp */ | |
1868 | if (cfa->base == CFI_SP && | |
e7c0219b | 1869 | cfi->vals[op->src.reg].base == CFI_CFA) { |
dd88a0a0 JP |
1870 | |
1871 | /* | |
1872 | * This is needed for the rare case | |
1873 | * where GCC does something dumb like: | |
1874 | * | |
1875 | * lea 0x8(%rsp), %rcx | |
1876 | * ... | |
1877 | * mov %rcx, %rsp | |
1878 | */ | |
e7c0219b PZ |
1879 | cfa->offset = -cfi->vals[op->src.reg].offset; |
1880 | cfi->stack_size = cfa->offset; | |
dd88a0a0 JP |
1881 | |
1882 | } else { | |
1883 | cfa->base = CFI_UNDEFINED; | |
1884 | cfa->offset = 0; | |
1885 | } | |
baa41469 JP |
1886 | } |
1887 | ||
1888 | break; | |
1889 | ||
1890 | case OP_SRC_ADD: | |
1891 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) { | |
1892 | ||
1893 | /* add imm, %rsp */ | |
e7c0219b | 1894 | cfi->stack_size -= op->src.offset; |
baa41469 JP |
1895 | if (cfa->base == CFI_SP) |
1896 | cfa->offset -= op->src.offset; | |
1897 | break; | |
1898 | } | |
1899 | ||
1900 | if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) { | |
1901 | ||
1902 | /* lea disp(%rbp), %rsp */ | |
e7c0219b | 1903 | cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset); |
baa41469 JP |
1904 | break; |
1905 | } | |
1906 | ||
dd88a0a0 | 1907 | if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { |
baa41469 JP |
1908 | |
1909 | /* drap: lea disp(%rsp), %drap */ | |
e7c0219b | 1910 | cfi->drap_reg = op->dest.reg; |
dd88a0a0 JP |
1911 | |
1912 | /* | |
1913 | * lea disp(%rsp), %reg | |
1914 | * | |
1915 | * This is needed for the rare case where GCC | |
1916 | * does something dumb like: | |
1917 | * | |
1918 | * lea 0x8(%rsp), %rcx | |
1919 | * ... | |
1920 | * mov %rcx, %rsp | |
1921 | */ | |
e7c0219b PZ |
1922 | cfi->vals[op->dest.reg].base = CFI_CFA; |
1923 | cfi->vals[op->dest.reg].offset = \ | |
1924 | -cfi->stack_size + op->src.offset; | |
dd88a0a0 | 1925 | |
baa41469 JP |
1926 | break; |
1927 | } | |
1928 | ||
e7c0219b PZ |
1929 | if (cfi->drap && op->dest.reg == CFI_SP && |
1930 | op->src.reg == cfi->drap_reg) { | |
baa41469 JP |
1931 | |
1932 | /* drap: lea disp(%drap), %rsp */ | |
1933 | cfa->base = CFI_SP; | |
e7c0219b PZ |
1934 | cfa->offset = cfi->stack_size = -op->src.offset; |
1935 | cfi->drap_reg = CFI_UNDEFINED; | |
1936 | cfi->drap = false; | |
baa41469 JP |
1937 | break; |
1938 | } | |
1939 | ||
e7c0219b | 1940 | if (op->dest.reg == cfi->cfa.base) { |
baa41469 JP |
1941 | WARN_FUNC("unsupported stack register modification", |
1942 | insn->sec, insn->offset); | |
1943 | return -1; | |
1944 | } | |
1945 | ||
1946 | break; | |
1947 | ||
1948 | case OP_SRC_AND: | |
1949 | if (op->dest.reg != CFI_SP || | |
e7c0219b PZ |
1950 | (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) || |
1951 | (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) { | |
baa41469 JP |
1952 | WARN_FUNC("unsupported stack pointer realignment", |
1953 | insn->sec, insn->offset); | |
1954 | return -1; | |
1955 | } | |
1956 | ||
e7c0219b | 1957 | if (cfi->drap_reg != CFI_UNDEFINED) { |
baa41469 | 1958 | /* drap: and imm, %rsp */ |
e7c0219b PZ |
1959 | cfa->base = cfi->drap_reg; |
1960 | cfa->offset = cfi->stack_size = 0; | |
1961 | cfi->drap = true; | |
baa41469 JP |
1962 | } |
1963 | ||
1964 | /* | |
1965 | * Older versions of GCC (4.8ish) realign the stack | |
1966 | * without DRAP, with a frame pointer. | |
1967 | */ | |
1968 | ||
1969 | break; | |
1970 | ||
1971 | case OP_SRC_POP: | |
ea24213d | 1972 | case OP_SRC_POPF: |
e7c0219b | 1973 | if (!cfi->drap && op->dest.reg == cfa->base) { |
baa41469 JP |
1974 | |
1975 | /* pop %rbp */ | |
1976 | cfa->base = CFI_SP; | |
1977 | } | |
1978 | ||
e7c0219b PZ |
1979 | if (cfi->drap && cfa->base == CFI_BP_INDIRECT && |
1980 | op->dest.reg == cfi->drap_reg && | |
1981 | cfi->drap_offset == -cfi->stack_size) { | |
baa41469 | 1982 | |
bf4d1a83 | 1983 | /* drap: pop %drap */ |
e7c0219b | 1984 | cfa->base = cfi->drap_reg; |
bf4d1a83 | 1985 | cfa->offset = 0; |
e7c0219b | 1986 | cfi->drap_offset = -1; |
baa41469 | 1987 | |
e7c0219b | 1988 | } else if (regs[op->dest.reg].offset == -cfi->stack_size) { |
baa41469 | 1989 | |
bf4d1a83 | 1990 | /* pop %reg */ |
e7c0219b | 1991 | restore_reg(cfi, op->dest.reg); |
baa41469 JP |
1992 | } |
1993 | ||
e7c0219b | 1994 | cfi->stack_size -= 8; |
baa41469 JP |
1995 | if (cfa->base == CFI_SP) |
1996 | cfa->offset -= 8; | |
1997 | ||
1998 | break; | |
1999 | ||
2000 | case OP_SRC_REG_INDIRECT: | |
e7c0219b PZ |
2001 | if (cfi->drap && op->src.reg == CFI_BP && |
2002 | op->src.offset == cfi->drap_offset) { | |
bf4d1a83 JP |
2003 | |
2004 | /* drap: mov disp(%rbp), %drap */ | |
e7c0219b | 2005 | cfa->base = cfi->drap_reg; |
bf4d1a83 | 2006 | cfa->offset = 0; |
e7c0219b | 2007 | cfi->drap_offset = -1; |
bf4d1a83 JP |
2008 | } |
2009 | ||
e7c0219b | 2010 | if (cfi->drap && op->src.reg == CFI_BP && |
baa41469 JP |
2011 | op->src.offset == regs[op->dest.reg].offset) { |
2012 | ||
2013 | /* drap: mov disp(%rbp), %reg */ | |
e7c0219b | 2014 | restore_reg(cfi, op->dest.reg); |
baa41469 JP |
2015 | |
2016 | } else if (op->src.reg == cfa->base && | |
2017 | op->src.offset == regs[op->dest.reg].offset + cfa->offset) { | |
2018 | ||
2019 | /* mov disp(%rbp), %reg */ | |
2020 | /* mov disp(%rsp), %reg */ | |
e7c0219b | 2021 | restore_reg(cfi, op->dest.reg); |
baa41469 JP |
2022 | } |
2023 | ||
2024 | break; | |
2025 | ||
2026 | default: | |
2027 | WARN_FUNC("unknown stack-related instruction", | |
2028 | insn->sec, insn->offset); | |
2029 | return -1; | |
2030 | } | |
2031 | ||
2032 | break; | |
2033 | ||
2034 | case OP_DEST_PUSH: | |
ea24213d | 2035 | case OP_DEST_PUSHF: |
e7c0219b | 2036 | cfi->stack_size += 8; |
baa41469 JP |
2037 | if (cfa->base == CFI_SP) |
2038 | cfa->offset += 8; | |
2039 | ||
2040 | if (op->src.type != OP_SRC_REG) | |
2041 | break; | |
2042 | ||
e7c0219b PZ |
2043 | if (cfi->drap) { |
2044 | if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { | |
baa41469 JP |
2045 | |
2046 | /* drap: push %drap */ | |
2047 | cfa->base = CFI_BP_INDIRECT; | |
e7c0219b | 2048 | cfa->offset = -cfi->stack_size; |
baa41469 | 2049 | |
bf4d1a83 | 2050 | /* save drap so we know when to restore it */ |
e7c0219b | 2051 | cfi->drap_offset = -cfi->stack_size; |
baa41469 | 2052 | |
e7c0219b | 2053 | } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) { |
baa41469 JP |
2054 | |
2055 | /* drap: push %rbp */ | |
e7c0219b | 2056 | cfi->stack_size = 0; |
baa41469 JP |
2057 | |
2058 | } else if (regs[op->src.reg].base == CFI_UNDEFINED) { | |
2059 | ||
2060 | /* drap: push %reg */ | |
e7c0219b | 2061 | save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size); |
baa41469 JP |
2062 | } |
2063 | ||
2064 | } else { | |
2065 | ||
2066 | /* push %reg */ | |
e7c0219b | 2067 | save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size); |
baa41469 JP |
2068 | } |
2069 | ||
2070 | /* detect when asm code uses rbp as a scratch register */ | |
867ac9d7 | 2071 | if (!no_fp && insn->func && op->src.reg == CFI_BP && |
baa41469 | 2072 | cfa->base != CFI_BP) |
e7c0219b | 2073 | cfi->bp_scratch = true; |
baa41469 JP |
2074 | break; |
2075 | ||
2076 | case OP_DEST_REG_INDIRECT: | |
2077 | ||
e7c0219b PZ |
2078 | if (cfi->drap) { |
2079 | if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { | |
baa41469 JP |
2080 | |
2081 | /* drap: mov %drap, disp(%rbp) */ | |
2082 | cfa->base = CFI_BP_INDIRECT; | |
2083 | cfa->offset = op->dest.offset; | |
2084 | ||
bf4d1a83 | 2085 | /* save drap offset so we know when to restore it */ |
e7c0219b | 2086 | cfi->drap_offset = op->dest.offset; |
baa41469 JP |
2087 | } |
2088 | ||
2089 | else if (regs[op->src.reg].base == CFI_UNDEFINED) { | |
2090 | ||
2091 | /* drap: mov reg, disp(%rbp) */ | |
e7c0219b | 2092 | save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset); |
baa41469 JP |
2093 | } |
2094 | ||
2095 | } else if (op->dest.reg == cfa->base) { | |
2096 | ||
2097 | /* mov reg, disp(%rbp) */ | |
2098 | /* mov reg, disp(%rsp) */ | |
e7c0219b PZ |
2099 | save_reg(cfi, op->src.reg, CFI_CFA, |
2100 | op->dest.offset - cfi->cfa.offset); | |
baa41469 JP |
2101 | } |
2102 | ||
2103 | break; | |
2104 | ||
2105 | case OP_DEST_LEAVE: | |
e7c0219b PZ |
2106 | if ((!cfi->drap && cfa->base != CFI_BP) || |
2107 | (cfi->drap && cfa->base != cfi->drap_reg)) { | |
baa41469 JP |
2108 | WARN_FUNC("leave instruction with modified stack frame", |
2109 | insn->sec, insn->offset); | |
2110 | return -1; | |
2111 | } | |
2112 | ||
2113 | /* leave (mov %rbp, %rsp; pop %rbp) */ | |
2114 | ||
e7c0219b PZ |
2115 | cfi->stack_size = -cfi->regs[CFI_BP].offset - 8; |
2116 | restore_reg(cfi, CFI_BP); | |
baa41469 | 2117 | |
e7c0219b | 2118 | if (!cfi->drap) { |
baa41469 JP |
2119 | cfa->base = CFI_SP; |
2120 | cfa->offset -= 8; | |
2121 | } | |
2122 | ||
2123 | break; | |
2124 | ||
2125 | case OP_DEST_MEM: | |
ea24213d | 2126 | if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) { |
baa41469 JP |
2127 | WARN_FUNC("unknown stack-related memory operation", |
2128 | insn->sec, insn->offset); | |
2129 | return -1; | |
2130 | } | |
2131 | ||
2132 | /* pop mem */ | |
e7c0219b | 2133 | cfi->stack_size -= 8; |
baa41469 JP |
2134 | if (cfa->base == CFI_SP) |
2135 | cfa->offset -= 8; | |
2136 | ||
2137 | break; | |
2138 | ||
2139 | default: | |
2140 | WARN_FUNC("unknown stack-related instruction", | |
2141 | insn->sec, insn->offset); | |
2142 | return -1; | |
2143 | } | |
2144 | ||
2145 | return 0; | |
2146 | } | |
2147 | ||
65ea47dc JT |
2148 | static int handle_insn_ops(struct instruction *insn, struct insn_state *state) |
2149 | { | |
2150 | struct stack_op *op; | |
2151 | ||
2152 | list_for_each_entry(op, &insn->stack_ops, list) { | |
ab3852ab | 2153 | struct cfi_state old_cfi = state->cfi; |
65ea47dc JT |
2154 | int res; |
2155 | ||
e7c0219b | 2156 | res = update_cfi_state(insn, &state->cfi, op); |
65ea47dc JT |
2157 | if (res) |
2158 | return res; | |
2159 | ||
ab3852ab PZ |
2160 | if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) { |
2161 | WARN_FUNC("alternative modifies stack", insn->sec, insn->offset); | |
2162 | return -1; | |
2163 | } | |
2164 | ||
65ea47dc JT |
2165 | if (op->dest.type == OP_DEST_PUSHF) { |
2166 | if (!state->uaccess_stack) { | |
2167 | state->uaccess_stack = 1; | |
2168 | } else if (state->uaccess_stack >> 31) { | |
2169 | WARN_FUNC("PUSHF stack exhausted", | |
2170 | insn->sec, insn->offset); | |
2171 | return 1; | |
2172 | } | |
2173 | state->uaccess_stack <<= 1; | |
2174 | state->uaccess_stack |= state->uaccess; | |
2175 | } | |
2176 | ||
2177 | if (op->src.type == OP_SRC_POPF) { | |
2178 | if (state->uaccess_stack) { | |
2179 | state->uaccess = state->uaccess_stack & 1; | |
2180 | state->uaccess_stack >>= 1; | |
2181 | if (state->uaccess_stack == 1) | |
2182 | state->uaccess_stack = 0; | |
2183 | } | |
2184 | } | |
2185 | } | |
2186 | ||
2187 | return 0; | |
2188 | } | |
2189 | ||
e7c0219b | 2190 | static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2) |
baa41469 | 2191 | { |
e7c0219b | 2192 | struct cfi_state *cfi1 = &insn->cfi; |
baa41469 JP |
2193 | int i; |
2194 | ||
e7c0219b PZ |
2195 | if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) { |
2196 | ||
baa41469 JP |
2197 | WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d", |
2198 | insn->sec, insn->offset, | |
e7c0219b PZ |
2199 | cfi1->cfa.base, cfi1->cfa.offset, |
2200 | cfi2->cfa.base, cfi2->cfa.offset); | |
baa41469 | 2201 | |
e7c0219b | 2202 | } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) { |
baa41469 | 2203 | for (i = 0; i < CFI_NUM_REGS; i++) { |
e7c0219b | 2204 | if (!memcmp(&cfi1->regs[i], &cfi2->regs[i], |
baa41469 JP |
2205 | sizeof(struct cfi_reg))) |
2206 | continue; | |
2207 | ||
2208 | WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d", | |
2209 | insn->sec, insn->offset, | |
e7c0219b PZ |
2210 | i, cfi1->regs[i].base, cfi1->regs[i].offset, |
2211 | i, cfi2->regs[i].base, cfi2->regs[i].offset); | |
baa41469 JP |
2212 | break; |
2213 | } | |
2214 | ||
e7c0219b PZ |
2215 | } else if (cfi1->type != cfi2->type) { |
2216 | ||
627fce14 | 2217 | WARN_FUNC("stack state mismatch: type1=%d type2=%d", |
e7c0219b PZ |
2218 | insn->sec, insn->offset, cfi1->type, cfi2->type); |
2219 | ||
2220 | } else if (cfi1->drap != cfi2->drap || | |
2221 | (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) || | |
2222 | (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) { | |
627fce14 | 2223 | |
bf4d1a83 | 2224 | WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)", |
baa41469 | 2225 | insn->sec, insn->offset, |
e7c0219b PZ |
2226 | cfi1->drap, cfi1->drap_reg, cfi1->drap_offset, |
2227 | cfi2->drap, cfi2->drap_reg, cfi2->drap_offset); | |
baa41469 JP |
2228 | |
2229 | } else | |
2230 | return true; | |
2231 | ||
2232 | return false; | |
dcc914f4 JP |
2233 | } |
2234 | ||
ea24213d PZ |
2235 | static inline bool func_uaccess_safe(struct symbol *func) |
2236 | { | |
2237 | if (func) | |
e10cd8fe | 2238 | return func->uaccess_safe; |
ea24213d PZ |
2239 | |
2240 | return false; | |
2241 | } | |
2242 | ||
0c1ddd33 | 2243 | static inline const char *call_dest_name(struct instruction *insn) |
ea24213d PZ |
2244 | { |
2245 | if (insn->call_dest) | |
2246 | return insn->call_dest->name; | |
2247 | ||
2248 | return "{dynamic}"; | |
2249 | } | |
2250 | ||
6b643a07 PZ |
2251 | static inline bool noinstr_call_dest(struct symbol *func) |
2252 | { | |
2253 | /* | |
2254 | * We can't deal with indirect function calls at present; | |
2255 | * assume they're instrumented. | |
2256 | */ | |
2257 | if (!func) | |
2258 | return false; | |
2259 | ||
2260 | /* | |
2261 | * If the symbol is from a noinstr section; we good. | |
2262 | */ | |
2263 | if (func->sec->noinstr) | |
2264 | return true; | |
2265 | ||
2266 | /* | |
2267 | * The __ubsan_handle_*() calls are like WARN(), they only happen when | |
2268 | * something 'BAD' happened. At the risk of taking the machine down, | |
2269 | * let them proceed to get the message out. | |
2270 | */ | |
2271 | if (!strncmp(func->name, "__ubsan_handle_", 15)) | |
2272 | return true; | |
2273 | ||
2274 | return false; | |
2275 | } | |
2276 | ||
ea24213d PZ |
2277 | static int validate_call(struct instruction *insn, struct insn_state *state) |
2278 | { | |
c4a33939 | 2279 | if (state->noinstr && state->instr <= 0 && |
6b643a07 | 2280 | !noinstr_call_dest(insn->call_dest)) { |
c4a33939 PZ |
2281 | WARN_FUNC("call to %s() leaves .noinstr.text section", |
2282 | insn->sec, insn->offset, call_dest_name(insn)); | |
2283 | return 1; | |
2284 | } | |
2285 | ||
ea24213d PZ |
2286 | if (state->uaccess && !func_uaccess_safe(insn->call_dest)) { |
2287 | WARN_FUNC("call to %s() with UACCESS enabled", | |
0c1ddd33 | 2288 | insn->sec, insn->offset, call_dest_name(insn)); |
ea24213d PZ |
2289 | return 1; |
2290 | } | |
2291 | ||
2f0f9e9a PZ |
2292 | if (state->df) { |
2293 | WARN_FUNC("call to %s() with DF set", | |
0c1ddd33 | 2294 | insn->sec, insn->offset, call_dest_name(insn)); |
2f0f9e9a PZ |
2295 | return 1; |
2296 | } | |
2297 | ||
ea24213d PZ |
2298 | return 0; |
2299 | } | |
2300 | ||
54262aa2 PZ |
2301 | static int validate_sibling_call(struct instruction *insn, struct insn_state *state) |
2302 | { | |
e25eea89 | 2303 | if (has_modified_stack_frame(insn, state)) { |
54262aa2 PZ |
2304 | WARN_FUNC("sibling call from callable instruction with modified stack frame", |
2305 | insn->sec, insn->offset); | |
2306 | return 1; | |
2307 | } | |
2308 | ||
ea24213d | 2309 | return validate_call(insn, state); |
54262aa2 PZ |
2310 | } |
2311 | ||
a92e92d1 PZ |
2312 | static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state) |
2313 | { | |
c4a33939 PZ |
2314 | if (state->noinstr && state->instr > 0) { |
2315 | WARN_FUNC("return with instrumentation enabled", | |
2316 | insn->sec, insn->offset); | |
2317 | return 1; | |
2318 | } | |
2319 | ||
a92e92d1 PZ |
2320 | if (state->uaccess && !func_uaccess_safe(func)) { |
2321 | WARN_FUNC("return with UACCESS enabled", | |
2322 | insn->sec, insn->offset); | |
2323 | return 1; | |
2324 | } | |
2325 | ||
2326 | if (!state->uaccess && func_uaccess_safe(func)) { | |
2327 | WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", | |
2328 | insn->sec, insn->offset); | |
2329 | return 1; | |
2330 | } | |
2331 | ||
2332 | if (state->df) { | |
2333 | WARN_FUNC("return with DF set", | |
2334 | insn->sec, insn->offset); | |
2335 | return 1; | |
2336 | } | |
2337 | ||
e25eea89 | 2338 | if (func && has_modified_stack_frame(insn, state)) { |
a92e92d1 PZ |
2339 | WARN_FUNC("return with modified stack frame", |
2340 | insn->sec, insn->offset); | |
2341 | return 1; | |
2342 | } | |
2343 | ||
e7c0219b | 2344 | if (state->cfi.bp_scratch) { |
b2966952 JP |
2345 | WARN_FUNC("BP used as a scratch register", |
2346 | insn->sec, insn->offset); | |
a92e92d1 PZ |
2347 | return 1; |
2348 | } | |
2349 | ||
2350 | return 0; | |
2351 | } | |
2352 | ||
7117f16b PZ |
2353 | /* |
2354 | * Alternatives should not contain any ORC entries, this in turn means they | |
2355 | * should not contain any CFI ops, which implies all instructions should have | |
2356 | * the same same CFI state. | |
2357 | * | |
2358 | * It is possible to constuct alternatives that have unreachable holes that go | |
2359 | * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED | |
2360 | * states which then results in ORC entries, which we just said we didn't want. | |
2361 | * | |
2362 | * Avoid them by copying the CFI entry of the first instruction into the whole | |
2363 | * alternative. | |
2364 | */ | |
2365 | static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn) | |
2366 | { | |
2367 | struct instruction *first_insn = insn; | |
2368 | int alt_group = insn->alt_group; | |
2369 | ||
2370 | sec_for_each_insn_continue(file, insn) { | |
2371 | if (insn->alt_group != alt_group) | |
2372 | break; | |
2373 | insn->cfi = first_insn->cfi; | |
2374 | } | |
2375 | } | |
2376 | ||
dcc914f4 JP |
2377 | /* |
2378 | * Follow the branch starting at the given instruction, and recursively follow | |
2379 | * any other branches (jumps). Meanwhile, track the frame pointer state at | |
2380 | * each instruction and validate all the rules described in | |
2381 | * tools/objtool/Documentation/stack-validation.txt. | |
2382 | */ | |
c705cecc | 2383 | static int validate_branch(struct objtool_file *file, struct symbol *func, |
b7460462 | 2384 | struct instruction *insn, struct insn_state state) |
dcc914f4 JP |
2385 | { |
2386 | struct alternative *alt; | |
b7460462 | 2387 | struct instruction *next_insn; |
dcc914f4 | 2388 | struct section *sec; |
882a0db9 | 2389 | u8 visited; |
dcc914f4 JP |
2390 | int ret; |
2391 | ||
dcc914f4 | 2392 | sec = insn->sec; |
dcc914f4 | 2393 | |
dcc914f4 | 2394 | while (1) { |
39358a03 JP |
2395 | next_insn = next_insn_same_sec(file, insn); |
2396 | ||
13810435 | 2397 | if (file->c_file && func && insn->func && func != insn->func->pfunc) { |
ee97638b JP |
2398 | WARN("%s() falls through to next function %s()", |
2399 | func->name, insn->func->name); | |
2400 | return 1; | |
dcc914f4 JP |
2401 | } |
2402 | ||
4855022a JP |
2403 | if (func && insn->ignore) { |
2404 | WARN_FUNC("BUG: why am I validating an ignored function?", | |
2405 | sec, insn->offset); | |
12b25729 | 2406 | return 1; |
4855022a JP |
2407 | } |
2408 | ||
882a0db9 | 2409 | visited = 1 << state.uaccess; |
dcc914f4 | 2410 | if (insn->visited) { |
e7c0219b | 2411 | if (!insn->hint && !insn_cfi_match(insn, &state.cfi)) |
dcc914f4 | 2412 | return 1; |
dcc914f4 | 2413 | |
882a0db9 | 2414 | if (insn->visited & visited) |
ea24213d | 2415 | return 0; |
dcc914f4 JP |
2416 | } |
2417 | ||
c4a33939 PZ |
2418 | if (state.noinstr) |
2419 | state.instr += insn->instr; | |
2420 | ||
c536ed2f | 2421 | if (insn->hint) |
e7c0219b | 2422 | state.cfi = insn->cfi; |
c536ed2f | 2423 | else |
e7c0219b | 2424 | insn->cfi = state.cfi; |
dcc914f4 | 2425 | |
882a0db9 | 2426 | insn->visited |= visited; |
baa41469 | 2427 | |
7117f16b | 2428 | if (!insn->ignore_alts && !list_empty(&insn->alts)) { |
764eef4b PZ |
2429 | bool skip_orig = false; |
2430 | ||
a845c7cf | 2431 | list_for_each_entry(alt, &insn->alts, list) { |
764eef4b PZ |
2432 | if (alt->skip_orig) |
2433 | skip_orig = true; | |
2434 | ||
c705cecc | 2435 | ret = validate_branch(file, func, alt->insn, state); |
7697eee3 PZ |
2436 | if (ret) { |
2437 | if (backtrace) | |
2438 | BT_FUNC("(alt)", insn); | |
2439 | return ret; | |
2440 | } | |
a845c7cf | 2441 | } |
764eef4b | 2442 | |
7117f16b PZ |
2443 | if (insn->alt_group) |
2444 | fill_alternative_cfi(file, insn); | |
2445 | ||
764eef4b PZ |
2446 | if (skip_orig) |
2447 | return 0; | |
dcc914f4 JP |
2448 | } |
2449 | ||
60041bcd PZ |
2450 | if (handle_insn_ops(insn, &state)) |
2451 | return 1; | |
2452 | ||
dcc914f4 JP |
2453 | switch (insn->type) { |
2454 | ||
dcc914f4 | 2455 | case INSN_RETURN: |
a92e92d1 | 2456 | return validate_return(func, insn, &state); |
dcc914f4 JP |
2457 | |
2458 | case INSN_CALL: | |
ea24213d PZ |
2459 | case INSN_CALL_DYNAMIC: |
2460 | ret = validate_call(insn, &state); | |
2461 | if (ret) | |
2462 | return ret; | |
dcc914f4 | 2463 | |
c9bab22b JP |
2464 | if (!no_fp && func && !is_fentry_call(insn) && |
2465 | !has_valid_stack_frame(&state)) { | |
dcc914f4 JP |
2466 | WARN_FUNC("call without frame pointer save/setup", |
2467 | sec, insn->offset); | |
2468 | return 1; | |
2469 | } | |
c9bab22b JP |
2470 | |
2471 | if (dead_end_function(file, insn->call_dest)) | |
2472 | return 0; | |
2473 | ||
1e7e4788 JP |
2474 | if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) { |
2475 | list_add_tail(&insn->static_call_node, | |
2476 | &file->static_call_list); | |
2477 | } | |
2478 | ||
dcc914f4 JP |
2479 | break; |
2480 | ||
2481 | case INSN_JUMP_CONDITIONAL: | |
2482 | case INSN_JUMP_UNCONDITIONAL: | |
0c1ddd33 | 2483 | if (func && is_sibling_call(insn)) { |
54262aa2 | 2484 | ret = validate_sibling_call(insn, &state); |
dcc914f4 | 2485 | if (ret) |
54262aa2 | 2486 | return ret; |
4855022a | 2487 | |
0c1ddd33 | 2488 | } else if (insn->jump_dest) { |
c705cecc JP |
2489 | ret = validate_branch(file, func, |
2490 | insn->jump_dest, state); | |
7697eee3 PZ |
2491 | if (ret) { |
2492 | if (backtrace) | |
2493 | BT_FUNC("(branch)", insn); | |
2494 | return ret; | |
2495 | } | |
4855022a | 2496 | } |
dcc914f4 JP |
2497 | |
2498 | if (insn->type == INSN_JUMP_UNCONDITIONAL) | |
2499 | return 0; | |
2500 | ||
2501 | break; | |
2502 | ||
2503 | case INSN_JUMP_DYNAMIC: | |
b68b9907 | 2504 | case INSN_JUMP_DYNAMIC_CONDITIONAL: |
0c1ddd33 | 2505 | if (func && is_sibling_call(insn)) { |
54262aa2 PZ |
2506 | ret = validate_sibling_call(insn, &state); |
2507 | if (ret) | |
2508 | return ret; | |
dcc914f4 JP |
2509 | } |
2510 | ||
b68b9907 JP |
2511 | if (insn->type == INSN_JUMP_DYNAMIC) |
2512 | return 0; | |
2513 | ||
2514 | break; | |
dcc914f4 | 2515 | |
39358a03 JP |
2516 | case INSN_CONTEXT_SWITCH: |
2517 | if (func && (!next_insn || !next_insn->hint)) { | |
2518 | WARN_FUNC("unsupported instruction in callable function", | |
2519 | sec, insn->offset); | |
2520 | return 1; | |
2521 | } | |
2522 | return 0; | |
2523 | ||
ea24213d PZ |
2524 | case INSN_STAC: |
2525 | if (state.uaccess) { | |
2526 | WARN_FUNC("recursive UACCESS enable", sec, insn->offset); | |
2527 | return 1; | |
2528 | } | |
2529 | ||
2530 | state.uaccess = true; | |
2531 | break; | |
2532 | ||
2533 | case INSN_CLAC: | |
c705cecc | 2534 | if (!state.uaccess && func) { |
ea24213d PZ |
2535 | WARN_FUNC("redundant UACCESS disable", sec, insn->offset); |
2536 | return 1; | |
2537 | } | |
2538 | ||
2539 | if (func_uaccess_safe(func) && !state.uaccess_stack) { | |
2540 | WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset); | |
2541 | return 1; | |
2542 | } | |
2543 | ||
2544 | state.uaccess = false; | |
baa41469 JP |
2545 | break; |
2546 | ||
2f0f9e9a PZ |
2547 | case INSN_STD: |
2548 | if (state.df) | |
2549 | WARN_FUNC("recursive STD", sec, insn->offset); | |
2550 | ||
2551 | state.df = true; | |
2552 | break; | |
2553 | ||
2554 | case INSN_CLD: | |
c705cecc | 2555 | if (!state.df && func) |
2f0f9e9a PZ |
2556 | WARN_FUNC("redundant CLD", sec, insn->offset); |
2557 | ||
2558 | state.df = false; | |
baa41469 JP |
2559 | break; |
2560 | ||
dcc914f4 JP |
2561 | default: |
2562 | break; | |
2563 | } | |
2564 | ||
2565 | if (insn->dead_end) | |
2566 | return 0; | |
2567 | ||
00d96180 | 2568 | if (!next_insn) { |
e7c0219b | 2569 | if (state.cfi.cfa.base == CFI_UNDEFINED) |
00d96180 | 2570 | return 0; |
dcc914f4 JP |
2571 | WARN("%s: unexpected end of section", sec->name); |
2572 | return 1; | |
2573 | } | |
00d96180 JP |
2574 | |
2575 | insn = next_insn; | |
dcc914f4 JP |
2576 | } |
2577 | ||
2578 | return 0; | |
2579 | } | |
2580 | ||
932f8e98 | 2581 | static int validate_unwind_hints(struct objtool_file *file, struct section *sec) |
39358a03 JP |
2582 | { |
2583 | struct instruction *insn; | |
39358a03 | 2584 | struct insn_state state; |
932f8e98 | 2585 | int ret, warnings = 0; |
39358a03 JP |
2586 | |
2587 | if (!file->hints) | |
2588 | return 0; | |
2589 | ||
932f8e98 | 2590 | init_insn_state(&state, sec); |
39358a03 | 2591 | |
932f8e98 PZ |
2592 | if (sec) { |
2593 | insn = find_insn(file, sec, 0); | |
2594 | if (!insn) | |
2595 | return 0; | |
2596 | } else { | |
2597 | insn = list_first_entry(&file->insn_list, typeof(*insn), list); | |
2598 | } | |
2599 | ||
2600 | while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) { | |
39358a03 | 2601 | if (insn->hint && !insn->visited) { |
c705cecc | 2602 | ret = validate_branch(file, insn->func, insn, state); |
7697eee3 PZ |
2603 | if (ret && backtrace) |
2604 | BT_FUNC("<=== (hint)", insn); | |
39358a03 JP |
2605 | warnings += ret; |
2606 | } | |
932f8e98 PZ |
2607 | |
2608 | insn = list_next_entry(insn, list); | |
39358a03 JP |
2609 | } |
2610 | ||
2611 | return warnings; | |
2612 | } | |
2613 | ||
b5bc2231 PZ |
2614 | static int validate_retpoline(struct objtool_file *file) |
2615 | { | |
2616 | struct instruction *insn; | |
2617 | int warnings = 0; | |
2618 | ||
2619 | for_each_insn(file, insn) { | |
2620 | if (insn->type != INSN_JUMP_DYNAMIC && | |
2621 | insn->type != INSN_CALL_DYNAMIC) | |
2622 | continue; | |
2623 | ||
2624 | if (insn->retpoline_safe) | |
2625 | continue; | |
2626 | ||
ca41b97e PZ |
2627 | /* |
2628 | * .init.text code is ran before userspace and thus doesn't | |
2629 | * strictly need retpolines, except for modules which are | |
2630 | * loaded late, they very much do need retpoline in their | |
2631 | * .init.text | |
2632 | */ | |
2633 | if (!strcmp(insn->sec->name, ".init.text") && !module) | |
2634 | continue; | |
2635 | ||
b5bc2231 PZ |
2636 | WARN_FUNC("indirect %s found in RETPOLINE build", |
2637 | insn->sec, insn->offset, | |
2638 | insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call"); | |
2639 | ||
2640 | warnings++; | |
2641 | } | |
2642 | ||
2643 | return warnings; | |
2644 | } | |
2645 | ||
dcc914f4 JP |
2646 | static bool is_kasan_insn(struct instruction *insn) |
2647 | { | |
2648 | return (insn->type == INSN_CALL && | |
2649 | !strcmp(insn->call_dest->name, "__asan_handle_no_return")); | |
2650 | } | |
2651 | ||
2652 | static bool is_ubsan_insn(struct instruction *insn) | |
2653 | { | |
2654 | return (insn->type == INSN_CALL && | |
2655 | !strcmp(insn->call_dest->name, | |
2656 | "__ubsan_handle_builtin_unreachable")); | |
2657 | } | |
2658 | ||
baa41469 | 2659 | static bool ignore_unreachable_insn(struct instruction *insn) |
dcc914f4 JP |
2660 | { |
2661 | int i; | |
2662 | ||
baa41469 JP |
2663 | if (insn->ignore || insn->type == INSN_NOP) |
2664 | return true; | |
2665 | ||
2666 | /* | |
2667 | * Ignore any unused exceptions. This can happen when a whitelisted | |
2668 | * function has an exception table entry. | |
0e2bb2bc JP |
2669 | * |
2670 | * Also ignore alternative replacement instructions. This can happen | |
2671 | * when a whitelisted function uses one of the ALTERNATIVE macros. | |
baa41469 | 2672 | */ |
0e2bb2bc JP |
2673 | if (!strcmp(insn->sec->name, ".fixup") || |
2674 | !strcmp(insn->sec->name, ".altinstr_replacement") || | |
2675 | !strcmp(insn->sec->name, ".altinstr_aux")) | |
dcc914f4 JP |
2676 | return true; |
2677 | ||
bd841d61 JP |
2678 | if (!insn->func) |
2679 | return false; | |
2680 | ||
2681 | /* | |
2682 | * CONFIG_UBSAN_TRAP inserts a UD2 when it sees | |
2683 | * __builtin_unreachable(). The BUG() macro has an unreachable() after | |
2684 | * the UD2, which causes GCC's undefined trap logic to emit another UD2 | |
2685 | * (or occasionally a JMP to UD2). | |
2686 | */ | |
2687 | if (list_prev_entry(insn, list)->dead_end && | |
2688 | (insn->type == INSN_BUG || | |
2689 | (insn->type == INSN_JUMP_UNCONDITIONAL && | |
2690 | insn->jump_dest && insn->jump_dest->type == INSN_BUG))) | |
2691 | return true; | |
2692 | ||
dcc914f4 JP |
2693 | /* |
2694 | * Check if this (or a subsequent) instruction is related to | |
2695 | * CONFIG_UBSAN or CONFIG_KASAN. | |
2696 | * | |
2697 | * End the search at 5 instructions to avoid going into the weeds. | |
2698 | */ | |
2699 | for (i = 0; i < 5; i++) { | |
2700 | ||
2701 | if (is_kasan_insn(insn) || is_ubsan_insn(insn)) | |
2702 | return true; | |
2703 | ||
fe24e271 JP |
2704 | if (insn->type == INSN_JUMP_UNCONDITIONAL) { |
2705 | if (insn->jump_dest && | |
2706 | insn->jump_dest->func == insn->func) { | |
2707 | insn = insn->jump_dest; | |
2708 | continue; | |
2709 | } | |
2710 | ||
2711 | break; | |
dcc914f4 JP |
2712 | } |
2713 | ||
baa41469 | 2714 | if (insn->offset + insn->len >= insn->func->offset + insn->func->len) |
dcc914f4 | 2715 | break; |
fe24e271 | 2716 | |
dcc914f4 JP |
2717 | insn = list_next_entry(insn, list); |
2718 | } | |
2719 | ||
2720 | return false; | |
2721 | } | |
2722 | ||
4b5e2e7f PZ |
2723 | static int validate_symbol(struct objtool_file *file, struct section *sec, |
2724 | struct symbol *sym, struct insn_state *state) | |
dcc914f4 | 2725 | { |
dcc914f4 | 2726 | struct instruction *insn; |
4b5e2e7f PZ |
2727 | int ret; |
2728 | ||
2729 | if (!sym->len) { | |
2730 | WARN("%s() is missing an ELF size annotation", sym->name); | |
2731 | return 1; | |
2732 | } | |
2733 | ||
2734 | if (sym->pfunc != sym || sym->alias != sym) | |
2735 | return 0; | |
2736 | ||
2737 | insn = find_insn(file, sec, sym->offset); | |
2738 | if (!insn || insn->ignore || insn->visited) | |
2739 | return 0; | |
2740 | ||
2741 | state->uaccess = sym->uaccess_safe; | |
2742 | ||
2743 | ret = validate_branch(file, insn->func, insn, *state); | |
2744 | if (ret && backtrace) | |
2745 | BT_FUNC("<=== (sym)", insn); | |
2746 | return ret; | |
2747 | } | |
2748 | ||
2749 | static int validate_section(struct objtool_file *file, struct section *sec) | |
2750 | { | |
baa41469 | 2751 | struct insn_state state; |
4b5e2e7f PZ |
2752 | struct symbol *func; |
2753 | int warnings = 0; | |
dcc914f4 | 2754 | |
350994bf PZ |
2755 | list_for_each_entry(func, &sec->symbol_list, list) { |
2756 | if (func->type != STT_FUNC) | |
2757 | continue; | |
e10cd8fe | 2758 | |
932f8e98 | 2759 | init_insn_state(&state, sec); |
e7c0219b PZ |
2760 | state.cfi.cfa = initial_func_cfi.cfa; |
2761 | memcpy(&state.cfi.regs, &initial_func_cfi.regs, | |
0699e551 | 2762 | CFI_NUM_REGS * sizeof(struct cfi_reg)); |
e7c0219b | 2763 | state.cfi.stack_size = initial_func_cfi.cfa.offset; |
0699e551 | 2764 | |
4b5e2e7f | 2765 | warnings += validate_symbol(file, sec, func, &state); |
dcc914f4 JP |
2766 | } |
2767 | ||
dcc914f4 JP |
2768 | return warnings; |
2769 | } | |
2770 | ||
c4a33939 PZ |
2771 | static int validate_vmlinux_functions(struct objtool_file *file) |
2772 | { | |
2773 | struct section *sec; | |
932f8e98 | 2774 | int warnings = 0; |
c4a33939 PZ |
2775 | |
2776 | sec = find_section_by_name(file->elf, ".noinstr.text"); | |
0cc9ac8d TG |
2777 | if (sec) { |
2778 | warnings += validate_section(file, sec); | |
2779 | warnings += validate_unwind_hints(file, sec); | |
2780 | } | |
c4a33939 | 2781 | |
0cc9ac8d TG |
2782 | sec = find_section_by_name(file->elf, ".entry.text"); |
2783 | if (sec) { | |
2784 | warnings += validate_section(file, sec); | |
2785 | warnings += validate_unwind_hints(file, sec); | |
2786 | } | |
932f8e98 PZ |
2787 | |
2788 | return warnings; | |
c4a33939 PZ |
2789 | } |
2790 | ||
350994bf PZ |
2791 | static int validate_functions(struct objtool_file *file) |
2792 | { | |
2793 | struct section *sec; | |
2794 | int warnings = 0; | |
2795 | ||
da837bd6 PZ |
2796 | for_each_sec(file, sec) { |
2797 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) | |
2798 | continue; | |
2799 | ||
350994bf | 2800 | warnings += validate_section(file, sec); |
da837bd6 | 2801 | } |
350994bf PZ |
2802 | |
2803 | return warnings; | |
2804 | } | |
2805 | ||
baa41469 | 2806 | static int validate_reachable_instructions(struct objtool_file *file) |
dcc914f4 JP |
2807 | { |
2808 | struct instruction *insn; | |
baa41469 JP |
2809 | |
2810 | if (file->ignore_unreachables) | |
2811 | return 0; | |
dcc914f4 JP |
2812 | |
2813 | for_each_insn(file, insn) { | |
baa41469 JP |
2814 | if (insn->visited || ignore_unreachable_insn(insn)) |
2815 | continue; | |
2816 | ||
baa41469 JP |
2817 | WARN_FUNC("unreachable instruction", insn->sec, insn->offset); |
2818 | return 1; | |
dcc914f4 JP |
2819 | } |
2820 | ||
baa41469 | 2821 | return 0; |
dcc914f4 JP |
2822 | } |
2823 | ||
d44becb9 | 2824 | int check(struct objtool_file *file) |
dcc914f4 | 2825 | { |
dcc914f4 JP |
2826 | int ret, warnings = 0; |
2827 | ||
baa41469 JP |
2828 | arch_initial_func_cfi_state(&initial_func_cfi); |
2829 | ||
6545eb03 | 2830 | ret = decode_sections(file); |
dcc914f4 JP |
2831 | if (ret < 0) |
2832 | goto out; | |
2833 | warnings += ret; | |
2834 | ||
6545eb03 | 2835 | if (list_empty(&file->insn_list)) |
dcc914f4 | 2836 | goto out; |
dcc914f4 | 2837 | |
c4a33939 | 2838 | if (vmlinux && !validate_dup) { |
6545eb03 | 2839 | ret = validate_vmlinux_functions(file); |
c4a33939 PZ |
2840 | if (ret < 0) |
2841 | goto out; | |
2842 | ||
2843 | warnings += ret; | |
2844 | goto out; | |
2845 | } | |
2846 | ||
b5bc2231 | 2847 | if (retpoline) { |
6545eb03 | 2848 | ret = validate_retpoline(file); |
b5bc2231 PZ |
2849 | if (ret < 0) |
2850 | return ret; | |
2851 | warnings += ret; | |
2852 | } | |
2853 | ||
6545eb03 | 2854 | ret = validate_functions(file); |
dcc914f4 JP |
2855 | if (ret < 0) |
2856 | goto out; | |
2857 | warnings += ret; | |
2858 | ||
6545eb03 | 2859 | ret = validate_unwind_hints(file, NULL); |
39358a03 JP |
2860 | if (ret < 0) |
2861 | goto out; | |
2862 | warnings += ret; | |
2863 | ||
baa41469 | 2864 | if (!warnings) { |
6545eb03 | 2865 | ret = validate_reachable_instructions(file); |
baa41469 JP |
2866 | if (ret < 0) |
2867 | goto out; | |
2868 | warnings += ret; | |
2869 | } | |
2870 | ||
6545eb03 | 2871 | ret = create_static_call_sections(file); |
1e7e4788 JP |
2872 | if (ret < 0) |
2873 | goto out; | |
2874 | warnings += ret; | |
2875 | ||
dcc914f4 | 2876 | out: |
644592d3 JP |
2877 | if (ret < 0) { |
2878 | /* | |
2879 | * Fatal error. The binary is corrupt or otherwise broken in | |
2880 | * some way, or objtool itself is broken. Fail the kernel | |
2881 | * build. | |
2882 | */ | |
2883 | return ret; | |
2884 | } | |
2885 | ||
dcc914f4 JP |
2886 | return 0; |
2887 | } |