As part of trying to write a compiler completely from scratch, I'm currently working on the part the handles ELF files.
After skimming through several articles and specifications about them, I still don't quite understand where section to segment mappings are stored. When observing small executables generated by NASM+ld, I can see that the .text section is somehow mapped onto a LOAD-type program header, but how?
A small piece of readelf's output when given a small (working) executable as input:
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
0x0000000000000084 0x0000000000000084 R E 200000
Section to Segment mapping:
Segment Sections...
00 .text
Is this mapping even required to have a working executable? Or can they be omitted completely and you would still have a valid executable?
I did a test according to the @Employed Russian.
readelf -l ./libandroid_servers.so
01 LOAD offset: 0x000000 fileSize 0x0f830
.ARM.exidx section end addr: hex(0xF2E8 + 1352) = 0xf830
02 LOAD offset: 0x010000 fileSize: 0x00cf4
.init_array section begin addr: 10000h
.bss section end addr: hex(0x10cf4 + 0 ) = 0x10cf4
You see the
readelf
surely print the sections in a segments by computes. They match well.They are not stored anywhere.
Rather,
readelf
computes the mapping by looking at file offset and size of sections and segments.