简体   繁体   中英

raw bin to coff conversion

I wonder if in such case i get a .bin file with the raw contents of some procedure (x86-32) is it possible to convert this with some tool (or if it is at all possible) to some linkable object file (like coff) which i can link and use (for example with GCC)

Speciffically it seem to me that when converting i shoulf gabe some export symbol name, dont know what with import symbol names, also do not know if some reallocation table would be needed to use this with such bonary or it is not needed and can be build at the process of conversion, i dont manage to understand what is going on with this realocation data

is it possible to convert data such way? could someone explain this?

Your best bet would be to write a normal C program that reads the contents of the .bin file, into memory that you've allocated as executable, and call it.

This (completely untested) program allocates read/write/execute -able memory with VirtualAlloc . A function pointer ( func ) that returns nothing and takes no parameters is pointed to the beginning of the buffer, and then called (just like a normal C function).

This assumes that your .bin starts (at offset 0) with a "normal" function that can be 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM