| 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 <string> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.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 { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 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) { | 50 void set_file_offset(FileOffset file_offset) { |
| 50 file_offset_ = file_offset; | 51 file_offset_ = file_offset; |
| 51 } | 52 } |
| 52 | 53 |
| 53 // Computes the relative jump's offset from the op in p. | 54 // Computes the relative jump's offset from the op in p. |
| 54 virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0; | 55 virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0; |
| 55 | 56 |
| 56 // Emits the courgette instruction corresponding to the RVA type. | 57 // Emits the assembly instruction corresponding to |label|. |
| 57 virtual CheckBool EmitInstruction(AssemblyProgram* program, | 58 virtual CheckBool EmitInstruction(AssemblyProgram* program, |
| 58 RVA target_rva) = 0; | 59 Label* label) = 0; |
| 59 | 60 |
| 60 // Returns the size of the instruction containing the RVA. | 61 // Returns the size of the instruction containing the RVA. |
| 61 virtual uint16_t op_size() const = 0; | 62 virtual uint16_t op_size() const = 0; |
| 62 | 63 |
| 63 // Comparator for sorting, which assumes uniqueness of RVAs. | 64 // Comparator for sorting, which assumes uniqueness of RVAs. |
| 64 static bool IsLessThanByRVA(TypedRVA* a, TypedRVA* b) { | 65 static bool IsLessThanByRVA(TypedRVA* a, TypedRVA* b) { |
| 65 return a->rva() < b->rva(); | 66 return a->rva() < b->rva(); |
| 66 } | 67 } |
| 67 | 68 |
| 68 // Comparator for sorting, which assumes uniqueness of file offsets. | 69 // Comparator for sorting, which assumes uniqueness of file offsets. |
| 69 static bool IsLessThanByFileOffset(TypedRVA* a, TypedRVA* b) { | 70 static bool IsLessThanByFileOffset(TypedRVA* a, 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 : public VectorRvaVisitor<TypedRVA*> { |
| 83 public: |
| 84 Elf32RvaVisitor_Rel32(const std::vector<TypedRVA*>& rva_locations); |
| 85 ~Elf32RvaVisitor_Rel32() override { } |
| 86 |
| 87 // VectorRvaVisitor<TypedRVA*> interfaces. |
| 88 RVA Get() const override; |
| 89 |
| 90 private: |
| 91 DISALLOW_COPY_AND_ASSIGN(Elf32RvaVisitor_Rel32); |
| 92 }; |
| 93 |
| 79 public: | 94 public: |
| 80 DisassemblerElf32(const void* start, size_t length); | 95 DisassemblerElf32(const void* start, size_t length); |
| 81 | 96 |
| 82 ~DisassemblerElf32() override { } | 97 ~DisassemblerElf32() override { } |
| 83 | 98 |
| 84 // Disassembler interfaces. | 99 // Disassembler interfaces. |
| 85 RVA FileOffsetToRVA(FileOffset file_offset) const override; | 100 RVA FileOffsetToRVA(FileOffset file_offset) const override; |
| 86 FileOffset RVAToFileOffset(RVA rva) const override; | 101 FileOffset RVAToFileOffset(RVA rva) const override; |
| 87 RVA PointerToTargetRVA(const uint8_t* p) const override; | 102 RVA PointerToTargetRVA(const uint8_t* p) const override; |
| 88 virtual ExecutableType kind() const override = 0; | 103 virtual ExecutableType kind() const override = 0; |
| 89 bool ParseHeader() override; | 104 bool ParseHeader() override; |
| 90 bool Disassemble(AssemblyProgram* target) override; | 105 bool Disassemble(AssemblyProgram* target) override; |
| 91 | 106 |
| 92 virtual e_machine_values ElfEM() const = 0; | 107 virtual e_machine_values ElfEM() const = 0; |
| 93 | 108 |
| 109 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; |
| 110 |
| 111 // Converts an ELF relocation instruction into an RVA. |
| 112 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) |
| 113 const WARN_UNUSED_RESULT = 0; |
| 114 |
| 94 // Public for unittests only | 115 // Public for unittests only |
| 95 std::vector<RVA> &Abs32Locations() { return abs32_locations_; } | 116 std::vector<RVA> &Abs32Locations() { return abs32_locations_; } |
| 96 ScopedVector<TypedRVA> &Rel32Locations() { return rel32_locations_; } | 117 ScopedVector<TypedRVA> &Rel32Locations() { return rel32_locations_; } |
| 97 | 118 |
| 98 protected: | 119 protected: |
| 99 bool UpdateLength(); | 120 bool UpdateLength(); |
| 100 | 121 |
| 101 // Misc Section Helpers | 122 // Misc Section Helpers |
| 102 | 123 |
| 103 Elf32_Half SectionHeaderCount() const { | 124 Elf32_Half SectionHeaderCount() const { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 123 return program_header_table_size_; | 144 return program_header_table_size_; |
| 124 } | 145 } |
| 125 | 146 |
| 126 const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const { | 147 const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const { |
| 127 assert(id >= 0 && id < ProgramSegmentHeaderCount()); | 148 assert(id >= 0 && id < ProgramSegmentHeaderCount()); |
| 128 return program_header_table_ + id; | 149 return program_header_table_ + id; |
| 129 } | 150 } |
| 130 | 151 |
| 131 // Misc address space helpers | 152 // Misc address space helpers |
| 132 | 153 |
| 133 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; | |
| 134 | |
| 135 // Converts an ELF relocation instruction into an RVA. | |
| 136 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) | |
| 137 const WARN_UNUSED_RESULT = 0; | |
| 138 | |
| 139 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, | 154 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, |
| 140 std::vector<FileOffset>* file_offsets); | 155 std::vector<FileOffset>* file_offsets); |
| 141 | 156 |
| 142 CheckBool RVAsToFileOffsets(ScopedVector<TypedRVA>* typed_rvas); | 157 CheckBool RVAsToFileOffsets(ScopedVector<TypedRVA>* typed_rvas); |
| 143 | 158 |
| 144 // Parsing code for Disassemble(). | 159 // Parsing code for Disassemble(). |
| 145 | 160 |
| 146 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, | 161 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, |
| 147 AssemblyProgram* program) | 162 AssemblyProgram* program) |
| 148 WARN_UNUSED_RESULT = 0; | 163 WARN_UNUSED_RESULT = 0; |
| 149 | 164 |
| 150 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) | 165 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) |
| 151 WARN_UNUSED_RESULT = 0; | 166 WARN_UNUSED_RESULT = 0; |
| 152 | 167 |
| 168 // Disassembler interfaces. |
| 169 RvaVisitor* CreateAbs32TargetRvaVisitor() override; |
| 170 RvaVisitor* CreateRel32TargetRvaVisitor() override; |
| 171 void RemoveUnusedRel32Locations(AssemblyProgram* program) override; |
| 172 |
| 153 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; | 173 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; |
| 154 | 174 |
| 155 CheckBool ParseProgbitsSection( | 175 CheckBool ParseProgbitsSection( |
| 156 const Elf32_Shdr* section_header, | 176 const Elf32_Shdr* section_header, |
| 157 std::vector<FileOffset>::iterator* current_abs_offset, | 177 std::vector<FileOffset>::iterator* current_abs_offset, |
| 158 std::vector<FileOffset>::iterator end_abs_offset, | 178 std::vector<FileOffset>::iterator end_abs_offset, |
| 159 ScopedVector<TypedRVA>::iterator* current_rel, | 179 ScopedVector<TypedRVA>::iterator* current_rel, |
| 160 ScopedVector<TypedRVA>::iterator end_rel, | 180 ScopedVector<TypedRVA>::iterator end_rel, |
| 161 AssemblyProgram* program) WARN_UNUSED_RESULT; | 181 AssemblyProgram* program) WARN_UNUSED_RESULT; |
| 162 | 182 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 190 | 210 |
| 191 std::vector<RVA> abs32_locations_; | 211 std::vector<RVA> abs32_locations_; |
| 192 ScopedVector<TypedRVA> rel32_locations_; | 212 ScopedVector<TypedRVA> rel32_locations_; |
| 193 | 213 |
| 194 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); | 214 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); |
| 195 }; | 215 }; |
| 196 | 216 |
| 197 } // namespace courgette | 217 } // namespace courgette |
| 198 | 218 |
| 199 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ | 219 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ |
| OLD | NEW |