Relocation (Linker and Libraries Guide) (2024)

Relocation

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 properdestination address at execution. In other words, relocatable files must have information that describes how to modify their section contents, thus allowing executable and shared object files to hold theright information for a process's program image. Relocation entries are these data.

Relocation entries can have the following structure (defined in 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;} Elf32_Rela;
r_offset

This member gives the location at which to apply the relocation action. For a relocatable file,the value is the byte offset from the beginning of the section to the storage unit affected by the relocation. For an executable file or a shared object, the value is the virtual address of the storageunit affected by the relocation.

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 will hold the symbol table index of the function being called. If the index is STN_UNDEF, the undefined symbol index, the relocationuses 0 as the symbol value. Relocation types are processor-specific; descriptions of their behavior appear below. When the text below refers to a relocation entry's relocation type or symbol table index,it means the result of applying ELF32_R_TYPE or ELF32_R_SYM, respectively, to the entry's r_info member:


#define ELF32_R_SYM(i) ((i)>>8)#define ELF32_R_TYPE(i) ((unsigned char)(i))#define ELF32_R_INFO(s, t) (((s)<<8)+(unsigned char)(t))#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 Elf64_Rel and Elf64_Rela structures, the r_info field is further broken down into an 8-bit type identifier and a 24-bittype dependent data field:


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

As shown above, only Elf32_Rela entries contain an explicit addend. Entries of type Elf32_Rel store an implicit addend in the location to be modified. 32-bitSPARCuses Elf32_Rela entries, 64-bit SPARC uses Elf64_Rela entries and IA uses Elf32_Rel entries.

A relocation section references two other sections: a symbol table and a section to modify. The section header's sh_info and sh_link members,described in "Sections" earlier, specify these relationships. Relocation entries for different object files have slightly different interpretations for the r_offset member.

  • In relocatable files, r_offset holds a section offset. That is, the relocation section itself describes how to modify another section in the file; relocation offsetsdesignate a storage unit within the second section.

  • In executable and shared object files, r_offset holds a virtual address. To make these files' relocation entries more useful for the runtime linker, the sectionoffset (file interpretation) gives way to a virtual address (memory interpretation).

Although the interpretation of r_offset changes for different object files to allow efficient access by the relevant programs, the relocation types' meanings stay the same.

Relocation Types (Processor-Specific)

On SPARC, relocation entries describe how to alter the following instruction and data fields (bit numbers appear in the lower box corners):

Relocation (Linker and Libraries Guide) (1)

On IA, relocation entries describe how to alter the following instruction and data fields (bit numbers appear in the lower box corners):

Relocation (Linker and Libraries Guide) (2)

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 IA architecture:

Relocation (Linker and Libraries Guide) (3)

64-bit SPARC also includes a 64-bit word field:

Relocation (Linker and Libraries Guide) (4)

Calculations below 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 formthe output. It first decides how to combine and locate the input files, then updates the symbol values, and finally performs the relocation. Relocations applied to executable or shared object files aresimilar and accomplish the same result. Descriptions below use the following notation:

A

Means the addend used to compute the value of the relocatable field

B

Means the base address at which a shared object is loaded into memory during execution. Generally, a shared object file is built with a 0 basevirtual address, but the execution address is different. See "Program Header" for more information about the base address

G

Means 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)" for more information.

GOT

Means the address of the global offset table. See "Global Offset Table (Processor-Specific)" for more information.

L

Means the place (section offset or address) of the procedure linkage table entry for a symbol. A procedure linkage tableentry redirects a function call to the proper destination. The link-editor builds the initial procedure linkage table, and the runtime linker modifies the entries during execution. See "Procedure Linkage Table (Processor-Specific)"for more information.

P

Means the place (section offset or address) of the storage unit being relocated (computed using r_offset).

S

Means the value of the symbol whose index resides in the relocation entry.

SPARC relocation entries apply to bytes (byte8), half-words (half16), or words (the others). IA relocation entries apply to words.In any case, 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 andhow to calculate their values.

32-bit SPARC uses only Elf32_Rela relocation entries with explicit addends and 64-bit SPARC uses Elf64_Rela. Thusthe r_addend member serves as the relocation addend. IA uses only Elf32_Rel relocation entries, the field to be relocated holds the addend. In all cases theaddend and the computed result use the same byte order.

SPARC: Relocation Types

Note -

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 typecan verify (V) the value fits or truncate (T) the result. As an example, V-simm13 means that the computed value can not have significant, nonzerobits outside the simm13 field.

