What parts of ELF file should be loaded in to the memory?


March 2019


691 time


I am writing a c program that gets an elf file, loads it and then executes it. I have read all of the headers (elf, section, program, relocation). The next part should be getting what is going to be loaded, right?

How do I proceed? As I understand I should either get LOAD, ALLOC and EXECINST type sections or LOAD type program segments? Or what?

When I get the needed data I should map it into the memory with mmap().

I've read ELF documentation many times now, but I still don't know how to proceed. Any and all help is greatly appreciated.

2 answers


This is done by operating system itself. Just need to execute the binary. For more details refer the source code of the dynamic linker module of Linux.


Only the PT_LOAD segments need to be loaded. However, loading them for the main program, which might need to be loaded at a fixed address, is complicated by the fact that your loader might already be using part or all of that address range for something else. You can cheat and give your loader a different base address from the "typical" main program addresses and hope it works, or you can make a relocatable loader that relocates itself to a different address.