5

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?

Dean Leitersdorf
  • 1,303
  • 1
  • 11
  • 22

1 Answers1

3

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).

Erlend Graff
  • 1,098
  • 16
  • 27
  • Interesting - would you happen to know what’s the case with PE? – Dean Leitersdorf Apr 10 '18 at 08:26
  • 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. – Erlend Graff Apr 10 '18 at 19:52