150b14bfdb4f4551a806cb22c9532ffc86f90c02
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / Targets / RuntimeDyldMachOARM.h
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_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H
11 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_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> {
21 private:
22   typedef RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> ParentT;
23
24 public:
25
26   typedef uint32_t TargetPtrT;
27
28   RuntimeDyldMachOARM(RTDyldMemoryManager *MM) : RuntimeDyldMachOCRTPBase(MM) {}
29
30   unsigned getMaxStubSize() override { return 8; }
31
32   unsigned getStubAlignment() override { return 4; }
33
34   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) {
39       default:
40         return memcpyAddend(RE);
41       case MachO::ARM_RELOC_BR24: {
42         uint32_t Temp;
43         memcpy(&Temp, LocalAddress, 4);
44         Temp &= 0x00ffffff; // Mask out the opcode.
45         // Now we've got the shifted immediate, shift by 2, sign extend and ret.
46         return SignExtend32<26>(Temp << 2);
47       }
48     }
49   }
50
51   relocation_iterator
52   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
53                        ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
54                        const SymbolTableMap &Symbols, StubMap &Stubs) override {
55     const MachOObjectFile &Obj =
56         static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
57     MachO::any_relocation_info RelInfo =
58         Obj.getRelocation(RelI->getRawDataRefImpl());
59     uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
60
61     if (Obj.isRelocationScattered(RelInfo)) {
62       if (RelType == MachO::ARM_RELOC_HALF_SECTDIFF)
63         return processHALFSECTDIFFRelocation(SectionID, RelI, ObjImg,
64                                              ObjSectionToID);
65       else
66         return ++++RelI;
67     }
68
69     RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI));
70     RE.Addend = decodeAddend(RE);
71     RelocationValueRef Value(
72         getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
73
74     if (RE.IsPCRel)
75       makeValueAddendPCRel(Value, ObjImg, RelI, 8);
76
77     if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
78       processBranchRelocation(RE, Value, Stubs);
79     else {
80       RE.Addend = Value.Offset;
81       if (Value.SymbolName)
82         addRelocationForSymbol(RE, Value.SymbolName);
83       else
84         addRelocationForSection(RE, Value.SectionID);
85     }
86
87     return ++RelI;
88   }
89
90   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
91     DEBUG(dumpRelocationToResolve(RE, Value));
92     const SectionEntry &Section = Sections[RE.SectionID];
93     uint8_t *LocalAddress = Section.Address + RE.Offset;
94
95     // If the relocation is PC-relative, the value to be encoded is the
96     // pointer difference.
97     if (RE.IsPCRel) {
98       uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
99       Value -= FinalAddress;
100       // ARM PCRel relocations have an effective-PC offset of two instructions
101       // (four bytes in Thumb mode, 8 bytes in ARM mode).
102       // FIXME: For now, assume ARM mode.
103       Value -= 8;
104     }
105
106     switch (RE.RelType) {
107     default:
108       llvm_unreachable("Invalid relocation type!");
109     case MachO::ARM_RELOC_VANILLA:
110       writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
111       break;
112     case MachO::ARM_RELOC_BR24: {
113       // Mask the value into the target address. We know instructions are
114       // 32-bit aligned, so we can do it all at once.
115       uint32_t *p = (uint32_t *)LocalAddress;
116       Value += RE.Addend;
117       // The low two bits of the value are not encoded.
118       Value >>= 2;
119       // Mask the value to 24 bits.
120       uint64_t FinalValue = Value & 0xffffff;
121       // FIXME: If the destination is a Thumb function (and the instruction
122       // is a non-predicated BL instruction), we need to change it to a BLX
123       // instruction instead.
124
125       // Insert the value into the instruction.
126       *p = (*p & ~0xffffff) | FinalValue;
127       break;
128     }
129     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
139       uint32_t Insn;
140       memcpy(&Insn, LocalAddress, 4);
141       Insn = (Insn & 0xfff0f000) | ((Value & 0xf000) << 4) | (Value & 0x0fff);
142       memcpy(LocalAddress, &Insn, 4);
143       break;
144     }
145
146     case MachO::ARM_THUMB_RELOC_BR22:
147     case MachO::ARM_THUMB_32BIT_BRANCH:
148     case MachO::ARM_RELOC_HALF:
149     case MachO::ARM_RELOC_PAIR:
150     case MachO::ARM_RELOC_SECTDIFF:
151     case MachO::ARM_RELOC_LOCAL_SECTDIFF:
152     case MachO::ARM_RELOC_PB_LA_PTR:
153       Error("Relocation type not implemented yet!");
154       return;
155     }
156   }
157
158   void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
159                        const SectionRef &Section) {
160     StringRef Name;
161     Section.getName(Name);
162
163     if (Name == "__nl_symbol_ptr")
164       populateIndirectSymbolPointersSection(
165                                  cast<MachOObjectFile>(*ObjImg.getObjectFile()),
166                                  Section, SectionID);
167   }
168
169 private:
170
171   void processBranchRelocation(const RelocationEntry &RE,
172                                const RelocationValueRef &Value,
173                                StubMap &Stubs) {
174     // This is an ARM branch relocation, need to use a stub function.
175     // Look up for existing stub.
176     SectionEntry &Section = Sections[RE.SectionID];
177     RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
178     uint8_t *Addr;
179     if (i != Stubs.end()) {
180       Addr = Section.Address + i->second;
181     } else {
182       // Create a new stub function.
183       Stubs[Value] = Section.StubOffset;
184       uint8_t *StubTargetAddr =
185           createStubFunction(Section.Address + Section.StubOffset);
186       RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address,
187                              MachO::GENERIC_RELOC_VANILLA, Value.Offset, false,
188                              2);
189       if (Value.SymbolName)
190         addRelocationForSymbol(StubRE, Value.SymbolName);
191       else
192         addRelocationForSection(StubRE, Value.SectionID);
193       Addr = Section.Address + Section.StubOffset;
194       Section.StubOffset += getMaxStubSize();
195     }
196     RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
197                              RE.IsPCRel, RE.Size);
198     resolveRelocation(TargetRE, (uint64_t)Addr);
199   }
200
201   relocation_iterator
202   processHALFSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
203                                 ObjectImage &Obj,
204                                 ObjSectionToIDMap &ObjSectionToID) {
205     const MachOObjectFile *MachO =
206         static_cast<const MachOObjectFile *>(Obj.getObjectFile());
207     MachO::any_relocation_info RE =
208         MachO->getRelocation(RelI->getRawDataRefImpl());
209
210
211     // For a half-diff relocation the length bits actually record whether this
212     // is a movw/movt, and whether this is arm or thumb.
213     // Bit 0 indicates movw (b0 == 0) or movt (b0 == 1).
214     // Bit 1 indicates arm (b1 == 0) or thumb (b1 == 1).
215     unsigned HalfDiffKindBits = MachO->getAnyRelocationLength(RE);
216     if (HalfDiffKindBits & 0x2)
217       llvm_unreachable("Thumb not yet supported.");
218
219     SectionEntry &Section = Sections[SectionID];
220     uint32_t RelocType = MachO->getAnyRelocationType(RE);
221     bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
222     uint64_t Offset;
223     RelI->getOffset(Offset);
224     uint8_t *LocalAddress = Section.Address + Offset;
225     int64_t Immediate = 0;
226     memcpy(&Immediate, LocalAddress, 4); // Copy the whole instruction out.
227     Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff);
228
229     ++RelI;
230     MachO::any_relocation_info RE2 =
231         MachO->getRelocation(RelI->getRawDataRefImpl());
232     uint32_t AddrA = MachO->getScatteredRelocationValue(RE);
233     section_iterator SAI = getSectionByAddress(*MachO, AddrA);
234     assert(SAI != MachO->section_end() && "Can't find section for address A");
235     uint64_t SectionABase;
236     SAI->getAddress(SectionABase);
237     uint64_t SectionAOffset = AddrA - SectionABase;
238     SectionRef SectionA = *SAI;
239     bool IsCode;
240     SectionA.isText(IsCode);
241     uint32_t SectionAID =
242         findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID);
243
244     uint32_t AddrB = MachO->getScatteredRelocationValue(RE2);
245     section_iterator SBI = getSectionByAddress(*MachO, AddrB);
246     assert(SBI != MachO->section_end() && "Can't find section for address B");
247     uint64_t SectionBBase;
248     SBI->getAddress(SectionBBase);
249     uint64_t SectionBOffset = AddrB - SectionBBase;
250     SectionRef SectionB = *SBI;
251     uint32_t SectionBID =
252         findOrEmitSection(Obj, SectionB, IsCode, ObjSectionToID);
253
254     uint32_t OtherHalf = MachO->getAnyRelocationAddress(RE2) & 0xffff;
255     unsigned Shift = (HalfDiffKindBits & 0x1) ? 16 : 0;
256     uint32_t FullImmVal = (Immediate << Shift) | (OtherHalf << (16 - Shift));
257     int64_t Addend = FullImmVal - (AddrA - AddrB);
258
259     // addend = Encoded - Expected
260     //        = Encoded - (AddrA - AddrB)
261
262     DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
263                  << ", Addend: " << Addend << ", SectionA ID: " << SectionAID
264                  << ", SectionAOffset: " << SectionAOffset
265                  << ", SectionB ID: " << SectionBID
266                  << ", SectionBOffset: " << SectionBOffset << "\n");
267     RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
268                       SectionAOffset, SectionBID, SectionBOffset, IsPCRel,
269                       HalfDiffKindBits);
270
271     addRelocationForSection(R, SectionAID);
272     addRelocationForSection(R, SectionBID);
273
274     return ++RelI;
275   }
276
277 };
278 }
279
280 #undef DEBUG_TYPE
281
282 #endif