blob: 1169331b1d8d6a94ce47b7c488f60004a893059b [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"
[email protected]fbd31eb2011-03-01 00:19:0217#include "courgette/memory_allocator.h"
[email protected]a8e80412013-07-18 22:07:5318#include "courgette/types_elf.h"
[email protected]f76b3b02009-05-06 04:02:1019
20namespace courgette {
21
huangs809972412015-09-09 23:37:0222// Stream indexes.
23const int kStreamMisc = 0;
24const int kStreamOps = 1;
25const int kStreamBytes = 2;
26const int kStreamAbs32Indexes = 3;
27const int kStreamRel32Indexes = 4;
28const int kStreamAbs32Addresses = 5;
29const int kStreamRel32Addresses = 6;
30const int kStreamCopyCounts = 7;
31const int kStreamOriginAddresses = kStreamMisc;
32
33const int kStreamLimit = 9;
34
huangs19281f32017-05-02 16:59:0835class LabelManager;
[email protected]f76b3b02009-05-06 04:02:1036class SinkStream;
37class SinkStreamSet;
38class SourceStreamSet;
39
huangs19281f32017-05-02 16:59:0840// EncodedProgram encodes Courgette's simple "binary assembly language", which
41// can be assembled to produce a sequence of bytes (e.g., a Windows 32-bit
42// executable).
[email protected]f76b3b02009-05-06 04:02:1043class EncodedProgram {
44 public:
45 EncodedProgram();
46 ~EncodedProgram();
47
48 // Generating an EncodedProgram:
49 //
50 // (1) The image base can be specified at any time.
aviab98dcc92015-12-21 19:35:3351 void set_image_base(uint64_t base) { image_base_ = base; }
[email protected]f76b3b02009-05-06 04:02:1052
huangsc8037632016-05-19 18:16:4053 // (2) Address tables and indexes imported first.
huangsbb4b8a92016-01-19 22:09:0354
huangsc8037632016-05-19 18:16:4055 CheckBool ImportLabels(const LabelManager& abs32_label_manager,
56 const LabelManager& rel32_label_manager)
57 WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:1058
59 // (3) Add instructions in the order needed to generate bytes of file.
[email protected]43a9e242011-04-06 17:42:4560 // NOTE: If any of these methods ever fail, the EncodedProgram instance
61 // has failed and should be discarded.
62 CheckBool AddOrigin(RVA rva) WARN_UNUSED_RESULT;
pkasting8e3a26a2014-10-03 18:52:2963 CheckBool AddCopy(size_t count, const void* bytes) WARN_UNUSED_RESULT;
[email protected]43a9e242011-04-06 17:42:4564 CheckBool AddRel32(int label_index) WARN_UNUSED_RESULT;
aviab98dcc92015-12-21 19:35:3365 CheckBool AddRel32ARM(uint16_t op, int label_index) WARN_UNUSED_RESULT;
[email protected]43a9e242011-04-06 17:42:4566 CheckBool AddAbs32(int label_index) WARN_UNUSED_RESULT;
wfhfde55c72015-03-13 04:24:1967 CheckBool AddAbs64(int label_index) WARN_UNUSED_RESULT;
[email protected]11336c02013-09-25 19:05:5168 CheckBool AddPeMakeRelocs(ExecutableType kind) WARN_UNUSED_RESULT;
[email protected]4b3d192b2011-11-08 20:32:2669 CheckBool AddElfMakeRelocs() WARN_UNUSED_RESULT;
[email protected]a8e80412013-07-18 22:07:5370 CheckBool AddElfARMMakeRelocs() WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:1071
72 // (3) Serialize binary assembly language tables to a set of streams.
[email protected]43a9e242011-04-06 17:42:4573 CheckBool WriteTo(SinkStreamSet* streams) WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:1074
75 // Using an EncodedProgram to generate a byte stream:
76 //
77 // (4) Deserializes a fresh EncodedProgram from a set of streams.
[email protected]c8240b12011-03-22 20:19:4978 bool ReadFrom(SourceStreamSet* streams);
[email protected]f76b3b02009-05-06 04:02:1079
80 // (5) Assembles the 'binary assembly language' into final file.
[email protected]43a9e242011-04-06 17:42:4581 CheckBool AssembleTo(SinkStream* buffer) WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:1082
83 private:
[email protected]54f1b822009-07-18 03:28:4084 // Binary assembly language operations.
[email protected]4b3d192b2011-11-08 20:32:2685 // These are part of the patch format. Reusing an existing value will
86 // break backwards compatibility.
[email protected]54f1b822009-07-18 03:28:4087 enum OP {
huangs19281f32017-05-02 16:59:0888 ORIGIN = 0, // ORIGIN <rva> - set address for subsequent assembly.
89 COPY = 1, // COPY <count> <bytes> - copy bytes to output.
90 COPY1 = 2, // COPY1 <byte> - same as COPY 1 <byte>.
91 REL32 = 3, // REL32 <index> - emit rel32 encoded reference to address at
92 // address table offset <index>
93 ABS32 = 4, // ABS32 <index> - emit abs32 encoded reference to address at
94 // address table offset <index>
95 MAKE_PE_RELOCATION_TABLE = 5, // Emit PE base relocation table blocks.
96 MAKE_ELF_RELOCATION_TABLE = 6, // Emit Elf relocation table for X86
97 MAKE_ELF_ARM_RELOCATION_TABLE = 7, // Emit Elf relocation table for ARM
98 MAKE_PE64_RELOCATION_TABLE = 8, // Emit PE64 base relocation table blocks.
99 ABS64 = 9, // ABS64 <index> - emit abs64 encoded reference to address at
100 // address table offset <index>
[email protected]2b637b62013-08-01 00:11:24101 // ARM reserves 0x1000-LAST_ARM, bits 13-16 define the opcode
102 // subset, and 1-12 are the compressed ARM op.
huangs19281f32017-05-02 16:59:08103 REL32ARM8 = 0x1000,
104 REL32ARM11 = 0x2000,
105 REL32ARM24 = 0x3000,
106 REL32ARM25 = 0x4000,
107 REL32ARM21 = 0x5000,
108 LAST_ARM = 0x5FFF,
[email protected]54f1b822009-07-18 03:28:40109 };
[email protected]f76b3b02009-05-06 04:02:10110
[email protected]43a9e242011-04-06 17:42:45111 typedef NoThrowBuffer<RVA> RvaVector;
pkasting8e3a26a2014-10-03 18:52:29112 typedef NoThrowBuffer<size_t> SizeTVector;
aviab98dcc92015-12-21 19:35:33113 typedef NoThrowBuffer<uint32_t> UInt32Vector;
114 typedef NoThrowBuffer<uint8_t> UInt8Vector;
[email protected]43a9e242011-04-06 17:42:45115 typedef NoThrowBuffer<OP> OPVector;
[email protected]fbd31eb2011-03-01 00:19:02116
[email protected]f76b3b02009-05-06 04:02:10117 void DebuggingSummary();
huangsc8037632016-05-19 18:16:40118
119 // Helper for ImportLabels().
120 static CheckBool WriteRvasToList(const LabelManager& label_manager,
121 RvaVector* rvas);
122
123 // Helper for ImportLabels().
124 static void FillUnassignedRvaSlots(RvaVector* rvas);
125
aviab98dcc92015-12-21 19:35:33126 CheckBool GeneratePeRelocations(SinkStream* buffer,
127 uint8_t type) WARN_UNUSED_RESULT;
[email protected]a8e80412013-07-18 22:07:53128 CheckBool GenerateElfRelocations(Elf32_Word pending_elf_relocation_table,
huangs19281f32017-05-02 16:59:08129 SinkStream* buffer) WARN_UNUSED_RESULT;
[email protected]f76b3b02009-05-06 04:02:10130
[email protected]2b637b62013-08-01 00:11:24131 // Decodes and evaluates courgette ops for ARM rel32 addresses.
huangs19281f32017-05-02 16:59:08132 CheckBool EvaluateRel32ARM(OP op,
133 size_t* ix_rel32_ix,
134 RVA* current_rva,
[email protected]2b637b62013-08-01 00:11:24135 SinkStream* output);
136
[email protected]f76b3b02009-05-06 04:02:10137 // Binary assembly language tables.
huangsbb4b8a92016-01-19 22:09:03138 uint64_t image_base_ = 0;
[email protected]fbd31eb2011-03-01 00:19:02139 RvaVector rel32_rva_;
140 RvaVector abs32_rva_;
141 OPVector ops_;
142 RvaVector origins_;
pkasting8e3a26a2014-10-03 18:52:29143 SizeTVector copy_counts_;
[email protected]fbd31eb2011-03-01 00:19:02144 UInt8Vector copy_bytes_;
145 UInt32Vector rel32_ix_;
146 UInt32Vector abs32_ix_;
[email protected]f76b3b02009-05-06 04:02:10147
148 // Table of the addresses containing abs32 relocations; computed during
149 // assembly, used to generate base relocation table.
[email protected]fbd31eb2011-03-01 00:19:02150 UInt32Vector abs32_relocs_;
[email protected]f76b3b02009-05-06 04:02:10151
152 DISALLOW_COPY_AND_ASSIGN(EncodedProgram);
153};
154
huangs8d5be252016-01-29 22:14:18155// Deserializes program from a stream set to |*output|. Returns C_OK if
156// successful, otherwise assigns |*output| to null and returns an error status.
157Status ReadEncodedProgram(SourceStreamSet* source,
mostynb1007a4a2016-04-11 23:18:06158 std::unique_ptr<EncodedProgram>* output);
huangs8d5be252016-01-29 22:14:18159
[email protected]f76b3b02009-05-06 04:02:10160} // namespace courgette
huangs8d5be252016-01-29 22:14:18161
[email protected]54f1b822009-07-18 03:28:40162#endif // COURGETTE_ENCODED_PROGRAM_H_