Commit a095263462

Jakub Konka <kubkon@jakubkonka.com>
2021-07-17 23:21:02
zld: more fixes todo with symbol resolution
namely, fixes proper symbol reolution when scanning and including objects from static archives, and properly discard any null symbols when a tentative definition was substituted by a defined, global symbol.
1 parent d8c4838
Changed files (1)
src
link
MachO
src/link/MachO/Zld.zig
@@ -1636,7 +1636,11 @@ fn resolveSymbols(self: *Zld) !void {
     }
 
     // Second pass, resolve symbols in static libraries.
-    loop: for (self.undefs.items) |sym| {
+    var next_sym: usize = 0;
+    loop: while (true) : (next_sym += 1) {
+        if (next_sym == self.undefs.items.len) break;
+
+        const sym = self.undefs.items[next_sym];
         if (symbolIsNull(sym)) continue;
 
         const sym_name = self.getString(sym.n_strx);
@@ -1661,6 +1665,8 @@ fn resolveSymbols(self: *Zld) !void {
     // Convert any tentative definition into a regular symbol and allocate
     // text blocks for each tentative defintion.
     for (self.tentatives.items) |sym| {
+        if (symbolIsNull(sym)) continue;
+
         const sym_name = self.getString(sym.n_strx);
         const match: MatchingSection = blk: {
             if (self.common_section_index == null) {
@@ -1813,6 +1819,13 @@ fn resolveSymbols(self: *Zld) !void {
             .n_desc = macho.N_WEAK_DEF,
             .n_value = seg.inner.vmaddr,
         });
+        undef.* = .{
+            .n_strx = 0,
+            .n_type = macho.N_UNDF,
+            .n_sect = 0,
+            .n_desc = 0,
+            .n_value = 0,
+        };
         resolv.* = .{
             .where = .global,
             .where_index = global_sym_index,