Port Android relocation packer to chromium build
[chromium-blink-merge.git] / courgette / disassembler.h
blob0154ec4bab1d5a6f9652c9989e5d5ab407458d48
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"
12 // COURGETTE_HISTOGRAM_TARGETS prints out a histogram of how frequently
13 // different target addresses are referenced. Purely for debugging.
14 #define COURGETTE_HISTOGRAM_TARGETS 0
16 namespace courgette {
18 class AssemblyProgram;
20 // A Relative Virtual Address is the address in the image file after it is
21 // loaded into memory relative to the image load address.
22 typedef uint32 RVA;
24 class Disassembler {
25 public:
26 virtual ~Disassembler();
28 virtual ExecutableType kind() { return EXE_UNKNOWN; }
30 // ok() may always be called but returns 'true' only after ParseHeader
31 // succeeds.
32 bool ok() const { return failure_reason_ == NULL; }
34 // Returns 'true' if the buffer appears to be a valid executable of the
35 // expected type. It is not required that this be called before Disassemble.
36 virtual bool ParseHeader() = 0;
38 // Disassembles the item passed to the factory method into the output
39 // parameter 'program'.
40 virtual bool Disassemble(AssemblyProgram* program) = 0;
42 // Returns the length of the source executable. May reduce after ParseHeader.
43 size_t length() const { return length_; }
44 const uint8* start() const { return start_; }
45 const uint8* end() const { return end_; }
47 // Returns a pointer into the memory copy of the file format.
48 // FileOffsetToPointer(0) returns a pointer to the start of the file format.
49 const uint8* OffsetToPointer(size_t offset) const;
51 protected:
52 Disassembler(const void* start, size_t length);
54 bool Good();
55 bool Bad(const char *reason);
57 // Returns true if the array lies within our memory region.
58 bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) {
59 return offset <= length() && elements <= (length() - offset) / element_size;
62 // These helper functions avoid the need for casts in the main code.
63 uint16 ReadU16(const uint8* address, size_t offset) {
64 return *reinterpret_cast<const uint16*>(address + offset);
67 uint32 ReadU32(const uint8* address, size_t offset) {
68 return *reinterpret_cast<const uint32*>(address + offset);
71 uint64 ReadU64(const uint8* address, size_t offset) {
72 return *reinterpret_cast<const uint64*>(address + offset);
75 static uint32 Read32LittleEndian(const void* address) {
76 return *reinterpret_cast<const uint32*>(address);
79 static uint64 Read64LittleEndian(const void* address) {
80 return *reinterpret_cast<const uint64*>(address);
83 static uint16 Read16LittleEndian(const void* address) {
84 return *reinterpret_cast<const uint16*>(address);
87 // Reduce the length of the image in memory. Does not actually free
88 // (or realloc) any memory. Usually only called via ParseHeader()
89 void ReduceLength(size_t reduced_length);
91 private:
92 const char* failure_reason_;
95 // Basic information that is always valid after Construction, though
96 // ParseHeader may shorten the length if the executable is shorter than
97 // the total data.
99 size_t length_; // In current memory.
100 const uint8* start_; // In current memory, base for 'file offsets'.
101 const uint8* end_; // In current memory.
103 DISALLOW_COPY_AND_ASSIGN(Disassembler);
106 } // namespace courgette
107 #endif // COURGETTE_DISASSEMBLER_H_