diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 2daa44c99..f82728eb4 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -76,6 +76,8 @@ if(BUILD_TESTING) find_package(Python3 REQUIRED COMPONENTS Interpreter) add_test(NAME optional_plt COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_optional_plt.py $) + add_test(NAME empty_tls COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_empty_tls.py $) add_executable(strict_nid_filter_tests ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/StrictNidFilterTests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/analysis/UnusedNidFilter/StrictNidFilter.cpp diff --git a/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp b/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp index f2390a534..ea58d8138 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp @@ -77,8 +77,15 @@ PeDirectory WindowsTlsBuilder::Build(const std::vector& source, co return {}; } - if (tls->MemorySize == 0 || tls->FileSize > tls->MemorySize || tls->Offset > source.size() || tls->FileSize > source.size() - tls->Offset || !std::has_single_bit(tls->Alignment) || tls->Alignment > 8192 || tls->MemorySize > 0x7fff0000u) + if (tls->FileSize > tls->MemorySize || tls->Offset > source.size() || tls->FileSize > source.size() - tls->Offset || !std::has_single_bit(tls->Alignment) || tls->Alignment > 8192 || tls->MemorySize > 0x7fff0000u) throw Domain::RelinkerException("Invalid or unsupported ELF TLS layout", tls->Offset); + // Native homebrew linkers can emit an empty PT_TLS placeholder. + // It needs no Windows TLS directory unless guest code accesses TLS. + if (tls->MemorySize == 0) { + if (!accesses.empty()) + throw Domain::RelinkerException("Guest TLS access with empty PT_TLS", tls->Offset); + return {}; + } const auto alignment = std::max(tls->Alignment, 16); const auto blockSize = CheckedRva((tls->MemorySize + alignment - 1) & ~(alignment - 1)); const auto templateOffset = CheckedRva((64 + alignment - 1) & ~(alignment - 1)); diff --git a/core/relinker/relinker/tests/test_empty_tls.py b/core/relinker/relinker/tests/test_empty_tls.py new file mode 100644 index 000000000..aaf0c633e --- /dev/null +++ b/core/relinker/relinker/tests/test_empty_tls.py @@ -0,0 +1,50 @@ +"""Validate empty PT_TLS handling through ELF conversion and PE execution.""" + +import os +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from test_optional_plt import fixture + + +def main(): + relinker = Path(sys.argv[1]).resolve() + cases = [ + ("empty", 0, 0, False, None), + ("nonempty", 8, 16, False, None), + ("invalid-sizes", 1, 0, False, "Invalid or unsupported ELF TLS layout"), + ("empty-access", 0, 0, True, "Guest TLS access with empty PT_TLS"), + ] + with tempfile.TemporaryDirectory(prefix="anyps5-tls-") as directory: + for name, file_size, memory_size, access, error in cases: + source = Path(directory) / (name + ".elf") + output = source.with_suffix(".exe") + data = fixture() + struct.pack_into("