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 {
10391039}
10401040
10411041fn allocateTextBlocks(self: *Zld) !void {
1042 log.debug("allocating text blocks", .{});
1043
10441042 var it = self.blocks.iterator();
10451043 while (it.next()) |entry| {
10461044 const match = entry.key_ptr.*;
......@@ -1103,8 +1101,6 @@ fn allocateTextBlocks(self: *Zld) !void {
11031101}
11041102
11051103fn writeTextBlocks(self: *Zld) !void {
1106 log.debug("writing text blocks", .{});
1107
11081104 var it = self.blocks.iterator();
11091105 while (it.next()) |entry| {
11101106 const match = entry.key_ptr.*;
src/link/MachO/reloc.zig+14-29
......@@ -48,18 +48,11 @@ pub const Relocation = struct {
4848 /// => * is unreachable
4949 is_64bit: bool,
5050
51 source_sect_addr: ?u64 = null,
52
5351 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
5952 const result = if (self.subtractor) |subtractor|
60 @intCast(i64, target_addr) - @intCast(i64, subtractor.payload.regular.address) + addend
53 @intCast(i64, target_addr) - @intCast(i64, subtractor.payload.regular.address) + self.addend
6154 else
62 @intCast(i64, target_addr) + addend;
55 @intCast(i64, target_addr) + self.addend;
6356
6457 if (self.is_64bit) {
6558 mem.writeIntLittle(u64, base.block.code[base.offset..][0..8], @bitCast(u64, result));
......@@ -344,10 +337,6 @@ pub const Relocation = struct {
344337 correction: i4,
345338
346339 pub fn resolve(self: Signed, base: Relocation, source_addr: u64, target_addr: u64) !void {
347 _ = self;
348 _ = base;
349 _ = source_addr;
350 _ = target_addr;
351340 // const target_addr = target_addr: {
352341 // if (signed.base.target == .section) {
353342 // 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 {
356345 // }
357346 // break :target_addr @intCast(i64, args.target_addr) + signed.addend;
358347 // };
359 // const displacement = try math.cast(
360 // i32,
361 // target_addr - @intCast(i64, args.source_addr) - signed.correction - 4,
362 // );
363
364 // log.debug(" | addend 0x{x}", .{signed.addend});
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));
348 const actual_target_addr = @intCast(i64, target_addr) + self.addend;
349 const displacement = try math.cast(
350 i32,
351 actual_target_addr - @intCast(i64, source_addr) - self.correction - 4,
352 );
353 mem.writeIntLittle(u32, base.block.code[base.offset..][0..4], @bitCast(u32, displacement));
369354 }
370355
371356 pub fn format(self: Signed, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
......@@ -759,21 +744,21 @@ pub const Parser = struct {
759744 2 => false,
760745 else => unreachable,
761746 };
762 const addend: i64 = if (is_64bit)
747
748 var addend: i64 = if (is_64bit)
763749 mem.readIntLittle(i64, self.block.code[parsed.offset..][0..8])
764750 else
765751 mem.readIntLittle(i32, self.block.code[parsed.offset..][0..4]);
766 const source_sect_addr = if (rel.r_extern == 0) blk: {
767 if (parsed.target.payload == .regular) break :blk parsed.target.payload.regular.address;
768 break :blk null;
769 } else null;
752
753 if (rel.r_extern == 0) {
754 addend -= @intCast(i64, parsed.target.payload.regular.address);
755 }
770756
771757 parsed.payload = .{
772758 .unsigned = .{
773759 .subtractor = self.subtractor,
774760 .is_64bit = is_64bit,
775761 .addend = addend,
776 .source_sect_addr = source_sect_addr,
777762 },
778763 };
779764