[llvm] r337401 - [llvm-objdump] Add -demangle (-C) option
Paul Semel via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 18 09:39:21 PDT 2018
Author: paulsemel
Date: Wed Jul 18 09:39:21 2018
New Revision: 337401
URL: http://llvm.org/viewvc/llvm-project?rev=337401&view=rev
Log:
[llvm-objdump] Add -demangle (-C) option
Differential Revision: https://reviews.llvm.org/D49043
Added:
llvm/trunk/test/tools/llvm-objdump/X86/disassemble-demangle.test
Modified:
llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
llvm/trunk/tools/llvm-objdump/llvm-objdump.h
Added: llvm/trunk/test/tools/llvm-objdump/X86/disassemble-demangle.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/X86/disassemble-demangle.test?rev=337401&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/X86/disassemble-demangle.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/X86/disassemble-demangle.test Wed Jul 18 09:39:21 2018
@@ -0,0 +1,47 @@
+# 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
Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=337401&r1=337400&r2=337401&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Wed Jul 18 09:39:21 2018
@@ -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 @@ static cl::alias
DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
cl::aliasopt(DisassembleAll));
+cl::opt<std::string> 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<std::string>
DisassembleFunctions("df",
cl::CommaSeparated,
@@ -330,6 +338,11 @@ LLVM_ATTRIBUTE_NORETURN void llvm::error
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 @@ static void DisassembleObject(const Obje
}
}
- 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.
@@ -2359,6 +2390,11 @@ int main(int argc, char **argv) {
if (DisassembleAll || PrintSource || PrintLines)
Disassemble = true;
+
+ if (Demangle.getValue() != "none" && Demangle.getValue() != "" &&
+ Demangle.getValue() != "itanium")
+ warn("Unsupported demangling style");
+
if (!Disassemble
&& !Relocations
&& !DynamicRelocations
Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.h?rev=337401&r1=337400&r2=337401&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.h (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.h Wed Jul 18 09:39:21 2018
@@ -30,6 +30,7 @@ namespace object {
extern cl::opt<std::string> TripleName;
extern cl::opt<std::string> ArchName;
extern cl::opt<std::string> MCPU;
+extern cl::opt<std::string> Demangle;
extern cl::list<std::string> MAttrs;
extern cl::list<std::string> FilterSections;
extern cl::opt<bool> AllHeaders;
@@ -96,6 +97,7 @@ void PrintSectionHeaders(const object::O
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);
More information about the llvm-commits
mailing list