blob: 44e3ee1425cd5e97ba719ce53524fdca9cc13627 [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));
Josh Poimboeuf44f6a7c2020-12-14 16:04:20 -0600470
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200471 if (!reloc) {
472 perror("malloc");
473 return -1;
474 }
475 memset(reloc, 0, sizeof(*reloc));
Josh Poimboeuf44f6a7c2020-12-14 16:04:20 -0600476
477 insn_to_reloc_sym_addend(insn->sec, insn->offset, reloc);
478 if (!reloc->sym) {
479 WARN_FUNC("static call tramp: missing containing symbol",
480 insn->sec, insn->offset);
481 return -1;
482 }
483
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200484 reloc->type = R_X86_64_PC32;
485 reloc->offset = idx * sizeof(struct static_call_site);
486 reloc->sec = reloc_sec;
487 elf_add_reloc(file->elf, reloc);
488
489 /* find key symbol */
490 key_name = strdup(insn->call_dest->name);
491 if (!key_name) {
492 perror("strdup");
493 return -1;
494 }
495 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
496 STATIC_CALL_TRAMP_PREFIX_LEN)) {
497 WARN("static_call: trampoline name malformed: %s", key_name);
498 return -1;
499 }
500 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
501 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
502
503 key_sym = find_symbol_by_name(file->elf, tmp);
504 if (!key_sym) {
505 WARN("static_call: can't find static_call_key symbol: %s", tmp);
506 return -1;
507 }
508 free(key_name);
509
510 /* populate reloc for 'key' */
511 reloc = malloc(sizeof(*reloc));
512 if (!reloc) {
513 perror("malloc");
514 return -1;
515 }
516 memset(reloc, 0, sizeof(*reloc));
517 reloc->sym = key_sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200518 reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200519 reloc->type = R_X86_64_PC32;
520 reloc->offset = idx * sizeof(struct static_call_site) + 4;
521 reloc->sec = reloc_sec;
522 elf_add_reloc(file->elf, reloc);
523
524 idx++;
525 }
526
527 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
528 return -1;
529
530 return 0;
531}
532
Peter Zijlstra99d00212020-08-06 15:14:09 -0700533static int create_mcount_loc_sections(struct objtool_file *file)
534{
535 struct section *sec, *reloc_sec;
536 struct reloc *reloc;
537 unsigned long *loc;
538 struct instruction *insn;
539 int idx;
540
541 sec = find_section_by_name(file->elf, "__mcount_loc");
542 if (sec) {
543 INIT_LIST_HEAD(&file->mcount_loc_list);
544 WARN("file already has __mcount_loc section, skipping");
545 return 0;
546 }
547
548 if (list_empty(&file->mcount_loc_list))
549 return 0;
550
551 idx = 0;
552 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node)
553 idx++;
554
555 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
556 if (!sec)
557 return -1;
558
559 reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
560 if (!reloc_sec)
561 return -1;
562
563 idx = 0;
564 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node) {
565
566 loc = (unsigned long *)sec->data->d_buf + idx;
567 memset(loc, 0, sizeof(unsigned long));
568
569 reloc = malloc(sizeof(*reloc));
570 if (!reloc) {
571 perror("malloc");
572 return -1;
573 }
574 memset(reloc, 0, sizeof(*reloc));
575
576 reloc->sym = insn->sec->sym;
577 reloc->addend = insn->offset;
578 reloc->type = R_X86_64_64;
579 reloc->offset = idx * sizeof(unsigned long);
580 reloc->sec = reloc_sec;
581 elf_add_reloc(file->elf, reloc);
582
583 idx++;
584 }
585
586 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
587 return -1;
588
589 return 0;
590}
591
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500592/*
593 * Warnings shouldn't be reported for ignored functions.
594 */
595static void add_ignores(struct objtool_file *file)
596{
597 struct instruction *insn;
598 struct section *sec;
599 struct symbol *func;
Matt Helsleyf1974222020-05-29 14:01:13 -0700600 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500601
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100602 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
603 if (!sec)
604 return;
605
Matt Helsleyf1974222020-05-29 14:01:13 -0700606 list_for_each_entry(reloc, &sec->reloc_list, list) {
607 switch (reloc->sym->type) {
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100608 case STT_FUNC:
Matt Helsleyf1974222020-05-29 14:01:13 -0700609 func = reloc->sym;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100610 break;
611
612 case STT_SECTION:
Matt Helsleyf1974222020-05-29 14:01:13 -0700613 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600614 if (!func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500615 continue;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100616 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500617
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100618 default:
Matt Helsleyf1974222020-05-29 14:01:13 -0700619 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100620 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500621 }
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100622
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100623 func_for_each_insn(file, func, insn)
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100624 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500625 }
626}
627
628/*
Peter Zijlstraea242132019-02-25 12:50:09 +0100629 * This is a whitelist of functions that is allowed to be called with AC set.
630 * The list is meant to be minimal and only contains compiler instrumentation
631 * ABI and a few functions used to implement *_{to,from}_user() functions.
632 *
633 * These functions must not directly change AC, but may PUSHF/POPF.
634 */
635static const char *uaccess_safe_builtin[] = {
636 /* KASAN */
637 "kasan_report",
638 "check_memory_region",
639 /* KASAN out-of-line */
640 "__asan_loadN_noabort",
641 "__asan_load1_noabort",
642 "__asan_load2_noabort",
643 "__asan_load4_noabort",
644 "__asan_load8_noabort",
645 "__asan_load16_noabort",
646 "__asan_storeN_noabort",
647 "__asan_store1_noabort",
648 "__asan_store2_noabort",
649 "__asan_store4_noabort",
650 "__asan_store8_noabort",
651 "__asan_store16_noabort",
Jann Hornb0b8e562020-09-29 00:49:16 +0200652 "__kasan_check_read",
653 "__kasan_check_write",
Peter Zijlstraea242132019-02-25 12:50:09 +0100654 /* KASAN in-line */
655 "__asan_report_load_n_noabort",
656 "__asan_report_load1_noabort",
657 "__asan_report_load2_noabort",
658 "__asan_report_load4_noabort",
659 "__asan_report_load8_noabort",
660 "__asan_report_load16_noabort",
661 "__asan_report_store_n_noabort",
662 "__asan_report_store1_noabort",
663 "__asan_report_store2_noabort",
664 "__asan_report_store4_noabort",
665 "__asan_report_store8_noabort",
666 "__asan_report_store16_noabort",
Marco Elver5f5c9712019-11-14 19:02:57 +0100667 /* KCSAN */
Marco Elver99676832020-03-25 17:41:57 +0100668 "__kcsan_check_access",
Marco Elver5f5c9712019-11-14 19:02:57 +0100669 "kcsan_found_watchpoint",
670 "kcsan_setup_watchpoint",
Marco Elver99676832020-03-25 17:41:57 +0100671 "kcsan_check_scoped_accesses",
Marco Elver50a19ad2020-04-24 17:47:30 +0200672 "kcsan_disable_current",
673 "kcsan_enable_current_nowarn",
Marco Elver5f5c9712019-11-14 19:02:57 +0100674 /* KCSAN/TSAN */
675 "__tsan_func_entry",
676 "__tsan_func_exit",
677 "__tsan_read_range",
678 "__tsan_write_range",
679 "__tsan_read1",
680 "__tsan_read2",
681 "__tsan_read4",
682 "__tsan_read8",
683 "__tsan_read16",
684 "__tsan_write1",
685 "__tsan_write2",
686 "__tsan_write4",
687 "__tsan_write8",
688 "__tsan_write16",
Marco Elvera81b3752020-07-24 09:00:02 +0200689 "__tsan_read_write1",
690 "__tsan_read_write2",
691 "__tsan_read_write4",
692 "__tsan_read_write8",
693 "__tsan_read_write16",
Marco Elver883957b2020-07-03 15:40:30 +0200694 "__tsan_atomic8_load",
695 "__tsan_atomic16_load",
696 "__tsan_atomic32_load",
697 "__tsan_atomic64_load",
698 "__tsan_atomic8_store",
699 "__tsan_atomic16_store",
700 "__tsan_atomic32_store",
701 "__tsan_atomic64_store",
702 "__tsan_atomic8_exchange",
703 "__tsan_atomic16_exchange",
704 "__tsan_atomic32_exchange",
705 "__tsan_atomic64_exchange",
706 "__tsan_atomic8_fetch_add",
707 "__tsan_atomic16_fetch_add",
708 "__tsan_atomic32_fetch_add",
709 "__tsan_atomic64_fetch_add",
710 "__tsan_atomic8_fetch_sub",
711 "__tsan_atomic16_fetch_sub",
712 "__tsan_atomic32_fetch_sub",
713 "__tsan_atomic64_fetch_sub",
714 "__tsan_atomic8_fetch_and",
715 "__tsan_atomic16_fetch_and",
716 "__tsan_atomic32_fetch_and",
717 "__tsan_atomic64_fetch_and",
718 "__tsan_atomic8_fetch_or",
719 "__tsan_atomic16_fetch_or",
720 "__tsan_atomic32_fetch_or",
721 "__tsan_atomic64_fetch_or",
722 "__tsan_atomic8_fetch_xor",
723 "__tsan_atomic16_fetch_xor",
724 "__tsan_atomic32_fetch_xor",
725 "__tsan_atomic64_fetch_xor",
726 "__tsan_atomic8_fetch_nand",
727 "__tsan_atomic16_fetch_nand",
728 "__tsan_atomic32_fetch_nand",
729 "__tsan_atomic64_fetch_nand",
730 "__tsan_atomic8_compare_exchange_strong",
731 "__tsan_atomic16_compare_exchange_strong",
732 "__tsan_atomic32_compare_exchange_strong",
733 "__tsan_atomic64_compare_exchange_strong",
734 "__tsan_atomic8_compare_exchange_weak",
735 "__tsan_atomic16_compare_exchange_weak",
736 "__tsan_atomic32_compare_exchange_weak",
737 "__tsan_atomic64_compare_exchange_weak",
738 "__tsan_atomic8_compare_exchange_val",
739 "__tsan_atomic16_compare_exchange_val",
740 "__tsan_atomic32_compare_exchange_val",
741 "__tsan_atomic64_compare_exchange_val",
742 "__tsan_atomic_thread_fence",
743 "__tsan_atomic_signal_fence",
Peter Zijlstraea242132019-02-25 12:50:09 +0100744 /* KCOV */
745 "write_comp_data",
Josh Poimboeufae033f02020-04-29 14:09:04 -0500746 "check_kcov_mode",
Peter Zijlstraea242132019-02-25 12:50:09 +0100747 "__sanitizer_cov_trace_pc",
748 "__sanitizer_cov_trace_const_cmp1",
749 "__sanitizer_cov_trace_const_cmp2",
750 "__sanitizer_cov_trace_const_cmp4",
751 "__sanitizer_cov_trace_const_cmp8",
752 "__sanitizer_cov_trace_cmp1",
753 "__sanitizer_cov_trace_cmp2",
754 "__sanitizer_cov_trace_cmp4",
755 "__sanitizer_cov_trace_cmp8",
Al Viro36b1c702020-02-16 13:07:49 -0500756 "__sanitizer_cov_trace_switch",
Peter Zijlstraea242132019-02-25 12:50:09 +0100757 /* UBSAN */
758 "ubsan_type_mismatch_common",
759 "__ubsan_handle_type_mismatch",
760 "__ubsan_handle_type_mismatch_v1",
Peter Zijlstra9a50dca2019-10-21 15:11:49 +0200761 "__ubsan_handle_shift_out_of_bounds",
Peter Zijlstraea242132019-02-25 12:50:09 +0100762 /* misc */
763 "csum_partial_copy_generic",
Dan Williamsec6347b2020-10-05 20:40:16 -0700764 "copy_mc_fragile",
765 "copy_mc_fragile_handle_tail",
Dan Williams5da8e4a2020-10-05 20:40:25 -0700766 "copy_mc_enhanced_fast_string",
Peter Zijlstraea242132019-02-25 12:50:09 +0100767 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
768 NULL
769};
770
771static void add_uaccess_safe(struct objtool_file *file)
772{
773 struct symbol *func;
774 const char **name;
775
776 if (!uaccess)
777 return;
778
779 for (name = uaccess_safe_builtin; *name; name++) {
780 func = find_symbol_by_name(file->elf, *name);
781 if (!func)
782 continue;
783
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500784 func->uaccess_safe = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500785 }
786}
787
788/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000789 * FIXME: For now, just ignore any alternatives which add retpolines. This is
790 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
791 * But it at least allows objtool to understand the control flow *around* the
792 * retpoline.
793 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100794static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000795{
796 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700797 struct reloc *reloc;
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000798 struct instruction *insn;
799
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100800 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000801 if (!sec)
802 return 0;
803
Matt Helsleyf1974222020-05-29 14:01:13 -0700804 list_for_each_entry(reloc, &sec->reloc_list, list) {
805 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000806 WARN("unexpected relocation symbol type in %s", sec->name);
807 return -1;
808 }
809
Matt Helsleyf1974222020-05-29 14:01:13 -0700810 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000811 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100812 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000813 return -1;
814 }
815
816 insn->ignore_alts = true;
817 }
818
819 return 0;
820}
821
822/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500823 * Find the destination instructions for all jumps.
824 */
825static int add_jump_destinations(struct objtool_file *file)
826{
827 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -0700828 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500829 struct section *dest_sec;
830 unsigned long dest_off;
831
832 for_each_insn(file, insn) {
Josh Poimboeufa2296142020-02-10 12:32:39 -0600833 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500834 continue;
835
Josh Poimboeufdb6c6a02020-09-10 10:24:57 -0500836 if (insn->offset == FAKE_JUMP_OFFSET)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500837 continue;
838
Matt Helsleyf1974222020-05-29 14:01:13 -0700839 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100840 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700841 if (!reloc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500842 dest_sec = insn->sec;
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000843 dest_off = arch_jump_destination(insn);
Matt Helsleyf1974222020-05-29 14:01:13 -0700844 } else if (reloc->sym->type == STT_SECTION) {
845 dest_sec = reloc->sym->sec;
846 dest_off = arch_dest_reloc_offset(reloc->addend);
847 } else if (reloc->sym->sec->idx) {
848 dest_sec = reloc->sym->sec;
849 dest_off = reloc->sym->sym.st_value +
850 arch_dest_reloc_offset(reloc->addend);
851 } else if (strstr(reloc->sym->name, "_indirect_thunk_")) {
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000852 /*
853 * Retpoline jumps are really dynamic jumps in
854 * disguise, so convert them accordingly.
855 */
Josh Poimboeufb68b9902019-07-17 20:36:57 -0500856 if (insn->type == INSN_JUMP_UNCONDITIONAL)
857 insn->type = INSN_JUMP_DYNAMIC;
858 else
859 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
860
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100861 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000862 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500863 } else {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500864 /* external sibling call */
Matt Helsleyf1974222020-05-29 14:01:13 -0700865 insn->call_dest = reloc->sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200866 if (insn->call_dest->static_call_tramp) {
867 list_add_tail(&insn->static_call_node,
868 &file->static_call_list);
869 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500870 continue;
871 }
872
873 insn->jump_dest = find_insn(file, dest_sec, dest_off);
874 if (!insn->jump_dest) {
875
876 /*
877 * This is a special case where an alt instruction
878 * jumps past the end of the section. These are
879 * handled later in handle_group_alt().
880 */
881 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
882 continue;
883
884 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
885 insn->sec, insn->offset, dest_sec->name,
886 dest_off);
887 return -1;
888 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500889
890 /*
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100891 * Cross-function jump.
Josh Poimboeufcd778492018-06-01 07:23:51 -0500892 */
893 if (insn->func && insn->jump_dest->func &&
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100894 insn->func != insn->jump_dest->func) {
895
896 /*
897 * For GCC 8+, create parent/child links for any cold
898 * subfunctions. This is _mostly_ redundant with a
899 * similar initialization in read_symbols().
900 *
901 * If a function has aliases, we want the *first* such
902 * function in the symbol table to be the subfunction's
903 * parent. In that case we overwrite the
904 * initialization done in read_symbols().
905 *
906 * However this code can't completely replace the
907 * read_symbols() code because this doesn't detect the
908 * case where the parent function's only reference to a
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500909 * subfunction is through a jump table.
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100910 */
911 if (!strstr(insn->func->name, ".cold.") &&
912 strstr(insn->jump_dest->func->name, ".cold.")) {
913 insn->func->cfunc = insn->jump_dest->func;
914 insn->jump_dest->func->pfunc = insn->func;
915
916 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
917 insn->jump_dest->offset == insn->jump_dest->func->offset) {
918
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500919 /* internal sibling call */
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100920 insn->call_dest = insn->jump_dest->func;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200921 if (insn->call_dest->static_call_tramp) {
922 list_add_tail(&insn->static_call_node,
923 &file->static_call_list);
924 }
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100925 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500926 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500927 }
928
929 return 0;
930}
931
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200932static void remove_insn_ops(struct instruction *insn)
933{
934 struct stack_op *op, *tmp;
935
936 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
937 list_del(&op->list);
938 free(op);
939 }
940}
941
Julien Thierry2b232a22020-09-15 08:53:18 +0100942static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
943{
944 struct symbol *call_dest;
945
946 call_dest = find_func_by_offset(sec, offset);
947 if (!call_dest)
948 call_dest = find_symbol_by_offset(sec, offset);
949
950 return call_dest;
951}
952
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500953/*
954 * Find the destination instructions for all calls.
955 */
956static int add_call_destinations(struct objtool_file *file)
957{
958 struct instruction *insn;
959 unsigned long dest_off;
Matt Helsleyf1974222020-05-29 14:01:13 -0700960 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500961
962 for_each_insn(file, insn) {
963 if (insn->type != INSN_CALL)
964 continue;
965
Matt Helsleyf1974222020-05-29 14:01:13 -0700966 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100967 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700968 if (!reloc) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000969 dest_off = arch_jump_destination(insn);
Julien Thierry2b232a22020-09-15 08:53:18 +0100970 insn->call_dest = find_call_destination(insn->sec, dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600971
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600972 if (insn->ignore)
973 continue;
974
975 if (!insn->call_dest) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200976 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500977 return -1;
978 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600979
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600980 if (insn->func && insn->call_dest->type != STT_FUNC) {
981 WARN_FUNC("unsupported call to non-function",
982 insn->sec, insn->offset);
983 return -1;
984 }
985
Matt Helsleyf1974222020-05-29 14:01:13 -0700986 } else if (reloc->sym->type == STT_SECTION) {
987 dest_off = arch_dest_reloc_offset(reloc->addend);
Julien Thierry2b232a22020-09-15 08:53:18 +0100988 insn->call_dest = find_call_destination(reloc->sym->sec,
989 dest_off);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600990 if (!insn->call_dest) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000991 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500992 insn->sec, insn->offset,
Matt Helsleyf1974222020-05-29 14:01:13 -0700993 reloc->sym->sec->name,
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000994 dest_off);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500995 return -1;
996 }
997 } else
Matt Helsleyf1974222020-05-29 14:01:13 -0700998 insn->call_dest = reloc->sym;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200999
1000 /*
Peter Zijlstra0f1441b2020-06-12 16:05:26 +02001001 * Many compilers cannot disable KCOV with a function attribute
1002 * so they need a little help, NOP out any KCOV calls from noinstr
1003 * text.
1004 */
1005 if (insn->sec->noinstr &&
1006 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
Peter Zijlstrad832c002020-06-18 17:55:29 +02001007 if (reloc) {
1008 reloc->type = R_NONE;
1009 elf_write_reloc(file->elf, reloc);
Peter Zijlstra0f1441b2020-06-12 16:05:26 +02001010 }
1011
1012 elf_write_insn(file->elf, insn->sec,
1013 insn->offset, insn->len,
1014 arch_nop_insn(insn->len));
1015 insn->type = INSN_NOP;
1016 }
1017
Peter Zijlstra99d00212020-08-06 15:14:09 -07001018 if (mcount && !strcmp(insn->call_dest->name, "__fentry__")) {
1019 if (reloc) {
1020 reloc->type = R_NONE;
1021 elf_write_reloc(file->elf, reloc);
1022 }
1023
1024 elf_write_insn(file->elf, insn->sec,
1025 insn->offset, insn->len,
1026 arch_nop_insn(insn->len));
1027
1028 insn->type = INSN_NOP;
1029
1030 list_add_tail(&insn->mcount_loc_node,
1031 &file->mcount_loc_list);
1032 }
1033
Peter Zijlstra0f1441b2020-06-12 16:05:26 +02001034 /*
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001035 * Whatever stack impact regular CALLs have, should be undone
1036 * by the RETURN of the called function.
1037 *
1038 * Annotated intra-function calls retain the stack_ops but
1039 * are converted to JUMP, see read_intra_function_calls().
1040 */
1041 remove_insn_ops(insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001042 }
1043
1044 return 0;
1045}
1046
1047/*
1048 * The .alternatives section requires some extra special care, over and above
1049 * what other special sections require:
1050 *
1051 * 1. Because alternatives are patched in-place, we need to insert a fake jump
1052 * instruction at the end so that validate_branch() skips all the original
1053 * replaced instructions when validating the new instruction path.
1054 *
1055 * 2. An added wrinkle is that the new instruction length might be zero. In
1056 * that case the old instructions are replaced with noops. We simulate that
1057 * by creating a fake jump as the only new instruction.
1058 *
1059 * 3. In some cases, the alternative section includes an instruction which
1060 * conditionally jumps to the _end_ of the entry. We have to modify these
1061 * jumps' destinations to point back to .text rather than the end of the
1062 * entry in .altinstr_replacement.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001063 */
1064static int handle_group_alt(struct objtool_file *file,
1065 struct special_alt *special_alt,
1066 struct instruction *orig_insn,
1067 struct instruction **new_insn)
1068{
Alexandre Chartre13fab062020-04-14 12:36:11 +02001069 static unsigned int alt_group_next_index = 1;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001070 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Alexandre Chartre13fab062020-04-14 12:36:11 +02001071 unsigned int alt_group = alt_group_next_index++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001072 unsigned long dest_off;
1073
1074 last_orig_insn = NULL;
1075 insn = orig_insn;
1076 sec_for_each_insn_from(file, insn) {
1077 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1078 break;
1079
Alexandre Chartre13fab062020-04-14 12:36:11 +02001080 insn->alt_group = alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001081 last_orig_insn = insn;
1082 }
1083
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001084 if (next_insn_same_sec(file, last_orig_insn)) {
1085 fake_jump = malloc(sizeof(*fake_jump));
1086 if (!fake_jump) {
1087 WARN("malloc failed");
1088 return -1;
1089 }
1090 memset(fake_jump, 0, sizeof(*fake_jump));
1091 INIT_LIST_HEAD(&fake_jump->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +00001092 INIT_LIST_HEAD(&fake_jump->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001093 init_cfi_state(&fake_jump->cfi);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001094
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001095 fake_jump->sec = special_alt->new_sec;
Josh Poimboeufe6da9562019-05-13 12:01:31 -05001096 fake_jump->offset = FAKE_JUMP_OFFSET;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001097 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
1098 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
Josh Poimboeufe6da9562019-05-13 12:01:31 -05001099 fake_jump->func = orig_insn->func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001100 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001101
1102 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001103 if (!fake_jump) {
1104 WARN("%s: empty alternative at end of section",
1105 special_alt->orig_sec->name);
1106 return -1;
1107 }
1108
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001109 *new_insn = fake_jump;
1110 return 0;
1111 }
1112
1113 last_new_insn = NULL;
Alexandre Chartre13fab062020-04-14 12:36:11 +02001114 alt_group = alt_group_next_index++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001115 insn = *new_insn;
1116 sec_for_each_insn_from(file, insn) {
Julien Thierry45245f52020-09-04 16:30:23 +01001117 struct reloc *alt_reloc;
1118
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001119 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1120 break;
1121
1122 last_new_insn = insn;
1123
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001124 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +01001125 insn->func = orig_insn->func;
Alexandre Chartre13fab062020-04-14 12:36:11 +02001126 insn->alt_group = alt_group;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001127
Josh Poimboeufdc419722020-02-10 12:32:40 -06001128 /*
1129 * Since alternative replacement code is copy/pasted by the
1130 * kernel after applying relocations, generally such code can't
1131 * have relative-address relocation references to outside the
1132 * .altinstr_replacement section, unless the arch's
1133 * alternatives code can adjust the relative offsets
1134 * accordingly.
Josh Poimboeufdc419722020-02-10 12:32:40 -06001135 */
Julien Thierry45245f52020-09-04 16:30:23 +01001136 alt_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1137 insn->offset, insn->len);
1138 if (alt_reloc &&
1139 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
Josh Poimboeufdc419722020-02-10 12:32:40 -06001140
1141 WARN_FUNC("unsupported relocation in alternatives section",
1142 insn->sec, insn->offset);
1143 return -1;
1144 }
1145
Josh Poimboeufa2296142020-02-10 12:32:39 -06001146 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001147 continue;
1148
1149 if (!insn->immediate)
1150 continue;
1151
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001152 dest_off = arch_jump_destination(insn);
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001153 if (dest_off == special_alt->new_off + special_alt->new_len) {
1154 if (!fake_jump) {
1155 WARN("%s: alternative jump to end of section",
1156 special_alt->orig_sec->name);
1157 return -1;
1158 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001159 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001160 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001161
1162 if (!insn->jump_dest) {
1163 WARN_FUNC("can't find alternative jump destination",
1164 insn->sec, insn->offset);
1165 return -1;
1166 }
1167 }
1168
1169 if (!last_new_insn) {
1170 WARN_FUNC("can't find last new alternative instruction",
1171 special_alt->new_sec, special_alt->new_off);
1172 return -1;
1173 }
1174
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001175 if (fake_jump)
1176 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001177
1178 return 0;
1179}
1180
1181/*
1182 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1183 * If the original instruction is a jump, make the alt entry an effective nop
1184 * by just skipping the original instruction.
1185 */
1186static int handle_jump_alt(struct objtool_file *file,
1187 struct special_alt *special_alt,
1188 struct instruction *orig_insn,
1189 struct instruction **new_insn)
1190{
1191 if (orig_insn->type == INSN_NOP)
1192 return 0;
1193
1194 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1195 WARN_FUNC("unsupported instruction at jump label",
1196 orig_insn->sec, orig_insn->offset);
1197 return -1;
1198 }
1199
1200 *new_insn = list_next_entry(orig_insn, list);
1201 return 0;
1202}
1203
1204/*
1205 * Read all the special sections which have alternate instructions which can be
1206 * patched in or redirected to at runtime. Each instruction having alternate
1207 * instruction(s) has them added to its insn->alts list, which will be
1208 * traversed in validate_branch().
1209 */
1210static int add_special_section_alts(struct objtool_file *file)
1211{
1212 struct list_head special_alts;
1213 struct instruction *orig_insn, *new_insn;
1214 struct special_alt *special_alt, *tmp;
1215 struct alternative *alt;
1216 int ret;
1217
1218 ret = special_get_alts(file->elf, &special_alts);
1219 if (ret)
1220 return ret;
1221
1222 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001223
1224 orig_insn = find_insn(file, special_alt->orig_sec,
1225 special_alt->orig_off);
1226 if (!orig_insn) {
1227 WARN_FUNC("special: can't find orig instruction",
1228 special_alt->orig_sec, special_alt->orig_off);
1229 ret = -1;
1230 goto out;
1231 }
1232
1233 new_insn = NULL;
1234 if (!special_alt->group || special_alt->new_len) {
1235 new_insn = find_insn(file, special_alt->new_sec,
1236 special_alt->new_off);
1237 if (!new_insn) {
1238 WARN_FUNC("special: can't find new instruction",
1239 special_alt->new_sec,
1240 special_alt->new_off);
1241 ret = -1;
1242 goto out;
1243 }
1244 }
1245
1246 if (special_alt->group) {
Julien Thierry7170cf42020-03-27 15:28:41 +00001247 if (!special_alt->orig_len) {
1248 WARN_FUNC("empty alternative entry",
1249 orig_insn->sec, orig_insn->offset);
1250 continue;
1251 }
1252
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001253 ret = handle_group_alt(file, special_alt, orig_insn,
1254 &new_insn);
1255 if (ret)
1256 goto out;
1257 } else if (special_alt->jump_or_nop) {
1258 ret = handle_jump_alt(file, special_alt, orig_insn,
1259 &new_insn);
1260 if (ret)
1261 goto out;
1262 }
1263
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001264 alt = malloc(sizeof(*alt));
1265 if (!alt) {
1266 WARN("malloc failed");
1267 ret = -1;
1268 goto out;
1269 }
1270
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001271 alt->insn = new_insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +01001272 alt->skip_orig = special_alt->skip_orig;
Peter Zijlstraea242132019-02-25 12:50:09 +01001273 orig_insn->ignore_alts |= special_alt->skip_alt;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001274 list_add_tail(&alt->list, &orig_insn->alts);
1275
1276 list_del(&special_alt->list);
1277 free(special_alt);
1278 }
1279
1280out:
1281 return ret;
1282}
1283
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001284static int add_jump_table(struct objtool_file *file, struct instruction *insn,
Matt Helsleyf1974222020-05-29 14:01:13 -07001285 struct reloc *table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001286{
Matt Helsleyf1974222020-05-29 14:01:13 -07001287 struct reloc *reloc = table;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001288 struct instruction *dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001289 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001290 struct symbol *pfunc = insn->func->pfunc;
1291 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001292
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001293 /*
Matt Helsleyf1974222020-05-29 14:01:13 -07001294 * Each @reloc is a switch table relocation which points to the target
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001295 * instruction.
1296 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001297 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
Jann Hornbd98c812019-07-17 20:36:54 -05001298
1299 /* Check for the end of the table: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001300 if (reloc != table && reloc->jump_table_start)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001301 break;
1302
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001303 /* Make sure the table entries are consecutive: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001304 if (prev_offset && reloc->offset != prev_offset + 8)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001305 break;
1306
1307 /* Detect function pointers from contiguous objects: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001308 if (reloc->sym->sec == pfunc->sec &&
1309 reloc->addend == pfunc->offset)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001310 break;
1311
Matt Helsleyf1974222020-05-29 14:01:13 -07001312 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001313 if (!dest_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001314 break;
1315
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001316 /* Make sure the destination is in the same function: */
Josh Poimboeufe65050b2019-07-17 20:36:55 -05001317 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -05001318 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001319
1320 alt = malloc(sizeof(*alt));
1321 if (!alt) {
1322 WARN("malloc failed");
1323 return -1;
1324 }
1325
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001326 alt->insn = dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001327 list_add_tail(&alt->list, &insn->alts);
Matt Helsleyf1974222020-05-29 14:01:13 -07001328 prev_offset = reloc->offset;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001329 }
1330
1331 if (!prev_offset) {
1332 WARN_FUNC("can't find switch jump table",
1333 insn->sec, insn->offset);
1334 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001335 }
1336
1337 return 0;
1338}
1339
1340/*
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001341 * find_jump_table() - Given a dynamic jump, find the switch jump table
1342 * associated with it.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001343 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001344static struct reloc *find_jump_table(struct objtool_file *file,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001345 struct symbol *func,
1346 struct instruction *insn)
1347{
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001348 struct reloc *table_reloc;
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001349 struct instruction *dest_insn, *orig_insn = insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001350
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001351 /*
1352 * Backward search using the @first_jump_src links, these help avoid
1353 * much of the 'in between' code. Which avoids us getting confused by
1354 * it.
1355 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001356 for (;
Josh Poimboeuf1119d262020-04-28 16:45:16 -05001357 insn && insn->func && insn->func->pfunc == func;
1358 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001359
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001360 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001361 break;
1362
1363 /* allow small jumps within the range */
1364 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1365 insn->jump_dest &&
1366 (insn->jump_dest->offset <= insn->offset ||
1367 insn->jump_dest->offset > orig_insn->offset))
1368 break;
1369
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001370 table_reloc = arch_find_switch_table(file, insn);
Matt Helsleyf1974222020-05-29 14:01:13 -07001371 if (!table_reloc)
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001372 continue;
Matt Helsleyf1974222020-05-29 14:01:13 -07001373 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001374 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1375 continue;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001376
Matt Helsleyf1974222020-05-29 14:01:13 -07001377 return table_reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001378 }
1379
1380 return NULL;
1381}
1382
Jann Hornbd98c812019-07-17 20:36:54 -05001383/*
1384 * First pass: Mark the head of each jump table so that in the next pass,
1385 * we know when a given jump table ends and the next one starts.
1386 */
1387static void mark_func_jump_tables(struct objtool_file *file,
1388 struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001389{
Jann Hornbd98c812019-07-17 20:36:54 -05001390 struct instruction *insn, *last = NULL;
Matt Helsleyf1974222020-05-29 14:01:13 -07001391 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001392
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001393 func_for_each_insn(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001394 if (!last)
1395 last = insn;
1396
1397 /*
1398 * Store back-pointers for unconditional forward jumps such
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001399 * that find_jump_table() can back-track using those and
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001400 * avoid some potentially confusing code.
1401 */
1402 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1403 insn->offset > last->offset &&
1404 insn->jump_dest->offset > insn->offset &&
1405 !insn->jump_dest->first_jump_src) {
1406
1407 insn->jump_dest->first_jump_src = insn;
1408 last = insn->jump_dest;
1409 }
1410
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001411 if (insn->type != INSN_JUMP_DYNAMIC)
1412 continue;
1413
Matt Helsleyf1974222020-05-29 14:01:13 -07001414 reloc = find_jump_table(file, func, insn);
1415 if (reloc) {
1416 reloc->jump_table_start = true;
1417 insn->jump_table = reloc;
Jann Hornbd98c812019-07-17 20:36:54 -05001418 }
1419 }
1420}
1421
1422static int add_func_jump_tables(struct objtool_file *file,
1423 struct symbol *func)
1424{
1425 struct instruction *insn;
1426 int ret;
1427
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001428 func_for_each_insn(file, func, insn) {
Jann Hornbd98c812019-07-17 20:36:54 -05001429 if (!insn->jump_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001430 continue;
1431
Jann Hornbd98c812019-07-17 20:36:54 -05001432 ret = add_jump_table(file, insn, insn->jump_table);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001433 if (ret)
1434 return ret;
1435 }
1436
1437 return 0;
1438}
1439
1440/*
1441 * For some switch statements, gcc generates a jump table in the .rodata
1442 * section which contains a list of addresses within the function to jump to.
1443 * This finds these jump tables and adds them to the insn->alts lists.
1444 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001445static int add_jump_table_alts(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001446{
1447 struct section *sec;
1448 struct symbol *func;
1449 int ret;
1450
Allan Xavier4a60aa02018-09-07 08:12:01 -05001451 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001452 return 0;
1453
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001454 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001455 list_for_each_entry(func, &sec->symbol_list, list) {
1456 if (func->type != STT_FUNC)
1457 continue;
1458
Jann Hornbd98c812019-07-17 20:36:54 -05001459 mark_func_jump_tables(file, func);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001460 ret = add_func_jump_tables(file, func);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001461 if (ret)
1462 return ret;
1463 }
1464 }
1465
1466 return 0;
1467}
1468
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001469static int read_unwind_hints(struct objtool_file *file)
1470{
Matt Helsleyf1974222020-05-29 14:01:13 -07001471 struct section *sec, *relocsec;
1472 struct reloc *reloc;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001473 struct unwind_hint *hint;
1474 struct instruction *insn;
1475 struct cfi_reg *cfa;
1476 int i;
1477
1478 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1479 if (!sec)
1480 return 0;
1481
Matt Helsleyf1974222020-05-29 14:01:13 -07001482 relocsec = sec->reloc;
1483 if (!relocsec) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001484 WARN("missing .rela.discard.unwind_hints section");
1485 return -1;
1486 }
1487
1488 if (sec->len % sizeof(struct unwind_hint)) {
1489 WARN("struct unwind_hint size mismatch");
1490 return -1;
1491 }
1492
1493 file->hints = true;
1494
1495 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1496 hint = (struct unwind_hint *)sec->data->d_buf + i;
1497
Matt Helsleyf1974222020-05-29 14:01:13 -07001498 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1499 if (!reloc) {
1500 WARN("can't find reloc for unwind_hints[%d]", i);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001501 return -1;
1502 }
1503
Matt Helsleyf1974222020-05-29 14:01:13 -07001504 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001505 if (!insn) {
1506 WARN("can't find insn for unwind_hints[%d]", i);
1507 return -1;
1508 }
1509
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001510 cfa = &insn->cfi.cfa;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001511
Peter Zijlstrac536ed22020-04-01 16:54:26 +02001512 if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001513 insn->ret_offset = hint->sp_offset;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001514 continue;
1515 }
1516
1517 insn->hint = true;
1518
Julien Thierryedea9e62020-09-04 16:30:28 +01001519 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001520 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1521 insn->sec, insn->offset, hint->sp_reg);
1522 return -1;
1523 }
1524
1525 cfa->offset = hint->sp_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001526 insn->cfi.type = hint->type;
1527 insn->cfi.end = hint->end;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001528 }
1529
1530 return 0;
1531}
1532
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001533static int read_retpoline_hints(struct objtool_file *file)
1534{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001535 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001536 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001537 struct reloc *reloc;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001538
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001539 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001540 if (!sec)
1541 return 0;
1542
Matt Helsleyf1974222020-05-29 14:01:13 -07001543 list_for_each_entry(reloc, &sec->reloc_list, list) {
1544 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001545 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001546 return -1;
1547 }
1548
Matt Helsleyf1974222020-05-29 14:01:13 -07001549 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001550 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001551 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001552 return -1;
1553 }
1554
1555 if (insn->type != INSN_JUMP_DYNAMIC &&
1556 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001557 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001558 insn->sec, insn->offset);
1559 return -1;
1560 }
1561
1562 insn->retpoline_safe = true;
1563 }
1564
1565 return 0;
1566}
1567
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001568static int read_instr_hints(struct objtool_file *file)
1569{
1570 struct section *sec;
1571 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001572 struct reloc *reloc;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001573
1574 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1575 if (!sec)
1576 return 0;
1577
Matt Helsleyf1974222020-05-29 14:01:13 -07001578 list_for_each_entry(reloc, &sec->reloc_list, list) {
1579 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001580 WARN("unexpected relocation symbol type in %s", sec->name);
1581 return -1;
1582 }
1583
Matt Helsleyf1974222020-05-29 14:01:13 -07001584 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001585 if (!insn) {
1586 WARN("bad .discard.instr_end entry");
1587 return -1;
1588 }
1589
1590 insn->instr--;
1591 }
1592
1593 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1594 if (!sec)
1595 return 0;
1596
Matt Helsleyf1974222020-05-29 14:01:13 -07001597 list_for_each_entry(reloc, &sec->reloc_list, list) {
1598 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001599 WARN("unexpected relocation symbol type in %s", sec->name);
1600 return -1;
1601 }
1602
Matt Helsleyf1974222020-05-29 14:01:13 -07001603 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001604 if (!insn) {
1605 WARN("bad .discard.instr_begin entry");
1606 return -1;
1607 }
1608
1609 insn->instr++;
1610 }
1611
1612 return 0;
1613}
1614
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001615static int read_intra_function_calls(struct objtool_file *file)
1616{
1617 struct instruction *insn;
1618 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -07001619 struct reloc *reloc;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001620
1621 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1622 if (!sec)
1623 return 0;
1624
Matt Helsleyf1974222020-05-29 14:01:13 -07001625 list_for_each_entry(reloc, &sec->reloc_list, list) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001626 unsigned long dest_off;
1627
Matt Helsleyf1974222020-05-29 14:01:13 -07001628 if (reloc->sym->type != STT_SECTION) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001629 WARN("unexpected relocation symbol type in %s",
1630 sec->name);
1631 return -1;
1632 }
1633
Matt Helsleyf1974222020-05-29 14:01:13 -07001634 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001635 if (!insn) {
1636 WARN("bad .discard.intra_function_call entry");
1637 return -1;
1638 }
1639
1640 if (insn->type != INSN_CALL) {
1641 WARN_FUNC("intra_function_call not a direct call",
1642 insn->sec, insn->offset);
1643 return -1;
1644 }
1645
1646 /*
1647 * Treat intra-function CALLs as JMPs, but with a stack_op.
1648 * See add_call_destinations(), which strips stack_ops from
1649 * normal CALLs.
1650 */
1651 insn->type = INSN_JUMP_UNCONDITIONAL;
1652
1653 dest_off = insn->offset + insn->len + insn->immediate;
1654 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1655 if (!insn->jump_dest) {
1656 WARN_FUNC("can't find call dest at %s+0x%lx",
1657 insn->sec, insn->offset,
1658 insn->sec->name, dest_off);
1659 return -1;
1660 }
1661 }
1662
1663 return 0;
1664}
1665
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02001666static int read_static_call_tramps(struct objtool_file *file)
1667{
1668 struct section *sec;
1669 struct symbol *func;
1670
1671 for_each_sec(file, sec) {
1672 list_for_each_entry(func, &sec->symbol_list, list) {
1673 if (func->bind == STB_GLOBAL &&
1674 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1675 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1676 func->static_call_tramp = true;
1677 }
1678 }
1679
1680 return 0;
1681}
1682
Allan Xavier4a60aa02018-09-07 08:12:01 -05001683static void mark_rodata(struct objtool_file *file)
1684{
1685 struct section *sec;
1686 bool found = false;
1687
1688 /*
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001689 * Search for the following rodata sections, each of which can
1690 * potentially contain jump tables:
1691 *
1692 * - .rodata: can contain GCC switch tables
1693 * - .rodata.<func>: same, if -fdata-sections is being used
1694 * - .rodata..c_jump_table: contains C annotated jump tables
1695 *
1696 * .rodata.str1.* sections are ignored; they don't contain jump tables.
Allan Xavier4a60aa02018-09-07 08:12:01 -05001697 */
1698 for_each_sec(file, sec) {
Muchun Song1ee444702020-04-12 22:44:05 +08001699 if (!strncmp(sec->name, ".rodata", 7) &&
1700 !strstr(sec->name, ".str1.")) {
Allan Xavier4a60aa02018-09-07 08:12:01 -05001701 sec->rodata = true;
1702 found = true;
1703 }
1704 }
1705
1706 file->rodata = found;
1707}
1708
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001709static int decode_sections(struct objtool_file *file)
1710{
1711 int ret;
1712
Allan Xavier4a60aa02018-09-07 08:12:01 -05001713 mark_rodata(file);
1714
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001715 ret = decode_instructions(file);
1716 if (ret)
1717 return ret;
1718
1719 ret = add_dead_ends(file);
1720 if (ret)
1721 return ret;
1722
1723 add_ignores(file);
Peter Zijlstraea242132019-02-25 12:50:09 +01001724 add_uaccess_safe(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001725
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001726 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001727 if (ret)
1728 return ret;
1729
Peter Zijlstra5b06fd32020-08-18 15:57:49 +02001730 ret = read_static_call_tramps(file);
1731 if (ret)
1732 return ret;
1733
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001734 ret = add_jump_destinations(file);
1735 if (ret)
1736 return ret;
1737
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001738 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001739 if (ret)
1740 return ret;
1741
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001742 ret = read_intra_function_calls(file);
1743 if (ret)
1744 return ret;
1745
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001746 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001747 if (ret)
1748 return ret;
1749
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001750 ret = add_jump_table_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001751 if (ret)
1752 return ret;
1753
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001754 ret = read_unwind_hints(file);
1755 if (ret)
1756 return ret;
1757
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001758 ret = read_retpoline_hints(file);
1759 if (ret)
1760 return ret;
1761
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001762 ret = read_instr_hints(file);
1763 if (ret)
1764 return ret;
1765
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001766 return 0;
1767}
1768
1769static bool is_fentry_call(struct instruction *insn)
1770{
Alexandre Chartre87cf61f2020-04-14 12:36:10 +02001771 if (insn->type == INSN_CALL && insn->call_dest &&
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001772 insn->call_dest->type == STT_NOTYPE &&
1773 !strcmp(insn->call_dest->name, "__fentry__"))
1774 return true;
1775
1776 return false;
1777}
1778
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001779static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001780{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001781 u8 ret_offset = insn->ret_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001782 struct cfi_state *cfi = &state->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001783 int i;
1784
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001785 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001786 return true;
1787
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001788 if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001789 return true;
1790
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001791 if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001792 return true;
1793
Alexandre Chartrec721b3f2020-04-07 09:31:35 +02001794 /*
1795 * If there is a ret offset hint then don't check registers
1796 * because a callee-saved register might have been pushed on
1797 * the stack.
1798 */
1799 if (ret_offset)
1800 return false;
1801
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001802 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001803 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1804 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001805 return true;
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001806 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001807
1808 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001809}
1810
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001811static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001812{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001813 struct cfi_state *cfi = &state->cfi;
1814
1815 if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
1816 cfi->regs[CFI_BP].offset == -16)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001817 return true;
1818
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001819 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001820 return true;
1821
1822 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001823}
1824
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001825static int update_cfi_state_regs(struct instruction *insn,
1826 struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001827 struct stack_op *op)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001828{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001829 struct cfi_reg *cfa = &cfi->cfa;
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001830
Josh Poimboeufd8dd25a2020-04-25 05:03:00 -05001831 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001832 return 0;
1833
1834 /* push */
Peter Zijlstraea242132019-02-25 12:50:09 +01001835 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001836 cfa->offset += 8;
1837
1838 /* pop */
Peter Zijlstraea242132019-02-25 12:50:09 +01001839 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001840 cfa->offset -= 8;
1841
1842 /* add immediate to sp */
1843 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1844 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1845 cfa->offset -= op->src.offset;
1846
1847 return 0;
1848}
1849
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001850static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001851{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001852 if (arch_callee_saved_reg(reg) &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001853 cfi->regs[reg].base == CFI_UNDEFINED) {
1854 cfi->regs[reg].base = base;
1855 cfi->regs[reg].offset = offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001856 }
1857}
1858
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001859static void restore_reg(struct cfi_state *cfi, unsigned char reg)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001860{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001861 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1862 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001863}
1864
1865/*
1866 * A note about DRAP stack alignment:
1867 *
1868 * GCC has the concept of a DRAP register, which is used to help keep track of
1869 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1870 * register. The typical DRAP pattern is:
1871 *
1872 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1873 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1874 * 41 ff 72 f8 pushq -0x8(%r10)
1875 * 55 push %rbp
1876 * 48 89 e5 mov %rsp,%rbp
1877 * (more pushes)
1878 * 41 52 push %r10
1879 * ...
1880 * 41 5a pop %r10
1881 * (more pops)
1882 * 5d pop %rbp
1883 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1884 * c3 retq
1885 *
1886 * There are some variations in the epilogues, like:
1887 *
1888 * 5b pop %rbx
1889 * 41 5a pop %r10
1890 * 41 5c pop %r12
1891 * 41 5d pop %r13
1892 * 41 5e pop %r14
1893 * c9 leaveq
1894 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1895 * c3 retq
1896 *
1897 * and:
1898 *
1899 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1900 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1901 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1902 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1903 * c9 leaveq
1904 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1905 * c3 retq
1906 *
1907 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1908 * restored beforehand:
1909 *
1910 * 41 55 push %r13
1911 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1912 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1913 * ...
1914 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1915 * 41 5d pop %r13
1916 * c3 retq
1917 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001918static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001919 struct stack_op *op)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001920{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001921 struct cfi_reg *cfa = &cfi->cfa;
1922 struct cfi_reg *regs = cfi->regs;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001923
1924 /* stack operations don't make sense with an undefined CFA */
1925 if (cfa->base == CFI_UNDEFINED) {
1926 if (insn->func) {
1927 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1928 return -1;
1929 }
1930 return 0;
1931 }
1932
Julien Thierryee819ae2020-09-04 16:30:27 +01001933 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
1934 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001935 return update_cfi_state_regs(insn, cfi, op);
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001936
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001937 switch (op->dest.type) {
1938
1939 case OP_DEST_REG:
1940 switch (op->src.type) {
1941
1942 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001943 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1944 cfa->base == CFI_SP &&
1945 regs[CFI_BP].base == CFI_CFA &&
1946 regs[CFI_BP].offset == -cfa->offset) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001947
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001948 /* mov %rsp, %rbp */
1949 cfa->base = op->dest.reg;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001950 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001951 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001952
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001953 else if (op->src.reg == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001954 op->dest.reg == CFI_BP && cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05001955
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001956 /* drap: mov %rsp, %rbp */
1957 regs[CFI_BP].base = CFI_BP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001958 regs[CFI_BP].offset = -cfi->stack_size;
1959 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001960 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001961
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001962 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1963
1964 /*
1965 * mov %rsp, %reg
1966 *
1967 * This is needed for the rare case where GCC
1968 * does:
1969 *
1970 * mov %rsp, %rax
1971 * ...
1972 * mov %rax, %rsp
1973 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001974 cfi->vals[op->dest.reg].base = CFI_CFA;
1975 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001976 }
1977
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001978 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1979 cfa->base == CFI_BP) {
1980
1981 /*
1982 * mov %rbp, %rsp
1983 *
1984 * Restore the original stack pointer (Clang).
1985 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001986 cfi->stack_size = -cfi->regs[CFI_BP].offset;
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001987 }
1988
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001989 else if (op->dest.reg == cfa->base) {
1990
1991 /* mov %reg, %rsp */
1992 if (cfa->base == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001993 cfi->vals[op->src.reg].base == CFI_CFA) {
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001994
1995 /*
1996 * This is needed for the rare case
1997 * where GCC does something dumb like:
1998 *
1999 * lea 0x8(%rsp), %rcx
2000 * ...
2001 * mov %rcx, %rsp
2002 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002003 cfa->offset = -cfi->vals[op->src.reg].offset;
2004 cfi->stack_size = cfa->offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002005
2006 } else {
2007 cfa->base = CFI_UNDEFINED;
2008 cfa->offset = 0;
2009 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002010 }
2011
2012 break;
2013
2014 case OP_SRC_ADD:
2015 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2016
2017 /* add imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002018 cfi->stack_size -= op->src.offset;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002019 if (cfa->base == CFI_SP)
2020 cfa->offset -= op->src.offset;
2021 break;
2022 }
2023
2024 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2025
2026 /* lea disp(%rbp), %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002027 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002028 break;
2029 }
2030
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002031 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002032
2033 /* drap: lea disp(%rsp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002034 cfi->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002035
2036 /*
2037 * lea disp(%rsp), %reg
2038 *
2039 * This is needed for the rare case where GCC
2040 * does something dumb like:
2041 *
2042 * lea 0x8(%rsp), %rcx
2043 * ...
2044 * mov %rcx, %rsp
2045 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002046 cfi->vals[op->dest.reg].base = CFI_CFA;
2047 cfi->vals[op->dest.reg].offset = \
2048 -cfi->stack_size + op->src.offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05002049
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002050 break;
2051 }
2052
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002053 if (cfi->drap && op->dest.reg == CFI_SP &&
2054 op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002055
2056 /* drap: lea disp(%drap), %rsp */
2057 cfa->base = CFI_SP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002058 cfa->offset = cfi->stack_size = -op->src.offset;
2059 cfi->drap_reg = CFI_UNDEFINED;
2060 cfi->drap = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002061 break;
2062 }
2063
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002064 if (op->dest.reg == cfi->cfa.base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002065 WARN_FUNC("unsupported stack register modification",
2066 insn->sec, insn->offset);
2067 return -1;
2068 }
2069
2070 break;
2071
2072 case OP_SRC_AND:
2073 if (op->dest.reg != CFI_SP ||
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002074 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2075 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002076 WARN_FUNC("unsupported stack pointer realignment",
2077 insn->sec, insn->offset);
2078 return -1;
2079 }
2080
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002081 if (cfi->drap_reg != CFI_UNDEFINED) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002082 /* drap: and imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002083 cfa->base = cfi->drap_reg;
2084 cfa->offset = cfi->stack_size = 0;
2085 cfi->drap = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002086 }
2087
2088 /*
2089 * Older versions of GCC (4.8ish) realign the stack
2090 * without DRAP, with a frame pointer.
2091 */
2092
2093 break;
2094
2095 case OP_SRC_POP:
Peter Zijlstraea242132019-02-25 12:50:09 +01002096 case OP_SRC_POPF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002097 if (!cfi->drap && op->dest.reg == cfa->base) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002098
2099 /* pop %rbp */
2100 cfa->base = CFI_SP;
2101 }
2102
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002103 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2104 op->dest.reg == cfi->drap_reg &&
2105 cfi->drap_offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002106
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002107 /* drap: pop %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002108 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002109 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002110 cfi->drap_offset = -1;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002111
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002112 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002113
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002114 /* pop %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002115 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002116 }
2117
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002118 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002119 if (cfa->base == CFI_SP)
2120 cfa->offset -= 8;
2121
2122 break;
2123
2124 case OP_SRC_REG_INDIRECT:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002125 if (cfi->drap && op->src.reg == CFI_BP &&
2126 op->src.offset == cfi->drap_offset) {
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002127
2128 /* drap: mov disp(%rbp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002129 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002130 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002131 cfi->drap_offset = -1;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002132 }
2133
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002134 if (cfi->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002135 op->src.offset == regs[op->dest.reg].offset) {
2136
2137 /* drap: mov disp(%rbp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002138 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002139
2140 } else if (op->src.reg == cfa->base &&
2141 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2142
2143 /* mov disp(%rbp), %reg */
2144 /* mov disp(%rsp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002145 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002146 }
2147
2148 break;
2149
2150 default:
2151 WARN_FUNC("unknown stack-related instruction",
2152 insn->sec, insn->offset);
2153 return -1;
2154 }
2155
2156 break;
2157
2158 case OP_DEST_PUSH:
Peter Zijlstraea242132019-02-25 12:50:09 +01002159 case OP_DEST_PUSHF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002160 cfi->stack_size += 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002161 if (cfa->base == CFI_SP)
2162 cfa->offset += 8;
2163
2164 if (op->src.type != OP_SRC_REG)
2165 break;
2166
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002167 if (cfi->drap) {
2168 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002169
2170 /* drap: push %drap */
2171 cfa->base = CFI_BP_INDIRECT;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002172 cfa->offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002173
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002174 /* save drap so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002175 cfi->drap_offset = -cfi->stack_size;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002176
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002177 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002178
2179 /* drap: push %rbp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002180 cfi->stack_size = 0;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002181
Julien Thierryf4f80392020-09-15 08:53:16 +01002182 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002183
2184 /* drap: push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002185 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002186 }
2187
2188 } else {
2189
2190 /* push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002191 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002192 }
2193
2194 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002195 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002196 cfa->base != CFI_BP)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002197 cfi->bp_scratch = true;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002198 break;
2199
2200 case OP_DEST_REG_INDIRECT:
2201
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002202 if (cfi->drap) {
2203 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002204
2205 /* drap: mov %drap, disp(%rbp) */
2206 cfa->base = CFI_BP_INDIRECT;
2207 cfa->offset = op->dest.offset;
2208
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002209 /* save drap offset so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002210 cfi->drap_offset = op->dest.offset;
Julien Thierryf4f80392020-09-15 08:53:16 +01002211 } else {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002212
2213 /* drap: mov reg, disp(%rbp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002214 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002215 }
2216
2217 } else if (op->dest.reg == cfa->base) {
2218
2219 /* mov reg, disp(%rbp) */
2220 /* mov reg, disp(%rsp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002221 save_reg(cfi, op->src.reg, CFI_CFA,
2222 op->dest.offset - cfi->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002223 }
2224
2225 break;
2226
2227 case OP_DEST_LEAVE:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002228 if ((!cfi->drap && cfa->base != CFI_BP) ||
2229 (cfi->drap && cfa->base != cfi->drap_reg)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002230 WARN_FUNC("leave instruction with modified stack frame",
2231 insn->sec, insn->offset);
2232 return -1;
2233 }
2234
2235 /* leave (mov %rbp, %rsp; pop %rbp) */
2236
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002237 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2238 restore_reg(cfi, CFI_BP);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002239
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002240 if (!cfi->drap) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002241 cfa->base = CFI_SP;
2242 cfa->offset -= 8;
2243 }
2244
2245 break;
2246
2247 case OP_DEST_MEM:
Peter Zijlstraea242132019-02-25 12:50:09 +01002248 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002249 WARN_FUNC("unknown stack-related memory operation",
2250 insn->sec, insn->offset);
2251 return -1;
2252 }
2253
2254 /* pop mem */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002255 cfi->stack_size -= 8;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002256 if (cfa->base == CFI_SP)
2257 cfa->offset -= 8;
2258
2259 break;
2260
2261 default:
2262 WARN_FUNC("unknown stack-related instruction",
2263 insn->sec, insn->offset);
2264 return -1;
2265 }
2266
2267 return 0;
2268}
2269
Julien Thierry65ea47d2020-03-27 15:28:47 +00002270static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2271{
2272 struct stack_op *op;
2273
2274 list_for_each_entry(op, &insn->stack_ops, list) {
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002275 struct cfi_state old_cfi = state->cfi;
Julien Thierry65ea47d2020-03-27 15:28:47 +00002276 int res;
2277
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002278 res = update_cfi_state(insn, &state->cfi, op);
Julien Thierry65ea47d2020-03-27 15:28:47 +00002279 if (res)
2280 return res;
2281
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002282 if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) {
2283 WARN_FUNC("alternative modifies stack", insn->sec, insn->offset);
2284 return -1;
2285 }
2286
Julien Thierry65ea47d2020-03-27 15:28:47 +00002287 if (op->dest.type == OP_DEST_PUSHF) {
2288 if (!state->uaccess_stack) {
2289 state->uaccess_stack = 1;
2290 } else if (state->uaccess_stack >> 31) {
2291 WARN_FUNC("PUSHF stack exhausted",
2292 insn->sec, insn->offset);
2293 return 1;
2294 }
2295 state->uaccess_stack <<= 1;
2296 state->uaccess_stack |= state->uaccess;
2297 }
2298
2299 if (op->src.type == OP_SRC_POPF) {
2300 if (state->uaccess_stack) {
2301 state->uaccess = state->uaccess_stack & 1;
2302 state->uaccess_stack >>= 1;
2303 if (state->uaccess_stack == 1)
2304 state->uaccess_stack = 0;
2305 }
2306 }
2307 }
2308
2309 return 0;
2310}
2311
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002312static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002313{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002314 struct cfi_state *cfi1 = &insn->cfi;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002315 int i;
2316
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002317 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2318
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002319 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2320 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002321 cfi1->cfa.base, cfi1->cfa.offset,
2322 cfi2->cfa.base, cfi2->cfa.offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002323
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002324 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002325 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002326 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002327 sizeof(struct cfi_reg)))
2328 continue;
2329
2330 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2331 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002332 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2333 i, cfi2->regs[i].base, cfi2->regs[i].offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002334 break;
2335 }
2336
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002337 } else if (cfi1->type != cfi2->type) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002338
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002339 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2340 insn->sec, insn->offset, cfi1->type, cfi2->type);
2341
2342 } else if (cfi1->drap != cfi2->drap ||
2343 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2344 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2345
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002346 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002347 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002348 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2349 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002350
2351 } else
2352 return true;
2353
2354 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002355}
2356
Peter Zijlstraea242132019-02-25 12:50:09 +01002357static inline bool func_uaccess_safe(struct symbol *func)
2358{
2359 if (func)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002360 return func->uaccess_safe;
Peter Zijlstraea242132019-02-25 12:50:09 +01002361
2362 return false;
2363}
2364
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002365static inline const char *call_dest_name(struct instruction *insn)
Peter Zijlstraea242132019-02-25 12:50:09 +01002366{
2367 if (insn->call_dest)
2368 return insn->call_dest->name;
2369
2370 return "{dynamic}";
2371}
2372
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002373static inline bool noinstr_call_dest(struct symbol *func)
2374{
2375 /*
2376 * We can't deal with indirect function calls at present;
2377 * assume they're instrumented.
2378 */
2379 if (!func)
2380 return false;
2381
2382 /*
2383 * If the symbol is from a noinstr section; we good.
2384 */
2385 if (func->sec->noinstr)
2386 return true;
2387
2388 /*
2389 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2390 * something 'BAD' happened. At the risk of taking the machine down,
2391 * let them proceed to get the message out.
2392 */
2393 if (!strncmp(func->name, "__ubsan_handle_", 15))
2394 return true;
2395
2396 return false;
2397}
2398
Peter Zijlstraea242132019-02-25 12:50:09 +01002399static int validate_call(struct instruction *insn, struct insn_state *state)
2400{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002401 if (state->noinstr && state->instr <= 0 &&
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002402 !noinstr_call_dest(insn->call_dest)) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002403 WARN_FUNC("call to %s() leaves .noinstr.text section",
2404 insn->sec, insn->offset, call_dest_name(insn));
2405 return 1;
2406 }
2407
Peter Zijlstraea242132019-02-25 12:50:09 +01002408 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2409 WARN_FUNC("call to %s() with UACCESS enabled",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002410 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstraea242132019-02-25 12:50:09 +01002411 return 1;
2412 }
2413
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002414 if (state->df) {
2415 WARN_FUNC("call to %s() with DF set",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002416 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002417 return 1;
2418 }
2419
Peter Zijlstraea242132019-02-25 12:50:09 +01002420 return 0;
2421}
2422
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002423static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2424{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002425 if (has_modified_stack_frame(insn, state)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002426 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2427 insn->sec, insn->offset);
2428 return 1;
2429 }
2430
Peter Zijlstraea242132019-02-25 12:50:09 +01002431 return validate_call(insn, state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002432}
2433
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002434static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2435{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002436 if (state->noinstr && state->instr > 0) {
2437 WARN_FUNC("return with instrumentation enabled",
2438 insn->sec, insn->offset);
2439 return 1;
2440 }
2441
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002442 if (state->uaccess && !func_uaccess_safe(func)) {
2443 WARN_FUNC("return with UACCESS enabled",
2444 insn->sec, insn->offset);
2445 return 1;
2446 }
2447
2448 if (!state->uaccess && func_uaccess_safe(func)) {
2449 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2450 insn->sec, insn->offset);
2451 return 1;
2452 }
2453
2454 if (state->df) {
2455 WARN_FUNC("return with DF set",
2456 insn->sec, insn->offset);
2457 return 1;
2458 }
2459
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002460 if (func && has_modified_stack_frame(insn, state)) {
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002461 WARN_FUNC("return with modified stack frame",
2462 insn->sec, insn->offset);
2463 return 1;
2464 }
2465
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002466 if (state->cfi.bp_scratch) {
Josh Poimboeufb2966952020-04-01 13:23:29 -05002467 WARN_FUNC("BP used as a scratch register",
2468 insn->sec, insn->offset);
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002469 return 1;
2470 }
2471
2472 return 0;
2473}
2474
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002475/*
Peter Zijlstra7117f162020-04-28 19:37:01 +02002476 * Alternatives should not contain any ORC entries, this in turn means they
2477 * should not contain any CFI ops, which implies all instructions should have
2478 * the same same CFI state.
2479 *
2480 * It is possible to constuct alternatives that have unreachable holes that go
2481 * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2482 * states which then results in ORC entries, which we just said we didn't want.
2483 *
2484 * Avoid them by copying the CFI entry of the first instruction into the whole
2485 * alternative.
2486 */
2487static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn)
2488{
2489 struct instruction *first_insn = insn;
2490 int alt_group = insn->alt_group;
2491
2492 sec_for_each_insn_continue(file, insn) {
2493 if (insn->alt_group != alt_group)
2494 break;
2495 insn->cfi = first_insn->cfi;
2496 }
2497}
2498
2499/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002500 * Follow the branch starting at the given instruction, and recursively follow
2501 * any other branches (jumps). Meanwhile, track the frame pointer state at
2502 * each instruction and validate all the rules described in
2503 * tools/objtool/Documentation/stack-validation.txt.
2504 */
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002505static int validate_branch(struct objtool_file *file, struct symbol *func,
Peter Zijlstrab7460462020-04-02 10:15:51 +02002506 struct instruction *insn, struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002507{
2508 struct alternative *alt;
Peter Zijlstrab7460462020-04-02 10:15:51 +02002509 struct instruction *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002510 struct section *sec;
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002511 u8 visited;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002512 int ret;
2513
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002514 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002515
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002516 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002517 next_insn = next_insn_same_sec(file, insn);
2518
Josh Poimboeuf13810432018-05-09 22:39:15 -05002519 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05002520 WARN("%s() falls through to next function %s()",
2521 func->name, insn->func->name);
2522 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002523 }
2524
Josh Poimboeuf48550222017-07-07 09:19:42 -05002525 if (func && insn->ignore) {
2526 WARN_FUNC("BUG: why am I validating an ignored function?",
2527 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05002528 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05002529 }
2530
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002531 visited = 1 << state.uaccess;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002532 if (insn->visited) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002533 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002534 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002535
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002536 if (insn->visited & visited)
Peter Zijlstraea242132019-02-25 12:50:09 +01002537 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002538 }
2539
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002540 if (state.noinstr)
2541 state.instr += insn->instr;
2542
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002543 if (insn->hint)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002544 state.cfi = insn->cfi;
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002545 else
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002546 insn->cfi = state.cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002547
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002548 insn->visited |= visited;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002549
Peter Zijlstra7117f162020-04-28 19:37:01 +02002550 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002551 bool skip_orig = false;
2552
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002553 list_for_each_entry(alt, &insn->alts, list) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002554 if (alt->skip_orig)
2555 skip_orig = true;
2556
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002557 ret = validate_branch(file, func, alt->insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002558 if (ret) {
2559 if (backtrace)
2560 BT_FUNC("(alt)", insn);
2561 return ret;
2562 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002563 }
Peter Zijlstra764eef42019-03-01 11:19:03 +01002564
Peter Zijlstra7117f162020-04-28 19:37:01 +02002565 if (insn->alt_group)
2566 fill_alternative_cfi(file, insn);
2567
Peter Zijlstra764eef42019-03-01 11:19:03 +01002568 if (skip_orig)
2569 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002570 }
2571
Peter Zijlstra60041bc2020-04-24 16:16:41 +02002572 if (handle_insn_ops(insn, &state))
2573 return 1;
2574
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002575 switch (insn->type) {
2576
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002577 case INSN_RETURN:
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002578 return validate_return(func, insn, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002579
2580 case INSN_CALL:
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002581 case INSN_CALL_DYNAMIC:
Peter Zijlstraea242132019-02-25 12:50:09 +01002582 ret = validate_call(insn, &state);
2583 if (ret)
2584 return ret;
2585
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002586 if (!no_fp && func && !is_fentry_call(insn) &&
2587 !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002588 WARN_FUNC("call without frame pointer save/setup",
2589 sec, insn->offset);
2590 return 1;
2591 }
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002592
2593 if (dead_end_function(file, insn->call_dest))
2594 return 0;
2595
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002596 if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) {
2597 list_add_tail(&insn->static_call_node,
2598 &file->static_call_list);
2599 }
2600
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002601 break;
2602
2603 case INSN_JUMP_CONDITIONAL:
2604 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002605 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002606 ret = validate_sibling_call(insn, &state);
2607 if (ret)
2608 return ret;
2609
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002610 } else if (insn->jump_dest) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002611 ret = validate_branch(file, func,
2612 insn->jump_dest, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002613 if (ret) {
2614 if (backtrace)
2615 BT_FUNC("(branch)", insn);
2616 return ret;
2617 }
Josh Poimboeuf48550222017-07-07 09:19:42 -05002618 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002619
2620 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2621 return 0;
2622
2623 break;
2624
2625 case INSN_JUMP_DYNAMIC:
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002626 case INSN_JUMP_DYNAMIC_CONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002627 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002628 ret = validate_sibling_call(insn, &state);
2629 if (ret)
2630 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002631 }
2632
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002633 if (insn->type == INSN_JUMP_DYNAMIC)
2634 return 0;
2635
2636 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002637
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002638 case INSN_CONTEXT_SWITCH:
2639 if (func && (!next_insn || !next_insn->hint)) {
2640 WARN_FUNC("unsupported instruction in callable function",
2641 sec, insn->offset);
2642 return 1;
2643 }
2644 return 0;
2645
Peter Zijlstraea242132019-02-25 12:50:09 +01002646 case INSN_STAC:
2647 if (state.uaccess) {
2648 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2649 return 1;
2650 }
2651
2652 state.uaccess = true;
2653 break;
2654
2655 case INSN_CLAC:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002656 if (!state.uaccess && func) {
Peter Zijlstraea242132019-02-25 12:50:09 +01002657 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2658 return 1;
2659 }
2660
2661 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2662 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2663 return 1;
2664 }
2665
2666 state.uaccess = false;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002667 break;
2668
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002669 case INSN_STD:
2670 if (state.df)
2671 WARN_FUNC("recursive STD", sec, insn->offset);
2672
2673 state.df = true;
2674 break;
2675
2676 case INSN_CLD:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002677 if (!state.df && func)
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002678 WARN_FUNC("redundant CLD", sec, insn->offset);
2679
2680 state.df = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002681 break;
2682
2683 default:
2684 break;
2685 }
2686
2687 if (insn->dead_end)
2688 return 0;
2689
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002690 if (!next_insn) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002691 if (state.cfi.cfa.base == CFI_UNDEFINED)
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002692 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002693 WARN("%s: unexpected end of section", sec->name);
2694 return 1;
2695 }
Josh Poimboeuf00d96182017-09-18 21:43:30 -05002696
2697 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002698 }
2699
2700 return 0;
2701}
2702
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002703static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002704{
2705 struct instruction *insn;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002706 struct insn_state state;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002707 int ret, warnings = 0;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002708
2709 if (!file->hints)
2710 return 0;
2711
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002712 init_insn_state(&state, sec);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002713
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002714 if (sec) {
2715 insn = find_insn(file, sec, 0);
2716 if (!insn)
2717 return 0;
2718 } else {
2719 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2720 }
2721
2722 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002723 if (insn->hint && !insn->visited) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002724 ret = validate_branch(file, insn->func, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002725 if (ret && backtrace)
2726 BT_FUNC("<=== (hint)", insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002727 warnings += ret;
2728 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002729
2730 insn = list_next_entry(insn, list);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002731 }
2732
2733 return warnings;
2734}
2735
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002736static int validate_retpoline(struct objtool_file *file)
2737{
2738 struct instruction *insn;
2739 int warnings = 0;
2740
2741 for_each_insn(file, insn) {
2742 if (insn->type != INSN_JUMP_DYNAMIC &&
2743 insn->type != INSN_CALL_DYNAMIC)
2744 continue;
2745
2746 if (insn->retpoline_safe)
2747 continue;
2748
Peter Zijlstraca41b972018-01-31 10:18:28 +01002749 /*
2750 * .init.text code is ran before userspace and thus doesn't
2751 * strictly need retpolines, except for modules which are
2752 * loaded late, they very much do need retpoline in their
2753 * .init.text
2754 */
2755 if (!strcmp(insn->sec->name, ".init.text") && !module)
2756 continue;
2757
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002758 WARN_FUNC("indirect %s found in RETPOLINE build",
2759 insn->sec, insn->offset,
2760 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2761
2762 warnings++;
2763 }
2764
2765 return warnings;
2766}
2767
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002768static bool is_kasan_insn(struct instruction *insn)
2769{
2770 return (insn->type == INSN_CALL &&
2771 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2772}
2773
2774static bool is_ubsan_insn(struct instruction *insn)
2775{
2776 return (insn->type == INSN_CALL &&
2777 !strcmp(insn->call_dest->name,
2778 "__ubsan_handle_builtin_unreachable"));
2779}
2780
Ilie Halip14db1f02020-09-19 09:41:18 +03002781static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002782{
2783 int i;
Ilie Halip14db1f02020-09-19 09:41:18 +03002784 struct instruction *prev_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002785
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002786 if (insn->ignore || insn->type == INSN_NOP)
2787 return true;
2788
2789 /*
2790 * Ignore any unused exceptions. This can happen when a whitelisted
2791 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002792 *
2793 * Also ignore alternative replacement instructions. This can happen
2794 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002795 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002796 if (!strcmp(insn->sec->name, ".fixup") ||
2797 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2798 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002799 return true;
2800
Julien Thierryfb136212020-09-15 08:53:17 +01002801 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->offset == FAKE_JUMP_OFFSET)
2802 return true;
2803
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002804 if (!insn->func)
2805 return false;
2806
2807 /*
2808 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2809 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2810 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2811 * (or occasionally a JMP to UD2).
Ilie Halip14db1f02020-09-19 09:41:18 +03002812 *
2813 * It may also insert a UD2 after calling a __noreturn function.
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002814 */
Ilie Halip14db1f02020-09-19 09:41:18 +03002815 prev_insn = list_prev_entry(insn, list);
2816 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002817 (insn->type == INSN_BUG ||
2818 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2819 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2820 return true;
2821
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002822 /*
2823 * Check if this (or a subsequent) instruction is related to
2824 * CONFIG_UBSAN or CONFIG_KASAN.
2825 *
2826 * End the search at 5 instructions to avoid going into the weeds.
2827 */
2828 for (i = 0; i < 5; i++) {
2829
2830 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2831 return true;
2832
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002833 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2834 if (insn->jump_dest &&
2835 insn->jump_dest->func == insn->func) {
2836 insn = insn->jump_dest;
2837 continue;
2838 }
2839
2840 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002841 }
2842
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002843 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002844 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002845
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002846 insn = list_next_entry(insn, list);
2847 }
2848
2849 return false;
2850}
2851
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002852static int validate_symbol(struct objtool_file *file, struct section *sec,
2853 struct symbol *sym, struct insn_state *state)
2854{
2855 struct instruction *insn;
2856 int ret;
2857
2858 if (!sym->len) {
2859 WARN("%s() is missing an ELF size annotation", sym->name);
2860 return 1;
2861 }
2862
2863 if (sym->pfunc != sym || sym->alias != sym)
2864 return 0;
2865
2866 insn = find_insn(file, sec, sym->offset);
2867 if (!insn || insn->ignore || insn->visited)
2868 return 0;
2869
2870 state->uaccess = sym->uaccess_safe;
2871
2872 ret = validate_branch(file, insn->func, insn, *state);
2873 if (ret && backtrace)
2874 BT_FUNC("<=== (sym)", insn);
2875 return ret;
2876}
2877
Peter Zijlstra350994b2020-03-23 20:57:13 +01002878static int validate_section(struct objtool_file *file, struct section *sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002879{
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002880 struct insn_state state;
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002881 struct symbol *func;
2882 int warnings = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002883
Peter Zijlstra350994b2020-03-23 20:57:13 +01002884 list_for_each_entry(func, &sec->symbol_list, list) {
2885 if (func->type != STT_FUNC)
2886 continue;
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002887
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002888 init_insn_state(&state, sec);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002889 state.cfi.cfa = initial_func_cfi.cfa;
2890 memcpy(&state.cfi.regs, &initial_func_cfi.regs,
Julien Thierry0699e552020-03-27 15:28:40 +00002891 CFI_NUM_REGS * sizeof(struct cfi_reg));
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002892 state.cfi.stack_size = initial_func_cfi.cfa.offset;
Julien Thierry0699e552020-03-27 15:28:40 +00002893
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002894 warnings += validate_symbol(file, sec, func, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002895 }
2896
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002897 return warnings;
2898}
2899
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002900static int validate_vmlinux_functions(struct objtool_file *file)
2901{
2902 struct section *sec;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002903 int warnings = 0;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002904
2905 sec = find_section_by_name(file->elf, ".noinstr.text");
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002906 if (sec) {
2907 warnings += validate_section(file, sec);
2908 warnings += validate_unwind_hints(file, sec);
2909 }
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002910
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002911 sec = find_section_by_name(file->elf, ".entry.text");
2912 if (sec) {
2913 warnings += validate_section(file, sec);
2914 warnings += validate_unwind_hints(file, sec);
2915 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002916
2917 return warnings;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002918}
2919
Peter Zijlstra350994b2020-03-23 20:57:13 +01002920static int validate_functions(struct objtool_file *file)
2921{
2922 struct section *sec;
2923 int warnings = 0;
2924
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002925 for_each_sec(file, sec) {
2926 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2927 continue;
2928
Peter Zijlstra350994b2020-03-23 20:57:13 +01002929 warnings += validate_section(file, sec);
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002930 }
Peter Zijlstra350994b2020-03-23 20:57:13 +01002931
2932 return warnings;
2933}
2934
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002935static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002936{
2937 struct instruction *insn;
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002938
2939 if (file->ignore_unreachables)
2940 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002941
2942 for_each_insn(file, insn) {
Ilie Halip14db1f02020-09-19 09:41:18 +03002943 if (insn->visited || ignore_unreachable_insn(file, insn))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002944 continue;
2945
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002946 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2947 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002948 }
2949
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002950 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002951}
2952
Julien Thierryd44becb2020-08-25 13:47:40 +01002953int check(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002954{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002955 int ret, warnings = 0;
2956
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002957 arch_initial_func_cfi_state(&initial_func_cfi);
2958
Julien Thierry6545eb02020-08-25 13:47:39 +01002959 ret = decode_sections(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002960 if (ret < 0)
2961 goto out;
2962 warnings += ret;
2963
Julien Thierry6545eb02020-08-25 13:47:39 +01002964 if (list_empty(&file->insn_list))
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002965 goto out;
2966
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002967 if (vmlinux && !validate_dup) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002968 ret = validate_vmlinux_functions(file);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002969 if (ret < 0)
2970 goto out;
2971
2972 warnings += ret;
2973 goto out;
2974 }
2975
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002976 if (retpoline) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002977 ret = validate_retpoline(file);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002978 if (ret < 0)
2979 return ret;
2980 warnings += ret;
2981 }
2982
Julien Thierry6545eb02020-08-25 13:47:39 +01002983 ret = validate_functions(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002984 if (ret < 0)
2985 goto out;
2986 warnings += ret;
2987
Julien Thierry6545eb02020-08-25 13:47:39 +01002988 ret = validate_unwind_hints(file, NULL);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002989 if (ret < 0)
2990 goto out;
2991 warnings += ret;
2992
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002993 if (!warnings) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002994 ret = validate_reachable_instructions(file);
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002995 if (ret < 0)
2996 goto out;
2997 warnings += ret;
2998 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002999
Julien Thierry6545eb02020-08-25 13:47:39 +01003000 ret = create_static_call_sections(file);
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02003001 if (ret < 0)
3002 goto out;
3003 warnings += ret;
3004
Peter Zijlstra99d00212020-08-06 15:14:09 -07003005 if (mcount) {
3006 ret = create_mcount_loc_sections(file);
3007 if (ret < 0)
3008 goto out;
3009 warnings += ret;
3010 }
3011
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003012out:
Josh Poimboeuf644592d2020-02-10 12:32:38 -06003013 if (ret < 0) {
3014 /*
3015 * Fatal error. The binary is corrupt or otherwise broken in
3016 * some way, or objtool itself is broken. Fail the kernel
3017 * build.
3018 */
3019 return ret;
3020 }
3021
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003022 return 0;
3023}