blob: 5eee156f6f903772511cbe2dfe112c4f38854c8b [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002/*
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05004 */
5
6#include <string.h>
7#include <stdlib.h>
8
Peter Zijlstra43a45252018-01-16 17:16:32 +01009#include "builtin.h"
Matt Helsley0decf1f2020-05-19 13:55:33 -070010#include "cfi.h"
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050011#include "arch.h"
Matt Helsley0decf1f2020-05-19 13:55:33 -070012#include "check.h"
13#include "special.h"
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050014#include "warn.h"
Peter Zijlstra0f1441b2020-06-12 16:05:26 +020015#include "arch_elf.h"
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050016
17#include <linux/hashtable.h>
18#include <linux/kernel.h>
19
Josh Poimboeufe6da9562019-05-13 12:01:31 -050020#define FAKE_JUMP_OFFSET -1
21
Josh Poimboeuf87b512d2019-06-27 20:50:46 -050022#define C_JUMP_TABLE_SECTION ".rodata..c_jump_table"
23
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050024struct alternative {
25 struct list_head list;
26 struct instruction *insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +010027 bool skip_orig;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050028};
29
30const char *objname;
Peter Zijlstraa3608f52020-03-25 15:34:50 +010031struct cfi_init_state initial_func_cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050032
Josh Poimboeuf627fce12017-07-11 10:33:42 -050033struct instruction *find_insn(struct objtool_file *file,
34 struct section *sec, unsigned long offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050035{
36 struct instruction *insn;
37
Peter Zijlstra87ecb582020-03-16 15:47:27 +010038 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050039 if (insn->sec == sec && insn->offset == offset)
40 return insn;
Peter Zijlstra87ecb582020-03-16 15:47:27 +010041 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050042
43 return NULL;
44}
45
46static struct instruction *next_insn_same_sec(struct objtool_file *file,
47 struct instruction *insn)
48{
49 struct instruction *next = list_next_entry(insn, list);
50
Josh Poimboeufbaa41462017-06-28 10:11:07 -050051 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050052 return NULL;
53
54 return next;
55}
56
Josh Poimboeuf13810432018-05-09 22:39:15 -050057static struct instruction *next_insn_same_func(struct objtool_file *file,
58 struct instruction *insn)
59{
60 struct instruction *next = list_next_entry(insn, list);
61 struct symbol *func = insn->func;
62
63 if (!func)
64 return NULL;
65
66 if (&next->list != &file->insn_list && next->func == func)
67 return next;
68
69 /* Check if we're already in the subfunction: */
70 if (func == func->cfunc)
71 return NULL;
72
73 /* Move to the subfunction: */
74 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
75}
76
Josh Poimboeuf1119d262020-04-28 16:45:16 -050077static struct instruction *prev_insn_same_sym(struct objtool_file *file,
78 struct instruction *insn)
79{
80 struct instruction *prev = list_prev_entry(insn, list);
81
82 if (&prev->list != &file->insn_list && prev->func == insn->func)
83 return prev;
84
85 return NULL;
86}
87
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +010088#define func_for_each_insn(file, func, insn) \
Josh Poimboeuf13810432018-05-09 22:39:15 -050089 for (insn = find_insn(file, func->sec, func->offset); \
90 insn; \
91 insn = next_insn_same_func(file, insn))
92
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +010093#define sym_for_each_insn(file, sym, insn) \
94 for (insn = find_insn(file, sym->sec, sym->offset); \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050095 insn && &insn->list != &file->insn_list && \
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +010096 insn->sec == sym->sec && \
97 insn->offset < sym->offset + sym->len; \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050098 insn = list_next_entry(insn, list))
99
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100100#define sym_for_each_insn_continue_reverse(file, sym, insn) \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500101 for (insn = list_prev_entry(insn, list); \
102 &insn->list != &file->insn_list && \
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100103 insn->sec == sym->sec && insn->offset >= sym->offset; \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500104 insn = list_prev_entry(insn, list))
105
106#define sec_for_each_insn_from(file, insn) \
107 for (; insn; insn = next_insn_same_sec(file, insn))
108
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500109#define sec_for_each_insn_continue(file, insn) \
110 for (insn = next_insn_same_sec(file, insn); insn; \
111 insn = next_insn_same_sec(file, insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500112
Josh Poimboeufa2296142020-02-10 12:32:39 -0600113static bool is_static_jump(struct instruction *insn)
114{
115 return insn->type == INSN_JUMP_CONDITIONAL ||
116 insn->type == INSN_JUMP_UNCONDITIONAL;
117}
118
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500119static bool is_sibling_call(struct instruction *insn)
120{
121 /* An indirect jump is either a sibling call or a jump to a table. */
122 if (insn->type == INSN_JUMP_DYNAMIC)
123 return list_empty(&insn->alts);
124
Josh Poimboeufa2296142020-02-10 12:32:39 -0600125 if (!is_static_jump(insn))
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500126 return false;
127
128 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
129 return !!insn->call_dest;
130}
131
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500132/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500133 * This checks to see if the given function is a "noreturn" function.
134 *
135 * For global functions which are outside the scope of this object file, we
136 * have to keep a manual list of them.
137 *
138 * For local functions, we have to detect them manually by simply looking for
139 * the lack of a return instruction.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500140 */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500141static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
142 int recursion)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500143{
144 int i;
145 struct instruction *insn;
146 bool empty = true;
147
148 /*
149 * Unfortunately these have to be hard coded because the noreturn
150 * attribute isn't provided in ELF data.
151 */
152 static const char * const global_noreturns[] = {
153 "__stack_chk_fail",
154 "panic",
155 "do_exit",
156 "do_task_dead",
157 "__module_put_and_exit",
158 "complete_and_exit",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500159 "__reiserfs_panic",
160 "lbug_with_loc",
161 "fortify_panic",
Kees Cookb394d462018-01-10 14:22:38 -0800162 "usercopy_abort",
Josh Poimboeuf684fb242018-06-19 10:47:50 -0500163 "machine_real_restart",
Josh Poimboeuf4fa5ecd2019-04-04 12:17:35 -0500164 "rewind_stack_do_exit",
Brendan Higgins33adf802019-09-23 02:02:38 -0700165 "kunit_try_catch_throw",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500166 };
167
Josh Poimboeufc9bab222019-07-17 20:36:51 -0500168 if (!func)
169 return false;
170
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500171 if (func->bind == STB_WEAK)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500172 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500173
174 if (func->bind == STB_GLOBAL)
175 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
176 if (!strcmp(func->name, global_noreturns[i]))
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500177 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500178
Josh Poimboeuf13810432018-05-09 22:39:15 -0500179 if (!func->len)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500180 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500181
Josh Poimboeuf13810432018-05-09 22:39:15 -0500182 insn = find_insn(file, func->sec, func->offset);
183 if (!insn->func)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500184 return false;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500185
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100186 func_for_each_insn(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500187 empty = false;
188
189 if (insn->type == INSN_RETURN)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500190 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500191 }
192
193 if (empty)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500194 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500195
196 /*
197 * A function can have a sibling call instead of a return. In that
198 * case, the function's dead-end status depends on whether the target
199 * of the sibling call returns.
200 */
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100201 func_for_each_insn(file, func, insn) {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500202 if (is_sibling_call(insn)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500203 struct instruction *dest = insn->jump_dest;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500204
205 if (!dest)
206 /* sibling call to another file */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500207 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500208
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500209 /* local sibling call */
210 if (recursion == 5) {
211 /*
212 * Infinite recursion: two functions have
213 * sibling calls to each other. This is a very
214 * rare case. It means they aren't dead ends.
215 */
216 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500217 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500218
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500219 return __dead_end_function(file, dest->func, recursion+1);
220 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500221 }
222
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500223 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500224}
225
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500226static bool dead_end_function(struct objtool_file *file, struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500227{
228 return __dead_end_function(file, func, 0);
229}
230
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100231static void init_cfi_state(struct cfi_state *cfi)
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500232{
233 int i;
234
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500235 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100236 cfi->regs[i].base = CFI_UNDEFINED;
237 cfi->vals[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500238 }
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100239 cfi->cfa.base = CFI_UNDEFINED;
240 cfi->drap_reg = CFI_UNDEFINED;
241 cfi->drap_offset = -1;
242}
243
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100244static void init_insn_state(struct insn_state *state, struct section *sec)
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100245{
246 memset(state, 0, sizeof(*state));
247 init_cfi_state(&state->cfi);
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100248
249 /*
250 * We need the full vmlinux for noinstr validation, otherwise we can
251 * not correctly determine insn->call_dest->sec (external symbols do
252 * not have a section).
253 */
254 if (vmlinux && sec)
255 state->noinstr = sec->noinstr;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500256}
257
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500258/*
259 * Call the arch-specific instruction decoder for all the instructions and add
260 * them to the global instruction list.
261 */
262static int decode_instructions(struct objtool_file *file)
263{
264 struct section *sec;
265 struct symbol *func;
266 unsigned long offset;
267 struct instruction *insn;
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100268 unsigned long nr_insns = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500269 int ret;
270
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500271 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500272
273 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
274 continue;
275
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500276 if (strcmp(sec->name, ".altinstr_replacement") &&
277 strcmp(sec->name, ".altinstr_aux") &&
278 strncmp(sec->name, ".discard.", 9))
279 sec->text = true;
280
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +0100281 if (!strcmp(sec->name, ".noinstr.text") ||
282 !strcmp(sec->name, ".entry.text"))
Peter Zijlstrac4a33932020-03-10 18:57:41 +0100283 sec->noinstr = true;
284
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500285 for (offset = 0; offset < sec->len; offset += insn->len) {
286 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500287 if (!insn) {
288 WARN("malloc failed");
289 return -1;
290 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500291 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500292 INIT_LIST_HEAD(&insn->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +0000293 INIT_LIST_HEAD(&insn->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100294 init_cfi_state(&insn->cfi);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500295
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500296 insn->sec = sec;
297 insn->offset = offset;
298
299 ret = arch_decode_instruction(file->elf, sec, offset,
300 sec->len - offset,
301 &insn->len, &insn->type,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500302 &insn->immediate,
Julien Thierry65ea47d2020-03-27 15:28:47 +0000303 &insn->stack_ops);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500304 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500305 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500306
Peter Zijlstra87ecb582020-03-16 15:47:27 +0100307 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500308 list_add_tail(&insn->list, &file->insn_list);
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100309 nr_insns++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500310 }
311
312 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500313 if (func->type != STT_FUNC || func->alias != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500314 continue;
315
316 if (!find_insn(file, sec, func->offset)) {
317 WARN("%s(): can't find starting instruction",
318 func->name);
319 return -1;
320 }
321
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100322 sym_for_each_insn(file, func, insn)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500323 insn->func = func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500324 }
325 }
326
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100327 if (stats)
328 printf("nr_insns: %lu\n", nr_insns);
329
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500330 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500331
332err:
333 free(insn);
334 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500335}
336
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700337static struct instruction *find_last_insn(struct objtool_file *file,
338 struct section *sec)
339{
340 struct instruction *insn = NULL;
341 unsigned int offset;
342 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
343
344 for (offset = sec->len - 1; offset >= end && !insn; offset--)
345 insn = find_insn(file, sec, offset);
346
347 return insn;
348}
349
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500350/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500351 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500352 */
353static int add_dead_ends(struct objtool_file *file)
354{
355 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700356 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500357 struct instruction *insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500358
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500359 /*
360 * By default, "ud2" is a dead end unless otherwise annotated, because
361 * GCC 7 inserts it for certain divide-by-zero cases.
362 */
363 for_each_insn(file, insn)
364 if (insn->type == INSN_BUG)
365 insn->dead_end = true;
366
367 /*
368 * Check for manually annotated dead ends.
369 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500370 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
371 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500372 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500373
Matt Helsleyf1974222020-05-29 14:01:13 -0700374 list_for_each_entry(reloc, &sec->reloc_list, list) {
375 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500376 WARN("unexpected relocation symbol type in %s", sec->name);
377 return -1;
378 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700379 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500380 if (insn)
381 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700382 else if (reloc->addend == reloc->sym->sec->len) {
383 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700384 if (!insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500385 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700386 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500387 return -1;
388 }
389 } else {
390 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700391 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500392 return -1;
393 }
394
395 insn->dead_end = true;
396 }
397
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500398reachable:
399 /*
400 * These manually annotated reachable checks are needed for GCC 4.4,
401 * where the Linux unreachable() macro isn't supported. In that case
402 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
403 * not a dead end.
404 */
405 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
406 if (!sec)
407 return 0;
408
Matt Helsleyf1974222020-05-29 14:01:13 -0700409 list_for_each_entry(reloc, &sec->reloc_list, list) {
410 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500411 WARN("unexpected relocation symbol type in %s", sec->name);
412 return -1;
413 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700414 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500415 if (insn)
416 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700417 else if (reloc->addend == reloc->sym->sec->len) {
418 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700419 if (!insn) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500420 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700421 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500422 return -1;
423 }
424 } else {
425 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700426 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500427 return -1;
428 }
429
430 insn->dead_end = false;
431 }
432
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500433 return 0;
434}
435
436/*
437 * Warnings shouldn't be reported for ignored functions.
438 */
439static void add_ignores(struct objtool_file *file)
440{
441 struct instruction *insn;
442 struct section *sec;
443 struct symbol *func;
Matt Helsleyf1974222020-05-29 14:01:13 -0700444 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500445
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100446 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
447 if (!sec)
448 return;
449
Matt Helsleyf1974222020-05-29 14:01:13 -0700450 list_for_each_entry(reloc, &sec->reloc_list, list) {
451 switch (reloc->sym->type) {
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100452 case STT_FUNC:
Matt Helsleyf1974222020-05-29 14:01:13 -0700453 func = reloc->sym;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100454 break;
455
456 case STT_SECTION:
Matt Helsleyf1974222020-05-29 14:01:13 -0700457 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600458 if (!func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500459 continue;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100460 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500461
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100462 default:
Matt Helsleyf1974222020-05-29 14:01:13 -0700463 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100464 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500465 }
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100466
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100467 func_for_each_insn(file, func, insn)
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100468 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500469 }
470}
471
472/*
Peter Zijlstraea242132019-02-25 12:50:09 +0100473 * This is a whitelist of functions that is allowed to be called with AC set.
474 * The list is meant to be minimal and only contains compiler instrumentation
475 * ABI and a few functions used to implement *_{to,from}_user() functions.
476 *
477 * These functions must not directly change AC, but may PUSHF/POPF.
478 */
479static const char *uaccess_safe_builtin[] = {
480 /* KASAN */
481 "kasan_report",
482 "check_memory_region",
483 /* KASAN out-of-line */
484 "__asan_loadN_noabort",
485 "__asan_load1_noabort",
486 "__asan_load2_noabort",
487 "__asan_load4_noabort",
488 "__asan_load8_noabort",
489 "__asan_load16_noabort",
490 "__asan_storeN_noabort",
491 "__asan_store1_noabort",
492 "__asan_store2_noabort",
493 "__asan_store4_noabort",
494 "__asan_store8_noabort",
495 "__asan_store16_noabort",
496 /* KASAN in-line */
497 "__asan_report_load_n_noabort",
498 "__asan_report_load1_noabort",
499 "__asan_report_load2_noabort",
500 "__asan_report_load4_noabort",
501 "__asan_report_load8_noabort",
502 "__asan_report_load16_noabort",
503 "__asan_report_store_n_noabort",
504 "__asan_report_store1_noabort",
505 "__asan_report_store2_noabort",
506 "__asan_report_store4_noabort",
507 "__asan_report_store8_noabort",
508 "__asan_report_store16_noabort",
Marco Elver5f5c9712019-11-14 19:02:57 +0100509 /* KCSAN */
Marco Elver99676832020-03-25 17:41:57 +0100510 "__kcsan_check_access",
Marco Elver5f5c9712019-11-14 19:02:57 +0100511 "kcsan_found_watchpoint",
512 "kcsan_setup_watchpoint",
Marco Elver99676832020-03-25 17:41:57 +0100513 "kcsan_check_scoped_accesses",
Marco Elver50a19ad2020-04-24 17:47:30 +0200514 "kcsan_disable_current",
515 "kcsan_enable_current_nowarn",
Marco Elver5f5c9712019-11-14 19:02:57 +0100516 /* KCSAN/TSAN */
517 "__tsan_func_entry",
518 "__tsan_func_exit",
519 "__tsan_read_range",
520 "__tsan_write_range",
521 "__tsan_read1",
522 "__tsan_read2",
523 "__tsan_read4",
524 "__tsan_read8",
525 "__tsan_read16",
526 "__tsan_write1",
527 "__tsan_write2",
528 "__tsan_write4",
529 "__tsan_write8",
530 "__tsan_write16",
Marco Elvera81b3752020-07-24 09:00:02 +0200531 "__tsan_read_write1",
532 "__tsan_read_write2",
533 "__tsan_read_write4",
534 "__tsan_read_write8",
535 "__tsan_read_write16",
Marco Elver883957b2020-07-03 15:40:30 +0200536 "__tsan_atomic8_load",
537 "__tsan_atomic16_load",
538 "__tsan_atomic32_load",
539 "__tsan_atomic64_load",
540 "__tsan_atomic8_store",
541 "__tsan_atomic16_store",
542 "__tsan_atomic32_store",
543 "__tsan_atomic64_store",
544 "__tsan_atomic8_exchange",
545 "__tsan_atomic16_exchange",
546 "__tsan_atomic32_exchange",
547 "__tsan_atomic64_exchange",
548 "__tsan_atomic8_fetch_add",
549 "__tsan_atomic16_fetch_add",
550 "__tsan_atomic32_fetch_add",
551 "__tsan_atomic64_fetch_add",
552 "__tsan_atomic8_fetch_sub",
553 "__tsan_atomic16_fetch_sub",
554 "__tsan_atomic32_fetch_sub",
555 "__tsan_atomic64_fetch_sub",
556 "__tsan_atomic8_fetch_and",
557 "__tsan_atomic16_fetch_and",
558 "__tsan_atomic32_fetch_and",
559 "__tsan_atomic64_fetch_and",
560 "__tsan_atomic8_fetch_or",
561 "__tsan_atomic16_fetch_or",
562 "__tsan_atomic32_fetch_or",
563 "__tsan_atomic64_fetch_or",
564 "__tsan_atomic8_fetch_xor",
565 "__tsan_atomic16_fetch_xor",
566 "__tsan_atomic32_fetch_xor",
567 "__tsan_atomic64_fetch_xor",
568 "__tsan_atomic8_fetch_nand",
569 "__tsan_atomic16_fetch_nand",
570 "__tsan_atomic32_fetch_nand",
571 "__tsan_atomic64_fetch_nand",
572 "__tsan_atomic8_compare_exchange_strong",
573 "__tsan_atomic16_compare_exchange_strong",
574 "__tsan_atomic32_compare_exchange_strong",
575 "__tsan_atomic64_compare_exchange_strong",
576 "__tsan_atomic8_compare_exchange_weak",
577 "__tsan_atomic16_compare_exchange_weak",
578 "__tsan_atomic32_compare_exchange_weak",
579 "__tsan_atomic64_compare_exchange_weak",
580 "__tsan_atomic8_compare_exchange_val",
581 "__tsan_atomic16_compare_exchange_val",
582 "__tsan_atomic32_compare_exchange_val",
583 "__tsan_atomic64_compare_exchange_val",
584 "__tsan_atomic_thread_fence",
585 "__tsan_atomic_signal_fence",
Peter Zijlstraea242132019-02-25 12:50:09 +0100586 /* KCOV */
587 "write_comp_data",
Josh Poimboeufae033f02020-04-29 14:09:04 -0500588 "check_kcov_mode",
Peter Zijlstraea242132019-02-25 12:50:09 +0100589 "__sanitizer_cov_trace_pc",
590 "__sanitizer_cov_trace_const_cmp1",
591 "__sanitizer_cov_trace_const_cmp2",
592 "__sanitizer_cov_trace_const_cmp4",
593 "__sanitizer_cov_trace_const_cmp8",
594 "__sanitizer_cov_trace_cmp1",
595 "__sanitizer_cov_trace_cmp2",
596 "__sanitizer_cov_trace_cmp4",
597 "__sanitizer_cov_trace_cmp8",
Al Viro36b1c702020-02-16 13:07:49 -0500598 "__sanitizer_cov_trace_switch",
Peter Zijlstraea242132019-02-25 12:50:09 +0100599 /* UBSAN */
600 "ubsan_type_mismatch_common",
601 "__ubsan_handle_type_mismatch",
602 "__ubsan_handle_type_mismatch_v1",
Peter Zijlstra9a50dca2019-10-21 15:11:49 +0200603 "__ubsan_handle_shift_out_of_bounds",
Peter Zijlstraea242132019-02-25 12:50:09 +0100604 /* misc */
605 "csum_partial_copy_generic",
606 "__memcpy_mcsafe",
Josh Poimboeufa7e47f22019-07-17 20:36:46 -0500607 "mcsafe_handle_tail",
Peter Zijlstraea242132019-02-25 12:50:09 +0100608 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
609 NULL
610};
611
612static void add_uaccess_safe(struct objtool_file *file)
613{
614 struct symbol *func;
615 const char **name;
616
617 if (!uaccess)
618 return;
619
620 for (name = uaccess_safe_builtin; *name; name++) {
621 func = find_symbol_by_name(file->elf, *name);
622 if (!func)
623 continue;
624
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500625 func->uaccess_safe = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500626 }
627}
628
629/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000630 * FIXME: For now, just ignore any alternatives which add retpolines. This is
631 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
632 * But it at least allows objtool to understand the control flow *around* the
633 * retpoline.
634 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100635static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000636{
637 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700638 struct reloc *reloc;
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000639 struct instruction *insn;
640
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100641 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000642 if (!sec)
643 return 0;
644
Matt Helsleyf1974222020-05-29 14:01:13 -0700645 list_for_each_entry(reloc, &sec->reloc_list, list) {
646 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000647 WARN("unexpected relocation symbol type in %s", sec->name);
648 return -1;
649 }
650
Matt Helsleyf1974222020-05-29 14:01:13 -0700651 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000652 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100653 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000654 return -1;
655 }
656
657 insn->ignore_alts = true;
658 }
659
660 return 0;
661}
662
663/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500664 * Find the destination instructions for all jumps.
665 */
666static int add_jump_destinations(struct objtool_file *file)
667{
668 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -0700669 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500670 struct section *dest_sec;
671 unsigned long dest_off;
672
673 for_each_insn(file, insn) {
Josh Poimboeufa2296142020-02-10 12:32:39 -0600674 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500675 continue;
676
Josh Poimboeufe6da9562019-05-13 12:01:31 -0500677 if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500678 continue;
679
Matt Helsleyf1974222020-05-29 14:01:13 -0700680 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100681 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700682 if (!reloc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500683 dest_sec = insn->sec;
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000684 dest_off = arch_jump_destination(insn);
Matt Helsleyf1974222020-05-29 14:01:13 -0700685 } else if (reloc->sym->type == STT_SECTION) {
686 dest_sec = reloc->sym->sec;
687 dest_off = arch_dest_reloc_offset(reloc->addend);
688 } else if (reloc->sym->sec->idx) {
689 dest_sec = reloc->sym->sec;
690 dest_off = reloc->sym->sym.st_value +
691 arch_dest_reloc_offset(reloc->addend);
692 } else if (strstr(reloc->sym->name, "_indirect_thunk_")) {
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000693 /*
694 * Retpoline jumps are really dynamic jumps in
695 * disguise, so convert them accordingly.
696 */
Josh Poimboeufb68b9902019-07-17 20:36:57 -0500697 if (insn->type == INSN_JUMP_UNCONDITIONAL)
698 insn->type = INSN_JUMP_DYNAMIC;
699 else
700 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
701
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100702 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000703 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500704 } else {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500705 /* external sibling call */
Matt Helsleyf1974222020-05-29 14:01:13 -0700706 insn->call_dest = reloc->sym;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500707 continue;
708 }
709
710 insn->jump_dest = find_insn(file, dest_sec, dest_off);
711 if (!insn->jump_dest) {
712
713 /*
714 * This is a special case where an alt instruction
715 * jumps past the end of the section. These are
716 * handled later in handle_group_alt().
717 */
718 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
719 continue;
720
721 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
722 insn->sec, insn->offset, dest_sec->name,
723 dest_off);
724 return -1;
725 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500726
727 /*
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100728 * Cross-function jump.
Josh Poimboeufcd778492018-06-01 07:23:51 -0500729 */
730 if (insn->func && insn->jump_dest->func &&
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100731 insn->func != insn->jump_dest->func) {
732
733 /*
734 * For GCC 8+, create parent/child links for any cold
735 * subfunctions. This is _mostly_ redundant with a
736 * similar initialization in read_symbols().
737 *
738 * If a function has aliases, we want the *first* such
739 * function in the symbol table to be the subfunction's
740 * parent. In that case we overwrite the
741 * initialization done in read_symbols().
742 *
743 * However this code can't completely replace the
744 * read_symbols() code because this doesn't detect the
745 * case where the parent function's only reference to a
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500746 * subfunction is through a jump table.
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100747 */
748 if (!strstr(insn->func->name, ".cold.") &&
749 strstr(insn->jump_dest->func->name, ".cold.")) {
750 insn->func->cfunc = insn->jump_dest->func;
751 insn->jump_dest->func->pfunc = insn->func;
752
753 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
754 insn->jump_dest->offset == insn->jump_dest->func->offset) {
755
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500756 /* internal sibling call */
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100757 insn->call_dest = insn->jump_dest->func;
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100758 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500759 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500760 }
761
762 return 0;
763}
764
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200765static void remove_insn_ops(struct instruction *insn)
766{
767 struct stack_op *op, *tmp;
768
769 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
770 list_del(&op->list);
771 free(op);
772 }
773}
774
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500775/*
776 * Find the destination instructions for all calls.
777 */
778static int add_call_destinations(struct objtool_file *file)
779{
780 struct instruction *insn;
781 unsigned long dest_off;
Matt Helsleyf1974222020-05-29 14:01:13 -0700782 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500783
784 for_each_insn(file, insn) {
785 if (insn->type != INSN_CALL)
786 continue;
787
Matt Helsleyf1974222020-05-29 14:01:13 -0700788 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100789 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700790 if (!reloc) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000791 dest_off = arch_jump_destination(insn);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600792 insn->call_dest = find_func_by_offset(insn->sec, dest_off);
793 if (!insn->call_dest)
794 insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600795
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600796 if (insn->ignore)
797 continue;
798
799 if (!insn->call_dest) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200800 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500801 return -1;
802 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600803
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600804 if (insn->func && insn->call_dest->type != STT_FUNC) {
805 WARN_FUNC("unsupported call to non-function",
806 insn->sec, insn->offset);
807 return -1;
808 }
809
Matt Helsleyf1974222020-05-29 14:01:13 -0700810 } else if (reloc->sym->type == STT_SECTION) {
811 dest_off = arch_dest_reloc_offset(reloc->addend);
812 insn->call_dest = find_func_by_offset(reloc->sym->sec,
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000813 dest_off);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600814 if (!insn->call_dest) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000815 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500816 insn->sec, insn->offset,
Matt Helsleyf1974222020-05-29 14:01:13 -0700817 reloc->sym->sec->name,
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000818 dest_off);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500819 return -1;
820 }
821 } else
Matt Helsleyf1974222020-05-29 14:01:13 -0700822 insn->call_dest = reloc->sym;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200823
824 /*
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200825 * Many compilers cannot disable KCOV with a function attribute
826 * so they need a little help, NOP out any KCOV calls from noinstr
827 * text.
828 */
829 if (insn->sec->noinstr &&
830 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
Peter Zijlstrad832c002020-06-18 17:55:29 +0200831 if (reloc) {
832 reloc->type = R_NONE;
833 elf_write_reloc(file->elf, reloc);
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200834 }
835
836 elf_write_insn(file->elf, insn->sec,
837 insn->offset, insn->len,
838 arch_nop_insn(insn->len));
839 insn->type = INSN_NOP;
840 }
841
842 /*
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200843 * Whatever stack impact regular CALLs have, should be undone
844 * by the RETURN of the called function.
845 *
846 * Annotated intra-function calls retain the stack_ops but
847 * are converted to JUMP, see read_intra_function_calls().
848 */
849 remove_insn_ops(insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500850 }
851
852 return 0;
853}
854
855/*
856 * The .alternatives section requires some extra special care, over and above
857 * what other special sections require:
858 *
859 * 1. Because alternatives are patched in-place, we need to insert a fake jump
860 * instruction at the end so that validate_branch() skips all the original
861 * replaced instructions when validating the new instruction path.
862 *
863 * 2. An added wrinkle is that the new instruction length might be zero. In
864 * that case the old instructions are replaced with noops. We simulate that
865 * by creating a fake jump as the only new instruction.
866 *
867 * 3. In some cases, the alternative section includes an instruction which
868 * conditionally jumps to the _end_ of the entry. We have to modify these
869 * jumps' destinations to point back to .text rather than the end of the
870 * entry in .altinstr_replacement.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500871 */
872static int handle_group_alt(struct objtool_file *file,
873 struct special_alt *special_alt,
874 struct instruction *orig_insn,
875 struct instruction **new_insn)
876{
Alexandre Chartre13fab062020-04-14 12:36:11 +0200877 static unsigned int alt_group_next_index = 1;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600878 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Alexandre Chartre13fab062020-04-14 12:36:11 +0200879 unsigned int alt_group = alt_group_next_index++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500880 unsigned long dest_off;
881
882 last_orig_insn = NULL;
883 insn = orig_insn;
884 sec_for_each_insn_from(file, insn) {
885 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
886 break;
887
Alexandre Chartre13fab062020-04-14 12:36:11 +0200888 insn->alt_group = alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500889 last_orig_insn = insn;
890 }
891
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600892 if (next_insn_same_sec(file, last_orig_insn)) {
893 fake_jump = malloc(sizeof(*fake_jump));
894 if (!fake_jump) {
895 WARN("malloc failed");
896 return -1;
897 }
898 memset(fake_jump, 0, sizeof(*fake_jump));
899 INIT_LIST_HEAD(&fake_jump->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +0000900 INIT_LIST_HEAD(&fake_jump->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100901 init_cfi_state(&fake_jump->cfi);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500902
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600903 fake_jump->sec = special_alt->new_sec;
Josh Poimboeufe6da9562019-05-13 12:01:31 -0500904 fake_jump->offset = FAKE_JUMP_OFFSET;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600905 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
906 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
Josh Poimboeufe6da9562019-05-13 12:01:31 -0500907 fake_jump->func = orig_insn->func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500908 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500909
910 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600911 if (!fake_jump) {
912 WARN("%s: empty alternative at end of section",
913 special_alt->orig_sec->name);
914 return -1;
915 }
916
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500917 *new_insn = fake_jump;
918 return 0;
919 }
920
921 last_new_insn = NULL;
Alexandre Chartre13fab062020-04-14 12:36:11 +0200922 alt_group = alt_group_next_index++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500923 insn = *new_insn;
924 sec_for_each_insn_from(file, insn) {
925 if (insn->offset >= special_alt->new_off + special_alt->new_len)
926 break;
927
928 last_new_insn = insn;
929
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600930 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +0100931 insn->func = orig_insn->func;
Alexandre Chartre13fab062020-04-14 12:36:11 +0200932 insn->alt_group = alt_group;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600933
Josh Poimboeufdc419722020-02-10 12:32:40 -0600934 /*
935 * Since alternative replacement code is copy/pasted by the
936 * kernel after applying relocations, generally such code can't
937 * have relative-address relocation references to outside the
938 * .altinstr_replacement section, unless the arch's
939 * alternatives code can adjust the relative offsets
940 * accordingly.
941 *
942 * The x86 alternatives code adjusts the offsets only when it
943 * encounters a branch instruction at the very beginning of the
944 * replacement group.
945 */
946 if ((insn->offset != special_alt->new_off ||
947 (insn->type != INSN_CALL && !is_static_jump(insn))) &&
Matt Helsleyf1974222020-05-29 14:01:13 -0700948 find_reloc_by_dest_range(file->elf, insn->sec, insn->offset, insn->len)) {
Josh Poimboeufdc419722020-02-10 12:32:40 -0600949
950 WARN_FUNC("unsupported relocation in alternatives section",
951 insn->sec, insn->offset);
952 return -1;
953 }
954
Josh Poimboeufa2296142020-02-10 12:32:39 -0600955 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500956 continue;
957
958 if (!insn->immediate)
959 continue;
960
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000961 dest_off = arch_jump_destination(insn);
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600962 if (dest_off == special_alt->new_off + special_alt->new_len) {
963 if (!fake_jump) {
964 WARN("%s: alternative jump to end of section",
965 special_alt->orig_sec->name);
966 return -1;
967 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500968 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600969 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500970
971 if (!insn->jump_dest) {
972 WARN_FUNC("can't find alternative jump destination",
973 insn->sec, insn->offset);
974 return -1;
975 }
976 }
977
978 if (!last_new_insn) {
979 WARN_FUNC("can't find last new alternative instruction",
980 special_alt->new_sec, special_alt->new_off);
981 return -1;
982 }
983
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600984 if (fake_jump)
985 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500986
987 return 0;
988}
989
990/*
991 * A jump table entry can either convert a nop to a jump or a jump to a nop.
992 * If the original instruction is a jump, make the alt entry an effective nop
993 * by just skipping the original instruction.
994 */
995static int handle_jump_alt(struct objtool_file *file,
996 struct special_alt *special_alt,
997 struct instruction *orig_insn,
998 struct instruction **new_insn)
999{
1000 if (orig_insn->type == INSN_NOP)
1001 return 0;
1002
1003 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1004 WARN_FUNC("unsupported instruction at jump label",
1005 orig_insn->sec, orig_insn->offset);
1006 return -1;
1007 }
1008
1009 *new_insn = list_next_entry(orig_insn, list);
1010 return 0;
1011}
1012
1013/*
1014 * Read all the special sections which have alternate instructions which can be
1015 * patched in or redirected to at runtime. Each instruction having alternate
1016 * instruction(s) has them added to its insn->alts list, which will be
1017 * traversed in validate_branch().
1018 */
1019static int add_special_section_alts(struct objtool_file *file)
1020{
1021 struct list_head special_alts;
1022 struct instruction *orig_insn, *new_insn;
1023 struct special_alt *special_alt, *tmp;
1024 struct alternative *alt;
1025 int ret;
1026
1027 ret = special_get_alts(file->elf, &special_alts);
1028 if (ret)
1029 return ret;
1030
1031 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001032
1033 orig_insn = find_insn(file, special_alt->orig_sec,
1034 special_alt->orig_off);
1035 if (!orig_insn) {
1036 WARN_FUNC("special: can't find orig instruction",
1037 special_alt->orig_sec, special_alt->orig_off);
1038 ret = -1;
1039 goto out;
1040 }
1041
1042 new_insn = NULL;
1043 if (!special_alt->group || special_alt->new_len) {
1044 new_insn = find_insn(file, special_alt->new_sec,
1045 special_alt->new_off);
1046 if (!new_insn) {
1047 WARN_FUNC("special: can't find new instruction",
1048 special_alt->new_sec,
1049 special_alt->new_off);
1050 ret = -1;
1051 goto out;
1052 }
1053 }
1054
1055 if (special_alt->group) {
Julien Thierry7170cf42020-03-27 15:28:41 +00001056 if (!special_alt->orig_len) {
1057 WARN_FUNC("empty alternative entry",
1058 orig_insn->sec, orig_insn->offset);
1059 continue;
1060 }
1061
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001062 ret = handle_group_alt(file, special_alt, orig_insn,
1063 &new_insn);
1064 if (ret)
1065 goto out;
1066 } else if (special_alt->jump_or_nop) {
1067 ret = handle_jump_alt(file, special_alt, orig_insn,
1068 &new_insn);
1069 if (ret)
1070 goto out;
1071 }
1072
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001073 alt = malloc(sizeof(*alt));
1074 if (!alt) {
1075 WARN("malloc failed");
1076 ret = -1;
1077 goto out;
1078 }
1079
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001080 alt->insn = new_insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +01001081 alt->skip_orig = special_alt->skip_orig;
Peter Zijlstraea242132019-02-25 12:50:09 +01001082 orig_insn->ignore_alts |= special_alt->skip_alt;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001083 list_add_tail(&alt->list, &orig_insn->alts);
1084
1085 list_del(&special_alt->list);
1086 free(special_alt);
1087 }
1088
1089out:
1090 return ret;
1091}
1092
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001093static int add_jump_table(struct objtool_file *file, struct instruction *insn,
Matt Helsleyf1974222020-05-29 14:01:13 -07001094 struct reloc *table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001095{
Matt Helsleyf1974222020-05-29 14:01:13 -07001096 struct reloc *reloc = table;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001097 struct instruction *dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001098 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001099 struct symbol *pfunc = insn->func->pfunc;
1100 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001101
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001102 /*
Matt Helsleyf1974222020-05-29 14:01:13 -07001103 * Each @reloc is a switch table relocation which points to the target
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001104 * instruction.
1105 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001106 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
Jann Hornbd98c812019-07-17 20:36:54 -05001107
1108 /* Check for the end of the table: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001109 if (reloc != table && reloc->jump_table_start)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001110 break;
1111
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001112 /* Make sure the table entries are consecutive: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001113 if (prev_offset && reloc->offset != prev_offset + 8)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001114 break;
1115
1116 /* Detect function pointers from contiguous objects: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001117 if (reloc->sym->sec == pfunc->sec &&
1118 reloc->addend == pfunc->offset)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001119 break;
1120
Matt Helsleyf1974222020-05-29 14:01:13 -07001121 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001122 if (!dest_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001123 break;
1124
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001125 /* Make sure the destination is in the same function: */
Josh Poimboeufe65050b2019-07-17 20:36:55 -05001126 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -05001127 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001128
1129 alt = malloc(sizeof(*alt));
1130 if (!alt) {
1131 WARN("malloc failed");
1132 return -1;
1133 }
1134
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001135 alt->insn = dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001136 list_add_tail(&alt->list, &insn->alts);
Matt Helsleyf1974222020-05-29 14:01:13 -07001137 prev_offset = reloc->offset;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001138 }
1139
1140 if (!prev_offset) {
1141 WARN_FUNC("can't find switch jump table",
1142 insn->sec, insn->offset);
1143 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001144 }
1145
1146 return 0;
1147}
1148
1149/*
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001150 * find_jump_table() - Given a dynamic jump, find the switch jump table in
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001151 * .rodata associated with it.
1152 *
1153 * There are 3 basic patterns:
1154 *
1155 * 1. jmpq *[rodata addr](,%reg,8)
1156 *
1157 * This is the most common case by far. It jumps to an address in a simple
1158 * jump table which is stored in .rodata.
1159 *
1160 * 2. jmpq *[rodata addr](%rip)
1161 *
1162 * This is caused by a rare GCC quirk, currently only seen in three driver
1163 * functions in the kernel, only with certain obscure non-distro configs.
1164 *
1165 * As part of an optimization, GCC makes a copy of an existing switch jump
1166 * table, modifies it, and then hard-codes the jump (albeit with an indirect
1167 * jump) to use a single entry in the table. The rest of the jump table and
1168 * some of its jump targets remain as dead code.
1169 *
1170 * In such a case we can just crudely ignore all unreachable instruction
1171 * warnings for the entire object file. Ideally we would just ignore them
1172 * for the function, but that would require redesigning the code quite a
1173 * bit. And honestly that's just not worth doing: unreachable instruction
1174 * warnings are of questionable value anyway, and this is such a rare issue.
1175 *
1176 * 3. mov [rodata addr],%reg1
1177 * ... some instructions ...
1178 * jmpq *(%reg1,%reg2,8)
1179 *
1180 * This is a fairly uncommon pattern which is new for GCC 6. As of this
1181 * writing, there are 11 occurrences of it in the allmodconfig kernel.
1182 *
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001183 * As of GCC 7 there are quite a few more of these and the 'in between' code
1184 * is significant. Esp. with KASAN enabled some of the code between the mov
1185 * and jmpq uses .rodata itself, which can confuse things.
1186 *
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001187 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1188 * ensure the same register is used in the mov and jump instructions.
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001189 *
1190 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001191 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001192static struct reloc *find_jump_table(struct objtool_file *file,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001193 struct symbol *func,
1194 struct instruction *insn)
1195{
Matt Helsleyf1974222020-05-29 14:01:13 -07001196 struct reloc *text_reloc, *table_reloc;
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001197 struct instruction *dest_insn, *orig_insn = insn;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001198 struct section *table_sec;
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -05001199 unsigned long table_offset;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001200
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001201 /*
1202 * Backward search using the @first_jump_src links, these help avoid
1203 * much of the 'in between' code. Which avoids us getting confused by
1204 * it.
1205 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001206 for (;
Josh Poimboeuf1119d262020-04-28 16:45:16 -05001207 insn && insn->func && insn->func->pfunc == func;
1208 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001209
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001210 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001211 break;
1212
1213 /* allow small jumps within the range */
1214 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1215 insn->jump_dest &&
1216 (insn->jump_dest->offset <= insn->offset ||
1217 insn->jump_dest->offset > orig_insn->offset))
1218 break;
1219
1220 /* look for a relocation which references .rodata */
Matt Helsleyf1974222020-05-29 14:01:13 -07001221 text_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +01001222 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -07001223 if (!text_reloc || text_reloc->sym->type != STT_SECTION ||
1224 !text_reloc->sym->sec->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001225 continue;
1226
Matt Helsleyf1974222020-05-29 14:01:13 -07001227 table_offset = text_reloc->addend;
1228 table_sec = text_reloc->sym->sec;
Allan Xavier4a60aa02018-09-07 08:12:01 -05001229
Matt Helsleyf1974222020-05-29 14:01:13 -07001230 if (text_reloc->type == R_X86_64_PC32)
Josh Poimboeuf6f5ec292018-05-14 08:53:24 -05001231 table_offset += 4;
1232
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001233 /*
1234 * Make sure the .rodata address isn't associated with a
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001235 * symbol. GCC jump tables are anonymous data.
1236 *
1237 * Also support C jump tables which are in the same format as
1238 * switch jump tables. For objtool to recognize them, they
1239 * need to be placed in the C_JUMP_TABLE_SECTION section. They
1240 * have symbols associated with them.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001241 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001242 if (find_symbol_containing(table_sec, table_offset) &&
1243 strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001244 continue;
1245
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001246 /*
Matt Helsleyf1974222020-05-29 14:01:13 -07001247 * Each table entry has a reloc associated with it. The reloc
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001248 * should reference text in the same function as the original
1249 * instruction.
1250 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001251 table_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset);
1252 if (!table_reloc)
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001253 continue;
Matt Helsleyf1974222020-05-29 14:01:13 -07001254 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001255 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1256 continue;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001257
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001258 /*
1259 * Use of RIP-relative switch jumps is quite rare, and
1260 * indicates a rare GCC quirk/bug which can leave dead code
1261 * behind.
1262 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001263 if (text_reloc->type == R_X86_64_PC32)
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001264 file->ignore_unreachables = true;
1265
Matt Helsleyf1974222020-05-29 14:01:13 -07001266 return table_reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001267 }
1268
1269 return NULL;
1270}
1271
Jann Hornbd98c812019-07-17 20:36:54 -05001272/*
1273 * First pass: Mark the head of each jump table so that in the next pass,
1274 * we know when a given jump table ends and the next one starts.
1275 */
1276static void mark_func_jump_tables(struct objtool_file *file,
1277 struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001278{
Jann Hornbd98c812019-07-17 20:36:54 -05001279 struct instruction *insn, *last = NULL;
Matt Helsleyf1974222020-05-29 14:01:13 -07001280 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001281
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001282 func_for_each_insn(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001283 if (!last)
1284 last = insn;
1285
1286 /*
1287 * Store back-pointers for unconditional forward jumps such
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001288 * that find_jump_table() can back-track using those and
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001289 * avoid some potentially confusing code.
1290 */
1291 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1292 insn->offset > last->offset &&
1293 insn->jump_dest->offset > insn->offset &&
1294 !insn->jump_dest->first_jump_src) {
1295
1296 insn->jump_dest->first_jump_src = insn;
1297 last = insn->jump_dest;
1298 }
1299
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001300 if (insn->type != INSN_JUMP_DYNAMIC)
1301 continue;
1302
Matt Helsleyf1974222020-05-29 14:01:13 -07001303 reloc = find_jump_table(file, func, insn);
1304 if (reloc) {
1305 reloc->jump_table_start = true;
1306 insn->jump_table = reloc;
Jann Hornbd98c812019-07-17 20:36:54 -05001307 }
1308 }
1309}
1310
1311static int add_func_jump_tables(struct objtool_file *file,
1312 struct symbol *func)
1313{
1314 struct instruction *insn;
1315 int ret;
1316
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001317 func_for_each_insn(file, func, insn) {
Jann Hornbd98c812019-07-17 20:36:54 -05001318 if (!insn->jump_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001319 continue;
1320
Jann Hornbd98c812019-07-17 20:36:54 -05001321 ret = add_jump_table(file, insn, insn->jump_table);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001322 if (ret)
1323 return ret;
1324 }
1325
1326 return 0;
1327}
1328
1329/*
1330 * For some switch statements, gcc generates a jump table in the .rodata
1331 * section which contains a list of addresses within the function to jump to.
1332 * This finds these jump tables and adds them to the insn->alts lists.
1333 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001334static int add_jump_table_alts(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001335{
1336 struct section *sec;
1337 struct symbol *func;
1338 int ret;
1339
Allan Xavier4a60aa02018-09-07 08:12:01 -05001340 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001341 return 0;
1342
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001343 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001344 list_for_each_entry(func, &sec->symbol_list, list) {
1345 if (func->type != STT_FUNC)
1346 continue;
1347
Jann Hornbd98c812019-07-17 20:36:54 -05001348 mark_func_jump_tables(file, func);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001349 ret = add_func_jump_tables(file, func);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001350 if (ret)
1351 return ret;
1352 }
1353 }
1354
1355 return 0;
1356}
1357
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001358static int read_unwind_hints(struct objtool_file *file)
1359{
Matt Helsleyf1974222020-05-29 14:01:13 -07001360 struct section *sec, *relocsec;
1361 struct reloc *reloc;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001362 struct unwind_hint *hint;
1363 struct instruction *insn;
1364 struct cfi_reg *cfa;
1365 int i;
1366
1367 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1368 if (!sec)
1369 return 0;
1370
Matt Helsleyf1974222020-05-29 14:01:13 -07001371 relocsec = sec->reloc;
1372 if (!relocsec) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001373 WARN("missing .rela.discard.unwind_hints section");
1374 return -1;
1375 }
1376
1377 if (sec->len % sizeof(struct unwind_hint)) {
1378 WARN("struct unwind_hint size mismatch");
1379 return -1;
1380 }
1381
1382 file->hints = true;
1383
1384 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1385 hint = (struct unwind_hint *)sec->data->d_buf + i;
1386
Matt Helsleyf1974222020-05-29 14:01:13 -07001387 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1388 if (!reloc) {
1389 WARN("can't find reloc for unwind_hints[%d]", i);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001390 return -1;
1391 }
1392
Matt Helsleyf1974222020-05-29 14:01:13 -07001393 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001394 if (!insn) {
1395 WARN("can't find insn for unwind_hints[%d]", i);
1396 return -1;
1397 }
1398
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001399 cfa = &insn->cfi.cfa;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001400
Peter Zijlstrac536ed22020-04-01 16:54:26 +02001401 if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001402 insn->ret_offset = hint->sp_offset;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001403 continue;
1404 }
1405
1406 insn->hint = true;
1407
1408 switch (hint->sp_reg) {
1409 case ORC_REG_UNDEFINED:
1410 cfa->base = CFI_UNDEFINED;
1411 break;
1412 case ORC_REG_SP:
1413 cfa->base = CFI_SP;
1414 break;
1415 case ORC_REG_BP:
1416 cfa->base = CFI_BP;
1417 break;
1418 case ORC_REG_SP_INDIRECT:
1419 cfa->base = CFI_SP_INDIRECT;
1420 break;
1421 case ORC_REG_R10:
1422 cfa->base = CFI_R10;
1423 break;
1424 case ORC_REG_R13:
1425 cfa->base = CFI_R13;
1426 break;
1427 case ORC_REG_DI:
1428 cfa->base = CFI_DI;
1429 break;
1430 case ORC_REG_DX:
1431 cfa->base = CFI_DX;
1432 break;
1433 default:
1434 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1435 insn->sec, insn->offset, hint->sp_reg);
1436 return -1;
1437 }
1438
1439 cfa->offset = hint->sp_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001440 insn->cfi.type = hint->type;
1441 insn->cfi.end = hint->end;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001442 }
1443
1444 return 0;
1445}
1446
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001447static int read_retpoline_hints(struct objtool_file *file)
1448{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001449 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001450 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001451 struct reloc *reloc;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001452
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001453 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001454 if (!sec)
1455 return 0;
1456
Matt Helsleyf1974222020-05-29 14:01:13 -07001457 list_for_each_entry(reloc, &sec->reloc_list, list) {
1458 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001459 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001460 return -1;
1461 }
1462
Matt Helsleyf1974222020-05-29 14:01:13 -07001463 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001464 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001465 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001466 return -1;
1467 }
1468
1469 if (insn->type != INSN_JUMP_DYNAMIC &&
1470 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001471 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001472 insn->sec, insn->offset);
1473 return -1;
1474 }
1475
1476 insn->retpoline_safe = true;
1477 }
1478
1479 return 0;
1480}
1481
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001482static int read_instr_hints(struct objtool_file *file)
1483{
1484 struct section *sec;
1485 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001486 struct reloc *reloc;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001487
1488 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1489 if (!sec)
1490 return 0;
1491
Matt Helsleyf1974222020-05-29 14:01:13 -07001492 list_for_each_entry(reloc, &sec->reloc_list, list) {
1493 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001494 WARN("unexpected relocation symbol type in %s", sec->name);
1495 return -1;
1496 }
1497
Matt Helsleyf1974222020-05-29 14:01:13 -07001498 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001499 if (!insn) {
1500 WARN("bad .discard.instr_end entry");
1501 return -1;
1502 }
1503
1504 insn->instr--;
1505 }
1506
1507 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1508 if (!sec)
1509 return 0;
1510
Matt Helsleyf1974222020-05-29 14:01:13 -07001511 list_for_each_entry(reloc, &sec->reloc_list, list) {
1512 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001513 WARN("unexpected relocation symbol type in %s", sec->name);
1514 return -1;
1515 }
1516
Matt Helsleyf1974222020-05-29 14:01:13 -07001517 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001518 if (!insn) {
1519 WARN("bad .discard.instr_begin entry");
1520 return -1;
1521 }
1522
1523 insn->instr++;
1524 }
1525
1526 return 0;
1527}
1528
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001529static int read_intra_function_calls(struct objtool_file *file)
1530{
1531 struct instruction *insn;
1532 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -07001533 struct reloc *reloc;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001534
1535 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1536 if (!sec)
1537 return 0;
1538
Matt Helsleyf1974222020-05-29 14:01:13 -07001539 list_for_each_entry(reloc, &sec->reloc_list, list) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001540 unsigned long dest_off;
1541
Matt Helsleyf1974222020-05-29 14:01:13 -07001542 if (reloc->sym->type != STT_SECTION) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001543 WARN("unexpected relocation symbol type in %s",
1544 sec->name);
1545 return -1;
1546 }
1547
Matt Helsleyf1974222020-05-29 14:01:13 -07001548 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001549 if (!insn) {
1550 WARN("bad .discard.intra_function_call entry");
1551 return -1;
1552 }
1553
1554 if (insn->type != INSN_CALL) {
1555 WARN_FUNC("intra_function_call not a direct call",
1556 insn->sec, insn->offset);
1557 return -1;
1558 }
1559
1560 /*
1561 * Treat intra-function CALLs as JMPs, but with a stack_op.
1562 * See add_call_destinations(), which strips stack_ops from
1563 * normal CALLs.
1564 */
1565 insn->type = INSN_JUMP_UNCONDITIONAL;
1566
1567 dest_off = insn->offset + insn->len + insn->immediate;
1568 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1569 if (!insn->jump_dest) {
1570 WARN_FUNC("can't find call dest at %s+0x%lx",
1571 insn->sec, insn->offset,
1572 insn->sec->name, dest_off);
1573 return -1;
1574 }
1575 }
1576
1577 return 0;
1578}
1579
Allan Xavier4a60aa02018-09-07 08:12:01 -05001580static void mark_rodata(struct objtool_file *file)
1581{
1582 struct section *sec;
1583 bool found = false;
1584
1585 /*
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001586 * Search for the following rodata sections, each of which can
1587 * potentially contain jump tables:
1588 *
1589 * - .rodata: can contain GCC switch tables
1590 * - .rodata.<func>: same, if -fdata-sections is being used
1591 * - .rodata..c_jump_table: contains C annotated jump tables
1592 *
1593 * .rodata.str1.* sections are ignored; they don't contain jump tables.
Allan Xavier4a60aa02018-09-07 08:12:01 -05001594 */
1595 for_each_sec(file, sec) {
Muchun Song1ee444702020-04-12 22:44:05 +08001596 if (!strncmp(sec->name, ".rodata", 7) &&
1597 !strstr(sec->name, ".str1.")) {
Allan Xavier4a60aa02018-09-07 08:12:01 -05001598 sec->rodata = true;
1599 found = true;
1600 }
1601 }
1602
1603 file->rodata = found;
1604}
1605
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001606static int decode_sections(struct objtool_file *file)
1607{
1608 int ret;
1609
Allan Xavier4a60aa02018-09-07 08:12:01 -05001610 mark_rodata(file);
1611
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001612 ret = decode_instructions(file);
1613 if (ret)
1614 return ret;
1615
1616 ret = add_dead_ends(file);
1617 if (ret)
1618 return ret;
1619
1620 add_ignores(file);
Peter Zijlstraea242132019-02-25 12:50:09 +01001621 add_uaccess_safe(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001622
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001623 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001624 if (ret)
1625 return ret;
1626
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001627 ret = add_jump_destinations(file);
1628 if (ret)
1629 return ret;
1630
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001631 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001632 if (ret)
1633 return ret;
1634
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001635 ret = read_intra_function_calls(file);
1636 if (ret)
1637 return ret;
1638
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001639 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001640 if (ret)
1641 return ret;
1642
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001643 ret = add_jump_table_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001644 if (ret)
1645 return ret;
1646
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001647 ret = read_unwind_hints(file);
1648 if (ret)
1649 return ret;
1650
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001651 ret = read_retpoline_hints(file);
1652 if (ret)
1653 return ret;
1654
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001655 ret = read_instr_hints(file);
1656 if (ret)
1657 return ret;
1658
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001659 return 0;
1660}
1661
1662static bool is_fentry_call(struct instruction *insn)
1663{
Alexandre Chartre87cf61f2020-04-14 12:36:10 +02001664 if (insn->type == INSN_CALL && insn->call_dest &&
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001665 insn->call_dest->type == STT_NOTYPE &&
1666 !strcmp(insn->call_dest->name, "__fentry__"))
1667 return true;
1668
1669 return false;
1670}
1671
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001672static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001673{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001674 u8 ret_offset = insn->ret_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001675 struct cfi_state *cfi = &state->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001676 int i;
1677
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001678 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001679 return true;
1680
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001681 if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001682 return true;
1683
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001684 if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001685 return true;
1686
Alexandre Chartrec721b3f2020-04-07 09:31:35 +02001687 /*
1688 * If there is a ret offset hint then don't check registers
1689 * because a callee-saved register might have been pushed on
1690 * the stack.
1691 */
1692 if (ret_offset)
1693 return false;
1694
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001695 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001696 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1697 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001698 return true;
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001699 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001700
1701 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001702}
1703
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001704static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001705{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001706 struct cfi_state *cfi = &state->cfi;
1707
1708 if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
1709 cfi->regs[CFI_BP].offset == -16)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001710 return true;
1711
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001712 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001713 return true;
1714
1715 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001716}
1717
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001718static int update_cfi_state_regs(struct instruction *insn,
1719 struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001720 struct stack_op *op)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001721{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001722 struct cfi_reg *cfa = &cfi->cfa;
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001723
Josh Poimboeufd8dd25a2020-04-25 05:03:00 -05001724 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001725 return 0;
1726
1727 /* push */
Peter Zijlstraea242132019-02-25 12:50:09 +01001728 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001729 cfa->offset += 8;
1730
1731 /* pop */
Peter Zijlstraea242132019-02-25 12:50:09 +01001732 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001733 cfa->offset -= 8;
1734
1735 /* add immediate to sp */
1736 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1737 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1738 cfa->offset -= op->src.offset;
1739
1740 return 0;
1741}
1742
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001743static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001744{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001745 if (arch_callee_saved_reg(reg) &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001746 cfi->regs[reg].base == CFI_UNDEFINED) {
1747 cfi->regs[reg].base = base;
1748 cfi->regs[reg].offset = offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001749 }
1750}
1751
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001752static void restore_reg(struct cfi_state *cfi, unsigned char reg)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001753{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001754 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1755 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001756}
1757
1758/*
1759 * A note about DRAP stack alignment:
1760 *
1761 * GCC has the concept of a DRAP register, which is used to help keep track of
1762 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1763 * register. The typical DRAP pattern is:
1764 *
1765 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1766 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1767 * 41 ff 72 f8 pushq -0x8(%r10)
1768 * 55 push %rbp
1769 * 48 89 e5 mov %rsp,%rbp
1770 * (more pushes)
1771 * 41 52 push %r10
1772 * ...
1773 * 41 5a pop %r10
1774 * (more pops)
1775 * 5d pop %rbp
1776 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1777 * c3 retq
1778 *
1779 * There are some variations in the epilogues, like:
1780 *
1781 * 5b pop %rbx
1782 * 41 5a pop %r10
1783 * 41 5c pop %r12
1784 * 41 5d pop %r13
1785 * 41 5e pop %r14
1786 * c9 leaveq
1787 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1788 * c3 retq
1789 *
1790 * and:
1791 *
1792 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1793 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1794 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1795 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1796 * c9 leaveq
1797 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1798 * c3 retq
1799 *
1800 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1801 * restored beforehand:
1802 *
1803 * 41 55 push %r13
1804 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1805 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1806 * ...
1807 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1808 * 41 5d pop %r13
1809 * c3 retq
1810 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001811static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001812 struct stack_op *op)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001813{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001814 struct cfi_reg *cfa = &cfi->cfa;
1815 struct cfi_reg *regs = cfi->regs;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001816
1817 /* stack operations don't make sense with an undefined CFA */
1818 if (cfa->base == CFI_UNDEFINED) {
1819 if (insn->func) {
1820 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1821 return -1;
1822 }
1823 return 0;
1824 }
1825
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001826 if (cfi->type == ORC_TYPE_REGS || cfi->type == ORC_TYPE_REGS_IRET)
1827 return update_cfi_state_regs(insn, cfi, op);
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001828
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001829 switch (op->dest.type) {
1830
1831 case OP_DEST_REG:
1832 switch (op->src.type) {
1833
1834 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001835 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1836 cfa->base == CFI_SP &&
1837 regs[CFI_BP].base == CFI_CFA &&
1838 regs[CFI_BP].offset == -cfa->offset) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001839
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001840 /* mov %rsp, %rbp */
1841 cfa->base = op->dest.reg;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001842 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001843 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001844
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001845 else if (op->src.reg == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001846 op->dest.reg == CFI_BP && cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001847
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001848 /* drap: mov %rsp, %rbp */
1849 regs[CFI_BP].base = CFI_BP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001850 regs[CFI_BP].offset = -cfi->stack_size;
1851 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001852 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001853
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001854 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1855
1856 /*
1857 * mov %rsp, %reg
1858 *
1859 * This is needed for the rare case where GCC
1860 * does:
1861 *
1862 * mov %rsp, %rax
1863 * ...
1864 * mov %rax, %rsp
1865 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001866 cfi->vals[op->dest.reg].base = CFI_CFA;
1867 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001868 }
1869
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001870 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1871 cfa->base == CFI_BP) {
1872
1873 /*
1874 * mov %rbp, %rsp
1875 *
1876 * Restore the original stack pointer (Clang).
1877 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001878 cfi->stack_size = -cfi->regs[CFI_BP].offset;
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001879 }
1880
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001881 else if (op->dest.reg == cfa->base) {
1882
1883 /* mov %reg, %rsp */
1884 if (cfa->base == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001885 cfi->vals[op->src.reg].base == CFI_CFA) {
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001886
1887 /*
1888 * This is needed for the rare case
1889 * where GCC does something dumb like:
1890 *
1891 * lea 0x8(%rsp), %rcx
1892 * ...
1893 * mov %rcx, %rsp
1894 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001895 cfa->offset = -cfi->vals[op->src.reg].offset;
1896 cfi->stack_size = cfa->offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001897
1898 } else {
1899 cfa->base = CFI_UNDEFINED;
1900 cfa->offset = 0;
1901 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001902 }
1903
1904 break;
1905
1906 case OP_SRC_ADD:
1907 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1908
1909 /* add imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001910 cfi->stack_size -= op->src.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001911 if (cfa->base == CFI_SP)
1912 cfa->offset -= op->src.offset;
1913 break;
1914 }
1915
1916 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1917
1918 /* lea disp(%rbp), %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001919 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001920 break;
1921 }
1922
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001923 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001924
1925 /* drap: lea disp(%rsp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001926 cfi->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001927
1928 /*
1929 * lea disp(%rsp), %reg
1930 *
1931 * This is needed for the rare case where GCC
1932 * does something dumb like:
1933 *
1934 * lea 0x8(%rsp), %rcx
1935 * ...
1936 * mov %rcx, %rsp
1937 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001938 cfi->vals[op->dest.reg].base = CFI_CFA;
1939 cfi->vals[op->dest.reg].offset = \
1940 -cfi->stack_size + op->src.offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001941
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001942 break;
1943 }
1944
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001945 if (cfi->drap && op->dest.reg == CFI_SP &&
1946 op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001947
1948 /* drap: lea disp(%drap), %rsp */
1949 cfa->base = CFI_SP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001950 cfa->offset = cfi->stack_size = -op->src.offset;
1951 cfi->drap_reg = CFI_UNDEFINED;
1952 cfi->drap = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001953 break;
1954 }
1955
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001956 if (op->dest.reg == cfi->cfa.base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001957 WARN_FUNC("unsupported stack register modification",
1958 insn->sec, insn->offset);
1959 return -1;
1960 }
1961
1962 break;
1963
1964 case OP_SRC_AND:
1965 if (op->dest.reg != CFI_SP ||
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001966 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1967 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001968 WARN_FUNC("unsupported stack pointer realignment",
1969 insn->sec, insn->offset);
1970 return -1;
1971 }
1972
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001973 if (cfi->drap_reg != CFI_UNDEFINED) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001974 /* drap: and imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001975 cfa->base = cfi->drap_reg;
1976 cfa->offset = cfi->stack_size = 0;
1977 cfi->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001978 }
1979
1980 /*
1981 * Older versions of GCC (4.8ish) realign the stack
1982 * without DRAP, with a frame pointer.
1983 */
1984
1985 break;
1986
1987 case OP_SRC_POP:
Peter Zijlstraea242132019-02-25 12:50:09 +01001988 case OP_SRC_POPF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001989 if (!cfi->drap && op->dest.reg == cfa->base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001990
1991 /* pop %rbp */
1992 cfa->base = CFI_SP;
1993 }
1994
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001995 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
1996 op->dest.reg == cfi->drap_reg &&
1997 cfi->drap_offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001998
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001999 /* drap: pop %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002000 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002001 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002002 cfi->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002003
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002004 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002005
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002006 /* pop %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002007 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002008 }
2009
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002010 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002011 if (cfa->base == CFI_SP)
2012 cfa->offset -= 8;
2013
2014 break;
2015
2016 case OP_SRC_REG_INDIRECT:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002017 if (cfi->drap && op->src.reg == CFI_BP &&
2018 op->src.offset == cfi->drap_offset) {
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002019
2020 /* drap: mov disp(%rbp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002021 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002022 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002023 cfi->drap_offset = -1;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002024 }
2025
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002026 if (cfi->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002027 op->src.offset == regs[op->dest.reg].offset) {
2028
2029 /* drap: mov disp(%rbp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002030 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002031
2032 } else if (op->src.reg == cfa->base &&
2033 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2034
2035 /* mov disp(%rbp), %reg */
2036 /* mov disp(%rsp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002037 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002038 }
2039
2040 break;
2041
2042 default:
2043 WARN_FUNC("unknown stack-related instruction",
2044 insn->sec, insn->offset);
2045 return -1;
2046 }
2047
2048 break;
2049
2050 case OP_DEST_PUSH:
Peter Zijlstraea242132019-02-25 12:50:09 +01002051 case OP_DEST_PUSHF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002052 cfi->stack_size += 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002053 if (cfa->base == CFI_SP)
2054 cfa->offset += 8;
2055
2056 if (op->src.type != OP_SRC_REG)
2057 break;
2058
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002059 if (cfi->drap) {
2060 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002061
2062 /* drap: push %drap */
2063 cfa->base = CFI_BP_INDIRECT;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002064 cfa->offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002065
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002066 /* save drap so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002067 cfi->drap_offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002068
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002069 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002070
2071 /* drap: push %rbp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002072 cfi->stack_size = 0;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002073
2074 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
2075
2076 /* drap: push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002077 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002078 }
2079
2080 } else {
2081
2082 /* push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002083 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002084 }
2085
2086 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002087 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002088 cfa->base != CFI_BP)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002089 cfi->bp_scratch = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002090 break;
2091
2092 case OP_DEST_REG_INDIRECT:
2093
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002094 if (cfi->drap) {
2095 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002096
2097 /* drap: mov %drap, disp(%rbp) */
2098 cfa->base = CFI_BP_INDIRECT;
2099 cfa->offset = op->dest.offset;
2100
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002101 /* save drap offset so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002102 cfi->drap_offset = op->dest.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002103 }
2104
2105 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
2106
2107 /* drap: mov reg, disp(%rbp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002108 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002109 }
2110
2111 } else if (op->dest.reg == cfa->base) {
2112
2113 /* mov reg, disp(%rbp) */
2114 /* mov reg, disp(%rsp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002115 save_reg(cfi, op->src.reg, CFI_CFA,
2116 op->dest.offset - cfi->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002117 }
2118
2119 break;
2120
2121 case OP_DEST_LEAVE:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002122 if ((!cfi->drap && cfa->base != CFI_BP) ||
2123 (cfi->drap && cfa->base != cfi->drap_reg)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002124 WARN_FUNC("leave instruction with modified stack frame",
2125 insn->sec, insn->offset);
2126 return -1;
2127 }
2128
2129 /* leave (mov %rbp, %rsp; pop %rbp) */
2130
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002131 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2132 restore_reg(cfi, CFI_BP);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002133
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002134 if (!cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002135 cfa->base = CFI_SP;
2136 cfa->offset -= 8;
2137 }
2138
2139 break;
2140
2141 case OP_DEST_MEM:
Peter Zijlstraea242132019-02-25 12:50:09 +01002142 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002143 WARN_FUNC("unknown stack-related memory operation",
2144 insn->sec, insn->offset);
2145 return -1;
2146 }
2147
2148 /* pop mem */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002149 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002150 if (cfa->base == CFI_SP)
2151 cfa->offset -= 8;
2152
2153 break;
2154
2155 default:
2156 WARN_FUNC("unknown stack-related instruction",
2157 insn->sec, insn->offset);
2158 return -1;
2159 }
2160
2161 return 0;
2162}
2163
Julien Thierry65ea47d2020-03-27 15:28:47 +00002164static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2165{
2166 struct stack_op *op;
2167
2168 list_for_each_entry(op, &insn->stack_ops, list) {
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002169 struct cfi_state old_cfi = state->cfi;
Julien Thierry65ea47d2020-03-27 15:28:47 +00002170 int res;
2171
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002172 res = update_cfi_state(insn, &state->cfi, op);
Julien Thierry65ea47d2020-03-27 15:28:47 +00002173 if (res)
2174 return res;
2175
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002176 if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) {
2177 WARN_FUNC("alternative modifies stack", insn->sec, insn->offset);
2178 return -1;
2179 }
2180
Julien Thierry65ea47d2020-03-27 15:28:47 +00002181 if (op->dest.type == OP_DEST_PUSHF) {
2182 if (!state->uaccess_stack) {
2183 state->uaccess_stack = 1;
2184 } else if (state->uaccess_stack >> 31) {
2185 WARN_FUNC("PUSHF stack exhausted",
2186 insn->sec, insn->offset);
2187 return 1;
2188 }
2189 state->uaccess_stack <<= 1;
2190 state->uaccess_stack |= state->uaccess;
2191 }
2192
2193 if (op->src.type == OP_SRC_POPF) {
2194 if (state->uaccess_stack) {
2195 state->uaccess = state->uaccess_stack & 1;
2196 state->uaccess_stack >>= 1;
2197 if (state->uaccess_stack == 1)
2198 state->uaccess_stack = 0;
2199 }
2200 }
2201 }
2202
2203 return 0;
2204}
2205
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002206static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002207{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002208 struct cfi_state *cfi1 = &insn->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002209 int i;
2210
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002211 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2212
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002213 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2214 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002215 cfi1->cfa.base, cfi1->cfa.offset,
2216 cfi2->cfa.base, cfi2->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002217
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002218 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002219 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002220 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002221 sizeof(struct cfi_reg)))
2222 continue;
2223
2224 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2225 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002226 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2227 i, cfi2->regs[i].base, cfi2->regs[i].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002228 break;
2229 }
2230
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002231 } else if (cfi1->type != cfi2->type) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002232
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002233 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2234 insn->sec, insn->offset, cfi1->type, cfi2->type);
2235
2236 } else if (cfi1->drap != cfi2->drap ||
2237 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2238 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2239
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002240 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002241 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002242 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2243 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002244
2245 } else
2246 return true;
2247
2248 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002249}
2250
Peter Zijlstraea242132019-02-25 12:50:09 +01002251static inline bool func_uaccess_safe(struct symbol *func)
2252{
2253 if (func)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002254 return func->uaccess_safe;
Peter Zijlstraea242132019-02-25 12:50:09 +01002255
2256 return false;
2257}
2258
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002259static inline const char *call_dest_name(struct instruction *insn)
Peter Zijlstraea242132019-02-25 12:50:09 +01002260{
2261 if (insn->call_dest)
2262 return insn->call_dest->name;
2263
2264 return "{dynamic}";
2265}
2266
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002267static inline bool noinstr_call_dest(struct symbol *func)
2268{
2269 /*
2270 * We can't deal with indirect function calls at present;
2271 * assume they're instrumented.
2272 */
2273 if (!func)
2274 return false;
2275
2276 /*
2277 * If the symbol is from a noinstr section; we good.
2278 */
2279 if (func->sec->noinstr)
2280 return true;
2281
2282 /*
2283 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2284 * something 'BAD' happened. At the risk of taking the machine down,
2285 * let them proceed to get the message out.
2286 */
2287 if (!strncmp(func->name, "__ubsan_handle_", 15))
2288 return true;
2289
2290 return false;
2291}
2292
Peter Zijlstraea242132019-02-25 12:50:09 +01002293static int validate_call(struct instruction *insn, struct insn_state *state)
2294{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002295 if (state->noinstr && state->instr <= 0 &&
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002296 !noinstr_call_dest(insn->call_dest)) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002297 WARN_FUNC("call to %s() leaves .noinstr.text section",
2298 insn->sec, insn->offset, call_dest_name(insn));
2299 return 1;
2300 }
2301
Peter Zijlstraea242132019-02-25 12:50:09 +01002302 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2303 WARN_FUNC("call to %s() with UACCESS enabled",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002304 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstraea242132019-02-25 12:50:09 +01002305 return 1;
2306 }
2307
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002308 if (state->df) {
2309 WARN_FUNC("call to %s() with DF set",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002310 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002311 return 1;
2312 }
2313
Peter Zijlstraea242132019-02-25 12:50:09 +01002314 return 0;
2315}
2316
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002317static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2318{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002319 if (has_modified_stack_frame(insn, state)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002320 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2321 insn->sec, insn->offset);
2322 return 1;
2323 }
2324
Peter Zijlstraea242132019-02-25 12:50:09 +01002325 return validate_call(insn, state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002326}
2327
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002328static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2329{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002330 if (state->noinstr && state->instr > 0) {
2331 WARN_FUNC("return with instrumentation enabled",
2332 insn->sec, insn->offset);
2333 return 1;
2334 }
2335
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002336 if (state->uaccess && !func_uaccess_safe(func)) {
2337 WARN_FUNC("return with UACCESS enabled",
2338 insn->sec, insn->offset);
2339 return 1;
2340 }
2341
2342 if (!state->uaccess && func_uaccess_safe(func)) {
2343 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2344 insn->sec, insn->offset);
2345 return 1;
2346 }
2347
2348 if (state->df) {
2349 WARN_FUNC("return with DF set",
2350 insn->sec, insn->offset);
2351 return 1;
2352 }
2353
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002354 if (func && has_modified_stack_frame(insn, state)) {
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002355 WARN_FUNC("return with modified stack frame",
2356 insn->sec, insn->offset);
2357 return 1;
2358 }
2359
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002360 if (state->cfi.bp_scratch) {
Josh Poimboeufb2966952020-04-01 13:23:29 -05002361 WARN_FUNC("BP used as a scratch register",
2362 insn->sec, insn->offset);
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002363 return 1;
2364 }
2365
2366 return 0;
2367}
2368
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002369/*
Peter Zijlstra7117f162020-04-28 19:37:01 +02002370 * Alternatives should not contain any ORC entries, this in turn means they
2371 * should not contain any CFI ops, which implies all instructions should have
2372 * the same same CFI state.
2373 *
2374 * It is possible to constuct alternatives that have unreachable holes that go
2375 * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2376 * states which then results in ORC entries, which we just said we didn't want.
2377 *
2378 * Avoid them by copying the CFI entry of the first instruction into the whole
2379 * alternative.
2380 */
2381static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn)
2382{
2383 struct instruction *first_insn = insn;
2384 int alt_group = insn->alt_group;
2385
2386 sec_for_each_insn_continue(file, insn) {
2387 if (insn->alt_group != alt_group)
2388 break;
2389 insn->cfi = first_insn->cfi;
2390 }
2391}
2392
2393/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002394 * Follow the branch starting at the given instruction, and recursively follow
2395 * any other branches (jumps). Meanwhile, track the frame pointer state at
2396 * each instruction and validate all the rules described in
2397 * tools/objtool/Documentation/stack-validation.txt.
2398 */
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002399static int validate_branch(struct objtool_file *file, struct symbol *func,
Peter Zijlstrab7460462020-04-02 10:15:51 +02002400 struct instruction *insn, struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002401{
2402 struct alternative *alt;
Peter Zijlstrab7460462020-04-02 10:15:51 +02002403 struct instruction *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002404 struct section *sec;
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002405 u8 visited;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002406 int ret;
2407
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002408 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002409
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002410 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002411 next_insn = next_insn_same_sec(file, insn);
2412
Josh Poimboeuf13810432018-05-09 22:39:15 -05002413 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05002414 WARN("%s() falls through to next function %s()",
2415 func->name, insn->func->name);
2416 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002417 }
2418
Josh Poimboeuf48550222017-07-07 09:19:42 -05002419 if (func && insn->ignore) {
2420 WARN_FUNC("BUG: why am I validating an ignored function?",
2421 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05002422 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05002423 }
2424
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002425 visited = 1 << state.uaccess;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002426 if (insn->visited) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002427 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002428 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002429
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002430 if (insn->visited & visited)
Peter Zijlstraea242132019-02-25 12:50:09 +01002431 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002432 }
2433
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002434 if (state.noinstr)
2435 state.instr += insn->instr;
2436
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002437 if (insn->hint)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002438 state.cfi = insn->cfi;
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002439 else
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002440 insn->cfi = state.cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002441
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002442 insn->visited |= visited;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002443
Peter Zijlstra7117f162020-04-28 19:37:01 +02002444 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002445 bool skip_orig = false;
2446
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002447 list_for_each_entry(alt, &insn->alts, list) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002448 if (alt->skip_orig)
2449 skip_orig = true;
2450
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002451 ret = validate_branch(file, func, alt->insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002452 if (ret) {
2453 if (backtrace)
2454 BT_FUNC("(alt)", insn);
2455 return ret;
2456 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002457 }
Peter Zijlstra764eef42019-03-01 11:19:03 +01002458
Peter Zijlstra7117f162020-04-28 19:37:01 +02002459 if (insn->alt_group)
2460 fill_alternative_cfi(file, insn);
2461
Peter Zijlstra764eef42019-03-01 11:19:03 +01002462 if (skip_orig)
2463 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002464 }
2465
Peter Zijlstra60041bc2020-04-24 16:16:41 +02002466 if (handle_insn_ops(insn, &state))
2467 return 1;
2468
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002469 switch (insn->type) {
2470
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002471 case INSN_RETURN:
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002472 return validate_return(func, insn, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002473
2474 case INSN_CALL:
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002475 case INSN_CALL_DYNAMIC:
Peter Zijlstraea242132019-02-25 12:50:09 +01002476 ret = validate_call(insn, &state);
2477 if (ret)
2478 return ret;
2479
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002480 if (!no_fp && func && !is_fentry_call(insn) &&
2481 !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002482 WARN_FUNC("call without frame pointer save/setup",
2483 sec, insn->offset);
2484 return 1;
2485 }
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002486
2487 if (dead_end_function(file, insn->call_dest))
2488 return 0;
2489
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002490 break;
2491
2492 case INSN_JUMP_CONDITIONAL:
2493 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002494 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002495 ret = validate_sibling_call(insn, &state);
2496 if (ret)
2497 return ret;
2498
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002499 } else if (insn->jump_dest) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002500 ret = validate_branch(file, func,
2501 insn->jump_dest, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002502 if (ret) {
2503 if (backtrace)
2504 BT_FUNC("(branch)", insn);
2505 return ret;
2506 }
Josh Poimboeuf48550222017-07-07 09:19:42 -05002507 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002508
2509 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2510 return 0;
2511
2512 break;
2513
2514 case INSN_JUMP_DYNAMIC:
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002515 case INSN_JUMP_DYNAMIC_CONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002516 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002517 ret = validate_sibling_call(insn, &state);
2518 if (ret)
2519 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002520 }
2521
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002522 if (insn->type == INSN_JUMP_DYNAMIC)
2523 return 0;
2524
2525 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002526
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002527 case INSN_CONTEXT_SWITCH:
2528 if (func && (!next_insn || !next_insn->hint)) {
2529 WARN_FUNC("unsupported instruction in callable function",
2530 sec, insn->offset);
2531 return 1;
2532 }
2533 return 0;
2534
Peter Zijlstraea242132019-02-25 12:50:09 +01002535 case INSN_STAC:
2536 if (state.uaccess) {
2537 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2538 return 1;
2539 }
2540
2541 state.uaccess = true;
2542 break;
2543
2544 case INSN_CLAC:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002545 if (!state.uaccess && func) {
Peter Zijlstraea242132019-02-25 12:50:09 +01002546 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2547 return 1;
2548 }
2549
2550 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2551 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2552 return 1;
2553 }
2554
2555 state.uaccess = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002556 break;
2557
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002558 case INSN_STD:
2559 if (state.df)
2560 WARN_FUNC("recursive STD", sec, insn->offset);
2561
2562 state.df = true;
2563 break;
2564
2565 case INSN_CLD:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002566 if (!state.df && func)
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002567 WARN_FUNC("redundant CLD", sec, insn->offset);
2568
2569 state.df = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002570 break;
2571
2572 default:
2573 break;
2574 }
2575
2576 if (insn->dead_end)
2577 return 0;
2578
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002579 if (!next_insn) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002580 if (state.cfi.cfa.base == CFI_UNDEFINED)
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002581 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002582 WARN("%s: unexpected end of section", sec->name);
2583 return 1;
2584 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002585
2586 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002587 }
2588
2589 return 0;
2590}
2591
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002592static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002593{
2594 struct instruction *insn;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002595 struct insn_state state;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002596 int ret, warnings = 0;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002597
2598 if (!file->hints)
2599 return 0;
2600
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002601 init_insn_state(&state, sec);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002602
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002603 if (sec) {
2604 insn = find_insn(file, sec, 0);
2605 if (!insn)
2606 return 0;
2607 } else {
2608 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2609 }
2610
2611 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002612 if (insn->hint && !insn->visited) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002613 ret = validate_branch(file, insn->func, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002614 if (ret && backtrace)
2615 BT_FUNC("<=== (hint)", insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002616 warnings += ret;
2617 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002618
2619 insn = list_next_entry(insn, list);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002620 }
2621
2622 return warnings;
2623}
2624
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002625static int validate_retpoline(struct objtool_file *file)
2626{
2627 struct instruction *insn;
2628 int warnings = 0;
2629
2630 for_each_insn(file, insn) {
2631 if (insn->type != INSN_JUMP_DYNAMIC &&
2632 insn->type != INSN_CALL_DYNAMIC)
2633 continue;
2634
2635 if (insn->retpoline_safe)
2636 continue;
2637
Peter Zijlstraca41b972018-01-31 10:18:28 +01002638 /*
2639 * .init.text code is ran before userspace and thus doesn't
2640 * strictly need retpolines, except for modules which are
2641 * loaded late, they very much do need retpoline in their
2642 * .init.text
2643 */
2644 if (!strcmp(insn->sec->name, ".init.text") && !module)
2645 continue;
2646
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002647 WARN_FUNC("indirect %s found in RETPOLINE build",
2648 insn->sec, insn->offset,
2649 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2650
2651 warnings++;
2652 }
2653
2654 return warnings;
2655}
2656
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002657static bool is_kasan_insn(struct instruction *insn)
2658{
2659 return (insn->type == INSN_CALL &&
2660 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2661}
2662
2663static bool is_ubsan_insn(struct instruction *insn)
2664{
2665 return (insn->type == INSN_CALL &&
2666 !strcmp(insn->call_dest->name,
2667 "__ubsan_handle_builtin_unreachable"));
2668}
2669
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002670static bool ignore_unreachable_insn(struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002671{
2672 int i;
2673
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002674 if (insn->ignore || insn->type == INSN_NOP)
2675 return true;
2676
2677 /*
2678 * Ignore any unused exceptions. This can happen when a whitelisted
2679 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002680 *
2681 * Also ignore alternative replacement instructions. This can happen
2682 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002683 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002684 if (!strcmp(insn->sec->name, ".fixup") ||
2685 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2686 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002687 return true;
2688
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002689 if (!insn->func)
2690 return false;
2691
2692 /*
2693 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2694 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2695 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2696 * (or occasionally a JMP to UD2).
2697 */
2698 if (list_prev_entry(insn, list)->dead_end &&
2699 (insn->type == INSN_BUG ||
2700 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2701 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2702 return true;
2703
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002704 /*
2705 * Check if this (or a subsequent) instruction is related to
2706 * CONFIG_UBSAN or CONFIG_KASAN.
2707 *
2708 * End the search at 5 instructions to avoid going into the weeds.
2709 */
2710 for (i = 0; i < 5; i++) {
2711
2712 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2713 return true;
2714
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002715 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2716 if (insn->jump_dest &&
2717 insn->jump_dest->func == insn->func) {
2718 insn = insn->jump_dest;
2719 continue;
2720 }
2721
2722 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002723 }
2724
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002725 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002726 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002727
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002728 insn = list_next_entry(insn, list);
2729 }
2730
2731 return false;
2732}
2733
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002734static int validate_symbol(struct objtool_file *file, struct section *sec,
2735 struct symbol *sym, struct insn_state *state)
2736{
2737 struct instruction *insn;
2738 int ret;
2739
2740 if (!sym->len) {
2741 WARN("%s() is missing an ELF size annotation", sym->name);
2742 return 1;
2743 }
2744
2745 if (sym->pfunc != sym || sym->alias != sym)
2746 return 0;
2747
2748 insn = find_insn(file, sec, sym->offset);
2749 if (!insn || insn->ignore || insn->visited)
2750 return 0;
2751
2752 state->uaccess = sym->uaccess_safe;
2753
2754 ret = validate_branch(file, insn->func, insn, *state);
2755 if (ret && backtrace)
2756 BT_FUNC("<=== (sym)", insn);
2757 return ret;
2758}
2759
Peter Zijlstra350994b2020-03-23 20:57:13 +01002760static int validate_section(struct objtool_file *file, struct section *sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002761{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002762 struct insn_state state;
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002763 struct symbol *func;
2764 int warnings = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002765
Peter Zijlstra350994b2020-03-23 20:57:13 +01002766 list_for_each_entry(func, &sec->symbol_list, list) {
2767 if (func->type != STT_FUNC)
2768 continue;
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002769
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002770 init_insn_state(&state, sec);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002771 state.cfi.cfa = initial_func_cfi.cfa;
2772 memcpy(&state.cfi.regs, &initial_func_cfi.regs,
Julien Thierry0699e552020-03-27 15:28:40 +00002773 CFI_NUM_REGS * sizeof(struct cfi_reg));
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002774 state.cfi.stack_size = initial_func_cfi.cfa.offset;
Julien Thierry0699e552020-03-27 15:28:40 +00002775
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002776 warnings += validate_symbol(file, sec, func, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002777 }
2778
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002779 return warnings;
2780}
2781
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002782static int validate_vmlinux_functions(struct objtool_file *file)
2783{
2784 struct section *sec;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002785 int warnings = 0;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002786
2787 sec = find_section_by_name(file->elf, ".noinstr.text");
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002788 if (sec) {
2789 warnings += validate_section(file, sec);
2790 warnings += validate_unwind_hints(file, sec);
2791 }
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002792
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002793 sec = find_section_by_name(file->elf, ".entry.text");
2794 if (sec) {
2795 warnings += validate_section(file, sec);
2796 warnings += validate_unwind_hints(file, sec);
2797 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002798
2799 return warnings;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002800}
2801
Peter Zijlstra350994b2020-03-23 20:57:13 +01002802static int validate_functions(struct objtool_file *file)
2803{
2804 struct section *sec;
2805 int warnings = 0;
2806
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002807 for_each_sec(file, sec) {
2808 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2809 continue;
2810
Peter Zijlstra350994b2020-03-23 20:57:13 +01002811 warnings += validate_section(file, sec);
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002812 }
Peter Zijlstra350994b2020-03-23 20:57:13 +01002813
2814 return warnings;
2815}
2816
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002817static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002818{
2819 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002820
2821 if (file->ignore_unreachables)
2822 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002823
2824 for_each_insn(file, insn) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002825 if (insn->visited || ignore_unreachable_insn(insn))
2826 continue;
2827
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002828 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2829 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002830 }
2831
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002832 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002833}
2834
Josh Poimboeuf0c671812019-03-18 19:09:38 -05002835static struct objtool_file file;
2836
Peter Zijlstra43a45252018-01-16 17:16:32 +01002837int check(const char *_objname, bool orc)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002838{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002839 int ret, warnings = 0;
2840
2841 objname = _objname;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002842
Peter Zijlstra2b10be22020-04-17 23:15:00 +02002843 file.elf = elf_open_read(objname, O_RDWR);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002844 if (!file.elf)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002845 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002846
2847 INIT_LIST_HEAD(&file.insn_list);
2848 hash_init(file.insn_hash);
Peter Zijlstra734d0992020-06-17 18:22:31 +02002849 file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment");
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002850 file.ignore_unreachables = no_unreachable;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002851 file.hints = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002852
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002853 arch_initial_func_cfi_state(&initial_func_cfi);
2854
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002855 ret = decode_sections(&file);
2856 if (ret < 0)
2857 goto out;
2858 warnings += ret;
2859
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002860 if (list_empty(&file.insn_list))
2861 goto out;
2862
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002863 if (vmlinux && !validate_dup) {
2864 ret = validate_vmlinux_functions(&file);
2865 if (ret < 0)
2866 goto out;
2867
2868 warnings += ret;
2869 goto out;
2870 }
2871
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002872 if (retpoline) {
2873 ret = validate_retpoline(&file);
2874 if (ret < 0)
2875 return ret;
2876 warnings += ret;
2877 }
2878
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002879 ret = validate_functions(&file);
2880 if (ret < 0)
2881 goto out;
2882 warnings += ret;
2883
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002884 ret = validate_unwind_hints(&file, NULL);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002885 if (ret < 0)
2886 goto out;
2887 warnings += ret;
2888
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002889 if (!warnings) {
2890 ret = validate_reachable_instructions(&file);
2891 if (ret < 0)
2892 goto out;
2893 warnings += ret;
2894 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002895
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002896 if (orc) {
2897 ret = create_orc(&file);
2898 if (ret < 0)
2899 goto out;
2900
2901 ret = create_orc_sections(&file);
2902 if (ret < 0)
2903 goto out;
Peter Zijlstra2b10be22020-04-17 23:15:00 +02002904 }
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002905
Peter Zijlstra2b10be22020-04-17 23:15:00 +02002906 if (file.elf->changed) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002907 ret = elf_write(file.elf);
2908 if (ret < 0)
2909 goto out;
2910 }
2911
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002912out:
Josh Poimboeuf644592d2020-02-10 12:32:38 -06002913 if (ret < 0) {
2914 /*
2915 * Fatal error. The binary is corrupt or otherwise broken in
2916 * some way, or objtool itself is broken. Fail the kernel
2917 * build.
2918 */
2919 return ret;
2920 }
2921
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002922 return 0;
2923}