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