1 //===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
38 using namespace llvm::object
;
40 static cl::list
<std::string
>
41 InputFileList(cl::Positional
, cl::ZeroOrMore
,
42 cl::desc("<input files>"));
46 AC_PrintObjectLineInfo
,
48 AC_PrintDebugLineInfo
,
52 static cl::opt
<ActionType
>
53 Action(cl::desc("Action to perform:"),
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
>
68 cl::desc("Function to call as entry point."),
71 static cl::list
<std::string
>
73 cl::desc("Add library."),
76 static cl::opt
<std::string
>
77 TripleName("triple", cl::desc("Target triple for disassembler"));
79 static cl::opt
<std::string
>
81 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
82 cl::value_desc("cpu-name"),
85 static cl::list
<std::string
>
87 cl::desc("File containing RuntimeDyld verifier checks."),
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
95 static cl::opt
<unsigned long long>
96 PreallocMemory("preallocate",
97 cl::desc("Allocate memory upfront rather than on-demand"),
100 static cl::opt
<unsigned long long>
101 TargetAddrStart("target-addr-start",
102 cl::desc("For -verify only: start of phony target address "
104 cl::init(4096), // Start at "page 1" - no allocating at "null".
107 static cl::opt
<unsigned long long>
108 TargetAddrEnd("target-addr-end",
109 cl::desc("For -verify only: end of phony target address range."),
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."),
120 static cl::list
<std::string
>
121 SpecificSectionMappings("map-section",
122 cl::desc("For -verify only: Map a section to a "
123 "specific address."),
127 static cl::list
<std::string
>
128 DummySymbolMappings("dummy-extern",
129 cl::desc("For -verify only: Inject a symbol into the extern "
135 PrintAllocationRequests("print-alloc-requests",
136 cl::desc("Print allocation requests made to the memory "
137 "manager by RuntimeDyld"),
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
{
146 SmallVector
<sys::MemoryBlock
, 16> FunctionMemory
;
147 SmallVector
<sys::MemoryBlock
, 16> DataMemory
;
149 uint8_t *allocateCodeSection(uintptr_t Size
, unsigned Alignment
,
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
{
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
) {
182 sys::MemoryBlock MB
=
183 sys::Memory::allocateMappedMemory(Size
, nullptr,
184 sys::Memory::MF_READ
|
185 sys::Memory::MF_WRITE
,
188 report_fatal_error("Can't allocate enough memory: " + EC
.message());
191 UsePreallocation
= true;
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
);
203 FunctionMemory
.push_back(MB
);
205 DataMemory
.push_back(MB
);
206 CurrentSlabOffset
+= Size
;
207 return (uint8_t*)OldSlabOffset
;
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
,
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 */);
230 sys::MemoryBlock MB
=
231 sys::Memory::allocateMappedMemory(Size
, nullptr,
232 sys::Memory::MF_READ
|
233 sys::Memory::MF_WRITE
,
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
,
244 StringRef SectionName
,
246 if (PrintAllocationRequests
)
247 outs() << "allocateDataSection(Size = " << Size
<< ", Alignment = "
248 << Alignment
<< ", SectionName = " << SectionName
<< ")\n";
250 if (UsePreallocation
)
251 return allocateFromSlab(Size
, Alignment
, false /* isCode */);
254 sys::MemoryBlock MB
=
255 sys::Memory::allocateMappedMemory(Size
, nullptr,
256 sys::Memory::MF_READ
|
257 sys::Memory::MF_WRITE
,
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";
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
+ "'.");
277 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib
.c_str(), &ErrMsg
))
278 report_fatal_error("Error loading '" + Dylib
+ "': " + ErrMsg
);
284 static int printLineInfoForInput(bool LoadObjects
, bool UseDebugObj
) {
285 assert(LoadObjects
|| !UseDebugObj
);
287 // Load any dylibs requested on the command line.
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()));
310 raw_string_ostream
OS(Buf
);
311 logAllUnhandledErrors(MaybeObj
.takeError(), OS
);
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
;
322 // Load the object file
324 Dyld
.loadObject(Obj
);
327 ErrorAndExit(Dyld
.getErrorString());
329 // Resolve all the relocations we can.
330 Dyld
.resolveRelocations();
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();
350 // TODO: Actually report errors helpfully.
351 consumeError(TypeOrErr
.takeError());
354 SymbolRef::Type Type
= *TypeOrErr
;
355 if (Type
== object::SymbolRef::ST_Function
) {
356 Expected
<StringRef
> Name
= Sym
.getName();
358 // TODO: Actually report errors helpfully.
359 consumeError(Name
.takeError());
362 Expected
<uint64_t> AddrOrErr
= Sym
.getAddress();
364 // TODO: Actually report errors helpfully.
365 consumeError(AddrOrErr
.takeError());
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();
377 // TODO: Actually report errors helpfully.
378 consumeError(SecOrErr
.takeError());
381 object::section_iterator Sec
= *SecOrErr
;
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";
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.
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()));
439 raw_string_ostream
OS(Buf
);
440 logAllUnhandledErrors(MaybeObj
.takeError(), OS
);
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
);
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() +
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();
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
+ "': " +
495 if (!Checker
.checkAllRulesInBuffer("# rtdyld-check:",
496 CheckerFileBuf
.get().get()))
497 ErrorAndExit("some checks in '" + CheckerFileName
+ "' failed");
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);
518 std::string ErrorMsg
;
519 std::tie(OldAddrInt
, ErrorMsg
) =
520 Checker
.getSectionAddr(FileName
, SectionName
, true);
523 report_fatal_error(ErrorMsg
);
525 void* OldAddr
= reinterpret_cast<void*>(static_cast<uintptr_t>(OldAddrInt
));
527 std::string NewAddrStr
= Mapping
.substr(EqualsIdx
+ 1);
530 if (StringRef(NewAddrStr
).getAsInteger(0, NewAddr
))
531 report_fatal_error("Invalid section address in mapping '" + Mapping
+
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
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
;
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
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
575 for (WorklistT::iterator I
= Worklist
.begin(), E
= Worklist
.end();
577 WorklistT::iterator Tmp
= I
;
579 auto LoadAddr
= Checker
.getSectionLoadAddress(Tmp
->first
);
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.
588 AlreadyAllocated
[*LoadAddr
] = Tmp
->second
;
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
)
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);
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
);
655 ErrorAndExit("Error accessing target '" + TripleName
+ "': " + ErrorStr
);
657 TripleName
= TheTriple
.getTriple();
659 std::unique_ptr
<MCSubtargetInfo
> STI(
660 TheTarget
->createMCSubtargetInfo(TripleName
, MCPU
, ""));
662 ErrorAndExit("Unable to create subtarget info!");
664 std::unique_ptr
<MCRegisterInfo
> MRI(TheTarget
->createMCRegInfo(TripleName
));
666 ErrorAndExit("Unable to create target register info!");
668 std::unique_ptr
<MCAsmInfo
> MAI(TheTarget
->createMCAsmInfo(*MRI
, TripleName
));
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
));
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.
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(),
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()));
711 raw_string_ostream
OS(Buf
);
712 logAllUnhandledErrors(MaybeObj
.takeError(), OS
);
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
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
);
738 ErrorAndExit("RTDyld reported an error applying relocations:\n " +
739 Dyld
.getErrorString());
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");
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);
764 return linkAndVerify();