Skip to content

Commit 69363cf

Browse files
authoredMar 27, 2024
Merge pull request #1472 from rust-lang/debuginfo_improvements2
Add debuginfo for statics
2 parents 68b5931 + 4b80941 commit 69363cf

File tree

10 files changed

+346
-116
lines changed

10 files changed

+346
-116
lines changed
 

‎src/base.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ pub(crate) fn codegen_fn<'tcx>(
9595
caller_location: None, // set by `codegen_fn_prelude`
9696

9797
clif_comments,
98-
last_source_file: None,
9998
next_ssa_var: 0,
10099
};
101100

‎src/common.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use cranelift_codegen::isa::TargetFrontendConfig;
2-
use gimli::write::FileId;
3-
use rustc_data_structures::sync::Lrc;
42
use rustc_index::IndexVec;
53
use rustc_middle::ty::layout::{
64
FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers,
75
};
86
use rustc_span::source_map::Spanned;
9-
use rustc_span::SourceFile;
107
use rustc_target::abi::call::FnAbi;
118
use rustc_target::abi::{Integer, Primitive};
129
use rustc_target::spec::{HasTargetSpec, Target};
@@ -305,11 +302,6 @@ pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
305302

306303
pub(crate) clif_comments: crate::pretty_clif::CommentWriter,
307304

308-
/// Last accessed source file and it's debuginfo file id.
309-
///
310-
/// For optimization purposes only
311-
pub(crate) last_source_file: Option<(Lrc<SourceFile>, FileId)>,
312-
313305
/// This should only be accessed by `CPlace::new_var`.
314306
pub(crate) next_ssa_var: u32,
315307
}
@@ -419,25 +411,8 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
419411

420412
pub(crate) fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
421413
if let Some(debug_context) = &mut self.cx.debug_context {
422-
let (file, line, column) =
423-
DebugContext::get_span_loc(self.tcx, self.mir.span, source_info.span);
424-
425-
// add_source_file is very slow.
426-
// Optimize for the common case of the current file not being changed.
427-
let mut cached_file_id = None;
428-
if let Some((ref last_source_file, last_file_id)) = self.last_source_file {
429-
// If the allocations are not equal, the files may still be equal, but that
430-
// doesn't matter, as this is just an optimization.
431-
if rustc_data_structures::sync::Lrc::ptr_eq(last_source_file, &file) {
432-
cached_file_id = Some(last_file_id);
433-
}
434-
}
435-
436-
let file_id = if let Some(file_id) = cached_file_id {
437-
file_id
438-
} else {
439-
debug_context.add_source_file(&file)
440-
};
414+
let (file_id, line, column) =
415+
debug_context.get_span_loc(self.tcx, self.mir.span, source_info.span);
441416

442417
let source_loc =
443418
self.func_debug_cx.as_mut().unwrap().add_dbg_loc(file_id, line, column);

‎src/constant.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ impl ConstantCx {
3131
}
3232
}
3333

