3.3 Moving Data and Code Sections

Unlike the AVR GCC compiler, which typically produces a single .text, .data and .bss output section, this is not the case when using MPLAB XC8. Using the --section-start option to move code and data with MPLAB XC8 will not work reliably.

Description

One consequence of the best-fit allocator (BFA) employed by MPLAB XC8 is that most objects and code are not placed in the standard .text, .data and .bss output sections . The compiler only maps the vector table, .dinit table and some special sections into the .text output section, leaving other .text* input sections unmapped. The linker implementation then creates an output section for each unmapped input section and proceeds to allocate them as it sees fit.

If you need to explicitly locate any of these standard sections, the -Wl,--section-start option will not work as expected. Additionally, any tools used to dump or analyze the output after linking and that expect to see a single .text section for all the program code might also fail.

Provided you are using the latest MPLAB XC8 compiler, the -Wl,--section-start option can be used to position sections with a user-defined name (sections not called .text, .data and .bss) and without any modification to the linker script. User-defined sections can be created using the __section() specifier.

Migration

The -Wl,-Tsection=addr option can instead be used to position standard sections, like the .text, .data and .bss sections at the specified address.

Although the -Wl,-Ttext option will move the .text section to the offset specified, this option also sets the starting address from which the linker begins program memory allocation for all code (even that handled by the BFA). Thus, when this option is used, the .text section will start at the exact offset specified; other code sections will be located after this. Similarly, the -Wl,-Tdata option also sets the starting address from which the linker begins data (both initialized and uninitialized) allocation.