blob: 9aa324b14d5d64dc31044aa1f57e79e450cc295c [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
Vasily Gorbik77860322020-11-13 00:03:32 +01009#include <arch/elf.h>
10#include <objtool/builtin.h>
11#include <objtool/cfi.h>
12#include <objtool/arch.h>
13#include <objtool/check.h>
14#include <objtool/special.h>
15#include <objtool/warn.h>
16#include <objtool/endianness.h>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050017
Julien Thierryee819ae2020-09-04 16:30:27 +010018#include <linux/objtool.h>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050019#include <linux/hashtable.h>
20#include <linux/kernel.h>
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +020021#include <linux/static_call_types.h>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050022
Josh Poimboeufe6da9562019-05-13 12:01:31 -050023#define FAKE_JUMP_OFFSET -1
24
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050025struct alternative {
26 struct list_head list;
27 struct instruction *insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +010028 bool skip_orig;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050029};
30
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 Poimboeuf0c1ddd32019-07-17 20:36:52 -0500113static bool is_sibling_call(struct instruction *insn)
114{
115 /* An indirect jump is either a sibling call or a jump to a table. */
116 if (insn->type == INSN_JUMP_DYNAMIC)
117 return list_empty(&insn->alts);
118
Josh Poimboeufa2296142020-02-10 12:32:39 -0600119 if (!is_static_jump(insn))
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500120 return false;
121
122 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
123 return !!insn->call_dest;
124}
125
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500126/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500127 * This checks to see if the given function is a "noreturn" function.
128 *
129 * For global functions which are outside the scope of this object file, we
130 * have to keep a manual list of them.
131 *
132 * For local functions, we have to detect them manually by simply looking for
133 * the lack of a return instruction.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500134 */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500135static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
136 int recursion)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500137{
138 int i;
139 struct instruction *insn;
140 bool empty = true;
141
142 /*
143 * Unfortunately these have to be hard coded because the noreturn
144 * attribute isn't provided in ELF data.
145 */
146 static const char * const global_noreturns[] = {
147 "__stack_chk_fail",
148 "panic",
149 "do_exit",
150 "do_task_dead",
151 "__module_put_and_exit",
152 "complete_and_exit",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500153 "__reiserfs_panic",
154 "lbug_with_loc",
155 "fortify_panic",
Kees Cookb394d462018-01-10 14:22:38 -0800156 "usercopy_abort",
Josh Poimboeuf684fb242018-06-19 10:47:50 -0500157 "machine_real_restart",
Josh Poimboeuf4fa5ecd2019-04-04 12:17:35 -0500158 "rewind_stack_do_exit",
Brendan Higgins33adf802019-09-23 02:02:38 -0700159 "kunit_try_catch_throw",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500160 };
161
Josh Poimboeufc9bab222019-07-17 20:36:51 -0500162 if (!func)
163 return false;
164
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500165 if (func->bind == STB_WEAK)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500166 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500167
168 if (func->bind == STB_GLOBAL)
169 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
170 if (!strcmp(func->name, global_noreturns[i]))
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500171 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500172
Josh Poimboeuf13810432018-05-09 22:39:15 -0500173 if (!func->len)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500174 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500175
Josh Poimboeuf13810432018-05-09 22:39:15 -0500176 insn = find_insn(file, func->sec, func->offset);
177 if (!insn->func)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500178 return false;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500179
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100180 func_for_each_insn(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500181 empty = false;
182
183 if (insn->type == INSN_RETURN)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500184 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500185 }
186
187 if (empty)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500188 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500189
190 /*
191 * A function can have a sibling call instead of a return. In that
192 * case, the function's dead-end status depends on whether the target
193 * of the sibling call returns.
194 */
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100195 func_for_each_insn(file, func, insn) {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500196 if (is_sibling_call(insn)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500197 struct instruction *dest = insn->jump_dest;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500198
199 if (!dest)
200 /* sibling call to another file */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500201 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500202
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500203 /* local sibling call */
204 if (recursion == 5) {
205 /*
206 * Infinite recursion: two functions have
207 * sibling calls to each other. This is a very
208 * rare case. It means they aren't dead ends.
209 */
210 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500211 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500212
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500213 return __dead_end_function(file, dest->func, recursion+1);
214 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500215 }
216
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500217 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500218}
219
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500220static bool dead_end_function(struct objtool_file *file, struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500221{
222 return __dead_end_function(file, func, 0);
223}
224
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100225static void init_cfi_state(struct cfi_state *cfi)
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500226{
227 int i;
228
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500229 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100230 cfi->regs[i].base = CFI_UNDEFINED;
231 cfi->vals[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500232 }
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100233 cfi->cfa.base = CFI_UNDEFINED;
234 cfi->drap_reg = CFI_UNDEFINED;
235 cfi->drap_offset = -1;
236}
237
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100238static void init_insn_state(struct insn_state *state, struct section *sec)
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100239{
240 memset(state, 0, sizeof(*state));
241 init_cfi_state(&state->cfi);
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100242
243 /*
244 * We need the full vmlinux for noinstr validation, otherwise we can
245 * not correctly determine insn->call_dest->sec (external symbols do
246 * not have a section).
247 */
248 if (vmlinux && sec)
249 state->noinstr = sec->noinstr;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500250}
251
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500252/*
253 * Call the arch-specific instruction decoder for all the instructions and add
254 * them to the global instruction list.
255 */
256static int decode_instructions(struct objtool_file *file)
257{
258 struct section *sec;
259 struct symbol *func;
260 unsigned long offset;
261 struct instruction *insn;
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100262 unsigned long nr_insns = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500263 int ret;
264
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500265 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500266
267 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
268 continue;
269
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500270 if (strcmp(sec->name, ".altinstr_replacement") &&
271 strcmp(sec->name, ".altinstr_aux") &&
272 strncmp(sec->name, ".discard.", 9))
273 sec->text = true;
274
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +0100275 if (!strcmp(sec->name, ".noinstr.text") ||
276 !strcmp(sec->name, ".entry.text"))
Peter Zijlstrac4a33932020-03-10 18:57:41 +0100277 sec->noinstr = true;
278
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500279 for (offset = 0; offset < sec->len; offset += insn->len) {
280 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500281 if (!insn) {
282 WARN("malloc failed");
283 return -1;
284 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500285 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500286 INIT_LIST_HEAD(&insn->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +0000287 INIT_LIST_HEAD(&insn->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100288 init_cfi_state(&insn->cfi);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500289
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500290 insn->sec = sec;
291 insn->offset = offset;
292
293 ret = arch_decode_instruction(file->elf, sec, offset,
294 sec->len - offset,
295 &insn->len, &insn->type,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500296 &insn->immediate,
Julien Thierry65ea47d2020-03-27 15:28:47 +0000297 &insn->stack_ops);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500298 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500299 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500300
Peter Zijlstra87ecb582020-03-16 15:47:27 +0100301 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500302 list_add_tail(&insn->list, &file->insn_list);
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100303 nr_insns++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500304 }
305
306 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500307 if (func->type != STT_FUNC || func->alias != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500308 continue;
309
310 if (!find_insn(file, sec, func->offset)) {
311 WARN("%s(): can't find starting instruction",
312 func->name);
313 return -1;
314 }
315
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100316 sym_for_each_insn(file, func, insn)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500317 insn->func = func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500318 }
319 }
320
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100321 if (stats)
322 printf("nr_insns: %lu\n", nr_insns);
323
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500324 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500325
326err:
327 free(insn);
328 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500329}
330
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700331static struct instruction *find_last_insn(struct objtool_file *file,
332 struct section *sec)
333{
334 struct instruction *insn = NULL;
335 unsigned int offset;
336 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
337
338 for (offset = sec->len - 1; offset >= end && !insn; offset--)
339 insn = find_insn(file, sec, offset);
340
341 return insn;
342}
343
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500344/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500345 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500346 */
347static int add_dead_ends(struct objtool_file *file)
348{
349 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700350 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500351 struct instruction *insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500352
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500353 /*
354 * By default, "ud2" is a dead end unless otherwise annotated, because
355 * GCC 7 inserts it for certain divide-by-zero cases.
356 */
357 for_each_insn(file, insn)
358 if (insn->type == INSN_BUG)
359 insn->dead_end = true;
360
361 /*
362 * Check for manually annotated dead ends.
363 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500364 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
365 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500366 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500367
Matt Helsleyf1974222020-05-29 14:01:13 -0700368 list_for_each_entry(reloc, &sec->reloc_list, list) {
369 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500370 WARN("unexpected relocation symbol type in %s", sec->name);
371 return -1;
372 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700373 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500374 if (insn)
375 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700376 else if (reloc->addend == reloc->sym->sec->len) {
377 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700378 if (!insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500379 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700380 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500381 return -1;
382 }
383 } else {
384 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700385 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500386 return -1;
387 }
388
389 insn->dead_end = true;
390 }
391
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500392reachable:
393 /*
394 * These manually annotated reachable checks are needed for GCC 4.4,
395 * where the Linux unreachable() macro isn't supported. In that case
396 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
397 * not a dead end.
398 */
399 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
400 if (!sec)
401 return 0;
402
Matt Helsleyf1974222020-05-29 14:01:13 -0700403 list_for_each_entry(reloc, &sec->reloc_list, list) {
404 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500405 WARN("unexpected relocation symbol type in %s", sec->name);
406 return -1;
407 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700408 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500409 if (insn)
410 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700411 else if (reloc->addend == reloc->sym->sec->len) {
412 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700413 if (!insn) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500414 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700415 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500416 return -1;
417 }
418 } else {
419 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700420 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500421 return -1;
422 }
423
424 insn->dead_end = false;
425 }
426
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500427 return 0;
428}
429
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200430static int create_static_call_sections(struct objtool_file *file)
431{
432 struct section *sec, *reloc_sec;
433 struct reloc *reloc;
434 struct static_call_site *site;
435 struct instruction *insn;
436 struct symbol *key_sym;
437 char *key_name, *tmp;
438 int idx;
439
440 sec = find_section_by_name(file->elf, ".static_call_sites");
441 if (sec) {
442 INIT_LIST_HEAD(&file->static_call_list);
443 WARN("file already has .static_call_sites section, skipping");
444 return 0;
445 }
446
447 if (list_empty(&file->static_call_list))
448 return 0;
449
450 idx = 0;
451 list_for_each_entry(insn, &file->static_call_list, static_call_node)
452 idx++;
453
454 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
455 sizeof(struct static_call_site), idx);
456 if (!sec)
457 return -1;
458
459 reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
460 if (!reloc_sec)
461 return -1;
462
463 idx = 0;
464 list_for_each_entry(insn, &file->static_call_list, static_call_node) {
465
466 site = (struct static_call_site *)sec->data->d_buf + idx;
467 memset(site, 0, sizeof(struct static_call_site));
468
469 /* populate reloc for 'addr' */
470 reloc = malloc(sizeof(*reloc));
Josh Poimboeuf44f6a7c2020-12-14 16:04:20 -0600471
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200472 if (!reloc) {
473 perror("malloc");
474 return -1;
475 }
476 memset(reloc, 0, sizeof(*reloc));
Josh Poimboeuf44f6a7c2020-12-14 16:04:20 -0600477
478 insn_to_reloc_sym_addend(insn->sec, insn->offset, reloc);
479 if (!reloc->sym) {
480 WARN_FUNC("static call tramp: missing containing symbol",
481 insn->sec, insn->offset);
482 return -1;
483 }
484
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200485 reloc->type = R_X86_64_PC32;
486 reloc->offset = idx * sizeof(struct static_call_site);
487 reloc->sec = reloc_sec;
488 elf_add_reloc(file->elf, reloc);
489
490 /* find key symbol */
491 key_name = strdup(insn->call_dest->name);
492 if (!key_name) {
493 perror("strdup");
494 return -1;
495 }
496 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
497 STATIC_CALL_TRAMP_PREFIX_LEN)) {
498 WARN("static_call: trampoline name malformed: %s", key_name);
499 return -1;
500 }
501 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
502 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
503
504 key_sym = find_symbol_by_name(file->elf, tmp);
505 if (!key_sym) {
506 WARN("static_call: can't find static_call_key symbol: %s", tmp);
507 return -1;
508 }
509 free(key_name);
510
511 /* populate reloc for 'key' */
512 reloc = malloc(sizeof(*reloc));
513 if (!reloc) {
514 perror("malloc");
515 return -1;
516 }
517 memset(reloc, 0, sizeof(*reloc));
518 reloc->sym = key_sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200519 reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200520 reloc->type = R_X86_64_PC32;
521 reloc->offset = idx * sizeof(struct static_call_site) + 4;
522 reloc->sec = reloc_sec;
523 elf_add_reloc(file->elf, reloc);
524
525 idx++;
526 }
527
528 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
529 return -1;
530
531 return 0;
532}
533
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500534/*
535 * Warnings shouldn't be reported for ignored functions.
536 */
537static void add_ignores(struct objtool_file *file)
538{
539 struct instruction *insn;
540 struct section *sec;
541 struct symbol *func;
Matt Helsleyf1974222020-05-29 14:01:13 -0700542 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500543
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100544 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
545 if (!sec)
546 return;
547
Matt Helsleyf1974222020-05-29 14:01:13 -0700548 list_for_each_entry(reloc, &sec->reloc_list, list) {
549 switch (reloc->sym->type) {
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100550 case STT_FUNC:
Matt Helsleyf1974222020-05-29 14:01:13 -0700551 func = reloc->sym;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100552 break;
553
554 case STT_SECTION:
Matt Helsleyf1974222020-05-29 14:01:13 -0700555 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600556 if (!func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500557 continue;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100558 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500559
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100560 default:
Matt Helsleyf1974222020-05-29 14:01:13 -0700561 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100562 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500563 }
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100564
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100565 func_for_each_insn(file, func, insn)
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100566 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500567 }
568}
569
570/*
Peter Zijlstraea242132019-02-25 12:50:09 +0100571 * This is a whitelist of functions that is allowed to be called with AC set.
572 * The list is meant to be minimal and only contains compiler instrumentation
573 * ABI and a few functions used to implement *_{to,from}_user() functions.
574 *
575 * These functions must not directly change AC, but may PUSHF/POPF.
576 */
577static const char *uaccess_safe_builtin[] = {
578 /* KASAN */
579 "kasan_report",
580 "check_memory_region",
581 /* KASAN out-of-line */
582 "__asan_loadN_noabort",
583 "__asan_load1_noabort",
584 "__asan_load2_noabort",
585 "__asan_load4_noabort",
586 "__asan_load8_noabort",
587 "__asan_load16_noabort",
588 "__asan_storeN_noabort",
589 "__asan_store1_noabort",
590 "__asan_store2_noabort",
591 "__asan_store4_noabort",
592 "__asan_store8_noabort",
593 "__asan_store16_noabort",
Jann Hornb0b8e562020-09-29 00:49:16 +0200594 "__kasan_check_read",
595 "__kasan_check_write",
Peter Zijlstraea242132019-02-25 12:50:09 +0100596 /* KASAN in-line */
597 "__asan_report_load_n_noabort",
598 "__asan_report_load1_noabort",
599 "__asan_report_load2_noabort",
600 "__asan_report_load4_noabort",
601 "__asan_report_load8_noabort",
602 "__asan_report_load16_noabort",
603 "__asan_report_store_n_noabort",
604 "__asan_report_store1_noabort",
605 "__asan_report_store2_noabort",
606 "__asan_report_store4_noabort",
607 "__asan_report_store8_noabort",
608 "__asan_report_store16_noabort",
Marco Elver5f5c9712019-11-14 19:02:57 +0100609 /* KCSAN */
Marco Elver99676832020-03-25 17:41:57 +0100610 "__kcsan_check_access",
Marco Elver5f5c9712019-11-14 19:02:57 +0100611 "kcsan_found_watchpoint",
612 "kcsan_setup_watchpoint",
Marco Elver99676832020-03-25 17:41:57 +0100613 "kcsan_check_scoped_accesses",
Marco Elver50a19ad2020-04-24 17:47:30 +0200614 "kcsan_disable_current",
615 "kcsan_enable_current_nowarn",
Marco Elver5f5c9712019-11-14 19:02:57 +0100616 /* KCSAN/TSAN */
617 "__tsan_func_entry",
618 "__tsan_func_exit",
619 "__tsan_read_range",
620 "__tsan_write_range",
621 "__tsan_read1",
622 "__tsan_read2",
623 "__tsan_read4",
624 "__tsan_read8",
625 "__tsan_read16",
626 "__tsan_write1",
627 "__tsan_write2",
628 "__tsan_write4",
629 "__tsan_write8",
630 "__tsan_write16",
Marco Elvera81b3752020-07-24 09:00:02 +0200631 "__tsan_read_write1",
632 "__tsan_read_write2",
633 "__tsan_read_write4",
634 "__tsan_read_write8",
635 "__tsan_read_write16",
Marco Elver883957b2020-07-03 15:40:30 +0200636 "__tsan_atomic8_load",
637 "__tsan_atomic16_load",
638 "__tsan_atomic32_load",
639 "__tsan_atomic64_load",
640 "__tsan_atomic8_store",
641 "__tsan_atomic16_store",
642 "__tsan_atomic32_store",
643 "__tsan_atomic64_store",
644 "__tsan_atomic8_exchange",
645 "__tsan_atomic16_exchange",
646 "__tsan_atomic32_exchange",
647 "__tsan_atomic64_exchange",
648 "__tsan_atomic8_fetch_add",
649 "__tsan_atomic16_fetch_add",
650 "__tsan_atomic32_fetch_add",
651 "__tsan_atomic64_fetch_add",
652 "__tsan_atomic8_fetch_sub",
653 "__tsan_atomic16_fetch_sub",
654 "__tsan_atomic32_fetch_sub",
655 "__tsan_atomic64_fetch_sub",
656 "__tsan_atomic8_fetch_and",
657 "__tsan_atomic16_fetch_and",
658 "__tsan_atomic32_fetch_and",
659 "__tsan_atomic64_fetch_and",
660 "__tsan_atomic8_fetch_or",
661 "__tsan_atomic16_fetch_or",
662 "__tsan_atomic32_fetch_or",
663 "__tsan_atomic64_fetch_or",
664 "__tsan_atomic8_fetch_xor",
665 "__tsan_atomic16_fetch_xor",
666 "__tsan_atomic32_fetch_xor",
667 "__tsan_atomic64_fetch_xor",
668 "__tsan_atomic8_fetch_nand",
669 "__tsan_atomic16_fetch_nand",
670 "__tsan_atomic32_fetch_nand",
671 "__tsan_atomic64_fetch_nand",
672 "__tsan_atomic8_compare_exchange_strong",
673 "__tsan_atomic16_compare_exchange_strong",
674 "__tsan_atomic32_compare_exchange_strong",
675 "__tsan_atomic64_compare_exchange_strong",
676 "__tsan_atomic8_compare_exchange_weak",
677 "__tsan_atomic16_compare_exchange_weak",
678 "__tsan_atomic32_compare_exchange_weak",
679 "__tsan_atomic64_compare_exchange_weak",
680 "__tsan_atomic8_compare_exchange_val",
681 "__tsan_atomic16_compare_exchange_val",
682 "__tsan_atomic32_compare_exchange_val",
683 "__tsan_atomic64_compare_exchange_val",
684 "__tsan_atomic_thread_fence",
685 "__tsan_atomic_signal_fence",
Peter Zijlstraea242132019-02-25 12:50:09 +0100686 /* KCOV */
687 "write_comp_data",
Josh Poimboeufae033f02020-04-29 14:09:04 -0500688 "check_kcov_mode",
Peter Zijlstraea242132019-02-25 12:50:09 +0100689 "__sanitizer_cov_trace_pc",
690 "__sanitizer_cov_trace_const_cmp1",
691 "__sanitizer_cov_trace_const_cmp2",
692 "__sanitizer_cov_trace_const_cmp4",
693 "__sanitizer_cov_trace_const_cmp8",
694 "__sanitizer_cov_trace_cmp1",
695 "__sanitizer_cov_trace_cmp2",
696 "__sanitizer_cov_trace_cmp4",
697 "__sanitizer_cov_trace_cmp8",
Al Viro36b1c702020-02-16 13:07:49 -0500698 "__sanitizer_cov_trace_switch",
Peter Zijlstraea242132019-02-25 12:50:09 +0100699 /* UBSAN */
700 "ubsan_type_mismatch_common",
701 "__ubsan_handle_type_mismatch",
702 "__ubsan_handle_type_mismatch_v1",
Peter Zijlstra9a50dca2019-10-21 15:11:49 +0200703 "__ubsan_handle_shift_out_of_bounds",
Peter Zijlstraea242132019-02-25 12:50:09 +0100704 /* misc */
705 "csum_partial_copy_generic",
Dan Williamsec6347b2020-10-05 20:40:16 -0700706 "copy_mc_fragile",
707 "copy_mc_fragile_handle_tail",
Dan Williams5da8e4a2020-10-05 20:40:25 -0700708 "copy_mc_enhanced_fast_string",
Peter Zijlstraea242132019-02-25 12:50:09 +0100709 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
710 NULL
711};
712
713static void add_uaccess_safe(struct objtool_file *file)
714{
715 struct symbol *func;
716 const char **name;
717
718 if (!uaccess)
719 return;
720
721 for (name = uaccess_safe_builtin; *name; name++) {
722 func = find_symbol_by_name(file->elf, *name);
723 if (!func)
724 continue;
725
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500726 func->uaccess_safe = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500727 }
728}
729
730/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000731 * FIXME: For now, just ignore any alternatives which add retpolines. This is
732 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
733 * But it at least allows objtool to understand the control flow *around* the
734 * retpoline.
735 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100736static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000737{
738 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700739 struct reloc *reloc;
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000740 struct instruction *insn;
741
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100742 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000743 if (!sec)
744 return 0;
745
Matt Helsleyf1974222020-05-29 14:01:13 -0700746 list_for_each_entry(reloc, &sec->reloc_list, list) {
747 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000748 WARN("unexpected relocation symbol type in %s", sec->name);
749 return -1;
750 }
751
Matt Helsleyf1974222020-05-29 14:01:13 -0700752 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000753 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100754 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000755 return -1;
756 }
757
758 insn->ignore_alts = true;
759 }
760
761 return 0;
762}
763
764/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500765 * Find the destination instructions for all jumps.
766 */
767static int add_jump_destinations(struct objtool_file *file)
768{
769 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -0700770 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500771 struct section *dest_sec;
772 unsigned long dest_off;
773
774 for_each_insn(file, insn) {
Josh Poimboeufa2296142020-02-10 12:32:39 -0600775 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500776 continue;
777
Josh Poimboeufdb6c6a02020-09-10 10:24:57 -0500778 if (insn->offset == FAKE_JUMP_OFFSET)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500779 continue;
780
Matt Helsleyf1974222020-05-29 14:01:13 -0700781 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100782 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700783 if (!reloc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500784 dest_sec = insn->sec;
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000785 dest_off = arch_jump_destination(insn);
Matt Helsleyf1974222020-05-29 14:01:13 -0700786 } else if (reloc->sym->type == STT_SECTION) {
787 dest_sec = reloc->sym->sec;
788 dest_off = arch_dest_reloc_offset(reloc->addend);
789 } else if (reloc->sym->sec->idx) {
790 dest_sec = reloc->sym->sec;
791 dest_off = reloc->sym->sym.st_value +
792 arch_dest_reloc_offset(reloc->addend);
793 } else if (strstr(reloc->sym->name, "_indirect_thunk_")) {
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000794 /*
795 * Retpoline jumps are really dynamic jumps in
796 * disguise, so convert them accordingly.
797 */
Josh Poimboeufb68b9902019-07-17 20:36:57 -0500798 if (insn->type == INSN_JUMP_UNCONDITIONAL)
799 insn->type = INSN_JUMP_DYNAMIC;
800 else
801 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
802
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100803 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000804 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500805 } else {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500806 /* external sibling call */
Matt Helsleyf1974222020-05-29 14:01:13 -0700807 insn->call_dest = reloc->sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200808 if (insn->call_dest->static_call_tramp) {
809 list_add_tail(&insn->static_call_node,
810 &file->static_call_list);
811 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500812 continue;
813 }
814
815 insn->jump_dest = find_insn(file, dest_sec, dest_off);
816 if (!insn->jump_dest) {
817
818 /*
819 * This is a special case where an alt instruction
820 * jumps past the end of the section. These are
821 * handled later in handle_group_alt().
822 */
823 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
824 continue;
825
826 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
827 insn->sec, insn->offset, dest_sec->name,
828 dest_off);
829 return -1;
830 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500831
832 /*
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100833 * Cross-function jump.
Josh Poimboeufcd778492018-06-01 07:23:51 -0500834 */
835 if (insn->func && insn->jump_dest->func &&
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100836 insn->func != insn->jump_dest->func) {
837
838 /*
839 * For GCC 8+, create parent/child links for any cold
840 * subfunctions. This is _mostly_ redundant with a
841 * similar initialization in read_symbols().
842 *
843 * If a function has aliases, we want the *first* such
844 * function in the symbol table to be the subfunction's
845 * parent. In that case we overwrite the
846 * initialization done in read_symbols().
847 *
848 * However this code can't completely replace the
849 * read_symbols() code because this doesn't detect the
850 * case where the parent function's only reference to a
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500851 * subfunction is through a jump table.
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100852 */
853 if (!strstr(insn->func->name, ".cold.") &&
854 strstr(insn->jump_dest->func->name, ".cold.")) {
855 insn->func->cfunc = insn->jump_dest->func;
856 insn->jump_dest->func->pfunc = insn->func;
857
858 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
859 insn->jump_dest->offset == insn->jump_dest->func->offset) {
860
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500861 /* internal sibling call */
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100862 insn->call_dest = insn->jump_dest->func;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200863 if (insn->call_dest->static_call_tramp) {
864 list_add_tail(&insn->static_call_node,
865 &file->static_call_list);
866 }
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100867 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500868 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500869 }
870
871 return 0;
872}
873
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200874static void remove_insn_ops(struct instruction *insn)
875{
876 struct stack_op *op, *tmp;
877
878 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
879 list_del(&op->list);
880 free(op);
881 }
882}
883
Julien Thierry2b232a22020-09-15 08:53:18 +0100884static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
885{
886 struct symbol *call_dest;
887
888 call_dest = find_func_by_offset(sec, offset);
889 if (!call_dest)
890 call_dest = find_symbol_by_offset(sec, offset);
891
892 return call_dest;
893}
894
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500895/*
896 * Find the destination instructions for all calls.
897 */
898static int add_call_destinations(struct objtool_file *file)
899{
900 struct instruction *insn;
901 unsigned long dest_off;
Matt Helsleyf1974222020-05-29 14:01:13 -0700902 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500903
904 for_each_insn(file, insn) {
905 if (insn->type != INSN_CALL)
906 continue;
907
Matt Helsleyf1974222020-05-29 14:01:13 -0700908 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100909 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700910 if (!reloc) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000911 dest_off = arch_jump_destination(insn);
Julien Thierry2b232a22020-09-15 08:53:18 +0100912 insn->call_dest = find_call_destination(insn->sec, dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600913
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600914 if (insn->ignore)
915 continue;
916
917 if (!insn->call_dest) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200918 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500919 return -1;
920 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600921
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600922 if (insn->func && insn->call_dest->type != STT_FUNC) {
923 WARN_FUNC("unsupported call to non-function",
924 insn->sec, insn->offset);
925 return -1;
926 }
927
Matt Helsleyf1974222020-05-29 14:01:13 -0700928 } else if (reloc->sym->type == STT_SECTION) {
929 dest_off = arch_dest_reloc_offset(reloc->addend);
Julien Thierry2b232a22020-09-15 08:53:18 +0100930 insn->call_dest = find_call_destination(reloc->sym->sec,
931 dest_off);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600932 if (!insn->call_dest) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000933 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500934 insn->sec, insn->offset,
Matt Helsleyf1974222020-05-29 14:01:13 -0700935 reloc->sym->sec->name,
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000936 dest_off);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500937 return -1;
938 }
939 } else
Matt Helsleyf1974222020-05-29 14:01:13 -0700940 insn->call_dest = reloc->sym;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200941
942 /*
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200943 * Many compilers cannot disable KCOV with a function attribute
944 * so they need a little help, NOP out any KCOV calls from noinstr
945 * text.
946 */
947 if (insn->sec->noinstr &&
948 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
Peter Zijlstrad832c002020-06-18 17:55:29 +0200949 if (reloc) {
950 reloc->type = R_NONE;
951 elf_write_reloc(file->elf, reloc);
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200952 }
953
954 elf_write_insn(file->elf, insn->sec,
955 insn->offset, insn->len,
956 arch_nop_insn(insn->len));
957 insn->type = INSN_NOP;
958 }
959
960 /*
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200961 * Whatever stack impact regular CALLs have, should be undone
962 * by the RETURN of the called function.
963 *
964 * Annotated intra-function calls retain the stack_ops but
965 * are converted to JUMP, see read_intra_function_calls().
966 */
967 remove_insn_ops(insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500968 }
969
970 return 0;
971}
972
973/*
974 * The .alternatives section requires some extra special care, over and above
975 * what other special sections require:
976 *
977 * 1. Because alternatives are patched in-place, we need to insert a fake jump
978 * instruction at the end so that validate_branch() skips all the original
979 * replaced instructions when validating the new instruction path.
980 *
981 * 2. An added wrinkle is that the new instruction length might be zero. In
982 * that case the old instructions are replaced with noops. We simulate that
983 * by creating a fake jump as the only new instruction.
984 *
985 * 3. In some cases, the alternative section includes an instruction which
986 * conditionally jumps to the _end_ of the entry. We have to modify these
987 * jumps' destinations to point back to .text rather than the end of the
988 * entry in .altinstr_replacement.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500989 */
990static int handle_group_alt(struct objtool_file *file,
991 struct special_alt *special_alt,
992 struct instruction *orig_insn,
993 struct instruction **new_insn)
994{
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600995 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Josh Poimboeufb23cc712020-12-18 14:19:32 -0600996 struct alt_group *orig_alt_group, *new_alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500997 unsigned long dest_off;
998
Josh Poimboeufb23cc712020-12-18 14:19:32 -0600999
1000 orig_alt_group = malloc(sizeof(*orig_alt_group));
1001 if (!orig_alt_group) {
1002 WARN("malloc failed");
1003 return -1;
1004 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001005 last_orig_insn = NULL;
1006 insn = orig_insn;
1007 sec_for_each_insn_from(file, insn) {
1008 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1009 break;
1010
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001011 insn->alt_group = orig_alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001012 last_orig_insn = insn;
1013 }
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001014 orig_alt_group->orig_group = NULL;
1015 orig_alt_group->first_insn = orig_insn;
1016 orig_alt_group->last_insn = last_orig_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001017
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001018 if (next_insn_same_sec(file, last_orig_insn)) {
1019 fake_jump = malloc(sizeof(*fake_jump));
1020 if (!fake_jump) {
1021 WARN("malloc failed");
1022 return -1;
1023 }
1024 memset(fake_jump, 0, sizeof(*fake_jump));
1025 INIT_LIST_HEAD(&fake_jump->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +00001026 INIT_LIST_HEAD(&fake_jump->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001027 init_cfi_state(&fake_jump->cfi);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001028
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001029 fake_jump->sec = special_alt->new_sec;
Josh Poimboeufe6da9562019-05-13 12:01:31 -05001030 fake_jump->offset = FAKE_JUMP_OFFSET;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001031 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
1032 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
Josh Poimboeufe6da9562019-05-13 12:01:31 -05001033 fake_jump->func = orig_insn->func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001034 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001035
1036 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001037 if (!fake_jump) {
1038 WARN("%s: empty alternative at end of section",
1039 special_alt->orig_sec->name);
1040 return -1;
1041 }
1042
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001043 *new_insn = fake_jump;
1044 return 0;
1045 }
1046
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001047 new_alt_group = malloc(sizeof(*new_alt_group));
1048 if (!new_alt_group) {
1049 WARN("malloc failed");
1050 return -1;
1051 }
1052
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001053 last_new_insn = NULL;
1054 insn = *new_insn;
1055 sec_for_each_insn_from(file, insn) {
Julien Thierry45245f52020-09-04 16:30:23 +01001056 struct reloc *alt_reloc;
1057
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001058 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1059 break;
1060
1061 last_new_insn = insn;
1062
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001063 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +01001064 insn->func = orig_insn->func;
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001065 insn->alt_group = new_alt_group;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001066
Josh Poimboeufdc419722020-02-10 12:32:40 -06001067 /*
1068 * Since alternative replacement code is copy/pasted by the
1069 * kernel after applying relocations, generally such code can't
1070 * have relative-address relocation references to outside the
1071 * .altinstr_replacement section, unless the arch's
1072 * alternatives code can adjust the relative offsets
1073 * accordingly.
Josh Poimboeufdc419722020-02-10 12:32:40 -06001074 */
Julien Thierry45245f52020-09-04 16:30:23 +01001075 alt_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1076 insn->offset, insn->len);
1077 if (alt_reloc &&
1078 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
Josh Poimboeufdc419722020-02-10 12:32:40 -06001079
1080 WARN_FUNC("unsupported relocation in alternatives section",
1081 insn->sec, insn->offset);
1082 return -1;
1083 }
1084
Josh Poimboeufa2296142020-02-10 12:32:39 -06001085 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001086 continue;
1087
1088 if (!insn->immediate)
1089 continue;
1090
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001091 dest_off = arch_jump_destination(insn);
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001092 if (dest_off == special_alt->new_off + special_alt->new_len) {
1093 if (!fake_jump) {
1094 WARN("%s: alternative jump to end of section",
1095 special_alt->orig_sec->name);
1096 return -1;
1097 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001098 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001099 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001100
1101 if (!insn->jump_dest) {
1102 WARN_FUNC("can't find alternative jump destination",
1103 insn->sec, insn->offset);
1104 return -1;
1105 }
1106 }
1107
1108 if (!last_new_insn) {
1109 WARN_FUNC("can't find last new alternative instruction",
1110 special_alt->new_sec, special_alt->new_off);
1111 return -1;
1112 }
1113
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001114 new_alt_group->orig_group = orig_alt_group;
1115 new_alt_group->first_insn = *new_insn;
1116 new_alt_group->last_insn = last_new_insn;
1117
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001118 if (fake_jump)
1119 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001120
1121 return 0;
1122}
1123
1124/*
1125 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1126 * If the original instruction is a jump, make the alt entry an effective nop
1127 * by just skipping the original instruction.
1128 */
1129static int handle_jump_alt(struct objtool_file *file,
1130 struct special_alt *special_alt,
1131 struct instruction *orig_insn,
1132 struct instruction **new_insn)
1133{
1134 if (orig_insn->type == INSN_NOP)
1135 return 0;
1136
1137 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1138 WARN_FUNC("unsupported instruction at jump label",
1139 orig_insn->sec, orig_insn->offset);
1140 return -1;
1141 }
1142
1143 *new_insn = list_next_entry(orig_insn, list);
1144 return 0;
1145}
1146
1147/*
1148 * Read all the special sections which have alternate instructions which can be
1149 * patched in or redirected to at runtime. Each instruction having alternate
1150 * instruction(s) has them added to its insn->alts list, which will be
1151 * traversed in validate_branch().
1152 */
1153static int add_special_section_alts(struct objtool_file *file)
1154{
1155 struct list_head special_alts;
1156 struct instruction *orig_insn, *new_insn;
1157 struct special_alt *special_alt, *tmp;
1158 struct alternative *alt;
1159 int ret;
1160
1161 ret = special_get_alts(file->elf, &special_alts);
1162 if (ret)
1163 return ret;
1164
1165 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001166
1167 orig_insn = find_insn(file, special_alt->orig_sec,
1168 special_alt->orig_off);
1169 if (!orig_insn) {
1170 WARN_FUNC("special: can't find orig instruction",
1171 special_alt->orig_sec, special_alt->orig_off);
1172 ret = -1;
1173 goto out;
1174 }
1175
1176 new_insn = NULL;
1177 if (!special_alt->group || special_alt->new_len) {
1178 new_insn = find_insn(file, special_alt->new_sec,
1179 special_alt->new_off);
1180 if (!new_insn) {
1181 WARN_FUNC("special: can't find new instruction",
1182 special_alt->new_sec,
1183 special_alt->new_off);
1184 ret = -1;
1185 goto out;
1186 }
1187 }
1188
1189 if (special_alt->group) {
Julien Thierry7170cf42020-03-27 15:28:41 +00001190 if (!special_alt->orig_len) {
1191 WARN_FUNC("empty alternative entry",
1192 orig_insn->sec, orig_insn->offset);
1193 continue;
1194 }
1195
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001196 ret = handle_group_alt(file, special_alt, orig_insn,
1197 &new_insn);
1198 if (ret)
1199 goto out;
1200 } else if (special_alt->jump_or_nop) {
1201 ret = handle_jump_alt(file, special_alt, orig_insn,
1202 &new_insn);
1203 if (ret)
1204 goto out;
1205 }
1206
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001207 alt = malloc(sizeof(*alt));
1208 if (!alt) {
1209 WARN("malloc failed");
1210 ret = -1;
1211 goto out;
1212 }
1213
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001214 alt->insn = new_insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +01001215 alt->skip_orig = special_alt->skip_orig;
Peter Zijlstraea242132019-02-25 12:50:09 +01001216 orig_insn->ignore_alts |= special_alt->skip_alt;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001217 list_add_tail(&alt->list, &orig_insn->alts);
1218
1219 list_del(&special_alt->list);
1220 free(special_alt);
1221 }
1222
1223out:
1224 return ret;
1225}
1226
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001227static int add_jump_table(struct objtool_file *file, struct instruction *insn,
Matt Helsleyf1974222020-05-29 14:01:13 -07001228 struct reloc *table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001229{
Matt Helsleyf1974222020-05-29 14:01:13 -07001230 struct reloc *reloc = table;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001231 struct instruction *dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001232 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001233 struct symbol *pfunc = insn->func->pfunc;
1234 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001235
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001236 /*
Matt Helsleyf1974222020-05-29 14:01:13 -07001237 * Each @reloc is a switch table relocation which points to the target
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001238 * instruction.
1239 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001240 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
Jann Hornbd98c812019-07-17 20:36:54 -05001241
1242 /* Check for the end of the table: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001243 if (reloc != table && reloc->jump_table_start)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001244 break;
1245
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001246 /* Make sure the table entries are consecutive: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001247 if (prev_offset && reloc->offset != prev_offset + 8)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001248 break;
1249
1250 /* Detect function pointers from contiguous objects: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001251 if (reloc->sym->sec == pfunc->sec &&
1252 reloc->addend == pfunc->offset)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001253 break;
1254
Matt Helsleyf1974222020-05-29 14:01:13 -07001255 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001256 if (!dest_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001257 break;
1258
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001259 /* Make sure the destination is in the same function: */
Josh Poimboeufe65050b2019-07-17 20:36:55 -05001260 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -05001261 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001262
1263 alt = malloc(sizeof(*alt));
1264 if (!alt) {
1265 WARN("malloc failed");
1266 return -1;
1267 }
1268
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001269 alt->insn = dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001270 list_add_tail(&alt->list, &insn->alts);
Matt Helsleyf1974222020-05-29 14:01:13 -07001271 prev_offset = reloc->offset;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001272 }
1273
1274 if (!prev_offset) {
1275 WARN_FUNC("can't find switch jump table",
1276 insn->sec, insn->offset);
1277 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001278 }
1279
1280 return 0;
1281}
1282
1283/*
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001284 * find_jump_table() - Given a dynamic jump, find the switch jump table
1285 * associated with it.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001286 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001287static struct reloc *find_jump_table(struct objtool_file *file,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001288 struct symbol *func,
1289 struct instruction *insn)
1290{
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001291 struct reloc *table_reloc;
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001292 struct instruction *dest_insn, *orig_insn = insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001293
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001294 /*
1295 * Backward search using the @first_jump_src links, these help avoid
1296 * much of the 'in between' code. Which avoids us getting confused by
1297 * it.
1298 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001299 for (;
Josh Poimboeuf1119d262020-04-28 16:45:16 -05001300 insn && insn->func && insn->func->pfunc == func;
1301 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001302
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001303 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001304 break;
1305
1306 /* allow small jumps within the range */
1307 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1308 insn->jump_dest &&
1309 (insn->jump_dest->offset <= insn->offset ||
1310 insn->jump_dest->offset > orig_insn->offset))
1311 break;
1312
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001313 table_reloc = arch_find_switch_table(file, insn);
Matt Helsleyf1974222020-05-29 14:01:13 -07001314 if (!table_reloc)
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001315 continue;
Matt Helsleyf1974222020-05-29 14:01:13 -07001316 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001317 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1318 continue;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001319
Matt Helsleyf1974222020-05-29 14:01:13 -07001320 return table_reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001321 }
1322
1323 return NULL;
1324}
1325
Jann Hornbd98c812019-07-17 20:36:54 -05001326/*
1327 * First pass: Mark the head of each jump table so that in the next pass,
1328 * we know when a given jump table ends and the next one starts.
1329 */
1330static void mark_func_jump_tables(struct objtool_file *file,
1331 struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001332{
Jann Hornbd98c812019-07-17 20:36:54 -05001333 struct instruction *insn, *last = NULL;
Matt Helsleyf1974222020-05-29 14:01:13 -07001334 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001335
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001336 func_for_each_insn(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001337 if (!last)
1338 last = insn;
1339
1340 /*
1341 * Store back-pointers for unconditional forward jumps such
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001342 * that find_jump_table() can back-track using those and
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001343 * avoid some potentially confusing code.
1344 */
1345 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1346 insn->offset > last->offset &&
1347 insn->jump_dest->offset > insn->offset &&
1348 !insn->jump_dest->first_jump_src) {
1349
1350 insn->jump_dest->first_jump_src = insn;
1351 last = insn->jump_dest;
1352 }
1353
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001354 if (insn->type != INSN_JUMP_DYNAMIC)
1355 continue;
1356
Matt Helsleyf1974222020-05-29 14:01:13 -07001357 reloc = find_jump_table(file, func, insn);
1358 if (reloc) {
1359 reloc->jump_table_start = true;
1360 insn->jump_table = reloc;
Jann Hornbd98c812019-07-17 20:36:54 -05001361 }
1362 }
1363}
1364
1365static int add_func_jump_tables(struct objtool_file *file,
1366 struct symbol *func)
1367{
1368 struct instruction *insn;
1369 int ret;
1370
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001371 func_for_each_insn(file, func, insn) {
Jann Hornbd98c812019-07-17 20:36:54 -05001372 if (!insn->jump_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001373 continue;
1374
Jann Hornbd98c812019-07-17 20:36:54 -05001375 ret = add_jump_table(file, insn, insn->jump_table);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001376 if (ret)
1377 return ret;
1378 }
1379
1380 return 0;
1381}
1382
1383/*
1384 * For some switch statements, gcc generates a jump table in the .rodata
1385 * section which contains a list of addresses within the function to jump to.
1386 * This finds these jump tables and adds them to the insn->alts lists.
1387 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001388static int add_jump_table_alts(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001389{
1390 struct section *sec;
1391 struct symbol *func;
1392 int ret;
1393
Allan Xavier4a60aa02018-09-07 08:12:01 -05001394 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001395 return 0;
1396
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001397 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001398 list_for_each_entry(func, &sec->symbol_list, list) {
1399 if (func->type != STT_FUNC)
1400 continue;
1401
Jann Hornbd98c812019-07-17 20:36:54 -05001402 mark_func_jump_tables(file, func);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001403 ret = add_func_jump_tables(file, func);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001404 if (ret)
1405 return ret;
1406 }
1407 }
1408
1409 return 0;
1410}
1411
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001412static int read_unwind_hints(struct objtool_file *file)
1413{
Matt Helsleyf1974222020-05-29 14:01:13 -07001414 struct section *sec, *relocsec;
1415 struct reloc *reloc;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001416 struct unwind_hint *hint;
1417 struct instruction *insn;
1418 struct cfi_reg *cfa;
1419 int i;
1420
1421 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1422 if (!sec)
1423 return 0;
1424
Matt Helsleyf1974222020-05-29 14:01:13 -07001425 relocsec = sec->reloc;
1426 if (!relocsec) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001427 WARN("missing .rela.discard.unwind_hints section");
1428 return -1;
1429 }
1430
1431 if (sec->len % sizeof(struct unwind_hint)) {
1432 WARN("struct unwind_hint size mismatch");
1433 return -1;
1434 }
1435
1436 file->hints = true;
1437
1438 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1439 hint = (struct unwind_hint *)sec->data->d_buf + i;
1440
Matt Helsleyf1974222020-05-29 14:01:13 -07001441 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1442 if (!reloc) {
1443 WARN("can't find reloc for unwind_hints[%d]", i);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001444 return -1;
1445 }
1446
Matt Helsleyf1974222020-05-29 14:01:13 -07001447 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001448 if (!insn) {
1449 WARN("can't find insn for unwind_hints[%d]", i);
1450 return -1;
1451 }
1452
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001453 cfa = &insn->cfi.cfa;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001454
Peter Zijlstrac536ed22020-04-01 16:54:26 +02001455 if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
Vasily Gorbik8bfe2732020-11-13 00:03:29 +01001456 insn->ret_offset = bswap_if_needed(hint->sp_offset);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001457 continue;
1458 }
1459
1460 insn->hint = true;
1461
Julien Thierryedea9e62020-09-04 16:30:28 +01001462 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001463 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1464 insn->sec, insn->offset, hint->sp_reg);
1465 return -1;
1466 }
1467
Vasily Gorbik8bfe2732020-11-13 00:03:29 +01001468 cfa->offset = bswap_if_needed(hint->sp_offset);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001469 insn->cfi.type = hint->type;
1470 insn->cfi.end = hint->end;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001471 }
1472
1473 return 0;
1474}
1475
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001476static int read_retpoline_hints(struct objtool_file *file)
1477{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001478 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001479 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001480 struct reloc *reloc;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001481
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001482 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001483 if (!sec)
1484 return 0;
1485
Matt Helsleyf1974222020-05-29 14:01:13 -07001486 list_for_each_entry(reloc, &sec->reloc_list, list) {
1487 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001488 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001489 return -1;
1490 }
1491
Matt Helsleyf1974222020-05-29 14:01:13 -07001492 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001493 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001494 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001495 return -1;
1496 }
1497
1498 if (insn->type != INSN_JUMP_DYNAMIC &&
1499 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001500 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001501 insn->sec, insn->offset);
1502 return -1;
1503 }
1504
1505 insn->retpoline_safe = true;
1506 }
1507
1508 return 0;
1509}
1510
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001511static int read_instr_hints(struct objtool_file *file)
1512{
1513 struct section *sec;
1514 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001515 struct reloc *reloc;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001516
1517 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1518 if (!sec)
1519 return 0;
1520
Matt Helsleyf1974222020-05-29 14:01:13 -07001521 list_for_each_entry(reloc, &sec->reloc_list, list) {
1522 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001523 WARN("unexpected relocation symbol type in %s", sec->name);
1524 return -1;
1525 }
1526
Matt Helsleyf1974222020-05-29 14:01:13 -07001527 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001528 if (!insn) {
1529 WARN("bad .discard.instr_end entry");
1530 return -1;
1531 }
1532
1533 insn->instr--;
1534 }
1535
1536 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1537 if (!sec)
1538 return 0;
1539
Matt Helsleyf1974222020-05-29 14:01:13 -07001540 list_for_each_entry(reloc, &sec->reloc_list, list) {
1541 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001542 WARN("unexpected relocation symbol type in %s", sec->name);
1543 return -1;
1544 }
1545
Matt Helsleyf1974222020-05-29 14:01:13 -07001546 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001547 if (!insn) {
1548 WARN("bad .discard.instr_begin entry");
1549 return -1;
1550 }
1551
1552 insn->instr++;
1553 }
1554
1555 return 0;
1556}
1557
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001558static int read_intra_function_calls(struct objtool_file *file)
1559{
1560 struct instruction *insn;
1561 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -07001562 struct reloc *reloc;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001563
1564 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1565 if (!sec)
1566 return 0;
1567
Matt Helsleyf1974222020-05-29 14:01:13 -07001568 list_for_each_entry(reloc, &sec->reloc_list, list) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001569 unsigned long dest_off;
1570
Matt Helsleyf1974222020-05-29 14:01:13 -07001571 if (reloc->sym->type != STT_SECTION) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001572 WARN("unexpected relocation symbol type in %s",
1573 sec->name);
1574 return -1;
1575 }
1576
Matt Helsleyf1974222020-05-29 14:01:13 -07001577 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001578 if (!insn) {
1579 WARN("bad .discard.intra_function_call entry");
1580 return -1;
1581 }
1582
1583 if (insn->type != INSN_CALL) {
1584 WARN_FUNC("intra_function_call not a direct call",
1585 insn->sec, insn->offset);
1586 return -1;
1587 }
1588
1589 /*
1590 * Treat intra-function CALLs as JMPs, but with a stack_op.
1591 * See add_call_destinations(), which strips stack_ops from
1592 * normal CALLs.
1593 */
1594 insn->type = INSN_JUMP_UNCONDITIONAL;
1595
1596 dest_off = insn->offset + insn->len + insn->immediate;
1597 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1598 if (!insn->jump_dest) {
1599 WARN_FUNC("can't find call dest at %s+0x%lx",
1600 insn->sec, insn->offset,
1601 insn->sec->name, dest_off);
1602 return -1;
1603 }
1604 }
1605
1606 return 0;
1607}
1608
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02001609static int read_static_call_tramps(struct objtool_file *file)
1610{
1611 struct section *sec;
1612 struct symbol *func;
1613
1614 for_each_sec(file, sec) {
1615 list_for_each_entry(func, &sec->symbol_list, list) {
1616 if (func->bind == STB_GLOBAL &&
1617 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1618 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1619 func->static_call_tramp = true;
1620 }
1621 }
1622
1623 return 0;
1624}
1625
Allan Xavier4a60aa02018-09-07 08:12:01 -05001626static void mark_rodata(struct objtool_file *file)
1627{
1628 struct section *sec;
1629 bool found = false;
1630
1631 /*
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001632 * Search for the following rodata sections, each of which can
1633 * potentially contain jump tables:
1634 *
1635 * - .rodata: can contain GCC switch tables
1636 * - .rodata.<func>: same, if -fdata-sections is being used
1637 * - .rodata..c_jump_table: contains C annotated jump tables
1638 *
1639 * .rodata.str1.* sections are ignored; they don't contain jump tables.
Allan Xavier4a60aa02018-09-07 08:12:01 -05001640 */
1641 for_each_sec(file, sec) {
Muchun Song1ee444702020-04-12 22:44:05 +08001642 if (!strncmp(sec->name, ".rodata", 7) &&
1643 !strstr(sec->name, ".str1.")) {
Allan Xavier4a60aa02018-09-07 08:12:01 -05001644 sec->rodata = true;
1645 found = true;
1646 }
1647 }
1648
1649 file->rodata = found;
1650}
1651
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001652static int decode_sections(struct objtool_file *file)
1653{
1654 int ret;
1655
Allan Xavier4a60aa02018-09-07 08:12:01 -05001656 mark_rodata(file);
1657
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001658 ret = decode_instructions(file);
1659 if (ret)
1660 return ret;
1661
1662 ret = add_dead_ends(file);
1663 if (ret)
1664 return ret;
1665
1666 add_ignores(file);
Peter Zijlstraea242132019-02-25 12:50:09 +01001667 add_uaccess_safe(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001668
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001669 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001670 if (ret)
1671 return ret;
1672
Peter Zijlstra5b06fd32020-08-18 15:57:49 +02001673 ret = read_static_call_tramps(file);
1674 if (ret)
1675 return ret;
1676
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001677 ret = add_jump_destinations(file);
1678 if (ret)
1679 return ret;
1680
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001681 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001682 if (ret)
1683 return ret;
1684
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001685 ret = read_intra_function_calls(file);
1686 if (ret)
1687 return ret;
1688
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001689 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001690 if (ret)
1691 return ret;
1692
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001693 ret = add_jump_table_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001694 if (ret)
1695 return ret;
1696
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001697 ret = read_unwind_hints(file);
1698 if (ret)
1699 return ret;
1700
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001701 ret = read_retpoline_hints(file);
1702 if (ret)
1703 return ret;
1704
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001705 ret = read_instr_hints(file);
1706 if (ret)
1707 return ret;
1708
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001709 return 0;
1710}
1711
1712static bool is_fentry_call(struct instruction *insn)
1713{
Alexandre Chartre87cf61f2020-04-14 12:36:10 +02001714 if (insn->type == INSN_CALL && insn->call_dest &&
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001715 insn->call_dest->type == STT_NOTYPE &&
1716 !strcmp(insn->call_dest->name, "__fentry__"))
1717 return true;
1718
1719 return false;
1720}
1721
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001722static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001723{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001724 u8 ret_offset = insn->ret_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001725 struct cfi_state *cfi = &state->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001726 int i;
1727
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001728 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001729 return true;
1730
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001731 if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001732 return true;
1733
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001734 if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001735 return true;
1736
Alexandre Chartrec721b3f2020-04-07 09:31:35 +02001737 /*
1738 * If there is a ret offset hint then don't check registers
1739 * because a callee-saved register might have been pushed on
1740 * the stack.
1741 */
1742 if (ret_offset)
1743 return false;
1744
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001745 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001746 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1747 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001748 return true;
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001749 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001750
1751 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001752}
1753
Julien Thierryfb084fd2020-10-14 08:38:00 +01001754static bool check_reg_frame_pos(const struct cfi_reg *reg,
1755 int expected_offset)
1756{
1757 return reg->base == CFI_CFA &&
1758 reg->offset == expected_offset;
1759}
1760
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001761static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001762{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001763 struct cfi_state *cfi = &state->cfi;
1764
Julien Thierryfb084fd2020-10-14 08:38:00 +01001765 if (cfi->cfa.base == CFI_BP &&
1766 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
1767 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001768 return true;
1769
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001770 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001771 return true;
1772
1773 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001774}
1775
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001776static int update_cfi_state_regs(struct instruction *insn,
1777 struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001778 struct stack_op *op)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001779{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001780 struct cfi_reg *cfa = &cfi->cfa;
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001781
Josh Poimboeufd8dd25a2020-04-25 05:03:00 -05001782 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001783 return 0;
1784
1785 /* push */
Peter Zijlstraea242132019-02-25 12:50:09 +01001786 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001787 cfa->offset += 8;
1788
1789 /* pop */
Peter Zijlstraea242132019-02-25 12:50:09 +01001790 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001791 cfa->offset -= 8;
1792
1793 /* add immediate to sp */
1794 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1795 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1796 cfa->offset -= op->src.offset;
1797
1798 return 0;
1799}
1800
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001801static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001802{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001803 if (arch_callee_saved_reg(reg) &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001804 cfi->regs[reg].base == CFI_UNDEFINED) {
1805 cfi->regs[reg].base = base;
1806 cfi->regs[reg].offset = offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001807 }
1808}
1809
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001810static void restore_reg(struct cfi_state *cfi, unsigned char reg)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001811{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001812 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1813 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001814}
1815
1816/*
1817 * A note about DRAP stack alignment:
1818 *
1819 * GCC has the concept of a DRAP register, which is used to help keep track of
1820 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1821 * register. The typical DRAP pattern is:
1822 *
1823 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1824 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1825 * 41 ff 72 f8 pushq -0x8(%r10)
1826 * 55 push %rbp
1827 * 48 89 e5 mov %rsp,%rbp
1828 * (more pushes)
1829 * 41 52 push %r10
1830 * ...
1831 * 41 5a pop %r10
1832 * (more pops)
1833 * 5d pop %rbp
1834 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1835 * c3 retq
1836 *
1837 * There are some variations in the epilogues, like:
1838 *
1839 * 5b pop %rbx
1840 * 41 5a pop %r10
1841 * 41 5c pop %r12
1842 * 41 5d pop %r13
1843 * 41 5e pop %r14
1844 * c9 leaveq
1845 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1846 * c3 retq
1847 *
1848 * and:
1849 *
1850 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1851 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1852 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1853 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1854 * c9 leaveq
1855 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1856 * c3 retq
1857 *
1858 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1859 * restored beforehand:
1860 *
1861 * 41 55 push %r13
1862 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1863 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1864 * ...
1865 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1866 * 41 5d pop %r13
1867 * c3 retq
1868 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001869static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001870 struct stack_op *op)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001871{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001872 struct cfi_reg *cfa = &cfi->cfa;
1873 struct cfi_reg *regs = cfi->regs;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001874
1875 /* stack operations don't make sense with an undefined CFA */
1876 if (cfa->base == CFI_UNDEFINED) {
1877 if (insn->func) {
1878 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1879 return -1;
1880 }
1881 return 0;
1882 }
1883
Julien Thierryee819ae2020-09-04 16:30:27 +01001884 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
1885 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001886 return update_cfi_state_regs(insn, cfi, op);
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001887
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001888 switch (op->dest.type) {
1889
1890 case OP_DEST_REG:
1891 switch (op->src.type) {
1892
1893 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001894 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1895 cfa->base == CFI_SP &&
Julien Thierryfb084fd2020-10-14 08:38:00 +01001896 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001897
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001898 /* mov %rsp, %rbp */
1899 cfa->base = op->dest.reg;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001900 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001901 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001902
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001903 else if (op->src.reg == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001904 op->dest.reg == CFI_BP && cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001905
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001906 /* drap: mov %rsp, %rbp */
1907 regs[CFI_BP].base = CFI_BP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001908 regs[CFI_BP].offset = -cfi->stack_size;
1909 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001910 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001911
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001912 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1913
1914 /*
1915 * mov %rsp, %reg
1916 *
1917 * This is needed for the rare case where GCC
1918 * does:
1919 *
1920 * mov %rsp, %rax
1921 * ...
1922 * mov %rax, %rsp
1923 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001924 cfi->vals[op->dest.reg].base = CFI_CFA;
1925 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001926 }
1927
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001928 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1929 cfa->base == CFI_BP) {
1930
1931 /*
1932 * mov %rbp, %rsp
1933 *
1934 * Restore the original stack pointer (Clang).
1935 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001936 cfi->stack_size = -cfi->regs[CFI_BP].offset;
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001937 }
1938
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001939 else if (op->dest.reg == cfa->base) {
1940
1941 /* mov %reg, %rsp */
1942 if (cfa->base == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001943 cfi->vals[op->src.reg].base == CFI_CFA) {
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001944
1945 /*
1946 * This is needed for the rare case
1947 * where GCC does something dumb like:
1948 *
1949 * lea 0x8(%rsp), %rcx
1950 * ...
1951 * mov %rcx, %rsp
1952 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001953 cfa->offset = -cfi->vals[op->src.reg].offset;
1954 cfi->stack_size = cfa->offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001955
1956 } else {
1957 cfa->base = CFI_UNDEFINED;
1958 cfa->offset = 0;
1959 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001960 }
1961
1962 break;
1963
1964 case OP_SRC_ADD:
1965 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1966
1967 /* add imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001968 cfi->stack_size -= op->src.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001969 if (cfa->base == CFI_SP)
1970 cfa->offset -= op->src.offset;
1971 break;
1972 }
1973
1974 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1975
1976 /* lea disp(%rbp), %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001977 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001978 break;
1979 }
1980
Julien Thierry468af562020-10-14 08:38:01 +01001981 if (!cfi->drap && op->src.reg == CFI_SP &&
1982 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
1983 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset + op->src.offset)) {
1984
1985 /* lea disp(%rsp), %rbp */
1986 cfa->base = CFI_BP;
1987 cfa->offset -= op->src.offset;
1988 cfi->bp_scratch = false;
1989 break;
1990 }
1991
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001992 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001993
1994 /* drap: lea disp(%rsp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001995 cfi->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001996
1997 /*
1998 * lea disp(%rsp), %reg
1999 *
2000 * This is needed for the rare case where GCC
2001 * does something dumb like:
2002 *
2003 * lea 0x8(%rsp), %rcx
2004 * ...
2005 * mov %rcx, %rsp
2006 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002007 cfi->vals[op->dest.reg].base = CFI_CFA;
2008 cfi->vals[op->dest.reg].offset = \
2009 -cfi->stack_size + op->src.offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002010
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002011 break;
2012 }
2013
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002014 if (cfi->drap && op->dest.reg == CFI_SP &&
2015 op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002016
2017 /* drap: lea disp(%drap), %rsp */
2018 cfa->base = CFI_SP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002019 cfa->offset = cfi->stack_size = -op->src.offset;
2020 cfi->drap_reg = CFI_UNDEFINED;
2021 cfi->drap = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002022 break;
2023 }
2024
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002025 if (op->dest.reg == cfi->cfa.base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002026 WARN_FUNC("unsupported stack register modification",
2027 insn->sec, insn->offset);
2028 return -1;
2029 }
2030
2031 break;
2032
2033 case OP_SRC_AND:
2034 if (op->dest.reg != CFI_SP ||
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002035 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2036 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002037 WARN_FUNC("unsupported stack pointer realignment",
2038 insn->sec, insn->offset);
2039 return -1;
2040 }
2041
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002042 if (cfi->drap_reg != CFI_UNDEFINED) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002043 /* drap: and imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002044 cfa->base = cfi->drap_reg;
2045 cfa->offset = cfi->stack_size = 0;
2046 cfi->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002047 }
2048
2049 /*
2050 * Older versions of GCC (4.8ish) realign the stack
2051 * without DRAP, with a frame pointer.
2052 */
2053
2054 break;
2055
2056 case OP_SRC_POP:
Peter Zijlstraea242132019-02-25 12:50:09 +01002057 case OP_SRC_POPF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002058 if (!cfi->drap && op->dest.reg == cfa->base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002059
2060 /* pop %rbp */
2061 cfa->base = CFI_SP;
2062 }
2063
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002064 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2065 op->dest.reg == cfi->drap_reg &&
2066 cfi->drap_offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002067
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002068 /* drap: pop %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002069 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002070 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002071 cfi->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002072
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002073 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002074
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002075 /* pop %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002076 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002077 }
2078
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002079 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002080 if (cfa->base == CFI_SP)
2081 cfa->offset -= 8;
2082
2083 break;
2084
2085 case OP_SRC_REG_INDIRECT:
Julien Thierry201ef5a2020-10-14 08:38:02 +01002086 if (!cfi->drap && op->dest.reg == cfa->base &&
2087 op->dest.reg == CFI_BP) {
2088
2089 /* mov disp(%rsp), %rbp */
2090 cfa->base = CFI_SP;
2091 cfa->offset = cfi->stack_size;
2092 }
2093
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002094 if (cfi->drap && op->src.reg == CFI_BP &&
2095 op->src.offset == cfi->drap_offset) {
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002096
2097 /* drap: mov disp(%rbp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002098 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002099 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002100 cfi->drap_offset = -1;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002101 }
2102
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002103 if (cfi->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002104 op->src.offset == regs[op->dest.reg].offset) {
2105
2106 /* drap: mov disp(%rbp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002107 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002108
2109 } else if (op->src.reg == cfa->base &&
2110 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2111
2112 /* mov disp(%rbp), %reg */
2113 /* mov disp(%rsp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002114 restore_reg(cfi, op->dest.reg);
Julien Thierry201ef5a2020-10-14 08:38:02 +01002115
2116 } else if (op->src.reg == CFI_SP &&
2117 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2118
2119 /* mov disp(%rsp), %reg */
2120 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002121 }
2122
2123 break;
2124
2125 default:
2126 WARN_FUNC("unknown stack-related instruction",
2127 insn->sec, insn->offset);
2128 return -1;
2129 }
2130
2131 break;
2132
2133 case OP_DEST_PUSH:
Peter Zijlstraea242132019-02-25 12:50:09 +01002134 case OP_DEST_PUSHF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002135 cfi->stack_size += 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002136 if (cfa->base == CFI_SP)
2137 cfa->offset += 8;
2138
2139 if (op->src.type != OP_SRC_REG)
2140 break;
2141
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002142 if (cfi->drap) {
2143 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002144
2145 /* drap: push %drap */
2146 cfa->base = CFI_BP_INDIRECT;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002147 cfa->offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002148
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002149 /* save drap so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002150 cfi->drap_offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002151
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002152 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002153
2154 /* drap: push %rbp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002155 cfi->stack_size = 0;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002156
Julien Thierryf4f80392020-09-15 08:53:16 +01002157 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002158
2159 /* drap: push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002160 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002161 }
2162
2163 } else {
2164
2165 /* push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002166 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002167 }
2168
2169 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002170 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002171 cfa->base != CFI_BP)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002172 cfi->bp_scratch = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002173 break;
2174
2175 case OP_DEST_REG_INDIRECT:
2176
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002177 if (cfi->drap) {
2178 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002179
2180 /* drap: mov %drap, disp(%rbp) */
2181 cfa->base = CFI_BP_INDIRECT;
2182 cfa->offset = op->dest.offset;
2183
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002184 /* save drap offset so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002185 cfi->drap_offset = op->dest.offset;
Julien Thierryf4f80392020-09-15 08:53:16 +01002186 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002187
2188 /* drap: mov reg, disp(%rbp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002189 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002190 }
2191
2192 } else if (op->dest.reg == cfa->base) {
2193
2194 /* mov reg, disp(%rbp) */
2195 /* mov reg, disp(%rsp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002196 save_reg(cfi, op->src.reg, CFI_CFA,
2197 op->dest.offset - cfi->cfa.offset);
Julien Thierry201ef5a2020-10-14 08:38:02 +01002198
2199 } else if (op->dest.reg == CFI_SP) {
2200
2201 /* mov reg, disp(%rsp) */
2202 save_reg(cfi, op->src.reg, CFI_CFA,
2203 op->dest.offset - cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002204 }
2205
2206 break;
2207
2208 case OP_DEST_LEAVE:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002209 if ((!cfi->drap && cfa->base != CFI_BP) ||
2210 (cfi->drap && cfa->base != cfi->drap_reg)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002211 WARN_FUNC("leave instruction with modified stack frame",
2212 insn->sec, insn->offset);
2213 return -1;
2214 }
2215
2216 /* leave (mov %rbp, %rsp; pop %rbp) */
2217
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002218 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2219 restore_reg(cfi, CFI_BP);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002220
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002221 if (!cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002222 cfa->base = CFI_SP;
2223 cfa->offset -= 8;
2224 }
2225
2226 break;
2227
2228 case OP_DEST_MEM:
Peter Zijlstraea242132019-02-25 12:50:09 +01002229 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002230 WARN_FUNC("unknown stack-related memory operation",
2231 insn->sec, insn->offset);
2232 return -1;
2233 }
2234
2235 /* pop mem */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002236 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002237 if (cfa->base == CFI_SP)
2238 cfa->offset -= 8;
2239
2240 break;
2241
2242 default:
2243 WARN_FUNC("unknown stack-related instruction",
2244 insn->sec, insn->offset);
2245 return -1;
2246 }
2247
2248 return 0;
2249}
2250
Julien Thierry65ea47d2020-03-27 15:28:47 +00002251static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2252{
2253 struct stack_op *op;
2254
2255 list_for_each_entry(op, &insn->stack_ops, list) {
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002256 struct cfi_state old_cfi = state->cfi;
Julien Thierry65ea47d2020-03-27 15:28:47 +00002257 int res;
2258
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002259 res = update_cfi_state(insn, &state->cfi, op);
Julien Thierry65ea47d2020-03-27 15:28:47 +00002260 if (res)
2261 return res;
2262
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002263 if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) {
2264 WARN_FUNC("alternative modifies stack", insn->sec, insn->offset);
2265 return -1;
2266 }
2267
Julien Thierry65ea47d2020-03-27 15:28:47 +00002268 if (op->dest.type == OP_DEST_PUSHF) {
2269 if (!state->uaccess_stack) {
2270 state->uaccess_stack = 1;
2271 } else if (state->uaccess_stack >> 31) {
2272 WARN_FUNC("PUSHF stack exhausted",
2273 insn->sec, insn->offset);
2274 return 1;
2275 }
2276 state->uaccess_stack <<= 1;
2277 state->uaccess_stack |= state->uaccess;
2278 }
2279
2280 if (op->src.type == OP_SRC_POPF) {
2281 if (state->uaccess_stack) {
2282 state->uaccess = state->uaccess_stack & 1;
2283 state->uaccess_stack >>= 1;
2284 if (state->uaccess_stack == 1)
2285 state->uaccess_stack = 0;
2286 }
2287 }
2288 }
2289
2290 return 0;
2291}
2292
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002293static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002294{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002295 struct cfi_state *cfi1 = &insn->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002296 int i;
2297
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002298 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2299
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002300 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2301 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002302 cfi1->cfa.base, cfi1->cfa.offset,
2303 cfi2->cfa.base, cfi2->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002304
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002305 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002306 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002307 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002308 sizeof(struct cfi_reg)))
2309 continue;
2310
2311 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2312 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002313 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2314 i, cfi2->regs[i].base, cfi2->regs[i].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002315 break;
2316 }
2317
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002318 } else if (cfi1->type != cfi2->type) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002319
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002320 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2321 insn->sec, insn->offset, cfi1->type, cfi2->type);
2322
2323 } else if (cfi1->drap != cfi2->drap ||
2324 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2325 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2326
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002327 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002328 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002329 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2330 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002331
2332 } else
2333 return true;
2334
2335 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002336}
2337
Peter Zijlstraea242132019-02-25 12:50:09 +01002338static inline bool func_uaccess_safe(struct symbol *func)
2339{
2340 if (func)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002341 return func->uaccess_safe;
Peter Zijlstraea242132019-02-25 12:50:09 +01002342
2343 return false;
2344}
2345
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002346static inline const char *call_dest_name(struct instruction *insn)
Peter Zijlstraea242132019-02-25 12:50:09 +01002347{
2348 if (insn->call_dest)
2349 return insn->call_dest->name;
2350
2351 return "{dynamic}";
2352}
2353
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002354static inline bool noinstr_call_dest(struct symbol *func)
2355{
2356 /*
2357 * We can't deal with indirect function calls at present;
2358 * assume they're instrumented.
2359 */
2360 if (!func)
2361 return false;
2362
2363 /*
2364 * If the symbol is from a noinstr section; we good.
2365 */
2366 if (func->sec->noinstr)
2367 return true;
2368
2369 /*
2370 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2371 * something 'BAD' happened. At the risk of taking the machine down,
2372 * let them proceed to get the message out.
2373 */
2374 if (!strncmp(func->name, "__ubsan_handle_", 15))
2375 return true;
2376
2377 return false;
2378}
2379
Peter Zijlstraea242132019-02-25 12:50:09 +01002380static int validate_call(struct instruction *insn, struct insn_state *state)
2381{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002382 if (state->noinstr && state->instr <= 0 &&
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002383 !noinstr_call_dest(insn->call_dest)) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002384 WARN_FUNC("call to %s() leaves .noinstr.text section",
2385 insn->sec, insn->offset, call_dest_name(insn));
2386 return 1;
2387 }
2388
Peter Zijlstraea242132019-02-25 12:50:09 +01002389 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2390 WARN_FUNC("call to %s() with UACCESS enabled",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002391 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstraea242132019-02-25 12:50:09 +01002392 return 1;
2393 }
2394
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002395 if (state->df) {
2396 WARN_FUNC("call to %s() with DF set",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002397 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002398 return 1;
2399 }
2400
Peter Zijlstraea242132019-02-25 12:50:09 +01002401 return 0;
2402}
2403
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002404static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2405{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002406 if (has_modified_stack_frame(insn, state)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002407 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2408 insn->sec, insn->offset);
2409 return 1;
2410 }
2411
Peter Zijlstraea242132019-02-25 12:50:09 +01002412 return validate_call(insn, state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002413}
2414
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002415static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2416{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002417 if (state->noinstr && state->instr > 0) {
2418 WARN_FUNC("return with instrumentation enabled",
2419 insn->sec, insn->offset);
2420 return 1;
2421 }
2422
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002423 if (state->uaccess && !func_uaccess_safe(func)) {
2424 WARN_FUNC("return with UACCESS enabled",
2425 insn->sec, insn->offset);
2426 return 1;
2427 }
2428
2429 if (!state->uaccess && func_uaccess_safe(func)) {
2430 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2431 insn->sec, insn->offset);
2432 return 1;
2433 }
2434
2435 if (state->df) {
2436 WARN_FUNC("return with DF set",
2437 insn->sec, insn->offset);
2438 return 1;
2439 }
2440
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002441 if (func && has_modified_stack_frame(insn, state)) {
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002442 WARN_FUNC("return with modified stack frame",
2443 insn->sec, insn->offset);
2444 return 1;
2445 }
2446
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002447 if (state->cfi.bp_scratch) {
Josh Poimboeufb2966952020-04-01 13:23:29 -05002448 WARN_FUNC("BP used as a scratch register",
2449 insn->sec, insn->offset);
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002450 return 1;
2451 }
2452
2453 return 0;
2454}
2455
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002456/*
Peter Zijlstra7117f162020-04-28 19:37:01 +02002457 * Alternatives should not contain any ORC entries, this in turn means they
2458 * should not contain any CFI ops, which implies all instructions should have
2459 * the same same CFI state.
2460 *
2461 * It is possible to constuct alternatives that have unreachable holes that go
2462 * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2463 * states which then results in ORC entries, which we just said we didn't want.
2464 *
2465 * Avoid them by copying the CFI entry of the first instruction into the whole
2466 * alternative.
2467 */
2468static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn)
2469{
2470 struct instruction *first_insn = insn;
Josh Poimboeufb23cc712020-12-18 14:19:32 -06002471 struct alt_group *alt_group = insn->alt_group;
Peter Zijlstra7117f162020-04-28 19:37:01 +02002472
2473 sec_for_each_insn_continue(file, insn) {
2474 if (insn->alt_group != alt_group)
2475 break;
2476 insn->cfi = first_insn->cfi;
2477 }
2478}
2479
2480/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002481 * Follow the branch starting at the given instruction, and recursively follow
2482 * any other branches (jumps). Meanwhile, track the frame pointer state at
2483 * each instruction and validate all the rules described in
2484 * tools/objtool/Documentation/stack-validation.txt.
2485 */
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002486static int validate_branch(struct objtool_file *file, struct symbol *func,
Peter Zijlstrab7460462020-04-02 10:15:51 +02002487 struct instruction *insn, struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002488{
2489 struct alternative *alt;
Peter Zijlstrab7460462020-04-02 10:15:51 +02002490 struct instruction *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002491 struct section *sec;
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002492 u8 visited;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002493 int ret;
2494
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002495 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002496
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002497 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002498 next_insn = next_insn_same_sec(file, insn);
2499
Josh Poimboeuf13810432018-05-09 22:39:15 -05002500 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05002501 WARN("%s() falls through to next function %s()",
2502 func->name, insn->func->name);
2503 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002504 }
2505
Josh Poimboeuf48550222017-07-07 09:19:42 -05002506 if (func && insn->ignore) {
2507 WARN_FUNC("BUG: why am I validating an ignored function?",
2508 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05002509 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05002510 }
2511
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002512 visited = 1 << state.uaccess;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002513 if (insn->visited) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002514 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002515 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002516
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002517 if (insn->visited & visited)
Peter Zijlstraea242132019-02-25 12:50:09 +01002518 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002519 }
2520
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002521 if (state.noinstr)
2522 state.instr += insn->instr;
2523
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002524 if (insn->hint)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002525 state.cfi = insn->cfi;
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002526 else
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002527 insn->cfi = state.cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002528
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002529 insn->visited |= visited;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002530
Peter Zijlstra7117f162020-04-28 19:37:01 +02002531 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002532 bool skip_orig = false;
2533
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002534 list_for_each_entry(alt, &insn->alts, list) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002535 if (alt->skip_orig)
2536 skip_orig = true;
2537
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002538 ret = validate_branch(file, func, alt->insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002539 if (ret) {
2540 if (backtrace)
2541 BT_FUNC("(alt)", insn);
2542 return ret;
2543 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002544 }
Peter Zijlstra764eef42019-03-01 11:19:03 +01002545
Peter Zijlstra7117f162020-04-28 19:37:01 +02002546 if (insn->alt_group)
2547 fill_alternative_cfi(file, insn);
2548
Peter Zijlstra764eef42019-03-01 11:19:03 +01002549 if (skip_orig)
2550 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002551 }
2552
Peter Zijlstra60041bc2020-04-24 16:16:41 +02002553 if (handle_insn_ops(insn, &state))
2554 return 1;
2555
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002556 switch (insn->type) {
2557
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002558 case INSN_RETURN:
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002559 return validate_return(func, insn, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002560
2561 case INSN_CALL:
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002562 case INSN_CALL_DYNAMIC:
Peter Zijlstraea242132019-02-25 12:50:09 +01002563 ret = validate_call(insn, &state);
2564 if (ret)
2565 return ret;
2566
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002567 if (!no_fp && func && !is_fentry_call(insn) &&
2568 !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002569 WARN_FUNC("call without frame pointer save/setup",
2570 sec, insn->offset);
2571 return 1;
2572 }
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002573
2574 if (dead_end_function(file, insn->call_dest))
2575 return 0;
2576
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002577 if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) {
2578 list_add_tail(&insn->static_call_node,
2579 &file->static_call_list);
2580 }
2581
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002582 break;
2583
2584 case INSN_JUMP_CONDITIONAL:
2585 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002586 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002587 ret = validate_sibling_call(insn, &state);
2588 if (ret)
2589 return ret;
2590
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002591 } else if (insn->jump_dest) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002592 ret = validate_branch(file, func,
2593 insn->jump_dest, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002594 if (ret) {
2595 if (backtrace)
2596 BT_FUNC("(branch)", insn);
2597 return ret;
2598 }
Josh Poimboeuf48550222017-07-07 09:19:42 -05002599 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002600
2601 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2602 return 0;
2603
2604 break;
2605
2606 case INSN_JUMP_DYNAMIC:
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002607 case INSN_JUMP_DYNAMIC_CONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002608 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002609 ret = validate_sibling_call(insn, &state);
2610 if (ret)
2611 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002612 }
2613
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002614 if (insn->type == INSN_JUMP_DYNAMIC)
2615 return 0;
2616
2617 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002618
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002619 case INSN_CONTEXT_SWITCH:
2620 if (func && (!next_insn || !next_insn->hint)) {
2621 WARN_FUNC("unsupported instruction in callable function",
2622 sec, insn->offset);
2623 return 1;
2624 }
2625 return 0;
2626
Peter Zijlstraea242132019-02-25 12:50:09 +01002627 case INSN_STAC:
2628 if (state.uaccess) {
2629 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2630 return 1;
2631 }
2632
2633 state.uaccess = true;
2634 break;
2635
2636 case INSN_CLAC:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002637 if (!state.uaccess && func) {
Peter Zijlstraea242132019-02-25 12:50:09 +01002638 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2639 return 1;
2640 }
2641
2642 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2643 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2644 return 1;
2645 }
2646
2647 state.uaccess = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002648 break;
2649
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002650 case INSN_STD:
2651 if (state.df)
2652 WARN_FUNC("recursive STD", sec, insn->offset);
2653
2654 state.df = true;
2655 break;
2656
2657 case INSN_CLD:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002658 if (!state.df && func)
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002659 WARN_FUNC("redundant CLD", sec, insn->offset);
2660
2661 state.df = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002662 break;
2663
2664 default:
2665 break;
2666 }
2667
2668 if (insn->dead_end)
2669 return 0;
2670
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002671 if (!next_insn) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002672 if (state.cfi.cfa.base == CFI_UNDEFINED)
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002673 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002674 WARN("%s: unexpected end of section", sec->name);
2675 return 1;
2676 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002677
2678 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002679 }
2680
2681 return 0;
2682}
2683
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002684static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002685{
2686 struct instruction *insn;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002687 struct insn_state state;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002688 int ret, warnings = 0;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002689
2690 if (!file->hints)
2691 return 0;
2692
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002693 init_insn_state(&state, sec);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002694
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002695 if (sec) {
2696 insn = find_insn(file, sec, 0);
2697 if (!insn)
2698 return 0;
2699 } else {
2700 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2701 }
2702
2703 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002704 if (insn->hint && !insn->visited) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002705 ret = validate_branch(file, insn->func, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002706 if (ret && backtrace)
2707 BT_FUNC("<=== (hint)", insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002708 warnings += ret;
2709 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002710
2711 insn = list_next_entry(insn, list);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002712 }
2713
2714 return warnings;
2715}
2716
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002717static int validate_retpoline(struct objtool_file *file)
2718{
2719 struct instruction *insn;
2720 int warnings = 0;
2721
2722 for_each_insn(file, insn) {
2723 if (insn->type != INSN_JUMP_DYNAMIC &&
2724 insn->type != INSN_CALL_DYNAMIC)
2725 continue;
2726
2727 if (insn->retpoline_safe)
2728 continue;
2729
Peter Zijlstraca41b972018-01-31 10:18:28 +01002730 /*
2731 * .init.text code is ran before userspace and thus doesn't
2732 * strictly need retpolines, except for modules which are
2733 * loaded late, they very much do need retpoline in their
2734 * .init.text
2735 */
2736 if (!strcmp(insn->sec->name, ".init.text") && !module)
2737 continue;
2738
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002739 WARN_FUNC("indirect %s found in RETPOLINE build",
2740 insn->sec, insn->offset,
2741 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2742
2743 warnings++;
2744 }
2745
2746 return warnings;
2747}
2748
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002749static bool is_kasan_insn(struct instruction *insn)
2750{
2751 return (insn->type == INSN_CALL &&
2752 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2753}
2754
2755static bool is_ubsan_insn(struct instruction *insn)
2756{
2757 return (insn->type == INSN_CALL &&
2758 !strcmp(insn->call_dest->name,
2759 "__ubsan_handle_builtin_unreachable"));
2760}
2761
Ilie Halip14db1f02020-09-19 09:41:18 +03002762static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002763{
2764 int i;
Ilie Halip14db1f02020-09-19 09:41:18 +03002765 struct instruction *prev_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002766
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002767 if (insn->ignore || insn->type == INSN_NOP)
2768 return true;
2769
2770 /*
2771 * Ignore any unused exceptions. This can happen when a whitelisted
2772 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002773 *
2774 * Also ignore alternative replacement instructions. This can happen
2775 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002776 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002777 if (!strcmp(insn->sec->name, ".fixup") ||
2778 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2779 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002780 return true;
2781
Julien Thierryfb136212020-09-15 08:53:17 +01002782 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->offset == FAKE_JUMP_OFFSET)
2783 return true;
2784
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002785 if (!insn->func)
2786 return false;
2787
2788 /*
2789 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2790 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2791 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2792 * (or occasionally a JMP to UD2).
Ilie Halip14db1f02020-09-19 09:41:18 +03002793 *
2794 * It may also insert a UD2 after calling a __noreturn function.
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002795 */
Ilie Halip14db1f02020-09-19 09:41:18 +03002796 prev_insn = list_prev_entry(insn, list);
2797 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002798 (insn->type == INSN_BUG ||
2799 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2800 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2801 return true;
2802
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002803 /*
2804 * Check if this (or a subsequent) instruction is related to
2805 * CONFIG_UBSAN or CONFIG_KASAN.
2806 *
2807 * End the search at 5 instructions to avoid going into the weeds.
2808 */
2809 for (i = 0; i < 5; i++) {
2810
2811 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2812 return true;
2813
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002814 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2815 if (insn->jump_dest &&
2816 insn->jump_dest->func == insn->func) {
2817 insn = insn->jump_dest;
2818 continue;
2819 }
2820
2821 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002822 }
2823
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002824 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002825 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002826
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002827 insn = list_next_entry(insn, list);
2828 }
2829
2830 return false;
2831}
2832
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002833static int validate_symbol(struct objtool_file *file, struct section *sec,
2834 struct symbol *sym, struct insn_state *state)
2835{
2836 struct instruction *insn;
2837 int ret;
2838
2839 if (!sym->len) {
2840 WARN("%s() is missing an ELF size annotation", sym->name);
2841 return 1;
2842 }
2843
2844 if (sym->pfunc != sym || sym->alias != sym)
2845 return 0;
2846
2847 insn = find_insn(file, sec, sym->offset);
2848 if (!insn || insn->ignore || insn->visited)
2849 return 0;
2850
2851 state->uaccess = sym->uaccess_safe;
2852
2853 ret = validate_branch(file, insn->func, insn, *state);
2854 if (ret && backtrace)
2855 BT_FUNC("<=== (sym)", insn);
2856 return ret;
2857}
2858
Peter Zijlstra350994b2020-03-23 20:57:13 +01002859static int validate_section(struct objtool_file *file, struct section *sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002860{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002861 struct insn_state state;
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002862 struct symbol *func;
2863 int warnings = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002864
Peter Zijlstra350994b2020-03-23 20:57:13 +01002865 list_for_each_entry(func, &sec->symbol_list, list) {
2866 if (func->type != STT_FUNC)
2867 continue;
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002868
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002869 init_insn_state(&state, sec);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002870 state.cfi.cfa = initial_func_cfi.cfa;
2871 memcpy(&state.cfi.regs, &initial_func_cfi.regs,
Julien Thierry0699e552020-03-27 15:28:40 +00002872 CFI_NUM_REGS * sizeof(struct cfi_reg));
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002873 state.cfi.stack_size = initial_func_cfi.cfa.offset;
Julien Thierry0699e552020-03-27 15:28:40 +00002874
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002875 warnings += validate_symbol(file, sec, func, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002876 }
2877
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002878 return warnings;
2879}
2880
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002881static int validate_vmlinux_functions(struct objtool_file *file)
2882{
2883 struct section *sec;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002884 int warnings = 0;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002885
2886 sec = find_section_by_name(file->elf, ".noinstr.text");
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002887 if (sec) {
2888 warnings += validate_section(file, sec);
2889 warnings += validate_unwind_hints(file, sec);
2890 }
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002891
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002892 sec = find_section_by_name(file->elf, ".entry.text");
2893 if (sec) {
2894 warnings += validate_section(file, sec);
2895 warnings += validate_unwind_hints(file, sec);
2896 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002897
2898 return warnings;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002899}
2900
Peter Zijlstra350994b2020-03-23 20:57:13 +01002901static int validate_functions(struct objtool_file *file)
2902{
2903 struct section *sec;
2904 int warnings = 0;
2905
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002906 for_each_sec(file, sec) {
2907 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2908 continue;
2909
Peter Zijlstra350994b2020-03-23 20:57:13 +01002910 warnings += validate_section(file, sec);
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002911 }
Peter Zijlstra350994b2020-03-23 20:57:13 +01002912
2913 return warnings;
2914}
2915
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002916static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002917{
2918 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002919
2920 if (file->ignore_unreachables)
2921 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002922
2923 for_each_insn(file, insn) {
Ilie Halip14db1f02020-09-19 09:41:18 +03002924 if (insn->visited || ignore_unreachable_insn(file, insn))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002925 continue;
2926
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002927 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2928 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002929 }
2930
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002931 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002932}
2933
Julien Thierryd44becb2020-08-25 13:47:40 +01002934int check(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002935{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002936 int ret, warnings = 0;
2937
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002938 arch_initial_func_cfi_state(&initial_func_cfi);
2939
Julien Thierry6545eb02020-08-25 13:47:39 +01002940 ret = decode_sections(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002941 if (ret < 0)
2942 goto out;
2943 warnings += ret;
2944
Julien Thierry6545eb02020-08-25 13:47:39 +01002945 if (list_empty(&file->insn_list))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002946 goto out;
2947
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002948 if (vmlinux && !validate_dup) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002949 ret = validate_vmlinux_functions(file);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002950 if (ret < 0)
2951 goto out;
2952
2953 warnings += ret;
2954 goto out;
2955 }
2956
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002957 if (retpoline) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002958 ret = validate_retpoline(file);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002959 if (ret < 0)
2960 return ret;
2961 warnings += ret;
2962 }
2963
Julien Thierry6545eb02020-08-25 13:47:39 +01002964 ret = validate_functions(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002965 if (ret < 0)
2966 goto out;
2967 warnings += ret;
2968
Julien Thierry6545eb02020-08-25 13:47:39 +01002969 ret = validate_unwind_hints(file, NULL);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002970 if (ret < 0)
2971 goto out;
2972 warnings += ret;
2973
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002974 if (!warnings) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002975 ret = validate_reachable_instructions(file);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002976 if (ret < 0)
2977 goto out;
2978 warnings += ret;
2979 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002980
Julien Thierry6545eb02020-08-25 13:47:39 +01002981 ret = create_static_call_sections(file);
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002982 if (ret < 0)
2983 goto out;
2984 warnings += ret;
2985
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002986out:
Josh Poimboeuf644592d2020-02-10 12:32:38 -06002987 if (ret < 0) {
2988 /*
2989 * Fatal error. The binary is corrupt or otherwise broken in
2990 * some way, or objtool itself is broken. Fail the kernel
2991 * build.
2992 */
2993 return ret;
2994 }
2995
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002996 return 0;
2997}