34-
pub(crate) fn codegen_static(tcx: TyCtxt<'_>, module: &mut dyn Module, def_id: DefId) {
34+
pub(crate) fn codegen_static(tcx: TyCtxt<'_>, module: &mut dyn Module, def_id: DefId) -> DataId {
3535
let mut constants_cx = ConstantCx::new();
3636
constants_cx.todo.push(TodoItem::Static(def_id));
3737
constants_cx.finalize(tcx, module);
38+
39+
data_id_for_static(
40+
tcx, module, def_id, false,
41+
// For a declaration the stated mutability doesn't matter.
42+
false,
43+
)
3844
}
3945

4046
pub(crate) fn codegen_tls_ref<'tcx>(

‎src/debuginfo/emit.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Write the debuginfo into an object file.
22
3+
use cranelift_module::{DataId, FuncId};
34
use cranelift_object::ObjectProduct;
45
use gimli::write::{Address, AttributeValue, EndianVec, Result, Sections, Writer};
56
use gimli::{RunTimeEndian, SectionId};
@@ -8,6 +9,18 @@ use rustc_data_structures::fx::FxHashMap;
89
use super::object::WriteDebugInfo;
910
use super::DebugContext;
1011

12+
pub(super) fn address_for_func(func_id: FuncId) -> Address {
13+
let symbol = func_id.as_u32();
14+
assert!(symbol & 1 << 31 == 0);
15+
Address::Symbol { symbol: symbol as usize, addend: 0 }
16+
}
17+
18+
pub(super) fn address_for_data(data_id: DataId) -> Address {
19+
let symbol = data_id.as_u32();
20+
assert!(symbol & 1 << 31 == 0);
21+
Address::Symbol { symbol: (symbol | 1 << 31) as usize, addend: 0 }
22+
}
23+
1124
impl DebugContext {
1225
pub(crate) fn emit(&mut self, product: &mut ObjectProduct) {
1326
let unit_range_list_id = self.dwarf.unit.ranges.add(self.unit_range_list.clone());
@@ -171,6 +184,7 @@ impl Writer for WriterRelocate {
171184
gimli::DW_EH_PE_pcrel => {
172185
let size = match eh_pe.format() {
173186
gimli::DW_EH_PE_sdata4 => 4,
187+
gimli::DW_EH_PE_sdata8 => 8,
174188
_ => return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)),
175189
};
176190
self.relocs.push(DebugReloc {

‎src/debuginfo/line_info.rs

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ use std::path::{Component, Path};
55

66
use cranelift_codegen::binemit::CodeOffset;
77
use cranelift_codegen::MachSrcLoc;
8-
use gimli::write::{
9-
Address, AttributeValue, FileId, FileInfo, LineProgram, LineString, LineStringTable,
10-
};
11-
use rustc_data_structures::sync::Lrc;
8+
use gimli::write::{AttributeValue, FileId, FileInfo, LineProgram, LineString, LineStringTable};
129
use rustc_span::{
1310
FileName, Pos, SourceFile, SourceFileAndLine, SourceFileHash, SourceFileHashAlgorithm,
1411
};
1512

13+
use crate::debuginfo::emit::address_for_func;
1614
use crate::debuginfo::FunctionDebugContext;
1715
use crate::prelude::*;
1816

@@ -60,72 +58,78 @@ fn make_file_info(hash: SourceFileHash) -> Option<FileInfo> {
6058

6159
impl DebugContext {
6260
pub(crate) fn get_span_loc(
61+
&mut self,
6362
tcx: TyCtxt<'_>,
6463
function_span: Span,
6564
span: Span,
66-
) -> (Lrc<SourceFile>, u64, u64) {
65+
) -> (FileId, u64, u64) {
6766
// Based on https://github.com/rust-lang/rust/blob/e369d87b015a84653343032833d65d0545fd3f26/src/librustc_codegen_ssa/mir/mod.rs#L116-L131
6867
// In order to have a good line stepping behavior in debugger, we overwrite debug
6968
// locations of macro expansions with that of the outermost expansion site (when the macro is
7069
// annotated with `#[collapse_debuginfo]` or when `-Zdebug-macros` is provided).
7170
let span = tcx.collapsed_debuginfo(span, function_span);
7271
match tcx.sess.source_map().lookup_line(span.lo()) {
7372
Ok(SourceFileAndLine { sf: file, line }) => {
73+
let file_id = self.add_source_file(&file);
7474
let line_pos = file.lines()[line];
7575
let col = file.relative_position(span.lo()) - line_pos;
7676

77-
(file, u64::try_from(line).unwrap() + 1, u64::from(col.to_u32()) + 1)
77+
(file_id, u64::try_from(line).unwrap() + 1, u64::from(col.to_u32()) + 1)
7878
}
79-
Err(file) => (file, 0, 0),
79+
Err(file) => (self.add_source_file(&file), 0, 0),
8080
}
8181
}
8282

8383
pub(crate) fn add_source_file(&mut self, source_file: &SourceFile) -> FileId {
84-
let line_program: &mut LineProgram = &mut self.dwarf.unit.line_program;
85-
let line_strings: &mut LineStringTable = &mut self.dwarf.line_strings;
86-
87-
match &source_file.name {
88-
FileName::Real(path) => {
89-
let (dir_path, file_name) =
90-
split_path_dir_and_file(if self.should_remap_filepaths {
91-
path.remapped_path_if_available()
92-
} else {
93-
path.local_path_if_available()
94-
});
95-
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
96-
let file_name = osstr_as_utf8_bytes(file_name);
97-
98-
let dir_id = if !dir_name.is_empty() {
99-
let dir_name = LineString::new(dir_name, line_program.encoding(), line_strings);
100-
line_program.add_directory(dir_name)
101-
} else {
102-
line_program.default_directory()
103-
};
104-
let file_name = LineString::new(file_name, line_program.encoding(), line_strings);
105-
106-
let info = make_file_info(source_file.src_hash);
107-
108-
line_program.file_has_md5 &= info.is_some();
109-
line_program.add_file(file_name, dir_id, info)
110-
}
111-
// FIXME give more appropriate file names
112-
filename => {
113-
let dir_id = line_program.default_directory();
114-
let dummy_file_name = LineString::new(
115-
filename
116-
.display(if self.should_remap_filepaths {
117-
FileNameDisplayPreference::Remapped
84+
let cache_key = (source_file.stable_id, source_file.src_hash);
85+
*self.created_files.entry(cache_key).or_insert_with(|| {
86+
let line_program: &mut LineProgram = &mut self.dwarf.unit.line_program;
87+
let line_strings: &mut LineStringTable = &mut self.dwarf.line_strings;
88+
89+
match &source_file.name {
90+
FileName::Real(path) => {
91+
let (dir_path, file_name) =
92+
split_path_dir_and_file(if self.should_remap_filepaths {
93+
path.remapped_path_if_available()
11894
} else {
119-
FileNameDisplayPreference::Local
120-
})
121-
.to_string()
122-
.into_bytes(),
123-
line_program.encoding(),
124-
line_strings,
125-
);
126-
line_program.add_file(dummy_file_name, dir_id, None)
95+
path.local_path_if_available()
96+
});
97+
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
98+
let file_name = osstr_as_utf8_bytes(file_name);
99+
100+
let dir_id = if !dir_name.is_empty() {
101+
let dir_name =
102+
LineString::new(dir_name, line_program.encoding(), line_strings);
103+
line_program.add_directory(dir_name)
104+
} else {
105+
line_program.default_directory()
106+
};
107+
let file_name =
108+
LineString::new(file_name, line_program.encoding(), line_strings);
109+
110+
let info = make_file_info(source_file.src_hash);
111+
112+
line_program.file_has_md5 &= info.is_some();
113+
line_program.add_file(file_name, dir_id, info)
114+
}
115+
filename => {
116+
let dir_id = line_program.default_directory();
117+
let dummy_file_name = LineString::new(
118+
filename
119+
.display(if self.should_remap_filepaths {
120+
FileNameDisplayPreference::Remapped
121+
} else {
122+
FileNameDisplayPreference::Local
123+
})
124+
.to_string()
125+
.into_bytes(),
126+
line_program.encoding(),
127+
line_strings,
128+
);
129+
line_program.add_file(dummy_file_name, dir_id, None)
130+
}
127131
}
128-
}
132+
})
129133
}
130134
}
131135

@@ -138,7 +142,7 @@ impl FunctionDebugContext {
138142
pub(super) fn create_debug_lines(
139143
&mut self,
140144
debug_context: &mut DebugContext,
141-
symbol: usize,
145+
func_id: FuncId,
142146
context: &Context,
143147
) -> CodeOffset {
144148
let create_row_for_span =
@@ -151,11 +155,7 @@ impl FunctionDebugContext {
151155
debug_context.dwarf.unit.line_program.generate_row();
152156
};
153157

154-
debug_context
155-
.dwarf
156-
.unit
157-
.line_program
158-
.begin_sequence(Some(Address::Symbol { symbol, addend: 0 }));
158+
debug_context.dwarf.unit.line_program.begin_sequence(Some(address_for_func(func_id)));
159159

160160
let mut func_end = 0;
161161

@@ -178,10 +178,7 @@ impl FunctionDebugContext {
178178
assert_ne!(func_end, 0);
179179

180180
let entry = debug_context.dwarf.unit.get_mut(self.entry_id);
181-
entry.set(
182-
gimli::DW_AT_low_pc,
183-
AttributeValue::Address(Address::Symbol { symbol, addend: 0 }),
184-
);
181+
entry.set(gimli::DW_AT_low_pc, AttributeValue::Address(address_for_func(func_id)));
185182
entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(func_end)));
186183

187184
func_end

‎src/debuginfo/mod.rs

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@
33
mod emit;
44
mod line_info;
55
mod object;
6+
mod types;
67
mod unwind;
78

89
use cranelift_codegen::ir::Endianness;
910
use cranelift_codegen::isa::TargetIsa;
11+
use cranelift_module::DataId;
1012
use gimli::write::{
1113
Address, AttributeValue, DwarfUnit, Expression, FileId, LineProgram, LineString, Range,
1214
RangeList, UnitEntryId,
1315
};
1416
use gimli::{AArch64, Encoding, Format, LineEncoding, Register, RiscV, RunTimeEndian, X86_64};
1517
use indexmap::IndexSet;
1618
use rustc_codegen_ssa::debuginfo::type_names;
19+
use rustc_hir::def::DefKind;
1720
use rustc_hir::def_id::DefIdMap;
1821
use rustc_session::Session;
22+
use rustc_span::{SourceFileHash, StableSourceFileId};
1923

2024
pub(crate) use self::emit::{DebugReloc, DebugRelocName};
25+
pub(crate) use self::types::TypeDebugContext;
2126
pub(crate) use self::unwind::UnwindContext;
27+
use crate::debuginfo::emit::{address_for_data, address_for_func};
2228
use crate::prelude::*;
2329

2430
pub(crate) fn producer(sess: &Session) -> String {
@@ -30,8 +36,10 @@ pub(crate) struct DebugContext {
3036

3137
dwarf: DwarfUnit,
3238
unit_range_list: RangeList,
39+
created_files: FxHashMap<(StableSourceFileId, SourceFileHash), FileId>,
3340
stack_pointer_register: Register,
3441
namespace_map: DefIdMap<UnitEntryId>,
42+
array_size_type: UnitEntryId,
3543

3644
should_remap_filepaths: bool,
3745
}
@@ -126,12 +134,27 @@ impl DebugContext {
126134
root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
127135
}
128136

137+
let array_size_type = dwarf.unit.add(dwarf.unit.root(), gimli::DW_TAG_base_type);
138+
let array_size_type_entry = dwarf.unit.get_mut(array_size_type);
139+
array_size_type_entry.set(
140+
gimli::DW_AT_name,
141+
AttributeValue::StringRef(dwarf.strings.add("__ARRAY_SIZE_TYPE__")),
142+
);
143+
array_size_type_entry
144+
.set(gimli::DW_AT_encoding, AttributeValue::Encoding(gimli::DW_ATE_unsigned));
145+
array_size_type_entry.set(
146+
gimli::DW_AT_byte_size,
147+
AttributeValue::Udata(isa.frontend_config().pointer_bytes().into()),
148+
);
149+
129150
DebugContext {
130151
endian,
131152
dwarf,
132153
unit_range_list: RangeList(Vec::new()),
154+
created_files: FxHashMap::default(),
133155
stack_pointer_register,
134156
namespace_map: DefIdMap::default(),
157+
array_size_type,
135158
should_remap_filepaths,
136159
}
137160
}
@@ -169,11 +192,8 @@ impl DebugContext {
169192
linkage_name: &str,
170193
function_span: Span,
171194
) -> FunctionDebugContext {
172-
let (file, line, column) = DebugContext::get_span_loc(tcx, function_span, function_span);
195+
let (file_id, line, column) = self.get_span_loc(tcx, function_span, function_span);
173196

174-
let file_id = self.add_source_file(&file);
175-
176-
// FIXME: add to appropriate scope instead of root
177197
let scope = self.item_namespace(tcx, tcx.parent(instance.def_id()));
178198

179199
let mut name = String::new();
@@ -230,6 +250,62 @@ impl DebugContext {
230250
source_loc_set: IndexSet::new(),
231251
}
232252
}
253+
254+
// Adapted from https://github.com/rust-lang/rust/blob/10a7aa14fed9b528b74b0f098c4899c37c09a9c7/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs#L1288-L1346
255+
pub(crate) fn define_static<'tcx>(
256+
&mut self,
257+
tcx: TyCtxt<'tcx>,
258+
type_dbg: &mut TypeDebugContext<'tcx>,
259+
def_id: DefId,
260+
data_id: DataId,
261+
) {
262+
let DefKind::Static { nested, .. } = tcx.def_kind(def_id) else { bug!() };
263+
if nested {
264+
return;
265+
}
266+
267+
let scope = self.item_namespace(tcx, tcx.parent(def_id));
268+
269+
let span = tcx.def_span(def_id);
270+
let (file_id, line, _column) = self.get_span_loc(tcx, span, span);
271+
272+
let static_type = Instance::mono(tcx, def_id).ty(tcx, ty::ParamEnv::reveal_all());
273+
let static_layout = tcx.layout_of(ty::ParamEnv::reveal_all().and(static_type)).unwrap();
274+
// FIXME use the actual type layout
275+
let type_id = self.debug_type(tcx, type_dbg, static_type);
276+
277+
let name = tcx.item_name(def_id);
278+
let linkage_name = tcx.symbol_name(Instance::mono(tcx, def_id)).name;
279+
280+
let entry_id = self.dwarf.unit.add(scope, gimli::DW_TAG_variable);
281+
let entry = self.dwarf.unit.get_mut(entry_id);
282+
let linkage_name_id = if name.as_str() != linkage_name {
283+
Some(self.dwarf.strings.add(linkage_name))
284+
} else {
285+
None
286+
};
287+
let name_id = self.dwarf.strings.add(name.as_str());
288+
289+
entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id));
290+
entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(type_id));
291+
292+
if tcx.is_reachable_non_generic(def_id) {
293+
entry.set(gimli::DW_AT_external, AttributeValue::FlagPresent);
294+
}
295+
296+
entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id)));
297+
entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(line));
298+
299+
entry.set(gimli::DW_AT_alignment, AttributeValue::Udata(static_layout.align.pref.bytes()));
300+
301+
let mut expr = Expression::new();
302+
expr.op_addr(address_for_data(data_id));
303+
entry.set(gimli::DW_AT_location, AttributeValue::Exprloc(expr));
304+
305+
if let Some(linkage_name_id) = linkage_name_id {
306+
entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(linkage_name_id));
307+
}
308+
}
233309
}
234310

