23#define DEBUG_TYPE "dyld"
27class LoadedMachOObjectInfo final
29 RuntimeDyld::LoadedObjectInfo> {
32 ObjSectionToIDMap ObjSecToIDMap)
36 getObjectForDebug(
const ObjectFile &Obj)
const override {
46 unsigned NumBytes = 1 << RE.
Size;
57 bool TargetIsLocalThumbFunc) {
69 unsigned NumBytes = 1 <<
Size;
77 bool IsCode = TargetSection.
isText();
79 if (
auto TargetSectionIDOrErr =
81 TargetSectionID = *TargetSectionIDOrErr;
83 return TargetSectionIDOrErr.takeError();
85 Addend -= SectionBaseAddr;
87 R.IsTargetThumbFunc = TargetIsLocalThumbFunc;
110 if (
auto TargetNameOrErr = Symbol->getName())
111 TargetName = *TargetNameOrErr;
113 return TargetNameOrErr.takeError();
117 const auto &
SymInfo = SI->second;
126 bool IsCode = Sec.
isText();
129 Value.SectionID = *SectionIDOrErr;
131 return SectionIDOrErr.takeError();
141 unsigned OffsetToNextPC) {
142 auto &O = *cast<MachOObjectFile>(RI->getObject());
154 <<
" LocalAddress: " <<
format(
"%p", LocalAddress)
155 <<
" FinalAddress: " <<
format(
"0x%016" PRIx64, FinalAddress)
158 <<
" Size: " << (1 << RE.
Size) <<
"\n";
167 for (; SI != SE; ++SI) {
170 if ((
Addr >= SAddr) && (
Addr < SAddr + SSize))
182 unsigned PTSectionID) {
184 "Pointer table section not supported in 64-bit MachO.");
189 unsigned FirstIndirectSymbol = Sec32.
reserved1;
190 const unsigned PTEntrySize = 4;
191 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
192 unsigned PTEntryOffset = 0;
194 assert((PTSectionSize % PTEntrySize) == 0 &&
195 "Pointers section does not contain a whole number of stubs?");
199 << PTSectionID <<
", " << NumPTEntries <<
" entries, "
200 << PTEntrySize <<
" bytes each:\n");
202 for (
unsigned i = 0; i < NumPTEntries; ++i) {
203 unsigned SymbolIndex =
207 if (
auto IndirectSymbolNameOrErr = SI->getName())
208 IndirectSymbolName = *IndirectSymbolNameOrErr;
210 return IndirectSymbolNameOrErr.takeError();
211 LLVM_DEBUG(
dbgs() <<
" " << IndirectSymbolName <<
": index " << SymbolIndex
212 <<
", PT offset: " << PTEntryOffset <<
"\n");
216 PTEntryOffset += PTEntrySize;
225template <
typename Impl>
233 for (
const auto &Section : Obj.
sections()) {
243 if (
Name ==
"__text") {
244 if (
auto TextSIDOrErr = findOrEmitSection(Obj, Section,
true, SectionMap))
245 TextSID = *TextSIDOrErr;
247 return TextSIDOrErr.takeError();
248 }
else if (
Name ==
"__eh_frame") {
249 if (
auto EHFrameSIDOrErr = findOrEmitSection(Obj, Section,
false,
251 EHFrameSID = *EHFrameSIDOrErr;
253 return EHFrameSIDOrErr.takeError();
254 }
else if (
Name ==
"__gcc_except_tab") {
255 if (
auto ExceptTabSIDOrErr = findOrEmitSection(Obj, Section,
true,
257 ExceptTabSID = *ExceptTabSIDOrErr;
259 return ExceptTabSIDOrErr.takeError();
261 auto I = SectionMap.find(Section);
262 if (
I != SectionMap.end())
263 if (
auto Err =
impl().finalizeSection(Obj,
I->second, Section))
267 UnregisteredEHFrameSections.push_back(
273template <
typename Impl>
275 int64_t DeltaForText,
276 int64_t DeltaForEH) {
277 typedef typename Impl::TargetPtrT TargetPtrT;
279 LLVM_DEBUG(
dbgs() <<
"Processing FDE: Delta for text: " << DeltaForText
280 <<
", Delta for EH: " << DeltaForEH <<
"\n");
289 TargetPtrT FDELocation = readBytesUnaligned(
P,
sizeof(TargetPtrT));
290 TargetPtrT NewLocation = FDELocation - DeltaForText;
291 writeBytesUnaligned(NewLocation,
P,
sizeof(TargetPtrT));
293 P +=
sizeof(TargetPtrT);
296 P +=
sizeof(TargetPtrT);
300 if (Augmentationsize != 0) {
301 TargetPtrT LSDA = readBytesUnaligned(
P,
sizeof(TargetPtrT));
302 TargetPtrT NewLSDA = LSDA - DeltaForEH;
303 writeBytesUnaligned(NewLSDA,
P,
sizeof(TargetPtrT));
310 int64_t ObjDistance =
static_cast<int64_t
>(
A->getObjAddress()) -
311 static_cast<int64_t
>(
B->getObjAddress());
312 int64_t MemDistance =
A->getLoadAddress() -
B->getLoadAddress();
313 return ObjDistance - MemDistance;
316template <
typename Impl>
319 for (
int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
331 int64_t DeltaForEH = 0;
338 P = processFDE(
P, DeltaForText, DeltaForEH);
344 UnregisteredEHFrameSections.clear();
347std::unique_ptr<RuntimeDyldMachO>
358 return std::make_unique<RuntimeDyldMachOAArch64>(
MemMgr,
Resolver);
360 return std::make_unique<RuntimeDyldMachOAArch64>(
MemMgr,
Resolver);
364 return std::make_unique<RuntimeDyldMachOX86_64>(
MemMgr,
Resolver);
368std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
371 return std::make_unique<LoadedMachOObjectInfo>(*
this,
372 *ObjSectionToIDOrErr);
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
place backedge safepoints impl
static StringRef getName(Value *V)
#define RTDYLD_INVALID_SECTION_ID
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
Symbol resolution interface.
RelocationEntry - used to represent relocations internally in the dynamic linker.
unsigned Size
The size of this relocation (MachO specific).
uint32_t RelType
RelType - relocation type.
uint64_t Offset
Offset - offset into the section.
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
unsigned SectionID
SectionID - the section this relocation points to.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
std::map< SectionRef, unsigned > ObjSectionToIDMap
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
RuntimeDyld::MemoryManager & MemMgr
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
RTDyldSymbolTable GlobalSymbolTable
Expected< ObjSectionToIDMap > loadObjectImpl(const object::ObjectFile &Obj)
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
void registerEHFrames() override
Error finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
static std::unique_ptr< RuntimeDyldMachO > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Create a RuntimeDyldMachO instance for the given target architecture.
static section_iterator getSectionByAddress(const MachOObjectFile &Obj, uint64_t Addr)
int64_t memcpyAddend(const RelocationEntry &RE) const
This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &O) override
Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj, const SectionRef &PTSection, unsigned PTSectionID)
void makeValueAddendPCRel(RelocationValueRef &Value, const relocation_iterator &RI, unsigned OffsetToNextPC)
Make the RelocationValueRef addend PC-relative.
Expected< RelocationValueRef > getRelocationValueRef(const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID)
Construct a RelocationValueRef representing the relocation target.
Expected< relocation_iterator > processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI, const ObjectFile &BaseObjT, RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID, bool TargetIsLocalThumbFunc=false)
Process a scattered vanilla relocation.
bool isCompatibleFile(const object::ObjectFile &Obj) const override
void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const
Dump information about the relocation entry (RE) and resolved value.
SectionEntry - represents a section emitted into memory by the dynamic linker.
uint8_t * getAddress() const
uint64_t getLoadAddress() const
iterator find(StringRef Key)
StringRef - Represent a constant reference to a string, i.e.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
LLVM Value Representation.
Expected< SectionRef > getSection(unsigned SectionIndex) const
section_iterator section_end() const override
uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const
SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const
MachO::dysymtab_command getDysymtabLoadCommand() const
section_iterator section_begin() const override
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
symbol_iterator getSymbolByIndex(unsigned Index) const
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
bool is64Bit() const override
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
This class is the base class for all object file types.
section_iterator_range sections() const
This is a value type class that represents a single section in the list of sections in the object fil...
DataRefImpl getRawDataRefImpl() const
uint64_t getAddress() const
bool isText() const
Whether this section contains instructions.
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
static int64_t computeDelta(SectionEntry *A, SectionEntry *B)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
void consumeError(Error Err)
Consume a Error without doing anything.
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
LoadedObjectInfoHelper()=default