Disclamer: I do not have proper developer/programmer education that would probably helped here. Whatever I know is gathered from different sources and put together in the shown form.
I am importing a piece of shellcode(or whatever binary) into a C program by referencing a resulting symbol from running ld -b binary
into my code. The idea is from embedding-binary-blobs-using-gcc-mingw/embedding-resources-in-executable-using-gcc.
The code is as follows:
extern unsigned int _binary_file_size; //Declaring the external symbol
extern char _binary_file_start[]; //Declaring the external symbols
memcpy(exec, _binary_file_start, _binary_file_size)
The problem is that the program does not work and the debugger shows it tries to load a memory address at the memory location of "size".
The content of the memory where the size is
Since I could not find documentation on this symbols, just references in SO I experimented a bit in the blind and tried playing around with making the type of the variable a pointer and de-referencing it when calling. As I don't have a 100% grasp on pointers, double pointers, de-referencing and memory addressing so I just made empirical changes to see the output.
At some point I wanted to see the variable memory from C and not debugger and to my surprise, &_binary_file_size
returned 0x80 (the size of the data). Now I've changed my code to use &_binary_file_size
and it works (but with some type warnings at compilation).
The question is: What is the correct way of using the _binary_file_size symbol?
There are two workarounds: using the address (&) of the variable or calculating the size from subtracting the _start from the _end variables.
It seems in one of the threads from SO that I used for inspiration someone else had issues with this variable.