diff --git a/llvm/test/tools/llvm-nm/XCOFF/demangle_sym_name.test b/llvm/test/tools/llvm-nm/XCOFF/demangle_sym_name.test new file mode 100644 --- /dev/null +++ b/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 diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp --- a/llvm/tools/llvm-nm/llvm-nm.cpp +++ b/llvm/tools/llvm-nm/llvm-nm.cpp @@ -619,10 +619,7 @@ outs() << format(" %02x", NType); } -static Optional demangle(StringRef Name, bool StripUnderscore) { - if (StripUnderscore && !Name.empty() && Name[0] == '_') - Name = Name.substr(1); - +static Optional demangle(StringRef Name) { if (!Name.startswith("_Z")) return None; @@ -637,6 +634,24 @@ return S; } +static Optional demangleXCOFF(StringRef Name) { + if (!Name.empty() && Name[0] == '.') + Name = Name.drop_front(); + else + return demangle(Name); + + Optional DemangledName = demangle(Name); + if (DemangledName) + return "." + *DemangledName; + return None; +} + +static Optional 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(&Obj); if (Demangle) { - if (Optional Opt = demangle(S.Name, MachO)) + function_ref(StringRef)> Fn = ::demangle; + if (Obj.isXCOFF()) + Fn = demangleXCOFF; + if (Obj.isMachO()) + Fn = demangleMachO; + if (Optional Opt = Fn(S.Name)) Name = *Opt; } if (S.Sym.getRawDataRefImpl().p) {