235311
impl FunctionDebugContext {
@@ -239,21 +315,16 @@ impl FunctionDebugContext {
239315
func_id: FuncId,
240316
context: &Context,
241317
) {
242-
let symbol = func_id.as_u32() as usize;
318+
let end = self.create_debug_lines(debug_context, func_id, context);
243319

244-
let end = self.create_debug_lines(debug_context, symbol, context);
245-
246-
debug_context.unit_range_list.0.push(Range::StartLength {
247-
begin: Address::Symbol { symbol, addend: 0 },
248-
length: u64::from(end),
249-
});
320+
debug_context
321+
.unit_range_list
322+
.0
323+
.push(Range::StartLength { begin: address_for_func(func_id), length: u64::from(end) });
250324

251325
let func_entry = debug_context.dwarf.unit.get_mut(self.entry_id);
252326
// Gdb requires both DW_AT_low_pc and DW_AT_high_pc. Otherwise the DW_TAG_subprogram is skipped.
253-
func_entry.set(
254-
gimli::DW_AT_low_pc,
255-
AttributeValue::Address(Address::Symbol { symbol, addend: 0 }),
256-
);
327+
func_entry.set(gimli::DW_AT_low_pc, AttributeValue::Address(address_for_func(func_id)));
257328
// Using Udata for DW_AT_high_pc requires at least DWARF4
258329
func_entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(end)));
259330
}

