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.


Overall structure

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).

Table F-1   ELF file headers 
Section   Description  

ELF header  

General information; always present.  

Program header table  

Information about an executable file; usually only present in executables.  

Section data  

The actual data for a section; some sections have special meaning, i.e. the symbol table and the string table.  

Section headers  

Information about the different ELF sections; one for each section.  

The following figure shows a typical ELF file structure:


ELF header

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;
};

Table F-2   ELF header fields 
Field   Description  

e_ident  

Sixteen byte long string with the following content: 4-byte file identification: "\x7FELF" 1-byte class: 1 for 32-bit objects 1-byte data encoding: little-endian: 1, big-endian: 2 1-byte version: 1 for current version

9-byte zero padding

e_type  

The file type: relocatable: 1, executable: 2

e_machine  

Target architecture:

 

20  

PowerPC  

e_version  

Object file version: set to 1.

e_entry  

Programs entry address.

e_phoff  

File offset to the Program Header Table.

e_shoff  

File offset to the Section Header Table.

e_flags  

Not used.

e_ehsize  

Size of the ELF Header.

e_phentsize  

Size of each entry in the Program Header Table.

e_phnum  

Number of entries in the Program Header Table.

e_shentsize  

Size of each entry in the Section Header Table.

e_shnum  

Number of entries in the Section Header Table.

e_shstrndx  

Section Header index of the entry containing the String Table for the section names.


Program header

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  

p_type  

Type of the segment; only PT_LOAD(1) is used by the linker.  

p_offset  

File offset where the raw data of the segment resides.  

p_vaddr  

Address where the segment resides when it is loaded in memory.  

p_paddr  

Not used.  

p_filesz  

Size of the segment in the file; it may be zero.  

p_memsz  

Size of the segment in memory; it may be zero.  

p_flags  

Bit mask containing a combination of the following flags:

PF_X (1)    Execute
PF_W (2)    Write
PF_R (4)     Read  

p_align  

Alignment of the segment in memory and in the file.  


Section headers

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  

sh_name  

Specifies the name of the section; it is an index into the section header string table defined below.

sh_type  

Type of the section and one of the below:

 

SHT_NULL (0)  

inactive header  

 

SHT_PROGBITS (1)  

code or data defined by the program  

 

SHT_SYMTAB (2)  

symbol table  

 

SHT_STRTAB (3)  

string table  

 

SHT_RELA (4)  

relocation entries  

 

SHT_NOBITS (8)  

uninitialized data  

 

SHT_COMDAT (12)  

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).  

sh_flags  

Combination of the following flags:

 

SHF_WRITE (1)  

contains writable data  

 

SHF_ALLOC (2)  

contains allocated data  

 

SHF_EXECINSTR (4)  

contains executable instructions  

sh_addr  

Address of the section if the section is to be loaded into memory.

sh_offset  

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.

sh_size  

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.

sh_link  

Link to the index of another section header:

 

SHT_COMDAT  

section with which this section should be combined  

 

SHT_RELA  

the symbol table  

 

SHT_NOBITS  

section with which this section should be combined  

 

SHT_PROGBITS  

section with which this section should be combined  

 

SHT_SYMTAB  

the string table  

sh_info  

Contains the following information:

 

SHT_RELA  

the section to which the relocation applies  

 

SHT_SYMTAB  

index of the first non-local symbol  

sh_addralign  

Alignment requirement of the section.

sh_entsize  

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)  

BSS  

SHT_NOBITS  

SHF_ALLOC | SHF_WRITE  

COMMENT  

SHT_PROGBITS  

(none)  

CONST  

SHT_PROGBITS  

SHF_ALLOC  

DATA  

SHT_PROGBITS  

SHF_ALLOC | SHF_WRITE  

TEXT  

SHT_PROGBITS  

SHF_ALLOC | SHF_EXECINSTR  


Special sections

The following table shows the names of some typical sections and explains their contents:


Relocation information

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 
Field   Description  

r_offset  

Relative address of the area within the current section to be patched with the correct address.  

r_info >> 8  

Upper 24 bits of r_info is an index into the symbol table pointing to the entry describing the symbol that is referenced at r_offset.  

r_info & 255  

Lower 8 bits is the relocation type that describes what addressing mode is used; it describes whether the mode is absolute or relative, and the size of the addressing mode. See the table below for a description of the various relocation types.  

