| ... | @@ -2602,8 +2602,10 @@ fn writeStubInStubHelper(self: *MachO, index: u32) !void { | ... | @@ -2602,8 +2602,10 @@ fn writeStubInStubHelper(self: *MachO, index: u32) !void { |
| 2602 | else => unreachable, | 2602 | else => unreachable, |
| 2603 | }; | 2603 | }; |
| 2604 | const stub_off = self.stub_helper_stubs_start_off.? + index * stub_size; | 2604 | const stub_off = self.stub_helper_stubs_start_off.? + index * stub_size; |
| | 2605 | |
| 2605 | var code = try self.base.allocator.alloc(u8, stub_size); | 2606 | var code = try self.base.allocator.alloc(u8, stub_size); |
| 2606 | defer self.base.allocator.free(code); | 2607 | defer self.base.allocator.free(code); |
| | 2608 | |
| 2607 | switch (self.base.options.target.cpu.arch) { | 2609 | switch (self.base.options.target.cpu.arch) { |
| 2608 | .x86_64 => { | 2610 | .x86_64 => { |
| 2609 | const displacement = try math.cast( | 2611 | const displacement = try math.cast( |
| ... | @@ -2618,12 +2620,19 @@ fn writeStubInStubHelper(self: *MachO, index: u32) !void { | ... | @@ -2618,12 +2620,19 @@ fn writeStubInStubHelper(self: *MachO, index: u32) !void { |
| 2618 | mem.writeIntLittle(u32, code[6..][0..4], @bitCast(u32, displacement)); | 2620 | mem.writeIntLittle(u32, code[6..][0..4], @bitCast(u32, displacement)); |
| 2619 | }, | 2621 | }, |
| 2620 | .aarch64 => { | 2622 | .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 |
| 2622 | mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.ldr(.w16, .{ | 2628 | mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.ldr(.w16, .{ |
| 2623 | .literal = @divExact(stub_size - @sizeOf(u32), 4), | 2629 | .literal = literal, |
| 2624 | }).toU32()); | 2630 | }).toU32()); |
| | 2631 | const displacement = try math.cast(i28, @intCast(i64, stub_helper.offset) - @intCast(i64, stub_off) - 4); |
| | 2632 | // b disp |
| 2625 | mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.b(displacement).toU32()); | 2633 | 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); |
| 2627 | }, | 2636 | }, |
| 2628 | else => unreachable, | 2637 | else => unreachable, |
| 2629 | } | 2638 | } |