| Issue |
87142
|
| Summary |
Get unexpected static variable initialization order when use -flto=full
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
akemihomura0105
|
## problem
use the following command to compile:
**clang++ -stdlib=libstdc++ --gcc-toolchain=/opt/tool/gcc-9.1.0/ -flto=full main.cpp a.cpp -g -fpic && ./a.out**
and get a coredump.

MyClass::ptr is a unexpected nullptr.
MyClass::ptr, boom are in the same compile unit, and MyClass:ptr is defined previous than the boom variable, so MyClass::ptr should be initialize before the boom initialize. However, in this case, boom is initialize before MyClass::ptr. I don't know it's gcc-toolchain bug or clang bug.
Here is the source code:
```
// a.h
class A
{
public:
static A& getInstance()
{
static A t;
return t;
}
virtual int fun() const
{
return 42;
};
};
class MyClass
{
public:
const static inline A* ptr = &A::getInstance(); // should be initialize before MyClass::ptr, am I wrong?
};
// a.cpp
#include "a.h"
static auto boom = MyClass::ptr->fun(); // MyClass::ptr is nullptr, initialize before MyClass::ptr.
// main.cpp
#include <iostream>
int main()
{
}
```
Here is some part of the assemble code:
```0000000000001c30 <__cxx_global_var_init.1>:
1c30: 55 push %rbp
1c31: 48 89 e5 mov %rsp,%rbp
1c34: 48 8b 3d 3d 24 00 00 mov 0x243d(%rip),%rdi # 4078 <_ZN7MyClass3ptrE>
1c3b: 48 8b 07 mov (%rdi),%rax
1c3e: 5d pop %rbp
1c3f: ff 20 jmpq *(%rax)
0000000000001c60 <__cxx_global_var_init.2>:
1c60: 55 push %rbp
1c61: 48 89 e5 mov %rsp,%rbp
1c64: 0f b6 05 15 24 00 00 movzbl 0x2415(%rip),%eax # 4080 <_ZGVN7MyClass3ptrE>
1c6b: 84 c0 test %al,%al
1c6d: 74 02 je 1c71 <__cxx_global_var_init.2+0x11>
1c6f: 5d pop %rbp
1c70: c3 retq
1c71: 48 8d 3d 08 24 00 00 lea 0x2408(%rip),%rdi # 4080 <_ZGVN7MyClass3ptrE>
1c78: e8 e3 00 00 00 callq 1d60 <__cxa_guard_acquire@plt>
1c7d: 85 c0 test %eax,%eax
1c7f: 74 ee je 1c6f <__cxx_global_var_init.2+0xf>
1c81: e8 2a 00 00 00 callq 1cb0 <_ZN1A11getInstanceEv>
1c86: 48 8d 05 43 23 00 00 lea 0x2343(%rip),%rax # 3fd0 <_ZZN1A11getInstanceEvE1t>
1c8d: 48 89 05 e4 23 00 00 mov %rax,0x23e4(%rip) # 4078 <_ZN7MyClass3ptrE>
1c94: 48 8d 3d e5 23 00 00 lea 0x23e5(%rip),%rdi # 4080 <_ZGVN7MyClass3ptrE>
1c9b: 5d pop %rbp
1c9c: e9 cf 00 00 00 jmpq 1d70 <__cxa_guard_release@plt>
```
## environment
**clang++ version**:
clang version 19.0.0git (https://github.com/llvm/llvm-project.git 33d804c6c2786cbbbc13743060f08d679941e0a4)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
** lld version **
LLD 19.0.0 (compatible with GNU linkers)
** llvm compile command **
cmake -B build -S llvm -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang -DLLVM_ENABLE_LTO=Thin -DLLVM_ENABLE_LLD=ON -DLLVM_ENABLE_LIBCXX=ON -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;libc;lld;lldb" -DLLVM_ENABLE_RUNTIMES="libunwind;libcxxabi;libcxx;compiler-rt" -DLLVM_TARGETS_TO_BUILD=X86 -DCLANG_DEFAULT_CXX_STDLIB=libc++ -DCLANG_DEFAULT_LINKER=lld -DCLANG_DEFAULT_RTLIB=compiler-rt -DCLANG_DEFAULT_UNWINDLIB=libunwind -DLLVM_ENABLE_LLD=ON
** linux kernal version **
5.10.134-15.1.al8.x86_64
**gcc version**
gcc (GCC) 9.1.0
here is the code zip
[tmp.zip](https://github.com/llvm/llvm-project/files/14810745/tmp.zip)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs