1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 20:51:52 +01:00

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
This commit is contained in:
Jameson Nash 2020-10-15 16:57:21 -04:00
parent cea367bb2a
commit b97e5e20ad
2 changed files with 35 additions and 0 deletions

View File

@ -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 << "\"";
}

View File

@ -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"