[llvm] 5114b9b - [ORC][llvm-jitlink] Extend weak-linking emulation to real dylibs.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 25 00:53:38 PST 2025


Author: Lang Hames
Date: 2025-02-25T19:53:31+11:00
New Revision: 5114b9b386ca69058d19d9c3dac53b4b429c71a6

URL: https://github.com/llvm/llvm-project/commit/5114b9b386ca69058d19d9c3dac53b4b429c71a6
DIFF: https://github.com/llvm/llvm-project/commit/5114b9b386ca69058d19d9c3dac53b4b429c71a6.diff

LOG: [ORC][llvm-jitlink] Extend weak-linking emulation to real dylibs.

Commit 253e11695ba added support for emulating weak-linking against dylibs
that are (under the emulation) absent at runtime. This commit extends emulated
weak linking support to allow a real dylib to supply the interface (i.e.
-weak-lx / -weak_library can be pointed at a dylib, in which case they should
be read as "weak-link against this dylib, behavining as if it weren't actually
present at runtime").

Added: 
    llvm/include/llvm/ExecutionEngine/Orc/GetDylibInterface.h
    llvm/lib/ExecutionEngine/Orc/GetDylibInterface.cpp

Modified: 
    llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
    llvm/tools/llvm-jitlink/llvm-jitlink.cpp

Removed: 
    llvm/include/llvm/ExecutionEngine/Orc/GetTapiInterface.h
    llvm/lib/ExecutionEngine/Orc/GetTapiInterface.cpp


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/GetDylibInterface.h b/llvm/include/llvm/ExecutionEngine/Orc/GetDylibInterface.h
new file mode 100644
index 0000000000000..3bdf33089b1c0
--- /dev/null
+++ b/llvm/include/llvm/ExecutionEngine/Orc/GetDylibInterface.h
@@ -0,0 +1,40 @@
+//===---- GetDylibInterface.h - Get interface for real dylib ----*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Get symbol interface from a real dynamic library or TAPI file. These
+// interfaces can be used to simulate weak linking (ld64 -weak-lx /
+// -weak_library) against a library that is absent at runtime.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_ORC_GETDYLIBINTERFACE_H
+#define LLVM_EXECUTIONENGINE_ORC_GETDYLIBINTERFACE_H
+
+#include "llvm/ExecutionEngine/Orc/Core.h"
+#include "llvm/Object/TapiUniversal.h"
+
+namespace llvm::orc {
+
+/// Returns a SymbolNameSet containing the exported symbols defined in the
+/// given dylib.
+Expected<SymbolNameSet> getDylibInterfaceFromDylib(ExecutionSession &ES,
+                                                   Twine Path);
+
+/// Returns a SymbolNameSet containing the exported symbols defined in the
+/// relevant slice of the TapiUniversal file.
+Expected<SymbolNameSet> getDylibInterfaceFromTapiFile(ExecutionSession &ES,
+                                                      Twine Path);
+
+/// Returns a SymbolNameSet containing the exported symbols defined in the
+/// relevant slice of the given file, which may be either a dylib or a tapi
+/// file.
+Expected<SymbolNameSet> getDylibInterface(ExecutionSession &ES, Twine Path);
+
+} // namespace llvm::orc
+
+#endif // LLVM_EXECUTIONENGINE_ORC_GETDYLIBINTERFACE_H

diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/GetTapiInterface.h b/llvm/include/llvm/ExecutionEngine/Orc/GetTapiInterface.h
deleted file mode 100644
index 36e943808e530..0000000000000
--- a/llvm/include/llvm/ExecutionEngine/Orc/GetTapiInterface.h
+++ /dev/null
@@ -1,28 +0,0 @@
-//===---- GetTapiInterface.h -- Get interface from TAPI file ----*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// Get symbol interface from TAPI file.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_EXECUTIONENGINE_ORC_GETTAPIINTERFACE_H
-#define LLVM_EXECUTIONENGINE_ORC_GETTAPIINTERFACE_H
-
-#include "llvm/ExecutionEngine/Orc/Core.h"
-#include "llvm/Object/TapiUniversal.h"
-
-namespace llvm::orc {
-
-/// Returns a SymbolNameSet containing the exported symbols defined in the
-/// relevant slice of the TapiUniversal file.
-Expected<SymbolNameSet> getInterfaceFromTapiFile(ExecutionSession &ES,
-                                                 object::TapiUniversal &TU);
-
-} // namespace llvm::orc
-
-#endif // LLVM_EXECUTIONENGINE_ORC_GETTAPIINTERFACE_H

diff  --git a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
index eab8ee160a733..c701a05ee301f 100644
--- a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
+++ b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
@@ -25,7 +25,7 @@ add_llvm_component_library(LLVMOrcJIT
   EPCIndirectionUtils.cpp
   ExecutionUtils.cpp
   ObjectFileInterface.cpp
-  GetTapiInterface.cpp
+  GetDylibInterface.cpp
   IndirectionUtils.cpp
   IRCompileLayer.cpp
   IRTransformLayer.cpp

diff  --git a/llvm/lib/ExecutionEngine/Orc/GetDylibInterface.cpp b/llvm/lib/ExecutionEngine/Orc/GetDylibInterface.cpp
new file mode 100644
index 0000000000000..f49b9fe3c1ec9
--- /dev/null
+++ b/llvm/lib/ExecutionEngine/Orc/GetDylibInterface.cpp
@@ -0,0 +1,127 @@
+//===-------- GetDylibInterface.cpp - Get interface for real dylib --------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ExecutionEngine/Orc/GetDylibInterface.h"
+
+#include "llvm/BinaryFormat/Magic.h"
+#include "llvm/Object/MachO.h"
+#include "llvm/Object/MachOUniversal.h"
+
+#define DEBUG_TYPE "orc"
+
+namespace llvm::orc {
+
+Expected<SymbolNameSet> getDylibInterfaceFromDylib(ExecutionSession &ES,
+                                                   Twine Path) {
+  auto CPUType = MachO::getCPUType(ES.getTargetTriple());
+  if (!CPUType)
+    return CPUType.takeError();
+
+  auto CPUSubType = MachO::getCPUSubType(ES.getTargetTriple());
+  if (!CPUSubType)
+    return CPUSubType.takeError();
+
+  auto Buf = MemoryBuffer::getFile(Path);
+  if (!Buf)
+    return createFileError(Path, Buf.getError());
+
+  auto SymFile =
+      object::SymbolicFile::createSymbolicFile((*Buf)->getMemBufferRef());
+  if (!SymFile)
+    return SymFile.takeError();
+
+  std::unique_ptr<object::MachOObjectFile> MachOFile;
+  if (isa<object::MachOObjectFile>(**SymFile))
+    MachOFile.reset(dyn_cast<object::MachOObjectFile>(SymFile->release()));
+  else if (auto *MachOUni =
+               dyn_cast<object::MachOUniversalBinary>(SymFile->get())) {
+    for (auto &O : MachOUni->objects()) {
+      if (O.getCPUType() == *CPUType && O.getCPUSubType() == *CPUSubType) {
+        if (auto Obj = O.getAsObjectFile())
+          MachOFile = std::move(*Obj);
+        else
+          return Obj.takeError();
+        break;
+      }
+    }
+    if (!MachOFile)
+      return make_error<StringError>("MachO universal binary at " + Path +
+                                         " does not contain a slice for " +
+                                         ES.getTargetTriple().str(),
+                                     inconvertibleErrorCode());
+  } else
+    return make_error<StringError>("File at " + Path + " is not a MachO",
+                                   inconvertibleErrorCode());
+
+  if (MachOFile->getHeader().filetype != MachO::MH_DYLIB)
+    return make_error<StringError>("MachO at " + Path + " is not a dylib",
+                                   inconvertibleErrorCode());
+
+  SymbolNameSet Symbols;
+  for (auto &Sym : MachOFile->symbols()) {
+    if (auto Name = Sym.getName())
+      Symbols.insert(ES.intern(*Name));
+    else
+      return Name.takeError();
+  }
+
+  return std::move(Symbols);
+}
+
+Expected<SymbolNameSet> getDylibInterfaceFromTapiFile(ExecutionSession &ES,
+                                                      Twine Path) {
+  SymbolNameSet Symbols;
+
+  auto TapiFileBuffer = MemoryBuffer::getFile(Path);
+  if (!TapiFileBuffer)
+    return createFileError(Path, TapiFileBuffer.getError());
+
+  auto Tapi =
+      object::TapiUniversal::create((*TapiFileBuffer)->getMemBufferRef());
+  if (!Tapi)
+    return Tapi.takeError();
+
+  auto CPUType = MachO::getCPUType(ES.getTargetTriple());
+  if (!CPUType)
+    return CPUType.takeError();
+
+  auto CPUSubType = MachO::getCPUSubType(ES.getTargetTriple());
+  if (!CPUSubType)
+    return CPUSubType.takeError();
+
+  auto &IF = (*Tapi)->getInterfaceFile();
+  auto Interface =
+      IF.extract(MachO::getArchitectureFromCpuType(*CPUType, *CPUSubType));
+  if (!Interface)
+    return Interface.takeError();
+
+  for (auto *Sym : (*Interface)->exports())
+    Symbols.insert(ES.intern(Sym->getName()));
+
+  return Symbols;
+}
+
+Expected<SymbolNameSet> getDylibInterface(ExecutionSession &ES, Twine Path) {
+  file_magic Magic;
+  if (auto EC = identify_magic(Path, Magic))
+    return createFileError(Path, EC);
+
+  SymbolNameSet Symbols;
+  switch (Magic) {
+  case file_magic::macho_dynamically_linked_shared_lib:
+    return getDylibInterfaceFromDylib(ES, Path);
+  case file_magic::tapi_file:
+    return getDylibInterfaceFromTapiFile(ES, Path);
+  default:
+    return make_error<StringError>("Cannot get interface for " + Path +
+                                       " unrecognized file type",
+                                   inconvertibleErrorCode());
+  }
+}
+
+} // namespace llvm::orc

diff  --git a/llvm/lib/ExecutionEngine/Orc/GetTapiInterface.cpp b/llvm/lib/ExecutionEngine/Orc/GetTapiInterface.cpp
deleted file mode 100644
index f74db3e7dd85a..0000000000000
--- a/llvm/lib/ExecutionEngine/Orc/GetTapiInterface.cpp
+++ /dev/null
@@ -1,39 +0,0 @@
-//===--------- GetTapiInterface.cpp - Get interface from TAPI file --------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/ExecutionEngine/Orc/GetTapiInterface.h"
-
-#define DEBUG_TYPE "orc"
-
-namespace llvm::orc {
-
-Expected<SymbolNameSet> getInterfaceFromTapiFile(ExecutionSession &ES,
-                                                 object::TapiUniversal &TU) {
-  SymbolNameSet Symbols;
-
-  auto CPUType = MachO::getCPUType(ES.getTargetTriple());
-  if (!CPUType)
-    return CPUType.takeError();
-
-  auto CPUSubType = MachO::getCPUSubType(ES.getTargetTriple());
-  if (!CPUSubType)
-    return CPUSubType.takeError();
-
-  auto &TUIF = TU.getInterfaceFile();
-  auto ArchInterface =
-      TUIF.extract(MachO::getArchitectureFromCpuType(*CPUType, *CPUSubType));
-  if (!ArchInterface)
-    return ArchInterface.takeError();
-
-  for (auto *Sym : (*ArchInterface)->exports())
-    Symbols.insert(ES.intern(Sym->getName()));
-
-  return Symbols;
-}
-
-} // namespace llvm::orc

diff  --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
index 81d5fe03419c0..34c1b87ae58ad 100644
--- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
+++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
@@ -29,7 +29,7 @@
 #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
 #include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
-#include "llvm/ExecutionEngine/Orc/GetTapiInterface.h"
+#include "llvm/ExecutionEngine/Orc/GetDylibInterface.h"
 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
 #include "llvm/ExecutionEngine/Orc/JITLinkRedirectableSymbolManager.h"
 #include "llvm/ExecutionEngine/Orc/JITLinkReentryTrampolines.h"
@@ -2117,17 +2117,8 @@ static SmallVector<StringRef, 5> getSearchPathsFromEnvVar(Session &S) {
 }
 
 static Expected<std::unique_ptr<DefinitionGenerator>>
-LoadLibraryWeak(Session &S, StringRef InterfacePath) {
-  auto TapiFileBuffer = getFile(InterfacePath);
-  if (!TapiFileBuffer)
-    return TapiFileBuffer.takeError();
-
-  auto Tapi =
-      object::TapiUniversal::create((*TapiFileBuffer)->getMemBufferRef());
-  if (!Tapi)
-    return Tapi.takeError();
-
-  auto Symbols = getInterfaceFromTapiFile(S.ES, **Tapi);
+LoadLibraryWeak(Session &S, StringRef Path) {
+  auto Symbols = getDylibInterface(S.ES, Path);
   if (!Symbols)
     return Symbols.takeError();
 
@@ -2227,7 +2218,7 @@ static Error addLibraries(Session &S,
   StringRef StandardExtensions[] = {".so", ".dylib", ".dll", ".a", ".lib"};
   StringRef DynLibExtensionsOnly[] = {".so", ".dylib", ".dll"};
   StringRef ArchiveExtensionsOnly[] = {".a", ".lib"};
-  StringRef InterfaceExtensionsOnly = {".tbd"};
+  StringRef WeakLinkExtensionsOnly[] = {".dylib", ".tbd"};
 
   // Add -lx arguments to LibraryLoads.
   for (auto LibItr = Libraries.begin(), LibEnd = Libraries.end();
@@ -2260,7 +2251,7 @@ static Error addLibraries(Session &S,
     LibraryLoad LL;
     LL.LibName = *LibWeakItr;
     LL.Position = LibrariesWeak.getPosition(LibWeakItr - LibrariesWeak.begin());
-    LL.CandidateExtensions = InterfaceExtensionsOnly;
+    LL.CandidateExtensions = WeakLinkExtensionsOnly;
     LL.Modifier = LibraryLoad::Weak;
     LibraryLoadQueue.push_back(std::move(LL));
   }
@@ -2403,8 +2394,15 @@ static Error addLibraries(Session &S,
         case file_magic::pecoff_executable:
         case file_magic::elf_shared_object:
         case file_magic::macho_dynamically_linked_shared_lib: {
-          if (auto Err = S.loadAndLinkDynamicLibrary(JD, LibPath.data()))
-            return Err;
+          if (LL.Modifier == LibraryLoad::Weak) {
+            if (auto G = LoadLibraryWeak(S, LibPath.data()))
+              JD.addGenerator(std::move(*G));
+            else
+              return G.takeError();
+          } else {
+            if (auto Err = S.loadAndLinkDynamicLibrary(JD, LibPath.data()))
+              return Err;
+          }
           break;
         }
         case file_magic::archive:


        


More information about the llvm-commits mailing list