| ... | ... | @@ -31,6 +31,7 @@ const MachO = @import("../MachO.zig"); |
| 31 | 31 | const Md5 = std.crypto.hash.Md5; |
| 32 | 32 | const LibStub = @import("../tapi.zig").LibStub; |
| 33 | 33 | const Object = @import("Object.zig"); |
| 34 | const Section = MachO.Section; |
| 34 | 35 | const StringTable = @import("../strtab.zig").StringTable; |
| 35 | 36 | const SymbolWithLoc = MachO.SymbolWithLoc; |
| 36 | 37 | const SymbolResolver = MachO.SymbolResolver; |
| ... | ... | @@ -231,7 +232,7 @@ pub const Zld = struct { |
| 231 | 232 | const sym = self.getSymbol(atom.getSymbolWithLoc()); |
| 232 | 233 | var section = self.sections.get(sym.n_sect - 1); |
| 233 | 234 | if (section.header.size > 0) { |
| 234 | | const last_atom = self.getAtomPtr(section.last_atom_index); |
| 235 | const last_atom = self.getAtomPtr(section.last_atom_index.?); |
| 235 | 236 | last_atom.next_index = atom_index; |
| 236 | 237 | atom.prev_index = section.last_atom_index; |
| 237 | 238 | } else { |
| ... | ... | @@ -445,7 +446,7 @@ pub const Zld = struct { |
| 445 | 446 | fn writeLazyPointer(self: *Zld, stub_helper_index: u32, writer: anytype) !void { |
| 446 | 447 | const target_addr = blk: { |
| 447 | 448 | const sect_id = self.getSectionByName("__TEXT", "__stub_helper").?; |
| 448 | | var atom_index = self.sections.items(.first_atom_index)[sect_id]; |
| 449 | var atom_index = self.sections.items(.first_atom_index)[sect_id].?; |
| 449 | 450 | var count: u32 = 0; |
| 450 | 451 | while (count < stub_helper_index + 1) : (count += 1) { |
| 451 | 452 | const atom = self.getAtom(atom_index); |
| ... | ... | @@ -497,7 +498,7 @@ pub const Zld = struct { |
| 497 | 498 | const target_addr = blk: { |
| 498 | 499 | // TODO: cache this at stub atom creation; they always go in pairs anyhow |
| 499 | 500 | const la_sect_id = self.getSectionByName("__DATA", "__la_symbol_ptr").?; |
| 500 | | var la_atom_index = self.sections.items(.first_atom_index)[la_sect_id]; |
| 501 | var la_atom_index = self.sections.items(.first_atom_index)[la_sect_id].?; |
| 501 | 502 | var count: u32 = 0; |
| 502 | 503 | while (count < stub_index) : (count += 1) { |
| 503 | 504 | const la_atom = self.getAtom(la_atom_index); |
| ... | ... | @@ -1012,11 +1013,10 @@ pub const Zld = struct { |
| 1012 | 1013 | |
| 1013 | 1014 | for (slice.items(.first_atom_index), 0..) |first_atom_index, sect_id| { |
| 1014 | 1015 | const header = slice.items(.header)[sect_id]; |
| 1015 | | var atom_index = first_atom_index; |
| 1016 | | |
| 1017 | | if (atom_index == 0) continue; |
| 1018 | 1016 | if (header.isZerofill()) continue; |
| 1019 | 1017 | |
| 1018 | var atom_index = first_atom_index orelse continue; |
| 1019 | |
| 1020 | 1020 | var buffer = std.ArrayList(u8).init(gpa); |
| 1021 | 1021 | defer buffer.deinit(); |
| 1022 | 1022 | try buffer.ensureTotalCapacity(math.cast(usize, header.size) orelse return error.Overflow); |
| ... | ... | @@ -1129,7 +1129,7 @@ pub const Zld = struct { |
| 1129 | 1129 | while (i < slice.len) : (i += 1) { |
| 1130 | 1130 | const section = self.sections.get(i); |
| 1131 | 1131 | if (section.header.size == 0) { |
| 1132 | | log.debug("pruning section {s},{s} {d}", .{ |
| 1132 | log.debug("pruning section {s},{s} {?d}", .{ |
| 1133 | 1133 | section.header.segName(), |
| 1134 | 1134 | section.header.sectName(), |
| 1135 | 1135 | section.first_atom_index, |
| ... | ... | @@ -1156,8 +1156,7 @@ pub const Zld = struct { |
| 1156 | 1156 | if (header.isCode() and !(header.type() == macho.S_SYMBOL_STUBS) and !mem.eql(u8, header.sectName(), "__stub_helper")) continue; |
| 1157 | 1157 | } |
| 1158 | 1158 | |
| 1159 | | var atom_index = slice.items(.first_atom_index)[sect_id]; |
| 1160 | | if (atom_index == 0) continue; |
| 1159 | var atom_index = slice.items(.first_atom_index)[sect_id] orelse continue; |
| 1161 | 1160 | |
| 1162 | 1161 | header.size = 0; |
| 1163 | 1162 | header.@"align" = 0; |
| ... | ... | @@ -1195,8 +1194,7 @@ pub const Zld = struct { |
| 1195 | 1194 | // We need to do this since our unwind info synthesiser relies on |
| 1196 | 1195 | // traversing the symbols when synthesising unwind info and DWARF CFI records. |
| 1197 | 1196 | for (slice.items(.first_atom_index)) |first_atom_index| { |
| 1198 | | if (first_atom_index == 0) continue; |
| 1199 | | var atom_index = first_atom_index; |
| 1197 | var atom_index = first_atom_index orelse continue; |
| 1200 | 1198 | |
| 1201 | 1199 | while (true) { |
| 1202 | 1200 | const atom = self.getAtom(atom_index); |
| ... | ... | @@ -1278,8 +1276,9 @@ pub const Zld = struct { |
| 1278 | 1276 | @as(u32, @intCast(segment.fileoff + start_aligned)); |
| 1279 | 1277 | header.addr = segment.vmaddr + start_aligned; |
| 1280 | 1278 | |
| 1281 | | var atom_index = slice.items(.first_atom_index)[indexes.start + sect_id]; |
| 1282 | | if (atom_index > 0) { |
| 1279 | if (slice.items(.first_atom_index)[indexes.start + sect_id]) |first_atom_index| { |
| 1280 | var atom_index = first_atom_index; |
| 1281 | |
| 1283 | 1282 | log.debug("allocating local symbols in sect({d}, '{s},{s}')", .{ |
| 1284 | 1283 | n_sect, |
| 1285 | 1284 | header.segName(), |
| ... | ... | @@ -1362,8 +1361,6 @@ pub const Zld = struct { |
| 1362 | 1361 | .reserved1 = opts.reserved1, |
| 1363 | 1362 | .reserved2 = opts.reserved2, |
| 1364 | 1363 | }, |
| 1365 | | .first_atom_index = 0, |
| 1366 | | .last_atom_index = 0, |
| 1367 | 1364 | }); |
| 1368 | 1365 | return index; |
| 1369 | 1366 | } |
| ... | ... | @@ -1491,7 +1488,7 @@ pub const Zld = struct { |
| 1491 | 1488 | if (self.getSectionByName("__DATA", "__la_symbol_ptr")) |sect_id| { |
| 1492 | 1489 | const segment_index = slice.items(.segment_index)[sect_id]; |
| 1493 | 1490 | const seg = self.getSegment(sect_id); |
| 1494 | | var atom_index = slice.items(.first_atom_index)[sect_id]; |
| 1491 | var atom_index = slice.items(.first_atom_index)[sect_id].?; |
| 1495 | 1492 | |
| 1496 | 1493 | try rebase.entries.ensureUnusedCapacity(self.gpa, self.stubs.items.len); |
| 1497 | 1494 | |
| ... | ... | @@ -1531,8 +1528,7 @@ pub const Zld = struct { |
| 1531 | 1528 | log.debug("{s},{s}", .{ header.segName(), header.sectName() }); |
| 1532 | 1529 | |
| 1533 | 1530 | const cpu_arch = self.options.target.cpu.arch; |
| 1534 | | var atom_index = slice.items(.first_atom_index)[sect_id]; |
| 1535 | | if (atom_index == 0) continue; |
| 1531 | var atom_index = slice.items(.first_atom_index)[sect_id] orelse continue; |
| 1536 | 1532 | |
| 1537 | 1533 | while (true) { |
| 1538 | 1534 | const atom = self.getAtom(atom_index); |
| ... | ... | @@ -1668,8 +1664,7 @@ pub const Zld = struct { |
| 1668 | 1664 | if (segment.maxprot & macho.PROT.WRITE == 0) continue; |
| 1669 | 1665 | |
| 1670 | 1666 | const cpu_arch = self.options.target.cpu.arch; |
| 1671 | | var atom_index = slice.items(.first_atom_index)[sect_id]; |
| 1672 | | if (atom_index == 0) continue; |
| 1667 | var atom_index = slice.items(.first_atom_index)[sect_id] orelse continue; |
| 1673 | 1668 | |
| 1674 | 1669 | log.debug("{s},{s}", .{ header.segName(), header.sectName() }); |
| 1675 | 1670 | |
| ... | ... | @@ -1757,7 +1752,7 @@ pub const Zld = struct { |
| 1757 | 1752 | const slice = self.sections.slice(); |
| 1758 | 1753 | const segment_index = slice.items(.segment_index)[sect_id]; |
| 1759 | 1754 | const seg = self.getSegment(sect_id); |
| 1760 | | var atom_index = slice.items(.first_atom_index)[sect_id]; |
| 1755 | var atom_index = slice.items(.first_atom_index)[sect_id].?; |
| 1761 | 1756 | |
| 1762 | 1757 | // TODO: we actually don't need to store lazy pointer atoms as they are synthetically generated by the linker |
| 1763 | 1758 | try lazy_bind.entries.ensureUnusedCapacity(self.gpa, self.stubs.items.len); |
| ... | ... | @@ -1920,7 +1915,7 @@ pub const Zld = struct { |
| 1920 | 1915 | const section = self.sections.get(stub_helper_section_index); |
| 1921 | 1916 | const stub_offset = stub_helpers.calcStubOffsetInStubHelper(self.options.target.cpu.arch); |
| 1922 | 1917 | const header = section.header; |
| 1923 | | var atom_index = section.first_atom_index; |
| 1918 | var atom_index = section.first_atom_index.?; |
| 1924 | 1919 | atom_index = self.getAtom(atom_index).next_index.?; // skip preamble |
| 1925 | 1920 | |
| 1926 | 1921 | var index: usize = 0; |
| ... | ... | @@ -2923,9 +2918,7 @@ pub const Zld = struct { |
| 2923 | 2918 | log.debug("atoms:", .{}); |
| 2924 | 2919 | const slice = self.sections.slice(); |
| 2925 | 2920 | for (slice.items(.first_atom_index), 0..) |first_atom_index, sect_id| { |
| 2926 | | var atom_index = first_atom_index; |
| 2927 | | if (atom_index == 0) continue; |
| 2928 | | |
| 2921 | var atom_index = first_atom_index orelse continue; |
| 2929 | 2922 | const header = slice.items(.header)[sect_id]; |
| 2930 | 2923 | |
| 2931 | 2924 | log.debug("{s},{s}", .{ header.segName(), header.sectName() }); |
| ... | ... | @@ -2990,13 +2983,6 @@ pub const Zld = struct { |
| 2990 | 2983 | |
| 2991 | 2984 | pub const N_DEAD: u16 = @as(u16, @bitCast(@as(i16, -1))); |
| 2992 | 2985 | |
| 2993 | | const Section = struct { |
| 2994 | | header: macho.section_64, |
| 2995 | | segment_index: u8, |
| 2996 | | first_atom_index: AtomIndex, |
| 2997 | | last_atom_index: AtomIndex, |
| 2998 | | }; |
| 2999 | | |
| 3000 | 2986 | pub const AtomIndex = u32; |
| 3001 | 2987 | |
| 3002 | 2988 | const IndirectPointer = struct { |
| ... | ... | @@ -3183,7 +3169,6 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr |
| 3183 | 3169 | }; |
| 3184 | 3170 | defer zld.deinit(); |
| 3185 | 3171 | |
| 3186 | | try zld.atoms.append(gpa, Atom.empty); // AtomIndex at 0 is reserved as null atom |
| 3187 | 3172 | try zld.strtab.buffer.append(gpa, 0); |
| 3188 | 3173 | |
| 3189 | 3174 | // Positional arguments to the linker such as object files and static archives. |