Vault 7: Projects
This publication series is about specific projects related to the Vault 7 main publication.
Linking Programs
2012-2013 Microchip Technology Inc. DS50002071C-page 225
20.3 REPLACING LIBRARY SYMBOLS
The MPLAB XC16 C Compiler comes with a librarian which allows you to unpack a
library file and replace modules with your own modified versions. See the MPLAB
XC16 Assembler, Linker and Utilities Users Guide (DS52106). However, you can eas-
ily replace a library module that is linked into your program without having to do this.
If you add to your project a source file which contains the definition for a routine with
the same name as a library routine, then the library routine will be replaced by your rou-
tine.
When trying to resolve a symbol (a function name, or variable name, for example) the
compiler first scans all the source modules for the definition. Only if it cannot resolve
the symbol in these files does it then search the library files.
If the symbol is defined in a source file, the compiler will never actually search the librar-
ies for this symbol and no error will result even if the symbol was present in the library
files. This may not be true if a symbol is defined twice in source files and an error may
result if there is a conflict in the definitions.
Another method is to use the weak attribute when declaring a symbol. A weak symbol
may be superseded by a global definition. When weak is applied to a reference to an
external symbol, the symbol is not required for linking.
The weak attribute may be applied to functions as well as variables. Code may be writ-
ten such that the function will be used only if it is linked in from some other module.
Deciding whether or not to use the feature becomes a link-time decision, not a compile
time decision.
For more information on the weak attribute, see Section 8.12 Variable Attributes.
20.4 LINKER-DEFINED SYMBOLS
The 16-bit linker defines several symbols that may be used in your C code develop-
ment. Please see the MPLAB XC16 Assembler, Linker and Utilities Users Guide
(DS52106) for more information.
A useful address symbol, _PROGRAM_END, is defined in program memory to mark the
highest address used by a CODE or PSV section. It should be referenced with the
address operator (&) in a built-in function call that accepts the address of an object in
program memory. This symbol can be used by applications as an end point for check-
sum calculations.
For example:
unsigned int end_page, end_offset;
_prog_addressT big_addr;
end_page = __builtin_tblpage(&_PROGRAM_END);
end_offset = __builtin_tbloffset(&_PROGRAM_END);
_init_prog_address(big_addr, _PROGRAM_END);
Protego_Release_01_05-Related-OEM-Documentation-MPLAB-XC16-C-Compiler.pdf