| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COURGETTE_ASSEMBLY_PROGRAM_H_ | 5 #ifndef COURGETTE_ASSEMBLY_PROGRAM_H_ |
| 6 #define COURGETTE_ASSEMBLY_PROGRAM_H_ | 6 #define COURGETTE_ASSEMBLY_PROGRAM_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 13 | 14 |
| 14 #include "courgette/image_info.h" | 15 #include "courgette/image_info.h" |
| 15 #include "courgette/memory_allocator.h" | 16 #include "courgette/memory_allocator.h" |
| 16 | 17 |
| 17 namespace courgette { | 18 namespace courgette { |
| 18 | 19 |
| 19 class EncodedProgram; | 20 class EncodedProgram; |
| 20 class Instruction; | 21 class Instruction; |
| 21 | 22 |
| 22 typedef std::vector<Instruction*, MemoryAllocator<Instruction*> > | 23 typedef NoThrowBuffer<Instruction*> InstructionVector; |
| 23 InstructionVector; | |
| 24 | 24 |
| 25 // A Label is a symbolic reference to an address. Unlike a conventional | 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 | 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 | 27 // stored in a table and the Label will be replaced with the index into the |
| 28 // table. | 28 // table. |
| 29 // | 29 // |
| 30 // TODO(sra): Make fields private and add setters and getters. | 30 // TODO(sra): Make fields private and add setters and getters. |
| 31 class Label { | 31 class Label { |
| 32 public: | 32 public: |
| 33 static const int kNoIndex = -1; | 33 static const int kNoIndex = -1; |
| 34 Label() : rva_(0), index_(kNoIndex) {} | 34 Label() : rva_(0), index_(kNoIndex) {} |
| 35 explicit Label(RVA rva) : rva_(rva), index_(kNoIndex) {} | 35 explicit Label(RVA rva) : rva_(rva), index_(kNoIndex) {} |
| 36 | 36 |
| 37 RVA rva_; // Address refered to by the label. | 37 RVA rva_; // Address referred to by the label. |
| 38 int index_; // Index of address in address table, kNoIndex until assigned. | 38 int index_; // Index of address in address table, kNoIndex until assigned. |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 typedef std::map<RVA, Label*> RVAToLabel; | 41 typedef std::map<RVA, Label*> RVAToLabel; |
| 42 | 42 |
| 43 // An AssemblyProgram is the result of disassembling an executable file. | 43 // An AssemblyProgram is the result of disassembling an executable file. |
| 44 // | 44 // |
| 45 // * The disassembler creates labels in the AssemblyProgram and emits | 45 // * The disassembler creates labels in the AssemblyProgram and emits |
| 46 // 'Instructions'. | 46 // 'Instructions'. |
| 47 // * The disassembler then calls DefaultAssignIndexes to assign | 47 // * The disassembler then calls DefaultAssignIndexes to assign |
| (...skipping 14 matching lines...) Expand all Loading... |
| 62 class AssemblyProgram { | 62 class AssemblyProgram { |
| 63 public: | 63 public: |
| 64 AssemblyProgram(); | 64 AssemblyProgram(); |
| 65 ~AssemblyProgram(); | 65 ~AssemblyProgram(); |
| 66 | 66 |
| 67 void set_image_base(uint64 image_base) { image_base_ = image_base; } | 67 void set_image_base(uint64 image_base) { image_base_ = image_base; } |
| 68 | 68 |
| 69 // Instructions will be assembled in the order they are emitted. | 69 // Instructions will be assembled in the order they are emitted. |
| 70 | 70 |
| 71 // Generates an entire base relocation table. | 71 // Generates an entire base relocation table. |
| 72 void EmitMakeRelocsInstruction(); | 72 CheckBool EmitMakeRelocsInstruction() WARN_UNUSED_RESULT; |
| 73 | 73 |
| 74 // Following instruction will be assembled at address 'rva'. | 74 // Following instruction will be assembled at address 'rva'. |
| 75 void EmitOriginInstruction(RVA rva); | 75 CheckBool EmitOriginInstruction(RVA rva) WARN_UNUSED_RESULT; |
| 76 | 76 |
| 77 // Generates a single byte of data or machine instruction. | 77 // Generates a single byte of data or machine instruction. |
| 78 void EmitByteInstruction(uint8 byte); | 78 CheckBool EmitByteInstruction(uint8 byte) WARN_UNUSED_RESULT; |
| 79 | 79 |
| 80 // Generates 4-byte relative reference to address of 'label'. | 80 // Generates 4-byte relative reference to address of 'label'. |
| 81 void EmitRel32(Label* label); | 81 CheckBool EmitRel32(Label* label) WARN_UNUSED_RESULT; |
| 82 | 82 |
| 83 // Generates 4-byte absolute reference to address of 'label'. | 83 // Generates 4-byte absolute reference to address of 'label'. |
| 84 void EmitAbs32(Label* label); | 84 CheckBool EmitAbs32(Label* label) WARN_UNUSED_RESULT; |
| 85 | 85 |
| 86 // Looks up a label or creates a new one. Might return NULL. |
| 86 Label* FindOrMakeAbs32Label(RVA rva); | 87 Label* FindOrMakeAbs32Label(RVA rva); |
| 88 |
| 89 // Looks up a label or creates a new one. Might return NULL. |
| 87 Label* FindOrMakeRel32Label(RVA rva); | 90 Label* FindOrMakeRel32Label(RVA rva); |
| 88 | 91 |
| 89 void DefaultAssignIndexes(); | 92 void DefaultAssignIndexes(); |
| 90 void UnassignIndexes(); | 93 void UnassignIndexes(); |
| 91 void AssignRemainingIndexes(); | 94 void AssignRemainingIndexes(); |
| 92 | 95 |
| 93 EncodedProgram* Encode() const; | 96 EncodedProgram* Encode() const; |
| 94 | 97 |
| 95 // Accessor for instruction list. | 98 // Accessor for instruction list. |
| 96 const InstructionVector& instructions() const { | 99 const InstructionVector& instructions() const { |
| 97 return instructions_; | 100 return instructions_; |
| 98 } | 101 } |
| 99 | 102 |
| 100 // Returns the label if the instruction contains and absolute address, | 103 // Returns the label if the instruction contains and absolute address, |
| 101 // otherwise returns NULL. | 104 // otherwise returns NULL. |
| 102 Label* InstructionAbs32Label(const Instruction* instruction) const; | 105 Label* InstructionAbs32Label(const Instruction* instruction) const; |
| 103 | 106 |
| 104 // Returns the label if the instruction contains and rel32 offset, | 107 // Returns the label if the instruction contains and rel32 offset, |
| 105 // otherwise returns NULL. | 108 // otherwise returns NULL. |
| 106 Label* InstructionRel32Label(const Instruction* instruction) const; | 109 Label* InstructionRel32Label(const Instruction* instruction) const; |
| 107 | 110 |
| 108 private: | 111 private: |
| 109 void Emit(Instruction* instruction) { instructions_.push_back(instruction); } | 112 CheckBool Emit(Instruction* instruction) WARN_UNUSED_RESULT; |
| 110 | 113 |
| 114 // Looks up a label or creates a new one. Might return NULL. |
| 111 Label* FindLabel(RVA rva, RVAToLabel* labels); | 115 Label* FindLabel(RVA rva, RVAToLabel* labels); |
| 112 | 116 |
| 113 // Helper methods for the public versions. | 117 // Helper methods for the public versions. |
| 114 static void UnassignIndexes(RVAToLabel* labels); | 118 static void UnassignIndexes(RVAToLabel* labels); |
| 115 static void DefaultAssignIndexes(RVAToLabel* labels); | 119 static void DefaultAssignIndexes(RVAToLabel* labels); |
| 116 static void AssignRemainingIndexes(RVAToLabel* labels); | 120 static void AssignRemainingIndexes(RVAToLabel* labels); |
| 117 | 121 |
| 118 // Sharing instructions that emit a single byte saves a lot of space. | 122 // Sharing instructions that emit a single byte saves a lot of space. |
| 119 Instruction* GetByteInstruction(uint8 byte); | 123 Instruction* GetByteInstruction(uint8 byte); |
| 120 Instruction** byte_instruction_cache_; | 124 scoped_array<Instruction*> byte_instruction_cache_; |
| 121 | 125 |
| 122 uint64 image_base_; // Desired or mandated base address of image. | 126 uint64 image_base_; // Desired or mandated base address of image. |
| 123 | 127 |
| 124 InstructionVector instructions_; // All the instructions in program. | 128 InstructionVector instructions_; // All the instructions in program. |
| 125 | 129 |
| 126 // These are lookup maps to find the label associated with a given address. | 130 // These are lookup maps to find the label associated with a given address. |
| 127 // We have separate label spaces for addresses referenced by rel32 labels and | 131 // We have separate label spaces for addresses referenced by rel32 labels and |
| 128 // abs32 labels. This is somewhat arbitrary. | 132 // abs32 labels. This is somewhat arbitrary. |
| 129 RVAToLabel rel32_labels_; | 133 RVAToLabel rel32_labels_; |
| 130 RVAToLabel abs32_labels_; | 134 RVAToLabel abs32_labels_; |
| 131 | 135 |
| 132 DISALLOW_COPY_AND_ASSIGN(AssemblyProgram); | 136 DISALLOW_COPY_AND_ASSIGN(AssemblyProgram); |
| 133 }; | 137 }; |
| 134 | 138 |
| 135 } // namespace courgette | 139 } // namespace courgette |
| 136 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_ | 140 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_ |
| OLD | NEW |