Skip to content

Commit e02cc40

Browse files
committed
Revert "Preserve the .debug_gdb_scripts section"
This reverts commit 868bdde.
1 parent 270c1a4 commit e02cc40

File tree

8 files changed

+60
-72
lines changed

8 files changed

+60
-72
lines changed

compiler/rustc_codegen_gcc/src/debuginfo.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ impl<'gcc, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
254254
// TODO(antoyo): implement.
255255
}
256256

257-
fn debuginfo_finalize(&mut self) {
258-
// TODO: emit section `.debug_gdb_scripts`.
257+
fn debuginfo_finalize(&self) {
259258
self.context.set_debug_info(true)
260259
}
261260

compiler/rustc_codegen_llvm/src/base.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,11 @@ pub(crate) fn compile_codegen_unit(
109109
}
110110

111111
// Finalize code coverage by injecting the coverage map. Note, the coverage map will
112-
// also be added to the `llvm.compiler.used` variable, created below.
112+
// also be added to the `llvm.compiler.used` variable, created next.
113113
if cx.sess().instrument_coverage() {
114114
cx.coverageinfo_finalize();
115115
}
116116

117-
// Finalize debuginfo. This adds to `llvm.used`, created below.
118-
if cx.sess().opts.debuginfo != DebugInfo::None {
119-
cx.debuginfo_finalize();
120-
}
121-
122117
// Create the llvm.used and llvm.compiler.used variables.
123118
if !cx.used_statics.is_empty() {
124119
cx.create_used_variable_impl(c"llvm.used", &cx.used_statics);
@@ -135,6 +130,11 @@ pub(crate) fn compile_codegen_unit(
135130
llvm::LLVMDeleteGlobal(old_g);
136131
}
137132
}
133+
134+
// Finalize debuginfo
135+
if cx.sess().opts.debuginfo != DebugInfo::None {
136+
cx.debuginfo_finalize();
137+
}
138138
}
139139

140140
ModuleCodegen::new_regular(cgu_name.to_string(), llvm_module)

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// .debug_gdb_scripts binary section.
22

3-
use std::ffi::CString;
4-
53
use rustc_codegen_ssa::base::collect_debugger_visualizers_transitive;
64
use rustc_codegen_ssa::traits::*;
75
use rustc_hir::def_id::LOCAL_CRATE;
@@ -33,12 +31,7 @@ pub(crate) fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Buil
3331
pub(crate) fn get_or_insert_gdb_debug_scripts_section_global<'ll>(
3432
cx: &CodegenCx<'ll, '_>,
3533
) -> &'ll Value {
36-
let c_section_var_name = CString::new(format!(
37-
"__rustc_debug_gdb_scripts_section_{}_{:08x}",
38-
cx.tcx.crate_name(LOCAL_CRATE),
39-
cx.tcx.stable_crate_id(LOCAL_CRATE),
40-
))
41-
.unwrap();
34+
let c_section_var_name = c"__rustc_debug_gdb_scripts_section__";
4235
let section_var_name = c_section_var_name.to_str().unwrap();
4336

4437
let section_var = unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr()) };
@@ -91,10 +84,17 @@ pub(crate) fn get_or_insert_gdb_debug_scripts_section_global<'ll>(
9184
}
9285

