]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/debuginfo/gdb.rs
Auto merge of #42264 - GuillaumeGomez:new-error-codes, r=Susurrus
[rust.git] / src / librustc_trans / 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::{C_bytes, CrateContext, C_i32};
16 use builder::Builder;
17 use declare;
18 use type_::Type;
19 use rustc::session::config::NoDebugInfo;
20
21 use std::ptr;
22 use syntax::attr;
23
24
25 /// Inserts a side-effect free instruction sequence that makes sure that the
26 /// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
27 pub fn insert_reference_to_gdb_debug_scripts_section_global(ccx: &CrateContext, builder: &Builder) {
28     if needs_gdb_debug_scripts_section(ccx) {
29         let gdb_debug_scripts_section_global = get_or_insert_gdb_debug_scripts_section_global(ccx);
30         // Load just the first byte as that's all that's necessary to force
31         // LLVM to keep around the reference to the global.
32         let indices = [C_i32(ccx, 0), C_i32(ccx, 0)];
33         let element = builder.inbounds_gep(gdb_debug_scripts_section_global, &indices);
34         let volative_load_instruction = builder.volatile_load(element);
35         unsafe {
36             llvm::LLVMSetAlignment(volative_load_instruction, 1);
37         }
38     }
39 }
40
41 /// Allocates the global variable responsible for the .debug_gdb_scripts binary
42 /// section.
43 pub fn get_or_insert_gdb_debug_scripts_section_global(ccx: &CrateContext)
44                                                   -> llvm::ValueRef {
45     let c_section_var_name = "__rustc_debug_gdb_scripts_section__\0";
46     let section_var_name = &c_section_var_name[..c_section_var_name.len()-1];
47
48     let section_var = unsafe {
49         llvm::LLVMGetNamedGlobal(ccx.llmod(),
50                                  c_section_var_name.as_ptr() as *const _)
51     };
52
53     if section_var == ptr::null_mut() {
54         let section_name = b".debug_gdb_scripts\0";
55         let section_contents = b"\x01gdb_load_rust_pretty_printers.py\0";
56
57         unsafe {
58             let llvm_type = Type::array(&Type::i8(ccx),
59                                         section_contents.len() as u64);
60
61             let section_var = declare::define_global(ccx, section_var_name,
62                                                      llvm_type).unwrap_or_else(||{
63                 bug!("symbol `{}` is already defined", section_var_name)
64             });
65             llvm::LLVMSetSection(section_var, section_name.as_ptr() as *const _);
66             llvm::LLVMSetInitializer(section_var, C_bytes(ccx, section_contents));
67             llvm::LLVMSetGlobalConstant(section_var, llvm::True);
68             llvm::LLVMSetUnnamedAddr(section_var, llvm::True);
69             llvm::LLVMRustSetLinkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
70             // This should make sure that the whole section is not larger than
71             // the string it contains. Otherwise we get a warning from GDB.
72             llvm::LLVMSetAlignment(section_var, 1);
73             section_var
74         }
75     } else {
76         section_var
77     }
78 }
79
80 pub fn needs_gdb_debug_scripts_section(ccx: &CrateContext) -> bool {
81     let omit_gdb_pretty_printer_section =
82         attr::contains_name(&ccx.tcx().hir.krate_attrs(),
83                             "omit_gdb_pretty_printer_section");
84
85     !omit_gdb_pretty_printer_section &&
86     !ccx.sess().target.target.options.is_like_osx &&
87     !ccx.sess().target.target.options.is_like_windows &&
88     ccx.sess().opts.debuginfo != NoDebugInfo
89 }