Skip to content

Commit e15abb7

Browse files
committed
[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
1 parent 62da359 commit e15abb7

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

llvm/include/llvm/ExecutionEngine/Orc/LoadRelocatableObject.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ namespace orc {
2727

2828
// Load an object file compatible with the given triple (if given) from the
2929
// given path. May return a file slice if the path contains a universal binary.
30-
Expected<std::unique_ptr<MemoryBuffer>> loadRelocatableObject(StringRef Path,
31-
const Triple &TT);
30+
Expected<std::unique_ptr<MemoryBuffer>> loadRelocatableObject(
31+
StringRef Path, const Triple &TT,
32+
std::optional<StringRef> IdentifierOverride = std::nullopt);
3233

3334
} // End namespace orc
3435
} // End namespace llvm

llvm/include/llvm/ExecutionEngine/Orc/MachO.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ checkMachORelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT,
3838
/// Load a relocatable object compatible with TT from Path.
3939
/// If Path is a universal binary, this function will return a buffer for the
4040
/// slice compatible with Triple (if one is present).
41-
Expected<std::unique_ptr<MemoryBuffer>>
42-
loadMachORelocatableObject(StringRef Path, const Triple &TT);
41+
Expected<std::unique_ptr<MemoryBuffer>> loadMachORelocatableObject(
42+
StringRef Path, const Triple &TT,
43+
std::optional<StringRef> IdentifierOverride = std::nullopt);
4344

4445
/// Load a compatible relocatable object (if available) from a MachO universal
4546
/// binary.
4647
Expected<std::unique_ptr<MemoryBuffer>>
4748
loadMachORelocatableObjectFromUniversalBinary(
48-
StringRef UBPath, std::unique_ptr<MemoryBuffer> UBBuf, const Triple &TT);
49+
StringRef UBPath, std::unique_ptr<MemoryBuffer> UBBuf, const Triple &TT,
50+
std::optional<StringRef> IdentifierOverride = std::nullopt);
4951

5052
/// Utility for identifying the file-slice compatible with TT in a universal
5153
/// binary.

llvm/lib/ExecutionEngine/Orc/LoadRelocatableObject.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "llvm/ExecutionEngine/Orc/LoadRelocatableObject.h"
1010
#include "llvm/BinaryFormat/Magic.h"
1111
#include "llvm/ExecutionEngine/Orc/MachO.h"
12+
#include "llvm/Support/FileSystem.h"
1213

1314
#define DEBUG_TYPE "orc"
1415

@@ -29,10 +30,22 @@ checkELFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT) {
2930
}
3031

