blob: bcc3df1b4e7c3e9a4bb0babe35ebe58f3ad066db [file] [log] [blame]
Jim Grosbache0934be2012-01-16 23:50:58 +00001//===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
Danil Malyshevcf852dc2011-07-13 07:57:58 +00002//
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// Implementation of the MC-JIT runtime dynamic linker.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dyld"
Eli Bendersky76463fd2012-01-22 07:05:02 +000015#include "RuntimeDyldMachO.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/OwningPtr.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000019using namespace llvm;
20using namespace llvm::object;
21
22namespace llvm {
23
Andrew Kaylora307a1c2012-11-02 19:45:23 +000024void RuntimeDyldMachO::resolveRelocation(const SectionEntry &Section,
25 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000026 uint64_t Value,
27 uint32_t Type,
28 int64_t Addend) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +000029 uint8_t *LocalAddress = Section.Address + Offset;
30 uint64_t FinalAddress = Section.LoadAddress + Offset;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000031 bool isPCRel = (Type >> 24) & 1;
32 unsigned MachoType = (Type >> 28) & 0xf;
33 unsigned Size = 1 << ((Type >> 25) & 3);
34
Eli Bendersky6d15e872012-04-30 10:06:27 +000035 DEBUG(dbgs() << "resolveRelocation LocalAddress: "
36 << format("%p", LocalAddress)
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000037 << " FinalAddress: " << format("%p", FinalAddress)
38 << " Value: " << format("%p", Value)
39 << " Addend: " << Addend
40 << " isPCRel: " << isPCRel
41 << " MachoType: " << MachoType
42 << " Size: " << Size
43 << "\n");
44
Danil Malyshevcf852dc2011-07-13 07:57:58 +000045 // This just dispatches to the proper target specific routine.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000046 switch (Arch) {
Craig Topper85814382012-02-07 05:05:23 +000047 default: llvm_unreachable("Unsupported CPU type!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000048 case Triple::x86_64:
49 resolveX86_64Relocation(LocalAddress,
50 FinalAddress,
51 (uintptr_t)Value,
52 isPCRel,
53 MachoType,
54 Size,
55 Addend);
56 break;
57 case Triple::x86:
58 resolveI386Relocation(LocalAddress,
Eli Bendersky6d15e872012-04-30 10:06:27 +000059 FinalAddress,
60 (uintptr_t)Value,
61 isPCRel,
Jim Grosbachba9ba9f2012-09-13 01:24:32 +000062 MachoType,
Eli Bendersky6d15e872012-04-30 10:06:27 +000063 Size,
64 Addend);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000065 break;
66 case Triple::arm: // Fall through.
67 case Triple::thumb:
68 resolveARMRelocation(LocalAddress,
69 FinalAddress,
70 (uintptr_t)Value,
71 isPCRel,
72 MachoType,
73 Size,
74 Addend);
75 break;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000076 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +000077}
78
Eli Bendersky6d15e872012-04-30 10:06:27 +000079bool RuntimeDyldMachO::resolveI386Relocation(uint8_t *LocalAddress,
80 uint64_t FinalAddress,
81 uint64_t Value,
82 bool isPCRel,
83 unsigned Type,
84 unsigned Size,
85 int64_t Addend) {
Sean Callananb38aae42012-03-26 20:45:52 +000086 if (isPCRel)
87 Value -= FinalAddress + 4; // see resolveX86_64Relocation
88
89 switch (Type) {
90 default:
91 llvm_unreachable("Invalid relocation type!");
92 case macho::RIT_Vanilla: {
93 uint8_t *p = LocalAddress;
94 uint64_t ValueToWrite = Value + Addend;
95 for (unsigned i = 0; i < Size; ++i) {
96 *p++ = (uint8_t)(ValueToWrite & 0xff);
97 ValueToWrite >>= 8;
98 }
Jim Grosbach6f6f1712013-01-31 19:46:28 +000099 return false;
Sean Callananb38aae42012-03-26 20:45:52 +0000100 }
101 case macho::RIT_Difference:
102 case macho::RIT_Generic_LocalDifference:
103 case macho::RIT_Generic_PreboundLazyPointer:
104 return Error("Relocation type not implemented yet!");
105 }
106}
107
Eli Bendersky6d15e872012-04-30 10:06:27 +0000108bool RuntimeDyldMachO::resolveX86_64Relocation(uint8_t *LocalAddress,
109 uint64_t FinalAddress,
110 uint64_t Value,
111 bool isPCRel,
112 unsigned Type,
113 unsigned Size,
114 int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000115 // If the relocation is PC-relative, the value to be encoded is the
116 // pointer difference.
117 if (isPCRel)
118 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
119 // address. Is that expected? Only for branches, perhaps?
Sean Callanan61dfa772012-03-07 23:05:25 +0000120 Value -= FinalAddress + 4;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000121
122 switch(Type) {
123 default:
124 llvm_unreachable("Invalid relocation type!");
Jim Grosbach652ca2f2012-01-16 23:50:49 +0000125 case macho::RIT_X86_64_Signed1:
126 case macho::RIT_X86_64_Signed2:
127 case macho::RIT_X86_64_Signed4:
128 case macho::RIT_X86_64_Signed:
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000129 case macho::RIT_X86_64_Unsigned:
130 case macho::RIT_X86_64_Branch: {
Jim Grosbach652ca2f2012-01-16 23:50:49 +0000131 Value += Addend;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000132 // Mask in the target value a byte at a time (we don't have an alignment
133 // guarantee for the target address, so this is safest).
Sean Callanan61dfa772012-03-07 23:05:25 +0000134 uint8_t *p = (uint8_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000135 for (unsigned i = 0; i < Size; ++i) {
136 *p++ = (uint8_t)Value;
137 Value >>= 8;
138 }
139 return false;
140 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000141 case macho::RIT_X86_64_GOTLoad:
142 case macho::RIT_X86_64_GOT:
143 case macho::RIT_X86_64_Subtractor:
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000144 case macho::RIT_X86_64_TLV:
145 return Error("Relocation type not implemented yet!");
146 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000147}
148
Eli Bendersky6d15e872012-04-30 10:06:27 +0000149bool RuntimeDyldMachO::resolveARMRelocation(uint8_t *LocalAddress,
150 uint64_t FinalAddress,
151 uint64_t Value,
152 bool isPCRel,
153 unsigned Type,
154 unsigned Size,
155 int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000156 // If the relocation is PC-relative, the value to be encoded is the
157 // pointer difference.
158 if (isPCRel) {
Sean Callanan61dfa772012-03-07 23:05:25 +0000159 Value -= FinalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000160 // ARM PCRel relocations have an effective-PC offset of two instructions
161 // (four bytes in Thumb mode, 8 bytes in ARM mode).
162 // FIXME: For now, assume ARM mode.
163 Value -= 8;
164 }
165
166 switch(Type) {
167 default:
168 llvm_unreachable("Invalid relocation type!");
169 case macho::RIT_Vanilla: {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000170 // Mask in the target value a byte at a time (we don't have an alignment
171 // guarantee for the target address, so this is safest).
Sean Callanan61dfa772012-03-07 23:05:25 +0000172 uint8_t *p = (uint8_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000173 for (unsigned i = 0; i < Size; ++i) {
174 *p++ = (uint8_t)Value;
175 Value >>= 8;
176 }
177 break;
178 }
179 case macho::RIT_ARM_Branch24Bit: {
180 // Mask the value into the target address. We know instructions are
181 // 32-bit aligned, so we can do it all at once.
Sean Callanan61dfa772012-03-07 23:05:25 +0000182 uint32_t *p = (uint32_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000183 // The low two bits of the value are not encoded.
184 Value >>= 2;
185 // Mask the value to 24 bits.
186 Value &= 0xffffff;
187 // FIXME: If the destination is a Thumb function (and the instruction
188 // is a non-predicated BL instruction), we need to change it to a BLX
189 // instruction instead.
190
191 // Insert the value into the instruction.
192 *p = (*p & ~0xffffff) | Value;
193 break;
194 }
195 case macho::RIT_ARM_ThumbBranch22Bit:
196 case macho::RIT_ARM_ThumbBranch32Bit:
197 case macho::RIT_ARM_Half:
198 case macho::RIT_ARM_HalfDifference:
199 case macho::RIT_Pair:
200 case macho::RIT_Difference:
201 case macho::RIT_ARM_LocalDifference:
202 case macho::RIT_ARM_PreboundLazyPointer:
203 return Error("Relocation type not implemented yet!");
204 }
205 return false;
206}
207
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000208void RuntimeDyldMachO::processRelocationRef(const ObjRelocationInfo &Rel,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000209 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000210 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000211 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000212 StubMap &Stubs) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000213
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000214 uint32_t RelType = (uint32_t) (Rel.Type & 0xffffffffL);
215 RelocationValueRef Value;
216 SectionEntry &Section = Sections[Rel.SectionID];
Jim Grosbach61425c02012-01-16 22:26:39 +0000217
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000218 bool isExtern = (RelType >> 27) & 1;
219 if (isExtern) {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000220 // Obtain the symbol name which is referenced in the relocation
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000221 StringRef TargetName;
222 const SymbolRef &Symbol = Rel.Symbol;
223 Symbol.getName(TargetName);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000224 // First search for the symbol in the local symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000225 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000226 if (lsi != Symbols.end()) {
227 Value.SectionID = lsi->second.first;
228 Value.Addend = lsi->second.second;
229 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000230 // Search for the symbol in the global symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000231 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.find(TargetName.data());
232 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000233 Value.SectionID = gsi->second.first;
234 Value.Addend = gsi->second.second;
235 } else
236 Value.SymbolName = TargetName.data();
Danil Malyshev4b0b8ef2012-03-29 21:46:18 +0000237 }
Bill Wendling288967d2012-03-29 23:23:59 +0000238 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000239 error_code err;
240 uint8_t sectionIndex = static_cast<uint8_t>(RelType & 0xFF);
241 section_iterator si = Obj.begin_sections(),
242 se = Obj.end_sections();
243 for (uint8_t i = 1; i < sectionIndex; i++) {
244 error_code err;
245 si.increment(err);
246 if (si == se)
247 break;
248 }
249 assert(si != se && "No section containing relocation!");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000250 Value.SectionID = findOrEmitSection(Obj, *si, true, ObjSectionToID);
Jim Grosbach7639f982012-09-13 01:24:37 +0000251 Value.Addend = 0;
252 // FIXME: The size and type of the relocation determines if we can
253 // encode an Addend in the target location itself, and if so, how many
254 // bytes we should read in order to get it. We don't yet support doing
255 // that, and just assuming it's sizeof(intptr_t) is blatantly wrong.
256 //Value.Addend = *(const intptr_t *)Target;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000257 if (Value.Addend) {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000258 // The MachO addend is an offset from the current section. We need it
259 // to be an offset from the destination section
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000260 Value.Addend += Section.ObjAddress - Sections[Value.SectionID].ObjAddress;
261 }
Bill Wendling288967d2012-03-29 23:23:59 +0000262 }
263
Jim Grosbach01e1a972012-09-13 01:24:35 +0000264 if (Arch == Triple::arm && (RelType & 0xf) == macho::RIT_ARM_Branch24Bit) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000265 // This is an ARM branch relocation, need to use a stub function.
Bill Wendling288967d2012-03-29 23:23:59 +0000266
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000267 // Look up for existing stub.
268 StubMap::const_iterator i = Stubs.find(Value);
269 if (i != Stubs.end())
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000270 resolveRelocation(Section, Rel.Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000271 (uint64_t)Section.Address + i->second,
272 RelType, 0);
273 else {
274 // Create a new stub function.
275 Stubs[Value] = Section.StubOffset;
276 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
277 Section.StubOffset);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000278 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
279 macho::RIT_Vanilla, Value.Addend);
280 if (Value.SymbolName)
281 addRelocationForSymbol(RE, Value.SymbolName);
282 else
283 addRelocationForSection(RE, Value.SectionID);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000284 resolveRelocation(Section, Rel.Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000285 (uint64_t)Section.Address + Section.StubOffset,
286 RelType, 0);
287 Section.StubOffset += getMaxStubSize();
288 }
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000289 } else {
290 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
291 if (Value.SymbolName)
292 addRelocationForSymbol(RE, Value.SymbolName);
293 else
294 addRelocationForSection(RE, Value.SectionID);
295 }
Bill Wendling288967d2012-03-29 23:23:59 +0000296}
297
Bill Wendling288967d2012-03-29 23:23:59 +0000298
Eli Bendersky6d15e872012-04-30 10:06:27 +0000299bool RuntimeDyldMachO::isCompatibleFormat(
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000300 const ObjectBuffer *InputBuffer) const {
301 if (InputBuffer->getBufferSize() < 4)
302 return false;
303 StringRef Magic(InputBuffer->getBufferStart(), 4);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000304 if (Magic == "\xFE\xED\xFA\xCE") return true;
305 if (Magic == "\xCE\xFA\xED\xFE") return true;
306 if (Magic == "\xFE\xED\xFA\xCF") return true;
307 if (Magic == "\xCF\xFA\xED\xFE") return true;
308 return false;
309}
310
311} // end namespace llvm