【问题标题】:Producing x86 assembly code from a binary executable/C array从二进制可执行文件/C 数组生成 x86 汇编代码
【发布时间】:2011-02-17 04:20:14
【问题描述】:

我有一个包含二进制 x86_64 机器代码的 C 数组,我需要在 Linux 机器上将其转换为汇编代码。我将 C 数组转换为二进制可执行文件没有问题,但找不到将其转换为 asm 代码的实用程序。

有什么想法吗?

谢谢!

【问题讨论】:

标签: assembly x86-64


【解决方案1】:

Linux 上的标准反汇编器是一个名为objdump 的工具,有一个非常简单的用例:

假设我们有一个简单的hello world ANSI C 程序。

#include <stdio.h>

int main() {

   printf("Hello world!\n");
   return 0;

}

在我名为hello.c 的文件中,上面的代码是我使用的。当我使用gcc 编译并获得我的hello 可执行文件时,我们将使用objdump 进行快速转储转换。使用objdump -D hello 时,我们得到以下输出:

objdump output

objdump 非常适合快速反汇编 C 二进制文件的可执行部分。

【讨论】:

    【解决方案2】:

    我更喜欢 libdisasm http://bastard.sourceforge.net/libdisasm.html, 但你总是可以调用 objdump -D。

    从我的抖动:

    #ifdef HAVE_LIBDISASM
    # define LINE_SIZE 255`
    char line[LINE_SIZE];
    int pos = 0;
    int insnsize;            /* size of instruction */
    x86_insn_t insn;         /* one instruction */
    
    x86_init(opt_none, NULL, NULL);
    while ( pos < size ) {
        insnsize = x86_disasm(code, size, 0, pos, &insn);
        if ( insnsize ) {
        x86_format_insn(&insn, line, LINE_SIZE, att_syntax);
        printf("(code+%3x): ", pos);
        for ( i = 0; i < 10; i++ ) {
            if ( i < insn.size ) printf(" %02x", insn.bytes[i]);
            else printf("   ");
        }
        printf("%s\n", line);
        pos += insnsize;
        } else {
        printf("Invalid instruction at 0x%x. size=0x%x\n", pos, size);
        pos++;
        }
    }
    x86_cleanup();
    #else
    fh = fopen("run-jit.bin", "w");
        fwrite(code,size,1,fh);
        fclose(fh);
        system("objdump -D --target=binary --architecture i386"
    #ifdef JIT_CPU_AMD64
               ":x86-64"
    #endif
               " run-jit.bin");
    #endif
    

    【讨论】:

      【解决方案3】:

      如果您负担得起链接到 GPL 代码的费用,则可以链接到 libbfd 并让它为您反汇编数组。如果您负担不起产生一个新进程来执行此操作,请执行此操作。

      【讨论】:

        【解决方案4】:

        您可能正在寻找反汇编程序。反汇编程序获取机器代码(来自可执行文件或目标文件)并将其转换回人类可读的汇编语言。如果这是您需要的,请查看this list of disassemblers for Linux


        这里没有列出的一些反汇编程序:

        • IDA Pro:显然是最强大的反汇编程序之一。出于您的目的,可能有点矫枉过正。

        • ndisasm(伴随 nasm 汇编器)

        【讨论】:

          【解决方案5】:

          你需要一个反汇编程序。我个人使用 NASM 包中的 NDISASM。

          【讨论】:

            猜你喜欢
            • 2011-09-05
            • 2012-03-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多