Home | History | Annotate | Download | only in courgette
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef COURGETTE_DISASSEMBLER_H_
      6 #define COURGETTE_DISASSEMBLER_H_
      7 
      8 #include "base/basictypes.h"
      9 
     10 #include "courgette/courgette.h"
     11 
     12 namespace courgette {
     13 
     14 class AssemblyProgram;
     15 
     16 // A Relative Virtual Address is the address in the image file after it is
     17 // loaded into memory relative to the image load address.
     18 typedef uint32 RVA;
     19 
     20 class Disassembler {
     21  public:
     22   virtual ~Disassembler();
     23 
     24   virtual ExecutableType kind() { return EXE_UNKNOWN; }
     25 
     26   // ok() may always be called but returns 'true' only after ParseHeader
     27   // succeeds.
     28   bool ok() const { return failure_reason_ == NULL; }
     29 
     30   // Returns 'true' if the buffer appears to be a valid executable of the
     31   // expected type. It is not required that this be called before Disassemble.
     32   virtual bool ParseHeader() = 0;
     33 
     34   // Disassembles the item passed to the factory method into the output
     35   // parameter 'program'.
     36   virtual bool Disassemble(AssemblyProgram* program) = 0;
     37 
     38   // Returns the length of the source executable. May reduce after ParseHeader.
     39   size_t length() const { return length_; }
     40   const uint8* start() const { return start_; }
     41   const uint8* end() const { return end_; }
     42 
     43   // Returns a pointer into the memory copy of the file format.
     44   // FileOffsetToPointer(0) returns a pointer to the start of the file format.
     45   const uint8* OffsetToPointer(size_t offset) const;
     46 
     47  protected:
     48   Disassembler(const void* start, size_t length);
     49 
     50   bool Good();
     51   bool Bad(const char *reason);
     52 
     53   // These helper functions avoid the need for casts in the main code.
     54   uint16 ReadU16(const uint8* address, size_t offset) {
     55     return *reinterpret_cast<const uint16*>(address + offset);
     56   }
     57 
     58   uint32 ReadU32(const uint8* address, size_t offset) {
     59     return *reinterpret_cast<const uint32*>(address + offset);
     60   }
     61 
     62   uint64 ReadU64(const uint8* address, size_t offset) {
     63     return *reinterpret_cast<const uint64*>(address + offset);
     64   }
     65 
     66   static uint32 Read32LittleEndian(const void* address) {
     67     return *reinterpret_cast<const uint32*>(address);
     68   }
     69 
     70   static uint16 Read16LittleEndian(const void* address) {
     71     return *reinterpret_cast<const uint16*>(address);
     72   }
     73 
     74   // Reduce the length of the image in memory. Does not actually free
     75   // (or realloc) any memory. Usually only called via ParseHeader()
     76   void ReduceLength(size_t reduced_length);
     77 
     78  private:
     79   const char* failure_reason_;
     80 
     81   //
     82   // Basic information that is always valid after Construction, though
     83   // ParseHeader may shorten the length if the executable is shorter than
     84   // the total data.
     85   //
     86   size_t length_;         // In current memory.
     87   const uint8* start_;    // In current memory, base for 'file offsets'.
     88   const uint8* end_;      // In current memory.
     89 
     90   DISALLOW_COPY_AND_ASSIGN(Disassembler);
     91 };
     92 
     93 }  // namespace courgette
     94 #endif  // COURGETTE_DISASSEMBLER_H_
     95