4.14.2 Changing and Linking the Allocated Section

The location of the default sections in which functions and objects are placed can be changed via driver options. The default sections the compiler uses to hold objects and code are listed in 4.14.1 Compiler-Generated Sections.

The __section() specifier allows you to have a object or function redirected into a user-define section. This allows you to relocate individual objects or functions.

Objects that use the __section() specifier will be cleared or initialized in the usual way by the runtime startup code.

The following are examples of a object and function allocated to a non-default section. To use the __section() specifier, the CCI (see 3.6.3.4 Ext Option) must be enabled.

int __section("myBss") foobar;
int __section("myText") helper(int mode) { /* ... */ }
You can link these sections by using the -Wl,--section-start=section=addr option when building (linking) your program, provided that the linker script has already defined an output section with the same name. If this is not the case, then you might need to copy the default linker script, modify it appropriately, and specify it using the -Wl,-Tscript driver option. The modification must define an output section using the same name as the section you have specified. The following, for example, will allocate any input sections called myBss to the output section with the same name.
myBss :
  {
    KEEP(*(myBss))
  }

Note that you need to use an offset of 0x800000 for any address that is in the data space. For example, suppose you wish to place the new myBss section, created above, at SRAM address 0x300, use an option similar to:

-Wl,--section-start=myBss=0x800300

The -Wl,-Tsection=addr option can be used to position standard sections, like the .text, .data and .bss sections; however, since a Best Fit Allocator (BFA) is used with MPLAB XC8, these sections are not commonly used by the compiler to store objects or code.

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.