From b9e2bedbb3473fbeb4c7a3e232ef838647a9a7a1 Mon Sep 17 00:00:00 2001
From: Axel Naumann <Axel.Naumann@cern.ch>
Date: Thu, 5 Dec 2013 16:22:34 +0100
Subject: [PATCH] Wrap a recurring question into an interface:
 getPointerToGlobalFromJIT().

(cherry picked from commit 0b7973cecb915abd7e0add8b907cec6784a30313)
---
 interpreter/cling/lib/Interpreter/ExecutionContext.cpp |  9 +++++++++
 interpreter/cling/lib/Interpreter/ExecutionContext.h   | 10 +++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/interpreter/cling/lib/Interpreter/ExecutionContext.cpp b/interpreter/cling/lib/Interpreter/ExecutionContext.cpp
index 371eead34ef..7a44299ab8b 100644
--- a/interpreter/cling/lib/Interpreter/ExecutionContext.cpp
+++ b/interpreter/cling/lib/Interpreter/ExecutionContext.cpp
@@ -413,3 +413,12 @@ void* ExecutionContext::getAddressOfGlobal(llvm::Module* m,
   }
   return address;
 }
+
+void*
+ExecutionContext::getPointerToGlobalFromJIT(const llvm::GlobalValue& GV) const {
+  if (void* addr = m_engine->getPointerToGlobalIfAvailable(&GV))
+    return addr;
+
+  //  Function not yet codegened by the JIT, force this to happen now.
+  return m_engine->getPointerToGlobal(&GV);
+}
diff --git a/interpreter/cling/lib/Interpreter/ExecutionContext.h b/interpreter/cling/lib/Interpreter/ExecutionContext.h
index 28bed25993e..26f323be122 100644
--- a/interpreter/cling/lib/Interpreter/ExecutionContext.h
+++ b/interpreter/cling/lib/Interpreter/ExecutionContext.h
@@ -14,8 +14,9 @@
 #include <set>
 
 namespace llvm {
-  class Module;
   class ExecutionEngine;
+  class GlobalValue;
+  class Module;
 }
 
 namespace clang {
@@ -177,6 +178,13 @@ namespace cling {
     void* getAddressOfGlobal(llvm::Module* m, const char* mangledName,
                              bool* fromJIT = 0) const;
 
+    ///\brief Return the address of a global from the ExecutionEngine (as
+    /// opposed to dynamic libraries). Forces the emission of the symbol if
+    /// it has not happened yet.
+    ///
+    ///param[in] GV - global value for which the address will be returned.
+    void* getPointerToGlobalFromJIT(const llvm::GlobalValue& GV) const;
+
     llvm::ExecutionEngine* getExecutionEngine() const {
       if (!m_engine)
         return 0;
-- 
GitLab