OLD | NEW |
| (Empty) |
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 | |
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" | |
11 #include "third_party/courgette/image_info.h" | |
12 | |
13 namespace courgette { | |
14 | |
15 class SinkStream; | |
16 class SinkStreamSet; | |
17 class SourceStreamSet; | |
18 | |
19 // 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 // a Windows 32-bit executable. | |
22 // | |
23 class EncodedProgram { | |
24 public: | |
25 EncodedProgram(); | |
26 ~EncodedProgram(); | |
27 | |
28 // Generating an EncodedProgram: | |
29 // | |
30 // (1) The image base can be specified at any time. | |
31 void set_image_base(uint64 base) { image_base_ = base; } | |
32 | |
33 // (2) Address tables and indexes defined first. | |
34 void DefineRel32Label(int index, RVA address); | |
35 void DefineAbs32Label(int index, RVA address); | |
36 void EndLabels(); | |
37 | |
38 // (3) Add instructions in the order needed to generate bytes of file. | |
39 void AddOrigin(RVA rva); | |
40 void AddCopy(int count, const void* bytes); | |
41 void AddRel32(int label_index); | |
42 void AddAbs32(int label_index); | |
43 void AddMakeRelocs(); | |
44 | |
45 // (3) Serialize binary assembly language tables to a set of streams. | |
46 void WriteTo(SinkStreamSet *streams); | |
47 | |
48 // Using an EncodedProgram to generate a byte stream: | |
49 // | |
50 // (4) Deserializes a fresh EncodedProgram from a set of streams. | |
51 bool ReadFrom(SourceStreamSet *streams); | |
52 | |
53 // (5) Assembles the 'binary assembly language' into final file. | |
54 bool AssembleTo(SinkStream *buffer); | |
55 | |
56 private: | |
57 enum OP; // Binary assembly language operations. | |
58 | |
59 void DebuggingSummary(); | |
60 void GenerateBaseRelocations(SinkStream *buffer); | |
61 void DefineLabelCommon(std::vector<RVA>*, int, RVA); | |
62 void FinishLabelsCommon(std::vector<RVA>* addresses); | |
63 | |
64 // Binary assembly language tables. | |
65 uint64 image_base_; | |
66 std::vector<RVA> rel32_rva_; | |
67 std::vector<RVA> abs32_rva_; | |
68 std::vector<OP> ops_; | |
69 std::vector<RVA> origins_; | |
70 std::vector<int> copy_counts_; | |
71 std::vector<uint8> copy_bytes_; | |
72 std::vector<uint32> rel32_ix_; | |
73 std::vector<uint32> abs32_ix_; | |
74 | |
75 // Table of the addresses containing abs32 relocations; computed during | |
76 // assembly, used to generate base relocation table. | |
77 std::vector<uint32> abs32_relocs_; | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(EncodedProgram); | |
80 }; | |
81 | |
82 } // namespace courgette | |
83 #endif // COURGETTE_ENCODED_FORMAT_H_ | |
84 | |
OLD | NEW |