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 | |
| 14 | #define DEBUG_TYPE "dyld" |
| 15 | #include "llvm/ADT/OwningPtr.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 18 | #include "RuntimeDyldMachO.h" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | using namespace llvm::object; |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
Danil Malyshev | 799184d | 2012-03-21 21:06:29 +0000 | [diff] [blame^] | 24 | void RuntimeDyldMachO::resolveRelocation(uint8_t *LocalAddress, |
| 25 | uint64_t FinalAddress, |
| 26 | uint64_t Value, |
| 27 | uint32_t Type, |
| 28 | int64_t Addend) { |
| 29 | bool isPCRel = (Type >> 24) & 1; |
| 30 | unsigned MachoType = (Type >> 28) & 0xf; |
| 31 | unsigned Size = 1 << ((Type >> 25) & 3); |
| 32 | |
| 33 | DEBUG(dbgs() << "resolveRelocation LocalAddress: " << format("%p", LocalAddress) |
| 34 | << " FinalAddress: " << format("%p", FinalAddress) |
| 35 | << " Value: " << format("%p", Value) |
| 36 | << " Addend: " << Addend |
| 37 | << " isPCRel: " << isPCRel |
| 38 | << " MachoType: " << MachoType |
| 39 | << " Size: " << Size |
| 40 | << "\n"); |
| 41 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 42 | // This just dispatches to the proper target specific routine. |
Danil Malyshev | 799184d | 2012-03-21 21:06:29 +0000 | [diff] [blame^] | 43 | switch (Arch) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 44 | default: llvm_unreachable("Unsupported CPU type!"); |
Danil Malyshev | 799184d | 2012-03-21 21:06:29 +0000 | [diff] [blame^] | 45 | case Triple::x86_64: // Fall through. |
| 46 | case Triple::x86: |
| 47 | resolveX86_64Relocation(LocalAddress, |
| 48 | FinalAddress, |
| 49 | (uintptr_t)Value, |
| 50 | isPCRel, |
| 51 | MachoType, |
| 52 | Size, |
| 53 | Addend); |
| 54 | break; |
| 55 | case Triple::arm: // Fall through. |
| 56 | case Triple::thumb: |
| 57 | resolveARMRelocation(LocalAddress, |
| 58 | FinalAddress, |
| 59 | (uintptr_t)Value, |
| 60 | isPCRel, |
| 61 | MachoType, |
| 62 | Size, |
| 63 | Addend); |
| 64 | break; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 65 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | bool RuntimeDyldMachO:: |
Sean Callanan | 61dfa77 | 2012-03-07 23:05:25 +0000 | [diff] [blame] | 69 | resolveX86_64Relocation(uint8_t *LocalAddress, |
| 70 | uint64_t FinalAddress, |
| 71 | uint64_t Value, |
| 72 | bool isPCRel, |
| 73 | unsigned Type, |
| 74 | unsigned Size, |
| 75 | int64_t Addend) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 76 | // If the relocation is PC-relative, the value to be encoded is the |
| 77 | // pointer difference. |
| 78 | if (isPCRel) |
| 79 | // FIXME: It seems this value needs to be adjusted by 4 for an effective PC |
| 80 | // address. Is that expected? Only for branches, perhaps? |
Sean Callanan | 61dfa77 | 2012-03-07 23:05:25 +0000 | [diff] [blame] | 81 | Value -= FinalAddress + 4; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 82 | |
| 83 | switch(Type) { |
| 84 | default: |
| 85 | llvm_unreachable("Invalid relocation type!"); |
Jim Grosbach | 652ca2f | 2012-01-16 23:50:49 +0000 | [diff] [blame] | 86 | case macho::RIT_X86_64_Signed1: |
| 87 | case macho::RIT_X86_64_Signed2: |
| 88 | case macho::RIT_X86_64_Signed4: |
| 89 | case macho::RIT_X86_64_Signed: |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 90 | case macho::RIT_X86_64_Unsigned: |
| 91 | case macho::RIT_X86_64_Branch: { |
Jim Grosbach | 652ca2f | 2012-01-16 23:50:49 +0000 | [diff] [blame] | 92 | Value += Addend; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 93 | // Mask in the target value a byte at a time (we don't have an alignment |
| 94 | // guarantee for the target address, so this is safest). |
Sean Callanan | 61dfa77 | 2012-03-07 23:05:25 +0000 | [diff] [blame] | 95 | uint8_t *p = (uint8_t*)LocalAddress; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 96 | for (unsigned i = 0; i < Size; ++i) { |
| 97 | *p++ = (uint8_t)Value; |
| 98 | Value >>= 8; |
| 99 | } |
| 100 | return false; |
| 101 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 102 | case macho::RIT_X86_64_GOTLoad: |
| 103 | case macho::RIT_X86_64_GOT: |
| 104 | case macho::RIT_X86_64_Subtractor: |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 105 | case macho::RIT_X86_64_TLV: |
| 106 | return Error("Relocation type not implemented yet!"); |
| 107 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 110 | bool RuntimeDyldMachO:: |
Sean Callanan | 61dfa77 | 2012-03-07 23:05:25 +0000 | [diff] [blame] | 111 | resolveARMRelocation(uint8_t *LocalAddress, |
| 112 | uint64_t FinalAddress, |
| 113 | uint64_t Value, |
| 114 | bool isPCRel, |
| 115 | unsigned Type, |
| 116 | unsigned Size, |
| 117 | int64_t Addend) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 118 | // If the relocation is PC-relative, the value to be encoded is the |
| 119 | // pointer difference. |
| 120 | if (isPCRel) { |
Sean Callanan | 61dfa77 | 2012-03-07 23:05:25 +0000 | [diff] [blame] | 121 | Value -= FinalAddress; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 122 | // ARM PCRel relocations have an effective-PC offset of two instructions |
| 123 | // (four bytes in Thumb mode, 8 bytes in ARM mode). |
| 124 | // FIXME: For now, assume ARM mode. |
| 125 | Value -= 8; |
| 126 | } |
| 127 | |
| 128 | switch(Type) { |
| 129 | default: |
| 130 | llvm_unreachable("Invalid relocation type!"); |
| 131 | case macho::RIT_Vanilla: { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 132 | // Mask in the target value a byte at a time (we don't have an alignment |
| 133 | // guarantee for the target address, so this is safest). |
Sean Callanan | 61dfa77 | 2012-03-07 23:05:25 +0000 | [diff] [blame] | 134 | uint8_t *p = (uint8_t*)LocalAddress; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 135 | for (unsigned i = 0; i < Size; ++i) { |
| 136 | *p++ = (uint8_t)Value; |
| 137 | Value >>= 8; |
| 138 | } |
| 139 | break; |
| 140 | } |
| 141 | case macho::RIT_ARM_Branch24Bit: { |
| 142 | // Mask the value into the target address. We know instructions are |
| 143 | // 32-bit aligned, so we can do it all at once. |
Sean Callanan | 61dfa77 | 2012-03-07 23:05:25 +0000 | [diff] [blame] | 144 | uint32_t *p = (uint32_t*)LocalAddress; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 145 | // The low two bits of the value are not encoded. |
| 146 | Value >>= 2; |
| 147 | // Mask the value to 24 bits. |
| 148 | Value &= 0xffffff; |
| 149 | // FIXME: If the destination is a Thumb function (and the instruction |
| 150 | // is a non-predicated BL instruction), we need to change it to a BLX |
| 151 | // instruction instead. |
| 152 | |
| 153 | // Insert the value into the instruction. |
| 154 | *p = (*p & ~0xffffff) | Value; |
| 155 | break; |
| 156 | } |
| 157 | case macho::RIT_ARM_ThumbBranch22Bit: |
| 158 | case macho::RIT_ARM_ThumbBranch32Bit: |
| 159 | case macho::RIT_ARM_Half: |
| 160 | case macho::RIT_ARM_HalfDifference: |
| 161 | case macho::RIT_Pair: |
| 162 | case macho::RIT_Difference: |
| 163 | case macho::RIT_ARM_LocalDifference: |
| 164 | case macho::RIT_ARM_PreboundLazyPointer: |
| 165 | return Error("Relocation type not implemented yet!"); |
| 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
Danil Malyshev | 799184d | 2012-03-21 21:06:29 +0000 | [diff] [blame^] | 170 | void RuntimeDyldMachO:: |
| 171 | processRelocationRef(const ObjRelocationInfo &Rel, const ObjectFile &Obj, |
| 172 | ObjSectionToIDMap &ObjSectionToID, |
| 173 | LocalSymbolMap &Symbols, StubMap &Stubs) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 174 | |
Danil Malyshev | 799184d | 2012-03-21 21:06:29 +0000 | [diff] [blame^] | 175 | uint32_t RelType = (uint32_t) (Rel.Type & 0xffffffffL); |
| 176 | RelocationValueRef Value; |
| 177 | SectionEntry &Section = Sections[Rel.SectionID]; |
| 178 | uint8_t *Target = Section.Address + Rel.Offset; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 179 | |
Danil Malyshev | 799184d | 2012-03-21 21:06:29 +0000 | [diff] [blame^] | 180 | bool isExtern = (RelType >> 27) & 1; |
| 181 | if (isExtern) { |
| 182 | StringRef TargetName; |
| 183 | const SymbolRef &Symbol = Rel.Symbol; |
| 184 | Symbol.getName(TargetName); |
| 185 | // First look the symbol in object file symbols. |
| 186 | LocalSymbolMap::iterator it = Symbols.find(TargetName.data()); |
| 187 | if (it != Symbols.end()) { |
| 188 | Value.SectionID = it->second.first; |
| 189 | Value.Addend = it->second.second; |
| 190 | } else { |
| 191 | // Second look the symbol in global symbol table. |
| 192 | StringMap<SymbolLoc>::iterator itS = SymbolTable.find(TargetName.data()); |
| 193 | if (itS != SymbolTable.end()) { |
| 194 | Value.SectionID = itS->second.first; |
| 195 | Value.Addend = itS->second.second; |
| 196 | } else |
| 197 | Value.SymbolName = TargetName.data(); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 198 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 199 | } else { |
Danil Malyshev | 799184d | 2012-03-21 21:06:29 +0000 | [diff] [blame^] | 200 | error_code err; |
| 201 | uint8_t sIdx = static_cast<uint8_t>(RelType & 0xFF); |
| 202 | section_iterator sIt = Obj.begin_sections(), |
| 203 | sItEnd = Obj.end_sections(); |
| 204 | for (uint8_t i = 1; i < sIdx; i++) { |
| 205 | error_code err; |
| 206 | sIt.increment(err); |
| 207 | if (sIt == sItEnd) |
| 208 | break; |
| 209 | } |
| 210 | assert(sIt != sItEnd && "No section containing relocation!"); |
| 211 | Value.SectionID = findOrEmitSection(*sIt, true, ObjSectionToID); |
| 212 | Value.Addend = *(const intptr_t *)Target; |
| 213 | if (Value.Addend) { |
| 214 | // The MachO addend is offset from the current section, we need set it |
| 215 | // as offset from destination section |
| 216 | Value.Addend += Section.ObjAddress - Sections[Value.SectionID].ObjAddress; |
| 217 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Danil Malyshev | 799184d | 2012-03-21 21:06:29 +0000 | [diff] [blame^] | 220 | if (Arch == Triple::arm && RelType == macho::RIT_ARM_Branch24Bit) { |
| 221 | // This is an ARM branch relocation, need to use a stub function. |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 222 | |
Danil Malyshev | 799184d | 2012-03-21 21:06:29 +0000 | [diff] [blame^] | 223 | // Look up for existing stub. |
| 224 | StubMap::const_iterator stubIt = Stubs.find(Value); |
| 225 | if (stubIt != Stubs.end()) |
| 226 | resolveRelocation(Target, (uint64_t)Target, |
| 227 | (uint64_t)Section.Address + stubIt->second, |
| 228 | RelType, 0); |
| 229 | else { |
| 230 | // Create a new stub function. |
| 231 | Stubs[Value] = Section.StubOffset; |
| 232 | uint8_t *StubTargetAddr = createStubFunction(Section.Address + |
| 233 | Section.StubOffset); |
| 234 | AddRelocation(Value, Rel.SectionID, StubTargetAddr - Section.Address, |
| 235 | macho::RIT_Vanilla); |
| 236 | resolveRelocation(Target, (uint64_t)Target, |
| 237 | (uint64_t)Section.Address + Section.StubOffset, |
| 238 | RelType, 0); |
| 239 | Section.StubOffset += getMaxStubSize(); |
| 240 | } |
| 241 | } else |
| 242 | AddRelocation(Value, Rel.SectionID, Rel.Offset, RelType); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 245 | |
Danil Malyshev | 799184d | 2012-03-21 21:06:29 +0000 | [diff] [blame^] | 246 | bool RuntimeDyldMachO::isCompatibleFormat(const MemoryBuffer *InputBuffer) const { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 247 | StringRef Magic = InputBuffer->getBuffer().slice(0, 4); |
| 248 | if (Magic == "\xFE\xED\xFA\xCE") return true; |
| 249 | if (Magic == "\xCE\xFA\xED\xFE") return true; |
| 250 | if (Magic == "\xFE\xED\xFA\xCF") return true; |
| 251 | if (Magic == "\xCF\xFA\xED\xFE") return true; |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | } // end namespace llvm |