| ... | @@ -48,18 +48,11 @@ pub const Relocation = struct { | ... | @@ -48,18 +48,11 @@ pub const Relocation = struct { |
| 48 | /// => * is unreachable | 48 | /// => * is unreachable |
| 49 | is_64bit: bool, | 49 | is_64bit: bool, |
| 50 | | 50 | |
| 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) + addend | 53 | @intCast(i64, target_addr) - @intCast(i64, subtractor.payload.regular.address) + self.addend |
| 61 | else | 54 | else |
| 62 | @intCast(i64, target_addr) + addend; | 55 | @intCast(i64, target_addr) + self.addend; |
| 63 | | 56 | |
| 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, |
| 345 | | 338 | |
| 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, |
| 363 | | 352 | ); |
| 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 | } |
| 370 | | 355 | |
| 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 | else | 750 | 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 | } |
| 770 | | 756 | |
| 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 | }; |
| 779 | | 764 | |