Skip to content

Commit f0dde6c

Browse files
committed
Make dylib metadata write backend agnostic
1 parent 0934dc8 commit f0dde6c

File tree

5 files changed

+75
-20
lines changed

5 files changed

+75
-20
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ crate-type = ["dylib"]
1414
cranelift = { git = "https://github.com/CraneStation/cranelift.git" }
1515
cranelift-module = { git = "https://github.com/CraneStation/cranelift.git" }
1616
cranelift-faerie = { git = "https://github.com/CraneStation/cranelift.git" }
17+
cranelift-object = { git = "https://github.com/CraneStation/cranelift.git" }
1718
target-lexicon = "0.8.1"
1819
faerie = "0.11.0"
1920

@@ -28,14 +29,15 @@ libloading = "0.5.1"
2829
[dependencies.object]
2930
version = "0.14.0"
3031
default-features = false
31-
features = ["compression", "read", "std"] # We don't need WASM support
32+
features = ["compression", "read", "std", "write"] # We don't need WASM support
3233

3334
# Uncomment to use local checkout of cranelift
3435
#[patch."https://github.com/CraneStation/cranelift.git"]
3536
#cranelift = { path = "../cranelift/cranelift-umbrella" }
3637
#cranelift-module = { path = "../cranelift/cranelift-module" }
3738
#cranelift-simplejit = { path = "../cranelift/cranelift-simplejit" }
3839
#cranelift-faerie = { path = "../cranelift/cranelift-faerie" }
40+
#cranelift-object = { path = "../cranelift/cranelift-object" }
3941

4042
#[patch.crates-io]
4143
#gimli = { path = "../" }

src/backend.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
pub trait Product {
2+
fn add_rustc_section(&mut self, symbol_name: String, data: Vec<u8>, is_like_osx: bool);
3+
}
4+
5+
impl Product for faerie::Artifact {
6+
fn add_rustc_section(&mut self, symbol_name: String, data: Vec<u8>, is_like_osx: bool) {
7+
self
8+
.declare(".rustc", faerie::Decl::section(faerie::SectionKind::Data))
9+
.unwrap();
10+
self
11+
.define_with_symbols(".rustc", data, {
12+
let mut map = std::collections::BTreeMap::new();
13+
// FIXME implement faerie elf backend section custom symbols
14+
// For MachO this is necessary to prevent the linker from throwing away the .rustc section,
15+
// but for ELF it isn't.
16+
if is_like_osx {
17+
map.insert(
18+
symbol_name,
19+
0,
20+
);
21+
}
22+
map
23+
})
24+
.unwrap();
25+
}
26+
}
27+
28+
impl Product for object::write::Object {
29+
fn add_rustc_section(&mut self, symbol_name: String, data: Vec<u8>, is_like_osx: bool) {
30+
let segment = self.segment_name(object::write::StandardSegment::Data).to_vec();
31+
let section_id = self.add_section(segment, b".rustc".to_vec(), object::SectionKind::Data);
32+
let offset = self.append_section_data(section_id, &data, 1);
33+
// FIXME implement faerie elf backend section custom symbols
34+
// For MachO this is necessary to prevent the linker from throwing away the .rustc section,
35+
// but for ELF it isn't.
36+
if is_like_osx {
37+
self.add_symbol(object::write::Symbol {
38+
name: symbol_name.into_bytes(),
39+
value: offset,
40+
size: data.len() as u64,
41+
kind: object::SymbolKind::Data,
42+
scope: object::SymbolScope::Compilation,
43+
weak: false,
44+
section: Some(section_id),
45+
});
46+
}
47+
}
48+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ mod allocator;
3434
mod analyze;
3535
mod archive;
3636
mod base;
37+
mod backend;
3738
mod cast;
3839
mod codegen_i128;
3940
mod common;

src/metadata.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use rustc_data_structures::owning_ref::{self, OwningRef};
99
use rustc_data_structures::rustc_erase_owner;
1010
use rustc_target::spec::Target;
1111

12+
use crate::backend::Product;
13+
1214
pub struct CraneliftMetadataLoader;
1315

1416
impl MetadataLoader for CraneliftMetadataLoader {
@@ -51,7 +53,7 @@ impl MetadataLoader for CraneliftMetadataLoader {
5153
}
5254

5355
// Adapted from https://github.com/rust-lang/rust/blob/da573206f87b5510de4b0ee1a9c044127e409bd3/src/librustc_codegen_llvm/base.rs#L47-L112
54-
pub fn write_metadata(tcx: TyCtxt<'_>, artifact: &mut faerie::Artifact) -> EncodedMetadata {
56+
pub fn write_metadata<P: Product>(tcx: TyCtxt<'_>, product: &mut P) -> EncodedMetadata {
5557
use flate2::write::DeflateEncoder;
5658
use flate2::Compression;
5759
use std::io::Write;
@@ -95,24 +97,11 @@ pub fn write_metadata(tcx: TyCtxt<'_>, artifact: &mut faerie::Artifact) -> Encod
9597
.write_all(&metadata.raw_data)
9698
.unwrap();
9799

98-
artifact
99-
.declare(".rustc", faerie::Decl::section(faerie::SectionKind::Data))
100-
.unwrap();
101-
artifact
102-
.define_with_symbols(".rustc", compressed, {
103-
let mut map = std::collections::BTreeMap::new();
104-
// FIXME implement faerie elf backend section custom symbols
105-
// For MachO this is necessary to prevent the linker from throwing away the .rustc section,
106-
// but for ELF it isn't.
107-
if tcx.sess.target.target.options.is_like_osx {
108-
map.insert(
109-
rustc::middle::exported_symbols::metadata_symbol_name(tcx),
110-
0,
111-
);
112-
}
113-
map
114-
})
115-
.unwrap();
100+
product.add_rustc_section(
101+
rustc::middle::exported_symbols::metadata_symbol_name(tcx),
102+
compressed,
103+
tcx.sess.target.target.options.is_like_osx,
104+
);
116105

117106
metadata
118107
}

0 commit comments

Comments
 (0)