24 #define DEBUG_TYPE "dyld" 28 class LoadedMachOObjectInfo final
30 RuntimeDyld::LoadedObjectInfo> {
33 ObjSectionToIDMap ObjSecToIDMap)
37 getObjectForDebug(
const ObjectFile &Obj)
const override {
47 unsigned NumBytes = 1 << RE.
Size;
50 return static_cast<int64_t
>(readBytesUnaligned(Src, NumBytes));
58 bool TargetIsLocalThumbFunc) {
68 uint64_t
Offset = RelI->getOffset();
70 unsigned NumBytes = 1 <<
Size;
71 int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
76 uint64_t SectionBaseAddr = TargetSI->getAddress();
78 bool IsCode = TargetSection.
isText();
80 if (
auto TargetSectionIDOrErr =
81 findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID))
82 TargetSectionID = *TargetSectionIDOrErr;
84 return TargetSectionIDOrErr.takeError();
86 Addend -= SectionBaseAddr;
90 addRelocationForSection(R, TargetSectionID);
111 if (
auto TargetNameOrErr = Symbol->
getName())
112 TargetName = *TargetNameOrErr;
114 return TargetNameOrErr.takeError();
116 GlobalSymbolTable.find(TargetName.
data());
117 if (SI != GlobalSymbolTable.end()) {
118 const auto &
SymInfo = SI->second;
127 bool IsCode = Sec.
isText();
128 if (
auto SectionIDOrErr = findOrEmitSection(Obj, Sec, IsCode,
132 return SectionIDOrErr.takeError();
142 unsigned OffsetToNextPC) {
143 auto &
O = *cast<MachOObjectFile>(RI->getObject());
145 Value.
Offset += RI->getOffset() + OffsetToNextPC + SecI->getAddress();
149 uint64_t
Value)
const {
155 <<
" LocalAddress: " <<
format(
"%p", LocalAddress)
156 <<
" FinalAddress: " <<
format(
"0x%016" PRIx64, FinalAddress)
157 <<
" Value: " <<
format(
"0x%016" PRIx64, Value) <<
" Addend: " << RE.
Addend 159 <<
" Size: " << (1 << RE.
Size) <<
"\n";
168 for (; SI != SE; ++
SI) {
169 uint64_t SAddr = SI->getAddress();
170 uint64_t SSize = SI->getSize();
171 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
183 unsigned PTSectionID) {
185 "Pointer table section not supported in 64-bit MachO.");
190 unsigned FirstIndirectSymbol = Sec32.
reserved1;
191 const unsigned PTEntrySize = 4;
192 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
193 unsigned PTEntryOffset = 0;
195 assert((PTSectionSize % PTEntrySize) == 0 &&
196 "Pointers section does not contain a whole number of stubs?");
199 << Sections[PTSectionID].
getName() <<
", Section ID " 200 << PTSectionID <<
", " << NumPTEntries <<
" entries, " 201 << PTEntrySize <<
" bytes each:\n");
203 for (
unsigned i = 0; i < NumPTEntries; ++i) {
204 unsigned SymbolIndex =
208 if (
auto IndirectSymbolNameOrErr = SI->
getName())
209 IndirectSymbolName = *IndirectSymbolNameOrErr;
211 return IndirectSymbolNameOrErr.takeError();
212 LLVM_DEBUG(
dbgs() <<
" " << IndirectSymbolName <<
": index " << SymbolIndex
213 <<
", PT offset: " << PTEntryOffset <<
"\n");
216 addRelocationForSymbol(RE, IndirectSymbolName);
217 PTEntryOffset += PTEntrySize;
226 template <
typename Impl>
244 if (Name ==
"__text") {
245 if (
auto TextSIDOrErr = findOrEmitSection(Obj,
Section,
true, SectionMap))
246 TextSID = *TextSIDOrErr;
248 return TextSIDOrErr.takeError();
249 }
else if (Name ==
"__eh_frame") {
250 if (
auto EHFrameSIDOrErr = findOrEmitSection(Obj,
Section,
false,
252 EHFrameSID = *EHFrameSIDOrErr;
254 return EHFrameSIDOrErr.takeError();
255 }
else if (Name ==
"__gcc_except_tab") {
256 if (
auto ExceptTabSIDOrErr = findOrEmitSection(Obj,
Section,
true,
258 ExceptTabSID = *ExceptTabSIDOrErr;
260 return ExceptTabSIDOrErr.takeError();
263 if (
I != SectionMap.end())
264 if (
auto Err =
impl().finalizeSection(Obj,
I->second,
Section))
268 UnregisteredEHFrameSections.push_back(
274 template <
typename Impl>
276 int64_t DeltaForText,
277 int64_t DeltaForEH) {
278 typedef typename Impl::TargetPtrT TargetPtrT;
280 LLVM_DEBUG(
dbgs() <<
"Processing FDE: Delta for text: " << DeltaForText
281 <<
", Delta for EH: " << DeltaForEH <<
"\n");
282 uint32_t Length = readBytesUnaligned(P, 4);
284 uint8_t *
Ret = P + Length;
290 TargetPtrT FDELocation = readBytesUnaligned(P,
sizeof(TargetPtrT));
291 TargetPtrT NewLocation = FDELocation - DeltaForText;
292 writeBytesUnaligned(NewLocation, P,
sizeof(TargetPtrT));
294 P +=
sizeof(TargetPtrT);
297 P +=
sizeof(TargetPtrT);
299 uint8_t Augmentationsize = *
P;
301 if (Augmentationsize != 0) {
302 TargetPtrT LSDA = readBytesUnaligned(P,
sizeof(TargetPtrT));
303 TargetPtrT NewLSDA = LSDA - DeltaForEH;
304 writeBytesUnaligned(NewLSDA, P,
sizeof(TargetPtrT));
311 int64_t ObjDistance =
static_cast<int64_t
>(A->
getObjAddress()) -
314 return ObjDistance - MemDistance;
317 template <
typename Impl>
320 for (
int i = 0,
e = UnregisteredEHFrameSections.size(); i !=
e; ++i) {
332 int64_t DeltaForEH = 0;
337 uint8_t *End = P + EHFrame->
getSize();
339 P = processFDE(P, DeltaForText, DeltaForEH);
345 UnregisteredEHFrameSections.clear();
348 std::unique_ptr<RuntimeDyldMachO>
357 return std::make_unique<RuntimeDyldMachOARM>(MemMgr, Resolver);
359 return std::make_unique<RuntimeDyldMachOAArch64>(MemMgr, Resolver);
361 return std::make_unique<RuntimeDyldMachOAArch64>(MemMgr, Resolver);
363 return std::make_unique<RuntimeDyldMachOI386>(MemMgr, Resolver);
365 return std::make_unique<RuntimeDyldMachOX86_64>(MemMgr, Resolver);
369 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
371 if (
auto ObjSectionToIDOrErr = loadObjectImpl(O))
372 return std::make_unique<LoadedMachOObjectInfo>(*
this,
373 *ObjSectionToIDOrErr);
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
Error finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
This class represents lattice values for constants.
Expected< StringRef > getName() const
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
Expected< SectionRef > getSection(unsigned SectionIndex) const
void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const
Dump information about the relocation entry (RE) and resolved value.
This class is the base class for all object file types.
uint8_t * getAddress() 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.
uint64_t getAddress() const
static StringRef getName(Value *V)
bool isText() const
Whether this section contains instructions.
Tagged union holding either a T or a Error.
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
int64_t memcpyAddend(const RelocationEntry &RE) const
This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...
SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const
symbol_iterator getSymbolByIndex(unsigned Index) const
section_iterator_range sections() const
void registerEHFrames() override
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
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.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static int64_t computeDelta(SectionEntry *A, SectionEntry *B)
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Symbol resolution interface.
uintptr_t getObjAddress() const
DataRefImpl getRawDataRefImpl() const
void consumeError(Error Err)
Consume a Error without doing anything.
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
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.
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
Expected< relocation_iterator > processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI, const ObjectFile &BaseObjT, RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID, bool TargetIsLocalThumbFunc=false)
Process a scattered vanilla relocation.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
#define RTDYLD_INVALID_SECTION_ID
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) 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.
uint64_t getLoadAddress() const
section_iterator section_begin() const override
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &O) override
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.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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
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.
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const
This is a value type class that represents a single section in the list of sections in the object fil...
MachO::dysymtab_command getDysymtabLoadCommand() const