rust/src/metadata.rs

119 lines
4.0 KiB
Rust
Raw Normal View History

2019-03-27 19:24:42 +01:00
use std::fs::File;
use std::path::Path;
2019-08-31 22:58:09 +05:30
use rustc::middle::cstore::{EncodedMetadata, MetadataLoader};
2019-03-27 19:24:42 +01:00
use rustc::session::config;
use rustc::ty::TyCtxt;
use rustc_codegen_ssa::METADATA_FILENAME;
use rustc_data_structures::owning_ref::{self, OwningRef};
2018-11-17 18:23:52 +01:00
use rustc_data_structures::rustc_erase_owner;
2019-03-27 19:24:42 +01:00
use rustc_target::spec::Target;
2018-08-15 12:07:08 +02:00
pub struct CraneliftMetadataLoader;
impl MetadataLoader for CraneliftMetadataLoader {
fn get_rlib_metadata(
&self,
2019-03-27 19:24:42 +01:00
_target: &Target,
2018-08-15 12:07:08 +02:00
path: &Path,
) -> Result<owning_ref::ErasedBoxRef<[u8]>, String> {
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() {
2018-08-15 12:07:08 +02:00
let mut buf = Vec::new();
::std::io::copy(&mut entry, &mut buf).map_err(|e| format!("{:?}", e))?;
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into();
return Ok(rustc_erase_owner!(buf.map_owner_box()));
}
}
Err("couldn't find metadata entry".to_string())
//self.get_dylib_metadata(target, path)
}
fn get_dylib_metadata(
&self,
2019-03-27 19:24:42 +01:00
_target: &Target,
path: &Path,
2018-08-15 12:07:08 +02:00
) -> Result<owning_ref::ErasedBoxRef<[u8]>, String> {
2019-03-27 19:24:42 +01:00
use object::Object;
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
.section_data_by_name(".rustc")
.ok_or("no .rustc section")?
.into_owned();
2019-03-27 19:24:42 +01:00
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into();
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
2019-08-31 22:58:09 +05:30
pub fn write_metadata(tcx: TyCtxt<'_>, artifact: &mut faerie::Artifact) -> EncodedMetadata {
2019-03-27 19:24:42 +01:00
use flate2::write::DeflateEncoder;
2019-08-31 22:58:09 +05:30
use flate2::Compression;
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
.borrow()
.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();
DeflateEncoder::new(&mut compressed, Compression::fast())
2019-08-31 22:58:09 +05:30
.write_all(&metadata.raw_data)
.unwrap();
2019-03-27 19:24:42 +01:00
2019-08-31 22:58:09 +05:30
artifact
.declare(".rustc", faerie::Decl::section(faerie::SectionKind::Data))
.unwrap();
artifact
.define_with_symbols(".rustc", compressed, {
let mut map = std::collections::BTreeMap::new();
// FIXME implement faerie elf backend section custom symbols
// For MachO this is necessary to prevent the linker from throwing away the .rustc section,
// but for ELF it isn't.
if tcx.sess.target.target.options.is_like_osx {
map.insert(
rustc::middle::exported_symbols::metadata_symbol_name(tcx),
0,
);
}
map
})
.unwrap();
2019-03-27 19:24:42 +01:00
metadata
}