master
1const std = @import("std");
2const builtin = @import("builtin");
3const windows = std.os.windows;
4
5export var _tls_index: u32 = std.os.windows.TLS_OUT_OF_INDEXES;
6export var _tls_start: ?*anyopaque linksection(".tls") = null;
7export var _tls_end: ?*anyopaque linksection(".tls$ZZZ") = null;
8export var __xl_a: windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLA") = null;
9export var __xl_z: windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLZ") = null;
10
11comptime {
12 if (builtin.cpu.arch == .x86 and !builtin.abi.isGnu() and builtin.zig_backend != .stage2_c) {
13 // The __tls_array is the offset of the ThreadLocalStoragePointer field
14 // in the TEB block whose base address held in the %fs segment.
15 asm (
16 \\ .global __tls_array
17 \\ __tls_array = 0x2C
18 );
19 }
20}
21
22// TODO this is how I would like it to be expressed
23//export const _tls_used linksection(".rdata$T") = std.os.windows.IMAGE_TLS_DIRECTORY {
24// .StartAddressOfRawData = @intFromPtr(&_tls_start),
25// .EndAddressOfRawData = @intFromPtr(&_tls_end),
26// .AddressOfIndex = @intFromPtr(&_tls_index),
27// .AddressOfCallBacks = @intFromPtr(__xl_a),
28// .SizeOfZeroFill = 0,
29// .Characteristics = 0,
30//};
31// This is the workaround because we can't do @intFromPtr at comptime like that.
32pub const IMAGE_TLS_DIRECTORY = extern struct {
33 StartAddressOfRawData: *?*anyopaque,
34 EndAddressOfRawData: *?*anyopaque,
35 AddressOfIndex: *u32,
36 AddressOfCallBacks: [*:null]windows.PIMAGE_TLS_CALLBACK,
37 SizeOfZeroFill: u32,
38 Characteristics: u32,
39};
40export const _tls_used linksection(".rdata$T") = IMAGE_TLS_DIRECTORY{
41 .StartAddressOfRawData = &_tls_start,
42 .EndAddressOfRawData = &_tls_end,
43 .AddressOfIndex = &_tls_index,
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,
48 .SizeOfZeroFill = 0,
49 .Characteristics = 0,
50};