From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by sourceware.org (Postfix) with ESMTPS id 98EF9382FADD for ; Sat, 10 Dec 2022 03:16:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 98EF9382FADD Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-x52d.google.com with SMTP id a16so5585912edb.9 for ; Fri, 09 Dec 2022 19:16:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:to:subject:message-id:date:from :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=nDwuW8W0IYutRw4qjDYwz44KbBn1LcGzCB3cTHBQ2kQ=; b=FlVxA6S8k6BKGzPm/xZ99c6RRN+WkK7pWVMqyrCfd6st5A/+Ir0uF+FFjExFKLP6YO isXcq2pkKV1xcywEiCslDUy/KhkOuEs1SXKV1cgE5SWklqHuEGtOKty/qcoqQC2kenos uHyU9jWlSf9bfSI/ohSbwgZXzO98IwuNBKQBwIFQBtaWd00FTGespUvg8AznnWwSeWMx wY2ntXW4zkjWihW+0U8ce9UTI8cq8qgTBV7QLC6Q20gnzFFpYGvSuzG0FPSujlNdDgEE xp3RqNueS+ppHvN9SeiRUj2e0OnEgPtpCUhdd2/wVjsDgr9yARvVVQdQvWdvW5FyFXJb 7MKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:to:subject:message-id:date:from :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nDwuW8W0IYutRw4qjDYwz44KbBn1LcGzCB3cTHBQ2kQ=; b=mPCEYmYbWRqYBmNti6qfAwoK5iaZLFExVN6yCBcFlQlyn+d0DRYt1ZapRSIMCj7SCs 4hQQDbYhcAn0nimuZwoULdmKZxRLkWjndL7rV5d7f1i79gpdDUOxKhf9fOpXKqim4tyd AH3znf82CaeK/HsAHOZbcmon+2Gg0IDMN413fTVxfYQ6KjwfAi/zvQrD2/l8D5jKoNmx /gqBmox1K5pJ5NkhRgC+FmCLcHsx3Mqz2NA7ZPXOWL8p7cJTNUUtIlGLzXf0SXeYb1KB ZJYcppOQh/A3Quk2icTNuJtxMF+ndAC5N/QQwDlllm73Eyd9QLpTUIJh7atK8oXpU1ma bTpg== X-Gm-Message-State: ANoB5pkZ1o0t2rgntzHPxDi89ASrx0Sdw2EoOBhRD0Cm/kF8ghytUoTR 53RoLsdH02lugNUyiNF2rjYiB2kPKF27Q9Rul27ekDFsJ30= X-Google-Smtp-Source: AA0mqf4lcDE8Iyh2YwWT1uTFfkcrCuSwnd9mEBZl06QYZf2N0dokeAJSz/zATXhIP4oO6vQgu54FJacSuKcZARh2f+w= X-Received: by 2002:a05:6402:43cc:b0:46c:d5e8:30e7 with SMTP id p12-20020a05640243cc00b0046cd5e830e7mr14213664edc.268.1670642158992; Fri, 09 Dec 2022 19:15:58 -0800 (PST) MIME-Version: 1.0 From: =?UTF-8?B?5YiY55WF?= Date: Sat, 10 Dec 2022 11:16:17 +0800 Message-ID: Subject: A problem of weak & weakref function attribute To: gcc@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 void func(void) __attribute__((weak)); int main() { if (func) printf("1\n"); else printf("0\n"); return 0; } b.c #include 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: =E2=80=98weakref=E2=80=99 attribute should be accompanie= d with an =E2=80=98alias=E2=80=99 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 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 0x0000000000001169 <+32>: eb 0c jmp 0x1177 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 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 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