Relocation Sections
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. This information allows dynamic object files to hold the right information for a process's program image. Relocation entries are these data.
Relocation entries can have the following structure. See
sys/elf.h
.
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; typedef struct { Elf64_Addr r_offset; Elf64_Xword r_info; } Elf64_Rel; typedef struct { Elf64_Addr r_offset; Elf64_Xword r_info; Elf64_Sxword r_addend; } Elf64_Rela;
-
r_offset
-
This member gives the location at which to apply the relocation action. Different object files have slightly different interpretations for this member.
For a relocatable file, the value indicates a section offset. The relocation section describes how to modify another section in the file. Relocation offsets designate a storage unit within the second section.
For a dynamic object, the value indicates the virtual address of the storage unit affected by the relocation. This information makes the relocation entries more useful for the runtime linker.
Although the interpretation of the member changes for different object files to allow efficient access by the relevant programs, the meanings of the relocation types stay the same.
-
r_info
-
This member gives both the symbol table index, with respect to which the relocation must be made, and the type of relocation to apply. For example, a call instruction's relocation entry holds the symbol table index of the function being called. If the index is
STN_UNDEF
, the undefined symbol index, the relocation uses zero as the symbol value.Relocation types are processor-specific. A relocation entry's relocation type or symbol table index is the result of applying
ELF32_R_TYPE
orELF32_R_SYM
, respectively, to the entry'sr_info
member.#define ELF32_R_SYM(info) ((info)>>8) #define ELF32_R_TYPE(info) ((unsigned char)(info)) #define ELF32_R_INFO(sym, type) (((sym)<<8)+(unsigned char)(type)) #define ELF64_R_SYM(info) ((info)>>32) #define ELF64_R_TYPE(info) ((Elf64_Word)(info)) #define ELF64_R_INFO(sym, type) (((Elf64_Xword)(sym)<<32)+ \ (Elf64_Xword)(type))
For 64-bit SPARC
Elf64_Rela
structures, ther_info
field is further broken down into an 8-bit type identifier and a 24-bit type dependent data field. For the existing relocation types, the data field is zero. New relocation types, however, might make use of the data bits.#define ELF64_R_TYPE_DATA(info) (((Elf64_Xword)(info)<<32)>>40) #define ELF64_R_TYPE_ID(info) (((Elf64_Xword)(info)<<56)>>56) #define ELF64_R_TYPE_INFO(data, type) (((Elf64_Xword)(data)<<8)+ \ (Elf64_Xword)(type))
-
r_addend
-
This member specifies a constant addend used to compute the value to be stored into the relocatable field.
Rela
entries contain an explicit addend. Entries of type
Rel
store an implicit addend in the location to be modified. In all
cases, the addend and the computed result use the same byte order. The relocation entry type
and interpretation of the addend value are defined by the platform specific ABI.
- SPARC
-
32-bit SPARC uses
Elf32_Rela
relocation entries. 64-bit SPARC usesElf64_Rela
relocation entries. The prior value of the field to be relocated, added to ther_addend
member, serves as the relocation addend. - 32-bit x86
-
32-bit x86 uses
Elf32_Rel
relocation entries. The field to be relocated holds the addend. - 64-bit x86
-
64-bit x86 uses
Elf64_Rela
relocation entries. Ther_addend
member serves as the relocation addend. The prior value of the field to be relocated is ignored.
A relocation section can reference two other sections: a symbol table, identified by the
sh_link
section header entry, and a section to modify, identified
by the sh_info
section header entry. Section Headers specifies these relationships. A sh_info
entry
is required when a relocation section exists in a relocatable object, but is optional for
dynamic objects. The relocation offset is sufficient to perform the
relocation.
In all cases, the r_offset
value designates the offset or
virtual address of the first byte of the affected storage unit. The relocation type specifies
which bits to change and how to calculate their values.