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"
13 #include "courgette/types_elf.h"
18 const int kStreamMisc
= 0;
19 const int kStreamOps
= 1;
20 const int kStreamBytes
= 2;
21 const int kStreamAbs32Indexes
= 3;
22 const int kStreamRel32Indexes
= 4;
23 const int kStreamAbs32Addresses
= 5;
24 const int kStreamRel32Addresses
= 6;
25 const int kStreamCopyCounts
= 7;
26 const int kStreamOriginAddresses
= kStreamMisc
;
28 const int kStreamLimit
= 9;
32 class SourceStreamSet
;
34 // An EncodedProgram is a set of tables that contain a simple 'binary assembly
35 // language' that can be assembled to produce a sequence of bytes, for example,
36 // a Windows 32-bit executable.
38 class EncodedProgram
{
43 // Generating an EncodedProgram:
45 // (1) The image base can be specified at any time.
46 void set_image_base(uint64 base
) { image_base_
= base
; }
48 // (2) Address tables and indexes defined first.
49 CheckBool
DefineRel32Label(int index
, RVA address
) WARN_UNUSED_RESULT
;
50 CheckBool
DefineAbs32Label(int index
, RVA address
) WARN_UNUSED_RESULT
;
53 // (3) Add instructions in the order needed to generate bytes of file.
54 // NOTE: If any of these methods ever fail, the EncodedProgram instance
55 // has failed and should be discarded.
56 CheckBool
AddOrigin(RVA rva
) WARN_UNUSED_RESULT
;
57 CheckBool
AddCopy(size_t count
, const void* bytes
) WARN_UNUSED_RESULT
;
58 CheckBool
AddRel32(int label_index
) WARN_UNUSED_RESULT
;
59 CheckBool
AddRel32ARM(uint16 op
, int label_index
) WARN_UNUSED_RESULT
;
60 CheckBool
AddAbs32(int label_index
) WARN_UNUSED_RESULT
;
61 CheckBool
AddAbs64(int label_index
) WARN_UNUSED_RESULT
;
62 CheckBool
AddPeMakeRelocs(ExecutableType kind
) WARN_UNUSED_RESULT
;
63 CheckBool
AddElfMakeRelocs() WARN_UNUSED_RESULT
;
64 CheckBool
AddElfARMMakeRelocs() WARN_UNUSED_RESULT
;
66 // (3) Serialize binary assembly language tables to a set of streams.
67 CheckBool
WriteTo(SinkStreamSet
* streams
) WARN_UNUSED_RESULT
;
69 // Using an EncodedProgram to generate a byte stream:
71 // (4) Deserializes a fresh EncodedProgram from a set of streams.
72 bool ReadFrom(SourceStreamSet
* streams
);
74 // (5) Assembles the 'binary assembly language' into final file.
75 CheckBool
AssembleTo(SinkStream
* buffer
) WARN_UNUSED_RESULT
;
78 // Binary assembly language operations.
79 // These are part of the patch format. Reusing an existing value will
80 // break backwards compatibility.
82 ORIGIN
= 0, // ORIGIN <rva> - set address for subsequent assembly.
83 COPY
= 1, // COPY <count> <bytes> - copy bytes to output.
84 COPY1
= 2, // COPY1 <byte> - same as COPY 1 <byte>.
85 REL32
= 3, // REL32 <index> - emit rel32 encoded reference to address at
86 // address table offset <index>
87 ABS32
= 4, // ABS32 <index> - emit abs32 encoded reference to address at
88 // address table offset <index>
89 MAKE_PE_RELOCATION_TABLE
= 5, // Emit PE base relocation table blocks.
90 MAKE_ELF_RELOCATION_TABLE
= 6, // Emit Elf relocation table for X86
91 MAKE_ELF_ARM_RELOCATION_TABLE
= 7, // Emit Elf relocation table for ARM
92 MAKE_PE64_RELOCATION_TABLE
= 8, // Emit PE64 base relocation table blocks.
93 ABS64
= 9, // ABS64 <index> - emit abs64 encoded reference to address at
94 // address table offset <index>
95 // ARM reserves 0x1000-LAST_ARM, bits 13-16 define the opcode
96 // subset, and 1-12 are the compressed ARM op.
105 typedef NoThrowBuffer
<RVA
> RvaVector
;
106 typedef NoThrowBuffer
<size_t> SizeTVector
;
107 typedef NoThrowBuffer
<uint32
> UInt32Vector
;
108 typedef NoThrowBuffer
<uint8
> UInt8Vector
;
109 typedef NoThrowBuffer
<OP
> OPVector
;
111 void DebuggingSummary();
112 CheckBool
GeneratePeRelocations(SinkStream
*buffer
,
113 uint8 type
) WARN_UNUSED_RESULT
;
114 CheckBool
GenerateElfRelocations(Elf32_Word pending_elf_relocation_table
,
115 SinkStream
*buffer
) WARN_UNUSED_RESULT
;
116 CheckBool
DefineLabelCommon(RvaVector
*, int, RVA
) WARN_UNUSED_RESULT
;
117 void FinishLabelsCommon(RvaVector
* addresses
);
119 // Decodes and evaluates courgette ops for ARM rel32 addresses.
120 CheckBool
EvaluateRel32ARM(OP op
, size_t& ix_rel32_ix
, RVA
& current_rva
,
123 // Binary assembly language tables.
125 RvaVector rel32_rva_
;
126 RvaVector abs32_rva_
;
129 SizeTVector copy_counts_
;
130 UInt8Vector copy_bytes_
;
131 UInt32Vector rel32_ix_
;
132 UInt32Vector abs32_ix_
;
134 // Table of the addresses containing abs32 relocations; computed during
135 // assembly, used to generate base relocation table.
136 UInt32Vector abs32_relocs_
;
138 DISALLOW_COPY_AND_ASSIGN(EncodedProgram
);
141 } // namespace courgette
142 #endif // COURGETTE_ENCODED_PROGRAM_H_