Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(856)

Side by Side Diff: courgette/disassembler.h

Issue 2771753004: [Courgette] Refactor: Unify Disassembler::Disassemble() and instantiate AssemblyProgram there. (Closed)
Patch Set: Fix signed/unsigned comparison issue in test. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « courgette/assembly_program.cc ('k') | courgette/disassembler.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory>
10 #include <vector> 11 #include <vector>
11 12
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "courgette/courgette.h" 14 #include "courgette/courgette.h"
14 #include "courgette/image_utils.h" 15 #include "courgette/image_utils.h"
16 #include "courgette/instruction_utils.h"
15 17
16 namespace courgette { 18 namespace courgette {
17 19
18 class AssemblyProgram; 20 class AssemblyProgram;
19 21
20 class Disassembler : public AddressTranslator { 22 class Disassembler : public AddressTranslator {
21 public: 23 public:
22 // Visitor/adaptor to translate RVA to target RVA for abs32. 24 // Visitor/adaptor to translate RVA to target RVA for abs32.
23 class RvaVisitor_Abs32 : public VectorRvaVisitor<RVA> { 25 class RvaVisitor_Abs32 : public VectorRvaVisitor<RVA> {
24 public: 26 public:
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 const uint8_t* FileOffsetToPointer(FileOffset file_offset) const override; 61 const uint8_t* FileOffsetToPointer(FileOffset file_offset) const override;
60 const uint8_t* RVAToPointer(RVA rva) const override; 62 const uint8_t* RVAToPointer(RVA rva) const override;
61 RVA PointerToTargetRVA(const uint8_t* p) const override = 0; 63 RVA PointerToTargetRVA(const uint8_t* p) const override = 0;
62 64
63 virtual ExecutableType kind() const = 0; 65 virtual ExecutableType kind() const = 0;
64 66
65 // Returns the preferred image base address. Using uint64_t to accommodate the 67 // Returns the preferred image base address. Using uint64_t to accommodate the
66 // general case of 64-bit architectures. 68 // general case of 64-bit architectures.
67 virtual uint64_t image_base() const = 0; 69 virtual uint64_t image_base() const = 0;
68 70
71 // Extracts and stores locations of abs32 references from the image file.
72 virtual bool ExtractAbs32Locations() = 0;
73
74 // Extracts and stores locations of rel32 references from the image file.
75 virtual bool ExtractRel32Locations() = 0;
76
69 // Returns a caller-owned new RvaVisitor to iterate through abs32 target RVAs. 77 // Returns a caller-owned new RvaVisitor to iterate through abs32 target RVAs.
70 virtual RvaVisitor* CreateAbs32TargetRvaVisitor() = 0; 78 virtual RvaVisitor* CreateAbs32TargetRvaVisitor() = 0;
71 79
72 // Returns a caller-owned new RvaVisitor to iterate through rel32 target RVAs. 80 // Returns a caller-owned new RvaVisitor to iterate through rel32 target RVAs.
73 virtual RvaVisitor* CreateRel32TargetRvaVisitor() = 0; 81 virtual RvaVisitor* CreateRel32TargetRvaVisitor() = 0;
74 82
75 // Removes unused rel32 locations (architecture-specific). This is needed 83 // Removes unused rel32 locations (architecture-specific). This is needed
76 // because we may remove rel32 Labels along the way. As a result the matching 84 // because we may remove rel32 Labels along the way. As a result the matching
77 // matching rel32 addresses become unused. Removing them saves space. 85 // rel32 addresses become unused. Removing them saves space.
78 virtual void RemoveUnusedRel32Locations(AssemblyProgram* program) = 0; 86 virtual void RemoveUnusedRel32Locations(AssemblyProgram* program) = 0;
79 87
80 // Returns true if the buffer appears to be a valid executable of the expected 88 // Extracts structural data from the main image. Returns true if the image
81 // type, and false otherwise. This needs not be called before Disassemble(). 89 // appears to be a valid executable of the expected type, or false otherwise.
90 // This needs to be called before Disassemble().
82 virtual bool ParseHeader() = 0; 91 virtual bool ParseHeader() = 0;
83 92
84 // Disassembles the item passed to the factory method into the output 93 // Extracts and stores references from the main image. Returns a new
85 // parameter 'program'. 94 // AssemblyProgram initialized using data parsed from the main image, or null
86 virtual bool Disassemble(AssemblyProgram* program) = 0; 95 // on failure.
96 std::unique_ptr<AssemblyProgram> Disassemble();
87 97
88 // ok() may always be called but returns true only after ParseHeader() 98 // ok() may always be called but returns true only after ParseHeader()
89 // succeeds. 99 // succeeds.
90 bool ok() const { return failure_reason_ == nullptr; } 100 bool ok() const { return failure_reason_ == nullptr; }
91 101
92 // Returns the length of the image. May reduce after ParseHeader(). 102 // Returns the length of the image. May reduce after ParseHeader().
93 size_t length() const { return length_; } 103 size_t length() const { return length_; }
94 const uint8_t* start() const { return start_; } 104 const uint8_t* start() const { return start_; }
95 const uint8_t* end() const { return end_; } 105 const uint8_t* end() const { return end_; }
96 106
97 protected: 107 protected:
98 Disassembler(const uint8_t* start, size_t length); 108 Disassembler(const uint8_t* start, size_t length);
99 109
100 bool Good(); 110 bool Good();
101 bool Bad(const char *reason); 111 bool Bad(const char *reason);
102 112
103 // Returns true if the array lies within our memory region. 113 // Returns true if the array lies within our memory region.
104 bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) { 114 bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) {
105 return offset <= length() && elements <= (length() - offset) / element_size; 115 return offset <= length() && elements <= (length() - offset) / element_size;
106 } 116 }
107 117
108 // Computes and stores all Labels before scanning program bytes. 118 // Computes and stores all Labels before scanning program bytes.
109 void PrecomputeLabels(AssemblyProgram* program); 119 void PrecomputeLabels(AssemblyProgram* program);
110 120
111 // Reduce the length of the image in memory. Does not actually free 121 // Reduce the length of the image in memory. Does not actually free
112 // (or realloc) any memory. Usually only called via ParseHeader(). 122 // (or realloc) any memory. Usually only called via ParseHeader().
113 void ReduceLength(size_t reduced_length); 123 void ReduceLength(size_t reduced_length);
114 124
125 // Returns a generator that emits instructions to a given receptor. |program|
126 // is required as helper.
127 virtual InstructionGenerator GetInstructionGenerator(
128 AssemblyProgram* program) = 0;
129
115 private: 130 private:
116 const char* failure_reason_; 131 const char* failure_reason_;
117 132
118 // 133 //
119 // Basic information that is always valid after construction, although 134 // Basic information that is always valid after construction, although
120 // ParseHeader() may shorten |length_| if the executable is shorter than the 135 // ParseHeader() may shorten |length_| if the executable is shorter than the
121 // total data. 136 // total data.
122 // 137 //
123 size_t length_; // In current memory. 138 size_t length_; // In current memory.
124 const uint8_t* start_; // In current memory, base for 'file offsets'. 139 const uint8_t* start_; // In current memory, base for 'file offsets'.
125 const uint8_t* end_; // In current memory. 140 const uint8_t* end_; // In current memory.
126 141
127 DISALLOW_COPY_AND_ASSIGN(Disassembler); 142 DISALLOW_COPY_AND_ASSIGN(Disassembler);
128 }; 143 };
129 144
130 } // namespace courgette 145 } // namespace courgette
131 146
132 #endif // COURGETTE_DISASSEMBLER_H_ 147 #endif // COURGETTE_DISASSEMBLER_H_
OLDNEW
« no previous file with comments | « courgette/assembly_program.cc ('k') | courgette/disassembler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698