Avi Drissman | 0ff8a7e | 2022-09-13 18:35:42 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
huangs | 257f9fb | 2017-03-23 23:17:50 | [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_INSTRUCTION_UTILS_H_ |
| 6 | #define COURGETTE_INSTRUCTION_UTILS_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
Avi Drissman | 710fdab | 2023-01-11 04:37:36 | [diff] [blame] | 10 | #include "base/functional/callback.h" |
huangs | 257f9fb | 2017-03-23 23:17:50 | [diff] [blame] | 11 | #include "courgette/image_utils.h" |
| 12 | #include "courgette/memory_allocator.h" |
| 13 | |
| 14 | namespace courgette { |
| 15 | |
| 16 | // An interface to receive emitted instructions parsed from an executable. |
| 17 | class InstructionReceptor { |
| 18 | public: |
| 19 | InstructionReceptor() = default; |
Peter Boström | c68c5aa | 2021-09-28 00:28:00 | [diff] [blame] | 20 | |
| 21 | InstructionReceptor(const InstructionReceptor&) = delete; |
| 22 | InstructionReceptor& operator=(const InstructionReceptor&) = delete; |
| 23 | |
huangs | 257f9fb | 2017-03-23 23:17:50 | [diff] [blame] | 24 | virtual ~InstructionReceptor() = default; |
| 25 | |
| 26 | // Generates an entire base relocation table. |
| 27 | virtual CheckBool EmitPeRelocs() = 0; |
| 28 | |
| 29 | // Generates an ELF style relocation table for X86. |
| 30 | virtual CheckBool EmitElfRelocation() = 0; |
| 31 | |
huangs | 257f9fb | 2017-03-23 23:17:50 | [diff] [blame] | 32 | // Following instruction will be assembled at address 'rva'. |
| 33 | virtual CheckBool EmitOrigin(RVA rva) = 0; |
| 34 | |
| 35 | // Generates a single byte of data or machine instruction. |
| 36 | virtual CheckBool EmitSingleByte(uint8_t byte) = 0; |
| 37 | |
| 38 | // Generates multiple bytes of data or machine instructions. |
| 39 | virtual CheckBool EmitMultipleBytes(const uint8_t* bytes, size_t len) = 0; |
| 40 | |
| 41 | // Generates a 4-byte relative reference to address of 'label'. |
| 42 | virtual CheckBool EmitRel32(Label* label) = 0; |
| 43 | |
huangs | 257f9fb | 2017-03-23 23:17:50 | [diff] [blame] | 44 | // Generates a 4-byte absolute reference to address of 'label'. |
| 45 | virtual CheckBool EmitAbs32(Label* label) = 0; |
| 46 | |
| 47 | // Generates an 8-byte absolute reference to address of 'label'. |
| 48 | virtual CheckBool EmitAbs64(Label* label) = 0; |
huangs | 257f9fb | 2017-03-23 23:17:50 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | // A rerunable callback that emit instructions to a provided receptor. Returns |
| 52 | // true on success, and false otherwise. |
tzik | 0881a400 | 2018-03-08 06:19:49 | [diff] [blame] | 53 | using InstructionGenerator = |
| 54 | base::RepeatingCallback<CheckBool(InstructionReceptor*)>; |
huangs | 257f9fb | 2017-03-23 23:17:50 | [diff] [blame] | 55 | |
huangs | 8845133 | 2017-05-18 19:50:34 | [diff] [blame] | 56 | // A counter that increments via .push_back(), so it can be passed via template |
| 57 | // to substitute std::vector<T>, to count elements instead of storing them. |
| 58 | template <typename T> |
| 59 | class CountingVector { |
| 60 | public: |
| 61 | CountingVector() {} |
| 62 | |
| 63 | void push_back(const T& /* unused */) { ++size_; } |
| 64 | size_t size() const { return size_; } |
| 65 | |
| 66 | private: |
| 67 | size_t size_ = 0; |
| 68 | }; |
| 69 | |
huangs | 257f9fb | 2017-03-23 23:17:50 | [diff] [blame] | 70 | } // namespace courgette |
| 71 | |
| 72 | #endif // COURGETTE_INSTRUCTION_UTILS_H_ |