Object Files/Executables: What's the difference between a segment and a section?
Asked Answered
N

1

6

I am confused at to whether there is a difference between "segment" and "section" when referring to object files/executables.

According to https://en.wikipedia.org/wiki/Object_file:

Most object file formats are structured as separate sections of data, each section containing a certain type of data.

However, the article later goes on to talking about segments (e.g. code segment, data segment, etc).

Additionally, the PE file format (.exe/.dll/.coff in Windows) refers to these different parts as sections (https://msdn.microsoft.com/en-us/library/windows/desktop/ms680547(v=vs.85).aspx).

So my question: Is there a difference between the two or are they practically synonyms?

Nathan answered 8/4, 2018 at 13:52 Comment(2)
"[The PE file format] refers to these different parts as segments" - I think you might have written "segments" when you meant "sections". The PE/COFF standard only deals with sections.Liturgics
Yes, my bad. Edited.Nathan
L
4

The terminology may depend on the specific object file format, but typically a section is a more fine-grained "chunk" of code or data than a segment, in the sense that a segment might consist of multiple sections.

For example, the PE/COFF standard document does not have a concept of segments -- only sections, whereas the ELF object format has both. In the case of ELF, segments in the object file are analogous to what is known as segments in context of a CPU or instruction set architecture, such as x86 -- that is, a segment is some contiguous partition of memory with a specific set of memory access rights (or similar) associated with it. The typical examples are executable "code segments" vs non-executable "data segments".

Sections, on the other hand, have more to do with how code or data are logically organized in an object file. For example, a table of exported symbols might be stored in a section separate from the data that is accessed by the application during its exection, although both are considered data.

If an object file format has a concept of both segments and sections, each section is typically fully contained within a single segment (at least that is the case with ELF).

Liturgics answered 8/4, 2018 at 21:56 Comment(2)
Interesting - would you happen to know what’s the case with PE?Nathan
Since the PE format only has sections, all the meta-data that is needed by both the linker and loader are contained within or associated with the sections. For object file formats that do have segments as well, such as ELF, it is worth to note that all the code/data is the same -- it is just the object file format that provides two different views of the data. Segments are typically not strictly needed, since all the information is also available via the section view. They have more to do with making it convenient for a loader.Liturgics

© 2022 - 2024 — McMap. All rights reserved.