authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-04 08:48:48+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-13 11:47:51+02:00
log1795b8eb4e8db95cb639349c6c1bc4b0b82741bb
treec6bba1429161f3372ec8153d3a0f523f7d4ebde8
parentcf9ba2965eb90de994dd648950ad81041f90b755

macho: emit TLS sections


1 files changed, 29 insertions(+), 1 deletions(-)

src/link/MachO.zig+29-1
......@@ -138,6 +138,8 @@ got_section_index: ?u8 = null,
138138data_const_section_index: ?u8 = null,
139139la_symbol_ptr_section_index: ?u8 = null,
140140data_section_index: ?u8 = null,
141tls_vars_section_index: ?u8 = null,
142tls_data_section_index: ?u8 = null,
141143
142144locals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
143145globals: std.ArrayListUnmanaged(SymbolWithLoc) = .{},
......@@ -2366,6 +2368,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {
23662368 const val = decl.val;
23672369 const zig_ty = ty.zigTypeTag();
23682370 const mode = self.base.options.optimize_mode;
2371 const single_threaded = self.base.options.single_threaded;
23692372 const sect_id: u8 = blk: {
23702373 // TODO finish and audit this function
23712374 if (val.isUndefDeep()) {
......@@ -2376,7 +2379,10 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {
23762379 }
23772380 }
23782381
2379 if (val.castTag(.variable)) |_| {
2382 if (val.castTag(.variable)) |variable| {
2383 if (variable.data.is_threadlocal and !single_threaded) {
2384 break :blk self.tls_data_section_index.?;
2385 }
23802386 break :blk self.data_section_index.?;
23812387 }
23822388
......@@ -2800,6 +2806,28 @@ fn populateMissingMetadata(self: *MachO) !void {
28002806 self.segment_table_dirty = true;
28012807 }
28022808
2809 if (!self.base.options.single_threaded) {
2810 if (self.tls_vars_section_index == null) {
2811 self.tls_vars_section_index = try self.allocateSection("__DATA2", "__thread_vars", .{
2812 .size = @sizeOf(u64) * 3,
2813 .alignment = @alignOf(u64),
2814 .flags = macho.S_THREAD_LOCAL_VARIABLES,
2815 .prot = macho.PROT.READ | macho.PROT.WRITE,
2816 });
2817 self.segment_table_dirty = true;
2818 }
2819
2820 if (self.tls_data_section_index == null) {
2821 self.tls_data_section_index = try self.allocateSection("__DATA3", "__thread_data", .{
2822 .size = @sizeOf(u64),
2823 .alignment = @alignOf(u64),
2824 .flags = macho.S_THREAD_LOCAL_REGULAR,
2825 .prot = macho.PROT.READ | macho.PROT.WRITE,
2826 });
2827 self.segment_table_dirty = true;
2828 }
2829 }
2830
28032831 if (self.linkedit_segment_cmd_index == null) {
28042832 self.linkedit_segment_cmd_index = @intCast(u8, self.segments.items.len);
28052833