blob: dfee97241a9aa4f2422fc4b7321028394505d5f7 [file] [log] [blame]
Lang Hames11c8dfa52019-04-20 17:10:34 +00001//===- llvm-jitlink.cpp -- Command line interface/tester for llvm-jitlink -===//
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 utility provides a simple command line interface to the llvm jitlink
10// library, which makes relocatable object files executable in memory. Its
11// primary function is as a testing utility for the jitlink library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm-jitlink.h"
16
Lang Hamesf5a885f2019-07-04 00:05:12 +000017#include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"
Lang Hames11c8dfa52019-04-20 17:10:34 +000018#include "llvm/ExecutionEngine/Orc/ExecutionUtils.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/COFF.h"
27#include "llvm/Object/MachO.h"
28#include "llvm/Object/ObjectFile.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/DynamicLibrary.h"
32#include "llvm/Support/InitLLVM.h"
33#include "llvm/Support/MemoryBuffer.h"
Lang Hames335676e2019-09-06 19:21:55 +000034#include "llvm/Support/Process.h"
Lang Hames11c8dfa52019-04-20 17:10:34 +000035#include "llvm/Support/TargetRegistry.h"
36#include "llvm/Support/TargetSelect.h"
Lang Hames6fd39602019-08-27 15:51:19 +000037#include "llvm/Support/Timer.h"
Lang Hames11c8dfa52019-04-20 17:10:34 +000038
39#include <list>
40#include <string>
41
Lang Hames11c8dfa52019-04-20 17:10:34 +000042#define DEBUG_TYPE "llvm-jitlink"
43
44using namespace llvm;
45using namespace llvm::jitlink;
46using namespace llvm::orc;
47
48static cl::list<std::string> InputFiles(cl::Positional, cl::OneOrMore,
49 cl::desc("input files"));
50
51static cl::opt<bool> NoExec("noexec", cl::desc("Do not execute loaded code"),
52 cl::init(false));
53
54static cl::list<std::string>
55 CheckFiles("check", cl::desc("File containing verifier checks"),
56 cl::ZeroOrMore);
57
58static cl::opt<std::string>
59 EntryPointName("entry", cl::desc("Symbol to call as main entry point"),
60 cl::init(""));
61
62static cl::list<std::string> JITLinkDylibs(
63 "jld", cl::desc("Specifies the JITDylib to be used for any subsequent "
64 "input file arguments"));
65
66static cl::list<std::string>
67 Dylibs("dlopen", cl::desc("Dynamic libraries to load before linking"),
68 cl::ZeroOrMore);
69
Lang Hamesd959a602019-04-24 17:23:05 +000070static cl::list<std::string> InputArgv("args", cl::Positional,
71 cl::desc("<program arguments>..."),
72 cl::ZeroOrMore, cl::PositionalEatsArgs);
73
Lang Hames11c8dfa52019-04-20 17:10:34 +000074static cl::opt<bool>
75 NoProcessSymbols("no-process-syms",
76 cl::desc("Do not resolve to llvm-jitlink process symbols"),
77 cl::init(false));
78
Nico Weber405e62b2019-04-21 23:50:24 +000079static cl::list<std::string> AbsoluteDefs(
80 "define-abs",
81 cl::desc("Inject absolute symbol definitions (syntax: <name>=<addr>)"),
82 cl::ZeroOrMore);
Lang Hames11c8dfa52019-04-20 17:10:34 +000083
84static cl::opt<bool> ShowAddrs(
85 "show-addrs",
86 cl::desc("Print registered symbol, section, got and stub addresses"),
87 cl::init(false));
88
89static cl::opt<bool> ShowAtomGraph(
90 "show-graph",
91 cl::desc("Print the atom graph after fixups have been applied"),
92 cl::init(false));
93
94static cl::opt<bool> ShowSizes(
Lang Hamesbc76bbc2019-04-21 20:34:19 +000095 "show-sizes",
96 cl::desc("Show sizes pre- and post-dead stripping, and allocations"),
97 cl::init(false));
98
Lang Hames6fd39602019-08-27 15:51:19 +000099static cl::opt<bool> ShowTimes("show-times",
100 cl::desc("Show times for llvm-jitlink phases"),
101 cl::init(false));
102
Lang Hames335676e2019-09-06 19:21:55 +0000103static cl::opt<std::string> SlabAllocateSizeString(
104 "slab-allocate",
105 cl::desc("Allocate from a slab of the given size "
106 "(allowable suffixes: Kb, Mb, Gb. default = "
107 "Kb)"),
108 cl::init(""));
109
Lang Hamesbc76bbc2019-04-21 20:34:19 +0000110static cl::opt<bool> ShowRelocatedSectionContents(
111 "show-relocated-section-contents",
112 cl::desc("show section contents after fixups have been applied"),
113 cl::init(false));
Lang Hames11c8dfa52019-04-20 17:10:34 +0000114
115ExitOnError ExitOnErr;
116
117namespace llvm {
118
119static raw_ostream &
120operator<<(raw_ostream &OS, const Session::MemoryRegionInfo &MRI) {
Lang Hames23085ec2019-05-12 22:26:33 +0000121 return OS << "target addr = "
122 << format("0x%016" PRIx64, MRI.getTargetAddress())
123 << ", content: " << (const void *)MRI.getContent().data() << " -- "
124 << (const void *)(MRI.getContent().data() + MRI.getContent().size())
125 << " (" << MRI.getContent().size() << " bytes)";
Lang Hames11c8dfa52019-04-20 17:10:34 +0000126}
127
128static raw_ostream &
129operator<<(raw_ostream &OS, const Session::SymbolInfoMap &SIM) {
130 OS << "Symbols:\n";
131 for (auto &SKV : SIM)
132 OS << " \"" << SKV.first() << "\" " << SKV.second << "\n";
133 return OS;
134}
135
136static raw_ostream &
137operator<<(raw_ostream &OS, const Session::FileInfo &FI) {
138 for (auto &SIKV : FI.SectionInfos)
139 OS << " Section \"" << SIKV.first() << "\": " << SIKV.second << "\n";
140 for (auto &GOTKV : FI.GOTEntryInfos)
141 OS << " GOT \"" << GOTKV.first() << "\": " << GOTKV.second << "\n";
142 for (auto &StubKV : FI.StubInfos)
143 OS << " Stub \"" << StubKV.first() << "\": " << StubKV.second << "\n";
144 return OS;
145}
146
147static raw_ostream &
148operator<<(raw_ostream &OS, const Session::FileInfoMap &FIM) {
149 for (auto &FIKV : FIM)
150 OS << "File \"" << FIKV.first() << "\":\n" << FIKV.second;
151 return OS;
152}
153
154static uint64_t computeTotalAtomSizes(AtomGraph &G) {
155 uint64_t TotalSize = 0;
156 for (auto *DA : G.defined_atoms())
157 if (DA->isZeroFill())
158 TotalSize += DA->getZeroFillSize();
159 else
160 TotalSize += DA->getContent().size();
161 return TotalSize;
162}
163
Lang Hamesbc76bbc2019-04-21 20:34:19 +0000164static void dumpSectionContents(raw_ostream &OS, AtomGraph &G) {
165 constexpr JITTargetAddress DumpWidth = 16;
166 static_assert(isPowerOf2_64(DumpWidth), "DumpWidth must be a power of two");
167
168 // Put sections in address order.
169 std::vector<Section *> Sections;
170 for (auto &S : G.sections())
171 Sections.push_back(&S);
172
173 std::sort(Sections.begin(), Sections.end(),
174 [](const Section *LHS, const Section *RHS) {
175 if (LHS->atoms_empty() && RHS->atoms_empty())
176 return false;
177 if (LHS->atoms_empty())
178 return false;
179 if (RHS->atoms_empty())
180 return true;
181 return (*LHS->atoms().begin())->getAddress() <
182 (*RHS->atoms().begin())->getAddress();
183 });
184
185 for (auto *S : Sections) {
186 OS << S->getName() << " content:";
187 if (S->atoms_empty()) {
188 OS << "\n section empty\n";
189 continue;
190 }
191
192 // Sort atoms into order, then render.
193 std::vector<DefinedAtom *> Atoms(S->atoms().begin(), S->atoms().end());
194 std::sort(Atoms.begin(), Atoms.end(),
195 [](const DefinedAtom *LHS, const DefinedAtom *RHS) {
196 return LHS->getAddress() < RHS->getAddress();
197 });
198
199 JITTargetAddress NextAddr = Atoms.front()->getAddress() & ~(DumpWidth - 1);
200 for (auto *DA : Atoms) {
201 bool IsZeroFill = DA->isZeroFill();
202 JITTargetAddress AtomStart = DA->getAddress();
203 JITTargetAddress AtomSize =
204 IsZeroFill ? DA->getZeroFillSize() : DA->getContent().size();
205 JITTargetAddress AtomEnd = AtomStart + AtomSize;
206 const uint8_t *AtomData =
207 IsZeroFill ? nullptr : DA->getContent().bytes_begin();
208
209 // Pad any space before the atom starts.
210 while (NextAddr != AtomStart) {
211 if (NextAddr % DumpWidth == 0)
212 OS << formatv("\n{0:x16}:", NextAddr);
213 OS << " ";
214 ++NextAddr;
215 }
216
217 // Render the atom content.
218 while (NextAddr != AtomEnd) {
219 if (NextAddr % DumpWidth == 0)
220 OS << formatv("\n{0:x16}:", NextAddr);
221 if (IsZeroFill)
222 OS << " 00";
223 else
224 OS << formatv(" {0:x-2}", AtomData[NextAddr - AtomStart]);
225 ++NextAddr;
226 }
227 }
228 OS << "\n";
229 }
230}
231
Lang Hames335676e2019-09-06 19:21:55 +0000232class JITLinkSlabAllocator final : public JITLinkMemoryManager {
233public:
234 static Expected<std::unique_ptr<JITLinkSlabAllocator>>
235 Create(uint64_t SlabSize) {
236 Error Err = Error::success();
237 std::unique_ptr<JITLinkSlabAllocator> Allocator(
238 new JITLinkSlabAllocator(SlabSize, Err));
239 if (Err)
240 return std::move(Err);
241 return std::move(Allocator);
242 }
243
244 Expected<std::unique_ptr<JITLinkMemoryManager::Allocation>>
245 allocate(const SegmentsRequestMap &Request) override {
246
247 using AllocationMap = DenseMap<unsigned, sys::MemoryBlock>;
248
249 // Local class for allocation.
250 class IPMMAlloc : public Allocation {
251 public:
252 IPMMAlloc(AllocationMap SegBlocks) : SegBlocks(std::move(SegBlocks)) {}
253 MutableArrayRef<char> getWorkingMemory(ProtectionFlags Seg) override {
254 assert(SegBlocks.count(Seg) && "No allocation for segment");
255 return {static_cast<char *>(SegBlocks[Seg].base()),
256 SegBlocks[Seg].allocatedSize()};
257 }
258 JITTargetAddress getTargetMemory(ProtectionFlags Seg) override {
259 assert(SegBlocks.count(Seg) && "No allocation for segment");
260 return reinterpret_cast<JITTargetAddress>(SegBlocks[Seg].base());
261 }
262 void finalizeAsync(FinalizeContinuation OnFinalize) override {
263 OnFinalize(applyProtections());
264 }
265 Error deallocate() override {
266 for (auto &KV : SegBlocks)
267 if (auto EC = sys::Memory::releaseMappedMemory(KV.second))
268 return errorCodeToError(EC);
269 return Error::success();
270 }
271
272 private:
273 Error applyProtections() {
274 for (auto &KV : SegBlocks) {
275 auto &Prot = KV.first;
276 auto &Block = KV.second;
277 if (auto EC = sys::Memory::protectMappedMemory(Block, Prot))
278 return errorCodeToError(EC);
279 if (Prot & sys::Memory::MF_EXEC)
280 sys::Memory::InvalidateInstructionCache(Block.base(),
281 Block.allocatedSize());
282 }
283 return Error::success();
284 }
285
286 AllocationMap SegBlocks;
287 };
288
289 AllocationMap Blocks;
290
291 for (auto &KV : Request) {
292 auto &Seg = KV.second;
293
294 if (Seg.getContentAlignment() > PageSize)
295 return make_error<StringError>("Cannot request higher than page "
296 "alignment",
297 inconvertibleErrorCode());
298
299 if (PageSize % Seg.getContentAlignment() != 0)
300 return make_error<StringError>("Page size is not a multiple of "
301 "alignment",
302 inconvertibleErrorCode());
303
304 uint64_t ZeroFillStart =
305 alignTo(Seg.getContentSize(), Seg.getZeroFillAlignment());
306 uint64_t SegmentSize = ZeroFillStart + Seg.getZeroFillSize();
307
308 // Round segment size up to page boundary.
309 SegmentSize = (SegmentSize + PageSize - 1) & ~(PageSize - 1);
310
311 // Take segment bytes from the front of the slab.
312 void *SlabBase = SlabRemaining.base();
313 uint64_t SlabRemainingSize = SlabRemaining.allocatedSize();
314
315 if (SegmentSize > SlabRemainingSize)
316 return make_error<StringError>("Slab allocator out of memory",
317 inconvertibleErrorCode());
318
319 sys::MemoryBlock SegMem(SlabBase, SegmentSize);
320 SlabRemaining =
321 sys::MemoryBlock(reinterpret_cast<char *>(SlabBase) + SegmentSize,
322 SlabRemainingSize - SegmentSize);
323
324 // Zero out the zero-fill memory.
325 memset(static_cast<char *>(SegMem.base()) + ZeroFillStart, 0,
326 Seg.getZeroFillSize());
327
328 // Record the block for this segment.
329 Blocks[KV.first] = std::move(SegMem);
330 }
331 return std::unique_ptr<InProcessMemoryManager::Allocation>(
332 new IPMMAlloc(std::move(Blocks)));
333 }
334
335private:
336 JITLinkSlabAllocator(uint64_t SlabSize, Error &Err) {
337 ErrorAsOutParameter _(&Err);
338
339 PageSize = sys::Process::getPageSizeEstimate();
340
341 if (!isPowerOf2_64(PageSize)) {
342 Err = make_error<StringError>("Page size is not a power of 2",
343 inconvertibleErrorCode());
344 return;
345 }
346
347 // Round slab request up to page size.
348 SlabSize = (SlabSize + PageSize - 1) & ~(PageSize - 1);
349
350 const sys::Memory::ProtectionFlags ReadWrite =
351 static_cast<sys::Memory::ProtectionFlags>(sys::Memory::MF_READ |
352 sys::Memory::MF_WRITE);
353
354 std::error_code EC;
355 SlabRemaining =
356 sys::Memory::allocateMappedMemory(SlabSize, nullptr, ReadWrite, EC);
357
358 if (EC) {
359 Err = errorCodeToError(EC);
360 return;
361 }
362 }
363
364 sys::MemoryBlock SlabRemaining;
365 uint64_t PageSize = 0;
366};
367
368Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {
369 SizeString = SizeString.trim();
370
371 uint64_t Units = 1024;
372
373 if (SizeString.endswith_lower("kb"))
374 SizeString = SizeString.drop_back(2).rtrim();
375 else if (SizeString.endswith_lower("mb")) {
376 Units = 1024 * 1024;
377 SizeString = SizeString.drop_back(2).rtrim();
378 } else if (SizeString.endswith_lower("gb")) {
379 Units = 1024 * 1024 * 1024;
380 SizeString = SizeString.drop_back(2).rtrim();
381 }
382
383 uint64_t SlabSize = 0;
384 if (SizeString.getAsInteger(10, SlabSize))
385 return make_error<StringError>("Invalid numeric format for slab size",
386 inconvertibleErrorCode());
387
388 return SlabSize * Units;
389}
390
391static std::unique_ptr<jitlink::JITLinkMemoryManager> createMemoryManager() {
392 if (!SlabAllocateSizeString.empty()) {
393 auto SlabSize = ExitOnErr(getSlabAllocSize(SlabAllocateSizeString));
394 return ExitOnErr(JITLinkSlabAllocator::Create(SlabSize));
395 }
396 return std::make_unique<jitlink::InProcessMemoryManager>();
397}
398
399Session::Session(Triple TT)
400 : MemMgr(createMemoryManager()), ObjLayer(ES, *MemMgr), TT(std::move(TT)) {
Lang Hamesa9fdf372019-04-26 22:58:39 +0000401
402 /// Local ObjectLinkingLayer::Plugin class to forward modifyPassConfig to the
403 /// Session.
404 class JITLinkSessionPlugin : public ObjectLinkingLayer::Plugin {
405 public:
406 JITLinkSessionPlugin(Session &S) : S(S) {}
407 void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT,
408 PassConfiguration &PassConfig) {
409 S.modifyPassConfig(TT, PassConfig);
410 }
411
412 private:
413 Session &S;
414 };
415
416 if (!NoExec && !TT.isOSWindows())
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000417 ObjLayer.addPlugin(std::make_unique<EHFrameRegistrationPlugin>(
Lang Hamesf5a885f2019-07-04 00:05:12 +0000418 InProcessEHFrameRegistrar::getInstance()));
Lang Hamesa9fdf372019-04-26 22:58:39 +0000419
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000420 ObjLayer.addPlugin(std::make_unique<JITLinkSessionPlugin>(*this));
Lang Hamesa9fdf372019-04-26 22:58:39 +0000421}
Lang Hames11c8dfa52019-04-20 17:10:34 +0000422
423void Session::dumpSessionInfo(raw_ostream &OS) {
424 OS << "Registered addresses:\n" << SymbolInfos << FileInfos;
425}
426
427void Session::modifyPassConfig(const Triple &FTT,
428 PassConfiguration &PassConfig) {
429 if (!CheckFiles.empty())
430 PassConfig.PostFixupPasses.push_back([this](AtomGraph &G) {
431 if (TT.getObjectFormat() == Triple::MachO)
432 return registerMachOStubsAndGOT(*this, G);
433 return make_error<StringError>("Unsupported object format for GOT/stub "
434 "registration",
435 inconvertibleErrorCode());
436 });
437
438 if (ShowAtomGraph)
439 PassConfig.PostFixupPasses.push_back([](AtomGraph &G) -> Error {
440 outs() << "Atom graph post-fixup:\n";
441 G.dump(outs());
442 return Error::success();
443 });
444
445
446 if (ShowSizes) {
447 PassConfig.PrePrunePasses.push_back([this](AtomGraph &G) -> Error {
448 SizeBeforePruning += computeTotalAtomSizes(G);
449 return Error::success();
450 });
451 PassConfig.PostFixupPasses.push_back([this](AtomGraph &G) -> Error {
452 SizeAfterFixups += computeTotalAtomSizes(G);
453 return Error::success();
454 });
455 }
456
Lang Hamesbc76bbc2019-04-21 20:34:19 +0000457 if (ShowRelocatedSectionContents)
458 PassConfig.PostFixupPasses.push_back([](AtomGraph &G) -> Error {
459 outs() << "Relocated section contents for " << G.getName() << ":\n";
460 dumpSectionContents(outs(), G);
461 return Error::success();
462 });
Lang Hames11c8dfa52019-04-20 17:10:34 +0000463}
464
465Expected<Session::FileInfo &> Session::findFileInfo(StringRef FileName) {
466 auto FileInfoItr = FileInfos.find(FileName);
467 if (FileInfoItr == FileInfos.end())
468 return make_error<StringError>("file \"" + FileName + "\" not recognized",
469 inconvertibleErrorCode());
470 return FileInfoItr->second;
471}
472
473Expected<Session::MemoryRegionInfo &>
474Session::findSectionInfo(StringRef FileName, StringRef SectionName) {
475 auto FI = findFileInfo(FileName);
476 if (!FI)
477 return FI.takeError();
478 auto SecInfoItr = FI->SectionInfos.find(SectionName);
479 if (SecInfoItr == FI->SectionInfos.end())
480 return make_error<StringError>("no section \"" + SectionName +
481 "\" registered for file \"" + FileName +
482 "\"",
483 inconvertibleErrorCode());
484 return SecInfoItr->second;
485}
486
487Expected<Session::MemoryRegionInfo &>
488Session::findStubInfo(StringRef FileName, StringRef TargetName) {
489 auto FI = findFileInfo(FileName);
490 if (!FI)
491 return FI.takeError();
492 auto StubInfoItr = FI->StubInfos.find(TargetName);
493 if (StubInfoItr == FI->StubInfos.end())
494 return make_error<StringError>("no stub for \"" + TargetName +
495 "\" registered for file \"" + FileName +
496 "\"",
497 inconvertibleErrorCode());
498 return StubInfoItr->second;
499}
500
501Expected<Session::MemoryRegionInfo &>
502Session::findGOTEntryInfo(StringRef FileName, StringRef TargetName) {
503 auto FI = findFileInfo(FileName);
504 if (!FI)
505 return FI.takeError();
506 auto GOTInfoItr = FI->GOTEntryInfos.find(TargetName);
507 if (GOTInfoItr == FI->GOTEntryInfos.end())
508 return make_error<StringError>("no GOT entry for \"" + TargetName +
509 "\" registered for file \"" + FileName +
510 "\"",
511 inconvertibleErrorCode());
512 return GOTInfoItr->second;
513}
514
515bool Session::isSymbolRegistered(StringRef SymbolName) {
516 return SymbolInfos.count(SymbolName);
517}
518
519Expected<Session::MemoryRegionInfo &>
520Session::findSymbolInfo(StringRef SymbolName, Twine ErrorMsgStem) {
521 auto SymInfoItr = SymbolInfos.find(SymbolName);
522 if (SymInfoItr == SymbolInfos.end())
523 return make_error<StringError>(ErrorMsgStem + ": symbol " + SymbolName +
524 " not found",
525 inconvertibleErrorCode());
526 return SymInfoItr->second;
527}
528
529} // end namespace llvm
530
Lang Hamesb1ba4d82019-04-24 15:15:55 +0000531Triple getFirstFileTriple() {
532 assert(!InputFiles.empty() && "InputFiles can not be empty");
533 auto ObjBuffer =
534 ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(InputFiles.front())));
535 auto Obj = ExitOnErr(
536 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()));
537 return Obj->makeTriple();
538}
539
Lang Hamesd959a602019-04-24 17:23:05 +0000540Error sanitizeArguments(const Session &S) {
Lang Hamesb1ba4d82019-04-24 15:15:55 +0000541 if (EntryPointName.empty()) {
542 if (S.TT.getObjectFormat() == Triple::MachO)
543 EntryPointName = "_main";
544 else
545 EntryPointName = "main";
546 }
Lang Hamesd959a602019-04-24 17:23:05 +0000547
548 if (NoExec && !InputArgv.empty())
549 outs() << "Warning: --args passed to -noexec run will be ignored.\n";
550
551 return Error::success();
Lang Hamesb1ba4d82019-04-24 15:15:55 +0000552}
553
Lang Hames11c8dfa52019-04-20 17:10:34 +0000554Error loadProcessSymbols(Session &S) {
555 std::string ErrMsg;
556 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, &ErrMsg))
557 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
558
559 char GlobalPrefix = S.TT.getObjectFormat() == Triple::MachO ? '_' : '\0';
Lang Hamesb1ba4d82019-04-24 15:15:55 +0000560 auto InternedEntryPointName = S.ES.intern(EntryPointName);
561 auto FilterMainEntryPoint = [InternedEntryPointName](SymbolStringPtr Name) {
562 return Name != InternedEntryPointName;
563 };
Lang Hames52a34a72019-08-13 16:05:18 +0000564 S.ES.getMainJITDylib().addGenerator(
Lang Hamesb1ba4d82019-04-24 15:15:55 +0000565 ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
566 GlobalPrefix, FilterMainEntryPoint)));
Lang Hames11c8dfa52019-04-20 17:10:34 +0000567
568 return Error::success();
569}
570
571Error loadDylibs() {
572 // FIXME: This should all be handled inside DynamicLibrary.
573 for (const auto &Dylib : Dylibs) {
574 if (!sys::fs::is_regular_file(Dylib))
575 return make_error<StringError>("\"" + Dylib + "\" is not a regular file",
576 inconvertibleErrorCode());
577 std::string ErrMsg;
578 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
579 return make_error<StringError>(ErrMsg, inconvertibleErrorCode());
580 }
581
582 return Error::success();
583}
584
Lang Hames11c8dfa52019-04-20 17:10:34 +0000585Error loadObjects(Session &S) {
586
587 std::map<unsigned, JITDylib *> IdxToJLD;
588
589 // First, set up JITDylibs.
590 LLVM_DEBUG(dbgs() << "Creating JITDylibs...\n");
591 {
592 // Create a "main" JITLinkDylib.
593 auto &MainJD = S.ES.getMainJITDylib();
594 IdxToJLD[0] = &MainJD;
595 S.JDSearchOrder.push_back(&MainJD);
596 LLVM_DEBUG(dbgs() << " 0: " << MainJD.getName() << "\n");
597
598 // Add any extra JITLinkDylibs from the command line.
599 std::string JDNamePrefix("lib");
600 for (auto JLDItr = JITLinkDylibs.begin(), JLDEnd = JITLinkDylibs.end();
601 JLDItr != JLDEnd; ++JLDItr) {
602 auto &JD = S.ES.createJITDylib(JDNamePrefix + *JLDItr);
603 unsigned JDIdx =
604 JITLinkDylibs.getPosition(JLDItr - JITLinkDylibs.begin());
605 IdxToJLD[JDIdx] = &JD;
606 S.JDSearchOrder.push_back(&JD);
607 LLVM_DEBUG(dbgs() << " " << JDIdx << ": " << JD.getName() << "\n");
608 }
609
610 // Set every dylib to link against every other, in command line order.
611 for (auto *JD : S.JDSearchOrder) {
612 JITDylibSearchList O;
613 for (auto *JD2 : S.JDSearchOrder) {
614 if (JD2 == JD)
615 continue;
616 O.push_back(std::make_pair(JD2, false));
617 }
618 JD->setSearchOrder(std::move(O));
619 }
620 }
621
622 // Load each object into the corresponding JITDylib..
623 LLVM_DEBUG(dbgs() << "Adding objects...\n");
624 for (auto InputFileItr = InputFiles.begin(), InputFileEnd = InputFiles.end();
625 InputFileItr != InputFileEnd; ++InputFileItr) {
626 unsigned InputFileArgIdx =
627 InputFiles.getPosition(InputFileItr - InputFiles.begin());
628 StringRef InputFile = *InputFileItr;
629 auto &JD = *std::prev(IdxToJLD.lower_bound(InputFileArgIdx))->second;
630 LLVM_DEBUG(dbgs() << " " << InputFileArgIdx << ": \"" << InputFile
631 << "\" to " << JD.getName() << "\n";);
632 auto ObjBuffer =
633 ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(InputFile)));
634 ExitOnErr(S.ObjLayer.add(JD, std::move(ObjBuffer)));
635 }
636
637 // Define absolute symbols.
638 LLVM_DEBUG(dbgs() << "Defining absolute symbols...\n");
639 for (auto AbsDefItr = AbsoluteDefs.begin(), AbsDefEnd = AbsoluteDefs.end();
640 AbsDefItr != AbsDefEnd; ++AbsDefItr) {
641 unsigned AbsDefArgIdx =
642 AbsoluteDefs.getPosition(AbsDefItr - AbsoluteDefs.begin());
643 auto &JD = *std::prev(IdxToJLD.lower_bound(AbsDefArgIdx))->second;
644
645 StringRef AbsDefStmt = *AbsDefItr;
646 size_t EqIdx = AbsDefStmt.find_first_of('=');
647 if (EqIdx == StringRef::npos)
648 return make_error<StringError>("Invalid absolute define \"" + AbsDefStmt +
649 "\". Syntax: <name>=<addr>",
650 inconvertibleErrorCode());
651 StringRef Name = AbsDefStmt.substr(0, EqIdx).trim();
652 StringRef AddrStr = AbsDefStmt.substr(EqIdx + 1).trim();
653
654 uint64_t Addr;
655 if (AddrStr.getAsInteger(0, Addr))
656 return make_error<StringError>("Invalid address expression \"" + AddrStr +
657 "\" in absolute define \"" + AbsDefStmt +
658 "\"",
659 inconvertibleErrorCode());
660 JITEvaluatedSymbol AbsDef(Addr, JITSymbolFlags::Exported);
661 if (auto Err = JD.define(absoluteSymbols({{S.ES.intern(Name), AbsDef}})))
662 return Err;
663
664 // Register the absolute symbol with the session symbol infos.
665 S.SymbolInfos[Name] = { StringRef(), Addr };
666 }
667
668 LLVM_DEBUG({
669 dbgs() << "Dylib search order is [ ";
670 for (auto *JD : S.JDSearchOrder)
671 dbgs() << JD->getName() << " ";
672 dbgs() << "]\n";
673 });
674
675 return Error::success();
676}
677
678Error runChecks(Session &S) {
679
680 auto TripleName = S.TT.str();
681 std::string ErrorStr;
682 const Target *TheTarget = TargetRegistry::lookupTarget("", S.TT, ErrorStr);
683 if (!TheTarget)
684 ExitOnErr(make_error<StringError>("Error accessing target '" + TripleName +
685 "': " + ErrorStr,
686 inconvertibleErrorCode()));
687
688 std::unique_ptr<MCSubtargetInfo> STI(
689 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
690 if (!STI)
691 ExitOnErr(
692 make_error<StringError>("Unable to create subtarget for " + TripleName,
693 inconvertibleErrorCode()));
694
695 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
696 if (!MRI)
697 ExitOnErr(make_error<StringError>("Unable to create target register info "
698 "for " +
699 TripleName,
700 inconvertibleErrorCode()));
701
702 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
703 if (!MAI)
704 ExitOnErr(make_error<StringError>("Unable to create target asm info " +
705 TripleName,
706 inconvertibleErrorCode()));
707
708 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
709
710 std::unique_ptr<MCDisassembler> Disassembler(
711 TheTarget->createMCDisassembler(*STI, Ctx));
712 if (!Disassembler)
713 ExitOnErr(make_error<StringError>("Unable to create disassembler for " +
714 TripleName,
715 inconvertibleErrorCode()));
716
717 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
718
719 std::unique_ptr<MCInstPrinter> InstPrinter(
720 TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
721
722 auto IsSymbolValid = [&S](StringRef Symbol) {
723 return S.isSymbolRegistered(Symbol);
724 };
725
726 auto GetSymbolInfo = [&S](StringRef Symbol) {
727 return S.findSymbolInfo(Symbol, "Can not get symbol info");
728 };
729
730 auto GetSectionInfo = [&S](StringRef FileName, StringRef SectionName) {
731 return S.findSectionInfo(FileName, SectionName);
732 };
733
734 auto GetStubInfo = [&S](StringRef FileName, StringRef SectionName) {
735 return S.findStubInfo(FileName, SectionName);
736 };
737
738 auto GetGOTInfo = [&S](StringRef FileName, StringRef SectionName) {
739 return S.findGOTEntryInfo(FileName, SectionName);
740 };
741
742 RuntimeDyldChecker Checker(
743 IsSymbolValid, GetSymbolInfo, GetSectionInfo, GetStubInfo, GetGOTInfo,
744 S.TT.isLittleEndian() ? support::little : support::big,
745 Disassembler.get(), InstPrinter.get(), dbgs());
746
747 for (auto &CheckFile : CheckFiles) {
748 auto CheckerFileBuf =
749 ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(CheckFile)));
750 if (!Checker.checkAllRulesInBuffer("# jitlink-check:", &*CheckerFileBuf))
751 ExitOnErr(make_error<StringError>(
752 "Some checks in " + CheckFile + " failed", inconvertibleErrorCode()));
753 }
754
755 return Error::success();
756}
757
758static void dumpSessionStats(Session &S) {
759 if (ShowSizes)
760 outs() << "Total size of all atoms before pruning: " << S.SizeBeforePruning
761 << "\nTotal size of all atoms after fixups: " << S.SizeAfterFixups
762 << "\n";
763}
764
765static Expected<JITEvaluatedSymbol> getMainEntryPoint(Session &S) {
Lang Hames11c8dfa52019-04-20 17:10:34 +0000766 return S.ES.lookup(S.JDSearchOrder, EntryPointName);
767}
768
769Expected<int> runEntryPoint(Session &S, JITEvaluatedSymbol EntryPoint) {
770 assert(EntryPoint.getAddress() && "Entry point address should not be null");
771
772 constexpr const char *JITProgramName = "<llvm-jitlink jit'd code>";
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000773 auto PNStorage = std::make_unique<char[]>(strlen(JITProgramName) + 1);
Lang Hames11c8dfa52019-04-20 17:10:34 +0000774 strcpy(PNStorage.get(), JITProgramName);
775
776 std::vector<const char *> EntryPointArgs;
777 EntryPointArgs.push_back(PNStorage.get());
Lang Hamesd959a602019-04-24 17:23:05 +0000778 for (auto &InputArg : InputArgv)
779 EntryPointArgs.push_back(InputArg.data());
Lang Hames11c8dfa52019-04-20 17:10:34 +0000780 EntryPointArgs.push_back(nullptr);
781
782 using MainTy = int (*)(int, const char *[]);
783 MainTy EntryPointPtr = reinterpret_cast<MainTy>(EntryPoint.getAddress());
784
785 return EntryPointPtr(EntryPointArgs.size() - 1, EntryPointArgs.data());
786}
787
Lang Hames200415c2019-09-04 18:38:29 +0000788struct JITLinkTimers {
Lang Hames41adc372019-09-04 20:26:26 +0000789 TimerGroup JITLinkTG{"llvm-jitlink timers", "timers for llvm-jitlink phases"};
790 Timer LoadObjectsTimer{"load", "time to load/add object files", JITLinkTG};
791 Timer LinkTimer{"link", "time to link object files", JITLinkTG};
792 Timer RunTimer{"run", "time to execute jitlink'd code", JITLinkTG};
Lang Hames200415c2019-09-04 18:38:29 +0000793};
794
Lang Hames11c8dfa52019-04-20 17:10:34 +0000795int main(int argc, char *argv[]) {
796 InitLLVM X(argc, argv);
797
798 InitializeAllTargetInfos();
799 InitializeAllTargetMCs();
800 InitializeAllDisassemblers();
801
802 cl::ParseCommandLineOptions(argc, argv, "llvm jitlink tool");
803 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
804
Lang Hames200415c2019-09-04 18:38:29 +0000805 /// If timers are enabled, create a JITLinkTimers instance.
806 std::unique_ptr<JITLinkTimers> Timers =
807 ShowTimes ? std::make_unique<JITLinkTimers>() : nullptr;
808
Lang Hames11c8dfa52019-04-20 17:10:34 +0000809 Session S(getFirstFileTriple());
810
Lang Hamesd959a602019-04-24 17:23:05 +0000811 ExitOnErr(sanitizeArguments(S));
Lang Hamesb1ba4d82019-04-24 15:15:55 +0000812
Lang Hames11c8dfa52019-04-20 17:10:34 +0000813 if (!NoProcessSymbols)
814 ExitOnErr(loadProcessSymbols(S));
815 ExitOnErr(loadDylibs());
816
Lang Hames11c8dfa52019-04-20 17:10:34 +0000817
Lang Hames6fd39602019-08-27 15:51:19 +0000818 {
Lang Hames200415c2019-09-04 18:38:29 +0000819 TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr);
Lang Hames6fd39602019-08-27 15:51:19 +0000820 ExitOnErr(loadObjects(S));
Lang Hames6fd39602019-08-27 15:51:19 +0000821 }
822
823 JITEvaluatedSymbol EntryPoint = 0;
824 {
Lang Hames200415c2019-09-04 18:38:29 +0000825 TimeRegion TR(Timers ? &Timers->LinkTimer : nullptr);
Lang Hames6fd39602019-08-27 15:51:19 +0000826 EntryPoint = ExitOnErr(getMainEntryPoint(S));
Lang Hames6fd39602019-08-27 15:51:19 +0000827 }
Lang Hames11c8dfa52019-04-20 17:10:34 +0000828
829 if (ShowAddrs)
830 S.dumpSessionInfo(outs());
831
832 ExitOnErr(runChecks(S));
833
834 dumpSessionStats(S);
835
836 if (NoExec)
837 return 0;
838
Lang Hames6fd39602019-08-27 15:51:19 +0000839 int Result = 0;
840 {
Lang Hames200415c2019-09-04 18:38:29 +0000841 TimeRegion TR(Timers ? &Timers->RunTimer : nullptr);
Lang Hames6fd39602019-08-27 15:51:19 +0000842 Result = ExitOnErr(runEntryPoint(S, EntryPoint));
Lang Hames6fd39602019-08-27 15:51:19 +0000843 }
844
Lang Hames6fd39602019-08-27 15:51:19 +0000845 return Result;
Lang Hames11c8dfa52019-04-20 17:10:34 +0000846}