authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-11-28 20:35:00+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-11-28 20:35:00+01:00
log7e27ab09476870e4b37a04172043c66b4bd77c20
tree1200fa721801ac675842b160a437a63907da84ce
parentaa61e03f244a72ea01f05c3ceea7c5fb5aadf1ff

macho: fix parsing addend for non-extern SIGNED_X reloc

If `r_extern == 0` (the relocation is non-extern, meaning it targets a specific memory offset within the object's section) and if the relocation type signifies that the relocation requires correction for RIP such as SIGNED_1, then we need to subtract the correction, here 1 for SIGNED_1, from the calculated addend value as it's implicitly included.

1 files changed, 3 insertions(+), 1 deletions(-)

src/link/MachO/Atom.zig+3-1
...@@ -437,9 +437,11 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC...@@ -437,9 +437,11 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
437 };437 };
438 addend = mem.readIntLittle(i32, self.code.items[offset..][0..4]) + correction;438 addend = mem.readIntLittle(i32, self.code.items[offset..][0..4]) + correction;
439 if (rel.r_extern == 0) {439 if (rel.r_extern == 0) {
440 // Note for the future self: when r_extern == 0, we should subtract correction from the
441 // addend.
440 const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment;442 const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment;
441 const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr;443 const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr;
442 addend += @intCast(i64, context.base_addr + offset + correction + 4) -444 addend += @intCast(i64, context.base_addr + offset + 4) -
443 @intCast(i64, target_sect_base_addr);445 @intCast(i64, target_sect_base_addr);
444 }446 }
445 },447 },