LLVM API Documentation
00001 //===- lib/System/Disassembler.cpp ------------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the necessary glue to call external disassembler 00011 // libraries. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Config/config.h" 00016 #include "llvm/System/Disassembler.h" 00017 00018 #include <cassert> 00019 #include <iomanip> 00020 #include <string> 00021 #include <sstream> 00022 00023 #if USE_UDIS86 00024 #include <udis86.h> 00025 #endif 00026 00027 using namespace llvm; 00028 00029 bool llvm::sys::hasDisassembler(void) 00030 { 00031 #if defined (__i386__) || defined (__amd64__) || defined (__x86_64__) 00032 // We have option to enable udis86 library. 00033 # if USE_UDIS86 00034 return true; 00035 #else 00036 return false; 00037 #endif 00038 #else 00039 return false; 00040 #endif 00041 } 00042 00043 std::string llvm::sys::disassembleBuffer(uint8_t* start, size_t length, 00044 uint64_t pc) { 00045 std::stringstream res; 00046 00047 #if defined (__i386__) || defined (__amd64__) || defined (__x86_64__) 00048 unsigned bits; 00049 # if defined(__i386__) 00050 bits = 32; 00051 # else 00052 bits = 64; 00053 # endif 00054 00055 # if USE_UDIS86 00056 ud_t ud_obj; 00057 00058 ud_init(&ud_obj); 00059 ud_set_input_buffer(&ud_obj, start, length); 00060 ud_set_mode(&ud_obj, bits); 00061 ud_set_pc(&ud_obj, pc); 00062 ud_set_syntax(&ud_obj, UD_SYN_ATT); 00063 00064 res << std::setbase(16) 00065 << std::setw(bits/4); 00066 00067 while (ud_disassemble(&ud_obj)) { 00068 res << ud_insn_off(&ud_obj) << ":\t" << ud_insn_asm(&ud_obj) << "\n"; 00069 } 00070 # else 00071 res << "No disassembler available. See configure help for options.\n"; 00072 # endif 00073 00074 #else 00075 res << "No disassembler available. See configure help for options.\n"; 00076 #endif 00077 00078 return res.str(); 00079 }
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.