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