Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/JITLink/PerGraphGOTAndPLTStubsBuilder.h
35271 views
//===--------------- PerGraphGOTAndPLTStubBuilder.h -------------*- 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// Construct GOT and PLT entries for each graph.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_EXECUTIONENGINE_JITLINK_PERGRAPHGOTANDPLTSTUBSBUILDER_H13#define LLVM_EXECUTIONENGINE_JITLINK_PERGRAPHGOTANDPLTSTUBSBUILDER_H1415#include "llvm/ExecutionEngine/JITLink/JITLink.h"16#include "llvm/Support/Debug.h"1718#define DEBUG_TYPE "jitlink"1920namespace llvm {21namespace jitlink {2223/// Per-object GOT and PLT Stub builder.24///25/// Constructs GOT entries and PLT stubs in every graph for referenced symbols.26/// Building these blocks in every graph is likely to lead to duplicate entries27/// in the JITLinkDylib, but allows graphs to be trivially removed independently28/// without affecting other graphs (since those other graphs will have their own29/// copies of any required entries).30template <typename BuilderImplT>31class PerGraphGOTAndPLTStubsBuilder {32public:33PerGraphGOTAndPLTStubsBuilder(LinkGraph &G) : G(G) {}3435static Error asPass(LinkGraph &G) { return BuilderImplT(G).run(); }3637Error run() {38LLVM_DEBUG(dbgs() << "Running Per-Graph GOT and Stubs builder:\n");3940// We're going to be adding new blocks, but we don't want to iterate over41// the new ones, so build a worklist.42std::vector<Block *> Worklist(G.blocks().begin(), G.blocks().end());4344for (auto *B : Worklist)45for (auto &E : B->edges()) {46if (impl().isGOTEdgeToFix(E)) {47LLVM_DEBUG({48dbgs() << " Fixing " << G.getEdgeKindName(E.getKind())49<< " edge at " << B->getFixupAddress(E) << " ("50<< B->getAddress() << " + "51<< formatv("{0:x}", E.getOffset()) << ")\n";52});53impl().fixGOTEdge(E, getGOTEntry(E.getTarget()));54} else if (impl().isExternalBranchEdge(E)) {55LLVM_DEBUG({56dbgs() << " Fixing " << G.getEdgeKindName(E.getKind())57<< " edge at " << B->getFixupAddress(E) << " ("58<< B->getAddress() << " + "59<< formatv("{0:x}", E.getOffset()) << ")\n";60});61impl().fixPLTEdge(E, getPLTStub(E.getTarget()));62}63}6465return Error::success();66}6768protected:69Symbol &getGOTEntry(Symbol &Target) {70assert(Target.hasName() && "GOT edge cannot point to anonymous target");7172auto GOTEntryI = GOTEntries.find(Target.getName());7374// Build the entry if it doesn't exist.75if (GOTEntryI == GOTEntries.end()) {76auto &GOTEntry = impl().createGOTEntry(Target);77LLVM_DEBUG({78dbgs() << " Created GOT entry for " << Target.getName() << ": "79<< GOTEntry << "\n";80});81GOTEntryI =82GOTEntries.insert(std::make_pair(Target.getName(), &GOTEntry)).first;83}8485assert(GOTEntryI != GOTEntries.end() && "Could not get GOT entry symbol");86LLVM_DEBUG(87{ dbgs() << " Using GOT entry " << *GOTEntryI->second << "\n"; });88return *GOTEntryI->second;89}9091Symbol &getPLTStub(Symbol &Target) {92assert(Target.hasName() &&93"External branch edge can not point to an anonymous target");94auto StubI = PLTStubs.find(Target.getName());9596if (StubI == PLTStubs.end()) {97auto &StubSymbol = impl().createPLTStub(Target);98LLVM_DEBUG({99dbgs() << " Created PLT stub for " << Target.getName() << ": "100<< StubSymbol << "\n";101});102StubI =103PLTStubs.insert(std::make_pair(Target.getName(), &StubSymbol)).first;104}105106assert(StubI != PLTStubs.end() && "Count not get stub symbol");107LLVM_DEBUG({ dbgs() << " Using PLT stub " << *StubI->second << "\n"; });108return *StubI->second;109}110111LinkGraph &G;112113private:114BuilderImplT &impl() { return static_cast<BuilderImplT &>(*this); }115116DenseMap<StringRef, Symbol *> GOTEntries;117DenseMap<StringRef, Symbol *> PLTStubs;118};119120} // end namespace jitlink121} // end namespace llvm122123#undef DEBUG_TYPE124125#endif // LLVM_EXECUTIONENGINE_JITLINK_PERGRAPHGOTANDPLTSTUBSBUILDER_H126127128