| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 1 | //===----- RuntimeDyldMachOARM.h ---- MachO/ARM specific code. ----*- C++ -*-=// |
| 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 | |
| Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 10 | #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H |
| 11 | #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 12 | |
| 13 | #include "../RuntimeDyldMachO.h" |
| 14 | |
| 15 | #define DEBUG_TYPE "dyld" |
| 16 | |
| 17 | namespace llvm { |
| 18 | |
| 19 | class RuntimeDyldMachOARM |
| 20 | : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> { |
| Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 21 | private: |
| 22 | typedef RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> ParentT; |
| 23 | |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 24 | public: |
| Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame] | 25 | |
| 26 | typedef uint32_t TargetPtrT; |
| 27 | |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 28 | RuntimeDyldMachOARM(RTDyldMemoryManager *MM) : RuntimeDyldMachOCRTPBase(MM) {} |
| 29 | |
| 30 | unsigned getMaxStubSize() override { return 8; } |
| 31 | |
| Lang Hames | e5fc826 | 2014-07-17 23:11:30 +0000 | [diff] [blame] | 32 | unsigned getStubAlignment() override { return 4; } |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 33 | |
| Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 34 | int64_t decodeAddend(const RelocationEntry &RE) const { |
| 35 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 36 | uint8_t *LocalAddress = Section.Address + RE.Offset; |
| 37 | |
| 38 | switch (RE.RelType) { |
| Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 39 | default: |
| Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 40 | return memcpyAddend(RE); |
| Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 41 | case MachO::ARM_RELOC_BR24: { |
| 42 | uint32_t Temp; |
| 43 | memcpy(&Temp, LocalAddress, 4); |
| 44 | Temp &= 0x00ffffff; // Mask out the opcode. |
| 45 | // Now we've got the shifted immediate, shift by 2, sign extend and ret. |
| 46 | return SignExtend32<26>(Temp << 2); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 51 | relocation_iterator |
| 52 | processRelocationRef(unsigned SectionID, relocation_iterator RelI, |
| 53 | ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID, |
| 54 | const SymbolTableMap &Symbols, StubMap &Stubs) override { |
| 55 | const MachOObjectFile &Obj = |
| 56 | static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile()); |
| 57 | MachO::any_relocation_info RelInfo = |
| 58 | Obj.getRelocation(RelI->getRawDataRefImpl()); |
| 59 | |
| 60 | if (Obj.isRelocationScattered(RelInfo)) |
| 61 | return ++++RelI; |
| 62 | |
| Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 63 | RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI)); |
| 64 | RE.Addend = decodeAddend(RE); |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 65 | RelocationValueRef Value( |
| 66 | getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols)); |
| 67 | |
| Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 68 | if (RE.IsPCRel) |
| 69 | makeValueAddendPCRel(Value, ObjImg, RelI, 8); |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 70 | |
| 71 | if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24) |
| 72 | processBranchRelocation(RE, Value, Stubs); |
| 73 | else { |
| Lang Hames | ca279c2 | 2014-09-07 04:03:32 +0000 | [diff] [blame^] | 74 | RE.Addend = Value.Offset; |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 75 | if (Value.SymbolName) |
| 76 | addRelocationForSymbol(RE, Value.SymbolName); |
| 77 | else |
| 78 | addRelocationForSection(RE, Value.SectionID); |
| 79 | } |
| 80 | |
| 81 | return ++RelI; |
| 82 | } |
| 83 | |
| Benjamin Kramer | 8c90fd7 | 2014-09-03 11:41:21 +0000 | [diff] [blame] | 84 | void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 85 | DEBUG(dumpRelocationToResolve(RE, Value)); |
| 86 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 87 | uint8_t *LocalAddress = Section.Address + RE.Offset; |
| 88 | |
| 89 | // If the relocation is PC-relative, the value to be encoded is the |
| 90 | // pointer difference. |
| 91 | if (RE.IsPCRel) { |
| 92 | uint64_t FinalAddress = Section.LoadAddress + RE.Offset; |
| 93 | Value -= FinalAddress; |
| 94 | // ARM PCRel relocations have an effective-PC offset of two instructions |
| 95 | // (four bytes in Thumb mode, 8 bytes in ARM mode). |
| 96 | // FIXME: For now, assume ARM mode. |
| 97 | Value -= 8; |
| 98 | } |
| 99 | |
| 100 | switch (RE.RelType) { |
| 101 | default: |
| 102 | llvm_unreachable("Invalid relocation type!"); |
| 103 | case MachO::ARM_RELOC_VANILLA: |
| Lang Hames | e1287c0 | 2014-08-29 23:17:47 +0000 | [diff] [blame] | 104 | writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size); |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 105 | break; |
| 106 | case MachO::ARM_RELOC_BR24: { |
| 107 | // Mask the value into the target address. We know instructions are |
| 108 | // 32-bit aligned, so we can do it all at once. |
| 109 | uint32_t *p = (uint32_t *)LocalAddress; |
| 110 | // The low two bits of the value are not encoded. |
| 111 | Value >>= 2; |
| 112 | // Mask the value to 24 bits. |
| 113 | uint64_t FinalValue = Value & 0xffffff; |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 114 | // FIXME: If the destination is a Thumb function (and the instruction |
| 115 | // is a non-predicated BL instruction), we need to change it to a BLX |
| 116 | // instruction instead. |
| 117 | |
| 118 | // Insert the value into the instruction. |
| 119 | *p = (*p & ~0xffffff) | FinalValue; |
| 120 | break; |
| 121 | } |
| 122 | case MachO::ARM_THUMB_RELOC_BR22: |
| 123 | case MachO::ARM_THUMB_32BIT_BRANCH: |
| 124 | case MachO::ARM_RELOC_HALF: |
| 125 | case MachO::ARM_RELOC_HALF_SECTDIFF: |
| 126 | case MachO::ARM_RELOC_PAIR: |
| 127 | case MachO::ARM_RELOC_SECTDIFF: |
| 128 | case MachO::ARM_RELOC_LOCAL_SECTDIFF: |
| 129 | case MachO::ARM_RELOC_PB_LA_PTR: |
| 130 | Error("Relocation type not implemented yet!"); |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void finalizeSection(ObjectImage &ObjImg, unsigned SectionID, |
| 136 | const SectionRef &Section) {} |
| 137 | |
| 138 | private: |
| 139 | void processBranchRelocation(const RelocationEntry &RE, |
| 140 | const RelocationValueRef &Value, |
| 141 | StubMap &Stubs) { |
| 142 | // This is an ARM branch relocation, need to use a stub function. |
| 143 | // Look up for existing stub. |
| 144 | SectionEntry &Section = Sections[RE.SectionID]; |
| 145 | RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value); |
| 146 | uint8_t *Addr; |
| 147 | if (i != Stubs.end()) { |
| 148 | Addr = Section.Address + i->second; |
| 149 | } else { |
| 150 | // Create a new stub function. |
| 151 | Stubs[Value] = Section.StubOffset; |
| 152 | uint8_t *StubTargetAddr = |
| 153 | createStubFunction(Section.Address + Section.StubOffset); |
| 154 | RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address, |
| Lang Hames | ca279c2 | 2014-09-07 04:03:32 +0000 | [diff] [blame^] | 155 | MachO::GENERIC_RELOC_VANILLA, Value.Offset, false, |
| Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 156 | 2); |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 157 | if (Value.SymbolName) |
| 158 | addRelocationForSymbol(StubRE, Value.SymbolName); |
| 159 | else |
| 160 | addRelocationForSection(StubRE, Value.SectionID); |
| 161 | Addr = Section.Address + Section.StubOffset; |
| 162 | Section.StubOffset += getMaxStubSize(); |
| 163 | } |
| Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 164 | RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0, |
| Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 165 | RE.IsPCRel, RE.Size); |
| 166 | resolveRelocation(TargetRE, (uint64_t)Addr); |
| 167 | } |
| 168 | }; |
| 169 | } |
| 170 | |
| 171 | #undef DEBUG_TYPE |
| 172 | |
| Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 173 | #endif |