[llvm] e15abb7 - [ORC] Add an identifier-override argument to loadRelocatableObject and friends.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 22 21:01:50 PDT 2024


Author: Lang Hames
Date: 2024-08-23T14:01:42+10:00
New Revision: e15abb798282e4151f546eef14be4906f428eb46

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

LOG: [ORC] Add an identifier-override argument to loadRelocatableObject and friends.

API clients may want to use things other than paths as the buffer identifiers.

No testcase -- I haven't thought of a good way to expose this via the regression
testing tools.

rdar://133536831

Added: 
    

Modified: 
    llvm/include/llvm/ExecutionEngine/Orc/LoadRelocatableObject.h
    llvm/include/llvm/ExecutionEngine/Orc/MachO.h
    llvm/lib/ExecutionEngine/Orc/LoadRelocatableObject.cpp
    llvm/lib/ExecutionEngine/Orc/MachO.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/LoadRelocatableObject.h b/llvm/include/llvm/ExecutionEngine/Orc/LoadRelocatableObject.h
index a6a2f41dcff7d6..c79c3e316e3828 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/LoadRelocatableObject.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/LoadRelocatableObject.h
@@ -27,8 +27,9 @@ namespace orc {
 
 // Load an object file compatible with the given triple (if given) from the
 // given path. May return a file slice if the path contains a universal binary.
-Expected<std::unique_ptr<MemoryBuffer>> loadRelocatableObject(StringRef Path,
-                                                              const Triple &TT);
+Expected<std::unique_ptr<MemoryBuffer>> loadRelocatableObject(
+    StringRef Path, const Triple &TT,
+    std::optional<StringRef> IdentifierOverride = std::nullopt);
 
 } // End namespace orc
 } // End namespace llvm

diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/MachO.h b/llvm/include/llvm/ExecutionEngine/Orc/MachO.h
index 58c04a9121c87b..fdaa2f73cda6a3 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/MachO.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/MachO.h
@@ -38,14 +38,16 @@ checkMachORelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT,
 /// Load a relocatable object compatible with TT from Path.
 /// If Path is a universal binary, this function will return a buffer for the
 /// slice compatible with Triple (if one is present).
-Expected<std::unique_ptr<MemoryBuffer>>
-loadMachORelocatableObject(StringRef Path, const Triple &TT);
+Expected<std::unique_ptr<MemoryBuffer>> loadMachORelocatableObject(
+    StringRef Path, const Triple &TT,
+    std::optional<StringRef> IdentifierOverride = std::nullopt);
 
 /// Load a compatible relocatable object (if available) from a MachO universal
 /// binary.
 Expected<std::unique_ptr<MemoryBuffer>>
 loadMachORelocatableObjectFromUniversalBinary(
-    StringRef UBPath, std::unique_ptr<MemoryBuffer> UBBuf, const Triple &TT);
+    StringRef UBPath, std::unique_ptr<MemoryBuffer> UBBuf, const Triple &TT,
+    std::optional<StringRef> IdentifierOverride = std::nullopt);
 
 /// Utility for identifying the file-slice compatible with TT in a universal
 /// binary.

diff  --git a/llvm/lib/ExecutionEngine/Orc/LoadRelocatableObject.cpp b/llvm/lib/ExecutionEngine/Orc/LoadRelocatableObject.cpp
index 5fde1b5fb4da92..0a32a768313fd8 100644
--- a/llvm/lib/ExecutionEngine/Orc/LoadRelocatableObject.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/LoadRelocatableObject.cpp
@@ -9,6 +9,7 @@
 #include "llvm/ExecutionEngine/Orc/LoadRelocatableObject.h"
 #include "llvm/BinaryFormat/Magic.h"
 #include "llvm/ExecutionEngine/Orc/MachO.h"
+#include "llvm/Support/FileSystem.h"
 
 #define DEBUG_TYPE "orc"
 
@@ -29,10 +30,22 @@ checkELFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT) {
 }
 
 Expected<std::unique_ptr<MemoryBuffer>>
