Skip to content

Commit 5cac546

Browse files
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". This eliminates the need for a volatile load in the main shim; since the LLVM codegen backend is the only implementer of the corresponding trait function, remove it entirely.
1 parent 558d253 commit 5cac546

File tree

8 files changed

+18
-49
lines changed

8 files changed

+18
-49
lines changed

compiler/rustc_codegen_gcc/src/debuginfo.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ impl<'a, 'gcc, 'tcx> DebugInfoBuilderMethods for Builder<'a, 'gcc, 'tcx> {
3636
_variable_alloca.set_location(_dbg_loc);
3737
}
3838

39-
fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) {
40-
// TODO(antoyo): insert reference to gdb debug scripts section global.
41-
}
42-
4339
/// FIXME(tempdragon): Currently, this function is not yet implemented. It seems that the
4440
/// debug name and the mangled name should both be included in the LValues.
4541
/// Besides, a function to get the rvalue type(m_is_lvalue) should also be included.

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

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

3+
use std::ffi::CString;
4+
35
use rustc_ast::attr;
46
use rustc_codegen_ssa::base::collect_debugger_visualizers_transitive;
57
use rustc_codegen_ssa::traits::*;
@@ -9,31 +11,21 @@ use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerType;
911
use rustc_session::config::{CrateType, DebugInfo};
1012
use rustc_span::sym;
1113

12-
use crate::builder::Builder;
1314
use crate::common::CodegenCx;
1415
use crate::llvm;
1516
use crate::value::Value;
1617

17-
/// Inserts a side-effect free instruction sequence that makes sure that the
18-
/// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
19-
pub(crate) fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Builder<'_, '_, '_>) {
20-
if needs_gdb_debug_scripts_section(bx) {
21-
let gdb_debug_scripts_section = get_or_insert_gdb_debug_scripts_section_global(bx);
22-
// Load just the first byte as that's all that's necessary to force
23-
// LLVM to keep around the reference to the global.
24-
let volatile_load_instruction = bx.volatile_load(bx.type_i8(), gdb_debug_scripts_section);
25-
unsafe {
26-
llvm::LLVMSetAlignment(volatile_load_instruction, 1);
27-
}
28-
}
29-
}
30-
3118
/// Allocates the global variable responsible for the .debug_gdb_scripts binary
3219
/// section.
3320
pub(crate) fn get_or_insert_gdb_debug_scripts_section_global<'ll>(
3421
cx: &CodegenCx<'ll, '_>,
3522
) -> &'ll Value {
36-
let c_section_var_name = c"__rustc_debug_gdb_scripts_section__";
23+
let c_section_var_name = CString::new(format!(
24+
"__rustc_debug_gdb_scripts_section_{}_{:08x}",
25+
cx.tcx.crate_name(LOCAL_CRATE),
26+
cx.tcx.stable_crate_id(LOCAL_CRATE),
27+
))
28+
.unwrap();
3729
let section_var_name = c_section_var_name.to_str().unwrap();
3830

3931
let section_var = unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr()) };
@@ -80,6 +72,8 @@ pub(crate) fn get_or_insert_gdb_debug_scripts_section_global<'ll>(
8072
// This should make sure that the whole section is not larger than
8173
// the string it contains. Otherwise we get a warning from GDB.
8274
llvm::LLVMSetAlignment(section_var, 1);
75+
// Make sure that the linker doesn't optimize the global away.
76+
llvm::LLVMRustAppendToUsed(cx.llmod, section_var);
8377
section_var
8478
}
8579
})

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,6 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
215215
}
216216
}
217217

218-
fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) {
219-
gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
220-
}
221-
222218
fn set_var_name(&mut self, value: &'ll Value, name: &str) {
223219
// Avoid wasting time if LLVM value names aren't even enabled.
224220
if self.sess().fewer_names() {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,7 @@ unsafe extern "C" {
18591859
T: &'a Type,
18601860
) -> &'a Value;
18611861
pub(crate) fn LLVMRustInsertPrivateGlobal<'a>(M: &'a Module, T: &'a Type) -> &'a Value;
1862+
pub(crate) fn LLVMRustAppendToUsed<'a>(M: &'a Module, V: &'a Value);
18621863
pub(crate) fn LLVMRustGetNamedValue(
18631864
M: &Module,
18641865
Name: *const c_char,

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,6 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
528528
let llbb = Bx::append_block(cx, llfn, "top");
529529
let mut bx = Bx::build(cx, llbb);
530530

531-
bx.insert_reference_to_gdb_debug_scripts_section_global();
532-
533531
let isize_ty = cx.type_isize();
534532
let ptr_ty = cx.type_ptr();
535533
let (arg_argc, arg_argv) = get_argc_argv(&mut bx);

compiler/rustc_codegen_ssa/src/traits/debuginfo.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,5 @@ pub trait DebugInfoBuilderMethods: BackendTypes {
8181
);
8282
fn set_dbg_loc(&mut self, dbg_loc: Self::DILocation);
8383
fn clear_dbg_loc(&mut self);
84-
fn insert_reference_to_gdb_debug_scripts_section_global(&mut self);
8584
fn set_var_name(&mut self, value: Self::Value, name: &str);
8685
}

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "llvm/Support/Signals.h"
3636
#include "llvm/Support/Timer.h"
3737
#include "llvm/Support/ToolOutputFile.h"
38+
#include "llvm/Transforms/Utils/ModuleUtils.h"
3839
#include <iostream>
3940

4041
// for raw `write` in the bad-alloc handler
@@ -233,6 +234,10 @@ extern "C" LLVMValueRef LLVMRustInsertPrivateGlobal(LLVMModuleRef M,
233234
GlobalValue::PrivateLinkage, nullptr));
234235
}
235236

237+
extern "C" void LLVMRustAppendToUsed(LLVMModuleRef M, LLVMValueRef V) {
238+
appendToUsed(*unwrap(M), {unwrap<GlobalValue>(V)});
239+
}
240+
236241
// Must match the layout of `rustc_codegen_llvm::llvm::ffi::AttributeKind`.
237242
enum class LLVMRustAttributeKind {
238243
AlwaysInline = 0,

tests/codegen/gdb_debug_script_load.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,12 @@
66

77
//@ compile-flags: -g -C no-prepopulate-passes -Cpanic=abort
88

9-
#![feature(lang_items)]
109
#![no_std]
10+
#![no_main]
1111

1212
#[panic_handler]
1313
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
1414
loop {}
1515
}
1616

17-
#[no_mangle]
18-
extern "C" fn rust_eh_personality() {
19-
loop {}
20-
}
21-
22-
// Needs rustc to generate `main` as that's where the magic load is inserted.
23-
// IOW, we cannot write this test with `#![no_main]`.
24-
// CHECK-LABEL: @main
25-
// CHECK: load volatile i8, {{.+}} @__rustc_debug_gdb_scripts_section__
26-
27-
#[lang = "start"]
28-
fn lang_start<T: 'static>(
29-
_main: fn() -> T,
30-
_argc: isize,
31-
_argv: *const *const u8,
32-
_sigpipe: u8,
33-
) -> isize {
34-
return 0;
35-
}
36-
37-
fn main() {}
17+
// CHECK: @llvm.used = {{.+}} @__rustc_debug_gdb_scripts_section

0 commit comments

Comments
 (0)