Skip to content

Commit

Permalink
[Symbolize][MinGW] Support demangling i386 call-conv-decorated C++ names
Browse files Browse the repository at this point in the history
On i386 Windows, after C++ names have been Itanium-mangled, the C name
mangling specific to its call convention may also be applied on top.
This change teaches symbolizer to be able to demangle this type of
mangled names.

As part of this change, `demanglePE32ExternCFunc` has also been modified
to fix unwanted stripping for vectorcall names when the demangled name
is supposed to contain a leading `_`. Notice that the vectorcall
mangling does not add either an `_` or `@` prefix. The old code always
tries to strip the prefix first, which for Itanium mangled names in
vectorcall, the leading underscore of the Itanium name gets stripped
instead and breaks the Itanium demangler.

Differential Revision: https://reviews.llvm.org/D144049
  • Loading branch information
alvinhochun committed Feb 15, 2023
1 parent 14fcdd7 commit e117fd2
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 7 deletions.
27 changes: 20 additions & 7 deletions llvm/lib/DebugInfo/Symbolize/Symbolize.cpp
Expand Up @@ -649,22 +649,29 @@ namespace {
// vectorcall - foo@@12
// These are all different linkage names for 'foo'.
StringRef demanglePE32ExternCFunc(StringRef SymbolName) {
// Remove any '_' or '@' prefix.
char Front = SymbolName.empty() ? '\0' : SymbolName[0];
if (Front == '_' || Front == '@')
SymbolName = SymbolName.drop_front();

// Remove any '@[0-9]+' suffix.
bool HasAtNumSuffix = false;
if (Front != '?') {
size_t AtPos = SymbolName.rfind('@');
if (AtPos != StringRef::npos &&
all_of(drop_begin(SymbolName, AtPos + 1), isDigit))
all_of(drop_begin(SymbolName, AtPos + 1), isDigit)) {
SymbolName = SymbolName.substr(0, AtPos);
HasAtNumSuffix = true;
}
}

// Remove any ending '@' for vectorcall.
if (SymbolName.endswith("@"))
bool IsVectorCall = false;
if (HasAtNumSuffix && SymbolName.endswith("@")) {
SymbolName = SymbolName.drop_back();
IsVectorCall = true;
}

// If not vectorcall, remove any '_' or '@' prefix.
if (!IsVectorCall && (Front == '_' || Front == '@'))
SymbolName = SymbolName.drop_front();

return SymbolName;
}
Expand Down Expand Up @@ -692,8 +699,14 @@ LLVMSymbolizer::DemangleName(const std::string &Name,
return Result;
}

if (DbiModuleDescriptor && DbiModuleDescriptor->isWin32Module())
return std::string(demanglePE32ExternCFunc(Name));
if (DbiModuleDescriptor && DbiModuleDescriptor->isWin32Module()) {
std::string DemangledCName(demanglePE32ExternCFunc(Name));
// On i386 Windows, the C name mangling for different calling conventions
// may also be applied on top of the Itanium or Rust name mangling.
if (nonMicrosoftDemangle(DemangledCName.c_str(), Result))
return Result;
return DemangledCName;
}
return Name;
}

Expand Down
124 changes: 124 additions & 0 deletions llvm/test/DebugInfo/symbolize-demangling-mingw32.s
@@ -0,0 +1,124 @@
# REQUIRES: x86-registered-target

# RUN: llvm-mc --filetype=obj --triple=i386-w64-windows-gnu %s -o %t.o -g

# RUN: llvm-symbolizer --obj=%t.o 0 1 2 3 4 5 6 7 8 9 10 | FileCheck %s

.def g
.scl 2
.type 32
.endef
g:
nop
# CHECK: {{^g$}}
# CHECK-NEXT: symbolize-demangling-mingw32.s:12
# CHECK-EMPTY:

.def baz
.scl 2
.type 32
.endef
baz:
nop
# CHECK-NEXT: {{^baz$}}
# CHECK-NEXT: symbolize-demangling-mingw32.s:22
# CHECK-EMPTY:

# extern "C" void c() {} // __cdecl
.def _c
.scl 2
.type 32
.endef
_c:
nop
# CHECK-NEXT: {{^c$}}
# CHECK-NEXT: symbolize-demangling-mingw32.s:33
# CHECK-EMPTY:

# extern "C" void __stdcall c1() {}
.def _c1@0
.scl 2
.type 32
.endef
_c1@0:
nop
# CHECK-NEXT: {{^c1$}}
# CHECK-NEXT: symbolize-demangling-mingw32.s:44
# CHECK-EMPTY:

# extern "C" void __fastcall c2(void) {}
.def @c2@0
.scl 2
.type 32
.endef
@c2@0:
nop
# CHECK-NEXT: {{^c2$}}
# CHECK-NEXT: symbolize-demangling-mingw32.s:55
# CHECK-EMPTY:

# extern "C" void __vectorcall c3(void) {}
.def c3@@0
.scl 2
.type 32
.endef
c3@@0:
nop
# CHECK-NEXT: {{^c3$}}
# CHECK-NEXT: symbolize-demangling-mingw32.s:66
# CHECK-EMPTY:

# void f() {} // __cdecl
.def __Z1fv
.scl 2
.type 32
.endef
__Z1fv:
nop
# CHECK-NEXT: {{^f\(\)$}}
# CHECK-NEXT: symbolize-demangling-mingw32.s:77
# CHECK-EMPTY:

# void __stdcall f1() {}
.def __Z2f1v@0
.scl 2
.type 32
.endef
__Z2f1v@0:
nop
# CHECK-NEXT: {{^f1\(\)$}}
# CHECK-NEXT: symbolize-demangling-mingw32.s:88
# CHECK-EMPTY:

# void __fastcall f2(void) {}
.def @_Z2f2v@0
.scl 2
.type 32
.endef
@_Z2f2v@0:
nop
# CHECK-NEXT: {{^f2\(\)$}}
# CHECK-NEXT: symbolize-demangling-mingw32.s:99
# CHECK-EMPTY:

# void __vectorcall f3(void) {}
.def _Z2f3v@@0
.scl 2
.type 32
.endef
_Z2f3v@@0:
nop
# CHECK-NEXT: {{^f3\(\)$}}
# CHECK-NEXT: symbolize-demangling-mingw32.s:110
# CHECK-EMPTY:

# Rust
.def __RNvC1x1y
.scl 2
.type 32
.endef
__RNvC1x1y:
nop
# CHECK-NEXT: {{^x::y$}}
# CHECK-NEXT: symbolize-demangling-mingw32.s:121
# CHECK-EMPTY:

0 comments on commit e117fd2

Please sign in to comment.