LLVM 20.0.0git
COFF_x86_64.cpp
Go to the documentation of this file.
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
15#include "JITLinkGeneric.h"
16#include "SEHFrameSupport.h"
19#include "llvm/Object/COFF.h"
20#include "llvm/Support/Endian.h"
21
22#define DEBUG_TYPE "jitlink"
23
24using namespace llvm;
25using namespace llvm::jitlink;
26
27namespace {
28
29enum EdgeKind_coff_x86_64 : Edge::Kind {
31 Pointer32NB,
32 Pointer64,
33 SectionIdx16,
34 SecRel32,
35};
36
37class COFFJITLinker_x86_64 : public JITLinker<COFFJITLinker_x86_64> {
38 friend class JITLinker<COFFJITLinker_x86_64>;
39
40public:
41 COFFJITLinker_x86_64(std::unique_ptr<JITLinkContext> Ctx,
42 std::unique_ptr<LinkGraph> G,
43 PassConfiguration PassConfig)
44 : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {}
45
46private:
47 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
48 return x86_64::applyFixup(G, B, E, nullptr);
49 }
50};
51
52class COFFLinkGraphBuilder_x86_64 : public COFFLinkGraphBuilder {
53private:
54 Error addRelocations() override {
55 LLVM_DEBUG(dbgs() << "Processing relocations:\n");
56
57 for (const auto &RelSect : sections())
59 RelSect, this, &COFFLinkGraphBuilder_x86_64::addSingleRelocation))
60 return Err;
61
62 return Error::success();
63 }
64
65 Error addSingleRelocation(const object::RelocationRef &Rel,
66 const object::SectionRef &FixupSect,
67 Block &BlockToFix) {
69 auto SymbolIt = Rel.getSymbol();
70 if (SymbolIt == getObject().symbol_end()) {
71 return make_error<StringError>(
72 formatv("Invalid symbol index in relocation entry. "
73 "index: {0}, section: {1}",
74 COFFRel->SymbolTableIndex, FixupSect.getIndex()),
76 }
77
78 object::COFFSymbolRef COFFSymbol = getObject().getCOFFSymbol(*SymbolIt);
79 COFFSymbolIndex SymIndex = getObject().getSymbolIndex(COFFSymbol);
80
81 Symbol *GraphSymbol = getGraphSymbol(SymIndex);
82 if (!GraphSymbol)
83 return make_error<StringError>(
84 formatv("Could not find symbol at given index, did you add it to "
85 "JITSymbolTable? index: {0}, section: {1}",
86 SymIndex, FixupSect.getIndex()),
88
89 int64_t Addend = 0;
90 orc::ExecutorAddr FixupAddress =
91 orc::ExecutorAddr(FixupSect.getAddress()) + Rel.getOffset();
92 Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
93
95 const char *FixupPtr = BlockToFix.getContent().data() + Offset;
96
97 switch (Rel.getType()) {
99 Kind = EdgeKind_coff_x86_64::Pointer32NB;
100 Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
101 break;
102 }
104 Kind = EdgeKind_coff_x86_64::PCRel32;
105 Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
106 break;
107 }
109 Kind = EdgeKind_coff_x86_64::PCRel32;
110 Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
111 Addend -= 1;
112 break;
113 }
115 Kind = EdgeKind_coff_x86_64::PCRel32;
116 Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
117 Addend -= 2;
118 break;
119 }
121 Kind = EdgeKind_coff_x86_64::PCRel32;
122 Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
123 Addend -= 3;
124 break;
125 }
127 Kind = EdgeKind_coff_x86_64::PCRel32;
128 Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
129 Addend -= 4;
130 break;
131 }
133 Kind = EdgeKind_coff_x86_64::PCRel32;
134 Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
135 Addend -= 5;
136 break;
137 }
139 Kind = EdgeKind_coff_x86_64::Pointer64;
140 Addend = *reinterpret_cast<const support::little64_t *>(FixupPtr);
141 break;
142 }
144 Kind = EdgeKind_coff_x86_64::SectionIdx16;
145 Addend = *reinterpret_cast<const support::little16_t *>(FixupPtr);
146 uint64_t SectionIdx = 0;
147 if (COFFSymbol.isAbsolute())
148 SectionIdx = getObject().getNumberOfSections() + 1;
149 else
150 SectionIdx = COFFSymbol.getSectionNumber();
151
152 auto *AbsSym = &getGraph().addAbsoluteSymbol(
153 "secidx", orc::ExecutorAddr(SectionIdx), 2, Linkage::Strong,
154 Scope::Local, false);
155 GraphSymbol = AbsSym;
156 break;
157 }
159 // FIXME: SECREL to external symbol should be handled
160 if (!GraphSymbol->isDefined())
161 return Error::success();
162 Kind = EdgeKind_coff_x86_64::SecRel32;
163 Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
164 break;
165 }
166 default: {
167 return make_error<JITLinkError>("Unsupported x86_64 relocation:" +
168 formatv("{0:d}", Rel.getType()));
169 }
170 };
171
172 Edge GE(Kind, Offset, *GraphSymbol, Addend);
173 LLVM_DEBUG({
174 dbgs() << " ";
175 printEdge(dbgs(), BlockToFix, GE, getCOFFX86RelocationKindName(Kind));
176 dbgs() << "\n";
177 });
178
179 BlockToFix.addEdge(std::move(GE));
180
181 return Error::success();
182 }
183
184public:
185 COFFLinkGraphBuilder_x86_64(const object::COFFObjectFile &Obj,
186 std::shared_ptr<orc::SymbolStringPool> SSP,
187 const Triple T, const SubtargetFeatures Features)
188 : COFFLinkGraphBuilder(Obj, std::move(SSP), std::move(T),
189 std::move(Features),
191};
192
193class COFFLinkGraphLowering_x86_64 {
194public:
195 COFFLinkGraphLowering_x86_64(std::shared_ptr<orc::SymbolStringPool> SSP)
196 : SSP(std::move(SSP)) {
197 ImageBaseName = this->SSP->intern("__ImageBase");
198 }
199 // Lowers COFF x86_64 specific edges to generic x86_64 edges.
200 Error lowerCOFFRelocationEdges(LinkGraph &G, JITLinkContext &Ctx) {
201 for (auto *B : G.blocks()) {
202 for (auto &E : B->edges()) {
203 switch (E.getKind()) {
204 case EdgeKind_coff_x86_64::Pointer32NB: {
205 auto ImageBase = getImageBaseAddress(G, Ctx);
206 if (!ImageBase)
207 return ImageBase.takeError();
208 E.setAddend(E.getAddend() - ImageBase->getValue());
209 E.setKind(x86_64::Pointer32);
210 break;
211 }
212 case EdgeKind_coff_x86_64::PCRel32: {
213 E.setKind(x86_64::PCRel32);
214 break;
215 }
216 case EdgeKind_coff_x86_64::Pointer64: {
217 E.setKind(x86_64::Pointer64);
218 break;
219 }
220 case EdgeKind_coff_x86_64::SectionIdx16: {
221 E.setKind(x86_64::Pointer16);
222 break;
223 }
224 case EdgeKind_coff_x86_64::SecRel32: {
225 E.setAddend(E.getAddend() -
226 getSectionStart(E.getTarget().getBlock().getSection())
227 .getValue());
228 E.setKind(x86_64::Pointer32);
229 break;
230 }
231 default:
232 break;
233 }
234 }
235 }
236 return Error::success();
237 }
238
239private:
240 const orc::SymbolStringPtr &getImageBaseSymbolName() const {
241 return this->ImageBaseName;
242 }
243
244 orc::ExecutorAddr getSectionStart(Section &Sec) {
245 if (!SectionStartCache.count(&Sec)) {
246 SectionRange Range(Sec);
247 SectionStartCache[&Sec] = Range.getStart();
248 }
249 return SectionStartCache[&Sec];
250 }
251
252 Expected<orc::ExecutorAddr> getImageBaseAddress(LinkGraph &G,
253 JITLinkContext &Ctx) {
254 if (this->ImageBase)
255 return this->ImageBase;
256 for (auto *S : G.defined_symbols())
257 if (S->getName() == getImageBaseSymbolName()) {
258 this->ImageBase = S->getAddress();
259 return this->ImageBase;
260 }
261
263 Symbols[getImageBaseSymbolName()] = SymbolLookupFlags::RequiredSymbol;
264 orc::ExecutorAddr ImageBase;
265 Error Err = Error::success();
266 Ctx.lookup(Symbols,
269 if (!LR) {
270 Err = LR.takeError();
271 return;
272 }
273 ImageBase = LR->begin()->second.getAddress();
274 }));
275 if (Err)
276 return std::move(Err);
277 this->ImageBase = ImageBase;
278 return ImageBase;
279 }
280
282 orc::ExecutorAddr ImageBase;
283 std::shared_ptr<orc::SymbolStringPool> SSP;
284 orc::SymbolStringPtr ImageBaseName = nullptr;
285};
286
287Error lowerEdges_COFF_x86_64(LinkGraph &G, JITLinkContext *Ctx) {
288 LLVM_DEBUG(dbgs() << "Lowering COFF x86_64 edges:\n");
289 COFFLinkGraphLowering_x86_64 GraphLowering(G.getSymbolStringPool());
290 if (auto Err = GraphLowering.lowerCOFFRelocationEdges(G, *Ctx))
291 return Err;
292
293 return Error::success();
294}
295} // namespace
296
297namespace llvm {
298namespace jitlink {
299
300/// Return the string name of the given COFF x86_64 edge kind.
302 switch (R) {
303 case PCRel32:
304 return "PCRel32";
305 case Pointer32NB:
306 return "Pointer32NB";
307 case Pointer64:
308 return "Pointer64";
309 case SectionIdx16:
310 return "SectionIdx16";
311 case SecRel32:
312 return "SecRel32";
313 default:
314 return x86_64::getEdgeKindName(R);
315 }
316}
317
319 MemoryBufferRef ObjectBuffer, std::shared_ptr<orc::SymbolStringPool> SSP) {
320 LLVM_DEBUG({
321 dbgs() << "Building jitlink graph for new input "
322 << ObjectBuffer.getBufferIdentifier() << "...\n";
323 });
324
325 auto COFFObj = object::ObjectFile::createCOFFObjectFile(ObjectBuffer);
326 if (!COFFObj)
327 return COFFObj.takeError();
328
329 auto Features = (*COFFObj)->getFeatures();
330 if (!Features)
331 return Features.takeError();
332
333 return COFFLinkGraphBuilder_x86_64(**COFFObj, std::move(SSP),
334 (*COFFObj)->makeTriple(),
335 std::move(*Features))
336 .buildGraph();
337}
338
339void link_COFF_x86_64(std::unique_ptr<LinkGraph> G,
340 std::unique_ptr<JITLinkContext> Ctx) {
342 const Triple &TT = G->getTargetTriple();
343 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
344 // Add a mark-live pass.
345 if (auto MarkLive = Ctx->getMarkLivePass(TT)) {
346 Config.PrePrunePasses.push_back(std::move(MarkLive));
347 Config.PrePrunePasses.push_back(SEHFrameKeepAlivePass(".pdata"));
348 } else
349 Config.PrePrunePasses.push_back(markAllSymbolsLive);
350
351 // Add COFF edge lowering passes.
352 JITLinkContext *CtxPtr = Ctx.get();
353 Config.PreFixupPasses.push_back(
354 [CtxPtr](LinkGraph &G) { return lowerEdges_COFF_x86_64(G, CtxPtr); });
355 }
356
357 if (auto Err = Ctx->modifyPassConfig(*G, Config))
358 return Ctx->notifyFailed(std::move(Err));
359
360 COFFJITLinker_x86_64::link(std::move(Ctx), std::move(G), std::move(Config));
361}
362
363} // namespace jitlink
364} // namespace llvm
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DEBUG(...)
Definition: Debug.h:106
RelaxConfig Config
Definition: ELF_riscv.cpp:506
#define _
#define G(x, y, z)
Definition: MD5.cpp:56
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
const T * data() const
Definition: ArrayRef.h:165
Helper for Errors used as out-parameters.
Definition: Error.h:1130
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
StringRef getBufferIdentifier() const
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
const coff_relocation * getCOFFRelocation(const RelocationRef &Reloc) const
uint32_t getNumberOfSections() const
Definition: COFF.h:983
uint32_t getSymbolIndex(COFFSymbolRef Symbol) const
COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const
bool isAbsolute() const
Definition: COFF.h:377
int32_t getSectionNumber() const
Definition: COFF.h:326
static Expected< std::unique_ptr< COFFObjectFile > > createCOFFObjectFile(MemoryBufferRef Object)
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
uint64_t getType() const
Definition: ObjectFile.h:628
uint64_t getOffset() const
Definition: ObjectFile.h:620
symbol_iterator getSymbol() const
Definition: ObjectFile.h:624
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
uint64_t getIndex() const
Definition: ObjectFile.h:525
uint64_t getAddress() const
Definition: ObjectFile.h:521
Represents an address in the executor process.
Pointer to a pooled string representing a symbol name.
@ IMAGE_REL_AMD64_REL32
Definition: COFF.h:364
@ IMAGE_REL_AMD64_REL32_5
Definition: COFF.h:369
@ IMAGE_REL_AMD64_ADDR64
Definition: COFF.h:361
@ IMAGE_REL_AMD64_REL32_3
Definition: COFF.h:367
@ IMAGE_REL_AMD64_ADDR32NB
Definition: COFF.h:363
@ IMAGE_REL_AMD64_SECTION
Definition: COFF.h:370
@ IMAGE_REL_AMD64_REL32_2
Definition: COFF.h:366
@ IMAGE_REL_AMD64_REL32_1
Definition: COFF.h:365
@ IMAGE_REL_AMD64_SECREL
Definition: COFF.h:371
@ IMAGE_REL_AMD64_REL32_4
Definition: COFF.h:368
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
support::ulittle32_t SymbolTableIndex
Definition: COFF.h:480