authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-02-27 21:32:03+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-03-17 19:59:13+01:00
log7e329478718ef5545083b1123d1df437101b8a5b
tree336c7db675bf94cccb56ee0250ffb147ffd240f8
parentd2008db6235f2544a1aa3dbd419db911bc858cbd

zld: add nop to reloc module


2 files changed, 16 insertions(+), 7 deletions(-)

src/link/MachO/Zld.zig+6-7
......@@ -16,7 +16,6 @@ const CodeSignature = @import("CodeSignature.zig");
1616const Archive = @import("Archive.zig");
1717const Object = @import("Object.zig");
1818const Trie = @import("Trie.zig");
19const aarch64 = @import("../../codegen/aarch64.zig");
2019
2120usingnamespace @import("commands.zig");
2221usingnamespace @import("bind.zig");
......@@ -566,14 +565,14 @@ fn writeStubHelperCommon(self: *Zld) !void {
566565 // adr x17, disp
567566 mem.writeIntLittle(u32, code[0..4], Arm64.adr(17, @bitCast(u21, displacement)).toU32());
568567 // nop
569 mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.nop().toU32());
568 mem.writeIntLittle(u32, code[4..8], Arm64.nop().toU32());
570569 break :data_blk_outer;
571570 }
572571 data_blk: {
573572 const new_this_addr = this_addr + @sizeOf(u32);
574573 const displacement = math.cast(i21, target_addr - new_this_addr) catch |_| break :data_blk;
575574 // nop
576 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.nop().toU32());
575 mem.writeIntLittle(u32, code[0..4], Arm64.nop().toU32());
577576 // adr x17, disp
578577 mem.writeIntLittle(u32, code[4..8], Arm64.adr(17, @bitCast(u21, displacement)).toU32());
579578 break :data_blk_outer;
......@@ -601,7 +600,7 @@ fn writeStubHelperCommon(self: *Zld) !void {
601600 // ldr x16, label
602601 mem.writeIntLittle(u32, code[12..16], Arm64.ldr(16, literal, 1).toU32());
603602 // nop
604 mem.writeIntLittle(u32, code[16..20], aarch64.Instruction.nop().toU32());
603 mem.writeIntLittle(u32, code[16..20], Arm64.nop().toU32());
605604 break :binder_blk_outer;
606605 }
607606 binder_blk: {
......@@ -611,7 +610,7 @@ fn writeStubHelperCommon(self: *Zld) !void {
611610 log.warn("2: disp=0x{x}, literal=0x{x}", .{ displacement, literal });
612611 // Pad with nop to please division.
613612 // nop
614 mem.writeIntLittle(u32, code[12..16], aarch64.Instruction.nop().toU32());
613 mem.writeIntLittle(u32, code[12..16], Arm64.nop().toU32());
615614 // ldr x16, label
616615 mem.writeIntLittle(u32, code[16..20], Arm64.ldr(16, literal, 1).toU32());
617616 break :binder_blk_outer;
......@@ -697,7 +696,7 @@ fn writeStub(self: *Zld, index: u32) !void {
697696 // ldr x16, literal
698697 mem.writeIntLittle(u32, code[0..4], Arm64.ldr(16, literal, 1).toU32());
699698 // nop
700 mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.nop().toU32());
699 mem.writeIntLittle(u32, code[4..8], Arm64.nop().toU32());
701700 break :outer;
702701 }
703702 inner: {
......@@ -705,7 +704,7 @@ fn writeStub(self: *Zld, index: u32) !void {
705704 const displacement = math.divExact(u64, target_addr - new_this_addr, 4) catch |_| break :inner;
706705 const literal = math.cast(u18, displacement) catch |_| break :inner;
707706 // nop
708 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.nop().toU32());
707 mem.writeIntLittle(u32, code[0..4], Arm64.nop().toU32());
709708 // ldr x16, literal
710709 mem.writeIntLittle(u32, code[4..8], Arm64.ldr(16, literal, 1).toU32());
711710 break :outer;
src/link/MachO/reloc.zig+10
......@@ -42,6 +42,9 @@ pub const Arm64 = union(enum) {
4242 _1: u9 = 0b0_0_100010_0,
4343 size: u1,
4444 },
45 Nop: packed struct {
46 fixed: u32 = 0b1101010100_0_00_011_0010_0000_000_11111,
47 },
4548
4649 pub fn toU32(self: Arm64) u32 {
4750 const as_u32 = switch (self) {
......@@ -51,6 +54,7 @@ pub const Arm64 = union(enum) {
5154 .LoadRegister => |x| @bitCast(u32, x),
5255 .LoadLiteral => |x| @bitCast(u32, x),
5356 .Add => |x| @bitCast(u32, x),
57 .Nop => |x| @bitCast(u32, x),
5458 };
5559 return as_u32;
5660 }
......@@ -165,6 +169,12 @@ pub const Arm64 = union(enum) {
165169 };
166170 }
167171
172 pub fn nop() Arm64 {
173 return Arm64{
174 .Nop = .{},
175 };
176 }
177
168178 pub fn isArithmetic(inst: *const [4]u8) bool {
169179 const group_decode = @truncate(u5, inst[3]);
170180 log.debug("{b}", .{group_decode});