Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding section to ELF file

Tags:

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.

like image 692
c4757p Avatar asked Jul 06 '09 17:07

c4757p


People also ask

What is in .text section of ELF file?

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.

What is the extension of ELF file?

An ELF file is an executable file format for the Linux and Unix platforms. Its known file extensions are . axf, . bin, .

How is an ELF file loaded?

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.


2 Answers

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
like image 200
Ben Avatar answered Sep 17 '22 21:09

Ben


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.

like image 40
Mark Rushakoff Avatar answered Sep 18 '22 21:09

Mark Rushakoff