Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
crtbegin.c
Go to the documentation of this file.
1 // Pulled from llvm commit review
2 
3 #include "__dso_handle.h"
4 #include <stddef.h>
5 
7 __attribute__((visibility("hidden"))) void* __dso_handle = (void*)NULL;
8 
9 // TODO: check that this form is a viable alternative to zero-length-array
10 static long* __EH_FRAME_LIST__ __attribute__((section(".eh_frame"), aligned(sizeof(void*))));
11 // static long __EH_FRAME_LIST__[]
12 // __attribute__((section(".eh_frame"), aligned(sizeof(void *)))) = {};
13 
14 extern void __register_frame_info(const void*, void*) __attribute__((weak));
15 extern void* __deregister_frame_info(const void*) __attribute__((weak));
16 
17 #ifndef CRT_HAS_INITFINI_ARRAY
18 typedef void (*fp)(void);
19 
20 static fp __CTOR_LIST__[] __attribute__((section(".ctors"), aligned(sizeof(fp)), used)) = {(fp)-1};
21 extern fp __CTOR_LIST_END__[];
22 #endif
23 
24 static void __attribute__((used)) __do_init()
25 {
26  static _Bool __initialized;
27  if(__builtin_expect(__initialized, 0))
28  {
29  return;
30  }
31  __initialized = 1;
32 
33  static struct
34  {
35  void* p[8];
36  } __object;
37  if(__register_frame_info)
38  {
39  __register_frame_info(__EH_FRAME_LIST__, &__object);
40  }
41 
42 #ifndef CRT_HAS_INITFINI_ARRAY
43  const size_t n = (size_t)__CTOR_LIST_END__ - (size_t)__CTOR_LIST__ - (size_t)1;
44  for(size_t i = n; i >= 1; i--)
45  {
46  __CTOR_LIST__[i]();
47  }
48 #endif
49 }
50 
51 #ifdef CRT_HAS_INITFINI_ARRAY
52 __attribute__((section(".init_array"), used)) static void (*__init)(void) = __do_init;
53 #else // CRT_HAS_INITFINI_ARRAY
54 #if defined(__i386__) || defined(__x86_64__)
55 __asm__(".pushsection .init,\"ax\",@progbits\n\t"
56  "call " __USER_LABEL_PREFIX__ "__do_init\n\t"
57  ".popsection");
58 #elif defined(__arm__)
59 __asm__(".pushsection .init,\"ax\",%progbits\n\t"
60  "bl " __USER_LABEL_PREFIX__ "__do_init\n\t"
61  ".popsection");
62 #endif // CRT_HAS_INITFINI_ARRAY
63 #endif
64 
65 #ifndef CRT_HAS_INITFINI_ARRAY
66 static fp __DTOR_LIST__[] __attribute__((section(".dtors"), aligned(sizeof(fp)), used)) = {(fp)-1};
67 extern fp __DTOR_LIST_END__[];
68 #endif
69 
70 static void __attribute__((used)) __do_fini()
71 {
72  static _Bool __finalized;
73  if(__builtin_expect(__finalized, 0))
74  {
75  return;
76  }
77  __finalized = 1;
78 
79 #ifndef CRT_HAS_INITFINI_ARRAY
80  if(__deregister_frame_info)
81  {
82  __deregister_frame_info(__EH_FRAME_LIST__);
83  }
84 
85  const size_t n = (size_t)__DTOR_LIST_END__ - (size_t)__DTOR_LIST__ - (size_t)1;
86  for(size_t i = 1; i < n; i++)
87  {
88  __DTOR_LIST__[i]();
89  }
90 #endif
91 }
92 
93 #ifdef CRT_HAS_INITFINI_ARRAY
94 __attribute__((section(".fini_array"), used)) static void (*__fini)(void) = __do_fini;
95 #else // CRT_HAS_INITFINI_ARRAY
96 #if defined(__i386__) || defined(__x86_64__)
97 __asm__(".pushsection .fini,\"ax\",@progbits\n\t"
98  "call " __USER_LABEL_PREFIX__ "__do_fini\n\t"
99  ".popsection");
100 #elif defined(__arm__)
101 __asm__(".pushsection .fini,\"ax\",%progbits\n\t"
102  "bl " __USER_LABEL_PREFIX__ "__do_fini\n\t"
103  ".popsection");
104 #endif
105 #endif // CRT_HAS_INIT_FINI_ARRAY
__attribute__((visibility("hidden")))
We don't have shared libraries, so dso_handle is just NULL.
Definition: crtbegin.c:7
fp __CTOR_LIST_END__[]
void(* fp)(void)
Definition: crtend.c:8
#define NULL
Definition: stddef.h:15
fp __DTOR_LIST_END__[]