Skip to main content

rustc_codegen_ssa/back/
rmeta_link.rs

1//! Late-metadata archive member that lists which rlib entries are Rust object files,
2//! and potentially other data collected and used when building or linking a rlib.
3//! See <https://github.com/rust-lang/rust/issues/138243>.
4
5use std::path::Path;
6
7use object::read::archive::ArchiveFile;
8use rustc_serialize::opaque::mem_encoder::MemEncoder;
9use rustc_serialize::opaque::{MAGIC_END_BYTES, MemDecoder};
10use rustc_serialize::{Decodable, Encodable};
11
12use super::metadata::search_for_section;
13
14pub(crate) const FILENAME: &str = "lib.rmeta-link";
15pub(crate) const SECTION: &str = ".rmeta-link";
16
17pub struct RmetaLink {
18    pub rust_object_files: Vec<String>,
19}
20
21impl RmetaLink {
22    pub(crate) fn encode(&self) -> Vec<u8> {
23        let mut encoder = MemEncoder::new();
24        self.rust_object_files.encode(&mut encoder);
25        let mut data = encoder.finish();
26        data.extend_from_slice(MAGIC_END_BYTES);
27        data
28    }
29
30    pub(crate) fn decode(data: &[u8]) -> Option<RmetaLink> {
31        let mut decoder = MemDecoder::new(data, 0).ok()?;
32        let rust_object_files = Vec::<String>::decode(&mut decoder);
33        Some(RmetaLink { rust_object_files })
34    }
35}
36
37/// Reads the link-time metadata from an already-parsed archive.
38pub fn read(archive: &ArchiveFile<'_>, archive_data: &[u8], rlib_path: &Path) -> Option<RmetaLink> {
39    for entry in archive.members() {
40        let entry = entry.ok()?;
41        if entry.name() == FILENAME.as_bytes() {
42            let data = entry.data(archive_data).ok()?;
43            let section_data = search_for_section(rlib_path, data, SECTION).ok()?;
44            return RmetaLink::decode(section_data);
45        }
46    }
47    None
48}
49
50/// Like [`read`], but parses the archive from raw bytes.
51///
52/// Use this when the caller's `ArchiveFile` comes from a different version of the `object` crate.
53pub fn read_from_data(archive_data: &[u8], rlib_path: &Path) -> Option<RmetaLink> {
54    let archive = ArchiveFile::parse(archive_data).ok()?;
55    read(&archive, archive_data, rlib_path)
56}