Skip to content

Commit 42eeb58

Browse files
committed
Implement support for DWARF version 5.
DWARF version 5 brings a number of improvements over version 4. Quoting from the announcement [1]: > Version 5 incorporates improvements in many areas: better data compression, > separation of debugging data from executable files, improved description of > macros and source files, faster searching for symbols, improved debugging > optimized code, as well as numerous improvements in functionality and > performance. On platforms where DWARF version 5 is supported (Linux, primarily), this commit adds support for it behind a new `-Z dwarf-version=5` flag. [1]: https://dwarfstd.org/Public_Review.php
1 parent 72fd41a commit 42eeb58

File tree

13 files changed

+59
-21
lines changed

13 files changed

+59
-21
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
103103
// for macOS to understand. For more info see #11352
104104
// This can be overridden using --llvm-opts -dwarf-version,N.
105105
// Android has the same issue (#22398)
106-
if let Some(version) = sess.target.dwarf_version {
107-
llvm::LLVMRustAddModuleFlag(
108-
self.llmod,
109-
llvm::LLVMModFlagBehavior::Warning,
110-
"Dwarf Version\0".as_ptr().cast(),
111-
version,
112-
)
113-
}
106+
let dwarf_version =
107+
sess.opts.debugging_opts.dwarf_version.unwrap_or(sess.target.default_dwarf_version);
108+
llvm::LLVMRustAddModuleFlag(
109+
self.llmod,
110+
llvm::LLVMModFlagBehavior::Warning,
111+
"Dwarf Version\0".as_ptr().cast(),
112+
dwarf_version,
113+
);
114114

115115
// Indicate that we want CodeView debug information on MSVC
116116
if sess.target.is_like_msvc {

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ fn test_debugging_options_tracking_hash() {
733733
tracked!(dep_info_omit_d_target, true);
734734
tracked!(drop_tracking, true);
735735
tracked!(dual_proc_macros, true);
736+
tracked!(dwarf_version, Some(5));
736737
tracked!(fewer_names, Some(true));
737738
tracked!(force_unstable_if_unmarked, true);
738739
tracked!(fuel, Some(("abc".to_string(), 99)));

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,8 @@ options! {
12691269
computed `block` spans (one span encompassing a block's terminator and \
12701270
all statements). If `-Z instrument-coverage` is also enabled, create \
12711271
an additional `.html` file showing the computed coverage spans."),
1272+
dwarf_version: Option<u32> = (None, parse_opt_number, [TRACKED],
1273+
"version of DWARF debug information to emit (default: 2 or 4, depending on platform)"),
12721274
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
12731275
"emit a section containing stack size metadata (default: no)"),
12741276
fewer_names: Option<bool> = (None, parse_opt_bool, [TRACKED],

compiler/rustc_session/src/session.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,12 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
14661466
))
14671467
}
14681468
}
1469+
1470+
if let Some(dwarf_version) = sess.opts.debugging_opts.dwarf_version {
1471+
if dwarf_version > 5 {
1472+
sess.err(&format!("requested DWARF version {} is greater than 5", dwarf_version));
1473+
}
1474+
}
14691475
}
14701476

14711477
/// Holds data on the current incremental compilation session, if there is one.

compiler/rustc_target/src/spec/android_base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::spec::TargetOptions;
33
pub fn opts() -> TargetOptions {
44
let mut base = super::linux_base::opts();
55
base.os = "android".into();
6-
base.dwarf_version = Some(2);
6+
base.default_dwarf_version = 2;
77
base.position_independent_executables = true;
88
base.has_thread_local = false;
99
// This is for backward compatibility, see https://github.com/rust-lang/rust/issues/49867

compiler/rustc_target/src/spec/apple_base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn opts(os: &'static str) -> TargetOptions {
2828
executables: true,
2929
families: cvs!["unix"],
3030
is_like_osx: true,
31-
dwarf_version: Some(2),
31+
default_dwarf_version: 2,
3232
frame_pointer: FramePointer::Always,
3333
has_rpath: true,
3434
dll_suffix: ".dylib".into(),

compiler/rustc_target/src/spec/dragonfly_base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn opts() -> TargetOptions {
99
has_rpath: true,
1010
position_independent_executables: true,
1111
relro_level: RelroLevel::Full,
12-
dwarf_version: Some(2),
12+
default_dwarf_version: 2,
1313
..Default::default()
1414
}
1515
}

compiler/rustc_target/src/spec/freebsd_base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn opts() -> TargetOptions {
1010
position_independent_executables: true,
1111
relro_level: RelroLevel::Full,
1212
abi_return_struct_as_int: true,
13-
dwarf_version: Some(2),
13+
default_dwarf_version: 2,
1414
..Default::default()
1515
}
1616
}

