xref: /llvm-project/compiler-rt/lib/orc/resolve.cpp (revision f2d18a4d00c5f5dea436b4f7b62ec5c87b98eac2)
1*f2d18a4dSLang Hames //===- resolve.cpp --------------------------------------------------------===//
2*f2d18a4dSLang Hames //
3*f2d18a4dSLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*f2d18a4dSLang Hames // See https://llvm.org/LICENSE.txt for license information.
5*f2d18a4dSLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*f2d18a4dSLang Hames //
7*f2d18a4dSLang Hames //===----------------------------------------------------------------------===//
8*f2d18a4dSLang Hames //
9*f2d18a4dSLang Hames // This file contains a generic "resolver" function compatible with the
10*f2d18a4dSLang Hames // __orc_rt_reenter function.
11*f2d18a4dSLang Hames //
12*f2d18a4dSLang Hames //===----------------------------------------------------------------------===//
13*f2d18a4dSLang Hames 
14*f2d18a4dSLang Hames #include "executor_symbol_def.h"
15*f2d18a4dSLang Hames #include "jit_dispatch.h"
16*f2d18a4dSLang Hames #include "wrapper_function_utils.h"
17*f2d18a4dSLang Hames 
18*f2d18a4dSLang Hames #include <stdio.h>
19*f2d18a4dSLang Hames 
20*f2d18a4dSLang Hames #define DEBUG_TYPE "resolve"
21*f2d18a4dSLang Hames 
22*f2d18a4dSLang Hames using namespace orc_rt;
23*f2d18a4dSLang Hames 
24*f2d18a4dSLang Hames // Declare function tags for functions in the JIT process.
25*f2d18a4dSLang Hames ORC_RT_JIT_DISPATCH_TAG(__orc_rt_resolve_tag)
26*f2d18a4dSLang Hames 
27*f2d18a4dSLang Hames // FIXME: Make this configurable via an alias.
28*f2d18a4dSLang Hames static void __orc_rt_resolve_fail(void *Caller, const char *ErrMsg) {
29*f2d18a4dSLang Hames   fprintf(stderr, "error resolving implementation for stub %p: %s\n", Caller,
30*f2d18a4dSLang Hames           ErrMsg);
31*f2d18a4dSLang Hames   abort();
32*f2d18a4dSLang Hames }
33*f2d18a4dSLang Hames 
34*f2d18a4dSLang Hames extern "C" ORC_RT_HIDDEN void *__orc_rt_resolve(void *Caller) {
35*f2d18a4dSLang Hames   Expected<ExecutorSymbolDef> Result((ExecutorSymbolDef()));
36*f2d18a4dSLang Hames   if (auto Err = WrapperFunction<SPSExpected<SPSExecutorSymbolDef>(
37*f2d18a4dSLang Hames           SPSExecutorAddr)>::call(JITDispatch(&__orc_rt_resolve_tag), Result,
38*f2d18a4dSLang Hames                                   ExecutorAddr::fromPtr(Caller))) {
39*f2d18a4dSLang Hames     __orc_rt_resolve_fail(Caller, toString(std::move(Err)).c_str());
40*f2d18a4dSLang Hames     return nullptr; // Unreachable.
41*f2d18a4dSLang Hames   }
42*f2d18a4dSLang Hames 
43*f2d18a4dSLang Hames   if (!Result) {
44*f2d18a4dSLang Hames     __orc_rt_resolve_fail(Caller, toString(Result.takeError()).c_str());
45*f2d18a4dSLang Hames     return nullptr; // Unreachable.
46*f2d18a4dSLang Hames   }
47*f2d18a4dSLang Hames 
48*f2d18a4dSLang Hames   return Result->getAddress().toPtr<void *>();
49*f2d18a4dSLang Hames }
50