Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding binary blobs using gcc mingw

Tags:

c

gcc

binary

mingw

I am trying to embed binary blobs into an exe file. I am using mingw gcc.

I make the object file like this:

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

I then look objdump output to get the symbols:

objdump -x binary.o 

And it gives symbols named:

_binary_input_txt_start _binary_input_txt_end _binary_input_txt_size 

I then try and 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 am doing wrong?

like image 566
myforwik Avatar asked Apr 13 '10 04:04

myforwik


2 Answers

In your C program remove the leading 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 prepend an underscore to extern names. I'm not entirely sure why that is - I assume that there's some truth to this wikipedia article's claim that

It was common practice for C compilers to prepend a leading underscore to all external scope program identifiers to avert clashes with contributions from runtime language support

But it strikes me that if underscores were prepended to all externs, then you're not really partitioning the namespace very much. Anyway, that's a question for another day, and the fact is that the underscores do get added.

like image 118
Michael Burr Avatar answered Sep 21 '22 23:09

Michael Burr


From ld man page:

--leading-underscore

--no-leading-underscore

For most targets default symbol-prefix is an underscore and is defined in target's description. By this option it is possible to disable/enable the default underscore symbol-prefix.

so

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

should be solution.

like image 44
Matěj Pokorný Avatar answered Sep 19 '22 23:09

Matěj Pokorný