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" |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | using namespace llvm::object; |
| 19 | |
Chandler Carruth | f58e376 | 2014-04-22 03:04:17 +0000 | [diff] [blame^] | 20 | #define DEBUG_TYPE "dyld" |
| 21 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 22 | namespace llvm { |
| 23 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 24 | static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText, |
| 25 | intptr_t DeltaForEH) { |
| 26 | uint32_t Length = *((uint32_t *)P); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 27 | P += 4; |
| 28 | unsigned char *Ret = P + Length; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 29 | uint32_t Offset = *((uint32_t *)P); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 30 | if (Offset == 0) // is a CIE |
| 31 | return Ret; |
| 32 | |
| 33 | P += 4; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 34 | intptr_t FDELocation = *((intptr_t *)P); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 35 | intptr_t NewLocation = FDELocation - DeltaForText; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 36 | *((intptr_t *)P) = NewLocation; |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 37 | P += sizeof(intptr_t); |
| 38 | |
| 39 | // Skip the FDE address range |
| 40 | P += sizeof(intptr_t); |
| 41 | |
| 42 | uint8_t Augmentationsize = *P; |
| 43 | P += 1; |
| 44 | if (Augmentationsize != 0) { |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 45 | intptr_t LSDA = *((intptr_t *)P); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 46 | intptr_t NewLSDA = LSDA - DeltaForEH; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 47 | *((intptr_t *)P) = NewLSDA; |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | return Ret; |
| 51 | } |
| 52 | |
| 53 | static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) { |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 54 | intptr_t ObjDistance = A->ObjAddress - B->ObjAddress; |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 55 | intptr_t MemDistance = A->LoadAddress - B->LoadAddress; |
| 56 | return ObjDistance - MemDistance; |
| 57 | } |
| 58 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 59 | void RuntimeDyldMachO::registerEHFrames() { |
| 60 | |
| 61 | if (!MemMgr) |
| 62 | return; |
| 63 | for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) { |
| 64 | EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i]; |
| 65 | if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID || |
| 66 | SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID) |
| 67 | continue; |
| 68 | SectionEntry *Text = &Sections[SectionInfo.TextSID]; |
| 69 | SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID]; |
| 70 | SectionEntry *ExceptTab = NULL; |
| 71 | if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID) |
| 72 | ExceptTab = &Sections[SectionInfo.ExceptTabSID]; |
| 73 | |
| 74 | intptr_t DeltaForText = computeDelta(Text, EHFrame); |
| 75 | intptr_t DeltaForEH = 0; |
| 76 | if (ExceptTab) |
| 77 | DeltaForEH = computeDelta(ExceptTab, EHFrame); |
| 78 | |
| 79 | unsigned char *P = EHFrame->Address; |
| 80 | unsigned char *End = P + EHFrame->Size; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 81 | do { |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 82 | P = processFDE(P, DeltaForText, DeltaForEH); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 83 | } while (P != End); |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 84 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 85 | MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress, |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 86 | EHFrame->Size); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 87 | } |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 88 | UnregisteredEHFrameSections.clear(); |
| 89 | } |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 90 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 91 | void RuntimeDyldMachO::finalizeLoad(ObjSectionToIDMap &SectionMap) { |
| 92 | unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID; |
| 93 | unsigned TextSID = RTDYLD_INVALID_SECTION_ID; |
| 94 | unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID; |
| 95 | ObjSectionToIDMap::iterator i, e; |
| 96 | for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) { |
| 97 | const SectionRef &Section = i->first; |
| 98 | StringRef Name; |
| 99 | Section.getName(Name); |
| 100 | if (Name == "__eh_frame") |
| 101 | EHFrameSID = i->second; |
| 102 | else if (Name == "__text") |
| 103 | TextSID = i->second; |
| 104 | else if (Name == "__gcc_except_tab") |
| 105 | ExceptTabSID = i->second; |
| 106 | } |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 107 | UnregisteredEHFrameSections.push_back( |
| 108 | EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID)); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Andrew Kaylor | 5f3a998 | 2013-08-19 19:38:06 +0000 | [diff] [blame] | 111 | // The target location for the relocation is described by RE.SectionID and |
| 112 | // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each |
| 113 | // SectionEntry has three members describing its location. |
| 114 | // SectionEntry::Address is the address at which the section has been loaded |
| 115 | // into memory in the current (host) process. SectionEntry::LoadAddress is the |
| 116 | // address that the section will have in the target process. |
| 117 | // SectionEntry::ObjAddress is the address of the bits for this section in the |
| 118 | // original emitted object image (also in the current address space). |
| 119 | // |
| 120 | // Relocations will be applied as if the section were loaded at |
| 121 | // SectionEntry::LoadAddress, but they will be applied at an address based |
| 122 | // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to |
| 123 | // Target memory contents if they are required for value calculations. |
| 124 | // |
| 125 | // The Value parameter here is the load address of the symbol for the |
| 126 | // relocation to be applied. For relocations which refer to symbols in the |
| 127 | // current object Value will be the LoadAddress of the section in which |
| 128 | // the symbol resides (RE.Addend provides additional information about the |
| 129 | // symbol location). For external symbols, Value will be the address of the |
| 130 | // symbol in the target address space. |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 131 | void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE, |
| 132 | uint64_t Value) { |
| 133 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 134 | return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend, |
| 135 | RE.IsPCRel, RE.Size); |
| 136 | } |
| 137 | |
Andrew Kaylor | fb05a50 | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 138 | void RuntimeDyldMachO::resolveRelocation(const SectionEntry &Section, |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 139 | uint64_t Offset, uint64_t Value, |
| 140 | uint32_t Type, int64_t Addend, |
| 141 | bool isPCRel, unsigned LogSize) { |
Andrew Kaylor | fb05a50 | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 142 | uint8_t *LocalAddress = Section.Address + Offset; |
| 143 | uint64_t FinalAddress = Section.LoadAddress + Offset; |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 144 | unsigned MachoType = Type; |
| 145 | unsigned Size = 1 << LogSize; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 146 | |
NAKAMURA Takumi | 87e0880 | 2013-12-07 11:21:42 +0000 | [diff] [blame] | 147 | DEBUG(dbgs() << "resolveRelocation LocalAddress: " |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 148 | << format("%p", LocalAddress) |
| 149 | << " FinalAddress: " << format("%p", FinalAddress) |
| 150 | << " Value: " << format("%p", Value) << " Addend: " << Addend |
| 151 | << " isPCRel: " << isPCRel << " MachoType: " << MachoType |
| 152 | << " Size: " << Size << "\n"); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 153 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 154 | // This just dispatches to the proper target specific routine. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 155 | switch (Arch) { |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 156 | default: |
| 157 | llvm_unreachable("Unsupported CPU type!"); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 158 | case Triple::x86_64: |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 159 | resolveX86_64Relocation(LocalAddress, FinalAddress, (uintptr_t)Value, |
| 160 | isPCRel, MachoType, Size, Addend); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 161 | break; |
| 162 | case Triple::x86: |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 163 | resolveI386Relocation(LocalAddress, FinalAddress, (uintptr_t)Value, isPCRel, |
| 164 | MachoType, Size, Addend); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 165 | break; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 166 | case Triple::arm: // Fall through. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 167 | case Triple::thumb: |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 168 | resolveARMRelocation(LocalAddress, FinalAddress, (uintptr_t)Value, isPCRel, |
| 169 | MachoType, Size, Addend); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 170 | break; |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 171 | case Triple::arm64: |
| 172 | resolveARM64Relocation(LocalAddress, FinalAddress, (uintptr_t)Value, |
| 173 | isPCRel, MachoType, Size, Addend); |
| 174 | break; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 175 | } |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 178 | bool RuntimeDyldMachO::resolveI386Relocation(uint8_t *LocalAddress, |
| 179 | uint64_t FinalAddress, |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 180 | uint64_t Value, bool isPCRel, |
| 181 | unsigned Type, unsigned Size, |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 182 | int64_t Addend) { |
Sean Callanan | a375943 | 2012-03-26 20:45:52 +0000 | [diff] [blame] | 183 | if (isPCRel) |
| 184 | Value -= FinalAddress + 4; // see resolveX86_64Relocation |
| 185 | |
| 186 | switch (Type) { |
| 187 | default: |
| 188 | llvm_unreachable("Invalid relocation type!"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 189 | case MachO::GENERIC_RELOC_VANILLA: { |
Sean Callanan | a375943 | 2012-03-26 20:45:52 +0000 | [diff] [blame] | 190 | uint8_t *p = LocalAddress; |
| 191 | uint64_t ValueToWrite = Value + Addend; |
| 192 | for (unsigned i = 0; i < Size; ++i) { |
| 193 | *p++ = (uint8_t)(ValueToWrite & 0xff); |
| 194 | ValueToWrite >>= 8; |
| 195 | } |
Jim Grosbach | 261dcca | 2013-01-31 19:46:28 +0000 | [diff] [blame] | 196 | return false; |
Sean Callanan | a375943 | 2012-03-26 20:45:52 +0000 | [diff] [blame] | 197 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 198 | case MachO::GENERIC_RELOC_SECTDIFF: |
| 199 | case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: |
| 200 | case MachO::GENERIC_RELOC_PB_LA_PTR: |
Sean Callanan | a375943 | 2012-03-26 20:45:52 +0000 | [diff] [blame] | 201 | return Error("Relocation type not implemented yet!"); |
| 202 | } |
| 203 | } |
| 204 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 205 | bool RuntimeDyldMachO::resolveX86_64Relocation(uint8_t *LocalAddress, |
| 206 | uint64_t FinalAddress, |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 207 | uint64_t Value, bool isPCRel, |
| 208 | unsigned Type, unsigned Size, |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 209 | int64_t Addend) { |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 210 | // If the relocation is PC-relative, the value to be encoded is the |
| 211 | // pointer difference. |
| 212 | if (isPCRel) |
| 213 | // FIXME: It seems this value needs to be adjusted by 4 for an effective PC |
| 214 | // address. Is that expected? Only for branches, perhaps? |
Sean Callanan | ca92a3d | 2012-03-07 23:05:25 +0000 | [diff] [blame] | 215 | Value -= FinalAddress + 4; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 216 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 217 | switch (Type) { |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 218 | default: |
| 219 | llvm_unreachable("Invalid relocation type!"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 220 | case MachO::X86_64_RELOC_SIGNED_1: |
| 221 | case MachO::X86_64_RELOC_SIGNED_2: |
| 222 | case MachO::X86_64_RELOC_SIGNED_4: |
| 223 | case MachO::X86_64_RELOC_SIGNED: |
| 224 | case MachO::X86_64_RELOC_UNSIGNED: |
| 225 | case MachO::X86_64_RELOC_BRANCH: { |
Jim Grosbach | 9df6cc8 | 2012-01-16 23:50:49 +0000 | [diff] [blame] | 226 | Value += Addend; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 227 | // Mask in the target value a byte at a time (we don't have an alignment |
| 228 | // guarantee for the target address, so this is safest). |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 229 | uint8_t *p = (uint8_t *)LocalAddress; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 230 | for (unsigned i = 0; i < Size; ++i) { |
| 231 | *p++ = (uint8_t)Value; |
| 232 | Value >>= 8; |
| 233 | } |
| 234 | return false; |
| 235 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 236 | case MachO::X86_64_RELOC_GOT_LOAD: |
| 237 | case MachO::X86_64_RELOC_GOT: |
| 238 | case MachO::X86_64_RELOC_SUBTRACTOR: |
| 239 | case MachO::X86_64_RELOC_TLV: |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 240 | return Error("Relocation type not implemented yet!"); |
| 241 | } |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 244 | bool RuntimeDyldMachO::resolveARMRelocation(uint8_t *LocalAddress, |
| 245 | uint64_t FinalAddress, |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 246 | uint64_t Value, bool isPCRel, |
| 247 | unsigned Type, unsigned Size, |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 248 | int64_t Addend) { |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 249 | // If the relocation is PC-relative, the value to be encoded is the |
| 250 | // pointer difference. |
| 251 | if (isPCRel) { |
Sean Callanan | ca92a3d | 2012-03-07 23:05:25 +0000 | [diff] [blame] | 252 | Value -= FinalAddress; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 253 | // ARM PCRel relocations have an effective-PC offset of two instructions |
| 254 | // (four bytes in Thumb mode, 8 bytes in ARM mode). |
| 255 | // FIXME: For now, assume ARM mode. |
| 256 | Value -= 8; |
| 257 | } |
| 258 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 259 | switch (Type) { |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 260 | default: |
| 261 | llvm_unreachable("Invalid relocation type!"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 262 | case MachO::ARM_RELOC_VANILLA: { |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 263 | // Mask in the target value a byte at a time (we don't have an alignment |
| 264 | // guarantee for the target address, so this is safest). |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 265 | uint8_t *p = (uint8_t *)LocalAddress; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 266 | for (unsigned i = 0; i < Size; ++i) { |
Charles Davis | 1827bd8 | 2013-08-27 05:38:30 +0000 | [diff] [blame] | 267 | *p++ = (uint8_t)Value; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 268 | Value >>= 8; |
| 269 | } |
| 270 | break; |
| 271 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 272 | case MachO::ARM_RELOC_BR24: { |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 273 | // Mask the value into the target address. We know instructions are |
| 274 | // 32-bit aligned, so we can do it all at once. |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 275 | uint32_t *p = (uint32_t *)LocalAddress; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 276 | // The low two bits of the value are not encoded. |
| 277 | Value >>= 2; |
| 278 | // Mask the value to 24 bits. |
| 279 | Value &= 0xffffff; |
| 280 | // FIXME: If the destination is a Thumb function (and the instruction |
| 281 | // is a non-predicated BL instruction), we need to change it to a BLX |
| 282 | // instruction instead. |
| 283 | |
| 284 | // Insert the value into the instruction. |
| 285 | *p = (*p & ~0xffffff) | Value; |
| 286 | break; |
| 287 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 288 | case MachO::ARM_THUMB_RELOC_BR22: |
| 289 | case MachO::ARM_THUMB_32BIT_BRANCH: |
| 290 | case MachO::ARM_RELOC_HALF: |
| 291 | case MachO::ARM_RELOC_HALF_SECTDIFF: |
| 292 | case MachO::ARM_RELOC_PAIR: |
| 293 | case MachO::ARM_RELOC_SECTDIFF: |
| 294 | case MachO::ARM_RELOC_LOCAL_SECTDIFF: |
| 295 | case MachO::ARM_RELOC_PB_LA_PTR: |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 296 | return Error("Relocation type not implemented yet!"); |
| 297 | } |
| 298 | return false; |
| 299 | } |
| 300 | |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 301 | bool RuntimeDyldMachO::resolveARM64Relocation(uint8_t *LocalAddress, |
| 302 | uint64_t FinalAddress, |
| 303 | uint64_t Value, bool isPCRel, |
| 304 | unsigned Type, unsigned Size, |
| 305 | int64_t Addend) { |
| 306 | // If the relocation is PC-relative, the value to be encoded is the |
| 307 | // pointer difference. |
| 308 | if (isPCRel) |
| 309 | Value -= FinalAddress; |
| 310 | |
| 311 | switch (Type) { |
| 312 | default: |
| 313 | llvm_unreachable("Invalid relocation type!"); |
| 314 | case MachO::ARM64_RELOC_UNSIGNED: { |
| 315 | // Mask in the target value a byte at a time (we don't have an alignment |
| 316 | // guarantee for the target address, so this is safest). |
| 317 | uint8_t *p = (uint8_t *)LocalAddress; |
| 318 | for (unsigned i = 0; i < Size; ++i) { |
| 319 | *p++ = (uint8_t)Value; |
| 320 | Value >>= 8; |
| 321 | } |
| 322 | break; |
| 323 | } |
| 324 | case MachO::ARM64_RELOC_BRANCH26: { |
| 325 | // Mask the value into the target address. We know instructions are |
| 326 | // 32-bit aligned, so we can do it all at once. |
| 327 | uint32_t *p = (uint32_t *)LocalAddress; |
| 328 | // The low two bits of the value are not encoded. |
| 329 | Value >>= 2; |
| 330 | // Mask the value to 26 bits. |
| 331 | Value &= 0x3ffffff; |
| 332 | // Insert the value into the instruction. |
| 333 | *p = (*p & ~0x3ffffff) | Value; |
| 334 | break; |
| 335 | } |
| 336 | case MachO::ARM64_RELOC_SUBTRACTOR: |
| 337 | case MachO::ARM64_RELOC_PAGE21: |
| 338 | case MachO::ARM64_RELOC_PAGEOFF12: |
| 339 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 340 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: |
| 341 | case MachO::ARM64_RELOC_POINTER_TO_GOT: |
| 342 | case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21: |
| 343 | case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12: |
| 344 | case MachO::ARM64_RELOC_ADDEND: |
| 345 | return Error("Relocation type not implemented yet!"); |
| 346 | } |
| 347 | return false; |
| 348 | } |
| 349 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 350 | relocation_iterator RuntimeDyldMachO::processRelocationRef( |
| 351 | unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj, |
| 352 | ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols, |
| 353 | StubMap &Stubs) { |
Rafael Espindola | 4d4a48d | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 354 | const ObjectFile *OF = Obj.getObjectFile(); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 355 | const MachOObjectFile *MachO = static_cast<const MachOObjectFile *>(OF); |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 356 | MachO::any_relocation_info RE = |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 357 | MachO->getRelocation(RelI->getRawDataRefImpl()); |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 358 | |
Rafael Espindola | 4d4a48d | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 359 | uint32_t RelType = MachO->getAnyRelocationType(RE); |
Lang Hames | fe2833b | 2013-08-09 00:57:01 +0000 | [diff] [blame] | 360 | |
| 361 | // FIXME: Properly handle scattered relocations. |
| 362 | // For now, optimistically skip these: they can often be ignored, as |
| 363 | // the static linker will already have applied the relocation, and it |
| 364 | // only needs to be reapplied if symbols move relative to one another. |
| 365 | // Note: This will fail horribly where the relocations *do* need to be |
| 366 | // applied, but that was already the case. |
| 367 | if (MachO->isRelocationScattered(RE)) |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 368 | return ++RelI; |
Lang Hames | fe2833b | 2013-08-09 00:57:01 +0000 | [diff] [blame] | 369 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 370 | RelocationValueRef Value; |
Rafael Espindola | 4d4a48d | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 371 | SectionEntry &Section = Sections[SectionID]; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 372 | |
Rafael Espindola | 4d4a48d | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 373 | bool isExtern = MachO->getPlainRelocationExternal(RE); |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 374 | bool IsPCRel = MachO->getAnyRelocationPCRel(RE); |
| 375 | unsigned Size = MachO->getAnyRelocationLength(RE); |
Rafael Espindola | d00c276 | 2013-04-30 01:29:57 +0000 | [diff] [blame] | 376 | uint64_t Offset; |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 377 | RelI->getOffset(Offset); |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 378 | uint8_t *LocalAddress = Section.Address + Offset; |
| 379 | unsigned NumBytes = 1 << Size; |
| 380 | uint64_t Addend = 0; |
| 381 | memcpy(&Addend, LocalAddress, NumBytes); |
Rafael Espindola | d00c276 | 2013-04-30 01:29:57 +0000 | [diff] [blame] | 382 | |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 383 | if (isExtern) { |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 384 | // Obtain the symbol name which is referenced in the relocation |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 385 | symbol_iterator Symbol = RelI->getSymbol(); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 386 | StringRef TargetName; |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 387 | Symbol->getName(TargetName); |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 388 | // First search for the symbol in the local symbol table |
Eli Bendersky | fc07908 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 389 | SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data()); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 390 | if (lsi != Symbols.end()) { |
| 391 | Value.SectionID = lsi->second.first; |
Rafael Espindola | d00c276 | 2013-04-30 01:29:57 +0000 | [diff] [blame] | 392 | Value.Addend = lsi->second.second + Addend; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 393 | } else { |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 394 | // Search for the symbol in the global symbol table |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 395 | SymbolTableMap::const_iterator gsi = |
| 396 | GlobalSymbolTable.find(TargetName.data()); |
Eli Bendersky | fc07908 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 397 | if (gsi != GlobalSymbolTable.end()) { |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 398 | Value.SectionID = gsi->second.first; |
Rafael Espindola | d00c276 | 2013-04-30 01:29:57 +0000 | [diff] [blame] | 399 | Value.Addend = gsi->second.second + Addend; |
| 400 | } else { |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 401 | Value.SymbolName = TargetName.data(); |
Rafael Espindola | d00c276 | 2013-04-30 01:29:57 +0000 | [diff] [blame] | 402 | Value.Addend = Addend; |
| 403 | } |
Danil Malyshev | 3548eaf | 2012-03-29 21:46:18 +0000 | [diff] [blame] | 404 | } |
Bill Wendling | 76fdc4b | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 405 | } else { |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 406 | SectionRef Sec = MachO->getRelocationSection(RE); |
Lang Hames | 9b2dc93 | 2014-02-18 21:46:39 +0000 | [diff] [blame] | 407 | bool IsCode = false; |
| 408 | Sec.isText(IsCode); |
| 409 | Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID); |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 410 | uint64_t Addr; |
| 411 | Sec.getAddress(Addr); |
| 412 | Value.Addend = Addend - Addr; |
Lang Hames | 2cbbdf4 | 2014-01-29 18:31:35 +0000 | [diff] [blame] | 413 | if (IsPCRel) |
| 414 | Value.Addend += Offset + NumBytes; |
Bill Wendling | 76fdc4b | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 417 | if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT || |
| 418 | RelType == MachO::X86_64_RELOC_GOT_LOAD)) { |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 419 | assert(IsPCRel); |
| 420 | assert(Size == 2); |
| 421 | StubMap::const_iterator i = Stubs.find(Value); |
| 422 | uint8_t *Addr; |
| 423 | if (i != Stubs.end()) { |
| 424 | Addr = Section.Address + i->second; |
| 425 | } else { |
| 426 | Stubs[Value] = Section.StubOffset; |
| 427 | uint8_t *GOTEntry = Section.Address + Section.StubOffset; |
| 428 | RelocationEntry RE(SectionID, Section.StubOffset, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 429 | MachO::X86_64_RELOC_UNSIGNED, 0, false, 3); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 430 | if (Value.SymbolName) |
| 431 | addRelocationForSymbol(RE, Value.SymbolName); |
| 432 | else |
| 433 | addRelocationForSection(RE, Value.SectionID); |
| 434 | Section.StubOffset += 8; |
| 435 | Addr = GOTEntry; |
| 436 | } |
| 437 | resolveRelocation(Section, Offset, (uint64_t)Addr, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 438 | MachO::X86_64_RELOC_UNSIGNED, Value.Addend, true, 2); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 439 | } else if (Arch == Triple::arm && (RelType & 0xf) == MachO::ARM_RELOC_BR24) { |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 440 | // This is an ARM branch relocation, need to use a stub function. |
Bill Wendling | 76fdc4b | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 441 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 442 | // Look up for existing stub. |
| 443 | StubMap::const_iterator i = Stubs.find(Value); |
| 444 | if (i != Stubs.end()) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 445 | resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second, |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 446 | RelType, 0, IsPCRel, Size); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 447 | else { |
| 448 | // Create a new stub function. |
| 449 | Stubs[Value] = Section.StubOffset; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 450 | uint8_t *StubTargetAddr = |
| 451 | createStubFunction(Section.Address + Section.StubOffset); |
Rafael Espindola | 4d4a48d | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 452 | RelocationEntry RE(SectionID, StubTargetAddr - Section.Address, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 453 | MachO::GENERIC_RELOC_VANILLA, Value.Addend); |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 454 | if (Value.SymbolName) |
| 455 | addRelocationForSymbol(RE, Value.SymbolName); |
| 456 | else |
| 457 | addRelocationForSection(RE, Value.SectionID); |
Rafael Espindola | 4d4a48d | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 458 | resolveRelocation(Section, Offset, |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 459 | (uint64_t)Section.Address + Section.StubOffset, RelType, |
| 460 | 0, IsPCRel, Size); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 461 | Section.StubOffset += getMaxStubSize(); |
| 462 | } |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 463 | } else { |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 464 | RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, IsPCRel, Size); |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 465 | if (Value.SymbolName) |
| 466 | addRelocationForSymbol(RE, Value.SymbolName); |
| 467 | else |
| 468 | addRelocationForSection(RE, Value.SectionID); |
| 469 | } |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 470 | return ++RelI; |
Bill Wendling | 76fdc4b | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 473 | bool |
| 474 | RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const { |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 475 | if (InputBuffer->getBufferSize() < 4) |
| 476 | return false; |
| 477 | StringRef Magic(InputBuffer->getBufferStart(), 4); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 478 | if (Magic == "\xFE\xED\xFA\xCE") |
| 479 | return true; |
| 480 | if (Magic == "\xCE\xFA\xED\xFE") |
| 481 | return true; |
| 482 | if (Magic == "\xFE\xED\xFA\xCF") |
| 483 | return true; |
| 484 | if (Magic == "\xCF\xFA\xED\xFE") |
| 485 | return true; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 486 | return false; |
| 487 | } |
| 488 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 489 | bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const { |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 490 | return Obj->isMachO(); |
| 491 | } |
| 492 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 493 | } // end namespace llvm |