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