1 //! A helper class for dealing with static archives
4 use std::ffi::{CStr, CString, OsString};
6 use std::io::{self, Write};
8 use std::path::{Path, PathBuf};
12 use object::read::macho::FatArch;
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;
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> {
26 additions: Vec<Addition>,
30 File { path: PathBuf, name_in_archive: String },
31 Archive { path: PathBuf, archive: ArchiveRO, skip: Box<dyn FnMut(&str) -> bool> },
35 fn path(&self) -> &Path {
37 Addition::File { path, .. } | Addition::Archive { path, .. } => path,
42 fn is_relevant_child(c: &Child<'_>) -> bool {
44 Some(name) => !name.contains("SYMDEF"),
49 /// Map machine type strings to values of LLVM's MachineTypes enum.
50 fn llvm_machine_type(cpu: &str) -> LLVMMachineType {
52 "x86_64" => LLVMMachineType::AMD64,
53 "x86" => LLVMMachineType::I386,
54 "aarch64" => LLVMMachineType::ARM64,
55 "arm" => LLVMMachineType::ARM,
56 _ => panic!("unsupported cpu type {}", cpu),
60 fn try_filter_fat_archs(
61 archs: object::read::Result<&[impl FatArch]>,
62 target_arch: object::Architecture,
64 archive_map_data: &[u8],
65 ) -> io::Result<Option<PathBuf>> {
66 let archs = archs.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
68 let desired = match archs.iter().filter(|a| a.architecture() == target_arch).next() {
70 None => return Ok(None),
73 let (mut new_f, extracted_path) = tempfile::Builder::new()
74 .suffix(archive_path.file_name().unwrap())
80 desired.data(archive_map_data).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?,
83 Ok(Some(extracted_path))
86 fn try_extract_macho_fat_archive(
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,
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)
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)
106 // Not a FatHeader at all, just return None.
111 impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
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
123 let archive_ro = match ArchiveRO::open(&archive) {
125 Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
127 if self.additions.iter().any(|ar| ar.path() == archive) {
130 self.additions.push(Addition::Archive {
133 skip: Box::new(skip),
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();
142 .push(Addition::File { path: file.to_path_buf(), name_in_archive: name.to_owned() });
145 /// Combine the provided files, rlibs, and native libraries into a single
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)),
155 pub struct LlvmArchiveBuilderBuilder;
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() })
162 fn create_dll_import_lib(
166 dll_imports: &[DllImport],
170 let mut output_path: PathBuf = tmpdir.to_path_buf();
171 output_path.push(format!("{}_imports", lib_name));
172 output_path.with_extension("lib")
175 let target = &sess.target;
176 let mingw_gnu_toolchain = common::is_mingw_gnu_toolchain(target);
178 let import_name_and_ordinal_vector: Vec<(String, Option<u16>)> = dll_imports
180 .map(|import: &DllImport| {
181 if sess.target.arch == "x86" {
183 common::i686_decorated_name(import, mingw_gnu_toolchain, false),
187 (import.name.to_string(), import.ordinal())
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");
200 let def_file_content = format!(
202 import_name_and_ordinal_vector
204 .map(|(name, ordinal)| {
206 Some(n) => format!("{} @{} NONAME", name, n),
210 .collect::<Vec<String>>()
214 match std::fs::write(&def_file_path, def_file_content) {
217 sess.fatal(&format!("Error writing .DEF file: {}", e));
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)
228 def_file_path.to_str().unwrap(),
232 output_path.to_str().unwrap(),
233 "--no-leading-underscore",
239 sess.fatal(&format!("Error calling dlltool: {}", e));
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)
249 // we've checked for \0 characters in the library name already
250 let dll_name_z = CString::new(lib_name).unwrap();
252 let output_path_z = rustc_fs_util::path_to_c_string(&output_path);
254 trace!("invoking LLVMRustWriteImportLibrary");
255 trace!(" dll_name {:#?}", dll_name_z);
256 trace!(" output_path {}", output_path.display());
261 .map(|import| import.name.to_string())
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
274 .map(|(name, ordinal)| (CString::new(name).unwrap(), ordinal))
277 let ffi_exports: Vec<LLVMRustCOFFShortExport> = cstring_import_name_and_ordinal_vector
279 .map(|(name_z, ordinal)| LLVMRustCOFFShortExport::new(name_z.as_ptr(), *ordinal))
281 let result = unsafe {
282 crate::llvm::LLVMRustWriteImportLibrary(
284 output_path_z.as_ptr(),
285 ffi_exports.as_ptr(),
287 llvm_machine_type(&sess.target.arch) as u16,
288 !sess.target.is_like_msvc,
292 if result == crate::llvm::LLVMRustResult::Failure {
294 "Error creating import library for {}: {}",
296 llvm::last_error().unwrap_or("unknown LLVM error".to_string())
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))
312 let mut additions = mem::take(&mut self.additions);
313 let mut strings = Vec::new();
314 let mut members = Vec::new();
316 let dst = CString::new(output.to_str().unwrap())?;
319 for addition in &mut additions {
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(
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) {
338 let child_name = child.name().unwrap();
339 if skip(child_name) {
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
348 // See LLVM bug 25877 for more info.
350 Path::new(child_name).file_name().unwrap().to_str().unwrap();
351 let name = CString::new(child_name)?;
352 let m = llvm::LLVMRustArchiveMemberNew(
364 let r = llvm::LLVMRustWriteArchive(
366 members.len() as libc::size_t,
367 members.as_ptr() as *const &_,
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()
376 String::from_utf8_lossy(CStr::from_ptr(err).to_bytes())
378 Err(io::Error::new(io::ErrorKind::Other, msg))
380 Ok(!members.is_empty())
382 for member in members {
383 llvm::LLVMRustArchiveMemberFree(member);
390 fn string_to_io_error(s: String) -> io::Error {
391 io::Error::new(io::ErrorKind::Other, format!("bad archive: {}", s))
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();
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"
405 "x86_64-w64-mingw32-dlltool"
408 // We are not cross-compiling, so we just want `dlltool`
413 if sess.host.options.is_like_windows {
414 // If we're compiling on Windows, add the .exe suffix
415 tool_name.push(".exe");
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();
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.