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