clang/clang-cl undefined symbol "__tls_index" (custom CRT implementation)

785 views Asked by At

I'm currently implementing my own CRT using Visual Studio 2022 and clang-cl (12.0.0 (ships with VS now) and 13.0.0) and when using /NODEFAULTLIB I'm getting undefined symbols on certain variables/functions despite implementing them.

// main.cpp
thread_local static int32_t test{};

int32_t __stdcall DllMain(HMODULE self, uint32_t reason, void* reserved)
{
    switch(reason)
    {
        case DLL_PROCESS_ATTACH:
        {
            ++test;
            return 0;
        }

        case DLL_PROCESS_DETACH:
        {
            return 1;
        }
    }

    return 0;
}

// crt.cpp
extern "C"
{
    // TLS index for this binary.
    uint32_t _tls_index{};
}

extern "C" int32_t __stdcall _DllMainCRTStartup(HMODULE self, uint32_t reason, void* reserved)
{
    // ...CRT init here...
    // Call into the client's entry point.
    const auto res = DllMain(self, reason, reserved);
    return res;
}

This results in this error on compile:

1>lld-link : error : undefined symbol: __tls_index
1>>>> referenced by [redacted]\src\crt.cpp:44
1>>>>               [redacted].dll.lto.1.obj:(__DllMainCRTStartup@12)
1>>>> referenced by [redacted]\src\main.cpp:11
1>>>>               [redacted].dll.lto.2.obj:(_DllMain@12)

Any idea why this happens? Are there specific compiler options that I'm forgetting with clang-cl? This all works fine under MSVC (the symbol is found with no issue).

0

There are 0 answers