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 uint8_t *LocalAddress = Section.Address + RE.Offset;
33 unsigned NumBytes = 1 << RE.Size;
35 memcpy(&Addend, LocalAddress, NumBytes);
39 RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
40 ObjectImage &ObjImg, const relocation_iterator &RI,
41 const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID,
42 const SymbolTableMap &Symbols) {
44 const MachOObjectFile &Obj =
45 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
46 MachO::any_relocation_info RelInfo =
47 Obj.getRelocation(RI->getRawDataRefImpl());
48 RelocationValueRef Value;
50 bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
52 symbol_iterator Symbol = RI->getSymbol();
54 Symbol->getName(TargetName);
55 SymbolTableMap::const_iterator SI = Symbols.find(TargetName.data());
56 if (SI != Symbols.end()) {
57 Value.SectionID = SI->second.first;
58 Value.Addend = SI->second.second + RE.Addend;
60 SI = GlobalSymbolTable.find(TargetName.data());
61 if (SI != GlobalSymbolTable.end()) {
62 Value.SectionID = SI->second.first;
63 Value.Addend = SI->second.second + RE.Addend;
65 Value.SymbolName = TargetName.data();
66 Value.Addend = RE.Addend;
70 SectionRef Sec = Obj.getRelocationSection(RelInfo);
73 Value.SectionID = findOrEmitSection(ObjImg, Sec, IsCode, ObjSectionToID);
76 Value.Addend = RE.Addend - Addr;
82 void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
84 const relocation_iterator &RI,
85 unsigned OffsetToNextPC) {
86 const MachOObjectFile &Obj =
87 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
88 MachO::any_relocation_info RelInfo =
89 Obj.getRelocation(RI->getRawDataRefImpl());
91 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
93 uint64_t RelocAddr = 0;
94 RI->getAddress(RelocAddr);
95 Value.Addend += RelocAddr + OffsetToNextPC;
99 void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
100 uint64_t Value) const {
101 const SectionEntry &Section = Sections[RE.SectionID];
102 uint8_t *LocalAddress = Section.Address + RE.Offset;
103 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
105 dbgs() << "resolveRelocation Section: " << RE.SectionID
106 << " LocalAddress: " << format("%p", LocalAddress)
107 << " FinalAddress: " << format("0x%x", FinalAddress)
108 << " Value: " << format("0x%x", Value) << " Addend: " << RE.Addend
109 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
110 << " Size: " << (1 << RE.Size) << "\n";
113 bool RuntimeDyldMachO::writeBytesUnaligned(uint8_t *Dst, uint64_t Value,
117 // If host and target endianness match use memcpy, otherwise copy in reverse
119 if (IsTargetLittleEndian == sys::IsLittleEndianHost)
120 memcpy(Dst, &Value, Size);
122 uint8_t *Src = reinterpret_cast<uint8_t*>(&Value) + Size - 1;
123 for (unsigned i = 0; i < Size; ++i)
131 RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
132 if (InputBuffer->getBufferSize() < 4)
134 StringRef Magic(InputBuffer->getBufferStart(), 4);
135 if (Magic == "\xFE\xED\xFA\xCE")
137 if (Magic == "\xCE\xFA\xED\xFE")
139 if (Magic == "\xFE\xED\xFA\xCF")
141 if (Magic == "\xCF\xFA\xED\xFE")
146 bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
147 return Obj->isMachO();
150 static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
151 intptr_t DeltaForEH) {
152 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
153 << ", Delta for EH: " << DeltaForEH << "\n");
154 uint32_t Length = *((uint32_t *)P);
156 unsigned char *Ret = P + Length;
157 uint32_t Offset = *((uint32_t *)P);
158 if (Offset == 0) // is a CIE
162 intptr_t FDELocation = *((intptr_t *)P);
163 intptr_t NewLocation = FDELocation - DeltaForText;
164 *((intptr_t *)P) = NewLocation;
165 P += sizeof(intptr_t);
167 // Skip the FDE address range
168 P += sizeof(intptr_t);
170 uint8_t Augmentationsize = *P;
172 if (Augmentationsize != 0) {
173 intptr_t LSDA = *((intptr_t *)P);
174 intptr_t NewLSDA = LSDA - DeltaForEH;
175 *((intptr_t *)P) = NewLSDA;
181 static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
182 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
183 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
184 return ObjDistance - MemDistance;
187 void RuntimeDyldMachO::registerEHFrames() {
191 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
192 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
193 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
194 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
196 SectionEntry *Text = &Sections[SectionInfo.TextSID];
197 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
198 SectionEntry *ExceptTab = nullptr;
199 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
200 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
202 intptr_t DeltaForText = computeDelta(Text, EHFrame);
203 intptr_t DeltaForEH = 0;
205 DeltaForEH = computeDelta(ExceptTab, EHFrame);
207 unsigned char *P = EHFrame->Address;
208 unsigned char *End = P + EHFrame->Size;
210 P = processFDE(P, DeltaForText, DeltaForEH);
213 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
216 UnregisteredEHFrameSections.clear();
219 std::unique_ptr<RuntimeDyldMachO>
220 llvm::RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
223 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
225 case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM);
226 case Triple::aarch64: return make_unique<RuntimeDyldMachOAArch64>(MM);
227 case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM);
228 case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM);
232 } // end namespace llvm