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