OLD | NEW |
1 // Copyright (c) 2011 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 #include "courgette/encoded_program.h" | 5 #include "courgette/encoded_program.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 120 |
121 items->clear(); | 121 items->clear(); |
122 bool ok = items->resize(count, 0); | 122 bool ok = items->resize(count, 0); |
123 if (ok && count != 0) { | 123 if (ok && count != 0) { |
124 size_t byte_count = count * sizeof(typename V::value_type); | 124 size_t byte_count = count * sizeof(typename V::value_type); |
125 return buffer->Read(static_cast<void*>(&((*items)[0])), byte_count); | 125 return buffer->Read(static_cast<void*>(&((*items)[0])), byte_count); |
126 } | 126 } |
127 return ok; | 127 return ok; |
128 } | 128 } |
129 | 129 |
| 130 /******** InstructionStoreReceptor ********/ |
| 131 |
| 132 // An InstructionReceptor that stores emitted instructions. |
| 133 class InstructionStoreReceptor : public InstructionReceptor { |
| 134 public: |
| 135 explicit InstructionStoreReceptor(ExecutableType exe_type, |
| 136 EncodedProgram* encoded) |
| 137 : exe_type_(exe_type), encoded_(encoded) { |
| 138 CHECK(encoded_); |
| 139 } |
| 140 |
| 141 CheckBool EmitPeRelocs() override { |
| 142 return encoded_->AddPeMakeRelocs(exe_type_); |
| 143 } |
| 144 CheckBool EmitElfRelocation() override { |
| 145 return encoded_->AddElfMakeRelocs(); |
| 146 } |
| 147 CheckBool EmitElfARMRelocation() override { |
| 148 return encoded_->AddElfARMMakeRelocs(); |
| 149 } |
| 150 CheckBool EmitOrigin(RVA rva) override { return encoded_->AddOrigin(rva); } |
| 151 CheckBool EmitSingleByte(uint8_t byte) override { |
| 152 return encoded_->AddCopy(1, &byte); |
| 153 } |
| 154 CheckBool EmitMultipleBytes(const uint8_t* bytes, size_t len) override { |
| 155 return encoded_->AddCopy(len, bytes); |
| 156 } |
| 157 CheckBool EmitRel32(Label* label) override { |
| 158 return encoded_->AddRel32(label->index_); |
| 159 } |
| 160 CheckBool EmitRel32ARM(uint16_t op, |
| 161 Label* label, |
| 162 const uint8_t* arm_op, |
| 163 uint16_t op_size) override { |
| 164 return encoded_->AddRel32ARM(op, label->index_); |
| 165 } |
| 166 CheckBool EmitAbs32(Label* label) override { |
| 167 return encoded_->AddAbs32(label->index_); |
| 168 } |
| 169 CheckBool EmitAbs64(Label* label) override { |
| 170 return encoded_->AddAbs64(label->index_); |
| 171 } |
| 172 |
| 173 private: |
| 174 ExecutableType exe_type_; |
| 175 EncodedProgram* encoded_; |
| 176 |
| 177 DISALLOW_COPY_AND_ASSIGN(InstructionStoreReceptor); |
| 178 }; |
| 179 |
130 } // namespace | 180 } // namespace |
131 | 181 |
132 //////////////////////////////////////////////////////////////////////////////// | 182 //////////////////////////////////////////////////////////////////////////////// |
133 | 183 |
134 // Constructor is here rather than in the header. Although the constructor | 184 // Constructor is here rather than in the header. Although the constructor |
135 // appears to do nothing it is fact quite large because of the implicit calls to | 185 // appears to do nothing it is fact quite large because of the implicit calls to |
136 // field constructors. Ditto for the destructor. | 186 // field constructors. Ditto for the destructor. |
137 EncodedProgram::EncodedProgram() {} | 187 EncodedProgram::EncodedProgram() {} |
138 EncodedProgram::~EncodedProgram() {} | 188 EncodedProgram::~EncodedProgram() {} |
139 | 189 |
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 if (ix_copy_bytes != copy_bytes_.size()) | 716 if (ix_copy_bytes != copy_bytes_.size()) |
667 return false; | 717 return false; |
668 if (ix_abs32_ix != abs32_ix_.size()) | 718 if (ix_abs32_ix != abs32_ix_.size()) |
669 return false; | 719 return false; |
670 if (ix_rel32_ix != rel32_ix_.size()) | 720 if (ix_rel32_ix != rel32_ix_.size()) |
671 return false; | 721 return false; |
672 | 722 |
673 return true; | 723 return true; |
674 } | 724 } |
675 | 725 |
| 726 CheckBool EncodedProgram::GenerateInstructions( |
| 727 ExecutableType exe_type, |
| 728 const InstructionGenerator& gen) { |
| 729 InstructionStoreReceptor store_receptor(exe_type, this); |
| 730 return gen.Run(&store_receptor); |
| 731 } |
| 732 |
676 // RelocBlock has the layout of a block of relocations in the base relocation | 733 // RelocBlock has the layout of a block of relocations in the base relocation |
677 // table file format. | 734 // table file format. |
678 struct RelocBlockPOD { | 735 struct RelocBlockPOD { |
679 uint32_t page_rva; | 736 uint32_t page_rva; |
680 uint32_t block_size; | 737 uint32_t block_size; |
681 uint16_t relocs[4096]; // Allow up to one relocation per byte of a 4k page. | 738 uint16_t relocs[4096]; // Allow up to one relocation per byte of a 4k page. |
682 }; | 739 }; |
683 | 740 |
684 static_assert(offsetof(RelocBlockPOD, relocs) == 8, "reloc block header size"); | 741 static_assert(offsetof(RelocBlockPOD, relocs) == 8, "reloc block header size"); |
685 | 742 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 } | 862 } |
806 | 863 |
807 Status Assemble(EncodedProgram* encoded, SinkStream* buffer) { | 864 Status Assemble(EncodedProgram* encoded, SinkStream* buffer) { |
808 bool assembled = encoded->AssembleTo(buffer); | 865 bool assembled = encoded->AssembleTo(buffer); |
809 if (assembled) | 866 if (assembled) |
810 return C_OK; | 867 return C_OK; |
811 return C_ASSEMBLY_FAILED; | 868 return C_ASSEMBLY_FAILED; |
812 } | 869 } |
813 | 870 |
814 } // namespace courgette | 871 } // namespace courgette |
OLD | NEW |