[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 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/basictypes.h" |
[email protected] | 423a381 | 2011-10-26 00:50:20 | [diff] [blame] | 11 | #include "courgette/disassembler.h" |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 12 | #include "courgette/memory_allocator.h" |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 13 | #include "courgette/types_elf.h" |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 14 | |
| 15 | namespace courgette { |
| 16 | |
| 17 | class SinkStream; |
| 18 | class SinkStreamSet; |
| 19 | class SourceStreamSet; |
| 20 | |
| 21 | // An EncodedProgram is a set of tables that contain a simple 'binary assembly |
| 22 | // language' that can be assembled to produce a sequence of bytes, for example, |
| 23 | // a Windows 32-bit executable. |
| 24 | // |
| 25 | class EncodedProgram { |
| 26 | public: |
| 27 | EncodedProgram(); |
| 28 | ~EncodedProgram(); |
| 29 | |
| 30 | // Generating an EncodedProgram: |
| 31 | // |
| 32 | // (1) The image base can be specified at any time. |
| 33 | void set_image_base(uint64 base) { image_base_ = base; } |
| 34 | |
| 35 | // (2) Address tables and indexes defined first. |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 36 | CheckBool DefineRel32Label(int index, RVA address) WARN_UNUSED_RESULT; |
| 37 | CheckBool DefineAbs32Label(int index, RVA address) WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 38 | void EndLabels(); |
| 39 | |
| 40 | // (3) Add instructions in the order needed to generate bytes of file. |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 41 | // NOTE: If any of these methods ever fail, the EncodedProgram instance |
| 42 | // has failed and should be discarded. |
| 43 | CheckBool AddOrigin(RVA rva) WARN_UNUSED_RESULT; |
pkasting | 8e3a26a | 2014-10-03 18:52:29 | [diff] [blame] | 44 | CheckBool AddCopy(size_t count, const void* bytes) WARN_UNUSED_RESULT; |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 45 | CheckBool AddRel32(int label_index) WARN_UNUSED_RESULT; |
[email protected] | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 46 | CheckBool AddRel32ARM(uint16 op, int label_index) WARN_UNUSED_RESULT; |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 47 | CheckBool AddAbs32(int label_index) WARN_UNUSED_RESULT; |
wfh | fde55c7 | 2015-03-13 04:24:19 | [diff] [blame] | 48 | CheckBool AddAbs64(int label_index) WARN_UNUSED_RESULT; |
[email protected] | 11336c0 | 2013-09-25 19:05:51 | [diff] [blame] | 49 | CheckBool AddPeMakeRelocs(ExecutableType kind) WARN_UNUSED_RESULT; |
[email protected] | 4b3d192b | 2011-11-08 20:32:26 | [diff] [blame] | 50 | CheckBool AddElfMakeRelocs() WARN_UNUSED_RESULT; |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 51 | CheckBool AddElfARMMakeRelocs() WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 52 | |
| 53 | // (3) Serialize binary assembly language tables to a set of streams. |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 54 | CheckBool WriteTo(SinkStreamSet* streams) WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 55 | |
| 56 | // Using an EncodedProgram to generate a byte stream: |
| 57 | // |
| 58 | // (4) Deserializes a fresh EncodedProgram from a set of streams. |
[email protected] | c8240b1 | 2011-03-22 20:19:49 | [diff] [blame] | 59 | bool ReadFrom(SourceStreamSet* streams); |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 60 | |
| 61 | // (5) Assembles the 'binary assembly language' into final file. |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 62 | CheckBool AssembleTo(SinkStream* buffer) WARN_UNUSED_RESULT; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 63 | |
| 64 | private: |
[email protected] | 54f1b82 | 2009-07-18 03:28:40 | [diff] [blame] | 65 | // Binary assembly language operations. |
[email protected] | 4b3d192b | 2011-11-08 20:32:26 | [diff] [blame] | 66 | // These are part of the patch format. Reusing an existing value will |
| 67 | // break backwards compatibility. |
[email protected] | 54f1b82 | 2009-07-18 03:28:40 | [diff] [blame] | 68 | enum OP { |
[email protected] | 4b3d192b | 2011-11-08 20:32:26 | [diff] [blame] | 69 | ORIGIN = 0, // ORIGIN <rva> - set address for subsequent assembly. |
| 70 | COPY = 1, // COPY <count> <bytes> - copy bytes to output. |
| 71 | COPY1 = 2, // COPY1 <byte> - same as COPY 1 <byte>. |
| 72 | REL32 = 3, // REL32 <index> - emit rel32 encoded reference to address at |
| 73 | // address table offset <index> |
| 74 | ABS32 = 4, // ABS32 <index> - emit abs32 encoded reference to address at |
| 75 | // address table offset <index> |
| 76 | MAKE_PE_RELOCATION_TABLE = 5, // Emit PE base relocation table blocks. |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 77 | MAKE_ELF_RELOCATION_TABLE = 6, // Emit Elf relocation table for X86 |
| 78 | MAKE_ELF_ARM_RELOCATION_TABLE = 7, // Emit Elf relocation table for ARM |
[email protected] | 11336c0 | 2013-09-25 19:05:51 | [diff] [blame] | 79 | MAKE_PE64_RELOCATION_TABLE = 8, // Emit PE64 base relocation table blocks. |
wfh | fde55c7 | 2015-03-13 04:24:19 | [diff] [blame] | 80 | ABS64 = 9, // ABS64 <index> - emit abs64 encoded reference to address at |
| 81 | // address table offset <index> |
[email protected] | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 82 | // ARM reserves 0x1000-LAST_ARM, bits 13-16 define the opcode |
| 83 | // subset, and 1-12 are the compressed ARM op. |
| 84 | REL32ARM8 = 0x1000, |
| 85 | REL32ARM11 = 0x2000, |
| 86 | REL32ARM24 = 0x3000, |
| 87 | REL32ARM25 = 0x4000, |
| 88 | REL32ARM21 = 0x5000, |
| 89 | LAST_ARM = 0x5FFF, |
[email protected] | 54f1b82 | 2009-07-18 03:28:40 | [diff] [blame] | 90 | }; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 91 | |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 92 | typedef NoThrowBuffer<RVA> RvaVector; |
pkasting | 8e3a26a | 2014-10-03 18:52:29 | [diff] [blame] | 93 | typedef NoThrowBuffer<size_t> SizeTVector; |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 94 | typedef NoThrowBuffer<uint32> UInt32Vector; |
| 95 | typedef NoThrowBuffer<uint8> UInt8Vector; |
| 96 | typedef NoThrowBuffer<OP> OPVector; |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 97 | |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 98 | void DebuggingSummary(); |
[email protected] | 11336c0 | 2013-09-25 19:05:51 | [diff] [blame] | 99 | CheckBool GeneratePeRelocations(SinkStream *buffer, |
| 100 | uint8 type) WARN_UNUSED_RESULT; |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 101 | CheckBool GenerateElfRelocations(Elf32_Word pending_elf_relocation_table, |
| 102 | SinkStream *buffer) WARN_UNUSED_RESULT; |
[email protected] | 43a9e24 | 2011-04-06 17:42:45 | [diff] [blame] | 103 | CheckBool DefineLabelCommon(RvaVector*, int, RVA) WARN_UNUSED_RESULT; |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 104 | void FinishLabelsCommon(RvaVector* addresses); |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 105 | |
[email protected] | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 106 | // Decodes and evaluates courgette ops for ARM rel32 addresses. |
| 107 | CheckBool EvaluateRel32ARM(OP op, size_t& ix_rel32_ix, RVA& current_rva, |
| 108 | SinkStream* output); |
| 109 | |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 110 | // Binary assembly language tables. |
| 111 | uint64 image_base_; |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 112 | RvaVector rel32_rva_; |
| 113 | RvaVector abs32_rva_; |
| 114 | OPVector ops_; |
| 115 | RvaVector origins_; |
pkasting | 8e3a26a | 2014-10-03 18:52:29 | [diff] [blame] | 116 | SizeTVector copy_counts_; |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 117 | UInt8Vector copy_bytes_; |
| 118 | UInt32Vector rel32_ix_; |
| 119 | UInt32Vector abs32_ix_; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 120 | |
| 121 | // Table of the addresses containing abs32 relocations; computed during |
| 122 | // assembly, used to generate base relocation table. |
[email protected] | fbd31eb | 2011-03-01 00:19:02 | [diff] [blame] | 123 | UInt32Vector abs32_relocs_; |
[email protected] | f76b3b0 | 2009-05-06 04:02:10 | [diff] [blame] | 124 | |
| 125 | DISALLOW_COPY_AND_ASSIGN(EncodedProgram); |
| 126 | }; |
| 127 | |
| 128 | } // namespace courgette |
[email protected] | 54f1b82 | 2009-07-18 03:28:40 | [diff] [blame] | 129 | #endif // COURGETTE_ENCODED_PROGRAM_H_ |