[llvm] 7256c91 - [JITLink][MachO] Add getOrCreateLocalMachOHeader utility.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 21 21:51:47 PST 2025
Author: Lang Hames
Date: 2025-01-22T16:51:40+11:00
New Revision: 7256c91ad29c1407320d5949414fd4736d1f2644
URL: https://github.com/llvm/llvm-project/commit/7256c91ad29c1407320d5949414fd4736d1f2644
DIFF: https://github.com/llvm/llvm-project/commit/7256c91ad29c1407320d5949414fd4736d1f2644.diff
LOG: [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.
Added:
Modified:
llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
llvm/include/llvm/ExecutionEngine/JITLink/MachO.h
llvm/lib/ExecutionEngine/JITLink/MachO.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
index dd3d6012d682ca..297e603164b244 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
@@ -754,6 +754,10 @@ class Section {
/// Returns the ordinal for this section.
SectionOrdinal getOrdinal() const { return SecOrdinal; }
+ /// Set the ordinal for this section. Ordinals are used to order the layout
+ /// of sections with the same permissions.
+ void setOrdinal(SectionOrdinal SecOrdinal) { this->SecOrdinal = SecOrdinal; }
+
/// Returns true if this section is empty (contains no blocks or symbols).
bool empty() const { return Blocks.empty(); }
diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/MachO.h b/llvm/include/llvm/ExecutionEngine/JITLink/MachO.h
index 2010b32cdf7660..b3bf96b8549f29 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/MachO.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/MachO.h
@@ -55,6 +55,9 @@ inline Section &getMachODefaultTextSection(LinkGraph &G) {
orc::MemProt::Read | orc::MemProt::Exec);
}
+/// Gets or creates a MachO header for the current LinkGraph.
+Expected<Symbol &> getOrCreateLocalMachOHeader(LinkGraph &G);
+
} // end namespace jitlink
} // end namespace llvm
diff --git a/llvm/lib/ExecutionEngine/JITLink/MachO.cpp b/llvm/lib/ExecutionEngine/JITLink/MachO.cpp
index b9cfb8fb8fa962..eeccc87f8dcc3e 100644
--- a/llvm/lib/ExecutionEngine/JITLink/MachO.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/MachO.cpp
@@ -86,5 +86,67 @@ void link_MachO(std::unique_ptr<LinkGraph> G,
}
}
+template <typename MachOHeaderType>
+static Expected<Block &> createLocalHeaderBlock(LinkGraph &G, Section &Sec) {
+ auto &B = G.createMutableContentBlock(Sec, sizeof(MachOHeaderType),
+ orc::ExecutorAddr(), 8, 0, true);
+ MachOHeaderType Hdr;
+ Hdr.magic = G.getPointerSize() == 4 ? MachO::MH_MAGIC : MachO::MH_MAGIC_64;
+ if (auto CPUType = MachO::getCPUType(G.getTargetTriple()))
+ Hdr.cputype = *CPUType;
+ else
+ return CPUType.takeError();
+ if (auto CPUSubType = MachO::getCPUSubType(G.getTargetTriple()))
+ Hdr.cpusubtype = *CPUSubType;
+ else
+ return CPUSubType.takeError();
+ Hdr.filetype = MachO::MH_OBJECT;
+
+ if (G.getEndianness() != endianness::native)
+ MachO::swapStruct(Hdr);
+
+ memcpy(B.getAlreadyMutableContent().data(), &Hdr, sizeof(Hdr));
+
+ return B;
+}
+
+Expected<Symbol &> getOrCreateLocalMachOHeader(LinkGraph &G) {
+ StringRef LocalHeaderSectionName("__TEXT,__lcl_macho_hdr");
+ Section *Sec = G.findSectionByName(LocalHeaderSectionName);
+ if (Sec) {
+ assert(Sec->blocks_size() == 1 && "Unexpected number of blocks");
+ assert(Sec->symbols_size() == 1 && "Unexpected number of symbols");
+ auto &Sym = **Sec->symbols().begin();
+ assert(Sym.getOffset() == 0 && "Symbol not at start of header block");
+ return Sym;
+ }
+
+ // Create the local header section, move all other sections up in the
+ // section ordering to ensure that it's laid out first.
+ for (auto &Sec : G.sections())
+ Sec.setOrdinal(Sec.getOrdinal() + 1);
+
+ Sec = &G.createSection(LocalHeaderSectionName, orc::MemProt::Read);
+
+ Sec->setOrdinal(0);
+
+ Block *B = nullptr;
+ switch (G.getTargetTriple().getArch()) {
+ case Triple::aarch64:
+ case Triple::x86_64:
+ if (auto BOrErr = createLocalHeaderBlock<MachO::mach_header_64>(G, *Sec))
+ B = &*BOrErr;
+ else
+ return BOrErr.takeError();
+ break;
+ default:
+ return make_error<JITLinkError>("Cannot create local Mach-O header for " +
+ G.getName() + ": unsupported triple " +
+ G.getTargetTriple().str());
+ }
+
+ return G.addAnonymousSymbol(*B, 0, B->getSize(), false, false);
+}
+
} // end namespace jitlink
} // end namespace llvm
More information about the llvm-commits
mailing list