blob: b1203535f25b1296cb45ab8679d78ee51c3cb782 [file] [log] [blame]
[email protected]43a9e242011-04-06 17:42:451// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]f76b3b02009-05-06 04:02:102// 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]423a3812011-10-26 00:50:2011#include "courgette/disassembler.h"
[email protected]fbd31eb2011-03-01 00:19:0212#include "courgette/memory_allocator.h"
[email protected]f76b3b02009-05-06 04:02:1013
14namespace courgette {
15
16class SinkStream;
17class SinkStreamSet;
18class 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//
24class 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]43a9e242011-04-06 17:42:4535 CheckBool DefineRel32Label(int index, RVA address) WARN_UNUSED_RESULT;
36 CheckBool DefineAbs32Label(int index, RVA address) WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:1037 void EndLabels();
38
39 // (3) Add instructions in the order needed to generate bytes of file.
[email protected]43a9e242011-04-06 17:42:4540 // 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]f76b3b02009-05-06 04:02:1047
48 // (3) Serialize binary assembly language tables to a set of streams.
[email protected]43a9e242011-04-06 17:42:4549 CheckBool WriteTo(SinkStreamSet* streams) WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:1050
51 // Using an EncodedProgram to generate a byte stream:
52 //
53 // (4) Deserializes a fresh EncodedProgram from a set of streams.
[email protected]c8240b12011-03-22 20:19:4954 bool ReadFrom(SourceStreamSet* streams);
[email protected]f76b3b02009-05-06 04:02:1055
56 // (5) Assembles the 'binary assembly language' into final file.
[email protected]43a9e242011-04-06 17:42:4557 CheckBool AssembleTo(SinkStream* buffer) WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:1058
59 private:
[email protected]54f1b822009-07-18 03:28:4060 // 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]f76b3b02009-05-06 04:02:1072
[email protected]43a9e242011-04-06 17:42:4573 typedef NoThrowBuffer<RVA> RvaVector;
74 typedef NoThrowBuffer<uint32> UInt32Vector;
75 typedef NoThrowBuffer<uint8> UInt8Vector;
76 typedef NoThrowBuffer<OP> OPVector;
[email protected]fbd31eb2011-03-01 00:19:0277
[email protected]f76b3b02009-05-06 04:02:1078 void DebuggingSummary();
[email protected]43a9e242011-04-06 17:42:4579 CheckBool GenerateBaseRelocations(SinkStream *buffer) WARN_UNUSED_RESULT;
80 CheckBool DefineLabelCommon(RvaVector*, int, RVA) WARN_UNUSED_RESULT;
[email protected]fbd31eb2011-03-01 00:19:0281 void FinishLabelsCommon(RvaVector* addresses);
[email protected]f76b3b02009-05-06 04:02:1082
83 // Binary assembly language tables.
84 uint64 image_base_;
[email protected]fbd31eb2011-03-01 00:19:0285 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]f76b3b02009-05-06 04:02:1093
94 // Table of the addresses containing abs32 relocations; computed during
95 // assembly, used to generate base relocation table.
[email protected]fbd31eb2011-03-01 00:19:0296 UInt32Vector abs32_relocs_;
[email protected]f76b3b02009-05-06 04:02:1097
98 DISALLOW_COPY_AND_ASSIGN(EncodedProgram);
99};
100
101} // namespace courgette
[email protected]54f1b822009-07-18 03:28:40102#endif // COURGETTE_ENCODED_PROGRAM_H_