| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COURGETTE_DISASSEMBLER_H_ | 5 #ifndef COURGETTE_DISASSEMBLER_H_ |
| 6 #define COURGETTE_DISASSEMBLER_H_ | 6 #define COURGETTE_DISASSEMBLER_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 | 9 |
| 10 #include "courgette/courgette.h" |
| 11 |
| 10 namespace courgette { | 12 namespace courgette { |
| 11 | 13 |
| 12 class AssemblyProgram; | 14 class AssemblyProgram; |
| 13 class PEInfo; | 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; |
| 14 | 19 |
| 15 class Disassembler { | 20 class Disassembler { |
| 16 public: | 21 public: |
| 17 virtual ~Disassembler() {} | 22 virtual ~Disassembler(); |
| 23 |
| 24 virtual ExecutableType kind() { return 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; |
| 18 | 33 |
| 19 // Disassembles the item passed to the factory method into the output | 34 // Disassembles the item passed to the factory method into the output |
| 20 // parameter 'program'. | 35 // parameter 'program'. |
| 21 virtual bool Disassemble(AssemblyProgram* program) = 0; | 36 virtual bool Disassemble(AssemblyProgram* program) = 0; |
| 22 | 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 |
| 23 protected: | 47 protected: |
| 24 Disassembler() {} | 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 // Reduce the length of the image in memory. Does not actually free |
| 71 // (or realloc) any memory. Unusally only called via ParseHeader() |
| 72 void ReduceLength(size_t reduced_length); |
| 25 | 73 |
| 26 private: | 74 private: |
| 75 const char* failure_reason_; |
| 76 |
| 77 // |
| 78 // Basic information that is always valid after Construction, though |
| 79 // ParseHeader may shorten the length if the executable is shorter than |
| 80 // the total data. |
| 81 // |
| 82 size_t length_; // In current memory. |
| 83 const uint8* start_; // In current memory, base for 'file offsets'. |
| 84 const uint8* end_; // In current memory. |
| 85 |
| 27 DISALLOW_COPY_AND_ASSIGN(Disassembler); | 86 DISALLOW_COPY_AND_ASSIGN(Disassembler); |
| 28 }; | 87 }; |
| 29 | 88 |
| 30 } // namespace courgette | 89 } // namespace courgette |
| 31 #endif // COURGETTE_DISASSEMBLER_H_ | 90 #endif // COURGETTE_DISASSEMBLER_H_ |
| OLD | NEW |