[llvm] r349613 - [llvm-objdump] - Demangle the symbols when printing symbol table and relocations.
George Rimar via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 19 02:21:45 PST 2018
Author: grimar
Date: Wed Dec 19 02:21:45 2018
New Revision: 349613
URL: http://llvm.org/viewvc/llvm-project?rev=349613&view=rev
Log:
[llvm-objdump] - Demangle the symbols when printing symbol table and relocations.
This is https://bugs.llvm.org/show_bug.cgi?id=40009,
llvm-objdump does not demangle the symbols when prints symbol
table and/or relocations.
Patch teaches it to do that.
Differential revision: https://reviews.llvm.org/D55821
Added:
llvm/trunk/test/tools/llvm-objdump/X86/demangle.s
Modified:
llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
Added: llvm/trunk/test/tools/llvm-objdump/X86/demangle.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/X86/demangle.s?rev=349613&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/X86/demangle.s (added)
+++ llvm/trunk/test/tools/llvm-objdump/X86/demangle.s Wed Dec 19 02:21:45 2018
@@ -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, at function
+_Z3foov:
+ callq _Z3foov at PLT
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=349613&r1=349612&r2=349613&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Wed Dec 19 02:21:45 2018
@@ -504,7 +504,7 @@ static std::error_code getRelocationValu
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 @@ static std::error_code getRelocationValu
Expected<StringRef> 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 void addPltEntries(const ObjectFi
}
}
+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 @@ static void DisassembleObject(const Obje
}
}
- 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 @@ void llvm::PrintSymbolTable(const Object
if (Hidden) {
outs() << ".hidden ";
}
- outs() << Name
- << '\n';
+
+ if (Demangle)
+ outs() << demangle(Name) << '\n';
+ else
+ outs() << Name << '\n';
}
}
More information about the llvm-commits
mailing list