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