Subject: Re: Split crtbegin/crtend into more pieces
To: None <tech-userlevel@netbsd.org>
From: Jason R Thorpe <thorpej@wasabisystems.com>
List: tech-toolchain
Date: 11/21/2002 21:56:15
--rwEMma7ioTxnRzrJ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Thu, Nov 21, 2002 at 06:35:55PM -0800, Jason R Thorpe wrote:

 > These changes are mostly mechanical -- code which was already present
 > in the various dot_init.h files was copied into crti.S and crtn.S for
 > each architecture.  Most of the dot_init.h files have been removed,

Actually, I ended up doing something much simpler, that required
a lot less code to change..

Attached is the final patch that I will be checking in.

-- 
        -- Jason R. Thorpe <thorpej@wasabisystems.com>

--rwEMma7ioTxnRzrJ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=crtstuff-patch

Index: alpha/dot_init.h
===================================================================
RCS file: /cvsroot/basesrc/lib/csu/alpha/dot_init.h,v
retrieving revision 1.1
diff -c -r1.1 dot_init.h
*** alpha/dot_init.h	2001/05/11 22:44:14	1.1
--- alpha/dot_init.h	2002/11/22 05:45:11
***************
*** 77,79 ****
--- 77,88 ----
  
  #define	MD_INIT_SECTION_EPILOGUE MD_SECTION_EPILOGUE(.init)
  #define	MD_FINI_SECTION_EPILOGUE MD_SECTION_EPILOGUE(.fini)
+ 
+ /* We assume we need to reload our GP. */
+ #define MD_CALL_STATIC_FUNCTION(section, func) \
+ asm(".section " #section "\n"		\
+ "    br $29, 1f		\n"		\
+ "1:  ldgp $29, 1f	\n"		\
+ "    unop		\n"		\
+ "    jsr $26, " #func "\n"		\
+ "    .align 3; .previous");
Index: common_elf/Makefile.inc
===================================================================
RCS file: /cvsroot/basesrc/lib/csu/common_elf/Makefile.inc,v
retrieving revision 1.18
diff -c -r1.18 Makefile.inc
*** common_elf/Makefile.inc	2002/08/19 09:41:27	1.18
--- common_elf/Makefile.inc	2002/11/22 05:45:11
***************
*** 18,24 ****
  
  .PATH:		${.CURDIR}/../common_elf
  
! OBJS+=		crt0.o gcrt0.o crtbegin.o crtend.o
  .if ${MKPIC} != "no"
  OBJS+=		crtbeginS.o crtendS.o
  COPTS+=		-fPIC
--- 18,24 ----
  
  .PATH:		${.CURDIR}/../common_elf
  
! OBJS+=		crt0.o gcrt0.o crtbegin.o crtend.o crti.o crtn.o
  .if ${MKPIC} != "no"
  OBJS+=		crtbeginS.o crtendS.o
  COPTS+=		-fPIC
***************
*** 59,64 ****
--- 59,76 ----
  crtendS.o: crtend.c
  	@echo "${COMPILE.c} -DSHARED ${.ALLSRC} -o ${.TARGET}"
  	@${COMPILE.c} -DSHARED ${.ALLSRC} -o ${.TARGET}.o
+ 	@${LD} -X -r -o ${.TARGET} ${.TARGET}.o
+ 	@rm -f ${.TARGET}.o
+ 
+ crti.o: crti.c
+ 	@echo "${COMPILE.c} ${.ALLSRC} -o ${.TARGET}"
+ 	@${COMPILE.c} ${.ALLSRC} -o ${.TARGET}.o
+ 	@${LD} -X -r -o ${.TARGET} ${.TARGET}.o
+ 	@rm -f ${.TARGET}.o
+ 
+ crtn.o: crtn.c
+ 	@echo "${COMPILE.c} ${.ALLSRC} -o ${.TARGET}"
+ 	@${COMPILE.c} ${.ALLSRC} -o ${.TARGET}.o
  	@${LD} -X -r -o ${.TARGET} ${.TARGET}.o
  	@rm -f ${.TARGET}.o
  
