llvm-jitlink.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //===---- llvm-jitlink.h - Session and format-specific decls ----*- C++ -*-===//
  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. // llvm-jitlink Session class and tool utilities.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
  13. #define LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
  14. #include "llvm/ADT/Optional.h"
  15. #include "llvm/ADT/StringSet.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/ExecutionEngine/Orc/Core.h"
  18. #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
  19. #include "llvm/ExecutionEngine/Orc/OrcRPCTargetProcessControl.h"
  20. #include "llvm/ExecutionEngine/Orc/Shared/FDRawByteChannel.h"
  21. #include "llvm/ExecutionEngine/Orc/Shared/RPCUtils.h"
  22. #include "llvm/ExecutionEngine/Orc/TargetProcessControl.h"
  23. #include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
  24. #include "llvm/Support/Error.h"
  25. #include "llvm/Support/Regex.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include <vector>
  28. namespace llvm {
  29. struct Session;
  30. /// ObjectLinkingLayer with additional support for symbol promotion.
  31. class LLVMJITLinkObjectLinkingLayer : public orc::ObjectLinkingLayer {
  32. public:
  33. using orc::ObjectLinkingLayer::add;
  34. LLVMJITLinkObjectLinkingLayer(Session &S,
  35. jitlink::JITLinkMemoryManager &MemMgr);
  36. Error add(orc::ResourceTrackerSP RT,
  37. std::unique_ptr<MemoryBuffer> O) override;
  38. private:
  39. Session &S;
  40. };
  41. using LLVMJITLinkChannel = orc::shared::FDRawByteChannel;
  42. using LLVMJITLinkRPCEndpoint =
  43. orc::shared::MultiThreadedRPCEndpoint<LLVMJITLinkChannel>;
  44. using LLVMJITLinkRemoteMemoryManager =
  45. orc::OrcRPCTPCJITLinkMemoryManager<LLVMJITLinkRPCEndpoint>;
  46. using LLVMJITLinkRemoteMemoryAccess =
  47. orc::OrcRPCTPCMemoryAccess<LLVMJITLinkRPCEndpoint>;
  48. class LLVMJITLinkRemoteTargetProcessControl
  49. : public orc::OrcRPCTargetProcessControlBase<LLVMJITLinkRPCEndpoint> {
  50. public:
  51. using BaseT = orc::OrcRPCTargetProcessControlBase<LLVMJITLinkRPCEndpoint>;
  52. static Expected<std::unique_ptr<TargetProcessControl>> LaunchExecutor();
  53. static Expected<std::unique_ptr<TargetProcessControl>> ConnectToExecutor();
  54. Error disconnect() override;
  55. private:
  56. using LLVMJITLinkRemoteMemoryAccess =
  57. orc::OrcRPCTPCMemoryAccess<LLVMJITLinkRemoteTargetProcessControl>;
  58. using LLVMJITLinkRemoteMemoryManager =
  59. orc::OrcRPCTPCJITLinkMemoryManager<LLVMJITLinkRemoteTargetProcessControl>;
  60. LLVMJITLinkRemoteTargetProcessControl(
  61. std::shared_ptr<orc::SymbolStringPool> SSP,
  62. std::unique_ptr<LLVMJITLinkChannel> Channel,
  63. std::unique_ptr<LLVMJITLinkRPCEndpoint> Endpoint,
  64. ErrorReporter ReportError, Error &Err)
  65. : BaseT(std::move(SSP), *Endpoint, std::move(ReportError)),
  66. Channel(std::move(Channel)), Endpoint(std::move(Endpoint)) {
  67. ErrorAsOutParameter _(&Err);
  68. ListenerThread = std::thread([&]() {
  69. while (!Finished) {
  70. if (auto Err = this->Endpoint->handleOne()) {
  71. reportError(std::move(Err));
  72. return;
  73. }
  74. }
  75. });
  76. if (auto Err2 = initializeORCRPCTPCBase()) {
  77. Err = joinErrors(std::move(Err2), disconnect());
  78. return;
  79. }
  80. OwnedMemAccess = std::make_unique<LLVMJITLinkRemoteMemoryAccess>(*this);
  81. MemAccess = OwnedMemAccess.get();
  82. OwnedMemMgr = std::make_unique<LLVMJITLinkRemoteMemoryManager>(*this);
  83. MemMgr = OwnedMemMgr.get();
  84. }
  85. std::unique_ptr<LLVMJITLinkChannel> Channel;
  86. std::unique_ptr<LLVMJITLinkRPCEndpoint> Endpoint;
  87. std::unique_ptr<TargetProcessControl::MemoryAccess> OwnedMemAccess;
  88. std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
  89. std::atomic<bool> Finished{false};
  90. std::thread ListenerThread;
  91. };
  92. struct Session {
  93. std::unique_ptr<orc::TargetProcessControl> TPC;
  94. orc::ExecutionSession ES;
  95. orc::JITDylib *MainJD;
  96. LLVMJITLinkObjectLinkingLayer ObjLayer;
  97. std::vector<orc::JITDylib *> JDSearchOrder;
  98. ~Session();
  99. static Expected<std::unique_ptr<Session>> Create(Triple TT);
  100. void dumpSessionInfo(raw_ostream &OS);
  101. void modifyPassConfig(const Triple &FTT,
  102. jitlink::PassConfiguration &PassConfig);
  103. using MemoryRegionInfo = RuntimeDyldChecker::MemoryRegionInfo;
  104. struct FileInfo {
  105. StringMap<MemoryRegionInfo> SectionInfos;
  106. StringMap<MemoryRegionInfo> StubInfos;
  107. StringMap<MemoryRegionInfo> GOTEntryInfos;
  108. };
  109. using SymbolInfoMap = StringMap<MemoryRegionInfo>;
  110. using FileInfoMap = StringMap<FileInfo>;
  111. Expected<FileInfo &> findFileInfo(StringRef FileName);
  112. Expected<MemoryRegionInfo &> findSectionInfo(StringRef FileName,
  113. StringRef SectionName);
  114. Expected<MemoryRegionInfo &> findStubInfo(StringRef FileName,
  115. StringRef TargetName);
  116. Expected<MemoryRegionInfo &> findGOTEntryInfo(StringRef FileName,
  117. StringRef TargetName);
  118. bool isSymbolRegistered(StringRef Name);
  119. Expected<MemoryRegionInfo &> findSymbolInfo(StringRef SymbolName,
  120. Twine ErrorMsgStem);
  121. SymbolInfoMap SymbolInfos;
  122. FileInfoMap FileInfos;
  123. uint64_t SizeBeforePruning = 0;
  124. uint64_t SizeAfterFixups = 0;
  125. StringSet<> HarnessFiles;
  126. StringSet<> HarnessExternals;
  127. StringSet<> HarnessDefinitions;
  128. DenseMap<StringRef, StringRef> CanonicalWeakDefs;
  129. private:
  130. Session(std::unique_ptr<orc::TargetProcessControl> TPC, Error &Err);
  131. };
  132. /// Record symbols, GOT entries, stubs, and sections for ELF file.
  133. Error registerELFGraphInfo(Session &S, jitlink::LinkGraph &G);
  134. /// Record symbols, GOT entries, stubs, and sections for MachO file.
  135. Error registerMachOGraphInfo(Session &S, jitlink::LinkGraph &G);
  136. } // end namespace llvm
  137. #endif // LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H