cygming-crtbegin.c revision 1.3 1 1.1 mrg /* crtbegin object for windows32 targets.
2 1.3 mrg Copyright (C) 2007-2015 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.1 mrg #ifndef LIBGCJ_SONAME
46 1.1 mrg #define LIBGCJ_SONAME "libgcj_s.dll"
47 1.1 mrg #endif
48 1.1 mrg
49 1.3 mrg #if DWARF2_UNWIND_INFO
50 1.1 mrg /* Make the declarations weak. This is critical for
51 1.1 mrg _Jv_RegisterClasses because it lives in libgcj.a */
52 1.3 mrg extern void __register_frame_info (__attribute__((unused)) const void *,
53 1.3 mrg __attribute__((unused)) struct object *)
54 1.1 mrg TARGET_ATTRIBUTE_WEAK;
55 1.3 mrg extern void *__deregister_frame_info (__attribute__((unused)) const void *)
56 1.1 mrg TARGET_ATTRIBUTE_WEAK;
57 1.3 mrg
58 1.3 mrg /* Work around for current cygwin32 build problems (Bug gas/16858).
59 1.3 mrg Compile weak default functions only for 64-bit systems,
60 1.3 mrg when absolutely necessary. */
61 1.3 mrg #ifdef __x86_64__
62 1.3 mrg TARGET_ATTRIBUTE_WEAK void
63 1.3 mrg __register_frame_info (__attribute__((unused)) const void *p,
64 1.3 mrg __attribute__((unused)) struct object *o)
65 1.3 mrg {
66 1.3 mrg }
67 1.3 mrg
68 1.3 mrg TARGET_ATTRIBUTE_WEAK void *
69 1.3 mrg __deregister_frame_info (__attribute__((unused)) const void *p)
70 1.3 mrg {
71 1.3 mrg return (void*) 0;
72 1.3 mrg }
73 1.3 mrg #endif
74 1.3 mrg #endif /* DWARF2_UNWIND_INFO */
75 1.3 mrg
76 1.3 mrg #if TARGET_USE_JCR_SECTION
77 1.3 mrg extern void _Jv_RegisterClasses (__attribute__((unused)) const void *)
78 1.3 mrg TARGET_ATTRIBUTE_WEAK;
79 1.3 mrg
80 1.3 mrg #ifdef __x86_64__
81 1.3 mrg TARGET_ATTRIBUTE_WEAK void
82 1.3 mrg _Jv_RegisterClasses (__attribute__((unused)) const void *p)
83 1.3 mrg {
84 1.3 mrg }
85 1.3 mrg #endif
86 1.3 mrg #endif /* TARGET_USE_JCR_SECTION */
87 1.1 mrg
88 1.1 mrg #if defined(HAVE_LD_RO_RW_SECTION_MIXING)
89 1.1 mrg # define EH_FRAME_SECTION_CONST const
90 1.1 mrg #else
91 1.1 mrg # define EH_FRAME_SECTION_CONST
92 1.1 mrg #endif
93 1.1 mrg
94 1.1 mrg /* Stick a label at the beginning of the frame unwind info so we can
95 1.1 mrg register/deregister it with the exception handling library code. */
96 1.1 mrg #if DWARF2_UNWIND_INFO
97 1.1 mrg static EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[]
98 1.3 mrg __attribute__((used, section(__LIBGCC_EH_FRAME_SECTION_NAME__), aligned(4)))
99 1.1 mrg = { };
100 1.1 mrg
101 1.1 mrg static struct object obj;
102 1.1 mrg
103 1.1 mrg /* Handle of libgcc's DLL reference. */
104 1.1 mrg HANDLE hmod_libgcc;
105 1.3 mrg static void * (*deregister_frame_fn) (const void *) = NULL;
106 1.1 mrg #endif
107 1.1 mrg
108 1.1 mrg #if TARGET_USE_JCR_SECTION
109 1.1 mrg static void *__JCR_LIST__[]
110 1.3 mrg __attribute__ ((used, section(__LIBGCC_JCR_SECTION_NAME__), aligned(4)))
111 1.1 mrg = { };
112 1.1 mrg #endif
113 1.1 mrg
114 1.3 mrg #ifdef __CYGWIN__
115 1.3 mrg /* Declare the __dso_handle variable. It should have a unique value
116 1.3 mrg in every shared-object; in a main program its value is zero. The
117 1.3 mrg object should in any case be protected. This means the instance
118 1.3 mrg in one DSO or the main program is not used in another object. The
119 1.3 mrg dynamic linker takes care of this. */
120 1.3 mrg
121 1.3 mrg #ifdef CRTSTUFFS_O
122 1.3 mrg extern void *__ImageBase;
123 1.3 mrg void *__dso_handle = &__ImageBase;
124 1.3 mrg #else
125 1.3 mrg void *__dso_handle = 0;
126 1.3 mrg #endif
127 1.3 mrg
128 1.3 mrg #endif /* __CYGWIN__ */
129 1.3 mrg
130 1.3 mrg
131 1.1 mrg /* Pull in references from libgcc.a(unwind-dw2-fde.o) in the
132 1.1 mrg startfile. These are referenced by a ctor and dtor in crtend.o. */
133 1.1 mrg extern void __gcc_register_frame (void);
134 1.1 mrg extern void __gcc_deregister_frame (void);
135 1.1 mrg
136 1.1 mrg void
137 1.1 mrg __gcc_register_frame (void)
138 1.1 mrg {
139 1.1 mrg #if DWARF2_UNWIND_INFO
140 1.1 mrg /* Weak undefined symbols won't be pulled in from dlls; hence
141 1.1 mrg we first test if the dll is already loaded and, if so,
142 1.1 mrg get the symbol's address at run-time. If the dll is not loaded,
143 1.1 mrg fallback to weak linkage to static archive. */
144 1.1 mrg
145 1.1 mrg void (*register_frame_fn) (const void *, struct object *);
146 1.1 mrg HANDLE h = GetModuleHandle (LIBGCC_SONAME);
147 1.1 mrg
148 1.1 mrg if (h)
149 1.1 mrg {
150 1.1 mrg /* Increasing the load-count of LIBGCC_SONAME DLL. */
151 1.1 mrg hmod_libgcc = LoadLibrary (LIBGCC_SONAME);
152 1.1 mrg register_frame_fn = (void (*) (const void *, struct object *))
153 1.1 mrg GetProcAddress (h, "__register_frame_info");
154 1.3 mrg deregister_frame_fn = (void* (*) (const void *))
155 1.3 mrg GetProcAddress (h, "__deregister_frame_info");
156 1.3 mrg }
157 1.3 mrg else
158 1.3 mrg {
159 1.3 mrg register_frame_fn = __register_frame_info;
160 1.3 mrg deregister_frame_fn = __deregister_frame_info;
161 1.1 mrg }
162 1.1 mrg if (register_frame_fn)
163 1.1 mrg register_frame_fn (__EH_FRAME_BEGIN__, &obj);
164 1.1 mrg #endif
165 1.1 mrg
166 1.1 mrg #if TARGET_USE_JCR_SECTION
167 1.1 mrg if (__JCR_LIST__[0])
168 1.1 mrg {
169 1.1 mrg void (*register_class_fn) (const void *);
170 1.1 mrg HANDLE h = GetModuleHandle (LIBGCJ_SONAME);
171 1.1 mrg if (h)
172 1.1 mrg register_class_fn = (void (*) (const void *))
173 1.1 mrg GetProcAddress (h, "_Jv_RegisterClasses");
174 1.1 mrg else
175 1.1 mrg register_class_fn = _Jv_RegisterClasses;
176 1.1 mrg
177 1.1 mrg if (register_class_fn)
178 1.1 mrg register_class_fn (__JCR_LIST__);
179 1.1 mrg }
180 1.1 mrg #endif
181 1.3 mrg
182 1.3 mrg #if DEFAULT_USE_CXA_ATEXIT
183 1.3 mrg /* If we use the __cxa_atexit method to register C++ dtors
184 1.3 mrg at object construction, also use atexit to register eh frame
185 1.3 mrg info cleanup. */
186 1.3 mrg atexit(__gcc_deregister_frame);
187 1.3 mrg #endif /* DEFAULT_USE_CXA_ATEXIT */
188 1.1 mrg }
189 1.1 mrg
190 1.1 mrg void
191 1.1 mrg __gcc_deregister_frame (void)
192 1.1 mrg {
193 1.1 mrg #if DWARF2_UNWIND_INFO
194 1.1 mrg if (deregister_frame_fn)
195 1.1 mrg deregister_frame_fn (__EH_FRAME_BEGIN__);
196 1.1 mrg if (hmod_libgcc)
197 1.1 mrg FreeLibrary (hmod_libgcc);
198 1.1 mrg #endif
199 1.1 mrg }
200