From 8cd4c3e6534a14566bf163301fd45bca34e655c1 Mon Sep 17 00:00:00 2001 From: Anton Korobeynikov Date: Fri, 19 Jan 2007 17:25:17 +0000 Subject: [PATCH] Adding disassembler interface and external hook to udis86 library. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33358 91177308-0d34-0410-b5e6-96231b3b80d8 --- autoconf/configure.ac | 23 ++++++++++- include/llvm/Config/config.h.in | 6 +++ include/llvm/Support/Disassembler.h | 36 +++++++++++++++++ lib/ExecutionEngine/JIT/JITEmitter.cpp | 15 +++++++- lib/Support/Disassembler.cpp | 53 ++++++++++++++++++++++++++ 5 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 include/llvm/Support/Disassembler.h create mode 100644 lib/Support/Disassembler.cpp diff --git a/autoconf/configure.ac b/autoconf/configure.ac index 207daef006d..522001c59a7 100644 --- a/autoconf/configure.ac +++ b/autoconf/configure.ac @@ -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=], + [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 diff --git a/include/llvm/Config/config.h.in b/include/llvm/Config/config.h.in index 3fd359b3143..2e9731e1a4a 100644 --- a/include/llvm/Config/config.h.in +++ b/include/llvm/Config/config.h.in @@ -190,6 +190,9 @@ /* 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 header file. */ #undef HAVE_LIMITS_H @@ -526,6 +529,9 @@ /* Define to 1 if your 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 index 00000000000..3d0d798de83 --- /dev/null +++ b/include/llvm/Support/Disassembler.h @@ -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 + +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 diff --git a/lib/ExecutionEngine/JIT/JITEmitter.cpp b/lib/ExecutionEngine/JIT/JITEmitter.cpp index 4fc454e2ba5..a5b90c96637 100644 --- a/lib/ExecutionEngine/JIT/JITEmitter.cpp +++ b/lib/ExecutionEngine/JIT/JITEmitter.cpp @@ -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 index 00000000000..cc1f5e4390e --- /dev/null +++ b/lib/Support/Disassembler.cpp @@ -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 +#include +#include +#include + +#if USE_UDIS86 +#include +#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(); +}