authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-02-06 13:23:03+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-02-06 13:23:08+01:00
logb32f5ee93283f7794c611ebd4a1fbc579b78d8ab
tree06d60c8a6011711279aa4ee971c7da68f42c9ca8
parenta5b34a61ab61882bf55d87e4cbc8186215ecf320

macho: downgrade alignment requirements for symtab in object files

Parse and sort relocations by address descending.

6 files changed, 104 insertions(+), 75 deletions(-)

src/link/MachO/Object.zig+79-41
...@@ -60,14 +60,20 @@ globals_lookup: []i64 = undefined,...@@ -60,14 +60,20 @@ globals_lookup: []i64 = undefined,
60/// Can be undefined as set together with in_symtab.60/// Can be undefined as set together with in_symtab.
61relocs_lookup: []RelocEntry = undefined,61relocs_lookup: []RelocEntry = undefined,
6262
63/// All relocations sorted and flatened.
64relocations: std.ArrayListUnmanaged(macho.relocation_info) = .{},
65/// Beginning index to the relocations array for each input section
66/// defined within this Object file.
67section_relocs_lookup: std.ArrayListUnmanaged(u32) = .{},
68
63atoms: std.ArrayListUnmanaged(AtomIndex) = .{},69atoms: std.ArrayListUnmanaged(AtomIndex) = .{},
64exec_atoms: std.ArrayListUnmanaged(AtomIndex) = .{},70exec_atoms: std.ArrayListUnmanaged(AtomIndex) = .{},
6571
66eh_frame_sect: ?macho.section_64 = null,72eh_frame_sect_id: ?u8 = null,
67eh_frame_relocs_lookup: std.AutoArrayHashMapUnmanaged(u32, Record) = .{},73eh_frame_relocs_lookup: std.AutoArrayHashMapUnmanaged(u32, Record) = .{},
68eh_frame_records_lookup: std.AutoArrayHashMapUnmanaged(AtomIndex, u32) = .{},74eh_frame_records_lookup: std.AutoArrayHashMapUnmanaged(AtomIndex, u32) = .{},
6975
70unwind_info_sect: ?macho.section_64 = null,76unwind_info_sect_id: ?u8 = null,
71unwind_relocs_lookup: []Record = undefined,77unwind_relocs_lookup: []Record = undefined,
72unwind_records_lookup: std.AutoHashMapUnmanaged(AtomIndex, u32) = .{},78unwind_records_lookup: std.AutoHashMapUnmanaged(AtomIndex, u32) = .{},
7379
...@@ -100,6 +106,8 @@ pub fn deinit(self: *Object, gpa: Allocator) void {...@@ -100,6 +106,8 @@ pub fn deinit(self: *Object, gpa: Allocator) void {
100 gpa.free(self.unwind_relocs_lookup);106 gpa.free(self.unwind_relocs_lookup);
101 }107 }
102 self.unwind_records_lookup.deinit(gpa);108 self.unwind_records_lookup.deinit(gpa);
109 self.relocations.deinit(gpa);
110 self.section_relocs_lookup.deinit(gpa);
103}111}
104112
105pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch) !void {113pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch) !void {
...@@ -137,15 +145,18 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)...@@ -137,15 +145,18 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)
137 .buffer = self.contents[@sizeOf(macho.mach_header_64)..][0..self.header.sizeofcmds],145 .buffer = self.contents[@sizeOf(macho.mach_header_64)..][0..self.header.sizeofcmds],
138 };146 };
139 const nsects = self.getSourceSections().len;147 const nsects = self.getSourceSections().len;
148
149 // Prepopulate relocations per section lookup table.
150 try self.section_relocs_lookup.resize(allocator, nsects);
151 mem.set(u32, self.section_relocs_lookup.items, 0);
152
153 // Parse symtab.
140 const symtab = while (it.next()) |cmd| switch (cmd.cmd()) {154 const symtab = while (it.next()) |cmd| switch (cmd.cmd()) {
141 .SYMTAB => break cmd.cast(macho.symtab_command).?,155 .SYMTAB => break cmd.cast(macho.symtab_command).?,
142 else => {},156 else => {},
143 } else return;157 } else return;
144158
145 self.in_symtab = @ptrCast(159 self.in_symtab = @ptrCast([*]align(1) const macho.nlist_64, self.contents.ptr + symtab.symoff)[0..symtab.nsyms];
146 [*]const macho.nlist_64,
147 @alignCast(@alignOf(macho.nlist_64), &self.contents[symtab.symoff]),
148 )[0..symtab.nsyms];
149 self.in_strtab = self.contents[symtab.stroff..][0..symtab.strsize];160 self.in_strtab = self.contents[symtab.stroff..][0..symtab.strsize];
150161
151 self.symtab = try allocator.alloc(macho.nlist_64, self.in_symtab.?.len + nsects);162 self.symtab = try allocator.alloc(macho.nlist_64, self.in_symtab.?.len + nsects);
...@@ -212,10 +223,10 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)...@@ -212,10 +223,10 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)
212 }223 }
213224
214 // Parse __TEXT,__eh_frame header if one exists225 // Parse __TEXT,__eh_frame header if one exists
215 self.eh_frame_sect = self.getSourceSectionByName("__TEXT", "__eh_frame");226 self.eh_frame_sect_id = self.getSourceSectionIndexByName("__TEXT", "__eh_frame");
216227
217 // Parse __LD,__compact_unwind header if one exists228 // Parse __LD,__compact_unwind header if one exists
218 self.unwind_info_sect = self.getSourceSectionByName("__LD", "__compact_unwind");229 self.unwind_info_sect_id = self.getSourceSectionIndexByName("__LD", "__compact_unwind");
219 if (self.hasUnwindRecords()) {230 if (self.hasUnwindRecords()) {
220 self.unwind_relocs_lookup = try allocator.alloc(Record, self.getUnwindRecords().len);231 self.unwind_relocs_lookup = try allocator.alloc(Record, self.getUnwindRecords().len);
221 mem.set(Record, self.unwind_relocs_lookup, .{232 mem.set(Record, self.unwind_relocs_lookup, .{
...@@ -452,6 +463,8 @@ pub fn splitRegularSections(self: *Object, zld: *Zld, object_id: u32) !void {...@@ -452,6 +463,8 @@ pub fn splitRegularSections(self: *Object, zld: *Zld, object_id: u32) !void {
452 zld.sections.items(.header)[out_sect_id].sectName(),463 zld.sections.items(.header)[out_sect_id].sectName(),
453 });464 });
454465
466 try self.parseRelocs(gpa, section.id);
467
455 const cpu_arch = zld.options.target.cpu.arch;468 const cpu_arch = zld.options.target.cpu.arch;
456 const sect_loc = filterSymbolsBySection(symtab[sect_sym_index..], sect_id + 1);469 const sect_loc = filterSymbolsBySection(symtab[sect_sym_index..], sect_id + 1);
457 const sect_start_index = sect_sym_index + sect_loc.index;470 const sect_start_index = sect_sym_index + sect_loc.index;
...@@ -623,25 +636,36 @@ fn filterRelocs(...@@ -623,25 +636,36 @@ fn filterRelocs(
623 return .{ .start = @intCast(u32, start), .len = @intCast(u32, len) };636 return .{ .start = @intCast(u32, start), .len = @intCast(u32, len) };
624}637}
625638
639/// Parse all relocs for the input section, and sort in descending order.
640/// Previously, I have wrongly assumed the compilers output relocations for each
641/// section in a sorted manner which is simply not true.
642fn parseRelocs(self: *Object, gpa: Allocator, sect_id: u8) !void {
643 const section = self.getSourceSection(sect_id);
644 const start = @intCast(u32, self.relocations.items.len);
645 if (self.getSourceRelocs(section)) |relocs| {
646 try self.relocations.ensureUnusedCapacity(gpa, relocs.len);
647 self.relocations.appendUnalignedSliceAssumeCapacity(relocs);
648 std.sort.sort(macho.relocation_info, self.relocations.items[start..], {}, relocGreaterThan);
649 }
650 self.section_relocs_lookup.items[sect_id] = start;
651}
652
626fn cacheRelocs(self: *Object, zld: *Zld, atom_index: AtomIndex) !void {653fn cacheRelocs(self: *Object, zld: *Zld, atom_index: AtomIndex) !void {
627 const atom = zld.getAtom(atom_index);654 const atom = zld.getAtom(atom_index);
628655
629 const source_sect = if (self.getSourceSymbol(atom.sym_index)) |source_sym| blk: {656 const source_sect_id = if (self.getSourceSymbol(atom.sym_index)) |source_sym| blk: {
630 const source_sect = self.getSourceSection(source_sym.n_sect - 1);657 break :blk source_sym.n_sect - 1;
631 assert(!source_sect.isZerofill());
632 break :blk source_sect;
633 } else blk: {658 } else blk: {
634 // If there was no matching symbol present in the source symtab, this means659 // If there was no matching symbol present in the source symtab, this means
635 // we are dealing with either an entire section, or part of it, but also660 // we are dealing with either an entire section, or part of it, but also
636 // starting at the beginning.661 // starting at the beginning.
637 const nbase = @intCast(u32, self.in_symtab.?.len);662 const nbase = @intCast(u32, self.in_symtab.?.len);
638 const sect_id = @intCast(u16, atom.sym_index - nbase);663 const sect_id = @intCast(u8, atom.sym_index - nbase);
639 const source_sect = self.getSourceSection(sect_id);664 break :blk sect_id;
640 assert(!source_sect.isZerofill());
641 break :blk source_sect;
642 };665 };
643666 const source_sect = self.getSourceSection(source_sect_id);
644 const relocs = self.getRelocs(source_sect);667 assert(!source_sect.isZerofill());
668 const relocs = self.getRelocs(source_sect_id);
645669
646 self.relocs_lookup[atom.sym_index] = if (self.getSourceSymbol(atom.sym_index)) |source_sym| blk: {670 self.relocs_lookup[atom.sym_index] = if (self.getSourceSymbol(atom.sym_index)) |source_sym| blk: {
647 const offset = source_sym.n_value - source_sect.addr;671 const offset = source_sym.n_value - source_sect.addr;
...@@ -649,8 +673,14 @@ fn cacheRelocs(self: *Object, zld: *Zld, atom_index: AtomIndex) !void {...@@ -649,8 +673,14 @@ fn cacheRelocs(self: *Object, zld: *Zld, atom_index: AtomIndex) !void {
649 } else filterRelocs(relocs, 0, atom.size);673 } else filterRelocs(relocs, 0, atom.size);
650}674}
651675
676fn relocGreaterThan(ctx: void, lhs: macho.relocation_info, rhs: macho.relocation_info) bool {
677 _ = ctx;
678 return lhs.r_address > rhs.r_address;
679}
680
652fn parseEhFrameSection(self: *Object, zld: *Zld, object_id: u32) !void {681fn parseEhFrameSection(self: *Object, zld: *Zld, object_id: u32) !void {
653 const sect = self.eh_frame_sect orelse return;682 const sect_id = self.eh_frame_sect_id orelse return;
683 const sect = self.getSourceSection(sect_id);
654684
655 log.debug("parsing __TEXT,__eh_frame section", .{});685 log.debug("parsing __TEXT,__eh_frame section", .{});
656686
...@@ -660,7 +690,8 @@ fn parseEhFrameSection(self: *Object, zld: *Zld, object_id: u32) !void {...@@ -660,7 +690,8 @@ fn parseEhFrameSection(self: *Object, zld: *Zld, object_id: u32) !void {
660690
661 const gpa = zld.gpa;691 const gpa = zld.gpa;
662 const cpu_arch = zld.options.target.cpu.arch;692 const cpu_arch = zld.options.target.cpu.arch;
663 const relocs = self.getRelocs(sect);693 try self.parseRelocs(gpa, sect_id);
694 const relocs = self.getRelocs(sect_id);
664695
665 var it = self.getEhFrameRecordsIterator();696 var it = self.getEhFrameRecordsIterator();
666 var record_count: u32 = 0;697 var record_count: u32 = 0;
...@@ -728,12 +759,12 @@ fn parseEhFrameSection(self: *Object, zld: *Zld, object_id: u32) !void {...@@ -728,12 +759,12 @@ fn parseEhFrameSection(self: *Object, zld: *Zld, object_id: u32) !void {
728}759}
729760
730fn parseUnwindInfo(self: *Object, zld: *Zld, object_id: u32) !void {761fn parseUnwindInfo(self: *Object, zld: *Zld, object_id: u32) !void {
731 const sect = self.unwind_info_sect orelse {762 const sect_id = self.unwind_info_sect_id orelse {
732 // If it so happens that the object had `__eh_frame` section defined but no `__compact_unwind`,763 // If it so happens that the object had `__eh_frame` section defined but no `__compact_unwind`,
733 // we will try fully synthesising unwind info records to somewhat match Apple ld's764 // we will try fully synthesising unwind info records to somewhat match Apple ld's
734 // approach. However, we will only synthesise DWARF records and nothing more. For this reason,765 // approach. However, we will only synthesise DWARF records and nothing more. For this reason,
735 // we still create the output `__TEXT,__unwind_info` section.766 // we still create the output `__TEXT,__unwind_info` section.
736 if (self.eh_frame_sect != null) {767 if (self.hasEhFrameRecords()) {
737 if (zld.getSectionByName("__TEXT", "__unwind_info") == null) {768 if (zld.getSectionByName("__TEXT", "__unwind_info") == null) {
738 _ = try zld.initSection("__TEXT", "__unwind_info", .{});769 _ = try zld.initSection("__TEXT", "__unwind_info", .{});
739 }770 }
...@@ -758,15 +789,15 @@ fn parseUnwindInfo(self: *Object, zld: *Zld, object_id: u32) !void {...@@ -758,15 +789,15 @@ fn parseUnwindInfo(self: *Object, zld: *Zld, object_id: u32) !void {
758 if (UnwindInfo.UnwindEncoding.isDwarf(record.compactUnwindEncoding, cpu_arch)) break true;789 if (UnwindInfo.UnwindEncoding.isDwarf(record.compactUnwindEncoding, cpu_arch)) break true;
759 } else false;790 } else false;
760791
761 if (needs_eh_frame) {792 if (needs_eh_frame and !self.hasEhFrameRecords()) {
762 if (self.eh_frame_sect == null) {793 log.err("missing __TEXT,__eh_frame section", .{});
763 log.err("missing __TEXT,__eh_frame section", .{});794 log.err(" in object {s}", .{self.name});
764 log.err(" in object {s}", .{self.name});795 return error.MissingSection;
765 return error.MissingSection;
766 }
767 }796 }
768797
769 const relocs = self.getRelocs(sect);798 try self.parseRelocs(gpa, sect_id);
799 const relocs = self.getRelocs(sect_id);
800
770 for (unwind_records) |record, record_id| {801 for (unwind_records) |record, record_id| {
771 const offset = record_id * @sizeOf(macho.compact_unwind_entry);802 const offset = record_id * @sizeOf(macho.compact_unwind_entry);
772 const rel_pos = filterRelocs(803 const rel_pos = filterRelocs(
...@@ -806,25 +837,23 @@ pub fn getSourceSymbol(self: Object, index: u32) ?macho.nlist_64 {...@@ -806,25 +837,23 @@ pub fn getSourceSymbol(self: Object, index: u32) ?macho.nlist_64 {
806 return symtab[mapped_index];837 return symtab[mapped_index];
807}838}
808839
809pub fn getSourceSection(self: Object, index: u16) macho.section_64 {840pub fn getSourceSection(self: Object, index: u8) macho.section_64 {
810 const sections = self.getSourceSections();841 const sections = self.getSourceSections();
811 assert(index < sections.len);842 assert(index < sections.len);
812 return sections[index];843 return sections[index];
813}844}
814845
815pub fn getSourceSectionByName(self: Object, segname: []const u8, sectname: []const u8) ?macho.section_64 {846pub fn getSourceSectionByName(self: Object, segname: []const u8, sectname: []const u8) ?macho.section_64 {
847 const index = self.getSourceSectionIndexByName(segname, sectname) orelse return null;
816 const sections = self.getSourceSections();848 const sections = self.getSourceSections();
817 for (sections) |sect| {849 return sections[index];
818 if (mem.eql(u8, segname, sect.segName()) and mem.eql(u8, sectname, sect.sectName()))
819 return sect;
820 } else return null;
821}850}
822851
823pub fn getSourceSectionIndexByName(self: Object, segname: []const u8, sectname: []const u8) ?u8 {852pub fn getSourceSectionIndexByName(self: Object, segname: []const u8, sectname: []const u8) ?u8 {
824 const sections = self.getSourceSections();853 const sections = self.getSourceSections();
825 for (sections) |sect, i| {854 for (sections) |sect, i| {
826 if (mem.eql(u8, segname, sect.segName()) and mem.eql(u8, sectname, sect.sectName()))855 if (mem.eql(u8, segname, sect.segName()) and mem.eql(u8, sectname, sect.sectName()))
827 return @intCast(u8, i + 1);856 return @intCast(u8, i);
828 } else return null;857 } else return null;
829}858}
830859
...@@ -914,11 +943,18 @@ pub fn getSectionAliasSymbolPtr(self: *Object, sect_id: u8) *macho.nlist_64 {...@@ -914,11 +943,18 @@ pub fn getSectionAliasSymbolPtr(self: *Object, sect_id: u8) *macho.nlist_64 {
914 return &self.symtab[self.getSectionAliasSymbolIndex(sect_id)];943 return &self.symtab[self.getSectionAliasSymbolIndex(sect_id)];
915}944}
916945
917pub fn getRelocs(self: Object, sect: macho.section_64) []align(1) const macho.relocation_info {946fn getSourceRelocs(self: Object, sect: macho.section_64) ?[]align(1) const macho.relocation_info {
918 if (sect.nreloc == 0) return &[0]macho.relocation_info{};947 if (sect.nreloc == 0) return null;
919 return @ptrCast([*]align(1) const macho.relocation_info, self.contents.ptr + sect.reloff)[0..sect.nreloc];948 return @ptrCast([*]align(1) const macho.relocation_info, self.contents.ptr + sect.reloff)[0..sect.nreloc];
920}949}
921950
951pub fn getRelocs(self: Object, sect_id: u8) []const macho.relocation_info {
952 const sect = self.getSourceSection(sect_id);
953 const start = self.section_relocs_lookup.items[sect_id];
954 const len = sect.nreloc;
955 return self.relocations.items[start..][0..len];
956}
957
922pub fn getSymbolName(self: Object, index: u32) []const u8 {958pub fn getSymbolName(self: Object, index: u32) []const u8 {
923 const strtab = self.in_strtab.?;959 const strtab = self.in_strtab.?;
924 const sym = self.symtab[index];960 const sym = self.symtab[index];
...@@ -976,22 +1012,24 @@ pub fn getAtomIndexForSymbol(self: Object, sym_index: u32) ?AtomIndex {...@@ -976,22 +1012,24 @@ pub fn getAtomIndexForSymbol(self: Object, sym_index: u32) ?AtomIndex {
976}1012}
9771013
978pub fn hasUnwindRecords(self: Object) bool {1014pub fn hasUnwindRecords(self: Object) bool {
979 return self.unwind_info_sect != null;1015 return self.unwind_info_sect_id != null;
980}1016}
9811017
982pub fn getUnwindRecords(self: Object) []align(1) const macho.compact_unwind_entry {1018pub 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{};1019 const sect_id = self.unwind_info_sect_id orelse return &[0]macho.compact_unwind_entry{};
1020 const sect = self.getSourceSection(sect_id);
984 const data = self.getSectionContents(sect);1021 const data = self.getSectionContents(sect);
985 const num_entries = @divExact(data.len, @sizeOf(macho.compact_unwind_entry));1022 const num_entries = @divExact(data.len, @sizeOf(macho.compact_unwind_entry));
986 return @ptrCast([*]align(1) const macho.compact_unwind_entry, data)[0..num_entries];1023 return @ptrCast([*]align(1) const macho.compact_unwind_entry, data)[0..num_entries];
987}1024}
9881025
989pub fn hasEhFrameRecords(self: Object) bool {1026pub fn hasEhFrameRecords(self: Object) bool {
990 return self.eh_frame_sect != null;1027 return self.eh_frame_sect_id != null;
991}1028}
9921029
993pub fn getEhFrameRecordsIterator(self: Object) eh_frame.Iterator {1030pub fn getEhFrameRecordsIterator(self: Object) eh_frame.Iterator {
994 const sect = self.eh_frame_sect orelse return .{ .data = &[0]u8{} };1031 const sect_id = self.eh_frame_sect_id orelse return .{ .data = &[0]u8{} };
1032 const sect = self.getSourceSection(sect_id);
995 const data = self.getSectionContents(sect);1033 const data = self.getSectionContents(sect);
996 return .{ .data = data };1034 return .{ .data = data };
997}1035}
src/link/MachO/UnwindInfo.zig+2-6
...@@ -703,15 +703,11 @@ pub fn parseRelocTarget(...@@ -703,15 +703,11 @@ pub fn parseRelocTarget(
703 } else return sym_loc;703 } else return sym_loc;
704}704}
705705
706fn getRelocs(706fn getRelocs(zld: *Zld, object_id: u32, record_id: usize) []const macho.relocation_info {
707 zld: *Zld,
708 object_id: u32,
709 record_id: usize,
710) []align(1) const macho.relocation_info {
711 const object = &zld.objects.items[object_id];707 const object = &zld.objects.items[object_id];
712 assert(object.hasUnwindRecords());708 assert(object.hasUnwindRecords());
713 const rel_pos = object.unwind_relocs_lookup[record_id].reloc;709 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.?);
715 return relocs[rel_pos.start..][0..rel_pos.len];711 return relocs[rel_pos.start..][0..rel_pos.len];
716}712}
717713
src/link/MachO/ZldAtom.zig+14-17
...@@ -143,7 +143,7 @@ pub fn calcInnerSymbolOffset(zld: *Zld, atom_index: AtomIndex, sym_index: u32) u...@@ -143,7 +143,7 @@ pub fn calcInnerSymbolOffset(zld: *Zld, atom_index: AtomIndex, sym_index: u32) u
143 sym.n_value143 sym.n_value
144 else blk: {144 else blk: {
145 const nbase = @intCast(u32, object.in_symtab.?.len);145 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);
147 const source_sect = object.getSourceSection(sect_id);147 const source_sect = object.getSourceSection(sect_id);
148 break :blk source_sect.addr;148 break :blk source_sect.addr;
149 };149 };
...@@ -180,7 +180,7 @@ pub fn getRelocContext(zld: *Zld, atom_index: AtomIndex) RelocContext {...@@ -180,7 +180,7 @@ pub fn getRelocContext(zld: *Zld, atom_index: AtomIndex) RelocContext {
180 };180 };
181 }181 }
182 const nbase = @intCast(u32, object.in_symtab.?.len);182 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);
184 const source_sect = object.getSourceSection(sect_id);184 const source_sect = object.getSourceSection(sect_id);
185 return .{185 return .{
186 .base_addr = source_sect.addr,186 .base_addr = source_sect.addr,
...@@ -724,7 +724,7 @@ fn resolveRelocsArm64(...@@ -724,7 +724,7 @@ fn resolveRelocsArm64(
724724
725 if (rel.r_extern == 0) {725 if (rel.r_extern == 0) {
726 const base_addr = if (target.sym_index > object.source_address_lookup.len)726 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)
728 else728 else
729 object.source_address_lookup[target.sym_index];729 object.source_address_lookup[target.sym_index];
730 ptr_addend -= base_addr;730 ptr_addend -= base_addr;
...@@ -861,7 +861,7 @@ fn resolveRelocsX86(...@@ -861,7 +861,7 @@ fn resolveRelocsX86(
861861
862 if (rel.r_extern == 0) {862 if (rel.r_extern == 0) {
863 const base_addr = if (target.sym_index > object.source_address_lookup.len)863 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)
865 else865 else
866 object.source_address_lookup[target.sym_index];866 object.source_address_lookup[target.sym_index];
867 addend += @intCast(i32, @intCast(i64, context.base_addr) + rel.r_address + 4 -867 addend += @intCast(i32, @intCast(i64, context.base_addr) + rel.r_address + 4 -
...@@ -884,7 +884,7 @@ fn resolveRelocsX86(...@@ -884,7 +884,7 @@ fn resolveRelocsX86(
884884
885 if (rel.r_extern == 0) {885 if (rel.r_extern == 0) {
886 const base_addr = if (target.sym_index > object.source_address_lookup.len)886 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)
888 else888 else
889 object.source_address_lookup[target.sym_index];889 object.source_address_lookup[target.sym_index];
890 addend -= base_addr;890 addend -= base_addr;
...@@ -928,7 +928,7 @@ pub fn getAtomCode(zld: *Zld, atom_index: AtomIndex) []const u8 {...@@ -928,7 +928,7 @@ pub fn getAtomCode(zld: *Zld, atom_index: AtomIndex) []const u8 {
928 // we are dealing with either an entire section, or part of it, but also928 // we are dealing with either an entire section, or part of it, but also
929 // starting at the beginning.929 // starting at the beginning.
930 const nbase = @intCast(u32, object.in_symtab.?.len);930 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);
932 const source_sect = object.getSourceSection(sect_id);932 const source_sect = object.getSourceSection(sect_id);
933 assert(!source_sect.isZerofill());933 assert(!source_sect.isZerofill());
934 const code = object.getSectionContents(source_sect);934 const code = object.getSectionContents(source_sect);
...@@ -943,28 +943,25 @@ pub fn getAtomCode(zld: *Zld, atom_index: AtomIndex) []const u8 {...@@ -943,28 +943,25 @@ pub fn getAtomCode(zld: *Zld, atom_index: AtomIndex) []const u8 {
943 return code[offset..][0..code_len];943 return code[offset..][0..code_len];
944}944}
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 {
947 const atom = zld.getAtom(atom_index);947 const atom = zld.getAtom(atom_index);
948 assert(atom.getFile() != null); // Synthetic atom shouldn't need to unique for relocs.948 assert(atom.getFile() != null); // Synthetic atom shouldn't need to unique for relocs.
949 const object = zld.objects.items[atom.getFile().?];949 const object = zld.objects.items[atom.getFile().?];
950 const cache = object.relocs_lookup[atom.sym_index];950 const cache = object.relocs_lookup[atom.sym_index];
951951
952 const source_sect = if (object.getSourceSymbol(atom.sym_index)) |source_sym| blk: {952 const source_sect_id = if (object.getSourceSymbol(atom.sym_index)) |source_sym| blk: {
953 const source_sect = object.getSourceSection(source_sym.n_sect - 1);953 break :blk source_sym.n_sect - 1;
954 assert(!source_sect.isZerofill());
955 break :blk source_sect;
956 } else blk: {954 } else blk: {
957 // If there was no matching symbol present in the source symtab, this means955 // If there was no matching symbol present in the source symtab, this means
958 // we are dealing with either an entire section, or part of it, but also956 // we are dealing with either an entire section, or part of it, but also
959 // starting at the beginning.957 // starting at the beginning.
960 const nbase = @intCast(u32, object.in_symtab.?.len);958 const nbase = @intCast(u32, object.in_symtab.?.len);
961 const sect_id = @intCast(u16, atom.sym_index - nbase);959 const sect_id = @intCast(u8, atom.sym_index - nbase);
962 const source_sect = object.getSourceSection(sect_id);960 break :blk sect_id;
963 assert(!source_sect.isZerofill());
964 break :blk source_sect;
965 };961 };
966962 const source_sect = object.getSourceSection(source_sect_id);
967 const relocs = object.getRelocs(source_sect);963 assert(!source_sect.isZerofill());
964 const relocs = object.getRelocs(source_sect_id);
968 return relocs[cache.start..][0..cache.len];965 return relocs[cache.start..][0..cache.len];
969}966}
970967
src/link/MachO/dead_strip.zig+4-3
...@@ -88,7 +88,7 @@ fn collectRoots(zld: *Zld, roots: *AtomTable) !void {...@@ -88,7 +88,7 @@ fn collectRoots(zld: *Zld, roots: *AtomTable) !void {
88 source_sym.n_sect - 188 source_sym.n_sect - 1
89 else sect_id: {89 else sect_id: {
90 const nbase = @intCast(u32, object.in_symtab.?.len);90 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);
92 break :sect_id sect_id;92 break :sect_id sect_id;
93 };93 };
94 const source_sect = object.getSourceSection(sect_id);94 const source_sect = object.getSourceSection(sect_id);
...@@ -223,7 +223,7 @@ fn mark(zld: *Zld, roots: AtomTable, alive: *AtomTable) !void {...@@ -223,7 +223,7 @@ fn mark(zld: *Zld, roots: AtomTable, alive: *AtomTable) !void {
223 source_sym.n_sect - 1223 source_sym.n_sect - 1
224 else blk: {224 else blk: {
225 const nbase = @intCast(u32, object.in_symtab.?.len);225 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);
227 break :blk sect_id;227 break :blk sect_id;
228 };228 };
229 const source_sect = object.getSourceSection(sect_id);229 const source_sect = object.getSourceSection(sect_id);
...@@ -350,8 +350,9 @@ fn markEhFrameRecord(zld: *Zld, object_id: u32, atom_index: AtomIndex, alive: *A...@@ -350,8 +350,9 @@ fn markEhFrameRecord(zld: *Zld, object_id: u32, atom_index: AtomIndex, alive: *A
350 }350 }
351 },351 },
352 .x86_64 => {352 .x86_64 => {
353 const sect = object.getSourceSection(object.eh_frame_sect_id.?);
353 const lsda_ptr = try fde.getLsdaPointer(cie, .{354 const lsda_ptr = try fde.getLsdaPointer(cie, .{
354 .base_addr = object.eh_frame_sect.?.addr,355 .base_addr = sect.addr,
355 .base_offset = fde_offset,356 .base_offset = fde_offset,
356 });357 });
357 if (lsda_ptr) |lsda_address| {358 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 {...@@ -171,8 +171,9 @@ pub fn write(zld: *Zld, unwind_info: *UnwindInfo) !void {
171 const cie_record = eh_records.get(171 const cie_record = eh_records.get(
172 eh_frame_offset + 4 - fde_record.getCiePointer(),172 eh_frame_offset + 4 - fde_record.getCiePointer(),
173 ).?;173 ).?;
174 const eh_frame_sect = object.getSourceSection(object.eh_frame_sect_id.?);
174 const source_lsda_ptr = try fde_record.getLsdaPointer(cie_record, .{175 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,
176 .base_offset = fde_record_offset,177 .base_offset = fde_record_offset,
177 });178 });
178 if (source_lsda_ptr) |ptr| {179 if (source_lsda_ptr) |ptr| {
...@@ -552,16 +553,12 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type {...@@ -552,16 +553,12 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type {
552 };553 };
553}554}
554555
555pub fn getRelocs(556pub fn getRelocs(zld: *Zld, object_id: u32, source_offset: u32) []const macho.relocation_info {
556 zld: *Zld,
557 object_id: u32,
558 source_offset: u32,
559) []align(1) const macho.relocation_info {
560 const object = &zld.objects.items[object_id];557 const object = &zld.objects.items[object_id];
561 assert(object.hasEhFrameRecords());558 assert(object.hasEhFrameRecords());
562 const urel = object.eh_frame_relocs_lookup.get(source_offset) orelse559 const urel = object.eh_frame_relocs_lookup.get(source_offset) orelse
563 return &[0]macho.relocation_info{};560 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.?);
565 return all_relocs[urel.reloc.start..][0..urel.reloc.len];562 return all_relocs[urel.reloc.start..][0..urel.reloc.len];
566}563}
567564
src/link/MachO/zld.zig+1-1
...@@ -2406,7 +2406,7 @@ pub const Zld = struct {...@@ -2406,7 +2406,7 @@ pub const Zld = struct {
2406 source_sym.n_value2406 source_sym.n_value
2407 else blk: {2407 else blk: {
2408 const nbase = @intCast(u32, object.in_symtab.?.len);2408 const nbase = @intCast(u32, object.in_symtab.?.len);
2409 const source_sect_id = @intCast(u16, atom.sym_index - nbase);2409 const source_sect_id = @intCast(u8, atom.sym_index - nbase);
2410 break :blk object.getSourceSection(source_sect_id).addr;2410 break :blk object.getSourceSection(source_sect_id).addr;
2411 };2411 };
2412 const filtered_dice = filterDataInCode(dice, source_addr, source_addr + atom.size);2412 const filtered_dice = filterDataInCode(dice, source_addr, source_addr + atom.size);