public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104631] New: Visibility of static member s yields duplicate symbols.
@ 2022-02-22  8:12 max.sagebaum at scicomp dot uni-kl.de
  2022-02-22  8:46 ` [Bug c++/104631] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: max.sagebaum at scicomp dot uni-kl.de @ 2022-02-22  8:12 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104631
           Summary: Visibility of static member s yields duplicate
                    symbols.
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: max.sagebaum at scicomp dot uni-kl.de
  Target Milestone: ---

Created attachment 52488
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52488&action=edit
Small case for showing the bug.

We develop a header only library where we have a static structure member inside
of a class. We require that this static member is seen by all operations on
this class.

If our library is included with '-fvisibility=hidden' then we get multiple
symbols of the same static member. We tried to fix this by declaring the static
member with '__attribute__((visibility("default")))' but for members which are
structs this does not seem to work.

We created a simple example, that can reproduce the behavior. If the static
member is an 'int' then everything works. If this static member is a structure,
then there is a duplication of the symbols.

You can run the example with 'make T=2'. 'T=1' sets no hidden flag and 'T=3'
removes the hidden flag for the library.

The output should always be:
func: Pointer A<int>::counter: 0x404194
func: Pointer A<Inc>::counter.counter: 0x404198
main: Pointer A<int>::counter: 0x404194
main: Pointer A<Inc>::counter.counter: 0x404198
Counter value 'int': 1
Counter value 'Inc': 1

But with T=2 the output on our machines is:
func: Pointer A<int>::counter: 0x404194
func: Pointer A<Inc>::counter.counter: 0x7f9f69625050
main: Pointer A<int>::counter: 0x404194
main: Pointer A<Inc>::counter.counter: 0x404198
Counter value 'int': 1
Counter value 'Inc': 0

In the real world case the struct 'Inc' is very involved and uses nearly every
other structure in our library. We would be fine by changing the visibility of
our library to default but we could not detect any preprocessor variables, that
hint that the library is included with the hidden visibility. (That is
'-fvisibility=hidden -E -dM func.cpp' and '-E -dM func.cpp' yielded the same
results.)

I hope this is the correct place to submit this bug and ask the question about
the preprocessor macro.

System: Fedora  5.16.9-200.fc35.x86_64
g++: g++ (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)
ld: GNU ld version 2.37-10.fc35

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

* [Bug c++/104631] Visibility of static member s yields duplicate symbols.
  2022-02-22  8:12 [Bug c++/104631] New: Visibility of static member s yields duplicate symbols max.sagebaum at scicomp dot uni-kl.de
@ 2022-02-22  8:46 ` pinskia at gcc dot gnu.org
  2022-02-22  9:10 ` max.sagebaum at scicomp dot uni-kl.de
  2022-04-22 14:58 ` amonakov at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-22  8:46 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Inc is a different class in the shared library and the header if you use
-fvisibility=hidden. Since the type of the static variable is hidden still, the
variable will be still hidden even if it was marked as default visibilitity.
If you want to share Inc across shared libraries and executables, then you need
to mark that class as visibility default too.

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

* [Bug c++/104631] Visibility of static member s yields duplicate symbols.
  2022-02-22  8:12 [Bug c++/104631] New: Visibility of static member s yields duplicate symbols max.sagebaum at scicomp dot uni-kl.de
  2022-02-22  8:46 ` [Bug c++/104631] " pinskia at gcc dot gnu.org
@ 2022-02-22  9:10 ` max.sagebaum at scicomp dot uni-kl.de
  2022-04-22 14:58 ` amonakov at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: max.sagebaum at scicomp dot uni-kl.de @ 2022-02-22  9:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Max S. <max.sagebaum at scicomp dot uni-kl.de> ---
Ok, thank you for the answer. In the example I can set this for the class but,
in the library it would be problematic. So here the best solution would be to
set the whole library to default.

Since the user should decide how the visibility of the library is, we do not
want to set it to default. Since after all, our library could only be used in a
library of the user and other programs never need to see it.

But we would like to warn the user if our library is included with visibility
hidden. 

Is there a preprocessor macro that can provide this information? 

Or is it possible to detect the visibility of a type during compile time in
order to trigger a static assert?

Thanks.

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

* [Bug c++/104631] Visibility of static member s yields duplicate symbols.
  2022-02-22  8:12 [Bug c++/104631] New: Visibility of static member s yields duplicate symbols max.sagebaum at scicomp dot uni-kl.de
  2022-02-22  8:46 ` [Bug c++/104631] " pinskia at gcc dot gnu.org
  2022-02-22  9:10 ` max.sagebaum at scicomp dot uni-kl.de
@ 2022-04-22 14:58 ` amonakov at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: amonakov at gcc dot gnu.org @ 2022-04-22 14:58 UTC (permalink / raw)
  To: gcc-bugs

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

Alexander Monakov <amonakov at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |amonakov at gcc dot gnu.org

--- Comment #3 from Alexander Monakov <amonakov at gcc dot gnu.org> ---
Note that what matters is not the type of the member, but whether template
parameters have hidden visibility, as the following example demonstrates:

struct S {
};

template<class T>
struct TS {
        __attribute__((visibility("default")))
        static int i;
        __attribute__((visibility("default")))
        static S s;
};

template<class T>
int TS<T>::i{};

template<class T>
S TS<T>::s{};

template struct TS<int>;
template struct TS<S>;

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

end of thread, other threads:[~2022-04-22 14:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-22  8:12 [Bug c++/104631] New: Visibility of static member s yields duplicate symbols max.sagebaum at scicomp dot uni-kl.de
2022-02-22  8:46 ` [Bug c++/104631] " pinskia at gcc dot gnu.org
2022-02-22  9:10 ` max.sagebaum at scicomp dot uni-kl.de
2022-04-22 14:58 ` amonakov 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).