blob: 7c33ec67c4a9534497f407d43c83998a98a7eb84 [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>
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02008#include <sys/mman.h>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05009
Vasily Gorbik77860322020-11-13 00:03:32 +010010#include <arch/elf.h>
11#include <objtool/builtin.h>
12#include <objtool/cfi.h>
13#include <objtool/arch.h>
14#include <objtool/check.h>
15#include <objtool/special.h>
16#include <objtool/warn.h>
17#include <objtool/endianness.h>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050018
Julien Thierryee819ae2020-09-04 16:30:27 +010019#include <linux/objtool.h>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050020#include <linux/hashtable.h>
21#include <linux/kernel.h>
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +020022#include <linux/static_call_types.h>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050023
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050024struct alternative {
25 struct list_head list;
26 struct instruction *insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +010027 bool skip_orig;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050028};
29
Peter Zijlstra8b946cc2021-06-24 11:41:01 +020030static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache;
31
32static struct cfi_init_state initial_func_cfi;
33static struct cfi_state init_cfi;
34static struct cfi_state func_cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050035
Josh Poimboeuf627fce12017-07-11 10:33:42 -050036struct instruction *find_insn(struct objtool_file *file,
37 struct section *sec, unsigned long offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050038{
39 struct instruction *insn;
40
Peter Zijlstra87ecb582020-03-16 15:47:27 +010041 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050042 if (insn->sec == sec && insn->offset == offset)
43 return insn;
Peter Zijlstra87ecb582020-03-16 15:47:27 +010044 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050045
46 return NULL;
47}
48
49static struct instruction *next_insn_same_sec(struct objtool_file *file,
50 struct instruction *insn)
51{
52 struct instruction *next = list_next_entry(insn, list);
53
Josh Poimboeufbaa41462017-06-28 10:11:07 -050054 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050055 return NULL;
56
57 return next;
58}
59
Josh Poimboeuf13810432018-05-09 22:39:15 -050060static struct instruction *next_insn_same_func(struct objtool_file *file,
61 struct instruction *insn)
62{
63 struct instruction *next = list_next_entry(insn, list);
64 struct symbol *func = insn->func;
65
66 if (!func)
67 return NULL;
68
69 if (&next->list != &file->insn_list && next->func == func)
70 return next;
71
72 /* Check if we're already in the subfunction: */
73 if (func == func->cfunc)
74 return NULL;
75
76 /* Move to the subfunction: */
77 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
78}
79
Josh Poimboeuf1119d262020-04-28 16:45:16 -050080static struct instruction *prev_insn_same_sym(struct objtool_file *file,
81 struct instruction *insn)
82{
83 struct instruction *prev = list_prev_entry(insn, list);
84
85 if (&prev->list != &file->insn_list && prev->func == insn->func)
86 return prev;
87
88 return NULL;
89}
90
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +010091#define func_for_each_insn(file, func, insn) \
Josh Poimboeuf13810432018-05-09 22:39:15 -050092 for (insn = find_insn(file, func->sec, func->offset); \
93 insn; \
94 insn = next_insn_same_func(file, insn))
95
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +010096#define sym_for_each_insn(file, sym, insn) \
97 for (insn = find_insn(file, sym->sec, sym->offset); \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050098 insn && &insn->list != &file->insn_list && \
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +010099 insn->sec == sym->sec && \
100 insn->offset < sym->offset + sym->len; \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500101 insn = list_next_entry(insn, list))
102
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100103#define sym_for_each_insn_continue_reverse(file, sym, insn) \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500104 for (insn = list_prev_entry(insn, list); \
105 &insn->list != &file->insn_list && \
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100106 insn->sec == sym->sec && insn->offset >= sym->offset; \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500107 insn = list_prev_entry(insn, list))
108
109#define sec_for_each_insn_from(file, insn) \
110 for (; insn; insn = next_insn_same_sec(file, insn))
111
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500112#define sec_for_each_insn_continue(file, insn) \
113 for (insn = next_insn_same_sec(file, insn); insn; \
114 insn = next_insn_same_sec(file, insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500115
Josh Poimboeuf99033462021-02-24 10:29:14 -0600116static bool is_jump_table_jump(struct instruction *insn)
117{
118 struct alt_group *alt_group = insn->alt_group;
119
120 if (insn->jump_table)
121 return true;
122
123 /* Retpoline alternative for a jump table? */
124 return alt_group && alt_group->orig_group &&
125 alt_group->orig_group->first_insn->jump_table;
126}
127
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500128static bool is_sibling_call(struct instruction *insn)
129{
Josh Poimboeufecf11ba2021-01-21 15:29:22 -0600130 /*
131 * Assume only ELF functions can make sibling calls. This ensures
132 * sibling call detection consistency between vmlinux.o and individual
133 * objects.
134 */
135 if (!insn->func)
136 return false;
137
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500138 /* An indirect jump is either a sibling call or a jump to a table. */
139 if (insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeuf99033462021-02-24 10:29:14 -0600140 return !is_jump_table_jump(insn);
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500141
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500142 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
Josh Poimboeufecf11ba2021-01-21 15:29:22 -0600143 return (is_static_jump(insn) && insn->call_dest);
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500144}
145
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500146/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500147 * This checks to see if the given function is a "noreturn" function.
148 *
149 * For global functions which are outside the scope of this object file, we
150 * have to keep a manual list of them.
151 *
152 * For local functions, we have to detect them manually by simply looking for
153 * the lack of a return instruction.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500154 */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500155static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
156 int recursion)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500157{
158 int i;
159 struct instruction *insn;
160 bool empty = true;
161
162 /*
163 * Unfortunately these have to be hard coded because the noreturn
164 * attribute isn't provided in ELF data.
165 */
166 static const char * const global_noreturns[] = {
167 "__stack_chk_fail",
168 "panic",
169 "do_exit",
170 "do_task_dead",
Eric W. Biedermanbbda86e2021-11-22 10:27:36 -0600171 "kthread_exit",
Eric W. Biederman0e254982021-06-28 14:52:01 -0500172 "make_task_dead",
Eric W. Biedermanca3574b2021-12-03 11:00:19 -0600173 "__module_put_and_kthread_exit",
Eric W. Biedermancead1852021-11-22 11:15:19 -0600174 "kthread_complete_and_exit",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500175 "__reiserfs_panic",
176 "lbug_with_loc",
177 "fortify_panic",
Kees Cookb394d462018-01-10 14:22:38 -0800178 "usercopy_abort",
Josh Poimboeuf684fb242018-06-19 10:47:50 -0500179 "machine_real_restart",
Eric W. Biederman1fb466d2021-12-15 11:24:50 -0600180 "rewind_stack_and_make_dead",
Brendan Higgins33adf802019-09-23 02:02:38 -0700181 "kunit_try_catch_throw",
Josh Poimboeufc26acfb2021-01-21 15:29:25 -0600182 "xen_start_kernel",
Peter Zijlstra9af9dcf2021-06-24 11:41:00 +0200183 "cpu_bringup_and_idle",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500184 };
185
Josh Poimboeufc9bab222019-07-17 20:36:51 -0500186 if (!func)
187 return false;
188
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500189 if (func->bind == STB_WEAK)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500190 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500191
192 if (func->bind == STB_GLOBAL)
193 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
194 if (!strcmp(func->name, global_noreturns[i]))
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500195 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500196
Josh Poimboeuf13810432018-05-09 22:39:15 -0500197 if (!func->len)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500198 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500199
Josh Poimboeuf13810432018-05-09 22:39:15 -0500200 insn = find_insn(file, func->sec, func->offset);
201 if (!insn->func)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500202 return false;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500203
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100204 func_for_each_insn(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500205 empty = false;
206
207 if (insn->type == INSN_RETURN)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500208 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500209 }
210
211 if (empty)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500212 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500213
214 /*
215 * A function can have a sibling call instead of a return. In that
216 * case, the function's dead-end status depends on whether the target
217 * of the sibling call returns.
218 */
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100219 func_for_each_insn(file, func, insn) {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500220 if (is_sibling_call(insn)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500221 struct instruction *dest = insn->jump_dest;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500222
223 if (!dest)
224 /* sibling call to another file */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500225 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500226
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500227 /* local sibling call */
228 if (recursion == 5) {
229 /*
230 * Infinite recursion: two functions have
231 * sibling calls to each other. This is a very
232 * rare case. It means they aren't dead ends.
233 */
234 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500235 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500236
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500237 return __dead_end_function(file, dest->func, recursion+1);
238 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500239 }
240
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500241 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500242}
243
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500244static bool dead_end_function(struct objtool_file *file, struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500245{
246 return __dead_end_function(file, func, 0);
247}
248
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100249static void init_cfi_state(struct cfi_state *cfi)
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500250{
251 int i;
252
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500253 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100254 cfi->regs[i].base = CFI_UNDEFINED;
255 cfi->vals[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500256 }
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100257 cfi->cfa.base = CFI_UNDEFINED;
258 cfi->drap_reg = CFI_UNDEFINED;
259 cfi->drap_offset = -1;
260}
261
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100262static void init_insn_state(struct insn_state *state, struct section *sec)
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100263{
264 memset(state, 0, sizeof(*state));
265 init_cfi_state(&state->cfi);
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100266
267 /*
268 * We need the full vmlinux for noinstr validation, otherwise we can
269 * not correctly determine insn->call_dest->sec (external symbols do
270 * not have a section).
271 */
Sami Tolvanen41425eb2020-09-30 14:36:59 -0700272 if (vmlinux && noinstr && sec)
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100273 state->noinstr = sec->noinstr;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500274}
275
Peter Zijlstra8b946cc2021-06-24 11:41:01 +0200276static struct cfi_state *cfi_alloc(void)
277{
278 struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
279 if (!cfi) {
280 WARN("calloc failed");
281 exit(1);
282 }
283 nr_cfi++;
284 return cfi;
285}
286
287static int cfi_bits;
288static struct hlist_head *cfi_hash;
289
290static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
291{
292 return memcmp((void *)cfi1 + sizeof(cfi1->hash),
293 (void *)cfi2 + sizeof(cfi2->hash),
294 sizeof(struct cfi_state) - sizeof(struct hlist_node));
295}
296
297static inline u32 cfi_key(struct cfi_state *cfi)
298{
299 return jhash((void *)cfi + sizeof(cfi->hash),
300 sizeof(*cfi) - sizeof(cfi->hash), 0);
301}
302
303static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
304{
305 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
306 struct cfi_state *obj;
307
308 hlist_for_each_entry(obj, head, hash) {
309 if (!cficmp(cfi, obj)) {
310 nr_cfi_cache++;
311 return obj;
312 }
313 }
314
315 obj = cfi_alloc();
316 *obj = *cfi;
317 hlist_add_head(&obj->hash, head);
318
319 return obj;
320}
321
322static void cfi_hash_add(struct cfi_state *cfi)
323{
324 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
325
326 hlist_add_head(&cfi->hash, head);
327}
328
329static void *cfi_hash_alloc(unsigned long size)
330{
331 cfi_bits = max(10, ilog2(size));
332 cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits,
333 PROT_READ|PROT_WRITE,
334 MAP_PRIVATE|MAP_ANON, -1, 0);
335 if (cfi_hash == (void *)-1L) {
336 WARN("mmap fail cfi_hash");
337 cfi_hash = NULL;
338 } else if (stats) {
339 printf("cfi_bits: %d\n", cfi_bits);
340 }
341
342 return cfi_hash;
343}
344
345static unsigned long nr_insns;
346static unsigned long nr_insns_visited;
347
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500348/*
349 * Call the arch-specific instruction decoder for all the instructions and add
350 * them to the global instruction list.
351 */
352static int decode_instructions(struct objtool_file *file)
353{
354 struct section *sec;
355 struct symbol *func;
356 unsigned long offset;
357 struct instruction *insn;
358 int ret;
359
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500360 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500361
362 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
363 continue;
364
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500365 if (strcmp(sec->name, ".altinstr_replacement") &&
366 strcmp(sec->name, ".altinstr_aux") &&
367 strncmp(sec->name, ".discard.", 9))
368 sec->text = true;
369
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +0100370 if (!strcmp(sec->name, ".noinstr.text") ||
371 !strcmp(sec->name, ".entry.text"))
Peter Zijlstrac4a33932020-03-10 18:57:41 +0100372 sec->noinstr = true;
373
Joe Lawrencefe255fe2021-08-22 18:50:37 -0400374 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500375 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500376 if (!insn) {
377 WARN("malloc failed");
378 return -1;
379 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500380 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500381 INIT_LIST_HEAD(&insn->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +0000382 INIT_LIST_HEAD(&insn->stack_ops);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500383
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500384 insn->sec = sec;
385 insn->offset = offset;
386
Peter Zijlstradb2b0c52021-06-24 11:41:23 +0200387 ret = arch_decode_instruction(file, sec, offset,
Joe Lawrencefe255fe2021-08-22 18:50:37 -0400388 sec->sh.sh_size - offset,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500389 &insn->len, &insn->type,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500390 &insn->immediate,
Julien Thierry65ea47d2020-03-27 15:28:47 +0000391 &insn->stack_ops);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500392 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500393 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500394
Peter Zijlstra87ecb582020-03-16 15:47:27 +0100395 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500396 list_add_tail(&insn->list, &file->insn_list);
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100397 nr_insns++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500398 }
399
400 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500401 if (func->type != STT_FUNC || func->alias != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500402 continue;
403
404 if (!find_insn(file, sec, func->offset)) {
405 WARN("%s(): can't find starting instruction",
406 func->name);
407 return -1;
408 }
409
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100410 sym_for_each_insn(file, func, insn)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500411 insn->func = func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500412 }
413 }
414
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100415 if (stats)
416 printf("nr_insns: %lu\n", nr_insns);
417
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500418 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500419
420err:
421 free(insn);
422 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500423}
424
Peter Zijlstradb2b0c52021-06-24 11:41:23 +0200425/*
426 * Read the pv_ops[] .data table to find the static initialized values.
427 */
428static int add_pv_ops(struct objtool_file *file, const char *symname)
429{
430 struct symbol *sym, *func;
431 unsigned long off, end;
432 struct reloc *rel;
433 int idx;
434
435 sym = find_symbol_by_name(file->elf, symname);
436 if (!sym)
437 return 0;
438
439 off = sym->offset;
440 end = off + sym->len;
441 for (;;) {
442 rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off);
443 if (!rel)
444 break;
445
446 func = rel->sym;
447 if (func->type == STT_SECTION)
448 func = find_symbol_by_offset(rel->sym->sec, rel->addend);
449
450 idx = (rel->offset - sym->offset) / sizeof(unsigned long);
451
452 objtool_pv_add(file, idx, func);
453
454 off = rel->offset + 1;
455 if (off > end)
456 break;
457 }
458
459 return 0;
460}
461
462/*
463 * Allocate and initialize file->pv_ops[].
464 */
465static int init_pv_ops(struct objtool_file *file)
466{
467 static const char *pv_ops_tables[] = {
468 "pv_ops",
469 "xen_cpu_ops",
470 "xen_irq_ops",
471 "xen_mmu_ops",
472 NULL,
473 };
474 const char *pv_ops;
475 struct symbol *sym;
476 int idx, nr;
477
478 if (!noinstr)
479 return 0;
480
481 file->pv_ops = NULL;
482
483 sym = find_symbol_by_name(file->elf, "pv_ops");
484 if (!sym)
485 return 0;
486
487 nr = sym->len / sizeof(unsigned long);
488 file->pv_ops = calloc(sizeof(struct pv_state), nr);
489 if (!file->pv_ops)
490 return -1;
491
492 for (idx = 0; idx < nr; idx++)
493 INIT_LIST_HEAD(&file->pv_ops[idx].targets);
494
495 for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++)
496 add_pv_ops(file, pv_ops);
497
498 return 0;
499}
500
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700501static struct instruction *find_last_insn(struct objtool_file *file,
502 struct section *sec)
503{
504 struct instruction *insn = NULL;
505 unsigned int offset;
Joe Lawrencefe255fe2021-08-22 18:50:37 -0400506 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700507
Joe Lawrencefe255fe2021-08-22 18:50:37 -0400508 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700509 insn = find_insn(file, sec, offset);
510
511 return insn;
512}
513
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500514/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500515 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500516 */
517static int add_dead_ends(struct objtool_file *file)
518{
519 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700520 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500521 struct instruction *insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500522
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500523 /*
524 * By default, "ud2" is a dead end unless otherwise annotated, because
525 * GCC 7 inserts it for certain divide-by-zero cases.
526 */
527 for_each_insn(file, insn)
528 if (insn->type == INSN_BUG)
529 insn->dead_end = true;
530
531 /*
532 * Check for manually annotated dead ends.
533 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500534 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
535 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500536 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500537
Matt Helsleyf1974222020-05-29 14:01:13 -0700538 list_for_each_entry(reloc, &sec->reloc_list, list) {
539 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500540 WARN("unexpected relocation symbol type in %s", sec->name);
541 return -1;
542 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700543 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500544 if (insn)
545 insn = list_prev_entry(insn, list);
Joe Lawrencefe255fe2021-08-22 18:50:37 -0400546 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
Matt Helsleyf1974222020-05-29 14:01:13 -0700547 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700548 if (!insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500549 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700550 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500551 return -1;
552 }
553 } else {
554 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700555 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500556 return -1;
557 }
558
559 insn->dead_end = true;
560 }
561
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500562reachable:
563 /*
564 * These manually annotated reachable checks are needed for GCC 4.4,
565 * where the Linux unreachable() macro isn't supported. In that case
566 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
567 * not a dead end.
568 */
569 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
570 if (!sec)
571 return 0;
572
Matt Helsleyf1974222020-05-29 14:01:13 -0700573 list_for_each_entry(reloc, &sec->reloc_list, list) {
574 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500575 WARN("unexpected relocation symbol type in %s", sec->name);
576 return -1;
577 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700578 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500579 if (insn)
580 insn = list_prev_entry(insn, list);
Joe Lawrencefe255fe2021-08-22 18:50:37 -0400581 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
Matt Helsleyf1974222020-05-29 14:01:13 -0700582 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700583 if (!insn) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500584 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700585 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500586 return -1;
587 }
588 } else {
589 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700590 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500591 return -1;
592 }
593
594 insn->dead_end = false;
595 }
596
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500597 return 0;
598}
599
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200600static int create_static_call_sections(struct objtool_file *file)
601{
Peter Zijlstraef47cc02021-03-26 16:12:07 +0100602 struct section *sec;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200603 struct static_call_site *site;
604 struct instruction *insn;
605 struct symbol *key_sym;
606 char *key_name, *tmp;
607 int idx;
608
609 sec = find_section_by_name(file->elf, ".static_call_sites");
610 if (sec) {
611 INIT_LIST_HEAD(&file->static_call_list);
612 WARN("file already has .static_call_sites section, skipping");
613 return 0;
614 }
615
616 if (list_empty(&file->static_call_list))
617 return 0;
618
619 idx = 0;
Peter Zijlstra43d54302021-03-26 16:12:12 +0100620 list_for_each_entry(insn, &file->static_call_list, call_node)
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200621 idx++;
622
623 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
624 sizeof(struct static_call_site), idx);
625 if (!sec)
626 return -1;
627
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200628 idx = 0;
Peter Zijlstra43d54302021-03-26 16:12:12 +0100629 list_for_each_entry(insn, &file->static_call_list, call_node) {
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200630
631 site = (struct static_call_site *)sec->data->d_buf + idx;
632 memset(site, 0, sizeof(struct static_call_site));
633
634 /* populate reloc for 'addr' */
Peter Zijlstraef47cc02021-03-26 16:12:07 +0100635 if (elf_add_reloc_to_insn(file->elf, sec,
636 idx * sizeof(struct static_call_site),
637 R_X86_64_PC32,
638 insn->sec, insn->offset))
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200639 return -1;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200640
641 /* find key symbol */
642 key_name = strdup(insn->call_dest->name);
643 if (!key_name) {
644 perror("strdup");
645 return -1;
646 }
647 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
648 STATIC_CALL_TRAMP_PREFIX_LEN)) {
649 WARN("static_call: trampoline name malformed: %s", key_name);
650 return -1;
651 }
652 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
653 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
654
655 key_sym = find_symbol_by_name(file->elf, tmp);
656 if (!key_sym) {
Josh Poimboeuf73f44fe2021-01-27 17:18:37 -0600657 if (!module) {
658 WARN("static_call: can't find static_call_key symbol: %s", tmp);
659 return -1;
660 }
661
662 /*
663 * For modules(), the key might not be exported, which
664 * means the module can make static calls but isn't
665 * allowed to change them.
666 *
667 * In that case we temporarily set the key to be the
668 * trampoline address. This is fixed up in
669 * static_call_add_module().
670 */
671 key_sym = insn->call_dest;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200672 }
673 free(key_name);
674
675 /* populate reloc for 'key' */
Peter Zijlstraef47cc02021-03-26 16:12:07 +0100676 if (elf_add_reloc(file->elf, sec,
677 idx * sizeof(struct static_call_site) + 4,
678 R_X86_64_PC32, key_sym,
679 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200680 return -1;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200681
682 idx++;
683 }
684
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200685 return 0;
686}
687
Peter Zijlstra134ab5b2021-10-26 14:01:36 +0200688static int create_retpoline_sites_sections(struct objtool_file *file)
689{
690 struct instruction *insn;
691 struct section *sec;
692 int idx;
693
694 sec = find_section_by_name(file->elf, ".retpoline_sites");
695 if (sec) {
696 WARN("file already has .retpoline_sites, skipping");
697 return 0;
698 }
699
700 idx = 0;
701 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
702 idx++;
703
704 if (!idx)
705 return 0;
706
707 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
708 sizeof(int), idx);
709 if (!sec) {
710 WARN("elf_create_section: .retpoline_sites");
711 return -1;
712 }
713
714 idx = 0;
715 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
716
717 int *site = (int *)sec->data->d_buf + idx;
718 *site = 0;
719
720 if (elf_add_reloc_to_insn(file->elf, sec,
721 idx * sizeof(int),
722 R_X86_64_PC32,
723 insn->sec, insn->offset)) {
724 WARN("elf_add_reloc_to_insn: .retpoline_sites");
725 return -1;
726 }
727
728 idx++;
729 }
730
731 return 0;
732}
733
Peter Zijlstra99d00212020-08-06 15:14:09 -0700734static int create_mcount_loc_sections(struct objtool_file *file)
735{
Peter Zijlstraef47cc02021-03-26 16:12:07 +0100736 struct section *sec;
Peter Zijlstra99d00212020-08-06 15:14:09 -0700737 unsigned long *loc;
738 struct instruction *insn;
739 int idx;
740
741 sec = find_section_by_name(file->elf, "__mcount_loc");
742 if (sec) {
743 INIT_LIST_HEAD(&file->mcount_loc_list);
744 WARN("file already has __mcount_loc section, skipping");
745 return 0;
746 }
747
748 if (list_empty(&file->mcount_loc_list))
749 return 0;
750
751 idx = 0;
Peter Zijlstrac5093312021-10-26 14:01:35 +0200752 list_for_each_entry(insn, &file->mcount_loc_list, call_node)
Peter Zijlstra99d00212020-08-06 15:14:09 -0700753 idx++;
754
755 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
756 if (!sec)
757 return -1;
758
Peter Zijlstra99d00212020-08-06 15:14:09 -0700759 idx = 0;
Peter Zijlstrac5093312021-10-26 14:01:35 +0200760 list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
Peter Zijlstra99d00212020-08-06 15:14:09 -0700761
762 loc = (unsigned long *)sec->data->d_buf + idx;
763 memset(loc, 0, sizeof(unsigned long));
764
Peter Zijlstraef47cc02021-03-26 16:12:07 +0100765 if (elf_add_reloc_to_insn(file->elf, sec,
766 idx * sizeof(unsigned long),
767 R_X86_64_64,
768 insn->sec, insn->offset))
Peter Zijlstra99d00212020-08-06 15:14:09 -0700769 return -1;
Peter Zijlstra99d00212020-08-06 15:14:09 -0700770
771 idx++;
772 }
773
Peter Zijlstra99d00212020-08-06 15:14:09 -0700774 return 0;
775}
776
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500777/*
778 * Warnings shouldn't be reported for ignored functions.
779 */
780static void add_ignores(struct objtool_file *file)
781{
782 struct instruction *insn;
783 struct section *sec;
784 struct symbol *func;
Matt Helsleyf1974222020-05-29 14:01:13 -0700785 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500786
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100787 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
788 if (!sec)
789 return;
790
Matt Helsleyf1974222020-05-29 14:01:13 -0700791 list_for_each_entry(reloc, &sec->reloc_list, list) {
792 switch (reloc->sym->type) {
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100793 case STT_FUNC:
Matt Helsleyf1974222020-05-29 14:01:13 -0700794 func = reloc->sym;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100795 break;
796
797 case STT_SECTION:
Matt Helsleyf1974222020-05-29 14:01:13 -0700798 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600799 if (!func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500800 continue;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100801 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500802
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100803 default:
Matt Helsleyf1974222020-05-29 14:01:13 -0700804 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100805 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500806 }
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100807
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100808 func_for_each_insn(file, func, insn)
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100809 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500810 }
811}
812
813/*
Peter Zijlstraea242132019-02-25 12:50:09 +0100814 * This is a whitelist of functions that is allowed to be called with AC set.
815 * The list is meant to be minimal and only contains compiler instrumentation
816 * ABI and a few functions used to implement *_{to,from}_user() functions.
817 *
818 * These functions must not directly change AC, but may PUSHF/POPF.
819 */
820static const char *uaccess_safe_builtin[] = {
821 /* KASAN */
822 "kasan_report",
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800823 "kasan_check_range",
Peter Zijlstraea242132019-02-25 12:50:09 +0100824 /* KASAN out-of-line */
825 "__asan_loadN_noabort",
826 "__asan_load1_noabort",
827 "__asan_load2_noabort",
828 "__asan_load4_noabort",
829 "__asan_load8_noabort",
830 "__asan_load16_noabort",
831 "__asan_storeN_noabort",
832 "__asan_store1_noabort",
833 "__asan_store2_noabort",
834 "__asan_store4_noabort",
835 "__asan_store8_noabort",
836 "__asan_store16_noabort",
Jann Hornb0b8e562020-09-29 00:49:16 +0200837 "__kasan_check_read",
838 "__kasan_check_write",
Peter Zijlstraea242132019-02-25 12:50:09 +0100839 /* KASAN in-line */
840 "__asan_report_load_n_noabort",
841 "__asan_report_load1_noabort",
842 "__asan_report_load2_noabort",
843 "__asan_report_load4_noabort",
844 "__asan_report_load8_noabort",
845 "__asan_report_load16_noabort",
846 "__asan_report_store_n_noabort",
847 "__asan_report_store1_noabort",
848 "__asan_report_store2_noabort",
849 "__asan_report_store4_noabort",
850 "__asan_report_store8_noabort",
851 "__asan_report_store16_noabort",
Marco Elver5f5c9712019-11-14 19:02:57 +0100852 /* KCSAN */
Marco Elver99676832020-03-25 17:41:57 +0100853 "__kcsan_check_access",
Marco Elver0525bd82021-11-30 12:44:30 +0100854 "__kcsan_mb",
855 "__kcsan_wmb",
856 "__kcsan_rmb",
857 "__kcsan_release",
Marco Elver5f5c9712019-11-14 19:02:57 +0100858 "kcsan_found_watchpoint",
859 "kcsan_setup_watchpoint",
Marco Elver99676832020-03-25 17:41:57 +0100860 "kcsan_check_scoped_accesses",
Marco Elver50a19ad2020-04-24 17:47:30 +0200861 "kcsan_disable_current",
862 "kcsan_enable_current_nowarn",
Marco Elver5f5c9712019-11-14 19:02:57 +0100863 /* KCSAN/TSAN */
864 "__tsan_func_entry",
865 "__tsan_func_exit",
866 "__tsan_read_range",
867 "__tsan_write_range",
868 "__tsan_read1",
869 "__tsan_read2",
870 "__tsan_read4",
871 "__tsan_read8",
872 "__tsan_read16",
873 "__tsan_write1",
874 "__tsan_write2",
875 "__tsan_write4",
876 "__tsan_write8",
877 "__tsan_write16",
Marco Elvera81b3752020-07-24 09:00:02 +0200878 "__tsan_read_write1",
879 "__tsan_read_write2",
880 "__tsan_read_write4",
881 "__tsan_read_write8",
882 "__tsan_read_write16",
Marco Elver883957b2020-07-03 15:40:30 +0200883 "__tsan_atomic8_load",
884 "__tsan_atomic16_load",
885 "__tsan_atomic32_load",
886 "__tsan_atomic64_load",
887 "__tsan_atomic8_store",
888 "__tsan_atomic16_store",
889 "__tsan_atomic32_store",
890 "__tsan_atomic64_store",
891 "__tsan_atomic8_exchange",
892 "__tsan_atomic16_exchange",
893 "__tsan_atomic32_exchange",
894 "__tsan_atomic64_exchange",
895 "__tsan_atomic8_fetch_add",
896 "__tsan_atomic16_fetch_add",
897 "__tsan_atomic32_fetch_add",
898 "__tsan_atomic64_fetch_add",
899 "__tsan_atomic8_fetch_sub",
900 "__tsan_atomic16_fetch_sub",
901 "__tsan_atomic32_fetch_sub",
902 "__tsan_atomic64_fetch_sub",
903 "__tsan_atomic8_fetch_and",
904 "__tsan_atomic16_fetch_and",
905 "__tsan_atomic32_fetch_and",
906 "__tsan_atomic64_fetch_and",
907 "__tsan_atomic8_fetch_or",
908 "__tsan_atomic16_fetch_or",
909 "__tsan_atomic32_fetch_or",
910 "__tsan_atomic64_fetch_or",
911 "__tsan_atomic8_fetch_xor",
912 "__tsan_atomic16_fetch_xor",
913 "__tsan_atomic32_fetch_xor",
914 "__tsan_atomic64_fetch_xor",
915 "__tsan_atomic8_fetch_nand",
916 "__tsan_atomic16_fetch_nand",
917 "__tsan_atomic32_fetch_nand",
918 "__tsan_atomic64_fetch_nand",
919 "__tsan_atomic8_compare_exchange_strong",
920 "__tsan_atomic16_compare_exchange_strong",
921 "__tsan_atomic32_compare_exchange_strong",
922 "__tsan_atomic64_compare_exchange_strong",
923 "__tsan_atomic8_compare_exchange_weak",
924 "__tsan_atomic16_compare_exchange_weak",
925 "__tsan_atomic32_compare_exchange_weak",
926 "__tsan_atomic64_compare_exchange_weak",
927 "__tsan_atomic8_compare_exchange_val",
928 "__tsan_atomic16_compare_exchange_val",
929 "__tsan_atomic32_compare_exchange_val",
930 "__tsan_atomic64_compare_exchange_val",
931 "__tsan_atomic_thread_fence",
932 "__tsan_atomic_signal_fence",
Peter Zijlstraea242132019-02-25 12:50:09 +0100933 /* KCOV */
934 "write_comp_data",
Josh Poimboeufae033f02020-04-29 14:09:04 -0500935 "check_kcov_mode",
Peter Zijlstraea242132019-02-25 12:50:09 +0100936 "__sanitizer_cov_trace_pc",
937 "__sanitizer_cov_trace_const_cmp1",
938 "__sanitizer_cov_trace_const_cmp2",
939 "__sanitizer_cov_trace_const_cmp4",
940 "__sanitizer_cov_trace_const_cmp8",
941 "__sanitizer_cov_trace_cmp1",
942 "__sanitizer_cov_trace_cmp2",
943 "__sanitizer_cov_trace_cmp4",
944 "__sanitizer_cov_trace_cmp8",
Al Viro36b1c702020-02-16 13:07:49 -0500945 "__sanitizer_cov_trace_switch",
Peter Zijlstraea242132019-02-25 12:50:09 +0100946 /* UBSAN */
947 "ubsan_type_mismatch_common",
948 "__ubsan_handle_type_mismatch",
949 "__ubsan_handle_type_mismatch_v1",
Peter Zijlstra9a50dca2019-10-21 15:11:49 +0200950 "__ubsan_handle_shift_out_of_bounds",
Peter Zijlstraea242132019-02-25 12:50:09 +0100951 /* misc */
952 "csum_partial_copy_generic",
Dan Williamsec6347b2020-10-05 20:40:16 -0700953 "copy_mc_fragile",
954 "copy_mc_fragile_handle_tail",
Dan Williams5da8e4a2020-10-05 20:40:25 -0700955 "copy_mc_enhanced_fast_string",
Peter Zijlstraea242132019-02-25 12:50:09 +0100956 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
957 NULL
958};
959
960static void add_uaccess_safe(struct objtool_file *file)
961{
962 struct symbol *func;
963 const char **name;
964
965 if (!uaccess)
966 return;
967
968 for (name = uaccess_safe_builtin; *name; name++) {
969 func = find_symbol_by_name(file->elf, *name);
970 if (!func)
971 continue;
972
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500973 func->uaccess_safe = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500974 }
975}
976
977/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000978 * FIXME: For now, just ignore any alternatives which add retpolines. This is
979 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
980 * But it at least allows objtool to understand the control flow *around* the
981 * retpoline.
982 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100983static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000984{
985 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700986 struct reloc *reloc;
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000987 struct instruction *insn;
988
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100989 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000990 if (!sec)
991 return 0;
992
Matt Helsleyf1974222020-05-29 14:01:13 -0700993 list_for_each_entry(reloc, &sec->reloc_list, list) {
994 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000995 WARN("unexpected relocation symbol type in %s", sec->name);
996 return -1;
997 }
998
Matt Helsleyf1974222020-05-29 14:01:13 -0700999 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001000 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001001 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001002 return -1;
1003 }
1004
1005 insn->ignore_alts = true;
1006 }
1007
1008 return 0;
1009}
1010
Peter Zijlstra530b4dd2021-03-26 16:12:04 +01001011__weak bool arch_is_retpoline(struct symbol *sym)
1012{
1013 return false;
1014}
1015
Peter Zijlstra7bd2a602021-03-26 16:12:13 +01001016#define NEGATIVE_RELOC ((void *)-1L)
1017
1018static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
1019{
1020 if (insn->reloc == NEGATIVE_RELOC)
1021 return NULL;
1022
1023 if (!insn->reloc) {
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02001024 if (!file)
1025 return NULL;
1026
Peter Zijlstra7bd2a602021-03-26 16:12:13 +01001027 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1028 insn->offset, insn->len);
1029 if (!insn->reloc) {
1030 insn->reloc = NEGATIVE_RELOC;
1031 return NULL;
1032 }
1033 }
1034
1035 return insn->reloc;
1036}
1037
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001038static void remove_insn_ops(struct instruction *insn)
1039{
1040 struct stack_op *op, *tmp;
1041
1042 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
1043 list_del(&op->list);
1044 free(op);
1045 }
1046}
1047
Peter Zijlstradd003ed2021-10-26 14:01:34 +02001048static void annotate_call_site(struct objtool_file *file,
1049 struct instruction *insn, bool sibling)
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001050{
1051 struct reloc *reloc = insn_reloc(file, insn);
Peter Zijlstradd003ed2021-10-26 14:01:34 +02001052 struct symbol *sym = insn->call_dest;
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001053
Peter Zijlstradd003ed2021-10-26 14:01:34 +02001054 if (!sym)
1055 sym = reloc->sym;
1056
1057 /*
1058 * Alternative replacement code is just template code which is
1059 * sometimes copied to the original instruction. For now, don't
1060 * annotate it. (In the future we might consider annotating the
1061 * original instruction if/when it ever makes sense to do so.)
1062 */
1063 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001064 return;
1065
Peter Zijlstradd003ed2021-10-26 14:01:34 +02001066 if (sym->static_call_tramp) {
1067 list_add_tail(&insn->call_node, &file->static_call_list);
1068 return;
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001069 }
1070
Peter Zijlstra134ab5b2021-10-26 14:01:36 +02001071 if (sym->retpoline_thunk) {
1072 list_add_tail(&insn->call_node, &file->retpoline_call_list);
1073 return;
1074 }
1075
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001076 /*
Marco Elver05098112021-11-30 12:44:31 +01001077 * Many compilers cannot disable KCOV or sanitizer calls with a function
1078 * attribute so they need a little help, NOP out any such calls from
1079 * noinstr text.
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001080 */
Marco Elver05098112021-11-30 12:44:31 +01001081 if (insn->sec->noinstr && sym->profiling_func) {
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001082 if (reloc) {
1083 reloc->type = R_NONE;
1084 elf_write_reloc(file->elf, reloc);
1085 }
1086
1087 elf_write_insn(file->elf, insn->sec,
1088 insn->offset, insn->len,
1089 sibling ? arch_ret_insn(insn->len)
1090 : arch_nop_insn(insn->len));
1091
1092 insn->type = sibling ? INSN_RETURN : INSN_NOP;
Peter Zijlstradd003ed2021-10-26 14:01:34 +02001093 return;
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001094 }
1095
Peter Zijlstradd003ed2021-10-26 14:01:34 +02001096 if (mcount && sym->fentry) {
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001097 if (sibling)
1098 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
1099
1100 if (reloc) {
1101 reloc->type = R_NONE;
1102 elf_write_reloc(file->elf, reloc);
1103 }
1104
1105 elf_write_insn(file->elf, insn->sec,
1106 insn->offset, insn->len,
1107 arch_nop_insn(insn->len));
1108
1109 insn->type = INSN_NOP;
1110
Peter Zijlstrac5093312021-10-26 14:01:35 +02001111 list_add_tail(&insn->call_node, &file->mcount_loc_list);
Peter Zijlstradd003ed2021-10-26 14:01:34 +02001112 return;
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001113 }
Peter Zijlstradd003ed2021-10-26 14:01:34 +02001114}
1115
1116static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1117 struct symbol *dest, bool sibling)
1118{
1119 insn->call_dest = dest;
1120 if (!dest)
1121 return;
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001122
1123 /*
1124 * Whatever stack impact regular CALLs have, should be undone
1125 * by the RETURN of the called function.
1126 *
1127 * Annotated intra-function calls retain the stack_ops but
1128 * are converted to JUMP, see read_intra_function_calls().
1129 */
1130 remove_insn_ops(insn);
Peter Zijlstradd003ed2021-10-26 14:01:34 +02001131
1132 annotate_call_site(file, insn, sibling);
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001133}
1134
Peter Zijlstra134ab5b2021-10-26 14:01:36 +02001135static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1136{
1137 /*
1138 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1139 * so convert them accordingly.
1140 */
1141 switch (insn->type) {
1142 case INSN_CALL:
1143 insn->type = INSN_CALL_DYNAMIC;
1144 break;
1145 case INSN_JUMP_UNCONDITIONAL:
1146 insn->type = INSN_JUMP_DYNAMIC;
1147 break;
1148 case INSN_JUMP_CONDITIONAL:
1149 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1150 break;
1151 default:
1152 return;
1153 }
1154
1155 insn->retpoline_safe = true;
1156
1157 /*
1158 * Whatever stack impact regular CALLs have, should be undone
1159 * by the RETURN of the called function.
1160 *
1161 * Annotated intra-function calls retain the stack_ops but
1162 * are converted to JUMP, see read_intra_function_calls().
1163 */
1164 remove_insn_ops(insn);
1165
1166 annotate_call_site(file, insn, false);
1167}
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001168/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001169 * Find the destination instructions for all jumps.
1170 */
1171static int add_jump_destinations(struct objtool_file *file)
1172{
1173 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001174 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001175 struct section *dest_sec;
1176 unsigned long dest_off;
1177
1178 for_each_insn(file, insn) {
Josh Poimboeufa2296142020-02-10 12:32:39 -06001179 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001180 continue;
1181
Peter Zijlstra7bd2a602021-03-26 16:12:13 +01001182 reloc = insn_reloc(file, insn);
Matt Helsleyf1974222020-05-29 14:01:13 -07001183 if (!reloc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001184 dest_sec = insn->sec;
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001185 dest_off = arch_jump_destination(insn);
Matt Helsleyf1974222020-05-29 14:01:13 -07001186 } else if (reloc->sym->type == STT_SECTION) {
1187 dest_sec = reloc->sym->sec;
1188 dest_off = arch_dest_reloc_offset(reloc->addend);
Peter Zijlstra1739c662021-10-26 14:01:33 +02001189 } else if (reloc->sym->retpoline_thunk) {
Peter Zijlstra134ab5b2021-10-26 14:01:36 +02001190 add_retpoline_call(file, insn);
Josh Poimboeuf39b73532018-01-11 21:46:23 +00001191 continue;
Josh Poimboeufecf11ba2021-01-21 15:29:22 -06001192 } else if (insn->func) {
1193 /* internal or external sibling call (with reloc) */
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001194 add_call_dest(file, insn, reloc->sym, true);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001195 continue;
Josh Poimboeufecf11ba2021-01-21 15:29:22 -06001196 } else if (reloc->sym->sec->idx) {
1197 dest_sec = reloc->sym->sec;
1198 dest_off = reloc->sym->sym.st_value +
1199 arch_dest_reloc_offset(reloc->addend);
1200 } else {
1201 /* non-func asm code jumping to another file */
1202 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001203 }
1204
1205 insn->jump_dest = find_insn(file, dest_sec, dest_off);
1206 if (!insn->jump_dest) {
1207
1208 /*
1209 * This is a special case where an alt instruction
1210 * jumps past the end of the section. These are
1211 * handled later in handle_group_alt().
1212 */
1213 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1214 continue;
1215
1216 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1217 insn->sec, insn->offset, dest_sec->name,
1218 dest_off);
1219 return -1;
1220 }
Josh Poimboeufcd778492018-06-01 07:23:51 -05001221
1222 /*
Peter Zijlstra54262aa2019-03-06 12:58:15 +01001223 * Cross-function jump.
Josh Poimboeufcd778492018-06-01 07:23:51 -05001224 */
1225 if (insn->func && insn->jump_dest->func &&
Peter Zijlstra54262aa2019-03-06 12:58:15 +01001226 insn->func != insn->jump_dest->func) {
1227
1228 /*
1229 * For GCC 8+, create parent/child links for any cold
1230 * subfunctions. This is _mostly_ redundant with a
1231 * similar initialization in read_symbols().
1232 *
1233 * If a function has aliases, we want the *first* such
1234 * function in the symbol table to be the subfunction's
1235 * parent. In that case we overwrite the
1236 * initialization done in read_symbols().
1237 *
1238 * However this code can't completely replace the
1239 * read_symbols() code because this doesn't detect the
1240 * case where the parent function's only reference to a
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001241 * subfunction is through a jump table.
Peter Zijlstra54262aa2019-03-06 12:58:15 +01001242 */
Josh Poimboeuf34ca59e2021-01-21 15:29:19 -06001243 if (!strstr(insn->func->name, ".cold") &&
1244 strstr(insn->jump_dest->func->name, ".cold")) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01001245 insn->func->cfunc = insn->jump_dest->func;
1246 insn->jump_dest->func->pfunc = insn->func;
1247
1248 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
1249 insn->jump_dest->offset == insn->jump_dest->func->offset) {
Josh Poimboeufecf11ba2021-01-21 15:29:22 -06001250 /* internal sibling call (without reloc) */
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001251 add_call_dest(file, insn, insn->jump_dest->func, true);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01001252 }
Josh Poimboeufcd778492018-06-01 07:23:51 -05001253 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001254 }
1255
1256 return 0;
1257}
1258
Julien Thierry2b232a22020-09-15 08:53:18 +01001259static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1260{
1261 struct symbol *call_dest;
1262
1263 call_dest = find_func_by_offset(sec, offset);
1264 if (!call_dest)
1265 call_dest = find_symbol_by_offset(sec, offset);
1266
1267 return call_dest;
1268}
1269
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001270/*
1271 * Find the destination instructions for all calls.
1272 */
1273static int add_call_destinations(struct objtool_file *file)
1274{
1275 struct instruction *insn;
1276 unsigned long dest_off;
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001277 struct symbol *dest;
Matt Helsleyf1974222020-05-29 14:01:13 -07001278 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001279
1280 for_each_insn(file, insn) {
1281 if (insn->type != INSN_CALL)
1282 continue;
1283
Peter Zijlstra7bd2a602021-03-26 16:12:13 +01001284 reloc = insn_reloc(file, insn);
Matt Helsleyf1974222020-05-29 14:01:13 -07001285 if (!reloc) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001286 dest_off = arch_jump_destination(insn);
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001287 dest = find_call_destination(insn->sec, dest_off);
1288
1289 add_call_dest(file, insn, dest, false);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001290
Josh Poimboeuf7acfe532020-02-17 21:41:54 -06001291 if (insn->ignore)
1292 continue;
1293
1294 if (!insn->call_dest) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001295 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001296 return -1;
1297 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001298
Josh Poimboeuf7acfe532020-02-17 21:41:54 -06001299 if (insn->func && insn->call_dest->type != STT_FUNC) {
1300 WARN_FUNC("unsupported call to non-function",
1301 insn->sec, insn->offset);
1302 return -1;
1303 }
1304
Matt Helsleyf1974222020-05-29 14:01:13 -07001305 } else if (reloc->sym->type == STT_SECTION) {
1306 dest_off = arch_dest_reloc_offset(reloc->addend);
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001307 dest = find_call_destination(reloc->sym->sec, dest_off);
1308 if (!dest) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001309 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001310 insn->sec, insn->offset,
Matt Helsleyf1974222020-05-29 14:01:13 -07001311 reloc->sym->sec->name,
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001312 dest_off);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001313 return -1;
1314 }
Peter Zijlstrabcb1b6f2021-03-26 16:12:03 +01001315
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001316 add_call_dest(file, insn, dest, false);
1317
Peter Zijlstra1739c662021-10-26 14:01:33 +02001318 } else if (reloc->sym->retpoline_thunk) {
Peter Zijlstra134ab5b2021-10-26 14:01:36 +02001319 add_retpoline_call(file, insn);
Peter Zijlstrabcb1b6f2021-03-26 16:12:03 +01001320
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001321 } else
Peter Zijlstraf56dae82021-06-24 11:41:02 +02001322 add_call_dest(file, insn, reloc->sym, false);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001323 }
1324
1325 return 0;
1326}
1327
1328/*
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001329 * The .alternatives section requires some extra special care over and above
1330 * other special sections because alternatives are patched in place.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001331 */
1332static int handle_group_alt(struct objtool_file *file,
1333 struct special_alt *special_alt,
1334 struct instruction *orig_insn,
1335 struct instruction **new_insn)
1336{
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001337 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001338 struct alt_group *orig_alt_group, *new_alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001339 unsigned long dest_off;
1340
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001341
1342 orig_alt_group = malloc(sizeof(*orig_alt_group));
1343 if (!orig_alt_group) {
1344 WARN("malloc failed");
1345 return -1;
1346 }
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001347 orig_alt_group->cfi = calloc(special_alt->orig_len,
1348 sizeof(struct cfi_state *));
1349 if (!orig_alt_group->cfi) {
1350 WARN("calloc failed");
1351 return -1;
1352 }
1353
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001354 last_orig_insn = NULL;
1355 insn = orig_insn;
1356 sec_for_each_insn_from(file, insn) {
1357 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1358 break;
1359
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001360 insn->alt_group = orig_alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001361 last_orig_insn = insn;
1362 }
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001363 orig_alt_group->orig_group = NULL;
1364 orig_alt_group->first_insn = orig_insn;
1365 orig_alt_group->last_insn = last_orig_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001366
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001367
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001368 new_alt_group = malloc(sizeof(*new_alt_group));
1369 if (!new_alt_group) {
1370 WARN("malloc failed");
1371 return -1;
1372 }
1373
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001374 if (special_alt->new_len < special_alt->orig_len) {
1375 /*
1376 * Insert a fake nop at the end to make the replacement
1377 * alt_group the same size as the original. This is needed to
1378 * allow propagate_alt_cfi() to do its magic. When the last
1379 * instruction affects the stack, the instruction after it (the
1380 * nop) will propagate the new state to the shared CFI array.
1381 */
1382 nop = malloc(sizeof(*nop));
1383 if (!nop) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001384 WARN("malloc failed");
1385 return -1;
1386 }
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001387 memset(nop, 0, sizeof(*nop));
1388 INIT_LIST_HEAD(&nop->alts);
1389 INIT_LIST_HEAD(&nop->stack_ops);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001390
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001391 nop->sec = special_alt->new_sec;
1392 nop->offset = special_alt->new_off + special_alt->new_len;
1393 nop->len = special_alt->orig_len - special_alt->new_len;
1394 nop->type = INSN_NOP;
1395 nop->func = orig_insn->func;
1396 nop->alt_group = new_alt_group;
1397 nop->ignore = orig_insn->ignore_alts;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001398 }
1399
1400 if (!special_alt->new_len) {
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001401 *new_insn = nop;
1402 goto end;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001403 }
1404
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001405 insn = *new_insn;
1406 sec_for_each_insn_from(file, insn) {
Julien Thierry45245f52020-09-04 16:30:23 +01001407 struct reloc *alt_reloc;
1408
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001409 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1410 break;
1411
1412 last_new_insn = insn;
1413
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001414 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +01001415 insn->func = orig_insn->func;
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001416 insn->alt_group = new_alt_group;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001417
Josh Poimboeufdc419722020-02-10 12:32:40 -06001418 /*
1419 * Since alternative replacement code is copy/pasted by the
1420 * kernel after applying relocations, generally such code can't
1421 * have relative-address relocation references to outside the
1422 * .altinstr_replacement section, unless the arch's
1423 * alternatives code can adjust the relative offsets
1424 * accordingly.
Josh Poimboeufdc419722020-02-10 12:32:40 -06001425 */
Peter Zijlstra7bd2a602021-03-26 16:12:13 +01001426 alt_reloc = insn_reloc(file, insn);
Julien Thierry45245f52020-09-04 16:30:23 +01001427 if (alt_reloc &&
1428 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
Josh Poimboeufdc419722020-02-10 12:32:40 -06001429
1430 WARN_FUNC("unsupported relocation in alternatives section",
1431 insn->sec, insn->offset);
1432 return -1;
1433 }
1434
Josh Poimboeufa2296142020-02-10 12:32:39 -06001435 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001436 continue;
1437
1438 if (!insn->immediate)
1439 continue;
1440
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001441 dest_off = arch_jump_destination(insn);
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001442 if (dest_off == special_alt->new_off + special_alt->new_len)
1443 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001444
1445 if (!insn->jump_dest) {
1446 WARN_FUNC("can't find alternative jump destination",
1447 insn->sec, insn->offset);
1448 return -1;
1449 }
1450 }
1451
1452 if (!last_new_insn) {
1453 WARN_FUNC("can't find last new alternative instruction",
1454 special_alt->new_sec, special_alt->new_off);
1455 return -1;
1456 }
1457
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001458 if (nop)
1459 list_add(&nop->list, &last_new_insn->list);
1460end:
Josh Poimboeufb23cc712020-12-18 14:19:32 -06001461 new_alt_group->orig_group = orig_alt_group;
1462 new_alt_group->first_insn = *new_insn;
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06001463 new_alt_group->last_insn = nop ? : last_new_insn;
1464 new_alt_group->cfi = orig_alt_group->cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001465 return 0;
1466}
1467
1468/*
1469 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1470 * If the original instruction is a jump, make the alt entry an effective nop
1471 * by just skipping the original instruction.
1472 */
1473static int handle_jump_alt(struct objtool_file *file,
1474 struct special_alt *special_alt,
1475 struct instruction *orig_insn,
1476 struct instruction **new_insn)
1477{
Peter Zijlstra48001d262021-05-13 16:15:50 +02001478 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1479 orig_insn->type != INSN_NOP) {
Peter Zijlstrae2d94942021-05-06 21:34:04 +02001480
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001481 WARN_FUNC("unsupported instruction at jump label",
1482 orig_insn->sec, orig_insn->offset);
1483 return -1;
1484 }
1485
Peter Zijlstra6d37b832021-05-06 21:34:03 +02001486 if (special_alt->key_addend & 2) {
1487 struct reloc *reloc = insn_reloc(file, orig_insn);
1488
1489 if (reloc) {
1490 reloc->type = R_NONE;
1491 elf_write_reloc(file->elf, reloc);
1492 }
1493 elf_write_insn(file->elf, orig_insn->sec,
1494 orig_insn->offset, orig_insn->len,
1495 arch_nop_insn(orig_insn->len));
1496 orig_insn->type = INSN_NOP;
Peter Zijlstra48001d262021-05-13 16:15:50 +02001497 }
1498
1499 if (orig_insn->type == INSN_NOP) {
1500 if (orig_insn->len == 2)
1501 file->jl_nop_short++;
1502 else
1503 file->jl_nop_long++;
1504
1505 return 0;
Peter Zijlstra6d37b832021-05-06 21:34:03 +02001506 }
1507
Peter Zijlstrae2d94942021-05-06 21:34:04 +02001508 if (orig_insn->len == 2)
1509 file->jl_short++;
1510 else
1511 file->jl_long++;
1512
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001513 *new_insn = list_next_entry(orig_insn, list);
1514 return 0;
1515}
1516
1517/*
1518 * Read all the special sections which have alternate instructions which can be
1519 * patched in or redirected to at runtime. Each instruction having alternate
1520 * instruction(s) has them added to its insn->alts list, which will be
1521 * traversed in validate_branch().
1522 */
1523static int add_special_section_alts(struct objtool_file *file)
1524{
1525 struct list_head special_alts;
1526 struct instruction *orig_insn, *new_insn;
1527 struct special_alt *special_alt, *tmp;
1528 struct alternative *alt;
1529 int ret;
1530
1531 ret = special_get_alts(file->elf, &special_alts);
1532 if (ret)
1533 return ret;
1534
1535 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001536
1537 orig_insn = find_insn(file, special_alt->orig_sec,
1538 special_alt->orig_off);
1539 if (!orig_insn) {
1540 WARN_FUNC("special: can't find orig instruction",
1541 special_alt->orig_sec, special_alt->orig_off);
1542 ret = -1;
1543 goto out;
1544 }
1545
1546 new_insn = NULL;
1547 if (!special_alt->group || special_alt->new_len) {
1548 new_insn = find_insn(file, special_alt->new_sec,
1549 special_alt->new_off);
1550 if (!new_insn) {
1551 WARN_FUNC("special: can't find new instruction",
1552 special_alt->new_sec,
1553 special_alt->new_off);
1554 ret = -1;
1555 goto out;
1556 }
1557 }
1558
1559 if (special_alt->group) {
Julien Thierry7170cf42020-03-27 15:28:41 +00001560 if (!special_alt->orig_len) {
1561 WARN_FUNC("empty alternative entry",
1562 orig_insn->sec, orig_insn->offset);
1563 continue;
1564 }
1565
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001566 ret = handle_group_alt(file, special_alt, orig_insn,
1567 &new_insn);
1568 if (ret)
1569 goto out;
1570 } else if (special_alt->jump_or_nop) {
1571 ret = handle_jump_alt(file, special_alt, orig_insn,
1572 &new_insn);
1573 if (ret)
1574 goto out;
1575 }
1576
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001577 alt = malloc(sizeof(*alt));
1578 if (!alt) {
1579 WARN("malloc failed");
1580 ret = -1;
1581 goto out;
1582 }
1583
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001584 alt->insn = new_insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +01001585 alt->skip_orig = special_alt->skip_orig;
Peter Zijlstraea242132019-02-25 12:50:09 +01001586 orig_insn->ignore_alts |= special_alt->skip_alt;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001587 list_add_tail(&alt->list, &orig_insn->alts);
1588
1589 list_del(&special_alt->list);
1590 free(special_alt);
1591 }
1592
Peter Zijlstrae2d94942021-05-06 21:34:04 +02001593 if (stats) {
1594 printf("jl\\\tNOP\tJMP\n");
1595 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
1596 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
1597 }
1598
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001599out:
1600 return ret;
1601}
1602
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001603static int add_jump_table(struct objtool_file *file, struct instruction *insn,
Matt Helsleyf1974222020-05-29 14:01:13 -07001604 struct reloc *table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001605{
Matt Helsleyf1974222020-05-29 14:01:13 -07001606 struct reloc *reloc = table;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001607 struct instruction *dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001608 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001609 struct symbol *pfunc = insn->func->pfunc;
1610 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001611
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001612 /*
Matt Helsleyf1974222020-05-29 14:01:13 -07001613 * Each @reloc is a switch table relocation which points to the target
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001614 * instruction.
1615 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001616 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
Jann Hornbd98c812019-07-17 20:36:54 -05001617
1618 /* Check for the end of the table: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001619 if (reloc != table && reloc->jump_table_start)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001620 break;
1621
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001622 /* Make sure the table entries are consecutive: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001623 if (prev_offset && reloc->offset != prev_offset + 8)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001624 break;
1625
1626 /* Detect function pointers from contiguous objects: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001627 if (reloc->sym->sec == pfunc->sec &&
1628 reloc->addend == pfunc->offset)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001629 break;
1630
Matt Helsleyf1974222020-05-29 14:01:13 -07001631 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001632 if (!dest_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001633 break;
1634
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001635 /* Make sure the destination is in the same function: */
Josh Poimboeufe65050b2019-07-17 20:36:55 -05001636 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -05001637 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001638
1639 alt = malloc(sizeof(*alt));
1640 if (!alt) {
1641 WARN("malloc failed");
1642 return -1;
1643 }
1644
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001645 alt->insn = dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001646 list_add_tail(&alt->list, &insn->alts);
Matt Helsleyf1974222020-05-29 14:01:13 -07001647 prev_offset = reloc->offset;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001648 }
1649
1650 if (!prev_offset) {
1651 WARN_FUNC("can't find switch jump table",
1652 insn->sec, insn->offset);
1653 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001654 }
1655
1656 return 0;
1657}
1658
1659/*
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001660 * find_jump_table() - Given a dynamic jump, find the switch jump table
1661 * associated with it.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001662 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001663static struct reloc *find_jump_table(struct objtool_file *file,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001664 struct symbol *func,
1665 struct instruction *insn)
1666{
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001667 struct reloc *table_reloc;
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001668 struct instruction *dest_insn, *orig_insn = insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001669
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001670 /*
1671 * Backward search using the @first_jump_src links, these help avoid
1672 * much of the 'in between' code. Which avoids us getting confused by
1673 * it.
1674 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001675 for (;
Josh Poimboeuf1119d262020-04-28 16:45:16 -05001676 insn && insn->func && insn->func->pfunc == func;
1677 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001678
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001679 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001680 break;
1681
1682 /* allow small jumps within the range */
1683 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1684 insn->jump_dest &&
1685 (insn->jump_dest->offset <= insn->offset ||
1686 insn->jump_dest->offset > orig_insn->offset))
1687 break;
1688
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001689 table_reloc = arch_find_switch_table(file, insn);
Matt Helsleyf1974222020-05-29 14:01:13 -07001690 if (!table_reloc)
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001691 continue;
Matt Helsleyf1974222020-05-29 14:01:13 -07001692 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001693 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1694 continue;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001695
Matt Helsleyf1974222020-05-29 14:01:13 -07001696 return table_reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001697 }
1698
1699 return NULL;
1700}
1701
Jann Hornbd98c812019-07-17 20:36:54 -05001702/*
1703 * First pass: Mark the head of each jump table so that in the next pass,
1704 * we know when a given jump table ends and the next one starts.
1705 */
1706static void mark_func_jump_tables(struct objtool_file *file,
1707 struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001708{
Jann Hornbd98c812019-07-17 20:36:54 -05001709 struct instruction *insn, *last = NULL;
Matt Helsleyf1974222020-05-29 14:01:13 -07001710 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001711
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001712 func_for_each_insn(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001713 if (!last)
1714 last = insn;
1715
1716 /*
1717 * Store back-pointers for unconditional forward jumps such
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001718 * that find_jump_table() can back-track using those and
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001719 * avoid some potentially confusing code.
1720 */
1721 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1722 insn->offset > last->offset &&
1723 insn->jump_dest->offset > insn->offset &&
1724 !insn->jump_dest->first_jump_src) {
1725
1726 insn->jump_dest->first_jump_src = insn;
1727 last = insn->jump_dest;
1728 }
1729
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001730 if (insn->type != INSN_JUMP_DYNAMIC)
1731 continue;
1732
Matt Helsleyf1974222020-05-29 14:01:13 -07001733 reloc = find_jump_table(file, func, insn);
1734 if (reloc) {
1735 reloc->jump_table_start = true;
1736 insn->jump_table = reloc;
Jann Hornbd98c812019-07-17 20:36:54 -05001737 }
1738 }
1739}
1740
1741static int add_func_jump_tables(struct objtool_file *file,
1742 struct symbol *func)
1743{
1744 struct instruction *insn;
1745 int ret;
1746
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001747 func_for_each_insn(file, func, insn) {
Jann Hornbd98c812019-07-17 20:36:54 -05001748 if (!insn->jump_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001749 continue;
1750
Jann Hornbd98c812019-07-17 20:36:54 -05001751 ret = add_jump_table(file, insn, insn->jump_table);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001752 if (ret)
1753 return ret;
1754 }
1755
1756 return 0;
1757}
1758
1759/*
1760 * For some switch statements, gcc generates a jump table in the .rodata
1761 * section which contains a list of addresses within the function to jump to.
1762 * This finds these jump tables and adds them to the insn->alts lists.
1763 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001764static int add_jump_table_alts(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001765{
1766 struct section *sec;
1767 struct symbol *func;
1768 int ret;
1769
Allan Xavier4a60aa02018-09-07 08:12:01 -05001770 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001771 return 0;
1772
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001773 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001774 list_for_each_entry(func, &sec->symbol_list, list) {
1775 if (func->type != STT_FUNC)
1776 continue;
1777
Jann Hornbd98c812019-07-17 20:36:54 -05001778 mark_func_jump_tables(file, func);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001779 ret = add_func_jump_tables(file, func);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001780 if (ret)
1781 return ret;
1782 }
1783 }
1784
1785 return 0;
1786}
1787
Josh Poimboeufb735bd32021-01-21 15:29:24 -06001788static void set_func_state(struct cfi_state *state)
1789{
1790 state->cfa = initial_func_cfi.cfa;
1791 memcpy(&state->regs, &initial_func_cfi.regs,
1792 CFI_NUM_REGS * sizeof(struct cfi_reg));
1793 state->stack_size = initial_func_cfi.cfa.offset;
1794}
1795
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001796static int read_unwind_hints(struct objtool_file *file)
1797{
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02001798 struct cfi_state cfi = init_cfi;
Matt Helsleyf1974222020-05-29 14:01:13 -07001799 struct section *sec, *relocsec;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001800 struct unwind_hint *hint;
1801 struct instruction *insn;
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02001802 struct reloc *reloc;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001803 int i;
1804
1805 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1806 if (!sec)
1807 return 0;
1808
Matt Helsleyf1974222020-05-29 14:01:13 -07001809 relocsec = sec->reloc;
1810 if (!relocsec) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001811 WARN("missing .rela.discard.unwind_hints section");
1812 return -1;
1813 }
1814
Joe Lawrencefe255fe2021-08-22 18:50:37 -04001815 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001816 WARN("struct unwind_hint size mismatch");
1817 return -1;
1818 }
1819
1820 file->hints = true;
1821
Joe Lawrencefe255fe2021-08-22 18:50:37 -04001822 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001823 hint = (struct unwind_hint *)sec->data->d_buf + i;
1824
Matt Helsleyf1974222020-05-29 14:01:13 -07001825 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1826 if (!reloc) {
1827 WARN("can't find reloc for unwind_hints[%d]", i);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001828 return -1;
1829 }
1830
Matt Helsleyf1974222020-05-29 14:01:13 -07001831 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001832 if (!insn) {
1833 WARN("can't find insn for unwind_hints[%d]", i);
1834 return -1;
1835 }
1836
Josh Poimboeufb735bd32021-01-21 15:29:24 -06001837 insn->hint = true;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001838
Josh Poimboeufb735bd32021-01-21 15:29:24 -06001839 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02001840 insn->cfi = &func_cfi;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001841 continue;
1842 }
1843
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02001844 if (insn->cfi)
1845 cfi = *(insn->cfi);
1846
1847 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001848 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1849 insn->sec, insn->offset, hint->sp_reg);
1850 return -1;
1851 }
1852
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02001853 cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1854 cfi.type = hint->type;
1855 cfi.end = hint->end;
1856
1857 insn->cfi = cfi_hash_find_or_add(&cfi);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001858 }
1859
1860 return 0;
1861}
1862
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001863static int read_retpoline_hints(struct objtool_file *file)
1864{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001865 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001866 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001867 struct reloc *reloc;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001868
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001869 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001870 if (!sec)
1871 return 0;
1872
Matt Helsleyf1974222020-05-29 14:01:13 -07001873 list_for_each_entry(reloc, &sec->reloc_list, list) {
1874 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001875 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001876 return -1;
1877 }
1878
Matt Helsleyf1974222020-05-29 14:01:13 -07001879 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001880 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001881 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001882 return -1;
1883 }
1884
1885 if (insn->type != INSN_JUMP_DYNAMIC &&
1886 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001887 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001888 insn->sec, insn->offset);
1889 return -1;
1890 }
1891
1892 insn->retpoline_safe = true;
1893 }
1894
1895 return 0;
1896}
1897
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001898static int read_instr_hints(struct objtool_file *file)
1899{
1900 struct section *sec;
1901 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001902 struct reloc *reloc;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001903
1904 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1905 if (!sec)
1906 return 0;
1907
Matt Helsleyf1974222020-05-29 14:01:13 -07001908 list_for_each_entry(reloc, &sec->reloc_list, list) {
1909 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001910 WARN("unexpected relocation symbol type in %s", sec->name);
1911 return -1;
1912 }
1913
Matt Helsleyf1974222020-05-29 14:01:13 -07001914 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001915 if (!insn) {
1916 WARN("bad .discard.instr_end entry");
1917 return -1;
1918 }
1919
1920 insn->instr--;
1921 }
1922
1923 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1924 if (!sec)
1925 return 0;
1926
Matt Helsleyf1974222020-05-29 14:01:13 -07001927 list_for_each_entry(reloc, &sec->reloc_list, list) {
1928 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001929 WARN("unexpected relocation symbol type in %s", sec->name);
1930 return -1;
1931 }
1932
Matt Helsleyf1974222020-05-29 14:01:13 -07001933 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001934 if (!insn) {
1935 WARN("bad .discard.instr_begin entry");
1936 return -1;
1937 }
1938
1939 insn->instr++;
1940 }
1941
1942 return 0;
1943}
1944
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001945static int read_intra_function_calls(struct objtool_file *file)
1946{
1947 struct instruction *insn;
1948 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -07001949 struct reloc *reloc;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001950
1951 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1952 if (!sec)
1953 return 0;
1954
Matt Helsleyf1974222020-05-29 14:01:13 -07001955 list_for_each_entry(reloc, &sec->reloc_list, list) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001956 unsigned long dest_off;
1957
Matt Helsleyf1974222020-05-29 14:01:13 -07001958 if (reloc->sym->type != STT_SECTION) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001959 WARN("unexpected relocation symbol type in %s",
1960 sec->name);
1961 return -1;
1962 }
1963
Matt Helsleyf1974222020-05-29 14:01:13 -07001964 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001965 if (!insn) {
1966 WARN("bad .discard.intra_function_call entry");
1967 return -1;
1968 }
1969
1970 if (insn->type != INSN_CALL) {
1971 WARN_FUNC("intra_function_call not a direct call",
1972 insn->sec, insn->offset);
1973 return -1;
1974 }
1975
1976 /*
1977 * Treat intra-function CALLs as JMPs, but with a stack_op.
1978 * See add_call_destinations(), which strips stack_ops from
1979 * normal CALLs.
1980 */
1981 insn->type = INSN_JUMP_UNCONDITIONAL;
1982
1983 dest_off = insn->offset + insn->len + insn->immediate;
1984 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1985 if (!insn->jump_dest) {
1986 WARN_FUNC("can't find call dest at %s+0x%lx",
1987 insn->sec, insn->offset,
1988 insn->sec->name, dest_off);
1989 return -1;
1990 }
1991 }
1992
1993 return 0;
1994}
1995
Marco Elver05098112021-11-30 12:44:31 +01001996/*
1997 * Return true if name matches an instrumentation function, where calls to that
1998 * function from noinstr code can safely be removed, but compilers won't do so.
1999 */
2000static bool is_profiling_func(const char *name)
2001{
2002 /*
2003 * Many compilers cannot disable KCOV with a function attribute.
2004 */
2005 if (!strncmp(name, "__sanitizer_cov_", 16))
2006 return true;
2007
2008 /*
2009 * Some compilers currently do not remove __tsan_func_entry/exit nor
2010 * __tsan_atomic_signal_fence (used for barrier instrumentation) with
2011 * the __no_sanitize_thread attribute, remove them. Once the kernel's
2012 * minimum Clang version is 14.0, this can be removed.
2013 */
2014 if (!strncmp(name, "__tsan_func_", 12) ||
2015 !strcmp(name, "__tsan_atomic_signal_fence"))
2016 return true;
2017
2018 return false;
2019}
2020
Peter Zijlstra1739c662021-10-26 14:01:33 +02002021static int classify_symbols(struct objtool_file *file)
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002022{
2023 struct section *sec;
2024 struct symbol *func;
2025
2026 for_each_sec(file, sec) {
2027 list_for_each_entry(func, &sec->symbol_list, list) {
Peter Zijlstra1739c662021-10-26 14:01:33 +02002028 if (func->bind != STB_GLOBAL)
2029 continue;
2030
2031 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002032 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2033 func->static_call_tramp = true;
Peter Zijlstra1739c662021-10-26 14:01:33 +02002034
2035 if (arch_is_retpoline(func))
2036 func->retpoline_thunk = true;
2037
2038 if (!strcmp(func->name, "__fentry__"))
2039 func->fentry = true;
2040
Marco Elver05098112021-11-30 12:44:31 +01002041 if (is_profiling_func(func->name))
2042 func->profiling_func = true;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002043 }
2044 }
2045
2046 return 0;
2047}
2048
Allan Xavier4a60aa02018-09-07 08:12:01 -05002049static void mark_rodata(struct objtool_file *file)
2050{
2051 struct section *sec;
2052 bool found = false;
2053
2054 /*
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05002055 * Search for the following rodata sections, each of which can
2056 * potentially contain jump tables:
2057 *
2058 * - .rodata: can contain GCC switch tables
2059 * - .rodata.<func>: same, if -fdata-sections is being used
2060 * - .rodata..c_jump_table: contains C annotated jump tables
2061 *
2062 * .rodata.str1.* sections are ignored; they don't contain jump tables.
Allan Xavier4a60aa02018-09-07 08:12:01 -05002063 */
2064 for_each_sec(file, sec) {
Muchun Song1ee444702020-04-12 22:44:05 +08002065 if (!strncmp(sec->name, ".rodata", 7) &&
2066 !strstr(sec->name, ".str1.")) {
Allan Xavier4a60aa02018-09-07 08:12:01 -05002067 sec->rodata = true;
2068 found = true;
2069 }
2070 }
2071
2072 file->rodata = found;
2073}
2074
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002075static int decode_sections(struct objtool_file *file)
2076{
2077 int ret;
2078
Allan Xavier4a60aa02018-09-07 08:12:01 -05002079 mark_rodata(file);
2080
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02002081 ret = init_pv_ops(file);
2082 if (ret)
2083 return ret;
2084
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002085 ret = decode_instructions(file);
2086 if (ret)
2087 return ret;
2088
2089 ret = add_dead_ends(file);
2090 if (ret)
2091 return ret;
2092
2093 add_ignores(file);
Peter Zijlstraea242132019-02-25 12:50:09 +01002094 add_uaccess_safe(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002095
Peter Zijlstraff05ab22019-03-18 14:33:07 +01002096 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00002097 if (ret)
2098 return ret;
2099
Peter Zijlstraa958c4f2021-03-26 16:12:05 +01002100 /*
2101 * Must be before add_{jump_call}_destination.
2102 */
Peter Zijlstra1739c662021-10-26 14:01:33 +02002103 ret = classify_symbols(file);
Peter Zijlstra5b06fd32020-08-18 15:57:49 +02002104 if (ret)
2105 return ret;
2106
Peter Zijlstra43d54302021-03-26 16:12:12 +01002107 /*
2108 * Must be before add_special_section_alts() as that depends on
2109 * jump_dest being set.
2110 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002111 ret = add_jump_destinations(file);
2112 if (ret)
2113 return ret;
2114
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002115 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002116 if (ret)
2117 return ret;
2118
Peter Zijlstraa958c4f2021-03-26 16:12:05 +01002119 /*
2120 * Must be before add_call_destination(); it changes INSN_CALL to
2121 * INSN_JUMP.
2122 */
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02002123 ret = read_intra_function_calls(file);
2124 if (ret)
2125 return ret;
2126
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002127 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002128 if (ret)
2129 return ret;
2130
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05002131 ret = add_jump_table_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002132 if (ret)
2133 return ret;
2134
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002135 ret = read_unwind_hints(file);
2136 if (ret)
2137 return ret;
2138
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002139 ret = read_retpoline_hints(file);
2140 if (ret)
2141 return ret;
2142
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002143 ret = read_instr_hints(file);
2144 if (ret)
2145 return ret;
2146
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002147 return 0;
2148}
2149
2150static bool is_fentry_call(struct instruction *insn)
2151{
Peter Zijlstra1739c662021-10-26 14:01:33 +02002152 if (insn->type == INSN_CALL &&
2153 insn->call_dest &&
2154 insn->call_dest->fentry)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002155 return true;
2156
2157 return false;
2158}
2159
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002160static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002161{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002162 struct cfi_state *cfi = &state->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002163 int i;
2164
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002165 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002166 return true;
2167
Josh Poimboeufb735bd32021-01-21 15:29:24 -06002168 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002169 return true;
2170
Josh Poimboeufb735bd32021-01-21 15:29:24 -06002171 if (cfi->stack_size != initial_func_cfi.cfa.offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002172 return true;
2173
2174 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002175 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2176 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002177 return true;
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002178 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002179
2180 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002181}
2182
Julien Thierryfb084fd2020-10-14 08:38:00 +01002183static bool check_reg_frame_pos(const struct cfi_reg *reg,
2184 int expected_offset)
2185{
2186 return reg->base == CFI_CFA &&
2187 reg->offset == expected_offset;
2188}
2189
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002190static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002191{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002192 struct cfi_state *cfi = &state->cfi;
2193
Julien Thierryfb084fd2020-10-14 08:38:00 +01002194 if (cfi->cfa.base == CFI_BP &&
2195 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2196 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002197 return true;
2198
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002199 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002200 return true;
2201
2202 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002203}
2204
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002205static int update_cfi_state_regs(struct instruction *insn,
2206 struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00002207 struct stack_op *op)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002208{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002209 struct cfi_reg *cfa = &cfi->cfa;
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002210
Josh Poimboeufd8dd25a2020-04-25 05:03:00 -05002211 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002212 return 0;
2213
2214 /* push */
Peter Zijlstraea242132019-02-25 12:50:09 +01002215 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002216 cfa->offset += 8;
2217
2218 /* pop */
Peter Zijlstraea242132019-02-25 12:50:09 +01002219 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002220 cfa->offset -= 8;
2221
2222 /* add immediate to sp */
2223 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2224 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2225 cfa->offset -= op->src.offset;
2226
2227 return 0;
2228}
2229
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002230static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002231{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002232 if (arch_callee_saved_reg(reg) &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002233 cfi->regs[reg].base == CFI_UNDEFINED) {
2234 cfi->regs[reg].base = base;
2235 cfi->regs[reg].offset = offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002236 }
2237}
2238
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002239static void restore_reg(struct cfi_state *cfi, unsigned char reg)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002240{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002241 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2242 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002243}
2244
2245/*
2246 * A note about DRAP stack alignment:
2247 *
2248 * GCC has the concept of a DRAP register, which is used to help keep track of
2249 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2250 * register. The typical DRAP pattern is:
2251 *
2252 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2253 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2254 * 41 ff 72 f8 pushq -0x8(%r10)
2255 * 55 push %rbp
2256 * 48 89 e5 mov %rsp,%rbp
2257 * (more pushes)
2258 * 41 52 push %r10
2259 * ...
2260 * 41 5a pop %r10
2261 * (more pops)
2262 * 5d pop %rbp
2263 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2264 * c3 retq
2265 *
2266 * There are some variations in the epilogues, like:
2267 *
2268 * 5b pop %rbx
2269 * 41 5a pop %r10
2270 * 41 5c pop %r12
2271 * 41 5d pop %r13
2272 * 41 5e pop %r14
2273 * c9 leaveq
2274 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2275 * c3 retq
2276 *
2277 * and:
2278 *
2279 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2280 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2281 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2282 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2283 * c9 leaveq
2284 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2285 * c3 retq
2286 *
2287 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2288 * restored beforehand:
2289 *
2290 * 41 55 push %r13
2291 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2292 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2293 * ...
2294 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2295 * 41 5d pop %r13
2296 * c3 retq
2297 */
Peter Zijlstrad54dba42021-02-11 13:03:28 +01002298static int update_cfi_state(struct instruction *insn,
2299 struct instruction *next_insn,
2300 struct cfi_state *cfi, struct stack_op *op)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002301{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002302 struct cfi_reg *cfa = &cfi->cfa;
2303 struct cfi_reg *regs = cfi->regs;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002304
2305 /* stack operations don't make sense with an undefined CFA */
2306 if (cfa->base == CFI_UNDEFINED) {
2307 if (insn->func) {
2308 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2309 return -1;
2310 }
2311 return 0;
2312 }
2313
Julien Thierryee819ae2020-09-04 16:30:27 +01002314 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2315 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002316 return update_cfi_state_regs(insn, cfi, op);
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002317
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002318 switch (op->dest.type) {
2319
2320 case OP_DEST_REG:
2321 switch (op->src.type) {
2322
2323 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05002324 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2325 cfa->base == CFI_SP &&
Julien Thierryfb084fd2020-10-14 08:38:00 +01002326 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002327
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05002328 /* mov %rsp, %rbp */
2329 cfa->base = op->dest.reg;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002330 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05002331 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002332
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05002333 else if (op->src.reg == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002334 op->dest.reg == CFI_BP && cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002335
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05002336 /* drap: mov %rsp, %rbp */
2337 regs[CFI_BP].base = CFI_BP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002338 regs[CFI_BP].offset = -cfi->stack_size;
2339 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05002340 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002341
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05002342 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2343
2344 /*
2345 * mov %rsp, %reg
2346 *
2347 * This is needed for the rare case where GCC
2348 * does:
2349 *
2350 * mov %rsp, %rax
2351 * ...
2352 * mov %rax, %rsp
2353 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002354 cfi->vals[op->dest.reg].base = CFI_CFA;
2355 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002356 }
2357
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05002358 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
Peter Zijlstraffc7e742021-02-09 21:41:13 +01002359 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05002360
2361 /*
2362 * mov %rbp, %rsp
2363 *
2364 * Restore the original stack pointer (Clang).
2365 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002366 cfi->stack_size = -cfi->regs[CFI_BP].offset;
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05002367 }
2368
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002369 else if (op->dest.reg == cfa->base) {
2370
2371 /* mov %reg, %rsp */
2372 if (cfa->base == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002373 cfi->vals[op->src.reg].base == CFI_CFA) {
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002374
2375 /*
2376 * This is needed for the rare case
2377 * where GCC does something dumb like:
2378 *
2379 * lea 0x8(%rsp), %rcx
2380 * ...
2381 * mov %rcx, %rsp
2382 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002383 cfa->offset = -cfi->vals[op->src.reg].offset;
2384 cfi->stack_size = cfa->offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002385
Peter Zijlstraaafeb142021-02-03 12:02:17 +01002386 } else if (cfa->base == CFI_SP &&
2387 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2388 cfi->vals[op->src.reg].offset == cfa->offset) {
2389
2390 /*
2391 * Stack swizzle:
2392 *
2393 * 1: mov %rsp, (%[tos])
2394 * 2: mov %[tos], %rsp
2395 * ...
2396 * 3: pop %rsp
2397 *
2398 * Where:
2399 *
2400 * 1 - places a pointer to the previous
2401 * stack at the Top-of-Stack of the
2402 * new stack.
2403 *
2404 * 2 - switches to the new stack.
2405 *
2406 * 3 - pops the Top-of-Stack to restore
2407 * the original stack.
2408 *
2409 * Note: we set base to SP_INDIRECT
2410 * here and preserve offset. Therefore
2411 * when the unwinder reaches ToS it
2412 * will dereference SP and then add the
2413 * offset to find the next frame, IOW:
2414 * (%rsp) + offset.
2415 */
2416 cfa->base = CFI_SP_INDIRECT;
2417
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002418 } else {
2419 cfa->base = CFI_UNDEFINED;
2420 cfa->offset = 0;
2421 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002422 }
2423
Peter Zijlstra724c8a22021-02-18 17:14:10 +01002424 else if (op->dest.reg == CFI_SP &&
2425 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2426 cfi->vals[op->src.reg].offset == cfa->offset) {
2427
2428 /*
2429 * The same stack swizzle case 2) as above. But
2430 * because we can't change cfa->base, case 3)
2431 * will become a regular POP. Pretend we're a
2432 * PUSH so things don't go unbalanced.
2433 */
2434 cfi->stack_size += 8;
2435 }
2436
2437
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002438 break;
2439
2440 case OP_SRC_ADD:
2441 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2442
2443 /* add imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002444 cfi->stack_size -= op->src.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002445 if (cfa->base == CFI_SP)
2446 cfa->offset -= op->src.offset;
2447 break;
2448 }
2449
2450 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2451
2452 /* lea disp(%rbp), %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002453 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002454 break;
2455 }
2456
Julien Thierry468af562020-10-14 08:38:01 +01002457 if (!cfi->drap && op->src.reg == CFI_SP &&
2458 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2459 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset + op->src.offset)) {
2460
2461 /* lea disp(%rsp), %rbp */
2462 cfa->base = CFI_BP;
2463 cfa->offset -= op->src.offset;
2464 cfi->bp_scratch = false;
2465 break;
2466 }
2467
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002468 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002469
2470 /* drap: lea disp(%rsp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002471 cfi->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002472
2473 /*
2474 * lea disp(%rsp), %reg
2475 *
2476 * This is needed for the rare case where GCC
2477 * does something dumb like:
2478 *
2479 * lea 0x8(%rsp), %rcx
2480 * ...
2481 * mov %rcx, %rsp
2482 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002483 cfi->vals[op->dest.reg].base = CFI_CFA;
2484 cfi->vals[op->dest.reg].offset = \
2485 -cfi->stack_size + op->src.offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002486
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002487 break;
2488 }
2489
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002490 if (cfi->drap && op->dest.reg == CFI_SP &&
2491 op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002492
2493 /* drap: lea disp(%drap), %rsp */
2494 cfa->base = CFI_SP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002495 cfa->offset = cfi->stack_size = -op->src.offset;
2496 cfi->drap_reg = CFI_UNDEFINED;
2497 cfi->drap = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002498 break;
2499 }
2500
Peter Zijlstrad54dba42021-02-11 13:03:28 +01002501 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002502 WARN_FUNC("unsupported stack register modification",
2503 insn->sec, insn->offset);
2504 return -1;
2505 }
2506
2507 break;
2508
2509 case OP_SRC_AND:
2510 if (op->dest.reg != CFI_SP ||
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002511 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2512 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002513 WARN_FUNC("unsupported stack pointer realignment",
2514 insn->sec, insn->offset);
2515 return -1;
2516 }
2517
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002518 if (cfi->drap_reg != CFI_UNDEFINED) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002519 /* drap: and imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002520 cfa->base = cfi->drap_reg;
2521 cfa->offset = cfi->stack_size = 0;
2522 cfi->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002523 }
2524
2525 /*
2526 * Older versions of GCC (4.8ish) realign the stack
2527 * without DRAP, with a frame pointer.
2528 */
2529
2530 break;
2531
2532 case OP_SRC_POP:
Peter Zijlstraea242132019-02-25 12:50:09 +01002533 case OP_SRC_POPF:
Peter Zijlstraaafeb142021-02-03 12:02:17 +01002534 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2535
2536 /* pop %rsp; # restore from a stack swizzle */
2537 cfa->base = CFI_SP;
2538 break;
2539 }
2540
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002541 if (!cfi->drap && op->dest.reg == cfa->base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002542
2543 /* pop %rbp */
2544 cfa->base = CFI_SP;
2545 }
2546
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002547 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2548 op->dest.reg == cfi->drap_reg &&
2549 cfi->drap_offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002550
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002551 /* drap: pop %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002552 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002553 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002554 cfi->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002555
Peter Zijlstraffc7e742021-02-09 21:41:13 +01002556 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002557
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002558 /* pop %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002559 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002560 }
2561
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002562 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002563 if (cfa->base == CFI_SP)
2564 cfa->offset -= 8;
2565
2566 break;
2567
2568 case OP_SRC_REG_INDIRECT:
Julien Thierry201ef5a2020-10-14 08:38:02 +01002569 if (!cfi->drap && op->dest.reg == cfa->base &&
2570 op->dest.reg == CFI_BP) {
2571
2572 /* mov disp(%rsp), %rbp */
2573 cfa->base = CFI_SP;
2574 cfa->offset = cfi->stack_size;
2575 }
2576
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002577 if (cfi->drap && op->src.reg == CFI_BP &&
2578 op->src.offset == cfi->drap_offset) {
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002579
2580 /* drap: mov disp(%rbp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002581 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002582 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002583 cfi->drap_offset = -1;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002584 }
2585
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002586 if (cfi->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002587 op->src.offset == regs[op->dest.reg].offset) {
2588
2589 /* drap: mov disp(%rbp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002590 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002591
2592 } else if (op->src.reg == cfa->base &&
2593 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2594
2595 /* mov disp(%rbp), %reg */
2596 /* mov disp(%rsp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002597 restore_reg(cfi, op->dest.reg);
Julien Thierry201ef5a2020-10-14 08:38:02 +01002598
2599 } else if (op->src.reg == CFI_SP &&
2600 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2601
2602 /* mov disp(%rsp), %reg */
2603 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002604 }
2605
2606 break;
2607
2608 default:
2609 WARN_FUNC("unknown stack-related instruction",
2610 insn->sec, insn->offset);
2611 return -1;
2612 }
2613
2614 break;
2615
2616 case OP_DEST_PUSH:
Peter Zijlstraea242132019-02-25 12:50:09 +01002617 case OP_DEST_PUSHF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002618 cfi->stack_size += 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002619 if (cfa->base == CFI_SP)
2620 cfa->offset += 8;
2621
2622 if (op->src.type != OP_SRC_REG)
2623 break;
2624
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002625 if (cfi->drap) {
2626 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002627
2628 /* drap: push %drap */
2629 cfa->base = CFI_BP_INDIRECT;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002630 cfa->offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002631
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002632 /* save drap so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002633 cfi->drap_offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002634
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002635 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002636
2637 /* drap: push %rbp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002638 cfi->stack_size = 0;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002639
Julien Thierryf4f80392020-09-15 08:53:16 +01002640 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002641
2642 /* drap: push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002643 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002644 }
2645
2646 } else {
2647
2648 /* push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002649 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002650 }
2651
2652 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002653 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002654 cfa->base != CFI_BP)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002655 cfi->bp_scratch = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002656 break;
2657
2658 case OP_DEST_REG_INDIRECT:
2659
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002660 if (cfi->drap) {
2661 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002662
2663 /* drap: mov %drap, disp(%rbp) */
2664 cfa->base = CFI_BP_INDIRECT;
2665 cfa->offset = op->dest.offset;
2666
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002667 /* save drap offset so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002668 cfi->drap_offset = op->dest.offset;
Julien Thierryf4f80392020-09-15 08:53:16 +01002669 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002670
2671 /* drap: mov reg, disp(%rbp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002672 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002673 }
2674
2675 } else if (op->dest.reg == cfa->base) {
2676
2677 /* mov reg, disp(%rbp) */
2678 /* mov reg, disp(%rsp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002679 save_reg(cfi, op->src.reg, CFI_CFA,
2680 op->dest.offset - cfi->cfa.offset);
Julien Thierry201ef5a2020-10-14 08:38:02 +01002681
2682 } else if (op->dest.reg == CFI_SP) {
2683
2684 /* mov reg, disp(%rsp) */
2685 save_reg(cfi, op->src.reg, CFI_CFA,
2686 op->dest.offset - cfi->stack_size);
Peter Zijlstraaafeb142021-02-03 12:02:17 +01002687
2688 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2689
2690 /* mov %rsp, (%reg); # setup a stack swizzle. */
2691 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2692 cfi->vals[op->dest.reg].offset = cfa->offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002693 }
2694
2695 break;
2696
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002697 case OP_DEST_MEM:
Peter Zijlstraea242132019-02-25 12:50:09 +01002698 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002699 WARN_FUNC("unknown stack-related memory operation",
2700 insn->sec, insn->offset);
2701 return -1;
2702 }
2703
2704 /* pop mem */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002705 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002706 if (cfa->base == CFI_SP)
2707 cfa->offset -= 8;
2708
2709 break;
2710
2711 default:
2712 WARN_FUNC("unknown stack-related instruction",
2713 insn->sec, insn->offset);
2714 return -1;
2715 }
2716
2717 return 0;
2718}
2719
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002720/*
2721 * The stack layouts of alternatives instructions can sometimes diverge when
2722 * they have stack modifications. That's fine as long as the potential stack
2723 * layouts don't conflict at any given potential instruction boundary.
2724 *
2725 * Flatten the CFIs of the different alternative code streams (both original
2726 * and replacement) into a single shared CFI array which can be used to detect
2727 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2728 */
2729static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2730{
2731 struct cfi_state **alt_cfi;
2732 int group_off;
2733
2734 if (!insn->alt_group)
2735 return 0;
2736
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02002737 if (!insn->cfi) {
2738 WARN("CFI missing");
2739 return -1;
2740 }
2741
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002742 alt_cfi = insn->alt_group->cfi;
2743 group_off = insn->offset - insn->alt_group->first_insn->offset;
2744
2745 if (!alt_cfi[group_off]) {
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02002746 alt_cfi[group_off] = insn->cfi;
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002747 } else {
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02002748 if (cficmp(alt_cfi[group_off], insn->cfi)) {
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002749 WARN_FUNC("stack layout conflict in alternatives",
2750 insn->sec, insn->offset);
2751 return -1;
2752 }
2753 }
2754
2755 return 0;
2756}
2757
Peter Zijlstrad54dba42021-02-11 13:03:28 +01002758static int handle_insn_ops(struct instruction *insn,
2759 struct instruction *next_insn,
2760 struct insn_state *state)
Julien Thierry65ea47d2020-03-27 15:28:47 +00002761{
2762 struct stack_op *op;
2763
2764 list_for_each_entry(op, &insn->stack_ops, list) {
Julien Thierry65ea47d2020-03-27 15:28:47 +00002765
Peter Zijlstrad54dba42021-02-11 13:03:28 +01002766 if (update_cfi_state(insn, next_insn, &state->cfi, op))
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06002767 return 1;
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002768
Peter Zijlstraba08abc2021-03-08 15:46:04 +01002769 if (!insn->alt_group)
2770 continue;
2771
Julien Thierry65ea47d2020-03-27 15:28:47 +00002772 if (op->dest.type == OP_DEST_PUSHF) {
2773 if (!state->uaccess_stack) {
2774 state->uaccess_stack = 1;
2775 } else if (state->uaccess_stack >> 31) {
2776 WARN_FUNC("PUSHF stack exhausted",
2777 insn->sec, insn->offset);
2778 return 1;
2779 }
2780 state->uaccess_stack <<= 1;
2781 state->uaccess_stack |= state->uaccess;
2782 }
2783
2784 if (op->src.type == OP_SRC_POPF) {
2785 if (state->uaccess_stack) {
2786 state->uaccess = state->uaccess_stack & 1;
2787 state->uaccess_stack >>= 1;
2788 if (state->uaccess_stack == 1)
2789 state->uaccess_stack = 0;
2790 }
2791 }
2792 }
2793
2794 return 0;
2795}
2796
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002797static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002798{
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02002799 struct cfi_state *cfi1 = insn->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002800 int i;
2801
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02002802 if (!cfi1) {
2803 WARN("CFI missing");
2804 return false;
2805 }
2806
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002807 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2808
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002809 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2810 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002811 cfi1->cfa.base, cfi1->cfa.offset,
2812 cfi2->cfa.base, cfi2->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002813
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002814 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002815 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002816 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002817 sizeof(struct cfi_reg)))
2818 continue;
2819
2820 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2821 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002822 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2823 i, cfi2->regs[i].base, cfi2->regs[i].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002824 break;
2825 }
2826
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002827 } else if (cfi1->type != cfi2->type) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002828
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002829 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2830 insn->sec, insn->offset, cfi1->type, cfi2->type);
2831
2832 } else if (cfi1->drap != cfi2->drap ||
2833 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2834 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2835
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002836 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002837 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002838 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2839 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002840
2841 } else
2842 return true;
2843
2844 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002845}
2846
Peter Zijlstraea242132019-02-25 12:50:09 +01002847static inline bool func_uaccess_safe(struct symbol *func)
2848{
2849 if (func)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002850 return func->uaccess_safe;
Peter Zijlstraea242132019-02-25 12:50:09 +01002851
2852 return false;
2853}
2854
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002855static inline const char *call_dest_name(struct instruction *insn)
Peter Zijlstraea242132019-02-25 12:50:09 +01002856{
Sergei Trofimovich82880282022-01-20 23:37:48 +00002857 static char pvname[19];
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02002858 struct reloc *rel;
2859 int idx;
2860
Peter Zijlstraea242132019-02-25 12:50:09 +01002861 if (insn->call_dest)
2862 return insn->call_dest->name;
2863
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02002864 rel = insn_reloc(NULL, insn);
2865 if (rel && !strcmp(rel->sym->name, "pv_ops")) {
2866 idx = (rel->addend / sizeof(void *));
2867 snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
2868 return pvname;
2869 }
2870
Peter Zijlstraea242132019-02-25 12:50:09 +01002871 return "{dynamic}";
2872}
2873
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02002874static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
2875{
2876 struct symbol *target;
2877 struct reloc *rel;
2878 int idx;
2879
2880 rel = insn_reloc(file, insn);
2881 if (!rel || strcmp(rel->sym->name, "pv_ops"))
2882 return false;
2883
2884 idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *));
2885
2886 if (file->pv_ops[idx].clean)
2887 return true;
2888
2889 file->pv_ops[idx].clean = true;
2890
2891 list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) {
2892 if (!target->sec->noinstr) {
2893 WARN("pv_ops[%d]: %s", idx, target->name);
2894 file->pv_ops[idx].clean = false;
2895 }
2896 }
2897
2898 return file->pv_ops[idx].clean;
2899}
2900
2901static inline bool noinstr_call_dest(struct objtool_file *file,
2902 struct instruction *insn,
2903 struct symbol *func)
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002904{
2905 /*
2906 * We can't deal with indirect function calls at present;
2907 * assume they're instrumented.
2908 */
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02002909 if (!func) {
2910 if (file->pv_ops)
2911 return pv_call_dest(file, insn);
2912
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002913 return false;
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02002914 }
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002915
2916 /*
2917 * If the symbol is from a noinstr section; we good.
2918 */
2919 if (func->sec->noinstr)
2920 return true;
2921
2922 /*
2923 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2924 * something 'BAD' happened. At the risk of taking the machine down,
2925 * let them proceed to get the message out.
2926 */
2927 if (!strncmp(func->name, "__ubsan_handle_", 15))
2928 return true;
2929
2930 return false;
2931}
2932
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02002933static int validate_call(struct objtool_file *file,
2934 struct instruction *insn,
2935 struct insn_state *state)
Peter Zijlstraea242132019-02-25 12:50:09 +01002936{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002937 if (state->noinstr && state->instr <= 0 &&
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02002938 !noinstr_call_dest(file, insn, insn->call_dest)) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002939 WARN_FUNC("call to %s() leaves .noinstr.text section",
2940 insn->sec, insn->offset, call_dest_name(insn));
2941 return 1;
2942 }
2943
Peter Zijlstraea242132019-02-25 12:50:09 +01002944 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2945 WARN_FUNC("call to %s() with UACCESS enabled",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002946 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstraea242132019-02-25 12:50:09 +01002947 return 1;
2948 }
2949
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002950 if (state->df) {
2951 WARN_FUNC("call to %s() with DF set",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002952 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002953 return 1;
2954 }
2955
Peter Zijlstraea242132019-02-25 12:50:09 +01002956 return 0;
2957}
2958
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02002959static int validate_sibling_call(struct objtool_file *file,
2960 struct instruction *insn,
2961 struct insn_state *state)
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002962{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002963 if (has_modified_stack_frame(insn, state)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002964 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2965 insn->sec, insn->offset);
2966 return 1;
2967 }
2968
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02002969 return validate_call(file, insn, state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002970}
2971
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002972static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2973{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002974 if (state->noinstr && state->instr > 0) {
2975 WARN_FUNC("return with instrumentation enabled",
2976 insn->sec, insn->offset);
2977 return 1;
2978 }
2979
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002980 if (state->uaccess && !func_uaccess_safe(func)) {
2981 WARN_FUNC("return with UACCESS enabled",
2982 insn->sec, insn->offset);
2983 return 1;
2984 }
2985
2986 if (!state->uaccess && func_uaccess_safe(func)) {
2987 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2988 insn->sec, insn->offset);
2989 return 1;
2990 }
2991
2992 if (state->df) {
2993 WARN_FUNC("return with DF set",
2994 insn->sec, insn->offset);
2995 return 1;
2996 }
2997
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002998 if (func && has_modified_stack_frame(insn, state)) {
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002999 WARN_FUNC("return with modified stack frame",
3000 insn->sec, insn->offset);
3001 return 1;
3002 }
3003
Peter Zijlstrae7c02192020-03-25 14:04:45 +01003004 if (state->cfi.bp_scratch) {
Josh Poimboeufb2966952020-04-01 13:23:29 -05003005 WARN_FUNC("BP used as a scratch register",
3006 insn->sec, insn->offset);
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01003007 return 1;
3008 }
3009
3010 return 0;
3011}
3012
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06003013static struct instruction *next_insn_to_validate(struct objtool_file *file,
3014 struct instruction *insn)
Peter Zijlstra7117f162020-04-28 19:37:01 +02003015{
Josh Poimboeufb23cc712020-12-18 14:19:32 -06003016 struct alt_group *alt_group = insn->alt_group;
Peter Zijlstra7117f162020-04-28 19:37:01 +02003017
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06003018 /*
3019 * Simulate the fact that alternatives are patched in-place. When the
3020 * end of a replacement alt_group is reached, redirect objtool flow to
3021 * the end of the original alt_group.
3022 */
3023 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
3024 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
3025
3026 return next_insn_same_sec(file, insn);
Peter Zijlstra7117f162020-04-28 19:37:01 +02003027}
3028
3029/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003030 * Follow the branch starting at the given instruction, and recursively follow
3031 * any other branches (jumps). Meanwhile, track the frame pointer state at
3032 * each instruction and validate all the rules described in
3033 * tools/objtool/Documentation/stack-validation.txt.
3034 */
Josh Poimboeufc705cec2019-07-17 20:36:47 -05003035static int validate_branch(struct objtool_file *file, struct symbol *func,
Peter Zijlstrab7460462020-04-02 10:15:51 +02003036 struct instruction *insn, struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003037{
3038 struct alternative *alt;
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02003039 struct instruction *next_insn, *prev_insn = NULL;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003040 struct section *sec;
Peter Zijlstra882a0db2019-07-24 17:47:26 -05003041 u8 visited;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003042 int ret;
3043
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003044 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003045
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003046 while (1) {
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06003047 next_insn = next_insn_to_validate(file, insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05003048
Josh Poimboeuf13810432018-05-09 22:39:15 -05003049 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05003050 WARN("%s() falls through to next function %s()",
3051 func->name, insn->func->name);
3052 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003053 }
3054
Josh Poimboeuf48550222017-07-07 09:19:42 -05003055 if (func && insn->ignore) {
3056 WARN_FUNC("BUG: why am I validating an ignored function?",
3057 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05003058 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05003059 }
3060
Peter Zijlstra882a0db2019-07-24 17:47:26 -05003061 visited = 1 << state.uaccess;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003062 if (insn->visited) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01003063 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003064 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003065
Peter Zijlstra882a0db2019-07-24 17:47:26 -05003066 if (insn->visited & visited)
Peter Zijlstraea242132019-02-25 12:50:09 +01003067 return 0;
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02003068 } else {
3069 nr_insns_visited++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003070 }
3071
Peter Zijlstrac4a33932020-03-10 18:57:41 +01003072 if (state.noinstr)
3073 state.instr += insn->instr;
3074
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02003075 if (insn->hint) {
3076 state.cfi = *insn->cfi;
3077 } else {
3078 /* XXX track if we actually changed state.cfi */
3079
3080 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
3081 insn->cfi = prev_insn->cfi;
3082 nr_cfi_reused++;
3083 } else {
3084 insn->cfi = cfi_hash_find_or_add(&state.cfi);
3085 }
3086 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003087
Peter Zijlstra882a0db2019-07-24 17:47:26 -05003088 insn->visited |= visited;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003089
Josh Poimboeufc9c324d2020-12-18 14:26:21 -06003090 if (propagate_alt_cfi(file, insn))
3091 return 1;
3092
Peter Zijlstra7117f162020-04-28 19:37:01 +02003093 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01003094 bool skip_orig = false;
3095
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06003096 list_for_each_entry(alt, &insn->alts, list) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01003097 if (alt->skip_orig)
3098 skip_orig = true;
3099
Josh Poimboeufc705cec2019-07-17 20:36:47 -05003100 ret = validate_branch(file, func, alt->insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01003101 if (ret) {
3102 if (backtrace)
3103 BT_FUNC("(alt)", insn);
3104 return ret;
3105 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06003106 }
Peter Zijlstra764eef42019-03-01 11:19:03 +01003107
3108 if (skip_orig)
3109 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003110 }
3111
Peter Zijlstrad54dba42021-02-11 13:03:28 +01003112 if (handle_insn_ops(insn, next_insn, &state))
Peter Zijlstra60041bc2020-04-24 16:16:41 +02003113 return 1;
3114
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003115 switch (insn->type) {
3116
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003117 case INSN_RETURN:
Peter Zijlstra1cc1e4c2021-12-04 14:43:42 +01003118 if (next_insn && next_insn->type == INSN_TRAP) {
3119 next_insn->ignore = true;
3120 } else if (sls && !insn->retpoline_safe) {
3121 WARN_FUNC("missing int3 after ret",
3122 insn->sec, insn->offset);
3123 }
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01003124 return validate_return(func, insn, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003125
3126 case INSN_CALL:
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003127 case INSN_CALL_DYNAMIC:
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02003128 ret = validate_call(file, insn, &state);
Peter Zijlstraea242132019-02-25 12:50:09 +01003129 if (ret)
3130 return ret;
3131
Josh Poimboeufc9bab222019-07-17 20:36:51 -05003132 if (!no_fp && func && !is_fentry_call(insn) &&
3133 !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003134 WARN_FUNC("call without frame pointer save/setup",
3135 sec, insn->offset);
3136 return 1;
3137 }
Josh Poimboeufc9bab222019-07-17 20:36:51 -05003138
3139 if (dead_end_function(file, insn->call_dest))
3140 return 0;
3141
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003142 break;
3143
3144 case INSN_JUMP_CONDITIONAL:
3145 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeufecf11ba2021-01-21 15:29:22 -06003146 if (is_sibling_call(insn)) {
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02003147 ret = validate_sibling_call(file, insn, &state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01003148 if (ret)
3149 return ret;
3150
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05003151 } else if (insn->jump_dest) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05003152 ret = validate_branch(file, func,
3153 insn->jump_dest, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01003154 if (ret) {
3155 if (backtrace)
3156 BT_FUNC("(branch)", insn);
3157 return ret;
3158 }
Josh Poimboeuf48550222017-07-07 09:19:42 -05003159 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003160
3161 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3162 return 0;
3163
3164 break;
3165
3166 case INSN_JUMP_DYNAMIC:
Peter Zijlstra1cc1e4c2021-12-04 14:43:42 +01003167 if (next_insn && next_insn->type == INSN_TRAP) {
3168 next_insn->ignore = true;
3169 } else if (sls && !insn->retpoline_safe) {
3170 WARN_FUNC("missing int3 after indirect jump",
3171 insn->sec, insn->offset);
3172 }
3173
3174 /* fallthrough */
Josh Poimboeufb68b9902019-07-17 20:36:57 -05003175 case INSN_JUMP_DYNAMIC_CONDITIONAL:
Josh Poimboeufecf11ba2021-01-21 15:29:22 -06003176 if (is_sibling_call(insn)) {
Peter Zijlstradb2b0c52021-06-24 11:41:23 +02003177 ret = validate_sibling_call(file, insn, &state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01003178 if (ret)
3179 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003180 }
3181
Josh Poimboeufb68b9902019-07-17 20:36:57 -05003182 if (insn->type == INSN_JUMP_DYNAMIC)
3183 return 0;
3184
3185 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003186
Josh Poimboeuf39358a02017-07-11 10:33:43 -05003187 case INSN_CONTEXT_SWITCH:
3188 if (func && (!next_insn || !next_insn->hint)) {
3189 WARN_FUNC("unsupported instruction in callable function",
3190 sec, insn->offset);
3191 return 1;
3192 }
3193 return 0;
3194
Peter Zijlstraea242132019-02-25 12:50:09 +01003195 case INSN_STAC:
3196 if (state.uaccess) {
3197 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
3198 return 1;
3199 }
3200
3201 state.uaccess = true;
3202 break;
3203
3204 case INSN_CLAC:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05003205 if (!state.uaccess && func) {
Peter Zijlstraea242132019-02-25 12:50:09 +01003206 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
3207 return 1;
3208 }
3209
3210 if (func_uaccess_safe(func) && !state.uaccess_stack) {
3211 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
3212 return 1;
3213 }
3214
3215 state.uaccess = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003216 break;
3217
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01003218 case INSN_STD:
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06003219 if (state.df) {
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01003220 WARN_FUNC("recursive STD", sec, insn->offset);
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06003221 return 1;
3222 }
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01003223
3224 state.df = true;
3225 break;
3226
3227 case INSN_CLD:
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06003228 if (!state.df && func) {
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01003229 WARN_FUNC("redundant CLD", sec, insn->offset);
Josh Poimboeuf6f567c92021-01-21 15:29:17 -06003230 return 1;
3231 }
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01003232
3233 state.df = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003234 break;
3235
3236 default:
3237 break;
3238 }
3239
3240 if (insn->dead_end)
3241 return 0;
3242
Josh Poimboeuf00d96182017-09-18 21:43:30 -05003243 if (!next_insn) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01003244 if (state.cfi.cfa.base == CFI_UNDEFINED)
Josh Poimboeuf00d96182017-09-18 21:43:30 -05003245 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003246 WARN("%s: unexpected end of section", sec->name);
3247 return 1;
3248 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05003249
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02003250 prev_insn = insn;
Josh Poimboeuf00d96182017-09-18 21:43:30 -05003251 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003252 }
3253
3254 return 0;
3255}
3256
Peter Zijlstra932f8e92020-03-23 18:26:03 +01003257static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
Josh Poimboeuf39358a02017-07-11 10:33:43 -05003258{
3259 struct instruction *insn;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05003260 struct insn_state state;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01003261 int ret, warnings = 0;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05003262
3263 if (!file->hints)
3264 return 0;
3265
Peter Zijlstra932f8e92020-03-23 18:26:03 +01003266 init_insn_state(&state, sec);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05003267
Peter Zijlstra932f8e92020-03-23 18:26:03 +01003268 if (sec) {
3269 insn = find_insn(file, sec, 0);
3270 if (!insn)
3271 return 0;
3272 } else {
3273 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
3274 }
3275
3276 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
Josh Poimboeuf5b284b12021-09-14 23:41:23 +09003277 if (insn->hint && !insn->visited && !insn->ignore) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05003278 ret = validate_branch(file, insn->func, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01003279 if (ret && backtrace)
3280 BT_FUNC("<=== (hint)", insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05003281 warnings += ret;
3282 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01003283
3284 insn = list_next_entry(insn, list);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05003285 }
3286
3287 return warnings;
3288}
3289
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01003290static int validate_retpoline(struct objtool_file *file)
3291{
3292 struct instruction *insn;
3293 int warnings = 0;
3294
3295 for_each_insn(file, insn) {
3296 if (insn->type != INSN_JUMP_DYNAMIC &&
3297 insn->type != INSN_CALL_DYNAMIC)
3298 continue;
3299
3300 if (insn->retpoline_safe)
3301 continue;
3302
Peter Zijlstraca41b972018-01-31 10:18:28 +01003303 /*
3304 * .init.text code is ran before userspace and thus doesn't
3305 * strictly need retpolines, except for modules which are
3306 * loaded late, they very much do need retpoline in their
3307 * .init.text
3308 */
3309 if (!strcmp(insn->sec->name, ".init.text") && !module)
3310 continue;
3311
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01003312 WARN_FUNC("indirect %s found in RETPOLINE build",
3313 insn->sec, insn->offset,
3314 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3315
3316 warnings++;
3317 }
3318
3319 return warnings;
3320}
3321
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003322static bool is_kasan_insn(struct instruction *insn)
3323{
3324 return (insn->type == INSN_CALL &&
3325 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3326}
3327
3328static bool is_ubsan_insn(struct instruction *insn)
3329{
3330 return (insn->type == INSN_CALL &&
3331 !strcmp(insn->call_dest->name,
3332 "__ubsan_handle_builtin_unreachable"));
3333}
3334
Ilie Halip14db1f02020-09-19 09:41:18 +03003335static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003336{
3337 int i;
Ilie Halip14db1f02020-09-19 09:41:18 +03003338 struct instruction *prev_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003339
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003340 if (insn->ignore || insn->type == INSN_NOP)
3341 return true;
3342
3343 /*
Peter Zijlstra82a89542021-11-10 11:01:25 +01003344 * Ignore alternative replacement instructions. This can happen
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05003345 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003346 */
Peter Zijlstra82a89542021-11-10 11:01:25 +01003347 if (!strcmp(insn->sec->name, ".altinstr_replacement") ||
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05003348 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003349 return true;
3350
Josh Poimboeufbd841d62020-04-01 13:23:25 -05003351 if (!insn->func)
3352 return false;
3353
Peter Zijlstra2105a922021-10-30 09:47:58 +02003354 if (insn->func->static_call_tramp)
3355 return true;
3356
Josh Poimboeufbd841d62020-04-01 13:23:25 -05003357 /*
3358 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3359 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3360 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3361 * (or occasionally a JMP to UD2).
Ilie Halip14db1f02020-09-19 09:41:18 +03003362 *
3363 * It may also insert a UD2 after calling a __noreturn function.
Josh Poimboeufbd841d62020-04-01 13:23:25 -05003364 */
Ilie Halip14db1f02020-09-19 09:41:18 +03003365 prev_insn = list_prev_entry(insn, list);
3366 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
Josh Poimboeufbd841d62020-04-01 13:23:25 -05003367 (insn->type == INSN_BUG ||
3368 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3369 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3370 return true;
3371
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003372 /*
3373 * Check if this (or a subsequent) instruction is related to
3374 * CONFIG_UBSAN or CONFIG_KASAN.
3375 *
3376 * End the search at 5 instructions to avoid going into the weeds.
3377 */
3378 for (i = 0; i < 5; i++) {
3379
3380 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3381 return true;
3382
Josh Poimboeuffe24e272018-02-08 17:09:25 -06003383 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3384 if (insn->jump_dest &&
3385 insn->jump_dest->func == insn->func) {
3386 insn = insn->jump_dest;
3387 continue;
3388 }
3389
3390 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003391 }
3392
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003393 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003394 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06003395
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003396 insn = list_next_entry(insn, list);
3397 }
3398
3399 return false;
3400}
3401
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01003402static int validate_symbol(struct objtool_file *file, struct section *sec,
3403 struct symbol *sym, struct insn_state *state)
3404{
3405 struct instruction *insn;
3406 int ret;
3407
3408 if (!sym->len) {
3409 WARN("%s() is missing an ELF size annotation", sym->name);
3410 return 1;
3411 }
3412
3413 if (sym->pfunc != sym || sym->alias != sym)
3414 return 0;
3415
3416 insn = find_insn(file, sec, sym->offset);
3417 if (!insn || insn->ignore || insn->visited)
3418 return 0;
3419
3420 state->uaccess = sym->uaccess_safe;
3421
3422 ret = validate_branch(file, insn->func, insn, *state);
3423 if (ret && backtrace)
3424 BT_FUNC("<=== (sym)", insn);
3425 return ret;
3426}
3427
Peter Zijlstra350994b2020-03-23 20:57:13 +01003428static int validate_section(struct objtool_file *file, struct section *sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003429{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003430 struct insn_state state;
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01003431 struct symbol *func;
3432 int warnings = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003433
Peter Zijlstra350994b2020-03-23 20:57:13 +01003434 list_for_each_entry(func, &sec->symbol_list, list) {
3435 if (func->type != STT_FUNC)
3436 continue;
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05003437
Peter Zijlstra932f8e92020-03-23 18:26:03 +01003438 init_insn_state(&state, sec);
Josh Poimboeufb735bd32021-01-21 15:29:24 -06003439 set_func_state(&state.cfi);
Julien Thierry0699e552020-03-27 15:28:40 +00003440
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01003441 warnings += validate_symbol(file, sec, func, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003442 }
3443
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003444 return warnings;
3445}
3446
Peter Zijlstrac4a33932020-03-10 18:57:41 +01003447static int validate_vmlinux_functions(struct objtool_file *file)
3448{
3449 struct section *sec;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01003450 int warnings = 0;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01003451
3452 sec = find_section_by_name(file->elf, ".noinstr.text");
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01003453 if (sec) {
3454 warnings += validate_section(file, sec);
3455 warnings += validate_unwind_hints(file, sec);
3456 }
Peter Zijlstrac4a33932020-03-10 18:57:41 +01003457
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01003458 sec = find_section_by_name(file->elf, ".entry.text");
3459 if (sec) {
3460 warnings += validate_section(file, sec);
3461 warnings += validate_unwind_hints(file, sec);
3462 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01003463
3464 return warnings;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01003465}
3466
Peter Zijlstra350994b2020-03-23 20:57:13 +01003467static int validate_functions(struct objtool_file *file)
3468{
3469 struct section *sec;
3470 int warnings = 0;
3471
Peter Zijlstrada837bd2020-03-23 21:11:14 +01003472 for_each_sec(file, sec) {
3473 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3474 continue;
3475
Peter Zijlstra350994b2020-03-23 20:57:13 +01003476 warnings += validate_section(file, sec);
Peter Zijlstrada837bd2020-03-23 21:11:14 +01003477 }
Peter Zijlstra350994b2020-03-23 20:57:13 +01003478
3479 return warnings;
3480}
3481
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003482static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003483{
3484 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003485
3486 if (file->ignore_unreachables)
3487 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003488
3489 for_each_insn(file, insn) {
Ilie Halip14db1f02020-09-19 09:41:18 +03003490 if (insn->visited || ignore_unreachable_insn(file, insn))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003491 continue;
3492
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003493 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3494 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003495 }
3496
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003497 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003498}
3499
Julien Thierryd44becb2020-08-25 13:47:40 +01003500int check(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003501{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003502 int ret, warnings = 0;
3503
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003504 arch_initial_func_cfi_state(&initial_func_cfi);
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02003505 init_cfi_state(&init_cfi);
3506 init_cfi_state(&func_cfi);
3507 set_func_state(&func_cfi);
3508
3509 if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
3510 goto out;
3511
3512 cfi_hash_add(&init_cfi);
3513 cfi_hash_add(&func_cfi);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003514
Julien Thierry6545eb02020-08-25 13:47:39 +01003515 ret = decode_sections(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003516 if (ret < 0)
3517 goto out;
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02003518
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003519 warnings += ret;
3520
Julien Thierry6545eb02020-08-25 13:47:39 +01003521 if (list_empty(&file->insn_list))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003522 goto out;
3523
Peter Zijlstrac4a33932020-03-10 18:57:41 +01003524 if (vmlinux && !validate_dup) {
Julien Thierry6545eb02020-08-25 13:47:39 +01003525 ret = validate_vmlinux_functions(file);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01003526 if (ret < 0)
3527 goto out;
3528
3529 warnings += ret;
3530 goto out;
3531 }
3532
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01003533 if (retpoline) {
Julien Thierry6545eb02020-08-25 13:47:39 +01003534 ret = validate_retpoline(file);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01003535 if (ret < 0)
3536 return ret;
3537 warnings += ret;
3538 }
3539
Julien Thierry6545eb02020-08-25 13:47:39 +01003540 ret = validate_functions(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003541 if (ret < 0)
3542 goto out;
3543 warnings += ret;
3544
Julien Thierry6545eb02020-08-25 13:47:39 +01003545 ret = validate_unwind_hints(file, NULL);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05003546 if (ret < 0)
3547 goto out;
3548 warnings += ret;
3549
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003550 if (!warnings) {
Julien Thierry6545eb02020-08-25 13:47:39 +01003551 ret = validate_reachable_instructions(file);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05003552 if (ret < 0)
3553 goto out;
3554 warnings += ret;
3555 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003556
Julien Thierry6545eb02020-08-25 13:47:39 +01003557 ret = create_static_call_sections(file);
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02003558 if (ret < 0)
3559 goto out;
3560 warnings += ret;
3561
Peter Zijlstra134ab5b2021-10-26 14:01:36 +02003562 if (retpoline) {
3563 ret = create_retpoline_sites_sections(file);
3564 if (ret < 0)
3565 goto out;
3566 warnings += ret;
3567 }
3568
Peter Zijlstra99d00212020-08-06 15:14:09 -07003569 if (mcount) {
3570 ret = create_mcount_loc_sections(file);
3571 if (ret < 0)
3572 goto out;
3573 warnings += ret;
3574 }
3575
Peter Zijlstra8b946cc2021-06-24 11:41:01 +02003576 if (stats) {
3577 printf("nr_insns_visited: %ld\n", nr_insns_visited);
3578 printf("nr_cfi: %ld\n", nr_cfi);
3579 printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
3580 printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
3581 }
3582
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003583out:
Josh Poimboeuf655cf862021-01-14 16:32:42 -06003584 /*
3585 * For now, don't fail the kernel build on fatal warnings. These
3586 * errors are still fairly common due to the growing matrix of
3587 * supported toolchains and their recent pace of change.
3588 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003589 return 0;
3590}