authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-03-16 23:07:23+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-03-17 12:10:39+01:00
log3df2ae1f9da3655462d668ec16088f43a4a11ce4
treeca2820bcce3768e7ef2f667e55884c42227d952c
parent643b4898f592c7193402dcf9a7ca465edb2d430a

macho: clean up writing of stub helper section


1 files changed, 12 insertions(+), 3 deletions(-)

src/link/MachO.zig+12-3
......@@ -2602,8 +2602,10 @@ fn writeStubInStubHelper(self: *MachO, index: u32) !void {
26022602 else => unreachable,
26032603 };
26042604 const stub_off = self.stub_helper_stubs_start_off.? + index * stub_size;
2605
26052606 var code = try self.base.allocator.alloc(u8, stub_size);
26062607 defer self.base.allocator.free(code);
2608
26072609 switch (self.base.options.target.cpu.arch) {
26082610 .x86_64 => {
26092611 const displacement = try math.cast(
......@@ -2618,12 +2620,19 @@ fn writeStubInStubHelper(self: *MachO, index: u32) !void {
26182620 mem.writeIntLittle(u32, code[6..][0..4], @bitCast(u32, displacement));
26192621 },
26202622 .aarch64 => {
2621 const displacement = try math.cast(i28, @intCast(i64, stub_helper.offset) - @intCast(i64, stub_off) - 4);
2623 const literal = blk: {
2624 const div_res = try math.divExact(u64, stub_size - @sizeOf(u32), 4);
2625 break :blk try math.cast(u18, div_res);
2626 };
2627 // ldr w16, literal
26222628 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.ldr(.w16, .{
2623 .literal = @divExact(stub_size - @sizeOf(u32), 4),
2629 .literal = literal,
26242630 }).toU32());
2631 const displacement = try math.cast(i28, @intCast(i64, stub_helper.offset) - @intCast(i64, stub_off) - 4);
2632 // b disp
26252633 mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.b(displacement).toU32());
2626 mem.writeIntLittle(u32, code[8..12], 0x0); // Just a placeholder populated in `populateLazyBindOffsetsInStubHelper`.
2634 // Just a placeholder populated in `populateLazyBindOffsetsInStubHelper`.
2635 mem.writeIntLittle(u32, code[8..12], 0x0);
26272636 },
26282637 else => unreachable,
26292638 }