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
3 // found in the LICENSE file.
5 #ifndef COURGETTE_DISASSEMBLER_H_
6 #define COURGETTE_DISASSEMBLER_H_
8 #include "base/basictypes.h"
10 #include "courgette/courgette.h"
14 class AssemblyProgram
;
16 // A Relative Virtual Address is the address in the image file after it is
17 // loaded into memory relative to the image load address.
22 virtual ~Disassembler();
24 virtual ExecutableType
kind() { return EXE_UNKNOWN
; }
26 // ok() may always be called but returns 'true' only after ParseHeader
28 bool ok() const { return failure_reason_
== NULL
; }
30 // Returns 'true' if the buffer appears to be a valid executable of the
31 // expected type. It is not required that this be called before Disassemble.
32 virtual bool ParseHeader() = 0;
34 // Disassembles the item passed to the factory method into the output
35 // parameter 'program'.
36 virtual bool Disassemble(AssemblyProgram
* program
) = 0;
38 // Returns the length of the source executable. May reduce after ParseHeader.
39 size_t length() const { return length_
; }
40 const uint8
* start() const { return start_
; }
41 const uint8
* end() const { return end_
; }
43 // Returns a pointer into the memory copy of the file format.
44 // FileOffsetToPointer(0) returns a pointer to the start of the file format.
45 const uint8
* OffsetToPointer(size_t offset
) const;
48 Disassembler(const void* start
, size_t length
);
51 bool Bad(const char *reason
);
53 // These helper functions avoid the need for casts in the main code.
54 uint16
ReadU16(const uint8
* address
, size_t offset
) {
55 return *reinterpret_cast<const uint16
*>(address
+ offset
);
58 uint32
ReadU32(const uint8
* address
, size_t offset
) {
59 return *reinterpret_cast<const uint32
*>(address
+ offset
);
62 uint64
ReadU64(const uint8
* address
, size_t offset
) {
63 return *reinterpret_cast<const uint64
*>(address
+ offset
);
66 static uint32
Read32LittleEndian(const void* address
) {
67 return *reinterpret_cast<const uint32
*>(address
);
70 // Reduce the length of the image in memory. Does not actually free
71 // (or realloc) any memory. Usually only called via ParseHeader()
72 void ReduceLength(size_t reduced_length
);
75 const char* failure_reason_
;
78 // Basic information that is always valid after Construction, though
79 // ParseHeader may shorten the length if the executable is shorter than
82 size_t length_
; // In current memory.
83 const uint8
* start_
; // In current memory, base for 'file offsets'.
84 const uint8
* end_
; // In current memory.
86 DISALLOW_COPY_AND_ASSIGN(Disassembler
);
89 } // namespace courgette
90 #endif // COURGETTE_DISASSEMBLER_H_