| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_X64_H_ | 5 #ifndef COURGETTE_DISASSEMBLER_WIN32_X64_H_ |
| 6 #define COURGETTE_DISASSEMBLER_WIN32_X64_H_ | 6 #define COURGETTE_DISASSEMBLER_WIN32_X64_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 DisassemblerWin32X64 : public Disassembler { | 24 class DisassemblerWin32X64 : public Disassembler { |
| 26 public: | 25 public: |
| 27 explicit DisassemblerWin32X64(const void* start, size_t length); | 26 explicit DisassemblerWin32X64(const void* start, size_t length); |
| 28 | 27 |
| 29 // Disassembler interfaces. | 28 virtual ExecutableType kind() { return EXE_WIN_32_X64; } |
| 30 RVA FileOffsetToRVA(FileOffset file_offset) const override; | |
| 31 FileOffset RVAToFileOffset(RVA rva) const override; | |
| 32 ExecutableType kind() const override { return EXE_WIN_32_X64; } | |
| 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 uint64_t image_base() const { return image_base_; } | 85 uint64_t image_base() const { return 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 // Subsumes rva != kUnassignedRVA. |
| 92 bool IsValidRVA(RVA rva) const { return rva < size_of_image_; } |
| 93 |
| 94 // Returns description of the RVA, e.g. ".text+0x1243". For debugging only. |
| 79 std::string DescribeRVA(RVA rva) const; | 95 std::string DescribeRVA(RVA rva) const; |
| 80 | 96 |
| 81 // Finds the first section at file_offset or above. Does not return sections | 97 // Finds the first section at file_offset or above. Does not return sections |
| 82 // that have no raw bytes in the file. | 98 // that have no raw bytes in the file. |
| 83 const Section* FindNextSection(FileOffset file_offset) const; | 99 const Section* FindNextSection(uint32_t file_offset) const; |
| 100 |
| 101 // There are 2 'coordinate systems' for reasoning about executables. |
| 102 // FileOffset - the the offset within a single .EXE or .DLL *file*. |
| 103 // RVA - relative virtual address (offset within *loaded image*) |
| 104 // FileOffsetToRVA and RVAToFileOffset convert between these representations. |
| 105 |
| 106 RVA FileOffsetToRVA(uint32_t offset) const; |
| 84 | 107 |
| 85 private: | 108 private: |
| 109 |
| 86 bool ReadDataDirectory(int index, ImageDataDirectory* dir); | 110 bool ReadDataDirectory(int index, ImageDataDirectory* dir); |
| 87 | 111 |
| 88 bool incomplete_disassembly_; // true if can omit "uninteresting" bits. | 112 bool incomplete_disassembly_; // 'true' if can leave out 'uninteresting' bits |
| 89 | 113 |
| 90 std::vector<RVA> abs32_locations_; | 114 std::vector<RVA> abs32_locations_; |
| 91 std::vector<RVA> rel32_locations_; | 115 std::vector<RVA> rel32_locations_; |
| 92 | 116 |
| 93 // | 117 // |
| 94 // Information that is valid after ParseHeader() succeeds. | 118 // Fields that are always valid. |
| 95 // | 119 // |
| 96 bool is_PE32_plus_; // PE32_plus is for 64 bit executables. | 120 |
| 121 // |
| 122 // Information that is valid after successful ParseHeader. |
| 123 // |
| 124 bool is_PE32_plus_; // PE32_plus is for 64 bit executables. |
| 97 | 125 |
| 98 // Location and size of IMAGE_OPTIONAL_HEADER in the buffer. | 126 // Location and size of IMAGE_OPTIONAL_HEADER in the buffer. |
| 99 const uint8_t* optional_header_; | 127 const uint8_t* optional_header_; |
| 100 uint16_t size_of_optional_header_; | 128 uint16_t size_of_optional_header_; |
| 101 uint16_t offset_of_data_directories_; | 129 uint16_t offset_of_data_directories_; |
| 102 | 130 |
| 103 uint16_t machine_type_; | 131 uint16_t machine_type_; |
| 104 uint16_t number_of_sections_; | 132 uint16_t number_of_sections_; |
| 105 const Section *sections_; | 133 const Section *sections_; |
| 106 bool has_text_section_; | 134 bool has_text_section_; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 123 ImageDataDirectory bound_import_table_; | 151 ImageDataDirectory bound_import_table_; |
| 124 ImageDataDirectory import_address_table_; | 152 ImageDataDirectory import_address_table_; |
| 125 ImageDataDirectory delay_import_descriptor_; | 153 ImageDataDirectory delay_import_descriptor_; |
| 126 ImageDataDirectory clr_runtime_header_; | 154 ImageDataDirectory clr_runtime_header_; |
| 127 | 155 |
| 128 #if COURGETTE_HISTOGRAM_TARGETS | 156 #if COURGETTE_HISTOGRAM_TARGETS |
| 129 std::map<RVA, int> abs32_target_rvas_; | 157 std::map<RVA, int> abs32_target_rvas_; |
| 130 std::map<RVA, int> rel32_target_rvas_; | 158 std::map<RVA, int> rel32_target_rvas_; |
| 131 #endif | 159 #endif |
| 132 | 160 |
| 161 |
| 133 DISALLOW_COPY_AND_ASSIGN(DisassemblerWin32X64); | 162 DISALLOW_COPY_AND_ASSIGN(DisassemblerWin32X64); |
| 134 }; | 163 }; |
| 135 | 164 |
| 136 } // namespace courgette | 165 } // namespace courgette |
| 137 | |
| 138 #endif // COURGETTE_DISASSEMBLER_WIN32_X64_H_ | 166 #endif // COURGETTE_DISASSEMBLER_WIN32_X64_H_ |
| OLD | NEW |