[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 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_ENCODED_PROGRAM_H_ |
| 6 | #define COURGETTE_ENCODED_PROGRAM_H_ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/basictypes.h" |
[email protected] | 423a381 | 2011-10-26 00:50:20 | [diff] [blame^] | 11 | #include "courgette/disassembler.h" |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 12 | #include "courgette/memory_allocator.h" |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 13 | |
| 14 | namespace courgette { |
| 15 | |
| 16 | class SinkStream; |
| 17 | class SinkStreamSet; |
| 18 | class SourceStreamSet; |
| 19 | |
| 20 | // An EncodedProgram is a set of tables that contain a simple 'binary assembly |
| 21 | // language' that can be assembled to produce a sequence of bytes, for example, |
| 22 | // a Windows 32-bit executable. |
| 23 | // |
| 24 | class EncodedProgram { |
| 25 | public: |
| 26 | EncodedProgram(); |
| 27 | ~EncodedProgram(); |
| 28 | |
| 29 | // Generating an EncodedProgram: |
| 30 | // |
| 31 | // (1) The image base can be specified at any time. |
| 32 | void set_image_base(uint64 base) { image_base_ = base; } |
| 33 | |
| 34 | // (2) Address tables and indexes defined first. |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 35 | CheckBool DefineRel32Label(int index, RVA address) WARN_UNUSED_RESULT; |
| 36 | CheckBool DefineAbs32Label(int index, RVA address) WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 37 | void EndLabels(); |
| 38 | |
| 39 | // (3) Add instructions in the order needed to generate bytes of file. |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 40 | // NOTE: If any of these methods ever fail, the EncodedProgram instance |
| 41 | // has failed and should be discarded. |
| 42 | CheckBool AddOrigin(RVA rva) WARN_UNUSED_RESULT; |
| 43 | CheckBool AddCopy(uint32 count, const void* bytes) WARN_UNUSED_RESULT; |
| 44 | CheckBool AddRel32(int label_index) WARN_UNUSED_RESULT; |
| 45 | CheckBool AddAbs32(int label_index) WARN_UNUSED_RESULT; |
| 46 | CheckBool AddMakeRelocs() WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 47 | |
| 48 | // (3) Serialize binary assembly language tables to a set of streams. |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 49 | CheckBool WriteTo(SinkStreamSet* streams) WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 50 | |
| 51 | // Using an EncodedProgram to generate a byte stream: |
| 52 | // |
| 53 | // (4) Deserializes a fresh EncodedProgram from a set of streams. |
[email protected] | c8240b1 | 2011-03-22 20:19:49 | [diff] [blame] | 54 | bool ReadFrom(SourceStreamSet* streams); |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 55 | |
| 56 | // (5) Assembles the 'binary assembly language' into final file. |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 57 | CheckBool AssembleTo(SinkStream* buffer) WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 58 | |
| 59 | private: |
[email protected] | 54f1b82 | 2009-07-18 03:28:40 | [diff] [blame] | 60 | // Binary assembly language operations. |
| 61 | enum OP { |
| 62 | ORIGIN, // ORIGIN <rva> - set address for subsequent assembly. |
| 63 | COPY, // COPY <count> <bytes> - copy bytes to output. |
| 64 | COPY1, // COPY1 <byte> - same as COPY 1 <byte>. |
| 65 | REL32, // REL32 <index> - emit rel32 encoded reference to address at |
| 66 | // address table offset <index> |
| 67 | ABS32, // ABS32 <index> - emit abs32 encoded reference to address at |
| 68 | // address table offset <index> |
| 69 | MAKE_BASE_RELOCATION_TABLE, // Emit base relocation table blocks. |
| 70 | OP_LAST |
| 71 | }; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 72 | |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 73 | typedef NoThrowBuffer<RVA> RvaVector; |
| 74 | typedef NoThrowBuffer<uint32> UInt32Vector; |
| 75 | typedef NoThrowBuffer<uint8> UInt8Vector; |
| 76 | typedef NoThrowBuffer<OP> OPVector; |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 77 | |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 78 | void DebuggingSummary(); |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 79 | CheckBool GenerateBaseRelocations(SinkStream *buffer) WARN_UNUSED_RESULT; |
| 80 | CheckBool DefineLabelCommon(RvaVector*, int, RVA) WARN_UNUSED_RESULT; |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 81 | void FinishLabelsCommon(RvaVector* addresses); |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 82 | |
| 83 | // Binary assembly language tables. |
| 84 | uint64 image_base_; |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 85 | RvaVector rel32_rva_; |
| 86 | RvaVector abs32_rva_; |
| 87 | OPVector ops_; |
| 88 | RvaVector origins_; |
| 89 | UInt32Vector copy_counts_; |
| 90 | UInt8Vector copy_bytes_; |
| 91 | UInt32Vector rel32_ix_; |
| 92 | UInt32Vector abs32_ix_; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 93 | |
| 94 | // Table of the addresses containing abs32 relocations; computed during |
| 95 | // assembly, used to generate base relocation table. |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 96 | UInt32Vector abs32_relocs_; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 97 | |
| 98 | DISALLOW_COPY_AND_ASSIGN(EncodedProgram); |
| 99 | }; |
| 100 | |
| 101 | } // namespace courgette |
[email protected] | 54f1b82 | 2009-07-18 03:28:40 | [diff] [blame] | 102 | #endif // COURGETTE_ENCODED_PROGRAM_H_ |