Search code examples
assemblyelf

Section vs. segment?


I have already read this post and I understand that segments contain runtime information and encapsulate sections, which contain linking information. But, I am still confused on why these terms are being used seemingly interchangeably in these two books.

"The Shellcoder's Handbook"

Next, information is loaded from the program’s executable file to the newly created address space. There are three types of segments: .text, .bss, and .data. The .text segment is mapped as read-only, whereas .data and .bss are writable. The .bss and .data segments are reserved for global variables. The .data segment contains static initialized data, and the .bss segment contains uninitialized data. The final segment, .text, holds the program instructions.

"Professional Assembly Language"

The text section is required in all assembly language programs. It is where the instruction codes are declared within the executable program. The data and bss sections are optional, but often used within a program. The data section declares data elements that are declared with an initial value. These data elements are used as variables within the assembly language program. The bss section declares data elements that are instantiated with a zero (or null) value. These data elements are most often used as buffer areas within the assembly language program.


Solution

  • In the context of ELF, they are two different related things.

    • Segments are described in the program header. Loosely, each segment describes a chunk of the file to be loaded into memory when an executable is run.

    • Sections are described in the section header. Loosely, each section describes a chunk of data relevant to the program.

    So both sections and segments are chunks of the file, described as an offset and size (though in both cases the size may be 0, in which case the offset is ignored). Any given ELF file might have only segments, or only sections, or both segments and sections. In order to be executable it must have segments to load. In order to be linkable, it must have sections describing what is where.

    Dynamically linked executable must have segments, but sections are still optional: There is a PT_DYNAMIC segment (see this) which indicates the content of the .dynamic section. In this way, the dynamic linker can still find the offset of the symbol table .dynsym.

    In general, segments do not overlap each other and sections do not overlap each other, but sections may describe data that is part (or all) of a segment. That is not a strict requirement of the format, but it would be strange to violate. It would also be very strange for a section to describe data in two different segments. There are also (generally) sections that are not part of any segment.