Example 1. Code That Generates Relocation Entries

1 ********************************************* 2 ** Generating Relocation Entries ** 3 ********************************************* 4 .ref X 5 .def Y 6 00000000 .text 7 00000000 E0921003 ADDS R1, R2, R3 8 00000004 0A000001 BEQ Y 9 00000008 E1C410BE STRH R1, [R4, #14] 10 0000000c EAFFFFFB! B X ; generates a relocation entry 11 00000010 E0821003 Y: ADD R1, R2, R3

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 (relative to address 0 in the .text section). 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 0x10014. Suppose also that the .text section is relocated to begin at address 0x10000; Y now has a relocated value of 0x10010. The linker uses the relocation entry for the reference to X to patch the branch instruction in the object code:

EAFFFFFB! B X becomes EA000000