Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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; |
| 63 | struct list_head insns; |
| 64 | }; |
| 65 | |
| 66 | const char *objname; |
| 67 | static bool nofp; |
| 68 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 69 | static struct instruction *find_insn(struct objtool_file *file, |
| 70 | struct section *sec, unsigned long offset) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 71 | { |
| 72 | struct instruction *insn; |
| 73 | |
| 74 | list_for_each_entry(insn, &file->insns, list) |
| 75 | if (insn->sec == sec && insn->offset == offset) |
| 76 | return insn; |
| 77 | |
| 78 | return NULL; |
| 79 | } |
| 80 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 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 | |
| 86 | if (&next->list == &file->insns || next->sec != insn->sec) |
| 87 | return NULL; |
| 88 | |
| 89 | return next; |
| 90 | } |
| 91 | |
| 92 | #define for_each_insn(file, insn) \ |
| 93 | list_for_each_entry(insn, &file->insns, list) |
| 94 | |
| 95 | #define func_for_each_insn(file, func, insn) \ |
| 96 | for (insn = find_insn(file, func->sec, func->offset); \ |
| 97 | insn && &insn->list != &file->insns && \ |
| 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 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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) |
| 120 | list_for_each_entry(rela, ¯o_sec->rela->relas, list) |
| 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 */ |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 126 | func_for_each_insn(file, func, insn) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 127 | if (insn->type == INSN_CONTEXT_SWITCH) |
| 128 | return true; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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. |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 141 | * |
| 142 | * Returns: |
| 143 | * -1: error |
| 144 | * 0: no dead end |
| 145 | * 1: dead end |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 146 | */ |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 147 | static int __dead_end_function(struct objtool_file *file, struct symbol *func, |
| 148 | int recursion) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 149 | { |
| 150 | int i; |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 151 | struct instruction *insn; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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) |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 170 | return 0; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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])) |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 175 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 176 | |
| 177 | if (!func->sec) |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 178 | return 0; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 179 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 180 | func_for_each_insn(file, func, insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 181 | empty = false; |
| 182 | |
| 183 | if (insn->type == INSN_RETURN) |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 184 | return 0; |
Josh Poimboeuf | 81bfafc | 2016-03-09 00:06:51 -0600 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if (empty) |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 188 | return 0; |
Josh Poimboeuf | 81bfafc | 2016-03-09 00:06:51 -0600 | [diff] [blame] | 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 | */ |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 195 | func_for_each_insn(file, func, insn) { |
Josh Poimboeuf | 81bfafc | 2016-03-09 00:06:51 -0600 | [diff] [blame] | 196 | if (insn->sec != func->sec || |
| 197 | insn->offset >= func->offset + func->len) |
| 198 | break; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 */ |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 206 | return 0; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 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); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | if (insn->type == INSN_JUMP_DYNAMIC) |
| 229 | /* sibling call */ |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 230 | return 0; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 231 | } |
| 232 | |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 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); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Call the arch-specific instruction decoder for all the instructions and add |
| 243 | * them to the global insns list. |
| 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 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
| 278 | list_add_tail(&insn->list, &file->insns); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Warnings shouldn't be reported for ignored functions. |
| 287 | */ |
| 288 | static void get_ignores(struct objtool_file *file) |
| 289 | { |
| 290 | struct instruction *insn; |
| 291 | struct section *sec; |
| 292 | struct symbol *func; |
| 293 | |
| 294 | list_for_each_entry(sec, &file->elf->sections, list) { |
| 295 | list_for_each_entry(func, &sec->symbols, list) { |
| 296 | if (func->type != STT_FUNC) |
| 297 | continue; |
| 298 | |
| 299 | if (!ignore_func(file, func)) |
| 300 | continue; |
| 301 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 302 | func_for_each_insn(file, func, insn) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 303 | insn->visited = true; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Find the destination instructions for all jumps. |
| 310 | */ |
| 311 | static int get_jump_destinations(struct objtool_file *file) |
| 312 | { |
| 313 | struct instruction *insn; |
| 314 | struct rela *rela; |
| 315 | struct section *dest_sec; |
| 316 | unsigned long dest_off; |
| 317 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 318 | for_each_insn(file, insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 344 | insn->jump_dest = find_insn(file, dest_sec, dest_off); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | */ |
| 368 | static int get_call_destinations(struct objtool_file *file) |
| 369 | { |
| 370 | struct instruction *insn; |
| 371 | unsigned long dest_off; |
| 372 | struct rela *rela; |
| 373 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 374 | for_each_insn(file, insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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; |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 437 | sec_for_each_insn_from(file, insn) { |
| 438 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 448 | if (!next_insn_same_sec(file, last_orig_insn)) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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; |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 473 | sec_for_each_insn_from(file, insn) { |
| 474 | if (insn->offset >= special_alt->new_off + special_alt->new_len) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | */ |
| 537 | static int get_special_section_alts(struct objtool_file *file) |
| 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 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 557 | orig_insn = find_insn(file, special_alt->orig_sec, |
| 558 | special_alt->orig_off); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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) { |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 568 | new_insn = find_insn(file, special_alt->new_sec, |
| 569 | special_alt->new_off); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | */ |
| 607 | static int get_switch_alts(struct objtool_file *file) |
| 608 | { |
| 609 | struct instruction *insn, *alt_insn; |
| 610 | struct rela *rodata_rela, *rela; |
| 611 | struct section *rodata; |
| 612 | struct symbol *func; |
| 613 | struct alternative *alt; |
| 614 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 615 | for_each_insn(file, insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 616 | if (insn->type != INSN_JUMP_DYNAMIC) |
| 617 | continue; |
| 618 | |
| 619 | rodata_rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 620 | insn->len); |
| 621 | if (!rodata_rela || strcmp(rodata_rela->sym->name, ".rodata")) |
| 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) */ |
| 629 | rela = find_rela_by_dest(rodata, rodata_rela->addend); |
| 630 | |
| 631 | /* rare case: jmpq *[addr](%rip) */ |
| 632 | if (!rela) |
| 633 | rela = find_rela_by_dest(rodata, |
| 634 | rodata_rela->addend + 4); |
| 635 | if (!rela) |
| 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 | |
| 645 | list_for_each_entry_from(rela, &rodata->rela->relas, list) { |
| 646 | if (rela->sym->sec != insn->sec || |
| 647 | rela->addend <= func->offset || |
| 648 | rela->addend >= func->offset + func->len) |
| 649 | break; |
| 650 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 651 | alt_insn = find_insn(file, insn->sec, rela->addend); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 652 | if (!alt_insn) { |
| 653 | WARN("%s: can't find instruction at %s+0x%x", |
| 654 | rodata->rela->name, insn->sec->name, |
| 655 | rela->addend); |
| 656 | return -1; |
| 657 | } |
| 658 | |
| 659 | alt = malloc(sizeof(*alt)); |
| 660 | if (!alt) { |
| 661 | WARN("malloc failed"); |
| 662 | return -1; |
| 663 | } |
| 664 | |
| 665 | alt->insn = alt_insn; |
| 666 | list_add_tail(&alt->list, &insn->alts); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int decode_sections(struct objtool_file *file) |
| 674 | { |
| 675 | int ret; |
| 676 | |
| 677 | ret = decode_instructions(file); |
| 678 | if (ret) |
| 679 | return ret; |
| 680 | |
| 681 | get_ignores(file); |
| 682 | |
| 683 | ret = get_jump_destinations(file); |
| 684 | if (ret) |
| 685 | return ret; |
| 686 | |
| 687 | ret = get_call_destinations(file); |
| 688 | if (ret) |
| 689 | return ret; |
| 690 | |
| 691 | ret = get_special_section_alts(file); |
| 692 | if (ret) |
| 693 | return ret; |
| 694 | |
| 695 | ret = get_switch_alts(file); |
| 696 | if (ret) |
| 697 | return ret; |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static bool is_fentry_call(struct instruction *insn) |
| 703 | { |
| 704 | if (insn->type == INSN_CALL && |
| 705 | insn->call_dest->type == STT_NOTYPE && |
| 706 | !strcmp(insn->call_dest->name, "__fentry__")) |
| 707 | return true; |
| 708 | |
| 709 | return false; |
| 710 | } |
| 711 | |
| 712 | static bool has_modified_stack_frame(struct instruction *insn) |
| 713 | { |
| 714 | return (insn->state & STATE_FP_SAVED) || |
| 715 | (insn->state & STATE_FP_SETUP); |
| 716 | } |
| 717 | |
| 718 | static bool has_valid_stack_frame(struct instruction *insn) |
| 719 | { |
| 720 | return (insn->state & STATE_FP_SAVED) && |
| 721 | (insn->state & STATE_FP_SETUP); |
| 722 | } |
| 723 | |
Josh Poimboeuf | d8d1b2c | 2016-03-09 00:06:54 -0600 | [diff] [blame] | 724 | static unsigned int frame_state(unsigned long state) |
| 725 | { |
| 726 | return (state & (STATE_FP_SAVED | STATE_FP_SETUP)); |
| 727 | } |
| 728 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 729 | /* |
| 730 | * Follow the branch starting at the given instruction, and recursively follow |
| 731 | * any other branches (jumps). Meanwhile, track the frame pointer state at |
| 732 | * each instruction and validate all the rules described in |
| 733 | * tools/objtool/Documentation/stack-validation.txt. |
| 734 | */ |
| 735 | static int validate_branch(struct objtool_file *file, |
| 736 | struct instruction *first, unsigned char first_state) |
| 737 | { |
| 738 | struct alternative *alt; |
| 739 | struct instruction *insn; |
| 740 | struct section *sec; |
| 741 | unsigned char state; |
| 742 | int ret, warnings = 0; |
| 743 | |
| 744 | insn = first; |
| 745 | sec = insn->sec; |
| 746 | state = first_state; |
| 747 | |
| 748 | if (insn->alt_group && list_empty(&insn->alts)) { |
| 749 | WARN_FUNC("don't know how to handle branch to middle of alternative instruction group", |
| 750 | sec, insn->offset); |
| 751 | warnings++; |
| 752 | } |
| 753 | |
| 754 | while (1) { |
| 755 | if (insn->visited) { |
Josh Poimboeuf | d8d1b2c | 2016-03-09 00:06:54 -0600 | [diff] [blame] | 756 | if (frame_state(insn->state) != frame_state(state)) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 757 | WARN_FUNC("frame pointer state mismatch", |
| 758 | sec, insn->offset); |
| 759 | warnings++; |
| 760 | } |
| 761 | |
| 762 | return warnings; |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * Catch a rare case where a noreturn function falls through to |
| 767 | * the next function. |
| 768 | */ |
| 769 | if (is_fentry_call(insn) && (state & STATE_FENTRY)) |
| 770 | return warnings; |
| 771 | |
| 772 | insn->visited = true; |
| 773 | insn->state = state; |
| 774 | |
| 775 | list_for_each_entry(alt, &insn->alts, list) { |
| 776 | ret = validate_branch(file, alt->insn, state); |
| 777 | warnings += ret; |
| 778 | } |
| 779 | |
| 780 | switch (insn->type) { |
| 781 | |
| 782 | case INSN_FP_SAVE: |
| 783 | if (!nofp) { |
| 784 | if (state & STATE_FP_SAVED) { |
| 785 | WARN_FUNC("duplicate frame pointer save", |
| 786 | sec, insn->offset); |
| 787 | warnings++; |
| 788 | } |
| 789 | state |= STATE_FP_SAVED; |
| 790 | } |
| 791 | break; |
| 792 | |
| 793 | case INSN_FP_SETUP: |
| 794 | if (!nofp) { |
| 795 | if (state & STATE_FP_SETUP) { |
| 796 | WARN_FUNC("duplicate frame pointer setup", |
| 797 | sec, insn->offset); |
| 798 | warnings++; |
| 799 | } |
| 800 | state |= STATE_FP_SETUP; |
| 801 | } |
| 802 | break; |
| 803 | |
| 804 | case INSN_FP_RESTORE: |
| 805 | if (!nofp) { |
| 806 | if (has_valid_stack_frame(insn)) |
| 807 | state &= ~STATE_FP_SETUP; |
| 808 | |
| 809 | state &= ~STATE_FP_SAVED; |
| 810 | } |
| 811 | break; |
| 812 | |
| 813 | case INSN_RETURN: |
| 814 | if (!nofp && has_modified_stack_frame(insn)) { |
| 815 | WARN_FUNC("return without frame pointer restore", |
| 816 | sec, insn->offset); |
| 817 | warnings++; |
| 818 | } |
| 819 | return warnings; |
| 820 | |
| 821 | case INSN_CALL: |
| 822 | if (is_fentry_call(insn)) { |
| 823 | state |= STATE_FENTRY; |
| 824 | break; |
| 825 | } |
| 826 | |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 827 | ret = dead_end_function(file, insn->call_dest); |
| 828 | if (ret == 1) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 829 | return warnings; |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 830 | if (ret == -1) |
| 831 | warnings++; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 832 | |
| 833 | /* fallthrough */ |
| 834 | case INSN_CALL_DYNAMIC: |
| 835 | if (!nofp && !has_valid_stack_frame(insn)) { |
| 836 | WARN_FUNC("call without frame pointer save/setup", |
| 837 | sec, insn->offset); |
| 838 | warnings++; |
| 839 | } |
| 840 | break; |
| 841 | |
| 842 | case INSN_JUMP_CONDITIONAL: |
| 843 | case INSN_JUMP_UNCONDITIONAL: |
| 844 | if (insn->jump_dest) { |
| 845 | ret = validate_branch(file, insn->jump_dest, |
| 846 | state); |
| 847 | warnings += ret; |
| 848 | } else if (has_modified_stack_frame(insn)) { |
| 849 | WARN_FUNC("sibling call from callable instruction with changed frame pointer", |
| 850 | sec, insn->offset); |
| 851 | warnings++; |
| 852 | } /* else it's a sibling call */ |
| 853 | |
| 854 | if (insn->type == INSN_JUMP_UNCONDITIONAL) |
| 855 | return warnings; |
| 856 | |
| 857 | break; |
| 858 | |
| 859 | case INSN_JUMP_DYNAMIC: |
| 860 | if (list_empty(&insn->alts) && |
| 861 | has_modified_stack_frame(insn)) { |
| 862 | WARN_FUNC("sibling call from callable instruction with changed frame pointer", |
| 863 | sec, insn->offset); |
| 864 | warnings++; |
| 865 | } |
| 866 | |
| 867 | return warnings; |
| 868 | |
| 869 | case INSN_BUG: |
| 870 | return warnings; |
| 871 | |
| 872 | default: |
| 873 | break; |
| 874 | } |
| 875 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 876 | insn = next_insn_same_sec(file, insn); |
| 877 | if (!insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 878 | WARN("%s: unexpected end of section", sec->name); |
| 879 | warnings++; |
| 880 | return warnings; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | return warnings; |
| 885 | } |
| 886 | |
| 887 | static bool is_gcov_insn(struct instruction *insn) |
| 888 | { |
| 889 | struct rela *rela; |
| 890 | struct section *sec; |
| 891 | struct symbol *sym; |
| 892 | unsigned long offset; |
| 893 | |
| 894 | rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len); |
| 895 | if (!rela) |
| 896 | return false; |
| 897 | |
| 898 | if (rela->sym->type != STT_SECTION) |
| 899 | return false; |
| 900 | |
| 901 | sec = rela->sym->sec; |
| 902 | offset = rela->addend + insn->offset + insn->len - rela->offset; |
| 903 | |
| 904 | list_for_each_entry(sym, &sec->symbols, list) { |
| 905 | if (sym->type != STT_OBJECT) |
| 906 | continue; |
| 907 | |
| 908 | if (offset >= sym->offset && offset < sym->offset + sym->len) |
| 909 | return (!memcmp(sym->name, "__gcov0.", 8)); |
| 910 | } |
| 911 | |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | static bool is_kasan_insn(struct instruction *insn) |
| 916 | { |
| 917 | return (insn->type == INSN_CALL && |
| 918 | !strcmp(insn->call_dest->name, "__asan_handle_no_return")); |
| 919 | } |
| 920 | |
| 921 | static bool is_ubsan_insn(struct instruction *insn) |
| 922 | { |
| 923 | return (insn->type == INSN_CALL && |
| 924 | !strcmp(insn->call_dest->name, |
| 925 | "__ubsan_handle_builtin_unreachable")); |
| 926 | } |
| 927 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 928 | static bool ignore_unreachable_insn(struct symbol *func, |
| 929 | struct instruction *insn) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 930 | { |
| 931 | int i; |
| 932 | |
| 933 | if (insn->type == INSN_NOP) |
| 934 | return true; |
| 935 | |
| 936 | if (is_gcov_insn(insn)) |
| 937 | return true; |
| 938 | |
| 939 | /* |
| 940 | * Check if this (or a subsequent) instruction is related to |
| 941 | * CONFIG_UBSAN or CONFIG_KASAN. |
| 942 | * |
| 943 | * End the search at 5 instructions to avoid going into the weeds. |
| 944 | */ |
| 945 | for (i = 0; i < 5; i++) { |
| 946 | |
| 947 | if (is_kasan_insn(insn) || is_ubsan_insn(insn)) |
| 948 | return true; |
| 949 | |
| 950 | if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) { |
| 951 | insn = insn->jump_dest; |
| 952 | continue; |
| 953 | } |
| 954 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 955 | if (insn->offset + insn->len >= func->offset + func->len) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 956 | break; |
| 957 | insn = list_next_entry(insn, list); |
| 958 | } |
| 959 | |
| 960 | return false; |
| 961 | } |
| 962 | |
| 963 | static int validate_functions(struct objtool_file *file) |
| 964 | { |
| 965 | struct section *sec; |
| 966 | struct symbol *func; |
| 967 | struct instruction *insn; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 968 | int ret, warnings = 0; |
| 969 | |
| 970 | list_for_each_entry(sec, &file->elf->sections, list) { |
| 971 | list_for_each_entry(func, &sec->symbols, list) { |
| 972 | if (func->type != STT_FUNC) |
| 973 | continue; |
| 974 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 975 | insn = find_insn(file, sec, func->offset); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 976 | if (!insn) { |
| 977 | WARN("%s(): can't find starting instruction", |
| 978 | func->name); |
| 979 | warnings++; |
| 980 | continue; |
| 981 | } |
| 982 | |
| 983 | ret = validate_branch(file, insn, 0); |
| 984 | warnings += ret; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | list_for_each_entry(sec, &file->elf->sections, list) { |
| 989 | list_for_each_entry(func, &sec->symbols, list) { |
| 990 | if (func->type != STT_FUNC) |
| 991 | continue; |
| 992 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 993 | func_for_each_insn(file, func, insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 994 | if (insn->visited) |
| 995 | continue; |
| 996 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 997 | if (!ignore_unreachable_insn(func, insn)) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 998 | WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset); |
| 999 | warnings++; |
| 1000 | } |
| 1001 | |
| 1002 | insn->visited = true; |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | return warnings; |
| 1008 | } |
| 1009 | |
| 1010 | static int validate_uncallable_instructions(struct objtool_file *file) |
| 1011 | { |
| 1012 | struct instruction *insn; |
| 1013 | int warnings = 0; |
| 1014 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 1015 | for_each_insn(file, insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1016 | if (!insn->visited && insn->type == INSN_RETURN) { |
| 1017 | WARN_FUNC("return instruction outside of a callable function", |
| 1018 | insn->sec, insn->offset); |
| 1019 | warnings++; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | return warnings; |
| 1024 | } |
| 1025 | |
| 1026 | static void cleanup(struct objtool_file *file) |
| 1027 | { |
| 1028 | struct instruction *insn, *tmpinsn; |
| 1029 | struct alternative *alt, *tmpalt; |
| 1030 | |
| 1031 | list_for_each_entry_safe(insn, tmpinsn, &file->insns, list) { |
| 1032 | list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) { |
| 1033 | list_del(&alt->list); |
| 1034 | free(alt); |
| 1035 | } |
| 1036 | list_del(&insn->list); |
| 1037 | free(insn); |
| 1038 | } |
| 1039 | elf_close(file->elf); |
| 1040 | } |
| 1041 | |
| 1042 | const char * const check_usage[] = { |
| 1043 | "objtool check [<options>] file.o", |
| 1044 | NULL, |
| 1045 | }; |
| 1046 | |
| 1047 | int cmd_check(int argc, const char **argv) |
| 1048 | { |
| 1049 | struct objtool_file file; |
| 1050 | int ret, warnings = 0; |
| 1051 | |
| 1052 | const struct option options[] = { |
| 1053 | OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"), |
| 1054 | OPT_END(), |
| 1055 | }; |
| 1056 | |
| 1057 | argc = parse_options(argc, argv, options, check_usage, 0); |
| 1058 | |
| 1059 | if (argc != 1) |
| 1060 | usage_with_options(check_usage, options); |
| 1061 | |
| 1062 | objname = argv[0]; |
| 1063 | |
| 1064 | file.elf = elf_open(objname); |
| 1065 | if (!file.elf) { |
| 1066 | fprintf(stderr, "error reading elf file %s\n", objname); |
| 1067 | return 1; |
| 1068 | } |
| 1069 | |
| 1070 | INIT_LIST_HEAD(&file.insns); |
| 1071 | |
| 1072 | ret = decode_sections(&file); |
| 1073 | if (ret < 0) |
| 1074 | goto out; |
| 1075 | warnings += ret; |
| 1076 | |
| 1077 | ret = validate_functions(&file); |
| 1078 | if (ret < 0) |
| 1079 | goto out; |
| 1080 | warnings += ret; |
| 1081 | |
| 1082 | ret = validate_uncallable_instructions(&file); |
| 1083 | if (ret < 0) |
| 1084 | goto out; |
| 1085 | warnings += ret; |
| 1086 | |
| 1087 | out: |
| 1088 | cleanup(&file); |
| 1089 | |
| 1090 | /* ignore warnings for now until we get all the code cleaned up */ |
| 1091 | if (ret || warnings) |
| 1092 | return 0; |
| 1093 | return 0; |
| 1094 | } |