Commit 102846315c

Jakub Konka <kubkon@jakubkonka.com>
2024-02-08 13:12:06
macho: couple small fixes
1 parent ce207ca
Changed files (3)
src/link/MachO/relocatable.zig
@@ -403,6 +403,7 @@ fn calcSectionSizes(macho_file: *MachO) !void {
             if (!atom.flags.alive) continue;
             const header = &macho_file.sections.items(.header)[atom.out_n_sect];
             if (!macho_file.isZigSection(atom.out_n_sect)) continue;
+            if (!macho_file.isDebugSection(atom.out_n_sect)) continue;
             header.nreloc += atom.calcNumRelocs(macho_file);
         }
     }
@@ -540,6 +541,7 @@ fn writeAtoms(macho_file: *MachO) !void {
         if (atoms.items.len == 0) continue;
         if (header.isZerofill()) continue;
         if (macho_file.isZigSection(@intCast(i))) continue;
+        if (macho_file.isDebugSection(@intCast(i))) continue;
 
         const size = math.cast(usize, header.size) orelse return error.Overflow;
         const code = try gpa.alloc(u8, size);
@@ -581,6 +583,7 @@ fn writeAtoms(macho_file: *MachO) !void {
         for (macho_file.sections.items(.header), 0..) |header, n_sect| {
             if (header.isZerofill()) continue;
             if (!macho_file.isZigSection(@intCast(n_sect))) continue;
+            if (!macho_file.isDebugSection(@intCast(n_sect))) continue;
             const gop = try relocs.getOrPut(@intCast(n_sect));
             if (gop.found_existing) continue;
             gop.value_ptr.* = try std.ArrayList(macho.relocation_info).initCapacity(gpa, header.nreloc);
@@ -592,6 +595,7 @@ fn writeAtoms(macho_file: *MachO) !void {
             const header = macho_file.sections.items(.header)[atom.out_n_sect];
             if (header.isZerofill()) continue;
             if (!macho_file.isZigSection(atom.out_n_sect)) continue;
+            if (!macho_file.isDebugSection(atom.out_n_sect)) continue;
             if (atom.getRelocs(macho_file).len == 0) continue;
             const atom_size = math.cast(usize, atom.size) orelse return error.Overflow;
             const code = try gpa.alloc(u8, atom_size);
src/link/MachO/ZigObject.zig
@@ -52,11 +52,11 @@ dynamic_relocs: MachO.DynamicRelocs = .{},
 output_symtab_ctx: MachO.SymtabCtx = .{},
 output_ar_state: Archive.ArState = .{},
 
-debug_strtab_dirty: bool = true,
-debug_abbrev_dirty: bool = true,
-debug_aranges_dirty: bool = true,
-debug_info_header_dirty: bool = true,
-debug_line_header_dirty: bool = true,
+debug_strtab_dirty: bool = false,
+debug_abbrev_dirty: bool = false,
+debug_aranges_dirty: bool = false,
+debug_info_header_dirty: bool = false,
+debug_line_header_dirty: bool = false,
 
 pub fn init(self: *ZigObject, macho_file: *MachO) !void {
     const comp = macho_file.base.comp;
@@ -70,6 +70,11 @@ pub fn init(self: *ZigObject, macho_file: *MachO) !void {
         .dwarf => |v| {
             assert(v == .@"32");
             self.dwarf = Dwarf.init(&macho_file.base, .dwarf32);
+            self.debug_strtab_dirty = true;
+            self.debug_abbrev_dirty = true;
+            self.debug_aranges_dirty = true;
+            self.debug_info_header_dirty = true;
+            self.debug_line_header_dirty = true;
         },
         .code_view => unreachable,
     }
src/link/MachO.zig
@@ -3205,7 +3205,7 @@ fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 {
     for (self.sections.items(.header)) |header| {
         if (header.isZerofill()) continue;
         const increased_size = padToIdeal(header.size);
-        const test_end = header.offset + increased_size;
+        const test_end = header.offset +| increased_size;
         if (end > header.offset and start < test_end) {
             return test_end;
         }
@@ -3233,7 +3233,7 @@ fn detectAllocCollisionVirtual(self: *MachO, start: u64, size: u64) ?u64 {
 
     for (self.sections.items(.header)) |header| {
         const increased_size = padToIdeal(header.size);
-        const test_end = header.addr + increased_size;
+        const test_end = header.addr +| increased_size;
         if (end > header.addr and start < test_end) {
             return test_end;
         }
@@ -3250,7 +3250,7 @@ fn detectAllocCollisionVirtual(self: *MachO, start: u64, size: u64) ?u64 {
     return null;
 }
 
-fn allocatedSize(self: *MachO, start: u64) u64 {
+pub fn allocatedSize(self: *MachO, start: u64) u64 {
     if (start == 0) return 0;
     var min_pos: u64 = std.math.maxInt(u64);
     for (self.sections.items(.header)) |header| {
@@ -3264,12 +3264,19 @@ fn allocatedSize(self: *MachO, start: u64) u64 {
     return min_pos - start;
 }
 
-fn allocatedSizeVirtual(self: *MachO, start: u64) u64 {
+pub fn allocatedSizeVirtual(self: *MachO, start: u64) u64 {
     if (start == 0) return 0;
     var min_pos: u64 = std.math.maxInt(u64);
-    for (self.segments.items) |seg| {
-        if (seg.vmaddr <= start) continue;
-        if (seg.vmaddr < min_pos) min_pos = seg.vmaddr;
+    if (self.base.isRelocatable()) {
+        for (self.sections.items(.header)) |header| {
+            if (header.addr <= start) continue;
+            if (header.addr < min_pos) min_pos = header.addr;
+        }
+    } else {
+        for (self.segments.items) |seg| {
+            if (seg.vmaddr <= start) continue;
+            if (seg.vmaddr < min_pos) min_pos = seg.vmaddr;
+        }
     }
     return min_pos - start;
 }
@@ -3482,7 +3489,7 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
         }
     }
 
-    if (self.base.isRelocatable()) {
+    if (self.base.isRelocatable() and options.zo.dwarf != null) {
         {
             self.debug_str_sect_index = try self.addSection("__DWARF", "__debug_str", .{
                 .flags = macho.S_ATTR_DEBUG,