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;
14 use std::error::Error;
16 use std::io::{self, Write};
17 use std::path::{Path, PathBuf};
19 // Re-exporting for rustc_codegen_llvm::back::archive
20 pub use crate::errors::{ArchiveBuildFailure, ExtractBundledLibsError, UnknownArchiveKind};
22 pub trait ArchiveBuilderBuilder {
23 fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a>;
25 /// Creates a DLL Import Library <https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-creation#creating-an-import-library>.
26 /// and returns the path on disk to that import library.
27 /// This functions doesn't take `self` so that it can be called from
28 /// `linker_with_args`, which is specialized on `ArchiveBuilder` but
29 /// doesn't take or create an instance of that type.
30 fn create_dll_import_lib(
34 dll_imports: &[DllImport],
36 is_direct_dependency: bool,
39 fn extract_bundled_libs<'a>(
43 bundled_lib_file_names: &FxHashSet<Symbol>,
44 ) -> Result<(), ExtractBundledLibsError<'_>> {
45 let archive_map = unsafe {
48 .map_err(|e| ExtractBundledLibsError::OpenFile { rlib, error: Box::new(e) })?,
50 .map_err(|e| ExtractBundledLibsError::MmapFile { rlib, error: Box::new(e) })?
52 let archive = ArchiveFile::parse(&*archive_map)
53 .map_err(|e| ExtractBundledLibsError::ParseArchive { rlib, error: Box::new(e) })?;
55 for entry in archive.members() {
57 .map_err(|e| ExtractBundledLibsError::ReadEntry { rlib, error: Box::new(e) })?;
60 .map_err(|e| ExtractBundledLibsError::ArchiveMember { rlib, error: Box::new(e) })?;
61 let name = std::str::from_utf8(entry.name())
62 .map_err(|e| ExtractBundledLibsError::ConvertName { rlib, error: Box::new(e) })?;
63 if !bundled_lib_file_names.contains(&Symbol::intern(name)) {
64 continue; // We need to extract only native libraries.
66 let data = search_for_section(rlib, data, ".bundled_lib").map_err(|e| {
67 ExtractBundledLibsError::ExtractSection { rlib, error: Box::<dyn Error>::from(e) }
69 std::fs::write(&outdir.join(&name), data)
70 .map_err(|e| ExtractBundledLibsError::WriteFile { rlib, error: Box::new(e) })?;
76 pub trait ArchiveBuilder<'a> {
77 fn add_file(&mut self, path: &Path);
82 skip: Box<dyn FnMut(&str) -> bool + 'static>,
85 fn build(self: Box<Self>, output: &Path) -> bool;
88 #[must_use = "must call build() to finish building the archive"]
89 pub struct ArArchiveBuilder<'a> {
92 fn(buf: &[u8], f: &mut dyn FnMut(&[u8]) -> io::Result<()>) -> io::Result<bool>,
94 src_archives: Vec<(PathBuf, Mmap)>,
95 // Don't use an `HashMap` here, as the order is important. `lib.rmeta` needs
96 // to be at the end of an archive in some cases for linkers to not get confused.
97 entries: Vec<(Vec<u8>, ArchiveEntry)>,
102 FromArchive { archive_index: usize, file_range: (u64, u64) },
106 impl<'a> ArArchiveBuilder<'a> {
109 get_object_symbols: fn(
111 f: &mut dyn FnMut(&[u8]) -> io::Result<()>,
112 ) -> io::Result<bool>,
113 ) -> ArArchiveBuilder<'a> {
114 ArArchiveBuilder { sess, get_object_symbols, src_archives: vec![], entries: vec![] }
118 fn try_filter_fat_archs(
119 archs: object::read::Result<&[impl FatArch]>,
120 target_arch: object::Architecture,
122 archive_map_data: &[u8],
123 ) -> io::Result<Option<PathBuf>> {
124 let archs = archs.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
126 let desired = match archs.iter().filter(|a| a.architecture() == target_arch).next() {
128 None => return Ok(None),
131 let (mut new_f, extracted_path) = tempfile::Builder::new()
132 .suffix(archive_path.file_name().unwrap())
138 desired.data(archive_map_data).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?,
141 Ok(Some(extracted_path))
144 pub fn try_extract_macho_fat_archive(
147 ) -> io::Result<Option<PathBuf>> {
148 let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? };
149 let target_arch = match sess.target.arch.as_ref() {
150 "aarch64" => object::Architecture::Aarch64,
151 "x86_64" => object::Architecture::X86_64,
152 _ => return Ok(None),
155 match object::macho::FatHeader::parse(&*archive_map) {
156 Ok(h) if h.magic.get(object::endian::BigEndian) == object::macho::FAT_MAGIC => {
157 let archs = object::macho::FatHeader::parse_arch32(&*archive_map);
158 try_filter_fat_archs(archs, target_arch, archive_path, &*archive_map)
160 Ok(h) if h.magic.get(object::endian::BigEndian) == object::macho::FAT_MAGIC_64 => {
161 let archs = object::macho::FatHeader::parse_arch64(&*archive_map);
162 try_filter_fat_archs(archs, target_arch, archive_path, &*archive_map)
164 // Not a FatHeader at all, just return None.
169 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
173 mut skip: Box<dyn FnMut(&str) -> bool + 'static>,
174 ) -> io::Result<()> {
175 let mut archive_path = archive_path.to_path_buf();
176 if self.sess.target.llvm_target.contains("-apple-macosx") {
177 if let Some(new_archive_path) =
178 try_extract_macho_fat_archive(&self.sess, &archive_path)?
180 archive_path = new_archive_path
184 if self.src_archives.iter().any(|archive| archive.0 == archive_path) {
188 let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? };
189 let archive = ArchiveFile::parse(&*archive_map)
190 .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
191 let archive_index = self.src_archives.len();
193 for entry in archive.members() {
194 let entry = entry.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
195 let file_name = String::from_utf8(entry.name().to_vec())
196 .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
197 if !skip(&file_name) {
199 file_name.into_bytes(),
200 ArchiveEntry::FromArchive { archive_index, file_range: entry.file_range() },
205 self.src_archives.push((archive_path.to_owned(), archive_map));
209 /// Adds an arbitrary file to this archive
210 fn add_file(&mut self, file: &Path) {
212 file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(),
213 ArchiveEntry::File(file.to_owned()),
217 /// Combine the provided files, rlibs, and native libraries into a single
219 fn build(self: Box<Self>, output: &Path) -> bool {
220 let sess = self.sess;
221 match self.build_inner(output) {
222 Ok(any_members) => any_members,
223 Err(e) => sess.emit_fatal(ArchiveBuildFailure { error: e }),
228 impl<'a> ArArchiveBuilder<'a> {
229 fn build_inner(self, output: &Path) -> io::Result<bool> {
230 let archive_kind = match &*self.sess.target.archive_format {
231 "gnu" => ArchiveKind::Gnu,
232 "bsd" => ArchiveKind::Bsd,
233 "darwin" => ArchiveKind::Darwin,
234 "coff" => ArchiveKind::Coff,
236 self.sess.emit_fatal(UnknownArchiveKind { kind });
240 let mut entries = Vec::new();
242 for (entry_name, entry) in self.entries {
245 ArchiveEntry::FromArchive { archive_index, file_range } => {
246 let src_archive = &self.src_archives[archive_index];
248 let data = &src_archive.1
249 [file_range.0 as usize..file_range.0 as usize + file_range.1 as usize];
251 Box::new(data) as Box<dyn AsRef<[u8]>>
253 ArchiveEntry::File(file) => unsafe {
255 Mmap::map(File::open(file).map_err(|err| {
256 io_error_context("failed to open object file", err)
258 .map_err(|err| io_error_context("failed to map object file", err))?,
259 ) as Box<dyn AsRef<[u8]>>
263 entries.push(NewArchiveMember {
265 get_symbols: self.get_object_symbols,
266 member_name: String::from_utf8(entry_name).unwrap(),
274 let mut w = File::create(output)
275 .map_err(|err| io_error_context("failed to create archive file", err))?;
277 write_archive_to_stream(&mut w, &entries, true, archive_kind, true, false)?;
279 Ok(!entries.is_empty())
283 fn io_error_context(context: &str, err: io::Error) -> io::Error {
284 io::Error::new(io::ErrorKind::Other, format!("{context}: {err}"))