Index: common_elf/crtbegin.c
===================================================================
RCS file: /cvsroot/basesrc/lib/csu/common_elf/crtbegin.c,v
retrieving revision 1.19
diff -c -r1.19 crtbegin.c
*** common_elf/crtbegin.c	2002/11/11 00:44:43	1.19
--- common_elf/crtbegin.c	2002/11/22 05:45:11
***************
*** 1,7 ****
  /*	$NetBSD: crtbegin.c,v 1.19 2002/11/11 00:44:43 thorpej Exp $	*/
  
  /*-
!  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
   * All rights reserved.
   *
   * This code is derived from software contributed to The NetBSD Foundation
--- 1,7 ----
  /*	$NetBSD: crtbegin.c,v 1.19 2002/11/11 00:44:43 thorpej Exp $	*/
  
  /*-
!  * Copyright (c) 1998, 2001, 2002 The NetBSD Foundation, Inc.
   * All rights reserved.
   *
   * This code is derived from software contributed to The NetBSD Foundation
***************
*** 94,112 ****
  #endif
  #endif
  
! static void __dtors(void);
! static void __ctors(void);
! 
! INIT_FALLTHRU_DECL;
! FINI_FALLTHRU_DECL;
! 
! extern void _init(void)   __attribute__((section(".init")));
! extern void _fini(void)   __attribute__((section(".fini")));
! static void __ctors(void) __attribute__((section(".init")));
! static void __dtors(void) __attribute__((section(".fini")));
  
  static void
! __ctors()
  {
  	unsigned long i = (unsigned long) __CTOR_LIST__[0];
  	void (**p)(void);
--- 94,116 ----
  #endif
  #endif
  
! #ifndef MD_CALL_STATIC_FUNCTION
! #if defined(__GNUC__)
! #define	MD_CALL_STATIC_FUNCTION(section, func)				\
! static void __attribute__((__unused__))					\
! __call_##func(void)							\
! {									\
! 	__asm __volatile (".section " #section);			\
! 	func();								\
! 	__asm __volatile (".previous");					\
! }
! #else
! #error Need MD_CALL_STATIC_FUNCTION
! #endif
! #endif /* ! MD_CALL_STATIC_FUNCTION */
  
  static void
! __ctors(void)
  {
  	unsigned long i = (unsigned long) __CTOR_LIST__[0];
  	void (**p)(void);
***************
*** 122,128 ****
  }
  
  static void
! __dtors()
  {
  	void (**p)(void) = __DTOR_LIST__ + 1;
  
--- 126,132 ----
  }
  
  static void
! __dtors(void)
  {
  	void (**p)(void) = __DTOR_LIST__ + 1;
  
***************
*** 130,149 ****
  		(**p++)();
  }
  
! void
! _init()
  {
! 	static int initialized = 0;
  #ifdef DWARF2_EH
  #if defined(__GNUC__)
  	static struct dwarf2_eh_object object;
  #endif /* __GNUC__ */
  #endif /* DWARF2_EH */
  
- 	/*
- 	 * Execute code in the .init sections
- 	 */
- 	INIT_FALLTHRU();
  	if (!initialized) {
  		initialized = 1;
  
--- 134,149 ----
  		(**p++)();
  }
  
! static void __attribute__((__unused__))
! __do_global_ctors_aux(void)
  {
! 	static int initialized;
  #ifdef DWARF2_EH
  #if defined(__GNUC__)
  	static struct dwarf2_eh_object object;
  #endif /* __GNUC__ */
  #endif /* DWARF2_EH */
  
  	if (!initialized) {
  		initialized = 1;
  
***************
*** 165,174 ****
  		__ctors();
  	}
  }
  
! void
! _fini()
  {
  
  #if defined(DSO_HANDLE) && defined(__GNUC__) && defined(SHARED)
  	/*
--- 165,179 ----
  		__ctors();
  	}
  }
+ MD_CALL_STATIC_FUNCTION(.init, __do_global_ctors_aux)
  
! static void __attribute__((__unused__))
! __do_global_dtors_aux(void)
  {
+ 	static int finished;
+ 
+ 	if (finished)
+ 		return;
  
  #if defined(DSO_HANDLE) && defined(__GNUC__) && defined(SHARED)
  	/*
***************
*** 190,201 ****
  #endif /* __GNUC__ */
  #endif /* DWARF2_EH */
  
! 	/*
! 	 * Execute code in the .fini sections
! 	 */
! 	FINI_FALLTHRU();
  }
! 
! MD_INIT_SECTION_PROLOGUE;
! 
! MD_FINI_SECTION_PROLOGUE;
--- 195,200 ----
  #endif /* __GNUC__ */
  #endif /* DWARF2_EH */
  
! 	finished = 1;
  }
