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/MC/MCTargetOptions.h"
27#include "llvm/MC/TargetRegistry.h"
28#include "llvm/Object/SymbolSize.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/DynamicLibrary.h"
31#include "llvm/Support/FileSystem.h"
32#include "llvm/Support/InitLLVM.h"
33#include "llvm/Support/MSVCErrorWorkarounds.h"
34#include "llvm/Support/Memory.h"
35#include "llvm/Support/MemoryBuffer.h"
36#include "llvm/Support/Path.h"
37#include "llvm/Support/TargetSelect.h"
38#include "llvm/Support/Timer.h"
39#include "llvm/Support/raw_ostream.h"
40
41#include <future>
42#include <list>
43
44using namespace llvm;
45using namespace llvm::object;
46
47static cl::OptionCategory RTDyldCategory("RTDyld Options");
48
49static cl::list<std::string> InputFileList(cl::Positional,
50 cl::desc("<input files>"),
51 cl::cat(RTDyldCategory));
52
53enum ActionType {
54 AC_Execute,
55 AC_PrintObjectLineInfo,
56 AC_PrintLineInfo,
57 AC_PrintDebugLineInfo,
58 AC_Verify
59};
60
61static cl::opt<ActionType> Action(
62 cl::desc("Action to perform:"), cl::init(Val: AC_Execute),
63 cl::values(
64 clEnumValN(AC_Execute, "execute",
65 "Load, link, and execute the inputs."),
66 clEnumValN(AC_PrintLineInfo, "printline",
67 "Load, link, and print line information for each function."),
68 clEnumValN(AC_PrintDebugLineInfo, "printdebugline",
69 "Load, link, and print line information for each function "
70 "using the debug object"),
71 clEnumValN(AC_PrintObjectLineInfo, "printobjline",
72 "Like -printlineinfo but does not load the object first"),
73 clEnumValN(AC_Verify, "verify",
74 "Load, link and verify the resulting memory image.")),
75 cl::cat(RTDyldCategory));
76
77static cl::opt<std::string>
78 EntryPoint("entry", cl::desc("Function to call as entry point."),
79 cl::init(Val: "_main"), cl::cat(RTDyldCategory));
80
81static cl::list<std::string> Dylibs("dylib", cl::desc("Add library."),
82 cl::cat(RTDyldCategory));
83
84static cl::list<std::string> InputArgv("args", cl::Positional,
85 cl::desc("<program arguments>..."),
86 cl::PositionalEatsArgs,
87 cl::cat(RTDyldCategory));
88
89static cl::opt<std::string>
90 TripleName("triple", cl::desc("Target triple for disassembler"),
91 cl::cat(RTDyldCategory));
92
93static cl::opt<std::string>
94 MCPU("mcpu",
95 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
96 cl::value_desc("cpu-name"), cl::init(Val: ""), cl::cat(RTDyldCategory));
97
98static cl::list<std::string>
99 CheckFiles("check",
100 cl::desc("File containing RuntimeDyld verifier checks."),
101 cl::cat(RTDyldCategory));
102
103static cl::opt<uint64_t>
104 PreallocMemory("preallocate",
105 cl::desc("Allocate memory upfront rather than on-demand"),
106 cl::init(Val: 0), cl::cat(RTDyldCategory));
107
108static cl::opt<uint64_t> TargetAddrStart(
109 "target-addr-start",
110 cl::desc("For -verify only: start of phony target address "
111 "range."),
112 cl::init(Val: 4096), // Start at "page 1" - no allocating at "null".
113 cl::Hidden, cl::cat(RTDyldCategory));
114
115static cl::opt<uint64_t> TargetAddrEnd(
116 "target-addr-end",
117 cl::desc("For -verify only: end of phony target address range."),
118 cl::init(Val: ~0ULL), cl::Hidden, cl::cat(RTDyldCategory));
119
120static cl::opt<uint64_t> TargetSectionSep(
121 "target-section-sep",
122 cl::desc("For -verify only: Separation between sections in "
123 "phony target address space."),
124 cl::init(Val: 0), cl::Hidden, cl::cat(RTDyldCategory));
125
126static cl::list<std::string>
127 SpecificSectionMappings("map-section",
128 cl::desc("For -verify only: Map a section to a "
129 "specific address."),
130 cl::Hidden, cl::cat(RTDyldCategory));
131
132static cl::list<std::string> DummySymbolMappings(
133 "dummy-extern",
134 cl::desc("For -verify only: Inject a symbol into the extern "
135 "symbol table."),
136 cl::Hidden, cl::cat(RTDyldCategory));
137
138static cl::opt<bool> PrintAllocationRequests(
139 "print-alloc-requests",
140 cl::desc("Print allocation requests made to the memory "
141 "manager by RuntimeDyld"),
142 cl::Hidden, cl::cat(RTDyldCategory));
143
144static cl::opt<bool> ShowTimes("show-times",
145 cl::desc("Show times for llvm-rtdyld phases"),
146 cl::init(Val: false), cl::cat(RTDyldCategory));
147
148ExitOnError ExitOnErr;
149
150struct RTDyldTimers {
151 TimerGroup RTDyldTG{"llvm-rtdyld timers", "timers for llvm-rtdyld phases"};
152 Timer LoadObjectsTimer{"load", "time to load/add object files", RTDyldTG};
153 Timer LinkTimer{"link", "time to link object files", RTDyldTG};
154 Timer RunTimer{"run", "time to execute jitlink'd code", RTDyldTG};
155};
156
157std::unique_ptr<RTDyldTimers> Timers;
158
159/* *** */
160
161using SectionIDMap = StringMap<unsigned>;
162using FileToSectionIDMap = StringMap<SectionIDMap>;
163
164void dumpFileToSectionIDMap(const FileToSectionIDMap &FileToSecIDMap) {
165 for (const auto &KV : FileToSecIDMap) {
166 llvm::dbgs() << "In " << KV.first() << "\n";
167 for (auto &KV2 : KV.second)
168 llvm::dbgs() << " \"" << KV2.first() << "\" -> " << KV2.second << "\n";
169 }
170}
171
172Expected<unsigned> getSectionId(const FileToSectionIDMap &FileToSecIDMap,
173 StringRef FileName, StringRef SectionName) {
174 auto I = FileToSecIDMap.find(Key: FileName);
175 if (I == FileToSecIDMap.end())
176 return make_error<StringError>(Args: "No file named " + FileName,
177 Args: inconvertibleErrorCode());
178 auto &SectionIDs = I->second;
179 auto J = SectionIDs.find(Key: SectionName);
180 if (J == SectionIDs.end())
181 return make_error<StringError>(Args: "No section named \"" + SectionName +
182 "\" in file " + FileName,
183 Args: inconvertibleErrorCode());
184 return J->second;
185}
186
187// A trivial memory manager that doesn't do anything fancy, just uses the
188// support library allocation routines directly.
189class TrivialMemoryManager : public RTDyldMemoryManager {
190public:
191 struct SectionInfo {
192 SectionInfo(StringRef Name, sys::MemoryBlock MB, unsigned SectionID)
193 : Name(std::string(Name)), MB(std::move(MB)), SectionID(SectionID) {}
194 std::string Name;
195 sys::MemoryBlock MB;
196 unsigned SectionID = ~0U;
197 };
198
199 SmallVector<SectionInfo, 16> FunctionMemory;
200 SmallVector<SectionInfo, 16> DataMemory;
201
202 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
203 unsigned SectionID,
204 StringRef SectionName) override;
205 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
206 unsigned SectionID, StringRef SectionName,
207 bool IsReadOnly) override;
208 TrivialMemoryManager::TLSSection
209 allocateTLSSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
210 StringRef SectionName) override;
211
212 /// If non null, records subsequent Name -> SectionID mappings.
213 void setSectionIDsMap(SectionIDMap *SecIDMap) {
214 this->SecIDMap = SecIDMap;
215 }
216
217 void *getPointerToNamedFunction(const std::string &Name,
218 bool AbortOnFailure = true) override {
219 return nullptr;
220 }
221
222 bool finalizeMemory(std::string *ErrMsg) override { return false; }
223
224 void addDummySymbol(const std::string &Name, uint64_t Addr) {
225 DummyExterns[Name] = Addr;
226 }
227
228 JITSymbol findSymbol(const std::string &Name) override {
229 auto I = DummyExterns.find(x: Name);
230
231 if (I != DummyExterns.end())
232 return JITSymbol(I->second, JITSymbolFlags::Exported);
233
234 if (auto Sym = RTDyldMemoryManager::findSymbol(Name))
235 return Sym;
236 else if (auto Err = Sym.takeError())
237 ExitOnErr(std::move(Err));
238 else
239 ExitOnErr(make_error<StringError>(Args: "Could not find definition for \"" +
240 Name + "\"",
241 Args: inconvertibleErrorCode()));
242 llvm_unreachable("Should have returned or exited by now");
243 }
244
245 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
246 size_t Size) override {}
247 void deregisterEHFrames() override {}
248
249 void preallocateSlab(uint64_t Size) {
250 std::error_code EC;
251 sys::MemoryBlock MB =
252 sys::Memory::allocateMappedMemory(NumBytes: Size, NearBlock: nullptr,
253 Flags: sys::Memory::MF_READ |
254 sys::Memory::MF_WRITE,
255 EC);
256 if (!MB.base())
257 report_fatal_error(reason: Twine("Can't allocate enough memory: ") +
258 EC.message());
259
260 PreallocSlab = MB;
261 UsePreallocation = true;
262 SlabSize = Size;
263 }
264
265 uint8_t *allocateFromSlab(uintptr_t Size, unsigned Alignment, bool isCode,
266 StringRef SectionName, unsigned SectionID) {
267 Size = alignTo(Value: Size, Align: Alignment);
268 if (CurrentSlabOffset + Size > SlabSize)
269 report_fatal_error(reason: "Can't allocate enough memory. Tune --preallocate");
270
271 uintptr_t OldSlabOffset = CurrentSlabOffset;
272 sys::MemoryBlock MB((void *)OldSlabOffset, Size);
273 if (isCode)
274 FunctionMemory.push_back(Elt: SectionInfo(SectionName, MB, SectionID));
275 else
276 DataMemory.push_back(Elt: SectionInfo(SectionName, MB, SectionID));
277 CurrentSlabOffset += Size;
278 return (uint8_t*)OldSlabOffset;
279 }
280
281private:
282 std::map<std::string, uint64_t> DummyExterns;
283 sys::MemoryBlock PreallocSlab;
284 bool UsePreallocation = false;
285 uintptr_t SlabSize = 0;
286 uintptr_t CurrentSlabOffset = 0;
287 SectionIDMap *SecIDMap = nullptr;
288#if defined(__x86_64__) && defined(__ELF__) && defined(__linux__)
289 unsigned UsedTLSStorage = 0;
290#endif
291};
292
293uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
294 unsigned Alignment,
295 unsigned SectionID,
296 StringRef SectionName) {
297 if (PrintAllocationRequests)
298 outs() << "allocateCodeSection(Size = " << Size << ", Alignment = "
299 << Alignment << ", SectionName = " << SectionName << ")\n";
300
301 if (SecIDMap)
302 (*SecIDMap)[SectionName] = SectionID;
303
304 if (UsePreallocation)
305 return allocateFromSlab(Size, Alignment, isCode: true /* isCode */,
306 SectionName, SectionID);
307
308 std::error_code EC;
309 sys::MemoryBlock MB =
310 sys::Memory::allocateMappedMemory(NumBytes: Size, NearBlock: nullptr,
311 Flags: sys::Memory::MF_READ |
312 sys::Memory::MF_WRITE,
313 EC);
314 if (!MB.base())
315 report_fatal_error(reason: Twine("MemoryManager allocation failed: ") +
316 EC.message());
317 FunctionMemory.push_back(Elt: SectionInfo(SectionName, MB, SectionID));
318 return (uint8_t*)MB.base();
319}
320
321uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
322 unsigned Alignment,
323 unsigned SectionID,
324 StringRef SectionName,
325 bool IsReadOnly) {
326 if (PrintAllocationRequests)
327 outs() << "allocateDataSection(Size = " << Size << ", Alignment = "
328 << Alignment << ", SectionName = " << SectionName << ")\n";
329
330 if (SecIDMap)
331 (*SecIDMap)[SectionName] = SectionID;
332
333 if (UsePreallocation)
334 return allocateFromSlab(Size, Alignment, isCode: false /* isCode */, SectionName,
335 SectionID);
336
337 std::error_code EC;
338 sys::MemoryBlock MB =
339 sys::Memory::allocateMappedMemory(NumBytes: Size, NearBlock: nullptr,
340 Flags: sys::Memory::MF_READ |
341 sys::Memory::MF_WRITE,
342 EC);
343 if (!MB.base())
344 report_fatal_error(reason: Twine("MemoryManager allocation failed: ") +
345 EC.message());
346 DataMemory.push_back(Elt: SectionInfo(SectionName, MB, SectionID));
347 return (uint8_t*)MB.base();
348}
349
350// In case the execution needs TLS storage, we define a very small TLS memory
351// area here that will be used in allocateTLSSection().
352#if defined(__x86_64__) && defined(__ELF__) && defined(__linux__)
353extern "C" {
354alignas(16) __attribute__((visibility("hidden"), tls_model("initial-exec"),
355 used)) thread_local char LLVMRTDyldTLSSpace[16];
356}
357#endif
358
359TrivialMemoryManager::TLSSection
360TrivialMemoryManager::allocateTLSSection(uintptr_t Size, unsigned Alignment,
361 unsigned SectionID,
362 StringRef SectionName) {
363#if defined(__x86_64__) && defined(__ELF__) && defined(__linux__)
364 if (Size + UsedTLSStorage > sizeof(LLVMRTDyldTLSSpace)) {
365 return {};
366 }
367
368 // Get the offset of the TLSSpace in the TLS block by using a tpoff
369 // relocation here.
370 int64_t TLSOffset;
371 asm("leaq LLVMRTDyldTLSSpace@tpoff, %0" : "=r"(TLSOffset));
372
373 TLSSection Section;
374 // We use the storage directly as the initialization image. This means that
375 // when a new thread is spawned after this allocation, it will not be
376 // initialized correctly. This means, llvm-rtdyld will only support TLS in a
377 // single thread.
378 Section.InitializationImage =
379 reinterpret_cast<uint8_t *>(LLVMRTDyldTLSSpace + UsedTLSStorage);
380 Section.Offset = TLSOffset + UsedTLSStorage;
381
382 UsedTLSStorage += Size;
383
384 return Section;
385#else
386 return {};
387#endif
388}
389
390static const char *ProgramName;
391
392static void ErrorAndExit(const Twine &Msg) {
393 errs() << ProgramName << ": error: " << Msg << "\n";
394 exit(status: 1);
395}
396
397static void loadDylibs() {
398 for (const std::string &Dylib : Dylibs) {
399 if (!sys::fs::is_regular_file(Path: Dylib))
400 report_fatal_error(reason: Twine("Dylib not found: '") + Dylib + "'.");
401 std::string ErrMsg;
402 if (sys::DynamicLibrary::LoadLibraryPermanently(Filename: Dylib.c_str(), ErrMsg: &ErrMsg))
403 report_fatal_error(reason: Twine("Error loading '") + Dylib + "': " + ErrMsg);
404 }
405}
406
407/* *** */
408
409static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
410 assert(LoadObjects || !UseDebugObj);
411
412 // Load any dylibs requested on the command line.
413 loadDylibs();
414
415 // If we don't have any input files, read from stdin.
416 if (!InputFileList.size())
417 InputFileList.push_back(value: "-");
418 for (auto &File : InputFileList) {
419 // Instantiate a dynamic linker.
420 TrivialMemoryManager MemMgr;
421 RuntimeDyld Dyld(MemMgr, MemMgr);
422
423 // Load the input memory buffer.
424
425 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
426 MemoryBuffer::getFileOrSTDIN(Filename: File);
427 if (std::error_code EC = InputBuffer.getError())
428 ErrorAndExit(Msg: "unable to read input: '" + EC.message() + "'");
429
430 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
431 ObjectFile::createObjectFile(Object: (*InputBuffer)->getMemBufferRef()));
432
433 if (!MaybeObj) {
434 std::string Buf;
435 raw_string_ostream OS(Buf);
436 logAllUnhandledErrors(E: MaybeObj.takeError(), OS);
437 ErrorAndExit(Msg: "unable to create object file: '" + Buf + "'");
438 }
439
440 ObjectFile &Obj = **MaybeObj;
441
442 OwningBinary<ObjectFile> DebugObj;
443 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = nullptr;
444 ObjectFile *SymbolObj = &Obj;
445 if (LoadObjects) {
446 // Load the object file
447 LoadedObjInfo =
448 Dyld.loadObject(O: Obj);
449
450 if (Dyld.hasError())
451 ErrorAndExit(Msg: Dyld.getErrorString());
452
453 // Resolve all the relocations we can.
454 Dyld.resolveRelocations();
455
456 if (UseDebugObj) {
457 DebugObj = LoadedObjInfo->getObjectForDebug(Obj);
458 SymbolObj = DebugObj.getBinary();
459 LoadedObjInfo.reset();
460 }
461 }
462
463 std::unique_ptr<DIContext> Context = DWARFContext::create(
464 Obj: *SymbolObj, RelocAction: DWARFContext::ProcessDebugRelocations::Process,
465 L: LoadedObjInfo.get());
466
467 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
468 object::computeSymbolSizes(O: *SymbolObj);
469
470 // Use symbol info to iterate functions in the object.
471 for (const auto &P : SymAddr) {
472 object::SymbolRef Sym = P.first;
473 Expected<SymbolRef::Type> TypeOrErr = Sym.getType();
474 if (!TypeOrErr) {
475 // TODO: Actually report errors helpfully.
476 consumeError(Err: TypeOrErr.takeError());
477 continue;
478 }
479 SymbolRef::Type Type = *TypeOrErr;
480 if (Type == object::SymbolRef::ST_Function) {
481 Expected<StringRef> Name = Sym.getName();
482 if (!Name) {
483 // TODO: Actually report errors helpfully.
484 consumeError(Err: Name.takeError());
485 continue;
486 }
487 Expected<uint64_t> AddrOrErr = Sym.getAddress();
488 if (!AddrOrErr) {
489 // TODO: Actually report errors helpfully.
490 consumeError(Err: AddrOrErr.takeError());
491 continue;
492 }
493 uint64_t Addr = *AddrOrErr;
494
495 object::SectionedAddress Address;
496
497 uint64_t Size = P.second;
498 // If we're not using the debug object, compute the address of the
499 // symbol in memory (rather than that in the unrelocated object file)
500 // and use that to query the DWARFContext.
501 if (!UseDebugObj && LoadObjects) {
502 auto SecOrErr = Sym.getSection();
503 if (!SecOrErr) {
504 // TODO: Actually report errors helpfully.
505 consumeError(Err: SecOrErr.takeError());
506 continue;
507 }
508 object::section_iterator Sec = *SecOrErr;
509 Address.SectionIndex = Sec->getIndex();
510 uint64_t SectionLoadAddress =
511 LoadedObjInfo->getSectionLoadAddress(Sec: *Sec);
512 if (SectionLoadAddress != 0)
513 Addr += SectionLoadAddress - Sec->getAddress();
514 } else if (auto SecOrErr = Sym.getSection())
515 Address.SectionIndex = SecOrErr.get()->getIndex();
516
517 outs() << "Function: " << *Name << ", Size = " << Size
518 << ", Addr = " << Addr << "\n";
519
520 Address.Address = Addr;
521 DILineInfoTable Lines =
522 Context->getLineInfoForAddressRange(Address, Size);
523 for (auto &D : Lines) {
524 outs() << " Line info @ " << D.first - Addr << ": "
525 << D.second.FileName << ", line:" << D.second.Line << "\n";
526 }
527 }
528 }
529 }
530
531 return 0;
532}
533
534static void doPreallocation(TrivialMemoryManager &MemMgr) {
535 // Allocate a slab of memory upfront, if required. This is used if
536 // we want to test small code models.
537 if (static_cast<intptr_t>(PreallocMemory) < 0)
538 report_fatal_error(reason: "Pre-allocated bytes of memory must be a positive integer.");
539
540 // FIXME: Limit the amount of memory that can be preallocated?
541 if (PreallocMemory != 0)
542 MemMgr.preallocateSlab(Size: PreallocMemory);
543}
544
545static int executeInput() {
546 // Load any dylibs requested on the command line.
547 loadDylibs();
548
549 // Instantiate a dynamic linker.
550 TrivialMemoryManager MemMgr;
551 doPreallocation(MemMgr);
552 RuntimeDyld Dyld(MemMgr, MemMgr);
553
554 // If we don't have any input files, read from stdin.
555 if (!InputFileList.size())
556 InputFileList.push_back(value: "-");
557 {
558 TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr);
559 for (auto &File : InputFileList) {
560 // Load the input memory buffer.
561 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
562 MemoryBuffer::getFileOrSTDIN(Filename: File);
563 if (std::error_code EC = InputBuffer.getError())
564 ErrorAndExit(Msg: "unable to read input: '" + EC.message() + "'");
565 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
566 ObjectFile::createObjectFile(Object: (*InputBuffer)->getMemBufferRef()));
567
568 if (!MaybeObj) {
569 std::string Buf;
570 raw_string_ostream OS(Buf);
571 logAllUnhandledErrors(E: MaybeObj.takeError(), OS);
572 ErrorAndExit(Msg: "unable to create object file: '" + Buf + "'");
573 }
574
575 ObjectFile &Obj = **MaybeObj;
576
577 // Load the object file
578 Dyld.loadObject(O: Obj);
579 if (Dyld.hasError()) {
580 ErrorAndExit(Msg: Dyld.getErrorString());
581 }
582 }
583 }
584
585 {
586 TimeRegion TR(Timers ? &Timers->LinkTimer : nullptr);
587 // Resove all the relocations we can.
588 // FIXME: Error out if there are unresolved relocations.
589 Dyld.resolveRelocations();
590 }
591
592 // Get the address of the entry point (_main by default).
593 void *MainAddress = Dyld.getSymbolLocalAddress(Name: EntryPoint);
594 if (!MainAddress)
595 ErrorAndExit(Msg: "no definition for '" + EntryPoint + "'");
596
597 // Invalidate the instruction cache for each loaded function.
598 for (auto &FM : MemMgr.FunctionMemory) {
599
600 auto &FM_MB = FM.MB;
601
602 // Make sure the memory is executable.
603 // setExecutable will call InvalidateInstructionCache.
604 if (auto EC = sys::Memory::protectMappedMemory(Block: FM_MB,
605 Flags: sys::Memory::MF_READ |
606 sys::Memory::MF_EXEC))
607 ErrorAndExit(Msg: "unable to mark function executable: '" + EC.message() +
608 "'");
609 }
610
611 // Dispatch to _main().
612 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
613
614 int (*Main)(int, const char**) =
615 (int(*)(int,const char**)) uintptr_t(MainAddress);
616 std::vector<const char *> Argv;
617 // Use the name of the first input object module as argv[0] for the target.
618 Argv.push_back(x: InputFileList[0].data());
619 for (auto &Arg : InputArgv)
620 Argv.push_back(x: Arg.data());
621 Argv.push_back(x: nullptr);
622 int Result = 0;
623 {
624 TimeRegion TR(Timers ? &Timers->RunTimer : nullptr);
625 Result = Main(Argv.size() - 1, Argv.data());
626 }
627
628 return Result;
629}
630
631static int checkAllExpressions(RuntimeDyldChecker &Checker) {
632 for (const auto& CheckerFileName : CheckFiles) {
633 ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
634 MemoryBuffer::getFileOrSTDIN(Filename: CheckerFileName);
635 if (std::error_code EC = CheckerFileBuf.getError())
636 ErrorAndExit(Msg: "unable to read input '" + CheckerFileName + "': " +
637 EC.message());
638
639 if (!Checker.checkAllRulesInBuffer(RulePrefix: "# rtdyld-check:",
640 MemBuf: CheckerFileBuf.get().get()))
641 ErrorAndExit(Msg: "some checks in '" + CheckerFileName + "' failed");
642 }
643 return 0;
644}
645
646void applySpecificSectionMappings(RuntimeDyld &Dyld,
647 const FileToSectionIDMap &FileToSecIDMap) {
648
649 for (StringRef Mapping : SpecificSectionMappings) {
650 size_t EqualsIdx = Mapping.find_first_of(C: '=');
651 std::string SectionIDStr = std::string(Mapping.substr(Start: 0, N: EqualsIdx));
652 size_t ComaIdx = Mapping.find_first_of(C: ',');
653
654 if (ComaIdx == StringRef::npos)
655 report_fatal_error(reason: "Invalid section specification '" + Mapping +
656 "'. Should be '<file name>,<section name>=<addr>'");
657
658 std::string FileName = SectionIDStr.substr(pos: 0, n: ComaIdx);
659 std::string SectionName = SectionIDStr.substr(pos: ComaIdx + 1);
660 unsigned SectionID =
661 ExitOnErr(getSectionId(FileToSecIDMap, FileName, SectionName));
662
663 auto* OldAddr = Dyld.getSectionContent(SectionID).data();
664 std::string NewAddrStr = std::string(Mapping.substr(Start: EqualsIdx + 1));
665 uint64_t NewAddr;
666
667 if (StringRef(NewAddrStr).getAsInteger(Radix: 0, Result&: NewAddr))
668 report_fatal_error(reason: "Invalid section address in mapping '" + Mapping +
669 "'.");
670
671 Dyld.mapSectionAddress(LocalAddress: OldAddr, TargetAddress: NewAddr);
672 }
673}
674
675// Scatter sections in all directions!
676// Remaps section addresses for -verify mode. The following command line options
677// can be used to customize the layout of the memory within the phony target's
678// address space:
679// -target-addr-start <s> -- Specify where the phony target address range starts.
680// -target-addr-end <e> -- Specify where the phony target address range ends.
681// -target-section-sep <d> -- Specify how big a gap should be left between the
682// end of one section and the start of the next.
683// Defaults to zero. Set to something big
684// (e.g. 1 << 32) to stress-test stubs, GOTs, etc.
685//
686static void remapSectionsAndSymbols(const llvm::Triple &TargetTriple,
687 RuntimeDyld &Dyld,
688 TrivialMemoryManager &MemMgr) {
689
690 // Set up a work list (section addr/size pairs).
691 typedef std::list<const TrivialMemoryManager::SectionInfo*> WorklistT;
692 WorklistT Worklist;
693
694 for (const auto& CodeSection : MemMgr.FunctionMemory)
695 Worklist.push_back(x: &CodeSection);
696 for (const auto& DataSection : MemMgr.DataMemory)
697 Worklist.push_back(x: &DataSection);
698
699 // Keep an "already allocated" mapping of section target addresses to sizes.
700 // Sections whose address mappings aren't specified on the command line will
701 // allocated around the explicitly mapped sections while maintaining the
702 // minimum separation.
703 std::map<uint64_t, uint64_t> AlreadyAllocated;
704
705 // Move the previously applied mappings (whether explicitly specified on the
706 // command line, or implicitly set by RuntimeDyld) into the already-allocated
707 // map.
708 for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end();
709 I != E;) {
710 WorklistT::iterator Tmp = I;
711 ++I;
712
713 auto LoadAddr = Dyld.getSectionLoadAddress(SectionID: (*Tmp)->SectionID);
714
715 if (LoadAddr != static_cast<uint64_t>(
716 reinterpret_cast<uintptr_t>((*Tmp)->MB.base()))) {
717 // A section will have a LoadAddr of 0 if it wasn't loaded for whatever
718 // reason (e.g. zero byte COFF sections). Don't include those sections in
719 // the allocation map.
720 if (LoadAddr != 0)
721 AlreadyAllocated[LoadAddr] = (*Tmp)->MB.allocatedSize();
722 Worklist.erase(position: Tmp);
723 }
724 }
725
726 // If the -target-addr-end option wasn't explicitly passed, then set it to a
727 // sensible default based on the target triple.
728 if (TargetAddrEnd.getNumOccurrences() == 0) {
729 if (TargetTriple.isArch16Bit())
730 TargetAddrEnd = (1ULL << 16) - 1;
731 else if (TargetTriple.isArch32Bit())
732 TargetAddrEnd = (1ULL << 32) - 1;
733 // TargetAddrEnd already has a sensible default for 64-bit systems, so
734 // there's nothing to do in the 64-bit case.
735 }
736
737 // Process any elements remaining in the worklist.
738 while (!Worklist.empty()) {
739 auto *CurEntry = Worklist.front();
740 Worklist.pop_front();
741
742 uint64_t NextSectionAddr = TargetAddrStart;
743
744 for (const auto &Alloc : AlreadyAllocated)
745 if (NextSectionAddr + CurEntry->MB.allocatedSize() + TargetSectionSep <=
746 Alloc.first)
747 break;
748 else
749 NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep;
750
751 Dyld.mapSectionAddress(LocalAddress: CurEntry->MB.base(), TargetAddress: NextSectionAddr);
752 AlreadyAllocated[NextSectionAddr] = CurEntry->MB.allocatedSize();
753 }
754
755 // Add dummy symbols to the memory manager.
756 for (const auto &Mapping : DummySymbolMappings) {
757 size_t EqualsIdx = Mapping.find_first_of(c: '=');
758
759 if (EqualsIdx == StringRef::npos)
760 report_fatal_error(reason: Twine("Invalid dummy symbol specification '") +
761 Mapping + "'. Should be '<symbol name>=<addr>'");
762
763 std::string Symbol = Mapping.substr(pos: 0, n: EqualsIdx);
764 std::string AddrStr = Mapping.substr(pos: EqualsIdx + 1);
765
766 uint64_t Addr;
767 if (StringRef(AddrStr).getAsInteger(Radix: 0, Result&: Addr))
768 report_fatal_error(reason: Twine("Invalid symbol mapping '") + Mapping + "'.");
769
770 MemMgr.addDummySymbol(Name: Symbol, Addr);
771 }
772}
773
774// Load and link the objects specified on the command line, but do not execute
775// anything. Instead, attach a RuntimeDyldChecker instance and call it to
776// verify the correctness of the linked memory.
777static int linkAndVerify() {
778
779 // Check for missing triple.
780 if (TripleName == "")
781 ErrorAndExit(Msg: "-triple required when running in -verify mode.");
782
783 // Look up the target and build the disassembler.
784 Triple TheTriple(Triple::normalize(Str: TripleName));
785 std::string ErrorStr;
786 const Target *TheTarget =
787 TargetRegistry::lookupTarget(ArchName: "", TheTriple, Error&: ErrorStr);
788 if (!TheTarget)
789 ErrorAndExit(Msg: "Error accessing target '" + TripleName + "': " + ErrorStr);
790
791 TripleName = TheTriple.getTriple();
792
793 std::unique_ptr<MCSubtargetInfo> STI(
794 TheTarget->createMCSubtargetInfo(TheTriple, CPU: MCPU, Features: ""));
795 if (!STI)
796 ErrorAndExit(Msg: "Unable to create subtarget info!");
797
798 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT: TheTriple));
799 if (!MRI)
800 ErrorAndExit(Msg: "Unable to create target register info!");
801
802 MCTargetOptions MCOptions;
803 std::unique_ptr<MCAsmInfo> MAI(
804 TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple, Options: MCOptions));
805 if (!MAI)
806 ErrorAndExit(Msg: "Unable to create target asm info!");
807
808 MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get());
809
810 std::unique_ptr<MCDisassembler> Disassembler(
811 TheTarget->createMCDisassembler(STI: *STI, Ctx));
812 if (!Disassembler)
813 ErrorAndExit(Msg: "Unable to create disassembler!");
814
815 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
816 if (!MII)
817 ErrorAndExit(Msg: "Unable to create target instruction info!");
818
819 std::unique_ptr<MCInstPrinter> InstPrinter(
820 TheTarget->createMCInstPrinter(T: TheTriple, SyntaxVariant: 0, MAI: *MAI, MII: *MII, MRI: *MRI));
821
822 // Load any dylibs requested on the command line.
823 loadDylibs();
824
825 // Instantiate a dynamic linker.
826 TrivialMemoryManager MemMgr;
827 doPreallocation(MemMgr);
828
829 struct StubID {
830 unsigned SectionID;
831 uint32_t Offset;
832 };
833 using StubInfos = StringMap<StubID>;
834 using StubContainers = StringMap<StubInfos>;
835
836 StubContainers StubMap;
837 RuntimeDyld Dyld(MemMgr, MemMgr);
838 Dyld.setProcessAllSections(true);
839
840 Dyld.setNotifyStubEmitted([&StubMap](StringRef FilePath,
841 StringRef SectionName,
842 StringRef SymbolName, unsigned SectionID,
843 uint32_t StubOffset) {
844 std::string ContainerName =
845 (sys::path::filename(path: FilePath) + "/" + SectionName).str();
846 StubMap[ContainerName][SymbolName] = {.SectionID: SectionID, .Offset: StubOffset};
847 });
848
849 auto GetSymbolInfo =
850 [&Dyld, &MemMgr](
851 StringRef Symbol) -> Expected<RuntimeDyldChecker::MemoryRegionInfo> {
852 RuntimeDyldChecker::MemoryRegionInfo SymInfo;
853
854 // First get the target address.
855 if (auto InternalSymbol = Dyld.getSymbol(Name: Symbol))
856 SymInfo.setTargetAddress(InternalSymbol.getAddress());
857 else {
858 // Symbol not found in RuntimeDyld. Fall back to external lookup.
859#ifdef _MSC_VER
860 using ExpectedLookupResult =
861 MSVCPExpected<JITSymbolResolver::LookupResult>;
862#else
863 using ExpectedLookupResult = Expected<JITSymbolResolver::LookupResult>;
864#endif
865
866 auto ResultP = std::make_shared<std::promise<ExpectedLookupResult>>();
867 auto ResultF = ResultP->get_future();
868
869 MemMgr.lookup(Symbols: JITSymbolResolver::LookupSet({Symbol}),
870 OnResolved: [=](Expected<JITSymbolResolver::LookupResult> Result) {
871 ResultP->set_value(std::move(Result));
872 });
873
874 auto Result = ResultF.get();
875 if (!Result)
876 return Result.takeError();
877
878 auto I = Result->find(x: Symbol);
879 assert(I != Result->end() &&
880 "Expected symbol address if no error occurred");
881 SymInfo.setTargetAddress(I->second.getAddress());
882 }
883
884 // Now find the symbol content if possible (otherwise leave content as a
885 // default-constructed StringRef).
886 if (auto *SymAddr = Dyld.getSymbolLocalAddress(Name: Symbol)) {
887 unsigned SectionID = Dyld.getSymbolSectionID(Name: Symbol);
888 if (SectionID != ~0U) {
889 char *CSymAddr = static_cast<char *>(SymAddr);
890 StringRef SecContent = Dyld.getSectionContent(SectionID);
891 uint64_t SymSize = SecContent.size() - (CSymAddr - SecContent.data());
892 SymInfo.setContent(ArrayRef<char>(CSymAddr, SymSize));
893 SymInfo.setTargetFlags(
894 Dyld.getSymbol(Name: Symbol).getFlags().getTargetFlags());
895 }
896 }
897 return SymInfo;
898 };
899
900 auto IsSymbolValid = [&Dyld, GetSymbolInfo](StringRef Symbol) {
901 if (Dyld.getSymbol(Name: Symbol))
902 return true;
903 auto SymInfo = GetSymbolInfo(Symbol);
904 if (!SymInfo) {
905 logAllUnhandledErrors(E: SymInfo.takeError(), OS&: errs(), ErrorBanner: "RTDyldChecker: ");
906 return false;
907 }
908 return SymInfo->getTargetAddress() != 0;
909 };
910
911 FileToSectionIDMap FileToSecIDMap;
912
913 auto GetSectionInfo = [&Dyld, &FileToSecIDMap](StringRef FileName,
914 StringRef SectionName)
915 -> Expected<RuntimeDyldChecker::MemoryRegionInfo> {
916 auto SectionID = getSectionId(FileToSecIDMap, FileName, SectionName);
917 if (!SectionID)
918 return SectionID.takeError();
919 RuntimeDyldChecker::MemoryRegionInfo SecInfo;
920 SecInfo.setTargetAddress(Dyld.getSectionLoadAddress(SectionID: *SectionID));
921 StringRef SecContent = Dyld.getSectionContent(SectionID: *SectionID);
922 SecInfo.setContent(ArrayRef<char>(SecContent));
923 return SecInfo;
924 };
925
926 auto GetStubInfo = [&Dyld, &StubMap](StringRef StubContainer,
927 StringRef SymbolName,
928 StringRef KindNameFilter)
929 -> Expected<RuntimeDyldChecker::MemoryRegionInfo> {
930 auto SMIt = StubMap.find(Key: StubContainer);
931 if (SMIt == StubMap.end())
932 return make_error<StringError>(Args: "Stub container not found: " +
933 StubContainer,
934 Args: inconvertibleErrorCode());
935 auto It = SMIt->second.find(Key: SymbolName);
936 if (It == SMIt->second.end())
937 return make_error<StringError>(Args: "Symbol name " + SymbolName +
938 " in stub container " + StubContainer,
939 Args: inconvertibleErrorCode());
940 auto &SI = It->second;
941 RuntimeDyldChecker::MemoryRegionInfo StubMemInfo;
942 StubMemInfo.setTargetAddress(Dyld.getSectionLoadAddress(SectionID: SI.SectionID) +
943 SI.Offset);
944 StringRef SecContent =
945 Dyld.getSectionContent(SectionID: SI.SectionID).substr(Start: SI.Offset);
946 StubMemInfo.setContent(ArrayRef<char>(SecContent));
947 return StubMemInfo;
948 };
949
950 auto GetGOTInfo = [&GetStubInfo](StringRef StubContainer,
951 StringRef SymbolName) {
952 return GetStubInfo(StubContainer, SymbolName, "");
953 };
954
955 // We will initialize this below once we have the first object file and can
956 // know the endianness.
957 std::unique_ptr<RuntimeDyldChecker> Checker;
958
959 // If we don't have any input files, read from stdin.
960 if (!InputFileList.size())
961 InputFileList.push_back(value: "-");
962 for (auto &InputFile : InputFileList) {
963 // Load the input memory buffer.
964 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
965 MemoryBuffer::getFileOrSTDIN(Filename: InputFile);
966
967 if (std::error_code EC = InputBuffer.getError())
968 ErrorAndExit(Msg: "unable to read input: '" + EC.message() + "'");
969
970 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
971 ObjectFile::createObjectFile(Object: (*InputBuffer)->getMemBufferRef()));
972
973 if (!MaybeObj) {
974 std::string Buf;
975 raw_string_ostream OS(Buf);
976 logAllUnhandledErrors(E: MaybeObj.takeError(), OS);
977 ErrorAndExit(Msg: "unable to create object file: '" + Buf + "'");
978 }
979
980 ObjectFile &Obj = **MaybeObj;
981
982 if (!Checker)
983 Checker = std::make_unique<RuntimeDyldChecker>(
984 args&: IsSymbolValid, args&: GetSymbolInfo, args&: GetSectionInfo, args&: GetStubInfo, args&: GetGOTInfo,
985 args: Obj.isLittleEndian() ? llvm::endianness::little
986 : llvm::endianness::big,
987 args&: TheTriple, args&: MCPU, args: SubtargetFeatures(), args&: dbgs());
988
989 auto FileName = sys::path::filename(path: InputFile);
990 MemMgr.setSectionIDsMap(&FileToSecIDMap[FileName]);
991
992 // Load the object file
993 Dyld.loadObject(O: Obj);
994 if (Dyld.hasError()) {
995 ErrorAndExit(Msg: Dyld.getErrorString());
996 }
997 }
998
999 // Re-map the section addresses into the phony target address space and add
1000 // dummy symbols.
1001 applySpecificSectionMappings(Dyld, FileToSecIDMap);
1002 remapSectionsAndSymbols(TargetTriple: TheTriple, Dyld, MemMgr);
1003
1004 // Resolve all the relocations we can.
1005 Dyld.resolveRelocations();
1006
1007 // Register EH frames.
1008 Dyld.registerEHFrames();
1009
1010 int ErrorCode = checkAllExpressions(Checker&: *Checker);
1011 if (Dyld.hasError())
1012 ErrorAndExit(Msg: "RTDyld reported an error applying relocations:\n " +
1013 Dyld.getErrorString());
1014
1015 return ErrorCode;
1016}
1017
1018int main(int argc, char **argv) {
1019 InitLLVM X(argc, argv);
1020 ProgramName = argv[0];
1021
1022 llvm::InitializeAllTargetInfos();
1023 llvm::InitializeAllTargetMCs();
1024 llvm::InitializeAllDisassemblers();
1025
1026 cl::HideUnrelatedOptions(Categories: {&RTDyldCategory, &getColorCategory()});
1027 cl::ParseCommandLineOptions(argc, argv, Overview: "llvm MC-JIT tool\n");
1028
1029 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
1030
1031 Timers = ShowTimes ? std::make_unique<RTDyldTimers>() : nullptr;
1032
1033 int Result = 0;
1034 switch (Action) {
1035 case AC_Execute:
1036 Result = executeInput();
1037 break;
1038 case AC_PrintDebugLineInfo:
1039 Result =
1040 printLineInfoForInput(/* LoadObjects */ true, /* UseDebugObj */ true);
1041 break;
1042 case AC_PrintLineInfo:
1043 Result =
1044 printLineInfoForInput(/* LoadObjects */ true, /* UseDebugObj */ false);
1045 break;
1046 case AC_PrintObjectLineInfo:
1047 Result =
1048 printLineInfoForInput(/* LoadObjects */ false, /* UseDebugObj */ false);
1049 break;
1050 case AC_Verify:
1051 Result = linkAndVerify();
1052 break;
1053 }
1054 return Result;
1055}
1056