tisdag 28 september 2010

otool and c++ symbol demangling

On Mac OS X, otool cannot demangle c++ symbols. For example

otool -SV Debug/lib/libLLVMX86Disassembler.a

spits out entries like

Archive : Debug/lib/libLLVMX86Disassembler.a
Table of contents from: Debug/lib/libLLVMX86Disassembler.a(__.SYMDEF SORTED)
size of ranlib structures: 488 (number 61)
size of strings: 2968
object           symbol name
X86Disassembler.o _LLVMInitializeX86Disassembler
X86Disassembler.o __ZN4llvm11raw_ostreamlsENS_9StringRefE
X86Disassembler.o __ZN4llvm11raw_ostreamlsEPKc
X86Disassembler.o __ZN4llvm11raw_ostreamlsEj
X86Disassembler.o __ZN4llvm14MCDisassemblerC2Ev
X86Disassembler.o __ZN4llvm14TargetRegistry22RegisterMCDisassemblerERNS_6TargetEPFPNS_14MCDisassemblerERKS1_E
X86Disassembler.o __ZN4llvm15SmallVectorImplINS_9MCOperandEE9push_backERKS1_
X86Disassembler.o __ZN4llvm15X86Disassembler18X86_32DisassemblerC1Ev

Fortunately, Mac OS X Developer tools comes with the GNU program c++filt, so we can use that to demangle the symbols



otool -SV Debug/lib/libLLVMX86Disassembler.a | awk 'NF==2 {printf "%s ",$1; system("c++filt " $2)} NF!=2{print $0}'


which looks much more readable

Archive : Debug/lib/libLLVMX86Disassembler.a
Table of contents from: Debug/lib/libLLVMX86Disassembler.a(__.SYMDEF SORTED)
size of ranlib structures: 488 (number 61)
size of strings: 2968
object           symbol name
X86Disassembler.o _LLVMInitializeX86Disassembler
X86Disassembler.o llvm::raw_ostream::operator<<(llvm::StringRef)
X86Disassembler.o llvm::raw_ostream::operator<<(char const*)
X86Disassembler.o llvm::raw_ostream::operator<<(unsigned int)
X86Disassembler.o llvm::MCDisassembler::MCDisassembler()
X86Disassembler.o llvm::TargetRegistry::RegisterMCDisassembler(llvm::Target&, llvm::MCDisassembler* (*)(llvm::Target const&))
X86Disassembler.o llvm::SmallVectorImpl::push_back(llvm::MCOperand const&)
X86Disassembler.o llvm::X86Disassembler::X86_32Disassembler::X86_32Disassembler()