blob: b3f4ce44e0989ae95fb8c7b787cc0048a00b6450 [file] [log] [blame]
Lang Hamesa5216882014-07-17 18:54:50 +00001//===----- 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 Kramera7c40ef2014-08-13 16:26:38 +000010#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H
11#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H
Lang Hamesa5216882014-07-17 18:54:50 +000012
13#include "../RuntimeDyldMachO.h"
14
15#define DEBUG_TYPE "dyld"
16
17namespace llvm {
18
19class RuntimeDyldMachOARM
20 : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> {
Lang Hames13163652014-07-30 03:35:05 +000021private:
22 typedef RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> ParentT;
23
Lang Hamesa5216882014-07-17 18:54:50 +000024public:
Lang Hameseb195f02014-09-04 04:53:03 +000025
26 typedef uint32_t TargetPtrT;
27
Lang Hames633fe142015-03-30 03:37:06 +000028 RuntimeDyldMachOARM(RuntimeDyld::MemoryManager &MM,
29 RuntimeDyld::SymbolResolver &Resolver)
30 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
Lang Hamesa5216882014-07-17 18:54:50 +000031
32 unsigned getMaxStubSize() override { return 8; }
33
Lang Hamese5fc8262014-07-17 23:11:30 +000034 unsigned getStubAlignment() override { return 4; }
Lang Hamesa5216882014-07-17 18:54:50 +000035
Lang Hames25d93092014-08-08 23:12:22 +000036 int64_t decodeAddend(const RelocationEntry &RE) const {
37 const SectionEntry &Section = Sections[RE.SectionID];
Sanjoy Das277776a2015-11-23 21:47:41 +000038 uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
Lang Hames25d93092014-08-08 23:12:22 +000039
40 switch (RE.RelType) {
Lang Hames13163652014-07-30 03:35:05 +000041 default:
Lang Hames25d93092014-08-08 23:12:22 +000042 return memcpyAddend(RE);
Lang Hames13163652014-07-30 03:35:05 +000043 case MachO::ARM_RELOC_BR24: {
Daniel Sanders66e799f2014-11-06 09:53:05 +000044 uint32_t Temp = readBytesUnaligned(LocalAddress, 4);
Lang Hames13163652014-07-30 03:35:05 +000045 Temp &= 0x00ffffff; // Mask out the opcode.
46 // Now we've got the shifted immediate, shift by 2, sign extend and ret.
47 return SignExtend32<26>(Temp << 2);
48 }
49 }
50 }
51
Lang Hames89595312016-04-27 20:24:48 +000052 Expected<relocation_iterator>
Lang Hamesa5216882014-07-17 18:54:50 +000053 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000054 const ObjectFile &BaseObjT,
55 ObjSectionToIDMap &ObjSectionToID,
Lang Hamesa5cd9502014-11-27 05:40:13 +000056 StubMap &Stubs) override {
Lang Hamesa5216882014-07-17 18:54:50 +000057 const MachOObjectFile &Obj =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000058 static_cast<const MachOObjectFile &>(BaseObjT);
Lang Hamesa5216882014-07-17 18:54:50 +000059 MachO::any_relocation_info RelInfo =
60 Obj.getRelocation(RelI->getRawDataRefImpl());
Lang Hames6f1048f2014-09-11 19:21:14 +000061 uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
Lang Hamesa5216882014-07-17 18:54:50 +000062
Lang Hames6f1048f2014-09-11 19:21:14 +000063 if (Obj.isRelocationScattered(RelInfo)) {
64 if (RelType == MachO::ARM_RELOC_HALF_SECTDIFF)
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000065 return processHALFSECTDIFFRelocation(SectionID, RelI, Obj,
Lang Hames6f1048f2014-09-11 19:21:14 +000066 ObjSectionToID);
Lang Hamesa8183e52015-07-24 17:40:04 +000067 else if (RelType == MachO::GENERIC_RELOC_VANILLA)
68 return processScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID);
Lang Hames6f1048f2014-09-11 19:21:14 +000069 else
Lang Hamesa8183e52015-07-24 17:40:04 +000070 return ++RelI;
Lang Hames6f1048f2014-09-11 19:21:14 +000071 }
Lang Hamesa5216882014-07-17 18:54:50 +000072
Lang Hames89595312016-04-27 20:24:48 +000073 // Sanity check relocation type.
74 switch (RelType) {
75 UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_PAIR);
76 UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_SECTDIFF);
77 UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_LOCAL_SECTDIFF);
78 UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_PB_LA_PTR);
79 UNIMPLEMENTED_RELOC(MachO::ARM_THUMB_RELOC_BR22);
80 UNIMPLEMENTED_RELOC(MachO::ARM_THUMB_32BIT_BRANCH);
81 UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_HALF);
82 default:
83 if (RelType > MachO::ARM_RELOC_HALF_SECTDIFF)
84 return make_error<RuntimeDyldError>("MachO ARM relocation type " +
85 std::to_string(RelType) +
86 " is out of range");
87 break;
88 }
89
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000090 RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
Lang Hames25d93092014-08-08 23:12:22 +000091 RE.Addend = decodeAddend(RE);
Lang Hames89595312016-04-27 20:24:48 +000092 RelocationValueRef Value;
93 if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID))
94 Value = *ValueOrErr;
95 else
96 return ValueOrErr.takeError();
Lang Hamesa5216882014-07-17 18:54:50 +000097
Lang Hames13163652014-07-30 03:35:05 +000098 if (RE.IsPCRel)
Rafael Espindola76ad2322015-07-06 14:55:37 +000099 makeValueAddendPCRel(Value, RelI, 8);
Lang Hamesa5216882014-07-17 18:54:50 +0000100
101 if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
102 processBranchRelocation(RE, Value, Stubs);
103 else {
Lang Hamesca279c22014-09-07 04:03:32 +0000104 RE.Addend = Value.Offset;
Lang Hamesa5216882014-07-17 18:54:50 +0000105 if (Value.SymbolName)
106 addRelocationForSymbol(RE, Value.SymbolName);
107 else
108 addRelocationForSection(RE, Value.SectionID);
109 }
110
111 return ++RelI;
112 }
113
Benjamin Kramer8c90fd72014-09-03 11:41:21 +0000114 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
Lang Hamesa5216882014-07-17 18:54:50 +0000115 DEBUG(dumpRelocationToResolve(RE, Value));
116 const SectionEntry &Section = Sections[RE.SectionID];
Sanjoy Das277776a2015-11-23 21:47:41 +0000117 uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
Lang Hamesa5216882014-07-17 18:54:50 +0000118
119 // If the relocation is PC-relative, the value to be encoded is the
120 // pointer difference.
121 if (RE.IsPCRel) {
Sanjoy Das277776a2015-11-23 21:47:41 +0000122 uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
Lang Hamesa5216882014-07-17 18:54:50 +0000123 Value -= FinalAddress;
124 // ARM PCRel relocations have an effective-PC offset of two instructions
125 // (four bytes in Thumb mode, 8 bytes in ARM mode).
126 // FIXME: For now, assume ARM mode.
127 Value -= 8;
128 }
129
130 switch (RE.RelType) {
Lang Hamesa5216882014-07-17 18:54:50 +0000131 case MachO::ARM_RELOC_VANILLA:
Lang Hames4669cd02014-09-11 17:27:01 +0000132 writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
Lang Hamesa5216882014-07-17 18:54:50 +0000133 break;
134 case MachO::ARM_RELOC_BR24: {
135 // Mask the value into the target address. We know instructions are
136 // 32-bit aligned, so we can do it all at once.
Lang Hames4669cd02014-09-11 17:27:01 +0000137 Value += RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +0000138 // The low two bits of the value are not encoded.
139 Value >>= 2;
140 // Mask the value to 24 bits.
141 uint64_t FinalValue = Value & 0xffffff;
Lang Hamesa5216882014-07-17 18:54:50 +0000142 // FIXME: If the destination is a Thumb function (and the instruction
143 // is a non-predicated BL instruction), we need to change it to a BLX
144 // instruction instead.
145
146 // Insert the value into the instruction.
Daniel Sanders66e799f2014-11-06 09:53:05 +0000147 uint32_t Temp = readBytesUnaligned(LocalAddress, 4);
148 writeBytesUnaligned((Temp & ~0xffffff) | FinalValue, LocalAddress, 4);
149
Lang Hamesa5216882014-07-17 18:54:50 +0000150 break;
151 }
Lang Hames6f1048f2014-09-11 19:21:14 +0000152 case MachO::ARM_RELOC_HALF_SECTDIFF: {
Sanjoy Das277776a2015-11-23 21:47:41 +0000153 uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
154 uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
Lang Hames6f1048f2014-09-11 19:21:14 +0000155 assert((Value == SectionABase || Value == SectionBBase) &&
156 "Unexpected HALFSECTDIFF relocation value.");
157 Value = SectionABase - SectionBBase + RE.Addend;
158 if (RE.Size & 0x1) // :upper16:
159 Value = (Value >> 16);
160 Value &= 0xffff;
161
Daniel Sanders66e799f2014-11-06 09:53:05 +0000162 uint32_t Insn = readBytesUnaligned(LocalAddress, 4);
Lang Hames6f1048f2014-09-11 19:21:14 +0000163 Insn = (Insn & 0xfff0f000) | ((Value & 0xf000) << 4) | (Value & 0x0fff);
Daniel Sanders66e799f2014-11-06 09:53:05 +0000164 writeBytesUnaligned(Insn, LocalAddress, 4);
Lang Hames6f1048f2014-09-11 19:21:14 +0000165 break;
166 }
167
Lang Hames89595312016-04-27 20:24:48 +0000168 default:
169 llvm_unreachable("Invalid relocation type");
Lang Hamesa5216882014-07-17 18:54:50 +0000170 }
171 }
172
Lang Hames89595312016-04-27 20:24:48 +0000173 Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,
Lang Hames6f1048f2014-09-11 19:21:14 +0000174 const SectionRef &Section) {
175 StringRef Name;
176 Section.getName(Name);
177
178 if (Name == "__nl_symbol_ptr")
Lang Hames89595312016-04-27 20:24:48 +0000179 return populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
180 Section, SectionID);
181 return Error::success();
Lang Hames6f1048f2014-09-11 19:21:14 +0000182 }
Lang Hamesa5216882014-07-17 18:54:50 +0000183
184private:
Lang Hames6f1048f2014-09-11 19:21:14 +0000185
Lang Hamesa5216882014-07-17 18:54:50 +0000186 void processBranchRelocation(const RelocationEntry &RE,
187 const RelocationValueRef &Value,
188 StubMap &Stubs) {
189 // This is an ARM branch relocation, need to use a stub function.
190 // Look up for existing stub.
191 SectionEntry &Section = Sections[RE.SectionID];
192 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
193 uint8_t *Addr;
194 if (i != Stubs.end()) {
Sanjoy Das277776a2015-11-23 21:47:41 +0000195 Addr = Section.getAddressWithOffset(i->second);
Lang Hamesa5216882014-07-17 18:54:50 +0000196 } else {
197 // Create a new stub function.
Sanjoy Das277776a2015-11-23 21:47:41 +0000198 Stubs[Value] = Section.getStubOffset();
199 uint8_t *StubTargetAddr = createStubFunction(
200 Section.getAddressWithOffset(Section.getStubOffset()));
201 RelocationEntry StubRE(
202 RE.SectionID, StubTargetAddr - Section.getAddress(),
203 MachO::GENERIC_RELOC_VANILLA, Value.Offset, false, 2);
Lang Hamesa5216882014-07-17 18:54:50 +0000204 if (Value.SymbolName)
205 addRelocationForSymbol(StubRE, Value.SymbolName);
206 else
207 addRelocationForSection(StubRE, Value.SectionID);
Sanjoy Das277776a2015-11-23 21:47:41 +0000208 Addr = Section.getAddressWithOffset(Section.getStubOffset());
209 Section.advanceStubOffset(getMaxStubSize());
Lang Hamesa5216882014-07-17 18:54:50 +0000210 }
Lang Hames13163652014-07-30 03:35:05 +0000211 RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
Lang Hamesa5216882014-07-17 18:54:50 +0000212 RE.IsPCRel, RE.Size);
213 resolveRelocation(TargetRE, (uint64_t)Addr);
214 }
Lang Hames6f1048f2014-09-11 19:21:14 +0000215
Lang Hames89595312016-04-27 20:24:48 +0000216 Expected<relocation_iterator>
Lang Hames6f1048f2014-09-11 19:21:14 +0000217 processHALFSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000218 const ObjectFile &BaseTObj,
Lang Hames6f1048f2014-09-11 19:21:14 +0000219 ObjSectionToIDMap &ObjSectionToID) {
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000220 const MachOObjectFile &MachO =
221 static_cast<const MachOObjectFile&>(BaseTObj);
Lang Hames6f1048f2014-09-11 19:21:14 +0000222 MachO::any_relocation_info RE =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000223 MachO.getRelocation(RelI->getRawDataRefImpl());
Lang Hames6f1048f2014-09-11 19:21:14 +0000224
225
226 // For a half-diff relocation the length bits actually record whether this
227 // is a movw/movt, and whether this is arm or thumb.
228 // Bit 0 indicates movw (b0 == 0) or movt (b0 == 1).
229 // Bit 1 indicates arm (b1 == 0) or thumb (b1 == 1).
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000230 unsigned HalfDiffKindBits = MachO.getAnyRelocationLength(RE);
Lang Hames6f1048f2014-09-11 19:21:14 +0000231 if (HalfDiffKindBits & 0x2)
232 llvm_unreachable("Thumb not yet supported.");
233
234 SectionEntry &Section = Sections[SectionID];
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000235 uint32_t RelocType = MachO.getAnyRelocationType(RE);
236 bool IsPCRel = MachO.getAnyRelocationPCRel(RE);
Rafael Espindola96d071c2015-06-29 23:29:12 +0000237 uint64_t Offset = RelI->getOffset();
Sanjoy Das277776a2015-11-23 21:47:41 +0000238 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
Daniel Sanders66e799f2014-11-06 09:53:05 +0000239 int64_t Immediate = readBytesUnaligned(LocalAddress, 4); // Copy the whole instruction out.
Lang Hames6f1048f2014-09-11 19:21:14 +0000240 Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff);
241
242 ++RelI;
243 MachO::any_relocation_info RE2 =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000244 MachO.getRelocation(RelI->getRawDataRefImpl());
245 uint32_t AddrA = MachO.getScatteredRelocationValue(RE);
246 section_iterator SAI = getSectionByAddress(MachO, AddrA);
247 assert(SAI != MachO.section_end() && "Can't find section for address A");
Rafael Espindola80291272014-10-08 15:28:58 +0000248 uint64_t SectionABase = SAI->getAddress();
Lang Hames6f1048f2014-09-11 19:21:14 +0000249 uint64_t SectionAOffset = AddrA - SectionABase;
250 SectionRef SectionA = *SAI;
Rafael Espindola80291272014-10-08 15:28:58 +0000251 bool IsCode = SectionA.isText();
Lang Hames89595312016-04-27 20:24:48 +0000252 uint32_t SectionAID = ~0U;
253 if (auto SectionAIDOrErr =
254 findOrEmitSection(MachO, SectionA, IsCode, ObjSectionToID))
255 SectionAID = *SectionAIDOrErr;
256 else
257 return SectionAIDOrErr.takeError();
Lang Hames6f1048f2014-09-11 19:21:14 +0000258
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000259 uint32_t AddrB = MachO.getScatteredRelocationValue(RE2);
260 section_iterator SBI = getSectionByAddress(MachO, AddrB);
261 assert(SBI != MachO.section_end() && "Can't find section for address B");
Rafael Espindola80291272014-10-08 15:28:58 +0000262 uint64_t SectionBBase = SBI->getAddress();
Lang Hames6f1048f2014-09-11 19:21:14 +0000263 uint64_t SectionBOffset = AddrB - SectionBBase;
264 SectionRef SectionB = *SBI;
Lang Hames89595312016-04-27 20:24:48 +0000265 uint32_t SectionBID = ~0U;
266 if (auto SectionBIDOrErr =
267 findOrEmitSection(MachO, SectionB, IsCode, ObjSectionToID))
268 SectionBID = *SectionBIDOrErr;
269 else
270 return SectionBIDOrErr.takeError();
Lang Hames6f1048f2014-09-11 19:21:14 +0000271
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000272 uint32_t OtherHalf = MachO.getAnyRelocationAddress(RE2) & 0xffff;
Lang Hames6f1048f2014-09-11 19:21:14 +0000273 unsigned Shift = (HalfDiffKindBits & 0x1) ? 16 : 0;
274 uint32_t FullImmVal = (Immediate << Shift) | (OtherHalf << (16 - Shift));
275 int64_t Addend = FullImmVal - (AddrA - AddrB);
276
277 // addend = Encoded - Expected
278 // = Encoded - (AddrA - AddrB)
279
280 DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
281 << ", Addend: " << Addend << ", SectionA ID: " << SectionAID
282 << ", SectionAOffset: " << SectionAOffset
283 << ", SectionB ID: " << SectionBID
284 << ", SectionBOffset: " << SectionBOffset << "\n");
285 RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
286 SectionAOffset, SectionBID, SectionBOffset, IsPCRel,
287 HalfDiffKindBits);
288
289 addRelocationForSection(R, SectionAID);
290 addRelocationForSection(R, SectionBID);
291
292 return ++RelI;
293 }
294
Lang Hamesa5216882014-07-17 18:54:50 +0000295};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000296}
Lang Hamesa5216882014-07-17 18:54:50 +0000297
298#undef DEBUG_TYPE
299
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000300#endif