1 //! Creation of ar archives like for the lib and staticlib crate type
3 use std::collections::BTreeMap;
4 use std::convert::TryFrom;
6 use std::io::{self, Read, Seek};
7 use std::path::{Path, PathBuf};
9 use rustc_codegen_ssa::back::archive::{find_library, ArchiveBuilder};
10 use rustc_codegen_ssa::METADATA_FILENAME;
11 use rustc_session::Session;
13 use object::read::archive::ArchiveFile;
14 use object::{Object, ObjectSymbol, ReadCache, SymbolKind};
18 FromArchive { archive_index: usize, file_range: (u64, u64) },
22 pub(crate) struct ArArchiveBuilder<'a> {
25 lib_search_paths: Vec<PathBuf>,
26 use_gnu_style_archive: bool,
27 no_builtin_ranlib: bool,
29 src_archives: Vec<File>,
30 // Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
31 // the end of an archive for linkers to not get confused.
32 entries: Vec<(String, ArchiveEntry)>,
35 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
36 fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self {
37 use rustc_codegen_ssa::back::link::archive_search_paths;
39 let (src_archives, entries) = if let Some(input) = input {
40 let read_cache = ReadCache::new(File::open(input).unwrap());
41 let archive = ArchiveFile::parse(&read_cache).unwrap();
42 let mut entries = Vec::new();
44 for entry in archive.members() {
45 let entry = entry.unwrap();
47 String::from_utf8(entry.name().to_vec()).unwrap(),
48 ArchiveEntry::FromArchive { archive_index: 0, file_range: entry.file_range() },
52 (vec![read_cache.into_inner()], entries)
59 dst: output.to_path_buf(),
60 lib_search_paths: archive_search_paths(sess),
61 use_gnu_style_archive: sess.target.archive_format == "gnu",
62 // FIXME fix builtin ranlib on macOS
63 no_builtin_ranlib: sess.target.is_like_osx,
70 fn src_files(&mut self) -> Vec<String> {
71 self.entries.iter().map(|(name, _)| name.clone()).collect()
74 fn remove_file(&mut self, name: &str) {
78 .position(|(entry_name, _)| entry_name == name)
79 .expect("Tried to remove file not existing in src archive");
80 self.entries.remove(index);
83 fn add_file(&mut self, file: &Path) {
85 file.file_name().unwrap().to_str().unwrap().to_string(),
86 ArchiveEntry::File(file.to_owned()),
90 fn add_native_library(&mut self, name: rustc_span::symbol::Symbol, verbatim: bool) {
91 let location = find_library(name, verbatim, &self.lib_search_paths, self.sess);
92 self.add_archive(location.clone(), |_| false).unwrap_or_else(|e| {
93 panic!("failed to add native library {}: {}", location.to_string_lossy(), e);
103 ) -> io::Result<()> {
104 let obj_start = name.to_owned();
106 self.add_archive(rlib.to_owned(), move |fname: &str| {
107 // Ignore metadata files, no matter the name.
108 if fname == METADATA_FILENAME {
112 // Don't include Rust objects if LTO is enabled
113 if lto && fname.starts_with(&obj_start) && fname.ends_with(".o") {
117 // Otherwise if this is *not* a rust object and we're skipping
118 // objects then skip this file
119 if skip_objects && (!fname.starts_with(&obj_start) || !fname.ends_with(".o")) {
123 // ok, don't skip this
128 fn update_symbols(&mut self) {}
132 Bsd(ar::Builder<File>),
133 Gnu(ar::GnuBuilder<File>),
136 let sess = self.sess;
138 let mut symbol_table = BTreeMap::new();
140 let mut entries = Vec::new();
142 for (entry_name, entry) in self.entries {
143 // FIXME only read the symbol table of the object files to avoid having to keep all
144 // object files in memory at once, or read them twice.
145 let data = match entry {
146 ArchiveEntry::FromArchive { archive_index, file_range } => {
147 // FIXME read symbols from symtab
148 let src_read_cache = &mut self.src_archives[archive_index];
150 src_read_cache.seek(io::SeekFrom::Start(file_range.0)).unwrap();
151 let mut data = std::vec::from_elem(0, usize::try_from(file_range.1).unwrap());
152 src_read_cache.read_exact(&mut data).unwrap();
156 ArchiveEntry::File(file) => std::fs::read(file).unwrap_or_else(|err| {
158 "error while reading object file during archive building: {}",
164 if !self.no_builtin_ranlib {
165 match object::File::parse(&*data) {
168 entry_name.as_bytes().to_vec(),
171 .filter_map(|symbol| {
172 if symbol.is_undefined()
174 || symbol.kind() != SymbolKind::Data
175 && symbol.kind() != SymbolKind::Text
176 && symbol.kind() != SymbolKind::Tls
180 symbol.name().map(|name| name.as_bytes().to_vec()).ok()
183 .collect::<Vec<_>>(),
187 let err = err.to_string();
188 if err == "Unknown file magic" {
189 // Not an object file; skip it.
192 "error parsing `{}` during archive creation: {}",
200 entries.push((entry_name, data));
203 let mut builder = if self.use_gnu_style_archive {
206 File::create(&self.dst).unwrap_or_else(|err| {
208 "error opening destination during archive building: {}",
212 entries.iter().map(|(name, _)| name.as_bytes().to_vec()).collect(),
213 ar::GnuSymbolTableFormat::Size32,
221 File::create(&self.dst).unwrap_or_else(|err| {
223 "error opening destination during archive building: {}",
234 for (entry_name, data) in entries.into_iter() {
235 let header = ar::Header::new(entry_name.into_bytes(), data.len() as u64);
237 BuilderKind::Bsd(ref mut builder) => builder.append(&header, &mut &*data).unwrap(),
238 BuilderKind::Gnu(ref mut builder) => builder.append(&header, &mut &*data).unwrap(),
243 std::mem::drop(builder);
245 if self.no_builtin_ranlib {
246 let ranlib = crate::toolchain::get_toolchain_binary(self.sess, "ranlib");
248 // Run ranlib to be able to link the archive
249 let status = std::process::Command::new(ranlib)
252 .expect("Couldn't run ranlib");
254 if !status.success() {
255 self.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
260 fn inject_dll_import_lib(
263 _dll_imports: &[rustc_middle::middle::cstore::DllImport],
264 _tmpdir: &rustc_data_structures::temp_dir::MaybeTempDir,
266 bug!("injecting dll imports is not supported");
270 impl<'a> ArArchiveBuilder<'a> {
271 fn add_archive<F>(&mut self, archive_path: PathBuf, mut skip: F) -> io::Result<()>
273 F: FnMut(&str) -> bool + 'static,
275 let read_cache = ReadCache::new(std::fs::File::open(&archive_path)?);
276 let archive = ArchiveFile::parse(&read_cache).unwrap();
277 let archive_index = self.src_archives.len();
279 for entry in archive.members() {
280 let entry = entry.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
281 let file_name = String::from_utf8(entry.name().to_vec())
282 .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
283 if !skip(&file_name) {
286 ArchiveEntry::FromArchive { archive_index, file_range: entry.file_range() },
291 self.src_archives.push(read_cache.into_inner());