| ... | ... | @@ -766,8 +766,6 @@ pub fn flush(self: *MachO, comp: *Compilation) !void { |
| 766 | 766 | try self.addCodeSignatureLC(); |
| 767 | 767 | |
| 768 | 768 | if (use_stage1) { |
| 769 | | try self.parseTextBlocks(); |
| 770 | | try self.sortSections(); |
| 771 | 769 | { |
| 772 | 770 | const atom = try self.createDyldPrivateAtom(); |
| 773 | 771 | try self.allocateAtomStage1(atom, .{ |
| ... | ... | @@ -789,6 +787,9 @@ pub fn flush(self: *MachO, comp: *Compilation) !void { |
| 789 | 787 | sect.size -= atom.size; |
| 790 | 788 | } |
| 791 | 789 | |
| 790 | try self.parseTextBlocks(); |
| 791 | try self.sortSections(); |
| 792 | |
| 792 | 793 | for (self.stubs.items) |_| { |
| 793 | 794 | const stub_helper_atom = try self.createStubHelperAtom(); |
| 794 | 795 | try self.allocateAtomStage1(stub_helper_atom, .{ |
| ... | ... | @@ -5334,35 +5335,30 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { |
| 5334 | 5335 | } |
| 5335 | 5336 | } |
| 5336 | 5337 | |
| 5337 | | const stub_size: u4 = switch (self.base.options.target.cpu.arch) { |
| 5338 | | .x86_64 => 10, |
| 5339 | | .aarch64 => 3 * @sizeOf(u32), |
| 5340 | | else => unreachable, |
| 5341 | | }; |
| 5342 | | const off: u4 = switch (self.base.options.target.cpu.arch) { |
| 5338 | const seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 5339 | const sect = seg.sections.items[self.stub_helper_section_index.?]; |
| 5340 | const stub_offset: u4 = switch (self.base.options.target.cpu.arch) { |
| 5343 | 5341 | .x86_64 => 1, |
| 5344 | 5342 | .aarch64 => 2 * @sizeOf(u32), |
| 5345 | 5343 | else => unreachable, |
| 5346 | 5344 | }; |
| 5347 | 5345 | var buf: [@sizeOf(u32)]u8 = undefined; |
| 5346 | var atom = last_atom; |
| 5347 | _ = offsets.pop(); |
| 5348 | while (offsets.popOrNull()) |bind_offset| { |
| 5349 | const sym = self.locals.items[atom.local_sym_index]; |
| 5350 | const file_offset = sect.offset + sym.n_value - sect.addr + stub_offset; |
| 5351 | mem.writeIntLittle(u32, &buf, bind_offset); |
| 5352 | log.debug("writing lazy binding offset in stub helper of 0x{x} for symbol {s} at offset 0x{x}", .{ |
| 5353 | bind_offset, |
| 5354 | self.getString(sym.n_strx), |
| 5355 | file_offset, |
| 5356 | }); |
| 5357 | try self.base.file.?.pwriteAll(&buf, file_offset); |
| 5348 | 5358 | |
| 5349 | | var first_atom = last_atom; |
| 5350 | | while (first_atom.prev) |prev| { |
| 5351 | | first_atom = prev; |
| 5352 | | } |
| 5353 | | |
| 5354 | | const start_off = blk: { |
| 5355 | | const seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 5356 | | const sect = seg.sections.items[self.stub_helper_section_index.?]; |
| 5357 | | const sym = self.locals.items[first_atom.next.?.local_sym_index]; |
| 5358 | | break :blk sym.n_value - sect.addr + sect.offset; |
| 5359 | | }; |
| 5360 | | log.warn("start_off = 0x{x}", .{start_off}); |
| 5361 | | |
| 5362 | | for (self.stubs.items) |_, index| { |
| 5363 | | const placeholder_off = start_off + index * stub_size + off; |
| 5364 | | mem.writeIntLittle(u32, &buf, offsets.items[index]); |
| 5365 | | try self.base.file.?.pwriteAll(&buf, placeholder_off); |
| 5359 | if (atom.prev) |prev| { |
| 5360 | atom = prev; |
| 5361 | } else break; |
| 5366 | 5362 | } |
| 5367 | 5363 | } |
| 5368 | 5364 | |