llvm.org GIT mirror llvm / 8cd4c3e
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 Anton Korobeynikov 12 years ago
5 changed file(s) with 130 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
3434
3535 dnl Provide a copyright substitution and ensure the copyright notice is included
3636 dnl in the output of --version option of the generated configure script.
37 AC_SUBST(LLVM_COPYRIGHT,["Copyright (c) 2003-2005 University of Illinois at Urbana-Champaign."])
38 AC_COPYRIGHT([Copyright (c) 2003-2005 University of Illinois at Urbana-Champaign.])
37 AC_SUBST(LLVM_COPYRIGHT,["Copyright (c) 2003-2007 University of Illinois at Urbana-Champaign."])
38 AC_COPYRIGHT([Copyright (c) 2003-2007 University of Illinois at Urbana-Champaign.])
3939
4040 dnl Indicate that we require autoconf 2.59 or later. Ths is needed because we
4141 dnl use some autoconf macros only available in 2.59.
628628 [Have pthread_mutex_lock]))
629629 fi
630630
631 dnl Allow extra x86-disassembler library
632 AC_ARG_WITH(udis86,
633 AS_HELP_STRING([--with-udis86=],
634 [Use udis86 external x86 disassembler library]),
635 [
636 AC_SUBST(USE_UDIS86, [1])
637 case "$withval" in
638 /usr/lib) ;;
639 *) LDFLAGS="$LDFLAGS -L${withval}" ;;
640 esac
641 AC_CHECK_LIB(udis86, ud_init, [], [
642 echo "Error! You need to have libudis86 around."
643 exit -1
644 ])
645 ],
646 AC_SUBST(USE_UDIS86, [0]))
647 AC_DEFINE_UNQUOTED([USE_UDIS86],$USE_UDIS86,
648 [Define if use udis86 library])
649
631650 dnl===-----------------------------------------------------------------------===
632651 dnl===
633652 dnl=== SECTION 6: Check for header files
188188
189189 /* Define to 1 if you have the `pthread' library (-lpthread). */
190190 #undef HAVE_LIBPTHREAD
191
192 /* Define to 1 if you have the `udis86' library (-ludis86). */
193 #undef HAVE_LIBUDIS86
191194
192195 /* Define to 1 if you have the header file. */
193196 #undef HAVE_LIMITS_H
525528 /* Define to 1 if your declares `struct tm'. */
526529 #undef TM_IN_SYS_TIME
527530
531 /* Define if use udis86 library */
532 #undef USE_UDIS86
533
528534 /* Define to 1 if `lex' declares `yytext' as a `char *' by default, not a
529535 `char[]'. */
530536 #undef YYTEXT_POINTER
0 //===- llvm/Support/Disassembler.h ------------------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Anton Korobeynikov and is distributed under the
5 // University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the necessary glue to call external disassembler
10 // libraries.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_DISASSEMBLER_H
15 #define LLVM_SUPPORT_DISASSEMBLER_H
16
17 #include "llvm/Support/DataTypes.h"
18 #include
19
20 namespace llvm {
21
22 namespace Disassembler {
23 enum Type {
24 X86_32,
25 X86_64,
26 Undefined
27 };
28 }
29
30
31 std::string disassembleBuffer(uint8_t* start, size_t length,
32 Disassembler::Type type, uint64_t pc);
33 }
34
35 #endif // LLVM_SUPPORT_DISASSEMBLER_H
2626 #include "llvm/Target/TargetJITInfo.h"
2727 #include "llvm/Target/TargetMachine.h"
2828 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Disassembler.h"
2930 #include "llvm/Support/MutexGuard.h"
3031 #include "llvm/ADT/Statistic.h"
3132 #include "llvm/System/Memory.h"
846847 }
847848
848849 // Update the GOT entry for F to point to the new code.
849 if(MemMgr.isManagingGOT()) {
850 if (MemMgr.isManagingGOT()) {
850851 unsigned idx = getJITResolver(this).getGOTIndexForAddr((void*)BufferBegin);
851852 if (((void**)MemMgr.getGOTBase())[idx] != (void*)BufferBegin) {
852853 DOUT << "GOT was out of date for " << (void*)BufferBegin
863864 << ": " << (FnEnd-FnStart) << " bytes of text, "
864865 << Relocations.size() << " relocations\n";
865866 Relocations.clear();
867
868 DOUT << "Disassembled code:\n"
869 #if defined(__i386__)
870 << disassembleBuffer(FnStart, FnEnd-FnStart,
871 Disassembler::X86_32, (uint32_t)FnStart);
872 #elif defined(__amd64__) || defined(__x86_64__)
873 << disassembleBuffer(FnStart, FnEnd-FnStart,
874 Disassembler::X86_64, (uint32_t)FnStart);
875 #else
876 << "N/A\n";
877 #endif
878
866879 return false;
867880 }
868881
0 //===- lib/Support/Disassembler.cpp -----------------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Anton Korobeynikov and is distributed under the
5 // University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the necessary glue to call external disassembler
10 // libraries.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Config/config.h"
15 #include "llvm/Support/Disassembler.h"
16
17 #include
18 #include
19 #include
20 #include
21
22 #if USE_UDIS86
23 #include
24 #endif
25
26 using namespace llvm;
27
28 std::string llvm::disassembleBuffer(uint8_t* start, size_t length,
29 Disassembler::Type type, uint64_t pc) {
30 std::stringstream res;
31
32 if (type == Disassembler::X86_32 || type == Disassembler::X86_64) {
33 #if USE_UDIS86
34 ud_t ud_obj;
35
36 ud_init(&ud_obj);
37 ud_set_input_buffer(&ud_obj, start, length);
38 ud_set_mode(&ud_obj, (type == Disassembler::X86_32 ? 32 : 64));
39 ud_set_pc(&ud_obj, pc);
40 ud_set_syntax(&ud_obj, UD_SYN_ATT);
41
42 res << std::setbase(16)
43 << std::setw((type == Disassembler::X86_32 ? 8 : 16));
44
45 while (ud_disassemble(&ud_obj)) {
46 res << ud_insn_off(&ud_obj) << ":\t" << ud_insn_asm(&ud_obj) << "\n";
47 }
48 #endif
49 }
50
51 return res.str();
52 }