diff --git a/llvm/test/tools/llvm-nm/XCOFF/demangle.test b/llvm/test/tools/llvm-nm/XCOFF/demangle.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-nm/XCOFF/demangle.test @@ -0,0 +1,31 @@ +## Test llvm-nm demangling of symbols for XCOFF object files. +# RUN: llvm-nm --demangle %p/Inputs/test_xlclang.o | FileCheck --check-prefix=NM-DEMANGLE %s + +## test_xlclang.o is generated by the following source compiled with IBM xlclang++ in aix os. +## int value = 0; +## +## __attribute__ ((weak)) int weak_value = 5; +## +## static int func0 () { +## return value; +## } +## +## int func1 (int i) { +## return func0() * i; +## } + +# NM-DEMANGLE: 00000000 t +# NM-DEMANGLE-NEXT: 00000040 T .func1(int) +# NM-DEMANGLE-NEXT: 00000000 t .func0() +# NM-DEMANGLE-NEXT: 00000120 b .bss +# NM-DEMANGLE-NEXT: 00000100 d .data +# NM-DEMANGLE-NEXT: 00000000 f .file +# NM-DEMANGLE-NEXT: 00000000 t .text +# NM-DEMANGLE-NEXT: 00000100 d TOC +# NM-DEMANGLE-NEXT: 00000104 d func1(int) +# NM-DEMANGLE-NEXT: 0000010c D func1(int) +# NM-DEMANGLE-NEXT: 00000100 d value +# NM-DEMANGLE-NEXT: 00000118 D value +# NM-DEMANGLE-NEXT: 00000108 d weak_value +# NM-DEMANGLE-NEXT: 0000011c W weak_value +~ 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.substr(1); + 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.substr(1); + 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) {