Skip to content

Commit 36f6e2f

Browse files
committed
Use FileIndex for DW_AT_decl_file and omit .debug_ranges and .debug_rnglists when empty
1 parent cf7ee00 commit 36f6e2f

File tree

1 file changed

+74
-56
lines changed

1 file changed

+74
-56
lines changed

src/debuginfo.rs

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ extern crate gimli;
22

33
use crate::prelude::*;
44

5+
use std::path::Path;
56
use std::marker::PhantomData;
67

78
use gimli::write::{
89
Address, AttributeValue, CompilationUnit, DebugAbbrev, DebugInfo, DebugLine, DebugRanges,
910
DebugRngLists, DebugStr, EndianVec, LineProgram, LineProgramId, LineProgramTable, Range,
1011
RangeList, RangeListTable, Result, SectionId, StringTable, UnitEntryId, UnitId, UnitTable,
11-
Writer,
12+
Writer, FileId,
1213
};
1314
use gimli::{Endianity, Format, RunTimeEndian};
1415

@@ -23,6 +24,17 @@ fn target_endian(tcx: TyCtxt) -> RunTimeEndian {
2324
}
2425
}
2526

27+
fn line_program_add_file<P: AsRef<Path>>(line_program: &mut LineProgram, file: P) -> FileId {
28+
let file = file.as_ref();
29+
let dir_id =
30+
line_program.add_directory(file.parent().unwrap().to_str().unwrap().as_bytes());
31+
line_program.add_file(
32+
file.file_name().unwrap().to_str().unwrap().as_bytes(),
33+
dir_id,
34+
None,
35+
)
36+
}
37+
2638
struct DebugReloc {
2739
offset: u32,
2840
size: u8,
@@ -53,7 +65,7 @@ pub struct DebugContext<'tcx> {
5365
impl<'a, 'tcx: 'a> DebugContext<'tcx> {
5466
pub fn new(tcx: TyCtxt, address_size: u8) -> Self {
5567
// TODO: this should be configurable
56-
let version = 4;
68+
let version = 3; // macOS doesn't seem to support DWARF > 3
5769
let format = Format::Dwarf32;
5870

5971
// FIXME: how to get version when building out of tree?
@@ -71,7 +83,7 @@ impl<'a, 'tcx: 'a> DebugContext<'tcx> {
7183
let range_lists = RangeListTable::default();
7284

7385
let global_line_program = line_programs.add(LineProgram::new(
74-
3, // FIXME https://github.com/gimli-rs/gimli/issues/363
86+
version,
7587
address_size,
7688
format,
7789
1,
@@ -135,11 +147,13 @@ impl<'a, 'tcx: 'a> DebugContext<'tcx> {
135147
fn emit_location(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>, entry_id: UnitEntryId, span: Span) {
136148
let loc = tcx.sess.source_map().lookup_char_pos(span.lo());
137149

150+
let line_program = self.line_programs.get_mut(self.global_line_program);
151+
let file_id = line_program_add_file(line_program, loc.file.name.to_string());
152+
138153
let unit = self.units.get_mut(self.unit_id);
139154
let entry = unit.get_mut(entry_id);
140155

141-
let file_id = self.strings.add(loc.file.name.to_string());
142-
entry.set(gimli::DW_AT_decl_file, AttributeValue::StringRef(file_id));
156+
entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(file_id));
143157
entry.set(
144158
gimli::DW_AT_decl_line,
145159
AttributeValue::Udata(loc.line as u64),
@@ -218,20 +232,28 @@ impl<'a, 'tcx: 'a> DebugContext<'tcx> {
218232
debug_line.0.writer.into_vec(),
219233
)
220234
.unwrap();
221-
artifact
222-
.declare_with(
223-
SectionId::DebugRanges.name(),
224-
Decl::DebugSection,
225-
debug_ranges.0.writer.into_vec(),
226-
)
227-
.unwrap();
228-
artifact
229-
.declare_with(
230-
SectionId::DebugRngLists.name(),
231-
Decl::DebugSection,
232-
debug_rnglists.0.writer.into_vec(),
233-
)
234-
.unwrap();
235+
236+
let debug_ranges_not_empty = !debug_ranges.0.writer.slice().is_empty();
237+
if debug_ranges_not_empty {
238+
artifact
239+
.declare_with(
240+
SectionId::DebugRanges.name(),
241+
Decl::DebugSection,
242+
debug_ranges.0.writer.into_vec(),
243+
)
244+
.unwrap();
245+
}
246+
247+
let debug_rnglists_not_empty = !debug_rnglists.0.writer.slice().is_empty();
248+
if debug_rnglists_not_empty {
249+
artifact
250+
.declare_with(
251+
SectionId::DebugRngLists.name(),
252+
Decl::DebugSection,
253+
debug_rnglists.0.writer.into_vec(),
254+
)
255+
.unwrap();
256+
}
235257

236258
for reloc in debug_abbrev.0.relocs {
237259
artifact
@@ -297,36 +319,40 @@ impl<'a, 'tcx: 'a> DebugContext<'tcx> {
297319
.expect("faerie relocation error");
298320
}
299321

300-
for reloc in debug_ranges.0.relocs {
301-
artifact
302-
.link_with(
303-
faerie::Link {
304-
from: SectionId::DebugRanges.name(),
305-
to: &reloc.name,
306-
at: u64::from(reloc.offset),
307-
},
308-
faerie::Reloc::Debug {
309-
size: reloc.size,
310-
addend: reloc.addend as i32,
311-
},
312-
)
313-
.expect("faerie relocation error");
322+
if debug_ranges_not_empty {
323+
for reloc in debug_ranges.0.relocs {
324+
artifact
325+
.link_with(
326+
faerie::Link {
327+
from: SectionId::DebugRanges.name(),
328+
to: &reloc.name,
329+
at: u64::from(reloc.offset),
330+
},
331+
faerie::Reloc::Debug {
332+
size: reloc.size,
333+
addend: reloc.addend as i32,
334+
},
335+
)
336+
.expect("faerie relocation error");
337+
}
314338
}
315339

316-
for reloc in debug_rnglists.0.relocs {
317-
artifact
318-
.link_with(
319-
faerie::Link {
320-
from: SectionId::DebugRngLists.name(),
321-
to: &reloc.name,
322-
at: u64::from(reloc.offset),
323-
},
324-
faerie::Reloc::Debug {
325-
size: reloc.size,
326-
addend: reloc.addend as i32,
327-
},
328-
)
329-
.expect("faerie relocation error");
340+
if debug_rnglists_not_empty {
341+
for reloc in debug_rnglists.0.relocs {
342+
artifact
343+
.link_with(
344+
faerie::Link {
345+
from: SectionId::DebugRngLists.name(),
346+
to: &reloc.name,
347+
at: u64::from(reloc.offset),
348+
},
349+
faerie::Reloc::Debug {
350+
size: reloc.size,
351+
addend: reloc.addend as i32,
352+
},
353+
)
354+
.expect("faerie relocation error");
355+
}
330356
}
331357
}
332358

@@ -423,15 +449,7 @@ impl<'a, 'b, 'tcx: 'b> FunctionDebugContext<'a, 'tcx> {
423449

424450
let create_row_for_span = |line_program: &mut LineProgram, span: Span| {
425451
let loc = tcx.sess.source_map().lookup_char_pos(span.lo());
426-
let file = loc.file.name.to_string();
427-
let file = ::std::path::Path::new(&file);
428-
let dir_id =
429-
line_program.add_directory(file.parent().unwrap().to_str().unwrap().as_bytes());
430-
let file_id = line_program.add_file(
431-
file.file_name().unwrap().to_str().unwrap().as_bytes(),
432-
dir_id,
433-
None,
434-
);
452+
let file_id = line_program_add_file(line_program, loc.file.name.to_string());
435453

436454
/*println!(
437455
"srcloc {:>04X} {}:{}:{}",

0 commit comments

Comments
 (0)