diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 58be34fd6..ebf08db01 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -167,6 +167,8 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_gui.py $) add_test(NAME guest_intel_trampolines COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_intel_trampolines.py $) + add_test(NAME guest_tls COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_tls.py $) add_test(NAME elf_header COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_elf_header.py $) else() diff --git a/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp b/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp index 2d6b7b3f1..2623629ec 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -148,6 +149,13 @@ std::vector GuestModuleWriter::WriteWindows(const Relinker::GuestI directories[0] = {nextRva, CheckedRva(data.size())}; nextRva = AlignRva(nextRva + data.size()); sections.push_back(std::move(exportSection)); + if (tlsIndex != 0) { + auto imports = WindowsImportBuilder().Build(nextRva); + directories[1] = imports.Directory; + directories[12] = imports.AddressTable; + nextRva = AlignRva(nextRva + imports.Section.Data.size()); + sections.push_back(std::move(imports.Section)); + } auto relocationData = WindowsRelocationBuilder().BuildBaseRelocations(relocations); if (!relocationData.empty()) { directories[5] = {nextRva, CheckedRva(relocationData.size())}; diff --git a/core/relinker/relinker/tests/test_guest_tls.py b/core/relinker/relinker/tests/test_guest_tls.py new file mode 100644 index 000000000..4735a3f0b --- /dev/null +++ b/core/relinker/relinker/tests/test_guest_tls.py @@ -0,0 +1,73 @@ +"""Check that Windows guest modules with TLS get their TLS set up by the Windows loader.""" + +import ctypes +import os +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from test_guest_intel_trampolines import PLAIN_SITE, guest_fixture, main_fixture + + +def guest_with_tls(tls): + image = guest_fixture(PLAIN_SITE) + if not tls: + return image + struct.pack_into("