[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 2 | // 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 | |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
mostynb | 1007a4a | 2016-04-11 23:18:06 | [diff] [blame] | 11 | #include <memory> |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 14 | #include "base/macros.h" |
huangs | 19281f3 | 2017-05-02 16:59:08 | [diff] [blame] | 15 | #include "courgette/courgette.h" |
| 16 | #include "courgette/image_utils.h" |
huangs | 8845133 | 2017-05-18 19:50:34 | [diff] [blame] | 17 | #include "courgette/instruction_utils.h" |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 18 | #include "courgette/memory_allocator.h" |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 19 | #include "courgette/types_elf.h" |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 20 | |
| 21 | namespace courgette { |
| 22 | |
huangs | 80997241 | 2015-09-09 23:37:02 | [diff] [blame] | 23 | // Stream indexes. |
| 24 | const int kStreamMisc = 0; |
| 25 | const int kStreamOps = 1; |
| 26 | const int kStreamBytes = 2; |
| 27 | const int kStreamAbs32Indexes = 3; |
| 28 | const int kStreamRel32Indexes = 4; |
| 29 | const int kStreamAbs32Addresses = 5; |
| 30 | const int kStreamRel32Addresses = 6; |
| 31 | const int kStreamCopyCounts = 7; |
| 32 | const int kStreamOriginAddresses = kStreamMisc; |
| 33 | |
| 34 | const int kStreamLimit = 9; |
| 35 | |
huangs | 19281f3 | 2017-05-02 16:59:08 | [diff] [blame] | 36 | class LabelManager; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 37 | class SinkStream; |
| 38 | class SinkStreamSet; |
| 39 | class SourceStreamSet; |
| 40 | |
huangs | 19281f3 | 2017-05-02 16:59:08 | [diff] [blame] | 41 | // 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] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 44 | class EncodedProgram { |
| 45 | public: |
| 46 | EncodedProgram(); |
| 47 | ~EncodedProgram(); |
| 48 | |
| 49 | // Generating an EncodedProgram: |
| 50 | // |
| 51 | // (1) The image base can be specified at any time. |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 52 | void set_image_base(uint64_t base) { image_base_ = base; } |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 53 | |
huangs | c803763 | 2016-05-19 18:16:40 | [diff] [blame] | 54 | // (2) Address tables and indexes imported first. |
huangs | bb4b8a9 | 2016-01-19 22:09:03 | [diff] [blame] | 55 | |
huangs | c803763 | 2016-05-19 18:16:40 | [diff] [blame] | 56 | CheckBool ImportLabels(const LabelManager& abs32_label_manager, |
| 57 | const LabelManager& rel32_label_manager) |
| 58 | WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 59 | |
| 60 | // (3) Add instructions in the order needed to generate bytes of file. |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 61 | // 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; |
pkasting | 8e3a26a | 2014-10-03 18:52:29 | [diff] [blame] | 64 | CheckBool AddCopy(size_t count, const void* bytes) WARN_UNUSED_RESULT; |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 65 | CheckBool AddRel32(int label_index) WARN_UNUSED_RESULT; |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 66 | CheckBool AddRel32ARM(uint16_t op, int label_index) WARN_UNUSED_RESULT; |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 67 | CheckBool AddAbs32(int label_index) WARN_UNUSED_RESULT; |
wfh | fde55c7 | 2015-03-13 04:24:19 | [diff] [blame] | 68 | CheckBool AddAbs64(int label_index) WARN_UNUSED_RESULT; |
[email protected] | 11336c0 | 2013-09-25 19:05:51 | [diff] [blame] | 69 | CheckBool AddPeMakeRelocs(ExecutableType kind) WARN_UNUSED_RESULT; |
[email protected] | 4b3d192b | 2011-11-08 20:32:26 | [diff] [blame] | 70 | CheckBool AddElfMakeRelocs() WARN_UNUSED_RESULT; |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 71 | CheckBool AddElfARMMakeRelocs() WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 72 | |
| 73 | // (3) Serialize binary assembly language tables to a set of streams. |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 74 | CheckBool WriteTo(SinkStreamSet* streams) WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 75 | |
| 76 | // Using an EncodedProgram to generate a byte stream: |
| 77 | // |
| 78 | // (4) Deserializes a fresh EncodedProgram from a set of streams. |
[email protected] | c8240b1 | 2011-03-22 20:19:49 | [diff] [blame] | 79 | bool ReadFrom(SourceStreamSet* streams); |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 80 | |
| 81 | // (5) Assembles the 'binary assembly language' into final file. |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 82 | CheckBool AssembleTo(SinkStream* buffer) WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 83 | |
huangs | 8845133 | 2017-05-18 19:50:34 | [diff] [blame] | 84 | // 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] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 88 | private: |
[email protected] | 54f1b82 | 2009-07-18 03:28:40 | [diff] [blame] | 89 | // Binary assembly language operations. |
[email protected] | 4b3d192b | 2011-11-08 20:32:26 | [diff] [blame] | 90 | // These are part of the patch format. Reusing an existing value will |
| 91 | // break backwards compatibility. |
[email protected] | 54f1b82 | 2009-07-18 03:28:40 | [diff] [blame] | 92 | enum OP { |
huangs | 19281f3 | 2017-05-02 16:59:08 | [diff] [blame] | 93 | 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] | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 106 | // ARM reserves 0x1000-LAST_ARM, bits 13-16 define the opcode |
| 107 | // subset, and 1-12 are the compressed ARM op. |
huangs | 19281f3 | 2017-05-02 16:59:08 | [diff] [blame] | 108 | REL32ARM8 = 0x1000, |
| 109 | REL32ARM11 = 0x2000, |
| 110 | REL32ARM24 = 0x3000, |
| 111 | REL32ARM25 = 0x4000, |
| 112 | REL32ARM21 = 0x5000, |
| 113 | LAST_ARM = 0x5FFF, |
[email protected] | 54f1b82 | 2009-07-18 03:28:40 | [diff] [blame] | 114 | }; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 115 | |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 116 | typedef NoThrowBuffer<RVA> RvaVector; |
pkasting | 8e3a26a | 2014-10-03 18:52:29 | [diff] [blame] | 117 | typedef NoThrowBuffer<size_t> SizeTVector; |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 118 | typedef NoThrowBuffer<uint32_t> UInt32Vector; |
| 119 | typedef NoThrowBuffer<uint8_t> UInt8Vector; |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 120 | typedef NoThrowBuffer<OP> OPVector; |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 121 | |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 122 | void DebuggingSummary(); |
huangs | c803763 | 2016-05-19 18:16:40 | [diff] [blame] | 123 | |
| 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 | |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 131 | CheckBool GeneratePeRelocations(SinkStream* buffer, |
| 132 | uint8_t type) WARN_UNUSED_RESULT; |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 133 | CheckBool GenerateElfRelocations(Elf32_Word pending_elf_relocation_table, |
huangs | 19281f3 | 2017-05-02 16:59:08 | [diff] [blame] | 134 | SinkStream* buffer) WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 135 | |
[email protected] | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 136 | // Decodes and evaluates courgette ops for ARM rel32 addresses. |
huangs | 19281f3 | 2017-05-02 16:59:08 | [diff] [blame] | 137 | CheckBool EvaluateRel32ARM(OP op, |
| 138 | size_t* ix_rel32_ix, |
| 139 | RVA* current_rva, |
[email protected] | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 140 | SinkStream* output); |
| 141 | |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 142 | // Binary assembly language tables. |
huangs | bb4b8a9 | 2016-01-19 22:09:03 | [diff] [blame] | 143 | uint64_t image_base_ = 0; |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 144 | RvaVector rel32_rva_; |
| 145 | RvaVector abs32_rva_; |
| 146 | OPVector ops_; |
| 147 | RvaVector origins_; |
pkasting | 8e3a26a | 2014-10-03 18:52:29 | [diff] [blame] | 148 | SizeTVector copy_counts_; |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 149 | UInt8Vector copy_bytes_; |
| 150 | UInt32Vector rel32_ix_; |
| 151 | UInt32Vector abs32_ix_; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 152 | |
| 153 | // Table of the addresses containing abs32 relocations; computed during |
| 154 | // assembly, used to generate base relocation table. |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 155 | UInt32Vector abs32_relocs_; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 156 | |
| 157 | DISALLOW_COPY_AND_ASSIGN(EncodedProgram); |
| 158 | }; |
| 159 | |
huangs | 8d5be25 | 2016-01-29 22:14:18 | [diff] [blame] | 160 | // Deserializes program from a stream set to |*output|. Returns C_OK if |
| 161 | // successful, otherwise assigns |*output| to null and returns an error status. |
| 162 | Status ReadEncodedProgram(SourceStreamSet* source, |
mostynb | 1007a4a | 2016-04-11 23:18:06 | [diff] [blame] | 163 | std::unique_ptr<EncodedProgram>* output); |
huangs | 8d5be25 | 2016-01-29 22:14:18 | [diff] [blame] | 164 | |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 165 | } // namespace courgette |
huangs | 8d5be25 | 2016-01-29 22:14:18 | [diff] [blame] | 166 | |
[email protected] | 54f1b82 | 2009-07-18 03:28:40 | [diff] [blame] | 167 | #endif // COURGETTE_ENCODED_PROGRAM_H_ |