OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / cygming-crtbegin.c
1 /* crtbegin object for windows32 targets.
2    Copyright (C) 2007  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 2, or (at your option) any later
11 version.
12
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file.  (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
20 executable.)
21
22 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
23 WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25 for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with GCC; see the file COPYING.  If not, write to the Free
29 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30 02111-1307, USA.  */
31
32 /* Target machine header files require this define. */
33 #define IN_LIBGCC2
34
35 #include "auto-host.h"
36 #include "tconfig.h"
37 #include "tsystem.h"
38 #include "coretypes.h"
39 #include "tm.h"
40 #include "unwind-dw2-fde.h"
41
42 #define WIN32_LEAN_AND_MEAN
43 #include <windows.h>
44
45 #ifndef LIBGCC_SONAME
46 #define LIBGCC_SONAME "libgcc_s.dll"
47 #endif
48
49 #ifndef LIBGCJ_SONAME
50 #define LIBGCJ_SONAME "libgcj_s.dll"
51 #endif
52
53
54 /* Make the declarations weak.  This is critical for
55    _Jv_RegisterClasses because it lives in libgcj.a  */
56 extern void __register_frame_info (const void *, struct object *)
57                                    TARGET_ATTRIBUTE_WEAK;
58 extern void *__deregister_frame_info (const void *)
59                                       TARGET_ATTRIBUTE_WEAK;
60 extern void _Jv_RegisterClasses (const void *) TARGET_ATTRIBUTE_WEAK;
61
62 #if defined(HAVE_LD_RO_RW_SECTION_MIXING)
63 # define EH_FRAME_SECTION_CONST const
64 #else
65 # define EH_FRAME_SECTION_CONST
66 #endif
67
68 /* Stick a label at the beginning of the frame unwind info so we can
69    register/deregister it with the exception handling library code.  */
70 #if DWARF2_UNWIND_INFO
71 static EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[]
72   __attribute__((section(EH_FRAME_SECTION_NAME), aligned(4)))
73   = { };
74
75 static struct object obj;
76 #endif
77
78 #if TARGET_USE_JCR_SECTION
79 static void *__JCR_LIST__[]
80   __attribute__ ((unused, section(JCR_SECTION_NAME), aligned(4)))
81   = { };
82 #endif
83
84 /* Pull in references from libgcc.a(unwind-dw2-fde.o) in the
85    startfile. These are referenced by a ctor and dtor in crtend.o.  */
86 extern void __gcc_register_frame (void);
87 extern void __gcc_deregister_frame (void);
88
89 void
90 __gcc_register_frame (void)
91 {
92 #if DWARF2_UNWIND_INFO
93 /* Weak undefined symbols won't be pulled in from dlls; hence
94    we first test if the dll is already loaded and, if so,
95    get the symbol's address at run-time.  If the dll is not loaded,
96    fallback to weak linkage to static archive.  */
97
98   void (*register_frame_fn) (const void *, struct object *);
99   HANDLE h = GetModuleHandle (LIBGCC_SONAME);
100   if (h)
101     register_frame_fn = (void (*) (const void *, struct object *))
102                         GetProcAddress (h, "__register_frame_info");
103   else 
104     register_frame_fn = __register_frame_info;
105   if (register_frame_fn)
106      register_frame_fn (__EH_FRAME_BEGIN__, &obj);
107 #endif
108
109 #if TARGET_USE_JCR_SECTION 
110   if (__JCR_LIST__[0])
111     {
112       void (*register_class_fn) (const void *);
113       HANDLE h = GetModuleHandle (LIBGCJ_SONAME);
114       if (h)
115         register_class_fn = (void (*) (const void *))
116                              GetProcAddress (h, "_Jv_RegisterClasses");
117       else
118         register_class_fn = _Jv_RegisterClasses;
119
120       if (register_class_fn)
121         register_class_fn (__JCR_LIST__);
122     }
123 #endif
124 }
125
126 void
127 __gcc_deregister_frame (void)
128 {
129 #if DWARF2_UNWIND_INFO
130   void *  (*deregister_frame_fn) (const void *);
131   HANDLE h = GetModuleHandle (LIBGCC_SONAME);
132   if (h)
133     deregister_frame_fn = (void* (*) (const void *))
134                           GetProcAddress (h, "__deregister_frame_info");
135   else 
136     deregister_frame_fn = __deregister_frame_info;
137   if (deregister_frame_fn)
138      deregister_frame_fn (__EH_FRAME_BEGIN__);
139 #endif
140 }