Correct PIC function stub codegen.
authorEvan Cheng <evan.cheng@apple.com>
Mon, 10 Nov 2008 23:14:47 +0000 (23:14 +0000)
committerEvan Cheng <evan.cheng@apple.com>
Mon, 10 Nov 2008 23:14:47 +0000 (23:14 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@59006 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/ARM/ARMJITInfo.cpp
lib/Target/ARM/ARMJITInfo.h

index 3ce311784da55a82ced0dc017c6350703073108d..c93e28be23b399ba0781026e3aa1c737d185ac26 100644 (file)
@@ -19,6 +19,7 @@
 #include "llvm/Function.h"
 #include "llvm/CodeGen/MachineCodeEmitter.h"
 #include "llvm/Config/alloca.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/Streams.h"
 #include "llvm/System/Memory.h"
 #include <cstdlib>
@@ -133,7 +134,9 @@ void *ARMJITInfo::emitGlobalValueIndirectSym(const GlobalValue *GV, void *Ptr,
                                              MachineCodeEmitter &MCE) {
   MCE.startGVStub(GV, 4, 4);
   MCE.emitWordLE((intptr_t)Ptr);
-  return MCE.finishGVStub(GV);
+  void *PtrAddr = MCE.finishGVStub(GV);
+  addIndirectSymAddr(Ptr, (intptr_t)PtrAddr);
+  return PtrAddr;
 }
 
 void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
@@ -142,12 +145,34 @@ void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
   // call.  The code is the same except for one bit of the last instruction.
   if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
     // Branch to the corresponding function addr.
-    // The stub is 8-byte size and 4-aligned.
-    MCE.startGVStub(F, 8, 4);
-    intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
-    MCE.emitWordLE(0xe51ff004);    // ldr pc, [pc, #-4]
-    MCE.emitWordLE((intptr_t)Fn);  // addr of function
-    sys::Memory::InvalidateInstructionCache((void*)Addr, 8);
+    if (IsPIC) {
+      // The stub is 8-byte size and 4-aligned.
+      intptr_t LazyPtr = getIndirectSymAddr(Fn);
+      if (!LazyPtr) {
+        // In PIC mode, the function stub is loading a lazy-ptr.
+        LazyPtr= (intptr_t)emitGlobalValueIndirectSym((GlobalValue*)F, Fn, MCE);
+        if (F)
+          DOUT << "JIT: Indirect symbol emitted at [" << LazyPtr << "] for GV '"
+               << F->getName() << "'\n";
+        else
+          DOUT << "JIT: Stub emitted at [" << LazyPtr
+               << "] for external function at '" << Fn << "'\n";
+      }
+      MCE.startGVStub(F, 16, 4);
+      intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
+      MCE.emitWordLE(0xe59fc004);            // ldr pc, [pc, #+4]
+      MCE.emitWordLE(0xe08fc00c);            // L_func$scv: add ip, pc, ip
+      MCE.emitWordLE(0xe59cf000);            // ldr pc, [ip]
+      MCE.emitWordLE(LazyPtr - (Addr+4+8));  // func - (L_func$scv+8)
+      sys::Memory::InvalidateInstructionCache((void*)Addr, 16);
+    } else {
+      // The stub is 8-byte size and 4-aligned.
+      MCE.startGVStub(F, 8, 4);
+      intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
+      MCE.emitWordLE(0xe51ff004);    // ldr pc, [pc, #-4]
+      MCE.emitWordLE((intptr_t)Fn);  // addr of function
+      sys::Memory::InvalidateInstructionCache((void*)Addr, 8);
+    }
   } else {
     // The compilation callback will overwrite the first two words of this
     // stub with indirect branch instructions targeting the compiled code. 
index a35611eaa3aae8fc806c3a12ba5eb10a17ce19db..8bcaa4c066907b1db6b3d1319e049100ff7cca12 100644 (file)
@@ -37,6 +37,10 @@ namespace llvm {
     // PCLabelMap - A map from PC labels to addresses.
     DenseMap<unsigned, intptr_t> PCLabelMap;
 
+    // Sym2IndirectSymMap - A map from symbol (GlobalValue and ExternalSymbol)
+    // addresses to their indirect symbol addresses.
+    DenseMap<void*, intptr_t> Sym2IndirectSymMap;
+
     // IsPIC - True if the relocation model is PIC. This is used to determine
     // how to codegen function stubs.
     bool IsPIC;
@@ -147,6 +151,22 @@ namespace llvm {
       PCLabelMap.insert(std::make_pair(Id, Addr));
     }
 
+    /// getIndirectSymAddr - Retrieve the address of the indirect symbol of the
+    /// specified symbol located at address. Returns 0 if the indirect symbol
+    /// has not been emitted.
+    intptr_t getIndirectSymAddr(void *Addr) const {
+      DenseMap<void*,intptr_t>::const_iterator I= Sym2IndirectSymMap.find(Addr);
+      if (I != Sym2IndirectSymMap.end())
+        return I->second;
+      return 0;
+    }
+
+    /// addIndirectSymAddr - Add a mapping from address of an emitted symbol to
+    /// its indirect symbol address.
+    void addIndirectSymAddr(void *SymAddr, intptr_t IndSymAddr) {
+      Sym2IndirectSymMap.insert(std::make_pair(SymAddr, IndSymAddr));
+    }
+
   private:
     /// resolveRelocDestAddr - Resolve the resulting address of the relocation
     /// if it's not already solved. Constantpool entries must be resolved by