An object file's section header table lets you locate all the file's sections. The section header table is an array of Elf32_Shdr structures. A section header table index is a subscript into this array. The ELF header e_shoff gives the byte offset from the beginning of the file to the section header table; e_shnum tells how many entries the section header table contains; and e_shentsize gives the size in bytes of each entry.
Sections contain all information in an object file except the ELF header, the program header table, and the section header table. Object file sections satisfy several conditions:
A section header has the following structure:
sh_name Specifies the name of the section. Its value is an index into the section header string table section, giving the location of a null-terminated string.
sh_type Categorizes the section's contents and semantics. See page 55 for more information.
sh_flags Sections support 1-bit flags that describe miscellaneous attributes.
sh_addr If the section appears in the memory image of a process, this structure gives the address at which the section's first byte should reside. Otherwise, the field contains zero.
sh_offset Gives the byte offset from the beginning of the file to the first byte in the section.
sh_size Gives the section's size in bytes. Unless the section type is SHT_NOBITS, the section occupies sh_size bytes in the file. A section of type SHT_NOBITS may have a non-zero size, but it occupies no space in the file.
sh_link Holds a section header table index link, whose interpretation depends on the section type.
sh_info Holds extra information, whose interpretation depends on the section type.
Some sections have address alignment constraints. For example, if a section holds a doubleword, the system may need to ensure doubleword alignment for the entire section. That is, the value of sh_addr must be equal to 0, modulo the value of sh_addralign. Currently, only 0 and positive integral powers of two are allowed. Values 0 and 1 mean the section has no alignment constraints.
sh_entsize Some sections hold a table of fixed-size entries, such as a symbol table. For such a section, this gives the size in bytes of each entry. This structure contains zero if the section does not hold a table of fixed-sized entries.
Symbol table entries index the section table through the st_shndx field. See "Symbol Tables".
Name |
Value |
SHN_UNDEF |
0 |
SHN_COMMON |
-14 |
SHN_ABS |
-15 |
SHN_GHS_SMALLCOMMON |
-256 |
Common, or .bss symbols are allocated space in this section.
SHN_ABS Contents of the section are absolute values; they are not affected by relocation.
Not available for all processors. Similar to SHN_COMMON, but for a limited number of small variables.
A section header's sh_type specifies the section's semantics:
Name |
Value |
SHT_NULL |
0 |
SHT_PROGBITS |
1 |
SHT_SYMTAB |
2 |
SHT_STRTAB |
3 |
SHT_REL |
9 |
SHT_RELA |
4 |
SHT_NOBITS |
8 |
Marks the section header as inactive. It does not have an associated section. Other members of the section header have undefined values.
Holds information defined by the program that created the ELF file, whose format and meaning are determined solely by the program.
Holds a symbol table. An object file may have only one section of this type, but this restriction may be relaxed in the future. It provides symbols for link editing, though it may also be used for dynamic linking. As a complete symbol table, it may contain many symbols unnecessary for dynamic linking.
Holds a string table. An object file may have multiple string table sections.
Holds relocation entries without explicit addends, such as type Elf32_Rel for the 32-bit class of object files. An object file may have multiple relocation sections.
Holds relocation entries with explicit addends, such as type Elf32_Rela for the 32-bit class of object files. An object file may have multiple relocation sections.
Occupies no space in the file but otherwise resembles SHT_PROGBITS.
A section header's sh_flags holds 1-bit flags that describe the section's attributes.
Name |
Value |
Abbreviation |
SHF_WRITE |
0x1 |
w=writable |
SHF_ALLOC |
0x2 |
a=allocated |
SHF_EXECINSTR |
0x4 |
e=executable |
SHF_GHS_ABS |
0x400 |
b=bits |
SHF_MCORE_NOREAD |
0x80000000 |
N/A |
Contains data that should be writable during process execution.
Occupies memory during process execution. Some control sections do not reside in the memory image of an object file, this attribute is off for those sections.
Contains execution machine instructions.
Indicates that these sections are to have an absolute, non-relocatable address.
Indicates that these sections are not readable, but may be executable.
Two structures in the section header, sh_link and sh_info, hold special information, depending on section type:
Section names beginning with a period (.) are reserved from general use by application programs, although application programs may use these sections if their existing meanings are satisfactory. Application programs may use names without the leading period to be certain of avoiding conflicts with predefined section names.
Some sections hold program and control information. Sections in the following list have predefined meaning, with the indicated types and attributes.
.bss Holds uninitialized data that contributes to the program's memory image. The system initializes the data with zeros when the program begins to run.
.data Holds initialized data that contributes to the program's memory image.
.relaname Both these sections hold relocation information. If the file has a loadable segment that includes relocation, the sections' attributes include the SHF_ALLOC bit; otherwise, that bit will be off. Conventionally, name is supplied by the section to which the relocations apply. A relocation section for .text has the name .rel.text or .rela.text.
.rodata Read-only data area. Similar to the .data section, but comprised of constant data.
.shstrtab Holds section names.
.strtab Holds strings, most commonly the strings that represent the names associated with symbol table entries. If the file has a loadable segment that includes the symbol string table, the section's attributes include the SHF_ALLOC bit; otherwise, that bit will be off.
.symtab Holds a symbol table. If the file has a loadable segment that includes the symbol table, the section's attributes include the SHF_ALLOC bit; otherwise, that bit will be off.
.text Holds the text, or executable instructions, of a program.
Some sections are specific to Green Hills ELF, listed on the next page. Target processors may support some or all of these sections.
.syscall A program code section to support the Green Hills system call mechanism.
.secinfo A table created by the linker describing actions to be taken on sections as they are loaded for program execution (sections to be cleared, copied from ROM to RAM, etc.)
.fixtype Tables created by the compiler for Position Independent Code (PIC) and Position Independent Data (PID) static pointer adjustments to be made when the program is loaded for execution.
.sdabase If not in PID mode, the runtime system initializes the Small Data Area (SDA) base register to be the address of this section.
.zdata Small data area, similar to the .data section but of limited size and more quickly addressed.
.zbss Small data area, similar to be the .bss section but of limited size and more quickly addressed.
.heap Section describing the area of memory for dynamic allocations through malloc and related functions.
.stack Section describing the area of memory that the program stack will occupy.
Relocation is the process of connecting symbolic references with symbolic definitions. For example, when a program calls a function, the associated call instruction must transfer control to the proper destination address at execution. Relocatable files must have information that describes how to modify their section contents, thus allowing executable files to hold the right information for a process' program image. Relocation entries contain this information. In the code below, the second example is preferred:
typedef struct { |
| |
|
Elf32_Addr |
r_offset; |
|
Elf32_Word |
r_info; |
} Elf32_Rel; |
| |
|
|
|
typedef struct { |
| |
|
Elf32_Addr |
r_offset; |
|
Elf32_Word |
r_info; |
|
Elf32_Sword |
r_addend; |
} Elf32_Rela; |
|
r_offset Gives the location at which to apply the relocation action. The value is the byte offset from the beginning of the section to the storage unit affected by the relocation.
r_info Gives both the symbol table index with respect to which the relocation must be made, and the type of relocation to apply information for a process' program image. For example, a call instruction's relocation entry, holds the symbol table index of the function being called. You may find the following macros helpful when reading from or writing to the r_info field:
#define ELF32_R_SYM(i) ((i) >> 8)
#define ELF32_R_TYPE(i) ((unsigned char) (i))
#define ELF32_R_INFO(s,t) (((s)<<8) + (unsigned char) (t))
r_addend Specifies a constant value used to compute the final value to be stored into the relocatable field.
A relocation section references two other sections: a symbol table and a section to modify. The section header's sh_info and sh_link specify these relationships. Relocation entries for different object files have slightly different interpretations for r_offset: