原始bin转换为coff

时间:2014-07-07 21:24:25

标签: c linker x86 coff

我想知道在这种情况下我是否会获得带有原始内容的.bin文件 一些程序(x86-32)可以用一些工具转换它 (或者如果可能的话)一些可链接的目标文件(如coff) 我可以链接和使用(例如与GCC)

特别是在我看来,当我转换一些时候,我需要一些东西 导出符号名称,不知道导入符号名称是什么, 也不知道是否需要一些重新分配表 使用这种胡子或它是不需要的,可以建立 在转换过程中,我无法理解是什么 继续这个重新定位数据

是否有可能以这种方式转换数据?有人可以解释一下吗?

1 个答案:

答案 0 :(得分:1)

最好的办法是编写一个普通的C程序,将.bin文件的内容读入您已分配为可执行文件的内存中,然后调用它。

这个(完全未经测试的)程序用VirtualAlloc分配读/写/可执行内存。函数指针(func)返回任何内容并且不带参数指向缓冲区的开头,然后调用(就像普通的C函数一样)。

这假定您的.bin以"正常"开始(在偏移0处)功能可以是call' d。

#include <stdio.h>
#include <Windows.h>

int main(int argc, char** argv)
{
    FILE* f;
    int size;

    // A function pointer to your binary blob.
    // Change this prototype, depending on the function you're calling
    void (*func)(void);


    if (argc < 2) exit(1);

    f = fopen(argv[1], "rb");
    if (!f) exit(1);

    // Get file size
    fseek(f, 0, SEEK_END);
    size = ftell(f);
    fseek(f, 0, SEEK_SET);

    // Allocate executable buffer
    buf = VirtualAlloc(NULL,
       size,
       MEM_COMMIT | MEM_RESERVE,
       PAGE_EXECUTE_READWRITE);

    // Read the file into the buffer
    if (fread(buf, 1, size, f) != size)
        exit(1);

    // Point your function pointer to the buffer
    func = (void*)buf;

    // ...and call it
    func();

    return 0;
}
相关问题