From: Andrew Pinski <pinskia@gmail.com>
To: 刘畅 <fluency0726@gmail.com>
Cc: gcc@gcc.gnu.org
Subject: Re: A problem of weak & weakref function attribute
Date: Fri, 9 Dec 2022 19:45:30 -0800 [thread overview]
Message-ID: <CA+=Sn1=TscGxFgWRKnQtZmC9Dwzc79q6iXRaY+9uyvcPxRHDug@mail.gmail.com> (raw)
In-Reply-To: <CA+hefuQCsO_OYpvnLQaOzPN4Y6MtiJRrjvQBEZCDypV=whu2Lw@mail.gmail.com>
On Fri, Dec 9, 2022 at 7:17 PM 刘畅 via Gcc <gcc@gcc.gnu.org> wrote:
>
> 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 :)
No it is a bug, at least the documentation no longer matches the implementation.
I filed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108042 and even
pointed out the (old) revision which changed the behavior to no longer
match the documentation.
Thanks,
Andrew
>
> Best regards,
>
> Chang Liu
prev parent reply other threads:[~2022-12-10 3:45 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-10 3:16 刘畅
2022-12-10 3:45 ` Andrew Pinski [this message]
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+=Sn1=TscGxFgWRKnQtZmC9Dwzc79q6iXRaY+9uyvcPxRHDug@mail.gmail.com' \
--to=pinskia@gmail.com \
--cc=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).