1 //===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is a testing tool for use with the MC-JIT LLVM components.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/DebugInfo/DIContext.h"
16 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
17 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
18 #include "llvm/ExecutionEngine/RuntimeDyld.h"
19 #include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCDisassembler.h"
23 #include "llvm/MC/MCInstPrinter.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/Object/MachO.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/DynamicLibrary.h"
30 #include "llvm/Support/ManagedStatic.h"
31 #include "llvm/Support/Memory.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/PrettyStackTrace.h"
34 #include "llvm/Support/Signals.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/TargetSelect.h"
37 #include "llvm/Support/raw_ostream.h"
39 #include <system_error>
42 using namespace llvm::object;
44 static cl::list<std::string>
45 InputFileList(cl::Positional, cl::ZeroOrMore,
46 cl::desc("<input file>"));
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."),
71 static cl::opt<std::string>
73 cl::desc("Function to call as entry point."),
76 static cl::list<std::string>
78 cl::desc("Add library."),
81 static cl::opt<std::string>
82 TripleName("triple", cl::desc("Target triple for disassembler"));
84 static cl::list<std::string>
86 cl::desc("File containing RuntimeDyld verifier checks."),
89 static cl::opt<uint64_t>
90 TargetAddrStart("target-addr-start",
91 cl::desc("For -verify only: start of phony target address "
93 cl::init(4096), // Start at "page 1" - no allocating at "null".
96 static cl::opt<uint64_t>
97 TargetAddrEnd("target-addr-end",
98 cl::desc("For -verify only: end of phony target address range."),
102 static cl::opt<uint64_t>
103 TargetSectionSep("target-section-sep",
104 cl::desc("For -verify only: Separation between sections in "
105 "phony target address space."),
109 static cl::list<std::string>
110 SpecificSectionMappings("map-section",
111 cl::desc("Map a section to a specific address."),
116 // A trivial memory manager that doesn't do anything fancy, just uses the
117 // support library allocation routines directly.
118 class TrivialMemoryManager : public RTDyldMemoryManager {
120 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
121 SmallVector<sys::MemoryBlock, 16> DataMemory;
123 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
125 StringRef SectionName) override;
126 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
127 unsigned SectionID, StringRef SectionName,
128 bool IsReadOnly) override;
130 void *getPointerToNamedFunction(const std::string &Name,
131 bool AbortOnFailure = true) override {
135 bool finalizeMemory(std::string *ErrMsg) override { return false; }
137 // Invalidate instruction cache for sections with execute permissions.
138 // Some platforms with separate data cache and instruction cache require
139 // explicit cache flush, otherwise JIT code manipulations (like resolved
140 // relocations) will get to the data cache but not to the instruction cache.
141 virtual void invalidateInstructionCache();
143 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
144 size_t Size) override {}
145 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
146 size_t Size) override {}
149 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
152 StringRef SectionName) {
153 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
154 FunctionMemory.push_back(MB);
155 return (uint8_t*)MB.base();
158 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
161 StringRef SectionName,
163 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
164 DataMemory.push_back(MB);
165 return (uint8_t*)MB.base();
168 void TrivialMemoryManager::invalidateInstructionCache() {
169 for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
170 sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
171 FunctionMemory[i].size());
173 for (int i = 0, e = DataMemory.size(); i != e; ++i)
174 sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
175 DataMemory[i].size());
178 static const char *ProgramName;
180 static void Message(const char *Type, const Twine &Msg) {
181 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
184 static int Error(const Twine &Msg) {
185 Message("error", Msg);
189 static void loadDylibs() {
190 for (const std::string &Dylib : Dylibs) {
191 if (sys::fs::is_regular_file(Dylib)) {
193 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
194 llvm::errs() << "Error loading '" << Dylib << "': "
197 llvm::errs() << "Dylib not found: '" << Dylib << "'.\n";
203 static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
204 assert(LoadObjects || !UseDebugObj);
206 // Load any dylibs requested on the command line.
209 // If we don't have any input files, read from stdin.
210 if (!InputFileList.size())
211 InputFileList.push_back("-");
212 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
213 // Instantiate a dynamic linker.
214 TrivialMemoryManager MemMgr;
215 RuntimeDyld Dyld(MemMgr, MemMgr);
217 // Load the input memory buffer.
219 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
220 MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
221 if (std::error_code EC = InputBuffer.getError())
222 return Error("unable to read input: '" + EC.message() + "'");
224 ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
225 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
227 if (std::error_code EC = MaybeObj.getError())
228 return Error("unable to create object file: '" + EC.message() + "'");
230 ObjectFile &Obj = **MaybeObj;
232 OwningBinary<ObjectFile> DebugObj;
233 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = nullptr;
234 ObjectFile *SymbolObj = &Obj;
236 // Load the object file
238 Dyld.loadObject(Obj);
241 return Error(Dyld.getErrorString());
243 // Resolve all the relocations we can.
244 Dyld.resolveRelocations();
247 DebugObj = LoadedObjInfo->getObjectForDebug(Obj);
248 SymbolObj = DebugObj.getBinary();
252 std::unique_ptr<DIContext> Context(
253 new DWARFContextInMemory(*SymbolObj,LoadedObjInfo.get()));
255 // FIXME: This is generally useful. Figure out a place in lib/Object to
256 // put utility functions.
257 std::map<object::SectionRef, std::vector<uint64_t>> FuncAddresses;
258 if (!isa<ELFObjectFileBase>(SymbolObj)) {
259 for (object::SymbolRef Sym : SymbolObj->symbols()) {
260 object::SymbolRef::Type SymType;
261 if (Sym.getType(SymType))
263 if (SymType != object::SymbolRef::ST_Function)
266 if (Sym.getAddress(Addr))
268 object::section_iterator Sec = SymbolObj->section_end();
269 if (Sym.getSection(Sec))
271 std::vector<uint64_t> &Addrs = FuncAddresses[*Sec];
273 uint64_t SecAddr = Sec->getAddress();
274 uint64_t SecSize = Sec->getSize();
275 Addrs.push_back(SecAddr + SecSize);
277 Addrs.push_back(Addr);
279 for (auto &Pair : FuncAddresses) {
280 std::vector<uint64_t> &Addrs = Pair.second;
281 array_pod_sort(Addrs.begin(), Addrs.end());
285 // Use symbol info to iterate functions in the object.
286 for (object::SymbolRef Sym : SymbolObj->symbols()) {
287 object::SymbolRef::Type SymType;
288 if (Sym.getType(SymType))
290 if (SymType == object::SymbolRef::ST_Function) {
293 if (Sym.getName(Name))
295 if (Sym.getAddress(Addr))
299 if (isa<ELFObjectFileBase>(SymbolObj)) {
300 Size = Sym.getSize();
302 object::section_iterator Sec = SymbolObj->section_end();
303 if (Sym.getSection(Sec))
305 const std::vector<uint64_t> &Addrs = FuncAddresses[*Sec];
306 auto AddrI = std::find(Addrs.begin(), Addrs.end(), Addr);
307 assert(AddrI != Addrs.end() && (AddrI + 1) != Addrs.end());
308 assert(*AddrI == Addr);
309 Size = *(AddrI + 1) - Addr;
312 // If we're not using the debug object, compute the address of the
313 // symbol in memory (rather than that in the unrelocated object file)
314 // and use that to query the DWARFContext.
315 if (!UseDebugObj && LoadObjects) {
316 object::section_iterator Sec(SymbolObj->section_end());
319 Sec->getName(SecName);
320 uint64_t SectionLoadAddress =
321 LoadedObjInfo->getSectionLoadAddress(SecName);
322 if (SectionLoadAddress != 0)
323 Addr += SectionLoadAddress - Sec->getAddress();
326 outs() << "Function: " << Name << ", Size = " << Size << ", Addr = " << Addr << "\n";
328 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
329 DILineInfoTable::iterator Begin = Lines.begin();
330 DILineInfoTable::iterator End = Lines.end();
331 for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
332 outs() << " Line info @ " << It->first - Addr << ": "
333 << It->second.FileName << ", line:" << It->second.Line << "\n";
342 static int executeInput() {
343 // Load any dylibs requested on the command line.
346 // Instantiate a dynamic linker.
347 TrivialMemoryManager MemMgr;
348 RuntimeDyld Dyld(MemMgr, MemMgr);
350 // FIXME: Preserve buffers until resolveRelocations time to work around a bug
351 // in RuntimeDyldELF.
352 // This fixme should be fixed ASAP. This is a very brittle workaround.
353 std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers;
355 // If we don't have any input files, read from stdin.
356 if (!InputFileList.size())
357 InputFileList.push_back("-");
358 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
359 // Load the input memory buffer.
360 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
361 MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
362 if (std::error_code EC = InputBuffer.getError())
363 return Error("unable to read input: '" + EC.message() + "'");
364 ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
365 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
367 if (std::error_code EC = MaybeObj.getError())
368 return Error("unable to create object file: '" + EC.message() + "'");
370 ObjectFile &Obj = **MaybeObj;
371 InputBuffers.push_back(std::move(*InputBuffer));
373 // Load the object file
374 Dyld.loadObject(Obj);
375 if (Dyld.hasError()) {
376 return Error(Dyld.getErrorString());
380 // Resolve all the relocations we can.
381 Dyld.resolveRelocations();
382 // Clear instruction cache before code will be executed.
383 MemMgr.invalidateInstructionCache();
385 // FIXME: Error out if there are unresolved relocations.
387 // Get the address of the entry point (_main by default).
388 void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint);
390 return Error("no definition for '" + EntryPoint + "'");
392 // Invalidate the instruction cache for each loaded function.
393 for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) {
394 sys::MemoryBlock &Data = MemMgr.FunctionMemory[i];
395 // Make sure the memory is executable.
396 std::string ErrorStr;
397 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
398 if (!sys::Memory::setExecutable(Data, &ErrorStr))
399 return Error("unable to mark function executable: '" + ErrorStr + "'");
402 // Dispatch to _main().
403 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
405 int (*Main)(int, const char**) =
406 (int(*)(int,const char**)) uintptr_t(MainAddress);
407 const char **Argv = new const char*[2];
408 // Use the name of the first input object module as argv[0] for the target.
409 Argv[0] = InputFileList[0].c_str();
411 return Main(1, Argv);
414 static int checkAllExpressions(RuntimeDyldChecker &Checker) {
415 for (const auto& CheckerFileName : CheckFiles) {
416 ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
417 MemoryBuffer::getFileOrSTDIN(CheckerFileName);
418 if (std::error_code EC = CheckerFileBuf.getError())
419 return Error("unable to read input '" + CheckerFileName + "': " +
422 if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
423 CheckerFileBuf.get().get()))
424 return Error("some checks in '" + CheckerFileName + "' failed");
429 static std::map<void *, uint64_t>
430 applySpecificSectionMappings(RuntimeDyldChecker &Checker) {
432 std::map<void*, uint64_t> SpecificMappings;
434 for (StringRef Mapping : SpecificSectionMappings) {
436 size_t EqualsIdx = Mapping.find_first_of("=");
437 StringRef SectionIDStr = Mapping.substr(0, EqualsIdx);
438 size_t ComaIdx = Mapping.find_first_of(",");
440 if (ComaIdx == StringRef::npos) {
441 errs() << "Invalid section specification '" << Mapping
442 << "'. Should be '<file name>,<section name>=<addr>'\n";
446 StringRef FileName = SectionIDStr.substr(0, ComaIdx);
447 StringRef SectionName = SectionIDStr.substr(ComaIdx + 1);
450 std::string ErrorMsg;
451 std::tie(OldAddrInt, ErrorMsg) =
452 Checker.getSectionAddr(FileName, SectionName, true);
454 if (ErrorMsg != "") {
459 void* OldAddr = reinterpret_cast<void*>(static_cast<uintptr_t>(OldAddrInt));
461 StringRef NewAddrStr = Mapping.substr(EqualsIdx + 1);
464 if (NewAddrStr.getAsInteger(0, NewAddr)) {
465 errs() << "Invalid section address in mapping: " << Mapping << "\n";
469 Checker.getRTDyld().mapSectionAddress(OldAddr, NewAddr);
470 SpecificMappings[OldAddr] = NewAddr;
473 return SpecificMappings;
476 // Scatter sections in all directions!
477 // Remaps section addresses for -verify mode. The following command line options
478 // can be used to customize the layout of the memory within the phony target's
480 // -target-addr-start <s> -- Specify where the phony target addres range starts.
481 // -target-addr-end <e> -- Specify where the phony target address range ends.
482 // -target-section-sep <d> -- Specify how big a gap should be left between the
483 // end of one section and the start of the next.
484 // Defaults to zero. Set to something big
485 // (e.g. 1 << 32) to stress-test stubs, GOTs, etc.
487 static void remapSections(const llvm::Triple &TargetTriple,
488 const TrivialMemoryManager &MemMgr,
489 RuntimeDyldChecker &Checker) {
491 // Set up a work list (section addr/size pairs).
492 typedef std::list<std::pair<void*, uint64_t>> WorklistT;
495 for (const auto& CodeSection : MemMgr.FunctionMemory)
496 Worklist.push_back(std::make_pair(CodeSection.base(), CodeSection.size()));
497 for (const auto& DataSection : MemMgr.DataMemory)
498 Worklist.push_back(std::make_pair(DataSection.base(), DataSection.size()));
500 // Apply any section-specific mappings that were requested on the command
502 typedef std::map<void*, uint64_t> AppliedMappingsT;
503 AppliedMappingsT AppliedMappings = applySpecificSectionMappings(Checker);
505 // Keep an "already allocated" mapping of section target addresses to sizes.
506 // Sections whose address mappings aren't specified on the command line will
507 // allocated around the explicitly mapped sections while maintaining the
508 // minimum separation.
509 std::map<uint64_t, uint64_t> AlreadyAllocated;
511 // Move the previously applied mappings into the already-allocated map.
512 for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end();
514 WorklistT::iterator Tmp = I;
516 AppliedMappingsT::iterator AI = AppliedMappings.find(Tmp->first);
518 if (AI != AppliedMappings.end()) {
519 AlreadyAllocated[AI->second] = Tmp->second;
524 // If the -target-addr-end option wasn't explicitly passed, then set it to a
525 // sensible default based on the target triple.
526 if (TargetAddrEnd.getNumOccurrences() == 0) {
527 if (TargetTriple.isArch16Bit())
528 TargetAddrEnd = (1ULL << 16) - 1;
529 else if (TargetTriple.isArch32Bit())
530 TargetAddrEnd = (1ULL << 32) - 1;
531 // TargetAddrEnd already has a sensible default for 64-bit systems, so
532 // there's nothing to do in the 64-bit case.
535 // Process any elements remaining in the worklist.
536 while (!Worklist.empty()) {
537 std::pair<void*, uint64_t> CurEntry = Worklist.front();
538 Worklist.pop_front();
540 uint64_t NextSectionAddr = TargetAddrStart;
542 for (const auto &Alloc : AlreadyAllocated)
543 if (NextSectionAddr + CurEntry.second + TargetSectionSep <= Alloc.first)
546 NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep;
548 AlreadyAllocated[NextSectionAddr] = CurEntry.second;
549 Checker.getRTDyld().mapSectionAddress(CurEntry.first, NextSectionAddr);
554 // Load and link the objects specified on the command line, but do not execute
555 // anything. Instead, attach a RuntimeDyldChecker instance and call it to
556 // verify the correctness of the linked memory.
557 static int linkAndVerify() {
559 // Check for missing triple.
560 if (TripleName == "") {
561 llvm::errs() << "Error: -triple required when running in -verify mode.\n";
565 // Look up the target and build the disassembler.
566 Triple TheTriple(Triple::normalize(TripleName));
567 std::string ErrorStr;
568 const Target *TheTarget =
569 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
571 llvm::errs() << "Error accessing target '" << TripleName << "': "
575 TripleName = TheTriple.getTriple();
577 std::unique_ptr<MCSubtargetInfo> STI(
578 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
579 assert(STI && "Unable to create subtarget info!");
581 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
582 assert(MRI && "Unable to create target register info!");
584 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
585 assert(MAI && "Unable to create target asm info!");
587 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
589 std::unique_ptr<MCDisassembler> Disassembler(
590 TheTarget->createMCDisassembler(*STI, Ctx));
591 assert(Disassembler && "Unable to create disassembler!");
593 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
595 std::unique_ptr<MCInstPrinter> InstPrinter(
596 TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
598 // Load any dylibs requested on the command line.
601 // Instantiate a dynamic linker.
602 TrivialMemoryManager MemMgr;
603 RuntimeDyld Dyld(MemMgr, MemMgr);
604 Dyld.setProcessAllSections(true);
605 RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),
608 // FIXME: Preserve buffers until resolveRelocations time to work around a bug
609 // in RuntimeDyldELF.
610 // This fixme should be fixed ASAP. This is a very brittle workaround.
611 std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers;
613 // If we don't have any input files, read from stdin.
614 if (!InputFileList.size())
615 InputFileList.push_back("-");
616 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
617 // Load the input memory buffer.
618 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
619 MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
621 if (std::error_code EC = InputBuffer.getError())
622 return Error("unable to read input: '" + EC.message() + "'");
624 ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
625 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
627 if (std::error_code EC = MaybeObj.getError())
628 return Error("unable to create object file: '" + EC.message() + "'");
630 ObjectFile &Obj = **MaybeObj;
631 InputBuffers.push_back(std::move(*InputBuffer));
633 // Load the object file
634 Dyld.loadObject(Obj);
635 if (Dyld.hasError()) {
636 return Error(Dyld.getErrorString());
640 // Re-map the section addresses into the phony target address space.
641 remapSections(TheTriple, MemMgr, Checker);
643 // Resolve all the relocations we can.
644 Dyld.resolveRelocations();
646 // Register EH frames.
647 Dyld.registerEHFrames();
649 int ErrorCode = checkAllExpressions(Checker);
650 if (Dyld.hasError()) {
651 errs() << "RTDyld reported an error applying relocations:\n "
652 << Dyld.getErrorString() << "\n";
659 int main(int argc, char **argv) {
660 sys::PrintStackTraceOnErrorSignal();
661 PrettyStackTraceProgram X(argc, argv);
663 ProgramName = argv[0];
664 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
666 llvm::InitializeAllTargetInfos();
667 llvm::InitializeAllTargetMCs();
668 llvm::InitializeAllDisassemblers();
670 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
674 return executeInput();
675 case AC_PrintDebugLineInfo:
676 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */ true);
677 case AC_PrintLineInfo:
678 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */false);
679 case AC_PrintObjectLineInfo:
680 return printLineInfoForInput(/* LoadObjects */false,/* UseDebugObj */false);
682 return linkAndVerify();