blob: dc8acfef5b7dc666b4b5f44f629f371895a495fc [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 Hames4669cd02014-09-11 17:27:01 +0000104 writeBytesUnaligned(Value + RE.Addend, 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;
Lang Hames4669cd02014-09-11 17:27:01 +0000110 Value += RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +0000111 // The low two bits of the value are not encoded.
112 Value >>= 2;
113 // Mask the value to 24 bits.
114 uint64_t FinalValue = Value & 0xffffff;
Lang Hamesa5216882014-07-17 18:54:50 +0000115 // FIXME: If the destination is a Thumb function (and the instruction
116 // is a non-predicated BL instruction), we need to change it to a BLX
117 // instruction instead.
118
119 // Insert the value into the instruction.
120 *p = (*p & ~0xffffff) | FinalValue;
121 break;
122 }
123 case MachO::ARM_THUMB_RELOC_BR22:
124 case MachO::ARM_THUMB_32BIT_BRANCH:
125 case MachO::ARM_RELOC_HALF:
126 case MachO::ARM_RELOC_HALF_SECTDIFF:
127 case MachO::ARM_RELOC_PAIR:
128 case MachO::ARM_RELOC_SECTDIFF:
129 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
130 case MachO::ARM_RELOC_PB_LA_PTR:
131 Error("Relocation type not implemented yet!");
132 return;
133 }
134 }
135
136 void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
137 const SectionRef &Section) {}
138
139private:
140 void processBranchRelocation(const RelocationEntry &RE,
141 const RelocationValueRef &Value,
142 StubMap &Stubs) {
143 // This is an ARM branch relocation, need to use a stub function.
144 // Look up for existing stub.
145 SectionEntry &Section = Sections[RE.SectionID];
146 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
147 uint8_t *Addr;
148 if (i != Stubs.end()) {
149 Addr = Section.Address + i->second;
150 } else {
151 // Create a new stub function.
152 Stubs[Value] = Section.StubOffset;
153 uint8_t *StubTargetAddr =
154 createStubFunction(Section.Address + Section.StubOffset);
155 RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address,
Lang Hamesca279c22014-09-07 04:03:32 +0000156 MachO::GENERIC_RELOC_VANILLA, Value.Offset, false,
Lang Hames13163652014-07-30 03:35:05 +0000157 2);
Lang Hamesa5216882014-07-17 18:54:50 +0000158 if (Value.SymbolName)
159 addRelocationForSymbol(StubRE, Value.SymbolName);
160 else
161 addRelocationForSection(StubRE, Value.SectionID);
162 Addr = Section.Address + Section.StubOffset;
163 Section.StubOffset += getMaxStubSize();
164 }
Lang Hames13163652014-07-30 03:35:05 +0000165 RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
Lang Hamesa5216882014-07-17 18:54:50 +0000166 RE.IsPCRel, RE.Size);
167 resolveRelocation(TargetRE, (uint64_t)Addr);
168 }
169};
170}
171
172#undef DEBUG_TYPE
173
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000174#endif