1 //===- coff_platform.cpp --------------------------------------------------===// 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 // This file contains code required to load the rest of the COFF runtime. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #define NOMINMAX 14 #include <windows.h> 15 16 #include "coff_platform.h" 17 18 #include "debug.h" 19 #include "error.h" 20 #include "wrapper_function_utils.h" 21 22 #include <array> 23 #include <list> 24 #include <map> 25 #include <mutex> 26 #include <sstream> 27 #include <string_view> 28 #include <vector> 29 30 #define DEBUG_TYPE "coff_platform" 31 32 using namespace __orc_rt; 33 34 namespace __orc_rt { 35 36 using COFFJITDylibDepInfo = std::vector<ExecutorAddr>; 37 using COFFJITDylibDepInfoMap = 38 std::unordered_map<ExecutorAddr, COFFJITDylibDepInfo>; 39 40 using SPSCOFFObjectSectionsMap = 41 SPSSequence<SPSTuple<SPSString, SPSExecutorAddrRange>>; 42 43 using SPSCOFFJITDylibDepInfo = SPSSequence<SPSExecutorAddr>; 44 45 using SPSCOFFJITDylibDepInfoMap = 46 SPSSequence<SPSTuple<SPSExecutorAddr, SPSCOFFJITDylibDepInfo>>; 47 48 } // namespace __orc_rt 49 50 ORC_RT_JIT_DISPATCH_TAG(__orc_rt_coff_symbol_lookup_tag) 51 ORC_RT_JIT_DISPATCH_TAG(__orc_rt_coff_push_initializers_tag) 52 53 namespace { 54 class COFFPlatformRuntimeState { 55 private: 56 // Ctor/dtor section. 57 // Manage lists of *tor functions sorted by the last character of subsection 58 // name. 59 struct XtorSection { 60 void Register(char SubsectionChar, span<void (*)(void)> Xtors) { 61 Subsections[SubsectionChar - 'A'].push_back(Xtors); 62 SubsectionsNew[SubsectionChar - 'A'].push_back(Xtors); 63 } 64 65 void RegisterNoRun(char SubsectionChar, span<void (*)(void)> Xtors) { 66 Subsections[SubsectionChar - 'A'].push_back(Xtors); 67 } 68 69 void Reset() { SubsectionsNew = Subsections; } 70 71 void RunAllNewAndFlush(); 72 73 private: 74 std::array<std::vector<span<void (*)(void)>>, 26> Subsections; 75 std::array<std::vector<span<void (*)(void)>>, 26> SubsectionsNew; 76 }; 77 78 struct JITDylibState { 79 std::string Name; 80 void *Header = nullptr; 81 size_t LinkedAgainstRefCount = 0; 82 size_t DlRefCount = 0; 83 std::vector<JITDylibState *> Deps; 84 std::vector<void (*)(void)> AtExits; 85 XtorSection CInitSection; // XIA~XIZ 86 XtorSection CXXInitSection; // XCA~XCZ 87 XtorSection CPreTermSection; // XPA~XPZ 88 XtorSection CTermSection; // XTA~XTZ 89 90 bool referenced() const { 91 return LinkedAgainstRefCount != 0 || DlRefCount != 0; 92 } 93 }; 94 95 public: 96 static void initialize(); 97 static COFFPlatformRuntimeState &get(); 98 static bool isInitialized() { return CPS; } 99 static void destroy(); 100 101 COFFPlatformRuntimeState() = default; 102 103 // Delete copy and move constructors. 104 COFFPlatformRuntimeState(const COFFPlatformRuntimeState &) = delete; 105 COFFPlatformRuntimeState & 106 operator=(const COFFPlatformRuntimeState &) = delete; 107 COFFPlatformRuntimeState(COFFPlatformRuntimeState &&) = delete; 108 COFFPlatformRuntimeState &operator=(COFFPlatformRuntimeState &&) = delete; 109 110 const char *dlerror(); 111 void *dlopen(std::string_view Name, int Mode); 112 int dlclose(void *Header); 113 void *dlsym(void *Header, std::string_view Symbol); 114 115 Error registerJITDylib(std::string Name, void *Header); 116 Error deregisterJITDylib(void *Header); 117 118 Error registerAtExit(ExecutorAddr HeaderAddr, void (*AtExit)(void)); 119 120 Error registerObjectSections( 121 ExecutorAddr HeaderAddr, 122 std::vector<std::pair<std::string_view, ExecutorAddrRange>> Secs, 123 bool RunInitializers); 124 Error deregisterObjectSections( 125 ExecutorAddr HeaderAddr, 126 std::vector<std::pair<std::string_view, ExecutorAddrRange>> Secs); 127 128 void *findJITDylibBaseByPC(uint64_t PC); 129 130 private: 131 Error registerBlockRange(ExecutorAddr HeaderAddr, ExecutorAddrRange Range); 132 Error deregisterBlockRange(ExecutorAddr HeaderAddr, ExecutorAddrRange Range); 133 134 Error registerSEHFrames(ExecutorAddr HeaderAddr, 135 ExecutorAddrRange SEHFrameRange); 136 Error deregisterSEHFrames(ExecutorAddr HeaderAddr, 137 ExecutorAddrRange SEHFrameRange); 138 139 Expected<void *> dlopenImpl(std::string_view Path, int Mode); 140 Error dlopenFull(JITDylibState &JDS); 141 Error dlopenInitialize(JITDylibState &JDS, COFFJITDylibDepInfoMap &DepInfo); 142 143 Error dlcloseImpl(void *DSOHandle); 144 Error dlcloseDeinitialize(JITDylibState &JDS); 145 146 JITDylibState *getJITDylibStateByHeader(void *DSOHandle); 147 JITDylibState *getJITDylibStateByName(std::string_view Path); 148 Expected<ExecutorAddr> lookupSymbolInJITDylib(void *DSOHandle, 149 std::string_view Symbol); 150 151 static COFFPlatformRuntimeState *CPS; 152 153 std::recursive_mutex JDStatesMutex; 154 std::map<void *, JITDylibState> JDStates; 155 struct BlockRange { 156 void *Header; 157 size_t Size; 158 }; 159 std::map<void *, BlockRange> BlockRanges; 160 std::unordered_map<std::string_view, void *> JDNameToHeader; 161 std::string DLFcnError; 162 }; 163 164 } // namespace 165 166 COFFPlatformRuntimeState *COFFPlatformRuntimeState::CPS = nullptr; 167 168 COFFPlatformRuntimeState::JITDylibState * 169 COFFPlatformRuntimeState::getJITDylibStateByHeader(void *Header) { 170 auto I = JDStates.find(Header); 171 if (I == JDStates.end()) 172 return nullptr; 173 return &I->second; 174 } 175 176 COFFPlatformRuntimeState::JITDylibState * 177 COFFPlatformRuntimeState::getJITDylibStateByName(std::string_view Name) { 178 // FIXME: Avoid creating string copy here. 179 auto I = JDNameToHeader.find(std::string(Name.data(), Name.size())); 180 if (I == JDNameToHeader.end()) 181 return nullptr; 182 void *H = I->second; 183 auto J = JDStates.find(H); 184 assert(J != JDStates.end() && 185 "JITDylib has name map entry but no header map entry"); 186 return &J->second; 187 } 188 189 Error COFFPlatformRuntimeState::registerJITDylib(std::string Name, 190 void *Header) { 191 ORC_RT_DEBUG({ 192 printdbg("Registering JITDylib %s: Header = %p\n", Name.c_str(), Header); 193 }); 194 std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex); 195 if (JDStates.count(Header)) { 196 std::ostringstream ErrStream; 197 ErrStream << "Duplicate JITDylib registration for header " << Header 198 << " (name = " << Name << ")"; 199 return make_error<StringError>(ErrStream.str()); 200 } 201 if (JDNameToHeader.count(Name)) { 202 std::ostringstream ErrStream; 203 ErrStream << "Duplicate JITDylib registration for header " << Header 204 << " (header = " << Header << ")"; 205 return make_error<StringError>(ErrStream.str()); 206 } 207 208 auto &JDS = JDStates[Header]; 209 JDS.Name = std::move(Name); 210 JDS.Header = Header; 211 JDNameToHeader[JDS.Name] = Header; 212 return Error::success(); 213 } 214 215 Error COFFPlatformRuntimeState::deregisterJITDylib(void *Header) { 216 std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex); 217 auto I = JDStates.find(Header); 218 if (I == JDStates.end()) { 219 std::ostringstream ErrStream; 220 ErrStream << "Attempted to deregister unrecognized header " << Header; 221 return make_error<StringError>(ErrStream.str()); 222 } 223 224 // Remove std::string construction once we can use C++20. 225 auto J = JDNameToHeader.find( 226 std::string(I->second.Name.data(), I->second.Name.size())); 227 assert(J != JDNameToHeader.end() && 228 "Missing JDNameToHeader entry for JITDylib"); 229 230 ORC_RT_DEBUG({ 231 printdbg("Deregistering JITDylib %s: Header = %p\n", I->second.Name.c_str(), 232 Header); 233 }); 234 235 JDNameToHeader.erase(J); 236 JDStates.erase(I); 237 return Error::success(); 238 } 239 240 void COFFPlatformRuntimeState::XtorSection::RunAllNewAndFlush() { 241 for (auto &Subsection : SubsectionsNew) { 242 for (auto &XtorGroup : Subsection) 243 for (auto &Xtor : XtorGroup) 244 if (Xtor) 245 Xtor(); 246 Subsection.clear(); 247 } 248 } 249 250 const char *COFFPlatformRuntimeState::dlerror() { return DLFcnError.c_str(); } 251 252 void *COFFPlatformRuntimeState::dlopen(std::string_view Path, int Mode) { 253 ORC_RT_DEBUG({ 254 std::string S(Path.data(), Path.size()); 255 printdbg("COFFPlatform::dlopen(\"%s\")\n", S.c_str()); 256 }); 257 std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex); 258 if (auto H = dlopenImpl(Path, Mode)) 259 return *H; 260 else { 261 // FIXME: Make dlerror thread safe. 262 DLFcnError = toString(H.takeError()); 263 return nullptr; 264 } 265 } 266 267 int COFFPlatformRuntimeState::dlclose(void *DSOHandle) { 268 ORC_RT_DEBUG({ 269 auto *JDS = getJITDylibStateByHeader(DSOHandle); 270 std::string DylibName; 271 if (JDS) { 272 std::string S; 273 printdbg("COFFPlatform::dlclose(%p) (%s)\n", DSOHandle, S.c_str()); 274 } else 275 printdbg("COFFPlatform::dlclose(%p) (%s)\n", DSOHandle, "invalid handle"); 276 }); 277 std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex); 278 if (auto Err = dlcloseImpl(DSOHandle)) { 279 // FIXME: Make dlerror thread safe. 280 DLFcnError = toString(std::move(Err)); 281 return -1; 282 } 283 return 0; 284 } 285 286 void *COFFPlatformRuntimeState::dlsym(void *Header, std::string_view Symbol) { 287 auto Addr = lookupSymbolInJITDylib(Header, Symbol); 288 if (!Addr) { 289 return 0; 290 } 291 292 return Addr->toPtr<void *>(); 293 } 294 295 Expected<void *> COFFPlatformRuntimeState::dlopenImpl(std::string_view Path, 296 int Mode) { 297 // Try to find JITDylib state by name. 298 auto *JDS = getJITDylibStateByName(Path); 299 300 if (!JDS) 301 return make_error<StringError>("No registered JTIDylib for path " + 302 std::string(Path.data(), Path.size())); 303 304 if (auto Err = dlopenFull(*JDS)) 305 return std::move(Err); 306 307 // Bump the ref-count on this dylib. 308 ++JDS->DlRefCount; 309 310 // Return the header address. 311 return JDS->Header; 312 } 313 314 Error COFFPlatformRuntimeState::dlopenFull(JITDylibState &JDS) { 315 // Call back to the JIT to push the initializers. 316 Expected<COFFJITDylibDepInfoMap> DepInfoMap((COFFJITDylibDepInfoMap())); 317 if (auto Err = WrapperFunction<SPSExpected<SPSCOFFJITDylibDepInfoMap>( 318 SPSExecutorAddr)>::call(&__orc_rt_coff_push_initializers_tag, 319 DepInfoMap, 320 ExecutorAddr::fromPtr(JDS.Header))) 321 return Err; 322 if (!DepInfoMap) 323 return DepInfoMap.takeError(); 324 325 if (auto Err = dlopenInitialize(JDS, *DepInfoMap)) 326 return Err; 327 328 if (!DepInfoMap->empty()) { 329 ORC_RT_DEBUG({ 330 printdbg("Unrecognized dep-info key headers in dlopen of %s\n", 331 JDS.Name.c_str()); 332 }); 333 std::ostringstream ErrStream; 334 ErrStream << "Encountered unrecognized dep-info key headers " 335 "while processing dlopen of " 336 << JDS.Name; 337 return make_error<StringError>(ErrStream.str()); 338 } 339 340 return Error::success(); 341 } 342 343 Error COFFPlatformRuntimeState::dlopenInitialize( 344 JITDylibState &JDS, COFFJITDylibDepInfoMap &DepInfo) { 345 ORC_RT_DEBUG({ 346 printdbg("COFFPlatformRuntimeState::dlopenInitialize(\"%s\")\n", 347 JDS.Name.c_str()); 348 }); 349 350 // Skip visited dependency. 351 auto I = DepInfo.find(ExecutorAddr::fromPtr(JDS.Header)); 352 if (I == DepInfo.end()) 353 return Error::success(); 354 355 auto DI = std::move(I->second); 356 DepInfo.erase(I); 357 358 // Run initializers of dependencies in proper order by depth-first traversal 359 // of dependency graph. 360 std::vector<JITDylibState *> OldDeps; 361 std::swap(JDS.Deps, OldDeps); 362 JDS.Deps.reserve(DI.size()); 363 for (auto DepHeaderAddr : DI) { 364 auto *DepJDS = getJITDylibStateByHeader(DepHeaderAddr.toPtr<void *>()); 365 if (!DepJDS) { 366 std::ostringstream ErrStream; 367 ErrStream << "Encountered unrecognized dep header " 368 << DepHeaderAddr.toPtr<void *>() << " while initializing " 369 << JDS.Name; 370 return make_error<StringError>(ErrStream.str()); 371 } 372 ++DepJDS->LinkedAgainstRefCount; 373 if (auto Err = dlopenInitialize(*DepJDS, DepInfo)) 374 return Err; 375 } 376 377 // Run static initializers. 378 JDS.CInitSection.RunAllNewAndFlush(); 379 JDS.CXXInitSection.RunAllNewAndFlush(); 380 381 // Decrement old deps. 382 for (auto *DepJDS : OldDeps) { 383 --DepJDS->LinkedAgainstRefCount; 384 if (!DepJDS->referenced()) 385 if (auto Err = dlcloseDeinitialize(*DepJDS)) 386 return Err; 387 } 388 389 return Error::success(); 390 } 391 392 Error COFFPlatformRuntimeState::dlcloseImpl(void *DSOHandle) { 393 // Try to find JITDylib state by header. 394 auto *JDS = getJITDylibStateByHeader(DSOHandle); 395 396 if (!JDS) { 397 std::ostringstream ErrStream; 398 ErrStream << "No registered JITDylib for " << DSOHandle; 399 return make_error<StringError>(ErrStream.str()); 400 } 401 402 // Bump the ref-count. 403 --JDS->DlRefCount; 404 405 if (!JDS->referenced()) 406 return dlcloseDeinitialize(*JDS); 407 408 return Error::success(); 409 } 410 411 Error COFFPlatformRuntimeState::dlcloseDeinitialize(JITDylibState &JDS) { 412 ORC_RT_DEBUG({ 413 printdbg("COFFPlatformRuntimeState::dlcloseDeinitialize(\"%s\")\n", 414 JDS.Name.c_str()); 415 }); 416 417 // Run atexits 418 for (auto AtExit : JDS.AtExits) 419 AtExit(); 420 JDS.AtExits.clear(); 421 422 // Run static terminators. 423 JDS.CPreTermSection.RunAllNewAndFlush(); 424 JDS.CTermSection.RunAllNewAndFlush(); 425 426 // Queue all xtors as new again. 427 JDS.CInitSection.Reset(); 428 JDS.CXXInitSection.Reset(); 429 JDS.CPreTermSection.Reset(); 430 JDS.CTermSection.Reset(); 431 432 // Deinitialize any dependencies. 433 for (auto *DepJDS : JDS.Deps) { 434 --DepJDS->LinkedAgainstRefCount; 435 if (!DepJDS->referenced()) 436 if (auto Err = dlcloseDeinitialize(*DepJDS)) 437 return Err; 438 } 439 440 return Error::success(); 441 } 442 443 Expected<ExecutorAddr> 444 COFFPlatformRuntimeState::lookupSymbolInJITDylib(void *header, 445 std::string_view Sym) { 446 Expected<ExecutorAddr> Result((ExecutorAddr())); 447 if (auto Err = WrapperFunction<SPSExpected<SPSExecutorAddr>( 448 SPSExecutorAddr, SPSString)>::call(&__orc_rt_coff_symbol_lookup_tag, 449 Result, 450 ExecutorAddr::fromPtr(header), 451 Sym)) 452 return std::move(Err); 453 return Result; 454 } 455 456 Error COFFPlatformRuntimeState::registerObjectSections( 457 ExecutorAddr HeaderAddr, 458 std::vector<std::pair<std::string_view, ExecutorAddrRange>> Secs, 459 bool RunInitializers) { 460 std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex); 461 auto I = JDStates.find(HeaderAddr.toPtr<void *>()); 462 if (I == JDStates.end()) { 463 std::ostringstream ErrStream; 464 ErrStream << "Unrecognized header " << HeaderAddr.getValue(); 465 return make_error<StringError>(ErrStream.str()); 466 } 467 auto &JDState = I->second; 468 for (auto &KV : Secs) { 469 if (auto Err = registerBlockRange(HeaderAddr, KV.second)) 470 return Err; 471 if (KV.first.empty()) 472 continue; 473 char LastChar = KV.first.data()[KV.first.size() - 1]; 474 if (KV.first == ".pdata") { 475 if (auto Err = registerSEHFrames(HeaderAddr, KV.second)) 476 return Err; 477 } else if (KV.first >= ".CRT$XIA" && KV.first <= ".CRT$XIZ") { 478 if (RunInitializers) 479 JDState.CInitSection.Register(LastChar, 480 KV.second.toSpan<void (*)(void)>()); 481 else 482 JDState.CInitSection.RegisterNoRun(LastChar, 483 KV.second.toSpan<void (*)(void)>()); 484 } else if (KV.first >= ".CRT$XCA" && KV.first <= ".CRT$XCZ") { 485 if (RunInitializers) 486 JDState.CXXInitSection.Register(LastChar, 487 KV.second.toSpan<void (*)(void)>()); 488 else 489 JDState.CXXInitSection.RegisterNoRun( 490 LastChar, KV.second.toSpan<void (*)(void)>()); 491 } else if (KV.first >= ".CRT$XPA" && KV.first <= ".CRT$XPZ") 492 JDState.CPreTermSection.Register(LastChar, 493 KV.second.toSpan<void (*)(void)>()); 494 else if (KV.first >= ".CRT$XTA" && KV.first <= ".CRT$XTZ") 495 JDState.CTermSection.Register(LastChar, 496 KV.second.toSpan<void (*)(void)>()); 497 } 498 return Error::success(); 499 } 500 501 Error COFFPlatformRuntimeState::deregisterObjectSections( 502 ExecutorAddr HeaderAddr, 503 std::vector<std::pair<std::string_view, ExecutorAddrRange>> Secs) { 504 std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex); 505 auto I = JDStates.find(HeaderAddr.toPtr<void *>()); 506 if (I == JDStates.end()) { 507 std::ostringstream ErrStream; 508 ErrStream << "Attempted to deregister unrecognized header " 509 << HeaderAddr.getValue(); 510 return make_error<StringError>(ErrStream.str()); 511 } 512 auto &JDState = I->second; 513 for (auto &KV : Secs) { 514 if (auto Err = deregisterBlockRange(HeaderAddr, KV.second)) 515 return Err; 516 if (KV.first == ".pdata") 517 if (auto Err = deregisterSEHFrames(HeaderAddr, KV.second)) 518 return Err; 519 } 520 return Error::success(); 521 } 522 523 Error COFFPlatformRuntimeState::registerSEHFrames( 524 ExecutorAddr HeaderAddr, ExecutorAddrRange SEHFrameRange) { 525 int N = (SEHFrameRange.End.getValue() - SEHFrameRange.Start.getValue()) / 526 sizeof(RUNTIME_FUNCTION); 527 auto Func = SEHFrameRange.Start.toPtr<PRUNTIME_FUNCTION>(); 528 if (!RtlAddFunctionTable(Func, N, 529 static_cast<DWORD64>(HeaderAddr.getValue()))) 530 return make_error<StringError>("Failed to register SEH frames"); 531 return Error::success(); 532 } 533 534 Error COFFPlatformRuntimeState::deregisterSEHFrames( 535 ExecutorAddr HeaderAddr, ExecutorAddrRange SEHFrameRange) { 536 if (!RtlDeleteFunctionTable(SEHFrameRange.Start.toPtr<PRUNTIME_FUNCTION>())) 537 return make_error<StringError>("Failed to deregister SEH frames"); 538 return Error::success(); 539 } 540 541 Error COFFPlatformRuntimeState::registerBlockRange(ExecutorAddr HeaderAddr, 542 ExecutorAddrRange Range) { 543 assert(!BlockRanges.count(Range.Start.toPtr<void *>()) && 544 "Block range address already registered"); 545 BlockRange B = {HeaderAddr.toPtr<void *>(), Range.size()}; 546 BlockRanges.emplace(Range.Start.toPtr<void *>(), B); 547 return Error::success(); 548 } 549 550 Error COFFPlatformRuntimeState::deregisterBlockRange(ExecutorAddr HeaderAddr, 551 ExecutorAddrRange Range) { 552 assert(BlockRanges.count(Range.Start.toPtr<void *>()) && 553 "Block range address not registered"); 554 BlockRanges.erase(Range.Start.toPtr<void *>()); 555 return Error::success(); 556 } 557 558 Error COFFPlatformRuntimeState::registerAtExit(ExecutorAddr HeaderAddr, 559 void (*AtExit)(void)) { 560 std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex); 561 auto I = JDStates.find(HeaderAddr.toPtr<void *>()); 562 if (I == JDStates.end()) { 563 std::ostringstream ErrStream; 564 ErrStream << "Unrecognized header " << HeaderAddr.getValue(); 565 return make_error<StringError>(ErrStream.str()); 566 } 567 I->second.AtExits.push_back(AtExit); 568 return Error::success(); 569 } 570 571 void COFFPlatformRuntimeState::initialize() { 572 assert(!CPS && "COFFPlatformRuntimeState should be null"); 573 CPS = new COFFPlatformRuntimeState(); 574 } 575 576 COFFPlatformRuntimeState &COFFPlatformRuntimeState::get() { 577 assert(CPS && "COFFPlatformRuntimeState not initialized"); 578 return *CPS; 579 } 580 581 void COFFPlatformRuntimeState::destroy() { 582 assert(CPS && "COFFPlatformRuntimeState not initialized"); 583 delete CPS; 584 } 585 586 void *COFFPlatformRuntimeState::findJITDylibBaseByPC(uint64_t PC) { 587 std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex); 588 auto It = BlockRanges.upper_bound(reinterpret_cast<void *>(PC)); 589 if (It == BlockRanges.begin()) 590 return nullptr; 591 --It; 592 auto &Range = It->second; 593 if (PC >= reinterpret_cast<uint64_t>(It->first) + Range.Size) 594 return nullptr; 595 return Range.Header; 596 } 597 598 ORC_RT_INTERFACE __orc_rt_CWrapperFunctionResult 599 __orc_rt_coff_platform_bootstrap(char *ArgData, size_t ArgSize) { 600 COFFPlatformRuntimeState::initialize(); 601 return WrapperFunctionResult().release(); 602 } 603 604 ORC_RT_INTERFACE __orc_rt_CWrapperFunctionResult 605 __orc_rt_coff_platform_shutdown(char *ArgData, size_t ArgSize) { 606 COFFPlatformRuntimeState::destroy(); 607 return WrapperFunctionResult().release(); 608 } 609 610 ORC_RT_INTERFACE __orc_rt_CWrapperFunctionResult 611 __orc_rt_coff_register_jitdylib(char *ArgData, size_t ArgSize) { 612 return WrapperFunction<SPSError(SPSString, SPSExecutorAddr)>::handle( 613 ArgData, ArgSize, 614 [](std::string &Name, ExecutorAddr HeaderAddr) { 615 return COFFPlatformRuntimeState::get().registerJITDylib( 616 std::move(Name), HeaderAddr.toPtr<void *>()); 617 }) 618 .release(); 619 } 620 621 ORC_RT_INTERFACE __orc_rt_CWrapperFunctionResult 622 __orc_rt_coff_deregister_jitdylib(char *ArgData, size_t ArgSize) { 623 return WrapperFunction<SPSError(SPSExecutorAddr)>::handle( 624 ArgData, ArgSize, 625 [](ExecutorAddr HeaderAddr) { 626 return COFFPlatformRuntimeState::get().deregisterJITDylib( 627 HeaderAddr.toPtr<void *>()); 628 }) 629 .release(); 630 } 631 632 ORC_RT_INTERFACE __orc_rt_CWrapperFunctionResult 633 __orc_rt_coff_register_object_sections(char *ArgData, size_t ArgSize) { 634 return WrapperFunction<SPSError(SPSExecutorAddr, SPSCOFFObjectSectionsMap, 635 bool)>:: 636 handle(ArgData, ArgSize, 637 [](ExecutorAddr HeaderAddr, 638 std::vector<std::pair<std::string_view, ExecutorAddrRange>> 639 &Secs, 640 bool RunInitializers) { 641 return COFFPlatformRuntimeState::get().registerObjectSections( 642 HeaderAddr, std::move(Secs), RunInitializers); 643 }) 644 .release(); 645 } 646 647 ORC_RT_INTERFACE __orc_rt_CWrapperFunctionResult 648 __orc_rt_coff_deregister_object_sections(char *ArgData, size_t ArgSize) { 649 return WrapperFunction<SPSError(SPSExecutorAddr, SPSCOFFObjectSectionsMap)>:: 650 handle(ArgData, ArgSize, 651 [](ExecutorAddr HeaderAddr, 652 std::vector<std::pair<std::string_view, ExecutorAddrRange>> 653 &Secs) { 654 return COFFPlatformRuntimeState::get().deregisterObjectSections( 655 HeaderAddr, std::move(Secs)); 656 }) 657 .release(); 658 } 659 //------------------------------------------------------------------------------ 660 // JIT'd dlfcn alternatives. 661 //------------------------------------------------------------------------------ 662 663 const char *__orc_rt_coff_jit_dlerror() { 664 return COFFPlatformRuntimeState::get().dlerror(); 665 } 666 667 void *__orc_rt_coff_jit_dlopen(const char *path, int mode) { 668 return COFFPlatformRuntimeState::get().dlopen(path, mode); 669 } 670 671 int __orc_rt_coff_jit_dlclose(void *header) { 672 return COFFPlatformRuntimeState::get().dlclose(header); 673 } 674 675 void *__orc_rt_coff_jit_dlsym(void *header, const char *symbol) { 676 return COFFPlatformRuntimeState::get().dlsym(header, symbol); 677 } 678 679 //------------------------------------------------------------------------------ 680 // COFF SEH exception support 681 //------------------------------------------------------------------------------ 682 683 struct ThrowInfo { 684 uint32_t attributes; 685 void *data; 686 }; 687 688 ORC_RT_INTERFACE void __stdcall __orc_rt_coff_cxx_throw_exception( 689 void *pExceptionObject, ThrowInfo *pThrowInfo) { 690 constexpr uint32_t EH_EXCEPTION_NUMBER = 'msc' | 0xE0000000; 691 constexpr uint32_t EH_MAGIC_NUMBER1 = 0x19930520; 692 auto BaseAddr = COFFPlatformRuntimeState::get().findJITDylibBaseByPC( 693 reinterpret_cast<uint64_t>(pThrowInfo)); 694 if (!BaseAddr) { 695 // This is not from JIT'd region. 696 // FIXME: Use the default implementation like below when alias api is 697 // capable. _CxxThrowException(pExceptionObject, pThrowInfo); 698 fprintf(stderr, "Throwing exception from compiled callback into JIT'd " 699 "exception handler not supported yet.\n"); 700 abort(); 701 return; 702 } 703 const ULONG_PTR parameters[] = { 704 EH_MAGIC_NUMBER1, 705 reinterpret_cast<ULONG_PTR>(pExceptionObject), 706 reinterpret_cast<ULONG_PTR>(pThrowInfo), 707 reinterpret_cast<ULONG_PTR>(BaseAddr), 708 }; 709 RaiseException(EH_EXCEPTION_NUMBER, EXCEPTION_NONCONTINUABLE, 710 _countof(parameters), parameters); 711 } 712 713 //------------------------------------------------------------------------------ 714 // COFF atexits 715 //------------------------------------------------------------------------------ 716 717 typedef int (*OnExitFunction)(void); 718 typedef void (*AtExitFunction)(void); 719 720 ORC_RT_INTERFACE OnExitFunction __orc_rt_coff_onexit(void *Header, 721 OnExitFunction Func) { 722 if (auto Err = COFFPlatformRuntimeState::get().registerAtExit( 723 ExecutorAddr::fromPtr(Header), (void (*)(void))Func)) { 724 consumeError(std::move(Err)); 725 return nullptr; 726 } 727 return Func; 728 } 729 730 ORC_RT_INTERFACE int __orc_rt_coff_atexit(void *Header, AtExitFunction Func) { 731 if (auto Err = COFFPlatformRuntimeState::get().registerAtExit( 732 ExecutorAddr::fromPtr(Header), (void (*)(void))Func)) { 733 consumeError(std::move(Err)); 734 return -1; 735 } 736 return 0; 737 } 738 739 //------------------------------------------------------------------------------ 740 // COFF Run Program 741 //------------------------------------------------------------------------------ 742 743 ORC_RT_INTERFACE int64_t __orc_rt_coff_run_program(const char *JITDylibName, 744 const char *EntrySymbolName, 745 int argc, char *argv[]) { 746 using MainTy = int (*)(int, char *[]); 747 748 void *H = 749 __orc_rt_coff_jit_dlopen(JITDylibName, __orc_rt::coff::ORC_RT_RTLD_LAZY); 750 if (!H) { 751 __orc_rt_log_error(__orc_rt_coff_jit_dlerror()); 752 return -1; 753 } 754 755 auto *Main = 756 reinterpret_cast<MainTy>(__orc_rt_coff_jit_dlsym(H, EntrySymbolName)); 757 758 if (!Main) { 759 __orc_rt_log_error(__orc_rt_coff_jit_dlerror()); 760 return -1; 761 } 762 763 int Result = Main(argc, argv); 764 765 if (__orc_rt_coff_jit_dlclose(H) == -1) 766 __orc_rt_log_error(__orc_rt_coff_jit_dlerror()); 767 768 return Result; 769 } 770