Skip to content

Commit 3db2996

Browse files
committed
WIP
1 parent 2c9139d commit 3db2996

File tree

7 files changed

+81
-68
lines changed

7 files changed

+81
-68
lines changed

Cargo.toml

+8-9
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ crate-type = ["rlib", "dylib"]
1313
# These have to be in sync with each other
1414
cranelift = { git = "https://github.com/CraneStation/cranelift.git" }
1515
cranelift-module = { git = "https://github.com/CraneStation/cranelift.git" }
16-
cranelift-faerie = { git = "https://github.com/CraneStation/cranelift.git" }
16+
cranelift-object = { git = "https://github.com/CraneStation/cranelift.git" }
1717
target-lexicon = "0.4.0"
18-
faerie = "0.10.0"
1918

2019
#goblin = "0.0.17"
2120
ar = "0.7.0"
@@ -28,11 +27,11 @@ indexmap = "1.0.2"
2827
object = "0.12.0"
2928

3029
# Uncomment to use local checkout of cranelift
31-
#[patch."https://github.com/CraneStation/cranelift.git"]
32-
#cranelift = { path = "../cranelift/cranelift-umbrella" }
33-
#cranelift-module = { path = "../cranelift/cranelift-module" }
34-
#cranelift-simplejit = { path = "../cranelift/cranelift-simplejit" }
35-
#cranelift-faerie = { path = "../cranelift/cranelift-faerie" }
30+
[patch."https://github.com/CraneStation/cranelift.git"]
31+
cranelift = { path = "../cranelift/cranelift-umbrella" }
32+
cranelift-module = { path = "../cranelift/cranelift-module" }
33+
cranelift-simplejit = { path = "../cranelift/cranelift-simplejit" }
34+
cranelift-object = { path = "../cranelift/cranelift-object" }
3635

3736
#[patch."https://github.com/gimli-rs/gimli.git"]
3837
#gimli = { path = "../" }
@@ -41,8 +40,8 @@ object = "0.12.0"
4140
cranelift-simplejit = { git = "https://github.com/CraneStation/cranelift.git" }
4241

4342
[patch.crates-io]
44-
faerie = { git = "https://github.com/m4b/faerie.git" }
45-
object = { git = "https://github.com/gimli-rs/object.git" }
43+
#object = { git = "https://github.com/gimli-rs/object.git" }
44+
object = { path = "../object", features = ["write"] }
4645

4746
[profile.dev.overrides."*"]
4847
opt-level = 3

build_sysroot/build_sysroot.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
set -x
23
set -e
34
cd $(dirname "$0")
45

src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
9494
let mut debug_context = cx
9595
.debug_context
9696
.as_mut()
97-
.map(|debug_context| FunctionDebugContext::new(tcx, debug_context, mir, &name, &sig));
97+
.map(|debug_context| FunctionDebugContext::new(tcx, debug_context, mir, func_id, &name, &sig));
9898

9999
// Make FunctionBuilder
100100
let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);

src/debuginfo.rs

+33-30
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use gimli::write::{
1010
};
1111
use gimli::{Encoding, Format, LineEncoding, RunTimeEndian, SectionId};
1212

13-
use faerie::*;
13+
use object::{SectionKind, RelocationKind, RelocationEncoding};
14+
use object::write::*;
15+
use cranelift_object::*;
1416

1517
fn target_endian(tcx: TyCtxt) -> RunTimeEndian {
1618
use rustc::ty::layout::Endian;
@@ -73,18 +75,9 @@ enum DebugRelocName {
7375
Symbol(usize),
7476
}
7577

76-
impl DebugReloc {
77-
fn name<'a>(&self, ctx: &'a DebugContext) -> &'a str {
78-
match self.name {
79-
DebugRelocName::Section(id) => id.name(),
80-
DebugRelocName::Symbol(index) => ctx.symbols.get_index(index).unwrap(),
81-
}
82-
}
83-
}
84-
8578
pub struct DebugContext<'tcx> {
8679
endian: RunTimeEndian,
87-
symbols: indexmap::IndexSet<String>,
80+
symbols: indexmap::IndexSet<FuncId>,
8881

8982
dwarf: DwarfUnit,
9083
unit_range_list: RangeList,
@@ -181,7 +174,7 @@ impl<'tcx> DebugContext<'tcx> {
181174
);
182175
}
183176

