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

            Bug ID: 107597
           Summary: LTO causes static inline variables to get a
                    non-uniqued global symbol
           Product: gcc
           Version: 8.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cfsteefel at arista dot com
  Target Milestone: ---

The following was seen with gcc 11.3.1 (centos7 based) and gcc 8.4 (also
centos7 based).

The following code produces a GNU extension unique symbol for NonTemplated::x
when flto is not used ('u' in nm), but a global non-unique symbol when flto is
used, even when building a shared library ('B' in nm).

class NonTemplated {
   static inline int x;
   public:
   void doFoo() {
      x++;
   }
};

int main() {
   NonTemplated n;
   n.doFoo();
   return 0;
}

> g++ -std=gnu++17 -O2 -shared -fPIC -o libFoo.so -flto test.cpp
> nm ./libFoo.so | c++filt | grep NonTemplated
000000000020102c B NonTemplated::x


> g++ -std=gnu++17 -O2 -shared -fPIC -o libFoo.so test.cpp
> nm ./libFoo.so | c++filt | grep NonTemplated
000000000020102c u NonTemplated::x

When compiled under clang++, the symbol is `V` (weak) regardless of flto is
used or not.

The resulting symptom of this is that Address Sanitizer will flag the variable
NonTemplated::X as an ODR violation, if the class is included into more than
one flto compiled shared library.

Reply via email to