2015-04-24 17:25:35 +12:00
|
|
|
// .debug_gdb_scripts binary section.
|
2015-04-24 15:25:42 +12:00
|
|
|
|
2019-02-18 03:58:58 +09:00
|
|
|
use crate::llvm;
|
2015-04-24 15:25:42 +12:00
|
|
|
|
2019-02-18 03:58:58 +09:00
|
|
|
use crate::builder::Builder;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::common::CodegenCx;
|
2019-02-18 03:58:58 +09:00
|
|
|
use crate::value::Value;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc::bug;
|
2018-07-26 11:41:10 -06:00
|
|
|
use rustc::session::config::DebugInfo;
|
2018-11-16 13:45:28 +02:00
|
|
|
use rustc_codegen_ssa::traits::*;
|
2015-04-24 15:25:42 +12:00
|
|
|
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::sym;
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::attr;
|
2015-04-24 15:25:42 +12:00
|
|
|
|
|
|
|
/// Inserts a side-effect free instruction sequence that makes sure that the
|
|
|
|
/// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
|
2019-02-25 08:40:18 +01:00
|
|
|
pub fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Builder<'_, '_, '_>) {
|
2018-11-27 19:00:25 +01:00
|
|
|
if needs_gdb_debug_scripts_section(bx) {
|
|
|
|
let gdb_debug_scripts_section = get_or_insert_gdb_debug_scripts_section_global(bx);
|
2016-12-16 18:48:25 -07:00
|
|
|
// Load just the first byte as that's all that's necessary to force
|
|
|
|
// LLVM to keep around the reference to the global.
|
2018-11-27 19:00:25 +01:00
|
|
|
let indices = [bx.const_i32(0), bx.const_i32(0)];
|
2018-01-05 07:12:32 +02:00
|
|
|
let element = bx.inbounds_gep(gdb_debug_scripts_section, &indices);
|
|
|
|
let volative_load_instruction = bx.volatile_load(element);
|
2015-04-24 15:25:42 +12:00
|
|
|
unsafe {
|
2015-09-15 16:22:16 -05:00
|
|
|
llvm::LLVMSetAlignment(volative_load_instruction, 1);
|
2015-04-24 15:25:42 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocates the global variable responsible for the .debug_gdb_scripts binary
|
|
|
|
/// section.
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn get_or_insert_gdb_debug_scripts_section_global(cx: &CodegenCx<'ll, '_>) -> &'ll Value {
|
2015-10-09 22:20:56 +02:00
|
|
|
let c_section_var_name = "__rustc_debug_gdb_scripts_section__\0";
|
2019-12-22 17:42:04 -05:00
|
|
|
let section_var_name = &c_section_var_name[..c_section_var_name.len() - 1];
|
2015-04-24 15:25:42 +12:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let section_var =
|
|
|
|
unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr().cast()) };
|
2015-04-24 15:25:42 +12:00
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
section_var.unwrap_or_else(|| {
|
2015-04-24 15:25:42 +12:00
|
|
|
let section_name = b".debug_gdb_scripts\0";
|
|
|
|
let section_contents = b"\x01gdb_load_rust_pretty_printers.py\0";
|
|
|
|
|
|
|
|
unsafe {
|
2019-12-22 17:42:04 -05:00
|
|
|
let llvm_type = cx.type_array(cx.type_i8(), section_contents.len() as u64);
|
2015-04-24 15:25:42 +12:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let section_var = cx
|
|
|
|
.define_global(section_var_name, llvm_type)
|
|
|
|
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
|
2019-10-05 03:48:14 -04:00
|
|
|
llvm::LLVMSetSection(section_var, section_name.as_ptr().cast());
|
2018-09-06 11:57:42 -07:00
|
|
|
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
|
2015-04-24 15:25:42 +12:00
|
|
|
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
|
|
|
|
llvm::LLVMSetUnnamedAddr(section_var, llvm::True);
|
2016-09-01 13:52:33 -05:00
|
|
|
llvm::LLVMRustSetLinkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
|
2015-04-24 15:25:42 +12:00
|
|
|
// This should make sure that the whole section is not larger than
|
|
|
|
// the string it contains. Otherwise we get a warning from GDB.
|
|
|
|
llvm::LLVMSetAlignment(section_var, 1);
|
|
|
|
section_var
|
|
|
|
}
|
2018-07-10 13:28:39 +03:00
|
|
|
})
|
2015-04-24 15:25:42 +12:00
|
|
|
}
|
|
|
|
|
2019-02-25 08:40:18 +01:00
|
|
|
pub fn needs_gdb_debug_scripts_section(cx: &CodegenCx<'_, '_>) -> bool {
|
2015-04-24 15:25:42 +12:00
|
|
|
let omit_gdb_pretty_printer_section =
|
2019-05-08 13:21:18 +10:00
|
|
|
attr::contains_name(&cx.tcx.hir().krate_attrs(), sym::omit_gdb_pretty_printer_section);
|
2015-04-24 15:25:42 +12:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
!omit_gdb_pretty_printer_section
|
|
|
|
&& cx.sess().opts.debuginfo != DebugInfo::None
|
|
|
|
&& cx.sess().target.target.options.emit_debug_gdb_scripts
|
2015-04-24 15:25:42 +12:00
|
|
|
}
|