184-
pub fn emit(&mut self, artifact: &mut Artifact) {
177+
pub fn emit(&mut self, product: &mut ObjectProduct) {
185178
let unit_range_list_id = self.dwarf.unit.ranges.add(self.unit_range_list.clone());
186179
let root = self.dwarf.unit.root();
187180
let root = self.dwarf.unit.get_mut(root);
@@ -193,30 +186,39 @@ impl<'tcx> DebugContext<'tcx> {
193186
let mut sections = Sections::new(WriterRelocate::new(self));
194187
self.dwarf.write(&mut sections).unwrap();
195188

189+
let mut map = HashMap::new();
196190
let _: Result<()> = sections.for_each_mut(|id, section| {
197191
if !section.writer.slice().is_empty() {
198-
artifact
199-
.declare_with(id.name(), Decl::section(SectionKind::Debug), section.writer.take())
200-
.unwrap();
192+
let segment = product.object.segment_name(StandardSegment::Debug).to_vec();
193+
let name = id.name().as_bytes().to_vec();
194+
let data = section.writer.take();
195+
let section_id = product.object.add_section(segment, name, SectionKind::Debug);
196+
product.object.section_mut(section_id).set_data(data, 1);
197+
let symbol_id = product.object.section_symbol(section_id);
198+
map.insert(id, (section_id, symbol_id));
201199
}
202200
Ok(())
203201
});
204202

205203
let _: Result<()> = sections.for_each(|id, section| {
206-
for reloc in &section.relocs {
207-
artifact
208-
.link_with(
209-
faerie::Link {
210-
from: id.name(),
211-
to: reloc.name(self),
212-
at: u64::from(reloc.offset),
213-
},
214-
faerie::Reloc::Debug {
215-
size: reloc.size,
216-
addend: reloc.addend as i32,
217-
},
218-
)
219-
.expect("faerie relocation error");
204+
if let Some((section_id, _)) = map.get(&id) {
205+
for reloc in &section.relocs {
206+
let symbol = match reloc.name {
207+
DebugRelocName::Section(id) => map.get(&id).unwrap().1,
208+
DebugRelocName::Symbol(id) => {
209+
let func_id = *self.symbols.get_index(id).unwrap();
210+
product.function_symbol(func_id)
211+
}
212+
};
213+
product.object.add_relocation(*section_id, Relocation {
214+
offset: u64::from(reloc.offset),
215+
symbol,
216+
kind: RelocationKind::Absolute,
217+
encoding: RelocationEncoding::Generic,
218+
size: reloc.size * 8,
219+
addend: reloc.addend,
220+
}).unwrap();
221+
}
220222
}
221223
Ok(())
222224
});
@@ -235,10 +237,11 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
235237
tcx: TyCtxt<'tcx>,
236238
debug_context: &'a mut DebugContext<'tcx>,
237239
mir: &Body,
240+
func_id: FuncId,
238241
name: &str,
239242
_sig: &Signature,
240243
) -> Self {
241-
let (symbol, _) = debug_context.symbols.insert_full(name.to_string());
244+
let (symbol, _) = debug_context.symbols.insert_full(func_id);
242245

243246
// FIXME: add to appropriate scope intead of root
244247
let scope = debug_context.dwarf.unit.root();

src/driver.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::session::config::{DebugInfo, OutputType};
99
use rustc_codegen_ssa::back::linker::LinkerInfo;
1010
use rustc_codegen_ssa::CrateInfo;
1111

12-
use cranelift_faerie::*;
12+
use cranelift_object::*;
1313

1414
use crate::prelude::*;
1515

@@ -102,11 +102,11 @@ fn run_aot(
102102
log: &mut Option<File>,
103103
) -> Box<CodegenResults> {
104104
let new_module = |name: String| {
105-
let module: Module<FaerieBackend> = Module::new(
106-
FaerieBuilder::new(
105+
let module: Module<ObjectBackend> = Module::new(
106+
ObjectBuilder::new(
107107
crate::build_isa(tcx.sess),
108108
name + ".o",
109-
FaerieTrapCollection::Disabled,
109+
ObjectTrapCollection::Disabled,
110110
cranelift_module::default_libcall_names(),
111111
)
112112
.unwrap(),
@@ -117,19 +117,19 @@ fn run_aot(
117117

118118
let emit_module = |name: &str,
119119
kind: ModuleKind,
120-
mut module: Module<FaerieBackend>,
120+
mut module: Module<ObjectBackend>,
121121
debug: Option<DebugContext>| {
122122
module.finalize_definitions();
123-
let mut artifact = module.finish().artifact;
123+
let mut product = module.finish();
124124

125125
if let Some(mut debug) = debug {
126-
debug.emit(&mut artifact);
126+
debug.emit(&mut product);
127127
}
128128

129129
let tmp_file = tcx
130130
.output_filenames(LOCAL_CRATE)
131131
.temp_path(OutputType::Object, Some(name));
132-
let obj = artifact.emit().unwrap();
132+
let obj = product.emit().unwrap();
133133
std::fs::write(&tmp_file, obj).unwrap();
134134
CompiledModule {
135135
name: name.to_string(),
@@ -140,22 +140,22 @@ fn run_aot(
140140
}
141141
};
142142

143-
let mut faerie_module = new_module("some_file".to_string());
143+
let mut object_module = new_module("some_file".to_string());
144144

145145
let mut debug = if tcx.sess.opts.debuginfo != DebugInfo::None
146146
// macOS debuginfo doesn't work yet (see #303)
147147
&& !tcx.sess.target.target.options.is_like_osx
148148
{
149149
let debug = DebugContext::new(
150150
tcx,
151-
faerie_module.target_config().pointer_type().bytes() as u8,
151+
object_module.target_config().pointer_type().bytes() as u8,
152152
);
153153
Some(debug)
154154
} else {
155155
None
156156
};
157157

158-
codegen_cgus(tcx, &mut faerie_module, &mut debug, log);
158+
codegen_cgus(tcx, &mut object_module, &mut debug, log);
159159

160160
tcx.sess.abort_if_errors();
161161

@@ -175,15 +175,17 @@ fn run_aot(
175175
.as_str()
176176
.to_string();
177177

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);
178+
let triple = crate::build_isa(tcx.sess).triple().clone();
179+
let mut metadata_object =
180+
object::write::Object::new(triple.binary_format, triple.architecture);
181+
metadata_object.add_file_symbol(metadata_cgu_name.as_bytes().to_vec());
182+
crate::metadata::write_metadata(tcx, &mut metadata_object);
181183

182184
let tmp_file = tcx
183185
.output_filenames(LOCAL_CRATE)
184186
.temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
185187

186-
let obj = metadata_artifact.emit().unwrap();
188+
let obj = metadata_object.write().unwrap();
187189
std::fs::write(&tmp_file, obj).unwrap();
188190

189191
Some(CompiledModule {
@@ -202,7 +204,7 @@ fn run_aot(
202204
modules: vec![emit_module(
203205
"dummy_name",
204206
ModuleKind::Regular,
205-
faerie_module,
207+
object_module,
206208
debug,
207209
)],
208210
allocator_module: if created_alloc_shim {

src/metadata.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl MetadataLoader for CraneliftMetadataLoader {
5050
// Adapted from https://github.com/rust-lang/rust/blob/da573206f87b5510de4b0ee1a9c044127e409bd3/src/librustc_codegen_llvm/base.rs#L47-L112
5151
pub fn write_metadata(
5252
tcx: TyCtxt<'_>,
53-
artifact: &mut faerie::Artifact
53+
object: &mut object::write::Object
5454
) -> EncodedMetadata {
5555
use std::io::Write;
5656
use flate2::Compression;
@@ -90,17 +90,23 @@ pub fn write_metadata(
9090
DeflateEncoder::new(&mut compressed, Compression::fast())
9191
.write_all(&metadata.raw_data).unwrap();
9292

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();
93+
let segment = object.segment_name(object::write::StandardSegment::Data).to_vec();
94+
let section_id = object.add_section(segment, b".rustc".to_vec(), object::SectionKind::Data);
95+
let offset = object.append_section_data(section_id, &compressed, 1);
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+
object.add_symbol(object::write::Symbol {
101+
name: rustc::middle::exported_symbols::metadata_symbol_name(tcx).into_bytes(),
102+
value: offset,
103+
size: compressed.len() as u64,
104+
kind: object::SymbolKind::Data,
105+
scope: object::SymbolScope::Compilation,
106+
weak: false,
107+
section: Some(section_id),
108+
});
109+
}
104110

105111
metadata
106112
}

test.sh

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/bash
22
source config.sh
33

4+
set -x
5+
46
rm -r target/out || true
57
mkdir -p target/out/clif
68

0 commit comments

Comments
 (0)