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" 17 #include "llvm/ExecutionEngine/Orc/Core.h" 18 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h" 19 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" 20 #include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h" 21 22 #include <future> 23 #include <thread> 24 #include <vector> 25 26 namespace llvm { 27 namespace orc { 28 29 struct ELFPerObjectSectionsToRegister { 30 ExecutorAddrRange EHFrameSection; 31 ExecutorAddrRange ThreadDataSection; 32 }; 33 34 struct ELFNixJITDylibInitializers { 35 using SectionList = std::vector<ExecutorAddrRange>; 36 37 ELFNixJITDylibInitializers(std::string Name, ExecutorAddr DSOHandleAddress) 38 : Name(std::move(Name)), DSOHandleAddress(std::move(DSOHandleAddress)) {} 39 40 std::string Name; 41 ExecutorAddr DSOHandleAddress; 42 43 StringMap<SectionList> InitSections; 44 }; 45 46 class ELFNixJITDylibDeinitializers {}; 47 48 using ELFNixJITDylibInitializerSequence = 49 std::vector<ELFNixJITDylibInitializers>; 50 51 using ELFNixJITDylibDeinitializerSequence = 52 std::vector<ELFNixJITDylibDeinitializers>; 53 54 /// Mediates between ELFNix initialization and ExecutionSession state. 55 class ELFNixPlatform : public Platform { 56 public: 57 /// Try to create a ELFNixPlatform instance, adding the ORC runtime to the 58 /// given JITDylib. 59 /// 60 /// The ORC runtime requires access to a number of symbols in 61 /// libc++. It is up to the caller to ensure that the requried 62 /// symbols can be referenced by code added to PlatformJD. The 63 /// standard way to achieve this is to first attach dynamic library 64 /// search generators for either the given process, or for the 65 /// specific required libraries, to PlatformJD, then to create the 66 /// platform instance: 67 /// 68 /// \code{.cpp} 69 /// auto &PlatformJD = ES.createBareJITDylib("stdlib"); 70 /// PlatformJD.addGenerator( 71 /// ExitOnErr(EPCDynamicLibrarySearchGenerator 72 /// ::GetForTargetProcess(EPC))); 73 /// ES.setPlatform( 74 /// ExitOnErr(ELFNixPlatform::Create(ES, ObjLayer, EPC, PlatformJD, 75 /// "/path/to/orc/runtime"))); 76 /// \endcode 77 /// 78 /// Alternatively, these symbols could be added to another JITDylib that 79 /// PlatformJD links against. 80 /// 81 /// Clients are also responsible for ensuring that any JIT'd code that 82 /// depends on runtime functions (including any code using TLV or static 83 /// destructors) can reference the runtime symbols. This is usually achieved 84 /// by linking any JITDylibs containing regular code against 85 /// PlatformJD. 86 /// 87 /// By default, ELFNixPlatform will add the set of aliases returned by the 88 /// standardPlatformAliases function. This includes both required aliases 89 /// (e.g. __cxa_atexit -> __orc_rt_elf_cxa_atexit for static destructor 90 /// support), and optional aliases that provide JIT versions of common 91 /// functions (e.g. dlopen -> __orc_rt_elf_jit_dlopen). Clients can 92 /// override these defaults by passing a non-None value for the 93 /// RuntimeAliases function, in which case the client is responsible for 94 /// setting up all aliases (including the required ones). 95 static Expected<std::unique_ptr<ELFNixPlatform>> 96 Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer, 97 JITDylib &PlatformJD, const char *OrcRuntimePath, 98 Optional<SymbolAliasMap> RuntimeAliases = None); 99 100 ExecutionSession &getExecutionSession() const { return ES; } 101 ObjectLinkingLayer &getObjectLinkingLayer() const { return ObjLinkingLayer; } 102 103 Error setupJITDylib(JITDylib &JD) override; 104 Error teardownJITDylib(JITDylib &JD) override; 105 Error notifyAdding(ResourceTracker &RT, 106 const MaterializationUnit &MU) override; 107 Error notifyRemoving(ResourceTracker &RT) override; 108 109 /// Returns an AliasMap containing the default aliases for the ELFNixPlatform. 110 /// This can be modified by clients when constructing the platform to add 111 /// or remove aliases. 112 static SymbolAliasMap standardPlatformAliases(ExecutionSession &ES); 113 114 /// Returns the array of required CXX aliases. 115 static ArrayRef<std::pair<const char *, const char *>> requiredCXXAliases(); 116 117 /// Returns the array of standard runtime utility aliases for ELF. 118 static ArrayRef<std::pair<const char *, const char *>> 119 standardRuntimeUtilityAliases(); 120 121 /// Returns true if the given section name is an initializer section. 122 static bool isInitializerSection(StringRef SecName); 123 124 private: 125 // The ELFNixPlatformPlugin scans/modifies LinkGraphs to support ELF 126 // platform features including initializers, exceptions, TLV, and language 127 // runtime registration. 128 class ELFNixPlatformPlugin : public ObjectLinkingLayer::Plugin { 129 public: 130 ELFNixPlatformPlugin(ELFNixPlatform &MP) : MP(MP) {} 131 132 void modifyPassConfig(MaterializationResponsibility &MR, 133 jitlink::LinkGraph &G, 134 jitlink::PassConfiguration &Config) override; 135 136 SyntheticSymbolDependenciesMap 137 getSyntheticSymbolDependencies(MaterializationResponsibility &MR) override; 138 139 // FIXME: We should be tentatively tracking scraped sections and discarding 140 // if the MR fails. 141 Error notifyFailed(MaterializationResponsibility &MR) override { 142 return Error::success(); 143 } 144 145 Error notifyRemovingResources(ResourceKey K) override { 146 return Error::success(); 147 } 148 149 void notifyTransferringResources(ResourceKey DstKey, 150 ResourceKey SrcKey) override {} 151 152 private: 153 using InitSymbolDepMap = 154 DenseMap<MaterializationResponsibility *, JITLinkSymbolSet>; 155 156 void addInitializerSupportPasses(MaterializationResponsibility &MR, 157 jitlink::PassConfiguration &Config); 158 159 void addDSOHandleSupportPasses(MaterializationResponsibility &MR, 160 jitlink::PassConfiguration &Config); 161 162 void addEHAndTLVSupportPasses(MaterializationResponsibility &MR, 163 jitlink::PassConfiguration &Config); 164 165 Error preserveInitSections(jitlink::LinkGraph &G, 166 MaterializationResponsibility &MR); 167 168 Error registerInitSections(jitlink::LinkGraph &G, JITDylib &JD); 169 170 Error fixTLVSectionsAndEdges(jitlink::LinkGraph &G, JITDylib &JD); 171 172 std::mutex PluginMutex; 173 ELFNixPlatform &MP; 174 InitSymbolDepMap InitSymbolDeps; 175 }; 176 177 using SendInitializerSequenceFn = 178 unique_function<void(Expected<ELFNixJITDylibInitializerSequence>)>; 179 180 using SendDeinitializerSequenceFn = 181 unique_function<void(Expected<ELFNixJITDylibDeinitializerSequence>)>; 182 183 using SendSymbolAddressFn = unique_function<void(Expected<ExecutorAddr>)>; 184 185 static bool supportedTarget(const Triple &TT); 186 187 ELFNixPlatform(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer, 188 JITDylib &PlatformJD, 189 std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator, 190 Error &Err); 191 192 // Associate ELFNixPlatform JIT-side runtime support functions with handlers. 193 Error associateRuntimeSupportFunctions(JITDylib &PlatformJD); 194 195 void getInitializersBuildSequencePhase(SendInitializerSequenceFn SendResult, 196 JITDylib &JD, 197 std::vector<JITDylibSP> DFSLinkOrder); 198 199 void getInitializersLookupPhase(SendInitializerSequenceFn SendResult, 200 JITDylib &JD); 201 202 void rt_getInitializers(SendInitializerSequenceFn SendResult, 203 StringRef JDName); 204 205 void rt_getDeinitializers(SendDeinitializerSequenceFn SendResult, 206 ExecutorAddr Handle); 207 208 void rt_lookupSymbol(SendSymbolAddressFn SendResult, ExecutorAddr Handle, 209 StringRef SymbolName); 210 211 // Records the addresses of runtime symbols used by the platform. 212 Error bootstrapELFNixRuntime(JITDylib &PlatformJD); 213 214 Error registerInitInfo(JITDylib &JD, 215 ArrayRef<jitlink::Section *> InitSections); 216 217 Error registerPerObjectSections(const ELFPerObjectSectionsToRegister &POSR); 218 219 Expected<uint64_t> createPThreadKey(); 220 221 ExecutionSession &ES; 222 ObjectLinkingLayer &ObjLinkingLayer; 223 224 SymbolStringPtr DSOHandleSymbol; 225 std::atomic<bool> RuntimeBootstrapped{false}; 226 227 ExecutorAddr orc_rt_elfnix_platform_bootstrap; 228 ExecutorAddr orc_rt_elfnix_platform_shutdown; 229 ExecutorAddr orc_rt_elfnix_register_object_sections; 230 ExecutorAddr orc_rt_elfnix_create_pthread_key; 231 232 DenseMap<JITDylib *, SymbolLookupSet> RegisteredInitSymbols; 233 234 // InitSeqs gets its own mutex to avoid locking the whole session when 235 // aggregating data from the jitlink. 236 std::mutex PlatformMutex; 237 DenseMap<JITDylib *, ELFNixJITDylibInitializers> InitSeqs; 238 std::vector<ELFPerObjectSectionsToRegister> BootstrapPOSRs; 239 240 DenseMap<ExecutorAddr, JITDylib *> HandleAddrToJITDylib; 241 DenseMap<JITDylib *, uint64_t> JITDylibToPThreadKey; 242 }; 243 244 namespace shared { 245 246 using SPSELFPerObjectSectionsToRegister = 247 SPSTuple<SPSExecutorAddrRange, SPSExecutorAddrRange>; 248 249 template <> 250 class SPSSerializationTraits<SPSELFPerObjectSectionsToRegister, 251 ELFPerObjectSectionsToRegister> { 252 253 public: 254 static size_t size(const ELFPerObjectSectionsToRegister &MOPOSR) { 255 return SPSELFPerObjectSectionsToRegister::AsArgList::size( 256 MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection); 257 } 258 259 static bool serialize(SPSOutputBuffer &OB, 260 const ELFPerObjectSectionsToRegister &MOPOSR) { 261 return SPSELFPerObjectSectionsToRegister::AsArgList::serialize( 262 OB, MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection); 263 } 264 265 static bool deserialize(SPSInputBuffer &IB, 266 ELFPerObjectSectionsToRegister &MOPOSR) { 267 return SPSELFPerObjectSectionsToRegister::AsArgList::deserialize( 268 IB, MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection); 269 } 270 }; 271 272 using SPSNamedExecutorAddrRangeSequenceMap = 273 SPSSequence<SPSTuple<SPSString, SPSExecutorAddrRangeSequence>>; 274 275 using SPSELFNixJITDylibInitializers = 276 SPSTuple<SPSString, SPSExecutorAddr, SPSNamedExecutorAddrRangeSequenceMap>; 277 278 using SPSELFNixJITDylibInitializerSequence = 279 SPSSequence<SPSELFNixJITDylibInitializers>; 280 281 /// Serialization traits for ELFNixJITDylibInitializers. 282 template <> 283 class SPSSerializationTraits<SPSELFNixJITDylibInitializers, 284 ELFNixJITDylibInitializers> { 285 public: 286 static size_t size(const ELFNixJITDylibInitializers &MOJDIs) { 287 return SPSELFNixJITDylibInitializers::AsArgList::size( 288 MOJDIs.Name, MOJDIs.DSOHandleAddress, MOJDIs.InitSections); 289 } 290 291 static bool serialize(SPSOutputBuffer &OB, 292 const ELFNixJITDylibInitializers &MOJDIs) { 293 return SPSELFNixJITDylibInitializers::AsArgList::serialize( 294 OB, MOJDIs.Name, MOJDIs.DSOHandleAddress, MOJDIs.InitSections); 295 } 296 297 static bool deserialize(SPSInputBuffer &IB, 298 ELFNixJITDylibInitializers &MOJDIs) { 299 return SPSELFNixJITDylibInitializers::AsArgList::deserialize( 300 IB, MOJDIs.Name, MOJDIs.DSOHandleAddress, MOJDIs.InitSections); 301 } 302 }; 303 304 using SPSELFJITDylibDeinitializers = SPSEmpty; 305 306 using SPSELFJITDylibDeinitializerSequence = 307 SPSSequence<SPSELFJITDylibDeinitializers>; 308 309 template <> 310 class SPSSerializationTraits<SPSELFJITDylibDeinitializers, 311 ELFNixJITDylibDeinitializers> { 312 public: 313 static size_t size(const ELFNixJITDylibDeinitializers &MOJDDs) { return 0; } 314 315 static bool serialize(SPSOutputBuffer &OB, 316 const ELFNixJITDylibDeinitializers &MOJDDs) { 317 return true; 318 } 319 320 static bool deserialize(SPSInputBuffer &IB, 321 ELFNixJITDylibDeinitializers &MOJDDs) { 322 MOJDDs = ELFNixJITDylibDeinitializers(); 323 return true; 324 } 325 }; 326 327 } // end namespace shared 328 } // end namespace orc 329 } // end namespace llvm 330 331 #endif // LLVM_EXECUTIONENGINE_ORC_ELFNIXPLATFORM_H 332