forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
11 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# `embed-source` | ||
|
||
This flag controls whether the compiler embeds the program source code text into | ||
the object debug information section. It takes one of the following values: | ||
|
||
* `y`, `yes`, `on` or `true`: put source code in debug info. | ||
* `n`, `no`, `off`, `false` or no value: omit source code from debug info (the default). | ||
|
||
This flag is ignored in configurations that don't emit DWARF debug information | ||
and is ignored on non-LLVM backends. `-Z embed-source` requires DWARFv5. Use | ||
`-Z dwarf-version=5` to control the compiler's DWARF target version and `-g` to | ||
enable debug info generation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// hello | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//@ ignore-windows | ||
//@ ignore-apple | ||
|
||
// LLVM 17's embed-source implementation requires that source code is attached | ||
// for all files in the output DWARF debug info. This restriction was lifted in | ||
// LLVM 18 (87e22bdd2bd6d77d782f9d64b3e3ae5bdcd5080d). | ||
//@ min-llvm-version: 18 | ||
|
||
// This test should be replaced with one in tests/debuginfo once we can easily | ||
// tell via GDB or LLDB if debuginfo contains source code. Cheap tricks in LLDB | ||
// like setting an invalid source map path don't appear to work, maybe this'll | ||
// become easier once GDB supports DWARFv6? | ||
|
||
use std::collections::HashMap; | ||
use std::path::PathBuf; | ||
use std::rc::Rc; | ||
|
||
use gimli::{AttributeValue, EndianRcSlice, Reader, RunTimeEndian}; | ||
use object::{Object, ObjectSection}; | ||
use run_make_support::{gimli, object, rfs, rustc}; | ||
|
||
fn main() { | ||
let output = PathBuf::from("embed-source-main"); | ||
rustc() | ||
.input("main.rs") | ||
.output(&output) | ||
.arg("-g") | ||
.arg("-Zembed-source=yes") | ||
.arg("-Zdwarf-version=5") | ||
.run(); | ||
let output = rfs::read(output); | ||
let obj = object::File::parse(output.as_slice()).unwrap(); | ||
let endian = if obj.is_little_endian() { RunTimeEndian::Little } else { RunTimeEndian::Big }; | ||
let dwarf = gimli::Dwarf::load(|section| -> Result<_, ()> { | ||
let data = obj.section_by_name(section.name()).map(|s| s.uncompressed_data().unwrap()); | ||
Ok(EndianRcSlice::new(Rc::from(data.unwrap_or_default().as_ref()), endian)) | ||
}) | ||
.unwrap(); | ||
|
||
let mut sources = HashMap::new(); | ||
|
||
let mut iter = dwarf.units(); | ||
while let Some(header) = iter.next().unwrap() { | ||
let unit = dwarf.unit(header).unwrap(); | ||
let unit = unit.unit_ref(&dwarf); | ||
|
||
if let Some(program) = &unit.line_program { | ||
let header = program.header(); | ||
for file in header.file_names() { | ||
if let Some(source) = file.source() { | ||
let path = unit | ||
.attr_string(file.path_name()) | ||
.unwrap() | ||
.to_string_lossy() | ||
.unwrap() | ||
.to_string(); | ||
let source = | ||
unit.attr_string(source).unwrap().to_string_lossy().unwrap().to_string(); | ||
if !source.is_empty() { | ||
sources.insert(path, source); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
dbg!(&sources); | ||
assert_eq!(sources.len(), 1); | ||
assert_eq!(sources.get("main.rs").unwrap(), "// hello\nfn main() {}\n"); | ||
} |