1 //===------ ELFNixPlatform.cpp - Utilities for executing MachO in Orc -----===// 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 #include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h" 10 11 #include "llvm/BinaryFormat/ELF.h" 12 #include "llvm/ExecutionEngine/JITLink/ELF_x86_64.h" 13 #include "llvm/ExecutionEngine/JITLink/x86_64.h" 14 #include "llvm/ExecutionEngine/Orc/DebugUtils.h" 15 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" 16 #include "llvm/Support/BinaryByteStream.h" 17 #include "llvm/Support/Debug.h" 18 19 #define DEBUG_TYPE "orc" 20 21 using namespace llvm; 22 using namespace llvm::orc; 23 using namespace llvm::orc::shared; 24 25 namespace { 26 27 class DSOHandleMaterializationUnit : public MaterializationUnit { 28 public: 29 DSOHandleMaterializationUnit(ELFNixPlatform &ENP, 30 const SymbolStringPtr &DSOHandleSymbol) 31 : MaterializationUnit( 32 createDSOHandleSectionInterface(ENP, DSOHandleSymbol)), 33 ENP(ENP) {} 34 35 StringRef getName() const override { return "DSOHandleMU"; } 36 37 void materialize(std::unique_ptr<MaterializationResponsibility> R) override { 38 unsigned PointerSize; 39 support::endianness Endianness; 40 jitlink::Edge::Kind EdgeKind; 41 const auto &TT = 42 ENP.getExecutionSession().getExecutorProcessControl().getTargetTriple(); 43 44 switch (TT.getArch()) { 45 case Triple::x86_64: 46 PointerSize = 8; 47 Endianness = support::endianness::little; 48 EdgeKind = jitlink::x86_64::Pointer64; 49 break; 50 default: 51 llvm_unreachable("Unrecognized architecture"); 52 } 53 54 // void *__dso_handle = &__dso_handle; 55 auto G = std::make_unique<jitlink::LinkGraph>( 56 "<DSOHandleMU>", TT, PointerSize, Endianness, 57 jitlink::getGenericEdgeKindName); 58 auto &DSOHandleSection = 59 G->createSection(".data.__dso_handle", jitlink::MemProt::Read); 60 auto &DSOHandleBlock = G->createContentBlock( 61 DSOHandleSection, getDSOHandleContent(PointerSize), orc::ExecutorAddr(), 62 8, 0); 63 auto &DSOHandleSymbol = G->addDefinedSymbol( 64 DSOHandleBlock, 0, *R->getInitializerSymbol(), DSOHandleBlock.getSize(), 65 jitlink::Linkage::Strong, jitlink::Scope::Default, false, true); 66 DSOHandleBlock.addEdge(EdgeKind, 0, DSOHandleSymbol, 0); 67 68 ENP.getObjectLinkingLayer().emit(std::move(R), std::move(G)); 69 } 70 71 void discard(const JITDylib &JD, const SymbolStringPtr &Sym) override {} 72 73 private: 74 static MaterializationUnit::Interface 75 createDSOHandleSectionInterface(ELFNixPlatform &ENP, 76 const SymbolStringPtr &DSOHandleSymbol) { 77 SymbolFlagsMap SymbolFlags; 78 SymbolFlags[DSOHandleSymbol] = JITSymbolFlags::Exported; 79 return MaterializationUnit::Interface(std::move(SymbolFlags), 80 DSOHandleSymbol); 81 } 82 83 ArrayRef<char> getDSOHandleContent(size_t PointerSize) { 84 static const char Content[8] = {0}; 85 assert(PointerSize <= sizeof Content); 86 return {Content, PointerSize}; 87 } 88 89 ELFNixPlatform &ENP; 90 }; 91 92 StringRef EHFrameSectionName = ".eh_frame"; 93 StringRef InitArrayFuncSectionName = ".init_array"; 94 95 StringRef ThreadBSSSectionName = ".tbss"; 96 StringRef ThreadDataSectionName = ".tdata"; 97 98 StringRef InitSectionNames[] = {InitArrayFuncSectionName}; 99 100 } // end anonymous namespace 101 102 namespace llvm { 103 namespace orc { 104 105 Expected<std::unique_ptr<ELFNixPlatform>> 106 ELFNixPlatform::Create(ExecutionSession &ES, 107 ObjectLinkingLayer &ObjLinkingLayer, 108 JITDylib &PlatformJD, const char *OrcRuntimePath, 109 Optional<SymbolAliasMap> RuntimeAliases) { 110 111 auto &EPC = ES.getExecutorProcessControl(); 112 113 // If the target is not supported then bail out immediately. 114 if (!supportedTarget(EPC.getTargetTriple())) 115 return make_error<StringError>("Unsupported ELFNixPlatform triple: " + 116 EPC.getTargetTriple().str(), 117 inconvertibleErrorCode()); 118 119 // Create default aliases if the caller didn't supply any. 120 if (!RuntimeAliases) 121 RuntimeAliases = standardPlatformAliases(ES); 122 123 // Define the aliases. 124 if (auto Err = PlatformJD.define(symbolAliases(std::move(*RuntimeAliases)))) 125 return std::move(Err); 126 127 // Add JIT-dispatch function support symbols. 128 if (auto Err = PlatformJD.define(absoluteSymbols( 129 {{ES.intern("__orc_rt_jit_dispatch"), 130 {EPC.getJITDispatchInfo().JITDispatchFunction.getValue(), 131 JITSymbolFlags::Exported}}, 132 {ES.intern("__orc_rt_jit_dispatch_ctx"), 133 {EPC.getJITDispatchInfo().JITDispatchContext.getValue(), 134 JITSymbolFlags::Exported}}}))) 135 return std::move(Err); 136 137 // Create a generator for the ORC runtime archive. 138 auto OrcRuntimeArchiveGenerator = StaticLibraryDefinitionGenerator::Load( 139 ObjLinkingLayer, OrcRuntimePath, EPC.getTargetTriple()); 140 if (!OrcRuntimeArchiveGenerator) 141 return OrcRuntimeArchiveGenerator.takeError(); 142 143 // Create the instance. 144 Error Err = Error::success(); 145 auto P = std::unique_ptr<ELFNixPlatform>( 146 new ELFNixPlatform(ES, ObjLinkingLayer, PlatformJD, 147 std::move(*OrcRuntimeArchiveGenerator), Err)); 148 if (Err) 149 return std::move(Err); 150 return std::move(P); 151 } 152 153 Error ELFNixPlatform::setupJITDylib(JITDylib &JD) { 154 return JD.define( 155 std::make_unique<DSOHandleMaterializationUnit>(*this, DSOHandleSymbol)); 156 } 157 158 Error ELFNixPlatform::teardownJITDylib(JITDylib &JD) { 159 return Error::success(); 160 } 161 162 Error ELFNixPlatform::notifyAdding(ResourceTracker &RT, 163 const MaterializationUnit &MU) { 164 auto &JD = RT.getJITDylib(); 165 const auto &InitSym = MU.getInitializerSymbol(); 166 if (!InitSym) 167 return Error::success(); 168 169 RegisteredInitSymbols[&JD].add(InitSym, 170 SymbolLookupFlags::WeaklyReferencedSymbol); 171 LLVM_DEBUG({ 172 dbgs() << "ELFNixPlatform: Registered init symbol " << *InitSym 173 << " for MU " << MU.getName() << "\n"; 174 }); 175 return Error::success(); 176 } 177 178 Error ELFNixPlatform::notifyRemoving(ResourceTracker &RT) { 179 llvm_unreachable("Not supported yet"); 180 } 181 182 static void addAliases(ExecutionSession &ES, SymbolAliasMap &Aliases, 183 ArrayRef<std::pair<const char *, const char *>> AL) { 184 for (auto &KV : AL) { 185 auto AliasName = ES.intern(KV.first); 186 assert(!Aliases.count(AliasName) && "Duplicate symbol name in alias map"); 187 Aliases[std::move(AliasName)] = {ES.intern(KV.second), 188 JITSymbolFlags::Exported}; 189 } 190 } 191 192 SymbolAliasMap ELFNixPlatform::standardPlatformAliases(ExecutionSession &ES) { 193 SymbolAliasMap Aliases; 194 addAliases(ES, Aliases, requiredCXXAliases()); 195 addAliases(ES, Aliases, standardRuntimeUtilityAliases()); 196 return Aliases; 197 } 198 199 ArrayRef<std::pair<const char *, const char *>> 200 ELFNixPlatform::requiredCXXAliases() { 201 static const std::pair<const char *, const char *> RequiredCXXAliases[] = { 202 {"__cxa_atexit", "__orc_rt_elfnix_cxa_atexit"}, 203 {"atexit", "__orc_rt_elfnix_atexit"}}; 204 205 return ArrayRef<std::pair<const char *, const char *>>(RequiredCXXAliases); 206 } 207 208 ArrayRef<std::pair<const char *, const char *>> 209 ELFNixPlatform::standardRuntimeUtilityAliases() { 210 static const std::pair<const char *, const char *> 211 StandardRuntimeUtilityAliases[] = { 212 {"__orc_rt_run_program", "__orc_rt_elfnix_run_program"}, 213 {"__orc_rt_log_error", "__orc_rt_log_error_to_stderr"}}; 214 215 return ArrayRef<std::pair<const char *, const char *>>( 216 StandardRuntimeUtilityAliases); 217 } 218 219 bool ELFNixPlatform::isInitializerSection(StringRef SecName) { 220 for (auto &Name : InitSectionNames) { 221 if (Name.equals(SecName)) 222 return true; 223 } 224 return false; 225 } 226 227 bool ELFNixPlatform::supportedTarget(const Triple &TT) { 228 switch (TT.getArch()) { 229 case Triple::x86_64: 230 return true; 231 default: 232 return false; 233 } 234 } 235 236 ELFNixPlatform::ELFNixPlatform( 237 ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer, 238 JITDylib &PlatformJD, 239 std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator, Error &Err) 240 : ES(ES), ObjLinkingLayer(ObjLinkingLayer), 241 DSOHandleSymbol(ES.intern("__dso_handle")) { 242 ErrorAsOutParameter _(&Err); 243 244 ObjLinkingLayer.addPlugin(std::make_unique<ELFNixPlatformPlugin>(*this)); 245 246 PlatformJD.addGenerator(std::move(OrcRuntimeGenerator)); 247 248 // PlatformJD hasn't been 'set-up' by the platform yet (since we're creating 249 // the platform now), so set it up. 250 if (auto E2 = setupJITDylib(PlatformJD)) { 251 Err = std::move(E2); 252 return; 253 } 254 255 RegisteredInitSymbols[&PlatformJD].add( 256 DSOHandleSymbol, SymbolLookupFlags::WeaklyReferencedSymbol); 257 258 // Associate wrapper function tags with JIT-side function implementations. 259 if (auto E2 = associateRuntimeSupportFunctions(PlatformJD)) { 260 Err = std::move(E2); 261 return; 262 } 263 264 // Lookup addresses of runtime functions callable by the platform, 265 // call the platform bootstrap function to initialize the platform-state 266 // object in the executor. 267 if (auto E2 = bootstrapELFNixRuntime(PlatformJD)) { 268 Err = std::move(E2); 269 return; 270 } 271 } 272 273 Error ELFNixPlatform::associateRuntimeSupportFunctions(JITDylib &PlatformJD) { 274 ExecutionSession::JITDispatchHandlerAssociationMap WFs; 275 276 using GetInitializersSPSSig = 277 SPSExpected<SPSELFNixJITDylibInitializerSequence>(SPSString); 278 WFs[ES.intern("__orc_rt_elfnix_get_initializers_tag")] = 279 ES.wrapAsyncWithSPS<GetInitializersSPSSig>( 280 this, &ELFNixPlatform::rt_getInitializers); 281 282 using GetDeinitializersSPSSig = 283 SPSExpected<SPSELFJITDylibDeinitializerSequence>(SPSExecutorAddr); 284 WFs[ES.intern("__orc_rt_elfnix_get_deinitializers_tag")] = 285 ES.wrapAsyncWithSPS<GetDeinitializersSPSSig>( 286 this, &ELFNixPlatform::rt_getDeinitializers); 287 288 using LookupSymbolSPSSig = 289 SPSExpected<SPSExecutorAddr>(SPSExecutorAddr, SPSString); 290 WFs[ES.intern("__orc_rt_elfnix_symbol_lookup_tag")] = 291 ES.wrapAsyncWithSPS<LookupSymbolSPSSig>(this, 292 &ELFNixPlatform::rt_lookupSymbol); 293 294 return ES.registerJITDispatchHandlers(PlatformJD, std::move(WFs)); 295 } 296 297 void ELFNixPlatform::getInitializersBuildSequencePhase( 298 SendInitializerSequenceFn SendResult, JITDylib &JD, 299 std::vector<JITDylibSP> DFSLinkOrder) { 300 ELFNixJITDylibInitializerSequence FullInitSeq; 301 { 302 std::lock_guard<std::mutex> Lock(PlatformMutex); 303 for (auto &InitJD : reverse(DFSLinkOrder)) { 304 LLVM_DEBUG({ 305 dbgs() << "ELFNixPlatform: Appending inits for \"" << InitJD->getName() 306 << "\" to sequence\n"; 307 }); 308 auto ISItr = InitSeqs.find(InitJD.get()); 309 if (ISItr != InitSeqs.end()) { 310 FullInitSeq.emplace_back(std::move(ISItr->second)); 311 InitSeqs.erase(ISItr); 312 } 313 } 314 } 315 316 SendResult(std::move(FullInitSeq)); 317 } 318 319 void ELFNixPlatform::getInitializersLookupPhase( 320 SendInitializerSequenceFn SendResult, JITDylib &JD) { 321 322 auto DFSLinkOrder = JD.getDFSLinkOrder(); 323 DenseMap<JITDylib *, SymbolLookupSet> NewInitSymbols; 324 ES.runSessionLocked([&]() { 325 for (auto &InitJD : DFSLinkOrder) { 326 auto RISItr = RegisteredInitSymbols.find(InitJD.get()); 327 if (RISItr != RegisteredInitSymbols.end()) { 328 NewInitSymbols[InitJD.get()] = std::move(RISItr->second); 329 RegisteredInitSymbols.erase(RISItr); 330 } 331 } 332 }); 333 334 // If there are no further init symbols to look up then move on to the next 335 // phase. 336 if (NewInitSymbols.empty()) { 337 getInitializersBuildSequencePhase(std::move(SendResult), JD, 338 std::move(DFSLinkOrder)); 339 return; 340 } 341 342 // Otherwise issue a lookup and re-run this phase when it completes. 343 lookupInitSymbolsAsync( 344 [this, SendResult = std::move(SendResult), &JD](Error Err) mutable { 345 if (Err) 346 SendResult(std::move(Err)); 347 else 348 getInitializersLookupPhase(std::move(SendResult), JD); 349 }, 350 ES, std::move(NewInitSymbols)); 351 } 352 353 void ELFNixPlatform::rt_getInitializers(SendInitializerSequenceFn SendResult, 354 StringRef JDName) { 355 LLVM_DEBUG({ 356 dbgs() << "ELFNixPlatform::rt_getInitializers(\"" << JDName << "\")\n"; 357 }); 358 359 JITDylib *JD = ES.getJITDylibByName(JDName); 360 if (!JD) { 361 LLVM_DEBUG({ 362 dbgs() << " No such JITDylib \"" << JDName << "\". Sending error.\n"; 363 }); 364 SendResult(make_error<StringError>("No JITDylib named " + JDName, 365 inconvertibleErrorCode())); 366 return; 367 } 368 369 getInitializersLookupPhase(std::move(SendResult), *JD); 370 } 371 372 void ELFNixPlatform::rt_getDeinitializers( 373 SendDeinitializerSequenceFn SendResult, ExecutorAddr Handle) { 374 LLVM_DEBUG({ 375 dbgs() << "ELFNixPlatform::rt_getDeinitializers(\"" 376 << formatv("{0:x}", Handle.getValue()) << "\")\n"; 377 }); 378 379 JITDylib *JD = nullptr; 380 381 { 382 std::lock_guard<std::mutex> Lock(PlatformMutex); 383 auto I = HandleAddrToJITDylib.find(Handle); 384 if (I != HandleAddrToJITDylib.end()) 385 JD = I->second; 386 } 387 388 if (!JD) { 389 LLVM_DEBUG({ 390 dbgs() << " No JITDylib for handle " 391 << formatv("{0:x}", Handle.getValue()) << "\n"; 392 }); 393 SendResult(make_error<StringError>("No JITDylib associated with handle " + 394 formatv("{0:x}", Handle.getValue()), 395 inconvertibleErrorCode())); 396 return; 397 } 398 399 SendResult(ELFNixJITDylibDeinitializerSequence()); 400 } 401 402 void ELFNixPlatform::rt_lookupSymbol(SendSymbolAddressFn SendResult, 403 ExecutorAddr Handle, 404 StringRef SymbolName) { 405 LLVM_DEBUG({ 406 dbgs() << "ELFNixPlatform::rt_lookupSymbol(\"" 407 << formatv("{0:x}", Handle.getValue()) << "\")\n"; 408 }); 409 410 JITDylib *JD = nullptr; 411 412 { 413 std::lock_guard<std::mutex> Lock(PlatformMutex); 414 auto I = HandleAddrToJITDylib.find(Handle); 415 if (I != HandleAddrToJITDylib.end()) 416 JD = I->second; 417 } 418 419 if (!JD) { 420 LLVM_DEBUG({ 421 dbgs() << " No JITDylib for handle " 422 << formatv("{0:x}", Handle.getValue()) << "\n"; 423 }); 424 SendResult(make_error<StringError>("No JITDylib associated with handle " + 425 formatv("{0:x}", Handle.getValue()), 426 inconvertibleErrorCode())); 427 return; 428 } 429 430 // Use functor class to work around XL build compiler issue on AIX. 431 class RtLookupNotifyComplete { 432 public: 433 RtLookupNotifyComplete(SendSymbolAddressFn &&SendResult) 434 : SendResult(std::move(SendResult)) {} 435 void operator()(Expected<SymbolMap> Result) { 436 if (Result) { 437 assert(Result->size() == 1 && "Unexpected result map count"); 438 SendResult(ExecutorAddr(Result->begin()->second.getAddress())); 439 } else { 440 SendResult(Result.takeError()); 441 } 442 } 443 444 private: 445 SendSymbolAddressFn SendResult; 446 }; 447 448 ES.lookup( 449 LookupKind::DLSym, {{JD, JITDylibLookupFlags::MatchExportedSymbolsOnly}}, 450 SymbolLookupSet(ES.intern(SymbolName)), SymbolState::Ready, 451 RtLookupNotifyComplete(std::move(SendResult)), NoDependenciesToRegister); 452 } 453 454 Error ELFNixPlatform::bootstrapELFNixRuntime(JITDylib &PlatformJD) { 455 456 std::pair<const char *, ExecutorAddr *> Symbols[] = { 457 {"__orc_rt_elfnix_platform_bootstrap", &orc_rt_elfnix_platform_bootstrap}, 458 {"__orc_rt_elfnix_platform_shutdown", &orc_rt_elfnix_platform_shutdown}, 459 {"__orc_rt_elfnix_register_object_sections", 460 &orc_rt_elfnix_register_object_sections}, 461 {"__orc_rt_elfnix_create_pthread_key", 462 &orc_rt_elfnix_create_pthread_key}}; 463 464 SymbolLookupSet RuntimeSymbols; 465 std::vector<std::pair<SymbolStringPtr, ExecutorAddr *>> AddrsToRecord; 466 for (const auto &KV : Symbols) { 467 auto Name = ES.intern(KV.first); 468 RuntimeSymbols.add(Name); 469 AddrsToRecord.push_back({std::move(Name), KV.second}); 470 } 471 472 auto RuntimeSymbolAddrs = ES.lookup( 473 {{&PlatformJD, JITDylibLookupFlags::MatchAllSymbols}}, RuntimeSymbols); 474 if (!RuntimeSymbolAddrs) 475 return RuntimeSymbolAddrs.takeError(); 476 477 for (const auto &KV : AddrsToRecord) { 478 auto &Name = KV.first; 479 assert(RuntimeSymbolAddrs->count(Name) && "Missing runtime symbol?"); 480 KV.second->setValue((*RuntimeSymbolAddrs)[Name].getAddress()); 481 } 482 483 auto PJDDSOHandle = ES.lookup( 484 {{&PlatformJD, JITDylibLookupFlags::MatchAllSymbols}}, DSOHandleSymbol); 485 if (!PJDDSOHandle) 486 return PJDDSOHandle.takeError(); 487 488 if (auto Err = ES.callSPSWrapper<void(uint64_t)>( 489 orc_rt_elfnix_platform_bootstrap, PJDDSOHandle->getAddress())) 490 return Err; 491 492 // FIXME: Ordering is fuzzy here. We're probably best off saying 493 // "behavior is undefined if code that uses the runtime is added before 494 // the platform constructor returns", then move all this to the constructor. 495 RuntimeBootstrapped = true; 496 std::vector<ELFPerObjectSectionsToRegister> DeferredPOSRs; 497 { 498 std::lock_guard<std::mutex> Lock(PlatformMutex); 499 DeferredPOSRs = std::move(BootstrapPOSRs); 500 } 501 502 for (auto &D : DeferredPOSRs) 503 if (auto Err = registerPerObjectSections(D)) 504 return Err; 505 506 return Error::success(); 507 } 508 509 Error ELFNixPlatform::registerInitInfo( 510 JITDylib &JD, ArrayRef<jitlink::Section *> InitSections) { 511 512 std::unique_lock<std::mutex> Lock(PlatformMutex); 513 514 ELFNixJITDylibInitializers *InitSeq = nullptr; 515 { 516 auto I = InitSeqs.find(&JD); 517 if (I == InitSeqs.end()) { 518 // If there's no init sequence entry yet then we need to look up the 519 // header symbol to force creation of one. 520 Lock.unlock(); 521 522 auto SearchOrder = 523 JD.withLinkOrderDo([](const JITDylibSearchOrder &SO) { return SO; }); 524 if (auto Err = ES.lookup(SearchOrder, DSOHandleSymbol).takeError()) 525 return Err; 526 527 Lock.lock(); 528 I = InitSeqs.find(&JD); 529 assert(I != InitSeqs.end() && 530 "Entry missing after header symbol lookup?"); 531 } 532 InitSeq = &I->second; 533 } 534 535 for (auto *Sec : InitSections) { 536 // FIXME: Avoid copy here. 537 jitlink::SectionRange R(*Sec); 538 InitSeq->InitSections[Sec->getName()].push_back( 539 {ExecutorAddr(R.getStart()), ExecutorAddr(R.getEnd())}); 540 } 541 542 return Error::success(); 543 } 544 545 Error ELFNixPlatform::registerPerObjectSections( 546 const ELFPerObjectSectionsToRegister &POSR) { 547 548 if (!orc_rt_elfnix_register_object_sections) 549 return make_error<StringError>("Attempting to register per-object " 550 "sections, but runtime support has not " 551 "been loaded yet", 552 inconvertibleErrorCode()); 553 554 Error ErrResult = Error::success(); 555 if (auto Err = ES.callSPSWrapper<shared::SPSError( 556 SPSELFPerObjectSectionsToRegister)>( 557 orc_rt_elfnix_register_object_sections, ErrResult, POSR)) 558 return Err; 559 return ErrResult; 560 } 561 562 Expected<uint64_t> ELFNixPlatform::createPThreadKey() { 563 if (!orc_rt_elfnix_create_pthread_key) 564 return make_error<StringError>( 565 "Attempting to create pthread key in target, but runtime support has " 566 "not been loaded yet", 567 inconvertibleErrorCode()); 568 569 Expected<uint64_t> Result(0); 570 if (auto Err = ES.callSPSWrapper<SPSExpected<uint64_t>(void)>( 571 orc_rt_elfnix_create_pthread_key, Result)) 572 return std::move(Err); 573 return Result; 574 } 575 576 void ELFNixPlatform::ELFNixPlatformPlugin::modifyPassConfig( 577 MaterializationResponsibility &MR, jitlink::LinkGraph &LG, 578 jitlink::PassConfiguration &Config) { 579 580 // If the initializer symbol is the __dso_handle symbol then just add 581 // the DSO handle support passes. 582 if (MR.getInitializerSymbol() == MP.DSOHandleSymbol) { 583 addDSOHandleSupportPasses(MR, Config); 584 // The DSOHandle materialization unit doesn't require any other 585 // support, so we can bail out early. 586 return; 587 } 588 589 // If the object contains initializers then add passes to record them. 590 if (MR.getInitializerSymbol()) 591 addInitializerSupportPasses(MR, Config); 592 593 // Add passes for eh-frame and TLV support. 594 addEHAndTLVSupportPasses(MR, Config); 595 } 596 597 ObjectLinkingLayer::Plugin::SyntheticSymbolDependenciesMap 598 ELFNixPlatform::ELFNixPlatformPlugin::getSyntheticSymbolDependencies( 599 MaterializationResponsibility &MR) { 600 std::lock_guard<std::mutex> Lock(PluginMutex); 601 auto I = InitSymbolDeps.find(&MR); 602 if (I != InitSymbolDeps.end()) { 603 SyntheticSymbolDependenciesMap Result; 604 Result[MR.getInitializerSymbol()] = std::move(I->second); 605 InitSymbolDeps.erase(&MR); 606 return Result; 607 } 608 return SyntheticSymbolDependenciesMap(); 609 } 610 611 void ELFNixPlatform::ELFNixPlatformPlugin::addInitializerSupportPasses( 612 MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) { 613 614 /// Preserve init sections. 615 Config.PrePrunePasses.push_back([this, &MR](jitlink::LinkGraph &G) -> Error { 616 if (auto Err = preserveInitSections(G, MR)) 617 return Err; 618 return Error::success(); 619 }); 620 621 Config.PostFixupPasses.push_back( 622 [this, &JD = MR.getTargetJITDylib()](jitlink::LinkGraph &G) { 623 return registerInitSections(G, JD); 624 }); 625 } 626 627 void ELFNixPlatform::ELFNixPlatformPlugin::addDSOHandleSupportPasses( 628 MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) { 629 630 Config.PostAllocationPasses.push_back([this, &JD = MR.getTargetJITDylib()]( 631 jitlink::LinkGraph &G) -> Error { 632 auto I = llvm::find_if(G.defined_symbols(), [this](jitlink::Symbol *Sym) { 633 return Sym->getName() == *MP.DSOHandleSymbol; 634 }); 635 assert(I != G.defined_symbols().end() && "Missing DSO handle symbol"); 636 { 637 std::lock_guard<std::mutex> Lock(MP.PlatformMutex); 638 auto HandleAddr = (*I)->getAddress(); 639 MP.HandleAddrToJITDylib[HandleAddr] = &JD; 640 assert(!MP.InitSeqs.count(&JD) && "InitSeq entry for JD already exists"); 641 MP.InitSeqs.insert(std::make_pair( 642 &JD, ELFNixJITDylibInitializers(JD.getName(), HandleAddr))); 643 } 644 return Error::success(); 645 }); 646 } 647 648 void ELFNixPlatform::ELFNixPlatformPlugin::addEHAndTLVSupportPasses( 649 MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) { 650 651 // Insert TLV lowering at the start of the PostPrunePasses, since we want 652 // it to run before GOT/PLT lowering. 653 654 // TODO: Check that before the fixTLVSectionsAndEdges pass, the GOT/PLT build 655 // pass has done. Because the TLS descriptor need to be allocate in GOT. 656 Config.PostPrunePasses.push_back( 657 [this, &JD = MR.getTargetJITDylib()](jitlink::LinkGraph &G) { 658 return fixTLVSectionsAndEdges(G, JD); 659 }); 660 661 // Add a pass to register the final addresses of the eh-frame and TLV sections 662 // with the runtime. 663 Config.PostFixupPasses.push_back([this](jitlink::LinkGraph &G) -> Error { 664 ELFPerObjectSectionsToRegister POSR; 665 666 if (auto *EHFrameSection = G.findSectionByName(EHFrameSectionName)) { 667 jitlink::SectionRange R(*EHFrameSection); 668 if (!R.empty()) 669 POSR.EHFrameSection = {ExecutorAddr(R.getStart()), 670 ExecutorAddr(R.getEnd())}; 671 } 672 673 // Get a pointer to the thread data section if there is one. It will be used 674 // below. 675 jitlink::Section *ThreadDataSection = 676 G.findSectionByName(ThreadDataSectionName); 677 678 // Handle thread BSS section if there is one. 679 if (auto *ThreadBSSSection = G.findSectionByName(ThreadBSSSectionName)) { 680 // If there's already a thread data section in this graph then merge the 681 // thread BSS section content into it, otherwise just treat the thread 682 // BSS section as the thread data section. 683 if (ThreadDataSection) 684 G.mergeSections(*ThreadDataSection, *ThreadBSSSection); 685 else 686 ThreadDataSection = ThreadBSSSection; 687 } 688 689 // Having merged thread BSS (if present) and thread data (if present), 690 // record the resulting section range. 691 if (ThreadDataSection) { 692 jitlink::SectionRange R(*ThreadDataSection); 693 if (!R.empty()) 694 POSR.ThreadDataSection = {ExecutorAddr(R.getStart()), 695 ExecutorAddr(R.getEnd())}; 696 } 697 698 if (POSR.EHFrameSection.Start || POSR.ThreadDataSection.Start) { 699 700 // If we're still bootstrapping the runtime then just record this 701 // frame for now. 702 if (!MP.RuntimeBootstrapped) { 703 std::lock_guard<std::mutex> Lock(MP.PlatformMutex); 704 MP.BootstrapPOSRs.push_back(POSR); 705 return Error::success(); 706 } 707 708 // Otherwise register it immediately. 709 if (auto Err = MP.registerPerObjectSections(POSR)) 710 return Err; 711 } 712 713 return Error::success(); 714 }); 715 } 716 717 Error ELFNixPlatform::ELFNixPlatformPlugin::preserveInitSections( 718 jitlink::LinkGraph &G, MaterializationResponsibility &MR) { 719 720 JITLinkSymbolSet InitSectionSymbols; 721 for (auto &InitSectionName : InitSectionNames) { 722 // Skip non-init sections. 723 auto *InitSection = G.findSectionByName(InitSectionName); 724 if (!InitSection) 725 continue; 726 727 // Make a pass over live symbols in the section: those blocks are already 728 // preserved. 729 DenseSet<jitlink::Block *> AlreadyLiveBlocks; 730 for (auto &Sym : InitSection->symbols()) { 731 auto &B = Sym->getBlock(); 732 if (Sym->isLive() && Sym->getOffset() == 0 && 733 Sym->getSize() == B.getSize() && !AlreadyLiveBlocks.count(&B)) { 734 InitSectionSymbols.insert(Sym); 735 AlreadyLiveBlocks.insert(&B); 736 } 737 } 738 739 // Add anonymous symbols to preserve any not-already-preserved blocks. 740 for (auto *B : InitSection->blocks()) 741 if (!AlreadyLiveBlocks.count(B)) 742 InitSectionSymbols.insert( 743 &G.addAnonymousSymbol(*B, 0, B->getSize(), false, true)); 744 } 745 746 if (!InitSectionSymbols.empty()) { 747 std::lock_guard<std::mutex> Lock(PluginMutex); 748 InitSymbolDeps[&MR] = std::move(InitSectionSymbols); 749 } 750 751 return Error::success(); 752 } 753 754 Error ELFNixPlatform::ELFNixPlatformPlugin::registerInitSections( 755 jitlink::LinkGraph &G, JITDylib &JD) { 756 757 SmallVector<jitlink::Section *> InitSections; 758 759 LLVM_DEBUG({ dbgs() << "ELFNixPlatform::registerInitSections\n"; }); 760 761 for (auto InitSectionName : InitSectionNames) { 762 if (auto *Sec = G.findSectionByName(InitSectionName)) { 763 InitSections.push_back(Sec); 764 } 765 } 766 767 // Dump the scraped inits. 768 LLVM_DEBUG({ 769 dbgs() << "ELFNixPlatform: Scraped " << G.getName() << " init sections:\n"; 770 for (auto *Sec : InitSections) { 771 jitlink::SectionRange R(*Sec); 772 dbgs() << " " << Sec->getName() << ": " 773 << formatv("[ {0:x} -- {1:x} ]", R.getStart(), R.getEnd()) << "\n"; 774 } 775 }); 776 777 return MP.registerInitInfo(JD, InitSections); 778 } 779 780 Error ELFNixPlatform::ELFNixPlatformPlugin::fixTLVSectionsAndEdges( 781 jitlink::LinkGraph &G, JITDylib &JD) { 782 783 // TODO implement TLV support 784 for (auto *Sym : G.external_symbols()) 785 if (Sym->getName() == "__tls_get_addr") { 786 Sym->setName("___orc_rt_elfnix_tls_get_addr"); 787 } 788 789 auto *TLSInfoEntrySection = G.findSectionByName("$__TLSINFO"); 790 791 if (TLSInfoEntrySection) { 792 Optional<uint64_t> Key; 793 { 794 std::lock_guard<std::mutex> Lock(MP.PlatformMutex); 795 auto I = MP.JITDylibToPThreadKey.find(&JD); 796 if (I != MP.JITDylibToPThreadKey.end()) 797 Key = I->second; 798 } 799 if (!Key) { 800 if (auto KeyOrErr = MP.createPThreadKey()) 801 Key = *KeyOrErr; 802 else 803 return KeyOrErr.takeError(); 804 } 805 806 uint64_t PlatformKeyBits = 807 support::endian::byte_swap(*Key, G.getEndianness()); 808 809 for (auto *B : TLSInfoEntrySection->blocks()) { 810 // FIXME: The TLS descriptor byte length may different with different 811 // ISA 812 assert(B->getSize() == (G.getPointerSize() * 2) && 813 "TLS descriptor must be 2 words length"); 814 auto TLSInfoEntryContent = B->getMutableContent(G); 815 memcpy(TLSInfoEntryContent.data(), &PlatformKeyBits, G.getPointerSize()); 816 } 817 } 818 819 return Error::success(); 820 } 821 822 } // End namespace orc. 823 } // End namespace llvm. 824