diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map index 48af578b7fa7..c1e0969b3cb3 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -464,6 +464,7 @@ FBSDprivate_1.0 { /* needed by thread libraries */ __thr_jtable; + _dl_iterate_phdr_locked; _pthread_atfork; _pthread_attr_destroy; _pthread_attr_getdetachstate; diff --git a/lib/libc/gen/dlfcn.c b/lib/libc/gen/dlfcn.c index b678df9eef47..bb04c4dabc2b 100644 --- a/lib/libc/gen/dlfcn.c +++ b/lib/libc/gen/dlfcn.c @@ -202,9 +202,10 @@ dl_init_phdr_info(void) } #endif -#pragma weak dl_iterate_phdr +#pragma weak _dl_iterate_phdr_locked int -dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *) __unused, +_dl_iterate_phdr_locked( + int (*callback)(struct dl_phdr_info *, size_t, void *) __unused, void *data __unused) { #if defined IN_LIBDL @@ -226,14 +227,29 @@ dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *) __unused, _once(&dl_phdr_info_once, dl_init_phdr_info); ti.ti_module = 1; ti.ti_offset = 0; - mutex_lock(&dl_phdr_info_lock); phdr_info.dlpi_tls_data = __tls_get_addr(&ti); ret = callback(&phdr_info, sizeof(phdr_info), data); - mutex_unlock(&dl_phdr_info_lock); return (ret); #endif } +#pragma weak dl_iterate_phdr +int +dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *) __unused, + void *data __unused) +{ + int error; + +#if !defined(IN_LIBDL) && !defined(PIC) + mutex_lock(&dl_phdr_info_lock); +#endif + error = _dl_iterate_phdr_locked(callback, data); +#if !defined(IN_LIBDL) && !defined(PIC) + mutex_unlock(&dl_phdr_info_lock); +#endif + return (error); +} + #pragma weak fdlopen void * fdlopen(int fd __unused, int mode __unused) diff --git a/libexec/rtld-elf/Symbol.map b/libexec/rtld-elf/Symbol.map index 0b7ad63f60a7..f33a89ccb031 100644 --- a/libexec/rtld-elf/Symbol.map +++ b/libexec/rtld-elf/Symbol.map @@ -22,6 +22,7 @@ FBSD_1.3 { }; FBSDprivate_1.0 { + _dl_iterate_phdr_locked; _rtld_thread_init; _rtld_allocate_tls; _rtld_free_tls; diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index a8db1198a899..5f7b5a3e3f1e 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -252,6 +252,7 @@ int dladdr(const void *, Dl_info *) __exported; void dllockinit(void *, void *(*)(void *), void (*)(void *), void (*)(void *), void (*)(void *), void (*)(void *), void (*)(void *)) __exported; int dlinfo(void *, int , void *) __exported; +int _dl_iterate_phdr_locked(__dl_iterate_hdr_callback, void *) __exported; int dl_iterate_phdr(__dl_iterate_hdr_callback, void *) __exported; int _rtld_addr_phdr(const void *, struct dl_phdr_info *) __exported; int _rtld_get_stack_prot(void) __exported; @@ -4192,43 +4193,67 @@ rtld_fill_dl_phdr_info(const Obj_Entry *obj, struct dl_phdr_info *phdr_info) phdr_info->dlpi_subs = obj_loads - obj_count; } -int -dl_iterate_phdr(__dl_iterate_hdr_callback callback, void *param) +/* + * Returns with the bind lock dropped. + */ +static int +_dl_iterate_phdr_impl(__dl_iterate_hdr_callback callback, void *param, + RtldLockState *bind_lockstate) { struct dl_phdr_info phdr_info; Obj_Entry *obj, marker; - RtldLockState bind_lockstate, phdr_lockstate; int error; - init_marker(&marker); error = 0; + init_marker(&marker); - wlock_acquire(rtld_phdr_lock, &phdr_lockstate); - wlock_acquire(rtld_bind_lock, &bind_lockstate); for (obj = globallist_curr(TAILQ_FIRST(&obj_list)); obj != NULL;) { TAILQ_INSERT_AFTER(&obj_list, obj, &marker, next); rtld_fill_dl_phdr_info(obj, &phdr_info); hold_object(obj); - lock_release(rtld_bind_lock, &bind_lockstate); + + if (bind_lockstate != NULL) + lock_release(rtld_bind_lock, bind_lockstate); error = callback(&phdr_info, sizeof phdr_info, param); - wlock_acquire(rtld_bind_lock, &bind_lockstate); + if (bind_lockstate != NULL) + wlock_acquire(rtld_bind_lock, bind_lockstate); unhold_object(obj); obj = globallist_next(&marker); TAILQ_REMOVE(&obj_list, &marker, next); if (error != 0) { - lock_release(rtld_bind_lock, &bind_lockstate); - lock_release(rtld_phdr_lock, &phdr_lockstate); + if (bind_lockstate != NULL) + lock_release(rtld_bind_lock, bind_lockstate); return (error); } } - if (error == 0) { - rtld_fill_dl_phdr_info(&obj_rtld, &phdr_info); - lock_release(rtld_bind_lock, &bind_lockstate); - error = callback(&phdr_info, sizeof(phdr_info), param); - } + rtld_fill_dl_phdr_info(&obj_rtld, &phdr_info); + if (bind_lockstate != NULL) + lock_release(rtld_bind_lock, bind_lockstate); + + return (callback(&phdr_info, sizeof(phdr_info), param)); +} + +int +_dl_iterate_phdr_locked(__dl_iterate_hdr_callback callback, void *param) +{ + + return (_dl_iterate_phdr_impl(callback, param, NULL)); +} + +int +dl_iterate_phdr(__dl_iterate_hdr_callback callback, void *param) +{ + RtldLockState bind_lockstate, phdr_lockstate; + int error; + + wlock_acquire(rtld_phdr_lock, &phdr_lockstate); + wlock_acquire(rtld_bind_lock, &bind_lockstate); + + error = _dl_iterate_phdr_impl(callback, param, &phdr_lockstate); + lock_release(rtld_phdr_lock, &phdr_lockstate); return (error); }