xref: /freebsd/contrib/llvm-project/compiler-rt/lib/orc/macho_platform.h (revision 13ec1e3155c7e9bf037b12af186351b7fa9b9450)
1 //===- macho_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 Darwin dynamic loading features.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef ORC_RT_MACHO_PLATFORM_H
14 #define ORC_RT_MACHO_PLATFORM_H
15 
16 #include "common.h"
17 #include "executor_address.h"
18 
19 // Atexit functions.
20 ORC_RT_INTERFACE int __orc_rt_macho_cxa_atexit(void (*func)(void *), void *arg,
21                                                void *dso_handle);
22 ORC_RT_INTERFACE void __orc_rt_macho_cxa_finalize(void *dso_handle);
23 
24 // dlfcn functions.
25 ORC_RT_INTERFACE const char *__orc_rt_macho_jit_dlerror();
26 ORC_RT_INTERFACE void *__orc_rt_macho_jit_dlopen(const char *path, int mode);
27 ORC_RT_INTERFACE int __orc_rt_macho_jit_dlclose(void *dso_handle);
28 ORC_RT_INTERFACE void *__orc_rt_macho_jit_dlsym(void *dso_handle,
29                                                 const char *symbol);
30 
31 namespace __orc_rt {
32 namespace macho {
33 
34 struct MachOPerObjectSectionsToRegister {
35   ExecutorAddressRange EHFrameSection;
36   ExecutorAddressRange ThreadDataSection;
37 };
38 
39 struct MachOJITDylibInitializers {
40   using SectionList = std::vector<ExecutorAddressRange>;
41 
42   MachOJITDylibInitializers() = default;
43   MachOJITDylibInitializers(std::string Name,
44                             ExecutorAddress MachOHeaderAddress)
45       : Name(std::move(Name)),
46         MachOHeaderAddress(std::move(MachOHeaderAddress)) {}
47 
48   std::string Name;
49   ExecutorAddress MachOHeaderAddress;
50   ExecutorAddress ObjCImageInfoAddress;
51 
52   std::unordered_map<std::string, SectionList> InitSections;
53 };
54 
55 class MachOJITDylibDeinitializers {};
56 
57 using MachOJITDylibInitializerSequence = std::vector<MachOJITDylibInitializers>;
58 
59 using MachOJITDylibDeinitializerSequence =
60     std::vector<MachOJITDylibDeinitializers>;
61 
62 enum dlopen_mode : int {
63   ORC_RT_RTLD_LAZY = 0x1,
64   ORC_RT_RTLD_NOW = 0x2,
65   ORC_RT_RTLD_LOCAL = 0x4,
66   ORC_RT_RTLD_GLOBAL = 0x8
67 };
68 
69 } // end namespace macho
70 
71 using SPSMachOPerObjectSectionsToRegister =
72     SPSTuple<SPSExecutorAddressRange, SPSExecutorAddressRange>;
73 
74 template <>
75 class SPSSerializationTraits<SPSMachOPerObjectSectionsToRegister,
76                              macho::MachOPerObjectSectionsToRegister> {
77 
78 public:
79   static size_t size(const macho::MachOPerObjectSectionsToRegister &MOPOSR) {
80     return SPSMachOPerObjectSectionsToRegister::AsArgList::size(
81         MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
82   }
83 
84   static bool serialize(SPSOutputBuffer &OB,
85                         const macho::MachOPerObjectSectionsToRegister &MOPOSR) {
86     return SPSMachOPerObjectSectionsToRegister::AsArgList::serialize(
87         OB, MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
88   }
89 
90   static bool deserialize(SPSInputBuffer &IB,
91                           macho::MachOPerObjectSectionsToRegister &MOPOSR) {
92     return SPSMachOPerObjectSectionsToRegister::AsArgList::deserialize(
93         IB, MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
94   }
95 };
96 
97 using SPSNamedExecutorAddressRangeSequenceMap =
98     SPSSequence<SPSTuple<SPSString, SPSExecutorAddressRangeSequence>>;
99 
100 using SPSMachOJITDylibInitializers =
101     SPSTuple<SPSString, SPSExecutorAddress, SPSExecutorAddress,
102              SPSNamedExecutorAddressRangeSequenceMap>;
103 
104 using SPSMachOJITDylibInitializerSequence =
105     SPSSequence<SPSMachOJITDylibInitializers>;
106 
107 /// Serialization traits for MachOJITDylibInitializers.
108 template <>
109 class SPSSerializationTraits<SPSMachOJITDylibInitializers,
110                              macho::MachOJITDylibInitializers> {
111 public:
112   static size_t size(const macho::MachOJITDylibInitializers &MOJDIs) {
113     return SPSMachOJITDylibInitializers::AsArgList::size(
114         MOJDIs.Name, MOJDIs.MachOHeaderAddress, MOJDIs.ObjCImageInfoAddress,
115         MOJDIs.InitSections);
116   }
117 
118   static bool serialize(SPSOutputBuffer &OB,
119                         const macho::MachOJITDylibInitializers &MOJDIs) {
120     return SPSMachOJITDylibInitializers::AsArgList::serialize(
121         OB, MOJDIs.Name, MOJDIs.MachOHeaderAddress, MOJDIs.ObjCImageInfoAddress,
122         MOJDIs.InitSections);
123   }
124 
125   static bool deserialize(SPSInputBuffer &IB,
126                           macho::MachOJITDylibInitializers &MOJDIs) {
127     return SPSMachOJITDylibInitializers::AsArgList::deserialize(
128         IB, MOJDIs.Name, MOJDIs.MachOHeaderAddress, MOJDIs.ObjCImageInfoAddress,
129         MOJDIs.InitSections);
130   }
131 };
132 
133 } // end namespace __orc_rt
134 
135 #endif // ORC_RT_MACHO_PLATFORM_H
136