Linker and Libraries Guide

Example: Section to Segment Assignment

This example demonstrates how to define segments and assign input sections to them.


Example 9–1 Basic Section to Segment Assignment

        1    $mapfile_version 2
        2    LOAD_SEGMENT elephant {
        3            ASSIGN_SECTION {
        4                    IS_NAME=.data;
        5                    FILE_PATH=peanuts.o;
        6            };
        7            ASSIGN_SECTION {
        8                    IS_NAME=.data;
        9                    FILE_OBJNAME=popcorn.o;
       10           };
       11   };
       12
       13   LOAD_SEGMENT monkey {
       14           VADDR=0x80000000;
       15           MAX_SIZE=0x4000;
       16           ASSIGN_SECTION {
       17                   TYPE=progbits;
       18                   FLAGS=ALLOC EXECUTE;
       19           };
       20           ASSIGN_SECTION {
       21                   IS_NAME=.data
       22           };
       23   };
       24
       25   LOAD_SEGMENT donkey {
       26           FLAGS=READ EXECUTE;
       27           ALIGN=0x1000;
       28           ASSIGN_SECTION {
       29                   IS_NAME=.data;
       30           };
       31   };
       32
       33   LOAD_SEGMENT text {
       34           VADDR=0x80008000
       35   };

Four separate segments are manipulated in this example. Every mapfile starts with a $mapfile_version declaration as shown on line 1. Segment elephant (lines 2-11) receives all of the data sections from the files peanuts.o or popcorn.o. The object popcorn.o can come from an archive, in which case the archive file can have any name. Alternatively, popcorn.o can come from any file with a basename of popcorn.o. In contrast, peanuts.o can only come from a file with exactly that name. For example, the file /var/tmp/peanuts.o supplied to a link-edit does not match peanuts.o.

Segment monkey (lines 13-23) has a virtual address of 0x80000000, and a maximum length of 0x4000. This segment receives all sections that are both PROGBITS and allocable-executable, as well as all sections not already in the segment elephant with the name .data. The .data sections entering the monkey segment need not be PROGBITS or allocable-executable, because they match the entrance criterion on line 20 rather than the one on line 16. This illustrates that and and relationship exists between the sub-attributes within a ASSIGN_SECTION attribute, while an or relationship exists between the different ASSIGN_SECTION attributes for a single segment.

The donkey segment (lines 25-31) is given non-default permission flags and alignment, and will accept all sections named .data. However, this segment will never be assigned any sections, and as a result, segment donkey will never appear in the output object. The reason for this is that the link-editor examines entrance criteria in the order they appear in themapfile. In this mapfile, segment elephant accepts some .data sections, and segment takes any that are left, leaving none for donkey.

Lines 33-35 set the virtual address of the text segment to 0x80008000. The text segment is one of the standard predefined segments, as described in Predefined Segments, so this statement modifies the existing segment rather than creating a new one.