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
13#ifndef LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
14#define LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
15
16#include "llvm/ADT/StringSet.h"
17#include "llvm/ExecutionEngine/Orc/COFF.h"
18#include "llvm/ExecutionEngine/Orc/Core.h"
19#include "llvm/ExecutionEngine/Orc/DylibManager.h"
20#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
21#include "llvm/ExecutionEngine/Orc/LazyObjectLinkingLayer.h"
22#include "llvm/ExecutionEngine/Orc/LazyReexports.h"
23#include "llvm/ExecutionEngine/Orc/MemoryAccess.h"
24#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
25#include "llvm/ExecutionEngine/Orc/RedirectionManager.h"
26#include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
27#include "llvm/ExecutionEngine/Orc/WaitingOnGraphOpReplay.h"
28#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
29#include "llvm/Support/Error.h"
30#include "llvm/Support/Regex.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/TargetParser/SubtargetFeature.h"
33#include "llvm/TargetParser/Triple.h"
34
35namespace llvm {
36
37struct Session {
38
39 class WaitingOnGraphOpRecorder
40 : public orc::detail::WaitingOnGraphOpStreamRecorder<
41 orc::JITDylib *, orc::NonOwningSymbolStringPtr> {
42 public:
43 static Expected<std::unique_ptr<WaitingOnGraphOpRecorder>>
44 Create(StringRef Path) {
45 std::error_code EC;
46 std::unique_ptr<WaitingOnGraphOpRecorder> Instance(
47 new WaitingOnGraphOpRecorder(Path, EC));
48
49 if (EC)
50 return createFileError(F: Path, EC);
51 return std::move(Instance);
52 }
53
54 private:
55 WaitingOnGraphOpRecorder(StringRef Path, std::error_code EC)
56 : orc::detail::WaitingOnGraphOpStreamRecorder<
57 orc::JITDylib *, orc::NonOwningSymbolStringPtr>(OutStream),
58 OutStream(Path, EC) {}
59 raw_fd_ostream OutStream;
60 };
61
62 struct LazyLinkingSupport {
63 LazyLinkingSupport(
64 std::unique_ptr<orc::MemoryAccess> MemAccess,
65 std::unique_ptr<orc::RedirectableSymbolManager> RSMgr,
66 std::shared_ptr<orc::SimpleLazyReexportsSpeculator> Speculator,
67 std::unique_ptr<orc::LazyReexportsManager> LRMgr,
68 orc::ObjectLinkingLayer &ObjLinkingLayer)
69 : MemAccess(std::move(MemAccess)), RSMgr(std::move(RSMgr)),
70 Speculator(std::move(Speculator)), LRMgr(std::move(LRMgr)),
71 LazyObjLinkingLayer(ObjLinkingLayer, *this->LRMgr) {}
72
73 std::unique_ptr<orc::MemoryAccess> MemAccess;
74 std::unique_ptr<orc::RedirectableSymbolManager> RSMgr;
75 std::shared_ptr<orc::SimpleLazyReexportsSpeculator> Speculator;
76 std::unique_ptr<orc::LazyReexportsManager> LRMgr;
77 orc::LazyObjectLinkingLayer LazyObjLinkingLayer;
78 };
79
80 orc::ExecutionSession ES;
81 std::unique_ptr<jitlink::JITLinkMemoryManager> MemoryMgr;
82 std::unique_ptr<orc::DylibManager> DylibMgr;
83 orc::JITDylib *MainJD = nullptr;
84 orc::JITDylib *ProcessSymsJD = nullptr;
85 orc::JITDylib *PlatformJD = nullptr;
86 std::unique_ptr<orc::ObjectLinkingLayer> ObjLayer;
87 std::unique_ptr<LazyLinkingSupport> LazyLinking;
88 orc::JITDylibSearchOrder JDSearchOrder;
89 SubtargetFeatures Features;
90 std::vector<std::pair<std::string, orc::SymbolStringPtr>> LazyFnExecOrder;
91
92 ~Session();
93
94 static Expected<std::unique_ptr<Session>> Create(Triple TT,
95 SubtargetFeatures Features);
96 void dumpSessionInfo(raw_ostream &OS);
97 void modifyPassConfig(jitlink::LinkGraph &G,
98 jitlink::PassConfiguration &PassConfig);
99
100 /// For -check: wait for all files that are referenced (transitively) from
101 /// the entry point *file* to be linked. (ORC's usual dependence tracking is
102 /// to fine-grained here: a lookup of the main symbol will return as soon as
103 /// all reachable symbols have been linked, but testcases may want to
104 /// inspect side-effects in unreachable symbols)..
105 void waitForFilesLinkedFromEntryPointFile() {
106 std::unique_lock<std::mutex> Lock(M);
107 return ActiveLinksCV.wait(lock&: Lock, p: [this]() { return ActiveLinks == 0; });
108 }
109
110 using MemoryRegionInfo = RuntimeDyldChecker::MemoryRegionInfo;
111
112 struct FileInfo {
113 StringMap<MemoryRegionInfo> SectionInfos;
114 StringMap<SmallVector<MemoryRegionInfo, 1>> StubInfos;
115 StringMap<MemoryRegionInfo> GOTEntryInfos;
116
117 using Symbol = jitlink::Symbol;
118 using LinkGraph = jitlink::LinkGraph;
119 using GetSymbolTargetFunction =
120 unique_function<Expected<Symbol &>(LinkGraph &G, jitlink::Block &)>;
121 Error registerGOTEntry(LinkGraph &G, Symbol &Sym,
122 GetSymbolTargetFunction GetSymbolTarget);
123 Error registerStubEntry(LinkGraph &G, Symbol &Sym,
124 GetSymbolTargetFunction GetSymbolTarget);
125 Error registerMultiStubEntry(LinkGraph &G, Symbol &Sym,
126 GetSymbolTargetFunction GetSymbolTarget);
127 };
128
129 using DynLibJDMap = std::map<std::string, orc::JITDylib *, std::less<>>;
130 using SymbolInfoMap = DenseMap<orc::SymbolStringPtr, MemoryRegionInfo>;
131 using FileInfoMap = StringMap<FileInfo>;
132
133 Expected<orc::JITDylib *> getOrLoadDynamicLibrary(StringRef LibPath);
134 Error loadAndLinkDynamicLibrary(orc::JITDylib &JD, StringRef LibPath);
135
136 orc::ObjectLayer &getLinkLayer(bool Lazy) {
137 assert((!Lazy || LazyLinking) &&
138 "Lazy linking requested but not available");
139 return Lazy ? static_cast<orc::ObjectLayer &>(
140 LazyLinking->LazyObjLinkingLayer)
141 : static_cast<orc::ObjectLayer &>(*ObjLayer);
142 }
143
144 Expected<FileInfo &> findFileInfo(StringRef FileName);
145 Expected<MemoryRegionInfo &> findSectionInfo(StringRef FileName,
146 StringRef SectionName);
147 Expected<MemoryRegionInfo &> findStubInfo(StringRef FileName,
148 StringRef TargetName,
149 StringRef KindNameFilter);
150 Expected<MemoryRegionInfo &> findGOTEntryInfo(StringRef FileName,
151 StringRef TargetName);
152
153 bool isSymbolRegistered(const orc::SymbolStringPtr &Name);
154 Expected<MemoryRegionInfo &> findSymbolInfo(const orc::SymbolStringPtr &Name,
155 Twine ErrorMsgStem);
156
157 DynLibJDMap DynLibJDs;
158
159 std::mutex M;
160 std::condition_variable ActiveLinksCV;
161 size_t ActiveLinks = 0;
162 SymbolInfoMap SymbolInfos;
163 FileInfoMap FileInfos;
164
165 StringSet<> HarnessFiles;
166 StringSet<> HarnessExternals;
167 StringSet<> HarnessDefinitions;
168 DenseMap<StringRef, StringRef> CanonicalWeakDefs;
169
170 StringSet<> HiddenArchives;
171
172 std::optional<Regex> ShowGraphsRegex;
173
174private:
175 Session(std::unique_ptr<orc::ExecutorProcessControl> EPC, Error &Err);
176
177 std::unique_ptr<WaitingOnGraphOpRecorder> GOpRecorder;
178};
179
180/// Record symbols, GOT entries, stubs, and sections for ELF file.
181Error registerELFGraphInfo(Session &S, jitlink::LinkGraph &G);
182
183/// Record symbols, GOT entries, stubs, and sections for MachO file.
184Error registerMachOGraphInfo(Session &S, jitlink::LinkGraph &G);
185
186/// Record symbols, GOT entries, stubs, and sections for COFF file.
187Error registerCOFFGraphInfo(Session &S, jitlink::LinkGraph &G);
188
189/// Adds a statistics gathering plugin if any stats options are used.
190void enableStatistics(Session &S, bool UsingOrcRuntime);
191
192} // end namespace llvm
193
194#endif // LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
195