LLVM  16.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 
14 #include "COFFLinkGraphBuilder.h"
15 #include "JITLinkGeneric.h"
16 #include "SEHFrameSupport.h"
17 #include "llvm/BinaryFormat/COFF.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 enum EdgeKind_coff_x86_64 : Edge::Kind {
31  Pointer32NB,
32  Pointer64,
33  SectionIdx16,
34  SecRel32,
35 };
36 
37 class COFFJITLinker_x86_64 : public JITLinker<COFFJITLinker_x86_64> {
38  friend class JITLinker<COFFJITLinker_x86_64>;
39 
40 public:
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 
46 private:
47  Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
48  return x86_64::applyFixup(G, B, E, nullptr);
49  }
50 };
51 
52 class COFFLinkGraphBuilder_x86_64 : public COFFLinkGraphBuilder {
53 private:
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) {
68  const object::coff_relocation *COFFRel = getObject().getCOFFRelocation(Rel);
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  }
105  Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
106  break;
107  }
110  Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
111  Addend -= 1;
112  break;
113  }
116  Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
117  Addend -= 2;
118  break;
119  }
122  Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
123  Addend -= 3;
124  break;
125  }
128  Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
129  Addend -= 4;
130  break;
131  }
134  Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
135  Addend -= 5;
136  break;
137  }
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  auto *AbsSym = &getGraph().addAbsoluteSymbol(
152  "secidx", orc::ExecutorAddr(SectionIdx), 2, Linkage::Strong,
153  Scope::Local, false);
154  GraphSymbol = AbsSym;
155  break;
156  }
158  // FIXME: SECREL to external symbol should be handled
159  if (!GraphSymbol->isDefined())
160  return Error::success();
161  Kind = EdgeKind_coff_x86_64::SecRel32;
162  Addend = *reinterpret_cast<const support::little32_t *>(FixupPtr);
163  break;
164  }
165  default: {
166  return make_error<JITLinkError>("Unsupported x86_64 relocation:" +
167  formatv("{0:d}", Rel.getType()));
168  }
169  };
170 
171  Edge GE(Kind, Offset, *GraphSymbol, Addend);
172  LLVM_DEBUG({
173  dbgs() << " ";
174  printEdge(dbgs(), BlockToFix, GE, getCOFFX86RelocationKindName(Kind));
175  dbgs() << "\n";
176  });
177 
178  BlockToFix.addEdge(std::move(GE));
179 
180  return Error::success();
181  }
182 
183 public:
184  COFFLinkGraphBuilder_x86_64(const object::COFFObjectFile &Obj, const Triple T)
186 };
187 
188 class COFFLinkGraphLowering_x86_64 {
189 public:
190  // Lowers COFF x86_64 specific edges to generic x86_64 edges.
191  Error lowerCOFFRelocationEdges(LinkGraph &G, JITLinkContext &Ctx) {
192  for (auto *B : G.blocks()) {
193  for (auto &E : B->edges()) {
194  switch (E.getKind()) {
195  case EdgeKind_coff_x86_64::Pointer32NB: {
196  auto ImageBase = getImageBaseAddress(G, Ctx);
197  if (!ImageBase)
198  return ImageBase.takeError();
199  E.setAddend(E.getAddend() - *ImageBase);
200  E.setKind(x86_64::Pointer32);
201  break;
202  }
204  E.setKind(x86_64::PCRel32);
205  break;
206  }
208  E.setKind(x86_64::Pointer64);
209  break;
210  }
211  case EdgeKind_coff_x86_64::SectionIdx16: {
212  E.setKind(x86_64::Pointer16);
213  break;
214  }
215  case EdgeKind_coff_x86_64::SecRel32: {
216  E.setAddend(E.getAddend() -
217  getSectionStart(E.getTarget().getBlock().getSection())
218  .getValue());
219  E.setKind(x86_64::Pointer32);
220  break;
221  }
222  default:
223  break;
224  }
225  }
226  }
227  return Error::success();
228  }
229 
230 private:
231  static StringRef getImageBaseSymbolName() { return "__ImageBase"; }
232 
233  orc::ExecutorAddr getSectionStart(Section &Sec) {
234  if (!SectionStartCache.count(&Sec)) {
235  SectionRange Range(Sec);
236  SectionStartCache[&Sec] = Range.getStart();
237  }
238  return SectionStartCache[&Sec];
239  }
240 
241  Expected<JITTargetAddress> getImageBaseAddress(LinkGraph &G,
242  JITLinkContext &Ctx) {
243  if (this->ImageBase)
244  return this->ImageBase;
245  for (auto *S : G.defined_symbols())
246  if (S->getName() == getImageBaseSymbolName()) {
247  this->ImageBase = S->getAddress().getValue();
248  return this->ImageBase;
249  }
250 
252  Symbols[getImageBaseSymbolName()] = SymbolLookupFlags::RequiredSymbol;
253  JITTargetAddress ImageBase;
254  Error Err = Error::success();
255  Ctx.lookup(Symbols,
257  ErrorAsOutParameter EAO(&Err);
258  if (!LR) {
259  Err = LR.takeError();
260  return;
261  }
262  auto &ImageBaseSymbol = LR->begin()->second;
263  ImageBase = ImageBaseSymbol.getAddress();
264  }));
265  if (Err)
266  return std::move(Err);
267  this->ImageBase = ImageBase;
268  return ImageBase;
269  }
270 
271  DenseMap<Section *, orc::ExecutorAddr> SectionStartCache;
272  JITTargetAddress ImageBase = 0;
273 };
274 
275 Error lowerEdges_COFF_x86_64(LinkGraph &G, JITLinkContext *Ctx) {
276  LLVM_DEBUG(dbgs() << "Lowering COFF x86_64 edges:\n");
277  COFFLinkGraphLowering_x86_64 GraphLowering;
278 
279  if (auto Err = GraphLowering.lowerCOFFRelocationEdges(G, *Ctx))
280  return Err;
281 
282  return Error::success();
283 }
284 } // namespace
285 
286 namespace llvm {
287 namespace jitlink {
288 
289 /// Return the string name of the given COFF x86_64 edge kind.
291  switch (R) {
292  case PCRel32:
293  return "PCRel32";
294  case Pointer32NB:
295  return "Pointer32NB";
296  case Pointer64:
297  return "Pointer64";
298  case SectionIdx16:
299  return "SectionIdx16";
300  case SecRel32:
301  return "SecRel32";
302  default:
303  return x86_64::getEdgeKindName(R);
304  }
305 }
306 
309  LLVM_DEBUG({
310  dbgs() << "Building jitlink graph for new input "
311  << ObjectBuffer.getBufferIdentifier() << "...\n";
312  });
313 
314  auto COFFObj = object::ObjectFile::createCOFFObjectFile(ObjectBuffer);
315  if (!COFFObj)
316  return COFFObj.takeError();
317 
318  return COFFLinkGraphBuilder_x86_64(**COFFObj, (*COFFObj)->makeTriple())
319  .buildGraph();
320 }
321 
322 void link_COFF_x86_64(std::unique_ptr<LinkGraph> G,
323  std::unique_ptr<JITLinkContext> Ctx) {
324  PassConfiguration Config;
325  const Triple &TT = G->getTargetTriple();
326  if (Ctx->shouldAddDefaultTargetPasses(TT)) {
327  // Add a mark-live pass.
328  if (auto MarkLive = Ctx->getMarkLivePass(TT)) {
329  Config.PrePrunePasses.push_back(std::move(MarkLive));
330  Config.PrePrunePasses.push_back(SEHFrameKeepAlivePass(".pdata"));
331  } else
332  Config.PrePrunePasses.push_back(markAllSymbolsLive);
333 
334  // Add COFF edge lowering passes.
335  JITLinkContext *CtxPtr = Ctx.get();
336  Config.PreFixupPasses.push_back(
337  [CtxPtr](LinkGraph &G) { return lowerEdges_COFF_x86_64(G, CtxPtr); });
338  }
339 
340  if (auto Err = Ctx->modifyPassConfig(*G, Config))
341  return Ctx->notifyFailed(std::move(Err));
342 
344 }
345 
346 } // namespace jitlink
347 } // namespace llvm
llvm::orc::ExecutorAddr
Represents an address in the executor process.
Definition: ExecutorAddress.h:31
llvm::object::RelocationRef::getSymbol
symbol_iterator getSymbol() const
Definition: ObjectFile.h:566
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::object::COFFSymbolRef::getSectionNumber
int32_t getSectionNumber() const
Definition: COFF.h:322
llvm::support::detail::packed_endian_specific_integral
Definition: Endian.h:206
T
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
COFF.h
llvm::COFF::IMAGE_REL_AMD64_REL32
@ IMAGE_REL_AMD64_REL32
Definition: COFF.h:350
llvm::object::ObjectFile::createCOFFObjectFile
static Expected< std::unique_ptr< COFFObjectFile > > createCOFFObjectFile(MemoryBufferRef Object)
Definition: COFFObjectFile.cpp:1718
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::ArrayRef::data
const T * data() const
Definition: ArrayRef.h:161
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::object::RelocationRef::getType
uint64_t getType() const
Definition: ObjectFile.h:570
llvm::formatv
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Definition: FormatVariadic.h:251
llvm::object::COFFSymbolRef
Definition: COFF.h:280
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
COFFLinkGraphBuilder.h
llvm::ErrorAsOutParameter
Helper for Errors used as out-parameters.
Definition: Error.h:1096
llvm::object::coff_relocation
Definition: COFF.h:474
llvm::object::SectionRef
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:80
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
uint64_t
llvm::COFF::IMAGE_REL_AMD64_REL32_4
@ IMAGE_REL_AMD64_REL32_4
Definition: COFF.h:354
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::COFF::IMAGE_REL_AMD64_REL32_5
@ IMAGE_REL_AMD64_REL32_5
Definition: COFF.h:355
llvm::DenseMap
Definition: DenseMap.h:714
llvm::COFF::IMAGE_REL_AMD64_ADDR64
@ IMAGE_REL_AMD64_ADDR64
Definition: COFF.h:347
llvm::object::RelocationRef::getOffset
uint64_t getOffset() const
Definition: ObjectFile.h:562
llvm::AArch64CC::GE
@ GE
Definition: AArch64BaseInfo.h:265
SEHFrameSupport.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::COFF::IMAGE_REL_AMD64_REL32_2
@ IMAGE_REL_AMD64_REL32_2
Definition: COFF.h:352
llvm::COFF::IMAGE_REL_AMD64_SECREL
@ IMAGE_REL_AMD64_SECREL
Definition: COFF.h:357
llvm::object::COFFSymbolRef::isAbsolute
bool isAbsolute() const
Definition: COFF.h:373
llvm::object::SectionRef::getAddress
uint64_t getAddress() const
Definition: ObjectFile.h:464
x86_64.h
llvm::object::coff_relocation::SymbolTableIndex
support::ulittle32_t SymbolTableIndex
Definition: COFF.h:476
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:79
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
getObject
static Error getObject(const T *&Obj, MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
Definition: COFFObjectFile.cpp:58
llvm::COFF::IMAGE_REL_AMD64_ADDR32NB
@ IMAGE_REL_AMD64_ADDR32NB
Definition: COFF.h:349
llvm::COFF::IMAGE_REL_AMD64_REL32_1
@ IMAGE_REL_AMD64_REL32_1
Definition: COFF.h:351
llvm::object::COFFObjectFile
Definition: COFF.h:788
COFF_x86_64.h
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:596
llvm::COFF::IMAGE_REL_AMD64_REL32_3
@ IMAGE_REL_AMD64_REL32_3
Definition: COFF.h:353
COFF.h
llvm::object::SectionRef::getIndex
uint64_t getIndex() const
Definition: ObjectFile.h:468
llvm::COFF::IMAGE_REL_AMD64_SECTION
@ IMAGE_REL_AMD64_SECTION
Definition: COFF.h:356
llvm::MemoryBufferRef::getBufferIdentifier
StringRef getBufferIdentifier() const
Definition: MemoryBufferRef.h:33
llvm::object::RelocationRef
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:51
Endian.h
JITLinkGeneric.h