Packing the file into an ELF executable

I'm currently looking for a way to add data to an already compiled ELF executable, that is, embedding a file in an executable without recompiling it.

I could easily do this with cat myexe mydata > myexe_with_mydata , but I could not access the data from the executable file because I do not know the size of the original executable file.

Does anyone have any idea how I can implement this? I thought about adding a section to the executable or using a special marker (e.g. 0xBADBEEFC0FFEE ) to detect the beginning of the data in the executable, but I do not know if there is a more beautiful way to do this.

Thanks in advance.

+7
binary file-format elf embed
source share
2 answers

You can add the file to the elf file as a special section with objcopy (1) :

 objcopy --addsection sname=file oldelf newelf 

will add the file to oldelf and write the results to newelf (oldelf will not be changed) Then you can use libbfd to read the elf file and extract the section by name, or just collapse your own code that reads the partition table and finds the section. Be sure to use a section name that does not come up against anything expected by the system - until your name begins with . You should be fine.

+5
source share

I created a small elfdataembed library that provides a simple interface for extracting / linking sections built in with objcopy . This allows you to transfer the offset / size to another tool or refer to it directly from the runtime using file descriptors. Hope this helps someone in the future.

It is worth noting that this approach is more efficient than compiling for a symbol, since it allows external tools to reference data without the need to extract it, and also does not require the entire binary file to be loaded into memory in order to extract / reference it.

+1
source share

All Articles