@@ -19,8 +19,11 @@ use crate::llvm::AttributePlace::Function;
1919use crate :: type_:: Type ;
2020use crate :: value:: Value ;
2121use rustc_codegen_ssa:: traits:: TypeMembershipMethods ;
22- use rustc_middle:: ty:: Ty ;
23- use rustc_symbol_mangling:: typeid:: { kcfi_typeid_for_fnabi, typeid_for_fnabi, TypeIdOptions } ;
22+ use rustc_middle:: ty:: { Instance , Ty } ;
23+ use rustc_symbol_mangling:: typeid:: {
24+ kcfi_typeid_for_fnabi, kcfi_typeid_for_instance, typeid_for_fnabi, typeid_for_instance,
25+ TypeIdOptions ,
26+ } ;
2427use smallvec:: SmallVec ;
2528
2629/// Declare a function.
@@ -116,7 +119,12 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
116119 ///
117120 /// If there’s a value with the same name already declared, the function will
118121 /// update the declaration and return existing Value instead.
119- pub fn declare_fn ( & self , name : & str , fn_abi : & FnAbi < ' tcx , Ty < ' tcx > > ) -> & ' ll Value {
122+ pub fn declare_fn (
123+ & self ,
124+ name : & str ,
125+ fn_abi : & FnAbi < ' tcx , Ty < ' tcx > > ,
126+ instance : Option < Instance < ' tcx > > ,
127+ ) -> & ' ll Value {
120128 debug ! ( "declare_rust_fn(name={:?}, fn_abi={:?})" , name, fn_abi) ;
121129
122130 // Function addresses in Rust are never significant, allowing functions to
@@ -132,18 +140,35 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
132140 fn_abi. apply_attrs_llfn ( self , llfn) ;
133141
134142 if self . tcx . sess . is_sanitizer_cfi_enabled ( ) {
135- let typeid = typeid_for_fnabi ( self . tcx , fn_abi, TypeIdOptions :: empty ( ) ) ;
136- self . set_type_metadata ( llfn, typeid) ;
137- let typeid = typeid_for_fnabi ( self . tcx , fn_abi, TypeIdOptions :: GENERALIZE_POINTERS ) ;
138- self . add_type_metadata ( llfn, typeid) ;
139- let typeid = typeid_for_fnabi ( self . tcx , fn_abi, TypeIdOptions :: NORMALIZE_INTEGERS ) ;
140- self . add_type_metadata ( llfn, typeid) ;
141- let typeid = typeid_for_fnabi (
142- self . tcx ,
143- fn_abi,
144- TypeIdOptions :: GENERALIZE_POINTERS | TypeIdOptions :: NORMALIZE_INTEGERS ,
145- ) ;
146- self . add_type_metadata ( llfn, typeid) ;
143+ if let Some ( instance) = instance {
144+ let typeid = typeid_for_instance ( self . tcx , & instance, TypeIdOptions :: empty ( ) ) ;
145+ self . set_type_metadata ( llfn, typeid) ;
146+ let typeid =
147+ typeid_for_instance ( self . tcx , & instance, TypeIdOptions :: GENERALIZE_POINTERS ) ;
148+ self . add_type_metadata ( llfn, typeid) ;
149+ let typeid =
150+ typeid_for_instance ( self . tcx , & instance, TypeIdOptions :: NORMALIZE_INTEGERS ) ;
151+ self . add_type_metadata ( llfn, typeid) ;
152+ let typeid = typeid_for_instance (
153+ self . tcx ,
154+ & instance,
155+ TypeIdOptions :: GENERALIZE_POINTERS | TypeIdOptions :: NORMALIZE_INTEGERS ,
156+ ) ;
157+ self . add_type_metadata ( llfn, typeid) ;
158+ } else {
159+ let typeid = typeid_for_fnabi ( self . tcx , fn_abi, TypeIdOptions :: empty ( ) ) ;
160+ self . set_type_metadata ( llfn, typeid) ;
161+ let typeid = typeid_for_fnabi ( self . tcx , fn_abi, TypeIdOptions :: GENERALIZE_POINTERS ) ;
162+ self . add_type_metadata ( llfn, typeid) ;
163+ let typeid = typeid_for_fnabi ( self . tcx , fn_abi, TypeIdOptions :: NORMALIZE_INTEGERS ) ;
164+ self . add_type_metadata ( llfn, typeid) ;
165+ let typeid = typeid_for_fnabi (
166+ self . tcx ,
167+ fn_abi,
168+ TypeIdOptions :: GENERALIZE_POINTERS | TypeIdOptions :: NORMALIZE_INTEGERS ,
169+ ) ;
170+ self . add_type_metadata ( llfn, typeid) ;
171+ }
147172 }
148173
149174 if self . tcx . sess . is_sanitizer_kcfi_enabled ( ) {
@@ -156,8 +181,13 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
156181 options. insert ( TypeIdOptions :: NORMALIZE_INTEGERS ) ;
157182 }
158183
159- let kcfi_typeid = kcfi_typeid_for_fnabi ( self . tcx , fn_abi, options) ;
160- self . set_kcfi_type_metadata ( llfn, kcfi_typeid) ;
184+ if let Some ( instance) = instance {
185+ let kcfi_typeid = kcfi_typeid_for_instance ( self . tcx , & instance, options) ;
186+ self . set_kcfi_type_metadata ( llfn, kcfi_typeid) ;
187+ } else {
188+ let kcfi_typeid = kcfi_typeid_for_fnabi ( self . tcx , fn_abi, options) ;
189+ self . set_kcfi_type_metadata ( llfn, kcfi_typeid) ;
190+ }
161191 }
162192
163193 llfn
0 commit comments