Skip to content

Commit db85194

Browse files
authored
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

+115-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5
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

+1-1
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

+1-1
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

+33-8
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

+1
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;

0 commit comments

Comments
 (0)