cygming-crtbegin.c revision 1.10 1 1.1 mrg /* crtbegin object for windows32 targets.
2 1.10 mrg Copyright (C) 2007-2022 Free Software Foundation, Inc.
3 1.1 mrg
4 1.1 mrg Contributed by Danny Smith <dannysmith (at) users.sourceforge.net>
5 1.1 mrg
6 1.1 mrg This file is part of GCC.
7 1.1 mrg
8 1.1 mrg GCC is free software; you can redistribute it and/or modify it under
9 1.1 mrg the terms of the GNU General Public License as published by the Free
10 1.1 mrg Software Foundation; either version 3, or (at your option) any later
11 1.1 mrg version.
12 1.1 mrg
13 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 1.1 mrg for more details.
17 1.1 mrg
18 1.1 mrg Under Section 7 of GPL version 3, you are granted additional
19 1.1 mrg permissions described in the GCC Runtime Library Exception, version
20 1.1 mrg 3.1, as published by the Free Software Foundation.
21 1.1 mrg
22 1.1 mrg You should have received a copy of the GNU General Public License and
23 1.1 mrg a copy of the GCC Runtime Library Exception along with this program;
24 1.1 mrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 1.1 mrg <http://www.gnu.org/licenses/>. */
26 1.1 mrg
27 1.1 mrg /* Target machine header files require this define. */
28 1.1 mrg #define IN_LIBGCC2
29 1.1 mrg
30 1.1 mrg #include "auto-host.h"
31 1.1 mrg #include "tconfig.h"
32 1.1 mrg #include "tsystem.h"
33 1.1 mrg #include "coretypes.h"
34 1.1 mrg #include "tm.h"
35 1.1 mrg #include "libgcc_tm.h"
36 1.1 mrg #include "unwind-dw2-fde.h"
37 1.1 mrg
38 1.1 mrg #define WIN32_LEAN_AND_MEAN
39 1.1 mrg #include <windows.h>
40 1.1 mrg
41 1.1 mrg #ifndef LIBGCC_SONAME
42 1.1 mrg #define LIBGCC_SONAME "libgcc_s.dll"
43 1.1 mrg #endif
44 1.1 mrg
45 1.3 mrg #if DWARF2_UNWIND_INFO
46 1.1 mrg /* Make the declarations weak. This is critical for
47 1.1 mrg _Jv_RegisterClasses because it lives in libgcj.a */
48 1.3 mrg extern void __register_frame_info (__attribute__((unused)) const void *,
49 1.3 mrg __attribute__((unused)) struct object *)
50 1.1 mrg TARGET_ATTRIBUTE_WEAK;
51 1.3 mrg extern void *__deregister_frame_info (__attribute__((unused)) const void *)
52 1.1 mrg TARGET_ATTRIBUTE_WEAK;
53 1.3 mrg
54 1.3 mrg /* Work around for current cygwin32 build problems (Bug gas/16858).
55 1.3 mrg Compile weak default functions only for 64-bit systems,
56 1.3 mrg when absolutely necessary. */
57 1.3 mrg #ifdef __x86_64__
58 1.3 mrg TARGET_ATTRIBUTE_WEAK void
59 1.3 mrg __register_frame_info (__attribute__((unused)) const void *p,
60 1.3 mrg __attribute__((unused)) struct object *o)
61 1.3 mrg {
62 1.3 mrg }
63 1.3 mrg
64 1.3 mrg TARGET_ATTRIBUTE_WEAK void *
65 1.3 mrg __deregister_frame_info (__attribute__((unused)) const void *p)
66 1.3 mrg {
67 1.3 mrg return (void*) 0;
68 1.3 mrg }
69 1.3 mrg #endif
70 1.3 mrg #endif /* DWARF2_UNWIND_INFO */
71 1.3 mrg
72 1.1 mrg #if defined(HAVE_LD_RO_RW_SECTION_MIXING)
73 1.1 mrg # define EH_FRAME_SECTION_CONST const
74 1.1 mrg #else
75 1.1 mrg # define EH_FRAME_SECTION_CONST
76 1.1 mrg #endif
77 1.1 mrg
78 1.1 mrg /* Stick a label at the beginning of the frame unwind info so we can
79 1.1 mrg register/deregister it with the exception handling library code. */
80 1.1 mrg #if DWARF2_UNWIND_INFO
81 1.1 mrg static EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[]
82 1.3 mrg __attribute__((used, section(__LIBGCC_EH_FRAME_SECTION_NAME__), aligned(4)))
83 1.1 mrg = { };
84 1.1 mrg
85 1.1 mrg static struct object obj;
86 1.1 mrg
87 1.1 mrg /* Handle of libgcc's DLL reference. */
88 1.1 mrg HANDLE hmod_libgcc;
89 1.3 mrg static void * (*deregister_frame_fn) (const void *) = NULL;
90 1.1 mrg #endif
91 1.1 mrg
92 1.3 mrg #ifdef __CYGWIN__
93 1.3 mrg /* Declare the __dso_handle variable. It should have a unique value
94 1.3 mrg in every shared-object; in a main program its value is zero. The
95 1.3 mrg object should in any case be protected. This means the instance
96 1.3 mrg in one DSO or the main program is not used in another object. The
97 1.3 mrg dynamic linker takes care of this. */
98 1.3 mrg
99 1.3 mrg #ifdef CRTSTUFFS_O
100 1.3 mrg extern void *__ImageBase;
101 1.3 mrg void *__dso_handle = &__ImageBase;
102 1.3 mrg #else
103 1.3 mrg void *__dso_handle = 0;
104 1.3 mrg #endif
105 1.3 mrg
106 1.3 mrg #endif /* __CYGWIN__ */
107 1.3 mrg
108 1.3 mrg
109 1.1 mrg /* Pull in references from libgcc.a(unwind-dw2-fde.o) in the
110 1.1 mrg startfile. These are referenced by a ctor and dtor in crtend.o. */
111 1.1 mrg extern void __gcc_register_frame (void);
112 1.1 mrg extern void __gcc_deregister_frame (void);
113 1.1 mrg
114 1.1 mrg void
115 1.1 mrg __gcc_register_frame (void)
116 1.1 mrg {
117 1.1 mrg #if DWARF2_UNWIND_INFO
118 1.1 mrg /* Weak undefined symbols won't be pulled in from dlls; hence
119 1.1 mrg we first test if the dll is already loaded and, if so,
120 1.1 mrg get the symbol's address at run-time. If the dll is not loaded,
121 1.1 mrg fallback to weak linkage to static archive. */
122 1.1 mrg
123 1.1 mrg void (*register_frame_fn) (const void *, struct object *);
124 1.1 mrg HANDLE h = GetModuleHandle (LIBGCC_SONAME);
125 1.1 mrg
126 1.1 mrg if (h)
127 1.1 mrg {
128 1.1 mrg /* Increasing the load-count of LIBGCC_SONAME DLL. */
129 1.1 mrg hmod_libgcc = LoadLibrary (LIBGCC_SONAME);
130 1.1 mrg register_frame_fn = (void (*) (const void *, struct object *))
131 1.1 mrg GetProcAddress (h, "__register_frame_info");
132 1.3 mrg deregister_frame_fn = (void* (*) (const void *))
133 1.3 mrg GetProcAddress (h, "__deregister_frame_info");
134 1.3 mrg }
135 1.3 mrg else
136 1.3 mrg {
137 1.3 mrg register_frame_fn = __register_frame_info;
138 1.3 mrg deregister_frame_fn = __deregister_frame_info;
139 1.1 mrg }
140 1.1 mrg if (register_frame_fn)
141 1.1 mrg register_frame_fn (__EH_FRAME_BEGIN__, &obj);
142 1.1 mrg #endif
143 1.1 mrg
144 1.3 mrg #if DEFAULT_USE_CXA_ATEXIT
145 1.3 mrg /* If we use the __cxa_atexit method to register C++ dtors
146 1.3 mrg at object construction, also use atexit to register eh frame
147 1.3 mrg info cleanup. */
148 1.3 mrg atexit(__gcc_deregister_frame);
149 1.3 mrg #endif /* DEFAULT_USE_CXA_ATEXIT */
150 1.1 mrg }
151 1.1 mrg
152 1.1 mrg void
153 1.1 mrg __gcc_deregister_frame (void)
154 1.1 mrg {
155 1.1 mrg #if DWARF2_UNWIND_INFO
156 1.1 mrg if (deregister_frame_fn)
157 1.1 mrg deregister_frame_fn (__EH_FRAME_BEGIN__);
158 1.1 mrg if (hmod_libgcc)
159 1.1 mrg FreeLibrary (hmod_libgcc);
160 1.1 mrg #endif
161 1.1 mrg }
162