0

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?

user2214913
  • 1,441
  • 2
  • 19
  • 29

1 Answers1

1

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;
}
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • but i want to do normal static linking not such runtime 'linking' - This is also interesting though 9think i shoukd try it) but its almost quite a different thing – user2214913 Jul 07 '14 at 23:42
  • An alternative would be to encode your binary blob as a C-syntax char array. Cast it to a a function pointer and call it. The trick there is to use `#pragma section` to make sure it is in an executable section, otherwise you'll get a pagefault. – Jonathon Reinhart Jul 09 '14 at 19:26