authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-10-16 12:43:31+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-02 22:05:21-05:00
logc824b350511780581c0e5c1da85d0d9d769701ea
treed60597bbf6fa57e83d7b5c140e7b569e67edd057
parent8e815000515182f06fa436668664e4329c407a3e

macho: move things around in MachO/Object.zig and refactor


1 files changed, 16 insertions(+), 42 deletions(-)

src/link/MachO/Object.zig+16-42
......@@ -443,11 +443,8 @@ fn initCstringLiterals(self: *Object, allocator: Allocator, file: File.Handle, m
443443 for (slice.items(.header), 0..) |sect, n_sect| {
444444 if (!isCstringLiteral(sect)) continue;
445445
446 const sect_size = math.cast(usize, sect.size) orelse return error.Overflow;
447 const data = try allocator.alloc(u8, sect_size);
446 const data = try self.readSectionData(allocator, file, @intCast(n_sect));
448447 defer allocator.free(data);
449 const amt = try file.preadAll(data, sect.offset + self.offset);
450 if (amt != data.len) return error.InputOutput;
451448
452449 var count: u32 = 0;
453450 var start: u32 = 0;
......@@ -646,13 +643,10 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO
646643 }
647644
648645 const slice = self.sections.slice();
649 for (slice.items(.header), slice.items(.subsections)) |header, subs| {
646 for (slice.items(.header), slice.items(.subsections), 0..) |header, subs, n_sect| {
650647 if (isCstringLiteral(header) or isFixedSizeLiteral(header)) {
651 const sect_size = math.cast(usize, header.size) orelse return error.Overflow;
652 const data = try gpa.alloc(u8, sect_size);
648 const data = try self.readSectionData(gpa, file, @intCast(n_sect));
653649 defer gpa.free(data);
654 const amt = try file.preadAll(data, header.offset + self.offset);
655 if (amt != data.len) return error.InputOutput;
656650
657651 for (subs.items) |sub| {
658652 const atom = self.getAtom(sub.atom).?;
......@@ -686,12 +680,7 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO
686680 buffer.resize(target_size) catch unreachable;
687681 const gop = try sections_data.getOrPut(target.n_sect);
688682 if (!gop.found_existing) {
689 const target_sect = slice.items(.header)[target.n_sect];
690 const target_sect_size = math.cast(usize, target_sect.size) orelse return error.Overflow;
691 const data = try gpa.alloc(u8, target_sect_size);
692 const amt = try file.preadAll(data, target_sect.offset + self.offset);
693 if (amt != data.len) return error.InputOutput;
694 gop.value_ptr.* = data;
683 gop.value_ptr.* = try self.readSectionData(gpa, file, @intCast(target.n_sect));
695684 }
696685 const data = gop.value_ptr.*;
697686 const target_off = math.cast(usize, target.off) orelse return error.Overflow;
......@@ -1000,7 +989,7 @@ fn initRelocs(self: *Object, file: File.Handle, cpu_arch: std.Target.Cpu.Arch, m
1000989 defer tracy.end();
1001990 const slice = self.sections.slice();
1002991
1003 for (slice.items(.header), slice.items(.relocs)) |sect, *out| {
992 for (slice.items(.header), slice.items(.relocs), 0..) |sect, *out, n_sect| {
1004993 if (sect.nreloc == 0) continue;
1005994 // We skip relocs for __DWARF since even in -r mode, the linker is expected to emit
1006995 // debug symbol stabs in the relocatable. This made me curious why that is. For now,
......@@ -1009,8 +998,8 @@ fn initRelocs(self: *Object, file: File.Handle, cpu_arch: std.Target.Cpu.Arch, m
1009998 !mem.eql(u8, sect.sectName(), "__compact_unwind")) continue;
1010999
10111000 switch (cpu_arch) {
1012 .x86_64 => try x86_64.parseRelocs(self, sect, out, file, macho_file),
1013 .aarch64 => try aarch64.parseRelocs(self, sect, out, file, macho_file),
1001 .x86_64 => try x86_64.parseRelocs(self, @intCast(n_sect), sect, out, file, macho_file),
1002 .aarch64 => try aarch64.parseRelocs(self, @intCast(n_sect), sect, out, file, macho_file),
10141003 else => unreachable,
10151004 }
10161005
......@@ -1146,11 +1135,8 @@ fn initUnwindRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fil
11461135 };
11471136
11481137 const header = self.sections.items(.header)[sect_id];
1149 const size = math.cast(usize, header.size) orelse return error.Overflow;
1150 const data = try allocator.alloc(u8, size);
1138 const data = try self.readSectionData(allocator, file, sect_id);
11511139 defer allocator.free(data);
1152 const amt = try file.preadAll(data, header.offset + self.offset);
1153 if (amt != data.len) return error.InputOutput;
11541140
11551141 const nrecs = @divExact(data.len, @sizeOf(macho.compact_unwind_entry));
11561142 const recs = @as([*]align(1) const macho.compact_unwind_entry, @ptrCast(data.ptr))[0..nrecs];
......@@ -2810,6 +2796,7 @@ const CompactUnwindCtx = struct {
28102796const x86_64 = struct {
28112797 fn parseRelocs(
28122798 self: *Object,
2799 n_sect: u8,
28132800 sect: macho.section_64,
28142801 out: *std.ArrayListUnmanaged(Relocation),
28152802 handle: File.Handle,
......@@ -2819,19 +2806,12 @@ const x86_64 = struct {
28192806
28202807 const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info));
28212808 defer gpa.free(relocs_buffer);
2822 {
2823 const amt = try handle.preadAll(relocs_buffer, sect.reloff + self.offset);
2824 if (amt != relocs_buffer.len) return error.InputOutput;
2825 }
2809 const amt = try handle.preadAll(relocs_buffer, sect.reloff + self.offset);
2810 if (amt != relocs_buffer.len) return error.InputOutput;
28262811 const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc];
28272812
2828 const sect_size = math.cast(usize, sect.size) orelse return error.Overflow;
2829 const code = try gpa.alloc(u8, sect_size);
2813 const code = try self.readSectionData(gpa, handle, n_sect);
28302814 defer gpa.free(code);
2831 {
2832 const amt = try handle.preadAll(code, sect.offset + self.offset);
2833 if (amt != code.len) return error.InputOutput;
2834 }
28352815
28362816 try out.ensureTotalCapacityPrecise(gpa, relocs.len);
28372817
......@@ -2983,6 +2963,7 @@ const x86_64 = struct {
29832963const aarch64 = struct {
29842964 fn parseRelocs(
29852965 self: *Object,
2966 n_sect: u8,
29862967 sect: macho.section_64,
29872968 out: *std.ArrayListUnmanaged(Relocation),
29882969 handle: File.Handle,
......@@ -2992,19 +2973,12 @@ const aarch64 = struct {
29922973
29932974 const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info));
29942975 defer gpa.free(relocs_buffer);
2995 {
2996 const amt = try handle.preadAll(relocs_buffer, sect.reloff + self.offset);
2997 if (amt != relocs_buffer.len) return error.InputOutput;
2998 }
2976 const amt = try handle.preadAll(relocs_buffer, sect.reloff + self.offset);
2977 if (amt != relocs_buffer.len) return error.InputOutput;
29992978 const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc];
30002979
3001 const sect_size = math.cast(usize, sect.size) orelse return error.Overflow;
3002 const code = try gpa.alloc(u8, sect_size);
2980 const code = try self.readSectionData(gpa, handle, n_sect);
30032981 defer gpa.free(code);
3004 {
3005 const amt = try handle.preadAll(code, sect.offset + self.offset);
3006 if (amt != code.len) return error.InputOutput;
3007 }
30082982
30092983 try out.ensureTotalCapacityPrecise(gpa, relocs.len);
30102984