| 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 : public AddressTranslator { | 19 class Disassembler { |
| 20 public: | 20 public: |
| 21 virtual ~Disassembler(); | 21 virtual ~Disassembler(); |
| 22 | 22 |
| 23 // AddressTranslator interfaces. | 23 virtual ExecutableType kind() { return EXE_UNKNOWN; } |
| 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; | |
| 28 | 24 |
| 29 virtual ExecutableType kind() const = 0; | 25 // ok() may always be called but returns 'true' only after ParseHeader |
| 26 // succeeds. |
| 27 bool ok() const { return failure_reason_ == NULL; } |
| 30 | 28 |
| 31 // Returns true if the buffer appears to be a valid executable of the expected | 29 // Returns 'true' if the buffer appears to be a valid executable of the |
| 32 // type, and false otherwise. This needs not be called before Disassemble(). | 30 // expected type. It is not required that this be called before Disassemble. |
| 33 virtual bool ParseHeader() = 0; | 31 virtual bool ParseHeader() = 0; |
| 34 | 32 |
| 35 // Disassembles the item passed to the factory method into the output | 33 // Disassembles the item passed to the factory method into the output |
| 36 // parameter 'program'. | 34 // parameter 'program'. |
| 37 virtual bool Disassemble(AssemblyProgram* program) = 0; | 35 virtual bool Disassemble(AssemblyProgram* program) = 0; |
| 38 | 36 |
| 39 // ok() may always be called but returns true only after ParseHeader() | 37 // Returns the length of the source executable. May reduce after ParseHeader. |
| 40 // succeeds. | |
| 41 bool ok() const { return failure_reason_ == nullptr; } | |
| 42 | |
| 43 // Returns the length of the image. May reduce after ParseHeader(). | |
| 44 size_t length() const { return length_; } | 38 size_t length() const { return length_; } |
| 45 const uint8_t* start() const { return start_; } | 39 const uint8_t* start() const { return start_; } |
| 46 const uint8_t* end() const { return end_; } | 40 const uint8_t* end() const { return end_; } |
| 47 | 41 |
| 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 |
| 48 protected: | 46 protected: |
| 49 Disassembler(const void* start, size_t length); | 47 Disassembler(const void* start, size_t length); |
| 50 | 48 |
| 51 bool Good(); | 49 bool Good(); |
| 52 bool Bad(const char *reason); | 50 bool Bad(const char *reason); |
| 53 | 51 |
| 54 // Returns true if the array lies within our memory region. | 52 // Returns true if the array lies within our memory region. |
| 55 bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) { | 53 bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) { |
| 56 return offset <= length() && elements <= (length() - offset) / element_size; | 54 return offset <= length() && elements <= (length() - offset) / element_size; |
| 57 } | 55 } |
| 58 | 56 |
| 59 // Reduce the length of the image in memory. Does not actually free | 57 // Reduce the length of the image in memory. Does not actually free |
| 60 // (or realloc) any memory. Usually only called via ParseHeader(). | 58 // (or realloc) any memory. Usually only called via ParseHeader() |
| 61 void ReduceLength(size_t reduced_length); | 59 void ReduceLength(size_t reduced_length); |
| 62 | 60 |
| 63 private: | 61 private: |
| 64 const char* failure_reason_; | 62 const char* failure_reason_; |
| 65 | 63 |
| 66 // | 64 // |
| 67 // Basic information that is always valid after construction, although | 65 // Basic information that is always valid after Construction, though |
| 68 // ParseHeader() may shorten |length_| if the executable is shorter than the | 66 // ParseHeader may shorten the length if the executable is shorter than |
| 69 // total data. | 67 // the total data. |
| 70 // | 68 // |
| 71 size_t length_; // In current memory. | 69 size_t length_; // In current memory. |
| 72 const uint8_t* start_; // In current memory, base for 'file offsets'. | 70 const uint8_t* start_; // In current memory, base for 'file offsets'. |
| 73 const uint8_t* end_; // In current memory. | 71 const uint8_t* end_; // In current memory. |
| 74 | 72 |
| 75 DISALLOW_COPY_AND_ASSIGN(Disassembler); | 73 DISALLOW_COPY_AND_ASSIGN(Disassembler); |
| 76 }; | 74 }; |
| 77 | 75 |
| 78 } // namespace courgette | 76 } // namespace courgette |
| 79 | 77 |
| 80 #endif // COURGETTE_DISASSEMBLER_H_ | 78 #endif // COURGETTE_DISASSEMBLER_H_ |
| OLD | NEW |