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