[llvm] 122d92d - fix symbol printing on windows

Jameson Nash via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 15 14:15:31 PDT 2020


Author: Jameson Nash
Date: 2020-10-15T17:14:55-04:00
New Revision: 122d92dfc31f27263f281244756f576147cec1f6

URL: https://github.com/llvm/llvm-project/commit/122d92dfc31f27263f281244756f576147cec1f6
DIFF: https://github.com/llvm/llvm-project/commit/122d92dfc31f27263f281244756f576147cec1f6.diff

LOG: fix symbol printing on windows

Similar to MCSymbol::print in 3d6c8ebb584375d01b1acead4c2056b3f0c501fc
(llvm-svn: 81682, PR4966), these symbols may need to be quoted to be handled by
the linker correctly.

Reviewed By: compnerd

Differential Revision: https://reviews.llvm.org/D87099

Added: 
    

Modified: 
    llvm/lib/IR/Mangler.cpp
    llvm/test/CodeGen/X86/dllexport.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Mangler.cpp b/llvm/lib/IR/Mangler.cpp
index 218c12ba433f..8536503cb6d8 100644
--- a/llvm/lib/IR/Mangler.cpp
+++ b/llvm/lib/IR/Mangler.cpp
@@ -184,6 +184,26 @@ void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
   getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
 }
 
+// Check if the name needs quotes to be safe for the linker to interpret.
+static bool canBeUnquotedInDirective(char C) {
+  return (C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') ||
+         (C >= '0' && C <= '9') || C == '_' || C == '$' || C == '.' || C == '@';
+}
+
+static bool canBeUnquotedInDirective(StringRef Name) {
+  if (Name.empty())
+    return false;
+
+  // If any of the characters in the string is an unacceptable character, force
+  // quotes.
+  for (char C : Name) {
+    if (!canBeUnquotedInDirective(C))
+      return false;
+  }
+
+  return true;
+}
+
 void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
                                         const Triple &TT, Mangler &Mangler) {
   if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
@@ -194,6 +214,9 @@ void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
   else
     OS << " -export:";
 
+  bool NeedQuotes = GV->hasName() && !canBeUnquotedInDirective(GV->getName());
+  if (NeedQuotes)
+    OS << "\"";
   if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
     std::string Flag;
     raw_string_ostream FlagOS(Flag);
@@ -206,6 +229,8 @@ void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
   } else {
     Mangler.getNameWithPrefix(OS, GV, false);
   }
+  if (NeedQuotes)
+    OS << "\"";
 
   if (!GV->getValueType()->isFunctionTy()) {
     if (TT.isWindowsMSVCEnvironment())
@@ -221,6 +246,11 @@ void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
     return;
 
   OS << " /INCLUDE:";
+  bool NeedQuotes = GV->hasName() && !canBeUnquotedInDirective(GV->getName());
+  if (NeedQuotes)
+    OS << "\"";
   M.getNameWithPrefix(OS, GV, false);
+  if (NeedQuotes)
+    OS << "\"";
 }
 

diff  --git a/llvm/test/CodeGen/X86/dllexport.ll b/llvm/test/CodeGen/X86/dllexport.ll
index a54fea136386..596fd4a3a4bd 100644
--- a/llvm/test/CodeGen/X86/dllexport.ll
+++ b/llvm/test/CodeGen/X86/dllexport.ll
@@ -81,6 +81,9 @@ define weak_odr dllexport void @weak1() {
 ; CHECK: .weak _WeakVar2
 @WeakVar2 = weak_odr dllexport unnamed_addr constant i32 1
 
+; CHECK: .globl "_complex-name"
+@"complex-name" = dllexport global i32 1, align 4
+
 
 ; Verify items that should not be exported do not appear in the export table.
 ; We use a separate check prefix to avoid confusion between -NOT and -SAME.
@@ -102,6 +105,7 @@ define weak_odr dllexport void @weak1() {
 ; CHECK-CL: .ascii " /EXPORT:_Var3,DATA"
 ; CHECK-CL: .ascii " /EXPORT:_WeakVar1,DATA"
 ; CHECK-CL: .ascii " /EXPORT:_WeakVar2,DATA"
+; CHECK-CL: .ascii " /EXPORT:\"_complex-name\",DATA"
 ; CHECK-CL: .ascii " /EXPORT:_alias"
 ; CHECK-CL: .ascii " /EXPORT:_alias2"
 ; CHECK-CL: .ascii " /EXPORT:_alias3"
@@ -119,6 +123,7 @@ define weak_odr dllexport void @weak1() {
 ; CHECK-GCC: .ascii " -export:Var3,data"
 ; CHECK-GCC: .ascii " -export:WeakVar1,data"
 ; CHECK-GCC: .ascii " -export:WeakVar2,data"
+; CHECK-GCC: .ascii " -export:\"complex-name\",data"
 ; CHECK-GCC: .ascii " -export:alias"
 ; CHECK-GCC: .ascii " -export:alias2"
 ; CHECK-GCC: .ascii " -export:alias3"


        


More information about the llvm-commits mailing list