SLAU131Y October 2004 – June 2021
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 MSP430 device for which the assembler generates relocation entries.
1 ** Generating Relocation Entries **
2
3 .ref X
4 .def Y
5
6 000000 .text
7 000000 5A0B ADD.W R10, R11
8 000002 4B82 MOV.W R11, &X
000004 0000!
9 000006 4030 BR #Y
000008 000A!
10
11 00000a 5B0C Y ADD.W R11, R12
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 8. The assembler generates two relocation entries: one for X and one for Y. The reference to X is an external reference and the reference to Y is to an internally defined relocatable symbol (both are indicated by the ! character in the listing).
After the code is linked, suppose that X is relocated to address 0x0800. Suppose also that the .text section is relocated to begin at address 0x0600; Y now has a relocated value of 0x0608. The linker uses the relocation entry for the reference to X to patch the branch instruction in the object code:
|
becomes |
|