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; |
Josh Poimboeuf | d1091c7 | 2017-02-21 15:35:32 -0600 | [diff] [blame] | 54 | bool alt_group, visited, dead_end; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | /* |
Josh Poimboeuf | d1091c7 | 2017-02-21 15:35:32 -0600 | [diff] [blame] | 333 | * Find all uses of the unreachable() macro, which are code path dead ends. |
| 334 | */ |
| 335 | static int add_dead_ends(struct objtool_file *file) |
| 336 | { |
| 337 | struct section *sec; |
| 338 | struct rela *rela; |
| 339 | struct instruction *insn; |
| 340 | bool found; |
| 341 | |
Josh Poimboeuf | e390f9a | 2017-03-01 12:04:44 -0600 | [diff] [blame] | 342 | sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); |
Josh Poimboeuf | d1091c7 | 2017-02-21 15:35:32 -0600 | [diff] [blame] | 343 | if (!sec) |
| 344 | return 0; |
| 345 | |
| 346 | list_for_each_entry(rela, &sec->rela_list, list) { |
| 347 | if (rela->sym->type != STT_SECTION) { |
Josh Poimboeuf | e390f9a | 2017-03-01 12:04:44 -0600 | [diff] [blame] | 348 | WARN("unexpected relocation symbol type in %s", sec->name); |
Josh Poimboeuf | d1091c7 | 2017-02-21 15:35:32 -0600 | [diff] [blame] | 349 | return -1; |
| 350 | } |
| 351 | insn = find_insn(file, rela->sym->sec, rela->addend); |
| 352 | if (insn) |
| 353 | insn = list_prev_entry(insn, list); |
| 354 | else if (rela->addend == rela->sym->sec->len) { |
| 355 | found = false; |
| 356 | list_for_each_entry_reverse(insn, &file->insn_list, list) { |
| 357 | if (insn->sec == rela->sym->sec) { |
| 358 | found = true; |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | if (!found) { |
| 364 | WARN("can't find unreachable insn at %s+0x%x", |
| 365 | rela->sym->sec->name, rela->addend); |
| 366 | return -1; |
| 367 | } |
| 368 | } else { |
| 369 | WARN("can't find unreachable insn at %s+0x%x", |
| 370 | rela->sym->sec->name, rela->addend); |
| 371 | return -1; |
| 372 | } |
| 373 | |
| 374 | insn->dead_end = true; |
| 375 | } |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 381 | * Warnings shouldn't be reported for ignored functions. |
| 382 | */ |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 383 | static void add_ignores(struct objtool_file *file) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 384 | { |
| 385 | struct instruction *insn; |
| 386 | struct section *sec; |
| 387 | struct symbol *func; |
| 388 | |
| 389 | list_for_each_entry(sec, &file->elf->sections, list) { |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 390 | list_for_each_entry(func, &sec->symbol_list, list) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 391 | if (func->type != STT_FUNC) |
| 392 | continue; |
| 393 | |
| 394 | if (!ignore_func(file, func)) |
| 395 | continue; |
| 396 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 397 | func_for_each_insn(file, func, insn) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 398 | insn->visited = true; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Find the destination instructions for all jumps. |
| 405 | */ |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 406 | static int add_jump_destinations(struct objtool_file *file) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 407 | { |
| 408 | struct instruction *insn; |
| 409 | struct rela *rela; |
| 410 | struct section *dest_sec; |
| 411 | unsigned long dest_off; |
| 412 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 413 | for_each_insn(file, insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 414 | if (insn->type != INSN_JUMP_CONDITIONAL && |
| 415 | insn->type != INSN_JUMP_UNCONDITIONAL) |
| 416 | continue; |
| 417 | |
| 418 | /* skip ignores */ |
| 419 | if (insn->visited) |
| 420 | continue; |
| 421 | |
| 422 | rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 423 | insn->len); |
| 424 | if (!rela) { |
| 425 | dest_sec = insn->sec; |
| 426 | dest_off = insn->offset + insn->len + insn->immediate; |
| 427 | } else if (rela->sym->type == STT_SECTION) { |
| 428 | dest_sec = rela->sym->sec; |
| 429 | dest_off = rela->addend + 4; |
| 430 | } else if (rela->sym->sec->idx) { |
| 431 | dest_sec = rela->sym->sec; |
| 432 | dest_off = rela->sym->sym.st_value + rela->addend + 4; |
| 433 | } else { |
| 434 | /* sibling call */ |
| 435 | insn->jump_dest = 0; |
| 436 | continue; |
| 437 | } |
| 438 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 439 | insn->jump_dest = find_insn(file, dest_sec, dest_off); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 440 | if (!insn->jump_dest) { |
| 441 | |
| 442 | /* |
| 443 | * This is a special case where an alt instruction |
| 444 | * jumps past the end of the section. These are |
| 445 | * handled later in handle_group_alt(). |
| 446 | */ |
| 447 | if (!strcmp(insn->sec->name, ".altinstr_replacement")) |
| 448 | continue; |
| 449 | |
| 450 | WARN_FUNC("can't find jump dest instruction at %s+0x%lx", |
| 451 | insn->sec, insn->offset, dest_sec->name, |
| 452 | dest_off); |
| 453 | return -1; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Find the destination instructions for all calls. |
| 462 | */ |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 463 | static int add_call_destinations(struct objtool_file *file) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 464 | { |
| 465 | struct instruction *insn; |
| 466 | unsigned long dest_off; |
| 467 | struct rela *rela; |
| 468 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 469 | for_each_insn(file, insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 470 | if (insn->type != INSN_CALL) |
| 471 | continue; |
| 472 | |
| 473 | rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 474 | insn->len); |
| 475 | if (!rela) { |
| 476 | dest_off = insn->offset + insn->len + insn->immediate; |
| 477 | insn->call_dest = find_symbol_by_offset(insn->sec, |
| 478 | dest_off); |
| 479 | if (!insn->call_dest) { |
| 480 | WARN_FUNC("can't find call dest symbol at offset 0x%lx", |
| 481 | insn->sec, insn->offset, dest_off); |
| 482 | return -1; |
| 483 | } |
| 484 | } else if (rela->sym->type == STT_SECTION) { |
| 485 | insn->call_dest = find_symbol_by_offset(rela->sym->sec, |
| 486 | rela->addend+4); |
| 487 | if (!insn->call_dest || |
| 488 | insn->call_dest->type != STT_FUNC) { |
| 489 | WARN_FUNC("can't find call dest symbol at %s+0x%x", |
| 490 | insn->sec, insn->offset, |
| 491 | rela->sym->sec->name, |
| 492 | rela->addend + 4); |
| 493 | return -1; |
| 494 | } |
| 495 | } else |
| 496 | insn->call_dest = rela->sym; |
| 497 | } |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * The .alternatives section requires some extra special care, over and above |
| 504 | * what other special sections require: |
| 505 | * |
| 506 | * 1. Because alternatives are patched in-place, we need to insert a fake jump |
| 507 | * instruction at the end so that validate_branch() skips all the original |
| 508 | * replaced instructions when validating the new instruction path. |
| 509 | * |
| 510 | * 2. An added wrinkle is that the new instruction length might be zero. In |
| 511 | * that case the old instructions are replaced with noops. We simulate that |
| 512 | * by creating a fake jump as the only new instruction. |
| 513 | * |
| 514 | * 3. In some cases, the alternative section includes an instruction which |
| 515 | * conditionally jumps to the _end_ of the entry. We have to modify these |
| 516 | * jumps' destinations to point back to .text rather than the end of the |
| 517 | * entry in .altinstr_replacement. |
| 518 | * |
| 519 | * 4. It has been requested that we don't validate the !POPCNT feature path |
| 520 | * which is a "very very small percentage of machines". |
| 521 | */ |
| 522 | static int handle_group_alt(struct objtool_file *file, |
| 523 | struct special_alt *special_alt, |
| 524 | struct instruction *orig_insn, |
| 525 | struct instruction **new_insn) |
| 526 | { |
| 527 | struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump; |
| 528 | unsigned long dest_off; |
| 529 | |
| 530 | last_orig_insn = NULL; |
| 531 | insn = orig_insn; |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 532 | sec_for_each_insn_from(file, insn) { |
| 533 | if (insn->offset >= special_alt->orig_off + special_alt->orig_len) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 534 | break; |
| 535 | |
| 536 | if (special_alt->skip_orig) |
| 537 | insn->type = INSN_NOP; |
| 538 | |
| 539 | insn->alt_group = true; |
| 540 | last_orig_insn = insn; |
| 541 | } |
| 542 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 543 | if (!next_insn_same_sec(file, last_orig_insn)) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 544 | WARN("%s: don't know how to handle alternatives at end of section", |
| 545 | special_alt->orig_sec->name); |
| 546 | return -1; |
| 547 | } |
| 548 | |
| 549 | fake_jump = malloc(sizeof(*fake_jump)); |
| 550 | if (!fake_jump) { |
| 551 | WARN("malloc failed"); |
| 552 | return -1; |
| 553 | } |
| 554 | memset(fake_jump, 0, sizeof(*fake_jump)); |
| 555 | INIT_LIST_HEAD(&fake_jump->alts); |
| 556 | fake_jump->sec = special_alt->new_sec; |
| 557 | fake_jump->offset = -1; |
| 558 | fake_jump->type = INSN_JUMP_UNCONDITIONAL; |
| 559 | fake_jump->jump_dest = list_next_entry(last_orig_insn, list); |
| 560 | |
| 561 | if (!special_alt->new_len) { |
| 562 | *new_insn = fake_jump; |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | last_new_insn = NULL; |
| 567 | insn = *new_insn; |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 568 | sec_for_each_insn_from(file, insn) { |
| 569 | if (insn->offset >= special_alt->new_off + special_alt->new_len) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 570 | break; |
| 571 | |
| 572 | last_new_insn = insn; |
| 573 | |
| 574 | if (insn->type != INSN_JUMP_CONDITIONAL && |
| 575 | insn->type != INSN_JUMP_UNCONDITIONAL) |
| 576 | continue; |
| 577 | |
| 578 | if (!insn->immediate) |
| 579 | continue; |
| 580 | |
| 581 | dest_off = insn->offset + insn->len + insn->immediate; |
| 582 | if (dest_off == special_alt->new_off + special_alt->new_len) |
| 583 | insn->jump_dest = fake_jump; |
| 584 | |
| 585 | if (!insn->jump_dest) { |
| 586 | WARN_FUNC("can't find alternative jump destination", |
| 587 | insn->sec, insn->offset); |
| 588 | return -1; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (!last_new_insn) { |
| 593 | WARN_FUNC("can't find last new alternative instruction", |
| 594 | special_alt->new_sec, special_alt->new_off); |
| 595 | return -1; |
| 596 | } |
| 597 | |
| 598 | list_add(&fake_jump->list, &last_new_insn->list); |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * A jump table entry can either convert a nop to a jump or a jump to a nop. |
| 605 | * If the original instruction is a jump, make the alt entry an effective nop |
| 606 | * by just skipping the original instruction. |
| 607 | */ |
| 608 | static int handle_jump_alt(struct objtool_file *file, |
| 609 | struct special_alt *special_alt, |
| 610 | struct instruction *orig_insn, |
| 611 | struct instruction **new_insn) |
| 612 | { |
| 613 | if (orig_insn->type == INSN_NOP) |
| 614 | return 0; |
| 615 | |
| 616 | if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { |
| 617 | WARN_FUNC("unsupported instruction at jump label", |
| 618 | orig_insn->sec, orig_insn->offset); |
| 619 | return -1; |
| 620 | } |
| 621 | |
| 622 | *new_insn = list_next_entry(orig_insn, list); |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Read all the special sections which have alternate instructions which can be |
| 628 | * patched in or redirected to at runtime. Each instruction having alternate |
| 629 | * instruction(s) has them added to its insn->alts list, which will be |
| 630 | * traversed in validate_branch(). |
| 631 | */ |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 632 | static int add_special_section_alts(struct objtool_file *file) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 633 | { |
| 634 | struct list_head special_alts; |
| 635 | struct instruction *orig_insn, *new_insn; |
| 636 | struct special_alt *special_alt, *tmp; |
| 637 | struct alternative *alt; |
| 638 | int ret; |
| 639 | |
| 640 | ret = special_get_alts(file->elf, &special_alts); |
| 641 | if (ret) |
| 642 | return ret; |
| 643 | |
| 644 | list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { |
| 645 | alt = malloc(sizeof(*alt)); |
| 646 | if (!alt) { |
| 647 | WARN("malloc failed"); |
| 648 | ret = -1; |
| 649 | goto out; |
| 650 | } |
| 651 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 652 | orig_insn = find_insn(file, special_alt->orig_sec, |
| 653 | special_alt->orig_off); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 654 | if (!orig_insn) { |
| 655 | WARN_FUNC("special: can't find orig instruction", |
| 656 | special_alt->orig_sec, special_alt->orig_off); |
| 657 | ret = -1; |
| 658 | goto out; |
| 659 | } |
| 660 | |
| 661 | new_insn = NULL; |
| 662 | if (!special_alt->group || special_alt->new_len) { |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 663 | new_insn = find_insn(file, special_alt->new_sec, |
| 664 | special_alt->new_off); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 665 | if (!new_insn) { |
| 666 | WARN_FUNC("special: can't find new instruction", |
| 667 | special_alt->new_sec, |
| 668 | special_alt->new_off); |
| 669 | ret = -1; |
| 670 | goto out; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if (special_alt->group) { |
| 675 | ret = handle_group_alt(file, special_alt, orig_insn, |
| 676 | &new_insn); |
| 677 | if (ret) |
| 678 | goto out; |
| 679 | } else if (special_alt->jump_or_nop) { |
| 680 | ret = handle_jump_alt(file, special_alt, orig_insn, |
| 681 | &new_insn); |
| 682 | if (ret) |
| 683 | goto out; |
| 684 | } |
| 685 | |
| 686 | alt->insn = new_insn; |
| 687 | list_add_tail(&alt->list, &orig_insn->alts); |
| 688 | |
| 689 | list_del(&special_alt->list); |
| 690 | free(special_alt); |
| 691 | } |
| 692 | |
| 693 | out: |
| 694 | return ret; |
| 695 | } |
| 696 | |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 697 | static int add_switch_table(struct objtool_file *file, struct symbol *func, |
| 698 | struct instruction *insn, struct rela *table, |
| 699 | struct rela *next_table) |
| 700 | { |
| 701 | struct rela *rela = table; |
| 702 | struct instruction *alt_insn; |
| 703 | struct alternative *alt; |
| 704 | |
| 705 | list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) { |
| 706 | if (rela == next_table) |
| 707 | break; |
| 708 | |
| 709 | if (rela->sym->sec != insn->sec || |
| 710 | rela->addend <= func->offset || |
| 711 | rela->addend >= func->offset + func->len) |
| 712 | break; |
| 713 | |
| 714 | alt_insn = find_insn(file, insn->sec, rela->addend); |
| 715 | if (!alt_insn) { |
| 716 | WARN("%s: can't find instruction at %s+0x%x", |
| 717 | file->rodata->rela->name, insn->sec->name, |
| 718 | rela->addend); |
| 719 | return -1; |
| 720 | } |
| 721 | |
| 722 | alt = malloc(sizeof(*alt)); |
| 723 | if (!alt) { |
| 724 | WARN("malloc failed"); |
| 725 | return -1; |
| 726 | } |
| 727 | |
| 728 | alt->insn = alt_insn; |
| 729 | list_add_tail(&alt->list, &insn->alts); |
| 730 | } |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
Josh Poimboeuf | 6d01f28 | 2016-07-28 19:14:58 -0500 | [diff] [blame] | 735 | /* |
| 736 | * find_switch_table() - Given a dynamic jump, find the switch jump table in |
| 737 | * .rodata associated with it. |
| 738 | * |
| 739 | * There are 3 basic patterns: |
| 740 | * |
| 741 | * 1. jmpq *[rodata addr](,%reg,8) |
| 742 | * |
| 743 | * This is the most common case by far. It jumps to an address in a simple |
| 744 | * jump table which is stored in .rodata. |
| 745 | * |
| 746 | * 2. jmpq *[rodata addr](%rip) |
| 747 | * |
| 748 | * This is caused by a rare GCC quirk, currently only seen in three driver |
| 749 | * functions in the kernel, only with certain obscure non-distro configs. |
| 750 | * |
| 751 | * As part of an optimization, GCC makes a copy of an existing switch jump |
| 752 | * table, modifies it, and then hard-codes the jump (albeit with an indirect |
| 753 | * jump) to use a single entry in the table. The rest of the jump table and |
| 754 | * some of its jump targets remain as dead code. |
| 755 | * |
| 756 | * In such a case we can just crudely ignore all unreachable instruction |
| 757 | * warnings for the entire object file. Ideally we would just ignore them |
| 758 | * for the function, but that would require redesigning the code quite a |
| 759 | * bit. And honestly that's just not worth doing: unreachable instruction |
| 760 | * warnings are of questionable value anyway, and this is such a rare issue. |
| 761 | * |
| 762 | * 3. mov [rodata addr],%reg1 |
| 763 | * ... some instructions ... |
| 764 | * jmpq *(%reg1,%reg2,8) |
| 765 | * |
| 766 | * This is a fairly uncommon pattern which is new for GCC 6. As of this |
| 767 | * writing, there are 11 occurrences of it in the allmodconfig kernel. |
| 768 | * |
| 769 | * TODO: Once we have DWARF CFI and smarter instruction decoding logic, |
| 770 | * ensure the same register is used in the mov and jump instructions. |
| 771 | */ |
| 772 | static struct rela *find_switch_table(struct objtool_file *file, |
| 773 | struct symbol *func, |
| 774 | struct instruction *insn) |
| 775 | { |
| 776 | struct rela *text_rela, *rodata_rela; |
Josh Poimboeuf | 3732710 | 2016-10-13 16:22:52 -0500 | [diff] [blame] | 777 | struct instruction *orig_insn = insn; |
Josh Poimboeuf | 6d01f28 | 2016-07-28 19:14:58 -0500 | [diff] [blame] | 778 | |
| 779 | text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len); |
| 780 | if (text_rela && text_rela->sym == file->rodata->sym) { |
| 781 | /* case 1 */ |
| 782 | rodata_rela = find_rela_by_dest(file->rodata, |
| 783 | text_rela->addend); |
| 784 | if (rodata_rela) |
| 785 | return rodata_rela; |
| 786 | |
| 787 | /* case 2 */ |
| 788 | rodata_rela = find_rela_by_dest(file->rodata, |
| 789 | text_rela->addend + 4); |
| 790 | if (!rodata_rela) |
| 791 | return NULL; |
| 792 | file->ignore_unreachables = true; |
| 793 | return rodata_rela; |
| 794 | } |
| 795 | |
| 796 | /* case 3 */ |
| 797 | func_for_each_insn_continue_reverse(file, func, insn) { |
Josh Poimboeuf | 3732710 | 2016-10-13 16:22:52 -0500 | [diff] [blame] | 798 | if (insn->type == INSN_JUMP_DYNAMIC) |
Josh Poimboeuf | 6d01f28 | 2016-07-28 19:14:58 -0500 | [diff] [blame] | 799 | break; |
| 800 | |
Josh Poimboeuf | 3732710 | 2016-10-13 16:22:52 -0500 | [diff] [blame] | 801 | /* allow small jumps within the range */ |
| 802 | if (insn->type == INSN_JUMP_UNCONDITIONAL && |
| 803 | insn->jump_dest && |
| 804 | (insn->jump_dest->offset <= insn->offset || |
Josh Poimboeuf | 56fb2d6 | 2016-10-26 10:34:08 -0500 | [diff] [blame] | 805 | insn->jump_dest->offset > orig_insn->offset)) |
Josh Poimboeuf | 3732710 | 2016-10-13 16:22:52 -0500 | [diff] [blame] | 806 | break; |
| 807 | |
Josh Poimboeuf | 5c51f4a | 2017-03-02 16:57:23 -0600 | [diff] [blame] | 808 | /* look for a relocation which references .rodata */ |
Josh Poimboeuf | 6d01f28 | 2016-07-28 19:14:58 -0500 | [diff] [blame] | 809 | text_rela = find_rela_by_dest_range(insn->sec, insn->offset, |
| 810 | insn->len); |
Josh Poimboeuf | 5c51f4a | 2017-03-02 16:57:23 -0600 | [diff] [blame] | 811 | if (!text_rela || text_rela->sym != file->rodata->sym) |
| 812 | continue; |
| 813 | |
| 814 | /* |
| 815 | * Make sure the .rodata address isn't associated with a |
| 816 | * symbol. gcc jump tables are anonymous data. |
| 817 | */ |
| 818 | if (find_symbol_containing(file->rodata, text_rela->addend)) |
| 819 | continue; |
| 820 | |
| 821 | return find_rela_by_dest(file->rodata, text_rela->addend); |
Josh Poimboeuf | 6d01f28 | 2016-07-28 19:14:58 -0500 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | return NULL; |
| 825 | } |
| 826 | |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 827 | static int add_func_switch_tables(struct objtool_file *file, |
| 828 | struct symbol *func) |
| 829 | { |
Josh Poimboeuf | 6d01f28 | 2016-07-28 19:14:58 -0500 | [diff] [blame] | 830 | struct instruction *insn, *prev_jump = NULL; |
| 831 | struct rela *rela, *prev_rela = NULL; |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 832 | int ret; |
| 833 | |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 834 | func_for_each_insn(file, func, insn) { |
| 835 | if (insn->type != INSN_JUMP_DYNAMIC) |
| 836 | continue; |
| 837 | |
Josh Poimboeuf | 6d01f28 | 2016-07-28 19:14:58 -0500 | [diff] [blame] | 838 | rela = find_switch_table(file, func, insn); |
| 839 | if (!rela) |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 840 | continue; |
| 841 | |
| 842 | /* |
| 843 | * We found a switch table, but we don't know yet how big it |
| 844 | * is. Don't add it until we reach the end of the function or |
| 845 | * the beginning of another switch table in the same function. |
| 846 | */ |
| 847 | if (prev_jump) { |
| 848 | ret = add_switch_table(file, func, prev_jump, prev_rela, |
Josh Poimboeuf | 6d01f28 | 2016-07-28 19:14:58 -0500 | [diff] [blame] | 849 | rela); |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 850 | if (ret) |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | prev_jump = insn; |
Josh Poimboeuf | 6d01f28 | 2016-07-28 19:14:58 -0500 | [diff] [blame] | 855 | prev_rela = rela; |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | if (prev_jump) { |
| 859 | ret = add_switch_table(file, func, prev_jump, prev_rela, NULL); |
| 860 | if (ret) |
| 861 | return ret; |
| 862 | } |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 867 | /* |
| 868 | * For some switch statements, gcc generates a jump table in the .rodata |
| 869 | * section which contains a list of addresses within the function to jump to. |
| 870 | * This finds these jump tables and adds them to the insn->alts lists. |
| 871 | */ |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 872 | static int add_switch_table_alts(struct objtool_file *file) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 873 | { |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 874 | struct section *sec; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 875 | struct symbol *func; |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 876 | int ret; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 877 | |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 878 | if (!file->rodata || !file->rodata->rela) |
| 879 | return 0; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 880 | |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 881 | list_for_each_entry(sec, &file->elf->sections, list) { |
| 882 | list_for_each_entry(func, &sec->symbol_list, list) { |
| 883 | if (func->type != STT_FUNC) |
| 884 | continue; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 885 | |
Josh Poimboeuf | 8133fbb | 2016-03-09 00:06:58 -0600 | [diff] [blame] | 886 | ret = add_func_switch_tables(file, func); |
| 887 | if (ret) |
| 888 | return ret; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static int decode_sections(struct objtool_file *file) |
| 896 | { |
| 897 | int ret; |
| 898 | |
| 899 | ret = decode_instructions(file); |
| 900 | if (ret) |
| 901 | return ret; |
| 902 | |
Josh Poimboeuf | d1091c7 | 2017-02-21 15:35:32 -0600 | [diff] [blame] | 903 | ret = add_dead_ends(file); |
| 904 | if (ret) |
| 905 | return ret; |
| 906 | |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 907 | add_ignores(file); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 908 | |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 909 | ret = add_jump_destinations(file); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 910 | if (ret) |
| 911 | return ret; |
| 912 | |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 913 | ret = add_call_destinations(file); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 914 | if (ret) |
| 915 | return ret; |
| 916 | |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 917 | ret = add_special_section_alts(file); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 918 | if (ret) |
| 919 | return ret; |
| 920 | |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 921 | ret = add_switch_table_alts(file); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 922 | if (ret) |
| 923 | return ret; |
| 924 | |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | static bool is_fentry_call(struct instruction *insn) |
| 929 | { |
| 930 | if (insn->type == INSN_CALL && |
| 931 | insn->call_dest->type == STT_NOTYPE && |
| 932 | !strcmp(insn->call_dest->name, "__fentry__")) |
| 933 | return true; |
| 934 | |
| 935 | return false; |
| 936 | } |
| 937 | |
| 938 | static bool has_modified_stack_frame(struct instruction *insn) |
| 939 | { |
| 940 | return (insn->state & STATE_FP_SAVED) || |
| 941 | (insn->state & STATE_FP_SETUP); |
| 942 | } |
| 943 | |
| 944 | static bool has_valid_stack_frame(struct instruction *insn) |
| 945 | { |
| 946 | return (insn->state & STATE_FP_SAVED) && |
| 947 | (insn->state & STATE_FP_SETUP); |
| 948 | } |
| 949 | |
Josh Poimboeuf | d8d1b2c | 2016-03-09 00:06:54 -0600 | [diff] [blame] | 950 | static unsigned int frame_state(unsigned long state) |
| 951 | { |
| 952 | return (state & (STATE_FP_SAVED | STATE_FP_SETUP)); |
| 953 | } |
| 954 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 955 | /* |
| 956 | * Follow the branch starting at the given instruction, and recursively follow |
| 957 | * any other branches (jumps). Meanwhile, track the frame pointer state at |
| 958 | * each instruction and validate all the rules described in |
| 959 | * tools/objtool/Documentation/stack-validation.txt. |
| 960 | */ |
| 961 | static int validate_branch(struct objtool_file *file, |
| 962 | struct instruction *first, unsigned char first_state) |
| 963 | { |
| 964 | struct alternative *alt; |
| 965 | struct instruction *insn; |
| 966 | struct section *sec; |
Josh Poimboeuf | b1547d3 | 2016-04-15 09:17:10 -0500 | [diff] [blame] | 967 | struct symbol *func = NULL; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 968 | unsigned char state; |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 969 | int ret; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 970 | |
| 971 | insn = first; |
| 972 | sec = insn->sec; |
| 973 | state = first_state; |
| 974 | |
| 975 | if (insn->alt_group && list_empty(&insn->alts)) { |
| 976 | WARN_FUNC("don't know how to handle branch to middle of alternative instruction group", |
| 977 | sec, insn->offset); |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 978 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | while (1) { |
Josh Poimboeuf | b1547d3 | 2016-04-15 09:17:10 -0500 | [diff] [blame] | 982 | if (file->c_file && insn->func) { |
| 983 | if (func && func != insn->func) { |
| 984 | WARN("%s() falls through to next function %s()", |
| 985 | func->name, insn->func->name); |
| 986 | return 1; |
| 987 | } |
| 988 | |
| 989 | func = insn->func; |
| 990 | } |
| 991 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 992 | if (insn->visited) { |
Josh Poimboeuf | d8d1b2c | 2016-03-09 00:06:54 -0600 | [diff] [blame] | 993 | if (frame_state(insn->state) != frame_state(state)) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 994 | WARN_FUNC("frame pointer state mismatch", |
| 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 | } |
| 998 | |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 999 | return 0; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1000 | } |
| 1001 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1002 | insn->visited = true; |
| 1003 | insn->state = state; |
| 1004 | |
| 1005 | list_for_each_entry(alt, &insn->alts, list) { |
| 1006 | ret = validate_branch(file, alt->insn, state); |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1007 | if (ret) |
| 1008 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | switch (insn->type) { |
| 1012 | |
| 1013 | case INSN_FP_SAVE: |
| 1014 | if (!nofp) { |
| 1015 | if (state & STATE_FP_SAVED) { |
| 1016 | WARN_FUNC("duplicate frame pointer save", |
| 1017 | sec, insn->offset); |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1018 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1019 | } |
| 1020 | state |= STATE_FP_SAVED; |
| 1021 | } |
| 1022 | break; |
| 1023 | |
| 1024 | case INSN_FP_SETUP: |
| 1025 | if (!nofp) { |
| 1026 | if (state & STATE_FP_SETUP) { |
| 1027 | WARN_FUNC("duplicate frame pointer setup", |
| 1028 | sec, insn->offset); |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1029 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1030 | } |
| 1031 | state |= STATE_FP_SETUP; |
| 1032 | } |
| 1033 | break; |
| 1034 | |
| 1035 | case INSN_FP_RESTORE: |
| 1036 | if (!nofp) { |
| 1037 | if (has_valid_stack_frame(insn)) |
| 1038 | state &= ~STATE_FP_SETUP; |
| 1039 | |
| 1040 | state &= ~STATE_FP_SAVED; |
| 1041 | } |
| 1042 | break; |
| 1043 | |
| 1044 | case INSN_RETURN: |
| 1045 | if (!nofp && has_modified_stack_frame(insn)) { |
| 1046 | WARN_FUNC("return without frame pointer restore", |
| 1047 | sec, insn->offset); |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1048 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1049 | } |
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 | case INSN_CALL: |
| 1053 | if (is_fentry_call(insn)) { |
| 1054 | state |= STATE_FENTRY; |
| 1055 | break; |
| 1056 | } |
| 1057 | |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 1058 | ret = dead_end_function(file, insn->call_dest); |
| 1059 | if (ret == 1) |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1060 | return 0; |
Josh Poimboeuf | b1e0324 | 2016-03-09 00:06:52 -0600 | [diff] [blame] | 1061 | if (ret == -1) |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1062 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1063 | |
| 1064 | /* fallthrough */ |
| 1065 | case INSN_CALL_DYNAMIC: |
| 1066 | if (!nofp && !has_valid_stack_frame(insn)) { |
| 1067 | WARN_FUNC("call without frame pointer save/setup", |
| 1068 | sec, insn->offset); |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1069 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1070 | } |
| 1071 | break; |
| 1072 | |
| 1073 | case INSN_JUMP_CONDITIONAL: |
| 1074 | case INSN_JUMP_UNCONDITIONAL: |
| 1075 | if (insn->jump_dest) { |
| 1076 | ret = validate_branch(file, insn->jump_dest, |
| 1077 | state); |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1078 | if (ret) |
| 1079 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1080 | } else if (has_modified_stack_frame(insn)) { |
| 1081 | WARN_FUNC("sibling call from callable instruction with changed frame pointer", |
| 1082 | sec, insn->offset); |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1083 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1084 | } /* else it's a sibling call */ |
| 1085 | |
| 1086 | if (insn->type == INSN_JUMP_UNCONDITIONAL) |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1087 | return 0; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1088 | |
| 1089 | break; |
| 1090 | |
| 1091 | case INSN_JUMP_DYNAMIC: |
| 1092 | if (list_empty(&insn->alts) && |
| 1093 | has_modified_stack_frame(insn)) { |
| 1094 | WARN_FUNC("sibling call from callable instruction with changed frame pointer", |
| 1095 | sec, insn->offset); |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1096 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1097 | } |
| 1098 | |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1099 | return 0; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1100 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1101 | default: |
| 1102 | break; |
| 1103 | } |
| 1104 | |
Josh Poimboeuf | d1091c7 | 2017-02-21 15:35:32 -0600 | [diff] [blame] | 1105 | if (insn->dead_end) |
| 1106 | return 0; |
| 1107 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 1108 | insn = next_insn_same_sec(file, insn); |
| 1109 | if (!insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1110 | WARN("%s: unexpected end of section", sec->name); |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1111 | return 1; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
Josh Poimboeuf | 1bcb58a | 2016-03-09 00:07:01 -0600 | [diff] [blame] | 1115 | return 0; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1116 | } |
| 1117 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1118 | static bool is_kasan_insn(struct instruction *insn) |
| 1119 | { |
| 1120 | return (insn->type == INSN_CALL && |
| 1121 | !strcmp(insn->call_dest->name, "__asan_handle_no_return")); |
| 1122 | } |
| 1123 | |
| 1124 | static bool is_ubsan_insn(struct instruction *insn) |
| 1125 | { |
| 1126 | return (insn->type == INSN_CALL && |
| 1127 | !strcmp(insn->call_dest->name, |
| 1128 | "__ubsan_handle_builtin_unreachable")); |
| 1129 | } |
| 1130 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 1131 | static bool ignore_unreachable_insn(struct symbol *func, |
| 1132 | struct instruction *insn) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1133 | { |
| 1134 | int i; |
| 1135 | |
| 1136 | if (insn->type == INSN_NOP) |
| 1137 | return true; |
| 1138 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1139 | /* |
| 1140 | * Check if this (or a subsequent) instruction is related to |
| 1141 | * CONFIG_UBSAN or CONFIG_KASAN. |
| 1142 | * |
| 1143 | * End the search at 5 instructions to avoid going into the weeds. |
| 1144 | */ |
| 1145 | for (i = 0; i < 5; i++) { |
| 1146 | |
| 1147 | if (is_kasan_insn(insn) || is_ubsan_insn(insn)) |
| 1148 | return true; |
| 1149 | |
| 1150 | if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) { |
| 1151 | insn = insn->jump_dest; |
| 1152 | continue; |
| 1153 | } |
| 1154 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 1155 | if (insn->offset + insn->len >= func->offset + func->len) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1156 | break; |
| 1157 | insn = list_next_entry(insn, list); |
| 1158 | } |
| 1159 | |
| 1160 | return false; |
| 1161 | } |
| 1162 | |
| 1163 | static int validate_functions(struct objtool_file *file) |
| 1164 | { |
| 1165 | struct section *sec; |
| 1166 | struct symbol *func; |
| 1167 | struct instruction *insn; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1168 | int ret, warnings = 0; |
| 1169 | |
| 1170 | list_for_each_entry(sec, &file->elf->sections, list) { |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 1171 | list_for_each_entry(func, &sec->symbol_list, list) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1172 | if (func->type != STT_FUNC) |
| 1173 | continue; |
| 1174 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 1175 | insn = find_insn(file, sec, func->offset); |
Josh Poimboeuf | b1547d3 | 2016-04-15 09:17:10 -0500 | [diff] [blame] | 1176 | if (!insn) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1177 | continue; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1178 | |
| 1179 | ret = validate_branch(file, insn, 0); |
| 1180 | warnings += ret; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | list_for_each_entry(sec, &file->elf->sections, list) { |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 1185 | list_for_each_entry(func, &sec->symbol_list, list) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1186 | if (func->type != STT_FUNC) |
| 1187 | continue; |
| 1188 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 1189 | func_for_each_insn(file, func, insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1190 | if (insn->visited) |
| 1191 | continue; |
| 1192 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1193 | insn->visited = true; |
Josh Poimboeuf | 7e57844 | 2016-04-14 14:52:24 -0500 | [diff] [blame] | 1194 | |
| 1195 | if (file->ignore_unreachables || warnings || |
| 1196 | ignore_unreachable_insn(func, insn)) |
| 1197 | continue; |
| 1198 | |
Josh Poimboeuf | 9cfffb1 | 2016-10-13 16:22:53 -0500 | [diff] [blame] | 1199 | /* |
| 1200 | * gcov produces a lot of unreachable |
| 1201 | * instructions. If we get an unreachable |
| 1202 | * warning and the file has gcov enabled, just |
| 1203 | * ignore it, and all other such warnings for |
| 1204 | * the file. |
| 1205 | */ |
| 1206 | if (!file->ignore_unreachables && |
| 1207 | gcov_enabled(file)) { |
| 1208 | file->ignore_unreachables = true; |
| 1209 | continue; |
| 1210 | } |
| 1211 | |
Josh Poimboeuf | 7e57844 | 2016-04-14 14:52:24 -0500 | [diff] [blame] | 1212 | WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset); |
| 1213 | warnings++; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | return warnings; |
| 1219 | } |
| 1220 | |
| 1221 | static int validate_uncallable_instructions(struct objtool_file *file) |
| 1222 | { |
| 1223 | struct instruction *insn; |
| 1224 | int warnings = 0; |
| 1225 | |
Josh Poimboeuf | 74aec05 | 2016-03-09 00:06:55 -0600 | [diff] [blame] | 1226 | for_each_insn(file, insn) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1227 | if (!insn->visited && insn->type == INSN_RETURN) { |
| 1228 | WARN_FUNC("return instruction outside of a callable function", |
| 1229 | insn->sec, insn->offset); |
| 1230 | warnings++; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | return warnings; |
| 1235 | } |
| 1236 | |
| 1237 | static void cleanup(struct objtool_file *file) |
| 1238 | { |
| 1239 | struct instruction *insn, *tmpinsn; |
| 1240 | struct alternative *alt, *tmpalt; |
| 1241 | |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 1242 | list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1243 | list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) { |
| 1244 | list_del(&alt->list); |
| 1245 | free(alt); |
| 1246 | } |
| 1247 | list_del(&insn->list); |
Josh Poimboeuf | 042ba73 | 2016-03-09 00:07:00 -0600 | [diff] [blame] | 1248 | hash_del(&insn->hash); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1249 | free(insn); |
| 1250 | } |
| 1251 | elf_close(file->elf); |
| 1252 | } |
| 1253 | |
| 1254 | const char * const check_usage[] = { |
| 1255 | "objtool check [<options>] file.o", |
| 1256 | NULL, |
| 1257 | }; |
| 1258 | |
| 1259 | int cmd_check(int argc, const char **argv) |
| 1260 | { |
| 1261 | struct objtool_file file; |
| 1262 | int ret, warnings = 0; |
| 1263 | |
| 1264 | const struct option options[] = { |
| 1265 | OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"), |
| 1266 | OPT_END(), |
| 1267 | }; |
| 1268 | |
| 1269 | argc = parse_options(argc, argv, options, check_usage, 0); |
| 1270 | |
| 1271 | if (argc != 1) |
| 1272 | usage_with_options(check_usage, options); |
| 1273 | |
| 1274 | objname = argv[0]; |
| 1275 | |
| 1276 | file.elf = elf_open(objname); |
| 1277 | if (!file.elf) { |
| 1278 | fprintf(stderr, "error reading elf file %s\n", objname); |
| 1279 | return 1; |
| 1280 | } |
| 1281 | |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 1282 | INIT_LIST_HEAD(&file.insn_list); |
Josh Poimboeuf | 042ba73 | 2016-03-09 00:07:00 -0600 | [diff] [blame] | 1283 | hash_init(file.insn_hash); |
Josh Poimboeuf | e390f9a | 2017-03-01 12:04:44 -0600 | [diff] [blame] | 1284 | file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard"); |
Josh Poimboeuf | 7e57844 | 2016-04-14 14:52:24 -0500 | [diff] [blame] | 1285 | file.rodata = find_section_by_name(file.elf, ".rodata"); |
| 1286 | file.ignore_unreachables = false; |
Josh Poimboeuf | b1547d3 | 2016-04-15 09:17:10 -0500 | [diff] [blame] | 1287 | file.c_file = find_section_by_name(file.elf, ".comment"); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 1288 | |
| 1289 | ret = decode_sections(&file); |
| 1290 | if (ret < 0) |
| 1291 | goto out; |
| 1292 | warnings += ret; |
| 1293 | |
| 1294 | ret = validate_functions(&file); |
| 1295 | if (ret < 0) |
| 1296 | goto out; |
| 1297 | warnings += ret; |
| 1298 | |
| 1299 | ret = validate_uncallable_instructions(&file); |
| 1300 | if (ret < 0) |
| 1301 | goto out; |
| 1302 | warnings += ret; |
| 1303 | |
| 1304 | out: |
| 1305 | cleanup(&file); |
| 1306 | |
| 1307 | /* ignore warnings for now until we get all the code cleaned up */ |
| 1308 | if (ret || warnings) |
| 1309 | return 0; |
| 1310 | return 0; |
| 1311 | } |