如何获得指令长度

时间:2015-12-09 02:01:20

标签: c

这里是segv时继续的代码,我不明白“6字节”,为什么是6?

static void sigaction_segv(int signal, siginfo_t *si, void *arg) {
    ucontext_t *ctx = (ucontext_t *) arg;
    /* We are on linux x86, the returning IP is stored in RIP (64bit) or EIP (32bit).
       In this example, the length of the offending instruction is 6 bytes.
       So we skip the offender ! */
#if __WORDSIZE == 64
    printf("Caught SIGSEGV, addr %p, RIP 0x%lx\n", si->si_addr, ctx->uc_mcontext.gregs[REG_RIP]);
    ctx->uc_mcontext.gregs[REG_RIP] += 6;
#else
    printf("Caught SIGSEGV, addr %p, EIP 0x%x\n", si->si_addr, ctx->uc_mcontext.gregs[REG_EIP]);
        ctx->uc_mcontext.gregs[REG_EIP] += 6;
#endif
}

完整代码为here

1 个答案:

答案 0 :(得分:1)

*(int *) NULL = 0;

将编译为(清除rax之后):

c7 00 00 00 00 00       ' movl   $0x0,(%rax)

这是6个字节的机器代码。 使用objdump查看代码的汇编。

相关问题