Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit db85194

Browse files
authoredJun 11, 2019
Merge pull request #435 from bjorn3/write_dylib_metadata
Read and write dylib metadata
2 parents 8fb70f2 + 44a3550 commit db85194

File tree

8 files changed

+231
-22
lines changed

8 files changed

+231
-22
lines changed
 

‎Cargo.lock

Lines changed: 115 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ libc = "0.2.53"
2525
tempfile = "3.0.7"
2626
gimli = { git = "https://github.com/gimli-rs/gimli.git" }
2727
indexmap = "1.0.2"
28+
object = "0.12.0"
2829

2930
# Uncomment to use local checkout of cranelift
3031
#[patch."https://github.com/CraneStation/cranelift.git"]
@@ -39,5 +40,9 @@ indexmap = "1.0.2"
3940
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
4041
cranelift-simplejit = { git = "https://github.com/CraneStation/cranelift.git" }
4142

43+
[patch.crates-io]
44+
faerie = { git = "https://github.com/m4b/faerie.git" }
45+
object = { git = "https://github.com/gimli-rs/object.git" }
46+
4247
[profile.dev.overrides."*"]
4348
opt-level = 3

‎config.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ fi
2020

2121
export RUSTFLAGS='-Zalways-encode-mir -Cpanic=abort -Cdebuginfo=2 -Zcodegen-backend='$(pwd)'/target/'$channel'/librustc_codegen_cranelift.'$dylib_ext' --sysroot '$(pwd)'/build_sysroot/sysroot'
2222
RUSTC="rustc $RUSTFLAGS -L crate=target/out --out-dir target/out"
23-
export RUST_LOG=warn # display metadata load errors
23+
export RUSTC_LOG=warn # display metadata load errors

