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/MSVCErrorWorkarounds.h"
33 #include "llvm/Support/Path.h"
34 #include "llvm/Support/TargetRegistry.h"
35 #include "llvm/Support/TargetSelect.h"
36 #include "llvm/Support/raw_ostream.h"
42 using namespace llvm::object
;
44 static cl::list
<std::string
>
45 InputFileList(cl::Positional
, cl::ZeroOrMore
,
46 cl::desc("<input files>"));
50 AC_PrintObjectLineInfo
,
52 AC_PrintDebugLineInfo
,
56 static cl::opt
<ActionType
>
57 Action(cl::desc("Action to perform:"),
59 cl::values(clEnumValN(AC_Execute
, "execute",
60 "Load, link, and execute the inputs."),
61 clEnumValN(AC_PrintLineInfo
, "printline",
62 "Load, link, and print line information for each function."),
63 clEnumValN(AC_PrintDebugLineInfo
, "printdebugline",
64 "Load, link, and print line information for each function using the debug object"),
65 clEnumValN(AC_PrintObjectLineInfo
, "printobjline",
66 "Like -printlineinfo but does not load the object first"),
67 clEnumValN(AC_Verify
, "verify",
68 "Load, link and verify the resulting memory image.")));
70 static cl::opt
<std::string
>
72 cl::desc("Function to call as entry point."),
75 static cl::list
<std::string
>
77 cl::desc("Add library."),
80 static cl::list
<std::string
> InputArgv("args", cl::Positional
,
81 cl::desc("<program arguments>..."),
82 cl::ZeroOrMore
, cl::PositionalEatsArgs
);
84 static cl::opt
<std::string
>
85 TripleName("triple", cl::desc("Target triple for disassembler"));
87 static cl::opt
<std::string
>
89 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
90 cl::value_desc("cpu-name"),
93 static cl::list
<std::string
>
95 cl::desc("File containing RuntimeDyld verifier checks."),
98 static cl::opt
<uint64_t>
99 PreallocMemory("preallocate",
100 cl::desc("Allocate memory upfront rather than on-demand"),
103 static cl::opt
<uint64_t> TargetAddrStart(
105 cl::desc("For -verify only: start of phony target address "
107 cl::init(4096), // Start at "page 1" - no allocating at "null".
110 static cl::opt
<uint64_t> TargetAddrEnd(
112 cl::desc("For -verify only: end of phony target address range."),
113 cl::init(~0ULL), cl::Hidden
);
115 static cl::opt
<uint64_t> TargetSectionSep(
116 "target-section-sep",
117 cl::desc("For -verify only: Separation between sections in "
118 "phony target address space."),
119 cl::init(0), cl::Hidden
);
121 static cl::list
<std::string
>
122 SpecificSectionMappings("map-section",
123 cl::desc("For -verify only: Map a section to a "
124 "specific address."),
128 static cl::list
<std::string
>
129 DummySymbolMappings("dummy-extern",
130 cl::desc("For -verify only: Inject a symbol into the extern "
136 PrintAllocationRequests("print-alloc-requests",
137 cl::desc("Print allocation requests made to the memory "
138 "manager by RuntimeDyld"),
141 ExitOnError ExitOnErr
;
145 using SectionIDMap
= StringMap
<unsigned>;
146 using FileToSectionIDMap
= StringMap
<SectionIDMap
>;
148 void dumpFileToSectionIDMap(const FileToSectionIDMap
&FileToSecIDMap
) {
149 for (const auto &KV
: FileToSecIDMap
) {
150 llvm::dbgs() << "In " << KV
.first() << "\n";
151 for (auto &KV2
: KV
.second
)
152 llvm::dbgs() << " \"" << KV2
.first() << "\" -> " << KV2
.second
<< "\n";
156 Expected
<unsigned> getSectionId(const FileToSectionIDMap
&FileToSecIDMap
,
157 StringRef FileName
, StringRef SectionName
) {
158 auto I
= FileToSecIDMap
.find(FileName
);
159 if (I
== FileToSecIDMap
.end())
160 return make_error
<StringError
>("No file named " + FileName
,
161 inconvertibleErrorCode());
162 auto &SectionIDs
= I
->second
;
163 auto J
= SectionIDs
.find(SectionName
);
164 if (J
== SectionIDs
.end())
165 return make_error
<StringError
>("No section named \"" + SectionName
+
166 "\" in file " + FileName
,
167 inconvertibleErrorCode());
171 // A trivial memory manager that doesn't do anything fancy, just uses the
172 // support library allocation routines directly.
173 class TrivialMemoryManager
: public RTDyldMemoryManager
{
176 SectionInfo(StringRef Name
, sys::MemoryBlock MB
, unsigned SectionID
)
177 : Name(Name
), MB(std::move(MB
)), SectionID(SectionID
) {}
180 unsigned SectionID
= ~0U;
183 SmallVector
<SectionInfo
, 16> FunctionMemory
;
184 SmallVector
<SectionInfo
, 16> DataMemory
;
186 uint8_t *allocateCodeSection(uintptr_t Size
, unsigned Alignment
,
188 StringRef SectionName
) override
;
189 uint8_t *allocateDataSection(uintptr_t Size
, unsigned Alignment
,
190 unsigned SectionID
, StringRef SectionName
,
191 bool IsReadOnly
) override
;
193 /// If non null, records subsequent Name -> SectionID mappings.
194 void setSectionIDsMap(SectionIDMap
*SecIDMap
) {
195 this->SecIDMap
= SecIDMap
;
198 void *getPointerToNamedFunction(const std::string
&Name
,
199 bool AbortOnFailure
= true) override
{
203 bool finalizeMemory(std::string
*ErrMsg
) override
{ return false; }
205 void addDummySymbol(const std::string
&Name
, uint64_t Addr
) {
206 DummyExterns
[Name
] = Addr
;
209 JITSymbol
findSymbol(const std::string
&Name
) override
{
210 auto I
= DummyExterns
.find(Name
);
212 if (I
!= DummyExterns
.end())
213 return JITSymbol(I
->second
, JITSymbolFlags::Exported
);
215 if (auto Sym
= RTDyldMemoryManager::findSymbol(Name
))
217 else if (auto Err
= Sym
.takeError())
218 ExitOnErr(std::move(Err
));
220 ExitOnErr(make_error
<StringError
>("Could not find definition for \"" +
222 inconvertibleErrorCode()));
223 llvm_unreachable("Should have returned or exited by now");
226 void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
,
227 size_t Size
) override
{}
228 void deregisterEHFrames() override
{}
230 void preallocateSlab(uint64_t Size
) {
232 sys::MemoryBlock MB
=
233 sys::Memory::allocateMappedMemory(Size
, nullptr,
234 sys::Memory::MF_READ
|
235 sys::Memory::MF_WRITE
,
238 report_fatal_error("Can't allocate enough memory: " + EC
.message());
241 UsePreallocation
= true;
245 uint8_t *allocateFromSlab(uintptr_t Size
, unsigned Alignment
, bool isCode
,
246 StringRef SectionName
, unsigned SectionID
) {
247 Size
= alignTo(Size
, Alignment
);
248 if (CurrentSlabOffset
+ Size
> SlabSize
)
249 report_fatal_error("Can't allocate enough memory. Tune --preallocate");
251 uintptr_t OldSlabOffset
= CurrentSlabOffset
;
252 sys::MemoryBlock
MB((void *)OldSlabOffset
, Size
);
254 FunctionMemory
.push_back(SectionInfo(SectionName
, MB
, SectionID
));
256 DataMemory
.push_back(SectionInfo(SectionName
, MB
, SectionID
));
257 CurrentSlabOffset
+= Size
;
258 return (uint8_t*)OldSlabOffset
;
262 std::map
<std::string
, uint64_t> DummyExterns
;
263 sys::MemoryBlock PreallocSlab
;
264 bool UsePreallocation
= false;
265 uintptr_t SlabSize
= 0;
266 uintptr_t CurrentSlabOffset
= 0;
267 SectionIDMap
*SecIDMap
= nullptr;
270 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size
,
273 StringRef SectionName
) {
274 if (PrintAllocationRequests
)
275 outs() << "allocateCodeSection(Size = " << Size
<< ", Alignment = "
276 << Alignment
<< ", SectionName = " << SectionName
<< ")\n";
279 (*SecIDMap
)[SectionName
] = SectionID
;
281 if (UsePreallocation
)
282 return allocateFromSlab(Size
, Alignment
, true /* isCode */,
283 SectionName
, SectionID
);
286 sys::MemoryBlock MB
=
287 sys::Memory::allocateMappedMemory(Size
, nullptr,
288 sys::Memory::MF_READ
|
289 sys::Memory::MF_WRITE
,
292 report_fatal_error("MemoryManager allocation failed: " + EC
.message());
293 FunctionMemory
.push_back(SectionInfo(SectionName
, MB
, SectionID
));
294 return (uint8_t*)MB
.base();
297 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size
,
300 StringRef SectionName
,
302 if (PrintAllocationRequests
)
303 outs() << "allocateDataSection(Size = " << Size
<< ", Alignment = "
304 << Alignment
<< ", SectionName = " << SectionName
<< ")\n";
307 (*SecIDMap
)[SectionName
] = SectionID
;
309 if (UsePreallocation
)
310 return allocateFromSlab(Size
, Alignment
, false /* isCode */, SectionName
,
314 sys::MemoryBlock MB
=
315 sys::Memory::allocateMappedMemory(Size
, nullptr,
316 sys::Memory::MF_READ
|
317 sys::Memory::MF_WRITE
,
320 report_fatal_error("MemoryManager allocation failed: " + EC
.message());
321 DataMemory
.push_back(SectionInfo(SectionName
, MB
, SectionID
));
322 return (uint8_t*)MB
.base();
325 static const char *ProgramName
;
327 static void ErrorAndExit(const Twine
&Msg
) {
328 errs() << ProgramName
<< ": error: " << Msg
<< "\n";
332 static void loadDylibs() {
333 for (const std::string
&Dylib
: Dylibs
) {
334 if (!sys::fs::is_regular_file(Dylib
))
335 report_fatal_error("Dylib not found: '" + Dylib
+ "'.");
337 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib
.c_str(), &ErrMsg
))
338 report_fatal_error("Error loading '" + Dylib
+ "': " + ErrMsg
);
344 static int printLineInfoForInput(bool LoadObjects
, bool UseDebugObj
) {
345 assert(LoadObjects
|| !UseDebugObj
);
347 // Load any dylibs requested on the command line.
350 // If we don't have any input files, read from stdin.
351 if (!InputFileList
.size())
352 InputFileList
.push_back("-");
353 for (auto &File
: InputFileList
) {
354 // Instantiate a dynamic linker.
355 TrivialMemoryManager MemMgr
;
356 RuntimeDyld
Dyld(MemMgr
, MemMgr
);
358 // Load the input memory buffer.
360 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> InputBuffer
=
361 MemoryBuffer::getFileOrSTDIN(File
);
362 if (std::error_code EC
= InputBuffer
.getError())
363 ErrorAndExit("unable to read input: '" + EC
.message() + "'");
365 Expected
<std::unique_ptr
<ObjectFile
>> MaybeObj(
366 ObjectFile::createObjectFile((*InputBuffer
)->getMemBufferRef()));
370 raw_string_ostream
OS(Buf
);
371 logAllUnhandledErrors(MaybeObj
.takeError(), OS
);
373 ErrorAndExit("unable to create object file: '" + Buf
+ "'");
376 ObjectFile
&Obj
= **MaybeObj
;
378 OwningBinary
<ObjectFile
> DebugObj
;
379 std::unique_ptr
<RuntimeDyld::LoadedObjectInfo
> LoadedObjInfo
= nullptr;
380 ObjectFile
*SymbolObj
= &Obj
;
382 // Load the object file
384 Dyld
.loadObject(Obj
);
387 ErrorAndExit(Dyld
.getErrorString());
389 // Resolve all the relocations we can.
390 Dyld
.resolveRelocations();
393 DebugObj
= LoadedObjInfo
->getObjectForDebug(Obj
);
394 SymbolObj
= DebugObj
.getBinary();
395 LoadedObjInfo
.reset();
399 std::unique_ptr
<DIContext
> Context
=
400 DWARFContext::create(*SymbolObj
, LoadedObjInfo
.get());
402 std::vector
<std::pair
<SymbolRef
, uint64_t>> SymAddr
=
403 object::computeSymbolSizes(*SymbolObj
);
405 // Use symbol info to iterate functions in the object.
406 for (const auto &P
: SymAddr
) {
407 object::SymbolRef Sym
= P
.first
;
408 Expected
<SymbolRef::Type
> TypeOrErr
= Sym
.getType();
410 // TODO: Actually report errors helpfully.
411 consumeError(TypeOrErr
.takeError());
414 SymbolRef::Type Type
= *TypeOrErr
;
415 if (Type
== object::SymbolRef::ST_Function
) {
416 Expected
<StringRef
> Name
= Sym
.getName();
418 // TODO: Actually report errors helpfully.
419 consumeError(Name
.takeError());
422 Expected
<uint64_t> AddrOrErr
= Sym
.getAddress();
424 // TODO: Actually report errors helpfully.
425 consumeError(AddrOrErr
.takeError());
428 uint64_t Addr
= *AddrOrErr
;
430 object::SectionedAddress Address
;
432 uint64_t Size
= P
.second
;
433 // If we're not using the debug object, compute the address of the
434 // symbol in memory (rather than that in the unrelocated object file)
435 // and use that to query the DWARFContext.
436 if (!UseDebugObj
&& LoadObjects
) {
437 auto SecOrErr
= Sym
.getSection();
439 // TODO: Actually report errors helpfully.
440 consumeError(SecOrErr
.takeError());
443 object::section_iterator Sec
= *SecOrErr
;
445 Sec
->getName(SecName
);
446 Address
.SectionIndex
= Sec
->getIndex();
447 uint64_t SectionLoadAddress
=
448 LoadedObjInfo
->getSectionLoadAddress(*Sec
);
449 if (SectionLoadAddress
!= 0)
450 Addr
+= SectionLoadAddress
- Sec
->getAddress();
451 } else if (auto SecOrErr
= Sym
.getSection())
452 Address
.SectionIndex
= SecOrErr
.get()->getIndex();
454 outs() << "Function: " << *Name
<< ", Size = " << Size
455 << ", Addr = " << Addr
<< "\n";
457 Address
.Address
= Addr
;
458 DILineInfoTable Lines
=
459 Context
->getLineInfoForAddressRange(Address
, Size
);
460 for (auto &D
: Lines
) {
461 outs() << " Line info @ " << D
.first
- Addr
<< ": "
462 << D
.second
.FileName
<< ", line:" << D
.second
.Line
<< "\n";
471 static void doPreallocation(TrivialMemoryManager
&MemMgr
) {
472 // Allocate a slab of memory upfront, if required. This is used if
473 // we want to test small code models.
474 if (static_cast<intptr_t>(PreallocMemory
) < 0)
475 report_fatal_error("Pre-allocated bytes of memory must be a positive integer.");
477 // FIXME: Limit the amount of memory that can be preallocated?
478 if (PreallocMemory
!= 0)
479 MemMgr
.preallocateSlab(PreallocMemory
);
482 static int executeInput() {
483 // Load any dylibs requested on the command line.
486 // Instantiate a dynamic linker.
487 TrivialMemoryManager MemMgr
;
488 doPreallocation(MemMgr
);
489 RuntimeDyld
Dyld(MemMgr
, MemMgr
);
491 // If we don't have any input files, read from stdin.
492 if (!InputFileList
.size())
493 InputFileList
.push_back("-");
494 for (auto &File
: InputFileList
) {
495 // Load the input memory buffer.
496 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> InputBuffer
=
497 MemoryBuffer::getFileOrSTDIN(File
);
498 if (std::error_code EC
= InputBuffer
.getError())
499 ErrorAndExit("unable to read input: '" + EC
.message() + "'");
500 Expected
<std::unique_ptr
<ObjectFile
>> MaybeObj(
501 ObjectFile::createObjectFile((*InputBuffer
)->getMemBufferRef()));
505 raw_string_ostream
OS(Buf
);
506 logAllUnhandledErrors(MaybeObj
.takeError(), OS
);
508 ErrorAndExit("unable to create object file: '" + Buf
+ "'");
511 ObjectFile
&Obj
= **MaybeObj
;
513 // Load the object file
514 Dyld
.loadObject(Obj
);
515 if (Dyld
.hasError()) {
516 ErrorAndExit(Dyld
.getErrorString());
520 // Resove all the relocations we can.
521 // FIXME: Error out if there are unresolved relocations.
522 Dyld
.resolveRelocations();
524 // Get the address of the entry point (_main by default).
525 void *MainAddress
= Dyld
.getSymbolLocalAddress(EntryPoint
);
527 ErrorAndExit("no definition for '" + EntryPoint
+ "'");
529 // Invalidate the instruction cache for each loaded function.
530 for (auto &FM
: MemMgr
.FunctionMemory
) {
534 // Make sure the memory is executable.
535 // setExecutable will call InvalidateInstructionCache.
536 if (auto EC
= sys::Memory::protectMappedMemory(FM_MB
,
537 sys::Memory::MF_READ
|
538 sys::Memory::MF_EXEC
))
539 ErrorAndExit("unable to mark function executable: '" + EC
.message() +
543 // Dispatch to _main().
544 errs() << "loaded '" << EntryPoint
<< "' at: " << (void*)MainAddress
<< "\n";
546 int (*Main
)(int, const char**) =
547 (int(*)(int,const char**)) uintptr_t(MainAddress
);
548 std::vector
<const char *> Argv
;
549 // Use the name of the first input object module as argv[0] for the target.
550 Argv
.push_back(InputFileList
[0].data());
551 for (auto &Arg
: InputArgv
)
552 Argv
.push_back(Arg
.data());
553 Argv
.push_back(nullptr);
554 return Main(Argv
.size() - 1, Argv
.data());
557 static int checkAllExpressions(RuntimeDyldChecker
&Checker
) {
558 for (const auto& CheckerFileName
: CheckFiles
) {
559 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> CheckerFileBuf
=
560 MemoryBuffer::getFileOrSTDIN(CheckerFileName
);
561 if (std::error_code EC
= CheckerFileBuf
.getError())
562 ErrorAndExit("unable to read input '" + CheckerFileName
+ "': " +
565 if (!Checker
.checkAllRulesInBuffer("# rtdyld-check:",
566 CheckerFileBuf
.get().get()))
567 ErrorAndExit("some checks in '" + CheckerFileName
+ "' failed");
572 void applySpecificSectionMappings(RuntimeDyld
&Dyld
,
573 const FileToSectionIDMap
&FileToSecIDMap
) {
575 for (StringRef Mapping
: SpecificSectionMappings
) {
576 size_t EqualsIdx
= Mapping
.find_first_of("=");
577 std::string SectionIDStr
= Mapping
.substr(0, EqualsIdx
);
578 size_t ComaIdx
= Mapping
.find_first_of(",");
580 if (ComaIdx
== StringRef::npos
)
581 report_fatal_error("Invalid section specification '" + Mapping
+
582 "'. Should be '<file name>,<section name>=<addr>'");
584 std::string FileName
= SectionIDStr
.substr(0, ComaIdx
);
585 std::string SectionName
= SectionIDStr
.substr(ComaIdx
+ 1);
587 ExitOnErr(getSectionId(FileToSecIDMap
, FileName
, SectionName
));
589 auto* OldAddr
= Dyld
.getSectionContent(SectionID
).data();
590 std::string NewAddrStr
= Mapping
.substr(EqualsIdx
+ 1);
593 if (StringRef(NewAddrStr
).getAsInteger(0, NewAddr
))
594 report_fatal_error("Invalid section address in mapping '" + Mapping
+
597 Dyld
.mapSectionAddress(OldAddr
, NewAddr
);
601 // Scatter sections in all directions!
602 // Remaps section addresses for -verify mode. The following command line options
603 // can be used to customize the layout of the memory within the phony target's
605 // -target-addr-start <s> -- Specify where the phony target address range starts.
606 // -target-addr-end <e> -- Specify where the phony target address range ends.
607 // -target-section-sep <d> -- Specify how big a gap should be left between the
608 // end of one section and the start of the next.
609 // Defaults to zero. Set to something big
610 // (e.g. 1 << 32) to stress-test stubs, GOTs, etc.
612 static void remapSectionsAndSymbols(const llvm::Triple
&TargetTriple
,
614 TrivialMemoryManager
&MemMgr
) {
616 // Set up a work list (section addr/size pairs).
617 typedef std::list
<const TrivialMemoryManager::SectionInfo
*> WorklistT
;
620 for (const auto& CodeSection
: MemMgr
.FunctionMemory
)
621 Worklist
.push_back(&CodeSection
);
622 for (const auto& DataSection
: MemMgr
.DataMemory
)
623 Worklist
.push_back(&DataSection
);
625 // Keep an "already allocated" mapping of section target addresses to sizes.
626 // Sections whose address mappings aren't specified on the command line will
627 // allocated around the explicitly mapped sections while maintaining the
628 // minimum separation.
629 std::map
<uint64_t, uint64_t> AlreadyAllocated
;
631 // Move the previously applied mappings (whether explicitly specified on the
632 // command line, or implicitly set by RuntimeDyld) into the already-allocated
634 for (WorklistT::iterator I
= Worklist
.begin(), E
= Worklist
.end();
636 WorklistT::iterator Tmp
= I
;
639 auto LoadAddr
= Dyld
.getSectionLoadAddress((*Tmp
)->SectionID
);
641 if (LoadAddr
!= static_cast<uint64_t>(
642 reinterpret_cast<uintptr_t>((*Tmp
)->MB
.base()))) {
643 // A section will have a LoadAddr of 0 if it wasn't loaded for whatever
644 // reason (e.g. zero byte COFF sections). Don't include those sections in
645 // the allocation map.
647 AlreadyAllocated
[LoadAddr
] = (*Tmp
)->MB
.allocatedSize();
652 // If the -target-addr-end option wasn't explicitly passed, then set it to a
653 // sensible default based on the target triple.
654 if (TargetAddrEnd
.getNumOccurrences() == 0) {
655 if (TargetTriple
.isArch16Bit())
656 TargetAddrEnd
= (1ULL << 16) - 1;
657 else if (TargetTriple
.isArch32Bit())
658 TargetAddrEnd
= (1ULL << 32) - 1;
659 // TargetAddrEnd already has a sensible default for 64-bit systems, so
660 // there's nothing to do in the 64-bit case.
663 // Process any elements remaining in the worklist.
664 while (!Worklist
.empty()) {
665 auto *CurEntry
= Worklist
.front();
666 Worklist
.pop_front();
668 uint64_t NextSectionAddr
= TargetAddrStart
;
670 for (const auto &Alloc
: AlreadyAllocated
)
671 if (NextSectionAddr
+ CurEntry
->MB
.allocatedSize() + TargetSectionSep
<=
675 NextSectionAddr
= Alloc
.first
+ Alloc
.second
+ TargetSectionSep
;
677 Dyld
.mapSectionAddress(CurEntry
->MB
.base(), NextSectionAddr
);
678 AlreadyAllocated
[NextSectionAddr
] = CurEntry
->MB
.allocatedSize();
681 // Add dummy symbols to the memory manager.
682 for (const auto &Mapping
: DummySymbolMappings
) {
683 size_t EqualsIdx
= Mapping
.find_first_of('=');
685 if (EqualsIdx
== StringRef::npos
)
686 report_fatal_error("Invalid dummy symbol specification '" + Mapping
+
687 "'. Should be '<symbol name>=<addr>'");
689 std::string Symbol
= Mapping
.substr(0, EqualsIdx
);
690 std::string AddrStr
= Mapping
.substr(EqualsIdx
+ 1);
693 if (StringRef(AddrStr
).getAsInteger(0, Addr
))
694 report_fatal_error("Invalid symbol mapping '" + Mapping
+ "'.");
696 MemMgr
.addDummySymbol(Symbol
, Addr
);
700 // Load and link the objects specified on the command line, but do not execute
701 // anything. Instead, attach a RuntimeDyldChecker instance and call it to
702 // verify the correctness of the linked memory.
703 static int linkAndVerify() {
705 // Check for missing triple.
706 if (TripleName
== "")
707 ErrorAndExit("-triple required when running in -verify mode.");
709 // Look up the target and build the disassembler.
710 Triple
TheTriple(Triple::normalize(TripleName
));
711 std::string ErrorStr
;
712 const Target
*TheTarget
=
713 TargetRegistry::lookupTarget("", TheTriple
, ErrorStr
);
715 ErrorAndExit("Error accessing target '" + TripleName
+ "': " + ErrorStr
);
717 TripleName
= TheTriple
.getTriple();
719 std::unique_ptr
<MCSubtargetInfo
> STI(
720 TheTarget
->createMCSubtargetInfo(TripleName
, MCPU
, ""));
722 ErrorAndExit("Unable to create subtarget info!");
724 std::unique_ptr
<MCRegisterInfo
> MRI(TheTarget
->createMCRegInfo(TripleName
));
726 ErrorAndExit("Unable to create target register info!");
728 std::unique_ptr
<MCAsmInfo
> MAI(TheTarget
->createMCAsmInfo(*MRI
, TripleName
));
730 ErrorAndExit("Unable to create target asm info!");
732 MCContext
Ctx(MAI
.get(), MRI
.get(), nullptr);
734 std::unique_ptr
<MCDisassembler
> Disassembler(
735 TheTarget
->createMCDisassembler(*STI
, Ctx
));
737 ErrorAndExit("Unable to create disassembler!");
739 std::unique_ptr
<MCInstrInfo
> MII(TheTarget
->createMCInstrInfo());
741 std::unique_ptr
<MCInstPrinter
> InstPrinter(
742 TheTarget
->createMCInstPrinter(Triple(TripleName
), 0, *MAI
, *MII
, *MRI
));
744 // Load any dylibs requested on the command line.
747 // Instantiate a dynamic linker.
748 TrivialMemoryManager MemMgr
;
749 doPreallocation(MemMgr
);
755 using StubInfos
= StringMap
<StubID
>;
756 using StubContainers
= StringMap
<StubInfos
>;
758 StubContainers StubMap
;
759 RuntimeDyld
Dyld(MemMgr
, MemMgr
);
760 Dyld
.setProcessAllSections(true);
762 Dyld
.setNotifyStubEmitted([&StubMap
](StringRef FilePath
,
763 StringRef SectionName
,
764 StringRef SymbolName
, unsigned SectionID
,
765 uint32_t StubOffset
) {
766 std::string ContainerName
=
767 (sys::path::filename(FilePath
) + "/" + SectionName
).str();
768 StubMap
[ContainerName
][SymbolName
] = {SectionID
, StubOffset
};
773 StringRef Symbol
) -> Expected
<RuntimeDyldChecker::MemoryRegionInfo
> {
774 RuntimeDyldChecker::MemoryRegionInfo SymInfo
;
776 // First get the target address.
777 if (auto InternalSymbol
= Dyld
.getSymbol(Symbol
))
778 SymInfo
.setTargetAddress(InternalSymbol
.getAddress());
780 // Symbol not found in RuntimeDyld. Fall back to external lookup.
782 using ExpectedLookupResult
=
783 MSVCPExpected
<JITSymbolResolver::LookupResult
>;
785 using ExpectedLookupResult
= Expected
<JITSymbolResolver::LookupResult
>;
788 auto ResultP
= std::make_shared
<std::promise
<ExpectedLookupResult
>>();
789 auto ResultF
= ResultP
->get_future();
791 MemMgr
.lookup(JITSymbolResolver::LookupSet({Symbol
}),
792 [=](Expected
<JITSymbolResolver::LookupResult
> Result
) {
793 ResultP
->set_value(std::move(Result
));
796 auto Result
= ResultF
.get();
798 return Result
.takeError();
800 auto I
= Result
->find(Symbol
);
801 assert(I
!= Result
->end() &&
802 "Expected symbol address if no error occurred");
803 SymInfo
.setTargetAddress(I
->second
.getAddress());
806 // Now find the symbol content if possible (otherwise leave content as a
807 // default-constructed StringRef).
808 if (auto *SymAddr
= Dyld
.getSymbolLocalAddress(Symbol
)) {
809 unsigned SectionID
= Dyld
.getSymbolSectionID(Symbol
);
810 if (SectionID
!= ~0U) {
811 char *CSymAddr
= static_cast<char *>(SymAddr
);
812 StringRef SecContent
= Dyld
.getSectionContent(SectionID
);
813 uint64_t SymSize
= SecContent
.size() - (CSymAddr
- SecContent
.data());
814 SymInfo
.setContent(StringRef(CSymAddr
, SymSize
));
820 auto IsSymbolValid
= [&Dyld
, GetSymbolInfo
](StringRef Symbol
) {
821 if (Dyld
.getSymbol(Symbol
))
823 auto SymInfo
= GetSymbolInfo(Symbol
);
825 logAllUnhandledErrors(SymInfo
.takeError(), errs(), "RTDyldChecker: ");
828 return SymInfo
->getTargetAddress() != 0;
831 FileToSectionIDMap FileToSecIDMap
;
833 auto GetSectionInfo
= [&Dyld
, &FileToSecIDMap
](StringRef FileName
,
834 StringRef SectionName
)
835 -> Expected
<RuntimeDyldChecker::MemoryRegionInfo
> {
836 auto SectionID
= getSectionId(FileToSecIDMap
, FileName
, SectionName
);
838 return SectionID
.takeError();
839 RuntimeDyldChecker::MemoryRegionInfo SecInfo
;
840 SecInfo
.setTargetAddress(Dyld
.getSectionLoadAddress(*SectionID
));
841 SecInfo
.setContent(Dyld
.getSectionContent(*SectionID
));
845 auto GetStubInfo
= [&Dyld
, &StubMap
](StringRef StubContainer
,
846 StringRef SymbolName
)
847 -> Expected
<RuntimeDyldChecker::MemoryRegionInfo
> {
848 if (!StubMap
.count(StubContainer
))
849 return make_error
<StringError
>("Stub container not found: " +
851 inconvertibleErrorCode());
852 if (!StubMap
[StubContainer
].count(SymbolName
))
853 return make_error
<StringError
>("Symbol name " + SymbolName
+
854 " in stub container " + StubContainer
,
855 inconvertibleErrorCode());
856 auto &SI
= StubMap
[StubContainer
][SymbolName
];
857 RuntimeDyldChecker::MemoryRegionInfo StubMemInfo
;
858 StubMemInfo
.setTargetAddress(Dyld
.getSectionLoadAddress(SI
.SectionID
) +
860 StubMemInfo
.setContent(
861 Dyld
.getSectionContent(SI
.SectionID
).substr(SI
.Offset
));
865 // We will initialize this below once we have the first object file and can
866 // know the endianness.
867 std::unique_ptr
<RuntimeDyldChecker
> Checker
;
869 // If we don't have any input files, read from stdin.
870 if (!InputFileList
.size())
871 InputFileList
.push_back("-");
872 for (auto &InputFile
: InputFileList
) {
873 // Load the input memory buffer.
874 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> InputBuffer
=
875 MemoryBuffer::getFileOrSTDIN(InputFile
);
877 if (std::error_code EC
= InputBuffer
.getError())
878 ErrorAndExit("unable to read input: '" + EC
.message() + "'");
880 Expected
<std::unique_ptr
<ObjectFile
>> MaybeObj(
881 ObjectFile::createObjectFile((*InputBuffer
)->getMemBufferRef()));
885 raw_string_ostream
OS(Buf
);
886 logAllUnhandledErrors(MaybeObj
.takeError(), OS
);
888 ErrorAndExit("unable to create object file: '" + Buf
+ "'");
891 ObjectFile
&Obj
= **MaybeObj
;
894 Checker
= llvm::make_unique
<RuntimeDyldChecker
>(
895 IsSymbolValid
, GetSymbolInfo
, GetSectionInfo
, GetStubInfo
,
896 GetStubInfo
, Obj
.isLittleEndian() ? support::little
: support::big
,
897 Disassembler
.get(), InstPrinter
.get(), dbgs());
899 auto FileName
= sys::path::filename(InputFile
);
900 MemMgr
.setSectionIDsMap(&FileToSecIDMap
[FileName
]);
902 // Load the object file
903 Dyld
.loadObject(Obj
);
904 if (Dyld
.hasError()) {
905 ErrorAndExit(Dyld
.getErrorString());
909 // Re-map the section addresses into the phony target address space and add
911 applySpecificSectionMappings(Dyld
, FileToSecIDMap
);
912 remapSectionsAndSymbols(TheTriple
, Dyld
, MemMgr
);
914 // Resolve all the relocations we can.
915 Dyld
.resolveRelocations();
917 // Register EH frames.
918 Dyld
.registerEHFrames();
920 int ErrorCode
= checkAllExpressions(*Checker
);
922 ErrorAndExit("RTDyld reported an error applying relocations:\n " +
923 Dyld
.getErrorString());
928 int main(int argc
, char **argv
) {
929 InitLLVM
X(argc
, argv
);
930 ProgramName
= argv
[0];
932 llvm::InitializeAllTargetInfos();
933 llvm::InitializeAllTargetMCs();
934 llvm::InitializeAllDisassemblers();
936 cl::ParseCommandLineOptions(argc
, argv
, "llvm MC-JIT tool\n");
938 ExitOnErr
.setBanner(std::string(argv
[0]) + ": ");
942 return executeInput();
943 case AC_PrintDebugLineInfo
:
944 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */ true);
945 case AC_PrintLineInfo
:
946 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */false);
947 case AC_PrintObjectLineInfo
:
948 return printLineInfoForInput(/* LoadObjects */false,/* UseDebugObj */false);
950 return linkAndVerify();