blob: 1de9942198249a695bcb623bff21bbab03b8b183 [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
10#ifndef LLVM_RUNTIMEDYLDMACHOARM_H
11#define LLVM_RUNTIMEDYLDMACHOARM_H
12
13#include "../RuntimeDyldMachO.h"
14
15#define DEBUG_TYPE "dyld"
16
17namespace llvm {
18
19class RuntimeDyldMachOARM
20 : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> {
21public:
22 RuntimeDyldMachOARM(RTDyldMemoryManager *MM) : RuntimeDyldMachOCRTPBase(MM) {}
23
24 unsigned getMaxStubSize() override { return 8; }
25
Lang Hamese5fc8262014-07-17 23:11:30 +000026 unsigned getStubAlignment() override { return 4; }
Lang Hamesa5216882014-07-17 18:54:50 +000027
28 relocation_iterator
29 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
30 ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
31 const SymbolTableMap &Symbols, StubMap &Stubs) override {
32 const MachOObjectFile &Obj =
33 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
34 MachO::any_relocation_info RelInfo =
35 Obj.getRelocation(RelI->getRawDataRefImpl());
36
37 if (Obj.isRelocationScattered(RelInfo))
38 return ++++RelI;
39
40 RelocationEntry RE(getBasicRelocationEntry(SectionID, ObjImg, RelI));
41 RelocationValueRef Value(
42 getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
43
44 bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
45 if (!IsExtern && RE.IsPCRel)
46 makeValueAddendPCRel(Value, ObjImg, RelI);
47
48 if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
49 processBranchRelocation(RE, Value, Stubs);
50 else {
51 RE.Addend = Value.Addend;
52 if (Value.SymbolName)
53 addRelocationForSymbol(RE, Value.SymbolName);
54 else
55 addRelocationForSection(RE, Value.SectionID);
56 }
57
58 return ++RelI;
59 }
60
61 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) {
62 DEBUG(dumpRelocationToResolve(RE, Value));
63 const SectionEntry &Section = Sections[RE.SectionID];
64 uint8_t *LocalAddress = Section.Address + RE.Offset;
65
66 // If the relocation is PC-relative, the value to be encoded is the
67 // pointer difference.
68 if (RE.IsPCRel) {
69 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
70 Value -= FinalAddress;
71 // ARM PCRel relocations have an effective-PC offset of two instructions
72 // (four bytes in Thumb mode, 8 bytes in ARM mode).
73 // FIXME: For now, assume ARM mode.
74 Value -= 8;
75 }
76
77 switch (RE.RelType) {
78 default:
79 llvm_unreachable("Invalid relocation type!");
80 case MachO::ARM_RELOC_VANILLA:
81 writeBytesUnaligned(LocalAddress, Value, 1 << RE.Size);
82 break;
83 case MachO::ARM_RELOC_BR24: {
84 // Mask the value into the target address. We know instructions are
85 // 32-bit aligned, so we can do it all at once.
86 uint32_t *p = (uint32_t *)LocalAddress;
87 // The low two bits of the value are not encoded.
88 Value >>= 2;
89 // Mask the value to 24 bits.
90 uint64_t FinalValue = Value & 0xffffff;
91 // Check for overflow.
92 if (Value != FinalValue) {
93 Error("ARM BR24 relocation out of range.");
94 return;
95 }
96 // FIXME: If the destination is a Thumb function (and the instruction
97 // is a non-predicated BL instruction), we need to change it to a BLX
98 // instruction instead.
99
100 // Insert the value into the instruction.
101 *p = (*p & ~0xffffff) | FinalValue;
102 break;
103 }
104 case MachO::ARM_THUMB_RELOC_BR22:
105 case MachO::ARM_THUMB_32BIT_BRANCH:
106 case MachO::ARM_RELOC_HALF:
107 case MachO::ARM_RELOC_HALF_SECTDIFF:
108 case MachO::ARM_RELOC_PAIR:
109 case MachO::ARM_RELOC_SECTDIFF:
110 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
111 case MachO::ARM_RELOC_PB_LA_PTR:
112 Error("Relocation type not implemented yet!");
113 return;
114 }
115 }
116
117 void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
118 const SectionRef &Section) {}
119
120private:
121 void processBranchRelocation(const RelocationEntry &RE,
122 const RelocationValueRef &Value,
123 StubMap &Stubs) {
124 // This is an ARM branch relocation, need to use a stub function.
125 // Look up for existing stub.
126 SectionEntry &Section = Sections[RE.SectionID];
127 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
128 uint8_t *Addr;
129 if (i != Stubs.end()) {
130 Addr = Section.Address + i->second;
131 } else {
132 // Create a new stub function.
133 Stubs[Value] = Section.StubOffset;
134 uint8_t *StubTargetAddr =
135 createStubFunction(Section.Address + Section.StubOffset);
136 RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address,
137 MachO::GENERIC_RELOC_VANILLA, Value.Addend);
138 if (Value.SymbolName)
139 addRelocationForSymbol(StubRE, Value.SymbolName);
140 else
141 addRelocationForSection(StubRE, Value.SectionID);
142 Addr = Section.Address + Section.StubOffset;
143 Section.StubOffset += getMaxStubSize();
144 }
145 RelocationEntry TargetRE(Value.SectionID, RE.Offset, RE.RelType, 0,
146 RE.IsPCRel, RE.Size);
147 resolveRelocation(TargetRE, (uint64_t)Addr);
148 }
149};
150}
151
152#undef DEBUG_TYPE
153
154#endif // LLVM_RUNTIMEDYLDMACHOARM_H