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

Side by Side Diff: courgette/disassembler_win32_x86.h

Issue 8166013: Further refactoring, move ImageInfo into Disassembler/DisassemblerWin32X86. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 2 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 | Annotate | Revision Log
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_WIN32_X86_H_ 5 #ifndef COURGETTE_DISASSEMBLER_WIN32_X86_H_
6 #define COURGETTE_DISASSEMBLER_WIN32_X86_H_ 6 #define COURGETTE_DISASSEMBLER_WIN32_X86_H_
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "courgette/disassembler.h" 9 #include "courgette/disassembler.h"
10 #include "courgette/image_info.h"
11 #include "courgette/memory_allocator.h" 10 #include "courgette/memory_allocator.h"
11 #include "courgette/types_win_pe.h"
12 12
13 namespace courgette { 13 namespace courgette {
14 14
15 class AssemblyProgram; 15 class AssemblyProgram;
16 16
17 class DisassemblerWin32X86 : public Disassembler { 17 class DisassemblerWin32X86 : public Disassembler {
18 public: 18 public:
19 explicit DisassemblerWin32X86(PEInfo* pe_info); 19 explicit DisassemblerWin32X86(const void* start, size_t length);
20
21 virtual ExecutableType kind() { return WIN32_X86; }
22
23 // Returns 'true' if the buffer appears to point to a Windows 32 bit
24 // executable, 'false' otherwise. If ParseHeader() succeeds, other member
25 // functions may be called.
26 virtual bool ParseHeader();
20 27
21 virtual bool Disassemble(AssemblyProgram* target); 28 virtual bool Disassemble(AssemblyProgram* target);
22 29
30 //
31 // Exposed for test purposes
32 //
33
34 // Returns 'true' if the base relocation table can be parsed.
35 // Output is a vector of the RVAs corresponding to locations within executable
36 // that are listed in the base relocation table.
37 bool ParseRelocs(std::vector<RVA> *addresses);
38
39 bool has_text_section() const { return has_text_section_; }
40
41 uint32 size_of_code() const { return size_of_code_; }
42
43 // Returns Section containing the relative virtual address, or NULL if none.
44 const Section* RVAToSection(RVA rva) const;
45
46 static const int kNoOffset = -1;
47 // Returns kNoOffset if there is no file offset corresponding to 'rva'.
48 int RVAToFileOffset(RVA rva) const;
49
50 // Returns same as FileOffsetToPointer(RVAToFileOffset(rva)) except that NULL
51 // is returned if there is no file offset corresponding to 'rva'.
52 const uint8* RVAToPointer(RVA rva) const;
53
54 static std::string SectionName(const Section* section);
55
23 protected: 56 protected:
24 PEInfo& pe_info() { return *pe_info_; }
25
26 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; 57 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT;
27 bool ParseAbs32Relocs(); 58 bool ParseAbs32Relocs();
28 void ParseRel32RelocsFromSections(); 59 void ParseRel32RelocsFromSections();
29 void ParseRel32RelocsFromSection(const Section* section); 60 void ParseRel32RelocsFromSection(const Section* section);
30 61
31 CheckBool ParseNonSectionFileRegion(uint32 start_file_offset, 62 CheckBool ParseNonSectionFileRegion(uint32 start_file_offset,
32 uint32 end_file_offset, AssemblyProgram* program) WARN_UNUSED_RESULT; 63 uint32 end_file_offset, AssemblyProgram* program) WARN_UNUSED_RESULT;
33 CheckBool ParseFileRegion(const Section* section, 64 CheckBool ParseFileRegion(const Section* section,
34 uint32 start_file_offset, uint32 end_file_offset, 65 uint32 start_file_offset, uint32 end_file_offset,
35 AssemblyProgram* program) WARN_UNUSED_RESULT; 66 AssemblyProgram* program) WARN_UNUSED_RESULT;
36 67
37 #if COURGETTE_HISTOGRAM_TARGETS 68 #if COURGETTE_HISTOGRAM_TARGETS
38 void HistogramTargets(const char* kind, const std::map<RVA, int>& map); 69 void HistogramTargets(const char* kind, const std::map<RVA, int>& map);
39 #endif 70 #endif
40 71
41 PEInfo* pe_info_; 72 bool is_32bit() const { return !is_PE32_plus_; }
73
74 // Most addresses are represented as 32-bit RVAs. The one address we can't
75 // do this with is the image base address. 'image_base' is valid only for
76 // 32-bit executables. 'image_base_64' is valid for 32- and 64-bit executable.
77 uint32 image_base() const { return static_cast<uint32>(image_base_); }
78
79 const ImageDataDirectory& base_relocation_table() const {
80 return base_relocation_table_;
81 }
82
83 bool IsValidRVA(RVA rva) const { return rva < size_of_image_; }
84
85 // Returns description of the RVA, e.g. ".text+0x1243". For debugging only.
86 std::string DescribeRVA(RVA rva) const;
87
88 // Finds the first section at file_offset or above. Does not return sections
89 // that have no raw bytes in the file.
90 const Section* FindNextSection(uint32 file_offset) const;
91
92 // There are 2 'coordinate systems' for reasoning about executables.
93 // FileOffset - the the offset within a single .EXE or .DLL *file*.
94 // RVA - relative virtual address (offset within *loaded image*)
95 // FileOffsetToRVA and RVAToFileOffset convert between these representations.
96
97 RVA FileOffsetToRVA(uint32 offset) const;
98
99
100 private:
101
102 bool ReadDataDirectory(int index, ImageDataDirectory* dir);
103
42 bool incomplete_disassembly_; // 'true' if can leave out 'uninteresting' bits 104 bool incomplete_disassembly_; // 'true' if can leave out 'uninteresting' bits
43 105
44 std::vector<RVA> abs32_locations_; 106 std::vector<RVA> abs32_locations_;
45 std::vector<RVA> rel32_locations_; 107 std::vector<RVA> rel32_locations_;
46 108
109 //
110 // Fields that are always valid.
111 //
112
113 //
114 // Information that is valid after successful ParseHeader.
115 //
116 bool is_PE32_plus_; // PE32_plus is for 64 bit executables.
117
118 // Location and size of IMAGE_OPTIONAL_HEADER in the buffer.
119 const uint8 *optional_header_;
120 uint16 size_of_optional_header_;
121 uint16 offset_of_data_directories_;
122
123 uint16 machine_type_;
124 uint16 number_of_sections_;
125 const Section *sections_;
126 bool has_text_section_;
127
128 uint32 size_of_code_;
129 uint32 size_of_initialized_data_;
130 uint32 size_of_uninitialized_data_;
131 RVA base_of_code_;
132 RVA base_of_data_;
133
134 uint64 image_base_; // range limited to 32 bits for 32 bit executable
135 uint32 size_of_image_;
136 int number_of_data_directories_;
137
138 ImageDataDirectory export_table_;
139 ImageDataDirectory import_table_;
140 ImageDataDirectory resource_table_;
141 ImageDataDirectory exception_table_;
142 ImageDataDirectory base_relocation_table_;
143 ImageDataDirectory bound_import_table_;
144 ImageDataDirectory import_address_table_;
145 ImageDataDirectory delay_import_descriptor_;
146 ImageDataDirectory clr_runtime_header_;
147
47 #if COURGETTE_HISTOGRAM_TARGETS 148 #if COURGETTE_HISTOGRAM_TARGETS
48 std::map<RVA, int> abs32_target_rvas_; 149 std::map<RVA, int> abs32_target_rvas_;
49 std::map<RVA, int> rel32_target_rvas_; 150 std::map<RVA, int> rel32_target_rvas_;
50 #endif 151 #endif
51 152
153
52 DISALLOW_COPY_AND_ASSIGN(DisassemblerWin32X86); 154 DISALLOW_COPY_AND_ASSIGN(DisassemblerWin32X86);
53 }; 155 };
54 156
55 } // namespace courgette 157 } // namespace courgette
56 #endif // COURGETTE_DISASSEMBLER_WIN32_X86_H_ 158 #endif // COURGETTE_DISASSEMBLER_WIN32_X86_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698