Jim Grosbach | e0934be | 2012-01-16 23:50:58 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=// |
Danil Malyshev | cf852dc | 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 | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 14 | #include "RuntimeDyldMachO.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | using namespace llvm::object; |
| 19 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 20 | #define DEBUG_TYPE "dyld" |
| 21 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 22 | namespace llvm { |
| 23 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 24 | static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText, |
| 25 | intptr_t DeltaForEH) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 26 | DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText |
| 27 | << ", Delta for EH: " << DeltaForEH << "\n"); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 28 | uint32_t Length = *((uint32_t *)P); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 29 | P += 4; |
| 30 | unsigned char *Ret = P + Length; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 31 | uint32_t Offset = *((uint32_t *)P); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 32 | if (Offset == 0) // is a CIE |
| 33 | return Ret; |
| 34 | |
| 35 | P += 4; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 36 | intptr_t FDELocation = *((intptr_t *)P); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 37 | intptr_t NewLocation = FDELocation - DeltaForText; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 38 | *((intptr_t *)P) = NewLocation; |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 39 | P += sizeof(intptr_t); |
| 40 | |
| 41 | // Skip the FDE address range |
| 42 | P += sizeof(intptr_t); |
| 43 | |
| 44 | uint8_t Augmentationsize = *P; |
| 45 | P += 1; |
| 46 | if (Augmentationsize != 0) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 47 | intptr_t LSDA = *((intptr_t *)P); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 48 | intptr_t NewLSDA = LSDA - DeltaForEH; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 49 | *((intptr_t *)P) = NewLSDA; |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return Ret; |
| 53 | } |
| 54 | |
| 55 | static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 56 | intptr_t ObjDistance = A->ObjAddress - B->ObjAddress; |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 57 | intptr_t MemDistance = A->LoadAddress - B->LoadAddress; |
| 58 | return ObjDistance - MemDistance; |
| 59 | } |
| 60 | |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 61 | void RuntimeDyldMachO::registerEHFrames() { |
| 62 | |
| 63 | if (!MemMgr) |
| 64 | return; |
| 65 | for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) { |
| 66 | EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i]; |
| 67 | if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID || |
| 68 | SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID) |
| 69 | continue; |
| 70 | SectionEntry *Text = &Sections[SectionInfo.TextSID]; |
| 71 | SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID]; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 72 | SectionEntry *ExceptTab = nullptr; |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 73 | if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID) |
| 74 | ExceptTab = &Sections[SectionInfo.ExceptTabSID]; |
| 75 | |
| 76 | intptr_t DeltaForText = computeDelta(Text, EHFrame); |
| 77 | intptr_t DeltaForEH = 0; |
| 78 | if (ExceptTab) |
| 79 | DeltaForEH = computeDelta(ExceptTab, EHFrame); |
| 80 | |
| 81 | unsigned char *P = EHFrame->Address; |
| 82 | unsigned char *End = P + EHFrame->Size; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 83 | do { |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 84 | P = processFDE(P, DeltaForText, DeltaForEH); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 85 | } while (P != End); |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 86 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 87 | MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress, |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 88 | EHFrame->Size); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 89 | } |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 90 | UnregisteredEHFrameSections.clear(); |
| 91 | } |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 92 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 93 | void RuntimeDyldMachO::finalizeLoad(ObjectImage &ObjImg, |
| 94 | ObjSectionToIDMap &SectionMap) { |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 95 | unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID; |
| 96 | unsigned TextSID = RTDYLD_INVALID_SECTION_ID; |
| 97 | unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID; |
| 98 | ObjSectionToIDMap::iterator i, e; |
| 99 | for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) { |
| 100 | const SectionRef &Section = i->first; |
| 101 | StringRef Name; |
| 102 | Section.getName(Name); |
| 103 | if (Name == "__eh_frame") |
| 104 | EHFrameSID = i->second; |
| 105 | else if (Name == "__text") |
| 106 | TextSID = i->second; |
| 107 | else if (Name == "__gcc_except_tab") |
| 108 | ExceptTabSID = i->second; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 109 | else if (Name == "__jump_table") |
| 110 | populateJumpTable(cast<MachOObjectFile>(*ObjImg.getObjectFile()), |
| 111 | Section, i->second); |
| 112 | else if (Name == "__pointers") |
| 113 | populatePointersSection(cast<MachOObjectFile>(*ObjImg.getObjectFile()), |
| 114 | Section, i->second); |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 115 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 116 | UnregisteredEHFrameSections.push_back( |
| 117 | EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID)); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Andrew Kaylor | 32bd10b | 2013-08-19 19:38:06 +0000 | [diff] [blame] | 120 | // The target location for the relocation is described by RE.SectionID and |
| 121 | // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each |
| 122 | // SectionEntry has three members describing its location. |
| 123 | // SectionEntry::Address is the address at which the section has been loaded |
| 124 | // into memory in the current (host) process. SectionEntry::LoadAddress is the |
| 125 | // address that the section will have in the target process. |
| 126 | // SectionEntry::ObjAddress is the address of the bits for this section in the |
| 127 | // original emitted object image (also in the current address space). |
| 128 | // |
| 129 | // Relocations will be applied as if the section were loaded at |
| 130 | // SectionEntry::LoadAddress, but they will be applied at an address based |
| 131 | // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to |
| 132 | // Target memory contents if they are required for value calculations. |
| 133 | // |
| 134 | // The Value parameter here is the load address of the symbol for the |
| 135 | // relocation to be applied. For relocations which refer to symbols in the |
| 136 | // current object Value will be the LoadAddress of the section in which |
| 137 | // the symbol resides (RE.Addend provides additional information about the |
| 138 | // symbol location). For external symbols, Value will be the address of the |
| 139 | // symbol in the target address space. |
Rafael Espindola | 87b5017 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 140 | void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE, |
| 141 | uint64_t Value) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 142 | DEBUG ( |
| 143 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 144 | uint8_t* LocalAddress = Section.Address + RE.Offset; |
| 145 | uint64_t FinalAddress = Section.LoadAddress + RE.Offset; |
Rafael Espindola | 87b5017 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 146 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 147 | dbgs() << "resolveRelocation Section: " << RE.SectionID |
| 148 | << " LocalAddress: " << format("%p", LocalAddress) |
| 149 | << " FinalAddress: " << format("%p", FinalAddress) |
| 150 | << " Value: " << format("%p", Value) |
| 151 | << " Addend: " << RE.Addend |
| 152 | << " isPCRel: " << RE.IsPCRel |
| 153 | << " MachoType: " << RE.RelType |
| 154 | << " Size: " << (1 << RE.Size) << "\n"; |
| 155 | ); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 156 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 157 | // This just dispatches to the proper target specific routine. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 158 | switch (Arch) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 159 | default: |
| 160 | llvm_unreachable("Unsupported CPU type!"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 161 | case Triple::x86_64: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 162 | resolveX86_64Relocation(RE, Value); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 163 | break; |
| 164 | case Triple::x86: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 165 | resolveI386Relocation(RE, Value); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 166 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 167 | case Triple::arm: // Fall through. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 168 | case Triple::thumb: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 169 | resolveARMRelocation(RE, Value); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 170 | break; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 171 | case Triple::aarch64: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 172 | case Triple::arm64: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 173 | resolveAArch64Relocation(RE, Value); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 174 | break; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 175 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 178 | bool RuntimeDyldMachO::resolveI386Relocation(const RelocationEntry &RE, |
| 179 | uint64_t Value) { |
| 180 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 181 | uint8_t* LocalAddress = Section.Address + RE.Offset; |
Sean Callanan | b38aae4 | 2012-03-26 20:45:52 +0000 | [diff] [blame] | 182 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 183 | if (RE.IsPCRel) { |
| 184 | uint64_t FinalAddress = Section.LoadAddress + RE.Offset; |
| 185 | Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation. |
Sean Callanan | b38aae4 | 2012-03-26 20:45:52 +0000 | [diff] [blame] | 186 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 187 | |
| 188 | switch (RE.RelType) { |
| 189 | default: |
| 190 | llvm_unreachable("Invalid relocation type!"); |
| 191 | case MachO::GENERIC_RELOC_VANILLA: |
| 192 | return applyRelocationValue(LocalAddress, Value + RE.Addend, |
| 193 | 1 << RE.Size); |
| 194 | case MachO::GENERIC_RELOC_SECTDIFF: |
| 195 | case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: { |
| 196 | uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress; |
| 197 | uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress; |
| 198 | assert((Value == SectionABase || Value == SectionBBase) && |
| 199 | "Unexpected SECTDIFF relocation value."); |
| 200 | Value = SectionABase - SectionBBase + RE.Addend; |
| 201 | return applyRelocationValue(LocalAddress, Value, 1 << RE.Size); |
| 202 | } |
| 203 | case MachO::GENERIC_RELOC_PB_LA_PTR: |
| 204 | return Error("Relocation type not implemented yet!"); |
Sean Callanan | b38aae4 | 2012-03-26 20:45:52 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 208 | bool RuntimeDyldMachO::resolveX86_64Relocation(const RelocationEntry &RE, |
| 209 | uint64_t Value) { |
| 210 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 211 | uint8_t* LocalAddress = Section.Address + RE.Offset; |
| 212 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 213 | // If the relocation is PC-relative, the value to be encoded is the |
| 214 | // pointer difference. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 215 | if (RE.IsPCRel) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 216 | // FIXME: It seems this value needs to be adjusted by 4 for an effective PC |
| 217 | // address. Is that expected? Only for branches, perhaps? |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 218 | uint64_t FinalAddress = Section.LoadAddress + RE.Offset; |
| 219 | Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation. |
| 220 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 221 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 222 | switch (RE.RelType) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 223 | default: |
| 224 | llvm_unreachable("Invalid relocation type!"); |
Charles Davis | 5510728 | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 225 | case MachO::X86_64_RELOC_SIGNED_1: |
| 226 | case MachO::X86_64_RELOC_SIGNED_2: |
| 227 | case MachO::X86_64_RELOC_SIGNED_4: |
| 228 | case MachO::X86_64_RELOC_SIGNED: |
| 229 | case MachO::X86_64_RELOC_UNSIGNED: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 230 | case MachO::X86_64_RELOC_BRANCH: |
| 231 | return applyRelocationValue(LocalAddress, Value + RE.Addend, 1 << RE.Size); |
Charles Davis | 5510728 | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 232 | case MachO::X86_64_RELOC_GOT_LOAD: |
| 233 | case MachO::X86_64_RELOC_GOT: |
| 234 | case MachO::X86_64_RELOC_SUBTRACTOR: |
| 235 | case MachO::X86_64_RELOC_TLV: |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 236 | return Error("Relocation type not implemented yet!"); |
| 237 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 240 | bool RuntimeDyldMachO::resolveARMRelocation(const RelocationEntry &RE, |
| 241 | uint64_t Value) { |
| 242 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 243 | uint8_t* LocalAddress = Section.Address + RE.Offset; |
| 244 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 245 | // If the relocation is PC-relative, the value to be encoded is the |
| 246 | // pointer difference. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 247 | if (RE.IsPCRel) { |
| 248 | uint64_t FinalAddress = Section.LoadAddress + RE.Offset; |
Sean Callanan | 61dfa77 | 2012-03-07 23:05:25 +0000 | [diff] [blame] | 249 | Value -= FinalAddress; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 250 | // ARM PCRel relocations have an effective-PC offset of two instructions |
| 251 | // (four bytes in Thumb mode, 8 bytes in ARM mode). |
| 252 | // FIXME: For now, assume ARM mode. |
| 253 | Value -= 8; |
| 254 | } |
| 255 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 256 | switch (RE.RelType) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 257 | default: |
| 258 | llvm_unreachable("Invalid relocation type!"); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 259 | case MachO::ARM_RELOC_VANILLA: |
| 260 | return applyRelocationValue(LocalAddress, Value, 1 << RE.Size); |
Charles Davis | 5510728 | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 261 | case MachO::ARM_RELOC_BR24: { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 262 | // Mask the value into the target address. We know instructions are |
| 263 | // 32-bit aligned, so we can do it all at once. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 264 | uint32_t *p = (uint32_t *)LocalAddress; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 265 | // The low two bits of the value are not encoded. |
| 266 | Value >>= 2; |
| 267 | // Mask the value to 24 bits. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 268 | uint64_t FinalValue = Value & 0xffffff; |
| 269 | // Check for overflow. |
| 270 | if (Value != FinalValue) |
| 271 | return Error("ARM BR24 relocation out of range."); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 272 | // FIXME: If the destination is a Thumb function (and the instruction |
| 273 | // is a non-predicated BL instruction), we need to change it to a BLX |
| 274 | // instruction instead. |
| 275 | |
| 276 | // Insert the value into the instruction. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 277 | *p = (*p & ~0xffffff) | FinalValue; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 278 | break; |
| 279 | } |
Charles Davis | 5510728 | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 280 | case MachO::ARM_THUMB_RELOC_BR22: |
| 281 | case MachO::ARM_THUMB_32BIT_BRANCH: |
| 282 | case MachO::ARM_RELOC_HALF: |
| 283 | case MachO::ARM_RELOC_HALF_SECTDIFF: |
| 284 | case MachO::ARM_RELOC_PAIR: |
| 285 | case MachO::ARM_RELOC_SECTDIFF: |
| 286 | case MachO::ARM_RELOC_LOCAL_SECTDIFF: |
| 287 | case MachO::ARM_RELOC_PB_LA_PTR: |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 288 | return Error("Relocation type not implemented yet!"); |
| 289 | } |
| 290 | return false; |
| 291 | } |
| 292 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 293 | bool RuntimeDyldMachO::resolveAArch64Relocation(const RelocationEntry &RE, |
| 294 | uint64_t Value) { |
| 295 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 296 | uint8_t* LocalAddress = Section.Address + RE.Offset; |
| 297 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 298 | // If the relocation is PC-relative, the value to be encoded is the |
| 299 | // pointer difference. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 300 | if (RE.IsPCRel) { |
| 301 | uint64_t FinalAddress = Section.LoadAddress + RE.Offset; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 302 | Value -= FinalAddress; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 303 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 304 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 305 | switch (RE.RelType) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 306 | default: |
| 307 | llvm_unreachable("Invalid relocation type!"); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 308 | case MachO::ARM64_RELOC_UNSIGNED: |
| 309 | return applyRelocationValue(LocalAddress, Value, 1 << RE.Size); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 310 | case MachO::ARM64_RELOC_BRANCH26: { |
| 311 | // Mask the value into the target address. We know instructions are |
| 312 | // 32-bit aligned, so we can do it all at once. |
| 313 | uint32_t *p = (uint32_t *)LocalAddress; |
| 314 | // The low two bits of the value are not encoded. |
| 315 | Value >>= 2; |
| 316 | // Mask the value to 26 bits. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 317 | uint64_t FinalValue = Value & 0x3ffffff; |
| 318 | // Check for overflow. |
| 319 | if (FinalValue != Value) |
| 320 | return Error("ARM64 BRANCH26 relocation out of range."); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 321 | // Insert the value into the instruction. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 322 | *p = (*p & ~0x3ffffff) | FinalValue; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | case MachO::ARM64_RELOC_SUBTRACTOR: |
| 326 | case MachO::ARM64_RELOC_PAGE21: |
| 327 | case MachO::ARM64_RELOC_PAGEOFF12: |
| 328 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 329 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: |
| 330 | case MachO::ARM64_RELOC_POINTER_TO_GOT: |
| 331 | case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21: |
| 332 | case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12: |
| 333 | case MachO::ARM64_RELOC_ADDEND: |
| 334 | return Error("Relocation type not implemented yet!"); |
| 335 | } |
| 336 | return false; |
| 337 | } |
| 338 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 339 | void RuntimeDyldMachO::populateJumpTable(MachOObjectFile &Obj, |
| 340 | const SectionRef &JTSection, |
| 341 | unsigned JTSectionID) { |
| 342 | assert(!Obj.is64Bit() && |
| 343 | "__jump_table section not supported in 64-bit MachO."); |
| 344 | |
| 345 | MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand(); |
| 346 | MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl()); |
| 347 | uint32_t JTSectionSize = Sec32.size; |
| 348 | unsigned FirstIndirectSymbol = Sec32.reserved1; |
| 349 | unsigned JTEntrySize = Sec32.reserved2; |
| 350 | unsigned NumJTEntries = JTSectionSize / JTEntrySize; |
| 351 | uint8_t* JTSectionAddr = getSectionAddress(JTSectionID); |
| 352 | unsigned JTEntryOffset = 0; |
| 353 | |
| 354 | assert((JTSectionSize % JTEntrySize) == 0 && |
| 355 | "Jump-table section does not contain a whole number of stubs?"); |
| 356 | |
| 357 | for (unsigned i = 0; i < NumJTEntries; ++i) { |
| 358 | unsigned SymbolIndex = |
| 359 | Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i); |
| 360 | symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex); |
| 361 | StringRef IndirectSymbolName; |
| 362 | SI->getName(IndirectSymbolName); |
| 363 | uint8_t* JTEntryAddr = JTSectionAddr + JTEntryOffset; |
| 364 | createStubFunction(JTEntryAddr); |
| 365 | RelocationEntry RE(JTSectionID, JTEntryOffset + 1, |
| 366 | MachO::GENERIC_RELOC_VANILLA, 0, true, 2); |
| 367 | addRelocationForSymbol(RE, IndirectSymbolName); |
| 368 | JTEntryOffset += JTEntrySize; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void RuntimeDyldMachO::populatePointersSection(MachOObjectFile &Obj, |
| 373 | const SectionRef &PTSection, |
| 374 | unsigned PTSectionID) { |
| 375 | assert(!Obj.is64Bit() && |
| 376 | "__pointers section not supported in 64-bit MachO."); |
| 377 | |
| 378 | MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand(); |
| 379 | MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl()); |
| 380 | uint32_t PTSectionSize = Sec32.size; |
| 381 | unsigned FirstIndirectSymbol = Sec32.reserved1; |
| 382 | const unsigned PTEntrySize = 4; |
| 383 | unsigned NumPTEntries = PTSectionSize / PTEntrySize; |
| 384 | unsigned PTEntryOffset = 0; |
| 385 | |
| 386 | assert((PTSectionSize % PTEntrySize) == 0 && |
| 387 | "Pointers section does not contain a whole number of stubs?"); |
| 388 | |
| 389 | DEBUG(dbgs() << "Populating __pointers, Section ID " << PTSectionID |
| 390 | << ", " << NumPTEntries << " entries, " |
| 391 | << PTEntrySize << " bytes each:\n"); |
| 392 | |
| 393 | for (unsigned i = 0; i < NumPTEntries; ++i) { |
| 394 | unsigned SymbolIndex = |
| 395 | Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i); |
| 396 | symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex); |
| 397 | StringRef IndirectSymbolName; |
| 398 | SI->getName(IndirectSymbolName); |
| 399 | DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex |
| 400 | << ", PT offset: " << PTEntryOffset << "\n"); |
| 401 | RelocationEntry RE(PTSectionID, PTEntryOffset, |
| 402 | MachO::GENERIC_RELOC_VANILLA, 0, false, 2); |
| 403 | addRelocationForSymbol(RE, IndirectSymbolName); |
| 404 | PTEntryOffset += PTEntrySize; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | |
| 409 | section_iterator getSectionByAddress(const MachOObjectFile &Obj, |
| 410 | uint64_t Addr) { |
| 411 | section_iterator SI = Obj.section_begin(); |
| 412 | section_iterator SE = Obj.section_end(); |
| 413 | |
| 414 | for (; SI != SE; ++SI) { |
| 415 | uint64_t SAddr, SSize; |
| 416 | SI->getAddress(SAddr); |
| 417 | SI->getSize(SSize); |
| 418 | if ((Addr >= SAddr) && (Addr < SAddr + SSize)) |
| 419 | return SI; |
| 420 | } |
| 421 | |
| 422 | return SE; |
| 423 | } |
| 424 | |
| 425 | relocation_iterator RuntimeDyldMachO::processSECTDIFFRelocation( |
| 426 | unsigned SectionID, |
| 427 | relocation_iterator RelI, |
| 428 | ObjectImage &Obj, |
| 429 | ObjSectionToIDMap &ObjSectionToID) { |
| 430 | const MachOObjectFile *MachO = |
| 431 | static_cast<const MachOObjectFile*>(Obj.getObjectFile()); |
| 432 | MachO::any_relocation_info RE = |
| 433 | MachO->getRelocation(RelI->getRawDataRefImpl()); |
| 434 | |
| 435 | SectionEntry &Section = Sections[SectionID]; |
| 436 | uint32_t RelocType = MachO->getAnyRelocationType(RE); |
| 437 | bool IsPCRel = MachO->getAnyRelocationPCRel(RE); |
| 438 | unsigned Size = MachO->getAnyRelocationLength(RE); |
| 439 | uint64_t Offset; |
| 440 | RelI->getOffset(Offset); |
| 441 | uint8_t *LocalAddress = Section.Address + Offset; |
| 442 | unsigned NumBytes = 1 << Size; |
| 443 | int64_t Addend = 0; |
| 444 | memcpy(&Addend, LocalAddress, NumBytes); |
| 445 | |
| 446 | ++RelI; |
| 447 | MachO::any_relocation_info RE2 = |
| 448 | MachO->getRelocation(RelI->getRawDataRefImpl()); |
| 449 | |
| 450 | uint32_t AddrA = MachO->getScatteredRelocationValue(RE); |
| 451 | section_iterator SAI = getSectionByAddress(*MachO, AddrA); |
| 452 | assert(SAI != MachO->section_end() && "Can't find section for address A"); |
| 453 | uint64_t SectionABase; |
| 454 | SAI->getAddress(SectionABase); |
| 455 | uint64_t SectionAOffset = AddrA - SectionABase; |
| 456 | SectionRef SectionA = *SAI; |
| 457 | bool IsCode; |
| 458 | SectionA.isText(IsCode); |
| 459 | uint32_t SectionAID = findOrEmitSection(Obj, SectionA, IsCode, |
| 460 | ObjSectionToID); |
| 461 | |
| 462 | uint32_t AddrB = MachO->getScatteredRelocationValue(RE2); |
| 463 | section_iterator SBI = getSectionByAddress(*MachO, AddrB); |
| 464 | assert(SBI != MachO->section_end() && "Can't find section for address B"); |
| 465 | uint64_t SectionBBase; |
| 466 | SBI->getAddress(SectionBBase); |
| 467 | uint64_t SectionBOffset = AddrB - SectionBBase; |
| 468 | SectionRef SectionB = *SBI; |
| 469 | uint32_t SectionBID = findOrEmitSection(Obj, SectionB, IsCode, |
| 470 | ObjSectionToID); |
| 471 | |
| 472 | if (Addend != AddrA - AddrB) |
| 473 | Error("Unexpected SECTDIFF relocation addend."); |
| 474 | |
| 475 | DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB |
| 476 | << ", Addend: " << Addend << ", SectionA ID: " |
| 477 | << SectionAID << ", SectionAOffset: " << SectionAOffset |
| 478 | << ", SectionB ID: " << SectionBID << ", SectionBOffset: " |
| 479 | << SectionBOffset << "\n"); |
| 480 | RelocationEntry R(SectionID, Offset, RelocType, 0, |
| 481 | SectionAID, SectionAOffset, SectionBID, SectionBOffset, |
| 482 | IsPCRel, Size); |
| 483 | |
| 484 | addRelocationForSection(R, SectionAID); |
| 485 | addRelocationForSection(R, SectionBID); |
| 486 | |
| 487 | return ++RelI; |
| 488 | } |
| 489 | |
| 490 | relocation_iterator RuntimeDyldMachO::processI386ScatteredVANILLA( |
| 491 | unsigned SectionID, |
| 492 | relocation_iterator RelI, |
| 493 | ObjectImage &Obj, |
| 494 | ObjSectionToIDMap &ObjSectionToID) { |
| 495 | const MachOObjectFile *MachO = |
| 496 | static_cast<const MachOObjectFile*>(Obj.getObjectFile()); |
| 497 | MachO::any_relocation_info RE = |
| 498 | MachO->getRelocation(RelI->getRawDataRefImpl()); |
| 499 | |
| 500 | SectionEntry &Section = Sections[SectionID]; |
| 501 | uint32_t RelocType = MachO->getAnyRelocationType(RE); |
| 502 | bool IsPCRel = MachO->getAnyRelocationPCRel(RE); |
| 503 | unsigned Size = MachO->getAnyRelocationLength(RE); |
| 504 | uint64_t Offset; |
| 505 | RelI->getOffset(Offset); |
| 506 | uint8_t *LocalAddress = Section.Address + Offset; |
| 507 | unsigned NumBytes = 1 << Size; |
| 508 | int64_t Addend = 0; |
| 509 | memcpy(&Addend, LocalAddress, NumBytes); |
| 510 | |
| 511 | unsigned SymbolBaseAddr = MachO->getScatteredRelocationValue(RE); |
| 512 | section_iterator TargetSI = getSectionByAddress(*MachO, SymbolBaseAddr); |
| 513 | assert(TargetSI != MachO->section_end() && "Can't find section for symbol"); |
| 514 | uint64_t SectionBaseAddr; |
| 515 | TargetSI->getAddress(SectionBaseAddr); |
| 516 | SectionRef TargetSection = *TargetSI; |
| 517 | bool IsCode; |
| 518 | TargetSection.isText(IsCode); |
| 519 | uint32_t TargetSectionID = findOrEmitSection(Obj, TargetSection, IsCode, |
| 520 | ObjSectionToID); |
| 521 | |
| 522 | Addend -= SectionBaseAddr; |
| 523 | RelocationEntry R(SectionID, Offset, RelocType, Addend, |
| 524 | IsPCRel, Size); |
| 525 | |
| 526 | addRelocationForSection(R, TargetSectionID); |
| 527 | |
| 528 | return ++RelI; |
| 529 | } |
| 530 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 531 | relocation_iterator RuntimeDyldMachO::processRelocationRef( |
| 532 | unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj, |
| 533 | ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols, |
| 534 | StubMap &Stubs) { |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 535 | const ObjectFile *OF = Obj.getObjectFile(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 536 | const MachOObjectFile *MachO = static_cast<const MachOObjectFile *>(OF); |
| 537 | MachO::any_relocation_info RE = |
| 538 | MachO->getRelocation(RelI->getRawDataRefImpl()); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 539 | |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 540 | uint32_t RelType = MachO->getAnyRelocationType(RE); |
Lang Hames | 623f202 | 2013-08-09 00:57:01 +0000 | [diff] [blame] | 541 | |
| 542 | // FIXME: Properly handle scattered relocations. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 543 | // Special case the couple of scattered relocations that we know how |
| 544 | // to handle: SECTDIFF relocations, and scattered VANILLA relocations |
| 545 | // on I386. |
| 546 | // For all other scattered relocations, just bail out and hope for the |
| 547 | // best, since the offsets computed by scattered relocations have often |
| 548 | // been optimisticaly filled in by the compiler. This will fail |
| 549 | // horribly where the relocations *do* need to be applied, but that was |
| 550 | // already the case. |
| 551 | if (MachO->isRelocationScattered(RE)) { |
| 552 | if (RelType == MachO::GENERIC_RELOC_SECTDIFF || |
| 553 | RelType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) |
| 554 | return processSECTDIFFRelocation(SectionID, RelI, Obj, ObjSectionToID); |
| 555 | else if (Arch == Triple::x86 && RelType == MachO::GENERIC_RELOC_VANILLA) |
| 556 | return processI386ScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID); |
| 557 | else |
| 558 | return ++RelI; |
| 559 | } |
Lang Hames | 623f202 | 2013-08-09 00:57:01 +0000 | [diff] [blame] | 560 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 561 | RelocationValueRef Value; |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 562 | SectionEntry &Section = Sections[SectionID]; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 563 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 564 | bool IsExtern = MachO->getPlainRelocationExternal(RE); |
Rafael Espindola | 87b5017 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 565 | bool IsPCRel = MachO->getAnyRelocationPCRel(RE); |
| 566 | unsigned Size = MachO->getAnyRelocationLength(RE); |
Rafael Espindola | 8e6e02a | 2013-04-30 01:29:57 +0000 | [diff] [blame] | 567 | uint64_t Offset; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 568 | RelI->getOffset(Offset); |
Rafael Espindola | e87dadc | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 569 | uint8_t *LocalAddress = Section.Address + Offset; |
| 570 | unsigned NumBytes = 1 << Size; |
| 571 | uint64_t Addend = 0; |
| 572 | memcpy(&Addend, LocalAddress, NumBytes); |
Rafael Espindola | 8e6e02a | 2013-04-30 01:29:57 +0000 | [diff] [blame] | 573 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 574 | if (IsExtern) { |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 575 | // Obtain the symbol name which is referenced in the relocation |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 576 | symbol_iterator Symbol = RelI->getSymbol(); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 577 | StringRef TargetName; |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 578 | Symbol->getName(TargetName); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 579 | // First search for the symbol in the local symbol table |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 580 | SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data()); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 581 | if (lsi != Symbols.end()) { |
| 582 | Value.SectionID = lsi->second.first; |
Rafael Espindola | 8e6e02a | 2013-04-30 01:29:57 +0000 | [diff] [blame] | 583 | Value.Addend = lsi->second.second + Addend; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 584 | } else { |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 585 | // Search for the symbol in the global symbol table |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 586 | SymbolTableMap::const_iterator gsi = |
| 587 | GlobalSymbolTable.find(TargetName.data()); |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 588 | if (gsi != GlobalSymbolTable.end()) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 589 | Value.SectionID = gsi->second.first; |
Rafael Espindola | 8e6e02a | 2013-04-30 01:29:57 +0000 | [diff] [blame] | 590 | Value.Addend = gsi->second.second + Addend; |
| 591 | } else { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 592 | Value.SymbolName = TargetName.data(); |
Rafael Espindola | 8e6e02a | 2013-04-30 01:29:57 +0000 | [diff] [blame] | 593 | Value.Addend = Addend; |
| 594 | } |
Danil Malyshev | 4b0b8ef | 2012-03-29 21:46:18 +0000 | [diff] [blame] | 595 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 596 | |
| 597 | // Addends for external, PC-rel relocations on i386 point back to the zero |
| 598 | // offset. Calculate the final offset from the relocation target instead. |
| 599 | // This allows us to use the same logic for both external and internal |
| 600 | // relocations in resolveI386RelocationRef. |
| 601 | if (Arch == Triple::x86 && IsPCRel) { |
| 602 | uint64_t RelocAddr = 0; |
| 603 | RelI->getAddress(RelocAddr); |
| 604 | Value.Addend += RelocAddr + 4; |
| 605 | } |
| 606 | |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 607 | } else { |
Rafael Espindola | e87dadc | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 608 | SectionRef Sec = MachO->getRelocationSection(RE); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 609 | bool IsCode = false; |
| 610 | Sec.isText(IsCode); |
| 611 | Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID); |
Rafael Espindola | e87dadc | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 612 | uint64_t Addr; |
| 613 | Sec.getAddress(Addr); |
| 614 | Value.Addend = Addend - Addr; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 615 | if (IsPCRel) |
| 616 | Value.Addend += Offset + NumBytes; |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Charles Davis | 5510728 | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 619 | if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT || |
| 620 | RelType == MachO::X86_64_RELOC_GOT_LOAD)) { |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 621 | assert(IsPCRel); |
| 622 | assert(Size == 2); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 623 | |
| 624 | // FIXME: Teach the generic code above not to prematurely conflate |
| 625 | // relocation addends and symbol offsets. |
| 626 | Value.Addend -= Addend; |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 627 | StubMap::const_iterator i = Stubs.find(Value); |
| 628 | uint8_t *Addr; |
| 629 | if (i != Stubs.end()) { |
| 630 | Addr = Section.Address + i->second; |
| 631 | } else { |
| 632 | Stubs[Value] = Section.StubOffset; |
| 633 | uint8_t *GOTEntry = Section.Address + Section.StubOffset; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 634 | RelocationEntry GOTRE(SectionID, Section.StubOffset, |
| 635 | MachO::X86_64_RELOC_UNSIGNED, Value.Addend, false, |
| 636 | 3); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 637 | if (Value.SymbolName) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 638 | addRelocationForSymbol(GOTRE, Value.SymbolName); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 639 | else |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 640 | addRelocationForSection(GOTRE, Value.SectionID); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 641 | Section.StubOffset += 8; |
| 642 | Addr = GOTEntry; |
| 643 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 644 | RelocationEntry TargetRE(SectionID, Offset, |
| 645 | MachO::X86_64_RELOC_UNSIGNED, Addend, true, |
| 646 | 2); |
| 647 | resolveRelocation(TargetRE, (uint64_t)Addr); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 648 | } else if (Arch == Triple::arm && (RelType & 0xf) == MachO::ARM_RELOC_BR24) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 649 | // This is an ARM branch relocation, need to use a stub function. |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 650 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 651 | // Look up for existing stub. |
| 652 | StubMap::const_iterator i = Stubs.find(Value); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 653 | uint8_t *Addr; |
| 654 | if (i != Stubs.end()) { |
| 655 | Addr = Section.Address + i->second; |
| 656 | } else { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 657 | // Create a new stub function. |
| 658 | Stubs[Value] = Section.StubOffset; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 659 | uint8_t *StubTargetAddr = |
| 660 | createStubFunction(Section.Address + Section.StubOffset); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 661 | RelocationEntry StubRE(SectionID, StubTargetAddr - Section.Address, |
| 662 | MachO::GENERIC_RELOC_VANILLA, Value.Addend); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 663 | if (Value.SymbolName) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 664 | addRelocationForSymbol(StubRE, Value.SymbolName); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 665 | else |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 666 | addRelocationForSection(StubRE, Value.SectionID); |
| 667 | Addr = Section.Address + Section.StubOffset; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 668 | Section.StubOffset += getMaxStubSize(); |
| 669 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 670 | RelocationEntry TargetRE(Value.SectionID, Offset, RelType, 0, IsPCRel, |
| 671 | Size); |
| 672 | resolveRelocation(TargetRE, (uint64_t)Addr); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 673 | } else { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 674 | RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, IsPCRel, Size); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 675 | if (Value.SymbolName) |
| 676 | addRelocationForSymbol(RE, Value.SymbolName); |
| 677 | else |
| 678 | addRelocationForSection(RE, Value.SectionID); |
| 679 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 680 | return ++RelI; |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 683 | bool |
| 684 | RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const { |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 685 | if (InputBuffer->getBufferSize() < 4) |
| 686 | return false; |
| 687 | StringRef Magic(InputBuffer->getBufferStart(), 4); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 688 | if (Magic == "\xFE\xED\xFA\xCE") |
| 689 | return true; |
| 690 | if (Magic == "\xCE\xFA\xED\xFE") |
| 691 | return true; |
| 692 | if (Magic == "\xFE\xED\xFA\xCF") |
| 693 | return true; |
| 694 | if (Magic == "\xCF\xFA\xED\xFE") |
| 695 | return true; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 696 | return false; |
| 697 | } |
| 698 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 699 | bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const { |
| 700 | return Obj->isMachO(); |
| 701 | } |
| 702 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 703 | } // end namespace llvm |