]>
Commit | Line | Data |
---|---|---|
d9579d0f AL |
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 | use llvm::ValueRef; | |
15 | ||
16 | use trans::common::{C_bytes, CrateContext}; | |
17 | use trans::declare; | |
18 | use trans::type_::Type; | |
d9579d0f AL |
19 | use session::config::NoDebugInfo; |
20 | ||
21 | use std::ffi::CString; | |
22 | use std::ptr; | |
23 | use syntax::attr; | |
24 | ||
25 | ||
26 | /// Inserts a side-effect free instruction sequence that makes sure that the | |
27 | /// .debug_gdb_scripts global is referenced, so it isn't removed by the linker. | |
28 | pub fn insert_reference_to_gdb_debug_scripts_section_global(ccx: &CrateContext) { | |
29 | if needs_gdb_debug_scripts_section(ccx) { | |
30 | let empty = CString::new("").unwrap(); | |
31 | let gdb_debug_scripts_section_global = | |
32 | get_or_insert_gdb_debug_scripts_section_global(ccx); | |
33 | unsafe { | |
34 | let volative_load_instruction = | |
35 | llvm::LLVMBuildLoad(ccx.raw_builder(), | |
36 | gdb_debug_scripts_section_global, | |
37 | empty.as_ptr()); | |
38 | llvm::LLVMSetVolatile(volative_load_instruction, llvm::True); | |
39 | } | |
40 | } | |
41 | } | |
42 | ||
43 | /// Allocates the global variable responsible for the .debug_gdb_scripts binary | |
44 | /// section. | |
45 | pub fn get_or_insert_gdb_debug_scripts_section_global(ccx: &CrateContext) | |
46 | -> llvm::ValueRef { | |
47 | let section_var_name = "__rustc_debug_gdb_scripts_section__"; | |
48 | ||
49 | let section_var = unsafe { | |
50 | llvm::LLVMGetNamedGlobal(ccx.llmod(), | |
51 | section_var_name.as_ptr() as *const _) | |
52 | }; | |
53 | ||
54 | if section_var == ptr::null_mut() { | |
55 | let section_name = b".debug_gdb_scripts\0"; | |
56 | let section_contents = b"\x01gdb_load_rust_pretty_printers.py\0"; | |
57 | ||
58 | unsafe { | |
59 | let llvm_type = Type::array(&Type::i8(ccx), | |
60 | section_contents.len() as u64); | |
61 | ||
62 | let section_var = declare::define_global(ccx, section_var_name, | |
63 | llvm_type).unwrap_or_else(||{ | |
64 | ccx.sess().bug(&format!("symbol `{}` is already defined", section_var_name)) | |
65 | }); | |
66 | llvm::LLVMSetSection(section_var, section_name.as_ptr() as *const _); | |
67 | llvm::LLVMSetInitializer(section_var, C_bytes(ccx, section_contents)); | |
68 | llvm::LLVMSetGlobalConstant(section_var, llvm::True); | |
69 | llvm::LLVMSetUnnamedAddr(section_var, llvm::True); | |
70 | llvm::SetLinkage(section_var, llvm::Linkage::LinkOnceODRLinkage); | |
71 | // This should make sure that the whole section is not larger than | |
72 | // the string it contains. Otherwise we get a warning from GDB. | |
73 | llvm::LLVMSetAlignment(section_var, 1); | |
74 | section_var | |
75 | } | |
76 | } else { | |
77 | section_var | |
78 | } | |
79 | } | |
80 | ||
81 | pub fn needs_gdb_debug_scripts_section(ccx: &CrateContext) -> bool { | |
82 | let omit_gdb_pretty_printer_section = | |
83 | attr::contains_name(&ccx.tcx() | |
84 | .map | |
85 | .krate() | |
86 | .attrs, | |
87 | "omit_gdb_pretty_printer_section"); | |
88 | ||
89 | !omit_gdb_pretty_printer_section && | |
90 | !ccx.sess().target.target.options.is_like_osx && | |
91 | !ccx.sess().target.target.options.is_like_windows && | |
92 | ccx.sess().opts.debuginfo != NoDebugInfo | |
93 | } |