compiler/rustc_target/src/spec/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1281,9 +1281,9 @@ pub struct TargetOptions {
12811281
pub is_like_fuchsia: bool,
12821282
/// Whether a target toolchain is like WASM.
12831283
pub is_like_wasm: bool,
1284-
/// Version of DWARF to use if not using the default.
1284+
/// Default supported version of DWARF on this platform.
12851285
/// Useful because some platforms (osx, bsd) only want up to DWARF2.
1286-
pub dwarf_version: Option<u32>,
1286+
pub default_dwarf_version: u32,
12871287
/// Whether the linker support GNU-like arguments such as -O. Defaults to true.
12881288
pub linker_is_gnu: bool,
12891289
/// The MinGW toolchain has a known issue that prevents it from correctly
@@ -1509,7 +1509,7 @@ impl Default for TargetOptions {
15091509
is_like_msvc: false,
15101510
is_like_fuchsia: false,
15111511
is_like_wasm: false,
1512-
dwarf_version: None,
1512+
default_dwarf_version: 4,
15131513
linker_is_gnu: true,
15141514
allows_weak_linkage: true,
15151515
has_rpath: false,
@@ -1748,13 +1748,13 @@ impl Target {
17481748
base.$key_name = s;
17491749
}
17501750
} );
1751-
($key_name:ident, Option<u32>) => ( {
1751+
($key_name:ident, u32) => ( {
17521752
let name = (stringify!($key_name)).replace("_", "-");
17531753
if let Some(s) = obj.remove(&name).and_then(|b| b.as_u64()) {
17541754
if s < 1 || s > 5 {
17551755
return Err("Not a valid DWARF version number".into());
17561756
}
1757-
base.$key_name = Some(s as u32);
1757+
base.$key_name = s as u32;
17581758
}
17591759
} );
17601760
($key_name:ident, Option<u64>) => ( {
@@ -2115,7 +2115,7 @@ impl Target {
21152115
key!(is_like_emscripten, bool);
21162116
key!(is_like_fuchsia, bool);
21172117
key!(is_like_wasm, bool);
2118-
key!(dwarf_version, Option<u32>);
2118+
key!(default_dwarf_version, u32);
21192119
key!(linker_is_gnu, bool);
21202120
key!(allows_weak_linkage, bool);
21212121
key!(has_rpath, bool);
@@ -2361,7 +2361,7 @@ impl ToJson for Target {
23612361
target_option_val!(is_like_emscripten);
23622362
target_option_val!(is_like_fuchsia);
23632363
target_option_val!(is_like_wasm);
2364-
target_option_val!(dwarf_version);
2364+
target_option_val!(default_dwarf_version);
23652365
target_option_val!(linker_is_gnu);
23662366
target_option_val!(allows_weak_linkage);
23672367
target_option_val!(has_rpath);

compiler/rustc_target/src/spec/netbsd_base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn opts() -> TargetOptions {
1111
position_independent_executables: true,
1212
relro_level: RelroLevel::Full,
1313
use_ctors_section: true,
14-
dwarf_version: Some(2),
14+
default_dwarf_version: 2,
1515
..Default::default()
1616
}
1717
}

compiler/rustc_target/src/spec/openbsd_base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn opts() -> TargetOptions {
1111
position_independent_executables: true,
1212
frame_pointer: FramePointer::Always, // FIXME 43575: should be MayOmit...
1313
relro_level: RelroLevel::Full,
14-
dwarf_version: Some(2),
14+
default_dwarf_version: 2,
1515
..Default::default()
1616
}
1717
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## `dwarf-version`
2+
3+
This option controls the version of DWARF that the compiler emits, on platforms
4+
that use DWARF to encode debug information. It takes one of the following
5+
values:
6+
7+
* `2`: DWARF version 2 (the default on certain platforms, like macOS).
8+
* `4`: DWARF version 4 (the default on certain platforms, like Linux).
9+
* `5`: DWARF version 5.

src/test/assembly/dwarf5.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Makes sure that `-Z dwarf-version=5` causes `rustc` to emit DWARF version 5.
2+
// assembly-output: emit-asm
3+
// compile-flags: -g --target x86_64-unknown-linux-gnu -Z dwarf-version=5
4+
// needs-llvm-components: x86
5+
6+
#![feature(no_core, lang_items)]
7+
#![crate_type = "rlib"]
8+
#![no_core]
9+
10+
#[lang = "sized"]
11+
trait Sized {}
12+
#[lang = "copy"]
13+
trait Copy {}
14+
15+
pub fn wibble() {}
16+
17+
// CHECK: .section .debug_info
18+
// CHECK-NOT: .short 2
19+
// CHECK-NOT: .short 4
20+
// CHECK: .short 5

0 commit comments

Comments
 (0)