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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 Elf32_Addr begin = segment_header->p_vaddr; | 208 Elf32_Addr begin = segment_header->p_vaddr; |
210 Elf32_Addr end = segment_header->p_vaddr + segment_header->p_memsz; | 209 Elf32_Addr end = segment_header->p_vaddr + segment_header->p_memsz; |
211 | 210 |
212 if (rva >= begin && rva < end) | 211 if (rva >= begin && rva < end) |
213 return true; | 212 return true; |
214 } | 213 } |
215 | 214 |
216 return false; | 215 return false; |
217 } | 216 } |
218 | 217 |
| 218 // static |
| 219 bool DisassemblerElf32::QuickDetect(const uint8_t* start, |
| 220 size_t length, |
| 221 e_machine_values elf_em) { |
| 222 if (length < sizeof(Elf32_Ehdr)) |
| 223 return false; |
| 224 |
| 225 const Elf32_Ehdr* header = reinterpret_cast<const Elf32_Ehdr*>(start); |
| 226 |
| 227 // Have magic for ELF header? |
| 228 if (header->e_ident[0] != 0x7f || header->e_ident[1] != 'E' || |
| 229 header->e_ident[2] != 'L' || header->e_ident[3] != 'F') |
| 230 return false; |
| 231 |
| 232 if (header->e_type != ET_EXEC && header->e_type != ET_DYN) |
| 233 return false; |
| 234 if (header->e_machine != elf_em) |
| 235 return false; |
| 236 if (header->e_version != 1) |
| 237 return false; |
| 238 if (header->e_shentsize != sizeof(Elf32_Shdr)) |
| 239 return false; |
| 240 |
| 241 return true; |
| 242 } |
| 243 |
219 bool DisassemblerElf32::UpdateLength() { | 244 bool DisassemblerElf32::UpdateLength() { |
220 Elf32_Off result = 0; | 245 Elf32_Off result = 0; |
221 | 246 |
222 // Find the end of the last section | 247 // Find the end of the last section |
223 for (Elf32_Half section_id = 0; section_id < SectionHeaderCount(); | 248 for (Elf32_Half section_id = 0; section_id < SectionHeaderCount(); |
224 ++section_id) { | 249 ++section_id) { |
225 const Elf32_Shdr* section_header = SectionHeader(section_id); | 250 const Elf32_Shdr* section_header = SectionHeader(section_id); |
226 | 251 |
227 if (section_header->sh_type == SHT_NOBITS) | 252 if (section_header->sh_type == SHT_NOBITS) |
228 continue; | 253 continue; |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 if (!ParseRel32RelocsFromSection(section_header)) | 624 if (!ParseRel32RelocsFromSection(section_header)) |
600 return false; | 625 return false; |
601 } | 626 } |
602 if (!found_rel32) | 627 if (!found_rel32) |
603 VLOG(1) << "Warning: Found no rel32 addresses. Missing .text section?"; | 628 VLOG(1) << "Warning: Found no rel32 addresses. Missing .text section?"; |
604 | 629 |
605 return true; | 630 return true; |
606 } | 631 } |
607 | 632 |
608 } // namespace courgette | 633 } // namespace courgette |
OLD | NEW |