blob: 9766751c43dcf6e084389ce8ddfd155ada7c9a97 [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 Hamesa5216882014-07-17 18:54:50 +000028 RuntimeDyldMachOARM(RTDyldMemoryManager *MM) : RuntimeDyldMachOCRTPBase(MM) {}
29
30 unsigned getMaxStubSize() override { return 8; }
31
Lang Hamese5fc8262014-07-17 23:11:30 +000032 unsigned getStubAlignment() override { return 4; }
Lang Hamesa5216882014-07-17 18:54:50 +000033
Lang Hames25d93092014-08-08 23:12:22 +000034 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 Hames13163652014-07-30 03:35:05 +000039 default:
Lang Hames25d93092014-08-08 23:12:22 +000040 return memcpyAddend(RE);
Lang Hames13163652014-07-30 03:35:05 +000041 case MachO::ARM_RELOC_BR24: {
Daniel Sanders66e799f2014-11-06 09:53:05 +000042 uint32_t Temp = readBytesUnaligned(LocalAddress, 4);
Lang Hames13163652014-07-30 03:35:05 +000043 Temp &= 0x00ffffff; // Mask out the opcode.
44 // Now we've got the shifted immediate, shift by 2, sign extend and ret.
45 return SignExtend32<26>(Temp << 2);
46 }
47 }
48 }
49
Lang Hamesa5216882014-07-17 18:54:50 +000050 relocation_iterator
51 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
Aaron Ballman9fb41142014-11-26 15:27:39 +000052 ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
Lang Hamesa5216882014-07-17 18:54:50 +000053 const SymbolTableMap &Symbols, StubMap &Stubs) override {
54 const MachOObjectFile &Obj =
Aaron Ballman9fb41142014-11-26 15:27:39 +000055 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
Lang Hamesa5216882014-07-17 18:54:50 +000056 MachO::any_relocation_info RelInfo =
57 Obj.getRelocation(RelI->getRawDataRefImpl());
Lang Hames6f1048f2014-09-11 19:21:14 +000058 uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
Lang Hamesa5216882014-07-17 18:54:50 +000059
Lang Hames6f1048f2014-09-11 19:21:14 +000060 if (Obj.isRelocationScattered(RelInfo)) {
61 if (RelType == MachO::ARM_RELOC_HALF_SECTDIFF)
Aaron Ballman9fb41142014-11-26 15:27:39 +000062 return processHALFSECTDIFFRelocation(SectionID, RelI, ObjImg,
Lang Hames6f1048f2014-09-11 19:21:14 +000063 ObjSectionToID);
64 else
65 return ++++RelI;
66 }
Lang Hamesa5216882014-07-17 18:54:50 +000067
Aaron Ballman9fb41142014-11-26 15:27:39 +000068 RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI));
Lang Hames25d93092014-08-08 23:12:22 +000069 RE.Addend = decodeAddend(RE);
Lang Hamesa5216882014-07-17 18:54:50 +000070 RelocationValueRef Value(
Aaron Ballman9fb41142014-11-26 15:27:39 +000071 getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
Lang Hamesa5216882014-07-17 18:54:50 +000072
Lang Hames13163652014-07-30 03:35:05 +000073 if (RE.IsPCRel)
Aaron Ballman9fb41142014-11-26 15:27:39 +000074 makeValueAddendPCRel(Value, ObjImg, RelI, 8);
Lang Hamesa5216882014-07-17 18:54:50 +000075
76 if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
77 processBranchRelocation(RE, Value, Stubs);
78 else {
Lang Hamesca279c22014-09-07 04:03:32 +000079 RE.Addend = Value.Offset;
Lang Hamesa5216882014-07-17 18:54:50 +000080 if (Value.SymbolName)
81 addRelocationForSymbol(RE, Value.SymbolName);
82 else
83 addRelocationForSection(RE, Value.SectionID);
84 }
85
86 return ++RelI;
87 }
88
Benjamin Kramer8c90fd72014-09-03 11:41:21 +000089 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
Lang Hamesa5216882014-07-17 18:54:50 +000090 DEBUG(dumpRelocationToResolve(RE, Value));
91 const SectionEntry &Section = Sections[RE.SectionID];
92 uint8_t *LocalAddress = Section.Address + RE.Offset;
93
94 // If the relocation is PC-relative, the value to be encoded is the
95 // pointer difference.
96 if (RE.IsPCRel) {
97 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
98 Value -= FinalAddress;
99 // ARM PCRel relocations have an effective-PC offset of two instructions
100 // (four bytes in Thumb mode, 8 bytes in ARM mode).
101 // FIXME: For now, assume ARM mode.
102 Value -= 8;
103 }
104
105 switch (RE.RelType) {
106 default:
107 llvm_unreachable("Invalid relocation type!");
108 case MachO::ARM_RELOC_VANILLA:
Lang Hames4669cd02014-09-11 17:27:01 +0000109 writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
Lang Hamesa5216882014-07-17 18:54:50 +0000110 break;
111 case MachO::ARM_RELOC_BR24: {
112 // Mask the value into the target address. We know instructions are
113 // 32-bit aligned, so we can do it all at once.
Lang Hames4669cd02014-09-11 17:27:01 +0000114 Value += RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +0000115 // The low two bits of the value are not encoded.
116 Value >>= 2;
117 // Mask the value to 24 bits.
118 uint64_t FinalValue = Value & 0xffffff;
Lang Hamesa5216882014-07-17 18:54:50 +0000119 // FIXME: If the destination is a Thumb function (and the instruction
120 // is a non-predicated BL instruction), we need to change it to a BLX
121 // instruction instead.
122
123 // Insert the value into the instruction.
Daniel Sanders66e799f2014-11-06 09:53:05 +0000124 uint32_t Temp = readBytesUnaligned(LocalAddress, 4);
125 writeBytesUnaligned((Temp & ~0xffffff) | FinalValue, LocalAddress, 4);
126
Lang Hamesa5216882014-07-17 18:54:50 +0000127 break;
128 }
Lang Hames6f1048f2014-09-11 19:21:14 +0000129 case MachO::ARM_RELOC_HALF_SECTDIFF: {
130 uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress;
131 uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress;
132 assert((Value == SectionABase || Value == SectionBBase) &&
133 "Unexpected HALFSECTDIFF relocation value.");
134 Value = SectionABase - SectionBBase + RE.Addend;
135 if (RE.Size & 0x1) // :upper16:
136 Value = (Value >> 16);
137 Value &= 0xffff;
138
Daniel Sanders66e799f2014-11-06 09:53:05 +0000139 uint32_t Insn = readBytesUnaligned(LocalAddress, 4);
Lang Hames6f1048f2014-09-11 19:21:14 +0000140 Insn = (Insn & 0xfff0f000) | ((Value & 0xf000) << 4) | (Value & 0x0fff);
Daniel Sanders66e799f2014-11-06 09:53:05 +0000141 writeBytesUnaligned(Insn, LocalAddress, 4);
Lang Hames6f1048f2014-09-11 19:21:14 +0000142 break;
143 }
144
Lang Hamesa5216882014-07-17 18:54:50 +0000145 case MachO::ARM_THUMB_RELOC_BR22:
146 case MachO::ARM_THUMB_32BIT_BRANCH:
147 case MachO::ARM_RELOC_HALF:
Lang Hamesa5216882014-07-17 18:54:50 +0000148 case MachO::ARM_RELOC_PAIR:
149 case MachO::ARM_RELOC_SECTDIFF:
150 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
151 case MachO::ARM_RELOC_PB_LA_PTR:
152 Error("Relocation type not implemented yet!");
153 return;
154 }
155 }
156
Aaron Ballman9fb41142014-11-26 15:27:39 +0000157 void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
Lang Hames6f1048f2014-09-11 19:21:14 +0000158 const SectionRef &Section) {
159 StringRef Name;
160 Section.getName(Name);
161
162 if (Name == "__nl_symbol_ptr")
Aaron Ballman9fb41142014-11-26 15:27:39 +0000163 populateIndirectSymbolPointersSection(
164 cast<MachOObjectFile>(*ObjImg.getObjectFile()),
165 Section, SectionID);
Lang Hames6f1048f2014-09-11 19:21:14 +0000166 }
Lang Hamesa5216882014-07-17 18:54:50 +0000167
168private:
Lang Hames6f1048f2014-09-11 19:21:14 +0000169
Lang Hamesa5216882014-07-17 18:54:50 +0000170 void processBranchRelocation(const RelocationEntry &RE,
171 const RelocationValueRef &Value,
172 StubMap &Stubs) {
173 // This is an ARM branch relocation, need to use a stub function.
174 // Look up for existing stub.
175 SectionEntry &Section = Sections[RE.SectionID];
176 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
177 uint8_t *Addr;
178 if (i != Stubs.end()) {
179 Addr = Section.Address + i->second;
180 } else {
181 // Create a new stub function.
182 Stubs[Value] = Section.StubOffset;
183 uint8_t *StubTargetAddr =
184 createStubFunction(Section.Address + Section.StubOffset);
185 RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address,
Lang Hamesca279c22014-09-07 04:03:32 +0000186 MachO::GENERIC_RELOC_VANILLA, Value.Offset, false,
Lang Hames13163652014-07-30 03:35:05 +0000187 2);
Lang Hamesa5216882014-07-17 18:54:50 +0000188 if (Value.SymbolName)
189 addRelocationForSymbol(StubRE, Value.SymbolName);
190 else
191 addRelocationForSection(StubRE, Value.SectionID);
192 Addr = Section.Address + Section.StubOffset;
193 Section.StubOffset += getMaxStubSize();
194 }
Lang Hames13163652014-07-30 03:35:05 +0000195 RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
Lang Hamesa5216882014-07-17 18:54:50 +0000196 RE.IsPCRel, RE.Size);
197 resolveRelocation(TargetRE, (uint64_t)Addr);
198 }
Lang Hames6f1048f2014-09-11 19:21:14 +0000199
200 relocation_iterator
201 processHALFSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
Aaron Ballman9fb41142014-11-26 15:27:39 +0000202 ObjectImage &Obj,
Lang Hames6f1048f2014-09-11 19:21:14 +0000203 ObjSectionToIDMap &ObjSectionToID) {
Aaron Ballman9fb41142014-11-26 15:27:39 +0000204 const MachOObjectFile *MachO =
205 static_cast<const MachOObjectFile *>(Obj.getObjectFile());
Lang Hames6f1048f2014-09-11 19:21:14 +0000206 MachO::any_relocation_info RE =
Aaron Ballman9fb41142014-11-26 15:27:39 +0000207 MachO->getRelocation(RelI->getRawDataRefImpl());
Lang Hames6f1048f2014-09-11 19:21:14 +0000208
209
210 // For a half-diff relocation the length bits actually record whether this
211 // is a movw/movt, and whether this is arm or thumb.
212 // Bit 0 indicates movw (b0 == 0) or movt (b0 == 1).
213 // Bit 1 indicates arm (b1 == 0) or thumb (b1 == 1).
Aaron Ballman9fb41142014-11-26 15:27:39 +0000214 unsigned HalfDiffKindBits = MachO->getAnyRelocationLength(RE);
Lang Hames6f1048f2014-09-11 19:21:14 +0000215 if (HalfDiffKindBits & 0x2)
216 llvm_unreachable("Thumb not yet supported.");
217
218 SectionEntry &Section = Sections[SectionID];
Aaron Ballman9fb41142014-11-26 15:27:39 +0000219 uint32_t RelocType = MachO->getAnyRelocationType(RE);
220 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
Lang Hames6f1048f2014-09-11 19:21:14 +0000221 uint64_t Offset;
222 RelI->getOffset(Offset);
223 uint8_t *LocalAddress = Section.Address + Offset;
Daniel Sanders66e799f2014-11-06 09:53:05 +0000224 int64_t Immediate = readBytesUnaligned(LocalAddress, 4); // Copy the whole instruction out.
Lang Hames6f1048f2014-09-11 19:21:14 +0000225 Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff);
226
227 ++RelI;
228 MachO::any_relocation_info RE2 =
Aaron Ballman9fb41142014-11-26 15:27:39 +0000229 MachO->getRelocation(RelI->getRawDataRefImpl());
230 uint32_t AddrA = MachO->getScatteredRelocationValue(RE);
231 section_iterator SAI = getSectionByAddress(*MachO, AddrA);
232 assert(SAI != MachO->section_end() && "Can't find section for address A");
Rafael Espindola80291272014-10-08 15:28:58 +0000233 uint64_t SectionABase = SAI->getAddress();
Lang Hames6f1048f2014-09-11 19:21:14 +0000234 uint64_t SectionAOffset = AddrA - SectionABase;
235 SectionRef SectionA = *SAI;
Rafael Espindola80291272014-10-08 15:28:58 +0000236 bool IsCode = SectionA.isText();
Lang Hames6f1048f2014-09-11 19:21:14 +0000237 uint32_t SectionAID =
Aaron Ballman9fb41142014-11-26 15:27:39 +0000238 findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID);
Lang Hames6f1048f2014-09-11 19:21:14 +0000239
Aaron Ballman9fb41142014-11-26 15:27:39 +0000240 uint32_t AddrB = MachO->getScatteredRelocationValue(RE2);
241 section_iterator SBI = getSectionByAddress(*MachO, AddrB);
242 assert(SBI != MachO->section_end() && "Can't find section for address B");
Rafael Espindola80291272014-10-08 15:28:58 +0000243 uint64_t SectionBBase = SBI->getAddress();
Lang Hames6f1048f2014-09-11 19:21:14 +0000244 uint64_t SectionBOffset = AddrB - SectionBBase;
245 SectionRef SectionB = *SBI;
246 uint32_t SectionBID =
Aaron Ballman9fb41142014-11-26 15:27:39 +0000247 findOrEmitSection(Obj, SectionB, IsCode, ObjSectionToID);
Lang Hames6f1048f2014-09-11 19:21:14 +0000248
Aaron Ballman9fb41142014-11-26 15:27:39 +0000249 uint32_t OtherHalf = MachO->getAnyRelocationAddress(RE2) & 0xffff;
Lang Hames6f1048f2014-09-11 19:21:14 +0000250 unsigned Shift = (HalfDiffKindBits & 0x1) ? 16 : 0;
251 uint32_t FullImmVal = (Immediate << Shift) | (OtherHalf << (16 - Shift));
252 int64_t Addend = FullImmVal - (AddrA - AddrB);
253
254 // addend = Encoded - Expected
255 // = Encoded - (AddrA - AddrB)
256
257 DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
258 << ", Addend: " << Addend << ", SectionA ID: " << SectionAID
259 << ", SectionAOffset: " << SectionAOffset
260 << ", SectionB ID: " << SectionBID
261 << ", SectionBOffset: " << SectionBOffset << "\n");
262 RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
263 SectionAOffset, SectionBID, SectionBOffset, IsPCRel,
264 HalfDiffKindBits);
265
266 addRelocationForSection(R, SectionAID);
267 addRelocationForSection(R, SectionBID);
268
269 return ++RelI;
270 }
271
Lang Hamesa5216882014-07-17 18:54:50 +0000272};
273}
274
275#undef DEBUG_TYPE
276
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000277#endif