LLVM: lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
14#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
15
30#include
31#include
32#include <system_error>
33#include <unordered_map>
34
35using namespace llvm;
37
38namespace llvm {
39
40#define UNIMPLEMENTED_RELOC(RelType) \
41 case RelType: \
42 return make_error("Unimplemented relocation: " #RelType)
43
44
45
47
48 std::string Name;
49
50
52
53
54 size_t Size;
55
56
57
58
59
61
62
63
64 uintptr_t StubOffset;
65
66
67
68 size_t AllocationSize;
69
70
71
72 uintptr_t ObjAddress;
73
74public:
76 size_t allocationSize, uintptr_t objAddress)
77 : Name(std::string(name)), Address(address), Size(size),
78 LoadAddress(reinterpret_cast<uintptr_t>(address)), StubOffset(size),
79 AllocationSize(allocationSize), ObjAddress(objAddress) {
80
81
82 (void)AllocationSize;
83 }
84
86
88
89
91 assert(OffsetBytes <= AllocationSize && "Offset out of bounds!");
92 return Address + OffsetBytes;
93 }
94
95 size_t getSize() const { return Size; }
96
99
100
102 assert(OffsetBytes <= AllocationSize && "Offset out of bounds!");
103 return LoadAddress + OffsetBytes;
104 }
105
107
109 StubOffset += StubSize;
110 assert(StubOffset <= AllocationSize && "Not enough space allocated!");
111 }
112
114};
115
116
117
119public:
120
122
123
124
126
127
129
130
132
137
138
139
140 union {
143 };
144
145
147
148
150
151
153
157
163
168
170 unsigned SectionA, uint64_t SectionAOffset, unsigned SectionB,
172 : Offset(offset), Addend(SectionAOffset - SectionBOffset + addend),
175 Sections.SectionA = SectionA;
176 Sections.SectionB = SectionB;
177 }
178
180 unsigned SectionA, uint64_t SectionAOffset, unsigned SectionB,
183 : Offset(offset), Addend(SectionAOffset - SectionBOffset + addend),
186 Sections.SectionA = SectionA;
187 Sections.SectionB = SectionB;
188 }
189};
190
210
211
213public:
215
217 : Offset(Offset), SectionID(SectionID), Flags(Flags) {}
218
222
224
225private:
227 unsigned SectionID = 0;
229};
230
232
235protected:
237
238
240
241
243
244
245
246
247
250
252#define RTDYLD_INVALID_SECTION_ID ((RuntimeDyldImpl::SID)(-1))
253
254
255
257
258
260
261
263
264
265
266
267
268
270
271
272
273 std::unordered_map<unsigned, RelocationList> Relocations;
274
275
276
277
279
280
281 typedef std::map<RelocationValueRef, uintptr_t> StubMap;
282
288
289
290
292
293
294
295
296
297
298
299
300
301
302
304
308
311
314
321
328
335
341
342
344
345
346
348
349
351
352
353
354
355
360
361
362
363
367
368
369
370
371
374 bool IsCode);
375
376
377
378
379
380
382 const SectionRef &Section, bool IsCode,
384
385
387
388
389
391
392
393
395
396
398
399
400
401
403
404
405
406
407
412
415
416
418
419
420
424 Align &RWDataAlign);
425
426
428
429
432
433
435
436
438
439
440
442
443
447
448
452
453
457
458
462
463public:
469
471
475
476 virtual std::unique_ptrRuntimeDyld::LoadedObjectInfo
478
481 return 0;
482 else
483 return Sections[SectionID].getLoadAddress();
484 }
485
488 return nullptr;
489 else
490 return Sections[SectionID].getAddress();
491 }
492
495 return {};
496 else
498 reinterpret_cast<char *>(Sections[SectionID].getAddress()),
500 }
501
503
504
507 return nullptr;
508 const auto &SymInfo = pos->second;
509
511 return nullptr;
513 }
514
518 return ~0U;
519 return GSTItr->second.getSectionID();
520 }
521
523
524
527 return nullptr;
528 const auto &SymEntry = pos->second;
533
534
535
536
537
540 }
541
543 std::map<StringRef, JITEvaluatedSymbol> Result;
544
546 auto SectionID = KV.second.getSectionID();
548 Result[KV.first()] =
549 JITEvaluatedSymbol(SectionAddr + KV.second.getOffset(), KV.second.getFlags());
550 }
551
552 return Result;
553 }
554
556
558
560 std::unique_ptr This,
562 std::unique_ptrRuntimeDyld::LoadedObjectInfo,
564 OnEmitted,
566 std::unique_ptrRuntimeDyld::LoadedObjectInfo Info);
567
569
571
572
574
575
577
578
580
582
586
588
590
595};
596
597}
598
599#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
This file defines the SmallVector class.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Represents a symbol that has been evaluated to an address already.
Flags for symbols in the JIT.
Symbol resolution interface.
RelocationEntry - used to represent relocations internally in the dynamic linker.
Definition RuntimeDyldImpl.h:118
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend)
Definition RuntimeDyldImpl.h:154
uint64_t SymOffset
Definition RuntimeDyldImpl.h:141
bool IsTargetThumbFunc
Definition RuntimeDyldImpl.h:152
unsigned Size
The size of this relocation (MachO specific).
Definition RuntimeDyldImpl.h:146
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend, bool IsPCRel, unsigned Size)
Definition RuntimeDyldImpl.h:164
uint32_t RelType
RelType - relocation type.
Definition RuntimeDyldImpl.h:131
uint64_t Offset
Offset - offset into the section.
Definition RuntimeDyldImpl.h:121
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend, uint64_t symoffset)
Definition RuntimeDyldImpl.h:158
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)
Definition RuntimeDyldImpl.h:179
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
Definition RuntimeDyldImpl.h:149
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
Definition RuntimeDyldImpl.h:125
unsigned SectionID
SectionID - the section this relocation points to.
Definition RuntimeDyldImpl.h:128
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)
Definition RuntimeDyldImpl.h:169
SectionPair Sections
Definition RuntimeDyldImpl.h:142
Definition RuntimeDyldImpl.h:191
const char * SymbolName
Definition RuntimeDyldImpl.h:196
int64_t Addend
Definition RuntimeDyldImpl.h:195
bool operator<(const RelocationValueRef &Other) const
Definition RuntimeDyldImpl.h:204
uint64_t Offset
Definition RuntimeDyldImpl.h:194
bool operator==(const RelocationValueRef &Other) const
Definition RuntimeDyldImpl.h:199
bool IsStubThumb
Definition RuntimeDyldImpl.h:197
unsigned SectionID
Definition RuntimeDyldImpl.h:193
virtual bool relocationNeedsGot(const RelocationRef &R) const
Definition RuntimeDyldImpl.h:444
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress)
virtual void processNewSymbol(const SymbolRef &ObjSymbol, SymbolTableEntry &Entry)
Definition RuntimeDyldImpl.h:441
StringMap< RelocationList > ExternalSymbolRelocations
Definition RuntimeDyldImpl.h:278
void reassignSectionAddress(unsigned SectionID, uint64_t Addr)
NotifyStubEmittedFunction NotifyStubEmitted
Definition RuntimeDyldImpl.h:307
virtual uint64_t modifyAddressBasedOnFlags(uint64_t Addr, JITSymbolFlags Flags) const
Modify the given target address based on the given symbol flags.
Definition RuntimeDyldImpl.h:356
virtual Expected< relocation_iterator > processRelocationRef(unsigned SectionID, relocation_iterator RelI, const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs)=0
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
std::map< SectionRef, unsigned > ObjSectionToIDMap
Definition RuntimeDyldImpl.h:256
virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value)=0
A object file specific relocation resolver.
virtual Align getStubAlignment()=0
virtual bool isCompatibleFile(const ObjectFile &Obj) const =0
RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmittedFunction
Definition RuntimeDyldImpl.h:305
void writeInt32BE(uint8_t *Addr, uint32_t Value)
Definition RuntimeDyldImpl.h:322
virtual Error finalizeLoad(const ObjectFile &ObjImg, ObjSectionToIDMap &SectionMap)
Definition RuntimeDyldImpl.h:591
RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Definition RuntimeDyldImpl.h:464
void applyExternalSymbolRelocations(const StringMap< JITEvaluatedSymbol > ExternalSymbolMap)
sys::Mutex lock
Definition RuntimeDyldImpl.h:303
bool IsMipsN64ABI
Definition RuntimeDyldImpl.h:287
void resolveRelocationList(const RelocationList &Relocs, uint64_t Value)
Resolves relocations from Relocs list with address from Value.
void writeInt64BE(uint8_t *Addr, uint64_t Value)
Definition RuntimeDyldImpl.h:329
SmallVector< RelocationEntry, 64 > RelocationList
Definition RuntimeDyldImpl.h:269
std::string ErrorStr
Definition RuntimeDyldImpl.h:313
StringRef getErrorString()
Definition RuntimeDyldImpl.h:579
std::map< RelocationValueRef, uintptr_t > StubMap
Definition RuntimeDyldImpl.h:281
JITEvaluatedSymbol getSymbol(StringRef Name) const
Definition RuntimeDyldImpl.h:522
std::map< StringRef, JITEvaluatedSymbol > getSymbolTable() const
Definition RuntimeDyldImpl.h:542
void deregisterEHFrames()
void writeInt16BE(uint8_t *Addr, uint16_t Value)
Definition RuntimeDyldImpl.h:315
static const unsigned AbsoluteSymbolSection
Definition RuntimeDyldImpl.h:236
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
StringRef getSectionContent(unsigned SectionID) const
Definition RuntimeDyldImpl.h:493
virtual void registerEHFrames()
bool IsTargetLittleEndian
Definition RuntimeDyldImpl.h:284
JITSymbolResolver & Resolver
Definition RuntimeDyldImpl.h:242
virtual ~RuntimeDyldImpl()
static void finalizeAsync(std::unique_ptr< RuntimeDyldImpl > This, unique_function< void(object::OwningBinary< object::ObjectFile >, std::unique_ptr< RuntimeDyld::LoadedObjectInfo >, Error)> OnEmitted, object::OwningBinary< object::ObjectFile > O, std::unique_ptr< RuntimeDyld::LoadedObjectInfo > Info)
bool ProcessAllSections
Definition RuntimeDyldImpl.h:291
Error emitCommonSymbols(const ObjectFile &Obj, CommonSymbolList &CommonSymbols, uint64_t CommonSize, uint32_t CommonAlign)
Given the common symbols discovered in the object file, emit a new section for them and update the sy...
bool hasError()
Definition RuntimeDyldImpl.h:573
virtual std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &Obj)=0
Expected< unsigned > emitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode)
Emits section data from the object file to the MemoryManager.
virtual unsigned sizeAfterAddingDLLImportStub(unsigned Size) const
Definition RuntimeDyldImpl.h:459
unsigned SID
Definition RuntimeDyldImpl.h:251
std::unordered_map< unsigned, RelocationList > Relocations
Definition RuntimeDyldImpl.h:273
bool IsMipsN32ABI
Definition RuntimeDyldImpl.h:286
std::vector< SymbolRef > CommonSymbolList
Definition RuntimeDyldImpl.h:262
RuntimeDyld::MemoryManager & MemMgr
Definition RuntimeDyldImpl.h:239
void resolveLocalRelocations()
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
bool IsMipsO32ABI
Definition RuntimeDyldImpl.h:285
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
virtual bool relocationNeedsStub(const RelocationRef &R) const
Definition RuntimeDyldImpl.h:449
Triple::ArchType Arch
Definition RuntimeDyldImpl.h:283
unsigned getSymbolSectionID(StringRef Name) const
Definition RuntimeDyldImpl.h:515
void setProcessAllSections(bool ProcessAllSections)
Definition RuntimeDyldImpl.h:472
unsigned computeGOTSize(const ObjectFile &Obj)
uint8_t * getSymbolLocalAddress(StringRef Name) const
Definition RuntimeDyldImpl.h:502
virtual void setMipsABI(const ObjectFile &Obj)
Definition RuntimeDyldImpl.h:336
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
bool HasError
Definition RuntimeDyldImpl.h:312
std::deque< SectionEntry > SectionList
Definition RuntimeDyldImpl.h:248
virtual size_t getGOTEntrySize()
Definition RuntimeDyldImpl.h:437
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
virtual bool relocationNeedsDLLImportStub(const RelocationRef &R) const
Definition RuntimeDyldImpl.h:454
uint64_t getSectionLoadAddress(unsigned SectionID) const
Definition RuntimeDyldImpl.h:479
void clearError()
Definition RuntimeDyldImpl.h:576
virtual unsigned getMaxStubSize() const =0
uint8_t * getSectionAddress(unsigned SectionID) const
Definition RuntimeDyldImpl.h:486
void setNotifyStubEmitted(NotifyStubEmittedFunction NotifyStubEmitted)
Definition RuntimeDyldImpl.h:583
Error computeTotalAllocSize(const ObjectFile &Obj, uint64_t &CodeSize, Align &CodeAlign, uint64_t &RODataSize, Align &RODataAlign, uint64_t &RWDataSize, Align &RWDataAlign)
unsigned computeSectionStubBufSize(const ObjectFile &Obj, const SectionRef &Section)
void resolveRelocations()
RTDyldSymbolTable GlobalSymbolTable
Definition RuntimeDyldImpl.h:259
SectionList Sections
Definition RuntimeDyldImpl.h:249
virtual Expected< JITSymbolFlags > getJITSymbolFlags(const SymbolRef &Sym)
Generate JITSymbolFlags from a libObject symbol.
Expected< ObjSectionToIDMap > loadObjectImpl(const object::ObjectFile &Obj)
Error resolveExternalSymbols()
Resolve relocations to external symbols.
Information about the loaded object.
std::function< void( StringRef FileName, StringRef SectionName, StringRef SymbolName, unsigned SectionID, uint32_t StubOffset)> NotifyStubEmittedFunction
uint8_t * getAddressWithOffset(unsigned OffsetBytes) const
Return the address of this section with an offset.
Definition RuntimeDyldImpl.h:90
SectionEntry(StringRef name, uint8_t *address, size_t size, size_t allocationSize, uintptr_t objAddress)
Definition RuntimeDyldImpl.h:75
void advanceStubOffset(unsigned StubSize)
Definition RuntimeDyldImpl.h:108
void setLoadAddress(uint64_t LA)
Definition RuntimeDyldImpl.h:98
uintptr_t getStubOffset() const
Definition RuntimeDyldImpl.h:106
uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const
Return the load address of this section with an offset.
Definition RuntimeDyldImpl.h:101
uintptr_t getObjAddress() const
Definition RuntimeDyldImpl.h:113
StringRef getName() const
Definition RuntimeDyldImpl.h:85
uint8_t * getAddress() const
Definition RuntimeDyldImpl.h:87
size_t getSize() const
Definition RuntimeDyldImpl.h:95
uint64_t getLoadAddress() const
Definition RuntimeDyldImpl.h:97
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
StringMapIterBase< SymbolTableEntry, true > const_iterator
StringRef - Represent a constant reference to a string, i.e.
Symbol info for RuntimeDyld.
Definition RuntimeDyldImpl.h:212
uint64_t getOffset() const
Definition RuntimeDyldImpl.h:220
SymbolTableEntry(unsigned SectionID, uint64_t Offset, JITSymbolFlags Flags)
Definition RuntimeDyldImpl.h:216
unsigned getSectionID() const
Definition RuntimeDyldImpl.h:219
JITSymbolFlags getFlags() const
Definition RuntimeDyldImpl.h:223
void setOffset(uint64_t NewOffset)
Definition RuntimeDyldImpl.h:221
SymbolTableEntry()=default
LLVM Value Representation.
This class is the base class for all object file types.
This is a value type class that represents a single relocation in the list of relocations in the obje...
This is a value type class that represents a single section in the list of sections in the object fil...
This is a value type class that represents a single symbol in the list of symbols in the object file.
unique_function is a type-erasing functor similar to std::function.
content_iterator< RelocationRef > relocation_iterator
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
SmartMutex< false > Mutex
Mutex - A standard, always enforced mutex.
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
StringMap< SymbolTableEntry > RTDyldSymbolTable
Definition RuntimeDyldImpl.h:231
Implement std::hash so that hash_code can be used in STL containers.
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition RuntimeDyldImpl.h:133
uint32_t SectionB
Definition RuntimeDyldImpl.h:135
uint32_t SectionA
Definition RuntimeDyldImpl.h:134