xref: /netbsd-src/external/gpl3/gcc/dist/libgcc/config/i386/cygming-crtbegin.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /* crtbegin object for windows32 targets.
2    Copyright (C) 2007-2013 Free Software Foundation, Inc.
3 
4    Contributed by Danny Smith <dannysmith@users.sourceforge.net>
5 
6 This file is part of GCC.
7 
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12 
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17 
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21 
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
25 <http://www.gnu.org/licenses/>.  */
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"
35 #include "libgcc_tm.h"
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 
49 
50 /* Make the declarations weak.  This is critical for
51    _Jv_RegisterClasses because it lives in libgcj.a  */
52 extern void __register_frame_info (const void *, struct object *)
53 				   TARGET_ATTRIBUTE_WEAK;
54 extern void *__deregister_frame_info (const void *)
55 				      TARGET_ATTRIBUTE_WEAK;
56 extern void _Jv_RegisterClasses (const void *) TARGET_ATTRIBUTE_WEAK;
57 
58 #if defined(HAVE_LD_RO_RW_SECTION_MIXING)
59 # define EH_FRAME_SECTION_CONST const
60 #else
61 # define EH_FRAME_SECTION_CONST
62 #endif
63 
64 /* Stick a label at the beginning of the frame unwind info so we can
65    register/deregister it with the exception handling library code.  */
66 #if DWARF2_UNWIND_INFO
67 static EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[]
68   __attribute__((used, section(EH_FRAME_SECTION_NAME), aligned(4)))
69   = { };
70 
71 static struct object obj;
72 
73 /* Handle of libgcc's DLL reference.  */
74 HANDLE hmod_libgcc;
75 #endif
76 
77 #if TARGET_USE_JCR_SECTION
78 static void *__JCR_LIST__[]
79   __attribute__ ((used, section(JCR_SECTION_NAME), aligned(4)))
80   = { };
81 #endif
82 
83 /* Pull in references from libgcc.a(unwind-dw2-fde.o) in the
84    startfile. These are referenced by a ctor and dtor in crtend.o.  */
85 extern void __gcc_register_frame (void);
86 extern void __gcc_deregister_frame (void);
87 
88 void
89 __gcc_register_frame (void)
90 {
91 #if DWARF2_UNWIND_INFO
92 /* Weak undefined symbols won't be pulled in from dlls; hence
93    we first test if the dll is already loaded and, if so,
94    get the symbol's address at run-time.  If the dll is not loaded,
95    fallback to weak linkage to static archive.  */
96 
97   void (*register_frame_fn) (const void *, struct object *);
98   HANDLE h = GetModuleHandle (LIBGCC_SONAME);
99 
100   if (h)
101     {
102       /* Increasing the load-count of LIBGCC_SONAME DLL.  */
103       hmod_libgcc = LoadLibrary (LIBGCC_SONAME);
104       register_frame_fn = (void (*) (const void *, struct object *))
105 			  GetProcAddress (h, "__register_frame_info");
106     }
107   else
108     register_frame_fn = __register_frame_info;
109   if (register_frame_fn)
110      register_frame_fn (__EH_FRAME_BEGIN__, &obj);
111 #endif
112 
113 #if TARGET_USE_JCR_SECTION
114   if (__JCR_LIST__[0])
115     {
116       void (*register_class_fn) (const void *);
117       HANDLE h = GetModuleHandle (LIBGCJ_SONAME);
118       if (h)
119 	register_class_fn = (void (*) (const void *))
120 			     GetProcAddress (h, "_Jv_RegisterClasses");
121       else
122 	register_class_fn = _Jv_RegisterClasses;
123 
124       if (register_class_fn)
125 	register_class_fn (__JCR_LIST__);
126     }
127 #endif
128 }
129 
130 void
131 __gcc_deregister_frame (void)
132 {
133 #if DWARF2_UNWIND_INFO
134   void *  (*deregister_frame_fn) (const void *);
135   HANDLE h = GetModuleHandle (LIBGCC_SONAME);
136   if (h)
137     deregister_frame_fn = (void* (*) (const void *))
138 			  GetProcAddress (h, "__deregister_frame_info");
139   else
140     deregister_frame_fn = __deregister_frame_info;
141   if (deregister_frame_fn)
142      deregister_frame_fn (__EH_FRAME_BEGIN__);
143   if (hmod_libgcc)
144     FreeLibrary (hmod_libgcc);
145 #endif
146 }
147