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

Side by Side Diff: courgette/disassembler_win32_x86.h

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

Powered by Google App Engine
This is Rietveld 408576698