blob: fd2edab8e6728398b3f7db77e74ebee8d4e38c32 [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 Poimboeufbaa414692017-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 Poimboeufbaa414692017-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 Poimboeufbaa414692017-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 Poimboeufbaa414692017-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 Poimboeufbaa414692017-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 Poimboeufbaa414692017-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 Poimboeufbaa414692017-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 Poimboeufbaa414692017-06-28 10:11:07 -0500295 &insn->immediate,
Julien Thierry65ea47d2020-03-27 15:28:47 +0000296 &insn->stack_ops);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500297 if (ret)
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500298 goto err;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500299
Peter Zijlstra87ecb582020-03-16 15:47:27 +0100300 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500301 list_add_tail(&insn->list, &file->insn_list);
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100302 nr_insns++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500303 }
304
305 list_for_each_entry(func, &sec->symbol_list, list) {
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500306 if (func->type != STT_FUNC || func->alias != func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500307 continue;
308
309 if (!find_insn(file, sec, func->offset)) {
310 WARN("%s(): can't find starting instruction",
311 func->name);
312 return -1;
313 }
314
Peter Zijlstradbf4aeb2020-03-10 18:24:59 +0100315 sym_for_each_insn(file, func, insn)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500316 insn->func = func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500317 }
318 }
319
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100320 if (stats)
321 printf("nr_insns: %lu\n", nr_insns);
322
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500323 return 0;
Kamalesh Babulalb7037982017-10-19 11:27:24 -0500324
325err:
326 free(insn);
327 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500328}
329
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700330static struct instruction *find_last_insn(struct objtool_file *file,
331 struct section *sec)
332{
333 struct instruction *insn = NULL;
334 unsigned int offset;
335 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
336
337 for (offset = sec->len - 1; offset >= end && !insn; offset--)
338 insn = find_insn(file, sec, offset);
339
340 return insn;
341}
342
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500343/*
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500344 * Mark "ud2" instructions and manually annotated dead ends.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500345 */
346static int add_dead_ends(struct objtool_file *file)
347{
348 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700349 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500350 struct instruction *insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500351
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500352 /*
353 * By default, "ud2" is a dead end unless otherwise annotated, because
354 * GCC 7 inserts it for certain divide-by-zero cases.
355 */
356 for_each_insn(file, insn)
357 if (insn->type == INSN_BUG)
358 insn->dead_end = true;
359
360 /*
361 * Check for manually annotated dead ends.
362 */
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500363 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
364 if (!sec)
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500365 goto reachable;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500366
Matt Helsleyf1974222020-05-29 14:01:13 -0700367 list_for_each_entry(reloc, &sec->reloc_list, list) {
368 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500369 WARN("unexpected relocation symbol type in %s", sec->name);
370 return -1;
371 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700372 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500373 if (insn)
374 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700375 else if (reloc->addend == reloc->sym->sec->len) {
376 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700377 if (!insn) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500378 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700379 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500380 return -1;
381 }
382 } else {
383 WARN("can't find unreachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700384 reloc->sym->sec->name, reloc->addend);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500385 return -1;
386 }
387
388 insn->dead_end = true;
389 }
390
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500391reachable:
392 /*
393 * These manually annotated reachable checks are needed for GCC 4.4,
394 * where the Linux unreachable() macro isn't supported. In that case
395 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
396 * not a dead end.
397 */
398 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
399 if (!sec)
400 return 0;
401
Matt Helsleyf1974222020-05-29 14:01:13 -0700402 list_for_each_entry(reloc, &sec->reloc_list, list) {
403 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500404 WARN("unexpected relocation symbol type in %s", sec->name);
405 return -1;
406 }
Matt Helsleyf1974222020-05-29 14:01:13 -0700407 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500408 if (insn)
409 insn = list_prev_entry(insn, list);
Matt Helsleyf1974222020-05-29 14:01:13 -0700410 else if (reloc->addend == reloc->sym->sec->len) {
411 insn = find_last_insn(file, reloc->sym->sec);
Sami Tolvanen6b5dd712020-04-21 15:08:43 -0700412 if (!insn) {
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500413 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700414 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500415 return -1;
416 }
417 } else {
418 WARN("can't find reachable insn at %s+0x%x",
Matt Helsleyf1974222020-05-29 14:01:13 -0700419 reloc->sym->sec->name, reloc->addend);
Josh Poimboeuf649ea4d2017-07-27 15:56:53 -0500420 return -1;
421 }
422
423 insn->dead_end = false;
424 }
425
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500426 return 0;
427}
428
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200429static int create_static_call_sections(struct objtool_file *file)
430{
431 struct section *sec, *reloc_sec;
432 struct reloc *reloc;
433 struct static_call_site *site;
434 struct instruction *insn;
435 struct symbol *key_sym;
436 char *key_name, *tmp;
437 int idx;
438
439 sec = find_section_by_name(file->elf, ".static_call_sites");
440 if (sec) {
441 INIT_LIST_HEAD(&file->static_call_list);
442 WARN("file already has .static_call_sites section, skipping");
443 return 0;
444 }
445
446 if (list_empty(&file->static_call_list))
447 return 0;
448
449 idx = 0;
450 list_for_each_entry(insn, &file->static_call_list, static_call_node)
451 idx++;
452
453 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
454 sizeof(struct static_call_site), idx);
455 if (!sec)
456 return -1;
457
458 reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
459 if (!reloc_sec)
460 return -1;
461
462 idx = 0;
463 list_for_each_entry(insn, &file->static_call_list, static_call_node) {
464
465 site = (struct static_call_site *)sec->data->d_buf + idx;
466 memset(site, 0, sizeof(struct static_call_site));
467
468 /* populate reloc for 'addr' */
469 reloc = malloc(sizeof(*reloc));
470 if (!reloc) {
471 perror("malloc");
472 return -1;
473 }
474 memset(reloc, 0, sizeof(*reloc));
475 reloc->sym = insn->sec->sym;
476 reloc->addend = insn->offset;
477 reloc->type = R_X86_64_PC32;
478 reloc->offset = idx * sizeof(struct static_call_site);
479 reloc->sec = reloc_sec;
480 elf_add_reloc(file->elf, reloc);
481
482 /* find key symbol */
483 key_name = strdup(insn->call_dest->name);
484 if (!key_name) {
485 perror("strdup");
486 return -1;
487 }
488 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
489 STATIC_CALL_TRAMP_PREFIX_LEN)) {
490 WARN("static_call: trampoline name malformed: %s", key_name);
491 return -1;
492 }
493 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
494 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
495
496 key_sym = find_symbol_by_name(file->elf, tmp);
497 if (!key_sym) {
498 WARN("static_call: can't find static_call_key symbol: %s", tmp);
499 return -1;
500 }
501 free(key_name);
502
503 /* populate reloc for 'key' */
504 reloc = malloc(sizeof(*reloc));
505 if (!reloc) {
506 perror("malloc");
507 return -1;
508 }
509 memset(reloc, 0, sizeof(*reloc));
510 reloc->sym = key_sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200511 reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +0200512 reloc->type = R_X86_64_PC32;
513 reloc->offset = idx * sizeof(struct static_call_site) + 4;
514 reloc->sec = reloc_sec;
515 elf_add_reloc(file->elf, reloc);
516
517 idx++;
518 }
519
520 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
521 return -1;
522
523 return 0;
524}
525
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500526/*
527 * Warnings shouldn't be reported for ignored functions.
528 */
529static void add_ignores(struct objtool_file *file)
530{
531 struct instruction *insn;
532 struct section *sec;
533 struct symbol *func;
Matt Helsleyf1974222020-05-29 14:01:13 -0700534 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500535
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100536 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
537 if (!sec)
538 return;
539
Matt Helsleyf1974222020-05-29 14:01:13 -0700540 list_for_each_entry(reloc, &sec->reloc_list, list) {
541 switch (reloc->sym->type) {
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100542 case STT_FUNC:
Matt Helsleyf1974222020-05-29 14:01:13 -0700543 func = reloc->sym;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100544 break;
545
546 case STT_SECTION:
Matt Helsleyf1974222020-05-29 14:01:13 -0700547 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600548 if (!func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500549 continue;
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100550 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500551
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100552 default:
Matt Helsleyf1974222020-05-29 14:01:13 -0700553 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100554 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500555 }
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100556
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +0100557 func_for_each_insn(file, func, insn)
Peter Zijlstraaaf5c622019-02-27 14:04:13 +0100558 insn->ignore = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500559 }
560}
561
562/*
Peter Zijlstraea242132019-02-25 12:50:09 +0100563 * This is a whitelist of functions that is allowed to be called with AC set.
564 * The list is meant to be minimal and only contains compiler instrumentation
565 * ABI and a few functions used to implement *_{to,from}_user() functions.
566 *
567 * These functions must not directly change AC, but may PUSHF/POPF.
568 */
569static const char *uaccess_safe_builtin[] = {
570 /* KASAN */
571 "kasan_report",
572 "check_memory_region",
573 /* KASAN out-of-line */
574 "__asan_loadN_noabort",
575 "__asan_load1_noabort",
576 "__asan_load2_noabort",
577 "__asan_load4_noabort",
578 "__asan_load8_noabort",
579 "__asan_load16_noabort",
580 "__asan_storeN_noabort",
581 "__asan_store1_noabort",
582 "__asan_store2_noabort",
583 "__asan_store4_noabort",
584 "__asan_store8_noabort",
585 "__asan_store16_noabort",
586 /* KASAN in-line */
587 "__asan_report_load_n_noabort",
588 "__asan_report_load1_noabort",
589 "__asan_report_load2_noabort",
590 "__asan_report_load4_noabort",
591 "__asan_report_load8_noabort",
592 "__asan_report_load16_noabort",
593 "__asan_report_store_n_noabort",
594 "__asan_report_store1_noabort",
595 "__asan_report_store2_noabort",
596 "__asan_report_store4_noabort",
597 "__asan_report_store8_noabort",
598 "__asan_report_store16_noabort",
Marco Elver5f5c9712019-11-14 19:02:57 +0100599 /* KCSAN */
Marco Elver99676832020-03-25 17:41:57 +0100600 "__kcsan_check_access",
Marco Elver5f5c9712019-11-14 19:02:57 +0100601 "kcsan_found_watchpoint",
602 "kcsan_setup_watchpoint",
Marco Elver99676832020-03-25 17:41:57 +0100603 "kcsan_check_scoped_accesses",
Marco Elver50a19ad2020-04-24 17:47:30 +0200604 "kcsan_disable_current",
605 "kcsan_enable_current_nowarn",
Marco Elver5f5c9712019-11-14 19:02:57 +0100606 /* KCSAN/TSAN */
607 "__tsan_func_entry",
608 "__tsan_func_exit",
609 "__tsan_read_range",
610 "__tsan_write_range",
611 "__tsan_read1",
612 "__tsan_read2",
613 "__tsan_read4",
614 "__tsan_read8",
615 "__tsan_read16",
616 "__tsan_write1",
617 "__tsan_write2",
618 "__tsan_write4",
619 "__tsan_write8",
620 "__tsan_write16",
Peter Zijlstraea242132019-02-25 12:50:09 +0100621 /* KCOV */
622 "write_comp_data",
Josh Poimboeufae033f02020-04-29 14:09:04 -0500623 "check_kcov_mode",
Peter Zijlstraea242132019-02-25 12:50:09 +0100624 "__sanitizer_cov_trace_pc",
625 "__sanitizer_cov_trace_const_cmp1",
626 "__sanitizer_cov_trace_const_cmp2",
627 "__sanitizer_cov_trace_const_cmp4",
628 "__sanitizer_cov_trace_const_cmp8",
629 "__sanitizer_cov_trace_cmp1",
630 "__sanitizer_cov_trace_cmp2",
631 "__sanitizer_cov_trace_cmp4",
632 "__sanitizer_cov_trace_cmp8",
Al Viro36b1c702020-02-16 13:07:49 -0500633 "__sanitizer_cov_trace_switch",
Peter Zijlstraea242132019-02-25 12:50:09 +0100634 /* UBSAN */
635 "ubsan_type_mismatch_common",
636 "__ubsan_handle_type_mismatch",
637 "__ubsan_handle_type_mismatch_v1",
Peter Zijlstra9a50dca2019-10-21 15:11:49 +0200638 "__ubsan_handle_shift_out_of_bounds",
Peter Zijlstraea242132019-02-25 12:50:09 +0100639 /* misc */
640 "csum_partial_copy_generic",
641 "__memcpy_mcsafe",
Josh Poimboeufa7e47f22019-07-17 20:36:46 -0500642 "mcsafe_handle_tail",
Peter Zijlstraea242132019-02-25 12:50:09 +0100643 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
644 NULL
645};
646
647static void add_uaccess_safe(struct objtool_file *file)
648{
649 struct symbol *func;
650 const char **name;
651
652 if (!uaccess)
653 return;
654
655 for (name = uaccess_safe_builtin; *name; name++) {
656 func = find_symbol_by_name(file->elf, *name);
657 if (!func)
658 continue;
659
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500660 func->uaccess_safe = true;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500661 }
662}
663
664/*
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000665 * FIXME: For now, just ignore any alternatives which add retpolines. This is
666 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
667 * But it at least allows objtool to understand the control flow *around* the
668 * retpoline.
669 */
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100670static int add_ignore_alternatives(struct objtool_file *file)
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000671{
672 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -0700673 struct reloc *reloc;
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000674 struct instruction *insn;
675
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100676 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000677 if (!sec)
678 return 0;
679
Matt Helsleyf1974222020-05-29 14:01:13 -0700680 list_for_each_entry(reloc, &sec->reloc_list, list) {
681 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000682 WARN("unexpected relocation symbol type in %s", sec->name);
683 return -1;
684 }
685
Matt Helsleyf1974222020-05-29 14:01:13 -0700686 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000687 if (!insn) {
Peter Zijlstraff05ab22019-03-18 14:33:07 +0100688 WARN("bad .discard.ignore_alts entry");
Josh Poimboeuf258c7602018-01-11 21:46:24 +0000689 return -1;
690 }
691
692 insn->ignore_alts = true;
693 }
694
695 return 0;
696}
697
698/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500699 * Find the destination instructions for all jumps.
700 */
701static int add_jump_destinations(struct objtool_file *file)
702{
703 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -0700704 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500705 struct section *dest_sec;
706 unsigned long dest_off;
707
708 for_each_insn(file, insn) {
Josh Poimboeufa2296142020-02-10 12:32:39 -0600709 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500710 continue;
711
Josh Poimboeufe6da9562019-05-13 12:01:31 -0500712 if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500713 continue;
714
Matt Helsleyf1974222020-05-29 14:01:13 -0700715 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100716 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700717 if (!reloc) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500718 dest_sec = insn->sec;
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000719 dest_off = arch_jump_destination(insn);
Matt Helsleyf1974222020-05-29 14:01:13 -0700720 } else if (reloc->sym->type == STT_SECTION) {
721 dest_sec = reloc->sym->sec;
722 dest_off = arch_dest_reloc_offset(reloc->addend);
723 } else if (reloc->sym->sec->idx) {
724 dest_sec = reloc->sym->sec;
725 dest_off = reloc->sym->sym.st_value +
726 arch_dest_reloc_offset(reloc->addend);
727 } else if (strstr(reloc->sym->name, "_indirect_thunk_")) {
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000728 /*
729 * Retpoline jumps are really dynamic jumps in
730 * disguise, so convert them accordingly.
731 */
Josh Poimboeufb68b9902019-07-17 20:36:57 -0500732 if (insn->type == INSN_JUMP_UNCONDITIONAL)
733 insn->type = INSN_JUMP_DYNAMIC;
734 else
735 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
736
Peter Zijlstrab5bc2232018-01-16 10:24:06 +0100737 insn->retpoline_safe = true;
Josh Poimboeuf39b73532018-01-11 21:46:23 +0000738 continue;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500739 } else {
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500740 /* external sibling call */
Matt Helsleyf1974222020-05-29 14:01:13 -0700741 insn->call_dest = reloc->sym;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200742 if (insn->call_dest->static_call_tramp) {
743 list_add_tail(&insn->static_call_node,
744 &file->static_call_list);
745 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500746 continue;
747 }
748
749 insn->jump_dest = find_insn(file, dest_sec, dest_off);
750 if (!insn->jump_dest) {
751
752 /*
753 * This is a special case where an alt instruction
754 * jumps past the end of the section. These are
755 * handled later in handle_group_alt().
756 */
757 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
758 continue;
759
760 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
761 insn->sec, insn->offset, dest_sec->name,
762 dest_off);
763 return -1;
764 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500765
766 /*
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100767 * Cross-function jump.
Josh Poimboeufcd778492018-06-01 07:23:51 -0500768 */
769 if (insn->func && insn->jump_dest->func &&
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100770 insn->func != insn->jump_dest->func) {
771
772 /*
773 * For GCC 8+, create parent/child links for any cold
774 * subfunctions. This is _mostly_ redundant with a
775 * similar initialization in read_symbols().
776 *
777 * If a function has aliases, we want the *first* such
778 * function in the symbol table to be the subfunction's
779 * parent. In that case we overwrite the
780 * initialization done in read_symbols().
781 *
782 * However this code can't completely replace the
783 * read_symbols() code because this doesn't detect the
784 * case where the parent function's only reference to a
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500785 * subfunction is through a jump table.
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100786 */
787 if (!strstr(insn->func->name, ".cold.") &&
788 strstr(insn->jump_dest->func->name, ".cold.")) {
789 insn->func->cfunc = insn->jump_dest->func;
790 insn->jump_dest->func->pfunc = insn->func;
791
792 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
793 insn->jump_dest->offset == insn->jump_dest->func->offset) {
794
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -0500795 /* internal sibling call */
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100796 insn->call_dest = insn->jump_dest->func;
Peter Zijlstra5b06fd32020-08-18 15:57:49 +0200797 if (insn->call_dest->static_call_tramp) {
798 list_add_tail(&insn->static_call_node,
799 &file->static_call_list);
800 }
Peter Zijlstra54262aa2019-03-06 12:58:15 +0100801 }
Josh Poimboeufcd778492018-06-01 07:23:51 -0500802 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500803 }
804
805 return 0;
806}
807
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200808static void remove_insn_ops(struct instruction *insn)
809{
810 struct stack_op *op, *tmp;
811
812 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
813 list_del(&op->list);
814 free(op);
815 }
816}
817
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500818/*
819 * Find the destination instructions for all calls.
820 */
821static int add_call_destinations(struct objtool_file *file)
822{
823 struct instruction *insn;
824 unsigned long dest_off;
Matt Helsleyf1974222020-05-29 14:01:13 -0700825 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500826
827 for_each_insn(file, insn) {
828 if (insn->type != INSN_CALL)
829 continue;
830
Matt Helsleyf1974222020-05-29 14:01:13 -0700831 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100832 insn->offset, insn->len);
Matt Helsleyf1974222020-05-29 14:01:13 -0700833 if (!reloc) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000834 dest_off = arch_jump_destination(insn);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600835 insn->call_dest = find_func_by_offset(insn->sec, dest_off);
836 if (!insn->call_dest)
837 insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600838
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600839 if (insn->ignore)
840 continue;
841
842 if (!insn->call_dest) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200843 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500844 return -1;
845 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600846
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600847 if (insn->func && insn->call_dest->type != STT_FUNC) {
848 WARN_FUNC("unsupported call to non-function",
849 insn->sec, insn->offset);
850 return -1;
851 }
852
Matt Helsleyf1974222020-05-29 14:01:13 -0700853 } else if (reloc->sym->type == STT_SECTION) {
854 dest_off = arch_dest_reloc_offset(reloc->addend);
855 insn->call_dest = find_func_by_offset(reloc->sym->sec,
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000856 dest_off);
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600857 if (!insn->call_dest) {
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000858 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500859 insn->sec, insn->offset,
Matt Helsleyf1974222020-05-29 14:01:13 -0700860 reloc->sym->sec->name,
Raphael Gaultbfb08f22020-03-27 15:28:45 +0000861 dest_off);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500862 return -1;
863 }
864 } else
Matt Helsleyf1974222020-05-29 14:01:13 -0700865 insn->call_dest = reloc->sym;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200866
867 /*
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200868 * Many compilers cannot disable KCOV with a function attribute
869 * so they need a little help, NOP out any KCOV calls from noinstr
870 * text.
871 */
872 if (insn->sec->noinstr &&
873 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
Peter Zijlstrad832c002020-06-18 17:55:29 +0200874 if (reloc) {
875 reloc->type = R_NONE;
876 elf_write_reloc(file->elf, reloc);
Peter Zijlstra0f1441b2020-06-12 16:05:26 +0200877 }
878
879 elf_write_insn(file->elf, insn->sec,
880 insn->offset, insn->len,
881 arch_nop_insn(insn->len));
882 insn->type = INSN_NOP;
883 }
884
885 /*
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +0200886 * Whatever stack impact regular CALLs have, should be undone
887 * by the RETURN of the called function.
888 *
889 * Annotated intra-function calls retain the stack_ops but
890 * are converted to JUMP, see read_intra_function_calls().
891 */
892 remove_insn_ops(insn);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500893 }
894
895 return 0;
896}
897
898/*
899 * The .alternatives section requires some extra special care, over and above
900 * what other special sections require:
901 *
902 * 1. Because alternatives are patched in-place, we need to insert a fake jump
903 * instruction at the end so that validate_branch() skips all the original
904 * replaced instructions when validating the new instruction path.
905 *
906 * 2. An added wrinkle is that the new instruction length might be zero. In
907 * that case the old instructions are replaced with noops. We simulate that
908 * by creating a fake jump as the only new instruction.
909 *
910 * 3. In some cases, the alternative section includes an instruction which
911 * conditionally jumps to the _end_ of the entry. We have to modify these
912 * jumps' destinations to point back to .text rather than the end of the
913 * entry in .altinstr_replacement.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500914 */
915static int handle_group_alt(struct objtool_file *file,
916 struct special_alt *special_alt,
917 struct instruction *orig_insn,
918 struct instruction **new_insn)
919{
Alexandre Chartre13fab062020-04-14 12:36:11 +0200920 static unsigned int alt_group_next_index = 1;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600921 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
Alexandre Chartre13fab062020-04-14 12:36:11 +0200922 unsigned int alt_group = alt_group_next_index++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500923 unsigned long dest_off;
924
925 last_orig_insn = NULL;
926 insn = orig_insn;
927 sec_for_each_insn_from(file, insn) {
928 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
929 break;
930
Alexandre Chartre13fab062020-04-14 12:36:11 +0200931 insn->alt_group = alt_group;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500932 last_orig_insn = insn;
933 }
934
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600935 if (next_insn_same_sec(file, last_orig_insn)) {
936 fake_jump = malloc(sizeof(*fake_jump));
937 if (!fake_jump) {
938 WARN("malloc failed");
939 return -1;
940 }
941 memset(fake_jump, 0, sizeof(*fake_jump));
942 INIT_LIST_HEAD(&fake_jump->alts);
Julien Thierry65ea47d2020-03-27 15:28:47 +0000943 INIT_LIST_HEAD(&fake_jump->stack_ops);
Peter Zijlstrae7c02192020-03-25 14:04:45 +0100944 init_cfi_state(&fake_jump->cfi);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500945
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600946 fake_jump->sec = special_alt->new_sec;
Josh Poimboeufe6da9562019-05-13 12:01:31 -0500947 fake_jump->offset = FAKE_JUMP_OFFSET;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600948 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
949 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
Josh Poimboeufe6da9562019-05-13 12:01:31 -0500950 fake_jump->func = orig_insn->func;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500951 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500952
953 if (!special_alt->new_len) {
Josh Poimboeuf17bc3392018-01-29 22:00:40 -0600954 if (!fake_jump) {
955 WARN("%s: empty alternative at end of section",
956 special_alt->orig_sec->name);
957 return -1;
958 }
959
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500960 *new_insn = fake_jump;
961 return 0;
962 }
963
964 last_new_insn = NULL;
Alexandre Chartre13fab062020-04-14 12:36:11 +0200965 alt_group = alt_group_next_index++;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500966 insn = *new_insn;
967 sec_for_each_insn_from(file, insn) {
Julien Thierry45245f52020-09-04 16:30:23 +0100968 struct reloc *alt_reloc;
969
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500970 if (insn->offset >= special_alt->new_off + special_alt->new_len)
971 break;
972
973 last_new_insn = insn;
974
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600975 insn->ignore = orig_insn->ignore_alts;
Peter Zijlstraa4d09dd2019-02-25 10:31:24 +0100976 insn->func = orig_insn->func;
Alexandre Chartre13fab062020-04-14 12:36:11 +0200977 insn->alt_group = alt_group;
Josh Poimboeufa845c7c2018-01-29 22:00:39 -0600978
Josh Poimboeufdc419722020-02-10 12:32:40 -0600979 /*
980 * Since alternative replacement code is copy/pasted by the
981 * kernel after applying relocations, generally such code can't
982 * have relative-address relocation references to outside the
983 * .altinstr_replacement section, unless the arch's
984 * alternatives code can adjust the relative offsets
985 * accordingly.
Josh Poimboeufdc419722020-02-10 12:32:40 -0600986 */
Julien Thierry45245f52020-09-04 16:30:23 +0100987 alt_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
988 insn->offset, insn->len);
989 if (alt_reloc &&
990 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
Josh Poimboeufdc419722020-02-10 12:32:40 -0600991
992 WARN_FUNC("unsupported relocation in alternatives section",
993 insn->sec, insn->offset);
994 return -1;
995 }
996
Josh Poimboeufa2296142020-02-10 12:32:39 -0600997 if (!is_static_jump(insn))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -0500998 continue;
999
1000 if (!insn->immediate)
1001 continue;
1002
Raphael Gaultbfb08f22020-03-27 15:28:45 +00001003 dest_off = arch_jump_destination(insn);
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001004 if (dest_off == special_alt->new_off + special_alt->new_len) {
1005 if (!fake_jump) {
1006 WARN("%s: alternative jump to end of section",
1007 special_alt->orig_sec->name);
1008 return -1;
1009 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001010 insn->jump_dest = fake_jump;
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001011 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001012
1013 if (!insn->jump_dest) {
1014 WARN_FUNC("can't find alternative jump destination",
1015 insn->sec, insn->offset);
1016 return -1;
1017 }
1018 }
1019
1020 if (!last_new_insn) {
1021 WARN_FUNC("can't find last new alternative instruction",
1022 special_alt->new_sec, special_alt->new_off);
1023 return -1;
1024 }
1025
Josh Poimboeuf17bc3392018-01-29 22:00:40 -06001026 if (fake_jump)
1027 list_add(&fake_jump->list, &last_new_insn->list);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001028
1029 return 0;
1030}
1031
1032/*
1033 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1034 * If the original instruction is a jump, make the alt entry an effective nop
1035 * by just skipping the original instruction.
1036 */
1037static int handle_jump_alt(struct objtool_file *file,
1038 struct special_alt *special_alt,
1039 struct instruction *orig_insn,
1040 struct instruction **new_insn)
1041{
1042 if (orig_insn->type == INSN_NOP)
1043 return 0;
1044
1045 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1046 WARN_FUNC("unsupported instruction at jump label",
1047 orig_insn->sec, orig_insn->offset);
1048 return -1;
1049 }
1050
1051 *new_insn = list_next_entry(orig_insn, list);
1052 return 0;
1053}
1054
1055/*
1056 * Read all the special sections which have alternate instructions which can be
1057 * patched in or redirected to at runtime. Each instruction having alternate
1058 * instruction(s) has them added to its insn->alts list, which will be
1059 * traversed in validate_branch().
1060 */
1061static int add_special_section_alts(struct objtool_file *file)
1062{
1063 struct list_head special_alts;
1064 struct instruction *orig_insn, *new_insn;
1065 struct special_alt *special_alt, *tmp;
1066 struct alternative *alt;
1067 int ret;
1068
1069 ret = special_get_alts(file->elf, &special_alts);
1070 if (ret)
1071 return ret;
1072
1073 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001074
1075 orig_insn = find_insn(file, special_alt->orig_sec,
1076 special_alt->orig_off);
1077 if (!orig_insn) {
1078 WARN_FUNC("special: can't find orig instruction",
1079 special_alt->orig_sec, special_alt->orig_off);
1080 ret = -1;
1081 goto out;
1082 }
1083
1084 new_insn = NULL;
1085 if (!special_alt->group || special_alt->new_len) {
1086 new_insn = find_insn(file, special_alt->new_sec,
1087 special_alt->new_off);
1088 if (!new_insn) {
1089 WARN_FUNC("special: can't find new instruction",
1090 special_alt->new_sec,
1091 special_alt->new_off);
1092 ret = -1;
1093 goto out;
1094 }
1095 }
1096
1097 if (special_alt->group) {
Julien Thierry7170cf42020-03-27 15:28:41 +00001098 if (!special_alt->orig_len) {
1099 WARN_FUNC("empty alternative entry",
1100 orig_insn->sec, orig_insn->offset);
1101 continue;
1102 }
1103
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001104 ret = handle_group_alt(file, special_alt, orig_insn,
1105 &new_insn);
1106 if (ret)
1107 goto out;
1108 } else if (special_alt->jump_or_nop) {
1109 ret = handle_jump_alt(file, special_alt, orig_insn,
1110 &new_insn);
1111 if (ret)
1112 goto out;
1113 }
1114
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001115 alt = malloc(sizeof(*alt));
1116 if (!alt) {
1117 WARN("malloc failed");
1118 ret = -1;
1119 goto out;
1120 }
1121
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001122 alt->insn = new_insn;
Peter Zijlstra764eef42019-03-01 11:19:03 +01001123 alt->skip_orig = special_alt->skip_orig;
Peter Zijlstraea242132019-02-25 12:50:09 +01001124 orig_insn->ignore_alts |= special_alt->skip_alt;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001125 list_add_tail(&alt->list, &orig_insn->alts);
1126
1127 list_del(&special_alt->list);
1128 free(special_alt);
1129 }
1130
1131out:
1132 return ret;
1133}
1134
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001135static int add_jump_table(struct objtool_file *file, struct instruction *insn,
Matt Helsleyf1974222020-05-29 14:01:13 -07001136 struct reloc *table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001137{
Matt Helsleyf1974222020-05-29 14:01:13 -07001138 struct reloc *reloc = table;
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001139 struct instruction *dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001140 struct alternative *alt;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001141 struct symbol *pfunc = insn->func->pfunc;
1142 unsigned int prev_offset = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001143
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001144 /*
Matt Helsleyf1974222020-05-29 14:01:13 -07001145 * Each @reloc is a switch table relocation which points to the target
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001146 * instruction.
1147 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001148 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
Jann Hornbd98c812019-07-17 20:36:54 -05001149
1150 /* Check for the end of the table: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001151 if (reloc != table && reloc->jump_table_start)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001152 break;
1153
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001154 /* Make sure the table entries are consecutive: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001155 if (prev_offset && reloc->offset != prev_offset + 8)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001156 break;
1157
1158 /* Detect function pointers from contiguous objects: */
Matt Helsleyf1974222020-05-29 14:01:13 -07001159 if (reloc->sym->sec == pfunc->sec &&
1160 reloc->addend == pfunc->offset)
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001161 break;
1162
Matt Helsleyf1974222020-05-29 14:01:13 -07001163 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001164 if (!dest_insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001165 break;
1166
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001167 /* Make sure the destination is in the same function: */
Josh Poimboeufe65050b2019-07-17 20:36:55 -05001168 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
Josh Poimboeuf13810432018-05-09 22:39:15 -05001169 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001170
1171 alt = malloc(sizeof(*alt));
1172 if (!alt) {
1173 WARN("malloc failed");
1174 return -1;
1175 }
1176
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001177 alt->insn = dest_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001178 list_add_tail(&alt->list, &insn->alts);
Matt Helsleyf1974222020-05-29 14:01:13 -07001179 prev_offset = reloc->offset;
Josh Poimboeuffd35c882018-05-10 17:48:49 -05001180 }
1181
1182 if (!prev_offset) {
1183 WARN_FUNC("can't find switch jump table",
1184 insn->sec, insn->offset);
1185 return -1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001186 }
1187
1188 return 0;
1189}
1190
1191/*
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001192 * find_jump_table() - Given a dynamic jump, find the switch jump table
1193 * associated with it.
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001194 */
Matt Helsleyf1974222020-05-29 14:01:13 -07001195static struct reloc *find_jump_table(struct objtool_file *file,
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001196 struct symbol *func,
1197 struct instruction *insn)
1198{
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001199 struct reloc *table_reloc;
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001200 struct instruction *dest_insn, *orig_insn = insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001201
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001202 /*
1203 * Backward search using the @first_jump_src links, these help avoid
1204 * much of the 'in between' code. Which avoids us getting confused by
1205 * it.
1206 */
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001207 for (;
Josh Poimboeuf1119d262020-04-28 16:45:16 -05001208 insn && insn->func && insn->func->pfunc == func;
1209 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001210
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001211 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001212 break;
1213
1214 /* allow small jumps within the range */
1215 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1216 insn->jump_dest &&
1217 (insn->jump_dest->offset <= insn->offset ||
1218 insn->jump_dest->offset > orig_insn->offset))
1219 break;
1220
Raphael Gaultd871f7b2020-09-04 16:30:24 +01001221 table_reloc = arch_find_switch_table(file, insn);
Matt Helsleyf1974222020-05-29 14:01:13 -07001222 if (!table_reloc)
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001223 continue;
Matt Helsleyf1974222020-05-29 14:01:13 -07001224 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
Josh Poimboeuf113d4bc2020-02-17 21:41:53 -06001225 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1226 continue;
Josh Poimboeuf7dec80c2018-05-18 15:10:34 -05001227
Matt Helsleyf1974222020-05-29 14:01:13 -07001228 return table_reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001229 }
1230
1231 return NULL;
1232}
1233
Jann Hornbd98c812019-07-17 20:36:54 -05001234/*
1235 * First pass: Mark the head of each jump table so that in the next pass,
1236 * we know when a given jump table ends and the next one starts.
1237 */
1238static void mark_func_jump_tables(struct objtool_file *file,
1239 struct symbol *func)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001240{
Jann Hornbd98c812019-07-17 20:36:54 -05001241 struct instruction *insn, *last = NULL;
Matt Helsleyf1974222020-05-29 14:01:13 -07001242 struct reloc *reloc;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001243
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001244 func_for_each_insn(file, func, insn) {
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001245 if (!last)
1246 last = insn;
1247
1248 /*
1249 * Store back-pointers for unconditional forward jumps such
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001250 * that find_jump_table() can back-track using those and
Peter Zijlstra99ce7962018-02-08 14:02:32 +01001251 * avoid some potentially confusing code.
1252 */
1253 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1254 insn->offset > last->offset &&
1255 insn->jump_dest->offset > insn->offset &&
1256 !insn->jump_dest->first_jump_src) {
1257
1258 insn->jump_dest->first_jump_src = insn;
1259 last = insn->jump_dest;
1260 }
1261
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001262 if (insn->type != INSN_JUMP_DYNAMIC)
1263 continue;
1264
Matt Helsleyf1974222020-05-29 14:01:13 -07001265 reloc = find_jump_table(file, func, insn);
1266 if (reloc) {
1267 reloc->jump_table_start = true;
1268 insn->jump_table = reloc;
Jann Hornbd98c812019-07-17 20:36:54 -05001269 }
1270 }
1271}
1272
1273static int add_func_jump_tables(struct objtool_file *file,
1274 struct symbol *func)
1275{
1276 struct instruction *insn;
1277 int ret;
1278
Peter Zijlstraf0f70ad2020-03-10 18:27:24 +01001279 func_for_each_insn(file, func, insn) {
Jann Hornbd98c812019-07-17 20:36:54 -05001280 if (!insn->jump_table)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001281 continue;
1282
Jann Hornbd98c812019-07-17 20:36:54 -05001283 ret = add_jump_table(file, insn, insn->jump_table);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001284 if (ret)
1285 return ret;
1286 }
1287
1288 return 0;
1289}
1290
1291/*
1292 * For some switch statements, gcc generates a jump table in the .rodata
1293 * section which contains a list of addresses within the function to jump to.
1294 * This finds these jump tables and adds them to the insn->alts lists.
1295 */
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001296static int add_jump_table_alts(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001297{
1298 struct section *sec;
1299 struct symbol *func;
1300 int ret;
1301
Allan Xavier4a60aa02018-09-07 08:12:01 -05001302 if (!file->rodata)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001303 return 0;
1304
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001305 for_each_sec(file, sec) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001306 list_for_each_entry(func, &sec->symbol_list, list) {
1307 if (func->type != STT_FUNC)
1308 continue;
1309
Jann Hornbd98c812019-07-17 20:36:54 -05001310 mark_func_jump_tables(file, func);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001311 ret = add_func_jump_tables(file, func);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001312 if (ret)
1313 return ret;
1314 }
1315 }
1316
1317 return 0;
1318}
1319
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001320static int read_unwind_hints(struct objtool_file *file)
1321{
Matt Helsleyf1974222020-05-29 14:01:13 -07001322 struct section *sec, *relocsec;
1323 struct reloc *reloc;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001324 struct unwind_hint *hint;
1325 struct instruction *insn;
1326 struct cfi_reg *cfa;
1327 int i;
1328
1329 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1330 if (!sec)
1331 return 0;
1332
Matt Helsleyf1974222020-05-29 14:01:13 -07001333 relocsec = sec->reloc;
1334 if (!relocsec) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001335 WARN("missing .rela.discard.unwind_hints section");
1336 return -1;
1337 }
1338
1339 if (sec->len % sizeof(struct unwind_hint)) {
1340 WARN("struct unwind_hint size mismatch");
1341 return -1;
1342 }
1343
1344 file->hints = true;
1345
1346 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1347 hint = (struct unwind_hint *)sec->data->d_buf + i;
1348
Matt Helsleyf1974222020-05-29 14:01:13 -07001349 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1350 if (!reloc) {
1351 WARN("can't find reloc for unwind_hints[%d]", i);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001352 return -1;
1353 }
1354
Matt Helsleyf1974222020-05-29 14:01:13 -07001355 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001356 if (!insn) {
1357 WARN("can't find insn for unwind_hints[%d]", i);
1358 return -1;
1359 }
1360
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001361 cfa = &insn->cfi.cfa;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001362
Peter Zijlstrac536ed22020-04-01 16:54:26 +02001363 if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001364 insn->ret_offset = hint->sp_offset;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001365 continue;
1366 }
1367
1368 insn->hint = true;
1369
Julien Thierryedea9e62020-09-04 16:30:28 +01001370 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001371 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1372 insn->sec, insn->offset, hint->sp_reg);
1373 return -1;
1374 }
1375
1376 cfa->offset = hint->sp_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001377 insn->cfi.type = hint->type;
1378 insn->cfi.end = hint->end;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001379 }
1380
1381 return 0;
1382}
1383
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001384static int read_retpoline_hints(struct objtool_file *file)
1385{
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001386 struct section *sec;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001387 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001388 struct reloc *reloc;
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001389
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001390 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001391 if (!sec)
1392 return 0;
1393
Matt Helsleyf1974222020-05-29 14:01:13 -07001394 list_for_each_entry(reloc, &sec->reloc_list, list) {
1395 if (reloc->sym->type != STT_SECTION) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001396 WARN("unexpected relocation symbol type in %s", sec->name);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001397 return -1;
1398 }
1399
Matt Helsleyf1974222020-05-29 14:01:13 -07001400 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001401 if (!insn) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001402 WARN("bad .discard.retpoline_safe entry");
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001403 return -1;
1404 }
1405
1406 if (insn->type != INSN_JUMP_DYNAMIC &&
1407 insn->type != INSN_CALL_DYNAMIC) {
Josh Poimboeuf63474dc2018-03-06 17:58:15 -06001408 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001409 insn->sec, insn->offset);
1410 return -1;
1411 }
1412
1413 insn->retpoline_safe = true;
1414 }
1415
1416 return 0;
1417}
1418
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001419static int read_instr_hints(struct objtool_file *file)
1420{
1421 struct section *sec;
1422 struct instruction *insn;
Matt Helsleyf1974222020-05-29 14:01:13 -07001423 struct reloc *reloc;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001424
1425 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1426 if (!sec)
1427 return 0;
1428
Matt Helsleyf1974222020-05-29 14:01:13 -07001429 list_for_each_entry(reloc, &sec->reloc_list, list) {
1430 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001431 WARN("unexpected relocation symbol type in %s", sec->name);
1432 return -1;
1433 }
1434
Matt Helsleyf1974222020-05-29 14:01:13 -07001435 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001436 if (!insn) {
1437 WARN("bad .discard.instr_end entry");
1438 return -1;
1439 }
1440
1441 insn->instr--;
1442 }
1443
1444 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1445 if (!sec)
1446 return 0;
1447
Matt Helsleyf1974222020-05-29 14:01:13 -07001448 list_for_each_entry(reloc, &sec->reloc_list, list) {
1449 if (reloc->sym->type != STT_SECTION) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001450 WARN("unexpected relocation symbol type in %s", sec->name);
1451 return -1;
1452 }
1453
Matt Helsleyf1974222020-05-29 14:01:13 -07001454 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001455 if (!insn) {
1456 WARN("bad .discard.instr_begin entry");
1457 return -1;
1458 }
1459
1460 insn->instr++;
1461 }
1462
1463 return 0;
1464}
1465
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001466static int read_intra_function_calls(struct objtool_file *file)
1467{
1468 struct instruction *insn;
1469 struct section *sec;
Matt Helsleyf1974222020-05-29 14:01:13 -07001470 struct reloc *reloc;
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001471
1472 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1473 if (!sec)
1474 return 0;
1475
Matt Helsleyf1974222020-05-29 14:01:13 -07001476 list_for_each_entry(reloc, &sec->reloc_list, list) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001477 unsigned long dest_off;
1478
Matt Helsleyf1974222020-05-29 14:01:13 -07001479 if (reloc->sym->type != STT_SECTION) {
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001480 WARN("unexpected relocation symbol type in %s",
1481 sec->name);
1482 return -1;
1483 }
1484
Matt Helsleyf1974222020-05-29 14:01:13 -07001485 insn = find_insn(file, reloc->sym->sec, reloc->addend);
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001486 if (!insn) {
1487 WARN("bad .discard.intra_function_call entry");
1488 return -1;
1489 }
1490
1491 if (insn->type != INSN_CALL) {
1492 WARN_FUNC("intra_function_call not a direct call",
1493 insn->sec, insn->offset);
1494 return -1;
1495 }
1496
1497 /*
1498 * Treat intra-function CALLs as JMPs, but with a stack_op.
1499 * See add_call_destinations(), which strips stack_ops from
1500 * normal CALLs.
1501 */
1502 insn->type = INSN_JUMP_UNCONDITIONAL;
1503
1504 dest_off = insn->offset + insn->len + insn->immediate;
1505 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1506 if (!insn->jump_dest) {
1507 WARN_FUNC("can't find call dest at %s+0x%lx",
1508 insn->sec, insn->offset,
1509 insn->sec->name, dest_off);
1510 return -1;
1511 }
1512 }
1513
1514 return 0;
1515}
1516
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02001517static int read_static_call_tramps(struct objtool_file *file)
1518{
1519 struct section *sec;
1520 struct symbol *func;
1521
1522 for_each_sec(file, sec) {
1523 list_for_each_entry(func, &sec->symbol_list, list) {
1524 if (func->bind == STB_GLOBAL &&
1525 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1526 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1527 func->static_call_tramp = true;
1528 }
1529 }
1530
1531 return 0;
1532}
1533
Allan Xavier4a60aa02018-09-07 08:12:01 -05001534static void mark_rodata(struct objtool_file *file)
1535{
1536 struct section *sec;
1537 bool found = false;
1538
1539 /*
Josh Poimboeuf87b512d2019-06-27 20:50:46 -05001540 * Search for the following rodata sections, each of which can
1541 * potentially contain jump tables:
1542 *
1543 * - .rodata: can contain GCC switch tables
1544 * - .rodata.<func>: same, if -fdata-sections is being used
1545 * - .rodata..c_jump_table: contains C annotated jump tables
1546 *
1547 * .rodata.str1.* sections are ignored; they don't contain jump tables.
Allan Xavier4a60aa02018-09-07 08:12:01 -05001548 */
1549 for_each_sec(file, sec) {
Muchun Song1ee444702020-04-12 22:44:05 +08001550 if (!strncmp(sec->name, ".rodata", 7) &&
1551 !strstr(sec->name, ".str1.")) {
Allan Xavier4a60aa02018-09-07 08:12:01 -05001552 sec->rodata = true;
1553 found = true;
1554 }
1555 }
1556
1557 file->rodata = found;
1558}
1559
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001560static int decode_sections(struct objtool_file *file)
1561{
1562 int ret;
1563
Allan Xavier4a60aa02018-09-07 08:12:01 -05001564 mark_rodata(file);
1565
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001566 ret = decode_instructions(file);
1567 if (ret)
1568 return ret;
1569
1570 ret = add_dead_ends(file);
1571 if (ret)
1572 return ret;
1573
1574 add_ignores(file);
Peter Zijlstraea242132019-02-25 12:50:09 +01001575 add_uaccess_safe(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001576
Peter Zijlstraff05ab22019-03-18 14:33:07 +01001577 ret = add_ignore_alternatives(file);
Josh Poimboeuf258c7602018-01-11 21:46:24 +00001578 if (ret)
1579 return ret;
1580
Peter Zijlstra5b06fd32020-08-18 15:57:49 +02001581 ret = read_static_call_tramps(file);
1582 if (ret)
1583 return ret;
1584
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001585 ret = add_jump_destinations(file);
1586 if (ret)
1587 return ret;
1588
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001589 ret = add_special_section_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001590 if (ret)
1591 return ret;
1592
Alexandre Chartre8aa8eb22020-04-14 12:36:12 +02001593 ret = read_intra_function_calls(file);
1594 if (ret)
1595 return ret;
1596
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06001597 ret = add_call_destinations(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001598 if (ret)
1599 return ret;
1600
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -05001601 ret = add_jump_table_alts(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001602 if (ret)
1603 return ret;
1604
Josh Poimboeuf39358a02017-07-11 10:33:43 -05001605 ret = read_unwind_hints(file);
1606 if (ret)
1607 return ret;
1608
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01001609 ret = read_retpoline_hints(file);
1610 if (ret)
1611 return ret;
1612
Peter Zijlstrac4a33932020-03-10 18:57:41 +01001613 ret = read_instr_hints(file);
1614 if (ret)
1615 return ret;
1616
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001617 return 0;
1618}
1619
1620static bool is_fentry_call(struct instruction *insn)
1621{
Alexandre Chartre87cf61f2020-04-14 12:36:10 +02001622 if (insn->type == INSN_CALL && insn->call_dest &&
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001623 insn->call_dest->type == STT_NOTYPE &&
1624 !strcmp(insn->call_dest->name, "__fentry__"))
1625 return true;
1626
1627 return false;
1628}
1629
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001630static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001631{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001632 u8 ret_offset = insn->ret_offset;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001633 struct cfi_state *cfi = &state->cfi;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001634 int i;
1635
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001636 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001637 return true;
1638
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001639 if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001640 return true;
1641
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001642 if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001643 return true;
1644
Alexandre Chartrec721b3f82020-04-07 09:31:35 +02001645 /*
1646 * If there is a ret offset hint then don't check registers
1647 * because a callee-saved register might have been pushed on
1648 * the stack.
1649 */
1650 if (ret_offset)
1651 return false;
1652
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001653 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001654 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1655 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001656 return true;
Peter Zijlstrae25eea82020-04-01 16:38:19 +02001657 }
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001658
1659 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001660}
1661
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001662static bool has_valid_stack_frame(struct insn_state *state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001663{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001664 struct cfi_state *cfi = &state->cfi;
1665
1666 if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
1667 cfi->regs[CFI_BP].offset == -16)
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001668 return true;
1669
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001670 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001671 return true;
1672
1673 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001674}
1675
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001676static int update_cfi_state_regs(struct instruction *insn,
1677 struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001678 struct stack_op *op)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001679{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001680 struct cfi_reg *cfa = &cfi->cfa;
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001681
Josh Poimboeufd8dd25a2020-04-25 05:03:00 -05001682 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001683 return 0;
1684
1685 /* push */
Peter Zijlstraea242132019-02-25 12:50:09 +01001686 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001687 cfa->offset += 8;
1688
1689 /* pop */
Peter Zijlstraea242132019-02-25 12:50:09 +01001690 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001691 cfa->offset -= 8;
1692
1693 /* add immediate to sp */
1694 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1695 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1696 cfa->offset -= op->src.offset;
1697
1698 return 0;
1699}
1700
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001701static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05001702{
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001703 if (arch_callee_saved_reg(reg) &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001704 cfi->regs[reg].base == CFI_UNDEFINED) {
1705 cfi->regs[reg].base = base;
1706 cfi->regs[reg].offset = offset;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001707 }
1708}
1709
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001710static void restore_reg(struct cfi_state *cfi, unsigned char reg)
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001711{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001712 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1713 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001714}
1715
1716/*
1717 * A note about DRAP stack alignment:
1718 *
1719 * GCC has the concept of a DRAP register, which is used to help keep track of
1720 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1721 * register. The typical DRAP pattern is:
1722 *
1723 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1724 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1725 * 41 ff 72 f8 pushq -0x8(%r10)
1726 * 55 push %rbp
1727 * 48 89 e5 mov %rsp,%rbp
1728 * (more pushes)
1729 * 41 52 push %r10
1730 * ...
1731 * 41 5a pop %r10
1732 * (more pops)
1733 * 5d pop %rbp
1734 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1735 * c3 retq
1736 *
1737 * There are some variations in the epilogues, like:
1738 *
1739 * 5b pop %rbx
1740 * 41 5a pop %r10
1741 * 41 5c pop %r12
1742 * 41 5d pop %r13
1743 * 41 5e pop %r14
1744 * c9 leaveq
1745 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1746 * c3 retq
1747 *
1748 * and:
1749 *
1750 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1751 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1752 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1753 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1754 * c9 leaveq
1755 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1756 * c3 retq
1757 *
1758 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1759 * restored beforehand:
1760 *
1761 * 41 55 push %r13
1762 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1763 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1764 * ...
1765 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1766 * 41 5d pop %r13
1767 * c3 retq
1768 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001769static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
Julien Thierry65ea47d2020-03-27 15:28:47 +00001770 struct stack_op *op)
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001771{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001772 struct cfi_reg *cfa = &cfi->cfa;
1773 struct cfi_reg *regs = cfi->regs;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001774
1775 /* stack operations don't make sense with an undefined CFA */
1776 if (cfa->base == CFI_UNDEFINED) {
1777 if (insn->func) {
1778 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1779 return -1;
1780 }
1781 return 0;
1782 }
1783
Julien Thierryee819ae2020-09-04 16:30:27 +01001784 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
1785 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001786 return update_cfi_state_regs(insn, cfi, op);
Josh Poimboeuf627fce12017-07-11 10:33:42 -05001787
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001788 switch (op->dest.type) {
1789
1790 case OP_DEST_REG:
1791 switch (op->src.type) {
1792
1793 case OP_SRC_REG:
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001794 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1795 cfa->base == CFI_SP &&
1796 regs[CFI_BP].base == CFI_CFA &&
1797 regs[CFI_BP].offset == -cfa->offset) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001798
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001799 /* mov %rsp, %rbp */
1800 cfa->base = op->dest.reg;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001801 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001802 }
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001803
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001804 else if (op->src.reg == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001805 op->dest.reg == CFI_BP && cfi->drap) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001806
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001807 /* drap: mov %rsp, %rbp */
1808 regs[CFI_BP].base = CFI_BP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001809 regs[CFI_BP].offset = -cfi->stack_size;
1810 cfi->bp_scratch = false;
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001811 }
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001812
Josh Poimboeuf0d0970e2017-09-20 16:24:32 -05001813 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1814
1815 /*
1816 * mov %rsp, %reg
1817 *
1818 * This is needed for the rare case where GCC
1819 * does:
1820 *
1821 * mov %rsp, %rax
1822 * ...
1823 * mov %rax, %rsp
1824 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001825 cfi->vals[op->dest.reg].base = CFI_CFA;
1826 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001827 }
1828
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001829 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1830 cfa->base == CFI_BP) {
1831
1832 /*
1833 * mov %rbp, %rsp
1834 *
1835 * Restore the original stack pointer (Clang).
1836 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001837 cfi->stack_size = -cfi->regs[CFI_BP].offset;
Josh Poimboeuf3c1f0582018-03-22 13:00:37 -05001838 }
1839
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001840 else if (op->dest.reg == cfa->base) {
1841
1842 /* mov %reg, %rsp */
1843 if (cfa->base == CFI_SP &&
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001844 cfi->vals[op->src.reg].base == CFI_CFA) {
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001845
1846 /*
1847 * This is needed for the rare case
1848 * where GCC does something dumb like:
1849 *
1850 * lea 0x8(%rsp), %rcx
1851 * ...
1852 * mov %rcx, %rsp
1853 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001854 cfa->offset = -cfi->vals[op->src.reg].offset;
1855 cfi->stack_size = cfa->offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001856
1857 } else {
1858 cfa->base = CFI_UNDEFINED;
1859 cfa->offset = 0;
1860 }
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001861 }
1862
1863 break;
1864
1865 case OP_SRC_ADD:
1866 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1867
1868 /* add imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001869 cfi->stack_size -= op->src.offset;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001870 if (cfa->base == CFI_SP)
1871 cfa->offset -= op->src.offset;
1872 break;
1873 }
1874
1875 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1876
1877 /* lea disp(%rbp), %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001878 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001879 break;
1880 }
1881
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001882 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001883
1884 /* drap: lea disp(%rsp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001885 cfi->drap_reg = op->dest.reg;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001886
1887 /*
1888 * lea disp(%rsp), %reg
1889 *
1890 * This is needed for the rare case where GCC
1891 * does something dumb like:
1892 *
1893 * lea 0x8(%rsp), %rcx
1894 * ...
1895 * mov %rcx, %rsp
1896 */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001897 cfi->vals[op->dest.reg].base = CFI_CFA;
1898 cfi->vals[op->dest.reg].offset = \
1899 -cfi->stack_size + op->src.offset;
Josh Poimboeufdd88a0a2017-08-29 12:51:03 -05001900
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001901 break;
1902 }
1903
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001904 if (cfi->drap && op->dest.reg == CFI_SP &&
1905 op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001906
1907 /* drap: lea disp(%drap), %rsp */
1908 cfa->base = CFI_SP;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001909 cfa->offset = cfi->stack_size = -op->src.offset;
1910 cfi->drap_reg = CFI_UNDEFINED;
1911 cfi->drap = false;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001912 break;
1913 }
1914
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001915 if (op->dest.reg == cfi->cfa.base) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001916 WARN_FUNC("unsupported stack register modification",
1917 insn->sec, insn->offset);
1918 return -1;
1919 }
1920
1921 break;
1922
1923 case OP_SRC_AND:
1924 if (op->dest.reg != CFI_SP ||
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001925 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1926 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001927 WARN_FUNC("unsupported stack pointer realignment",
1928 insn->sec, insn->offset);
1929 return -1;
1930 }
1931
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001932 if (cfi->drap_reg != CFI_UNDEFINED) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001933 /* drap: and imm, %rsp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001934 cfa->base = cfi->drap_reg;
1935 cfa->offset = cfi->stack_size = 0;
1936 cfi->drap = true;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001937 }
1938
1939 /*
1940 * Older versions of GCC (4.8ish) realign the stack
1941 * without DRAP, with a frame pointer.
1942 */
1943
1944 break;
1945
1946 case OP_SRC_POP:
Peter Zijlstraea242132019-02-25 12:50:09 +01001947 case OP_SRC_POPF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001948 if (!cfi->drap && op->dest.reg == cfa->base) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001949
1950 /* pop %rbp */
1951 cfa->base = CFI_SP;
1952 }
1953
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001954 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
1955 op->dest.reg == cfi->drap_reg &&
1956 cfi->drap_offset == -cfi->stack_size) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001957
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001958 /* drap: pop %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001959 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001960 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001961 cfi->drap_offset = -1;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001962
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001963 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001964
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001965 /* pop %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001966 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001967 }
1968
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001969 cfi->stack_size -= 8;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001970 if (cfa->base == CFI_SP)
1971 cfa->offset -= 8;
1972
1973 break;
1974
1975 case OP_SRC_REG_INDIRECT:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001976 if (cfi->drap && op->src.reg == CFI_BP &&
1977 op->src.offset == cfi->drap_offset) {
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001978
1979 /* drap: mov disp(%rbp), %drap */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001980 cfa->base = cfi->drap_reg;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001981 cfa->offset = 0;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001982 cfi->drap_offset = -1;
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05001983 }
1984
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001985 if (cfi->drap && op->src.reg == CFI_BP &&
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001986 op->src.offset == regs[op->dest.reg].offset) {
1987
1988 /* drap: mov disp(%rbp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001989 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001990
1991 } else if (op->src.reg == cfa->base &&
1992 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1993
1994 /* mov disp(%rbp), %reg */
1995 /* mov disp(%rsp), %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01001996 restore_reg(cfi, op->dest.reg);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05001997 }
1998
1999 break;
2000
2001 default:
2002 WARN_FUNC("unknown stack-related instruction",
2003 insn->sec, insn->offset);
2004 return -1;
2005 }
2006
2007 break;
2008
2009 case OP_DEST_PUSH:
Peter Zijlstraea242132019-02-25 12:50:09 +01002010 case OP_DEST_PUSHF:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002011 cfi->stack_size += 8;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002012 if (cfa->base == CFI_SP)
2013 cfa->offset += 8;
2014
2015 if (op->src.type != OP_SRC_REG)
2016 break;
2017
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002018 if (cfi->drap) {
2019 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002020
2021 /* drap: push %drap */
2022 cfa->base = CFI_BP_INDIRECT;
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002023 cfa->offset = -cfi->stack_size;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002024
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002025 /* save drap so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002026 cfi->drap_offset = -cfi->stack_size;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002027
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002028 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002029
2030 /* drap: push %rbp */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002031 cfi->stack_size = 0;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002032
Julien Thierryf4f80392020-09-15 08:53:16 +01002033 } else {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002034
2035 /* drap: push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002036 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002037 }
2038
2039 } else {
2040
2041 /* push %reg */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002042 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002043 }
2044
2045 /* detect when asm code uses rbp as a scratch register */
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -05002046 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002047 cfa->base != CFI_BP)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002048 cfi->bp_scratch = true;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002049 break;
2050
2051 case OP_DEST_REG_INDIRECT:
2052
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002053 if (cfi->drap) {
2054 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002055
2056 /* drap: mov %drap, disp(%rbp) */
2057 cfa->base = CFI_BP_INDIRECT;
2058 cfa->offset = op->dest.offset;
2059
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002060 /* save drap offset so we know when to restore it */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002061 cfi->drap_offset = op->dest.offset;
Julien Thierryf4f80392020-09-15 08:53:16 +01002062 } else {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002063
2064 /* drap: mov reg, disp(%rbp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002065 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002066 }
2067
2068 } else if (op->dest.reg == cfa->base) {
2069
2070 /* mov reg, disp(%rbp) */
2071 /* mov reg, disp(%rsp) */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002072 save_reg(cfi, op->src.reg, CFI_CFA,
2073 op->dest.offset - cfi->cfa.offset);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002074 }
2075
2076 break;
2077
2078 case OP_DEST_LEAVE:
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002079 if ((!cfi->drap && cfa->base != CFI_BP) ||
2080 (cfi->drap && cfa->base != cfi->drap_reg)) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002081 WARN_FUNC("leave instruction with modified stack frame",
2082 insn->sec, insn->offset);
2083 return -1;
2084 }
2085
2086 /* leave (mov %rbp, %rsp; pop %rbp) */
2087
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002088 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2089 restore_reg(cfi, CFI_BP);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002090
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002091 if (!cfi->drap) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002092 cfa->base = CFI_SP;
2093 cfa->offset -= 8;
2094 }
2095
2096 break;
2097
2098 case OP_DEST_MEM:
Peter Zijlstraea242132019-02-25 12:50:09 +01002099 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002100 WARN_FUNC("unknown stack-related memory operation",
2101 insn->sec, insn->offset);
2102 return -1;
2103 }
2104
2105 /* pop mem */
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002106 cfi->stack_size -= 8;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002107 if (cfa->base == CFI_SP)
2108 cfa->offset -= 8;
2109
2110 break;
2111
2112 default:
2113 WARN_FUNC("unknown stack-related instruction",
2114 insn->sec, insn->offset);
2115 return -1;
2116 }
2117
2118 return 0;
2119}
2120
Julien Thierry65ea47d2020-03-27 15:28:47 +00002121static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2122{
2123 struct stack_op *op;
2124
2125 list_for_each_entry(op, &insn->stack_ops, list) {
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002126 struct cfi_state old_cfi = state->cfi;
Julien Thierry65ea47d2020-03-27 15:28:47 +00002127 int res;
2128
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002129 res = update_cfi_state(insn, &state->cfi, op);
Julien Thierry65ea47d2020-03-27 15:28:47 +00002130 if (res)
2131 return res;
2132
Peter Zijlstraab3852a2020-05-08 12:34:33 +02002133 if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) {
2134 WARN_FUNC("alternative modifies stack", insn->sec, insn->offset);
2135 return -1;
2136 }
2137
Julien Thierry65ea47d2020-03-27 15:28:47 +00002138 if (op->dest.type == OP_DEST_PUSHF) {
2139 if (!state->uaccess_stack) {
2140 state->uaccess_stack = 1;
2141 } else if (state->uaccess_stack >> 31) {
2142 WARN_FUNC("PUSHF stack exhausted",
2143 insn->sec, insn->offset);
2144 return 1;
2145 }
2146 state->uaccess_stack <<= 1;
2147 state->uaccess_stack |= state->uaccess;
2148 }
2149
2150 if (op->src.type == OP_SRC_POPF) {
2151 if (state->uaccess_stack) {
2152 state->uaccess = state->uaccess_stack & 1;
2153 state->uaccess_stack >>= 1;
2154 if (state->uaccess_stack == 1)
2155 state->uaccess_stack = 0;
2156 }
2157 }
2158 }
2159
2160 return 0;
2161}
2162
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002163static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002164{
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002165 struct cfi_state *cfi1 = &insn->cfi;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002166 int i;
2167
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002168 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2169
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002170 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2171 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002172 cfi1->cfa.base, cfi1->cfa.offset,
2173 cfi2->cfa.base, cfi2->cfa.offset);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002174
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002175 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002176 for (i = 0; i < CFI_NUM_REGS; i++) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002177 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002178 sizeof(struct cfi_reg)))
2179 continue;
2180
2181 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2182 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002183 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2184 i, cfi2->regs[i].base, cfi2->regs[i].offset);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002185 break;
2186 }
2187
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002188 } else if (cfi1->type != cfi2->type) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -05002189
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002190 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2191 insn->sec, insn->offset, cfi1->type, cfi2->type);
2192
2193 } else if (cfi1->drap != cfi2->drap ||
2194 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2195 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2196
Josh Poimboeufbf4d1a82017-08-10 16:37:26 -05002197 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002198 insn->sec, insn->offset,
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002199 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2200 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002201
2202 } else
2203 return true;
2204
2205 return false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002206}
2207
Peter Zijlstraea242132019-02-25 12:50:09 +01002208static inline bool func_uaccess_safe(struct symbol *func)
2209{
2210 if (func)
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002211 return func->uaccess_safe;
Peter Zijlstraea242132019-02-25 12:50:09 +01002212
2213 return false;
2214}
2215
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002216static inline const char *call_dest_name(struct instruction *insn)
Peter Zijlstraea242132019-02-25 12:50:09 +01002217{
2218 if (insn->call_dest)
2219 return insn->call_dest->name;
2220
2221 return "{dynamic}";
2222}
2223
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002224static inline bool noinstr_call_dest(struct symbol *func)
2225{
2226 /*
2227 * We can't deal with indirect function calls at present;
2228 * assume they're instrumented.
2229 */
2230 if (!func)
2231 return false;
2232
2233 /*
2234 * If the symbol is from a noinstr section; we good.
2235 */
2236 if (func->sec->noinstr)
2237 return true;
2238
2239 /*
2240 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2241 * something 'BAD' happened. At the risk of taking the machine down,
2242 * let them proceed to get the message out.
2243 */
2244 if (!strncmp(func->name, "__ubsan_handle_", 15))
2245 return true;
2246
2247 return false;
2248}
2249
Peter Zijlstraea242132019-02-25 12:50:09 +01002250static int validate_call(struct instruction *insn, struct insn_state *state)
2251{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002252 if (state->noinstr && state->instr <= 0 &&
Peter Zijlstra6b643a02020-06-03 20:09:06 +02002253 !noinstr_call_dest(insn->call_dest)) {
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002254 WARN_FUNC("call to %s() leaves .noinstr.text section",
2255 insn->sec, insn->offset, call_dest_name(insn));
2256 return 1;
2257 }
2258
Peter Zijlstraea242132019-02-25 12:50:09 +01002259 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2260 WARN_FUNC("call to %s() with UACCESS enabled",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002261 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstraea242132019-02-25 12:50:09 +01002262 return 1;
2263 }
2264
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002265 if (state->df) {
2266 WARN_FUNC("call to %s() with DF set",
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002267 insn->sec, insn->offset, call_dest_name(insn));
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002268 return 1;
2269 }
2270
Peter Zijlstraea242132019-02-25 12:50:09 +01002271 return 0;
2272}
2273
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002274static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2275{
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002276 if (has_modified_stack_frame(insn, state)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002277 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2278 insn->sec, insn->offset);
2279 return 1;
2280 }
2281
Peter Zijlstraea242132019-02-25 12:50:09 +01002282 return validate_call(insn, state);
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002283}
2284
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002285static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2286{
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002287 if (state->noinstr && state->instr > 0) {
2288 WARN_FUNC("return with instrumentation enabled",
2289 insn->sec, insn->offset);
2290 return 1;
2291 }
2292
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002293 if (state->uaccess && !func_uaccess_safe(func)) {
2294 WARN_FUNC("return with UACCESS enabled",
2295 insn->sec, insn->offset);
2296 return 1;
2297 }
2298
2299 if (!state->uaccess && func_uaccess_safe(func)) {
2300 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2301 insn->sec, insn->offset);
2302 return 1;
2303 }
2304
2305 if (state->df) {
2306 WARN_FUNC("return with DF set",
2307 insn->sec, insn->offset);
2308 return 1;
2309 }
2310
Peter Zijlstrae25eea82020-04-01 16:38:19 +02002311 if (func && has_modified_stack_frame(insn, state)) {
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002312 WARN_FUNC("return with modified stack frame",
2313 insn->sec, insn->offset);
2314 return 1;
2315 }
2316
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002317 if (state->cfi.bp_scratch) {
Josh Poimboeufb2966952020-04-01 13:23:29 -05002318 WARN_FUNC("BP used as a scratch register",
2319 insn->sec, insn->offset);
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002320 return 1;
2321 }
2322
2323 return 0;
2324}
2325
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002326/*
Peter Zijlstra7117f16b2020-04-28 19:37:01 +02002327 * Alternatives should not contain any ORC entries, this in turn means they
2328 * should not contain any CFI ops, which implies all instructions should have
2329 * the same same CFI state.
2330 *
2331 * It is possible to constuct alternatives that have unreachable holes that go
2332 * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2333 * states which then results in ORC entries, which we just said we didn't want.
2334 *
2335 * Avoid them by copying the CFI entry of the first instruction into the whole
2336 * alternative.
2337 */
2338static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn)
2339{
2340 struct instruction *first_insn = insn;
2341 int alt_group = insn->alt_group;
2342
2343 sec_for_each_insn_continue(file, insn) {
2344 if (insn->alt_group != alt_group)
2345 break;
2346 insn->cfi = first_insn->cfi;
2347 }
2348}
2349
2350/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002351 * Follow the branch starting at the given instruction, and recursively follow
2352 * any other branches (jumps). Meanwhile, track the frame pointer state at
2353 * each instruction and validate all the rules described in
2354 * tools/objtool/Documentation/stack-validation.txt.
2355 */
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002356static int validate_branch(struct objtool_file *file, struct symbol *func,
Peter Zijlstrab7460462020-04-02 10:15:51 +02002357 struct instruction *insn, struct insn_state state)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002358{
2359 struct alternative *alt;
Peter Zijlstrab7460462020-04-02 10:15:51 +02002360 struct instruction *next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002361 struct section *sec;
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002362 u8 visited;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002363 int ret;
2364
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002365 sec = insn->sec;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002366
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002367 while (1) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002368 next_insn = next_insn_same_sec(file, insn);
2369
Josh Poimboeuf13810432018-05-09 22:39:15 -05002370 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
Josh Poimboeufee976382017-08-11 12:24:15 -05002371 WARN("%s() falls through to next function %s()",
2372 func->name, insn->func->name);
2373 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002374 }
2375
Josh Poimboeuf48550222017-07-07 09:19:42 -05002376 if (func && insn->ignore) {
2377 WARN_FUNC("BUG: why am I validating an ignored function?",
2378 sec, insn->offset);
Josh Poimboeuf12b25722017-08-10 16:37:25 -05002379 return 1;
Josh Poimboeuf48550222017-07-07 09:19:42 -05002380 }
2381
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002382 visited = 1 << state.uaccess;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002383 if (insn->visited) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002384 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002385 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002386
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002387 if (insn->visited & visited)
Peter Zijlstraea242132019-02-25 12:50:09 +01002388 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002389 }
2390
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002391 if (state.noinstr)
2392 state.instr += insn->instr;
2393
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002394 if (insn->hint)
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002395 state.cfi = insn->cfi;
Peter Zijlstrac536ed22020-04-01 16:54:26 +02002396 else
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002397 insn->cfi = state.cfi;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002398
Peter Zijlstra882a0db2019-07-24 17:47:26 -05002399 insn->visited |= visited;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002400
Peter Zijlstra7117f16b2020-04-28 19:37:01 +02002401 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002402 bool skip_orig = false;
2403
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002404 list_for_each_entry(alt, &insn->alts, list) {
Peter Zijlstra764eef42019-03-01 11:19:03 +01002405 if (alt->skip_orig)
2406 skip_orig = true;
2407
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002408 ret = validate_branch(file, func, alt->insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002409 if (ret) {
2410 if (backtrace)
2411 BT_FUNC("(alt)", insn);
2412 return ret;
2413 }
Josh Poimboeufa845c7c2018-01-29 22:00:39 -06002414 }
Peter Zijlstra764eef42019-03-01 11:19:03 +01002415
Peter Zijlstra7117f16b2020-04-28 19:37:01 +02002416 if (insn->alt_group)
2417 fill_alternative_cfi(file, insn);
2418
Peter Zijlstra764eef42019-03-01 11:19:03 +01002419 if (skip_orig)
2420 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002421 }
2422
Peter Zijlstra60041bc2020-04-24 16:16:41 +02002423 if (handle_insn_ops(insn, &state))
2424 return 1;
2425
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002426 switch (insn->type) {
2427
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002428 case INSN_RETURN:
Peter Zijlstraa92e92d2020-03-10 18:07:44 +01002429 return validate_return(func, insn, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002430
2431 case INSN_CALL:
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002432 case INSN_CALL_DYNAMIC:
Peter Zijlstraea242132019-02-25 12:50:09 +01002433 ret = validate_call(insn, &state);
2434 if (ret)
2435 return ret;
2436
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002437 if (!no_fp && func && !is_fentry_call(insn) &&
2438 !has_valid_stack_frame(&state)) {
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002439 WARN_FUNC("call without frame pointer save/setup",
2440 sec, insn->offset);
2441 return 1;
2442 }
Josh Poimboeufc9bab222019-07-17 20:36:51 -05002443
2444 if (dead_end_function(file, insn->call_dest))
2445 return 0;
2446
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002447 if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) {
2448 list_add_tail(&insn->static_call_node,
2449 &file->static_call_list);
2450 }
2451
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002452 break;
2453
2454 case INSN_JUMP_CONDITIONAL:
2455 case INSN_JUMP_UNCONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002456 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002457 ret = validate_sibling_call(insn, &state);
2458 if (ret)
2459 return ret;
2460
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002461 } else if (insn->jump_dest) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002462 ret = validate_branch(file, func,
2463 insn->jump_dest, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002464 if (ret) {
2465 if (backtrace)
2466 BT_FUNC("(branch)", insn);
2467 return ret;
2468 }
Josh Poimboeuf48550222017-07-07 09:19:42 -05002469 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002470
2471 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2472 return 0;
2473
2474 break;
2475
2476 case INSN_JUMP_DYNAMIC:
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002477 case INSN_JUMP_DYNAMIC_CONDITIONAL:
Josh Poimboeuf0c1ddd32019-07-17 20:36:52 -05002478 if (func && is_sibling_call(insn)) {
Peter Zijlstra54262aa2019-03-06 12:58:15 +01002479 ret = validate_sibling_call(insn, &state);
2480 if (ret)
2481 return ret;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002482 }
2483
Josh Poimboeufb68b9902019-07-17 20:36:57 -05002484 if (insn->type == INSN_JUMP_DYNAMIC)
2485 return 0;
2486
2487 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002488
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002489 case INSN_CONTEXT_SWITCH:
2490 if (func && (!next_insn || !next_insn->hint)) {
2491 WARN_FUNC("unsupported instruction in callable function",
2492 sec, insn->offset);
2493 return 1;
2494 }
2495 return 0;
2496
Peter Zijlstraea242132019-02-25 12:50:09 +01002497 case INSN_STAC:
2498 if (state.uaccess) {
2499 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2500 return 1;
2501 }
2502
2503 state.uaccess = true;
2504 break;
2505
2506 case INSN_CLAC:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002507 if (!state.uaccess && func) {
Peter Zijlstraea242132019-02-25 12:50:09 +01002508 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2509 return 1;
2510 }
2511
2512 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2513 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2514 return 1;
2515 }
2516
2517 state.uaccess = false;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002518 break;
2519
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002520 case INSN_STD:
2521 if (state.df)
2522 WARN_FUNC("recursive STD", sec, insn->offset);
2523
2524 state.df = true;
2525 break;
2526
2527 case INSN_CLD:
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002528 if (!state.df && func)
Peter Zijlstra2f0f9e92019-02-25 11:10:55 +01002529 WARN_FUNC("redundant CLD", sec, insn->offset);
2530
2531 state.df = false;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002532 break;
2533
2534 default:
2535 break;
2536 }
2537
2538 if (insn->dead_end)
2539 return 0;
2540
Josh Poimboeuf00d961802017-09-18 21:43:30 -05002541 if (!next_insn) {
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002542 if (state.cfi.cfa.base == CFI_UNDEFINED)
Josh Poimboeuf00d961802017-09-18 21:43:30 -05002543 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002544 WARN("%s: unexpected end of section", sec->name);
2545 return 1;
2546 }
Josh Poimboeuf00d961802017-09-18 21:43:30 -05002547
2548 insn = next_insn;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002549 }
2550
2551 return 0;
2552}
2553
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002554static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002555{
2556 struct instruction *insn;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002557 struct insn_state state;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002558 int ret, warnings = 0;
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002559
2560 if (!file->hints)
2561 return 0;
2562
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002563 init_insn_state(&state, sec);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002564
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002565 if (sec) {
2566 insn = find_insn(file, sec, 0);
2567 if (!insn)
2568 return 0;
2569 } else {
2570 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2571 }
2572
2573 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002574 if (insn->hint && !insn->visited) {
Josh Poimboeufc705cec2019-07-17 20:36:47 -05002575 ret = validate_branch(file, insn->func, insn, state);
Peter Zijlstra7697eee2019-03-01 11:15:49 +01002576 if (ret && backtrace)
2577 BT_FUNC("<=== (hint)", insn);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002578 warnings += ret;
2579 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002580
2581 insn = list_next_entry(insn, list);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002582 }
2583
2584 return warnings;
2585}
2586
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002587static int validate_retpoline(struct objtool_file *file)
2588{
2589 struct instruction *insn;
2590 int warnings = 0;
2591
2592 for_each_insn(file, insn) {
2593 if (insn->type != INSN_JUMP_DYNAMIC &&
2594 insn->type != INSN_CALL_DYNAMIC)
2595 continue;
2596
2597 if (insn->retpoline_safe)
2598 continue;
2599
Peter Zijlstraca41b972018-01-31 10:18:28 +01002600 /*
2601 * .init.text code is ran before userspace and thus doesn't
2602 * strictly need retpolines, except for modules which are
2603 * loaded late, they very much do need retpoline in their
2604 * .init.text
2605 */
2606 if (!strcmp(insn->sec->name, ".init.text") && !module)
2607 continue;
2608
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002609 WARN_FUNC("indirect %s found in RETPOLINE build",
2610 insn->sec, insn->offset,
2611 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2612
2613 warnings++;
2614 }
2615
2616 return warnings;
2617}
2618
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002619static bool is_kasan_insn(struct instruction *insn)
2620{
2621 return (insn->type == INSN_CALL &&
2622 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2623}
2624
2625static bool is_ubsan_insn(struct instruction *insn)
2626{
2627 return (insn->type == INSN_CALL &&
2628 !strcmp(insn->call_dest->name,
2629 "__ubsan_handle_builtin_unreachable"));
2630}
2631
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002632static bool ignore_unreachable_insn(struct instruction *insn)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002633{
2634 int i;
2635
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002636 if (insn->ignore || insn->type == INSN_NOP)
2637 return true;
2638
2639 /*
2640 * Ignore any unused exceptions. This can happen when a whitelisted
2641 * function has an exception table entry.
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002642 *
2643 * Also ignore alternative replacement instructions. This can happen
2644 * when a whitelisted function uses one of the ALTERNATIVE macros.
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002645 */
Josh Poimboeuf0e2bb2b2017-07-27 15:56:54 -05002646 if (!strcmp(insn->sec->name, ".fixup") ||
2647 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2648 !strcmp(insn->sec->name, ".altinstr_aux"))
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002649 return true;
2650
Josh Poimboeufbd841d62020-04-01 13:23:25 -05002651 if (!insn->func)
2652 return false;
2653
2654 /*
2655 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2656 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2657 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2658 * (or occasionally a JMP to UD2).
2659 */
2660 if (list_prev_entry(insn, list)->dead_end &&
2661 (insn->type == INSN_BUG ||
2662 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2663 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2664 return true;
2665
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002666 /*
2667 * Check if this (or a subsequent) instruction is related to
2668 * CONFIG_UBSAN or CONFIG_KASAN.
2669 *
2670 * End the search at 5 instructions to avoid going into the weeds.
2671 */
2672 for (i = 0; i < 5; i++) {
2673
2674 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2675 return true;
2676
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002677 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2678 if (insn->jump_dest &&
2679 insn->jump_dest->func == insn->func) {
2680 insn = insn->jump_dest;
2681 continue;
2682 }
2683
2684 break;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002685 }
2686
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002687 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002688 break;
Josh Poimboeuffe24e272018-02-08 17:09:25 -06002689
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002690 insn = list_next_entry(insn, list);
2691 }
2692
2693 return false;
2694}
2695
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002696static int validate_symbol(struct objtool_file *file, struct section *sec,
2697 struct symbol *sym, struct insn_state *state)
2698{
2699 struct instruction *insn;
2700 int ret;
2701
2702 if (!sym->len) {
2703 WARN("%s() is missing an ELF size annotation", sym->name);
2704 return 1;
2705 }
2706
2707 if (sym->pfunc != sym || sym->alias != sym)
2708 return 0;
2709
2710 insn = find_insn(file, sec, sym->offset);
2711 if (!insn || insn->ignore || insn->visited)
2712 return 0;
2713
2714 state->uaccess = sym->uaccess_safe;
2715
2716 ret = validate_branch(file, insn->func, insn, *state);
2717 if (ret && backtrace)
2718 BT_FUNC("<=== (sym)", insn);
2719 return ret;
2720}
2721
Peter Zijlstra350994b2020-03-23 20:57:13 +01002722static int validate_section(struct objtool_file *file, struct section *sec)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002723{
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002724 struct insn_state state;
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002725 struct symbol *func;
2726 int warnings = 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002727
Peter Zijlstra350994b2020-03-23 20:57:13 +01002728 list_for_each_entry(func, &sec->symbol_list, list) {
2729 if (func->type != STT_FUNC)
2730 continue;
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -05002731
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002732 init_insn_state(&state, sec);
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002733 state.cfi.cfa = initial_func_cfi.cfa;
2734 memcpy(&state.cfi.regs, &initial_func_cfi.regs,
Julien Thierry0699e552020-03-27 15:28:40 +00002735 CFI_NUM_REGS * sizeof(struct cfi_reg));
Peter Zijlstrae7c02192020-03-25 14:04:45 +01002736 state.cfi.stack_size = initial_func_cfi.cfa.offset;
Julien Thierry0699e552020-03-27 15:28:40 +00002737
Peter Zijlstra4b5e2e72020-03-23 21:17:50 +01002738 warnings += validate_symbol(file, sec, func, &state);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002739 }
2740
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002741 return warnings;
2742}
2743
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002744static int validate_vmlinux_functions(struct objtool_file *file)
2745{
2746 struct section *sec;
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002747 int warnings = 0;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002748
2749 sec = find_section_by_name(file->elf, ".noinstr.text");
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002750 if (sec) {
2751 warnings += validate_section(file, sec);
2752 warnings += validate_unwind_hints(file, sec);
2753 }
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002754
Thomas Gleixner0cc9ac8d2020-03-25 17:18:17 +01002755 sec = find_section_by_name(file->elf, ".entry.text");
2756 if (sec) {
2757 warnings += validate_section(file, sec);
2758 warnings += validate_unwind_hints(file, sec);
2759 }
Peter Zijlstra932f8e92020-03-23 18:26:03 +01002760
2761 return warnings;
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002762}
2763
Peter Zijlstra350994b2020-03-23 20:57:13 +01002764static int validate_functions(struct objtool_file *file)
2765{
2766 struct section *sec;
2767 int warnings = 0;
2768
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002769 for_each_sec(file, sec) {
2770 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2771 continue;
2772
Peter Zijlstra350994b2020-03-23 20:57:13 +01002773 warnings += validate_section(file, sec);
Peter Zijlstrada837bd2020-03-23 21:11:14 +01002774 }
Peter Zijlstra350994b2020-03-23 20:57:13 +01002775
2776 return warnings;
2777}
2778
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002779static int validate_reachable_instructions(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002780{
2781 struct instruction *insn;
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002782
2783 if (file->ignore_unreachables)
2784 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002785
2786 for_each_insn(file, insn) {
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002787 if (insn->visited || ignore_unreachable_insn(insn))
2788 continue;
2789
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002790 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2791 return 1;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002792 }
2793
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002794 return 0;
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002795}
2796
Julien Thierryd44becb2020-08-25 13:47:40 +01002797int check(struct objtool_file *file)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002798{
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002799 int ret, warnings = 0;
2800
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002801 arch_initial_func_cfi_state(&initial_func_cfi);
2802
Julien Thierry6545eb02020-08-25 13:47:39 +01002803 ret = decode_sections(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002804 if (ret < 0)
2805 goto out;
2806 warnings += ret;
2807
Julien Thierry6545eb02020-08-25 13:47:39 +01002808 if (list_empty(&file->insn_list))
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002809 goto out;
2810
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002811 if (vmlinux && !validate_dup) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002812 ret = validate_vmlinux_functions(file);
Peter Zijlstrac4a33932020-03-10 18:57:41 +01002813 if (ret < 0)
2814 goto out;
2815
2816 warnings += ret;
2817 goto out;
2818 }
2819
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002820 if (retpoline) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002821 ret = validate_retpoline(file);
Peter Zijlstrab5bc2232018-01-16 10:24:06 +01002822 if (ret < 0)
2823 return ret;
2824 warnings += ret;
2825 }
2826
Julien Thierry6545eb02020-08-25 13:47:39 +01002827 ret = validate_functions(file);
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002828 if (ret < 0)
2829 goto out;
2830 warnings += ret;
2831
Julien Thierry6545eb02020-08-25 13:47:39 +01002832 ret = validate_unwind_hints(file, NULL);
Josh Poimboeuf39358a02017-07-11 10:33:43 -05002833 if (ret < 0)
2834 goto out;
2835 warnings += ret;
2836
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002837 if (!warnings) {
Julien Thierry6545eb02020-08-25 13:47:39 +01002838 ret = validate_reachable_instructions(file);
Josh Poimboeufbaa414692017-06-28 10:11:07 -05002839 if (ret < 0)
2840 goto out;
2841 warnings += ret;
2842 }
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002843
Julien Thierry6545eb02020-08-25 13:47:39 +01002844 ret = create_static_call_sections(file);
Josh Poimboeuf1e7e4782020-08-18 15:57:45 +02002845 if (ret < 0)
2846 goto out;
2847 warnings += ret;
2848
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002849out:
Josh Poimboeuf644592d2020-02-10 12:32:38 -06002850 if (ret < 0) {
2851 /*
2852 * Fatal error. The binary is corrupt or otherwise broken in
2853 * some way, or objtool itself is broken. Fail the kernel
2854 * build.
2855 */
2856 return ret;
2857 }
2858
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05002859 return 0;
2860}