| author | |
| committer | |
| log | 06a0da3e8a34d12de7adece6223d4235c4673aaf |
| tree | fc13aa129468878e74c8c0ce19c565dac78b65fb |
| parent | 79fefec599fd940adfd611c9da880c01e2aa842c |
7 files changed, 93 insertions(+), 73 deletions(-)
src/link/MachO.zig+5| ... | ... | @@ -4578,6 +4578,11 @@ pub const SymbolResolver = struct { |
| 4578 | 4578 | pub const Index = u32; |
| 4579 | 4579 | }; |
| 4580 | 4580 | |
| 4581 | pub const String = struct { | |
| 4582 | pos: u32 = 0, | |
| 4583 | len: u32 = 0, | |
| 4584 | }; | |
| 4585 | ||
| 4581 | 4586 | const MachO = @This(); |
| 4582 | 4587 | |
| 4583 | 4588 | const std = @import("std"); |
src/link/MachO/Atom.zig+2-2| ... | ... | @@ -2,7 +2,7 @@ |
| 2 | 2 | value: u64 = 0, |
| 3 | 3 | |
| 4 | 4 | /// Name of this Atom. |
| 5 | name: u32 = 0, | |
| 5 | name: MachO.String = .{}, | |
| 6 | 6 | |
| 7 | 7 | /// Index into linker's input file table. |
| 8 | 8 | file: File.Index = 0, |
| ... | ... | @@ -42,7 +42,7 @@ extra: u32 = 0, |
| 42 | 42 | pub fn getName(self: Atom, macho_file: *MachO) [:0]const u8 { |
| 43 | 43 | return switch (self.getFile(macho_file)) { |
| 44 | 44 | .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], | |
| 46 | 46 | inline else => |x| x.getString(self.name), |
| 47 | 47 | }; |
| 48 | 48 | } |
src/link/MachO/Dylib.zig+10-7| ... | ... | @@ -610,15 +610,18 @@ pub inline fn getUmbrella(self: Dylib, macho_file: *MachO) *Dylib { |
| 610 | 610 | return macho_file.getFile(self.umbrella).?.dylib; |
| 611 | 611 | } |
| 612 | 612 | |
| 613 | fn addString(self: *Dylib, allocator: Allocator, name: []const u8) !u32 { | |
| 613 | fn addString(self: *Dylib, allocator: Allocator, name: []const u8) !MachO.String { | |
| 614 | 614 | 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) }; | |
| 617 | 619 | } |
| 618 | 620 | |
| 619 | pub 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); | |
| 621 | pub 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]; | |
| 622 | 625 | } |
| 623 | 626 | |
| 624 | 627 | pub fn asFile(self: *Dylib) File { |
| ... | ... | @@ -932,7 +935,7 @@ pub const Id = struct { |
| 932 | 935 | }; |
| 933 | 936 | |
| 934 | 937 | const Export = struct { |
| 935 | name: u32, | |
| 938 | name: MachO.String, | |
| 936 | 939 | flags: Flags, |
| 937 | 940 | |
| 938 | 941 | const Flags = packed struct { |
src/link/MachO/InternalObject.zig+17-16| ... | ... | @@ -53,7 +53,7 @@ pub fn init(self: *InternalObject, allocator: Allocator) !void { |
| 53 | 53 | |
| 54 | 54 | pub fn initSymbols(self: *InternalObject, macho_file: *MachO) !void { |
| 55 | 55 | const newSymbolAssumeCapacity = struct { |
| 56 | fn newSymbolAssumeCapacity(obj: *InternalObject, name: u32, args: struct { | |
| 56 | fn newSymbolAssumeCapacity(obj: *InternalObject, name: MachO.String, args: struct { | |
| 57 | 57 | type: u8 = macho.N_UNDF | macho.N_EXT, |
| 58 | 58 | desc: u16 = 0, |
| 59 | 59 | }) Symbol.Index { |
| ... | ... | @@ -69,7 +69,7 @@ pub fn initSymbols(self: *InternalObject, macho_file: *MachO) !void { |
| 69 | 69 | const nlist_idx: u32 = @intCast(obj.symtab.items.len); |
| 70 | 70 | const nlist = obj.symtab.addOneAssumeCapacity(); |
| 71 | 71 | nlist.* = .{ |
| 72 | .n_strx = name, | |
| 72 | .n_strx = name.pos, | |
| 73 | 73 | .n_type = args.type, |
| 74 | 74 | .n_sect = 0, |
| 75 | 75 | .n_desc = args.desc, |
| ... | ... | @@ -197,16 +197,16 @@ pub fn resolveBoundarySymbols(self: *InternalObject, macho_file: *MachO) !void { |
| 197 | 197 | try self.globals.ensureUnusedCapacity(gpa, nsyms); |
| 198 | 198 | |
| 199 | 199 | 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); | |
| 201 | 201 | const sym_index = self.addSymbolAssumeCapacity(); |
| 202 | 202 | self.boundary_symbols.appendAssumeCapacity(sym_index); |
| 203 | 203 | const sym = &self.symbols.items[sym_index]; |
| 204 | sym.name = name_off; | |
| 204 | sym.name = name_str; | |
| 205 | 205 | sym.visibility = .local; |
| 206 | 206 | const nlist_idx: u32 = @intCast(self.symtab.items.len); |
| 207 | 207 | const nlist = self.symtab.addOneAssumeCapacity(); |
| 208 | 208 | nlist.* = .{ |
| 209 | .n_strx = name_off, | |
| 209 | .n_strx = name_str.pos, | |
| 210 | 210 | .n_type = macho.N_SECT, |
| 211 | 211 | .n_sect = 0, |
| 212 | 212 | .n_desc = 0, |
| ... | ... | @@ -273,7 +273,7 @@ fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_fil |
| 273 | 273 | const nlist_idx: u32 = @intCast(self.symtab.items.len); |
| 274 | 274 | const nlist = try self.symtab.addOne(gpa); |
| 275 | 275 | nlist.* = .{ |
| 276 | .n_strx = name_str, | |
| 276 | .n_strx = name_str.pos, | |
| 277 | 277 | .n_type = macho.N_SECT, |
| 278 | 278 | .n_sect = @intCast(n_sect + 1), |
| 279 | 279 | .n_desc = 0, |
| ... | ... | @@ -373,15 +373,15 @@ pub fn resolveObjcMsgSendSymbols(self: *InternalObject, macho_file: *MachO) !voi |
| 373 | 373 | const name = MachO.eatPrefix(sym_name, "_objc_msgSend$").?; |
| 374 | 374 | const selrefs_index = try self.addObjcMsgsendSections(name, macho_file); |
| 375 | 375 | |
| 376 | const name_off = try self.addString(gpa, sym_name); | |
| 376 | const name_str = try self.addString(gpa, sym_name); | |
| 377 | 377 | const sym_index = try self.addSymbol(gpa); |
| 378 | 378 | const sym = &self.symbols.items[sym_index]; |
| 379 | sym.name = name_off; | |
| 379 | sym.name = name_str; | |
| 380 | 380 | sym.visibility = .hidden; |
| 381 | 381 | const nlist_idx: u32 = @intCast(self.symtab.items.len); |
| 382 | 382 | const nlist = try self.symtab.addOne(gpa); |
| 383 | 383 | nlist.* = .{ |
| 384 | .n_strx = name_off, | |
| 384 | .n_strx = name_str.pos, | |
| 385 | 385 | .n_type = macho.N_SECT | macho.N_EXT | macho.N_PEXT, |
| 386 | 386 | .n_sect = 0, |
| 387 | 387 | .n_desc = 0, |
| ... | ... | @@ -624,17 +624,18 @@ fn getSectionData(self: *const InternalObject, index: u32) error{Overflow}![]con |
| 624 | 624 | @panic("ref to non-existent section"); |
| 625 | 625 | } |
| 626 | 626 | |
| 627 | pub fn addString(self: *InternalObject, allocator: Allocator, name: []const u8) !u32 { | |
| 627 | pub fn addString(self: *InternalObject, allocator: Allocator, string: []const u8) !MachO.String { | |
| 628 | 628 | 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); | |
| 631 | 631 | self.strtab.appendAssumeCapacity(0); |
| 632 | return off; | |
| 632 | return .{ .pos = off, .len = @intCast(string.len + 1) }; | |
| 633 | 633 | } |
| 634 | 634 | |
| 635 | pub 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); | |
| 635 | pub 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]; | |
| 638 | 639 | } |
| 639 | 640 | |
| 640 | 641 | pub 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 { |
| 178 | 178 | |
| 179 | 179 | fn rank(ctx: *const Object, nl: macho.nlist_64) u8 { |
| 180 | 180 | if (!nl.ext()) { |
| 181 | const name = ctx.getString(nl.n_strx); | |
| 181 | const name = ctx.getNStrx(nl.n_strx); | |
| 182 | 182 | if (name.len == 0) return 5; |
| 183 | 183 | if (name[0] == 'l' or name[0] == 'L') return 4; |
| 184 | 184 | return 3; |
| ... | ... | @@ -341,7 +341,7 @@ fn initSubsections(self: *Object, allocator: Allocator, nlists: anytype) !void { |
| 341 | 341 | else |
| 342 | 342 | sect.@"align"; |
| 343 | 343 | 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) }, | |
| 345 | 345 | .n_sect = @intCast(n_sect), |
| 346 | 346 | .off = nlist.nlist.n_value - sect.addr, |
| 347 | 347 | .size = size, |
| ... | ... | @@ -465,7 +465,7 @@ fn initCstringLiterals(self: *Object, allocator: Allocator, file: File.Handle, m |
| 465 | 465 | const nlist_index: u32 = @intCast(try self.symtab.addOne(allocator)); |
| 466 | 466 | self.symtab.set(nlist_index, .{ |
| 467 | 467 | .nlist = .{ |
| 468 | .n_strx = name_str, | |
| 468 | .n_strx = name_str.pos, | |
| 469 | 469 | .n_type = macho.N_SECT, |
| 470 | 470 | .n_sect = @intCast(atom.n_sect + 1), |
| 471 | 471 | .n_desc = 0, |
| ... | ... | @@ -532,7 +532,7 @@ fn initFixedSizeLiterals(self: *Object, allocator: Allocator, macho_file: *MachO |
| 532 | 532 | const nlist_index: u32 = @intCast(try self.symtab.addOne(allocator)); |
| 533 | 533 | self.symtab.set(nlist_index, .{ |
| 534 | 534 | .nlist = .{ |
| 535 | .n_strx = name_str, | |
| 535 | .n_strx = name_str.pos, | |
| 536 | 536 | .n_type = macho.N_SECT, |
| 537 | 537 | .n_sect = @intCast(atom.n_sect + 1), |
| 538 | 538 | .n_desc = 0, |
| ... | ... | @@ -590,7 +590,7 @@ fn initPointerLiterals(self: *Object, allocator: Allocator, macho_file: *MachO) |
| 590 | 590 | const nlist_index: u32 = @intCast(try self.symtab.addOne(allocator)); |
| 591 | 591 | self.symtab.set(nlist_index, .{ |
| 592 | 592 | .nlist = .{ |
| 593 | .n_strx = name_str, | |
| 593 | .n_strx = name_str.pos, | |
| 594 | 594 | .n_type = macho.N_SECT, |
| 595 | 595 | .n_sect = @intCast(atom.n_sect + 1), |
| 596 | 596 | .n_desc = 0, |
| ... | ... | @@ -796,7 +796,7 @@ fn linkNlistToAtom(self: *Object, macho_file: *MachO) !void { |
| 796 | 796 | atom.* = atom_index; |
| 797 | 797 | } else { |
| 798 | 798 | 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), | |
| 800 | 800 | }); |
| 801 | 801 | return error.MalformedObject; |
| 802 | 802 | } |
| ... | ... | @@ -821,7 +821,7 @@ fn initSymbols(self: *Object, allocator: Allocator, macho_file: *MachO) !void { |
| 821 | 821 | const index = self.addSymbolAssumeCapacity(); |
| 822 | 822 | const symbol = &self.symbols.items[index]; |
| 823 | 823 | 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) }; | |
| 825 | 825 | symbol.nlist_idx = @intCast(i); |
| 826 | 826 | symbol.extra = self.addSymbolExtraAssumeCapacity(.{}); |
| 827 | 827 | |
| ... | ... | @@ -894,7 +894,7 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f |
| 894 | 894 | defer addr_lookup.deinit(); |
| 895 | 895 | for (syms) |sym| { |
| 896 | 896 | 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); | |
| 898 | 898 | } |
| 899 | 899 | } |
| 900 | 900 | |
| ... | ... | @@ -926,7 +926,7 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f |
| 926 | 926 | }, |
| 927 | 927 | macho.N_GSYM => { |
| 928 | 928 | 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)).?); | |
| 930 | 930 | }, |
| 931 | 931 | macho.N_STSYM => { |
| 932 | 932 | stab.is_func = false; |
| ... | ... | @@ -1708,7 +1708,7 @@ pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, macho_file: *M |
| 1708 | 1708 | const gpa = macho_file.base.comp.gpa; |
| 1709 | 1709 | for (self.symtab.items(.nlist)) |nlist| { |
| 1710 | 1710 | 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)); | |
| 1712 | 1712 | try ar_symtab.entries.append(gpa, .{ .off = off, .file = self.index }); |
| 1713 | 1713 | } |
| 1714 | 1714 | } |
| ... | ... | @@ -2292,17 +2292,23 @@ pub fn getAtomRelocs(self: *const Object, atom: Atom, macho_file: *MachO) []cons |
| 2292 | 2292 | return relocs.items[extra.rel_index..][0..extra.rel_count]; |
| 2293 | 2293 | } |
| 2294 | 2294 | |
| 2295 | fn addString(self: *Object, allocator: Allocator, name: [:0]const u8) error{OutOfMemory}!u32 { | |
| 2295 | fn addString(self: *Object, allocator: Allocator, string: [:0]const u8) error{OutOfMemory}!MachO.String { | |
| 2296 | 2296 | 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); | |
| 2299 | 2299 | self.strtab.appendAssumeCapacity(0); |
| 2300 | return off; | |
| 2300 | return .{ .pos = off, .len = @intCast(string.len + 1) }; | |
| 2301 | 2301 | } |
| 2302 | 2302 | |
| 2303 | pub 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); | |
| 2303 | pub 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 | ||
| 2309 | fn 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); | |
| 2306 | 2312 | } |
| 2307 | 2313 | |
| 2308 | 2314 | pub fn hasUnwindRecords(self: Object) bool { |
| ... | ... | @@ -2323,7 +2329,7 @@ fn hasSymbolStabs(self: Object) bool { |
| 2323 | 2329 | |
| 2324 | 2330 | fn hasObjC(self: Object) bool { |
| 2325 | 2331 | for (self.symtab.items(.nlist)) |nlist| { |
| 2326 | const name = self.getString(nlist.n_strx); | |
| 2332 | const name = self.getNStrx(nlist.n_strx); | |
| 2327 | 2333 | if (mem.startsWith(u8, name, "_OBJC_CLASS_$_")) return true; |
| 2328 | 2334 | } |
| 2329 | 2335 | for (self.sections.items(.header)) |sect| { |
| ... | ... | @@ -2346,7 +2352,7 @@ pub fn asFile(self: *Object) File { |
| 2346 | 2352 | } |
| 2347 | 2353 | |
| 2348 | 2354 | const AddAtomArgs = struct { |
| 2349 | name: u32, | |
| 2355 | name: MachO.String, | |
| 2350 | 2356 | n_sect: u8, |
| 2351 | 2357 | off: u64, |
| 2352 | 2358 | size: u64, |
| ... | ... | @@ -2690,17 +2696,17 @@ const StabFile = struct { |
| 2690 | 2696 | |
| 2691 | 2697 | fn getCompDir(sf: StabFile, object: Object) [:0]const u8 { |
| 2692 | 2698 | const nlist = object.symtab.items(.nlist)[sf.comp_dir]; |
| 2693 | return object.getString(nlist.n_strx); | |
| 2699 | return object.getNStrx(nlist.n_strx); | |
| 2694 | 2700 | } |
| 2695 | 2701 | |
| 2696 | 2702 | fn getTuName(sf: StabFile, object: Object) [:0]const u8 { |
| 2697 | 2703 | const nlist = object.symtab.items(.nlist)[sf.comp_dir + 1]; |
| 2698 | return object.getString(nlist.n_strx); | |
| 2704 | return object.getNStrx(nlist.n_strx); | |
| 2699 | 2705 | } |
| 2700 | 2706 | |
| 2701 | 2707 | fn getOsoPath(sf: StabFile, object: Object) [:0]const u8 { |
| 2702 | 2708 | const nlist = object.symtab.items(.nlist)[sf.comp_dir + 2]; |
| 2703 | return object.getString(nlist.n_strx); | |
| 2709 | return object.getNStrx(nlist.n_strx); | |
| 2704 | 2710 | } |
| 2705 | 2711 | |
| 2706 | 2712 | fn getOsoModTime(sf: StabFile, object: Object) u64 { |
| ... | ... | @@ -2758,8 +2764,8 @@ const StabFile = struct { |
| 2758 | 2764 | }; |
| 2759 | 2765 | |
| 2760 | 2766 | const CompileUnit = struct { |
| 2761 | comp_dir: u32, | |
| 2762 | tu_name: u32, | |
| 2767 | comp_dir: MachO.String, | |
| 2768 | tu_name: MachO.String, | |
| 2763 | 2769 | |
| 2764 | 2770 | fn getCompDir(cu: CompileUnit, object: Object) [:0]const u8 { |
| 2765 | 2771 | return object.getString(cu.comp_dir); |
src/link/MachO/Symbol.zig+2-2| ... | ... | @@ -4,7 +4,7 @@ |
| 4 | 4 | value: u64 = 0, |
| 5 | 5 | |
| 6 | 6 | /// Offset into the linker's intern table. |
| 7 | name: u32 = 0, | |
| 7 | name: MachO.String = .{}, | |
| 8 | 8 | |
| 9 | 9 | /// File where this symbol is defined. |
| 10 | 10 | file: File.Index = 0, |
| ... | ... | @@ -57,7 +57,7 @@ pub fn weakRef(symbol: Symbol, macho_file: *MachO) bool { |
| 57 | 57 | |
| 58 | 58 | pub fn getName(symbol: Symbol, macho_file: *MachO) [:0]const u8 { |
| 59 | 59 | 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], | |
| 61 | 61 | inline else => |x| x.getString(symbol.name), |
| 62 | 62 | }; |
| 63 | 63 | } |
src/link/MachO/ZigObject.zig+27-22| ... | ... | @@ -141,7 +141,7 @@ pub fn deinit(self: *ZigObject, allocator: Allocator) void { |
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | fn newSymbol(self: *ZigObject, allocator: Allocator, name: u32, args: struct { | |
| 144 | fn newSymbol(self: *ZigObject, allocator: Allocator, name: MachO.String, args: struct { | |
| 145 | 145 | type: u8 = macho.N_UNDF | macho.N_EXT, |
| 146 | 146 | desc: u16 = 0, |
| 147 | 147 | }) !Symbol.Index { |
| ... | ... | @@ -158,7 +158,7 @@ fn newSymbol(self: *ZigObject, allocator: Allocator, name: u32, args: struct { |
| 158 | 158 | const nlist_idx: u32 = @intCast(self.symtab.addOneAssumeCapacity()); |
| 159 | 159 | self.symtab.set(nlist_idx, .{ |
| 160 | 160 | .nlist = .{ |
| 161 | .n_strx = name, | |
| 161 | .n_strx = name.pos, | |
| 162 | 162 | .n_type = args.type, |
| 163 | 163 | .n_sect = 0, |
| 164 | 164 | .n_desc = args.desc, |
| ... | ... | @@ -174,7 +174,7 @@ fn newSymbol(self: *ZigObject, allocator: Allocator, name: u32, args: struct { |
| 174 | 174 | return index; |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | fn newAtom(self: *ZigObject, allocator: Allocator, name: u32, macho_file: *MachO) !Atom.Index { | |
| 177 | fn newAtom(self: *ZigObject, allocator: Allocator, name: MachO.String, macho_file: *MachO) !Atom.Index { | |
| 178 | 178 | try self.atoms.ensureUnusedCapacity(allocator, 1); |
| 179 | 179 | try self.atoms_extra.ensureUnusedCapacity(allocator, @sizeOf(Atom.Extra)); |
| 180 | 180 | try self.atoms_indexes.ensureUnusedCapacity(allocator, 1); |
| ... | ... | @@ -192,7 +192,7 @@ fn newAtom(self: *ZigObject, allocator: Allocator, name: u32, macho_file: *MachO |
| 192 | 192 | return index; |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | fn newSymbolWithAtom(self: *ZigObject, allocator: Allocator, name: u32, macho_file: *MachO) !Symbol.Index { | |
| 195 | fn newSymbolWithAtom(self: *ZigObject, allocator: Allocator, name: MachO.String, macho_file: *MachO) !Symbol.Index { | |
| 196 | 196 | const atom_index = try self.newAtom(allocator, name, macho_file); |
| 197 | 197 | const sym_index = try self.newSymbol(allocator, name, .{ .type = macho.N_SECT }); |
| 198 | 198 | const sym = &self.symbols.items[sym_index]; |
| ... | ... | @@ -992,10 +992,10 @@ fn updateDeclCode( |
| 992 | 992 | |
| 993 | 993 | const sym_name = try std.fmt.allocPrintZ(gpa, "_{s}", .{decl.fqn.toSlice(ip)}); |
| 994 | 994 | defer gpa.free(sym_name); |
| 995 | sym.name = try self.strtab.insert(gpa, sym_name); | |
| 995 | sym.name = try self.addString(gpa, sym_name); | |
| 996 | 996 | atom.setAlive(true); |
| 997 | 997 | atom.name = sym.name; |
| 998 | nlist.n_strx = sym.name; | |
| 998 | nlist.n_strx = sym.name.pos; | |
| 999 | 999 | nlist.n_type = macho.N_SECT; |
| 1000 | 1000 | nlist.n_sect = sect_index + 1; |
| 1001 | 1001 | self.symtab.items(.size)[sym.nlist_idx] = code.len; |
| ... | ... | @@ -1090,9 +1090,9 @@ fn createTlvInitializer( |
| 1090 | 1090 | const gpa = macho_file.base.comp.gpa; |
| 1091 | 1091 | const sym_name = try std.fmt.allocPrint(gpa, "{s}$tlv$init", .{name}); |
| 1092 | 1092 | defer gpa.free(sym_name); |
| 1093 | const off = try self.strtab.insert(gpa, sym_name); | |
| 1093 | const string = try self.addString(gpa, sym_name); | |
| 1094 | 1094 | |
| 1095 | const sym_index = try self.newSymbolWithAtom(gpa, off, macho_file); | |
| 1095 | const sym_index = try self.newSymbolWithAtom(gpa, string, macho_file); | |
| 1096 | 1096 | const sym = &self.symbols.items[sym_index]; |
| 1097 | 1097 | const nlist = &self.symtab.items(.nlist)[sym.nlist_idx]; |
| 1098 | 1098 | const atom = sym.getAtom(macho_file).?; |
| ... | ... | @@ -1142,10 +1142,10 @@ fn createTlvDescriptor( |
| 1142 | 1142 | atom.out_n_sect = sect_index; |
| 1143 | 1143 | |
| 1144 | 1144 | sym.value = 0; |
| 1145 | sym.name = try self.strtab.insert(gpa, name); | |
| 1145 | sym.name = try self.addString(gpa, name); | |
| 1146 | 1146 | atom.setAlive(true); |
| 1147 | 1147 | atom.name = sym.name; |
| 1148 | nlist.n_strx = sym.name; | |
| 1148 | nlist.n_strx = sym.name.pos; | |
| 1149 | 1149 | nlist.n_sect = sect_index + 1; |
| 1150 | 1150 | nlist.n_type = macho.N_SECT; |
| 1151 | 1151 | nlist.n_value = 0; |
| ... | ... | @@ -1296,8 +1296,8 @@ fn lowerConst( |
| 1296 | 1296 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 1297 | 1297 | defer code_buffer.deinit(); |
| 1298 | 1298 | |
| 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); | |
| 1301 | 1301 | |
| 1302 | 1302 | const res = try codegen.generateSymbol(&macho_file.base, pt, src_loc, val, &code_buffer, .{ |
| 1303 | 1303 | .none = {}, |
| ... | ... | @@ -1447,13 +1447,13 @@ fn updateLazySymbol( |
| 1447 | 1447 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 1448 | 1448 | defer code_buffer.deinit(); |
| 1449 | 1449 | |
| 1450 | const name_str_index = blk: { | |
| 1450 | const name_str = blk: { | |
| 1451 | 1451 | const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{ |
| 1452 | 1452 | @tagName(lazy_sym.kind), |
| 1453 | 1453 | lazy_sym.ty.fmt(pt), |
| 1454 | 1454 | }); |
| 1455 | 1455 | defer gpa.free(name); |
| 1456 | break :blk try self.strtab.insert(gpa, name); | |
| 1456 | break :blk try self.addString(gpa, name); | |
| 1457 | 1457 | }; |
| 1458 | 1458 | |
| 1459 | 1459 | const src = lazy_sym.ty.srcLocOrNull(mod) orelse Module.LazySrcLoc.unneeded; |
| ... | ... | @@ -1480,18 +1480,18 @@ fn updateLazySymbol( |
| 1480 | 1480 | .const_data => macho_file.zig_const_sect_index.?, |
| 1481 | 1481 | }; |
| 1482 | 1482 | const sym = &self.symbols.items[symbol_index]; |
| 1483 | sym.name = name_str_index; | |
| 1483 | sym.name = name_str; | |
| 1484 | 1484 | sym.out_n_sect = output_section_index; |
| 1485 | 1485 | |
| 1486 | 1486 | const nlist = &self.symtab.items(.nlist)[sym.nlist_idx]; |
| 1487 | nlist.n_strx = name_str_index; | |
| 1487 | nlist.n_strx = name_str.pos; | |
| 1488 | 1488 | nlist.n_type = macho.N_SECT; |
| 1489 | 1489 | nlist.n_sect = output_section_index + 1; |
| 1490 | 1490 | self.symtab.items(.size)[sym.nlist_idx] = code.len; |
| 1491 | 1491 | |
| 1492 | 1492 | const atom = sym.getAtom(macho_file).?; |
| 1493 | 1493 | atom.setAlive(true); |
| 1494 | atom.name = name_str_index; | |
| 1494 | atom.name = name_str; | |
| 1495 | 1495 | atom.alignment = required_alignment; |
| 1496 | 1496 | atom.size = code.len; |
| 1497 | 1497 | atom.out_n_sect = output_section_index; |
| ... | ... | @@ -1553,10 +1553,10 @@ pub fn getGlobalSymbol(self: *ZigObject, macho_file: *MachO, name: []const u8, l |
| 1553 | 1553 | const gpa = macho_file.base.comp.gpa; |
| 1554 | 1554 | const sym_name = try std.fmt.allocPrint(gpa, "_{s}", .{name}); |
| 1555 | 1555 | 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); | |
| 1558 | 1558 | if (!lookup_gop.found_existing) { |
| 1559 | const sym_index = try self.newSymbol(gpa, off, .{}); | |
| 1559 | const sym_index = try self.newSymbol(gpa, name_str, .{}); | |
| 1560 | 1560 | const sym = &self.symbols.items[sym_index]; |
| 1561 | 1561 | lookup_gop.value_ptr.* = sym.nlist_idx; |
| 1562 | 1562 | } |
| ... | ... | @@ -1571,7 +1571,7 @@ pub fn getOrCreateMetadataForDecl( |
| 1571 | 1571 | const gpa = macho_file.base.comp.gpa; |
| 1572 | 1572 | const gop = try self.decls.getOrPut(gpa, decl_index); |
| 1573 | 1573 | 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); | |
| 1575 | 1575 | const sym = &self.symbols.items[sym_index]; |
| 1576 | 1576 | if (isThreadlocal(macho_file, decl_index)) { |
| 1577 | 1577 | sym.flags.tlv = true; |
| ... | ... | @@ -1609,7 +1609,7 @@ pub fn getOrCreateMetadataForLazySymbol( |
| 1609 | 1609 | }; |
| 1610 | 1610 | switch (metadata.state.*) { |
| 1611 | 1611 | .unused => { |
| 1612 | const symbol_index = try self.newSymbolWithAtom(gpa, 0, macho_file); | |
| 1612 | const symbol_index = try self.newSymbolWithAtom(gpa, .{}, macho_file); | |
| 1613 | 1613 | const sym = &self.symbols.items[symbol_index]; |
| 1614 | 1614 | sym.setSectionFlags(.{ .needs_zig_got = true }); |
| 1615 | 1615 | metadata.symbol_index.* = symbol_index; |
| ... | ... | @@ -1762,6 +1762,11 @@ pub fn setSymbolExtra(self: *ZigObject, index: u32, extra: Symbol.Extra) void { |
| 1762 | 1762 | } |
| 1763 | 1763 | } |
| 1764 | 1764 | |
| 1765 | fn 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 | ||
| 1765 | 1770 | pub fn asFile(self: *ZigObject) File { |
| 1766 | 1771 | return .{ .zig_object = self }; |
| 1767 | 1772 | } |