77 lines
3.1 KiB
Rust
Raw Normal View History

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::common::CodegenCx;
use crate::builder::Builder;
use crate::value::Value;
use rustc::session::config::DebugInfo;
use rustc_codegen_ssa::traits::*;
rustc: Link LLVM directly into rustc again This commit builds on #65501 continue to simplify the build system and compiler now that we no longer have multiple LLVM backends to ship by default. Here this switches the compiler back to what it once was long long ago, which is linking LLVM directly to the compiler rather than dynamically loading it at runtime. The `codegen-backends` directory of the sysroot no longer exists and all relevant support in the build system is removed. Note that `rustc` still supports a dynamically loaded codegen backend as it did previously, it just no longer supports dynamically loaded codegen backends in its own sysroot. Additionally as part of this the `librustc_codegen_llvm` crate now once again explicitly depends on all of its crates instead of implicitly loading them through the sysroot. This involved filling out its `Cargo.toml` and deleting all the now-unnecessary `extern crate` annotations in the header of the crate. (this in turn required adding a number of imports for names of macros too). The end results of this change are: * Rustbuild's build process for the compiler as all the "oh don't forget the codegen backend" checks can be easily removed. * Building `rustc_codegen_llvm` is much simpler since it's simply another compiler crate. * Managing the dependencies of `rustc_codegen_llvm` is much simpler since it's "just another `Cargo.toml` to edit" * The build process should be a smidge faster because there's more parallelism in the main rustc build step rather than splitting `librustc_codegen_llvm` out to its own step. * The compiler is expected to be slightly faster by default because the codegen backend does not need to be dynamically loaded. * Disabling LLVM as part of rustbuild is still supported, supporting multiple codegen backends is still supported, and dynamic loading of a codegen backend is still supported.
2019-10-22 08:51:35 -07:00
use rustc::bug;
2015-04-24 15:25:42 +12:00
use syntax::attr;
use syntax::symbol::sym;
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.
pub fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Builder<'_, '_, '_>) {
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.
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 {
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.
pub fn get_or_insert_gdb_debug_scripts_section_global(cx: &CodegenCx<'ll, '_>)
-> &'ll Value {
let c_section_var_name = "__rustc_debug_gdb_scripts_section__\0";
let section_var_name = &c_section_var_name[..c_section_var_name.len()-1];
2015-04-24 15:25:42 +12:00
let section_var = unsafe {
2018-01-05 07:04:08 +02:00
llvm::LLVMGetNamedGlobal(cx.llmod,
c_section_var_name.as_ptr().cast())
2015-04-24 15:25:42 +12: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 {
let llvm_type = cx.type_array(cx.type_i8(),
2015-04-24 15:25:42 +12:00
section_contents.len() as u64);
let section_var = cx.define_global(section_var_name,
2015-04-24 15:25:42 +12:00
llvm_type).unwrap_or_else(||{
bug!("symbol `{}` is already defined", section_var_name)
2015-04-24 15:25:42 +12:00
});
llvm::LLVMSetSection(section_var, section_name.as_ptr().cast());
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);
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
}
})
2015-04-24 15:25:42 +12: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 =
attr::contains_name(&cx.tcx.hir().krate_attrs(), sym::omit_gdb_pretty_printer_section);
2015-04-24 15:25:42 +12: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
}