Home | History | Annotate | Line # | Download | only in i386
cygming-crtbegin.c revision 1.1
      1  1.1  mrg /* crtbegin object for windows32 targets.
      2  1.1  mrg    Copyright (C) 2007-2013 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.1  mrg 
     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.1  mrg extern void __register_frame_info (const void *, struct object *)
     53  1.1  mrg 				   TARGET_ATTRIBUTE_WEAK;
     54  1.1  mrg extern void *__deregister_frame_info (const void *)
     55  1.1  mrg 				      TARGET_ATTRIBUTE_WEAK;
     56  1.1  mrg extern void _Jv_RegisterClasses (const void *) TARGET_ATTRIBUTE_WEAK;
     57  1.1  mrg 
     58  1.1  mrg #if defined(HAVE_LD_RO_RW_SECTION_MIXING)
     59  1.1  mrg # define EH_FRAME_SECTION_CONST const
     60  1.1  mrg #else
     61  1.1  mrg # define EH_FRAME_SECTION_CONST
     62  1.1  mrg #endif
     63  1.1  mrg 
     64  1.1  mrg /* Stick a label at the beginning of the frame unwind info so we can
     65  1.1  mrg    register/deregister it with the exception handling library code.  */
     66  1.1  mrg #if DWARF2_UNWIND_INFO
     67  1.1  mrg static EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[]
     68  1.1  mrg   __attribute__((used, section(EH_FRAME_SECTION_NAME), aligned(4)))
     69  1.1  mrg   = { };
     70  1.1  mrg 
     71  1.1  mrg static struct object obj;
     72  1.1  mrg 
     73  1.1  mrg /* Handle of libgcc's DLL reference.  */
     74  1.1  mrg HANDLE hmod_libgcc;
     75  1.1  mrg #endif
     76  1.1  mrg 
     77  1.1  mrg #if TARGET_USE_JCR_SECTION
     78  1.1  mrg static void *__JCR_LIST__[]
     79  1.1  mrg   __attribute__ ((used, section(JCR_SECTION_NAME), aligned(4)))
     80  1.1  mrg   = { };
     81  1.1  mrg #endif
     82  1.1  mrg 
     83  1.1  mrg /* Pull in references from libgcc.a(unwind-dw2-fde.o) in the
     84  1.1  mrg    startfile. These are referenced by a ctor and dtor in crtend.o.  */
     85  1.1  mrg extern void __gcc_register_frame (void);
     86  1.1  mrg extern void __gcc_deregister_frame (void);
     87  1.1  mrg 
     88  1.1  mrg void
     89  1.1  mrg __gcc_register_frame (void)
     90  1.1  mrg {
     91  1.1  mrg #if DWARF2_UNWIND_INFO
     92  1.1  mrg /* Weak undefined symbols won't be pulled in from dlls; hence
     93  1.1  mrg    we first test if the dll is already loaded and, if so,
     94  1.1  mrg    get the symbol's address at run-time.  If the dll is not loaded,
     95  1.1  mrg    fallback to weak linkage to static archive.  */
     96  1.1  mrg 
     97  1.1  mrg   void (*register_frame_fn) (const void *, struct object *);
     98  1.1  mrg   HANDLE h = GetModuleHandle (LIBGCC_SONAME);
     99  1.1  mrg 
    100  1.1  mrg   if (h)
    101  1.1  mrg     {
    102  1.1  mrg       /* Increasing the load-count of LIBGCC_SONAME DLL.  */
    103  1.1  mrg       hmod_libgcc = LoadLibrary (LIBGCC_SONAME);
    104  1.1  mrg       register_frame_fn = (void (*) (const void *, struct object *))
    105  1.1  mrg 			  GetProcAddress (h, "__register_frame_info");
    106  1.1  mrg     }
    107  1.1  mrg   else
    108  1.1  mrg     register_frame_fn = __register_frame_info;
    109  1.1  mrg   if (register_frame_fn)
    110  1.1  mrg      register_frame_fn (__EH_FRAME_BEGIN__, &obj);
    111  1.1  mrg #endif
    112  1.1  mrg 
    113  1.1  mrg #if TARGET_USE_JCR_SECTION
    114  1.1  mrg   if (__JCR_LIST__[0])
    115  1.1  mrg     {
    116  1.1  mrg       void (*register_class_fn) (const void *);
    117  1.1  mrg       HANDLE h = GetModuleHandle (LIBGCJ_SONAME);
    118  1.1  mrg       if (h)
    119  1.1  mrg 	register_class_fn = (void (*) (const void *))
    120  1.1  mrg 			     GetProcAddress (h, "_Jv_RegisterClasses");
    121  1.1  mrg       else
    122  1.1  mrg 	register_class_fn = _Jv_RegisterClasses;
    123  1.1  mrg 
    124  1.1  mrg       if (register_class_fn)
    125  1.1  mrg 	register_class_fn (__JCR_LIST__);
    126  1.1  mrg     }
    127  1.1  mrg #endif
    128  1.1  mrg }
    129  1.1  mrg 
    130  1.1  mrg void
    131  1.1  mrg __gcc_deregister_frame (void)
    132  1.1  mrg {
    133  1.1  mrg #if DWARF2_UNWIND_INFO
    134  1.1  mrg   void *  (*deregister_frame_fn) (const void *);
    135  1.1  mrg   HANDLE h = GetModuleHandle (LIBGCC_SONAME);
    136  1.1  mrg   if (h)
    137  1.1  mrg     deregister_frame_fn = (void* (*) (const void *))
    138  1.1  mrg 			  GetProcAddress (h, "__deregister_frame_info");
    139  1.1  mrg   else
    140  1.1  mrg     deregister_frame_fn = __deregister_frame_info;
    141  1.1  mrg   if (deregister_frame_fn)
    142  1.1  mrg      deregister_frame_fn (__EH_FRAME_BEGIN__);
    143  1.1  mrg   if (hmod_libgcc)
    144  1.1  mrg     FreeLibrary (hmod_libgcc);
    145  1.1  mrg #endif
    146  1.1  mrg }
    147