Skip to content

Commit ef314d3

Browse files
committed
[ORC][MachO] Enable customization of MachO-headers produced by MachOPlatform.
MachOPlatform users can now override the default MachO header graph produced for JITDylibs when setupJITDylib is called.
1 parent f92d970 commit ef314d3

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

llvm/include/llvm/ExecutionEngine/Orc/MachOPlatform.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ class MachOPlatform : public Platform {
4747
LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ Callable)
4848
};
4949

50+
/// Used by setupJITDylib to create MachO header MaterializationUnits for
51+
/// JITDylibs.
52+
using MachOHeaderMUBuilder =
53+
unique_function<std::unique_ptr<MaterializationUnit>(MachOPlatform &MOP)>;
54+
55+
static std::unique_ptr<MaterializationUnit>
56+
defaultMachOHeaderBuilder(MachOPlatform &MOP);
57+
5058
/// Try to create a MachOPlatform instance, adding the ORC runtime to the
5159
/// given JITDylib.
5260
///
@@ -88,17 +96,23 @@ class MachOPlatform : public Platform {
8896
static Expected<std::unique_ptr<MachOPlatform>>
8997
Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
9098
JITDylib &PlatformJD, std::unique_ptr<DefinitionGenerator> OrcRuntime,
99+
MachOHeaderMUBuilder BuildMachOHeaderMU = defaultMachOHeaderBuilder,
91100
std::optional<SymbolAliasMap> RuntimeAliases = std::nullopt);
92101

93102
/// Construct using a path to the ORC runtime.
94103
static Expected<std::unique_ptr<MachOPlatform>>
95104
Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
96105
JITDylib &PlatformJD, const char *OrcRuntimePath,
106+
MachOHeaderMUBuilder BuildMachOHeaderMU = defaultMachOHeaderBuilder,
97107
std::optional<SymbolAliasMap> RuntimeAliases = std::nullopt);
98108

99109
ExecutionSession &getExecutionSession() const { return ES; }
100110
ObjectLinkingLayer &getObjectLinkingLayer() const { return ObjLinkingLayer; }
101111

