From 2e04b61275062f4a3672cdea2456effbc0cb22a5 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Fri, 6 Mar 2020 09:17:14 +0100 Subject: [PATCH] std: Work around unexported NtCurrentTeb Apparently NtCurrentTeb is only exported for i386 and some other platforms but not for x86_64 nor AArch64. Let's go with the flow and provide our own NtCurrentTeb like the Windows headers do. Thank you Microsoft. --- lib/std/os/windows.zig | 20 +++++++++++++++++++- lib/std/os/windows/ntdll.zig | 1 - 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index 14e7b2b04c..ba55845a4f 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -1083,8 +1083,26 @@ pub fn SetFileTime( } } +pub fn teb() *TEB { + return switch (builtin.arch) { + .i386 => asm volatile ( + \\ movl %%fs:0x18, %[ptr] + : [ptr] "=r" (-> *TEB) + ), + .x86_64 => asm volatile ( + \\ movq %%gs:0x30, %[ptr] + : [ptr] "=r" (-> *TEB) + ), + .aarch64 => asm volatile ( + \\ mov %[ptr], x18 + : [ptr] "=r" (-> *TEB) + ), + else => @compileError("unsupported arch"), + }; +} + pub fn peb() *PEB { - return ntdll.NtCurrentTeb().ProcessEnvironmentBlock; + return teb().ProcessEnvironmentBlock; } /// A file time is a 64-bit value that represents the number of 100-nanosecond diff --git a/lib/std/os/windows/ntdll.zig b/lib/std/os/windows/ntdll.zig index 76a11a19c1..49e60803bc 100644 --- a/lib/std/os/windows/ntdll.zig +++ b/lib/std/os/windows/ntdll.zig @@ -16,7 +16,6 @@ pub extern "NtDll" fn NtQueryInformationFile( Length: ULONG, FileInformationClass: FILE_INFORMATION_CLASS, ) callconv(.Stdcall) NTSTATUS; -pub extern "NtDll" fn NtCurrentTeb() callconv(.Stdcall) *TEB; pub extern "NtDll" fn NtQueryAttributesFile( ObjectAttributes: *OBJECT_ATTRIBUTES,