public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107907] New: weak integer constant not linked correctly
@ 2022-11-29  8:28 bseifert at gmx dot at
  2022-11-29  9:24 ` [Bug c++/107907] weak integer constant not used correctly rguenth at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: bseifert at gmx dot at @ 2022-11-29  8:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107907

            Bug ID: 107907
           Summary: weak integer constant not linked correctly
           Product: gcc
           Version: 9.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bseifert at gmx dot at
  Target Milestone: ---

given the following files:

def.hpp
-----------
struct A
{
   static const int C;
   static int get_C();
};
-----------


weak.cpp
-----------
#include "def.hpp"

const int A::C __attribute__((weak)) = 1;

int A::get_C()
{
   return C;
}
-----------

main.cpp
-----------
#include <stdio.h>
#include "def.hpp"

const int A::C = 2;

int main()
{
   printf("%u\n", A::get_C());
}
-----------

the constant C in class A is defined weak in weak.cpp (to the value 1) and
defined strong in main.cpp (to the value 2).
it is expected that the static method get_C defined in weak.cpp is using the
strong symbol (value 2) but it uses the local definition of C regardless of the
weak attribute.

test case:
g++ -o test.exe -O2 main.cpp weak.cpp
./test.exe
1

then the file weak.cpp is replaced with file weak2.cpp (only difference is that
definition of C is AFTER the definition of get_C):

weak2.cpp
-----------
#include "def.hpp"

int A::get_C()
{
   return C;
}

const int A::C __attribute__((weak)) = 1;
-----------

test case:
g++ -o test.exe -O2 main.cpp weak2.cpp
./test.exe
2

it seems that the compilation of get_C is not done properly (wrongly optimized)
because the compiler should not directly use the value of a constant if the
constant is attributed weak!

here is the assembly of the methods get_C:

weak.lss
-----------
0000000000000000 <A::get_C()>:
   0:   f3 0f 1e fa             endbr64
   4:   b8 01 00 00 00          mov    $0x1,%eax
   9:   c3                      retq
-----------

weak2.lss
-----------
0000000000000000 <A::get_C()>:
   0:   f3 0f 1e fa             endbr64
   4:   8b 05 00 00 00 00       mov    0x0(%rip),%eax        # a
<A::get_C()+0xa>
   a:   c3                      retq
-----------

this clearly shows the direct use of the value of the constant in the first
case and the indirect loading of the value in the second case

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/107907] weak integer constant not used correctly
  2022-11-29  8:28 [Bug c++/107907] New: weak integer constant not linked correctly bseifert at gmx dot at
@ 2022-11-29  9:24 ` rguenth at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-11-29  9:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107907

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-11-29
             Status|UNCONFIRMED                 |NEW
            Version|9.4.0                       |12.2.1
           Keywords|                            |wrong-code

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed with GCC 12.  The .original dump shows the frontend performs constant
folding even at -O0 here.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-11-29  9:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-29  8:28 [Bug c++/107907] New: weak integer constant not linked correctly bseifert at gmx dot at
2022-11-29  9:24 ` [Bug c++/107907] weak integer constant not used correctly rguenth at gcc dot gnu.org

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).