GO-V.ORG
As seen in

Weak symbol

A weak symbol denotes a specially annotated symbol during linking of Executable and Linkable Format (ELF) object files. By default, without any annotation, a symbol in an object file is strong. During linking, a strong symbol can override a weak symbol of the same name. This behavior allows an executable to override standard library functions, such as malloc(3). When linking a binary executable, a weakly declared symbol does not need a definition. In comparison, (by default) a declared strong symbol without a definition triggers an undefined symbol link error. Weak symbols are not mentioned by the C or C++ language standards; as such, inserting them into code is not very portable. Even if... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

A weak symbol denotes a specially annotated symbol during linking of Executable and Linkable Format (ELF) object files. By default, without any annotation, a symbol in an object file is strong. During linking, a strong symbol can override a weak symbol of the same name. This behavior allows an executable to override standard library functions, such as malloc(3). When linking a binary executable, a weakly declared symbol does not need a definition. In comparison, (by default) a declared strong symbol without a definition triggers an undefined symbol link error. Weak symbols are not mentioned by the C or C++ language standards; as such, inserting them into code is not very portable. Even if two platforms support the same or similar syntax for marking symbols as weak, the semantics may differ in subtle points, e.g. whether weak symbols during dynamic linking at runtime lose their semantics or not. // function declaration pragma weak power2 int power2(int x); // function declaration int __attribute__((weak)) power2(int x); // or int power2(int x) __attribute__((weak)); // variable declaration; extern int __attribute__((weak)) global_var; include include include "power_slow.h" int main(int argc, char **argv) { fprintf(stderr, "power3() = %d\n", power3(atoi(argv[1]))); return 0; } power_slow.h: ifndef POWER2_SLOW_H define POWER2_SLOW_H // alternative syntax // #pragma weak power2 int __attribute__((weak)) power2(int x) // alternatively after symbol // __attribute__((weak)) ; int power3(int x); endif : include include "power_slow.h" int power2(int x) { fprintf(stderr, "slow power2()\n"); return x*x; } int power3(int x) { return power2(x)*x; } : include int power2(int x) { fprintf(stderr, "fast power2()\n"); return x*x; } Build commands: cc -g -c -o main.o main.c cc -g -c -o power_slow.o power_slow.c cc -g -c -o power.o power.c cc main.o power_slow.o -o cc main.o power_slow.o power.o -o fast Output: $ ./slow 3 slow power2() power3() = 27 $ ./fast 3 fast power2() power3() = 27 When removing the weak attribute and re-executing the build commands, the last one fails with the following error message (on Linux): multiple definition of `power2' The second-last one still succeeds, and ./slow has the same output. If there are no definition for a weak symbol function as a default implementation or as another weak symbol function definition or a strong symbol function definition in any of the object files linked together, the linking will be done successfully without any undefined symbol error for that weak symbol, but the execution may cause runtime crash. ifndef NO_USER_HOOK void user_hook(void) { fprintf(stderr, "main: user_hook()\n"); } endif Replacing power_slow.c with: include include "power_slow.h" void __attribute__((weak)) user_hook(void); ifdef ENABLE_DEF void user_hook(void) { fprintf(stderr, "power_slow: user_hook()\n"); } endif int power2(int x) { if (user_hook) // only needed ifndef ENABLE_DEF user_hook(); return x*x; } int power3(int x) { return power2(x)*x; } Build commands: cc -g -c -o main.o main.c cc -g -fpic -c -o power_slow.po power_slow.c cc -shared -fpic -o libpowerslow.so power_slow.po cc main.o -L`pwd` -Wl,-R`pwd` -lpowerslow -o main cc -g -DENABLE_DEF -fpic -c -o power_slow.po power_slow.c cc -shared -fpic -o libpowerslow.so power_slow.po cc main.o -L`pwd` -Wl,-R`pwd` -lpowerslow -o main2 cc -g -DNO_USER_HOOK -c -o main.o main.c cc -g -fpic -c -o power_slow.po power_slow.c cc -shared -fpic -o libpowerslow.so power_slow.po cc main.o -L`pwd` -Wl,-R`pwd` -lpowerslow -o main3 cc -g -DNO_USER_HOOK -c -o main.o main.c cc -g -DENABLE_DEF -fpic -c -o power_slow.po power_slow.c cc -shared -fpic -o libpowerslow.so power_slow.po cc main.o -L`pwd` -Wl,-R`pwd` -lpowerslow -o main4 Output: $ ./main 3 main: user_hook() power3() = 27 $ ./main2 3 main: user_hook() power3() = 27 $ ./main3 3 power3() = 27 $ ./main4 3 power_slow: user_hook() power3() = 27 Removing the weak attribute and re-executing the build commands does not yield build errors and leads to the same output (on Linux) for main and main2 . The build commands for the main3 lead to following warning and error messages (on Linux): warning: the address of ‘user_hook’ will always evaluate as ‘true’ libpowerslow.so: undefined reference to `user_hook' The warning is issued by the compiler because it can statically determine that in if (user_hook) the expression user_hook evaluates always to true, because it contains an ELF jump table entry. The error message is issued by the linker. The build for main4 includes the same warning but no link error. pragma weak func void func(); void bar() { if (func) func(); } , depending on the compiler and used optimization level, the compiler may interpret the conditional as always true (because func can be seen as undefined from a standards point of view). An alternative to the above construct is using a system API to check if func is defined (e.g. dlsym with RTLD_DEFAULT ). The above check may also fail for other reasons, e.g. when func contains an elf jump table entry. Using weak symbols in static libraries has other semantics than in shared ones, i.e. with a static library the symbol lookup stops at the first symbol – even if it is just weak and an object file with a strong symbol is also included in the library archive. On Linux, the linker option --whole-archive changes that behavior. The weak function attribute is supposed to be used on function declarations. Using it on a function definition may yield unexpected results, depending on the compiler and optimization level. In Solaris, the weak symbols are also used within kernel. The generic part of kernel (called genunix ) specifies weak functions that are overridden in platform specific part of the kernel (called unix ) such as Virtual memory routines. The kernel runtime linker sets the addresses of these functions when the kernel is combined in memory during boot. This does not work for kernel loadable modules though - weak symbol in the kernel is not replaced with kernel module symbol when the module is loaded.

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Weak symbol". 1751155459. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1297873261

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·