Advanced Embedded C Language: What are Weak Symbols and Weak References?

I am Lao Wen, an embedded engineer who loves learning.Follow me, let’s become better together!

<span><span>__attribute__</span></span> is a compiler directive, essentially a mechanism of <span><span>GNU C</span></span>, which provides certain attributes during declaration that take effect during the compilation phase for diversified error checking and advanced optimization.

It is used to modify variables, functions, parameters, methods, classes, etc., in <span><span>C</span></span>, <span><span>C++</span></span>, and <span><span>Objective-C</span></span>.

What are the benefits of using <span><span>__attribute__</span></span> properly?

(1) It provides context to the compiler, helping it optimize; proper usage can yield significant optimization effects.(2) The compiler generates some compilation warnings based on <span><span>__attribute__</span></span>, making the code more standardized.(3) It provides necessary annotations for code readers, aiding in understanding the intent of the code.In summary, <span><span>__attribute__</span></span> serves to provide context to the compiler; incorrect usage of <span><span>__attribute__</span></span> can lead to errors that are often hard to detect.

Strong Symbols and Weak Symbols

Many C language learners assume that the same variable or function cannot be defined in the same scope.This is somewhat biased; GNU C extends standard C, and in GCC, there is a distinction between strong symbols and weak symbols (where variables and functions are abstracted as symbols during compilation).Whether this feature is supported depends on different C language standards.For C/C++, the compiler defaults to treating functions and initialized global variables as strong symbols, while uninitialized global variables are treated as weak symbols.When programmers do not explicitly specify, the compiler has some default behaviors regarding strong and weak symbols, and developers can specify symbols using attribute((weak)) to declare a symbol as a weak symbol.Defining the same variable, when neither is a strong symbol, GCC will not report an error during compilation but will follow certain rules for selection:

  • When both are strong symbols, a redefinition error occurs:<span><span>redefinition of 'xxx'</span></span>
  • When one is strong and the other is weak, the value of the strong symbol is selected.
  • When both are weak, the symbol that occupies more space is chosen; this is understandable as the compiler does not know the programmer’s intent, and selecting the larger symbol at least avoids serious consequences like overflow or out-of-bounds errors.

Under default symbol type conditions, strong and weak symbols can coexist, as shown:

int x;
int x = 1;

The compilation will not report an error, and the value of x during compilation will be 1.Note that you can use <span><span>__attribute__((weak))</span></span> to convert a strong symbol to a weak symbol, but it cannot coexist with a strong symbol, as shown:

int __attribute__((weak)) x = 0;
int x = 1;

The compiler will report a redefinition error.

Strong References and Weak References

In addition to the distinction between strong symbols and weak symbols, GNU C has another feature: strong references and weak references.We know that the compiler is responsible for compiling source files into object files (i.e., binary files) during the compilation phase, and then the linker performs linking operations on all binary files.The compiler defaults all variables and functions as strong references, while programmers can use <span><span>__attribute__((weakref))</span></span><code><span><span> to declare a function.</span></span><span><span>Note that this is a declaration, not a definition; since it is a reference, it uses entities defined in other modules. For functions, we can write:</span></span><pre><code class="language-c">__attribute__((weakref)) void func(void);Then call <span><span>func()</span></span> in the function; if <span><span>func()</span></span><code><span><span> is not defined, then the value of </span></span><code><span><span>func</span></span><span><span> will be 0; if </span></span><code><span><span>func</span></span><span><span> is defined, the corresponding func will be called. In the book </span></span><span><span>Self-Improvement for Programmers</span></span><span><span>, it is written as follows:</span></span><pre><code class="language-c">__attribute__((weakref)) void func(void);
void main(void)
{
if(func)
{
func();
}
}
However, in modern compilation systems, this writing is incorrect; the compilation passes (with warning messages), but it is not correct:

warning: ‘weakref’ attribute should be accompanied with an ‘alias’ attribute [-Wattributes]

The warning indicates that weakref needs to be accompanied by an alias to function properly.

The Role of Strong/Weak Symbols and Strong/Weak References

This extension mechanism of weak symbols and weak references is very useful in library implementations.In libraries, we can use weak symbols and weak references, allowing users to define extension functions to override weak symbol functions.At the same time, we can define certain extension functions as weak references; when users need to use the extension function, they define it and link it into the program.If users do not define it, the linking will not report an error, making it very convenient to trim and combine library functionalities.Note: The C standard does not mention strong and weak symbols. This is a feature defined by the GCC implementation, and this concept does not exist in the MSC compiler.

Source: Internet, copyright belongs to the original author. If there is any infringement, please contact for deletion.

-END-

Previous Recommendations: Click the image to jump to readAdvanced Embedded C Language: What are Weak Symbols and Weak References?

Insights from chip manufacturers in the embedded technology direction!

Advanced Embedded C Language: What are Weak Symbols and Weak References?

Is it necessary to refactor code when taking over some embedded software projects?

Advanced Embedded C Language: What are Weak Symbols and Weak References?

A significant improvement in supporting materials for embedded development boards, making it easier for beginners without feeling lost!

I am Lao Wen, an embedded engineer who loves learning.Follow me, let’s become better together!

Leave a Comment