Path: blob/main/contrib/llvm-project/compiler-rt/lib/builtins/crtbegin.c
35260 views
//===-- crtbegin.c - Start of constructors and destructors ----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <stddef.h>910__attribute__((visibility("hidden"))) void *__dso_handle = &__dso_handle;1112#ifdef EH_USE_FRAME_REGISTRY13__extension__ static void *__EH_FRAME_LIST__[]14__attribute__((section(".eh_frame"), aligned(sizeof(void *)))) = {};1516extern void __register_frame_info(const void *, void *) __attribute__((weak));17extern void *__deregister_frame_info(const void *) __attribute__((weak));18#endif1920#ifndef CRT_HAS_INITFINI_ARRAY21typedef void (*fp)(void);2223static fp __CTOR_LIST__[]24__attribute__((section(".ctors"), aligned(sizeof(fp)))) = {(fp)-1};25extern fp __CTOR_LIST_END__[];26#endif2728extern void __cxa_finalize(void *) __attribute__((weak));2930static void __attribute__((used)) __do_init(void) {31static _Bool __initialized;32if (__builtin_expect(__initialized, 0))33return;34__initialized = 1;3536#ifdef EH_USE_FRAME_REGISTRY37static struct { void *p[8]; } __object;38if (__register_frame_info)39__register_frame_info(__EH_FRAME_LIST__, &__object);40#endif41#ifndef CRT_HAS_INITFINI_ARRAY42const size_t n = __CTOR_LIST_END__ - __CTOR_LIST__ - 1;43for (size_t i = n; i >= 1; i--) __CTOR_LIST__[i]();44#endif45}4647#ifdef CRT_HAS_INITFINI_ARRAY48__attribute__((section(".init_array"),49used)) static void (*__init)(void) = __do_init;50#elif defined(__i386__) || defined(__x86_64__)51__asm__(".pushsection .init,\"ax\",@progbits\n\t"52"call __do_init\n\t"53".popsection");54#elif defined(__riscv)55__asm__(".pushsection .init,\"ax\",%progbits\n\t"56"call __do_init\n\t"57".popsection");58#elif defined(__arm__) || defined(__aarch64__)59__asm__(".pushsection .init,\"ax\",%progbits\n\t"60"bl __do_init\n\t"61".popsection");62#elif defined(__mips__)63__asm__(".pushsection .init,\"ax\",@progbits\n\t"64"jal __do_init\n\t"65".popsection");66#elif defined(__powerpc__) || defined(__powerpc64__)67__asm__(".pushsection .init,\"ax\",@progbits\n\t"68"bl __do_init\n\t"69"nop\n\t"70".popsection");71#elif defined(__sparc__)72__asm__(".pushsection .init,\"ax\",@progbits\n\t"73"call __do_init\n\t"74".popsection");75#else76#error "crtbegin without .init_fini array unimplemented for this architecture"77#endif // CRT_HAS_INITFINI_ARRAY7879#ifndef CRT_HAS_INITFINI_ARRAY80static fp __DTOR_LIST__[]81__attribute__((section(".dtors"), aligned(sizeof(fp)))) = {(fp)-1};82extern fp __DTOR_LIST_END__[];83#endif8485static void __attribute__((used)) __do_fini(void) {86static _Bool __finalized;87if (__builtin_expect(__finalized, 0))88return;89__finalized = 1;9091if (__cxa_finalize)92__cxa_finalize(__dso_handle);9394#ifndef CRT_HAS_INITFINI_ARRAY95const size_t n = __DTOR_LIST_END__ - __DTOR_LIST__ - 1;96for (size_t i = 1; i <= n; i++) __DTOR_LIST__[i]();97#endif98#ifdef EH_USE_FRAME_REGISTRY99if (__deregister_frame_info)100__deregister_frame_info(__EH_FRAME_LIST__);101#endif102}103104#ifdef CRT_HAS_INITFINI_ARRAY105__attribute__((section(".fini_array"),106used)) static void (*__fini)(void) = __do_fini;107#elif defined(__i386__) || defined(__x86_64__)108__asm__(".pushsection .fini,\"ax\",@progbits\n\t"109"call __do_fini\n\t"110".popsection");111#elif defined(__arm__) || defined(__aarch64__)112__asm__(".pushsection .fini,\"ax\",%progbits\n\t"113"bl __do_fini\n\t"114".popsection");115#elif defined(__mips__)116__asm__(".pushsection .fini,\"ax\",@progbits\n\t"117"jal __do_fini\n\t"118".popsection");119#elif defined(__powerpc__) || defined(__powerpc64__)120__asm__(".pushsection .fini,\"ax\",@progbits\n\t"121"bl __do_fini\n\t"122"nop\n\t"123".popsection");124#elif defined(__riscv)125__asm__(".pushsection .fini,\"ax\",@progbits\n\t"126"call __do_fini\n\t"127".popsection");128#elif defined(__sparc__)129__asm__(".pushsection .fini,\"ax\",@progbits\n\t"130"call __do_fini\n\t"131".popsection");132#else133#error "crtbegin without .init_fini array unimplemented for this architecture"134#endif // CRT_HAS_INIT_FINI_ARRAY135136137