Skip Navigation Links | |
Exit Print View | |
Linker and Libraries Guide Oracle Solaris 10 1/13 Information Library |
Part I Using the Link-Editor and Runtime Linker
1. Introduction to the Oracle Solaris Link Editors
5. Link-Editor Quick Reference
7. Building Objects to Optimize System Performance
10. Establishing Dependencies with Dynamic String Tokens
Part IV ELF Application Binary Interface
Symbol Table Layout and Conventions
13. Program Loading and Dynamic Linking
A. Linker and Libraries Updates and New Features
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 executable and shared 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;
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 an executable or shared 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.
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 or ELF32_R_SYM, respectively, to the entry's r_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, the r_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))
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. 32–bit SPARC use only Elf32_Rela relocation enteries. 64–bit SPARC and 64–bit x86 use only Elf64_Rela relocation entries. Thus, the r_addend member serves as the relocation addend. x86 uses only Elf32_Rel relocation entries. The field to be relocated holds the addend. In all cases, the addend and the computed result use the same byte order.
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. Sections specifies these relationships. A sh_info entry is required when a relocation section exists in a relocatable object, but is optional for executables and shared 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.
The following notation is used to describe relocation computations.
The addend used to compute the value of the relocatable field.
The base address at which a shared object is loaded into memory during execution. Generally, a shared object file is built with a base virtual address of 0. However, the execution address of the shared object is different. See Program Header.
The offset into the global offset table at which the address of the relocation entry's symbol resides during execution. See Global Offset Table (Processor-Specific).
The address of the global offset table. See Global Offset Table (Processor-Specific).
The section offset or address of the procedure linkage table entry for a symbol. See Procedure Linkage Table (Processor-Specific).
The section offset or address of the storage unit being relocated, computed using r_offset.
The value of the symbol whose index resides in the relocation entry.
The size of the symbol whose index resides in the relocation entry.
On the SPARC platform, relocation entries apply to bytes (byte8), half-words (half16), words (word32), and extended-words (xword64).
The dispn family of relocation fields (disp19, disp22, disp30) are word-aligned, sign-extended, PC-relative displacements. All encode a value with its least significant bit in position 0 of the word, and differ only in the number of bits allocated to the value.
The d2/disp8 and d2/disp14 variants encode 16 and 10-bit displacement values using two non-contiguous bit fields, d2, and dispn.
The immn family of relocation fields (imm5, imm6, imm7, imm10, imm13, imm22) represent unsigned integer constants. All encode a value with its least significant bit in position 0 of the word, and differ only in the number of bits allocated to the value.
The simmn family of relocation fields (simm10, simm11, simm13, simm22) represent signed integer constants. All encode a value with its least significant bit in position 0 of the word, and differ only in the number of bits allocated to the value.
Field names in the following table tell whether the relocation type checks for overflow. A calculated relocation value can be larger than the intended field, and a relocation type can verify (V) the value fits or truncate (T) the result. As an example, V-simm13 means that the computed value can not have significant, nonzero bits outside the simm13 field.
Table 12-13 SPARC: ELF Relocation Types
|
Note - Additional relocations are available for thread-local storage references. These relocations are covered in Chapter 14, Thread-Local Storage.
Some relocation types have semantics beyond simple calculation.
Resembles R_SPARC_LO10, except that the relocation refers to the address of the symbol's GOT entry. Additionally, R_SPARC_GOT10 instructs the link-editor to create a global offset table.
Resembles R_SPARC_13, except that the relocation refers to the address of the symbol's GOT entry. Additionally, R_SPARC_GOT13 instructs the link-editor to create a global offset table.
Resembles R_SPARC_22, except that the relocation refers to the address of the symbol's GOT entry. Additionally, R_SPARC_GOT22 instructs the link-editor to create a global offset table.
Resembles R_SPARC_WDISP30, except that the relocation refers to the address of the symbol's procedure linkage table entry. Additionally, R_SPARC_WPLT30 instructs the link-editor to create a procedure linkage table.
Created by the link-editor for dynamic executables to preserve a read-only text segment. The relocation offset member refers to a location in a writable segment. The symbol table index specifies a symbol that should exist both in the current object file and in a shared object. During execution, the runtime linker copies data associated with the shared object's symbol to the location specified by the offset. See Copy Relocations.
Resembles R_SPARC_32, except that the relocation sets a GOT entry to the address of the specified symbol. The special relocation type enables you to determine the correspondence between symbols and GOT entries.
Created by the link-editor for dynamic objects to provide lazy binding. The relocation offset member gives the location of a procedure linkage table entry. The runtime linker modifies the procedure linkage table entry to transfer control to the designated symbol address.
Created by the link-editor for dynamic objects. The relocation offset member gives the location within a shared object that contains a value representing a relative address. The runtime linker computes the corresponding virtual address by adding the virtual address at which the shared object is loaded to the relative address. Relocation entries for this type must specify a value of zero for the symbol table index.
Resembles R_SPARC_32, except that the relocation refers to an unaligned word. The word to be relocated must be treated as four separate bytes with arbitrary alignment, not as a word aligned according to the architecture requirements.
Resembles R_SPARC_HI22, except that the relocation truncates rather than validates.
Resembles R_SPARC_PC22, except that the relocation truncates rather than validates.
Used with R_SPARC_LOX10 for executables that are confined to the uppermost 4 gigabytes of the 64–bit address space. Similar to R_SPARC_HI22, but supplies ones complement of linked value.
Used with R_SPARC_HIX22. Similar to R_SPARC_LO10, but always sets bits 10 through 12 of the linked value.
Used with the R_SPARC_H44 and R_SPARC_M44 relocation types to generate a 44-bit absolute addressing model.
Used to initialize a register symbol. The relocation offset member contains the register number to be initialized. A corresponding register symbol must exist for this register. The symbol must be of type SHN_ABS.
These relocations provide for code transformations.
The following notation, used in relocation calculation, is unique to 64–bit SPARC.
The secondary addend used to compute the value of the relocation field. This addend is extracted from the r_info field by applying the ELF64_R_TYPE_DATA macro.
The relocations that are listed in the following table extend, or alter, the relocations defined for 32–bit SPARC. See SPARC: Relocation Types.
Table 12-14 64-bit SPARC: ELF Relocation Types
|
The following relocation type has semantics beyond simple calculation.
Resembles R_SPARC_LO10, except that an extra offset is added to make full use of the 13-bit signed immediate field.
On x86, relocation entries apply to words (word32), and extended-words (xword64).
word32 specifies a 32–bit field occupying 4 bytes with an arbitrary byte alignment. These values use the same byte order as other word values in the x86 architecture.
The relocations that are listed in the following table are defined for 32–bit x86.
Table 12-15 32-bit x86: ELF Relocation Types
|
Note - Additional relocations are available for thread-local storage references. These relocations are covered in Chapter 14, Thread-Local Storage.
Some relocation types have semantics beyond simple calculation.
Computes the distance from the base of the GOT to the symbol's GOT entry. The relocation also instructs the link-editor to create a global offset table.
Computes the address of the symbol's procedure linkage table entry and instructs the link-editor to create a procedure linkage table.
Created by the link-editor for dynamic executables to preserve a read-only text segment. The relocation offset member refers to a location in a writable segment. The symbol table index specifies a symbol that should exist both in the current object file and in a shared object. During execution, the runtime linker copies data associated with the shared object's symbol to the location specified by the offset. See Copy Relocations.
Used to set a GOT entry to the address of the specified symbol. The special relocation type enable you to determine the correspondence between symbols and GOT entries.
Created by the link-editor for dynamic objects to provide lazy binding. The relocation offset member gives the location of a procedure linkage table entry. The runtime linker modifies the procedure linkage table entry to transfer control to the designated symbol address.
Created by the link-editor for dynamic objects. The relocation offset member gives the location within a shared object that contains a value representing a relative address. The runtime linker computes the corresponding virtual address by adding the virtual address at which the shared object is loaded to the relative address. Relocation entries for this type must specify a value of zero for the symbol table index.
Computes the difference between a symbol's value and the address of the GOT. The relocation also instructs the link-editor to create the global offset table.
Resembles R_386_PC32, except that it uses the address of the GOT in its calculation. The symbol referenced in this relocation normally is _GLOBAL_OFFSET_TABLE_, which also instructs the link-editor to create the global offset table.
The relocations that are listed in the following table are defined for x64.
Table 12-16 x64: ELF Relocation Types
|
Note - Additional relocations are available for thread-local storage references. These relocations are covered in Chapter 14, Thread-Local Storage.
The special semantics for most of these relocation types are identical to those used for x86. Some relocation types have semantics beyond simple calculation.
This relocations has different semantics from the R_AMD64_GOT32 or equivalent R_386_GOTPC relocation. The x64 architecture provides an addressing mode that is relative to the instruction pointer. Therefore, an address can be loaded from the GOT using a single instruction.
The calculation for the R_AMD64_GOTPCREL relocation provides the difference between the location in the GOT where the symbol's address is given, and the location where the relocation is applied.
The computed value is truncated to 32–bits. The link-editor verifies that the generated value for the relocation zero-extends to the original 64–bit value.
The computed value is truncated to 32–bits. The link-editor verifies that the generated value for the relocation sign-extends to the original 64–bit value.
These relocations are not conformant to the x64 ABI, but are added here for documentation purposes. The R_AMD64_8 relocation truncates the computed value to 8-bits. The R_AMD64_16 relocation truncates the computed value to 16-bits.