RuntimeDyld: override EH frame registration with trivial version.
[oota-llvm.git] / tools / llvm-rtdyld / llvm-rtdyld.cpp
1 //===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===//
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 // This is a testing tool for use with the MC-JIT LLVM components.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/DebugInfo/DIContext.h"
16 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
17 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
18 #include "llvm/ExecutionEngine/RuntimeDyld.h"
19 #include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCDisassembler.h"
23 #include "llvm/MC/MCInstPrinter.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/Object/MachO.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/DynamicLibrary.h"
30 #include "llvm/Support/ManagedStatic.h"
31 #include "llvm/Support/Memory.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/PrettyStackTrace.h"
34 #include "llvm/Support/Signals.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/TargetSelect.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include <list>
39 #include <system_error>
40
41 using namespace llvm;
42 using namespace llvm::object;
43
44 static cl::list<std::string>
45 InputFileList(cl::Positional, cl::ZeroOrMore,
46               cl::desc("<input file>"));
47
48 enum ActionType {
49   AC_Execute,
50   AC_PrintObjectLineInfo,
51   AC_PrintLineInfo,
52   AC_PrintDebugLineInfo,
53   AC_Verify
54 };
55
56 static cl::opt<ActionType>
57 Action(cl::desc("Action to perform:"),
58        cl::init(AC_Execute),
59        cl::values(clEnumValN(AC_Execute, "execute",
60                              "Load, link, and execute the inputs."),
61                   clEnumValN(AC_PrintLineInfo, "printline",
62                              "Load, link, and print line information for each function."),
63                   clEnumValN(AC_PrintDebugLineInfo, "printdebugline",
64                              "Load, link, and print line information for each function using the debug object"),
65                   clEnumValN(AC_PrintObjectLineInfo, "printobjline",
66                              "Like -printlineinfo but does not load the object first"),
67                   clEnumValN(AC_Verify, "verify",
68                              "Load, link and verify the resulting memory image."),
69                   clEnumValEnd));
70
71 static cl::opt<std::string>
72 EntryPoint("entry",
73            cl::desc("Function to call as entry point."),
74            cl::init("_main"));
75
76 static cl::list<std::string>
77 Dylibs("dylib",
78        cl::desc("Add library."),
79        cl::ZeroOrMore);
80
81 static cl::opt<std::string>
82 TripleName("triple", cl::desc("Target triple for disassembler"));
83
84 static cl::list<std::string>
85 CheckFiles("check",
86            cl::desc("File containing RuntimeDyld verifier checks."),
87            cl::ZeroOrMore);
88
89 static cl::opt<uint64_t>
90 TargetAddrStart("target-addr-start",
91                 cl::desc("For -verify only: start of phony target address "
92                          "range."),
93                 cl::init(4096), // Start at "page 1" - no allocating at "null".
94                 cl::Hidden);
95
96 static cl::opt<uint64_t>
97 TargetAddrEnd("target-addr-end",
98               cl::desc("For -verify only: end of phony target address range."),
99               cl::init(~0ULL),
100               cl::Hidden);
101
102 static cl::opt<uint64_t>
103 TargetSectionSep("target-section-sep",
104                  cl::desc("For -verify only: Separation between sections in "
105                           "phony target address space."),
106                  cl::init(0),
107                  cl::Hidden);
108
109 static cl::list<std::string>
110 SpecificSectionMappings("map-section",
111                         cl::desc("Map a section to a specific address."),
112                         cl::ZeroOrMore);
113
114 /* *** */
115
116 // A trivial memory manager that doesn't do anything fancy, just uses the
117 // support library allocation routines directly.
118 class TrivialMemoryManager : public RTDyldMemoryManager {
119 public:
120   SmallVector<sys::MemoryBlock, 16> FunctionMemory;
121   SmallVector<sys::MemoryBlock, 16> DataMemory;
122
123   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
124                                unsigned SectionID,
125                                StringRef SectionName) override;
126   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
127                                unsigned SectionID, StringRef SectionName,
128                                bool IsReadOnly) override;
129
130   void *getPointerToNamedFunction(const std::string &Name,
131                                   bool AbortOnFailure = true) override {
132     return nullptr;
133   }
134
135   bool finalizeMemory(std::string *ErrMsg) override { return false; }
136
137   // Invalidate instruction cache for sections with execute permissions.
138   // Some platforms with separate data cache and instruction cache require
139   // explicit cache flush, otherwise JIT code manipulations (like resolved
140   // relocations) will get to the data cache but not to the instruction cache.
141   virtual void invalidateInstructionCache();
142
143   void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
144                         size_t Size) override {}
145   void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
146                           size_t Size) override {}
147 };
148
149 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
150                                                    unsigned Alignment,
151                                                    unsigned SectionID,
152                                                    StringRef SectionName) {
153   sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
154   FunctionMemory.push_back(MB);
155   return (uint8_t*)MB.base();
156 }
157
158 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
159                                                    unsigned Alignment,
160                                                    unsigned SectionID,
161                                                    StringRef SectionName,
162                                                    bool IsReadOnly) {
163   sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
164   DataMemory.push_back(MB);
165   return (uint8_t*)MB.base();
166 }
167
168 void TrivialMemoryManager::invalidateInstructionCache() {
169   for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
170     sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
171                                             FunctionMemory[i].size());
172
173   for (int i = 0, e = DataMemory.size(); i != e; ++i)
174     sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
175                                             DataMemory[i].size());
176 }
177
178 static const char *ProgramName;
179
180 static void Message(const char *Type, const Twine &Msg) {
181   errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
182 }
183
184 static int Error(const Twine &Msg) {
185   Message("error", Msg);
186   return 1;
187 }
188
189 static void loadDylibs() {
190   for (const std::string &Dylib : Dylibs) {
191     if (sys::fs::is_regular_file(Dylib)) {
192       std::string ErrMsg;
193       if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
194         llvm::errs() << "Error loading '" << Dylib << "': "
195                      << ErrMsg << "\n";
196     } else
197       llvm::errs() << "Dylib not found: '" << Dylib << "'.\n";
198   }
199 }
200
201 /* *** */
202
203 static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
204   assert(LoadObjects || !UseDebugObj);
205
206   // Load any dylibs requested on the command line.
207   loadDylibs();
208
209   // If we don't have any input files, read from stdin.
210   if (!InputFileList.size())
211     InputFileList.push_back("-");
212   for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
213     // Instantiate a dynamic linker.
214     TrivialMemoryManager MemMgr;
215     RuntimeDyld Dyld(MemMgr, MemMgr);
216
217     // Load the input memory buffer.
218
219     ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
220         MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
221     if (std::error_code EC = InputBuffer.getError())
222       return Error("unable to read input: '" + EC.message() + "'");
223
224     ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
225       ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
226
227     if (std::error_code EC = MaybeObj.getError())
228       return Error("unable to create object file: '" + EC.message() + "'");
229
230     ObjectFile &Obj = **MaybeObj;
231
232     OwningBinary<ObjectFile> DebugObj;
233     std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = nullptr;
234     ObjectFile *SymbolObj = &Obj;
235     if (LoadObjects) {
236       // Load the object file
237       LoadedObjInfo =
238         Dyld.loadObject(Obj);
239
240       if (Dyld.hasError())
241         return Error(Dyld.getErrorString());
242
243       // Resolve all the relocations we can.
244       Dyld.resolveRelocations();
245
246       if (UseDebugObj) {
247         DebugObj = LoadedObjInfo->getObjectForDebug(Obj);
248         SymbolObj = DebugObj.getBinary();
249       }
250     }
251
252     std::unique_ptr<DIContext> Context(
253       new DWARFContextInMemory(*SymbolObj,LoadedObjInfo.get()));
254
255     // FIXME: This is generally useful. Figure out a place in lib/Object to
256     // put utility functions.
257     std::map<object::SectionRef, std::vector<uint64_t>> FuncAddresses;
258     if (!isa<ELFObjectFileBase>(SymbolObj)) {
259       for (object::SymbolRef Sym : SymbolObj->symbols()) {
260         object::SymbolRef::Type SymType;
261         if (Sym.getType(SymType))
262           continue;
263         if (SymType != object::SymbolRef::ST_Function)
264           continue;
265         uint64_t Addr;
266         if (Sym.getAddress(Addr))
267           continue;
268         object::section_iterator Sec = SymbolObj->section_end();
269         if (Sym.getSection(Sec))
270           continue;
271         std::vector<uint64_t> &Addrs = FuncAddresses[*Sec];
272         if (Addrs.empty()) {
273           uint64_t SecAddr = Sec->getAddress();
274           uint64_t SecSize = Sec->getSize();
275           Addrs.push_back(SecAddr + SecSize);
276         }
277         Addrs.push_back(Addr);
278       }
279       for (auto &Pair : FuncAddresses) {
280         std::vector<uint64_t> &Addrs = Pair.second;
281         array_pod_sort(Addrs.begin(), Addrs.end());
282       }
283     }
284
285     // Use symbol info to iterate functions in the object.
286     for (object::SymbolRef Sym : SymbolObj->symbols()) {
287       object::SymbolRef::Type SymType;
288       if (Sym.getType(SymType))
289         continue;
290       if (SymType == object::SymbolRef::ST_Function) {
291         StringRef  Name;
292         uint64_t   Addr;
293         if (Sym.getName(Name))
294           continue;
295         if (Sym.getAddress(Addr))
296           continue;
297
298         uint64_t Size;
299         if (isa<ELFObjectFileBase>(SymbolObj)) {
300           Size = Sym.getSize();
301         } else {
302           object::section_iterator Sec = SymbolObj->section_end();
303           if (Sym.getSection(Sec))
304             continue;
305           const std::vector<uint64_t> &Addrs = FuncAddresses[*Sec];
306           auto AddrI = std::find(Addrs.begin(), Addrs.end(), Addr);
307           assert(AddrI != Addrs.end() && (AddrI + 1) != Addrs.end());
308           assert(*AddrI == Addr);
309           Size = *(AddrI + 1) - Addr;
310         }
311
312         // If we're not using the debug object, compute the address of the
313         // symbol in memory (rather than that in the unrelocated object file)
314         // and use that to query the DWARFContext.
315         if (!UseDebugObj && LoadObjects) {
316           object::section_iterator Sec(SymbolObj->section_end());
317           Sym.getSection(Sec);
318           StringRef SecName;
319           Sec->getName(SecName);
320           uint64_t SectionLoadAddress =
321             LoadedObjInfo->getSectionLoadAddress(SecName);
322           if (SectionLoadAddress != 0)
323             Addr += SectionLoadAddress - Sec->getAddress();
324         }
325
326         outs() << "Function: " << Name << ", Size = " << Size << ", Addr = " << Addr << "\n";
327
328         DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
329         DILineInfoTable::iterator  Begin = Lines.begin();
330         DILineInfoTable::iterator  End = Lines.end();
331         for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
332           outs() << "  Line info @ " << It->first - Addr << ": "
333                  << It->second.FileName << ", line:" << It->second.Line << "\n";
334         }
335       }
336     }
337   }
338
339   return 0;
340 }
341
342 static int executeInput() {
343   // Load any dylibs requested on the command line.
344   loadDylibs();
345
346   // Instantiate a dynamic linker.
347   TrivialMemoryManager MemMgr;
348   RuntimeDyld Dyld(MemMgr, MemMgr);
349
350   // FIXME: Preserve buffers until resolveRelocations time to work around a bug
351   //        in RuntimeDyldELF.
352   // This fixme should be fixed ASAP. This is a very brittle workaround.
353   std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers;
354
355   // If we don't have any input files, read from stdin.
356   if (!InputFileList.size())
357     InputFileList.push_back("-");
358   for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
359     // Load the input memory buffer.
360     ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
361         MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
362     if (std::error_code EC = InputBuffer.getError())
363       return Error("unable to read input: '" + EC.message() + "'");
364     ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
365       ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
366
367     if (std::error_code EC = MaybeObj.getError())
368       return Error("unable to create object file: '" + EC.message() + "'");
369
370     ObjectFile &Obj = **MaybeObj;
371     InputBuffers.push_back(std::move(*InputBuffer));
372
373     // Load the object file
374     Dyld.loadObject(Obj);
375     if (Dyld.hasError()) {
376       return Error(Dyld.getErrorString());
377     }
378   }
379
380   // Resolve all the relocations we can.
381   Dyld.resolveRelocations();
382   // Clear instruction cache before code will be executed.
383   MemMgr.invalidateInstructionCache();
384
385   // FIXME: Error out if there are unresolved relocations.
386
387   // Get the address of the entry point (_main by default).
388   void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint);
389   if (!MainAddress)
390     return Error("no definition for '" + EntryPoint + "'");
391
392   // Invalidate the instruction cache for each loaded function.
393   for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) {
394     sys::MemoryBlock &Data = MemMgr.FunctionMemory[i];
395     // Make sure the memory is executable.
396     std::string ErrorStr;
397     sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
398     if (!sys::Memory::setExecutable(Data, &ErrorStr))
399       return Error("unable to mark function executable: '" + ErrorStr + "'");
400   }
401
402   // Dispatch to _main().
403   errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
404
405   int (*Main)(int, const char**) =
406     (int(*)(int,const char**)) uintptr_t(MainAddress);
407   const char **Argv = new const char*[2];
408   // Use the name of the first input object module as argv[0] for the target.
409   Argv[0] = InputFileList[0].c_str();
410   Argv[1] = nullptr;
411   return Main(1, Argv);
412 }
413
414 static int checkAllExpressions(RuntimeDyldChecker &Checker) {
415   for (const auto& CheckerFileName : CheckFiles) {
416     ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
417         MemoryBuffer::getFileOrSTDIN(CheckerFileName);
418     if (std::error_code EC = CheckerFileBuf.getError())
419       return Error("unable to read input '" + CheckerFileName + "': " +
420                    EC.message());
421
422     if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
423                                        CheckerFileBuf.get().get()))
424       return Error("some checks in '" + CheckerFileName + "' failed");
425   }
426   return 0;
427 }
428
429 static std::map<void *, uint64_t>
430 applySpecificSectionMappings(RuntimeDyldChecker &Checker) {
431
432   std::map<void*, uint64_t> SpecificMappings;
433
434   for (StringRef Mapping : SpecificSectionMappings) {
435
436     size_t EqualsIdx = Mapping.find_first_of("=");
437     StringRef SectionIDStr = Mapping.substr(0, EqualsIdx);
438     size_t ComaIdx = Mapping.find_first_of(",");
439
440     if (ComaIdx == StringRef::npos) {
441       errs() << "Invalid section specification '" << Mapping
442              << "'. Should be '<file name>,<section name>=<addr>'\n";
443       exit(1);
444     }
445
446     StringRef FileName = SectionIDStr.substr(0, ComaIdx);
447     StringRef SectionName = SectionIDStr.substr(ComaIdx + 1);
448
449     uint64_t OldAddrInt;
450     std::string ErrorMsg;
451     std::tie(OldAddrInt, ErrorMsg) =
452       Checker.getSectionAddr(FileName, SectionName, true);
453
454     if (ErrorMsg != "") {
455       errs() << ErrorMsg;
456       exit(1);
457     }
458
459     void* OldAddr = reinterpret_cast<void*>(static_cast<uintptr_t>(OldAddrInt));
460
461     StringRef NewAddrStr = Mapping.substr(EqualsIdx + 1);
462     uint64_t NewAddr;
463
464     if (NewAddrStr.getAsInteger(0, NewAddr)) {
465       errs() << "Invalid section address in mapping: " << Mapping << "\n";
466       exit(1);
467     }
468
469     Checker.getRTDyld().mapSectionAddress(OldAddr, NewAddr);
470     SpecificMappings[OldAddr] = NewAddr;
471   }
472
473   return SpecificMappings;
474 }
475
476 // Scatter sections in all directions!
477 // Remaps section addresses for -verify mode. The following command line options
478 // can be used to customize the layout of the memory within the phony target's
479 // address space:
480 // -target-addr-start <s> -- Specify where the phony target addres range starts.
481 // -target-addr-end   <e> -- Specify where the phony target address range ends.
482 // -target-section-sep <d> -- Specify how big a gap should be left between the
483 //                            end of one section and the start of the next.
484 //                            Defaults to zero. Set to something big
485 //                            (e.g. 1 << 32) to stress-test stubs, GOTs, etc.
486 //
487 static void remapSections(const llvm::Triple &TargetTriple,
488                           const TrivialMemoryManager &MemMgr,
489                           RuntimeDyldChecker &Checker) {
490
491   // Set up a work list (section addr/size pairs).
492   typedef std::list<std::pair<void*, uint64_t>> WorklistT;
493   WorklistT Worklist;
494
495   for (const auto& CodeSection : MemMgr.FunctionMemory)
496     Worklist.push_back(std::make_pair(CodeSection.base(), CodeSection.size()));
497   for (const auto& DataSection : MemMgr.DataMemory)
498     Worklist.push_back(std::make_pair(DataSection.base(), DataSection.size()));
499
500   // Apply any section-specific mappings that were requested on the command
501   // line.
502   typedef std::map<void*, uint64_t> AppliedMappingsT;
503   AppliedMappingsT AppliedMappings = applySpecificSectionMappings(Checker);
504
505   // Keep an "already allocated" mapping of section target addresses to sizes.
506   // Sections whose address mappings aren't specified on the command line will
507   // allocated around the explicitly mapped sections while maintaining the
508   // minimum separation.
509   std::map<uint64_t, uint64_t> AlreadyAllocated;
510
511   // Move the previously applied mappings into the already-allocated map.
512   for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end();
513        I != E;) {
514     WorklistT::iterator Tmp = I;
515     ++I;
516     AppliedMappingsT::iterator AI = AppliedMappings.find(Tmp->first);
517
518     if (AI != AppliedMappings.end()) {
519       AlreadyAllocated[AI->second] = Tmp->second;
520       Worklist.erase(Tmp);
521     }
522   }
523
524   // If the -target-addr-end option wasn't explicitly passed, then set it to a
525   // sensible default based on the target triple.
526   if (TargetAddrEnd.getNumOccurrences() == 0) {
527     if (TargetTriple.isArch16Bit())
528       TargetAddrEnd = (1ULL << 16) - 1;
529     else if (TargetTriple.isArch32Bit())
530       TargetAddrEnd = (1ULL << 32) - 1;
531     // TargetAddrEnd already has a sensible default for 64-bit systems, so
532     // there's nothing to do in the 64-bit case.
533   }
534
535   // Process any elements remaining in the worklist.
536   while (!Worklist.empty()) {
537     std::pair<void*, uint64_t> CurEntry = Worklist.front();
538     Worklist.pop_front();
539
540     uint64_t NextSectionAddr = TargetAddrStart;
541
542     for (const auto &Alloc : AlreadyAllocated)
543       if (NextSectionAddr + CurEntry.second + TargetSectionSep <= Alloc.first)
544         break;
545       else
546         NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep;
547
548     AlreadyAllocated[NextSectionAddr] = CurEntry.second;
549     Checker.getRTDyld().mapSectionAddress(CurEntry.first, NextSectionAddr);
550   }
551
552 }
553
554 // Load and link the objects specified on the command line, but do not execute
555 // anything. Instead, attach a RuntimeDyldChecker instance and call it to
556 // verify the correctness of the linked memory.
557 static int linkAndVerify() {
558
559   // Check for missing triple.
560   if (TripleName == "") {
561     llvm::errs() << "Error: -triple required when running in -verify mode.\n";
562     return 1;
563   }
564
565   // Look up the target and build the disassembler.
566   Triple TheTriple(Triple::normalize(TripleName));
567   std::string ErrorStr;
568   const Target *TheTarget =
569     TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
570   if (!TheTarget) {
571     llvm::errs() << "Error accessing target '" << TripleName << "': "
572                  << ErrorStr << "\n";
573     return 1;
574   }
575   TripleName = TheTriple.getTriple();
576
577   std::unique_ptr<MCSubtargetInfo> STI(
578     TheTarget->createMCSubtargetInfo(TripleName, "", ""));
579   assert(STI && "Unable to create subtarget info!");
580
581   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
582   assert(MRI && "Unable to create target register info!");
583
584   std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
585   assert(MAI && "Unable to create target asm info!");
586
587   MCContext Ctx(MAI.get(), MRI.get(), nullptr);
588
589   std::unique_ptr<MCDisassembler> Disassembler(
590     TheTarget->createMCDisassembler(*STI, Ctx));
591   assert(Disassembler && "Unable to create disassembler!");
592
593   std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
594
595   std::unique_ptr<MCInstPrinter> InstPrinter(
596       TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
597
598   // Load any dylibs requested on the command line.
599   loadDylibs();
600
601   // Instantiate a dynamic linker.
602   TrivialMemoryManager MemMgr;
603   RuntimeDyld Dyld(MemMgr, MemMgr);
604   Dyld.setProcessAllSections(true);
605   RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),
606                              llvm::dbgs());
607
608   // FIXME: Preserve buffers until resolveRelocations time to work around a bug
609   //        in RuntimeDyldELF.
610   // This fixme should be fixed ASAP. This is a very brittle workaround.
611   std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers;
612
613   // If we don't have any input files, read from stdin.
614   if (!InputFileList.size())
615     InputFileList.push_back("-");
616   for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
617     // Load the input memory buffer.
618     ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
619         MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
620
621     if (std::error_code EC = InputBuffer.getError())
622       return Error("unable to read input: '" + EC.message() + "'");
623
624     ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
625       ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
626
627     if (std::error_code EC = MaybeObj.getError())
628       return Error("unable to create object file: '" + EC.message() + "'");
629
630     ObjectFile &Obj = **MaybeObj;
631     InputBuffers.push_back(std::move(*InputBuffer));
632
633     // Load the object file
634     Dyld.loadObject(Obj);
635     if (Dyld.hasError()) {
636       return Error(Dyld.getErrorString());
637     }
638   }
639
640   // Re-map the section addresses into the phony target address space.
641   remapSections(TheTriple, MemMgr, Checker);
642
643   // Resolve all the relocations we can.
644   Dyld.resolveRelocations();
645
646   // Register EH frames.
647   Dyld.registerEHFrames();
648
649   int ErrorCode = checkAllExpressions(Checker);
650   if (Dyld.hasError()) {
651     errs() << "RTDyld reported an error applying relocations:\n  "
652            << Dyld.getErrorString() << "\n";
653     ErrorCode = 1;
654   }
655
656   return ErrorCode;
657 }
658
659 int main(int argc, char **argv) {
660   sys::PrintStackTraceOnErrorSignal();
661   PrettyStackTraceProgram X(argc, argv);
662
663   ProgramName = argv[0];
664   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
665
666   llvm::InitializeAllTargetInfos();
667   llvm::InitializeAllTargetMCs();
668   llvm::InitializeAllDisassemblers();
669
670   cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
671
672   switch (Action) {
673   case AC_Execute:
674     return executeInput();
675   case AC_PrintDebugLineInfo:
676     return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */ true);
677   case AC_PrintLineInfo:
678     return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */false);
679   case AC_PrintObjectLineInfo:
680     return printLineInfoForInput(/* LoadObjects */false,/* UseDebugObj */false);
681   case AC_Verify:
682     return linkAndVerify();
683   }
684 }