LLVM 20.0.0git
ELFNixPlatform.h
Go to the documentation of this file.
1//===-- ELFNixPlatform.h -- Utilities for executing ELF in Orc --*- 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// Linux/BSD support for executing JIT'd ELF in Orc.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_ELFNIXPLATFORM_H
14#define LLVM_EXECUTIONENGINE_ORC_ELFNIXPLATFORM_H
15
16#include "llvm/ADT/StringRef.h"
21
22#include <future>
23#include <thread>
24#include <unordered_map>
25#include <vector>
26
27namespace llvm {
28namespace orc {
29
33};
34
35using ELFNixJITDylibDepInfo = std::vector<ExecutorAddr>;
37 std::vector<std::pair<ExecutorAddr, ELFNixJITDylibDepInfo>>;
38
43};
44
46 std::size_t
47 operator()(const std::pair<RuntimeFunction *, RuntimeFunction *> &key) const {
48 return std::hash<void *>()(key.first->Addr.toPtr<void *>()) ^
49 std::hash<void *>()(key.second->Addr.toPtr<void *>());
50 }
51};
52
54 std::size_t
55 operator()(const std::pair<RuntimeFunction *, RuntimeFunction *> &lhs,
56 const std::pair<RuntimeFunction *, RuntimeFunction *> &rhs) const {
57 return lhs.first == rhs.first && lhs.second == rhs.second;
58 }
59};
60
61using DeferredRuntimeFnMap = std::unordered_map<
62 std::pair<RuntimeFunction *, RuntimeFunction *>,
66
67/// Mediates between ELFNix initialization and ExecutionSession state.
68class ELFNixPlatform : public Platform {
69public:
70 /// Try to create a ELFNixPlatform instance, adding the ORC runtime to the
71 /// given JITDylib.
72 ///
73 /// The ORC runtime requires access to a number of symbols in
74 /// libc++. It is up to the caller to ensure that the required
75 /// symbols can be referenced by code added to PlatformJD. The
76 /// standard way to achieve this is to first attach dynamic library
77 /// search generators for either the given process, or for the
78 /// specific required libraries, to PlatformJD, then to create the
79 /// platform instance:
80 ///
81 /// \code{.cpp}
82 /// auto &PlatformJD = ES.createBareJITDylib("stdlib");
83 /// PlatformJD.addGenerator(
84 /// ExitOnErr(EPCDynamicLibrarySearchGenerator
85 /// ::GetForTargetProcess(EPC)));
86 /// ES.setPlatform(
87 /// ExitOnErr(ELFNixPlatform::Create(ES, ObjLayer, EPC, PlatformJD,
88 /// "/path/to/orc/runtime")));
89 /// \endcode
90 ///
91 /// Alternatively, these symbols could be added to another JITDylib that
92 /// PlatformJD links against.
93 ///
94 /// Clients are also responsible for ensuring that any JIT'd code that
95 /// depends on runtime functions (including any code using TLV or static
96 /// destructors) can reference the runtime symbols. This is usually achieved
97 /// by linking any JITDylibs containing regular code against
98 /// PlatformJD.
99 ///
100 /// By default, ELFNixPlatform will add the set of aliases returned by the
101 /// standardPlatformAliases function. This includes both required aliases
102 /// (e.g. __cxa_atexit -> __orc_rt_elf_cxa_atexit for static destructor
103 /// support), and optional aliases that provide JIT versions of common
104 /// functions (e.g. dlopen -> __orc_rt_elf_jit_dlopen). Clients can
105 /// override these defaults by passing a non-None value for the
106 /// RuntimeAliases function, in which case the client is responsible for
107 /// setting up all aliases (including the required ones).
109 Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD,
110 std::unique_ptr<DefinitionGenerator> OrcRuntime,
111 std::optional<SymbolAliasMap> RuntimeAliases = std::nullopt);
112
113 /// Construct using a path to the ORC runtime.
115 Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD,
116 const char *OrcRuntimePath,
117 std::optional<SymbolAliasMap> RuntimeAliases = std::nullopt);
118
119 ExecutionSession &getExecutionSession() const { return ES; }
120 ObjectLinkingLayer &getObjectLinkingLayer() const { return ObjLinkingLayer; }
121
122 Error setupJITDylib(JITDylib &JD) override;
123 Error teardownJITDylib(JITDylib &JD) override;
125 const MaterializationUnit &MU) override;
127
128 /// Returns an AliasMap containing the default aliases for the ELFNixPlatform.
129 /// This can be modified by clients when constructing the platform to add
130 /// or remove aliases.
132 JITDylib &PlatformJD);
133
134 /// Returns the array of required CXX aliases.
136
137 /// Returns the array of standard runtime utility aliases for ELF.
140
141 /// Returns a list of aliases required to enable lazy compilation via the
142 /// ORC runtime.
145
146private:
147 // Data needed for bootstrap only.
148 struct BootstrapInfo {
149 std::mutex Mutex;
150 std::condition_variable CV;
151 size_t ActiveGraphs = 0;
152 ExecutorAddr ELFNixHeaderAddr;
153 DeferredRuntimeFnMap DeferredRTFnMap;
154
155 void addArgumentsToRTFnMap(
156 RuntimeFunction *func1, RuntimeFunction *func2,
159 auto &argList = DeferredRTFnMap[std::make_pair(func1, func2)];
160 argList.emplace_back(arg1, arg2);
161 }
162 };
163
164 // The ELFNixPlatformPlugin scans/modifies LinkGraphs to support ELF
165 // platform features including initializers, exceptions, TLV, and language
166 // runtime registration.
167 class ELFNixPlatformPlugin : public ObjectLinkingLayer::Plugin {
168 public:
169 ELFNixPlatformPlugin(ELFNixPlatform &MP) : MP(MP) {}
170
171 void modifyPassConfig(MaterializationResponsibility &MR,
172 jitlink::LinkGraph &G,
173 jitlink::PassConfiguration &Config) override;
174
175 // FIXME: We should be tentatively tracking scraped sections and discarding
176 // if the MR fails.
177 Error notifyFailed(MaterializationResponsibility &MR) override {
178 return Error::success();
179 }
180
181 Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override {
182 return Error::success();
183 }
184
185 void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey,
186 ResourceKey SrcKey) override {}
187
188 private:
189 Error bootstrapPipelineStart(jitlink::LinkGraph &G);
190 Error bootstrapPipelineRecordRuntimeFunctions(jitlink::LinkGraph &G);
191 Error bootstrapPipelineEnd(jitlink::LinkGraph &G);
192
193 void addDSOHandleSupportPasses(MaterializationResponsibility &MR,
194 jitlink::PassConfiguration &Config);
195
196 void addEHAndTLVSupportPasses(MaterializationResponsibility &MR,
197 jitlink::PassConfiguration &Config,
198 bool IsBootstrapping);
199
200 Error preserveInitSections(jitlink::LinkGraph &G,
201 MaterializationResponsibility &MR);
202
203 Error registerInitSections(jitlink::LinkGraph &G, JITDylib &JD,
204 bool IsBootstrapping);
205
206 Error fixTLVSectionsAndEdges(jitlink::LinkGraph &G, JITDylib &JD);
207
208 std::mutex PluginMutex;
209 ELFNixPlatform &MP;
210 };
211
212 using PushInitializersSendResultFn =
213 unique_function<void(Expected<ELFNixJITDylibDepInfoMap>)>;
214
215 using SendSymbolAddressFn = unique_function<void(Expected<ExecutorAddr>)>;
216
217 static bool supportedTarget(const Triple &TT);
218
219 ELFNixPlatform(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD,
220 std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator,
221 Error &Err);
222
223 // Associate ELFNixPlatform JIT-side runtime support functions with handlers.
224 Error associateRuntimeSupportFunctions(JITDylib &PlatformJD);
225
226 void pushInitializersLoop(PushInitializersSendResultFn SendResult,
227 JITDylibSP JD);
228
229 void rt_recordInitializers(PushInitializersSendResultFn SendResult,
230 ExecutorAddr JDHeader);
231
232 void rt_lookupSymbol(SendSymbolAddressFn SendResult, ExecutorAddr Handle,
233 StringRef SymbolName);
234
235 Error registerPerObjectSections(jitlink::LinkGraph &G,
236 const ELFPerObjectSectionsToRegister &POSR,
237 bool IsBootstrapping);
238
239 Expected<uint64_t> createPThreadKey();
240
241 ExecutionSession &ES;
242 JITDylib &PlatformJD;
243 ObjectLinkingLayer &ObjLinkingLayer;
244
245 SymbolStringPtr DSOHandleSymbol;
246
247 RuntimeFunction PlatformBootstrap{
248 ES.intern("__orc_rt_elfnix_platform_bootstrap")};
249 RuntimeFunction PlatformShutdown{
250 ES.intern("__orc_rt_elfnix_platform_shutdown")};
251 RuntimeFunction RegisterJITDylib{
252 ES.intern("__orc_rt_elfnix_register_jitdylib")};
253 RuntimeFunction DeregisterJITDylib{
254 ES.intern("__orc_rt_elfnix_deregister_jitdylib")};
255 RuntimeFunction RegisterObjectSections{
256 ES.intern("__orc_rt_elfnix_register_object_sections")};
257 RuntimeFunction DeregisterObjectSections{
258 ES.intern("__orc_rt_elfnix_deregister_object_sections")};
259 RuntimeFunction RegisterInitSections{
260 ES.intern("__orc_rt_elfnix_register_init_sections")};
261 RuntimeFunction DeregisterInitSections{
262 ES.intern("__orc_rt_elfnix_deregister_init_sections")};
263 RuntimeFunction CreatePThreadKey{
264 ES.intern("__orc_rt_elfnix_create_pthread_key")};
265
266 DenseMap<JITDylib *, SymbolLookupSet> RegisteredInitSymbols;
267
268 // InitSeqs gets its own mutex to avoid locking the whole session when
269 // aggregating data from the jitlink.
270 std::mutex PlatformMutex;
271 std::vector<ELFPerObjectSectionsToRegister> BootstrapPOSRs;
272
273 DenseMap<ExecutorAddr, JITDylib *> HandleAddrToJITDylib;
274 DenseMap<JITDylib *, ExecutorAddr> JITDylibToHandleAddr;
275 DenseMap<JITDylib *, uint64_t> JITDylibToPThreadKey;
276
277 std::atomic<BootstrapInfo *> Bootstrap;
278};
279
280namespace shared {
281
284
285template <>
288
289public:
290 static size_t size(const ELFPerObjectSectionsToRegister &MOPOSR) {
291 return SPSELFPerObjectSectionsToRegister::AsArgList::size(
292 MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
293 }
294
295 static bool serialize(SPSOutputBuffer &OB,
296 const ELFPerObjectSectionsToRegister &MOPOSR) {
297 return SPSELFPerObjectSectionsToRegister::AsArgList::serialize(
298 OB, MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
299 }
300
303 return SPSELFPerObjectSectionsToRegister::AsArgList::deserialize(
304 IB, MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
305 }
306};
307
310
311} // end namespace shared
312} // end namespace orc
313} // end namespace llvm
314
315#endif // LLVM_EXECUTIONENGINE_ORC_ELFNIXPLATFORM_H
RelaxConfig Config
Definition: ELF_riscv.cpp:506
#define G(x, y, z)
Definition: MD5.cpp:56
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
Mediates between ELFNix initialization and ExecutionSession state.
ObjectLinkingLayer & getObjectLinkingLayer() const
static Expected< SymbolAliasMap > standardPlatformAliases(ExecutionSession &ES, JITDylib &PlatformJD)
Returns an AliasMap containing the default aliases for the ELFNixPlatform.
Error notifyAdding(ResourceTracker &RT, const MaterializationUnit &MU) override
This method will be called under the ExecutionSession lock each time a MaterializationUnit is added t...
Error notifyRemoving(ResourceTracker &RT) override
This method will be called under the ExecutionSession lock when a ResourceTracker is removed.
Error setupJITDylib(JITDylib &JD) override
This method will be called outside the session lock each time a JITDylib is created (unless it is cre...
ExecutionSession & getExecutionSession() const
static ArrayRef< std::pair< const char *, const char * > > standardRuntimeUtilityAliases()
Returns the array of standard runtime utility aliases for ELF.
static Expected< std::unique_ptr< ELFNixPlatform > > Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, std::unique_ptr< DefinitionGenerator > OrcRuntime, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a ELFNixPlatform instance, adding the ORC runtime to the given JITDylib.
static ArrayRef< std::pair< const char *, const char * > > standardLazyCompilationAliases()
Returns a list of aliases required to enable lazy compilation via the ORC runtime.
static ArrayRef< std::pair< const char *, const char * > > requiredCXXAliases()
Returns the array of required CXX aliases.
Error teardownJITDylib(JITDylib &JD) override
This method will be called outside the session lock each time a JITDylib is removed to allow the Plat...
An ExecutionSession represents a running JIT program.
Definition: Core.h:1339
Represents an address in the executor process.
Represents a JIT'd dynamic library.
Definition: Core.h:897
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
An ObjectLayer implementation built on JITLink.
Platforms set up standard symbols and mediate interactions between dynamic initializers (e....
Definition: Core.h:1268
API to remove / transfer ownership of JIT resources.
Definition: Core.h:77
Pointer to a pooled string representing a symbol name.
Input char buffer with underflow check.
Output char buffer with overflow check.
static bool serialize(SPSOutputBuffer &OB, const ELFPerObjectSectionsToRegister &MOPOSR)
Specialize to describe how to serialize/deserialize to/from the given concrete type.
RuntimeFunction
IDs for all omp runtime library (RTL) functions.
Definition: OMPConstants.h:45
IntrusiveRefCntPtr< JITDylib > JITDylibSP
Definition: Core.h:52
std::vector< std::pair< ExecutorAddr, ELFNixJITDylibDepInfo > > ELFNixJITDylibDepInfoMap
std::vector< ExecutorAddr > ELFNixJITDylibDepInfo
std::unordered_map< std::pair< RuntimeFunction *, RuntimeFunction * >, SmallVector< std::pair< shared::WrapperFunctionCall::ArgDataBufferType, shared::WrapperFunctionCall::ArgDataBufferType > >, FunctionPairKeyHash, FunctionPairKeyEqual > DeferredRuntimeFnMap
uintptr_t ResourceKey
Definition: Core.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1873
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Represents an address range in the exceutor process.
std::size_t operator()(const std::pair< RuntimeFunction *, RuntimeFunction * > &lhs, const std::pair< RuntimeFunction *, RuntimeFunction * > &rhs) const
std::size_t operator()(const std::pair< RuntimeFunction *, RuntimeFunction * > &key) const
RuntimeFunction(SymbolStringPtr Name)