xref: /freebsd/contrib/llvm-project/compiler-rt/lib/orc/macho_platform.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 MachOJITDylibInitializers {
35   using SectionList = std::vector<ExecutorAddrRange>;
36 
37   MachOJITDylibInitializers() = default;
38   MachOJITDylibInitializers(std::string Name, ExecutorAddr MachOHeaderAddress)
39       : Name(std::move(Name)),
40         MachOHeaderAddress(std::move(MachOHeaderAddress)) {}
41 
42   std::string Name;
43   ExecutorAddr MachOHeaderAddress;
44   ExecutorAddr ObjCImageInfoAddress;
45 
46   std::unordered_map<std::string, SectionList> InitSections;
47 };
48 
49 class MachOJITDylibDeinitializers {};
50 
51 using MachOJITDylibInitializerSequence = std::vector<MachOJITDylibInitializers>;
52 
53 using MachOJITDylibDeinitializerSequence =
54     std::vector<MachOJITDylibDeinitializers>;
55 
56 enum dlopen_mode : int {
57   ORC_RT_RTLD_LAZY = 0x1,
58   ORC_RT_RTLD_NOW = 0x2,
59   ORC_RT_RTLD_LOCAL = 0x4,
60   ORC_RT_RTLD_GLOBAL = 0x8
61 };
62 
63 } // end namespace macho
64 
65 using SPSNamedExecutorAddrRangeSequenceMap =
66     SPSSequence<SPSTuple<SPSString, SPSExecutorAddrRangeSequence>>;
67 
68 using SPSMachOJITDylibInitializers =
69     SPSTuple<SPSString, SPSExecutorAddr, SPSExecutorAddr,
70              SPSNamedExecutorAddrRangeSequenceMap>;
71 
72 using SPSMachOJITDylibInitializerSequence =
73     SPSSequence<SPSMachOJITDylibInitializers>;
74 
75 /// Serialization traits for MachOJITDylibInitializers.
76 template <>
77 class SPSSerializationTraits<SPSMachOJITDylibInitializers,
78                              macho::MachOJITDylibInitializers> {
79 public:
80   static size_t size(const macho::MachOJITDylibInitializers &MOJDIs) {
81     return SPSMachOJITDylibInitializers::AsArgList::size(
82         MOJDIs.Name, MOJDIs.MachOHeaderAddress, MOJDIs.ObjCImageInfoAddress,
83         MOJDIs.InitSections);
84   }
85 
86   static bool serialize(SPSOutputBuffer &OB,
87                         const macho::MachOJITDylibInitializers &MOJDIs) {
88     return SPSMachOJITDylibInitializers::AsArgList::serialize(
89         OB, MOJDIs.Name, MOJDIs.MachOHeaderAddress, MOJDIs.ObjCImageInfoAddress,
90         MOJDIs.InitSections);
91   }
92 
93   static bool deserialize(SPSInputBuffer &IB,
94                           macho::MachOJITDylibInitializers &MOJDIs) {
95     return SPSMachOJITDylibInitializers::AsArgList::deserialize(
96         IB, MOJDIs.Name, MOJDIs.MachOHeaderAddress, MOJDIs.ObjCImageInfoAddress,
97         MOJDIs.InitSections);
98   }
99 };
100 
101 } // end namespace __orc_rt
102 
103 #endif // ORC_RT_MACHO_PLATFORM_H
104