| 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/assembly_program.h" | 5 #include "courgette/assembly_program.h" |
| 6 | 6 |
| 7 #include <memory.h> | 7 #include <memory.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <sstream> | 11 #include <sstream> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 | 16 |
| 17 #include "courgette/courgette.h" | 17 #include "courgette/courgette.h" |
| 18 #include "courgette/encoded_program.h" | 18 #include "courgette/encoded_program.h" |
| 19 | 19 |
| 20 namespace courgette { | 20 namespace courgette { |
| 21 | 21 |
| 22 // Opcodes of simple assembly language | 22 // Opcodes of simple assembly language |
| 23 enum OP { | 23 enum OP { |
| 24 ORIGIN, // ORIGIN <rva> - set current address for assembly. | 24 ORIGIN, // ORIGIN <rva> - set current address for assembly. |
| 25 MAKERELOCS, // Generates a base relocation table. | 25 MAKEPERELOCS, // Generates a base relocation table. |
| 26 MAKEELFRELOCS, // Generates a base relocation table. |
| 26 DEFBYTE, // DEFBYTE <value> - emit a byte literal. | 27 DEFBYTE, // DEFBYTE <value> - emit a byte literal. |
| 27 REL32, // REL32 <label> - emit a rel32 encoded reference to 'label'. | 28 REL32, // REL32 <label> - emit a rel32 encoded reference to 'label'. |
| 28 ABS32, // REL32 <label> - emit am abs32 encoded reference to 'label'. | 29 ABS32, // REL32 <label> - emit am abs32 encoded reference to 'label'. |
| 29 LAST_OP | 30 LAST_OP |
| 30 }; | 31 }; |
| 31 | 32 |
| 32 // Base class for instructions. Because we have so many instructions we want to | 33 // Base class for instructions. Because we have so many instructions we want to |
| 33 // keep them as small as possible. For this reason we avoid virtual functions. | 34 // keep them as small as possible. For this reason we avoid virtual functions. |
| 34 // | 35 // |
| 35 class Instruction { | 36 class Instruction { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 51 | 52 |
| 52 // Sets the current address for the emitting instructions. | 53 // Sets the current address for the emitting instructions. |
| 53 class OriginInstruction : public Instruction { | 54 class OriginInstruction : public Instruction { |
| 54 public: | 55 public: |
| 55 explicit OriginInstruction(RVA rva) : Instruction(ORIGIN, 0), rva_(rva) {} | 56 explicit OriginInstruction(RVA rva) : Instruction(ORIGIN, 0), rva_(rva) {} |
| 56 RVA origin_rva() const { return rva_; } | 57 RVA origin_rva() const { return rva_; } |
| 57 private: | 58 private: |
| 58 RVA rva_; | 59 RVA rva_; |
| 59 }; | 60 }; |
| 60 | 61 |
| 61 // Emits an entire base relocation table. | 62 // Emits an entire PE base relocation table. |
| 62 class MakeRelocsInstruction : public Instruction { | 63 class PeRelocsInstruction : public Instruction { |
| 63 public: | 64 public: |
| 64 MakeRelocsInstruction() : Instruction(MAKERELOCS) {} | 65 PeRelocsInstruction() : Instruction(MAKEPERELOCS) {} |
| 66 }; |
| 67 |
| 68 // Emits an ELF relocation table. |
| 69 class ElfRelocsInstruction : public Instruction { |
| 70 public: |
| 71 ElfRelocsInstruction() : Instruction(MAKEELFRELOCS) {} |
| 65 }; | 72 }; |
| 66 | 73 |
| 67 // Emits a single byte. | 74 // Emits a single byte. |
| 68 class ByteInstruction : public Instruction { | 75 class ByteInstruction : public Instruction { |
| 69 public: | 76 public: |
| 70 explicit ByteInstruction(uint8 value) : Instruction(DEFBYTE, value) {} | 77 explicit ByteInstruction(uint8 value) : Instruction(DEFBYTE, value) {} |
| 71 uint8 byte_value() const { return info_; } | 78 uint8 byte_value() const { return info_; } |
| 72 }; | 79 }; |
| 73 | 80 |
| 74 // A ABS32 to REL32 instruction emits a reference to a label's address. | 81 // A ABS32 to REL32 instruction emits a reference to a label's address. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 101 delete instruction; | 108 delete instruction; |
| 102 } | 109 } |
| 103 if (byte_instruction_cache_.get()) { | 110 if (byte_instruction_cache_.get()) { |
| 104 for (size_t i = 0; i < 256; ++i) | 111 for (size_t i = 0; i < 256; ++i) |
| 105 delete byte_instruction_cache_[i]; | 112 delete byte_instruction_cache_[i]; |
| 106 } | 113 } |
| 107 DeleteContainedLabels(rel32_labels_); | 114 DeleteContainedLabels(rel32_labels_); |
| 108 DeleteContainedLabels(abs32_labels_); | 115 DeleteContainedLabels(abs32_labels_); |
| 109 } | 116 } |
| 110 | 117 |
| 111 CheckBool AssemblyProgram::EmitMakeRelocsInstruction() { | 118 CheckBool AssemblyProgram::EmitPeRelocsInstruction() { |
| 112 return Emit(new(std::nothrow) MakeRelocsInstruction()); | 119 return Emit(new(std::nothrow) PeRelocsInstruction()); |
| 120 } |
| 121 |
| 122 CheckBool AssemblyProgram::EmitElfRelocationInstruction() { |
| 123 return Emit(new(std::nothrow) ElfRelocsInstruction()); |
| 113 } | 124 } |
| 114 | 125 |
| 115 CheckBool AssemblyProgram::EmitOriginInstruction(RVA rva) { | 126 CheckBool AssemblyProgram::EmitOriginInstruction(RVA rva) { |
| 116 return Emit(new(std::nothrow) OriginInstruction(rva)); | 127 return Emit(new(std::nothrow) OriginInstruction(rva)); |
| 117 } | 128 } |
| 118 | 129 |
| 119 CheckBool AssemblyProgram::EmitByteInstruction(uint8 byte) { | 130 CheckBool AssemblyProgram::EmitByteInstruction(uint8 byte) { |
| 120 return Emit(GetByteInstruction(byte)); | 131 return Emit(GetByteInstruction(byte)); |
| 121 } | 132 } |
| 122 | 133 |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 if (!encoded->AddRel32(label->index_)) | 361 if (!encoded->AddRel32(label->index_)) |
| 351 return NULL; | 362 return NULL; |
| 352 break; | 363 break; |
| 353 } | 364 } |
| 354 case ABS32: { | 365 case ABS32: { |
| 355 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); | 366 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); |
| 356 if (!encoded->AddAbs32(label->index_)) | 367 if (!encoded->AddAbs32(label->index_)) |
| 357 return NULL; | 368 return NULL; |
| 358 break; | 369 break; |
| 359 } | 370 } |
| 360 case MAKERELOCS: { | 371 case MAKEPERELOCS: { |
| 361 if (!encoded->AddMakeRelocs()) | 372 if (!encoded->AddPeMakeRelocs()) |
| 362 return NULL; | 373 return NULL; |
| 363 break; | 374 break; |
| 364 } | 375 } |
| 376 case MAKEELFRELOCS: { |
| 377 if (!encoded->AddElfMakeRelocs()) |
| 378 return NULL; |
| 379 break; |
| 380 } |
| 365 default: { | 381 default: { |
| 366 NOTREACHED() << "Unknown Insn OP kind"; | 382 NOTREACHED() << "Unknown Insn OP kind"; |
| 367 } | 383 } |
| 368 } | 384 } |
| 369 } | 385 } |
| 370 | 386 |
| 371 return encoded.release(); | 387 return encoded.release(); |
| 372 } | 388 } |
| 373 | 389 |
| 374 Instruction* AssemblyProgram::GetByteInstruction(uint8 byte) { | 390 Instruction* AssemblyProgram::GetByteInstruction(uint8 byte) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 399 EncodedProgram *encoded = program->Encode(); | 415 EncodedProgram *encoded = program->Encode(); |
| 400 if (encoded) { | 416 if (encoded) { |
| 401 *output = encoded; | 417 *output = encoded; |
| 402 return C_OK; | 418 return C_OK; |
| 403 } else { | 419 } else { |
| 404 return C_GENERAL_ERROR; | 420 return C_GENERAL_ERROR; |
| 405 } | 421 } |
| 406 } | 422 } |
| 407 | 423 |
| 408 } // namespace courgette | 424 } // namespace courgette |
| OLD | NEW |