! MD_CALL_STATIC_FUNCTION(.fini, __do_global_dtors_aux)
Index: common_elf/crtend.c
===================================================================
RCS file: /cvsroot/basesrc/lib/csu/common_elf/crtend.c,v
retrieving revision 1.9
diff -c -r1.9 crtend.c
*** common_elf/crtend.c	2001/12/30 23:45:01	1.9
--- common_elf/crtend.c	2002/11/22 05:45:11
***************
*** 20,26 ****
  static void *__JCR_END__[1]
      __attribute__((__unused__, section(".jcr"))) = { (void *) 0 };
  #endif
- 
- MD_INIT_SECTION_EPILOGUE;
- 
- MD_FINI_SECTION_EPILOGUE;
--- 20,22 ----
Index: common_elf/crti.c
===================================================================
RCS file: crti.c
diff -N crti.c
*** /dev/null	Fri Nov 22 07:44:52 2002
--- crti.c	Fri Nov 22 07:45:11 2002
***************
*** 0 ****
--- 1,67 ----
+ /*	$NetBSD$	*/
+ 
+ /*- 
+  * Copyright (c) 1998, 2001, 2002 The NetBSD Foundation, Inc.
+  * All rights reserved.
+  *
+  * This code is derived from software contributed to The NetBSD Foundation
+  * by Paul Kranenburg, Ross Harvey, and Jason R. Thorpe.
+  *
+  * Redistribution and use in source and binary forms, with or without
+  * modification, are permitted provided that the following conditions
+  * are met:     
+  * 1. Redistributions of source code must retain the above copyright
+  *    notice, this list of conditions and the following disclaimer.
+  * 2. Redistributions in binary form must reproduce the above copyright
+  *    notice, this list of conditions and the following disclaimer in the
+  *    documentation and/or other materials provided with the distribution.
+  * 3. All advertising materials mentioning features or use of this software
+  *    must display the following acknowledgement:
+  *	This product includes software developed by the NetBSD
+  *	Foundation, Inc. and its contributors.
+  * 4. Neither the name of The NetBSD Foundation nor the names of its
+  *    contributors may be used to endorse or promote products derived
+  *    from this software without specific prior written permission.
+  *
+  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+  * POSSIBILITY OF SUCH DAMAGE.
+  */
+ 
+ #include <sys/param.h>		/* sysident.h requires `NetBSD' constant */
+ #include <sys/exec.h> 
+ #include <sys/exec_elf.h>
+ 
+ #include "sysident.h"
+ #include "dot_init.h"
+ 
+ INIT_FALLTHRU_DECL;
+ FINI_FALLTHRU_DECL;
+ 
+ void	_init(void);
+ void	_fini(void);
+ 
+ void
+ _init(void)
+ {
+ 
+ 	INIT_FALLTHRU();
+ }
+ 
+ void
+ _fini(void)
+ {
+ 
+ 	FINI_FALLTHRU();
+ }
+ 
+ MD_INIT_SECTION_PROLOGUE;
+ MD_FINI_SECTION_PROLOGUE;
Index: common_elf/crtn.c
===================================================================
RCS file: crtn.c
diff -N crtn.c
*** /dev/null	Fri Nov 22 07:44:52 2002
--- crtn.c	Fri Nov 22 07:45:12 2002
***************
*** 0 ****
--- 1,42 ----
+ /*	$NetBSD$	*/
+ 
+ /*- 
+  * Copyright (c) 1998, 2001, 2002 The NetBSD Foundation, Inc.
+  * All rights reserved.
+  *
+  * This code is derived from software contributed to The NetBSD Foundation
+  * by Paul Kranenburg, Ross Harvey, and Jason R. Thorpe.
+  *
+  * Redistribution and use in source and binary forms, with or without
+  * modification, are permitted provided that the following conditions
+  * are met:     
+  * 1. Redistributions of source code must retain the above copyright
+  *    notice, this list of conditions and the following disclaimer.
+  * 2. Redistributions in binary form must reproduce the above copyright
+  *    notice, this list of conditions and the following disclaimer in the
+  *    documentation and/or other materials provided with the distribution.
+  * 3. All advertising materials mentioning features or use of this software
+  *    must display the following acknowledgement:
+  *	This product includes software developed by the NetBSD
+  *	Foundation, Inc. and its contributors.
+  * 4. Neither the name of The NetBSD Foundation nor the names of its
+  *    contributors may be used to endorse or promote products derived
+  *    from this software without specific prior written permission.
+  *
+  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+  * POSSIBILITY OF SUCH DAMAGE.
+  */
+ 
+ #include "dot_init.h"
+ 
+ MD_INIT_SECTION_EPILOGUE;
+ MD_FINI_SECTION_EPILOGUE;
Index: i386_elf/dot_init.h
===================================================================
RCS file: /cvsroot/basesrc/lib/csu/i386_elf/dot_init.h,v
retrieving revision 1.2
diff -c -r1.2 dot_init.h
*** i386_elf/dot_init.h	2002/01/14 12:06:49	1.2
--- i386_elf/dot_init.h	2002/11/22 05:45:12
***************
*** 67,69 ****
--- 67,72 ----
  
  #define	MD_INIT_SECTION_EPILOGUE MD_SECTION_EPILOGUE(.init)
  #define	MD_FINI_SECTION_EPILOGUE MD_SECTION_EPILOGUE(.fini)
