blob: 8932f41c387ff71f6bc21269f5140509d90a8465 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002/*
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05004 */
5
6#include <string.h>
7#include <stdlib.h>
8
Peter Zijlstra43a45252018-01-16 17:16:32 +01009#include "builtin.h"
Matt Helsley0decf1f2020-05-19 13:55:33 -070010#include "cfi.h"
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050011#include "arch.h"
Matt Helsley0decf1f2020-05-19 13:55:33 -070012#include "check.h"
13#include "special.h"
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050014#include "warn.h"
Peter Zijlstra0f1441b2020-06-12 16:05:26 +020015#include "arch_elf.h"
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050016
Julien Thierryee819ae2020-09-04 16:30:27 +010017#include <linux/objtool.h>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050018#include <linux/hashtable.h>
19#include <linux/kernel.h>
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +020020#include <linux/static_call_types.h>
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050021
Josh Poimboeufe6da9562019-05-13 12:01:31 -050022#define FAKE_JUMP_OFFSET -1
23
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050024struct alternative {
25 struct list_head list;
26 struct instruction *insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +010027 bool skip_orig;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050028};
29
Peter Zijlstraa3608f52020-03-25 15:34:50 +010030struct cfi_init_state initial_func_cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050031
Josh Poimboeuf627fce12017-07-11 10:33:42 -050032struct instruction *find_insn(struct objtool_file *file,
33 struct section *sec, unsigned long offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050034{
35 struct instruction *insn;
36
Peter Zijlstra87ecb582020-03-16 15:47:27 +010037 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050038 if (insn->sec == sec && insn->offset == offset)
39 return insn;
Peter Zijlstra87ecb582020-03-16 15:47:27 +010040 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050041
42 return NULL;
43}
44
45static struct instruction *next_insn_same_sec(struct objtool_file *file,
46 struct instruction *insn)
47{
48 struct instruction *next = list_next_entry(insn, list);
49
Josh Poimboeufbaa41462017-06-28 10:11:07 -050050 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050051 return NULL;
52
53 return next;
54}
55
Josh Poimboeuf13810432018-05-09 22:39:15 -050056static struct instruction *next_insn_same_func(struct objtool_file *file,
57 struct instruction *insn)
58{
59 struct instruction *next = list_next_entry(insn, list);
60 struct symbol *func = insn->func;
61
62 if (!func)
63 return NULL;
64
65 if (&next->list != &file->insn_list && next->func == func)
66 return next;
67
68 /* Check if we're already in the subfunction: */
69 if (func == func->cfunc)
70 return NULL;
71
72 /* Move to the subfunction: */
73 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
74}
75
Josh Poimboeuf1119d262020-04-28 16:45:16 -050076static struct instruction *prev_insn_same_sym(struct objtool_file *file,
77 struct instruction *insn)
78{
79 struct instruction *prev = list_prev_entry(insn, list);
80
81 if (&prev->list != &file->insn_list && prev->func == insn->func)
82 return prev;
83
84 return NULL;
85}
86
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +010087#define func_for_each_insn(file, func, insn) \
Josh Poimboeuf13810432018-05-09 22:39:15 -050088 for (insn = find_insn(file, func->sec, func->offset); \
89 insn; \
90 insn = next_insn_same_func(file, insn))
91
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +010092#define sym_for_each_insn(file, sym, insn) \
93 for (insn = find_insn(file, sym->sec, sym->offset); \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050094 insn && &insn->list != &file->insn_list && \
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +010095 insn->sec == sym->sec && \
96 insn->offset < sym->offset + sym->len; \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050097 insn = list_next_entry(insn, list))
98
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +010099#define sym_for_each_insn_continue_reverse(file, sym, insn) \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500100 for (insn = list_prev_entry(insn, list); \
101 &insn->list != &file->insn_list && \
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100102 insn->sec == sym->sec && insn->offset >= sym->offset; \
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500103 insn = list_prev_entry(insn, list))
104
105#define sec_for_each_insn_from(file, insn) \
106 for (; insn; insn = next_insn_same_sec(file, insn))
107
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500108#define sec_for_each_insn_continue(file, insn) \
109 for (insn = next_insn_same_sec(file, insn); insn; \
110 insn = next_insn_same_sec(file, insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500111
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500112static bool is_sibling_call(struct instruction *insn)
113{
114 /* An indirect jump is either a sibling call or a jump to a table. */
115 if (insn->type == INSN_JUMP_DYNAMIC)
116 return list_empty(&insn->alts);
117
Josh Poimboeufa2296142020-02-10 12:32:39 -0600118 if (!is_static_jump(insn))
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500119 return false;
120
121 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
122 return !!insn->call_dest;
123}
124
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500125/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500126 * This checks to see if the given function is a "noreturn" function.
127 *
128 * For global functions which are outside the scope of this object file, we
129 * have to keep a manual list of them.
130 *
131 * For local functions, we have to detect them manually by simply looking for
132 * the lack of a return instruction.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500133 */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500134static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
135 int recursion)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500136{
137 int i;
138 struct instruction *insn;
139 bool empty = true;
140
141 /*
142 * Unfortunately these have to be hard coded because the noreturn
143 * attribute isn't provided in ELF data.
144 */
145 static const char * const global_noreturns[] = {
146 "__stack_chk_fail",
147 "panic",
148 "do_exit",
149 "do_task_dead",
150 "__module_put_and_exit",
151 "complete_and_exit",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500152 "__reiserfs_panic",
153 "lbug_with_loc",
154 "fortify_panic",
Kees Cookb394d462018-01-10 14:22:38 -0800155 "usercopy_abort",
Josh Poimboeuf684fb242018-06-19 10:47:50 -0500156 "machine_real_restart",
Josh Poimboeuf4fa5ecd2019-04-04 12:17:35 -0500157 "rewind_stack_do_exit",
Brendan Higgins33adf802019-09-23 02:02:38 -0700158 "kunit_try_catch_throw",
Josh Poimboeufabf37e82021-01-21 15:29:25 -0600159 "xen_start_kernel",
Peter Zijlstrab36ab502021-06-24 11:41:00 +0200160 "cpu_bringup_and_idle",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500161 };
162
Josh Poimboeufc9bab222019-07-17 20:36:51 -0500163 if (!func)
164 return false;
165
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500166 if (func->bind == STB_WEAK)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500167 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500168
169 if (func->bind == STB_GLOBAL)
170 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
171 if (!strcmp(func->name, global_noreturns[i]))
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500172 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500173
Josh Poimboeuf13810432018-05-09 22:39:15 -0500174 if (!func->len)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500175 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500176
Josh Poimboeuf13810432018-05-09 22:39:15 -0500177 insn = find_insn(file, func->sec, func->offset);
178 if (!insn->func)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500179 return false;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500180
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100181 func_for_each_insn(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500182 empty = false;
183
184 if (insn->type == INSN_RETURN)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500185 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500186 }
187
188 if (empty)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500189 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500190
191 /*
192 * A function can have a sibling call instead of a return. In that
193 * case, the function's dead-end status depends on whether the target
194 * of the sibling call returns.
195 */
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100196 func_for_each_insn(file, func, insn) {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500197 if (is_sibling_call(insn)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500198 struct instruction *dest = insn->jump_dest;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500199
200 if (!dest)
201 /* sibling call to another file */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500202 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500203
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500204 /* local sibling call */
205 if (recursion == 5) {
206 /*
207 * Infinite recursion: two functions have
208 * sibling calls to each other. This is a very
209 * rare case. It means they aren't dead ends.
210 */
211 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500212 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500213
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500214 return __dead_end_function(file, dest->func, recursion+1);
215 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500216 }
217
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500218 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500219}
220
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500221static bool dead_end_function(struct objtool_file *file, struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500222{
223 return __dead_end_function(file, func, 0);
224}
225
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100226static void init_cfi_state(struct cfi_state *cfi)
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500227{
228 int i;
229
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500230 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100231 cfi->regs[i].base = CFI_UNDEFINED;
232 cfi->vals[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500233 }
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100234 cfi->cfa.base = CFI_UNDEFINED;
235 cfi->drap_reg = CFI_UNDEFINED;
236 cfi->drap_offset = -1;
237}
238
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100239static void init_insn_state(struct insn_state *state, struct section *sec)
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100240{
241 memset(state, 0, sizeof(*state));
242 init_cfi_state(&state->cfi);
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100243
244 /*
245 * We need the full vmlinux for noinstr validation, otherwise we can
246 * not correctly determine insn->call_dest->sec (external symbols do
247 * not have a section).
248 */
249 if (vmlinux && sec)
250 state->noinstr = sec->noinstr;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500251}
252
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500253/*
254 * Call the arch-specific instruction decoder for all the instructions and add
255 * them to the global instruction list.
256 */
257static int decode_instructions(struct objtool_file *file)
258{
259 struct section *sec;
260 struct symbol *func;
261 unsigned long offset;
262 struct instruction *insn;
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100263 unsigned long nr_insns = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500264 int ret;
265
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500266 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500267
268 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
269 continue;
270
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500271 if (strcmp(sec->name, ".altinstr_replacement") &&
272 strcmp(sec->name, ".altinstr_aux") &&
273 strncmp(sec->name, ".discard.", 9))
274 sec->text = true;
275
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +0100276 if (!strcmp(sec->name, ".noinstr.text") ||
277 !strcmp(sec->name, ".entry.text"))
Peter Zijlstrac4a33932020-03-10 18:57:41 +0100278 sec->noinstr = true;
279
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500280 for (offset = 0; offset < sec->len; offset += insn->len) {
281 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500282 if (!insn) {
283 WARN("malloc failed");
284 return -1;
285 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500286 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500287 INIT_LIST_HEAD(&insn->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +0000288 INIT_LIST_HEAD(&insn->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100289 init_cfi_state(&insn->cfi);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500290
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500291 insn->sec = sec;
292 insn->offset = offset;
293
294 ret = arch_decode_instruction(file->elf, sec, offset,
295 sec->len - offset,
296 &insn->len, &insn->type,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500297 &insn->immediate,
Julien Thierry65ea47d2020-03-27 15:28:47 +0000298 &insn->stack_ops);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500299 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500300 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500301
Peter Zijlstra87ecb582020-03-16 15:47:27 +0100302 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500303 list_add_tail(&insn->list, &file->insn_list);
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100304 nr_insns++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500305 }
306
307 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500308 if (func->type != STT_FUNC || func->alias != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500309 continue;
310
311 if (!find_insn(file, sec, func->offset)) {
312 WARN("%s(): can't find starting instruction",
313 func->name);
314 return -1;
315 }
316
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100317 sym_for_each_insn(file, func, insn)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500318 insn->func = func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500319 }
320 }
321
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100322 if (stats)
323 printf("nr_insns: %lu\n", nr_insns);
324
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500325 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500326
327err:
328 free(insn);
329 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500330}
331
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700332static struct instruction *find_last_insn(struct objtool_file *file,
333 struct section *sec)
334{
335 struct instruction *insn = NULL;
336 unsigned int offset;
337 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
338
339 for (offset = sec->len - 1; offset >= end && !insn; offset--)
340 insn = find_insn(file, sec, offset);
341
342 return insn;
343}
344
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500345/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500346 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500347 */
348static int add_dead_ends(struct objtool_file *file)
349{
350 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700351 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500352 struct instruction *insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500353
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500354 /*
355 * By default, "ud2" is a dead end unless otherwise annotated, because
356 * GCC 7 inserts it for certain divide-by-zero cases.
357 */
358 for_each_insn(file, insn)
359 if (insn->type == INSN_BUG)
360 insn->dead_end = true;
361
362 /*
363 * Check for manually annotated dead ends.
364 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500365 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
366 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500367 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500368
Matt Helsleyf1974222020-05-29 14:01:13 -0700369 list_for_each_entry(reloc, &sec->reloc_list, list) {
370 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500371 WARN("unexpected relocation symbol type in %s", sec->name);
372 return -1;
373 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700374 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500375 if (insn)
376 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700377 else if (reloc->addend == reloc->sym->sec->len) {
378 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700379 if (!insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500380 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700381 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500382 return -1;
383 }
384 } else {
385 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700386 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500387 return -1;
388 }
389
390 insn->dead_end = true;
391 }
392
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500393reachable:
394 /*
395 * These manually annotated reachable checks are needed for GCC 4.4,
396 * where the Linux unreachable() macro isn't supported. In that case
397 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
398 * not a dead end.
399 */
400 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
401 if (!sec)
402 return 0;
403
Matt Helsleyf1974222020-05-29 14:01:13 -0700404 list_for_each_entry(reloc, &sec->reloc_list, list) {
405 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500406 WARN("unexpected relocation symbol type in %s", sec->name);
407 return -1;
408 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700409 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500410 if (insn)
411 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700412 else if (reloc->addend == reloc->sym->sec->len) {
413 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700414 if (!insn) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500415 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700416 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500417 return -1;
418 }
419 } else {
420 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700421 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500422 return -1;
423 }
424
425 insn->dead_end = false;
426 }
427
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500428 return 0;
429}
430
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200431static int create_static_call_sections(struct objtool_file *file)
432{
433 struct section *sec, *reloc_sec;
434 struct reloc *reloc;
435 struct static_call_site *site;
436 struct instruction *insn;
437 struct symbol *key_sym;
438 char *key_name, *tmp;
439 int idx;
440
441 sec = find_section_by_name(file->elf, ".static_call_sites");
442 if (sec) {
443 INIT_LIST_HEAD(&file->static_call_list);
444 WARN("file already has .static_call_sites section, skipping");
445 return 0;
446 }
447
448 if (list_empty(&file->static_call_list))
449 return 0;
450
451 idx = 0;
452 list_for_each_entry(insn, &file->static_call_list, static_call_node)
453 idx++;
454
455 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
456 sizeof(struct static_call_site), idx);
457 if (!sec)
458 return -1;
459
460 reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
461 if (!reloc_sec)
462 return -1;
463
464 idx = 0;
465 list_for_each_entry(insn, &file->static_call_list, static_call_node) {
466
467 site = (struct static_call_site *)sec->data->d_buf + idx;
468 memset(site, 0, sizeof(struct static_call_site));
469
470 /* populate reloc for 'addr' */
471 reloc = malloc(sizeof(*reloc));
Josh Poimboeuf2b029852020-12-14 16:04:20 -0600472
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200473 if (!reloc) {
474 perror("malloc");
475 return -1;
476 }
477 memset(reloc, 0, sizeof(*reloc));
Josh Poimboeuf2b029852020-12-14 16:04:20 -0600478
479 insn_to_reloc_sym_addend(insn->sec, insn->offset, reloc);
480 if (!reloc->sym) {
481 WARN_FUNC("static call tramp: missing containing symbol",
482 insn->sec, insn->offset);
483 return -1;
484 }
485
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200486 reloc->type = R_X86_64_PC32;
487 reloc->offset = idx * sizeof(struct static_call_site);
488 reloc->sec = reloc_sec;
489 elf_add_reloc(file->elf, reloc);
490
491 /* find key symbol */
492 key_name = strdup(insn->call_dest->name);
493 if (!key_name) {
494 perror("strdup");
495 return -1;
496 }
497 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
498 STATIC_CALL_TRAMP_PREFIX_LEN)) {
499 WARN("static_call: trampoline name malformed: %s", key_name);
500 return -1;
501 }
502 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
503 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
504
505 key_sym = find_symbol_by_name(file->elf, tmp);
506 if (!key_sym) {
Josh Poimboeufa63068e2021-01-27 17:18:37 -0600507 if (!module) {
508 WARN("static_call: can't find static_call_key symbol: %s", tmp);
509 return -1;
510 }
511
512 /*
513 * For modules(), the key might not be exported, which
514 * means the module can make static calls but isn't
515 * allowed to change them.
516 *
517 * In that case we temporarily set the key to be the
518 * trampoline address. This is fixed up in
519 * static_call_add_module().
520 */
521 key_sym = insn->call_dest;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200522 }
523 free(key_name);
524
525 /* populate reloc for 'key' */
526 reloc = malloc(sizeof(*reloc));
527 if (!reloc) {
528 perror("malloc");
529 return -1;
530 }
531 memset(reloc, 0, sizeof(*reloc));
532 reloc->sym = key_sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200533 reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200534 reloc->type = R_X86_64_PC32;
535 reloc->offset = idx * sizeof(struct static_call_site) + 4;
536 reloc->sec = reloc_sec;
537 elf_add_reloc(file->elf, reloc);
538
539 idx++;
540 }
541
542 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
543 return -1;
544
545 return 0;
546}
547
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500548/*
549 * Warnings shouldn't be reported for ignored functions.
550 */
551static void add_ignores(struct objtool_file *file)
552{
553 struct instruction *insn;
554 struct section *sec;
555 struct symbol *func;
Matt Helsleyf1974222020-05-29 14:01:13 -0700556 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500557
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100558 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
559 if (!sec)
560 return;
561
Matt Helsleyf1974222020-05-29 14:01:13 -0700562 list_for_each_entry(reloc, &sec->reloc_list, list) {
563 switch (reloc->sym->type) {
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100564 case STT_FUNC:
Matt Helsleyf1974222020-05-29 14:01:13 -0700565 func = reloc->sym;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100566 break;
567
568 case STT_SECTION:
Matt Helsleyf1974222020-05-29 14:01:13 -0700569 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600570 if (!func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500571 continue;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100572 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500573
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100574 default:
Matt Helsleyf1974222020-05-29 14:01:13 -0700575 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100576 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500577 }
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100578
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100579 func_for_each_insn(file, func, insn)
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100580 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500581 }
582}
583
584/*
Peter Zijlstraea242132019-02-25 12:50:09 +0100585 * This is a whitelist of functions that is allowed to be called with AC set.
586 * The list is meant to be minimal and only contains compiler instrumentation
587 * ABI and a few functions used to implement *_{to,from}_user() functions.
588 *
589 * These functions must not directly change AC, but may PUSHF/POPF.
590 */
591static const char *uaccess_safe_builtin[] = {
592 /* KASAN */
593 "kasan_report",
594 "check_memory_region",
595 /* KASAN out-of-line */
596 "__asan_loadN_noabort",
597 "__asan_load1_noabort",
598 "__asan_load2_noabort",
599 "__asan_load4_noabort",
600 "__asan_load8_noabort",
601 "__asan_load16_noabort",
602 "__asan_storeN_noabort",
603 "__asan_store1_noabort",
604 "__asan_store2_noabort",
605 "__asan_store4_noabort",
606 "__asan_store8_noabort",
607 "__asan_store16_noabort",
Jann Hornb0b8e562020-09-29 00:49:16 +0200608 "__kasan_check_read",
609 "__kasan_check_write",
Peter Zijlstraea242132019-02-25 12:50:09 +0100610 /* KASAN in-line */
611 "__asan_report_load_n_noabort",
612 "__asan_report_load1_noabort",
613 "__asan_report_load2_noabort",
614 "__asan_report_load4_noabort",
615 "__asan_report_load8_noabort",
616 "__asan_report_load16_noabort",
617 "__asan_report_store_n_noabort",
618 "__asan_report_store1_noabort",
619 "__asan_report_store2_noabort",
620 "__asan_report_store4_noabort",
621 "__asan_report_store8_noabort",
622 "__asan_report_store16_noabort",
Marco Elver5f5c9712019-11-14 19:02:57 +0100623 /* KCSAN */
Marco Elver99676832020-03-25 17:41:57 +0100624 "__kcsan_check_access",
Marco Elver5f5c9712019-11-14 19:02:57 +0100625 "kcsan_found_watchpoint",
626 "kcsan_setup_watchpoint",
Marco Elver99676832020-03-25 17:41:57 +0100627 "kcsan_check_scoped_accesses",
Marco Elver50a19ad2020-04-24 17:47:30 +0200628 "kcsan_disable_current",
629 "kcsan_enable_current_nowarn",
Marco Elver5f5c9712019-11-14 19:02:57 +0100630 /* KCSAN/TSAN */
631 "__tsan_func_entry",
632 "__tsan_func_exit",
633 "__tsan_read_range",
634 "__tsan_write_range",
635 "__tsan_read1",
636 "__tsan_read2",
637 "__tsan_read4",
638 "__tsan_read8",
639 "__tsan_read16",
640 "__tsan_write1",
641 "__tsan_write2",
642 "__tsan_write4",
643 "__tsan_write8",
644 "__tsan_write16",
Marco Elvera81b3752020-07-24 09:00:02 +0200645 "__tsan_read_write1",
646 "__tsan_read_write2",
647 "__tsan_read_write4",
648 "__tsan_read_write8",
649 "__tsan_read_write16",
Marco Elver883957b2020-07-03 15:40:30 +0200650 "__tsan_atomic8_load",
651 "__tsan_atomic16_load",
652 "__tsan_atomic32_load",
653 "__tsan_atomic64_load",
654 "__tsan_atomic8_store",
655 "__tsan_atomic16_store",
656 "__tsan_atomic32_store",
657 "__tsan_atomic64_store",
658 "__tsan_atomic8_exchange",
659 "__tsan_atomic16_exchange",
660 "__tsan_atomic32_exchange",
661 "__tsan_atomic64_exchange",
662 "__tsan_atomic8_fetch_add",
663 "__tsan_atomic16_fetch_add",
664 "__tsan_atomic32_fetch_add",
665 "__tsan_atomic64_fetch_add",
666 "__tsan_atomic8_fetch_sub",
667 "__tsan_atomic16_fetch_sub",
668 "__tsan_atomic32_fetch_sub",
669 "__tsan_atomic64_fetch_sub",
670 "__tsan_atomic8_fetch_and",
671 "__tsan_atomic16_fetch_and",
672 "__tsan_atomic32_fetch_and",
673 "__tsan_atomic64_fetch_and",
674 "__tsan_atomic8_fetch_or",
675 "__tsan_atomic16_fetch_or",
676 "__tsan_atomic32_fetch_or",
677 "__tsan_atomic64_fetch_or",
678 "__tsan_atomic8_fetch_xor",
679 "__tsan_atomic16_fetch_xor",
680 "__tsan_atomic32_fetch_xor",
681 "__tsan_atomic64_fetch_xor",
682 "__tsan_atomic8_fetch_nand",
683 "__tsan_atomic16_fetch_nand",
684 "__tsan_atomic32_fetch_nand",
685 "__tsan_atomic64_fetch_nand",
686 "__tsan_atomic8_compare_exchange_strong",
687 "__tsan_atomic16_compare_exchange_strong",
688 "__tsan_atomic32_compare_exchange_strong",
689 "__tsan_atomic64_compare_exchange_strong",
690 "__tsan_atomic8_compare_exchange_weak",
691 "__tsan_atomic16_compare_exchange_weak",
692 "__tsan_atomic32_compare_exchange_weak",
693 "__tsan_atomic64_compare_exchange_weak",
694 "__tsan_atomic8_compare_exchange_val",
695 "__tsan_atomic16_compare_exchange_val",
696 "__tsan_atomic32_compare_exchange_val",
697 "__tsan_atomic64_compare_exchange_val",
698 "__tsan_atomic_thread_fence",
699 "__tsan_atomic_signal_fence",
Peter Zijlstraea242132019-02-25 12:50:09 +0100700 /* KCOV */
701 "write_comp_data",
Josh Poimboeufae033f02020-04-29 14:09:04 -0500702 "check_kcov_mode",
Peter Zijlstraea242132019-02-25 12:50:09 +0100703 "__sanitizer_cov_trace_pc",
704 "__sanitizer_cov_trace_const_cmp1",
705 "__sanitizer_cov_trace_const_cmp2",
706 "__sanitizer_cov_trace_const_cmp4",
707 "__sanitizer_cov_trace_const_cmp8",
708 "__sanitizer_cov_trace_cmp1",
709 "__sanitizer_cov_trace_cmp2",
710 "__sanitizer_cov_trace_cmp4",
711 "__sanitizer_cov_trace_cmp8",
Al Viro36b1c702020-02-16 13:07:49 -0500712 "__sanitizer_cov_trace_switch",
Peter Zijlstraea242132019-02-25 12:50:09 +0100713 /* UBSAN */
714 "ubsan_type_mismatch_common",
715 "__ubsan_handle_type_mismatch",
716 "__ubsan_handle_type_mismatch_v1",
Peter Zijlstra9a50dca2019-10-21 15:11:49 +0200717 "__ubsan_handle_shift_out_of_bounds",
Peter Zijlstraea242132019-02-25 12:50:09 +0100718 /* misc */
719 "csum_partial_copy_generic",
Dan Williamsec6347b2020-10-05 20:40:16 -0700720 "copy_mc_fragile",
721 "copy_mc_fragile_handle_tail",
Dan Williams5da8e4a2020-10-05 20:40:25 -0700722 "copy_mc_enhanced_fast_string",
Peter Zijlstraea242132019-02-25 12:50:09 +0100723 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
724 NULL
725};
726
727static void add_uaccess_safe(struct objtool_file *file)
728{
729 struct symbol *func;
730 const char **name;
731
732 if (!uaccess)
733 return;
734
735 for (name = uaccess_safe_builtin; *name; name++) {
736 func = find_symbol_by_name(file->elf, *name);
737 if (!func)
738 continue;
739
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500740 func->uaccess_safe = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500741 }
742}
743
744/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000745 * FIXME: For now, just ignore any alternatives which add retpolines. This is
746 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
747 * But it at least allows objtool to understand the control flow *around* the
748 * retpoline.
749 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100750static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000751{
752 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700753 struct reloc *reloc;
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000754 struct instruction *insn;
755
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100756 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000757 if (!sec)
758 return 0;
759
Matt Helsleyf1974222020-05-29 14:01:13 -0700760 list_for_each_entry(reloc, &sec->reloc_list, list) {
761 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000762 WARN("unexpected relocation symbol type in %s", sec->name);
763 return -1;
764 }
765
Matt Helsleyf1974222020-05-29 14:01:13 -0700766 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000767 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100768 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000769 return -1;
770 }
771
772 insn->ignore_alts = true;
773 }
774
775 return 0;
776}
777
778/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500779 * Find the destination instructions for all jumps.
780 */
781static int add_jump_destinations(struct objtool_file *file)
782{
783 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -0700784 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500785 struct section *dest_sec;
786 unsigned long dest_off;
787
788 for_each_insn(file, insn) {
Josh Poimboeufa2296142020-02-10 12:32:39 -0600789 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500790 continue;
791
Josh Poimboeufdb6c6a02020-09-10 10:24:57 -0500792 if (insn->offset == FAKE_JUMP_OFFSET)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500793 continue;
794
Matt Helsleyf1974222020-05-29 14:01:13 -0700795 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100796 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700797 if (!reloc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500798 dest_sec = insn->sec;
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000799 dest_off = arch_jump_destination(insn);
Matt Helsleyf1974222020-05-29 14:01:13 -0700800 } else if (reloc->sym->type == STT_SECTION) {
801 dest_sec = reloc->sym->sec;
802 dest_off = arch_dest_reloc_offset(reloc->addend);
803 } else if (reloc->sym->sec->idx) {
804 dest_sec = reloc->sym->sec;
805 dest_off = reloc->sym->sym.st_value +
806 arch_dest_reloc_offset(reloc->addend);
Josh Poimboeuf76313762021-01-21 15:29:18 -0600807 } else if (!strncmp(reloc->sym->name, "__x86_indirect_thunk_", 21) ||
808 !strncmp(reloc->sym->name, "__x86_retpoline_", 16)) {
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000809 /*
810 * Retpoline jumps are really dynamic jumps in
811 * disguise, so convert them accordingly.
812 */
Josh Poimboeufb68b9902019-07-17 20:36:57 -0500813 if (insn->type == INSN_JUMP_UNCONDITIONAL)
814 insn->type = INSN_JUMP_DYNAMIC;
815 else
816 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
817
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100818 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000819 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500820 } else {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500821 /* external sibling call */
Matt Helsleyf1974222020-05-29 14:01:13 -0700822 insn->call_dest = reloc->sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200823 if (insn->call_dest->static_call_tramp) {
824 list_add_tail(&insn->static_call_node,
825 &file->static_call_list);
826 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500827 continue;
828 }
829
830 insn->jump_dest = find_insn(file, dest_sec, dest_off);
831 if (!insn->jump_dest) {
832
833 /*
834 * This is a special case where an alt instruction
835 * jumps past the end of the section. These are
836 * handled later in handle_group_alt().
837 */
838 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
839 continue;
840
841 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
842 insn->sec, insn->offset, dest_sec->name,
843 dest_off);
844 return -1;
845 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500846
847 /*
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100848 * Cross-function jump.
Josh Poimboeufcd778492018-06-01 07:23:51 -0500849 */
850 if (insn->func && insn->jump_dest->func &&
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100851 insn->func != insn->jump_dest->func) {
852
853 /*
854 * For GCC 8+, create parent/child links for any cold
855 * subfunctions. This is _mostly_ redundant with a
856 * similar initialization in read_symbols().
857 *
858 * If a function has aliases, we want the *first* such
859 * function in the symbol table to be the subfunction's
860 * parent. In that case we overwrite the
861 * initialization done in read_symbols().
862 *
863 * However this code can't completely replace the
864 * read_symbols() code because this doesn't detect the
865 * case where the parent function's only reference to a
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500866 * subfunction is through a jump table.
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100867 */
Josh Poimboeufc41fc752021-01-21 15:29:19 -0600868 if (!strstr(insn->func->name, ".cold") &&
869 strstr(insn->jump_dest->func->name, ".cold")) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100870 insn->func->cfunc = insn->jump_dest->func;
871 insn->jump_dest->func->pfunc = insn->func;
872
873 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
874 insn->jump_dest->offset == insn->jump_dest->func->offset) {
875
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500876 /* internal sibling call */
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100877 insn->call_dest = insn->jump_dest->func;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200878 if (insn->call_dest->static_call_tramp) {
879 list_add_tail(&insn->static_call_node,
880 &file->static_call_list);
881 }
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100882 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500883 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500884 }
885
886 return 0;
887}
888
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200889static void remove_insn_ops(struct instruction *insn)
890{
891 struct stack_op *op, *tmp;
892
893 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
894 list_del(&op->list);
895 free(op);
896 }
897}
898
Julien Thierry2b232a22020-09-15 08:53:18 +0100899static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
900{
901 struct symbol *call_dest;
902
903 call_dest = find_func_by_offset(sec, offset);
904 if (!call_dest)
905 call_dest = find_symbol_by_offset(sec, offset);
906
907 return call_dest;
908}
909
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500910/*
911 * Find the destination instructions for all calls.
912 */
913static int add_call_destinations(struct objtool_file *file)
914{
915 struct instruction *insn;
916 unsigned long dest_off;
Matt Helsleyf1974222020-05-29 14:01:13 -0700917 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500918
919 for_each_insn(file, insn) {
920 if (insn->type != INSN_CALL)
921 continue;
922
Matt Helsleyf1974222020-05-29 14:01:13 -0700923 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100924 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700925 if (!reloc) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000926 dest_off = arch_jump_destination(insn);
Julien Thierry2b232a22020-09-15 08:53:18 +0100927 insn->call_dest = find_call_destination(insn->sec, dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600928
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600929 if (insn->ignore)
930 continue;
931
932 if (!insn->call_dest) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200933 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500934 return -1;
935 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600936
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600937 if (insn->func && insn->call_dest->type != STT_FUNC) {
938 WARN_FUNC("unsupported call to non-function",
939 insn->sec, insn->offset);
940 return -1;
941 }
942
Matt Helsleyf1974222020-05-29 14:01:13 -0700943 } else if (reloc->sym->type == STT_SECTION) {
944 dest_off = arch_dest_reloc_offset(reloc->addend);
Julien Thierry2b232a22020-09-15 08:53:18 +0100945 insn->call_dest = find_call_destination(reloc->sym->sec,
946 dest_off);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600947 if (!insn->call_dest) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000948 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500949 insn->sec, insn->offset,
Matt Helsleyf1974222020-05-29 14:01:13 -0700950 reloc->sym->sec->name,
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000951 dest_off);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500952 return -1;
953 }
954 } else
Matt Helsleyf1974222020-05-29 14:01:13 -0700955 insn->call_dest = reloc->sym;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200956
Peter Zijlstraf0bc12b2021-03-26 16:12:05 +0100957 if (insn->call_dest && insn->call_dest->static_call_tramp) {
958 list_add_tail(&insn->static_call_node,
959 &file->static_call_list);
960 }
961
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200962 /*
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200963 * Many compilers cannot disable KCOV with a function attribute
964 * so they need a little help, NOP out any KCOV calls from noinstr
965 * text.
966 */
967 if (insn->sec->noinstr &&
968 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
Peter Zijlstrad832c002020-06-18 17:55:29 +0200969 if (reloc) {
970 reloc->type = R_NONE;
971 elf_write_reloc(file->elf, reloc);
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200972 }
973
974 elf_write_insn(file->elf, insn->sec,
975 insn->offset, insn->len,
976 arch_nop_insn(insn->len));
977 insn->type = INSN_NOP;
978 }
979
980 /*
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200981 * Whatever stack impact regular CALLs have, should be undone
982 * by the RETURN of the called function.
983 *
984 * Annotated intra-function calls retain the stack_ops but
985 * are converted to JUMP, see read_intra_function_calls().
986 */
987 remove_insn_ops(insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500988 }
989
990 return 0;
991}
992
993/*
994 * The .alternatives section requires some extra special care, over and above
995 * what other special sections require:
996 *
997 * 1. Because alternatives are patched in-place, we need to insert a fake jump
998 * instruction at the end so that validate_branch() skips all the original
999 * replaced instructions when validating the new instruction path.
1000 *
1001 * 2. An added wrinkle is that the new instruction length might be zero. In
1002 * that case the old instructions are replaced with noops. We simulate that
1003 * by creating a fake jump as the only new instruction.
1004 *
1005 * 3. In some cases, the alternative section includes an instruction which
1006 * conditionally jumps to the _end_ of the entry. We have to modify these
1007 * jumps' destinations to point back to .text rather than the end of the
1008 * entry in .altinstr_replacement.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001009 */
1010static int handle_group_alt(struct objtool_file *file,
1011 struct special_alt *special_alt,
1012 struct instruction *orig_insn,
1013 struct instruction **new_insn)
1014{
Alexandre Chartre13fab062020-04-14 12:36:11 +02001015 static unsigned int alt_group_next_index = 1;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001016 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Alexandre Chartre13fab062020-04-14 12:36:11 +02001017 unsigned int alt_group = alt_group_next_index++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001018 unsigned long dest_off;
1019
1020 last_orig_insn = NULL;
1021 insn = orig_insn;
1022 sec_for_each_insn_from(file, insn) {
1023 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1024 break;
1025
Alexandre Chartre13fab062020-04-14 12:36:11 +02001026 insn->alt_group = alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001027 last_orig_insn = insn;
1028 }
1029
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001030 if (next_insn_same_sec(file, last_orig_insn)) {
1031 fake_jump = malloc(sizeof(*fake_jump));
1032 if (!fake_jump) {
1033 WARN("malloc failed");
1034 return -1;
1035 }
1036 memset(fake_jump, 0, sizeof(*fake_jump));
1037 INIT_LIST_HEAD(&fake_jump->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +00001038 INIT_LIST_HEAD(&fake_jump->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001039 init_cfi_state(&fake_jump->cfi);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001040
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001041 fake_jump->sec = special_alt->new_sec;
Josh Poimboeufe6da9562019-05-13 12:01:31 -05001042 fake_jump->offset = FAKE_JUMP_OFFSET;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001043 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
1044 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
Josh Poimboeufe6da9562019-05-13 12:01:31 -05001045 fake_jump->func = orig_insn->func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001046 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001047
1048 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001049 if (!fake_jump) {
1050 WARN("%s: empty alternative at end of section",
1051 special_alt->orig_sec->name);
1052 return -1;
1053 }
1054
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001055 *new_insn = fake_jump;
1056 return 0;
1057 }
1058
1059 last_new_insn = NULL;
Alexandre Chartre13fab062020-04-14 12:36:11 +02001060 alt_group = alt_group_next_index++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001061 insn = *new_insn;
1062 sec_for_each_insn_from(file, insn) {
Julien Thierry45245f52020-09-04 16:30:23 +01001063 struct reloc *alt_reloc;
1064
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001065 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1066 break;
1067
1068 last_new_insn = insn;
1069
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001070 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +01001071 insn->func = orig_insn->func;
Alexandre Chartre13fab062020-04-14 12:36:11 +02001072 insn->alt_group = alt_group;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001073
Josh Poimboeufdc419722020-02-10 12:32:40 -06001074 /*
1075 * Since alternative replacement code is copy/pasted by the
1076 * kernel after applying relocations, generally such code can't
1077 * have relative-address relocation references to outside the
1078 * .altinstr_replacement section, unless the arch's
1079 * alternatives code can adjust the relative offsets
1080 * accordingly.
Josh Poimboeufdc419722020-02-10 12:32:40 -06001081 */
Julien Thierry45245f52020-09-04 16:30:23 +01001082 alt_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1083 insn->offset, insn->len);
1084 if (alt_reloc &&
1085 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
Josh Poimboeufdc419722020-02-10 12:32:40 -06001086
1087 WARN_FUNC("unsupported relocation in alternatives section",
1088 insn->sec, insn->offset);
1089 return -1;
1090 }
1091
Josh Poimboeufa2296142020-02-10 12:32:39 -06001092 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001093 continue;
1094
1095 if (!insn->immediate)
1096 continue;
1097
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001098 dest_off = arch_jump_destination(insn);
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001099 if (dest_off == special_alt->new_off + special_alt->new_len) {
1100 if (!fake_jump) {
1101 WARN("%s: alternative jump to end of section",
1102 special_alt->orig_sec->name);
1103 return -1;
1104 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001105 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001106 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001107
1108 if (!insn->jump_dest) {
1109 WARN_FUNC("can't find alternative jump destination",
1110 insn->sec, insn->offset);
1111 return -1;
1112 }
1113 }
1114
1115 if (!last_new_insn) {
1116 WARN_FUNC("can't find last new alternative instruction",
1117 special_alt->new_sec, special_alt->new_off);
1118 return -1;
1119 }
1120
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001121 if (fake_jump)
1122 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001123
1124 return 0;
1125}
1126
1127/*
1128 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1129 * If the original instruction is a jump, make the alt entry an effective nop
1130 * by just skipping the original instruction.
1131 */
1132static int handle_jump_alt(struct objtool_file *file,
1133 struct special_alt *special_alt,
1134 struct instruction *orig_insn,
1135 struct instruction **new_insn)
1136{
1137 if (orig_insn->type == INSN_NOP)
1138 return 0;
1139
1140 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1141 WARN_FUNC("unsupported instruction at jump label",
1142 orig_insn->sec, orig_insn->offset);
1143 return -1;
1144 }
1145
1146 *new_insn = list_next_entry(orig_insn, list);
1147 return 0;
1148}
1149
1150/*
1151 * Read all the special sections which have alternate instructions which can be
1152 * patched in or redirected to at runtime. Each instruction having alternate
1153 * instruction(s) has them added to its insn->alts list, which will be
1154 * traversed in validate_branch().
1155 */
1156static int add_special_section_alts(struct objtool_file *file)
1157{
1158 struct list_head special_alts;
1159 struct instruction *orig_insn, *new_insn;
1160 struct special_alt *special_alt, *tmp;
1161 struct alternative *alt;
1162 int ret;
1163
1164 ret = special_get_alts(file->elf, &special_alts);
1165 if (ret)
1166 return ret;
1167
1168 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001169
1170 orig_insn = find_insn(file, special_alt->orig_sec,
1171 special_alt->orig_off);
1172 if (!orig_insn) {
1173 WARN_FUNC("special: can't find orig instruction",
1174 special_alt->orig_sec, special_alt->orig_off);
1175 ret = -1;
1176 goto out;
1177 }
1178
1179 new_insn = NULL;
1180 if (!special_alt->group || special_alt->new_len) {
1181 new_insn = find_insn(file, special_alt->new_sec,
1182 special_alt->new_off);
1183 if (!new_insn) {
1184 WARN_FUNC("special: can't find new instruction",
1185 special_alt->new_sec,
1186 special_alt->new_off);
1187 ret = -1;
1188 goto out;
1189 }
1190 }
1191
1192 if (special_alt->group) {
Julien Thierry7170cf42020-03-27 15:28:41 +00001193 if (!special_alt->orig_len) {
1194 WARN_FUNC("empty alternative entry",
1195 orig_insn->sec, orig_insn->offset);
1196 continue;
1197 }
1198
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001199 ret = handle_group_alt(file, special_alt, orig_insn,
1200 &new_insn);
1201 if (ret)
1202 goto out;
1203 } else if (special_alt->jump_or_nop) {
1204 ret = handle_jump_alt(file, special_alt, orig_insn,
1205 &new_insn);
1206 if (ret)
1207 goto out;
1208 }
1209
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001210 alt = malloc(sizeof(*alt));
1211 if (!alt) {
1212 WARN("malloc failed");
1213 ret = -1;
1214 goto out;
1215 }
1216
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001217 alt->insn = new_insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +01001218 alt->skip_orig = special_alt->skip_orig;
Peter Zijlstraea242132019-02-25 12:50:09 +01001219 orig_insn->ignore_alts |= special_alt->skip_alt;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001220 list_add_tail(&alt->list, &orig_insn->alts);
1221
1222 list_del(&special_alt->list);
1223 free(special_alt);
1224 }
1225
1226out:
1227 return ret;
1228}
1229
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001230static int add_jump_table(struct objtool_file *file, struct instruction *insn,
Matt Helsleyf1974222020-05-29 14:01:13 -07001231 struct reloc *table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001232{
Matt Helsleyf1974222020-05-29 14:01:13 -07001233 struct reloc *reloc = table;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001234 struct instruction *dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001235 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001236 struct symbol *pfunc = insn->func->pfunc;
1237 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001238
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001239 /*
Matt Helsleyf1974222020-05-29 14:01:13 -07001240 * Each @reloc is a switch table relocation which points to the target
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001241 * instruction.
1242 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001243 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
Jann Hornbd98c812019-07-17 20:36:54 -05001244
1245 /* Check for the end of the table: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001246 if (reloc != table && reloc->jump_table_start)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001247 break;
1248
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001249 /* Make sure the table entries are consecutive: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001250 if (prev_offset && reloc->offset != prev_offset + 8)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001251 break;
1252
1253 /* Detect function pointers from contiguous objects: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001254 if (reloc->sym->sec == pfunc->sec &&
1255 reloc->addend == pfunc->offset)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001256 break;
1257
Matt Helsleyf1974222020-05-29 14:01:13 -07001258 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001259 if (!dest_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001260 break;
1261
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001262 /* Make sure the destination is in the same function: */
Josh Poimboeufe65050b2019-07-17 20:36:55 -05001263 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -05001264 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001265
1266 alt = malloc(sizeof(*alt));
1267 if (!alt) {
1268 WARN("malloc failed");
1269 return -1;
1270 }
1271
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001272 alt->insn = dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001273 list_add_tail(&alt->list, &insn->alts);
Matt Helsleyf1974222020-05-29 14:01:13 -07001274 prev_offset = reloc->offset;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001275 }
1276
1277 if (!prev_offset) {
1278 WARN_FUNC("can't find switch jump table",
1279 insn->sec, insn->offset);
1280 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001281 }
1282
1283 return 0;
1284}
1285
1286/*
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001287 * find_jump_table() - Given a dynamic jump, find the switch jump table
1288 * associated with it.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001289 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001290static struct reloc *find_jump_table(struct objtool_file *file,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001291 struct symbol *func,
1292 struct instruction *insn)
1293{
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001294 struct reloc *table_reloc;
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001295 struct instruction *dest_insn, *orig_insn = insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001296
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001297 /*
1298 * Backward search using the @first_jump_src links, these help avoid
1299 * much of the 'in between' code. Which avoids us getting confused by
1300 * it.
1301 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001302 for (;
Josh Poimboeuf1119d262020-04-28 16:45:16 -05001303 insn && insn->func && insn->func->pfunc == func;
1304 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001305
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001306 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001307 break;
1308
1309 /* allow small jumps within the range */
1310 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1311 insn->jump_dest &&
1312 (insn->jump_dest->offset <= insn->offset ||
1313 insn->jump_dest->offset > orig_insn->offset))
1314 break;
1315
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001316 table_reloc = arch_find_switch_table(file, insn);
Matt Helsleyf1974222020-05-29 14:01:13 -07001317 if (!table_reloc)
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001318 continue;
Matt Helsleyf1974222020-05-29 14:01:13 -07001319 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001320 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1321 continue;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001322
Matt Helsleyf1974222020-05-29 14:01:13 -07001323 return table_reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001324 }
1325
1326 return NULL;
1327}
1328
Jann Hornbd98c812019-07-17 20:36:54 -05001329/*
1330 * First pass: Mark the head of each jump table so that in the next pass,
1331 * we know when a given jump table ends and the next one starts.
1332 */
1333static void mark_func_jump_tables(struct objtool_file *file,
1334 struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001335{
Jann Hornbd98c812019-07-17 20:36:54 -05001336 struct instruction *insn, *last = NULL;
Matt Helsleyf1974222020-05-29 14:01:13 -07001337 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001338
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001339 func_for_each_insn(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001340 if (!last)
1341 last = insn;
1342
1343 /*
1344 * Store back-pointers for unconditional forward jumps such
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001345 * that find_jump_table() can back-track using those and
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001346 * avoid some potentially confusing code.
1347 */
1348 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1349 insn->offset > last->offset &&
1350 insn->jump_dest->offset > insn->offset &&
1351 !insn->jump_dest->first_jump_src) {
1352
1353 insn->jump_dest->first_jump_src = insn;
1354 last = insn->jump_dest;
1355 }
1356
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001357 if (insn->type != INSN_JUMP_DYNAMIC)
1358 continue;
1359
Matt Helsleyf1974222020-05-29 14:01:13 -07001360 reloc = find_jump_table(file, func, insn);
1361 if (reloc) {
1362 reloc->jump_table_start = true;
1363 insn->jump_table = reloc;
Jann Hornbd98c812019-07-17 20:36:54 -05001364 }
1365 }
1366}
1367
1368static int add_func_jump_tables(struct objtool_file *file,
1369 struct symbol *func)
1370{
1371 struct instruction *insn;
1372 int ret;
1373
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001374 func_for_each_insn(file, func, insn) {
Jann Hornbd98c812019-07-17 20:36:54 -05001375 if (!insn->jump_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001376 continue;
1377
Jann Hornbd98c812019-07-17 20:36:54 -05001378 ret = add_jump_table(file, insn, insn->jump_table);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001379 if (ret)
1380 return ret;
1381 }
1382
1383 return 0;
1384}
1385
1386/*
1387 * For some switch statements, gcc generates a jump table in the .rodata
1388 * section which contains a list of addresses within the function to jump to.
1389 * This finds these jump tables and adds them to the insn->alts lists.
1390 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001391static int add_jump_table_alts(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001392{
1393 struct section *sec;
1394 struct symbol *func;
1395 int ret;
1396
Allan Xavier4a60aa02018-09-07 08:12:01 -05001397 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001398 return 0;
1399
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001400 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001401 list_for_each_entry(func, &sec->symbol_list, list) {
1402 if (func->type != STT_FUNC)
1403 continue;
1404
Jann Hornbd98c812019-07-17 20:36:54 -05001405 mark_func_jump_tables(file, func);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001406 ret = add_func_jump_tables(file, func);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001407 if (ret)
1408 return ret;
1409 }
1410 }
1411
1412 return 0;
1413}
1414
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001415static int read_unwind_hints(struct objtool_file *file)
1416{
Matt Helsleyf1974222020-05-29 14:01:13 -07001417 struct section *sec, *relocsec;
1418 struct reloc *reloc;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001419 struct unwind_hint *hint;
1420 struct instruction *insn;
1421 struct cfi_reg *cfa;
1422 int i;
1423
1424 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1425 if (!sec)
1426 return 0;
1427
Matt Helsleyf1974222020-05-29 14:01:13 -07001428 relocsec = sec->reloc;
1429 if (!relocsec) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001430 WARN("missing .rela.discard.unwind_hints section");
1431 return -1;
1432 }
1433
1434 if (sec->len % sizeof(struct unwind_hint)) {
1435 WARN("struct unwind_hint size mismatch");
1436 return -1;
1437 }
1438
1439 file->hints = true;
1440
1441 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1442 hint = (struct unwind_hint *)sec->data->d_buf + i;
1443
Matt Helsleyf1974222020-05-29 14:01:13 -07001444 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1445 if (!reloc) {
1446 WARN("can't find reloc for unwind_hints[%d]", i);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001447 return -1;
1448 }
1449
Matt Helsleyf1974222020-05-29 14:01:13 -07001450 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001451 if (!insn) {
1452 WARN("can't find insn for unwind_hints[%d]", i);
1453 return -1;
1454 }
1455
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001456 cfa = &insn->cfi.cfa;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001457
Peter Zijlstrac536ed22020-04-01 16:54:26 +02001458 if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001459 insn->ret_offset = hint->sp_offset;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001460 continue;
1461 }
1462
1463 insn->hint = true;
1464
Julien Thierryedea9e62020-09-04 16:30:28 +01001465 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001466 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1467 insn->sec, insn->offset, hint->sp_reg);
1468 return -1;
1469 }
1470
1471 cfa->offset = hint->sp_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001472 insn->cfi.type = hint->type;
1473 insn->cfi.end = hint->end;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001474 }
1475
1476 return 0;
1477}
1478
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001479static int read_retpoline_hints(struct objtool_file *file)
1480{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001481 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001482 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001483 struct reloc *reloc;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001484
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001485 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001486 if (!sec)
1487 return 0;
1488
Matt Helsleyf1974222020-05-29 14:01:13 -07001489 list_for_each_entry(reloc, &sec->reloc_list, list) {
1490 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001491 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001492 return -1;
1493 }
1494
Matt Helsleyf1974222020-05-29 14:01:13 -07001495 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001496 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001497 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001498 return -1;
1499 }
1500
1501 if (insn->type != INSN_JUMP_DYNAMIC &&
1502 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001503 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001504 insn->sec, insn->offset);
1505 return -1;
1506 }
1507
1508 insn->retpoline_safe = true;
1509 }
1510
1511 return 0;
1512}
1513
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001514static int read_instr_hints(struct objtool_file *file)
1515{
1516 struct section *sec;
1517 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001518 struct reloc *reloc;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001519
1520 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1521 if (!sec)
1522 return 0;
1523
Matt Helsleyf1974222020-05-29 14:01:13 -07001524 list_for_each_entry(reloc, &sec->reloc_list, list) {
1525 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001526 WARN("unexpected relocation symbol type in %s", sec->name);
1527 return -1;
1528 }
1529
Matt Helsleyf1974222020-05-29 14:01:13 -07001530 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001531 if (!insn) {
1532 WARN("bad .discard.instr_end entry");
1533 return -1;
1534 }
1535
1536 insn->instr--;
1537 }
1538
1539 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1540 if (!sec)
1541 return 0;
1542
Matt Helsleyf1974222020-05-29 14:01:13 -07001543 list_for_each_entry(reloc, &sec->reloc_list, list) {
1544 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001545 WARN("unexpected relocation symbol type in %s", sec->name);
1546 return -1;
1547 }
1548
Matt Helsleyf1974222020-05-29 14:01:13 -07001549 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001550 if (!insn) {
1551 WARN("bad .discard.instr_begin entry");
1552 return -1;
1553 }
1554
1555 insn->instr++;
1556 }
1557
1558 return 0;
1559}
1560
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001561static int read_intra_function_calls(struct objtool_file *file)
1562{
1563 struct instruction *insn;
1564 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -07001565 struct reloc *reloc;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001566
1567 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1568 if (!sec)
1569 return 0;
1570
Matt Helsleyf1974222020-05-29 14:01:13 -07001571 list_for_each_entry(reloc, &sec->reloc_list, list) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001572 unsigned long dest_off;
1573
Matt Helsleyf1974222020-05-29 14:01:13 -07001574 if (reloc->sym->type != STT_SECTION) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001575 WARN("unexpected relocation symbol type in %s",
1576 sec->name);
1577 return -1;
1578 }
1579
Matt Helsleyf1974222020-05-29 14:01:13 -07001580 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001581 if (!insn) {
1582 WARN("bad .discard.intra_function_call entry");
1583 return -1;
1584 }
1585
1586 if (insn->type != INSN_CALL) {
1587 WARN_FUNC("intra_function_call not a direct call",
1588 insn->sec, insn->offset);
1589 return -1;
1590 }
1591
1592 /*
1593 * Treat intra-function CALLs as JMPs, but with a stack_op.
1594 * See add_call_destinations(), which strips stack_ops from
1595 * normal CALLs.
1596 */
1597 insn->type = INSN_JUMP_UNCONDITIONAL;
1598
1599 dest_off = insn->offset + insn->len + insn->immediate;
1600 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1601 if (!insn->jump_dest) {
1602 WARN_FUNC("can't find call dest at %s+0x%lx",
1603 insn->sec, insn->offset,
1604 insn->sec->name, dest_off);
1605 return -1;
1606 }
1607 }
1608
1609 return 0;
1610}
1611
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02001612static int read_static_call_tramps(struct objtool_file *file)
1613{
1614 struct section *sec;
1615 struct symbol *func;
1616
1617 for_each_sec(file, sec) {
1618 list_for_each_entry(func, &sec->symbol_list, list) {
1619 if (func->bind == STB_GLOBAL &&
1620 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1621 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1622 func->static_call_tramp = true;
1623 }
1624 }
1625
1626 return 0;
1627}
1628
Allan Xavier4a60aa02018-09-07 08:12:01 -05001629static void mark_rodata(struct objtool_file *file)
1630{
1631 struct section *sec;
1632 bool found = false;
1633
1634 /*
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001635 * Search for the following rodata sections, each of which can
1636 * potentially contain jump tables:
1637 *
1638 * - .rodata: can contain GCC switch tables
1639 * - .rodata.<func>: same, if -fdata-sections is being used
1640 * - .rodata..c_jump_table: contains C annotated jump tables
1641 *
1642 * .rodata.str1.* sections are ignored; they don't contain jump tables.
Allan Xavier4a60aa02018-09-07 08:12:01 -05001643 */
1644 for_each_sec(file, sec) {
Muchun Song1ee444702020-04-12 22:44:05 +08001645 if (!strncmp(sec->name, ".rodata", 7) &&
1646 !strstr(sec->name, ".str1.")) {
Allan Xavier4a60aa02018-09-07 08:12:01 -05001647 sec->rodata = true;
1648 found = true;
1649 }
1650 }
1651
1652 file->rodata = found;
1653}
1654
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001655static int decode_sections(struct objtool_file *file)
1656{
1657 int ret;
1658
Allan Xavier4a60aa02018-09-07 08:12:01 -05001659 mark_rodata(file);
1660
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001661 ret = decode_instructions(file);
1662 if (ret)
1663 return ret;
1664
1665 ret = add_dead_ends(file);
1666 if (ret)
1667 return ret;
1668
1669 add_ignores(file);
Peter Zijlstraea242132019-02-25 12:50:09 +01001670 add_uaccess_safe(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001671
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001672 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001673 if (ret)
1674 return ret;
1675
Peter Zijlstraf0bc12b2021-03-26 16:12:05 +01001676 /*
1677 * Must be before add_{jump_call}_destination.
1678 */
Peter Zijlstra5b06fd32020-08-18 15:57:49 +02001679 ret = read_static_call_tramps(file);
1680 if (ret)
1681 return ret;
1682
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001683 ret = add_jump_destinations(file);
1684 if (ret)
1685 return ret;
1686
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001687 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001688 if (ret)
1689 return ret;
1690
Peter Zijlstraf0bc12b2021-03-26 16:12:05 +01001691 /*
1692 * Must be before add_call_destination(); it changes INSN_CALL to
1693 * INSN_JUMP.
1694 */
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001695 ret = read_intra_function_calls(file);
1696 if (ret)
1697 return ret;
1698
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001699 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001700 if (ret)
1701 return ret;
1702
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001703 ret = add_jump_table_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001704 if (ret)
1705 return ret;
1706
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001707 ret = read_unwind_hints(file);
1708 if (ret)
1709 return ret;
1710
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001711 ret = read_retpoline_hints(file);
1712 if (ret)
1713 return ret;
1714
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001715 ret = read_instr_hints(file);
1716 if (ret)
1717 return ret;
1718
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001719 return 0;
1720}
1721
1722static bool is_fentry_call(struct instruction *insn)
1723{
Alexandre Chartre87cf61f2020-04-14 12:36:10 +02001724 if (insn->type == INSN_CALL && insn->call_dest &&
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001725 insn->call_dest->type == STT_NOTYPE &&
1726 !strcmp(insn->call_dest->name, "__fentry__"))
1727 return true;
1728
1729 return false;
1730}
1731
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001732static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001733{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001734 u8 ret_offset = insn->ret_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001735 struct cfi_state *cfi = &state->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001736 int i;
1737
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001738 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001739 return true;
1740
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001741 if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001742 return true;
1743
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001744 if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001745 return true;
1746
Alexandre Chartrec721b3f2020-04-07 09:31:35 +02001747 /*
1748 * If there is a ret offset hint then don't check registers
1749 * because a callee-saved register might have been pushed on
1750 * the stack.
1751 */
1752 if (ret_offset)
1753 return false;
1754
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001755 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001756 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1757 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001758 return true;
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001759 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001760
1761 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001762}
1763
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001764static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001765{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001766 struct cfi_state *cfi = &state->cfi;
1767
1768 if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
1769 cfi->regs[CFI_BP].offset == -16)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001770 return true;
1771
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001772 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001773 return true;
1774
1775 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001776}
1777
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001778static int update_cfi_state_regs(struct instruction *insn,
1779 struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001780 struct stack_op *op)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001781{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001782 struct cfi_reg *cfa = &cfi->cfa;
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001783
Josh Poimboeufd8dd25a2020-04-25 05:03:00 -05001784 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001785 return 0;
1786
1787 /* push */
Peter Zijlstraea242132019-02-25 12:50:09 +01001788 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001789 cfa->offset += 8;
1790
1791 /* pop */
Peter Zijlstraea242132019-02-25 12:50:09 +01001792 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001793 cfa->offset -= 8;
1794
1795 /* add immediate to sp */
1796 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1797 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1798 cfa->offset -= op->src.offset;
1799
1800 return 0;
1801}
1802
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001803static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001804{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001805 if (arch_callee_saved_reg(reg) &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001806 cfi->regs[reg].base == CFI_UNDEFINED) {
1807 cfi->regs[reg].base = base;
1808 cfi->regs[reg].offset = offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001809 }
1810}
1811
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001812static void restore_reg(struct cfi_state *cfi, unsigned char reg)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001813{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001814 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1815 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001816}
1817
1818/*
1819 * A note about DRAP stack alignment:
1820 *
1821 * GCC has the concept of a DRAP register, which is used to help keep track of
1822 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1823 * register. The typical DRAP pattern is:
1824 *
1825 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1826 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1827 * 41 ff 72 f8 pushq -0x8(%r10)
1828 * 55 push %rbp
1829 * 48 89 e5 mov %rsp,%rbp
1830 * (more pushes)
1831 * 41 52 push %r10
1832 * ...
1833 * 41 5a pop %r10
1834 * (more pops)
1835 * 5d pop %rbp
1836 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1837 * c3 retq
1838 *
1839 * There are some variations in the epilogues, like:
1840 *
1841 * 5b pop %rbx
1842 * 41 5a pop %r10
1843 * 41 5c pop %r12
1844 * 41 5d pop %r13
1845 * 41 5e pop %r14
1846 * c9 leaveq
1847 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1848 * c3 retq
1849 *
1850 * and:
1851 *
1852 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1853 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1854 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1855 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1856 * c9 leaveq
1857 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1858 * c3 retq
1859 *
1860 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1861 * restored beforehand:
1862 *
1863 * 41 55 push %r13
1864 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1865 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1866 * ...
1867 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1868 * 41 5d pop %r13
1869 * c3 retq
1870 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001871static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001872 struct stack_op *op)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001873{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001874 struct cfi_reg *cfa = &cfi->cfa;
1875 struct cfi_reg *regs = cfi->regs;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001876
1877 /* stack operations don't make sense with an undefined CFA */
1878 if (cfa->base == CFI_UNDEFINED) {
1879 if (insn->func) {
1880 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1881 return -1;
1882 }
1883 return 0;
1884 }
1885
Julien Thierryee819ae2020-09-04 16:30:27 +01001886 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
1887 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001888 return update_cfi_state_regs(insn, cfi, op);
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001889
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001890 switch (op->dest.type) {
1891
1892 case OP_DEST_REG:
1893 switch (op->src.type) {
1894
1895 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001896 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1897 cfa->base == CFI_SP &&
1898 regs[CFI_BP].base == CFI_CFA &&
1899 regs[CFI_BP].offset == -cfa->offset) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001900
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001901 /* mov %rsp, %rbp */
1902 cfa->base = op->dest.reg;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001903 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001904 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001905
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001906 else if (op->src.reg == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001907 op->dest.reg == CFI_BP && cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001908
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001909 /* drap: mov %rsp, %rbp */
1910 regs[CFI_BP].base = CFI_BP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001911 regs[CFI_BP].offset = -cfi->stack_size;
1912 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001913 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001914
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001915 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1916
1917 /*
1918 * mov %rsp, %reg
1919 *
1920 * This is needed for the rare case where GCC
1921 * does:
1922 *
1923 * mov %rsp, %rax
1924 * ...
1925 * mov %rax, %rsp
1926 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001927 cfi->vals[op->dest.reg].base = CFI_CFA;
1928 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001929 }
1930
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001931 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1932 cfa->base == CFI_BP) {
1933
1934 /*
1935 * mov %rbp, %rsp
1936 *
1937 * Restore the original stack pointer (Clang).
1938 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001939 cfi->stack_size = -cfi->regs[CFI_BP].offset;
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001940 }
1941
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001942 else if (op->dest.reg == cfa->base) {
1943
1944 /* mov %reg, %rsp */
1945 if (cfa->base == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001946 cfi->vals[op->src.reg].base == CFI_CFA) {
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001947
1948 /*
1949 * This is needed for the rare case
1950 * where GCC does something dumb like:
1951 *
1952 * lea 0x8(%rsp), %rcx
1953 * ...
1954 * mov %rcx, %rsp
1955 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001956 cfa->offset = -cfi->vals[op->src.reg].offset;
1957 cfi->stack_size = cfa->offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001958
1959 } else {
1960 cfa->base = CFI_UNDEFINED;
1961 cfa->offset = 0;
1962 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001963 }
1964
1965 break;
1966
1967 case OP_SRC_ADD:
1968 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1969
1970 /* add imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001971 cfi->stack_size -= op->src.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001972 if (cfa->base == CFI_SP)
1973 cfa->offset -= op->src.offset;
1974 break;
1975 }
1976
1977 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1978
1979 /* lea disp(%rbp), %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001980 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001981 break;
1982 }
1983
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001984 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001985
1986 /* drap: lea disp(%rsp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001987 cfi->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001988
1989 /*
1990 * lea disp(%rsp), %reg
1991 *
1992 * This is needed for the rare case where GCC
1993 * does something dumb like:
1994 *
1995 * lea 0x8(%rsp), %rcx
1996 * ...
1997 * mov %rcx, %rsp
1998 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001999 cfi->vals[op->dest.reg].base = CFI_CFA;
2000 cfi->vals[op->dest.reg].offset = \
2001 -cfi->stack_size + op->src.offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002002
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002003 break;
2004 }
2005
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002006 if (cfi->drap && op->dest.reg == CFI_SP &&
2007 op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002008
2009 /* drap: lea disp(%drap), %rsp */
2010 cfa->base = CFI_SP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002011 cfa->offset = cfi->stack_size = -op->src.offset;
2012 cfi->drap_reg = CFI_UNDEFINED;
2013 cfi->drap = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002014 break;
2015 }
2016
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002017 if (op->dest.reg == cfi->cfa.base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002018 WARN_FUNC("unsupported stack register modification",
2019 insn->sec, insn->offset);
2020 return -1;
2021 }
2022
2023 break;
2024
2025 case OP_SRC_AND:
2026 if (op->dest.reg != CFI_SP ||
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002027 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2028 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002029 WARN_FUNC("unsupported stack pointer realignment",
2030 insn->sec, insn->offset);
2031 return -1;
2032 }
2033
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002034 if (cfi->drap_reg != CFI_UNDEFINED) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002035 /* drap: and imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002036 cfa->base = cfi->drap_reg;
2037 cfa->offset = cfi->stack_size = 0;
2038 cfi->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002039 }
2040
2041 /*
2042 * Older versions of GCC (4.8ish) realign the stack
2043 * without DRAP, with a frame pointer.
2044 */
2045
2046 break;
2047
2048 case OP_SRC_POP:
Peter Zijlstraea242132019-02-25 12:50:09 +01002049 case OP_SRC_POPF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002050 if (!cfi->drap && op->dest.reg == cfa->base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002051
2052 /* pop %rbp */
2053 cfa->base = CFI_SP;
2054 }
2055
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002056 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2057 op->dest.reg == cfi->drap_reg &&
2058 cfi->drap_offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002059
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002060 /* drap: pop %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002061 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002062 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002063 cfi->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002064
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002065 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002066
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002067 /* pop %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002068 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002069 }
2070
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002071 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002072 if (cfa->base == CFI_SP)
2073 cfa->offset -= 8;
2074
2075 break;
2076
2077 case OP_SRC_REG_INDIRECT:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002078 if (cfi->drap && op->src.reg == CFI_BP &&
2079 op->src.offset == cfi->drap_offset) {
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002080
2081 /* drap: mov disp(%rbp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002082 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002083 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002084 cfi->drap_offset = -1;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002085 }
2086
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002087 if (cfi->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002088 op->src.offset == regs[op->dest.reg].offset) {
2089
2090 /* drap: mov disp(%rbp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002091 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002092
2093 } else if (op->src.reg == cfa->base &&
2094 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2095
2096 /* mov disp(%rbp), %reg */
2097 /* mov disp(%rsp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002098 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002099 }
2100
2101 break;
2102
2103 default:
2104 WARN_FUNC("unknown stack-related instruction",
2105 insn->sec, insn->offset);
2106 return -1;
2107 }
2108
2109 break;
2110
2111 case OP_DEST_PUSH:
Peter Zijlstraea242132019-02-25 12:50:09 +01002112 case OP_DEST_PUSHF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002113 cfi->stack_size += 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002114 if (cfa->base == CFI_SP)
2115 cfa->offset += 8;
2116
2117 if (op->src.type != OP_SRC_REG)
2118 break;
2119
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002120 if (cfi->drap) {
2121 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002122
2123 /* drap: push %drap */
2124 cfa->base = CFI_BP_INDIRECT;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002125 cfa->offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002126
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002127 /* save drap so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002128 cfi->drap_offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002129
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002130 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002131
2132 /* drap: push %rbp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002133 cfi->stack_size = 0;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002134
Julien Thierryf4f80392020-09-15 08:53:16 +01002135 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002136
2137 /* drap: push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002138 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002139 }
2140
2141 } else {
2142
2143 /* push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002144 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002145 }
2146
2147 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002148 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002149 cfa->base != CFI_BP)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002150 cfi->bp_scratch = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002151 break;
2152
2153 case OP_DEST_REG_INDIRECT:
2154
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002155 if (cfi->drap) {
2156 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002157
2158 /* drap: mov %drap, disp(%rbp) */
2159 cfa->base = CFI_BP_INDIRECT;
2160 cfa->offset = op->dest.offset;
2161
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002162 /* save drap offset so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002163 cfi->drap_offset = op->dest.offset;
Julien Thierryf4f80392020-09-15 08:53:16 +01002164 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002165
2166 /* drap: mov reg, disp(%rbp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002167 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002168 }
2169
2170 } else if (op->dest.reg == cfa->base) {
2171
2172 /* mov reg, disp(%rbp) */
2173 /* mov reg, disp(%rsp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002174 save_reg(cfi, op->src.reg, CFI_CFA,
2175 op->dest.offset - cfi->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002176 }
2177
2178 break;
2179
2180 case OP_DEST_LEAVE:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002181 if ((!cfi->drap && cfa->base != CFI_BP) ||
2182 (cfi->drap && cfa->base != cfi->drap_reg)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002183 WARN_FUNC("leave instruction with modified stack frame",
2184 insn->sec, insn->offset);
2185 return -1;
2186 }
2187
2188 /* leave (mov %rbp, %rsp; pop %rbp) */
2189
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002190 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2191 restore_reg(cfi, CFI_BP);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002192
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002193 if (!cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002194 cfa->base = CFI_SP;
2195 cfa->offset -= 8;
2196 }
2197
2198 break;
2199
2200 case OP_DEST_MEM:
Peter Zijlstraea242132019-02-25 12:50:09 +01002201 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002202 WARN_FUNC("unknown stack-related memory operation",
2203 insn->sec, insn->offset);
2204 return -1;
2205 }
2206
2207 /* pop mem */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002208 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002209 if (cfa->base == CFI_SP)
2210 cfa->offset -= 8;
2211
2212 break;
2213
2214 default:
2215 WARN_FUNC("unknown stack-related instruction",
2216 insn->sec, insn->offset);
2217 return -1;
2218 }
2219
2220 return 0;
2221}
2222
Julien Thierry65ea47d2020-03-27 15:28:47 +00002223static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2224{
2225 struct stack_op *op;
2226
2227 list_for_each_entry(op, &insn->stack_ops, list) {
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002228 struct cfi_state old_cfi = state->cfi;
Julien Thierry65ea47d2020-03-27 15:28:47 +00002229 int res;
2230
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002231 res = update_cfi_state(insn, &state->cfi, op);
Julien Thierry65ea47d2020-03-27 15:28:47 +00002232 if (res)
2233 return res;
2234
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002235 if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) {
2236 WARN_FUNC("alternative modifies stack", insn->sec, insn->offset);
2237 return -1;
2238 }
2239
Julien Thierry65ea47d2020-03-27 15:28:47 +00002240 if (op->dest.type == OP_DEST_PUSHF) {
2241 if (!state->uaccess_stack) {
2242 state->uaccess_stack = 1;
2243 } else if (state->uaccess_stack >> 31) {
2244 WARN_FUNC("PUSHF stack exhausted",
2245 insn->sec, insn->offset);
2246 return 1;
2247 }
2248 state->uaccess_stack <<= 1;
2249 state->uaccess_stack |= state->uaccess;
2250 }
2251
2252 if (op->src.type == OP_SRC_POPF) {
2253 if (state->uaccess_stack) {
2254 state->uaccess = state->uaccess_stack & 1;
2255 state->uaccess_stack >>= 1;
2256 if (state->uaccess_stack == 1)
2257 state->uaccess_stack = 0;
2258 }
2259 }
2260 }
2261
2262 return 0;
2263}
2264
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002265static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002266{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002267 struct cfi_state *cfi1 = &insn->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002268 int i;
2269
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002270 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2271
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002272 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2273 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002274 cfi1->cfa.base, cfi1->cfa.offset,
2275 cfi2->cfa.base, cfi2->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002276
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002277 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002278 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002279 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002280 sizeof(struct cfi_reg)))
2281 continue;
2282
2283 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2284 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002285 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2286 i, cfi2->regs[i].base, cfi2->regs[i].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002287 break;
2288 }
2289
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002290 } else if (cfi1->type != cfi2->type) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002291
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002292 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2293 insn->sec, insn->offset, cfi1->type, cfi2->type);
2294
2295 } else if (cfi1->drap != cfi2->drap ||
2296 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2297 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2298
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002299 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002300 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002301 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2302 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002303
2304 } else
2305 return true;
2306
2307 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002308}
2309
Peter Zijlstraea242132019-02-25 12:50:09 +01002310static inline bool func_uaccess_safe(struct symbol *func)
2311{
2312 if (func)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002313 return func->uaccess_safe;
Peter Zijlstraea242132019-02-25 12:50:09 +01002314
2315 return false;
2316}
2317
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002318static inline const char *call_dest_name(struct instruction *insn)
Peter Zijlstraea242132019-02-25 12:50:09 +01002319{
2320 if (insn->call_dest)
2321 return insn->call_dest->name;
2322
2323 return "{dynamic}";
2324}
2325
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002326static inline bool noinstr_call_dest(struct symbol *func)
2327{
2328 /*
2329 * We can't deal with indirect function calls at present;
2330 * assume they're instrumented.
2331 */
2332 if (!func)
2333 return false;
2334
2335 /*
2336 * If the symbol is from a noinstr section; we good.
2337 */
2338 if (func->sec->noinstr)
2339 return true;
2340
2341 /*
2342 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2343 * something 'BAD' happened. At the risk of taking the machine down,
2344 * let them proceed to get the message out.
2345 */
2346 if (!strncmp(func->name, "__ubsan_handle_", 15))
2347 return true;
2348
2349 return false;
2350}
2351
Peter Zijlstraea242132019-02-25 12:50:09 +01002352static int validate_call(struct instruction *insn, struct insn_state *state)
2353{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002354 if (state->noinstr && state->instr <= 0 &&
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002355 !noinstr_call_dest(insn->call_dest)) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002356 WARN_FUNC("call to %s() leaves .noinstr.text section",
2357 insn->sec, insn->offset, call_dest_name(insn));
2358 return 1;
2359 }
2360
Peter Zijlstraea242132019-02-25 12:50:09 +01002361 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2362 WARN_FUNC("call to %s() with UACCESS enabled",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002363 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstraea242132019-02-25 12:50:09 +01002364 return 1;
2365 }
2366
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002367 if (state->df) {
2368 WARN_FUNC("call to %s() with DF set",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002369 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002370 return 1;
2371 }
2372
Peter Zijlstraea242132019-02-25 12:50:09 +01002373 return 0;
2374}
2375
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002376static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2377{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002378 if (has_modified_stack_frame(insn, state)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002379 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2380 insn->sec, insn->offset);
2381 return 1;
2382 }
2383
Peter Zijlstraea242132019-02-25 12:50:09 +01002384 return validate_call(insn, state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002385}
2386
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002387static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2388{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002389 if (state->noinstr && state->instr > 0) {
2390 WARN_FUNC("return with instrumentation enabled",
2391 insn->sec, insn->offset);
2392 return 1;
2393 }
2394
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002395 if (state->uaccess && !func_uaccess_safe(func)) {
2396 WARN_FUNC("return with UACCESS enabled",
2397 insn->sec, insn->offset);
2398 return 1;
2399 }
2400
2401 if (!state->uaccess && func_uaccess_safe(func)) {
2402 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2403 insn->sec, insn->offset);
2404 return 1;
2405 }
2406
2407 if (state->df) {
2408 WARN_FUNC("return with DF set",
2409 insn->sec, insn->offset);
2410 return 1;
2411 }
2412
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002413 if (func && has_modified_stack_frame(insn, state)) {
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002414 WARN_FUNC("return with modified stack frame",
2415 insn->sec, insn->offset);
2416 return 1;
2417 }
2418
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002419 if (state->cfi.bp_scratch) {
Josh Poimboeufb2966952020-04-01 13:23:29 -05002420 WARN_FUNC("BP used as a scratch register",
2421 insn->sec, insn->offset);
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002422 return 1;
2423 }
2424
2425 return 0;
2426}
2427
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002428/*
Peter Zijlstra7117f162020-04-28 19:37:01 +02002429 * Alternatives should not contain any ORC entries, this in turn means they
2430 * should not contain any CFI ops, which implies all instructions should have
2431 * the same same CFI state.
2432 *
2433 * It is possible to constuct alternatives that have unreachable holes that go
2434 * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2435 * states which then results in ORC entries, which we just said we didn't want.
2436 *
2437 * Avoid them by copying the CFI entry of the first instruction into the whole
2438 * alternative.
2439 */
2440static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn)
2441{
2442 struct instruction *first_insn = insn;
2443 int alt_group = insn->alt_group;
2444
2445 sec_for_each_insn_continue(file, insn) {
2446 if (insn->alt_group != alt_group)
2447 break;
2448 insn->cfi = first_insn->cfi;
2449 }
2450}
2451
2452/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002453 * Follow the branch starting at the given instruction, and recursively follow
2454 * any other branches (jumps). Meanwhile, track the frame pointer state at
2455 * each instruction and validate all the rules described in
2456 * tools/objtool/Documentation/stack-validation.txt.
2457 */
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002458static int validate_branch(struct objtool_file *file, struct symbol *func,
Peter Zijlstrab7460462020-04-02 10:15:51 +02002459 struct instruction *insn, struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002460{
2461 struct alternative *alt;
Peter Zijlstrab7460462020-04-02 10:15:51 +02002462 struct instruction *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002463 struct section *sec;
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002464 u8 visited;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002465 int ret;
2466
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002467 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002468
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002469 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002470 next_insn = next_insn_same_sec(file, insn);
2471
Josh Poimboeuf13810432018-05-09 22:39:15 -05002472 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05002473 WARN("%s() falls through to next function %s()",
2474 func->name, insn->func->name);
2475 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002476 }
2477
Josh Poimboeuf48550222017-07-07 09:19:42 -05002478 if (func && insn->ignore) {
2479 WARN_FUNC("BUG: why am I validating an ignored function?",
2480 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05002481 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05002482 }
2483
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002484 visited = 1 << state.uaccess;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002485 if (insn->visited) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002486 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002487 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002488
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002489 if (insn->visited & visited)
Peter Zijlstraea242132019-02-25 12:50:09 +01002490 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002491 }
2492
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002493 if (state.noinstr)
2494 state.instr += insn->instr;
2495
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002496 if (insn->hint)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002497 state.cfi = insn->cfi;
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002498 else
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002499 insn->cfi = state.cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002500
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002501 insn->visited |= visited;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002502
Peter Zijlstra7117f162020-04-28 19:37:01 +02002503 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002504 bool skip_orig = false;
2505
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002506 list_for_each_entry(alt, &insn->alts, list) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002507 if (alt->skip_orig)
2508 skip_orig = true;
2509
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002510 ret = validate_branch(file, func, alt->insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002511 if (ret) {
2512 if (backtrace)
2513 BT_FUNC("(alt)", insn);
2514 return ret;
2515 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002516 }
Peter Zijlstra764eef42019-03-01 11:19:03 +01002517
Peter Zijlstra7117f162020-04-28 19:37:01 +02002518 if (insn->alt_group)
2519 fill_alternative_cfi(file, insn);
2520
Peter Zijlstra764eef42019-03-01 11:19:03 +01002521 if (skip_orig)
2522 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002523 }
2524
Peter Zijlstra60041bc2020-04-24 16:16:41 +02002525 if (handle_insn_ops(insn, &state))
2526 return 1;
2527
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002528 switch (insn->type) {
2529
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002530 case INSN_RETURN:
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002531 return validate_return(func, insn, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002532
2533 case INSN_CALL:
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002534 case INSN_CALL_DYNAMIC:
Peter Zijlstraea242132019-02-25 12:50:09 +01002535 ret = validate_call(insn, &state);
2536 if (ret)
2537 return ret;
2538
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002539 if (!no_fp && func && !is_fentry_call(insn) &&
2540 !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002541 WARN_FUNC("call without frame pointer save/setup",
2542 sec, insn->offset);
2543 return 1;
2544 }
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002545
2546 if (dead_end_function(file, insn->call_dest))
2547 return 0;
2548
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002549 break;
2550
2551 case INSN_JUMP_CONDITIONAL:
2552 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002553 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002554 ret = validate_sibling_call(insn, &state);
2555 if (ret)
2556 return ret;
2557
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002558 } else if (insn->jump_dest) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002559 ret = validate_branch(file, func,
2560 insn->jump_dest, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002561 if (ret) {
2562 if (backtrace)
2563 BT_FUNC("(branch)", insn);
2564 return ret;
2565 }
Josh Poimboeuf48550222017-07-07 09:19:42 -05002566 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002567
2568 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2569 return 0;
2570
2571 break;
2572
2573 case INSN_JUMP_DYNAMIC:
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002574 case INSN_JUMP_DYNAMIC_CONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002575 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002576 ret = validate_sibling_call(insn, &state);
2577 if (ret)
2578 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002579 }
2580
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002581 if (insn->type == INSN_JUMP_DYNAMIC)
2582 return 0;
2583
2584 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002585
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002586 case INSN_CONTEXT_SWITCH:
2587 if (func && (!next_insn || !next_insn->hint)) {
2588 WARN_FUNC("unsupported instruction in callable function",
2589 sec, insn->offset);
2590 return 1;
2591 }
2592 return 0;
2593
Peter Zijlstraea242132019-02-25 12:50:09 +01002594 case INSN_STAC:
2595 if (state.uaccess) {
2596 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2597 return 1;
2598 }
2599
2600 state.uaccess = true;
2601 break;
2602
2603 case INSN_CLAC:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002604 if (!state.uaccess && func) {
Peter Zijlstraea242132019-02-25 12:50:09 +01002605 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2606 return 1;
2607 }
2608
2609 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2610 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2611 return 1;
2612 }
2613
2614 state.uaccess = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002615 break;
2616
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002617 case INSN_STD:
Josh Poimboeuf9e06f362021-01-21 15:29:17 -06002618 if (state.df) {
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002619 WARN_FUNC("recursive STD", sec, insn->offset);
Josh Poimboeuf9e06f362021-01-21 15:29:17 -06002620 return 1;
2621 }
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002622
2623 state.df = true;
2624 break;
2625
2626 case INSN_CLD:
Josh Poimboeuf9e06f362021-01-21 15:29:17 -06002627 if (!state.df && func) {
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002628 WARN_FUNC("redundant CLD", sec, insn->offset);
Josh Poimboeuf9e06f362021-01-21 15:29:17 -06002629 return 1;
2630 }
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002631
2632 state.df = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002633 break;
2634
2635 default:
2636 break;
2637 }
2638
2639 if (insn->dead_end)
2640 return 0;
2641
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002642 if (!next_insn) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002643 if (state.cfi.cfa.base == CFI_UNDEFINED)
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002644 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002645 WARN("%s: unexpected end of section", sec->name);
2646 return 1;
2647 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002648
2649 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002650 }
2651
2652 return 0;
2653}
2654
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002655static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002656{
2657 struct instruction *insn;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002658 struct insn_state state;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002659 int ret, warnings = 0;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002660
2661 if (!file->hints)
2662 return 0;
2663
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002664 init_insn_state(&state, sec);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002665
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002666 if (sec) {
2667 insn = find_insn(file, sec, 0);
2668 if (!insn)
2669 return 0;
2670 } else {
2671 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2672 }
2673
2674 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002675 if (insn->hint && !insn->visited) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002676 ret = validate_branch(file, insn->func, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002677 if (ret && backtrace)
2678 BT_FUNC("<=== (hint)", insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002679 warnings += ret;
2680 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002681
2682 insn = list_next_entry(insn, list);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002683 }
2684
2685 return warnings;
2686}
2687
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002688static int validate_retpoline(struct objtool_file *file)
2689{
2690 struct instruction *insn;
2691 int warnings = 0;
2692
2693 for_each_insn(file, insn) {
2694 if (insn->type != INSN_JUMP_DYNAMIC &&
2695 insn->type != INSN_CALL_DYNAMIC)
2696 continue;
2697
2698 if (insn->retpoline_safe)
2699 continue;
2700
Peter Zijlstraca41b972018-01-31 10:18:28 +01002701 /*
2702 * .init.text code is ran before userspace and thus doesn't
2703 * strictly need retpolines, except for modules which are
2704 * loaded late, they very much do need retpoline in their
2705 * .init.text
2706 */
2707 if (!strcmp(insn->sec->name, ".init.text") && !module)
2708 continue;
2709
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002710 WARN_FUNC("indirect %s found in RETPOLINE build",
2711 insn->sec, insn->offset,
2712 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2713
2714 warnings++;
2715 }
2716
2717 return warnings;
2718}
2719
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002720static bool is_kasan_insn(struct instruction *insn)
2721{
2722 return (insn->type == INSN_CALL &&
2723 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2724}
2725
2726static bool is_ubsan_insn(struct instruction *insn)
2727{
2728 return (insn->type == INSN_CALL &&
2729 !strcmp(insn->call_dest->name,
2730 "__ubsan_handle_builtin_unreachable"));
2731}
2732
Ilie Halip14db1f02020-09-19 09:41:18 +03002733static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002734{
2735 int i;
Ilie Halip14db1f02020-09-19 09:41:18 +03002736 struct instruction *prev_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002737
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002738 if (insn->ignore || insn->type == INSN_NOP)
2739 return true;
2740
2741 /*
2742 * Ignore any unused exceptions. This can happen when a whitelisted
2743 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002744 *
2745 * Also ignore alternative replacement instructions. This can happen
2746 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002747 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002748 if (!strcmp(insn->sec->name, ".fixup") ||
2749 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2750 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002751 return true;
2752
Julien Thierryfb136212020-09-15 08:53:17 +01002753 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->offset == FAKE_JUMP_OFFSET)
2754 return true;
2755
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002756 if (!insn->func)
2757 return false;
2758
2759 /*
2760 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2761 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2762 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2763 * (or occasionally a JMP to UD2).
Ilie Halip14db1f02020-09-19 09:41:18 +03002764 *
2765 * It may also insert a UD2 after calling a __noreturn function.
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002766 */
Ilie Halip14db1f02020-09-19 09:41:18 +03002767 prev_insn = list_prev_entry(insn, list);
2768 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002769 (insn->type == INSN_BUG ||
2770 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2771 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2772 return true;
2773
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002774 /*
2775 * Check if this (or a subsequent) instruction is related to
2776 * CONFIG_UBSAN or CONFIG_KASAN.
2777 *
2778 * End the search at 5 instructions to avoid going into the weeds.
2779 */
2780 for (i = 0; i < 5; i++) {
2781
2782 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2783 return true;
2784
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002785 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2786 if (insn->jump_dest &&
2787 insn->jump_dest->func == insn->func) {
2788 insn = insn->jump_dest;
2789 continue;
2790 }
2791
2792 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002793 }
2794
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002795 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002796 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002797
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002798 insn = list_next_entry(insn, list);
2799 }
2800
2801 return false;
2802}
2803
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002804static int validate_symbol(struct objtool_file *file, struct section *sec,
2805 struct symbol *sym, struct insn_state *state)
2806{
2807 struct instruction *insn;
2808 int ret;
2809
2810 if (!sym->len) {
2811 WARN("%s() is missing an ELF size annotation", sym->name);
2812 return 1;
2813 }
2814
2815 if (sym->pfunc != sym || sym->alias != sym)
2816 return 0;
2817
2818 insn = find_insn(file, sec, sym->offset);
2819 if (!insn || insn->ignore || insn->visited)
2820 return 0;
2821
2822 state->uaccess = sym->uaccess_safe;
2823
2824 ret = validate_branch(file, insn->func, insn, *state);
2825 if (ret && backtrace)
2826 BT_FUNC("<=== (sym)", insn);
2827 return ret;
2828}
2829
Peter Zijlstra350994b2020-03-23 20:57:13 +01002830static int validate_section(struct objtool_file *file, struct section *sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002831{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002832 struct insn_state state;
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002833 struct symbol *func;
2834 int warnings = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002835
Peter Zijlstra350994b2020-03-23 20:57:13 +01002836 list_for_each_entry(func, &sec->symbol_list, list) {
2837 if (func->type != STT_FUNC)
2838 continue;
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002839
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002840 init_insn_state(&state, sec);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002841 state.cfi.cfa = initial_func_cfi.cfa;
2842 memcpy(&state.cfi.regs, &initial_func_cfi.regs,
Julien Thierry0699e552020-03-27 15:28:40 +00002843 CFI_NUM_REGS * sizeof(struct cfi_reg));
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002844 state.cfi.stack_size = initial_func_cfi.cfa.offset;
Julien Thierry0699e552020-03-27 15:28:40 +00002845
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002846 warnings += validate_symbol(file, sec, func, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002847 }
2848
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002849 return warnings;
2850}
2851
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002852static int validate_vmlinux_functions(struct objtool_file *file)
2853{
2854 struct section *sec;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002855 int warnings = 0;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002856
2857 sec = find_section_by_name(file->elf, ".noinstr.text");
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002858 if (sec) {
2859 warnings += validate_section(file, sec);
2860 warnings += validate_unwind_hints(file, sec);
2861 }
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002862
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002863 sec = find_section_by_name(file->elf, ".entry.text");
2864 if (sec) {
2865 warnings += validate_section(file, sec);
2866 warnings += validate_unwind_hints(file, sec);
2867 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002868
2869 return warnings;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002870}
2871
Peter Zijlstra350994b2020-03-23 20:57:13 +01002872static int validate_functions(struct objtool_file *file)
2873{
2874 struct section *sec;
2875 int warnings = 0;
2876
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002877 for_each_sec(file, sec) {
2878 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2879 continue;
2880
Peter Zijlstra350994b2020-03-23 20:57:13 +01002881 warnings += validate_section(file, sec);
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002882 }
Peter Zijlstra350994b2020-03-23 20:57:13 +01002883
2884 return warnings;
2885}
2886
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002887static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002888{
2889 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002890
2891 if (file->ignore_unreachables)
2892 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002893
2894 for_each_insn(file, insn) {
Ilie Halip14db1f02020-09-19 09:41:18 +03002895 if (insn->visited || ignore_unreachable_insn(file, insn))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002896 continue;
2897
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002898 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2899 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002900 }
2901
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002902 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002903}
2904
Julien Thierryd44becb2020-08-25 13:47:40 +01002905int check(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002906{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002907 int ret, warnings = 0;
2908
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002909 arch_initial_func_cfi_state(&initial_func_cfi);
2910
Julien Thierry6545eb02020-08-25 13:47:39 +01002911 ret = decode_sections(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002912 if (ret < 0)
2913 goto out;
2914 warnings += ret;
2915
Julien Thierry6545eb02020-08-25 13:47:39 +01002916 if (list_empty(&file->insn_list))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002917 goto out;
2918
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002919 if (vmlinux && !validate_dup) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002920 ret = validate_vmlinux_functions(file);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002921 if (ret < 0)
2922 goto out;
2923
2924 warnings += ret;
2925 goto out;
2926 }
2927
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002928 if (retpoline) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002929 ret = validate_retpoline(file);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002930 if (ret < 0)
2931 return ret;
2932 warnings += ret;
2933 }
2934
Julien Thierry6545eb02020-08-25 13:47:39 +01002935 ret = validate_functions(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002936 if (ret < 0)
2937 goto out;
2938 warnings += ret;
2939
Julien Thierry6545eb02020-08-25 13:47:39 +01002940 ret = validate_unwind_hints(file, NULL);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002941 if (ret < 0)
2942 goto out;
2943 warnings += ret;
2944
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002945 if (!warnings) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002946 ret = validate_reachable_instructions(file);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002947 if (ret < 0)
2948 goto out;
2949 warnings += ret;
2950 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002951
Julien Thierry6545eb02020-08-25 13:47:39 +01002952 ret = create_static_call_sections(file);
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002953 if (ret < 0)
2954 goto out;
2955 warnings += ret;
2956
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002957out:
Josh Poimboeuf9c8bb3e2021-01-14 16:32:42 -06002958 /*
2959 * For now, don't fail the kernel build on fatal warnings. These
2960 * errors are still fairly common due to the growing matrix of
2961 * supported toolchains and their recent pace of change.
2962 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002963 return 0;
2964}