crtbegin.c revision 360784
1//===-- crtbegin.c - Start of constructors and destructors ----------------===// 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#include <stddef.h> 10 11__attribute__((visibility("hidden"))) void *__dso_handle = &__dso_handle; 12 13#ifdef EH_USE_FRAME_REGISTRY 14__extension__ static void *__EH_FRAME_LIST__[] 15 __attribute__((section(".eh_frame"), aligned(sizeof(void *)))) = {}; 16 17extern void __register_frame_info(const void *, void *) __attribute__((weak)); 18extern void *__deregister_frame_info(const void *) __attribute__((weak)); 19#endif 20 21#ifndef CRT_HAS_INITFINI_ARRAY 22typedef void (*fp)(void); 23 24static fp __CTOR_LIST__[] 25 __attribute__((section(".ctors"), aligned(sizeof(fp)))) = {(fp)-1}; 26extern fp __CTOR_LIST_END__[]; 27#endif 28 29extern void __cxa_finalize(void *) __attribute__((weak)); 30 31static void __attribute__((used)) __do_init() { 32 static _Bool __initialized; 33 if (__builtin_expect(__initialized, 0)) 34 return; 35 __initialized = 1; 36 37#ifdef EH_USE_FRAME_REGISTRY 38 static struct { void *p[8]; } __object; 39 if (__register_frame_info) 40 __register_frame_info(__EH_FRAME_LIST__, &__object); 41#endif 42#ifndef CRT_HAS_INITFINI_ARRAY 43 const size_t n = __CTOR_LIST_END__ - __CTOR_LIST__ - 1; 44 for (size_t i = n; i >= 1; i--) __CTOR_LIST__[i](); 45#endif 46} 47 48#ifdef CRT_HAS_INITFINI_ARRAY 49__attribute__((section(".init_array"), 50 used)) static void (*__init)(void) = __do_init; 51#elif defined(__i386__) || defined(__x86_64__) 52__asm__(".pushsection .init,\"ax\",@progbits\n\t" 53 "call " __USER_LABEL_PREFIX__ "__do_init\n\t" 54 ".popsection"); 55#elif defined(__arm__) || defined(__aarch64__) 56__asm__(".pushsection .init,\"ax\",%progbits\n\t" 57 "bl " __USER_LABEL_PREFIX__ "__do_init\n\t" 58 ".popsection"); 59#elif defined(__powerpc__) || defined(__powerpc64__) 60__asm__(".pushsection .init,\"ax\",@progbits\n\t" 61 "bl " __USER_LABEL_PREFIX__ "__do_init\n\t" 62 "nop\n\t" 63 ".popsection"); 64#elif defined(__sparc__) 65__asm__(".pushsection .init,\"ax\",@progbits\n\t" 66 "call " __USER_LABEL_PREFIX__ "__do_init\n\t" 67 ".popsection"); 68#else 69#error "crtbegin without .init_fini array unimplemented for this architecture" 70#endif // CRT_HAS_INITFINI_ARRAY 71 72#ifndef CRT_HAS_INITFINI_ARRAY 73static fp __DTOR_LIST__[] 74 __attribute__((section(".dtors"), aligned(sizeof(fp)))) = {(fp)-1}; 75extern fp __DTOR_LIST_END__[]; 76#endif 77 78static void __attribute__((used)) __do_fini() { 79 static _Bool __finalized; 80 if (__builtin_expect(__finalized, 0)) 81 return; 82 __finalized = 1; 83 84 if (__cxa_finalize) 85 __cxa_finalize(__dso_handle); 86 87#ifndef CRT_HAS_INITFINI_ARRAY 88 const size_t n = __DTOR_LIST_END__ - __DTOR_LIST__ - 1; 89 for (size_t i = 1; i <= n; i++) __DTOR_LIST__[i](); 90#endif 91#ifdef EH_USE_FRAME_REGISTRY 92 if (__deregister_frame_info) 93 __deregister_frame_info(__EH_FRAME_LIST__); 94#endif 95} 96 97#ifdef CRT_HAS_INITFINI_ARRAY 98__attribute__((section(".fini_array"), 99 used)) static void (*__fini)(void) = __do_fini; 100#elif defined(__i386__) || defined(__x86_64__) 101__asm__(".pushsection .fini,\"ax\",@progbits\n\t" 102 "call " __USER_LABEL_PREFIX__ "__do_fini\n\t" 103 ".popsection"); 104#elif defined(__arm__) || defined(__aarch64__) 105__asm__(".pushsection .fini,\"ax\",%progbits\n\t" 106 "bl " __USER_LABEL_PREFIX__ "__do_fini\n\t" 107 ".popsection"); 108#elif defined(__powerpc__) || defined(__powerpc64__) 109__asm__(".pushsection .fini,\"ax\",@progbits\n\t" 110 "bl " __USER_LABEL_PREFIX__ "__do_fini\n\t" 111 "nop\n\t" 112 ".popsection"); 113#elif defined(__sparc__) 114__asm__(".pushsection .fini,\"ax\",@progbits\n\t" 115 "call " __USER_LABEL_PREFIX__ "__do_fini\n\t" 116 ".popsection"); 117#else 118#error "crtbegin without .init_fini array unimplemented for this architecture" 119#endif // CRT_HAS_INIT_FINI_ARRAY 120