public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105645] New: Template specializations are not hidden with fvisibility=hidden
@ 2022-05-18 14:02 kndevl at outlook dot com
  2022-05-18 14:25 ` [Bug c++/105645] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: kndevl at outlook dot com @ 2022-05-18 14:02 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105645
           Summary: Template specializations are not hidden with
                    fvisibility=hidden
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kndevl at outlook dot com
  Target Milestone: ---

Consider this snippet `encoding.cpp`

```
namespace Test {
enum class Encoding { A, B };

template <Encoding enc>
int encode(int);

template <>
int encode<Encoding::A>(int x)
{
    return x + 1;
}

int f() { return 42; }
}
```

I am compiling it through CMake with `/usr/bin/g++ -Dencoding_EXPORTS  -g -fPIC
-fvisibility=hidden -fvisibility-inlines-hidden -std=gnu++17 -MD -MT
CMakeFiles/encoding.dir/encoding.cpp.o -MF
CMakeFiles/encoding.dir/encoding.cpp.o.d -o
CMakeFiles/encoding.dir/encoding.cpp.o -c
/home/nishanth/source/main/encoding.cpp`

and linking it with `/usr/bin/g++ -fPIC -g   -shared -Wl,-soname,libencoding.so
-o libencoding.so CMakeFiles/encoding.dir/encoding.cpp.o
CMakeFiles/encoding.dir/encoding-user.cpp.o`

I expect `f()` and `Test::encode<Encoding::A>` to have the same symbol binding
in `libencoding.so`. This is the output of `nm -C libencoding.so`

```
0000000000001118 t Test::f()
0000000000001109 T int Test::encode<(Test::Encoding)0>(int)
```

`objdump -t encoding.cpp.o` does not have a `.hidden` tag on the `encode`
specialization

```
0000000000000000 g     F .text  000000000000000f
_ZN4Test6encodeILNS_8EncodingE0EEEii
000000000000000f g     F .text  000000000000000b .hidden _ZN4Test1fEv
```

I can make `Test::encode` specialization local only by using
`__attribute__((visibility("hidden")))` explicitly on the definition, in
addition to calling the compiler with `-fvisibility=hidden`. 

On the other hand, clang 13.0.1 marks both `f()` and the `encode`
specialization with `.hidden` in `objdump -t` output. Is there a way I can
force g++ to hide template specializations?

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

* [Bug c++/105645] Template specializations are not hidden with fvisibility=hidden
  2022-05-18 14:02 [Bug c++/105645] New: Template specializations are not hidden with fvisibility=hidden kndevl at outlook dot com
@ 2022-05-18 14:25 ` pinskia at gcc dot gnu.org
  2022-05-19  6:10 ` kndevl at outlook dot com
  2024-02-23 13:40 ` tanksherman27 at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-05-18 14:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There might be a dup of this one already.

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

* [Bug c++/105645] Template specializations are not hidden with fvisibility=hidden
  2022-05-18 14:02 [Bug c++/105645] New: Template specializations are not hidden with fvisibility=hidden kndevl at outlook dot com
  2022-05-18 14:25 ` [Bug c++/105645] " pinskia at gcc dot gnu.org
@ 2022-05-19  6:10 ` kndevl at outlook dot com
  2024-02-23 13:40 ` tanksherman27 at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: kndevl at outlook dot com @ 2022-05-19  6:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Karthik Nishanth <kndevl at outlook dot com> ---
Additional details:

When I add a template definition for `Test::encode()` before specialization, it
hides the specialized symbol. The snippet below produces `00000000000010e9 t
int Test::encode<(Test::Encoding)0>(int)`.

```
namespace Test {
enum class Encoding { A,
    B };

template <Encoding enc>
int encode(int);

template <Encoding enc>
int encode(int x) { return x - 1; }

template <>
int encode<Encoding::A>(int x)
{
    return x + 1;
}

int f() { return 42; }
}
```

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

* [Bug c++/105645] Template specializations are not hidden with fvisibility=hidden
  2022-05-18 14:02 [Bug c++/105645] New: Template specializations are not hidden with fvisibility=hidden kndevl at outlook dot com
  2022-05-18 14:25 ` [Bug c++/105645] " pinskia at gcc dot gnu.org
  2022-05-19  6:10 ` kndevl at outlook dot com
@ 2024-02-23 13:40 ` tanksherman27 at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: tanksherman27 at gmail dot com @ 2024-02-23 13:40 UTC (permalink / raw)
  To: gcc-bugs

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

Julian Waters <tanksherman27 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tanksherman27 at gmail dot com

--- Comment #3 from Julian Waters <tanksherman27 at gmail dot com> ---
I can confirm this on gcc 11 and higher:

#include <cstdlib>

template<typename T>
void method(T* src, T* dst, size_t length);

template<>
void method<short>(short* src, short* dst, size_t length) {

}

template<>
void method<int>(int* src, int* dst, size_t length) {

}

Compiled using command line:
g++ -O3 -fvisibility=hidden -std=c++14 -pedantic -Wpedantic -Wall -Werror
templates.cpp

Linked using:
g++ -shared -o libtemplates.so templates.o

When nm is run over it using nm -gDC libtemplates.so, the following symbols are
seen:
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000001110 T void method<int>(int*, int*, unsigned long)
0000000000001100 T void method<short>(short*, short*, unsigned long)
                 w __cxa_finalize
                 w __gmon_start__

T void method<int>(int*, int*, unsigned long) and T void method<short>(short*,
short*, unsigned long) should not be in the symbol table, but they are even
with -fvisibility=hidden.

Strangely, marking both methods with [[gnu::visibility("hidden")]] works and
results in both not being exported.

This was recently observed in the JDK, inside the Java HotSpot VM (see
https://github.com/openjdk/jdk/pull/17955/files#r1498933843)

Is this enough of a reproducer to get this confirmed?

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

end of thread, other threads:[~2024-02-23 13:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-18 14:02 [Bug c++/105645] New: Template specializations are not hidden with fvisibility=hidden kndevl at outlook dot com
2022-05-18 14:25 ` [Bug c++/105645] " pinskia at gcc dot gnu.org
2022-05-19  6:10 ` kndevl at outlook dot com
2024-02-23 13:40 ` tanksherman27 at gmail dot com

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