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.
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.
11 // .debug_gdb_scripts binary section.
15 use common::{C_bytes, CodegenCx, C_i32};
18 use rustc::session::config::DebugInfo;
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(bx: &Builder) {
28 if needs_gdb_debug_scripts_section(bx.cx) {
29 let gdb_debug_scripts_section = get_or_insert_gdb_debug_scripts_section_global(bx.cx);
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(bx.cx, 0), C_i32(bx.cx, 0)];
33 let element = bx.inbounds_gep(gdb_debug_scripts_section, &indices);
34 let volative_load_instruction = bx.volatile_load(element);
36 llvm::LLVMSetAlignment(volative_load_instruction, 1);
41 /// Allocates the global variable responsible for the .debug_gdb_scripts binary
43 pub fn get_or_insert_gdb_debug_scripts_section_global(cx: &CodegenCx<'ll, '_>)
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];
48 let section_var = unsafe {
49 llvm::LLVMGetNamedGlobal(cx.llmod,
50 c_section_var_name.as_ptr() as *const _)
53 section_var.unwrap_or_else(|| {
54 let section_name = b".debug_gdb_scripts\0";
55 let section_contents = b"\x01gdb_load_rust_pretty_printers.py\0";
58 let llvm_type = Type::array(Type::i8(cx),
59 section_contents.len() as u64);
61 let section_var = declare::define_global(cx, section_var_name,
62 llvm_type).unwrap_or_else(||{
63 bug!("symbol `{}` is already defined", section_var_name)
65 llvm::LLVMSetSection(section_var, section_name.as_ptr() as *const _);
66 llvm::LLVMSetInitializer(section_var, C_bytes(cx, 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);
78 pub fn needs_gdb_debug_scripts_section(cx: &CodegenCx) -> bool {
79 let omit_gdb_pretty_printer_section =
80 attr::contains_name(&cx.tcx.hir.krate_attrs(),
81 "omit_gdb_pretty_printer_section");
83 !omit_gdb_pretty_printer_section &&
84 cx.sess().opts.debuginfo != DebugInfo::None &&
85 cx.sess().target.target.options.emit_debug_gdb_scripts