blob: 3bdd946c2027ef415fb57f4154faa924e1ac27d5 [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{
113 /* An indirect jump is either a sibling call or a jump to a table. */
114 if (insn->type == INSN_JUMP_DYNAMIC)
115 return list_empty(&insn->alts);
116
Josh Poimboeufa2296142020-02-10 12:32:39 -0600117 if (!is_static_jump(insn))
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500118 return false;
119
120 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
121 return !!insn->call_dest;
122}
123
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500124/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500125 * This checks to see if the given function is a "noreturn" function.
126 *
127 * For global functions which are outside the scope of this object file, we
128 * have to keep a manual list of them.
129 *
130 * For local functions, we have to detect them manually by simply looking for
131 * the lack of a return instruction.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500132 */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500133static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
134 int recursion)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500135{
136 int i;
137 struct instruction *insn;
138 bool empty = true;
139
140 /*
141 * Unfortunately these have to be hard coded because the noreturn
142 * attribute isn't provided in ELF data.
143 */
144 static const char * const global_noreturns[] = {
145 "__stack_chk_fail",
146 "panic",
147 "do_exit",
148 "do_task_dead",
149 "__module_put_and_exit",
150 "complete_and_exit",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500151 "__reiserfs_panic",
152 "lbug_with_loc",
153 "fortify_panic",
Kees Cookb394d462018-01-10 14:22:38 -0800154 "usercopy_abort",
Josh Poimboeuf684fb242018-06-19 10:47:50 -0500155 "machine_real_restart",
Josh Poimboeuf4fa5ecd2019-04-04 12:17:35 -0500156 "rewind_stack_do_exit",
Brendan Higgins33adf802019-09-23 02:02:38 -0700157 "kunit_try_catch_throw",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500158 };
159
Josh Poimboeufc9bab222019-07-17 20:36:51 -0500160 if (!func)
161 return false;
162
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500163 if (func->bind == STB_WEAK)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500164 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500165
166 if (func->bind == STB_GLOBAL)
167 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
168 if (!strcmp(func->name, global_noreturns[i]))
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500169 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500170
Josh Poimboeuf13810432018-05-09 22:39:15 -0500171 if (!func->len)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500172 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500173
Josh Poimboeuf13810432018-05-09 22:39:15 -0500174 insn = find_insn(file, func->sec, func->offset);
175 if (!insn->func)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500176 return false;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500177
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100178 func_for_each_insn(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500179 empty = false;
180
181 if (insn->type == INSN_RETURN)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500182 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500183 }
184
185 if (empty)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500186 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500187
188 /*
189 * A function can have a sibling call instead of a return. In that
190 * case, the function's dead-end status depends on whether the target
191 * of the sibling call returns.
192 */
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100193 func_for_each_insn(file, func, insn) {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500194 if (is_sibling_call(insn)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500195 struct instruction *dest = insn->jump_dest;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500196
197 if (!dest)
198 /* sibling call to another file */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500199 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500200
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500201 /* local sibling call */
202 if (recursion == 5) {
203 /*
204 * Infinite recursion: two functions have
205 * sibling calls to each other. This is a very
206 * rare case. It means they aren't dead ends.
207 */
208 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500209 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500210
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500211 return __dead_end_function(file, dest->func, recursion+1);
212 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500213 }
214
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500215 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500216}
217
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500218static bool dead_end_function(struct objtool_file *file, struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500219{
220 return __dead_end_function(file, func, 0);
221}
222
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100223static void init_cfi_state(struct cfi_state *cfi)
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500224{
225 int i;
226
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500227 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100228 cfi->regs[i].base = CFI_UNDEFINED;
229 cfi->vals[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500230 }
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100231 cfi->cfa.base = CFI_UNDEFINED;
232 cfi->drap_reg = CFI_UNDEFINED;
233 cfi->drap_offset = -1;
234}
235
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100236static void init_insn_state(struct insn_state *state, struct section *sec)
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100237{
238 memset(state, 0, sizeof(*state));
239 init_cfi_state(&state->cfi);
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100240
241 /*
242 * We need the full vmlinux for noinstr validation, otherwise we can
243 * not correctly determine insn->call_dest->sec (external symbols do
244 * not have a section).
245 */
246 if (vmlinux && sec)
247 state->noinstr = sec->noinstr;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500248}
249
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500250/*
251 * Call the arch-specific instruction decoder for all the instructions and add
252 * them to the global instruction list.
253 */
254static int decode_instructions(struct objtool_file *file)
255{
256 struct section *sec;
257 struct symbol *func;
258 unsigned long offset;
259 struct instruction *insn;
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100260 unsigned long nr_insns = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500261 int ret;
262
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500263 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500264
265 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
266 continue;
267
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500268 if (strcmp(sec->name, ".altinstr_replacement") &&
269 strcmp(sec->name, ".altinstr_aux") &&
270 strncmp(sec->name, ".discard.", 9))
271 sec->text = true;
272
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +0100273 if (!strcmp(sec->name, ".noinstr.text") ||
274 !strcmp(sec->name, ".entry.text"))
Peter Zijlstrac4a33932020-03-10 18:57:41 +0100275 sec->noinstr = true;
276
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500277 for (offset = 0; offset < sec->len; offset += insn->len) {
278 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500279 if (!insn) {
280 WARN("malloc failed");
281 return -1;
282 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500283 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500284 INIT_LIST_HEAD(&insn->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +0000285 INIT_LIST_HEAD(&insn->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100286 init_cfi_state(&insn->cfi);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500287
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500288 insn->sec = sec;
289 insn->offset = offset;
290
291 ret = arch_decode_instruction(file->elf, sec, offset,
292 sec->len - offset,
293 &insn->len, &insn->type,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500294 &insn->immediate,
Julien Thierry65ea47d2020-03-27 15:28:47 +0000295 &insn->stack_ops);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500296 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500297 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500298
Peter Zijlstra87ecb582020-03-16 15:47:27 +0100299 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500300 list_add_tail(&insn->list, &file->insn_list);
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100301 nr_insns++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500302 }
303
304 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500305 if (func->type != STT_FUNC || func->alias != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500306 continue;
307
308 if (!find_insn(file, sec, func->offset)) {
309 WARN("%s(): can't find starting instruction",
310 func->name);
311 return -1;
312 }
313
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100314 sym_for_each_insn(file, func, insn)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500315 insn->func = func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500316 }
317 }
318
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100319 if (stats)
320 printf("nr_insns: %lu\n", nr_insns);
321
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500322 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500323
324err:
325 free(insn);
326 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500327}
328
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700329static struct instruction *find_last_insn(struct objtool_file *file,
330 struct section *sec)
331{
332 struct instruction *insn = NULL;
333 unsigned int offset;
334 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
335
336 for (offset = sec->len - 1; offset >= end && !insn; offset--)
337 insn = find_insn(file, sec, offset);
338
339 return insn;
340}
341
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500342/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500343 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500344 */
345static int add_dead_ends(struct objtool_file *file)
346{
347 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700348 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500349 struct instruction *insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500350
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500351 /*
352 * By default, "ud2" is a dead end unless otherwise annotated, because
353 * GCC 7 inserts it for certain divide-by-zero cases.
354 */
355 for_each_insn(file, insn)
356 if (insn->type == INSN_BUG)
357 insn->dead_end = true;
358
359 /*
360 * Check for manually annotated dead ends.
361 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500362 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
363 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500364 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500365
Matt Helsleyf1974222020-05-29 14:01:13 -0700366 list_for_each_entry(reloc, &sec->reloc_list, list) {
367 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500368 WARN("unexpected relocation symbol type in %s", sec->name);
369 return -1;
370 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700371 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500372 if (insn)
373 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700374 else if (reloc->addend == reloc->sym->sec->len) {
375 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700376 if (!insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500377 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700378 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500379 return -1;
380 }
381 } else {
382 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700383 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500384 return -1;
385 }
386
387 insn->dead_end = true;
388 }
389
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500390reachable:
391 /*
392 * These manually annotated reachable checks are needed for GCC 4.4,
393 * where the Linux unreachable() macro isn't supported. In that case
394 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
395 * not a dead end.
396 */
397 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
398 if (!sec)
399 return 0;
400
Matt Helsleyf1974222020-05-29 14:01:13 -0700401 list_for_each_entry(reloc, &sec->reloc_list, list) {
402 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500403 WARN("unexpected relocation symbol type in %s", sec->name);
404 return -1;
405 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700406 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500407 if (insn)
408 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700409 else if (reloc->addend == reloc->sym->sec->len) {
410 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700411 if (!insn) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500412 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700413 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500414 return -1;
415 }
416 } else {
417 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700418 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500419 return -1;
420 }
421
422 insn->dead_end = false;
423 }
424
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500425 return 0;
426}
427
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200428static int create_static_call_sections(struct objtool_file *file)
429{
430 struct section *sec, *reloc_sec;
431 struct reloc *reloc;
432 struct static_call_site *site;
433 struct instruction *insn;
434 struct symbol *key_sym;
435 char *key_name, *tmp;
436 int idx;
437
438 sec = find_section_by_name(file->elf, ".static_call_sites");
439 if (sec) {
440 INIT_LIST_HEAD(&file->static_call_list);
441 WARN("file already has .static_call_sites section, skipping");
442 return 0;
443 }
444
445 if (list_empty(&file->static_call_list))
446 return 0;
447
448 idx = 0;
449 list_for_each_entry(insn, &file->static_call_list, static_call_node)
450 idx++;
451
452 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
453 sizeof(struct static_call_site), idx);
454 if (!sec)
455 return -1;
456
457 reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
458 if (!reloc_sec)
459 return -1;
460
461 idx = 0;
462 list_for_each_entry(insn, &file->static_call_list, static_call_node) {
463
464 site = (struct static_call_site *)sec->data->d_buf + idx;
465 memset(site, 0, sizeof(struct static_call_site));
466
467 /* populate reloc for 'addr' */
468 reloc = malloc(sizeof(*reloc));
Josh Poimboeuf44f6a7c2020-12-14 16:04:20 -0600469
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200470 if (!reloc) {
471 perror("malloc");
472 return -1;
473 }
474 memset(reloc, 0, sizeof(*reloc));
Josh Poimboeuf44f6a7c2020-12-14 16:04:20 -0600475
476 insn_to_reloc_sym_addend(insn->sec, insn->offset, reloc);
477 if (!reloc->sym) {
478 WARN_FUNC("static call tramp: missing containing symbol",
479 insn->sec, insn->offset);
480 return -1;
481 }
482
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200483 reloc->type = R_X86_64_PC32;
484 reloc->offset = idx * sizeof(struct static_call_site);
485 reloc->sec = reloc_sec;
486 elf_add_reloc(file->elf, reloc);
487
488 /* find key symbol */
489 key_name = strdup(insn->call_dest->name);
490 if (!key_name) {
491 perror("strdup");
492 return -1;
493 }
494 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
495 STATIC_CALL_TRAMP_PREFIX_LEN)) {
496 WARN("static_call: trampoline name malformed: %s", key_name);
497 return -1;
498 }
499 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
500 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
501
502 key_sym = find_symbol_by_name(file->elf, tmp);
503 if (!key_sym) {
504 WARN("static_call: can't find static_call_key symbol: %s", tmp);
505 return -1;
506 }
507 free(key_name);
508
509 /* populate reloc for 'key' */
510 reloc = malloc(sizeof(*reloc));
511 if (!reloc) {
512 perror("malloc");
513 return -1;
514 }
515 memset(reloc, 0, sizeof(*reloc));
516 reloc->sym = key_sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200517 reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200518 reloc->type = R_X86_64_PC32;
519 reloc->offset = idx * sizeof(struct static_call_site) + 4;
520 reloc->sec = reloc_sec;
521 elf_add_reloc(file->elf, reloc);
522
523 idx++;
524 }
525
526 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
527 return -1;
528
529 return 0;
530}
531
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500532/*
533 * Warnings shouldn't be reported for ignored functions.
534 */
535static void add_ignores(struct objtool_file *file)
536{
537 struct instruction *insn;
538 struct section *sec;
539 struct symbol *func;
Matt Helsleyf1974222020-05-29 14:01:13 -0700540 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500541
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100542 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
543 if (!sec)
544 return;
545
Matt Helsleyf1974222020-05-29 14:01:13 -0700546 list_for_each_entry(reloc, &sec->reloc_list, list) {
547 switch (reloc->sym->type) {
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100548 case STT_FUNC:
Matt Helsleyf1974222020-05-29 14:01:13 -0700549 func = reloc->sym;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100550 break;
551
552 case STT_SECTION:
Matt Helsleyf1974222020-05-29 14:01:13 -0700553 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600554 if (!func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500555 continue;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100556 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500557
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100558 default:
Matt Helsleyf1974222020-05-29 14:01:13 -0700559 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100560 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500561 }
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100562
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100563 func_for_each_insn(file, func, insn)
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100564 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500565 }
566}
567
568/*
Peter Zijlstraea242132019-02-25 12:50:09 +0100569 * This is a whitelist of functions that is allowed to be called with AC set.
570 * The list is meant to be minimal and only contains compiler instrumentation
571 * ABI and a few functions used to implement *_{to,from}_user() functions.
572 *
573 * These functions must not directly change AC, but may PUSHF/POPF.
574 */
575static const char *uaccess_safe_builtin[] = {
576 /* KASAN */
577 "kasan_report",
578 "check_memory_region",
579 /* KASAN out-of-line */
580 "__asan_loadN_noabort",
581 "__asan_load1_noabort",
582 "__asan_load2_noabort",
583 "__asan_load4_noabort",
584 "__asan_load8_noabort",
585 "__asan_load16_noabort",
586 "__asan_storeN_noabort",
587 "__asan_store1_noabort",
588 "__asan_store2_noabort",
589 "__asan_store4_noabort",
590 "__asan_store8_noabort",
591 "__asan_store16_noabort",
Jann Hornb0b8e562020-09-29 00:49:16 +0200592 "__kasan_check_read",
593 "__kasan_check_write",
Peter Zijlstraea242132019-02-25 12:50:09 +0100594 /* KASAN in-line */
595 "__asan_report_load_n_noabort",
596 "__asan_report_load1_noabort",
597 "__asan_report_load2_noabort",
598 "__asan_report_load4_noabort",
599 "__asan_report_load8_noabort",
600 "__asan_report_load16_noabort",
601 "__asan_report_store_n_noabort",
602 "__asan_report_store1_noabort",
603 "__asan_report_store2_noabort",
604 "__asan_report_store4_noabort",
605 "__asan_report_store8_noabort",
606 "__asan_report_store16_noabort",
Marco Elver5f5c9712019-11-14 19:02:57 +0100607 /* KCSAN */
Marco Elver99676832020-03-25 17:41:57 +0100608 "__kcsan_check_access",
Marco Elver5f5c9712019-11-14 19:02:57 +0100609 "kcsan_found_watchpoint",
610 "kcsan_setup_watchpoint",
Marco Elver99676832020-03-25 17:41:57 +0100611 "kcsan_check_scoped_accesses",
Marco Elver50a19ad2020-04-24 17:47:30 +0200612 "kcsan_disable_current",
613 "kcsan_enable_current_nowarn",
Marco Elver5f5c9712019-11-14 19:02:57 +0100614 /* KCSAN/TSAN */
615 "__tsan_func_entry",
616 "__tsan_func_exit",
617 "__tsan_read_range",
618 "__tsan_write_range",
619 "__tsan_read1",
620 "__tsan_read2",
621 "__tsan_read4",
622 "__tsan_read8",
623 "__tsan_read16",
624 "__tsan_write1",
625 "__tsan_write2",
626 "__tsan_write4",
627 "__tsan_write8",
628 "__tsan_write16",
Marco Elvera81b3752020-07-24 09:00:02 +0200629 "__tsan_read_write1",
630 "__tsan_read_write2",
631 "__tsan_read_write4",
632 "__tsan_read_write8",
633 "__tsan_read_write16",
Marco Elver883957b2020-07-03 15:40:30 +0200634 "__tsan_atomic8_load",
635 "__tsan_atomic16_load",
636 "__tsan_atomic32_load",
637 "__tsan_atomic64_load",
638 "__tsan_atomic8_store",
639 "__tsan_atomic16_store",
640 "__tsan_atomic32_store",
641 "__tsan_atomic64_store",
642 "__tsan_atomic8_exchange",
643 "__tsan_atomic16_exchange",
644 "__tsan_atomic32_exchange",
645 "__tsan_atomic64_exchange",
646 "__tsan_atomic8_fetch_add",
647 "__tsan_atomic16_fetch_add",
648 "__tsan_atomic32_fetch_add",
649 "__tsan_atomic64_fetch_add",
650 "__tsan_atomic8_fetch_sub",
651 "__tsan_atomic16_fetch_sub",
652 "__tsan_atomic32_fetch_sub",
653 "__tsan_atomic64_fetch_sub",
654 "__tsan_atomic8_fetch_and",
655 "__tsan_atomic16_fetch_and",
656 "__tsan_atomic32_fetch_and",
657 "__tsan_atomic64_fetch_and",
658 "__tsan_atomic8_fetch_or",
659 "__tsan_atomic16_fetch_or",
660 "__tsan_atomic32_fetch_or",
661 "__tsan_atomic64_fetch_or",
662 "__tsan_atomic8_fetch_xor",
663 "__tsan_atomic16_fetch_xor",
664 "__tsan_atomic32_fetch_xor",
665 "__tsan_atomic64_fetch_xor",
666 "__tsan_atomic8_fetch_nand",
667 "__tsan_atomic16_fetch_nand",
668 "__tsan_atomic32_fetch_nand",
669 "__tsan_atomic64_fetch_nand",
670 "__tsan_atomic8_compare_exchange_strong",
671 "__tsan_atomic16_compare_exchange_strong",
672 "__tsan_atomic32_compare_exchange_strong",
673 "__tsan_atomic64_compare_exchange_strong",
674 "__tsan_atomic8_compare_exchange_weak",
675 "__tsan_atomic16_compare_exchange_weak",
676 "__tsan_atomic32_compare_exchange_weak",
677 "__tsan_atomic64_compare_exchange_weak",
678 "__tsan_atomic8_compare_exchange_val",
679 "__tsan_atomic16_compare_exchange_val",
680 "__tsan_atomic32_compare_exchange_val",
681 "__tsan_atomic64_compare_exchange_val",
682 "__tsan_atomic_thread_fence",
683 "__tsan_atomic_signal_fence",
Peter Zijlstraea242132019-02-25 12:50:09 +0100684 /* KCOV */
685 "write_comp_data",
Josh Poimboeufae033f02020-04-29 14:09:04 -0500686 "check_kcov_mode",
Peter Zijlstraea242132019-02-25 12:50:09 +0100687 "__sanitizer_cov_trace_pc",
688 "__sanitizer_cov_trace_const_cmp1",
689 "__sanitizer_cov_trace_const_cmp2",
690 "__sanitizer_cov_trace_const_cmp4",
691 "__sanitizer_cov_trace_const_cmp8",
692 "__sanitizer_cov_trace_cmp1",
693 "__sanitizer_cov_trace_cmp2",
694 "__sanitizer_cov_trace_cmp4",
695 "__sanitizer_cov_trace_cmp8",
Al Viro36b1c702020-02-16 13:07:49 -0500696 "__sanitizer_cov_trace_switch",
Peter Zijlstraea242132019-02-25 12:50:09 +0100697 /* UBSAN */
698 "ubsan_type_mismatch_common",
699 "__ubsan_handle_type_mismatch",
700 "__ubsan_handle_type_mismatch_v1",
Peter Zijlstra9a50dca2019-10-21 15:11:49 +0200701 "__ubsan_handle_shift_out_of_bounds",
Peter Zijlstraea242132019-02-25 12:50:09 +0100702 /* misc */
703 "csum_partial_copy_generic",
Dan Williamsec6347b2020-10-05 20:40:16 -0700704 "copy_mc_fragile",
705 "copy_mc_fragile_handle_tail",
Dan Williams5da8e4a2020-10-05 20:40:25 -0700706 "copy_mc_enhanced_fast_string",
Peter Zijlstraea242132019-02-25 12:50:09 +0100707 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
708 NULL
709};
710
711static void add_uaccess_safe(struct objtool_file *file)
712{
713 struct symbol *func;
714 const char **name;
715
716 if (!uaccess)
717 return;
718
719 for (name = uaccess_safe_builtin; *name; name++) {
720 func = find_symbol_by_name(file->elf, *name);
721 if (!func)
722 continue;
723
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500724 func->uaccess_safe = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500725 }
726}
727
728/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000729 * FIXME: For now, just ignore any alternatives which add retpolines. This is
730 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
731 * But it at least allows objtool to understand the control flow *around* the
732 * retpoline.
733 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100734static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000735{
736 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700737 struct reloc *reloc;
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000738 struct instruction *insn;
739
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100740 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000741 if (!sec)
742 return 0;
743
Matt Helsleyf1974222020-05-29 14:01:13 -0700744 list_for_each_entry(reloc, &sec->reloc_list, list) {
745 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000746 WARN("unexpected relocation symbol type in %s", sec->name);
747 return -1;
748 }
749
Matt Helsleyf1974222020-05-29 14:01:13 -0700750 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000751 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100752 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000753 return -1;
754 }
755
756 insn->ignore_alts = true;
757 }
758
759 return 0;
760}
761
762/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500763 * Find the destination instructions for all jumps.
764 */
765static int add_jump_destinations(struct objtool_file *file)
766{
767 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -0700768 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500769 struct section *dest_sec;
770 unsigned long dest_off;
771
772 for_each_insn(file, insn) {
Josh Poimboeufa2296142020-02-10 12:32:39 -0600773 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500774 continue;
775
Matt Helsleyf1974222020-05-29 14:01:13 -0700776 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100777 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700778 if (!reloc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500779 dest_sec = insn->sec;
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000780 dest_off = arch_jump_destination(insn);
Matt Helsleyf1974222020-05-29 14:01:13 -0700781 } else if (reloc->sym->type == STT_SECTION) {
782 dest_sec = reloc->sym->sec;
783 dest_off = arch_dest_reloc_offset(reloc->addend);
784 } else if (reloc->sym->sec->idx) {
785 dest_sec = reloc->sym->sec;
786 dest_off = reloc->sym->sym.st_value +
787 arch_dest_reloc_offset(reloc->addend);
788 } else if (strstr(reloc->sym->name, "_indirect_thunk_")) {
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000789 /*
790 * Retpoline jumps are really dynamic jumps in
791 * disguise, so convert them accordingly.
792 */
Josh Poimboeufb68b9902019-07-17 20:36:57 -0500793 if (insn->type == INSN_JUMP_UNCONDITIONAL)
794 insn->type = INSN_JUMP_DYNAMIC;
795 else
796 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
797
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100798 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000799 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500800 } else {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500801 /* external sibling call */
Matt Helsleyf1974222020-05-29 14:01:13 -0700802 insn->call_dest = reloc->sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200803 if (insn->call_dest->static_call_tramp) {
804 list_add_tail(&insn->static_call_node,
805 &file->static_call_list);
806 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500807 continue;
808 }
809
810 insn->jump_dest = find_insn(file, dest_sec, dest_off);
811 if (!insn->jump_dest) {
812
813 /*
814 * This is a special case where an alt instruction
815 * jumps past the end of the section. These are
816 * handled later in handle_group_alt().
817 */
818 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
819 continue;
820
821 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
822 insn->sec, insn->offset, dest_sec->name,
823 dest_off);
824 return -1;
825 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500826
827 /*
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100828 * Cross-function jump.
Josh Poimboeufcd778492018-06-01 07:23:51 -0500829 */
830 if (insn->func && insn->jump_dest->func &&
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100831 insn->func != insn->jump_dest->func) {
832
833 /*
834 * For GCC 8+, create parent/child links for any cold
835 * subfunctions. This is _mostly_ redundant with a
836 * similar initialization in read_symbols().
837 *
838 * If a function has aliases, we want the *first* such
839 * function in the symbol table to be the subfunction's
840 * parent. In that case we overwrite the
841 * initialization done in read_symbols().
842 *
843 * However this code can't completely replace the
844 * read_symbols() code because this doesn't detect the
845 * case where the parent function's only reference to a
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500846 * subfunction is through a jump table.
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100847 */
848 if (!strstr(insn->func->name, ".cold.") &&
849 strstr(insn->jump_dest->func->name, ".cold.")) {
850 insn->func->cfunc = insn->jump_dest->func;
851 insn->jump_dest->func->pfunc = insn->func;
852
853 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
854 insn->jump_dest->offset == insn->jump_dest->func->offset) {
855
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500856 /* internal sibling call */
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100857 insn->call_dest = insn->jump_dest->func;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200858 if (insn->call_dest->static_call_tramp) {
859 list_add_tail(&insn->static_call_node,
860 &file->static_call_list);
861 }
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100862 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500863 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500864 }
865
866 return 0;
867}
868
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200869static void remove_insn_ops(struct instruction *insn)
870{
871 struct stack_op *op, *tmp;
872
873 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
874 list_del(&op->list);
875 free(op);
876 }
877}
878
Julien Thierry2b232a22020-09-15 08:53:18 +0100879static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
880{
881 struct symbol *call_dest;
882
883 call_dest = find_func_by_offset(sec, offset);
884 if (!call_dest)
885 call_dest = find_symbol_by_offset(sec, offset);
886
887 return call_dest;
888}
889
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500890/*
891 * Find the destination instructions for all calls.
892 */
893static int add_call_destinations(struct objtool_file *file)
894{
895 struct instruction *insn;
896 unsigned long dest_off;
Matt Helsleyf1974222020-05-29 14:01:13 -0700897 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500898
899 for_each_insn(file, insn) {
900 if (insn->type != INSN_CALL)
901 continue;
902
Matt Helsleyf1974222020-05-29 14:01:13 -0700903 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100904 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700905 if (!reloc) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000906 dest_off = arch_jump_destination(insn);
Julien Thierry2b232a22020-09-15 08:53:18 +0100907 insn->call_dest = find_call_destination(insn->sec, dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600908
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600909 if (insn->ignore)
910 continue;
911
912 if (!insn->call_dest) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200913 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500914 return -1;
915 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600916
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600917 if (insn->func && insn->call_dest->type != STT_FUNC) {
918 WARN_FUNC("unsupported call to non-function",
919 insn->sec, insn->offset);
920 return -1;
921 }
922
Matt Helsleyf1974222020-05-29 14:01:13 -0700923 } else if (reloc->sym->type == STT_SECTION) {
924 dest_off = arch_dest_reloc_offset(reloc->addend);
Julien Thierry2b232a22020-09-15 08:53:18 +0100925 insn->call_dest = find_call_destination(reloc->sym->sec,
926 dest_off);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600927 if (!insn->call_dest) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000928 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500929 insn->sec, insn->offset,
Matt Helsleyf1974222020-05-29 14:01:13 -0700930 reloc->sym->sec->name,
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000931 dest_off);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500932 return -1;
933 }
934 } else
Matt Helsleyf1974222020-05-29 14:01:13 -0700935 insn->call_dest = reloc->sym;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200936
937 /*
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200938 * Many compilers cannot disable KCOV with a function attribute
939 * so they need a little help, NOP out any KCOV calls from noinstr
940 * text.
941 */
942 if (insn->sec->noinstr &&
943 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
Peter Zijlstrad832c002020-06-18 17:55:29 +0200944 if (reloc) {
945 reloc->type = R_NONE;
946 elf_write_reloc(file->elf, reloc);
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200947 }
948
949 elf_write_insn(file->elf, insn->sec,
950 insn->offset, insn->len,
951 arch_nop_insn(insn->len));
952 insn->type = INSN_NOP;
953 }
954
955 /*
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200956 * Whatever stack impact regular CALLs have, should be undone
957 * by the RETURN of the called function.
958 *
959 * Annotated intra-function calls retain the stack_ops but
960 * are converted to JUMP, see read_intra_function_calls().
961 */
962 remove_insn_ops(insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500963 }
964
965 return 0;
966}
967
968/*
Josh Poimboeufc9c324d2020-12-18 14:26:21 -0600969 * The .alternatives section requires some extra special care over and above
970 * other special sections because alternatives are patched in place.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500971 */
972static int handle_group_alt(struct objtool_file *file,
973 struct special_alt *special_alt,
974 struct instruction *orig_insn,
975 struct instruction **new_insn)
976{
Josh Poimboeufc9c324d2020-12-18 14:26:21 -0600977 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
Josh Poimboeufb23cc712020-12-18 14:19:32 -0600978 struct alt_group *orig_alt_group, *new_alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500979 unsigned long dest_off;
980
Josh Poimboeufb23cc712020-12-18 14:19:32 -0600981
982 orig_alt_group = malloc(sizeof(*orig_alt_group));
983 if (!orig_alt_group) {
984 WARN("malloc failed");
985 return -1;
986 }
Josh Poimboeufc9c324d2020-12-18 14:26:21 -0600987 orig_alt_group->cfi = calloc(special_alt->orig_len,
988 sizeof(struct cfi_state *));
989 if (!orig_alt_group->cfi) {
990 WARN("calloc failed");
991 return -1;
992 }
993
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500994 last_orig_insn = NULL;
995 insn = orig_insn;
996 sec_for_each_insn_from(file, insn) {
997 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
998 break;
999
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001000 insn->alt_group = orig_alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001001 last_orig_insn = insn;
1002 }
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001003 orig_alt_group->orig_group = NULL;
1004 orig_alt_group->first_insn = orig_insn;
1005 orig_alt_group->last_insn = last_orig_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001006
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001007
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001008 new_alt_group = malloc(sizeof(*new_alt_group));
1009 if (!new_alt_group) {
1010 WARN("malloc failed");
1011 return -1;
1012 }
1013
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001014 if (special_alt->new_len < special_alt->orig_len) {
1015 /*
1016 * Insert a fake nop at the end to make the replacement
1017 * alt_group the same size as the original. This is needed to
1018 * allow propagate_alt_cfi() to do its magic. When the last
1019 * instruction affects the stack, the instruction after it (the
1020 * nop) will propagate the new state to the shared CFI array.
1021 */
1022 nop = malloc(sizeof(*nop));
1023 if (!nop) {
1024 WARN("malloc failed");
1025 return -1;
1026 }
1027 memset(nop, 0, sizeof(*nop));
1028 INIT_LIST_HEAD(&nop->alts);
1029 INIT_LIST_HEAD(&nop->stack_ops);
1030 init_cfi_state(&nop->cfi);
1031
1032 nop->sec = special_alt->new_sec;
1033 nop->offset = special_alt->new_off + special_alt->new_len;
1034 nop->len = special_alt->orig_len - special_alt->new_len;
1035 nop->type = INSN_NOP;
1036 nop->func = orig_insn->func;
1037 nop->alt_group = new_alt_group;
1038 nop->ignore = orig_insn->ignore_alts;
1039 }
1040
1041 if (!special_alt->new_len) {
1042 *new_insn = nop;
1043 goto end;
1044 }
1045
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001046 insn = *new_insn;
1047 sec_for_each_insn_from(file, insn) {
Julien Thierry45245f52020-09-04 16:30:23 +01001048 struct reloc *alt_reloc;
1049
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001050 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1051 break;
1052
1053 last_new_insn = insn;
1054
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001055 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +01001056 insn->func = orig_insn->func;
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001057 insn->alt_group = new_alt_group;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001058
Josh Poimboeufdc419722020-02-10 12:32:40 -06001059 /*
1060 * Since alternative replacement code is copy/pasted by the
1061 * kernel after applying relocations, generally such code can't
1062 * have relative-address relocation references to outside the
1063 * .altinstr_replacement section, unless the arch's
1064 * alternatives code can adjust the relative offsets
1065 * accordingly.
Josh Poimboeufdc419722020-02-10 12:32:40 -06001066 */
Julien Thierry45245f52020-09-04 16:30:23 +01001067 alt_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1068 insn->offset, insn->len);
1069 if (alt_reloc &&
1070 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
Josh Poimboeufdc419722020-02-10 12:32:40 -06001071
1072 WARN_FUNC("unsupported relocation in alternatives section",
1073 insn->sec, insn->offset);
1074 return -1;
1075 }
1076
Josh Poimboeufa2296142020-02-10 12:32:39 -06001077 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001078 continue;
1079
1080 if (!insn->immediate)
1081 continue;
1082
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001083 dest_off = arch_jump_destination(insn);
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001084 if (dest_off == special_alt->new_off + special_alt->new_len)
1085 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001086
1087 if (!insn->jump_dest) {
1088 WARN_FUNC("can't find alternative jump destination",
1089 insn->sec, insn->offset);
1090 return -1;
1091 }
1092 }
1093
1094 if (!last_new_insn) {
1095 WARN_FUNC("can't find last new alternative instruction",
1096 special_alt->new_sec, special_alt->new_off);
1097 return -1;
1098 }
1099
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001100 if (nop)
1101 list_add(&nop->list, &last_new_insn->list);
1102end:
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001103 new_alt_group->orig_group = orig_alt_group;
1104 new_alt_group->first_insn = *new_insn;
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001105 new_alt_group->last_insn = nop ? : last_new_insn;
1106 new_alt_group->cfi = orig_alt_group->cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001107 return 0;
1108}
1109
1110/*
1111 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1112 * If the original instruction is a jump, make the alt entry an effective nop
1113 * by just skipping the original instruction.
1114 */
1115static int handle_jump_alt(struct objtool_file *file,
1116 struct special_alt *special_alt,
1117 struct instruction *orig_insn,
1118 struct instruction **new_insn)
1119{
1120 if (orig_insn->type == INSN_NOP)
1121 return 0;
1122
1123 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1124 WARN_FUNC("unsupported instruction at jump label",
1125 orig_insn->sec, orig_insn->offset);
1126 return -1;
1127 }
1128
1129 *new_insn = list_next_entry(orig_insn, list);
1130 return 0;
1131}
1132
1133/*
1134 * Read all the special sections which have alternate instructions which can be
1135 * patched in or redirected to at runtime. Each instruction having alternate
1136 * instruction(s) has them added to its insn->alts list, which will be
1137 * traversed in validate_branch().
1138 */
1139static int add_special_section_alts(struct objtool_file *file)
1140{
1141 struct list_head special_alts;
1142 struct instruction *orig_insn, *new_insn;
1143 struct special_alt *special_alt, *tmp;
1144 struct alternative *alt;
1145 int ret;
1146
1147 ret = special_get_alts(file->elf, &special_alts);
1148 if (ret)
1149 return ret;
1150
1151 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001152
1153 orig_insn = find_insn(file, special_alt->orig_sec,
1154 special_alt->orig_off);
1155 if (!orig_insn) {
1156 WARN_FUNC("special: can't find orig instruction",
1157 special_alt->orig_sec, special_alt->orig_off);
1158 ret = -1;
1159 goto out;
1160 }
1161
1162 new_insn = NULL;
1163 if (!special_alt->group || special_alt->new_len) {
1164 new_insn = find_insn(file, special_alt->new_sec,
1165 special_alt->new_off);
1166 if (!new_insn) {
1167 WARN_FUNC("special: can't find new instruction",
1168 special_alt->new_sec,
1169 special_alt->new_off);
1170 ret = -1;
1171 goto out;
1172 }
1173 }
1174
1175 if (special_alt->group) {
Julien Thierry7170cf42020-03-27 15:28:41 +00001176 if (!special_alt->orig_len) {
1177 WARN_FUNC("empty alternative entry",
1178 orig_insn->sec, orig_insn->offset);
1179 continue;
1180 }
1181
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001182 ret = handle_group_alt(file, special_alt, orig_insn,
1183 &new_insn);
1184 if (ret)
1185 goto out;
1186 } else if (special_alt->jump_or_nop) {
1187 ret = handle_jump_alt(file, special_alt, orig_insn,
1188 &new_insn);
1189 if (ret)
1190 goto out;
1191 }
1192
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001193 alt = malloc(sizeof(*alt));
1194 if (!alt) {
1195 WARN("malloc failed");
1196 ret = -1;
1197 goto out;
1198 }
1199
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001200 alt->insn = new_insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +01001201 alt->skip_orig = special_alt->skip_orig;
Peter Zijlstraea242132019-02-25 12:50:09 +01001202 orig_insn->ignore_alts |= special_alt->skip_alt;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001203 list_add_tail(&alt->list, &orig_insn->alts);
1204
1205 list_del(&special_alt->list);
1206 free(special_alt);
1207 }
1208
1209out:
1210 return ret;
1211}
1212
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001213static int add_jump_table(struct objtool_file *file, struct instruction *insn,
Matt Helsleyf1974222020-05-29 14:01:13 -07001214 struct reloc *table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001215{
Matt Helsleyf1974222020-05-29 14:01:13 -07001216 struct reloc *reloc = table;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001217 struct instruction *dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001218 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001219 struct symbol *pfunc = insn->func->pfunc;
1220 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001221
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001222 /*
Matt Helsleyf1974222020-05-29 14:01:13 -07001223 * Each @reloc is a switch table relocation which points to the target
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001224 * instruction.
1225 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001226 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
Jann Hornbd98c812019-07-17 20:36:54 -05001227
1228 /* Check for the end of the table: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001229 if (reloc != table && reloc->jump_table_start)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001230 break;
1231
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001232 /* Make sure the table entries are consecutive: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001233 if (prev_offset && reloc->offset != prev_offset + 8)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001234 break;
1235
1236 /* Detect function pointers from contiguous objects: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001237 if (reloc->sym->sec == pfunc->sec &&
1238 reloc->addend == pfunc->offset)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001239 break;
1240
Matt Helsleyf1974222020-05-29 14:01:13 -07001241 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001242 if (!dest_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001243 break;
1244
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001245 /* Make sure the destination is in the same function: */
Josh Poimboeufe65050b2019-07-17 20:36:55 -05001246 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -05001247 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001248
1249 alt = malloc(sizeof(*alt));
1250 if (!alt) {
1251 WARN("malloc failed");
1252 return -1;
1253 }
1254
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001255 alt->insn = dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001256 list_add_tail(&alt->list, &insn->alts);
Matt Helsleyf1974222020-05-29 14:01:13 -07001257 prev_offset = reloc->offset;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001258 }
1259
1260 if (!prev_offset) {
1261 WARN_FUNC("can't find switch jump table",
1262 insn->sec, insn->offset);
1263 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001264 }
1265
1266 return 0;
1267}
1268
1269/*
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001270 * find_jump_table() - Given a dynamic jump, find the switch jump table
1271 * associated with it.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001272 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001273static struct reloc *find_jump_table(struct objtool_file *file,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001274 struct symbol *func,
1275 struct instruction *insn)
1276{
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001277 struct reloc *table_reloc;
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001278 struct instruction *dest_insn, *orig_insn = insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001279
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001280 /*
1281 * Backward search using the @first_jump_src links, these help avoid
1282 * much of the 'in between' code. Which avoids us getting confused by
1283 * it.
1284 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001285 for (;
Josh Poimboeuf1119d262020-04-28 16:45:16 -05001286 insn && insn->func && insn->func->pfunc == func;
1287 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001288
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001289 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001290 break;
1291
1292 /* allow small jumps within the range */
1293 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1294 insn->jump_dest &&
1295 (insn->jump_dest->offset <= insn->offset ||
1296 insn->jump_dest->offset > orig_insn->offset))
1297 break;
1298
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001299 table_reloc = arch_find_switch_table(file, insn);
Matt Helsleyf1974222020-05-29 14:01:13 -07001300 if (!table_reloc)
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001301 continue;
Matt Helsleyf1974222020-05-29 14:01:13 -07001302 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001303 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1304 continue;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001305
Matt Helsleyf1974222020-05-29 14:01:13 -07001306 return table_reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001307 }
1308
1309 return NULL;
1310}
1311
Jann Hornbd98c812019-07-17 20:36:54 -05001312/*
1313 * First pass: Mark the head of each jump table so that in the next pass,
1314 * we know when a given jump table ends and the next one starts.
1315 */
1316static void mark_func_jump_tables(struct objtool_file *file,
1317 struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001318{
Jann Hornbd98c812019-07-17 20:36:54 -05001319 struct instruction *insn, *last = NULL;
Matt Helsleyf1974222020-05-29 14:01:13 -07001320 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001321
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001322 func_for_each_insn(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001323 if (!last)
1324 last = insn;
1325
1326 /*
1327 * Store back-pointers for unconditional forward jumps such
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001328 * that find_jump_table() can back-track using those and
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001329 * avoid some potentially confusing code.
1330 */
1331 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1332 insn->offset > last->offset &&
1333 insn->jump_dest->offset > insn->offset &&
1334 !insn->jump_dest->first_jump_src) {
1335
1336 insn->jump_dest->first_jump_src = insn;
1337 last = insn->jump_dest;
1338 }
1339
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001340 if (insn->type != INSN_JUMP_DYNAMIC)
1341 continue;
1342
Matt Helsleyf1974222020-05-29 14:01:13 -07001343 reloc = find_jump_table(file, func, insn);
1344 if (reloc) {
1345 reloc->jump_table_start = true;
1346 insn->jump_table = reloc;
Jann Hornbd98c812019-07-17 20:36:54 -05001347 }
1348 }
1349}
1350
1351static int add_func_jump_tables(struct objtool_file *file,
1352 struct symbol *func)
1353{
1354 struct instruction *insn;
1355 int ret;
1356
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001357 func_for_each_insn(file, func, insn) {
Jann Hornbd98c812019-07-17 20:36:54 -05001358 if (!insn->jump_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001359 continue;
1360
Jann Hornbd98c812019-07-17 20:36:54 -05001361 ret = add_jump_table(file, insn, insn->jump_table);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001362 if (ret)
1363 return ret;
1364 }
1365
1366 return 0;
1367}
1368
1369/*
1370 * For some switch statements, gcc generates a jump table in the .rodata
1371 * section which contains a list of addresses within the function to jump to.
1372 * This finds these jump tables and adds them to the insn->alts lists.
1373 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001374static int add_jump_table_alts(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001375{
1376 struct section *sec;
1377 struct symbol *func;
1378 int ret;
1379
Allan Xavier4a60aa02018-09-07 08:12:01 -05001380 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001381 return 0;
1382
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001383 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001384 list_for_each_entry(func, &sec->symbol_list, list) {
1385 if (func->type != STT_FUNC)
1386 continue;
1387
Jann Hornbd98c812019-07-17 20:36:54 -05001388 mark_func_jump_tables(file, func);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001389 ret = add_func_jump_tables(file, func);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001390 if (ret)
1391 return ret;
1392 }
1393 }
1394
1395 return 0;
1396}
1397
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001398static int read_unwind_hints(struct objtool_file *file)
1399{
Matt Helsleyf1974222020-05-29 14:01:13 -07001400 struct section *sec, *relocsec;
1401 struct reloc *reloc;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001402 struct unwind_hint *hint;
1403 struct instruction *insn;
1404 struct cfi_reg *cfa;
1405 int i;
1406
1407 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1408 if (!sec)
1409 return 0;
1410
Matt Helsleyf1974222020-05-29 14:01:13 -07001411 relocsec = sec->reloc;
1412 if (!relocsec) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001413 WARN("missing .rela.discard.unwind_hints section");
1414 return -1;
1415 }
1416
1417 if (sec->len % sizeof(struct unwind_hint)) {
1418 WARN("struct unwind_hint size mismatch");
1419 return -1;
1420 }
1421
1422 file->hints = true;
1423
1424 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1425 hint = (struct unwind_hint *)sec->data->d_buf + i;
1426
Matt Helsleyf1974222020-05-29 14:01:13 -07001427 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1428 if (!reloc) {
1429 WARN("can't find reloc for unwind_hints[%d]", i);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001430 return -1;
1431 }
1432
Matt Helsleyf1974222020-05-29 14:01:13 -07001433 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001434 if (!insn) {
1435 WARN("can't find insn for unwind_hints[%d]", i);
1436 return -1;
1437 }
1438
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001439 cfa = &insn->cfi.cfa;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001440
Peter Zijlstrac536ed22020-04-01 16:54:26 +02001441 if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
Vasily Gorbik8bfe2732020-11-13 00:03:29 +01001442 insn->ret_offset = bswap_if_needed(hint->sp_offset);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001443 continue;
1444 }
1445
1446 insn->hint = true;
1447
Julien Thierryedea9e62020-09-04 16:30:28 +01001448 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001449 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1450 insn->sec, insn->offset, hint->sp_reg);
1451 return -1;
1452 }
1453
Vasily Gorbik8bfe2732020-11-13 00:03:29 +01001454 cfa->offset = bswap_if_needed(hint->sp_offset);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001455 insn->cfi.type = hint->type;
1456 insn->cfi.end = hint->end;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001457 }
1458
1459 return 0;
1460}
1461
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001462static int read_retpoline_hints(struct objtool_file *file)
1463{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001464 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001465 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001466 struct reloc *reloc;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001467
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001468 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001469 if (!sec)
1470 return 0;
1471
Matt Helsleyf1974222020-05-29 14:01:13 -07001472 list_for_each_entry(reloc, &sec->reloc_list, list) {
1473 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001474 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001475 return -1;
1476 }
1477
Matt Helsleyf1974222020-05-29 14:01:13 -07001478 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001479 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001480 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001481 return -1;
1482 }
1483
1484 if (insn->type != INSN_JUMP_DYNAMIC &&
1485 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001486 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001487 insn->sec, insn->offset);
1488 return -1;
1489 }
1490
1491 insn->retpoline_safe = true;
1492 }
1493
1494 return 0;
1495}
1496
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001497static int read_instr_hints(struct objtool_file *file)
1498{
1499 struct section *sec;
1500 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001501 struct reloc *reloc;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001502
1503 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1504 if (!sec)
1505 return 0;
1506
Matt Helsleyf1974222020-05-29 14:01:13 -07001507 list_for_each_entry(reloc, &sec->reloc_list, list) {
1508 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001509 WARN("unexpected relocation symbol type in %s", sec->name);
1510 return -1;
1511 }
1512
Matt Helsleyf1974222020-05-29 14:01:13 -07001513 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001514 if (!insn) {
1515 WARN("bad .discard.instr_end entry");
1516 return -1;
1517 }
1518
1519 insn->instr--;
1520 }
1521
1522 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1523 if (!sec)
1524 return 0;
1525
Matt Helsleyf1974222020-05-29 14:01:13 -07001526 list_for_each_entry(reloc, &sec->reloc_list, list) {
1527 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001528 WARN("unexpected relocation symbol type in %s", sec->name);
1529 return -1;
1530 }
1531
Matt Helsleyf1974222020-05-29 14:01:13 -07001532 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001533 if (!insn) {
1534 WARN("bad .discard.instr_begin entry");
1535 return -1;
1536 }
1537
1538 insn->instr++;
1539 }
1540
1541 return 0;
1542}
1543
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001544static int read_intra_function_calls(struct objtool_file *file)
1545{
1546 struct instruction *insn;
1547 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -07001548 struct reloc *reloc;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001549
1550 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1551 if (!sec)
1552 return 0;
1553
Matt Helsleyf1974222020-05-29 14:01:13 -07001554 list_for_each_entry(reloc, &sec->reloc_list, list) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001555 unsigned long dest_off;
1556
Matt Helsleyf1974222020-05-29 14:01:13 -07001557 if (reloc->sym->type != STT_SECTION) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001558 WARN("unexpected relocation symbol type in %s",
1559 sec->name);
1560 return -1;
1561 }
1562
Matt Helsleyf1974222020-05-29 14:01:13 -07001563 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001564 if (!insn) {
1565 WARN("bad .discard.intra_function_call entry");
1566 return -1;
1567 }
1568
1569 if (insn->type != INSN_CALL) {
1570 WARN_FUNC("intra_function_call not a direct call",
1571 insn->sec, insn->offset);
1572 return -1;
1573 }
1574
1575 /*
1576 * Treat intra-function CALLs as JMPs, but with a stack_op.
1577 * See add_call_destinations(), which strips stack_ops from
1578 * normal CALLs.
1579 */
1580 insn->type = INSN_JUMP_UNCONDITIONAL;
1581
1582 dest_off = insn->offset + insn->len + insn->immediate;
1583 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1584 if (!insn->jump_dest) {
1585 WARN_FUNC("can't find call dest at %s+0x%lx",
1586 insn->sec, insn->offset,
1587 insn->sec->name, dest_off);
1588 return -1;
1589 }
1590 }
1591
1592 return 0;
1593}
1594
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02001595static int read_static_call_tramps(struct objtool_file *file)
1596{
1597 struct section *sec;
1598 struct symbol *func;
1599
1600 for_each_sec(file, sec) {
1601 list_for_each_entry(func, &sec->symbol_list, list) {
1602 if (func->bind == STB_GLOBAL &&
1603 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1604 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1605 func->static_call_tramp = true;
1606 }
1607 }
1608
1609 return 0;
1610}
1611
Allan Xavier4a60aa02018-09-07 08:12:01 -05001612static void mark_rodata(struct objtool_file *file)
1613{
1614 struct section *sec;
1615 bool found = false;
1616
1617 /*
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001618 * Search for the following rodata sections, each of which can
1619 * potentially contain jump tables:
1620 *
1621 * - .rodata: can contain GCC switch tables
1622 * - .rodata.<func>: same, if -fdata-sections is being used
1623 * - .rodata..c_jump_table: contains C annotated jump tables
1624 *
1625 * .rodata.str1.* sections are ignored; they don't contain jump tables.
Allan Xavier4a60aa02018-09-07 08:12:01 -05001626 */
1627 for_each_sec(file, sec) {
Muchun Song1ee444702020-04-12 22:44:05 +08001628 if (!strncmp(sec->name, ".rodata", 7) &&
1629 !strstr(sec->name, ".str1.")) {
Allan Xavier4a60aa02018-09-07 08:12:01 -05001630 sec->rodata = true;
1631 found = true;
1632 }
1633 }
1634
1635 file->rodata = found;
1636}
1637
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001638static int decode_sections(struct objtool_file *file)
1639{
1640 int ret;
1641
Allan Xavier4a60aa02018-09-07 08:12:01 -05001642 mark_rodata(file);
1643
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001644 ret = decode_instructions(file);
1645 if (ret)
1646 return ret;
1647
1648 ret = add_dead_ends(file);
1649 if (ret)
1650 return ret;
1651
1652 add_ignores(file);
Peter Zijlstraea242132019-02-25 12:50:09 +01001653 add_uaccess_safe(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001654
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001655 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001656 if (ret)
1657 return ret;
1658
Peter Zijlstra5b06fd32020-08-18 15:57:49 +02001659 ret = read_static_call_tramps(file);
1660 if (ret)
1661 return ret;
1662
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001663 ret = add_jump_destinations(file);
1664 if (ret)
1665 return ret;
1666
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001667 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001668 if (ret)
1669 return ret;
1670
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001671 ret = read_intra_function_calls(file);
1672 if (ret)
1673 return ret;
1674
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001675 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001676 if (ret)
1677 return ret;
1678
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001679 ret = add_jump_table_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001680 if (ret)
1681 return ret;
1682
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001683 ret = read_unwind_hints(file);
1684 if (ret)
1685 return ret;
1686
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001687 ret = read_retpoline_hints(file);
1688 if (ret)
1689 return ret;
1690
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001691 ret = read_instr_hints(file);
1692 if (ret)
1693 return ret;
1694
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001695 return 0;
1696}
1697
1698static bool is_fentry_call(struct instruction *insn)
1699{
Alexandre Chartre87cf61f2020-04-14 12:36:10 +02001700 if (insn->type == INSN_CALL && insn->call_dest &&
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001701 insn->call_dest->type == STT_NOTYPE &&
1702 !strcmp(insn->call_dest->name, "__fentry__"))
1703 return true;
1704
1705 return false;
1706}
1707
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001708static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001709{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001710 u8 ret_offset = insn->ret_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001711 struct cfi_state *cfi = &state->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001712 int i;
1713
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001714 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001715 return true;
1716
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001717 if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001718 return true;
1719
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001720 if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001721 return true;
1722
Alexandre Chartrec721b3f2020-04-07 09:31:35 +02001723 /*
1724 * If there is a ret offset hint then don't check registers
1725 * because a callee-saved register might have been pushed on
1726 * the stack.
1727 */
1728 if (ret_offset)
1729 return false;
1730
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001731 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001732 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1733 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001734 return true;
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001735 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001736
1737 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001738}
1739
Julien Thierryfb084fd2020-10-14 08:38:00 +01001740static bool check_reg_frame_pos(const struct cfi_reg *reg,
1741 int expected_offset)
1742{
1743 return reg->base == CFI_CFA &&
1744 reg->offset == expected_offset;
1745}
1746
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001747static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001748{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001749 struct cfi_state *cfi = &state->cfi;
1750
Julien Thierryfb084fd2020-10-14 08:38:00 +01001751 if (cfi->cfa.base == CFI_BP &&
1752 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
1753 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001754 return true;
1755
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001756 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001757 return true;
1758
1759 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001760}
1761
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001762static int update_cfi_state_regs(struct instruction *insn,
1763 struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001764 struct stack_op *op)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001765{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001766 struct cfi_reg *cfa = &cfi->cfa;
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001767
Josh Poimboeufd8dd25a2020-04-25 05:03:00 -05001768 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001769 return 0;
1770
1771 /* push */
Peter Zijlstraea242132019-02-25 12:50:09 +01001772 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001773 cfa->offset += 8;
1774
1775 /* pop */
Peter Zijlstraea242132019-02-25 12:50:09 +01001776 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001777 cfa->offset -= 8;
1778
1779 /* add immediate to sp */
1780 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1781 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1782 cfa->offset -= op->src.offset;
1783
1784 return 0;
1785}
1786
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001787static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001788{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001789 if (arch_callee_saved_reg(reg) &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001790 cfi->regs[reg].base == CFI_UNDEFINED) {
1791 cfi->regs[reg].base = base;
1792 cfi->regs[reg].offset = offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001793 }
1794}
1795
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001796static void restore_reg(struct cfi_state *cfi, unsigned char reg)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001797{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001798 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1799 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001800}
1801
1802/*
1803 * A note about DRAP stack alignment:
1804 *
1805 * GCC has the concept of a DRAP register, which is used to help keep track of
1806 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1807 * register. The typical DRAP pattern is:
1808 *
1809 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1810 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1811 * 41 ff 72 f8 pushq -0x8(%r10)
1812 * 55 push %rbp
1813 * 48 89 e5 mov %rsp,%rbp
1814 * (more pushes)
1815 * 41 52 push %r10
1816 * ...
1817 * 41 5a pop %r10
1818 * (more pops)
1819 * 5d pop %rbp
1820 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1821 * c3 retq
1822 *
1823 * There are some variations in the epilogues, like:
1824 *
1825 * 5b pop %rbx
1826 * 41 5a pop %r10
1827 * 41 5c pop %r12
1828 * 41 5d pop %r13
1829 * 41 5e pop %r14
1830 * c9 leaveq
1831 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1832 * c3 retq
1833 *
1834 * and:
1835 *
1836 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1837 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1838 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1839 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1840 * c9 leaveq
1841 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1842 * c3 retq
1843 *
1844 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1845 * restored beforehand:
1846 *
1847 * 41 55 push %r13
1848 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1849 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1850 * ...
1851 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1852 * 41 5d pop %r13
1853 * c3 retq
1854 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001855static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001856 struct stack_op *op)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001857{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001858 struct cfi_reg *cfa = &cfi->cfa;
1859 struct cfi_reg *regs = cfi->regs;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001860
1861 /* stack operations don't make sense with an undefined CFA */
1862 if (cfa->base == CFI_UNDEFINED) {
1863 if (insn->func) {
1864 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1865 return -1;
1866 }
1867 return 0;
1868 }
1869
Julien Thierryee819ae2020-09-04 16:30:27 +01001870 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
1871 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001872 return update_cfi_state_regs(insn, cfi, op);
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001873
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001874 switch (op->dest.type) {
1875
1876 case OP_DEST_REG:
1877 switch (op->src.type) {
1878
1879 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001880 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1881 cfa->base == CFI_SP &&
Julien Thierryfb084fd2020-10-14 08:38:00 +01001882 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001883
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001884 /* mov %rsp, %rbp */
1885 cfa->base = op->dest.reg;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001886 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001887 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001888
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001889 else if (op->src.reg == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001890 op->dest.reg == CFI_BP && cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001891
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001892 /* drap: mov %rsp, %rbp */
1893 regs[CFI_BP].base = CFI_BP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001894 regs[CFI_BP].offset = -cfi->stack_size;
1895 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001896 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001897
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001898 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1899
1900 /*
1901 * mov %rsp, %reg
1902 *
1903 * This is needed for the rare case where GCC
1904 * does:
1905 *
1906 * mov %rsp, %rax
1907 * ...
1908 * mov %rax, %rsp
1909 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001910 cfi->vals[op->dest.reg].base = CFI_CFA;
1911 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001912 }
1913
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001914 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1915 cfa->base == CFI_BP) {
1916
1917 /*
1918 * mov %rbp, %rsp
1919 *
1920 * Restore the original stack pointer (Clang).
1921 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001922 cfi->stack_size = -cfi->regs[CFI_BP].offset;
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001923 }
1924
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001925 else if (op->dest.reg == cfa->base) {
1926
1927 /* mov %reg, %rsp */
1928 if (cfa->base == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001929 cfi->vals[op->src.reg].base == CFI_CFA) {
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001930
1931 /*
1932 * This is needed for the rare case
1933 * where GCC does something dumb like:
1934 *
1935 * lea 0x8(%rsp), %rcx
1936 * ...
1937 * mov %rcx, %rsp
1938 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001939 cfa->offset = -cfi->vals[op->src.reg].offset;
1940 cfi->stack_size = cfa->offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001941
1942 } else {
1943 cfa->base = CFI_UNDEFINED;
1944 cfa->offset = 0;
1945 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001946 }
1947
1948 break;
1949
1950 case OP_SRC_ADD:
1951 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1952
1953 /* add imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001954 cfi->stack_size -= op->src.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001955 if (cfa->base == CFI_SP)
1956 cfa->offset -= op->src.offset;
1957 break;
1958 }
1959
1960 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1961
1962 /* lea disp(%rbp), %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001963 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001964 break;
1965 }
1966
Julien Thierry468af562020-10-14 08:38:01 +01001967 if (!cfi->drap && op->src.reg == CFI_SP &&
1968 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
1969 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset + op->src.offset)) {
1970
1971 /* lea disp(%rsp), %rbp */
1972 cfa->base = CFI_BP;
1973 cfa->offset -= op->src.offset;
1974 cfi->bp_scratch = false;
1975 break;
1976 }
1977
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001978 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001979
1980 /* drap: lea disp(%rsp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001981 cfi->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001982
1983 /*
1984 * lea disp(%rsp), %reg
1985 *
1986 * This is needed for the rare case where GCC
1987 * does something dumb like:
1988 *
1989 * lea 0x8(%rsp), %rcx
1990 * ...
1991 * mov %rcx, %rsp
1992 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001993 cfi->vals[op->dest.reg].base = CFI_CFA;
1994 cfi->vals[op->dest.reg].offset = \
1995 -cfi->stack_size + op->src.offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001996
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001997 break;
1998 }
1999
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002000 if (cfi->drap && op->dest.reg == CFI_SP &&
2001 op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002002
2003 /* drap: lea disp(%drap), %rsp */
2004 cfa->base = CFI_SP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002005 cfa->offset = cfi->stack_size = -op->src.offset;
2006 cfi->drap_reg = CFI_UNDEFINED;
2007 cfi->drap = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002008 break;
2009 }
2010
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002011 if (op->dest.reg == cfi->cfa.base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002012 WARN_FUNC("unsupported stack register modification",
2013 insn->sec, insn->offset);
2014 return -1;
2015 }
2016
2017 break;
2018
2019 case OP_SRC_AND:
2020 if (op->dest.reg != CFI_SP ||
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002021 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2022 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002023 WARN_FUNC("unsupported stack pointer realignment",
2024 insn->sec, insn->offset);
2025 return -1;
2026 }
2027
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002028 if (cfi->drap_reg != CFI_UNDEFINED) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002029 /* drap: and imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002030 cfa->base = cfi->drap_reg;
2031 cfa->offset = cfi->stack_size = 0;
2032 cfi->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002033 }
2034
2035 /*
2036 * Older versions of GCC (4.8ish) realign the stack
2037 * without DRAP, with a frame pointer.
2038 */
2039
2040 break;
2041
2042 case OP_SRC_POP:
Peter Zijlstraea242132019-02-25 12:50:09 +01002043 case OP_SRC_POPF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002044 if (!cfi->drap && op->dest.reg == cfa->base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002045
2046 /* pop %rbp */
2047 cfa->base = CFI_SP;
2048 }
2049
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002050 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2051 op->dest.reg == cfi->drap_reg &&
2052 cfi->drap_offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002053
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002054 /* drap: pop %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002055 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002056 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002057 cfi->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002058
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002059 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002060
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002061 /* pop %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002062 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002063 }
2064
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002065 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002066 if (cfa->base == CFI_SP)
2067 cfa->offset -= 8;
2068
2069 break;
2070
2071 case OP_SRC_REG_INDIRECT:
Julien Thierry201ef5a2020-10-14 08:38:02 +01002072 if (!cfi->drap && op->dest.reg == cfa->base &&
2073 op->dest.reg == CFI_BP) {
2074
2075 /* mov disp(%rsp), %rbp */
2076 cfa->base = CFI_SP;
2077 cfa->offset = cfi->stack_size;
2078 }
2079
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002080 if (cfi->drap && op->src.reg == CFI_BP &&
2081 op->src.offset == cfi->drap_offset) {
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002082
2083 /* drap: mov disp(%rbp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002084 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002085 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002086 cfi->drap_offset = -1;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002087 }
2088
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002089 if (cfi->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002090 op->src.offset == regs[op->dest.reg].offset) {
2091
2092 /* drap: mov disp(%rbp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002093 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002094
2095 } else if (op->src.reg == cfa->base &&
2096 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2097
2098 /* mov disp(%rbp), %reg */
2099 /* mov disp(%rsp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002100 restore_reg(cfi, op->dest.reg);
Julien Thierry201ef5a2020-10-14 08:38:02 +01002101
2102 } else if (op->src.reg == CFI_SP &&
2103 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2104
2105 /* mov disp(%rsp), %reg */
2106 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002107 }
2108
2109 break;
2110
2111 default:
2112 WARN_FUNC("unknown stack-related instruction",
2113 insn->sec, insn->offset);
2114 return -1;
2115 }
2116
2117 break;
2118
2119 case OP_DEST_PUSH:
Peter Zijlstraea242132019-02-25 12:50:09 +01002120 case OP_DEST_PUSHF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002121 cfi->stack_size += 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002122 if (cfa->base == CFI_SP)
2123 cfa->offset += 8;
2124
2125 if (op->src.type != OP_SRC_REG)
2126 break;
2127
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002128 if (cfi->drap) {
2129 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002130
2131 /* drap: push %drap */
2132 cfa->base = CFI_BP_INDIRECT;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002133 cfa->offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002134
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002135 /* save drap so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002136 cfi->drap_offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002137
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002138 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002139
2140 /* drap: push %rbp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002141 cfi->stack_size = 0;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002142
Julien Thierryf4f80392020-09-15 08:53:16 +01002143 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002144
2145 /* drap: push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002146 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002147 }
2148
2149 } else {
2150
2151 /* push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002152 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002153 }
2154
2155 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002156 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002157 cfa->base != CFI_BP)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002158 cfi->bp_scratch = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002159 break;
2160
2161 case OP_DEST_REG_INDIRECT:
2162
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002163 if (cfi->drap) {
2164 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002165
2166 /* drap: mov %drap, disp(%rbp) */
2167 cfa->base = CFI_BP_INDIRECT;
2168 cfa->offset = op->dest.offset;
2169
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002170 /* save drap offset so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002171 cfi->drap_offset = op->dest.offset;
Julien Thierryf4f80392020-09-15 08:53:16 +01002172 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002173
2174 /* drap: mov reg, disp(%rbp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002175 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002176 }
2177
2178 } else if (op->dest.reg == cfa->base) {
2179
2180 /* mov reg, disp(%rbp) */
2181 /* mov reg, disp(%rsp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002182 save_reg(cfi, op->src.reg, CFI_CFA,
2183 op->dest.offset - cfi->cfa.offset);
Julien Thierry201ef5a2020-10-14 08:38:02 +01002184
2185 } else if (op->dest.reg == CFI_SP) {
2186
2187 /* mov reg, disp(%rsp) */
2188 save_reg(cfi, op->src.reg, CFI_CFA,
2189 op->dest.offset - cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002190 }
2191
2192 break;
2193
2194 case OP_DEST_LEAVE:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002195 if ((!cfi->drap && cfa->base != CFI_BP) ||
2196 (cfi->drap && cfa->base != cfi->drap_reg)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002197 WARN_FUNC("leave instruction with modified stack frame",
2198 insn->sec, insn->offset);
2199 return -1;
2200 }
2201
2202 /* leave (mov %rbp, %rsp; pop %rbp) */
2203
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002204 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2205 restore_reg(cfi, CFI_BP);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002206
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002207 if (!cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002208 cfa->base = CFI_SP;
2209 cfa->offset -= 8;
2210 }
2211
2212 break;
2213
2214 case OP_DEST_MEM:
Peter Zijlstraea242132019-02-25 12:50:09 +01002215 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002216 WARN_FUNC("unknown stack-related memory operation",
2217 insn->sec, insn->offset);
2218 return -1;
2219 }
2220
2221 /* pop mem */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002222 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002223 if (cfa->base == CFI_SP)
2224 cfa->offset -= 8;
2225
2226 break;
2227
2228 default:
2229 WARN_FUNC("unknown stack-related instruction",
2230 insn->sec, insn->offset);
2231 return -1;
2232 }
2233
2234 return 0;
2235}
2236
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002237/*
2238 * The stack layouts of alternatives instructions can sometimes diverge when
2239 * they have stack modifications. That's fine as long as the potential stack
2240 * layouts don't conflict at any given potential instruction boundary.
2241 *
2242 * Flatten the CFIs of the different alternative code streams (both original
2243 * and replacement) into a single shared CFI array which can be used to detect
2244 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2245 */
2246static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2247{
2248 struct cfi_state **alt_cfi;
2249 int group_off;
2250
2251 if (!insn->alt_group)
2252 return 0;
2253
2254 alt_cfi = insn->alt_group->cfi;
2255 group_off = insn->offset - insn->alt_group->first_insn->offset;
2256
2257 if (!alt_cfi[group_off]) {
2258 alt_cfi[group_off] = &insn->cfi;
2259 } else {
2260 if (memcmp(alt_cfi[group_off], &insn->cfi, sizeof(struct cfi_state))) {
2261 WARN_FUNC("stack layout conflict in alternatives",
2262 insn->sec, insn->offset);
2263 return -1;
2264 }
2265 }
2266
2267 return 0;
2268}
2269
Julien Thierry65ea47d2020-03-27 15:28:47 +00002270static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2271{
2272 struct stack_op *op;
2273
2274 list_for_each_entry(op, &insn->stack_ops, list) {
Julien Thierry65ea47d2020-03-27 15:28:47 +00002275
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002276 if (update_cfi_state(insn, &state->cfi, op))
2277 return 1;
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002278
Julien Thierry65ea47d2020-03-27 15:28:47 +00002279 if (op->dest.type == OP_DEST_PUSHF) {
2280 if (!state->uaccess_stack) {
2281 state->uaccess_stack = 1;
2282 } else if (state->uaccess_stack >> 31) {
2283 WARN_FUNC("PUSHF stack exhausted",
2284 insn->sec, insn->offset);
2285 return 1;
2286 }
2287 state->uaccess_stack <<= 1;
2288 state->uaccess_stack |= state->uaccess;
2289 }
2290
2291 if (op->src.type == OP_SRC_POPF) {
2292 if (state->uaccess_stack) {
2293 state->uaccess = state->uaccess_stack & 1;
2294 state->uaccess_stack >>= 1;
2295 if (state->uaccess_stack == 1)
2296 state->uaccess_stack = 0;
2297 }
2298 }
2299 }
2300
2301 return 0;
2302}
2303
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002304static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002305{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002306 struct cfi_state *cfi1 = &insn->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002307 int i;
2308
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002309 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2310
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002311 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2312 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002313 cfi1->cfa.base, cfi1->cfa.offset,
2314 cfi2->cfa.base, cfi2->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002315
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002316 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002317 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002318 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002319 sizeof(struct cfi_reg)))
2320 continue;
2321
2322 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2323 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002324 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2325 i, cfi2->regs[i].base, cfi2->regs[i].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002326 break;
2327 }
2328
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002329 } else if (cfi1->type != cfi2->type) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002330
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002331 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2332 insn->sec, insn->offset, cfi1->type, cfi2->type);
2333
2334 } else if (cfi1->drap != cfi2->drap ||
2335 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2336 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2337
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002338 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002339 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002340 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2341 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002342
2343 } else
2344 return true;
2345
2346 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002347}
2348
Peter Zijlstraea242132019-02-25 12:50:09 +01002349static inline bool func_uaccess_safe(struct symbol *func)
2350{
2351 if (func)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002352 return func->uaccess_safe;
Peter Zijlstraea242132019-02-25 12:50:09 +01002353
2354 return false;
2355}
2356
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002357static inline const char *call_dest_name(struct instruction *insn)
Peter Zijlstraea242132019-02-25 12:50:09 +01002358{
2359 if (insn->call_dest)
2360 return insn->call_dest->name;
2361
2362 return "{dynamic}";
2363}
2364
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002365static inline bool noinstr_call_dest(struct symbol *func)
2366{
2367 /*
2368 * We can't deal with indirect function calls at present;
2369 * assume they're instrumented.
2370 */
2371 if (!func)
2372 return false;
2373
2374 /*
2375 * If the symbol is from a noinstr section; we good.
2376 */
2377 if (func->sec->noinstr)
2378 return true;
2379
2380 /*
2381 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2382 * something 'BAD' happened. At the risk of taking the machine down,
2383 * let them proceed to get the message out.
2384 */
2385 if (!strncmp(func->name, "__ubsan_handle_", 15))
2386 return true;
2387
2388 return false;
2389}
2390
Peter Zijlstraea242132019-02-25 12:50:09 +01002391static int validate_call(struct instruction *insn, struct insn_state *state)
2392{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002393 if (state->noinstr && state->instr <= 0 &&
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002394 !noinstr_call_dest(insn->call_dest)) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002395 WARN_FUNC("call to %s() leaves .noinstr.text section",
2396 insn->sec, insn->offset, call_dest_name(insn));
2397 return 1;
2398 }
2399
Peter Zijlstraea242132019-02-25 12:50:09 +01002400 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2401 WARN_FUNC("call to %s() with UACCESS enabled",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002402 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstraea242132019-02-25 12:50:09 +01002403 return 1;
2404 }
2405
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002406 if (state->df) {
2407 WARN_FUNC("call to %s() with DF set",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002408 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002409 return 1;
2410 }
2411
Peter Zijlstraea242132019-02-25 12:50:09 +01002412 return 0;
2413}
2414
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002415static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2416{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002417 if (has_modified_stack_frame(insn, state)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002418 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2419 insn->sec, insn->offset);
2420 return 1;
2421 }
2422
Peter Zijlstraea242132019-02-25 12:50:09 +01002423 return validate_call(insn, state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002424}
2425
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002426static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2427{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002428 if (state->noinstr && state->instr > 0) {
2429 WARN_FUNC("return with instrumentation enabled",
2430 insn->sec, insn->offset);
2431 return 1;
2432 }
2433
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002434 if (state->uaccess && !func_uaccess_safe(func)) {
2435 WARN_FUNC("return with UACCESS enabled",
2436 insn->sec, insn->offset);
2437 return 1;
2438 }
2439
2440 if (!state->uaccess && func_uaccess_safe(func)) {
2441 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2442 insn->sec, insn->offset);
2443 return 1;
2444 }
2445
2446 if (state->df) {
2447 WARN_FUNC("return with DF set",
2448 insn->sec, insn->offset);
2449 return 1;
2450 }
2451
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002452 if (func && has_modified_stack_frame(insn, state)) {
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002453 WARN_FUNC("return with modified stack frame",
2454 insn->sec, insn->offset);
2455 return 1;
2456 }
2457
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002458 if (state->cfi.bp_scratch) {
Josh Poimboeufb2966952020-04-01 13:23:29 -05002459 WARN_FUNC("BP used as a scratch register",
2460 insn->sec, insn->offset);
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002461 return 1;
2462 }
2463
2464 return 0;
2465}
2466
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002467static struct instruction *next_insn_to_validate(struct objtool_file *file,
2468 struct instruction *insn)
Peter Zijlstra7117f162020-04-28 19:37:01 +02002469{
Josh Poimboeufb23cc712020-12-18 14:19:32 -06002470 struct alt_group *alt_group = insn->alt_group;
Peter Zijlstra7117f162020-04-28 19:37:01 +02002471
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002472 /*
2473 * Simulate the fact that alternatives are patched in-place. When the
2474 * end of a replacement alt_group is reached, redirect objtool flow to
2475 * the end of the original alt_group.
2476 */
2477 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2478 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2479
2480 return next_insn_same_sec(file, insn);
Peter Zijlstra7117f162020-04-28 19:37:01 +02002481}
2482
2483/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002484 * Follow the branch starting at the given instruction, and recursively follow
2485 * any other branches (jumps). Meanwhile, track the frame pointer state at
2486 * each instruction and validate all the rules described in
2487 * tools/objtool/Documentation/stack-validation.txt.
2488 */
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002489static int validate_branch(struct objtool_file *file, struct symbol *func,
Peter Zijlstrab7460462020-04-02 10:15:51 +02002490 struct instruction *insn, struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002491{
2492 struct alternative *alt;
Peter Zijlstrab7460462020-04-02 10:15:51 +02002493 struct instruction *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002494 struct section *sec;
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002495 u8 visited;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002496 int ret;
2497
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002498 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002499
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002500 while (1) {
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002501 next_insn = next_insn_to_validate(file, insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002502
Josh Poimboeuf13810432018-05-09 22:39:15 -05002503 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05002504 WARN("%s() falls through to next function %s()",
2505 func->name, insn->func->name);
2506 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002507 }
2508
Josh Poimboeuf48550222017-07-07 09:19:42 -05002509 if (func && insn->ignore) {
2510 WARN_FUNC("BUG: why am I validating an ignored function?",
2511 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05002512 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05002513 }
2514
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002515 visited = 1 << state.uaccess;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002516 if (insn->visited) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002517 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002518 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002519
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002520 if (insn->visited & visited)
Peter Zijlstraea242132019-02-25 12:50:09 +01002521 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002522 }
2523
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002524 if (state.noinstr)
2525 state.instr += insn->instr;
2526
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002527 if (insn->hint)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002528 state.cfi = insn->cfi;
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002529 else
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002530 insn->cfi = state.cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002531
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002532 insn->visited |= visited;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002533
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002534 if (propagate_alt_cfi(file, insn))
2535 return 1;
2536
Peter Zijlstra7117f162020-04-28 19:37:01 +02002537 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002538 bool skip_orig = false;
2539
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002540 list_for_each_entry(alt, &insn->alts, list) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002541 if (alt->skip_orig)
2542 skip_orig = true;
2543
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002544 ret = validate_branch(file, func, alt->insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002545 if (ret) {
2546 if (backtrace)
2547 BT_FUNC("(alt)", insn);
2548 return ret;
2549 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002550 }
Peter Zijlstra764eef42019-03-01 11:19:03 +01002551
2552 if (skip_orig)
2553 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002554 }
2555
Peter Zijlstra60041bc2020-04-24 16:16:41 +02002556 if (handle_insn_ops(insn, &state))
2557 return 1;
2558
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002559 switch (insn->type) {
2560
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002561 case INSN_RETURN:
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002562 return validate_return(func, insn, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002563
2564 case INSN_CALL:
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002565 case INSN_CALL_DYNAMIC:
Peter Zijlstraea242132019-02-25 12:50:09 +01002566 ret = validate_call(insn, &state);
2567 if (ret)
2568 return ret;
2569
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002570 if (!no_fp && func && !is_fentry_call(insn) &&
2571 !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002572 WARN_FUNC("call without frame pointer save/setup",
2573 sec, insn->offset);
2574 return 1;
2575 }
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002576
2577 if (dead_end_function(file, insn->call_dest))
2578 return 0;
2579
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002580 if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) {
2581 list_add_tail(&insn->static_call_node,
2582 &file->static_call_list);
2583 }
2584
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002585 break;
2586
2587 case INSN_JUMP_CONDITIONAL:
2588 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002589 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002590 ret = validate_sibling_call(insn, &state);
2591 if (ret)
2592 return ret;
2593
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002594 } else if (insn->jump_dest) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002595 ret = validate_branch(file, func,
2596 insn->jump_dest, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002597 if (ret) {
2598 if (backtrace)
2599 BT_FUNC("(branch)", insn);
2600 return ret;
2601 }
Josh Poimboeuf48550222017-07-07 09:19:42 -05002602 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002603
2604 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2605 return 0;
2606
2607 break;
2608
2609 case INSN_JUMP_DYNAMIC:
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002610 case INSN_JUMP_DYNAMIC_CONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002611 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002612 ret = validate_sibling_call(insn, &state);
2613 if (ret)
2614 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002615 }
2616
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002617 if (insn->type == INSN_JUMP_DYNAMIC)
2618 return 0;
2619
2620 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002621
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002622 case INSN_CONTEXT_SWITCH:
2623 if (func && (!next_insn || !next_insn->hint)) {
2624 WARN_FUNC("unsupported instruction in callable function",
2625 sec, insn->offset);
2626 return 1;
2627 }
2628 return 0;
2629
Peter Zijlstraea242132019-02-25 12:50:09 +01002630 case INSN_STAC:
2631 if (state.uaccess) {
2632 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2633 return 1;
2634 }
2635
2636 state.uaccess = true;
2637 break;
2638
2639 case INSN_CLAC:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002640 if (!state.uaccess && func) {
Peter Zijlstraea242132019-02-25 12:50:09 +01002641 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2642 return 1;
2643 }
2644
2645 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2646 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2647 return 1;
2648 }
2649
2650 state.uaccess = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002651 break;
2652
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002653 case INSN_STD:
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06002654 if (state.df) {
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002655 WARN_FUNC("recursive STD", sec, insn->offset);
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06002656 return 1;
2657 }
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002658
2659 state.df = true;
2660 break;
2661
2662 case INSN_CLD:
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06002663 if (!state.df && func) {
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002664 WARN_FUNC("redundant CLD", sec, insn->offset);
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06002665 return 1;
2666 }
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002667
2668 state.df = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002669 break;
2670
2671 default:
2672 break;
2673 }
2674
2675 if (insn->dead_end)
2676 return 0;
2677
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002678 if (!next_insn) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002679 if (state.cfi.cfa.base == CFI_UNDEFINED)
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002680 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002681 WARN("%s: unexpected end of section", sec->name);
2682 return 1;
2683 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002684
2685 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002686 }
2687
2688 return 0;
2689}
2690
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002691static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002692{
2693 struct instruction *insn;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002694 struct insn_state state;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002695 int ret, warnings = 0;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002696
2697 if (!file->hints)
2698 return 0;
2699
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002700 init_insn_state(&state, sec);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002701
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002702 if (sec) {
2703 insn = find_insn(file, sec, 0);
2704 if (!insn)
2705 return 0;
2706 } else {
2707 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2708 }
2709
2710 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002711 if (insn->hint && !insn->visited) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002712 ret = validate_branch(file, insn->func, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002713 if (ret && backtrace)
2714 BT_FUNC("<=== (hint)", insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002715 warnings += ret;
2716 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002717
2718 insn = list_next_entry(insn, list);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002719 }
2720
2721 return warnings;
2722}
2723
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002724static int validate_retpoline(struct objtool_file *file)
2725{
2726 struct instruction *insn;
2727 int warnings = 0;
2728
2729 for_each_insn(file, insn) {
2730 if (insn->type != INSN_JUMP_DYNAMIC &&
2731 insn->type != INSN_CALL_DYNAMIC)
2732 continue;
2733
2734 if (insn->retpoline_safe)
2735 continue;
2736
Peter Zijlstraca41b972018-01-31 10:18:28 +01002737 /*
2738 * .init.text code is ran before userspace and thus doesn't
2739 * strictly need retpolines, except for modules which are
2740 * loaded late, they very much do need retpoline in their
2741 * .init.text
2742 */
2743 if (!strcmp(insn->sec->name, ".init.text") && !module)
2744 continue;
2745
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002746 WARN_FUNC("indirect %s found in RETPOLINE build",
2747 insn->sec, insn->offset,
2748 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2749
2750 warnings++;
2751 }
2752
2753 return warnings;
2754}
2755
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002756static bool is_kasan_insn(struct instruction *insn)
2757{
2758 return (insn->type == INSN_CALL &&
2759 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2760}
2761
2762static bool is_ubsan_insn(struct instruction *insn)
2763{
2764 return (insn->type == INSN_CALL &&
2765 !strcmp(insn->call_dest->name,
2766 "__ubsan_handle_builtin_unreachable"));
2767}
2768
Ilie Halip14db1f02020-09-19 09:41:18 +03002769static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002770{
2771 int i;
Ilie Halip14db1f02020-09-19 09:41:18 +03002772 struct instruction *prev_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002773
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002774 if (insn->ignore || insn->type == INSN_NOP)
2775 return true;
2776
2777 /*
2778 * Ignore any unused exceptions. This can happen when a whitelisted
2779 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002780 *
2781 * Also ignore alternative replacement instructions. This can happen
2782 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002783 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002784 if (!strcmp(insn->sec->name, ".fixup") ||
2785 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2786 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002787 return true;
2788
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002789 if (!insn->func)
2790 return false;
2791
2792 /*
2793 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2794 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2795 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2796 * (or occasionally a JMP to UD2).
Ilie Halip14db1f02020-09-19 09:41:18 +03002797 *
2798 * It may also insert a UD2 after calling a __noreturn function.
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002799 */
Ilie Halip14db1f02020-09-19 09:41:18 +03002800 prev_insn = list_prev_entry(insn, list);
2801 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002802 (insn->type == INSN_BUG ||
2803 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2804 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2805 return true;
2806
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002807 /*
2808 * Check if this (or a subsequent) instruction is related to
2809 * CONFIG_UBSAN or CONFIG_KASAN.
2810 *
2811 * End the search at 5 instructions to avoid going into the weeds.
2812 */
2813 for (i = 0; i < 5; i++) {
2814
2815 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2816 return true;
2817
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002818 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2819 if (insn->jump_dest &&
2820 insn->jump_dest->func == insn->func) {
2821 insn = insn->jump_dest;
2822 continue;
2823 }
2824
2825 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002826 }
2827
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002828 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002829 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002830
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002831 insn = list_next_entry(insn, list);
2832 }
2833
2834 return false;
2835}
2836
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002837static int validate_symbol(struct objtool_file *file, struct section *sec,
2838 struct symbol *sym, struct insn_state *state)
2839{
2840 struct instruction *insn;
2841 int ret;
2842
2843 if (!sym->len) {
2844 WARN("%s() is missing an ELF size annotation", sym->name);
2845 return 1;
2846 }
2847
2848 if (sym->pfunc != sym || sym->alias != sym)
2849 return 0;
2850
2851 insn = find_insn(file, sec, sym->offset);
2852 if (!insn || insn->ignore || insn->visited)
2853 return 0;
2854
2855 state->uaccess = sym->uaccess_safe;
2856
2857 ret = validate_branch(file, insn->func, insn, *state);
2858 if (ret && backtrace)
2859 BT_FUNC("<=== (sym)", insn);
2860 return ret;
2861}
2862
Peter Zijlstra350994b2020-03-23 20:57:13 +01002863static int validate_section(struct objtool_file *file, struct section *sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002864{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002865 struct insn_state state;
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002866 struct symbol *func;
2867 int warnings = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002868
Peter Zijlstra350994b2020-03-23 20:57:13 +01002869 list_for_each_entry(func, &sec->symbol_list, list) {
2870 if (func->type != STT_FUNC)
2871 continue;
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002872
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002873 init_insn_state(&state, sec);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002874 state.cfi.cfa = initial_func_cfi.cfa;
2875 memcpy(&state.cfi.regs, &initial_func_cfi.regs,
Julien Thierry0699e552020-03-27 15:28:40 +00002876 CFI_NUM_REGS * sizeof(struct cfi_reg));
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002877 state.cfi.stack_size = initial_func_cfi.cfa.offset;
Julien Thierry0699e552020-03-27 15:28:40 +00002878
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002879 warnings += validate_symbol(file, sec, func, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002880 }
2881
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002882 return warnings;
2883}
2884
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002885static int validate_vmlinux_functions(struct objtool_file *file)
2886{
2887 struct section *sec;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002888 int warnings = 0;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002889
2890 sec = find_section_by_name(file->elf, ".noinstr.text");
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002891 if (sec) {
2892 warnings += validate_section(file, sec);
2893 warnings += validate_unwind_hints(file, sec);
2894 }
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002895
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002896 sec = find_section_by_name(file->elf, ".entry.text");
2897 if (sec) {
2898 warnings += validate_section(file, sec);
2899 warnings += validate_unwind_hints(file, sec);
2900 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002901
2902 return warnings;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002903}
2904
Peter Zijlstra350994b2020-03-23 20:57:13 +01002905static int validate_functions(struct objtool_file *file)
2906{
2907 struct section *sec;
2908 int warnings = 0;
2909
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002910 for_each_sec(file, sec) {
2911 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2912 continue;
2913
Peter Zijlstra350994b2020-03-23 20:57:13 +01002914 warnings += validate_section(file, sec);
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002915 }
Peter Zijlstra350994b2020-03-23 20:57:13 +01002916
2917 return warnings;
2918}
2919
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002920static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002921{
2922 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002923
2924 if (file->ignore_unreachables)
2925 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002926
2927 for_each_insn(file, insn) {
Ilie Halip14db1f02020-09-19 09:41:18 +03002928 if (insn->visited || ignore_unreachable_insn(file, insn))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002929 continue;
2930
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002931 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2932 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002933 }
2934
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002935 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002936}
2937
Julien Thierryd44becb2020-08-25 13:47:40 +01002938int check(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002939{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002940 int ret, warnings = 0;
2941
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002942 arch_initial_func_cfi_state(&initial_func_cfi);
2943
Julien Thierry6545eb02020-08-25 13:47:39 +01002944 ret = decode_sections(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002945 if (ret < 0)
2946 goto out;
2947 warnings += ret;
2948
Julien Thierry6545eb02020-08-25 13:47:39 +01002949 if (list_empty(&file->insn_list))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002950 goto out;
2951
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002952 if (vmlinux && !validate_dup) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002953 ret = validate_vmlinux_functions(file);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002954 if (ret < 0)
2955 goto out;
2956
2957 warnings += ret;
2958 goto out;
2959 }
2960
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002961 if (retpoline) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002962 ret = validate_retpoline(file);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002963 if (ret < 0)
2964 return ret;
2965 warnings += ret;
2966 }
2967
Julien Thierry6545eb02020-08-25 13:47:39 +01002968 ret = validate_functions(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002969 if (ret < 0)
2970 goto out;
2971 warnings += ret;
2972
Julien Thierry6545eb02020-08-25 13:47:39 +01002973 ret = validate_unwind_hints(file, NULL);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002974 if (ret < 0)
2975 goto out;
2976 warnings += ret;
2977
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002978 if (!warnings) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002979 ret = validate_reachable_instructions(file);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002980 if (ret < 0)
2981 goto out;
2982 warnings += ret;
2983 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002984
Julien Thierry6545eb02020-08-25 13:47:39 +01002985 ret = create_static_call_sections(file);
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002986 if (ret < 0)
2987 goto out;
2988 warnings += ret;
2989
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002990out:
Josh Poimboeuf644592d2020-02-10 12:32:38 -06002991 if (ret < 0) {
2992 /*
2993 * Fatal error. The binary is corrupt or otherwise broken in
2994 * some way, or objtool itself is broken. Fail the kernel
2995 * build.
2996 */
2997 return ret;
2998 }
2999
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003000 return 0;
3001}