1 //===------------------------ OrcRTBootstrap.cpp --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "OrcRTBootstrap.h"
10 
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"
15 
16 #define DEBUG_TYPE "orc"
17 
18 using namespace llvm::orc::shared;
19 
20 namespace llvm {
21 namespace orc {
22 namespace rt_bootstrap {
23 
24 template <typename WriteT, typename SPSWriteT>
25 static llvm::orc::shared::detail::CWrapperFunctionResult
26 writeUIntsWrapper(const char *ArgData, size_t ArgSize) {
27   return WrapperFunction<void(SPSSequence<SPSWriteT>)>::handle(
28              ArgData, ArgSize,
29              [](std::vector<WriteT> Ws) {
30                for (auto &W : Ws)
31                  *jitTargetAddressToPointer<decltype(W.Value) *>(W.Address) =
32                      W.Value;
33              })
34       .release();
35 }
36 
37 static llvm::orc::shared::detail::CWrapperFunctionResult
38 writeBuffersWrapper(const char *ArgData, size_t ArgSize) {
39   return WrapperFunction<void(SPSSequence<SPSMemoryAccessBufferWrite>)>::handle(
40              ArgData, ArgSize,
41              [](std::vector<tpctypes::BufferWrite> Ws) {
42                for (auto &W : Ws)
43                  memcpy(jitTargetAddressToPointer<char *>(W.Address),
44                         W.Buffer.data(), W.Buffer.size());
45              })
46       .release();
47 }
48 
49 static llvm::orc::shared::detail::CWrapperFunctionResult
50 runAsMainWrapper(const char *ArgData, size_t ArgSize) {
51   return WrapperFunction<rt::SPSRunAsMainSignature>::handle(
52              ArgData, ArgSize,
53              [](ExecutorAddr MainAddr,
54                 std::vector<std::string> Args) -> int64_t {
55                return runAsMain(MainAddr.toPtr<int (*)(int, char *[])>(), Args);
56              })
57       .release();
58 }
59 
60 void addTo(StringMap<ExecutorAddr> &M) {
61   M[rt::MemoryWriteUInt8sWrapperName] = ExecutorAddr::fromPtr(
62       &writeUIntsWrapper<tpctypes::UInt8Write,
63                          shared::SPSMemoryAccessUInt8Write>);
64   M[rt::MemoryWriteUInt16sWrapperName] = ExecutorAddr::fromPtr(
65       &writeUIntsWrapper<tpctypes::UInt16Write,
66                          shared::SPSMemoryAccessUInt16Write>);
67   M[rt::MemoryWriteUInt32sWrapperName] = ExecutorAddr::fromPtr(
68       &writeUIntsWrapper<tpctypes::UInt32Write,
69                          shared::SPSMemoryAccessUInt32Write>);
70   M[rt::MemoryWriteUInt64sWrapperName] = ExecutorAddr::fromPtr(
71       &writeUIntsWrapper<tpctypes::UInt64Write,
72                          shared::SPSMemoryAccessUInt64Write>);
73   M[rt::MemoryWriteBuffersWrapperName] =
74       ExecutorAddr::fromPtr(&writeBuffersWrapper);
75   M[rt::RegisterEHFrameSectionCustomDirectWrapperName] = ExecutorAddr::fromPtr(
76       &llvm_orc_registerEHFrameSectionCustomDirectWrapper);
77   M[rt::DeregisterEHFrameSectionCustomDirectWrapperName] =
78       ExecutorAddr::fromPtr(
79           &llvm_orc_deregisterEHFrameSectionCustomDirectWrapper);
80   M[rt::RunAsMainWrapperName] = ExecutorAddr::fromPtr(&runAsMainWrapper);
81 }
82 
83 } // end namespace rt_bootstrap
84 } // end namespace orc
85 } // end namespace llvm
86