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