The process of assigning input sections to output segments involves the following data structures.
Input sections are read from relocatable objects input to the link editor. Some are examined and processed by the link-editor, while others are simply passed to the output without examination of their contents (e.g. PROGBITS).
Output sections are sections that are written to the output object. Some are formed from the concatenation of sections passed through from the input objects. Others, such as symbol tables and relocation sections are generated by the link-editor itself, often incorporating information read from the input objects.
When the link-editor passes an input section through to become an output section, the section usually retains the input section name. However, the link-editor can modify the name in certain circumstances. For instance, the link-editor translates input section names of the form name%XXX, dropping the % character and any characters following from the output section name.
The link-editor maintains a list of known segments. This list initially contains the predefined segments, described in Predefined Segments. When a LOAD_SEGMENT, NOTE_SEGMENT, or NULL_SEGMENT mapfile directive is used to create a new segment, an additional segment descriptor for the new segment is added to this list. The new segment goes at the end of the list following other segments of the same type, unless explicitly ordered by setting a virtual address (LOAD_SEGMENT), or by using the SEGMENT_ORDER directive.
When creating the output object, the link-editor only creates program headers for the segments that receive a section. Empty segments are quietly ignored. Hence, user specified segment definitions have the power to completely replace the use of the predefined segments definitions, despite the fact that there is no explicit facility for removing a segment definition from the link-editor list.
A set of section attributes required in order to place that section in a given segment is called an entrance criterion for the segment. A given segment can have an arbitrary number of entrance criteria.
The link-editor maintains an internal list of all defined entrance criteria. This list is used to place sections into segments, as described below. Each mapfile inserts the entrance criterion created by the ASSIGN_SECTION attribute to the LOAD_SEGMENT, NOTE_SEGMENT, or NULL_SEGMENT mapfile directive at the top of this list, in the order they are encountered in the mapfile. The entrance criteria for the built-in segments discussed in Predefined Segments are placed at the end of this list. Therefore, mapfile defined entrance criteria take precedence over the built in rules, and mapfiles at the end of the command line take precedence over those found at the beginning.
For each section written to the output object, the link-editor performs the following steps to place the section in an output segment.
The attributes of the section are compared to each record in the internal entrance criteria list, starting at the head of the list and considering each entrance criterion in turn. A match occurs when every attribute in the entrance criterion matches exactly, and the segment associated with the entrance criterion is not disabled. The search stops with the first entrance criterion that matches, and the section is directed to the associated segment.
If no Entrance Criterion match is found, the section is placed at the end of the output file after all other segments. No program header entry is created for this information. Most non-allocable sections (e.g. debug sections) end up in this area.
When the section falls into a segment, the link-editor checks the list of existing output sections in that segment as follows.
If the section attribute values match those of an existing output section exactly, the section is placed at the end of the list of sections associated with that output section.
If no matching output section is found, a new output section is created with the attributes of the section being placed, and the input section is placed within the new output section. This new output section is positioned within the segment following any other output sections with the same section type, or at the end of the segment if there are none.