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");...@@ -16,7 +16,6 @@ const CodeSignature = @import("CodeSignature.zig");
16const Archive = @import("Archive.zig");16const Archive = @import("Archive.zig");
17const Object = @import("Object.zig");17const Object = @import("Object.zig");
18const Trie = @import("Trie.zig");18const Trie = @import("Trie.zig");
19const aarch64 = @import("../../codegen/aarch64.zig");
2019
21usingnamespace @import("commands.zig");20usingnamespace @import("commands.zig");
22usingnamespace @import("bind.zig");21usingnamespace @import("bind.zig");
...@@ -566,14 +565,14 @@ fn writeStubHelperCommon(self: *Zld) !void {...@@ -566,14 +565,14 @@ fn writeStubHelperCommon(self: *Zld) !void {
566 // adr x17, disp565 // adr x17, disp
567 mem.writeIntLittle(u32, code[0..4], Arm64.adr(17, @bitCast(u21, displacement)).toU32());566 mem.writeIntLittle(u32, code[0..4], Arm64.adr(17, @bitCast(u21, displacement)).toU32());
568 // nop567 // nop
569 mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.nop().toU32());568 mem.writeIntLittle(u32, code[4..8], Arm64.nop().toU32());
570 break :data_blk_outer;569 break :data_blk_outer;
571 }570 }
572 data_blk: {571 data_blk: {
573 const new_this_addr = this_addr + @sizeOf(u32);572 const new_this_addr = this_addr + @sizeOf(u32);
574 const displacement = math.cast(i21, target_addr - new_this_addr) catch |_| break :data_blk;573 const displacement = math.cast(i21, target_addr - new_this_addr) catch |_| break :data_blk;
575 // nop574 // nop
576 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.nop().toU32());575 mem.writeIntLittle(u32, code[0..4], Arm64.nop().toU32());
577 // adr x17, disp576 // adr x17, disp
578 mem.writeIntLittle(u32, code[4..8], Arm64.adr(17, @bitCast(u21, displacement)).toU32());577 mem.writeIntLittle(u32, code[4..8], Arm64.adr(17, @bitCast(u21, displacement)).toU32());
579 break :data_blk_outer;578 break :data_blk_outer;
...@@ -601,7 +600,7 @@ fn writeStubHelperCommon(self: *Zld) !void {...@@ -601,7 +600,7 @@ fn writeStubHelperCommon(self: *Zld) !void {
601 // ldr x16, label600 // ldr x16, label
602 mem.writeIntLittle(u32, code[12..16], Arm64.ldr(16, literal, 1).toU32());601 mem.writeIntLittle(u32, code[12..16], Arm64.ldr(16, literal, 1).toU32());
603 // nop602 // nop
604 mem.writeIntLittle(u32, code[16..20], aarch64.Instruction.nop().toU32());603 mem.writeIntLittle(u32, code[16..20], Arm64.nop().toU32());
605 break :binder_blk_outer;604 break :binder_blk_outer;
606 }605 }
607 binder_blk: {606 binder_blk: {
...@@ -611,7 +610,7 @@ fn writeStubHelperCommon(self: *Zld) !void {...@@ -611,7 +610,7 @@ fn writeStubHelperCommon(self: *Zld) !void {
611 log.warn("2: disp=0x{x}, literal=0x{x}", .{ displacement, literal });610 log.warn("2: disp=0x{x}, literal=0x{x}", .{ displacement, literal });
612 // Pad with nop to please division.611 // Pad with nop to please division.
613 // nop612 // nop
614 mem.writeIntLittle(u32, code[12..16], aarch64.Instruction.nop().toU32());613 mem.writeIntLittle(u32, code[12..16], Arm64.nop().toU32());
615 // ldr x16, label614 // ldr x16, label
616 mem.writeIntLittle(u32, code[16..20], Arm64.ldr(16, literal, 1).toU32());615 mem.writeIntLittle(u32, code[16..20], Arm64.ldr(16, literal, 1).toU32());
617 break :binder_blk_outer;616 break :binder_blk_outer;
...@@ -697,7 +696,7 @@ fn writeStub(self: *Zld, index: u32) !void {...@@ -697,7 +696,7 @@ fn writeStub(self: *Zld, index: u32) !void {
697 // ldr x16, literal696 // ldr x16, literal
698 mem.writeIntLittle(u32, code[0..4], Arm64.ldr(16, literal, 1).toU32());697 mem.writeIntLittle(u32, code[0..4], Arm64.ldr(16, literal, 1).toU32());
699 // nop698 // nop
700 mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.nop().toU32());699 mem.writeIntLittle(u32, code[4..8], Arm64.nop().toU32());
701 break :outer;700 break :outer;
702 }701 }
703 inner: {702 inner: {
...@@ -705,7 +704,7 @@ fn writeStub(self: *Zld, index: u32) !void {...@@ -705,7 +704,7 @@ fn writeStub(self: *Zld, index: u32) !void {
705 const displacement = math.divExact(u64, target_addr - new_this_addr, 4) catch |_| break :inner;704 const displacement = math.divExact(u64, target_addr - new_this_addr, 4) catch |_| break :inner;
706 const literal = math.cast(u18, displacement) catch |_| break :inner;705 const literal = math.cast(u18, displacement) catch |_| break :inner;
707 // nop706 // nop
708 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.nop().toU32());707 mem.writeIntLittle(u32, code[0..4], Arm64.nop().toU32());
709 // ldr x16, literal708 // ldr x16, literal
710 mem.writeIntLittle(u32, code[4..8], Arm64.ldr(16, literal, 1).toU32());709 mem.writeIntLittle(u32, code[4..8], Arm64.ldr(16, literal, 1).toU32());
711 break :outer;710 break :outer;
src/link/MachO/reloc.zig+10
...@@ -42,6 +42,9 @@ pub const Arm64 = union(enum) {...@@ -42,6 +42,9 @@ pub const Arm64 = union(enum) {
42 _1: u9 = 0b0_0_100010_0,42 _1: u9 = 0b0_0_100010_0,
43 size: u1,43 size: u1,
44 },44 },
45 Nop: packed struct {
46 fixed: u32 = 0b1101010100_0_00_011_0010_0000_000_11111,
47 },
4548
46 pub fn toU32(self: Arm64) u32 {49 pub fn toU32(self: Arm64) u32 {
47 const as_u32 = switch (self) {50 const as_u32 = switch (self) {
...@@ -51,6 +54,7 @@ pub const Arm64 = union(enum) {...@@ -51,6 +54,7 @@ pub const Arm64 = union(enum) {
51 .LoadRegister => |x| @bitCast(u32, x),54 .LoadRegister => |x| @bitCast(u32, x),
52 .LoadLiteral => |x| @bitCast(u32, x),55 .LoadLiteral => |x| @bitCast(u32, x),
53 .Add => |x| @bitCast(u32, x),56 .Add => |x| @bitCast(u32, x),
57 .Nop => |x| @bitCast(u32, x),
54 };58 };
55 return as_u32;59 return as_u32;
56 }60 }
...@@ -165,6 +169,12 @@ pub const Arm64 = union(enum) {...@@ -165,6 +169,12 @@ pub const Arm64 = union(enum) {
165 };169 };
166 }170 }
167171
172 pub fn nop() Arm64 {
173 return Arm64{
174 .Nop = .{},
175 };
176 }
177
168 pub fn isArithmetic(inst: *const [4]u8) bool {178 pub fn isArithmetic(inst: *const [4]u8) bool {
169 const group_decode = @truncate(u5, inst[3]);179 const group_decode = @truncate(u5, inst[3]);
170 log.debug("{b}", .{group_decode});180 log.debug("{b}", .{group_decode});