[Bug c++/87947] New: Symbol Does Not Appear in Object File

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[Bug c++/87947] New: Symbol Does Not Appear in Object File

segher at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87947

            Bug ID: 87947
           Summary: Symbol Does Not Appear in Object File
           Product: gcc
           Version: 8.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: comm+gnu at squotd dot net
  Target Milestone: ---

A symbol is not exposed in cirucumstances where it seems it should be, and is
exposed in version 6.3.0 (and clang 7.0.0, fwiw).

$ gcc --save-temps -std=c++17 -c symbol.cpp
$ cat symbol.ii
# 1 "symbol.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "symbol.cpp"
struct A {
    static constexpr auto foobar = 0;
};

constexpr int A::foobar;

$ nm symbol.o
$

The expected behavior is that A::foobar is exposed as a symbol. Note that
-std=c++17 is required to duplicate this.

The same thing done with 6.3.0 results in this.

$ nm symbol.o
0000000000000000 R _ZN1A6foobarE

Version information is as follows.

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared
--enable-threads=posix --enable-libmpx --with-system-zlib --with-isl
--enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu
--disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object
--enable-linker-build-id --enable-lto --enable-plugin
--enable-install-libiberty --with-linker-hash-style=gnu
--enable-gnu-indirect-function --enable-multilib --disable-werror
--enable-checking=release --enable-default-pie --enable-default-ssp
--enable-cet=auto
Thread model: posix
gcc version 8.2.1 20180831 (GCC)
Reply | Threaded
Open this post in threaded view
|

[Bug c++/87947] Symbol Does Not Appear in Object File

segher at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87947

Jonathan Wakely <redi at gcc dot gnu.org> changed:

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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to comm+gnu from comment #0)
> The expected behavior is that A::foobar is exposed as a symbol. Note that
> -std=c++17 is required to duplicate this.

Your expectation is wrong. In C++17 the static data member is implicitly
"inline" which means a definition is only emitted when it's odr-used (and the
namespace scope definition is redundant, and deprecated).

GCC 6.x does not implement the C++17 rule, so still emits the symbol.

If you need to ensure a definition is emitted in a given object file either
compile it as C++14, or mark it as used:

[[gnu::used]] constexpr int A::foobar;
Reply | Threaded
Open this post in threaded view
|

[Bug c++/87947] Symbol Does Not Appear in Object File

segher at gcc dot gnu.org
In reply to this post by segher at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87947

--- Comment #2 from comm+gnu at squotd dot net ---
Thanks. I had no idea that had changed.
Reply | Threaded
Open this post in threaded view
|

[Bug c++/87947] Symbol Does Not Appear in Object File

segher at gcc dot gnu.org
In reply to this post by segher at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87947

--- Comment #3 from comm+gnu at squotd dot net ---
Does this result in an exception to the answer you gave here about gcc being
able to link c++14 and c++17 object files together? (Or am I missing
something?)

https://stackoverflow.com/questions/46746878/is-it-safe-to-link-c17-c14-and-c11-objects?answertab=active#tab-top
Reply | Threaded
Open this post in threaded view
|

[Bug c++/87947] Symbol Does Not Appear in Object File

segher at gcc dot gnu.org
In reply to this post by segher at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87947

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
That answer is about whether it's "safe", and explains that the -std options do
not create ABI incompatibilities. The question is also stated as (emphasis in
the original):

> For simplicity, let's assume all headers were written in C++11, **using only constructs whose semantics haven't changed between all three standard versions**.

This is a feature whose semantics changed. What you're seeing is pretty much
required behaviour for a correct implementation, not an ABI incompatibility
introduced by GCC.
Reply | Threaded
Open this post in threaded view
|

[Bug c++/87947] Symbol Does Not Appear in Object File

segher at gcc dot gnu.org
In reply to this post by segher at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87947

--- Comment #5 from comm+gnu at squotd dot net ---
OK, thanks. I appreciate the explanation.