Merge pull request #20925 from alexrp/windows-tls

`std`: Some Windows TLS cleanup and fixes
This commit is contained in:
Andrew Kelley 2024-08-07 01:12:11 -07:00 committed by GitHub
commit 8184912a98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 15 deletions

View File

@ -1,14 +1,15 @@
const std = @import("std");
const builtin = @import("builtin");
const windows = std.os.windows;
export var _tls_index: u32 = std.os.windows.TLS_OUT_OF_INDEXES;
export var _tls_start: u8 linksection(".tls") = 0;
export var _tls_end: u8 linksection(".tls$ZZZ") = 0;
export var __xl_a: std.os.windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLA") = null;
export var __xl_z: std.os.windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLZ") = null;
export var _tls_start: ?*anyopaque linksection(".tls") = null;
export var _tls_end: ?*anyopaque linksection(".tls$ZZZ") = null;
export var __xl_a: windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLA") = null;
export var __xl_z: windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLZ") = null;
comptime {
if (builtin.target.cpu.arch == .x86 and builtin.zig_backend != .stage2_c) {
if (builtin.cpu.arch == .x86 and builtin.abi == .msvc and builtin.zig_backend != .stage2_c) {
// The __tls_array is the offset of the ThreadLocalStoragePointer field
// in the TEB block whose base address held in the %fs segment.
asm (
@ -19,8 +20,6 @@ comptime {
}
// TODO this is how I would like it to be expressed
// TODO also note, ReactOS has a +1 on StartAddressOfRawData and AddressOfCallBacks. Investigate
// why they do that.
//export const _tls_used linksection(".rdata$T") = std.os.windows.IMAGE_TLS_DIRECTORY {
// .StartAddressOfRawData = @intFromPtr(&_tls_start),
// .EndAddressOfRawData = @intFromPtr(&_tls_end),
@ -31,10 +30,10 @@ comptime {
//};
// This is the workaround because we can't do @intFromPtr at comptime like that.
pub const IMAGE_TLS_DIRECTORY = extern struct {
StartAddressOfRawData: *anyopaque,
EndAddressOfRawData: *anyopaque,
AddressOfIndex: *anyopaque,
AddressOfCallBacks: *anyopaque,
StartAddressOfRawData: *?*anyopaque,
EndAddressOfRawData: *?*anyopaque,
AddressOfIndex: *u32,
AddressOfCallBacks: [*:null]windows.PIMAGE_TLS_CALLBACK,
SizeOfZeroFill: u32,
Characteristics: u32,
};
@ -42,7 +41,10 @@ export const _tls_used linksection(".rdata$T") = IMAGE_TLS_DIRECTORY{
.StartAddressOfRawData = &_tls_start,
.EndAddressOfRawData = &_tls_end,
.AddressOfIndex = &_tls_index,
.AddressOfCallBacks = @as(*anyopaque, @ptrCast(&__xl_a)),
// __xl_a is just a global variable containing a null pointer; the actual callbacks sit in
// between __xl_a and __xl_z. So we need to skip over __xl_a here. If there are no callbacks,
// this just means we point to __xl_z (the null terminator).
.AddressOfCallBacks = @as([*:null]windows.PIMAGE_TLS_CALLBACK, @ptrCast(&__xl_a)) + 1,
.SizeOfZeroFill = 0,
.Characteristics = 0,
};

View File

@ -176,7 +176,7 @@ fn _DllMainCRTStartup(
lpReserved: std.os.windows.LPVOID,
) callconv(std.os.windows.WINAPI) std.os.windows.BOOL {
if (!builtin.single_threaded and !builtin.link_libc) {
_ = @import("start_windows_tls.zig");
_ = @import("os/windows/tls.zig");
}
if (@hasDecl(root, "DllMain")) {
@ -434,7 +434,7 @@ fn _start() callconv(.Naked) noreturn {
fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {
@setAlignStack(16);
if (!builtin.single_threaded and !builtin.link_libc) {
_ = @import("start_windows_tls.zig");
_ = @import("os/windows/tls.zig");
}
std.debug.maybeEnableSegfaultHandler();
@ -445,7 +445,7 @@ fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {
fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {
@setAlignStack(16);
if (!builtin.single_threaded and !builtin.link_libc) {
_ = @import("start_windows_tls.zig");
_ = @import("os/windows/tls.zig");
}
std.debug.maybeEnableSegfaultHandler();