This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

ctor/dtor support for DJGPP


Hello,

DJGPP currently uses it's own scheme for handling ctors, dtors, and initializing C++ 
exception handling. This patch adds support for .fini sections for non-ELF targets 
when .init sections are also being used. Another patch will follow to change DJGPP's 
config to enable it to use these changes.

gas/ChangeLog

1999-08-17 Mark Elbrecht <snowball3@bigfoot.com>

* libgcc2.c (__do_global_dtors): Don't define when both .init
  and .fini sections are defined for non-ELF targets.
 (exit) [L_EXIT]: Don't call __do_global_dtors or use its replacement
 when both .init and .fini sections are defined for non-ELF targets.

* crtstuff.c: Update copyright. Document effect of FINI_SECTION_ASM_OP on
  non-ELF, non-MACHO targets. Update comments associated with certain conditionals 
to include the word 'not'.

* crtstuff.c [!OBJ_FORMAT_MACHO, !OBJ_FORMAT_ELF]: When FINI_SECTION_ASM_OP
  is defined, place the code for calling destructors in the .fini section.

*** gcc/libgcc2.c.orig	Thu Jun 10 23:12:22 1999
--- gcc/libgcc2.c	Tue Aug 17 15:04:38 1999
*************** cacheflush (char *beg, int size, int fla
*** 2831,2839 ****
  #undef HAS_INIT_SECTION
  #define HAS_INIT_SECTION
  #endif
  
! #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
  /* Run all the global destructors on exit from the program.  */
  
  void
  __do_global_dtors ()
--- 2831,2840 ----
  #undef HAS_INIT_SECTION
  #define HAS_INIT_SECTION
  #endif
  
! #if !defined (OBJ_FORMAT_ELF) \
!     && (!defined (INIT_SECTION_ASM_OP) || !defined (FINI_SECTION_ASM_OP))
  /* Run all the global destructors on exit from the program.  */
  
  void
  __do_global_dtors ()
*************** extern void _exit () __attribute__ ((nor
*** 2981,2989 ****
  
  void 
  exit (int status)
  {
! #if !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF)
  #ifdef NEED_ATEXIT
    if (atexit_chain)
      {
        for ( ; last_atexit_chain_slot-- >= 0; )
--- 2982,2991 ----
  
  void 
  exit (int status)
  {
! #if !defined (OBJ_FORMAT_ELF) \
!     && (!defined (INIT_SECTION_ASM_OP) || !defined (FINI_SECTION_ASM_OP))
  #ifdef NEED_ATEXIT
    if (atexit_chain)
      {
        for ( ; last_atexit_chain_slot-- >= 0; )
*************** exit (int status)
*** 2996,3004 ****
      }
  #else /* No NEED_ATEXIT */
    __do_global_dtors ();
  #endif /* No NEED_ATEXIT */
! #endif /* !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF) */
  /* In gbl-ctors.h, ON_EXIT is defined if HAVE_ATEXIT is defined.  In
     __bb_init_func and _bb_init_prg, __bb_exit_func is registered with
     ON_EXIT if ON_EXIT is defined.  Thus we must not call __bb_exit_func here
     if HAVE_ATEXIT is defined. */
--- 2998,3007 ----
      }
  #else /* No NEED_ATEXIT */
    __do_global_dtors ();
  #endif /* No NEED_ATEXIT */
! #endif /* !defined (OBJ_FORMAT_ELF) && (!defined (INIT_SECTION_ASM_OP)
!             || !defined (FINI_SECTION_ASM_OP)) */
  /* In gbl-ctors.h, ON_EXIT is defined if HAVE_ATEXIT is defined.  In
     __bb_init_func and _bb_init_prg, __bb_exit_func is registered with
     ON_EXIT if ON_EXIT is defined.  Thus we must not call __bb_exit_func here
     if HAVE_ATEXIT is defined. */
*** gcc/crtstuff.c.orig	Mon Mar 22 19:43:50 1999
--- gcc/crtstuff.c	Tue Aug 17 15:28:50 1999
***************
*** 1,7 ****
  /* Specialized bits of code needed to support construction and
     destruction of file-scope objects in C++ code.
!    Copyright (C) 1991, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
     Contributed by Ron Guilmette (rfg@monkeys.com).
  
  This file is part of GNU CC.
  
--- 1,7 ----
  /* Specialized bits of code needed to support construction and
     destruction of file-scope objects in C++ code.
!    Copyright (C) 1991, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
     Contributed by Ron Guilmette (rfg@monkeys.com).
  
  This file is part of GNU CC.
  
*************** Boston, MA 02111-1307, USA.  */
*** 39,46 ****
--- 39,51 ----
     Note that if INIT_SECTION_ASM_OP is defined in the tm.h file, then
     this file *will* make use of the .init section.  If that symbol is
     not defined however, then the .init section will not be used.
  
+    For COFF and other non-ELF object formats, if FINI_SECTION_ASM_OP is
+    defined along with INIT_SECTION_ASM_OP in a target's config file,
+    then this file will also make use of the .fini section and take care
+    of initializing C++ exception handling.
+ 
     Currently, only ELF and COFF are supported.  It is likely however that
     ROSE could also be supported, if someone was willing to do the work to
     make whatever (small?) adaptations are needed.  (Some work may be
     needed on the ROSE assembler and linker also.)
*************** extern void *__deregister_frame_info (vo
*** 117,134 ****
  /*  Declare a pointer to void function type.  */
  typedef void (*func_ptr) (void);
  #define STATIC static
  
! #else  /* OBJECT_FORMAT_ELF */
  
  #include "gbl-ctors.h"
  
  #ifndef ON_EXIT
  #define ON_EXIT(a, b)
  #endif
  #define STATIC
  
! #endif /* OBJECT_FORMAT_ELF */
  
  #ifdef CRT_BEGIN
  
  #ifdef INIT_SECTION_ASM_OP
--- 122,141 ----
  /*  Declare a pointer to void function type.  */
  typedef void (*func_ptr) (void);
  #define STATIC static
  
! #else  /* not OBJECT_FORMAT_ELF */
  
  #include "gbl-ctors.h"
  
  #ifndef ON_EXIT
  #define ON_EXIT(a, b)
  #endif
  #define STATIC
  
! STATIC char __EH_FRAME_BEGIN__[];
! 
! #endif /* not OBJECT_FORMAT_ELF */
  
  #ifdef CRT_BEGIN
  
  #ifdef INIT_SECTION_ASM_OP
*************** init_dummy (void)
*** 216,224 ****
    asm (TEXT_SECTION_ASM_OP);
  }
  #endif /* EH_FRAME_SECTION_ASM_OP */
  
! #else  /* OBJECT_FORMAT_ELF */
  
  /* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
     and once in crtend.o).  It must be declared static to avoid a link
     error.  Here, we define __do_global_ctors as an externally callable
--- 223,231 ----
    asm (TEXT_SECTION_ASM_OP);
  }
  #endif /* EH_FRAME_SECTION_ASM_OP */
  
! #else  /* not OBJECT_FORMAT_ELF */
  
  /* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
     and once in crtend.o).  It must be declared static to avoid a link
     error.  Here, we define __do_global_ctors as an externally callable
*************** __do_global_ctors_aux (void)	/* prologue
*** 264,277 ****
    FORCE_INIT_SECTION_ALIGN;	/* Explicit align before switch to .text */
  #endif
    asm (TEXT_SECTION_ASM_OP);	/* don't put epilogue and body in .init */
    DO_GLOBAL_CTORS_BODY;
    ON_EXIT (__do_global_dtors, 0);
  }
  
! #endif /* OBJECT_FORMAT_ELF */
  
! #else /* defined(INIT_SECTION_ASM_OP) */
  
  #ifdef HAS_INIT_SECTION
  /* This case is used by the Irix 6 port, which supports named sections but
     not an SVR4-style .fini section.  __do_global_dtors can be non-static
--- 271,305 ----
    FORCE_INIT_SECTION_ALIGN;	/* Explicit align before switch to .text */
  #endif
    asm (TEXT_SECTION_ASM_OP);	/* don't put epilogue and body in .init */
    DO_GLOBAL_CTORS_BODY;
+ #if !defined (FINI_SECTION_ASM_OP)
    ON_EXIT (__do_global_dtors, 0);
+ #endif
+ }
+ 
+ #ifdef FINI_SECTION_ASM_OP
+  
+ /* Run all the global destructors on exit from the program.  */
+ 
+ asm (FINI_SECTION_ASM_OP);	/* cc1 doesn't know that we are switching! */
+ 
+ static void
+ __do_global_dtors (void) /* Prologue goes in the .fini section.  */
+ {
+ #ifdef FORCE_FINI_SECTION_ALIGN
+   FORCE_FINI_SECTION_ALIGN;
+ #endif
+   asm (TEXT_SECTION_ASM_OP);
+   /* Don't put the body and epilogue in '.init' yet.
+      They will instead be inserted into crtend.o.  */
  }
  
! #endif /* FINI_SECTION_ASM_OP */
! 
! #endif /* not OBJECT_FORMAT_ELF */
  
! #else /* not defined(INIT_SECTION_ASM_OP) */
  
  #ifdef HAS_INIT_SECTION
  /* This case is used by the Irix 6 port, which supports named sections but
     not an SVR4-style .fini section.  __do_global_dtors can be non-static
*************** __frame_dummy (void)
*** 305,313 ****
  }
  #endif
  #endif
  
! #endif /* defined(INIT_SECTION_ASM_OP) */
  
  /* Force cc1 to switch to .data section.  */
  static func_ptr force_to_data[0] __attribute__ ((__unused__)) = { };
  
--- 333,341 ----
  }
  #endif
  #endif
  
! #endif /* not defined(INIT_SECTION_ASM_OP) */
  
  /* Force cc1 to switch to .data section.  */
  static func_ptr force_to_data[0] __attribute__ ((__unused__)) = { };
  
*************** init_dummy (void)
*** 395,403 ****
    }
  #endif
  }
  
