| ... | ... | @@ -479,13 +479,13 @@ pub const Relocation = struct { |
| 479 | 479 | |
| 480 | 480 | pub const Signed = struct { |
| 481 | 481 | addend: i64, |
| 482 | | correction: i4, |
| 482 | correction: u3, |
| 483 | 483 | |
| 484 | 484 | pub fn resolve(self: Signed, args: ResolveArgs) !void { |
| 485 | 485 | const target_addr = @intCast(i64, args.target_addr) + self.addend; |
| 486 | 486 | const displacement = try math.cast( |
| 487 | 487 | i32, |
| 488 | | target_addr - @intCast(i64, args.source_addr) - self.correction - 4, |
| 488 | target_addr - @intCast(i64, args.source_addr + self.correction + 4), |
| 489 | 489 | ); |
| 490 | 490 | mem.writeIntLittle(u32, args.block.code.items[args.offset..][0..4], @bitCast(u32, displacement)); |
| 491 | 491 | } |
| ... | ... | @@ -613,6 +613,7 @@ pub fn freeListEligible(self: TextBlock, macho_file: MachO) bool { |
| 613 | 613 | |
| 614 | 614 | const RelocContext = struct { |
| 615 | 615 | base_addr: u64 = 0, |
| 616 | base_offset: u64 = 0, |
| 616 | 617 | allocator: *Allocator, |
| 617 | 618 | object: *Object, |
| 618 | 619 | macho_file: *MachO, |
| ... | ... | @@ -620,7 +621,7 @@ const RelocContext = struct { |
| 620 | 621 | |
| 621 | 622 | fn initRelocFromObject(rel: macho.relocation_info, context: RelocContext) !Relocation { |
| 622 | 623 | var parsed_rel = Relocation{ |
| 623 | | .offset = @intCast(u32, @intCast(u64, rel.r_address) - context.base_addr), |
| 624 | .offset = @intCast(u32, @intCast(u64, rel.r_address) - context.base_offset), |
| 624 | 625 | .where = undefined, |
| 625 | 626 | .where_index = undefined, |
| 626 | 627 | .payload = undefined, |
| ... | ... | @@ -684,7 +685,7 @@ fn initRelocFromObject(rel: macho.relocation_info, context: RelocContext) !Reloc |
| 684 | 685 | } |
| 685 | 686 | |
| 686 | 687 | pub fn parseRelocs(self: *TextBlock, relocs: []macho.relocation_info, context: RelocContext) !void { |
| 687 | | const filtered_relocs = filterRelocs(relocs, context.base_addr, context.base_addr + self.size); |
| 688 | const filtered_relocs = filterRelocs(relocs, context.base_offset, context.base_offset + self.size); |
| 688 | 689 | var it = RelocIterator{ |
| 689 | 690 | .buffer = filtered_relocs, |
| 690 | 691 | }; |
| ... | ... | @@ -956,9 +957,9 @@ fn parseUnsigned( |
| 956 | 957 | mem.readIntLittle(i32, self.code.items[out.offset..][0..4]); |
| 957 | 958 | |
| 958 | 959 | if (rel.r_extern == 0) { |
| 959 | | const source_seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; |
| 960 | | const source_sect_base_addr = source_seg.sections.items[rel.r_symbolnum - 1].addr; |
| 961 | | addend -= @intCast(i64, source_sect_base_addr); |
| 960 | const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; |
| 961 | const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr; |
| 962 | addend -= @intCast(i64, target_sect_base_addr); |
| 962 | 963 | } |
| 963 | 964 | |
| 964 | 965 | out.payload = .{ |
| ... | ... | @@ -1043,7 +1044,7 @@ fn parseSigned(self: TextBlock, rel: macho.relocation_info, out: *Relocation, co |
| 1043 | 1044 | assert(rel.r_length == 2); |
| 1044 | 1045 | |
| 1045 | 1046 | const rel_type = @intToEnum(macho.reloc_type_x86_64, rel.r_type); |
| 1046 | | const correction: i4 = switch (rel_type) { |
| 1047 | const correction: u3 = switch (rel_type) { |
| 1047 | 1048 | .X86_64_RELOC_SIGNED => 0, |
| 1048 | 1049 | .X86_64_RELOC_SIGNED_1 => 1, |
| 1049 | 1050 | .X86_64_RELOC_SIGNED_2 => 2, |
| ... | ... | @@ -1053,9 +1054,9 @@ fn parseSigned(self: TextBlock, rel: macho.relocation_info, out: *Relocation, co |
| 1053 | 1054 | var addend: i64 = mem.readIntLittle(i32, self.code.items[out.offset..][0..4]) + correction; |
| 1054 | 1055 | |
| 1055 | 1056 | if (rel.r_extern == 0) { |
| 1056 | | const source_seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; |
| 1057 | | const source_sect_base_addr = source_seg.sections.items[rel.r_symbolnum - 1].addr; |
| 1058 | | addend = @intCast(i64, out.offset) + addend - @intCast(i64, source_sect_base_addr) + 4 + correction; |
| 1057 | const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; |
| 1058 | const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr; |
| 1059 | addend += @intCast(i64, context.base_addr + out.offset + correction + 4) - @intCast(i64, target_sect_base_addr); |
| 1059 | 1060 | } |
| 1060 | 1061 | |
| 1061 | 1062 | out.payload = .{ |