blob: c6ab44543c92aea9999010b17931ac4430a14331 [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 Poimboeufdcc914f2017-06-28 10:11:05 -0500159 };
160
Josh Poimboeufc9bab222019-07-17 20:36:51 -0500161 if (!func)
162 return false;
163
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500164 if (func->bind == STB_WEAK)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500165 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500166
167 if (func->bind == STB_GLOBAL)
168 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
169 if (!strcmp(func->name, global_noreturns[i]))
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500170 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500171
Josh Poimboeuf13810432018-05-09 22:39:15 -0500172 if (!func->len)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500173 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500174
Josh Poimboeuf13810432018-05-09 22:39:15 -0500175 insn = find_insn(file, func->sec, func->offset);
176 if (!insn->func)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500177 return false;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500178
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100179 func_for_each_insn(file, func, insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500180 empty = false;
181
182 if (insn->type == INSN_RETURN)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500183 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500184 }
185
186 if (empty)
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500187 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500188
189 /*
190 * A function can have a sibling call instead of a return. In that
191 * case, the function's dead-end status depends on whether the target
192 * of the sibling call returns.
193 */
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100194 func_for_each_insn(file, func, insn) {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500195 if (is_sibling_call(insn)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500196 struct instruction *dest = insn->jump_dest;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500197
198 if (!dest)
199 /* sibling call to another file */
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500200 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500201
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500202 /* local sibling call */
203 if (recursion == 5) {
204 /*
205 * Infinite recursion: two functions have
206 * sibling calls to each other. This is a very
207 * rare case. It means they aren't dead ends.
208 */
209 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500210 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500211
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500212 return __dead_end_function(file, dest->func, recursion+1);
213 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500214 }
215
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500216 return true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500217}
218
Josh Poimboeuf8e25c9f2019-07-17 20:36:50 -0500219static bool dead_end_function(struct objtool_file *file, struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500220{
221 return __dead_end_function(file, func, 0);
222}
223
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100224static void init_cfi_state(struct cfi_state *cfi)
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500225{
226 int i;
227
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500228 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100229 cfi->regs[i].base = CFI_UNDEFINED;
230 cfi->vals[i].base = CFI_UNDEFINED;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -0500231 }
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100232 cfi->cfa.base = CFI_UNDEFINED;
233 cfi->drap_reg = CFI_UNDEFINED;
234 cfi->drap_offset = -1;
235}
236
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100237static void init_insn_state(struct insn_state *state, struct section *sec)
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100238{
239 memset(state, 0, sizeof(*state));
240 init_cfi_state(&state->cfi);
Peter Zijlstra932f8e92020-03-23 18:26:03 +0100241
242 /*
243 * We need the full vmlinux for noinstr validation, otherwise we can
244 * not correctly determine insn->call_dest->sec (external symbols do
245 * not have a section).
246 */
247 if (vmlinux && sec)
248 state->noinstr = sec->noinstr;
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500249}
250
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500251/*
252 * Call the arch-specific instruction decoder for all the instructions and add
253 * them to the global instruction list.
254 */
255static int decode_instructions(struct objtool_file *file)
256{
257 struct section *sec;
258 struct symbol *func;
259 unsigned long offset;
260 struct instruction *insn;
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100261 unsigned long nr_insns = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500262 int ret;
263
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500264 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500265
266 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
267 continue;
268
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500269 if (strcmp(sec->name, ".altinstr_replacement") &&
270 strcmp(sec->name, ".altinstr_aux") &&
271 strncmp(sec->name, ".discard.", 9))
272 sec->text = true;
273
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +0100274 if (!strcmp(sec->name, ".noinstr.text") ||
275 !strcmp(sec->name, ".entry.text"))
Peter Zijlstrac4a33932020-03-10 18:57:41 +0100276 sec->noinstr = true;
277
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500278 for (offset = 0; offset < sec->len; offset += insn->len) {
279 insn = malloc(sizeof(*insn));
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500280 if (!insn) {
281 WARN("malloc failed");
282 return -1;
283 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500284 memset(insn, 0, sizeof(*insn));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500285 INIT_LIST_HEAD(&insn->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +0000286 INIT_LIST_HEAD(&insn->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100287 init_cfi_state(&insn->cfi);
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500288
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500289 insn->sec = sec;
290 insn->offset = offset;
291
292 ret = arch_decode_instruction(file->elf, sec, offset,
293 sec->len - offset,
294 &insn->len, &insn->type,
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500295 &insn->immediate,
Julien Thierry65ea47d2020-03-27 15:28:47 +0000296 &insn->stack_ops);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500297 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500298 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500299
Peter Zijlstra87ecb582020-03-16 15:47:27 +0100300 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500301 list_add_tail(&insn->list, &file->insn_list);
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100302 nr_insns++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500303 }
304
305 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500306 if (func->type != STT_FUNC || func->alias != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500307 continue;
308
309 if (!find_insn(file, sec, func->offset)) {
310 WARN("%s(): can't find starting instruction",
311 func->name);
312 return -1;
313 }
314
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100315 sym_for_each_insn(file, func, insn)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500316 insn->func = func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500317 }
318 }
319
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100320 if (stats)
321 printf("nr_insns: %lu\n", nr_insns);
322
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500323 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500324
325err:
326 free(insn);
327 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500328}
329
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700330static struct instruction *find_last_insn(struct objtool_file *file,
331 struct section *sec)
332{
333 struct instruction *insn = NULL;
334 unsigned int offset;
335 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
336
337 for (offset = sec->len - 1; offset >= end && !insn; offset--)
338 insn = find_insn(file, sec, offset);
339
340 return insn;
341}
342
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500343/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500344 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500345 */
346static int add_dead_ends(struct objtool_file *file)
347{
348 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700349 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500350 struct instruction *insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500351
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500352 /*
353 * By default, "ud2" is a dead end unless otherwise annotated, because
354 * GCC 7 inserts it for certain divide-by-zero cases.
355 */
356 for_each_insn(file, insn)
357 if (insn->type == INSN_BUG)
358 insn->dead_end = true;
359
360 /*
361 * Check for manually annotated dead ends.
362 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500363 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
364 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500365 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500366
Matt Helsleyf1974222020-05-29 14:01:13 -0700367 list_for_each_entry(reloc, &sec->reloc_list, list) {
368 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500369 WARN("unexpected relocation symbol type in %s", sec->name);
370 return -1;
371 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700372 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500373 if (insn)
374 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700375 else if (reloc->addend == reloc->sym->sec->len) {
376 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700377 if (!insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500378 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700379 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500380 return -1;
381 }
382 } else {
383 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700384 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500385 return -1;
386 }
387
388 insn->dead_end = true;
389 }
390
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500391reachable:
392 /*
393 * These manually annotated reachable checks are needed for GCC 4.4,
394 * where the Linux unreachable() macro isn't supported. In that case
395 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
396 * not a dead end.
397 */
398 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
399 if (!sec)
400 return 0;
401
Matt Helsleyf1974222020-05-29 14:01:13 -0700402 list_for_each_entry(reloc, &sec->reloc_list, list) {
403 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500404 WARN("unexpected relocation symbol type in %s", sec->name);
405 return -1;
406 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700407 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500408 if (insn)
409 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700410 else if (reloc->addend == reloc->sym->sec->len) {
411 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700412 if (!insn) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500413 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700414 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500415 return -1;
416 }
417 } else {
418 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700419 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500420 return -1;
421 }
422
423 insn->dead_end = false;
424 }
425
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500426 return 0;
427}
428
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200429static int create_static_call_sections(struct objtool_file *file)
430{
431 struct section *sec, *reloc_sec;
432 struct reloc *reloc;
433 struct static_call_site *site;
434 struct instruction *insn;
435 struct symbol *key_sym;
436 char *key_name, *tmp;
437 int idx;
438
439 sec = find_section_by_name(file->elf, ".static_call_sites");
440 if (sec) {
441 INIT_LIST_HEAD(&file->static_call_list);
442 WARN("file already has .static_call_sites section, skipping");
443 return 0;
444 }
445
446 if (list_empty(&file->static_call_list))
447 return 0;
448
449 idx = 0;
450 list_for_each_entry(insn, &file->static_call_list, static_call_node)
451 idx++;
452
453 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
454 sizeof(struct static_call_site), idx);
455 if (!sec)
456 return -1;
457
458 reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
459 if (!reloc_sec)
460 return -1;
461
462 idx = 0;
463 list_for_each_entry(insn, &file->static_call_list, static_call_node) {
464
465 site = (struct static_call_site *)sec->data->d_buf + idx;
466 memset(site, 0, sizeof(struct static_call_site));
467
468 /* populate reloc for 'addr' */
469 reloc = malloc(sizeof(*reloc));
470 if (!reloc) {
471 perror("malloc");
472 return -1;
473 }
474 memset(reloc, 0, sizeof(*reloc));
475 reloc->sym = insn->sec->sym;
476 reloc->addend = insn->offset;
477 reloc->type = R_X86_64_PC32;
478 reloc->offset = idx * sizeof(struct static_call_site);
479 reloc->sec = reloc_sec;
480 elf_add_reloc(file->elf, reloc);
481
482 /* find key symbol */
483 key_name = strdup(insn->call_dest->name);
484 if (!key_name) {
485 perror("strdup");
486 return -1;
487 }
488 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
489 STATIC_CALL_TRAMP_PREFIX_LEN)) {
490 WARN("static_call: trampoline name malformed: %s", key_name);
491 return -1;
492 }
493 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
494 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
495
496 key_sym = find_symbol_by_name(file->elf, tmp);
497 if (!key_sym) {
498 WARN("static_call: can't find static_call_key symbol: %s", tmp);
499 return -1;
500 }
501 free(key_name);
502
503 /* populate reloc for 'key' */
504 reloc = malloc(sizeof(*reloc));
505 if (!reloc) {
506 perror("malloc");
507 return -1;
508 }
509 memset(reloc, 0, sizeof(*reloc));
510 reloc->sym = key_sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200511 reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200512 reloc->type = R_X86_64_PC32;
513 reloc->offset = idx * sizeof(struct static_call_site) + 4;
514 reloc->sec = reloc_sec;
515 elf_add_reloc(file->elf, reloc);
516
517 idx++;
518 }
519
520 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
521 return -1;
522
523 return 0;
524}
525
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500526/*
527 * Warnings shouldn't be reported for ignored functions.
528 */
529static void add_ignores(struct objtool_file *file)
530{
531 struct instruction *insn;
532 struct section *sec;
533 struct symbol *func;
Matt Helsleyf1974222020-05-29 14:01:13 -0700534 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500535
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100536 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
537 if (!sec)
538 return;
539
Matt Helsleyf1974222020-05-29 14:01:13 -0700540 list_for_each_entry(reloc, &sec->reloc_list, list) {
541 switch (reloc->sym->type) {
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100542 case STT_FUNC:
Matt Helsleyf1974222020-05-29 14:01:13 -0700543 func = reloc->sym;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100544 break;
545
546 case STT_SECTION:
Matt Helsleyf1974222020-05-29 14:01:13 -0700547 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600548 if (!func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500549 continue;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100550 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500551
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100552 default:
Matt Helsleyf1974222020-05-29 14:01:13 -0700553 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100554 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500555 }
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100556
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100557 func_for_each_insn(file, func, insn)
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100558 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500559 }
560}
561
562/*
Peter Zijlstraea242132019-02-25 12:50:09 +0100563 * This is a whitelist of functions that is allowed to be called with AC set.
564 * The list is meant to be minimal and only contains compiler instrumentation
565 * ABI and a few functions used to implement *_{to,from}_user() functions.
566 *
567 * These functions must not directly change AC, but may PUSHF/POPF.
568 */
569static const char *uaccess_safe_builtin[] = {
570 /* KASAN */
571 "kasan_report",
572 "check_memory_region",
573 /* KASAN out-of-line */
574 "__asan_loadN_noabort",
575 "__asan_load1_noabort",
576 "__asan_load2_noabort",
577 "__asan_load4_noabort",
578 "__asan_load8_noabort",
579 "__asan_load16_noabort",
580 "__asan_storeN_noabort",
581 "__asan_store1_noabort",
582 "__asan_store2_noabort",
583 "__asan_store4_noabort",
584 "__asan_store8_noabort",
585 "__asan_store16_noabort",
Jann Hornb0b8e562020-09-29 00:49:16 +0200586 "__kasan_check_read",
587 "__kasan_check_write",
Peter Zijlstraea242132019-02-25 12:50:09 +0100588 /* KASAN in-line */
589 "__asan_report_load_n_noabort",
590 "__asan_report_load1_noabort",
591 "__asan_report_load2_noabort",
592 "__asan_report_load4_noabort",
593 "__asan_report_load8_noabort",
594 "__asan_report_load16_noabort",
595 "__asan_report_store_n_noabort",
596 "__asan_report_store1_noabort",
597 "__asan_report_store2_noabort",
598 "__asan_report_store4_noabort",
599 "__asan_report_store8_noabort",
600 "__asan_report_store16_noabort",
Marco Elver5f5c9712019-11-14 19:02:57 +0100601 /* KCSAN */
Marco Elver99676832020-03-25 17:41:57 +0100602 "__kcsan_check_access",
Marco Elver5f5c9712019-11-14 19:02:57 +0100603 "kcsan_found_watchpoint",
604 "kcsan_setup_watchpoint",
Marco Elver99676832020-03-25 17:41:57 +0100605 "kcsan_check_scoped_accesses",
Marco Elver50a19ad2020-04-24 17:47:30 +0200606 "kcsan_disable_current",
607 "kcsan_enable_current_nowarn",
Marco Elver5f5c9712019-11-14 19:02:57 +0100608 /* KCSAN/TSAN */
609 "__tsan_func_entry",
610 "__tsan_func_exit",
611 "__tsan_read_range",
612 "__tsan_write_range",
613 "__tsan_read1",
614 "__tsan_read2",
615 "__tsan_read4",
616 "__tsan_read8",
617 "__tsan_read16",
618 "__tsan_write1",
619 "__tsan_write2",
620 "__tsan_write4",
621 "__tsan_write8",
622 "__tsan_write16",
Marco Elvera81b3752020-07-24 09:00:02 +0200623 "__tsan_read_write1",
624 "__tsan_read_write2",
625 "__tsan_read_write4",
626 "__tsan_read_write8",
627 "__tsan_read_write16",
Marco Elver883957b2020-07-03 15:40:30 +0200628 "__tsan_atomic8_load",
629 "__tsan_atomic16_load",
630 "__tsan_atomic32_load",
631 "__tsan_atomic64_load",
632 "__tsan_atomic8_store",
633 "__tsan_atomic16_store",
634 "__tsan_atomic32_store",
635 "__tsan_atomic64_store",
636 "__tsan_atomic8_exchange",
637 "__tsan_atomic16_exchange",
638 "__tsan_atomic32_exchange",
639 "__tsan_atomic64_exchange",
640 "__tsan_atomic8_fetch_add",
641 "__tsan_atomic16_fetch_add",
642 "__tsan_atomic32_fetch_add",
643 "__tsan_atomic64_fetch_add",
644 "__tsan_atomic8_fetch_sub",
645 "__tsan_atomic16_fetch_sub",
646 "__tsan_atomic32_fetch_sub",
647 "__tsan_atomic64_fetch_sub",
648 "__tsan_atomic8_fetch_and",
649 "__tsan_atomic16_fetch_and",
650 "__tsan_atomic32_fetch_and",
651 "__tsan_atomic64_fetch_and",
652 "__tsan_atomic8_fetch_or",
653 "__tsan_atomic16_fetch_or",
654 "__tsan_atomic32_fetch_or",
655 "__tsan_atomic64_fetch_or",
656 "__tsan_atomic8_fetch_xor",
657 "__tsan_atomic16_fetch_xor",
658 "__tsan_atomic32_fetch_xor",
659 "__tsan_atomic64_fetch_xor",
660 "__tsan_atomic8_fetch_nand",
661 "__tsan_atomic16_fetch_nand",
662 "__tsan_atomic32_fetch_nand",
663 "__tsan_atomic64_fetch_nand",
664 "__tsan_atomic8_compare_exchange_strong",
665 "__tsan_atomic16_compare_exchange_strong",
666 "__tsan_atomic32_compare_exchange_strong",
667 "__tsan_atomic64_compare_exchange_strong",
668 "__tsan_atomic8_compare_exchange_weak",
669 "__tsan_atomic16_compare_exchange_weak",
670 "__tsan_atomic32_compare_exchange_weak",
671 "__tsan_atomic64_compare_exchange_weak",
672 "__tsan_atomic8_compare_exchange_val",
673 "__tsan_atomic16_compare_exchange_val",
674 "__tsan_atomic32_compare_exchange_val",
675 "__tsan_atomic64_compare_exchange_val",
676 "__tsan_atomic_thread_fence",
677 "__tsan_atomic_signal_fence",
Peter Zijlstraea242132019-02-25 12:50:09 +0100678 /* KCOV */
679 "write_comp_data",
Josh Poimboeufae033f02020-04-29 14:09:04 -0500680 "check_kcov_mode",
Peter Zijlstraea242132019-02-25 12:50:09 +0100681 "__sanitizer_cov_trace_pc",
682 "__sanitizer_cov_trace_const_cmp1",
683 "__sanitizer_cov_trace_const_cmp2",
684 "__sanitizer_cov_trace_const_cmp4",
685 "__sanitizer_cov_trace_const_cmp8",
686 "__sanitizer_cov_trace_cmp1",
687 "__sanitizer_cov_trace_cmp2",
688 "__sanitizer_cov_trace_cmp4",
689 "__sanitizer_cov_trace_cmp8",
Al Viro36b1c702020-02-16 13:07:49 -0500690 "__sanitizer_cov_trace_switch",
Peter Zijlstraea242132019-02-25 12:50:09 +0100691 /* UBSAN */
692 "ubsan_type_mismatch_common",
693 "__ubsan_handle_type_mismatch",
694 "__ubsan_handle_type_mismatch_v1",
Peter Zijlstra9a50dca2019-10-21 15:11:49 +0200695 "__ubsan_handle_shift_out_of_bounds",
Peter Zijlstraea242132019-02-25 12:50:09 +0100696 /* misc */
697 "csum_partial_copy_generic",
Dan Williamsec6347b2020-10-05 20:40:16 -0700698 "copy_mc_fragile",
699 "copy_mc_fragile_handle_tail",
Dan Williams5da8e4a2020-10-05 20:40:25 -0700700 "copy_mc_enhanced_fast_string",
Peter Zijlstraea242132019-02-25 12:50:09 +0100701 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
702 NULL
703};
704
705static void add_uaccess_safe(struct objtool_file *file)
706{
707 struct symbol *func;
708 const char **name;
709
710 if (!uaccess)
711 return;
712
713 for (name = uaccess_safe_builtin; *name; name++) {
714 func = find_symbol_by_name(file->elf, *name);
715 if (!func)
716 continue;
717
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500718 func->uaccess_safe = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500719 }
720}
721
722/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000723 * FIXME: For now, just ignore any alternatives which add retpolines. This is
724 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
725 * But it at least allows objtool to understand the control flow *around* the
726 * retpoline.
727 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100728static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000729{
730 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700731 struct reloc *reloc;
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000732 struct instruction *insn;
733
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100734 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000735 if (!sec)
736 return 0;
737
Matt Helsleyf1974222020-05-29 14:01:13 -0700738 list_for_each_entry(reloc, &sec->reloc_list, list) {
739 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000740 WARN("unexpected relocation symbol type in %s", sec->name);
741 return -1;
742 }
743
Matt Helsleyf1974222020-05-29 14:01:13 -0700744 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000745 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100746 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000747 return -1;
748 }
749
750 insn->ignore_alts = true;
751 }
752
753 return 0;
754}
755
756/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500757 * Find the destination instructions for all jumps.
758 */
759static int add_jump_destinations(struct objtool_file *file)
760{
761 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -0700762 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500763 struct section *dest_sec;
764 unsigned long dest_off;
765
766 for_each_insn(file, insn) {
Josh Poimboeufa2296142020-02-10 12:32:39 -0600767 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500768 continue;
769
Josh Poimboeufdb6c6a02020-09-10 10:24:57 -0500770 if (insn->offset == FAKE_JUMP_OFFSET)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500771 continue;
772
Matt Helsleyf1974222020-05-29 14:01:13 -0700773 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100774 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700775 if (!reloc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500776 dest_sec = insn->sec;
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000777 dest_off = arch_jump_destination(insn);
Matt Helsleyf1974222020-05-29 14:01:13 -0700778 } else if (reloc->sym->type == STT_SECTION) {
779 dest_sec = reloc->sym->sec;
780 dest_off = arch_dest_reloc_offset(reloc->addend);
781 } else if (reloc->sym->sec->idx) {
782 dest_sec = reloc->sym->sec;
783 dest_off = reloc->sym->sym.st_value +
784 arch_dest_reloc_offset(reloc->addend);
785 } else if (strstr(reloc->sym->name, "_indirect_thunk_")) {
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000786 /*
787 * Retpoline jumps are really dynamic jumps in
788 * disguise, so convert them accordingly.
789 */
Josh Poimboeufb68b9902019-07-17 20:36:57 -0500790 if (insn->type == INSN_JUMP_UNCONDITIONAL)
791 insn->type = INSN_JUMP_DYNAMIC;
792 else
793 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
794
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100795 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000796 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500797 } else {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500798 /* external sibling call */
Matt Helsleyf1974222020-05-29 14:01:13 -0700799 insn->call_dest = reloc->sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200800 if (insn->call_dest->static_call_tramp) {
801 list_add_tail(&insn->static_call_node,
802 &file->static_call_list);
803 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500804 continue;
805 }
806
807 insn->jump_dest = find_insn(file, dest_sec, dest_off);
808 if (!insn->jump_dest) {
809
810 /*
811 * This is a special case where an alt instruction
812 * jumps past the end of the section. These are
813 * handled later in handle_group_alt().
814 */
815 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
816 continue;
817
818 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
819 insn->sec, insn->offset, dest_sec->name,
820 dest_off);
821 return -1;
822 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500823
824 /*
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100825 * Cross-function jump.
Josh Poimboeufcd778492018-06-01 07:23:51 -0500826 */
827 if (insn->func && insn->jump_dest->func &&
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100828 insn->func != insn->jump_dest->func) {
829
830 /*
831 * For GCC 8+, create parent/child links for any cold
832 * subfunctions. This is _mostly_ redundant with a
833 * similar initialization in read_symbols().
834 *
835 * If a function has aliases, we want the *first* such
836 * function in the symbol table to be the subfunction's
837 * parent. In that case we overwrite the
838 * initialization done in read_symbols().
839 *
840 * However this code can't completely replace the
841 * read_symbols() code because this doesn't detect the
842 * case where the parent function's only reference to a
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500843 * subfunction is through a jump table.
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100844 */
845 if (!strstr(insn->func->name, ".cold.") &&
846 strstr(insn->jump_dest->func->name, ".cold.")) {
847 insn->func->cfunc = insn->jump_dest->func;
848 insn->jump_dest->func->pfunc = insn->func;
849
850 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
851 insn->jump_dest->offset == insn->jump_dest->func->offset) {
852
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500853 /* internal sibling call */
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100854 insn->call_dest = insn->jump_dest->func;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200855 if (insn->call_dest->static_call_tramp) {
856 list_add_tail(&insn->static_call_node,
857 &file->static_call_list);
858 }
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100859 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500860 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500861 }
862
863 return 0;
864}
865
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200866static void remove_insn_ops(struct instruction *insn)
867{
868 struct stack_op *op, *tmp;
869
870 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
871 list_del(&op->list);
872 free(op);
873 }
874}
875
Julien Thierry2b232a22020-09-15 08:53:18 +0100876static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
877{
878 struct symbol *call_dest;
879
880 call_dest = find_func_by_offset(sec, offset);
881 if (!call_dest)
882 call_dest = find_symbol_by_offset(sec, offset);
883
884 return call_dest;
885}
886
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500887/*
888 * Find the destination instructions for all calls.
889 */
890static int add_call_destinations(struct objtool_file *file)
891{
892 struct instruction *insn;
893 unsigned long dest_off;
Matt Helsleyf1974222020-05-29 14:01:13 -0700894 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500895
896 for_each_insn(file, insn) {
897 if (insn->type != INSN_CALL)
898 continue;
899
Matt Helsleyf1974222020-05-29 14:01:13 -0700900 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100901 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700902 if (!reloc) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000903 dest_off = arch_jump_destination(insn);
Julien Thierry2b232a22020-09-15 08:53:18 +0100904 insn->call_dest = find_call_destination(insn->sec, dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600905
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600906 if (insn->ignore)
907 continue;
908
909 if (!insn->call_dest) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200910 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500911 return -1;
912 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600913
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600914 if (insn->func && insn->call_dest->type != STT_FUNC) {
915 WARN_FUNC("unsupported call to non-function",
916 insn->sec, insn->offset);
917 return -1;
918 }
919
Matt Helsleyf1974222020-05-29 14:01:13 -0700920 } else if (reloc->sym->type == STT_SECTION) {
921 dest_off = arch_dest_reloc_offset(reloc->addend);
Julien Thierry2b232a22020-09-15 08:53:18 +0100922 insn->call_dest = find_call_destination(reloc->sym->sec,
923 dest_off);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600924 if (!insn->call_dest) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000925 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500926 insn->sec, insn->offset,
Matt Helsleyf1974222020-05-29 14:01:13 -0700927 reloc->sym->sec->name,
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000928 dest_off);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500929 return -1;
930 }
931 } else
Matt Helsleyf1974222020-05-29 14:01:13 -0700932 insn->call_dest = reloc->sym;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200933
934 /*
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200935 * Many compilers cannot disable KCOV with a function attribute
936 * so they need a little help, NOP out any KCOV calls from noinstr
937 * text.
938 */
939 if (insn->sec->noinstr &&
940 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
Peter Zijlstrad832c002020-06-18 17:55:29 +0200941 if (reloc) {
942 reloc->type = R_NONE;
943 elf_write_reloc(file->elf, reloc);
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200944 }
945
946 elf_write_insn(file->elf, insn->sec,
947 insn->offset, insn->len,
948 arch_nop_insn(insn->len));
949 insn->type = INSN_NOP;
950 }
951
952 /*
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200953 * Whatever stack impact regular CALLs have, should be undone
954 * by the RETURN of the called function.
955 *
956 * Annotated intra-function calls retain the stack_ops but
957 * are converted to JUMP, see read_intra_function_calls().
958 */
959 remove_insn_ops(insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500960 }
961
962 return 0;
963}
964
965/*
966 * The .alternatives section requires some extra special care, over and above
967 * what other special sections require:
968 *
969 * 1. Because alternatives are patched in-place, we need to insert a fake jump
970 * instruction at the end so that validate_branch() skips all the original
971 * replaced instructions when validating the new instruction path.
972 *
973 * 2. An added wrinkle is that the new instruction length might be zero. In
974 * that case the old instructions are replaced with noops. We simulate that
975 * by creating a fake jump as the only new instruction.
976 *
977 * 3. In some cases, the alternative section includes an instruction which
978 * conditionally jumps to the _end_ of the entry. We have to modify these
979 * jumps' destinations to point back to .text rather than the end of the
980 * entry in .altinstr_replacement.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500981 */
982static int handle_group_alt(struct objtool_file *file,
983 struct special_alt *special_alt,
984 struct instruction *orig_insn,
985 struct instruction **new_insn)
986{
Alexandre Chartre13fab062020-04-14 12:36:11 +0200987 static unsigned int alt_group_next_index = 1;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600988 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Alexandre Chartre13fab062020-04-14 12:36:11 +0200989 unsigned int alt_group = alt_group_next_index++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500990 unsigned long dest_off;
991
992 last_orig_insn = NULL;
993 insn = orig_insn;
994 sec_for_each_insn_from(file, insn) {
995 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
996 break;
997
Alexandre Chartre13fab062020-04-14 12:36:11 +0200998 insn->alt_group = alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500999 last_orig_insn = insn;
1000 }
1001
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001002 if (next_insn_same_sec(file, last_orig_insn)) {
1003 fake_jump = malloc(sizeof(*fake_jump));
1004 if (!fake_jump) {
1005 WARN("malloc failed");
1006 return -1;
1007 }
1008 memset(fake_jump, 0, sizeof(*fake_jump));
1009 INIT_LIST_HEAD(&fake_jump->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +00001010 INIT_LIST_HEAD(&fake_jump->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001011 init_cfi_state(&fake_jump->cfi);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001012
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001013 fake_jump->sec = special_alt->new_sec;
Josh Poimboeufe6da9562019-05-13 12:01:31 -05001014 fake_jump->offset = FAKE_JUMP_OFFSET;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001015 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
1016 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
Josh Poimboeufe6da9562019-05-13 12:01:31 -05001017 fake_jump->func = orig_insn->func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001018 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001019
1020 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001021 if (!fake_jump) {
1022 WARN("%s: empty alternative at end of section",
1023 special_alt->orig_sec->name);
1024 return -1;
1025 }
1026
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001027 *new_insn = fake_jump;
1028 return 0;
1029 }
1030
1031 last_new_insn = NULL;
Alexandre Chartre13fab062020-04-14 12:36:11 +02001032 alt_group = alt_group_next_index++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001033 insn = *new_insn;
1034 sec_for_each_insn_from(file, insn) {
Julien Thierry45245f52020-09-04 16:30:23 +01001035 struct reloc *alt_reloc;
1036
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001037 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1038 break;
1039
1040 last_new_insn = insn;
1041
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001042 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +01001043 insn->func = orig_insn->func;
Alexandre Chartre13fab062020-04-14 12:36:11 +02001044 insn->alt_group = alt_group;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001045
Josh Poimboeufdc419722020-02-10 12:32:40 -06001046 /*
1047 * Since alternative replacement code is copy/pasted by the
1048 * kernel after applying relocations, generally such code can't
1049 * have relative-address relocation references to outside the
1050 * .altinstr_replacement section, unless the arch's
1051 * alternatives code can adjust the relative offsets
1052 * accordingly.
Josh Poimboeufdc419722020-02-10 12:32:40 -06001053 */
Julien Thierry45245f52020-09-04 16:30:23 +01001054 alt_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1055 insn->offset, insn->len);
1056 if (alt_reloc &&
1057 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
Josh Poimboeufdc419722020-02-10 12:32:40 -06001058
1059 WARN_FUNC("unsupported relocation in alternatives section",
1060 insn->sec, insn->offset);
1061 return -1;
1062 }
1063
Josh Poimboeufa2296142020-02-10 12:32:39 -06001064 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001065 continue;
1066
1067 if (!insn->immediate)
1068 continue;
1069
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001070 dest_off = arch_jump_destination(insn);
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001071 if (dest_off == special_alt->new_off + special_alt->new_len) {
1072 if (!fake_jump) {
1073 WARN("%s: alternative jump to end of section",
1074 special_alt->orig_sec->name);
1075 return -1;
1076 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001077 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001078 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001079
1080 if (!insn->jump_dest) {
1081 WARN_FUNC("can't find alternative jump destination",
1082 insn->sec, insn->offset);
1083 return -1;
1084 }
1085 }
1086
1087 if (!last_new_insn) {
1088 WARN_FUNC("can't find last new alternative instruction",
1089 special_alt->new_sec, special_alt->new_off);
1090 return -1;
1091 }
1092
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001093 if (fake_jump)
1094 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001095
1096 return 0;
1097}
1098
1099/*
1100 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1101 * If the original instruction is a jump, make the alt entry an effective nop
1102 * by just skipping the original instruction.
1103 */
1104static int handle_jump_alt(struct objtool_file *file,
1105 struct special_alt *special_alt,
1106 struct instruction *orig_insn,
1107 struct instruction **new_insn)
1108{
1109 if (orig_insn->type == INSN_NOP)
1110 return 0;
1111
1112 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1113 WARN_FUNC("unsupported instruction at jump label",
1114 orig_insn->sec, orig_insn->offset);
1115 return -1;
1116 }
1117
1118 *new_insn = list_next_entry(orig_insn, list);
1119 return 0;
1120}
1121
1122/*
1123 * Read all the special sections which have alternate instructions which can be
1124 * patched in or redirected to at runtime. Each instruction having alternate
1125 * instruction(s) has them added to its insn->alts list, which will be
1126 * traversed in validate_branch().
1127 */
1128static int add_special_section_alts(struct objtool_file *file)
1129{
1130 struct list_head special_alts;
1131 struct instruction *orig_insn, *new_insn;
1132 struct special_alt *special_alt, *tmp;
1133 struct alternative *alt;
1134 int ret;
1135
1136 ret = special_get_alts(file->elf, &special_alts);
1137 if (ret)
1138 return ret;
1139
1140 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001141
1142 orig_insn = find_insn(file, special_alt->orig_sec,
1143 special_alt->orig_off);
1144 if (!orig_insn) {
1145 WARN_FUNC("special: can't find orig instruction",
1146 special_alt->orig_sec, special_alt->orig_off);
1147 ret = -1;
1148 goto out;
1149 }
1150
1151 new_insn = NULL;
1152 if (!special_alt->group || special_alt->new_len) {
1153 new_insn = find_insn(file, special_alt->new_sec,
1154 special_alt->new_off);
1155 if (!new_insn) {
1156 WARN_FUNC("special: can't find new instruction",
1157 special_alt->new_sec,
1158 special_alt->new_off);
1159 ret = -1;
1160 goto out;
1161 }
1162 }
1163
1164 if (special_alt->group) {
Julien Thierry7170cf42020-03-27 15:28:41 +00001165 if (!special_alt->orig_len) {
1166 WARN_FUNC("empty alternative entry",
1167 orig_insn->sec, orig_insn->offset);
1168 continue;
1169 }
1170
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001171 ret = handle_group_alt(file, special_alt, orig_insn,
1172 &new_insn);
1173 if (ret)
1174 goto out;
1175 } else if (special_alt->jump_or_nop) {
1176 ret = handle_jump_alt(file, special_alt, orig_insn,
1177 &new_insn);
1178 if (ret)
1179 goto out;
1180 }
1181
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001182 alt = malloc(sizeof(*alt));
1183 if (!alt) {
1184 WARN("malloc failed");
1185 ret = -1;
1186 goto out;
1187 }
1188
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001189 alt->insn = new_insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +01001190 alt->skip_orig = special_alt->skip_orig;
Peter Zijlstraea242132019-02-25 12:50:09 +01001191 orig_insn->ignore_alts |= special_alt->skip_alt;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001192 list_add_tail(&alt->list, &orig_insn->alts);
1193
1194 list_del(&special_alt->list);
1195 free(special_alt);
1196 }
1197
1198out:
1199 return ret;
1200}
1201
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001202static int add_jump_table(struct objtool_file *file, struct instruction *insn,
Matt Helsleyf1974222020-05-29 14:01:13 -07001203 struct reloc *table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001204{
Matt Helsleyf1974222020-05-29 14:01:13 -07001205 struct reloc *reloc = table;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001206 struct instruction *dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001207 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001208 struct symbol *pfunc = insn->func->pfunc;
1209 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001210
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001211 /*
Matt Helsleyf1974222020-05-29 14:01:13 -07001212 * Each @reloc is a switch table relocation which points to the target
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001213 * instruction.
1214 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001215 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
Jann Hornbd98c812019-07-17 20:36:54 -05001216
1217 /* Check for the end of the table: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001218 if (reloc != table && reloc->jump_table_start)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001219 break;
1220
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001221 /* Make sure the table entries are consecutive: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001222 if (prev_offset && reloc->offset != prev_offset + 8)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001223 break;
1224
1225 /* Detect function pointers from contiguous objects: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001226 if (reloc->sym->sec == pfunc->sec &&
1227 reloc->addend == pfunc->offset)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001228 break;
1229
Matt Helsleyf1974222020-05-29 14:01:13 -07001230 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001231 if (!dest_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001232 break;
1233
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001234 /* Make sure the destination is in the same function: */
Josh Poimboeufe65050b2019-07-17 20:36:55 -05001235 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -05001236 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001237
1238 alt = malloc(sizeof(*alt));
1239 if (!alt) {
1240 WARN("malloc failed");
1241 return -1;
1242 }
1243
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001244 alt->insn = dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001245 list_add_tail(&alt->list, &insn->alts);
Matt Helsleyf1974222020-05-29 14:01:13 -07001246 prev_offset = reloc->offset;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001247 }
1248
1249 if (!prev_offset) {
1250 WARN_FUNC("can't find switch jump table",
1251 insn->sec, insn->offset);
1252 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001253 }
1254
1255 return 0;
1256}
1257
1258/*
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001259 * find_jump_table() - Given a dynamic jump, find the switch jump table
1260 * associated with it.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001261 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001262static struct reloc *find_jump_table(struct objtool_file *file,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001263 struct symbol *func,
1264 struct instruction *insn)
1265{
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001266 struct reloc *table_reloc;
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001267 struct instruction *dest_insn, *orig_insn = insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001268
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001269 /*
1270 * Backward search using the @first_jump_src links, these help avoid
1271 * much of the 'in between' code. Which avoids us getting confused by
1272 * it.
1273 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001274 for (;
Josh Poimboeuf1119d262020-04-28 16:45:16 -05001275 insn && insn->func && insn->func->pfunc == func;
1276 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001277
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001278 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001279 break;
1280
1281 /* allow small jumps within the range */
1282 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1283 insn->jump_dest &&
1284 (insn->jump_dest->offset <= insn->offset ||
1285 insn->jump_dest->offset > orig_insn->offset))
1286 break;
1287
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001288 table_reloc = arch_find_switch_table(file, insn);
Matt Helsleyf1974222020-05-29 14:01:13 -07001289 if (!table_reloc)
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001290 continue;
Matt Helsleyf1974222020-05-29 14:01:13 -07001291 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001292 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1293 continue;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001294
Matt Helsleyf1974222020-05-29 14:01:13 -07001295 return table_reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001296 }
1297
1298 return NULL;
1299}
1300
Jann Hornbd98c812019-07-17 20:36:54 -05001301/*
1302 * First pass: Mark the head of each jump table so that in the next pass,
1303 * we know when a given jump table ends and the next one starts.
1304 */
1305static void mark_func_jump_tables(struct objtool_file *file,
1306 struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001307{
Jann Hornbd98c812019-07-17 20:36:54 -05001308 struct instruction *insn, *last = NULL;
Matt Helsleyf1974222020-05-29 14:01:13 -07001309 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001310
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001311 func_for_each_insn(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001312 if (!last)
1313 last = insn;
1314
1315 /*
1316 * Store back-pointers for unconditional forward jumps such
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001317 * that find_jump_table() can back-track using those and
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001318 * avoid some potentially confusing code.
1319 */
1320 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1321 insn->offset > last->offset &&
1322 insn->jump_dest->offset > insn->offset &&
1323 !insn->jump_dest->first_jump_src) {
1324
1325 insn->jump_dest->first_jump_src = insn;
1326 last = insn->jump_dest;
1327 }
1328
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001329 if (insn->type != INSN_JUMP_DYNAMIC)
1330 continue;
1331
Matt Helsleyf1974222020-05-29 14:01:13 -07001332 reloc = find_jump_table(file, func, insn);
1333 if (reloc) {
1334 reloc->jump_table_start = true;
1335 insn->jump_table = reloc;
Jann Hornbd98c812019-07-17 20:36:54 -05001336 }
1337 }
1338}
1339
1340static int add_func_jump_tables(struct objtool_file *file,
1341 struct symbol *func)
1342{
1343 struct instruction *insn;
1344 int ret;
1345
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001346 func_for_each_insn(file, func, insn) {
Jann Hornbd98c812019-07-17 20:36:54 -05001347 if (!insn->jump_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001348 continue;
1349
Jann Hornbd98c812019-07-17 20:36:54 -05001350 ret = add_jump_table(file, insn, insn->jump_table);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001351 if (ret)
1352 return ret;
1353 }
1354
1355 return 0;
1356}
1357
1358/*
1359 * For some switch statements, gcc generates a jump table in the .rodata
1360 * section which contains a list of addresses within the function to jump to.
1361 * This finds these jump tables and adds them to the insn->alts lists.
1362 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001363static int add_jump_table_alts(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001364{
1365 struct section *sec;
1366 struct symbol *func;
1367 int ret;
1368
Allan Xavier4a60aa02018-09-07 08:12:01 -05001369 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001370 return 0;
1371
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001372 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001373 list_for_each_entry(func, &sec->symbol_list, list) {
1374 if (func->type != STT_FUNC)
1375 continue;
1376
Jann Hornbd98c812019-07-17 20:36:54 -05001377 mark_func_jump_tables(file, func);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001378 ret = add_func_jump_tables(file, func);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001379 if (ret)
1380 return ret;
1381 }
1382 }
1383
1384 return 0;
1385}
1386
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001387static int read_unwind_hints(struct objtool_file *file)
1388{
Matt Helsleyf1974222020-05-29 14:01:13 -07001389 struct section *sec, *relocsec;
1390 struct reloc *reloc;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001391 struct unwind_hint *hint;
1392 struct instruction *insn;
1393 struct cfi_reg *cfa;
1394 int i;
1395
1396 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1397 if (!sec)
1398 return 0;
1399
Matt Helsleyf1974222020-05-29 14:01:13 -07001400 relocsec = sec->reloc;
1401 if (!relocsec) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001402 WARN("missing .rela.discard.unwind_hints section");
1403 return -1;
1404 }
1405
1406 if (sec->len % sizeof(struct unwind_hint)) {
1407 WARN("struct unwind_hint size mismatch");
1408 return -1;
1409 }
1410
1411 file->hints = true;
1412
1413 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1414 hint = (struct unwind_hint *)sec->data->d_buf + i;
1415
Matt Helsleyf1974222020-05-29 14:01:13 -07001416 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1417 if (!reloc) {
1418 WARN("can't find reloc for unwind_hints[%d]", i);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001419 return -1;
1420 }
1421
Matt Helsleyf1974222020-05-29 14:01:13 -07001422 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001423 if (!insn) {
1424 WARN("can't find insn for unwind_hints[%d]", i);
1425 return -1;
1426 }
1427
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001428 cfa = &insn->cfi.cfa;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001429
Peter Zijlstrac536ed22020-04-01 16:54:26 +02001430 if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001431 insn->ret_offset = hint->sp_offset;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001432 continue;
1433 }
1434
1435 insn->hint = true;
1436
Julien Thierryedea9e62020-09-04 16:30:28 +01001437 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001438 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1439 insn->sec, insn->offset, hint->sp_reg);
1440 return -1;
1441 }
1442
1443 cfa->offset = hint->sp_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001444 insn->cfi.type = hint->type;
1445 insn->cfi.end = hint->end;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001446 }
1447
1448 return 0;
1449}
1450
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001451static int read_retpoline_hints(struct objtool_file *file)
1452{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001453 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001454 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001455 struct reloc *reloc;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001456
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001457 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001458 if (!sec)
1459 return 0;
1460
Matt Helsleyf1974222020-05-29 14:01:13 -07001461 list_for_each_entry(reloc, &sec->reloc_list, list) {
1462 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001463 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001464 return -1;
1465 }
1466
Matt Helsleyf1974222020-05-29 14:01:13 -07001467 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001468 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001469 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001470 return -1;
1471 }
1472
1473 if (insn->type != INSN_JUMP_DYNAMIC &&
1474 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001475 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001476 insn->sec, insn->offset);
1477 return -1;
1478 }
1479
1480 insn->retpoline_safe = true;
1481 }
1482
1483 return 0;
1484}
1485
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001486static int read_instr_hints(struct objtool_file *file)
1487{
1488 struct section *sec;
1489 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001490 struct reloc *reloc;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001491
1492 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1493 if (!sec)
1494 return 0;
1495
Matt Helsleyf1974222020-05-29 14:01:13 -07001496 list_for_each_entry(reloc, &sec->reloc_list, list) {
1497 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001498 WARN("unexpected relocation symbol type in %s", sec->name);
1499 return -1;
1500 }
1501
Matt Helsleyf1974222020-05-29 14:01:13 -07001502 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001503 if (!insn) {
1504 WARN("bad .discard.instr_end entry");
1505 return -1;
1506 }
1507
1508 insn->instr--;
1509 }
1510
1511 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1512 if (!sec)
1513 return 0;
1514
Matt Helsleyf1974222020-05-29 14:01:13 -07001515 list_for_each_entry(reloc, &sec->reloc_list, list) {
1516 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001517 WARN("unexpected relocation symbol type in %s", sec->name);
1518 return -1;
1519 }
1520
Matt Helsleyf1974222020-05-29 14:01:13 -07001521 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001522 if (!insn) {
1523 WARN("bad .discard.instr_begin entry");
1524 return -1;
1525 }
1526
1527 insn->instr++;
1528 }
1529
1530 return 0;
1531}
1532
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001533static int read_intra_function_calls(struct objtool_file *file)
1534{
1535 struct instruction *insn;
1536 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -07001537 struct reloc *reloc;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001538
1539 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1540 if (!sec)
1541 return 0;
1542
Matt Helsleyf1974222020-05-29 14:01:13 -07001543 list_for_each_entry(reloc, &sec->reloc_list, list) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001544 unsigned long dest_off;
1545
Matt Helsleyf1974222020-05-29 14:01:13 -07001546 if (reloc->sym->type != STT_SECTION) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001547 WARN("unexpected relocation symbol type in %s",
1548 sec->name);
1549 return -1;
1550 }
1551
Matt Helsleyf1974222020-05-29 14:01:13 -07001552 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001553 if (!insn) {
1554 WARN("bad .discard.intra_function_call entry");
1555 return -1;
1556 }
1557
1558 if (insn->type != INSN_CALL) {
1559 WARN_FUNC("intra_function_call not a direct call",
1560 insn->sec, insn->offset);
1561 return -1;
1562 }
1563
1564 /*
1565 * Treat intra-function CALLs as JMPs, but with a stack_op.
1566 * See add_call_destinations(), which strips stack_ops from
1567 * normal CALLs.
1568 */
1569 insn->type = INSN_JUMP_UNCONDITIONAL;
1570
1571 dest_off = insn->offset + insn->len + insn->immediate;
1572 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1573 if (!insn->jump_dest) {
1574 WARN_FUNC("can't find call dest at %s+0x%lx",
1575 insn->sec, insn->offset,
1576 insn->sec->name, dest_off);
1577 return -1;
1578 }
1579 }
1580
1581 return 0;
1582}
1583
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02001584static int read_static_call_tramps(struct objtool_file *file)
1585{
1586 struct section *sec;
1587 struct symbol *func;
1588
1589 for_each_sec(file, sec) {
1590 list_for_each_entry(func, &sec->symbol_list, list) {
1591 if (func->bind == STB_GLOBAL &&
1592 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1593 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1594 func->static_call_tramp = true;
1595 }
1596 }
1597
1598 return 0;
1599}
1600
Allan Xavier4a60aa02018-09-07 08:12:01 -05001601static void mark_rodata(struct objtool_file *file)
1602{
1603 struct section *sec;
1604 bool found = false;
1605
1606 /*
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001607 * Search for the following rodata sections, each of which can
1608 * potentially contain jump tables:
1609 *
1610 * - .rodata: can contain GCC switch tables
1611 * - .rodata.<func>: same, if -fdata-sections is being used
1612 * - .rodata..c_jump_table: contains C annotated jump tables
1613 *
1614 * .rodata.str1.* sections are ignored; they don't contain jump tables.
Allan Xavier4a60aa02018-09-07 08:12:01 -05001615 */
1616 for_each_sec(file, sec) {
Muchun Song1ee444702020-04-12 22:44:05 +08001617 if (!strncmp(sec->name, ".rodata", 7) &&
1618 !strstr(sec->name, ".str1.")) {
Allan Xavier4a60aa02018-09-07 08:12:01 -05001619 sec->rodata = true;
1620 found = true;
1621 }
1622 }
1623
1624 file->rodata = found;
1625}
1626
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001627static int decode_sections(struct objtool_file *file)
1628{
1629 int ret;
1630
Allan Xavier4a60aa02018-09-07 08:12:01 -05001631 mark_rodata(file);
1632
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001633 ret = decode_instructions(file);
1634 if (ret)
1635 return ret;
1636
1637 ret = add_dead_ends(file);
1638 if (ret)
1639 return ret;
1640
1641 add_ignores(file);
Peter Zijlstraea242132019-02-25 12:50:09 +01001642 add_uaccess_safe(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001643
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001644 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001645 if (ret)
1646 return ret;
1647
Peter Zijlstra5b06fd32020-08-18 15:57:49 +02001648 ret = read_static_call_tramps(file);
1649 if (ret)
1650 return ret;
1651
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001652 ret = add_jump_destinations(file);
1653 if (ret)
1654 return ret;
1655
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001656 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001657 if (ret)
1658 return ret;
1659
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001660 ret = read_intra_function_calls(file);
1661 if (ret)
1662 return ret;
1663
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001664 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001665 if (ret)
1666 return ret;
1667
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001668 ret = add_jump_table_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001669 if (ret)
1670 return ret;
1671
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001672 ret = read_unwind_hints(file);
1673 if (ret)
1674 return ret;
1675
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001676 ret = read_retpoline_hints(file);
1677 if (ret)
1678 return ret;
1679
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001680 ret = read_instr_hints(file);
1681 if (ret)
1682 return ret;
1683
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001684 return 0;
1685}
1686
1687static bool is_fentry_call(struct instruction *insn)
1688{
Alexandre Chartre87cf61f2020-04-14 12:36:10 +02001689 if (insn->type == INSN_CALL && insn->call_dest &&
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001690 insn->call_dest->type == STT_NOTYPE &&
1691 !strcmp(insn->call_dest->name, "__fentry__"))
1692 return true;
1693
1694 return false;
1695}
1696
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001697static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001698{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001699 u8 ret_offset = insn->ret_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001700 struct cfi_state *cfi = &state->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001701 int i;
1702
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001703 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001704 return true;
1705
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001706 if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001707 return true;
1708
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001709 if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001710 return true;
1711
Alexandre Chartrec721b3f2020-04-07 09:31:35 +02001712 /*
1713 * If there is a ret offset hint then don't check registers
1714 * because a callee-saved register might have been pushed on
1715 * the stack.
1716 */
1717 if (ret_offset)
1718 return false;
1719
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001720 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001721 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1722 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001723 return true;
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001724 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001725
1726 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001727}
1728
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001729static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001730{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001731 struct cfi_state *cfi = &state->cfi;
1732
1733 if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
1734 cfi->regs[CFI_BP].offset == -16)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001735 return true;
1736
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001737 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001738 return true;
1739
1740 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001741}
1742
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001743static int update_cfi_state_regs(struct instruction *insn,
1744 struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001745 struct stack_op *op)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001746{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001747 struct cfi_reg *cfa = &cfi->cfa;
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001748
Josh Poimboeufd8dd25a2020-04-25 05:03:00 -05001749 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001750 return 0;
1751
1752 /* push */
Peter Zijlstraea242132019-02-25 12:50:09 +01001753 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001754 cfa->offset += 8;
1755
1756 /* pop */
Peter Zijlstraea242132019-02-25 12:50:09 +01001757 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001758 cfa->offset -= 8;
1759
1760 /* add immediate to sp */
1761 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1762 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1763 cfa->offset -= op->src.offset;
1764
1765 return 0;
1766}
1767
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001768static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001769{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001770 if (arch_callee_saved_reg(reg) &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001771 cfi->regs[reg].base == CFI_UNDEFINED) {
1772 cfi->regs[reg].base = base;
1773 cfi->regs[reg].offset = offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001774 }
1775}
1776
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001777static void restore_reg(struct cfi_state *cfi, unsigned char reg)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001778{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001779 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1780 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001781}
1782
1783/*
1784 * A note about DRAP stack alignment:
1785 *
1786 * GCC has the concept of a DRAP register, which is used to help keep track of
1787 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1788 * register. The typical DRAP pattern is:
1789 *
1790 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1791 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1792 * 41 ff 72 f8 pushq -0x8(%r10)
1793 * 55 push %rbp
1794 * 48 89 e5 mov %rsp,%rbp
1795 * (more pushes)
1796 * 41 52 push %r10
1797 * ...
1798 * 41 5a pop %r10
1799 * (more pops)
1800 * 5d pop %rbp
1801 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1802 * c3 retq
1803 *
1804 * There are some variations in the epilogues, like:
1805 *
1806 * 5b pop %rbx
1807 * 41 5a pop %r10
1808 * 41 5c pop %r12
1809 * 41 5d pop %r13
1810 * 41 5e pop %r14
1811 * c9 leaveq
1812 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1813 * c3 retq
1814 *
1815 * and:
1816 *
1817 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1818 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1819 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1820 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1821 * c9 leaveq
1822 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1823 * c3 retq
1824 *
1825 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1826 * restored beforehand:
1827 *
1828 * 41 55 push %r13
1829 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1830 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1831 * ...
1832 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1833 * 41 5d pop %r13
1834 * c3 retq
1835 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001836static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001837 struct stack_op *op)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001838{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001839 struct cfi_reg *cfa = &cfi->cfa;
1840 struct cfi_reg *regs = cfi->regs;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001841
1842 /* stack operations don't make sense with an undefined CFA */
1843 if (cfa->base == CFI_UNDEFINED) {
1844 if (insn->func) {
1845 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1846 return -1;
1847 }
1848 return 0;
1849 }
1850
Julien Thierryee819ae2020-09-04 16:30:27 +01001851 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
1852 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001853 return update_cfi_state_regs(insn, cfi, op);
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001854
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001855 switch (op->dest.type) {
1856
1857 case OP_DEST_REG:
1858 switch (op->src.type) {
1859
1860 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001861 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1862 cfa->base == CFI_SP &&
1863 regs[CFI_BP].base == CFI_CFA &&
1864 regs[CFI_BP].offset == -cfa->offset) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001865
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001866 /* mov %rsp, %rbp */
1867 cfa->base = op->dest.reg;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001868 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001869 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001870
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001871 else if (op->src.reg == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001872 op->dest.reg == CFI_BP && cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001873
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001874 /* drap: mov %rsp, %rbp */
1875 regs[CFI_BP].base = CFI_BP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001876 regs[CFI_BP].offset = -cfi->stack_size;
1877 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001878 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001879
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001880 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1881
1882 /*
1883 * mov %rsp, %reg
1884 *
1885 * This is needed for the rare case where GCC
1886 * does:
1887 *
1888 * mov %rsp, %rax
1889 * ...
1890 * mov %rax, %rsp
1891 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001892 cfi->vals[op->dest.reg].base = CFI_CFA;
1893 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001894 }
1895
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001896 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1897 cfa->base == CFI_BP) {
1898
1899 /*
1900 * mov %rbp, %rsp
1901 *
1902 * Restore the original stack pointer (Clang).
1903 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001904 cfi->stack_size = -cfi->regs[CFI_BP].offset;
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001905 }
1906
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001907 else if (op->dest.reg == cfa->base) {
1908
1909 /* mov %reg, %rsp */
1910 if (cfa->base == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001911 cfi->vals[op->src.reg].base == CFI_CFA) {
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001912
1913 /*
1914 * This is needed for the rare case
1915 * where GCC does something dumb like:
1916 *
1917 * lea 0x8(%rsp), %rcx
1918 * ...
1919 * mov %rcx, %rsp
1920 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001921 cfa->offset = -cfi->vals[op->src.reg].offset;
1922 cfi->stack_size = cfa->offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001923
1924 } else {
1925 cfa->base = CFI_UNDEFINED;
1926 cfa->offset = 0;
1927 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001928 }
1929
1930 break;
1931
1932 case OP_SRC_ADD:
1933 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1934
1935 /* add imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001936 cfi->stack_size -= op->src.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001937 if (cfa->base == CFI_SP)
1938 cfa->offset -= op->src.offset;
1939 break;
1940 }
1941
1942 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1943
1944 /* lea disp(%rbp), %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001945 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001946 break;
1947 }
1948
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001949 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001950
1951 /* drap: lea disp(%rsp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001952 cfi->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001953
1954 /*
1955 * lea disp(%rsp), %reg
1956 *
1957 * This is needed for the rare case where GCC
1958 * does something dumb like:
1959 *
1960 * lea 0x8(%rsp), %rcx
1961 * ...
1962 * mov %rcx, %rsp
1963 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001964 cfi->vals[op->dest.reg].base = CFI_CFA;
1965 cfi->vals[op->dest.reg].offset = \
1966 -cfi->stack_size + op->src.offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001967
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001968 break;
1969 }
1970
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001971 if (cfi->drap && op->dest.reg == CFI_SP &&
1972 op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001973
1974 /* drap: lea disp(%drap), %rsp */
1975 cfa->base = CFI_SP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001976 cfa->offset = cfi->stack_size = -op->src.offset;
1977 cfi->drap_reg = CFI_UNDEFINED;
1978 cfi->drap = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001979 break;
1980 }
1981
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001982 if (op->dest.reg == cfi->cfa.base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001983 WARN_FUNC("unsupported stack register modification",
1984 insn->sec, insn->offset);
1985 return -1;
1986 }
1987
1988 break;
1989
1990 case OP_SRC_AND:
1991 if (op->dest.reg != CFI_SP ||
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001992 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1993 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001994 WARN_FUNC("unsupported stack pointer realignment",
1995 insn->sec, insn->offset);
1996 return -1;
1997 }
1998
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001999 if (cfi->drap_reg != CFI_UNDEFINED) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002000 /* drap: and imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002001 cfa->base = cfi->drap_reg;
2002 cfa->offset = cfi->stack_size = 0;
2003 cfi->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002004 }
2005
2006 /*
2007 * Older versions of GCC (4.8ish) realign the stack
2008 * without DRAP, with a frame pointer.
2009 */
2010
2011 break;
2012
2013 case OP_SRC_POP:
Peter Zijlstraea242132019-02-25 12:50:09 +01002014 case OP_SRC_POPF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002015 if (!cfi->drap && op->dest.reg == cfa->base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002016
2017 /* pop %rbp */
2018 cfa->base = CFI_SP;
2019 }
2020
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002021 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2022 op->dest.reg == cfi->drap_reg &&
2023 cfi->drap_offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002024
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002025 /* drap: pop %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002026 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002027 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002028 cfi->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002029
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002030 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002031
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002032 /* pop %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002033 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002034 }
2035
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002036 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002037 if (cfa->base == CFI_SP)
2038 cfa->offset -= 8;
2039
2040 break;
2041
2042 case OP_SRC_REG_INDIRECT:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002043 if (cfi->drap && op->src.reg == CFI_BP &&
2044 op->src.offset == cfi->drap_offset) {
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002045
2046 /* drap: mov disp(%rbp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002047 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002048 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002049 cfi->drap_offset = -1;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002050 }
2051
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002052 if (cfi->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002053 op->src.offset == regs[op->dest.reg].offset) {
2054
2055 /* drap: mov disp(%rbp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002056 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002057
2058 } else if (op->src.reg == cfa->base &&
2059 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2060
2061 /* mov disp(%rbp), %reg */
2062 /* mov disp(%rsp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002063 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002064 }
2065
2066 break;
2067
2068 default:
2069 WARN_FUNC("unknown stack-related instruction",
2070 insn->sec, insn->offset);
2071 return -1;
2072 }
2073
2074 break;
2075
2076 case OP_DEST_PUSH:
Peter Zijlstraea242132019-02-25 12:50:09 +01002077 case OP_DEST_PUSHF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002078 cfi->stack_size += 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002079 if (cfa->base == CFI_SP)
2080 cfa->offset += 8;
2081
2082 if (op->src.type != OP_SRC_REG)
2083 break;
2084
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002085 if (cfi->drap) {
2086 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002087
2088 /* drap: push %drap */
2089 cfa->base = CFI_BP_INDIRECT;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002090 cfa->offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002091
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002092 /* save drap so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002093 cfi->drap_offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002094
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002095 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002096
2097 /* drap: push %rbp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002098 cfi->stack_size = 0;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002099
Julien Thierryf4f80392020-09-15 08:53:16 +01002100 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002101
2102 /* drap: push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002103 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002104 }
2105
2106 } else {
2107
2108 /* push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002109 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002110 }
2111
2112 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002113 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002114 cfa->base != CFI_BP)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002115 cfi->bp_scratch = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002116 break;
2117
2118 case OP_DEST_REG_INDIRECT:
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: mov %drap, disp(%rbp) */
2124 cfa->base = CFI_BP_INDIRECT;
2125 cfa->offset = op->dest.offset;
2126
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002127 /* save drap offset so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002128 cfi->drap_offset = op->dest.offset;
Julien Thierryf4f80392020-09-15 08:53:16 +01002129 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002130
2131 /* drap: mov reg, disp(%rbp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002132 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002133 }
2134
2135 } else if (op->dest.reg == cfa->base) {
2136
2137 /* mov reg, disp(%rbp) */
2138 /* mov reg, disp(%rsp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002139 save_reg(cfi, op->src.reg, CFI_CFA,
2140 op->dest.offset - cfi->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002141 }
2142
2143 break;
2144
2145 case OP_DEST_LEAVE:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002146 if ((!cfi->drap && cfa->base != CFI_BP) ||
2147 (cfi->drap && cfa->base != cfi->drap_reg)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002148 WARN_FUNC("leave instruction with modified stack frame",
2149 insn->sec, insn->offset);
2150 return -1;
2151 }
2152
2153 /* leave (mov %rbp, %rsp; pop %rbp) */
2154
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002155 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2156 restore_reg(cfi, CFI_BP);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002157
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002158 if (!cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002159 cfa->base = CFI_SP;
2160 cfa->offset -= 8;
2161 }
2162
2163 break;
2164
2165 case OP_DEST_MEM:
Peter Zijlstraea242132019-02-25 12:50:09 +01002166 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002167 WARN_FUNC("unknown stack-related memory operation",
2168 insn->sec, insn->offset);
2169 return -1;
2170 }
2171
2172 /* pop mem */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002173 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002174 if (cfa->base == CFI_SP)
2175 cfa->offset -= 8;
2176
2177 break;
2178
2179 default:
2180 WARN_FUNC("unknown stack-related instruction",
2181 insn->sec, insn->offset);
2182 return -1;
2183 }
2184
2185 return 0;
2186}
2187
Julien Thierry65ea47d2020-03-27 15:28:47 +00002188static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2189{
2190 struct stack_op *op;
2191
2192 list_for_each_entry(op, &insn->stack_ops, list) {
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002193 struct cfi_state old_cfi = state->cfi;
Julien Thierry65ea47d2020-03-27 15:28:47 +00002194 int res;
2195
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002196 res = update_cfi_state(insn, &state->cfi, op);
Julien Thierry65ea47d2020-03-27 15:28:47 +00002197 if (res)
2198 return res;
2199
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002200 if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) {
2201 WARN_FUNC("alternative modifies stack", insn->sec, insn->offset);
2202 return -1;
2203 }
2204
Julien Thierry65ea47d2020-03-27 15:28:47 +00002205 if (op->dest.type == OP_DEST_PUSHF) {
2206 if (!state->uaccess_stack) {
2207 state->uaccess_stack = 1;
2208 } else if (state->uaccess_stack >> 31) {
2209 WARN_FUNC("PUSHF stack exhausted",
2210 insn->sec, insn->offset);
2211 return 1;
2212 }
2213 state->uaccess_stack <<= 1;
2214 state->uaccess_stack |= state->uaccess;
2215 }
2216
2217 if (op->src.type == OP_SRC_POPF) {
2218 if (state->uaccess_stack) {
2219 state->uaccess = state->uaccess_stack & 1;
2220 state->uaccess_stack >>= 1;
2221 if (state->uaccess_stack == 1)
2222 state->uaccess_stack = 0;
2223 }
2224 }
2225 }
2226
2227 return 0;
2228}
2229
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002230static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002231{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002232 struct cfi_state *cfi1 = &insn->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002233 int i;
2234
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002235 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2236
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002237 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2238 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002239 cfi1->cfa.base, cfi1->cfa.offset,
2240 cfi2->cfa.base, cfi2->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002241
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002242 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002243 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002244 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002245 sizeof(struct cfi_reg)))
2246 continue;
2247
2248 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2249 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002250 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2251 i, cfi2->regs[i].base, cfi2->regs[i].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002252 break;
2253 }
2254
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002255 } else if (cfi1->type != cfi2->type) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002256
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002257 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2258 insn->sec, insn->offset, cfi1->type, cfi2->type);
2259
2260 } else if (cfi1->drap != cfi2->drap ||
2261 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2262 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2263
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002264 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002265 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002266 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2267 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002268
2269 } else
2270 return true;
2271
2272 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002273}
2274
Peter Zijlstraea242132019-02-25 12:50:09 +01002275static inline bool func_uaccess_safe(struct symbol *func)
2276{
2277 if (func)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002278 return func->uaccess_safe;
Peter Zijlstraea242132019-02-25 12:50:09 +01002279
2280 return false;
2281}
2282
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002283static inline const char *call_dest_name(struct instruction *insn)
Peter Zijlstraea242132019-02-25 12:50:09 +01002284{
2285 if (insn->call_dest)
2286 return insn->call_dest->name;
2287
2288 return "{dynamic}";
2289}
2290
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002291static inline bool noinstr_call_dest(struct symbol *func)
2292{
2293 /*
2294 * We can't deal with indirect function calls at present;
2295 * assume they're instrumented.
2296 */
2297 if (!func)
2298 return false;
2299
2300 /*
2301 * If the symbol is from a noinstr section; we good.
2302 */
2303 if (func->sec->noinstr)
2304 return true;
2305
2306 /*
2307 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2308 * something 'BAD' happened. At the risk of taking the machine down,
2309 * let them proceed to get the message out.
2310 */
2311 if (!strncmp(func->name, "__ubsan_handle_", 15))
2312 return true;
2313
2314 return false;
2315}
2316
Peter Zijlstraea242132019-02-25 12:50:09 +01002317static int validate_call(struct instruction *insn, struct insn_state *state)
2318{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002319 if (state->noinstr && state->instr <= 0 &&
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002320 !noinstr_call_dest(insn->call_dest)) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002321 WARN_FUNC("call to %s() leaves .noinstr.text section",
2322 insn->sec, insn->offset, call_dest_name(insn));
2323 return 1;
2324 }
2325
Peter Zijlstraea242132019-02-25 12:50:09 +01002326 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2327 WARN_FUNC("call to %s() with UACCESS enabled",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002328 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstraea242132019-02-25 12:50:09 +01002329 return 1;
2330 }
2331
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002332 if (state->df) {
2333 WARN_FUNC("call to %s() with DF set",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002334 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002335 return 1;
2336 }
2337
Peter Zijlstraea242132019-02-25 12:50:09 +01002338 return 0;
2339}
2340
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002341static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2342{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002343 if (has_modified_stack_frame(insn, state)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002344 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2345 insn->sec, insn->offset);
2346 return 1;
2347 }
2348
Peter Zijlstraea242132019-02-25 12:50:09 +01002349 return validate_call(insn, state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002350}
2351
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002352static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2353{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002354 if (state->noinstr && state->instr > 0) {
2355 WARN_FUNC("return with instrumentation enabled",
2356 insn->sec, insn->offset);
2357 return 1;
2358 }
2359
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002360 if (state->uaccess && !func_uaccess_safe(func)) {
2361 WARN_FUNC("return with UACCESS enabled",
2362 insn->sec, insn->offset);
2363 return 1;
2364 }
2365
2366 if (!state->uaccess && func_uaccess_safe(func)) {
2367 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2368 insn->sec, insn->offset);
2369 return 1;
2370 }
2371
2372 if (state->df) {
2373 WARN_FUNC("return with DF set",
2374 insn->sec, insn->offset);
2375 return 1;
2376 }
2377
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002378 if (func && has_modified_stack_frame(insn, state)) {
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002379 WARN_FUNC("return with modified stack frame",
2380 insn->sec, insn->offset);
2381 return 1;
2382 }
2383
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002384 if (state->cfi.bp_scratch) {
Josh Poimboeufb2966952020-04-01 13:23:29 -05002385 WARN_FUNC("BP used as a scratch register",
2386 insn->sec, insn->offset);
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002387 return 1;
2388 }
2389
2390 return 0;
2391}
2392
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002393/*
Peter Zijlstra7117f162020-04-28 19:37:01 +02002394 * Alternatives should not contain any ORC entries, this in turn means they
2395 * should not contain any CFI ops, which implies all instructions should have
2396 * the same same CFI state.
2397 *
2398 * It is possible to constuct alternatives that have unreachable holes that go
2399 * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2400 * states which then results in ORC entries, which we just said we didn't want.
2401 *
2402 * Avoid them by copying the CFI entry of the first instruction into the whole
2403 * alternative.
2404 */
2405static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn)
2406{
2407 struct instruction *first_insn = insn;
2408 int alt_group = insn->alt_group;
2409
2410 sec_for_each_insn_continue(file, insn) {
2411 if (insn->alt_group != alt_group)
2412 break;
2413 insn->cfi = first_insn->cfi;
2414 }
2415}
2416
2417/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002418 * Follow the branch starting at the given instruction, and recursively follow
2419 * any other branches (jumps). Meanwhile, track the frame pointer state at
2420 * each instruction and validate all the rules described in
2421 * tools/objtool/Documentation/stack-validation.txt.
2422 */
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002423static int validate_branch(struct objtool_file *file, struct symbol *func,
Peter Zijlstrab7460462020-04-02 10:15:51 +02002424 struct instruction *insn, struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002425{
2426 struct alternative *alt;
Peter Zijlstrab7460462020-04-02 10:15:51 +02002427 struct instruction *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002428 struct section *sec;
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002429 u8 visited;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002430 int ret;
2431
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002432 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002433
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002434 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002435 next_insn = next_insn_same_sec(file, insn);
2436
Josh Poimboeuf13810432018-05-09 22:39:15 -05002437 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05002438 WARN("%s() falls through to next function %s()",
2439 func->name, insn->func->name);
2440 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002441 }
2442
Josh Poimboeuf48550222017-07-07 09:19:42 -05002443 if (func && insn->ignore) {
2444 WARN_FUNC("BUG: why am I validating an ignored function?",
2445 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05002446 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05002447 }
2448
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002449 visited = 1 << state.uaccess;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002450 if (insn->visited) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002451 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002452 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002453
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002454 if (insn->visited & visited)
Peter Zijlstraea242132019-02-25 12:50:09 +01002455 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002456 }
2457
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002458 if (state.noinstr)
2459 state.instr += insn->instr;
2460
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002461 if (insn->hint)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002462 state.cfi = insn->cfi;
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002463 else
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002464 insn->cfi = state.cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002465
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002466 insn->visited |= visited;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002467
Peter Zijlstra7117f162020-04-28 19:37:01 +02002468 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002469 bool skip_orig = false;
2470
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002471 list_for_each_entry(alt, &insn->alts, list) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002472 if (alt->skip_orig)
2473 skip_orig = true;
2474
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002475 ret = validate_branch(file, func, alt->insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002476 if (ret) {
2477 if (backtrace)
2478 BT_FUNC("(alt)", insn);
2479 return ret;
2480 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002481 }
Peter Zijlstra764eef42019-03-01 11:19:03 +01002482
Peter Zijlstra7117f162020-04-28 19:37:01 +02002483 if (insn->alt_group)
2484 fill_alternative_cfi(file, insn);
2485
Peter Zijlstra764eef42019-03-01 11:19:03 +01002486 if (skip_orig)
2487 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002488 }
2489
Peter Zijlstra60041bc2020-04-24 16:16:41 +02002490 if (handle_insn_ops(insn, &state))
2491 return 1;
2492
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002493 switch (insn->type) {
2494
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002495 case INSN_RETURN:
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002496 return validate_return(func, insn, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002497
2498 case INSN_CALL:
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002499 case INSN_CALL_DYNAMIC:
Peter Zijlstraea242132019-02-25 12:50:09 +01002500 ret = validate_call(insn, &state);
2501 if (ret)
2502 return ret;
2503
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002504 if (!no_fp && func && !is_fentry_call(insn) &&
2505 !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002506 WARN_FUNC("call without frame pointer save/setup",
2507 sec, insn->offset);
2508 return 1;
2509 }
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002510
2511 if (dead_end_function(file, insn->call_dest))
2512 return 0;
2513
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002514 if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) {
2515 list_add_tail(&insn->static_call_node,
2516 &file->static_call_list);
2517 }
2518
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002519 break;
2520
2521 case INSN_JUMP_CONDITIONAL:
2522 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002523 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002524 ret = validate_sibling_call(insn, &state);
2525 if (ret)
2526 return ret;
2527
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002528 } else if (insn->jump_dest) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002529 ret = validate_branch(file, func,
2530 insn->jump_dest, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002531 if (ret) {
2532 if (backtrace)
2533 BT_FUNC("(branch)", insn);
2534 return ret;
2535 }
Josh Poimboeuf48550222017-07-07 09:19:42 -05002536 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002537
2538 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2539 return 0;
2540
2541 break;
2542
2543 case INSN_JUMP_DYNAMIC:
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002544 case INSN_JUMP_DYNAMIC_CONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002545 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002546 ret = validate_sibling_call(insn, &state);
2547 if (ret)
2548 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002549 }
2550
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002551 if (insn->type == INSN_JUMP_DYNAMIC)
2552 return 0;
2553
2554 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002555
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002556 case INSN_CONTEXT_SWITCH:
2557 if (func && (!next_insn || !next_insn->hint)) {
2558 WARN_FUNC("unsupported instruction in callable function",
2559 sec, insn->offset);
2560 return 1;
2561 }
2562 return 0;
2563
Peter Zijlstraea242132019-02-25 12:50:09 +01002564 case INSN_STAC:
2565 if (state.uaccess) {
2566 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2567 return 1;
2568 }
2569
2570 state.uaccess = true;
2571 break;
2572
2573 case INSN_CLAC:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002574 if (!state.uaccess && func) {
Peter Zijlstraea242132019-02-25 12:50:09 +01002575 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2576 return 1;
2577 }
2578
2579 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2580 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2581 return 1;
2582 }
2583
2584 state.uaccess = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002585 break;
2586
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002587 case INSN_STD:
2588 if (state.df)
2589 WARN_FUNC("recursive STD", sec, insn->offset);
2590
2591 state.df = true;
2592 break;
2593
2594 case INSN_CLD:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002595 if (!state.df && func)
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002596 WARN_FUNC("redundant CLD", sec, insn->offset);
2597
2598 state.df = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002599 break;
2600
2601 default:
2602 break;
2603 }
2604
2605 if (insn->dead_end)
2606 return 0;
2607
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002608 if (!next_insn) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002609 if (state.cfi.cfa.base == CFI_UNDEFINED)
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002610 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002611 WARN("%s: unexpected end of section", sec->name);
2612 return 1;
2613 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002614
2615 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002616 }
2617
2618 return 0;
2619}
2620
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002621static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002622{
2623 struct instruction *insn;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002624 struct insn_state state;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002625 int ret, warnings = 0;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002626
2627 if (!file->hints)
2628 return 0;
2629
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002630 init_insn_state(&state, sec);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002631
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002632 if (sec) {
2633 insn = find_insn(file, sec, 0);
2634 if (!insn)
2635 return 0;
2636 } else {
2637 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2638 }
2639
2640 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002641 if (insn->hint && !insn->visited) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002642 ret = validate_branch(file, insn->func, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002643 if (ret && backtrace)
2644 BT_FUNC("<=== (hint)", insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002645 warnings += ret;
2646 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002647
2648 insn = list_next_entry(insn, list);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002649 }
2650
2651 return warnings;
2652}
2653
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002654static int validate_retpoline(struct objtool_file *file)
2655{
2656 struct instruction *insn;
2657 int warnings = 0;
2658
2659 for_each_insn(file, insn) {
2660 if (insn->type != INSN_JUMP_DYNAMIC &&
2661 insn->type != INSN_CALL_DYNAMIC)
2662 continue;
2663
2664 if (insn->retpoline_safe)
2665 continue;
2666
Peter Zijlstraca41b972018-01-31 10:18:28 +01002667 /*
2668 * .init.text code is ran before userspace and thus doesn't
2669 * strictly need retpolines, except for modules which are
2670 * loaded late, they very much do need retpoline in their
2671 * .init.text
2672 */
2673 if (!strcmp(insn->sec->name, ".init.text") && !module)
2674 continue;
2675
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002676 WARN_FUNC("indirect %s found in RETPOLINE build",
2677 insn->sec, insn->offset,
2678 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2679
2680 warnings++;
2681 }
2682
2683 return warnings;
2684}
2685
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002686static bool is_kasan_insn(struct instruction *insn)
2687{
2688 return (insn->type == INSN_CALL &&
2689 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2690}
2691
2692static bool is_ubsan_insn(struct instruction *insn)
2693{
2694 return (insn->type == INSN_CALL &&
2695 !strcmp(insn->call_dest->name,
2696 "__ubsan_handle_builtin_unreachable"));
2697}
2698
Ilie Halip14db1f02020-09-19 09:41:18 +03002699static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002700{
2701 int i;
Ilie Halip14db1f02020-09-19 09:41:18 +03002702 struct instruction *prev_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002703
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002704 if (insn->ignore || insn->type == INSN_NOP)
2705 return true;
2706
2707 /*
2708 * Ignore any unused exceptions. This can happen when a whitelisted
2709 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002710 *
2711 * Also ignore alternative replacement instructions. This can happen
2712 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002713 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002714 if (!strcmp(insn->sec->name, ".fixup") ||
2715 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2716 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002717 return true;
2718
Julien Thierryfb136212020-09-15 08:53:17 +01002719 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->offset == FAKE_JUMP_OFFSET)
2720 return true;
2721
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002722 if (!insn->func)
2723 return false;
2724
2725 /*
2726 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2727 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2728 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2729 * (or occasionally a JMP to UD2).
Ilie Halip14db1f02020-09-19 09:41:18 +03002730 *
2731 * It may also insert a UD2 after calling a __noreturn function.
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002732 */
Ilie Halip14db1f02020-09-19 09:41:18 +03002733 prev_insn = list_prev_entry(insn, list);
2734 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002735 (insn->type == INSN_BUG ||
2736 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2737 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2738 return true;
2739
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002740 /*
2741 * Check if this (or a subsequent) instruction is related to
2742 * CONFIG_UBSAN or CONFIG_KASAN.
2743 *
2744 * End the search at 5 instructions to avoid going into the weeds.
2745 */
2746 for (i = 0; i < 5; i++) {
2747
2748 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2749 return true;
2750
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002751 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2752 if (insn->jump_dest &&
2753 insn->jump_dest->func == insn->func) {
2754 insn = insn->jump_dest;
2755 continue;
2756 }
2757
2758 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002759 }
2760
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002761 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002762 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002763
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002764 insn = list_next_entry(insn, list);
2765 }
2766
2767 return false;
2768}
2769
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002770static int validate_symbol(struct objtool_file *file, struct section *sec,
2771 struct symbol *sym, struct insn_state *state)
2772{
2773 struct instruction *insn;
2774 int ret;
2775
2776 if (!sym->len) {
2777 WARN("%s() is missing an ELF size annotation", sym->name);
2778 return 1;
2779 }
2780
2781 if (sym->pfunc != sym || sym->alias != sym)
2782 return 0;
2783
2784 insn = find_insn(file, sec, sym->offset);
2785 if (!insn || insn->ignore || insn->visited)
2786 return 0;
2787
2788 state->uaccess = sym->uaccess_safe;
2789
2790 ret = validate_branch(file, insn->func, insn, *state);
2791 if (ret && backtrace)
2792 BT_FUNC("<=== (sym)", insn);
2793 return ret;
2794}
2795
Peter Zijlstra350994b2020-03-23 20:57:13 +01002796static int validate_section(struct objtool_file *file, struct section *sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002797{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002798 struct insn_state state;
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002799 struct symbol *func;
2800 int warnings = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002801
Peter Zijlstra350994b2020-03-23 20:57:13 +01002802 list_for_each_entry(func, &sec->symbol_list, list) {
2803 if (func->type != STT_FUNC)
2804 continue;
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002805
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002806 init_insn_state(&state, sec);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002807 state.cfi.cfa = initial_func_cfi.cfa;
2808 memcpy(&state.cfi.regs, &initial_func_cfi.regs,
Julien Thierry0699e552020-03-27 15:28:40 +00002809 CFI_NUM_REGS * sizeof(struct cfi_reg));
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002810 state.cfi.stack_size = initial_func_cfi.cfa.offset;
Julien Thierry0699e552020-03-27 15:28:40 +00002811
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002812 warnings += validate_symbol(file, sec, func, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002813 }
2814
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002815 return warnings;
2816}
2817
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002818static int validate_vmlinux_functions(struct objtool_file *file)
2819{
2820 struct section *sec;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002821 int warnings = 0;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002822
2823 sec = find_section_by_name(file->elf, ".noinstr.text");
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002824 if (sec) {
2825 warnings += validate_section(file, sec);
2826 warnings += validate_unwind_hints(file, sec);
2827 }
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002828
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002829 sec = find_section_by_name(file->elf, ".entry.text");
2830 if (sec) {
2831 warnings += validate_section(file, sec);
2832 warnings += validate_unwind_hints(file, sec);
2833 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002834
2835 return warnings;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002836}
2837
Peter Zijlstra350994b2020-03-23 20:57:13 +01002838static int validate_functions(struct objtool_file *file)
2839{
2840 struct section *sec;
2841 int warnings = 0;
2842
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002843 for_each_sec(file, sec) {
2844 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2845 continue;
2846
Peter Zijlstra350994b2020-03-23 20:57:13 +01002847 warnings += validate_section(file, sec);
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002848 }
Peter Zijlstra350994b2020-03-23 20:57:13 +01002849
2850 return warnings;
2851}
2852
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002853static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002854{
2855 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002856
2857 if (file->ignore_unreachables)
2858 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002859
2860 for_each_insn(file, insn) {
Ilie Halip14db1f02020-09-19 09:41:18 +03002861 if (insn->visited || ignore_unreachable_insn(file, insn))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002862 continue;
2863
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002864 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2865 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002866 }
2867
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002868 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002869}
2870
Julien Thierryd44becb2020-08-25 13:47:40 +01002871int check(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002872{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002873 int ret, warnings = 0;
2874
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002875 arch_initial_func_cfi_state(&initial_func_cfi);
2876
Julien Thierry6545eb02020-08-25 13:47:39 +01002877 ret = decode_sections(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002878 if (ret < 0)
2879 goto out;
2880 warnings += ret;
2881
Julien Thierry6545eb02020-08-25 13:47:39 +01002882 if (list_empty(&file->insn_list))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002883 goto out;
2884
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002885 if (vmlinux && !validate_dup) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002886 ret = validate_vmlinux_functions(file);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002887 if (ret < 0)
2888 goto out;
2889
2890 warnings += ret;
2891 goto out;
2892 }
2893
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002894 if (retpoline) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002895 ret = validate_retpoline(file);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002896 if (ret < 0)
2897 return ret;
2898 warnings += ret;
2899 }
2900
Julien Thierry6545eb02020-08-25 13:47:39 +01002901 ret = validate_functions(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002902 if (ret < 0)
2903 goto out;
2904 warnings += ret;
2905
Julien Thierry6545eb02020-08-25 13:47:39 +01002906 ret = validate_unwind_hints(file, NULL);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002907 if (ret < 0)
2908 goto out;
2909 warnings += ret;
2910
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002911 if (!warnings) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002912 ret = validate_reachable_instructions(file);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002913 if (ret < 0)
2914 goto out;
2915 warnings += ret;
2916 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002917
Julien Thierry6545eb02020-08-25 13:47:39 +01002918 ret = create_static_call_sections(file);
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002919 if (ret < 0)
2920 goto out;
2921 warnings += ret;
2922
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002923out:
Josh Poimboeuf644592d2020-02-10 12:32:38 -06002924 if (ret < 0) {
2925 /*
2926 * Fatal error. The binary is corrupt or otherwise broken in
2927 * some way, or objtool itself is broken. Fail the kernel
2928 * build.
2929 */
2930 return ret;
2931 }
2932
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002933 return 0;
2934}