authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-02 16:22:54+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-02 16:22:58+01:00
log06a037bb9577e42c329a72082b3e44c75d1b2a7d
tree5ab2e0130b55d254ed5f9b32f7ad9575f0aaad11
parent380462ffc0366896de42b08d2517b7f6d631c6d4

macho: handle bss like normal section in stage2

This is just a temporary workaround until I work out how to manage non-physical sections between stage2 incremental updates.

1 files changed, 14 insertions(+), 9 deletions(-)

src/link/MachO.zig+14-9
...@@ -144,9 +144,6 @@ objc_data_section_index: ?u16 = null,...@@ -144,9 +144,6 @@ objc_data_section_index: ?u16 = null,
144rustc_section_index: ?u16 = null,144rustc_section_index: ?u16 = null,
145rustc_section_size: u64 = 0,145rustc_section_size: u64 = 0,
146146
147bss_file_offset: u32 = 0,
148tlv_bss_file_offset: u32 = 0,
149
150locals: std.ArrayListUnmanaged(macho.nlist_64) = .{},147locals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
151globals: std.ArrayListUnmanaged(macho.nlist_64) = .{},148globals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
152undefs: std.ArrayListUnmanaged(macho.nlist_64) = .{},149undefs: std.ArrayListUnmanaged(macho.nlist_64) = .{},
...@@ -383,7 +380,8 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*MachO {...@@ -383,7 +380,8 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*MachO {
383 // Adhoc code signature is required when targeting aarch64-macos either directly or indirectly via the simulator380 // Adhoc code signature is required when targeting aarch64-macos either directly or indirectly via the simulator
384 // ABI such as aarch64-ios-simulator, etc.381 // ABI such as aarch64-ios-simulator, etc.
385 const requires_adhoc_codesig = cpu_arch == .aarch64 and (os_tag == .macos or abi == .simulator);382 const requires_adhoc_codesig = cpu_arch == .aarch64 and (os_tag == .macos or abi == .simulator);
386 const needs_prealloc = !(build_options.is_stage1 and options.use_stage1);383 const use_stage1 = build_options.is_stage1 and options.use_stage1;
384 const needs_prealloc = !(use_stage1 or options.cache_mode == .whole);
387385
388 const self = try gpa.create(MachO);386 const self = try gpa.create(MachO);
389 errdefer gpa.destroy(self);387 errdefer gpa.destroy(self);
...@@ -401,7 +399,6 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*MachO {...@@ -401,7 +399,6 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*MachO {
401 };399 };
402400
403 const use_llvm = build_options.have_llvm and options.use_llvm;401 const use_llvm = build_options.have_llvm and options.use_llvm;
404 const use_stage1 = build_options.is_stage1 and options.use_stage1;
405 if (use_llvm and !use_stage1) {402 if (use_llvm and !use_stage1) {
406 self.llvm_object = try LlvmObject.create(gpa, options);403 self.llvm_object = try LlvmObject.create(gpa, options);
407 }404 }
...@@ -2158,7 +2155,8 @@ fn writeAtoms(self: *MachO) !void {...@@ -2158,7 +2155,8 @@ fn writeAtoms(self: *MachO) !void {
2158 const sect = seg.sections.items[match.sect];2155 const sect = seg.sections.items[match.sect];
2159 var atom: *Atom = entry.value_ptr.*;2156 var atom: *Atom = entry.value_ptr.*;
21602157
2161 if (sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL) continue;2158 // TODO handle zerofill in stage2
2159 // if (sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL) continue;
21622160
2163 log.debug("writing atoms in {s},{s}", .{ sect.segName(), sect.sectName() });2161 log.debug("writing atoms in {s},{s}", .{ sect.segName(), sect.sectName() });
21642162
...@@ -4756,9 +4754,12 @@ fn allocateSegment(self: *MachO, index: u16, offset: u64) !void {...@@ -4756,9 +4754,12 @@ fn allocateSegment(self: *MachO, index: u16, offset: u64) !void {
4756 var start: u64 = offset;4754 var start: u64 = offset;
4757 for (seg.sections.items) |*sect, sect_id| {4755 for (seg.sections.items) |*sect, sect_id| {
4758 const is_zerofill = sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL;4756 const is_zerofill = sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL;
4757 const use_stage1 = build_options.is_stage1 and self.base.options.use_stage1;
4759 const alignment = try math.powi(u32, 2, sect.@"align");4758 const alignment = try math.powi(u32, 2, sect.@"align");
4760 const start_aligned = mem.alignForwardGeneric(u64, start, alignment);4759 const start_aligned = mem.alignForwardGeneric(u64, start, alignment);
4761 sect.offset = if (is_zerofill) 0 else @intCast(u32, seg.inner.fileoff + start_aligned);4760
4761 // TODO handle zerofill sections in stage2
4762 sect.offset = if (is_zerofill and use_stage1) 0 else @intCast(u32, seg.inner.fileoff + start_aligned);
4762 sect.addr = seg.inner.vmaddr + start_aligned;4763 sect.addr = seg.inner.vmaddr + start_aligned;
47634764
4764 // Recalculate section size given the allocated start address4765 // Recalculate section size given the allocated start address
...@@ -4786,7 +4787,7 @@ fn allocateSegment(self: *MachO, index: u16, offset: u64) !void {...@@ -4786,7 +4787,7 @@ fn allocateSegment(self: *MachO, index: u16, offset: u64) !void {
47864787
4787 start = start_aligned + sect.size;4788 start = start_aligned + sect.size;
47884789
4789 if (!is_zerofill) {4790 if (!(is_zerofill and use_stage1)) {
4790 seg.inner.filesize = start;4791 seg.inner.filesize = start;
4791 }4792 }
4792 seg.inner.vmsize = start;4793 seg.inner.vmsize = start;
...@@ -4834,7 +4835,11 @@ fn initSection(...@@ -4834,7 +4835,11 @@ fn initSection(
48344835
4835 sect.addr = seg.inner.vmaddr + off - seg.inner.fileoff;4836 sect.addr = seg.inner.vmaddr + off - seg.inner.fileoff;
48364837
4837 if (opts.flags != macho.S_ZEROFILL and opts.flags != macho.S_THREAD_LOCAL_ZEROFILL) {4838 const is_zerofill = opts.flags == macho.S_ZEROFILL or opts.flags == macho.S_THREAD_LOCAL_ZEROFILL;
4839 const use_stage1 = build_options.is_stage1 and self.base.options.use_stage1;
4840
4841 // TODO handle zerofill in stage2
4842 if (!(is_zerofill and use_stage1)) {
4838 sect.offset = @intCast(u32, off);4843 sect.offset = @intCast(u32, off);
4839 }4844 }
4840 }4845 }