3.2 Link Order
When using MPLAB XC8, the order in which the linker will process object files can differ to that used by AVR-GCC. This might affect how sections within those files are positioned in memory.
Description
Whether building a project using an IDE or make files on the command-line, you can specify the order in which source files are presented to the MPLAB XC8 compiler driver. The driver then passes to the linker application the object files generated from these source files, any object or library archives specified in the project or on the command line, and any object or library archives automatically linked in by the compiler driver. This operation is similar for both compilers; however, even though files are passed to each linker in the same order, the way in which these files are internally processed by the linker and hence the order in which the sections within those files are allocated memory can differ.
With MPLAB XC8, sections are allocated using a best-fit allocator (BFA) when the
-mrelax
option has not been specified. When this option has
been specified, it uses a section-references-based heuristic to increase the
linker's chance of shortening long jumps and calls. In either case, functions and
objects might be processed in different orders and hence allocated at different
addresses to those allocated when building the same project with AVR-GCC.
Migration
.text
output section could be specified as
follows: .text // Output section
{
...
*objfile1.o(.text) *objfile1.o(.text.*) // All .text and .text.* from objfile1.o
*objfile2.o(.text) *objfile2.o(.text.*) // All .text and .text.* from objfile2.o
...
}
When processed, .text
and .text.*
sections from objfile1.o
are allocated first, followed by those
from objfile2.o
, leaving the linker to allocate however it chooses
only those sections that have not been specified by the linker script.The linker script will need to define similar output sections for the other input sections and must also specify the names of object files or library archives that are automatically linked in by the compiler driver.