1 //===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is a testing tool for use with the MC-JIT LLVM components.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/DebugInfo/DIContext.h"
15 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
16 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
17 #include "llvm/ExecutionEngine/RuntimeDyld.h"
18 #include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
22 #include "llvm/MC/MCInstPrinter.h"
23 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/MC/MCTargetOptions.h"
27 #include "llvm/Object/SymbolSize.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/DynamicLibrary.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/InitLLVM.h"
32 #include "llvm/Support/MSVCErrorWorkarounds.h"
33 #include "llvm/Support/Memory.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/TargetSelect.h"
38 #include "llvm/Support/Timer.h"
39 #include "llvm/Support/raw_ostream.h"
40 
41 #include <future>
42 #include <list>
43 
44 using namespace llvm;
45 using namespace llvm::object;
46 
47 static cl::OptionCategory RTDyldCategory("RTDyld Options");
48 
49 static cl::list<std::string> InputFileList(cl::Positional, cl::ZeroOrMore,
50                                            cl::desc("<input files>"),
51                                            cl::cat(RTDyldCategory));
52 
53 enum ActionType {
54   AC_Execute,
55   AC_PrintObjectLineInfo,
56   AC_PrintLineInfo,
57   AC_PrintDebugLineInfo,
58   AC_Verify
59 };
60 
61 static cl::opt<ActionType> Action(
62     cl::desc("Action to perform:"), cl::init(AC_Execute),
63     cl::values(
64         clEnumValN(AC_Execute, "execute",
65                    "Load, link, and execute the inputs."),
66         clEnumValN(AC_PrintLineInfo, "printline",
67                    "Load, link, and print line information for each function."),
68         clEnumValN(AC_PrintDebugLineInfo, "printdebugline",
69                    "Load, link, and print line information for each function "
70                    "using the debug object"),
71         clEnumValN(AC_PrintObjectLineInfo, "printobjline",
72                    "Like -printlineinfo but does not load the object first"),
73         clEnumValN(AC_Verify, "verify",
74                    "Load, link and verify the resulting memory image.")),
75     cl::cat(RTDyldCategory));
76 
77 static cl::opt<std::string>
78     EntryPoint("entry", cl::desc("Function to call as entry point."),
79                cl::init("_main"), cl::cat(RTDyldCategory));
80 
81 static cl::list<std::string> Dylibs("dylib", cl::desc("Add library."),
82                                     cl::ZeroOrMore, cl::cat(RTDyldCategory));
83 
84 static cl::list<std::string> InputArgv("args", cl::Positional,
85                                        cl::desc("<program arguments>..."),
86                                        cl::ZeroOrMore, cl::PositionalEatsArgs,
87                                        cl::cat(RTDyldCategory));
88 
89 static cl::opt<std::string>
90     TripleName("triple", cl::desc("Target triple for disassembler"),
91                cl::cat(RTDyldCategory));
92 
93 static cl::opt<std::string>
94     MCPU("mcpu",
95          cl::desc("Target a specific cpu type (-mcpu=help for details)"),
96          cl::value_desc("cpu-name"), cl::init(""), cl::cat(RTDyldCategory));
97 
98 static cl::list<std::string>
99     CheckFiles("check",
100                cl::desc("File containing RuntimeDyld verifier checks."),
101                cl::ZeroOrMore, cl::cat(RTDyldCategory));
102 
103 static cl::opt<uint64_t>
104     PreallocMemory("preallocate",
105                    cl::desc("Allocate memory upfront rather than on-demand"),
106                    cl::init(0), cl::cat(RTDyldCategory));
107 
108 static cl::opt<uint64_t> TargetAddrStart(
109     "target-addr-start",
110     cl::desc("For -verify only: start of phony target address "
111              "range."),
112     cl::init(4096), // Start at "page 1" - no allocating at "null".
113     cl::Hidden, cl::cat(RTDyldCategory));
114 
115 static cl::opt<uint64_t> TargetAddrEnd(
116     "target-addr-end",
117     cl::desc("For -verify only: end of phony target address range."),
118     cl::init(~0ULL), cl::Hidden, cl::cat(RTDyldCategory));
119 
120 static cl::opt<uint64_t> TargetSectionSep(
121     "target-section-sep",
122     cl::desc("For -verify only: Separation between sections in "
123              "phony target address space."),
124     cl::init(0), cl::Hidden, cl::cat(RTDyldCategory));
125 
126 static cl::list<std::string>
127     SpecificSectionMappings("map-section",
128                             cl::desc("For -verify only: Map a section to a "
129                                      "specific address."),
130                             cl::ZeroOrMore, cl::Hidden,
131                             cl::cat(RTDyldCategory));
132 
133 static cl::list<std::string> DummySymbolMappings(
134     "dummy-extern",
135     cl::desc("For -verify only: Inject a symbol into the extern "
136              "symbol table."),
137     cl::ZeroOrMore, cl::Hidden, cl::cat(RTDyldCategory));
138 
139 static cl::opt<bool> PrintAllocationRequests(
140     "print-alloc-requests",
141     cl::desc("Print allocation requests made to the memory "
142              "manager by RuntimeDyld"),
143     cl::Hidden, cl::cat(RTDyldCategory));
144 
145 static cl::opt<bool> ShowTimes("show-times",
146                                cl::desc("Show times for llvm-rtdyld phases"),
147                                cl::init(false), cl::cat(RTDyldCategory));
148 
149 ExitOnError ExitOnErr;
150 
151 struct RTDyldTimers {
152   TimerGroup RTDyldTG{"llvm-rtdyld timers", "timers for llvm-rtdyld phases"};
153   Timer LoadObjectsTimer{"load", "time to load/add object files", RTDyldTG};
154   Timer LinkTimer{"link", "time to link object files", RTDyldTG};
155   Timer RunTimer{"run", "time to execute jitlink'd code", RTDyldTG};
156 };
157 
158 std::unique_ptr<RTDyldTimers> Timers;
159 
160 /* *** */
161 
162 using SectionIDMap = StringMap<unsigned>;
163 using FileToSectionIDMap = StringMap<SectionIDMap>;
164 
165 void dumpFileToSectionIDMap(const FileToSectionIDMap &FileToSecIDMap) {
166   for (const auto &KV : FileToSecIDMap) {
167     llvm::dbgs() << "In " << KV.first() << "\n";
168     for (auto &KV2 : KV.second)
169       llvm::dbgs() << "  \"" << KV2.first() << "\" -> " << KV2.second << "\n";
170   }
171 }
172 
173 Expected<unsigned> getSectionId(const FileToSectionIDMap &FileToSecIDMap,
174                                 StringRef FileName, StringRef SectionName) {
175   auto I = FileToSecIDMap.find(FileName);
176   if (I == FileToSecIDMap.end())
177     return make_error<StringError>("No file named " + FileName,
178                                    inconvertibleErrorCode());
179   auto &SectionIDs = I->second;
180   auto J = SectionIDs.find(SectionName);
181   if (J == SectionIDs.end())
182     return make_error<StringError>("No section named \"" + SectionName +
183                                    "\" in file " + FileName,
184                                    inconvertibleErrorCode());
185   return J->second;
186 }
187 
188 // A trivial memory manager that doesn't do anything fancy, just uses the
189 // support library allocation routines directly.
190 class TrivialMemoryManager : public RTDyldMemoryManager {
191 public:
192   struct SectionInfo {
193     SectionInfo(StringRef Name, sys::MemoryBlock MB, unsigned SectionID)
194         : Name(std::string(Name)), MB(std::move(MB)), SectionID(SectionID) {}
195     std::string Name;
196     sys::MemoryBlock MB;
197     unsigned SectionID = ~0U;
198   };
199 
200   SmallVector<SectionInfo, 16> FunctionMemory;
201   SmallVector<SectionInfo, 16> DataMemory;
202 
203   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
204                                unsigned SectionID,
205                                StringRef SectionName) override;
206   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
207                                unsigned SectionID, StringRef SectionName,
208                                bool IsReadOnly) override;
209 
210   /// If non null, records subsequent Name -> SectionID mappings.
211   void setSectionIDsMap(SectionIDMap *SecIDMap) {
212     this->SecIDMap = SecIDMap;
213   }
214 
215   void *getPointerToNamedFunction(const std::string &Name,
216                                   bool AbortOnFailure = true) override {
217     return nullptr;
218   }
219 
220   bool finalizeMemory(std::string *ErrMsg) override { return false; }
221 
222   void addDummySymbol(const std::string &Name, uint64_t Addr) {
223     DummyExterns[Name] = Addr;
224   }
225 
226   JITSymbol findSymbol(const std::string &Name) override {
227     auto I = DummyExterns.find(Name);
228 
229     if (I != DummyExterns.end())
230       return JITSymbol(I->second, JITSymbolFlags::Exported);
231 
232     if (auto Sym = RTDyldMemoryManager::findSymbol(Name))
233       return Sym;
234     else if (auto Err = Sym.takeError())
235       ExitOnErr(std::move(Err));
236     else
237       ExitOnErr(make_error<StringError>("Could not find definition for \"" +
238                                             Name + "\"",
239                                         inconvertibleErrorCode()));
240     llvm_unreachable("Should have returned or exited by now");
241   }
242 
243   void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
244                         size_t Size) override {}
245   void deregisterEHFrames() override {}
246 
247   void preallocateSlab(uint64_t Size) {
248     std::error_code EC;
249     sys::MemoryBlock MB =
250       sys::Memory::allocateMappedMemory(Size, nullptr,
251                                         sys::Memory::MF_READ |
252                                         sys::Memory::MF_WRITE,
253                                         EC);
254     if (!MB.base())
255       report_fatal_error("Can't allocate enough memory: " + EC.message());
256 
257     PreallocSlab = MB;
258     UsePreallocation = true;
259     SlabSize = Size;
260   }
261 
262   uint8_t *allocateFromSlab(uintptr_t Size, unsigned Alignment, bool isCode,
263                             StringRef SectionName, unsigned SectionID) {
264     Size = alignTo(Size, Alignment);
265     if (CurrentSlabOffset + Size > SlabSize)
266       report_fatal_error("Can't allocate enough memory. Tune --preallocate");
267 
268     uintptr_t OldSlabOffset = CurrentSlabOffset;
269     sys::MemoryBlock MB((void *)OldSlabOffset, Size);
270     if (isCode)
271       FunctionMemory.push_back(SectionInfo(SectionName, MB, SectionID));
272     else
273       DataMemory.push_back(SectionInfo(SectionName, MB, SectionID));
274     CurrentSlabOffset += Size;
275     return (uint8_t*)OldSlabOffset;
276   }
277 
278 private:
279   std::map<std::string, uint64_t> DummyExterns;
280   sys::MemoryBlock PreallocSlab;
281   bool UsePreallocation = false;
282   uintptr_t SlabSize = 0;
283   uintptr_t CurrentSlabOffset = 0;
284   SectionIDMap *SecIDMap = nullptr;
285 };
286 
287 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
288                                                    unsigned Alignment,
289                                                    unsigned SectionID,
290                                                    StringRef SectionName) {
291   if (PrintAllocationRequests)
292     outs() << "allocateCodeSection(Size = " << Size << ", Alignment = "
293            << Alignment << ", SectionName = " << SectionName << ")\n";
294 
295   if (SecIDMap)
296     (*SecIDMap)[SectionName] = SectionID;
297 
298   if (UsePreallocation)
299     return allocateFromSlab(Size, Alignment, true /* isCode */,
300                             SectionName, SectionID);
301 
302   std::error_code EC;
303   sys::MemoryBlock MB =
304     sys::Memory::allocateMappedMemory(Size, nullptr,
305                                       sys::Memory::MF_READ |
306                                       sys::Memory::MF_WRITE,
307                                       EC);
308   if (!MB.base())
309     report_fatal_error("MemoryManager allocation failed: " + EC.message());
310   FunctionMemory.push_back(SectionInfo(SectionName, MB, SectionID));
311   return (uint8_t*)MB.base();
312 }
313 
314 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
315                                                    unsigned Alignment,
316                                                    unsigned SectionID,
317                                                    StringRef SectionName,
318                                                    bool IsReadOnly) {
319   if (PrintAllocationRequests)
320     outs() << "allocateDataSection(Size = " << Size << ", Alignment = "
321            << Alignment << ", SectionName = " << SectionName << ")\n";
322 
323   if (SecIDMap)
324     (*SecIDMap)[SectionName] = SectionID;
325 
326   if (UsePreallocation)
327     return allocateFromSlab(Size, Alignment, false /* isCode */, SectionName,
328                             SectionID);
329 
330   std::error_code EC;
331   sys::MemoryBlock MB =
332     sys::Memory::allocateMappedMemory(Size, nullptr,
333                                       sys::Memory::MF_READ |
334                                       sys::Memory::MF_WRITE,
335                                       EC);
336   if (!MB.base())
337     report_fatal_error("MemoryManager allocation failed: " + EC.message());
338   DataMemory.push_back(SectionInfo(SectionName, MB, SectionID));
339   return (uint8_t*)MB.base();
340 }
341 
342 static const char *ProgramName;
343 
344 static void ErrorAndExit(const Twine &Msg) {
345   errs() << ProgramName << ": error: " << Msg << "\n";
346   exit(1);
347 }
348 
349 static void loadDylibs() {
350   for (const std::string &Dylib : Dylibs) {
351     if (!sys::fs::is_regular_file(Dylib))
352       report_fatal_error("Dylib not found: '" + Dylib + "'.");
353     std::string ErrMsg;
354     if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
355       report_fatal_error("Error loading '" + Dylib + "': " + ErrMsg);
356   }
357 }
358 
359 /* *** */
360 
361 static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
362   assert(LoadObjects || !UseDebugObj);
363 
364   // Load any dylibs requested on the command line.
365   loadDylibs();
366 
367   // If we don't have any input files, read from stdin.
368   if (!InputFileList.size())
369     InputFileList.push_back("-");
370   for (auto &File : InputFileList) {
371     // Instantiate a dynamic linker.
372     TrivialMemoryManager MemMgr;
373     RuntimeDyld Dyld(MemMgr, MemMgr);
374 
375     // Load the input memory buffer.
376 
377     ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
378         MemoryBuffer::getFileOrSTDIN(File);
379     if (std::error_code EC = InputBuffer.getError())
380       ErrorAndExit("unable to read input: '" + EC.message() + "'");
381 
382     Expected<std::unique_ptr<ObjectFile>> MaybeObj(
383       ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
384 
385     if (!MaybeObj) {
386       std::string Buf;
387       raw_string_ostream OS(Buf);
388       logAllUnhandledErrors(MaybeObj.takeError(), OS);
389       OS.flush();
390       ErrorAndExit("unable to create object file: '" + Buf + "'");
391     }
392 
393     ObjectFile &Obj = **MaybeObj;
394 
395     OwningBinary<ObjectFile> DebugObj;
396     std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = nullptr;
397     ObjectFile *SymbolObj = &Obj;
398     if (LoadObjects) {
399       // Load the object file
400       LoadedObjInfo =
401         Dyld.loadObject(Obj);
402 
403       if (Dyld.hasError())
404         ErrorAndExit(Dyld.getErrorString());
405 
406       // Resolve all the relocations we can.
407       Dyld.resolveRelocations();
408 
409       if (UseDebugObj) {
410         DebugObj = LoadedObjInfo->getObjectForDebug(Obj);
411         SymbolObj = DebugObj.getBinary();
412         LoadedObjInfo.reset();
413       }
414     }
415 
416     std::unique_ptr<DIContext> Context =
417         DWARFContext::create(*SymbolObj, LoadedObjInfo.get());
418 
419     std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
420         object::computeSymbolSizes(*SymbolObj);
421 
422     // Use symbol info to iterate functions in the object.
423     for (const auto &P : SymAddr) {
424       object::SymbolRef Sym = P.first;
425       Expected<SymbolRef::Type> TypeOrErr = Sym.getType();
426       if (!TypeOrErr) {
427         // TODO: Actually report errors helpfully.
428         consumeError(TypeOrErr.takeError());
429         continue;
430       }
431       SymbolRef::Type Type = *TypeOrErr;
432       if (Type == object::SymbolRef::ST_Function) {
433         Expected<StringRef> Name = Sym.getName();
434         if (!Name) {
435           // TODO: Actually report errors helpfully.
436           consumeError(Name.takeError());
437           continue;
438         }
439         Expected<uint64_t> AddrOrErr = Sym.getAddress();
440         if (!AddrOrErr) {
441           // TODO: Actually report errors helpfully.
442           consumeError(AddrOrErr.takeError());
443           continue;
444         }
445         uint64_t Addr = *AddrOrErr;
446 
447         object::SectionedAddress Address;
448 
449         uint64_t Size = P.second;
450         // If we're not using the debug object, compute the address of the
451         // symbol in memory (rather than that in the unrelocated object file)
452         // and use that to query the DWARFContext.
453         if (!UseDebugObj && LoadObjects) {
454           auto SecOrErr = Sym.getSection();
455           if (!SecOrErr) {
456             // TODO: Actually report errors helpfully.
457             consumeError(SecOrErr.takeError());
458             continue;
459           }
460           object::section_iterator Sec = *SecOrErr;
461           Address.SectionIndex = Sec->getIndex();
462           uint64_t SectionLoadAddress =
463             LoadedObjInfo->getSectionLoadAddress(*Sec);
464           if (SectionLoadAddress != 0)
465             Addr += SectionLoadAddress - Sec->getAddress();
466         } else if (auto SecOrErr = Sym.getSection())
467           Address.SectionIndex = SecOrErr.get()->getIndex();
468 
469         outs() << "Function: " << *Name << ", Size = " << Size
470                << ", Addr = " << Addr << "\n";
471 
472         Address.Address = Addr;
473         DILineInfoTable Lines =
474             Context->getLineInfoForAddressRange(Address, Size);
475         for (auto &D : Lines) {
476           outs() << "  Line info @ " << D.first - Addr << ": "
477                  << D.second.FileName << ", line:" << D.second.Line << "\n";
478         }
479       }
480     }
481   }
482 
483   return 0;
484 }
485 
486 static void doPreallocation(TrivialMemoryManager &MemMgr) {
487   // Allocate a slab of memory upfront, if required. This is used if
488   // we want to test small code models.
489   if (static_cast<intptr_t>(PreallocMemory) < 0)
490     report_fatal_error("Pre-allocated bytes of memory must be a positive integer.");
491 
492   // FIXME: Limit the amount of memory that can be preallocated?
493   if (PreallocMemory != 0)
494     MemMgr.preallocateSlab(PreallocMemory);
495 }
496 
497 static int executeInput() {
498   // Load any dylibs requested on the command line.
499   loadDylibs();
500 
501   // Instantiate a dynamic linker.
502   TrivialMemoryManager MemMgr;
503   doPreallocation(MemMgr);
504   RuntimeDyld Dyld(MemMgr, MemMgr);
505 
506   // If we don't have any input files, read from stdin.
507   if (!InputFileList.size())
508     InputFileList.push_back("-");
509   {
510     TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr);
511     for (auto &File : InputFileList) {
512       // Load the input memory buffer.
513       ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
514           MemoryBuffer::getFileOrSTDIN(File);
515       if (std::error_code EC = InputBuffer.getError())
516         ErrorAndExit("unable to read input: '" + EC.message() + "'");
517       Expected<std::unique_ptr<ObjectFile>> MaybeObj(
518           ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
519 
520       if (!MaybeObj) {
521         std::string Buf;
522         raw_string_ostream OS(Buf);
523         logAllUnhandledErrors(MaybeObj.takeError(), OS);
524         OS.flush();
525         ErrorAndExit("unable to create object file: '" + Buf + "'");
526       }
527 
528       ObjectFile &Obj = **MaybeObj;
529 
530       // Load the object file
531       Dyld.loadObject(Obj);
532       if (Dyld.hasError()) {
533         ErrorAndExit(Dyld.getErrorString());
534       }
535     }
536   }
537 
538   {
539     TimeRegion TR(Timers ? &Timers->LinkTimer : nullptr);
540     // Resove all the relocations we can.
541     // FIXME: Error out if there are unresolved relocations.
542     Dyld.resolveRelocations();
543   }
544 
545   // Get the address of the entry point (_main by default).
546   void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint);
547   if (!MainAddress)
548     ErrorAndExit("no definition for '" + EntryPoint + "'");
549 
550   // Invalidate the instruction cache for each loaded function.
551   for (auto &FM : MemMgr.FunctionMemory) {
552 
553     auto &FM_MB = FM.MB;
554 
555     // Make sure the memory is executable.
556     // setExecutable will call InvalidateInstructionCache.
557     if (auto EC = sys::Memory::protectMappedMemory(FM_MB,
558                                                    sys::Memory::MF_READ |
559                                                    sys::Memory::MF_EXEC))
560       ErrorAndExit("unable to mark function executable: '" + EC.message() +
561                    "'");
562   }
563 
564   // Dispatch to _main().
565   errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
566 
567   int (*Main)(int, const char**) =
568     (int(*)(int,const char**)) uintptr_t(MainAddress);
569   std::vector<const char *> Argv;
570   // Use the name of the first input object module as argv[0] for the target.
571   Argv.push_back(InputFileList[0].data());
572   for (auto &Arg : InputArgv)
573     Argv.push_back(Arg.data());
574   Argv.push_back(nullptr);
575   int Result = 0;
576   {
577     TimeRegion TR(Timers ? &Timers->RunTimer : nullptr);
578     Result = Main(Argv.size() - 1, Argv.data());
579   }
580 
581   return Result;
582 }
583 
584 static int checkAllExpressions(RuntimeDyldChecker &Checker) {
585   for (const auto& CheckerFileName : CheckFiles) {
586     ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
587         MemoryBuffer::getFileOrSTDIN(CheckerFileName);
588     if (std::error_code EC = CheckerFileBuf.getError())
589       ErrorAndExit("unable to read input '" + CheckerFileName + "': " +
590                    EC.message());
591 
592     if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
593                                        CheckerFileBuf.get().get()))
594       ErrorAndExit("some checks in '" + CheckerFileName + "' failed");
595   }
596   return 0;
597 }
598 
599 void applySpecificSectionMappings(RuntimeDyld &Dyld,
600                                   const FileToSectionIDMap &FileToSecIDMap) {
601 
602   for (StringRef Mapping : SpecificSectionMappings) {
603     size_t EqualsIdx = Mapping.find_first_of("=");
604     std::string SectionIDStr = std::string(Mapping.substr(0, EqualsIdx));
605     size_t ComaIdx = Mapping.find_first_of(",");
606 
607     if (ComaIdx == StringRef::npos)
608       report_fatal_error("Invalid section specification '" + Mapping +
609                          "'. Should be '<file name>,<section name>=<addr>'");
610 
611     std::string FileName = SectionIDStr.substr(0, ComaIdx);
612     std::string SectionName = SectionIDStr.substr(ComaIdx + 1);
613     unsigned SectionID =
614       ExitOnErr(getSectionId(FileToSecIDMap, FileName, SectionName));
615 
616     auto* OldAddr = Dyld.getSectionContent(SectionID).data();
617     std::string NewAddrStr = std::string(Mapping.substr(EqualsIdx + 1));
618     uint64_t NewAddr;
619 
620     if (StringRef(NewAddrStr).getAsInteger(0, NewAddr))
621       report_fatal_error("Invalid section address in mapping '" + Mapping +
622                          "'.");
623 
624     Dyld.mapSectionAddress(OldAddr, NewAddr);
625   }
626 }
627 
628 // Scatter sections in all directions!
629 // Remaps section addresses for -verify mode. The following command line options
630 // can be used to customize the layout of the memory within the phony target's
631 // address space:
632 // -target-addr-start <s> -- Specify where the phony target address range starts.
633 // -target-addr-end   <e> -- Specify where the phony target address range ends.
634 // -target-section-sep <d> -- Specify how big a gap should be left between the
635 //                            end of one section and the start of the next.
636 //                            Defaults to zero. Set to something big
637 //                            (e.g. 1 << 32) to stress-test stubs, GOTs, etc.
638 //
639 static void remapSectionsAndSymbols(const llvm::Triple &TargetTriple,
640                                     RuntimeDyld &Dyld,
641                                     TrivialMemoryManager &MemMgr) {
642 
643   // Set up a work list (section addr/size pairs).
644   typedef std::list<const TrivialMemoryManager::SectionInfo*> WorklistT;
645   WorklistT Worklist;
646 
647   for (const auto& CodeSection : MemMgr.FunctionMemory)
648     Worklist.push_back(&CodeSection);
649   for (const auto& DataSection : MemMgr.DataMemory)
650     Worklist.push_back(&DataSection);
651 
652   // Keep an "already allocated" mapping of section target addresses to sizes.
653   // Sections whose address mappings aren't specified on the command line will
654   // allocated around the explicitly mapped sections while maintaining the
655   // minimum separation.
656   std::map<uint64_t, uint64_t> AlreadyAllocated;
657 
658   // Move the previously applied mappings (whether explicitly specified on the
659   // command line, or implicitly set by RuntimeDyld) into the already-allocated
660   // map.
661   for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end();
662        I != E;) {
663     WorklistT::iterator Tmp = I;
664     ++I;
665 
666     auto LoadAddr = Dyld.getSectionLoadAddress((*Tmp)->SectionID);
667 
668     if (LoadAddr != static_cast<uint64_t>(
669           reinterpret_cast<uintptr_t>((*Tmp)->MB.base()))) {
670       // A section will have a LoadAddr of 0 if it wasn't loaded for whatever
671       // reason (e.g. zero byte COFF sections). Don't include those sections in
672       // the allocation map.
673       if (LoadAddr != 0)
674         AlreadyAllocated[LoadAddr] = (*Tmp)->MB.allocatedSize();
675       Worklist.erase(Tmp);
676     }
677   }
678 
679   // If the -target-addr-end option wasn't explicitly passed, then set it to a
680   // sensible default based on the target triple.
681   if (TargetAddrEnd.getNumOccurrences() == 0) {
682     if (TargetTriple.isArch16Bit())
683       TargetAddrEnd = (1ULL << 16) - 1;
684     else if (TargetTriple.isArch32Bit())
685       TargetAddrEnd = (1ULL << 32) - 1;
686     // TargetAddrEnd already has a sensible default for 64-bit systems, so
687     // there's nothing to do in the 64-bit case.
688   }
689 
690   // Process any elements remaining in the worklist.
691   while (!Worklist.empty()) {
692     auto *CurEntry = Worklist.front();
693     Worklist.pop_front();
694 
695     uint64_t NextSectionAddr = TargetAddrStart;
696 
697     for (const auto &Alloc : AlreadyAllocated)
698       if (NextSectionAddr + CurEntry->MB.allocatedSize() + TargetSectionSep <=
699           Alloc.first)
700         break;
701       else
702         NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep;
703 
704     Dyld.mapSectionAddress(CurEntry->MB.base(), NextSectionAddr);
705     AlreadyAllocated[NextSectionAddr] = CurEntry->MB.allocatedSize();
706   }
707 
708   // Add dummy symbols to the memory manager.
709   for (const auto &Mapping : DummySymbolMappings) {
710     size_t EqualsIdx = Mapping.find_first_of('=');
711 
712     if (EqualsIdx == StringRef::npos)
713       report_fatal_error("Invalid dummy symbol specification '" + Mapping +
714                          "'. Should be '<symbol name>=<addr>'");
715 
716     std::string Symbol = Mapping.substr(0, EqualsIdx);
717     std::string AddrStr = Mapping.substr(EqualsIdx + 1);
718 
719     uint64_t Addr;
720     if (StringRef(AddrStr).getAsInteger(0, Addr))
721       report_fatal_error("Invalid symbol mapping '" + Mapping + "'.");
722 
723     MemMgr.addDummySymbol(Symbol, Addr);
724   }
725 }
726 
727 // Load and link the objects specified on the command line, but do not execute
728 // anything. Instead, attach a RuntimeDyldChecker instance and call it to
729 // verify the correctness of the linked memory.
730 static int linkAndVerify() {
731 
732   // Check for missing triple.
733   if (TripleName == "")
734     ErrorAndExit("-triple required when running in -verify mode.");
735 
736   // Look up the target and build the disassembler.
737   Triple TheTriple(Triple::normalize(TripleName));
738   std::string ErrorStr;
739   const Target *TheTarget =
740     TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
741   if (!TheTarget)
742     ErrorAndExit("Error accessing target '" + TripleName + "': " + ErrorStr);
743 
744   TripleName = TheTriple.getTriple();
745 
746   std::unique_ptr<MCSubtargetInfo> STI(
747     TheTarget->createMCSubtargetInfo(TripleName, MCPU, ""));
748   if (!STI)
749     ErrorAndExit("Unable to create subtarget info!");
750 
751   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
752   if (!MRI)
753     ErrorAndExit("Unable to create target register info!");
754 
755   MCTargetOptions MCOptions;
756   std::unique_ptr<MCAsmInfo> MAI(
757       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
758   if (!MAI)
759     ErrorAndExit("Unable to create target asm info!");
760 
761   MCContext Ctx(Triple(TripleName), MAI.get(), MRI.get(), STI.get());
762 
763   std::unique_ptr<MCDisassembler> Disassembler(
764     TheTarget->createMCDisassembler(*STI, Ctx));
765   if (!Disassembler)
766     ErrorAndExit("Unable to create disassembler!");
767 
768   std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
769   if (!MII)
770     ErrorAndExit("Unable to create target instruction info!");
771 
772   std::unique_ptr<MCInstPrinter> InstPrinter(
773       TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
774 
775   // Load any dylibs requested on the command line.
776   loadDylibs();
777 
778   // Instantiate a dynamic linker.
779   TrivialMemoryManager MemMgr;
780   doPreallocation(MemMgr);
781 
782   struct StubID {
783     unsigned SectionID;
784     uint32_t Offset;
785   };
786   using StubInfos = StringMap<StubID>;
787   using StubContainers = StringMap<StubInfos>;
788 
789   StubContainers StubMap;
790   RuntimeDyld Dyld(MemMgr, MemMgr);
791   Dyld.setProcessAllSections(true);
792 
793   Dyld.setNotifyStubEmitted([&StubMap](StringRef FilePath,
794                                        StringRef SectionName,
795                                        StringRef SymbolName, unsigned SectionID,
796                                        uint32_t StubOffset) {
797     std::string ContainerName =
798         (sys::path::filename(FilePath) + "/" + SectionName).str();
799     StubMap[ContainerName][SymbolName] = {SectionID, StubOffset};
800   });
801 
802   auto GetSymbolInfo =
803       [&Dyld, &MemMgr](
804           StringRef Symbol) -> Expected<RuntimeDyldChecker::MemoryRegionInfo> {
805     RuntimeDyldChecker::MemoryRegionInfo SymInfo;
806 
807     // First get the target address.
808     if (auto InternalSymbol = Dyld.getSymbol(Symbol))
809       SymInfo.setTargetAddress(InternalSymbol.getAddress());
810     else {
811       // Symbol not found in RuntimeDyld. Fall back to external lookup.
812 #ifdef _MSC_VER
813       using ExpectedLookupResult =
814           MSVCPExpected<JITSymbolResolver::LookupResult>;
815 #else
816       using ExpectedLookupResult = Expected<JITSymbolResolver::LookupResult>;
817 #endif
818 
819       auto ResultP = std::make_shared<std::promise<ExpectedLookupResult>>();
820       auto ResultF = ResultP->get_future();
821 
822       MemMgr.lookup(JITSymbolResolver::LookupSet({Symbol}),
823                     [=](Expected<JITSymbolResolver::LookupResult> Result) {
824                       ResultP->set_value(std::move(Result));
825                     });
826 
827       auto Result = ResultF.get();
828       if (!Result)
829         return Result.takeError();
830 
831       auto I = Result->find(Symbol);
832       assert(I != Result->end() &&
833              "Expected symbol address if no error occurred");
834       SymInfo.setTargetAddress(I->second.getAddress());
835     }
836 
837     // Now find the symbol content if possible (otherwise leave content as a
838     // default-constructed StringRef).
839     if (auto *SymAddr = Dyld.getSymbolLocalAddress(Symbol)) {
840       unsigned SectionID = Dyld.getSymbolSectionID(Symbol);
841       if (SectionID != ~0U) {
842         char *CSymAddr = static_cast<char *>(SymAddr);
843         StringRef SecContent = Dyld.getSectionContent(SectionID);
844         uint64_t SymSize = SecContent.size() - (CSymAddr - SecContent.data());
845         SymInfo.setContent(ArrayRef<char>(CSymAddr, SymSize));
846       }
847     }
848     return SymInfo;
849   };
850 
851   auto IsSymbolValid = [&Dyld, GetSymbolInfo](StringRef Symbol) {
852     if (Dyld.getSymbol(Symbol))
853       return true;
854     auto SymInfo = GetSymbolInfo(Symbol);
855     if (!SymInfo) {
856       logAllUnhandledErrors(SymInfo.takeError(), errs(), "RTDyldChecker: ");
857       return false;
858     }
859     return SymInfo->getTargetAddress() != 0;
860   };
861 
862   FileToSectionIDMap FileToSecIDMap;
863 
864   auto GetSectionInfo = [&Dyld, &FileToSecIDMap](StringRef FileName,
865                                                  StringRef SectionName)
866       -> Expected<RuntimeDyldChecker::MemoryRegionInfo> {
867     auto SectionID = getSectionId(FileToSecIDMap, FileName, SectionName);
868     if (!SectionID)
869       return SectionID.takeError();
870     RuntimeDyldChecker::MemoryRegionInfo SecInfo;
871     SecInfo.setTargetAddress(Dyld.getSectionLoadAddress(*SectionID));
872     StringRef SecContent = Dyld.getSectionContent(*SectionID);
873     SecInfo.setContent(ArrayRef<char>(SecContent.data(), SecContent.size()));
874     return SecInfo;
875   };
876 
877   auto GetStubInfo = [&Dyld, &StubMap](StringRef StubContainer,
878                                        StringRef SymbolName)
879       -> Expected<RuntimeDyldChecker::MemoryRegionInfo> {
880     if (!StubMap.count(StubContainer))
881       return make_error<StringError>("Stub container not found: " +
882                                          StubContainer,
883                                      inconvertibleErrorCode());
884     if (!StubMap[StubContainer].count(SymbolName))
885       return make_error<StringError>("Symbol name " + SymbolName +
886                                          " in stub container " + StubContainer,
887                                      inconvertibleErrorCode());
888     auto &SI = StubMap[StubContainer][SymbolName];
889     RuntimeDyldChecker::MemoryRegionInfo StubMemInfo;
890     StubMemInfo.setTargetAddress(Dyld.getSectionLoadAddress(SI.SectionID) +
891                                  SI.Offset);
892     StringRef SecContent =
893         Dyld.getSectionContent(SI.SectionID).substr(SI.Offset);
894     StubMemInfo.setContent(
895         ArrayRef<char>(SecContent.data(), SecContent.size()));
896     return StubMemInfo;
897   };
898 
899   // We will initialize this below once we have the first object file and can
900   // know the endianness.
901   std::unique_ptr<RuntimeDyldChecker> Checker;
902 
903   // If we don't have any input files, read from stdin.
904   if (!InputFileList.size())
905     InputFileList.push_back("-");
906   for (auto &InputFile : InputFileList) {
907     // Load the input memory buffer.
908     ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
909         MemoryBuffer::getFileOrSTDIN(InputFile);
910 
911     if (std::error_code EC = InputBuffer.getError())
912       ErrorAndExit("unable to read input: '" + EC.message() + "'");
913 
914     Expected<std::unique_ptr<ObjectFile>> MaybeObj(
915       ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
916 
917     if (!MaybeObj) {
918       std::string Buf;
919       raw_string_ostream OS(Buf);
920       logAllUnhandledErrors(MaybeObj.takeError(), OS);
921       OS.flush();
922       ErrorAndExit("unable to create object file: '" + Buf + "'");
923     }
924 
925     ObjectFile &Obj = **MaybeObj;
926 
927     if (!Checker)
928       Checker = std::make_unique<RuntimeDyldChecker>(
929           IsSymbolValid, GetSymbolInfo, GetSectionInfo, GetStubInfo,
930           GetStubInfo, Obj.isLittleEndian() ? support::little : support::big,
931           Disassembler.get(), InstPrinter.get(), dbgs());
932 
933     auto FileName = sys::path::filename(InputFile);
934     MemMgr.setSectionIDsMap(&FileToSecIDMap[FileName]);
935 
936     // Load the object file
937     Dyld.loadObject(Obj);
938     if (Dyld.hasError()) {
939       ErrorAndExit(Dyld.getErrorString());
940     }
941   }
942 
943   // Re-map the section addresses into the phony target address space and add
944   // dummy symbols.
945   applySpecificSectionMappings(Dyld, FileToSecIDMap);
946   remapSectionsAndSymbols(TheTriple, Dyld, MemMgr);
947 
948   // Resolve all the relocations we can.
949   Dyld.resolveRelocations();
950 
951   // Register EH frames.
952   Dyld.registerEHFrames();
953 
954   int ErrorCode = checkAllExpressions(*Checker);
955   if (Dyld.hasError())
956     ErrorAndExit("RTDyld reported an error applying relocations:\n  " +
957                  Dyld.getErrorString());
958 
959   return ErrorCode;
960 }
961 
962 int main(int argc, char **argv) {
963   InitLLVM X(argc, argv);
964   ProgramName = argv[0];
965 
966   llvm::InitializeAllTargetInfos();
967   llvm::InitializeAllTargetMCs();
968   llvm::InitializeAllDisassemblers();
969 
970   cl::HideUnrelatedOptions({&RTDyldCategory, &getColorCategory()});
971   cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
972 
973   ExitOnErr.setBanner(std::string(argv[0]) + ": ");
974 
975   Timers = ShowTimes ? std::make_unique<RTDyldTimers>() : nullptr;
976 
977   int Result;
978   switch (Action) {
979   case AC_Execute:
980     Result = executeInput();
981     break;
982   case AC_PrintDebugLineInfo:
983     Result =
984         printLineInfoForInput(/* LoadObjects */ true, /* UseDebugObj */ true);
985     break;
986   case AC_PrintLineInfo:
987     Result =
988         printLineInfoForInput(/* LoadObjects */ true, /* UseDebugObj */ false);
989     break;
990   case AC_PrintObjectLineInfo:
991     Result =
992         printLineInfoForInput(/* LoadObjects */ false, /* UseDebugObj */ false);
993     break;
994   case AC_Verify:
995     Result = linkAndVerify();
996     break;
997   }
998   return Result;
999 }
1000