authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-02-07 15:20:56+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-02-07 15:20:56+01:00
log9ccd8ed0ad4cc9e68c2a2e0c9b1e32d50259357e
treeb44064a12274e2b7adcfaea3adbca93ba8ce1af2
parenta5b34a61ab61882bf55d87e4cbc8186215ecf320
parent4b1a883d35565766d30f587d2cb2ccfe8c065c8b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14575 from ziglang/fix-14459

macho+zld: fix misc alignment issues when parsing object files in archives

7 files changed, 137 insertions(+), 95 deletions(-)

src/link/MachO/Dylib.zig+2
......@@ -166,6 +166,8 @@ pub fn parseFromBinary(
166166 const symtab_cmd = cmd.cast(macho.symtab_command).?;
167167 const symtab = @ptrCast(
168168 [*]const macho.nlist_64,
169 // Alignment is guaranteed as a dylib is a final linked image and has to have sections
170 // properly aligned in order to be correctly loaded by the loader.
169171 @alignCast(@alignOf(macho.nlist_64), &data[symtab_cmd.symoff]),
170172 )[0..symtab_cmd.nsyms];
171173 const strtab = data[symtab_cmd.stroff..][0..symtab_cmd.strsize];
src/link/MachO/Object.zig+103-52
......@@ -60,14 +60,24 @@ globals_lookup: []i64 = undefined,
6060/// Can be undefined as set together with in_symtab.
6161relocs_lookup: []RelocEntry = undefined,
6262
63/// All relocations sorted and flatened, sorted by address descending
64/// per section.
65relocations: std.ArrayListUnmanaged(macho.relocation_info) = .{},
66/// Beginning index to the relocations array for each input section
67/// defined within this Object file.
68section_relocs_lookup: std.ArrayListUnmanaged(u32) = .{},
69
70/// Data-in-code records sorted by address.
71data_in_code: std.ArrayListUnmanaged(macho.data_in_code_entry) = .{},
72
6373atoms: std.ArrayListUnmanaged(AtomIndex) = .{},
6474exec_atoms: std.ArrayListUnmanaged(AtomIndex) = .{},
6575
66eh_frame_sect: ?macho.section_64 = null,
76eh_frame_sect_id: ?u8 = null,
6777eh_frame_relocs_lookup: std.AutoArrayHashMapUnmanaged(u32, Record) = .{},
6878eh_frame_records_lookup: std.AutoArrayHashMapUnmanaged(AtomIndex, u32) = .{},
6979
70unwind_info_sect: ?macho.section_64 = null,
80unwind_info_sect_id: ?u8 = null,
7181unwind_relocs_lookup: []Record = undefined,
7282unwind_records_lookup: std.AutoHashMapUnmanaged(AtomIndex, u32) = .{},
7383
......@@ -100,6 +110,9 @@ pub fn deinit(self: *Object, gpa: Allocator) void {
100110 gpa.free(self.unwind_relocs_lookup);
101111 }
102112 self.unwind_records_lookup.deinit(gpa);
113 self.relocations.deinit(gpa);
114 self.section_relocs_lookup.deinit(gpa);
115 self.data_in_code.deinit(gpa);
103116}
104117
105118pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch) !void {
......@@ -137,15 +150,18 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)
137150 .buffer = self.contents[@sizeOf(macho.mach_header_64)..][0..self.header.sizeofcmds],
138151 };
139152 const nsects = self.getSourceSections().len;
153
154 // Prepopulate relocations per section lookup table.
155 try self.section_relocs_lookup.resize(allocator, nsects);
156 mem.set(u32, self.section_relocs_lookup.items, 0);
157
158 // Parse symtab.
140159 const symtab = while (it.next()) |cmd| switch (cmd.cmd()) {
141160 .SYMTAB => break cmd.cast(macho.symtab_command).?,
142161 else => {},
143162 } else return;
144163
145 self.in_symtab = @ptrCast(
146 [*]const macho.nlist_64,
147 @alignCast(@alignOf(macho.nlist_64), &self.contents[symtab.symoff]),
148 )[0..symtab.nsyms];
164 self.in_symtab = @ptrCast([*]align(1) const macho.nlist_64, self.contents.ptr + symtab.symoff)[0..symtab.nsyms];
149165 self.in_strtab = self.contents[symtab.stroff..][0..symtab.strsize];
150166
151167 self.symtab = try allocator.alloc(macho.nlist_64, self.in_symtab.?.len + nsects);
......@@ -212,10 +228,10 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)
212228 }
213229
214230 // Parse __TEXT,__eh_frame header if one exists
215 self.eh_frame_sect = self.getSourceSectionByName("__TEXT", "__eh_frame");
231 self.eh_frame_sect_id = self.getSourceSectionIndexByName("__TEXT", "__eh_frame");
216232
217233 // Parse __LD,__compact_unwind header if one exists
218 self.unwind_info_sect = self.getSourceSectionByName("__LD", "__compact_unwind");
234 self.unwind_info_sect_id = self.getSourceSectionIndexByName("__LD", "__compact_unwind");
219235 if (self.hasUnwindRecords()) {
220236 self.unwind_relocs_lookup = try allocator.alloc(Record, self.getUnwindRecords().len);
221237 mem.set(Record, self.unwind_relocs_lookup, .{
......@@ -354,6 +370,7 @@ pub fn splitIntoAtoms(self: *Object, zld: *Zld, object_id: u32) !void {
354370 try self.splitRegularSections(zld, object_id);
355371 try self.parseEhFrameSection(zld, object_id);
356372 try self.parseUnwindInfo(zld, object_id);
373 try self.parseDataInCode(zld.gpa);
357374}
358375
359376/// Splits input regular sections into Atoms.
......@@ -452,6 +469,8 @@ pub fn splitRegularSections(self: *Object, zld: *Zld, object_id: u32) !void {
452469 zld.sections.items(.header)[out_sect_id].sectName(),
453470 });
454471
472 try self.parseRelocs(gpa, section.id);
473
455474 const cpu_arch = zld.options.target.cpu.arch;
456475 const sect_loc = filterSymbolsBySection(symtab[sect_sym_index..], sect_id + 1);
457476 const sect_start_index = sect_sym_index + sect_loc.index;
......@@ -623,25 +642,36 @@ fn filterRelocs(
623642 return .{ .start = @intCast(u32, start), .len = @intCast(u32, len) };
624643}
625644
645/// Parse all relocs for the input section, and sort in descending order.
646/// Previously, I have wrongly assumed the compilers output relocations for each
647/// section in a sorted manner which is simply not true.
648fn parseRelocs(self: *Object, gpa: Allocator, sect_id: u8) !void {
649 const section = self.getSourceSection(sect_id);
650 const start = @intCast(u32, self.relocations.items.len);
651 if (self.getSourceRelocs(section)) |relocs| {
652 try self.relocations.ensureUnusedCapacity(gpa, relocs.len);
653 self.relocations.appendUnalignedSliceAssumeCapacity(relocs);
654 std.sort.sort(macho.relocation_info, self.relocations.items[start..], {}, relocGreaterThan);
655 }
656 self.section_relocs_lookup.items[sect_id] = start;
657}
658
626659fn cacheRelocs(self: *Object, zld: *Zld, atom_index: AtomIndex) !void {
627660 const atom = zld.getAtom(atom_index);
628661
629 const source_sect = if (self.getSourceSymbol(atom.sym_index)) |source_sym| blk: {
630 const source_sect = self.getSourceSection(source_sym.n_sect - 1);
631 assert(!source_sect.isZerofill());
632 break :blk source_sect;
662 const source_sect_id = if (self.getSourceSymbol(atom.sym_index)) |source_sym| blk: {
663 break :blk source_sym.n_sect - 1;
633664 } else blk: {
634665 // If there was no matching symbol present in the source symtab, this means
635666 // we are dealing with either an entire section, or part of it, but also
636667 // starting at the beginning.
637668 const nbase = @intCast(u32, self.in_symtab.?.len);
638 const sect_id = @intCast(u16, atom.sym_index - nbase);
639 const source_sect = self.getSourceSection(sect_id);
640 assert(!source_sect.isZerofill());
641 break :blk source_sect;
669 const sect_id = @intCast(u8, atom.sym_index - nbase);
670 break :blk sect_id;
642671 };
643
644 const relocs = self.getRelocs(source_sect);
672 const source_sect = self.getSourceSection(source_sect_id);
673 assert(!source_sect.isZerofill());
674 const relocs = self.getRelocs(source_sect_id);
645675
646676 self.relocs_lookup[atom.sym_index] = if (self.getSourceSymbol(atom.sym_index)) |source_sym| blk: {
647677 const offset = source_sym.n_value - source_sect.addr;
......@@ -649,8 +679,14 @@ fn cacheRelocs(self: *Object, zld: *Zld, atom_index: AtomIndex) !void {
649679 } else filterRelocs(relocs, 0, atom.size);
650680}
651681
682fn relocGreaterThan(ctx: void, lhs: macho.relocation_info, rhs: macho.relocation_info) bool {
683 _ = ctx;
684 return lhs.r_address > rhs.r_address;
685}
686
652687fn parseEhFrameSection(self: *Object, zld: *Zld, object_id: u32) !void {
653 const sect = self.eh_frame_sect orelse return;
688 const sect_id = self.eh_frame_sect_id orelse return;
689 const sect = self.getSourceSection(sect_id);
654690
655691 log.debug("parsing __TEXT,__eh_frame section", .{});
656692
......@@ -660,7 +696,8 @@ fn parseEhFrameSection(self: *Object, zld: *Zld, object_id: u32) !void {
660696
661697 const gpa = zld.gpa;
662698 const cpu_arch = zld.options.target.cpu.arch;
663 const relocs = self.getRelocs(sect);
699 try self.parseRelocs(gpa, sect_id);
700 const relocs = self.getRelocs(sect_id);
664701
665702 var it = self.getEhFrameRecordsIterator();
666703 var record_count: u32 = 0;
......@@ -728,12 +765,12 @@ fn parseEhFrameSection(self: *Object, zld: *Zld, object_id: u32) !void {
728765}
729766
730767fn parseUnwindInfo(self: *Object, zld: *Zld, object_id: u32) !void {
731 const sect = self.unwind_info_sect orelse {
768 const sect_id = self.unwind_info_sect_id orelse {
732769 // If it so happens that the object had `__eh_frame` section defined but no `__compact_unwind`,
733770 // we will try fully synthesising unwind info records to somewhat match Apple ld's
734771 // approach. However, we will only synthesise DWARF records and nothing more. For this reason,
735772 // we still create the output `__TEXT,__unwind_info` section.
736 if (self.eh_frame_sect != null) {
773 if (self.hasEhFrameRecords()) {
737774 if (zld.getSectionByName("__TEXT", "__unwind_info") == null) {
738775 _ = try zld.initSection("__TEXT", "__unwind_info", .{});
739776 }
......@@ -758,15 +795,15 @@ fn parseUnwindInfo(self: *Object, zld: *Zld, object_id: u32) !void {
758795 if (UnwindInfo.UnwindEncoding.isDwarf(record.compactUnwindEncoding, cpu_arch)) break true;
759796 } else false;
760797
761 if (needs_eh_frame) {
762 if (self.eh_frame_sect == null) {
763 log.err("missing __TEXT,__eh_frame section", .{});
764 log.err(" in object {s}", .{self.name});
765 return error.MissingSection;
766 }
798 if (needs_eh_frame and !self.hasEhFrameRecords()) {
799 log.err("missing __TEXT,__eh_frame section", .{});
800 log.err(" in object {s}", .{self.name});
801 return error.MissingSection;
767802 }
768803
769 const relocs = self.getRelocs(sect);
804 try self.parseRelocs(gpa, sect_id);
805 const relocs = self.getRelocs(sect_id);
806
770807 for (unwind_records) |record, record_id| {
771808 const offset = record_id * @sizeOf(macho.compact_unwind_entry);
772809 const rel_pos = filterRelocs(
......@@ -806,25 +843,23 @@ pub fn getSourceSymbol(self: Object, index: u32) ?macho.nlist_64 {
806843 return symtab[mapped_index];
807844}
808845
809pub fn getSourceSection(self: Object, index: u16) macho.section_64 {
846pub fn getSourceSection(self: Object, index: u8) macho.section_64 {
810847 const sections = self.getSourceSections();
811848 assert(index < sections.len);
812849 return sections[index];
813850}
814851
815852pub fn getSourceSectionByName(self: Object, segname: []const u8, sectname: []const u8) ?macho.section_64 {
853 const index = self.getSourceSectionIndexByName(segname, sectname) orelse return null;
816854 const sections = self.getSourceSections();
817 for (sections) |sect| {
818 if (mem.eql(u8, segname, sect.segName()) and mem.eql(u8, sectname, sect.sectName()))
819 return sect;
820 } else return null;
855 return sections[index];
821856}
822857
823858pub fn getSourceSectionIndexByName(self: Object, segname: []const u8, sectname: []const u8) ?u8 {
824859 const sections = self.getSourceSections();
825860 for (sections) |sect, i| {
826861 if (mem.eql(u8, segname, sect.segName()) and mem.eql(u8, sectname, sect.sectName()))
827 return @intCast(u8, i + 1);
862 return @intCast(u8, i);
828863 } else return null;
829864}
830865
......@@ -841,24 +876,27 @@ pub fn getSourceSections(self: Object) []const macho.section_64 {
841876 } else unreachable;
842877}
843878
844pub fn parseDataInCode(self: Object) ?[]const macho.data_in_code_entry {
879pub fn parseDataInCode(self: *Object, gpa: Allocator) !void {
845880 var it = LoadCommandIterator{
846881 .ncmds = self.header.ncmds,
847882 .buffer = self.contents[@sizeOf(macho.mach_header_64)..][0..self.header.sizeofcmds],
848883 };
849 while (it.next()) |cmd| {
884 const cmd = while (it.next()) |cmd| {
850885 switch (cmd.cmd()) {
851 .DATA_IN_CODE => {
852 const dice = cmd.cast(macho.linkedit_data_command).?;
853 const ndice = @divExact(dice.datasize, @sizeOf(macho.data_in_code_entry));
854 return @ptrCast(
855 [*]const macho.data_in_code_entry,
856 @alignCast(@alignOf(macho.data_in_code_entry), &self.contents[dice.dataoff]),
857 )[0..ndice];
858 },
886 .DATA_IN_CODE => break cmd.cast(macho.linkedit_data_command).?,
859887 else => {},
860888 }
861 } else return null;
889 } else return;
890 const ndice = @divExact(cmd.datasize, @sizeOf(macho.data_in_code_entry));
891 const dice = @ptrCast([*]align(1) const macho.data_in_code_entry, self.contents.ptr + cmd.dataoff)[0..ndice];
892 try self.data_in_code.ensureTotalCapacityPrecise(gpa, dice.len);
893 self.data_in_code.appendUnalignedSliceAssumeCapacity(dice);
894 std.sort.sort(macho.data_in_code_entry, self.data_in_code.items, {}, diceLessThan);
895}
896
897fn diceLessThan(ctx: void, lhs: macho.data_in_code_entry, rhs: macho.data_in_code_entry) bool {
898 _ = ctx;
899 return lhs.offset < rhs.offset;
862900}
863901
864902fn parseDysymtab(self: Object) ?macho.dysymtab_command {
......@@ -914,11 +952,18 @@ pub fn getSectionAliasSymbolPtr(self: *Object, sect_id: u8) *macho.nlist_64 {
914952 return &self.symtab[self.getSectionAliasSymbolIndex(sect_id)];
915953}
916954
917pub fn getRelocs(self: Object, sect: macho.section_64) []align(1) const macho.relocation_info {
918 if (sect.nreloc == 0) return &[0]macho.relocation_info{};
955fn getSourceRelocs(self: Object, sect: macho.section_64) ?[]align(1) const macho.relocation_info {
956 if (sect.nreloc == 0) return null;
919957 return @ptrCast([*]align(1) const macho.relocation_info, self.contents.ptr + sect.reloff)[0..sect.nreloc];
920958}
921959
960pub fn getRelocs(self: Object, sect_id: u8) []const macho.relocation_info {
961 const sect = self.getSourceSection(sect_id);
962 const start = self.section_relocs_lookup.items[sect_id];
963 const len = sect.nreloc;
964 return self.relocations.items[start..][0..len];
965}
966
922967pub fn getSymbolName(self: Object, index: u32) []const u8 {
923968 const strtab = self.in_strtab.?;
924969 const sym = self.symtab[index];
......@@ -976,22 +1021,28 @@ pub fn getAtomIndexForSymbol(self: Object, sym_index: u32) ?AtomIndex {
9761021}
9771022
9781023pub fn hasUnwindRecords(self: Object) bool {
979 return self.unwind_info_sect != null;
1024 return self.unwind_info_sect_id != null;
9801025}
9811026
9821027pub fn getUnwindRecords(self: Object) []align(1) const macho.compact_unwind_entry {
983 const sect = self.unwind_info_sect orelse return &[0]macho.compact_unwind_entry{};
1028 const sect_id = self.unwind_info_sect_id orelse return &[0]macho.compact_unwind_entry{};
1029 const sect = self.getSourceSection(sect_id);
9841030 const data = self.getSectionContents(sect);
9851031 const num_entries = @divExact(data.len, @sizeOf(macho.compact_unwind_entry));
9861032 return @ptrCast([*]align(1) const macho.compact_unwind_entry, data)[0..num_entries];
9871033}
9881034
9891035pub fn hasEhFrameRecords(self: Object) bool {
990 return self.eh_frame_sect != null;
1036 return self.eh_frame_sect_id != null;
9911037}
9921038
9931039pub fn getEhFrameRecordsIterator(self: Object) eh_frame.Iterator {
994 const sect = self.eh_frame_sect orelse return .{ .data = &[0]u8{} };
1040 const sect_id = self.eh_frame_sect_id orelse return .{ .data = &[0]u8{} };
1041 const sect = self.getSourceSection(sect_id);
9951042 const data = self.getSectionContents(sect);
9961043 return .{ .data = data };
9971044}
1045
1046pub fn hasDataInCode(self: Object) bool {
1047 return self.data_in_code.items.len > 0;
1048}
src/link/MachO/UnwindInfo.zig+2-6
......@@ -703,15 +703,11 @@ pub fn parseRelocTarget(
703703 } else return sym_loc;
704704}
705705
706fn getRelocs(
707 zld: *Zld,
708 object_id: u32,
709 record_id: usize,
710) []align(1) const macho.relocation_info {
706fn getRelocs(zld: *Zld, object_id: u32, record_id: usize) []const macho.relocation_info {
711707 const object = &zld.objects.items[object_id];
712708 assert(object.hasUnwindRecords());
713709 const rel_pos = object.unwind_relocs_lookup[record_id].reloc;
714 const relocs = object.getRelocs(object.unwind_info_sect.?);
710 const relocs = object.getRelocs(object.unwind_info_sect_id.?);
715711 return relocs[rel_pos.start..][0..rel_pos.len];
716712}
717713
src/link/MachO/ZldAtom.zig+14-17
......@@ -143,7 +143,7 @@ pub fn calcInnerSymbolOffset(zld: *Zld, atom_index: AtomIndex, sym_index: u32) u
143143 sym.n_value
144144 else blk: {
145145 const nbase = @intCast(u32, object.in_symtab.?.len);
146 const sect_id = @intCast(u16, atom.sym_index - nbase);
146 const sect_id = @intCast(u8, atom.sym_index - nbase);
147147 const source_sect = object.getSourceSection(sect_id);
148148 break :blk source_sect.addr;
149149 };
......@@ -180,7 +180,7 @@ pub fn getRelocContext(zld: *Zld, atom_index: AtomIndex) RelocContext {
180180 };
181181 }
182182 const nbase = @intCast(u32, object.in_symtab.?.len);
183 const sect_id = @intCast(u16, atom.sym_index - nbase);
183 const sect_id = @intCast(u8, atom.sym_index - nbase);
184184 const source_sect = object.getSourceSection(sect_id);
185185 return .{
186186 .base_addr = source_sect.addr,
......@@ -724,7 +724,7 @@ fn resolveRelocsArm64(
724724
725725 if (rel.r_extern == 0) {
726726 const base_addr = if (target.sym_index > object.source_address_lookup.len)
727 @intCast(i64, object.getSourceSection(@intCast(u16, rel.r_symbolnum - 1)).addr)
727 @intCast(i64, object.getSourceSection(@intCast(u8, rel.r_symbolnum - 1)).addr)
728728 else
729729 object.source_address_lookup[target.sym_index];
730730 ptr_addend -= base_addr;
......@@ -861,7 +861,7 @@ fn resolveRelocsX86(
861861
862862 if (rel.r_extern == 0) {
863863 const base_addr = if (target.sym_index > object.source_address_lookup.len)
864 @intCast(i64, object.getSourceSection(@intCast(u16, rel.r_symbolnum - 1)).addr)
864 @intCast(i64, object.getSourceSection(@intCast(u8, rel.r_symbolnum - 1)).addr)
865865 else
866866 object.source_address_lookup[target.sym_index];
867867 addend += @intCast(i32, @intCast(i64, context.base_addr) + rel.r_address + 4 -
......@@ -884,7 +884,7 @@ fn resolveRelocsX86(
884884
885885 if (rel.r_extern == 0) {
886886 const base_addr = if (target.sym_index > object.source_address_lookup.len)
887 @intCast(i64, object.getSourceSection(@intCast(u16, rel.r_symbolnum - 1)).addr)
887 @intCast(i64, object.getSourceSection(@intCast(u8, rel.r_symbolnum - 1)).addr)
888888 else
889889 object.source_address_lookup[target.sym_index];
890890 addend -= base_addr;
......@@ -928,7 +928,7 @@ pub fn getAtomCode(zld: *Zld, atom_index: AtomIndex) []const u8 {
928928 // we are dealing with either an entire section, or part of it, but also
929929 // starting at the beginning.
930930 const nbase = @intCast(u32, object.in_symtab.?.len);
931 const sect_id = @intCast(u16, atom.sym_index - nbase);
931 const sect_id = @intCast(u8, atom.sym_index - nbase);
932932 const source_sect = object.getSourceSection(sect_id);
933933 assert(!source_sect.isZerofill());
934934 const code = object.getSectionContents(source_sect);
......@@ -943,28 +943,25 @@ pub fn getAtomCode(zld: *Zld, atom_index: AtomIndex) []const u8 {
943943 return code[offset..][0..code_len];
944944}
945945
946pub fn getAtomRelocs(zld: *Zld, atom_index: AtomIndex) []align(1) const macho.relocation_info {
946pub fn getAtomRelocs(zld: *Zld, atom_index: AtomIndex) []const macho.relocation_info {
947947 const atom = zld.getAtom(atom_index);
948948 assert(atom.getFile() != null); // Synthetic atom shouldn't need to unique for relocs.
949949 const object = zld.objects.items[atom.getFile().?];
950950 const cache = object.relocs_lookup[atom.sym_index];
951951
952 const source_sect = if (object.getSourceSymbol(atom.sym_index)) |source_sym| blk: {
953 const source_sect = object.getSourceSection(source_sym.n_sect - 1);
954 assert(!source_sect.isZerofill());
955 break :blk source_sect;
952 const source_sect_id = if (object.getSourceSymbol(atom.sym_index)) |source_sym| blk: {
953 break :blk source_sym.n_sect - 1;
956954 } else blk: {
957955 // If there was no matching symbol present in the source symtab, this means
958956 // we are dealing with either an entire section, or part of it, but also
959957 // starting at the beginning.
960958 const nbase = @intCast(u32, object.in_symtab.?.len);
961 const sect_id = @intCast(u16, atom.sym_index - nbase);
962 const source_sect = object.getSourceSection(sect_id);
963 assert(!source_sect.isZerofill());
964 break :blk source_sect;
959 const sect_id = @intCast(u8, atom.sym_index - nbase);
960 break :blk sect_id;
965961 };
966
967 const relocs = object.getRelocs(source_sect);
962 const source_sect = object.getSourceSection(source_sect_id);
963 assert(!source_sect.isZerofill());
964 const relocs = object.getRelocs(source_sect_id);
968965 return relocs[cache.start..][0..cache.len];
969966}
970967
src/link/MachO/dead_strip.zig+4-3
......@@ -88,7 +88,7 @@ fn collectRoots(zld: *Zld, roots: *AtomTable) !void {
8888 source_sym.n_sect - 1
8989 else sect_id: {
9090 const nbase = @intCast(u32, object.in_symtab.?.len);
91 const sect_id = @intCast(u16, atom.sym_index - nbase);
91 const sect_id = @intCast(u8, atom.sym_index - nbase);
9292 break :sect_id sect_id;
9393 };
9494 const source_sect = object.getSourceSection(sect_id);
......@@ -223,7 +223,7 @@ fn mark(zld: *Zld, roots: AtomTable, alive: *AtomTable) !void {
223223 source_sym.n_sect - 1
224224 else blk: {
225225 const nbase = @intCast(u32, object.in_symtab.?.len);
226 const sect_id = @intCast(u16, atom.sym_index - nbase);
226 const sect_id = @intCast(u8, atom.sym_index - nbase);
227227 break :blk sect_id;
228228 };
229229 const source_sect = object.getSourceSection(sect_id);
......@@ -350,8 +350,9 @@ fn markEhFrameRecord(zld: *Zld, object_id: u32, atom_index: AtomIndex, alive: *A
350350 }
351351 },
352352 .x86_64 => {
353 const sect = object.getSourceSection(object.eh_frame_sect_id.?);
353354 const lsda_ptr = try fde.getLsdaPointer(cie, .{
354 .base_addr = object.eh_frame_sect.?.addr,
355 .base_addr = sect.addr,
355356 .base_offset = fde_offset,
356357 });
357358 if (lsda_ptr) |lsda_address| {
src/link/MachO/eh_frame.zig+4-7
......@@ -171,8 +171,9 @@ pub fn write(zld: *Zld, unwind_info: *UnwindInfo) !void {
171171 const cie_record = eh_records.get(
172172 eh_frame_offset + 4 - fde_record.getCiePointer(),
173173 ).?;
174 const eh_frame_sect = object.getSourceSection(object.eh_frame_sect_id.?);
174175 const source_lsda_ptr = try fde_record.getLsdaPointer(cie_record, .{
175 .base_addr = object.eh_frame_sect.?.addr,
176 .base_addr = eh_frame_sect.addr,
176177 .base_offset = fde_record_offset,
177178 });
178179 if (source_lsda_ptr) |ptr| {
......@@ -552,16 +553,12 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type {
552553 };
553554}
554555
555pub fn getRelocs(
556 zld: *Zld,
557 object_id: u32,
558 source_offset: u32,
559) []align(1) const macho.relocation_info {
556pub fn getRelocs(zld: *Zld, object_id: u32, source_offset: u32) []const macho.relocation_info {
560557 const object = &zld.objects.items[object_id];
561558 assert(object.hasEhFrameRecords());
562559 const urel = object.eh_frame_relocs_lookup.get(source_offset) orelse
563560 return &[0]macho.relocation_info{};
564 const all_relocs = object.getRelocs(object.eh_frame_sect.?);
561 const all_relocs = object.getRelocs(object.eh_frame_sect_id.?);
565562 return all_relocs[urel.reloc.start..][0..urel.reloc.len];
566563}
567564
src/link/MachO/zld.zig+8-10
......@@ -2391,22 +2391,20 @@ pub const Zld = struct {
23912391 const text_sect_header = self.sections.items(.header)[text_sect_id];
23922392
23932393 for (self.objects.items) |object| {
2394 const dice = object.parseDataInCode() orelse continue;
2394 if (!object.hasDataInCode()) continue;
2395 const dice = object.data_in_code.items;
23952396 try out_dice.ensureUnusedCapacity(dice.len);
23962397
2397 for (object.atoms.items) |atom_index| {
2398 for (object.exec_atoms.items) |atom_index| {
23982399 const atom = self.getAtom(atom_index);
23992400 const sym = self.getSymbol(atom.getSymbolWithLoc());
2400 const sect_id = sym.n_sect - 1;
2401 if (sect_id != text_sect_id) {
2402 continue;
2403 }
2401 if (sym.n_desc == N_DEAD) continue;
24042402
24052403 const source_addr = if (object.getSourceSymbol(atom.sym_index)) |source_sym|
24062404 source_sym.n_value
24072405 else blk: {
24082406 const nbase = @intCast(u32, object.in_symtab.?.len);
2409 const source_sect_id = @intCast(u16, atom.sym_index - nbase);
2407 const source_sect_id = @intCast(u8, atom.sym_index - nbase);
24102408 break :blk object.getSourceSection(source_sect_id).addr;
24112409 };
24122410 const filtered_dice = filterDataInCode(dice, source_addr, source_addr + atom.size);
......@@ -2699,12 +2697,12 @@ pub const Zld = struct {
26992697 // Exclude region comprising all symbol stabs.
27002698 const nlocals = self.dysymtab_cmd.nlocalsym;
27012699
2702 const locals_buf = try self.gpa.alloc(u8, nlocals * @sizeOf(macho.nlist_64));
2703 defer self.gpa.free(locals_buf);
2700 const locals = try self.gpa.alloc(macho.nlist_64, nlocals);
2701 defer self.gpa.free(locals);
27042702
2703 const locals_buf = @ptrCast([*]u8, locals.ptr)[0 .. @sizeOf(macho.nlist_64) * nlocals];
27052704 const amt = try self.file.preadAll(locals_buf, self.symtab_cmd.symoff);
27062705 if (amt != locals_buf.len) return error.InputOutput;
2707 const locals = @ptrCast([*]macho.nlist_64, @alignCast(@alignOf(macho.nlist_64), locals_buf))[0..nlocals];
27082706
27092707 const istab: usize = for (locals) |local, i| {
27102708 if (local.stab()) break i;