[PATCH] D87099: fix symbol printing on windows

Jameson Nash via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 3 10:28:10 PDT 2020


vtjnash created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
vtjnash requested review of this revision.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87099

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


Index: llvm/test/CodeGen/X86/dllexport.ll
===================================================================
--- llvm/test/CodeGen/X86/dllexport.ll
+++ llvm/test/CodeGen/X86/dllexport.ll
@@ -81,6 +81,9 @@
 ; CHECK: .weak _WeakVar2
 @WeakVar2 = weak_odr dllexport unnamed_addr constant i32 1
 
+; CHECK: .globl "_complex-name"
+@"complex-name" = dllexport global i32 1, align 4
+
 
 ; CHECK: .globl _alias
 ; CHECK: .set _alias, _notExported
@@ -118,6 +121,7 @@
 ; 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"
@@ -135,6 +139,7 @@
 ; 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"
Index: llvm/lib/IR/Mangler.cpp
===================================================================
--- llvm/lib/IR/Mangler.cpp
+++ llvm/lib/IR/Mangler.cpp
@@ -184,6 +184,26 @@
   getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
 }
 
+// Check if the name needs quotes to be safe for the linker to interpret.
+static bool isAcceptableChar(char C) {
+  return (C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') ||
+         (C >= '0' && C <= '9') || C == '_' || C == '$' || C == '.' || C == '@';
+}
+
+static bool isValidUnquotedName(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 (!isAcceptableChar(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 @@
   else
     OS << " -export:";
 
+  bool NeedQuotes = GV->hasName() && !isValidUnquotedName(GV->getName());
+  if (NeedQuotes)
+    OS << "\"";
   if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
     std::string Flag;
     raw_string_ostream FlagOS(Flag);
@@ -206,6 +229,8 @@
   } else {
     Mangler.getNameWithPrefix(OS, GV, false);
   }
+  if (NeedQuotes)
+    OS << "\"";
 
   if (!GV->getValueType()->isFunctionTy()) {
     if (TT.isWindowsMSVCEnvironment())
@@ -221,6 +246,11 @@
     return;
 
   OS << " /INCLUDE:";
+  bool NeedQuotes = GV->hasName() && !isValidUnquotedName(GV->getName());
+  if (NeedQuotes)
+    OS << "\"";
   M.getNameWithPrefix(OS, GV, false);
+  if (NeedQuotes)
+    OS << "\"";
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87099.289757.patch
Type: text/x-patch
Size: 2862 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200903/1fae006f/attachment-0001.bin>


More information about the llvm-commits mailing list