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 #include "courgette/disassembler_elf_32.h" | 5 #include "courgette/disassembler_elf_32.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... | |
36 | 36 |
37 DisassemblerElf32::Elf32RvaVisitor_Rel32::Elf32RvaVisitor_Rel32( | 37 DisassemblerElf32::Elf32RvaVisitor_Rel32::Elf32RvaVisitor_Rel32( |
38 const std::vector<std::unique_ptr<TypedRVA>>& rva_locations) | 38 const std::vector<std::unique_ptr<TypedRVA>>& rva_locations) |
39 : VectorRvaVisitor<std::unique_ptr<TypedRVA>>(rva_locations) { | 39 : VectorRvaVisitor<std::unique_ptr<TypedRVA>>(rva_locations) { |
40 } | 40 } |
41 | 41 |
42 RVA DisassemblerElf32::Elf32RvaVisitor_Rel32::Get() const { | 42 RVA DisassemblerElf32::Elf32RvaVisitor_Rel32::Get() const { |
43 return (*it_)->rva() + (*it_)->relative_target(); | 43 return (*it_)->rva() + (*it_)->relative_target(); |
44 } | 44 } |
45 | 45 |
46 DisassemblerElf32::DisassemblerElf32(const void* start, size_t length) | 46 DisassemblerElf32::DisassemblerElf32(const uint8_t* start, size_t length) |
47 : Disassembler(start, length), | 47 : Disassembler(start, length), |
48 header_(nullptr), | 48 header_(nullptr), |
49 section_header_table_size_(0), | 49 section_header_table_size_(0), |
50 program_header_table_(nullptr), | 50 program_header_table_(nullptr), |
51 program_header_table_size_(0), | 51 program_header_table_size_(0), |
52 default_string_section_(nullptr) { | 52 default_string_section_(nullptr) {} |
53 } | |
54 | 53 |
55 RVA DisassemblerElf32::FileOffsetToRVA(FileOffset offset) const { | 54 RVA DisassemblerElf32::FileOffsetToRVA(FileOffset offset) const { |
56 // File offsets can be 64-bit values, but we are dealing with 32-bit | 55 // File offsets can be 64-bit values, but we are dealing with 32-bit |
57 // executables and so only need to support 32-bit file sizes. | 56 // executables and so only need to support 32-bit file sizes. |
58 uint32_t offset32 = static_cast<uint32_t>(offset); | 57 uint32_t offset32 = static_cast<uint32_t>(offset); |
59 | 58 |
60 // Visit section headers ordered by file offset. | 59 // Visit section headers ordered by file offset. |
61 for (Elf32_Half section_id : section_header_file_offset_order_) { | 60 for (Elf32_Half section_id : section_header_file_offset_order_) { |
62 const Elf32_Shdr* section_header = SectionHeader(section_id); | 61 const Elf32_Shdr* section_header = SectionHeader(section_id); |
63 // These can appear to have a size in the file, but don't. | 62 // These can appear to have a size in the file, but don't. |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 name->clear(); | 266 name->clear(); |
268 } else { | 267 } else { |
269 if (string_pos >= default_string_section_size_) | 268 if (string_pos >= default_string_section_size_) |
270 return false; | 269 return false; |
271 // Safe because string section must terminate with null. | 270 // Safe because string section must terminate with null. |
272 *name = default_string_section_ + string_pos; | 271 *name = default_string_section_ + string_pos; |
273 } | 272 } |
274 return true; | 273 return true; |
275 } | 274 } |
276 | 275 |
276 bool DisassemblerElf32::QuickDetect(const uint8_t* start, | |
huangs
2016/06/13 18:30:28
NIT:
// static
Also, move to Line 218 to match .h
etiennep
2016/06/14 21:16:37
Done.
| |
277 size_t length, | |
278 e_machine_values elf_em) { | |
279 if (length < sizeof(Elf32_Ehdr)) | |
280 return false; | |
281 | |
282 const Elf32_Ehdr* header = reinterpret_cast<const Elf32_Ehdr*>(start); | |
283 | |
284 // Have magic for ELF header? | |
285 if (header->e_ident[0] != 0x7f || header->e_ident[1] != 'E' || | |
286 header->e_ident[2] != 'L' || header->e_ident[3] != 'F') | |
287 return false; | |
288 | |
289 if (header->e_type != ET_EXEC && header->e_type != ET_DYN) | |
290 return false; | |
291 if (header->e_machine != elf_em) | |
292 return false; | |
293 if (header->e_version != 1) | |
294 return false; | |
295 if (header->e_shentsize != sizeof(Elf32_Shdr)) | |
296 return false; | |
297 | |
298 return true; | |
299 } | |
300 | |
277 CheckBool DisassemblerElf32::RVAsToFileOffsets( | 301 CheckBool DisassemblerElf32::RVAsToFileOffsets( |
278 const std::vector<RVA>& rvas, | 302 const std::vector<RVA>& rvas, |
279 std::vector<FileOffset>* file_offsets) { | 303 std::vector<FileOffset>* file_offsets) { |
280 file_offsets->clear(); | 304 file_offsets->clear(); |
281 file_offsets->reserve(rvas.size()); | 305 file_offsets->reserve(rvas.size()); |
282 for (RVA rva : rvas) { | 306 for (RVA rva : rvas) { |
283 FileOffset file_offset = RVAToFileOffset(rva); | 307 FileOffset file_offset = RVAToFileOffset(rva); |
284 if (file_offset == kNoFileOffset) | 308 if (file_offset == kNoFileOffset) |
285 return false; | 309 return false; |
286 file_offsets->push_back(file_offset); | 310 file_offsets->push_back(file_offset); |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
599 if (!ParseRel32RelocsFromSection(section_header)) | 623 if (!ParseRel32RelocsFromSection(section_header)) |
600 return false; | 624 return false; |
601 } | 625 } |
602 if (!found_rel32) | 626 if (!found_rel32) |
603 VLOG(1) << "Warning: Found no rel32 addresses. Missing .text section?"; | 627 VLOG(1) << "Warning: Found no rel32 addresses. Missing .text section?"; |
604 | 628 |
605 return true; | 629 return true; |
606 } | 630 } |
607 | 631 |
608 } // namespace courgette | 632 } // namespace courgette |
OLD | NEW |