[lld] r256779 - Fix x86_64 delta*Anon relocs.

Pete Cooper via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 4 13:23:12 PST 2016


Author: pete
Date: Mon Jan  4 15:23:12 2016
New Revision: 256779

URL: http://llvm.org/viewvc/llvm-project?rev=256779&view=rev
Log:
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.

Modified:
    lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp

Modified: lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp?rev=256779&r1=256778&r2=256779&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp Mon Jan  4 15:23:12 2016
@@ -647,13 +647,33 @@ void ArchHandler_x86_64::applyFixupReloc
     *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();




More information about the llvm-commits mailing list