From 0090e01f08f7ed564a62fb3cd2e3b652317d39d7 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 23 Mar 2015 00:58:19 -0400 Subject: [PATCH 1/3] Get __pthread_get_minstack at runtime with dlsym MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Linking __pthread_get_minstack, even weakly, was causing Debian’s dpkg-shlibdeps to detect an unnecessarily strict versioned dependency on libc6. Closes #23628. Signed-off-by: Anders Kaseorg --- src/libstd/sys/unix/thread.rs | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index c5f07c6c75a..1d3cb43465b 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -13,12 +13,14 @@ use core::prelude::*; use cmp; +use dynamic_lib::DynamicLibrary; use ffi::CString; use io; use libc::consts::os::posix01::PTHREAD_STACK_MIN; use libc; use mem; use ptr; +use sync::{Once, ONCE_INIT}; use sys::os; use thunk::Thunk; use time::Duration; @@ -314,21 +316,30 @@ pub fn sleep(dur: Duration) { // is created in an application with big thread-local storage requirements. // See #6233 for rationale and details. // -// Link weakly to the symbol for compatibility with older versions of glibc. -// Assumes that we've been dynamically linked to libpthread but that is -// currently always the case. Note that you need to check that the symbol -// is non-null before calling it! +// Use dlsym to get the symbol value at runtime, for compatibility +// with older versions of glibc. Assumes that we've been dynamically +// linked to libpthread but that is currently always the case. We +// previously used weak linkage (under the same assumption), but that +// caused Debian to detect an unnecessarily strict versioned +// dependency on libc6 (#23628). #[cfg(target_os = "linux")] fn min_stack_size(attr: *const libc::pthread_attr_t) -> libc::size_t { type F = unsafe extern "C" fn(*const libc::pthread_attr_t) -> libc::size_t; - extern { - #[linkage = "extern_weak"] - static __pthread_get_minstack: *const (); - } - if __pthread_get_minstack.is_null() { - PTHREAD_STACK_MIN - } else { - unsafe { mem::transmute::<*const (), F>(__pthread_get_minstack)(attr) } + static INIT: Once = ONCE_INIT; + static mut __pthread_get_minstack: Option = None; + + INIT.call_once(|| { + let lib = DynamicLibrary::open(None).unwrap(); + unsafe { + if let Ok(f) = lib.symbol("__pthread_get_minstack") { + __pthread_get_minstack = Some(mem::transmute::<*const (), F>(f)); + } + } + }); + + match unsafe { __pthread_get_minstack } { + None => PTHREAD_STACK_MIN, + Some(f) => unsafe { f(attr) }, } } From b6641c1595687a6c7c8ba381b46b931a58a3ada4 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 23 Mar 2015 03:48:06 -0400 Subject: [PATCH 2/3] min_stack_size: update non-Linux implementation comment Signed-off-by: Anders Kaseorg --- src/libstd/sys/unix/thread.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index 1d3cb43465b..b3594dcaa75 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -343,8 +343,8 @@ fn min_stack_size(attr: *const libc::pthread_attr_t) -> libc::size_t { } } -// __pthread_get_minstack() is marked as weak but extern_weak linkage is -// not supported on OS X, hence this kludge... +// No point in looking up __pthread_get_minstack() on non-glibc +// platforms. #[cfg(not(target_os = "linux"))] fn min_stack_size(_: *const libc::pthread_attr_t) -> libc::size_t { PTHREAD_STACK_MIN From 737bb30f0af5b27785a006a91a8792a06478de87 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 23 Mar 2015 04:02:02 -0400 Subject: [PATCH 3/3] min_stack_size: clarify both reasons to use dlsym Signed-off-by: Anders Kaseorg --- src/libstd/sys/unix/thread.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index b3594dcaa75..c66d86b7625 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -316,11 +316,12 @@ pub fn sleep(dur: Duration) { // is created in an application with big thread-local storage requirements. // See #6233 for rationale and details. // -// Use dlsym to get the symbol value at runtime, for compatibility -// with older versions of glibc. Assumes that we've been dynamically -// linked to libpthread but that is currently always the case. We -// previously used weak linkage (under the same assumption), but that -// caused Debian to detect an unnecessarily strict versioned +// Use dlsym to get the symbol value at runtime, both for +// compatibility with older versions of glibc, and to avoid creating +// dependencies on GLIBC_PRIVATE symbols. Assumes that we've been +// dynamically linked to libpthread but that is currently always the +// case. We previously used weak linkage (under the same assumption), +// but that caused Debian to detect an unnecessarily strict versioned // dependency on libc6 (#23628). #[cfg(target_os = "linux")] fn min_stack_size(attr: *const libc::pthread_attr_t) -> libc::size_t {