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,57 @@ +## Test llvm-nm demangle symbols for XCOFF object files. +# RUN: llvm-nm --demangle %p/Inputs/test_gcc.o | FileCheck --check-prefix=NM-DEMANGLE %s + +## test_gcc.o is generated by the following source compiled with g++ in aix os. +## int v = 0; +## +## __attribute__((visibility ("protected"))) int vp = 1; +## __attribute__((visibility ("hidden"))) int vh = 2; +## __attribute__((visibility ("default"))) int vd = 3; +## __attribute__ ((weak)) __attribute__((visibility ("hidden"))) int vwh = 4; +## __attribute__ ((weak)) __attribute__((visibility ("protected"))) int vwp = 5; +## +## class C { +## public: +## int c; +## C(int v):c(v) {} +## }; +## +## C cc(2); +## +## static int si = 6; +## +## static int func0 () { +## return vp+si; +## } +## +## int func1 (int i) { +## return func0() * i; +## } +## +## __attribute__ ((weak)) __attribute__((visibility ("hidden"))) +## int fwh() { +## return si+1; +## } +## +## __attribute__ ((weak)) __attribute__((visibility ("protected"))) +## int fwp() { +## return si+2; +## } + +# NM-DEMANGLE: 000001e4 t ._GLOBAL__sub_I_v +# NM-DEMANGLE-NEXT: 000000b0 W .fwh() +# NM-DEMANGLE-NEXT: 000000f4 W .fwp() +# NM-DEMANGLE-NEXT: 00000138 t .__static_initialization_and_destruction_0(int, int) +# NM-DEMANGLE-NEXT: 00000050 T .func1(int) +# NM-DEMANGLE-NEXT: 00000000 t .func0() +# NM-DEMANGLE-NEXT: 00000294 W .C::C(int) +# NM-DEMANGLE-NEXT: 00000294 t .C::C(int) +# NM-DEMANGLE: 00000360 d _GLOBAL__sub_I_v +# NM-DEMANGLE-NEXT: 0000033c W fwh() +# NM-DEMANGLE-NEXT: 00000348 W fwp() +# NM-DEMANGLE-NEXT: 00000354 d __static_initialization_and_destruction_0(int, int) +# NM-DEMANGLE-NEXT: 00000330 D func1(int) +# NM-DEMANGLE-NEXT: 00000330 d func1(int) +# NM-DEMANGLE-NEXT: 0000037c d si +# NM-DEMANGLE-NEXT: 00000324 d func0() +# NM-DEMANGLE-NEXT: 00000310 W C::C(int) 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 @@ -617,10 +617,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; @@ -635,6 +632,23 @@ return S; } +static Optional demangleXCOFF(StringRef Name) { + + if (!Name.empty() && Name[0] == '.') + Name = Name.substr(1); + else + return demangle(Name); + + Optional Opt = demangle(Name); + return (Opt ? "." + *Opt : 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'; } @@ -723,7 +737,12 @@ std::string Name = S.Name; MachOObjectFile *MachO = dyn_cast(&Obj); if (Demangle) { - if (Optional Opt = demangle(S.Name, MachO)) + Optional (*Fn)(StringRef Name) = demangle; + if (Obj.isXCOFF()) + Fn = demangleXCOFF; + if (Obj.isMachO()) + Fn = demangleMachO; + if (Optional Opt = Fn(S.Name)) Name = *Opt; } if (S.Sym.getRawDataRefImpl().p) {