Index: llvm/trunk/test/tools/llvm-objdump/X86/demangle.s =================================================================== --- llvm/trunk/test/tools/llvm-objdump/X86/demangle.s +++ llvm/trunk/test/tools/llvm-objdump/X86/demangle.s @@ -0,0 +1,23 @@ +# RUN: llvm-mc %s -filetype=obj -triple=x86_64-pc-linux -o %t +# RUN: llvm-objdump -syms -reloc -demangle %t | FileCheck %s + +## Check we demangle symbols when printing relocations. +# CHECK: 000000000000001 R_X86_64_PLT32 foo()-4 + +## Check we demangle symbols when printing symbol table. +# CHECK: SYMBOL TABLE: +# CHECK-NEXT: 0000000000000000 *UND* 00000000 +# CHECK-NEXT: 0000000000000000 g F .text 00000000 foo() + +## Check the case when relocations are inlined into disassembly. +# RUN: llvm-objdump -d -r -demangle %t | FileCheck %s --check-prefix=INLINE +# INLINE: Disassembly of section .text: +# INLINE-NEXT: foo(): +# INLINE-NEXT: 0: {{.*}} callq 0 <_Z3foov+0x5> +# INLINE-NEXT: 0000000000000001: R_X86_64_PLT32 foo()-4 + +.text +.globl _Z3foov +.type _Z3foov,@function +_Z3foov: + callq _Z3foov@PLT Index: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp +++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp @@ -504,7 +504,7 @@ break; } } - StringRef Target; + std::string Target; if (!undef) { symbol_iterator SI = RelRef.getSymbol(); const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl()); @@ -521,7 +521,7 @@ Expected SymName = symb->getName(StrTab); if (!SymName) return errorToErrorCode(SymName.takeError()); - Target = *SymName; + Target = Demangle ? demangle(*SymName) : *SymName; } } else Target = "*ABS*"; @@ -1280,6 +1280,22 @@ } } +static std::string demangle(StringRef Name) { + char *Demangled = nullptr; + size_t Size = 0; + if (Name.startswith("_Z")) + Demangled = itaniumDemangle(Name.data(), Demangled, &Size, nullptr); + else if (Name.startswith("?")) + Demangled = microsoftDemangle(Name.data(), Demangled, &Size, nullptr); + + if (!Demangled) + return Name; + + std::string Ret = Demangled; + free(Demangled); + return Ret; +} + static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { if (StartAddress > StopAddress) error("Start address should be less than stop address"); @@ -1573,30 +1589,12 @@ } } - auto PrintSymbol = [](StringRef Name) { - outs() << '\n' << Name << ":\n"; - }; + outs() << '\n'; StringRef SymbolName = std::get<1>(Symbols[si]); - if (Demangle) { - char *DemangledSymbol = nullptr; - size_t Size = 0; - int Status = -1; - if (SymbolName.startswith("_Z")) - DemangledSymbol = itaniumDemangle(SymbolName.data(), DemangledSymbol, - &Size, &Status); - else if (SymbolName.startswith("?")) - DemangledSymbol = microsoftDemangle(SymbolName.data(), - DemangledSymbol, &Size, &Status); - - if (Status == 0 && DemangledSymbol) - PrintSymbol(StringRef(DemangledSymbol)); - else - PrintSymbol(SymbolName); - - if (DemangledSymbol) - free(DemangledSymbol); - } else - PrintSymbol(SymbolName); + if (Demangle) + outs() << demangle(SymbolName) << ":\n"; + else + outs() << SymbolName << ":\n"; // Don't print raw contents of a virtual section. A virtual section // doesn't have any contents in the file. @@ -2061,8 +2059,11 @@ if (Hidden) { outs() << ".hidden "; } - outs() << Name - << '\n'; + + if (Demangle) + outs() << demangle(Name) << '\n'; + else + outs() << Name << '\n'; } }