llvm-jitlink.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. //===- 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. #include "llvm-jitlink.h"
  15. #include "llvm/BinaryFormat/Magic.h"
  16. #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
  17. #include "llvm/ExecutionEngine/Orc/TPCDynamicLibrarySearchGenerator.h"
  18. #include "llvm/ExecutionEngine/Orc/TPCEHFrameRegistrar.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/Object/COFF.h"
  28. #include "llvm/Object/MachO.h"
  29. #include "llvm/Object/ObjectFile.h"
  30. #include "llvm/Support/CommandLine.h"
  31. #include "llvm/Support/Debug.h"
  32. #include "llvm/Support/InitLLVM.h"
  33. #include "llvm/Support/MemoryBuffer.h"
  34. #include "llvm/Support/Process.h"
  35. #include "llvm/Support/TargetRegistry.h"
  36. #include "llvm/Support/TargetSelect.h"
  37. #include "llvm/Support/Timer.h"
  38. #include <list>
  39. #include <string>
  40. #ifdef LLVM_ON_UNIX
  41. #include <netdb.h>
  42. #include <netinet/in.h>
  43. #include <sys/socket.h>
  44. #include <unistd.h>
  45. #endif // LLVM_ON_UNIX
  46. #define DEBUG_TYPE "llvm_jitlink"
  47. using namespace llvm;
  48. using namespace llvm::jitlink;
  49. using namespace llvm::orc;
  50. static cl::list<std::string> InputFiles(cl::Positional, cl::OneOrMore,
  51. cl::desc("input files"));
  52. static cl::opt<bool> NoExec("noexec", cl::desc("Do not execute loaded code"),
  53. cl::init(false));
  54. static cl::list<std::string>
  55. CheckFiles("check", cl::desc("File containing verifier checks"),
  56. cl::ZeroOrMore);
  57. static cl::opt<std::string>
  58. CheckName("check-name", cl::desc("Name of checks to match against"),
  59. cl::init("jitlink-check"));
  60. static cl::opt<std::string>
  61. EntryPointName("entry", cl::desc("Symbol to call as main entry point"),
  62. cl::init(""));
  63. static cl::list<std::string> JITLinkDylibs(
  64. "jld", cl::desc("Specifies the JITDylib to be used for any subsequent "
  65. "input file arguments"));
  66. static cl::list<std::string>
  67. Dylibs("dlopen", cl::desc("Dynamic libraries to load before linking"),
  68. cl::ZeroOrMore);
  69. static cl::list<std::string> InputArgv("args", cl::Positional,
  70. cl::desc("<program arguments>..."),
  71. cl::ZeroOrMore, cl::PositionalEatsArgs);
  72. static cl::opt<bool>
  73. NoProcessSymbols("no-process-syms",
  74. cl::desc("Do not resolve to llvm-jitlink process symbols"),
  75. cl::init(false));
  76. static cl::list<std::string> AbsoluteDefs(
  77. "define-abs",
  78. cl::desc("Inject absolute symbol definitions (syntax: <name>=<addr>)"),
  79. cl::ZeroOrMore);
  80. static cl::list<std::string> TestHarnesses("harness", cl::Positional,
  81. cl::desc("Test harness files"),
  82. cl::ZeroOrMore,
  83. cl::PositionalEatsArgs);
  84. static cl::opt<bool> ShowInitialExecutionSessionState(
  85. "show-init-es",
  86. cl::desc("Print ExecutionSession state before resolving entry point"),
  87. cl::init(false));
  88. static cl::opt<bool> ShowAddrs(
  89. "show-addrs",
  90. cl::desc("Print registered symbol, section, got and stub addresses"),
  91. cl::init(false));
  92. static cl::opt<bool> ShowLinkGraph(
  93. "show-graph",
  94. cl::desc("Print the link graph after fixups have been applied"),
  95. cl::init(false));
  96. static cl::opt<bool> ShowSizes(
  97. "show-sizes",
  98. cl::desc("Show sizes pre- and post-dead stripping, and allocations"),
  99. cl::init(false));
  100. static cl::opt<bool> ShowTimes("show-times",
  101. cl::desc("Show times for llvm-jitlink phases"),
  102. cl::init(false));
  103. static 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. static cl::opt<uint64_t> SlabAddress(
  110. "slab-address",
  111. cl::desc("Set slab target address (requires -slab-allocate and -noexec)"),
  112. cl::init(~0ULL));
  113. static cl::opt<bool> ShowRelocatedSectionContents(
  114. "show-relocated-section-contents",
  115. cl::desc("show section contents after fixups have been applied"),
  116. cl::init(false));
  117. static cl::opt<bool> PhonyExternals(
  118. "phony-externals",
  119. cl::desc("resolve all otherwise unresolved externals to null"),
  120. cl::init(false));
  121. static cl::opt<std::string> OutOfProcessExecutor(
  122. "oop-executor", cl::desc("Launch an out-of-process executor to run code"),
  123. cl::ValueOptional);
  124. static cl::opt<std::string> OutOfProcessExecutorConnect(
  125. "oop-executor-connect",
  126. cl::desc("Connect to an out-of-process executor via TCP"));
  127. ExitOnError ExitOnErr;
  128. namespace llvm {
  129. static raw_ostream &
  130. operator<<(raw_ostream &OS, const Session::MemoryRegionInfo &MRI) {
  131. return OS << "target addr = "
  132. << format("0x%016" PRIx64, MRI.getTargetAddress())
  133. << ", content: " << (const void *)MRI.getContent().data() << " -- "
  134. << (const void *)(MRI.getContent().data() + MRI.getContent().size())
  135. << " (" << MRI.getContent().size() << " bytes)";
  136. }
  137. static raw_ostream &
  138. operator<<(raw_ostream &OS, const Session::SymbolInfoMap &SIM) {
  139. OS << "Symbols:\n";
  140. for (auto &SKV : SIM)
  141. OS << " \"" << SKV.first() << "\" " << SKV.second << "\n";
  142. return OS;
  143. }
  144. static raw_ostream &
  145. operator<<(raw_ostream &OS, const Session::FileInfo &FI) {
  146. for (auto &SIKV : FI.SectionInfos)
  147. OS << " Section \"" << SIKV.first() << "\": " << SIKV.second << "\n";
  148. for (auto &GOTKV : FI.GOTEntryInfos)
  149. OS << " GOT \"" << GOTKV.first() << "\": " << GOTKV.second << "\n";
  150. for (auto &StubKV : FI.StubInfos)
  151. OS << " Stub \"" << StubKV.first() << "\": " << StubKV.second << "\n";
  152. return OS;
  153. }
  154. static raw_ostream &
  155. operator<<(raw_ostream &OS, const Session::FileInfoMap &FIM) {
  156. for (auto &FIKV : FIM)
  157. OS << "File \"" << FIKV.first() << "\":\n" << FIKV.second;
  158. return OS;
  159. }
  160. static Error applyHarnessPromotions(Session &S, LinkGraph &G) {
  161. // If this graph is part of the test harness there's nothing to do.
  162. if (S.HarnessFiles.empty() || S.HarnessFiles.count(G.getName()))
  163. return Error::success();
  164. LLVM_DEBUG(dbgs() << "Appling promotions to graph " << G.getName() << "\n");
  165. // If this graph is part of the test then promote any symbols referenced by
  166. // the harness to default scope, remove all symbols that clash with harness
  167. // definitions.
  168. std::vector<Symbol *> DefinitionsToRemove;
  169. for (auto *Sym : G.defined_symbols()) {
  170. if (!Sym->hasName())
  171. continue;
  172. if (Sym->getLinkage() == Linkage::Weak) {
  173. if (!S.CanonicalWeakDefs.count(Sym->getName()) ||
  174. S.CanonicalWeakDefs[Sym->getName()] != G.getName()) {
  175. LLVM_DEBUG({
  176. dbgs() << " Externalizing weak symbol " << Sym->getName() << "\n";
  177. });
  178. DefinitionsToRemove.push_back(Sym);
  179. } else {
  180. LLVM_DEBUG({
  181. dbgs() << " Making weak symbol " << Sym->getName() << " strong\n";
  182. });
  183. if (S.HarnessExternals.count(Sym->getName()))
  184. Sym->setScope(Scope::Default);
  185. else
  186. Sym->setScope(Scope::Hidden);
  187. Sym->setLinkage(Linkage::Strong);
  188. }
  189. } else if (S.HarnessExternals.count(Sym->getName())) {
  190. LLVM_DEBUG(dbgs() << " Promoting " << Sym->getName() << "\n");
  191. Sym->setScope(Scope::Default);
  192. Sym->setLive(true);
  193. continue;
  194. } else if (S.HarnessDefinitions.count(Sym->getName())) {
  195. LLVM_DEBUG(dbgs() << " Externalizing " << Sym->getName() << "\n");
  196. DefinitionsToRemove.push_back(Sym);
  197. }
  198. }
  199. for (auto *Sym : DefinitionsToRemove)
  200. G.makeExternal(*Sym);
  201. return Error::success();
  202. }
  203. static uint64_t computeTotalBlockSizes(LinkGraph &G) {
  204. uint64_t TotalSize = 0;
  205. for (auto *B : G.blocks())
  206. TotalSize += B->getSize();
  207. return TotalSize;
  208. }
  209. static void dumpSectionContents(raw_ostream &OS, LinkGraph &G) {
  210. constexpr JITTargetAddress DumpWidth = 16;
  211. static_assert(isPowerOf2_64(DumpWidth), "DumpWidth must be a power of two");
  212. // Put sections in address order.
  213. std::vector<Section *> Sections;
  214. for (auto &S : G.sections())
  215. Sections.push_back(&S);
  216. llvm::sort(Sections, [](const Section *LHS, const Section *RHS) {
  217. if (llvm::empty(LHS->symbols()) && llvm::empty(RHS->symbols()))
  218. return false;
  219. if (llvm::empty(LHS->symbols()))
  220. return false;
  221. if (llvm::empty(RHS->symbols()))
  222. return true;
  223. SectionRange LHSRange(*LHS);
  224. SectionRange RHSRange(*RHS);
  225. return LHSRange.getStart() < RHSRange.getStart();
  226. });
  227. for (auto *S : Sections) {
  228. OS << S->getName() << " content:";
  229. if (llvm::empty(S->symbols())) {
  230. OS << "\n section empty\n";
  231. continue;
  232. }
  233. // Sort symbols into order, then render.
  234. std::vector<Symbol *> Syms(S->symbols().begin(), S->symbols().end());
  235. llvm::sort(Syms, [](const Symbol *LHS, const Symbol *RHS) {
  236. return LHS->getAddress() < RHS->getAddress();
  237. });
  238. JITTargetAddress NextAddr = Syms.front()->getAddress() & ~(DumpWidth - 1);
  239. for (auto *Sym : Syms) {
  240. bool IsZeroFill = Sym->getBlock().isZeroFill();
  241. JITTargetAddress SymStart = Sym->getAddress();
  242. JITTargetAddress SymSize = Sym->getSize();
  243. JITTargetAddress SymEnd = SymStart + SymSize;
  244. const uint8_t *SymData =
  245. IsZeroFill ? nullptr : Sym->getSymbolContent().bytes_begin();
  246. // Pad any space before the symbol starts.
  247. while (NextAddr != SymStart) {
  248. if (NextAddr % DumpWidth == 0)
  249. OS << formatv("\n{0:x16}:", NextAddr);
  250. OS << " ";
  251. ++NextAddr;
  252. }
  253. // Render the symbol content.
  254. while (NextAddr != SymEnd) {
  255. if (NextAddr % DumpWidth == 0)
  256. OS << formatv("\n{0:x16}:", NextAddr);
  257. if (IsZeroFill)
  258. OS << " 00";
  259. else
  260. OS << formatv(" {0:x-2}", SymData[NextAddr - SymStart]);
  261. ++NextAddr;
  262. }
  263. }
  264. OS << "\n";
  265. }
  266. }
  267. class JITLinkSlabAllocator final : public JITLinkMemoryManager {
  268. public:
  269. static Expected<std::unique_ptr<JITLinkSlabAllocator>>
  270. Create(uint64_t SlabSize) {
  271. Error Err = Error::success();
  272. std::unique_ptr<JITLinkSlabAllocator> Allocator(
  273. new JITLinkSlabAllocator(SlabSize, Err));
  274. if (Err)
  275. return std::move(Err);
  276. return std::move(Allocator);
  277. }
  278. Expected<std::unique_ptr<JITLinkMemoryManager::Allocation>>
  279. allocate(const JITLinkDylib *JD, const SegmentsRequestMap &Request) override {
  280. using AllocationMap = DenseMap<unsigned, sys::MemoryBlock>;
  281. // Local class for allocation.
  282. class IPMMAlloc : public Allocation {
  283. public:
  284. IPMMAlloc(JITLinkSlabAllocator &Parent, AllocationMap SegBlocks)
  285. : Parent(Parent), SegBlocks(std::move(SegBlocks)) {}
  286. MutableArrayRef<char> getWorkingMemory(ProtectionFlags Seg) override {
  287. assert(SegBlocks.count(Seg) && "No allocation for segment");
  288. return {static_cast<char *>(SegBlocks[Seg].base()),
  289. SegBlocks[Seg].allocatedSize()};
  290. }
  291. JITTargetAddress getTargetMemory(ProtectionFlags Seg) override {
  292. assert(SegBlocks.count(Seg) && "No allocation for segment");
  293. return pointerToJITTargetAddress(SegBlocks[Seg].base()) +
  294. Parent.TargetDelta;
  295. }
  296. void finalizeAsync(FinalizeContinuation OnFinalize) override {
  297. OnFinalize(applyProtections());
  298. }
  299. Error deallocate() override {
  300. for (auto &KV : SegBlocks)
  301. if (auto EC = sys::Memory::releaseMappedMemory(KV.second))
  302. return errorCodeToError(EC);
  303. return Error::success();
  304. }
  305. private:
  306. Error applyProtections() {
  307. for (auto &KV : SegBlocks) {
  308. auto &Prot = KV.first;
  309. auto &Block = KV.second;
  310. if (auto EC = sys::Memory::protectMappedMemory(Block, Prot))
  311. return errorCodeToError(EC);
  312. if (Prot & sys::Memory::MF_EXEC)
  313. sys::Memory::InvalidateInstructionCache(Block.base(),
  314. Block.allocatedSize());
  315. }
  316. return Error::success();
  317. }
  318. JITLinkSlabAllocator &Parent;
  319. AllocationMap SegBlocks;
  320. };
  321. AllocationMap Blocks;
  322. for (auto &KV : Request) {
  323. auto &Seg = KV.second;
  324. if (Seg.getAlignment() > PageSize)
  325. return make_error<StringError>("Cannot request higher than page "
  326. "alignment",
  327. inconvertibleErrorCode());
  328. if (PageSize % Seg.getAlignment() != 0)
  329. return make_error<StringError>("Page size is not a multiple of "
  330. "alignment",
  331. inconvertibleErrorCode());
  332. uint64_t ZeroFillStart = Seg.getContentSize();
  333. uint64_t SegmentSize = ZeroFillStart + Seg.getZeroFillSize();
  334. // Round segment size up to page boundary.
  335. SegmentSize = (SegmentSize + PageSize - 1) & ~(PageSize - 1);
  336. // Take segment bytes from the front of the slab.
  337. void *SlabBase = SlabRemaining.base();
  338. uint64_t SlabRemainingSize = SlabRemaining.allocatedSize();
  339. if (SegmentSize > SlabRemainingSize)
  340. return make_error<StringError>("Slab allocator out of memory",
  341. inconvertibleErrorCode());
  342. sys::MemoryBlock SegMem(SlabBase, SegmentSize);
  343. SlabRemaining =
  344. sys::MemoryBlock(reinterpret_cast<char *>(SlabBase) + SegmentSize,
  345. SlabRemainingSize - SegmentSize);
  346. // Zero out the zero-fill memory.
  347. memset(static_cast<char *>(SegMem.base()) + ZeroFillStart, 0,
  348. Seg.getZeroFillSize());
  349. // Record the block for this segment.
  350. Blocks[KV.first] = std::move(SegMem);
  351. }
  352. return std::unique_ptr<InProcessMemoryManager::Allocation>(
  353. new IPMMAlloc(*this, std::move(Blocks)));
  354. }
  355. private:
  356. JITLinkSlabAllocator(uint64_t SlabSize, Error &Err) {
  357. ErrorAsOutParameter _(&Err);
  358. PageSize = sys::Process::getPageSizeEstimate();
  359. if (!isPowerOf2_64(PageSize)) {
  360. Err = make_error<StringError>("Page size is not a power of 2",
  361. inconvertibleErrorCode());
  362. return;
  363. }
  364. // Round slab request up to page size.
  365. SlabSize = (SlabSize + PageSize - 1) & ~(PageSize - 1);
  366. const sys::Memory::ProtectionFlags ReadWrite =
  367. static_cast<sys::Memory::ProtectionFlags>(sys::Memory::MF_READ |
  368. sys::Memory::MF_WRITE);
  369. std::error_code EC;
  370. SlabRemaining =
  371. sys::Memory::allocateMappedMemory(SlabSize, nullptr, ReadWrite, EC);
  372. if (EC) {
  373. Err = errorCodeToError(EC);
  374. return;
  375. }
  376. // Calculate the target address delta to link as-if slab were at
  377. // SlabAddress.
  378. if (SlabAddress != ~0ULL)
  379. TargetDelta =
  380. SlabAddress - pointerToJITTargetAddress(SlabRemaining.base());
  381. }
  382. sys::MemoryBlock SlabRemaining;
  383. uint64_t PageSize = 0;
  384. int64_t TargetDelta = 0;
  385. };
  386. Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {
  387. SizeString = SizeString.trim();
  388. uint64_t Units = 1024;
  389. if (SizeString.endswith_lower("kb"))
  390. SizeString = SizeString.drop_back(2).rtrim();
  391. else if (SizeString.endswith_lower("mb")) {
  392. Units = 1024 * 1024;
  393. SizeString = SizeString.drop_back(2).rtrim();
  394. } else if (SizeString.endswith_lower("gb")) {
  395. Units = 1024 * 1024 * 1024;
  396. SizeString = SizeString.drop_back(2).rtrim();
  397. }
  398. uint64_t SlabSize = 0;
  399. if (SizeString.getAsInteger(10, SlabSize))
  400. return make_error<StringError>("Invalid numeric format for slab size",
  401. inconvertibleErrorCode());
  402. return SlabSize * Units;
  403. }
  404. static std::unique_ptr<JITLinkMemoryManager> createMemoryManager() {
  405. if (!SlabAllocateSizeString.empty()) {
  406. auto SlabSize = ExitOnErr(getSlabAllocSize(SlabAllocateSizeString));
  407. return ExitOnErr(JITLinkSlabAllocator::Create(SlabSize));
  408. }
  409. return std::make_unique<InProcessMemoryManager>();
  410. }
  411. LLVMJITLinkObjectLinkingLayer::LLVMJITLinkObjectLinkingLayer(
  412. Session &S, JITLinkMemoryManager &MemMgr)
  413. : ObjectLinkingLayer(S.ES, MemMgr), S(S) {}
  414. Error LLVMJITLinkObjectLinkingLayer::add(ResourceTrackerSP RT,
  415. std::unique_ptr<MemoryBuffer> O) {
  416. if (S.HarnessFiles.empty() || S.HarnessFiles.count(O->getBufferIdentifier()))
  417. return ObjectLinkingLayer::add(std::move(RT), std::move(O));
  418. // Use getObjectSymbolInfo to compute the init symbol, but ignore
  419. // the symbols field. We'll handle that manually to include promotion.
  420. auto ObjSymInfo =
  421. getObjectSymbolInfo(getExecutionSession(), O->getMemBufferRef());
  422. if (!ObjSymInfo)
  423. return ObjSymInfo.takeError();
  424. auto &InitSymbol = ObjSymInfo->second;
  425. // If creating an object file was going to fail it would have happened above,
  426. // so we can 'cantFail' this.
  427. auto Obj =
  428. cantFail(object::ObjectFile::createObjectFile(O->getMemBufferRef()));
  429. SymbolFlagsMap SymbolFlags;
  430. // The init symbol must be included in the SymbolFlags map if present.
  431. if (InitSymbol)
  432. SymbolFlags[InitSymbol] = JITSymbolFlags::MaterializationSideEffectsOnly;
  433. for (auto &Sym : Obj->symbols()) {
  434. Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
  435. if (!SymFlagsOrErr)
  436. // TODO: Test this error.
  437. return SymFlagsOrErr.takeError();
  438. // Skip symbols not defined in this object file.
  439. if ((*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined))
  440. continue;
  441. auto Name = Sym.getName();
  442. if (!Name)
  443. return Name.takeError();
  444. // Skip symbols that have type SF_File.
  445. if (auto SymType = Sym.getType()) {
  446. if (*SymType == object::SymbolRef::ST_File)
  447. continue;
  448. } else
  449. return SymType.takeError();
  450. auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
  451. if (!SymFlags)
  452. return SymFlags.takeError();
  453. if (SymFlags->isWeak()) {
  454. // If this is a weak symbol that's not defined in the harness then we
  455. // need to either mark it as strong (if this is the first definition
  456. // that we've seen) or discard it.
  457. if (S.HarnessDefinitions.count(*Name) || S.CanonicalWeakDefs.count(*Name))
  458. continue;
  459. S.CanonicalWeakDefs[*Name] = O->getBufferIdentifier();
  460. *SymFlags &= ~JITSymbolFlags::Weak;
  461. if (!S.HarnessExternals.count(*Name))
  462. *SymFlags &= ~JITSymbolFlags::Exported;
  463. } else if (S.HarnessExternals.count(*Name)) {
  464. *SymFlags |= JITSymbolFlags::Exported;
  465. } else if (S.HarnessDefinitions.count(*Name) ||
  466. !(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
  467. continue;
  468. auto InternedName = S.ES.intern(*Name);
  469. SymbolFlags[InternedName] = std::move(*SymFlags);
  470. }
  471. auto MU = std::make_unique<BasicObjectLayerMaterializationUnit>(
  472. *this, std::move(O), std::move(SymbolFlags), std::move(InitSymbol));
  473. auto &JD = RT->getJITDylib();
  474. return JD.define(std::move(MU), std::move(RT));
  475. }
  476. Expected<std::unique_ptr<TargetProcessControl>>
  477. LLVMJITLinkRemoteTargetProcessControl::LaunchExecutor() {
  478. #ifndef LLVM_ON_UNIX
  479. // FIXME: Add support for Windows.
  480. return make_error<StringError>("-" + OutOfProcessExecutor.ArgStr +
  481. " not supported on non-unix platforms",
  482. inconvertibleErrorCode());
  483. #else
  484. shared::registerStringError<LLVMJITLinkChannel>();
  485. constexpr int ReadEnd = 0;
  486. constexpr int WriteEnd = 1;
  487. // Pipe FDs.
  488. int ToExecutor[2];
  489. int FromExecutor[2];
  490. pid_t ChildPID;
  491. // Create pipes to/from the executor..
  492. if (pipe(ToExecutor) != 0 || pipe(FromExecutor) != 0)
  493. return make_error<StringError>("Unable to create pipe for executor",
  494. inconvertibleErrorCode());
  495. ChildPID = fork();
  496. if (ChildPID == 0) {
  497. // In the child...
  498. // Close the parent ends of the pipes
  499. close(ToExecutor[WriteEnd]);
  500. close(FromExecutor[ReadEnd]);
  501. // Execute the child process.
  502. std::unique_ptr<char[]> ExecutorPath, FDSpecifier;
  503. {
  504. ExecutorPath = std::make_unique<char[]>(OutOfProcessExecutor.size() + 1);
  505. strcpy(ExecutorPath.get(), OutOfProcessExecutor.data());
  506. std::string FDSpecifierStr("filedescs=");
  507. FDSpecifierStr += utostr(ToExecutor[ReadEnd]);
  508. FDSpecifierStr += ',';
  509. FDSpecifierStr += utostr(FromExecutor[WriteEnd]);
  510. FDSpecifier = std::make_unique<char[]>(FDSpecifierStr.size() + 1);
  511. strcpy(FDSpecifier.get(), FDSpecifierStr.c_str());
  512. }
  513. char *const Args[] = {ExecutorPath.get(), FDSpecifier.get(), nullptr};
  514. int RC = execvp(ExecutorPath.get(), Args);
  515. if (RC != 0) {
  516. errs() << "unable to launch out-of-process executor \""
  517. << ExecutorPath.get() << "\"\n";
  518. exit(1);
  519. }
  520. }
  521. // else we're the parent...
  522. // Close the child ends of the pipes
  523. close(ToExecutor[ReadEnd]);
  524. close(FromExecutor[WriteEnd]);
  525. // Return an RPC channel connected to our end of the pipes.
  526. auto SSP = std::make_shared<SymbolStringPool>();
  527. auto Channel = std::make_unique<shared::FDRawByteChannel>(
  528. FromExecutor[ReadEnd], ToExecutor[WriteEnd]);
  529. auto Endpoint = std::make_unique<LLVMJITLinkRPCEndpoint>(*Channel, true);
  530. auto ReportError = [](Error Err) {
  531. logAllUnhandledErrors(std::move(Err), errs(), "");
  532. };
  533. Error Err = Error::success();
  534. std::unique_ptr<LLVMJITLinkRemoteTargetProcessControl> RTPC(
  535. new LLVMJITLinkRemoteTargetProcessControl(
  536. std::move(SSP), std::move(Channel), std::move(Endpoint),
  537. std::move(ReportError), Err));
  538. if (Err)
  539. return std::move(Err);
  540. return std::move(RTPC);
  541. #endif
  542. }
  543. Expected<std::unique_ptr<TargetProcessControl>>
  544. LLVMJITLinkRemoteTargetProcessControl::ConnectToExecutor() {
  545. #ifndef LLVM_ON_UNIX
  546. // FIXME: Add TCP support for Windows.
  547. return make_error<StringError>("-" + OutOfProcessExecutorConnect.ArgStr +
  548. " not supported on non-unix platforms",
  549. inconvertibleErrorCode());
  550. #else
  551. shared::registerStringError<LLVMJITLinkChannel>();
  552. StringRef HostNameStr, PortStr;
  553. std::tie(HostNameStr, PortStr) =
  554. StringRef(OutOfProcessExecutorConnect).split(':');
  555. if (HostNameStr.empty())
  556. return make_error<StringError>("host name for -" +
  557. OutOfProcessExecutorConnect.ArgStr +
  558. " can not be empty",
  559. inconvertibleErrorCode());
  560. if (PortStr.empty())
  561. return make_error<StringError>(
  562. "port for -" + OutOfProcessExecutorConnect.ArgStr + " can not be empty",
  563. inconvertibleErrorCode());
  564. std::string HostName = HostNameStr.str();
  565. int Port = 0;
  566. if (PortStr.getAsInteger(10, Port))
  567. return make_error<StringError>("port number " + PortStr +
  568. " is not a valid integer",
  569. inconvertibleErrorCode());
  570. int SockFD = socket(PF_INET, SOCK_STREAM, 0);
  571. hostent *Server = gethostbyname(HostName.c_str());
  572. sockaddr_in ServAddr;
  573. memset(&ServAddr, 0, sizeof(ServAddr));
  574. ServAddr.sin_family = PF_INET;
  575. memmove(&Server->h_addr, &ServAddr.sin_addr.s_addr, Server->h_length);
  576. ServAddr.sin_port = htons(Port);
  577. if (connect(SockFD, reinterpret_cast<sockaddr *>(&ServAddr),
  578. sizeof(ServAddr)) < 0)
  579. return make_error<StringError>("Failed to connect to " + HostName + ":" +
  580. Twine(Port),
  581. inconvertibleErrorCode());
  582. auto SSP = std::make_shared<SymbolStringPool>();
  583. auto Channel = std::make_unique<shared::FDRawByteChannel>(SockFD, SockFD);
  584. auto Endpoint = std::make_unique<LLVMJITLinkRPCEndpoint>(*Channel, true);
  585. auto ReportError = [](Error Err) {
  586. logAllUnhandledErrors(std::move(Err), errs(), "");
  587. };
  588. Error Err = Error::success();
  589. std::unique_ptr<LLVMJITLinkRemoteTargetProcessControl> RTPC(
  590. new LLVMJITLinkRemoteTargetProcessControl(
  591. std::move(SSP), std::move(Channel), std::move(Endpoint),
  592. std::move(ReportError), Err));
  593. if (Err)
  594. return std::move(Err);
  595. return std::move(RTPC);
  596. #endif
  597. }
  598. Error LLVMJITLinkRemoteTargetProcessControl::disconnect() {
  599. std::promise<MSVCPError> P;
  600. auto F = P.get_future();
  601. auto Err = closeConnection([&](Error Err) -> Error {
  602. P.set_value(std::move(Err));
  603. Finished = true;
  604. return Error::success();
  605. });
  606. ListenerThread.join();
  607. return joinErrors(std::move(Err), F.get());
  608. }
  609. class PhonyExternalsGenerator : public DefinitionGenerator {
  610. public:
  611. Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,
  612. JITDylibLookupFlags JDLookupFlags,
  613. const SymbolLookupSet &LookupSet) override {
  614. SymbolMap PhonySymbols;
  615. for (auto &KV : LookupSet)
  616. PhonySymbols[KV.first] = JITEvaluatedSymbol(0, JITSymbolFlags::Exported);
  617. return JD.define(absoluteSymbols(std::move(PhonySymbols)));
  618. }
  619. };
  620. Expected<std::unique_ptr<Session>> Session::Create(Triple TT) {
  621. auto PageSize = sys::Process::getPageSize();
  622. if (!PageSize)
  623. return PageSize.takeError();
  624. /// If -oop-executor is passed then launch the executor.
  625. std::unique_ptr<TargetProcessControl> TPC;
  626. if (OutOfProcessExecutor.getNumOccurrences()) {
  627. if (auto RTPC = LLVMJITLinkRemoteTargetProcessControl::LaunchExecutor())
  628. TPC = std::move(*RTPC);
  629. else
  630. return RTPC.takeError();
  631. } else if (OutOfProcessExecutorConnect.getNumOccurrences()) {
  632. if (auto RTPC = LLVMJITLinkRemoteTargetProcessControl::ConnectToExecutor())
  633. TPC = std::move(*RTPC);
  634. else
  635. return RTPC.takeError();
  636. } else
  637. TPC = std::make_unique<SelfTargetProcessControl>(
  638. std::make_shared<SymbolStringPool>(), std::move(TT), *PageSize,
  639. createMemoryManager());
  640. Error Err = Error::success();
  641. std::unique_ptr<Session> S(new Session(std::move(TPC), Err));
  642. if (Err)
  643. return std::move(Err);
  644. return std::move(S);
  645. }
  646. Session::~Session() {
  647. if (auto Err = ES.endSession())
  648. ES.reportError(std::move(Err));
  649. }
  650. // FIXME: Move to createJITDylib if/when we start using Platform support in
  651. // llvm-jitlink.
  652. Session::Session(std::unique_ptr<TargetProcessControl> TPC, Error &Err)
  653. : TPC(std::move(TPC)), ObjLayer(*this, this->TPC->getMemMgr()) {
  654. /// Local ObjectLinkingLayer::Plugin class to forward modifyPassConfig to the
  655. /// Session.
  656. class JITLinkSessionPlugin : public ObjectLinkingLayer::Plugin {
  657. public:
  658. JITLinkSessionPlugin(Session &S) : S(S) {}
  659. void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT,
  660. PassConfiguration &PassConfig) override {
  661. S.modifyPassConfig(TT, PassConfig);
  662. }
  663. Error notifyFailed(MaterializationResponsibility &MR) override {
  664. return Error::success();
  665. }
  666. Error notifyRemovingResources(ResourceKey K) override {
  667. return Error::success();
  668. }
  669. void notifyTransferringResources(ResourceKey DstKey,
  670. ResourceKey SrcKey) override {}
  671. private:
  672. Session &S;
  673. };
  674. ErrorAsOutParameter _(&Err);
  675. if (auto MainJDOrErr = ES.createJITDylib("main"))
  676. MainJD = &*MainJDOrErr;
  677. else {
  678. Err = MainJDOrErr.takeError();
  679. return;
  680. }
  681. if (!NoExec && !this->TPC->getTargetTriple().isOSWindows())
  682. ObjLayer.addPlugin(std::make_unique<EHFrameRegistrationPlugin>(
  683. ES, ExitOnErr(TPCEHFrameRegistrar::Create(*this->TPC))));
  684. ObjLayer.addPlugin(std::make_unique<JITLinkSessionPlugin>(*this));
  685. // Process any harness files.
  686. for (auto &HarnessFile : TestHarnesses) {
  687. HarnessFiles.insert(HarnessFile);
  688. auto ObjBuffer =
  689. ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(HarnessFile)));
  690. auto ObjSymbolInfo =
  691. ExitOnErr(getObjectSymbolInfo(ES, ObjBuffer->getMemBufferRef()));
  692. for (auto &KV : ObjSymbolInfo.first)
  693. HarnessDefinitions.insert(*KV.first);
  694. auto Obj = ExitOnErr(
  695. object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()));
  696. for (auto &Sym : Obj->symbols()) {
  697. uint32_t SymFlags = ExitOnErr(Sym.getFlags());
  698. auto Name = ExitOnErr(Sym.getName());
  699. if (Name.empty())
  700. continue;
  701. if (SymFlags & object::BasicSymbolRef::SF_Undefined)
  702. HarnessExternals.insert(Name);
  703. }
  704. }
  705. // If a name is defined by some harness file then it's a definition, not an
  706. // external.
  707. for (auto &DefName : HarnessDefinitions)
  708. HarnessExternals.erase(DefName.getKey());
  709. }
  710. void Session::dumpSessionInfo(raw_ostream &OS) {
  711. OS << "Registered addresses:\n" << SymbolInfos << FileInfos;
  712. }
  713. void Session::modifyPassConfig(const Triple &TT,
  714. PassConfiguration &PassConfig) {
  715. if (!CheckFiles.empty())
  716. PassConfig.PostFixupPasses.push_back([this](LinkGraph &G) {
  717. if (TPC->getTargetTriple().getObjectFormat() == Triple::ELF)
  718. return registerELFGraphInfo(*this, G);
  719. if (TPC->getTargetTriple().getObjectFormat() == Triple::MachO)
  720. return registerMachOGraphInfo(*this, G);
  721. return make_error<StringError>("Unsupported object format for GOT/stub "
  722. "registration",
  723. inconvertibleErrorCode());
  724. });
  725. if (ShowLinkGraph)
  726. PassConfig.PostFixupPasses.push_back([](LinkGraph &G) -> Error {
  727. outs() << "Link graph \"" << G.getName() << "\" post-fixup:\n";
  728. G.dump(outs());
  729. return Error::success();
  730. });
  731. PassConfig.PrePrunePasses.push_back(
  732. [this](LinkGraph &G) { return applyHarnessPromotions(*this, G); });
  733. if (ShowSizes) {
  734. PassConfig.PrePrunePasses.push_back([this](LinkGraph &G) -> Error {
  735. SizeBeforePruning += computeTotalBlockSizes(G);
  736. return Error::success();
  737. });
  738. PassConfig.PostFixupPasses.push_back([this](LinkGraph &G) -> Error {
  739. SizeAfterFixups += computeTotalBlockSizes(G);
  740. return Error::success();
  741. });
  742. }
  743. if (ShowRelocatedSectionContents)
  744. PassConfig.PostFixupPasses.push_back([](LinkGraph &G) -> Error {
  745. outs() << "Relocated section contents for " << G.getName() << ":\n";
  746. dumpSectionContents(outs(), G);
  747. return Error::success();
  748. });
  749. }
  750. Expected<Session::FileInfo &> Session::findFileInfo(StringRef FileName) {
  751. auto FileInfoItr = FileInfos.find(FileName);
  752. if (FileInfoItr == FileInfos.end())
  753. return make_error<StringError>("file \"" + FileName + "\" not recognized",
  754. inconvertibleErrorCode());
  755. return FileInfoItr->second;
  756. }
  757. Expected<Session::MemoryRegionInfo &>
  758. Session::findSectionInfo(StringRef FileName, StringRef SectionName) {
  759. auto FI = findFileInfo(FileName);
  760. if (!FI)
  761. return FI.takeError();
  762. auto SecInfoItr = FI->SectionInfos.find(SectionName);
  763. if (SecInfoItr == FI->SectionInfos.end())
  764. return make_error<StringError>("no section \"" + SectionName +
  765. "\" registered for file \"" + FileName +
  766. "\"",
  767. inconvertibleErrorCode());
  768. return SecInfoItr->second;
  769. }
  770. Expected<Session::MemoryRegionInfo &>
  771. Session::findStubInfo(StringRef FileName, StringRef TargetName) {
  772. auto FI = findFileInfo(FileName);
  773. if (!FI)
  774. return FI.takeError();
  775. auto StubInfoItr = FI->StubInfos.find(TargetName);
  776. if (StubInfoItr == FI->StubInfos.end())
  777. return make_error<StringError>("no stub for \"" + TargetName +
  778. "\" registered for file \"" + FileName +
  779. "\"",
  780. inconvertibleErrorCode());
  781. return StubInfoItr->second;
  782. }
  783. Expected<Session::MemoryRegionInfo &>
  784. Session::findGOTEntryInfo(StringRef FileName, StringRef TargetName) {
  785. auto FI = findFileInfo(FileName);
  786. if (!FI)
  787. return FI.takeError();
  788. auto GOTInfoItr = FI->GOTEntryInfos.find(TargetName);
  789. if (GOTInfoItr == FI->GOTEntryInfos.end())
  790. return make_error<StringError>("no GOT entry for \"" + TargetName +
  791. "\" registered for file \"" + FileName +
  792. "\"",
  793. inconvertibleErrorCode());
  794. return GOTInfoItr->second;
  795. }
  796. bool Session::isSymbolRegistered(StringRef SymbolName) {
  797. return SymbolInfos.count(SymbolName);
  798. }
  799. Expected<Session::MemoryRegionInfo &>
  800. Session::findSymbolInfo(StringRef SymbolName, Twine ErrorMsgStem) {
  801. auto SymInfoItr = SymbolInfos.find(SymbolName);
  802. if (SymInfoItr == SymbolInfos.end())
  803. return make_error<StringError>(ErrorMsgStem + ": symbol " + SymbolName +
  804. " not found",
  805. inconvertibleErrorCode());
  806. return SymInfoItr->second;
  807. }
  808. } // end namespace llvm
  809. static Triple getFirstFileTriple() {
  810. static Triple FirstTT = []() {
  811. assert(!InputFiles.empty() && "InputFiles can not be empty");
  812. auto ObjBuffer =
  813. ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(InputFiles.front())));
  814. auto Obj = ExitOnErr(
  815. object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()));
  816. return Obj->makeTriple();
  817. }();
  818. return FirstTT;
  819. }
  820. static Error sanitizeArguments(const Triple &TT, const char *ArgV0) {
  821. // Set the entry point name if not specified.
  822. if (EntryPointName.empty()) {
  823. if (TT.getObjectFormat() == Triple::MachO)
  824. EntryPointName = "_main";
  825. else
  826. EntryPointName = "main";
  827. }
  828. // -noexec and --args should not be used together.
  829. if (NoExec && !InputArgv.empty())
  830. outs() << "Warning: --args passed to -noexec run will be ignored.\n";
  831. // If -slab-address is passed, require -slab-allocate and -noexec
  832. if (SlabAddress != ~0ULL) {
  833. if (SlabAllocateSizeString == "" || !NoExec)
  834. return make_error<StringError>(
  835. "-slab-address requires -slab-allocate and -noexec",
  836. inconvertibleErrorCode());
  837. }
  838. // Only one of -oop-executor and -oop-executor-connect can be used.
  839. if (!!OutOfProcessExecutor.getNumOccurrences() &&
  840. !!OutOfProcessExecutorConnect.getNumOccurrences())
  841. return make_error<StringError>(
  842. "Only one of -" + OutOfProcessExecutor.ArgStr + " and -" +
  843. OutOfProcessExecutorConnect.ArgStr + " can be specified",
  844. inconvertibleErrorCode());
  845. // If -oop-executor was used but no value was specified then use a sensible
  846. // default.
  847. if (!!OutOfProcessExecutor.getNumOccurrences() &&
  848. OutOfProcessExecutor.empty()) {
  849. SmallString<256> OOPExecutorPath(sys::fs::getMainExecutable(
  850. ArgV0, reinterpret_cast<void *>(&sanitizeArguments)));
  851. sys::path::remove_filename(OOPExecutorPath);
  852. if (OOPExecutorPath.back() != '/')
  853. OOPExecutorPath += '/';
  854. OOPExecutorPath += "llvm-jitlink-executor";
  855. OutOfProcessExecutor = OOPExecutorPath.str().str();
  856. }
  857. return Error::success();
  858. }
  859. static Error loadProcessSymbols(Session &S) {
  860. auto FilterMainEntryPoint =
  861. [EPName = S.ES.intern(EntryPointName)](SymbolStringPtr Name) {
  862. return Name != EPName;
  863. };
  864. S.MainJD->addGenerator(
  865. ExitOnErr(orc::TPCDynamicLibrarySearchGenerator::GetForTargetProcess(
  866. *S.TPC, std::move(FilterMainEntryPoint))));
  867. return Error::success();
  868. }
  869. static Error loadDylibs(Session &S) {
  870. for (const auto &Dylib : Dylibs) {
  871. auto G = orc::TPCDynamicLibrarySearchGenerator::Load(*S.TPC, Dylib.c_str());
  872. if (!G)
  873. return G.takeError();
  874. S.MainJD->addGenerator(std::move(*G));
  875. }
  876. return Error::success();
  877. }
  878. static void addPhonyExternalsGenerator(Session &S) {
  879. S.MainJD->addGenerator(std::make_unique<PhonyExternalsGenerator>());
  880. }
  881. static Error loadObjects(Session &S) {
  882. std::map<unsigned, JITDylib *> IdxToJLD;
  883. // First, set up JITDylibs.
  884. LLVM_DEBUG(dbgs() << "Creating JITDylibs...\n");
  885. {
  886. // Create a "main" JITLinkDylib.
  887. IdxToJLD[0] = S.MainJD;
  888. S.JDSearchOrder.push_back(S.MainJD);
  889. LLVM_DEBUG(dbgs() << " 0: " << S.MainJD->getName() << "\n");
  890. // Add any extra JITLinkDylibs from the command line.
  891. std::string JDNamePrefix("lib");
  892. for (auto JLDItr = JITLinkDylibs.begin(), JLDEnd = JITLinkDylibs.end();
  893. JLDItr != JLDEnd; ++JLDItr) {
  894. auto JD = S.ES.createJITDylib(JDNamePrefix + *JLDItr);
  895. if (!JD)
  896. return JD.takeError();
  897. unsigned JDIdx =
  898. JITLinkDylibs.getPosition(JLDItr - JITLinkDylibs.begin());
  899. IdxToJLD[JDIdx] = &*JD;
  900. S.JDSearchOrder.push_back(&*JD);
  901. LLVM_DEBUG(dbgs() << " " << JDIdx << ": " << JD->getName() << "\n");
  902. }
  903. // Set every dylib to link against every other, in command line order.
  904. for (auto *JD : S.JDSearchOrder) {
  905. auto LookupFlags = JITDylibLookupFlags::MatchExportedSymbolsOnly;
  906. JITDylibSearchOrder LinkOrder;
  907. for (auto *JD2 : S.JDSearchOrder) {
  908. if (JD2 == JD)
  909. continue;
  910. LinkOrder.push_back(std::make_pair(JD2, LookupFlags));
  911. }
  912. JD->setLinkOrder(std::move(LinkOrder));
  913. }
  914. }
  915. LLVM_DEBUG(dbgs() << "Adding test harness objects...\n");
  916. for (auto HarnessFile : TestHarnesses) {
  917. LLVM_DEBUG(dbgs() << " " << HarnessFile << "\n");
  918. auto ObjBuffer =
  919. ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(HarnessFile)));
  920. ExitOnErr(S.ObjLayer.add(*S.MainJD, std::move(ObjBuffer)));
  921. }
  922. // Load each object into the corresponding JITDylib..
  923. LLVM_DEBUG(dbgs() << "Adding objects...\n");
  924. for (auto InputFileItr = InputFiles.begin(), InputFileEnd = InputFiles.end();
  925. InputFileItr != InputFileEnd; ++InputFileItr) {
  926. unsigned InputFileArgIdx =
  927. InputFiles.getPosition(InputFileItr - InputFiles.begin());
  928. const std::string &InputFile = *InputFileItr;
  929. auto &JD = *std::prev(IdxToJLD.lower_bound(InputFileArgIdx))->second;
  930. LLVM_DEBUG(dbgs() << " " << InputFileArgIdx << ": \"" << InputFile
  931. << "\" to " << JD.getName() << "\n";);
  932. auto ObjBuffer =
  933. ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(InputFile)));
  934. auto Magic = identify_magic(ObjBuffer->getBuffer());
  935. if (Magic == file_magic::archive ||
  936. Magic == file_magic::macho_universal_binary)
  937. JD.addGenerator(ExitOnErr(StaticLibraryDefinitionGenerator::Load(
  938. S.ObjLayer, InputFile.c_str(), S.TPC->getTargetTriple())));
  939. else
  940. ExitOnErr(S.ObjLayer.add(JD, std::move(ObjBuffer)));
  941. }
  942. // Define absolute symbols.
  943. LLVM_DEBUG(dbgs() << "Defining absolute symbols...\n");
  944. for (auto AbsDefItr = AbsoluteDefs.begin(), AbsDefEnd = AbsoluteDefs.end();
  945. AbsDefItr != AbsDefEnd; ++AbsDefItr) {
  946. unsigned AbsDefArgIdx =
  947. AbsoluteDefs.getPosition(AbsDefItr - AbsoluteDefs.begin());
  948. auto &JD = *std::prev(IdxToJLD.lower_bound(AbsDefArgIdx))->second;
  949. StringRef AbsDefStmt = *AbsDefItr;
  950. size_t EqIdx = AbsDefStmt.find_first_of('=');
  951. if (EqIdx == StringRef::npos)
  952. return make_error<StringError>("Invalid absolute define \"" + AbsDefStmt +
  953. "\". Syntax: <name>=<addr>",
  954. inconvertibleErrorCode());
  955. StringRef Name = AbsDefStmt.substr(0, EqIdx).trim();
  956. StringRef AddrStr = AbsDefStmt.substr(EqIdx + 1).trim();
  957. uint64_t Addr;
  958. if (AddrStr.getAsInteger(0, Addr))
  959. return make_error<StringError>("Invalid address expression \"" + AddrStr +
  960. "\" in absolute define \"" + AbsDefStmt +
  961. "\"",
  962. inconvertibleErrorCode());
  963. JITEvaluatedSymbol AbsDef(Addr, JITSymbolFlags::Exported);
  964. if (auto Err = JD.define(absoluteSymbols({{S.ES.intern(Name), AbsDef}})))
  965. return Err;
  966. // Register the absolute symbol with the session symbol infos.
  967. S.SymbolInfos[Name] = { StringRef(), Addr };
  968. }
  969. LLVM_DEBUG({
  970. dbgs() << "Dylib search order is [ ";
  971. for (auto *JD : S.JDSearchOrder)
  972. dbgs() << JD->getName() << " ";
  973. dbgs() << "]\n";
  974. });
  975. return Error::success();
  976. }
  977. static Error runChecks(Session &S) {
  978. auto TripleName = S.TPC->getTargetTriple().str();
  979. std::string ErrorStr;
  980. const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, ErrorStr);
  981. if (!TheTarget)
  982. ExitOnErr(make_error<StringError>("Error accessing target '" + TripleName +
  983. "': " + ErrorStr,
  984. inconvertibleErrorCode()));
  985. std::unique_ptr<MCSubtargetInfo> STI(
  986. TheTarget->createMCSubtargetInfo(TripleName, "", ""));
  987. if (!STI)
  988. ExitOnErr(
  989. make_error<StringError>("Unable to create subtarget for " + TripleName,
  990. inconvertibleErrorCode()));
  991. std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
  992. if (!MRI)
  993. ExitOnErr(make_error<StringError>("Unable to create target register info "
  994. "for " +
  995. TripleName,
  996. inconvertibleErrorCode()));
  997. MCTargetOptions MCOptions;
  998. std::unique_ptr<MCAsmInfo> MAI(
  999. TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
  1000. if (!MAI)
  1001. ExitOnErr(make_error<StringError>("Unable to create target asm info " +
  1002. TripleName,
  1003. inconvertibleErrorCode()));
  1004. MCContext Ctx(MAI.get(), MRI.get(), nullptr);
  1005. std::unique_ptr<MCDisassembler> Disassembler(
  1006. TheTarget->createMCDisassembler(*STI, Ctx));
  1007. if (!Disassembler)
  1008. ExitOnErr(make_error<StringError>("Unable to create disassembler for " +
  1009. TripleName,
  1010. inconvertibleErrorCode()));
  1011. std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
  1012. std::unique_ptr<MCInstPrinter> InstPrinter(
  1013. TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
  1014. auto IsSymbolValid = [&S](StringRef Symbol) {
  1015. return S.isSymbolRegistered(Symbol);
  1016. };
  1017. auto GetSymbolInfo = [&S](StringRef Symbol) {
  1018. return S.findSymbolInfo(Symbol, "Can not get symbol info");
  1019. };
  1020. auto GetSectionInfo = [&S](StringRef FileName, StringRef SectionName) {
  1021. return S.findSectionInfo(FileName, SectionName);
  1022. };
  1023. auto GetStubInfo = [&S](StringRef FileName, StringRef SectionName) {
  1024. return S.findStubInfo(FileName, SectionName);
  1025. };
  1026. auto GetGOTInfo = [&S](StringRef FileName, StringRef SectionName) {
  1027. return S.findGOTEntryInfo(FileName, SectionName);
  1028. };
  1029. RuntimeDyldChecker Checker(
  1030. IsSymbolValid, GetSymbolInfo, GetSectionInfo, GetStubInfo, GetGOTInfo,
  1031. S.TPC->getTargetTriple().isLittleEndian() ? support::little
  1032. : support::big,
  1033. Disassembler.get(), InstPrinter.get(), dbgs());
  1034. std::string CheckLineStart = "# " + CheckName + ":";
  1035. for (auto &CheckFile : CheckFiles) {
  1036. auto CheckerFileBuf =
  1037. ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(CheckFile)));
  1038. if (!Checker.checkAllRulesInBuffer(CheckLineStart, &*CheckerFileBuf))
  1039. ExitOnErr(make_error<StringError>(
  1040. "Some checks in " + CheckFile + " failed", inconvertibleErrorCode()));
  1041. }
  1042. return Error::success();
  1043. }
  1044. static void dumpSessionStats(Session &S) {
  1045. if (ShowSizes)
  1046. outs() << "Total size of all blocks before pruning: " << S.SizeBeforePruning
  1047. << "\nTotal size of all blocks after fixups: " << S.SizeAfterFixups
  1048. << "\n";
  1049. }
  1050. static Expected<JITEvaluatedSymbol> getMainEntryPoint(Session &S) {
  1051. return S.ES.lookup(S.JDSearchOrder, EntryPointName);
  1052. }
  1053. namespace {
  1054. struct JITLinkTimers {
  1055. TimerGroup JITLinkTG{"llvm-jitlink timers", "timers for llvm-jitlink phases"};
  1056. Timer LoadObjectsTimer{"load", "time to load/add object files", JITLinkTG};
  1057. Timer LinkTimer{"link", "time to link object files", JITLinkTG};
  1058. Timer RunTimer{"run", "time to execute jitlink'd code", JITLinkTG};
  1059. };
  1060. } // namespace
  1061. int main(int argc, char *argv[]) {
  1062. InitLLVM X(argc, argv);
  1063. InitializeAllTargetInfos();
  1064. InitializeAllTargetMCs();
  1065. InitializeAllDisassemblers();
  1066. cl::ParseCommandLineOptions(argc, argv, "llvm jitlink tool");
  1067. ExitOnErr.setBanner(std::string(argv[0]) + ": ");
  1068. /// If timers are enabled, create a JITLinkTimers instance.
  1069. std::unique_ptr<JITLinkTimers> Timers =
  1070. ShowTimes ? std::make_unique<JITLinkTimers>() : nullptr;
  1071. ExitOnErr(sanitizeArguments(getFirstFileTriple(), argv[0]));
  1072. auto S = ExitOnErr(Session::Create(getFirstFileTriple()));
  1073. {
  1074. TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr);
  1075. ExitOnErr(loadObjects(*S));
  1076. }
  1077. if (!NoProcessSymbols)
  1078. ExitOnErr(loadProcessSymbols(*S));
  1079. ExitOnErr(loadDylibs(*S));
  1080. if (PhonyExternals)
  1081. addPhonyExternalsGenerator(*S);
  1082. if (ShowInitialExecutionSessionState)
  1083. S->ES.dump(outs());
  1084. JITEvaluatedSymbol EntryPoint = 0;
  1085. {
  1086. TimeRegion TR(Timers ? &Timers->LinkTimer : nullptr);
  1087. EntryPoint = ExitOnErr(getMainEntryPoint(*S));
  1088. }
  1089. if (ShowAddrs)
  1090. S->dumpSessionInfo(outs());
  1091. ExitOnErr(runChecks(*S));
  1092. dumpSessionStats(*S);
  1093. if (NoExec)
  1094. return 0;
  1095. int Result = 0;
  1096. {
  1097. TimeRegion TR(Timers ? &Timers->RunTimer : nullptr);
  1098. Result = ExitOnErr(S->TPC->runAsMain(EntryPoint.getAddress(), InputArgv));
  1099. }
  1100. ExitOnErr(S->ES.endSession());
  1101. ExitOnErr(S->TPC->disconnect());
  1102. return Result;
  1103. }