+ 
+ #define MD_CALL_STATIC_FUNCTION(section, func) \
+ asm(".section " #section "; call " #func "; .previous");
Index: sh3_elf/dot_init.h
===================================================================
RCS file: /cvsroot/basesrc/lib/csu/sh3_elf/dot_init.h,v
retrieving revision 1.1
diff -c -r1.1 dot_init.h
*** sh3_elf/dot_init.h	2001/07/23 20:43:31	1.1
--- sh3_elf/dot_init.h	2002/11/22 05:45:12
***************
*** 73,75 ****
--- 73,89 ----
  
  #define MD_INIT_SECTION_EPILOGUE MD_SECTION_EPILOGUE(.init)
  #define MD_FINI_SECTION_EPILOGUE MD_SECTION_EPILOGUE(.fini)
+ 
+ /*
+  * We need to put the function pointer in our own constant
+  * pool (otherwise it might be too far away to reference).
+  */
+ #define MD_CALL_STATIC_FUNCTION(section, func) \
+ asm(".section " #section "\n"		\
+ "    mov.l 1f, r1	\n"		\
+ "    mova 2f, r0	\n"		\
+ "    braf r1		\n"		\
+ "     lds r0, pr	\n"		\
+ "0:  .p2align 2		\n"		\
+ "1:  .long " #func " - 0b \n"		\
+ "2:  .previous");
Index: x86_64/dot_init.h
===================================================================
RCS file: /cvsroot/basesrc/lib/csu/x86_64/dot_init.h,v
retrieving revision 1.1
diff -c -r1.1 dot_init.h
*** x86_64/dot_init.h	2002/06/06 19:18:22	1.1
--- x86_64/dot_init.h	2002/11/22 05:45:12
***************
*** 67,69 ****
--- 67,72 ----
  
  #define	MD_INIT_SECTION_EPILOGUE MD_SECTION_EPILOGUE(.init)
  #define	MD_FINI_SECTION_EPILOGUE MD_SECTION_EPILOGUE(.fini)
+ 
+ #define MD_CALL_STATIC_FUNCTION(section, func) \
+ asm(".section " #section "; call " #func "; .previous");

--rwEMma7ioTxnRzrJ--