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.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.h')
-rw-r--r-- | courgette/disassembler.h | 65 |
1 files changed, 62 insertions, 3 deletions
diff --git a/courgette/disassembler.h b/courgette/disassembler.h index bef1a90..2b4714d 100644 --- a/courgette/disassembler.h +++ b/courgette/disassembler.h @@ -7,23 +7,82 @@ #include "base/basictypes.h" +#include "courgette/courgette.h" + namespace courgette { class AssemblyProgram; -class PEInfo; + +// A Relative Virtual Address is the address in the image file after it is +// loaded into memory relative to the image load address. +typedef uint32 RVA; class Disassembler { public: - virtual ~Disassembler() {} + virtual ~Disassembler(); + + virtual ExecutableType kind() { return UNKNOWN; } + + // ok() may always be called but returns 'true' only after ParseHeader + // succeeds. + bool ok() const { return failure_reason_ == NULL; } + + // Returns 'true' if the buffer appears to be a valid executable of the + // expected type. It is not required that this be called before Disassemble. + virtual bool ParseHeader() = 0; // Disassembles the item passed to the factory method into the output // parameter 'program'. virtual bool Disassemble(AssemblyProgram* program) = 0; + // Returns the length of the source executable. May reduce after ParseHeader. + size_t length() const { return length_; } + const uint8* start() const { return start_; } + const uint8* end() const { return end_; } + + // Returns a pointer into the memory copy of the file format. + // FileOffsetToPointer(0) returns a pointer to the start of the file format. + const uint8* OffsetToPointer(size_t offset) const; + protected: - Disassembler() {} + Disassembler(const void* start, size_t length); + + bool Good(); + bool Bad(const char *reason); + + // These helper functions avoid the need for casts in the main code. + uint16 ReadU16(const uint8* address, size_t offset) { + return *reinterpret_cast<const uint16*>(address + offset); + } + + uint32 ReadU32(const uint8* address, size_t offset) { + return *reinterpret_cast<const uint32*>(address + offset); + } + + uint64 ReadU64(const uint8* address, size_t offset) { + return *reinterpret_cast<const uint64*>(address + offset); + } + + static uint32 Read32LittleEndian(const void* address) { + return *reinterpret_cast<const uint32*>(address); + } + + // Reduce the length of the image in memory. Does not actually free + // (or realloc) any memory. Unusally only called via ParseHeader() + void ReduceLength(size_t reduced_length); private: + const char* failure_reason_; + + // + // Basic information that is always valid after Construction, though + // ParseHeader may shorten the length if the executable is shorter than + // the total data. + // + size_t length_; // In current memory. + const uint8* start_; // In current memory, base for 'file offsets'. + const uint8* end_; // In current memory. + DISALLOW_COPY_AND_ASSIGN(Disassembler); }; |