Page 1 of 1
How to create a section in ELF?
Posted: Fri Mar 14, 2014 11:44 am
by ASMMan
I'm reading about ELF file format and trying to create one without any library, to really understand how the things works--I'm looking for actual code examples using it. In fact, I was able to create one by setting all needed headers and put some op code to it print a string on std out. But how to create the string table and then the sections, to be honest, I have no code to show because I didn't understand even how to create the string table myself and set it to sh_name. I'd love any code example how to do this. I've search a lot on internet without luck. Even
libelf by example doesn't show how to do it directly the explanation actually I get it's same as from standard elf document.
Re: How to create a section in ELF?
Posted: Fri Mar 14, 2014 6:20 pm
by Pancakes
This might help explain it better for you,
http://stackoverflow.com/questions/1086 ... r-elf-file.
It has been so long since I messed with the ELF internals that I can not walk you through it, but I bet that link will help you understand.
The section is .shstrtab and it, IIRC, is pretty much a heap or zero terminated strings.
Re: How to create a section in ELF?
Posted: Sat Mar 15, 2014 9:36 am
by sortie
Hi ASMMan,
Usually you just use an existing assembler to assemble ELF object files and a linker to generate the final loadable ELF object. There is a great deal of complexity involved depending on how many ELF features you want. If you are creating an object file that isn't linked, the you need to do extra work so the linker can work on your custom object files. I suppose things are easier if you are just creating executables directly. Could you tell us more about what you are doing? It helps to understand your use case.
You want to understand the ELF format in depth. You should consult the System V ABI core document, plus the System V supplement for i386 (or x86_64 depending on your platform). This specification tells you everything about the ELF format (except vendor-specific extensions later added) that you'd need to know. I normally read the document from the program loader's point of view, rather than the toolchain's, so I don't know what difficulties are involved in creating ELF executables. There are some alignment constraints as well as the issue of the section names being in the string table that is another section as well. Surely you should be able to resolve such design constraints yourself.
You could perhaps use libbfd from binutils to help emitting ELF objects, but then you are depending on binutils and you might as well just use GNU as. Perhaps you can get useful information by examining backends of other assemblers like nasm, though I'd recommend you use the specification and figure it yourself.
Perhaps a good exercise would be to write a program that dumps information the ELF object to the standard output? This would mean you get to know the data structures. Once you understand the data structure, it shouldn't be too hard to modify it and potentially create an instance entirely from scratch.