<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/87142>87142</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Get unexpected static variable initialization order when use -flto=full
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
akemihomura0105
</td>
</tr>
</table>
<pre>
## 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.
![image](https://github.com/llvm/llvm-project/assets/43955018/be35848f-0bb9-4ad3-8e3c-0b177c40251a)
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)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycWFuT2rgS_jWaly67ZMk3HuYBxjA7Z8lka0L25OwLJcsyaCNfIsszbH79KdkGDDi3pSgulrr76_6-li2xppG7Uoh7FCxQkNyx1uwrfc8-i0Luq6LVDHs4uEur7J97RCgiFGpdpUoUCCcIz9tGgNkLyCulqjdZ7oBXRcHKDExlf9ZSCUTn_WRE7JsrVu4QWSCyAKcxmZIpoomSaWMyfrzu7Dh3TFUpvmeyRDRBZFXVBpGVvYjIyo7PXM_FiKzAyZWpEE3yVikomCxdXtfAuk9nB05eSw6IhIiE4CKyYm7Vmh5Nj8wC3gkDDHilRdYWtXuE7KFgIQu2EyhIEIn3xtSNzYisLApp9m3q8qpAZKXU6_HLqXX1t-AWL2saYRpEVj6dBQH2YkRWqaBB7Me5g9N05vgso04sKHdw6kUR9zEJPIbIrIfw7p8HxZouJp3XRoNsgEFbikMtuBEZlK1StdFHxJNGiDxAWlUFMC1Alh1nDSvEkSNoS2nsJFuJs_EQLhO5LEUGtRavsmobMHvW--h8vjItWaqEtW8quAHc7KtWZZDayNJIpuRXAanIKy3OTs5DLvxWvYlX0YHusMoGOGvEKQnZTHi6DuvCE2RViUhk4HNZvYFNMGrgQlmQtjuoNHSitH-GKv4mbJ2avk5Vq7mtVDaScoiH9yATqwZg7r7_zy0SOM6NFv0PAIC6TZXkJz8wvBrDjOQwtwrdCfNUNoaVXCASn3QAF36uDcEgOjGshWl1OTGIomS48Cq1aZkCWRrI27IPCbwqG3Nt8u0APjlHsK6Pf8a_-6Icafrp0nRIjnnKUslS2DrNwUoL0cQ29rxn_bZydAEwkPM9FU61CyvgCd50Ve4QXd1mc0U8r-vjFSpLrtpMACLEKoKQfmRIgbWm6mVswV9H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