SPRUI03E June 2015 – January 2023
The assembler treats each section as if it began at address 0. Of course, all sections cannot actually begin at address 0 in memory, so the linker must relocate sections. Relocations are symbol-relative rather than section-relative.
The linker can relocate sections by:
The linker uses relocation entries to adjust references to symbol values. The assembler creates a relocation entry each time a relocatable symbol is referenced. The linker then uses these entries to patch the references after the symbols are relocated. The following example contains a code fragment for a TMS320C6000 device for which the assembler generates relocation entries.
1 .global X
2 00000000 00000012! Z: B X ; Uses an external relocation
3 00000004 0180082A' MVKL Y,B3 ; Uses an internal relocation
4 00000008 0180006A' MVKH Y,B3 ; Uses an internal relocation
5 0000000C 00004000 NOP 3
6
7 00000010 0001E000 Y: IDLE
8 00000014 00000212 B Y
9 00000018 00008000 NOP 5
In the previous example, both symbols X and Y are relocatable. Y is defined in the .text section of this module; X is defined in another module. When the code is assembled, X has a value of 0 (the assembler assumes all undefined external symbols have values of 0), and Y has a value of 16 . The assembler generates two relocation entries: one for X and one for Y. The reference to X is an external reference (indicated by the ! character in the listing). The reference to Y is to an internally defined relocatable symbol (indicated by the ' character in the listing).
After the code is linked, suppose that X is relocated to address 0x7100. Suppose also that the .text section is relocated to begin at address 0x7200; Y now has a relocated value of 0x7210. The linker uses the two relocation entries to patch the two references in the object code:
|
becomes |
|
|
becomes |
|
|
becomes |
|
Relocations are symbol-relative rather than section-relative. This means that the relocation generated for 'Y' in the example above would refer to the symbol 'Y' and resolve the value for 'Y' in the opcode based on where the definition of 'Y' ends up.