about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBerrysoft <Strawberry_Str@hotmail.com>2025-05-16 09:41:52 +0800
committerBerrysoft <Strawberry_Str@hotmail.com>2025-05-16 09:41:52 +0800
commit5e048e0786674db68220b7e0e8b19b2a96bcae7c (patch)
treee2ebca896250bac82a515089a55e65cf0bac21fe
parent12fda865f2f67081a96b116cae5194c791b20fb1 (diff)
downloadrust-5e048e0786674db68220b7e0e8b19b2a96bcae7c.tar.gz
rust-5e048e0786674db68220b7e0e8b19b2a96bcae7c.zip
Simplify current_dll_path for Cygwin
-rw-r--r--compiler/rustc_session/src/filesearch.rs33
1 files changed, 11 insertions, 22 deletions
diff --git a/compiler/rustc_session/src/filesearch.rs b/compiler/rustc_session/src/filesearch.rs
index 2a85f44278a..207ba5157bd 100644
--- a/compiler/rustc_session/src/filesearch.rs
+++ b/compiler/rustc_session/src/filesearch.rs
@@ -58,7 +58,7 @@ pub fn make_target_bin_path(sysroot: &Path, target_triple: &str) -> PathBuf {
     sysroot.join(rustlib_path).join("bin")
 }
 
-#[cfg(all(unix, not(target_os = "cygwin")))]
+#[cfg(unix)]
 fn current_dll_path() -> Result<PathBuf, String> {
     use std::sync::OnceLock;
 
@@ -78,10 +78,16 @@ fn current_dll_path() -> Result<PathBuf, String> {
                 if libc::dladdr(addr, &mut info) == 0 {
                     return Err("dladdr failed".into());
                 }
-                if info.dli_fname.is_null() {
-                    return Err("dladdr returned null pointer".into());
-                }
-                let bytes = CStr::from_ptr(info.dli_fname).to_bytes();
+                #[cfg(target_os = "cygwin")]
+                let fname_ptr = info.dli_fname.as_ptr();
+                #[cfg(not(target_os = "cygwin"))]
+                let fname_ptr = {
+                    if info.dli_fname.is_null() {
+                        return Err("dladdr returned null pointer".into());
+                    }
+                    info.dli_fname
+                };
+                let bytes = CStr::from_ptr(fname_ptr).to_bytes();
                 let os = OsStr::from_bytes(bytes);
                 Ok(PathBuf::from(os))
             }
@@ -132,23 +138,6 @@ fn current_dll_path() -> Result<PathBuf, String> {
         .clone()
 }
 
-#[cfg(target_os = "cygwin")]
-fn current_dll_path() -> Result<PathBuf, String> {
-    use std::ffi::{CStr, OsStr};
-    use std::os::unix::prelude::*;
-
-    unsafe {
-        let addr = current_dll_path as usize as *mut _;
-        let mut info = std::mem::zeroed();
-        if libc::dladdr(addr, &mut info) == 0 {
-            return Err("dladdr failed".into());
-        }
-        let bytes = CStr::from_ptr(info.dli_fname.as_ptr()).to_bytes();
-        let os = OsStr::from_bytes(bytes);
-        Ok(PathBuf::from(os))
-    }
-}
-
 #[cfg(windows)]
 fn current_dll_path() -> Result<PathBuf, String> {
     use std::ffi::OsString;