[ORC] Specialize MachOBuilder support for the LC_ID_DYLIB command.

Provides a natural API for adding LC_ID_DYLIB commands, including the arbitrary
install name.
This commit is contained in:
Lang Hames 2024-01-12 10:20:38 -08:00 committed by Ben Langmuir
parent b06bc7c6a0
commit ab244b64df

View File

@ -43,20 +43,20 @@ struct MachOBuilderLoadCommandBase {
};
/// MachOBuilder load command wrapper type.
template <MachO::LoadCommandType LCType> struct MachOBuilderLoadCommand;
template <MachO::LoadCommandType LCType> struct MachOBuilderLoadCommandImplBase;
#define HANDLE_LOAD_COMMAND(Name, Value, LCStruct) \
template <> \
struct MachOBuilderLoadCommand<MachO::Name> \
struct MachOBuilderLoadCommandImplBase<MachO::Name> \
: public MachO::LCStruct, public MachOBuilderLoadCommandBase { \
using CmdStruct = LCStruct; \
MachOBuilderLoadCommand() { \
MachOBuilderLoadCommandImplBase() { \
memset(&rawStruct(), 0, sizeof(CmdStruct)); \
cmd = Value; \
cmdsize = sizeof(CmdStruct); \
} \
template <typename... ArgTs> \
MachOBuilderLoadCommand(ArgTs &&...Args) \
MachOBuilderLoadCommandImplBase(ArgTs &&...Args) \
: CmdStruct{Value, sizeof(CmdStruct), std::forward<ArgTs>(Args)...} {} \
CmdStruct &rawStruct() { return static_cast<CmdStruct &>(*this); } \
size_t size() const override { return cmdsize; } \
@ -70,6 +70,40 @@ template <MachO::LoadCommandType LCType> struct MachOBuilderLoadCommand;
#undef HANDLE_LOAD_COMMAND
template <MachO::LoadCommandType LCType>
struct MachOBuilderLoadCommand
: public MachOBuilderLoadCommandImplBase<LCType> {
public:
MachOBuilderLoadCommand() = default;
template <typename... ArgTs>
MachOBuilderLoadCommand(ArgTs &&...Args)
: MachOBuilderLoadCommand(std::forward<ArgTs>(Args)...) {}
};
template <>
struct MachOBuilderLoadCommand<MachO::LC_ID_DYLIB>
: public MachOBuilderLoadCommandImplBase<MachO::LC_ID_DYLIB> {
MachOBuilderLoadCommand(std::string Name, uint32_t Timestamp,
uint32_t CurrentVersion,
uint32_t CompatibilityVersion)
: MachOBuilderLoadCommandImplBase(
MachO::dylib{24, Timestamp, CurrentVersion, CompatibilityVersion}),
Name(std::move(Name)) {
cmdsize += (this->Name.size() + 1 + 3) & ~0x3;
}
size_t write(MutableArrayRef<char> Buf, size_t Offset,
bool SwapStruct) override {
Offset = writeMachOStruct(Buf, Offset, rawStruct(), SwapStruct);
strcpy(Buf.data() + Offset, Name.data());
return Offset + ((Name.size() + 1 + 3) & ~0x3);
}
std::string Name;
};
// Builds MachO objects.
template <typename MachOTraits> class MachOBuilder {
private: