2 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #define unlikely(cond) (cond)
22 #include "insn/insn.h"
23 #include "insn/inat.c"
24 #include "insn/insn.c"
26 #include "../../elf.h"
27 #include "../../arch.h"
28 #include "../../warn.h"
30 static unsigned char op_to_cfi_reg[][2] = {
41 static int is_x86_64(struct elf *elf)
43 switch (elf->ehdr.e_machine) {
49 WARN("unexpected ELF machine type %d", elf->ehdr.e_machine);
54 bool arch_callee_saved_reg(unsigned char reg)
81 int arch_decode_instruction(struct elf *elf, struct section *sec,
82 unsigned long offset, unsigned int maxlen,
83 unsigned int *len, unsigned char *type,
84 unsigned long *immediate, struct stack_op *op)
88 unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
89 modrm = 0, modrm_mod = 0, modrm_rm = 0, modrm_reg = 0,
92 x86_64 = is_x86_64(elf);
96 insn_init(&insn, sec->data->d_buf + offset, maxlen, x86_64);
97 insn_get_length(&insn);
99 if (!insn_complete(&insn)) {
100 WARN_FUNC("can't decode instruction", sec, offset);
107 if (insn.vex_prefix.nbytes)
110 op1 = insn.opcode.bytes[0];
111 op2 = insn.opcode.bytes[1];
113 if (insn.rex_prefix.nbytes) {
114 rex = insn.rex_prefix.bytes[0];
115 rex_w = X86_REX_W(rex) >> 3;
116 rex_r = X86_REX_R(rex) >> 2;
117 rex_b = X86_REX_B(rex);
120 if (insn.modrm.nbytes) {
121 modrm = insn.modrm.bytes[0];
122 modrm_mod = X86_MODRM_MOD(modrm);
123 modrm_reg = X86_MODRM_REG(modrm);
124 modrm_rm = X86_MODRM_RM(modrm);
128 sib = insn.sib.bytes[0];
134 if (rex_w && !rex_b && modrm_mod == 3 && modrm_rm == 4) {
136 /* add/sub reg, %rsp */
138 op->src.type = OP_SRC_ADD;
139 op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
140 op->dest.type = OP_SRC_REG;
141 op->dest.reg = CFI_SP;
149 op->src.type = OP_SRC_REG;
150 op->src.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
151 op->dest.type = OP_DEST_PUSH;
159 op->src.type = OP_SRC_POP;
160 op->dest.type = OP_DEST_REG;
161 op->dest.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
169 op->src.type = OP_SRC_CONST;
170 op->dest.type = OP_DEST_PUSH;
174 *type = INSN_JUMP_CONDITIONAL;
185 op->src.type = OP_SRC_AND;
186 op->src.reg = CFI_SP;
187 op->src.offset = insn.immediate.value;
188 op->dest.type = OP_DEST_REG;
189 op->dest.reg = CFI_SP;
195 else if (modrm == 0xec)
200 /* add/sub imm, %rsp */
202 op->src.type = OP_SRC_ADD;
203 op->src.reg = CFI_SP;
204 op->src.offset = insn.immediate.value * sign;
205 op->dest.type = OP_DEST_REG;
206 op->dest.reg = CFI_SP;
210 if (rex == 0x48 && modrm == 0xe5) {
214 op->src.type = OP_SRC_REG;
215 op->src.reg = CFI_SP;
216 op->dest.type = OP_DEST_REG;
217 op->dest.reg = CFI_BP;
223 (modrm_mod == 1 || modrm_mod == 2) && modrm_rm == 5) {
225 /* mov reg, disp(%rbp) */
227 op->src.type = OP_SRC_REG;
228 op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
229 op->dest.type = OP_DEST_REG_INDIRECT;
230 op->dest.reg = CFI_BP;
231 op->dest.offset = insn.displacement.value;
233 } else if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
235 /* mov reg, disp(%rsp) */
237 op->src.type = OP_SRC_REG;
238 op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
239 op->dest.type = OP_DEST_REG_INDIRECT;
240 op->dest.reg = CFI_SP;
241 op->dest.offset = insn.displacement.value;
247 if (rex_w && !rex_b && modrm_mod == 1 && modrm_rm == 5) {
249 /* mov disp(%rbp), reg */
251 op->src.type = OP_SRC_REG_INDIRECT;
252 op->src.reg = CFI_BP;
253 op->src.offset = insn.displacement.value;
254 op->dest.type = OP_DEST_REG;
255 op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
257 } else if (rex_w && !rex_b && sib == 0x24 &&
258 modrm_mod != 3 && modrm_rm == 4) {
260 /* mov disp(%rsp), reg */
262 op->src.type = OP_SRC_REG_INDIRECT;
263 op->src.reg = CFI_SP;
264 op->src.offset = insn.displacement.value;
265 op->dest.type = OP_DEST_REG;
266 op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
272 if (rex == 0x48 && modrm == 0x65) {
274 /* lea disp(%rbp), %rsp */
276 op->src.type = OP_SRC_ADD;
277 op->src.reg = CFI_BP;
278 op->src.offset = insn.displacement.value;
279 op->dest.type = OP_DEST_REG;
280 op->dest.reg = CFI_SP;
284 if (rex == 0x48 && (modrm == 0xa4 || modrm == 0x64) &&
287 /* lea disp(%rsp), %rsp */
289 op->src.type = OP_SRC_ADD;
290 op->src.reg = CFI_SP;
291 op->src.offset = insn.displacement.value;
292 op->dest.type = OP_DEST_REG;
293 op->dest.reg = CFI_SP;
297 if (rex == 0x48 && modrm == 0x2c && sib == 0x24) {
299 /* lea (%rsp), %rbp */
301 op->src.type = OP_SRC_REG;
302 op->src.reg = CFI_SP;
303 op->dest.type = OP_DEST_REG;
304 op->dest.reg = CFI_BP;
308 if (rex == 0x4c && modrm == 0x54 && sib == 0x24 &&
309 insn.displacement.value == 8) {
312 * lea 0x8(%rsp), %r10
314 * Here r10 is the "drap" pointer, used as a stack
315 * pointer helper when the stack gets realigned.
318 op->src.type = OP_SRC_ADD;
319 op->src.reg = CFI_SP;
321 op->dest.type = OP_DEST_REG;
322 op->dest.reg = CFI_R10;
326 if (rex == 0x4c && modrm == 0x6c && sib == 0x24 &&
327 insn.displacement.value == 16) {
330 * lea 0x10(%rsp), %r13
332 * Here r13 is the "drap" pointer, used as a stack
333 * pointer helper when the stack gets realigned.
336 op->src.type = OP_SRC_ADD;
337 op->src.reg = CFI_SP;
339 op->dest.type = OP_DEST_REG;
340 op->dest.reg = CFI_R13;
344 if (rex == 0x49 && modrm == 0x62 &&
345 insn.displacement.value == -8) {
348 * lea -0x8(%r10), %rsp
350 * Restoring rsp back to its original value after a
354 op->src.type = OP_SRC_ADD;
355 op->src.reg = CFI_R10;
357 op->dest.type = OP_DEST_REG;
358 op->dest.reg = CFI_SP;
362 if (rex == 0x49 && modrm == 0x65 &&
363 insn.displacement.value == -16) {
366 * lea -0x10(%r13), %rsp
368 * Restoring rsp back to its original value after a
372 op->src.type = OP_SRC_ADD;
373 op->src.reg = CFI_R13;
374 op->src.offset = -16;
375 op->dest.type = OP_DEST_REG;
376 op->dest.reg = CFI_SP;
385 op->src.type = OP_SRC_POP;
386 op->dest.type = OP_DEST_MEM;
396 op->src.type = OP_SRC_CONST;
397 op->dest.type = OP_DEST_PUSH;
403 op->src.type = OP_SRC_POP;
404 op->dest.type = OP_DEST_MEM;
409 if (op2 >= 0x80 && op2 <= 0x8f)
410 *type = INSN_JUMP_CONDITIONAL;
411 else if (op2 == 0x05 || op2 == 0x07 || op2 == 0x34 ||
414 /* sysenter, sysret */
415 *type = INSN_CONTEXT_SWITCH;
417 else if (op2 == 0x0d || op2 == 0x1f)
422 else if (op2 == 0xa0 || op2 == 0xa8) {
426 op->src.type = OP_SRC_CONST;
427 op->dest.type = OP_DEST_PUSH;
429 } else if (op2 == 0xa1 || op2 == 0xa9) {
433 op->src.type = OP_SRC_POP;
434 op->dest.type = OP_DEST_MEM;
448 op->dest.type = OP_DEST_LEAVE;
454 *type = INSN_JUMP_CONDITIONAL;
459 *type = INSN_JUMP_UNCONDITIONAL;
467 case 0xca: /* retf */
468 case 0xcb: /* retf */
469 case 0xcf: /* iret */
470 *type = INSN_CONTEXT_SWITCH;
478 if (modrm_reg == 2 || modrm_reg == 3)
480 *type = INSN_CALL_DYNAMIC;
482 else if (modrm_reg == 4)
484 *type = INSN_JUMP_DYNAMIC;
486 else if (modrm_reg == 5)
489 *type = INSN_CONTEXT_SWITCH;
491 else if (modrm_reg == 6) {
495 op->src.type = OP_SRC_CONST;
496 op->dest.type = OP_DEST_PUSH;
505 *immediate = insn.immediate.nbytes ? insn.immediate.value : 0;
510 void arch_initial_func_cfi_state(struct cfi_state *state)
514 for (i = 0; i < CFI_NUM_REGS; i++) {
515 state->regs[i].base = CFI_UNDEFINED;
516 state->regs[i].offset = 0;
519 /* initial CFA (call frame address) */
520 state->cfa.base = CFI_SP;
521 state->cfa.offset = 8;
523 /* initial RA (return address) */
524 state->regs[16].base = CFI_CFA;
525 state->regs[16].offset = -8;