From: 刘畅 <fluency0726@gmail.com>
To: gcc@gcc.gnu.org
Subject: A problem of weak & weakref function attribute
Date: Sat, 10 Dec 2022 11:16:17 +0800 [thread overview]
Message-ID: <CA+hefuQCsO_OYpvnLQaOzPN4Y6MtiJRrjvQBEZCDypV=whu2Lw@mail.gmail.com> (raw)
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
next reply other threads:[~2022-12-10 3:16 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-10 3:16 刘畅 [this message]
2022-12-10 3:45 ` Andrew Pinski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CA+hefuQCsO_OYpvnLQaOzPN4Y6MtiJRrjvQBEZCDypV=whu2Lw@mail.gmail.com' \
--to=fluency0726@gmail.com \
--cc=gcc@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).