1 //===- elfnix_platform.h ----------------------------------------*- C++ -*-===//
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 // ORC Runtime support for dynamic loading features on ELF-based platforms.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef ORC_RT_ELFNIX_PLATFORM_H
14 #define ORC_RT_ELFNIX_PLATFORM_H
15 
16 #include "common.h"
17 #include "executor_address.h"
18 
19 // Atexit functions.
20 ORC_RT_INTERFACE int __orc_rt_elfnix_cxa_atexit(void (*func)(void *), void *arg,
21                                                 void *dso_handle);
22 ORC_RT_INTERFACE int __orc_rt_elfnix_atexit(void (*func)(void *));
23 ORC_RT_INTERFACE void __orc_rt_elfnix_cxa_finalize(void *dso_handle);
24 
25 // dlfcn functions.
26 ORC_RT_INTERFACE const char *__orc_rt_elfnix_jit_dlerror();
27 ORC_RT_INTERFACE void *__orc_rt_elfnix_jit_dlopen(const char *path, int mode);
28 ORC_RT_INTERFACE int __orc_rt_elfnix_jit_dlclose(void *dso_handle);
29 ORC_RT_INTERFACE void *__orc_rt_elfnix_jit_dlsym(void *dso_handle,
30                                                  const char *symbol);
31 
32 namespace __orc_rt {
33 namespace elfnix {
34 
35 struct ELFNixPerObjectSectionsToRegister {
36   ExecutorAddrRange EHFrameSection;
37   ExecutorAddrRange ThreadDataSection;
38 };
39 
40 struct ELFNixJITDylibInitializers {
41   using SectionList = std::vector<ExecutorAddrRange>;
42 
43   ELFNixJITDylibInitializers() = default;
ELFNixJITDylibInitializersELFNixJITDylibInitializers44   ELFNixJITDylibInitializers(std::string Name, ExecutorAddr DSOHandleAddress)
45       : Name(std::move(Name)), DSOHandleAddress(std::move(DSOHandleAddress)) {}
46 
47   std::string Name;
48   ExecutorAddr DSOHandleAddress;
49 
50   std::vector<std::pair<std::string, SectionList>> InitSections;
51 };
52 
53 class ELFNixJITDylibDeinitializers {};
54 
55 using ELFNixJITDylibInitializerSequence =
56     std::vector<ELFNixJITDylibInitializers>;
57 
58 using ELFNixJITDylibDeinitializerSequence =
59     std::vector<ELFNixJITDylibDeinitializers>;
60 
61 enum dlopen_mode : int {
62   ORC_RT_RTLD_LAZY = 0x1,
63   ORC_RT_RTLD_NOW = 0x2,
64   ORC_RT_RTLD_LOCAL = 0x4,
65   ORC_RT_RTLD_GLOBAL = 0x8
66 };
67 
68 } // end namespace elfnix
69 
70 using SPSELFNixPerObjectSectionsToRegister =
71     SPSTuple<SPSExecutorAddrRange, SPSExecutorAddrRange>;
72 
73 template <>
74 class SPSSerializationTraits<SPSELFNixPerObjectSectionsToRegister,
75                              elfnix::ELFNixPerObjectSectionsToRegister> {
76 
77 public:
size(const elfnix::ELFNixPerObjectSectionsToRegister & MOPOSR)78   static size_t size(const elfnix::ELFNixPerObjectSectionsToRegister &MOPOSR) {
79     return SPSELFNixPerObjectSectionsToRegister::AsArgList::size(
80         MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
81   }
82 
83   static bool
serialize(SPSOutputBuffer & OB,const elfnix::ELFNixPerObjectSectionsToRegister & MOPOSR)84   serialize(SPSOutputBuffer &OB,
85             const elfnix::ELFNixPerObjectSectionsToRegister &MOPOSR) {
86     return SPSELFNixPerObjectSectionsToRegister::AsArgList::serialize(
87         OB, MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
88   }
89 
deserialize(SPSInputBuffer & IB,elfnix::ELFNixPerObjectSectionsToRegister & MOPOSR)90   static bool deserialize(SPSInputBuffer &IB,
91                           elfnix::ELFNixPerObjectSectionsToRegister &MOPOSR) {
92     return SPSELFNixPerObjectSectionsToRegister::AsArgList::deserialize(
93         IB, MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
94   }
95 };
96 
97 using SPSNamedExecutorAddrRangeSequenceMap =
98     SPSSequence<SPSTuple<SPSString, SPSExecutorAddrRangeSequence>>;
99 
100 using SPSELFNixJITDylibInitializers =
101     SPSTuple<SPSString, SPSExecutorAddr, SPSNamedExecutorAddrRangeSequenceMap>;
102 
103 using SPSELFNixJITDylibInitializerSequence =
104     SPSSequence<SPSELFNixJITDylibInitializers>;
105 
106 /// Serialization traits for ELFNixJITDylibInitializers.
107 template <>
108 class SPSSerializationTraits<SPSELFNixJITDylibInitializers,
109                              elfnix::ELFNixJITDylibInitializers> {
110 public:
size(const elfnix::ELFNixJITDylibInitializers & MOJDIs)111   static size_t size(const elfnix::ELFNixJITDylibInitializers &MOJDIs) {
112     return SPSELFNixJITDylibInitializers::AsArgList::size(
113         MOJDIs.Name, MOJDIs.DSOHandleAddress, MOJDIs.InitSections);
114   }
115 
serialize(SPSOutputBuffer & OB,const elfnix::ELFNixJITDylibInitializers & MOJDIs)116   static bool serialize(SPSOutputBuffer &OB,
117                         const elfnix::ELFNixJITDylibInitializers &MOJDIs) {
118     return SPSELFNixJITDylibInitializers::AsArgList::serialize(
119         OB, MOJDIs.Name, MOJDIs.DSOHandleAddress, MOJDIs.InitSections);
120   }
121 
deserialize(SPSInputBuffer & IB,elfnix::ELFNixJITDylibInitializers & MOJDIs)122   static bool deserialize(SPSInputBuffer &IB,
123                           elfnix::ELFNixJITDylibInitializers &MOJDIs) {
124     return SPSELFNixJITDylibInitializers::AsArgList::deserialize(
125         IB, MOJDIs.Name, MOJDIs.DSOHandleAddress, MOJDIs.InitSections);
126   }
127 };
128 
129 } // end namespace __orc_rt
130 
131 #endif // ORC_RT_ELFNIX_PLATFORM_H
132