Chromium Code Reviews| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "courgette/courgette.h" | 12 #include "courgette/courgette.h" |
| 13 #include "courgette/image_utils.h" | 13 #include "courgette/image_utils.h" |
| 14 | 14 |
| 15 namespace courgette { | 15 namespace courgette { |
| 16 | 16 |
| 17 class AssemblyProgram; | 17 class AssemblyProgram; |
| 18 | 18 |
| 19 class Disassembler { | 19 class Disassembler : public AddressTranslator { |
|
huangs
2016/02/05 21:19:05
Going against "composition over inheritance", but
| |
| 20 public: | 20 public: |
| 21 virtual ~Disassembler(); | 21 virtual ~Disassembler(); |
| 22 | 22 |
| 23 virtual ExecutableType kind() { return EXE_UNKNOWN; } | 23 // AddressTranslator interfaces. |
| 24 virtual RVA FileOffsetToRVA(FileOffset file_offset) const override = 0; | |
| 25 virtual FileOffset RVAToFileOffset(RVA rva) const override = 0; | |
| 26 const uint8_t* FileOffsetToPointer(FileOffset file_offset) const override; | |
| 27 const uint8_t* RVAToPointer(RVA rva) const override; | |
| 24 | 28 |
| 25 // ok() may always be called but returns 'true' only after ParseHeader | 29 virtual ExecutableType kind() const = 0; |
| 26 // succeeds. | |
| 27 bool ok() const { return failure_reason_ == NULL; } | |
| 28 | 30 |
| 29 // Returns 'true' if the buffer appears to be a valid executable of the | 31 // Returns true if the buffer appears to be a valid executable of the expected |
| 30 // expected type. It is not required that this be called before Disassemble. | 32 // type, and false otherwise. This need not be called before Disassemble(). |
| 31 virtual bool ParseHeader() = 0; | 33 virtual bool ParseHeader() = 0; |
| 32 | 34 |
| 33 // Disassembles the item passed to the factory method into the output | 35 // Disassembles the item passed to the factory method into the output |
| 34 // parameter 'program'. | 36 // parameter 'program'. |
| 35 virtual bool Disassemble(AssemblyProgram* program) = 0; | 37 virtual bool Disassemble(AssemblyProgram* program) = 0; |
| 36 | 38 |
| 37 // Returns the length of the source executable. May reduce after ParseHeader. | 39 // ok() may always be called but returns 'true' only after ParseHeader |
| 40 // succeeds. | |
| 41 bool ok() const { return failure_reason_ == nullptr; } | |
| 42 | |
| 43 // Returns the length of the image. May reduce after ParseHeader. | |
| 38 size_t length() const { return length_; } | 44 size_t length() const { return length_; } |
| 39 const uint8_t* start() const { return start_; } | 45 const uint8_t* start() const { return start_; } |
| 40 const uint8_t* end() const { return end_; } | 46 const uint8_t* end() const { return end_; } |
| 41 | 47 |
| 42 // Returns a pointer into the memory copy of the file format. | |
| 43 // FileOffsetToPointer(0) returns a pointer to the start of the file format. | |
| 44 const uint8_t* OffsetToPointer(size_t offset) const; | |
| 45 | |
| 46 protected: | 48 protected: |
| 47 Disassembler(const void* start, size_t length); | 49 Disassembler(const void* start, size_t length); |
| 48 | 50 |
| 49 bool Good(); | 51 bool Good(); |
| 50 bool Bad(const char *reason); | 52 bool Bad(const char *reason); |
| 51 | 53 |
| 52 // Returns true if the array lies within our memory region. | 54 // Returns true if the array lies within our memory region. |
| 53 bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) { | 55 bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) { |
| 54 return offset <= length() && elements <= (length() - offset) / element_size; | 56 return offset <= length() && elements <= (length() - offset) / element_size; |
| 55 } | 57 } |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 69 size_t length_; // In current memory. | 71 size_t length_; // In current memory. |
| 70 const uint8_t* start_; // In current memory, base for 'file offsets'. | 72 const uint8_t* start_; // In current memory, base for 'file offsets'. |
| 71 const uint8_t* end_; // In current memory. | 73 const uint8_t* end_; // In current memory. |
| 72 | 74 |
| 73 DISALLOW_COPY_AND_ASSIGN(Disassembler); | 75 DISALLOW_COPY_AND_ASSIGN(Disassembler); |
| 74 }; | 76 }; |
| 75 | 77 |
| 76 } // namespace courgette | 78 } // namespace courgette |
| 77 | 79 |
| 78 #endif // COURGETTE_DISASSEMBLER_H_ | 80 #endif // COURGETTE_DISASSEMBLER_H_ |
| OLD | NEW |