ELF重定位:Relocation Sections
Posted rtoax
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ELF重定位:Relocation Sections相关的知识,希望对你有一定的参考价值。
Relocation Sections (Linker and Libraries Guide) https://docs.oracle.com/cd/E19120-01/open.solaris/819-0690/6n33n7fct/index.html
目录
Relocation Types (Processor-Specific)
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 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;
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 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.
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 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))
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. 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.
Relocation Types (Processor-Specific)
Relocation entries describe how to alter instruction and data fields in the following figures. Bit numbers appear in the lower box corners.
On the SPARC platform, relocation entries apply to bytes (byte8), half-words (half16), or words.
On 64–bit SPARC and x64, relocations also apply to extended-words (xword64).
On x86, relocation entries apply to words (word32).
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.
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.
Calculations for the following relocation types assume the actions are transforming a relocatable file into either an executable or a shared object file. Conceptually, the link-editor merges one or more relocatable files to form the output. The link-editor first decides how to combine and locate the input files. The link-editor then updates the symbol values and performs the relocation. Relocations applied to executable or shared object files are similar and accomplish the same result. Descriptions in the tables in this section use the following notation.
A
The addend used to compute the value of the relocatable field.
B
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.
G
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).
GOT
The address of the global offset table. See Global Offset Table (Processor-Specific).
L
The section offset or address of the procedure linkage table entry for a symbol. See Procedure Linkage Table (Processor-Specific).
P
The section offset or address of the storage unit being relocated, computed using r_offset.
S
The value of the symbol whose index resides in the relocation entry.
Z
The size of the symbol whose index resides in the relocation entry.
SPARC: Relocation Types
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 7–13 SPARC: ELF Relocation Types
Name | Value | Field | Calculation |
---|---|---|---|
R_SPARC_NONE | 0 | None | None |
R_SPARC_8 | 1 | V-byte8 | S + A |
R_SPARC_16 | 2 | V-half16 | S + A |
R_SPARC_32 | 3 | V-word32 | S + A |
R_SPARC_DISP8 | 4 | V-byte8 | S + A - P |
R_SPARC_DISP16 | 5 | V-half16 | S + A - P |
R_SPARC_DISP32 | 6 | V-disp32 | S + A - P |
R_SPARC_WDISP30 | 7 | V-disp30 | (S + A - P) >> 2 |
R_SPARC_WDISP22 | 8 | V-disp22 | (S + A - P) >> 2 |
R_SPARC_HI22 | 9 | T-imm22 | (S + A) >> 10 |
R_SPARC_22 | 10 | V-imm22 | S + A |
R_SPARC_13 | 11 | V-simm13 | S + A |
R_SPARC_LO10 | 12 | T-simm13 | (S + A) & 0x3ff |
R_SPARC_GOT10 | 13 | T-simm13 | G & 0x3ff |
R_SPARC_GOT13 | 14 | V-simm13 | G |
R_SPARC_GOT22 | 15 | T-simm22 | G >> 10 |
R_SPARC_PC10 | 16 | T-simm13 | (S + A - P) & 0x3ff |
R_SPARC_PC22 | 17 | V-disp22 | (S + A - P) >> 10 |
R_SPARC_WPLT30 | 18 | V-disp30 | (L + A - P) >> 2 |
R_SPARC_COPY | 19 | None | Refer to the explanation following this table. |
R_SPARC_GLOB_DAT | 20 | V-word32 | S + A |
R_SPARC_JMP_SLOT | 21 | None | Refer to the explanation following this table. |
R_SPARC_RELATIVE | 22 | V-word32 | B + A |
R_SPARC_UA32 | 23 | V-word32 | S + A |
R_SPARC_PLT32 | 24 | V-word32 | L + A |
R_SPARC_HIPLT22 | 25 | T-imm22 | (L + A) >> 10 |
R_SPARC_LOPLT10 | 26 | T-simm13 | (L + A) & 0x3ff |
R_SPARC_PCPLT32 | 27 | V-word32 | L + A - P |
R_SPARC_PCPLT22 | 28 | V-disp22 | (L + A - P) >> 10 |
R_SPARC_PCPLT10 | 29 | V-simm13 | (L + A - P) & 0x3ff |
R_SPARC_10 | 30 | V-simm10 | S + A |
R_SPARC_11 | 31 | V-simm11 | S + A |
R_SPARC_HH22 | 34 | V-imm22 | (S + A) >> 42 |
R_SPARC_HM10 | 35 | T-simm13 | ((S + A) >> 32) & 0x3ff |
R_SPARC_LM22 | 36 | T-imm22 | (S + A) >> 10 |
R_SPARC_PC_HH22 | 37 | V-imm22 | (S + A - P) >> 42 |
R_SPARC_PC_HM10 | 38 | T-simm13 | ((S + A - P) >> 32) & 0x3ff |
R_SPARC_PC_LM22 | 39 | T-imm22 | (S + A - P) >> 10 |
R_SPARC_WDISP16 | 40 | V-d2/disp14 | (S + A - P) >> 2 |
R_SPARC_WDISP19 | 41 | V-disp19 | (S + A - P) >> 2 |
R_SPARC_7 | 43 | V-imm7 | S + A |
R_SPARC_5 | 44 | V-imm5 | S + A |
R_SPARC_6 | 45 | V-imm6 | S + A |
R_SPARC_HIX22 | 48 | V-imm22 | ((S + A) ^ 0xffffffffffffffff) >> 10 |
R_SPARC_LOX10 | 49 | T-simm13 | ((S + A) & 0x3ff) | 0x1c00 |
R_SPARC_H44 | 50 | V-imm22 | (S + A) >> 22 |
R_SPARC_M44 | 51 | T-imm10 | ((S + A) >> 12) & 0x3ff |
R_SPARC_L44 | 52 | T-imm13 | (S + A) & 0xfff |
R_SPARC_REGISTER | 53 | V-word32 | S + A |
R_SPARC_UA16 | 55 | V-half16 | S + A |
R_SPARC_GOTDATA_HIX22 | 80 | V-imm22 | ((S + A - GOT) >> 10) ^ ((S + A - GOT) >> 31) |
R_SPARC_GOTDATA_LOX10 | 81 | T-imm13 | ((S + A - GOT) & 0x3ff) | (((S + A - GOT) >> 31) & 0x1c00) |
R_SPARC_GOTDATA_OP_HIX22 | 82 | T-imm22 | (G >> 10) ^ (G >> 31) |
R_SPARC_GOTDATA_OP_LOX10 | 83 | T-imm13 | (G & 0x3ff) | ((G >> 31) & 0x1c00) |
R_SPARC_GOTDATA_OP | 84 | Word32 | Refer to the explanation following this table. |
R_SPARC_SIZE32 | 86 | V-word32 | Z + A |
Note –
Additional relocations are available for thread-local storage references. These relocations are covered in Chapter 8, Thread-Local Storage.
Some relocation types have semantics beyond simple calculation.
R_SPARC_GOT10
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.
R_SPARC_GOT13
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.
R_SPARC_GOT22
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.
R_SPARC_WPLT30
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.
R_SPARC_COPY
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.
R_SPARC_GLOB_DAT
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.
R_SPARC_JMP_SLOT
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.
R_SPARC_RELATIVE
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.
R_SPARC_UA32
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.
R_SPARC_LM22
Resembles R_SPARC_HI22, except that the relocation truncates rather than validates.
R_SPARC_PC_LM22
Resembles R_SPARC_PC22, except that the relocation truncates rather than validates.
R_SPARC_HIX22
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.
R_SPARC_LOX10
Used with R_SPARC_HIX22. Similar to R_SPARC_LO10, but always sets bits 10 through 12 of the linked value.
R_SPARC_L44
Used with the R_SPARC_H44 and R_SPARC_M44 relocation types to generate a 44-bit absolute addressing model.
R_SPARC_REGISTER
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.
R_SPARC_GOTDATA_OP_HIX22, R_SPARC_GOTDATA_OP_LOX10, and R_SPARC_GOTDATA_OP
These relocations provide for code transformations.
64-bit SPARC: Relocation Types
The following notation, used in relocation calculation, is unique to 64–bit SPARC.
O
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 7–14 64-bit SPARC: ELF Relocation Types
Name | Value | Field | Calculation |
---|---|---|---|
R_SPARC_HI22 | 9 | V-imm22 | (S + A) >> 10 |
R_SPARC_GLOB_DAT | 20 | V-xword64 | S + A |
R_SPARC_RELATIVE | 22 | V-xword64 | B + A |
R_SPARC_64 | 32 | V-xword64 | S + A |
R_SPARC_OLO10 | 33 | V-simm13 | ((S + A) & 0x3ff) + O |
R_SPARC_DISP64 | 46 | V-xword64 | S + A - P |
R_SPARC_PLT64 | 47 | V-xword64 | L + A |
R_SPARC_REGISTER | 53 | V-xword64 | S + A |
R_SPARC_UA64 | 54 | V-xword64 | S + A |
R_SPARC_H34 | 85 | V-imm22 | (S + A) >> 12 |
R_SPARC_SIZE64 | 87 | V-xword64 | Z + A |
The following relocation type has semantics beyond simple calculation.
R_SPARC_OLO10
Resembles R_SPARC_LO10, except that an extra offset is added to make full use of the 13-bit signed immediate field.
32-bit x86: Relocation Types
The relocations that are listed in the following table are defined for 32–bit x86.
Table 7–15 32-bit x86: ELF Relocation Types
Name | Value | Field | Calculation |
---|---|---|---|
R_386_NONE | 0 | None | None |
R_386_32 | 1 | word32 | S + A |
R_386_PC32 | 2 | word32 | S + A - P |
R_386_GOT32 | 3 | word32 | G + A |
R_386_PLT32 | 4 | word32 | L + A - P |
R_386_COPY | 5 | None | Refer to the explanation following this table. |
R_386_GLOB_DAT | 6 | word32 | S |
R_386_JMP_SLOT | 7 | word32 | S |
R_386_RELATIVE | 8 | word32 | B + A |
R_386_GOTOFF | 9 | word32 | S + A - GOT |
R_386_GOTPC | 10 | word32 | GOT + A - P |
R_386_32PLT | 11 | word32 | L + A |
R_386_16 | 20 | word16 | S + A |
R_386_PC16 | 21 | word16 | S + A - P |
R_386_8 | 22 | word8 | S + A |
R_386_PC8 | 23 | word8 | S + A - P |
R_386_SIZE32 | 38 | word32 | Z + A |
Note –
Additional relocations are available for thread-local storage references. These relocations are covered in Chapter 8, Thread-Local Storage.
Some relocation types have semantics beyond simple calculation.
R_386_GOT32
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.
R_386_PLT32
Computes the address of the symbol's procedure linkage table entry and instructs the link-editor to create a procedure linkage table.
R_386_COPY
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.
R_386_GLOB_DAT
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.
R_386_JMP_SLOT
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.
R_386_RELATIVE
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.
R_386_GOTOFF
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.
R_386_GOTPC
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.
x64: Relocation Types
The relocations that are listed in the following table are defined for x64.
Table 7–16 x64: ELF Relocation Types
Name | Value | Field | Calculation |
---|---|---|---|
R_AMD64_NONE | 0 | None | None |
R_AMD64_64 | 1 | word64 | S + A |
R_AMD64_PC32 | 2 | word32 | S + A - P |
R_AMD64_GOT32 | 3 | word32 | G + A |
R_AMD64_PLT32 | 4 | word32 | L + A - P |
R_AMD64_COPY | 5 | None | Refer to the explanation following this table. |
R_AMD64_GLOB_DAT | 6 | word64 | S |
R_AMD64_JUMP_SLOT | 7 | word64 | S |
R_AMD64_RELATIVE | 8 | word64 | B + A |
R_AMD64_GOTPCREL | 9 | word32 | G + GOT + A - P |
R_AMD64_32 | 10 | word32 | S + A |
R_AMD64_32S | 11 | word32 | S + A |
R_AMD64_16 | 12 | word16 | S + A |
R_AMD64_PC16 | 13 | word16 | S + A - P |
R_AMD64_8 | 14 | word8 | S + A |
R_AMD64_PC8 | 15 | word8 | S + A - P |
R_AMD64_PC64 | 24 | word64 | S + A - P |
R_AMD64_GOTOFF64 | 25 | word64 | S + A - GOT |
R_AMD64_GOTPC32 | 26 | word32 | GOT + A + P |
R_AMD64_SIZE32 | 32 | word32 | Z + A |
R_AMD64_SIZE64 | 33 | word64 | Z + A |
Note –
Additional relocations are available for thread-local storage references. These relocations are covered in Chapter 8, 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.
R_AMD64_GOTPCREL
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.
R_AMD64_32
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.
R_AMD64_32S
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.
R_AMD64_8, R_AMD64_16, R_AMD64_PC16, and R_AMD64_PC8
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.
- © 2010, Oracle Corporation and/or its affiliates
以上是关于ELF重定位:Relocation Sections的主要内容,如果未能解决你的问题,请参考以下文章