| 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 <utility> |
| 8 | 9 |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "courgette/assembly_program.h" | 11 #include "courgette/assembly_program.h" |
| 11 #include "courgette/courgette.h" | 12 #include "courgette/courgette.h" |
| 12 | 13 |
| 13 namespace courgette { | 14 namespace courgette { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 // Sorts |section_headers| by file offset and stores the resulting permutation | 18 // Sorts |section_headers| by file offset and stores the resulting permutation |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 for (RVA rva : rvas) { | 267 for (RVA rva : rvas) { |
| 267 FileOffset file_offset = RVAToFileOffset(rva); | 268 FileOffset file_offset = RVAToFileOffset(rva); |
| 268 if (file_offset == kNoFileOffset) | 269 if (file_offset == kNoFileOffset) |
| 269 return false; | 270 return false; |
| 270 file_offsets->push_back(file_offset); | 271 file_offsets->push_back(file_offset); |
| 271 } | 272 } |
| 272 return true; | 273 return true; |
| 273 } | 274 } |
| 274 | 275 |
| 275 CheckBool DisassemblerElf32::RVAsToFileOffsets( | 276 CheckBool DisassemblerElf32::RVAsToFileOffsets( |
| 276 ScopedVector<TypedRVA>* typed_rvas) { | 277 std::vector<std::unique_ptr<TypedRVA>>* typed_rvas) { |
| 277 for (TypedRVA* typed_rva : *typed_rvas) { | 278 for (auto& typed_rva : *typed_rvas) { |
| 278 FileOffset file_offset = RVAToFileOffset(typed_rva->rva()); | 279 FileOffset file_offset = RVAToFileOffset(typed_rva->rva()); |
| 279 if (file_offset == kNoFileOffset) | 280 if (file_offset == kNoFileOffset) |
| 280 return false; | 281 return false; |
| 281 typed_rva->set_file_offset(file_offset); | 282 typed_rva->set_file_offset(file_offset); |
| 282 } | 283 } |
| 283 return true; | 284 return true; |
| 284 } | 285 } |
| 285 | 286 |
| 286 CheckBool DisassemblerElf32::ParseFile(AssemblyProgram* program) { | 287 CheckBool DisassemblerElf32::ParseFile(AssemblyProgram* program) { |
| 287 // Walk all the bytes in the file, whether or not in a section. | 288 // Walk all the bytes in the file, whether or not in a section. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 298 | 299 |
| 299 if (!RVAsToFileOffsets(&rel32_locations_)) | 300 if (!RVAsToFileOffsets(&rel32_locations_)) |
| 300 return false; | 301 return false; |
| 301 std::sort(rel32_locations_.begin(), | 302 std::sort(rel32_locations_.begin(), |
| 302 rel32_locations_.end(), | 303 rel32_locations_.end(), |
| 303 TypedRVA::IsLessThanByFileOffset); | 304 TypedRVA::IsLessThanByFileOffset); |
| 304 | 305 |
| 305 std::vector<FileOffset>::iterator current_abs_offset = abs_offsets.begin(); | 306 std::vector<FileOffset>::iterator current_abs_offset = abs_offsets.begin(); |
| 306 std::vector<FileOffset>::iterator end_abs_offset = abs_offsets.end(); | 307 std::vector<FileOffset>::iterator end_abs_offset = abs_offsets.end(); |
| 307 | 308 |
| 308 ScopedVector<TypedRVA>::iterator current_rel = rel32_locations_.begin(); | 309 std::vector<std::unique_ptr<TypedRVA>>::iterator current_rel = |
| 309 ScopedVector<TypedRVA>::iterator end_rel = rel32_locations_.end(); | 310 rel32_locations_.begin(); |
| 311 std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel = |
| 312 rel32_locations_.end(); |
| 310 | 313 |
| 311 // Visit section headers ordered by file offset. | 314 // Visit section headers ordered by file offset. |
| 312 for (Elf32_Half section_id : section_header_file_offset_order_) { | 315 for (Elf32_Half section_id : section_header_file_offset_order_) { |
| 313 const Elf32_Shdr* section_header = SectionHeader(section_id); | 316 const Elf32_Shdr* section_header = SectionHeader(section_id); |
| 314 | 317 |
| 315 if (section_header->sh_type == SHT_NOBITS) | 318 if (section_header->sh_type == SHT_NOBITS) |
| 316 continue; | 319 continue; |
| 317 | 320 |
| 318 if (!ParseSimpleRegion(file_offset, section_header->sh_offset, program)) | 321 if (!ParseSimpleRegion(file_offset, section_header->sh_offset, program)) |
| 319 return false; | 322 return false; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 return false; | 370 return false; |
| 368 | 371 |
| 369 // Make certain we consume all of the relocations as expected | 372 // Make certain we consume all of the relocations as expected |
| 370 return (current_abs_offset == end_abs_offset); | 373 return (current_abs_offset == end_abs_offset); |
| 371 } | 374 } |
| 372 | 375 |
| 373 CheckBool DisassemblerElf32::ParseProgbitsSection( | 376 CheckBool DisassemblerElf32::ParseProgbitsSection( |
| 374 const Elf32_Shdr* section_header, | 377 const Elf32_Shdr* section_header, |
| 375 std::vector<FileOffset>::iterator* current_abs_offset, | 378 std::vector<FileOffset>::iterator* current_abs_offset, |
| 376 std::vector<FileOffset>::iterator end_abs_offset, | 379 std::vector<FileOffset>::iterator end_abs_offset, |
| 377 ScopedVector<TypedRVA>::iterator* current_rel, | 380 std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel, |
| 378 ScopedVector<TypedRVA>::iterator end_rel, | 381 std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel, |
| 379 AssemblyProgram* program) { | 382 AssemblyProgram* program) { |
| 380 // Walk all the bytes in the file, whether or not in a section. | 383 // Walk all the bytes in the file, whether or not in a section. |
| 381 FileOffset file_offset = section_header->sh_offset; | 384 FileOffset file_offset = section_header->sh_offset; |
| 382 FileOffset section_end = section_header->sh_offset + section_header->sh_size; | 385 FileOffset section_end = section_header->sh_offset + section_header->sh_size; |
| 383 | 386 |
| 384 Elf32_Addr origin = section_header->sh_addr; | 387 Elf32_Addr origin = section_header->sh_addr; |
| 385 FileOffset origin_offset = section_header->sh_offset; | 388 FileOffset origin_offset = section_header->sh_offset; |
| 386 if (!program->EmitOriginInstruction(origin)) | 389 if (!program->EmitOriginInstruction(origin)) |
| 387 return false; | 390 return false; |
| 388 | 391 |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 if (!ParseRel32RelocsFromSection(section_header)) | 551 if (!ParseRel32RelocsFromSection(section_header)) |
| 549 return false; | 552 return false; |
| 550 } | 553 } |
| 551 if (!found_rel32) | 554 if (!found_rel32) |
| 552 VLOG(1) << "Warning: Found no rel32 addresses. Missing .text section?"; | 555 VLOG(1) << "Warning: Found no rel32 addresses. Missing .text section?"; |
| 553 | 556 |
| 554 return true; | 557 return true; |
| 555 } | 558 } |
| 556 | 559 |
| 557 } // namespace courgette | 560 } // namespace courgette |
| OLD | NEW |