authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-24 15:19:28+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-24 15:19:28+02:00
log32ce8238a8f722a17a408bf9d15db4a74662ae71
tree0dd69341b4f54715b3361ad0265d1c3ae66c3ef6
parent8d300927045f3f2be3cc2eb6c665a7b17d81a655

macho: rewrite populateLazyBindOffsetsInStubHelper to use atoms

Instead of referencing stub indices since these can now be obtained in a more generic fashion from the actual linked-list of atoms in the __stub_helper section.

1 files changed, 21 insertions(+), 25 deletions(-)

src/link/MachO.zig+21-25
......@@ -766,8 +766,6 @@ pub fn flush(self: *MachO, comp: *Compilation) !void {
766766 try self.addCodeSignatureLC();
767767
768768 if (use_stage1) {
769 try self.parseTextBlocks();
770 try self.sortSections();
771769 {
772770 const atom = try self.createDyldPrivateAtom();
773771 try self.allocateAtomStage1(atom, .{
......@@ -789,6 +787,9 @@ pub fn flush(self: *MachO, comp: *Compilation) !void {
789787 sect.size -= atom.size;
790788 }
791789
790 try self.parseTextBlocks();
791 try self.sortSections();
792
792793 for (self.stubs.items) |_| {
793794 const stub_helper_atom = try self.createStubHelperAtom();
794795 try self.allocateAtomStage1(stub_helper_atom, .{
......@@ -5334,35 +5335,30 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void {
53345335 }
53355336 }
53365337
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) {
53435341 .x86_64 => 1,
53445342 .aarch64 => 2 * @sizeOf(u32),
53455343 else => unreachable,
53465344 };
53475345 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);
53485358
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;
53665362 }
53675363}
53685364