11use std:: collections:: hash_map:: Entry :: * ;
22
33use object:: pe;
4- use rustc_abi:: CanonAbi ;
4+ use rustc_abi:: { CanonAbi , X86Call } ;
55use rustc_ast:: expand:: allocator:: { AllocatorKind , NO_ALLOC_SHIM_IS_UNSTABLE , global_fn_name} ;
66use rustc_crate_store:: CrateDepKind ;
77use rustc_data_structures:: fx:: FxHashSet ;
@@ -758,31 +758,9 @@ fn calling_convention_for_symbol<'tcx>(
758758 . unwrap_or ( ( CanonAbi :: Rust , & [ ] ) )
759759}
760760
761- /// Names used to preserve an exported symbol during LTO.
762- ///
763- /// The restriction pass matches LLVM IR names, which retain the `\x01`
764- /// marker for explicitly decorated symbols. ThinLTO GUIDs instead use
765- /// the identifier without that marker, so preserve both spellings.
766- pub ( crate ) fn symbol_names_for_lto < ' tcx > (
767- tcx : TyCtxt < ' tcx > ,
768- symbol : ExportedSymbol < ' tcx > ,
769- instantiating_crate : CrateNum ,
770- ) -> Vec < String > {
771- let name = symbol_name_for_instance_in_crate ( tcx, symbol, instantiating_crate) ;
772- let ( callconv, args) = calling_convention_for_symbol ( tcx, symbol) ;
773-
774- if let Some ( decorated) =
775- crate :: common:: windows_calling_convention_name ( & tcx. sess . target , callconv, & name, args)
776- {
777- vec ! [ format!( "\x01 {decorated}" ) , decorated]
778- } else {
779- vec ! [ name]
780- }
781- }
782761/// This is the symbol name of the given instance as seen by the linker.
783762///
784- /// On Windows, symbols are decorated according to their calling conventions.
785- /// Vectorcall symbols are also decorated on non-Windows targets.
763+ /// On 32-bit Windows symbols are decorated according to their calling conventions.
786764pub ( crate ) fn linking_symbol_name_for_instance_in_crate < ' tcx > (
787765 tcx : TyCtxt < ' tcx > ,
788766 symbol : ExportedSymbol < ' tcx > ,
@@ -799,15 +777,8 @@ pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>(
799777
800778 let target = & tcx. sess . target ;
801779 if !target. is_like_windows {
802- let ( callconv, args) = calling_convention_for_symbol ( tcx, symbol) ;
803- if let Some ( name) =
804- crate :: common:: windows_calling_convention_name ( target, callconv, & undecorated, args)
805- {
806- return name;
807- }
808-
809780 // Mach-O has a global "_" suffix and `object` crate will handle it.
810- // Other calling conventions do not need decoration here .
781+ // ELF does not have any symbol decorations .
811782 return undecorated;
812783 }
813784
@@ -820,26 +791,27 @@ pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>(
820791 _ => return undecorated,
821792 } ;
822793
823- // LLVM treats names beginning with '\x01' as already mangled and strips that prefix.
824- // Names beginning with '?' are left undecorated by LLVM; the prefix is kept.
825- if let Some ( name) = undecorated. strip_prefix ( '\x01' ) {
826- return name. to_string ( ) ;
827- }
828- if undecorated. starts_with ( '?' ) {
829- return undecorated;
830- }
831794 let ( callconv, args) = calling_convention_for_symbol ( tcx, symbol) ;
832795
833- if let Some ( name) =
834- crate :: common:: windows_calling_convention_name ( target, callconv, & undecorated, args)
835- {
836- return name;
837- }
796+ // Decorate symbols with prefixes, suffixes and total number of bytes of arguments.
797+ // Reference: https://docs.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170
798+ let ( prefix, suffix) = match callconv {
799+ CanonAbi :: X86 ( X86Call :: Fastcall ) => ( "@" , "@" ) ,
800+ CanonAbi :: X86 ( X86Call :: Stdcall ) => ( "_" , "@" ) ,
801+ CanonAbi :: X86 ( X86Call :: Vectorcall ) => ( "" , "@@" ) ,
802+ _ => {
803+ if let Some ( prefix) = prefix {
804+ undecorated. insert ( 0 , prefix) ;
805+ }
806+ return undecorated;
807+ }
808+ } ;
838809
839- if let Some ( prefix) = prefix {
840- undecorated. insert ( 0 , prefix) ;
841- }
842- undecorated
810+ let args_in_bytes: u64 = args
811+ . iter ( )
812+ . map ( |abi| abi. layout . size . bytes ( ) . next_multiple_of ( target. pointer_width as u64 / 8 ) )
813+ . sum ( ) ;
814+ format ! ( "{prefix}{undecorated}{suffix}{args_in_bytes}" )
843815}
844816
845817pub ( crate ) fn exporting_symbol_name_for_instance_in_crate < ' tcx > (
0 commit comments