• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * This file has no copyright assigned and is placed in the Public Domain.
3  * This file is part of the mingw-w64 runtime package.
4  * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5  */
6 
7 #include <windows.h>
8 #include <stdlib.h>
9 #include <setjmp.h>
10 
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14 
15 typedef void (*func_ptr) (void);
16 extern func_ptr __CTOR_LIST__[];
17 extern func_ptr __DTOR_LIST__[];
18 
19 void __do_global_dtors (void);
20 void __do_global_ctors (void);
21 void __main (void);
22 
23 void
__do_global_dtors(void)24 __do_global_dtors (void)
25 {
26   static func_ptr *p = __DTOR_LIST__ + 1;
27 
28   while (*p)
29     {
30       (*(p)) ();
31       p++;
32     }
33 }
34 
35 #ifndef HAVE_CTOR_LIST
36 // If the linker didn't provide __CTOR_LIST__, we provided it ourselves,
37 // and then we also know we have __CTOR_END__ available.
38 extern func_ptr __CTOR_END__[];
39 extern func_ptr __DTOR_END__[];
40 
__do_global_ctors(void)41 void __do_global_ctors (void)
42 {
43   static func_ptr *p = __CTOR_END__ - 1;
44   while (*p != (func_ptr) -1) {
45     (*(p))();
46     p--;
47   }
48   atexit (__do_global_dtors);
49 }
50 
51 #else
52 // old method that iterates the list twice because old linker scripts do not have __CTOR_END__
53 
54 void
__do_global_ctors(void)55 __do_global_ctors (void)
56 {
57   unsigned long nptrs = (unsigned long) (ptrdiff_t) __CTOR_LIST__[0];
58   unsigned long i;
59 
60   if (nptrs == (unsigned long) -1)
61     {
62       for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++);
63     }
64 
65   for (i = nptrs; i >= 1; i--)
66     {
67       __CTOR_LIST__[i] ();
68     }
69 
70   atexit (__do_global_dtors);
71 }
72 
73 #endif
74 
75 static int initialized = 0;
76 
77 void
__main(void)78 __main (void)
79 {
80   if (!initialized)
81     {
82       initialized = 1;
83       __do_global_ctors ();
84     }
85 }
86