1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
9 #define unlikely(cond) (cond)
11 #include "../../../arch/x86/lib/inat.c"
12 #include "../../../arch/x86/lib/insn.c"
14 #include "../../check.h"
15 #include "../../elf.h"
16 #include "../../arch.h"
17 #include "../../warn.h"
19 static unsigned char op_to_cfi_reg[][2] = {
30 static int is_x86_64(const struct elf *elf)
32 switch (elf->ehdr.e_machine) {
38 WARN("unexpected ELF machine type %d", elf->ehdr.e_machine);
43 bool arch_callee_saved_reg(unsigned char reg)
70 unsigned long arch_dest_reloc_offset(int addend)
75 unsigned long arch_jump_destination(struct instruction *insn)
77 return insn->offset + insn->len + insn->immediate;
81 if (!(op = calloc(1, sizeof(*op)))) \
83 else for (list_add_tail(&op->list, ops_list); op; op = NULL)
85 int arch_decode_instruction(const struct elf *elf, const struct section *sec,
86 unsigned long offset, unsigned int maxlen,
87 unsigned int *len, enum insn_type *type,
88 unsigned long *immediate,
89 struct list_head *ops_list)
93 unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
94 rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0,
95 modrm_reg = 0, sib = 0;
96 struct stack_op *op = NULL;
99 x86_64 = is_x86_64(elf);
103 insn_init(&insn, sec->data->d_buf + offset, maxlen, x86_64);
104 insn_get_length(&insn);
106 if (!insn_complete(&insn)) {
107 WARN("can't decode instruction at %s:0x%lx", sec->name, offset);
114 if (insn.vex_prefix.nbytes)
117 op1 = insn.opcode.bytes[0];
118 op2 = insn.opcode.bytes[1];
120 if (insn.rex_prefix.nbytes) {
121 rex = insn.rex_prefix.bytes[0];
122 rex_w = X86_REX_W(rex) >> 3;
123 rex_r = X86_REX_R(rex) >> 2;
124 rex_x = X86_REX_X(rex) >> 1;
125 rex_b = X86_REX_B(rex);
128 if (insn.modrm.nbytes) {
129 modrm = insn.modrm.bytes[0];
130 modrm_mod = X86_MODRM_MOD(modrm);
131 modrm_reg = X86_MODRM_REG(modrm);
132 modrm_rm = X86_MODRM_RM(modrm);
136 sib = insn.sib.bytes[0];
142 if (rex_w && !rex_b && modrm_mod == 3 && modrm_rm == 4) {
144 /* add/sub reg, %rsp */
146 op->src.type = OP_SRC_ADD;
147 op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
148 op->dest.type = OP_DEST_REG;
149 op->dest.reg = CFI_SP;
158 op->src.type = OP_SRC_REG;
159 op->src.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
160 op->dest.type = OP_DEST_PUSH;
169 op->src.type = OP_SRC_POP;
170 op->dest.type = OP_DEST_REG;
171 op->dest.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
180 op->src.type = OP_SRC_CONST;
181 op->dest.type = OP_DEST_PUSH;
186 *type = INSN_JUMP_CONDITIONAL;
197 op->src.type = OP_SRC_AND;
198 op->src.reg = CFI_SP;
199 op->src.offset = insn.immediate.value;
200 op->dest.type = OP_DEST_REG;
201 op->dest.reg = CFI_SP;
208 else if (modrm == 0xec)
213 /* add/sub imm, %rsp */
215 op->src.type = OP_SRC_ADD;
216 op->src.reg = CFI_SP;
217 op->src.offset = insn.immediate.value * sign;
218 op->dest.type = OP_DEST_REG;
219 op->dest.reg = CFI_SP;
224 if (rex_w && !rex_r && modrm_mod == 3 && modrm_reg == 4) {
228 op->src.type = OP_SRC_REG;
229 op->src.reg = CFI_SP;
230 op->dest.type = OP_DEST_REG;
231 op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
236 if (rex_w && !rex_b && modrm_mod == 3 && modrm_rm == 4) {
240 op->src.type = OP_SRC_REG;
241 op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
242 op->dest.type = OP_DEST_REG;
243 op->dest.reg = CFI_SP;
251 (modrm_mod == 1 || modrm_mod == 2) && modrm_rm == 5) {
253 /* mov reg, disp(%rbp) */
255 op->src.type = OP_SRC_REG;
256 op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
257 op->dest.type = OP_DEST_REG_INDIRECT;
258 op->dest.reg = CFI_BP;
259 op->dest.offset = insn.displacement.value;
262 } else if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
264 /* mov reg, disp(%rsp) */
266 op->src.type = OP_SRC_REG;
267 op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
268 op->dest.type = OP_DEST_REG_INDIRECT;
269 op->dest.reg = CFI_SP;
270 op->dest.offset = insn.displacement.value;
277 if (rex_w && !rex_b && modrm_mod == 1 && modrm_rm == 5) {
279 /* mov disp(%rbp), reg */
281 op->src.type = OP_SRC_REG_INDIRECT;
282 op->src.reg = CFI_BP;
283 op->src.offset = insn.displacement.value;
284 op->dest.type = OP_DEST_REG;
285 op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
288 } else if (rex_w && !rex_b && sib == 0x24 &&
289 modrm_mod != 3 && modrm_rm == 4) {
291 /* mov disp(%rsp), reg */
293 op->src.type = OP_SRC_REG_INDIRECT;
294 op->src.reg = CFI_SP;
295 op->src.offset = insn.displacement.value;
296 op->dest.type = OP_DEST_REG;
297 op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
304 if (sib == 0x24 && rex_w && !rex_b && !rex_x) {
307 if (!insn.displacement.value) {
308 /* lea (%rsp), reg */
309 op->src.type = OP_SRC_REG;
311 /* lea disp(%rsp), reg */
312 op->src.type = OP_SRC_ADD;
313 op->src.offset = insn.displacement.value;
315 op->src.reg = CFI_SP;
316 op->dest.type = OP_DEST_REG;
317 op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
320 } else if (rex == 0x48 && modrm == 0x65) {
322 /* lea disp(%rbp), %rsp */
324 op->src.type = OP_SRC_ADD;
325 op->src.reg = CFI_BP;
326 op->src.offset = insn.displacement.value;
327 op->dest.type = OP_DEST_REG;
328 op->dest.reg = CFI_SP;
331 } else if (rex == 0x49 && modrm == 0x62 &&
332 insn.displacement.value == -8) {
335 * lea -0x8(%r10), %rsp
337 * Restoring rsp back to its original value after a
341 op->src.type = OP_SRC_ADD;
342 op->src.reg = CFI_R10;
344 op->dest.type = OP_DEST_REG;
345 op->dest.reg = CFI_SP;
348 } else if (rex == 0x49 && modrm == 0x65 &&
349 insn.displacement.value == -16) {
352 * lea -0x10(%r13), %rsp
354 * Restoring rsp back to its original value after a
358 op->src.type = OP_SRC_ADD;
359 op->src.reg = CFI_R13;
360 op->src.offset = -16;
361 op->dest.type = OP_DEST_REG;
362 op->dest.reg = CFI_SP;
371 op->src.type = OP_SRC_POP;
372 op->dest.type = OP_DEST_MEM;
383 op->src.type = OP_SRC_CONST;
384 op->dest.type = OP_DEST_PUSHF;
391 op->src.type = OP_SRC_POPF;
392 op->dest.type = OP_DEST_MEM;
402 else if (modrm == 0xcb)
405 } else if (op2 >= 0x80 && op2 <= 0x8f) {
407 *type = INSN_JUMP_CONDITIONAL;
409 } else if (op2 == 0x05 || op2 == 0x07 || op2 == 0x34 ||
412 /* sysenter, sysret */
413 *type = INSN_CONTEXT_SWITCH;
415 } else if (op2 == 0x0b || op2 == 0xb9) {
420 } else if (op2 == 0x0d || op2 == 0x1f) {
425 } else if (op2 == 0xa0 || op2 == 0xa8) {
429 op->src.type = OP_SRC_CONST;
430 op->dest.type = OP_DEST_PUSH;
433 } else if (op2 == 0xa1 || op2 == 0xa9) {
437 op->src.type = OP_SRC_POP;
438 op->dest.type = OP_DEST_MEM;
453 op->dest.type = OP_DEST_LEAVE;
459 *type = INSN_JUMP_CONDITIONAL;
464 *type = INSN_JUMP_UNCONDITIONAL;
472 case 0xcf: /* iret */
474 * Handle sync_core(), which has an IRET to self.
475 * All other IRET are in STT_NONE entry code.
477 sym = find_symbol_containing(sec, offset);
478 if (sym && sym->type == STT_FUNC) {
481 op->src.type = OP_SRC_ADD;
482 op->src.reg = CFI_SP;
483 op->src.offset = 5*8;
484 op->dest.type = OP_DEST_REG;
485 op->dest.reg = CFI_SP;
492 case 0xca: /* retf */
493 case 0xcb: /* retf */
494 *type = INSN_CONTEXT_SWITCH;
500 * For the impact on the stack, a CALL behaves like
501 * a PUSH of an immediate value (the return address).
504 op->src.type = OP_SRC_CONST;
505 op->dest.type = OP_DEST_PUSH;
518 if (modrm_reg == 2 || modrm_reg == 3)
520 *type = INSN_CALL_DYNAMIC;
522 else if (modrm_reg == 4)
524 *type = INSN_JUMP_DYNAMIC;
526 else if (modrm_reg == 5)
529 *type = INSN_CONTEXT_SWITCH;
531 else if (modrm_reg == 6) {
535 op->src.type = OP_SRC_CONST;
536 op->dest.type = OP_DEST_PUSH;
546 *immediate = insn.immediate.nbytes ? insn.immediate.value : 0;
551 void arch_initial_func_cfi_state(struct cfi_init_state *state)
555 for (i = 0; i < CFI_NUM_REGS; i++) {
556 state->regs[i].base = CFI_UNDEFINED;
557 state->regs[i].offset = 0;
560 /* initial CFA (call frame address) */
561 state->cfa.base = CFI_SP;
562 state->cfa.offset = 8;
564 /* initial RA (return address) */
565 state->regs[16].base = CFI_CFA;
566 state->regs[16].offset = -8;
569 const char *arch_nop_insn(int len)
571 static const char nops[5][5] = {
573 /* 2 */ { 0x66, 0x90 },
574 /* 3 */ { 0x0f, 0x1f, 0x00 },
575 /* 4 */ { 0x0f, 0x1f, 0x40, 0x00 },
576 /* 5 */ { 0x0f, 0x1f, 0x44, 0x00, 0x00 },
579 if (len < 1 || len > 5) {
580 WARN("invalid NOP size: %d\n", len);