1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <__bits/abi.hpp>
|
---|
30 | #include <cstdlib>
|
---|
31 | #include <cstdint>
|
---|
32 | #include <exception>
|
---|
33 | #include <mutex>
|
---|
34 |
|
---|
35 | void* __dso_handle = nullptr;
|
---|
36 |
|
---|
37 | namespace __cxxabiv1
|
---|
38 | {
|
---|
39 | namespace aux
|
---|
40 | {
|
---|
41 | struct destructor_t
|
---|
42 | {
|
---|
43 | void (*func)(void*);
|
---|
44 | void* ptr;
|
---|
45 | void* dso;
|
---|
46 | };
|
---|
47 |
|
---|
48 | destructor_t* destructors{nullptr};
|
---|
49 | std::size_t destructor_count{0};
|
---|
50 | std::size_t destructor_size{32};
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * C atexit does not pass any arguments,
|
---|
54 | * but __cxa_finalize requires one so we
|
---|
55 | * use a wrapper.
|
---|
56 | */
|
---|
57 | void atexit_destructors()
|
---|
58 | {
|
---|
59 | __cxa_finalize(nullptr);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * No need for a body, this function is called when a virtual
|
---|
65 | * call of a pure virtual function cannot be made.
|
---|
66 | */
|
---|
67 | extern "C" void __cxa_pure_virtual()
|
---|
68 | {
|
---|
69 | std::terminate();
|
---|
70 | }
|
---|
71 |
|
---|
72 | #ifdef __arm__
|
---|
73 | extern "C" int __aeabi_atexit(void* p, void (*f)(void*), void* d)
|
---|
74 | {
|
---|
75 | return __cxa_atexit(f, p, d);
|
---|
76 | }
|
---|
77 | #endif
|
---|
78 |
|
---|
79 | extern "C" int __cxa_atexit(void (*f)(void*), void* p, void* d)
|
---|
80 | {
|
---|
81 | if (!aux::destructors)
|
---|
82 | {
|
---|
83 | aux::destructors = new aux::destructor_t[aux::destructor_size];
|
---|
84 | std::atexit(aux::atexit_destructors);
|
---|
85 | }
|
---|
86 | else if (aux::destructor_count >= aux::destructor_size)
|
---|
87 | {
|
---|
88 | auto tmp = std::realloc(aux::destructors, aux::destructor_size * 2);
|
---|
89 |
|
---|
90 | if (!tmp)
|
---|
91 | return -1;
|
---|
92 |
|
---|
93 | aux::destructors = static_cast<aux::destructor_t*>(tmp);
|
---|
94 | aux::destructor_size *= 2;
|
---|
95 | }
|
---|
96 |
|
---|
97 | auto& destr = aux::destructors[aux::destructor_count++];
|
---|
98 | destr.func = f;
|
---|
99 | destr.ptr = p;
|
---|
100 | destr.dso = d;
|
---|
101 |
|
---|
102 | return 0;
|
---|
103 | }
|
---|
104 |
|
---|
105 | extern "C" void __cxa_finalize(void *f)
|
---|
106 | {
|
---|
107 | if (!f)
|
---|
108 | {
|
---|
109 | for (std::size_t i = aux::destructor_count; i > 0; --i)
|
---|
110 | {
|
---|
111 | if (aux::destructors[i - 1].func)
|
---|
112 | (*aux::destructors[i - 1].func)(aux::destructors[i - 1].ptr);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | else
|
---|
116 | {
|
---|
117 | for (std::size_t i = aux::destructor_count; i > 0; --i)
|
---|
118 | {
|
---|
119 | if (aux::destructors[i - 1].func == f)
|
---|
120 | {
|
---|
121 | (*aux::destructors[i - 1].func)(aux::destructors[i - 1].ptr);
|
---|
122 | aux::destructors[i - 1].func = nullptr;
|
---|
123 | aux::destructors[i - 1].ptr = nullptr;
|
---|
124 | aux::destructors[i - 1].dso = nullptr;
|
---|
125 | // TODO: shift and decrement count
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | using guard_t = std::uint64_t;
|
---|
132 | std::mutex static_guard_mtx{};
|
---|
133 |
|
---|
134 | extern "C" int __cxa_guard_acquire(guard_t* guard)
|
---|
135 | {
|
---|
136 | static_guard_mtx.lock();
|
---|
137 |
|
---|
138 | return !*((std::uint8_t*)guard);
|
---|
139 | }
|
---|
140 |
|
---|
141 | extern "C" void __cxa_guard_release(guard_t* guard)
|
---|
142 | {
|
---|
143 | *((std::uint8_t*)guard) = 1;
|
---|
144 |
|
---|
145 | static_guard_mtx.unlock();
|
---|
146 | }
|
---|
147 |
|
---|
148 | extern "C" void __cxa_guard_abort(guard_t* guard)
|
---|
149 | {
|
---|
150 | static_guard_mtx.unlock();
|
---|
151 | }
|
---|
152 |
|
---|
153 | __fundamental_type_info::~__fundamental_type_info()
|
---|
154 | { /* DUMMY BODY */ }
|
---|
155 |
|
---|
156 | __array_type_info::~__array_type_info()
|
---|
157 | { /* DUMMY BODY */ }
|
---|
158 |
|
---|
159 | __function_type_info::~__function_type_info()
|
---|
160 | { /* DUMMY BODY */ }
|
---|
161 |
|
---|
162 | __enum_type_info::~__enum_type_info()
|
---|
163 | { /* DUMMY BODY */ }
|
---|
164 |
|
---|
165 | __class_type_info::~__class_type_info()
|
---|
166 | { /* DUMMY BODY */ }
|
---|
167 |
|
---|
168 | __si_class_type_info::~__si_class_type_info()
|
---|
169 | { /* DUMMY BODY */ }
|
---|
170 |
|
---|
171 | __vmi_class_type_info::~__vmi_class_type_info()
|
---|
172 | { /* DUMMY BODY */ }
|
---|
173 |
|
---|
174 | __pbase_type_info::~__pbase_type_info()
|
---|
175 | { /* DUMMY BODY */ }
|
---|
176 |
|
---|
177 | __pointer_type_info::~__pointer_type_info()
|
---|
178 | { /* DUMMY BODY */ }
|
---|
179 |
|
---|
180 | __pointer_to_member_type_info::~__pointer_to_member_type_info()
|
---|
181 | { /* DUMMY BODY */ }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * This structure represents part of the vtable segment
|
---|
185 | * that contains data related to dynamic_cast.
|
---|
186 | */
|
---|
187 | struct vtable
|
---|
188 | {
|
---|
189 | // Unimportant to us.
|
---|
190 |
|
---|
191 | std::ptrdiff_t offset_to_top;
|
---|
192 | std::type_info* tinfo;
|
---|
193 |
|
---|
194 | // Actual vtable.
|
---|
195 | };
|
---|
196 | extern "C" void* __dynamic_cast(const void* sub, const __class_type_info* src,
|
---|
197 | const __class_type_info* dst, std::ptrdiff_t offset)
|
---|
198 | {
|
---|
199 | // TODO: implement
|
---|
200 | // NOTE: as far as I understand it, we get vtable prefix from sub, get the type_info
|
---|
201 | // ptr from that and then climb the inheritance hierarchy upwards till we either
|
---|
202 | // fint dst or fail and return nullptr
|
---|
203 | return nullptr;
|
---|
204 | }
|
---|
205 |
|
---|
206 | // Needed on arm.
|
---|
207 | extern "C" void __cxa_end_cleanup()
|
---|
208 | { /* DUMMY BODY */ }
|
---|
209 | }
|
---|