rust/src/metadata.rs

109 lines
3.7 KiB
Rust
Raw Normal View History

//! Reading and writing of the rustc metadata for rlibs and dylibs
use std::convert::TryFrom;
2019-03-27 19:24:42 +01:00
use std::fs::File;
use std::path::Path;
use rustc_codegen_ssa::METADATA_FILENAME;
use rustc_data_structures::owning_ref::OwningRef;
2018-11-17 18:23:52 +01:00
use rustc_data_structures::rustc_erase_owner;
use rustc_data_structures::sync::MetadataRef;
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
use crate::backend::WriteMetadata;
pub(crate) struct CraneliftMetadataLoader;
2018-08-15 12:07:08 +02:00
impl MetadataLoader for CraneliftMetadataLoader {
fn get_rlib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
2018-08-15 12:07:08 +02:00
let mut archive = ar::Archive::new(File::open(path).map_err(|e| format!("{:?}", e))?);
// Iterate over all entries in the archive:
while let Some(entry_result) = archive.next_entry() {
let mut entry = entry_result.map_err(|e| format!("{:?}", e))?;
if entry.header().identifier() == METADATA_FILENAME.as_bytes() {
let mut buf = Vec::with_capacity(
usize::try_from(entry.header().size())
.expect("Rlib metadata file too big to load into memory."),
);
2018-08-15 12:07:08 +02:00
::std::io::copy(&mut entry, &mut buf).map_err(|e| format!("{:?}", e))?;
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf);
2018-08-15 12:07:08 +02:00
return Ok(rustc_erase_owner!(buf.map_owner_box()));
}
}
Err("couldn't find metadata entry".to_string())
}
fn get_dylib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
2020-03-24 13:17:03 +01:00
use object::{Object, ObjectSection};
2019-03-27 19:24:42 +01:00
let file = std::fs::read(path).map_err(|e| format!("read:{:?}", e))?;
let file = object::File::parse(&file).map_err(|e| format!("parse: {:?}", e))?;
2019-08-31 22:58:09 +05:30
let buf = file
2020-03-24 13:17:03 +01:00
.section_by_name(".rustc")
2019-08-31 22:58:09 +05:30
.ok_or("no .rustc section")?
2020-03-24 13:17:03 +01:00
.data()
.map_err(|e| format!("failed to read .rustc section: {:?}", e))?
.to_owned();
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf);
2019-03-27 19:24:42 +01:00
Ok(rustc_erase_owner!(buf.map_owner_box()))
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
pub(crate) fn write_metadata<P: WriteMetadata>(
tcx: TyCtxt<'_>,
product: &mut P,
) -> EncodedMetadata {
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
.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();
FrameEncoder::new(&mut compressed)
2019-08-31 22:58:09 +05:30
.write_all(&metadata.raw_data)
.unwrap();
2019-03-27 19:24:42 +01:00
product.add_rustc_section(
rustc_middle::middle::exported_symbols::metadata_symbol_name(tcx),
compressed,
tcx.sess.target.is_like_osx,
);
2019-03-27 19:24:42 +01:00
metadata
}