diff --git a/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp b/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp index a77ac4001..3d111335a 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp @@ -109,11 +109,12 @@ PeDirectory WindowsTlsBuilder::Build(const std::vector& source, co const auto blockSize = CheckedRva((tls->MemorySize + alignment - 1) & ~(alignment - 1)); const auto templateOffset = CheckedRva((64 + alignment - 1) & ~(alignment - 1)); constexpr std::uint32_t threadControlBlockSize = 0x30; + constexpr std::int64_t stackGuardDisplacement = 0x28; for (const auto& access : accesses) { if (access.StoreImmediate || access.Displacement == 0) continue; const auto displacement = static_cast(std::bit_cast(access.Displacement)); - if (displacement < -static_cast(blockSize) || displacement + 8 > threadControlBlockSize) - throw Domain::RelinkerException("Windows guest TLS load displacement " + std::to_string(displacement) + " is outside the thread TLS block", access.FileOffset); + if (displacement != stackGuardDisplacement && (displacement > 0 || displacement < -static_cast(blockSize))) + throw Domain::RelinkerException("Windows guest TLS load displacement " + std::to_string(displacement) + " is not in the thread TLS block, 0 or the stack guard at 0x28", access.FileOffset); } PeSection data{".gtls", nextRva, SectionRead | SectionWrite | 0x40u, std::vector(templateOffset + blockSize + threadControlBlockSize)}; diff --git a/core/relinker/relinker/tests/test_tls_function_coverage.py b/core/relinker/relinker/tests/test_tls_function_coverage.py index 31b2ce8df..31606aa11 100644 --- a/core/relinker/relinker/tests/test_tls_function_coverage.py +++ b/core/relinker/relinker/tests/test_tls_function_coverage.py @@ -60,8 +60,8 @@ def displacement_load(register, displacement, flags, round_trip=False): immediate(1, 0x2468ace013579bdf) emit(bytes.fromhex("48 89 08")) if displacement == 0: - emit(fs_load(2, 8)) - immediate(1, positive[8]) + emit(fs_load(2, 40)) + immediate(1, positive[40]) emit(bytes.fromhex("48 39 ca")) check() else: @@ -115,8 +115,8 @@ def displacement_cases(): if register == 4: continue for flags in (0x202, 0xad7): - for displacement, round_trip in ((0, False), (8, False), (16, False), (40, False), - (-64, False), (-40, False), (-8, False), (40, True)): + for displacement, round_trip in ((0, False), (40, False), (-64, False), (-40, False), + (-8, False), (40, True)): offset, body = displacement_load(register, displacement, flags, round_trip) image = make_image("register", "unwind", body=body) struct.pack_into("