13 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H 14 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H 30 #include <system_error> 31 #include <unordered_map> 40 #define UNIMPLEMENTED_RELOC(RelType) \ 42 return make_error<RuntimeDyldError>("Unimplemented relocation: " #RelType) 68 size_t AllocationSize;
76 size_t allocationSize, uintptr_t objAddress)
77 : Name(name), Address(address), Size(size),
78 LoadAddress(reinterpret_cast<uintptr_t>(address)), StubOffset(size),
79 AllocationSize(allocationSize), ObjAddress(objAddress) {
91 assert(OffsetBytes <= AllocationSize &&
"Offset out of bounds!");
92 return Address + OffsetBytes;
102 assert(OffsetBytes <= AllocationSize &&
"Offset out of bounds!");
103 return LoadAddress + OffsetBytes;
109 StubOffset += StubSize;
110 assert(StubOffset <= AllocationSize &&
"Not enough space allocated!");
152 bool IsTargetThumbFunc =
false;
155 : SectionID(id), Offset(offset), RelType(type), Addend(addend),
156 SymOffset(0), IsPCRel(
false), Size(0), IsTargetThumbFunc(
false) {}
160 : SectionID(id), Offset(offset), RelType(type), Addend(addend),
161 SymOffset(symoffset), IsPCRel(
false), Size(0),
162 IsTargetThumbFunc(
false) {}
165 bool IsPCRel,
unsigned Size)
166 : SectionID(id), Offset(offset), RelType(type), Addend(addend),
167 SymOffset(0), IsPCRel(IsPCRel), Size(Size), IsTargetThumbFunc(
false) {}
170 unsigned SectionA, uint64_t SectionAOffset,
unsigned SectionB,
171 uint64_t SectionBOffset,
bool IsPCRel,
unsigned Size)
172 : SectionID(id), Offset(offset), RelType(type),
173 Addend(SectionAOffset - SectionBOffset + addend), IsPCRel(IsPCRel),
174 Size(Size), IsTargetThumbFunc(
false) {
175 Sections.SectionA = SectionA;
176 Sections.SectionB = SectionB;
180 unsigned SectionA, uint64_t SectionAOffset,
unsigned SectionB,
181 uint64_t SectionBOffset,
bool IsPCRel,
unsigned Size,
182 bool IsTargetThumbFunc)
183 : SectionID(id), Offset(offset), RelType(type),
184 Addend(SectionAOffset - SectionBOffset + addend), IsPCRel(IsPCRel),
185 Size(Size), IsTargetThumbFunc(IsTargetThumbFunc) {
186 Sections.SectionA = SectionA;
187 Sections.SectionB = SectionB;
197 bool IsStubThumb =
false;
199 SymbolName(nullptr) {}
209 if (Offset != Other.
Offset)
210 return Offset < Other.
Offset;
211 if (Addend != Other.
Addend)
212 return Addend < Other.
Addend;
225 : Offset(Offset), SectionID(SectionID), Flags(Flags) {}
235 unsigned SectionID = 0;
244 static const unsigned AbsoluteSymbolSection = ~0U;
258 #define RTDYLD_INVALID_SECTION_ID ((RuntimeDyldImpl::SID)(-1)) 287 typedef std::map<RelocationValueRef, uintptr_t>
StubMap;
315 virtual unsigned getMaxStubSize()
const = 0;
316 virtual unsigned getStubAlignment() = 0;
322 if (IsTargetLittleEndian)
324 *Addr = (Value >> 8) & 0xFF;
325 *(Addr + 1) = Value & 0xFF;
329 if (IsTargetLittleEndian)
331 *Addr = (Value >> 24) & 0xFF;
332 *(Addr + 1) = (Value >> 16) & 0xFF;
333 *(Addr + 2) = (Value >> 8) & 0xFF;
334 *(Addr + 3) = Value & 0xFF;
338 if (IsTargetLittleEndian)
340 *Addr = (Value >> 56) & 0xFF;
341 *(Addr + 1) = (Value >> 48) & 0xFF;
342 *(Addr + 2) = (Value >> 40) & 0xFF;
343 *(Addr + 3) = (Value >> 32) & 0xFF;
344 *(Addr + 4) = (Value >> 24) & 0xFF;
345 *(Addr + 5) = (Value >> 16) & 0xFF;
346 *(Addr + 6) = (Value >> 8) & 0xFF;
347 *(Addr + 7) = Value & 0xFF;
351 IsMipsO32ABI =
false;
352 IsMipsN32ABI =
false;
353 IsMipsN64ABI =
false;
357 uint64_t readBytesUnaligned(uint8_t *Src,
unsigned Size)
const;
361 void writeBytesUnaligned(uint64_t
Value, uint8_t *Dst,
unsigned Size)
const;
379 CommonSymbolList &CommonSymbols, uint64_t CommonSize,
397 ObjSectionToIDMap &LocalSections);
400 void addRelocationForSection(
const RelocationEntry &RE,
unsigned SectionID);
408 uint8_t *createStubFunction(uint8_t *Addr,
unsigned AbiVariant = 0);
411 void resolveRelocationList(
const RelocationList &Relocs, uint64_t Value);
416 virtual void resolveRelocation(
const RelocationEntry &RE, uint64_t Value) = 0;
424 const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID,
427 void applyExternalSymbolRelocations(
431 Error resolveExternalSymbols();
436 uint64_t &CodeSize,
uint32_t &CodeAlign,
437 uint64_t &RODataSize,
uint32_t &RODataAlign,
438 uint64_t &RWDataSize,
uint32_t &RWDataAlign);
441 unsigned computeGOTSize(
const ObjectFile &Obj);
444 unsigned computeSectionStubBufSize(
const ObjectFile &Obj,
466 : MemMgr(MemMgr), Resolver(Resolver),
473 this->ProcessAllSections = ProcessAllSections;
476 virtual std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
480 return Sections[SectionID].getLoadAddress();
484 return Sections[SectionID].getAddress();
488 return StringRef(reinterpret_cast<char *>(Sections[SectionID].getAddress()),
489 Sections[SectionID].getStubOffset() + getMaxStubSize());
496 if (pos == GlobalSymbolTable.
end())
498 const auto &
SymInfo = pos->second;
500 if (
SymInfo.getSectionID() == AbsoluteSymbolSection)
502 return getSectionAddress(
SymInfo.getSectionID()) +
SymInfo.getOffset();
506 auto GSTItr = GlobalSymbolTable.
find(Name);
507 if (GSTItr == GlobalSymbolTable.
end())
509 return GSTItr->second.getSectionID();
516 if (pos == GlobalSymbolTable.
end())
519 uint64_t SectionAddr = 0;
520 if (
SymEntry.getSectionID() != AbsoluteSymbolSection)
521 SectionAddr = getSectionLoadAddress(
SymEntry.getSectionID());
522 uint64_t TargetAddr = SectionAddr +
SymEntry.getOffset();
528 TargetAddr = modifyAddressBasedOnFlags(TargetAddr,
SymEntry.getFlags());
533 std::map<StringRef, JITEvaluatedSymbol> Result;
535 for (
auto &KV : GlobalSymbolTable) {
536 auto SectionID = KV.second.getSectionID();
537 uint64_t SectionAddr = 0;
538 if (SectionID != AbsoluteSymbolSection)
539 SectionAddr = getSectionLoadAddress(SectionID);
547 void resolveRelocations();
549 void resolveLocalRelocations();
551 static void finalizeAsync(std::unique_ptr<RuntimeDyldImpl> This,
553 std::unique_ptr<MemoryBuffer> UnderlyingBuffer);
555 void reassignSectionAddress(
unsigned SectionID, uint64_t Addr);
557 void mapSectionAddress(
const void *LocalAddress, uint64_t TargetAddress);
568 virtual bool isCompatibleFile(
const ObjectFile &Obj)
const = 0;
571 this->NotifyStubEmitted = std::move(NotifyStubEmitted);
574 virtual void registerEHFrames();
576 void deregisterEHFrames();
579 ObjSectionToIDMap &SectionMap) {
Information about the loaded object.
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
JITEvaluatedSymbol getSymbol(StringRef Name) const
uint8_t * getSymbolLocalAddress(StringRef Name) const
StringRef getSectionContent(unsigned SectionID) const
void swapByteOrder(T &Value)
virtual void setMipsABI(const ObjectFile &Obj)
This class represents lattice values for constants.
void writeInt16BE(uint8_t *Addr, uint16_t Value)
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const
Return the load address of this section with an offset.
virtual uint64_t modifyAddressBasedOnFlags(uint64_t Addr, JITSymbolFlags Flags) const
Modify the given target address based on the given symbol flags.
bool operator==(const RelocationValueRef &Other) const
RuntimeDyld::MemoryManager & MemMgr
RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
iterator find(StringRef Key)
This class is the base class for all object file types.
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend, uint64_t symoffset)
uint8_t * getAddress() const
JITSymbolFlags getFlags() const
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmittedFunction
SectionEntry(StringRef name, uint8_t *address, size_t size, size_t allocationSize, uintptr_t objAddress)
unsigned SectionID
SectionID - the section this relocation points to.
bool IsTargetLittleEndian
virtual bool relocationNeedsStub(const RelocationRef &R) const
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
std::function< void(StringRef FileName, StringRef SectionName, StringRef SymbolName, unsigned SectionID, uint32_t StubOffset)> NotifyStubEmittedFunction
void setNotifyStubEmitted(NotifyStubEmittedFunction NotifyStubEmitted)
This is a value type class that represents a single relocation in the list of relocations in the obje...
std::map< RelocationValueRef, uintptr_t > StubMap
Tagged union holding either a T or a Error.
virtual size_t getGOTEntrySize()
StringRef getErrorString()
virtual Error finalizeLoad(const ObjectFile &ObjImg, ObjSectionToIDMap &SectionMap)
void setLoadAddress(uint64_t LA)
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend)
virtual bool relocationNeedsGot(const RelocationRef &R) const
void setOffset(uint64_t NewOffset)
SmallVector< SectionEntry, 64 > SectionList
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend, unsigned SectionA, uint64_t SectionAOffset, unsigned SectionB, uint64_t SectionBOffset, bool IsPCRel, unsigned Size, bool IsTargetThumbFunc)
Flags for symbols in the JIT.
Symbol resolution interface.
std::vector< SymbolRef > CommonSymbolList
bool operator<(const RelocationValueRef &Other) const
uintptr_t getObjAddress() const
void writeInt32BE(uint8_t *Addr, uint32_t Value)
uint8_t * getSectionAddress(unsigned SectionID) const
uint64_t getOffset() const
static ErrorSuccess success()
Create a success value.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
unsigned getSectionID() const
StringMap< SymbolTableEntry > RTDyldSymbolTable
NotifyStubEmittedFunction NotifyStubEmitted
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
uint64_t getSectionLoadAddress(unsigned SectionID) const
JITSymbolResolver & Resolver
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend, bool IsPCRel, unsigned Size)
unsigned getSymbolSectionID(StringRef Name) const
Symbol info for RuntimeDyld.
Represents a symbol that has been evaluated to an address already.
uintptr_t getStubOffset() const
void setProcessAllSections(bool ProcessAllSections)
This is a value type class that represents a single symbol in the list of symbols in the object file...
std::map< StringRef, JITEvaluatedSymbol > getSymbolTable() const
uint64_t Offset
Offset - offset into the section.
std::map< SectionRef, unsigned > ObjSectionToIDMap
uint8_t * getAddressWithOffset(unsigned OffsetBytes) const
Return the address of this section with an offset.
void writeInt64BE(uint8_t *Addr, uint64_t Value)
uint64_t getLoadAddress() const
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend, unsigned SectionA, uint64_t SectionAOffset, unsigned SectionB, uint64_t SectionBOffset, bool IsPCRel, unsigned Size)
StringRef getName() const
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
SectionEntry - represents a section emitted into memory by the dynamic linker.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SymbolTableEntry(unsigned SectionID, uint64_t Offset, JITSymbolFlags Flags)
LLVM Value Representation.
RTDyldSymbolTable GlobalSymbolTable
Lightweight error class with error context and mandatory checking.
std::unordered_map< unsigned, RelocationList > Relocations
unsigned Size
The size of this relocation (MachO specific).
void advanceStubOffset(unsigned StubSize)
StringRef - Represent a constant reference to a string, i.e.
StringMap< RelocationList > ExternalSymbolRelocations
SmallVector< RelocationEntry, 64 > RelocationList
This is a value type class that represents a single section in the list of sections in the object fil...