‎src/debuginfo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl<'a, 'tcx: 'a> DebugContext<'tcx> {
191191
let _: Result<()> = sections.for_each_mut(|id, section| {
192192
if !section.writer.slice().is_empty() {
193193
artifact
194-
.declare_with(id.name(), Decl::debug_section(), section.writer.take())
194+
.declare_with(id.name(), Decl::section(SectionKind::Debug), section.writer.take())
195195
.unwrap();
196196
}
197197
Ok(())

‎src/driver.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn run_jit<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, log: &mut Option<File>) ->
9898
fn run_aot<'a, 'tcx: 'a>(
9999
tcx: TyCtxt<'a, 'tcx, 'tcx>,
100100
metadata: EncodedMetadata,
101-
_need_metadata_module: bool,
101+
need_metadata_module: bool,
102102
log: &mut Option<File>,
103103
) -> Box<CodegenResults> {
104104
let new_module = |name: String| {
@@ -166,6 +166,37 @@ fn run_aot<'a, 'tcx: 'a>(
166166
rustc_incremental::save_dep_graph(tcx);
167167
rustc_incremental::finalize_session_directory(tcx.sess, tcx.crate_hash(LOCAL_CRATE));
168168

169+
let metadata_module = if need_metadata_module {
170+
use rustc::mir::mono::CodegenUnitNameBuilder;
171+
172+
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
173+
let metadata_cgu_name = cgu_name_builder
174+
.build_cgu_name(LOCAL_CRATE, &["crate"], Some("metadata"))
175+
.as_str()
176+
.to_string();
177+
178+
let mut metadata_artifact =
179+
faerie::Artifact::new(crate::build_isa(tcx.sess).triple().clone(), metadata_cgu_name.clone());
180+
crate::metadata::write_metadata(tcx, &mut metadata_artifact);
181+
182+
let tmp_file = tcx
183+
.output_filenames(LOCAL_CRATE)
184+
.temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
185+
186+
let obj = metadata_artifact.emit().unwrap();
187+
std::fs::write(&tmp_file, obj).unwrap();
188+
189+
Some(CompiledModule {
190+
name: metadata_cgu_name,
191+
kind: ModuleKind::Metadata,
192+
object: Some(tmp_file),
193+
bytecode: None,
194+
bytecode_compressed: None,
195+
})
196+
} else {
197+
None
198+
};
199+
169200
Box::new(CodegenResults {
170201
crate_name: tcx.crate_name(LOCAL_CRATE),
171202
modules: vec![emit_module(
@@ -184,13 +215,7 @@ fn run_aot<'a, 'tcx: 'a>(
184215
} else {
185216
None
186217
},
187-
metadata_module: Some(CompiledModule {
188-
name: "dummy_metadata".to_string(),
189-
kind: ModuleKind::Metadata,
190-
object: None,
191-
bytecode: None,
192-
bytecode_compressed: None,
193-
}),
218+
metadata_module,
194219
crate_hash: tcx.crate_hash(LOCAL_CRATE),
195220
metadata,
196221
windows_subsystem: None, // Windows is not yet supported

‎src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(rustc_private, never_type, decl_macro)]
22
#![allow(intra_doc_link_resolution_failure)]
33

4+
extern crate flate2;
45
extern crate rustc;
56
extern crate rustc_allocator;
67
extern crate rustc_codegen_ssa;

‎src/metadata.rs

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
use rustc::middle::cstore::MetadataLoader;
1+
use std::fs::File;
2+
use std::path::Path;
3+
4+
use rustc::session::config;
5+
use rustc::ty::TyCtxt;
6+
use rustc::middle::cstore::{EncodedMetadata, MetadataLoader};
27
use rustc_codegen_ssa::METADATA_FILENAME;
38
use rustc_data_structures::owning_ref::{self, OwningRef};
49
use rustc_data_structures::rustc_erase_owner;
5-
use std::fs::File;
6-
use std::path::Path;
10+
use rustc_target::spec::Target;
711

812
pub struct CraneliftMetadataLoader;
913

1014
impl MetadataLoader for CraneliftMetadataLoader {
1115
fn get_rlib_metadata(
1216
&self,
13-
_target: &crate::rustc_target::spec::Target,
17+
_target: &Target,
1418
path: &Path,
1519
) -> Result<owning_ref::ErasedBoxRef<[u8]>, String> {
1620
let mut archive = ar::Archive::new(File::open(path).map_err(|e| format!("{:?}", e))?);
@@ -31,9 +35,72 @@ impl MetadataLoader for CraneliftMetadataLoader {
3135

3236
fn get_dylib_metadata(
3337
&self,
34-
_target: &crate::rustc_target::spec::Target,
35-
_path: &Path,
38+
_target: &Target,
39+
path: &Path,
3640
) -> Result<owning_ref::ErasedBoxRef<[u8]>, String> {
37-
Err("dylib metadata loading is not yet supported".to_string())
41+
use object::Object;
42+
let file = std::fs::read(path).map_err(|e| format!("read:{:?}", e))?;
43+
let file = object::File::parse(&file).map_err(|e| format!("parse: {:?}", e))?;
44+
let buf = file.section_data_by_name(".rustc").ok_or("no .rustc section")?.into_owned();
45+
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into();
46+
Ok(rustc_erase_owner!(buf.map_owner_box()))
47+
}
48+
}
49+
50+
// Adapted from https://github.com/rust-lang/rust/blob/da573206f87b5510de4b0ee1a9c044127e409bd3/src/librustc_codegen_llvm/base.rs#L47-L112
51+
pub fn write_metadata<'a, 'gcx>(
52+
tcx: TyCtxt<'a, 'gcx, 'gcx>,
53+
artifact: &mut faerie::Artifact
54+
) -> EncodedMetadata {
55+
use std::io::Write;
56+
use flate2::Compression;
57+
use flate2::write::DeflateEncoder;
58+
59+
#[derive(PartialEq, Eq, PartialOrd, Ord)]
60+
enum MetadataKind {
61+
None,
62+
Uncompressed,
63+
Compressed
64+
}
65+
66+
let kind = tcx.sess.crate_types.borrow().iter().map(|ty| {
67+
match *ty {
68+
config::CrateType::Executable |
69+
config::CrateType::Staticlib |
70+
config::CrateType::Cdylib => MetadataKind::None,
71+
72+
config::CrateType::Rlib => MetadataKind::Uncompressed,
73+
74+
config::CrateType::Dylib |
75+
config::CrateType::ProcMacro => MetadataKind::Compressed,
76+
}
77+
}).max().unwrap_or(MetadataKind::None);
78+
79+
if kind == MetadataKind::None {
80+
return EncodedMetadata::new();
3881
}
82+
83+
let metadata = tcx.encode_metadata();
84+
if kind == MetadataKind::Uncompressed {
85+
return metadata;
86+
}
87+
88+
assert!(kind == MetadataKind::Compressed);
89+
let mut compressed = tcx.metadata_encoding_version();
90+
DeflateEncoder::new(&mut compressed, Compression::fast())
91+
.write_all(&metadata.raw_data).unwrap();
92+
93+
artifact.declare(".rustc", faerie::Decl::section(faerie::SectionKind::Data)).unwrap();
94+
artifact.define_with_symbols(".rustc", compressed, {
95+
let mut map = std::collections::BTreeMap::new();
96+
// FIXME implement faerie elf backend section custom symbols
97+
// For MachO this is necessary to prevent the linker from throwing away the .rustc section,
98+
// but for ELF it isn't.
99+
if tcx.sess.target.target.options.is_like_osx {
100+
map.insert(rustc::middle::exported_symbols::metadata_symbol_name(tcx), 0);
101+
}
102+
map
103+
}).unwrap();
104+
105+
metadata
39106
}

‎test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ rm -r target/out || true
55
mkdir -p target/out/clif
66

77
echo "[BUILD] mini_core"
8-
$RUSTC example/mini_core.rs --crate-name mini_core --crate-type lib
8+
$RUSTC example/mini_core.rs --crate-name mini_core --crate-type dylib
99

1010
echo "[BUILD] example"
1111
$RUSTC example/example.rs --crate-type lib

0 commit comments

Comments
 (0)
Please sign in to comment.