Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/JITLink/JITLinkGeneric.h
35271 views
//===------ JITLinkGeneric.h - Generic JIT linker utilities -----*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Generic JITLinker utilities. E.g. graph pruning, eh-frame parsing.9//10//===----------------------------------------------------------------------===//1112#ifndef LIB_EXECUTIONENGINE_JITLINK_JITLINKGENERIC_H13#define LIB_EXECUTIONENGINE_JITLINK_JITLINKGENERIC_H1415#include "llvm/ExecutionEngine/JITLink/JITLink.h"1617#define DEBUG_TYPE "jitlink"1819namespace llvm {20namespace jitlink {2122/// Base class for a JIT linker.23///24/// A JITLinkerBase instance links one object file into an ongoing JIT25/// session. Symbol resolution and finalization operations are pluggable,26/// and called using continuation passing (passing a continuation for the27/// remaining linker work) to allow them to be performed asynchronously.28class JITLinkerBase {29public:30JITLinkerBase(std::unique_ptr<JITLinkContext> Ctx,31std::unique_ptr<LinkGraph> G, PassConfiguration Passes)32: Ctx(std::move(Ctx)), G(std::move(G)), Passes(std::move(Passes)) {33assert(this->Ctx && "Ctx can not be null");34assert(this->G && "G can not be null");35}3637virtual ~JITLinkerBase();3839protected:40using InFlightAlloc = JITLinkMemoryManager::InFlightAlloc;41using AllocResult = Expected<std::unique_ptr<InFlightAlloc>>;42using FinalizeResult = Expected<JITLinkMemoryManager::FinalizedAlloc>;4344// Returns a reference to the graph being linked.45LinkGraph &getGraph() { return *G; }4647// Returns true if the context says that the linker should add default48// passes. This can be used by JITLinkerBase implementations when deciding49// whether they should add default passes.50bool shouldAddDefaultTargetPasses(const Triple &TT) {51return Ctx->shouldAddDefaultTargetPasses(TT);52}5354// Returns the PassConfiguration for this instance. This can be used by55// JITLinkerBase implementations to add late passes that reference their56// own data structures (e.g. for ELF implementations to locate / construct57// a GOT start symbol prior to fixup).58PassConfiguration &getPassConfig() { return Passes; }5960// Phase 1:61// 1.1: Run pre-prune passes62// 1.2: Prune graph63// 1.3: Run post-prune passes64// 1.4: Allocate memory.65void linkPhase1(std::unique_ptr<JITLinkerBase> Self);6667// Phase 2:68// 2.2: Run post-allocation passes69// 2.3: Notify context of final assigned symbol addresses70// 2.4: Identify external symbols and make an async call to resolve71void linkPhase2(std::unique_ptr<JITLinkerBase> Self, AllocResult AR);7273// Phase 3:74// 3.1: Apply resolution results75// 3.2: Run pre-fixup passes76// 3.3: Fix up block contents77// 3.4: Run post-fixup passes78// 3.5: Make an async call to transfer and finalize memory.79void linkPhase3(std::unique_ptr<JITLinkerBase> Self,80Expected<AsyncLookupResult> LookupResult);8182// Phase 4:83// 4.1: Call OnFinalized callback, handing off allocation.84void linkPhase4(std::unique_ptr<JITLinkerBase> Self, FinalizeResult FR);8586private:87// Run all passes in the given pass list, bailing out immediately if any pass88// returns an error.89Error runPasses(LinkGraphPassList &Passes);9091// Copy block contents and apply relocations.92// Implemented in JITLinker.93virtual Error fixUpBlocks(LinkGraph &G) const = 0;9495JITLinkContext::LookupMap getExternalSymbolNames() const;96void applyLookupResult(AsyncLookupResult LR);97void abandonAllocAndBailOut(std::unique_ptr<JITLinkerBase> Self, Error Err);9899std::unique_ptr<JITLinkContext> Ctx;100std::unique_ptr<LinkGraph> G;101PassConfiguration Passes;102std::unique_ptr<InFlightAlloc> Alloc;103};104105template <typename LinkerImpl> class JITLinker : public JITLinkerBase {106public:107using JITLinkerBase::JITLinkerBase;108109/// Link constructs a LinkerImpl instance and calls linkPhase1.110/// Link should be called with the constructor arguments for LinkerImpl, which111/// will be forwarded to the constructor.112template <typename... ArgTs> static void link(ArgTs &&... Args) {113auto L = std::make_unique<LinkerImpl>(std::forward<ArgTs>(Args)...);114115// Ownership of the linker is passed into the linker's doLink function to116// allow it to be passed on to async continuations.117//118// FIXME: Remove LTmp once we have c++17.119// C++17 sequencing rules guarantee that function name expressions are120// sequenced before arguments, so L->linkPhase1(std::move(L), ...) will be121// well formed.122auto <mp = *L;123LTmp.linkPhase1(std::move(L));124}125126private:127const LinkerImpl &impl() const {128return static_cast<const LinkerImpl &>(*this);129}130131Error fixUpBlocks(LinkGraph &G) const override {132LLVM_DEBUG(dbgs() << "Fixing up blocks:\n");133134for (auto &Sec : G.sections()) {135bool NoAllocSection = Sec.getMemLifetime() == orc::MemLifetime::NoAlloc;136137for (auto *B : Sec.blocks()) {138LLVM_DEBUG(dbgs() << " " << *B << ":\n");139140// Copy Block data and apply fixups.141LLVM_DEBUG(dbgs() << " Applying fixups.\n");142assert((!B->isZeroFill() || all_of(B->edges(),143[](const Edge &E) {144return E.getKind() ==145Edge::KeepAlive;146})) &&147"Non-KeepAlive edges in zero-fill block?");148149// If this is a no-alloc section then copy the block content into150// memory allocated on the Graph's allocator (if it hasn't been151// already).152if (NoAllocSection)153(void)B->getMutableContent(G);154155for (auto &E : B->edges()) {156157// Skip non-relocation edges.158if (!E.isRelocation())159continue;160161// If B is a block in a Standard or Finalize section then make sure162// that no edges point to symbols in NoAlloc sections.163assert((NoAllocSection || !E.getTarget().isDefined() ||164E.getTarget().getBlock().getSection().getMemLifetime() !=165orc::MemLifetime::NoAlloc) &&166"Block in allocated section has edge pointing to no-alloc "167"section");168169// Dispatch to LinkerImpl for fixup.170if (auto Err = impl().applyFixup(G, *B, E))171return Err;172}173}174}175176return Error::success();177}178};179180/// Removes dead symbols/blocks/addressables.181///182/// Finds the set of symbols and addressables reachable from any symbol183/// initially marked live. All symbols/addressables not marked live at the end184/// of this process are removed.185void prune(LinkGraph &G);186187} // end namespace jitlink188} // end namespace llvm189190#undef DEBUG_TYPE // "jitlink"191192#endif // LIB_EXECUTIONENGINE_JITLINK_JITLINKGENERIC_H193194195