authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-07-11 13:58:36+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-07-15 18:49:47+02:00
log2a880897b0f7466604058422a7e2fc9f401a4284
treeafdbd742bb04f17be648436f7e15c16e71967b09
parent570660bb4658823227d9cb227b1585eccff2af50

zld: add basic Signed reloc resolution

and fix handling Unsigned for x86_64.

2 files changed, 14 insertions(+), 33 deletions(-)

src/link/MachO/Zld.zig-4
...@@ -1039,8 +1039,6 @@ fn allocateSegment(self: *Zld, index: u16, offset: u64) !void {...@@ -1039,8 +1039,6 @@ fn allocateSegment(self: *Zld, index: u16, offset: u64) !void {
1039}1039}
10401040
1041fn allocateTextBlocks(self: *Zld) !void {1041fn allocateTextBlocks(self: *Zld) !void {
1042 log.debug("allocating text blocks", .{});
1043
1044 var it = self.blocks.iterator();1042 var it = self.blocks.iterator();
1045 while (it.next()) |entry| {1043 while (it.next()) |entry| {
1046 const match = entry.key_ptr.*;1044 const match = entry.key_ptr.*;
...@@ -1103,8 +1101,6 @@ fn allocateTextBlocks(self: *Zld) !void {...@@ -1103,8 +1101,6 @@ fn allocateTextBlocks(self: *Zld) !void {
1103}1101}
11041102
1105fn writeTextBlocks(self: *Zld) !void {1103fn writeTextBlocks(self: *Zld) !void {
1106 log.debug("writing text blocks", .{});
1107
1108 var it = self.blocks.iterator();1104 var it = self.blocks.iterator();
1109 while (it.next()) |entry| {1105 while (it.next()) |entry| {
1110 const match = entry.key_ptr.*;1106 const match = entry.key_ptr.*;
src/link/MachO/reloc.zig+14-29
...@@ -48,18 +48,11 @@ pub const Relocation = struct {...@@ -48,18 +48,11 @@ pub const Relocation = struct {
48 /// => * is unreachable48 /// => * is unreachable
49 is_64bit: bool,49 is_64bit: bool,
5050
51 source_sect_addr: ?u64 = null,
52
53 pub fn resolve(self: Unsigned, base: Relocation, _: u64, target_addr: u64) !void {51 pub fn resolve(self: Unsigned, base: Relocation, _: u64, target_addr: u64) !void {
54 const addend = if (self.source_sect_addr) |addr|
55 self.addend - @intCast(i64, addr)
56 else
57 self.addend;
58
59 const result = if (self.subtractor) |subtractor|52 const result = if (self.subtractor) |subtractor|
60 @intCast(i64, target_addr) - @intCast(i64, subtractor.payload.regular.address) + addend53 @intCast(i64, target_addr) - @intCast(i64, subtractor.payload.regular.address) + self.addend
61 else54 else
62 @intCast(i64, target_addr) + addend;55 @intCast(i64, target_addr) + self.addend;
6356
64 if (self.is_64bit) {57 if (self.is_64bit) {
65 mem.writeIntLittle(u64, base.block.code[base.offset..][0..8], @bitCast(u64, result));58 mem.writeIntLittle(u64, base.block.code[base.offset..][0..8], @bitCast(u64, result));
...@@ -344,10 +337,6 @@ pub const Relocation = struct {...@@ -344,10 +337,6 @@ pub const Relocation = struct {
344 correction: i4,337 correction: i4,
345338
346 pub fn resolve(self: Signed, base: Relocation, source_addr: u64, target_addr: u64) !void {339 pub fn resolve(self: Signed, base: Relocation, source_addr: u64, target_addr: u64) !void {
347 _ = self;
348 _ = base;
349 _ = source_addr;
350 _ = target_addr;
351 // const target_addr = target_addr: {340 // const target_addr = target_addr: {
352 // if (signed.base.target == .section) {341 // if (signed.base.target == .section) {
353 // const source_target = @intCast(i64, args.source_source_sect_addr.?) + @intCast(i64, signed.base.offset) + signed.addend + 4;342 // const source_target = @intCast(i64, args.source_source_sect_addr.?) + @intCast(i64, signed.base.offset) + signed.addend + 4;
...@@ -356,16 +345,12 @@ pub const Relocation = struct {...@@ -356,16 +345,12 @@ pub const Relocation = struct {
356 // }345 // }
357 // break :target_addr @intCast(i64, args.target_addr) + signed.addend;346 // break :target_addr @intCast(i64, args.target_addr) + signed.addend;
358 // };347 // };
359 // const displacement = try math.cast(348 const actual_target_addr = @intCast(i64, target_addr) + self.addend;
360 // i32,349 const displacement = try math.cast(
361 // target_addr - @intCast(i64, args.source_addr) - signed.correction - 4,350 i32,
362 // );351 actual_target_addr - @intCast(i64, source_addr) - self.correction - 4,
363352 );
364 // log.debug(" | addend 0x{x}", .{signed.addend});353 mem.writeIntLittle(u32, base.block.code[base.offset..][0..4], @bitCast(u32, displacement));
365 // log.debug(" | correction 0x{x}", .{signed.correction});
366 // log.debug(" | displacement 0x{x}", .{displacement});
367
368 // mem.writeIntLittle(u32, signed.base.code[0..4], @bitCast(u32, displacement));
369 }354 }
370355
371 pub fn format(self: Signed, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {356 pub fn format(self: Signed, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
...@@ -759,21 +744,21 @@ pub const Parser = struct {...@@ -759,21 +744,21 @@ pub const Parser = struct {
759 2 => false,744 2 => false,
760 else => unreachable,745 else => unreachable,
761 };746 };
762 const addend: i64 = if (is_64bit)747
748 var addend: i64 = if (is_64bit)
763 mem.readIntLittle(i64, self.block.code[parsed.offset..][0..8])749 mem.readIntLittle(i64, self.block.code[parsed.offset..][0..8])
764 else750 else
765 mem.readIntLittle(i32, self.block.code[parsed.offset..][0..4]);751 mem.readIntLittle(i32, self.block.code[parsed.offset..][0..4]);
766 const source_sect_addr = if (rel.r_extern == 0) blk: {752
767 if (parsed.target.payload == .regular) break :blk parsed.target.payload.regular.address;753 if (rel.r_extern == 0) {
768 break :blk null;754 addend -= @intCast(i64, parsed.target.payload.regular.address);
769 } else null;755 }
770756
771 parsed.payload = .{757 parsed.payload = .{
772 .unsigned = .{758 .unsigned = .{
773 .subtractor = self.subtractor,759 .subtractor = self.subtractor,
774 .is_64bit = is_64bit,760 .is_64bit = is_64bit,
775 .addend = addend,761 .addend = addend,
776 .source_sect_addr = source_sect_addr,
777 },762 },
778 };763 };
779764