objcopy --add-section allows you to add an arbitrary file as a section to the ELF executable. (user help page). However, this is only half the solution, because I have not yet found a way to access this data from within C, except by downloading and analyzing the ELF binary using the ELF library.
Change Additional Information:
If you have a compiled program MyProgram and a resource file MyResource.dat that you want to embed in MyProgram, you can use the objcopy command as follows:
objcopy MyProgram --add-section MyResource=MyResource.dat
Now, if you look at your program using the objdump -x MyProgram command
You will see the MyResource section, which contains the contents of MyResource.dat. The file is now embedded in the executable file.
The trick now is how you access data from your program. My instinct tells me that the bootloader must put the file in memory somewhere, and you can get a pointer to it, but I'm not sure how easy it is. Ideally, I would like to be able to expand my exeutable and dlsym section, but this does not work because its section is not a symbol.
The only alternative I know to access the section from within the program is to use the libelf library or something similar, which is a bit like using a sledgehammer to knock on a nail. You can use it in your application to download yourself as an ELF resource and get partitions. The documentation is sparse, but here is an example
http://em386.blogspot.com/2007/03/quick-libelf-guide.html
I would really like it if someone could listen to an easier way to access data from the -add section.
Edit 2 In my research, I included this question: Embedding binary drops using gcc mingw
Which should work for both gcc and mingw and shows a way to use ld instead of objcopy to add data and access it as a symbol. It looks promising
bdk Mar 29 2018-11-11T00: 00Z
source share