5.3. Introduction to Sections

The smallest unit of an object file is a section. A section is a block of code or data that occupies contiguous space in the memory map. Each section of an object file is separate and distinct.

ELF format executable object files contain segments. An ELF segment is a meta-section. It represents a contiguous region of target memory. It is a collection of sections that have the same property, such as writeable or readable. An ELF loader needs the segment information, but does not need the section information. The ELF standard allows the linker to omit ELF section information entirely from the executable object file.

Object files usually contain three default sections:

.text section

Contains executable code

.data section

Usually contains initialized data

.bss

Usually reserves space for uninitialized variables

Some targets allow content other than text, such as constants, in .text sections.

The linker allows you to create, name, and link other kinds of sections. The .text, .data, and .bss sections are archetypes for how sections are handled.

There are two basic types of sections:

  • Initialized sections: Contain data or code. The .text and .data sections are initialized.

  • Uninitialized sections: Reserve space in the memory map for uninitialized data. The .bss section is uninitialized.

One of the linker’s functions is to relocate sections into the target system’s memory map; this function is called placement. Because most systems contain several types of memory, using sections can help you use target memory more efficiently. All sections are independently relocatable; you can place any section into any allocated block of target memory. For example, you can define a section that contains an initialization routine and then allocate the routine in a portion of the memory map that contains ROM. For information on section placement, see Section Allocation and Placement.

The figure below shows the relationship between sections in an object file and a hypothetical target memory. ROM may be EEPROM, FLASH or some other type of physical memory in an actual system.

../../_images/memblocks_new.png

Figure 5.1 Sections in Object File and in Memory

5.3.1. Special Section Names

You can use the .sect and .usect directives to create any section name you like, but certain sections are treated in a special manner by the linker and the compiler’s run-time support library. If you create a section with the same name as a special section, you should take care to follow the rules for that special section.

A few common special sections are:

  • .text – Used for program code.

  • .data – Used for initialized non-const objects (global variables).

  • .bss – Used for uninitialized objects (global variables).

  • .const – Used for initialized const objects (variables declared const).

  • .rodata – Used for initialized const objects (string constants).

  • .cinit – Used to initialize C global variables at startup.

  • .stack – Used for the function call stack.

  • .sysmem – Used for the dynamic memory allocation pool.

For more information on sections, see Section Allocation and Placement.