Skip to content

Commit 7dbbfe6

Browse files
authored
Merge pull request #982 from bjorn3/backtrace_support
Implement .eh_frame writing
2 parents 9d6f65e + e7661d4 commit 7dbbfe6

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ crate-type = ["dylib"]
99

1010
[dependencies]
1111
# These have to be in sync with each other
12-
cranelift-codegen = { git = "https://github.com/bytecodealliance/wasmtime/" }
12+
cranelift-codegen = { git = "https://github.com/bytecodealliance/wasmtime/", features = ["unwind"] }
1313
cranelift-frontend = { git = "https://github.com/bytecodealliance/wasmtime/" }
1414
cranelift-module = { git = "https://github.com/bytecodealliance/wasmtime/" }
1515
cranelift-object = { git = "https://github.com/bytecodealliance/wasmtime/" }

src/backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl WriteMetadata for object::write::Object {
3939
}
4040

4141
pub(crate) trait WriteDebugInfo {
42-
type SectionId;
42+
type SectionId: Copy;
4343

4444
fn add_debug_section(&mut self, name: SectionId, data: Vec<u8>) -> Self::SectionId;
4545
fn add_debug_reloc(

src/debuginfo/emit.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_data_structures::fx::FxHashMap;
22

3-
use gimli::write::{Address, AttributeValue, EndianVec, Result, Sections, Writer};
3+
use gimli::write::{Address, AttributeValue, EhFrame, EndianVec, Result, Sections, Writer, Section};
44
use gimli::{RunTimeEndian, SectionId};
55

66
use crate::backend::WriteDebugInfo;
@@ -20,6 +20,9 @@ impl DebugContext<'_> {
2020
let mut sections = Sections::new(WriterRelocate::new(self));
2121
self.dwarf.write(&mut sections).unwrap();
2222

23+
let mut eh_frame = EhFrame::from(WriterRelocate::new(self));
24+
self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
25+
2326
let mut section_map = FxHashMap::default();
2427
let _: Result<()> = sections.for_each_mut(|id, section| {
2528
if !section.writer.slice().is_empty() {
@@ -37,6 +40,16 @@ impl DebugContext<'_> {
3740
}
3841
Ok(())
3942
});
43+
44+
if !eh_frame.0.writer.slice().is_empty() {
45+
let id = eh_frame.id();
46+
let section_id = product.add_debug_section(id, eh_frame.0.writer.into_vec());
47+
section_map.insert(id, section_id);
48+
49+
for reloc in &eh_frame.0.relocs {
50+
product.add_debug_reloc(&section_map, &self.symbols, &section_id, reloc);
51+
}
52+
}
4053
}
4154
}
4255

src/debuginfo/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod emit;
22
mod line_info;
3+
mod unwind;
34

45
use crate::prelude::*;
56

@@ -10,8 +11,8 @@ use cranelift_codegen::isa::TargetIsa;
1011
use cranelift_codegen::ValueLocRange;
1112

1213
use gimli::write::{
13-
self, Address, AttributeValue, DwarfUnit, Expression, LineProgram, LineString, Location,
14-
LocationList, Range, RangeList, UnitEntryId, Writer,
14+
self, Address, AttributeValue, CieId, DwarfUnit, Expression, FrameTable, LineProgram,
15+
LineString, Location, LocationList, Range, RangeList, UnitEntryId, Writer,
1516
};
1617
use gimli::{Encoding, Format, LineEncoding, RunTimeEndian, X86_64};
1718

@@ -34,13 +35,15 @@ pub(crate) struct DebugContext<'tcx> {
3435

3536
dwarf: DwarfUnit,
3637
unit_range_list: RangeList,
38+
frame_table: FrameTable,
3739

40+
cie: CieId,
3841
clif_types: FxHashMap<Type, UnitEntryId>,
3942
types: FxHashMap<Ty<'tcx>, UnitEntryId>,
4043
}
4144

4245
impl<'tcx> DebugContext<'tcx> {
43-
pub(crate) fn new(tcx: TyCtxt<'tcx>, address_size: u8) -> Self {
46+
pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa) -> Self {
4447
let encoding = Encoding {
4548
format: Format::Dwarf32,
4649
// TODO: this should be configurable
@@ -53,7 +56,7 @@ impl<'tcx> DebugContext<'tcx> {
5356
// support it.
5457
4
5558
},
56-
address_size,
59+
address_size: isa.frontend_config().pointer_bytes(),
5760
};
5861

5962
let mut dwarf = DwarfUnit::new(encoding);
@@ -108,6 +111,9 @@ impl<'tcx> DebugContext<'tcx> {
108111
);
109112
}
110113

114+
let mut frame_table = FrameTable::default();
115+
let cie = frame_table.add_cie(isa.create_systemv_cie().expect("SystemV unwind info CIE"));
116+
111117
DebugContext {
112118
tcx,
113119

@@ -116,7 +122,9 @@ impl<'tcx> DebugContext<'tcx> {
116122

117123
dwarf,
118124
unit_range_list: RangeList(Vec::new()),
125+
frame_table,
119126

127+
cie,
120128
clif_types: FxHashMap::default(),
121129
types: FxHashMap::default(),
122130
}
@@ -312,6 +320,8 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
312320
source_info_set: &indexmap::IndexSet<SourceInfo>,
313321
local_map: FxHashMap<mir::Local, CPlace<'tcx>>,
314322
) {
323+
self.create_unwind_info(context, isa);
324+
315325
let end = self.create_debug_lines(context, isa, source_info_set);
316326

317327
self.debug_context

src/debuginfo/unwind.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::prelude::*;
2+
3+
use cranelift_codegen::isa::unwind::UnwindInfo;
4+
5+
use gimli::write::Address;
6+
7+
impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
8+
pub(super) fn create_unwind_info(
9+
&mut self,
10+
context: &Context,
11+
isa: &dyn cranelift_codegen::isa::TargetIsa,
12+
) {
13+
let unwind_info = if let Some(unwind_info) = context.create_unwind_info(isa).unwrap() {
14+
unwind_info
15+
} else {
16+
return;
17+
};
18+
19+
match unwind_info {
20+
UnwindInfo::SystemV(unwind_info) => {
21+
self.debug_context.frame_table.add_fde(self.debug_context.cie, unwind_info.to_fde(Address::Symbol {
22+
symbol: self.symbol,
23+
addend: 0,
24+
}));
25+
},
26+
UnwindInfo::WindowsX64(_) => {
27+
// FIXME implement this
28+
}
29+
}
30+
}
31+
}

src/driver/aot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege
120120
let mut debug = if tcx.sess.opts.debuginfo != DebugInfo::None {
121121
let debug = DebugContext::new(
122122
tcx,
123-
module.target_config().pointer_type().bytes() as u8,
123+
module.isa(),
124124
);
125125
Some(debug)
126126
} else {

0 commit comments

Comments
 (0)