-loadRelocatableObject(StringRef Path, const Triple &TT) {
-  auto Buf = MemoryBuffer::getFile(Path);
+loadRelocatableObject(StringRef Path, const Triple &TT,
+                      std::optional<StringRef> IdentifierOverride) {
+  if (!IdentifierOverride)
+    IdentifierOverride = Path;
+
+  Expected<sys::fs::file_t> FDOrErr =
+      sys::fs::openNativeFileForRead(Path, sys::fs::OF_None);
+  if (!FDOrErr)
+    return createFileError(Path, FDOrErr.takeError());
+  sys::fs::file_t FD = *FDOrErr;
+  auto Buf =
+      MemoryBuffer::getOpenFile(FD, *IdentifierOverride, /*FileSize=*/-1);
+  sys::fs::closeFile(FD);
   if (!Buf)
-    return createFileError(Path, Buf.getError());
+    return make_error<StringError>(
+        StringRef("Could not load object at path ") + Path, Buf.getError());
 
   std::optional<Triple::ObjectFormatType> RequireFormat;
   if (TT.getObjectFormat() != Triple::UnknownObjectFormat)
@@ -53,8 +66,8 @@ loadRelocatableObject(StringRef Path, const Triple &TT) {
     break;
   case file_magic::macho_universal_binary:
     if (!RequireFormat || *RequireFormat == Triple::MachO)
-      return loadMachORelocatableObjectFromUniversalBinary(Path,
-                                                           std::move(*Buf), TT);
+      return loadMachORelocatableObjectFromUniversalBinary(
+          Path, std::move(*Buf), TT, IdentifierOverride);
     break;
   default:
     break;

diff  --git a/llvm/lib/ExecutionEngine/Orc/MachO.cpp b/llvm/lib/ExecutionEngine/Orc/MachO.cpp
index fe5a1dbbb9f8a1..8fc262220bf892 100644
--- a/llvm/lib/ExecutionEngine/Orc/MachO.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/MachO.cpp
@@ -10,6 +10,7 @@
 
 #include "llvm/BinaryFormat/MachO.h"
 #include "llvm/Object/MachOUniversal.h"
+#include "llvm/Support/FileSystem.h"
 
 #define DEBUG_TYPE "orc"
 
@@ -85,14 +86,27 @@ checkMachORelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT,
 }
 
 Expected<std::unique_ptr<MemoryBuffer>>
-loadMachORelocatableObject(StringRef Path, const Triple &TT) {
+loadMachORelocatableObject(StringRef Path, const Triple &TT,
+                           std::optional<StringRef> IdentifierOverride) {
   assert((TT.getObjectFormat() == Triple::UnknownObjectFormat ||
           TT.getObjectFormat() == Triple::MachO) &&
          "TT must specify MachO or Unknown object format");
 
-  auto Buf = MemoryBuffer::getFile(Path);
+  if (!IdentifierOverride)
+    IdentifierOverride = Path;
+
+  Expected<sys::fs::file_t> FDOrErr =
+      sys::fs::openNativeFileForRead(Path, sys::fs::OF_None);
+  if (!FDOrErr)
+    return createFileError(Path, FDOrErr.takeError());
+  sys::fs::file_t FD = *FDOrErr;
+  auto Buf =
+      MemoryBuffer::getOpenFile(FD, *IdentifierOverride, /*FileSize=*/-1);
+  sys::fs::closeFile(FD);
   if (!Buf)
-    return createFileError(Path, Buf.getError());
+    return make_error<StringError>(
+        StringRef("Could not load MachO object at path ") + Path,
+        Buf.getError());
 
   switch (identify_magic((*Buf)->getBuffer())) {
   case file_magic::macho_object:
@@ -110,7 +124,8 @@ loadMachORelocatableObject(StringRef Path, const Triple &TT) {
 
 Expected<std::unique_ptr<MemoryBuffer>>
 loadMachORelocatableObjectFromUniversalBinary(
-    StringRef UBPath, std::unique_ptr<MemoryBuffer> UBBuf, const Triple &TT) {
+    StringRef UBPath, std::unique_ptr<MemoryBuffer> UBBuf, const Triple &TT,
+    std::optional<StringRef> IdentifierOverride) {
 
   auto UniversalBin =
       object::MachOUniversalBinary::create(UBBuf->getMemBufferRef());
@@ -121,6 +136,20 @@ loadMachORelocatableObjectFromUniversalBinary(
   if (!SliceRange)
     return SliceRange.takeError();
 
+  Expected<sys::fs::file_t> FDOrErr =
+      sys::fs::openNativeFileForRead(UBPath, sys::fs::OF_None);
+  if (!FDOrErr)
+    return createFileError(UBPath, FDOrErr.takeError());
+  sys::fs::file_t FD = *FDOrErr;
+  auto Buf = MemoryBuffer::getOpenFileSlice(
+      FD, *IdentifierOverride, SliceRange->second, SliceRange->first);
+  sys::fs::closeFile(FD);
+  if (!Buf)
+    return make_error<StringError>(
+        "Could not load " + TT.getArchName() +
+            " slice of MachO universal binary at path " + UBPath,
+        Buf.getError());
+
   auto ObjBuf = errorOrToExpected(MemoryBuffer::getFileSlice(
       UBPath, SliceRange->second, SliceRange->first, false));
   if (!ObjBuf)


        


More information about the llvm-commits mailing list