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 <vector> |
| 12 |
11 #include "base/macros.h" | 13 #include "base/macros.h" |
12 #include "courgette/courgette.h" | 14 #include "courgette/courgette.h" |
13 #include "courgette/image_utils.h" | 15 #include "courgette/image_utils.h" |
14 | 16 |
15 namespace courgette { | 17 namespace courgette { |
16 | 18 |
17 class AssemblyProgram; | 19 class AssemblyProgram; |
18 | 20 |
19 class Disassembler : public AddressTranslator { | 21 class Disassembler : public AddressTranslator { |
20 public: | 22 public: |
| 23 // Visitor/adaptor to translate RVA to target RVA for abs32. |
| 24 class RvaVisitor_Abs32 : public VectorRvaVisitor<RVA> { |
| 25 public: |
| 26 RvaVisitor_Abs32(const std::vector<RVA>& rva_locations, |
| 27 const AddressTranslator& translator); |
| 28 ~RvaVisitor_Abs32() override { } |
| 29 |
| 30 // VectorRvaVisitor<RVA> interfaces. |
| 31 RVA Get() const override; |
| 32 |
| 33 private: |
| 34 const AddressTranslator& translator_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(RvaVisitor_Abs32); |
| 37 }; |
| 38 |
| 39 // Visitor/adaptor to translate RVA to target RVA for rel32. |
| 40 class RvaVisitor_Rel32 : public VectorRvaVisitor<RVA> { |
| 41 public: |
| 42 RvaVisitor_Rel32(const std::vector<RVA>& rva_locations, |
| 43 const AddressTranslator& translator); |
| 44 ~RvaVisitor_Rel32() override { } |
| 45 |
| 46 // VectorRvaVisitor<RVA> interfaces. |
| 47 RVA Get() const override; |
| 48 |
| 49 private: |
| 50 const AddressTranslator& translator_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(RvaVisitor_Rel32); |
| 53 }; |
| 54 |
21 virtual ~Disassembler(); | 55 virtual ~Disassembler(); |
22 | 56 |
23 // AddressTranslator interfaces. | 57 // AddressTranslator interfaces. |
24 virtual RVA FileOffsetToRVA(FileOffset file_offset) const override = 0; | 58 virtual RVA FileOffsetToRVA(FileOffset file_offset) const override = 0; |
25 virtual FileOffset RVAToFileOffset(RVA rva) const override = 0; | 59 virtual FileOffset RVAToFileOffset(RVA rva) const override = 0; |
26 const uint8_t* FileOffsetToPointer(FileOffset file_offset) const override; | 60 const uint8_t* FileOffsetToPointer(FileOffset file_offset) const override; |
27 const uint8_t* RVAToPointer(RVA rva) const override; | 61 const uint8_t* RVAToPointer(RVA rva) const override; |
28 RVA PointerToTargetRVA(const uint8_t* p) const = 0; | 62 RVA PointerToTargetRVA(const uint8_t* p) const = 0; |
29 | 63 |
30 virtual ExecutableType kind() const = 0; | 64 virtual ExecutableType kind() const = 0; |
31 | 65 |
| 66 // Returns a caller-owned new RvaVisitor to iterate through abs32 target RVAs. |
| 67 virtual RvaVisitor* CreateAbs32TargetRvaVisitor() = 0; |
| 68 |
| 69 // Returns a caller-owned new RvaVisitor to iterate through rel32 target RVAs. |
| 70 virtual RvaVisitor* CreateRel32TargetRvaVisitor() = 0; |
| 71 |
| 72 // Removes unused rel32 locations (architecture-specific). This is needed |
| 73 // because we may remove rel32 Labels along the way. As a result the matching |
| 74 // matching rel32 addresses become unused. Removing them saves space. |
| 75 virtual void RemoveUnusedRel32Locations(AssemblyProgram* program) = 0; |
| 76 |
32 // Returns true if the buffer appears to be a valid executable of the expected | 77 // Returns true if the buffer appears to be a valid executable of the expected |
33 // type, and false otherwise. This needs not be called before Disassemble(). | 78 // type, and false otherwise. This needs not be called before Disassemble(). |
34 virtual bool ParseHeader() = 0; | 79 virtual bool ParseHeader() = 0; |
35 | 80 |
36 // Disassembles the item passed to the factory method into the output | 81 // Disassembles the item passed to the factory method into the output |
37 // parameter 'program'. | 82 // parameter 'program'. |
38 virtual bool Disassemble(AssemblyProgram* program) = 0; | 83 virtual bool Disassemble(AssemblyProgram* program) = 0; |
39 | 84 |
40 // ok() may always be called but returns true only after ParseHeader() | 85 // ok() may always be called but returns true only after ParseHeader() |
41 // succeeds. | 86 // succeeds. |
42 bool ok() const { return failure_reason_ == nullptr; } | 87 bool ok() const { return failure_reason_ == nullptr; } |
43 | 88 |
44 // Returns the length of the image. May reduce after ParseHeader(). | 89 // Returns the length of the image. May reduce after ParseHeader(). |
45 size_t length() const { return length_; } | 90 size_t length() const { return length_; } |
46 const uint8_t* start() const { return start_; } | 91 const uint8_t* start() const { return start_; } |
47 const uint8_t* end() const { return end_; } | 92 const uint8_t* end() const { return end_; } |
48 | 93 |
49 protected: | 94 protected: |
50 Disassembler(const void* start, size_t length); | 95 Disassembler(const void* start, size_t length); |
51 | 96 |
52 bool Good(); | 97 bool Good(); |
53 bool Bad(const char *reason); | 98 bool Bad(const char *reason); |
54 | 99 |
55 // Returns true if the array lies within our memory region. | 100 // Returns true if the array lies within our memory region. |
56 bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) { | 101 bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) { |
57 return offset <= length() && elements <= (length() - offset) / element_size; | 102 return offset <= length() && elements <= (length() - offset) / element_size; |
58 } | 103 } |
59 | 104 |
| 105 // Computes and stores all Labels before scanning program bytes. |
| 106 void PrecomputeLabels(AssemblyProgram* program); |
| 107 |
60 // Reduce the length of the image in memory. Does not actually free | 108 // Reduce the length of the image in memory. Does not actually free |
61 // (or realloc) any memory. Usually only called via ParseHeader(). | 109 // (or realloc) any memory. Usually only called via ParseHeader(). |
62 void ReduceLength(size_t reduced_length); | 110 void ReduceLength(size_t reduced_length); |
63 | 111 |
64 private: | 112 private: |
65 const char* failure_reason_; | 113 const char* failure_reason_; |
66 | 114 |
67 // | 115 // |
68 // Basic information that is always valid after construction, although | 116 // Basic information that is always valid after construction, although |
69 // ParseHeader() may shorten |length_| if the executable is shorter than the | 117 // ParseHeader() may shorten |length_| if the executable is shorter than the |
70 // total data. | 118 // total data. |
71 // | 119 // |
72 size_t length_; // In current memory. | 120 size_t length_; // In current memory. |
73 const uint8_t* start_; // In current memory, base for 'file offsets'. | 121 const uint8_t* start_; // In current memory, base for 'file offsets'. |
74 const uint8_t* end_; // In current memory. | 122 const uint8_t* end_; // In current memory. |
75 | 123 |
76 DISALLOW_COPY_AND_ASSIGN(Disassembler); | 124 DISALLOW_COPY_AND_ASSIGN(Disassembler); |
77 }; | 125 }; |
78 | 126 |
79 } // namespace courgette | 127 } // namespace courgette |
80 | 128 |
81 #endif // COURGETTE_DISASSEMBLER_H_ | 129 #endif // COURGETTE_DISASSEMBLER_H_ |
OLD | NEW |