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