mirror of
https://github.com/intel/llvm.git
synced 2026-01-19 01:15:50 +08:00
RuntimeDyld has been deprecated in favor of JITLink. [1] This patch replaces all uses of RuntimeDyld in BOLT with JITLink. Care has been taken to minimize the impact on the code structure in order to ease the inspection of this (rather large) changeset. Since BOLT relied on the RuntimeDyld API in multiple places, this wasn't always possible though and I'll explain the changes in code structure first. Design note: BOLT uses a JIT linker to perform what essentially is static linking. No linked code is ever executed; the result of linking is simply written back to an executable file. For this reason, I restricted myself to the use of the core JITLink library and avoided ORC as much as possible. RuntimeDyld contains methods for loading objects (loadObject) and symbol lookup (getSymbol). Since JITLink doesn't provide a class with a similar interface, the BOLTLinker abstract class was added to implement it. It was added to Core since both the Rewrite and RuntimeLibs libraries make use of it. Wherever a RuntimeDyld object was used before, it was replaced with a BOLTLinker object. There is one major difference between the RuntimeDyld and BOLTLinker interfaces: in JITLink, section allocation and the application of fixups (relocation) happens in a single call (jitlink::link). That is, there is no separate method like finalizeWithMemoryManagerLocking in RuntimeDyld. BOLT used to remap sections between allocating (loadObject) and linking them (finalizeWithMemoryManagerLocking). This doesn't work anymore with JITLink. Instead, BOLTLinker::loadObject accepts a callback that is called before fixups are applied which is used to remap sections. The actual implementation of the BOLTLinker interface lives in the JITLinkLinker class in the Rewrite library. It's the only part of the BOLT code that should directly interact with the JITLink API. For loading object, JITLinkLinker first creates a LinkGraph (jitlink::createLinkGraphFromObject) and then links it (jitlink::link). For the latter, it uses a custom JITLinkContext with the following properties: - Use BOLT's ExecutableFileMemoryManager. This one was updated to implement the JITLinkMemoryManager interface. Since BOLT never executes code, its finalization step is a no-op. - Pass config: don't use the default target passes since they modify DWARF sections in a way that seems incompatible with BOLT. Also run a custom pre-prune pass that makes sure sections without symbols are not pruned by JITLink. - Implement symbol lookup. This used to be implemented by BOLTSymbolResolver. - Call the section mapper callback before the final linking step. - Copy symbol values when the LinkGraph is resolved. Symbols are stored inside JITLinkLinker to ensure that later objects (i.e., instrumentation libraries) can find them. This functionality used to be provided by RuntimeDyld but I did not find a way to use JITLink directly for this. Some more minor points of interest: - BinarySection::SectionID: JITLink doesn't have something equivalent to RuntimeDyld's Section IDs. Instead, sections can only be referred to by name. Hence, SectionID was updated to a string. - There seem to be no tests for Mach-O. I've tested a small hello-world style binary but not more than that. - On Mach-O, JITLink "normalizes" section names to include the segment name. I had to parse the section name back from this manually which feels slightly hacky. [1] https://reviews.llvm.org/D145686#4222642 Reviewed By: rafauler Differential Revision: https://reviews.llvm.org/D147544
207 lines
6.7 KiB
C++
207 lines
6.7 KiB
C++
//===- bolt/Rewrite/JITLinkLinker.cpp - BOLTLinker using JITLink ----------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
#include "bolt/Rewrite/JITLinkLinker.h"
|
|
#include "bolt/Core/BinaryData.h"
|
|
#include "bolt/Rewrite/RewriteInstance.h"
|
|
#include "llvm/ExecutionEngine/JITLink/JITLink.h"
|
|
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
|
|
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h"
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#define DEBUG_TYPE "bolt"
|
|
|
|
namespace llvm {
|
|
namespace bolt {
|
|
|
|
namespace {
|
|
|
|
bool hasSymbols(const jitlink::Block &B) {
|
|
return llvm::any_of(B.getSection().symbols(),
|
|
[&B](const auto &S) { return &S->getBlock() == &B; });
|
|
}
|
|
|
|
/// Liveness in JITLink is based on symbols so sections that do not contain
|
|
/// any symbols will always be pruned. This pass adds anonymous symbols to
|
|
/// needed sections to prevent pruning.
|
|
Error markSectionsLive(jitlink::LinkGraph &G) {
|
|
for (auto &Section : G.sections()) {
|
|
// We only need allocatable sections.
|
|
if (Section.getMemLifetimePolicy() == orc::MemLifetimePolicy::NoAlloc)
|
|
continue;
|
|
|
|
// Skip empty sections.
|
|
if (JITLinkLinker::sectionSize(Section) == 0)
|
|
continue;
|
|
|
|
for (auto *Block : Section.blocks()) {
|
|
// No need to add symbols if it already has some.
|
|
if (hasSymbols(*Block))
|
|
continue;
|
|
|
|
G.addAnonymousSymbol(*Block, /*Offset=*/0, /*Size=*/0,
|
|
/*IsCallable=*/false, /*IsLive=*/true);
|
|
}
|
|
}
|
|
|
|
return jitlink::markAllSymbolsLive(G);
|
|
}
|
|
|
|
void reassignSectionAddress(jitlink::LinkGraph &LG,
|
|
const BinarySection &BinSection, uint64_t Address) {
|
|
auto *JLSection = LG.findSectionByName(BinSection.getSectionID());
|
|
assert(JLSection && "cannot find section in LinkGraph");
|
|
|
|
auto BlockAddress = Address;
|
|
for (auto *Block : JITLinkLinker::orderedBlocks(*JLSection)) {
|
|
// FIXME it would seem to make sense to align here. However, in
|
|
// non-relocation mode, we simply use the original address of functions
|
|
// which might not be aligned with the minimum alignment used by
|
|
// BinaryFunction (2). Example failing test when aligning:
|
|
// bolt/test/X86/addr32.s
|
|
Block->setAddress(orc::ExecutorAddr(BlockAddress));
|
|
BlockAddress += Block->getSize();
|
|
}
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
struct JITLinkLinker::Context : jitlink::JITLinkContext {
|
|
JITLinkLinker &Linker;
|
|
JITLinkLinker::SectionsMapper MapSections;
|
|
|
|
Context(JITLinkLinker &Linker, JITLinkLinker::SectionsMapper MapSections)
|
|
: JITLinkContext(&Linker.Dylib), Linker(Linker),
|
|
MapSections(MapSections) {}
|
|
|
|
jitlink::JITLinkMemoryManager &getMemoryManager() override {
|
|
return *Linker.MM;
|
|
}
|
|
|
|
bool shouldAddDefaultTargetPasses(const Triple &TT) const override {
|
|
// The default passes manipulate DWARF sections in a way incompatible with
|
|
// BOLT.
|
|
// TODO check if we can actually use these passes to remove some of the
|
|
// DWARF manipulation done in BOLT.
|
|
return false;
|
|
}
|
|
|
|
Error modifyPassConfig(jitlink::LinkGraph &G,
|
|
jitlink::PassConfiguration &Config) override {
|
|
Config.PrePrunePasses.push_back(markSectionsLive);
|
|
return Error::success();
|
|
}
|
|
|
|
void notifyFailed(Error Err) override {
|
|
errs() << "BOLT-ERROR: JITLink failed: " << Err << '\n';
|
|
exit(1);
|
|
}
|
|
|
|
void
|
|
lookup(const LookupMap &Symbols,
|
|
std::unique_ptr<jitlink::JITLinkAsyncLookupContinuation> LC) override {
|
|
jitlink::AsyncLookupResult AllResults;
|
|
|
|
for (const auto &Symbol : Symbols) {
|
|
std::string SymName = Symbol.first.str();
|
|
LLVM_DEBUG(dbgs() << "BOLT: looking for " << SymName << "\n");
|
|
|
|
if (auto Address = Linker.lookupSymbol(SymName)) {
|
|
LLVM_DEBUG(dbgs() << "Resolved to address 0x"
|
|
<< Twine::utohexstr(*Address) << "\n");
|
|
AllResults[Symbol.first] = orc::ExecutorSymbolDef(
|
|
orc::ExecutorAddr(*Address), JITSymbolFlags());
|
|
continue;
|
|
}
|
|
|
|
if (const BinaryData *I = Linker.BC.getBinaryDataByName(SymName)) {
|
|
uint64_t Address = I->isMoved() && !I->isJumpTable()
|
|
? I->getOutputAddress()
|
|
: I->getAddress();
|
|
LLVM_DEBUG(dbgs() << "Resolved to address 0x"
|
|
<< Twine::utohexstr(Address) << "\n");
|
|
AllResults[Symbol.first] = orc::ExecutorSymbolDef(
|
|
orc::ExecutorAddr(Address), JITSymbolFlags());
|
|
continue;
|
|
}
|
|
LLVM_DEBUG(dbgs() << "Resolved to address 0x0\n");
|
|
AllResults[Symbol.first] =
|
|
orc::ExecutorSymbolDef(orc::ExecutorAddr(0), JITSymbolFlags());
|
|
}
|
|
|
|
LC->run(std::move(AllResults));
|
|
}
|
|
|
|
Error notifyResolved(jitlink::LinkGraph &G) override {
|
|
MapSections([&G](const BinarySection &Section, uint64_t Address) {
|
|
reassignSectionAddress(G, Section, Address);
|
|
});
|
|
|
|
for (auto *Symbol : G.defined_symbols()) {
|
|
Linker.Symtab.insert(
|
|
{Symbol->getName().str(), Symbol->getAddress().getValue()});
|
|
}
|
|
|
|
return Error::success();
|
|
}
|
|
|
|
void notifyFinalized(
|
|
jitlink::JITLinkMemoryManager::FinalizedAlloc Alloc) override {
|
|
Linker.Allocs.push_back(std::move(Alloc));
|
|
++Linker.MM->ObjectsLoaded;
|
|
}
|
|
};
|
|
|
|
JITLinkLinker::JITLinkLinker(BinaryContext &BC,
|
|
std::unique_ptr<ExecutableFileMemoryManager> MM)
|
|
: BC(BC), MM(std::move(MM)) {}
|
|
|
|
JITLinkLinker::~JITLinkLinker() { cantFail(MM->deallocate(std::move(Allocs))); }
|
|
|
|
void JITLinkLinker::loadObject(MemoryBufferRef Obj,
|
|
SectionsMapper MapSections) {
|
|
auto LG = jitlink::createLinkGraphFromObject(Obj);
|
|
if (auto E = LG.takeError()) {
|
|
errs() << "BOLT-ERROR: JITLink failed: " << E << '\n';
|
|
exit(1);
|
|
}
|
|
|
|
auto Ctx = std::make_unique<Context>(*this, MapSections);
|
|
jitlink::link(std::move(*LG), std::move(Ctx));
|
|
}
|
|
|
|
std::optional<uint64_t> JITLinkLinker::lookupSymbol(StringRef Name) const {
|
|
auto It = Symtab.find(Name.data());
|
|
if (It == Symtab.end())
|
|
return std::nullopt;
|
|
|
|
return It->second;
|
|
}
|
|
|
|
SmallVector<jitlink::Block *, 2>
|
|
JITLinkLinker::orderedBlocks(const jitlink::Section &Section) {
|
|
SmallVector<jitlink::Block *, 2> Blocks(Section.blocks());
|
|
llvm::sort(Blocks, [](const auto *LHS, const auto *RHS) {
|
|
return LHS->getAddress() < RHS->getAddress();
|
|
});
|
|
return Blocks;
|
|
}
|
|
|
|
size_t JITLinkLinker::sectionSize(const jitlink::Section &Section) {
|
|
size_t Size = 0;
|
|
|
|
for (const auto *Block : orderedBlocks(Section)) {
|
|
Size = jitlink::alignToBlock(Size, *Block);
|
|
Size += Block->getSize();
|
|
}
|
|
|
|
return Size;
|
|
}
|
|
|
|
} // namespace bolt
|
|
} // namespace llvm
|