authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-22 09:37:32+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-22 12:06:02+02:00
log06a0da3e8a34d12de7adece6223d4235c4673aaf
treefc13aa129468878e74c8c0ce19c565dac78b65fb
parent79fefec599fd940adfd611c9da880c01e2aa842c

macho: cache string len


7 files changed, 93 insertions(+), 73 deletions(-)

src/link/MachO.zig+5
......@@ -4578,6 +4578,11 @@ pub const SymbolResolver = struct {
45784578 pub const Index = u32;
45794579};
45804580
4581pub const String = struct {
4582 pos: u32 = 0,
4583 len: u32 = 0,
4584};
4585
45814586const MachO = @This();
45824587
45834588const std = @import("std");
src/link/MachO/Atom.zig+2-2
......@@ -2,7 +2,7 @@
22value: u64 = 0,
33
44/// Name of this Atom.
5name: u32 = 0,
5name: MachO.String = .{},
66
77/// Index into linker's input file table.
88file: File.Index = 0,
......@@ -42,7 +42,7 @@ extra: u32 = 0,
4242pub fn getName(self: Atom, macho_file: *MachO) [:0]const u8 {
4343 return switch (self.getFile(macho_file)) {
4444 .dylib => unreachable,
45 .zig_object => |x| x.strtab.getAssumeExists(self.name),
45 .zig_object => |x| x.strtab.buffer.items[self.name.pos..][0 .. self.name.len - 1 :0],
4646 inline else => |x| x.getString(self.name),
4747 };
4848}
src/link/MachO/Dylib.zig+10-7
......@@ -610,15 +610,18 @@ pub inline fn getUmbrella(self: Dylib, macho_file: *MachO) *Dylib {
610610 return macho_file.getFile(self.umbrella).?.dylib;
611611}
612612
613fn addString(self: *Dylib, allocator: Allocator, name: []const u8) !u32 {
613fn addString(self: *Dylib, allocator: Allocator, name: []const u8) !MachO.String {
614614 const off = @as(u32, @intCast(self.strtab.items.len));
615 try self.strtab.writer(allocator).print("{s}\x00", .{name});
616 return off;
615 try self.strtab.ensureUnusedCapacity(allocator, name.len + 1);
616 self.strtab.appendSliceAssumeCapacity(name);
617 self.strtab.appendAssumeCapacity(0);
618 return .{ .pos = off, .len = @intCast(name.len + 1) };
617619}
618620
619pub fn getString(self: Dylib, off: u32) [:0]const u8 {
620 assert(off < self.strtab.items.len);
621 return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0);
621pub fn getString(self: Dylib, string: MachO.String) [:0]const u8 {
622 assert(string.pos < self.strtab.items.len and string.pos + string.len <= self.strtab.items.len);
623 if (string.len == 0) return "";
624 return self.strtab.items[string.pos..][0 .. string.len - 1 :0];
622625}
623626
624627pub fn asFile(self: *Dylib) File {
......@@ -932,7 +935,7 @@ pub const Id = struct {
932935};
933936
934937const Export = struct {
935 name: u32,
938 name: MachO.String,
936939 flags: Flags,
937940
938941 const Flags = packed struct {
src/link/MachO/InternalObject.zig+17-16
......@@ -53,7 +53,7 @@ pub fn init(self: *InternalObject, allocator: Allocator) !void {
5353
5454pub fn initSymbols(self: *InternalObject, macho_file: *MachO) !void {
5555 const newSymbolAssumeCapacity = struct {
56 fn newSymbolAssumeCapacity(obj: *InternalObject, name: u32, args: struct {
56 fn newSymbolAssumeCapacity(obj: *InternalObject, name: MachO.String, args: struct {
5757 type: u8 = macho.N_UNDF | macho.N_EXT,
5858 desc: u16 = 0,
5959 }) Symbol.Index {
......@@ -69,7 +69,7 @@ pub fn initSymbols(self: *InternalObject, macho_file: *MachO) !void {
6969 const nlist_idx: u32 = @intCast(obj.symtab.items.len);
7070 const nlist = obj.symtab.addOneAssumeCapacity();
7171 nlist.* = .{
72 .n_strx = name,
72 .n_strx = name.pos,
7373 .n_type = args.type,
7474 .n_sect = 0,
7575 .n_desc = args.desc,
......@@ -197,16 +197,16 @@ pub fn resolveBoundarySymbols(self: *InternalObject, macho_file: *MachO) !void {
197197 try self.globals.ensureUnusedCapacity(gpa, nsyms);
198198
199199 for (boundary_symbols.keys(), boundary_symbols.values()) |name, ref| {
200 const name_off = try self.addString(gpa, name);
200 const name_str = try self.addString(gpa, name);
201201 const sym_index = self.addSymbolAssumeCapacity();
202202 self.boundary_symbols.appendAssumeCapacity(sym_index);
203203 const sym = &self.symbols.items[sym_index];
204 sym.name = name_off;
204 sym.name = name_str;
205205 sym.visibility = .local;
206206 const nlist_idx: u32 = @intCast(self.symtab.items.len);
207207 const nlist = self.symtab.addOneAssumeCapacity();
208208 nlist.* = .{
209 .n_strx = name_off,
209 .n_strx = name_str.pos,
210210 .n_type = macho.N_SECT,
211211 .n_sect = 0,
212212 .n_desc = 0,
......@@ -273,7 +273,7 @@ fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_fil
273273 const nlist_idx: u32 = @intCast(self.symtab.items.len);
274274 const nlist = try self.symtab.addOne(gpa);
275275 nlist.* = .{
276 .n_strx = name_str,
276 .n_strx = name_str.pos,
277277 .n_type = macho.N_SECT,
278278 .n_sect = @intCast(n_sect + 1),
279279 .n_desc = 0,
......@@ -373,15 +373,15 @@ pub fn resolveObjcMsgSendSymbols(self: *InternalObject, macho_file: *MachO) !voi
373373 const name = MachO.eatPrefix(sym_name, "_objc_msgSend$").?;
374374 const selrefs_index = try self.addObjcMsgsendSections(name, macho_file);
375375
376 const name_off = try self.addString(gpa, sym_name);
376 const name_str = try self.addString(gpa, sym_name);
377377 const sym_index = try self.addSymbol(gpa);
378378 const sym = &self.symbols.items[sym_index];
379 sym.name = name_off;
379 sym.name = name_str;
380380 sym.visibility = .hidden;
381381 const nlist_idx: u32 = @intCast(self.symtab.items.len);
382382 const nlist = try self.symtab.addOne(gpa);
383383 nlist.* = .{
384 .n_strx = name_off,
384 .n_strx = name_str.pos,
385385 .n_type = macho.N_SECT | macho.N_EXT | macho.N_PEXT,
386386 .n_sect = 0,
387387 .n_desc = 0,
......@@ -624,17 +624,18 @@ fn getSectionData(self: *const InternalObject, index: u32) error{Overflow}![]con
624624 @panic("ref to non-existent section");
625625}
626626
627pub fn addString(self: *InternalObject, allocator: Allocator, name: []const u8) !u32 {
627pub fn addString(self: *InternalObject, allocator: Allocator, string: []const u8) !MachO.String {
628628 const off: u32 = @intCast(self.strtab.items.len);
629 try self.strtab.ensureUnusedCapacity(allocator, name.len + 1);
630 self.strtab.appendSliceAssumeCapacity(name);
629 try self.strtab.ensureUnusedCapacity(allocator, string.len + 1);
630 self.strtab.appendSliceAssumeCapacity(string);
631631 self.strtab.appendAssumeCapacity(0);
632 return off;
632 return .{ .pos = off, .len = @intCast(string.len + 1) };
633633}
634634
635pub fn getString(self: InternalObject, off: u32) [:0]const u8 {
636 assert(off < self.strtab.items.len);
637 return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0);
635pub fn getString(self: InternalObject, string: MachO.String) [:0]const u8 {
636 assert(string.pos < self.strtab.items.len and string.pos + string.len <= self.strtab.items.len);
637 if (string.len == 0) return "";
638 return self.strtab.items[string.pos..][0 .. string.len - 1 :0];
638639}
639640
640641pub fn asFile(self: *InternalObject) File {
src/link/MachO/Object.zig+30-24
......@@ -178,7 +178,7 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
178178
179179 fn rank(ctx: *const Object, nl: macho.nlist_64) u8 {
180180 if (!nl.ext()) {
181 const name = ctx.getString(nl.n_strx);
181 const name = ctx.getNStrx(nl.n_strx);
182182 if (name.len == 0) return 5;
183183 if (name[0] == 'l' or name[0] == 'L') return 4;
184184 return 3;
......@@ -341,7 +341,7 @@ fn initSubsections(self: *Object, allocator: Allocator, nlists: anytype) !void {
341341 else
342342 sect.@"align";
343343 const atom_index = try self.addAtom(allocator, .{
344 .name = nlist.nlist.n_strx,
344 .name = .{ .pos = nlist.nlist.n_strx, .len = @intCast(self.getNStrx(nlist.nlist.n_strx).len + 1) },
345345 .n_sect = @intCast(n_sect),
346346 .off = nlist.nlist.n_value - sect.addr,
347347 .size = size,
......@@ -465,7 +465,7 @@ fn initCstringLiterals(self: *Object, allocator: Allocator, file: File.Handle, m
465465 const nlist_index: u32 = @intCast(try self.symtab.addOne(allocator));
466466 self.symtab.set(nlist_index, .{
467467 .nlist = .{
468 .n_strx = name_str,
468 .n_strx = name_str.pos,
469469 .n_type = macho.N_SECT,
470470 .n_sect = @intCast(atom.n_sect + 1),
471471 .n_desc = 0,
......@@ -532,7 +532,7 @@ fn initFixedSizeLiterals(self: *Object, allocator: Allocator, macho_file: *MachO
532532 const nlist_index: u32 = @intCast(try self.symtab.addOne(allocator));
533533 self.symtab.set(nlist_index, .{
534534 .nlist = .{
535 .n_strx = name_str,
535 .n_strx = name_str.pos,
536536 .n_type = macho.N_SECT,
537537 .n_sect = @intCast(atom.n_sect + 1),
538538 .n_desc = 0,
......@@ -590,7 +590,7 @@ fn initPointerLiterals(self: *Object, allocator: Allocator, macho_file: *MachO)
590590 const nlist_index: u32 = @intCast(try self.symtab.addOne(allocator));
591591 self.symtab.set(nlist_index, .{
592592 .nlist = .{
593 .n_strx = name_str,
593 .n_strx = name_str.pos,
594594 .n_type = macho.N_SECT,
595595 .n_sect = @intCast(atom.n_sect + 1),
596596 .n_desc = 0,
......@@ -796,7 +796,7 @@ fn linkNlistToAtom(self: *Object, macho_file: *MachO) !void {
796796 atom.* = atom_index;
797797 } else {
798798 try macho_file.reportParseError2(self.index, "symbol {s} not attached to any (sub)section", .{
799 self.getString(nlist.n_strx),
799 self.getNStrx(nlist.n_strx),
800800 });
801801 return error.MalformedObject;
802802 }
......@@ -821,7 +821,7 @@ fn initSymbols(self: *Object, allocator: Allocator, macho_file: *MachO) !void {
821821 const index = self.addSymbolAssumeCapacity();
822822 const symbol = &self.symbols.items[index];
823823 symbol.value = nlist.n_value;
824 symbol.name = nlist.n_strx;
824 symbol.name = .{ .pos = nlist.n_strx, .len = @intCast(self.getNStrx(nlist.n_strx).len + 1) };
825825 symbol.nlist_idx = @intCast(i);
826826 symbol.extra = self.addSymbolExtraAssumeCapacity(.{});
827827
......@@ -894,7 +894,7 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f
894894 defer addr_lookup.deinit();
895895 for (syms) |sym| {
896896 if (sym.sect() and (sym.ext() or sym.pext())) {
897 try addr_lookup.putNoClobber(self.getString(sym.n_strx), sym.n_value);
897 try addr_lookup.putNoClobber(self.getNStrx(sym.n_strx), sym.n_value);
898898 }
899899 }
900900
......@@ -926,7 +926,7 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f
926926 },
927927 macho.N_GSYM => {
928928 stab.is_func = false;
929 stab.index = sym_lookup.find(addr_lookup.get(self.getString(nlist.n_strx)).?);
929 stab.index = sym_lookup.find(addr_lookup.get(self.getNStrx(nlist.n_strx)).?);
930930 },
931931 macho.N_STSYM => {
932932 stab.is_func = false;
......@@ -1708,7 +1708,7 @@ pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, macho_file: *M
17081708 const gpa = macho_file.base.comp.gpa;
17091709 for (self.symtab.items(.nlist)) |nlist| {
17101710 if (!nlist.ext() or (nlist.undf() and !nlist.tentative())) continue;
1711 const off = try ar_symtab.strtab.insert(gpa, self.getString(nlist.n_strx));
1711 const off = try ar_symtab.strtab.insert(gpa, self.getNStrx(nlist.n_strx));
17121712 try ar_symtab.entries.append(gpa, .{ .off = off, .file = self.index });
17131713 }
17141714}
......@@ -2292,17 +2292,23 @@ pub fn getAtomRelocs(self: *const Object, atom: Atom, macho_file: *MachO) []cons
22922292 return relocs.items[extra.rel_index..][0..extra.rel_count];
22932293}
22942294
2295fn addString(self: *Object, allocator: Allocator, name: [:0]const u8) error{OutOfMemory}!u32 {
2295fn addString(self: *Object, allocator: Allocator, string: [:0]const u8) error{OutOfMemory}!MachO.String {
22962296 const off: u32 = @intCast(self.strtab.items.len);
2297 try self.strtab.ensureUnusedCapacity(allocator, name.len + 1);
2298 self.strtab.appendSliceAssumeCapacity(name);
2297 try self.strtab.ensureUnusedCapacity(allocator, string.len + 1);
2298 self.strtab.appendSliceAssumeCapacity(string);
22992299 self.strtab.appendAssumeCapacity(0);
2300 return off;
2300 return .{ .pos = off, .len = @intCast(string.len + 1) };
23012301}
23022302
2303pub fn getString(self: Object, off: u32) [:0]const u8 {
2304 assert(off < self.strtab.items.len);
2305 return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0);
2303pub fn getString(self: Object, string: MachO.String) [:0]const u8 {
2304 assert(string.pos < self.strtab.items.len and string.pos + string.len <= self.strtab.items.len);
2305 if (string.len == 0) return "";
2306 return self.strtab.items[string.pos..][0 .. string.len - 1 :0];
2307}
2308
2309fn getNStrx(self: Object, n_strx: u32) [:0]const u8 {
2310 assert(n_strx < self.strtab.items.len);
2311 return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + n_strx)), 0);
23062312}
23072313
23082314pub fn hasUnwindRecords(self: Object) bool {
......@@ -2323,7 +2329,7 @@ fn hasSymbolStabs(self: Object) bool {
23232329
23242330fn hasObjC(self: Object) bool {
23252331 for (self.symtab.items(.nlist)) |nlist| {
2326 const name = self.getString(nlist.n_strx);
2332 const name = self.getNStrx(nlist.n_strx);
23272333 if (mem.startsWith(u8, name, "_OBJC_CLASS_$_")) return true;
23282334 }
23292335 for (self.sections.items(.header)) |sect| {
......@@ -2346,7 +2352,7 @@ pub fn asFile(self: *Object) File {
23462352}
23472353
23482354const AddAtomArgs = struct {
2349 name: u32,
2355 name: MachO.String,
23502356 n_sect: u8,
23512357 off: u64,
23522358 size: u64,
......@@ -2690,17 +2696,17 @@ const StabFile = struct {
26902696
26912697 fn getCompDir(sf: StabFile, object: Object) [:0]const u8 {
26922698 const nlist = object.symtab.items(.nlist)[sf.comp_dir];
2693 return object.getString(nlist.n_strx);
2699 return object.getNStrx(nlist.n_strx);
26942700 }
26952701
26962702 fn getTuName(sf: StabFile, object: Object) [:0]const u8 {
26972703 const nlist = object.symtab.items(.nlist)[sf.comp_dir + 1];
2698 return object.getString(nlist.n_strx);
2704 return object.getNStrx(nlist.n_strx);
26992705 }
27002706
27012707 fn getOsoPath(sf: StabFile, object: Object) [:0]const u8 {
27022708 const nlist = object.symtab.items(.nlist)[sf.comp_dir + 2];
2703 return object.getString(nlist.n_strx);
2709 return object.getNStrx(nlist.n_strx);
27042710 }
27052711
27062712 fn getOsoModTime(sf: StabFile, object: Object) u64 {
......@@ -2758,8 +2764,8 @@ const StabFile = struct {
27582764};
27592765
27602766const CompileUnit = struct {
2761 comp_dir: u32,
2762 tu_name: u32,
2767 comp_dir: MachO.String,
2768 tu_name: MachO.String,
27632769
27642770 fn getCompDir(cu: CompileUnit, object: Object) [:0]const u8 {
27652771 return object.getString(cu.comp_dir);
src/link/MachO/Symbol.zig+2-2
......@@ -4,7 +4,7 @@
44value: u64 = 0,
55
66/// Offset into the linker's intern table.
7name: u32 = 0,
7name: MachO.String = .{},
88
99/// File where this symbol is defined.
1010file: File.Index = 0,
......@@ -57,7 +57,7 @@ pub fn weakRef(symbol: Symbol, macho_file: *MachO) bool {
5757
5858pub fn getName(symbol: Symbol, macho_file: *MachO) [:0]const u8 {
5959 return switch (symbol.getFile(macho_file).?) {
60 .zig_object => |x| x.strtab.getAssumeExists(symbol.name),
60 .zig_object => |x| x.strtab.buffer.items[symbol.name.pos..][0 .. symbol.name.len - 1 :0],
6161 inline else => |x| x.getString(symbol.name),
6262 };
6363}
src/link/MachO/ZigObject.zig+27-22
......@@ -141,7 +141,7 @@ pub fn deinit(self: *ZigObject, allocator: Allocator) void {
141141 }
142142}
143143
144fn newSymbol(self: *ZigObject, allocator: Allocator, name: u32, args: struct {
144fn newSymbol(self: *ZigObject, allocator: Allocator, name: MachO.String, args: struct {
145145 type: u8 = macho.N_UNDF | macho.N_EXT,
146146 desc: u16 = 0,
147147}) !Symbol.Index {
......@@ -158,7 +158,7 @@ fn newSymbol(self: *ZigObject, allocator: Allocator, name: u32, args: struct {
158158 const nlist_idx: u32 = @intCast(self.symtab.addOneAssumeCapacity());
159159 self.symtab.set(nlist_idx, .{
160160 .nlist = .{
161 .n_strx = name,
161 .n_strx = name.pos,
162162 .n_type = args.type,
163163 .n_sect = 0,
164164 .n_desc = args.desc,
......@@ -174,7 +174,7 @@ fn newSymbol(self: *ZigObject, allocator: Allocator, name: u32, args: struct {
174174 return index;
175175}
176176
177fn newAtom(self: *ZigObject, allocator: Allocator, name: u32, macho_file: *MachO) !Atom.Index {
177fn newAtom(self: *ZigObject, allocator: Allocator, name: MachO.String, macho_file: *MachO) !Atom.Index {
178178 try self.atoms.ensureUnusedCapacity(allocator, 1);
179179 try self.atoms_extra.ensureUnusedCapacity(allocator, @sizeOf(Atom.Extra));
180180 try self.atoms_indexes.ensureUnusedCapacity(allocator, 1);
......@@ -192,7 +192,7 @@ fn newAtom(self: *ZigObject, allocator: Allocator, name: u32, macho_file: *MachO
192192 return index;
193193}
194194
195fn newSymbolWithAtom(self: *ZigObject, allocator: Allocator, name: u32, macho_file: *MachO) !Symbol.Index {
195fn newSymbolWithAtom(self: *ZigObject, allocator: Allocator, name: MachO.String, macho_file: *MachO) !Symbol.Index {
196196 const atom_index = try self.newAtom(allocator, name, macho_file);
197197 const sym_index = try self.newSymbol(allocator, name, .{ .type = macho.N_SECT });
198198 const sym = &self.symbols.items[sym_index];
......@@ -992,10 +992,10 @@ fn updateDeclCode(
992992
993993 const sym_name = try std.fmt.allocPrintZ(gpa, "_{s}", .{decl.fqn.toSlice(ip)});
994994 defer gpa.free(sym_name);
995 sym.name = try self.strtab.insert(gpa, sym_name);
995 sym.name = try self.addString(gpa, sym_name);
996996 atom.setAlive(true);
997997 atom.name = sym.name;
998 nlist.n_strx = sym.name;
998 nlist.n_strx = sym.name.pos;
999999 nlist.n_type = macho.N_SECT;
10001000 nlist.n_sect = sect_index + 1;
10011001 self.symtab.items(.size)[sym.nlist_idx] = code.len;
......@@ -1090,9 +1090,9 @@ fn createTlvInitializer(
10901090 const gpa = macho_file.base.comp.gpa;
10911091 const sym_name = try std.fmt.allocPrint(gpa, "{s}$tlv$init", .{name});
10921092 defer gpa.free(sym_name);
1093 const off = try self.strtab.insert(gpa, sym_name);
1093 const string = try self.addString(gpa, sym_name);
10941094
1095 const sym_index = try self.newSymbolWithAtom(gpa, off, macho_file);
1095 const sym_index = try self.newSymbolWithAtom(gpa, string, macho_file);
10961096 const sym = &self.symbols.items[sym_index];
10971097 const nlist = &self.symtab.items(.nlist)[sym.nlist_idx];
10981098 const atom = sym.getAtom(macho_file).?;
......@@ -1142,10 +1142,10 @@ fn createTlvDescriptor(
11421142 atom.out_n_sect = sect_index;
11431143
11441144 sym.value = 0;
1145 sym.name = try self.strtab.insert(gpa, name);
1145 sym.name = try self.addString(gpa, name);
11461146 atom.setAlive(true);
11471147 atom.name = sym.name;
1148 nlist.n_strx = sym.name;
1148 nlist.n_strx = sym.name.pos;
11491149 nlist.n_sect = sect_index + 1;
11501150 nlist.n_type = macho.N_SECT;
11511151 nlist.n_value = 0;
......@@ -1296,8 +1296,8 @@ fn lowerConst(
12961296 var code_buffer = std.ArrayList(u8).init(gpa);
12971297 defer code_buffer.deinit();
12981298
1299 const name_str_index = try self.strtab.insert(gpa, name);
1300 const sym_index = try self.newSymbolWithAtom(gpa, name_str_index, macho_file);
1299 const name_str = try self.addString(gpa, name);
1300 const sym_index = try self.newSymbolWithAtom(gpa, name_str, macho_file);
13011301
13021302 const res = try codegen.generateSymbol(&macho_file.base, pt, src_loc, val, &code_buffer, .{
13031303 .none = {},
......@@ -1447,13 +1447,13 @@ fn updateLazySymbol(
14471447 var code_buffer = std.ArrayList(u8).init(gpa);
14481448 defer code_buffer.deinit();
14491449
1450 const name_str_index = blk: {
1450 const name_str = blk: {
14511451 const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{
14521452 @tagName(lazy_sym.kind),
14531453 lazy_sym.ty.fmt(pt),
14541454 });
14551455 defer gpa.free(name);
1456 break :blk try self.strtab.insert(gpa, name);
1456 break :blk try self.addString(gpa, name);
14571457 };
14581458
14591459 const src = lazy_sym.ty.srcLocOrNull(mod) orelse Module.LazySrcLoc.unneeded;
......@@ -1480,18 +1480,18 @@ fn updateLazySymbol(
14801480 .const_data => macho_file.zig_const_sect_index.?,
14811481 };
14821482 const sym = &self.symbols.items[symbol_index];
1483 sym.name = name_str_index;
1483 sym.name = name_str;
14841484 sym.out_n_sect = output_section_index;
14851485
14861486 const nlist = &self.symtab.items(.nlist)[sym.nlist_idx];
1487 nlist.n_strx = name_str_index;
1487 nlist.n_strx = name_str.pos;
14881488 nlist.n_type = macho.N_SECT;
14891489 nlist.n_sect = output_section_index + 1;
14901490 self.symtab.items(.size)[sym.nlist_idx] = code.len;
14911491
14921492 const atom = sym.getAtom(macho_file).?;
14931493 atom.setAlive(true);
1494 atom.name = name_str_index;
1494 atom.name = name_str;
14951495 atom.alignment = required_alignment;
14961496 atom.size = code.len;
14971497 atom.out_n_sect = output_section_index;
......@@ -1553,10 +1553,10 @@ pub fn getGlobalSymbol(self: *ZigObject, macho_file: *MachO, name: []const u8, l
15531553 const gpa = macho_file.base.comp.gpa;
15541554 const sym_name = try std.fmt.allocPrint(gpa, "_{s}", .{name});
15551555 defer gpa.free(sym_name);
1556 const off = try self.strtab.insert(gpa, sym_name);
1557 const lookup_gop = try self.globals_lookup.getOrPut(gpa, off);
1556 const name_str = try self.addString(gpa, sym_name);
1557 const lookup_gop = try self.globals_lookup.getOrPut(gpa, name_str.pos);
15581558 if (!lookup_gop.found_existing) {
1559 const sym_index = try self.newSymbol(gpa, off, .{});
1559 const sym_index = try self.newSymbol(gpa, name_str, .{});
15601560 const sym = &self.symbols.items[sym_index];
15611561 lookup_gop.value_ptr.* = sym.nlist_idx;
15621562 }
......@@ -1571,7 +1571,7 @@ pub fn getOrCreateMetadataForDecl(
15711571 const gpa = macho_file.base.comp.gpa;
15721572 const gop = try self.decls.getOrPut(gpa, decl_index);
15731573 if (!gop.found_existing) {
1574 const sym_index = try self.newSymbolWithAtom(gpa, 0, macho_file);
1574 const sym_index = try self.newSymbolWithAtom(gpa, .{}, macho_file);
15751575 const sym = &self.symbols.items[sym_index];
15761576 if (isThreadlocal(macho_file, decl_index)) {
15771577 sym.flags.tlv = true;
......@@ -1609,7 +1609,7 @@ pub fn getOrCreateMetadataForLazySymbol(
16091609 };
16101610 switch (metadata.state.*) {
16111611 .unused => {
1612 const symbol_index = try self.newSymbolWithAtom(gpa, 0, macho_file);
1612 const symbol_index = try self.newSymbolWithAtom(gpa, .{}, macho_file);
16131613 const sym = &self.symbols.items[symbol_index];
16141614 sym.setSectionFlags(.{ .needs_zig_got = true });
16151615 metadata.symbol_index.* = symbol_index;
......@@ -1762,6 +1762,11 @@ pub fn setSymbolExtra(self: *ZigObject, index: u32, extra: Symbol.Extra) void {
17621762 }
17631763}
17641764
1765fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !MachO.String {
1766 const off = try self.strtab.insert(allocator, string);
1767 return .{ .pos = off, .len = @intCast(string.len + 1) };
1768}
1769
17651770pub fn asFile(self: *ZigObject) File {
17661771 return .{ .zig_object = self };
17671772}