| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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. | |
| 4 | |
| 5 #ifndef COURGETTE_ASSEMBLY_PROGRAM_H_ | |
| 6 #define COURGETTE_ASSEMBLY_PROGRAM_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 | |
| 14 #include "third_party/courgette/image_info.h" | |
| 15 | |
| 16 namespace courgette { | |
| 17 | |
| 18 class EncodedProgram; | |
| 19 class Instruction; | |
| 20 | |
| 21 // A Label is a symbolic reference to an address. Unlike a conventional | |
| 22 // assembly language, we always know the address. The address will later be | |
| 23 // stored in a table and the Label will be replaced with the index into the | |
| 24 // table. | |
| 25 // | |
| 26 // TODO(sra): Make fields private and add setters and getters. | |
| 27 class Label { | |
| 28 public: | |
| 29 static const int kNoIndex = -1; | |
| 30 Label() : rva_(0), index_(kNoIndex) {} | |
| 31 explicit Label(RVA rva) : rva_(rva), index_(kNoIndex) {} | |
| 32 | |
| 33 RVA rva_; // Address refered to by the label. | |
| 34 int index_; // Index of address in address table, kNoIndex until assigned. | |
| 35 }; | |
| 36 | |
| 37 typedef std::map<RVA, Label*> RVAToLabel; | |
| 38 | |
| 39 // An AssemblyProgram is the result of disassembling an executable file. | |
| 40 // | |
| 41 // * The disassembler creates labels in the AssemblyProgram and emits | |
| 42 // 'Instructions'. | |
| 43 // * The disassembler then calls DefaultAssignIndexes to assign | |
| 44 // addresses to positions in the address tables. | |
| 45 // * [Optional step] | |
| 46 // * At this point the AssemblyProgram can be converted into an | |
| 47 // EncodedProgram and serialized to an output stream. | |
| 48 // * Later, the EncodedProgram can be deserialized and assembled into | |
| 49 // the original file. | |
| 50 // | |
| 51 // The optional step is to modify the AssemblyProgram. One form of modification | |
| 52 // is to assign indexes in such a way as to make the EncodedProgram for this | |
| 53 // AssemblyProgram look more like the EncodedProgram for some other | |
| 54 // AssemblyProgram. The modification process should call UnassignIndexes, do | |
| 55 // its own assignment, and then call AssignRemainingIndexes to ensure all | |
| 56 // indexes are assigned. | |
| 57 // | |
| 58 class AssemblyProgram { | |
| 59 public: | |
| 60 AssemblyProgram(); | |
| 61 ~AssemblyProgram(); | |
| 62 | |
| 63 void set_image_base(uint64 image_base) { image_base_ = image_base; } | |
| 64 | |
| 65 // Instructions will be assembled in the order they are emitted. | |
| 66 | |
| 67 // Generates an entire base relocation table. | |
| 68 void EmitMakeRelocsInstruction(); | |
| 69 | |
| 70 // Following instruction will be assembled at address 'rva'. | |
| 71 void EmitOriginInstruction(RVA rva); | |
| 72 | |
| 73 // Generates a single byte of data or machine instruction. | |
| 74 void EmitByteInstruction(uint8 byte); | |
| 75 | |
| 76 // Generates 4-byte relative reference to address of 'label'. | |
| 77 void EmitRel32(Label* label); | |
| 78 | |
| 79 // Generates 4-byte absolute reference to address of 'label'. | |
| 80 void EmitAbs32(Label* label); | |
| 81 | |
| 82 Label* FindOrMakeAbs32Label(RVA rva); | |
| 83 Label* FindOrMakeRel32Label(RVA rva); | |
| 84 | |
| 85 void DefaultAssignIndexes(); | |
| 86 void UnassignIndexes(); | |
| 87 void AssignRemainingIndexes(); | |
| 88 | |
| 89 EncodedProgram* Encode() const; | |
| 90 | |
| 91 // Accessor for instruction list. | |
| 92 const std::vector<Instruction*>& instructions() const { | |
| 93 return instructions_; | |
| 94 } | |
| 95 | |
| 96 // Returns the label if the instruction contains and absolute address, | |
| 97 // otherwise returns NULL. | |
| 98 Label* InstructionAbs32Label(const Instruction* instruction) const; | |
| 99 | |
| 100 // Returns the label if the instruction contains and rel32 offset, | |
| 101 // otherwise returns NULL. | |
| 102 Label* InstructionRel32Label(const Instruction* instruction) const; | |
| 103 | |
| 104 | |
| 105 private: | |
| 106 void Emit(Instruction* instruction) { instructions_.push_back(instruction); } | |
| 107 | |
| 108 Label* FindLabel(RVA rva, RVAToLabel* labels); | |
| 109 | |
| 110 // Helper methods for the public versions. | |
| 111 static void UnassignIndexes(RVAToLabel* labels); | |
| 112 static void DefaultAssignIndexes(RVAToLabel* labels); | |
| 113 static void AssignRemainingIndexes(RVAToLabel* labels); | |
| 114 | |
| 115 // Sharing instructions that emit a single byte saves a lot of space. | |
| 116 Instruction* GetByteInstruction(uint8 byte); | |
| 117 Instruction** byte_instruction_cache_; | |
| 118 | |
| 119 uint64 image_base_; // Desired or mandated base address of image. | |
| 120 | |
| 121 std::vector<Instruction*> instructions_; // All the instructions in program. | |
| 122 | |
| 123 // These are lookup maps to find the label associated with a given address. | |
| 124 // We have separate label spaces for addresses referenced by rel32 labels and | |
| 125 // abs32 labels. This is somewhat arbitrary. | |
| 126 RVAToLabel rel32_labels_; | |
| 127 RVAToLabel abs32_labels_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(AssemblyProgram); | |
| 130 }; | |
| 131 | |
| 132 } // namespace courgette | |
| 133 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_ | |
| 134 | |
| OLD | NEW |