| ... | @@ -443,11 +443,8 @@ fn initCstringLiterals(self: *Object, allocator: Allocator, file: File.Handle, m | ... | @@ -443,11 +443,8 @@ fn initCstringLiterals(self: *Object, allocator: Allocator, file: File.Handle, m |
| 443 | for (slice.items(.header), 0..) |sect, n_sect| { | 443 | for (slice.items(.header), 0..) |sect, n_sect| { |
| 444 | if (!isCstringLiteral(sect)) continue; | 444 | if (!isCstringLiteral(sect)) continue; |
| 445 | | 445 | |
| 446 | const sect_size = math.cast(usize, sect.size) orelse return error.Overflow; | 446 | const data = try self.readSectionData(allocator, file, @intCast(n_sect)); |
| 447 | const data = try allocator.alloc(u8, sect_size); | | |
| 448 | defer allocator.free(data); | 447 | defer allocator.free(data); |
| 449 | const amt = try file.preadAll(data, sect.offset + self.offset); | | |
| 450 | if (amt != data.len) return error.InputOutput; | | |
| 451 | | 448 | |
| 452 | var count: u32 = 0; | 449 | var count: u32 = 0; |
| 453 | var start: u32 = 0; | 450 | var start: u32 = 0; |
| ... | @@ -646,13 +643,10 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO | ... | @@ -646,13 +643,10 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO |
| 646 | } | 643 | } |
| 647 | | 644 | |
| 648 | const slice = self.sections.slice(); | 645 | 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| { |
| 650 | if (isCstringLiteral(header) or isFixedSizeLiteral(header)) { | 647 | if (isCstringLiteral(header) or isFixedSizeLiteral(header)) { |
| 651 | const sect_size = math.cast(usize, header.size) orelse return error.Overflow; | 648 | const data = try self.readSectionData(gpa, file, @intCast(n_sect)); |
| 652 | const data = try gpa.alloc(u8, sect_size); | | |
| 653 | defer gpa.free(data); | 649 | defer gpa.free(data); |
| 654 | const amt = try file.preadAll(data, header.offset + self.offset); | | |
| 655 | if (amt != data.len) return error.InputOutput; | | |
| 656 | | 650 | |
| 657 | for (subs.items) |sub| { | 651 | for (subs.items) |sub| { |
| 658 | const atom = self.getAtom(sub.atom).?; | 652 | const atom = self.getAtom(sub.atom).?; |
| ... | @@ -686,12 +680,7 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO | ... | @@ -686,12 +680,7 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO |
| 686 | buffer.resize(target_size) catch unreachable; | 680 | buffer.resize(target_size) catch unreachable; |
| 687 | const gop = try sections_data.getOrPut(target.n_sect); | 681 | const gop = try sections_data.getOrPut(target.n_sect); |
| 688 | if (!gop.found_existing) { | 682 | if (!gop.found_existing) { |
| 689 | const target_sect = slice.items(.header)[target.n_sect]; | 683 | gop.value_ptr.* = try self.readSectionData(gpa, file, @intCast(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; | | |
| 695 | } | 684 | } |
| 696 | const data = gop.value_ptr.*; | 685 | const data = gop.value_ptr.*; |
| 697 | const target_off = math.cast(usize, target.off) orelse return error.Overflow; | 686 | 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 | ... | @@ -1000,7 +989,7 @@ fn initRelocs(self: *Object, file: File.Handle, cpu_arch: std.Target.Cpu.Arch, m |
| 1000 | defer tracy.end(); | 989 | defer tracy.end(); |
| 1001 | const slice = self.sections.slice(); | 990 | const slice = self.sections.slice(); |
| 1002 | | 991 | |
| 1003 | for (slice.items(.header), slice.items(.relocs)) |sect, *out| { | 992 | for (slice.items(.header), slice.items(.relocs), 0..) |sect, *out, n_sect| { |
| 1004 | if (sect.nreloc == 0) continue; | 993 | if (sect.nreloc == 0) continue; |
| 1005 | // We skip relocs for __DWARF since even in -r mode, the linker is expected to emit | 994 | // We skip relocs for __DWARF since even in -r mode, the linker is expected to emit |
| 1006 | // debug symbol stabs in the relocatable. This made me curious why that is. For now, | 995 | // 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 | ... | @@ -1009,8 +998,8 @@ fn initRelocs(self: *Object, file: File.Handle, cpu_arch: std.Target.Cpu.Arch, m |
| 1009 | !mem.eql(u8, sect.sectName(), "__compact_unwind")) continue; | 998 | !mem.eql(u8, sect.sectName(), "__compact_unwind")) continue; |
| 1010 | | 999 | |
| 1011 | switch (cpu_arch) { | 1000 | switch (cpu_arch) { |
| 1012 | .x86_64 => try x86_64.parseRelocs(self, sect, out, file, macho_file), | 1001 | .x86_64 => try x86_64.parseRelocs(self, @intCast(n_sect), sect, out, file, macho_file), |
| 1013 | .aarch64 => try aarch64.parseRelocs(self, sect, out, file, macho_file), | 1002 | .aarch64 => try aarch64.parseRelocs(self, @intCast(n_sect), sect, out, file, macho_file), |
| 1014 | else => unreachable, | 1003 | else => unreachable, |
| 1015 | } | 1004 | } |
| 1016 | | 1005 | |
| ... | @@ -1146,11 +1135,8 @@ fn initUnwindRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fil | ... | @@ -1146,11 +1135,8 @@ fn initUnwindRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fil |
| 1146 | }; | 1135 | }; |
| 1147 | | 1136 | |
| 1148 | const header = self.sections.items(.header)[sect_id]; | 1137 | const header = self.sections.items(.header)[sect_id]; |
| 1149 | const size = math.cast(usize, header.size) orelse return error.Overflow; | 1138 | const data = try self.readSectionData(allocator, file, sect_id); |
| 1150 | const data = try allocator.alloc(u8, size); | | |
| 1151 | defer allocator.free(data); | 1139 | defer allocator.free(data); |
| 1152 | const amt = try file.preadAll(data, header.offset + self.offset); | | |
| 1153 | if (amt != data.len) return error.InputOutput; | | |
| 1154 | | 1140 | |
| 1155 | const nrecs = @divExact(data.len, @sizeOf(macho.compact_unwind_entry)); | 1141 | const nrecs = @divExact(data.len, @sizeOf(macho.compact_unwind_entry)); |
| 1156 | const recs = @as([*]align(1) const macho.compact_unwind_entry, @ptrCast(data.ptr))[0..nrecs]; | 1142 | const recs = @as([*]align(1) const macho.compact_unwind_entry, @ptrCast(data.ptr))[0..nrecs]; |
| ... | @@ -2810,6 +2796,7 @@ const CompactUnwindCtx = struct { | ... | @@ -2810,6 +2796,7 @@ const CompactUnwindCtx = struct { |
| 2810 | const x86_64 = struct { | 2796 | const x86_64 = struct { |
| 2811 | fn parseRelocs( | 2797 | fn parseRelocs( |
| 2812 | self: *Object, | 2798 | self: *Object, |
| | 2799 | n_sect: u8, |
| 2813 | sect: macho.section_64, | 2800 | sect: macho.section_64, |
| 2814 | out: *std.ArrayListUnmanaged(Relocation), | 2801 | out: *std.ArrayListUnmanaged(Relocation), |
| 2815 | handle: File.Handle, | 2802 | handle: File.Handle, |
| ... | @@ -2819,19 +2806,12 @@ const x86_64 = struct { | ... | @@ -2819,19 +2806,12 @@ const x86_64 = struct { |
| 2819 | | 2806 | |
| 2820 | const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info)); | 2807 | const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info)); |
| 2821 | defer gpa.free(relocs_buffer); | 2808 | defer gpa.free(relocs_buffer); |
| 2822 | { | 2809 | const amt = try handle.preadAll(relocs_buffer, sect.reloff + self.offset); |
| 2823 | const amt = try handle.preadAll(relocs_buffer, sect.reloff + self.offset); | 2810 | if (amt != relocs_buffer.len) return error.InputOutput; |
| 2824 | if (amt != relocs_buffer.len) return error.InputOutput; | | |
| 2825 | } | | |
| 2826 | const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc]; | 2811 | const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc]; |
| 2827 | | 2812 | |
| 2828 | const sect_size = math.cast(usize, sect.size) orelse return error.Overflow; | 2813 | const code = try self.readSectionData(gpa, handle, n_sect); |
| 2829 | const code = try gpa.alloc(u8, sect_size); | | |
| 2830 | defer gpa.free(code); | 2814 | 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 | } | | |
| 2835 | | 2815 | |
| 2836 | try out.ensureTotalCapacityPrecise(gpa, relocs.len); | 2816 | try out.ensureTotalCapacityPrecise(gpa, relocs.len); |
| 2837 | | 2817 | |
| ... | @@ -2983,6 +2963,7 @@ const x86_64 = struct { | ... | @@ -2983,6 +2963,7 @@ const x86_64 = struct { |
| 2983 | const aarch64 = struct { | 2963 | const aarch64 = struct { |
| 2984 | fn parseRelocs( | 2964 | fn parseRelocs( |
| 2985 | self: *Object, | 2965 | self: *Object, |
| | 2966 | n_sect: u8, |
| 2986 | sect: macho.section_64, | 2967 | sect: macho.section_64, |
| 2987 | out: *std.ArrayListUnmanaged(Relocation), | 2968 | out: *std.ArrayListUnmanaged(Relocation), |
| 2988 | handle: File.Handle, | 2969 | handle: File.Handle, |
| ... | @@ -2992,19 +2973,12 @@ const aarch64 = struct { | ... | @@ -2992,19 +2973,12 @@ const aarch64 = struct { |
| 2992 | | 2973 | |
| 2993 | const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info)); | 2974 | const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info)); |
| 2994 | defer gpa.free(relocs_buffer); | 2975 | defer gpa.free(relocs_buffer); |
| 2995 | { | 2976 | const amt = try handle.preadAll(relocs_buffer, sect.reloff + self.offset); |
| 2996 | const amt = try handle.preadAll(relocs_buffer, sect.reloff + self.offset); | 2977 | if (amt != relocs_buffer.len) return error.InputOutput; |
| 2997 | if (amt != relocs_buffer.len) return error.InputOutput; | | |
| 2998 | } | | |
| 2999 | const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc]; | 2978 | const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc]; |
| 3000 | | 2979 | |
| 3001 | const sect_size = math.cast(usize, sect.size) orelse return error.Overflow; | 2980 | const code = try self.readSectionData(gpa, handle, n_sect); |
| 3002 | const code = try gpa.alloc(u8, sect_size); | | |
| 3003 | defer gpa.free(code); | 2981 | 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 | } | | |
| 3008 | | 2982 | |
| 3009 | try out.ensureTotalCapacityPrecise(gpa, relocs.len); | 2983 | try out.ensureTotalCapacityPrecise(gpa, relocs.len); |
| 3010 | | 2984 | |