diff --git a/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp b/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp index cec7b18d6..bdf125dfb 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp @@ -19,6 +19,7 @@ struct TlsAccess { bool StoreImmediate; std::uint32_t Immediate; std::uint8_t Register; + std::uint32_t Displacement; }; void patchAccess(std::vector& sections, const TlsAccess& access, const std::uint32_t target) { @@ -78,11 +79,11 @@ PeDirectory WindowsTlsBuilder::Build(const std::vector& source, co else if (value != 0x64 && !(prefix + 1 == position && value >= 0x40 && value <= 0x4f)) supportedPrefixes = false; } const auto loadRegister = info.Length - position == 7 ? static_cast(((bytes[position + 1] >> 3) & 7) | ((info.RexPrefix & 4) << 1)) : std::uint8_t{4}; - const bool loadPointer = supportedPrefixes && (info.RexPrefix == 0x48 || info.RexPrefix == 0x4c) && loadRegister != 4 && bytes[position] == 0x8b && (bytes[position + 1] & 0xc7) == 0x04 && bytes[position + 2] == 0x25 && Io::ReadU32(source, header.Offset + offset + position + 3) == 0; + const bool loadValue = supportedPrefixes && (info.RexPrefix == 0x48 || info.RexPrefix == 0x4c) && loadRegister != 4 && bytes[position] == 0x8b && (bytes[position + 1] & 0xc7) == 0x04 && bytes[position + 2] == 0x25; const bool storeImmediate = supportedPrefixes && !hasOperandSizePrefix && (info.RexPrefix == 0 || info.RexPrefix == 0x40) && info.Length - position == 11 && bytes[position] == 0xc7 && bytes[position + 1] == 0x04 && bytes[position + 2] == 0x25 && Io::ReadU32(source, header.Offset + offset + position + 3) == 0x28; - if (!loadPointer && !storeImmediate) + if (!loadValue && !storeImmediate) throw Domain::RelinkerException("Unsupported Windows guest TLS instruction", header.Offset + offset); - accesses.push_back({rva, info.Length, storeImmediate, storeImmediate ? Io::ReadU32(source, header.Offset + offset + position + 7) : 0, storeImmediate ? std::uint8_t{0} : loadRegister}); + accesses.push_back({rva, info.Length, storeImmediate, storeImmediate ? Io::ReadU32(source, header.Offset + offset + position + 7) : 0, storeImmediate ? std::uint8_t{0} : loadRegister, storeImmediate ? 0 : Io::ReadU32(source, header.Offset + offset + position + 3)}); } } } @@ -143,6 +144,10 @@ PeDirectory WindowsTlsBuilder::Build(const std::vector& source, co if (preserveCounter) code.Emit({0x51}); if (preserveAccumulator) code.Emit({0x50}); loadPointer(); + if (!access.StoreImmediate && access.Displacement != 0) { + code.Emit({0x48, 0x8b, 0x80}); + code.U32(access.Displacement); + } if (access.StoreImmediate) { code.Emit({0xc7, 0x40, 0x28}); code.U32(access.Immediate); diff --git a/core/relinker/relinker/tests/test_tls_function_coverage.py b/core/relinker/relinker/tests/test_tls_function_coverage.py index 0eead3d5d..25446ae2a 100644 --- a/core/relinker/relinker/tests/test_tls_function_coverage.py +++ b/core/relinker/relinker/tests/test_tls_function_coverage.py @@ -26,6 +26,107 @@ def register_load(register): return len(prefix), prefix + load + rest + restore + bytes.fromhex("31 c0 c3") +def fs_load(register, displacement): + return bytes([0x64, 0x48 | (register >> 3) << 2, 0x8b, 0x04 | (register & 7) << 3, 0x25]) + struct.pack("> 3), 0xb8 | (target & 7)]) + struct.pack("> 3) << 2, 0x89, 0x84 | (target & 7) << 3, 0x24]) + struct.pack("= 8 else b"") + bytes([0x50 | (target & 7)])) + emit(bytes.fromhex("48 8d a4 24 00 ff ff ff")) + emit(fs_load(0, 0)) + stack_store(0, 136) + positive = {8: 0x123456789abcdef0, 16: 0xfedcba9876543210, 40: 0xabcdef1278563412} + for offset, value in positive.items(): + immediate(1, value) + emit(bytes([0x48, 0x89, 0x48, offset])) + immediate(1, 0x2468ace013579bdf) + emit(bytes.fromhex("48 89 08")) + if displacement == 0: + emit(fs_load(2, 8)) + immediate(1, positive[8]) + emit(bytes.fromhex("48 39 ca")) + check() + else: + value = 0xabcdef1289abcdef if round_trip else { + **positive, -64: 0x8877665544332211, -40: 0x1122334455667788, -8: 0, + }[displacement] + immediate(1, value) + stack_store(1, 136) + markers = [0x1020304050607000 + target * 0x101 for target in range(16)] + for target in range(16): + if target != 4: + immediate(target, markers[target]) + emit(b"\x68" + struct.pack("= 8 else b"") + bytes([0x58 | (target & 7)])) + emit(b"\xc3") + for offset in failures: + struct.pack_into("