! #else  /* OBJECT_FORMAT_ELF */
  
  /* Stick the real initialization code, followed by a normal sort of
     function epilogue at the very end of the .init section for this
     entire root executable file or for this entire shared library file.
--- 423,431 ----
    }
  #endif
  }
  
! #else  /* not OBJECT_FORMAT_ELF */
  
  /* Stick the real initialization code, followed by a normal sort of
     function epilogue at the very end of the .init section for this
     entire root executable file or for this entire shared library file.
*************** init_dummy (void)
*** 418,442 ****
     include setup operations for very primitive things (e.g. initializing
     the state of the floating-point coprocessor, etc.) which should be done
     before we start to execute any of the user's code.  */
  
  static void
  __do_global_ctors_aux (void)	/* prologue goes in .text section */
  {
    asm (INIT_SECTION_ASM_OP);
    DO_GLOBAL_CTORS_BODY;
    ON_EXIT (__do_global_dtors, 0);
  }				/* epilogue and body go in .init section */
  
  #ifdef FORCE_INIT_SECTION_ALIGN
  FORCE_INIT_SECTION_ALIGN;
  #endif
  
  asm (TEXT_SECTION_ASM_OP);
  
! #endif /* OBJECT_FORMAT_ELF */
  
! #else /* defined(INIT_SECTION_ASM_OP) */
  
  #ifdef HAS_INIT_SECTION
  /* This case is used by the Irix 6 port, which supports named sections but
     not an SVR4-style .init section.  __do_global_ctors can be non-static
--- 446,530 ----
     include setup operations for very primitive things (e.g. initializing
     the state of the floating-point coprocessor, etc.) which should be done
     before we start to execute any of the user's code.  */
  
