Embedding binary drops using gcc mingw

I am trying to insert binary drops in an exe file. I am using mingw gcc.

I am making an object file as follows:

ld -r -b binary -o binary.o input.txt 

Then I look at the output of objdump to get the characters:

 objdump -x binary.o 

And he gives the characters with the name:

 _binary_input_txt_start _binary_input_txt_end _binary_input_txt_size 

Then I will try to access them in my C program:

 #include <stdlib.h> #include <stdio.h> extern char _binary_input_txt_start[]; int main (int argc, char *argv[]) { char *p; p = _binary_input_txt_start; return 0; } 

Then I compile like this:

 gcc -o test.exe test.c binary.o 

But I always get:

 undefined reference to _binary_input_txt_start 

Does anyone know what I'm doing wrong?

+37
c gcc binary mingw
Apr 13 '10 at 4:38
source share
4 answers

In program C, remove the underscore:

 #include <stdlib.h> #include <stdio.h> extern char binary_input_txt_start[]; int main (int argc, char *argv[]) { char *p; p = binary_input_txt_start; return 0; } 

C compilers often (always?) Seem to add an underscore to extern names. I am not quite sure why this is so - I assume that there is some truth in this wikipedia article that

A common practice for C compilers has been to add leading underscores to all external identifiers of the external scope to prevent collisions with contributions from runtime support.

But it seems to me that if underscores were added to all external files, then you do not really separate the namespace. Anyway, this is a question the next day, and the fact is that underscores are really added.

+26
Apr 13 2018-10-10T00:
source share

I tested it on Linux (Ubuntu 10.10).

  • Resouce File:
    input.txt

  • gcc (Ubuntu / Linaro 4.4.4-14ubuntu5) 4.4.5 [generates ELF executable for Linux]
    Generates the _binary__input_txt_start character.
    Accepts the _binary__input_txt_start character (with an underscore).

  • i586-mingw32msvc-gcc (GCC) 4.2.1-sjlj (mingw32-2) [creates a PE executable for Windows]
    Generates the _binary__input_txt_start character.
    Accepts the binary__input_txt_start character (no underscore).

+4
Oct 12 '12 at 23:21
source share

From the ld man page :

- Presenter-Underline

- Non-Lead-Underline

For most purposes, the default prefix character is an underscore and is defined in the description of the target. With this option, you can disable / enable the default underscore prefix.

So

 ld -r -b binary -o binary.o input.txt --leading-underscore 

should be a solution.

+2
Apr 05 '15 at 11:54 on
source share

Apparently, this function is missing in OSX ld, so you have to do it completely differently with the custom gcc flag that they added, and you cannot directly reference the data, but to get the address you need to do some initialization of the runtime.

Thus, it may be more portable to make yourself the assembler source file, which includes the binary file at build time, aa this answer .

0
May 25 '15 at 23:22
source share



All Articles