Table 7-26 SPARC: Relocation Types

Name

Value

Field

Calculation

R_SPARC_NONE

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

None

R_SPARC_GLOB_DAT

20

V-word32

S + A

R_SPARC_JMP_SLOT

21

None

See R_SPARC_JMP_SLOT,

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_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

Some relocation types have semantics beyond simple calculation:

R_SPARC_GOT10

This relocation type resembles R_SPARC_LO10, except that it refers to theaddress of the symbol's global offset table entry and additionally instructs the link-editor to build a global offset table.

R_SPARC_GOT13

This relocation type resembles R_SPARC_13, except that it refers to the address of the symbol's global offsettable entry and additionally instructs the link-editor to build a global offset table.

R_SPARC_GOT22

This relocation type resembles R_SPARC_22, except that it refers to the address of the symbol's global offsettable entry and additionally instructs the link-editor to build a global offset table.

R_SPARC_WPLT30

This relocation type resembles R_SPARC_WDISP30, except that it refers to the address of the symbol's procedurelinkage table entry and additionally instructs the link-editor to build a procedure linkage table.

R_SPARC_COPY

The link-editor creates this relocation type for dynamic linking. Its 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 symbolto the location specified by the offset. See "Copy Relocations" for more details.

R_SPARC_GLOB_DAT

This relocation type resembles R_SPARC_32, except that it sets a global offset table entry to the addressof the specified symbol. The special relocation type allows you to determine the correspondence between symbols and global offset table entries.

R_SPARC_JMP_SLOT

The link-editor creates this relocation type for dynamic linking. Its offset member gives the location of aprocedure linkage table entry. The runtime linker modifies the procedure linkage table entry to transfer control to the designated symbol address.

R_SPARC_RELATIVE

The link-editor creates this relocation type for dynamic linking. Its offset member gives the location within a shared objectthat 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 0 for the symbol table index.

R_SPARC_UA32

This relocation type resembles R_SPARC_32, except that it refers to an unaligned word. That is, the word tobe relocated must be treated as four separate bytes with arbitrary alignment, not as a word aligned according to the architecture requirements.

64-bit SPARC: Relocation Types

Note -

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 typecan verify (V) the value fits or truncate (T) the result. As an example, V-simm13 means that the computed value can not have significant, nonzerobits outside the simm13 field.

Table 7-27 64-bit SPARC: Relocation Types

Name

Value

Field

Calculation

R_SPARC_NONE

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

V-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

None

R_SPARC_GLOB_DAT

20

V-xword64

S + A

R_SPARC_JMP_SLOT

21

None

See R_SPARC_JMP_SLOT,

R_SPARC_RELATIVE

22

V-xword64

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-disp32

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_64

32

V-xword64

S + A

R_SPARC_OLO10

33

V-simm13

((S + A) & 0x3ff) + O

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) & 0x7f

R_SPARC_5

44

V-imm5

(S + A) & 0x1f

R_SPARC_6

45

V-imm6

(S + A) & 0x3f

R_SPARC_DISP64

46

V-xword64

S + A - P

R_SPARC_PLT64

47

V-xword64

L +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-xword64

S + A

R_SPARC_UA64

54

V-xword64

S + A

R_SPARC_UA16

55

V-half16

S + A

Some relocation types have semantics beyond simple calculation:

R_SPARC_GOT10

This relocation type resembles R_SPARC_LO10, except that it refers to theaddress of the symbol's global offset table entry and additionally instructs the link-editor to build a global offset table.

R_SPARC_GOT13

This relocation type resembles R_SPARC_13, except that it refers to the address of the symbol's global offsettable entry and additionally instructs the link-editor to build a global offset table.

R_SPARC_GOT22

This relocation type resembles R_SPARC_22, except that it refers to the address of the symbol's global offsettable entry and additionally instructs the link-editor to build a global offset table.

R_SPARC_WPLT30

This relocation type resembles R_SPARC_WDISP30, except that it refers to the address of the symbol's procedurelinkage table entry and additionally instructs the link-editor to build a procedure linkage table.

R_SPARC_COPY

The link-editor creates this relocation type for dynamic linking. Its 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 symbolto the location specified by the offset. See "Copy Relocations" for more details.

R_SPARC_GLOB_DAT

This relocation type resembles R_SPARC_64, except that it sets a global offset table entry to the addressof the specified symbol. The special relocation type allows you to determine the correspondence between symbols and global offset table entries.

