authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-15 22:33:09+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-18 09:13:08+02:00
loga9e3088d9ce9edd61765e610e5d1a5ea8a5f6733
treeba949fb4a1d8a8e2401745e15821b8f34cf78d42
parent103c16c87955facd373bd20435100fa7eb322349

macho: extract testing logic for TLS into a helper


1 files changed, 14 insertions(+), 15 deletions(-)

src/link/MachO/ZigObject.zig+14-15
...@@ -958,14 +958,11 @@ pub fn updateDecl(...@@ -958,14 +958,11 @@ pub fn updateDecl(
958 return;958 return;
959 },959 },
960 };960 };
961 const sect_index = try self.getDeclOutputSection(macho_file, decl, code);961 if (isThreadlocal(macho_file, decl_index)) {
962 const is_threadlocal = switch (macho_file.sections.items(.header)[sect_index].type()) {962 const sect_index = try self.getDeclOutputSection(macho_file, decl, code);
963 macho.S_THREAD_LOCAL_ZEROFILL, macho.S_THREAD_LOCAL_REGULAR => true,
964 else => false,
965 };
966 if (is_threadlocal) {
967 try self.updateTlv(macho_file, pt, decl_index, sym_index, sect_index, code);963 try self.updateTlv(macho_file, pt, decl_index, sym_index, sect_index, code);
968 } else {964 } else {
965 const sect_index = try self.getDeclOutputSection(macho_file, decl, code);
969 try self.updateDeclCode(macho_file, pt, decl_index, sym_index, sect_index, code);966 try self.updateDeclCode(macho_file, pt, decl_index, sym_index, sect_index, code);
970 }967 }
971968
...@@ -1590,17 +1587,11 @@ pub fn getOrCreateMetadataForDecl(...@@ -1590,17 +1587,11 @@ pub fn getOrCreateMetadataForDecl(
1590 const gpa = macho_file.base.comp.gpa;1587 const gpa = macho_file.base.comp.gpa;
1591 const gop = try self.decls.getOrPut(gpa, decl_index);1588 const gop = try self.decls.getOrPut(gpa, decl_index);
1592 if (!gop.found_existing) {1589 if (!gop.found_existing) {
1593 const any_non_single_threaded = macho_file.base.comp.config.any_non_single_threaded;
1594 const sym_index = try self.newSymbolWithAtom(gpa, 0, macho_file);1590 const sym_index = try self.newSymbolWithAtom(gpa, 0, macho_file);
1595 const sym = &self.symbols.items[sym_index];1591 const sym = &self.symbols.items[sym_index];
1596 const mod = macho_file.base.comp.module.?;1592 if (isThreadlocal(macho_file, decl_index)) {
1597 const decl = mod.declPtr(decl_index);1593 sym.flags.tlv = true;
1598 if (decl.getOwnedVariable(mod)) |variable| {1594 } else {
1599 if (variable.is_threadlocal and any_non_single_threaded) {
1600 sym.flags.tlv = true;
1601 }
1602 }
1603 if (!sym.flags.tlv) {
1604 sym.flags.needs_zig_got = true;1595 sym.flags.needs_zig_got = true;
1605 }1596 }
1606 gop.value_ptr.* = .{ .symbol_index = sym_index };1597 gop.value_ptr.* = .{ .symbol_index = sym_index };
...@@ -1649,6 +1640,14 @@ pub fn getOrCreateMetadataForLazySymbol(...@@ -1649,6 +1640,14 @@ pub fn getOrCreateMetadataForLazySymbol(
1649 return symbol_index;1640 return symbol_index;
1650}1641}
16511642
1643fn isThreadlocal(macho_file: *MachO, decl_index: InternPool.DeclIndex) bool {
1644 const any_non_single_threaded = macho_file.base.comp.config.any_non_single_threaded;
1645 const zcu = macho_file.base.comp.module.?;
1646 const decl = zcu.declPtr(decl_index);
1647 const variable = decl.getOwnedVariable(zcu) orelse return false;
1648 return variable.is_threadlocal and any_non_single_threaded;
1649}
1650
1652fn addAtom(self: *ZigObject, allocator: Allocator) !Atom.Index {1651fn addAtom(self: *ZigObject, allocator: Allocator) !Atom.Index {
1653 try self.atoms.ensureUnusedCapacity(allocator, 1);1652 try self.atoms.ensureUnusedCapacity(allocator, 1);
1654 try self.atoms_extra.ensureUnusedCapacity(allocator, 1);1653 try self.atoms_extra.ensureUnusedCapacity(allocator, 1);