1 // .debug_gdb_scripts binary section.
5 use crate::builder::Builder;
6 use crate::common::CodegenCx;
7 use crate::value::Value;
8 use rustc_codegen_ssa::traits::*;
10 use rustc_session::config::DebugInfo;
12 use rustc_span::symbol::sym;
14 /// Inserts a side-effect free instruction sequence that makes sure that the
15 /// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
16 pub fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Builder<'_, '_, '_>) {
17 if needs_gdb_debug_scripts_section(bx) {
18 let gdb_debug_scripts_section =
19 bx.const_bitcast(get_or_insert_gdb_debug_scripts_section_global(bx), bx.type_i8p());
20 // Load just the first byte as that's all that's necessary to force
21 // LLVM to keep around the reference to the global.
22 let volative_load_instruction = bx.volatile_load(bx.type_i8(), gdb_debug_scripts_section);
24 llvm::LLVMSetAlignment(volative_load_instruction, 1);
29 /// Allocates the global variable responsible for the .debug_gdb_scripts binary
31 pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll Value {
32 let c_section_var_name = "__rustc_debug_gdb_scripts_section__\0";
33 let section_var_name = &c_section_var_name[..c_section_var_name.len() - 1];
36 unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr().cast()) };
38 section_var.unwrap_or_else(|| {
39 let section_name = b".debug_gdb_scripts\0";
40 let section_contents = b"\x01gdb_load_rust_pretty_printers.py\0";
43 let llvm_type = cx.type_array(cx.type_i8(), section_contents.len() as u64);
46 .define_global(section_var_name, llvm_type)
47 .unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
48 llvm::LLVMSetSection(section_var, section_name.as_ptr().cast());
49 llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
50 llvm::LLVMSetGlobalConstant(section_var, llvm::True);
51 llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);
52 llvm::LLVMRustSetLinkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
53 // This should make sure that the whole section is not larger than
54 // the string it contains. Otherwise we get a warning from GDB.
55 llvm::LLVMSetAlignment(section_var, 1);
61 pub fn needs_gdb_debug_scripts_section(cx: &CodegenCx<'_, '_>) -> bool {
62 let omit_gdb_pretty_printer_section =
63 cx.tcx.sess.contains_name(cx.tcx.hir().krate_attrs(), sym::omit_gdb_pretty_printer_section);
65 !omit_gdb_pretty_printer_section
66 && cx.sess().opts.debuginfo != DebugInfo::None
67 && cx.sess().target.emit_debug_gdb_scripts