‎src/debuginfo/object.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cranelift_module::FuncId;
1+
use cranelift_module::{DataId, FuncId};
22
use cranelift_object::ObjectProduct;
33
use gimli::SectionId;
44
use object::write::{Relocation, StandardSegment};
@@ -57,10 +57,13 @@ impl WriteDebugInfo for ObjectProduct {
5757
let (symbol, symbol_offset) = match reloc.name {
5858
DebugRelocName::Section(id) => (section_map.get(&id).unwrap().1, 0),
5959
DebugRelocName::Symbol(id) => {
60-
let symbol_id = self.function_symbol(FuncId::from_u32(id.try_into().unwrap()));
61-
self.object
62-
.symbol_section_and_offset(symbol_id)
63-
.expect("Debug reloc for undef sym???")
60+
let id = id.try_into().unwrap();
61+
let symbol_id = if id & 1 << 31 == 0 {
62+
self.function_symbol(FuncId::from_u32(id))
63+
} else {
64+
self.data_symbol(DataId::from_u32(id & !(1 << 31)))
65+
};
66+
self.object.symbol_section_and_offset(symbol_id).unwrap_or((symbol_id, 0))
6467
}
6568
};
6669
self.object

‎src/debuginfo/types.rs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Adapted from https://github.com/rust-lang/rust/blob/10a7aa14fed9b528b74b0f098c4899c37c09a9c7/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
2+
3+
use gimli::write::{AttributeValue, UnitEntryId};
4+
use rustc_codegen_ssa::debuginfo::type_names;
5+
use rustc_data_structures::fx::FxHashMap;
6+
use rustc_middle::ty::{self, Ty, TyCtxt};
7+
8+
use crate::{has_ptr_meta, DebugContext};
9+
10+
#[derive(Default)]
11+
pub(crate) struct TypeDebugContext<'tcx> {
12+
type_map: FxHashMap<Ty<'tcx>, UnitEntryId>,
13+
}
14+
15+
/// Returns from the enclosing function if the type debuginfo node with the given
16+
/// unique ID can be found in the type map.
17+
macro_rules! return_if_type_created_in_meantime {
18+
($type_dbg:expr, $ty:expr) => {
19+
if let Some(&type_id) = $type_dbg.type_map.get(&$ty) {
20+
return type_id;
21+
}
22+
};
23+
}
24+
25+
impl DebugContext {
26+
pub(crate) fn debug_type<'tcx>(
27+
&mut self,
28+
tcx: TyCtxt<'tcx>,
29+
type_dbg: &mut TypeDebugContext<'tcx>,
30+
ty: Ty<'tcx>,
31+
) -> UnitEntryId {
32+
if let Some(&type_id) = type_dbg.type_map.get(&ty) {
33+
return type_id;
34+
}
35+
36+
let type_id = match ty.kind() {
37+
ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
38+
self.basic_type(tcx, ty)
39+
}
40+
ty::Tuple(elems) if elems.is_empty() => self.basic_type(tcx, ty),
41+
ty::Array(elem_ty, len) => self.array_type(
42+
tcx,
43+
type_dbg,
44+
*elem_ty,
45+
len.eval_target_usize(tcx, ty::ParamEnv::reveal_all()),
46+
),
47+
// ty::Slice(_) | ty::Str
48+
// ty::Dynamic
49+
// ty::Foreign
50+
ty::RawPtr(pointee_type, _) | ty::Ref(_, pointee_type, _) => {
51+
self.pointer_type(tcx, type_dbg, ty, *pointee_type)
52+
}
53+
// ty::Adt(def, args) if def.is_box() && args.get(1).map_or(true, |arg| cx.layout_of(arg.expect_ty()).is_1zst())
54+
// ty::FnDef(..) | ty::FnPtr(..)
55+
// ty::Closure(..)
56+
// ty::Adt(def, ..)
57+
// ty::Tuple(_)
58+
// ty::Param(_)
59+
// FIXME implement remaining types and add unreachable!() to the fallback branch
60+
_ => self.placeholder_for_type(tcx, type_dbg, ty),
61+
};
62+
63+
type_dbg.type_map.insert(ty, type_id);
64+
65+
type_id
66+
}
67+
68+
fn basic_type<'tcx>(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> UnitEntryId {
69+
let (name, encoding) = match ty.kind() {
70+
ty::Never => ("!", gimli::DW_ATE_unsigned),
71+
ty::Tuple(elems) if elems.is_empty() => ("()", gimli::DW_ATE_unsigned),
72+
ty::Bool => ("bool", gimli::DW_ATE_boolean),
73+
ty::Char => ("char", gimli::DW_ATE_UTF),
74+
ty::Int(int_ty) => (int_ty.name_str(), gimli::DW_ATE_signed),
75+
ty::Uint(uint_ty) => (uint_ty.name_str(), gimli::DW_ATE_unsigned),
76+
ty::Float(float_ty) => (float_ty.name_str(), gimli::DW_ATE_float),
77+
_ => unreachable!(),
78+
};
79+
80+
let type_id = self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_base_type);
81+
let type_entry = self.dwarf.unit.get_mut(type_id);
82+
type_entry.set(gimli::DW_AT_name, AttributeValue::StringRef(self.dwarf.strings.add(name)));
83+
type_entry.set(gimli::DW_AT_encoding, AttributeValue::Encoding(encoding));
84+
type_entry.set(
85+
gimli::DW_AT_byte_size,
86+
AttributeValue::Udata(
87+
tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).expect("FIXME").size.bytes(),
88+
),
89+
);
90+
91+
type_id
92+
}
93+
94+
fn array_type<'tcx>(
95+
&mut self,
96+
tcx: TyCtxt<'tcx>,
97+
type_dbg: &mut TypeDebugContext<'tcx>,
98+
elem_ty: Ty<'tcx>,
99+
len: u64,
100+
) -> UnitEntryId {
101+
let elem_dw_ty = self.debug_type(tcx, type_dbg, elem_ty);
102+
103+
return_if_type_created_in_meantime!(type_dbg, elem_ty);
104+
105+
let array_type_id = self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_array_type);
106+
let array_type_entry = self.dwarf.unit.get_mut(array_type_id);
107+
array_type_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(elem_dw_ty));
108+
109+
let subrange_id = self.dwarf.unit.add(array_type_id, gimli::DW_TAG_subrange_type);
110+
let subrange_entry = self.dwarf.unit.get_mut(subrange_id);
111+
subrange_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(self.array_size_type));
112+
subrange_entry.set(gimli::DW_AT_lower_bound, AttributeValue::Udata(0));
113+
subrange_entry.set(gimli::DW_AT_count, AttributeValue::Udata(len));
114+
115+
array_type_id
116+
}
117+
118+
fn pointer_type<'tcx>(
119+
&mut self,
120+
tcx: TyCtxt<'tcx>,
121+
type_dbg: &mut TypeDebugContext<'tcx>,
122+
ptr_type: Ty<'tcx>,
123+
pointee_type: Ty<'tcx>,
124+
) -> UnitEntryId {
125+
let pointee_dw_ty = self.debug_type(tcx, type_dbg, pointee_type);
126+
127+
return_if_type_created_in_meantime!(type_dbg, ptr_type);
128+
129+
let name = type_names::compute_debuginfo_type_name(tcx, ptr_type, true);
130+
131+
if !has_ptr_meta(tcx, ptr_type) {
132+
let pointer_type_id =
133+
self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_pointer_type);
134+
let pointer_entry = self.dwarf.unit.get_mut(pointer_type_id);
135+
pointer_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(pointee_dw_ty));
136+
pointer_entry
137+
.set(gimli::DW_AT_name, AttributeValue::StringRef(self.dwarf.strings.add(name)));
138+
139+
pointer_type_id
140+
} else {
141+
// FIXME implement debuginfo for fat pointers
142+
self.placeholder_for_type(tcx, type_dbg, ptr_type)
143+
}
144+
}
145+
146+
fn placeholder_for_type<'tcx>(
147+
&mut self,
148+
tcx: TyCtxt<'tcx>,
149+
type_dbg: &mut TypeDebugContext<'tcx>,
150+
ty: Ty<'tcx>,
151+
) -> UnitEntryId {
152+
self.debug_type(
153+
tcx,
154+
type_dbg,
155+
Ty::new_array(
156+
tcx,
157+
tcx.types.u8,
158+
tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).unwrap().size.bytes(),
159+
),
160+
)
161+
}
162+
}

