KTL icon indicating copy to clipboard operation
KTL copied to clipboard

Fix CXX initializers

Open DymOK93 opened this issue 3 years ago • 1 comments

Recent MSVC compiler uses

using ktl::crt::global_c_handler_t = int(__cdecl*)();
using ktl::crt::global_cxx_handler_t = void(__cdecl*)();

CRTALLOC(".CRT$XIA")
ktl::crt::global_c_handler_t __xi_a[] {nullptr};  // C initializers (first)
CRTALLOC(".CRT$XIZ")
ktl::crt::global_c_handler_t __xi_z[] {nullptr};  // C initializers (last)
CRTALLOC(".CRT$XCA")
ktl::crt::global__cxx_handler_t __xc_a[] {nullptr};  // C++ initializers (first)
CRTALLOC(".CRT$XCZ")
ktl::crt::global_cxx_handler_t __xc_z[] {nullptr};  // C++ initializers (last)
CRTALLOC(".CRT$XPA")
ktl::crt::global_cxx_handler_t __xp_a[] {nullptr};  // C pre-terminators (first)
CRTALLOC(".CRT$XPZ")
ktl::crt::global_cxx_handler_t __xp_z[] {nullptr};  // C pre-terminators (last)
CRTALLOC(".CRT$XTA")
ktl::crt::global_cxx_handler_t __xt_a[] {nullptr};  // C terminators (first)
CRTALLOC(".CRT$XTZ")
ktl::crt::global_cxx_handler_t __xt_z[] {nullptr};  // C terminators (last)

instead of old initializers provided by KTL.

Usually MSVC places C++ initializers between __cxx_ctors_begin__ and __cxx_ctors_end__ symbols as expected, but with LTO enabled (/GL compiler option) includes these symbols in reverse order into a weird ?__cxx_c section without the initializers themselves:

изображение

Reproducible on MSVC 19.28+

DymOK93 avatar Jan 12 '22 14:01 DymOK93

The problem turned out to be simpler ... Symbol names aren't important: a bug occurs only if these symbols are marked as inline and LTO is enabled. Without an inline specifier (symbols can be left in the header file since it is included in one TU) - everything is OK. I suppose it is due to the fact that the phase of forming the section of dynamic initializers in .rodata occurs before the resolution of symbols with a weak linkage.

DymOK93 avatar Jan 12 '22 16:01 DymOK93