12.1 Memory Addresses and Relocatable Code

For most applications it is preferable to write fully relocatable source code, thus allowing the linker to determine the exact addresses in memory where functions and variables are placed. The final address of external symbols in data memory and program memory can be determined from the link map output, as shown in this excerpt:

...
External Symbols in Data Memory (by address):
                 0x0802 __curbrk
                 0x0804 _Stdin
                 0x082c _Stdout
                 0x0854 _Stderr
                 0x087c _Files
                 0x088c _Aldata
                 0x0890 _Size_block
...
External Symbols in Data Memory (by name):
                 0x0802 __curbrk
                 0x088c _Aldata
                 0x087c _Files
                 0x0890 _Size_block
                 0x0854 _Stderr
                 0x0804 _Stdin
                 0x082c _Stdout
...

In some cases it is necessary for the programmer to specify the address where a certain variable or function should be located. Traditionally this is done by creating a user-defined section and writing a custom linker script. The MPLAB XC32 assembler and compiler provide a set of attributes that can be used to specify absolute addresses and memory spaces directly in source code. When these attributes are used, custom linker scripts are not required.

Note: By specifying an absolute address, the programmer assumes the responsibility to ensure the specified address is reasonable and available. If the specified address is out of range, or conflicts with a statically allocated resource, a link error will occur.