【问题标题】:raw bin to coff conversionraw bin 到 coff 转换
【发布时间】:2014-07-08 06:31:29
【问题描述】:

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

特别是在我看来,在转换时我应该加倍 导出符号名称,不知道导入符号名称是什么, 也不知道是否需要一些重新分配表 将它与这样的bonary一起使用,或者不需要它并且可以构建 在转换过程中,我无法理解什么是 继续处理这个重新分配数据

可以这样转换数据吗?有人可以解释一下吗?

【问题讨论】:

  • 另外,你提到了 COFF。如果这个问题是关于 Windows 的,请标记它。

标签: c linker x86 coff


【解决方案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;
}

【讨论】:

  • 但我想做普通的静态链接而不是这样的运行时“链接”——虽然 9think 我应该尝试一下这也很有趣)但它几乎完全不同
  • 另一种方法是将二进制 blob 编码为 C 语法 char 数组。将其转换为函数指针并调用它。诀窍是使用#pragma section 确保它在可执行部分中,否则你会得到一个页面错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
  • 2012-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多