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]
Other format: [Raw text]

Shared-library constructor and destructor routines on NetBSD



I first proposed this patch about a year ago, but for various reasons 
never got around to committing it.

This patch adds support for generating the appropriate shared-library 
entry points for constructors and destructors on non-ELF NetBSD systems.  
On these systems the constructor and destructor functions in a shared 
library are called .init and .fini respectively.

The patch also fixes LINK_SPEC so that gcc -shared will pass the correct 
arguments to ld (we don't want to pull "start" into a shared library.

Finally, on arm/NetBSD, this adds support for building libgcc as a shared 
library.

All bootstrapped on arm-netbsd (obviously:-)

2001-12-13  Richard Earnshaw  <rearnsha@arm.com>

	* collect2.c (COLLECT_SHARED_INIT_FUNC)
	(COLLECT_SHARD_FINI_FUNC): Define default values.
	(write_c_file_stat): Use them to call construtor and destructor 
	functions in shared libraries.
	* doc/tm.texi (COLLECT_SHARED_INIT_FUNC)
	(COLLECT_SHARD_FINI_FUNC): Document them.
	* config/netbsd.h (COLLECT_SHARED_INIT_FUNC)
	(COLLECT_SHARD_FINI_FUNC): Define.

	* config/netbsd.h (LINK_SPEC): Don't pull in estart if creating a
	shared library.  Pass -shared through to the linker.
	* config/arm/netbsd.h (LINK_SPEC): Likewise.

	* config/arm/t-netbsd (TARGET_LIBGCC2_CFLAGS): Add -fpic.
	(SHLIB_EXT, SHLIB_NAME, SHLIB_SONAME, SHLIB_OBJS, SHLIB_LINK)
	(SHLIB_INSTALL): Define.


Index: collect2.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/collect2.c,v
retrieving revision 1.129
diff -p -r1.129 collect2.c
*** collect2.c	2001/12/02 00:04:16	1.129
--- collect2.c	2001/12/13 14:17:45
*************** Software Foundation, 59 Temple Place - S
*** 154,159 ****
--- 154,168 ----
  /* This must match tree.h.  */
  #define DEFAULT_INIT_PRIORITY 65535
  
+ #ifndef COLLECT_SHARED_INIT_FUNC
+ #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
+   fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
+ #endif
+ #ifndef COLLECT_SHARED_FINI_FUNC
+ #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
+   fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
+ #endif
+ 
  #if defined (LDD_SUFFIX) || SUNOS4_SHARED_LIBRARIES
  #define SCAN_LIBRARIES
  #endif
*************** write_c_file_stat (stream, name)
*** 1944,1951 ****
  
    if (shared_obj)
      {
!       fprintf (stream, "void _GLOBAL__DI() {\n\t%s();\n}\n", initname);
!       fprintf (stream, "void _GLOBAL__DD() {\n\t%s();\n}\n", fininame);
      }
  }
  
--- 1953,1960 ----
  
    if (shared_obj)
      {
!       COLLECT_SHARED_INIT_FUNC(stream, initname);
!       COLLECT_SHARED_FINI_FUNC(stream, fininame);
      }
  }
  
Index: config/netbsd.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/config/netbsd.h,v
retrieving revision 1.9
diff -p -r1.9 netbsd.h
*** netbsd.h	2001/11/14 21:44:19	1.9
--- netbsd.h	2001/12/13 14:17:47
***************
*** 54,64 ****
  #define LIB_SPEC "%{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
  
  /* Provide a LINK_SPEC appropriate for NetBSD.  Here we provide support
!    for the special GCC options -static, -assert, and -nostdlib.  */
  
  #undef LINK_SPEC
  #define LINK_SPEC \
!   "%{!nostdlib:%{!r*:%{!e*:-e start}}} -dc -dp %{R*} %{static:-Bstatic} %{assert*}"
  
  /* This defines which switch letters take arguments. */
  #undef SWITCH_TAKES_ARG
--- 54,79 ----
  #define LIB_SPEC "%{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
  
  /* Provide a LINK_SPEC appropriate for NetBSD.  Here we provide support
!    for the special GCC options -shared, -static, -assert, and -nostdlib.  */
  
  #undef LINK_SPEC
  #define LINK_SPEC \
!   "%{!shared:%{!nostdlib:%{!r*:%{!e*:-e start}}} -dc -dp %{R*} %{static:-Bstatic}} %{shared} %{assert*}"
! 
! /* When building shared libraries, the initialization and finalization 
!    functions for the library are .init and .fini respectively.  */
! 
! #define COLLECT_SHARED_INIT_FUNC(STREAM,FUNC)				\
!   do {									\
!     fprintf ((STREAM), "void __init() __asm__ (\".init\");");		\
!     fprintf ((STREAM), "void __init() {\n\t%s();\n}\n", (FUNC));	\
!   } while (0);
! 
! #define COLLECT_SHARED_FINI_FUNC(STREAM,FUNC)				\
!   do {									\
!     fprintf ((STREAM), "void __fini() __asm__ (\".fini\");");		\
!     fprintf ((STREAM), "void __fini() {\n\t%s();\n}\n", (FUNC));	\
!   } while (0);
  
  /* This defines which switch letters take arguments. */
  #undef SWITCH_TAKES_ARG
