| 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_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 <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
| 11 #include "base/macros.h" |
| 9 #include "courgette/disassembler.h" | 12 #include "courgette/disassembler.h" |
| 10 #include "courgette/memory_allocator.h" | 13 #include "courgette/memory_allocator.h" |
| 11 #include "courgette/types_win_pe.h" | 14 #include "courgette/types_win_pe.h" |
| 12 | 15 |
| 13 #ifdef COURGETTE_HISTOGRAM_TARGETS | 16 #ifdef COURGETTE_HISTOGRAM_TARGETS |
| 14 #include <map> | 17 #include <map> |
| 15 #endif | 18 #endif |
| 16 | 19 |
| 17 namespace courgette { | 20 namespace courgette { |
| 18 | 21 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 29 // functions may be called. | 32 // functions may be called. |
| 30 virtual bool ParseHeader(); | 33 virtual bool ParseHeader(); |
| 31 | 34 |
| 32 virtual bool Disassemble(AssemblyProgram* target); | 35 virtual bool Disassemble(AssemblyProgram* target); |
| 33 | 36 |
| 34 // | 37 // |
| 35 // Exposed for test purposes | 38 // Exposed for test purposes |
| 36 // | 39 // |
| 37 | 40 |
| 38 bool has_text_section() const { return has_text_section_; } | 41 bool has_text_section() const { return has_text_section_; } |
| 39 uint32 size_of_code() const { return size_of_code_; } | 42 uint32_t size_of_code() const { return size_of_code_; } |
| 40 bool is_32bit() const { return !is_PE32_plus_; } | 43 bool is_32bit() const { return !is_PE32_plus_; } |
| 41 | 44 |
| 42 // Returns 'true' if the base relocation table can be parsed. | 45 // Returns 'true' if the base relocation table can be parsed. |
| 43 // 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 |
| 44 // that are listed in the base relocation table. | 47 // that are listed in the base relocation table. |
| 45 bool ParseRelocs(std::vector<RVA> *addresses); | 48 bool ParseRelocs(std::vector<RVA> *addresses); |
| 46 | 49 |
| 47 // Returns Section containing the relative virtual address, or NULL if none. | 50 // Returns Section containing the relative virtual address, or NULL if none. |
| 48 const Section* RVAToSection(RVA rva) const; | 51 const Section* RVAToSection(RVA rva) const; |
| 49 | 52 |
| 50 static const int kNoOffset = -1; | 53 static const int kNoOffset = -1; |
| 51 // Returns kNoOffset if there is no file offset corresponding to 'rva'. | 54 // Returns kNoOffset if there is no file offset corresponding to 'rva'. |
| 52 int RVAToFileOffset(RVA rva) const; | 55 int RVAToFileOffset(RVA rva) const; |
| 53 | 56 |
| 54 // Returns same as FileOffsetToPointer(RVAToFileOffset(rva)) except that NULL | 57 // Returns same as FileOffsetToPointer(RVAToFileOffset(rva)) except that NULL |
| 55 // is returned if there is no file offset corresponding to 'rva'. | 58 // is returned if there is no file offset corresponding to 'rva'. |
| 56 const uint8* RVAToPointer(RVA rva) const; | 59 const uint8_t* RVAToPointer(RVA rva) const; |
| 57 | 60 |
| 58 static std::string SectionName(const Section* section); | 61 static std::string SectionName(const Section* section); |
| 59 | 62 |
| 60 protected: | 63 protected: |
| 61 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; | 64 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; |
| 62 bool ParseAbs32Relocs(); | 65 bool ParseAbs32Relocs(); |
| 63 void ParseRel32RelocsFromSections(); | 66 void ParseRel32RelocsFromSections(); |
| 64 void ParseRel32RelocsFromSection(const Section* section); | 67 void ParseRel32RelocsFromSection(const Section* section); |
| 65 | 68 |
| 66 CheckBool ParseNonSectionFileRegion(uint32 start_file_offset, | 69 CheckBool ParseNonSectionFileRegion(uint32_t start_file_offset, |
| 67 uint32 end_file_offset, AssemblyProgram* program) WARN_UNUSED_RESULT; | 70 uint32_t end_file_offset, |
| 71 AssemblyProgram* program) |
| 72 WARN_UNUSED_RESULT; |
| 68 CheckBool ParseFileRegion(const Section* section, | 73 CheckBool ParseFileRegion(const Section* section, |
| 69 uint32 start_file_offset, uint32 end_file_offset, | 74 uint32_t start_file_offset, |
| 70 AssemblyProgram* program) WARN_UNUSED_RESULT; | 75 uint32_t end_file_offset, |
| 76 AssemblyProgram* program) WARN_UNUSED_RESULT; |
| 71 | 77 |
| 72 #if COURGETTE_HISTOGRAM_TARGETS | 78 #if COURGETTE_HISTOGRAM_TARGETS |
| 73 void HistogramTargets(const char* kind, const std::map<RVA, int>& map); | 79 void HistogramTargets(const char* kind, const std::map<RVA, int>& map); |
| 74 #endif | 80 #endif |
| 75 | 81 |
| 76 // 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 |
| 77 // do this with is the image base address. 'image_base' is valid only for | 83 // do this with is the image base address. 'image_base' is valid only for |
| 78 // 32-bit executables. 'image_base_64' is valid for 32- and 64-bit executable. | 84 // 32-bit executables. 'image_base_64' is valid for 32- and 64-bit executable. |
| 79 uint32 image_base() const { return static_cast<uint32>(image_base_); } | 85 uint32_t image_base() const { return static_cast<uint32_t>(image_base_); } |
| 80 | 86 |
| 81 const ImageDataDirectory& base_relocation_table() const { | 87 const ImageDataDirectory& base_relocation_table() const { |
| 82 return base_relocation_table_; | 88 return base_relocation_table_; |
| 83 } | 89 } |
| 84 | 90 |
| 85 // 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. |
| 86 std::string DescribeRVA(RVA rva) const; | 92 std::string DescribeRVA(RVA rva) const; |
| 87 | 93 |
| 88 // 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 |
| 89 // that have no raw bytes in the file. | 95 // that have no raw bytes in the file. |
| 90 const Section* FindNextSection(uint32 file_offset) const; | 96 const Section* FindNextSection(uint32_t file_offset) const; |
| 91 | 97 |
| 92 // There are 2 'coordinate systems' for reasoning about executables. | 98 // There are 2 'coordinate systems' for reasoning about executables. |
| 93 // FileOffset - the the offset within a single .EXE or .DLL *file*. | 99 // FileOffset - the the offset within a single .EXE or .DLL *file*. |
| 94 // RVA - relative virtual address (offset within *loaded image*) | 100 // RVA - relative virtual address (offset within *loaded image*) |
| 95 // FileOffsetToRVA and RVAToFileOffset convert between these representations. | 101 // FileOffsetToRVA and RVAToFileOffset convert between these representations. |
| 96 | 102 |
| 97 RVA FileOffsetToRVA(uint32 offset) const; | 103 RVA FileOffsetToRVA(uint32_t offset) const; |
| 98 | |
| 99 | 104 |
| 100 private: | 105 private: |
| 101 | 106 |
| 102 bool ReadDataDirectory(int index, ImageDataDirectory* dir); | 107 bool ReadDataDirectory(int index, ImageDataDirectory* dir); |
| 103 | 108 |
| 104 bool incomplete_disassembly_; // 'true' if can leave out 'uninteresting' bits | 109 bool incomplete_disassembly_; // 'true' if can leave out 'uninteresting' bits |
| 105 | 110 |
| 106 std::vector<RVA> abs32_locations_; | 111 std::vector<RVA> abs32_locations_; |
| 107 std::vector<RVA> rel32_locations_; | 112 std::vector<RVA> rel32_locations_; |
| 108 | 113 |
| 109 // | 114 // |
| 110 // Fields that are always valid. | 115 // Fields that are always valid. |
| 111 // | 116 // |
| 112 | 117 |
| 113 // | 118 // |
| 114 // Information that is valid after successful ParseHeader. | 119 // Information that is valid after successful ParseHeader. |
| 115 // | 120 // |
| 116 bool is_PE32_plus_; // PE32_plus is for 64 bit executables. | 121 bool is_PE32_plus_; // PE32_plus is for 64 bit executables. |
| 117 | 122 |
| 118 // Location and size of IMAGE_OPTIONAL_HEADER in the buffer. | 123 // Location and size of IMAGE_OPTIONAL_HEADER in the buffer. |
| 119 const uint8 *optional_header_; | 124 const uint8_t* optional_header_; |
| 120 uint16 size_of_optional_header_; | 125 uint16_t size_of_optional_header_; |
| 121 uint16 offset_of_data_directories_; | 126 uint16_t offset_of_data_directories_; |
| 122 | 127 |
| 123 uint16 machine_type_; | 128 uint16_t machine_type_; |
| 124 uint16 number_of_sections_; | 129 uint16_t number_of_sections_; |
| 125 const Section *sections_; | 130 const Section *sections_; |
| 126 bool has_text_section_; | 131 bool has_text_section_; |
| 127 | 132 |
| 128 uint32 size_of_code_; | 133 uint32_t size_of_code_; |
| 129 uint32 size_of_initialized_data_; | 134 uint32_t size_of_initialized_data_; |
| 130 uint32 size_of_uninitialized_data_; | 135 uint32_t size_of_uninitialized_data_; |
| 131 RVA base_of_code_; | 136 RVA base_of_code_; |
| 132 RVA base_of_data_; | 137 RVA base_of_data_; |
| 133 | 138 |
| 134 uint64 image_base_; // range limited to 32 bits for 32 bit executable | 139 uint64_t image_base_; // range limited to 32 bits for 32 bit executable |
| 135 uint32 size_of_image_; | 140 uint32_t size_of_image_; |
| 136 int number_of_data_directories_; | 141 int number_of_data_directories_; |
| 137 | 142 |
| 138 ImageDataDirectory export_table_; | 143 ImageDataDirectory export_table_; |
| 139 ImageDataDirectory import_table_; | 144 ImageDataDirectory import_table_; |
| 140 ImageDataDirectory resource_table_; | 145 ImageDataDirectory resource_table_; |
| 141 ImageDataDirectory exception_table_; | 146 ImageDataDirectory exception_table_; |
| 142 ImageDataDirectory base_relocation_table_; | 147 ImageDataDirectory base_relocation_table_; |
| 143 ImageDataDirectory bound_import_table_; | 148 ImageDataDirectory bound_import_table_; |
| 144 ImageDataDirectory import_address_table_; | 149 ImageDataDirectory import_address_table_; |
| 145 ImageDataDirectory delay_import_descriptor_; | 150 ImageDataDirectory delay_import_descriptor_; |
| 146 ImageDataDirectory clr_runtime_header_; | 151 ImageDataDirectory clr_runtime_header_; |
| 147 | 152 |
| 148 #if COURGETTE_HISTOGRAM_TARGETS | 153 #if COURGETTE_HISTOGRAM_TARGETS |
| 149 std::map<RVA, int> abs32_target_rvas_; | 154 std::map<RVA, int> abs32_target_rvas_; |
| 150 std::map<RVA, int> rel32_target_rvas_; | 155 std::map<RVA, int> rel32_target_rvas_; |
| 151 #endif | 156 #endif |
| 152 | 157 |
| 153 | 158 |
| 154 DISALLOW_COPY_AND_ASSIGN(DisassemblerWin32X86); | 159 DISALLOW_COPY_AND_ASSIGN(DisassemblerWin32X86); |
| 155 }; | 160 }; |
| 156 | 161 |
| 157 } // namespace courgette | 162 } // namespace courgette |
| 158 #endif // COURGETTE_DISASSEMBLER_WIN32_X86_H_ | 163 #endif // COURGETTE_DISASSEMBLER_WIN32_X86_H_ |
| OLD | NEW |