3132
Expected<std::unique_ptr<MemoryBuffer>>
32-
loadRelocatableObject(StringRef Path, const Triple &TT) {
33-
auto Buf = MemoryBuffer::getFile(Path);
33+
loadRelocatableObject(StringRef Path, const Triple &TT,
34+
std::optional<StringRef> IdentifierOverride) {
35+
if (!IdentifierOverride)
36+
IdentifierOverride = Path;
37+
38+
Expected<sys::fs::file_t> FDOrErr =
39+
sys::fs::openNativeFileForRead(Path, sys::fs::OF_None);
40+
if (!FDOrErr)
41+
return createFileError(Path, FDOrErr.takeError());
42+
sys::fs::file_t FD = *FDOrErr;
43+
auto Buf =
44+
MemoryBuffer::getOpenFile(FD, *IdentifierOverride, /*FileSize=*/-1);
45+
sys::fs::closeFile(FD);
3446
if (!Buf)
35-
return createFileError(Path, Buf.getError());
47+
return make_error<StringError>(
48+
StringRef("Could not load object at path ") + Path, Buf.getError());
3649

3750
std::optional<Triple::ObjectFormatType> RequireFormat;
3851
if (TT.getObjectFormat() != Triple::UnknownObjectFormat)
@@ -53,8 +66,8 @@ loadRelocatableObject(StringRef Path, const Triple &TT) {
5366
break;
5467
case file_magic::macho_universal_binary:
5568
if (!RequireFormat || *RequireFormat == Triple::MachO)
56-
return loadMachORelocatableObjectFromUniversalBinary(Path,
57-
std::move(*Buf), TT);
69+
return loadMachORelocatableObjectFromUniversalBinary(
70+
Path, std::move(*Buf), TT, IdentifierOverride);
5871
break;
5972
default:
6073
break;

llvm/lib/ExecutionEngine/Orc/MachO.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "llvm/BinaryFormat/MachO.h"
1212
#include "llvm/Object/MachOUniversal.h"
13+
#include "llvm/Support/FileSystem.h"
1314

1415
#define DEBUG_TYPE "orc"
1516

@@ -85,14 +86,27 @@ checkMachORelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT,
8586
}
8687

8788
Expected<std::unique_ptr<MemoryBuffer>>
88-
loadMachORelocatableObject(StringRef Path, const Triple &TT) {
89+
loadMachORelocatableObject(StringRef Path, const Triple &TT,
90+
std::optional<StringRef> IdentifierOverride) {
8991
assert((TT.getObjectFormat() == Triple::UnknownObjectFormat ||
9092
TT.getObjectFormat() == Triple::MachO) &&
9193
"TT must specify MachO or Unknown object format");
9294

93-
auto Buf = MemoryBuffer::getFile(Path);
95+
if (!IdentifierOverride)
96+
IdentifierOverride = Path;
97+
98+
Expected<sys::fs::file_t> FDOrErr =
99+
sys::fs::openNativeFileForRead(Path, sys::fs::OF_None);
100+
if (!FDOrErr)
101+
return createFileError(Path, FDOrErr.takeError());
102+
sys::fs::file_t FD = *FDOrErr;
103+
auto Buf =
104+
MemoryBuffer::getOpenFile(FD, *IdentifierOverride, /*FileSize=*/-1);
105+
sys::fs::closeFile(FD);
94106
if (!Buf)
95-
return createFileError(Path, Buf.getError());
107+
return make_error<StringError>(
108+
StringRef("Could not load MachO object at path ") + Path,
109+
Buf.getError());
96110

97111
switch (identify_magic((*Buf)->getBuffer())) {
98112
case file_magic::macho_object:
@@ -110,7 +124,8 @@ loadMachORelocatableObject(StringRef Path, const Triple &TT) {
110124

111125
Expected<std::unique_ptr<MemoryBuffer>>
112126
loadMachORelocatableObjectFromUniversalBinary(
113-
StringRef UBPath, std::unique_ptr<MemoryBuffer> UBBuf, const Triple &TT) {
127+
StringRef UBPath, std::unique_ptr<MemoryBuffer> UBBuf, const Triple &TT,
128+
std::optional<StringRef> IdentifierOverride) {
114129

115130
auto UniversalBin =
116131
object::MachOUniversalBinary::create(UBBuf->getMemBufferRef());
@@ -121,6 +136,20 @@ loadMachORelocatableObjectFromUniversalBinary(
121136
if (!SliceRange)
122137
return SliceRange.takeError();
123138

139+
Expected<sys::fs::file_t> FDOrErr =
140+
sys::fs::openNativeFileForRead(UBPath, sys::fs::OF_None);
141+
if (!FDOrErr)
142+
return createFileError(UBPath, FDOrErr.takeError());
143+
sys::fs::file_t FD = *FDOrErr;
144+
auto Buf = MemoryBuffer::getOpenFileSlice(
145+
FD, *IdentifierOverride, SliceRange->second, SliceRange->first);
146+
sys::fs::closeFile(FD);
147+
if (!Buf)
148+
return make_error<StringError>(
149+
"Could not load " + TT.getArchName() +
150+
" slice of MachO universal binary at path " + UBPath,
151+
Buf.getError());
152+
124153
auto ObjBuf = errorOrToExpected(MemoryBuffer::getFileSlice(
125154
UBPath, SliceRange->second, SliceRange->first, false));
126155
if (!ObjBuf)

0 commit comments

Comments
 (0)