Index: config/arm/netbsd.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/config/arm/netbsd.h,v
retrieving revision 1.14
diff -p -r1.14 netbsd.h
*** netbsd.h	2001/12/13 00:27:30	1.14
--- netbsd.h	2001/12/13 14:17:47
*************** Boston, MA 02111-1307, USA.  */
*** 67,74 ****
  /* Pass -X to the linker so that it will strip symbols starting with 'L' */
  #undef LINK_SPEC
  #define LINK_SPEC "\
! -X %{!nostdlib:%{!r*:%{!e*:-e start}}} -dc -dp %{R*} \
! %{static:-Bstatic} %{assert*} \
  "
  
  #undef SIZE_TYPE
--- 67,74 ----
  /* Pass -X to the linker so that it will strip symbols starting with 'L' */
  #undef LINK_SPEC
  #define LINK_SPEC "\
! -X %{!shared:%{!nostdlib:%{!r*:%{!e*:-e start}}} -dc -dp %{R*} \
! %{static:-Bstatic}} %{shared} %{assert*} \
  "
  
  #undef SIZE_TYPE
Index: config/arm/t-netbsd
===================================================================
RCS file: /cvs/gcc/gcc/gcc/config/arm/t-netbsd,v
retrieving revision 1.4
diff -p -r1.4 t-netbsd
*** t-netbsd	2000/04/08 14:29:52	1.4
--- t-netbsd	2001/12/13 14:17:47
***************
*** 1,7 ****
  # Just for these, we omit the frame pointer since it makes such a big
  # difference.  It is then pointless adding debugging.
! TARGET_LIBGCC2_CFLAGS = -fomit-frame-pointer
  LIBGCC2_DEBUG_CFLAGS = -g0
  
  # Don't build enquire
  ENQUIRE=
--- 1,24 ----
  # Just for these, we omit the frame pointer since it makes such a big
  # difference.  It is then pointless adding debugging.
! TARGET_LIBGCC2_CFLAGS = -fomit-frame-pointer -fpic
  LIBGCC2_DEBUG_CFLAGS = -g0
+ 
+ # Build a shared libgcc library.
+ SHLIB_EXT = .so
+ SHLIB_NAME = @shlib_base_name@.so
+ SHLIB_SONAME = @shlib_base_name@.so.1
+ SHLIB_OBJS = @shlib_objs@
+ 
+ SHLIB_LINK = $(GCC_FOR_TARGET) $(LIBGCC2_CFLAGS) -shared -nodefaultlibs \
+ 	-Wl,-soname,$(SHLIB_SONAME) \
+ 	-o $(SHLIB_NAME) @multilib_flags@ $(SHLIB_OBJS) -lc && \
+ 	rm -f $(SHLIB_SONAME) && \
+ 	$(LN_S) $(SHLIB_NAME) $(SHLIB_SONAME)
+ # $(slibdir) double quoted to protect it from expansion while building
+ # libgcc.mk.  We want this delayed until actual install time.
+ SHLIB_INSTALL = $(INSTALL_DATA) $(SHLIB_NAME) $$(slibdir)/$(SHLIB_SONAME); \
+ 	rm -f $$(slibdir)/$(SHLIB_NAME); \
+ 	$(LN_S) $(SHLIB_SONAME) $$(slibdir)/$(SHLIB_NAME)
  
  # Don't build enquire
  ENQUIRE=
Index: doc/tm.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/tm.texi,v
retrieving revision 1.78
diff -p -r1.78 tm.texi
*** tm.texi	2001/12/11 23:04:36	1.78
--- tm.texi	2001/12/13 14:17:51
*************** the following symbol is an initializatio
*** 6638,6643 ****
--- 6638,6661 ----
  If defined, a C string constant for a switch that tells the linker that
  the following symbol is a finalization routine.
  
+ @item COLLECT_SHARED_INIT_FUNC (@var{stream}, @var{func})
+ If defined, a C statement that will write a function that can be
+ automatically called when a shared library is loaded.  The function
+ should call @var{func}, which takes no arguments.  If not defined, and
+ the object format requires an explicit initialization function, then a
+ function called @var{_GLOBAL__DI} will be generated.
+ 
+ This function and the following one are used by collect2 when linking a
+ shared library that needs constructors or destructors, or has DWARF2 
+ exception tables embedded in the code.
+ 
+ @item COLLECT_SHARED_FINI_FUNC (@var{stream}, @var{func})
+ If defined, a C statement that will write a function that can be
+ automatically called when a shared library is unloaded.  The function
+ should call @var{func}, which takes no arguments.  If not defined, and
+ the object format requires an explicit finalization function, then a
+ function called @var{_GLOBAL__DD} will be generated.
+ 
  @item INVOKE__main
  @findex INVOKE__main
  If defined, @code{main} will call @code{__main} despite the presence of

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