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
3 // found in the LICENSE file.
5 #ifndef COURGETTE_ENCODED_PROGRAM_H_
6 #define COURGETTE_ENCODED_PROGRAM_H_
10 #include "base/basictypes.h"
11 #include "courgette/disassembler.h"
12 #include "courgette/memory_allocator.h"
18 class SourceStreamSet
;
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.
24 class EncodedProgram
{
29 // Generating an EncodedProgram:
31 // (1) The image base can be specified at any time.
32 void set_image_base(uint64 base
) { image_base_
= base
; }
34 // (2) Address tables and indexes defined first.
35 CheckBool
DefineRel32Label(int index
, RVA address
) WARN_UNUSED_RESULT
;
36 CheckBool
DefineAbs32Label(int index
, RVA address
) WARN_UNUSED_RESULT
;
39 // (3) Add instructions in the order needed to generate bytes of file.
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
AddPeMakeRelocs() WARN_UNUSED_RESULT
;
47 CheckBool
AddElfMakeRelocs() WARN_UNUSED_RESULT
;
49 // (3) Serialize binary assembly language tables to a set of streams.
50 CheckBool
WriteTo(SinkStreamSet
* streams
) WARN_UNUSED_RESULT
;
52 // Using an EncodedProgram to generate a byte stream:
54 // (4) Deserializes a fresh EncodedProgram from a set of streams.
55 bool ReadFrom(SourceStreamSet
* streams
);
57 // (5) Assembles the 'binary assembly language' into final file.
58 CheckBool
AssembleTo(SinkStream
* buffer
) WARN_UNUSED_RESULT
;
61 // Binary assembly language operations.
62 // These are part of the patch format. Reusing an existing value will
63 // break backwards compatibility.
65 ORIGIN
= 0, // ORIGIN <rva> - set address for subsequent assembly.
66 COPY
= 1, // COPY <count> <bytes> - copy bytes to output.
67 COPY1
= 2, // COPY1 <byte> - same as COPY 1 <byte>.
68 REL32
= 3, // REL32 <index> - emit rel32 encoded reference to address at
69 // address table offset <index>
70 ABS32
= 4, // ABS32 <index> - emit abs32 encoded reference to address at
71 // address table offset <index>
72 MAKE_PE_RELOCATION_TABLE
= 5, // Emit PE base relocation table blocks.
73 MAKE_ELF_RELOCATION_TABLE
= 6, // Emit Elf relocation table.
76 typedef NoThrowBuffer
<RVA
> RvaVector
;
77 typedef NoThrowBuffer
<uint32
> UInt32Vector
;
78 typedef NoThrowBuffer
<uint8
> UInt8Vector
;
79 typedef NoThrowBuffer
<OP
> OPVector
;
81 void DebuggingSummary();
82 CheckBool
GeneratePeRelocations(SinkStream
*buffer
) WARN_UNUSED_RESULT
;
83 CheckBool
GenerateElfRelocations(SinkStream
*buffer
) WARN_UNUSED_RESULT
;
84 CheckBool
DefineLabelCommon(RvaVector
*, int, RVA
) WARN_UNUSED_RESULT
;
85 void FinishLabelsCommon(RvaVector
* addresses
);
87 // Binary assembly language tables.
93 UInt32Vector copy_counts_
;
94 UInt8Vector copy_bytes_
;
95 UInt32Vector rel32_ix_
;
96 UInt32Vector abs32_ix_
;
98 // Table of the addresses containing abs32 relocations; computed during
99 // assembly, used to generate base relocation table.
100 UInt32Vector abs32_relocs_
;
102 DISALLOW_COPY_AND_ASSIGN(EncodedProgram
);
105 } // namespace courgette
106 #endif // COURGETTE_ENCODED_PROGRAM_H_