[MCJIT] Fix the ARM BR24 relocation in RuntimeDyldMachO.
authorLang Hames <lhames@gmail.com>
Wed, 30 Jul 2014 03:35:05 +0000 (03:35 +0000)
committerLang Hames <lhames@gmail.com>
Wed, 30 Jul 2014 03:35:05 +0000 (03:35 +0000)
We now (1) correctly decode the branch immediate, (2) modify the immediate to
corretly treat it as PC-rel, and (3) properly populate the stub entry.
Previously we had been doing each of these wrong.

<rdar://problem/17750739>

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@214285 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp
lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h
lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOAArch64.h
lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h
lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h
lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h
test/ExecutionEngine/RuntimeDyld/ARM/MachO_ARM_PIC_relocations.s

index 920b999fa089cc26cdb6cecc1cbec6e268164f6a..bae2471054df548ede68341043b92260bc4c5127 100644 (file)
@@ -79,7 +79,8 @@ RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
 
 void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
                                             ObjectImage &ObjImg,
 
 void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
                                             ObjectImage &ObjImg,
-                                            const relocation_iterator &RI) {
+                                            const relocation_iterator &RI,
+                                            unsigned OffsetToNextPC) {
   const MachOObjectFile &Obj =
       static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
   MachO::any_relocation_info RelInfo =
   const MachOObjectFile &Obj =
       static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
   MachO::any_relocation_info RelInfo =
@@ -89,8 +90,7 @@ void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
   if (IsPCRel) {
     uint64_t RelocAddr = 0;
     RI->getAddress(RelocAddr);
   if (IsPCRel) {
     uint64_t RelocAddr = 0;
     RI->getAddress(RelocAddr);
-    unsigned RelocSize = Obj.getAnyRelocationLength(RelInfo);
-    Value.Addend += RelocAddr + (1ULL << RelocSize);
+    Value.Addend += RelocAddr + OffsetToNextPC;
   }
 }
 
   }
 }
 
index 7450f1141e1622469455c92d348d417197e57221..30f61e98e32c2948c56a71bed3e5c86ca0a66878 100644 (file)
@@ -73,7 +73,8 @@ protected:
 
   /// Make the RelocationValueRef addend PC-relative.
   void makeValueAddendPCRel(RelocationValueRef &Value, ObjectImage &ObjImg,
 
   /// Make the RelocationValueRef addend PC-relative.
   void makeValueAddendPCRel(RelocationValueRef &Value, ObjectImage &ObjImg,
-                            const relocation_iterator &RI);
+                            const relocation_iterator &RI,
+                            unsigned OffsetToNextPC);
 
   /// Dump information about the relocation entry (RE) and resolved value.
   void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;
 
   /// Dump information about the relocation entry (RE) and resolved value.
   void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;
index 967b12ff44097fd083d91620d897f0595c5af8a7..ced85f3e55a93d9fb3aef0a355d1f036385dbcec 100644 (file)
@@ -276,7 +276,7 @@ public:
 
     bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
     if (!IsExtern && RE.IsPCRel)
 
     bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
     if (!IsExtern && RE.IsPCRel)
-      makeValueAddendPCRel(Value, ObjImg, RelI);
+      makeValueAddendPCRel(Value, ObjImg, RelI, 1 << RE.Size);
 
     RE.Addend = Value.Addend;
 
 
     RE.Addend = Value.Addend;
 
index 1de9942198249a695bcb623bff21bbab03b8b183..dadde769656741ff87c528b1e608a2074f58e00a 100644 (file)
@@ -18,6 +18,9 @@ namespace llvm {
 
 class RuntimeDyldMachOARM
     : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> {
 
 class RuntimeDyldMachOARM
     : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> {
+private:
+  typedef RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> ParentT;
+
 public:
   RuntimeDyldMachOARM(RTDyldMemoryManager *MM) : RuntimeDyldMachOCRTPBase(MM) {}
 
 public:
   RuntimeDyldMachOARM(RTDyldMemoryManager *MM) : RuntimeDyldMachOCRTPBase(MM) {}
 
@@ -25,6 +28,21 @@ public:
 
   unsigned getStubAlignment() override { return 4; }
 
 
   unsigned getStubAlignment() override { return 4; }
 
+  int64_t decodeAddend(uint8_t *LocalAddress, unsigned NumBytes,
+                       MachO::RelocationInfoType RelType) const {
+    switch (RelType) {
+      default:
+        return ParentT::decodeAddend(LocalAddress, NumBytes, RelType);
+      case MachO::ARM_RELOC_BR24: {
+        uint32_t Temp;
+        memcpy(&Temp, LocalAddress, 4);
+        Temp &= 0x00ffffff; // Mask out the opcode.
+        // Now we've got the shifted immediate, shift by 2, sign extend and ret.
+        return SignExtend32<26>(Temp << 2);
+      }
+    }
+  }
+
   relocation_iterator
   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
                        ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
   relocation_iterator
   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
                        ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
@@ -41,9 +59,8 @@ public:
     RelocationValueRef Value(
         getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
 
     RelocationValueRef Value(
         getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
 
-    bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
-    if (!IsExtern && RE.IsPCRel)
-      makeValueAddendPCRel(Value, ObjImg, RelI);
+    if (RE.IsPCRel)
+      makeValueAddendPCRel(Value, ObjImg, RelI, 8);
 
     if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
       processBranchRelocation(RE, Value, Stubs);
 
     if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
       processBranchRelocation(RE, Value, Stubs);
@@ -134,7 +151,8 @@ private:
       uint8_t *StubTargetAddr =
           createStubFunction(Section.Address + Section.StubOffset);
       RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address,
       uint8_t *StubTargetAddr =
           createStubFunction(Section.Address + Section.StubOffset);
       RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address,
-                             MachO::GENERIC_RELOC_VANILLA, Value.Addend);
+                             MachO::GENERIC_RELOC_VANILLA, Value.Addend, false,
+                             2);
       if (Value.SymbolName)
         addRelocationForSymbol(StubRE, Value.SymbolName);
       else
       if (Value.SymbolName)
         addRelocationForSymbol(StubRE, Value.SymbolName);
       else
@@ -142,7 +160,7 @@ private:
       Addr = Section.Address + Section.StubOffset;
       Section.StubOffset += getMaxStubSize();
     }
       Addr = Section.Address + Section.StubOffset;
       Section.StubOffset += getMaxStubSize();
     }
-    RelocationEntry TargetRE(Value.SectionID, RE.Offset, RE.RelType, 0,
+    RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
                              RE.IsPCRel, RE.Size);
     resolveRelocation(TargetRE, (uint64_t)Addr);
   }
                              RE.IsPCRel, RE.Size);
     resolveRelocation(TargetRE, (uint64_t)Addr);
   }
