Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual

Product codes
SW006021-1
Page of 518
Linker
 2012 Microchip Technology Inc.
DS52053B-page 285
7.3
RELOCATION AND PSECTS
This section looks at the input files that the linker has to work with.
The linker can read both relocatable object files and object-file libraries (.lib exten-
sion). The library files are a collection of object files packaged into a single unit, so 
essentially we only need consider the format of object files.
Each object file consists of a number of records. Each record has a type that indicates 
what sort of information it holds. Some record types hold general information about the 
target device and its configuration, other records types may hold data, and others, 
program debugging information, for example.
A lot of the information in object files relates to psects. Psects are an assembly domain 
construct and are essentially a block of something, either instructions or data. Every-
thing that contributes to the program is located in a psect. See 
Section 6.4.8 “Program Sections” for an introductory guide. There is a particular 
record type that is used to hold the data in psects. The bulk of each object file consists 
of psect records containing the executable code and variables etc.
We are now in a position to look at the fundamental tasks the linker performs, which 
are:
• combining all the relocatable object files into one
• relocation of psects contained in the object files into memory
• fixup of symbolic references in the psects
There is typically at least two object files that are passed to the linker. One will be pro-
duced from all the C code in the project, including C library code. There is only one of 
these files since the code generator compiles and combines all the C code of the pro-
gram and produces just the one assembly output. The other file passed to the linker will 
be the object code produced from the runtime startup code, see 
Section 4.4.2 “Startup and Initialization”.
If there are assembly source files in the project, then there will also be one object file 
produced for each source file and these will be passed to the linker. Existing object files, 
or object file libraries can also be specified in a project, and if present, these will also 
be passed to the linker.
The output of the linker is also an object file, but there is only ever one file produced. 
The file is absolute since relocation will have been performed by the linker. The output 
file will consist of the information from all input object files merged together.
Relocation consists of placing the psect data into the memory of the target device.
The target device memory specification is passed to the linker by the way of linker 
options. These options are generated by the command-line driver, xc8. There are no 
linker scripts or means of specifying options in any source file. The default linker 
options rarely need adjusting, but can be changed, if required and with caution, using 
the driver option -L-, see Section 4.8.7 “-L-: Adjust Linker Options Directly”.
Once psects are placed at actual memory locations, symbolic references made in the 
psects data can be replaced with absolute values. This is a process called fixup.
For each psect record in the object file, there is a corresponding relocation record that 
indicates which bytes (or bits) in the psect record need to be adjusted once relocation 
is complete. The relocation records also specify how the values are to be determined. 
A linker fixup overflow error can occur if the value determined by the linker is too large 
to fit in the “hole” reserved for the value in the psect. See Section “(477) fixup 
overflow in expression (location 0x* (0x*+*), size *, value 0x*)   (Linker)”
 for 
information on finding the cause of these errors.