| ... | ... | @@ -48,18 +48,11 @@ pub const Relocation = struct { |
| 48 | 48 | /// => * is unreachable |
| 49 | 49 | is_64bit: bool, |
| 50 | 50 | |
| 51 | | source_sect_addr: ?u64 = null, |
| 52 | | |
| 53 | 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 | 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 | 54 | else |
| 62 | | @intCast(i64, target_addr) + addend; |
| 55 | @intCast(i64, target_addr) + self.addend; |
| 63 | 56 | |
| 64 | 57 | if (self.is_64bit) { |
| 65 | 58 | mem.writeIntLittle(u64, base.block.code[base.offset..][0..8], @bitCast(u64, result)); |
| ... | ... | @@ -344,10 +337,6 @@ pub const Relocation = struct { |
| 344 | 337 | correction: i4, |
| 345 | 338 | |
| 346 | 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 | 340 | // const target_addr = target_addr: { |
| 352 | 341 | // if (signed.base.target == .section) { |
| 353 | 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 | 345 | // } |
| 357 | 346 | // break :target_addr @intCast(i64, args.target_addr) + signed.addend; |
| 358 | 347 | // }; |
| 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)); |
| 369 | 354 | } |
| 370 | 355 | |
| 371 | 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 | 744 | 2 => false, |
| 760 | 745 | else => unreachable, |
| 761 | 746 | }; |
| 762 | | const addend: i64 = if (is_64bit) |
| 747 | |
| 748 | var addend: i64 = if (is_64bit) |
| 763 | 749 | mem.readIntLittle(i64, self.block.code[parsed.offset..][0..8]) |
| 764 | 750 | else |
| 765 | 751 | 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 | } |
| 770 | 756 | |
| 771 | 757 | parsed.payload = .{ |
| 772 | 758 | .unsigned = .{ |
| 773 | 759 | .subtractor = self.subtractor, |
| 774 | 760 | .is_64bit = is_64bit, |
| 775 | 761 | .addend = addend, |
| 776 | | .source_sect_addr = source_sect_addr, |
| 777 | 762 | }, |
| 778 | 763 | }; |
| 779 | 764 | |