diff options
author | dgarrett@chromium.org <dgarrett@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-26 00:50:20 +0000 |
---|---|---|
committer | dgarrett@chromium.org <dgarrett@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-26 00:50:20 +0000 |
commit | 423a381f4fd3efd99dfd7bc932777ea596cf7b17 (patch) | |
tree | fdbf4a4bc5f2b8d73b90020da470c40a22f4cc2a /courgette/disassembler_win32_x86.h | |
parent | da1543a1a526aefd1114853cf737846eb5c29640 (diff) | |
download | chromium_src-423a381f4fd3efd99dfd7bc932777ea596cf7b17.zip chromium_src-423a381f4fd3efd99dfd7bc932777ea596cf7b17.tar.gz chromium_src-423a381f4fd3efd99dfd7bc932777ea596cf7b17.tar.bz2 |
Further refactoring, move ImageInfo into Disassembler/DisassemblerWin32X86.
This means that all PE specific knowledge is now contained in a single class
which leaves us in pretty good shape for supporting ELF 32.
There are still widespread assumptions about being 32 bit, but those can be
addressed at a much later date.
BUG=None
TEST=Unittests
Review URL: http://codereview.chromium.org/8166013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@107260 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'courgette/disassembler_win32_x86.h')
-rw-r--r-- | courgette/disassembler_win32_x86.h | 110 |
1 files changed, 105 insertions, 5 deletions
diff --git a/courgette/disassembler_win32_x86.h b/courgette/disassembler_win32_x86.h index fe00b6d..733222f 100644 --- a/courgette/disassembler_win32_x86.h +++ b/courgette/disassembler_win32_x86.h @@ -7,8 +7,8 @@ #include "base/basictypes.h" #include "courgette/disassembler.h" -#include "courgette/image_info.h" #include "courgette/memory_allocator.h" +#include "courgette/types_win_pe.h" namespace courgette { @@ -16,13 +16,44 @@ class AssemblyProgram; class DisassemblerWin32X86 : public Disassembler { public: - explicit DisassemblerWin32X86(PEInfo* pe_info); + explicit DisassemblerWin32X86(const void* start, size_t length); + + virtual ExecutableType kind() { return WIN32_X86; } + + // Returns 'true' if the buffer appears to point to a Windows 32 bit + // executable, 'false' otherwise. If ParseHeader() succeeds, other member + // functions may be called. + virtual bool ParseHeader(); virtual bool Disassemble(AssemblyProgram* target); - protected: - PEInfo& pe_info() { return *pe_info_; } + // + // Exposed for test purposes + // + + bool has_text_section() const { return has_text_section_; } + uint32 size_of_code() const { return size_of_code_; } + bool is_32bit() const { return !is_PE32_plus_; } + + // Returns 'true' if the base relocation table can be parsed. + // Output is a vector of the RVAs corresponding to locations within executable + // that are listed in the base relocation table. + bool ParseRelocs(std::vector<RVA> *addresses); + + // Returns Section containing the relative virtual address, or NULL if none. + const Section* RVAToSection(RVA rva) const; + + static const int kNoOffset = -1; + // Returns kNoOffset if there is no file offset corresponding to 'rva'. + int RVAToFileOffset(RVA rva) const; + // Returns same as FileOffsetToPointer(RVAToFileOffset(rva)) except that NULL + // is returned if there is no file offset corresponding to 'rva'. + const uint8* RVAToPointer(RVA rva) const; + + static std::string SectionName(const Section* section); + + protected: CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; bool ParseAbs32Relocs(); void ParseRel32RelocsFromSections(); @@ -38,17 +69,86 @@ class DisassemblerWin32X86 : public Disassembler { void HistogramTargets(const char* kind, const std::map<RVA, int>& map); #endif - PEInfo* pe_info_; + // Most addresses are represented as 32-bit RVAs. The one address we can't + // do this with is the image base address. 'image_base' is valid only for + // 32-bit executables. 'image_base_64' is valid for 32- and 64-bit executable. + uint32 image_base() const { return static_cast<uint32>(image_base_); } + + const ImageDataDirectory& base_relocation_table() const { + return base_relocation_table_; + } + + bool IsValidRVA(RVA rva) const { return rva < size_of_image_; } + + // Returns description of the RVA, e.g. ".text+0x1243". For debugging only. + std::string DescribeRVA(RVA rva) const; + + // Finds the first section at file_offset or above. Does not return sections + // that have no raw bytes in the file. + const Section* FindNextSection(uint32 file_offset) const; + + // There are 2 'coordinate systems' for reasoning about executables. + // FileOffset - the the offset within a single .EXE or .DLL *file*. + // RVA - relative virtual address (offset within *loaded image*) + // FileOffsetToRVA and RVAToFileOffset convert between these representations. + + RVA FileOffsetToRVA(uint32 offset) const; + + + private: + + bool ReadDataDirectory(int index, ImageDataDirectory* dir); + bool incomplete_disassembly_; // 'true' if can leave out 'uninteresting' bits std::vector<RVA> abs32_locations_; std::vector<RVA> rel32_locations_; + // + // Fields that are always valid. + // + + // + // Information that is valid after successful ParseHeader. + // + bool is_PE32_plus_; // PE32_plus is for 64 bit executables. + + // Location and size of IMAGE_OPTIONAL_HEADER in the buffer. + const uint8 *optional_header_; + uint16 size_of_optional_header_; + uint16 offset_of_data_directories_; + + uint16 machine_type_; + uint16 number_of_sections_; + const Section *sections_; + bool has_text_section_; + + uint32 size_of_code_; + uint32 size_of_initialized_data_; + uint32 size_of_uninitialized_data_; + RVA base_of_code_; + RVA base_of_data_; + + uint64 image_base_; // range limited to 32 bits for 32 bit executable + uint32 size_of_image_; + int number_of_data_directories_; + + ImageDataDirectory export_table_; + ImageDataDirectory import_table_; + ImageDataDirectory resource_table_; + ImageDataDirectory exception_table_; + ImageDataDirectory base_relocation_table_; + ImageDataDirectory bound_import_table_; + ImageDataDirectory import_address_table_; + ImageDataDirectory delay_import_descriptor_; + ImageDataDirectory clr_runtime_header_; + #if COURGETTE_HISTOGRAM_TARGETS std::map<RVA, int> abs32_target_rvas_; std::map<RVA, int> rel32_target_rvas_; #endif + DISALLOW_COPY_AND_ASSIGN(DisassemblerWin32X86); }; |