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/MC/MCTargetOptions.h"
27 #include "llvm/Object/SymbolSize.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/DynamicLibrary.h"
30 #include "llvm/Support/InitLLVM.h"
31 #include "llvm/Support/MSVCErrorWorkarounds.h"
32 #include "llvm/Support/Memory.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/Path.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/TargetSelect.h"
37 #include "llvm/Support/Timer.h"
38 #include "llvm/Support/raw_ostream.h"
44 using namespace llvm::object
;
46 static cl::list
<std::string
>
47 InputFileList(cl::Positional
, cl::ZeroOrMore
,
48 cl::desc("<input files>"));
52 AC_PrintObjectLineInfo
,
54 AC_PrintDebugLineInfo
,
58 static cl::opt
<ActionType
>
59 Action(cl::desc("Action to perform:"),
61 cl::values(clEnumValN(AC_Execute
, "execute",
62 "Load, link, and execute the inputs."),
63 clEnumValN(AC_PrintLineInfo
, "printline",
64 "Load, link, and print line information for each function."),
65 clEnumValN(AC_PrintDebugLineInfo
, "printdebugline",
66 "Load, link, and print line information for each function using the debug object"),
67 clEnumValN(AC_PrintObjectLineInfo
, "printobjline",
68 "Like -printlineinfo but does not load the object first"),
69 clEnumValN(AC_Verify
, "verify",
70 "Load, link and verify the resulting memory image.")));
72 static cl::opt
<std::string
>
74 cl::desc("Function to call as entry point."),
77 static cl::list
<std::string
>
79 cl::desc("Add library."),
82 static cl::list
<std::string
> InputArgv("args", cl::Positional
,
83 cl::desc("<program arguments>..."),
84 cl::ZeroOrMore
, cl::PositionalEatsArgs
);
86 static cl::opt
<std::string
>
87 TripleName("triple", cl::desc("Target triple for disassembler"));
89 static cl::opt
<std::string
>
91 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
92 cl::value_desc("cpu-name"),
95 static cl::list
<std::string
>
97 cl::desc("File containing RuntimeDyld verifier checks."),
100 static cl::opt
<uint64_t>
101 PreallocMemory("preallocate",
102 cl::desc("Allocate memory upfront rather than on-demand"),
105 static cl::opt
<uint64_t> TargetAddrStart(
107 cl::desc("For -verify only: start of phony target address "
109 cl::init(4096), // Start at "page 1" - no allocating at "null".
112 static cl::opt
<uint64_t> TargetAddrEnd(
114 cl::desc("For -verify only: end of phony target address range."),
115 cl::init(~0ULL), cl::Hidden
);
117 static cl::opt
<uint64_t> TargetSectionSep(
118 "target-section-sep",
119 cl::desc("For -verify only: Separation between sections in "
120 "phony target address space."),
121 cl::init(0), cl::Hidden
);
123 static cl::list
<std::string
>
124 SpecificSectionMappings("map-section",
125 cl::desc("For -verify only: Map a section to a "
126 "specific address."),
130 static cl::list
<std::string
>
131 DummySymbolMappings("dummy-extern",
132 cl::desc("For -verify only: Inject a symbol into the extern "
138 PrintAllocationRequests("print-alloc-requests",
139 cl::desc("Print allocation requests made to the memory "
140 "manager by RuntimeDyld"),
143 static cl::opt
<bool> ShowTimes("show-times",
144 cl::desc("Show times for llvm-rtdyld phases"),
147 ExitOnError ExitOnErr
;
149 struct RTDyldTimers
{
150 TimerGroup RTDyldTG
{"llvm-rtdyld timers", "timers for llvm-rtdyld phases"};
151 Timer LoadObjectsTimer
{"load", "time to load/add object files", RTDyldTG
};
152 Timer LinkTimer
{"link", "time to link object files", RTDyldTG
};
153 Timer RunTimer
{"run", "time to execute jitlink'd code", RTDyldTG
};
156 std::unique_ptr
<RTDyldTimers
> Timers
;
160 using SectionIDMap
= StringMap
<unsigned>;
161 using FileToSectionIDMap
= StringMap
<SectionIDMap
>;
163 void dumpFileToSectionIDMap(const FileToSectionIDMap
&FileToSecIDMap
) {
164 for (const auto &KV
: FileToSecIDMap
) {
165 llvm::dbgs() << "In " << KV
.first() << "\n";
166 for (auto &KV2
: KV
.second
)
167 llvm::dbgs() << " \"" << KV2
.first() << "\" -> " << KV2
.second
<< "\n";
171 Expected
<unsigned> getSectionId(const FileToSectionIDMap
&FileToSecIDMap
,
172 StringRef FileName
, StringRef SectionName
) {
173 auto I
= FileToSecIDMap
.find(FileName
);
174 if (I
== FileToSecIDMap
.end())
175 return make_error
<StringError
>("No file named " + FileName
,
176 inconvertibleErrorCode());
177 auto &SectionIDs
= I
->second
;
178 auto J
= SectionIDs
.find(SectionName
);
179 if (J
== SectionIDs
.end())
180 return make_error
<StringError
>("No section named \"" + SectionName
+
181 "\" in file " + FileName
,
182 inconvertibleErrorCode());
186 // A trivial memory manager that doesn't do anything fancy, just uses the
187 // support library allocation routines directly.
188 class TrivialMemoryManager
: public RTDyldMemoryManager
{
191 SectionInfo(StringRef Name
, sys::MemoryBlock MB
, unsigned SectionID
)
192 : Name(Name
), MB(std::move(MB
)), SectionID(SectionID
) {}
195 unsigned SectionID
= ~0U;
198 SmallVector
<SectionInfo
, 16> FunctionMemory
;
199 SmallVector
<SectionInfo
, 16> DataMemory
;
201 uint8_t *allocateCodeSection(uintptr_t Size
, unsigned Alignment
,
203 StringRef SectionName
) override
;
204 uint8_t *allocateDataSection(uintptr_t Size
, unsigned Alignment
,
205 unsigned SectionID
, StringRef SectionName
,
206 bool IsReadOnly
) override
;
208 /// If non null, records subsequent Name -> SectionID mappings.
209 void setSectionIDsMap(SectionIDMap
*SecIDMap
) {
210 this->SecIDMap
= SecIDMap
;
213 void *getPointerToNamedFunction(const std::string
&Name
,
214 bool AbortOnFailure
= true) override
{
218 bool finalizeMemory(std::string
*ErrMsg
) override
{ return false; }
220 void addDummySymbol(const std::string
&Name
, uint64_t Addr
) {
221 DummyExterns
[Name
] = Addr
;
224 JITSymbol
findSymbol(const std::string
&Name
) override
{
225 auto I
= DummyExterns
.find(Name
);
227 if (I
!= DummyExterns
.end())
228 return JITSymbol(I
->second
, JITSymbolFlags::Exported
);
230 if (auto Sym
= RTDyldMemoryManager::findSymbol(Name
))
232 else if (auto Err
= Sym
.takeError())
233 ExitOnErr(std::move(Err
));
235 ExitOnErr(make_error
<StringError
>("Could not find definition for \"" +
237 inconvertibleErrorCode()));
238 llvm_unreachable("Should have returned or exited by now");
241 void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
,
242 size_t Size
) override
{}
243 void deregisterEHFrames() override
{}
245 void preallocateSlab(uint64_t Size
) {
247 sys::MemoryBlock MB
=
248 sys::Memory::allocateMappedMemory(Size
, nullptr,
249 sys::Memory::MF_READ
|
250 sys::Memory::MF_WRITE
,
253 report_fatal_error("Can't allocate enough memory: " + EC
.message());
256 UsePreallocation
= true;
260 uint8_t *allocateFromSlab(uintptr_t Size
, unsigned Alignment
, bool isCode
,
261 StringRef SectionName
, unsigned SectionID
) {
262 Size
= alignTo(Size
, Alignment
);
263 if (CurrentSlabOffset
+ Size
> SlabSize
)
264 report_fatal_error("Can't allocate enough memory. Tune --preallocate");
266 uintptr_t OldSlabOffset
= CurrentSlabOffset
;
267 sys::MemoryBlock
MB((void *)OldSlabOffset
, Size
);
269 FunctionMemory
.push_back(SectionInfo(SectionName
, MB
, SectionID
));
271 DataMemory
.push_back(SectionInfo(SectionName
, MB
, SectionID
));
272 CurrentSlabOffset
+= Size
;
273 return (uint8_t*)OldSlabOffset
;
277 std::map
<std::string
, uint64_t> DummyExterns
;
278 sys::MemoryBlock PreallocSlab
;
279 bool UsePreallocation
= false;
280 uintptr_t SlabSize
= 0;
281 uintptr_t CurrentSlabOffset
= 0;
282 SectionIDMap
*SecIDMap
= nullptr;
285 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size
,
288 StringRef SectionName
) {
289 if (PrintAllocationRequests
)
290 outs() << "allocateCodeSection(Size = " << Size
<< ", Alignment = "
291 << Alignment
<< ", SectionName = " << SectionName
<< ")\n";
294 (*SecIDMap
)[SectionName
] = SectionID
;
296 if (UsePreallocation
)
297 return allocateFromSlab(Size
, Alignment
, true /* isCode */,
298 SectionName
, SectionID
);
301 sys::MemoryBlock MB
=
302 sys::Memory::allocateMappedMemory(Size
, nullptr,
303 sys::Memory::MF_READ
|
304 sys::Memory::MF_WRITE
,
307 report_fatal_error("MemoryManager allocation failed: " + EC
.message());
308 FunctionMemory
.push_back(SectionInfo(SectionName
, MB
, SectionID
));
309 return (uint8_t*)MB
.base();
312 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size
,
315 StringRef SectionName
,
317 if (PrintAllocationRequests
)
318 outs() << "allocateDataSection(Size = " << Size
<< ", Alignment = "
319 << Alignment
<< ", SectionName = " << SectionName
<< ")\n";
322 (*SecIDMap
)[SectionName
] = SectionID
;
324 if (UsePreallocation
)
325 return allocateFromSlab(Size
, Alignment
, false /* isCode */, SectionName
,
329 sys::MemoryBlock MB
=
330 sys::Memory::allocateMappedMemory(Size
, nullptr,
331 sys::Memory::MF_READ
|
332 sys::Memory::MF_WRITE
,
335 report_fatal_error("MemoryManager allocation failed: " + EC
.message());
336 DataMemory
.push_back(SectionInfo(SectionName
, MB
, SectionID
));
337 return (uint8_t*)MB
.base();
340 static const char *ProgramName
;
342 static void ErrorAndExit(const Twine
&Msg
) {
343 errs() << ProgramName
<< ": error: " << Msg
<< "\n";
347 static void loadDylibs() {
348 for (const std::string
&Dylib
: Dylibs
) {
349 if (!sys::fs::is_regular_file(Dylib
))
350 report_fatal_error("Dylib not found: '" + Dylib
+ "'.");
352 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib
.c_str(), &ErrMsg
))
353 report_fatal_error("Error loading '" + Dylib
+ "': " + ErrMsg
);
359 static int printLineInfoForInput(bool LoadObjects
, bool UseDebugObj
) {
360 assert(LoadObjects
|| !UseDebugObj
);
362 // Load any dylibs requested on the command line.
365 // If we don't have any input files, read from stdin.
366 if (!InputFileList
.size())
367 InputFileList
.push_back("-");
368 for (auto &File
: InputFileList
) {
369 // Instantiate a dynamic linker.
370 TrivialMemoryManager MemMgr
;
371 RuntimeDyld
Dyld(MemMgr
, MemMgr
);
373 // Load the input memory buffer.
375 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> InputBuffer
=
376 MemoryBuffer::getFileOrSTDIN(File
);
377 if (std::error_code EC
= InputBuffer
.getError())
378 ErrorAndExit("unable to read input: '" + EC
.message() + "'");
380 Expected
<std::unique_ptr
<ObjectFile
>> MaybeObj(
381 ObjectFile::createObjectFile((*InputBuffer
)->getMemBufferRef()));
385 raw_string_ostream
OS(Buf
);
386 logAllUnhandledErrors(MaybeObj
.takeError(), OS
);
388 ErrorAndExit("unable to create object file: '" + Buf
+ "'");
391 ObjectFile
&Obj
= **MaybeObj
;
393 OwningBinary
<ObjectFile
> DebugObj
;
394 std::unique_ptr
<RuntimeDyld::LoadedObjectInfo
> LoadedObjInfo
= nullptr;
395 ObjectFile
*SymbolObj
= &Obj
;
397 // Load the object file
399 Dyld
.loadObject(Obj
);
402 ErrorAndExit(Dyld
.getErrorString());
404 // Resolve all the relocations we can.
405 Dyld
.resolveRelocations();
408 DebugObj
= LoadedObjInfo
->getObjectForDebug(Obj
);
409 SymbolObj
= DebugObj
.getBinary();
410 LoadedObjInfo
.reset();
414 std::unique_ptr
<DIContext
> Context
=
415 DWARFContext::create(*SymbolObj
, LoadedObjInfo
.get());
417 std::vector
<std::pair
<SymbolRef
, uint64_t>> SymAddr
=
418 object::computeSymbolSizes(*SymbolObj
);
420 // Use symbol info to iterate functions in the object.
421 for (const auto &P
: SymAddr
) {
422 object::SymbolRef Sym
= P
.first
;
423 Expected
<SymbolRef::Type
> TypeOrErr
= Sym
.getType();
425 // TODO: Actually report errors helpfully.
426 consumeError(TypeOrErr
.takeError());
429 SymbolRef::Type Type
= *TypeOrErr
;
430 if (Type
== object::SymbolRef::ST_Function
) {
431 Expected
<StringRef
> Name
= Sym
.getName();
433 // TODO: Actually report errors helpfully.
434 consumeError(Name
.takeError());
437 Expected
<uint64_t> AddrOrErr
= Sym
.getAddress();
439 // TODO: Actually report errors helpfully.
440 consumeError(AddrOrErr
.takeError());
443 uint64_t Addr
= *AddrOrErr
;
445 object::SectionedAddress Address
;
447 uint64_t Size
= P
.second
;
448 // If we're not using the debug object, compute the address of the
449 // symbol in memory (rather than that in the unrelocated object file)
450 // and use that to query the DWARFContext.
451 if (!UseDebugObj
&& LoadObjects
) {
452 auto SecOrErr
= Sym
.getSection();
454 // TODO: Actually report errors helpfully.
455 consumeError(SecOrErr
.takeError());
458 object::section_iterator Sec
= *SecOrErr
;
459 Address
.SectionIndex
= Sec
->getIndex();
460 uint64_t SectionLoadAddress
=
461 LoadedObjInfo
->getSectionLoadAddress(*Sec
);
462 if (SectionLoadAddress
!= 0)
463 Addr
+= SectionLoadAddress
- Sec
->getAddress();
464 } else if (auto SecOrErr
= Sym
.getSection())
465 Address
.SectionIndex
= SecOrErr
.get()->getIndex();
467 outs() << "Function: " << *Name
<< ", Size = " << Size
468 << ", Addr = " << Addr
<< "\n";
470 Address
.Address
= Addr
;
471 DILineInfoTable Lines
=
472 Context
->getLineInfoForAddressRange(Address
, Size
);
473 for (auto &D
: Lines
) {
474 outs() << " Line info @ " << D
.first
- Addr
<< ": "
475 << D
.second
.FileName
<< ", line:" << D
.second
.Line
<< "\n";
484 static void doPreallocation(TrivialMemoryManager
&MemMgr
) {
485 // Allocate a slab of memory upfront, if required. This is used if
486 // we want to test small code models.
487 if (static_cast<intptr_t>(PreallocMemory
) < 0)
488 report_fatal_error("Pre-allocated bytes of memory must be a positive integer.");
490 // FIXME: Limit the amount of memory that can be preallocated?
491 if (PreallocMemory
!= 0)
492 MemMgr
.preallocateSlab(PreallocMemory
);
495 static int executeInput() {
496 // Load any dylibs requested on the command line.
499 // Instantiate a dynamic linker.
500 TrivialMemoryManager MemMgr
;
501 doPreallocation(MemMgr
);
502 RuntimeDyld
Dyld(MemMgr
, MemMgr
);
504 // If we don't have any input files, read from stdin.
505 if (!InputFileList
.size())
506 InputFileList
.push_back("-");
508 TimeRegion
TR(Timers
? &Timers
->LoadObjectsTimer
: nullptr);
509 for (auto &File
: InputFileList
) {
510 // Load the input memory buffer.
511 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> InputBuffer
=
512 MemoryBuffer::getFileOrSTDIN(File
);
513 if (std::error_code EC
= InputBuffer
.getError())
514 ErrorAndExit("unable to read input: '" + EC
.message() + "'");
515 Expected
<std::unique_ptr
<ObjectFile
>> MaybeObj(
516 ObjectFile::createObjectFile((*InputBuffer
)->getMemBufferRef()));
520 raw_string_ostream
OS(Buf
);
521 logAllUnhandledErrors(MaybeObj
.takeError(), OS
);
523 ErrorAndExit("unable to create object file: '" + Buf
+ "'");
526 ObjectFile
&Obj
= **MaybeObj
;
528 // Load the object file
529 Dyld
.loadObject(Obj
);
530 if (Dyld
.hasError()) {
531 ErrorAndExit(Dyld
.getErrorString());
537 TimeRegion
TR(Timers
? &Timers
->LinkTimer
: nullptr);
538 // Resove all the relocations we can.
539 // FIXME: Error out if there are unresolved relocations.
540 Dyld
.resolveRelocations();
543 // Get the address of the entry point (_main by default).
544 void *MainAddress
= Dyld
.getSymbolLocalAddress(EntryPoint
);
546 ErrorAndExit("no definition for '" + EntryPoint
+ "'");
548 // Invalidate the instruction cache for each loaded function.
549 for (auto &FM
: MemMgr
.FunctionMemory
) {
553 // Make sure the memory is executable.
554 // setExecutable will call InvalidateInstructionCache.
555 if (auto EC
= sys::Memory::protectMappedMemory(FM_MB
,
556 sys::Memory::MF_READ
|
557 sys::Memory::MF_EXEC
))
558 ErrorAndExit("unable to mark function executable: '" + EC
.message() +
562 // Dispatch to _main().
563 errs() << "loaded '" << EntryPoint
<< "' at: " << (void*)MainAddress
<< "\n";
565 int (*Main
)(int, const char**) =
566 (int(*)(int,const char**)) uintptr_t(MainAddress
);
567 std::vector
<const char *> Argv
;
568 // Use the name of the first input object module as argv[0] for the target.
569 Argv
.push_back(InputFileList
[0].data());
570 for (auto &Arg
: InputArgv
)
571 Argv
.push_back(Arg
.data());
572 Argv
.push_back(nullptr);
575 TimeRegion
TR(Timers
? &Timers
->RunTimer
: nullptr);
576 Result
= Main(Argv
.size() - 1, Argv
.data());
582 static int checkAllExpressions(RuntimeDyldChecker
&Checker
) {
583 for (const auto& CheckerFileName
: CheckFiles
) {
584 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> CheckerFileBuf
=
585 MemoryBuffer::getFileOrSTDIN(CheckerFileName
);
586 if (std::error_code EC
= CheckerFileBuf
.getError())
587 ErrorAndExit("unable to read input '" + CheckerFileName
+ "': " +
590 if (!Checker
.checkAllRulesInBuffer("# rtdyld-check:",
591 CheckerFileBuf
.get().get()))
592 ErrorAndExit("some checks in '" + CheckerFileName
+ "' failed");
597 void applySpecificSectionMappings(RuntimeDyld
&Dyld
,
598 const FileToSectionIDMap
&FileToSecIDMap
) {
600 for (StringRef Mapping
: SpecificSectionMappings
) {
601 size_t EqualsIdx
= Mapping
.find_first_of("=");
602 std::string SectionIDStr
= Mapping
.substr(0, EqualsIdx
);
603 size_t ComaIdx
= Mapping
.find_first_of(",");
605 if (ComaIdx
== StringRef::npos
)
606 report_fatal_error("Invalid section specification '" + Mapping
+
607 "'. Should be '<file name>,<section name>=<addr>'");
609 std::string FileName
= SectionIDStr
.substr(0, ComaIdx
);
610 std::string SectionName
= SectionIDStr
.substr(ComaIdx
+ 1);
612 ExitOnErr(getSectionId(FileToSecIDMap
, FileName
, SectionName
));
614 auto* OldAddr
= Dyld
.getSectionContent(SectionID
).data();
615 std::string NewAddrStr
= Mapping
.substr(EqualsIdx
+ 1);
618 if (StringRef(NewAddrStr
).getAsInteger(0, NewAddr
))
619 report_fatal_error("Invalid section address in mapping '" + Mapping
+
622 Dyld
.mapSectionAddress(OldAddr
, NewAddr
);
626 // Scatter sections in all directions!
627 // Remaps section addresses for -verify mode. The following command line options
628 // can be used to customize the layout of the memory within the phony target's
630 // -target-addr-start <s> -- Specify where the phony target address range starts.
631 // -target-addr-end <e> -- Specify where the phony target address range ends.
632 // -target-section-sep <d> -- Specify how big a gap should be left between the
633 // end of one section and the start of the next.
634 // Defaults to zero. Set to something big
635 // (e.g. 1 << 32) to stress-test stubs, GOTs, etc.
637 static void remapSectionsAndSymbols(const llvm::Triple
&TargetTriple
,
639 TrivialMemoryManager
&MemMgr
) {
641 // Set up a work list (section addr/size pairs).
642 typedef std::list
<const TrivialMemoryManager::SectionInfo
*> WorklistT
;
645 for (const auto& CodeSection
: MemMgr
.FunctionMemory
)
646 Worklist
.push_back(&CodeSection
);
647 for (const auto& DataSection
: MemMgr
.DataMemory
)
648 Worklist
.push_back(&DataSection
);
650 // Keep an "already allocated" mapping of section target addresses to sizes.
651 // Sections whose address mappings aren't specified on the command line will
652 // allocated around the explicitly mapped sections while maintaining the
653 // minimum separation.
654 std::map
<uint64_t, uint64_t> AlreadyAllocated
;
656 // Move the previously applied mappings (whether explicitly specified on the
657 // command line, or implicitly set by RuntimeDyld) into the already-allocated
659 for (WorklistT::iterator I
= Worklist
.begin(), E
= Worklist
.end();
661 WorklistT::iterator Tmp
= I
;
664 auto LoadAddr
= Dyld
.getSectionLoadAddress((*Tmp
)->SectionID
);
666 if (LoadAddr
!= static_cast<uint64_t>(
667 reinterpret_cast<uintptr_t>((*Tmp
)->MB
.base()))) {
668 // A section will have a LoadAddr of 0 if it wasn't loaded for whatever
669 // reason (e.g. zero byte COFF sections). Don't include those sections in
670 // the allocation map.
672 AlreadyAllocated
[LoadAddr
] = (*Tmp
)->MB
.allocatedSize();
677 // If the -target-addr-end option wasn't explicitly passed, then set it to a
678 // sensible default based on the target triple.
679 if (TargetAddrEnd
.getNumOccurrences() == 0) {
680 if (TargetTriple
.isArch16Bit())
681 TargetAddrEnd
= (1ULL << 16) - 1;
682 else if (TargetTriple
.isArch32Bit())
683 TargetAddrEnd
= (1ULL << 32) - 1;
684 // TargetAddrEnd already has a sensible default for 64-bit systems, so
685 // there's nothing to do in the 64-bit case.
688 // Process any elements remaining in the worklist.
689 while (!Worklist
.empty()) {
690 auto *CurEntry
= Worklist
.front();
691 Worklist
.pop_front();
693 uint64_t NextSectionAddr
= TargetAddrStart
;
695 for (const auto &Alloc
: AlreadyAllocated
)
696 if (NextSectionAddr
+ CurEntry
->MB
.allocatedSize() + TargetSectionSep
<=
700 NextSectionAddr
= Alloc
.first
+ Alloc
.second
+ TargetSectionSep
;
702 Dyld
.mapSectionAddress(CurEntry
->MB
.base(), NextSectionAddr
);
703 AlreadyAllocated
[NextSectionAddr
] = CurEntry
->MB
.allocatedSize();
706 // Add dummy symbols to the memory manager.
707 for (const auto &Mapping
: DummySymbolMappings
) {
708 size_t EqualsIdx
= Mapping
.find_first_of('=');
710 if (EqualsIdx
== StringRef::npos
)
711 report_fatal_error("Invalid dummy symbol specification '" + Mapping
+
712 "'. Should be '<symbol name>=<addr>'");
714 std::string Symbol
= Mapping
.substr(0, EqualsIdx
);
715 std::string AddrStr
= Mapping
.substr(EqualsIdx
+ 1);
718 if (StringRef(AddrStr
).getAsInteger(0, Addr
))
719 report_fatal_error("Invalid symbol mapping '" + Mapping
+ "'.");
721 MemMgr
.addDummySymbol(Symbol
, Addr
);
725 // Load and link the objects specified on the command line, but do not execute
726 // anything. Instead, attach a RuntimeDyldChecker instance and call it to
727 // verify the correctness of the linked memory.
728 static int linkAndVerify() {
730 // Check for missing triple.
731 if (TripleName
== "")
732 ErrorAndExit("-triple required when running in -verify mode.");
734 // Look up the target and build the disassembler.
735 Triple
TheTriple(Triple::normalize(TripleName
));
736 std::string ErrorStr
;
737 const Target
*TheTarget
=
738 TargetRegistry::lookupTarget("", TheTriple
, ErrorStr
);
740 ErrorAndExit("Error accessing target '" + TripleName
+ "': " + ErrorStr
);
742 TripleName
= TheTriple
.getTriple();
744 std::unique_ptr
<MCSubtargetInfo
> STI(
745 TheTarget
->createMCSubtargetInfo(TripleName
, MCPU
, ""));
747 ErrorAndExit("Unable to create subtarget info!");
749 std::unique_ptr
<MCRegisterInfo
> MRI(TheTarget
->createMCRegInfo(TripleName
));
751 ErrorAndExit("Unable to create target register info!");
753 MCTargetOptions MCOptions
;
754 std::unique_ptr
<MCAsmInfo
> MAI(
755 TheTarget
->createMCAsmInfo(*MRI
, TripleName
, MCOptions
));
757 ErrorAndExit("Unable to create target asm info!");
759 MCContext
Ctx(MAI
.get(), MRI
.get(), nullptr);
761 std::unique_ptr
<MCDisassembler
> Disassembler(
762 TheTarget
->createMCDisassembler(*STI
, Ctx
));
764 ErrorAndExit("Unable to create disassembler!");
766 std::unique_ptr
<MCInstrInfo
> MII(TheTarget
->createMCInstrInfo());
768 std::unique_ptr
<MCInstPrinter
> InstPrinter(
769 TheTarget
->createMCInstPrinter(Triple(TripleName
), 0, *MAI
, *MII
, *MRI
));
771 // Load any dylibs requested on the command line.
774 // Instantiate a dynamic linker.
775 TrivialMemoryManager MemMgr
;
776 doPreallocation(MemMgr
);
782 using StubInfos
= StringMap
<StubID
>;
783 using StubContainers
= StringMap
<StubInfos
>;
785 StubContainers StubMap
;
786 RuntimeDyld
Dyld(MemMgr
, MemMgr
);
787 Dyld
.setProcessAllSections(true);
789 Dyld
.setNotifyStubEmitted([&StubMap
](StringRef FilePath
,
790 StringRef SectionName
,
791 StringRef SymbolName
, unsigned SectionID
,
792 uint32_t StubOffset
) {
793 std::string ContainerName
=
794 (sys::path::filename(FilePath
) + "/" + SectionName
).str();
795 StubMap
[ContainerName
][SymbolName
] = {SectionID
, StubOffset
};
800 StringRef Symbol
) -> Expected
<RuntimeDyldChecker::MemoryRegionInfo
> {
801 RuntimeDyldChecker::MemoryRegionInfo SymInfo
;
803 // First get the target address.
804 if (auto InternalSymbol
= Dyld
.getSymbol(Symbol
))
805 SymInfo
.setTargetAddress(InternalSymbol
.getAddress());
807 // Symbol not found in RuntimeDyld. Fall back to external lookup.
809 using ExpectedLookupResult
=
810 MSVCPExpected
<JITSymbolResolver::LookupResult
>;
812 using ExpectedLookupResult
= Expected
<JITSymbolResolver::LookupResult
>;
815 auto ResultP
= std::make_shared
<std::promise
<ExpectedLookupResult
>>();
816 auto ResultF
= ResultP
->get_future();
818 MemMgr
.lookup(JITSymbolResolver::LookupSet({Symbol
}),
819 [=](Expected
<JITSymbolResolver::LookupResult
> Result
) {
820 ResultP
->set_value(std::move(Result
));
823 auto Result
= ResultF
.get();
825 return Result
.takeError();
827 auto I
= Result
->find(Symbol
);
828 assert(I
!= Result
->end() &&
829 "Expected symbol address if no error occurred");
830 SymInfo
.setTargetAddress(I
->second
.getAddress());
833 // Now find the symbol content if possible (otherwise leave content as a
834 // default-constructed StringRef).
835 if (auto *SymAddr
= Dyld
.getSymbolLocalAddress(Symbol
)) {
836 unsigned SectionID
= Dyld
.getSymbolSectionID(Symbol
);
837 if (SectionID
!= ~0U) {
838 char *CSymAddr
= static_cast<char *>(SymAddr
);
839 StringRef SecContent
= Dyld
.getSectionContent(SectionID
);
840 uint64_t SymSize
= SecContent
.size() - (CSymAddr
- SecContent
.data());
841 SymInfo
.setContent(StringRef(CSymAddr
, SymSize
));
847 auto IsSymbolValid
= [&Dyld
, GetSymbolInfo
](StringRef Symbol
) {
848 if (Dyld
.getSymbol(Symbol
))
850 auto SymInfo
= GetSymbolInfo(Symbol
);
852 logAllUnhandledErrors(SymInfo
.takeError(), errs(), "RTDyldChecker: ");
855 return SymInfo
->getTargetAddress() != 0;
858 FileToSectionIDMap FileToSecIDMap
;
860 auto GetSectionInfo
= [&Dyld
, &FileToSecIDMap
](StringRef FileName
,
861 StringRef SectionName
)
862 -> Expected
<RuntimeDyldChecker::MemoryRegionInfo
> {
863 auto SectionID
= getSectionId(FileToSecIDMap
, FileName
, SectionName
);
865 return SectionID
.takeError();
866 RuntimeDyldChecker::MemoryRegionInfo SecInfo
;
867 SecInfo
.setTargetAddress(Dyld
.getSectionLoadAddress(*SectionID
));
868 SecInfo
.setContent(Dyld
.getSectionContent(*SectionID
));
872 auto GetStubInfo
= [&Dyld
, &StubMap
](StringRef StubContainer
,
873 StringRef SymbolName
)
874 -> Expected
<RuntimeDyldChecker::MemoryRegionInfo
> {
875 if (!StubMap
.count(StubContainer
))
876 return make_error
<StringError
>("Stub container not found: " +
878 inconvertibleErrorCode());
879 if (!StubMap
[StubContainer
].count(SymbolName
))
880 return make_error
<StringError
>("Symbol name " + SymbolName
+
881 " in stub container " + StubContainer
,
882 inconvertibleErrorCode());
883 auto &SI
= StubMap
[StubContainer
][SymbolName
];
884 RuntimeDyldChecker::MemoryRegionInfo StubMemInfo
;
885 StubMemInfo
.setTargetAddress(Dyld
.getSectionLoadAddress(SI
.SectionID
) +
887 StubMemInfo
.setContent(
888 Dyld
.getSectionContent(SI
.SectionID
).substr(SI
.Offset
));
892 // We will initialize this below once we have the first object file and can
893 // know the endianness.
894 std::unique_ptr
<RuntimeDyldChecker
> Checker
;
896 // If we don't have any input files, read from stdin.
897 if (!InputFileList
.size())
898 InputFileList
.push_back("-");
899 for (auto &InputFile
: InputFileList
) {
900 // Load the input memory buffer.
901 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> InputBuffer
=
902 MemoryBuffer::getFileOrSTDIN(InputFile
);
904 if (std::error_code EC
= InputBuffer
.getError())
905 ErrorAndExit("unable to read input: '" + EC
.message() + "'");
907 Expected
<std::unique_ptr
<ObjectFile
>> MaybeObj(
908 ObjectFile::createObjectFile((*InputBuffer
)->getMemBufferRef()));
912 raw_string_ostream
OS(Buf
);
913 logAllUnhandledErrors(MaybeObj
.takeError(), OS
);
915 ErrorAndExit("unable to create object file: '" + Buf
+ "'");
918 ObjectFile
&Obj
= **MaybeObj
;
921 Checker
= std::make_unique
<RuntimeDyldChecker
>(
922 IsSymbolValid
, GetSymbolInfo
, GetSectionInfo
, GetStubInfo
,
923 GetStubInfo
, Obj
.isLittleEndian() ? support::little
: support::big
,
924 Disassembler
.get(), InstPrinter
.get(), dbgs());
926 auto FileName
= sys::path::filename(InputFile
);
927 MemMgr
.setSectionIDsMap(&FileToSecIDMap
[FileName
]);
929 // Load the object file
930 Dyld
.loadObject(Obj
);
931 if (Dyld
.hasError()) {
932 ErrorAndExit(Dyld
.getErrorString());
936 // Re-map the section addresses into the phony target address space and add
938 applySpecificSectionMappings(Dyld
, FileToSecIDMap
);
939 remapSectionsAndSymbols(TheTriple
, Dyld
, MemMgr
);
941 // Resolve all the relocations we can.
942 Dyld
.resolveRelocations();
944 // Register EH frames.
945 Dyld
.registerEHFrames();
947 int ErrorCode
= checkAllExpressions(*Checker
);
949 ErrorAndExit("RTDyld reported an error applying relocations:\n " +
950 Dyld
.getErrorString());
955 int main(int argc
, char **argv
) {
956 InitLLVM
X(argc
, argv
);
957 ProgramName
= argv
[0];
959 llvm::InitializeAllTargetInfos();
960 llvm::InitializeAllTargetMCs();
961 llvm::InitializeAllDisassemblers();
963 cl::ParseCommandLineOptions(argc
, argv
, "llvm MC-JIT tool\n");
965 ExitOnErr
.setBanner(std::string(argv
[0]) + ": ");
967 Timers
= ShowTimes
? std::make_unique
<RTDyldTimers
>() : nullptr;
972 Result
= executeInput();
974 case AC_PrintDebugLineInfo
:
976 printLineInfoForInput(/* LoadObjects */ true, /* UseDebugObj */ true);
978 case AC_PrintLineInfo
:
980 printLineInfoForInput(/* LoadObjects */ true, /* UseDebugObj */ false);
982 case AC_PrintObjectLineInfo
:
984 printLineInfoForInput(/* LoadObjects */ false, /* UseDebugObj */ false);
987 Result
= linkAndVerify();