diff options
Diffstat (limited to 'src/link/MachO/Object.zig')
| -rw-r--r-- | src/link/MachO/Object.zig | 54 |
1 files changed, 30 insertions, 24 deletions
diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig index 47bee7befe..38cb82e858 100644 --- a/src/link/MachO/Object.zig +++ b/src/link/MachO/Object.zig @@ -178,7 +178,7 @@ pub fn parse(self: *Object, macho_file: *MachO) !void { fn rank(ctx: *const Object, nl: macho.nlist_64) u8 { if (!nl.ext()) { - const name = ctx.getString(nl.n_strx); + const name = ctx.getNStrx(nl.n_strx); if (name.len == 0) return 5; if (name[0] == 'l' or name[0] == 'L') return 4; return 3; @@ -341,7 +341,7 @@ fn initSubsections(self: *Object, allocator: Allocator, nlists: anytype) !void { else sect.@"align"; const atom_index = try self.addAtom(allocator, .{ - .name = nlist.nlist.n_strx, + .name = .{ .pos = nlist.nlist.n_strx, .len = @intCast(self.getNStrx(nlist.nlist.n_strx).len + 1) }, .n_sect = @intCast(n_sect), .off = nlist.nlist.n_value - sect.addr, .size = size, @@ -465,7 +465,7 @@ fn initCstringLiterals(self: *Object, allocator: Allocator, file: File.Handle, m const nlist_index: u32 = @intCast(try self.symtab.addOne(allocator)); self.symtab.set(nlist_index, .{ .nlist = .{ - .n_strx = name_str, + .n_strx = name_str.pos, .n_type = macho.N_SECT, .n_sect = @intCast(atom.n_sect + 1), .n_desc = 0, @@ -532,7 +532,7 @@ fn initFixedSizeLiterals(self: *Object, allocator: Allocator, macho_file: *MachO const nlist_index: u32 = @intCast(try self.symtab.addOne(allocator)); self.symtab.set(nlist_index, .{ .nlist = .{ - .n_strx = name_str, + .n_strx = name_str.pos, .n_type = macho.N_SECT, .n_sect = @intCast(atom.n_sect + 1), .n_desc = 0, @@ -590,7 +590,7 @@ fn initPointerLiterals(self: *Object, allocator: Allocator, macho_file: *MachO) const nlist_index: u32 = @intCast(try self.symtab.addOne(allocator)); self.symtab.set(nlist_index, .{ .nlist = .{ - .n_strx = name_str, + .n_strx = name_str.pos, .n_type = macho.N_SECT, .n_sect = @intCast(atom.n_sect + 1), .n_desc = 0, @@ -796,7 +796,7 @@ fn linkNlistToAtom(self: *Object, macho_file: *MachO) !void { atom.* = atom_index; } else { try macho_file.reportParseError2(self.index, "symbol {s} not attached to any (sub)section", .{ - self.getString(nlist.n_strx), + self.getNStrx(nlist.n_strx), }); return error.MalformedObject; } @@ -821,7 +821,7 @@ fn initSymbols(self: *Object, allocator: Allocator, macho_file: *MachO) !void { const index = self.addSymbolAssumeCapacity(); const symbol = &self.symbols.items[index]; symbol.value = nlist.n_value; - symbol.name = nlist.n_strx; + symbol.name = .{ .pos = nlist.n_strx, .len = @intCast(self.getNStrx(nlist.n_strx).len + 1) }; symbol.nlist_idx = @intCast(i); symbol.extra = self.addSymbolExtraAssumeCapacity(.{}); @@ -894,7 +894,7 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f defer addr_lookup.deinit(); for (syms) |sym| { if (sym.sect() and (sym.ext() or sym.pext())) { - try addr_lookup.putNoClobber(self.getString(sym.n_strx), sym.n_value); + try addr_lookup.putNoClobber(self.getNStrx(sym.n_strx), sym.n_value); } } @@ -926,7 +926,7 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f }, macho.N_GSYM => { stab.is_func = false; - stab.index = sym_lookup.find(addr_lookup.get(self.getString(nlist.n_strx)).?); + stab.index = sym_lookup.find(addr_lookup.get(self.getNStrx(nlist.n_strx)).?); }, macho.N_STSYM => { stab.is_func = false; @@ -1708,7 +1708,7 @@ pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, macho_file: *M const gpa = macho_file.base.comp.gpa; for (self.symtab.items(.nlist)) |nlist| { if (!nlist.ext() or (nlist.undf() and !nlist.tentative())) continue; - const off = try ar_symtab.strtab.insert(gpa, self.getString(nlist.n_strx)); + const off = try ar_symtab.strtab.insert(gpa, self.getNStrx(nlist.n_strx)); try ar_symtab.entries.append(gpa, .{ .off = off, .file = self.index }); } } @@ -2292,17 +2292,23 @@ pub fn getAtomRelocs(self: *const Object, atom: Atom, macho_file: *MachO) []cons return relocs.items[extra.rel_index..][0..extra.rel_count]; } -fn addString(self: *Object, allocator: Allocator, name: [:0]const u8) error{OutOfMemory}!u32 { +fn addString(self: *Object, allocator: Allocator, string: [:0]const u8) error{OutOfMemory}!MachO.String { const off: u32 = @intCast(self.strtab.items.len); - try self.strtab.ensureUnusedCapacity(allocator, name.len + 1); - self.strtab.appendSliceAssumeCapacity(name); + try self.strtab.ensureUnusedCapacity(allocator, string.len + 1); + self.strtab.appendSliceAssumeCapacity(string); self.strtab.appendAssumeCapacity(0); - return off; + return .{ .pos = off, .len = @intCast(string.len + 1) }; } -pub fn getString(self: Object, off: u32) [:0]const u8 { - assert(off < self.strtab.items.len); - return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0); +pub fn getString(self: Object, string: MachO.String) [:0]const u8 { + assert(string.pos < self.strtab.items.len and string.pos + string.len <= self.strtab.items.len); + if (string.len == 0) return ""; + return self.strtab.items[string.pos..][0 .. string.len - 1 :0]; +} + +fn getNStrx(self: Object, n_strx: u32) [:0]const u8 { + assert(n_strx < self.strtab.items.len); + return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + n_strx)), 0); } pub fn hasUnwindRecords(self: Object) bool { @@ -2323,7 +2329,7 @@ fn hasSymbolStabs(self: Object) bool { fn hasObjC(self: Object) bool { for (self.symtab.items(.nlist)) |nlist| { - const name = self.getString(nlist.n_strx); + const name = self.getNStrx(nlist.n_strx); if (mem.startsWith(u8, name, "_OBJC_CLASS_$_")) return true; } for (self.sections.items(.header)) |sect| { @@ -2346,7 +2352,7 @@ pub fn asFile(self: *Object) File { } const AddAtomArgs = struct { - name: u32, + name: MachO.String, n_sect: u8, off: u64, size: u64, @@ -2690,17 +2696,17 @@ const StabFile = struct { fn getCompDir(sf: StabFile, object: Object) [:0]const u8 { const nlist = object.symtab.items(.nlist)[sf.comp_dir]; - return object.getString(nlist.n_strx); + return object.getNStrx(nlist.n_strx); } fn getTuName(sf: StabFile, object: Object) [:0]const u8 { const nlist = object.symtab.items(.nlist)[sf.comp_dir + 1]; - return object.getString(nlist.n_strx); + return object.getNStrx(nlist.n_strx); } fn getOsoPath(sf: StabFile, object: Object) [:0]const u8 { const nlist = object.symtab.items(.nlist)[sf.comp_dir + 2]; - return object.getString(nlist.n_strx); + return object.getNStrx(nlist.n_strx); } fn getOsoModTime(sf: StabFile, object: Object) u64 { @@ -2758,8 +2764,8 @@ const StabFile = struct { }; const CompileUnit = struct { - comp_dir: u32, - tu_name: u32, + comp_dir: MachO.String, + tu_name: MachO.String, fn getCompDir(cu: CompileUnit, object: Object) [:0]const u8 { return object.getString(cu.comp_dir); |
