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