Skip to content

Commit fe678d2

Browse files
committed
Support -Zembed-source in debuginfo
- honour the session DWARF version, enabling v5 when embed-source is requested and plumb the flag through the debug context - attach embedded source bytes to line table FileInfo records alongside existing MD5 hashes - stop excluding rustc’s embed-source-dwarf run-make test so CG_CLIF exercises the feature
1 parent 1988e68 commit fe678d2

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

scripts/test_rustc_tests.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ rm tests/ui/asm/global-asm-mono-sym-fn.rs # same
6060
rm tests/ui/asm/naked-asm-mono-sym-fn.rs # same
6161
rm tests/ui/asm/x86_64/goto.rs # inline asm labels not supported
6262
rm tests/ui/simd/simd-bitmask-notpow2.rs # non-pow-of-2 simd vector sizes
63-
rm -r tests/run-make/embed-source-dwarf # embedding sources in debuginfo
6463
rm -r tests/run-make/used-proc-macro # used(linker) isn't supported yet
6564
rm tests/ui/linking/no-gc-encapsulation-symbols.rs # same
6665
rm tests/ui/attributes/fn-align-dyn.rs # per-function alignment not supported

src/debuginfo/line_info.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use cranelift_codegen::MachSrcLoc;
77
use cranelift_codegen::binemit::CodeOffset;
88
use gimli::write::{AttributeValue, FileId, FileInfo, LineProgram, LineString, LineStringTable};
99
use rustc_span::{
10-
FileName, Pos, SourceFile, SourceFileAndLine, SourceFileHash, SourceFileHashAlgorithm, hygiene,
10+
FileName, Pos, SourceFile, SourceFileAndLine, SourceFileHashAlgorithm, hygiene,
1111
};
1212

1313
use crate::debuginfo::FunctionDebugContext;
@@ -44,21 +44,27 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
4444
}
4545
}
4646

47-
const MD5_LEN: usize = 16;
48-
49-
fn make_file_info(hash: SourceFileHash) -> Option<FileInfo> {
50-
if hash.kind == SourceFileHashAlgorithm::Md5 {
51-
let mut buf = [0u8; MD5_LEN];
52-
buf.copy_from_slice(hash.hash_bytes());
53-
Some(FileInfo {
54-
timestamp: 0,
55-
size: 0,
56-
md5: buf,
57-
source: None, // FIXME implement -Zembed-source
58-
})
59-
} else {
60-
None
47+
fn make_file_info(source_file: &SourceFile, embed_source: bool) -> Option<FileInfo> {
48+
let has_md5 = source_file.src_hash.kind == SourceFileHashAlgorithm::Md5;
49+
let has_source = embed_source && source_file.src.is_some();
50+
51+
if !has_md5 && !has_source {
52+
return None;
53+
}
54+
55+
let mut info = FileInfo::default();
56+
57+
if has_md5 {
58+
info.md5.copy_from_slice(source_file.src_hash.hash_bytes());
6159
}
60+
61+
if embed_source {
62+
if let Some(src) = &source_file.src {
63+
info.source = Some(LineString::String(src.as_bytes().to_vec()));
64+
}
65+
}
66+
67+
Some(info)
6268
}
6369

6470
impl DebugContext {
@@ -105,9 +111,10 @@ impl DebugContext {
105111
let file_name =
106112
LineString::new(file_name, line_program.encoding(), line_strings);
107113

108-
let info = make_file_info(source_file.src_hash);
114+
let info = make_file_info(source_file, self.embed_source);
109115

110-
line_program.file_has_md5 &= info.is_some();
116+
let has_md5 = source_file.src_hash.kind == SourceFileHashAlgorithm::Md5;
117+
line_program.file_has_md5 &= has_md5;
111118
line_program.add_file(file_name, dir_id, info)
112119
}
113120
filename => {

src/debuginfo/mod.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub(crate) struct DebugContext {
4545
array_size_type: UnitEntryId,
4646

4747
filename_display_preference: FileNameDisplayPreference,
48+
embed_source: bool,
4849
}
4950

5051
pub(crate) struct FunctionDebugContext {
@@ -67,21 +68,27 @@ impl DebugContext {
6768
return None;
6869
}
6970

71+
// FIXME this should be configurable
72+
// macOS doesn't seem to support DWARF > 3
73+
// DWARF version 5 is required for MD5 file hashes and embedded sources
74+
let mut requested_dwarf_version = tcx.sess.dwarf_version();
75+
if tcx.sess.target.is_like_darwin && requested_dwarf_version > 4 {
76+
// Apple’s shipped debuggers still expect DWARF <= 4 by default.
77+
// Stay on v4 unless the user explicitly opts into a feature that
78+
// only works with v5 (e.g. -Zembed-source).
79+
if !tcx.sess.opts.unstable_opts.embed_source {
80+
requested_dwarf_version = 4;
81+
}
82+
}
83+
7084
let encoding = Encoding {
7185
format: Format::Dwarf32,
72-
// FIXME this should be configurable
73-
// macOS doesn't seem to support DWARF > 3
74-
// 5 version is required for md5 file hash
75-
version: if tcx.sess.target.is_like_darwin {
76-
3
77-
} else {
78-
// FIXME change to version 5 once the gdb and lldb shipping with the latest debian
79-
// support it.
80-
4
81-
},
86+
version: requested_dwarf_version as u16,
8287
address_size: isa.frontend_config().pointer_bytes(),
8388
};
8489

90+
let embed_source = tcx.sess.opts.unstable_opts.embed_source && encoding.version >= 5;
91+
8592
let endian = match isa.endianness() {
8693
Endianness::Little => RunTimeEndian::Little,
8794
Endianness::Big => RunTimeEndian::Big,
@@ -125,6 +132,9 @@ impl DebugContext {
125132
file_info,
126133
);
127134
line_program.file_has_md5 = file_has_md5;
135+
if embed_source {
136+
line_program.file_has_source = true;
137+
}
128138

129139
dwarf.unit.line_program = line_program;
130140

@@ -169,6 +179,7 @@ impl DebugContext {
169179
namespace_map: DefIdMap::default(),
170180
array_size_type,
171181
filename_display_preference,
182+
embed_source,
172183
})
173184
}
174185

0 commit comments

Comments
 (0)