| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_DISASSEMBLER_ELF_32_H_ | 5 #ifndef COURGETTE_DISASSEMBLER_ELF_32_H_ |
| 6 #define COURGETTE_DISASSEMBLER_ELF_32_H_ | 6 #define COURGETTE_DISASSEMBLER_ELF_32_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 <string> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "courgette/disassembler.h" | 16 #include "courgette/disassembler.h" |
| 16 #include "courgette/image_utils.h" | 17 #include "courgette/image_utils.h" |
| 17 #include "courgette/memory_allocator.h" | 18 #include "courgette/memory_allocator.h" |
| 18 #include "courgette/types_elf.h" | 19 #include "courgette/types_elf.h" |
| 19 | 20 |
| 20 namespace courgette { | 21 namespace courgette { |
| 21 | 22 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 44 FileOffset file_offset() const { return file_offset_; } | 45 FileOffset file_offset() const { return file_offset_; } |
| 45 | 46 |
| 46 void set_relative_target(RVA relative_target) { | 47 void set_relative_target(RVA relative_target) { |
| 47 relative_target_ = relative_target; | 48 relative_target_ = relative_target; |
| 48 } | 49 } |
| 49 void set_file_offset(FileOffset file_offset) { file_offset_ = file_offset; } | 50 void set_file_offset(FileOffset file_offset) { file_offset_ = file_offset; } |
| 50 | 51 |
| 51 // Computes the relative jump's offset from the op in p. | 52 // Computes the relative jump's offset from the op in p. |
| 52 virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0; | 53 virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0; |
| 53 | 54 |
| 54 // Emits the courgette instruction corresponding to the RVA type. | 55 // Emits the assembly instruction corresponding to |label|. |
| 55 virtual CheckBool EmitInstruction(AssemblyProgram* program, | 56 virtual CheckBool EmitInstruction(AssemblyProgram* program, |
| 56 RVA target_rva) = 0; | 57 Label* label) = 0; |
| 57 | 58 |
| 58 // Returns the size of the instruction containing the RVA. | 59 // Returns the size of the instruction containing the RVA. |
| 59 virtual uint16_t op_size() const = 0; | 60 virtual uint16_t op_size() const = 0; |
| 60 | 61 |
| 61 // Comparator for sorting, which assumes uniqueness of RVAs. | 62 // Comparator for sorting, which assumes uniqueness of RVAs. |
| 62 static bool IsLessThanByRVA(const std::unique_ptr<TypedRVA>& a, | 63 static bool IsLessThanByRVA(const std::unique_ptr<TypedRVA>& a, |
| 63 const std::unique_ptr<TypedRVA>& b) { | 64 const std::unique_ptr<TypedRVA>& b) { |
| 64 return a->rva() < b->rva(); | 65 return a->rva() < b->rva(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 // Comparator for sorting, which assumes uniqueness of file offsets. | 68 // Comparator for sorting, which assumes uniqueness of file offsets. |
| 68 static bool IsLessThanByFileOffset(const std::unique_ptr<TypedRVA>& a, | 69 static bool IsLessThanByFileOffset(const std::unique_ptr<TypedRVA>& a, |
| 69 const std::unique_ptr<TypedRVA>& b) { | 70 const std::unique_ptr<TypedRVA>& b) { |
| 70 return a->file_offset() < b->file_offset(); | 71 return a->file_offset() < b->file_offset(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 private: | 74 private: |
| 74 const RVA rva_; | 75 const RVA rva_; |
| 75 RVA relative_target_ = kNoRVA; | 76 RVA relative_target_ = kNoRVA; |
| 76 FileOffset file_offset_ = kNoFileOffset; | 77 FileOffset file_offset_ = kNoFileOffset; |
| 77 }; | 78 }; |
| 78 | 79 |
| 80 // Visitor/adaptor to translate RVA to target RVA. This is the ELF |
| 81 // counterpart to RvaVisitor_Rel32 that uses TypedRVA. |
| 82 class Elf32RvaVisitor_Rel32 : |
| 83 public VectorRvaVisitor<std::unique_ptr<TypedRVA>> { |
| 84 public: |
| 85 Elf32RvaVisitor_Rel32( |
| 86 const std::vector<std::unique_ptr<TypedRVA>>& rva_locations); |
| 87 ~Elf32RvaVisitor_Rel32() override { } |
| 88 |
| 89 // VectorRvaVisitor<TypedRVA*> interfaces. |
| 90 RVA Get() const override; |
| 91 |
| 92 private: |
| 93 DISALLOW_COPY_AND_ASSIGN(Elf32RvaVisitor_Rel32); |
| 94 }; |
| 95 |
| 79 public: | 96 public: |
| 80 DisassemblerElf32(const void* start, size_t length); | 97 DisassemblerElf32(const void* start, size_t length); |
| 81 | 98 |
| 82 ~DisassemblerElf32() override { } | 99 ~DisassemblerElf32() override { } |
| 83 | 100 |
| 84 // Disassembler interfaces. | 101 // Disassembler interfaces. |
| 85 RVA FileOffsetToRVA(FileOffset file_offset) const override; | 102 RVA FileOffsetToRVA(FileOffset file_offset) const override; |
| 86 FileOffset RVAToFileOffset(RVA rva) const override; | 103 FileOffset RVAToFileOffset(RVA rva) const override; |
| 87 RVA PointerToTargetRVA(const uint8_t* p) const override; | 104 RVA PointerToTargetRVA(const uint8_t* p) const override; |
| 88 virtual ExecutableType kind() const override = 0; | 105 virtual ExecutableType kind() const override = 0; |
| 89 bool ParseHeader() override; | 106 bool ParseHeader() override; |
| 90 bool Disassemble(AssemblyProgram* target) override; | 107 bool Disassemble(AssemblyProgram* target) override; |
| 91 | 108 |
| 92 virtual e_machine_values ElfEM() const = 0; | 109 virtual e_machine_values ElfEM() const = 0; |
| 93 | 110 |
| 111 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; |
| 112 |
| 113 // Converts an ELF relocation instruction into an RVA. |
| 114 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) |
| 115 const WARN_UNUSED_RESULT = 0; |
| 116 |
| 94 // Public for unittests only | 117 // Public for unittests only |
| 95 std::vector<RVA>& Abs32Locations() { return abs32_locations_; } | 118 std::vector<RVA>& Abs32Locations() { return abs32_locations_; } |
| 96 std::vector<std::unique_ptr<TypedRVA>>& Rel32Locations() { | 119 std::vector<std::unique_ptr<TypedRVA>>& Rel32Locations() { |
| 97 return rel32_locations_; | 120 return rel32_locations_; |
| 98 } | 121 } |
| 99 | 122 |
| 100 protected: | 123 protected: |
| 101 bool UpdateLength(); | 124 bool UpdateLength(); |
| 102 | 125 |
| 103 // Misc Section Helpers | 126 // Misc Section Helpers |
| (...skipping 21 matching lines...) Expand all Loading... |
| 125 return program_header_table_size_; | 148 return program_header_table_size_; |
| 126 } | 149 } |
| 127 | 150 |
| 128 const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const { | 151 const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const { |
| 129 assert(id >= 0 && id < ProgramSegmentHeaderCount()); | 152 assert(id >= 0 && id < ProgramSegmentHeaderCount()); |
| 130 return program_header_table_ + id; | 153 return program_header_table_ + id; |
| 131 } | 154 } |
| 132 | 155 |
| 133 // Misc address space helpers | 156 // Misc address space helpers |
| 134 | 157 |
| 135 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; | |
| 136 | |
| 137 // Converts an ELF relocation instruction into an RVA. | |
| 138 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) | |
| 139 const WARN_UNUSED_RESULT = 0; | |
| 140 | |
| 141 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, | 158 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, |
| 142 std::vector<FileOffset>* file_offsets); | 159 std::vector<FileOffset>* file_offsets); |
| 143 | 160 |
| 144 CheckBool RVAsToFileOffsets( | 161 CheckBool RVAsToFileOffsets( |
| 145 std::vector<std::unique_ptr<TypedRVA>>* typed_rvas); | 162 std::vector<std::unique_ptr<TypedRVA>>* typed_rvas); |
| 146 | 163 |
| 147 // Parsing code for Disassemble(). | 164 // Parsing code for Disassemble(). |
| 148 | 165 |
| 149 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, | 166 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, |
| 150 AssemblyProgram* program) | 167 AssemblyProgram* program) |
| 151 WARN_UNUSED_RESULT = 0; | 168 WARN_UNUSED_RESULT = 0; |
| 152 | 169 |
| 153 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) | 170 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) |
| 154 WARN_UNUSED_RESULT = 0; | 171 WARN_UNUSED_RESULT = 0; |
| 155 | 172 |
| 173 // Disassembler interfaces. |
| 174 RvaVisitor* CreateAbs32TargetRvaVisitor() override; |
| 175 RvaVisitor* CreateRel32TargetRvaVisitor() override; |
| 176 void RemoveUnusedRel32Locations(AssemblyProgram* program) override; |
| 177 |
| 156 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; | 178 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; |
| 157 | 179 |
| 158 CheckBool ParseProgbitsSection( | 180 CheckBool ParseProgbitsSection( |
| 159 const Elf32_Shdr* section_header, | 181 const Elf32_Shdr* section_header, |
| 160 std::vector<FileOffset>::iterator* current_abs_offset, | 182 std::vector<FileOffset>::iterator* current_abs_offset, |
| 161 std::vector<FileOffset>::iterator end_abs_offset, | 183 std::vector<FileOffset>::iterator end_abs_offset, |
| 162 std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel, | 184 std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel, |
| 163 std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel, | 185 std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel, |
| 164 AssemblyProgram* program) WARN_UNUSED_RESULT; | 186 AssemblyProgram* program) WARN_UNUSED_RESULT; |
| 165 | 187 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 193 | 215 |
| 194 std::vector<RVA> abs32_locations_; | 216 std::vector<RVA> abs32_locations_; |
| 195 std::vector<std::unique_ptr<TypedRVA>> rel32_locations_; | 217 std::vector<std::unique_ptr<TypedRVA>> rel32_locations_; |
| 196 | 218 |
| 197 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); | 219 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); |
| 198 }; | 220 }; |
| 199 | 221 |
| 200 } // namespace courgette | 222 } // namespace courgette |
| 201 | 223 |
| 202 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ | 224 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ |
| OLD | NEW |