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();
144 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
147 StringRef SectionName) {
148 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
149 FunctionMemory.push_back(MB);
150 return (uint8_t*)MB.base();
153 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
156 StringRef SectionName,
158 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
159 DataMemory.push_back(MB);
160 return (uint8_t*)MB.base();
163 void TrivialMemoryManager::invalidateInstructionCache() {
164 for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
165 sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
166 FunctionMemory[i].size());
168 for (int i = 0, e = DataMemory.size(); i != e; ++i)
169 sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
170 DataMemory[i].size());
173 static const char *ProgramName;
175 static void Message(const char *Type, const Twine &Msg) {
176 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
179 static int Error(const Twine &Msg) {
180 Message("error", Msg);
184 static void loadDylibs() {
185 for (const std::string &Dylib : Dylibs) {
186 if (sys::fs::is_regular_file(Dylib)) {
188 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
189 llvm::errs() << "Error loading '" << Dylib << "': "
192 llvm::errs() << "Dylib not found: '" << Dylib << "'.\n";
198 static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
199 assert(LoadObjects || !UseDebugObj);
201 // Load any dylibs requested on the command line.
204 // If we don't have any input files, read from stdin.
205 if (!InputFileList.size())
206 InputFileList.push_back("-");
207 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
208 // Instantiate a dynamic linker.
209 TrivialMemoryManager MemMgr;
210 RuntimeDyld Dyld(MemMgr, MemMgr);
212 // Load the input memory buffer.
214 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
215 MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
216 if (std::error_code EC = InputBuffer.getError())
217 return Error("unable to read input: '" + EC.message() + "'");
219 ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
220 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
222 if (std::error_code EC = MaybeObj.getError())
223 return Error("unable to create object file: '" + EC.message() + "'");
225 ObjectFile &Obj = **MaybeObj;
227 OwningBinary<ObjectFile> DebugObj;
228 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = nullptr;
229 ObjectFile *SymbolObj = &Obj;
231 // Load the object file
233 Dyld.loadObject(Obj);
236 return Error(Dyld.getErrorString());
238 // Resolve all the relocations we can.
239 Dyld.resolveRelocations();
242 DebugObj = LoadedObjInfo->getObjectForDebug(Obj);
243 SymbolObj = DebugObj.getBinary();
247 std::unique_ptr<DIContext> Context(
248 new DWARFContextInMemory(*SymbolObj,LoadedObjInfo.get()));
250 // FIXME: This is generally useful. Figure out a place in lib/Object to
251 // put utility functions.
252 std::map<object::SectionRef, std::vector<uint64_t>> FuncAddresses;
253 if (!isa<ELFObjectFileBase>(SymbolObj)) {
254 for (object::SymbolRef Sym : SymbolObj->symbols()) {
255 object::SymbolRef::Type SymType;
256 if (Sym.getType(SymType))
258 if (SymType != object::SymbolRef::ST_Function)
261 if (Sym.getAddress(Addr))
263 object::section_iterator Sec = SymbolObj->section_end();
264 if (Sym.getSection(Sec))
266 std::vector<uint64_t> &Addrs = FuncAddresses[*Sec];
268 uint64_t SecAddr = Sec->getAddress();
269 uint64_t SecSize = Sec->getSize();
270 Addrs.push_back(SecAddr + SecSize);
272 Addrs.push_back(Addr);
274 for (auto &Pair : FuncAddresses) {
275 std::vector<uint64_t> &Addrs = Pair.second;
276 array_pod_sort(Addrs.begin(), Addrs.end());
280 // Use symbol info to iterate functions in the object.
281 for (object::SymbolRef Sym : SymbolObj->symbols()) {
282 object::SymbolRef::Type SymType;
283 if (Sym.getType(SymType))
285 if (SymType == object::SymbolRef::ST_Function) {
288 if (Sym.getName(Name))
290 if (Sym.getAddress(Addr))
294 if (isa<ELFObjectFileBase>(SymbolObj)) {
295 if (Sym.getSize(Size))
298 object::section_iterator Sec = SymbolObj->section_end();
299 if (Sym.getSection(Sec))
301 const std::vector<uint64_t> &Addrs = FuncAddresses[*Sec];
302 auto AddrI = std::find(Addrs.begin(), Addrs.end(), Addr);
303 assert(AddrI != Addrs.end() && (AddrI + 1) != Addrs.end());
304 assert(*AddrI == Addr);
305 Size = *(AddrI + 1) - Addr;
308 // If we're not using the debug object, compute the address of the
309 // symbol in memory (rather than that in the unrelocated object file)
310 // and use that to query the DWARFContext.
311 if (!UseDebugObj && LoadObjects) {
312 object::section_iterator Sec(SymbolObj->section_end());
315 Sec->getName(SecName);
316 uint64_t SectionLoadAddress =
317 LoadedObjInfo->getSectionLoadAddress(SecName);
318 if (SectionLoadAddress != 0)
319 Addr += SectionLoadAddress - Sec->getAddress();
322 outs() << "Function: " << Name << ", Size = " << Size << ", Addr = " << Addr << "\n";
324 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
325 DILineInfoTable::iterator Begin = Lines.begin();
326 DILineInfoTable::iterator End = Lines.end();
327 for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
328 outs() << " Line info @ " << It->first - Addr << ": "
329 << It->second.FileName << ", line:" << It->second.Line << "\n";
338 static int executeInput() {
339 // Load any dylibs requested on the command line.
342 // Instantiate a dynamic linker.
343 TrivialMemoryManager MemMgr;
344 RuntimeDyld Dyld(MemMgr, MemMgr);
346 // FIXME: Preserve buffers until resolveRelocations time to work around a bug
347 // in RuntimeDyldELF.
348 // This fixme should be fixed ASAP. This is a very brittle workaround.
349 std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers;
351 // If we don't have any input files, read from stdin.
352 if (!InputFileList.size())
353 InputFileList.push_back("-");
354 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
355 // Load the input memory buffer.
356 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
357 MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
358 if (std::error_code EC = InputBuffer.getError())
359 return Error("unable to read input: '" + EC.message() + "'");
360 ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
361 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
363 if (std::error_code EC = MaybeObj.getError())
364 return Error("unable to create object file: '" + EC.message() + "'");
366 ObjectFile &Obj = **MaybeObj;
367 InputBuffers.push_back(std::move(*InputBuffer));
369 // Load the object file
370 Dyld.loadObject(Obj);
371 if (Dyld.hasError()) {
372 return Error(Dyld.getErrorString());
376 // Resolve all the relocations we can.
377 Dyld.resolveRelocations();
378 // Clear instruction cache before code will be executed.
379 MemMgr.invalidateInstructionCache();
381 // FIXME: Error out if there are unresolved relocations.
383 // Get the address of the entry point (_main by default).
384 void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint);
386 return Error("no definition for '" + EntryPoint + "'");
388 // Invalidate the instruction cache for each loaded function.
389 for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) {
390 sys::MemoryBlock &Data = MemMgr.FunctionMemory[i];
391 // Make sure the memory is executable.
392 std::string ErrorStr;
393 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
394 if (!sys::Memory::setExecutable(Data, &ErrorStr))
395 return Error("unable to mark function executable: '" + ErrorStr + "'");
398 // Dispatch to _main().
399 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
401 int (*Main)(int, const char**) =
402 (int(*)(int,const char**)) uintptr_t(MainAddress);
403 const char **Argv = new const char*[2];
404 // Use the name of the first input object module as argv[0] for the target.
405 Argv[0] = InputFileList[0].c_str();
407 return Main(1, Argv);
410 static int checkAllExpressions(RuntimeDyldChecker &Checker) {
411 for (const auto& CheckerFileName : CheckFiles) {
412 ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
413 MemoryBuffer::getFileOrSTDIN(CheckerFileName);
414 if (std::error_code EC = CheckerFileBuf.getError())
415 return Error("unable to read input '" + CheckerFileName + "': " +
418 if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
419 CheckerFileBuf.get().get()))
420 return Error("some checks in '" + CheckerFileName + "' failed");
425 static std::map<void *, uint64_t>
426 applySpecificSectionMappings(RuntimeDyldChecker &Checker) {
428 std::map<void*, uint64_t> SpecificMappings;
430 for (StringRef Mapping : SpecificSectionMappings) {
432 size_t EqualsIdx = Mapping.find_first_of("=");
433 StringRef SectionIDStr = Mapping.substr(0, EqualsIdx);
434 size_t ComaIdx = Mapping.find_first_of(",");
436 if (ComaIdx == StringRef::npos) {
437 errs() << "Invalid section specification '" << Mapping
438 << "'. Should be '<file name>,<section name>=<addr>'\n";
442 StringRef FileName = SectionIDStr.substr(0, ComaIdx);
443 StringRef SectionName = SectionIDStr.substr(ComaIdx + 1);
446 std::string ErrorMsg;
447 std::tie(OldAddrInt, ErrorMsg) =
448 Checker.getSectionAddr(FileName, SectionName, true);
450 if (ErrorMsg != "") {
455 void* OldAddr = reinterpret_cast<void*>(static_cast<uintptr_t>(OldAddrInt));
457 StringRef NewAddrStr = Mapping.substr(EqualsIdx + 1);
460 if (NewAddrStr.getAsInteger(0, NewAddr)) {
461 errs() << "Invalid section address in mapping: " << Mapping << "\n";
465 Checker.getRTDyld().mapSectionAddress(OldAddr, NewAddr);
466 SpecificMappings[OldAddr] = NewAddr;
469 return SpecificMappings;
472 // Scatter sections in all directions!
473 // Remaps section addresses for -verify mode. The following command line options
474 // can be used to customize the layout of the memory within the phony target's
476 // -target-addr-start <s> -- Specify where the phony target addres range starts.
477 // -target-addr-end <e> -- Specify where the phony target address range ends.
478 // -target-section-sep <d> -- Specify how big a gap should be left between the
479 // end of one section and the start of the next.
480 // Defaults to zero. Set to something big
481 // (e.g. 1 << 32) to stress-test stubs, GOTs, etc.
483 static void remapSections(const llvm::Triple &TargetTriple,
484 const TrivialMemoryManager &MemMgr,
485 RuntimeDyldChecker &Checker) {
487 // Set up a work list (section addr/size pairs).
488 typedef std::list<std::pair<void*, uint64_t>> WorklistT;
491 for (const auto& CodeSection : MemMgr.FunctionMemory)
492 Worklist.push_back(std::make_pair(CodeSection.base(), CodeSection.size()));
493 for (const auto& DataSection : MemMgr.DataMemory)
494 Worklist.push_back(std::make_pair(DataSection.base(), DataSection.size()));
496 // Apply any section-specific mappings that were requested on the command
498 typedef std::map<void*, uint64_t> AppliedMappingsT;
499 AppliedMappingsT AppliedMappings = applySpecificSectionMappings(Checker);
501 // Keep an "already allocated" mapping of section target addresses to sizes.
502 // Sections whose address mappings aren't specified on the command line will
503 // allocated around the explicitly mapped sections while maintaining the
504 // minimum separation.
505 std::map<uint64_t, uint64_t> AlreadyAllocated;
507 // Move the previously applied mappings into the already-allocated map.
508 for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end();
510 WorklistT::iterator Tmp = I;
512 AppliedMappingsT::iterator AI = AppliedMappings.find(Tmp->first);
514 if (AI != AppliedMappings.end()) {
515 AlreadyAllocated[AI->second] = Tmp->second;
520 // If the -target-addr-end option wasn't explicitly passed, then set it to a
521 // sensible default based on the target triple.
522 if (TargetAddrEnd.getNumOccurrences() == 0) {
523 if (TargetTriple.isArch16Bit())
524 TargetAddrEnd = (1ULL << 16) - 1;
525 else if (TargetTriple.isArch32Bit())
526 TargetAddrEnd = (1ULL << 32) - 1;
527 // TargetAddrEnd already has a sensible default for 64-bit systems, so
528 // there's nothing to do in the 64-bit case.
531 // Process any elements remaining in the worklist.
532 while (!Worklist.empty()) {
533 std::pair<void*, uint64_t> CurEntry = Worklist.front();
534 Worklist.pop_front();
536 uint64_t NextSectionAddr = TargetAddrStart;
538 for (const auto &Alloc : AlreadyAllocated)
539 if (NextSectionAddr + CurEntry.second + TargetSectionSep <= Alloc.first)
542 NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep;
544 AlreadyAllocated[NextSectionAddr] = CurEntry.second;
545 Checker.getRTDyld().mapSectionAddress(CurEntry.first, NextSectionAddr);
550 // Load and link the objects specified on the command line, but do not execute
551 // anything. Instead, attach a RuntimeDyldChecker instance and call it to
552 // verify the correctness of the linked memory.
553 static int linkAndVerify() {
555 // Check for missing triple.
556 if (TripleName == "") {
557 llvm::errs() << "Error: -triple required when running in -verify mode.\n";
561 // Look up the target and build the disassembler.
562 Triple TheTriple(Triple::normalize(TripleName));
563 std::string ErrorStr;
564 const Target *TheTarget =
565 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
567 llvm::errs() << "Error accessing target '" << TripleName << "': "
571 TripleName = TheTriple.getTriple();
573 std::unique_ptr<MCSubtargetInfo> STI(
574 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
575 assert(STI && "Unable to create subtarget info!");
577 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
578 assert(MRI && "Unable to create target register info!");
580 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
581 assert(MAI && "Unable to create target asm info!");
583 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
585 std::unique_ptr<MCDisassembler> Disassembler(
586 TheTarget->createMCDisassembler(*STI, Ctx));
587 assert(Disassembler && "Unable to create disassembler!");
589 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
591 std::unique_ptr<MCInstPrinter> InstPrinter(
592 TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
594 // Load any dylibs requested on the command line.
597 // Instantiate a dynamic linker.
598 TrivialMemoryManager MemMgr;
599 RuntimeDyld Dyld(MemMgr, MemMgr);
600 Dyld.setProcessAllSections(true);
601 RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),
604 // FIXME: Preserve buffers until resolveRelocations time to work around a bug
605 // in RuntimeDyldELF.
606 // This fixme should be fixed ASAP. This is a very brittle workaround.
607 std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers;
609 // If we don't have any input files, read from stdin.
610 if (!InputFileList.size())
611 InputFileList.push_back("-");
612 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
613 // Load the input memory buffer.
614 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
615 MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
617 if (std::error_code EC = InputBuffer.getError())
618 return Error("unable to read input: '" + EC.message() + "'");
620 ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
621 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
623 if (std::error_code EC = MaybeObj.getError())
624 return Error("unable to create object file: '" + EC.message() + "'");
626 ObjectFile &Obj = **MaybeObj;
627 InputBuffers.push_back(std::move(*InputBuffer));
629 // Load the object file
630 Dyld.loadObject(Obj);
631 if (Dyld.hasError()) {
632 return Error(Dyld.getErrorString());
636 // Re-map the section addresses into the phony target address space.
637 remapSections(TheTriple, MemMgr, Checker);
639 // Resolve all the relocations we can.
640 Dyld.resolveRelocations();
642 // Register EH frames.
643 Dyld.registerEHFrames();
645 int ErrorCode = checkAllExpressions(Checker);
646 if (Dyld.hasError()) {
647 errs() << "RTDyld reported an error applying relocations:\n "
648 << Dyld.getErrorString() << "\n";
655 int main(int argc, char **argv) {
656 sys::PrintStackTraceOnErrorSignal();
657 PrettyStackTraceProgram X(argc, argv);
659 ProgramName = argv[0];
660 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
662 llvm::InitializeAllTargetInfos();
663 llvm::InitializeAllTargetMCs();
664 llvm::InitializeAllDisassemblers();
666 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
670 return executeInput();
671 case AC_PrintDebugLineInfo:
672 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */ true);
673 case AC_PrintLineInfo:
674 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */false);
675 case AC_PrintObjectLineInfo:
676 return printLineInfoForInput(/* LoadObjects */false,/* UseDebugObj */false);
678 return linkAndVerify();