Skip to content

Commit 7256c91

Browse files
committed
[JITLink][MachO] Add getOrCreateLocalMachOHeader utility.
This function can be called on a LinkGraph to get an anonymous symbol pointing to the start of a mach_header / mach_header_64 block with suitable cputype and cpusubtype values for the LinkGraph, and with filetype = MachO::MH_OBJECT. This functionality will be used in the upcoming compact-unwind support patch: For graphs that do not have a suitable existing header to use (indicated by the presence of a "__jitlink$libunwind_dso_base" symbol) the compact-unwind support plugin will create a local header to use as the dso-base to report to libunwind.
1 parent 527c030 commit 7256c91

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,10 @@ class Section {
754754
/// Returns the ordinal for this section.
755755
SectionOrdinal getOrdinal() const { return SecOrdinal; }
756756

757+
/// Set the ordinal for this section. Ordinals are used to order the layout
758+
/// of sections with the same permissions.
759+
void setOrdinal(SectionOrdinal SecOrdinal) { this->SecOrdinal = SecOrdinal; }
760+
757761
/// Returns true if this section is empty (contains no blocks or symbols).
758762
bool empty() const { return Blocks.empty(); }
759763

llvm/include/llvm/ExecutionEngine/JITLink/MachO.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ inline Section &getMachODefaultTextSection(LinkGraph &G) {
5555
orc::MemProt::Read | orc::MemProt::Exec);
5656
}
5757

58+
/// Gets or creates a MachO header for the current LinkGraph.
59+
Expected<Symbol &> getOrCreateLocalMachOHeader(LinkGraph &G);
60+
5861
} // end namespace jitlink
5962
} // end namespace llvm
6063

llvm/lib/ExecutionEngine/JITLink/MachO.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,67 @@ void link_MachO(std::unique_ptr<LinkGraph> G,
8686
}
8787
}
8888

89+
template <typename MachOHeaderType>
90+
static Expected<Block &> createLocalHeaderBlock(LinkGraph &G, Section &Sec) {
91+
auto &B = G.createMutableContentBlock(Sec, sizeof(MachOHeaderType),
92+
orc::ExecutorAddr(), 8, 0, true);
93+
MachOHeaderType Hdr;
94+
Hdr.magic = G.getPointerSize() == 4 ? MachO::MH_MAGIC : MachO::MH_MAGIC_64;
95+
if (auto CPUType = MachO::getCPUType(G.getTargetTriple()))
96+
Hdr.cputype = *CPUType;
97+
else
98+
return CPUType.takeError();
99+
if (auto CPUSubType = MachO::getCPUSubType(G.getTargetTriple()))
100+
Hdr.cpusubtype = *CPUSubType;
101+
else
102+
return CPUSubType.takeError();
103+
Hdr.filetype = MachO::MH_OBJECT;
104+
105+
if (G.getEndianness() != endianness::native)
106+
MachO::swapStruct(Hdr);
107+
108+
memcpy(B.getAlreadyMutableContent().data(), &Hdr, sizeof(Hdr));
109+
110+
return B;
111+
}
112+
113+
Expected<Symbol &> getOrCreateLocalMachOHeader(LinkGraph &G) {
114+
StringRef LocalHeaderSectionName("__TEXT,__lcl_macho_hdr");
115+
Section *Sec = G.findSectionByName(LocalHeaderSectionName);
116+
if (Sec) {
117+
assert(Sec->blocks_size() == 1 && "Unexpected number of blocks");
118+
assert(Sec->symbols_size() == 1 && "Unexpected number of symbols");
119+
auto &Sym = **Sec->symbols().begin();
120+
assert(Sym.getOffset() == 0 && "Symbol not at start of header block");
121+
return Sym;
122+
}
123+
124+
// Create the local header section, move all other sections up in the
125+
// section ordering to ensure that it's laid out first.
126+
for (auto &Sec : G.sections())
127+
Sec.setOrdinal(Sec.getOrdinal() + 1);
128+
129+
Sec = &G.createSection(LocalHeaderSectionName, orc::MemProt::Read);
130+
131+
Sec->setOrdinal(0);
132+
133+
Block *B = nullptr;
134+
switch (G.getTargetTriple().getArch()) {
135+
case Triple::aarch64:
136+
case Triple::x86_64:
137+
if (auto BOrErr = createLocalHeaderBlock<MachO::mach_header_64>(G, *Sec))
138+
B = &*BOrErr;
139+
else
140+
return BOrErr.takeError();
141+
break;
142+
default:
143+
return make_error<JITLinkError>("Cannot create local Mach-O header for " +
144+
G.getName() + ": unsupported triple " +
145+
G.getTargetTriple().str());
146+
}
147+
148+
return G.addAnonymousSymbol(*B, 0, B->getSize(), false, false);
149+
}
150+
89151
} // end namespace jitlink
90152
} // end namespace llvm

0 commit comments

Comments
 (0)