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_ASSEMBLY_PROGRAM_H_
6 #define COURGETTE_ASSEMBLY_PROGRAM_H_
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
15 #include "courgette/image_info.h"
16 #include "courgette/memory_allocator.h"
23 typedef NoThrowBuffer
<Instruction
*> InstructionVector
;
25 // A Label is a symbolic reference to an address. Unlike a conventional
26 // assembly language, we always know the address. The address will later be
27 // stored in a table and the Label will be replaced with the index into the
30 // TODO(sra): Make fields private and add setters and getters.
33 static const int kNoIndex
= -1;
34 Label() : rva_(0), index_(kNoIndex
) {}
35 explicit Label(RVA rva
) : rva_(rva
), index_(kNoIndex
) {}
37 RVA rva_
; // Address referred to by the label.
38 int index_
; // Index of address in address table, kNoIndex until assigned.
41 typedef std::map
<RVA
, Label
*> RVAToLabel
;
43 // An AssemblyProgram is the result of disassembling an executable file.
45 // * The disassembler creates labels in the AssemblyProgram and emits
47 // * The disassembler then calls DefaultAssignIndexes to assign
48 // addresses to positions in the address tables.
50 // * At this point the AssemblyProgram can be converted into an
51 // EncodedProgram and serialized to an output stream.
52 // * Later, the EncodedProgram can be deserialized and assembled into
55 // The optional step is to modify the AssemblyProgram. One form of modification
56 // is to assign indexes in such a way as to make the EncodedProgram for this
57 // AssemblyProgram look more like the EncodedProgram for some other
58 // AssemblyProgram. The modification process should call UnassignIndexes, do
59 // its own assignment, and then call AssignRemainingIndexes to ensure all
60 // indexes are assigned.
62 class AssemblyProgram
{
67 void set_image_base(uint64 image_base
) { image_base_
= image_base
; }
69 // Instructions will be assembled in the order they are emitted.
71 // Generates an entire base relocation table.
72 CheckBool
EmitMakeRelocsInstruction() WARN_UNUSED_RESULT
;
74 // Following instruction will be assembled at address 'rva'.
75 CheckBool
EmitOriginInstruction(RVA rva
) WARN_UNUSED_RESULT
;
77 // Generates a single byte of data or machine instruction.
78 CheckBool
EmitByteInstruction(uint8 byte
) WARN_UNUSED_RESULT
;
80 // Generates 4-byte relative reference to address of 'label'.
81 CheckBool
EmitRel32(Label
* label
) WARN_UNUSED_RESULT
;
83 // Generates 4-byte absolute reference to address of 'label'.
84 CheckBool
EmitAbs32(Label
* label
) WARN_UNUSED_RESULT
;
86 // Looks up a label or creates a new one. Might return NULL.
87 Label
* FindOrMakeAbs32Label(RVA rva
);
89 // Looks up a label or creates a new one. Might return NULL.
90 Label
* FindOrMakeRel32Label(RVA rva
);
92 void DefaultAssignIndexes();
93 void UnassignIndexes();
94 void AssignRemainingIndexes();
96 EncodedProgram
* Encode() const;
98 // Accessor for instruction list.
99 const InstructionVector
& instructions() const {
100 return instructions_
;
103 // Returns the label if the instruction contains and absolute address,
104 // otherwise returns NULL.
105 Label
* InstructionAbs32Label(const Instruction
* instruction
) const;
107 // Returns the label if the instruction contains and rel32 offset,
108 // otherwise returns NULL.
109 Label
* InstructionRel32Label(const Instruction
* instruction
) const;
112 CheckBool
Emit(Instruction
* instruction
) WARN_UNUSED_RESULT
;
114 // Looks up a label or creates a new one. Might return NULL.
115 Label
* FindLabel(RVA rva
, RVAToLabel
* labels
);
117 // Helper methods for the public versions.
118 static void UnassignIndexes(RVAToLabel
* labels
);
119 static void DefaultAssignIndexes(RVAToLabel
* labels
);
120 static void AssignRemainingIndexes(RVAToLabel
* labels
);
122 // Sharing instructions that emit a single byte saves a lot of space.
123 Instruction
* GetByteInstruction(uint8 byte
);
124 scoped_array
<Instruction
*> byte_instruction_cache_
;
126 uint64 image_base_
; // Desired or mandated base address of image.
128 InstructionVector instructions_
; // All the instructions in program.
130 // These are lookup maps to find the label associated with a given address.
131 // We have separate label spaces for addresses referenced by rel32 labels and
132 // abs32 labels. This is somewhat arbitrary.
133 RVAToLabel rel32_labels_
;
134 RVAToLabel abs32_labels_
;
136 DISALLOW_COPY_AND_ASSIGN(AssemblyProgram
);
139 } // namespace courgette
140 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_