Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 820
BizarreCake Avatar asked Apr 11 '14 17:04

BizarreCake


People also ask

What are segments of ELF file?

Segments, which are commonly known as Program Headers, break down the structure of an ELF binary into suitable chunks to prepare the executable to be loaded into memory. In contrast with Section Headers, Program Headers are not needed on linktime.

What is section and segment?

The segments contain information that is necessary for runtime execution of the file, while sections contain important data for linking and relocation. Any byte in the entire file can be owned by at most one section, and there can be orphan bytes which are not owned by any section.

What is an ELF header and what does it do?

The ELF header is always located at the beginning of the ELF file. It describes the ELF file organization and contains the actual sizes of the object file control structures. The initial bytes of an ELF header specify how the file is to be interpreted.


1 Answers

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.

like image 88
Employed Russian Avatar answered Sep 24 '22 11:09

Employed Russian