1 // SPDX-License-Identifier: GPL-2.0-or-later
9 #define unlikely(cond) (cond)
11 #include "../../../arch/x86/lib/inat.c"
12 #include "../../../arch/x86/lib/insn.c"
14 #define CONFIG_64BIT 1
17 #include <asm/orc_types.h>
18 #include <objtool/check.h>
19 #include <objtool/elf.h>
20 #include <objtool/arch.h>
21 #include <objtool/warn.h>
22 #include <objtool/endianness.h>
23 #include <objtool/builtin.h>
26 int arch_ftrace_match(char *name)
28 return !strcmp(name, "__fentry__");
31 static int is_x86_64(const struct elf *elf)
33 switch (elf->ehdr.e_machine) {
39 WARN("unexpected ELF machine type %d", elf->ehdr.e_machine);
44 bool arch_callee_saved_reg(unsigned char reg)
71 unsigned long arch_dest_reloc_offset(int addend)
76 unsigned long arch_jump_destination(struct instruction *insn)
78 return insn->offset + insn->len + insn->immediate;
81 bool arch_pc_relative_reloc(struct reloc *reloc)
84 * All relocation types where P (the address of the target)
85 * is included in the computation.
87 switch (reloc_type(reloc)) {
94 case R_X86_64_GOTPC32:
95 case R_X86_64_GOTPCREL:
106 if (!(op = calloc(1, sizeof(*op)))) \
108 else for (*ops_list = op, ops_list = &op->next; op; op = NULL)
111 * Helpers to decode ModRM/SIB:
113 * r/m| AX CX DX BX | SP | BP | SI DI |
114 * | R8 R9 R10 R11 | R12 | R13 | R14 R15 |
115 * Mod+----------------+-----+-----+---------+
116 * 00 | [r/m] |[SIB]|[IP+]| [r/m] |
117 * 01 | [r/m + d8] |[S+d]| [r/m + d8] |
118 * 10 | [r/m + d32] |[S+D]| [r/m + d32] |
122 #define mod_is_mem() (modrm_mod != 3)
123 #define mod_is_reg() (modrm_mod == 3)
125 #define is_RIP() ((modrm_rm & 7) == CFI_BP && modrm_mod == 0)
126 #define have_SIB() ((modrm_rm & 7) == CFI_SP && mod_is_mem())
129 * Check the ModRM register. If there is a SIB byte then check with
130 * the SIB base register. But if the SIB base is 5 (i.e. CFI_BP) and
131 * ModRM mod is 0 then there is no base register.
133 #define rm_is(reg) (have_SIB() ? \
134 sib_base == (reg) && sib_index == CFI_SP && \
135 (sib_base != CFI_BP || modrm_mod != 0) : \
138 #define rm_is_mem(reg) (mod_is_mem() && !is_RIP() && rm_is(reg))
139 #define rm_is_reg(reg) (mod_is_reg() && modrm_rm == (reg))
141 static bool has_notrack_prefix(struct insn *insn)
145 for (i = 0; i < insn->prefixes.nbytes; i++) {
146 if (insn->prefixes.bytes[i] == 0x3e)
153 int arch_decode_instruction(struct objtool_file *file, const struct section *sec,
154 unsigned long offset, unsigned int maxlen,
155 struct instruction *insn)
157 struct stack_op **ops_list = &insn->stack_ops;
158 const struct elf *elf = file->elf;
161 unsigned char op1, op2, op3, prefix,
162 rex = 0, rex_b = 0, rex_r = 0, rex_w = 0, rex_x = 0,
163 modrm = 0, modrm_mod = 0, modrm_rm = 0, modrm_reg = 0,
164 sib = 0, /* sib_scale = 0, */ sib_index = 0, sib_base = 0;
165 struct stack_op *op = NULL;
169 x86_64 = is_x86_64(elf);
173 ret = insn_decode(&ins, sec->data->d_buf + offset, maxlen,
174 x86_64 ? INSN_MODE_64 : INSN_MODE_32);
176 WARN("can't decode instruction at %s:0x%lx", sec->name, offset);
180 insn->len = ins.length;
181 insn->type = INSN_OTHER;
183 if (ins.vex_prefix.nbytes)
186 prefix = ins.prefixes.bytes[0];
188 op1 = ins.opcode.bytes[0];
189 op2 = ins.opcode.bytes[1];
190 op3 = ins.opcode.bytes[2];
192 if (ins.rex_prefix.nbytes) {
193 rex = ins.rex_prefix.bytes[0];
194 rex_w = X86_REX_W(rex) >> 3;
195 rex_r = X86_REX_R(rex) >> 2;
196 rex_x = X86_REX_X(rex) >> 1;
197 rex_b = X86_REX_B(rex);
200 if (ins.modrm.nbytes) {
201 modrm = ins.modrm.bytes[0];
202 modrm_mod = X86_MODRM_MOD(modrm);
203 modrm_reg = X86_MODRM_REG(modrm) + 8*rex_r;
204 modrm_rm = X86_MODRM_RM(modrm) + 8*rex_b;
207 if (ins.sib.nbytes) {
208 sib = ins.sib.bytes[0];
209 /* sib_scale = X86_SIB_SCALE(sib); */
210 sib_index = X86_SIB_INDEX(sib) + 8*rex_x;
211 sib_base = X86_SIB_BASE(sib) + 8*rex_b;
218 if (rex_w && rm_is_reg(CFI_SP)) {
220 /* add/sub reg, %rsp */
222 op->src.type = OP_SRC_ADD;
223 op->src.reg = modrm_reg;
224 op->dest.type = OP_DEST_REG;
225 op->dest.reg = CFI_SP;
234 op->src.type = OP_SRC_REG;
235 op->src.reg = (op1 & 0x7) + 8*rex_b;
236 op->dest.type = OP_DEST_PUSH;
245 op->src.type = OP_SRC_POP;
246 op->dest.type = OP_DEST_REG;
247 op->dest.reg = (op1 & 0x7) + 8*rex_b;
256 op->src.type = OP_SRC_CONST;
257 op->dest.type = OP_DEST_PUSH;
262 insn->type = INSN_JUMP_CONDITIONAL;
267 * 1000 00sw : mod OP r/m : immediate
269 * s - sign extend immediate
272 * OP: 000 ADD 100 AND
282 /* %rsp target only */
283 if (!rm_is_reg(CFI_SP))
286 imm = ins.immediate.value;
287 if (op1 & 2) { /* sign extend */
288 if (op1 & 1) { /* imm32 */
290 imm = (s64)imm >> 32;
293 imm = (s64)imm >> 56;
297 switch (modrm_reg & 7) {
302 /* add/sub imm, %rsp */
304 op->src.type = OP_SRC_ADD;
305 op->src.reg = CFI_SP;
306 op->src.offset = imm;
307 op->dest.type = OP_DEST_REG;
308 op->dest.reg = CFI_SP;
315 op->src.type = OP_SRC_AND;
316 op->src.reg = CFI_SP;
317 op->src.offset = ins.immediate.value;
318 op->dest.type = OP_DEST_REG;
319 op->dest.reg = CFI_SP;
334 if (modrm_reg == CFI_SP) {
339 op->src.type = OP_SRC_REG;
340 op->src.reg = CFI_SP;
341 op->dest.type = OP_DEST_REG;
342 op->dest.reg = modrm_rm;
347 /* skip RIP relative displacement */
351 /* skip nontrivial SIB */
354 if (sib_index != CFI_SP)
358 /* mov %rsp, disp(%reg) */
360 op->src.type = OP_SRC_REG;
361 op->src.reg = CFI_SP;
362 op->dest.type = OP_DEST_REG_INDIRECT;
363 op->dest.reg = modrm_rm;
364 op->dest.offset = ins.displacement.value;
372 if (rm_is_reg(CFI_SP)) {
376 op->src.type = OP_SRC_REG;
377 op->src.reg = modrm_reg;
378 op->dest.type = OP_DEST_REG;
379 op->dest.reg = CFI_SP;
389 if (rm_is_mem(CFI_BP)) {
391 /* mov reg, disp(%rbp) */
393 op->src.type = OP_SRC_REG;
394 op->src.reg = modrm_reg;
395 op->dest.type = OP_DEST_REG_INDIRECT;
396 op->dest.reg = CFI_BP;
397 op->dest.offset = ins.displacement.value;
402 if (rm_is_mem(CFI_SP)) {
404 /* mov reg, disp(%rsp) */
406 op->src.type = OP_SRC_REG;
407 op->src.reg = modrm_reg;
408 op->dest.type = OP_DEST_REG_INDIRECT;
409 op->dest.reg = CFI_SP;
410 op->dest.offset = ins.displacement.value;
421 if (rm_is_mem(CFI_BP)) {
423 /* mov disp(%rbp), reg */
425 op->src.type = OP_SRC_REG_INDIRECT;
426 op->src.reg = CFI_BP;
427 op->src.offset = ins.displacement.value;
428 op->dest.type = OP_DEST_REG;
429 op->dest.reg = modrm_reg;
434 if (rm_is_mem(CFI_SP)) {
436 /* mov disp(%rsp), reg */
438 op->src.type = OP_SRC_REG_INDIRECT;
439 op->src.reg = CFI_SP;
440 op->src.offset = ins.displacement.value;
441 op->dest.type = OP_DEST_REG;
442 op->dest.reg = modrm_reg;
451 WARN("invalid LEA encoding at %s:0x%lx", sec->name, offset);
455 /* skip non 64bit ops */
459 /* skip nontrivial SIB */
462 if (sib_index != CFI_SP)
466 /* lea disp(%rip), %dst */
468 insn->type = INSN_LEA_RIP;
472 /* lea disp(%src), %dst */
474 op->src.offset = ins.displacement.value;
475 if (!op->src.offset) {
476 /* lea (%src), %dst */
477 op->src.type = OP_SRC_REG;
479 /* lea disp(%src), %dst */
480 op->src.type = OP_SRC_ADD;
482 op->src.reg = modrm_rm;
483 op->dest.type = OP_DEST_REG;
484 op->dest.reg = modrm_reg;
491 op->src.type = OP_SRC_POP;
492 op->dest.type = OP_DEST_MEM;
497 insn->type = INSN_NOP;
503 op->src.type = OP_SRC_CONST;
504 op->dest.type = OP_DEST_PUSHF;
511 op->src.type = OP_SRC_POPF;
512 op->dest.type = OP_DEST_MEM;
520 switch (insn_last_prefix_id(&ins)) {
525 insn->type = INSN_CONTEXT_SWITCH;
529 insn->type = INSN_CLAC;
530 else if (modrm == 0xcb)
531 insn->type = INSN_STAC;
534 } else if (op2 >= 0x80 && op2 <= 0x8f) {
536 insn->type = INSN_JUMP_CONDITIONAL;
538 } else if (op2 == 0x05 || op2 == 0x07 || op2 == 0x34 ||
541 /* sysenter, sysret */
542 insn->type = INSN_CONTEXT_SWITCH;
544 } else if (op2 == 0x0b || op2 == 0xb9) {
547 insn->type = INSN_BUG;
549 } else if (op2 == 0x0d || op2 == 0x1f) {
552 insn->type = INSN_NOP;
554 } else if (op2 == 0x1e) {
556 if (prefix == 0xf3 && (modrm == 0xfa || modrm == 0xfb))
557 insn->type = INSN_ENDBR;
560 } else if (op2 == 0x38 && op3 == 0xf8) {
561 if (ins.prefixes.nbytes == 1 &&
562 ins.prefixes.bytes[0] == 0xf2) {
563 /* ENQCMD cannot be used in the kernel. */
564 WARN("ENQCMD instruction at %s:%lx", sec->name,
568 } else if (op2 == 0xa0 || op2 == 0xa8) {
572 op->src.type = OP_SRC_CONST;
573 op->dest.type = OP_DEST_PUSH;
576 } else if (op2 == 0xa1 || op2 == 0xa9) {
580 op->src.type = OP_SRC_POP;
581 op->dest.type = OP_DEST_MEM;
596 op->src.type = OP_SRC_REG;
597 op->src.reg = CFI_BP;
598 op->dest.type = OP_DEST_REG;
599 op->dest.reg = CFI_SP;
602 op->src.type = OP_SRC_POP;
603 op->dest.type = OP_DEST_REG;
604 op->dest.reg = CFI_BP;
610 insn->type = INSN_TRAP;
615 insn->type = INSN_JUMP_CONDITIONAL;
620 insn->type = INSN_JUMP_UNCONDITIONAL;
625 insn->type = INSN_RETURN;
628 case 0xc7: /* mov imm, r/m */
632 if (ins.length == 3+4+4 && !strncmp(sec->name, ".init.text", 10)) {
633 struct reloc *immr, *disp;
637 immr = find_reloc_by_dest(elf, (void *)sec, offset+3);
638 disp = find_reloc_by_dest(elf, (void *)sec, offset+7);
640 if (!immr || strcmp(immr->sym->name, "pv_ops"))
643 idx = (reloc_addend(immr) + 8) / sizeof(void *);
646 if (disp->sym->type == STT_SECTION)
647 func = find_symbol_by_offset(disp->sym->sec, reloc_addend(disp));
649 WARN("no func for pv_ops[]");
653 objtool_pv_add(file, idx, func);
658 case 0xcf: /* iret */
660 * Handle sync_core(), which has an IRET to self.
661 * All other IRET are in STT_NONE entry code.
663 sym = find_symbol_containing(sec, offset);
664 if (sym && sym->type == STT_FUNC) {
667 op->src.type = OP_SRC_ADD;
668 op->src.reg = CFI_SP;
669 op->src.offset = 5*8;
670 op->dest.type = OP_DEST_REG;
671 op->dest.reg = CFI_SP;
678 case 0xca: /* retf */
679 case 0xcb: /* retf */
680 insn->type = INSN_CONTEXT_SWITCH;
683 case 0xe0: /* loopne */
684 case 0xe1: /* loope */
685 case 0xe2: /* loop */
686 insn->type = INSN_JUMP_CONDITIONAL;
690 insn->type = INSN_CALL;
692 * For the impact on the stack, a CALL behaves like
693 * a PUSH of an immediate value (the return address).
696 op->src.type = OP_SRC_CONST;
697 op->dest.type = OP_DEST_PUSH;
702 insn->type = INSN_CLD;
706 insn->type = INSN_STD;
710 if (modrm_reg == 2 || modrm_reg == 3) {
712 insn->type = INSN_CALL_DYNAMIC;
713 if (has_notrack_prefix(&ins))
714 WARN("notrack prefix found at %s:0x%lx", sec->name, offset);
716 } else if (modrm_reg == 4) {
718 insn->type = INSN_JUMP_DYNAMIC;
719 if (has_notrack_prefix(&ins))
720 WARN("notrack prefix found at %s:0x%lx", sec->name, offset);
722 } else if (modrm_reg == 5) {
725 insn->type = INSN_CONTEXT_SWITCH;
727 } else if (modrm_reg == 6) {
731 op->src.type = OP_SRC_CONST;
732 op->dest.type = OP_DEST_PUSH;
742 if (ins.immediate.nbytes)
743 insn->immediate = ins.immediate.value;
744 else if (ins.displacement.nbytes)
745 insn->immediate = ins.displacement.value;
750 void arch_initial_func_cfi_state(struct cfi_init_state *state)
754 for (i = 0; i < CFI_NUM_REGS; i++) {
755 state->regs[i].base = CFI_UNDEFINED;
756 state->regs[i].offset = 0;
759 /* initial CFA (call frame address) */
760 state->cfa.base = CFI_SP;
761 state->cfa.offset = 8;
763 /* initial RA (return address) */
764 state->regs[CFI_RA].base = CFI_CFA;
765 state->regs[CFI_RA].offset = -8;
768 const char *arch_nop_insn(int len)
770 static const char nops[5][5] = {
778 if (len < 1 || len > 5) {
779 WARN("invalid NOP size: %d\n", len);
786 #define BYTE_RET 0xC3
788 const char *arch_ret_insn(int len)
790 static const char ret[5][5] = {
793 { BYTE_RET, 0xcc, BYTES_NOP1 },
794 { BYTE_RET, 0xcc, BYTES_NOP2 },
795 { BYTE_RET, 0xcc, BYTES_NOP3 },
798 if (len < 1 || len > 5) {
799 WARN("invalid RET size: %d\n", len);
806 int arch_decode_hint_reg(u8 sp_reg, int *base)
809 case ORC_REG_UNDEFINED:
810 *base = CFI_UNDEFINED;
818 case ORC_REG_SP_INDIRECT:
819 *base = CFI_SP_INDIRECT;
840 bool arch_is_retpoline(struct symbol *sym)
842 return !strncmp(sym->name, "__x86_indirect_", 15);
845 bool arch_is_rethunk(struct symbol *sym)
847 return !strcmp(sym->name, "__x86_return_thunk");
850 bool arch_is_embedded_insn(struct symbol *sym)
852 return !strcmp(sym->name, "retbleed_return_thunk") ||
853 !strcmp(sym->name, "srso_safe_ret");