11// .debug_gdb_scripts binary section.
22
3+ use std:: ffi:: CString ;
4+
35use rustc_ast:: attr;
46use rustc_codegen_ssa:: base:: collect_debugger_visualizers_transitive;
57use rustc_codegen_ssa:: traits:: * ;
@@ -9,31 +11,21 @@ use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerType;
911use rustc_session:: config:: { CrateType , DebugInfo } ;
1012use rustc_span:: sym;
1113
12- use crate :: builder:: Builder ;
1314use crate :: common:: CodegenCx ;
1415use crate :: llvm;
1516use 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.
3320pub ( 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 } )
0 commit comments