]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs
Auto merge of #101629 - compiler-errors:issue-101623, r=sanxiyn
[rust.git] / compiler / rustc_codegen_cranelift / src / debuginfo / mod.rs
1 //! Handling of everything related to debuginfo.
2
3 mod emit;
4 mod line_info;
5 mod object;
6 mod unwind;
7
8 use crate::prelude::*;
9
10 use cranelift_codegen::ir::Endianness;
11 use cranelift_codegen::isa::TargetIsa;
12
13 use gimli::write::{
14     Address, AttributeValue, DwarfUnit, FileId, LineProgram, LineString, Range, RangeList,
15     UnitEntryId,
16 };
17 use gimli::{Encoding, Format, LineEncoding, RunTimeEndian};
18 use indexmap::IndexSet;
19
20 pub(crate) use emit::{DebugReloc, DebugRelocName};
21 pub(crate) use unwind::UnwindContext;
22
23 pub(crate) struct DebugContext {
24     endian: RunTimeEndian,
25
26     dwarf: DwarfUnit,
27     unit_range_list: RangeList,
28 }
29
30 pub(crate) struct FunctionDebugContext {
31     entry_id: UnitEntryId,
32     function_source_loc: (FileId, u64, u64),
33     source_loc_set: indexmap::IndexSet<(FileId, u64, u64)>,
34 }
35
36 impl DebugContext {
37     pub(crate) fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa) -> Self {
38         let encoding = Encoding {
39             format: Format::Dwarf32,
40             // FIXME this should be configurable
41             // macOS doesn't seem to support DWARF > 3
42             // 5 version is required for md5 file hash
43             version: if tcx.sess.target.is_like_osx {
44                 3
45             } else {
46                 // FIXME change to version 5 once the gdb and lldb shipping with the latest debian
47                 // support it.
48                 4
49             },
50             address_size: isa.frontend_config().pointer_bytes(),
51         };
52
53         let endian = match isa.endianness() {
54             Endianness::Little => RunTimeEndian::Little,
55             Endianness::Big => RunTimeEndian::Big,
56         };
57
58         let mut dwarf = DwarfUnit::new(encoding);
59
60         let producer = format!(
61             "cg_clif (rustc {}, cranelift {})",
62             rustc_interface::util::version_str().unwrap_or("unknown version"),
63             cranelift_codegen::VERSION,
64         );
65         let comp_dir = tcx
66             .sess
67             .opts
68             .working_dir
69             .to_string_lossy(FileNameDisplayPreference::Remapped)
70             .into_owned();
71         let (name, file_info) = match tcx.sess.local_crate_source_file.clone() {
72             Some(path) => {
73                 let name = path.to_string_lossy().into_owned();
74                 (name, None)
75             }
76             None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
77         };
78
79         let mut line_program = LineProgram::new(
80             encoding,
81             LineEncoding::default(),
82             LineString::new(comp_dir.as_bytes(), encoding, &mut dwarf.line_strings),
83             LineString::new(name.as_bytes(), encoding, &mut dwarf.line_strings),
84             file_info,
85         );
86         line_program.file_has_md5 = file_info.is_some();
87
88         dwarf.unit.line_program = line_program;
89
90         {
91             let name = dwarf.strings.add(name);
92             let comp_dir = dwarf.strings.add(comp_dir);
93
94             let root = dwarf.unit.root();
95             let root = dwarf.unit.get_mut(root);
96             root.set(gimli::DW_AT_producer, AttributeValue::StringRef(dwarf.strings.add(producer)));
97             root.set(gimli::DW_AT_language, AttributeValue::Language(gimli::DW_LANG_Rust));
98             root.set(gimli::DW_AT_name, AttributeValue::StringRef(name));
99             root.set(gimli::DW_AT_comp_dir, AttributeValue::StringRef(comp_dir));
100             root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
101         }
102
103         DebugContext { endian, dwarf, unit_range_list: RangeList(Vec::new()) }
104     }
105
106     pub(crate) fn define_function(
107         &mut self,
108         tcx: TyCtxt<'_>,
109         name: &str,
110         function_span: Span,
111     ) -> FunctionDebugContext {
112         let (file, line, column) = DebugContext::get_span_loc(tcx, function_span, function_span);
113
114         let file_id = self.add_source_file(&file);
115
116         // FIXME: add to appropriate scope instead of root
117         let scope = self.dwarf.unit.root();
118
119         let entry_id = self.dwarf.unit.add(scope, gimli::DW_TAG_subprogram);
120         let entry = self.dwarf.unit.get_mut(entry_id);
121         let name_id = self.dwarf.strings.add(name);
122         // Gdb requires DW_AT_name. Otherwise the DW_TAG_subprogram is skipped.
123         entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id));
124         entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(name_id));
125
126         entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id)));
127         entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(line));
128         entry.set(gimli::DW_AT_decl_column, AttributeValue::Udata(column));
129
130         FunctionDebugContext {
131             entry_id,
132             function_source_loc: (file_id, line, column),
133             source_loc_set: IndexSet::new(),
134         }
135     }
136 }
137
138 impl FunctionDebugContext {
139     pub(crate) fn finalize(
140         mut self,
141         debug_context: &mut DebugContext,
142         func_id: FuncId,
143         context: &Context,
144     ) {
145         let symbol = func_id.as_u32() as usize;
146
147         let end = self.create_debug_lines(debug_context, symbol, context);
148
149         debug_context.unit_range_list.0.push(Range::StartLength {
150             begin: Address::Symbol { symbol, addend: 0 },
151             length: u64::from(end),
152         });
153
154         let func_entry = debug_context.dwarf.unit.get_mut(self.entry_id);
155         // Gdb requires both DW_AT_low_pc and DW_AT_high_pc. Otherwise the DW_TAG_subprogram is skipped.
156         func_entry.set(
157             gimli::DW_AT_low_pc,
158             AttributeValue::Address(Address::Symbol { symbol, addend: 0 }),
159         );
160         // Using Udata for DW_AT_high_pc requires at least DWARF4
161         func_entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(end)));
162     }
163 }