I need to be able to add an arbitrary section to an ELF file. I cannot use GPL code in this program, so BFD is out of the question. I can use libelf/gelf to read sections, but the documentation is fairly sparse for these, and I cannot figure out how to add a section. Does anybody know how to do this? I would rather not write my own ELF code.
An ELF file is divided into sections. For an executable program, these are the text section for the code, the data section for global variables and the rodata section that usually contains constant strings. The ELF file contains headers that describe how these sections should be stored in memory.
An ELF file is an executable file format for the Linux and Unix platforms. Its known file extensions are . axf, . bin, .
ELF files are used by two tools: the linker and the loader. A linker combines multiple ELF files into an executable or a library and a loader loads the executable ELF file in the memory of the process.
I know this is an old question but i found a working example that helped me to apply it to my project. (In case anyone stumbles upon this question)
taken from Sourceware Mail Archiv
$ echo 'int main() { puts ("Hello world"); }' | gcc -x c - -c -o hello.o
$ echo "this is my special data" >mydata
$ objcopy --add-section .mydata=mydata \
--set-section-flags .mydata=noload,readonly hello.o hello2.o
$ gcc hello2.o -o hello
$ ./hello
Hello world
$ objdump -sj .mydata hello
There's a few (possibly) related answers in this question about ELF file headers. The accepted answer mentioned using objcopy
to add sections to an ELF file, and the BSD bintools claims to have a BSD-licensed implementation of objcopy
that might suit your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With