Hi all, I met a problem when I was testing the weak attribute and the weakref attribute of GCC. I've read the documentation and in the 6.33.1 Common Function Attributes - weakref part I found:
Without a target given as an argument to weakref or to alias, weakref is equivalent to weak (in that case the declaration may be extern). To verify this statement, I wrote the following two C programs: a.c #include <stdio.h> void func(void) __attribute__((weak)); int main() { if (func) printf("1\n"); else printf("0\n"); return 0; } b.c #include <stdio.h> extern void func(void) __attribute__((weakref)); int main() { if (func) printf("1\n"); else printf("0\n"); return 0; } The only difference is a.c uses __attribute__((weak)) while b.c uses __attribute__((weakref)). According to the statement I referred above, I expect the two programs have the smae behavior. However, after I compiled the two programs with: $ gcc a.c -o a.out; gcc b.c -o b.out I got a warning: b.c:3:13: warning: ‘weakref’ attribute should be accompanied with an ‘alias’ attribute [-Wattributes] 3 | extern void func(void) __attribute__((weakref)); | then I found they have different output: $ ./a.out; ./b.out 0 1 Then I disassembled the main function of a.out and b.out, and found the func symbol didn't even appear in the assemble code of b.c (I recompiled the 2 programs with -g option): assemble code of a.c: 5 int main() { 0x0000000000001149 <+0>: f3 0f 1e fa endbr64 0x000000000000114d <+4>: 55 push %rbp 0x000000000000114e <+5>: 48 89 e5 mov %rsp,%rbp 6 if (func) 0x0000000000001151 <+8>: 48 8b 05 90 2e 00 00 mov 0x2e90(%rip),%rax # 0x3fe8 0x0000000000001158 <+15>: 48 85 c0 test %rax,%rax 0x000000000000115b <+18>: 74 0e je 0x116b <main+34> 7 printf("1\n"); 0x000000000000115d <+20>: 48 8d 3d a0 0e 00 00 lea 0xea0(%rip),%rdi # 0x2004 0x0000000000001164 <+27>: e8 e7 fe ff ff callq 0x1050 <puts@plt> 0x0000000000001169 <+32>: eb 0c jmp 0x1177 <main+46> 8 else 9 printf("0\n"); 0x000000000000116b <+34>: 48 8d 3d 94 0e 00 00 lea 0xe94(%rip),%rdi # 0x2006 0x0000000000001172 <+41>: e8 d9 fe ff ff callq 0x1050 <puts@plt> 10 11 return 0; 0x0000000000001177 <+46>: b8 00 00 00 00 mov $0x0,%eax 12 } 0x000000000000117c <+51>: 5d pop %rbp 0x000000000000117d <+52>: c3 retq assemble code of b.c: 5 int main() { 0x0000000000001149 <+0>: f3 0f 1e fa endbr64 0x000000000000114d <+4>: 55 push %rbp 0x000000000000114e <+5>: 48 89 e5 mov %rsp,%rbp 6 if (func) 7 printf("1\n"); 0x0000000000001151 <+8>: 48 8d 3d ac 0e 00 00 lea 0xeac(%rip),%rdi # 0x2004 0x0000000000001158 <+15>: e8 f3 fe ff ff callq 0x1050 <puts@plt> 8 else 9 printf("0\n"); 10 11 return 0; 0x000000000000115d <+20>: b8 00 00 00 00 mov $0x0,%eax 12 } 0x0000000000001162 <+25>: 5d pop %rbp 0x0000000000001163 <+26>: c3 retq In my test, the weak attribute and the weakref attribute without a target given as an argument to weakref or to alias have different behavior, which is different from the documentation. I don't know if it's because I misunderstood the documentation. I would be appreciate if anyone can help me :) Best regards, Chang Liu