From 9942bd025d8716393b82afab89b6c394d752c0b5 Mon Sep 17 00:00:00 2001 From: Pete Cooper Date: Mon, 4 Jan 2016 21:23:12 +0000 Subject: [PATCH] Fix x86_64 delta*Anon relocs. The encoded value should be an offset from the fixup location, which means that it should take in to account the fixup offset in its section. We weren't subtracting the base address of the atom, which meant that when we parsed the file again for a round trip, we had 2x the atom address in our target address. I've also improved comments for these to try and describe what is going on. There's no test case right now, as the bug is only exhibited when __data is at a non-zero address in a -r link. A commit will soon sort the sections differently and move __data to after __text. Then these relocations in test/mach-o/parse-data-relocs-x86_64.yaml will test for this bug. llvm-svn: 256779 --- .../ReaderWriter/MachO/ArchHandler_x86_64.cpp | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lld/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp b/lld/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp index c57a00359e33..8b4d1cf38cba 100644 --- a/lld/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp +++ b/lld/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp @@ -647,13 +647,33 @@ void ArchHandler_x86_64::applyFixupRelocatable(const Reference &ref, *loc32 = ref.addend() + inAtomAddress - fixupAddress; return; case delta32Anon: - *loc32 = (targetAddress - fixupAddress) + ref.addend(); + // The value we write here should be the the delta to the target + // after taking in to account the difference from the fixup back to the + // last defined label + // ie, if we have: + // _base: ... + // Lfixup: .quad Ltarget - . + // ... + // Ltarget: + // + // Then we want to encode the value (Ltarget + addend) - (LFixup - _base) + *loc32 = (targetAddress + ref.addend()) - (fixupAddress - inAtomAddress); return; case delta64: *loc64 = ref.addend() + inAtomAddress - fixupAddress; return; case delta64Anon: - *loc64 = (targetAddress - fixupAddress) + ref.addend(); + // The value we write here should be the the delta to the target + // after taking in to account the difference from the fixup back to the + // last defined label + // ie, if we have: + // _base: ... + // Lfixup: .quad Ltarget - . + // ... + // Ltarget: + // + // Then we want to encode the value (Ltarget + addend) - (LFixup - _base) + *loc64 = (targetAddress + ref.addend()) - (fixupAddress - inAtomAddress); return; case negDelta32: *loc32 = fixupAddress - targetAddress + ref.addend();