@@ -414,7 +414,7 @@ impl<'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
414414 llvm:: LLVMRustGetMangledName ( llval, s) ;
415415 } )
416416 . expect ( "symbol is not valid UTF-8" ) ;
417- template_str. push_str ( & symbol) ;
417+ template_str. push_str ( & escape_symbol_name ( self , symbol) ) ;
418418 }
419419 GlobalAsmOperandRef :: SymStatic { def_id } => {
420420 let llval = self
@@ -428,7 +428,7 @@ impl<'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
428428 llvm:: LLVMRustGetMangledName ( llval, s) ;
429429 } )
430430 . expect ( "symbol is not valid UTF-8" ) ;
431- template_str. push_str ( & symbol) ;
431+ template_str. push_str ( & escape_symbol_name ( self , symbol) ) ;
432432 }
433433 }
434434 }
@@ -1390,3 +1390,36 @@ fn llvm_fixup_output_type<'ll, 'tcx>(
13901390 _ => layout. llvm_type ( cx) ,
13911391 }
13921392}
1393+
1394+ fn escape_symbol_name < ' ll , ' tcx > ( cx : & CodegenCx < ' ll , ' tcx > , symbol : String ) -> String {
1395+ use rustc_target:: spec:: { Arch , BinaryFormat } ;
1396+ if !symbol. is_empty ( )
1397+ && symbol. chars ( ) . all ( |c| matches ! ( c, '0' ..='9' | 'A' ..='Z' | 'a' ..='z' | '_' | '$' | '.' ) )
1398+ {
1399+ return symbol;
1400+ }
1401+ if cx. tcx . sess . target . binary_format == BinaryFormat :: Xcoff {
1402+ cx. tcx . sess . dcx ( ) . fatal ( format ! (
1403+ "symbol escaping is not supported for the binary format {}" ,
1404+ cx. tcx. sess. target. binary_format
1405+ ) ) ;
1406+ }
1407+ if matches ! ( cx. tcx. sess. target. arch, Arch :: Nvptx64 ) {
1408+ cx. tcx . sess . dcx ( ) . fatal ( format ! (
1409+ "symbol escaping is not supported for the architecture {}" ,
1410+ cx. tcx. sess. target. arch
1411+ ) ) ;
1412+ }
1413+ let mut escaped_symbol = String :: new ( ) ;
1414+ escaped_symbol. push ( '\"' ) ;
1415+ for c in symbol. chars ( ) {
1416+ match c {
1417+ '\n' => escaped_symbol. push_str ( "\\ \n " ) ,
1418+ '"' => escaped_symbol. push_str ( "\\ \" " ) ,
1419+ '\\' => escaped_symbol. push_str ( "\\ \\ " ) ,
1420+ c => escaped_symbol. push ( c) ,
1421+ }
1422+ }
1423+ escaped_symbol. push ( '\"' ) ;
1424+ escaped_symbol
1425+ }
0 commit comments