ARM Compiler toolchain Using the Linker - ARM Information Center
ARM Compiler toolchain Using the Linker - ARM Information Center
ARM Compiler toolchain Using the Linker - ARM Information Center
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
4.22 About weak references and definitions<br />
Image structure and generation<br />
Weak references and definitions provide additional flexibility in <strong>the</strong> way <strong>the</strong> linker includes<br />
various functions and variables in a build. These references are typically to library functions.<br />
Weak references<br />
If <strong>the</strong> linker cannot resolve normal, non-weak, references to symbols included in<br />
<strong>the</strong> link, it attempts to do so by finding <strong>the</strong> symbol in a library:<br />
• If it is unable to find such a reference, <strong>the</strong> linker reports an error.<br />
• If such a reference is resolved, <strong>the</strong> section it is resolved to is marked as<br />
used. This ensures <strong>the</strong> section is not removed by <strong>the</strong> linker as an unused<br />
section. Each non-weak reference must be resolved by exactly one<br />
definition. If <strong>the</strong>re are multiple definitions, <strong>the</strong> linker reports an error.<br />
Function or variable declarations in C source files can be marked with <strong>the</strong> __weak<br />
qualifier. As with extern, this qualifier tells <strong>the</strong> compiler that a function or<br />
variable is declared in ano<strong>the</strong>r source file. Because <strong>the</strong> definition of this function<br />
or variable might not be available to <strong>the</strong> compiler, it creates a weak reference to<br />
be resolved by <strong>the</strong> linker.<br />
The linker does not load an object from a library to resolve a weak reference. It is<br />
able to resolve <strong>the</strong> weak reference only if <strong>the</strong> definition is included in <strong>the</strong> image<br />
for o<strong>the</strong>r reasons. The weak reference does not cause <strong>the</strong> linker to mark <strong>the</strong><br />
section containing <strong>the</strong> definition as used, so it might be removed by <strong>the</strong> linker as<br />
unused. The definition might already exist in <strong>the</strong> image for several reasons:<br />
• The symbol is strongly referenced somewhere else in <strong>the</strong> code.<br />
• The symbol definition exists in <strong>the</strong> same ELF section as a symbol definition<br />
that is included for any of <strong>the</strong>se reasons.<br />
• The symbol definition is in a section that has been specified using --keep,<br />
or contains an ENTRY point.<br />
• The symbol definition is in an object file included in <strong>the</strong> link and <strong>the</strong><br />
--no_remove option is used. The object file is not referenced from a library<br />
unless that object file within <strong>the</strong> library is explicitly included on <strong>the</strong> linker<br />
command-line.<br />
In summary, a weak reference is resolved if <strong>the</strong> definition is already included in<br />
<strong>the</strong> image, but it does not determine if that definition is included.<br />
An unresolved weak function call is replaced with ei<strong>the</strong>r:<br />
• A no-operation instruction, NOP.<br />
• A branch with link instruction, BL, to <strong>the</strong> following instruction. That is, <strong>the</strong><br />
function call just does not happen.<br />
Weak definitions<br />
A function definition, or an exported label in assembler, can also be marked as<br />
weak, as can a variable definition. In this case, a weak symbol definition is<br />
created in <strong>the</strong> object file.<br />
A weak definition can be used to resolve any reference to that symbol in <strong>the</strong> same<br />
way as a normal definition. However, if ano<strong>the</strong>r non-weak definition of that<br />
symbol exists in <strong>the</strong> build, <strong>the</strong> linker uses that definition instead of <strong>the</strong> weak<br />
definition, and does not produce an error due to multiply-defined symbols.<br />
<strong>ARM</strong> DUI 0474C Copyright © 2010-2011 <strong>ARM</strong>. All rights reserved. 4-32<br />
ID080411 Non-Confidential