[ This has a fix for powerpc md_init.h, bug found by brad ] .init and .fini are not quite set up like some programs desire. I was under the impression that only some systems expect the .init section to be an instruction stream. However it seems this is not the case. Only on i386 have I seen code which expects this behavior, however it makes sense to fix this properly. This change paritally comes from NetBSD, however it is a much simpler implementation of that hairy mess. The code which currently uses the 'other behavior' in .init/.fini is wine. Index: alpha/Makefile =================================================================== RCS file: /cvs/src/lib/csu/alpha/Makefile,v retrieving revision 1.13 diff -u -r1.13 Makefile --- alpha/Makefile 30 May 2003 19:03:24 -0000 1.13 +++ alpha/Makefile 26 Aug 2003 18:24:41 -0000 @@ -7,7 +7,7 @@ ELFDIR= ${.CURDIR}/../common_elf .PATH: ${ELFDIR} -CFLAGS+= -I${ELFDIR} +CFLAGS+= -I${ELFDIR} -I${.CURDIR} PICFLAG?=-fpic Index: alpha/md_init.h =================================================================== RCS file: alpha/md_init.h diff -N alpha/md_init.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ alpha/md_init.h 26 Aug 2003 18:24:41 -0000 @@ -0,0 +1,62 @@ +/*- + * Copyright (c) 2001 Ross Harvey + * All rights reserved. + * + * 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. + */ + +#define MD_SECT_CALL_FUNC(section, func) \ + __asm (".section "#section", \"ax\"\n" \ + " jsr $26," #func " \n" \ + " ldgp $29,0($26) \n" \ + " .previous") + +#define MD_SECTION_PROLOGUE(sect, entry_pt) \ + __asm ( \ + ".section "#sect",\"ax\",@progbits \n" \ + " .globl " #entry_pt " \n" \ + " .type " #entry_pt ",@function \n" \ + #entry_pt": \n" \ + " ldgp $29, 0($27) \n" \ + " lda $30, -16($30) \n" \ + " stq $26, 0($30) \n" \ + " stq $15, 8($30) \n" \ + " .align 5 \n" \ + " /* fall thru */ \n" \ + " .previous") + + +#define MD_SECTION_EPILOGUE(sect) \ + __asm ( \ + ".section "#sect",\"ax\",@progbits \n" \ + " ldq $15, 8($30) \n" \ + " ldq $26, 0($30) \n" \ + " lda $30, 16($30) \n" \ + " ret \n" \ + " .previous") Index: common_elf/crtbegin.c =================================================================== RCS file: /cvs/src/lib/csu/common_elf/crtbegin.c,v retrieving revision 1.6 diff -u -r1.6 crtbegin.c --- common_elf/crtbegin.c 16 Feb 2002 21:27:20 -0000 1.6 +++ common_elf/crtbegin.c 26 Aug 2003 18:24:41 -0000 @@ -40,7 +40,7 @@ * The tables are also null-terminated. */ #include - +#include "md_init.h" #include "os-note-elf.h" static void (*__CTOR_LIST__[1])(void) @@ -76,11 +76,21 @@ (**p++)(); } -void __init(void) __attribute__((section(".init"))); -void __fini(void) __attribute__((section(".fini"))); +void __init(void); +void __fini(void); +static void __do_init(void); +static void __do_fini(void); + +MD_SECTION_PROLOGUE(".init", __init); + +MD_SECTION_PROLOGUE(".fini", __fini); + +MD_SECT_CALL_FUNC(".init", __do_init); +MD_SECT_CALL_FUNC(".fini", __do_fini); + void -__init() +__do_init() { static int initialized = 0; @@ -90,18 +100,18 @@ */ if (!initialized) { initialized = 1; - __ctors(); - } + (__ctors)(); - atexit(__fini); + atexit(__fini); + } } void -__fini() +__do_fini() { /* * Call global destructors. */ - __dtors(); + (__dtors)(); } Index: common_elf/crtbeginS.c =================================================================== RCS file: /cvs/src/lib/csu/common_elf/crtbeginS.c,v retrieving revision 1.3 diff -u -r1.3 crtbeginS.c --- common_elf/crtbeginS.c 26 Jun 2003 23:19:18 -0000 1.3 +++ common_elf/crtbeginS.c 26 Aug 2003 18:24:41 -0000 @@ -41,6 +41,7 @@ */ #include +#include "md_init.h" static void (*__CTOR_LIST__[0])(void) __attribute__((section(".ctors"))) = { (void *)-1 }; /* XXX */ @@ -76,9 +77,20 @@ (**p++)(); } } +void _init(void); +void _fini(void); +static void _do_init(void); +static void _do_fini(void); + +MD_SECTION_PROLOGUE(".init", _init); + +MD_SECTION_PROLOGUE(".fini", _fini); + +MD_SECT_CALL_FUNC(".init", _do_init); +MD_SECT_CALL_FUNC(".fini", _do_fini); void -_init(void) +_do_init(void) { static int initialized = 0; @@ -93,7 +105,7 @@ } void -_fini(void) +_do_fini(void) { /* * since the _init() function sets up the destructors to be called Index: common_elf/crtend.c =================================================================== RCS file: /cvs/src/lib/csu/common_elf/crtend.c,v retrieving revision 1.2 diff -u -r1.2 crtend.c --- common_elf/crtend.c 16 Feb 2002 21:27:20 -0000 1.2 +++ common_elf/crtend.c 26 Aug 2003 18:24:41 -0000 @@ -1,13 +1,13 @@ /* $OpenBSD: crtend.c,v 1.2 2002/02/16 21:27:20 millert Exp $ */ /* $NetBSD: crtend.c,v 1.1 1996/09/12 16:59:04 cgd Exp $ */ -#ifndef ECOFF_COMPAT - #include +#include "md_init.h" static void (*__CTOR_LIST__[1])(void) __attribute__((section(".ctors"))) = { (void *)0 }; /* XXX */ static void (*__DTOR_LIST__[1])(void) __attribute__((section(".dtors"))) = { (void *)0 }; /* XXX */ -#endif /* !ECOFF_COMPAT */ +MD_SECTION_EPILOGUE(".init"); +MD_SECTION_EPILOGUE(".fini"); Index: common_elf/crtendS.c =================================================================== RCS file: /cvs/src/lib/csu/common_elf/crtendS.c,v retrieving revision 1.2 diff -u -r1.2 crtendS.c --- common_elf/crtendS.c 16 Feb 2002 21:27:20 -0000 1.2 +++ common_elf/crtendS.c 26 Aug 2003 18:24:41 -0000 @@ -2,8 +2,12 @@ /* $NetBSD: crtend.c,v 1.1 1997/04/16 19:38:24 thorpej Exp $ */ #include +#include "md_init.h" static void (*__CTOR_LIST__[1])(void) __attribute__((section(".ctors"))) = { (void *)0 }; /* XXX */ static void (*__DTOR_LIST__[1])(void) __attribute__((section(".dtors"))) = { (void *)0 }; /* XXX */ + +MD_SECTION_EPILOGUE(".init"); +MD_SECTION_EPILOGUE(".fini"); Index: hppa/Makefile =================================================================== RCS file: /cvs/src/lib/csu/hppa/Makefile,v retrieving revision 1.3 diff -u -r1.3 Makefile --- hppa/Makefile 30 May 2003 19:03:24 -0000 1.3 +++ hppa/Makefile 26 Aug 2003 18:24:41 -0000 @@ -7,7 +7,7 @@ ELFDIR= ${.CURDIR}/../common_elf .PATH: ${ELFDIR} -CFLAGS+= -I${ELFDIR} +CFLAGS+= -I${ELFDIR} -I${.CURDIR} #PICFLAG?=-fPIC Index: i386/Makefile =================================================================== RCS file: /cvs/src/lib/csu/i386/Makefile,v retrieving revision 1.8 diff -u -r1.8 Makefile --- i386/Makefile 30 May 2003 19:03:24 -0000 1.8 +++ i386/Makefile 26 Aug 2003 18:24:41 -0000 @@ -7,7 +7,7 @@ ELFDIR= ${.CURDIR}/../common_elf .PATH: ${ELFDIR} -CFLAGS+= -I${ELFDIR} +CFLAGS+= -I${ELFDIR} -I${.CURDIR} PICFLAG?=-fpic Index: i386/md_init.h =================================================================== RCS file: i386/md_init.h diff -N i386/md_init.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ i386/md_init.h 26 Aug 2003 18:24:41 -0000 @@ -0,0 +1,59 @@ +/*- + * Copyright (c) 2001 Ross Harvey + * All rights reserved. + * + * 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. + */ + +#define MD_SECT_CALL_FUNC(section, func) \ + __asm (".section "#section", \"ax\"\n" \ + " call " #func "\n" \ + " .previous") + +/* + * Align is after because we want the function to start at the first + * address of the section, but overall we want the section to be + * aligned by the align amount. + */ +#define MD_SECTION_PROLOGUE(sect, entry_pt) \ + __asm ( \ + ".section "#sect",\"ax\",@progbits \n" \ + " .globl " #entry_pt " \n" \ + " .type " #entry_pt ",@function \n" \ + #entry_pt": \n" \ + " .align 16 \n" \ + " /* fall thru */ \n" \ + " .previous") + + +#define MD_SECTION_EPILOGUE(sect) \ + __asm ( \ + ".section "#sect",\"ax\",@progbits \n" \ + " ret \n" \ + " .previous") Index: powerpc/Makefile =================================================================== RCS file: /cvs/src/lib/csu/powerpc/Makefile,v retrieving revision 1.11 diff -u -r1.11 Makefile --- powerpc/Makefile 30 May 2003 19:03:24 -0000 1.11 +++ powerpc/Makefile 26 Aug 2003 18:24:41 -0000 @@ -7,7 +7,7 @@ ELFDIR= ${.CURDIR}/../common_elf .PATH: ${ELFDIR} -CFLAGS+= -I${ELFDIR} +CFLAGS+= -I${ELFDIR} -I${.CURDIR} PICFLAG?=-fpic Index: powerpc/md_init.h =================================================================== RCS file: powerpc/md_init.h diff -N powerpc/md_init.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ powerpc/md_init.h 26 Aug 2003 18:24:41 -0000 @@ -0,0 +1,60 @@ +/*- + * Copyright (c) 2001 Ross Harvey + * All rights reserved. + * + * 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. + */ + +#define MD_SECT_CALL_FUNC(section, func) \ + __asm (".section "#section", \"ax\"\n" \ + " bl " #func "\n" \ + " .previous") + +#define MD_SECTION_PROLOGUE(sect, entry_pt) \ + __asm ( \ + ".section "#sect",\"ax\",@progbits \n" \ + " .globl " #entry_pt " \n" \ + " .type " #entry_pt ",@function \n" \ + " .align 4 \n" \ + #entry_pt": \n" \ + " stwu %r1,-16(%r1) \n" \ + " mflr %r0 \n" \ + " stw %r0,12(%r1) \n" \ + " /* fall thru */ \n" \ + " .previous") + + +#define MD_SECTION_EPILOGUE(sect) \ + __asm ( \ + ".section "#sect",\"ax\",@progbits \n" \ + " lwz %r0,12(%r1) \n" \ + " mtlr %r0 \n" \ + " addi %r1,%r1,16 \n" \ + " blr \n" \ + " .previous") Index: sparc/Makefile =================================================================== RCS file: /cvs/src/lib/csu/sparc/Makefile,v retrieving revision 1.7 diff -u -r1.7 Makefile --- sparc/Makefile 30 May 2003 19:03:24 -0000 1.7 +++ sparc/Makefile 26 Aug 2003 18:24:41 -0000 @@ -7,7 +7,7 @@ ELFDIR= ${.CURDIR}/../common_elf .PATH: ${ELFDIR} -CFLAGS+= -I${ELFDIR} +CFLAGS+= -I${ELFDIR} -I${.CURDIR} PICFLAG?=-fPIC Index: sparc/md_init.h =================================================================== RCS file: sparc/md_init.h diff -N sparc/md_init.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ sparc/md_init.h 26 Aug 2003 18:24:41 -0000 @@ -0,0 +1,57 @@ +/*- + * Copyright (c) 2001 Ross Harvey + * All rights reserved. + * + * 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. + */ + +#define MD_SECT_CALL_FUNC(section, func) \ + __asm (".section "#section", \"ax\" \n" \ + " call " #func ", 0 \n" \ + " nop \n" \ + " .previous") + +#define MD_SECTION_PROLOGUE(sect, entry_pt) \ + __asm ( \ + ".section "#sect",\"ax\",@progbits \n" \ + " .globl " #entry_pt " \n" \ + " .type " #entry_pt ",@function \n" \ + #entry_pt": \n" \ + " save %sp, -96, %sp \n" \ + " .align 4 \n" \ + " /* fall thru */ \n" \ + " .previous") + + +#define MD_SECTION_EPILOGUE(sect) \ + __asm ( \ + ".section "#sect",\"ax\",@progbits \n" \ + " ret \n" \ + " restore \n" \ + " .previous") Index: sparc64/Makefile =================================================================== RCS file: /cvs/src/lib/csu/sparc64/Makefile,v retrieving revision 1.2 diff -u -r1.2 Makefile --- sparc64/Makefile 30 May 2003 19:03:24 -0000 1.2 +++ sparc64/Makefile 26 Aug 2003 18:24:41 -0000 @@ -7,7 +7,7 @@ ELFDIR= ${.CURDIR}/../common_elf .PATH: ${ELFDIR} -CFLAGS+= -I${ELFDIR} +CFLAGS+= -I${ELFDIR} -I${.CURDIR} PICFLAG?=-fpic Index: sparc64/md_init.h =================================================================== RCS file: sparc64/md_init.h diff -N sparc64/md_init.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ sparc64/md_init.h 26 Aug 2003 18:24:41 -0000 @@ -0,0 +1,57 @@ +/*- + * Copyright (c) 2001 Ross Harvey + * All rights reserved. + * + * 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. + */ + +#define MD_SECT_CALL_FUNC(section, func) \ + __asm (".section "#section", \"ax\" \n" \ + " call " #func ", 0 \n" \ + " nop \n" \ + " .previous") + +#define MD_SECTION_PROLOGUE(sect, entry_pt) \ + __asm ( \ + ".section "#sect",\"ax\",@progbits \n" \ + " .globl " #entry_pt " \n" \ + " .type " #entry_pt ",@function \n" \ + #entry_pt": \n" \ + " save %sp, -192, %sp \n" \ + " .align 4 \n" \ + " /* fall thru */ \n" \ + " .previous") + + +#define MD_SECTION_EPILOGUE(sect) \ + __asm ( \ + ".section "#sect",\"ax\",@progbits \n" \ + " ret \n" \ + " restore \n" \ + " .previous")