f1b6ebebb507d58779740d7b3acaf133003a5cd5
[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
60     if (Obj.isRelocationScattered(RelInfo))
61       return ++++RelI;
62
63     RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI));
64     RE.Addend = decodeAddend(RE);
65     RelocationValueRef Value(
66         getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
67
68     if (RE.IsPCRel)
69       makeValueAddendPCRel(Value, ObjImg, RelI, 8);
70
71     if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
72       processBranchRelocation(RE, Value, Stubs);
73     else {
74       RE.Addend = Value.Addend;
75       if (Value.SymbolName)
76         addRelocationForSymbol(RE, Value.SymbolName);
77       else
78         addRelocationForSection(RE, Value.SectionID);
79     }
80
81     return ++RelI;
82   }
83
84   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
85     DEBUG(dumpRelocationToResolve(RE, Value));
86     const SectionEntry &Section = Sections[RE.SectionID];
87     uint8_t *LocalAddress = Section.Address + RE.Offset;
88
89     // If the relocation is PC-relative, the value to be encoded is the
90     // pointer difference.
91     if (RE.IsPCRel) {
92       uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
93       Value -= FinalAddress;
94       // ARM PCRel relocations have an effective-PC offset of two instructions
95       // (four bytes in Thumb mode, 8 bytes in ARM mode).
96       // FIXME: For now, assume ARM mode.
97       Value -= 8;
98     }
99
100     switch (RE.RelType) {
101     default:
102       llvm_unreachable("Invalid relocation type!");
103     case MachO::ARM_RELOC_VANILLA:
104       writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
105       break;
106     case MachO::ARM_RELOC_BR24: {
107       // Mask the value into the target address. We know instructions are
108       // 32-bit aligned, so we can do it all at once.
109       uint32_t *p = (uint32_t *)LocalAddress;
110       // The low two bits of the value are not encoded.
111       Value >>= 2;
112       // Mask the value to 24 bits.
113       uint64_t FinalValue = Value & 0xffffff;
114       // FIXME: If the destination is a Thumb function (and the instruction
115       // is a non-predicated BL instruction), we need to change it to a BLX
116       // instruction instead.
117
118       // Insert the value into the instruction.
119       *p = (*p & ~0xffffff) | FinalValue;
120       break;
121     }
122     case MachO::ARM_THUMB_RELOC_BR22:
123     case MachO::ARM_THUMB_32BIT_BRANCH:
124     case MachO::ARM_RELOC_HALF:
125     case MachO::ARM_RELOC_HALF_SECTDIFF:
126     case MachO::ARM_RELOC_PAIR:
127     case MachO::ARM_RELOC_SECTDIFF:
128     case MachO::ARM_RELOC_LOCAL_SECTDIFF:
129     case MachO::ARM_RELOC_PB_LA_PTR:
130       Error("Relocation type not implemented yet!");
131       return;
132     }
133   }
134
135   void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
136                        const SectionRef &Section) {}
137
138 private:
139   void processBranchRelocation(const RelocationEntry &RE,
140                                const RelocationValueRef &Value,
141                                StubMap &Stubs) {
142     // This is an ARM branch relocation, need to use a stub function.
143     // Look up for existing stub.
144     SectionEntry &Section = Sections[RE.SectionID];
145     RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
146     uint8_t *Addr;
147     if (i != Stubs.end()) {
148       Addr = Section.Address + i->second;
149     } else {
150       // Create a new stub function.
151       Stubs[Value] = Section.StubOffset;
152       uint8_t *StubTargetAddr =
153           createStubFunction(Section.Address + Section.StubOffset);
154       RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address,
155                              MachO::GENERIC_RELOC_VANILLA, Value.Addend, false,
156                              2);
157       if (Value.SymbolName)
158         addRelocationForSymbol(StubRE, Value.SymbolName);
159       else
160         addRelocationForSection(StubRE, Value.SectionID);
161       Addr = Section.Address + Section.StubOffset;
162       Section.StubOffset += getMaxStubSize();
163     }
164     RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
165                              RE.IsPCRel, RE.Size);
166     resolveRelocation(TargetRE, (uint64_t)Addr);
167   }
168 };
169 }
170
171 #undef DEBUG_TYPE
172
173 #endif