diff options
| author | Konstantin Belousov <kib@FreeBSD.org> | 2012-05-09 15:57:59 +0000 |
|---|---|---|
| committer | Konstantin Belousov <kib@FreeBSD.org> | 2012-05-09 15:57:59 +0000 |
| commit | 9e57e94da3730b1557432fb0919fa64939f800d9 (patch) | |
| tree | 8c5087bcbd0ec2547580cc5b88e11ad76a995d5b | |
| parent | 05912cdac1c5e785359e07e9570e0726d278b2cd (diff) | |
Notes
| -rw-r--r-- | lib/libc/gen/Makefile.inc | 2 | ||||
| -rw-r--r-- | lib/libc/gen/Symbol.map | 1 | ||||
| -rw-r--r-- | lib/libc/gen/elf_utils.c | 47 | ||||
| -rw-r--r-- | lib/libc/include/libc_private.h | 3 | ||||
| -rw-r--r-- | lib/libc/stdlib/atexit.c | 23 | ||||
| -rw-r--r-- | lib/libthr/pthread.map | 1 | ||||
| -rw-r--r-- | lib/libthr/thread/thr_fork.c | 22 | ||||
| -rw-r--r-- | lib/libthr/thread/thr_private.h | 3 | ||||
| -rw-r--r-- | libexec/rtld-elf/rtld.c | 8 |
9 files changed, 106 insertions, 4 deletions
diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index ed61bd58a932..f5257b0cc9d5 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -10,7 +10,7 @@ SRCS+= __getosreldate.c __xuname.c \ alarm.c arc4random.c assert.c basename.c check_utility_compat.c \ clock.c closedir.c confstr.c \ crypt.c ctermid.c daemon.c devname.c dirname.c disklabel.c \ - dlfcn.c drand48.c erand48.c err.c errlst.c errno.c \ + dlfcn.c drand48.c elf_utils.c erand48.c err.c errlst.c errno.c \ exec.c fdevname.c feature_present.c fmtcheck.c fmtmsg.c fnmatch.c \ fpclassify.c frexp.c fstab.c ftok.c fts.c fts-compat.c ftw.c \ getbootfile.c getbsize.c \ diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map index fd7b10a1b8c8..4888b97b6e9e 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -445,6 +445,7 @@ FBSDprivate_1.0 { _rtld_atfork_post; _rtld_error; /* for private use */ _rtld_thread_init; /* for private use */ + __elf_phdr_match_addr; _err; _warn; __fmtcheck; diff --git a/lib/libc/gen/elf_utils.c b/lib/libc/gen/elf_utils.c new file mode 100644 index 000000000000..9ee5f419607e --- /dev/null +++ b/lib/libc/gen/elf_utils.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2010 Konstantin Belousov <kib@freebsd.org> + * 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. Neither the name of the author nor the names of any co-contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. + * + * $FreeBSD$ + */ + +#include <link.h> + +int +__elf_phdr_match_addr(struct dl_phdr_info *phdr_info, void *addr) +{ + const Elf_Phdr *ph; + int i; + + for (i = 0; i < phdr_info->dlpi_phnum; i++) { + ph = &phdr_info->dlpi_phdr[i]; + if (ph->p_type != PT_LOAD || (ph->p_flags & PF_X) == 0) + continue; + if (phdr_info->dlpi_addr + ph->p_vaddr <= (uintptr_t)addr && + (uintptr_t)addr + sizeof(addr) < phdr_info->dlpi_addr + + ph->p_vaddr + ph->p_memsz) + break; + } + return (i != phdr_info->dlpi_phnum); +} diff --git a/lib/libc/include/libc_private.h b/lib/libc/include/libc_private.h index cc1c12cf40e1..ff37568de195 100644 --- a/lib/libc/include/libc_private.h +++ b/lib/libc/include/libc_private.h @@ -235,4 +235,7 @@ extern int __sys_fcntl(int, int, ...); /* execve() with PATH processing to implement posix_spawnp() */ int _execvpe(const char *, char * const *, char * const *); +struct dl_phdr_info; +int __elf_phdr_match_addr(struct dl_phdr_info *, void *); + #endif /* _LIBC_PRIVATE_H_ */ diff --git a/lib/libc/stdlib/atexit.c b/lib/libc/stdlib/atexit.c index 916119f315e8..06f31c36f68e 100644 --- a/lib/libc/stdlib/atexit.c +++ b/lib/libc/stdlib/atexit.c @@ -37,6 +37,7 @@ static char sccsid[] = "@(#)atexit.c 8.2 (Berkeley) 7/3/94"; __FBSDID("$FreeBSD$"); #include "namespace.h" +#include <link.h> #include <stddef.h> #include <stdlib.h> #include <unistd.h> @@ -146,6 +147,9 @@ __cxa_atexit(void (*func)(void *), void *arg, void *dso) return (error); } +#pragma weak __pthread_cxa_finalize +void __pthread_cxa_finalize(const struct dl_phdr_info *); + /* * Call all handlers registered with __cxa_atexit for the shared * object owning 'dso'. Note: if 'dso' is NULL, then all remaining @@ -154,18 +158,28 @@ __cxa_atexit(void (*func)(void *), void *arg, void *dso) void __cxa_finalize(void *dso) { + struct dl_phdr_info phdr_info; struct atexit *p; struct atexit_fn fn; - int n; + int n, has_phdr; + + if (dso != NULL) + has_phdr = _rtld_addr_phdr(dso, &phdr_info); + else + has_phdr = 0; _MUTEX_LOCK(&atexit_mutex); for (p = __atexit; p; p = p->next) { for (n = p->ind; --n >= 0;) { if (p->fns[n].fn_type == ATEXIT_FN_EMPTY) continue; /* already been called */ - if (dso != NULL && dso != p->fns[n].fn_dso) - continue; /* wrong DSO */ fn = p->fns[n]; + if (dso != NULL && dso != fn.fn_dso) { + /* wrong DSO ? */ + if (!has_phdr || !__elf_phdr_match_addr( + &phdr_info, fn.fn_ptr.cxa_func)) + continue; + } /* Mark entry to indicate that this particular handler has already been called. @@ -182,4 +196,7 @@ __cxa_finalize(void *dso) } } _MUTEX_UNLOCK(&atexit_mutex); + + if (has_phdr && &__pthread_cxa_finalize != NULL) + __pthread_cxa_finalize(&phdr_info); } diff --git a/lib/libthr/pthread.map b/lib/libthr/pthread.map index 79bbd4c11a81..0cd7ebbc9811 100644 --- a/lib/libthr/pthread.map +++ b/lib/libthr/pthread.map @@ -198,6 +198,7 @@ FBSDprivate_1.0 { __poll; __pthread_cond_timedwait; __pthread_cond_wait; + __pthread_cxa_finalize; __pthread_mutex_init; __pthread_mutex_lock; __pthread_mutex_timedlock; diff --git a/lib/libthr/thread/thr_fork.c b/lib/libthr/thread/thr_fork.c index 529f38f7c356..b1c221eca83d 100644 --- a/lib/libthr/thread/thr_fork.c +++ b/lib/libthr/thread/thr_fork.c @@ -59,6 +59,7 @@ #include "namespace.h" #include <errno.h> +#include <link.h> #include <string.h> #include <stdlib.h> #include <unistd.h> @@ -94,6 +95,27 @@ _pthread_atfork(void (*prepare)(void), void (*parent)(void), return (0); } +void +__pthread_cxa_finalize(struct dl_phdr_info *phdr_info) +{ + struct pthread *curthread; + struct pthread_atfork *af, *af1; + + _thr_check_init(); + + curthread = _get_curthread(); + THR_UMUTEX_LOCK(curthread, &_thr_atfork_lock); + TAILQ_FOREACH_SAFE(af, &_thr_atfork_list, qe, af1) { + if (__elf_phdr_match_addr(phdr_info, af->prepare) || + __elf_phdr_match_addr(phdr_info, af->parent) || + __elf_phdr_match_addr(phdr_info, af->child)) { + TAILQ_REMOVE(&_thr_atfork_list, af, qe); + free(af); + } + } + THR_UMUTEX_UNLOCK(curthread, &_thr_atfork_lock); +} + __weak_reference(_fork, fork); pid_t _fork(void); diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h index dba494609201..ee9c0bf58a31 100644 --- a/lib/libthr/thread/thr_private.h +++ b/lib/libthr/thread/thr_private.h @@ -725,6 +725,9 @@ _thr_check_init(void) _libpthread_init(NULL); } +struct dl_phdr_info; +void __pthread_cxa_finalize(struct dl_phdr_info *phdr_info); + __END_DECLS #endif /* !_THR_PRIVATE_H */ diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index 1077350e9974..aaa0369f301f 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -3719,3 +3719,11 @@ fetch_ventry(const Obj_Entry *obj, unsigned long symnum) } return NULL; } + +/* + * No unresolved symbols for rtld. + */ +void +__pthread_cxa_finalize(struct dl_phdr_info *a) +{ +} |
