diff options
author | Dale Rahn <drahn@cvs.openbsd.org> | 2001-05-28 21:38:15 +0000 |
---|---|---|
committer | Dale Rahn <drahn@cvs.openbsd.org> | 2001-05-28 21:38:15 +0000 |
commit | 0f4ee19637361b907ef00b9b8e60ac559bb95989 (patch) | |
tree | 2cb117b60d3ded48c4ddf4123c480da421af771b | |
parent | ef3eb9521d508777e6f29b4faad924d844922b58 (diff) |
Commonize csu code for elf systems, powerpc now no longer has it's own
versions of these files.
Fixed a bug in ld.so in this, instead of scheduling the fini of each of
the shared libraries with atexit. schedule a function of ld.so itself
and it will walk all of the open libraries when the program exits.
otherwise a shared library could be dl_open()ed and then dl_close()d
and then it would not be mapped for the atexit processing.
TODO:
What if atexit is not found (process did not link against libc?)
Do shared libraries that are dl_closed have their global destructors run?
-rw-r--r-- | lib/csu/common_elf/crtbeginS.c (renamed from lib/csu/powerpc/crtbeginS.c) | 10 | ||||
-rw-r--r-- | lib/csu/common_elf/crtendS.c (renamed from lib/csu/powerpc/crtend.c) | 2 | ||||
-rw-r--r-- | lib/csu/powerpc/Makefile | 1 | ||||
-rw-r--r-- | lib/csu/powerpc/crtbegin.c | 110 | ||||
-rw-r--r-- | lib/csu/powerpc/crtendS.c | 9 | ||||
-rw-r--r-- | libexec/ld.so/loader.c | 41 | ||||
-rw-r--r-- | libexec/ld.so/test/Makefile | 10 |
7 files changed, 53 insertions, 130 deletions
diff --git a/lib/csu/powerpc/crtbeginS.c b/lib/csu/common_elf/crtbeginS.c index bf2ce4498fd..6945484f55c 100644 --- a/lib/csu/powerpc/crtbeginS.c +++ b/lib/csu/common_elf/crtbeginS.c @@ -1,4 +1,4 @@ -/* $OpenBSD: crtbeginS.c,v 1.2 2000/10/13 05:14:03 drahn Exp $ */ +/* $OpenBSD: crtbeginS.c,v 1.1 2001/05/28 21:38:13 drahn Exp $ */ /* $NetBSD: crtbegin.c,v 1.1 1996/09/12 16:59:03 cgd Exp $ */ /* @@ -32,10 +32,6 @@ */ /* - * XXX EVENTUALLY SHOULD BE MERGED BACK WITH c++rt0.c - */ - -/* * Run-time module for GNU C++ compiled shared libraries. * * The linker constructs the following arrays of pointers to global @@ -93,10 +89,9 @@ _init() if (!initialized) { initialized = 1; __ctors(); - - atexit(__dtors); } } + void _fini() { @@ -104,4 +99,5 @@ _fini() * since the _init() function sets up the destructors to be called * by atexit, do not call the destructors here. */ + __dtors(); } diff --git a/lib/csu/powerpc/crtend.c b/lib/csu/common_elf/crtendS.c index 1b875de471b..2b3dbb174a1 100644 --- a/lib/csu/powerpc/crtend.c +++ b/lib/csu/common_elf/crtendS.c @@ -1,4 +1,4 @@ -/* $OpenBSD: crtend.c,v 1.2 1999/08/20 14:11:36 niklas Exp $ */ +/* $OpenBSD: crtendS.c,v 1.1 2001/05/28 21:38:13 drahn Exp $ */ /* $NetBSD: crtend.c,v 1.1 1997/04/16 19:38:24 thorpej Exp $ */ #include <sys/cdefs.h> diff --git a/lib/csu/powerpc/Makefile b/lib/csu/powerpc/Makefile index 25094662210..d6621620630 100644 --- a/lib/csu/powerpc/Makefile +++ b/lib/csu/powerpc/Makefile @@ -6,6 +6,7 @@ OBJS= crt0.o gcrt0.o scrt0.o crtbegin.o crtend.o crtbeginS.o crtendS.o PICFLAG?=-fpic CLEANFILES+= core a.out +.PATH: ${.CURDIR}/../common_elf all: ${OBJS} diff --git a/lib/csu/powerpc/crtbegin.c b/lib/csu/powerpc/crtbegin.c deleted file mode 100644 index 4160b272ca6..00000000000 --- a/lib/csu/powerpc/crtbegin.c +++ /dev/null @@ -1,110 +0,0 @@ -/* $OpenBSD: crtbegin.c,v 1.2 1999/01/28 05:01:15 rahnds Exp $ */ -/* $NetBSD: crtbegin.c,v 1.1 1996/09/12 16:59:03 cgd Exp $ */ - -/* - * Copyright (c) 1993 Paul Kranenburg - * 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 Paul Kranenburg. - * 4. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. - */ - -#ifndef ECOFF_COMPAT - -/* - * XXX EVENTUALLY SHOULD BE MERGED BACK WITH c++rt0.c - */ - -/* - * Run-time module for GNU C++ compiled shared libraries. - * - * The linker constructs the following arrays of pointers to global - * constructors and destructors. The first element contains the - * number of pointers in each. - * The tables are also null-terminated. - - */ -#include <stdlib.h> - -void (*__CTOR_LIST__[0]) __P((void)) - __attribute__((section(".ctors"))) = { (void *)-1 }; /* XXX */ -void (*__DTOR_LIST__[0]) __P((void)) - __attribute__((section(".dtors"))) = { (void *)-1 }; /* XXX */ - -static void __dtors __P((void)); -static void __ctors __P((void)); - -static void -__dtors() -{ - unsigned long i = (unsigned long) __DTOR_LIST__[0]; - void (**p)(void); - - if (i == -1) { - for (i = 1; __DTOR_LIST__[i] != NULL; i++) - ; - i--; - } - p = __DTOR_LIST__ + i; - while (i--) - (**p--)(); -} - -static void -__ctors() -{ - void (**p)(void) = __CTOR_LIST__ + 1; - - while (*p) - (**p++)(); -} - -void -__init() -{ - static int initialized = 0; - - /* - * Call global constructors. - * Arrange to call global destructors at exit. - */ - if (!initialized) { - initialized = 1; - __ctors(); - } - atexit(__dtors); - -} - -void -__fini() -{ - /* - * Call global destructors. - */ - __dtors(); -} - -#endif /* !ECOFF_COMPAT */ diff --git a/lib/csu/powerpc/crtendS.c b/lib/csu/powerpc/crtendS.c deleted file mode 100644 index ea35355a16f..00000000000 --- a/lib/csu/powerpc/crtendS.c +++ /dev/null @@ -1,9 +0,0 @@ -/* $OpenBSD: crtendS.c,v 1.1 2000/06/13 04:07:03 rahnds Exp $ */ -/* $NetBSD: crtend.c,v 1.1 1997/04/16 19:38:24 thorpej Exp $ */ - -#include <sys/cdefs.h> - -static void (*__CTOR_LIST__[1]) __P((void)) - __attribute__((section(".ctors"))) = { (void *)0 }; /* XXX */ -static void (*__DTOR_LIST__[1]) __P((void)) - __attribute__((section(".dtors"))) = { (void *)0 }; /* XXX */ diff --git a/libexec/ld.so/loader.c b/libexec/ld.so/loader.c index 5c85b1d7e4b..1267ff23070 100644 --- a/libexec/ld.so/loader.c +++ b/libexec/ld.so/loader.c @@ -1,4 +1,4 @@ -/* $OpenBSD: loader.c,v 1.9 2001/05/14 22:18:19 niklas Exp $ */ +/* $OpenBSD: loader.c,v 1.10 2001/05/28 21:38:14 drahn Exp $ */ /* * Copyright (c) 1998 Per Fogelstrom, Opsycon AB @@ -123,6 +123,31 @@ putc(char c) #endif /* + * Routine to walk thru all of the objects except the first (main executable). + */ + +void +_dl_run_dtors(elf_object_t *object) +{ + if(_dl_debug) + _dl_printf("doing dtors: [%s]\n", object->load_name); + if(object->dyn.fini) { + (*object->dyn.fini)(); + } + if (object->next) { + _dl_run_dtors(object->next); + } +} +void +_dl_dtors() +{ + if(_dl_debug) + _dl_printf("doing dtors\n"); + if (_dl_objects->next) { + _dl_run_dtors(_dl_objects->next); + } +} +/* * This is the dynamic loader entrypoint. When entering here, depending * on architecture type, the stack and registers are set up according * to the architectures ABI specification. The first thing required @@ -244,7 +269,7 @@ _dl_printf("%p %p 0x%lx %p %p\n", argv, envp, loff, dynp, dl_data); _dl_rtld(_dl_objects); /* the first object is the executable itself, - * it is responsible for running it's ctors/dtors + * it is responsible for running it's own ctors/dtors * thus do NOT run the ctors for the executable, all of * the shared libraries which follow. */ @@ -252,6 +277,18 @@ _dl_printf("%p %p 0x%lx %p %p\n", argv, envp, loff, dynp, dl_data); _dl_call_init(_dl_objects->next); } + /* schedule a routine to be run at shutdown, by using atexit. + * cannot call atexit directly from ld.so ?? + */ + { + const Elf_Sym *sym; + Elf_Addr ooff; + + ooff = _dl_find_symbol("atexit", _dl_objects, &sym, 0, 0); + (*(void (*)(Elf_Addr))(sym->st_value + ooff))((Elf_Addr)_dl_dtors); + } + + /* * Finally make something to help gdb when poking around in the code. */ diff --git a/libexec/ld.so/test/Makefile b/libexec/ld.so/test/Makefile index d5dbc4c2e57..2266ad0c2fb 100644 --- a/libexec/ld.so/test/Makefile +++ b/libexec/ld.so/test/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.4 2001/05/14 22:18:23 niklas Exp $ +# $OpenBSD: Makefile,v 1.5 2001/05/28 21:38:14 drahn Exp $ DIR=/usr/src/libexec/ld.so/obj/ld.so .if (${MACHINE_ARCH} != "mips") @@ -46,5 +46,13 @@ libB.so: B.o CCtest: libA.so libB.so tst.o g++ ${LDFLAGS} -o $@ tst.o libB.so libA.so +run: + @echo running CCtest + LD_LIBRARY_PATH=. CCtest + @echo running dltest -l bar -f bar + LD_LIBRARY_PATH=. dltest -l bar -f bar + @echo running dltest -l foo -f foo + LD_LIBRARY_PATH=. dltest -l foo -f foo + .include <bsd.prog.mk> .include <bsd.subdir.mk> |