| 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> | |
| 10 | 9 |
| 11 #include "base/bind.h" | 10 #include "base/bind.h" |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #include "courgette/assembly_program.h" | 12 #include "courgette/assembly_program.h" |
| 14 #include "courgette/courgette.h" | 13 #include "courgette/courgette.h" |
| 15 | 14 |
| 16 namespace courgette { | 15 namespace courgette { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 if (default_string_section_[default_string_section_size_ - 1] != '\0') | 156 if (default_string_section_[default_string_section_size_ - 1] != '\0') |
| 158 return Bad("String section does not terminate"); | 157 return Bad("String section does not terminate"); |
| 159 } | 158 } |
| 160 | 159 |
| 161 if (!UpdateLength()) | 160 if (!UpdateLength()) |
| 162 return Bad("Out of bounds section or segment"); | 161 return Bad("Out of bounds section or segment"); |
| 163 | 162 |
| 164 return Good(); | 163 return Good(); |
| 165 } | 164 } |
| 166 | 165 |
| 167 bool DisassemblerElf32::Disassemble(AssemblyProgram* target) { | 166 bool DisassemblerElf32::Disassemble(AssemblyProgram* program) { |
| 168 if (!ok()) | 167 if (!ok()) |
| 169 return false; | 168 return false; |
| 170 | 169 |
| 171 // The Image Base is always 0 for ELF Executables | |
| 172 target->set_image_base(0); | |
| 173 | |
| 174 if (!ParseAbs32Relocs()) | 170 if (!ParseAbs32Relocs()) |
| 175 return false; | 171 return false; |
| 176 | 172 |
| 177 if (!ParseRel32RelocsFromSections()) // Does not sort rel32 locations. | 173 if (!ParseRel32RelocsFromSections()) // Does not sort rel32 locations. |
| 178 return false; | 174 return false; |
| 179 | 175 |
| 180 PrecomputeLabels(target); | 176 PrecomputeLabels(program); |
| 181 RemoveUnusedRel32Locations(target); | 177 RemoveUnusedRel32Locations(program); |
| 182 | 178 |
| 183 if (!target->GenerateInstructions( | 179 if (!program->GenerateInstructions( |
| 184 base::Bind(&DisassemblerElf32::ParseFile, base::Unretained(this)))) { | 180 base::Bind(&DisassemblerElf32::ParseFile, base::Unretained(this)))) { |
| 185 return false; | 181 return false; |
| 186 } | 182 } |
| 187 | 183 |
| 188 // Finally sort rel32 locations. | 184 // Finally sort rel32 locations. |
| 189 std::sort(rel32_locations_.begin(), | 185 std::sort(rel32_locations_.begin(), |
| 190 rel32_locations_.end(), | 186 rel32_locations_.end(), |
| 191 TypedRVA::IsLessThanByRVA); | 187 TypedRVA::IsLessThanByRVA); |
| 192 DCHECK(rel32_locations_.empty() || | 188 DCHECK(rel32_locations_.empty() || |
| 193 rel32_locations_.back()->rva() != kUnassignedRVA); | 189 rel32_locations_.back()->rva() != kUnassignedRVA); |
| 194 | 190 |
| 195 target->DefaultAssignIndexes(); | 191 program->DefaultAssignIndexes(); |
| 196 return true; | 192 return true; |
| 197 } | 193 } |
| 198 | 194 |
| 199 CheckBool DisassemblerElf32::IsValidTargetRVA(RVA rva) const { | 195 CheckBool DisassemblerElf32::IsValidTargetRVA(RVA rva) const { |
| 200 if (rva == kUnassignedRVA) | 196 if (rva == kUnassignedRVA) |
| 201 return false; | 197 return false; |
| 202 | 198 |
| 203 // |rva| is valid if it's contained in any program segment. | 199 // |rva| is valid if it's contained in any program segment. |
| 204 for (Elf32_Half segment_id = 0; segment_id < ProgramSegmentHeaderCount(); | 200 for (Elf32_Half segment_id = 0; segment_id < ProgramSegmentHeaderCount(); |
| 205 ++segment_id) { | 201 ++segment_id) { |
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 if (!ParseRel32RelocsFromSection(section_header)) | 623 if (!ParseRel32RelocsFromSection(section_header)) |
| 628 return false; | 624 return false; |
| 629 } | 625 } |
| 630 if (!found_rel32) | 626 if (!found_rel32) |
| 631 VLOG(1) << "Warning: Found no rel32 addresses. Missing .text section?"; | 627 VLOG(1) << "Warning: Found no rel32 addresses. Missing .text section?"; |
| 632 | 628 |
| 633 return true; | 629 return true; |
| 634 } | 630 } |
| 635 | 631 |
| 636 } // namespace courgette | 632 } // namespace courgette |
| OLD | NEW |