Adding section to ELF file

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

have a look at ELFsh, which is now part of the ERESI project http://www.eresi-project.org/ They have a lot of documentation and cool uses of their tools.


The following links could be useful:

  • The tutorial "libelf by Example" has a chapter on creating new ELF objects.
  • The elftoolchain project has manual pages describing libelf's API in detail.

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.

Tags:

Linux

C

Elf