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,
144144rustc_section_index: ?u16 = null,
145145rustc_section_size: u64 = 0,
146146
147bss_file_offset: u32 = 0,
148tlv_bss_file_offset: u32 = 0,
149
150147locals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
151148globals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
152149undefs: std.ArrayListUnmanaged(macho.nlist_64) = .{},
......@@ -383,7 +380,8 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*MachO {
383380 // Adhoc code signature is required when targeting aarch64-macos either directly or indirectly via the simulator
384381 // ABI such as aarch64-ios-simulator, etc.
385382 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
388386 const self = try gpa.create(MachO);
389387 errdefer gpa.destroy(self);
......@@ -401,7 +399,6 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*MachO {
401399 };
402400
403401 const use_llvm = build_options.have_llvm and options.use_llvm;
404 const use_stage1 = build_options.is_stage1 and options.use_stage1;
405402 if (use_llvm and !use_stage1) {
406403 self.llvm_object = try LlvmObject.create(gpa, options);
407404 }
......@@ -2158,7 +2155,8 @@ fn writeAtoms(self: *MachO) !void {
21582155 const sect = seg.sections.items[match.sect];
21592156 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
21632161 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 {
47564754 var start: u64 = offset;
47574755 for (seg.sections.items) |*sect, sect_id| {
47584756 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;
47594758 const alignment = try math.powi(u32, 2, sect.@"align");
47604759 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);
47624763 sect.addr = seg.inner.vmaddr + start_aligned;
47634764
47644765 // Recalculate section size given the allocated start address
......@@ -4786,7 +4787,7 @@ fn allocateSegment(self: *MachO, index: u16, offset: u64) !void {
47864787
47874788 start = start_aligned + sect.size;
47884789
4789 if (!is_zerofill) {
4790 if (!(is_zerofill and use_stage1)) {
47904791 seg.inner.filesize = start;
47914792 }
47924793 seg.inner.vmsize = start;
......@@ -4834,7 +4835,11 @@ fn initSection(
48344835
48354836 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)) {
48384843 sect.offset = @intCast(u32, off);
48394844 }
48404845 }