Add the option, -dis-symname to llvm-objdump used with -macho and
authorKevin Enderby <enderby@apple.com>
Tue, 17 Mar 2015 17:10:57 +0000 (17:10 +0000)
committerKevin Enderby <enderby@apple.com>
Tue, 17 Mar 2015 17:10:57 +0000 (17:10 +0000)
-disassemble to disassemble just one symbol’s instructions.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@232503 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-objdump/X86/macho-dis-symname.test [new file with mode: 0644]
tools/llvm-objdump/MachODump.cpp
tools/llvm-objdump/llvm-objdump.h

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 (file)
index 0000000..39d16ec
--- /dev/null
@@ -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:
index 625820f273d6487cc41ef49e428aa6d1193d7c39..3281b321fe7fcf16e1b09bb2f6083108275d21d0 100644 (file)
@@ -116,6 +116,10 @@ cl::opt<bool>
                      cl::desc("Print the info for Mach-O objects in "
                               "non-verbose or numeric form (requires -macho)"));
 
+cl::opt<std::string> llvm::DisSymName(
+    "dis-symname",
+    cl::desc("disassemble just this symbol's instructions (requires -macho"));
+
 static cl::list<std::string>
     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();
index 68cb4374dffda885f7ee0db9483ed34a7fb3b032..b846e1018f8263de69cd690917d4b6bc441513fe 100644 (file)
@@ -43,6 +43,7 @@ extern cl::opt<bool> LinkOptHints;
 extern cl::opt<bool> InfoPlist;
 extern cl::opt<bool> DylibsUsed;
 extern cl::opt<bool> DylibId;
+extern cl::opt<std::string> DisSymName;
 extern cl::opt<bool> NonVerbose;
 extern cl::opt<bool> Relocations;
 extern cl::opt<bool> SectionHeaders;