diff --git a/llvm/include/llvm/ExecutionEngine/Orc/MachOBuilder.h b/llvm/include/llvm/ExecutionEngine/Orc/MachOBuilder.h index 2bc66b11e270..d6dcdabab0e5 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/MachOBuilder.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/MachOBuilder.h @@ -43,20 +43,20 @@ struct MachOBuilderLoadCommandBase { }; /// MachOBuilder load command wrapper type. -template struct MachOBuilderLoadCommand; +template struct MachOBuilderLoadCommandImplBase; #define HANDLE_LOAD_COMMAND(Name, Value, LCStruct) \ template <> \ - struct MachOBuilderLoadCommand \ + struct MachOBuilderLoadCommandImplBase \ : public MachO::LCStruct, public MachOBuilderLoadCommandBase { \ using CmdStruct = LCStruct; \ - MachOBuilderLoadCommand() { \ + MachOBuilderLoadCommandImplBase() { \ memset(&rawStruct(), 0, sizeof(CmdStruct)); \ cmd = Value; \ cmdsize = sizeof(CmdStruct); \ } \ template \ - MachOBuilderLoadCommand(ArgTs &&...Args) \ + MachOBuilderLoadCommandImplBase(ArgTs &&...Args) \ : CmdStruct{Value, sizeof(CmdStruct), std::forward(Args)...} {} \ CmdStruct &rawStruct() { return static_cast(*this); } \ size_t size() const override { return cmdsize; } \ @@ -70,6 +70,40 @@ template struct MachOBuilderLoadCommand; #undef HANDLE_LOAD_COMMAND +template +struct MachOBuilderLoadCommand + : public MachOBuilderLoadCommandImplBase { +public: + MachOBuilderLoadCommand() = default; + + template + MachOBuilderLoadCommand(ArgTs &&...Args) + : MachOBuilderLoadCommand(std::forward(Args)...) {} +}; + +template <> +struct MachOBuilderLoadCommand + : public MachOBuilderLoadCommandImplBase { + + 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 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 class MachOBuilder { private: