Skip to content

[LTO][MinGW] Cannot build a DLL containing auto-exported emulated-TLS variable for MinGW target if LTO enabled #145195

@kikairoya

Description

@kikairoya

When building a DLL for MinGW with LTO, the linker cannot resolve auto-exported emulated-TLS thread_local variables.

// pass
// RUN: clang++ -fuse-ld=lld %s -shared -o %t.dll -target x86_64-windows-gnu -femulated-tls -flto -Wl,--exclude-all-symbols
// RUN: clang++ -fuse-ld=lld %s -shared -o %t.dll -target x86_64-windows-gnu -fno-emulated-tls -flto 
// RUN: clang++ -fuse-ld=lld %s -shared -o %t.dll -target x86_64-windows-gnu -femulated-tls -flto -DUSE_DLLEXPORT
// RUN: clang++ -fuse-ld=lld %s -shared -o %t.dll -target x86_64-windows-gnu -femulated-tls -fno-lto
// RUN: llvm-readobj --coff-exports %t.dll | FileCheck %s
// RUN: clang++ -fuse-ld=lld %s -shared -o %t.dll -target x86_64-linux-gnu -femulated-tls -flto
// RUN: llvm-readobj --dt %t.dll | FileCheck %s

// CHECK: Name: __emutls_v.tls_var

// fail
// RUN: clang++ -fuse-ld=lld %s -shared -o %t.dll -target x86_64-windows-gnu -femulated-tls -flto

int thread_local tls_var;
#ifdef USE_DLLEXPORT
__declspec(dllexport) int other_var;
#endif

result:

ld.lld: error: <root>: undefined symbol: tls_var
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

The linker reports tls_var as undefined, although __emutls_v.tls_var should be referenced instead in emulated-TLS mode.
For Linux target, tls_var is resolved to __emutls_v.tls_var correctly.

Maybe related to: #134725 #79715

Metadata

Metadata

Assignees

No one assigned

    Labels

    LTOLink time optimization (regular/full LTO or ThinLTO)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions