23 using namespace llvm::object;
25 #define DEBUG_TYPE "dyld"
29 class LoadedMachOObjectInfo
34 : LoadedObjectInfoHelper(RTDyld, BeginIdx, EndIdx) {}
37 getObjectForDebug(
const ObjectFile &Obj)
const override {
47 unsigned NumBytes = 1 << RE.
Size;
50 return static_cast<int64_t
>(readBytesUnaligned(Src, NumBytes));
67 if (std::error_code EC = TargetNameOrErr.
getError())
71 GlobalSymbolTable.find(TargetName.
data());
72 if (SI != GlobalSymbolTable.end()) {
73 const auto &SymInfo = SI->
second;
82 bool IsCode = Sec.
isText();
83 Value.
SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
93 unsigned OffsetToNextPC) {
94 auto &O = *cast<MachOObjectFile>(RI->getObject());
96 Value.
Offset += RI->getOffset() + OffsetToNextPC + SecI->getAddress();
100 uint64_t
Value)
const {
106 <<
" LocalAddress: " <<
format(
"%p", LocalAddress)
107 <<
" FinalAddress: " <<
format(
"0x%016" PRIx64, FinalAddress)
108 <<
" Value: " <<
format(
"0x%016" PRIx64, Value) <<
" Addend: " << RE.
Addend
110 <<
" Size: " << (1 << RE.
Size) <<
"\n";
119 for (; SI != SE; ++SI) {
120 uint64_t SAddr = SI->getAddress();
121 uint64_t SSize = SI->getSize();
122 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
134 unsigned PTSectionID) {
136 "Pointer table section not supported in 64-bit MachO.");
140 uint32_t PTSectionSize = Sec32.
size;
141 unsigned FirstIndirectSymbol = Sec32.
reserved1;
142 const unsigned PTEntrySize = 4;
143 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
144 unsigned PTEntryOffset = 0;
146 assert((PTSectionSize % PTEntrySize) == 0 &&
147 "Pointers section does not contain a whole number of stubs?");
149 DEBUG(
dbgs() <<
"Populating pointer table section "
150 << Sections[PTSectionID].
Name
151 <<
", Section ID " << PTSectionID <<
", "
152 << NumPTEntries <<
" entries, " << PTEntrySize
153 <<
" bytes each:\n");
155 for (
unsigned i = 0; i < NumPTEntries; ++i) {
156 unsigned SymbolIndex =
160 if (std::error_code EC = IndirectSymbolNameOrErr.
getError())
162 StringRef IndirectSymbolName = *IndirectSymbolNameOrErr;
163 DEBUG(
dbgs() <<
" " << IndirectSymbolName <<
": index " << SymbolIndex
164 <<
", PT offset: " << PTEntryOffset <<
"\n");
167 addRelocationForSymbol(RE, IndirectSymbolName);
168 PTEntryOffset += PTEntrySize;
176 template <
typename Impl>
190 if (Name ==
"__text")
191 TextSID = findOrEmitSection(Obj,
Section,
true, SectionMap);
192 else if (Name ==
"__eh_frame")
193 EHFrameSID = findOrEmitSection(Obj,
Section,
false, SectionMap);
194 else if (Name ==
"__gcc_except_tab")
195 ExceptTabSID = findOrEmitSection(Obj,
Section,
true, SectionMap);
198 if (
I != SectionMap.end())
202 UnregisteredEHFrameSections.push_back(
206 template <
typename Impl>
208 int64_t DeltaForText,
209 int64_t DeltaForEH) {
210 typedef typename Impl::TargetPtrT TargetPtrT;
212 DEBUG(
dbgs() <<
"Processing FDE: Delta for text: " << DeltaForText
213 <<
", Delta for EH: " << DeltaForEH <<
"\n");
214 uint32_t Length = readBytesUnaligned(P, 4);
216 unsigned char *
Ret = P + Length;
217 uint32_t Offset = readBytesUnaligned(P, 4);
222 TargetPtrT FDELocation = readBytesUnaligned(P,
sizeof(TargetPtrT));
223 TargetPtrT NewLocation = FDELocation - DeltaForText;
224 writeBytesUnaligned(NewLocation, P,
sizeof(TargetPtrT));
226 P +=
sizeof(TargetPtrT);
229 P +=
sizeof(TargetPtrT);
231 uint8_t Augmentationsize = *
P;
233 if (Augmentationsize != 0) {
234 TargetPtrT LSDA = readBytesUnaligned(P,
sizeof(TargetPtrT));
235 TargetPtrT NewLSDA = LSDA - DeltaForEH;
236 writeBytesUnaligned(NewLSDA, P,
sizeof(TargetPtrT));
243 int64_t ObjDistance =
246 return ObjDistance - MemDistance;
249 template <
typename Impl>
252 for (
int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
264 int64_t DeltaForEH = 0;
268 unsigned char *P = EHFrame->
Address;
269 unsigned char *End = P + EHFrame->
Size;
271 P = processFDE(P, DeltaForText, DeltaForEH);
277 UnregisteredEHFrameSections.clear();
280 std::unique_ptr<RuntimeDyldMachO>
289 return make_unique<RuntimeDyldMachOARM>(MemMgr, Resolver);
291 return make_unique<RuntimeDyldMachOAArch64>(MemMgr, Resolver);
293 return make_unique<RuntimeDyldMachOI386>(MemMgr, Resolver);
295 return make_unique<RuntimeDyldMachOX86_64>(MemMgr, Resolver);
299 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
301 unsigned SectionStartIdx, SectionEndIdx;
302 std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
303 return llvm::make_unique<LoadedMachOObjectInfo>(*
this, SectionStartIdx,
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
std::error_code getError() const
size_t Size
Size - section size. Doesn't include the stubs.
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
Represents either an error or a value T.
This class is the base class for all object file types.
uint8_t * Address
Address - address in the linker's memory where the section resides.
DataRefImpl getRawDataRefImpl() const
void populateIndirectSymbolPointersSection(const MachOObjectFile &Obj, const SectionRef &PTSection, unsigned PTSectionID)
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
bool isCompatibleFile(const object::ObjectFile &Obj) const override
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
unsigned SectionID
SectionID - the section this relocation points to.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
int64_t memcpyAddend(const RelocationEntry &RE) const
This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...
place backedge safepoints impl
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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...
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
MachO::section getSection(DataRefImpl DRI) const
static int64_t computeDelta(SectionEntry *A, SectionEntry *B)
static std::unique_ptr< RuntimeDyldMachO > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, RuntimeDyld::SymbolResolver &Resolver)
Create a RuntimeDyldMachO instance for the given target architecture.
section_iterator_range sections() const
uint64_t getAddress() const
static section_iterator getSectionByAddress(const MachOObjectFile &Obj, uint64_t Addr)
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
uintptr_t ObjAddress
ObjAddress - address of the section in the in-memory object file.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const
ErrorOr< StringRef > getName() const
#define RTDYLD_INVALID_SECTION_ID
uint64_t Offset
Offset - offset into the section.
std::map< SectionRef, unsigned > ObjSectionToIDMap
RelocationValueRef getRelocationValueRef(const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID)
Construct a RelocationValueRef representing the relocation target.
uint64_t LoadAddress
LoadAddress - the address of the section in the target process's memory.
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
SectionEntry - represents a section emitted into memory by the dynamic linker.
LLVM Value Representation.
unsigned Size
The size of this relocation (MachO specific).
section_iterator section_end() const override
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.
void finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
This is a value type class that represents a single section in the list of sections in the object fil...