Questions tagged [weak-symbol]

A weak symbol denotes a specially annotated symbol during linking of elf object files.

A weak symbol denotes a specially annotated symbol during linking of 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. In contrast, 2 strong symbols that share a name yield a link error during compile-time. 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 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. if weak symbols during dynamic linking at runtime lose their semantics or not.

17 questions
4
votes
3 answers

__attribute__ ((weak)) not work for global variable

pqy@localhost ~/src/test/a $ cat m.c #include int aaaaa __attribute__ ((weak)) =8; int main(void){ printf("%d\n", aaaaa); return 0; } pqy@localhost ~/src/test/a $ cat lib.c int aaaaa = 5; pqy@localhost ~/src/test/a $ gcc lib.c…
Mr Pang
  • 1,083
  • 1
  • 8
  • 20
4
votes
2 answers

Why cannot we use the negation operator when checking for the existence of a symbol?

The answers to this question state that if (x != nil) is the same as if (x). But Apple documentation reads: Note: When checking for the existence of a symbol, you must explicitly compare it to NULL or nil in your code. You cannot use the negation…
Cœur
  • 37,241
  • 25
  • 195
  • 267
3
votes
0 answers

__attribute__((weakref)) not work for external function

Recently I 'm studying the linking process and when it comes to weak symbol, my textbook give a code below to demonstrate how to use __attribute__((weakref)) to declare a weak reference to external function: //pthread.c #include #include…
NO3
  • 31
  • 1
  • 3
3
votes
1 answer

Why are functions generated on use of template have "weak" as symbol type?

Created a template function C++ template void myswap(T&d,T&s). Main uses it for int, double, complex types. Compiled the source using g++. On dumping the symbol table via nm, the functions generated for the respective types have symbol…
3
votes
0 answers

How to replace a symbol in static library with by attribute weak

Suppose I have such code: lib.h: void func(); lib.cpp: void __attribute__((weak)) func() { printf("original"); } and these code would be compile into libA.a. clang++ lib.cpp -o libA.a Then in my test unit I want to override this func function…
jayatubi
  • 1,972
  • 1
  • 21
  • 51
3
votes
0 answers

weak symbol attributes and extern declarations

Here is some ARM assembly code compiled with GCC. Snippet from startup.s: .globl Default_Handler .type Default_Handler, %function Default_Handler: B . .size Default_Handler, . - Default_Handler .macro IRQ handler …
rem
  • 1,131
  • 10
  • 15
1
vote
0 answers

Allow a specific function to be undefined, as long as it's never called at runtime

I have several source files, which are compiled and linked into several executables. Some executables don't link some of the files. I'm getting undefined reference errors, because the calls to the missing functions are still present in the code,…
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
1
vote
1 answer

Does OpenWatcom support an equivalent to ELF weak symbols?

I have two C source files: /* w1.c */ #include __attribute__((weak)) void fw(void) { printf("FW1\n"); } int main(int argc, char **argv) { (void)argc; (void)argv; fw(); return 0; } /* w2.c */ #include void fw(void) {…
pts
  • 80,836
  • 20
  • 110
  • 183
1
vote
0 answers

Override printf function

I have to override the standard printf function and implement it in a user-specific way. I have tried it as follows: extern "C" int printf(const char* format, ...) { // user specific implementation } With gcc, this code can be built. Visual…
Markus F
  • 25
  • 3
0
votes
0 answers

I have one weak symbol from a specific file which is getting overrirden by strong symbol in release build

I have one weak symbol from a specific file which is getting overrirden by strong symbol in release build. Is there any way to convert weak symbol to strong I have one weak symbol from a specific file which is getting overrirden by strong symbol in…
Techie123
  • 1
  • 2
0
votes
1 answer

How to compile C header with inline functions using g++11?

I'm having problem compiling cpp file with C inline functions using g++11. Following is an example: c.h: #ifndef C_H #define C_H #ifdef __cplusplus extern "C" { #endif inline int add(int a,int b){return a+b;} #ifdef…
Jackoo
  • 117
  • 1
  • 1
  • 12
0
votes
0 answers

C++ threading reported as disabled when pthread is linked to shared lib, but not to main executable

This question is very similar to Using C++11 multithreading in shared library loaded by program without thread support I have a shared library which uses OpenMP and a main program, which calls a function from that. testlib.cpp #include void…
Flamefire
  • 5,313
  • 3
  • 35
  • 70
0
votes
0 answers

gcc weak symbol with __attribute((weak)) handling with shared library

write file, one with the weak file and another with the normal symbol. Below are the contents of the files: where weak_ex() is the definition declared with weak symbol. bgl-ads-2997:~/weak_symbol > cat m.c #include #include "weak.h" void…
Deepak
  • 86
  • 9
0
votes
1 answer

Bypass/Switch between/Select weak symbols

In a trivial example: #include #include int main() { void* test = malloc(10); printf("%p\n", test); return 0; } When I step into malloc with gdb I end up in libc: #0 __GI___libc_malloc (bytes=10) at malloc.c:3028 #1 …
Anastasios Andronidis
  • 6,310
  • 4
  • 30
  • 53
0
votes
1 answer

How to use weak function in C++

I am trying to use weak function in a class in C++. Below is what I wrote: #include #include class A { public: void func(int argc, char *argv[]) __attribute__((weak)); }; // optional definition: #if 0 void A::func(int…
1
2