Commit 5154ece8b9

Jakub Konka <kubkon@jakubkonka.com>
2023-04-09 13:25:40
macho: fix incorrect DWARF subprogram generation
1 parent e72c41a
Changed files (2)
src/link/Dwarf.zig
@@ -150,7 +150,7 @@ pub const DeclState = struct {
                 .type = ty,
                 .offset = undefined,
             });
-            log.debug("%{d}: {}", .{ sym_index, ty.fmtDebug() });
+            log.debug("%{d}: {}", .{ sym_index, ty.fmt(self.mod) });
             try self.abbrev_resolver.putNoClobberContext(self.gpa, ty, sym_index, .{
                 .mod = self.mod,
             });
@@ -570,7 +570,7 @@ pub const DeclState = struct {
                 try dbg_info_buffer.append(0);
             },
             else => {
-                log.debug("TODO implement .debug_info for type '{}'", .{ty.fmtDebug()});
+                log.debug("TODO implement .debug_info for type '{}'", .{ty.fmt(self.mod)});
                 try dbg_info_buffer.append(@enumToInt(AbbrevKind.pad1));
             },
         }
@@ -1055,6 +1055,10 @@ pub fn commitDeclState(
                 },
             }
             {
+                log.debug("relocating subprogram high PC value: {x} => {x}", .{
+                    self.getRelocDbgInfoSubprogramHighPC(),
+                    sym_size,
+                });
                 const ptr = dbg_info_buffer.items[self.getRelocDbgInfoSubprogramHighPC()..][0..4];
                 mem.writeInt(u32, ptr, @intCast(u32, sym_size), target_endian);
             }
@@ -1263,7 +1267,12 @@ pub fn commitDeclState(
             } else {
                 const atom = self.getAtom(.di_atom, symbol.atom_index);
                 const value = atom.off + symbol.offset + reloc.addend;
-                log.debug("{x}: [() => {x}] (%{d}, '{}')", .{ reloc.offset, value, target, ty.fmtDebug() });
+                log.debug("{x}: [() => {x}] (%{d}, '{}')", .{
+                    reloc.offset,
+                    value,
+                    target,
+                    ty.fmt(module),
+                });
                 mem.writeInt(
                     u32,
                     dbg_info_buffer.items[reloc.offset..][0..@sizeOf(u32)],
src/link/MachO.zig
@@ -2070,8 +2070,6 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv
     self.freeUnnamedConsts(decl_index);
     Atom.freeRelocations(self, atom_index);
 
-    const atom = self.getAtom(atom_index);
-
     var code_buffer = std.ArrayList(u8).init(self.base.allocator);
     defer code_buffer.deinit();
 
@@ -2100,7 +2098,13 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv
     const addr = try self.updateDeclCode(decl_index, code);
 
     if (decl_state) |*ds| {
-        try self.d_sym.?.dwarf.commitDeclState(module, decl_index, addr, atom.size, ds);
+        try self.d_sym.?.dwarf.commitDeclState(
+            module,
+            decl_index,
+            addr,
+            self.getAtom(atom_index).size,
+            ds,
+        );
     }
 
     // Since we updated the vaddr and the size, each corresponding export symbol also
@@ -2196,8 +2200,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
     }
 
     const atom_index = try self.getOrCreateAtomForDecl(decl_index);
+    const sym_index = self.getAtom(atom_index).getSymbolIndex().?;
     Atom.freeRelocations(self, atom_index);
-    const atom = self.getAtom(atom_index);
 
     var code_buffer = std.ArrayList(u8).init(self.base.allocator);
     defer code_buffer.deinit();
@@ -2216,14 +2220,14 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
         }, &code_buffer, .{
             .dwarf = ds,
         }, .{
-            .parent_atom_index = atom.getSymbolIndex().?,
+            .parent_atom_index = sym_index,
         })
     else
         try codegen.generateSymbol(&self.base, decl.srcLoc(), .{
             .ty = decl.ty,
             .val = decl_val,
         }, &code_buffer, .none, .{
-            .parent_atom_index = atom.getSymbolIndex().?,
+            .parent_atom_index = sym_index,
         });
 
     var code = switch (res) {
@@ -2237,7 +2241,13 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
     const addr = try self.updateDeclCode(decl_index, code);
 
     if (decl_state) |*ds| {
-        try self.d_sym.?.dwarf.commitDeclState(module, decl_index, addr, atom.size, ds);
+        try self.d_sym.?.dwarf.commitDeclState(
+            module,
+            decl_index,
+            addr,
+            self.getAtom(atom_index).size,
+            ds,
+        );
     }
 
     // Since we updated the vaddr and the size, each corresponding export symbol also