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