112+
NonOwningSymbolStringPtr getMachOHeaderStartSymbol() const {
113+
return NonOwningSymbolStringPtr(MachOHeaderStartSymbol);
114+
}
115+
102116
Error setupJITDylib(JITDylib &JD) override;
103117
Error teardownJITDylib(JITDylib &JD) override;
104118
Error notifyAdding(ResourceTracker &RT,
@@ -243,7 +257,7 @@ class MachOPlatform : public Platform {
243257
MachOPlatform(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
244258
JITDylib &PlatformJD,
245259
std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator,
246-
Error &Err);
260+
MachOHeaderMUBuilder BuildMachOHeaderMU, Error &Err);
247261

248262
// Associate MachOPlatform JIT-side runtime support functions with handlers.
249263
Error associateRuntimeSupportFunctions();
@@ -271,6 +285,7 @@ class MachOPlatform : public Platform {
271285
ExecutionSession &ES;
272286
JITDylib &PlatformJD;
273287
ObjectLinkingLayer &ObjLinkingLayer;
288+
MachOHeaderMUBuilder BuildMachOHeaderMU;
274289

275290
SymbolStringPtr MachOHeaderStartSymbol = ES.intern("___dso_handle");
276291

llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
1616
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
1717
#include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h"
18+
#include "llvm/ExecutionEngine/Orc/MachOBuilder.h"
1819
#include "llvm/ExecutionEngine/Orc/Shared/ObjectFormats.h"
1920
#include "llvm/Support/BinaryByteStream.h"
2021
#include "llvm/Support/Debug.h"
@@ -119,8 +120,9 @@ std::unique_ptr<jitlink::LinkGraph> createPlatformGraph(MachOPlatform &MOP,
119120
class MachOHeaderMaterializationUnit : public MaterializationUnit {
120121
public:
121122
MachOHeaderMaterializationUnit(MachOPlatform &MOP,
122-
const SymbolStringPtr &HeaderStartSymbol)
123-
: MaterializationUnit(createHeaderInterface(MOP, HeaderStartSymbol)),
123+
SymbolStringPtr HeaderStartSymbol)
124+
: MaterializationUnit(
125+
createHeaderInterface(MOP, std::move(HeaderStartSymbol))),
124126
MOP(MOP) {}
125127

126128
StringRef getName() const override { return "MachOHeaderMU"; }
@@ -348,10 +350,17 @@ struct ObjCImageInfoFlags {
348350
namespace llvm {
349351
namespace orc {
350352

353+
std::unique_ptr<MaterializationUnit>
354+
MachOPlatform::defaultMachOHeaderBuilder(MachOPlatform &MOP) {
355+
return std::make_unique<MachOHeaderMaterializationUnit>(
356+
MOP, SymbolStringPtr(MOP.getMachOHeaderStartSymbol()));
357+
}
358+
351359
Expected<std::unique_ptr<MachOPlatform>>
352360
MachOPlatform::Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
353361
JITDylib &PlatformJD,
354362
std::unique_ptr<DefinitionGenerator> OrcRuntime,
363+
MachOHeaderMUBuilder BuildMachOHeaderMU,
355364
std::optional<SymbolAliasMap> RuntimeAliases) {
356365

357366
// If the target is not supported then bail out immediately.
@@ -382,8 +391,9 @@ MachOPlatform::Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
382391

383392
// Create the instance.
384393
Error Err = Error::success();
385-
auto P = std::unique_ptr<MachOPlatform>(new MachOPlatform(
386-
ES, ObjLinkingLayer, PlatformJD, std::move(OrcRuntime), Err));
394+
auto P = std::unique_ptr<MachOPlatform>(
395+
new MachOPlatform(ES, ObjLinkingLayer, PlatformJD, std::move(OrcRuntime),
396+
std::move(BuildMachOHeaderMU), Err));
387397
if (Err)
388398
return std::move(Err);
389399
return std::move(P);
@@ -392,6 +402,7 @@ MachOPlatform::Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
392402
Expected<std::unique_ptr<MachOPlatform>>
393403
MachOPlatform::Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
394404
JITDylib &PlatformJD, const char *OrcRuntimePath,
405+
MachOHeaderMUBuilder BuildMachOHeaderMU,
395406
std::optional<SymbolAliasMap> RuntimeAliases) {
396407

397408
// Create a generator for the ORC runtime archive.
@@ -402,12 +413,11 @@ MachOPlatform::Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
402413

403414
return Create(ES, ObjLinkingLayer, PlatformJD,
404415
std::move(*OrcRuntimeArchiveGenerator),
405-
std::move(RuntimeAliases));
416+
std::move(BuildMachOHeaderMU), std::move(RuntimeAliases));
406417
}
407418

408419
Error MachOPlatform::setupJITDylib(JITDylib &JD) {
409-
if (auto Err = JD.define(std::make_unique<MachOHeaderMaterializationUnit>(
410-
*this, MachOHeaderStartSymbol)))
420+
if (auto Err = JD.define(BuildMachOHeaderMU(*this)))
411421
return Err;
412422

413423
return ES.lookup({&JD}, MachOHeaderStartSymbol).takeError();
@@ -522,8 +532,10 @@ MachOPlatform::flagsForSymbol(jitlink::Symbol &Sym) {
522532
MachOPlatform::MachOPlatform(
523533
ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
524534
JITDylib &PlatformJD,
525-
std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator, Error &Err)
526-
: ES(ES), PlatformJD(PlatformJD), ObjLinkingLayer(ObjLinkingLayer) {
535+
std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator,
536+
MachOHeaderMUBuilder BuildMachOHeaderMU, Error &Err)
537+
: ES(ES), PlatformJD(PlatformJD), ObjLinkingLayer(ObjLinkingLayer),
538+
BuildMachOHeaderMU(std::move(BuildMachOHeaderMU)) {
527539
ErrorAsOutParameter _(&Err);
528540
ObjLinkingLayer.addPlugin(std::make_unique<MachOPlatformPlugin>(*this));
529541
PlatformJD.addGenerator(std::move(OrcRuntimeGenerator));

0 commit comments

Comments
 (0)