Put RTDyldMemoryManager into its own file, and make it linked into
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyld.cpp
1 //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- 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 // Implementation of the MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "dyld"
15 #include "llvm/ExecutionEngine/RuntimeDyld.h"
16 #include "ObjectImageCommon.h"
17 #include "RuntimeDyldELF.h"
18 #include "RuntimeDyldImpl.h"
19 #include "RuntimeDyldMachO.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/Path.h"
22
23 using namespace llvm;
24 using namespace llvm::object;
25
26 // Empty out-of-line virtual destructor as the key function.
27 RuntimeDyldImpl::~RuntimeDyldImpl() {}
28
29 namespace llvm {
30
31 StringRef RuntimeDyldImpl::getEHFrameSection() {
32   return StringRef();
33 }
34
35 // Resolve the relocations for all symbols we currently know about.
36 void RuntimeDyldImpl::resolveRelocations() {
37   // First, resolve relocations associated with external symbols.
38   resolveExternalSymbols();
39
40   // Just iterate over the sections we have and resolve all the relocations
41   // in them. Gross overkill, but it gets the job done.
42   for (int i = 0, e = Sections.size(); i != e; ++i) {
43     uint64_t Addr = Sections[i].LoadAddress;
44     DEBUG(dbgs() << "Resolving relocations Section #" << i
45             << "\t" << format("%p", (uint8_t *)Addr)
46             << "\n");
47     resolveRelocationList(Relocations[i], Addr);
48   }
49 }
50
51 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
52                                         uint64_t TargetAddress) {
53   for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
54     if (Sections[i].Address == LocalAddress) {
55       reassignSectionAddress(i, TargetAddress);
56       return;
57     }
58   }
59   llvm_unreachable("Attempting to remap address of unknown section!");
60 }
61
62 // Subclasses can implement this method to create specialized image instances.
63 // The caller owns the pointer that is returned.
64 ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
65   return new ObjectImageCommon(InputBuffer);
66 }
67
68 ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
69   OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
70   if (!obj)
71     report_fatal_error("Unable to create object image from memory buffer!");
72
73   Arch = (Triple::ArchType)obj->getArch();
74
75   // Symbols found in this object
76   StringMap<SymbolLoc> LocalSymbols;
77   // Used sections from the object file
78   ObjSectionToIDMap LocalSections;
79
80   // Common symbols requiring allocation, with their sizes and alignments
81   CommonSymbolMap CommonSymbols;
82   // Maximum required total memory to allocate all common symbols
83   uint64_t CommonSize = 0;
84
85   error_code err;
86   // Parse symbols
87   DEBUG(dbgs() << "Parse symbols:\n");
88   for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
89        i != e; i.increment(err)) {
90     Check(err);
91     object::SymbolRef::Type SymType;
92     StringRef Name;
93     Check(i->getType(SymType));
94     Check(i->getName(Name));
95
96     uint32_t flags;
97     Check(i->getFlags(flags));
98
99     bool isCommon = flags & SymbolRef::SF_Common;
100     if (isCommon) {
101       // Add the common symbols to a list.  We'll allocate them all below.
102       uint32_t Align;
103       Check(i->getAlignment(Align));
104       uint64_t Size = 0;
105       Check(i->getSize(Size));
106       CommonSize += Size + Align;
107       CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
108     } else {
109       if (SymType == object::SymbolRef::ST_Function ||
110           SymType == object::SymbolRef::ST_Data ||
111           SymType == object::SymbolRef::ST_Unknown) {
112         uint64_t FileOffset;
113         StringRef SectionData;
114         bool IsCode;
115         section_iterator si = obj->end_sections();
116         Check(i->getFileOffset(FileOffset));
117         Check(i->getSection(si));
118         if (si == obj->end_sections()) continue;
119         Check(si->getContents(SectionData));
120         Check(si->isText(IsCode));
121         const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
122                                 (uintptr_t)FileOffset;
123         uintptr_t SectOffset = (uintptr_t)(SymPtr -
124                                            (const uint8_t*)SectionData.begin());
125         unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections);
126         LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
127         DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
128                      << " flags: " << flags
129                      << " SID: " << SectionID
130                      << " Offset: " << format("%p", SectOffset));
131         GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
132       }
133     }
134     DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
135   }
136
137   // Allocate common symbols
138   if (CommonSize != 0)
139     emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
140
141   // Parse and process relocations
142   DEBUG(dbgs() << "Parse relocations:\n");
143   for (section_iterator si = obj->begin_sections(),
144        se = obj->end_sections(); si != se; si.increment(err)) {
145     Check(err);
146     bool isFirstRelocation = true;
147     unsigned SectionID = 0;
148     StubMap Stubs;
149
150     for (relocation_iterator i = si->begin_relocations(),
151          e = si->end_relocations(); i != e; i.increment(err)) {
152       Check(err);
153
154       // If it's the first relocation in this section, find its SectionID
155       if (isFirstRelocation) {
156         SectionID = findOrEmitSection(*obj, *si, true, LocalSections);
157         DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
158         isFirstRelocation = false;
159       }
160
161       processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols,
162                            Stubs);
163     }
164   }
165
166   return obj.take();
167 }
168
169 void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
170                                         const CommonSymbolMap &CommonSymbols,
171                                         uint64_t TotalSize,
172                                         SymbolTableMap &SymbolTable) {
173   // Allocate memory for the section
174   unsigned SectionID = Sections.size();
175   uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
176                                               SectionID, false);
177   if (!Addr)
178     report_fatal_error("Unable to allocate memory for common symbols!");
179   uint64_t Offset = 0;
180   Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
181   memset(Addr, 0, TotalSize);
182
183   DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
184                << " new addr: " << format("%p", Addr)
185                << " DataSize: " << TotalSize
186                << "\n");
187
188   // Assign the address of each symbol
189   for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
190        itEnd = CommonSymbols.end(); it != itEnd; it++) {
191     uint64_t Size = it->second.first;
192     uint64_t Align = it->second.second;
193     StringRef Name;
194     it->first.getName(Name);
195     if (Align) {
196       // This symbol has an alignment requirement.
197       uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
198       Addr += AlignOffset;
199       Offset += AlignOffset;
200       DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
201                       format("%p\n", Addr));
202     }
203     Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
204     SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
205     Offset += Size;
206     Addr += Size;
207   }
208 }
209
210 unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
211                                       const SectionRef &Section,
212                                       bool IsCode) {
213
214   unsigned StubBufSize = 0,
215            StubSize = getMaxStubSize();
216   error_code err;
217   if (StubSize > 0) {
218     for (relocation_iterator i = Section.begin_relocations(),
219          e = Section.end_relocations(); i != e; i.increment(err), Check(err))
220       StubBufSize += StubSize;
221   }
222   StringRef data;
223   uint64_t Alignment64;
224   Check(Section.getContents(data));
225   Check(Section.getAlignment(Alignment64));
226
227   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
228   bool IsRequired;
229   bool IsVirtual;
230   bool IsZeroInit;
231   bool IsReadOnly;
232   uint64_t DataSize;
233   StringRef Name;
234   Check(Section.isRequiredForExecution(IsRequired));
235   Check(Section.isVirtual(IsVirtual));
236   Check(Section.isZeroInit(IsZeroInit));
237   Check(Section.isReadOnlyData(IsReadOnly));
238   Check(Section.getSize(DataSize));
239   Check(Section.getName(Name));
240   if (StubSize > 0) {
241     unsigned StubAlignment = getStubAlignment();
242     unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
243     if (StubAlignment > EndAlignment)
244       StubBufSize += StubAlignment - EndAlignment;
245   }
246
247   unsigned Allocate;
248   unsigned SectionID = Sections.size();
249   uint8_t *Addr;
250   const char *pData = 0;
251
252   // Some sections, such as debug info, don't need to be loaded for execution.
253   // Leave those where they are.
254   if (IsRequired) {
255     Allocate = DataSize + StubBufSize;
256     Addr = IsCode
257       ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID)
258       : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, IsReadOnly);
259     if (!Addr)
260       report_fatal_error("Unable to allocate section memory!");
261
262     // Virtual sections have no data in the object image, so leave pData = 0
263     if (!IsVirtual)
264       pData = data.data();
265
266     // Zero-initialize or copy the data from the image
267     if (IsZeroInit || IsVirtual)
268       memset(Addr, 0, DataSize);
269     else
270       memcpy(Addr, pData, DataSize);
271
272     DEBUG(dbgs() << "emitSection SectionID: " << SectionID
273                  << " Name: " << Name
274                  << " obj addr: " << format("%p", pData)
275                  << " new addr: " << format("%p", Addr)
276                  << " DataSize: " << DataSize
277                  << " StubBufSize: " << StubBufSize
278                  << " Allocate: " << Allocate
279                  << "\n");
280     Obj.updateSectionAddress(Section, (uint64_t)Addr);
281   }
282   else {
283     // Even if we didn't load the section, we need to record an entry for it
284     // to handle later processing (and by 'handle' I mean don't do anything
285     // with these sections).
286     Allocate = 0;
287     Addr = 0;
288     DEBUG(dbgs() << "emitSection SectionID: " << SectionID
289                  << " Name: " << Name
290                  << " obj addr: " << format("%p", data.data())
291                  << " new addr: 0"
292                  << " DataSize: " << DataSize
293                  << " StubBufSize: " << StubBufSize
294                  << " Allocate: " << Allocate
295                  << "\n");
296   }
297
298   Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
299   return SectionID;
300 }
301
302 unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
303                                             const SectionRef &Section,
304                                             bool IsCode,
305                                             ObjSectionToIDMap &LocalSections) {
306
307   unsigned SectionID = 0;
308   ObjSectionToIDMap::iterator i = LocalSections.find(Section);
309   if (i != LocalSections.end())
310     SectionID = i->second;
311   else {
312     SectionID = emitSection(Obj, Section, IsCode);
313     LocalSections[Section] = SectionID;
314   }
315   return SectionID;
316 }
317
318 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
319                                               unsigned SectionID) {
320   Relocations[SectionID].push_back(RE);
321 }
322
323 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
324                                              StringRef SymbolName) {
325   // Relocation by symbol.  If the symbol is found in the global symbol table,
326   // create an appropriate section relocation.  Otherwise, add it to
327   // ExternalSymbolRelocations.
328   SymbolTableMap::const_iterator Loc =
329       GlobalSymbolTable.find(SymbolName);
330   if (Loc == GlobalSymbolTable.end()) {
331     ExternalSymbolRelocations[SymbolName].push_back(RE);
332   } else {
333     // Copy the RE since we want to modify its addend.
334     RelocationEntry RECopy = RE;
335     RECopy.Addend += Loc->second.second;
336     Relocations[Loc->second.first].push_back(RECopy);
337   }
338 }
339
340 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
341   if (Arch == Triple::aarch64) {
342     // This stub has to be able to access the full address space,
343     // since symbol lookup won't necessarily find a handy, in-range,
344     // PLT stub for functions which could be anywhere.
345     uint32_t *StubAddr = (uint32_t*)Addr;
346
347     // Stub can use ip0 (== x16) to calculate address
348     *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
349     StubAddr++;
350     *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
351     StubAddr++;
352     *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
353     StubAddr++;
354     *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
355     StubAddr++;
356     *StubAddr = 0xd61f0200; // br ip0
357
358     return Addr;
359   } else if (Arch == Triple::arm) {
360     // TODO: There is only ARM far stub now. We should add the Thumb stub,
361     // and stubs for branches Thumb - ARM and ARM - Thumb.
362     uint32_t *StubAddr = (uint32_t*)Addr;
363     *StubAddr = 0xe51ff004; // ldr pc,<label>
364     return (uint8_t*)++StubAddr;
365   } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
366     uint32_t *StubAddr = (uint32_t*)Addr;
367     // 0:   3c190000        lui     t9,%hi(addr).
368     // 4:   27390000        addiu   t9,t9,%lo(addr).
369     // 8:   03200008        jr      t9.
370     // c:   00000000        nop.
371     const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
372     const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
373
374     *StubAddr = LuiT9Instr;
375     StubAddr++;
376     *StubAddr = AdduiT9Instr;
377     StubAddr++;
378     *StubAddr = JrT9Instr;
379     StubAddr++;
380     *StubAddr = NopInstr;
381     return Addr;
382   } else if (Arch == Triple::ppc64) {
383     // PowerPC64 stub: the address points to a function descriptor
384     // instead of the function itself. Load the function address
385     // on r11 and sets it to control register. Also loads the function
386     // TOC in r2 and environment pointer to r11.
387     writeInt32BE(Addr,    0x3D800000); // lis   r12, highest(addr)
388     writeInt32BE(Addr+4,  0x618C0000); // ori   r12, higher(addr)
389     writeInt32BE(Addr+8,  0x798C07C6); // sldi  r12, r12, 32
390     writeInt32BE(Addr+12, 0x658C0000); // oris  r12, r12, h(addr)
391     writeInt32BE(Addr+16, 0x618C0000); // ori   r12, r12, l(addr)
392     writeInt32BE(Addr+20, 0xF8410028); // std   r2,  40(r1)
393     writeInt32BE(Addr+24, 0xE96C0000); // ld    r11, 0(r12)
394     writeInt32BE(Addr+28, 0xE84C0008); // ld    r2,  0(r12)
395     writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
396     writeInt32BE(Addr+36, 0xE96C0010); // ld    r11, 16(r2)
397     writeInt32BE(Addr+40, 0x4E800420); // bctr
398
399     return Addr;
400   } else if (Arch == Triple::systemz) {
401     writeInt16BE(Addr,    0xC418);     // lgrl %r1,.+8
402     writeInt16BE(Addr+2,  0x0000);
403     writeInt16BE(Addr+4,  0x0004);
404     writeInt16BE(Addr+6,  0x07F1);     // brc 15,%r1
405     // 8-byte address stored at Addr + 8
406     return Addr;
407   }
408   return Addr;
409 }
410
411 // Assign an address to a symbol name and resolve all the relocations
412 // associated with it.
413 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
414                                              uint64_t Addr) {
415   // The address to use for relocation resolution is not
416   // the address of the local section buffer. We must be doing
417   // a remote execution environment of some sort. Relocations can't
418   // be applied until all the sections have been moved.  The client must
419   // trigger this with a call to MCJIT::finalize() or
420   // RuntimeDyld::resolveRelocations().
421   //
422   // Addr is a uint64_t because we can't assume the pointer width
423   // of the target is the same as that of the host. Just use a generic
424   // "big enough" type.
425   Sections[SectionID].LoadAddress = Addr;
426 }
427
428 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
429                                             uint64_t Value) {
430   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
431     const RelocationEntry &RE = Relocs[i];
432     // Ignore relocations for sections that were not loaded
433     if (Sections[RE.SectionID].Address == 0)
434       continue;
435     resolveRelocation(RE, Value);
436   }
437 }
438
439 void RuntimeDyldImpl::resolveExternalSymbols() {
440   StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(),
441                                       e = ExternalSymbolRelocations.end();
442   for (; i != e; i++) {
443     StringRef Name = i->first();
444     RelocationList &Relocs = i->second;
445     SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
446     if (Loc == GlobalSymbolTable.end()) {
447       if (Name.size() == 0) {
448         // This is an absolute symbol, use an address of zero.
449         DEBUG(dbgs() << "Resolving absolute relocations." << "\n");
450         resolveRelocationList(Relocs, 0);
451       } else {
452         // This is an external symbol, try to get its address from
453         // MemoryManager.
454         uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
455                                                                    true);
456         DEBUG(dbgs() << "Resolving relocations Name: " << Name
457                 << "\t" << format("%p", Addr)
458                 << "\n");
459         resolveRelocationList(Relocs, (uintptr_t)Addr);
460       }
461     } else {
462       report_fatal_error("Expected external symbol");
463     }
464   }
465 }
466
467
468 //===----------------------------------------------------------------------===//
469 // RuntimeDyld class implementation
470 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
471   // FIXME: There's a potential issue lurking here if a single instance of
472   // RuntimeDyld is used to load multiple objects.  The current implementation
473   // associates a single memory manager with a RuntimeDyld instance.  Even
474   // though the public class spawns a new 'impl' instance for each load,
475   // they share a single memory manager.  This can become a problem when page
476   // permissions are applied.
477   Dyld = 0;
478   MM = mm;
479 }
480
481 RuntimeDyld::~RuntimeDyld() {
482   delete Dyld;
483 }
484
485 ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
486   if (!Dyld) {
487     sys::LLVMFileType type = sys::IdentifyFileType(
488             InputBuffer->getBufferStart(),
489             static_cast<unsigned>(InputBuffer->getBufferSize()));
490     switch (type) {
491       case sys::ELF_Relocatable_FileType:
492       case sys::ELF_Executable_FileType:
493       case sys::ELF_SharedObject_FileType:
494       case sys::ELF_Core_FileType:
495         Dyld = new RuntimeDyldELF(MM);
496         break;
497       case sys::Mach_O_Object_FileType:
498       case sys::Mach_O_Executable_FileType:
499       case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
500       case sys::Mach_O_Core_FileType:
501       case sys::Mach_O_PreloadExecutable_FileType:
502       case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
503       case sys::Mach_O_DynamicLinker_FileType:
504       case sys::Mach_O_Bundle_FileType:
505       case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
506       case sys::Mach_O_DSYMCompanion_FileType:
507         Dyld = new RuntimeDyldMachO(MM);
508         break;
509       case sys::Unknown_FileType:
510       case sys::Bitcode_FileType:
511       case sys::Archive_FileType:
512       case sys::COFF_FileType:
513         report_fatal_error("Incompatible object format!");
514     }
515   } else {
516     if (!Dyld->isCompatibleFormat(InputBuffer))
517       report_fatal_error("Incompatible object format!");
518   }
519
520   return Dyld->loadObject(InputBuffer);
521 }
522
523 void *RuntimeDyld::getSymbolAddress(StringRef Name) {
524   return Dyld->getSymbolAddress(Name);
525 }
526
527 uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
528   return Dyld->getSymbolLoadAddress(Name);
529 }
530
531 void RuntimeDyld::resolveRelocations() {
532   Dyld->resolveRelocations();
533 }
534
535 void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
536                                          uint64_t Addr) {
537   Dyld->reassignSectionAddress(SectionID, Addr);
538 }
539
540 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
541                                     uint64_t TargetAddress) {
542   Dyld->mapSectionAddress(LocalAddress, TargetAddress);
543 }
544
545 StringRef RuntimeDyld::getErrorString() {
546   return Dyld->getErrorString();
547 }
548
549 StringRef RuntimeDyld::getEHFrameSection() {
550   return Dyld->getEHFrameSection();
551 }
552
553 } // end namespace llvm