1 use rustc_data_structures::fx::FxHashSet;
2 use rustc_data_structures::memmap::Mmap;
3 use rustc_session::cstore::DllImport;
4 use rustc_session::Session;
5 use rustc_span::symbol::Symbol;
7 use super::metadata::search_for_section;
9 pub use ar_archive_writer::get_native_object_symbols;
10 use ar_archive_writer::{write_archive_to_stream, ArchiveKind, NewArchiveMember};
11 use object::read::archive::ArchiveFile;
12 use object::read::macho::FatArch;
13 use tempfile::Builder as TempFileBuilder;
15 use std::error::Error;
18 use std::path::{Path, PathBuf};
20 // Re-exporting for rustc_codegen_llvm::back::archive
21 pub use crate::errors::{ArchiveBuildFailure, ExtractBundledLibsError, UnknownArchiveKind};
23 pub trait ArchiveBuilderBuilder {
24 fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a>;
26 /// Creates a DLL Import Library <https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-creation#creating-an-import-library>.
27 /// and returns the path on disk to that import library.
28 /// This functions doesn't take `self` so that it can be called from
29 /// `linker_with_args`, which is specialized on `ArchiveBuilder` but
30 /// doesn't take or create an instance of that type.
31 fn create_dll_import_lib(
35 dll_imports: &[DllImport],
37 is_direct_dependency: bool,
40 fn extract_bundled_libs<'a>(
44 bundled_lib_file_names: &FxHashSet<Symbol>,
45 ) -> Result<(), ExtractBundledLibsError<'_>> {
46 let archive_map = unsafe {
49 .map_err(|e| ExtractBundledLibsError::OpenFile { rlib, error: Box::new(e) })?,
51 .map_err(|e| ExtractBundledLibsError::MmapFile { rlib, error: Box::new(e) })?
53 let archive = ArchiveFile::parse(&*archive_map)
54 .map_err(|e| ExtractBundledLibsError::ParseArchive { rlib, error: Box::new(e) })?;
56 for entry in archive.members() {
58 .map_err(|e| ExtractBundledLibsError::ReadEntry { rlib, error: Box::new(e) })?;
61 .map_err(|e| ExtractBundledLibsError::ArchiveMember { rlib, error: Box::new(e) })?;
62 let name = std::str::from_utf8(entry.name())
63 .map_err(|e| ExtractBundledLibsError::ConvertName { rlib, error: Box::new(e) })?;
64 if !bundled_lib_file_names.contains(&Symbol::intern(name)) {
65 continue; // We need to extract only native libraries.
67 let data = search_for_section(rlib, data, ".bundled_lib").map_err(|e| {
68 ExtractBundledLibsError::ExtractSection { rlib, error: Box::<dyn Error>::from(e) }
70 std::fs::write(&outdir.join(&name), data)
71 .map_err(|e| ExtractBundledLibsError::WriteFile { rlib, error: Box::new(e) })?;
77 pub trait ArchiveBuilder<'a> {
78 fn add_file(&mut self, path: &Path);
83 skip: Box<dyn FnMut(&str) -> bool + 'static>,
86 fn build(self: Box<Self>, output: &Path) -> bool;
89 #[must_use = "must call build() to finish building the archive"]
90 pub struct ArArchiveBuilder<'a> {
93 fn(buf: &[u8], f: &mut dyn FnMut(&[u8]) -> io::Result<()>) -> io::Result<bool>,
95 src_archives: Vec<(PathBuf, Mmap)>,
96 // Don't use an `HashMap` here, as the order is important. `lib.rmeta` needs
97 // to be at the end of an archive in some cases for linkers to not get confused.
98 entries: Vec<(Vec<u8>, ArchiveEntry)>,
103 FromArchive { archive_index: usize, file_range: (u64, u64) },
107 impl<'a> ArArchiveBuilder<'a> {
110 get_object_symbols: fn(
112 f: &mut dyn FnMut(&[u8]) -> io::Result<()>,
113 ) -> io::Result<bool>,
114 ) -> ArArchiveBuilder<'a> {
115 ArArchiveBuilder { sess, get_object_symbols, src_archives: vec![], entries: vec![] }
119 fn try_filter_fat_archs<'a>(
120 archs: object::read::Result<&[impl FatArch]>,
121 target_arch: object::Architecture,
122 archive_map_data: &'a [u8],
123 ) -> io::Result<Option<(&'a [u8], u64)>> {
124 let archs = archs.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
126 let desired = match archs.iter().find(|a| a.architecture() == target_arch) {
128 None => return Ok(None),
132 desired.data(archive_map_data).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?,
133 desired.offset().into(),
137 pub fn try_extract_macho_fat_archive<'a>(
139 archive_bytes: &'a [u8],
140 ) -> io::Result<Option<(&'a [u8], u64)>> {
141 let target_arch = match sess.target.arch.as_ref() {
142 "aarch64" => object::Architecture::Aarch64,
143 "x86_64" => object::Architecture::X86_64,
144 _ => return Ok(None),
147 match object::macho::FatHeader::parse(archive_bytes) {
148 Ok(h) if h.magic.get(object::endian::BigEndian) == object::macho::FAT_MAGIC => {
149 let archs = object::macho::FatHeader::parse_arch32(archive_bytes);
150 try_filter_fat_archs(archs, target_arch, archive_bytes)
152 Ok(h) if h.magic.get(object::endian::BigEndian) == object::macho::FAT_MAGIC_64 => {
153 let archs = object::macho::FatHeader::parse_arch64(archive_bytes);
154 try_filter_fat_archs(archs, target_arch, archive_bytes)
156 // Not a FatHeader at all, just return None.
161 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
165 mut skip: Box<dyn FnMut(&str) -> bool + 'static>,
166 ) -> io::Result<()> {
167 let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? };
168 if self.src_archives.iter().any(|archive| archive.0 == archive_path) {
172 let (archive_bytes, offset) = if self.sess.target.llvm_target.contains("-apple-macosx") {
173 if let Some((sub_archive, archive_offset)) =
174 try_extract_macho_fat_archive(&self.sess, &*archive_map)?
176 (sub_archive, Some(archive_offset))
178 (&*archive_map, None)
181 (&*archive_map, None)
184 let archive = ArchiveFile::parse(&*archive_bytes)
185 .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
186 let archive_index = self.src_archives.len();
188 for entry in archive.members() {
189 let entry = entry.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
190 let file_name = String::from_utf8(entry.name().to_vec())
191 .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
192 if !skip(&file_name) {
193 let mut range = entry.file_range();
194 if let Some(offset) = offset {
198 file_name.into_bytes(),
199 ArchiveEntry::FromArchive { archive_index, file_range: range },
204 self.src_archives.push((archive_path.to_owned(), archive_map));
208 /// Adds an arbitrary file to this archive
209 fn add_file(&mut self, file: &Path) {
211 file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(),
212 ArchiveEntry::File(file.to_owned()),
216 /// Combine the provided files, rlibs, and native libraries into a single
218 fn build(self: Box<Self>, output: &Path) -> bool {
219 let sess = self.sess;
220 match self.build_inner(output) {
221 Ok(any_members) => any_members,
222 Err(e) => sess.emit_fatal(ArchiveBuildFailure { error: e }),
227 impl<'a> ArArchiveBuilder<'a> {
228 fn build_inner(self, output: &Path) -> io::Result<bool> {
229 let archive_kind = match &*self.sess.target.archive_format {
230 "gnu" => ArchiveKind::Gnu,
231 "bsd" => ArchiveKind::Bsd,
232 "darwin" => ArchiveKind::Darwin,
233 "coff" => ArchiveKind::Coff,
235 self.sess.emit_fatal(UnknownArchiveKind { kind });
239 let mut entries = Vec::new();
241 for (entry_name, entry) in self.entries {
244 ArchiveEntry::FromArchive { archive_index, file_range } => {
245 let src_archive = &self.src_archives[archive_index];
247 let data = &src_archive.1
248 [file_range.0 as usize..file_range.0 as usize + file_range.1 as usize];
250 Box::new(data) as Box<dyn AsRef<[u8]>>
252 ArchiveEntry::File(file) => unsafe {
254 Mmap::map(File::open(file).map_err(|err| {
255 io_error_context("failed to open object file", err)
257 .map_err(|err| io_error_context("failed to map object file", err))?,
258 ) as Box<dyn AsRef<[u8]>>
262 entries.push(NewArchiveMember {
264 get_symbols: self.get_object_symbols,
265 member_name: String::from_utf8(entry_name).unwrap(),
273 // Write to a temporary file first before atomically renaming to the final name.
274 // This prevents programs (including rustc) from attempting to read a partial archive.
275 // It also enables writing an archive with the same filename as a dependency on Windows as
276 // required by a test.
277 let mut archive_tmpfile = TempFileBuilder::new()
278 .suffix(".temp-archive")
279 .tempfile_in(output.parent().unwrap_or_else(|| Path::new("")))
280 .map_err(|err| io_error_context("couldn't create a temp file", err))?;
282 write_archive_to_stream(
283 archive_tmpfile.as_file_mut(),
291 let any_entries = !entries.is_empty();
293 // Drop src_archives to unmap all input archives, which is necessary if we want to write the
294 // output archive to the same location as an input archive on Windows.
295 drop(self.src_archives);
299 .map_err(|err| io_error_context("failed to rename archive file", err.error))?;
305 fn io_error_context(context: &str, err: io::Error) -> io::Error {
306 io::Error::new(io::ErrorKind::Other, format!("{context}: {err}"))