9386
pub(crate) fn needs_gdb_debug_scripts_section(cx: &CodegenCx<'_, '_>) -> bool {
94-
// We collect pretty printers transitively for all crates, so we make sure
95-
// that the section is only emitted for leaf crates.
87+
// To ensure the section `__rustc_debug_gdb_scripts_section__` will not create
88+
// ODR violations at link time, this section will not be emitted for rlibs since
89+
// each rlib could produce a different set of visualizers that would be embedded
90+
// in the `.debug_gdb_scripts` section. For that reason, we make sure that the
91+
// section is only emitted for leaf crates.
9692
let embed_visualizers = cx.tcx.crate_types().iter().any(|&crate_type| match crate_type {
97-
CrateType::Executable | CrateType::Cdylib | CrateType::Staticlib | CrateType::Sdylib => {
93+
CrateType::Executable
94+
| CrateType::Dylib
95+
| CrateType::Cdylib
96+
| CrateType::Staticlib
97+
| CrateType::Sdylib => {
9898
// These are crate types for which we will embed pretty printers since they
9999
// are treated as leaf crates.
100100
true
@@ -105,11 +105,9 @@ pub(crate) fn needs_gdb_debug_scripts_section(cx: &CodegenCx<'_, '_>) -> bool {
105105
// want to slow down the common case.
106106
false
107107
}
108-
CrateType::Rlib | CrateType::Dylib => {
109-
// Don't embed pretty printers for these crate types; the compiler
110-
// can see the `#[debug_visualizer]` attributes when using the
111-
// library, and emitting `.debug_gdb_scripts` regardless would
112-
// break `#![omit_gdb_pretty_printer_section]`.
108+
CrateType::Rlib => {
109+
// As per the above description, embedding pretty printers for rlibs could
110+
// lead to ODR violations so we skip this crate type as well.
113111
false
114112
}
115113
});

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use tracing::debug;
3030

3131
use self::metadata::{UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER, file_metadata, type_di_node};
3232
use self::namespace::mangled_name_of_instance;
33-
use self::utils::{DIB, create_DIArray, debug_context, is_node_local_to_unit};
33+
use self::utils::{DIB, create_DIArray, is_node_local_to_unit};
3434
use crate::builder::Builder;
3535
use crate::common::{AsCCharPtr, CodegenCx};
3636
use crate::llvm;
@@ -131,28 +131,20 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
131131
}
132132

133133
/// Creates any deferred debug metadata nodes
134-
pub(crate) fn finalize(cx: &mut CodegenCx<'_, '_>) {
135-
if cx.dbg_cx.is_none() {
136-
return;
137-
}
138-
139-
debug!("finalize");
140-
141-
if gdb::needs_gdb_debug_scripts_section(cx) {
142-
// Add a .debug_gdb_scripts section to this compile-unit. This will
143-
// cause GDB to try and load the gdb_load_rust_pretty_printers.py file,
144-
// which activates the Rust pretty printers for binary this section is
145-
// contained in.
146-
let section_var = gdb::get_or_insert_gdb_debug_scripts_section_global(cx);
134+
pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
135+
if let Some(dbg_cx) = &cx.dbg_cx {
136+
debug!("finalize");
137+
138+
if gdb::needs_gdb_debug_scripts_section(cx) {
139+
// Add a .debug_gdb_scripts section to this compile-unit. This will
140+
// cause GDB to try and load the gdb_load_rust_pretty_printers.py file,
141+
// which activates the Rust pretty printers for binary this section is
142+
// contained in.
143+
gdb::get_or_insert_gdb_debug_scripts_section_global(cx);
144+
}
147145

148-
// Make sure that the linker doesn't optimize the global away. Adding
149-
// it to `llvm.used` has the advantage that it works even in no_std
150-
// binaries, where we don't have a main shim and thus don't emit a
151-
// volatile load to preserve the global.
152-
cx.add_used_global(section_var);
146+
dbg_cx.finalize(cx.sess());
153147
}
154-
155-
debug_context(cx).finalize(cx.sess());
156148
}
157149

158150
impl<'ll> Builder<'_, 'll, '_> {
@@ -622,7 +614,7 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
622614
metadata::extend_scope_to_file(self, scope_metadata, file)
623615
}
624616

625-
fn debuginfo_finalize(&mut self) {
617+
fn debuginfo_finalize(&self) {
626618
finalize(self)
627619
}
628620

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,15 @@ pub fn collect_debugger_visualizers_transitive(
609609
) -> BTreeSet<DebuggerVisualizerFile> {
610610
tcx.debugger_visualizers(LOCAL_CRATE)
611611
.iter()
612-
.chain(tcx.crates(()).iter().flat_map(|&cnum| tcx.debugger_visualizers(cnum)))
612+
.chain(
613+
tcx.crates(())
614+
.iter()
615+
.filter(|&cnum| {
616+
let used_crate_source = tcx.used_crate_source(*cnum);
617+
used_crate_source.rlib.is_some() || used_crate_source.rmeta.is_some()
618+
})
619+
.flat_map(|&cnum| tcx.debugger_visualizers(cnum)),
620+
)
613621
.filter(|visualizer| visualizer.visualizer_type == visualizer_type)
614622
.cloned()
615623
.collect::<BTreeSet<_>>()

compiler/rustc_codegen_ssa/src/traits/debuginfo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub trait DebugInfoCodegenMethods<'tcx>: BackendTypes {
5050
scope_metadata: Self::DIScope,
5151
file: &SourceFile,
5252
) -> Self::DIScope;
53-
fn debuginfo_finalize(&mut self);
53+
fn debuginfo_finalize(&self);
5454

5555
// FIXME(eddyb) find a common convention for all of the debuginfo-related
5656
// names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.).

tests/codegen-llvm/gdb_debug_script_load.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#![feature(lang_items)]
1010
#![no_std]
1111

12-
// CHECK: @llvm.used = {{.+}} @__rustc_debug_gdb_scripts_section
13-
1412
#[panic_handler]
1513
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
1614
loop {}
@@ -24,7 +22,7 @@ extern "C" fn rust_eh_personality() {
2422
// Needs rustc to generate `main` as that's where the magic load is inserted.
2523
// IOW, we cannot write this test with `#![no_main]`.
2624
// CHECK-LABEL: @main
27-
// CHECK: load volatile i8, {{.+}} @__rustc_debug_gdb_scripts_section
25+
// CHECK: load volatile i8, {{.+}} @__rustc_debug_gdb_scripts_section__
2826

2927
#[lang = "start"]
3028
fn lang_start<T: 'static>(

tests/run-make/symbols-all-mangled/rmake.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ fn symbols_check_archive(path: &str) {
3535
continue; // All compiler-builtins symbols must remain unmangled
3636
}
3737

38-
if symbol_ok_everywhere(name) {
38+
if name.contains("rust_eh_personality") {
39+
continue; // Unfortunately LLVM doesn't allow us to mangle this symbol
40+
}
41+
42+
if name.contains(".llvm.") {
43+
// Starting in LLVM 21 we get various implementation-detail functions which
44+
// contain .llvm. that are not a problem.
3945
continue;
4046
}
4147

@@ -65,7 +71,13 @@ fn symbols_check(path: &str) {
6571
continue;
6672
}
6773

68-
if symbol_ok_everywhere(name) {
74+
if name.contains("rust_eh_personality") {
75+
continue; // Unfortunately LLVM doesn't allow us to mangle this symbol
76+
}
77+
78+
if name.contains(".llvm.") {
79+
// Starting in LLVM 21 we get various implementation-detail functions which
80+
// contain .llvm. that are not a problem.
6981
continue;
7082
}
7183

@@ -76,22 +88,3 @@ fn symbols_check(path: &str) {
7688
fn strip_underscore_if_apple(symbol: &str) -> &str {
7789
if cfg!(target_vendor = "apple") { symbol.strip_prefix("_").unwrap() } else { symbol }
7890
}
79-
80-
fn symbol_ok_everywhere(name: &str) -> bool {
81-
if name.contains("rust_eh_personality") {
82-
return true; // Unfortunately LLVM doesn't allow us to mangle this symbol
83-
}
84-
85-
if name.contains(".llvm.") {
86-
// Starting in LLVM 21 we get various implementation-detail functions which
87-
// contain .llvm. that are not a problem.
88-
return true;
89-
}
90-
91-
if name.starts_with("__rustc_debug_gdb_scripts_section") {
92-
// These symbols are fine; they're made unique by the crate ID.
93-
return true;
94-
}
95-
96-
return false;
97-
}

0 commit comments

Comments
 (0)