23 using namespace llvm::object;
25 #define DEBUG_TYPE "dyld"
29 class LoadedMachOObjectInfo final
33 ObjSectionToIDMap ObjSecToIDMap)
34 : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
37 getObjectForDebug(
const ObjectFile &Obj)
const override {
47 unsigned NumBytes = 1 << RE.
Size;
50 return static_cast<int64_t
>(readBytesUnaligned(Src, NumBytes));
67 uint64_t
Offset = RelI->getOffset();
69 unsigned NumBytes = 1 << Size;
70 int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
75 uint64_t SectionBaseAddr = TargetSI->getAddress();
77 bool IsCode = TargetSection.
isText();
79 if (
auto TargetSectionIDOrErr =
80 findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID))
81 TargetSectionID = *TargetSectionIDOrErr;
83 return TargetSectionIDOrErr.takeError();
85 Addend -= SectionBaseAddr;
88 addRelocationForSection(R, TargetSectionID);
109 if (
auto TargetNameOrErr = Symbol->
getName())
110 TargetName = *TargetNameOrErr;
112 return TargetNameOrErr.takeError();
114 GlobalSymbolTable.find(TargetName.
data());
115 if (SI != GlobalSymbolTable.end()) {
116 const auto &SymInfo = SI->
second;
117 Value.
SectionID = SymInfo.getSectionID();
125 bool IsCode = Sec.
isText();
126 if (
auto SectionIDOrErr = findOrEmitSection(Obj, Sec, IsCode,
130 return SectionIDOrErr.takeError();
140 unsigned OffsetToNextPC) {
141 auto &O = *cast<MachOObjectFile>(RI->getObject());
143 Value.
Offset += RI->getOffset() + OffsetToNextPC + SecI->getAddress();
147 uint64_t
Value)
const {
153 <<
" LocalAddress: " <<
format(
"%p", LocalAddress)
154 <<
" FinalAddress: " <<
format(
"0x%016" PRIx64, FinalAddress)
155 <<
" Value: " <<
format(
"0x%016" PRIx64, Value) <<
" Addend: " << RE.
Addend
157 <<
" Size: " << (1 << RE.
Size) <<
"\n";
166 for (; SI != SE; ++SI) {
167 uint64_t SAddr = SI->getAddress();
168 uint64_t SSize = SI->getSize();
169 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
181 unsigned PTSectionID) {
183 "Pointer table section not supported in 64-bit MachO.");
188 unsigned FirstIndirectSymbol = Sec32.
reserved1;
189 const unsigned PTEntrySize = 4;
190 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
191 unsigned PTEntryOffset = 0;
193 assert((PTSectionSize % PTEntrySize) == 0 &&
194 "Pointers section does not contain a whole number of stubs?");
196 DEBUG(
dbgs() <<
"Populating pointer table section "
197 << Sections[PTSectionID].
getName() <<
", Section ID "
198 << PTSectionID <<
", " << NumPTEntries <<
" entries, "
199 << PTEntrySize <<
" bytes each:\n");
201 for (
unsigned i = 0;
i < NumPTEntries; ++
i) {
202 unsigned SymbolIndex =
206 if (
auto IndirectSymbolNameOrErr = SI->
getName())
207 IndirectSymbolName = *IndirectSymbolNameOrErr;
209 return IndirectSymbolNameOrErr.takeError();
210 DEBUG(
dbgs() <<
" " << IndirectSymbolName <<
": index " << SymbolIndex
211 <<
", PT offset: " << PTEntryOffset <<
"\n");
214 addRelocationForSymbol(RE, IndirectSymbolName);
215 PTEntryOffset += PTEntrySize;
224 template <
typename Impl>
239 if (Name ==
"__text") {
240 if (
auto TextSIDOrErr = findOrEmitSection(Obj,
Section,
true, SectionMap))
241 TextSID = *TextSIDOrErr;
243 return TextSIDOrErr.takeError();
244 }
else if (Name ==
"__eh_frame") {
245 if (
auto EHFrameSIDOrErr = findOrEmitSection(Obj,
Section,
false,
247 EHFrameSID = *EHFrameSIDOrErr;
249 return EHFrameSIDOrErr.takeError();
250 }
else if (Name ==
"__gcc_except_tab") {
251 if (
auto ExceptTabSIDOrErr = findOrEmitSection(Obj,
Section,
true,
253 ExceptTabSID = *ExceptTabSIDOrErr;
255 return ExceptTabSIDOrErr.takeError();
258 if (
I != SectionMap.end())
259 if (
auto Err =
impl().finalizeSection(Obj,
I->second,
Section))
263 UnregisteredEHFrameSections.push_back(
269 template <
typename Impl>
271 int64_t DeltaForText,
272 int64_t DeltaForEH) {
273 typedef typename Impl::TargetPtrT TargetPtrT;
275 DEBUG(
dbgs() <<
"Processing FDE: Delta for text: " << DeltaForText
276 <<
", Delta for EH: " << DeltaForEH <<
"\n");
277 uint32_t Length = readBytesUnaligned(P, 4);
279 uint8_t *
Ret = P + Length;
285 TargetPtrT FDELocation = readBytesUnaligned(P,
sizeof(TargetPtrT));
286 TargetPtrT NewLocation = FDELocation - DeltaForText;
287 writeBytesUnaligned(NewLocation, P,
sizeof(TargetPtrT));
289 P +=
sizeof(TargetPtrT);
292 P +=
sizeof(TargetPtrT);
294 uint8_t Augmentationsize = *
P;
296 if (Augmentationsize != 0) {
297 TargetPtrT LSDA = readBytesUnaligned(P,
sizeof(TargetPtrT));
298 TargetPtrT NewLSDA = LSDA - DeltaForEH;
299 writeBytesUnaligned(NewLSDA, P,
sizeof(TargetPtrT));
306 int64_t ObjDistance =
static_cast<int64_t
>(A->
getObjAddress()) -
309 return ObjDistance - MemDistance;
312 template <
typename Impl>
315 for (
int i = 0, e = UnregisteredEHFrameSections.size();
i != e; ++
i) {
327 int64_t DeltaForEH = 0;
334 P = processFDE(P, DeltaForText, DeltaForEH);
340 UnregisteredEHFrameSections.clear();
343 std::unique_ptr<RuntimeDyldMachO>
352 return make_unique<RuntimeDyldMachOARM>(MemMgr, Resolver);
354 return make_unique<RuntimeDyldMachOAArch64>(MemMgr, Resolver);
356 return make_unique<RuntimeDyldMachOI386>(MemMgr, Resolver);
358 return make_unique<RuntimeDyldMachOX86_64>(MemMgr, Resolver);
362 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
364 if (
auto ObjSectionToIDOrErr = loadObjectImpl(O))
365 return llvm::make_unique<LoadedMachOObjectInfo>(*
this,
366 *ObjSectionToIDOrErr);
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
Error finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
Expected< StringRef > getName() const
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner)
Log all errors (if any) in E to OS.
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
uintptr_t getObjAddress() const
uint8_t * getAddress() const
This class is the base class for all object file types.
DataRefImpl getRawDataRefImpl() const
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
bool isCompatibleFile(const object::ObjectFile &Obj) const override
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
unsigned SectionID
SectionID - the section this relocation points to.
static StringRef getName(Value *V)
int64_t memcpyAddend(const RelocationEntry &RE) const
This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...
Tagged union holding either a T or a Error.
uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const
static std::unique_ptr< RuntimeDyldMachO > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Create a RuntimeDyldMachO instance for the given target architecture.
place backedge safepoints impl
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
void registerEHFrames() override
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const
Dump information about the relocation entry (RE) and resolved value.
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
Expected< RelocationValueRef > getRelocationValueRef(const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID)
Construct a RelocationValueRef representing the relocation target.
uint8_t * getAddressWithOffset(unsigned OffsetBytes) const
Return the address of this section with an offset.
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
MachO::section getSection(DataRefImpl DRI) const
static int64_t computeDelta(SectionEntry *A, SectionEntry *B)
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
static const unsigned End
Expected< relocation_iterator > processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI, const ObjectFile &BaseObjT, RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID)
Process a scattered vanilla relocation.
section_iterator_range sections() const
uint64_t getAddress() const
static section_iterator getSectionByAddress(const MachOObjectFile &Obj, uint64_t Addr)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static ErrorSuccess success()
Create a success value.
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const
#define RTDYLD_INVALID_SECTION_ID
uint64_t Offset
Offset - offset into the section.
std::map< SectionRef, unsigned > ObjSectionToIDMap
basic_symbol_iterator getSymbolByIndex(unsigned Index) const
section_iterator section_begin() const override
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &O) override
MachO::dysymtab_command getDysymtabLoadCommand() const
uint64_t getLoadAddress() const
SectionEntry - represents a section emitted into memory by the dynamic linker.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
LLVM Value Representation.
Lightweight error class with error context and mandatory checking.
unsigned Size
The size of this relocation (MachO specific).
section_iterator section_end() const override
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj, const SectionRef &PTSection, unsigned PTSectionID)
StringRef - Represent a constant reference to a string, i.e.
void makeValueAddendPCRel(RelocationValueRef &Value, const relocation_iterator &RI, unsigned OffsetToNextPC)
Make the RelocationValueRef addend PC-relative.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
This is a value type class that represents a single section in the list of sections in the object fil...