blob: bad427a58d8cbdcb624dc9dd8d50132e8ab0ad14 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- lib/System/Disassembler.cpp ------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the necessary glue to call external disassembler
11// libraries.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Config/config.h"
16#include "llvm/System/Disassembler.h"
17
18#include <cassert>
19#include <iomanip>
20#include <string>
21#include <sstream>
22
23#if USE_UDIS86
24#include <udis86.h>
25#endif
26
27using namespace llvm;
28
Dan Gohman3dbe9442009-08-12 22:10:57 +000029bool llvm::sys::hasDisassembler()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030{
31#if defined (__i386__) || defined (__amd64__) || defined (__x86_64__)
32 // We have option to enable udis86 library.
Evan Cheng28f92a82008-11-04 06:09:38 +000033# if USE_UDIS86
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 return true;
35#else
36 return false;
37#endif
Evan Cheng28f92a82008-11-04 06:09:38 +000038#else
39 return false;
40#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041}
42
43std::string llvm::sys::disassembleBuffer(uint8_t* start, size_t length,
44 uint64_t pc) {
45 std::stringstream res;
46
47#if defined (__i386__) || defined (__amd64__) || defined (__x86_64__)
48 unsigned bits;
49# if defined(__i386__)
50 bits = 32;
51# else
52 bits = 64;
53# endif
54
55# if USE_UDIS86
56 ud_t ud_obj;
57
58 ud_init(&ud_obj);
59 ud_set_input_buffer(&ud_obj, start, length);
60 ud_set_mode(&ud_obj, bits);
61 ud_set_pc(&ud_obj, pc);
62 ud_set_syntax(&ud_obj, UD_SYN_ATT);
63
64 res << std::setbase(16)
65 << std::setw(bits/4);
66
67 while (ud_disassemble(&ud_obj)) {
68 res << ud_insn_off(&ud_obj) << ":\t" << ud_insn_asm(&ud_obj) << "\n";
69 }
70# else
71 res << "No disassembler available. See configure help for options.\n";
72# endif
73
74#else
75 res << "No disassembler available. See configure help for options.\n";
76#endif
77
78 return res.str();
79}