LLVM  14.0.0git
RuntimeDyldCOFFX86_64.h
Go to the documentation of this file.
1 //===-- RuntimeDyldCOFFX86_64.h --- COFF/X86_64 specific code ---*- C++ -*-===//
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_x64 support for MC-JIT runtime dynamic linker.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFF86_64_H
14 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFF86_64_H
15 
16 #include "../RuntimeDyldCOFF.h"
17 #include "llvm/BinaryFormat/COFF.h"
18 #include "llvm/Object/COFF.h"
19 
20 #define DEBUG_TYPE "dyld"
21 
22 namespace llvm {
23 
25 
26 private:
27  // When a module is loaded we save the SectionID of the unwind
28  // sections in a table until we receive a request to register all
29  // unregisteredEH frame sections with the memory manager.
30  SmallVector<SID, 2> UnregisteredEHFrameSections;
31  SmallVector<SID, 2> RegisteredEHFrameSections;
32  uint64_t ImageBase;
33 
34  // Fake an __ImageBase pointer by returning the section with the lowest adress
35  uint64_t getImageBase() {
36  if (!ImageBase) {
38  for (const SectionEntry &Section : Sections)
39  // The Sections list may contain sections that weren't loaded for
40  // whatever reason: they may be debug sections, and ProcessAllSections
41  // is false, or they may be sections that contain 0 bytes. If the
42  // section isn't loaded, the load address will be 0, and it should not
43  // be included in the ImageBase calculation.
44  if (Section.getLoadAddress() != 0)
45  ImageBase = std::min(ImageBase, Section.getLoadAddress());
46  }
47  return ImageBase;
48  }
49 
50  void write32BitOffset(uint8_t *Target, int64_t Addend, uint64_t Delta) {
51  uint64_t Result = Addend + Delta;
52  assert(Result <= UINT32_MAX && "Relocation overflow");
53  writeBytesUnaligned(Result, Target, 4);
54  }
55 
56 public:
60  ImageBase(0) {}
61 
62  unsigned getStubAlignment() override { return 1; }
63 
64  // 2-byte jmp instruction + 32-bit relative address + 64-bit absolute jump
65  unsigned getMaxStubSize() const override { return 14; }
66 
67  // The target location for the relocation is described by RE.SectionID and
68  // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
69  // SectionEntry has three members describing its location.
70  // SectionEntry::Address is the address at which the section has been loaded
71  // into memory in the current (host) process. SectionEntry::LoadAddress is
72  // the address that the section will have in the target process.
73  // SectionEntry::ObjAddress is the address of the bits for this section in the
74  // original emitted object image (also in the current address space).
75  //
76  // Relocations will be applied as if the section were loaded at
77  // SectionEntry::LoadAddress, but they will be applied at an address based
78  // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer
79  // to Target memory contents if they are required for value calculations.
80  //
81  // The Value parameter here is the load address of the symbol for the
82  // relocation to be applied. For relocations which refer to symbols in the
83  // current object Value will be the LoadAddress of the section in which
84  // the symbol resides (RE.Addend provides additional information about the
85  // symbol location). For external symbols, Value will be the address of the
86  // symbol in the target address space.
87  void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
89  uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
90 
91  switch (RE.RelType) {
92 
99  uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
100  // Delta is the distance from the start of the reloc to the end of the
101  // instruction with the reloc.
102  uint64_t Delta = 4 + (RE.RelType - COFF::IMAGE_REL_AMD64_REL32);
103  Value -= FinalAddress + Delta;
104  uint64_t Result = Value + RE.Addend;
105  assert(((int64_t)Result <= INT32_MAX) && "Relocation overflow");
106  assert(((int64_t)Result >= INT32_MIN) && "Relocation underflow");
107  writeBytesUnaligned(Result, Target, 4);
108  break;
109  }
110 
112  // ADDR32NB requires an offset less than 2GB from 'ImageBase'.
113  // The MemoryManager can make sure this is always true by forcing the
114  // memory layout to be: CodeSection < ReadOnlySection < ReadWriteSection.
115  const uint64_t ImageBase = getImageBase();
116  if (Value < ImageBase || ((Value - ImageBase) > UINT32_MAX))
117  report_fatal_error("IMAGE_REL_AMD64_ADDR32NB relocation requires an "
118  "ordered section layout");
119  else {
120  write32BitOffset(Target, RE.Addend, Value - ImageBase);
121  }
122  break;
123  }
124 
127  break;
128  }
129 
131  assert(static_cast<int64_t>(RE.Addend) <= INT32_MAX && "Relocation overflow");
132  assert(static_cast<int64_t>(RE.Addend) >= INT32_MIN && "Relocation underflow");
134  break;
135  }
136 
137  default:
138  llvm_unreachable("Relocation type not implemented yet!");
139  break;
140  }
141  }
142 
143  std::tuple<uint64_t, uint64_t, uint64_t>
144  generateRelocationStub(unsigned SectionID, StringRef TargetName,
145  uint64_t Offset, uint64_t RelType, uint64_t Addend,
146  StubMap &Stubs) {
147  uintptr_t StubOffset;
148  SectionEntry &Section = Sections[SectionID];
149 
150  RelocationValueRef OriginalRelValueRef;
151  OriginalRelValueRef.SectionID = SectionID;
152  OriginalRelValueRef.Offset = Offset;
153  OriginalRelValueRef.Addend = Addend;
154  OriginalRelValueRef.SymbolName = TargetName.data();
155 
156  auto Stub = Stubs.find(OriginalRelValueRef);
157  if (Stub == Stubs.end()) {
158  LLVM_DEBUG(dbgs() << " Create a new stub function for "
159  << TargetName.data() << "\n");
160 
161  StubOffset = Section.getStubOffset();
162  Stubs[OriginalRelValueRef] = StubOffset;
163  createStubFunction(Section.getAddressWithOffset(StubOffset));
164  Section.advanceStubOffset(getMaxStubSize());
165  } else {
166  LLVM_DEBUG(dbgs() << " Stub function found for " << TargetName.data()
167  << "\n");
168  StubOffset = Stub->second;
169  }
170 
171  // FIXME: If RelType == COFF::IMAGE_REL_AMD64_ADDR32NB we should be able
172  // to ignore the __ImageBase requirement and just forward to the stub
173  // directly as an offset of this section:
174  // write32BitOffset(Section.getAddressWithOffset(Offset), 0, StubOffset);
175  // .xdata exception handler's aren't having this though.
176 
177  // Resolve original relocation to stub function.
178  const RelocationEntry RE(SectionID, Offset, RelType, Addend);
179  resolveRelocation(RE, Section.getLoadAddressWithOffset(StubOffset));
180 
181  // adjust relocation info so resolution writes to the stub function
182  Addend = 0;
183  Offset = StubOffset + 6;
185 
186  return std::make_tuple(Offset, RelType, Addend);
187  }
188 
190  processRelocationRef(unsigned SectionID,
192  const object::ObjectFile &Obj,
193  ObjSectionToIDMap &ObjSectionToID,
194  StubMap &Stubs) override {
195  // If possible, find the symbol referred to in the relocation,
196  // and the section that contains it.
197  object::symbol_iterator Symbol = RelI->getSymbol();
198  if (Symbol == Obj.symbol_end())
199  report_fatal_error("Unknown symbol in relocation");
200  auto SectionOrError = Symbol->getSection();
201  if (!SectionOrError)
202  return SectionOrError.takeError();
203  object::section_iterator SecI = *SectionOrError;
204  // If there is no section, this must be an external reference.
205  bool IsExtern = SecI == Obj.section_end();
206 
207  // Determine the Addend used to adjust the relocation value.
208  uint64_t RelType = RelI->getType();
209  uint64_t Offset = RelI->getOffset();
210  uint64_t Addend = 0;
211  SectionEntry &Section = Sections[SectionID];
212  uintptr_t ObjTarget = Section.getObjAddress() + Offset;
213 
214  Expected<StringRef> TargetNameOrErr = Symbol->getName();
215  if (!TargetNameOrErr)
216  return TargetNameOrErr.takeError();
217 
218  StringRef TargetName = *TargetNameOrErr;
219  unsigned TargetSectionID = 0;
220  uint64_t TargetOffset = 0;
221 
222  if (TargetName.startswith(getImportSymbolPrefix())) {
223  assert(IsExtern && "DLLImport not marked extern?");
224  TargetSectionID = SectionID;
225  TargetOffset = getDLLImportOffset(SectionID, Stubs, TargetName);
226  TargetName = StringRef();
227  IsExtern = false;
228  } else if (!IsExtern) {
229  if (auto TargetSectionIDOrErr =
230  findOrEmitSection(Obj, *SecI, SecI->isText(), ObjSectionToID))
231  TargetSectionID = *TargetSectionIDOrErr;
232  else
233  return TargetSectionIDOrErr.takeError();
234  TargetOffset = getSymbolOffset(*Symbol);
235  }
236 
237  switch (RelType) {
238 
246  uint8_t *Displacement = (uint8_t *)ObjTarget;
247  Addend = readBytesUnaligned(Displacement, 4);
248 
249  if (IsExtern)
250  std::tie(Offset, RelType, Addend) = generateRelocationStub(
251  SectionID, TargetName, Offset, RelType, Addend, Stubs);
252 
253  break;
254  }
255 
257  uint8_t *Displacement = (uint8_t *)ObjTarget;
258  Addend = readBytesUnaligned(Displacement, 8);
259  break;
260  }
261 
262  default:
263  break;
264  }
265 
266  LLVM_DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
267  << " RelType: " << RelType << " TargetName: "
268  << TargetName << " Addend " << Addend << "\n");
269 
270  if (IsExtern) {
271  RelocationEntry RE(SectionID, Offset, RelType, Addend);
272  addRelocationForSymbol(RE, TargetName);
273  } else {
274  RelocationEntry RE(SectionID, Offset, RelType, TargetOffset + Addend);
275  addRelocationForSection(RE, TargetSectionID);
276  }
277 
278  return ++RelI;
279  }
280 
281  void registerEHFrames() override {
282  for (auto const &EHFrameSID : UnregisteredEHFrameSections) {
283  uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
284  uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
285  size_t EHFrameSize = Sections[EHFrameSID].getSize();
286  MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
287  RegisteredEHFrameSections.push_back(EHFrameSID);
288  }
289  UnregisteredEHFrameSections.clear();
290  }
291 
293  ObjSectionToIDMap &SectionMap) override {
294  // Look for and record the EH frame section IDs.
295  for (const auto &SectionPair : SectionMap) {
296  const object::SectionRef &Section = SectionPair.first;
297  Expected<StringRef> NameOrErr = Section.getName();
298  if (!NameOrErr)
299  return NameOrErr.takeError();
300 
301  // Note unwind info is stored in .pdata but often points to .xdata
302  // with an IMAGE_REL_AMD64_ADDR32NB relocation. Using a memory manager
303  // that keeps sections ordered in relation to __ImageBase is necessary.
304  if ((*NameOrErr) == ".pdata")
305  UnregisteredEHFrameSections.push_back(SectionPair.second);
306  }
307  return Error::success();
308  }
309 };
310 
311 } // end namespace llvm
312 
313 #undef DEBUG_TYPE
314 
315 #endif
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:286
llvm::RuntimeDyldCOFFX86_64::RuntimeDyldCOFFX86_64
RuntimeDyldCOFFX86_64(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)
Definition: RuntimeDyldCOFFX86_64.h:57
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::RuntimeDyldImpl::MemMgr
RuntimeDyld::MemoryManager & MemMgr
Definition: RuntimeDyldImpl.h:244
llvm::RuntimeDyldCOFFX86_64::registerEHFrames
void registerEHFrames() override
Definition: RuntimeDyldCOFFX86_64.h:281
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
llvm::RuntimeDyldImpl::Sections
SectionList Sections
Definition: RuntimeDyldImpl.h:254
llvm::SmallVector< SID, 2 >
llvm::RuntimeDyldCOFFX86_64::generateRelocationStub
std::tuple< uint64_t, uint64_t, uint64_t > generateRelocationStub(unsigned SectionID, StringRef TargetName, uint64_t Offset, uint64_t RelType, uint64_t Addend, StubMap &Stubs)
Definition: RuntimeDyldCOFFX86_64.h:144
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
COFF.h
llvm::COFF::IMAGE_REL_AMD64_REL32
@ IMAGE_REL_AMD64_REL32
Definition: COFF.h:350
llvm::RuntimeDyldImpl::addRelocationForSection
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Definition: RuntimeDyld.cpp:948
llvm::RuntimeDyldCOFFX86_64::getMaxStubSize
unsigned getMaxStubSize() const override
Definition: RuntimeDyldCOFFX86_64.h:65
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::RuntimeDyldImpl::addRelocationForSymbol
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
Definition: RuntimeDyld.cpp:953
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::RelocationEntry::SectionID
unsigned SectionID
SectionID - the section this relocation points to.
Definition: RuntimeDyldImpl.h:120
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:78
llvm::Resolver
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2000
llvm::object::SymbolicFile::symbol_end
virtual basic_symbol_iterator symbol_end() const =0
llvm::RuntimeDyldCOFFX86_64::getStubAlignment
unsigned getStubAlignment() override
Definition: RuntimeDyldCOFFX86_64.h:62
llvm::RelocationEntry::Offset
uint64_t Offset
Offset - offset into the section.
Definition: RuntimeDyldImpl.h:123
llvm::RelocationValueRef::Offset
uint64_t Offset
Definition: RuntimeDyldImpl.h:193
llvm::object::ObjectFile::section_end
virtual section_iterator section_end() const =0
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::RuntimeDyldImpl::writeBytesUnaligned
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
Definition: RuntimeDyld.cpp:721
llvm::RuntimeDyld::MemoryManager::registerEHFrames
virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size)=0
Register the EH frames with the runtime so that c++ exceptions work.
llvm::RelocationEntry::Addend
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
Definition: RuntimeDyldImpl.h:130
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
llvm::RelocationEntry
RelocationEntry - used to represent relocations internally in the dynamic linker.
Definition: RuntimeDyldImpl.h:117
uint64_t
llvm::object::symbol_iterator
Definition: ObjectFile.h:207
llvm::COFF::IMAGE_REL_AMD64_REL32_4
@ IMAGE_REL_AMD64_REL32_4
Definition: COFF.h:354
llvm::COFF::IMAGE_REL_AMD64_REL32_5
@ IMAGE_REL_AMD64_REL32_5
Definition: COFF.h:355
llvm::COFF::IMAGE_REL_AMD64_ADDR64
@ IMAGE_REL_AMD64_ADDR64
Definition: COFF.h:347
llvm::SectionEntry
SectionEntry - represents a section emitted into memory by the dynamic linker.
Definition: RuntimeDyldImpl.h:45
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::object::content_iterator
Definition: SymbolicFile.h:67
llvm::RuntimeDyldCOFF::getDLLImportOffset
uint64_t getDLLImportOffset(unsigned SectionID, StubMap &Stubs, StringRef Name, bool SetSectionIDMinus1=false)
Definition: RuntimeDyldCOFF.cpp:82
llvm::RuntimeDyldCOFF::getImportSymbolPrefix
static constexpr StringRef getImportSymbolPrefix()
Definition: RuntimeDyldCOFF.h:50
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
llvm::RuntimeDyldCOFF::getSymbolOffset
uint64_t getSymbolOffset(const SymbolRef &Sym)
Definition: RuntimeDyldCOFF.cpp:77
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
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::RelocationValueRef::SectionID
unsigned SectionID
Definition: RuntimeDyldImpl.h:192
llvm::RuntimeDyldImpl::createStubFunction
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
Definition: RuntimeDyld.cpp:972
llvm::RuntimeDyldCOFFX86_64
Definition: RuntimeDyldCOFFX86_64.h:24
llvm::JITSymbolResolver
Symbol resolution interface.
Definition: JITSymbol.h:371
llvm::RuntimeDyldCOFF
Definition: RuntimeDyldCOFF.h:26
llvm::RuntimeDyldImpl::findOrEmitSection
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
Definition: RuntimeDyld.cpp:929
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::COFF::IMAGE_REL_AMD64_ADDR32NB
@ IMAGE_REL_AMD64_ADDR32NB
Definition: COFF.h:349
llvm::RuntimeDyldCOFFX86_64::processRelocationRef
Expected< object::relocation_iterator > processRelocationRef(unsigned SectionID, object::relocation_iterator RelI, const object::ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) override
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
Definition: RuntimeDyldCOFFX86_64.h:190
llvm::RuntimeDyldCOFFX86_64::finalizeLoad
Error finalizeLoad(const object::ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
Definition: RuntimeDyldCOFFX86_64.h:292
llvm::COFF::IMAGE_REL_AMD64_REL32_1
@ IMAGE_REL_AMD64_REL32_1
Definition: COFF.h:351
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:585
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
llvm::RuntimeDyldCOFFX86_64::resolveRelocation
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override
A object file specific relocation resolver.
Definition: RuntimeDyldCOFFX86_64.h:87
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:589
llvm::COFF::IMAGE_REL_AMD64_REL32_3
@ IMAGE_REL_AMD64_REL32_3
Definition: COFF.h:353
COFF.h
llvm::RelocationValueRef::SymbolName
const char * SymbolName
Definition: RuntimeDyldImpl.h:195
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::RuntimeDyldImpl::readBytesUnaligned
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
Definition: RuntimeDyld.cpp:707
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:149
llvm::RuntimeDyldImpl::StubMap
std::map< RelocationValueRef, uintptr_t > StubMap
Definition: RuntimeDyldImpl.h:286
llvm::RelocationValueRef
Definition: RuntimeDyldImpl.h:190
llvm::RelocationValueRef::Addend
int64_t Addend
Definition: RuntimeDyldImpl.h:194
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::RelocationEntry::RelType
uint32_t RelType
RelType - relocation type.
Definition: RuntimeDyldImpl.h:126
llvm::RuntimeDyldImpl::ObjSectionToIDMap
std::map< SectionRef, unsigned > ObjSectionToIDMap
Definition: RuntimeDyldImpl.h:261
llvm::RuntimeDyld::MemoryManager
Memory Management.
Definition: RuntimeDyld.h:92