xref: /trunk/main/bridges/source/cpp_uno/s5abi_macosx_aarch64/share.hxx (revision 79670aa18e015d263a20a2826ce53832fcd22063)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "uno/mapping.h"
25 
26 #include <typeinfo>
27 #include <exception>
28 #include <cstddef>
29 
30 namespace CPPU_CURRENT_NAMESPACE
31 {
32 
33 void dummy_can_throw_anything( char const * );
34 
35 typedef unsigned _Unwind_Ptr __attribute__((__mode__(__pointer__)));
36 
37 // ----- the following structure is compatible with the one declared in libunwind's unwind.h
38 // (use forced types)
39 
40 struct _Unwind_Exception
41 {
42     uint64_t exception_class;
43     void * exception_cleanup;
44     uintptr_t private_1;
45     uintptr_t private_2;
46 };
47 
48 struct __cxa_exception
49 {
50     /* From LLVM 10 a reserved member was added at the top of the struct on
51        64-bit targets. Who the hell does that?
52        https://reviews.llvm.org/rG674ec1eb16678b8addc02a4b0534ab383d22fa77
53        It is required on arm64 (Apple Silicon): the trailing _Unwind_Exception
54        must be 16-byte aligned within the allocation, and only WITH this member
55        does unwindHeader land at a 16-byte boundary (offset 96, vs a misaligned
56        88 without it).  Verified empirically against this host's libc++abi.
57        NOTE: Apple clang version != upstream LLVM version. */
58     void *reserved;
59     size_t referenceCount;
60     ::std::type_info *exceptionType;
61     void (*exceptionDestructor)(void *);
62     ::std::unexpected_handler unexpectedHandler;
63     ::std::terminate_handler terminateHandler;
64     __cxa_exception *nextException;
65     int handlerCount;
66     int handlerSwitchValue;
67     const unsigned char *actionRecord;
68     const unsigned char *languageSpecificData;
69     void *catchTemp;
70     void *adjustedPtr;
71     _Unwind_Exception unwindHeader;
72 };
73 
74 extern "C" void *__cxa_allocate_exception(
75     std::size_t thrown_size ) throw();
76 extern "C" void __cxa_throw (
77     void *thrown_exception, std::type_info *tinfo, void (*dest) (void *) ) __attribute__((noreturn));
78 
79 struct __cxa_eh_globals
80 {
81     __cxa_exception *caughtExceptions;
82     unsigned int uncaughtExceptions;
83 };
84 extern "C" __cxa_eh_globals *__cxa_get_globals () throw();
85 extern "C" std::type_info *__cxa_current_exception_type();
86 
87 // -----
88 
89 // on OSX 64bit the class_type_info classes are specified
90 // in http://refspecs.linuxbase.org/cxxabi-1.86.html#rtti but
91 // these details are not generally available in a public header
92 // of most development environments. So we define them here.
93 // NOTE: https://www.hexblog.com/wp-content/uploads/2012/06/Recon-2012-Skochinsky-Compiler-Internals.pdf
94 class __class_type_info : public std::type_info
95 {
96 public:
97         explicit __class_type_info( const char* pRttiName)
98         : std::type_info( pRttiName)
99         {}
100 };
101 
102 class __si_class_type_info : public __class_type_info
103 {
104         const __class_type_info* mpBaseType;
105 public:
106         explicit __si_class_type_info( const char* pRttiName, __class_type_info* pBaseType)
107         : __class_type_info( pRttiName), mpBaseType( pBaseType)
108         {}
109 };
110 
111 //==================================================================================================
112 void raiseException(
113     uno_Any * pUnoExc, uno_Mapping * pUno2Cpp );
114 //==================================================================================================
115 void fillUnoException(
116     __cxa_exception * header, uno_Any *, uno_Mapping * pCpp2Uno );
117 void fillUnoException(
118     std::type_info const & type, void * exception, uno_Any *,
119     uno_Mapping * pCpp2Uno );
120 }
121