| 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 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "courgette/assembly_program.h" | 10 #include "courgette/assembly_program.h" |
| 11 #include "courgette/courgette.h" | 11 #include "courgette/courgette.h" |
| 12 | 12 |
| 13 namespace courgette { | 13 namespace courgette { |
| 14 | 14 |
| 15 namespace { |
| 16 |
| 17 // Initializes |section_header_table| by copying |section_header_table_size| |
| 18 // entries from |section_header_table_raw|, and sorting by |sh_offset|. |
| 19 void SortSectionHeader(const Elf32_Shdr* section_header_table_raw, |
| 20 Elf32_Half section_header_table_size, |
| 21 std::vector<Elf32_Shdr>* section_header_table) { |
| 22 section_header_table->assign(section_header_table_raw, |
| 23 section_header_table_raw + section_header_table_size); |
| 24 auto comp = [](const Elf32_Shdr& header1, const Elf32_Shdr& header2) { |
| 25 return header1.sh_offset < header2.sh_offset; |
| 26 }; |
| 27 std::stable_sort( |
| 28 section_header_table->begin(), section_header_table->end(), comp); |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 15 DisassemblerElf32::DisassemblerElf32(const void* start, size_t length) | 33 DisassemblerElf32::DisassemblerElf32(const void* start, size_t length) |
| 16 : Disassembler(start, length), | 34 : Disassembler(start, length), |
| 17 header_(nullptr), | 35 header_(nullptr), |
| 18 section_header_table_(nullptr), | |
| 19 section_header_table_size_(0), | 36 section_header_table_size_(0), |
| 20 program_header_table_(nullptr), | 37 program_header_table_(nullptr), |
| 21 program_header_table_size_(0), | 38 program_header_table_size_(0), |
| 22 default_string_section_(nullptr) { | 39 default_string_section_(nullptr) { |
| 23 } | 40 } |
| 24 | 41 |
| 25 RVA DisassemblerElf32::FileOffsetToRVA(FileOffset offset) const { | 42 RVA DisassemblerElf32::FileOffsetToRVA(FileOffset offset) const { |
| 26 // File offsets can be 64-bit values, but we are dealing with 32-bit | 43 // File offsets can be 64-bit values, but we are dealing with 32-bit |
| 27 // executables and so only need to support 32-bit file sizes. | 44 // executables and so only need to support 32-bit file sizes. |
| 28 uint32_t offset32 = static_cast<uint32_t>(offset); | 45 uint32_t offset32 = static_cast<uint32_t>(offset); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 105 |
| 89 if (header_->e_version != 1) | 106 if (header_->e_version != 1) |
| 90 return Bad("Unknown file version"); | 107 return Bad("Unknown file version"); |
| 91 | 108 |
| 92 if (header_->e_shentsize != sizeof(Elf32_Shdr)) | 109 if (header_->e_shentsize != sizeof(Elf32_Shdr)) |
| 93 return Bad("Unexpected section header size"); | 110 return Bad("Unexpected section header size"); |
| 94 | 111 |
| 95 if (!IsArrayInBounds(header_->e_shoff, header_->e_shnum, sizeof(Elf32_Shdr))) | 112 if (!IsArrayInBounds(header_->e_shoff, header_->e_shnum, sizeof(Elf32_Shdr))) |
| 96 return Bad("Out of bounds section header table"); | 113 return Bad("Out of bounds section header table"); |
| 97 | 114 |
| 98 section_header_table_ = reinterpret_cast<const Elf32_Shdr*>( | 115 const Elf32_Shdr* section_header_table_raw = |
| 99 FileOffsetToPointer(header_->e_shoff)); | 116 reinterpret_cast<const Elf32_Shdr*>( |
| 117 FileOffsetToPointer(header_->e_shoff)); |
| 100 section_header_table_size_ = header_->e_shnum; | 118 section_header_table_size_ = header_->e_shnum; |
| 119 SortSectionHeader(section_header_table_raw, section_header_table_size_, |
| 120 §ion_header_table_); |
| 101 | 121 |
| 102 if (!IsArrayInBounds(header_->e_phoff, header_->e_phnum, sizeof(Elf32_Phdr))) | 122 if (!IsArrayInBounds(header_->e_phoff, header_->e_phnum, sizeof(Elf32_Phdr))) |
| 103 return Bad("Out of bounds program header table"); | 123 return Bad("Out of bounds program header table"); |
| 104 | 124 |
| 105 program_header_table_ = reinterpret_cast<const Elf32_Phdr*>( | 125 program_header_table_ = reinterpret_cast<const Elf32_Phdr*>( |
| 106 FileOffsetToPointer(header_->e_phoff)); | 126 FileOffsetToPointer(header_->e_phoff)); |
| 107 program_header_table_size_ = header_->e_phnum; | 127 program_header_table_size_ = header_->e_phnum; |
| 108 | 128 |
| 109 if (header_->e_shstrndx >= header_->e_shnum) | 129 if (header_->e_shstrndx >= header_->e_shnum) |
| 110 return Bad("Out of bounds string section index"); | 130 return Bad("Out of bounds string section index"); |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 | 499 |
| 480 std::sort(rel32_locations_.begin(), | 500 std::sort(rel32_locations_.begin(), |
| 481 rel32_locations_.end(), | 501 rel32_locations_.end(), |
| 482 TypedRVA::IsLessThan); | 502 TypedRVA::IsLessThan); |
| 483 DCHECK(rel32_locations_.empty() || | 503 DCHECK(rel32_locations_.empty() || |
| 484 rel32_locations_.back()->rva() != kUnassignedRVA); | 504 rel32_locations_.back()->rva() != kUnassignedRVA); |
| 485 return true; | 505 return true; |
| 486 } | 506 } |
| 487 | 507 |
| 488 } // namespace courgette | 508 } // namespace courgette |
| OLD | NEW |