1 //===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "dyld"
15 #include "RuntimeDyldMachO.h"
16 #include "llvm/ADT/OwningPtr.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringRef.h"
20 using namespace llvm::object;
24 static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText, intptr_t DeltaForEH) {
25 uint32_t Length = *((uint32_t*)P);
27 unsigned char *Ret = P + Length;
28 uint32_t Offset = *((uint32_t*)P);
29 if (Offset == 0) // is a CIE
33 intptr_t FDELocation = *((intptr_t*)P);
34 intptr_t NewLocation = FDELocation - DeltaForText;
35 *((intptr_t*)P) = NewLocation;
36 P += sizeof(intptr_t);
38 // Skip the FDE address range
39 P += sizeof(intptr_t);
41 uint8_t Augmentationsize = *P;
43 if (Augmentationsize != 0) {
44 intptr_t LSDA = *((intptr_t*)P);
45 intptr_t NewLSDA = LSDA - DeltaForEH;
46 *((intptr_t*)P) = NewLSDA;
52 static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
53 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
54 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
55 return ObjDistance - MemDistance;
58 StringRef RuntimeDyldMachO::getEHFrameSection() {
59 SectionEntry *Text = NULL;
60 SectionEntry *EHFrame = NULL;
61 SectionEntry *ExceptTab = NULL;
62 for (int i = 0, e = Sections.size(); i != e; ++i) {
63 if (Sections[i].Name == "__eh_frame")
64 EHFrame = &Sections[i];
65 else if (Sections[i].Name == "__text")
67 else if (Sections[i].Name == "__gcc_except_tab")
68 ExceptTab = &Sections[i];
70 if (Text == NULL || EHFrame == NULL)
73 intptr_t DeltaForText = computeDelta(Text, EHFrame);
74 intptr_t DeltaForEH = 0;
76 DeltaForEH = computeDelta(ExceptTab, EHFrame);
78 unsigned char *P = EHFrame->Address;
79 unsigned char *End = P + EHFrame->Size;
81 P = processFDE(P, DeltaForText, DeltaForEH);
84 return StringRef((char*)EHFrame->Address, EHFrame->Size);
87 void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE,
89 const SectionEntry &Section = Sections[RE.SectionID];
90 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
94 void RuntimeDyldMachO::resolveRelocation(const SectionEntry &Section,
101 uint8_t *LocalAddress = Section.Address + Offset;
102 uint64_t FinalAddress = Section.LoadAddress + Offset;
103 unsigned MachoType = Type;
104 unsigned Size = 1 << LogSize;
106 DEBUG(dbgs() << "resolveRelocation LocalAddress: "
107 << format("%p", LocalAddress)
108 << " FinalAddress: " << format("%p", FinalAddress)
109 << " Value: " << format("%p", Value)
110 << " Addend: " << Addend
111 << " isPCRel: " << isPCRel
112 << " MachoType: " << MachoType
116 // This just dispatches to the proper target specific routine.
118 default: llvm_unreachable("Unsupported CPU type!");
120 resolveX86_64Relocation(LocalAddress,
129 resolveI386Relocation(LocalAddress,
137 case Triple::arm: // Fall through.
139 resolveARMRelocation(LocalAddress,
150 bool RuntimeDyldMachO::resolveI386Relocation(uint8_t *LocalAddress,
151 uint64_t FinalAddress,
158 Value -= FinalAddress + 4; // see resolveX86_64Relocation
162 llvm_unreachable("Invalid relocation type!");
163 case macho::RIT_Vanilla: {
164 uint8_t *p = LocalAddress;
165 uint64_t ValueToWrite = Value + Addend;
166 for (unsigned i = 0; i < Size; ++i) {
167 *p++ = (uint8_t)(ValueToWrite & 0xff);
172 case macho::RIT_Difference:
173 case macho::RIT_Generic_LocalDifference:
174 case macho::RIT_Generic_PreboundLazyPointer:
175 return Error("Relocation type not implemented yet!");
179 bool RuntimeDyldMachO::resolveX86_64Relocation(uint8_t *LocalAddress,
180 uint64_t FinalAddress,
186 // If the relocation is PC-relative, the value to be encoded is the
187 // pointer difference.
189 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
190 // address. Is that expected? Only for branches, perhaps?
191 Value -= FinalAddress + 4;
195 llvm_unreachable("Invalid relocation type!");
196 case macho::RIT_X86_64_Signed1:
197 case macho::RIT_X86_64_Signed2:
198 case macho::RIT_X86_64_Signed4:
199 case macho::RIT_X86_64_Signed:
200 case macho::RIT_X86_64_Unsigned:
201 case macho::RIT_X86_64_Branch: {
203 // Mask in the target value a byte at a time (we don't have an alignment
204 // guarantee for the target address, so this is safest).
205 uint8_t *p = (uint8_t*)LocalAddress;
206 for (unsigned i = 0; i < Size; ++i) {
207 *p++ = (uint8_t)Value;
212 case macho::RIT_X86_64_GOTLoad:
213 case macho::RIT_X86_64_GOT:
214 case macho::RIT_X86_64_Subtractor:
215 case macho::RIT_X86_64_TLV:
216 return Error("Relocation type not implemented yet!");
220 bool RuntimeDyldMachO::resolveARMRelocation(uint8_t *LocalAddress,
221 uint64_t FinalAddress,
227 // If the relocation is PC-relative, the value to be encoded is the
228 // pointer difference.
230 Value -= FinalAddress;
231 // ARM PCRel relocations have an effective-PC offset of two instructions
232 // (four bytes in Thumb mode, 8 bytes in ARM mode).
233 // FIXME: For now, assume ARM mode.
239 llvm_unreachable("Invalid relocation type!");
240 case macho::RIT_Vanilla: {
241 // Mask in the target value a byte at a time (we don't have an alignment
242 // guarantee for the target address, so this is safest).
243 uint8_t *p = (uint8_t*)LocalAddress;
244 for (unsigned i = 0; i < Size; ++i) {
245 *p++ = (uint8_t)Value;
250 case macho::RIT_ARM_Branch24Bit: {
251 // Mask the value into the target address. We know instructions are
252 // 32-bit aligned, so we can do it all at once.
253 uint32_t *p = (uint32_t*)LocalAddress;
254 // The low two bits of the value are not encoded.
256 // Mask the value to 24 bits.
258 // FIXME: If the destination is a Thumb function (and the instruction
259 // is a non-predicated BL instruction), we need to change it to a BLX
260 // instruction instead.
262 // Insert the value into the instruction.
263 *p = (*p & ~0xffffff) | Value;
266 case macho::RIT_ARM_ThumbBranch22Bit:
267 case macho::RIT_ARM_ThumbBranch32Bit:
268 case macho::RIT_ARM_Half:
269 case macho::RIT_ARM_HalfDifference:
270 case macho::RIT_Pair:
271 case macho::RIT_Difference:
272 case macho::RIT_ARM_LocalDifference:
273 case macho::RIT_ARM_PreboundLazyPointer:
274 return Error("Relocation type not implemented yet!");
279 void RuntimeDyldMachO::processRelocationRef(unsigned SectionID,
282 ObjSectionToIDMap &ObjSectionToID,
283 const SymbolTableMap &Symbols,
285 const ObjectFile *OF = Obj.getObjectFile();
286 const MachOObjectFile *MachO = static_cast<const MachOObjectFile*>(OF);
287 macho::RelocationEntry RE = MachO->getRelocation(RelI.getRawDataRefImpl());
289 uint32_t RelType = MachO->getAnyRelocationType(RE);
291 // FIXME: Properly handle scattered relocations.
292 // For now, optimistically skip these: they can often be ignored, as
293 // the static linker will already have applied the relocation, and it
294 // only needs to be reapplied if symbols move relative to one another.
295 // Note: This will fail horribly where the relocations *do* need to be
296 // applied, but that was already the case.
297 if (MachO->isRelocationScattered(RE))
300 RelocationValueRef Value;
301 SectionEntry &Section = Sections[SectionID];
303 bool isExtern = MachO->getPlainRelocationExternal(RE);
304 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
305 unsigned Size = MachO->getAnyRelocationLength(RE);
307 RelI.getOffset(Offset);
308 uint8_t *LocalAddress = Section.Address + Offset;
309 unsigned NumBytes = 1 << Size;
311 memcpy(&Addend, LocalAddress, NumBytes);
314 // Obtain the symbol name which is referenced in the relocation
315 symbol_iterator Symbol = RelI.getSymbol();
316 StringRef TargetName;
317 Symbol->getName(TargetName);
318 // First search for the symbol in the local symbol table
319 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
320 if (lsi != Symbols.end()) {
321 Value.SectionID = lsi->second.first;
322 Value.Addend = lsi->second.second + Addend;
324 // Search for the symbol in the global symbol table
325 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.find(TargetName.data());
326 if (gsi != GlobalSymbolTable.end()) {
327 Value.SectionID = gsi->second.first;
328 Value.Addend = gsi->second.second + Addend;
330 Value.SymbolName = TargetName.data();
331 Value.Addend = Addend;
335 SectionRef Sec = MachO->getRelocationSection(RE);
336 Value.SectionID = findOrEmitSection(Obj, Sec, true, ObjSectionToID);
338 Sec.getAddress(Addr);
339 Value.Addend = Addend - Addr;
342 if (Arch == Triple::x86_64 && RelType == macho::RIT_X86_64_GOT) {
345 StubMap::const_iterator i = Stubs.find(Value);
347 if (i != Stubs.end()) {
348 Addr = Section.Address + i->second;
350 Stubs[Value] = Section.StubOffset;
351 uint8_t *GOTEntry = Section.Address + Section.StubOffset;
352 RelocationEntry RE(SectionID, Section.StubOffset,
353 macho::RIT_X86_64_Unsigned, Value.Addend - 4, false,
355 if (Value.SymbolName)
356 addRelocationForSymbol(RE, Value.SymbolName);
358 addRelocationForSection(RE, Value.SectionID);
359 Section.StubOffset += 8;
362 resolveRelocation(Section, Offset, (uint64_t)Addr,
363 macho::RIT_X86_64_Unsigned, 4, true, 2);
364 } else if (Arch == Triple::arm &&
365 (RelType & 0xf) == macho::RIT_ARM_Branch24Bit) {
366 // This is an ARM branch relocation, need to use a stub function.
368 // Look up for existing stub.
369 StubMap::const_iterator i = Stubs.find(Value);
370 if (i != Stubs.end())
371 resolveRelocation(Section, Offset,
372 (uint64_t)Section.Address + i->second,
373 RelType, 0, IsPCRel, Size);
375 // Create a new stub function.
376 Stubs[Value] = Section.StubOffset;
377 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
379 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
380 macho::RIT_Vanilla, Value.Addend);
381 if (Value.SymbolName)
382 addRelocationForSymbol(RE, Value.SymbolName);
384 addRelocationForSection(RE, Value.SectionID);
385 resolveRelocation(Section, Offset,
386 (uint64_t)Section.Address + Section.StubOffset,
387 RelType, 0, IsPCRel, Size);
388 Section.StubOffset += getMaxStubSize();
391 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend,
393 if (Value.SymbolName)
394 addRelocationForSymbol(RE, Value.SymbolName);
396 addRelocationForSection(RE, Value.SectionID);
401 bool RuntimeDyldMachO::isCompatibleFormat(
402 const ObjectBuffer *InputBuffer) const {
403 if (InputBuffer->getBufferSize() < 4)
405 StringRef Magic(InputBuffer->getBufferStart(), 4);
406 if (Magic == "\xFE\xED\xFA\xCE") return true;
407 if (Magic == "\xCE\xFA\xED\xFE") return true;
408 if (Magic == "\xFE\xED\xFA\xCF") return true;
409 if (Magic == "\xCF\xFA\xED\xFE") return true;
413 } // end namespace llvm