Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / tools / llvm-rtdyld / llvm-rtdyld.cpp
blobe792b7257ea5e877b19c50f98a640c0ef668ef54
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/Memory.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/TargetSelect.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <list>
37 using namespace llvm;
38 using namespace llvm::object;
40 static cl::list<std::string>
41 InputFileList(cl::Positional, cl::ZeroOrMore,
42 cl::desc("<input files>"));
44 enum ActionType {
45 AC_Execute,
46 AC_PrintObjectLineInfo,
47 AC_PrintLineInfo,
48 AC_PrintDebugLineInfo,
49 AC_Verify
52 static cl::opt<ActionType>
53 Action(cl::desc("Action to perform:"),
54 cl::init(AC_Execute),
55 cl::values(clEnumValN(AC_Execute, "execute",
56 "Load, link, and execute the inputs."),
57 clEnumValN(AC_PrintLineInfo, "printline",
58 "Load, link, and print line information for each function."),
59 clEnumValN(AC_PrintDebugLineInfo, "printdebugline",
60 "Load, link, and print line information for each function using the debug object"),
61 clEnumValN(AC_PrintObjectLineInfo, "printobjline",
62 "Like -printlineinfo but does not load the object first"),
63 clEnumValN(AC_Verify, "verify",
64 "Load, link and verify the resulting memory image.")));
66 static cl::opt<std::string>
67 EntryPoint("entry",
68 cl::desc("Function to call as entry point."),
69 cl::init("_main"));
71 static cl::list<std::string>
72 Dylibs("dylib",
73 cl::desc("Add library."),
74 cl::ZeroOrMore);
76 static cl::opt<std::string>
77 TripleName("triple", cl::desc("Target triple for disassembler"));
79 static cl::opt<std::string>
80 MCPU("mcpu",
81 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
82 cl::value_desc("cpu-name"),
83 cl::init(""));
85 static cl::list<std::string>
86 CheckFiles("check",
87 cl::desc("File containing RuntimeDyld verifier checks."),
88 cl::ZeroOrMore);
90 // Tracking BUG: 19665
91 // http://llvm.org/bugs/show_bug.cgi?id=19665
93 // Do not change these options to cl::opt<uint64_t> since this silently breaks
94 // argument parsing.
95 static cl::opt<unsigned long long>
96 PreallocMemory("preallocate",
97 cl::desc("Allocate memory upfront rather than on-demand"),
98 cl::init(0));
100 static cl::opt<unsigned long long>
101 TargetAddrStart("target-addr-start",
102 cl::desc("For -verify only: start of phony target address "
103 "range."),
104 cl::init(4096), // Start at "page 1" - no allocating at "null".
105 cl::Hidden);
107 static cl::opt<unsigned long long>
108 TargetAddrEnd("target-addr-end",
109 cl::desc("For -verify only: end of phony target address range."),
110 cl::init(~0ULL),
111 cl::Hidden);
113 static cl::opt<unsigned long long>
114 TargetSectionSep("target-section-sep",
115 cl::desc("For -verify only: Separation between sections in "
116 "phony target address space."),
117 cl::init(0),
118 cl::Hidden);
120 static cl::list<std::string>
121 SpecificSectionMappings("map-section",
122 cl::desc("For -verify only: Map a section to a "
123 "specific address."),
124 cl::ZeroOrMore,
125 cl::Hidden);
127 static cl::list<std::string>
128 DummySymbolMappings("dummy-extern",
129 cl::desc("For -verify only: Inject a symbol into the extern "
130 "symbol table."),
131 cl::ZeroOrMore,
132 cl::Hidden);
134 static cl::opt<bool>
135 PrintAllocationRequests("print-alloc-requests",
136 cl::desc("Print allocation requests made to the memory "
137 "manager by RuntimeDyld"),
138 cl::Hidden);
140 /* *** */
142 // A trivial memory manager that doesn't do anything fancy, just uses the
143 // support library allocation routines directly.
144 class TrivialMemoryManager : public RTDyldMemoryManager {
145 public:
146 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
147 SmallVector<sys::MemoryBlock, 16> DataMemory;
149 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
150 unsigned SectionID,
151 StringRef SectionName) override;
152 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
153 unsigned SectionID, StringRef SectionName,
154 bool IsReadOnly) override;
156 void *getPointerToNamedFunction(const std::string &Name,
157 bool AbortOnFailure = true) override {
158 return nullptr;
161 bool finalizeMemory(std::string *ErrMsg) override { return false; }
163 void addDummySymbol(const std::string &Name, uint64_t Addr) {
164 DummyExterns[Name] = Addr;
167 JITSymbol findSymbol(const std::string &Name) override {
168 auto I = DummyExterns.find(Name);
170 if (I != DummyExterns.end())
171 return JITSymbol(I->second, JITSymbolFlags::Exported);
173 return RTDyldMemoryManager::findSymbol(Name);
176 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
177 size_t Size) override {}
178 void deregisterEHFrames() override {}
180 void preallocateSlab(uint64_t Size) {
181 std::error_code EC;
182 sys::MemoryBlock MB =
183 sys::Memory::allocateMappedMemory(Size, nullptr,
184 sys::Memory::MF_READ |
185 sys::Memory::MF_WRITE,
186 EC);
187 if (!MB.base())
188 report_fatal_error("Can't allocate enough memory: " + EC.message());
190 PreallocSlab = MB;
191 UsePreallocation = true;
192 SlabSize = Size;
195 uint8_t *allocateFromSlab(uintptr_t Size, unsigned Alignment, bool isCode) {
196 Size = alignTo(Size, Alignment);
197 if (CurrentSlabOffset + Size > SlabSize)
198 report_fatal_error("Can't allocate enough memory. Tune --preallocate");
200 uintptr_t OldSlabOffset = CurrentSlabOffset;
201 sys::MemoryBlock MB((void *)OldSlabOffset, Size);
202 if (isCode)
203 FunctionMemory.push_back(MB);
204 else
205 DataMemory.push_back(MB);
206 CurrentSlabOffset += Size;
207 return (uint8_t*)OldSlabOffset;
210 private:
211 std::map<std::string, uint64_t> DummyExterns;
212 sys::MemoryBlock PreallocSlab;
213 bool UsePreallocation = false;
214 uintptr_t SlabSize = 0;
215 uintptr_t CurrentSlabOffset = 0;
218 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
219 unsigned Alignment,
220 unsigned SectionID,
221 StringRef SectionName) {
222 if (PrintAllocationRequests)
223 outs() << "allocateCodeSection(Size = " << Size << ", Alignment = "
224 << Alignment << ", SectionName = " << SectionName << ")\n";
226 if (UsePreallocation)
227 return allocateFromSlab(Size, Alignment, true /* isCode */);
229 std::error_code EC;
230 sys::MemoryBlock MB =
231 sys::Memory::allocateMappedMemory(Size, nullptr,
232 sys::Memory::MF_READ |
233 sys::Memory::MF_WRITE,
234 EC);
235 if (!MB.base())
236 report_fatal_error("MemoryManager allocation failed: " + EC.message());
237 FunctionMemory.push_back(MB);
238 return (uint8_t*)MB.base();
241 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
242 unsigned Alignment,
243 unsigned SectionID,
244 StringRef SectionName,
245 bool IsReadOnly) {
246 if (PrintAllocationRequests)
247 outs() << "allocateDataSection(Size = " << Size << ", Alignment = "
248 << Alignment << ", SectionName = " << SectionName << ")\n";
250 if (UsePreallocation)
251 return allocateFromSlab(Size, Alignment, false /* isCode */);
253 std::error_code EC;
254 sys::MemoryBlock MB =
255 sys::Memory::allocateMappedMemory(Size, nullptr,
256 sys::Memory::MF_READ |
257 sys::Memory::MF_WRITE,
258 EC);
259 if (!MB.base())
260 report_fatal_error("MemoryManager allocation failed: " + EC.message());
261 DataMemory.push_back(MB);
262 return (uint8_t*)MB.base();
265 static const char *ProgramName;
267 static void ErrorAndExit(const Twine &Msg) {
268 errs() << ProgramName << ": error: " << Msg << "\n";
269 exit(1);
272 static void loadDylibs() {
273 for (const std::string &Dylib : Dylibs) {
274 if (!sys::fs::is_regular_file(Dylib))
275 report_fatal_error("Dylib not found: '" + Dylib + "'.");
276 std::string ErrMsg;
277 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
278 report_fatal_error("Error loading '" + Dylib + "': " + ErrMsg);
282 /* *** */
284 static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
285 assert(LoadObjects || !UseDebugObj);
287 // Load any dylibs requested on the command line.
288 loadDylibs();
290 // If we don't have any input files, read from stdin.
291 if (!InputFileList.size())
292 InputFileList.push_back("-");
293 for (auto &File : InputFileList) {
294 // Instantiate a dynamic linker.
295 TrivialMemoryManager MemMgr;
296 RuntimeDyld Dyld(MemMgr, MemMgr);
298 // Load the input memory buffer.
300 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
301 MemoryBuffer::getFileOrSTDIN(File);
302 if (std::error_code EC = InputBuffer.getError())
303 ErrorAndExit("unable to read input: '" + EC.message() + "'");
305 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
306 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
308 if (!MaybeObj) {
309 std::string Buf;
310 raw_string_ostream OS(Buf);
311 logAllUnhandledErrors(MaybeObj.takeError(), OS);
312 OS.flush();
313 ErrorAndExit("unable to create object file: '" + Buf + "'");
316 ObjectFile &Obj = **MaybeObj;
318 OwningBinary<ObjectFile> DebugObj;
319 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = nullptr;
320 ObjectFile *SymbolObj = &Obj;
321 if (LoadObjects) {
322 // Load the object file
323 LoadedObjInfo =
324 Dyld.loadObject(Obj);
326 if (Dyld.hasError())
327 ErrorAndExit(Dyld.getErrorString());
329 // Resolve all the relocations we can.
330 Dyld.resolveRelocations();
332 if (UseDebugObj) {
333 DebugObj = LoadedObjInfo->getObjectForDebug(Obj);
334 SymbolObj = DebugObj.getBinary();
335 LoadedObjInfo.reset();
339 std::unique_ptr<DIContext> Context =
340 DWARFContext::create(*SymbolObj, LoadedObjInfo.get());
342 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
343 object::computeSymbolSizes(*SymbolObj);
345 // Use symbol info to iterate functions in the object.
346 for (const auto &P : SymAddr) {
347 object::SymbolRef Sym = P.first;
348 Expected<SymbolRef::Type> TypeOrErr = Sym.getType();
349 if (!TypeOrErr) {
350 // TODO: Actually report errors helpfully.
351 consumeError(TypeOrErr.takeError());
352 continue;
354 SymbolRef::Type Type = *TypeOrErr;
355 if (Type == object::SymbolRef::ST_Function) {
356 Expected<StringRef> Name = Sym.getName();
357 if (!Name) {
358 // TODO: Actually report errors helpfully.
359 consumeError(Name.takeError());
360 continue;
362 Expected<uint64_t> AddrOrErr = Sym.getAddress();
363 if (!AddrOrErr) {
364 // TODO: Actually report errors helpfully.
365 consumeError(AddrOrErr.takeError());
366 continue;
368 uint64_t Addr = *AddrOrErr;
370 uint64_t Size = P.second;
371 // If we're not using the debug object, compute the address of the
372 // symbol in memory (rather than that in the unrelocated object file)
373 // and use that to query the DWARFContext.
374 if (!UseDebugObj && LoadObjects) {
375 auto SecOrErr = Sym.getSection();
376 if (!SecOrErr) {
377 // TODO: Actually report errors helpfully.
378 consumeError(SecOrErr.takeError());
379 continue;
381 object::section_iterator Sec = *SecOrErr;
382 StringRef SecName;
383 Sec->getName(SecName);
384 uint64_t SectionLoadAddress =
385 LoadedObjInfo->getSectionLoadAddress(*Sec);
386 if (SectionLoadAddress != 0)
387 Addr += SectionLoadAddress - Sec->getAddress();
390 outs() << "Function: " << *Name << ", Size = " << Size
391 << ", Addr = " << Addr << "\n";
393 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
394 for (auto &D : Lines) {
395 outs() << " Line info @ " << D.first - Addr << ": "
396 << D.second.FileName << ", line:" << D.second.Line << "\n";
402 return 0;
405 static void doPreallocation(TrivialMemoryManager &MemMgr) {
406 // Allocate a slab of memory upfront, if required. This is used if
407 // we want to test small code models.
408 if (static_cast<intptr_t>(PreallocMemory) < 0)
409 report_fatal_error("Pre-allocated bytes of memory must be a positive integer.");
411 // FIXME: Limit the amount of memory that can be preallocated?
412 if (PreallocMemory != 0)
413 MemMgr.preallocateSlab(PreallocMemory);
416 static int executeInput() {
417 // Load any dylibs requested on the command line.
418 loadDylibs();
420 // Instantiate a dynamic linker.
421 TrivialMemoryManager MemMgr;
422 doPreallocation(MemMgr);
423 RuntimeDyld Dyld(MemMgr, MemMgr);
425 // If we don't have any input files, read from stdin.
426 if (!InputFileList.size())
427 InputFileList.push_back("-");
428 for (auto &File : InputFileList) {
429 // Load the input memory buffer.
430 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
431 MemoryBuffer::getFileOrSTDIN(File);
432 if (std::error_code EC = InputBuffer.getError())
433 ErrorAndExit("unable to read input: '" + EC.message() + "'");
434 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
435 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
437 if (!MaybeObj) {
438 std::string Buf;
439 raw_string_ostream OS(Buf);
440 logAllUnhandledErrors(MaybeObj.takeError(), OS);
441 OS.flush();
442 ErrorAndExit("unable to create object file: '" + Buf + "'");
445 ObjectFile &Obj = **MaybeObj;
447 // Load the object file
448 Dyld.loadObject(Obj);
449 if (Dyld.hasError()) {
450 ErrorAndExit(Dyld.getErrorString());
454 // Resove all the relocations we can.
455 // FIXME: Error out if there are unresolved relocations.
456 Dyld.resolveRelocations();
458 // Get the address of the entry point (_main by default).
459 void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint);
460 if (!MainAddress)
461 ErrorAndExit("no definition for '" + EntryPoint + "'");
463 // Invalidate the instruction cache for each loaded function.
464 for (auto &FM : MemMgr.FunctionMemory) {
466 // Make sure the memory is executable.
467 // setExecutable will call InvalidateInstructionCache.
468 if (auto EC = sys::Memory::protectMappedMemory(FM,
469 sys::Memory::MF_READ |
470 sys::Memory::MF_EXEC))
471 ErrorAndExit("unable to mark function executable: '" + EC.message() +
472 "'");
475 // Dispatch to _main().
476 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
478 int (*Main)(int, const char**) =
479 (int(*)(int,const char**)) uintptr_t(MainAddress);
480 const char **Argv = new const char*[2];
481 // Use the name of the first input object module as argv[0] for the target.
482 Argv[0] = InputFileList[0].c_str();
483 Argv[1] = nullptr;
484 return Main(1, Argv);
487 static int checkAllExpressions(RuntimeDyldChecker &Checker) {
488 for (const auto& CheckerFileName : CheckFiles) {
489 ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
490 MemoryBuffer::getFileOrSTDIN(CheckerFileName);
491 if (std::error_code EC = CheckerFileBuf.getError())
492 ErrorAndExit("unable to read input '" + CheckerFileName + "': " +
493 EC.message());
495 if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
496 CheckerFileBuf.get().get()))
497 ErrorAndExit("some checks in '" + CheckerFileName + "' failed");
499 return 0;
502 void applySpecificSectionMappings(RuntimeDyldChecker &Checker) {
504 for (StringRef Mapping : SpecificSectionMappings) {
506 size_t EqualsIdx = Mapping.find_first_of("=");
507 std::string SectionIDStr = Mapping.substr(0, EqualsIdx);
508 size_t ComaIdx = Mapping.find_first_of(",");
510 if (ComaIdx == StringRef::npos)
511 report_fatal_error("Invalid section specification '" + Mapping +
512 "'. Should be '<file name>,<section name>=<addr>'");
514 std::string FileName = SectionIDStr.substr(0, ComaIdx);
515 std::string SectionName = SectionIDStr.substr(ComaIdx + 1);
517 uint64_t OldAddrInt;
518 std::string ErrorMsg;
519 std::tie(OldAddrInt, ErrorMsg) =
520 Checker.getSectionAddr(FileName, SectionName, true);
522 if (ErrorMsg != "")
523 report_fatal_error(ErrorMsg);
525 void* OldAddr = reinterpret_cast<void*>(static_cast<uintptr_t>(OldAddrInt));
527 std::string NewAddrStr = Mapping.substr(EqualsIdx + 1);
528 uint64_t NewAddr;
530 if (StringRef(NewAddrStr).getAsInteger(0, NewAddr))
531 report_fatal_error("Invalid section address in mapping '" + Mapping +
532 "'.");
534 Checker.getRTDyld().mapSectionAddress(OldAddr, NewAddr);
538 // Scatter sections in all directions!
539 // Remaps section addresses for -verify mode. The following command line options
540 // can be used to customize the layout of the memory within the phony target's
541 // address space:
542 // -target-addr-start <s> -- Specify where the phony target address range starts.
543 // -target-addr-end <e> -- Specify where the phony target address range ends.
544 // -target-section-sep <d> -- Specify how big a gap should be left between the
545 // end of one section and the start of the next.
546 // Defaults to zero. Set to something big
547 // (e.g. 1 << 32) to stress-test stubs, GOTs, etc.
549 static void remapSectionsAndSymbols(const llvm::Triple &TargetTriple,
550 TrivialMemoryManager &MemMgr,
551 RuntimeDyldChecker &Checker) {
553 // Set up a work list (section addr/size pairs).
554 typedef std::list<std::pair<void*, uint64_t>> WorklistT;
555 WorklistT Worklist;
557 for (const auto& CodeSection : MemMgr.FunctionMemory)
558 Worklist.push_back(std::make_pair(CodeSection.base(), CodeSection.size()));
559 for (const auto& DataSection : MemMgr.DataMemory)
560 Worklist.push_back(std::make_pair(DataSection.base(), DataSection.size()));
562 // Apply any section-specific mappings that were requested on the command
563 // line.
564 applySpecificSectionMappings(Checker);
566 // Keep an "already allocated" mapping of section target addresses to sizes.
567 // Sections whose address mappings aren't specified on the command line will
568 // allocated around the explicitly mapped sections while maintaining the
569 // minimum separation.
570 std::map<uint64_t, uint64_t> AlreadyAllocated;
572 // Move the previously applied mappings (whether explicitly specified on the
573 // command line, or implicitly set by RuntimeDyld) into the already-allocated
574 // map.
575 for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end();
576 I != E;) {
577 WorklistT::iterator Tmp = I;
578 ++I;
579 auto LoadAddr = Checker.getSectionLoadAddress(Tmp->first);
581 if (LoadAddr &&
582 *LoadAddr != static_cast<uint64_t>(
583 reinterpret_cast<uintptr_t>(Tmp->first))) {
584 // A section will have a LoadAddr of 0 if it wasn't loaded for whatever
585 // reason (e.g. zero byte COFF sections). Don't include those sections in
586 // the allocation map.
587 if (*LoadAddr != 0)
588 AlreadyAllocated[*LoadAddr] = Tmp->second;
589 Worklist.erase(Tmp);
593 // If the -target-addr-end option wasn't explicitly passed, then set it to a
594 // sensible default based on the target triple.
595 if (TargetAddrEnd.getNumOccurrences() == 0) {
596 if (TargetTriple.isArch16Bit())
597 TargetAddrEnd = (1ULL << 16) - 1;
598 else if (TargetTriple.isArch32Bit())
599 TargetAddrEnd = (1ULL << 32) - 1;
600 // TargetAddrEnd already has a sensible default for 64-bit systems, so
601 // there's nothing to do in the 64-bit case.
604 // Process any elements remaining in the worklist.
605 while (!Worklist.empty()) {
606 std::pair<void*, uint64_t> CurEntry = Worklist.front();
607 Worklist.pop_front();
609 uint64_t NextSectionAddr = TargetAddrStart;
611 for (const auto &Alloc : AlreadyAllocated)
612 if (NextSectionAddr + CurEntry.second + TargetSectionSep <= Alloc.first)
613 break;
614 else
615 NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep;
617 AlreadyAllocated[NextSectionAddr] = CurEntry.second;
618 Checker.getRTDyld().mapSectionAddress(CurEntry.first, NextSectionAddr);
621 // Add dummy symbols to the memory manager.
622 for (const auto &Mapping : DummySymbolMappings) {
623 size_t EqualsIdx = Mapping.find_first_of('=');
625 if (EqualsIdx == StringRef::npos)
626 report_fatal_error("Invalid dummy symbol specification '" + Mapping +
627 "'. Should be '<symbol name>=<addr>'");
629 std::string Symbol = Mapping.substr(0, EqualsIdx);
630 std::string AddrStr = Mapping.substr(EqualsIdx + 1);
632 uint64_t Addr;
633 if (StringRef(AddrStr).getAsInteger(0, Addr))
634 report_fatal_error("Invalid symbol mapping '" + Mapping + "'.");
636 MemMgr.addDummySymbol(Symbol, Addr);
640 // Load and link the objects specified on the command line, but do not execute
641 // anything. Instead, attach a RuntimeDyldChecker instance and call it to
642 // verify the correctness of the linked memory.
643 static int linkAndVerify() {
645 // Check for missing triple.
646 if (TripleName == "")
647 ErrorAndExit("-triple required when running in -verify mode.");
649 // Look up the target and build the disassembler.
650 Triple TheTriple(Triple::normalize(TripleName));
651 std::string ErrorStr;
652 const Target *TheTarget =
653 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
654 if (!TheTarget)
655 ErrorAndExit("Error accessing target '" + TripleName + "': " + ErrorStr);
657 TripleName = TheTriple.getTriple();
659 std::unique_ptr<MCSubtargetInfo> STI(
660 TheTarget->createMCSubtargetInfo(TripleName, MCPU, ""));
661 if (!STI)
662 ErrorAndExit("Unable to create subtarget info!");
664 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
665 if (!MRI)
666 ErrorAndExit("Unable to create target register info!");
668 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
669 if (!MAI)
670 ErrorAndExit("Unable to create target asm info!");
672 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
674 std::unique_ptr<MCDisassembler> Disassembler(
675 TheTarget->createMCDisassembler(*STI, Ctx));
676 if (!Disassembler)
677 ErrorAndExit("Unable to create disassembler!");
679 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
681 std::unique_ptr<MCInstPrinter> InstPrinter(
682 TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
684 // Load any dylibs requested on the command line.
685 loadDylibs();
687 // Instantiate a dynamic linker.
688 TrivialMemoryManager MemMgr;
689 doPreallocation(MemMgr);
690 RuntimeDyld Dyld(MemMgr, MemMgr);
691 Dyld.setProcessAllSections(true);
692 RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),
693 llvm::dbgs());
695 // If we don't have any input files, read from stdin.
696 if (!InputFileList.size())
697 InputFileList.push_back("-");
698 for (auto &Filename : InputFileList) {
699 // Load the input memory buffer.
700 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
701 MemoryBuffer::getFileOrSTDIN(Filename);
703 if (std::error_code EC = InputBuffer.getError())
704 ErrorAndExit("unable to read input: '" + EC.message() + "'");
706 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
707 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
709 if (!MaybeObj) {
710 std::string Buf;
711 raw_string_ostream OS(Buf);
712 logAllUnhandledErrors(MaybeObj.takeError(), OS);
713 OS.flush();
714 ErrorAndExit("unable to create object file: '" + Buf + "'");
717 ObjectFile &Obj = **MaybeObj;
719 // Load the object file
720 Dyld.loadObject(Obj);
721 if (Dyld.hasError()) {
722 ErrorAndExit(Dyld.getErrorString());
726 // Re-map the section addresses into the phony target address space and add
727 // dummy symbols.
728 remapSectionsAndSymbols(TheTriple, MemMgr, Checker);
730 // Resolve all the relocations we can.
731 Dyld.resolveRelocations();
733 // Register EH frames.
734 Dyld.registerEHFrames();
736 int ErrorCode = checkAllExpressions(Checker);
737 if (Dyld.hasError())
738 ErrorAndExit("RTDyld reported an error applying relocations:\n " +
739 Dyld.getErrorString());
741 return ErrorCode;
744 int main(int argc, char **argv) {
745 InitLLVM X(argc, argv);
746 ProgramName = argv[0];
748 llvm::InitializeAllTargetInfos();
749 llvm::InitializeAllTargetMCs();
750 llvm::InitializeAllDisassemblers();
752 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
754 switch (Action) {
755 case AC_Execute:
756 return executeInput();
757 case AC_PrintDebugLineInfo:
758 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */ true);
759 case AC_PrintLineInfo:
760 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */false);
761 case AC_PrintObjectLineInfo:
762 return printLineInfoForInput(/* LoadObjects */false,/* UseDebugObj */false);
763 case AC_Verify:
764 return linkAndVerify();