Skip to content

Commit 12c7908

Browse files
committed
[ORC] De-duplicate some logic for handling MachO::dylib-based load commands.
All such commands share a common struct layout, and we'll be introducing another soon (LC_LOAD_WEAK_DYLIB). To avoid redundant specializations this commit moves the logic for these commands into a common base class.
1 parent a2fac3f commit 12c7908

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

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

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,22 @@ struct MachOBuilderLoadCommand
8181
: MachOBuilderLoadCommandImplBase<LCType>(std::forward<ArgTs>(Args)...) {}
8282
};
8383

84-
template <>
85-
struct MachOBuilderLoadCommand<MachO::LC_ID_DYLIB>
86-
: public MachOBuilderLoadCommandImplBase<MachO::LC_ID_DYLIB> {
84+
template <MachO::LoadCommandType LCType>
85+
struct MachOBuilderDylibLoadCommand
86+
: public MachOBuilderLoadCommandImplBase<LCType> {
8787

88-
MachOBuilderLoadCommand(std::string Name, uint32_t Timestamp,
89-
uint32_t CurrentVersion,
90-
uint32_t CompatibilityVersion)
91-
: MachOBuilderLoadCommandImplBase(
88+
MachOBuilderDylibLoadCommand(std::string Name, uint32_t Timestamp,
89+
uint32_t CurrentVersion,
90+
uint32_t CompatibilityVersion)
91+
: MachOBuilderLoadCommandImplBase<LCType>(
9292
MachO::dylib{24, Timestamp, CurrentVersion, CompatibilityVersion}),
9393
Name(std::move(Name)) {
94-
cmdsize += (this->Name.size() + 1 + 3) & ~0x3;
94+
this->cmdsize += (this->Name.size() + 1 + 3) & ~0x3;
9595
}
9696

9797
size_t write(MutableArrayRef<char> Buf, size_t Offset,
9898
bool SwapStruct) override {
99-
Offset = writeMachOStruct(Buf, Offset, rawStruct(), SwapStruct);
99+
Offset = writeMachOStruct(Buf, Offset, this->rawStruct(), SwapStruct);
100100
strcpy(Buf.data() + Offset, Name.data());
101101
return Offset + ((Name.size() + 1 + 3) & ~0x3);
102102
}
@@ -105,26 +105,15 @@ struct MachOBuilderLoadCommand<MachO::LC_ID_DYLIB>
105105
};
106106

107107
template <>
108-
struct MachOBuilderLoadCommand<MachO::LC_LOAD_DYLIB>
109-
: public MachOBuilderLoadCommandImplBase<MachO::LC_LOAD_DYLIB> {
110-
111-
MachOBuilderLoadCommand(std::string Name, uint32_t Timestamp,
112-
uint32_t CurrentVersion,
113-
uint32_t CompatibilityVersion)
114-
: MachOBuilderLoadCommandImplBase(
115-
MachO::dylib{24, Timestamp, CurrentVersion, CompatibilityVersion}),
116-
Name(std::move(Name)) {
117-
cmdsize += (this->Name.size() + 1 + 3) & ~0x3;
118-
}
119-
120-
size_t write(MutableArrayRef<char> Buf, size_t Offset,
121-
bool SwapStruct) override {
122-
Offset = writeMachOStruct(Buf, Offset, rawStruct(), SwapStruct);
123-
strcpy(Buf.data() + Offset, Name.data());
124-
return Offset + ((Name.size() + 1 + 3) & ~0x3);
125-
}
108+
struct MachOBuilderLoadCommand<MachO::LC_ID_DYLIB>
109+
: public MachOBuilderDylibLoadCommand<MachO::LC_ID_DYLIB> {
110+
using MachOBuilderDylibLoadCommand::MachOBuilderDylibLoadCommand;
111+
};
126112

127-
std::string Name;
113+
template <>
114+
struct MachOBuilderLoadCommand<MachO::LC_LOAD_DYLIB>
115+
: public MachOBuilderDylibLoadCommand<MachO::LC_LOAD_DYLIB> {
116+
using MachOBuilderDylibLoadCommand::MachOBuilderDylibLoadCommand;
128117
};
129118

130119
template <>

0 commit comments

Comments
 (0)