Example 1. Code That 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 Example 1, 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:

00000012 B X becomes 0fffe012
0180082A MVKL Y becomes 01B9082A
0180006A MVKH Y becomes 1860006A

Relocations are symbol-relative rather than section-relative. This means that the relocation in Example 1 generated for 'Y' would refer to the symbol 'Y' and resolve the value for 'Y' in the opcode based on where the definition of 'Y' ends up.