Adding disassembler interface and external hook to udis86 library.
authorAnton Korobeynikov <asl@math.spbu.ru>
Fri, 19 Jan 2007 17:25:17 +0000 (17:25 +0000)
committerAnton Korobeynikov <asl@math.spbu.ru>
Fri, 19 Jan 2007 17:25:17 +0000 (17:25 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33358 91177308-0d34-0410-b5e6-96231b3b80d8

autoconf/configure.ac
include/llvm/Config/config.h.in
include/llvm/Support/Disassembler.h [new file with mode: 0644]
lib/ExecutionEngine/JIT/JITEmitter.cpp
lib/Support/Disassembler.cpp [new file with mode: 0644]

index 207daef006d1602e26320206664b94849ce46fe9..522001c59a74d5b27ade491b50946151a59f35c9 100644 (file)
@@ -35,8 +35,8 @@ AC_INIT([[llvm]],[[2.0cvs]],[llvmbugs@cs.uiuc.edu])
 
 dnl Provide a copyright substitution and ensure the copyright notice is included
 dnl in the output of --version option of the generated configure script.
-AC_SUBST(LLVM_COPYRIGHT,["Copyright (c) 2003-2005 University of Illinois at Urbana-Champaign."])
-AC_COPYRIGHT([Copyright (c) 2003-2005 University of Illinois at Urbana-Champaign.])
+AC_SUBST(LLVM_COPYRIGHT,["Copyright (c) 2003-2007 University of Illinois at Urbana-Champaign."])
+AC_COPYRIGHT([Copyright (c) 2003-2007 University of Illinois at Urbana-Champaign.])
 
 dnl Indicate that we require autoconf 2.59 or later. Ths is needed because we
 dnl use some autoconf macros only available in 2.59.
@@ -629,6 +629,25 @@ if test "$ENABLE_THREADS" -eq 1 ; then
                            [Have pthread_mutex_lock]))
 fi
 
+dnl Allow extra x86-disassembler library
+AC_ARG_WITH(udis86,
+  AS_HELP_STRING([--with-udis86=<path>],
+    [Use udis86 external x86 disassembler library]),
+    [
+      AC_SUBST(USE_UDIS86, [1])
+      case "$withval" in
+        /usr/lib) ;;
+        *) LDFLAGS="$LDFLAGS -L${withval}" ;;
+      esac
+      AC_CHECK_LIB(udis86, ud_init, [], [
+        echo "Error! You need to have libudis86 around."
+        exit -1
+      ])
+    ],
+    AC_SUBST(USE_UDIS86, [0]))
+AC_DEFINE_UNQUOTED([USE_UDIS86],$USE_UDIS86,
+                   [Define if use udis86 library])
+
 dnl===-----------------------------------------------------------------------===
 dnl===
 dnl=== SECTION 6: Check for header files
index 3fd359b3143acba856c150d78dee3f98dc8ef68b..2e9731e1a4a1ff00f4b0fdc0b833a63144d9e53b 100644 (file)
 /* Define to 1 if you have the `pthread' library (-lpthread). */
 #undef HAVE_LIBPTHREAD
 
+/* Define to 1 if you have the `udis86' library (-ludis86). */
+#undef HAVE_LIBUDIS86
+
 /* Define to 1 if you have the <limits.h> header file. */
 #undef HAVE_LIMITS_H
 
 /* Define to 1 if your <sys/time.h> declares `struct tm'. */
 #undef TM_IN_SYS_TIME
 
+/* Define if use udis86 library */
+#undef USE_UDIS86
+
 /* Define to 1 if `lex' declares `yytext' as a `char *' by default, not a
    `char[]'. */
 #undef YYTEXT_POINTER
