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