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

Side by Side Diff: courgette/disassembler_win32_x86.h

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

Powered by Google App Engine
This is Rietveld 408576698