r_addend  

A constant to be added to the symbol when computing the value to be stored in the relocatable field.  

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  
Number
 
Description  

<PPC>  

 

 

R_PPC_ADDR32  

1
 

32 bit absolute address:

.long  var  

R_PPC_ADDR24  

2
 

26 bit absolute address where the lower two bits are ignored:

bla    func  

R_PPC_ADDR16  

3
 

16 bit absolute address:

lwz    r3,var(r0)  

R_PPC_ADDR16_LO  

4
 

The lower 16 bits of an absolute address:

lwz    r3,var@l(r0)  

R_PPC_ADDR16_HI  

5
 

The higher 16 bits of an absolute address:

addis  r3,r0,var@h  

R_PPC_ADDR16_HA  

6
 

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:

addis  r3,r0,var@ha  

R_PPC_ADDR14  

7
 

16 bit absolute address where the lower two bits are ignored:

bca    4,2,label  

R_PPC_REL24  

10
 

26 bit PC relative address where the lower two bits are ignored:

bl     func  

R_PPC_REL14  

11
 

16 bit PC relative address where the lower two bits are ignored:

bc     4,2,label  

R_PPC_EMB_SDA21  

109
 

An instruction with the lower 16 bits being the offset into a 64KB big Small Data Area (SDA). There are three SDAs:

  • The absolute SDA (address 0) pointed to by r0

  • The constant SDA2 (typically the .sdata2 section) pointed to by r2

  • The normal SDA (typically the .sdata and .sbss sections) pointed to by r13

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:

lwz    r3,var@sdarx(r0)  

R_PPC_EMB_RELSDA  

116
 

16 bit SDA offset. Depending on which SDA the identifier is defined in, the linker will use the corresponding offset:

.short var@sdax  

R_PPC_DIAB_SDA21_LO a  

180
 

Similar to R_PPC_EMB_SDA21 but uses the lower 16 bits of the identifier:

lwz    r3,var@sdarx@l(r3)  

R_PPC_DIAB_SDA21_HI  

181
 

Similar to R_PPC_EMB_SDA21 but uses the higher 16 bits of the identifier:

addis  r3,r0,var@sdarx@h  

R_PPC_DIAB_SDA21_HA  

182
 

Similar to R_PPC_EMB_SDA21 but uses the higher adjusted 16 bits of the identifier:

addis  r3,r0,var@sdarx@ha  

R_PPC_DIAB_RELSDA_LO  

183
 

Similar to R_PPC_EMB_RELSDA but uses the lower 16 bits of the identifier:

.short var@sdax@l  

R_PPC_DIAB_RELSDA_HI  

184
 

Similar to R_PPC_EMB_RELSDA but uses the higher 16 bits of the identifier:

.short var@sdax@h  

R_PPC_DIAB_RELSDA_HA  

185
 

Similar to R_PPC_EMB_RELSDA but uses the higher adjusted 16 bits of the identifier:

.short var@sdax@ha  

a The Diab relocation modes are only used when the far-data-relative mode and the far-code-relative mode is used. See Addressing mode - functions, variables, strings, p.239 for information on these modes.


Line number information

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.


Symbol table

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  
Field   Description  

st_name  

Index into the symbol string table which holds the name of the symbol.  

st_value  

Value of the symbol:

  • The alignment requirement of symbols whose section index is SHN_COMMON

  • The offset from the beginning of a section in relocatable files.

  • The address of the symbol in executable files.
 

st_size  

Size of an object  

st_info >> 4  

Upper four bits define the binding of the symbol:

STB_LOCAL  (0)  symbol is local to the file
STB_GLOBAL (1)  symbol is visible to all object files
STB_WEAK   (2)  symbol is global with lower precedence  

st_info & 15  

Lower four bits define the type of the symbol:

STT_NOTYPE  (0)  symbol has no type
STT_OBJECT  (1)  symbol is a data object (a variable)
STT_FUNC    (2)  symbol is a function
STT_SECTION (3)  symbol is a section name
STT_FILE    (4)  symbol is the filename  

st_other  

Currently not used.  

st_shndx  

Index of the section where the symbol is defined. Special section numbers include:

SHN_UNDEF  (0x0000)  undefined section
SHN_ABS    (0xfff1)  absolute, non-relocatable symbol
SHN_COMMON (0xfff2)  unallocated, external variable  


String table

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.