| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COURGETTE_ENCODED_PROGRAM_H_ | 5 #ifndef COURGETTE_ENCODED_PROGRAM_H_ |
| 6 #define COURGETTE_ENCODED_PROGRAM_H_ | 6 #define COURGETTE_ENCODED_PROGRAM_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "courgette/disassembler.h" | 15 #include "courgette/courgette.h" |
| 16 #include "courgette/label_manager.h" | 16 #include "courgette/image_utils.h" |
| 17 #include "courgette/memory_allocator.h" | 17 #include "courgette/memory_allocator.h" |
| 18 #include "courgette/types_elf.h" | 18 #include "courgette/types_elf.h" |
| 19 | 19 |
| 20 namespace courgette { | 20 namespace courgette { |
| 21 | 21 |
| 22 // Stream indexes. | 22 // Stream indexes. |
| 23 const int kStreamMisc = 0; | 23 const int kStreamMisc = 0; |
| 24 const int kStreamOps = 1; | 24 const int kStreamOps = 1; |
| 25 const int kStreamBytes = 2; | 25 const int kStreamBytes = 2; |
| 26 const int kStreamAbs32Indexes = 3; | 26 const int kStreamAbs32Indexes = 3; |
| 27 const int kStreamRel32Indexes = 4; | 27 const int kStreamRel32Indexes = 4; |
| 28 const int kStreamAbs32Addresses = 5; | 28 const int kStreamAbs32Addresses = 5; |
| 29 const int kStreamRel32Addresses = 6; | 29 const int kStreamRel32Addresses = 6; |
| 30 const int kStreamCopyCounts = 7; | 30 const int kStreamCopyCounts = 7; |
| 31 const int kStreamOriginAddresses = kStreamMisc; | 31 const int kStreamOriginAddresses = kStreamMisc; |
| 32 | 32 |
| 33 const int kStreamLimit = 9; | 33 const int kStreamLimit = 9; |
| 34 | 34 |
| 35 class LabelManager; |
| 35 class SinkStream; | 36 class SinkStream; |
| 36 class SinkStreamSet; | 37 class SinkStreamSet; |
| 37 class SourceStreamSet; | 38 class SourceStreamSet; |
| 38 | 39 |
| 39 // An EncodedProgram is a set of tables that contain a simple 'binary assembly | 40 // EncodedProgram encodes Courgette's simple "binary assembly language", which |
| 40 // language' that can be assembled to produce a sequence of bytes, for example, | 41 // can be assembled to produce a sequence of bytes (e.g., a Windows 32-bit |
| 41 // a Windows 32-bit executable. | 42 // executable). |
| 42 // | |
| 43 class EncodedProgram { | 43 class EncodedProgram { |
| 44 public: | 44 public: |
| 45 EncodedProgram(); | 45 EncodedProgram(); |
| 46 ~EncodedProgram(); | 46 ~EncodedProgram(); |
| 47 | 47 |
| 48 // Generating an EncodedProgram: | 48 // Generating an EncodedProgram: |
| 49 // | 49 // |
| 50 // (1) The image base can be specified at any time. | 50 // (1) The image base can be specified at any time. |
| 51 void set_image_base(uint64_t base) { image_base_ = base; } | 51 void set_image_base(uint64_t base) { image_base_ = base; } |
| 52 | 52 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 78 bool ReadFrom(SourceStreamSet* streams); | 78 bool ReadFrom(SourceStreamSet* streams); |
| 79 | 79 |
| 80 // (5) Assembles the 'binary assembly language' into final file. | 80 // (5) Assembles the 'binary assembly language' into final file. |
| 81 CheckBool AssembleTo(SinkStream* buffer) WARN_UNUSED_RESULT; | 81 CheckBool AssembleTo(SinkStream* buffer) WARN_UNUSED_RESULT; |
| 82 | 82 |
| 83 private: | 83 private: |
| 84 // Binary assembly language operations. | 84 // Binary assembly language operations. |
| 85 // These are part of the patch format. Reusing an existing value will | 85 // These are part of the patch format. Reusing an existing value will |
| 86 // break backwards compatibility. | 86 // break backwards compatibility. |
| 87 enum OP { | 87 enum OP { |
| 88 ORIGIN = 0, // ORIGIN <rva> - set address for subsequent assembly. | 88 ORIGIN = 0, // ORIGIN <rva> - set address for subsequent assembly. |
| 89 COPY = 1, // COPY <count> <bytes> - copy bytes to output. | 89 COPY = 1, // COPY <count> <bytes> - copy bytes to output. |
| 90 COPY1 = 2, // COPY1 <byte> - same as COPY 1 <byte>. | 90 COPY1 = 2, // COPY1 <byte> - same as COPY 1 <byte>. |
| 91 REL32 = 3, // REL32 <index> - emit rel32 encoded reference to address at | 91 REL32 = 3, // REL32 <index> - emit rel32 encoded reference to address at |
| 92 // address table offset <index> | 92 // address table offset <index> |
| 93 ABS32 = 4, // ABS32 <index> - emit abs32 encoded reference to address at | 93 ABS32 = 4, // ABS32 <index> - emit abs32 encoded reference to address at |
| 94 // address table offset <index> | 94 // address table offset <index> |
| 95 MAKE_PE_RELOCATION_TABLE = 5, // Emit PE base relocation table blocks. | 95 MAKE_PE_RELOCATION_TABLE = 5, // Emit PE base relocation table blocks. |
| 96 MAKE_ELF_RELOCATION_TABLE = 6, // Emit Elf relocation table for X86 | 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 | 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. | 98 MAKE_PE64_RELOCATION_TABLE = 8, // Emit PE64 base relocation table blocks. |
| 99 ABS64 = 9, // ABS64 <index> - emit abs64 encoded reference to address at | 99 ABS64 = 9, // ABS64 <index> - emit abs64 encoded reference to address at |
| 100 // address table offset <index> | 100 // address table offset <index> |
| 101 // ARM reserves 0x1000-LAST_ARM, bits 13-16 define the opcode | 101 // ARM reserves 0x1000-LAST_ARM, bits 13-16 define the opcode |
| 102 // subset, and 1-12 are the compressed ARM op. | 102 // subset, and 1-12 are the compressed ARM op. |
| 103 REL32ARM8 = 0x1000, | 103 REL32ARM8 = 0x1000, |
| 104 REL32ARM11 = 0x2000, | 104 REL32ARM11 = 0x2000, |
| 105 REL32ARM24 = 0x3000, | 105 REL32ARM24 = 0x3000, |
| 106 REL32ARM25 = 0x4000, | 106 REL32ARM25 = 0x4000, |
| 107 REL32ARM21 = 0x5000, | 107 REL32ARM21 = 0x5000, |
| 108 LAST_ARM = 0x5FFF, | 108 LAST_ARM = 0x5FFF, |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 typedef NoThrowBuffer<RVA> RvaVector; | 111 typedef NoThrowBuffer<RVA> RvaVector; |
| 112 typedef NoThrowBuffer<size_t> SizeTVector; | 112 typedef NoThrowBuffer<size_t> SizeTVector; |
| 113 typedef NoThrowBuffer<uint32_t> UInt32Vector; | 113 typedef NoThrowBuffer<uint32_t> UInt32Vector; |
| 114 typedef NoThrowBuffer<uint8_t> UInt8Vector; | 114 typedef NoThrowBuffer<uint8_t> UInt8Vector; |
| 115 typedef NoThrowBuffer<OP> OPVector; | 115 typedef NoThrowBuffer<OP> OPVector; |
| 116 | 116 |
| 117 void DebuggingSummary(); | 117 void DebuggingSummary(); |
| 118 | 118 |
| 119 // Helper for ImportLabels(). | 119 // Helper for ImportLabels(). |
| 120 static CheckBool WriteRvasToList(const LabelManager& label_manager, | 120 static CheckBool WriteRvasToList(const LabelManager& label_manager, |
| 121 RvaVector* rvas); | 121 RvaVector* rvas); |
| 122 | 122 |
| 123 // Helper for ImportLabels(). | 123 // Helper for ImportLabels(). |
| 124 static void FillUnassignedRvaSlots(RvaVector* rvas); | 124 static void FillUnassignedRvaSlots(RvaVector* rvas); |
| 125 | 125 |
| 126 CheckBool GeneratePeRelocations(SinkStream* buffer, | 126 CheckBool GeneratePeRelocations(SinkStream* buffer, |
| 127 uint8_t type) WARN_UNUSED_RESULT; | 127 uint8_t type) WARN_UNUSED_RESULT; |
| 128 CheckBool GenerateElfRelocations(Elf32_Word pending_elf_relocation_table, | 128 CheckBool GenerateElfRelocations(Elf32_Word pending_elf_relocation_table, |
| 129 SinkStream *buffer) WARN_UNUSED_RESULT; | 129 SinkStream* buffer) WARN_UNUSED_RESULT; |
| 130 | 130 |
| 131 // Decodes and evaluates courgette ops for ARM rel32 addresses. | 131 // Decodes and evaluates courgette ops for ARM rel32 addresses. |
| 132 CheckBool EvaluateRel32ARM(OP op, size_t& ix_rel32_ix, RVA& current_rva, | 132 CheckBool EvaluateRel32ARM(OP op, |
| 133 size_t* ix_rel32_ix, |
| 134 RVA* current_rva, |
| 133 SinkStream* output); | 135 SinkStream* output); |
| 134 | 136 |
| 135 // Binary assembly language tables. | 137 // Binary assembly language tables. |
| 136 uint64_t image_base_ = 0; | 138 uint64_t image_base_ = 0; |
| 137 RvaVector rel32_rva_; | 139 RvaVector rel32_rva_; |
| 138 RvaVector abs32_rva_; | 140 RvaVector abs32_rva_; |
| 139 OPVector ops_; | 141 OPVector ops_; |
| 140 RvaVector origins_; | 142 RvaVector origins_; |
| 141 SizeTVector copy_counts_; | 143 SizeTVector copy_counts_; |
| 142 UInt8Vector copy_bytes_; | 144 UInt8Vector copy_bytes_; |
| 143 UInt32Vector rel32_ix_; | 145 UInt32Vector rel32_ix_; |
| 144 UInt32Vector abs32_ix_; | 146 UInt32Vector abs32_ix_; |
| 145 | 147 |
| 146 // Table of the addresses containing abs32 relocations; computed during | 148 // Table of the addresses containing abs32 relocations; computed during |
| 147 // assembly, used to generate base relocation table. | 149 // assembly, used to generate base relocation table. |
| 148 UInt32Vector abs32_relocs_; | 150 UInt32Vector abs32_relocs_; |
| 149 | 151 |
| 150 DISALLOW_COPY_AND_ASSIGN(EncodedProgram); | 152 DISALLOW_COPY_AND_ASSIGN(EncodedProgram); |
| 151 }; | 153 }; |
| 152 | 154 |
| 153 // Deserializes program from a stream set to |*output|. Returns C_OK if | 155 // Deserializes program from a stream set to |*output|. Returns C_OK if |
| 154 // successful, otherwise assigns |*output| to null and returns an error status. | 156 // successful, otherwise assigns |*output| to null and returns an error status. |
| 155 Status ReadEncodedProgram(SourceStreamSet* source, | 157 Status ReadEncodedProgram(SourceStreamSet* source, |
| 156 std::unique_ptr<EncodedProgram>* output); | 158 std::unique_ptr<EncodedProgram>* output); |
| 157 | 159 |
| 158 } // namespace courgette | 160 } // namespace courgette |
| 159 | 161 |
| 160 #endif // COURGETTE_ENCODED_PROGRAM_H_ | 162 #endif // COURGETTE_ENCODED_PROGRAM_H_ |
| OLD | NEW |