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