SPRUIG8J January 2018 – March 2024
Weak symbol references may or may not have a definition after the final link is performed. If a symbol is undefined, its address is considered to be 0. C/C++ code must check the address of weak references to make sure the value is not 0 before attempting to use the contents of that variable.
extern __attribute__((weak)) unsigned char * foo;
if (&foo != 0)
*foo = 1;
If the linker symbol corresponding to foo might not have a valid address (for instance, because the symbol contains an integer value instead of an address) or might be beyond the 2 GB reach of PC-relative addressing, use the _symval built-in operator as follows:
extern __attribute__((weak)) unsigned char * foo;
if (_symval(&foo) != 0)
*foo = 1;