| 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 <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "courgette/disassembler.h" | 16 #include "courgette/disassembler.h" |
| 17 #include "courgette/image_utils.h" | 17 #include "courgette/image_utils.h" |
| 18 #include "courgette/memory_allocator.h" | 18 #include "courgette/memory_allocator.h" |
| 19 #include "courgette/types_elf.h" | 19 #include "courgette/types_elf.h" |
| 20 | 20 |
| 21 namespace courgette { | 21 namespace courgette { |
| 22 | 22 |
| 23 class AssemblyProgram; | 23 class AssemblyProgram; |
| 24 class InstructionReceptor; |
| 24 | 25 |
| 25 // A Courgette disassembler for 32-bit ELF files. This is only a partial | 26 // A Courgette disassembler for 32-bit ELF files. This is only a partial |
| 26 // implementation that admits subclasses for the architecture-specific parts of | 27 // implementation that admits subclasses for the architecture-specific parts of |
| 27 // 32-bit ELF file processing. Specifically: | 28 // 32-bit ELF file processing. Specifically: |
| 28 // - RelToRVA() processes entries in ELF relocation table. | 29 // - RelToRVA() processes entries in ELF relocation table. |
| 29 // - ParseRelocationSection() verifies the organization of the ELF relocation | 30 // - ParseRelocationSection() verifies the organization of the ELF relocation |
| 30 // table. | 31 // table. |
| 31 // - ParseRel32RelocsFromSection() finds branch targets by looking for relative | 32 // - ParseRel32RelocsFromSection() finds branch targets by looking for relative |
| 32 // branch/call opcodes in the particular architecture's machine code. | 33 // branch/call opcodes in the particular architecture's machine code. |
| 33 class DisassemblerElf32 : public Disassembler { | 34 class DisassemblerElf32 : public Disassembler { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 46 | 47 |
| 47 void set_relative_target(RVA relative_target) { | 48 void set_relative_target(RVA relative_target) { |
| 48 relative_target_ = relative_target; | 49 relative_target_ = relative_target; |
| 49 } | 50 } |
| 50 void set_file_offset(FileOffset file_offset) { file_offset_ = file_offset; } | 51 void set_file_offset(FileOffset file_offset) { file_offset_ = file_offset; } |
| 51 | 52 |
| 52 // Computes the relative jump's offset from the op in p. | 53 // Computes the relative jump's offset from the op in p. |
| 53 virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0; | 54 virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0; |
| 54 | 55 |
| 55 // Emits the assembly instruction corresponding to |label|. | 56 // Emits the assembly instruction corresponding to |label|. |
| 56 virtual CheckBool EmitInstruction(AssemblyProgram* program, | 57 virtual CheckBool EmitInstruction(Label* label, |
| 57 Label* label) = 0; | 58 InstructionReceptor* receptor) = 0; |
| 58 | 59 |
| 59 // Returns the size of the instruction containing the RVA. | 60 // Returns the size of the instruction containing the RVA. |
| 60 virtual uint16_t op_size() const = 0; | 61 virtual uint16_t op_size() const = 0; |
| 61 | 62 |
| 62 // Comparator for sorting, which assumes uniqueness of RVAs. | 63 // Comparator for sorting, which assumes uniqueness of RVAs. |
| 63 static bool IsLessThanByRVA(const std::unique_ptr<TypedRVA>& a, | 64 static bool IsLessThanByRVA(const std::unique_ptr<TypedRVA>& a, |
| 64 const std::unique_ptr<TypedRVA>& b) { | 65 const std::unique_ptr<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(const std::unique_ptr<TypedRVA>& a, | 70 static bool IsLessThanByFileOffset(const std::unique_ptr<TypedRVA>& a, |
| 70 const std::unique_ptr<TypedRVA>& b) { | 71 const std::unique_ptr<TypedRVA>& b) { |
| 71 return a->file_offset() < b->file_offset(); | 72 return a->file_offset() < b->file_offset(); |
| 72 } | 73 } |
| 73 | 74 |
| 74 private: | 75 private: |
| 75 const RVA rva_; | 76 const RVA rva_; |
| 76 RVA relative_target_ = kNoRVA; | 77 RVA relative_target_ = kNoRVA; |
| 77 FileOffset file_offset_ = kNoFileOffset; | 78 FileOffset file_offset_ = kNoFileOffset; |
| 78 }; | 79 }; |
| 79 | 80 |
| 80 // Visitor/adaptor to translate RVA to target RVA. This is the ELF | 81 // Visitor/adaptor to translate RVA to target RVA. This is the ELF |
| 81 // counterpart to RvaVisitor_Rel32 that uses TypedRVA. | 82 // counterpart to RvaVisitor_Rel32 that uses TypedRVA. |
| 82 class Elf32RvaVisitor_Rel32 : | 83 class Elf32RvaVisitor_Rel32 : |
| 83 public VectorRvaVisitor<std::unique_ptr<TypedRVA>> { | 84 public VectorRvaVisitor<std::unique_ptr<TypedRVA>> { |
| 84 public: | 85 public: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 95 | 96 |
| 96 public: | 97 public: |
| 97 DisassemblerElf32(const uint8_t* start, size_t length); | 98 DisassemblerElf32(const uint8_t* start, size_t length); |
| 98 | 99 |
| 99 ~DisassemblerElf32() override { } | 100 ~DisassemblerElf32() override { } |
| 100 | 101 |
| 101 // Disassembler interfaces. | 102 // Disassembler interfaces. |
| 102 RVA FileOffsetToRVA(FileOffset file_offset) const override; | 103 RVA FileOffsetToRVA(FileOffset file_offset) const override; |
| 103 FileOffset RVAToFileOffset(RVA rva) const override; | 104 FileOffset RVAToFileOffset(RVA rva) const override; |
| 104 RVA PointerToTargetRVA(const uint8_t* p) const override; | 105 RVA PointerToTargetRVA(const uint8_t* p) const override; |
| 105 virtual ExecutableType kind() const override = 0; | 106 ExecutableType kind() const override = 0; |
| 106 bool ParseHeader() override; | 107 bool ParseHeader() override; |
| 107 bool Disassemble(AssemblyProgram* target) override; | 108 bool Disassemble(AssemblyProgram* target) override; |
| 108 | 109 |
| 109 virtual e_machine_values ElfEM() const = 0; | 110 virtual e_machine_values ElfEM() const = 0; |
| 110 | 111 |
| 111 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; | 112 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; |
| 112 | 113 |
| 113 // Converts an ELF relocation instruction into an RVA. | 114 // Converts an ELF relocation instruction into an RVA. |
| 114 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) | 115 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) |
| 115 const WARN_UNUSED_RESULT = 0; | 116 const WARN_UNUSED_RESULT = 0; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 } | 157 } |
| 157 | 158 |
| 158 const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const { | 159 const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const { |
| 159 assert(id >= 0 && id < ProgramSegmentHeaderCount()); | 160 assert(id >= 0 && id < ProgramSegmentHeaderCount()); |
| 160 return program_header_table_ + id; | 161 return program_header_table_ + id; |
| 161 } | 162 } |
| 162 | 163 |
| 163 // Misc address space helpers | 164 // Misc address space helpers |
| 164 | 165 |
| 165 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, | 166 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, |
| 166 std::vector<FileOffset>* file_offsets); | 167 std::vector<FileOffset>* file_offsets) const; |
| 167 | 168 |
| 168 CheckBool RVAsToFileOffsets( | 169 CheckBool RVAsToFileOffsets( |
| 169 std::vector<std::unique_ptr<TypedRVA>>* typed_rvas); | 170 std::vector<std::unique_ptr<TypedRVA>>* typed_rvas) const; |
| 170 | 171 |
| 171 // Parsing code for Disassemble(). | 172 // Parsing code for Disassemble(). |
| 172 | 173 |
| 173 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, | 174 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, |
| 174 AssemblyProgram* program) | 175 InstructionReceptor* receptor) const |
| 175 WARN_UNUSED_RESULT = 0; | 176 WARN_UNUSED_RESULT = 0; |
| 176 | 177 |
| 177 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) | 178 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) |
| 178 WARN_UNUSED_RESULT = 0; | 179 WARN_UNUSED_RESULT = 0; |
| 179 | 180 |
| 180 // Disassembler interfaces. | 181 // Disassembler interfaces. |
| 181 RvaVisitor* CreateAbs32TargetRvaVisitor() override; | 182 RvaVisitor* CreateAbs32TargetRvaVisitor() override; |
| 182 RvaVisitor* CreateRel32TargetRvaVisitor() override; | 183 RvaVisitor* CreateRel32TargetRvaVisitor() override; |
| 183 void RemoveUnusedRel32Locations(AssemblyProgram* program) override; | 184 void RemoveUnusedRel32Locations(AssemblyProgram* program) override; |
| 184 | 185 |
| 185 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; | 186 CheckBool ParseFile(AssemblyProgram* target, |
| 187 InstructionReceptor* receptor) const WARN_UNUSED_RESULT; |
| 186 | 188 |
| 187 CheckBool ParseProgbitsSection( | 189 CheckBool ParseProgbitsSection( |
| 188 const Elf32_Shdr* section_header, | 190 const Elf32_Shdr* section_header, |
| 189 std::vector<FileOffset>::iterator* current_abs_offset, | 191 std::vector<FileOffset>::iterator* current_abs_offset, |
| 190 std::vector<FileOffset>::iterator end_abs_offset, | 192 std::vector<FileOffset>::iterator end_abs_offset, |
| 191 std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel, | 193 std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel, |
| 192 std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel, | 194 std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel, |
| 193 AssemblyProgram* program) WARN_UNUSED_RESULT; | 195 AssemblyProgram* program, |
| 196 InstructionReceptor* receptor) const WARN_UNUSED_RESULT; |
| 194 | 197 |
| 195 CheckBool ParseSimpleRegion(FileOffset start_file_offset, | 198 CheckBool ParseSimpleRegion(FileOffset start_file_offset, |
| 196 FileOffset end_file_offset, | 199 FileOffset end_file_offset, |
| 197 AssemblyProgram* program) WARN_UNUSED_RESULT; | 200 InstructionReceptor* receptor) const |
| 201 WARN_UNUSED_RESULT; |
| 198 | 202 |
| 199 CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT; | 203 CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT; |
| 200 | 204 |
| 201 CheckBool CheckSection(RVA rva) WARN_UNUSED_RESULT; | 205 CheckBool CheckSection(RVA rva) WARN_UNUSED_RESULT; |
| 202 | 206 |
| 203 // Extracts all rel32 TypedRVAs. Does not sort the result. | 207 // Extracts all rel32 TypedRVAs. Does not sort the result. |
| 204 CheckBool ParseRel32RelocsFromSections() WARN_UNUSED_RESULT; | 208 CheckBool ParseRel32RelocsFromSections() WARN_UNUSED_RESULT; |
| 205 | 209 |
| 206 const Elf32_Ehdr* header_; | 210 const Elf32_Ehdr* header_; |
| 207 | 211 |
| 208 Elf32_Half section_header_table_size_; | 212 Elf32_Half section_header_table_size_; |
| 209 | 213 |
| 210 // Section header table, ordered by section id. | 214 // Section header table, ordered by section id. |
| 211 std::vector<Elf32_Shdr> section_header_table_; | 215 std::vector<Elf32_Shdr> section_header_table_; |
| 212 | 216 |
| 213 // An ordering of |section_header_table_|, sorted by file offset. | 217 // An ordering of |section_header_table_|, sorted by file offset. |
| 214 std::vector<Elf32_Half> section_header_file_offset_order_; | 218 std::vector<Elf32_Half> section_header_file_offset_order_; |
| 215 | 219 |
| 216 const Elf32_Phdr* program_header_table_; | 220 const Elf32_Phdr* program_header_table_; |
| 217 Elf32_Half program_header_table_size_; | 221 Elf32_Half program_header_table_size_; |
| 218 | 222 |
| 219 // Pointer to string table containing section names. | 223 // Pointer to string table containing section names. |
| 220 const char* default_string_section_; | 224 const char* default_string_section_; |
| 221 size_t default_string_section_size_; | 225 size_t default_string_section_size_; |
| 222 | 226 |
| 223 std::vector<RVA> abs32_locations_; | 227 // Sorted abs32 and reel32 RVAs. These are mutable because ParseFile() needs |
| 224 std::vector<std::unique_ptr<TypedRVA>> rel32_locations_; | 228 // to sort these by file offsets. |
| 229 mutable std::vector<RVA> abs32_locations_; |
| 230 mutable std::vector<std::unique_ptr<TypedRVA>> rel32_locations_; |
| 225 | 231 |
| 232 private: |
| 226 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); | 233 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); |
| 227 }; | 234 }; |
| 228 | 235 |
| 229 } // namespace courgette | 236 } // namespace courgette |
| 230 | 237 |
| 231 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ | 238 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ |
| OLD | NEW |