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
This commit is contained in:
Pete Cooper 2016-01-04 21:23:12 +00:00
parent 703d4042ad
commit 9942bd025d
1 changed files with 22 additions and 2 deletions

View File

@ -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();