1 //===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #include "RuntimeDyldMachO.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringRef.h"
18 #include "Targets/RuntimeDyldMachOARM.h"
19 #include "Targets/RuntimeDyldMachOAArch64.h"
20 #include "Targets/RuntimeDyldMachOI386.h"
21 #include "Targets/RuntimeDyldMachOX86_64.h"
24 using namespace llvm::object;
26 #define DEBUG_TYPE "dyld"
31 RuntimeDyldMachO::getBasicRelocationEntry(unsigned SectionID,
33 const relocation_iterator &RI) const {
35 const MachOObjectFile &Obj =
36 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
37 MachO::any_relocation_info RelInfo =
38 Obj.getRelocation(RI->getRawDataRefImpl());
40 const SectionEntry &Section = Sections[SectionID];
41 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
42 unsigned Size = Obj.getAnyRelocationLength(RelInfo);
44 RI->getOffset(Offset);
45 uint8_t *LocalAddress = Section.Address + Offset;
46 unsigned NumBytes = 1 << Size;
48 memcpy(&Addend, LocalAddress, NumBytes);
49 uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
51 return RelocationEntry(SectionID, Offset, RelType, Addend, IsPCRel, Size);
54 RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
55 ObjectImage &ObjImg, const relocation_iterator &RI,
56 const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID,
57 const SymbolTableMap &Symbols) {
59 const MachOObjectFile &Obj =
60 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
61 MachO::any_relocation_info RelInfo =
62 Obj.getRelocation(RI->getRawDataRefImpl());
63 RelocationValueRef Value;
65 bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
67 symbol_iterator Symbol = RI->getSymbol();
69 Symbol->getName(TargetName);
70 SymbolTableMap::const_iterator SI = Symbols.find(TargetName.data());
71 if (SI != Symbols.end()) {
72 Value.SectionID = SI->second.first;
73 Value.Addend = SI->second.second + RE.Addend;
75 SI = GlobalSymbolTable.find(TargetName.data());
76 if (SI != GlobalSymbolTable.end()) {
77 Value.SectionID = SI->second.first;
78 Value.Addend = SI->second.second + RE.Addend;
80 Value.SymbolName = TargetName.data();
81 Value.Addend = RE.Addend;
85 SectionRef Sec = Obj.getRelocationSection(RelInfo);
88 Value.SectionID = findOrEmitSection(ObjImg, Sec, IsCode, ObjSectionToID);
91 Value.Addend = RE.Addend - Addr;
97 void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
99 const relocation_iterator &RI) {
100 const MachOObjectFile &Obj =
101 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
102 MachO::any_relocation_info RelInfo =
103 Obj.getRelocation(RI->getRawDataRefImpl());
105 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
107 uint64_t RelocAddr = 0;
108 RI->getAddress(RelocAddr);
109 unsigned RelocSize = Obj.getAnyRelocationLength(RelInfo);
110 Value.Addend += RelocAddr + (1 << RelocSize);
114 void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
115 uint64_t Value) const {
116 const SectionEntry &Section = Sections[RE.SectionID];
117 uint8_t *LocalAddress = Section.Address + RE.Offset;
118 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
120 dbgs() << "resolveRelocation Section: " << RE.SectionID
121 << " LocalAddress: " << format("%p", LocalAddress)
122 << " FinalAddress: " << format("%p", FinalAddress)
123 << " Value: " << format("%p", Value) << " Addend: " << RE.Addend
124 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
125 << " Size: " << (1 << RE.Size) << "\n";
128 bool RuntimeDyldMachO::writeBytesUnaligned(uint8_t *Addr, uint64_t Value,
130 for (unsigned i = 0; i < Size; ++i) {
131 *Addr++ = (uint8_t)Value;
139 RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
140 if (InputBuffer->getBufferSize() < 4)
142 StringRef Magic(InputBuffer->getBufferStart(), 4);
143 if (Magic == "\xFE\xED\xFA\xCE")
145 if (Magic == "\xCE\xFA\xED\xFE")
147 if (Magic == "\xFE\xED\xFA\xCF")
149 if (Magic == "\xCF\xFA\xED\xFE")
154 bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
155 return Obj->isMachO();
158 static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
159 intptr_t DeltaForEH) {
160 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
161 << ", Delta for EH: " << DeltaForEH << "\n");
162 uint32_t Length = *((uint32_t *)P);
164 unsigned char *Ret = P + Length;
165 uint32_t Offset = *((uint32_t *)P);
166 if (Offset == 0) // is a CIE
170 intptr_t FDELocation = *((intptr_t *)P);
171 intptr_t NewLocation = FDELocation - DeltaForText;
172 *((intptr_t *)P) = NewLocation;
173 P += sizeof(intptr_t);
175 // Skip the FDE address range
176 P += sizeof(intptr_t);
178 uint8_t Augmentationsize = *P;
180 if (Augmentationsize != 0) {
181 intptr_t LSDA = *((intptr_t *)P);
182 intptr_t NewLSDA = LSDA - DeltaForEH;
183 *((intptr_t *)P) = NewLSDA;
189 static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
190 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
191 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
192 return ObjDistance - MemDistance;
195 void RuntimeDyldMachO::registerEHFrames() {
199 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
200 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
201 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
202 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
204 SectionEntry *Text = &Sections[SectionInfo.TextSID];
205 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
206 SectionEntry *ExceptTab = nullptr;
207 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
208 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
210 intptr_t DeltaForText = computeDelta(Text, EHFrame);
211 intptr_t DeltaForEH = 0;
213 DeltaForEH = computeDelta(ExceptTab, EHFrame);
215 unsigned char *P = EHFrame->Address;
216 unsigned char *End = P + EHFrame->Size;
218 P = processFDE(P, DeltaForText, DeltaForEH);
221 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
224 UnregisteredEHFrameSections.clear();
227 std::unique_ptr<RuntimeDyldMachO>
228 llvm::RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
231 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
233 case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM);
234 case Triple::arm64: return make_unique<RuntimeDyldMachOAArch64>(MM);
235 case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM);
236 case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM);
240 } // end namespace llvm