Jim Grosbach | 06594e1 | 2012-01-16 23:50:58 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=// |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implementation of the MC-JIT runtime dynamic linker. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 14 | #include "RuntimeDyldMachO.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 17 | |
| 18 | #include "Targets/RuntimeDyldMachOARM.h" |
| 19 | #include "Targets/RuntimeDyldMachOAArch64.h" |
| 20 | #include "Targets/RuntimeDyldMachOI386.h" |
| 21 | #include "Targets/RuntimeDyldMachOX86_64.h" |
| 22 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | using namespace llvm::object; |
| 25 | |
Chandler Carruth | f58e376 | 2014-04-22 03:04:17 +0000 | [diff] [blame] | 26 | #define DEBUG_TYPE "dyld" |
| 27 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 28 | namespace llvm { |
| 29 | |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 30 | int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const { |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 31 | unsigned NumBytes = 1 << RE.Size; |
Lang Hames | e1287c0 | 2014-08-29 23:17:47 +0000 | [diff] [blame] | 32 | uint8_t *Src = Sections[RE.SectionID].Address + RE.Offset; |
Lang Hames | dc77feb | 2014-08-27 17:41:06 +0000 | [diff] [blame] | 33 | |
Lang Hames | e1287c0 | 2014-08-29 23:17:47 +0000 | [diff] [blame] | 34 | return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes)); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | RelocationValueRef RuntimeDyldMachO::getRelocationValueRef( |
| 38 | ObjectImage &ObjImg, const relocation_iterator &RI, |
| 39 | const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID, |
| 40 | const SymbolTableMap &Symbols) { |
| 41 | |
| 42 | const MachOObjectFile &Obj = |
| 43 | static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile()); |
| 44 | MachO::any_relocation_info RelInfo = |
| 45 | Obj.getRelocation(RI->getRawDataRefImpl()); |
| 46 | RelocationValueRef Value; |
| 47 | |
| 48 | bool IsExternal = Obj.getPlainRelocationExternal(RelInfo); |
| 49 | if (IsExternal) { |
| 50 | symbol_iterator Symbol = RI->getSymbol(); |
| 51 | StringRef TargetName; |
| 52 | Symbol->getName(TargetName); |
| 53 | SymbolTableMap::const_iterator SI = Symbols.find(TargetName.data()); |
| 54 | if (SI != Symbols.end()) { |
| 55 | Value.SectionID = SI->second.first; |
| 56 | Value.Addend = SI->second.second + RE.Addend; |
| 57 | } else { |
| 58 | SI = GlobalSymbolTable.find(TargetName.data()); |
| 59 | if (SI != GlobalSymbolTable.end()) { |
| 60 | Value.SectionID = SI->second.first; |
| 61 | Value.Addend = SI->second.second + RE.Addend; |
| 62 | } else { |
| 63 | Value.SymbolName = TargetName.data(); |
| 64 | Value.Addend = RE.Addend; |
| 65 | } |
| 66 | } |
| 67 | } else { |
| 68 | SectionRef Sec = Obj.getRelocationSection(RelInfo); |
| 69 | bool IsCode = false; |
| 70 | Sec.isText(IsCode); |
| 71 | Value.SectionID = findOrEmitSection(ObjImg, Sec, IsCode, ObjSectionToID); |
| 72 | uint64_t Addr; |
| 73 | Sec.getAddress(Addr); |
| 74 | Value.Addend = RE.Addend - Addr; |
| 75 | } |
| 76 | |
| 77 | return Value; |
| 78 | } |
| 79 | |
| 80 | void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value, |
| 81 | ObjectImage &ObjImg, |
Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 82 | const relocation_iterator &RI, |
| 83 | unsigned OffsetToNextPC) { |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 84 | const MachOObjectFile &Obj = |
| 85 | static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile()); |
| 86 | MachO::any_relocation_info RelInfo = |
| 87 | Obj.getRelocation(RI->getRawDataRefImpl()); |
| 88 | |
| 89 | bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo); |
| 90 | if (IsPCRel) { |
| 91 | uint64_t RelocAddr = 0; |
| 92 | RI->getAddress(RelocAddr); |
Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 93 | Value.Addend += RelocAddr + OffsetToNextPC; |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE, |
| 98 | uint64_t Value) const { |
| 99 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 100 | uint8_t *LocalAddress = Section.Address + RE.Offset; |
| 101 | uint64_t FinalAddress = Section.LoadAddress + RE.Offset; |
| 102 | |
| 103 | dbgs() << "resolveRelocation Section: " << RE.SectionID |
| 104 | << " LocalAddress: " << format("%p", LocalAddress) |
Lang Hames | c5cafbb | 2014-08-28 04:25:17 +0000 | [diff] [blame] | 105 | << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress) |
| 106 | << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 107 | << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType |
| 108 | << " Size: " << (1 << RE.Size) << "\n"; |
| 109 | } |
| 110 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 111 | bool |
| 112 | RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const { |
| 113 | if (InputBuffer->getBufferSize() < 4) |
| 114 | return false; |
| 115 | StringRef Magic(InputBuffer->getBufferStart(), 4); |
| 116 | if (Magic == "\xFE\xED\xFA\xCE") |
| 117 | return true; |
| 118 | if (Magic == "\xCE\xFA\xED\xFE") |
| 119 | return true; |
| 120 | if (Magic == "\xFE\xED\xFA\xCF") |
| 121 | return true; |
| 122 | if (Magic == "\xCF\xFA\xED\xFE") |
| 123 | return true; |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const { |
| 128 | return Obj->isMachO(); |
| 129 | } |
| 130 | |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame^] | 131 | template <typename Impl> |
| 132 | void RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(ObjectImage &ObjImg, |
| 133 | ObjSectionToIDMap &SectionMap) { |
| 134 | unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID; |
| 135 | unsigned TextSID = RTDYLD_INVALID_SECTION_ID; |
| 136 | unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID; |
| 137 | ObjSectionToIDMap::iterator i, e; |
| 138 | |
| 139 | for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) { |
| 140 | const SectionRef &Section = i->first; |
| 141 | StringRef Name; |
| 142 | Section.getName(Name); |
| 143 | if (Name == "__eh_frame") |
| 144 | EHFrameSID = i->second; |
| 145 | else if (Name == "__text") |
| 146 | TextSID = i->second; |
| 147 | else if (Name == "__gcc_except_tab") |
| 148 | ExceptTabSID = i->second; |
| 149 | else |
| 150 | impl().finalizeSection(ObjImg, i->second, Section); |
| 151 | } |
| 152 | UnregisteredEHFrameSections.push_back( |
| 153 | EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID)); |
| 154 | } |
| 155 | |
| 156 | template <typename Impl> |
| 157 | unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(unsigned char *P, |
| 158 | int64_t DeltaForText, |
| 159 | int64_t DeltaForEH) { |
| 160 | typedef typename Impl::TargetPtrT TargetPtrT; |
| 161 | |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 162 | DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText |
| 163 | << ", Delta for EH: " << DeltaForEH << "\n"); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 164 | uint32_t Length = *((uint32_t *)P); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 165 | P += 4; |
| 166 | unsigned char *Ret = P + Length; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 167 | uint32_t Offset = *((uint32_t *)P); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 168 | if (Offset == 0) // is a CIE |
| 169 | return Ret; |
| 170 | |
| 171 | P += 4; |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame^] | 172 | TargetPtrT FDELocation = *((TargetPtrT*)P); |
| 173 | TargetPtrT NewLocation = FDELocation - DeltaForText; |
| 174 | *((TargetPtrT*)P) = NewLocation; |
| 175 | P += sizeof(TargetPtrT); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 176 | |
| 177 | // Skip the FDE address range |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame^] | 178 | P += sizeof(TargetPtrT); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 179 | |
| 180 | uint8_t Augmentationsize = *P; |
| 181 | P += 1; |
| 182 | if (Augmentationsize != 0) { |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame^] | 183 | TargetPtrT LSDA = *((TargetPtrT *)P); |
| 184 | TargetPtrT NewLSDA = LSDA - DeltaForEH; |
| 185 | *((TargetPtrT *)P) = NewLSDA; |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | return Ret; |
| 189 | } |
| 190 | |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame^] | 191 | static int64_t computeDelta(SectionEntry *A, SectionEntry *B) { |
| 192 | int64_t ObjDistance = A->ObjAddress - B->ObjAddress; |
| 193 | int64_t MemDistance = A->LoadAddress - B->LoadAddress; |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 194 | return ObjDistance - MemDistance; |
| 195 | } |
| 196 | |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame^] | 197 | template <typename Impl> |
| 198 | void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() { |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 199 | |
| 200 | if (!MemMgr) |
| 201 | return; |
| 202 | for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) { |
| 203 | EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i]; |
| 204 | if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID || |
| 205 | SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID) |
| 206 | continue; |
| 207 | SectionEntry *Text = &Sections[SectionInfo.TextSID]; |
| 208 | SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID]; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 209 | SectionEntry *ExceptTab = nullptr; |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 210 | if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID) |
| 211 | ExceptTab = &Sections[SectionInfo.ExceptTabSID]; |
| 212 | |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame^] | 213 | int64_t DeltaForText = computeDelta(Text, EHFrame); |
| 214 | int64_t DeltaForEH = 0; |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 215 | if (ExceptTab) |
| 216 | DeltaForEH = computeDelta(ExceptTab, EHFrame); |
| 217 | |
| 218 | unsigned char *P = EHFrame->Address; |
| 219 | unsigned char *End = P + EHFrame->Size; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 220 | do { |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 221 | P = processFDE(P, DeltaForText, DeltaForEH); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 222 | } while (P != End); |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 223 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 224 | MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress, |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 225 | EHFrame->Size); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 226 | } |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 227 | UnregisteredEHFrameSections.clear(); |
| 228 | } |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 229 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 230 | std::unique_ptr<RuntimeDyldMachO> |
| 231 | llvm::RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) { |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 232 | switch (Arch) { |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 233 | default: |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 234 | llvm_unreachable("Unsupported target for RuntimeDyldMachO."); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 235 | break; |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 236 | case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM); |
Tim Northover | e19bed7 | 2014-07-23 12:32:47 +0000 | [diff] [blame] | 237 | case Triple::aarch64: return make_unique<RuntimeDyldMachOAArch64>(MM); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 238 | case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM); |
| 239 | case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM); |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 240 | } |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 243 | } // end namespace llvm |