diff --git a/src/MachO.zig b/src/MachO.zig index 86f1078f..6c14ff68 100644 --- a/src/MachO.zig +++ b/src/MachO.zig @@ -1139,26 +1139,14 @@ fn initOutputSections(self: *MachO) !void { for (object.atoms.items) |atom_index| { const atom = self.getAtom(atom_index) orelse continue; if (!atom.flags.alive) continue; - const isec = atom.getInputSection(self); - atom.out_n_sect = try Atom.initOutputSection(.{ - .segname = isec.segName(), - .sectname = isec.sectName(), - .flags = isec.flags, - .is_code = isec.isCode(), - }, self); + atom.out_n_sect = try Atom.initOutputSection(atom.getInputSection(self), self); } } if (self.getInternalObject()) |object| { for (object.atoms.items) |atom_index| { const atom = self.getAtom(atom_index) orelse continue; if (!atom.flags.alive) continue; - const isec = atom.getInputSection(self); - atom.out_n_sect = try Atom.initOutputSection(.{ - .segname = isec.segName(), - .sectname = isec.sectName(), - .flags = isec.flags, - .is_code = isec.isCode(), - }, self); + atom.out_n_sect = try Atom.initOutputSection(atom.getInputSection(self), self); } } if (self.data_sect_index == null) { diff --git a/src/MachO/Atom.zig b/src/MachO/Atom.zig index 1f938ec2..6246d160 100644 --- a/src/MachO/Atom.zig +++ b/src/MachO/Atom.zig @@ -146,45 +146,40 @@ pub inline fn setExtra(atom: Atom, extra: Extra, macho_file: *MachO) void { macho_file.setAtomExtra(atom.extra, extra); } -pub fn initOutputSection(args: struct { - segname: []const u8, - sectname: []const u8, - flags: u32, - is_code: bool = false, -}, macho_file: *MachO) !u8 { +pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 { if (macho_file.options.relocatable) { - const osec = macho_file.getSectionByName(args.segname, args.sectname) orelse try macho_file.addSection( - args.segname, - args.sectname, - .{ .flags = args.flags }, + const osec = macho_file.getSectionByName(sect.segName(), sect.sectName()) orelse + try macho_file.addSection( + sect.segName(), + sect.sectName(), + .{ .flags = sect.flags }, ); return osec; } - const @"type": u8 = @truncate(args.flags & 0xff); const segname, const sectname, const flags = blk: { - if (args.is_code) break :blk .{ + if (sect.isCode()) break :blk .{ "__TEXT", "__text", macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, }; - switch (@"type") { + switch (sect.type()) { macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS, => break :blk .{ "__TEXT", "__const", macho.S_REGULAR }, macho.S_CSTRING_LITERALS => { - if (mem.startsWith(u8, args.sectname, "__objc")) break :blk .{ - args.segname, args.sectname, macho.S_REGULAR, + if (mem.startsWith(u8, sect.sectName(), "__objc")) break :blk .{ + sect.segName(), sect.sectName(), macho.S_REGULAR, }; break :blk .{ "__TEXT", "__cstring", macho.S_CSTRING_LITERALS }; }, macho.S_MOD_INIT_FUNC_POINTERS, macho.S_MOD_TERM_FUNC_POINTERS, - => break :blk .{ "__DATA_CONST", args.sectname, args.flags }, + => break :blk .{ "__DATA_CONST", sect.sectName(), sect.flags }, macho.S_LITERAL_POINTERS, macho.S_ZEROFILL, @@ -193,28 +188,30 @@ pub fn initOutputSection(args: struct { macho.S_THREAD_LOCAL_VARIABLE_POINTERS, macho.S_THREAD_LOCAL_REGULAR, macho.S_THREAD_LOCAL_ZEROFILL, - => break :blk .{ args.segname, args.sectname, args.flags }, + => break :blk .{ sect.segName(), sect.sectName(), sect.flags }, macho.S_COALESCED => break :blk .{ - args.segname, - args.sectname, + sect.segName(), + sect.sectName(), macho.S_REGULAR, }, macho.S_REGULAR => { - if (mem.eql(u8, args.segname, "__DATA")) { - if (mem.eql(u8, args.sectname, "__cfstring") or - mem.eql(u8, args.sectname, "__objc_classlist") or - mem.eql(u8, args.sectname, "__objc_imageinfo")) break :blk .{ + const segname = sect.segName(); + const sectname = sect.sectName(); + if (mem.eql(u8, segname, "__DATA")) { + if (mem.eql(u8, sectname, "__cfstring") or + mem.eql(u8, sectname, "__objc_classlist") or + mem.eql(u8, sectname, "__objc_imageinfo")) break :blk .{ "__DATA_CONST", - args.sectname, + sectname, macho.S_REGULAR, }; } - break :blk .{ args.segname, args.sectname, args.flags }; + break :blk .{ segname, sectname, sect.flags }; }, - else => break :blk .{ args.segname, args.sectname, args.flags }, + else => break :blk .{ sect.segName(), sect.sectName(), sect.flags }, } }; const osec = macho_file.getSectionByName(segname, sectname) orelse try macho_file.addSection( @@ -865,9 +862,8 @@ fn format2( const atom = ctx.atom; const macho_file = ctx.macho_file; try writer.print("atom({d}) : {s} : @{x} : sect({d}) : align({x}) : size({x})", .{ - atom.atom_index, atom.getName(macho_file), - atom.getAddress(macho_file), atom.out_n_sect, - atom.alignment, atom.size, + atom.atom_index, atom.getName(macho_file), atom.getAddress(macho_file), + atom.out_n_sect, atom.alignment, atom.size, }); if (atom.flags.thunk) try writer.print(" : thunk({d})", .{atom.getExtra(macho_file).?.thunk}); if (!atom.flags.alive) try writer.writeAll(" : [*]"); diff --git a/src/MachO/Object.zig b/src/MachO/Object.zig index 3e549bb5..214e0279 100644 --- a/src/MachO/Object.zig +++ b/src/MachO/Object.zig @@ -11,7 +11,6 @@ strtab: std.ArrayListUnmanaged(u8) = .{}, symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, atoms: std.ArrayListUnmanaged(Atom.Index) = .{}, -literal_strings: std.ArrayListUnmanaged(u8) = .{}, platform: ?MachO.Options.Platform = null, dwarf_info: ?DwarfInfo = null, @@ -60,7 +59,6 @@ pub fn deinit(self: *Object, allocator: Allocator) void { } self.stab_files.deinit(allocator); self.data_in_code.deinit(allocator); - self.literal_strings.deinit(allocator); } pub fn parse(self: *Object, macho_file: *MachO) !void { @@ -1768,18 +1766,6 @@ pub fn getString(self: Object, off: u32) [:0]const u8 { return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0); } -fn addLiteralString(self: *Object, allocator: Allocator, str: []const u8) error{OutOfMemory}!u32 { - const off: u32 = @intCast(self.literal_strings.items.len); - try self.literal_strings.ensureUnusedCapacity(allocator, str.len); - self.literal_strings.appendSliceAssumeCapacity(str); - return off; -} - -pub fn getLiteralString(self: Object, off: u32, len: u32) []const u8 { - assert(off < self.literal_strings.items.len and off + len <= self.literal_strings.items.len); - return self.literal_strings.items[off..][0..len]; -} - /// TODO handle multiple CUs pub fn hasDebugInfo(self: Object) bool { if (self.dwarf_info) |dw| { diff --git a/src/MachO/relocatable.zig b/src/MachO/relocatable.zig index 4af9250f..1709a05e 100644 --- a/src/MachO/relocatable.zig +++ b/src/MachO/relocatable.zig @@ -104,13 +104,7 @@ fn initOutputSections(macho_file: *MachO) !void { for (object.atoms.items) |atom_index| { const atom = macho_file.getAtom(atom_index) orelse continue; if (!atom.flags.alive) continue; - const isec = atom.getInputSection(macho_file); - atom.out_n_sect = try Atom.initOutputSection(.{ - .segname = isec.segName(), - .sectname = isec.sectName(), - .flags = isec.flags, - .is_code = isec.isCode(), - }, macho_file); + atom.out_n_sect = try Atom.initOutputSection(atom.getInputSection(macho_file), macho_file); } }