<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58248>58248</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            win32 + `-flto` + ASAN = ODR violation or bug?
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          croepha
      </td>
    </tr>
</table>

<pre>
    ```
// odr-dll0.cpp
#include <stdio.h>

void dll0() {
      printf("dll0\n");
}

// odr-dll1.cpp
#include <stdarg.h>
#include <stdio.h>

void dll1(char const * fmt, ...) {
    char buf[2048];
    va_list ap;
    va_start(ap, fmt);
    vsnprintf(buf, sizeof buf, fmt, ap);
}

// odr-main.cpp
#include <stdio.h>
#include <windows.h>

int main () {
    printf("main\n");
    // crashes here...
    auto handle = LoadLibraryA("odr.dll");
}

// build.bat
clang -fsanitize=address -c -flto odr-dll0.cpp -o odr-dll0.o
clang -fsanitize=address -c -flto odr-dll1.cpp -o odr-dll1.o
clang -fsanitize=address -shared -fuse-ld=lld -flto  odr-dll0.o odr-dll1.o -o odr.dll
clang -fsanitize=address -c odr-main.cpp -o odr-main.o  
clang -fsanitize=address -fuse-ld=lld  odr-main.o -o odr.exe 
./odr.exe
```
Output:
```
PS C:\projects\hello-world> .\odr.exe
main
=================================================================
==52800==ERROR: AddressSanitizer: odr-violation (0x7ff943089940):
  [1] size=8 'unsigned __int64 `extern "C" __local_stdio_printf_options'::`2'::_OptionsStorage' odr-dll0.cpp
  [2] size=8 'unsigned __int64 `extern "C" __local_stdio_printf_options'::`2'::_OptionsStorage' odr-dll1.cpp
These globals were registered at these points:
  [1]:
    #0 0x7ff63d2229fd in __asan_register_globals C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_globals.cpp:356
    #1 0x7ff943036524 in __sanitizer::atomic_load C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_atomic_clang_x86.h:37
    #2 0x7ff943036524 in __sanitizer::atomic_load_relaxed C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_atomic.h:76
    #3 0x7ff943036524 in __sanitizer::Mutex::Unlock C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_mutex.h:230
    #4 0x7ff943036524 in __sanitizer::ThreadRegistry::Unlock C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_thread_registry.h:100
    #5 0x7ff943036524 in __sanitizer::GenericScopedLock<__sanitizer::ThreadRegistry>::~GenericScopedLock C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_mutex.h:386
    #6 0x7ff943036524 in __sanitizer::ThreadRegistry::GetNumberOfThreads(unsigned __int64 *, unsigned __int64 *, unsigned __int64 *) C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_thread_registry.cpp:129
    #7 0x7ff94300eb15 in __asan::FakeStack::FlagsOffset C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.h:94
    #8 0x7ff94300eb15 in __asan::FakeStack::GetFlags C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.h:108
    #9 0x7ff94300eb15 in __asan::FakeStack::Allocate C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:95
    #10 0x7ff94300eb15 in __asan::OnMalloc C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:211
    #11 0x7ff94300eb15 in __asan_stack_malloc_2 C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:257
    #12 0x7ff94300ecff in __asan::SetShadow C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:34
    #13 0x7ff94300ecff in __asan::OnMallocAlways C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:228
    #14 0x7ff94300ecff in __asan_stack_malloc_always_2 C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:257
    #15 0x7ff9903bff06  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18003ff06)
    #16 0x7ff9903f2f2d  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x180072f2d)
    #17 0x7ff9903f2cdd  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x180072cdd)
    #18 0x7ff9903cdb3a  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18004db3a)
    #19 0x7ff9903bbafe  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18003bafe)
    #20 0x7ff9903c4dfb  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x180044dfb)
    #21 0x7ff9903baeb5  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18003aeb5)
    #22 0x7ff98dbf3ec1  (C:\WINDOWS\System32\KERNELBASE.dll+0x180033ec1)
    #23 0x7ff98dc3b4f0  (C:\WINDOWS\System32\KERNELBASE.dll+0x18007b4f0)
    #24 0x7ff98dc37f6e  (C:\WINDOWS\System32\KERNELBASE.dll+0x180077f6e)
    #25 0x7ff63d25e6ec in main (C:\projects\hello-world\odr.exe+0x14004e6ec)
    #26 0x7ff63d25f693 in invoke_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
    #27 0x7ff63d25f693 in __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #28 0x7ff98ed154df  (C:\WINDOWS\System32\KERNEL32.DLL+0x1800154df)
    #29 0x7ff99038485a  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18000485a)
  [2]:
    #0 0x7ff63d2229fd in __asan_register_globals C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_globals.cpp:356
    #1 0x7ff943036524 in __sanitizer::atomic_load C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_atomic_clang_x86.h:37
    #2 0x7ff943036524 in __sanitizer::atomic_load_relaxed C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_atomic.h:76
    #3 0x7ff943036524 in __sanitizer::Mutex::Unlock C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_mutex.h:230
    #4 0x7ff943036524 in __sanitizer::ThreadRegistry::Unlock C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_thread_registry.h:100
    #5 0x7ff943036524 in __sanitizer::GenericScopedLock<__sanitizer::ThreadRegistry>::~GenericScopedLock C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_mutex.h:386
    #6 0x7ff943036524 in __sanitizer::ThreadRegistry::GetNumberOfThreads(unsigned __int64 *, unsigned __int64 *, unsigned __int64 *) C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\sanitizer_common\sanitizer_thread_registry.cpp:129
    #7 0x7ff94300eb15 in __asan::FakeStack::FlagsOffset C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.h:94
    #8 0x7ff94300eb15 in __asan::FakeStack::GetFlags C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.h:108
    #9 0x7ff94300eb15 in __asan::FakeStack::Allocate C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:95
    #10 0x7ff94300eb15 in __asan::OnMalloc C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:211
    #11 0x7ff94300eb15 in __asan_stack_malloc_2 C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:257
    #12 0x7ff94300ecff in __asan::SetShadow C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:34
    #13 0x7ff94300ecff in __asan::OnMallocAlways C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:228
    #14 0x7ff94300ecff in __asan_stack_malloc_always_2 C:\src\llvm_package_15.0.1\llvm-project\compiler-rt\lib\asan\asan_fake_stack.cpp:257
    #15 0x7ff9903bff06  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18003ff06)
    #16 0x7ff9903f2f2d  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x180072f2d)
    #17 0x7ff9903f2cdd  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x180072cdd)
    #18 0x7ff9903cdb3a  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18004db3a)
    #19 0x7ff9903bbafe  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18003bafe)
    #20 0x7ff9903c4dfb  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x180044dfb)
    #21 0x7ff9903baeb5  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18003aeb5)
    #22 0x7ff98dbf3ec1  (C:\WINDOWS\System32\KERNELBASE.dll+0x180033ec1)
    #23 0x7ff98dc3b4f0  (C:\WINDOWS\System32\KERNELBASE.dll+0x18007b4f0)
    #24 0x7ff98dc37f6e  (C:\WINDOWS\System32\KERNELBASE.dll+0x180077f6e)
    #25 0x7ff63d25e6ec in main (C:\projects\hello-world\odr.exe+0x14004e6ec)
    #26 0x7ff63d25f693 in invoke_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
    #27 0x7ff63d25f693 in __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #28 0x7ff98ed154df  (C:\WINDOWS\System32\KERNEL32.DLL+0x1800154df)
    #29 0x7ff99038485a  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18000485a)

==52800==HINT: if you don't care about these errors you may set ASAN_OPTIONS=detect_odr_violation=0
SUMMARY: AddressSanitizer: odr-violation: global 'unsigned __int64 `extern "C" __local_stdio_printf_options'::`2'::_OptionsStorage' at odr-dll0.cpp
==52800==ABORTING
PS C:\projects\hello-world>
```
Have I hit some strange bug or am I doing something wrong?  If I turn off `-flto` on `odr-dll1.cpp` then I don't hit this problem..

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWt9vozgQ_mvIi9UIbCDwkIe0SXera5tV09Npn5ABk3B1cGRMf9xff2NDEmjabRpdszopVZpgj_3Nx8x4wCPHIn0ZWr7dfOyxZY8sfAkfJFJ5lnJu95PVai0geZHwKmXIIhelSnPRX1hk0kjN96PIU6SnWTiwcIiswXktQOZvJfNCZUaGzSjvooBLGGmRZqA1GLcBu2ycX7Chct6m8wmyDhBKFlSiRBSlQhYeoWypLHyB-v3-zl2YkXGVWd45tt3A8sYb7lr8SCOeAwpdve4uFZWAGoAEoI2GsDumLDYG0gpgVJn_w0SGmlbDSgPsY7AlzYu93deRPuVFKp7KHYsBOaRR0VvubTtXD3rDuXpYQzCRtFywEi2YZNrMGzmtlEALWqRcUxmja0HT6zyWVL6ManC4tz64ba_Aiaucp_2Yqro34bSYo7OspEWuwLaggKapZGWJzhLo56C7HfnorNUWn8ZwXmE4-2CUEGAsBWFVsjOegojztMFtkWlhNhqMUfag2I6NNTnTBvyP53dptSc3LNgza2Bg8Vw2PY1TuqlmWqlVpSwyelP6Y4YutMy7WEnxN0tUCZcLxrk4exJSM5igPnR1NJjAq9HI-H__ad2IhwPbri8nd3fTO7AMGtU-mTVekrpP--MxF5yqXJh1aj8Psix0iR2EoWubFTNarzbIYQ4kMJNmADiA8YOqKPN5AQEYRbCefReBQ9izYlKj4Qv4BwkXCeWRSSJRve4jsdIaS0DQCoj2Jt40omktnSkh6ZxB_xsPGMMH_2Y-rUfMPeQnhuZcxJSX6AkSFZJsDsmd6fVJFVJmwEqAvnLXqq0enfWIjYwrfJJijMMsRZBHo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