]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/debuginfo/gdb.rs
Rollup merge of #58353 - matthewjasper:typeck-pattern-constants, r=arielb1
[rust.git] / src / librustc_codegen_llvm / debuginfo / gdb.rs
1 // .debug_gdb_scripts binary section.
2
3 use crate::llvm;
4
5 use crate::common::CodegenCx;
6 use crate::builder::Builder;
7 use crate::value::Value;
8 use rustc::session::config::DebugInfo;
9 use rustc_codegen_ssa::traits::*;
10
11 use syntax::attr;
12
13
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 = get_or_insert_gdb_debug_scripts_section_global(bx);
19         // Load just the first byte as that's all that's necessary to force
20         // LLVM to keep around the reference to the global.
21         let indices = [bx.const_i32(0), bx.const_i32(0)];
22         let element = bx.inbounds_gep(gdb_debug_scripts_section, &indices);
23         let volative_load_instruction = bx.volatile_load(element);
24         unsafe {
25             llvm::LLVMSetAlignment(volative_load_instruction, 1);
26         }
27     }
28 }
29
30 /// Allocates the global variable responsible for the .debug_gdb_scripts binary
31 /// section.
32 pub fn get_or_insert_gdb_debug_scripts_section_global(cx: &CodegenCx<'ll, '_>)
33                                                   -> &'ll Value {
34     let c_section_var_name = "__rustc_debug_gdb_scripts_section__\0";
35     let section_var_name = &c_section_var_name[..c_section_var_name.len()-1];
36
37     let section_var = unsafe {
38         llvm::LLVMGetNamedGlobal(cx.llmod,
39                                  c_section_var_name.as_ptr() as *const _)
40     };
41
42     section_var.unwrap_or_else(|| {
43         let section_name = b".debug_gdb_scripts\0";
44         let section_contents = b"\x01gdb_load_rust_pretty_printers.py\0";
45
46         unsafe {
47             let llvm_type = cx.type_array(cx.type_i8(),
48                                         section_contents.len() as u64);
49
50             let section_var = cx.define_global(section_var_name,
51                                                      llvm_type).unwrap_or_else(||{
52                 bug!("symbol `{}` is already defined", section_var_name)
53             });
54             llvm::LLVMSetSection(section_var, section_name.as_ptr() as *const _);
55             llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
56             llvm::LLVMSetGlobalConstant(section_var, llvm::True);
57             llvm::LLVMSetUnnamedAddr(section_var, llvm::True);
58             llvm::LLVMRustSetLinkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
59             // This should make sure that the whole section is not larger than
60             // the string it contains. Otherwise we get a warning from GDB.
61             llvm::LLVMSetAlignment(section_var, 1);
62             section_var
63         }
64     })
65 }
66
67 pub fn needs_gdb_debug_scripts_section(cx: &CodegenCx) -> bool {
68     let omit_gdb_pretty_printer_section =
69         attr::contains_name(&cx.tcx.hir().krate_attrs(),
70                             "omit_gdb_pretty_printer_section");
71
72     !omit_gdb_pretty_printer_section &&
73     cx.sess().opts.debuginfo != DebugInfo::None &&
74     cx.sess().target.target.options.emit_debug_gdb_scripts
75 }