Skip to content

Commit a5da29c

Browse files
committed
Auto merge of #83640 - bjorn3:shared_metadata_reader, r=nagisa
Use the object crate for metadata reading This allows sharing the metadata reader between cg_llvm, cg_clif and other codegen backends. This is not currently useful for rlib reading with cg_spirv ([rust-gpu](https://github.com/EmbarkStudios/rust-gpu/)) as it uses tar rather than ar as .rlib format, but it is useful for dylib reading required for loading proc macros. (cc `@eddyb)` The object crate is already trusted as dependency of libstd through backtrace. As far as I know it supports reading all object file formats used by targets for which we support rust dylibs with crate metadata, but I am not certain. If this happens to not be the case, I could keep using LLVM for reading dylib metadata. Marked as WIP for a perf run and as it is based on #83637.
2 parents d342a34 + 62f64af commit a5da29c

File tree

2 files changed

+2
-66
lines changed

2 files changed

+2
-66
lines changed

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
165165
}
166166

167167
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync> {
168-
Box::new(crate::metadata::CraneliftMetadataLoader)
168+
Box::new(rustc_codegen_ssa::back::metadata::DefaultMetadataLoader)
169169
}
170170

171171
fn provide(&self, _providers: &mut Providers) {}

src/metadata.rs

+1-65
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,9 @@
1-
//! Reading and writing of the rustc metadata for rlibs and dylibs
1+
//! Writing of the rustc metadata for dylibs
22
3-
use std::fs::File;
4-
use std::path::Path;
5-
6-
use rustc_codegen_ssa::METADATA_FILENAME;
7-
use rustc_data_structures::memmap::Mmap;
8-
use rustc_data_structures::owning_ref::OwningRef;
9-
use rustc_data_structures::rustc_erase_owner;
10-
use rustc_data_structures::sync::MetadataRef;
11-
use rustc_middle::middle::cstore::MetadataLoader;
123
use rustc_middle::ty::TyCtxt;
13-
use rustc_target::spec::Target;
144

155
use crate::backend::WriteMetadata;
166

17-
/// The metadata loader used by cg_clif.
18-
///
19-
/// The metadata is stored in the same format as cg_llvm.
20-
///
21-
/// # Metadata location
22-
///
23-
/// <dl>
24-
/// <dt>rlib</dt>
25-
/// <dd>The metadata can be found in the `lib.rmeta` file inside of the ar archive.</dd>
26-
/// <dt>dylib</dt>
27-
/// <dd>The metadata can be found in the `.rustc` section of the shared library.</dd>
28-
/// </dl>
29-
pub(crate) struct CraneliftMetadataLoader;
30-
31-
fn load_metadata_with(
32-
path: &Path,
33-
f: impl for<'a> FnOnce(&'a [u8]) -> Result<&'a [u8], String>,
34-
) -> Result<MetadataRef, String> {
35-
let file = File::open(path).map_err(|e| format!("{:?}", e))?;
36-
let data = unsafe { Mmap::map(file) }.map_err(|e| format!("{:?}", e))?;
37-
let metadata = OwningRef::new(data).try_map(f)?;
38-
return Ok(rustc_erase_owner!(metadata.map_owner_box()));
39-
}
40-
41-
impl MetadataLoader for CraneliftMetadataLoader {
42-
fn get_rlib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
43-
load_metadata_with(path, |data| {
44-
let archive = object::read::archive::ArchiveFile::parse(&*data)
45-
.map_err(|e| format!("{:?}", e))?;
46-
47-
for entry_result in archive.members() {
48-
let entry = entry_result.map_err(|e| format!("{:?}", e))?;
49-
if entry.name() == METADATA_FILENAME.as_bytes() {
50-
return Ok(entry.data());
51-
}
52-
}
53-
54-
Err("couldn't find metadata entry".to_string())
55-
})
56-
}
57-
58-
fn get_dylib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
59-
use object::{Object, ObjectSection};
60-
61-
load_metadata_with(path, |data| {
62-
let file = object::File::parse(&data).map_err(|e| format!("parse: {:?}", e))?;
63-
file.section_by_name(".rustc")
64-
.ok_or("no .rustc section")?
65-
.data()
66-
.map_err(|e| format!("failed to read .rustc section: {:?}", e))
67-
})
68-
}
69-
}
70-
717
// Adapted from https://github.com/rust-lang/rust/blob/da573206f87b5510de4b0ee1a9c044127e409bd3/src/librustc_codegen_llvm/base.rs#L47-L112
728
pub(crate) fn write_metadata<O: WriteMetadata>(tcx: TyCtxt<'_>, object: &mut O) {
739
use snap::write::FrameEncoder;

0 commit comments

Comments
 (0)