aboutsummaryrefslogtreecommitdiff
path: root/lib/std/os
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2020-03-06 00:02:45 +0100
committerLemonBoy <thatlemon@gmail.com>2020-03-06 09:06:26 +0100
commitb9a1d67637cfae6206fffce78fc2e40780ef4fb5 (patch)
tree9b36f734cb9814ab03114ddecb0b4b0965858a6a /lib/std/os
parenteaccfffe56655ab11a382685886492e5b9b0865c (diff)
downloadzig-b9a1d67637cfae6206fffce78fc2e40780ef4fb5.tar.gz
zig-b9a1d67637cfae6206fffce78fc2e40780ef4fb5.zip
std: Nicer way to access the PEB
Use the NtCurrentTeb API instead of using some inline asm, this is much nicer and also more portable. Closes #4645
Diffstat (limited to 'lib/std/os')
-rw-r--r--lib/std/os/windows.zig17
-rw-r--r--lib/std/os/windows/bits.zig13
-rw-r--r--lib/std/os/windows/ntdll.zig1
3 files changed, 15 insertions, 16 deletions
diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig
index b8e14a220d..14e7b2b04c 100644
--- a/lib/std/os/windows.zig
+++ b/lib/std/os/windows.zig
@@ -1084,22 +1084,7 @@ pub fn SetFileTime(
}
pub fn peb() *PEB {
- switch (builtin.arch) {
- .i386 => {
- return asm (
- \\ mov %%fs:0x18, %[ptr]
- \\ mov %%ds:0x30(%[ptr]), %[ptr]
- : [ptr] "=r" (-> *PEB)
- );
- },
- .x86_64 => {
- return asm (
- \\ mov %%gs:0x60, %[ptr]
- : [ptr] "=r" (-> *PEB)
- );
- },
- else => @compileError("unsupported architecture"),
- }
+ return ntdll.NtCurrentTeb().ProcessEnvironmentBlock;
}
/// A file time is a 64-bit value that represents the number of 100-nanosecond
diff --git a/lib/std/os/windows/bits.zig b/lib/std/os/windows/bits.zig
index f9ba6cf40e..41466234ed 100644
--- a/lib/std/os/windows/bits.zig
+++ b/lib/std/os/windows/bits.zig
@@ -1154,6 +1154,19 @@ const RTL_BITMAP = @OpaqueType();
pub const PRTL_BITMAP = *RTL_BITMAP;
const KAFFINITY = usize;
+pub const TEB = extern struct {
+ Reserved1: [12]PVOID,
+ ProcessEnvironmentBlock: *PEB,
+ Reserved2: [399]PVOID,
+ Reserved3: [1952]u8,
+ TlsSlots: [64]PVOID,
+ Reserved4: [8]u8,
+ Reserved5: [26]PVOID,
+ ReservedForOle: PVOID,
+ Reserved6: [4]PVOID,
+ TlsExpansionSlots: PVOID,
+};
+
/// Process Environment Block
/// Microsoft documentation of this is incomplete, the fields here are taken from various resources including:
/// - https://github.com/wine-mirror/wine/blob/1aff1e6a370ee8c0213a0fd4b220d121da8527aa/include/winternl.h#L269
diff --git a/lib/std/os/windows/ntdll.zig b/lib/std/os/windows/ntdll.zig
index 49e60803bc..76a11a19c1 100644
--- a/lib/std/os/windows/ntdll.zig
+++ b/lib/std/os/windows/ntdll.zig
@@ -16,6 +16,7 @@ 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,