1 //===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This is a testing tool for use with the MC-JIT LLVM components. 10 // 11 //===----------------------------------------------------------------------===// 12 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" 37 38 #include <future> 39 #include <list> 40 41 using namespace llvm; 42 using namespace llvm::object; 43 44 static cl::list<std::string> 45 InputFileList(cl::Positional, cl::ZeroOrMore, 46 cl::desc("<input files>")); 47 48 enum ActionType { 49 AC_Execute, 50 AC_PrintObjectLineInfo, 51 AC_PrintLineInfo, 52 AC_PrintDebugLineInfo, 53 AC_Verify 54 }; 55 56 static cl::opt<ActionType> 57 Action(cl::desc("Action to perform:"), 58 cl::init(AC_Execute), 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."))); 69 70 static cl::opt<std::string> 71 EntryPoint("entry", 72 cl::desc("Function to call as entry point."), 73 cl::init("_main")); 74 75 static cl::list<std::string> 76 Dylibs("dylib", 77 cl::desc("Add library."), 78 cl::ZeroOrMore); 79 80 static cl::list<std::string> InputArgv("args", cl::Positional, 81 cl::desc("<program arguments>..."), 82 cl::ZeroOrMore, cl::PositionalEatsArgs); 83 84 static cl::opt<std::string> 85 TripleName("triple", cl::desc("Target triple for disassembler")); 86 87 static cl::opt<std::string> 88 MCPU("mcpu", 89 cl::desc("Target a specific cpu type (-mcpu=help for details)"), 90 cl::value_desc("cpu-name"), 91 cl::init("")); 92 93 static cl::list<std::string> 94 CheckFiles("check", 95 cl::desc("File containing RuntimeDyld verifier checks."), 96 cl::ZeroOrMore); 97 98 static cl::opt<uint64_t> 99 PreallocMemory("preallocate", 100 cl::desc("Allocate memory upfront rather than on-demand"), 101 cl::init(0)); 102 103 static cl::opt<uint64_t> TargetAddrStart( 104 "target-addr-start", 105 cl::desc("For -verify only: start of phony target address " 106 "range."), 107 cl::init(4096), // Start at "page 1" - no allocating at "null". 108 cl::Hidden); 109 110 static cl::opt<uint64_t> TargetAddrEnd( 111 "target-addr-end", 112 cl::desc("For -verify only: end of phony target address range."), 113 cl::init(~0ULL), cl::Hidden); 114 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); 120 121 static cl::list<std::string> 122 SpecificSectionMappings("map-section", 123 cl::desc("For -verify only: Map a section to a " 124 "specific address."), 125 cl::ZeroOrMore, 126 cl::Hidden); 127 128 static cl::list<std::string> 129 DummySymbolMappings("dummy-extern", 130 cl::desc("For -verify only: Inject a symbol into the extern " 131 "symbol table."), 132 cl::ZeroOrMore, 133 cl::Hidden); 134 135 static cl::opt<bool> 136 PrintAllocationRequests("print-alloc-requests", 137 cl::desc("Print allocation requests made to the memory " 138 "manager by RuntimeDyld"), 139 cl::Hidden); 140 141 ExitOnError ExitOnErr; 142 143 /* *** */ 144 145 using SectionIDMap = StringMap<unsigned>; 146 using FileToSectionIDMap = StringMap<SectionIDMap>; 147 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"; 153 } 154 } 155 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()); 168 return J->second; 169 } 170 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 { 174 public: 175 struct SectionInfo { 176 SectionInfo(StringRef Name, sys::MemoryBlock MB, unsigned SectionID) 177 : Name(Name), MB(std::move(MB)), SectionID(SectionID) {} 178 std::string Name; 179 sys::MemoryBlock MB; 180 unsigned SectionID = ~0U; 181 }; 182 183 SmallVector<SectionInfo, 16> FunctionMemory; 184 SmallVector<SectionInfo, 16> DataMemory; 185 186 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, 187 unsigned SectionID, 188 StringRef SectionName) override; 189 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, 190 unsigned SectionID, StringRef SectionName, 191 bool IsReadOnly) override; 192 193 /// If non null, records subsequent Name -> SectionID mappings. 194 void setSectionIDsMap(SectionIDMap *SecIDMap) { 195 this->SecIDMap = SecIDMap; 196 } 197 198 void *getPointerToNamedFunction(const std::string &Name, 199 bool AbortOnFailure = true) override { 200 return nullptr; 201 } 202 203 bool finalizeMemory(std::string *ErrMsg) override { return false; } 204 205 void addDummySymbol(const std::string &Name, uint64_t Addr) { 206 DummyExterns[Name] = Addr; 207 } 208 209 JITSymbol findSymbol(const std::string &Name) override { 210 auto I = DummyExterns.find(Name); 211 212 if (I != DummyExterns.end()) 213 return JITSymbol(I->second, JITSymbolFlags::Exported); 214 215 if (auto Sym = RTDyldMemoryManager::findSymbol(Name)) 216 return Sym; 217 else if (auto Err = Sym.takeError()) 218 ExitOnErr(std::move(Err)); 219 else 220 ExitOnErr(make_error<StringError>("Could not find definition for \"" + 221 Name + "\"", 222 inconvertibleErrorCode())); 223 llvm_unreachable("Should have returned or exited by now"); 224 } 225 226 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, 227 size_t Size) override {} 228 void deregisterEHFrames() override {} 229 230 void preallocateSlab(uint64_t Size) { 231 std::error_code EC; 232 sys::MemoryBlock MB = 233 sys::Memory::allocateMappedMemory(Size, nullptr, 234 sys::Memory::MF_READ | 235 sys::Memory::MF_WRITE, 236 EC); 237 if (!MB.base()) 238 report_fatal_error("Can't allocate enough memory: " + EC.message()); 239 240 PreallocSlab = MB; 241 UsePreallocation = true; 242 SlabSize = Size; 243 } 244 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"); 250 251 uintptr_t OldSlabOffset = CurrentSlabOffset; 252 sys::MemoryBlock MB((void *)OldSlabOffset, Size); 253 if (isCode) 254 FunctionMemory.push_back(SectionInfo(SectionName, MB, SectionID)); 255 else 256 DataMemory.push_back(SectionInfo(SectionName, MB, SectionID)); 257 CurrentSlabOffset += Size; 258 return (uint8_t*)OldSlabOffset; 259 } 260 261 private: 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; 268 }; 269 270 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size, 271 unsigned Alignment, 272 unsigned SectionID, 273 StringRef SectionName) { 274 if (PrintAllocationRequests) 275 outs() << "allocateCodeSection(Size = " << Size << ", Alignment = " 276 << Alignment << ", SectionName = " << SectionName << ")\n"; 277 278 if (SecIDMap) 279 (*SecIDMap)[SectionName] = SectionID; 280 281 if (UsePreallocation) 282 return allocateFromSlab(Size, Alignment, true /* isCode */, 283 SectionName, SectionID); 284 285 std::error_code EC; 286 sys::MemoryBlock MB = 287 sys::Memory::allocateMappedMemory(Size, nullptr, 288 sys::Memory::MF_READ | 289 sys::Memory::MF_WRITE, 290 EC); 291 if (!MB.base()) 292 report_fatal_error("MemoryManager allocation failed: " + EC.message()); 293 FunctionMemory.push_back(SectionInfo(SectionName, MB, SectionID)); 294 return (uint8_t*)MB.base(); 295 } 296 297 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size, 298 unsigned Alignment, 299 unsigned SectionID, 300 StringRef SectionName, 301 bool IsReadOnly) { 302 if (PrintAllocationRequests) 303 outs() << "allocateDataSection(Size = " << Size << ", Alignment = " 304 << Alignment << ", SectionName = " << SectionName << ")\n"; 305 306 if (SecIDMap) 307 (*SecIDMap)[SectionName] = SectionID; 308 309 if (UsePreallocation) 310 return allocateFromSlab(Size, Alignment, false /* isCode */, SectionName, 311 SectionID); 312 313 std::error_code EC; 314 sys::MemoryBlock MB = 315 sys::Memory::allocateMappedMemory(Size, nullptr, 316 sys::Memory::MF_READ | 317 sys::Memory::MF_WRITE, 318 EC); 319 if (!MB.base()) 320 report_fatal_error("MemoryManager allocation failed: " + EC.message()); 321 DataMemory.push_back(SectionInfo(SectionName, MB, SectionID)); 322 return (uint8_t*)MB.base(); 323 } 324 325 static const char *ProgramName; 326 327 static void ErrorAndExit(const Twine &Msg) { 328 errs() << ProgramName << ": error: " << Msg << "\n"; 329 exit(1); 330 } 331 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 + "'."); 336 std::string ErrMsg; 337 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg)) 338 report_fatal_error("Error loading '" + Dylib + "': " + ErrMsg); 339 } 340 } 341 342 /* *** */ 343 344 static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) { 345 assert(LoadObjects || !UseDebugObj); 346 347 // Load any dylibs requested on the command line. 348 loadDylibs(); 349 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); 357 358 // Load the input memory buffer. 359 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() + "'"); 364 365 Expected<std::unique_ptr<ObjectFile>> MaybeObj( 366 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); 367 368 if (!MaybeObj) { 369 std::string Buf; 370 raw_string_ostream OS(Buf); 371 logAllUnhandledErrors(MaybeObj.takeError(), OS); 372 OS.flush(); 373 ErrorAndExit("unable to create object file: '" + Buf + "'"); 374 } 375 376 ObjectFile &Obj = **MaybeObj; 377 378 OwningBinary<ObjectFile> DebugObj; 379 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = nullptr; 380 ObjectFile *SymbolObj = &Obj; 381 if (LoadObjects) { 382 // Load the object file 383 LoadedObjInfo = 384 Dyld.loadObject(Obj); 385 386 if (Dyld.hasError()) 387 ErrorAndExit(Dyld.getErrorString()); 388 389 // Resolve all the relocations we can. 390 Dyld.resolveRelocations(); 391 392 if (UseDebugObj) { 393 DebugObj = LoadedObjInfo->getObjectForDebug(Obj); 394 SymbolObj = DebugObj.getBinary(); 395 LoadedObjInfo.reset(); 396 } 397 } 398 399 std::unique_ptr<DIContext> Context = 400 DWARFContext::create(*SymbolObj, LoadedObjInfo.get()); 401 402 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr = 403 object::computeSymbolSizes(*SymbolObj); 404 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(); 409 if (!TypeOrErr) { 410 // TODO: Actually report errors helpfully. 411 consumeError(TypeOrErr.takeError()); 412 continue; 413 } 414 SymbolRef::Type Type = *TypeOrErr; 415 if (Type == object::SymbolRef::ST_Function) { 416 Expected<StringRef> Name = Sym.getName(); 417 if (!Name) { 418 // TODO: Actually report errors helpfully. 419 consumeError(Name.takeError()); 420 continue; 421 } 422 Expected<uint64_t> AddrOrErr = Sym.getAddress(); 423 if (!AddrOrErr) { 424 // TODO: Actually report errors helpfully. 425 consumeError(AddrOrErr.takeError()); 426 continue; 427 } 428 uint64_t Addr = *AddrOrErr; 429 430 object::SectionedAddress Address; 431 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(); 438 if (!SecOrErr) { 439 // TODO: Actually report errors helpfully. 440 consumeError(SecOrErr.takeError()); 441 continue; 442 } 443 object::section_iterator Sec = *SecOrErr; 444 StringRef SecName; 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(); 453 454 outs() << "Function: " << *Name << ", Size = " << Size 455 << ", Addr = " << Addr << "\n"; 456 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"; 463 } 464 } 465 } 466 } 467 468 return 0; 469 } 470 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."); 476 477 // FIXME: Limit the amount of memory that can be preallocated? 478 if (PreallocMemory != 0) 479 MemMgr.preallocateSlab(PreallocMemory); 480 } 481 482 static int executeInput() { 483 // Load any dylibs requested on the command line. 484 loadDylibs(); 485 486 // Instantiate a dynamic linker. 487 TrivialMemoryManager MemMgr; 488 doPreallocation(MemMgr); 489 RuntimeDyld Dyld(MemMgr, MemMgr); 490 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())); 502 503 if (!MaybeObj) { 504 std::string Buf; 505 raw_string_ostream OS(Buf); 506 logAllUnhandledErrors(MaybeObj.takeError(), OS); 507 OS.flush(); 508 ErrorAndExit("unable to create object file: '" + Buf + "'"); 509 } 510 511 ObjectFile &Obj = **MaybeObj; 512 513 // Load the object file 514 Dyld.loadObject(Obj); 515 if (Dyld.hasError()) { 516 ErrorAndExit(Dyld.getErrorString()); 517 } 518 } 519 520 // Resove all the relocations we can. 521 // FIXME: Error out if there are unresolved relocations. 522 Dyld.resolveRelocations(); 523 524 // Get the address of the entry point (_main by default). 525 void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint); 526 if (!MainAddress) 527 ErrorAndExit("no definition for '" + EntryPoint + "'"); 528 529 // Invalidate the instruction cache for each loaded function. 530 for (auto &FM : MemMgr.FunctionMemory) { 531 532 auto &FM_MB = FM.MB; 533 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() + 540 "'"); 541 } 542 543 // Dispatch to _main(). 544 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n"; 545 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()); 555 } 556 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 + "': " + 563 EC.message()); 564 565 if (!Checker.checkAllRulesInBuffer("# rtdyld-check:", 566 CheckerFileBuf.get().get())) 567 ErrorAndExit("some checks in '" + CheckerFileName + "' failed"); 568 } 569 return 0; 570 } 571 572 void applySpecificSectionMappings(RuntimeDyld &Dyld, 573 const FileToSectionIDMap &FileToSecIDMap) { 574 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(","); 579 580 if (ComaIdx == StringRef::npos) 581 report_fatal_error("Invalid section specification '" + Mapping + 582 "'. Should be '<file name>,<section name>=<addr>'"); 583 584 std::string FileName = SectionIDStr.substr(0, ComaIdx); 585 std::string SectionName = SectionIDStr.substr(ComaIdx + 1); 586 unsigned SectionID = 587 ExitOnErr(getSectionId(FileToSecIDMap, FileName, SectionName)); 588 589 auto* OldAddr = Dyld.getSectionContent(SectionID).data(); 590 std::string NewAddrStr = Mapping.substr(EqualsIdx + 1); 591 uint64_t NewAddr; 592 593 if (StringRef(NewAddrStr).getAsInteger(0, NewAddr)) 594 report_fatal_error("Invalid section address in mapping '" + Mapping + 595 "'."); 596 597 Dyld.mapSectionAddress(OldAddr, NewAddr); 598 } 599 } 600 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 604 // address space: 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. 611 // 612 static void remapSectionsAndSymbols(const llvm::Triple &TargetTriple, 613 RuntimeDyld &Dyld, 614 TrivialMemoryManager &MemMgr) { 615 616 // Set up a work list (section addr/size pairs). 617 typedef std::list<const TrivialMemoryManager::SectionInfo*> WorklistT; 618 WorklistT Worklist; 619 620 for (const auto& CodeSection : MemMgr.FunctionMemory) 621 Worklist.push_back(&CodeSection); 622 for (const auto& DataSection : MemMgr.DataMemory) 623 Worklist.push_back(&DataSection); 624 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; 630 631 // Move the previously applied mappings (whether explicitly specified on the 632 // command line, or implicitly set by RuntimeDyld) into the already-allocated 633 // map. 634 for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end(); 635 I != E;) { 636 WorklistT::iterator Tmp = I; 637 ++I; 638 639 auto LoadAddr = Dyld.getSectionLoadAddress((*Tmp)->SectionID); 640 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. 646 if (LoadAddr != 0) 647 AlreadyAllocated[LoadAddr] = (*Tmp)->MB.allocatedSize(); 648 Worklist.erase(Tmp); 649 } 650 } 651 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. 661 } 662 663 // Process any elements remaining in the worklist. 664 while (!Worklist.empty()) { 665 auto *CurEntry = Worklist.front(); 666 Worklist.pop_front(); 667 668 uint64_t NextSectionAddr = TargetAddrStart; 669 670 for (const auto &Alloc : AlreadyAllocated) 671 if (NextSectionAddr + CurEntry->MB.allocatedSize() + TargetSectionSep <= 672 Alloc.first) 673 break; 674 else 675 NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep; 676 677 Dyld.mapSectionAddress(CurEntry->MB.base(), NextSectionAddr); 678 AlreadyAllocated[NextSectionAddr] = CurEntry->MB.allocatedSize(); 679 } 680 681 // Add dummy symbols to the memory manager. 682 for (const auto &Mapping : DummySymbolMappings) { 683 size_t EqualsIdx = Mapping.find_first_of('='); 684 685 if (EqualsIdx == StringRef::npos) 686 report_fatal_error("Invalid dummy symbol specification '" + Mapping + 687 "'. Should be '<symbol name>=<addr>'"); 688 689 std::string Symbol = Mapping.substr(0, EqualsIdx); 690 std::string AddrStr = Mapping.substr(EqualsIdx + 1); 691 692 uint64_t Addr; 693 if (StringRef(AddrStr).getAsInteger(0, Addr)) 694 report_fatal_error("Invalid symbol mapping '" + Mapping + "'."); 695 696 MemMgr.addDummySymbol(Symbol, Addr); 697 } 698 } 699 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() { 704 705 // Check for missing triple. 706 if (TripleName == "") 707 ErrorAndExit("-triple required when running in -verify mode."); 708 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); 714 if (!TheTarget) 715 ErrorAndExit("Error accessing target '" + TripleName + "': " + ErrorStr); 716 717 TripleName = TheTriple.getTriple(); 718 719 std::unique_ptr<MCSubtargetInfo> STI( 720 TheTarget->createMCSubtargetInfo(TripleName, MCPU, "")); 721 if (!STI) 722 ErrorAndExit("Unable to create subtarget info!"); 723 724 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); 725 if (!MRI) 726 ErrorAndExit("Unable to create target register info!"); 727 728 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); 729 if (!MAI) 730 ErrorAndExit("Unable to create target asm info!"); 731 732 MCContext Ctx(MAI.get(), MRI.get(), nullptr); 733 734 std::unique_ptr<MCDisassembler> Disassembler( 735 TheTarget->createMCDisassembler(*STI, Ctx)); 736 if (!Disassembler) 737 ErrorAndExit("Unable to create disassembler!"); 738 739 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); 740 741 std::unique_ptr<MCInstPrinter> InstPrinter( 742 TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI)); 743 744 // Load any dylibs requested on the command line. 745 loadDylibs(); 746 747 // Instantiate a dynamic linker. 748 TrivialMemoryManager MemMgr; 749 doPreallocation(MemMgr); 750 751 struct StubID { 752 unsigned SectionID; 753 uint32_t Offset; 754 }; 755 using StubInfos = StringMap<StubID>; 756 using StubContainers = StringMap<StubInfos>; 757 758 StubContainers StubMap; 759 RuntimeDyld Dyld(MemMgr, MemMgr); 760 Dyld.setProcessAllSections(true); 761 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}; 769 }); 770 771 auto GetSymbolInfo = 772 [&Dyld, &MemMgr]( 773 StringRef Symbol) -> Expected<RuntimeDyldChecker::MemoryRegionInfo> { 774 RuntimeDyldChecker::MemoryRegionInfo SymInfo; 775 776 // First get the target address. 777 if (auto InternalSymbol = Dyld.getSymbol(Symbol)) 778 SymInfo.setTargetAddress(InternalSymbol.getAddress()); 779 else { 780 // Symbol not found in RuntimeDyld. Fall back to external lookup. 781 #ifdef _MSC_VER 782 using ExpectedLookupResult = 783 MSVCPExpected<JITSymbolResolver::LookupResult>; 784 #else 785 using ExpectedLookupResult = Expected<JITSymbolResolver::LookupResult>; 786 #endif 787 788 auto ResultP = std::make_shared<std::promise<ExpectedLookupResult>>(); 789 auto ResultF = ResultP->get_future(); 790 791 MemMgr.lookup(JITSymbolResolver::LookupSet({Symbol}), 792 [=](Expected<JITSymbolResolver::LookupResult> Result) { 793 ResultP->set_value(std::move(Result)); 794 }); 795 796 auto Result = ResultF.get(); 797 if (!Result) 798 return Result.takeError(); 799 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()); 804 } 805 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)); 815 } 816 } 817 return SymInfo; 818 }; 819 820 auto IsSymbolValid = [&Dyld, GetSymbolInfo](StringRef Symbol) { 821 if (Dyld.getSymbol(Symbol)) 822 return true; 823 auto SymInfo = GetSymbolInfo(Symbol); 824 if (!SymInfo) { 825 logAllUnhandledErrors(SymInfo.takeError(), errs(), "RTDyldChecker: "); 826 return false; 827 } 828 return SymInfo->getTargetAddress() != 0; 829 }; 830 831 FileToSectionIDMap FileToSecIDMap; 832 833 auto GetSectionInfo = [&Dyld, &FileToSecIDMap](StringRef FileName, 834 StringRef SectionName) 835 -> Expected<RuntimeDyldChecker::MemoryRegionInfo> { 836 auto SectionID = getSectionId(FileToSecIDMap, FileName, SectionName); 837 if (!SectionID) 838 return SectionID.takeError(); 839 RuntimeDyldChecker::MemoryRegionInfo SecInfo; 840 SecInfo.setTargetAddress(Dyld.getSectionLoadAddress(*SectionID)); 841 SecInfo.setContent(Dyld.getSectionContent(*SectionID)); 842 return SecInfo; 843 }; 844 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: " + 850 StubContainer, 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) + 859 SI.Offset); 860 StubMemInfo.setContent( 861 Dyld.getSectionContent(SI.SectionID).substr(SI.Offset)); 862 return StubMemInfo; 863 }; 864 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; 868 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); 876 877 if (std::error_code EC = InputBuffer.getError()) 878 ErrorAndExit("unable to read input: '" + EC.message() + "'"); 879 880 Expected<std::unique_ptr<ObjectFile>> MaybeObj( 881 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); 882 883 if (!MaybeObj) { 884 std::string Buf; 885 raw_string_ostream OS(Buf); 886 logAllUnhandledErrors(MaybeObj.takeError(), OS); 887 OS.flush(); 888 ErrorAndExit("unable to create object file: '" + Buf + "'"); 889 } 890 891 ObjectFile &Obj = **MaybeObj; 892 893 if (!Checker) 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()); 898 899 auto FileName = sys::path::filename(InputFile); 900 MemMgr.setSectionIDsMap(&FileToSecIDMap[FileName]); 901 902 // Load the object file 903 Dyld.loadObject(Obj); 904 if (Dyld.hasError()) { 905 ErrorAndExit(Dyld.getErrorString()); 906 } 907 } 908 909 // Re-map the section addresses into the phony target address space and add 910 // dummy symbols. 911 applySpecificSectionMappings(Dyld, FileToSecIDMap); 912 remapSectionsAndSymbols(TheTriple, Dyld, MemMgr); 913 914 // Resolve all the relocations we can. 915 Dyld.resolveRelocations(); 916 917 // Register EH frames. 918 Dyld.registerEHFrames(); 919 920 int ErrorCode = checkAllExpressions(*Checker); 921 if (Dyld.hasError()) 922 ErrorAndExit("RTDyld reported an error applying relocations:\n " + 923 Dyld.getErrorString()); 924 925 return ErrorCode; 926 } 927 928 int main(int argc, char **argv) { 929 InitLLVM X(argc, argv); 930 ProgramName = argv[0]; 931 932 llvm::InitializeAllTargetInfos(); 933 llvm::InitializeAllTargetMCs(); 934 llvm::InitializeAllDisassemblers(); 935 936 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); 937 938 ExitOnErr.setBanner(std::string(argv[0]) + ": "); 939 940 switch (Action) { 941 case AC_Execute: 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); 949 case AC_Verify: 950 return linkAndVerify(); 951 } 952 } 953