0

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).

f4rw3llvv
  • 53
  • 5
  • I think you are simply missing an underscore at the start of `_tls_index{};` in crt.cpp. The error says `__tls_index{};`, which has 2. –  Nov 11 '21 at 05:50
  • @Frank No difference. I believe most errors like this prefix an extra underscore. For example, if you don't define `_DllMainCRTStartup` the error will show up as `undefined symbol: __DllMainCRTStartup`. Anyway, the MSVC CRT (which is used by default without `/NODEFAULTLIB` defines as `_tls_index`. – f4rw3llvv Nov 11 '21 at 05:55
  • please provide full `clang-cl` command line – Alex Guteniev Nov 11 '21 at 12:17
  • do you compile 32-bit or 64-bit? – Alex Guteniev Nov 11 '21 at 12:25
  • @AlexGuteniev I'm compiling for 32-bit at the moment (x86-32) but I do plan on making this universal down the line (That's a conversation for another topic). Here is my full compiler/linker output from Visual Studio: https://pastebin.com/FASNMSnW – f4rw3llvv Nov 11 '21 at 13:56

0 Answers0