diff --git a/include/llvm/Support/Disassembler.h b/include/llvm/Support/Disassembler.h
new file mode 100644 (file)
index 0000000..3d0d798
--- /dev/null
@@ -0,0 +1,36 @@
+//===- llvm/Support/Disassembler.h ------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by Anton Korobeynikov and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the necessary glue to call external disassembler
+// libraries.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_DISASSEMBLER_H
+#define LLVM_SUPPORT_DISASSEMBLER_H
+
+#include "llvm/Support/DataTypes.h"
+#include <string>
+
+namespace llvm {
+
+namespace Disassembler {
+  enum Type {
+    X86_32,
+    X86_64,
+    Undefined
+  };
+}
+  
+
+std::string disassembleBuffer(uint8_t* start, size_t length,
+                              Disassembler::Type type, uint64_t pc);
+}
+
+#endif // LLVM_SUPPORT_DISASSEMBLER_H
index 4fc454e2ba58814fab36cb594793d8b5924e2486..a5b90c966379e6c25245c4a9acbd0b25a4bc3981 100644 (file)
@@ -27,6 +27,7 @@
 #include "llvm/Target/TargetJITInfo.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/Disassembler.h"
 #include "llvm/Support/MutexGuard.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/System/Memory.h"
@@ -847,7 +848,7 @@ bool JITEmitter::finishFunction(MachineFunction &F) {
   }
 
   // Update the GOT entry for F to point to the new code.
-  if(MemMgr.isManagingGOT()) {
+  if (MemMgr.isManagingGOT()) {
     unsigned idx = getJITResolver(this).getGOTIndexForAddr((void*)BufferBegin);
     if (((void**)MemMgr.getGOTBase())[idx] != (void*)BufferBegin) {
       DOUT << "GOT was out of date for " << (void*)BufferBegin
@@ -864,6 +865,18 @@ bool JITEmitter::finishFunction(MachineFunction &F) {
        << ": " << (FnEnd-FnStart) << " bytes of text, "
        << Relocations.size() << " relocations\n";
   Relocations.clear();
+
+  DOUT << "Disassembled code:\n"
+#if defined(__i386__)
+       << disassembleBuffer(FnStart, FnEnd-FnStart,
+                            Disassembler::X86_32, (uint32_t)FnStart);
+#elif defined(__amd64__) || defined(__x86_64__)
+       << disassembleBuffer(FnStart, FnEnd-FnStart,
+                            Disassembler::X86_64, (uint32_t)FnStart);
+#else
+       << "N/A\n";
+#endif
+  
   return false;
 }
 
diff --git a/lib/Support/Disassembler.cpp b/lib/Support/Disassembler.cpp
new file mode 100644 (file)
index 0000000..cc1f5e4
--- /dev/null
@@ -0,0 +1,53 @@
+//===- lib/Support/Disassembler.cpp -----------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by Anton Korobeynikov and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the necessary glue to call external disassembler
+// libraries.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Config/config.h"
+#include "llvm/Support/Disassembler.h"
+
+#include <cassert>
+#include <iomanip>
+#include <string>
+#include <sstream>
+
+#if USE_UDIS86
+#include <udis86.h>
+#endif
+
+using namespace llvm;
+
+std::string llvm::disassembleBuffer(uint8_t* start, size_t length,
+                              Disassembler::Type type, uint64_t pc) {
+  std::stringstream res;
+  
+  if (type == Disassembler::X86_32 || type == Disassembler::X86_64) {
+#if USE_UDIS86
+    ud_t ud_obj;
+   
+    ud_init(&ud_obj);
+    ud_set_input_buffer(&ud_obj, start, length);
+    ud_set_mode(&ud_obj, (type == Disassembler::X86_32 ? 32 : 64));
+    ud_set_pc(&ud_obj, pc);
+    ud_set_syntax(&ud_obj, UD_SYN_ATT);
+
+    res << std::setbase(16)
+        << std::setw((type == Disassembler::X86_32 ? 8 : 16));
+
+    while (ud_disassemble(&ud_obj)) {
+      res << ud_insn_off(&ud_obj) << ":\t" << ud_insn_asm(&ud_obj) << "\n";
+    }
+#endif
+  }
+
+  return res.str();
+}