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