R_SPARC_JMP_SLOT

The link-editor creates this relocation type for dynamic linking. Its offset member gives the location ofa procedure linkage table entry. The runtime linker modifies the procedure linkage table entry to transfer control to the designated symbol address.

R_SPARC_RELATIVE

The link-editor creates this relocation type for dynamic linking. Its offset member gives the location within a shared objectthat 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 0 for the symbol table index.

R_SPARC_UA32

This relocation type resembles R_SPARC_32, except that it refers to an unaligned word. That is, the word tobe relocated must be treated as four separate bytes with arbitrary alignment, not as a word aligned according to the architecture requirements.

R_SPARC_OLO10

This relocation type resembles R_SPARC_LO10, except that an extra offset is added to make full use of the 13-bitsigned immediate field.

R_SPARC_HH22

This relocation type is used by the assembler when it sees an instruction of the form "imm22-instruction ... %hh(absolute)...".

R_SPARC_HM10

This relocation type is generated by the assembler when it sees an instruction of the form "simm13-instruction ... %hm(absolute)..."

R_SPARC_LM22

This relocation type is used by the assembler when it sees an instruction of the form "imm22-instruction ... %lm(absolute)...". This resembles R_SPARC_HI22, except it truncates rather than validates.

R_SPARC_PC_HH22

This relocation type is used by the assembler when it sees an instruction of the form "imm22-instruction ... %hh(pc-relative)...".

R_SPARC_PC_HM10

This relocation type is generated by the assembler when it sees an instruction of the form "simm13-instruction ...%hm(pc-relative) ...".

R_SPARC_PC_LM22

This relocation type is used by the assembler when it sees an instruction of the form "imm22-instruction ... %lm(pc-relative)...". This resembles R_SPARC_PC22, except that it truncates rather than validates.

R_SPARC_7

This relocation type is used by the assembler for 7 bit software trap numbers.

R_SPARC_HIX22

This relocation type is used with R_SPARC_LOX10 for executables that will be confined to the uppermost 4GBof 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..12 of the linkedvalue.

R_SPARC_H44

This relocation type is used by the assembler when it sees an instruction of the form "imm44-instruction ... %h44(absolute)..".

R_SPARC_M44

This relocation type is generated by the assembler when it sees an instruction of the form "imm44-instruction ... %m44(absolute)...".

R_SPARC_L44

This relocation type is used with the R_SPARC_H44 and R_SPARC_M44 relocation types to generatea 44-bit absolute addressing model. The assembler will generate this type when it sees an instruction of the form "imm44-instruction ... %l44(absolute) ...".

R_SPARC_REGISTER

This relocation type is used to initialize a register symbol. Its offset member contains the register number to be initialized.There must be a corresponding register symbol for this register of type SHN_ABS.

IA: Relocation Types

Table 7-28 IA: Relocation Types

Name

Value

Field

Calculation

R_386_NONE

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

none

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

Some relocation types have semantics beyond simple calculation:

R_386_GOT32

This relocation type computes the distance from the base of the global offset table to the symbol'sglobal offset table entry. It also instructs the link-editor to build a global offset table.

R_386_PLT32

This relocation type computes the address of the symbol's procedure linkage table entry and instructs the link-editor to build aprocedure linkage table.

R_386_COPY

The link-editor creates this relocation type for dynamic linking. Its offset member refers to a location in a writable segment. Thesymbol 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 symbolto the location specified by the offset. See "Copy Relocations".

R_386_GLOB_DAT

This relocation type is used to set a global offset table entry to the address of the specified symbol. The special relocationtype lets one determine the correspondence between symbols and global offset table entries.

R_386_JMP_SLOT

The link-editor creates this relocation type for dynamic linking. Its offset member gives the location of a procedure linkagetable entry. The runtime linker modifies the procedure linkage table entry to transfer control to the designated symbol address.

R_386_RELATIVE

The link-editor creates this relocation type for dynamic linking. Its offset member gives the location within a shared objectthat 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 0 for the symbol table index.

R_386_GOTOFF

This relocation type computes the difference between a symbol's value and the address of the global offset table. It also instructsthe link-editor to build the global offset table.

R_386_GOTPC

This relocation type resembles R_386_PC32, except it uses the address of the global offset table in its calculation.The symbol referenced in this relocation normally is _GLOBAL_OFFSET_TABLE_, which also instructs the link-editor to build the global offset table.

Relocation (Linker and Libraries Guide) (2024)
Top Articles
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated:

Views: 5677

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.