Skip to content

Commit df8dfde

Browse files
committed
Auto merge of #46457 - m4b:no_mangle_static, r=michaelwoerister
Don't set the linkage_name for static variables For `no_mangle` statics: 1. Linkage_name no longer set 2. The static variable also no longer has a dwarf namespace scope This matches C++ output, which does not set the linkage_name and is not scoped: e.g. c++: ``` 0x000000b6: DW_TAG_base_type [8] DW_AT_name [DW_FORM_strp] ( .debug_str[0x00000077] = "long int") DW_AT_encoding [DW_FORM_data1] (DW_ATE_signed) DW_AT_byte_size [DW_FORM_data1] (0x08) 0x000000bd: DW_TAG_variable [9] DW_AT_name [DW_FORM_strp] ( .debug_str[0x00000053] = "TEST") DW_AT_type [DW_FORM_ref4] (cu + 0x0048 => {0x00000048}) DW_AT_external [DW_FORM_flag_present] (true) DW_AT_decl_file [DW_FORM_data1] ("/home/m4b/tmp/bad_debug/test.cpp") DW_AT_decl_line [DW_FORM_data1] (14) DW_AT_location [DW_FORM_exprloc] (<0x9> 03 40 10 20 00 00 00 00 00 ) 0x000000d2: DW_TAG_namespace [2] * DW_AT_name [DW_FORM_strp] ( .debug_str[0x0000009d] = "std") ``` and (now) Rust: ``` 0x0000002a: DW_TAG_variable [2] DW_AT_name [DW_FORM_strp] ( .debug_str[0x00000046] = "TEST") DW_AT_type [DW_FORM_ref4] (cu + 0x0045 => {0x00000045}) DW_AT_external [DW_FORM_flag_present] (true) DW_AT_decl_file [DW_FORM_data1] ("/tmp/test.rs") DW_AT_decl_line [DW_FORM_data1] (8) DW_AT_alignment [DW_FORM_udata] (1) DW_AT_location [DW_FORM_exprloc] (<0x9> 03 c0 4d 06 00 00 00 00 00 ) 0x00000040: DW_TAG_namespace [3] * DW_AT_name [DW_FORM_strp] ( .debug_str[0x0000004b] = "test") ```
2 parents 16212b9 + 500dc14 commit df8dfde

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/librustc_trans/debuginfo/metadata.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use std::ffi::CString;
4242
use std::fmt::Write;
4343
use std::ptr;
4444
use std::path::{Path, PathBuf};
45-
use syntax::ast;
45+
use syntax::{ast, attr};
4646
use syntax::symbol::{Interner, InternedString, Symbol};
4747
use syntax_pos::{self, Span, FileName};
4848

@@ -1643,8 +1643,10 @@ pub fn create_global_var_metadata(cx: &CrateContext,
16431643
}
16441644

16451645
let tcx = cx.tcx();
1646-
16471646
let node_def_id = tcx.hir.local_def_id(node_id);
1647+
let no_mangle = attr::contains_name(&tcx.get_attrs(node_def_id), "no_mangle");
1648+
// We may want to remove the namespace scope if we're in an extern block, see:
1649+
// https://github.com/rust-lang/rust/pull/46457#issuecomment-351750952
16481650
let var_scope = get_namespace_for_item(cx, node_def_id);
16491651
let span = cx.tcx().def_span(node_def_id);
16501652

@@ -1659,18 +1661,24 @@ pub fn create_global_var_metadata(cx: &CrateContext,
16591661
let variable_type = Instance::mono(cx.tcx(), node_def_id).ty(cx.tcx());
16601662
let type_metadata = type_metadata(cx, variable_type, span);
16611663
let var_name = tcx.item_name(node_def_id).to_string();
1662-
let linkage_name = mangled_name_of_item(cx, node_def_id, "");
1663-
16641664
let var_name = CString::new(var_name).unwrap();
1665-
let linkage_name = CString::new(linkage_name).unwrap();
1665+
let linkage_name = if no_mangle {
1666+
None
1667+
} else {
1668+
let linkage_name = mangled_name_of_item(cx, node_def_id, "");
1669+
Some(CString::new(linkage_name).unwrap())
1670+
};
16661671

16671672
let global_align = cx.align_of(variable_type);
16681673

16691674
unsafe {
16701675
llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx),
16711676
var_scope,
16721677
var_name.as_ptr(),
1673-
linkage_name.as_ptr(),
1678+
// If null, linkage_name field is omitted,
1679+
// which is what we want for no_mangle statics
1680+
linkage_name.as_ref()
1681+
.map_or(ptr::null(), |name| name.as_ptr()),
16741682
file_metadata,
16751683
line_number,
16761684
type_metadata,

0 commit comments

Comments
 (0)