2020-09-23 15:13:49 +02:00
|
|
|
//! Creation of ar archives like for the lib and staticlib crate type
|
|
|
|
|
2020-07-11 21:19:10 +02:00
|
|
|
use std::collections::BTreeMap;
|
2018-11-09 18:38:30 +01:00
|
|
|
use std::fs::File;
|
2021-09-19 13:56:58 +02:00
|
|
|
use std::io::{self, Read, Seek};
|
2019-04-01 19:34:26 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-11-09 18:38:30 +01:00
|
|
|
|
2021-09-01 14:38:37 +02:00
|
|
|
use rustc_codegen_ssa::back::archive::ArchiveBuilder;
|
2020-08-28 12:10:48 +02:00
|
|
|
use rustc_session::Session;
|
2019-04-01 19:34:26 +02:00
|
|
|
|
2021-09-19 13:56:58 +02:00
|
|
|
use object::read::archive::ArchiveFile;
|
2021-12-20 18:56:35 +01:00
|
|
|
use object::{Object, ObjectSymbol, ReadCache};
|
2020-07-11 21:19:10 +02:00
|
|
|
|
2019-04-16 19:07:30 +02:00
|
|
|
#[derive(Debug)]
|
2019-04-16 18:41:07 +02:00
|
|
|
enum ArchiveEntry {
|
2021-09-19 13:56:58 +02:00
|
|
|
FromArchive { archive_index: usize, file_range: (u64, u64) },
|
2019-08-18 16:06:59 +02:00
|
|
|
File(PathBuf),
|
2019-04-16 18:41:07 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) struct ArArchiveBuilder<'a> {
|
2020-08-08 16:19:42 +02:00
|
|
|
sess: &'a Session,
|
|
|
|
use_gnu_style_archive: bool,
|
|
|
|
no_builtin_ranlib: bool,
|
|
|
|
|
2021-09-19 13:56:58 +02:00
|
|
|
src_archives: Vec<File>,
|
2019-04-27 17:47:15 +02:00
|
|
|
// Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
|
|
|
|
// the end of an archive for linkers to not get confused.
|
2021-09-19 13:56:58 +02:00
|
|
|
entries: Vec<(Vec<u8>, ArchiveEntry)>,
|
2018-11-09 18:38:30 +01:00
|
|
|
}
|
|
|
|
|
2019-04-01 19:34:26 +02:00
|
|
|
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
2022-07-28 08:39:19 +00:00
|
|
|
fn new(sess: &'a Session) -> Self {
|
2019-04-01 19:34:26 +02:00
|
|
|
ArArchiveBuilder {
|
2020-08-08 16:19:42 +02:00
|
|
|
sess,
|
2020-11-08 14:27:51 +03:00
|
|
|
use_gnu_style_archive: sess.target.archive_format == "gnu",
|
2020-08-08 16:19:42 +02:00
|
|
|
// FIXME fix builtin ranlib on macOS
|
2020-11-08 14:27:51 +03:00
|
|
|
no_builtin_ranlib: sess.target.is_like_osx,
|
2020-08-08 16:19:42 +02:00
|
|
|
|
2022-06-14 15:16:51 +00:00
|
|
|
src_archives: vec![],
|
|
|
|
entries: vec![],
|
2018-11-09 18:38:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:34:26 +02:00
|
|
|
fn add_file(&mut self, file: &Path) {
|
2019-04-27 17:47:15 +02:00
|
|
|
self.entries.push((
|
2021-09-19 13:56:58 +02:00
|
|
|
file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(),
|
2019-08-18 16:06:59 +02:00
|
|
|
ArchiveEntry::File(file.to_owned()),
|
2019-04-27 17:47:15 +02:00
|
|
|
));
|
2019-04-01 19:34:26 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 14:38:37 +02:00
|
|
|
fn add_archive<F>(&mut self, archive_path: &Path, mut skip: F) -> std::io::Result<()>
|
|
|
|
where
|
|
|
|
F: FnMut(&str) -> bool + 'static,
|
|
|
|
{
|
2021-09-19 13:56:58 +02:00
|
|
|
let read_cache = ReadCache::new(std::fs::File::open(&archive_path)?);
|
|
|
|
let archive = ArchiveFile::parse(&read_cache).unwrap();
|
2021-09-01 14:38:37 +02:00
|
|
|
let archive_index = self.src_archives.len();
|
2019-04-01 19:34:26 +02:00
|
|
|
|
2021-09-19 13:56:58 +02:00
|
|
|
for entry in archive.members() {
|
|
|
|
let entry = entry.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
|
|
|
|
let file_name = String::from_utf8(entry.name().to_vec())
|
|
|
|
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
|
2021-09-01 14:38:37 +02:00
|
|
|
if !skip(&file_name) {
|
2021-09-19 13:56:58 +02:00
|
|
|
self.entries.push((
|
|
|
|
file_name.into_bytes(),
|
|
|
|
ArchiveEntry::FromArchive { archive_index, file_range: entry.file_range() },
|
|
|
|
));
|
2019-04-01 19:34:26 +02:00
|
|
|
}
|
2021-09-01 14:38:37 +02:00
|
|
|
}
|
2019-04-01 19:34:26 +02:00
|
|
|
|
2021-09-19 13:56:58 +02:00
|
|
|
self.src_archives.push(read_cache.into_inner());
|
2021-09-01 14:38:37 +02:00
|
|
|
Ok(())
|
2019-04-01 19:34:26 +02:00
|
|
|
}
|
|
|
|
|
2022-07-28 08:39:19 +00:00
|
|
|
fn build(mut self, output: &Path) -> bool {
|
2020-07-11 21:19:10 +02:00
|
|
|
enum BuilderKind {
|
2019-04-16 19:07:30 +02:00
|
|
|
Bsd(ar::Builder<File>),
|
|
|
|
Gnu(ar::GnuBuilder<File>),
|
|
|
|
}
|
|
|
|
|
2020-08-08 16:19:42 +02:00
|
|
|
let sess = self.sess;
|
2020-08-08 16:14:11 +02:00
|
|
|
|
2020-07-11 21:19:10 +02:00
|
|
|
let mut symbol_table = BTreeMap::new();
|
2019-04-16 18:41:07 +02:00
|
|
|
|
2020-07-11 21:19:10 +02:00
|
|
|
let mut entries = Vec::new();
|
|
|
|
|
2022-07-25 16:07:57 +02:00
|
|
|
for (mut entry_name, entry) in self.entries {
|
2020-07-11 21:19:10 +02:00
|
|
|
// FIXME only read the symbol table of the object files to avoid having to keep all
|
|
|
|
// object files in memory at once, or read them twice.
|
|
|
|
let data = match entry {
|
2021-09-19 13:56:58 +02:00
|
|
|
ArchiveEntry::FromArchive { archive_index, file_range } => {
|
2020-07-11 21:19:10 +02:00
|
|
|
// FIXME read symbols from symtab
|
2021-09-19 13:56:58 +02:00
|
|
|
let src_read_cache = &mut self.src_archives[archive_index];
|
|
|
|
|
|
|
|
src_read_cache.seek(io::SeekFrom::Start(file_range.0)).unwrap();
|
|
|
|
let mut data = std::vec::from_elem(0, usize::try_from(file_range.1).unwrap());
|
|
|
|
src_read_cache.read_exact(&mut data).unwrap();
|
|
|
|
|
2020-07-11 21:19:10 +02:00
|
|
|
data
|
|
|
|
}
|
2020-08-28 12:10:48 +02:00
|
|
|
ArchiveEntry::File(file) => std::fs::read(file).unwrap_or_else(|err| {
|
|
|
|
sess.fatal(&format!(
|
|
|
|
"error while reading object file during archive building: {}",
|
|
|
|
err
|
|
|
|
));
|
|
|
|
}),
|
2020-07-11 21:19:10 +02:00
|
|
|
};
|
|
|
|
|
2020-08-08 16:19:42 +02:00
|
|
|
if !self.no_builtin_ranlib {
|
2022-07-25 16:07:57 +02:00
|
|
|
if symbol_table.contains_key(&entry_name) {
|
|
|
|
// The ar crate can't handle creating a symbol table in case of multiple archive
|
|
|
|
// members with the same name. Work around this by prepending a number until we
|
|
|
|
// get a unique name.
|
|
|
|
for i in 1.. {
|
|
|
|
let new_name = format!("{}_", i)
|
|
|
|
.into_bytes()
|
|
|
|
.into_iter()
|
|
|
|
.chain(entry_name.iter().copied())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if !symbol_table.contains_key(&new_name) {
|
|
|
|
entry_name = new_name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-27 13:08:14 +02:00
|
|
|
match object::File::parse(&*data) {
|
2020-07-11 21:19:10 +02:00
|
|
|
Ok(object) => {
|
2020-08-28 12:10:48 +02:00
|
|
|
symbol_table.insert(
|
2021-09-19 13:56:58 +02:00
|
|
|
entry_name.to_vec(),
|
2020-08-28 12:10:48 +02:00
|
|
|
object
|
|
|
|
.symbols()
|
2020-11-27 20:48:53 +01:00
|
|
|
.filter_map(|symbol| {
|
2021-12-20 18:56:35 +01:00
|
|
|
if symbol.is_undefined() || symbol.is_local() {
|
2020-08-28 12:10:48 +02:00
|
|
|
None
|
|
|
|
} else {
|
2020-11-27 20:48:53 +01:00
|
|
|
symbol.name().map(|name| name.as_bytes().to_vec()).ok()
|
2020-08-28 12:10:48 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
);
|
2020-07-11 21:19:10 +02:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let err = err.to_string();
|
|
|
|
if err == "Unknown file magic" {
|
|
|
|
// Not an object file; skip it.
|
|
|
|
} else {
|
2020-08-28 12:10:48 +02:00
|
|
|
sess.fatal(&format!(
|
|
|
|
"error parsing `{}` during archive creation: {}",
|
2021-09-19 13:56:58 +02:00
|
|
|
String::from_utf8_lossy(&entry_name),
|
|
|
|
err
|
2020-08-28 12:10:48 +02:00
|
|
|
));
|
2019-08-18 16:06:59 +02:00
|
|
|
}
|
2019-04-16 19:07:30 +02:00
|
|
|
}
|
2019-04-16 18:41:07 +02:00
|
|
|
}
|
2020-07-11 21:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
entries.push((entry_name, data));
|
|
|
|
}
|
|
|
|
|
2020-08-08 16:19:42 +02:00
|
|
|
let mut builder = if self.use_gnu_style_archive {
|
2020-08-28 12:10:48 +02:00
|
|
|
BuilderKind::Gnu(
|
|
|
|
ar::GnuBuilder::new(
|
2022-07-28 08:39:19 +00:00
|
|
|
File::create(output).unwrap_or_else(|err| {
|
2020-08-28 12:10:48 +02:00
|
|
|
sess.fatal(&format!(
|
|
|
|
"error opening destination during archive building: {}",
|
|
|
|
err
|
|
|
|
));
|
|
|
|
}),
|
2021-09-19 13:56:58 +02:00
|
|
|
entries.iter().map(|(name, _)| name.clone()).collect(),
|
2020-08-28 12:10:48 +02:00
|
|
|
ar::GnuSymbolTableFormat::Size32,
|
|
|
|
symbol_table,
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
2020-07-11 21:19:10 +02:00
|
|
|
} else {
|
2020-08-28 12:10:48 +02:00
|
|
|
BuilderKind::Bsd(
|
|
|
|
ar::Builder::new(
|
2022-07-28 08:39:19 +00:00
|
|
|
File::create(output).unwrap_or_else(|err| {
|
2020-08-28 12:10:48 +02:00
|
|
|
sess.fatal(&format!(
|
|
|
|
"error opening destination during archive building: {}",
|
|
|
|
err
|
|
|
|
));
|
|
|
|
}),
|
|
|
|
symbol_table,
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
2020-07-11 21:19:10 +02:00
|
|
|
};
|
|
|
|
|
2022-06-18 17:55:24 +00:00
|
|
|
let any_members = !entries.is_empty();
|
|
|
|
|
2020-07-11 21:19:10 +02:00
|
|
|
// Add all files
|
|
|
|
for (entry_name, data) in entries.into_iter() {
|
2021-09-19 13:56:58 +02:00
|
|
|
let header = ar::Header::new(entry_name, data.len() as u64);
|
2020-07-11 21:19:10 +02:00
|
|
|
match builder {
|
2020-08-28 12:10:48 +02:00
|
|
|
BuilderKind::Bsd(ref mut builder) => builder.append(&header, &mut &*data).unwrap(),
|
|
|
|
BuilderKind::Gnu(ref mut builder) => builder.append(&header, &mut &*data).unwrap(),
|
2018-11-09 18:38:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize archive
|
2019-04-16 18:41:07 +02:00
|
|
|
std::mem::drop(builder);
|
2018-11-09 18:38:30 +01:00
|
|
|
|
2020-08-08 16:19:42 +02:00
|
|
|
if self.no_builtin_ranlib {
|
|
|
|
let ranlib = crate::toolchain::get_toolchain_binary(self.sess, "ranlib");
|
2020-07-09 18:55:46 +02:00
|
|
|
|
2020-06-16 11:40:21 +02:00
|
|
|
// Run ranlib to be able to link the archive
|
2020-07-09 18:55:46 +02:00
|
|
|
let status = std::process::Command::new(ranlib)
|
2022-07-28 08:39:19 +00:00
|
|
|
.arg(output)
|
2020-06-16 11:40:21 +02:00
|
|
|
.status()
|
|
|
|
.expect("Couldn't run ranlib");
|
|
|
|
|
|
|
|
if !status.success() {
|
2021-03-05 19:12:59 +01:00
|
|
|
self.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
|
2020-06-16 11:40:21 +02:00
|
|
|
}
|
2020-04-14 21:36:28 +05:30
|
|
|
}
|
2022-06-18 17:55:24 +00:00
|
|
|
|
|
|
|
any_members
|
2018-11-09 18:38:30 +01:00
|
|
|
}
|
2021-03-08 12:42:54 -08:00
|
|
|
|
2022-07-01 13:01:41 -07:00
|
|
|
fn create_dll_import_lib(
|
|
|
|
_sess: &Session,
|
2021-03-08 12:42:54 -08:00
|
|
|
_lib_name: &str,
|
2020-11-14 03:02:03 +01:00
|
|
|
_dll_imports: &[rustc_session::cstore::DllImport],
|
2022-07-01 13:01:41 -07:00
|
|
|
_tmpdir: &Path,
|
|
|
|
) -> PathBuf {
|
|
|
|
bug!("creating dll imports is not supported");
|
2021-03-08 12:42:54 -08:00
|
|
|
}
|
2018-11-09 18:38:30 +01:00
|
|
|
}
|