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