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