]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/back/archive.rs
Get rid of native_library projection queries
[rust.git] / compiler / rustc_codegen_llvm / src / back / archive.rs
1 //! A helper class for dealing with static archives
2
3 use std::env;
4 use std::ffi::{CStr, CString, OsString};
5 use std::fs;
6 use std::io::{self, Write};
7 use std::mem;
8 use std::path::{Path, PathBuf};
9 use std::ptr;
10 use std::str;
11
12 use object::read::macho::FatArch;
13
14 use crate::common;
15 use crate::llvm::archive_ro::{ArchiveRO, Child};
16 use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport};
17 use rustc_codegen_ssa::back::archive::{ArchiveBuilder, ArchiveBuilderBuilder};
18 use rustc_data_structures::memmap::Mmap;
19 use rustc_session::cstore::DllImport;
20 use rustc_session::Session;
21
22 /// Helper for adding many files to an archive.
23 #[must_use = "must call build() to finish building the archive"]
24 pub struct LlvmArchiveBuilder<'a> {
25     sess: &'a Session,
26     additions: Vec<Addition>,
27 }
28
29 enum Addition {
30     File { path: PathBuf, name_in_archive: String },
31     Archive { path: PathBuf, archive: ArchiveRO, skip: Box<dyn FnMut(&str) -> bool> },
32 }
33
34 impl Addition {
35     fn path(&self) -> &Path {
36         match self {
37             Addition::File { path, .. } | Addition::Archive { path, .. } => path,
38         }
39     }
40 }
41
42 fn is_relevant_child(c: &Child<'_>) -> bool {
43     match c.name() {
44         Some(name) => !name.contains("SYMDEF"),
45         None => false,
46     }
47 }
48
49 /// Map machine type strings to values of LLVM's MachineTypes enum.
50 fn llvm_machine_type(cpu: &str) -> LLVMMachineType {
51     match cpu {
52         "x86_64" => LLVMMachineType::AMD64,
53         "x86" => LLVMMachineType::I386,
54         "aarch64" => LLVMMachineType::ARM64,
55         "arm" => LLVMMachineType::ARM,
56         _ => panic!("unsupported cpu type {}", cpu),
57     }
58 }
59
60 fn try_filter_fat_archs(
61     archs: object::read::Result<&[impl FatArch]>,
62     target_arch: object::Architecture,
63     archive_path: &Path,
64     archive_map_data: &[u8],
65 ) -> io::Result<Option<PathBuf>> {
66     let archs = archs.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
67
68     let desired = match archs.iter().filter(|a| a.architecture() == target_arch).next() {
69         Some(a) => a,
70         None => return Ok(None),
71     };
72
73     let (mut new_f, extracted_path) = tempfile::Builder::new()
74         .suffix(archive_path.file_name().unwrap())
75         .tempfile()?
76         .keep()
77         .unwrap();
78
79     new_f.write_all(
80         desired.data(archive_map_data).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?,
81     )?;
82
83     Ok(Some(extracted_path))
84 }
85
86 fn try_extract_macho_fat_archive(
87     sess: &Session,
88     archive_path: &Path,
89 ) -> io::Result<Option<PathBuf>> {
90     let archive_map = unsafe { Mmap::map(fs::File::open(&archive_path)?)? };
91     let target_arch = match sess.target.arch.as_ref() {
92         "aarch64" => object::Architecture::Aarch64,
93         "x86_64" => object::Architecture::X86_64,
94         _ => return Ok(None),
95     };
96
97     match object::macho::FatHeader::parse(&*archive_map) {
98         Ok(h) if h.magic.get(object::endian::BigEndian) == object::macho::FAT_MAGIC => {
99             let archs = object::macho::FatHeader::parse_arch32(&*archive_map);
100             try_filter_fat_archs(archs, target_arch, archive_path, &*archive_map)
101         }
102         Ok(h) if h.magic.get(object::endian::BigEndian) == object::macho::FAT_MAGIC_64 => {
103             let archs = object::macho::FatHeader::parse_arch64(&*archive_map);
104             try_filter_fat_archs(archs, target_arch, archive_path, &*archive_map)
105         }
106         // Not a FatHeader at all, just return None.
107         _ => Ok(None),
108     }
109 }
110
111 impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
112     fn add_archive(
113         &mut self,
114         archive: &Path,
115         skip: Box<dyn FnMut(&str) -> bool + 'static>,
116     ) -> io::Result<()> {
117         let mut archive = archive.to_path_buf();
118         if self.sess.target.llvm_target.contains("-apple-macosx") {
119             if let Some(new_archive) = try_extract_macho_fat_archive(&self.sess, &archive)? {
120                 archive = new_archive
121             }
122         }
123         let archive_ro = match ArchiveRO::open(&archive) {
124             Ok(ar) => ar,
125             Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
126         };
127         if self.additions.iter().any(|ar| ar.path() == archive) {
128             return Ok(());
129         }
130         self.additions.push(Addition::Archive {
131             path: archive,
132             archive: archive_ro,
133             skip: Box::new(skip),
134         });
135         Ok(())
136     }
137
138     /// Adds an arbitrary file to this archive
139     fn add_file(&mut self, file: &Path) {
140         let name = file.file_name().unwrap().to_str().unwrap();
141         self.additions
142             .push(Addition::File { path: file.to_path_buf(), name_in_archive: name.to_owned() });
143     }
144
145     /// Combine the provided files, rlibs, and native libraries into a single
146     /// `Archive`.
147     fn build(mut self: Box<Self>, output: &Path) -> bool {
148         match self.build_with_llvm(output) {
149             Ok(any_members) => any_members,
150             Err(e) => self.sess.fatal(&format!("failed to build archive: {}", e)),
151         }
152     }
153 }
154
155 pub struct LlvmArchiveBuilderBuilder;
156
157 impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {
158     fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a> {
159         Box::new(LlvmArchiveBuilder { sess, additions: Vec::new() })
160     }
161
162     fn create_dll_import_lib(
163         &self,
164         sess: &Session,
165         lib_name: &str,
166         dll_imports: &[DllImport],
167         tmpdir: &Path,
168     ) -> PathBuf {
169         let output_path = {
170             let mut output_path: PathBuf = tmpdir.to_path_buf();
171             output_path.push(format!("{}_imports", lib_name));
172             output_path.with_extension("lib")
173         };
174
175         let target = &sess.target;
176         let mingw_gnu_toolchain = common::is_mingw_gnu_toolchain(target);
177
178         let import_name_and_ordinal_vector: Vec<(String, Option<u16>)> = dll_imports
179             .iter()
180             .map(|import: &DllImport| {
181                 if sess.target.arch == "x86" {
182                     (
183                         common::i686_decorated_name(import, mingw_gnu_toolchain, false),
184                         import.ordinal(),
185                     )
186                 } else {
187                     (import.name.to_string(), import.ordinal())
188                 }
189             })
190             .collect();
191
192         if mingw_gnu_toolchain {
193             // The binutils linker used on -windows-gnu targets cannot read the import
194             // libraries generated by LLVM: in our attempts, the linker produced an .EXE
195             // that loaded but crashed with an AV upon calling one of the imported
196             // functions.  Therefore, use binutils to create the import library instead,
197             // by writing a .DEF file to the temp dir and calling binutils's dlltool.
198             let def_file_path = tmpdir.join(format!("{}_imports", lib_name)).with_extension("def");
199
200             let def_file_content = format!(
201                 "EXPORTS\n{}",
202                 import_name_and_ordinal_vector
203                     .into_iter()
204                     .map(|(name, ordinal)| {
205                         match ordinal {
206                             Some(n) => format!("{} @{} NONAME", name, n),
207                             None => name,
208                         }
209                     })
210                     .collect::<Vec<String>>()
211                     .join("\n")
212             );
213
214             match std::fs::write(&def_file_path, def_file_content) {
215                 Ok(_) => {}
216                 Err(e) => {
217                     sess.fatal(&format!("Error writing .DEF file: {}", e));
218                 }
219             };
220
221             // --no-leading-underscore: For the `import_name_type` feature to work, we need to be
222             // able to control the *exact* spelling of each of the symbols that are being imported:
223             // hence we don't want `dlltool` adding leading underscores automatically.
224             let dlltool = find_binutils_dlltool(sess);
225             let result = std::process::Command::new(dlltool)
226                 .args([
227                     "-d",
228                     def_file_path.to_str().unwrap(),
229                     "-D",
230                     lib_name,
231                     "-l",
232                     output_path.to_str().unwrap(),
233                     "--no-leading-underscore",
234                 ])
235                 .output();
236
237             match result {
238                 Err(e) => {
239                     sess.fatal(&format!("Error calling dlltool: {}", e));
240                 }
241                 Ok(output) if !output.status.success() => sess.fatal(&format!(
242                     "Dlltool could not create import library: {}\n{}",
243                     String::from_utf8_lossy(&output.stdout),
244                     String::from_utf8_lossy(&output.stderr)
245                 )),
246                 _ => {}
247             }
248         } else {
249             // we've checked for \0 characters in the library name already
250             let dll_name_z = CString::new(lib_name).unwrap();
251
252             let output_path_z = rustc_fs_util::path_to_c_string(&output_path);
253
254             trace!("invoking LLVMRustWriteImportLibrary");
255             trace!("  dll_name {:#?}", dll_name_z);
256             trace!("  output_path {}", output_path.display());
257             trace!(
258                 "  import names: {}",
259                 dll_imports
260                     .iter()
261                     .map(|import| import.name.to_string())
262                     .collect::<Vec<_>>()
263                     .join(", "),
264             );
265
266             // All import names are Rust identifiers and therefore cannot contain \0 characters.
267             // FIXME: when support for #[link_name] is implemented, ensure that the import names
268             // still don't contain any \0 characters.  Also need to check that the names don't
269             // contain substrings like " @" or "NONAME" that are keywords or otherwise reserved
270             // in definition files.
271             let cstring_import_name_and_ordinal_vector: Vec<(CString, Option<u16>)> =
272                 import_name_and_ordinal_vector
273                     .into_iter()
274                     .map(|(name, ordinal)| (CString::new(name).unwrap(), ordinal))
275                     .collect();
276
277             let ffi_exports: Vec<LLVMRustCOFFShortExport> = cstring_import_name_and_ordinal_vector
278                 .iter()
279                 .map(|(name_z, ordinal)| LLVMRustCOFFShortExport::new(name_z.as_ptr(), *ordinal))
280                 .collect();
281             let result = unsafe {
282                 crate::llvm::LLVMRustWriteImportLibrary(
283                     dll_name_z.as_ptr(),
284                     output_path_z.as_ptr(),
285                     ffi_exports.as_ptr(),
286                     ffi_exports.len(),
287                     llvm_machine_type(&sess.target.arch) as u16,
288                     !sess.target.is_like_msvc,
289                 )
290             };
291
292             if result == crate::llvm::LLVMRustResult::Failure {
293                 sess.fatal(&format!(
294                     "Error creating import library for {}: {}",
295                     lib_name,
296                     llvm::last_error().unwrap_or("unknown LLVM error".to_string())
297                 ));
298             }
299         };
300
301         output_path
302     }
303 }
304
305 impl<'a> LlvmArchiveBuilder<'a> {
306     fn build_with_llvm(&mut self, output: &Path) -> io::Result<bool> {
307         let kind = &*self.sess.target.archive_format;
308         let kind = kind.parse::<ArchiveKind>().map_err(|_| kind).unwrap_or_else(|kind| {
309             self.sess.fatal(&format!("Don't know how to build archive of type: {}", kind))
310         });
311
312         let mut additions = mem::take(&mut self.additions);
313         let mut strings = Vec::new();
314         let mut members = Vec::new();
315
316         let dst = CString::new(output.to_str().unwrap())?;
317
318         unsafe {
319             for addition in &mut additions {
320                 match addition {
321                     Addition::File { path, name_in_archive } => {
322                         let path = CString::new(path.to_str().unwrap())?;
323                         let name = CString::new(name_in_archive.clone())?;
324                         members.push(llvm::LLVMRustArchiveMemberNew(
325                             path.as_ptr(),
326                             name.as_ptr(),
327                             None,
328                         ));
329                         strings.push(path);
330                         strings.push(name);
331                     }
332                     Addition::Archive { archive, skip, .. } => {
333                         for child in archive.iter() {
334                             let child = child.map_err(string_to_io_error)?;
335                             if !is_relevant_child(&child) {
336                                 continue;
337                             }
338                             let child_name = child.name().unwrap();
339                             if skip(child_name) {
340                                 continue;
341                             }
342
343                             // It appears that LLVM's archive writer is a little
344                             // buggy if the name we pass down isn't just the
345                             // filename component, so chop that off here and
346                             // pass it in.
347                             //
348                             // See LLVM bug 25877 for more info.
349                             let child_name =
350                                 Path::new(child_name).file_name().unwrap().to_str().unwrap();
351                             let name = CString::new(child_name)?;
352                             let m = llvm::LLVMRustArchiveMemberNew(
353                                 ptr::null(),
354                                 name.as_ptr(),
355                                 Some(child.raw),
356                             );
357                             members.push(m);
358                             strings.push(name);
359                         }
360                     }
361                 }
362             }
363
364             let r = llvm::LLVMRustWriteArchive(
365                 dst.as_ptr(),
366                 members.len() as libc::size_t,
367                 members.as_ptr() as *const &_,
368                 true,
369                 kind,
370             );
371             let ret = if r.into_result().is_err() {
372                 let err = llvm::LLVMRustGetLastError();
373                 let msg = if err.is_null() {
374                     "failed to write archive".into()
375                 } else {
376                     String::from_utf8_lossy(CStr::from_ptr(err).to_bytes())
377                 };
378                 Err(io::Error::new(io::ErrorKind::Other, msg))
379             } else {
380                 Ok(!members.is_empty())
381             };
382             for member in members {
383                 llvm::LLVMRustArchiveMemberFree(member);
384             }
385             ret
386         }
387     }
388 }
389
390 fn string_to_io_error(s: String) -> io::Error {
391     io::Error::new(io::ErrorKind::Other, format!("bad archive: {}", s))
392 }
393
394 fn find_binutils_dlltool(sess: &Session) -> OsString {
395     assert!(sess.target.options.is_like_windows && !sess.target.options.is_like_msvc);
396     if let Some(dlltool_path) = &sess.opts.unstable_opts.dlltool {
397         return dlltool_path.clone().into_os_string();
398     }
399
400     let mut tool_name: OsString = if sess.host.arch != sess.target.arch {
401         // We are cross-compiling, so we need the tool with the prefix matching our target
402         if sess.target.arch == "x86" {
403             "i686-w64-mingw32-dlltool"
404         } else {
405             "x86_64-w64-mingw32-dlltool"
406         }
407     } else {
408         // We are not cross-compiling, so we just want `dlltool`
409         "dlltool"
410     }
411     .into();
412
413     if sess.host.options.is_like_windows {
414         // If we're compiling on Windows, add the .exe suffix
415         tool_name.push(".exe");
416     }
417
418     // NOTE: it's not clear how useful it is to explicitly search PATH.
419     for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) {
420         let full_path = dir.join(&tool_name);
421         if full_path.is_file() {
422             return full_path.into_os_string();
423         }
424     }
425
426     // The user didn't specify the location of the dlltool binary, and we weren't able
427     // to find the appropriate one on the PATH.  Just return the name of the tool
428     // and let the invocation fail with a hopefully useful error message.
429     tool_name
430 }