blob: 2087974c70d357268d672c076189e81cdb2365ba [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 Poimboeufdcc914f2017-06-28 10:11:05 -050023struct alternative {
24 struct list_head list;
25 struct instruction *insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +010026 bool skip_orig;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050027};
28
Peter Zijlstraa3608f52020-03-25 15:34:50 +010029struct cfi_init_state initial_func_cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050030
Josh Poimboeuf627fce12017-07-11 10:33:42 -050031struct instruction *find_insn(struct objtool_file *file,
32 struct section *sec, unsigned long offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050033{
34 struct instruction *insn;
35
Peter Zijlstra87ecb582020-03-16 15:47:27 +010036 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050037 if (insn->sec == sec && insn->offset == offset)
38 return insn;
Peter Zijlstra87ecb582020-03-16 15:47:27 +010039 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050040
41 return NULL;
42}
43
44static struct instruction *next_insn_same_sec(struct objtool_file *file,
45 struct instruction *insn)
46{
47 struct instruction *next = list_next_entry(insn, list);
48
Josh Poimboeufbaa41462017-06-28 10:11:07 -050049 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050050 return NULL;
51
52 return next;
53}
54
Josh Poimboeuf13810432018-05-09 22:39:15 -050055static struct instruction *next_insn_same_func(struct objtool_file *file,
56 struct instruction *insn)
57{
58 struct instruction *next = list_next_entry(insn, list);
59 struct symbol *func = insn->func;
60
61 if (!func)
62 return NULL;
63
64 if (&next->list != &file->insn_list && next->func == func)
65 return next;
66
67 /* Check if we're already in the subfunction: */
68 if (func == func->cfunc)
69 return NULL;
70
71 /* Move to the subfunction: */
72 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
73}
74
Josh Poimboeuf1119d262020-04-28 16:45:16 -050075static struct instruction *prev_insn_same_sym(struct objtool_file *file,
76 struct instruction *insn)
77{
78 struct instruction *prev = list_prev_entry(insn, list);
79
80 if (&prev->list != &file->insn_list && prev->func == insn->func)
81 return prev;
82
83 return NULL;
84}
85
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +010086#define func_for_each_insn(file, func, insn) \
Josh Poimboeuf13810432018-05-09 22:39:15 -050087 for (insn = find_insn(file, func->sec, func->offset); \
88 insn; \
89 insn = next_insn_same_func(file, insn))
90
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +010091#define sym_for_each_insn(file, sym, insn) \
92 for (insn = find_insn(file, sym->sec, sym->offset); \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050093 insn && &insn->list != &file->insn_list && \
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +010094 insn->sec == sym->sec && \
95 insn->offset < sym->offset + sym->len; \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050096 insn = list_next_entry(insn, list))
97
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +010098#define sym_for_each_insn_continue_reverse(file, sym, insn) \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050099 for (insn = list_prev_entry(insn, list); \
100 &insn->list != &file->insn_list && \
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100101 insn->sec == sym->sec && insn->offset >= sym->offset; \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500102 insn = list_prev_entry(insn, list))
103
104#define sec_for_each_insn_from(file, insn) \
105 for (; insn; insn = next_insn_same_sec(file, insn))
106
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500107#define sec_for_each_insn_continue(file, insn) \
108 for (insn = next_insn_same_sec(file, insn); insn; \
109 insn = next_insn_same_sec(file, insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500110
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500111static bool is_sibling_call(struct instruction *insn)
112{
Josh Poimboeufecf11ba2021-01-21 15:29:22 -0600113 /*
114 * Assume only ELF functions can make sibling calls. This ensures
115 * sibling call detection consistency between vmlinux.o and individual
116 * objects.
117 */
118 if (!insn->func)
119 return false;
120
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500121 /* An indirect jump is either a sibling call or a jump to a table. */
122 if (insn->type == INSN_JUMP_DYNAMIC)
123 return list_empty(&insn->alts);
124
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500125 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
Josh Poimboeufecf11ba2021-01-21 15:29:22 -0600126 return (is_static_jump(insn) && insn->call_dest);
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500127}
128
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500129/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500130 * This checks to see if the given function is a "noreturn" function.
131 *
132 * For global functions which are outside the scope of this object file, we
133 * have to keep a manual list of them.
134 *
135 * For local functions, we have to detect them manually by simply looking for
136 * the lack of a return instruction.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500137 */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500138static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
139 int recursion)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500140{
141 int i;
142 struct instruction *insn;
143 bool empty = true;
144
145 /*
146 * Unfortunately these have to be hard coded because the noreturn
147 * attribute isn't provided in ELF data.
148 */
149 static const char * const global_noreturns[] = {
150 "__stack_chk_fail",
151 "panic",
152 "do_exit",
153 "do_task_dead",
154 "__module_put_and_exit",
155 "complete_and_exit",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500156 "__reiserfs_panic",
157 "lbug_with_loc",
158 "fortify_panic",
Kees Cookb394d462018-01-10 14:22:38 -0800159 "usercopy_abort",
Josh Poimboeuf684fb242018-06-19 10:47:50 -0500160 "machine_real_restart",
Josh Poimboeuf4fa5ecd2019-04-04 12:17:35 -0500161 "rewind_stack_do_exit",
Brendan Higgins33adf802019-09-23 02:02:38 -0700162 "kunit_try_catch_throw",
Josh Poimboeufc26acfb2021-01-21 15:29:25 -0600163 "xen_start_kernel",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500164 };
165
Josh Poimboeufc9bab222019-07-17 20:36:51 -0500166 if (!func)
167 return false;
168
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500169 if (func->bind == STB_WEAK)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500170 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500171
172 if (func->bind == STB_GLOBAL)
173 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
174 if (!strcmp(func->name, global_noreturns[i]))
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500175 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500176
Josh Poimboeuf13810432018-05-09 22:39:15 -0500177 if (!func->len)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500178 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500179
Josh Poimboeuf13810432018-05-09 22:39:15 -0500180 insn = find_insn(file, func->sec, func->offset);
181 if (!insn->func)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500182 return false;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500183
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100184 func_for_each_insn(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500185 empty = false;
186
187 if (insn->type == INSN_RETURN)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500188 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500189 }
190
191 if (empty)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500192 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500193
194 /*
195 * A function can have a sibling call instead of a return. In that
196 * case, the function's dead-end status depends on whether the target
197 * of the sibling call returns.
198 */
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100199 func_for_each_insn(file, func, insn) {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500200 if (is_sibling_call(insn)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500201 struct instruction *dest = insn->jump_dest;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500202
203 if (!dest)
204 /* sibling call to another file */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500205 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500206
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500207 /* local sibling call */
208 if (recursion == 5) {
209 /*
210 * Infinite recursion: two functions have
211 * sibling calls to each other. This is a very
212 * rare case. It means they aren't dead ends.
213 */
214 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500215 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500216
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500217 return __dead_end_function(file, dest->func, recursion+1);
218 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500219 }
220
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500221 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500222}
223
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500224static bool dead_end_function(struct objtool_file *file, struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500225{
226 return __dead_end_function(file, func, 0);
227}
228
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100229static void init_cfi_state(struct cfi_state *cfi)
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500230{
231 int i;
232
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500233 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100234 cfi->regs[i].base = CFI_UNDEFINED;
235 cfi->vals[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500236 }
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100237 cfi->cfa.base = CFI_UNDEFINED;
238 cfi->drap_reg = CFI_UNDEFINED;
239 cfi->drap_offset = -1;
240}
241
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100242static void init_insn_state(struct insn_state *state, struct section *sec)
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100243{
244 memset(state, 0, sizeof(*state));
245 init_cfi_state(&state->cfi);
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100246
247 /*
248 * We need the full vmlinux for noinstr validation, otherwise we can
249 * not correctly determine insn->call_dest->sec (external symbols do
250 * not have a section).
251 */
252 if (vmlinux && sec)
253 state->noinstr = sec->noinstr;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500254}
255
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500256/*
257 * Call the arch-specific instruction decoder for all the instructions and add
258 * them to the global instruction list.
259 */
260static int decode_instructions(struct objtool_file *file)
261{
262 struct section *sec;
263 struct symbol *func;
264 unsigned long offset;
265 struct instruction *insn;
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100266 unsigned long nr_insns = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500267 int ret;
268
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500269 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500270
271 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
272 continue;
273
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500274 if (strcmp(sec->name, ".altinstr_replacement") &&
275 strcmp(sec->name, ".altinstr_aux") &&
276 strncmp(sec->name, ".discard.", 9))
277 sec->text = true;
278
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +0100279 if (!strcmp(sec->name, ".noinstr.text") ||
280 !strcmp(sec->name, ".entry.text"))
Peter Zijlstrac4a33932020-03-10 18:57:41 +0100281 sec->noinstr = true;
282
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500283 for (offset = 0; offset < sec->len; offset += insn->len) {
284 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500285 if (!insn) {
286 WARN("malloc failed");
287 return -1;
288 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500289 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500290 INIT_LIST_HEAD(&insn->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +0000291 INIT_LIST_HEAD(&insn->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100292 init_cfi_state(&insn->cfi);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500293
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500294 insn->sec = sec;
295 insn->offset = offset;
296
297 ret = arch_decode_instruction(file->elf, sec, offset,
298 sec->len - offset,
299 &insn->len, &insn->type,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500300 &insn->immediate,
Julien Thierry65ea47d2020-03-27 15:28:47 +0000301 &insn->stack_ops);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500302 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500303 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500304
Peter Zijlstra87ecb582020-03-16 15:47:27 +0100305 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500306 list_add_tail(&insn->list, &file->insn_list);
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100307 nr_insns++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500308 }
309
310 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500311 if (func->type != STT_FUNC || func->alias != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500312 continue;
313
314 if (!find_insn(file, sec, func->offset)) {
315 WARN("%s(): can't find starting instruction",
316 func->name);
317 return -1;
318 }
319
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100320 sym_for_each_insn(file, func, insn)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500321 insn->func = func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500322 }
323 }
324
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100325 if (stats)
326 printf("nr_insns: %lu\n", nr_insns);
327
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500328 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500329
330err:
331 free(insn);
332 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500333}
334
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700335static struct instruction *find_last_insn(struct objtool_file *file,
336 struct section *sec)
337{
338 struct instruction *insn = NULL;
339 unsigned int offset;
340 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
341
342 for (offset = sec->len - 1; offset >= end && !insn; offset--)
343 insn = find_insn(file, sec, offset);
344
345 return insn;
346}
347
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500348/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500349 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500350 */
351static int add_dead_ends(struct objtool_file *file)
352{
353 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700354 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500355 struct instruction *insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500356
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500357 /*
358 * By default, "ud2" is a dead end unless otherwise annotated, because
359 * GCC 7 inserts it for certain divide-by-zero cases.
360 */
361 for_each_insn(file, insn)
362 if (insn->type == INSN_BUG)
363 insn->dead_end = true;
364
365 /*
366 * Check for manually annotated dead ends.
367 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500368 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
369 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500370 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500371
Matt Helsleyf1974222020-05-29 14:01:13 -0700372 list_for_each_entry(reloc, &sec->reloc_list, list) {
373 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500374 WARN("unexpected relocation symbol type in %s", sec->name);
375 return -1;
376 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700377 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500378 if (insn)
379 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700380 else if (reloc->addend == reloc->sym->sec->len) {
381 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700382 if (!insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500383 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700384 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500385 return -1;
386 }
387 } else {
388 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700389 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500390 return -1;
391 }
392
393 insn->dead_end = true;
394 }
395
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500396reachable:
397 /*
398 * These manually annotated reachable checks are needed for GCC 4.4,
399 * where the Linux unreachable() macro isn't supported. In that case
400 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
401 * not a dead end.
402 */
403 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
404 if (!sec)
405 return 0;
406
Matt Helsleyf1974222020-05-29 14:01:13 -0700407 list_for_each_entry(reloc, &sec->reloc_list, list) {
408 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500409 WARN("unexpected relocation symbol type in %s", sec->name);
410 return -1;
411 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700412 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500413 if (insn)
414 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700415 else if (reloc->addend == reloc->sym->sec->len) {
416 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700417 if (!insn) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500418 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700419 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500420 return -1;
421 }
422 } else {
423 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700424 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500425 return -1;
426 }
427
428 insn->dead_end = false;
429 }
430
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500431 return 0;
432}
433
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200434static int create_static_call_sections(struct objtool_file *file)
435{
436 struct section *sec, *reloc_sec;
437 struct reloc *reloc;
438 struct static_call_site *site;
439 struct instruction *insn;
440 struct symbol *key_sym;
441 char *key_name, *tmp;
442 int idx;
443
444 sec = find_section_by_name(file->elf, ".static_call_sites");
445 if (sec) {
446 INIT_LIST_HEAD(&file->static_call_list);
447 WARN("file already has .static_call_sites section, skipping");
448 return 0;
449 }
450
451 if (list_empty(&file->static_call_list))
452 return 0;
453
454 idx = 0;
455 list_for_each_entry(insn, &file->static_call_list, static_call_node)
456 idx++;
457
458 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
459 sizeof(struct static_call_site), idx);
460 if (!sec)
461 return -1;
462
463 reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
464 if (!reloc_sec)
465 return -1;
466
467 idx = 0;
468 list_for_each_entry(insn, &file->static_call_list, static_call_node) {
469
470 site = (struct static_call_site *)sec->data->d_buf + idx;
471 memset(site, 0, sizeof(struct static_call_site));
472
473 /* populate reloc for 'addr' */
474 reloc = malloc(sizeof(*reloc));
Josh Poimboeuf44f6a7c2020-12-14 16:04:20 -0600475
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200476 if (!reloc) {
477 perror("malloc");
478 return -1;
479 }
480 memset(reloc, 0, sizeof(*reloc));
Josh Poimboeuf44f6a7c2020-12-14 16:04:20 -0600481
482 insn_to_reloc_sym_addend(insn->sec, insn->offset, reloc);
483 if (!reloc->sym) {
484 WARN_FUNC("static call tramp: missing containing symbol",
485 insn->sec, insn->offset);
486 return -1;
487 }
488
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200489 reloc->type = R_X86_64_PC32;
490 reloc->offset = idx * sizeof(struct static_call_site);
491 reloc->sec = reloc_sec;
492 elf_add_reloc(file->elf, reloc);
493
494 /* find key symbol */
495 key_name = strdup(insn->call_dest->name);
496 if (!key_name) {
497 perror("strdup");
498 return -1;
499 }
500 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
501 STATIC_CALL_TRAMP_PREFIX_LEN)) {
502 WARN("static_call: trampoline name malformed: %s", key_name);
503 return -1;
504 }
505 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
506 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
507
508 key_sym = find_symbol_by_name(file->elf, tmp);
509 if (!key_sym) {
510 WARN("static_call: can't find static_call_key symbol: %s", tmp);
511 return -1;
512 }
513 free(key_name);
514
515 /* populate reloc for 'key' */
516 reloc = malloc(sizeof(*reloc));
517 if (!reloc) {
518 perror("malloc");
519 return -1;
520 }
521 memset(reloc, 0, sizeof(*reloc));
522 reloc->sym = key_sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200523 reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200524 reloc->type = R_X86_64_PC32;
525 reloc->offset = idx * sizeof(struct static_call_site) + 4;
526 reloc->sec = reloc_sec;
527 elf_add_reloc(file->elf, reloc);
528
529 idx++;
530 }
531
532 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
533 return -1;
534
535 return 0;
536}
537
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500538/*
539 * Warnings shouldn't be reported for ignored functions.
540 */
541static void add_ignores(struct objtool_file *file)
542{
543 struct instruction *insn;
544 struct section *sec;
545 struct symbol *func;
Matt Helsleyf1974222020-05-29 14:01:13 -0700546 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500547
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100548 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
549 if (!sec)
550 return;
551
Matt Helsleyf1974222020-05-29 14:01:13 -0700552 list_for_each_entry(reloc, &sec->reloc_list, list) {
553 switch (reloc->sym->type) {
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100554 case STT_FUNC:
Matt Helsleyf1974222020-05-29 14:01:13 -0700555 func = reloc->sym;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100556 break;
557
558 case STT_SECTION:
Matt Helsleyf1974222020-05-29 14:01:13 -0700559 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600560 if (!func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500561 continue;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100562 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500563
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100564 default:
Matt Helsleyf1974222020-05-29 14:01:13 -0700565 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100566 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500567 }
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100568
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100569 func_for_each_insn(file, func, insn)
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100570 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500571 }
572}
573
574/*
Peter Zijlstraea242132019-02-25 12:50:09 +0100575 * This is a whitelist of functions that is allowed to be called with AC set.
576 * The list is meant to be minimal and only contains compiler instrumentation
577 * ABI and a few functions used to implement *_{to,from}_user() functions.
578 *
579 * These functions must not directly change AC, but may PUSHF/POPF.
580 */
581static const char *uaccess_safe_builtin[] = {
582 /* KASAN */
583 "kasan_report",
584 "check_memory_region",
585 /* KASAN out-of-line */
586 "__asan_loadN_noabort",
587 "__asan_load1_noabort",
588 "__asan_load2_noabort",
589 "__asan_load4_noabort",
590 "__asan_load8_noabort",
591 "__asan_load16_noabort",
592 "__asan_storeN_noabort",
593 "__asan_store1_noabort",
594 "__asan_store2_noabort",
595 "__asan_store4_noabort",
596 "__asan_store8_noabort",
597 "__asan_store16_noabort",
Jann Hornb0b8e562020-09-29 00:49:16 +0200598 "__kasan_check_read",
599 "__kasan_check_write",
Peter Zijlstraea242132019-02-25 12:50:09 +0100600 /* KASAN in-line */
601 "__asan_report_load_n_noabort",
602 "__asan_report_load1_noabort",
603 "__asan_report_load2_noabort",
604 "__asan_report_load4_noabort",
605 "__asan_report_load8_noabort",
606 "__asan_report_load16_noabort",
607 "__asan_report_store_n_noabort",
608 "__asan_report_store1_noabort",
609 "__asan_report_store2_noabort",
610 "__asan_report_store4_noabort",
611 "__asan_report_store8_noabort",
612 "__asan_report_store16_noabort",
Marco Elver5f5c9712019-11-14 19:02:57 +0100613 /* KCSAN */
Marco Elver99676832020-03-25 17:41:57 +0100614 "__kcsan_check_access",
Marco Elver5f5c9712019-11-14 19:02:57 +0100615 "kcsan_found_watchpoint",
616 "kcsan_setup_watchpoint",
Marco Elver99676832020-03-25 17:41:57 +0100617 "kcsan_check_scoped_accesses",
Marco Elver50a19ad2020-04-24 17:47:30 +0200618 "kcsan_disable_current",
619 "kcsan_enable_current_nowarn",
Marco Elver5f5c9712019-11-14 19:02:57 +0100620 /* KCSAN/TSAN */
621 "__tsan_func_entry",
622 "__tsan_func_exit",
623 "__tsan_read_range",
624 "__tsan_write_range",
625 "__tsan_read1",
626 "__tsan_read2",
627 "__tsan_read4",
628 "__tsan_read8",
629 "__tsan_read16",
630 "__tsan_write1",
631 "__tsan_write2",
632 "__tsan_write4",
633 "__tsan_write8",
634 "__tsan_write16",
Marco Elvera81b3752020-07-24 09:00:02 +0200635 "__tsan_read_write1",
636 "__tsan_read_write2",
637 "__tsan_read_write4",
638 "__tsan_read_write8",
639 "__tsan_read_write16",
Marco Elver883957b2020-07-03 15:40:30 +0200640 "__tsan_atomic8_load",
641 "__tsan_atomic16_load",
642 "__tsan_atomic32_load",
643 "__tsan_atomic64_load",
644 "__tsan_atomic8_store",
645 "__tsan_atomic16_store",
646 "__tsan_atomic32_store",
647 "__tsan_atomic64_store",
648 "__tsan_atomic8_exchange",
649 "__tsan_atomic16_exchange",
650 "__tsan_atomic32_exchange",
651 "__tsan_atomic64_exchange",
652 "__tsan_atomic8_fetch_add",
653 "__tsan_atomic16_fetch_add",
654 "__tsan_atomic32_fetch_add",
655 "__tsan_atomic64_fetch_add",
656 "__tsan_atomic8_fetch_sub",
657 "__tsan_atomic16_fetch_sub",
658 "__tsan_atomic32_fetch_sub",
659 "__tsan_atomic64_fetch_sub",
660 "__tsan_atomic8_fetch_and",
661 "__tsan_atomic16_fetch_and",
662 "__tsan_atomic32_fetch_and",
663 "__tsan_atomic64_fetch_and",
664 "__tsan_atomic8_fetch_or",
665 "__tsan_atomic16_fetch_or",
666 "__tsan_atomic32_fetch_or",
667 "__tsan_atomic64_fetch_or",
668 "__tsan_atomic8_fetch_xor",
669 "__tsan_atomic16_fetch_xor",
670 "__tsan_atomic32_fetch_xor",
671 "__tsan_atomic64_fetch_xor",
672 "__tsan_atomic8_fetch_nand",
673 "__tsan_atomic16_fetch_nand",
674 "__tsan_atomic32_fetch_nand",
675 "__tsan_atomic64_fetch_nand",
676 "__tsan_atomic8_compare_exchange_strong",
677 "__tsan_atomic16_compare_exchange_strong",
678 "__tsan_atomic32_compare_exchange_strong",
679 "__tsan_atomic64_compare_exchange_strong",
680 "__tsan_atomic8_compare_exchange_weak",
681 "__tsan_atomic16_compare_exchange_weak",
682 "__tsan_atomic32_compare_exchange_weak",
683 "__tsan_atomic64_compare_exchange_weak",
684 "__tsan_atomic8_compare_exchange_val",
685 "__tsan_atomic16_compare_exchange_val",
686 "__tsan_atomic32_compare_exchange_val",
687 "__tsan_atomic64_compare_exchange_val",
688 "__tsan_atomic_thread_fence",
689 "__tsan_atomic_signal_fence",
Peter Zijlstraea242132019-02-25 12:50:09 +0100690 /* KCOV */
691 "write_comp_data",
Josh Poimboeufae033f02020-04-29 14:09:04 -0500692 "check_kcov_mode",
Peter Zijlstraea242132019-02-25 12:50:09 +0100693 "__sanitizer_cov_trace_pc",
694 "__sanitizer_cov_trace_const_cmp1",
695 "__sanitizer_cov_trace_const_cmp2",
696 "__sanitizer_cov_trace_const_cmp4",
697 "__sanitizer_cov_trace_const_cmp8",
698 "__sanitizer_cov_trace_cmp1",
699 "__sanitizer_cov_trace_cmp2",
700 "__sanitizer_cov_trace_cmp4",
701 "__sanitizer_cov_trace_cmp8",
Al Viro36b1c702020-02-16 13:07:49 -0500702 "__sanitizer_cov_trace_switch",
Peter Zijlstraea242132019-02-25 12:50:09 +0100703 /* UBSAN */
704 "ubsan_type_mismatch_common",
705 "__ubsan_handle_type_mismatch",
706 "__ubsan_handle_type_mismatch_v1",
Peter Zijlstra9a50dca2019-10-21 15:11:49 +0200707 "__ubsan_handle_shift_out_of_bounds",
Peter Zijlstraea242132019-02-25 12:50:09 +0100708 /* misc */
709 "csum_partial_copy_generic",
Dan Williamsec6347b2020-10-05 20:40:16 -0700710 "copy_mc_fragile",
711 "copy_mc_fragile_handle_tail",
Dan Williams5da8e4a2020-10-05 20:40:25 -0700712 "copy_mc_enhanced_fast_string",
Peter Zijlstraea242132019-02-25 12:50:09 +0100713 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
714 NULL
715};
716
717static void add_uaccess_safe(struct objtool_file *file)
718{
719 struct symbol *func;
720 const char **name;
721
722 if (!uaccess)
723 return;
724
725 for (name = uaccess_safe_builtin; *name; name++) {
726 func = find_symbol_by_name(file->elf, *name);
727 if (!func)
728 continue;
729
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500730 func->uaccess_safe = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500731 }
732}
733
734/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000735 * FIXME: For now, just ignore any alternatives which add retpolines. This is
736 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
737 * But it at least allows objtool to understand the control flow *around* the
738 * retpoline.
739 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100740static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000741{
742 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700743 struct reloc *reloc;
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000744 struct instruction *insn;
745
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100746 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000747 if (!sec)
748 return 0;
749
Matt Helsleyf1974222020-05-29 14:01:13 -0700750 list_for_each_entry(reloc, &sec->reloc_list, list) {
751 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000752 WARN("unexpected relocation symbol type in %s", sec->name);
753 return -1;
754 }
755
Matt Helsleyf1974222020-05-29 14:01:13 -0700756 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000757 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100758 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000759 return -1;
760 }
761
762 insn->ignore_alts = true;
763 }
764
765 return 0;
766}
767
768/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500769 * Find the destination instructions for all jumps.
770 */
771static int add_jump_destinations(struct objtool_file *file)
772{
773 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -0700774 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500775 struct section *dest_sec;
776 unsigned long dest_off;
777
778 for_each_insn(file, insn) {
Josh Poimboeufa2296142020-02-10 12:32:39 -0600779 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500780 continue;
781
Matt Helsleyf1974222020-05-29 14:01:13 -0700782 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Josh Poimboeufecf11ba2021-01-21 15:29:22 -0600783 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700784 if (!reloc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500785 dest_sec = insn->sec;
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000786 dest_off = arch_jump_destination(insn);
Matt Helsleyf1974222020-05-29 14:01:13 -0700787 } else if (reloc->sym->type == STT_SECTION) {
788 dest_sec = reloc->sym->sec;
789 dest_off = arch_dest_reloc_offset(reloc->addend);
Josh Poimboeuf1f9a1b72021-01-21 15:29:18 -0600790 } else if (!strncmp(reloc->sym->name, "__x86_indirect_thunk_", 21) ||
791 !strncmp(reloc->sym->name, "__x86_retpoline_", 16)) {
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000792 /*
793 * Retpoline jumps are really dynamic jumps in
794 * disguise, so convert them accordingly.
795 */
Josh Poimboeufb68b9902019-07-17 20:36:57 -0500796 if (insn->type == INSN_JUMP_UNCONDITIONAL)
797 insn->type = INSN_JUMP_DYNAMIC;
798 else
799 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
800
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100801 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000802 continue;
Josh Poimboeufecf11ba2021-01-21 15:29:22 -0600803 } else if (insn->func) {
804 /* internal or external sibling call (with reloc) */
Matt Helsleyf1974222020-05-29 14:01:13 -0700805 insn->call_dest = reloc->sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200806 if (insn->call_dest->static_call_tramp) {
807 list_add_tail(&insn->static_call_node,
808 &file->static_call_list);
809 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500810 continue;
Josh Poimboeufecf11ba2021-01-21 15:29:22 -0600811 } else if (reloc->sym->sec->idx) {
812 dest_sec = reloc->sym->sec;
813 dest_off = reloc->sym->sym.st_value +
814 arch_dest_reloc_offset(reloc->addend);
815 } else {
816 /* non-func asm code jumping to another file */
817 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500818 }
819
820 insn->jump_dest = find_insn(file, dest_sec, dest_off);
821 if (!insn->jump_dest) {
822
823 /*
824 * This is a special case where an alt instruction
825 * jumps past the end of the section. These are
826 * handled later in handle_group_alt().
827 */
828 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
829 continue;
830
831 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
832 insn->sec, insn->offset, dest_sec->name,
833 dest_off);
834 return -1;
835 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500836
837 /*
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100838 * Cross-function jump.
Josh Poimboeufcd778492018-06-01 07:23:51 -0500839 */
840 if (insn->func && insn->jump_dest->func &&
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100841 insn->func != insn->jump_dest->func) {
842
843 /*
844 * For GCC 8+, create parent/child links for any cold
845 * subfunctions. This is _mostly_ redundant with a
846 * similar initialization in read_symbols().
847 *
848 * If a function has aliases, we want the *first* such
849 * function in the symbol table to be the subfunction's
850 * parent. In that case we overwrite the
851 * initialization done in read_symbols().
852 *
853 * However this code can't completely replace the
854 * read_symbols() code because this doesn't detect the
855 * case where the parent function's only reference to a
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500856 * subfunction is through a jump table.
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100857 */
Josh Poimboeuf34ca59e2021-01-21 15:29:19 -0600858 if (!strstr(insn->func->name, ".cold") &&
859 strstr(insn->jump_dest->func->name, ".cold")) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100860 insn->func->cfunc = insn->jump_dest->func;
861 insn->jump_dest->func->pfunc = insn->func;
862
863 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
864 insn->jump_dest->offset == insn->jump_dest->func->offset) {
865
Josh Poimboeufecf11ba2021-01-21 15:29:22 -0600866 /* internal sibling call (without reloc) */
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100867 insn->call_dest = insn->jump_dest->func;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200868 if (insn->call_dest->static_call_tramp) {
869 list_add_tail(&insn->static_call_node,
870 &file->static_call_list);
871 }
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100872 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500873 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500874 }
875
876 return 0;
877}
878
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200879static void remove_insn_ops(struct instruction *insn)
880{
881 struct stack_op *op, *tmp;
882
883 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
884 list_del(&op->list);
885 free(op);
886 }
887}
888
Julien Thierry2b232a22020-09-15 08:53:18 +0100889static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
890{
891 struct symbol *call_dest;
892
893 call_dest = find_func_by_offset(sec, offset);
894 if (!call_dest)
895 call_dest = find_symbol_by_offset(sec, offset);
896
897 return call_dest;
898}
899
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500900/*
901 * Find the destination instructions for all calls.
902 */
903static int add_call_destinations(struct objtool_file *file)
904{
905 struct instruction *insn;
906 unsigned long dest_off;
Matt Helsleyf1974222020-05-29 14:01:13 -0700907 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500908
909 for_each_insn(file, insn) {
910 if (insn->type != INSN_CALL)
911 continue;
912
Matt Helsleyf1974222020-05-29 14:01:13 -0700913 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100914 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700915 if (!reloc) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000916 dest_off = arch_jump_destination(insn);
Julien Thierry2b232a22020-09-15 08:53:18 +0100917 insn->call_dest = find_call_destination(insn->sec, dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600918
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600919 if (insn->ignore)
920 continue;
921
922 if (!insn->call_dest) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200923 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500924 return -1;
925 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600926
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600927 if (insn->func && insn->call_dest->type != STT_FUNC) {
928 WARN_FUNC("unsupported call to non-function",
929 insn->sec, insn->offset);
930 return -1;
931 }
932
Matt Helsleyf1974222020-05-29 14:01:13 -0700933 } else if (reloc->sym->type == STT_SECTION) {
934 dest_off = arch_dest_reloc_offset(reloc->addend);
Julien Thierry2b232a22020-09-15 08:53:18 +0100935 insn->call_dest = find_call_destination(reloc->sym->sec,
936 dest_off);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600937 if (!insn->call_dest) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000938 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500939 insn->sec, insn->offset,
Matt Helsleyf1974222020-05-29 14:01:13 -0700940 reloc->sym->sec->name,
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000941 dest_off);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500942 return -1;
943 }
944 } else
Matt Helsleyf1974222020-05-29 14:01:13 -0700945 insn->call_dest = reloc->sym;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200946
947 /*
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200948 * Many compilers cannot disable KCOV with a function attribute
949 * so they need a little help, NOP out any KCOV calls from noinstr
950 * text.
951 */
952 if (insn->sec->noinstr &&
953 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
Peter Zijlstrad832c002020-06-18 17:55:29 +0200954 if (reloc) {
955 reloc->type = R_NONE;
956 elf_write_reloc(file->elf, reloc);
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200957 }
958
959 elf_write_insn(file->elf, insn->sec,
960 insn->offset, insn->len,
961 arch_nop_insn(insn->len));
962 insn->type = INSN_NOP;
963 }
964
965 /*
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200966 * Whatever stack impact regular CALLs have, should be undone
967 * by the RETURN of the called function.
968 *
969 * Annotated intra-function calls retain the stack_ops but
970 * are converted to JUMP, see read_intra_function_calls().
971 */
972 remove_insn_ops(insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500973 }
974
975 return 0;
976}
977
978/*
Josh Poimboeufc9c324d2020-12-18 14:26:21 -0600979 * The .alternatives section requires some extra special care over and above
980 * other special sections because alternatives are patched in place.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500981 */
982static int handle_group_alt(struct objtool_file *file,
983 struct special_alt *special_alt,
984 struct instruction *orig_insn,
985 struct instruction **new_insn)
986{
Josh Poimboeufc9c324d2020-12-18 14:26:21 -0600987 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
Josh Poimboeufb23cc712020-12-18 14:19:32 -0600988 struct alt_group *orig_alt_group, *new_alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500989 unsigned long dest_off;
990
Josh Poimboeufb23cc712020-12-18 14:19:32 -0600991
992 orig_alt_group = malloc(sizeof(*orig_alt_group));
993 if (!orig_alt_group) {
994 WARN("malloc failed");
995 return -1;
996 }
Josh Poimboeufc9c324d2020-12-18 14:26:21 -0600997 orig_alt_group->cfi = calloc(special_alt->orig_len,
998 sizeof(struct cfi_state *));
999 if (!orig_alt_group->cfi) {
1000 WARN("calloc failed");
1001 return -1;
1002 }
1003
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001004 last_orig_insn = NULL;
1005 insn = orig_insn;
1006 sec_for_each_insn_from(file, insn) {
1007 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1008 break;
1009
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001010 insn->alt_group = orig_alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001011 last_orig_insn = insn;
1012 }
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001013 orig_alt_group->orig_group = NULL;
1014 orig_alt_group->first_insn = orig_insn;
1015 orig_alt_group->last_insn = last_orig_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001016
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001017
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001018 new_alt_group = malloc(sizeof(*new_alt_group));
1019 if (!new_alt_group) {
1020 WARN("malloc failed");
1021 return -1;
1022 }
1023
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001024 if (special_alt->new_len < special_alt->orig_len) {
1025 /*
1026 * Insert a fake nop at the end to make the replacement
1027 * alt_group the same size as the original. This is needed to
1028 * allow propagate_alt_cfi() to do its magic. When the last
1029 * instruction affects the stack, the instruction after it (the
1030 * nop) will propagate the new state to the shared CFI array.
1031 */
1032 nop = malloc(sizeof(*nop));
1033 if (!nop) {
1034 WARN("malloc failed");
1035 return -1;
1036 }
1037 memset(nop, 0, sizeof(*nop));
1038 INIT_LIST_HEAD(&nop->alts);
1039 INIT_LIST_HEAD(&nop->stack_ops);
1040 init_cfi_state(&nop->cfi);
1041
1042 nop->sec = special_alt->new_sec;
1043 nop->offset = special_alt->new_off + special_alt->new_len;
1044 nop->len = special_alt->orig_len - special_alt->new_len;
1045 nop->type = INSN_NOP;
1046 nop->func = orig_insn->func;
1047 nop->alt_group = new_alt_group;
1048 nop->ignore = orig_insn->ignore_alts;
1049 }
1050
1051 if (!special_alt->new_len) {
1052 *new_insn = nop;
1053 goto end;
1054 }
1055
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001056 insn = *new_insn;
1057 sec_for_each_insn_from(file, insn) {
Julien Thierry45245f52020-09-04 16:30:23 +01001058 struct reloc *alt_reloc;
1059
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001060 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1061 break;
1062
1063 last_new_insn = insn;
1064
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001065 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +01001066 insn->func = orig_insn->func;
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001067 insn->alt_group = new_alt_group;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001068
Josh Poimboeufdc419722020-02-10 12:32:40 -06001069 /*
1070 * Since alternative replacement code is copy/pasted by the
1071 * kernel after applying relocations, generally such code can't
1072 * have relative-address relocation references to outside the
1073 * .altinstr_replacement section, unless the arch's
1074 * alternatives code can adjust the relative offsets
1075 * accordingly.
Josh Poimboeufdc419722020-02-10 12:32:40 -06001076 */
Julien Thierry45245f52020-09-04 16:30:23 +01001077 alt_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1078 insn->offset, insn->len);
1079 if (alt_reloc &&
1080 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
Josh Poimboeufdc419722020-02-10 12:32:40 -06001081
1082 WARN_FUNC("unsupported relocation in alternatives section",
1083 insn->sec, insn->offset);
1084 return -1;
1085 }
1086
Josh Poimboeufa2296142020-02-10 12:32:39 -06001087 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001088 continue;
1089
1090 if (!insn->immediate)
1091 continue;
1092
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001093 dest_off = arch_jump_destination(insn);
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001094 if (dest_off == special_alt->new_off + special_alt->new_len)
1095 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001096
1097 if (!insn->jump_dest) {
1098 WARN_FUNC("can't find alternative jump destination",
1099 insn->sec, insn->offset);
1100 return -1;
1101 }
1102 }
1103
1104 if (!last_new_insn) {
1105 WARN_FUNC("can't find last new alternative instruction",
1106 special_alt->new_sec, special_alt->new_off);
1107 return -1;
1108 }
1109
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001110 if (nop)
1111 list_add(&nop->list, &last_new_insn->list);
1112end:
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001113 new_alt_group->orig_group = orig_alt_group;
1114 new_alt_group->first_insn = *new_insn;
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001115 new_alt_group->last_insn = nop ? : last_new_insn;
1116 new_alt_group->cfi = orig_alt_group->cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001117 return 0;
1118}
1119
1120/*
1121 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1122 * If the original instruction is a jump, make the alt entry an effective nop
1123 * by just skipping the original instruction.
1124 */
1125static int handle_jump_alt(struct objtool_file *file,
1126 struct special_alt *special_alt,
1127 struct instruction *orig_insn,
1128 struct instruction **new_insn)
1129{
1130 if (orig_insn->type == INSN_NOP)
1131 return 0;
1132
1133 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1134 WARN_FUNC("unsupported instruction at jump label",
1135 orig_insn->sec, orig_insn->offset);
1136 return -1;
1137 }
1138
1139 *new_insn = list_next_entry(orig_insn, list);
1140 return 0;
1141}
1142
1143/*
1144 * Read all the special sections which have alternate instructions which can be
1145 * patched in or redirected to at runtime. Each instruction having alternate
1146 * instruction(s) has them added to its insn->alts list, which will be
1147 * traversed in validate_branch().
1148 */
1149static int add_special_section_alts(struct objtool_file *file)
1150{
1151 struct list_head special_alts;
1152 struct instruction *orig_insn, *new_insn;
1153 struct special_alt *special_alt, *tmp;
1154 struct alternative *alt;
1155 int ret;
1156
1157 ret = special_get_alts(file->elf, &special_alts);
1158 if (ret)
1159 return ret;
1160
1161 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001162
1163 orig_insn = find_insn(file, special_alt->orig_sec,
1164 special_alt->orig_off);
1165 if (!orig_insn) {
1166 WARN_FUNC("special: can't find orig instruction",
1167 special_alt->orig_sec, special_alt->orig_off);
1168 ret = -1;
1169 goto out;
1170 }
1171
1172 new_insn = NULL;
1173 if (!special_alt->group || special_alt->new_len) {
1174 new_insn = find_insn(file, special_alt->new_sec,
1175 special_alt->new_off);
1176 if (!new_insn) {
1177 WARN_FUNC("special: can't find new instruction",
1178 special_alt->new_sec,
1179 special_alt->new_off);
1180 ret = -1;
1181 goto out;
1182 }
1183 }
1184
1185 if (special_alt->group) {
Julien Thierry7170cf42020-03-27 15:28:41 +00001186 if (!special_alt->orig_len) {
1187 WARN_FUNC("empty alternative entry",
1188 orig_insn->sec, orig_insn->offset);
1189 continue;
1190 }
1191
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001192 ret = handle_group_alt(file, special_alt, orig_insn,
1193 &new_insn);
1194 if (ret)
1195 goto out;
1196 } else if (special_alt->jump_or_nop) {
1197 ret = handle_jump_alt(file, special_alt, orig_insn,
1198 &new_insn);
1199 if (ret)
1200 goto out;
1201 }
1202
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001203 alt = malloc(sizeof(*alt));
1204 if (!alt) {
1205 WARN("malloc failed");
1206 ret = -1;
1207 goto out;
1208 }
1209
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001210 alt->insn = new_insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +01001211 alt->skip_orig = special_alt->skip_orig;
Peter Zijlstraea242132019-02-25 12:50:09 +01001212 orig_insn->ignore_alts |= special_alt->skip_alt;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001213 list_add_tail(&alt->list, &orig_insn->alts);
1214
1215 list_del(&special_alt->list);
1216 free(special_alt);
1217 }
1218
1219out:
1220 return ret;
1221}
1222
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001223static int add_jump_table(struct objtool_file *file, struct instruction *insn,
Matt Helsleyf1974222020-05-29 14:01:13 -07001224 struct reloc *table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001225{
Matt Helsleyf1974222020-05-29 14:01:13 -07001226 struct reloc *reloc = table;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001227 struct instruction *dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001228 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001229 struct symbol *pfunc = insn->func->pfunc;
1230 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001231
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001232 /*
Matt Helsleyf1974222020-05-29 14:01:13 -07001233 * Each @reloc is a switch table relocation which points to the target
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001234 * instruction.
1235 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001236 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
Jann Hornbd98c812019-07-17 20:36:54 -05001237
1238 /* Check for the end of the table: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001239 if (reloc != table && reloc->jump_table_start)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001240 break;
1241
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001242 /* Make sure the table entries are consecutive: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001243 if (prev_offset && reloc->offset != prev_offset + 8)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001244 break;
1245
1246 /* Detect function pointers from contiguous objects: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001247 if (reloc->sym->sec == pfunc->sec &&
1248 reloc->addend == pfunc->offset)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001249 break;
1250
Matt Helsleyf1974222020-05-29 14:01:13 -07001251 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001252 if (!dest_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001253 break;
1254
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001255 /* Make sure the destination is in the same function: */
Josh Poimboeufe65050b2019-07-17 20:36:55 -05001256 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -05001257 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001258
1259 alt = malloc(sizeof(*alt));
1260 if (!alt) {
1261 WARN("malloc failed");
1262 return -1;
1263 }
1264
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001265 alt->insn = dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001266 list_add_tail(&alt->list, &insn->alts);
Matt Helsleyf1974222020-05-29 14:01:13 -07001267 prev_offset = reloc->offset;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001268 }
1269
1270 if (!prev_offset) {
1271 WARN_FUNC("can't find switch jump table",
1272 insn->sec, insn->offset);
1273 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001274 }
1275
1276 return 0;
1277}
1278
1279/*
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001280 * find_jump_table() - Given a dynamic jump, find the switch jump table
1281 * associated with it.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001282 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001283static struct reloc *find_jump_table(struct objtool_file *file,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001284 struct symbol *func,
1285 struct instruction *insn)
1286{
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001287 struct reloc *table_reloc;
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001288 struct instruction *dest_insn, *orig_insn = insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001289
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001290 /*
1291 * Backward search using the @first_jump_src links, these help avoid
1292 * much of the 'in between' code. Which avoids us getting confused by
1293 * it.
1294 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001295 for (;
Josh Poimboeuf1119d262020-04-28 16:45:16 -05001296 insn && insn->func && insn->func->pfunc == func;
1297 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001298
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001299 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001300 break;
1301
1302 /* allow small jumps within the range */
1303 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1304 insn->jump_dest &&
1305 (insn->jump_dest->offset <= insn->offset ||
1306 insn->jump_dest->offset > orig_insn->offset))
1307 break;
1308
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001309 table_reloc = arch_find_switch_table(file, insn);
Matt Helsleyf1974222020-05-29 14:01:13 -07001310 if (!table_reloc)
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001311 continue;
Matt Helsleyf1974222020-05-29 14:01:13 -07001312 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001313 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1314 continue;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001315
Matt Helsleyf1974222020-05-29 14:01:13 -07001316 return table_reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001317 }
1318
1319 return NULL;
1320}
1321
Jann Hornbd98c812019-07-17 20:36:54 -05001322/*
1323 * First pass: Mark the head of each jump table so that in the next pass,
1324 * we know when a given jump table ends and the next one starts.
1325 */
1326static void mark_func_jump_tables(struct objtool_file *file,
1327 struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001328{
Jann Hornbd98c812019-07-17 20:36:54 -05001329 struct instruction *insn, *last = NULL;
Matt Helsleyf1974222020-05-29 14:01:13 -07001330 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001331
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001332 func_for_each_insn(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001333 if (!last)
1334 last = insn;
1335
1336 /*
1337 * Store back-pointers for unconditional forward jumps such
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001338 * that find_jump_table() can back-track using those and
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001339 * avoid some potentially confusing code.
1340 */
1341 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1342 insn->offset > last->offset &&
1343 insn->jump_dest->offset > insn->offset &&
1344 !insn->jump_dest->first_jump_src) {
1345
1346 insn->jump_dest->first_jump_src = insn;
1347 last = insn->jump_dest;
1348 }
1349
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001350 if (insn->type != INSN_JUMP_DYNAMIC)
1351 continue;
1352
Matt Helsleyf1974222020-05-29 14:01:13 -07001353 reloc = find_jump_table(file, func, insn);
1354 if (reloc) {
1355 reloc->jump_table_start = true;
1356 insn->jump_table = reloc;
Jann Hornbd98c812019-07-17 20:36:54 -05001357 }
1358 }
1359}
1360
1361static int add_func_jump_tables(struct objtool_file *file,
1362 struct symbol *func)
1363{
1364 struct instruction *insn;
1365 int ret;
1366
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001367 func_for_each_insn(file, func, insn) {
Jann Hornbd98c812019-07-17 20:36:54 -05001368 if (!insn->jump_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001369 continue;
1370
Jann Hornbd98c812019-07-17 20:36:54 -05001371 ret = add_jump_table(file, insn, insn->jump_table);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001372 if (ret)
1373 return ret;
1374 }
1375
1376 return 0;
1377}
1378
1379/*
1380 * For some switch statements, gcc generates a jump table in the .rodata
1381 * section which contains a list of addresses within the function to jump to.
1382 * This finds these jump tables and adds them to the insn->alts lists.
1383 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001384static int add_jump_table_alts(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001385{
1386 struct section *sec;
1387 struct symbol *func;
1388 int ret;
1389
Allan Xavier4a60aa02018-09-07 08:12:01 -05001390 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001391 return 0;
1392
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001393 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001394 list_for_each_entry(func, &sec->symbol_list, list) {
1395 if (func->type != STT_FUNC)
1396 continue;
1397
Jann Hornbd98c812019-07-17 20:36:54 -05001398 mark_func_jump_tables(file, func);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001399 ret = add_func_jump_tables(file, func);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001400 if (ret)
1401 return ret;
1402 }
1403 }
1404
1405 return 0;
1406}
1407
Josh Poimboeufb735bd32021-01-21 15:29:24 -06001408static void set_func_state(struct cfi_state *state)
1409{
1410 state->cfa = initial_func_cfi.cfa;
1411 memcpy(&state->regs, &initial_func_cfi.regs,
1412 CFI_NUM_REGS * sizeof(struct cfi_reg));
1413 state->stack_size = initial_func_cfi.cfa.offset;
1414}
1415
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001416static int read_unwind_hints(struct objtool_file *file)
1417{
Matt Helsleyf1974222020-05-29 14:01:13 -07001418 struct section *sec, *relocsec;
1419 struct reloc *reloc;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001420 struct unwind_hint *hint;
1421 struct instruction *insn;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001422 int i;
1423
1424 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1425 if (!sec)
1426 return 0;
1427
Matt Helsleyf1974222020-05-29 14:01:13 -07001428 relocsec = sec->reloc;
1429 if (!relocsec) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001430 WARN("missing .rela.discard.unwind_hints section");
1431 return -1;
1432 }
1433
1434 if (sec->len % sizeof(struct unwind_hint)) {
1435 WARN("struct unwind_hint size mismatch");
1436 return -1;
1437 }
1438
1439 file->hints = true;
1440
1441 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1442 hint = (struct unwind_hint *)sec->data->d_buf + i;
1443
Matt Helsleyf1974222020-05-29 14:01:13 -07001444 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1445 if (!reloc) {
1446 WARN("can't find reloc for unwind_hints[%d]", i);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001447 return -1;
1448 }
1449
Matt Helsleyf1974222020-05-29 14:01:13 -07001450 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001451 if (!insn) {
1452 WARN("can't find insn for unwind_hints[%d]", i);
1453 return -1;
1454 }
1455
Josh Poimboeufb735bd32021-01-21 15:29:24 -06001456 insn->hint = true;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001457
Josh Poimboeufb735bd32021-01-21 15:29:24 -06001458 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1459 set_func_state(&insn->cfi);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001460 continue;
1461 }
1462
Julien Thierryedea9e62020-09-04 16:30:28 +01001463 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001464 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1465 insn->sec, insn->offset, hint->sp_reg);
1466 return -1;
1467 }
1468
Josh Poimboeufb735bd32021-01-21 15:29:24 -06001469 insn->cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001470 insn->cfi.type = hint->type;
1471 insn->cfi.end = hint->end;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001472 }
1473
1474 return 0;
1475}
1476
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001477static int read_retpoline_hints(struct objtool_file *file)
1478{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001479 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001480 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001481 struct reloc *reloc;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001482
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001483 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001484 if (!sec)
1485 return 0;
1486
Matt Helsleyf1974222020-05-29 14:01:13 -07001487 list_for_each_entry(reloc, &sec->reloc_list, list) {
1488 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001489 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001490 return -1;
1491 }
1492
Matt Helsleyf1974222020-05-29 14:01:13 -07001493 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001494 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001495 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001496 return -1;
1497 }
1498
1499 if (insn->type != INSN_JUMP_DYNAMIC &&
1500 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001501 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001502 insn->sec, insn->offset);
1503 return -1;
1504 }
1505
1506 insn->retpoline_safe = true;
1507 }
1508
1509 return 0;
1510}
1511
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001512static int read_instr_hints(struct objtool_file *file)
1513{
1514 struct section *sec;
1515 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001516 struct reloc *reloc;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001517
1518 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1519 if (!sec)
1520 return 0;
1521
Matt Helsleyf1974222020-05-29 14:01:13 -07001522 list_for_each_entry(reloc, &sec->reloc_list, list) {
1523 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001524 WARN("unexpected relocation symbol type in %s", sec->name);
1525 return -1;
1526 }
1527
Matt Helsleyf1974222020-05-29 14:01:13 -07001528 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001529 if (!insn) {
1530 WARN("bad .discard.instr_end entry");
1531 return -1;
1532 }
1533
1534 insn->instr--;
1535 }
1536
1537 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1538 if (!sec)
1539 return 0;
1540
Matt Helsleyf1974222020-05-29 14:01:13 -07001541 list_for_each_entry(reloc, &sec->reloc_list, list) {
1542 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001543 WARN("unexpected relocation symbol type in %s", sec->name);
1544 return -1;
1545 }
1546
Matt Helsleyf1974222020-05-29 14:01:13 -07001547 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001548 if (!insn) {
1549 WARN("bad .discard.instr_begin entry");
1550 return -1;
1551 }
1552
1553 insn->instr++;
1554 }
1555
1556 return 0;
1557}
1558
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001559static int read_intra_function_calls(struct objtool_file *file)
1560{
1561 struct instruction *insn;
1562 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -07001563 struct reloc *reloc;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001564
1565 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1566 if (!sec)
1567 return 0;
1568
Matt Helsleyf1974222020-05-29 14:01:13 -07001569 list_for_each_entry(reloc, &sec->reloc_list, list) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001570 unsigned long dest_off;
1571
Matt Helsleyf1974222020-05-29 14:01:13 -07001572 if (reloc->sym->type != STT_SECTION) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001573 WARN("unexpected relocation symbol type in %s",
1574 sec->name);
1575 return -1;
1576 }
1577
Matt Helsleyf1974222020-05-29 14:01:13 -07001578 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001579 if (!insn) {
1580 WARN("bad .discard.intra_function_call entry");
1581 return -1;
1582 }
1583
1584 if (insn->type != INSN_CALL) {
1585 WARN_FUNC("intra_function_call not a direct call",
1586 insn->sec, insn->offset);
1587 return -1;
1588 }
1589
1590 /*
1591 * Treat intra-function CALLs as JMPs, but with a stack_op.
1592 * See add_call_destinations(), which strips stack_ops from
1593 * normal CALLs.
1594 */
1595 insn->type = INSN_JUMP_UNCONDITIONAL;
1596
1597 dest_off = insn->offset + insn->len + insn->immediate;
1598 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1599 if (!insn->jump_dest) {
1600 WARN_FUNC("can't find call dest at %s+0x%lx",
1601 insn->sec, insn->offset,
1602 insn->sec->name, dest_off);
1603 return -1;
1604 }
1605 }
1606
1607 return 0;
1608}
1609
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02001610static int read_static_call_tramps(struct objtool_file *file)
1611{
1612 struct section *sec;
1613 struct symbol *func;
1614
1615 for_each_sec(file, sec) {
1616 list_for_each_entry(func, &sec->symbol_list, list) {
1617 if (func->bind == STB_GLOBAL &&
1618 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1619 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1620 func->static_call_tramp = true;
1621 }
1622 }
1623
1624 return 0;
1625}
1626
Allan Xavier4a60aa02018-09-07 08:12:01 -05001627static void mark_rodata(struct objtool_file *file)
1628{
1629 struct section *sec;
1630 bool found = false;
1631
1632 /*
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001633 * Search for the following rodata sections, each of which can
1634 * potentially contain jump tables:
1635 *
1636 * - .rodata: can contain GCC switch tables
1637 * - .rodata.<func>: same, if -fdata-sections is being used
1638 * - .rodata..c_jump_table: contains C annotated jump tables
1639 *
1640 * .rodata.str1.* sections are ignored; they don't contain jump tables.
Allan Xavier4a60aa02018-09-07 08:12:01 -05001641 */
1642 for_each_sec(file, sec) {
Muchun Song1ee444702020-04-12 22:44:05 +08001643 if (!strncmp(sec->name, ".rodata", 7) &&
1644 !strstr(sec->name, ".str1.")) {
Allan Xavier4a60aa02018-09-07 08:12:01 -05001645 sec->rodata = true;
1646 found = true;
1647 }
1648 }
1649
1650 file->rodata = found;
1651}
1652
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001653static int decode_sections(struct objtool_file *file)
1654{
1655 int ret;
1656
Allan Xavier4a60aa02018-09-07 08:12:01 -05001657 mark_rodata(file);
1658
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001659 ret = decode_instructions(file);
1660 if (ret)
1661 return ret;
1662
1663 ret = add_dead_ends(file);
1664 if (ret)
1665 return ret;
1666
1667 add_ignores(file);
Peter Zijlstraea242132019-02-25 12:50:09 +01001668 add_uaccess_safe(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001669
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001670 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001671 if (ret)
1672 return ret;
1673
Peter Zijlstra5b06fd32020-08-18 15:57:49 +02001674 ret = read_static_call_tramps(file);
1675 if (ret)
1676 return ret;
1677
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001678 ret = add_jump_destinations(file);
1679 if (ret)
1680 return ret;
1681
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001682 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001683 if (ret)
1684 return ret;
1685
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001686 ret = read_intra_function_calls(file);
1687 if (ret)
1688 return ret;
1689
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001690 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001691 if (ret)
1692 return ret;
1693
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001694 ret = add_jump_table_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001695 if (ret)
1696 return ret;
1697
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001698 ret = read_unwind_hints(file);
1699 if (ret)
1700 return ret;
1701
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001702 ret = read_retpoline_hints(file);
1703 if (ret)
1704 return ret;
1705
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001706 ret = read_instr_hints(file);
1707 if (ret)
1708 return ret;
1709
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001710 return 0;
1711}
1712
1713static bool is_fentry_call(struct instruction *insn)
1714{
Alexandre Chartre87cf61f2020-04-14 12:36:10 +02001715 if (insn->type == INSN_CALL && insn->call_dest &&
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001716 insn->call_dest->type == STT_NOTYPE &&
1717 !strcmp(insn->call_dest->name, "__fentry__"))
1718 return true;
1719
1720 return false;
1721}
1722
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001723static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001724{
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
Josh Poimboeufb735bd32021-01-21 15:29:24 -06001731 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001732 return true;
1733
Josh Poimboeufb735bd32021-01-21 15:29:24 -06001734 if (cfi->stack_size != initial_func_cfi.cfa.offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001735 return true;
1736
1737 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001738 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1739 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001740 return true;
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001741 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001742
1743 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001744}
1745
Julien Thierryfb084fd2020-10-14 08:38:00 +01001746static bool check_reg_frame_pos(const struct cfi_reg *reg,
1747 int expected_offset)
1748{
1749 return reg->base == CFI_CFA &&
1750 reg->offset == expected_offset;
1751}
1752
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001753static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001754{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001755 struct cfi_state *cfi = &state->cfi;
1756
Julien Thierryfb084fd2020-10-14 08:38:00 +01001757 if (cfi->cfa.base == CFI_BP &&
1758 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
1759 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001760 return true;
1761
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001762 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001763 return true;
1764
1765 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001766}
1767
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001768static int update_cfi_state_regs(struct instruction *insn,
1769 struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001770 struct stack_op *op)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001771{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001772 struct cfi_reg *cfa = &cfi->cfa;
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001773
Josh Poimboeufd8dd25a2020-04-25 05:03:00 -05001774 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001775 return 0;
1776
1777 /* push */
Peter Zijlstraea242132019-02-25 12:50:09 +01001778 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001779 cfa->offset += 8;
1780
1781 /* pop */
Peter Zijlstraea242132019-02-25 12:50:09 +01001782 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001783 cfa->offset -= 8;
1784
1785 /* add immediate to sp */
1786 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1787 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1788 cfa->offset -= op->src.offset;
1789
1790 return 0;
1791}
1792
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001793static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001794{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001795 if (arch_callee_saved_reg(reg) &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001796 cfi->regs[reg].base == CFI_UNDEFINED) {
1797 cfi->regs[reg].base = base;
1798 cfi->regs[reg].offset = offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001799 }
1800}
1801
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001802static void restore_reg(struct cfi_state *cfi, unsigned char reg)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001803{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001804 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1805 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001806}
1807
1808/*
1809 * A note about DRAP stack alignment:
1810 *
1811 * GCC has the concept of a DRAP register, which is used to help keep track of
1812 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1813 * register. The typical DRAP pattern is:
1814 *
1815 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1816 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1817 * 41 ff 72 f8 pushq -0x8(%r10)
1818 * 55 push %rbp
1819 * 48 89 e5 mov %rsp,%rbp
1820 * (more pushes)
1821 * 41 52 push %r10
1822 * ...
1823 * 41 5a pop %r10
1824 * (more pops)
1825 * 5d pop %rbp
1826 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1827 * c3 retq
1828 *
1829 * There are some variations in the epilogues, like:
1830 *
1831 * 5b pop %rbx
1832 * 41 5a pop %r10
1833 * 41 5c pop %r12
1834 * 41 5d pop %r13
1835 * 41 5e pop %r14
1836 * c9 leaveq
1837 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1838 * c3 retq
1839 *
1840 * and:
1841 *
1842 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1843 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1844 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1845 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1846 * c9 leaveq
1847 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1848 * c3 retq
1849 *
1850 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1851 * restored beforehand:
1852 *
1853 * 41 55 push %r13
1854 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1855 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1856 * ...
1857 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1858 * 41 5d pop %r13
1859 * c3 retq
1860 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001861static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001862 struct stack_op *op)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001863{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001864 struct cfi_reg *cfa = &cfi->cfa;
1865 struct cfi_reg *regs = cfi->regs;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001866
1867 /* stack operations don't make sense with an undefined CFA */
1868 if (cfa->base == CFI_UNDEFINED) {
1869 if (insn->func) {
1870 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1871 return -1;
1872 }
1873 return 0;
1874 }
1875
Julien Thierryee819ae2020-09-04 16:30:27 +01001876 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
1877 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001878 return update_cfi_state_regs(insn, cfi, op);
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001879
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001880 switch (op->dest.type) {
1881
1882 case OP_DEST_REG:
1883 switch (op->src.type) {
1884
1885 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001886 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1887 cfa->base == CFI_SP &&
Julien Thierryfb084fd2020-10-14 08:38:00 +01001888 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001889
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001890 /* mov %rsp, %rbp */
1891 cfa->base = op->dest.reg;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001892 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001893 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001894
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001895 else if (op->src.reg == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001896 op->dest.reg == CFI_BP && cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001897
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001898 /* drap: mov %rsp, %rbp */
1899 regs[CFI_BP].base = CFI_BP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001900 regs[CFI_BP].offset = -cfi->stack_size;
1901 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001902 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001903
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001904 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1905
1906 /*
1907 * mov %rsp, %reg
1908 *
1909 * This is needed for the rare case where GCC
1910 * does:
1911 *
1912 * mov %rsp, %rax
1913 * ...
1914 * mov %rax, %rsp
1915 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001916 cfi->vals[op->dest.reg].base = CFI_CFA;
1917 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001918 }
1919
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001920 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1921 cfa->base == CFI_BP) {
1922
1923 /*
1924 * mov %rbp, %rsp
1925 *
1926 * Restore the original stack pointer (Clang).
1927 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001928 cfi->stack_size = -cfi->regs[CFI_BP].offset;
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001929 }
1930
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001931 else if (op->dest.reg == cfa->base) {
1932
1933 /* mov %reg, %rsp */
1934 if (cfa->base == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001935 cfi->vals[op->src.reg].base == CFI_CFA) {
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001936
1937 /*
1938 * This is needed for the rare case
1939 * where GCC does something dumb like:
1940 *
1941 * lea 0x8(%rsp), %rcx
1942 * ...
1943 * mov %rcx, %rsp
1944 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001945 cfa->offset = -cfi->vals[op->src.reg].offset;
1946 cfi->stack_size = cfa->offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001947
Peter Zijlstraaafeb142021-02-03 12:02:17 +01001948 } else if (cfa->base == CFI_SP &&
1949 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
1950 cfi->vals[op->src.reg].offset == cfa->offset) {
1951
1952 /*
1953 * Stack swizzle:
1954 *
1955 * 1: mov %rsp, (%[tos])
1956 * 2: mov %[tos], %rsp
1957 * ...
1958 * 3: pop %rsp
1959 *
1960 * Where:
1961 *
1962 * 1 - places a pointer to the previous
1963 * stack at the Top-of-Stack of the
1964 * new stack.
1965 *
1966 * 2 - switches to the new stack.
1967 *
1968 * 3 - pops the Top-of-Stack to restore
1969 * the original stack.
1970 *
1971 * Note: we set base to SP_INDIRECT
1972 * here and preserve offset. Therefore
1973 * when the unwinder reaches ToS it
1974 * will dereference SP and then add the
1975 * offset to find the next frame, IOW:
1976 * (%rsp) + offset.
1977 */
1978 cfa->base = CFI_SP_INDIRECT;
1979
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001980 } else {
1981 cfa->base = CFI_UNDEFINED;
1982 cfa->offset = 0;
1983 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001984 }
1985
Peter Zijlstra724c8a22021-02-18 17:14:10 +01001986 else if (op->dest.reg == CFI_SP &&
1987 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
1988 cfi->vals[op->src.reg].offset == cfa->offset) {
1989
1990 /*
1991 * The same stack swizzle case 2) as above. But
1992 * because we can't change cfa->base, case 3)
1993 * will become a regular POP. Pretend we're a
1994 * PUSH so things don't go unbalanced.
1995 */
1996 cfi->stack_size += 8;
1997 }
1998
1999
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002000 break;
2001
2002 case OP_SRC_ADD:
2003 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2004
2005 /* add imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002006 cfi->stack_size -= op->src.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002007 if (cfa->base == CFI_SP)
2008 cfa->offset -= op->src.offset;
2009 break;
2010 }
2011
2012 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2013
2014 /* lea disp(%rbp), %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002015 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002016 break;
2017 }
2018
Julien Thierry468af562020-10-14 08:38:01 +01002019 if (!cfi->drap && op->src.reg == CFI_SP &&
2020 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2021 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset + op->src.offset)) {
2022
2023 /* lea disp(%rsp), %rbp */
2024 cfa->base = CFI_BP;
2025 cfa->offset -= op->src.offset;
2026 cfi->bp_scratch = false;
2027 break;
2028 }
2029
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002030 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002031
2032 /* drap: lea disp(%rsp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002033 cfi->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002034
2035 /*
2036 * lea disp(%rsp), %reg
2037 *
2038 * This is needed for the rare case where GCC
2039 * does something dumb like:
2040 *
2041 * lea 0x8(%rsp), %rcx
2042 * ...
2043 * mov %rcx, %rsp
2044 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002045 cfi->vals[op->dest.reg].base = CFI_CFA;
2046 cfi->vals[op->dest.reg].offset = \
2047 -cfi->stack_size + op->src.offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002048
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002049 break;
2050 }
2051
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002052 if (cfi->drap && op->dest.reg == CFI_SP &&
2053 op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002054
2055 /* drap: lea disp(%drap), %rsp */
2056 cfa->base = CFI_SP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002057 cfa->offset = cfi->stack_size = -op->src.offset;
2058 cfi->drap_reg = CFI_UNDEFINED;
2059 cfi->drap = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002060 break;
2061 }
2062
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002063 if (op->dest.reg == cfi->cfa.base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002064 WARN_FUNC("unsupported stack register modification",
2065 insn->sec, insn->offset);
2066 return -1;
2067 }
2068
2069 break;
2070
2071 case OP_SRC_AND:
2072 if (op->dest.reg != CFI_SP ||
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002073 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2074 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002075 WARN_FUNC("unsupported stack pointer realignment",
2076 insn->sec, insn->offset);
2077 return -1;
2078 }
2079
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002080 if (cfi->drap_reg != CFI_UNDEFINED) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002081 /* drap: and imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002082 cfa->base = cfi->drap_reg;
2083 cfa->offset = cfi->stack_size = 0;
2084 cfi->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002085 }
2086
2087 /*
2088 * Older versions of GCC (4.8ish) realign the stack
2089 * without DRAP, with a frame pointer.
2090 */
2091
2092 break;
2093
2094 case OP_SRC_POP:
Peter Zijlstraea242132019-02-25 12:50:09 +01002095 case OP_SRC_POPF:
Peter Zijlstraaafeb142021-02-03 12:02:17 +01002096 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2097
2098 /* pop %rsp; # restore from a stack swizzle */
2099 cfa->base = CFI_SP;
2100 break;
2101 }
2102
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002103 if (!cfi->drap && op->dest.reg == cfa->base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002104
2105 /* pop %rbp */
2106 cfa->base = CFI_SP;
2107 }
2108
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002109 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2110 op->dest.reg == cfi->drap_reg &&
2111 cfi->drap_offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002112
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002113 /* drap: pop %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002114 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002115 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002116 cfi->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002117
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002118 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002119
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002120 /* pop %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002121 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002122 }
2123
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002124 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002125 if (cfa->base == CFI_SP)
2126 cfa->offset -= 8;
2127
2128 break;
2129
2130 case OP_SRC_REG_INDIRECT:
Julien Thierry201ef5a2020-10-14 08:38:02 +01002131 if (!cfi->drap && op->dest.reg == cfa->base &&
2132 op->dest.reg == CFI_BP) {
2133
2134 /* mov disp(%rsp), %rbp */
2135 cfa->base = CFI_SP;
2136 cfa->offset = cfi->stack_size;
2137 }
2138
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002139 if (cfi->drap && op->src.reg == CFI_BP &&
2140 op->src.offset == cfi->drap_offset) {
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002141
2142 /* drap: mov disp(%rbp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002143 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002144 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002145 cfi->drap_offset = -1;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002146 }
2147
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002148 if (cfi->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002149 op->src.offset == regs[op->dest.reg].offset) {
2150
2151 /* drap: mov disp(%rbp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002152 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002153
2154 } else if (op->src.reg == cfa->base &&
2155 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2156
2157 /* mov disp(%rbp), %reg */
2158 /* mov disp(%rsp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002159 restore_reg(cfi, op->dest.reg);
Julien Thierry201ef5a2020-10-14 08:38:02 +01002160
2161 } else if (op->src.reg == CFI_SP &&
2162 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2163
2164 /* mov disp(%rsp), %reg */
2165 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002166 }
2167
2168 break;
2169
2170 default:
2171 WARN_FUNC("unknown stack-related instruction",
2172 insn->sec, insn->offset);
2173 return -1;
2174 }
2175
2176 break;
2177
2178 case OP_DEST_PUSH:
Peter Zijlstraea242132019-02-25 12:50:09 +01002179 case OP_DEST_PUSHF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002180 cfi->stack_size += 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002181 if (cfa->base == CFI_SP)
2182 cfa->offset += 8;
2183
2184 if (op->src.type != OP_SRC_REG)
2185 break;
2186
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002187 if (cfi->drap) {
2188 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002189
2190 /* drap: push %drap */
2191 cfa->base = CFI_BP_INDIRECT;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002192 cfa->offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002193
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002194 /* save drap so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002195 cfi->drap_offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002196
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002197 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002198
2199 /* drap: push %rbp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002200 cfi->stack_size = 0;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002201
Julien Thierryf4f80392020-09-15 08:53:16 +01002202 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002203
2204 /* drap: push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002205 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002206 }
2207
2208 } else {
2209
2210 /* push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002211 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002212 }
2213
2214 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002215 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002216 cfa->base != CFI_BP)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002217 cfi->bp_scratch = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002218 break;
2219
2220 case OP_DEST_REG_INDIRECT:
2221
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002222 if (cfi->drap) {
2223 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002224
2225 /* drap: mov %drap, disp(%rbp) */
2226 cfa->base = CFI_BP_INDIRECT;
2227 cfa->offset = op->dest.offset;
2228
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002229 /* save drap offset so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002230 cfi->drap_offset = op->dest.offset;
Julien Thierryf4f80392020-09-15 08:53:16 +01002231 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002232
2233 /* drap: mov reg, disp(%rbp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002234 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002235 }
2236
2237 } else if (op->dest.reg == cfa->base) {
2238
2239 /* mov reg, disp(%rbp) */
2240 /* mov reg, disp(%rsp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002241 save_reg(cfi, op->src.reg, CFI_CFA,
2242 op->dest.offset - cfi->cfa.offset);
Julien Thierry201ef5a2020-10-14 08:38:02 +01002243
2244 } else if (op->dest.reg == CFI_SP) {
2245
2246 /* mov reg, disp(%rsp) */
2247 save_reg(cfi, op->src.reg, CFI_CFA,
2248 op->dest.offset - cfi->stack_size);
Peter Zijlstraaafeb142021-02-03 12:02:17 +01002249
2250 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2251
2252 /* mov %rsp, (%reg); # setup a stack swizzle. */
2253 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2254 cfi->vals[op->dest.reg].offset = cfa->offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002255 }
2256
2257 break;
2258
2259 case OP_DEST_LEAVE:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002260 if ((!cfi->drap && cfa->base != CFI_BP) ||
2261 (cfi->drap && cfa->base != cfi->drap_reg)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002262 WARN_FUNC("leave instruction with modified stack frame",
2263 insn->sec, insn->offset);
2264 return -1;
2265 }
2266
2267 /* leave (mov %rbp, %rsp; pop %rbp) */
2268
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002269 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2270 restore_reg(cfi, CFI_BP);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002271
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002272 if (!cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002273 cfa->base = CFI_SP;
2274 cfa->offset -= 8;
2275 }
2276
2277 break;
2278
2279 case OP_DEST_MEM:
Peter Zijlstraea242132019-02-25 12:50:09 +01002280 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002281 WARN_FUNC("unknown stack-related memory operation",
2282 insn->sec, insn->offset);
2283 return -1;
2284 }
2285
2286 /* pop mem */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002287 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002288 if (cfa->base == CFI_SP)
2289 cfa->offset -= 8;
2290
2291 break;
2292
2293 default:
2294 WARN_FUNC("unknown stack-related instruction",
2295 insn->sec, insn->offset);
2296 return -1;
2297 }
2298
2299 return 0;
2300}
2301
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002302/*
2303 * The stack layouts of alternatives instructions can sometimes diverge when
2304 * they have stack modifications. That's fine as long as the potential stack
2305 * layouts don't conflict at any given potential instruction boundary.
2306 *
2307 * Flatten the CFIs of the different alternative code streams (both original
2308 * and replacement) into a single shared CFI array which can be used to detect
2309 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2310 */
2311static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2312{
2313 struct cfi_state **alt_cfi;
2314 int group_off;
2315
2316 if (!insn->alt_group)
2317 return 0;
2318
2319 alt_cfi = insn->alt_group->cfi;
2320 group_off = insn->offset - insn->alt_group->first_insn->offset;
2321
2322 if (!alt_cfi[group_off]) {
2323 alt_cfi[group_off] = &insn->cfi;
2324 } else {
2325 if (memcmp(alt_cfi[group_off], &insn->cfi, sizeof(struct cfi_state))) {
2326 WARN_FUNC("stack layout conflict in alternatives",
2327 insn->sec, insn->offset);
2328 return -1;
2329 }
2330 }
2331
2332 return 0;
2333}
2334
Julien Thierry65ea47d2020-03-27 15:28:47 +00002335static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2336{
2337 struct stack_op *op;
2338
2339 list_for_each_entry(op, &insn->stack_ops, list) {
Julien Thierry65ea47d2020-03-27 15:28:47 +00002340
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002341 if (update_cfi_state(insn, &state->cfi, op))
2342 return 1;
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002343
Julien Thierry65ea47d2020-03-27 15:28:47 +00002344 if (op->dest.type == OP_DEST_PUSHF) {
2345 if (!state->uaccess_stack) {
2346 state->uaccess_stack = 1;
2347 } else if (state->uaccess_stack >> 31) {
2348 WARN_FUNC("PUSHF stack exhausted",
2349 insn->sec, insn->offset);
2350 return 1;
2351 }
2352 state->uaccess_stack <<= 1;
2353 state->uaccess_stack |= state->uaccess;
2354 }
2355
2356 if (op->src.type == OP_SRC_POPF) {
2357 if (state->uaccess_stack) {
2358 state->uaccess = state->uaccess_stack & 1;
2359 state->uaccess_stack >>= 1;
2360 if (state->uaccess_stack == 1)
2361 state->uaccess_stack = 0;
2362 }
2363 }
2364 }
2365
2366 return 0;
2367}
2368
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002369static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002370{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002371 struct cfi_state *cfi1 = &insn->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002372 int i;
2373
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002374 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2375
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002376 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2377 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002378 cfi1->cfa.base, cfi1->cfa.offset,
2379 cfi2->cfa.base, cfi2->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002380
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002381 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002382 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002383 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002384 sizeof(struct cfi_reg)))
2385 continue;
2386
2387 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2388 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002389 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2390 i, cfi2->regs[i].base, cfi2->regs[i].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002391 break;
2392 }
2393
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002394 } else if (cfi1->type != cfi2->type) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002395
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002396 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2397 insn->sec, insn->offset, cfi1->type, cfi2->type);
2398
2399 } else if (cfi1->drap != cfi2->drap ||
2400 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2401 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2402
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002403 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002404 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002405 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2406 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002407
2408 } else
2409 return true;
2410
2411 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002412}
2413
Peter Zijlstraea242132019-02-25 12:50:09 +01002414static inline bool func_uaccess_safe(struct symbol *func)
2415{
2416 if (func)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002417 return func->uaccess_safe;
Peter Zijlstraea242132019-02-25 12:50:09 +01002418
2419 return false;
2420}
2421
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002422static inline const char *call_dest_name(struct instruction *insn)
Peter Zijlstraea242132019-02-25 12:50:09 +01002423{
2424 if (insn->call_dest)
2425 return insn->call_dest->name;
2426
2427 return "{dynamic}";
2428}
2429
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002430static inline bool noinstr_call_dest(struct symbol *func)
2431{
2432 /*
2433 * We can't deal with indirect function calls at present;
2434 * assume they're instrumented.
2435 */
2436 if (!func)
2437 return false;
2438
2439 /*
2440 * If the symbol is from a noinstr section; we good.
2441 */
2442 if (func->sec->noinstr)
2443 return true;
2444
2445 /*
2446 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2447 * something 'BAD' happened. At the risk of taking the machine down,
2448 * let them proceed to get the message out.
2449 */
2450 if (!strncmp(func->name, "__ubsan_handle_", 15))
2451 return true;
2452
2453 return false;
2454}
2455
Peter Zijlstraea242132019-02-25 12:50:09 +01002456static int validate_call(struct instruction *insn, struct insn_state *state)
2457{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002458 if (state->noinstr && state->instr <= 0 &&
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002459 !noinstr_call_dest(insn->call_dest)) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002460 WARN_FUNC("call to %s() leaves .noinstr.text section",
2461 insn->sec, insn->offset, call_dest_name(insn));
2462 return 1;
2463 }
2464
Peter Zijlstraea242132019-02-25 12:50:09 +01002465 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2466 WARN_FUNC("call to %s() with UACCESS enabled",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002467 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstraea242132019-02-25 12:50:09 +01002468 return 1;
2469 }
2470
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002471 if (state->df) {
2472 WARN_FUNC("call to %s() with DF set",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002473 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002474 return 1;
2475 }
2476
Peter Zijlstraea242132019-02-25 12:50:09 +01002477 return 0;
2478}
2479
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002480static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2481{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002482 if (has_modified_stack_frame(insn, state)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002483 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2484 insn->sec, insn->offset);
2485 return 1;
2486 }
2487
Peter Zijlstraea242132019-02-25 12:50:09 +01002488 return validate_call(insn, state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002489}
2490
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002491static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2492{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002493 if (state->noinstr && state->instr > 0) {
2494 WARN_FUNC("return with instrumentation enabled",
2495 insn->sec, insn->offset);
2496 return 1;
2497 }
2498
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002499 if (state->uaccess && !func_uaccess_safe(func)) {
2500 WARN_FUNC("return with UACCESS enabled",
2501 insn->sec, insn->offset);
2502 return 1;
2503 }
2504
2505 if (!state->uaccess && func_uaccess_safe(func)) {
2506 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2507 insn->sec, insn->offset);
2508 return 1;
2509 }
2510
2511 if (state->df) {
2512 WARN_FUNC("return with DF set",
2513 insn->sec, insn->offset);
2514 return 1;
2515 }
2516
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002517 if (func && has_modified_stack_frame(insn, state)) {
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002518 WARN_FUNC("return with modified stack frame",
2519 insn->sec, insn->offset);
2520 return 1;
2521 }
2522
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002523 if (state->cfi.bp_scratch) {
Josh Poimboeufb2966952020-04-01 13:23:29 -05002524 WARN_FUNC("BP used as a scratch register",
2525 insn->sec, insn->offset);
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002526 return 1;
2527 }
2528
2529 return 0;
2530}
2531
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002532static struct instruction *next_insn_to_validate(struct objtool_file *file,
2533 struct instruction *insn)
Peter Zijlstra7117f162020-04-28 19:37:01 +02002534{
Josh Poimboeufb23cc712020-12-18 14:19:32 -06002535 struct alt_group *alt_group = insn->alt_group;
Peter Zijlstra7117f162020-04-28 19:37:01 +02002536
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002537 /*
2538 * Simulate the fact that alternatives are patched in-place. When the
2539 * end of a replacement alt_group is reached, redirect objtool flow to
2540 * the end of the original alt_group.
2541 */
2542 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2543 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2544
2545 return next_insn_same_sec(file, insn);
Peter Zijlstra7117f162020-04-28 19:37:01 +02002546}
2547
2548/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002549 * Follow the branch starting at the given instruction, and recursively follow
2550 * any other branches (jumps). Meanwhile, track the frame pointer state at
2551 * each instruction and validate all the rules described in
2552 * tools/objtool/Documentation/stack-validation.txt.
2553 */
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002554static int validate_branch(struct objtool_file *file, struct symbol *func,
Peter Zijlstrab7460462020-04-02 10:15:51 +02002555 struct instruction *insn, struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002556{
2557 struct alternative *alt;
Peter Zijlstrab7460462020-04-02 10:15:51 +02002558 struct instruction *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002559 struct section *sec;
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002560 u8 visited;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002561 int ret;
2562
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002563 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002564
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002565 while (1) {
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002566 next_insn = next_insn_to_validate(file, insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002567
Josh Poimboeuf13810432018-05-09 22:39:15 -05002568 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05002569 WARN("%s() falls through to next function %s()",
2570 func->name, insn->func->name);
2571 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002572 }
2573
Josh Poimboeuf48550222017-07-07 09:19:42 -05002574 if (func && insn->ignore) {
2575 WARN_FUNC("BUG: why am I validating an ignored function?",
2576 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05002577 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05002578 }
2579
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002580 visited = 1 << state.uaccess;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002581 if (insn->visited) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002582 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002583 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002584
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002585 if (insn->visited & visited)
Peter Zijlstraea242132019-02-25 12:50:09 +01002586 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002587 }
2588
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002589 if (state.noinstr)
2590 state.instr += insn->instr;
2591
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002592 if (insn->hint)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002593 state.cfi = insn->cfi;
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002594 else
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002595 insn->cfi = state.cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002596
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002597 insn->visited |= visited;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002598
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002599 if (propagate_alt_cfi(file, insn))
2600 return 1;
2601
Peter Zijlstra7117f162020-04-28 19:37:01 +02002602 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002603 bool skip_orig = false;
2604
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002605 list_for_each_entry(alt, &insn->alts, list) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002606 if (alt->skip_orig)
2607 skip_orig = true;
2608
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002609 ret = validate_branch(file, func, alt->insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002610 if (ret) {
2611 if (backtrace)
2612 BT_FUNC("(alt)", insn);
2613 return ret;
2614 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002615 }
Peter Zijlstra764eef42019-03-01 11:19:03 +01002616
2617 if (skip_orig)
2618 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002619 }
2620
Peter Zijlstra60041bc2020-04-24 16:16:41 +02002621 if (handle_insn_ops(insn, &state))
2622 return 1;
2623
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002624 switch (insn->type) {
2625
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002626 case INSN_RETURN:
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002627 return validate_return(func, insn, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002628
2629 case INSN_CALL:
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002630 case INSN_CALL_DYNAMIC:
Peter Zijlstraea242132019-02-25 12:50:09 +01002631 ret = validate_call(insn, &state);
2632 if (ret)
2633 return ret;
2634
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002635 if (!no_fp && func && !is_fentry_call(insn) &&
2636 !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002637 WARN_FUNC("call without frame pointer save/setup",
2638 sec, insn->offset);
2639 return 1;
2640 }
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002641
2642 if (dead_end_function(file, insn->call_dest))
2643 return 0;
2644
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002645 if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) {
2646 list_add_tail(&insn->static_call_node,
2647 &file->static_call_list);
2648 }
2649
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002650 break;
2651
2652 case INSN_JUMP_CONDITIONAL:
2653 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeufecf11ba2021-01-21 15:29:22 -06002654 if (is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002655 ret = validate_sibling_call(insn, &state);
2656 if (ret)
2657 return ret;
2658
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002659 } else if (insn->jump_dest) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002660 ret = validate_branch(file, func,
2661 insn->jump_dest, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002662 if (ret) {
2663 if (backtrace)
2664 BT_FUNC("(branch)", insn);
2665 return ret;
2666 }
Josh Poimboeuf48550222017-07-07 09:19:42 -05002667 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002668
2669 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2670 return 0;
2671
2672 break;
2673
2674 case INSN_JUMP_DYNAMIC:
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002675 case INSN_JUMP_DYNAMIC_CONDITIONAL:
Josh Poimboeufecf11ba2021-01-21 15:29:22 -06002676 if (is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002677 ret = validate_sibling_call(insn, &state);
2678 if (ret)
2679 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002680 }
2681
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002682 if (insn->type == INSN_JUMP_DYNAMIC)
2683 return 0;
2684
2685 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002686
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002687 case INSN_CONTEXT_SWITCH:
2688 if (func && (!next_insn || !next_insn->hint)) {
2689 WARN_FUNC("unsupported instruction in callable function",
2690 sec, insn->offset);
2691 return 1;
2692 }
2693 return 0;
2694
Peter Zijlstraea242132019-02-25 12:50:09 +01002695 case INSN_STAC:
2696 if (state.uaccess) {
2697 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2698 return 1;
2699 }
2700
2701 state.uaccess = true;
2702 break;
2703
2704 case INSN_CLAC:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002705 if (!state.uaccess && func) {
Peter Zijlstraea242132019-02-25 12:50:09 +01002706 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2707 return 1;
2708 }
2709
2710 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2711 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2712 return 1;
2713 }
2714
2715 state.uaccess = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002716 break;
2717
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002718 case INSN_STD:
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06002719 if (state.df) {
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002720 WARN_FUNC("recursive STD", sec, insn->offset);
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06002721 return 1;
2722 }
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002723
2724 state.df = true;
2725 break;
2726
2727 case INSN_CLD:
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06002728 if (!state.df && func) {
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002729 WARN_FUNC("redundant CLD", sec, insn->offset);
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06002730 return 1;
2731 }
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002732
2733 state.df = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002734 break;
2735
2736 default:
2737 break;
2738 }
2739
2740 if (insn->dead_end)
2741 return 0;
2742
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002743 if (!next_insn) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002744 if (state.cfi.cfa.base == CFI_UNDEFINED)
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002745 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002746 WARN("%s: unexpected end of section", sec->name);
2747 return 1;
2748 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002749
2750 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002751 }
2752
2753 return 0;
2754}
2755
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002756static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002757{
2758 struct instruction *insn;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002759 struct insn_state state;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002760 int ret, warnings = 0;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002761
2762 if (!file->hints)
2763 return 0;
2764
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002765 init_insn_state(&state, sec);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002766
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002767 if (sec) {
2768 insn = find_insn(file, sec, 0);
2769 if (!insn)
2770 return 0;
2771 } else {
2772 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2773 }
2774
2775 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002776 if (insn->hint && !insn->visited) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002777 ret = validate_branch(file, insn->func, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002778 if (ret && backtrace)
2779 BT_FUNC("<=== (hint)", insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002780 warnings += ret;
2781 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002782
2783 insn = list_next_entry(insn, list);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002784 }
2785
2786 return warnings;
2787}
2788
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002789static int validate_retpoline(struct objtool_file *file)
2790{
2791 struct instruction *insn;
2792 int warnings = 0;
2793
2794 for_each_insn(file, insn) {
2795 if (insn->type != INSN_JUMP_DYNAMIC &&
2796 insn->type != INSN_CALL_DYNAMIC)
2797 continue;
2798
2799 if (insn->retpoline_safe)
2800 continue;
2801
Peter Zijlstraca41b972018-01-31 10:18:28 +01002802 /*
2803 * .init.text code is ran before userspace and thus doesn't
2804 * strictly need retpolines, except for modules which are
2805 * loaded late, they very much do need retpoline in their
2806 * .init.text
2807 */
2808 if (!strcmp(insn->sec->name, ".init.text") && !module)
2809 continue;
2810
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002811 WARN_FUNC("indirect %s found in RETPOLINE build",
2812 insn->sec, insn->offset,
2813 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2814
2815 warnings++;
2816 }
2817
2818 return warnings;
2819}
2820
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002821static bool is_kasan_insn(struct instruction *insn)
2822{
2823 return (insn->type == INSN_CALL &&
2824 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2825}
2826
2827static bool is_ubsan_insn(struct instruction *insn)
2828{
2829 return (insn->type == INSN_CALL &&
2830 !strcmp(insn->call_dest->name,
2831 "__ubsan_handle_builtin_unreachable"));
2832}
2833
Ilie Halip14db1f02020-09-19 09:41:18 +03002834static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002835{
2836 int i;
Ilie Halip14db1f02020-09-19 09:41:18 +03002837 struct instruction *prev_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002838
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002839 if (insn->ignore || insn->type == INSN_NOP)
2840 return true;
2841
2842 /*
2843 * Ignore any unused exceptions. This can happen when a whitelisted
2844 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002845 *
2846 * Also ignore alternative replacement instructions. This can happen
2847 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002848 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002849 if (!strcmp(insn->sec->name, ".fixup") ||
2850 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2851 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002852 return true;
2853
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002854 if (!insn->func)
2855 return false;
2856
2857 /*
2858 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2859 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2860 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2861 * (or occasionally a JMP to UD2).
Ilie Halip14db1f02020-09-19 09:41:18 +03002862 *
2863 * It may also insert a UD2 after calling a __noreturn function.
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002864 */
Ilie Halip14db1f02020-09-19 09:41:18 +03002865 prev_insn = list_prev_entry(insn, list);
2866 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002867 (insn->type == INSN_BUG ||
2868 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2869 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2870 return true;
2871
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002872 /*
2873 * Check if this (or a subsequent) instruction is related to
2874 * CONFIG_UBSAN or CONFIG_KASAN.
2875 *
2876 * End the search at 5 instructions to avoid going into the weeds.
2877 */
2878 for (i = 0; i < 5; i++) {
2879
2880 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2881 return true;
2882
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002883 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2884 if (insn->jump_dest &&
2885 insn->jump_dest->func == insn->func) {
2886 insn = insn->jump_dest;
2887 continue;
2888 }
2889
2890 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002891 }
2892
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002893 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002894 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002895
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002896 insn = list_next_entry(insn, list);
2897 }
2898
2899 return false;
2900}
2901
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002902static int validate_symbol(struct objtool_file *file, struct section *sec,
2903 struct symbol *sym, struct insn_state *state)
2904{
2905 struct instruction *insn;
2906 int ret;
2907
2908 if (!sym->len) {
2909 WARN("%s() is missing an ELF size annotation", sym->name);
2910 return 1;
2911 }
2912
2913 if (sym->pfunc != sym || sym->alias != sym)
2914 return 0;
2915
2916 insn = find_insn(file, sec, sym->offset);
2917 if (!insn || insn->ignore || insn->visited)
2918 return 0;
2919
2920 state->uaccess = sym->uaccess_safe;
2921
2922 ret = validate_branch(file, insn->func, insn, *state);
2923 if (ret && backtrace)
2924 BT_FUNC("<=== (sym)", insn);
2925 return ret;
2926}
2927
Peter Zijlstra350994b2020-03-23 20:57:13 +01002928static int validate_section(struct objtool_file *file, struct section *sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002929{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002930 struct insn_state state;
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002931 struct symbol *func;
2932 int warnings = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002933
Peter Zijlstra350994b2020-03-23 20:57:13 +01002934 list_for_each_entry(func, &sec->symbol_list, list) {
2935 if (func->type != STT_FUNC)
2936 continue;
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002937
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002938 init_insn_state(&state, sec);
Josh Poimboeufb735bd32021-01-21 15:29:24 -06002939 set_func_state(&state.cfi);
Julien Thierry0699e552020-03-27 15:28:40 +00002940
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002941 warnings += validate_symbol(file, sec, func, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002942 }
2943
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002944 return warnings;
2945}
2946
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002947static int validate_vmlinux_functions(struct objtool_file *file)
2948{
2949 struct section *sec;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002950 int warnings = 0;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002951
2952 sec = find_section_by_name(file->elf, ".noinstr.text");
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002953 if (sec) {
2954 warnings += validate_section(file, sec);
2955 warnings += validate_unwind_hints(file, sec);
2956 }
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002957
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002958 sec = find_section_by_name(file->elf, ".entry.text");
2959 if (sec) {
2960 warnings += validate_section(file, sec);
2961 warnings += validate_unwind_hints(file, sec);
2962 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002963
2964 return warnings;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002965}
2966
Peter Zijlstra350994b2020-03-23 20:57:13 +01002967static int validate_functions(struct objtool_file *file)
2968{
2969 struct section *sec;
2970 int warnings = 0;
2971
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002972 for_each_sec(file, sec) {
2973 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2974 continue;
2975
Peter Zijlstra350994b2020-03-23 20:57:13 +01002976 warnings += validate_section(file, sec);
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002977 }
Peter Zijlstra350994b2020-03-23 20:57:13 +01002978
2979 return warnings;
2980}
2981
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002982static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002983{
2984 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002985
2986 if (file->ignore_unreachables)
2987 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002988
2989 for_each_insn(file, insn) {
Ilie Halip14db1f02020-09-19 09:41:18 +03002990 if (insn->visited || ignore_unreachable_insn(file, insn))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002991 continue;
2992
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002993 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2994 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002995 }
2996
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002997 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002998}
2999
Julien Thierryd44becb2020-08-25 13:47:40 +01003000int check(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003001{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003002 int ret, warnings = 0;
3003
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003004 arch_initial_func_cfi_state(&initial_func_cfi);
3005
Julien Thierry6545eb02020-08-25 13:47:39 +01003006 ret = decode_sections(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003007 if (ret < 0)
3008 goto out;
3009 warnings += ret;
3010
Julien Thierry6545eb02020-08-25 13:47:39 +01003011 if (list_empty(&file->insn_list))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003012 goto out;
3013
Peter Zijlstrac4a33932020-03-10 18:57:41 +01003014 if (vmlinux && !validate_dup) {
Julien Thierry6545eb02020-08-25 13:47:39 +01003015 ret = validate_vmlinux_functions(file);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01003016 if (ret < 0)
3017 goto out;
3018
3019 warnings += ret;
3020 goto out;
3021 }
3022
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01003023 if (retpoline) {
Julien Thierry6545eb02020-08-25 13:47:39 +01003024 ret = validate_retpoline(file);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01003025 if (ret < 0)
3026 return ret;
3027 warnings += ret;
3028 }
3029
Julien Thierry6545eb02020-08-25 13:47:39 +01003030 ret = validate_functions(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003031 if (ret < 0)
3032 goto out;
3033 warnings += ret;
3034
Julien Thierry6545eb02020-08-25 13:47:39 +01003035 ret = validate_unwind_hints(file, NULL);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05003036 if (ret < 0)
3037 goto out;
3038 warnings += ret;
3039
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003040 if (!warnings) {
Julien Thierry6545eb02020-08-25 13:47:39 +01003041 ret = validate_reachable_instructions(file);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003042 if (ret < 0)
3043 goto out;
3044 warnings += ret;
3045 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003046
Julien Thierry6545eb02020-08-25 13:47:39 +01003047 ret = create_static_call_sections(file);
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02003048 if (ret < 0)
3049 goto out;
3050 warnings += ret;
3051
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003052out:
Josh Poimboeuf655cf862021-01-14 16:32:42 -06003053 /*
3054 * For now, don't fail the kernel build on fatal warnings. These
3055 * errors are still fairly common due to the growing matrix of
3056 * supported toolchains and their recent pace of change.
3057 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003058 return 0;
3059}