aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm12/lib/ExecutionEngine/JITLink/BasicGOTAndStubsBuilder.h
blob: 82258a35a67525b92a1714c7707e24a38ae4cde9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//===--- BasicGOTAndStubsBuilder.h - Generic GOT/Stub creation --*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// A base for simple GOT and stub creation.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_EXECUTIONENGINE_JITLINK_BASICGOTANDSTUBSBUILDER_H
#define LLVM_LIB_EXECUTIONENGINE_JITLINK_BASICGOTANDSTUBSBUILDER_H

#include "llvm/ExecutionEngine/JITLink/JITLink.h"

#define DEBUG_TYPE "jitlink"

namespace llvm {
namespace jitlink {

template <typename BuilderImpl> class BasicGOTAndStubsBuilder {
public:
  BasicGOTAndStubsBuilder(LinkGraph &G) : G(G) {}

  void run() {
    // We're going to be adding new blocks, but we don't want to iterate over
    // the newly added ones, so just copy the existing blocks out.
    std::vector<Block *> Blocks(G.blocks().begin(), G.blocks().end());

    LLVM_DEBUG(dbgs() << "Creating GOT entries and stubs:\n");

    for (auto *B : Blocks)
      for (auto &E : B->edges())
        if (impl().isGOTEdge(E)) {
          LLVM_DEBUG({
            dbgs() << "  Updating GOT edge ";
            printEdge(dbgs(), *B, E, "<target GOT>");
            dbgs() << "\n";
          });
          impl().fixGOTEdge(E, getGOTEntrySymbol(E.getTarget()));
        } else if (impl().isExternalBranchEdge(E)) {
          LLVM_DEBUG({
            dbgs() << "  Updating external branch edge ";
            printEdge(dbgs(), *B, E, "<target PC-rel>");
            dbgs() << "\n";
          });
          impl().fixExternalBranchEdge(E, getStubSymbol(E.getTarget()));
        }
  }

protected:
  Symbol &getGOTEntrySymbol(Symbol &Target) {
    assert(Target.hasName() && "GOT edge cannot point to anonymous target");

    auto GOTEntryI = GOTEntries.find(Target.getName());

    // Build the entry if it doesn't exist.
    if (GOTEntryI == GOTEntries.end()) {
      auto &GOTEntry = impl().createGOTEntry(Target);
      LLVM_DEBUG({
        dbgs() << "    Created GOT entry for " << Target.getName() << ": "
               << GOTEntry << "\n";
      });
      GOTEntryI =
          GOTEntries.insert(std::make_pair(Target.getName(), &GOTEntry)).first;
    }

    assert(GOTEntryI != GOTEntries.end() && "Could not get GOT entry symbol");
    LLVM_DEBUG(
        { dbgs() << "    Using GOT entry " << *GOTEntryI->second << "\n"; });
    return *GOTEntryI->second;
  }

  Symbol &getStubSymbol(Symbol &Target) {
    assert(Target.hasName() &&
           "External branch edge can not point to an anonymous target");
    auto StubI = Stubs.find(Target.getName());

    if (StubI == Stubs.end()) {
      auto &StubSymbol = impl().createStub(Target);
      LLVM_DEBUG({
        dbgs() << "    Created stub for " << Target.getName() << ": "
               << StubSymbol << "\n";
      });
      StubI = Stubs.insert(std::make_pair(Target.getName(), &StubSymbol)).first;
    }

    assert(StubI != Stubs.end() && "Count not get stub symbol");
    LLVM_DEBUG({ dbgs() << "    Using stub " << *StubI->second << "\n"; });
    return *StubI->second;
  }

  LinkGraph &G;

private:
  BuilderImpl &impl() { return static_cast<BuilderImpl &>(*this); }

  DenseMap<StringRef, Symbol *> GOTEntries;
  DenseMap<StringRef, Symbol *> Stubs;
};

} // end namespace jitlink
} // end namespace llvm

#endif // LLVM_LIB_EXECUTIONENGINE_JITLINK_BASICGOTANDSTUBSBUILDER_H