1 //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Implementation of the MC-JIT runtime dynamic linker.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ExecutionEngine/RuntimeDyld.h"
14 #include "RuntimeDyldCOFF.h"
15 #include "RuntimeDyldELF.h"
16 #include "RuntimeDyldImpl.h"
17 #include "RuntimeDyldMachO.h"
18 #include "llvm/Object/COFF.h"
19 #include "llvm/Object/ELFObjectFile.h"
20 #include "llvm/Support/Alignment.h"
21 #include "llvm/Support/MSVCErrorWorkarounds.h"
22 #include "llvm/Support/MathExtras.h"
28 using namespace llvm::object
;
30 #define DEBUG_TYPE "dyld"
34 enum RuntimeDyldErrorCode
{
35 GenericRTDyldError
= 1
38 // FIXME: This class is only here to support the transition to llvm::Error. It
39 // will be removed once this transition is complete. Clients should prefer to
40 // deal with the Error value directly, rather than converting to error_code.
41 class RuntimeDyldErrorCategory
: public std::error_category
{
43 const char *name() const noexcept override
{ return "runtimedyld"; }
45 std::string
message(int Condition
) const override
{
46 switch (static_cast<RuntimeDyldErrorCode
>(Condition
)) {
47 case GenericRTDyldError
: return "Generic RuntimeDyld error";
49 llvm_unreachable("Unrecognized RuntimeDyldErrorCode");
55 char RuntimeDyldError::ID
= 0;
57 void RuntimeDyldError::log(raw_ostream
&OS
) const {
61 std::error_code
RuntimeDyldError::convertToErrorCode() const {
62 static RuntimeDyldErrorCategory RTDyldErrorCategory
;
63 return std::error_code(GenericRTDyldError
, RTDyldErrorCategory
);
66 // Empty out-of-line virtual destructor as the key function.
67 RuntimeDyldImpl::~RuntimeDyldImpl() = default;
69 // Pin LoadedObjectInfo's vtables to this file.
70 void RuntimeDyld::LoadedObjectInfo::anchor() {}
74 void RuntimeDyldImpl::registerEHFrames() {}
76 void RuntimeDyldImpl::deregisterEHFrames() {
77 MemMgr
.deregisterEHFrames();
81 static void dumpSectionMemory(const SectionEntry
&S
, StringRef State
) {
82 dbgs() << "----- Contents of section " << S
.getName() << " " << State
85 if (S
.getAddress() == nullptr) {
86 dbgs() << "\n <section not emitted>\n";
90 const unsigned ColsPerRow
= 16;
92 uint8_t *DataAddr
= S
.getAddress();
93 uint64_t LoadAddr
= S
.getLoadAddress();
95 unsigned StartPadding
= LoadAddr
& (ColsPerRow
- 1);
96 unsigned BytesRemaining
= S
.getSize();
99 dbgs() << "\n" << format("0x%016" PRIx64
,
100 LoadAddr
& ~(uint64_t)(ColsPerRow
- 1)) << ":";
101 while (StartPadding
--)
105 while (BytesRemaining
> 0) {
106 if ((LoadAddr
& (ColsPerRow
- 1)) == 0)
107 dbgs() << "\n" << format("0x%016" PRIx64
, LoadAddr
) << ":";
109 dbgs() << " " << format("%02x", *DataAddr
);
120 // Resolve the relocations for all symbols we currently know about.
121 void RuntimeDyldImpl::resolveRelocations() {
122 std::lock_guard
<sys::Mutex
> locked(lock
);
124 // Print out the sections prior to relocation.
126 for (SectionEntry
&S
: Sections
)
127 dumpSectionMemory(S
, "before relocations");
130 // First, resolve relocations associated with external symbols.
131 if (auto Err
= resolveExternalSymbols()) {
133 ErrorStr
= toString(std::move(Err
));
136 resolveLocalRelocations();
138 // Print out sections after relocation.
140 for (SectionEntry
&S
: Sections
)
141 dumpSectionMemory(S
, "after relocations");
145 void RuntimeDyldImpl::resolveLocalRelocations() {
146 // Iterate over all outstanding relocations
147 for (const auto &Rel
: Relocations
) {
148 // The Section here (Sections[i]) refers to the section in which the
149 // symbol for the relocation is located. The SectionID in the relocation
150 // entry provides the section to which the relocation will be applied.
151 unsigned Idx
= Rel
.first
;
152 uint64_t Addr
= getSectionLoadAddress(Idx
);
153 LLVM_DEBUG(dbgs() << "Resolving relocations Section #" << Idx
<< "\t"
154 << format("%p", (uintptr_t)Addr
) << "\n");
155 resolveRelocationList(Rel
.second
, Addr
);
160 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress
,
161 uint64_t TargetAddress
) {
162 std::lock_guard
<sys::Mutex
> locked(lock
);
163 for (unsigned i
= 0, e
= Sections
.size(); i
!= e
; ++i
) {
164 if (Sections
[i
].getAddress() == LocalAddress
) {
165 reassignSectionAddress(i
, TargetAddress
);
169 llvm_unreachable("Attempting to remap address of unknown section!");
172 static Error
getOffset(const SymbolRef
&Sym
, SectionRef Sec
,
174 Expected
<uint64_t> AddressOrErr
= Sym
.getAddress();
176 return AddressOrErr
.takeError();
177 Result
= *AddressOrErr
- Sec
.getAddress();
178 return Error::success();
181 Expected
<RuntimeDyldImpl::ObjSectionToIDMap
>
182 RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile
&Obj
) {
183 std::lock_guard
<sys::Mutex
> locked(lock
);
185 // Save information about our target
186 Arch
= (Triple::ArchType
)Obj
.getArch();
187 IsTargetLittleEndian
= Obj
.isLittleEndian();
190 // Compute the memory size required to load all sections to be loaded
191 // and pass this information to the memory manager
192 if (MemMgr
.needsToReserveAllocationSpace()) {
193 uint64_t CodeSize
= 0, RODataSize
= 0, RWDataSize
= 0;
194 Align CodeAlign
, RODataAlign
, RWDataAlign
;
195 if (auto Err
= computeTotalAllocSize(Obj
, CodeSize
, CodeAlign
, RODataSize
,
196 RODataAlign
, RWDataSize
, RWDataAlign
))
197 return std::move(Err
);
198 MemMgr
.reserveAllocationSpace(CodeSize
, CodeAlign
, RODataSize
, RODataAlign
,
199 RWDataSize
, RWDataAlign
);
202 // Used sections from the object file
203 ObjSectionToIDMap LocalSections
;
205 // Common symbols requiring allocation, with their sizes and alignments
206 CommonSymbolList CommonSymbolsToAllocate
;
208 uint64_t CommonSize
= 0;
209 uint32_t CommonAlign
= 0;
211 // First, collect all weak and common symbols. We need to know if stronger
212 // definitions occur elsewhere.
213 JITSymbolResolver::LookupSet ResponsibilitySet
;
215 JITSymbolResolver::LookupSet Symbols
;
216 for (auto &Sym
: Obj
.symbols()) {
217 Expected
<uint32_t> FlagsOrErr
= Sym
.getFlags();
219 // TODO: Test this error.
220 return FlagsOrErr
.takeError();
221 if ((*FlagsOrErr
& SymbolRef::SF_Common
) ||
222 (*FlagsOrErr
& SymbolRef::SF_Weak
)) {
224 if (auto NameOrErr
= Sym
.getName())
225 Symbols
.insert(*NameOrErr
);
227 return NameOrErr
.takeError();
231 if (auto ResultOrErr
= Resolver
.getResponsibilitySet(Symbols
))
232 ResponsibilitySet
= std::move(*ResultOrErr
);
234 return ResultOrErr
.takeError();
238 LLVM_DEBUG(dbgs() << "Parse symbols:\n");
239 for (symbol_iterator I
= Obj
.symbol_begin(), E
= Obj
.symbol_end(); I
!= E
;
241 Expected
<uint32_t> FlagsOrErr
= I
->getFlags();
243 // TODO: Test this error.
244 return FlagsOrErr
.takeError();
246 // Skip undefined symbols.
247 if (*FlagsOrErr
& SymbolRef::SF_Undefined
)
250 // Get the symbol type.
251 object::SymbolRef::Type SymType
;
252 if (auto SymTypeOrErr
= I
->getType())
253 SymType
= *SymTypeOrErr
;
255 return SymTypeOrErr
.takeError();
259 if (auto NameOrErr
= I
->getName())
262 return NameOrErr
.takeError();
264 // Compute JIT symbol flags.
265 auto JITSymFlags
= getJITSymbolFlags(*I
);
267 return JITSymFlags
.takeError();
269 // If this is a weak definition, check to see if there's a strong one.
270 // If there is, skip this symbol (we won't be providing it: the strong
271 // definition will). If there's no strong definition, make this definition
273 if (JITSymFlags
->isWeak() || JITSymFlags
->isCommon()) {
274 // First check whether there's already a definition in this instance.
275 if (GlobalSymbolTable
.count(Name
))
278 // If we're not responsible for this symbol, skip it.
279 if (!ResponsibilitySet
.count(Name
))
282 // Otherwise update the flags on the symbol to make this definition
284 if (JITSymFlags
->isWeak())
285 *JITSymFlags
&= ~JITSymbolFlags::Weak
;
286 if (JITSymFlags
->isCommon()) {
287 *JITSymFlags
&= ~JITSymbolFlags::Common
;
288 uint32_t Align
= I
->getAlignment();
289 uint64_t Size
= I
->getCommonSize();
292 CommonSize
= alignTo(CommonSize
, Align
) + Size
;
293 CommonSymbolsToAllocate
.push_back(*I
);
297 if (*FlagsOrErr
& SymbolRef::SF_Absolute
&&
298 SymType
!= object::SymbolRef::ST_File
) {
300 if (auto AddrOrErr
= I
->getAddress())
303 return AddrOrErr
.takeError();
305 unsigned SectionID
= AbsoluteSymbolSection
;
307 LLVM_DEBUG(dbgs() << "\tType: " << SymType
<< " (absolute) Name: " << Name
308 << " SID: " << SectionID
309 << " Offset: " << format("%p", (uintptr_t)Addr
)
310 << " flags: " << *FlagsOrErr
<< "\n");
311 // Skip absolute symbol relocations.
313 auto Result
= GlobalSymbolTable
.insert_or_assign(
314 Name
, SymbolTableEntry(SectionID
, Addr
, *JITSymFlags
));
315 processNewSymbol(*I
, Result
.first
->getValue());
317 } else if (SymType
== object::SymbolRef::ST_Function
||
318 SymType
== object::SymbolRef::ST_Data
||
319 SymType
== object::SymbolRef::ST_Unknown
||
320 SymType
== object::SymbolRef::ST_Other
) {
322 section_iterator SI
= Obj
.section_end();
323 if (auto SIOrErr
= I
->getSection())
326 return SIOrErr
.takeError();
328 if (SI
== Obj
.section_end())
331 // Get symbol offset.
333 if (auto Err
= getOffset(*I
, *SI
, SectOffset
))
334 return std::move(Err
);
336 bool IsCode
= SI
->isText();
338 if (auto SectionIDOrErr
=
339 findOrEmitSection(Obj
, *SI
, IsCode
, LocalSections
))
340 SectionID
= *SectionIDOrErr
;
342 return SectionIDOrErr
.takeError();
344 LLVM_DEBUG(dbgs() << "\tType: " << SymType
<< " Name: " << Name
345 << " SID: " << SectionID
346 << " Offset: " << format("%p", (uintptr_t)SectOffset
)
347 << " flags: " << *FlagsOrErr
<< "\n");
348 // Skip absolute symbol relocations.
350 auto Result
= GlobalSymbolTable
.insert_or_assign(
351 Name
, SymbolTableEntry(SectionID
, SectOffset
, *JITSymFlags
));
352 processNewSymbol(*I
, Result
.first
->getValue());
357 // Allocate common symbols
358 if (auto Err
= emitCommonSymbols(Obj
, CommonSymbolsToAllocate
, CommonSize
,
360 return std::move(Err
);
362 // Parse and process relocations
363 LLVM_DEBUG(dbgs() << "Parse relocations:\n");
364 for (section_iterator SI
= Obj
.section_begin(), SE
= Obj
.section_end();
368 Expected
<section_iterator
> RelSecOrErr
= SI
->getRelocatedSection();
370 return RelSecOrErr
.takeError();
372 section_iterator RelocatedSection
= *RelSecOrErr
;
373 if (RelocatedSection
== SE
)
376 relocation_iterator I
= SI
->relocation_begin();
377 relocation_iterator E
= SI
->relocation_end();
379 if (I
== E
&& !ProcessAllSections
)
382 bool IsCode
= RelocatedSection
->isText();
383 unsigned SectionID
= 0;
384 if (auto SectionIDOrErr
= findOrEmitSection(Obj
, *RelocatedSection
, IsCode
,
386 SectionID
= *SectionIDOrErr
;
388 return SectionIDOrErr
.takeError();
390 LLVM_DEBUG(dbgs() << "\tSectionID: " << SectionID
<< "\n");
393 if (auto IOrErr
= processRelocationRef(SectionID
, I
, Obj
, LocalSections
, Stubs
))
396 return IOrErr
.takeError();
398 // If there is a NotifyStubEmitted callback set, call it to register any
399 // stubs created for this section.
400 if (NotifyStubEmitted
) {
401 StringRef FileName
= Obj
.getFileName();
402 StringRef SectionName
= Sections
[SectionID
].getName();
403 for (auto &KV
: Stubs
) {
406 uint64_t StubAddr
= KV
.second
;
408 // If this is a named stub, just call NotifyStubEmitted.
410 NotifyStubEmitted(FileName
, SectionName
, VR
.SymbolName
, SectionID
,
415 // Otherwise we will have to try a reverse lookup on the globla symbol table.
416 for (auto &GSTMapEntry
: GlobalSymbolTable
) {
417 StringRef SymbolName
= GSTMapEntry
.first();
418 auto &GSTEntry
= GSTMapEntry
.second
;
419 if (GSTEntry
.getSectionID() == VR
.SectionID
&&
420 GSTEntry
.getOffset() == VR
.Offset
) {
421 NotifyStubEmitted(FileName
, SectionName
, SymbolName
, SectionID
,
430 // Process remaining sections
431 if (ProcessAllSections
) {
432 LLVM_DEBUG(dbgs() << "Process remaining sections:\n");
433 for (section_iterator SI
= Obj
.section_begin(), SE
= Obj
.section_end();
436 /* Ignore already loaded sections */
437 if (LocalSections
.find(*SI
) != LocalSections
.end())
440 bool IsCode
= SI
->isText();
441 if (auto SectionIDOrErr
=
442 findOrEmitSection(Obj
, *SI
, IsCode
, LocalSections
))
443 LLVM_DEBUG(dbgs() << "\tSectionID: " << (*SectionIDOrErr
) << "\n");
445 return SectionIDOrErr
.takeError();
449 // Give the subclasses a chance to tie-up any loose ends.
450 if (auto Err
= finalizeLoad(Obj
, LocalSections
))
451 return std::move(Err
);
453 // for (auto E : LocalSections)
454 // llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n";
456 return LocalSections
;
459 // A helper method for computeTotalAllocSize.
460 // Computes the memory size required to allocate sections with the given sizes,
461 // assuming that all sections are allocated with the given alignment
463 computeAllocationSizeForSections(std::vector
<uint64_t> &SectionSizes
,
465 uint64_t TotalSize
= 0;
466 for (uint64_t SectionSize
: SectionSizes
)
467 TotalSize
+= alignTo(SectionSize
, Alignment
);
471 static bool isRequiredForExecution(const SectionRef Section
) {
472 const ObjectFile
*Obj
= Section
.getObject();
473 if (isa
<object::ELFObjectFileBase
>(Obj
))
474 return ELFSectionRef(Section
).getFlags() & ELF::SHF_ALLOC
;
475 if (auto *COFFObj
= dyn_cast
<object::COFFObjectFile
>(Obj
)) {
476 const coff_section
*CoffSection
= COFFObj
->getCOFFSection(Section
);
477 // Avoid loading zero-sized COFF sections.
478 // In PE files, VirtualSize gives the section size, and SizeOfRawData
479 // may be zero for sections with content. In Obj files, SizeOfRawData
480 // gives the section size, and VirtualSize is always zero. Hence
481 // the need to check for both cases below.
483 (CoffSection
->VirtualSize
> 0) || (CoffSection
->SizeOfRawData
> 0);
485 CoffSection
->Characteristics
&
486 (COFF::IMAGE_SCN_MEM_DISCARDABLE
| COFF::IMAGE_SCN_LNK_INFO
);
487 return HasContent
&& !IsDiscardable
;
490 assert(isa
<MachOObjectFile
>(Obj
));
494 static bool isReadOnlyData(const SectionRef Section
) {
495 const ObjectFile
*Obj
= Section
.getObject();
496 if (isa
<object::ELFObjectFileBase
>(Obj
))
497 return !(ELFSectionRef(Section
).getFlags() &
498 (ELF::SHF_WRITE
| ELF::SHF_EXECINSTR
));
499 if (auto *COFFObj
= dyn_cast
<object::COFFObjectFile
>(Obj
))
500 return ((COFFObj
->getCOFFSection(Section
)->Characteristics
&
501 (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
502 | COFF::IMAGE_SCN_MEM_READ
503 | COFF::IMAGE_SCN_MEM_WRITE
))
505 (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
506 | COFF::IMAGE_SCN_MEM_READ
));
508 assert(isa
<MachOObjectFile
>(Obj
));
512 static bool isZeroInit(const SectionRef Section
) {
513 const ObjectFile
*Obj
= Section
.getObject();
514 if (isa
<object::ELFObjectFileBase
>(Obj
))
515 return ELFSectionRef(Section
).getType() == ELF::SHT_NOBITS
;
516 if (auto *COFFObj
= dyn_cast
<object::COFFObjectFile
>(Obj
))
517 return COFFObj
->getCOFFSection(Section
)->Characteristics
&
518 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
;
520 auto *MachO
= cast
<MachOObjectFile
>(Obj
);
521 unsigned SectionType
= MachO
->getSectionType(Section
);
522 return SectionType
== MachO::S_ZEROFILL
||
523 SectionType
== MachO::S_GB_ZEROFILL
;
526 static bool isTLS(const SectionRef Section
) {
527 const ObjectFile
*Obj
= Section
.getObject();
528 if (isa
<object::ELFObjectFileBase
>(Obj
))
529 return ELFSectionRef(Section
).getFlags() & ELF::SHF_TLS
;
533 // Compute an upper bound of the memory size that is required to load all
535 Error
RuntimeDyldImpl::computeTotalAllocSize(
536 const ObjectFile
&Obj
, uint64_t &CodeSize
, Align
&CodeAlign
,
537 uint64_t &RODataSize
, Align
&RODataAlign
, uint64_t &RWDataSize
,
538 Align
&RWDataAlign
) {
539 // Compute the size of all sections required for execution
540 std::vector
<uint64_t> CodeSectionSizes
;
541 std::vector
<uint64_t> ROSectionSizes
;
542 std::vector
<uint64_t> RWSectionSizes
;
544 // Collect sizes of all sections to be loaded;
545 // also determine the max alignment of all sections
546 for (section_iterator SI
= Obj
.section_begin(), SE
= Obj
.section_end();
548 const SectionRef
&Section
= *SI
;
550 bool IsRequired
= isRequiredForExecution(Section
) || ProcessAllSections
;
552 // Consider only the sections that are required to be loaded for execution
554 uint64_t DataSize
= Section
.getSize();
555 Align Alignment
= Section
.getAlignment();
556 bool IsCode
= Section
.isText();
557 bool IsReadOnly
= isReadOnlyData(Section
);
558 bool IsTLS
= isTLS(Section
);
560 Expected
<StringRef
> NameOrErr
= Section
.getName();
562 return NameOrErr
.takeError();
563 StringRef Name
= *NameOrErr
;
565 uint64_t StubBufSize
= computeSectionStubBufSize(Obj
, Section
);
567 uint64_t PaddingSize
= 0;
568 if (Name
== ".eh_frame")
570 if (StubBufSize
!= 0)
571 PaddingSize
+= getStubAlignment().value() - 1;
573 uint64_t SectionSize
= DataSize
+ PaddingSize
+ StubBufSize
;
575 // The .eh_frame section (at least on Linux) needs an extra four bytes
577 // with zeroes added at the end. For MachO objects, this section has a
578 // slightly different name, so this won't have any effect for MachO
580 if (Name
== ".eh_frame")
587 CodeAlign
= std::max(CodeAlign
, Alignment
);
588 CodeSectionSizes
.push_back(SectionSize
);
589 } else if (IsReadOnly
) {
590 RODataAlign
= std::max(RODataAlign
, Alignment
);
591 ROSectionSizes
.push_back(SectionSize
);
593 RWDataAlign
= std::max(RWDataAlign
, Alignment
);
594 RWSectionSizes
.push_back(SectionSize
);
599 // Compute Global Offset Table size. If it is not zero we
600 // also update alignment, which is equal to a size of a
602 if (unsigned GotSize
= computeGOTSize(Obj
)) {
603 RWSectionSizes
.push_back(GotSize
);
604 RWDataAlign
= std::max(RWDataAlign
, Align(getGOTEntrySize()));
607 // Compute the size of all common symbols
608 uint64_t CommonSize
= 0;
610 for (symbol_iterator I
= Obj
.symbol_begin(), E
= Obj
.symbol_end(); I
!= E
;
612 Expected
<uint32_t> FlagsOrErr
= I
->getFlags();
614 // TODO: Test this error.
615 return FlagsOrErr
.takeError();
616 if (*FlagsOrErr
& SymbolRef::SF_Common
) {
617 // Add the common symbols to a list. We'll allocate them all below.
618 uint64_t Size
= I
->getCommonSize();
619 Align Alignment
= Align(I
->getAlignment());
620 // If this is the first common symbol, use its alignment as the alignment
621 // for the common symbols section.
623 CommonAlign
= Alignment
;
624 CommonSize
= alignTo(CommonSize
, Alignment
) + Size
;
627 if (CommonSize
!= 0) {
628 RWSectionSizes
.push_back(CommonSize
);
629 RWDataAlign
= std::max(RWDataAlign
, CommonAlign
);
632 if (!CodeSectionSizes
.empty()) {
633 // Add 64 bytes for a potential IFunc resolver stub
634 CodeSectionSizes
.push_back(64);
637 // Compute the required allocation space for each different type of sections
638 // (code, read-only data, read-write data) assuming that all sections are
639 // allocated with the max alignment. Note that we cannot compute with the
640 // individual alignments of the sections, because then the required size
641 // depends on the order, in which the sections are allocated.
642 CodeSize
= computeAllocationSizeForSections(CodeSectionSizes
, CodeAlign
);
643 RODataSize
= computeAllocationSizeForSections(ROSectionSizes
, RODataAlign
);
644 RWDataSize
= computeAllocationSizeForSections(RWSectionSizes
, RWDataAlign
);
646 return Error::success();
650 unsigned RuntimeDyldImpl::computeGOTSize(const ObjectFile
&Obj
) {
651 size_t GotEntrySize
= getGOTEntrySize();
656 for (section_iterator SI
= Obj
.section_begin(), SE
= Obj
.section_end();
659 for (const RelocationRef
&Reloc
: SI
->relocations())
660 if (relocationNeedsGot(Reloc
))
661 GotSize
+= GotEntrySize
;
667 // compute stub buffer size for the given section
668 unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile
&Obj
,
669 const SectionRef
&Section
) {
670 if (!MemMgr
.allowStubAllocation()) {
674 unsigned StubSize
= getMaxStubSize();
678 // FIXME: this is an inefficient way to handle this. We should computed the
679 // necessary section allocation size in loadObject by walking all the sections
681 unsigned StubBufSize
= 0;
682 for (section_iterator SI
= Obj
.section_begin(), SE
= Obj
.section_end();
685 Expected
<section_iterator
> RelSecOrErr
= SI
->getRelocatedSection();
687 report_fatal_error(Twine(toString(RelSecOrErr
.takeError())));
689 section_iterator RelSecI
= *RelSecOrErr
;
690 if (!(RelSecI
== Section
))
693 for (const RelocationRef
&Reloc
: SI
->relocations()) {
694 if (relocationNeedsStub(Reloc
))
695 StubBufSize
+= StubSize
;
696 if (relocationNeedsDLLImportStub(Reloc
))
697 StubBufSize
= sizeAfterAddingDLLImportStub(StubBufSize
);
701 // Get section data size and alignment
702 uint64_t DataSize
= Section
.getSize();
703 Align Alignment
= Section
.getAlignment();
705 // Add stubbuf size alignment
706 Align StubAlignment
= getStubAlignment();
707 Align EndAlignment
= commonAlignment(Alignment
, DataSize
);
708 if (StubAlignment
> EndAlignment
)
709 StubBufSize
+= StubAlignment
.value() - EndAlignment
.value();
713 uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src
,
714 unsigned Size
) const {
716 if (IsTargetLittleEndian
) {
719 Result
= (Result
<< 8) | *Src
--;
722 Result
= (Result
<< 8) | *Src
++;
727 void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value
, uint8_t *Dst
,
728 unsigned Size
) const {
729 if (IsTargetLittleEndian
) {
731 *Dst
++ = Value
& 0xFF;
737 *Dst
-- = Value
& 0xFF;
743 Expected
<JITSymbolFlags
>
744 RuntimeDyldImpl::getJITSymbolFlags(const SymbolRef
&SR
) {
745 return JITSymbolFlags::fromObjectSymbol(SR
);
748 Error
RuntimeDyldImpl::emitCommonSymbols(const ObjectFile
&Obj
,
749 CommonSymbolList
&SymbolsToAllocate
,
751 uint32_t CommonAlign
) {
752 if (SymbolsToAllocate
.empty())
753 return Error::success();
755 // Allocate memory for the section
756 unsigned SectionID
= Sections
.size();
757 uint8_t *Addr
= MemMgr
.allocateDataSection(CommonSize
, CommonAlign
, SectionID
,
758 "<common symbols>", false);
760 report_fatal_error("Unable to allocate memory for common symbols!");
763 SectionEntry("<common symbols>", Addr
, CommonSize
, CommonSize
, 0));
764 memset(Addr
, 0, CommonSize
);
766 LLVM_DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
767 << " new addr: " << format("%p", Addr
)
768 << " DataSize: " << CommonSize
<< "\n");
770 // Assign the address of each symbol
771 for (auto &Sym
: SymbolsToAllocate
) {
772 uint32_t Alignment
= Sym
.getAlignment();
773 uint64_t Size
= Sym
.getCommonSize();
775 if (auto NameOrErr
= Sym
.getName())
778 return NameOrErr
.takeError();
780 // This symbol has an alignment requirement.
781 uint64_t AlignOffset
=
782 offsetToAlignment((uint64_t)Addr
, Align(Alignment
));
784 Offset
+= AlignOffset
;
786 auto JITSymFlags
= getJITSymbolFlags(Sym
);
789 return JITSymFlags
.takeError();
791 LLVM_DEBUG(dbgs() << "Allocating common symbol " << Name
<< " address "
792 << format("%p", Addr
) << "\n");
793 if (!Name
.empty()) // Skip absolute symbol relocations.
794 GlobalSymbolTable
[Name
] =
795 SymbolTableEntry(SectionID
, Offset
, std::move(*JITSymFlags
));
800 return Error::success();
804 RuntimeDyldImpl::emitSection(const ObjectFile
&Obj
,
805 const SectionRef
&Section
,
808 Align Alignment
= Section
.getAlignment();
810 unsigned PaddingSize
= 0;
811 unsigned StubBufSize
= 0;
812 bool IsRequired
= isRequiredForExecution(Section
);
813 bool IsVirtual
= Section
.isVirtual();
814 bool IsZeroInit
= isZeroInit(Section
);
815 bool IsReadOnly
= isReadOnlyData(Section
);
816 bool IsTLS
= isTLS(Section
);
817 uint64_t DataSize
= Section
.getSize();
819 Expected
<StringRef
> NameOrErr
= Section
.getName();
821 return NameOrErr
.takeError();
822 StringRef Name
= *NameOrErr
;
824 StubBufSize
= computeSectionStubBufSize(Obj
, Section
);
826 // The .eh_frame section (at least on Linux) needs an extra four bytes padded
827 // with zeroes added at the end. For MachO objects, this section has a
828 // slightly different name, so this won't have any effect for MachO objects.
829 if (Name
== ".eh_frame")
833 unsigned SectionID
= Sections
.size();
835 uint64_t LoadAddress
= 0;
836 const char *pData
= nullptr;
838 // If this section contains any bits (i.e. isn't a virtual or bss section),
839 // grab a reference to them.
840 if (!IsVirtual
&& !IsZeroInit
) {
841 // In either case, set the location of the unrelocated section in memory,
842 // since we still process relocations for it even if we're not applying them.
843 if (Expected
<StringRef
> E
= Section
.getContents())
846 return E
.takeError();
850 // If there are any stubs then the section alignment needs to be at least as
851 // high as stub alignment or padding calculations may by incorrect when the
852 // section is remapped.
853 if (StubBufSize
!= 0) {
854 Alignment
= std::max(Alignment
, getStubAlignment());
855 PaddingSize
+= getStubAlignment().value() - 1;
858 // Some sections, such as debug info, don't need to be loaded for execution.
859 // Process those only if explicitly requested.
860 if (IsRequired
|| ProcessAllSections
) {
861 Allocate
= DataSize
+ PaddingSize
+ StubBufSize
;
865 auto TLSSection
= MemMgr
.allocateTLSSection(Allocate
, Alignment
.value(),
867 Addr
= TLSSection
.InitializationImage
;
868 LoadAddress
= TLSSection
.Offset
;
870 Addr
= MemMgr
.allocateCodeSection(Allocate
, Alignment
.value(), SectionID
,
873 Addr
= MemMgr
.allocateDataSection(Allocate
, Alignment
.value(), SectionID
,
877 report_fatal_error("Unable to allocate section memory!");
879 // Zero-initialize or copy the data from the image
880 if (IsZeroInit
|| IsVirtual
)
881 memset(Addr
, 0, DataSize
);
883 memcpy(Addr
, pData
, DataSize
);
885 // Fill in any extra bytes we allocated for padding
886 if (PaddingSize
!= 0) {
887 memset(Addr
+ DataSize
, 0, PaddingSize
);
888 // Update the DataSize variable to include padding.
889 DataSize
+= PaddingSize
;
891 // Align DataSize to stub alignment if we have any stubs (PaddingSize will
892 // have been increased above to account for this).
894 DataSize
&= -(uint64_t)getStubAlignment().value();
897 LLVM_DEBUG(dbgs() << "emitSection SectionID: " << SectionID
<< " Name: "
898 << Name
<< " obj addr: " << format("%p", pData
)
899 << " new addr: " << format("%p", Addr
) << " DataSize: "
900 << DataSize
<< " StubBufSize: " << StubBufSize
901 << " Allocate: " << Allocate
<< "\n");
903 // Even if we didn't load the section, we need to record an entry for it
904 // to handle later processing (and by 'handle' I mean don't do anything
905 // with these sections).
909 dbgs() << "emitSection SectionID: " << SectionID
<< " Name: " << Name
910 << " obj addr: " << format("%p", data
.data()) << " new addr: 0"
911 << " DataSize: " << DataSize
<< " StubBufSize: " << StubBufSize
912 << " Allocate: " << Allocate
<< "\n");
916 SectionEntry(Name
, Addr
, DataSize
, Allocate
, (uintptr_t)pData
));
918 // The load address of a TLS section is not equal to the address of its
919 // initialization image
921 Sections
.back().setLoadAddress(LoadAddress
);
922 // Debug info sections are linked as if their load address was zero
924 Sections
.back().setLoadAddress(0);
930 RuntimeDyldImpl::findOrEmitSection(const ObjectFile
&Obj
,
931 const SectionRef
&Section
,
933 ObjSectionToIDMap
&LocalSections
) {
935 unsigned SectionID
= 0;
936 ObjSectionToIDMap::iterator i
= LocalSections
.find(Section
);
937 if (i
!= LocalSections
.end())
938 SectionID
= i
->second
;
940 if (auto SectionIDOrErr
= emitSection(Obj
, Section
, IsCode
))
941 SectionID
= *SectionIDOrErr
;
943 return SectionIDOrErr
.takeError();
944 LocalSections
[Section
] = SectionID
;
949 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry
&RE
,
950 unsigned SectionID
) {
951 Relocations
[SectionID
].push_back(RE
);
954 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry
&RE
,
955 StringRef SymbolName
) {
956 // Relocation by symbol. If the symbol is found in the global symbol table,
957 // create an appropriate section relocation. Otherwise, add it to
958 // ExternalSymbolRelocations.
959 RTDyldSymbolTable::const_iterator Loc
= GlobalSymbolTable
.find(SymbolName
);
960 if (Loc
== GlobalSymbolTable
.end()) {
961 ExternalSymbolRelocations
[SymbolName
].push_back(RE
);
963 assert(!SymbolName
.empty() &&
964 "Empty symbol should not be in GlobalSymbolTable");
965 // Copy the RE since we want to modify its addend.
966 RelocationEntry RECopy
= RE
;
967 const auto &SymInfo
= Loc
->second
;
968 RECopy
.Addend
+= SymInfo
.getOffset();
969 Relocations
[SymInfo
.getSectionID()].push_back(RECopy
);
973 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr
,
974 unsigned AbiVariant
) {
975 if (Arch
== Triple::aarch64
|| Arch
== Triple::aarch64_be
||
976 Arch
== Triple::aarch64_32
) {
977 // This stub has to be able to access the full address space,
978 // since symbol lookup won't necessarily find a handy, in-range,
979 // PLT stub for functions which could be anywhere.
980 // Stub can use ip0 (== x16) to calculate address
981 writeBytesUnaligned(0xd2e00010, Addr
, 4); // movz ip0, #:abs_g3:<addr>
982 writeBytesUnaligned(0xf2c00010, Addr
+4, 4); // movk ip0, #:abs_g2_nc:<addr>
983 writeBytesUnaligned(0xf2a00010, Addr
+8, 4); // movk ip0, #:abs_g1_nc:<addr>
984 writeBytesUnaligned(0xf2800010, Addr
+12, 4); // movk ip0, #:abs_g0_nc:<addr>
985 writeBytesUnaligned(0xd61f0200, Addr
+16, 4); // br ip0
988 } else if (Arch
== Triple::arm
|| Arch
== Triple::armeb
) {
989 // TODO: There is only ARM far stub now. We should add the Thumb stub,
990 // and stubs for branches Thumb - ARM and ARM - Thumb.
991 writeBytesUnaligned(0xe51ff004, Addr
, 4); // ldr pc, [pc, #-4]
993 } else if (Arch
== Triple::loongarch64
) {
994 // lu12i.w $t0, %abs_hi20(addr)
995 // ori $t0, $t0, %abs_lo12(addr)
996 // lu32i.d $t0, %abs64_lo20(addr)
997 // lu52i.d $t0, $t0, %abs64_lo12(addr)
999 writeBytesUnaligned(0x1400000c, Addr
, 4);
1000 writeBytesUnaligned(0x0380018c, Addr
+ 4, 4);
1001 writeBytesUnaligned(0x1600000c, Addr
+ 8, 4);
1002 writeBytesUnaligned(0x0300018c, Addr
+ 12, 4);
1003 writeBytesUnaligned(0x4c000180, Addr
+ 16, 4);
1005 } else if (IsMipsO32ABI
|| IsMipsN32ABI
) {
1006 // 0: 3c190000 lui t9,%hi(addr).
1007 // 4: 27390000 addiu t9,t9,%lo(addr).
1008 // 8: 03200008 jr t9.
1010 const unsigned LuiT9Instr
= 0x3c190000, AdduiT9Instr
= 0x27390000;
1011 const unsigned NopInstr
= 0x0;
1012 unsigned JrT9Instr
= 0x03200008;
1013 if ((AbiVariant
& ELF::EF_MIPS_ARCH
) == ELF::EF_MIPS_ARCH_32R6
||
1014 (AbiVariant
& ELF::EF_MIPS_ARCH
) == ELF::EF_MIPS_ARCH_64R6
)
1015 JrT9Instr
= 0x03200009;
1017 writeBytesUnaligned(LuiT9Instr
, Addr
, 4);
1018 writeBytesUnaligned(AdduiT9Instr
, Addr
+ 4, 4);
1019 writeBytesUnaligned(JrT9Instr
, Addr
+ 8, 4);
1020 writeBytesUnaligned(NopInstr
, Addr
+ 12, 4);
1022 } else if (IsMipsN64ABI
) {
1023 // 0: 3c190000 lui t9,%highest(addr).
1024 // 4: 67390000 daddiu t9,t9,%higher(addr).
1025 // 8: 0019CC38 dsll t9,t9,16.
1026 // c: 67390000 daddiu t9,t9,%hi(addr).
1027 // 10: 0019CC38 dsll t9,t9,16.
1028 // 14: 67390000 daddiu t9,t9,%lo(addr).
1029 // 18: 03200008 jr t9.
1030 // 1c: 00000000 nop.
1031 const unsigned LuiT9Instr
= 0x3c190000, DaddiuT9Instr
= 0x67390000,
1032 DsllT9Instr
= 0x19CC38;
1033 const unsigned NopInstr
= 0x0;
1034 unsigned JrT9Instr
= 0x03200008;
1035 if ((AbiVariant
& ELF::EF_MIPS_ARCH
) == ELF::EF_MIPS_ARCH_64R6
)
1036 JrT9Instr
= 0x03200009;
1038 writeBytesUnaligned(LuiT9Instr
, Addr
, 4);
1039 writeBytesUnaligned(DaddiuT9Instr
, Addr
+ 4, 4);
1040 writeBytesUnaligned(DsllT9Instr
, Addr
+ 8, 4);
1041 writeBytesUnaligned(DaddiuT9Instr
, Addr
+ 12, 4);
1042 writeBytesUnaligned(DsllT9Instr
, Addr
+ 16, 4);
1043 writeBytesUnaligned(DaddiuT9Instr
, Addr
+ 20, 4);
1044 writeBytesUnaligned(JrT9Instr
, Addr
+ 24, 4);
1045 writeBytesUnaligned(NopInstr
, Addr
+ 28, 4);
1047 } else if (Arch
== Triple::ppc64
|| Arch
== Triple::ppc64le
) {
1048 // Depending on which version of the ELF ABI is in use, we need to
1049 // generate one of two variants of the stub. They both start with
1050 // the same sequence to load the target address into r12.
1051 writeInt32BE(Addr
, 0x3D800000); // lis r12, highest(addr)
1052 writeInt32BE(Addr
+4, 0x618C0000); // ori r12, higher(addr)
1053 writeInt32BE(Addr
+8, 0x798C07C6); // sldi r12, r12, 32
1054 writeInt32BE(Addr
+12, 0x658C0000); // oris r12, r12, h(addr)
1055 writeInt32BE(Addr
+16, 0x618C0000); // ori r12, r12, l(addr)
1056 if (AbiVariant
== 2) {
1057 // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
1058 // The address is already in r12 as required by the ABI. Branch to it.
1059 writeInt32BE(Addr
+20, 0xF8410018); // std r2, 24(r1)
1060 writeInt32BE(Addr
+24, 0x7D8903A6); // mtctr r12
1061 writeInt32BE(Addr
+28, 0x4E800420); // bctr
1063 // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
1064 // Load the function address on r11 and sets it to control register. Also
1065 // loads the function TOC in r2 and environment pointer to r11.
1066 writeInt32BE(Addr
+20, 0xF8410028); // std r2, 40(r1)
1067 writeInt32BE(Addr
+24, 0xE96C0000); // ld r11, 0(r12)
1068 writeInt32BE(Addr
+28, 0xE84C0008); // ld r2, 0(r12)
1069 writeInt32BE(Addr
+32, 0x7D6903A6); // mtctr r11
1070 writeInt32BE(Addr
+36, 0xE96C0010); // ld r11, 16(r2)
1071 writeInt32BE(Addr
+40, 0x4E800420); // bctr
1074 } else if (Arch
== Triple::systemz
) {
1075 writeInt16BE(Addr
, 0xC418); // lgrl %r1,.+8
1076 writeInt16BE(Addr
+2, 0x0000);
1077 writeInt16BE(Addr
+4, 0x0004);
1078 writeInt16BE(Addr
+6, 0x07F1); // brc 15,%r1
1079 // 8-byte address stored at Addr + 8
1081 } else if (Arch
== Triple::x86_64
) {
1082 *Addr
= 0xFF; // jmp
1083 *(Addr
+1) = 0x25; // rip
1084 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
1085 } else if (Arch
== Triple::x86
) {
1086 *Addr
= 0xE9; // 32-bit pc-relative jump.
1091 // Assign an address to a symbol name and resolve all the relocations
1092 // associated with it.
1093 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID
,
1095 // The address to use for relocation resolution is not
1096 // the address of the local section buffer. We must be doing
1097 // a remote execution environment of some sort. Relocations can't
1098 // be applied until all the sections have been moved. The client must
1099 // trigger this with a call to MCJIT::finalize() or
1100 // RuntimeDyld::resolveRelocations().
1102 // Addr is a uint64_t because we can't assume the pointer width
1103 // of the target is the same as that of the host. Just use a generic
1104 // "big enough" type.
1106 dbgs() << "Reassigning address for section " << SectionID
<< " ("
1107 << Sections
[SectionID
].getName() << "): "
1108 << format("0x%016" PRIx64
, Sections
[SectionID
].getLoadAddress())
1109 << " -> " << format("0x%016" PRIx64
, Addr
) << "\n");
1110 Sections
[SectionID
].setLoadAddress(Addr
);
1113 void RuntimeDyldImpl::resolveRelocationList(const RelocationList
&Relocs
,
1115 for (const RelocationEntry
&RE
: Relocs
) {
1116 // Ignore relocations for sections that were not loaded
1117 if (RE
.SectionID
!= AbsoluteSymbolSection
&&
1118 Sections
[RE
.SectionID
].getAddress() == nullptr)
1120 resolveRelocation(RE
, Value
);
1124 void RuntimeDyldImpl::applyExternalSymbolRelocations(
1125 const StringMap
<JITEvaluatedSymbol
> ExternalSymbolMap
) {
1126 for (auto &RelocKV
: ExternalSymbolRelocations
) {
1127 StringRef Name
= RelocKV
.first();
1128 RelocationList
&Relocs
= RelocKV
.second
;
1129 if (Name
.size() == 0) {
1130 // This is an absolute symbol, use an address of zero.
1131 LLVM_DEBUG(dbgs() << "Resolving absolute relocations."
1133 resolveRelocationList(Relocs
, 0);
1136 JITSymbolFlags Flags
;
1137 RTDyldSymbolTable::const_iterator Loc
= GlobalSymbolTable
.find(Name
);
1138 if (Loc
== GlobalSymbolTable
.end()) {
1139 auto RRI
= ExternalSymbolMap
.find(Name
);
1140 assert(RRI
!= ExternalSymbolMap
.end() && "No result for symbol");
1141 Addr
= RRI
->second
.getAddress();
1142 Flags
= RRI
->second
.getFlags();
1144 // We found the symbol in our global table. It was probably in a
1145 // Module that we loaded previously.
1146 const auto &SymInfo
= Loc
->second
;
1147 Addr
= getSectionLoadAddress(SymInfo
.getSectionID()) +
1148 SymInfo
.getOffset();
1149 Flags
= SymInfo
.getFlags();
1152 // FIXME: Implement error handling that doesn't kill the host program!
1153 if (!Addr
&& !Resolver
.allowsZeroSymbols())
1154 report_fatal_error(Twine("Program used external function '") + Name
+
1155 "' which could not be resolved!");
1157 // If Resolver returned UINT64_MAX, the client wants to handle this symbol
1158 // manually and we shouldn't resolve its relocations.
1159 if (Addr
!= UINT64_MAX
) {
1161 // Tweak the address based on the symbol flags if necessary.
1162 // For example, this is used by RuntimeDyldMachOARM to toggle the low bit
1163 // if the target symbol is Thumb.
1164 Addr
= modifyAddressBasedOnFlags(Addr
, Flags
);
1166 LLVM_DEBUG(dbgs() << "Resolving relocations Name: " << Name
<< "\t"
1167 << format("0x%lx", Addr
) << "\n");
1168 resolveRelocationList(Relocs
, Addr
);
1172 ExternalSymbolRelocations
.clear();
1175 Error
RuntimeDyldImpl::resolveExternalSymbols() {
1176 StringMap
<JITEvaluatedSymbol
> ExternalSymbolMap
;
1178 // Resolution can trigger emission of more symbols, so iterate until
1179 // we've resolved *everything*.
1181 JITSymbolResolver::LookupSet ResolvedSymbols
;
1184 JITSymbolResolver::LookupSet NewSymbols
;
1186 for (auto &RelocKV
: ExternalSymbolRelocations
) {
1187 StringRef Name
= RelocKV
.first();
1188 if (!Name
.empty() && !GlobalSymbolTable
.count(Name
) &&
1189 !ResolvedSymbols
.count(Name
))
1190 NewSymbols
.insert(Name
);
1193 if (NewSymbols
.empty())
1197 using ExpectedLookupResult
=
1198 MSVCPExpected
<JITSymbolResolver::LookupResult
>;
1200 using ExpectedLookupResult
= Expected
<JITSymbolResolver::LookupResult
>;
1203 auto NewSymbolsP
= std::make_shared
<std::promise
<ExpectedLookupResult
>>();
1204 auto NewSymbolsF
= NewSymbolsP
->get_future();
1205 Resolver
.lookup(NewSymbols
,
1206 [=](Expected
<JITSymbolResolver::LookupResult
> Result
) {
1207 NewSymbolsP
->set_value(std::move(Result
));
1210 auto NewResolverResults
= NewSymbolsF
.get();
1212 if (!NewResolverResults
)
1213 return NewResolverResults
.takeError();
1215 assert(NewResolverResults
->size() == NewSymbols
.size() &&
1216 "Should have errored on unresolved symbols");
1218 for (auto &RRKV
: *NewResolverResults
) {
1219 assert(!ResolvedSymbols
.count(RRKV
.first
) && "Redundant resolution?");
1220 ExternalSymbolMap
.insert(RRKV
);
1221 ResolvedSymbols
.insert(RRKV
.first
);
1226 applyExternalSymbolRelocations(ExternalSymbolMap
);
1228 return Error::success();
1231 void RuntimeDyldImpl::finalizeAsync(
1232 std::unique_ptr
<RuntimeDyldImpl
> This
,
1233 unique_function
<void(object::OwningBinary
<object::ObjectFile
>,
1234 std::unique_ptr
<RuntimeDyld::LoadedObjectInfo
>, Error
)>
1236 object::OwningBinary
<object::ObjectFile
> O
,
1237 std::unique_ptr
<RuntimeDyld::LoadedObjectInfo
> Info
) {
1239 auto SharedThis
= std::shared_ptr
<RuntimeDyldImpl
>(std::move(This
));
1240 auto PostResolveContinuation
=
1241 [SharedThis
, OnEmitted
= std::move(OnEmitted
), O
= std::move(O
),
1242 Info
= std::move(Info
)](
1243 Expected
<JITSymbolResolver::LookupResult
> Result
) mutable {
1245 OnEmitted(std::move(O
), std::move(Info
), Result
.takeError());
1249 /// Copy the result into a StringMap, where the keys are held by value.
1250 StringMap
<JITEvaluatedSymbol
> Resolved
;
1251 for (auto &KV
: *Result
)
1252 Resolved
[KV
.first
] = KV
.second
;
1254 SharedThis
->applyExternalSymbolRelocations(Resolved
);
1255 SharedThis
->resolveLocalRelocations();
1256 SharedThis
->registerEHFrames();
1258 if (SharedThis
->MemMgr
.finalizeMemory(&ErrMsg
))
1259 OnEmitted(std::move(O
), std::move(Info
),
1260 make_error
<StringError
>(std::move(ErrMsg
),
1261 inconvertibleErrorCode()));
1263 OnEmitted(std::move(O
), std::move(Info
), Error::success());
1266 JITSymbolResolver::LookupSet Symbols
;
1268 for (auto &RelocKV
: SharedThis
->ExternalSymbolRelocations
) {
1269 StringRef Name
= RelocKV
.first();
1270 if (Name
.empty()) // Skip absolute symbol relocations.
1272 assert(!SharedThis
->GlobalSymbolTable
.count(Name
) &&
1273 "Name already processed. RuntimeDyld instances can not be re-used "
1274 "when finalizing with finalizeAsync.");
1275 Symbols
.insert(Name
);
1278 if (!Symbols
.empty()) {
1279 SharedThis
->Resolver
.lookup(Symbols
, std::move(PostResolveContinuation
));
1281 PostResolveContinuation(std::map
<StringRef
, JITEvaluatedSymbol
>());
1284 //===----------------------------------------------------------------------===//
1285 // RuntimeDyld class implementation
1287 uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress(
1288 const object::SectionRef
&Sec
) const {
1290 auto I
= ObjSecToIDMap
.find(Sec
);
1291 if (I
!= ObjSecToIDMap
.end())
1292 return RTDyld
.Sections
[I
->second
].getLoadAddress();
1297 RuntimeDyld::MemoryManager::TLSSection
1298 RuntimeDyld::MemoryManager::allocateTLSSection(uintptr_t Size
,
1301 StringRef SectionName
) {
1302 report_fatal_error("allocation of TLS not implemented");
1305 void RuntimeDyld::MemoryManager::anchor() {}
1306 void JITSymbolResolver::anchor() {}
1307 void LegacyJITSymbolResolver::anchor() {}
1309 RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager
&MemMgr
,
1310 JITSymbolResolver
&Resolver
)
1311 : MemMgr(MemMgr
), Resolver(Resolver
) {
1312 // FIXME: There's a potential issue lurking here if a single instance of
1313 // RuntimeDyld is used to load multiple objects. The current implementation
1314 // associates a single memory manager with a RuntimeDyld instance. Even
1315 // though the public class spawns a new 'impl' instance for each load,
1316 // they share a single memory manager. This can become a problem when page
1317 // permissions are applied.
1319 ProcessAllSections
= false;
1322 RuntimeDyld::~RuntimeDyld() = default;
1324 static std::unique_ptr
<RuntimeDyldCOFF
>
1325 createRuntimeDyldCOFF(
1326 Triple::ArchType Arch
, RuntimeDyld::MemoryManager
&MM
,
1327 JITSymbolResolver
&Resolver
, bool ProcessAllSections
,
1328 RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted
) {
1329 std::unique_ptr
<RuntimeDyldCOFF
> Dyld
=
1330 RuntimeDyldCOFF::create(Arch
, MM
, Resolver
);
1331 Dyld
->setProcessAllSections(ProcessAllSections
);
1332 Dyld
->setNotifyStubEmitted(std::move(NotifyStubEmitted
));
1336 static std::unique_ptr
<RuntimeDyldELF
>
1337 createRuntimeDyldELF(Triple::ArchType Arch
, RuntimeDyld::MemoryManager
&MM
,
1338 JITSymbolResolver
&Resolver
, bool ProcessAllSections
,
1339 RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted
) {
1340 std::unique_ptr
<RuntimeDyldELF
> Dyld
=
1341 RuntimeDyldELF::create(Arch
, MM
, Resolver
);
1342 Dyld
->setProcessAllSections(ProcessAllSections
);
1343 Dyld
->setNotifyStubEmitted(std::move(NotifyStubEmitted
));
1347 static std::unique_ptr
<RuntimeDyldMachO
>
1348 createRuntimeDyldMachO(
1349 Triple::ArchType Arch
, RuntimeDyld::MemoryManager
&MM
,
1350 JITSymbolResolver
&Resolver
,
1351 bool ProcessAllSections
,
1352 RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted
) {
1353 std::unique_ptr
<RuntimeDyldMachO
> Dyld
=
1354 RuntimeDyldMachO::create(Arch
, MM
, Resolver
);
1355 Dyld
->setProcessAllSections(ProcessAllSections
);
1356 Dyld
->setNotifyStubEmitted(std::move(NotifyStubEmitted
));
1360 std::unique_ptr
<RuntimeDyld::LoadedObjectInfo
>
1361 RuntimeDyld::loadObject(const ObjectFile
&Obj
) {
1365 createRuntimeDyldELF(static_cast<Triple::ArchType
>(Obj
.getArch()),
1366 MemMgr
, Resolver
, ProcessAllSections
,
1367 std::move(NotifyStubEmitted
));
1368 else if (Obj
.isMachO())
1369 Dyld
= createRuntimeDyldMachO(
1370 static_cast<Triple::ArchType
>(Obj
.getArch()), MemMgr
, Resolver
,
1371 ProcessAllSections
, std::move(NotifyStubEmitted
));
1372 else if (Obj
.isCOFF())
1373 Dyld
= createRuntimeDyldCOFF(
1374 static_cast<Triple::ArchType
>(Obj
.getArch()), MemMgr
, Resolver
,
1375 ProcessAllSections
, std::move(NotifyStubEmitted
));
1377 report_fatal_error("Incompatible object format!");
1380 if (!Dyld
->isCompatibleFile(Obj
))
1381 report_fatal_error("Incompatible object format!");
1383 auto LoadedObjInfo
= Dyld
->loadObject(Obj
);
1384 MemMgr
.notifyObjectLoaded(*this, Obj
);
1385 return LoadedObjInfo
;
1388 void *RuntimeDyld::getSymbolLocalAddress(StringRef Name
) const {
1391 return Dyld
->getSymbolLocalAddress(Name
);
1394 unsigned RuntimeDyld::getSymbolSectionID(StringRef Name
) const {
1395 assert(Dyld
&& "No RuntimeDyld instance attached");
1396 return Dyld
->getSymbolSectionID(Name
);
1399 JITEvaluatedSymbol
RuntimeDyld::getSymbol(StringRef Name
) const {
1402 return Dyld
->getSymbol(Name
);
1405 std::map
<StringRef
, JITEvaluatedSymbol
> RuntimeDyld::getSymbolTable() const {
1407 return std::map
<StringRef
, JITEvaluatedSymbol
>();
1408 return Dyld
->getSymbolTable();
1411 void RuntimeDyld::resolveRelocations() { Dyld
->resolveRelocations(); }
1413 void RuntimeDyld::reassignSectionAddress(unsigned SectionID
, uint64_t Addr
) {
1414 Dyld
->reassignSectionAddress(SectionID
, Addr
);
1417 void RuntimeDyld::mapSectionAddress(const void *LocalAddress
,
1418 uint64_t TargetAddress
) {
1419 Dyld
->mapSectionAddress(LocalAddress
, TargetAddress
);
1422 bool RuntimeDyld::hasError() { return Dyld
->hasError(); }
1424 StringRef
RuntimeDyld::getErrorString() { return Dyld
->getErrorString(); }
1426 void RuntimeDyld::finalizeWithMemoryManagerLocking() {
1427 bool MemoryFinalizationLocked
= MemMgr
.FinalizationLocked
;
1428 MemMgr
.FinalizationLocked
= true;
1429 resolveRelocations();
1431 if (!MemoryFinalizationLocked
) {
1432 MemMgr
.finalizeMemory();
1433 MemMgr
.FinalizationLocked
= false;
1437 StringRef
RuntimeDyld::getSectionContent(unsigned SectionID
) const {
1438 assert(Dyld
&& "No Dyld instance attached");
1439 return Dyld
->getSectionContent(SectionID
);
1442 uint64_t RuntimeDyld::getSectionLoadAddress(unsigned SectionID
) const {
1443 assert(Dyld
&& "No Dyld instance attached");
1444 return Dyld
->getSectionLoadAddress(SectionID
);
1447 void RuntimeDyld::registerEHFrames() {
1449 Dyld
->registerEHFrames();
1452 void RuntimeDyld::deregisterEHFrames() {
1454 Dyld
->deregisterEHFrames();
1456 // FIXME: Kill this with fire once we have a new JIT linker: this is only here
1457 // so that we can re-use RuntimeDyld's implementation without twisting the
1458 // interface any further for ORC's purposes.
1460 object::OwningBinary
<object::ObjectFile
> O
,
1461 RuntimeDyld::MemoryManager
&MemMgr
, JITSymbolResolver
&Resolver
,
1462 bool ProcessAllSections
,
1463 unique_function
<Error(const object::ObjectFile
&Obj
,
1464 RuntimeDyld::LoadedObjectInfo
&LoadedObj
,
1465 std::map
<StringRef
, JITEvaluatedSymbol
>)>
1467 unique_function
<void(object::OwningBinary
<object::ObjectFile
>,
1468 std::unique_ptr
<RuntimeDyld::LoadedObjectInfo
>, Error
)>
1471 RuntimeDyld
RTDyld(MemMgr
, Resolver
);
1472 RTDyld
.setProcessAllSections(ProcessAllSections
);
1474 auto Info
= RTDyld
.loadObject(*O
.getBinary());
1476 if (RTDyld
.hasError()) {
1477 OnEmitted(std::move(O
), std::move(Info
),
1478 make_error
<StringError
>(RTDyld
.getErrorString(),
1479 inconvertibleErrorCode()));
1483 if (auto Err
= OnLoaded(*O
.getBinary(), *Info
, RTDyld
.getSymbolTable())) {
1484 OnEmitted(std::move(O
), std::move(Info
), std::move(Err
));
1488 RuntimeDyldImpl::finalizeAsync(std::move(RTDyld
.Dyld
), std::move(OnEmitted
),
1489 std::move(O
), std::move(Info
));
1492 } // end namespace llvm