Where is the "Section to segment mapping" stored in ELF files?
Asked Answered
B

2

21

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?

Bevon answered 11/4, 2014 at 17:1 Comment(0)
U
31

I still don't quite understand where section to segment mappings are stored.

They are not stored anywhere.

Rather, readelf computes the mapping by looking at file offset and size of sections and segments.

Undue answered 12/4, 2014 at 16:5 Comment(7)
File offsets, that's it! I thought it had something to do with equal virtual memory addresses. Thanks, it works.Bevon
Do you mean: if the executable contains the optional section header table, then that table is made to point to the middle of segments that contain the sections is that it?Effable
@CiroSantilli巴拿馬文件六四事件法轮功 No, I didn't. The question is about where the "Section to Segment mapping" in readelf output is stored, and the answer is that it's computed on-demand by readelf itself, and is not stored anywhere.Undue
Humm, then I don't understand how it can be computed on demand (other than what me previous comment said, which is "on demand", but requires the optional section header). I mean, the the option section header is not present, then it can't be determined right?Effable
@CiroSantilli巴拿馬文件六四事件法轮功 If the section header is not present, then readelf will not produce the "Section to Sergment mapping" output.Undue
how do you figure out if you have the section headers? is that with readelf -S ?If that gives an output then you have a table of section headers? In the comments it is given as "The" optional section header which implies only one of them are present. Do you mean the array of section headers?Feriga
@Feriga U can find a sections with readelf --sections --wide a.out, note that section headers are optional and may not be present at all.Bouldin
E
4

I did a test according to the @Employed Russian.

readelf -l ./libandroid_servers.so

Elf file type is DYN (Shared object file)
Entry point 0x0
There are 6 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x00000034 0x00000034 0x000c0 0x000c0 R   0x4
  LOAD           0x000000 0x00000000 0x00000000 0x0f830 0x0f830 R E 0x1000
  LOAD           0x010000 0x00010000 0x00010000 0x00cf4 0x011ac RW  0x1000
  DYNAMIC        0x010540 0x00010540 0x00010540 0x00130 0x00130 RW  0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0
  EXIDX          0x00f2e8 0x0000f2e8 0x0000f2e8 0x00548 0x00548 R   0x4

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .hash .dynsym .dynstr .rel.plt .rel.dyn .plt .text .rodata .ARM.extab .ARM.exidx 
   02     .init_array .fini_array .data.rel.ro .dynamic .got .data .bss 
   03     .dynamic 
   04     
   05     .ARM.exidx 

01 LOAD offset: 0x000000 fileSize 0x0f830
.ARM.exidx section end addr: hex(0x00f2e8 + 0x00548) = 0xf830

02 LOAD offset: 0x010000 fileSize: 0x00cf4
.init_array section begin addr: 10000h
.bss section end addr: hex(0x00f2e8 + 0 ) = 0x10cf4

You see the readelf surely print the sections in a segments by computes. They match well.

Extremity answered 12/9, 2018 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.