Adding disassembler interface and external hook to udis86 library.

llvm-svn: 33358
This commit is contained in:
Anton Korobeynikov 2007-01-19 17:25:17 +00:00
parent 49c6e09c7c
commit 3e956974b3
5 changed files with 130 additions and 3 deletions

View 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

View File

@ -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 <limits.h> header file. */
#undef HAVE_LIMITS_H
@ -526,6 +529,9 @@
/* 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

View File

@ -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

View 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;
}

View File

@ -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();
}