OSDN Git Service

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