From 859c67c82abbbc6a47fd83e1b7b5305186273ffd Mon Sep 17 00:00:00 2001 From: kettenis Date: Sat, 28 Dec 2013 18:38:42 +0000 Subject: [PATCH] Move atexit(3) into crtbegin.c and certbeginS.c such that we can pass the right __dso_handle and have dlopen'ed shared objects run their atexit handlers when they get unloaded. This is what Linux does, and several ports depend on this behaviour (and will crash upon exit without this chang). Based on an earlier diff from matthew@ Tested by ajacoutot@ ok deraadt@ --- lib/csu/common_elf/crtbegin.c | 10 +++++++++- lib/csu/common_elf/crtbeginS.c | 10 +++++++++- lib/csu/crtbegin.c | 10 +++++++++- lib/csu/crtbeginS.c | 10 +++++++++- lib/libc/shlib_version | 2 +- lib/libc/stdlib/atexit.c | 11 +---------- 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/lib/csu/common_elf/crtbegin.c b/lib/csu/common_elf/crtbegin.c index f594675d78d..bf2c4fb09cd 100644 --- a/lib/csu/common_elf/crtbegin.c +++ b/lib/csu/common_elf/crtbegin.c @@ -1,4 +1,4 @@ -/* $OpenBSD: crtbegin.c,v 1.16 2012/09/08 20:08:33 matthew Exp $ */ +/* $OpenBSD: crtbegin.c,v 1.17 2013/12/28 18:38:42 kettenis Exp $ */ /* $NetBSD: crtbegin.c,v 1.1 1996/09/12 16:59:03 cgd Exp $ */ /* @@ -84,6 +84,14 @@ void *__dso_handle = NULL; __asm(".hidden __dso_handle"); long __guard_local __dso_hidden __attribute__((section(".openbsd.randomdata"))); + +extern int __cxa_atexit(void (*)(void *), void *, void *) __attribute__((weak)); + +int +atexit(void (*fn)(void)) +{ + return (__cxa_atexit((void (*)(void *))fn, NULL, NULL)); +} #endif static const init_f __CTOR_LIST__[1] diff --git a/lib/csu/common_elf/crtbeginS.c b/lib/csu/common_elf/crtbeginS.c index b72698fe2b4..f3c784214a7 100644 --- a/lib/csu/common_elf/crtbeginS.c +++ b/lib/csu/common_elf/crtbeginS.c @@ -1,4 +1,4 @@ -/* $OpenBSD: crtbeginS.c,v 1.13 2012/09/08 20:08:33 matthew Exp $ */ +/* $OpenBSD: crtbeginS.c,v 1.14 2013/12/28 18:38:42 kettenis Exp $ */ /* $NetBSD: crtbegin.c,v 1.1 1996/09/12 16:59:03 cgd Exp $ */ /* @@ -72,7 +72,15 @@ __asm(".hidden __dso_handle"); long __guard_local __dso_hidden __attribute__((section(".openbsd.randomdata"))); +extern int __cxa_atexit(void (*)(void *), void *, void *) __attribute__((weak)); extern void __cxa_finalize(void *) __attribute__((weak)); + +int +atexit(void (*fn)(void)) +{ + return (__cxa_atexit((void (*)(void *))fn, NULL, &__dso_handle)); +} +asm(".hidden atexit"); #endif static init_f __CTOR_LIST__[1] diff --git a/lib/csu/crtbegin.c b/lib/csu/crtbegin.c index f594675d78d..bf2c4fb09cd 100644 --- a/lib/csu/crtbegin.c +++ b/lib/csu/crtbegin.c @@ -1,4 +1,4 @@ -/* $OpenBSD: crtbegin.c,v 1.16 2012/09/08 20:08:33 matthew Exp $ */ +/* $OpenBSD: crtbegin.c,v 1.17 2013/12/28 18:38:42 kettenis Exp $ */ /* $NetBSD: crtbegin.c,v 1.1 1996/09/12 16:59:03 cgd Exp $ */ /* @@ -84,6 +84,14 @@ void *__dso_handle = NULL; __asm(".hidden __dso_handle"); long __guard_local __dso_hidden __attribute__((section(".openbsd.randomdata"))); + +extern int __cxa_atexit(void (*)(void *), void *, void *) __attribute__((weak)); + +int +atexit(void (*fn)(void)) +{ + return (__cxa_atexit((void (*)(void *))fn, NULL, NULL)); +} #endif static const init_f __CTOR_LIST__[1] diff --git a/lib/csu/crtbeginS.c b/lib/csu/crtbeginS.c index b72698fe2b4..f3c784214a7 100644 --- a/lib/csu/crtbeginS.c +++ b/lib/csu/crtbeginS.c @@ -1,4 +1,4 @@ -/* $OpenBSD: crtbeginS.c,v 1.13 2012/09/08 20:08:33 matthew Exp $ */ +/* $OpenBSD: crtbeginS.c,v 1.14 2013/12/28 18:38:42 kettenis Exp $ */ /* $NetBSD: crtbegin.c,v 1.1 1996/09/12 16:59:03 cgd Exp $ */ /* @@ -72,7 +72,15 @@ __asm(".hidden __dso_handle"); long __guard_local __dso_hidden __attribute__((section(".openbsd.randomdata"))); +extern int __cxa_atexit(void (*)(void *), void *, void *) __attribute__((weak)); extern void __cxa_finalize(void *) __attribute__((weak)); + +int +atexit(void (*fn)(void)) +{ + return (__cxa_atexit((void (*)(void *))fn, NULL, &__dso_handle)); +} +asm(".hidden atexit"); #endif static init_f __CTOR_LIST__[1] diff --git a/lib/libc/shlib_version b/lib/libc/shlib_version index 396b3134567..0b2ee0d1ffd 100644 --- a/lib/libc/shlib_version +++ b/lib/libc/shlib_version @@ -1,4 +1,4 @@ -major=72 +major=73 minor=0 # note: If changes were made to include/thread_private.h or if system # calls were added/changed then librthread/shlib_version also be updated. diff --git a/lib/libc/stdlib/atexit.c b/lib/libc/stdlib/atexit.c index 52f1cf3c5c7..049da3261d8 100644 --- a/lib/libc/stdlib/atexit.c +++ b/lib/libc/stdlib/atexit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: atexit.c,v 1.16 2013/06/02 21:08:36 matthew Exp $ */ +/* $OpenBSD: atexit.c,v 1.17 2013/12/28 18:38:42 kettenis Exp $ */ /* * Copyright (c) 2002 Daniel Hartmeier * All rights reserved. @@ -101,15 +101,6 @@ unlock: return (ret); } -/* - * Register a function to be performed at exit. - */ -int -atexit(void (*func)(void)) -{ - return (__cxa_atexit((void (*)(void *))func, NULL, NULL)); -} - /* * Call all handlers registered with __cxa_atexit() for the shared * object owning 'dso'. -- 2.20.1