blob: e29b8dc5091496675cb8090d2ad669daa08f7663 [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: {
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
Lang Hamesa5216882014-07-17 18:54:50 +000051 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
Lang Hames25d93092014-08-08 23:12:22 +000063 RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI));
64 RE.Addend = decodeAddend(RE);
Lang Hamesa5216882014-07-17 18:54:50 +000065 RelocationValueRef Value(
66 getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
67
Lang Hames13163652014-07-30 03:35:05 +000068 if (RE.IsPCRel)
69 makeValueAddendPCRel(Value, ObjImg, RelI, 8);
Lang Hamesa5216882014-07-17 18:54:50 +000070
71 if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
72 processBranchRelocation(RE, Value, Stubs);
73 else {
Lang Hamesca279c22014-09-07 04:03:32 +000074 RE.Addend = Value.Offset;
Lang Hamesa5216882014-07-17 18:54:50 +000075 if (Value.SymbolName)
76 addRelocationForSymbol(RE, Value.SymbolName);
77 else
78 addRelocationForSection(RE, Value.SectionID);
79 }
80
81 return ++RelI;
82 }
83
Benjamin Kramer8c90fd72014-09-03 11:41:21 +000084 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
Lang Hamesa5216882014-07-17 18:54:50 +000085 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:
Lang Hamese1287c02014-08-29 23:17:47 +0000104 writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
Lang Hamesa5216882014-07-17 18:54:50 +0000105 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;
Lang Hamesa5216882014-07-17 18:54:50 +0000114 // 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
138private:
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,
Lang Hamesca279c22014-09-07 04:03:32 +0000155 MachO::GENERIC_RELOC_VANILLA, Value.Offset, false,
Lang Hames13163652014-07-30 03:35:05 +0000156 2);
Lang Hamesa5216882014-07-17 18:54:50 +0000157 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 }
Lang Hames13163652014-07-30 03:35:05 +0000164 RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
Lang Hamesa5216882014-07-17 18:54:50 +0000165 RE.IsPCRel, RE.Size);
166 resolveRelocation(TargetRE, (uint64_t)Addr);
167 }
168};
169}
170
171#undef DEBUG_TYPE
172
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000173#endif