From 73cd697dec358f6362c11cbec445f8cf928b0427 Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Tue, 17 Mar 2015 17:10:57 +0000 Subject: [PATCH] =?UTF-8?q?Add=20the=20option,=20-dis-symname=20to=20llvm-?= =?UTF-8?q?objdump=20used=20with=20-macho=20and=20-disassemble=20to=20disa?= =?UTF-8?q?ssemble=20just=20one=20symbol=E2=80=99s=20instructions.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@232503 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../llvm-objdump/X86/macho-dis-symname.test | 19 +++++++++++++++++++ tools/llvm-objdump/MachODump.cpp | 15 +++++++++++++++ tools/llvm-objdump/llvm-objdump.h | 1 + 3 files changed, 35 insertions(+) create mode 100644 test/tools/llvm-objdump/X86/macho-dis-symname.test diff --git a/test/tools/llvm-objdump/X86/macho-dis-symname.test b/test/tools/llvm-objdump/X86/macho-dis-symname.test new file mode 100644 index 00000000000..39d16ecba6f --- /dev/null +++ b/test/tools/llvm-objdump/X86/macho-dis-symname.test @@ -0,0 +1,19 @@ +# RUN: llvm-objdump -m -d %p/Inputs/exeThread.macho-x86_64 -dis-symname start -no-show-raw-insn -full-leading-addr -print-imm-hex | FileCheck %s + +# CHECK: (__TEXT,__text) section +# CHECK: start: +# CHECK: 0000000100000d00 pushq $0x0 +# CHECK: 0000000100000d02 movq %rsp, %rbp +# CHECK: 0000000100000d05 andq $-0x10, %rsp +# CHECK: 0000000100000d09 movq 0x8(%rbp), %rdi +# CHECK: 0000000100000d0d leaq 0x10(%rbp), %rsi +# CHECK: 0000000100000d11 movl %edi, %edx +# CHECK: 0000000100000d13 addl $0x1, %edx +# CHECK: 0000000100000d16 shll $0x3, %edx +# CHECK: 0000000100000d19 addq %rsi, %rdx +# CHECK: 0000000100000d1c callq __start +# CHECK: 0000000100000d21 hlt + +# CHECK-NOT: __start: +# CHECK-NOT: 0000000100000d22 +# CHECK-NOT: _main: diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index 625820f273d..3281b321fe7 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -116,6 +116,10 @@ cl::opt cl::desc("Print the info for Mach-O objects in " "non-verbose or numeric form (requires -macho)")); +cl::opt llvm::DisSymName( + "dis-symname", + cl::desc("disassemble just this symbol's instructions (requires -macho")); + static cl::list ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"), cl::ZeroOrMore); @@ -3181,6 +3185,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, // Create a map of symbol addresses to symbol names for use by // the SymbolizerSymbolLookUp() routine. SymbolAddressMap AddrMap; + bool DisSymNameFound = false; for (const SymbolRef &Symbol : MachOOF->symbols()) { SymbolRef::Type ST; Symbol.getType(ST); @@ -3191,8 +3196,14 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, StringRef SymName; Symbol.getName(SymName); AddrMap[Address] = SymName; + if (!DisSymName.empty() && DisSymName == SymName) + DisSymNameFound = true; } } + if (!DisSymName.empty() && DisSymNameFound == false) { + outs() << "Can't find -dis-symname: " << DisSymName << "\n"; + return; + } // Set up the block of info used by the Symbolizer call backs. SymbolizerInfo.verbose = true; SymbolizerInfo.O = MachOOF; @@ -3235,6 +3246,10 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, if (!containsSym) continue; + // If we are only disassembling one symbol see if this is that symbol. + if (!DisSymName.empty() && DisSymName != SymName) + continue; + // Start at the address of the symbol relative to the section's address. uint64_t Start = 0; uint64_t SectionAddress = Sections[SectIdx].getAddress(); diff --git a/tools/llvm-objdump/llvm-objdump.h b/tools/llvm-objdump/llvm-objdump.h index 68cb4374dff..b846e1018f8 100644 --- a/tools/llvm-objdump/llvm-objdump.h +++ b/tools/llvm-objdump/llvm-objdump.h @@ -43,6 +43,7 @@ extern cl::opt LinkOptHints; extern cl::opt InfoPlist; extern cl::opt DylibsUsed; extern cl::opt DylibId; +extern cl::opt DisSymName; extern cl::opt NonVerbose; extern cl::opt Relocations; extern cl::opt SectionHeaders;