Skip to content

Commit 3f992f9

Browse files
authored
Unrolled build for rust-lang#126985
Rollup merge of rust-lang#126985 - Mrmaxmeier:dwarf-embed-source, r=davidtwco Implement `-Z embed-source` (DWARFv5 source code embedding extension) Implement rust-lang/compiler-team#764 MCP which adds an unstable flag that exposes LLVM's [DWARFv5 source code embedding](https://dwarfstd.org/issues/180201.1.html) support.
2 parents bf662eb + 6899f5a commit 3f992f9

File tree

11 files changed

+135
-4
lines changed

11 files changed

+135
-4
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+9
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,9 @@ pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFi
629629
};
630630
let hash_value = hex_encode(source_file.src_hash.hash_bytes());
631631

632+
let source =
633+
cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
634+
632635
unsafe {
633636
llvm::LLVMRustDIBuilderCreateFile(
634637
DIB(cx),
@@ -639,6 +642,8 @@ pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFi
639642
hash_kind,
640643
hash_value.as_ptr().cast(),
641644
hash_value.len(),
645+
source.map_or(ptr::null(), |x| x.as_ptr().cast()),
646+
source.map_or(0, |x| x.len()),
642647
)
643648
}
644649
}
@@ -659,6 +664,8 @@ fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
659664
llvm::ChecksumKind::None,
660665
hash_value.as_ptr().cast(),
661666
hash_value.len(),
667+
ptr::null(),
668+
0,
662669
)
663670
})
664671
}
@@ -943,6 +950,8 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
943950
llvm::ChecksumKind::None,
944951
ptr::null(),
945952
0,
953+
ptr::null(),
954+
0,
946955
);
947956

948957
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,8 @@ extern "C" {
18601860
CSKind: ChecksumKind,
18611861
Checksum: *const c_char,
18621862
ChecksumLen: size_t,
1863+
Source: *const c_char,
1864+
SourceLen: size_t,
18631865
) -> &'a DIFile;
18641866

18651867
pub fn LLVMRustDIBuilderCreateSubroutineType<'a>(

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ fn test_unstable_options_tracking_hash() {
774774
tracked!(direct_access_external_data, Some(true));
775775
tracked!(dual_proc_macros, true);
776776
tracked!(dwarf_version, Some(5));
777+
tracked!(embed_source, true);
777778
tracked!(emit_thin_lto, false);
778779
tracked!(export_executable_symbols, true);
779780
tracked!(fewer_names, Some(true));

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -913,14 +913,19 @@ extern "C" LLVMMetadataRef
913913
LLVMRustDIBuilderCreateFile(LLVMRustDIBuilderRef Builder, const char *Filename,
914914
size_t FilenameLen, const char *Directory,
915915
size_t DirectoryLen, LLVMRustChecksumKind CSKind,
916-
const char *Checksum, size_t ChecksumLen) {
916+
const char *Checksum, size_t ChecksumLen,
917+
const char *Source, size_t SourceLen) {
917918

918919
std::optional<DIFile::ChecksumKind> llvmCSKind = fromRust(CSKind);
919920
std::optional<DIFile::ChecksumInfo<StringRef>> CSInfo{};
920921
if (llvmCSKind)
921922
CSInfo.emplace(*llvmCSKind, StringRef{Checksum, ChecksumLen});
923+
std::optional<StringRef> oSource{};
924+
if (Source)
925+
oSource = StringRef(Source, SourceLen);
922926
return wrap(Builder->createFile(StringRef(Filename, FilenameLen),
923-
StringRef(Directory, DirectoryLen), CSInfo));
927+
StringRef(Directory, DirectoryLen), CSInfo,
928+
oSource));
924929
}
925930

926931
extern "C" LLVMMetadataRef

compiler/rustc_session/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ session_crate_name_empty = crate name must not be empty
1414
1515
session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has a leading hyphen
1616
17+
session_embed_source_insufficient_dwarf_version = `-Zembed-source=y` requires at least `-Z dwarf-version=5` but DWARF version is {$dwarf_version}
18+
19+
session_embed_source_requires_debug_info = `-Zembed-source=y` requires debug information to be enabled
20+
1721
session_expr_parentheses_needed = parentheses are required to parse this as an expression
1822
1923
session_failed_to_create_profiler = failed to create profiler: {$err}

compiler/rustc_session/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ pub(crate) struct UnsupportedDwarfVersion {
165165
pub(crate) dwarf_version: u32,
166166
}
167167

