#ifndef COURGETTE_DISASSEMBLER_WIN32_X86_H_
#define COURGETTE_DISASSEMBLER_WIN32_X86_H_
#include "base/basictypes.h"
#include "courgette/disassembler.h"
#include "courgette/memory_allocator.h"
#include "courgette/types_win_pe.h"
#ifdef COURGETTE_HISTOGRAM_TARGETS
#include <map>
#endif
namespace courgette {
class AssemblyProgram;
class DisassemblerWin32X86 : public Disassembler {
public:
explicit DisassemblerWin32X86(const void* start, size_t length);
virtual ExecutableType kind() { return EXE_WIN_32_X86; }
virtual bool ParseHeader();
virtual bool Disassemble(AssemblyProgram* target);
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_; }
bool ParseRelocs(std::vector<RVA> *addresses);
const Section* RVAToSection(RVA rva) const;
static const int kNoOffset = -1;
int RVAToFileOffset(RVA rva) const;
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();
void ParseRel32RelocsFromSection(const Section* section);
CheckBool ParseNonSectionFileRegion(uint32 start_file_offset,
uint32 end_file_offset, AssemblyProgram* program) WARN_UNUSED_RESULT;
CheckBool ParseFileRegion(const Section* section,
uint32 start_file_offset, uint32 end_file_offset,
AssemblyProgram* program) WARN_UNUSED_RESULT;
#if COURGETTE_HISTOGRAM_TARGETS
void HistogramTargets(const char* kind, const std::map<RVA, int>& map);
#endif
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_; }
std::string DescribeRVA(RVA rva) const;
const Section* FindNextSection(uint32 file_offset) const;
RVA FileOffsetToRVA(uint32 offset) const;
private:
bool ReadDataDirectory(int index, ImageDataDirectory* dir);
bool incomplete_disassembly_;
std::vector<RVA> abs32_locations_;
std::vector<RVA> rel32_locations_;
bool is_PE32_plus_;
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_;
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);
};
}
#endif