[PATCH] D113106: demangle xcoff label symbol for llvm-nm
Digger Lin via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 16 08:17:38 PST 2021
DiggerLin updated this revision to Diff 394887.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D113106/new/
https://reviews.llvm.org/D113106
Files:
llvm/test/tools/llvm-nm/XCOFF/demangle_sym_name.test
llvm/tools/llvm-nm/llvm-nm.cpp
Index: llvm/tools/llvm-nm/llvm-nm.cpp
===================================================================
--- llvm/tools/llvm-nm/llvm-nm.cpp
+++ llvm/tools/llvm-nm/llvm-nm.cpp
@@ -619,10 +619,7 @@
outs() << format(" %02x", NType);
}
-static Optional<std::string> demangle(StringRef Name, bool StripUnderscore) {
- if (StripUnderscore && !Name.empty() && Name[0] == '_')
- Name = Name.substr(1);
-
+static Optional<std::string> demangle(StringRef Name) {
if (!Name.startswith("_Z"))
return None;
@@ -637,6 +634,24 @@
return S;
}
+static Optional<std::string> demangleXCOFF(StringRef Name) {
+ if (!Name.empty() && Name[0] == '.')
+ Name = Name.drop_front();
+ else
+ return demangle(Name);
+
+ Optional<std::string> DemangledName = demangle(Name);
+ if (DemangledName)
+ return "." + *DemangledName;
+ return None;
+}
+
+static Optional<std::string> demangleMachO(StringRef Name) {
+ if (!Name.empty() && Name[0] == '_')
+ Name = Name.drop_front();
+ return demangle(Name);
+}
+
static bool symbolIsDefined(const NMSymbol &Sym) {
return Sym.TypeChar != 'U' && Sym.TypeChar != 'w' && Sym.TypeChar != 'v';
}
@@ -725,7 +740,12 @@
std::string Name = S.Name;
MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
if (Demangle) {
- if (Optional<std::string> Opt = demangle(S.Name, MachO))
+ function_ref<Optional<std::string>(StringRef)> Fn = ::demangle;
+ if (Obj.isXCOFF())
+ Fn = demangleXCOFF;
+ if (Obj.isMachO())
+ Fn = demangleMachO;
+ if (Optional<std::string> Opt = Fn(S.Name))
Name = *Opt;
}
if (S.Sym.getRawDataRefImpl().p) {
Index: llvm/test/tools/llvm-nm/XCOFF/demangle_sym_name.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-nm/XCOFF/demangle_sym_name.test
@@ -0,0 +1,40 @@
+## Test llvm-nm demangling of symbols for XCOFF object files.
+## Test following symbols:
+## 1. Empty name
+## 2. Name consisting solely of .
+## 3. Name starting with . that can't be demangled
+## 4. Name starting with . that can be demangled
+## 5. Name not starting with . that can't be demangled
+## 6. Name not starting with . that can be demangled
+
+# RUN: yaml2obj --docnum=1 %s -o %t.o
+# RUN: llvm-nm --demangle --format=just-symbols %t.o 2>&1 | FileCheck %s --check-prefix=NM-DEMANGLE --match-full-lines
+
+# NM-DEMANGLE: {{$}}
+# NM-DEMANGLE-NEXT: .
+# NM-DEMANGLE-NEXT: ._5func1i
+# NM-DEMANGLE-NEXT: .func1(int)
+# NM-DEMANGLE-NEXT: L5func0v
+# NM-DEMANGLE-NEXT: func0()
+
+--- !XCOFF
+FileHeader:
+ MagicNumber: 0x1DF
+Sections:
+ - Name: .text
+ Flags: [ STYP_TEXT ]
+ - Name: .data
+ Flags: [ STYP_DATA ]
+Symbols:
+ - Name: ""
+ Section: .text
+ - Name: .
+ Section: .text
+ - Name: ._Z5func1i
+ Section: .text
+ - Name: ._5func1i
+ Section: .text
+ - Name: _ZL5func0v
+ Section: .data
+ - Name: L5func0v
+ Section: .data
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113106.394887.patch
Type: text/x-patch
Size: 3133 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211216/0222a020/attachment.bin>
More information about the llvm-commits
mailing list