F Executable and Linking Format (ELF)
Overall structure 616
ELF header 617
Program header 618
Section headers 620
Special sections 622
Relocation information 623
Line number information 627
Symbol table 627
String table 628
This appendix describes the Executable and Linking Format (ELF). The linker can also handle COFF, which is described in G, Common Object File Format (COFF), p.629.
In this appendix, the form NAME(n) means that the symbolic value NAME has the value shown in the parentheses.
The ELF Object Format is used both for object files (.o extension) and executable files. Some of the information is only present in object files, some only in the executable files.
ELF files consist of the following parts. The ELF header must be in the beginning of the file; the other parts can come in any order (the ELF header gives offsets to the other parts).
The following figure shows a typical ELF file structure:
The ELF header contains general information about the object file and has the following structure from the file elf.h (Elf32_Half is two bytes, the other types are four bytes):
#define EI_NIDENT 16
typedef struct {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
};
The program header is an array of structures, each describing a loadable segment of an executable file. The following structure from the file elf.h describes each entry:
typedef struct {
Elf32_Word p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
Elf32_Word p_filesz;
Elf32_Word p_memsz;
Elf32_Word p_flags;
Elf32_Word p_align;
} Elf32_Phdr;
Table F-3 ELF program header fields
| Field | Description |
|---|---|
Address where the segment resides when it is loaded in memory. | |
There is incitation header for each section in the ELF file, specified by the e_shnum field in the ELF Header. Section headers have the following structure from the file elf.h:
typedef struct {
Elf32_Word sh_name;
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Word sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Word sh_addralign;
Elf32_Word sh_entsize;
} Elf32_Shdr;
Table F-4 ELF section header fields
| Field | Description | |
|---|---|---|
Specifies the name of the section; it is an index into the section header string table defined below. | ||
like SHT_PROGBITS except that the linker removes duplicate SHT_COMDAT sections having the same name and removes unreferenced SHT_COMDAT sections (used in C++ template instantiation - see Templates, p.225). | ||
Address of the section if the section is to be loaded into memory. | ||
File offset to the raw data of the section; note that the SHT_NOBITS sections does not have any raw data since it will be initialized by the operating system. | ||
Size of the section; an SHT_NOBITS section may have a non-zero size even though it does not occupy any space in the file. | ||
Size for each entry in sections that contains fixed-sized entries, such as symbol tables. | ||
The following table shows the correspondence between the type-spec as defined on p.409 and the ELF section type and flags assigned to the output section.
Table F-5 type-spec - ELF section type and flags correspondence
| type-spec | Section type (sh_type) | Section flags (sh_flags) |
|---|---|---|
The following table shows the names of some typical sections and explains their contents:
Small constant data; see the -Xsmall-const option on p.106. | |
Small initialized data; see the -Xsmall-data option on p.106. | |
Code that is to be executed when the program has finished execution. | |
Relocation Information sections contain information about unresolved references. Since compilers and assemblers do not know at what absolute memory address a symbol will be allocated, and since they are unaware of definitions of symbols in other files, every reference to such a symbol will create a relocation entry. The relocation entry will point to the address where the reference is being made, and to the symbol table entry that contains the symbol that is referenced. The linker will use this information to fill in the correct address after it has allocated addresses to all symbols.
When an offset is added to a symbol in the assembly source,
lwz r3,(var+16)(r0)
that offset is stored in the r_addend field, so that adding the real address of the symbol with the address field will yield a correct reference.
The relocation section does not normally exist in executable files.
A relocation entry has the following structure from the file elf.h:
typedef struct {
Elf32_Addr r_offset;
Elf32_Word r_info;
Elf32_Sword r_addend;
} Elf32_Rela;
Table F-6 ELF relocation entry fields
The relocation types for each supported target are documented in version_path/include/elf_target.h.
Table F-7 ELF relocation types and examples
| Relocation type |
| Description |
|---|---|---|
26 bit absolute address where the lower two bits are ignored: | ||
The adjusted higher 16 bits of an absolute address. If the lower 16 bits is a negative number, one is added to the upper 16 bits: | ||
16 bit absolute address where the lower two bits are ignored: | ||
26 bit PC relative address where the lower two bits are ignored: | ||
16 bit PC relative address where the lower two bits are ignored: | ||
An instruction with the lower 16 bits being the offset into a 64KB big Small Data Area (SDA). There are three SDAs:
Depending on which of these three SDA's the identifier is defined in, the linker will patch the instruction to use the correct register and offset: | ||
16 bit SDA offset. Depending on which SDA the identifier is defined in, the linker will use the corresponding offset: | ||
R_PPC_DIAB_SDA21_LO a | Similar to R_PPC_EMB_SDA21 but uses the lower 16 bits of the identifier: | |
Similar to R_PPC_EMB_SDA21 but uses the higher 16 bits of the identifier: | ||
Similar to R_PPC_EMB_SDA21 but uses the higher adjusted 16 bits of the identifier: | ||
Similar to R_PPC_EMB_RELSDA but uses the lower 16 bits of the identifier: | ||
Similar to R_PPC_EMB_RELSDA but uses the higher 16 bits of the identifier: | ||
Similar to R_PPC_EMB_RELSDA but uses the higher adjusted 16 bits of the identifier: |
The line number information section .line contains the mapping from source line numbers to machine instruction addresses used by symbolic debuggers. This information is only available if the -g option is specified to the compiler.
The symbol table section .symtab is an array of entries containing information about the symbols referenced in the ELF file. A symbol table entry has the following structure from the file elf.h:
typedef struct {
ELF32_Word st_name;
ELF32_Addr st_value;
ELF32_Word st_size;
unsigned char st_info;
unsigned char st_other;
Elf32_Half st_shndx;
} Elf32_Sym;
Table F-8 ELF symbol table fields
The string table sections, .strtab and .shstrtab, contain the null terminated names of symbols in the symbol table and section names. Those symbols point into the string table through an offset. The first byte of the string table is always zero and after that all strings are stored sequentially.
support@windriver.com
Copyright © 2002, Wind River Systems, Inc. All rights reserved.