LLVM 17.0.0git
COFFPlatform.h
Go to the documentation of this file.
1//===--- COFFPlatform.h -- Utilities for executing COFF 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// Utilities for executing JIT'd COFF in Orc.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_COFFPLATFORM_H
14#define LLVM_EXECUTIONENGINE_ORC_COFFPLATFORM_H
15
16#include "llvm/ADT/StringRef.h"
23
24#include <future>
25#include <memory>
26#include <thread>
27#include <vector>
28
29namespace llvm {
30namespace orc {
31
32/// Mediates between COFF initialization and ExecutionSession state.
33class COFFPlatform : public Platform {
34public:
35 /// A function that will be called with the name of dll file that must be
36 /// loaded.
38 unique_function<Error(JITDylib &JD, StringRef DLLFileName)>;
39
40 /// Try to create a COFFPlatform instance, adding the ORC runtime to the
41 /// given JITDylib.
43 Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
44 JITDylib &PlatformJD, const char *OrcRuntimePath,
45 LoadDynamicLibrary LoadDynLibrary, bool StaticVCRuntime = false,
46 const char *VCRuntimePath = nullptr,
47 std::optional<SymbolAliasMap> RuntimeAliases = std::nullopt);
48
49 ExecutionSession &getExecutionSession() const { return ES; }
50 ObjectLinkingLayer &getObjectLinkingLayer() const { return ObjLinkingLayer; }
51
52 Error setupJITDylib(JITDylib &JD) override;
53 Error teardownJITDylib(JITDylib &JD) override;
55 const MaterializationUnit &MU) override;
57
58 /// Returns an AliasMap containing the default aliases for the COFFPlatform.
59 /// This can be modified by clients when constructing the platform to add
60 /// or remove aliases.
62
63 /// Returns the array of required CXX aliases.
65
66 /// Returns the array of standard runtime utility aliases for COFF.
69
70 static StringRef getSEHFrameSectionName() { return ".pdata"; }
71
72private:
73 using COFFJITDylibDepInfo = std::vector<ExecutorAddr>;
74 using COFFJITDylibDepInfoMap =
75 std::vector<std::pair<ExecutorAddr, COFFJITDylibDepInfo>>;
76 using COFFObjectSectionsMap =
78 using PushInitializersSendResultFn =
80 using SendSymbolAddressFn = unique_function<void(Expected<ExecutorAddr>)>;
82
83 // The COFFPlatformPlugin scans/modifies LinkGraphs to support COFF
84 // platform features including initializers, exceptions, and language
85 // runtime registration.
86 class COFFPlatformPlugin : public ObjectLinkingLayer::Plugin {
87 public:
88 COFFPlatformPlugin(COFFPlatform &CP) : CP(CP) {}
89
90 void modifyPassConfig(MaterializationResponsibility &MR,
92 jitlink::PassConfiguration &Config) override;
93
95 getSyntheticSymbolDependencies(MaterializationResponsibility &MR) override;
96
97 // FIXME: We should be tentatively tracking scraped sections and discarding
98 // if the MR fails.
99 Error notifyFailed(MaterializationResponsibility &MR) override {
100 return Error::success();
101 }
102
103 Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override {
104 return Error::success();
105 }
106
107 void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey,
108 ResourceKey SrcKey) override {}
109
110 private:
111 using InitSymbolDepMap =
112 DenseMap<MaterializationResponsibility *, JITLinkSymbolSet>;
113
114 Error associateJITDylibHeaderSymbol(jitlink::LinkGraph &G,
115 MaterializationResponsibility &MR,
116 bool Bootstrap);
117
118 Error preserveInitializerSections(jitlink::LinkGraph &G,
119 MaterializationResponsibility &MR);
120 Error registerObjectPlatformSections(jitlink::LinkGraph &G, JITDylib &JD);
121 Error registerObjectPlatformSectionsInBootstrap(jitlink::LinkGraph &G,
122 JITDylib &JD);
123
124 std::mutex PluginMutex;
125 COFFPlatform &CP;
126 InitSymbolDepMap InitSymbolDeps;
127 };
128
129 struct JDBootstrapState {
130 JITDylib *JD = nullptr;
131 std::string JDName;
132 ExecutorAddr HeaderAddr;
133 std::list<COFFObjectSectionsMap> ObjectSectionsMaps;
134 SmallVector<std::pair<std::string, ExecutorAddr>> Initializers;
135 };
136
137 static bool supportedTarget(const Triple &TT);
138
139 COFFPlatform(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
140 JITDylib &PlatformJD, const char *OrcRuntimePath,
141 LoadDynamicLibrary LoadDynamicLibrary, bool StaticVCRuntime,
142 const char *VCRuntimePath, Error &Err);
143
144 // Associate COFFPlatform JIT-side runtime support functions with handlers.
145 Error associateRuntimeSupportFunctions(JITDylib &PlatformJD);
146
147 // Records the addresses of runtime symbols used by the platform.
148 Error bootstrapCOFFRuntime(JITDylib &PlatformJD);
149
150 // Run a specific void function if it exists.
151 Error runSymbolIfExists(JITDylib &PlatformJD, StringRef SymbolName);
152
153 // Run collected initializers in boostrap stage.
154 Error runBootstrapInitializers(JDBootstrapState &BState);
155 Error runBootstrapSubsectionInitializers(JDBootstrapState &BState,
156 StringRef Start, StringRef End);
157
158 // Build dependency graph of a JITDylib
159 Expected<JITDylibDepMap> buildJDDepMap(JITDylib &JD);
160
161 Expected<MemoryBufferRef> getPerJDObjectFile();
162
163 // Implements rt_pushInitializers by making repeat async lookups for
164 // initializer symbols (each lookup may spawn more initializer symbols if
165 // it pulls in new materializers, e.g. from objects in a static library).
166 void pushInitializersLoop(PushInitializersSendResultFn SendResult,
167 JITDylibSP JD, JITDylibDepMap &JDDepMap);
168
169 void rt_pushInitializers(PushInitializersSendResultFn SendResult,
170 ExecutorAddr JDHeaderAddr);
171
172 void rt_lookupSymbol(SendSymbolAddressFn SendResult, ExecutorAddr Handle,
173 StringRef SymbolName);
174
175 ExecutionSession &ES;
176 ObjectLinkingLayer &ObjLinkingLayer;
177
178 LoadDynamicLibrary LoadDynLibrary;
179 std::unique_ptr<COFFVCRuntimeBootstrapper> VCRuntimeBootstrap;
180 std::unique_ptr<MemoryBuffer> OrcRuntimeArchiveBuffer;
181 std::unique_ptr<object::Archive> OrcRuntimeArchive;
182 bool StaticVCRuntime;
183
184 SymbolStringPtr COFFHeaderStartSymbol;
185
186 // State of bootstrap in progress
187 std::map<JITDylib *, JDBootstrapState> JDBootstrapStates;
188 std::atomic<bool> Bootstrapping;
189
190 ExecutorAddr orc_rt_coff_platform_bootstrap;
191 ExecutorAddr orc_rt_coff_platform_shutdown;
192 ExecutorAddr orc_rt_coff_register_object_sections;
193 ExecutorAddr orc_rt_coff_deregister_object_sections;
194 ExecutorAddr orc_rt_coff_register_jitdylib;
195 ExecutorAddr orc_rt_coff_deregister_jitdylib;
196
197 DenseMap<JITDylib *, ExecutorAddr> JITDylibToHeaderAddr;
198 DenseMap<ExecutorAddr, JITDylib *> HeaderAddrToJITDylib;
199
200 DenseMap<JITDylib *, SymbolLookupSet> RegisteredInitSymbols;
201
202 std::set<std::string> DylibsToPreload;
203
204 std::mutex PlatformMutex;
205};
206
207} // end namespace orc
208} // end namespace llvm
209
210#endif // LLVM_EXECUTIONENGINE_ORC_COFFPLATFORM_H
#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:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Mediates between COFF initialization and ExecutionSession state.
Definition: COFFPlatform.h:33
Error setupJITDylib(JITDylib &JD) override
This method will be called outside the session lock each time a JITDylib is created (unless it is cre...
static ArrayRef< std::pair< const char *, const char * > > standardRuntimeUtilityAliases()
Returns the array of standard runtime utility aliases for COFF.
Error teardownJITDylib(JITDylib &JD) override
This method will be called outside the session lock each time a JITDylib is removed to allow the Plat...
static StringRef getSEHFrameSectionName()
Definition: COFFPlatform.h:70
ObjectLinkingLayer & getObjectLinkingLayer() const
Definition: COFFPlatform.h:50
static SymbolAliasMap standardPlatformAliases(ExecutionSession &ES)
Returns an AliasMap containing the default aliases for the COFFPlatform.
Error notifyAdding(ResourceTracker &RT, const MaterializationUnit &MU) override
This method will be called under the ExecutionSession lock each time a MaterializationUnit is added t...
static ArrayRef< std::pair< const char *, const char * > > requiredCXXAliases()
Returns the array of required CXX aliases.
ExecutionSession & getExecutionSession() const
Definition: COFFPlatform.h:49
static Expected< std::unique_ptr< COFFPlatform > > Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, const char *OrcRuntimePath, LoadDynamicLibrary LoadDynLibrary, bool StaticVCRuntime=false, const char *VCRuntimePath=nullptr, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a COFFPlatform instance, adding the ORC runtime to the given JITDylib.
unique_function< Error(JITDylib &JD, StringRef DLLFileName)> LoadDynamicLibrary
A function that will be called with the name of dll file that must be loaded.
Definition: COFFPlatform.h:38
Error notifyRemoving(ResourceTracker &RT) override
This method will be called under the ExecutionSession lock when a ResourceTracker is removed.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1373
Represents a JIT'd dynamic library.
Definition: Core.h:962
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:673
Plugin instances can be added to the ObjectLinkingLayer to receive callbacks when code is loaded or e...
DenseMap< SymbolStringPtr, JITLinkSymbolSet > SyntheticSymbolDependenciesMap
An ObjectLayer implementation built on JITLink.
Platforms set up standard symbols and mediate interactions between dynamic initializers (e....
Definition: Core.h:1317
API to remove / transfer ownership of JIT resources.
Definition: Core.h:53
IntrusiveRefCntPtr< JITDylib > JITDylibSP
Definition: Core.h:48
uintptr_t ResourceKey
Definition: Core.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18