authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-08 13:12:06+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-08 13:12:06+01:00
log102846315c1be4518ce7783717dc1ceb133b66cf
tree51d6fdf2d20240e3114e33ae672d4743c178dc6e
parentce207caa24dc3a283288beb7ee5fd4a07c2c8691

macho: couple small fixes


3 files changed, 29 insertions(+), 13 deletions(-)

src/link/MachO.zig+15-8
......@@ -3205,7 +3205,7 @@ fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 {
32053205 for (self.sections.items(.header)) |header| {
32063206 if (header.isZerofill()) continue;
32073207 const increased_size = padToIdeal(header.size);
3208 const test_end = header.offset + increased_size;
3208 const test_end = header.offset +| increased_size;
32093209 if (end > header.offset and start < test_end) {
32103210 return test_end;
32113211 }
......@@ -3233,7 +3233,7 @@ fn detectAllocCollisionVirtual(self: *MachO, start: u64, size: u64) ?u64 {
32333233
32343234 for (self.sections.items(.header)) |header| {
32353235 const increased_size = padToIdeal(header.size);
3236 const test_end = header.addr + increased_size;
3236 const test_end = header.addr +| increased_size;
32373237 if (end > header.addr and start < test_end) {
32383238 return test_end;
32393239 }
......@@ -3250,7 +3250,7 @@ fn detectAllocCollisionVirtual(self: *MachO, start: u64, size: u64) ?u64 {
32503250 return null;
32513251}
32523252
3253fn allocatedSize(self: *MachO, start: u64) u64 {
3253pub fn allocatedSize(self: *MachO, start: u64) u64 {
32543254 if (start == 0) return 0;
32553255 var min_pos: u64 = std.math.maxInt(u64);
32563256 for (self.sections.items(.header)) |header| {
......@@ -3264,12 +3264,19 @@ fn allocatedSize(self: *MachO, start: u64) u64 {
32643264 return min_pos - start;
32653265}
32663266
3267fn allocatedSizeVirtual(self: *MachO, start: u64) u64 {
3267pub fn allocatedSizeVirtual(self: *MachO, start: u64) u64 {
32683268 if (start == 0) return 0;
32693269 var min_pos: u64 = std.math.maxInt(u64);
3270 for (self.segments.items) |seg| {
3271 if (seg.vmaddr <= start) continue;
3272 if (seg.vmaddr < min_pos) min_pos = seg.vmaddr;
3270 if (self.base.isRelocatable()) {
3271 for (self.sections.items(.header)) |header| {
3272 if (header.addr <= start) continue;
3273 if (header.addr < min_pos) min_pos = header.addr;
3274 }
3275 } else {
3276 for (self.segments.items) |seg| {
3277 if (seg.vmaddr <= start) continue;
3278 if (seg.vmaddr < min_pos) min_pos = seg.vmaddr;
3279 }
32733280 }
32743281 return min_pos - start;
32753282}
......@@ -3482,7 +3489,7 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
34823489 }
34833490 }
34843491
3485 if (self.base.isRelocatable()) {
3492 if (self.base.isRelocatable() and options.zo.dwarf != null) {
34863493 {
34873494 self.debug_str_sect_index = try self.addSection("__DWARF", "__debug_str", .{
34883495 .flags = macho.S_ATTR_DEBUG,
src/link/MachO/ZigObject.zig+10-5
......@@ -52,11 +52,11 @@ dynamic_relocs: MachO.DynamicRelocs = .{},
5252output_symtab_ctx: MachO.SymtabCtx = .{},
5353output_ar_state: Archive.ArState = .{},
5454
55debug_strtab_dirty: bool = true,
56debug_abbrev_dirty: bool = true,
57debug_aranges_dirty: bool = true,
58debug_info_header_dirty: bool = true,
59debug_line_header_dirty: bool = true,
55debug_strtab_dirty: bool = false,
56debug_abbrev_dirty: bool = false,
57debug_aranges_dirty: bool = false,
58debug_info_header_dirty: bool = false,
59debug_line_header_dirty: bool = false,
6060
6161pub fn init(self: *ZigObject, macho_file: *MachO) !void {
6262 const comp = macho_file.base.comp;
......@@ -70,6 +70,11 @@ pub fn init(self: *ZigObject, macho_file: *MachO) !void {
7070 .dwarf => |v| {
7171 assert(v == .@"32");
7272 self.dwarf = Dwarf.init(&macho_file.base, .dwarf32);
73 self.debug_strtab_dirty = true;
74 self.debug_abbrev_dirty = true;
75 self.debug_aranges_dirty = true;
76 self.debug_info_header_dirty = true;
77 self.debug_line_header_dirty = true;
7378 },
7479 .code_view => unreachable,
7580 }
src/link/MachO/relocatable.zig+4
......@@ -403,6 +403,7 @@ fn calcSectionSizes(macho_file: *MachO) !void {
403403 if (!atom.flags.alive) continue;
404404 const header = &macho_file.sections.items(.header)[atom.out_n_sect];
405405 if (!macho_file.isZigSection(atom.out_n_sect)) continue;
406 if (!macho_file.isDebugSection(atom.out_n_sect)) continue;
406407 header.nreloc += atom.calcNumRelocs(macho_file);
407408 }
408409 }
......@@ -540,6 +541,7 @@ fn writeAtoms(macho_file: *MachO) !void {
540541 if (atoms.items.len == 0) continue;
541542 if (header.isZerofill()) continue;
542543 if (macho_file.isZigSection(@intCast(i))) continue;
544 if (macho_file.isDebugSection(@intCast(i))) continue;
543545
544546 const size = math.cast(usize, header.size) orelse return error.Overflow;
545547 const code = try gpa.alloc(u8, size);
......@@ -581,6 +583,7 @@ fn writeAtoms(macho_file: *MachO) !void {
581583 for (macho_file.sections.items(.header), 0..) |header, n_sect| {
582584 if (header.isZerofill()) continue;
583585 if (!macho_file.isZigSection(@intCast(n_sect))) continue;
586 if (!macho_file.isDebugSection(@intCast(n_sect))) continue;
584587 const gop = try relocs.getOrPut(@intCast(n_sect));
585588 if (gop.found_existing) continue;
586589 gop.value_ptr.* = try std.ArrayList(macho.relocation_info).initCapacity(gpa, header.nreloc);
......@@ -592,6 +595,7 @@ fn writeAtoms(macho_file: *MachO) !void {
592595 const header = macho_file.sections.items(.header)[atom.out_n_sect];
593596 if (header.isZerofill()) continue;
594597 if (!macho_file.isZigSection(atom.out_n_sect)) continue;
598 if (!macho_file.isDebugSection(atom.out_n_sect)) continue;
595599 if (atom.getRelocs(macho_file).len == 0) continue;
596600 const atom_size = math.cast(usize, atom.size) orelse return error.Overflow;
597601 const code = try gpa.alloc(u8, atom_size);