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