1
votes

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.

1
In this context section is the correct term. The first book is just being sloppy.Jester
@Jester: arguably segment is the correct term for grouping types of sections, and the sloppiness was applying section names to segments. e.g. .text and .rodata sections both get linked into the text segment. (Or used to; modern ld makes .rodata a separate ELF segment so it can be non-executable.)Peter Cordes
@Jester: memory chunks .text, .data etc are defined in memory by segment descriptors and not by section descriptors, so I think that the term segments is more appropriate for memory blocks with different access rights.vitsoft
"There are three types of segments: .text, .bss, and .data." is just plain wrong. Those are not segment types. They are just standard names for some of the sections.Jester

1 Answers

3
votes

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

  • Segments are described in the program header. Loosely, each segment descibes 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, desbribed 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. So a dynmaically linked executable will always have both.

In general, segments do not overlap each other and sections do not overlap each other, but sections may decribe 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 decribe data in two different segments. There are also (generally) sections that are not part of any segment.