‎src/debuginfo/unwind.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
use cranelift_codegen::ir::Endianness;
44
use cranelift_codegen::isa::{unwind::UnwindInfo, TargetIsa};
55
use cranelift_object::ObjectProduct;
6-
use gimli::write::{Address, CieId, EhFrame, FrameTable, Section};
6+
use gimli::write::{CieId, EhFrame, FrameTable, Section};
77
use gimli::RunTimeEndian;
88

9+
use super::emit::address_for_func;
910
use super::object::WriteDebugInfo;
1011
use crate::prelude::*;
1112

@@ -47,11 +48,8 @@ impl UnwindContext {
4748

4849
match unwind_info {
4950
UnwindInfo::SystemV(unwind_info) => {
50-
self.frame_table.add_fde(
51-
self.cie_id.unwrap(),
52-
unwind_info
53-
.to_fde(Address::Symbol { symbol: func_id.as_u32() as usize, addend: 0 }),
54-
);
51+
self.frame_table
52+
.add_fde(self.cie_id.unwrap(), unwind_info.to_fde(address_for_func(func_id)));
5553
}
5654
UnwindInfo::WindowsX64(_) => {
5755
// FIXME implement this

‎src/driver/aot.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_session::config::{DebugInfo, OutFileName, OutputFilenames, OutputType}
2323
use rustc_session::Session;
2424

2525
use crate::concurrency_limiter::{ConcurrencyLimiter, ConcurrencyLimiterToken};
26+
use crate::debuginfo::TypeDebugContext;
2627
use crate::global_asm::GlobalAsmConfig;
2728
use crate::{prelude::*, BackendConfig};
2829

@@ -460,6 +461,7 @@ fn module_codegen(
460461
tcx.sess.opts.debuginfo != DebugInfo::None,
461462
cgu_name,
462463
);
464+
let mut type_dbg = TypeDebugContext::default();
463465
super::predefine_mono_items(tcx, &mut module, &mono_items);
464466
let mut codegened_functions = vec![];
465467
for (mono_item, _) in mono_items {
@@ -475,7 +477,10 @@ fn module_codegen(
475477
codegened_functions.push(codegened_function);
476478
}
477479
MonoItem::Static(def_id) => {
478-
crate::constant::codegen_static(tcx, &mut module, def_id)
480+
let data_id = crate::constant::codegen_static(tcx, &mut module, def_id);
481+
if let Some(debug_context) = &mut cx.debug_context {
482+
debug_context.define_static(tcx, &mut type_dbg, def_id, data_id);
483+
}
479484
}
480485
MonoItem::GlobalAsm(item_id) => {
481486
crate::global_asm::codegen_global_asm_item(

0 commit comments

Comments
 (0)
Please sign in to comment.