This commit applies rustfmt with rust-lang/rust's default settings to files in src/libstd/sys *that are not involved in any currently open PR* to minimize merge conflicts. THe list of files involved in open PRs was determined by querying GitHub's GraphQL API with this script: https://gist.github.com/dtolnay/aa9c34993dc051a4f344d1b10e4487e8 With the list of files from the script in outstanding_files, the relevant commands were: $ find src/libstd/sys -name '*.rs' \ | xargs rustfmt --edition=2018 --unstable-features --skip-children $ rg libstd/sys outstanding_files | xargs git checkout -- Repeating this process several months apart should get us coverage of most of the rest of the files. To confirm no funny business: $ git checkout $THIS_COMMIT^ $ git show --pretty= --name-only $THIS_COMMIT \ | xargs rustfmt --edition=2018 --unstable-features --skip-children $ git diff $THIS_COMMIT # there should be no difference
56 lines
2 KiB
Rust
56 lines
2 KiB
Rust
//! Support for "weak linkage" to symbols on Unix
|
|
//!
|
|
//! Some I/O operations we do in libstd require newer versions of OSes but we
|
|
//! need to maintain binary compatibility with older releases for now. In order
|
|
//! to use the new functionality when available we use this module for
|
|
//! detection.
|
|
//!
|
|
//! One option to use here is weak linkage, but that is unfortunately only
|
|
//! really workable on Linux. Hence, use dlsym to get the symbol value at
|
|
//! runtime. This is also done for compatibility with older versions of glibc,
|
|
//! and to avoid creating dependencies on GLIBC_PRIVATE symbols. It assumes that
|
|
//! we've been dynamically linked to the library the symbol comes from, but that
|
|
//! is currently always the case for things like libpthread/libc.
|
|
//!
|
|
//! A long time ago this used weak linkage for the __pthread_get_minstack
|
|
//! symbol, but that caused Debian to detect an unnecessarily strict versioned
|
|
//! dependency on libc6 (#23628).
|
|
|
|
use crate::ffi::CStr;
|
|
use crate::marker;
|
|
use crate::mem;
|
|
use crate::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
pub struct Weak<F> {
|
|
name: &'static str,
|
|
addr: AtomicUsize,
|
|
_marker: marker::PhantomData<F>,
|
|
}
|
|
|
|
impl<F> Weak<F> {
|
|
pub const fn new(name: &'static str) -> Weak<F> {
|
|
Weak { name, addr: AtomicUsize::new(1), _marker: marker::PhantomData }
|
|
}
|
|
|
|
pub fn get(&self) -> Option<F> {
|
|
assert_eq!(mem::size_of::<F>(), mem::size_of::<usize>());
|
|
unsafe {
|
|
if self.addr.load(Ordering::SeqCst) == 1 {
|
|
self.addr.store(fetch(self.name), Ordering::SeqCst);
|
|
}
|
|
match self.addr.load(Ordering::SeqCst) {
|
|
0 => None,
|
|
addr => Some(mem::transmute_copy::<usize, F>(&addr)),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
unsafe fn fetch(name: &str) -> usize {
|
|
let name = match CStr::from_bytes_with_nul(name.as_bytes()) {
|
|
Ok(cstr) => cstr,
|
|
Err(..) => return 0,
|
|
};
|
|
assert!(false, "FIXME: fetch");
|
|
libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) as usize
|
|
}
|