blob: 0a24bb2f5eae982a8ccec5a9c28dfe2b0a9c79f8 [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];
38 uint8_t *LocalAddress = Section.Address + RE.Offset;
39
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 Hamesa5216882014-07-17 18:54:50 +000052 relocation_iterator
53 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);
67 else
68 return ++++RelI;
69 }
Lang Hamesa5216882014-07-17 18:54:50 +000070
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000071 RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
Lang Hames25d93092014-08-08 23:12:22 +000072 RE.Addend = decodeAddend(RE);
Lang Hamesa5216882014-07-17 18:54:50 +000073 RelocationValueRef Value(
Lang Hamesa5cd9502014-11-27 05:40:13 +000074 getRelocationValueRef(Obj, RelI, RE, ObjSectionToID));
Lang Hamesa5216882014-07-17 18:54:50 +000075
Lang Hames13163652014-07-30 03:35:05 +000076 if (RE.IsPCRel)
Rafael Espindola76ad2322015-07-06 14:55:37 +000077 makeValueAddendPCRel(Value, RelI, 8);
Lang Hamesa5216882014-07-17 18:54:50 +000078
79 if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
80 processBranchRelocation(RE, Value, Stubs);
81 else {
Lang Hamesca279c22014-09-07 04:03:32 +000082 RE.Addend = Value.Offset;
Lang Hamesa5216882014-07-17 18:54:50 +000083 if (Value.SymbolName)
84 addRelocationForSymbol(RE, Value.SymbolName);
85 else
86 addRelocationForSection(RE, Value.SectionID);
87 }
88
89 return ++RelI;
90 }
91
Benjamin Kramer8c90fd72014-09-03 11:41:21 +000092 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
Lang Hamesa5216882014-07-17 18:54:50 +000093 DEBUG(dumpRelocationToResolve(RE, Value));
94 const SectionEntry &Section = Sections[RE.SectionID];
95 uint8_t *LocalAddress = Section.Address + RE.Offset;
96
97 // If the relocation is PC-relative, the value to be encoded is the
98 // pointer difference.
99 if (RE.IsPCRel) {
100 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
101 Value -= FinalAddress;
102 // ARM PCRel relocations have an effective-PC offset of two instructions
103 // (four bytes in Thumb mode, 8 bytes in ARM mode).
104 // FIXME: For now, assume ARM mode.
105 Value -= 8;
106 }
107
108 switch (RE.RelType) {
109 default:
110 llvm_unreachable("Invalid relocation type!");
111 case MachO::ARM_RELOC_VANILLA:
Lang Hames4669cd02014-09-11 17:27:01 +0000112 writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
Lang Hamesa5216882014-07-17 18:54:50 +0000113 break;
114 case MachO::ARM_RELOC_BR24: {
115 // Mask the value into the target address. We know instructions are
116 // 32-bit aligned, so we can do it all at once.
Lang Hames4669cd02014-09-11 17:27:01 +0000117 Value += RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +0000118 // The low two bits of the value are not encoded.
119 Value >>= 2;
120 // Mask the value to 24 bits.
121 uint64_t FinalValue = Value & 0xffffff;
Lang Hamesa5216882014-07-17 18:54:50 +0000122 // FIXME: If the destination is a Thumb function (and the instruction
123 // is a non-predicated BL instruction), we need to change it to a BLX
124 // instruction instead.
125
126 // Insert the value into the instruction.
Daniel Sanders66e799f2014-11-06 09:53:05 +0000127 uint32_t Temp = readBytesUnaligned(LocalAddress, 4);
128 writeBytesUnaligned((Temp & ~0xffffff) | FinalValue, LocalAddress, 4);
129
Lang Hamesa5216882014-07-17 18:54:50 +0000130 break;
131 }
Lang Hames6f1048f2014-09-11 19:21:14 +0000132 case MachO::ARM_RELOC_HALF_SECTDIFF: {
133 uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress;
134 uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress;
135 assert((Value == SectionABase || Value == SectionBBase) &&
136 "Unexpected HALFSECTDIFF relocation value.");
137 Value = SectionABase - SectionBBase + RE.Addend;
138 if (RE.Size & 0x1) // :upper16:
139 Value = (Value >> 16);
140 Value &= 0xffff;
141
Daniel Sanders66e799f2014-11-06 09:53:05 +0000142 uint32_t Insn = readBytesUnaligned(LocalAddress, 4);
Lang Hames6f1048f2014-09-11 19:21:14 +0000143 Insn = (Insn & 0xfff0f000) | ((Value & 0xf000) << 4) | (Value & 0x0fff);
Daniel Sanders66e799f2014-11-06 09:53:05 +0000144 writeBytesUnaligned(Insn, LocalAddress, 4);
Lang Hames6f1048f2014-09-11 19:21:14 +0000145 break;
146 }
147
Lang Hamesa5216882014-07-17 18:54:50 +0000148 case MachO::ARM_THUMB_RELOC_BR22:
149 case MachO::ARM_THUMB_32BIT_BRANCH:
150 case MachO::ARM_RELOC_HALF:
Lang Hamesa5216882014-07-17 18:54:50 +0000151 case MachO::ARM_RELOC_PAIR:
152 case MachO::ARM_RELOC_SECTDIFF:
153 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
154 case MachO::ARM_RELOC_PB_LA_PTR:
155 Error("Relocation type not implemented yet!");
156 return;
157 }
158 }
159
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000160 void finalizeSection(const ObjectFile &Obj, unsigned SectionID,
Lang Hames6f1048f2014-09-11 19:21:14 +0000161 const SectionRef &Section) {
162 StringRef Name;
163 Section.getName(Name);
164
165 if (Name == "__nl_symbol_ptr")
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000166 populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
167 Section, SectionID);
Lang Hames6f1048f2014-09-11 19:21:14 +0000168 }
Lang Hamesa5216882014-07-17 18:54:50 +0000169
170private:
Lang Hames6f1048f2014-09-11 19:21:14 +0000171
Lang Hamesa5216882014-07-17 18:54:50 +0000172 void processBranchRelocation(const RelocationEntry &RE,
173 const RelocationValueRef &Value,
174 StubMap &Stubs) {
175 // This is an ARM branch relocation, need to use a stub function.
176 // Look up for existing stub.
177 SectionEntry &Section = Sections[RE.SectionID];
178 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
179 uint8_t *Addr;
180 if (i != Stubs.end()) {
181 Addr = Section.Address + i->second;
182 } else {
183 // Create a new stub function.
184 Stubs[Value] = Section.StubOffset;
185 uint8_t *StubTargetAddr =
186 createStubFunction(Section.Address + Section.StubOffset);
187 RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address,
Lang Hamesca279c22014-09-07 04:03:32 +0000188 MachO::GENERIC_RELOC_VANILLA, Value.Offset, false,
Lang Hames13163652014-07-30 03:35:05 +0000189 2);
Lang Hamesa5216882014-07-17 18:54:50 +0000190 if (Value.SymbolName)
191 addRelocationForSymbol(StubRE, Value.SymbolName);
192 else
193 addRelocationForSection(StubRE, Value.SectionID);
194 Addr = Section.Address + Section.StubOffset;
195 Section.StubOffset += getMaxStubSize();
196 }
Lang Hames13163652014-07-30 03:35:05 +0000197 RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
Lang Hamesa5216882014-07-17 18:54:50 +0000198 RE.IsPCRel, RE.Size);
199 resolveRelocation(TargetRE, (uint64_t)Addr);
200 }
Lang Hames6f1048f2014-09-11 19:21:14 +0000201
202 relocation_iterator
203 processHALFSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000204 const ObjectFile &BaseTObj,
Lang Hames6f1048f2014-09-11 19:21:14 +0000205 ObjSectionToIDMap &ObjSectionToID) {
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000206 const MachOObjectFile &MachO =
207 static_cast<const MachOObjectFile&>(BaseTObj);
Lang Hames6f1048f2014-09-11 19:21:14 +0000208 MachO::any_relocation_info RE =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000209 MachO.getRelocation(RelI->getRawDataRefImpl());
Lang Hames6f1048f2014-09-11 19:21:14 +0000210
211
212 // For a half-diff relocation the length bits actually record whether this
213 // is a movw/movt, and whether this is arm or thumb.
214 // Bit 0 indicates movw (b0 == 0) or movt (b0 == 1).
215 // Bit 1 indicates arm (b1 == 0) or thumb (b1 == 1).
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000216 unsigned HalfDiffKindBits = MachO.getAnyRelocationLength(RE);
Lang Hames6f1048f2014-09-11 19:21:14 +0000217 if (HalfDiffKindBits & 0x2)
218 llvm_unreachable("Thumb not yet supported.");
219
220 SectionEntry &Section = Sections[SectionID];
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000221 uint32_t RelocType = MachO.getAnyRelocationType(RE);
222 bool IsPCRel = MachO.getAnyRelocationPCRel(RE);
Rafael Espindola96d071c2015-06-29 23:29:12 +0000223 uint64_t Offset = RelI->getOffset();
Lang Hames6f1048f2014-09-11 19:21:14 +0000224 uint8_t *LocalAddress = Section.Address + Offset;
Daniel Sanders66e799f2014-11-06 09:53:05 +0000225 int64_t Immediate = readBytesUnaligned(LocalAddress, 4); // Copy the whole instruction out.
Lang Hames6f1048f2014-09-11 19:21:14 +0000226 Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff);
227
228 ++RelI;
229 MachO::any_relocation_info RE2 =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000230 MachO.getRelocation(RelI->getRawDataRefImpl());
231 uint32_t AddrA = MachO.getScatteredRelocationValue(RE);
232 section_iterator SAI = getSectionByAddress(MachO, AddrA);
233 assert(SAI != MachO.section_end() && "Can't find section for address A");
Rafael Espindola80291272014-10-08 15:28:58 +0000234 uint64_t SectionABase = SAI->getAddress();
Lang Hames6f1048f2014-09-11 19:21:14 +0000235 uint64_t SectionAOffset = AddrA - SectionABase;
236 SectionRef SectionA = *SAI;
Rafael Espindola80291272014-10-08 15:28:58 +0000237 bool IsCode = SectionA.isText();
Lang Hames6f1048f2014-09-11 19:21:14 +0000238 uint32_t SectionAID =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000239 findOrEmitSection(MachO, SectionA, IsCode, ObjSectionToID);
Lang Hames6f1048f2014-09-11 19:21:14 +0000240
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000241 uint32_t AddrB = MachO.getScatteredRelocationValue(RE2);
242 section_iterator SBI = getSectionByAddress(MachO, AddrB);
243 assert(SBI != MachO.section_end() && "Can't find section for address B");
Rafael Espindola80291272014-10-08 15:28:58 +0000244 uint64_t SectionBBase = SBI->getAddress();
Lang Hames6f1048f2014-09-11 19:21:14 +0000245 uint64_t SectionBOffset = AddrB - SectionBBase;
246 SectionRef SectionB = *SBI;
247 uint32_t SectionBID =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000248 findOrEmitSection(MachO, SectionB, IsCode, ObjSectionToID);
Lang Hames6f1048f2014-09-11 19:21:14 +0000249
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000250 uint32_t OtherHalf = MachO.getAnyRelocationAddress(RE2) & 0xffff;
Lang Hames6f1048f2014-09-11 19:21:14 +0000251 unsigned Shift = (HalfDiffKindBits & 0x1) ? 16 : 0;
252 uint32_t FullImmVal = (Immediate << Shift) | (OtherHalf << (16 - Shift));
253 int64_t Addend = FullImmVal - (AddrA - AddrB);
254
255 // addend = Encoded - Expected
256 // = Encoded - (AddrA - AddrB)
257
258 DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
259 << ", Addend: " << Addend << ", SectionA ID: " << SectionAID
260 << ", SectionAOffset: " << SectionAOffset
261 << ", SectionB ID: " << SectionBID
262 << ", SectionBOffset: " << SectionBOffset << "\n");
263 RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
264 SectionAOffset, SectionBID, SectionBOffset, IsPCRel,
265 HalfDiffKindBits);
266
267 addRelocationForSection(R, SectionAID);
268 addRelocationForSection(R, SectionBID);
269
270 return ++RelI;
271 }
272
Lang Hamesa5216882014-07-17 18:54:50 +0000273};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000274}
Lang Hamesa5216882014-07-17 18:54:50 +0000275
276#undef DEBUG_TYPE
277
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000278#endif