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 {...@@ -3205,7 +3205,7 @@ fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 {
3205 for (self.sections.items(.header)) |header| {3205 for (self.sections.items(.header)) |header| {
3206 if (header.isZerofill()) continue;3206 if (header.isZerofill()) continue;
3207 const increased_size = padToIdeal(header.size);3207 const increased_size = padToIdeal(header.size);
3208 const test_end = header.offset + increased_size;3208 const test_end = header.offset +| increased_size;
3209 if (end > header.offset and start < test_end) {3209 if (end > header.offset and start < test_end) {
3210 return test_end;3210 return test_end;
3211 }3211 }
...@@ -3233,7 +3233,7 @@ fn detectAllocCollisionVirtual(self: *MachO, start: u64, size: u64) ?u64 {...@@ -3233,7 +3233,7 @@ fn detectAllocCollisionVirtual(self: *MachO, start: u64, size: u64) ?u64 {
32333233
3234 for (self.sections.items(.header)) |header| {3234 for (self.sections.items(.header)) |header| {
3235 const increased_size = padToIdeal(header.size);3235 const increased_size = padToIdeal(header.size);
3236 const test_end = header.addr + increased_size;3236 const test_end = header.addr +| increased_size;
3237 if (end > header.addr and start < test_end) {3237 if (end > header.addr and start < test_end) {
3238 return test_end;3238 return test_end;
3239 }3239 }
...@@ -3250,7 +3250,7 @@ fn detectAllocCollisionVirtual(self: *MachO, start: u64, size: u64) ?u64 {...@@ -3250,7 +3250,7 @@ fn detectAllocCollisionVirtual(self: *MachO, start: u64, size: u64) ?u64 {
3250 return null;3250 return null;
3251}3251}
32523252
3253fn allocatedSize(self: *MachO, start: u64) u64 {3253pub fn allocatedSize(self: *MachO, start: u64) u64 {
3254 if (start == 0) return 0;3254 if (start == 0) return 0;
3255 var min_pos: u64 = std.math.maxInt(u64);3255 var min_pos: u64 = std.math.maxInt(u64);
3256 for (self.sections.items(.header)) |header| {3256 for (self.sections.items(.header)) |header| {
...@@ -3264,12 +3264,19 @@ fn allocatedSize(self: *MachO, start: u64) u64 {...@@ -3264,12 +3264,19 @@ fn allocatedSize(self: *MachO, start: u64) u64 {
3264 return min_pos - start;3264 return min_pos - start;
3265}3265}
32663266
3267fn allocatedSizeVirtual(self: *MachO, start: u64) u64 {3267pub fn allocatedSizeVirtual(self: *MachO, start: u64) u64 {
3268 if (start == 0) return 0;3268 if (start == 0) return 0;
3269 var min_pos: u64 = std.math.maxInt(u64);3269 var min_pos: u64 = std.math.maxInt(u64);
3270 for (self.segments.items) |seg| {3270 if (self.base.isRelocatable()) {
3271 if (seg.vmaddr <= start) continue;3271 for (self.sections.items(.header)) |header| {
3272 if (seg.vmaddr < min_pos) min_pos = seg.vmaddr;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 }
3273 }3280 }
3274 return min_pos - start;3281 return min_pos - start;
3275}3282}
...@@ -3482,7 +3489,7 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {...@@ -3482,7 +3489,7 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
3482 }3489 }
3483 }3490 }
34843491
3485 if (self.base.isRelocatable()) {3492 if (self.base.isRelocatable() and options.zo.dwarf != null) {
3486 {3493 {
3487 self.debug_str_sect_index = try self.addSection("__DWARF", "__debug_str", .{3494 self.debug_str_sect_index = try self.addSection("__DWARF", "__debug_str", .{
3488 .flags = macho.S_ATTR_DEBUG,3495 .flags = macho.S_ATTR_DEBUG,
src/link/MachO/ZigObject.zig+10-5
...@@ -52,11 +52,11 @@ dynamic_relocs: MachO.DynamicRelocs = .{},...@@ -52,11 +52,11 @@ dynamic_relocs: MachO.DynamicRelocs = .{},
52output_symtab_ctx: MachO.SymtabCtx = .{},52output_symtab_ctx: MachO.SymtabCtx = .{},
53output_ar_state: Archive.ArState = .{},53output_ar_state: Archive.ArState = .{},
5454
55debug_strtab_dirty: bool = true,55debug_strtab_dirty: bool = false,
56debug_abbrev_dirty: bool = true,56debug_abbrev_dirty: bool = false,
57debug_aranges_dirty: bool = true,57debug_aranges_dirty: bool = false,
58debug_info_header_dirty: bool = true,58debug_info_header_dirty: bool = false,
59debug_line_header_dirty: bool = true,59debug_line_header_dirty: bool = false,
6060
61pub fn init(self: *ZigObject, macho_file: *MachO) !void {61pub fn init(self: *ZigObject, macho_file: *MachO) !void {
62 const comp = macho_file.base.comp;62 const comp = macho_file.base.comp;
...@@ -70,6 +70,11 @@ pub fn init(self: *ZigObject, macho_file: *MachO) !void {...@@ -70,6 +70,11 @@ pub fn init(self: *ZigObject, macho_file: *MachO) !void {
70 .dwarf => |v| {70 .dwarf => |v| {
71 assert(v == .@"32");71 assert(v == .@"32");
72 self.dwarf = Dwarf.init(&macho_file.base, .dwarf32);72 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;
73 },78 },
74 .code_view => unreachable,79 .code_view => unreachable,
75 }80 }
src/link/MachO/relocatable.zig+4
...@@ -403,6 +403,7 @@ fn calcSectionSizes(macho_file: *MachO) !void {...@@ -403,6 +403,7 @@ fn calcSectionSizes(macho_file: *MachO) !void {
403 if (!atom.flags.alive) continue;403 if (!atom.flags.alive) continue;
404 const header = &macho_file.sections.items(.header)[atom.out_n_sect];404 const header = &macho_file.sections.items(.header)[atom.out_n_sect];
405 if (!macho_file.isZigSection(atom.out_n_sect)) continue;405 if (!macho_file.isZigSection(atom.out_n_sect)) continue;
406 if (!macho_file.isDebugSection(atom.out_n_sect)) continue;
406 header.nreloc += atom.calcNumRelocs(macho_file);407 header.nreloc += atom.calcNumRelocs(macho_file);
407 }408 }
408 }409 }
...@@ -540,6 +541,7 @@ fn writeAtoms(macho_file: *MachO) !void {...@@ -540,6 +541,7 @@ fn writeAtoms(macho_file: *MachO) !void {
540 if (atoms.items.len == 0) continue;541 if (atoms.items.len == 0) continue;
541 if (header.isZerofill()) continue;542 if (header.isZerofill()) continue;
542 if (macho_file.isZigSection(@intCast(i))) continue;543 if (macho_file.isZigSection(@intCast(i))) continue;
544 if (macho_file.isDebugSection(@intCast(i))) continue;
543545
544 const size = math.cast(usize, header.size) orelse return error.Overflow;546 const size = math.cast(usize, header.size) orelse return error.Overflow;
545 const code = try gpa.alloc(u8, size);547 const code = try gpa.alloc(u8, size);
...@@ -581,6 +583,7 @@ fn writeAtoms(macho_file: *MachO) !void {...@@ -581,6 +583,7 @@ fn writeAtoms(macho_file: *MachO) !void {
581 for (macho_file.sections.items(.header), 0..) |header, n_sect| {583 for (macho_file.sections.items(.header), 0..) |header, n_sect| {
582 if (header.isZerofill()) continue;584 if (header.isZerofill()) continue;
583 if (!macho_file.isZigSection(@intCast(n_sect))) continue;585 if (!macho_file.isZigSection(@intCast(n_sect))) continue;
586 if (!macho_file.isDebugSection(@intCast(n_sect))) continue;
584 const gop = try relocs.getOrPut(@intCast(n_sect));587 const gop = try relocs.getOrPut(@intCast(n_sect));
585 if (gop.found_existing) continue;588 if (gop.found_existing) continue;
586 gop.value_ptr.* = try std.ArrayList(macho.relocation_info).initCapacity(gpa, header.nreloc);589 gop.value_ptr.* = try std.ArrayList(macho.relocation_info).initCapacity(gpa, header.nreloc);
...@@ -592,6 +595,7 @@ fn writeAtoms(macho_file: *MachO) !void {...@@ -592,6 +595,7 @@ fn writeAtoms(macho_file: *MachO) !void {
592 const header = macho_file.sections.items(.header)[atom.out_n_sect];595 const header = macho_file.sections.items(.header)[atom.out_n_sect];
593 if (header.isZerofill()) continue;596 if (header.isZerofill()) continue;
594 if (!macho_file.isZigSection(atom.out_n_sect)) continue;597 if (!macho_file.isZigSection(atom.out_n_sect)) continue;
598 if (!macho_file.isDebugSection(atom.out_n_sect)) continue;
595 if (atom.getRelocs(macho_file).len == 0) continue;599 if (atom.getRelocs(macho_file).len == 0) continue;
596 const atom_size = math.cast(usize, atom.size) orelse return error.Overflow;600 const atom_size = math.cast(usize, atom.size) orelse return error.Overflow;
597 const code = try gpa.alloc(u8, atom_size);601 const code = try gpa.alloc(u8, atom_size);