2020-09-23 15:13:49 +02:00
|
|
|
//! Reading and writing of the rustc metadata for rlibs and dylibs
|
|
|
|
|
2019-03-27 19:24:42 +01:00
|
|
|
use std::fs::File;
|
|
|
|
use std::path::Path;
|
|
|
|
|
2019-04-20 17:35:58 +02:00
|
|
|
use rustc_codegen_ssa::METADATA_FILENAME;
|
2021-03-29 11:18:52 +02:00
|
|
|
use rustc_data_structures::memmap::Mmap;
|
|
|
|
use rustc_data_structures::owning_ref::OwningRef;
|
2018-11-17 18:23:52 +01:00
|
|
|
use rustc_data_structures::rustc_erase_owner;
|
2020-10-15 16:15:04 +02:00
|
|
|
use rustc_data_structures::sync::MetadataRef;
|
2020-08-28 12:10:48 +02:00
|
|
|
use rustc_middle::middle::cstore::{EncodedMetadata, MetadataLoader};
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
|
|
|
use rustc_session::config;
|
2019-03-27 19:24:42 +01:00
|
|
|
use rustc_target::spec::Target;
|
2018-08-15 12:07:08 +02:00
|
|
|
|
2019-10-16 21:21:20 +02:00
|
|
|
use crate::backend::WriteMetadata;
|
2019-10-16 20:48:09 +02:00
|
|
|
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) struct CraneliftMetadataLoader;
|
2018-08-15 12:07:08 +02:00
|
|
|
|
2021-03-29 10:45:09 +02:00
|
|
|
fn load_metadata_with(
|
|
|
|
path: &Path,
|
|
|
|
f: impl for<'a> FnOnce(&'a [u8]) -> Result<&'a [u8], String>,
|
|
|
|
) -> Result<MetadataRef, String> {
|
|
|
|
let file = File::open(path).map_err(|e| format!("{:?}", e))?;
|
2021-03-29 11:18:52 +02:00
|
|
|
let data = unsafe { Mmap::map(file) }.map_err(|e| format!("{:?}", e))?;
|
|
|
|
let metadata = OwningRef::new(data).try_map(f)?;
|
2021-03-29 10:45:09 +02:00
|
|
|
return Ok(rustc_erase_owner!(metadata.map_owner_box()));
|
|
|
|
}
|
|
|
|
|
2018-08-15 12:07:08 +02:00
|
|
|
impl MetadataLoader for CraneliftMetadataLoader {
|
2020-10-15 16:15:04 +02:00
|
|
|
fn get_rlib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
|
2021-03-29 10:45:09 +02:00
|
|
|
load_metadata_with(path, |data| {
|
|
|
|
let archive = object::read::archive::ArchiveFile::parse(&*data)
|
|
|
|
.map_err(|e| format!("{:?}", e))?;
|
|
|
|
|
|
|
|
for entry_result in archive.members() {
|
|
|
|
let entry = entry_result.map_err(|e| format!("{:?}", e))?;
|
|
|
|
if entry.name() == METADATA_FILENAME.as_bytes() {
|
|
|
|
return Ok(entry.data());
|
|
|
|
}
|
2018-08-15 12:07:08 +02:00
|
|
|
}
|
|
|
|
|
2021-03-29 10:45:09 +02:00
|
|
|
Err("couldn't find metadata entry".to_string())
|
|
|
|
})
|
2018-08-15 12:07:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 16:15:04 +02:00
|
|
|
fn get_dylib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
|
2020-03-24 13:17:03 +01:00
|
|
|
use object::{Object, ObjectSection};
|
2021-03-29 10:45:09 +02:00
|
|
|
|
|
|
|
load_metadata_with(path, |data| {
|
|
|
|
let file = object::File::parse(&data).map_err(|e| format!("parse: {:?}", e))?;
|
|
|
|
file.section_by_name(".rustc")
|
|
|
|
.ok_or("no .rustc section")?
|
|
|
|
.data()
|
|
|
|
.map_err(|e| format!("failed to read .rustc section: {:?}", e))
|
|
|
|
})
|
2018-08-15 12:07:08 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-27 19:24:42 +01:00
|
|
|
|
|
|
|
// Adapted from https://github.com/rust-lang/rust/blob/da573206f87b5510de4b0ee1a9c044127e409bd3/src/librustc_codegen_llvm/base.rs#L47-L112
|
2020-08-28 12:10:48 +02:00
|
|
|
pub(crate) fn write_metadata<P: WriteMetadata>(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
product: &mut P,
|
|
|
|
) -> EncodedMetadata {
|
2020-09-05 11:00:34 +02:00
|
|
|
use snap::write::FrameEncoder;
|
2019-08-31 22:58:09 +05:30
|
|
|
use std::io::Write;
|
2019-03-27 19:24:42 +01:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
enum MetadataKind {
|
|
|
|
None,
|
|
|
|
Uncompressed,
|
2019-08-31 22:58:09 +05:30
|
|
|
Compressed,
|
2019-03-27 19:24:42 +01:00
|
|
|
}
|
|
|
|
|
2019-08-31 22:58:09 +05:30
|
|
|
let kind = tcx
|
|
|
|
.sess
|
2020-05-18 11:35:23 +02:00
|
|
|
.crate_types()
|
2019-08-31 22:58:09 +05:30
|
|
|
.iter()
|
|
|
|
.map(|ty| match *ty {
|
|
|
|
config::CrateType::Executable
|
|
|
|
| config::CrateType::Staticlib
|
|
|
|
| config::CrateType::Cdylib => MetadataKind::None,
|
2019-03-27 19:24:42 +01:00
|
|
|
|
|
|
|
config::CrateType::Rlib => MetadataKind::Uncompressed,
|
|
|
|
|
2019-08-31 22:58:09 +05:30
|
|
|
config::CrateType::Dylib | config::CrateType::ProcMacro => MetadataKind::Compressed,
|
|
|
|
})
|
|
|
|
.max()
|
|
|
|
.unwrap_or(MetadataKind::None);
|
2019-03-27 19:24:42 +01:00
|
|
|
|
|
|
|
if kind == MetadataKind::None {
|
|
|
|
return EncodedMetadata::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
let metadata = tcx.encode_metadata();
|
|
|
|
if kind == MetadataKind::Uncompressed {
|
|
|
|
return metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(kind == MetadataKind::Compressed);
|
|
|
|
let mut compressed = tcx.metadata_encoding_version();
|
2021-03-05 19:12:59 +01:00
|
|
|
FrameEncoder::new(&mut compressed).write_all(&metadata.raw_data).unwrap();
|
2019-03-27 19:24:42 +01:00
|
|
|
|
2019-10-16 20:48:09 +02:00
|
|
|
product.add_rustc_section(
|
2020-03-31 13:20:19 +02:00
|
|
|
rustc_middle::middle::exported_symbols::metadata_symbol_name(tcx),
|
2019-10-16 20:48:09 +02:00
|
|
|
compressed,
|
2020-11-08 14:27:51 +03:00
|
|
|
tcx.sess.target.is_like_osx,
|
2019-10-16 20:48:09 +02:00
|
|
|
);
|
2019-03-27 19:24:42 +01:00
|
|
|
|
|
|
|
metadata
|
|
|
|
}
|