| 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 | typedef void (*func_ptr) (void); |
| 12 | extern func_ptr __CTOR_LIST__[]; |
| 13 | extern func_ptr __DTOR_LIST__[]; |
| 14 | |
| 15 | void __do_global_dtors (void); |
| 16 | void __do_global_ctors (void); |
| 17 | void __main (void); |
| 18 | |
| 19 | void |
| 20 | __do_global_dtors (void) |
| 21 | { |
| 22 | static func_ptr *p = __DTOR_LIST__ + 1; |
| 23 | |
| 24 | while (*p) |
| 25 | { |
| 26 | (*(p)) (); |
| 27 | p++; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | void |
| 32 | __do_global_ctors (void) |
| 33 | { |
| 34 | unsigned long nptrs = (unsigned long) (ptrdiff_t) __CTOR_LIST__[0]; |
| 35 | unsigned long i; |
| 36 | |
| 37 | if (nptrs == (unsigned long) -1) |
| 38 | { |
| 39 | for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++); |
| 40 | } |
| 41 | |
| 42 | for (i = nptrs; i >= 1; i--) |
| 43 | { |
| 44 | __CTOR_LIST__[i] (); |
| 45 | } |
| 46 | |
| 47 | atexit (__do_global_dtors); |
| 48 | } |
| 49 | |
| 50 | static int initialized = 0; |
| 51 | |
| 52 | __attribute__((used)) /* required for gcc -flto -Ofast */ |
| 53 | void |
| 54 | __main (void) |
| 55 | { |
| 56 | if (!initialized) |
| 57 | { |
| 58 | initialized = 1; |
| 59 | __do_global_ctors (); |
| 60 | } |
| 61 | } |