index 856c6ca3035c5a2cf2c72df65806139bc0adfdda..156287b3af1861db881bf11bbde4fc795abd589f 100644 (file)
@@ -62,7 +62,7 @@ public:
     //   Value.Addend += RelocAddr + 4;
     // }
     if (RE.IsPCRel)
     //   Value.Addend += RelocAddr + 4;
     // }
     if (RE.IsPCRel)
-      makeValueAddendPCRel(Value, ObjImg, RelI);
+      makeValueAddendPCRel(Value, ObjImg, RelI, 1 << RE.Size);
 
     RE.Addend = Value.Addend;
 
 
     RE.Addend = Value.Addend;
 
index 99efe9da448690b45162c8198f92e569518304a1..9106f4154227c061ac8b5752e5c4dec6331bc46b 100644 (file)
@@ -44,7 +44,7 @@ public:
 
     bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
     if (!IsExtern && RE.IsPCRel)
 
     bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
     if (!IsExtern && RE.IsPCRel)
-      makeValueAddendPCRel(Value, ObjImg, RelI);
+      makeValueAddendPCRel(Value, ObjImg, RelI, 1 << RE.Size);
 
     if (RE.RelType == MachO::X86_64_RELOC_GOT ||
         RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)
 
     if (RE.RelType == MachO::X86_64_RELOC_GOT ||
         RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)
index 86041835591f484ac32f8fffb0348c2622b5be60..d0d30271f0d85d59c59d312e398aca7e6109f2fa 100644 (file)
@@ -1,6 +1,5 @@
-# RUN: llvm-mc -triple=armv7s-apple-ios7.0.0 -relocation-model=pic -filetype=obj -o %t.o %s
-# RUN: llvm-rtdyld -triple=armv7s-apple-ios7.0.0 -verify -check=%s %t.o
-# RUN: rm %t.o
+# RUN: llvm-mc -triple=armv7s-apple-ios7.0.0 -relocation-model=pic -filetype=obj -o %T/foo.o %s
+# RUN: llvm-rtdyld -triple=armv7s-apple-ios7.0.0 -verify -check=%s %/T/foo.o
 
        .syntax unified
        .section        __TEXT,__text,regular,pure_instructions
 
        .syntax unified
        .section        __TEXT,__text,regular,pure_instructions
@@ -17,6 +16,17 @@ insn2:
        movt    r0, :upper16:(foo-(nextPC+8))
 nextPC:
        add     r0, pc, r0
        movt    r0, :upper16:(foo-(nextPC+8))
 nextPC:
        add     r0, pc, r0
+
+# Check stub generation by referencing a common symbol, 'baz'. Check both the
+# Content of the stub, and the reference to the stub.
+# Stub should contain '0xe51ff004' (ldr pc, [pc, #-4]), followed by the target.
+#
+# rtdyld-check: *{4}(stub_addr(foo.o, __text, baz)) = 0xe51ff004
+# rtdyld-check: *{4}(stub_addr(foo.o, __text, baz) + 4) = baz
+#
+# rtdyld-check: decode_operand(insn3, 0) = stub_addr(foo.o, __text, baz) - (insn3 + 8)
+insn3:
+        bl      baz
        bx      lr
 
        .globl  foo
        bx      lr
 
        .globl  foo
@@ -24,4 +34,6 @@ nextPC:
 foo:
        bx      lr
 
 foo:
        bx      lr
 
+        .comm   baz, 4, 2
+
 .subsections_via_symbols
 .subsections_via_symbols