Skip to content

Commit 8184912

Browse files
authored
Merge pull request #20925 from alexrp/windows-tls
`std`: Some Windows TLS cleanup and fixes
2 parents c0681d6 + cb1fffb commit 8184912

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
const std = @import("std");
22
const builtin = @import("builtin");
3+
const windows = std.os.windows;
34

45
export var _tls_index: u32 = std.os.windows.TLS_OUT_OF_INDEXES;
5-
export var _tls_start: u8 linksection(".tls") = 0;
6-
export var _tls_end: u8 linksection(".tls$ZZZ") = 0;
7-
export var __xl_a: std.os.windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLA") = null;
8-
export var __xl_z: std.os.windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLZ") = null;
6+
export var _tls_start: ?*anyopaque linksection(".tls") = null;
7+
export var _tls_end: ?*anyopaque linksection(".tls$ZZZ") = null;
8+
export var __xl_a: windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLA") = null;
9+
export var __xl_z: windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLZ") = null;
910

1011
comptime {
11-
if (builtin.target.cpu.arch == .x86 and builtin.zig_backend != .stage2_c) {
12+
if (builtin.cpu.arch == .x86 and builtin.abi == .msvc and builtin.zig_backend != .stage2_c) {
1213
// The __tls_array is the offset of the ThreadLocalStoragePointer field
1314
// in the TEB block whose base address held in the %fs segment.
1415
asm (
@@ -19,8 +20,6 @@ comptime {
1920
}
2021

2122
// TODO this is how I would like it to be expressed
22-
// TODO also note, ReactOS has a +1 on StartAddressOfRawData and AddressOfCallBacks. Investigate
23-
// why they do that.
2423
//export const _tls_used linksection(".rdata$T") = std.os.windows.IMAGE_TLS_DIRECTORY {
2524
// .StartAddressOfRawData = @intFromPtr(&_tls_start),
2625
// .EndAddressOfRawData = @intFromPtr(&_tls_end),
@@ -31,18 +30,21 @@ comptime {
3130
//};
3231
// This is the workaround because we can't do @intFromPtr at comptime like that.
3332
pub const IMAGE_TLS_DIRECTORY = extern struct {
34-
StartAddressOfRawData: *anyopaque,
35-
EndAddressOfRawData: *anyopaque,
36-
AddressOfIndex: *anyopaque,
37-
AddressOfCallBacks: *anyopaque,
33+
StartAddressOfRawData: *?*anyopaque,
34+
EndAddressOfRawData: *?*anyopaque,
35+
AddressOfIndex: *u32,
36+
AddressOfCallBacks: [*:null]windows.PIMAGE_TLS_CALLBACK,
3837
SizeOfZeroFill: u32,
3938
Characteristics: u32,
4039
};
4140
export const _tls_used linksection(".rdata$T") = IMAGE_TLS_DIRECTORY{
4241
.StartAddressOfRawData = &_tls_start,
4342
.EndAddressOfRawData = &_tls_end,
4443
.AddressOfIndex = &_tls_index,
45-
.AddressOfCallBacks = @as(*anyopaque, @ptrCast(&__xl_a)),
44+
// __xl_a is just a global variable containing a null pointer; the actual callbacks sit in
45+
// between __xl_a and __xl_z. So we need to skip over __xl_a here. If there are no callbacks,
46+
// this just means we point to __xl_z (the null terminator).
47+
.AddressOfCallBacks = @as([*:null]windows.PIMAGE_TLS_CALLBACK, @ptrCast(&__xl_a)) + 1,
4648
.SizeOfZeroFill = 0,
4749
.Characteristics = 0,
4850
};

lib/std/start.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn _DllMainCRTStartup(
176176
lpReserved: std.os.windows.LPVOID,
177177
) callconv(std.os.windows.WINAPI) std.os.windows.BOOL {
178178
if (!builtin.single_threaded and !builtin.link_libc) {
179-
_ = @import("start_windows_tls.zig");
179+
_ = @import("os/windows/tls.zig");
180180
}
181181

182182
if (@hasDecl(root, "DllMain")) {
@@ -434,7 +434,7 @@ fn _start() callconv(.Naked) noreturn {
434434
fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {
435435
@setAlignStack(16);
436436
if (!builtin.single_threaded and !builtin.link_libc) {
437-
_ = @import("start_windows_tls.zig");
437+
_ = @import("os/windows/tls.zig");
438438
}
439439

440440
std.debug.maybeEnableSegfaultHandler();
@@ -445,7 +445,7 @@ fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {
445445
fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {
446446
@setAlignStack(16);
447447
if (!builtin.single_threaded and !builtin.link_libc) {
448-
_ = @import("start_windows_tls.zig");
448+
_ = @import("os/windows/tls.zig");
449449
}
450450

451451
std.debug.maybeEnableSegfaultHandler();

0 commit comments

Comments
 (0)