ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Disassembler.h
Go to the documentation of this file.
1 #ifndef ROSE_DISASSEMBLER_H
2 #define ROSE_DISASSEMBLER_H
3 
4 #include "threadSupport.h" /* needed for RTS_mutex_t in this header */
5 #include "Registers.h"
6 #include "MemoryMap.h"
7 #include "integerOps.h"
8 #include "Map.h"
9 
117 public:
119  class Exception: public std::runtime_error {
120  public:
122  Exception(const std::string &reason)
123  : std::runtime_error(reason), ip(0), bit(0), insn(NULL)
124  {}
126  Exception(const std::string &reason, rose_addr_t ip)
127  : std::runtime_error(reason), ip(ip), bit(0), insn(NULL)
128  {}
130  Exception(const std::string &reason, rose_addr_t ip, const SgUnsignedCharList &raw_data, size_t bit)
131  : std::runtime_error(reason), ip(ip), bytes(raw_data), bit(bit), insn(NULL)
132  {}
134  Exception(const std::string &reason, SgAsmInstruction *insn)
135  : std::runtime_error(reason), ip(insn->get_address()), bit(0), insn(insn)
136  {}
137  ~Exception() throw() {}
138 
139  void print(std::ostream&) const;
140  friend std::ostream& operator<<(std::ostream &o, const Exception &e);
141 
146  size_t bit;
149  };
150 
156  {
157  SEARCH_FOLLOWING = 0x0001,
159  SEARCH_IMMEDIATE = 0x0002,
163  SEARCH_WORDS = 0x0004,
167  SEARCH_ALLBYTES = 0x0008,
169  SEARCH_UNUSED = 0x0010,
176  SEARCH_NONEXE = 0x0020,
185  SEARCH_DEADEND = 0x0040,
191  SEARCH_UNKNOWN = 0x0080,
194  SEARCH_FUNCSYMS = 0x0100,
198  SEARCH_DEFAULT = 0x0101
199  };
200 
209  static unsigned parse_switches(const std::string &s, unsigned initial=SEARCH_DEFAULT);
210 
213  typedef std::set<rose_addr_t> AddressSet;
214 
216  typedef Map<rose_addr_t, SgAsmInstruction*> InstructionMap;
217 
220  typedef Map<rose_addr_t, Exception> BadMap;
221 
223  : p_registers(NULL), p_partitioner(NULL), p_search(SEARCH_DEFAULT), p_debug(NULL),
224  p_wordsize(4), p_sex(ByteOrder::ORDER_LSB), p_alignment(4), p_ndisassembled(0),
225  p_protection(MemoryMap::MM_PROT_EXEC)
226  {ctor();}
227 
230  p_debug(other.p_debug), p_wordsize(other.p_wordsize), p_sex(other.p_sex), p_alignment(other.p_alignment),
232  {}
233 
234  virtual ~Disassembler() {}
235 
236 
237 
238 
239  /***************************************************************************************************************************
240  * Registration and lookup methods
241  ***************************************************************************************************************************/
242 
243 public:
249  static void register_subclass(Disassembler*);
250 
256  virtual bool can_disassemble(SgAsmGenericHeader*) const = 0;
257 
265 
275 
279  virtual Disassembler *clone() const = 0;
280 
281 
282  /***************************************************************************************************************************
283  * Main public disassembly methods
284  ***************************************************************************************************************************/
285 public:
304  void disassemble(SgAsmInterpretation*, AddressSet *successors=NULL, BadMap *bad=NULL);
305 
315 
316 
317 
318  /***************************************************************************************************************************
319  * Disassembler properties and settings
320  ***************************************************************************************************************************/
321 public:
329  void set_registers(const RegisterDictionary *rdict) {
330  p_registers = rdict;
331  }
332 
337  return p_registers;
338  }
339 
340 #ifndef USE_ROSE
341  // DQ (2/11/2013): I think it is a problem to use this function (currently being evaluated using delta debug case).
342  // This fails for ROSE compilign "rose.h" header file (ROSE compiling ROSE).
343  // Generates error: ERROR: In parse_function_body(): (this should have been a function body) entry_kind = 0x3a4d47e8 = src-seq-sublist
344 
349  void set_partitioner(class Partitioner *p) {
350  p_partitioner = p;
351  }
352 #else
353  void set_partitioner(class Partitioner *p);
354 #endif
355 
356 #ifndef USE_ROSE
357  // DQ (2/11/2013): I think it is a problem to use this function (currently being evaluated using delta debug case).
358  // This fails for ROSE compilign "rose.h" header file (ROSE compiling ROSE).
359 
363  class Partitioner *get_partitioner() const {
364  return p_partitioner;
365  }
366 #else
367  class Partitioner *get_partitioner() const;
368 #endif
369 
375  void set_search(unsigned bits) {
376  p_search = bits;
377  }
378 
383  unsigned get_search() const {
384  return p_search;
385  }
386 
392  void set_wordsize(size_t);
393 
397  size_t get_wordsize() const {
398  return p_wordsize;
399  }
400 
407  void set_alignment(size_t);
408 
412  size_t get_alignment() const {
413  return p_alignment;
414  }
415 
421  p_sex = sex;
422  }
423 
428  return p_sex;
429  }
430 
435  void set_debug(FILE *f) {
436  p_debug = f;
437  }
438 
442  FILE *get_debug() const {
443  return p_debug;
444  }
445 
450  size_t get_ndisassembled() const {
451  return p_ndisassembled;
452  }
453 
459  void set_protection(unsigned bitvec) {
460  p_protection = bitvec;
461  }
462 
467  unsigned get_protection() const {
468  return p_protection;
469  }
470 
477  void set_progress_reporting(FILE*, unsigned min_interval);
478 
479 
480  /***************************************************************************************************************************
481  * Low-level disassembly functions
482  ***************************************************************************************************************************/
483 public:
494  virtual SgAsmInstruction *disassembleOne(const MemoryMap *map, rose_addr_t start_va, AddressSet *successors=NULL) = 0;
495 
502  SgAsmInstruction *disassembleOne(const unsigned char *buf, rose_addr_t buf_va, size_t buf_size, rose_addr_t start_va,
503  AddressSet *successors=NULL);
504 
529  InstructionMap *cache=NULL);
530 
536  InstructionMap disassembleBlock(const unsigned char *buf, rose_addr_t buf_va, size_t buf_size, rose_addr_t start_va,
537  AddressSet *successors=NULL, InstructionMap *cache=NULL);
538 
549  InstructionMap disassembleBuffer(const MemoryMap *map, size_t start_va, AddressSet *successors=NULL, BadMap *bad=NULL);
550 
556  InstructionMap disassembleBuffer(const unsigned char *buf, rose_addr_t buf_va, size_t buf_size, rose_addr_t start_va,
557  AddressSet *successors=NULL, BadMap *bad=NULL);
558 
565 
571  AddressSet *successors=NULL, BadMap *bad=NULL);
572 
582 
583 
584 
585  /***************************************************************************************************************************
586  * Methods for searching for disassembly addresses.
587  ***************************************************************************************************************************/
588 public:
596  void search_following(AddressSet *worklist, const InstructionMap &bb, rose_addr_t bb_va,
597  const MemoryMap *map, const InstructionMap &tried);
598 
607  void search_immediate(AddressSet *worklist, const InstructionMap &bb, const MemoryMap *map, const InstructionMap &tried);
608 
616  void search_words(AddressSet *worklist, const MemoryMap *map, const InstructionMap &tried);
617 
629  void search_next_address(AddressSet *worklist, rose_addr_t start_va, const MemoryMap *map, const InstructionMap &insns,
630  const InstructionMap &tried, bool avoid_overlaps);
631 
639 
640 
641  /***************************************************************************************************************************
642  * Miscellaneous methods
643  ***************************************************************************************************************************/
644 public:
650 
656  void progress(FILE*, const char *fmt, ...) const __attribute__((format(printf, 3, 4)));
657 
661  virtual SgAsmInstruction *make_unknown_instruction(const Exception&) = 0;
662 
667 
673  AddressSet get_block_successors(const InstructionMap&, bool *complete);
674 
675 private:
677  static void initclass();
678 
680  void ctor();
681 
687  static SgAsmInstruction *find_instruction_containing(const InstructionMap &insns, rose_addr_t va);
688 
689 
690 
691  /***************************************************************************************************************************
692  * Data members
693  ***************************************************************************************************************************/
694 protected:
697  unsigned p_search;
698  FILE *p_debug;
699  size_t p_wordsize;
700  ByteOrder::Endianness p_sex;
701  size_t p_alignment;
702  static std::vector<Disassembler*> disassemblers;
704  unsigned p_protection;
706  static time_t progress_interval;
707  static time_t progress_time;
708  static FILE *progress_file;
711 };
712 
713 #endif