| ... | ... | @@ -443,11 +443,8 @@ fn initCstringLiterals(self: *Object, allocator: Allocator, file: File.Handle, m |
| 443 | 443 | for (slice.items(.header), 0..) |sect, n_sect| { |
| 444 | 444 | if (!isCstringLiteral(sect)) continue; |
| 445 | 445 | |
| 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)); |
| 448 | 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 | 449 | var count: u32 = 0; |
| 453 | 450 | var start: u32 = 0; |
| ... | ... | @@ -646,13 +643,10 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO |
| 646 | 643 | } |
| 647 | 644 | |
| 648 | 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 | 647 | 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)); |
| 653 | 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 | 651 | for (subs.items) |sub| { |
| 658 | 652 | const atom = self.getAtom(sub.atom).?; |
| ... | ... | @@ -686,12 +680,7 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO |
| 686 | 680 | buffer.resize(target_size) catch unreachable; |
| 687 | 681 | const gop = try sections_data.getOrPut(target.n_sect); |
| 688 | 682 | 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)); |
| 695 | 684 | } |
| 696 | 685 | const data = gop.value_ptr.*; |
| 697 | 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 | 989 | defer tracy.end(); |
| 1001 | 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 | 993 | if (sect.nreloc == 0) continue; |
| 1005 | 994 | // We skip relocs for __DWARF since even in -r mode, the linker is expected to emit |
| 1006 | 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 | 998 | !mem.eql(u8, sect.sectName(), "__compact_unwind")) continue; |
| 1010 | 999 | |
| 1011 | 1000 | 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), |
| 1014 | 1003 | else => unreachable, |
| 1015 | 1004 | } |
| 1016 | 1005 | |
| ... | ... | @@ -1146,11 +1135,8 @@ fn initUnwindRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fil |
| 1146 | 1135 | }; |
| 1147 | 1136 | |
| 1148 | 1137 | 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); |
| 1151 | 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 | 1141 | const nrecs = @divExact(data.len, @sizeOf(macho.compact_unwind_entry)); |
| 1156 | 1142 | const recs = @as([*]align(1) const macho.compact_unwind_entry, @ptrCast(data.ptr))[0..nrecs]; |
| ... | ... | @@ -2810,6 +2796,7 @@ const CompactUnwindCtx = struct { |
| 2810 | 2796 | const x86_64 = struct { |
| 2811 | 2797 | fn parseRelocs( |
| 2812 | 2798 | self: *Object, |
| 2799 | n_sect: u8, |
| 2813 | 2800 | sect: macho.section_64, |
| 2814 | 2801 | out: *std.ArrayListUnmanaged(Relocation), |
| 2815 | 2802 | handle: File.Handle, |
| ... | ... | @@ -2819,19 +2806,12 @@ const x86_64 = struct { |
| 2819 | 2806 | |
| 2820 | 2807 | const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info)); |
| 2821 | 2808 | 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; |
| 2826 | 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; |
| 2829 | | const code = try gpa.alloc(u8, sect_size); |
| 2813 | const code = try self.readSectionData(gpa, handle, n_sect); |
| 2830 | 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 | 2816 | try out.ensureTotalCapacityPrecise(gpa, relocs.len); |
| 2837 | 2817 | |
| ... | ... | @@ -2983,6 +2963,7 @@ const x86_64 = struct { |
| 2983 | 2963 | const aarch64 = struct { |
| 2984 | 2964 | fn parseRelocs( |
| 2985 | 2965 | self: *Object, |
| 2966 | n_sect: u8, |
| 2986 | 2967 | sect: macho.section_64, |
| 2987 | 2968 | out: *std.ArrayListUnmanaged(Relocation), |
| 2988 | 2969 | handle: File.Handle, |
| ... | ... | @@ -2992,19 +2973,12 @@ const aarch64 = struct { |
| 2992 | 2973 | |
| 2993 | 2974 | const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info)); |
| 2994 | 2975 | 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; |
| 2999 | 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; |
| 3002 | | const code = try gpa.alloc(u8, sect_size); |
| 2980 | const code = try self.readSectionData(gpa, handle, n_sect); |
| 3003 | 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 | 2983 | try out.ensureTotalCapacityPrecise(gpa, relocs.len); |
| 3010 | 2984 | |