]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/debuginfo/gdb.rs
All Builder methods now take &mut self instead of &self
[rust.git] / src / librustc_codegen_llvm / debuginfo / gdb.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // .debug_gdb_scripts binary section.
12
13 use llvm;
14
15 use common::CodegenCx;
16 use builder::Builder;
17 use rustc::session::config::DebugInfo;
18 use value::Value;
19 use rustc_codegen_ssa::interfaces::*;
20
21 use syntax::attr;
22
23
24 /// Inserts a side-effect free instruction sequence that makes sure that the
25 /// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
26 pub fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Builder) {
27     if needs_gdb_debug_scripts_section(bx.cx()) {
28         let gdb_debug_scripts_section = get_or_insert_gdb_debug_scripts_section_global(bx.cx());
29         // Load just the first byte as that's all that's necessary to force
30         // LLVM to keep around the reference to the global.
31         let indices = [bx.cx().const_i32(0), bx.cx().const_i32(0)];
32         let element = bx.inbounds_gep(gdb_debug_scripts_section, &indices);
33         let volative_load_instruction = bx.volatile_load(element);
34         unsafe {
35             llvm::LLVMSetAlignment(volative_load_instruction, 1);
36         }
37     }
38 }
39
40 /// Allocates the global variable responsible for the .debug_gdb_scripts binary
41 /// section.
42 pub fn get_or_insert_gdb_debug_scripts_section_global(cx: &CodegenCx<'ll, '_>)
43                                                   -> &'ll Value {
44     let c_section_var_name = "__rustc_debug_gdb_scripts_section__\0";
45     let section_var_name = &c_section_var_name[..c_section_var_name.len()-1];
46
47     let section_var = unsafe {
48         llvm::LLVMGetNamedGlobal(cx.llmod,
49                                  c_section_var_name.as_ptr() as *const _)
50     };
51
52     section_var.unwrap_or_else(|| {
53         let section_name = b".debug_gdb_scripts\0";
54         let section_contents = b"\x01gdb_load_rust_pretty_printers.py\0";
55
56         unsafe {
57             let llvm_type = cx.type_array(cx.type_i8(),
58                                         section_contents.len() as u64);
59
60             let section_var = cx.define_global(section_var_name,
61                                                      llvm_type).unwrap_or_else(||{
62                 bug!("symbol `{}` is already defined", section_var_name)
63             });
64             llvm::LLVMSetSection(section_var, section_name.as_ptr() as *const _);
65             llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
66             llvm::LLVMSetGlobalConstant(section_var, llvm::True);
67             llvm::LLVMSetUnnamedAddr(section_var, llvm::True);
68             llvm::LLVMRustSetLinkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
69             // This should make sure that the whole section is not larger than
70             // the string it contains. Otherwise we get a warning from GDB.
71             llvm::LLVMSetAlignment(section_var, 1);
72             section_var
73         }
74     })
75 }
76
77 pub fn needs_gdb_debug_scripts_section(cx: &CodegenCx) -> bool {
78     let omit_gdb_pretty_printer_section =
79         attr::contains_name(&cx.tcx.hir.krate_attrs(),
80                             "omit_gdb_pretty_printer_section");
81
82     !omit_gdb_pretty_printer_section &&
83     cx.sess().opts.debuginfo != DebugInfo::None &&
84     cx.sess().target.target.options.emit_debug_gdb_scripts
85 }