[Orc] Add a new JITSymbol constructor to build a symbol from an existing address.
[oota-llvm.git] / include / llvm / ExecutionEngine / Orc / ObjectLinkingLayer.h
index 4dcb3808dabf9e7db6545ad419b451f2e09e2456..ad7c9ebdd9b772aa25e51d668a8630e6a4cbda65 100644 (file)
@@ -213,16 +213,27 @@ public:
   ///         given object set.
   JITSymbol findSymbolIn(ObjSetHandleT H, StringRef Name,
                          bool ExportedSymbolsOnly) {
-    if (auto Addr = H->getSymbolAddress(Name, ExportedSymbolsOnly))
-      return JITSymbol(
-        [this, Addr, H](){
-          if (H->NeedsFinalization()) {
-            H->Finalize();
-            if (NotifyFinalized)
-              NotifyFinalized(H);
-          }
-          return Addr;
-        });
+    if (auto Addr = H->getSymbolAddress(Name, ExportedSymbolsOnly)) {
+      if (!H->NeedsFinalization()) {
+        // If this instance has already been finalized then we can just return
+        // the address.
+        return JITSymbol(Addr);
+      } else {
+        // If this instance needs finalization return a functor that will do it.
+        // The functor still needs to double-check whether finalization is
+        // required, in case someone else finalizes this set before the functor
+        // is called.
+        return JITSymbol(
+          [this, Addr, H]() {
+            if (H->NeedsFinalization()) {
+              H->Finalize();
+              if (NotifyFinalized)
+                NotifyFinalized(H);
+            }
+            return Addr;
+          });
+      }
+    }
 
     return nullptr;
   }