+ #ifdef FINI_SECTION_ASM_OP
+ /* Make it a separate function to avoid changing away from .fini
+    in __do_global_ctors_aux.  */
+ static void
+ __do_init_frame (void)
+ {
+   static struct object object;
+   if (__register_frame_info)
+     __register_frame_info (__EH_FRAME_BEGIN__, &object);
+ }
+ #endif /* FINI_SECTION_ASM_OP */
+ 
  static void
  __do_global_ctors_aux (void)	/* prologue goes in .text section */
  {
    asm (INIT_SECTION_ASM_OP);
+ #if defined (FINI_SECTION_ASM_OP)
+   __do_init_frame();
+ #endif
    DO_GLOBAL_CTORS_BODY;
+ #if !defined (FINI_SECTION_ASM_OP)
    ON_EXIT (__do_global_dtors, 0);
+ #endif
  }				/* epilogue and body go in .init section */
  
  #ifdef FORCE_INIT_SECTION_ALIGN
  FORCE_INIT_SECTION_ALIGN;
  #endif
  
  asm (TEXT_SECTION_ASM_OP);
  
! #ifdef FINI_SECTION_ASM_OP
!  
! /* Run all the global destructors on exit from the program.  */
  
! static void
! __do_global_dtors_aux (void)
! {
! #ifdef DO_GLOBAL_DTORS_BODY
!   DO_GLOBAL_DTORS_BODY;
! #else
!   static func_ptr *p = __DTOR_LIST__ + 1;
!   static int completed = 0;
! 
!   if (completed)
!     return;
! 
!   while (*p)
!     {
!       p++;
!       (*(p-1)) ();
!     }
! 
!   completed = 1;
! #endif
! }
! 
! static void
! __do_global_dtors (void) /* Prologue goes in the .text section.  */
! {
!   asm (FINI_SECTION_ASM_OP);	/* cc1 doesn't know that we are switching! */
!   /* Here is the body and epilogue for .fini, as promised.  */
!   __do_global_dtors_aux();
! 
! #ifdef EH_FRAME_SECTION_ASM_OP
!   if (__deregister_frame_info)
!     __deregister_frame_info (__EH_FRAME_BEGIN__);
! #endif
! }
! 
! asm (TEXT_SECTION_ASM_OP);
! 
! #endif /* FINI_SECTION_ASM_OP */
! 
! #endif /* not OBJECT_FORMAT_ELF */
! 
! #else /* not defined(INIT_SECTION_ASM_OP) */
  
  #ifdef HAS_INIT_SECTION
  /* This case is used by the Irix 6 port, which supports named sections but
     not an SVR4-style .init section.  __do_global_ctors can be non-static
*************** __do_global_ctors (void)
*** 454,462 ****
  #endif
    for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
      (*p) ();
  }
! #endif
  
  #endif /* defined(INIT_SECTION_ASM_OP) */
  
  /* Force cc1 to switch to .data section.  */
--- 542,550 ----
  #endif
    for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
      (*p) ();
  }
! #endif /* HAS_INIT_SECTION */
  
  #endif /* defined(INIT_SECTION_ASM_OP) */
  
  /* Force cc1 to switch to .data section.  */

--- 
Mark Elbrecht, snowball3@bigfoot.com
http://snowball.frogspace.net/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]