In K, since you just need to load an ELF file and execute it, only the segments
are interesting. So the first thing is to get the program header table and its
size, fortunately, those information are present in the ELF main header. After
getting the program header table, you just have to load every segment marked as
PT_LOAD at the good memory address.
Don’t forget that in K, the data segment and the code segment are separated,
both in memory, with the help of the segmented memory, and in the ELF file. To
determine if an ELF segment is a code one, take a look at
PF_X defined in the
elf.h header. Due to the
sbrk() system call, the data segment should
physically be located above the code segment, so that this segment
could be expanded.
Possible bug causes
- You may have loaded some segments at the wrong place.
- Some segments have a memory occupation greater than their file occupation (think of the .bss section).
- Your filesystem code may be wrong.