1 //===------------------------ OrcRTBootstrap.cpp --------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "OrcRTBootstrap.h"
11 #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
12 #include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
13 #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
14 #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
16 #define DEBUG_TYPE "orc"
18 using namespace llvm::orc::shared
;
22 namespace rt_bootstrap
{
24 template <typename WriteT
, typename SPSWriteT
>
25 static llvm::orc::shared::CWrapperFunctionResult
26 writeUIntsWrapper(const char *ArgData
, size_t ArgSize
) {
27 return WrapperFunction
<void(SPSSequence
<SPSWriteT
>)>::handle(
29 [](std::vector
<WriteT
> Ws
) {
31 *W
.Addr
.template toPtr
<decltype(W
.Value
) *>() = W
.Value
;
36 static llvm::orc::shared::CWrapperFunctionResult
37 writeBuffersWrapper(const char *ArgData
, size_t ArgSize
) {
38 return WrapperFunction
<void(SPSSequence
<SPSMemoryAccessBufferWrite
>)>::handle(
40 [](std::vector
<tpctypes::BufferWrite
> Ws
) {
42 memcpy(W
.Addr
.template toPtr
<char *>(), W
.Buffer
.data(),
48 static llvm::orc::shared::CWrapperFunctionResult
49 writePointersWrapper(const char *ArgData
, size_t ArgSize
) {
50 return WrapperFunction
<void(SPSSequence
<SPSMemoryAccessPointerWrite
>)>::
51 handle(ArgData
, ArgSize
,
52 [](std::vector
<tpctypes::PointerWrite
> Ws
) {
54 *W
.Addr
.template toPtr
<void **>() =
55 W
.Value
.template toPtr
<void *>();
60 static llvm::orc::shared::CWrapperFunctionResult
61 runAsMainWrapper(const char *ArgData
, size_t ArgSize
) {
62 return WrapperFunction
<rt::SPSRunAsMainSignature
>::handle(
64 [](ExecutorAddr MainAddr
,
65 std::vector
<std::string
> Args
) -> int64_t {
66 return runAsMain(MainAddr
.toPtr
<int (*)(int, char *[])>(), Args
);
71 static llvm::orc::shared::CWrapperFunctionResult
72 runAsVoidFunctionWrapper(const char *ArgData
, size_t ArgSize
) {
73 return WrapperFunction
<rt::SPSRunAsVoidFunctionSignature
>::handle(
75 [](ExecutorAddr MainAddr
) -> int32_t {
76 return runAsVoidFunction(MainAddr
.toPtr
<int32_t (*)(void)>());
81 static llvm::orc::shared::CWrapperFunctionResult
82 runAsIntFunctionWrapper(const char *ArgData
, size_t ArgSize
) {
83 return WrapperFunction
<rt::SPSRunAsIntFunctionSignature
>::handle(
85 [](ExecutorAddr MainAddr
, int32_t Arg
) -> int32_t {
86 return runAsIntFunction(MainAddr
.toPtr
<int32_t (*)(int32_t)>(),
92 void addTo(StringMap
<ExecutorAddr
> &M
) {
93 M
[rt::MemoryWriteUInt8sWrapperName
] = ExecutorAddr::fromPtr(
94 &writeUIntsWrapper
<tpctypes::UInt8Write
,
95 shared::SPSMemoryAccessUInt8Write
>);
96 M
[rt::MemoryWriteUInt16sWrapperName
] = ExecutorAddr::fromPtr(
97 &writeUIntsWrapper
<tpctypes::UInt16Write
,
98 shared::SPSMemoryAccessUInt16Write
>);
99 M
[rt::MemoryWriteUInt32sWrapperName
] = ExecutorAddr::fromPtr(
100 &writeUIntsWrapper
<tpctypes::UInt32Write
,
101 shared::SPSMemoryAccessUInt32Write
>);
102 M
[rt::MemoryWriteUInt64sWrapperName
] = ExecutorAddr::fromPtr(
103 &writeUIntsWrapper
<tpctypes::UInt64Write
,
104 shared::SPSMemoryAccessUInt64Write
>);
105 M
[rt::MemoryWriteBuffersWrapperName
] =
106 ExecutorAddr::fromPtr(&writeBuffersWrapper
);
107 M
[rt::MemoryWritePointersWrapperName
] =
108 ExecutorAddr::fromPtr(&writePointersWrapper
);
109 M
[rt::RegisterEHFrameSectionWrapperName
] =
110 ExecutorAddr::fromPtr(&llvm_orc_registerEHFrameSectionWrapper
);
111 M
[rt::DeregisterEHFrameSectionWrapperName
] =
112 ExecutorAddr::fromPtr(&llvm_orc_deregisterEHFrameSectionWrapper
);
113 M
[rt::RunAsMainWrapperName
] = ExecutorAddr::fromPtr(&runAsMainWrapper
);
114 M
[rt::RunAsVoidFunctionWrapperName
] =
115 ExecutorAddr::fromPtr(&runAsVoidFunctionWrapper
);
116 M
[rt::RunAsIntFunctionWrapperName
] =
117 ExecutorAddr::fromPtr(&runAsIntFunctionWrapper
);
120 } // end namespace rt_bootstrap
121 } // end namespace orc
122 } // end namespace llvm