168+
#[derive(Diagnostic)]
169+
#[diag(session_embed_source_insufficient_dwarf_version)]
170+
pub(crate) struct EmbedSourceInsufficientDwarfVersion {
171+
pub(crate) dwarf_version: u32,
172+
}
173+
174+
#[derive(Diagnostic)]
175+
#[diag(session_embed_source_requires_debug_info)]
176+
pub(crate) struct EmbedSourceRequiresDebugInfo;
177+
168178
#[derive(Diagnostic)]
169179
#[diag(session_target_stack_protector_not_supported)]
170180
pub(crate) struct StackProtectorNotSupportedForTarget<'a> {

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,8 @@ options! {
17011701
them only if an error has not been emitted"),
17021702
ehcont_guard: bool = (false, parse_bool, [TRACKED],
17031703
"generate Windows EHCont Guard tables"),
1704+
embed_source: bool = (false, parse_bool, [TRACKED],
1705+
"embed source text in DWARF debug sections (default: no)"),
17041706
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
17051707
"emit a section containing stack size metadata (default: no)"),
17061708
emit_thin_lto: bool = (true, parse_bool, [TRACKED],

compiler/rustc_session/src/session.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ use rustc_target::spec::{
3737
use crate::code_stats::CodeStats;
3838
pub use crate::code_stats::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
3939
use crate::config::{
40-
self, CoverageLevel, CrateType, ErrorOutputType, FunctionReturn, Input, InstrumentCoverage,
41-
OptLevel, OutFileName, OutputType, RemapPathScopeComponents, SwitchWithOptPath,
40+
self, CoverageLevel, CrateType, DebugInfo, ErrorOutputType, FunctionReturn, Input,
41+
InstrumentCoverage, OptLevel, OutFileName, OutputType, RemapPathScopeComponents,
42+
SwitchWithOptPath,
4243
};
4344
use crate::parse::{add_feature_diagnostics, ParseSess};
4445
use crate::search_paths::{PathKind, SearchPath};
@@ -1306,6 +1307,19 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
13061307
.emit_err(errors::SplitDebugInfoUnstablePlatform { debuginfo: sess.split_debuginfo() });
13071308
}
13081309

1310+
if sess.opts.unstable_opts.embed_source {
1311+
let dwarf_version =
1312+
sess.opts.unstable_opts.dwarf_version.unwrap_or(sess.target.default_dwarf_version);
1313+
1314+
if dwarf_version < 5 {
1315+
sess.dcx().emit_warn(errors::EmbedSourceInsufficientDwarfVersion { dwarf_version });
1316+
}
1317+
1318+
if sess.opts.debuginfo == DebugInfo::None {
1319+
sess.dcx().emit_warn(errors::EmbedSourceRequiresDebugInfo);
1320+
}
1321+
}
1322+
13091323
if sess.opts.unstable_opts.instrument_xray.is_some() && !sess.target.options.supports_xray {
13101324
sess.dcx().emit_err(errors::InstrumentationNotSupported { us: "XRay".to_string() });
13111325
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# `embed-source`
2+
3+
This flag controls whether the compiler embeds the program source code text into
4+
the object debug information section. It takes one of the following values:
5+
6+
* `y`, `yes`, `on` or `true`: put source code in debug info.
7+
* `n`, `no`, `off`, `false` or no value: omit source code from debug info (the default).
8+
9+
This flag is ignored in configurations that don't emit DWARF debug information
10+
and is ignored on non-LLVM backends. `-Z embed-source` requires DWARFv5. Use
11+
`-Z dwarf-version=5` to control the compiler's DWARF target version and `-g` to
12+
enable debug info generation.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// hello
2+
fn main() {}
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//@ ignore-windows
2+
//@ ignore-apple
3+
4+
// LLVM 17's embed-source implementation requires that source code is attached
5+
// for all files in the output DWARF debug info. This restriction was lifted in
6+
// LLVM 18 (87e22bdd2bd6d77d782f9d64b3e3ae5bdcd5080d).
7+
//@ min-llvm-version: 18
8+
9+
// This test should be replaced with one in tests/debuginfo once we can easily
10+
// tell via GDB or LLDB if debuginfo contains source code. Cheap tricks in LLDB
11+
// like setting an invalid source map path don't appear to work, maybe this'll
12+
// become easier once GDB supports DWARFv6?
13+
14+
use std::collections::HashMap;
15+
use std::path::PathBuf;
16+
use std::rc::Rc;
17+
18+
use gimli::{AttributeValue, EndianRcSlice, Reader, RunTimeEndian};
19+
use object::{Object, ObjectSection};
20+
use run_make_support::{gimli, object, rfs, rustc};
21+
22+
fn main() {
23+
let output = PathBuf::from("embed-source-main");
24+
rustc()
25+
.input("main.rs")
26+
.output(&output)
27+
.arg("-g")
28+
.arg("-Zembed-source=yes")
29+
.arg("-Zdwarf-version=5")
30+
.run();
31+
let output = rfs::read(output);
32+
let obj = object::File::parse(output.as_slice()).unwrap();
33+
let endian = if obj.is_little_endian() { RunTimeEndian::Little } else { RunTimeEndian::Big };
34+
let dwarf = gimli::Dwarf::load(|section| -> Result<_, ()> {
35+
let data = obj.section_by_name(section.name()).map(|s| s.uncompressed_data().unwrap());
36+
Ok(EndianRcSlice::new(Rc::from(data.unwrap_or_default().as_ref()), endian))
37+
})
38+
.unwrap();
39+
40+
let mut sources = HashMap::new();
41+
42+
let mut iter = dwarf.units();
43+
while let Some(header) = iter.next().unwrap() {
44+
let unit = dwarf.unit(header).unwrap();
45+
let unit = unit.unit_ref(&dwarf);
46+
47+
if let Some(program) = &unit.line_program {
48+
let header = program.header();
49+
for file in header.file_names() {
50+
if let Some(source) = file.source() {
51+
let path = unit
52+
.attr_string(file.path_name())
53+
.unwrap()
54+
.to_string_lossy()
55+
.unwrap()
56+
.to_string();
57+
let source =
58+
unit.attr_string(source).unwrap().to_string_lossy().unwrap().to_string();
59+
if !source.is_empty() {
60+
sources.insert(path, source);
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
dbg!(&sources);
68+
assert_eq!(sources.len(), 1);
69+
assert_eq!(sources.get("main.rs").unwrap(), "// hello\nfn main() {}\n");
70+
}

0 commit comments

Comments
 (0)