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"
30 int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const {
31 const SectionEntry &Section = Sections[RE.SectionID];
32 unsigned NumBytes = 1 << RE.Size;
34 uint8_t *LocalAddress = Section.Address + RE.Offset;
35 uint8_t *Dst = reinterpret_cast<uint8_t*>(&Addend);
37 if (IsTargetLittleEndian == sys::IsLittleEndianHost) {
38 if (!sys::IsLittleEndianHost)
39 Dst += sizeof(Addend) - NumBytes;
40 memcpy(Dst, LocalAddress, NumBytes);
43 for (unsigned i = 0; i < NumBytes; ++i)
44 *Dst-- = *LocalAddress++;
50 RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
51 ObjectImage &ObjImg, const relocation_iterator &RI,
52 const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID,
53 const SymbolTableMap &Symbols) {
55 const MachOObjectFile &Obj =
56 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
57 MachO::any_relocation_info RelInfo =
58 Obj.getRelocation(RI->getRawDataRefImpl());
59 RelocationValueRef Value;
61 bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
63 symbol_iterator Symbol = RI->getSymbol();
65 Symbol->getName(TargetName);
66 SymbolTableMap::const_iterator SI = Symbols.find(TargetName.data());
67 if (SI != Symbols.end()) {
68 Value.SectionID = SI->second.first;
69 Value.Addend = SI->second.second + RE.Addend;
71 SI = GlobalSymbolTable.find(TargetName.data());
72 if (SI != GlobalSymbolTable.end()) {
73 Value.SectionID = SI->second.first;
74 Value.Addend = SI->second.second + RE.Addend;
76 Value.SymbolName = TargetName.data();
77 Value.Addend = RE.Addend;
81 SectionRef Sec = Obj.getRelocationSection(RelInfo);
84 Value.SectionID = findOrEmitSection(ObjImg, Sec, IsCode, ObjSectionToID);
87 Value.Addend = RE.Addend - Addr;
93 void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
95 const relocation_iterator &RI,
96 unsigned OffsetToNextPC) {
97 const MachOObjectFile &Obj =
98 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
99 MachO::any_relocation_info RelInfo =
100 Obj.getRelocation(RI->getRawDataRefImpl());
102 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
104 uint64_t RelocAddr = 0;
105 RI->getAddress(RelocAddr);
106 Value.Addend += RelocAddr + OffsetToNextPC;
110 void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
111 uint64_t Value) const {
112 const SectionEntry &Section = Sections[RE.SectionID];
113 uint8_t *LocalAddress = Section.Address + RE.Offset;
114 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
116 dbgs() << "resolveRelocation Section: " << RE.SectionID
117 << " LocalAddress: " << format("%p", LocalAddress)
118 << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
119 << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
120 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
121 << " Size: " << (1 << RE.Size) << "\n";
124 bool RuntimeDyldMachO::writeBytesUnaligned(uint8_t *Dst, uint64_t Value,
127 uint8_t *Src = reinterpret_cast<uint8_t*>(&Value);
128 // If host and target endianness match use memcpy, otherwise copy in reverse
130 if (IsTargetLittleEndian == sys::IsLittleEndianHost) {
131 if (!sys::IsLittleEndianHost)
132 Src += sizeof(Value) - Size;
133 memcpy(Dst, Src, Size);
136 for (unsigned i = 0; i < Size; ++i)
144 RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
145 if (InputBuffer->getBufferSize() < 4)
147 StringRef Magic(InputBuffer->getBufferStart(), 4);
148 if (Magic == "\xFE\xED\xFA\xCE")
150 if (Magic == "\xCE\xFA\xED\xFE")
152 if (Magic == "\xFE\xED\xFA\xCF")
154 if (Magic == "\xCF\xFA\xED\xFE")
159 bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
160 return Obj->isMachO();
163 static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
164 intptr_t DeltaForEH) {
165 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
166 << ", Delta for EH: " << DeltaForEH << "\n");
167 uint32_t Length = *((uint32_t *)P);
169 unsigned char *Ret = P + Length;
170 uint32_t Offset = *((uint32_t *)P);
171 if (Offset == 0) // is a CIE
175 intptr_t FDELocation = *((intptr_t *)P);
176 intptr_t NewLocation = FDELocation - DeltaForText;
177 *((intptr_t *)P) = NewLocation;
178 P += sizeof(intptr_t);
180 // Skip the FDE address range
181 P += sizeof(intptr_t);
183 uint8_t Augmentationsize = *P;
185 if (Augmentationsize != 0) {
186 intptr_t LSDA = *((intptr_t *)P);
187 intptr_t NewLSDA = LSDA - DeltaForEH;
188 *((intptr_t *)P) = NewLSDA;
194 static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
195 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
196 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
197 return ObjDistance - MemDistance;
200 void RuntimeDyldMachO::registerEHFrames() {
204 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
205 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
206 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
207 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
209 SectionEntry *Text = &Sections[SectionInfo.TextSID];
210 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
211 SectionEntry *ExceptTab = nullptr;
212 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
213 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
215 intptr_t DeltaForText = computeDelta(Text, EHFrame);
216 intptr_t DeltaForEH = 0;
218 DeltaForEH = computeDelta(ExceptTab, EHFrame);
220 unsigned char *P = EHFrame->Address;
221 unsigned char *End = P + EHFrame->Size;
223 P = processFDE(P, DeltaForText, DeltaForEH);
226 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
229 UnregisteredEHFrameSections.clear();
232 std::unique_ptr<RuntimeDyldMachO>
233 llvm::RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
236 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
238 case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM);
239 case Triple::aarch64: return make_unique<RuntimeDyldMachOAArch64>(MM);
240 case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM);
241 case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM);
245 } // end namespace llvm