Emit DW_AT_frame_base debuginfo attribute

This commit is contained in:
bjorn3 2024-03-27 10:57:13 +00:00
parent cfd7325f20
commit 09ec683ae7

View File

@ -8,10 +8,10 @@
use cranelift_codegen::ir::Endianness; use cranelift_codegen::ir::Endianness;
use cranelift_codegen::isa::TargetIsa; use cranelift_codegen::isa::TargetIsa;
use gimli::write::{ use gimli::write::{
Address, AttributeValue, DwarfUnit, FileId, LineProgram, LineString, Range, RangeList, Address, AttributeValue, DwarfUnit, Expression, FileId, LineProgram, LineString, Range,
UnitEntryId, RangeList, UnitEntryId,
}; };
use gimli::{Encoding, Format, LineEncoding, RunTimeEndian}; use gimli::{AArch64, Encoding, Format, LineEncoding, Register, RiscV, RunTimeEndian, X86_64};
use indexmap::IndexSet; use indexmap::IndexSet;
use rustc_session::Session; use rustc_session::Session;
@ -28,6 +28,7 @@ pub(crate) struct DebugContext {
dwarf: DwarfUnit, dwarf: DwarfUnit,
unit_range_list: RangeList, unit_range_list: RangeList,
stack_pointer_register: Register,
should_remap_filepaths: bool, should_remap_filepaths: bool,
} }
@ -60,6 +61,15 @@ pub(crate) fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa) -> Self {
Endianness::Big => RunTimeEndian::Big, Endianness::Big => RunTimeEndian::Big,
}; };
let stack_pointer_register = match isa.triple().architecture {
target_lexicon::Architecture::Aarch64(_) => AArch64::SP,
target_lexicon::Architecture::Riscv64(_) => RiscV::SP,
target_lexicon::Architecture::X86_64 | target_lexicon::Architecture::X86_64h => {
X86_64::RSP
}
_ => Register(u16::MAX),
};
let mut dwarf = DwarfUnit::new(encoding); let mut dwarf = DwarfUnit::new(encoding);
let should_remap_filepaths = tcx.sess.should_prefer_remapped_for_codegen(); let should_remap_filepaths = tcx.sess.should_prefer_remapped_for_codegen();
@ -111,6 +121,7 @@ pub(crate) fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa) -> Self {
endian, endian,
dwarf, dwarf,
unit_range_list: RangeList(Vec::new()), unit_range_list: RangeList(Vec::new()),
stack_pointer_register,
should_remap_filepaths, should_remap_filepaths,
} }
} }
@ -135,6 +146,10 @@ pub(crate) fn define_function(
entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id)); entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id));
entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(name_id)); entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(name_id));
let mut frame_base_expr = Expression::new();
frame_base_expr.op_reg(self.stack_pointer_register);
entry.set(gimli::DW_AT_frame_base, AttributeValue::Exprloc(frame_base_expr));
entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id))); entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id)));
entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(line)); entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(line));
entry.set(gimli::DW_AT_decl_column, AttributeValue::Udata(column)); entry.set(gimli::DW_AT_decl_column, AttributeValue::Udata(column));