1// ABI Support -*- C++ -*-
2
3// Copyright (C) 2000-2016 Free Software Foundation, Inc.
4//
5// This file is part of GCC.
6//
7// GCC is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 3, or (at your option)
10// any later version.
11//
12// GCC is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
20
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
25
26// Written by Nathan Sidwell, Codesourcery LLC, <nathan@codesourcery.com>
27
28/* This file declares the new abi entry points into the runtime. It is not
29 normally necessary for user programs to include this header, or use the
30 entry points directly. However, this header is available should that be
31 needed.
32
33 Some of the entry points are intended for both C and C++, thus this header
34 is includable from both C and C++. Though the C++ specific parts are not
35 available in C, naturally enough. */
36
37/** @file cxxabi.h
38 * The header provides an interface to the C++ ABI.
39 */
40
41#ifndef _CXXABI_H
42#define _CXXABI_H 1
43
44#pragma GCC system_header
45
46#pragma GCC visibility push(default)
47
48#include <stddef.h>
49#include <bits/c++config.h>
50#include <bits/cxxabi_tweaks.h>
51#include <bits/cxxabi_forced.h>
52#include <bits/cxxabi_init_exception.h>
53
54#ifdef __cplusplus
55namespace __cxxabiv1
56{
57 extern "C"
58 {
59#endif
60
61 typedef __cxa_cdtor_return_type (*__cxa_cdtor_type)(void *);
62
63 // Allocate array.
64 void*
65 __cxa_vec_new(size_t __element_count, size_t __element_size,
66 size_t __padding_size, __cxa_cdtor_type __constructor,
67 __cxa_cdtor_type __destructor);
68
69 void*
70 __cxa_vec_new2(size_t __element_count, size_t __element_size,
71 size_t __padding_size, __cxa_cdtor_type __constructor,
72 __cxa_cdtor_type __destructor, void *(*__alloc) (size_t),
73 void (*__dealloc) (void*));
74
75 void*
76 __cxa_vec_new3(size_t __element_count, size_t __element_size,
77 size_t __padding_size, __cxa_cdtor_type __constructor,
78 __cxa_cdtor_type __destructor, void *(*__alloc) (size_t),
79 void (*__dealloc) (void*, size_t));
80
81 // Construct array.
82 __cxa_vec_ctor_return_type
83 __cxa_vec_ctor(void* __array_address, size_t __element_count,
84 size_t __element_size, __cxa_cdtor_type __constructor,
85 __cxa_cdtor_type __destructor);
86
87 __cxa_vec_ctor_return_type
88 __cxa_vec_cctor(void* __dest_array, void* __src_array,
89 size_t __element_count, size_t __element_size,
90 __cxa_cdtor_return_type (*__constructor) (void*, void*),
91 __cxa_cdtor_type __destructor);
92
93 // Destruct array.
94 void
95 __cxa_vec_dtor(void* __array_address, size_t __element_count,
96 size_t __element_size, __cxa_cdtor_type __destructor);
97
98 void
99 __cxa_vec_cleanup(void* __array_address, size_t __element_count, size_t __s,
100 __cxa_cdtor_type __destructor) _GLIBCXX_NOTHROW;
101
102 // Destruct and release array.
103 void
104 __cxa_vec_delete(void* __array_address, size_t __element_size,
105 size_t __padding_size, __cxa_cdtor_type __destructor);
106
107 void
108 __cxa_vec_delete2(void* __array_address, size_t __element_size,
109 size_t __padding_size, __cxa_cdtor_type __destructor,
110 void (*__dealloc) (void*));
111
112 void
113 __cxa_vec_delete3(void* __array_address, size_t __element_size,
114 size_t __padding_size, __cxa_cdtor_type __destructor,
115 void (*__dealloc) (void*, size_t));
116
117 int
118 __cxa_guard_acquire(__guard*);
119
120 void
121 __cxa_guard_release(__guard*) _GLIBCXX_NOTHROW;
122
123 void
124 __cxa_guard_abort(__guard*) _GLIBCXX_NOTHROW;
125
126 // DSO destruction.
127 int
128 __cxa_atexit(void (*)(void*), void*, void*) _GLIBCXX_NOTHROW;
129
130 int
131 __cxa_finalize(void*);
132
133 // TLS destruction.
134 int
135 __cxa_thread_atexit(void (*)(void*), void*, void *) _GLIBCXX_NOTHROW;
136
137 // Pure virtual functions.
138 void
139 __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
140
141 void
142 __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
143
144 // Exception handling auxiliary.
145 void
146 __cxa_bad_cast() __attribute__((__noreturn__));
147
148 void
149 __cxa_bad_typeid() __attribute__((__noreturn__));
150
151 void
152 __cxa_throw_bad_array_new_length() __attribute__((__noreturn__));
153
154 /**
155 * @brief Demangling routine.
156 * ABI-mandated entry point in the C++ runtime library for demangling.
157 *
158 * @param __mangled_name A NUL-terminated character string
159 * containing the name to be demangled.
160 *
161 * @param __output_buffer A region of memory, allocated with
162 * malloc, of @a *__length bytes, into which the demangled name is
163 * stored. If @a __output_buffer is not long enough, it is
164 * expanded using realloc. @a __output_buffer may instead be NULL;
165 * in that case, the demangled name is placed in a region of memory
166 * allocated with malloc.
167 *
168 * @param __length If @a __length is non-NULL, the length of the
169 * buffer containing the demangled name is placed in @a *__length.
170 *
171 * @param __status @a *__status is set to one of the following values:
172 * 0: The demangling operation succeeded.
173 * -1: A memory allocation failure occurred.
174 * -2: @a mangled_name is not a valid name under the C++ ABI mangling rules.
175 * -3: One of the arguments is invalid.
176 *
177 * @return A pointer to the start of the NUL-terminated demangled
178 * name, or NULL if the demangling fails. The caller is
179 * responsible for deallocating this memory using @c free.
180 *
181 * The demangling is performed using the C++ ABI mangling rules,
182 * with GNU extensions. For example, this function is used in
183 * __gnu_cxx::__verbose_terminate_handler.
184 *
185 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch39.html
186 * for other examples of use.
187 *
188 * @note The same demangling functionality is available via
189 * libiberty (@c <libiberty/demangle.h> and @c libiberty.a) in GCC
190 * 3.1 and later, but that requires explicit installation (@c
191 * --enable-install-libiberty) and uses a different API, although
192 * the ABI is unchanged.
193 */
194 char*
195 __cxa_demangle(const char* __mangled_name, char* __output_buffer,
196 size_t* __length, int* __status);
197
198#ifdef __cplusplus
199 }
200} // namespace __cxxabiv1
201#endif
202
203#ifdef __cplusplus
204
205#include <typeinfo>
206
207namespace __cxxabiv1
208{
209 // Type information for int, float etc.
210 class __fundamental_type_info : public std::type_info
211 {
212 public:
213 explicit
214 __fundamental_type_info(const char* __n) : std::type_info(__n) { }
215
216 virtual
217 ~__fundamental_type_info();
218 };
219
220 // Type information for array objects.
221 class __array_type_info : public std::type_info
222 {
223 public:
224 explicit
225 __array_type_info(const char* __n) : std::type_info(__n) { }
226
227 virtual
228 ~__array_type_info();
229 };
230
231 // Type information for functions (both member and non-member).
232 class __function_type_info : public std::type_info
233 {
234 public:
235 explicit
236 __function_type_info(const char* __n) : std::type_info(__n) { }
237
238 virtual
239 ~__function_type_info();
240
241 protected:
242 // Implementation defined member function.
243 virtual bool
244 __is_function_p() const;
245 };
246
247 // Type information for enumerations.
248 class __enum_type_info : public std::type_info
249 {
250 public:
251 explicit
252 __enum_type_info(const char* __n) : std::type_info(__n) { }
253
254 virtual
255 ~__enum_type_info();
256 };
257
258 // Common type information for simple pointers and pointers to member.
259 class __pbase_type_info : public std::type_info
260 {
261 public:
262 unsigned int __flags; // Qualification of the target object.
263 const std::type_info* __pointee; // Type of pointed to object.
264
265 explicit
266 __pbase_type_info(const char* __n, int __quals,
267 const std::type_info* __type)
268 : std::type_info(__n), __flags(__quals), __pointee(__type)
269 { }
270
271 virtual
272 ~__pbase_type_info();
273
274 // Implementation defined type.
275 enum __masks
276 {
277 __const_mask = 0x1,
278 __volatile_mask = 0x2,
279 __restrict_mask = 0x4,
280 __incomplete_mask = 0x8,
281 __incomplete_class_mask = 0x10,
282 __transaction_safe_mask = 0x20
283 };
284
285 protected:
286 __pbase_type_info(const __pbase_type_info&);
287
288 __pbase_type_info&
289 operator=(const __pbase_type_info&);
290
291 // Implementation defined member functions.
292 virtual bool
293 __do_catch(const std::type_info* __thr_type, void** __thr_obj,
294 unsigned int __outer) const;
295
296 inline virtual bool
297 __pointer_catch(const __pbase_type_info* __thr_type, void** __thr_obj,
298 unsigned __outer) const;
299 };
300
301 inline bool __pbase_type_info::
302 __pointer_catch (const __pbase_type_info *thrown_type,
303 void **thr_obj,
304 unsigned outer) const
305 {
306 return __pointee->__do_catch (thrown_type->__pointee, thr_obj, outer + 2);
307 }
308
309 // Type information for simple pointers.
310 class __pointer_type_info : public __pbase_type_info
311 {
312 public:
313 explicit
314 __pointer_type_info(const char* __n, int __quals,
315 const std::type_info* __type)
316 : __pbase_type_info (__n, __quals, __type) { }
317
318
319 virtual
320 ~__pointer_type_info();
321
322 protected:
323 // Implementation defined member functions.
324 virtual bool
325 __is_pointer_p() const;
326
327 virtual bool
328 __pointer_catch(const __pbase_type_info* __thr_type, void** __thr_obj,
329 unsigned __outer) const;
330 };
331
332 class __class_type_info;
333
334 // Type information for a pointer to member variable.
335 class __pointer_to_member_type_info : public __pbase_type_info
336 {
337 public:
338 __class_type_info* __context; // Class of the member.
339
340 explicit
341 __pointer_to_member_type_info(const char* __n, int __quals,
342 const std::type_info* __type,
343 __class_type_info* __klass)
344 : __pbase_type_info(__n, __quals, __type), __context(__klass) { }
345
346 virtual
347 ~__pointer_to_member_type_info();
348
349 protected:
350 __pointer_to_member_type_info(const __pointer_to_member_type_info&);
351
352 __pointer_to_member_type_info&
353 operator=(const __pointer_to_member_type_info&);
354
355 // Implementation defined member function.
356 virtual bool
357 __pointer_catch(const __pbase_type_info* __thr_type, void** __thr_obj,
358 unsigned __outer) const;
359 };
360
361 // Helper class for __vmi_class_type.
362 class __base_class_type_info
363 {
364 public:
365 const __class_type_info* __base_type; // Base class type.
366#ifdef _GLIBCXX_LLP64
367 long long __offset_flags; // Offset and info.
368#else
369 long __offset_flags; // Offset and info.
370#endif
371
372 enum __offset_flags_masks
373 {
374 __virtual_mask = 0x1,
375 __public_mask = 0x2,
376 __hwm_bit = 2,
377 __offset_shift = 8 // Bits to shift offset.
378 };
379
380 // Implementation defined member functions.
381 bool
382 __is_virtual_p() const
383 { return __offset_flags & __virtual_mask; }
384
385 bool
386 __is_public_p() const
387 { return __offset_flags & __public_mask; }
388
389 ptrdiff_t
390 __offset() const
391 {
392 // This shift, being of a signed type, is implementation
393 // defined. GCC implements such shifts as arithmetic, which is
394 // what we want.
395 return static_cast<ptrdiff_t>(__offset_flags) >> __offset_shift;
396 }
397 };
398
399 // Type information for a class.
400 class __class_type_info : public std::type_info
401 {
402 public:
403 explicit
404 __class_type_info (const char *__n) : type_info(__n) { }
405
406 virtual
407 ~__class_type_info ();
408
409 // Implementation defined types.
410 // The type sub_kind tells us about how a base object is contained
411 // within a derived object. We often do this lazily, hence the
412 // UNKNOWN value. At other times we may use NOT_CONTAINED to mean
413 // not publicly contained.
414 enum __sub_kind
415 {
416 // We have no idea.
417 __unknown = 0,
418
419 // Not contained within us (in some circumstances this might
420 // mean not contained publicly)
421 __not_contained,
422
423 // Contained ambiguously.
424 __contained_ambig,
425
426 // Via a virtual path.
427 __contained_virtual_mask = __base_class_type_info::__virtual_mask,
428
429 // Via a public path.
430 __contained_public_mask = __base_class_type_info::__public_mask,
431
432 // Contained within us.
433 __contained_mask = 1 << __base_class_type_info::__hwm_bit,
434
435 __contained_private = __contained_mask,
436 __contained_public = __contained_mask | __contained_public_mask
437 };
438
439 struct __upcast_result;
440 struct __dyncast_result;
441
442 protected:
443 // Implementation defined member functions.
444 virtual bool
445 __do_upcast(const __class_type_info* __dst_type, void**__obj_ptr) const;
446
447 virtual bool
448 __do_catch(const type_info* __thr_type, void** __thr_obj,
449 unsigned __outer) const;
450
451 public:
452 // Helper for upcast. See if DST is us, or one of our bases.
453 // Return false if not found, true if found.
454 virtual bool
455 __do_upcast(const __class_type_info* __dst, const void* __obj,
456 __upcast_result& __restrict __result) const;
457
458 // Indicate whether SRC_PTR of type SRC_TYPE is contained publicly
459 // within OBJ_PTR. OBJ_PTR points to a base object of our type,
460 // which is the destination type. SRC2DST indicates how SRC
461 // objects might be contained within this type. If SRC_PTR is one
462 // of our SRC_TYPE bases, indicate the virtuality. Returns
463 // not_contained for non containment or private containment.
464 inline __sub_kind
465 __find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
466 const __class_type_info* __src_type,
467 const void* __src_ptr) const;
468
469 // Helper for dynamic cast. ACCESS_PATH gives the access from the
470 // most derived object to this base. DST_TYPE indicates the
471 // desired type we want. OBJ_PTR points to a base of our type
472 // within the complete object. SRC_TYPE indicates the static type
473 // started from and SRC_PTR points to that base within the most
474 // derived object. Fill in RESULT with what we find. Return true
475 // if we have located an ambiguous match.
476 virtual bool
477 __do_dyncast(ptrdiff_t __src2dst, __sub_kind __access_path,
478 const __class_type_info* __dst_type, const void* __obj_ptr,
479 const __class_type_info* __src_type, const void* __src_ptr,
480 __dyncast_result& __result) const;
481
482 // Helper for find_public_subobj. SRC2DST indicates how SRC_TYPE
483 // bases are inherited by the type started from -- which is not
484 // necessarily the current type. The current type will be a base
485 // of the destination type. OBJ_PTR points to the current base.
486 virtual __sub_kind
487 __do_find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
488 const __class_type_info* __src_type,
489 const void* __src_ptr) const;
490 };
491
492 // Type information for a class with a single non-virtual base.
493 class __si_class_type_info : public __class_type_info
494 {
495 public:
496 const __class_type_info* __base_type;
497
498 explicit
499 __si_class_type_info(const char *__n, const __class_type_info *__base)
500 : __class_type_info(__n), __base_type(__base) { }
501
502 virtual
503 ~__si_class_type_info();
504
505 protected:
506 __si_class_type_info(const __si_class_type_info&);
507
508 __si_class_type_info&
509 operator=(const __si_class_type_info&);
510
511 // Implementation defined member functions.
512 virtual bool
513 __do_dyncast(ptrdiff_t __src2dst, __sub_kind __access_path,
514 const __class_type_info* __dst_type, const void* __obj_ptr,
515 const __class_type_info* __src_type, const void* __src_ptr,
516 __dyncast_result& __result) const;
517
518 virtual __sub_kind
519 __do_find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
520 const __class_type_info* __src_type,
521 const void* __sub_ptr) const;
522
523 virtual bool
524 __do_upcast(const __class_type_info*__dst, const void*__obj,
525 __upcast_result& __restrict __result) const;
526 };
527
528 // Type information for a class with multiple and/or virtual bases.
529 class __vmi_class_type_info : public __class_type_info
530 {
531 public:
532 unsigned int __flags; // Details about the class hierarchy.
533 unsigned int __base_count; // Number of direct bases.
534
535 // The array of bases uses the trailing array struct hack so this
536 // class is not constructable with a normal constructor. It is
537 // internally generated by the compiler.
538 __base_class_type_info __base_info[1]; // Array of bases.
539
540 explicit
541 __vmi_class_type_info(const char* __n, int ___flags)
542 : __class_type_info(__n), __flags(___flags), __base_count(0) { }
543
544 virtual
545 ~__vmi_class_type_info();
546
547 // Implementation defined types.
548 enum __flags_masks
549 {
550 __non_diamond_repeat_mask = 0x1, // Distinct instance of repeated base.
551 __diamond_shaped_mask = 0x2, // Diamond shaped multiple inheritance.
552 __flags_unknown_mask = 0x10
553 };
554
555 protected:
556 // Implementation defined member functions.
557 virtual bool
558 __do_dyncast(ptrdiff_t __src2dst, __sub_kind __access_path,
559 const __class_type_info* __dst_type, const void* __obj_ptr,
560 const __class_type_info* __src_type, const void* __src_ptr,
561 __dyncast_result& __result) const;
562
563 virtual __sub_kind
564 __do_find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
565 const __class_type_info* __src_type,
566 const void* __src_ptr) const;
567
568 virtual bool
569 __do_upcast(const __class_type_info* __dst, const void* __obj,
570 __upcast_result& __restrict __result) const;
571 };
572
573 // Exception handling forward declarations.
574 struct __cxa_exception;
575 struct __cxa_refcounted_exception;
576 struct __cxa_dependent_exception;
577 struct __cxa_eh_globals;
578
579 extern "C"
580 {
581 // Dynamic cast runtime.
582
583 // src2dst has the following possible values
584 // >-1: src_type is a unique public non-virtual base of dst_type
585 // dst_ptr + src2dst == src_ptr
586 // -1: unspecified relationship
587 // -2: src_type is not a public base of dst_type
588 // -3: src_type is a multiple public non-virtual base of dst_type
589 void*
590 __dynamic_cast(const void* __src_ptr, // Starting object.
591 const __class_type_info* __src_type, // Static type of object.
592 const __class_type_info* __dst_type, // Desired target type.
593 ptrdiff_t __src2dst); // How src and dst are related.
594
595
596 // Exception handling runtime.
597
598 // The __cxa_eh_globals for the current thread can be obtained by using
599 // either of the following functions. The "fast" version assumes at least
600 // one prior call of __cxa_get_globals has been made from the current
601 // thread, so no initialization is necessary.
602 __cxa_eh_globals*
603 __cxa_get_globals() _GLIBCXX_NOTHROW __attribute__ ((__const__));
604
605 __cxa_eh_globals*
606 __cxa_get_globals_fast() _GLIBCXX_NOTHROW __attribute__ ((__const__));
607
608 // Free the space allocated for the primary exception.
609 void
610 __cxa_free_exception(void*) _GLIBCXX_NOTHROW;
611
612 // Throw the exception.
613 void
614 __cxa_throw(void*, std::type_info*, void (_GLIBCXX_CDTOR_CALLABI *) (void *))
615 __attribute__((__noreturn__));
616
617 // Used to implement exception handlers.
618 void*
619 __cxa_get_exception_ptr(void*) _GLIBCXX_NOTHROW __attribute__ ((__pure__));
620
621 void*
622 __cxa_begin_catch(void*) _GLIBCXX_NOTHROW;
623
624 void
625 __cxa_end_catch();
626
627 void
628 __cxa_rethrow() __attribute__((__noreturn__));
629
630 // Returns the type_info for the currently handled exception [15.3/8], or
631 // null if there is none.
632 std::type_info*
633 __cxa_current_exception_type() _GLIBCXX_NOTHROW __attribute__ ((__pure__));
634
635 // GNU Extensions.
636
637 // Allocate memory for a dependent exception.
638 __cxa_dependent_exception*
639 __cxa_allocate_dependent_exception() _GLIBCXX_NOTHROW;
640
641 // Free the space allocated for the dependent exception.
642 void
643 __cxa_free_dependent_exception(__cxa_dependent_exception*) _GLIBCXX_NOTHROW;
644
645 } // extern "C"
646
647 // A magic placeholder class that can be caught by reference
648 // to recognize foreign exceptions.
649 class __foreign_exception
650 {
651 virtual ~__foreign_exception() throw();
652 virtual void __pure_dummy() = 0; // prevent catch by value
653 };
654
655} // namespace __cxxabiv1
656
657/** @namespace abi
658 * @brief The cross-vendor C++ Application Binary Interface. A
659 * namespace alias to __cxxabiv1, but user programs should use the
660 * alias 'abi'.
661 *
662 * A brief overview of an ABI is given in the libstdc++ FAQ, question
663 * 5.8 (you may have a copy of the FAQ locally, or you can view the online
664 * version at http://gcc.gnu.org/onlinedocs/libstdc++/faq.html#5_8 ).
665 *
666 * GCC subscribes to a cross-vendor ABI for C++, sometimes
667 * called the IA64 ABI because it happens to be the native ABI for that
668 * platform. It is summarized at http://www.codesourcery.com/cxx-abi/
669 * along with the current specification.
670 *
671 * For users of GCC greater than or equal to 3.x, entry points are
672 * available in <cxxabi.h>, which notes, <em>'It is not normally
673 * necessary for user programs to include this header, or use the
674 * entry points directly. However, this header is available should
675 * that be needed.'</em>
676*/
677namespace abi = __cxxabiv1;
678
679namespace __gnu_cxx
680{
681 /**
682 * @brief Exception thrown by __cxa_guard_acquire.
683 * @ingroup exceptions
684 *
685 * 6.7[stmt.dcl]/4: If control re-enters the declaration (recursively)
686 * while the object is being initialized, the behavior is undefined.
687 *
688 * Since we already have a library function to handle locking, we might
689 * as well check for this situation and throw an exception.
690 * We use the second byte of the guard variable to remember that we're
691 * in the middle of an initialization.
692 */
693 class recursive_init_error: public std::exception
694 {
695 public:
696 recursive_init_error() throw() { }
697 virtual ~recursive_init_error() throw ();
698 };
699}
700#endif // __cplusplus
701
702#pragma GCC visibility pop
703
704#endif // __CXXABI_H
705