Skip to content

Commit e77a473

Browse files
Lang Hameslhames
authored andcommitted
[ORC][MachO] Simplify use of LC_BUILD_VERSION in JITDylib headers.
API clients can now set a MachO::HeaderOptions::BuildVersionOpts field to have MachOPlatform add an LC_BUILD_VERSION load command to the Mach header for each JITDylib. No testcase yet. In the future we'll try to add a MachO parser to the ORC runtime and extra test options to llvm-jitlink for this. This commit also incidentally fixes a bug in the MachOBuilder class that lead to a delegation cycle.
1 parent 58d8805 commit e77a473

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

llvm/include/llvm/ExecutionEngine/Orc/MachOBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct MachOBuilderLoadCommand
7878

7979
template <typename... ArgTs>
8080
MachOBuilderLoadCommand(ArgTs &&...Args)
81-
: MachOBuilderLoadCommand(std::forward<ArgTs>(Args)...) {}
81+
: MachOBuilderLoadCommandImplBase<LCType>(std::forward<ArgTs>(Args)...) {}
8282
};
8383

8484
template <>

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,24 @@ class MachOPlatform : public Platform {
5858
uint32_t CompatibilityVersion;
5959
};
6060

61+
struct BuildVersionOpts {
62+
63+
// Derive platform from triple.
64+
static BuildVersionOpts fromTriple(const Triple &TT, uint32_t MinOS,
65+
uint32_t SDK);
66+
67+
uint32_t Platform; // Platform.
68+
uint32_t MinOS; // X.Y.Z is encoded in nibbles xxxx.yy.zz
69+
uint32_t SDK; // X.Y.Z is encoded in nibbles xxxx.yy.zz
70+
};
71+
6172
/// Override for LC_IC_DYLIB. If this is nullopt, {JD.getName(), 0, 0, 0}
6273
/// will be used.
6374
std::optional<Dylib> IDDylib;
75+
76+
/// Override for LC_BUILD_VERSION. If this is nullopt then
77+
std::optional<BuildVersionOpts> BuildVersion;
78+
6479
/// List of LC_LOAD_DYLIBs.
6580
std::vector<Dylib> LoadDylibs;
6681
/// List of LC_RPATHs.

llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,36 @@ struct ObjCImageInfoFlags {
255255
namespace llvm {
256256
namespace orc {
257257

258+
MachOPlatform::HeaderOptions::BuildVersionOpts
259+
MachOPlatform::HeaderOptions::BuildVersionOpts::fromTriple(const Triple &TT,
260+
uint32_t MinOS,
261+
uint32_t SDK) {
262+
263+
uint32_t Platform;
264+
switch (TT.getOS()) {
265+
case Triple::IOS:
266+
Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_IOS
267+
: MachO::PLATFORM_IOSSIMULATOR;
268+
break;
269+
case Triple::MacOSX:
270+
Platform = MachO::PLATFORM_MACOS;
271+
break;
272+
case Triple::TvOS:
273+
Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_TVOS
274+
: MachO::PLATFORM_TVOSSIMULATOR;
275+
break;
276+
case Triple::WatchOS:
277+
Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_WATCHOS
278+
: MachO::PLATFORM_WATCHOSSIMULATOR;
279+
break;
280+
default:
281+
Platform = MachO::PLATFORM_UNKNOWN;
282+
break;
283+
}
284+
285+
return {Platform, MinOS, SDK};
286+
}
287+
258288
Expected<std::unique_ptr<MachOPlatform>> MachOPlatform::Create(
259289
ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
260290
JITDylib &PlatformJD, std::unique_ptr<DefinitionGenerator> OrcRuntime,
@@ -1695,6 +1725,11 @@ jitlink::Block &createHeaderBlock(MachOPlatform &MOP,
16951725
else
16961726
B.template addLoadCommand<MachO::LC_ID_DYLIB>(JD.getName(), 0, 0, 0);
16971727

1728+
if (Opts.BuildVersion)
1729+
B.template addLoadCommand<MachO::LC_BUILD_VERSION>(
1730+
Opts.BuildVersion->Platform, Opts.BuildVersion->MinOS,
1731+
Opts.BuildVersion->SDK, static_cast<uint32_t>(0));
1732+
16981733
for (auto &D : Opts.LoadDylibs)
16991734
B.template addLoadCommand<MachO::LC_LOAD_DYLIB>(
17001735
D.Name, D.Timestamp, D.CurrentVersion, D.CompatibilityVersion);

0 commit comments

Comments
 (0)