Index: test/tools/llvm-objdump/disassemble-demangle.test =================================================================== --- /dev/null +++ test/tools/llvm-objdump/disassemble-demangle.test @@ -0,0 +1,48 @@ +# REQUIRES: x86 +# RUN: yaml2obj %s > %t +# RUN: llvm-objdump -d -C %t | FileCheck --check-prefix=DEMANGLE %s +# RUN: llvm-objdump -d --demangle=itanium %t | FileCheck --check-prefix=DEMANGLE %s +# RUN: llvm-objdump -d %t | FileCheck --check-prefix=NO-DEMANGLE %s +# RUN: llvm-objdump -d --demangle=none %t | FileCheck --check-prefix=NO-DEMANGLE %s +# RUN: llvm-objdump -d -C=wrong-style %t 2>&1 | FileCheck --check-prefix=BAD-STYLE %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .text1 + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x1000 + AddressAlign: 0x0000000000000010 + Content: "0000000000000000" + - Name: .text2 + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x1010 + AddressAlign: 0x0000000000000010 + Content: "0000000000000000" +Symbols: + Local: + - Name: _Z3fooi + Type: STT_FUNC + Section: .text1 + Value: 0x1000 + - Name: _Z3foov + Type: STT_FUNC + Section: .text2 + Value: 0x1010 + +# We just want to check that the symbols are demangled +# DEMANGLE: foo(int) +# DEMANGLE: foo() + +# NO-DEMANGLE: _Z3fooi +# NO-DEMANGLE: _Z3foov + +# BAD-STYLE: warning: Unsupported demangling style. +# BAD-STYLE: _Z3fooi +# BAD-STYLE: _Z3foov Index: tools/llvm-objdump/llvm-objdump.h =================================================================== --- tools/llvm-objdump/llvm-objdump.h +++ tools/llvm-objdump/llvm-objdump.h @@ -30,6 +30,7 @@ extern cl::opt TripleName; extern cl::opt ArchName; extern cl::opt MCPU; +extern cl::opt Demangle; extern cl::list MAttrs; extern cl::list FilterSections; extern cl::opt AllHeaders; @@ -96,6 +97,7 @@ void PrintSectionContents(const object::ObjectFile *o); void PrintSymbolTable(const object::ObjectFile *o, StringRef ArchiveName, StringRef ArchitectureName = StringRef()); +void warn(StringRef Message); LLVM_ATTRIBUTE_NORETURN void error(Twine Message); LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message); LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC); Index: tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- tools/llvm-objdump/llvm-objdump.cpp +++ tools/llvm-objdump/llvm-objdump.cpp @@ -25,6 +25,7 @@ #include "llvm/CodeGen/FaultMaps.h" #include "llvm/DebugInfo/DWARF/DWARFContext.h" #include "llvm/DebugInfo/Symbolize/Symbolize.h" +#include "llvm/Demangle/Demangle.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCDisassembler/MCDisassembler.h" @@ -90,6 +91,13 @@ DisassembleAlld("D", cl::desc("Alias for --disassemble-all"), cl::aliasopt(DisassembleAll)); +cl::opt llvm::Demangle("demangle", + cl::desc("Demangle symbols names"), + cl::ValueOptional, cl::init("none")); + +static cl::alias DemangleShort("C", cl::desc("Alias for --demangle"), + cl::aliasopt(Demangle)); + static cl::list DisassembleFunctions("df", cl::CommaSeparated, @@ -330,6 +338,11 @@ exit(1); } +void llvm::warn(StringRef Message) { + errs() << ToolName << ": warning: " << Message << ".\n"; + errs().flush(); +} + LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, Twine Message) { errs() << ToolName << ": '" << File << "': " << Message << ".\n"; @@ -1511,7 +1524,25 @@ } } - outs() << '\n' << std::get<1>(Symbols[si]) << ":\n"; + auto PrintSymbol = [](StringRef Name) { + outs() << '\n' << Name << ":\n"; + }; + StringRef SymbolName = std::get<1>(Symbols[si]); + if (Demangle.getValue() == "" || Demangle.getValue() == "itanium") { + char *DemangledSymbol = nullptr; + size_t Size = 0; + int Status; + DemangledSymbol = + itaniumDemangle(SymbolName.data(), DemangledSymbol, &Size, &Status); + if (Status == 0) + PrintSymbol(StringRef(DemangledSymbol)); + else + PrintSymbol(SymbolName); + + if (Size != 0) + free(DemangledSymbol); + } else + PrintSymbol(SymbolName); // Don't print raw contents of a virtual section. A virtual section // doesn't have any contents in the file. @@ -2360,6 +2391,11 @@ if (DisassembleAll || PrintSource || PrintLines) Disassemble = true; + + if (Demangle.getValue() != "none" && Demangle.getValue() != "" && + Demangle.getValue() != "itanium") + warn("Unsupported demangling style"); + if (!Disassemble && !Relocations && !DynamicRelocations