blob: 390514606191399bc1a364911560029461abb98b [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
aviab98dcc92015-12-21 19:35:338#include <stddef.h>
9#include <stdint.h>
10
mostynb1007a4a2016-04-11 23:18:0611#include <memory>
[email protected]f76b3b02009-05-06 04:02:1012#include <vector>
13
aviab98dcc92015-12-21 19:35:3314#include "base/macros.h"
huangs19281f32017-05-02 16:59:0815#include "courgette/courgette.h"
16#include "courgette/image_utils.h"
huangs88451332017-05-18 19:50:3417#include "courgette/instruction_utils.h"
[email protected]fbd31eb2011-03-01 00:19:0218#include "courgette/memory_allocator.h"
[email protected]a8e80412013-07-18 22:07:5319#include "courgette/types_elf.h"
[email protected]f76b3b02009-05-06 04:02:1020
21namespace courgette {
22
huangs809972412015-09-09 23:37:0223// Stream indexes.
24const int kStreamMisc = 0;
25const int kStreamOps = 1;
26const int kStreamBytes = 2;
27const int kStreamAbs32Indexes = 3;
28const int kStreamRel32Indexes = 4;
29const int kStreamAbs32Addresses = 5;
30const int kStreamRel32Addresses = 6;
31const int kStreamCopyCounts = 7;
32const int kStreamOriginAddresses = kStreamMisc;
33
34const int kStreamLimit = 9;
35
huangs19281f32017-05-02 16:59:0836class LabelManager;
[email protected]f76b3b02009-05-06 04:02:1037class SinkStream;
38class SinkStreamSet;
39class SourceStreamSet;
40
huangs19281f32017-05-02 16:59:0841// EncodedProgram encodes Courgette's simple "binary assembly language", which
42// can be assembled to produce a sequence of bytes (e.g., a Windows 32-bit
43// executable).
[email protected]f76b3b02009-05-06 04:02:1044class EncodedProgram {
45 public:
46 EncodedProgram();
47 ~EncodedProgram();
48
49 // Generating an EncodedProgram:
50 //
51 // (1) The image base can be specified at any time.
aviab98dcc92015-12-21 19:35:3352 void set_image_base(uint64_t base) { image_base_ = base; }
[email protected]f76b3b02009-05-06 04:02:1053
huangsc8037632016-05-19 18:16:4054 // (2) Address tables and indexes imported first.
huangsbb4b8a92016-01-19 22:09:0355
huangsc8037632016-05-19 18:16:4056 CheckBool ImportLabels(const LabelManager& abs32_label_manager,
57 const LabelManager& rel32_label_manager)
58 WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:1059
60 // (3) Add instructions in the order needed to generate bytes of file.
[email protected]43a9e242011-04-06 17:42:4561 // NOTE: If any of these methods ever fail, the EncodedProgram instance
62 // has failed and should be discarded.
63 CheckBool AddOrigin(RVA rva) WARN_UNUSED_RESULT;
pkasting8e3a26a2014-10-03 18:52:2964 CheckBool AddCopy(size_t count, const void* bytes) WARN_UNUSED_RESULT;
[email protected]43a9e242011-04-06 17:42:4565 CheckBool AddRel32(int label_index) WARN_UNUSED_RESULT;
aviab98dcc92015-12-21 19:35:3366 CheckBool AddRel32ARM(uint16_t op, int label_index) WARN_UNUSED_RESULT;
[email protected]43a9e242011-04-06 17:42:4567 CheckBool AddAbs32(int label_index) WARN_UNUSED_RESULT;
wfhfde55c72015-03-13 04:24:1968 CheckBool AddAbs64(int label_index) WARN_UNUSED_RESULT;
[email protected]11336c02013-09-25 19:05:5169 CheckBool AddPeMakeRelocs(ExecutableType kind) WARN_UNUSED_RESULT;
[email protected]4b3d192b2011-11-08 20:32:2670 CheckBool AddElfMakeRelocs() WARN_UNUSED_RESULT;
[email protected]a8e80412013-07-18 22:07:5371 CheckBool AddElfARMMakeRelocs() WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:1072
73 // (3) Serialize binary assembly language tables to a set of streams.
[email protected]43a9e242011-04-06 17:42:4574 CheckBool WriteTo(SinkStreamSet* streams) WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:1075
76 // Using an EncodedProgram to generate a byte stream:
77 //
78 // (4) Deserializes a fresh EncodedProgram from a set of streams.
[email protected]c8240b12011-03-22 20:19:4979 bool ReadFrom(SourceStreamSet* streams);
[email protected]f76b3b02009-05-06 04:02:1080
81 // (5) Assembles the 'binary assembly language' into final file.
[email protected]43a9e242011-04-06 17:42:4582 CheckBool AssembleTo(SinkStream* buffer) WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:1083
huangs88451332017-05-18 19:50:3484 // Calls |gen| to extract all instructions, which are then encoded and stored.
85 CheckBool GenerateInstructions(ExecutableType exe_type,
86 const InstructionGenerator& gen);
87
[email protected]f76b3b02009-05-06 04:02:1088 private:
[email protected]54f1b822009-07-18 03:28:4089 // Binary assembly language operations.
[email protected]4b3d192b2011-11-08 20:32:2690 // These are part of the patch format. Reusing an existing value will
91 // break backwards compatibility.
[email protected]54f1b822009-07-18 03:28:4092 enum OP {
huangs19281f32017-05-02 16:59:0893 ORIGIN = 0, // ORIGIN <rva> - set address for subsequent assembly.
94 COPY = 1, // COPY <count> <bytes> - copy bytes to output.
95 COPY1 = 2, // COPY1 <byte> - same as COPY 1 <byte>.
96 REL32 = 3, // REL32 <index> - emit rel32 encoded reference to address at
97 // address table offset <index>
98 ABS32 = 4, // ABS32 <index> - emit abs32 encoded reference to address at
99 // address table offset <index>
100 MAKE_PE_RELOCATION_TABLE = 5, // Emit PE base relocation table blocks.
101 MAKE_ELF_RELOCATION_TABLE = 6, // Emit Elf relocation table for X86
102 MAKE_ELF_ARM_RELOCATION_TABLE = 7, // Emit Elf relocation table for ARM
103 MAKE_PE64_RELOCATION_TABLE = 8, // Emit PE64 base relocation table blocks.
104 ABS64 = 9, // ABS64 <index> - emit abs64 encoded reference to address at
105 // address table offset <index>
[email protected]2b637b62013-08-01 00:11:24106 // ARM reserves 0x1000-LAST_ARM, bits 13-16 define the opcode
107 // subset, and 1-12 are the compressed ARM op.
huangs19281f32017-05-02 16:59:08108 REL32ARM8 = 0x1000,
109 REL32ARM11 = 0x2000,
110 REL32ARM24 = 0x3000,
111 REL32ARM25 = 0x4000,
112 REL32ARM21 = 0x5000,
113 LAST_ARM = 0x5FFF,
[email protected]54f1b822009-07-18 03:28:40114 };
[email protected]f76b3b02009-05-06 04:02:10115
[email protected]43a9e242011-04-06 17:42:45116 typedef NoThrowBuffer<RVA> RvaVector;
pkasting8e3a26a2014-10-03 18:52:29117 typedef NoThrowBuffer<size_t> SizeTVector;
aviab98dcc92015-12-21 19:35:33118 typedef NoThrowBuffer<uint32_t> UInt32Vector;
119 typedef NoThrowBuffer<uint8_t> UInt8Vector;
[email protected]43a9e242011-04-06 17:42:45120 typedef NoThrowBuffer<OP> OPVector;
[email protected]fbd31eb2011-03-01 00:19:02121
[email protected]f76b3b02009-05-06 04:02:10122 void DebuggingSummary();
huangsc8037632016-05-19 18:16:40123
124 // Helper for ImportLabels().
125 static CheckBool WriteRvasToList(const LabelManager& label_manager,
126 RvaVector* rvas);
127
128 // Helper for ImportLabels().
129 static void FillUnassignedRvaSlots(RvaVector* rvas);
130
aviab98dcc92015-12-21 19:35:33131 CheckBool GeneratePeRelocations(SinkStream* buffer,
132 uint8_t type) WARN_UNUSED_RESULT;
[email protected]a8e80412013-07-18 22:07:53133 CheckBool GenerateElfRelocations(Elf32_Word pending_elf_relocation_table,
huangs19281f32017-05-02 16:59:08134 SinkStream* buffer) WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:10135
[email protected]2b637b62013-08-01 00:11:24136 // Decodes and evaluates courgette ops for ARM rel32 addresses.
huangs19281f32017-05-02 16:59:08137 CheckBool EvaluateRel32ARM(OP op,
138 size_t* ix_rel32_ix,
139 RVA* current_rva,
[email protected]2b637b62013-08-01 00:11:24140 SinkStream* output);
141
[email protected]f76b3b02009-05-06 04:02:10142 // Binary assembly language tables.
huangsbb4b8a92016-01-19 22:09:03143 uint64_t image_base_ = 0;
[email protected]fbd31eb2011-03-01 00:19:02144 RvaVector rel32_rva_;
145 RvaVector abs32_rva_;
146 OPVector ops_;
147 RvaVector origins_;
pkasting8e3a26a2014-10-03 18:52:29148 SizeTVector copy_counts_;
[email protected]fbd31eb2011-03-01 00:19:02149 UInt8Vector copy_bytes_;
150 UInt32Vector rel32_ix_;
151 UInt32Vector abs32_ix_;
[email protected]f76b3b02009-05-06 04:02:10152
153 // Table of the addresses containing abs32 relocations; computed during
154 // assembly, used to generate base relocation table.
[email protected]fbd31eb2011-03-01 00:19:02155 UInt32Vector abs32_relocs_;
[email protected]f76b3b02009-05-06 04:02:10156
157 DISALLOW_COPY_AND_ASSIGN(EncodedProgram);
158};
159
huangs8d5be252016-01-29 22:14:18160// Deserializes program from a stream set to |*output|. Returns C_OK if
161// successful, otherwise assigns |*output| to null and returns an error status.
162Status ReadEncodedProgram(SourceStreamSet* source,
mostynb1007a4a2016-04-11 23:18:06163 std::unique_ptr<EncodedProgram>* output);
huangs8d5be252016-01-29 22:14:18164
[email protected]f76b3b02009-05-06 04:02:10165} // namespace courgette
huangs8d5be252016-01-29 22:14:18166
[email protected]54f1b822009-07-18 03:28:40167#endif // COURGETTE_ENCODED_PROGRAM_H_