1 //===----- COFF_x86_64.cpp - JIT linker implementation for COFF/x86_64 ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // COFF/x86_64 jit-link implementation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ExecutionEngine/JITLink/COFF_x86_64.h"
14 #include "COFFLinkGraphBuilder.h"
15 #include "EHFrameSupportImpl.h"
16 #include "JITLinkGeneric.h"
17 #include "llvm/BinaryFormat/COFF.h"
18 #include "llvm/ExecutionEngine/JITLink/x86_64.h"
19 #include "llvm/Object/COFF.h"
20 #include "llvm/Support/Endian.h"
21 
22 #define DEBUG_TYPE "jitlink"
23 
24 using namespace llvm;
25 using namespace llvm::jitlink;
26 
27 namespace {
28 
29 class COFFJITLinker_x86_64 : public JITLinker<COFFJITLinker_x86_64> {
30   friend class JITLinker<COFFJITLinker_x86_64>;
31 
32 public:
33   COFFJITLinker_x86_64(std::unique_ptr<JITLinkContext> Ctx,
34                        std::unique_ptr<LinkGraph> G,
35                        PassConfiguration PassConfig)
36       : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {}
37 
38 private:
39   Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
40     return x86_64::applyFixup(G, B, E, nullptr);
41   }
42 };
43 
44 class COFFLinkGraphBuilder_x86_64 : public COFFLinkGraphBuilder {
45 private:
46   uint64_t ImageBase = 0;
47   enum COFFX86RelocationKind {
48     COFFAddr32NB,
49     COFFRel32,
50   };
51 
52   static Expected<COFFX86RelocationKind>
53   getRelocationKind(const uint32_t Type) {
54     switch (Type) {
55     case COFF::RelocationTypeAMD64::IMAGE_REL_AMD64_ADDR32NB:
56       return COFFAddr32NB;
57     case COFF::RelocationTypeAMD64::IMAGE_REL_AMD64_REL32:
58       return COFFRel32;
59     }
60 
61     return make_error<JITLinkError>("Unsupported x86_64 relocation:" +
62                                     formatv("{0:d}", Type));
63   }
64 
65   Error addRelocations() override {
66 
67     LLVM_DEBUG(dbgs() << "Processing relocations:\n");
68 
69     for (const auto &RelSect : sections())
70       if (Error Err = COFFLinkGraphBuilder::forEachRelocation(
71               RelSect, this, &COFFLinkGraphBuilder_x86_64::addSingleRelocation))
72         return Err;
73 
74     return Error::success();
75   }
76 
77   uint64_t getImageBase() {
78     if (!ImageBase) {
79       ImageBase = std::numeric_limits<uint64_t>::max();
80       for (const auto &Block : getGraph().blocks()) {
81         if (Block->getAddress().getValue())
82           ImageBase = std::min(ImageBase, Block->getAddress().getValue());
83       }
84     }
85     return ImageBase;
86   }
87 
88   Error addSingleRelocation(const object::RelocationRef &Rel,
89                             const object::SectionRef &FixupSect,
90                             Block &BlockToFix) {
91 
92     const object::coff_relocation *COFFRel = getObject().getCOFFRelocation(Rel);
93     auto SymbolIt = Rel.getSymbol();
94     if (SymbolIt == getObject().symbol_end()) {
95       return make_error<StringError>(
96           formatv("Invalid symbol index in relocation entry. "
97                   "index: {0}, section: {1}",
98                   COFFRel->SymbolTableIndex, FixupSect.getIndex()),
99           inconvertibleErrorCode());
100     }
101 
102     object::COFFSymbolRef COFFSymbol = getObject().getCOFFSymbol(*SymbolIt);
103     COFFSymbolIndex SymIndex = getObject().getSymbolIndex(COFFSymbol);
104 
105     Symbol *GraphSymbol = getGraphSymbol(SymIndex);
106     if (!GraphSymbol)
107       return make_error<StringError>(
108           formatv("Could not find symbol at given index, did you add it to "
109                   "JITSymbolTable? index: {0}, section: {1}",
110                   SymIndex, FixupSect.getIndex()),
111           inconvertibleErrorCode());
112 
113     Expected<COFFX86RelocationKind> RelocKind =
114         getRelocationKind(Rel.getType());
115     if (!RelocKind)
116       return RelocKind.takeError();
117 
118     int64_t Addend = 0;
119     orc::ExecutorAddr FixupAddress =
120         orc::ExecutorAddr(FixupSect.getAddress()) + Rel.getOffset();
121     Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
122 
123     Edge::Kind Kind = Edge::Invalid;
124 
125     switch (*RelocKind) {
126     case COFFAddr32NB: {
127       Kind = x86_64::Pointer32;
128       Offset -= getImageBase();
129       break;
130     }
131     case COFFRel32: {
132       Kind = x86_64::BranchPCRel32;
133       break;
134     }
135     };
136 
137     Edge GE(Kind, Offset, *GraphSymbol, Addend);
138     LLVM_DEBUG({
139       dbgs() << "    ";
140       printEdge(dbgs(), BlockToFix, GE, x86_64::getEdgeKindName(Kind));
141       dbgs() << "\n";
142     });
143 
144     BlockToFix.addEdge(std::move(GE));
145     return Error::success();
146   }
147 
148   /// Return the string name of the given COFF x86_64 edge kind.
149   const char *getCOFFX86RelocationKindName(COFFX86RelocationKind R) {
150     switch (R) {
151     case COFFAddr32NB:
152       return "COFFAddr32NB";
153     case COFFRel32:
154       return "COFFRel32";
155     }
156   }
157 
158 public:
159   COFFLinkGraphBuilder_x86_64(const object::COFFObjectFile &Obj, const Triple T)
160       : COFFLinkGraphBuilder(Obj, std::move(T), x86_64::getEdgeKindName) {}
161 };
162 
163 Error buildTables_COFF_x86_64(LinkGraph &G) {
164   LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
165 
166   x86_64::GOTTableManager GOT;
167   x86_64::PLTTableManager PLT(GOT);
168   visitExistingEdges(G, GOT, PLT);
169   return Error::success();
170 }
171 } // namespace
172 
173 namespace llvm {
174 namespace jitlink {
175 
176 Expected<std::unique_ptr<LinkGraph>>
177 createLinkGraphFromCOFFObject_x86_64(MemoryBufferRef ObjectBuffer) {
178   LLVM_DEBUG({
179     dbgs() << "Building jitlink graph for new input "
180            << ObjectBuffer.getBufferIdentifier() << "...\n";
181   });
182 
183   auto COFFObj = object::ObjectFile::createCOFFObjectFile(ObjectBuffer);
184   if (!COFFObj)
185     return COFFObj.takeError();
186 
187   return COFFLinkGraphBuilder_x86_64(**COFFObj, (*COFFObj)->makeTriple())
188       .buildGraph();
189 }
190 
191 void link_COFF_x86_64(std::unique_ptr<LinkGraph> G,
192                       std::unique_ptr<JITLinkContext> Ctx) {
193   PassConfiguration Config;
194   const Triple &TT = G->getTargetTriple();
195   if (Ctx->shouldAddDefaultTargetPasses(TT)) {
196     // Add a mark-live pass.
197     if (auto MarkLive = Ctx->getMarkLivePass(TT))
198       Config.PrePrunePasses.push_back(std::move(MarkLive));
199     else
200       Config.PrePrunePasses.push_back(markAllSymbolsLive);
201 
202     // Add an in-place GOT/Stubs/TLSInfoEntry build pass.
203     Config.PostPrunePasses.push_back(buildTables_COFF_x86_64);
204 
205     // Add GOT/Stubs optimizer pass.
206     Config.PreFixupPasses.push_back(x86_64::optimizeGOTAndStubAccesses);
207   }
208 
209   if (auto Err = Ctx->modifyPassConfig(*G, Config))
210     return Ctx->notifyFailed(std::move(Err));
211 
212   COFFJITLinker_x86_64::link(std::move(Ctx), std::move(G), std::move(Config));
213 }
214 
215 } // namespace jitlink
216 } // namespace llvm
217