rustc_session/
filesearch.rs

1//! A module for searching for libraries
2
3use std::path::{Path, PathBuf};
4use std::{env, fs};
5
6use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
7use rustc_target::spec::Target;
8use smallvec::{SmallVec, smallvec};
9
10use crate::search_paths::{PathKind, SearchPath};
11
12#[derive(Clone)]
13pub struct FileSearch {
14    cli_search_paths: Vec<SearchPath>,
15    tlib_path: SearchPath,
16}
17
18impl FileSearch {
19    pub fn cli_search_paths<'b>(&'b self, kind: PathKind) -> impl Iterator<Item = &'b SearchPath> {
20        self.cli_search_paths.iter().filter(move |sp| sp.kind.matches(kind))
21    }
22
23    pub fn search_paths<'b>(&'b self, kind: PathKind) -> impl Iterator<Item = &'b SearchPath> {
24        self.cli_search_paths
25            .iter()
26            .filter(move |sp| sp.kind.matches(kind))
27            .chain(std::iter::once(&self.tlib_path))
28    }
29
30    pub fn new(cli_search_paths: &[SearchPath], tlib_path: &SearchPath, target: &Target) -> Self {
31        let this = FileSearch {
32            cli_search_paths: cli_search_paths.to_owned(),
33            tlib_path: tlib_path.clone(),
34        };
35        this.refine(&["lib", &target.staticlib_prefix, &target.dll_prefix])
36    }
37    // Produce a new file search from this search that has a smaller set of candidates.
38    fn refine(mut self, allowed_prefixes: &[&str]) -> FileSearch {
39        self.cli_search_paths
40            .iter_mut()
41            .for_each(|search_paths| search_paths.files.retain(allowed_prefixes));
42        self.tlib_path.files.retain(allowed_prefixes);
43
44        self
45    }
46}
47
48pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
49    let rustlib_path = rustc_target::relative_target_rustlib_path(sysroot, target_triple);
50    sysroot.join(rustlib_path).join("lib")
51}
52
53/// Returns a path to the target's `bin` folder within its `rustlib` path in the sysroot. This is
54/// where binaries are usually installed, e.g. the self-contained linkers, lld-wrappers, LLVM tools,
55/// etc.
56pub fn make_target_bin_path(sysroot: &Path, target_triple: &str) -> PathBuf {
57    let rustlib_path = rustc_target::relative_target_rustlib_path(sysroot, target_triple);
58    sysroot.join(rustlib_path).join("bin")
59}
60
61#[cfg(unix)]
62fn current_dll_path() -> Result<PathBuf, String> {
63    use std::sync::OnceLock;
64
65    // This is somewhat expensive relative to other work when compiling `fn main() {}` as `dladdr`
66    // needs to iterate over the symbol table of librustc_driver.so until it finds a match.
67    // As such cache this to avoid recomputing if we try to get the sysroot in multiple places.
68    static CURRENT_DLL_PATH: OnceLock<Result<PathBuf, String>> = OnceLock::new();
69    CURRENT_DLL_PATH
70        .get_or_init(|| {
71            use std::ffi::{CStr, OsStr};
72            use std::os::unix::prelude::*;
73
74            #[cfg(not(target_os = "aix"))]
75            unsafe {
76                let addr = current_dll_path as usize as *mut _;
77                let mut info = std::mem::zeroed();
78                if libc::dladdr(addr, &mut info) == 0 {
79                    return Err("dladdr failed".into());
80                }
81                if info.dli_fname.is_null() {
82                    return Err("dladdr returned null pointer".into());
83                }
84                let bytes = CStr::from_ptr(info.dli_fname).to_bytes();
85                let os = OsStr::from_bytes(bytes);
86                Ok(PathBuf::from(os))
87            }
88
89            #[cfg(target_os = "aix")]
90            unsafe {
91                // On AIX, the symbol `current_dll_path` references a function descriptor.
92                // A function descriptor is consisted of (See https://reviews.llvm.org/D62532)
93                // * The address of the entry point of the function.
94                // * The TOC base address for the function.
95                // * The environment pointer.
96                // The function descriptor is in the data section.
97                let addr = current_dll_path as u64;
98                let mut buffer = vec![std::mem::zeroed::<libc::ld_info>(); 64];
99                loop {
100                    if libc::loadquery(
101                        libc::L_GETINFO,
102                        buffer.as_mut_ptr() as *mut u8,
103                        (size_of::<libc::ld_info>() * buffer.len()) as u32,
104                    ) >= 0
105                    {
106                        break;
107                    } else {
108                        if std::io::Error::last_os_error().raw_os_error().unwrap() != libc::ENOMEM {
109                            return Err("loadquery failed".into());
110                        }
111                        buffer.resize(buffer.len() * 2, std::mem::zeroed::<libc::ld_info>());
112                    }
113                }
114                let mut current = buffer.as_mut_ptr() as *mut libc::ld_info;
115                loop {
116                    let data_base = (*current).ldinfo_dataorg as u64;
117                    let data_end = data_base + (*current).ldinfo_datasize;
118                    if (data_base..data_end).contains(&addr) {
119                        let bytes = CStr::from_ptr(&(*current).ldinfo_filename[0]).to_bytes();
120                        let os = OsStr::from_bytes(bytes);
121                        return Ok(PathBuf::from(os));
122                    }
123                    if (*current).ldinfo_next == 0 {
124                        break;
125                    }
126                    current = (current as *mut i8).offset((*current).ldinfo_next as isize)
127                        as *mut libc::ld_info;
128                }
129                return Err(format!("current dll's address {} is not in the load map", addr));
130            }
131        })
132        .clone()
133}
134
135#[cfg(windows)]
136fn current_dll_path() -> Result<PathBuf, String> {
137    use std::ffi::OsString;
138    use std::io;
139    use std::os::windows::prelude::*;
140
141    use windows::Win32::Foundation::HMODULE;
142    use windows::Win32::System::LibraryLoader::{
143        GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, GetModuleFileNameW, GetModuleHandleExW,
144    };
145    use windows::core::PCWSTR;
146
147    let mut module = HMODULE::default();
148    unsafe {
149        GetModuleHandleExW(
150            GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
151            PCWSTR(current_dll_path as *mut u16),
152            &mut module,
153        )
154    }
155    .map_err(|e| e.to_string())?;
156
157    let mut filename = vec![0; 1024];
158    let n = unsafe { GetModuleFileNameW(Some(module), &mut filename) } as usize;
159    if n == 0 {
160        return Err(format!("GetModuleFileNameW failed: {}", io::Error::last_os_error()));
161    }
162    if n >= filename.capacity() {
163        return Err(format!("our buffer was too small? {}", io::Error::last_os_error()));
164    }
165
166    filename.truncate(n);
167
168    Ok(OsString::from_wide(&filename).into())
169}
170
171pub fn sysroot_candidates() -> SmallVec<[PathBuf; 2]> {
172    let target = crate::config::host_tuple();
173    let mut sysroot_candidates: SmallVec<[PathBuf; 2]> = smallvec![get_or_default_sysroot()];
174    let path = current_dll_path().and_then(|s| try_canonicalize(s).map_err(|e| e.to_string()));
175    if let Ok(dll) = path {
176        // use `parent` twice to chop off the file name and then also the
177        // directory containing the dll which should be either `lib` or `bin`.
178        if let Some(path) = dll.parent().and_then(|p| p.parent()) {
179            // The original `path` pointed at the `rustc_driver` crate's dll.
180            // Now that dll should only be in one of two locations. The first is
181            // in the compiler's libdir, for example `$sysroot/lib/*.dll`. The
182            // other is the target's libdir, for example
183            // `$sysroot/lib/rustlib/$target/lib/*.dll`.
184            //
185            // We don't know which, so let's assume that if our `path` above
186            // ends in `$target` we *could* be in the target libdir, and always
187            // assume that we may be in the main libdir.
188            sysroot_candidates.push(path.to_owned());
189
190            if path.ends_with(target) {
191                sysroot_candidates.extend(
192                    path.parent() // chop off `$target`
193                        .and_then(|p| p.parent()) // chop off `rustlib`
194                        .and_then(|p| p.parent()) // chop off `lib`
195                        .map(|s| s.to_owned()),
196                );
197            }
198        }
199    }
200
201    sysroot_candidates
202}
203
204/// Returns the provided sysroot or calls [`get_or_default_sysroot`] if it's none.
205/// Panics if [`get_or_default_sysroot`]  returns an error.
206pub fn materialize_sysroot(maybe_sysroot: Option<PathBuf>) -> PathBuf {
207    maybe_sysroot.unwrap_or_else(|| get_or_default_sysroot())
208}
209
210/// This function checks if sysroot is found using env::args().next(), and if it
211/// is not found, finds sysroot from current rustc_driver dll.
212pub fn get_or_default_sysroot() -> PathBuf {
213    // Follow symlinks. If the resolved path is relative, make it absolute.
214    fn canonicalize(path: PathBuf) -> PathBuf {
215        let path = try_canonicalize(&path).unwrap_or(path);
216        // See comments on this target function, but the gist is that
217        // gcc chokes on verbatim paths which fs::canonicalize generates
218        // so we try to avoid those kinds of paths.
219        fix_windows_verbatim_for_gcc(&path)
220    }
221
222    fn default_from_rustc_driver_dll() -> Result<PathBuf, String> {
223        let dll = current_dll_path().map(|s| canonicalize(s))?;
224
225        // `dll` will be in one of the following two:
226        // - compiler's libdir: $sysroot/lib/*.dll
227        // - target's libdir: $sysroot/lib/rustlib/$target/lib/*.dll
228        //
229        // use `parent` twice to chop off the file name and then also the
230        // directory containing the dll
231        let dir = dll.parent().and_then(|p| p.parent()).ok_or(format!(
232            "Could not move 2 levels upper using `parent()` on {}",
233            dll.display()
234        ))?;
235
236        // if `dir` points target's dir, move up to the sysroot
237        let mut sysroot_dir = if dir.ends_with(crate::config::host_tuple()) {
238            dir.parent() // chop off `$target`
239                .and_then(|p| p.parent()) // chop off `rustlib`
240                .and_then(|p| p.parent()) // chop off `lib`
241                .map(|s| s.to_owned())
242                .ok_or_else(|| {
243                    format!("Could not move 3 levels upper using `parent()` on {}", dir.display())
244                })?
245        } else {
246            dir.to_owned()
247        };
248
249        // On multiarch linux systems, there will be multiarch directory named
250        // with the architecture(e.g `x86_64-linux-gnu`) under the `lib` directory.
251        // Which cause us to mistakenly end up in the lib directory instead of the sysroot directory.
252        if sysroot_dir.ends_with("lib") {
253            sysroot_dir =
254                sysroot_dir.parent().map(|real_sysroot| real_sysroot.to_owned()).ok_or_else(
255                    || format!("Could not move to parent path of {}", sysroot_dir.display()),
256                )?
257        }
258
259        Ok(sysroot_dir)
260    }
261
262    // Use env::args().next() to get the path of the executable without
263    // following symlinks/canonicalizing any component. This makes the rustc
264    // binary able to locate Rust libraries in systems using content-addressable
265    // storage (CAS).
266    fn from_env_args_next() -> Option<PathBuf> {
267        let mut p = PathBuf::from(env::args_os().next()?);
268
269        // Check if sysroot is found using env::args().next() only if the rustc in argv[0]
270        // is a symlink (see #79253). We might want to change/remove it to conform with
271        // https://www.gnu.org/prep/standards/standards.html#Finding-Program-Files in the
272        // future.
273        if fs::read_link(&p).is_err() {
274            // Path is not a symbolic link or does not exist.
275            return None;
276        }
277
278        // Pop off `bin/rustc`, obtaining the suspected sysroot.
279        p.pop();
280        p.pop();
281        // Look for the target rustlib directory in the suspected sysroot.
282        let mut rustlib_path = rustc_target::relative_target_rustlib_path(&p, "dummy");
283        rustlib_path.pop(); // pop off the dummy target.
284        rustlib_path.exists().then_some(p)
285    }
286
287    from_env_args_next().unwrap_or(default_from_rustc_driver_dll().expect("Failed finding sysroot"))
288}