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/instruction_utils.h" | |
huangs
2017/03/23 18:51:23
Including for InstructionGenerator (as side effect
| |
18 #include "courgette/memory_allocator.h" | 19 #include "courgette/memory_allocator.h" |
19 #include "courgette/types_elf.h" | 20 #include "courgette/types_elf.h" |
20 | 21 |
21 namespace courgette { | 22 namespace courgette { |
22 | 23 |
23 class AssemblyProgram; | 24 class AssemblyProgram; |
24 class InstructionReceptor; | |
25 | 25 |
26 // 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 |
27 // implementation that admits subclasses for the architecture-specific parts of | 27 // implementation that admits subclasses for the architecture-specific parts of |
28 // 32-bit ELF file processing. Specifically: | 28 // 32-bit ELF file processing. Specifically: |
29 // - RelToRVA() processes entries in ELF relocation table. | 29 // - RelToRVA() processes entries in ELF relocation table. |
30 // - ParseRelocationSection() verifies the organization of the ELF relocation | 30 // - ParseRelocationSection() verifies the organization of the ELF relocation |
31 // table. | 31 // table. |
32 // - ParseRel32RelocsFromSection() finds branch targets by looking for relative | 32 // - ParseRel32RelocsFromSection() finds branch targets by looking for relative |
33 // branch/call opcodes in the particular architecture's machine code. | 33 // branch/call opcodes in the particular architecture's machine code. |
34 class DisassemblerElf32 : public Disassembler { | 34 class DisassemblerElf32 : public Disassembler { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 | 99 |
100 ~DisassemblerElf32() override { } | 100 ~DisassemblerElf32() override { } |
101 | 101 |
102 // Disassembler interfaces. | 102 // Disassembler interfaces. |
103 RVA FileOffsetToRVA(FileOffset file_offset) const override; | 103 RVA FileOffsetToRVA(FileOffset file_offset) const override; |
104 FileOffset RVAToFileOffset(RVA rva) const override; | 104 FileOffset RVAToFileOffset(RVA rva) const override; |
105 RVA PointerToTargetRVA(const uint8_t* p) const override; | 105 RVA PointerToTargetRVA(const uint8_t* p) const override; |
106 ExecutableType kind() const override = 0; | 106 ExecutableType kind() const override = 0; |
107 uint64_t image_base() const override { return 0; } | 107 uint64_t image_base() const override { return 0; } |
108 bool ParseHeader() override; | 108 bool ParseHeader() override; |
109 bool Disassemble(AssemblyProgram* program) override; | |
110 | 109 |
111 virtual e_machine_values ElfEM() const = 0; | 110 virtual e_machine_values ElfEM() const = 0; |
112 | 111 |
113 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; | 112 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; |
114 | 113 |
115 // Converts an ELF relocation instruction into an RVA. | 114 // Converts an ELF relocation instruction into an RVA. |
116 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) | 115 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) |
117 const WARN_UNUSED_RESULT = 0; | 116 const WARN_UNUSED_RESULT = 0; |
118 | 117 |
119 // Public for unittests only | 118 // Public for unittests only |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 } | 162 } |
164 | 163 |
165 // Misc address space helpers | 164 // Misc address space helpers |
166 | 165 |
167 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, | 166 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, |
168 std::vector<FileOffset>* file_offsets) const; | 167 std::vector<FileOffset>* file_offsets) const; |
169 | 168 |
170 CheckBool RVAsToFileOffsets( | 169 CheckBool RVAsToFileOffsets( |
171 std::vector<std::unique_ptr<TypedRVA>>* typed_rvas) const; | 170 std::vector<std::unique_ptr<TypedRVA>>* typed_rvas) const; |
172 | 171 |
173 // Parsing code for Disassemble(). | 172 // Helpers for ParseFile(). |
174 | 173 |
175 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, | 174 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, |
176 InstructionReceptor* receptor) const | 175 InstructionReceptor* receptor) const |
177 WARN_UNUSED_RESULT = 0; | 176 WARN_UNUSED_RESULT = 0; |
178 | 177 |
179 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) | 178 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) |
180 WARN_UNUSED_RESULT = 0; | 179 WARN_UNUSED_RESULT = 0; |
181 | 180 |
182 CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT; | 181 CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT; |
183 | 182 |
184 // Extracts all rel32 TypedRVAs. Does not sort the result. | 183 // Extracts all rel32 TypedRVAs. Does not sort the result. |
185 CheckBool ParseRel32RelocsFromSections() WARN_UNUSED_RESULT; | 184 CheckBool ParseRel32RelocsFromSections() WARN_UNUSED_RESULT; |
186 | 185 |
187 // Disassembler interfaces. | 186 // Disassembler interfaces. |
187 bool ExtractAbs32Locations() override; | |
188 bool ExtractRel32Locations() override; | |
188 RvaVisitor* CreateAbs32TargetRvaVisitor() override; | 189 RvaVisitor* CreateAbs32TargetRvaVisitor() override; |
189 RvaVisitor* CreateRel32TargetRvaVisitor() override; | 190 RvaVisitor* CreateRel32TargetRvaVisitor() override; |
190 void RemoveUnusedRel32Locations(AssemblyProgram* program) override; | 191 void RemoveUnusedRel32Locations(AssemblyProgram* program) override; |
192 InstructionGenerator GetInstructionGenerator( | |
193 AssemblyProgram* program) override; | |
191 | 194 |
192 CheckBool ParseFile(AssemblyProgram* target, | 195 CheckBool ParseFile(AssemblyProgram* target, |
193 InstructionReceptor* receptor) const WARN_UNUSED_RESULT; | 196 InstructionReceptor* receptor) const WARN_UNUSED_RESULT; |
194 | 197 |
195 CheckBool ParseProgbitsSection( | 198 CheckBool ParseProgbitsSection( |
196 const Elf32_Shdr* section_header, | 199 const Elf32_Shdr* section_header, |
197 std::vector<FileOffset>::iterator* current_abs_offset, | 200 std::vector<FileOffset>::iterator* current_abs_offset, |
198 std::vector<FileOffset>::iterator end_abs_offset, | 201 std::vector<FileOffset>::iterator end_abs_offset, |
199 std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel, | 202 std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel, |
200 std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel, | 203 std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel, |
(...skipping 17 matching lines...) Expand all Loading... | |
218 // An ordering of |section_header_table_|, sorted by file offset. | 221 // An ordering of |section_header_table_|, sorted by file offset. |
219 std::vector<Elf32_Half> section_header_file_offset_order_; | 222 std::vector<Elf32_Half> section_header_file_offset_order_; |
220 | 223 |
221 const Elf32_Phdr* program_header_table_; | 224 const Elf32_Phdr* program_header_table_; |
222 Elf32_Half program_header_table_size_; | 225 Elf32_Half program_header_table_size_; |
223 | 226 |
224 // Pointer to string table containing section names. | 227 // Pointer to string table containing section names. |
225 const char* default_string_section_; | 228 const char* default_string_section_; |
226 size_t default_string_section_size_; | 229 size_t default_string_section_size_; |
227 | 230 |
228 // Sorted abs32 and reel32 RVAs. These are mutable because ParseFile() needs | 231 // Sorted abs32 RVAs. |
229 // to sort these by file offsets. | 232 std::vector<RVA> abs32_locations_; |
230 mutable std::vector<RVA> abs32_locations_; | 233 // Sorted rel32 RVAs. This is mutable because ParseFile() temporarily sorts |
234 // these by file offsets. | |
231 mutable std::vector<std::unique_ptr<TypedRVA>> rel32_locations_; | 235 mutable std::vector<std::unique_ptr<TypedRVA>> rel32_locations_; |
232 | 236 |
233 private: | 237 private: |
234 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); | 238 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); |
235 }; | 239 }; |
236 | 240 |
237 } // namespace courgette | 241 } // namespace courgette |
238 | 242 |
239 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ | 243 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ |
OLD | NEW |