22
33use crate :: consts:: const_alloc_to_llvm;
44pub use crate :: context:: CodegenCx ;
5+ use crate :: debuginfo:: metadata:: {
6+ build_const_str_di_node, build_opaque_pointer_global_var_di_node,
7+ } ;
58use crate :: llvm:: { self , BasicBlock , Bool , ConstantInt , False , OperandBundleDef , True } ;
69use crate :: type_:: Type ;
710use crate :: value:: Value ;
@@ -11,7 +14,9 @@ use rustc_codegen_ssa::traits::*;
1114use rustc_data_structures:: stable_hasher:: { Hash128 , HashStable , StableHasher } ;
1215use rustc_hir:: def_id:: DefId ;
1316use rustc_middle:: bug;
14- use rustc_middle:: mir:: interpret:: { ConstAllocation , GlobalAlloc , Scalar } ;
17+ use rustc_middle:: mir:: interpret:: {
18+ ConstAllocation , ConstAllocationDebugHint , GlobalAlloc , Scalar ,
19+ } ;
1520use rustc_middle:: ty:: TyCtxt ;
1621use rustc_session:: cstore:: { DllCallingConvention , DllImport , PeImportNameType } ;
1722use rustc_target:: abi:: { self , AddressSpace , HasDataLayout , Pointer } ;
@@ -20,6 +25,8 @@ use rustc_target::spec::Target;
2025use libc:: { c_char, c_uint} ;
2126use std:: fmt:: Write ;
2227
28+ const RUST_VERSION : & str = env ! ( "CFG_RELEASE_NUM" ) ;
29+
2330/*
2431* A note on nomenclature of linking: "extern", "foreign", and "upcall".
2532*
@@ -196,15 +203,23 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
196203 . from_key ( s)
197204 . or_insert_with ( || {
198205 let sc = self . const_bytes ( s. as_bytes ( ) ) ;
199- let sym = self . generate_local_symbol_name ( "str" ) ;
206+ let hash = self . tcx . with_stable_hashing_context ( |mut hcx| {
207+ let mut hasher = StableHasher :: new ( ) ;
208+ s. hash_stable ( & mut hcx, & mut hasher) ;
209+ hasher. finish :: < Hash128 > ( )
210+ } ) ;
211+ let sym = format ! ( "__rust_{RUST_VERSION}_conststr_{hash:032x}" ) ;
200212 let g = self . define_global ( & sym, self . val_ty ( sc) ) . unwrap_or_else ( || {
201213 bug ! ( "symbol `{}` is already defined" , sym) ;
202214 } ) ;
203215 unsafe {
204216 llvm:: LLVMSetInitializer ( g, sc) ;
205217 llvm:: LLVMSetGlobalConstant ( g, True ) ;
206- llvm:: LLVMRustSetLinkage ( g, llvm:: Linkage :: InternalLinkage ) ;
218+ llvm:: LLVMRustSetLinkage ( g, llvm:: Linkage :: LinkOnceODRLinkage ) ;
219+ llvm:: SetUniqueComdat ( self . llmod , g) ;
220+ llvm:: LLVMRustSetVisibility ( g, llvm:: Visibility :: Hidden ) ;
207221 }
222+ build_const_str_di_node ( self , & sym, g) ;
208223 ( s. to_owned ( ) , g)
209224 } )
210225 . 1 ;
@@ -249,18 +264,38 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
249264 let ( base_addr, base_addr_space) = match self . tcx . global_alloc ( alloc_id) {
250265 GlobalAlloc :: Memory ( alloc) => {
251266 let init = const_alloc_to_llvm ( self , alloc) ;
267+ let debug_hint = alloc. 1 ;
252268 let alloc = alloc. inner ( ) ;
253269 let value = match alloc. mutability {
254270 Mutability :: Mut => self . static_addr_of_mut ( init, alloc. align , None ) ,
255- _ => self . static_addr_of ( init, alloc. align , None ) ,
271+ _ => {
272+ let value = self . static_addr_of ( init, alloc. align , None ) ;
273+ llvm:: set_linkage ( value, llvm:: Linkage :: LinkOnceODRLinkage ) ;
274+ llvm:: set_visibility ( value, llvm:: Visibility :: Hidden ) ;
275+ value
276+ }
256277 } ;
257- if !self . sess ( ) . fewer_names ( ) && llvm:: get_value_name ( value) . is_empty ( ) {
278+
279+ if llvm:: get_value_name ( value) . is_empty ( ) {
280+ let name_prefix = match debug_hint {
281+ Some ( ConstAllocationDebugHint :: StrLiteral ) => "str" ,
282+ Some ( ConstAllocationDebugHint :: CallerLocation ) => "callerloc" ,
283+ Some ( ConstAllocationDebugHint :: TypeName ) => "typename" ,
284+ Some ( ConstAllocationDebugHint :: VTable ) => "vtable" ,
285+ None => "alloc" ,
286+ } ;
287+
258288 let hash = self . tcx . with_stable_hashing_context ( |mut hcx| {
259289 let mut hasher = StableHasher :: new ( ) ;
260290 alloc. hash_stable ( & mut hcx, & mut hasher) ;
261291 hasher. finish :: < Hash128 > ( )
262292 } ) ;
263- llvm:: set_value_name ( value, format ! ( "alloc_{hash:032x}" ) . as_bytes ( ) ) ;
293+ let name = format ! ( "__rust_{RUST_VERSION}_{name_prefix}_{hash:032x}" ) ;
294+ llvm:: set_value_name ( value, name. as_bytes ( ) ) ;
295+ build_opaque_pointer_global_var_di_node ( self , & name, value) ;
296+ if alloc. mutability == Mutability :: Not {
297+ llvm:: SetUniqueComdat ( self . llmod , value) ;
298+ }
264299 }
265300 ( value, AddressSpace :: DATA )
266301 }
0 commit comments