diff options
author | 2001-05-28 21:38:13 +0000 | |
---|---|---|
committer | 2001-05-28 21:38:13 +0000 | |
commit | 52dd79880f22e4413e4ee7193242cbe7f87c33d8 (patch) | |
tree | 39567012300c21ff5aff173b38438708efb64ea5 | |
parent | Fix directory state tracking. (diff) | |
download | wireguard-openbsd-52dd79880f22e4413e4ee7193242cbe7f87c33d8.tar.xz wireguard-openbsd-52dd79880f22e4413e4ee7193242cbe7f87c33d8.zip |
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/crtbeginS.c (renamed from lib/csu/powerpc/crtbegin.c) | 31 | ||||
-rw-r--r-- | lib/csu/crtendS.c (renamed from lib/csu/powerpc/crtendS.c) | 2 | ||||
-rw-r--r-- | lib/csu/powerpc/Makefile | 1 | ||||
-rw-r--r-- | libexec/ld.so/loader.c | 41 | ||||
-rw-r--r-- | libexec/ld.so/test/Makefile | 10 |
7 files changed, 66 insertions, 31 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/crtbegin.c b/lib/csu/crtbeginS.c index 4160b272ca6..6945484f55c 100644 --- a/lib/csu/powerpc/crtbegin.c +++ b/lib/csu/crtbeginS.c @@ -1,4 +1,4 @@ -/* $OpenBSD: crtbegin.c,v 1.2 1999/01/28 05:01:15 rahnds 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 $ */ /* @@ -31,12 +31,6 @@ * 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. * @@ -48,15 +42,15 @@ */ #include <stdlib.h> -void (*__CTOR_LIST__[0]) __P((void)) +static void (*__CTOR_LIST__[0]) __P((void)) __attribute__((section(".ctors"))) = { (void *)-1 }; /* XXX */ -void (*__DTOR_LIST__[0]) __P((void)) +static void (*__DTOR_LIST__[0]) __P((void)) __attribute__((section(".dtors"))) = { (void *)-1 }; /* XXX */ static void __dtors __P((void)); static void __ctors __P((void)); -static void +void __dtors() { unsigned long i = (unsigned long) __DTOR_LIST__[0]; @@ -68,8 +62,9 @@ __dtors() i--; } p = __DTOR_LIST__ + i; - while (i--) + while (i--) { (**p--)(); + } } static void @@ -77,12 +72,13 @@ __ctors() { void (**p)(void) = __CTOR_LIST__ + 1; - while (*p) + while (*p) { (**p++)(); + } } void -__init() +_init() { static int initialized = 0; @@ -94,17 +90,14 @@ __init() initialized = 1; __ctors(); } - atexit(__dtors); - } void -__fini() +_fini() { /* - * Call global destructors. + * since the _init() function sets up the destructors to be called + * by atexit, do not call the destructors here. */ __dtors(); } - -#endif /* !ECOFF_COMPAT */ diff --git a/lib/csu/powerpc/crtendS.c b/lib/csu/crtendS.c index ea35355a16f..2b3dbb174a1 100644 --- a/lib/csu/powerpc/crtendS.c +++ b/lib/csu/crtendS.c @@ -1,4 +1,4 @@ -/* $OpenBSD: crtendS.c,v 1.1 2000/06/13 04:07:03 rahnds 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/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> |