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