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