Preserve the .debug_gdb_scripts section

Make sure that compiler and linker don't optimize the section's contents
away by adding the global holding the data to "llvm.used". The volatile
load in the main shim is retained because "llvm.used", which translates
to SHF_GNU_RETAIN on ELF targets, requires a reasonably recent linker;
emitting the volatile load ensures compatibility with older linkers, at
least when libstd is used.

Pretty printers in dylib dependencies are now emitted by the main crate
instead of the dylib; apart from matching how rlibs are handled, this
approach has the advantage that `omit_gdb_pretty_printer_section` keeps
working with dylib dependencies.
This commit is contained in:
Sebastian Poeplau
2025-08-01 12:39:50 +02:00
parent 0f35336396
commit 868bdde25b
8 changed files with 73 additions and 61 deletions

View File

@@ -30,7 +30,7 @@ use tracing::debug;
use self::metadata::{UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER, file_metadata, type_di_node};
use self::namespace::mangled_name_of_instance;
use self::utils::{DIB, create_DIArray, is_node_local_to_unit};
use self::utils::{DIB, create_DIArray, debug_context, is_node_local_to_unit};
use crate::builder::Builder;
use crate::common::{AsCCharPtr, CodegenCx};
use crate::llvm;
@@ -131,20 +131,28 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
}
/// Creates any deferred debug metadata nodes
pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
if let Some(dbg_cx) = &cx.dbg_cx {
debug!("finalize");
if gdb::needs_gdb_debug_scripts_section(cx) {
// Add a .debug_gdb_scripts section to this compile-unit. This will
// cause GDB to try and load the gdb_load_rust_pretty_printers.py file,
// which activates the Rust pretty printers for binary this section is
// contained in.
gdb::get_or_insert_gdb_debug_scripts_section_global(cx);
}
dbg_cx.finalize(cx.sess());
pub(crate) fn finalize(cx: &mut CodegenCx<'_, '_>) {
if cx.dbg_cx.is_none() {
return;
}
debug!("finalize");
if gdb::needs_gdb_debug_scripts_section(cx) {
// Add a .debug_gdb_scripts section to this compile-unit. This will
// cause GDB to try and load the gdb_load_rust_pretty_printers.py file,
// which activates the Rust pretty printers for binary this section is
// contained in.
let section_var = gdb::get_or_insert_gdb_debug_scripts_section_global(cx);
// Make sure that the linker doesn't optimize the global away. Adding
// it to `llvm.used` has the advantage that it works even in no_std
// binaries, where we don't have a main shim and thus don't emit a
// volatile load to preserve the global.
cx.add_used_global(section_var);
}
debug_context(cx).finalize(cx.sess());
}
impl<'ll> Builder<'_, 'll, '_> {
@@ -614,7 +622,7 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
metadata::extend_scope_to_file(self, scope_metadata, file)
}
fn debuginfo_finalize(&self) {
fn debuginfo_finalize(&mut self) {
finalize(self)
}