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" |
Lang Hames | bc38ea9 | 2016-04-27 20:54:49 +0000 | [diff] [blame] | 14 | #include <string> |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 15 | |
| 16 | #define DEBUG_TYPE "dyld" |
| 17 | |
| 18 | namespace llvm { |
| 19 | |
| 20 | class RuntimeDyldMachOARM |
| 21 | : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> { |
Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 22 | private: |
| 23 | typedef RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> ParentT; |
| 24 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 25 | public: |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame] | 26 | |
| 27 | typedef uint32_t TargetPtrT; |
| 28 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 29 | RuntimeDyldMachOARM(RuntimeDyld::MemoryManager &MM, |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 30 | JITSymbolResolver &Resolver) |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 31 | : RuntimeDyldMachOCRTPBase(MM, Resolver) {} |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 32 | |
| 33 | unsigned getMaxStubSize() override { return 8; } |
| 34 | |
Lang Hames | e5fc826 | 2014-07-17 23:11:30 +0000 | [diff] [blame] | 35 | unsigned getStubAlignment() override { return 4; } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 36 | |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 37 | int64_t decodeAddend(const RelocationEntry &RE) const { |
| 38 | const SectionEntry &Section = Sections[RE.SectionID]; |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 39 | uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset); |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 40 | |
| 41 | switch (RE.RelType) { |
Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 42 | default: |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 43 | return memcpyAddend(RE); |
Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 44 | case MachO::ARM_RELOC_BR24: { |
Daniel Sanders | 66e799f | 2014-11-06 09:53:05 +0000 | [diff] [blame] | 45 | uint32_t Temp = readBytesUnaligned(LocalAddress, 4); |
Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 46 | Temp &= 0x00ffffff; // Mask out the opcode. |
| 47 | // Now we've got the shifted immediate, shift by 2, sign extend and ret. |
| 48 | return SignExtend32<26>(Temp << 2); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 53 | Expected<relocation_iterator> |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 54 | processRelocationRef(unsigned SectionID, relocation_iterator RelI, |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 55 | const ObjectFile &BaseObjT, |
| 56 | ObjSectionToIDMap &ObjSectionToID, |
Lang Hames | a5cd950 | 2014-11-27 05:40:13 +0000 | [diff] [blame] | 57 | StubMap &Stubs) override { |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 58 | const MachOObjectFile &Obj = |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 59 | static_cast<const MachOObjectFile &>(BaseObjT); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 60 | MachO::any_relocation_info RelInfo = |
| 61 | Obj.getRelocation(RelI->getRawDataRefImpl()); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 62 | uint32_t RelType = Obj.getAnyRelocationType(RelInfo); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 63 | |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 64 | if (Obj.isRelocationScattered(RelInfo)) { |
| 65 | if (RelType == MachO::ARM_RELOC_HALF_SECTDIFF) |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 66 | return processHALFSECTDIFFRelocation(SectionID, RelI, Obj, |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 67 | ObjSectionToID); |
Lang Hames | a8183e5 | 2015-07-24 17:40:04 +0000 | [diff] [blame] | 68 | else if (RelType == MachO::GENERIC_RELOC_VANILLA) |
| 69 | return processScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 70 | else |
Lang Hames | a8183e5 | 2015-07-24 17:40:04 +0000 | [diff] [blame] | 71 | return ++RelI; |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 72 | } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 73 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 74 | // Sanity check relocation type. |
| 75 | switch (RelType) { |
| 76 | UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_PAIR); |
| 77 | UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_SECTDIFF); |
| 78 | UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_LOCAL_SECTDIFF); |
| 79 | UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_PB_LA_PTR); |
| 80 | UNIMPLEMENTED_RELOC(MachO::ARM_THUMB_RELOC_BR22); |
| 81 | UNIMPLEMENTED_RELOC(MachO::ARM_THUMB_32BIT_BRANCH); |
| 82 | UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_HALF); |
| 83 | default: |
| 84 | if (RelType > MachO::ARM_RELOC_HALF_SECTDIFF) |
Chaoren Lin | 49317f2 | 2016-04-28 00:49:37 +0000 | [diff] [blame] | 85 | return make_error<RuntimeDyldError>(("MachO ARM relocation type " + |
| 86 | Twine(RelType) + |
| 87 | " is out of range").str()); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 91 | RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI)); |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 92 | RE.Addend = decodeAddend(RE); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 93 | RelocationValueRef Value; |
| 94 | if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID)) |
| 95 | Value = *ValueOrErr; |
| 96 | else |
| 97 | return ValueOrErr.takeError(); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 98 | |
Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 99 | if (RE.IsPCRel) |
Rafael Espindola | 76ad232 | 2015-07-06 14:55:37 +0000 | [diff] [blame] | 100 | makeValueAddendPCRel(Value, RelI, 8); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 101 | |
| 102 | if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24) |
| 103 | processBranchRelocation(RE, Value, Stubs); |
| 104 | else { |
Lang Hames | ca279c2 | 2014-09-07 04:03:32 +0000 | [diff] [blame] | 105 | RE.Addend = Value.Offset; |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 106 | if (Value.SymbolName) |
| 107 | addRelocationForSymbol(RE, Value.SymbolName); |
| 108 | else |
| 109 | addRelocationForSection(RE, Value.SectionID); |
| 110 | } |
| 111 | |
| 112 | return ++RelI; |
| 113 | } |
| 114 | |
Benjamin Kramer | 8c90fd7 | 2014-09-03 11:41:21 +0000 | [diff] [blame] | 115 | void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 116 | DEBUG(dumpRelocationToResolve(RE, Value)); |
| 117 | const SectionEntry &Section = Sections[RE.SectionID]; |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 118 | uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 119 | |
| 120 | // If the relocation is PC-relative, the value to be encoded is the |
| 121 | // pointer difference. |
| 122 | if (RE.IsPCRel) { |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 123 | uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 124 | Value -= FinalAddress; |
| 125 | // ARM PCRel relocations have an effective-PC offset of two instructions |
| 126 | // (four bytes in Thumb mode, 8 bytes in ARM mode). |
| 127 | // FIXME: For now, assume ARM mode. |
| 128 | Value -= 8; |
| 129 | } |
| 130 | |
| 131 | switch (RE.RelType) { |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 132 | case MachO::ARM_RELOC_VANILLA: |
Lang Hames | 4669cd0 | 2014-09-11 17:27:01 +0000 | [diff] [blame] | 133 | writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 134 | break; |
| 135 | case MachO::ARM_RELOC_BR24: { |
| 136 | // Mask the value into the target address. We know instructions are |
| 137 | // 32-bit aligned, so we can do it all at once. |
Lang Hames | 4669cd0 | 2014-09-11 17:27:01 +0000 | [diff] [blame] | 138 | Value += RE.Addend; |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 139 | // The low two bits of the value are not encoded. |
| 140 | Value >>= 2; |
| 141 | // Mask the value to 24 bits. |
| 142 | uint64_t FinalValue = Value & 0xffffff; |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 143 | // FIXME: If the destination is a Thumb function (and the instruction |
| 144 | // is a non-predicated BL instruction), we need to change it to a BLX |
| 145 | // instruction instead. |
| 146 | |
| 147 | // Insert the value into the instruction. |
Daniel Sanders | 66e799f | 2014-11-06 09:53:05 +0000 | [diff] [blame] | 148 | uint32_t Temp = readBytesUnaligned(LocalAddress, 4); |
| 149 | writeBytesUnaligned((Temp & ~0xffffff) | FinalValue, LocalAddress, 4); |
| 150 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 151 | break; |
| 152 | } |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 153 | case MachO::ARM_RELOC_HALF_SECTDIFF: { |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 154 | uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress(); |
| 155 | uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress(); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 156 | assert((Value == SectionABase || Value == SectionBBase) && |
| 157 | "Unexpected HALFSECTDIFF relocation value."); |
| 158 | Value = SectionABase - SectionBBase + RE.Addend; |
| 159 | if (RE.Size & 0x1) // :upper16: |
| 160 | Value = (Value >> 16); |
| 161 | Value &= 0xffff; |
| 162 | |
Daniel Sanders | 66e799f | 2014-11-06 09:53:05 +0000 | [diff] [blame] | 163 | uint32_t Insn = readBytesUnaligned(LocalAddress, 4); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 164 | Insn = (Insn & 0xfff0f000) | ((Value & 0xf000) << 4) | (Value & 0x0fff); |
Daniel Sanders | 66e799f | 2014-11-06 09:53:05 +0000 | [diff] [blame] | 165 | writeBytesUnaligned(Insn, LocalAddress, 4); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 169 | default: |
| 170 | llvm_unreachable("Invalid relocation type"); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 174 | Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 175 | const SectionRef &Section) { |
| 176 | StringRef Name; |
| 177 | Section.getName(Name); |
| 178 | |
| 179 | if (Name == "__nl_symbol_ptr") |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 180 | return populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj), |
| 181 | Section, SectionID); |
| 182 | return Error::success(); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 183 | } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 184 | |
| 185 | private: |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 186 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 187 | void processBranchRelocation(const RelocationEntry &RE, |
| 188 | const RelocationValueRef &Value, |
| 189 | StubMap &Stubs) { |
| 190 | // This is an ARM branch relocation, need to use a stub function. |
| 191 | // Look up for existing stub. |
| 192 | SectionEntry &Section = Sections[RE.SectionID]; |
| 193 | RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value); |
| 194 | uint8_t *Addr; |
| 195 | if (i != Stubs.end()) { |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 196 | Addr = Section.getAddressWithOffset(i->second); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 197 | } else { |
| 198 | // Create a new stub function. |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 199 | Stubs[Value] = Section.getStubOffset(); |
| 200 | uint8_t *StubTargetAddr = createStubFunction( |
| 201 | Section.getAddressWithOffset(Section.getStubOffset())); |
| 202 | RelocationEntry StubRE( |
| 203 | RE.SectionID, StubTargetAddr - Section.getAddress(), |
| 204 | MachO::GENERIC_RELOC_VANILLA, Value.Offset, false, 2); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 205 | if (Value.SymbolName) |
| 206 | addRelocationForSymbol(StubRE, Value.SymbolName); |
| 207 | else |
| 208 | addRelocationForSection(StubRE, Value.SectionID); |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 209 | Addr = Section.getAddressWithOffset(Section.getStubOffset()); |
| 210 | Section.advanceStubOffset(getMaxStubSize()); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 211 | } |
Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 212 | RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0, |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 213 | RE.IsPCRel, RE.Size); |
| 214 | resolveRelocation(TargetRE, (uint64_t)Addr); |
| 215 | } |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 216 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 217 | Expected<relocation_iterator> |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 218 | processHALFSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI, |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 219 | const ObjectFile &BaseTObj, |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 220 | ObjSectionToIDMap &ObjSectionToID) { |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 221 | const MachOObjectFile &MachO = |
| 222 | static_cast<const MachOObjectFile&>(BaseTObj); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 223 | MachO::any_relocation_info RE = |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 224 | MachO.getRelocation(RelI->getRawDataRefImpl()); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 225 | |
| 226 | |
| 227 | // For a half-diff relocation the length bits actually record whether this |
| 228 | // is a movw/movt, and whether this is arm or thumb. |
| 229 | // Bit 0 indicates movw (b0 == 0) or movt (b0 == 1). |
| 230 | // Bit 1 indicates arm (b1 == 0) or thumb (b1 == 1). |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 231 | unsigned HalfDiffKindBits = MachO.getAnyRelocationLength(RE); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 232 | if (HalfDiffKindBits & 0x2) |
| 233 | llvm_unreachable("Thumb not yet supported."); |
| 234 | |
| 235 | SectionEntry &Section = Sections[SectionID]; |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 236 | uint32_t RelocType = MachO.getAnyRelocationType(RE); |
| 237 | bool IsPCRel = MachO.getAnyRelocationPCRel(RE); |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 238 | uint64_t Offset = RelI->getOffset(); |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 239 | uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); |
Daniel Sanders | 66e799f | 2014-11-06 09:53:05 +0000 | [diff] [blame] | 240 | int64_t Immediate = readBytesUnaligned(LocalAddress, 4); // Copy the whole instruction out. |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 241 | Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff); |
| 242 | |
| 243 | ++RelI; |
| 244 | MachO::any_relocation_info RE2 = |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 245 | MachO.getRelocation(RelI->getRawDataRefImpl()); |
| 246 | uint32_t AddrA = MachO.getScatteredRelocationValue(RE); |
| 247 | section_iterator SAI = getSectionByAddress(MachO, AddrA); |
| 248 | assert(SAI != MachO.section_end() && "Can't find section for address A"); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 249 | uint64_t SectionABase = SAI->getAddress(); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 250 | uint64_t SectionAOffset = AddrA - SectionABase; |
| 251 | SectionRef SectionA = *SAI; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 252 | bool IsCode = SectionA.isText(); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 253 | uint32_t SectionAID = ~0U; |
| 254 | if (auto SectionAIDOrErr = |
| 255 | findOrEmitSection(MachO, SectionA, IsCode, ObjSectionToID)) |
| 256 | SectionAID = *SectionAIDOrErr; |
| 257 | else |
| 258 | return SectionAIDOrErr.takeError(); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 259 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 260 | uint32_t AddrB = MachO.getScatteredRelocationValue(RE2); |
| 261 | section_iterator SBI = getSectionByAddress(MachO, AddrB); |
| 262 | assert(SBI != MachO.section_end() && "Can't find section for address B"); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 263 | uint64_t SectionBBase = SBI->getAddress(); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 264 | uint64_t SectionBOffset = AddrB - SectionBBase; |
| 265 | SectionRef SectionB = *SBI; |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 266 | uint32_t SectionBID = ~0U; |
| 267 | if (auto SectionBIDOrErr = |
| 268 | findOrEmitSection(MachO, SectionB, IsCode, ObjSectionToID)) |
| 269 | SectionBID = *SectionBIDOrErr; |
| 270 | else |
| 271 | return SectionBIDOrErr.takeError(); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 272 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 273 | uint32_t OtherHalf = MachO.getAnyRelocationAddress(RE2) & 0xffff; |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 274 | unsigned Shift = (HalfDiffKindBits & 0x1) ? 16 : 0; |
| 275 | uint32_t FullImmVal = (Immediate << Shift) | (OtherHalf << (16 - Shift)); |
| 276 | int64_t Addend = FullImmVal - (AddrA - AddrB); |
| 277 | |
| 278 | // addend = Encoded - Expected |
| 279 | // = Encoded - (AddrA - AddrB) |
| 280 | |
| 281 | DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB |
| 282 | << ", Addend: " << Addend << ", SectionA ID: " << SectionAID |
| 283 | << ", SectionAOffset: " << SectionAOffset |
| 284 | << ", SectionB ID: " << SectionBID |
| 285 | << ", SectionBOffset: " << SectionBOffset << "\n"); |
| 286 | RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID, |
| 287 | SectionAOffset, SectionBID, SectionBOffset, IsPCRel, |
| 288 | HalfDiffKindBits); |
| 289 | |
| 290 | addRelocationForSection(R, SectionAID); |
| 291 | addRelocationForSection(R, SectionBID); |
| 292 | |
| 293 | return ++RelI; |
| 294 | } |
| 295 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 296 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 297 | } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 298 | |
| 299 | #undef DEBUG_TYPE |
| 300 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 301 | #endif |