Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer runapp05.tex V1 - 09/04/2008 6:13pm Page 1261

Appendix E: The ELF Binary Format


Once the addresses of theprintfandaddfunctions have been determined, they must be inserted at the
specified offsets in order to generate executable code that runs correctly.

Data Structures


For technical reasons, there are two different types of relocation information that are, unfortunately,
represented by slightly different data structures. Thefirst type refers to normal relocation. The relocation
table entries in aSHT_RELtype section are defined by the following data structure:

<elf.h>
typedef struct elf32_rel {
Elf32_Addr r_offset;
Elf32_Word r_info;
} Elf32_Rel;

Whiler_offsetspecifies the position of the entry to be relocated,r_infosupplies not only a position in
the symbol table but also additional information on the type of relocation (as described shortly). This is
achieved by splitting the value into two parts (exactly how is not important here).

The alternative type — known as a relocation entry with a constant addend — may occur only in sections
of theSHT_RELAtype. The entries of such sections are defined by the following data structure:

<elf.h>
typedef struct elf32_rela{
Elf32_Addr r_offset;
Elf32_Word r_info;
Elf32_Sword r_addend;
} Elf32_Rela;

Ther_offsetandr_infofields of the first relocation type are supplemented by anr_addendelement
that holds a value known as anaddend. When the relocation value is computed, this value is treated
differently depending on relocation type.

Notice that the addend value is also present whenelf32_relis used. Although it is not explicitly
held in the data structure, the linker uses the valuelocated at the memory position where the computed
relocation size is to be inserted as the addend. The purpose of this value is illustrated in the following
example.

There are functionally equivalent 64-bit data structures for both relocation types:

<elf.h>
typedef struct elf64_rel {
Elf64_Addr r_offset; /* Location at which to apply the action */
Elf64_Xword r_info; /* Index and type of relocation */
} Elf64_Rel;

<elf.h>
typedef struct elf64_rela {
Elf64_Addr r_offset; /* Location at which to apply the action */
Elf64_Xword r_info; /* Index and type of relocation */
Elf64_Sxword r_addend; /* Constant addend used to compute value */
} Elf64_Rela;
Free download pdf