Embedding a file into an ELF binary
If you want to embed binary data (for example: images, keys, data) into your ELF executable and then use it from inside your application, this is what you do:NB: This example may crash because I’m printing the string which may or may not have a terminating NULL character. You should see the beginning of the file anyway.
Create your data file:
README.TXT: This is a readme file.
Turn it into an object file:
objcopy -I binary -O elf32-i386 \ --redefine-sym _binary_README_TXT_start=_README \ --redefine-sym _binary_README_TXT_end=_README_END \ --redefine-sym _binary_README_TXT_size=_README_SIZE \ README.TXT README.o
Verify that it is the correct format: (objcopy messes up sometimes)
objdump -t README.o -- README.o: file format elf32-i386
If it comes out as a elf32-little, you need to change the byte at 0×12 to a 3:
echo -e "\3" | dd seek=18 bs=1 count=1 conv=notrunc of=./README.o
.. check again
Create your application:
main.cpp
#include <stdio.h>
extern char* _README;
int main(int argc, char *argv[] )
{
printf("%s\n", &_README);
return 0;
}
Compile it all together:
g++ -Xlinker README.o main.cpp -o test
Run it:
./test
all done.