Commit | Line | Data |
---|---|---|
442f04c3 JP |
1 | /* |
2 | * Copyright (C) 2015 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 | /* | |
19 | * objtool check: | |
20 | * | |
21 | * This command analyzes every .o file and ensures the validity of its stack | |
22 | * trace metadata. It enforces a set of rules on asm code and C inline | |
23 | * assembly code so that stack traces can be reliable. | |
24 | * | |
25 | * For more information, see tools/objtool/Documentation/stack-validation.txt. | |
26 | */ | |
27 | ||
28 | #include <string.h> | |
29 | #include <subcmd/parse-options.h> | |
30 | ||
31 | #include "builtin.h" | |
32 | #include "elf.h" | |
33 | #include "special.h" | |
34 | #include "arch.h" | |
35 | #include "warn.h" | |
36 | ||
37 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | |
38 | ||
39 | #define STATE_FP_SAVED 0x1 | |
40 | #define STATE_FP_SETUP 0x2 | |
41 | #define STATE_FENTRY 0x4 | |
42 | ||
43 | struct instruction { | |
44 | struct list_head list; | |
45 | struct section *sec; | |
46 | unsigned long offset; | |
47 | unsigned int len, state; | |
48 | unsigned char type; | |
49 | unsigned long immediate; | |
50 | bool alt_group, visited; | |
51 | struct symbol *call_dest; | |
52 | struct instruction *jump_dest; | |
53 | struct list_head alts; | |
54 | }; | |
55 | ||
56 | struct alternative { | |
57 | struct list_head list; | |
58 | struct instruction *insn; | |
59 | }; | |
60 | ||
61 | struct objtool_file { | |
62 | struct elf *elf; | |
a196e171 | 63 | struct list_head insn_list; |
442f04c3 JP |
64 | }; |
65 | ||
66 | const char *objname; | |
67 | static bool nofp; | |
68 | ||
74aec058 JP |
69 | static struct instruction *find_insn(struct objtool_file *file, |
70 | struct section *sec, unsigned long offset) | |
442f04c3 JP |
71 | { |
72 | struct instruction *insn; | |
73 | ||
a196e171 | 74 | list_for_each_entry(insn, &file->insn_list, list) |
442f04c3 JP |
75 | if (insn->sec == sec && insn->offset == offset) |
76 | return insn; | |
77 | ||
78 | return NULL; | |
79 | } | |
80 | ||
74aec058 JP |
81 | static struct instruction *next_insn_same_sec(struct objtool_file *file, |
82 | struct instruction *insn) | |
83 | { | |
84 | struct instruction *next = list_next_entry(insn, list); | |
85 | ||
a196e171 | 86 | if (&next->list == &file->insn_list || next->sec != insn->sec) |
74aec058 JP |
87 | return NULL; |
88 | ||
89 | return next; | |
90 | } | |
91 | ||
92 | #define for_each_insn(file, insn) \ | |
a196e171 | 93 | list_for_each_entry(insn, &file->insn_list, list) |
74aec058 JP |
94 | |
95 | #define func_for_each_insn(file, func, insn) \ | |
96 | for (insn = find_insn(file, func->sec, func->offset); \ | |
a196e171 | 97 | insn && &insn->list != &file->insn_list && \ |
74aec058 JP |
98 | insn->sec == func->sec && \ |
99 | insn->offset < func->offset + func->len; \ | |
100 | insn = list_next_entry(insn, list)) | |
101 | ||
102 | #define sec_for_each_insn_from(file, insn) \ | |
103 | for (; insn; insn = next_insn_same_sec(file, insn)) | |
104 | ||
105 | ||
442f04c3 JP |
106 | /* |
107 | * Check if the function has been manually whitelisted with the | |
108 | * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted | |
109 | * due to its use of a context switching instruction. | |
110 | */ | |
111 | static bool ignore_func(struct objtool_file *file, struct symbol *func) | |
112 | { | |
113 | struct section *macro_sec; | |
114 | struct rela *rela; | |
115 | struct instruction *insn; | |
116 | ||
117 | /* check for STACK_FRAME_NON_STANDARD */ | |
118 | macro_sec = find_section_by_name(file->elf, "__func_stack_frame_non_standard"); | |
119 | if (macro_sec && macro_sec->rela) | |
a196e171 | 120 | list_for_each_entry(rela, ¯o_sec->rela->rela_list, list) |
442f04c3 JP |
121 | if (rela->sym->sec == func->sec && |
122 | rela->addend == func->offset) | |
123 | return true; | |
124 | ||
125 | /* check if it has a context switching instruction */ | |
74aec058 | 126 | func_for_each_insn(file, func, insn) |
442f04c3 JP |
127 | if (insn->type == INSN_CONTEXT_SWITCH) |
128 | return true; | |
442f04c3 JP |
129 | |
130 | return false; | |
131 | } | |
132 | ||
133 | /* | |
134 | * This checks to see if the given function is a "noreturn" function. | |
135 | * | |
136 | * For global functions which are outside the scope of this object file, we | |
137 | * have to keep a manual list of them. | |
138 | * | |
139 | * For local functions, we have to detect them manually by simply looking for | |
140 | * the lack of a return instruction. | |
b1e03249 JP |
141 | * |
142 | * Returns: | |
143 | * -1: error | |
144 | * 0: no dead end | |
145 | * 1: dead end | |
442f04c3 | 146 | */ |
b1e03249 JP |
147 | static int __dead_end_function(struct objtool_file *file, struct symbol *func, |
148 | int recursion) | |
442f04c3 JP |
149 | { |
150 | int i; | |
74aec058 | 151 | struct instruction *insn; |
442f04c3 JP |
152 | bool empty = true; |
153 | ||
154 | /* | |
155 | * Unfortunately these have to be hard coded because the noreturn | |
156 | * attribute isn't provided in ELF data. | |
157 | */ | |
158 | static const char * const global_noreturns[] = { | |
159 | "__stack_chk_fail", | |
160 | "panic", | |
161 | "do_exit", | |
162 | "__module_put_and_exit", | |
163 | "complete_and_exit", | |
164 | "kvm_spurious_fault", | |
165 | "__reiserfs_panic", | |
166 | "lbug_with_loc" | |
167 | }; | |
168 | ||
169 | if (func->bind == STB_WEAK) | |
b1e03249 | 170 | return 0; |
442f04c3 JP |
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])) | |
b1e03249 | 175 | return 1; |
442f04c3 JP |
176 | |
177 | if (!func->sec) | |
b1e03249 | 178 | return 0; |
442f04c3 | 179 | |
74aec058 | 180 | func_for_each_insn(file, func, insn) { |
442f04c3 JP |
181 | empty = false; |
182 | ||
183 | if (insn->type == INSN_RETURN) | |
b1e03249 | 184 | return 0; |
81bfafca JP |
185 | } |
186 | ||
187 | if (empty) | |
b1e03249 | 188 | return 0; |
81bfafca JP |
189 | |
190 | /* | |
191 | * A function can have a sibling call instead of a return. In that | |
192 | * case, the function's dead-end status depends on whether the target | |
193 | * of the sibling call returns. | |
194 | */ | |
74aec058 | 195 | func_for_each_insn(file, func, insn) { |
81bfafca JP |
196 | if (insn->sec != func->sec || |
197 | insn->offset >= func->offset + func->len) | |
198 | break; | |
442f04c3 JP |
199 | |
200 | if (insn->type == INSN_JUMP_UNCONDITIONAL) { | |
201 | struct instruction *dest = insn->jump_dest; | |
202 | struct symbol *dest_func; | |
203 | ||
204 | if (!dest) | |
205 | /* sibling call to another file */ | |
b1e03249 | 206 | return 0; |
442f04c3 JP |
207 | |
208 | if (dest->sec != func->sec || | |
209 | dest->offset < func->offset || | |
210 | dest->offset >= func->offset + func->len) { | |
211 | /* local sibling call */ | |
212 | dest_func = find_symbol_by_offset(dest->sec, | |
213 | dest->offset); | |
214 | if (!dest_func) | |
215 | continue; | |
216 | ||
b1e03249 JP |
217 | if (recursion == 5) { |
218 | WARN_FUNC("infinite recursion (objtool bug!)", | |
219 | dest->sec, dest->offset); | |
220 | return -1; | |
221 | } | |
222 | ||
223 | return __dead_end_function(file, dest_func, | |
224 | recursion + 1); | |
442f04c3 JP |
225 | } |
226 | } | |
227 | ||
228 | if (insn->type == INSN_JUMP_DYNAMIC) | |
229 | /* sibling call */ | |
b1e03249 | 230 | return 0; |
442f04c3 JP |
231 | } |
232 | ||
b1e03249 JP |
233 | return 1; |
234 | } | |
235 | ||
236 | static int dead_end_function(struct objtool_file *file, struct symbol *func) | |
237 | { | |
238 | return __dead_end_function(file, func, 0); | |
442f04c3 JP |
239 | } |
240 | ||
241 | /* | |
242 | * Call the arch-specific instruction decoder for all the instructions and add | |
a196e171 | 243 | * them to the global instruction list. |
442f04c3 JP |
244 | */ |
245 | static int decode_instructions(struct objtool_file *file) | |
246 | { | |
247 | struct section *sec; | |
248 | unsigned long offset; | |
249 | struct instruction *insn; | |
250 | int ret; | |
251 | ||
442f04c3 JP |
252 | list_for_each_entry(sec, &file->elf->sections, list) { |
253 | ||
254 | if (!(sec->sh.sh_flags & SHF_EXECINSTR)) | |
255 | continue; | |
256 | ||
257 | for (offset = 0; offset < sec->len; offset += insn->len) { | |
258 | insn = malloc(sizeof(*insn)); | |
259 | memset(insn, 0, sizeof(*insn)); | |
260 | ||
261 | INIT_LIST_HEAD(&insn->alts); | |
262 | insn->sec = sec; | |
263 | insn->offset = offset; | |
264 | ||
265 | ret = arch_decode_instruction(file->elf, sec, offset, | |
266 | sec->len - offset, | |
267 | &insn->len, &insn->type, | |
268 | &insn->immediate); | |
269 | if (ret) | |
270 | return ret; | |
271 | ||
272 | if (!insn->type || insn->type > INSN_LAST) { | |
273 | WARN_FUNC("invalid instruction type %d", | |
274 | insn->sec, insn->offset, insn->type); | |
275 | return -1; | |
276 | } | |
277 | ||
a196e171 | 278 | list_add_tail(&insn->list, &file->insn_list); |
442f04c3 JP |
279 | } |
280 | } | |
281 | ||
282 | return 0; | |
283 | } | |
284 | ||
285 | /* | |
286 | * Warnings shouldn't be reported for ignored functions. | |
287 | */ | |
a196e171 | 288 | static void add_ignores(struct objtool_file *file) |
442f04c3 JP |
289 | { |
290 | struct instruction *insn; | |
291 | struct section *sec; | |
292 | struct symbol *func; | |
293 | ||
294 | list_for_each_entry(sec, &file->elf->sections, list) { | |
a196e171 | 295 | list_for_each_entry(func, &sec->symbol_list, list) { |
442f04c3 JP |
296 | if (func->type != STT_FUNC) |
297 | continue; | |
298 | ||
299 | if (!ignore_func(file, func)) | |
300 | continue; | |
301 | ||
74aec058 | 302 | func_for_each_insn(file, func, insn) |
442f04c3 | 303 | insn->visited = true; |
442f04c3 JP |
304 | } |
305 | } | |
306 | } | |
307 | ||
308 | /* | |
309 | * Find the destination instructions for all jumps. | |
310 | */ | |
a196e171 | 311 | static int add_jump_destinations(struct objtool_file *file) |
442f04c3 JP |
312 | { |
313 | struct instruction *insn; | |
314 | struct rela *rela; | |
315 | struct section *dest_sec; | |
316 | unsigned long dest_off; | |
317 | ||
74aec058 | 318 | for_each_insn(file, insn) { |
442f04c3 JP |
319 | if (insn->type != INSN_JUMP_CONDITIONAL && |
320 | insn->type != INSN_JUMP_UNCONDITIONAL) | |
321 | continue; | |
322 | ||
323 | /* skip ignores */ | |
324 | if (insn->visited) | |
325 | continue; | |
326 | ||
327 | rela = find_rela_by_dest_range(insn->sec, insn->offset, | |
328 | insn->len); | |
329 | if (!rela) { | |
330 | dest_sec = insn->sec; | |
331 | dest_off = insn->offset + insn->len + insn->immediate; | |
332 | } else if (rela->sym->type == STT_SECTION) { | |
333 | dest_sec = rela->sym->sec; | |
334 | dest_off = rela->addend + 4; | |
335 | } else if (rela->sym->sec->idx) { | |
336 | dest_sec = rela->sym->sec; | |
337 | dest_off = rela->sym->sym.st_value + rela->addend + 4; | |
338 | } else { | |
339 | /* sibling call */ | |
340 | insn->jump_dest = 0; | |
341 | continue; | |
342 | } | |
343 | ||
74aec058 | 344 | insn->jump_dest = find_insn(file, dest_sec, dest_off); |
442f04c3 JP |
345 | if (!insn->jump_dest) { |
346 | ||
347 | /* | |
348 | * This is a special case where an alt instruction | |
349 | * jumps past the end of the section. These are | |
350 | * handled later in handle_group_alt(). | |
351 | */ | |
352 | if (!strcmp(insn->sec->name, ".altinstr_replacement")) | |
353 | continue; | |
354 | ||
355 | WARN_FUNC("can't find jump dest instruction at %s+0x%lx", | |
356 | insn->sec, insn->offset, dest_sec->name, | |
357 | dest_off); | |
358 | return -1; | |
359 | } | |
360 | } | |
361 | ||
362 | return 0; | |
363 | } | |
364 | ||
365 | /* | |
366 | * Find the destination instructions for all calls. | |
367 | */ | |
a196e171 | 368 | static int add_call_destinations(struct objtool_file *file) |
442f04c3 JP |
369 | { |
370 | struct instruction *insn; | |
371 | unsigned long dest_off; | |
372 | struct rela *rela; | |
373 | ||
74aec058 | 374 | for_each_insn(file, insn) { |
442f04c3 JP |
375 | if (insn->type != INSN_CALL) |
376 | continue; | |
377 | ||
378 | rela = find_rela_by_dest_range(insn->sec, insn->offset, | |
379 | insn->len); | |
380 | if (!rela) { | |
381 | dest_off = insn->offset + insn->len + insn->immediate; | |
382 | insn->call_dest = find_symbol_by_offset(insn->sec, | |
383 | dest_off); | |
384 | if (!insn->call_dest) { | |
385 | WARN_FUNC("can't find call dest symbol at offset 0x%lx", | |
386 | insn->sec, insn->offset, dest_off); | |
387 | return -1; | |
388 | } | |
389 | } else if (rela->sym->type == STT_SECTION) { | |
390 | insn->call_dest = find_symbol_by_offset(rela->sym->sec, | |
391 | rela->addend+4); | |
392 | if (!insn->call_dest || | |
393 | insn->call_dest->type != STT_FUNC) { | |
394 | WARN_FUNC("can't find call dest symbol at %s+0x%x", | |
395 | insn->sec, insn->offset, | |
396 | rela->sym->sec->name, | |
397 | rela->addend + 4); | |
398 | return -1; | |
399 | } | |
400 | } else | |
401 | insn->call_dest = rela->sym; | |
402 | } | |
403 | ||
404 | return 0; | |
405 | } | |
406 | ||
407 | /* | |
408 | * The .alternatives section requires some extra special care, over and above | |
409 | * what other special sections require: | |
410 | * | |
411 | * 1. Because alternatives are patched in-place, we need to insert a fake jump | |
412 | * instruction at the end so that validate_branch() skips all the original | |
413 | * replaced instructions when validating the new instruction path. | |
414 | * | |
415 | * 2. An added wrinkle is that the new instruction length might be zero. In | |
416 | * that case the old instructions are replaced with noops. We simulate that | |
417 | * by creating a fake jump as the only new instruction. | |
418 | * | |
419 | * 3. In some cases, the alternative section includes an instruction which | |
420 | * conditionally jumps to the _end_ of the entry. We have to modify these | |
421 | * jumps' destinations to point back to .text rather than the end of the | |
422 | * entry in .altinstr_replacement. | |
423 | * | |
424 | * 4. It has been requested that we don't validate the !POPCNT feature path | |
425 | * which is a "very very small percentage of machines". | |
426 | */ | |
427 | static int handle_group_alt(struct objtool_file *file, | |
428 | struct special_alt *special_alt, | |
429 | struct instruction *orig_insn, | |
430 | struct instruction **new_insn) | |
431 | { | |
432 | struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump; | |
433 | unsigned long dest_off; | |
434 | ||
435 | last_orig_insn = NULL; | |
436 | insn = orig_insn; | |
74aec058 JP |
437 | sec_for_each_insn_from(file, insn) { |
438 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) | |
442f04c3 JP |
439 | break; |
440 | ||
441 | if (special_alt->skip_orig) | |
442 | insn->type = INSN_NOP; | |
443 | ||
444 | insn->alt_group = true; | |
445 | last_orig_insn = insn; | |
446 | } | |
447 | ||
74aec058 | 448 | if (!next_insn_same_sec(file, last_orig_insn)) { |
442f04c3 JP |
449 | WARN("%s: don't know how to handle alternatives at end of section", |
450 | special_alt->orig_sec->name); | |
451 | return -1; | |
452 | } | |
453 | ||
454 | fake_jump = malloc(sizeof(*fake_jump)); | |
455 | if (!fake_jump) { | |
456 | WARN("malloc failed"); | |
457 | return -1; | |
458 | } | |
459 | memset(fake_jump, 0, sizeof(*fake_jump)); | |
460 | INIT_LIST_HEAD(&fake_jump->alts); | |
461 | fake_jump->sec = special_alt->new_sec; | |
462 | fake_jump->offset = -1; | |
463 | fake_jump->type = INSN_JUMP_UNCONDITIONAL; | |
464 | fake_jump->jump_dest = list_next_entry(last_orig_insn, list); | |
465 | ||
466 | if (!special_alt->new_len) { | |
467 | *new_insn = fake_jump; | |
468 | return 0; | |
469 | } | |
470 | ||
471 | last_new_insn = NULL; | |
472 | insn = *new_insn; | |
74aec058 JP |
473 | sec_for_each_insn_from(file, insn) { |
474 | if (insn->offset >= special_alt->new_off + special_alt->new_len) | |
442f04c3 JP |
475 | break; |
476 | ||
477 | last_new_insn = insn; | |
478 | ||
479 | if (insn->type != INSN_JUMP_CONDITIONAL && | |
480 | insn->type != INSN_JUMP_UNCONDITIONAL) | |
481 | continue; | |
482 | ||
483 | if (!insn->immediate) | |
484 | continue; | |
485 | ||
486 | dest_off = insn->offset + insn->len + insn->immediate; | |
487 | if (dest_off == special_alt->new_off + special_alt->new_len) | |
488 | insn->jump_dest = fake_jump; | |
489 | ||
490 | if (!insn->jump_dest) { | |
491 | WARN_FUNC("can't find alternative jump destination", | |
492 | insn->sec, insn->offset); | |
493 | return -1; | |
494 | } | |
495 | } | |
496 | ||
497 | if (!last_new_insn) { | |
498 | WARN_FUNC("can't find last new alternative instruction", | |
499 | special_alt->new_sec, special_alt->new_off); | |
500 | return -1; | |
501 | } | |
502 | ||
503 | list_add(&fake_jump->list, &last_new_insn->list); | |
504 | ||
505 | return 0; | |
506 | } | |
507 | ||
508 | /* | |
509 | * A jump table entry can either convert a nop to a jump or a jump to a nop. | |
510 | * If the original instruction is a jump, make the alt entry an effective nop | |
511 | * by just skipping the original instruction. | |
512 | */ | |
513 | static int handle_jump_alt(struct objtool_file *file, | |
514 | struct special_alt *special_alt, | |
515 | struct instruction *orig_insn, | |
516 | struct instruction **new_insn) | |
517 | { | |
518 | if (orig_insn->type == INSN_NOP) | |
519 | return 0; | |
520 | ||
521 | if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { | |
522 | WARN_FUNC("unsupported instruction at jump label", | |
523 | orig_insn->sec, orig_insn->offset); | |
524 | return -1; | |
525 | } | |
526 | ||
527 | *new_insn = list_next_entry(orig_insn, list); | |
528 | return 0; | |
529 | } | |
530 | ||
531 | /* | |
532 | * Read all the special sections which have alternate instructions which can be | |
533 | * patched in or redirected to at runtime. Each instruction having alternate | |
534 | * instruction(s) has them added to its insn->alts list, which will be | |
535 | * traversed in validate_branch(). | |
536 | */ | |
a196e171 | 537 | static int add_special_section_alts(struct objtool_file *file) |
442f04c3 JP |
538 | { |
539 | struct list_head special_alts; | |
540 | struct instruction *orig_insn, *new_insn; | |
541 | struct special_alt *special_alt, *tmp; | |
542 | struct alternative *alt; | |
543 | int ret; | |
544 | ||
545 | ret = special_get_alts(file->elf, &special_alts); | |
546 | if (ret) | |
547 | return ret; | |
548 | ||
549 | list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { | |
550 | alt = malloc(sizeof(*alt)); | |
551 | if (!alt) { | |
552 | WARN("malloc failed"); | |
553 | ret = -1; | |
554 | goto out; | |
555 | } | |
556 | ||
74aec058 JP |
557 | orig_insn = find_insn(file, special_alt->orig_sec, |
558 | special_alt->orig_off); | |
442f04c3 JP |
559 | if (!orig_insn) { |
560 | WARN_FUNC("special: can't find orig instruction", | |
561 | special_alt->orig_sec, special_alt->orig_off); | |
562 | ret = -1; | |
563 | goto out; | |
564 | } | |
565 | ||
566 | new_insn = NULL; | |
567 | if (!special_alt->group || special_alt->new_len) { | |
74aec058 JP |
568 | new_insn = find_insn(file, special_alt->new_sec, |
569 | special_alt->new_off); | |
442f04c3 JP |
570 | if (!new_insn) { |
571 | WARN_FUNC("special: can't find new instruction", | |
572 | special_alt->new_sec, | |
573 | special_alt->new_off); | |
574 | ret = -1; | |
575 | goto out; | |
576 | } | |
577 | } | |
578 | ||
579 | if (special_alt->group) { | |
580 | ret = handle_group_alt(file, special_alt, orig_insn, | |
581 | &new_insn); | |
582 | if (ret) | |
583 | goto out; | |
584 | } else if (special_alt->jump_or_nop) { | |
585 | ret = handle_jump_alt(file, special_alt, orig_insn, | |
586 | &new_insn); | |
587 | if (ret) | |
588 | goto out; | |
589 | } | |
590 | ||
591 | alt->insn = new_insn; | |
592 | list_add_tail(&alt->list, &orig_insn->alts); | |
593 | ||
594 | list_del(&special_alt->list); | |
595 | free(special_alt); | |
596 | } | |
597 | ||
598 | out: | |
599 | return ret; | |
600 | } | |
601 | ||
602 | /* | |
603 | * For some switch statements, gcc generates a jump table in the .rodata | |
604 | * section which contains a list of addresses within the function to jump to. | |
605 | * This finds these jump tables and adds them to the insn->alts lists. | |
606 | */ | |
a196e171 | 607 | static int add_switch_table_alts(struct objtool_file *file) |
442f04c3 JP |
608 | { |
609 | struct instruction *insn, *alt_insn; | |
a196e171 | 610 | struct rela *rodata_rela, *text_rela; |
442f04c3 JP |
611 | struct section *rodata; |
612 | struct symbol *func; | |
613 | struct alternative *alt; | |
614 | ||
74aec058 | 615 | for_each_insn(file, insn) { |
442f04c3 JP |
616 | if (insn->type != INSN_JUMP_DYNAMIC) |
617 | continue; | |
618 | ||
a196e171 JP |
619 | text_rela = find_rela_by_dest_range(insn->sec, insn->offset, |
620 | insn->len); | |
621 | if (!text_rela || strcmp(text_rela->sym->name, ".rodata")) | |
442f04c3 JP |
622 | continue; |
623 | ||
624 | rodata = find_section_by_name(file->elf, ".rodata"); | |
625 | if (!rodata || !rodata->rela) | |
626 | continue; | |
627 | ||
628 | /* common case: jmpq *[addr](,%rax,8) */ | |
a196e171 | 629 | rodata_rela = find_rela_by_dest(rodata, text_rela->addend); |
442f04c3 JP |
630 | |
631 | /* rare case: jmpq *[addr](%rip) */ | |
a196e171 JP |
632 | if (!rodata_rela) |
633 | rodata_rela = find_rela_by_dest(rodata, | |
634 | text_rela->addend + 4); | |
635 | if (!rodata_rela) | |
442f04c3 JP |
636 | continue; |
637 | ||
638 | func = find_containing_func(insn->sec, insn->offset); | |
639 | if (!func) { | |
640 | WARN_FUNC("can't find containing func", | |
641 | insn->sec, insn->offset); | |
642 | return -1; | |
643 | } | |
644 | ||
a196e171 JP |
645 | list_for_each_entry_from(rodata_rela, &rodata->rela->rela_list, |
646 | list) { | |
647 | if (rodata_rela->sym->sec != insn->sec || | |
648 | rodata_rela->addend <= func->offset || | |
649 | rodata_rela->addend >= func->offset + func->len) | |
442f04c3 JP |
650 | break; |
651 | ||
a196e171 JP |
652 | alt_insn = find_insn(file, insn->sec, |
653 | rodata_rela->addend); | |
442f04c3 JP |
654 | if (!alt_insn) { |
655 | WARN("%s: can't find instruction at %s+0x%x", | |
656 | rodata->rela->name, insn->sec->name, | |
a196e171 | 657 | rodata_rela->addend); |
442f04c3 JP |
658 | return -1; |
659 | } | |
660 | ||
661 | alt = malloc(sizeof(*alt)); | |
662 | if (!alt) { | |
663 | WARN("malloc failed"); | |
664 | return -1; | |
665 | } | |
666 | ||
667 | alt->insn = alt_insn; | |
668 | list_add_tail(&alt->list, &insn->alts); | |
669 | } | |
670 | } | |
671 | ||
672 | return 0; | |
673 | } | |
674 | ||
675 | static int decode_sections(struct objtool_file *file) | |
676 | { | |
677 | int ret; | |
678 | ||
679 | ret = decode_instructions(file); | |
680 | if (ret) | |
681 | return ret; | |
682 | ||
a196e171 | 683 | add_ignores(file); |
442f04c3 | 684 | |
a196e171 | 685 | ret = add_jump_destinations(file); |
442f04c3 JP |
686 | if (ret) |
687 | return ret; | |
688 | ||
a196e171 | 689 | ret = add_call_destinations(file); |
442f04c3 JP |
690 | if (ret) |
691 | return ret; | |
692 | ||
a196e171 | 693 | ret = add_special_section_alts(file); |
442f04c3 JP |
694 | if (ret) |
695 | return ret; | |
696 | ||
a196e171 | 697 | ret = add_switch_table_alts(file); |
442f04c3 JP |
698 | if (ret) |
699 | return ret; | |
700 | ||
701 | return 0; | |
702 | } | |
703 | ||
704 | static bool is_fentry_call(struct instruction *insn) | |
705 | { | |
706 | if (insn->type == INSN_CALL && | |
707 | insn->call_dest->type == STT_NOTYPE && | |
708 | !strcmp(insn->call_dest->name, "__fentry__")) | |
709 | return true; | |
710 | ||
711 | return false; | |
712 | } | |
713 | ||
714 | static bool has_modified_stack_frame(struct instruction *insn) | |
715 | { | |
716 | return (insn->state & STATE_FP_SAVED) || | |
717 | (insn->state & STATE_FP_SETUP); | |
718 | } | |
719 | ||
720 | static bool has_valid_stack_frame(struct instruction *insn) | |
721 | { | |
722 | return (insn->state & STATE_FP_SAVED) && | |
723 | (insn->state & STATE_FP_SETUP); | |
724 | } | |
725 | ||
d8d1b2cb JP |
726 | static unsigned int frame_state(unsigned long state) |
727 | { | |
728 | return (state & (STATE_FP_SAVED | STATE_FP_SETUP)); | |
729 | } | |
730 | ||
442f04c3 JP |
731 | /* |
732 | * Follow the branch starting at the given instruction, and recursively follow | |
733 | * any other branches (jumps). Meanwhile, track the frame pointer state at | |
734 | * each instruction and validate all the rules described in | |
735 | * tools/objtool/Documentation/stack-validation.txt. | |
736 | */ | |
737 | static int validate_branch(struct objtool_file *file, | |
738 | struct instruction *first, unsigned char first_state) | |
739 | { | |
740 | struct alternative *alt; | |
741 | struct instruction *insn; | |
742 | struct section *sec; | |
743 | unsigned char state; | |
744 | int ret, warnings = 0; | |
745 | ||
746 | insn = first; | |
747 | sec = insn->sec; | |
748 | state = first_state; | |
749 | ||
750 | if (insn->alt_group && list_empty(&insn->alts)) { | |
751 | WARN_FUNC("don't know how to handle branch to middle of alternative instruction group", | |
752 | sec, insn->offset); | |
753 | warnings++; | |
754 | } | |
755 | ||
756 | while (1) { | |
757 | if (insn->visited) { | |
d8d1b2cb | 758 | if (frame_state(insn->state) != frame_state(state)) { |
442f04c3 JP |
759 | WARN_FUNC("frame pointer state mismatch", |
760 | sec, insn->offset); | |
761 | warnings++; | |
762 | } | |
763 | ||
764 | return warnings; | |
765 | } | |
766 | ||
767 | /* | |
768 | * Catch a rare case where a noreturn function falls through to | |
769 | * the next function. | |
770 | */ | |
771 | if (is_fentry_call(insn) && (state & STATE_FENTRY)) | |
772 | return warnings; | |
773 | ||
774 | insn->visited = true; | |
775 | insn->state = state; | |
776 | ||
777 | list_for_each_entry(alt, &insn->alts, list) { | |
778 | ret = validate_branch(file, alt->insn, state); | |
779 | warnings += ret; | |
780 | } | |
781 | ||
782 | switch (insn->type) { | |
783 | ||
784 | case INSN_FP_SAVE: | |
785 | if (!nofp) { | |
786 | if (state & STATE_FP_SAVED) { | |
787 | WARN_FUNC("duplicate frame pointer save", | |
788 | sec, insn->offset); | |
789 | warnings++; | |
790 | } | |
791 | state |= STATE_FP_SAVED; | |
792 | } | |
793 | break; | |
794 | ||
795 | case INSN_FP_SETUP: | |
796 | if (!nofp) { | |
797 | if (state & STATE_FP_SETUP) { | |
798 | WARN_FUNC("duplicate frame pointer setup", | |
799 | sec, insn->offset); | |
800 | warnings++; | |
801 | } | |
802 | state |= STATE_FP_SETUP; | |
803 | } | |
804 | break; | |
805 | ||
806 | case INSN_FP_RESTORE: | |
807 | if (!nofp) { | |
808 | if (has_valid_stack_frame(insn)) | |
809 | state &= ~STATE_FP_SETUP; | |
810 | ||
811 | state &= ~STATE_FP_SAVED; | |
812 | } | |
813 | break; | |
814 | ||
815 | case INSN_RETURN: | |
816 | if (!nofp && has_modified_stack_frame(insn)) { | |
817 | WARN_FUNC("return without frame pointer restore", | |
818 | sec, insn->offset); | |
819 | warnings++; | |
820 | } | |
821 | return warnings; | |
822 | ||
823 | case INSN_CALL: | |
824 | if (is_fentry_call(insn)) { | |
825 | state |= STATE_FENTRY; | |
826 | break; | |
827 | } | |
828 | ||
b1e03249 JP |
829 | ret = dead_end_function(file, insn->call_dest); |
830 | if (ret == 1) | |
442f04c3 | 831 | return warnings; |
b1e03249 JP |
832 | if (ret == -1) |
833 | warnings++; | |
442f04c3 JP |
834 | |
835 | /* fallthrough */ | |
836 | case INSN_CALL_DYNAMIC: | |
837 | if (!nofp && !has_valid_stack_frame(insn)) { | |
838 | WARN_FUNC("call without frame pointer save/setup", | |
839 | sec, insn->offset); | |
840 | warnings++; | |
841 | } | |
842 | break; | |
843 | ||
844 | case INSN_JUMP_CONDITIONAL: | |
845 | case INSN_JUMP_UNCONDITIONAL: | |
846 | if (insn->jump_dest) { | |
847 | ret = validate_branch(file, insn->jump_dest, | |
848 | state); | |
849 | warnings += ret; | |
850 | } else if (has_modified_stack_frame(insn)) { | |
851 | WARN_FUNC("sibling call from callable instruction with changed frame pointer", | |
852 | sec, insn->offset); | |
853 | warnings++; | |
854 | } /* else it's a sibling call */ | |
855 | ||
856 | if (insn->type == INSN_JUMP_UNCONDITIONAL) | |
857 | return warnings; | |
858 | ||
859 | break; | |
860 | ||
861 | case INSN_JUMP_DYNAMIC: | |
862 | if (list_empty(&insn->alts) && | |
863 | has_modified_stack_frame(insn)) { | |
864 | WARN_FUNC("sibling call from callable instruction with changed frame pointer", | |
865 | sec, insn->offset); | |
866 | warnings++; | |
867 | } | |
868 | ||
869 | return warnings; | |
870 | ||
871 | case INSN_BUG: | |
872 | return warnings; | |
873 | ||
874 | default: | |
875 | break; | |
876 | } | |
877 | ||
74aec058 JP |
878 | insn = next_insn_same_sec(file, insn); |
879 | if (!insn) { | |
442f04c3 JP |
880 | WARN("%s: unexpected end of section", sec->name); |
881 | warnings++; | |
882 | return warnings; | |
883 | } | |
884 | } | |
885 | ||
886 | return warnings; | |
887 | } | |
888 | ||
889 | static bool is_gcov_insn(struct instruction *insn) | |
890 | { | |
891 | struct rela *rela; | |
892 | struct section *sec; | |
893 | struct symbol *sym; | |
894 | unsigned long offset; | |
895 | ||
896 | rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len); | |
897 | if (!rela) | |
898 | return false; | |
899 | ||
900 | if (rela->sym->type != STT_SECTION) | |
901 | return false; | |
902 | ||
903 | sec = rela->sym->sec; | |
904 | offset = rela->addend + insn->offset + insn->len - rela->offset; | |
905 | ||
a196e171 | 906 | list_for_each_entry(sym, &sec->symbol_list, list) { |
442f04c3 JP |
907 | if (sym->type != STT_OBJECT) |
908 | continue; | |
909 | ||
910 | if (offset >= sym->offset && offset < sym->offset + sym->len) | |
911 | return (!memcmp(sym->name, "__gcov0.", 8)); | |
912 | } | |
913 | ||
914 | return false; | |
915 | } | |
916 | ||
917 | static bool is_kasan_insn(struct instruction *insn) | |
918 | { | |
919 | return (insn->type == INSN_CALL && | |
920 | !strcmp(insn->call_dest->name, "__asan_handle_no_return")); | |
921 | } | |
922 | ||
923 | static bool is_ubsan_insn(struct instruction *insn) | |
924 | { | |
925 | return (insn->type == INSN_CALL && | |
926 | !strcmp(insn->call_dest->name, | |
927 | "__ubsan_handle_builtin_unreachable")); | |
928 | } | |
929 | ||
74aec058 JP |
930 | static bool ignore_unreachable_insn(struct symbol *func, |
931 | struct instruction *insn) | |
442f04c3 JP |
932 | { |
933 | int i; | |
934 | ||
935 | if (insn->type == INSN_NOP) | |
936 | return true; | |
937 | ||
938 | if (is_gcov_insn(insn)) | |
939 | return true; | |
940 | ||
941 | /* | |
942 | * Check if this (or a subsequent) instruction is related to | |
943 | * CONFIG_UBSAN or CONFIG_KASAN. | |
944 | * | |
945 | * End the search at 5 instructions to avoid going into the weeds. | |
946 | */ | |
947 | for (i = 0; i < 5; i++) { | |
948 | ||
949 | if (is_kasan_insn(insn) || is_ubsan_insn(insn)) | |
950 | return true; | |
951 | ||
952 | if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) { | |
953 | insn = insn->jump_dest; | |
954 | continue; | |
955 | } | |
956 | ||
74aec058 | 957 | if (insn->offset + insn->len >= func->offset + func->len) |
442f04c3 JP |
958 | break; |
959 | insn = list_next_entry(insn, list); | |
960 | } | |
961 | ||
962 | return false; | |
963 | } | |
964 | ||
965 | static int validate_functions(struct objtool_file *file) | |
966 | { | |
967 | struct section *sec; | |
968 | struct symbol *func; | |
969 | struct instruction *insn; | |
442f04c3 JP |
970 | int ret, warnings = 0; |
971 | ||
972 | list_for_each_entry(sec, &file->elf->sections, list) { | |
a196e171 | 973 | list_for_each_entry(func, &sec->symbol_list, list) { |
442f04c3 JP |
974 | if (func->type != STT_FUNC) |
975 | continue; | |
976 | ||
74aec058 | 977 | insn = find_insn(file, sec, func->offset); |
442f04c3 JP |
978 | if (!insn) { |
979 | WARN("%s(): can't find starting instruction", | |
980 | func->name); | |
981 | warnings++; | |
982 | continue; | |
983 | } | |
984 | ||
985 | ret = validate_branch(file, insn, 0); | |
986 | warnings += ret; | |
987 | } | |
988 | } | |
989 | ||
990 | list_for_each_entry(sec, &file->elf->sections, list) { | |
a196e171 | 991 | list_for_each_entry(func, &sec->symbol_list, list) { |
442f04c3 JP |
992 | if (func->type != STT_FUNC) |
993 | continue; | |
994 | ||
74aec058 | 995 | func_for_each_insn(file, func, insn) { |
442f04c3 JP |
996 | if (insn->visited) |
997 | continue; | |
998 | ||
74aec058 | 999 | if (!ignore_unreachable_insn(func, insn)) { |
442f04c3 JP |
1000 | WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset); |
1001 | warnings++; | |
1002 | } | |
1003 | ||
1004 | insn->visited = true; | |
1005 | } | |
1006 | } | |
1007 | } | |
1008 | ||
1009 | return warnings; | |
1010 | } | |
1011 | ||
1012 | static int validate_uncallable_instructions(struct objtool_file *file) | |
1013 | { | |
1014 | struct instruction *insn; | |
1015 | int warnings = 0; | |
1016 | ||
74aec058 | 1017 | for_each_insn(file, insn) { |
442f04c3 JP |
1018 | if (!insn->visited && insn->type == INSN_RETURN) { |
1019 | WARN_FUNC("return instruction outside of a callable function", | |
1020 | insn->sec, insn->offset); | |
1021 | warnings++; | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | return warnings; | |
1026 | } | |
1027 | ||
1028 | static void cleanup(struct objtool_file *file) | |
1029 | { | |
1030 | struct instruction *insn, *tmpinsn; | |
1031 | struct alternative *alt, *tmpalt; | |
1032 | ||
a196e171 | 1033 | list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) { |
442f04c3 JP |
1034 | list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) { |
1035 | list_del(&alt->list); | |
1036 | free(alt); | |
1037 | } | |
1038 | list_del(&insn->list); | |
1039 | free(insn); | |
1040 | } | |
1041 | elf_close(file->elf); | |
1042 | } | |
1043 | ||
1044 | const char * const check_usage[] = { | |
1045 | "objtool check [<options>] file.o", | |
1046 | NULL, | |
1047 | }; | |
1048 | ||
1049 | int cmd_check(int argc, const char **argv) | |
1050 | { | |
1051 | struct objtool_file file; | |
1052 | int ret, warnings = 0; | |
1053 | ||
1054 | const struct option options[] = { | |
1055 | OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"), | |
1056 | OPT_END(), | |
1057 | }; | |
1058 | ||
1059 | argc = parse_options(argc, argv, options, check_usage, 0); | |
1060 | ||
1061 | if (argc != 1) | |
1062 | usage_with_options(check_usage, options); | |
1063 | ||
1064 | objname = argv[0]; | |
1065 | ||
1066 | file.elf = elf_open(objname); | |
1067 | if (!file.elf) { | |
1068 | fprintf(stderr, "error reading elf file %s\n", objname); | |
1069 | return 1; | |
1070 | } | |
1071 | ||
a196e171 | 1072 | INIT_LIST_HEAD(&file.insn_list); |
442f04c3 JP |
1073 | |
1074 | ret = decode_sections(&file); | |
1075 | if (ret < 0) | |
1076 | goto out; | |
1077 | warnings += ret; | |
1078 | ||
1079 | ret = validate_functions(&file); | |
1080 | if (ret < 0) | |
1081 | goto out; | |
1082 | warnings += ret; | |
1083 | ||
1084 | ret = validate_uncallable_instructions(&file); | |
1085 | if (ret < 0) | |
1086 | goto out; | |
1087 | warnings += ret; | |
1088 | ||
1089 | out: | |
1090 | cleanup(&file); | |
1091 | ||
1092 | /* ignore warnings for now until we get all the code cleaned up */ | |
1093 | if (ret || warnings) | |
1094 | return 0; | |
1095 | return 0; | |
1096 | } |