Difference between Program header and Section Header in ELF
Asked Answered
E

2

27

Q1 What is the difference between Program header and Section Header in ELF?

Q1.1 What is the difference between segment and a section?

I believe pheaders point to sections only.

Q2. What is the difference between File Header and Program Header?

As per GNU ld linker script, Using Id: The GNU Linker:

PHDRS
{
name type [ FILEHDR ] [ PHDRS ] [ AT ( address ) ]
[ FLAGS ( flags ) ] ;
}

You may use the FILEHDR and PHDRS keywords appear after the program header type to further describe the contents of the segment. The FILEHDR keyword means that the segment should include the ELF file header. The PHDRS keyword means that the segment should include the ELF program headers themselves.

This is a bit confusing.

Everglades answered 30/4, 2014 at 4:48 Comment(1)
Section vs segment: #14361748Saying
S
29

The Executable & Linkable Format wikipage has a nice picture explaining ELF, and the difference between its program header & sections header. See also elf(5)

The [initial] program header is defining segments (in the address space of a process running that ELF executable) projected in virtual memory (the executable point of view) at execve(2) time. The [final] sections header is defining sections (the linkable point of view, for ld(1) etc...). Each section belongs to a segment (and may, or not, be visible -i.e. mapped into memory- at execution time). The ELF file header tells where program header table & section header table are.

Use also objdump(1) and readelf(1) to explore several ELF files (executables, shared objects, linkable objects) existing on your Linux system.

Levine's Linkers & Loaders book has a chapter explaining that in details.

And Drepper's paper How to Write Shared Libraries also has some good explanation.

Scumble answered 30/4, 2014 at 4:50 Comment(3)
But my answer still fits. Read & follow carefully the links.Scumble
Dear Basile, It doesnt say much about Program header. I am looking for detail on usage of PHDRS { name type [ FILEHDR ] [ PHDRS ] [ AT ( address ) ] [ FLAGS ( flags ) ] ; }Everglades
Summary: run time and load time. Non-allocated sections (debug info), BSS sections not in the ELF, but allocated at run-time (NOLOAD). Similar concepts/keywords are VMA and LMA.Mitzvah
B
1

Q1 What is the difference between the Program header and the Section Header in ELF?

  • A program header describes a segment or other information that the system needs to prepare the program for execution.
  • A section is an interface that can represent a lot of things. Look here for details (search for Elf64_Shdr)
  • A section header is inside a segment.

Q1.1 What is the difference between a segment and a section?

  • A segment consists of one or more sections, though this fact is transparent to the program header.

Q2. What is the difference between File Header and Program Header?

  • The ELF file header. This appears at the start of every ELF file (see /usr/include/elf.h). It also has the number of program headers existing in this file.
  • The ELF file always starts with the ELF file header. And it references the program headers. You need at least one program header.
Boxboard answered 26/11, 2022 at 23:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.