| 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_win32_x64.h" | 5 #include "courgette/disassembler_win32_x64.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 if (!ok()) | 245 if (!ok()) |
| 246 return false; | 246 return false; |
| 247 | 247 |
| 248 target->set_image_base(image_base()); | 248 target->set_image_base(image_base()); |
| 249 | 249 |
| 250 if (!ParseAbs32Relocs()) | 250 if (!ParseAbs32Relocs()) |
| 251 return false; | 251 return false; |
| 252 | 252 |
| 253 ParseRel32RelocsFromSections(); | 253 ParseRel32RelocsFromSections(); |
| 254 | 254 |
| 255 PrecomputeLabels(target); |
| 256 RemoveUnusedRel32Locations(target); |
| 257 |
| 255 if (!ParseFile(target)) | 258 if (!ParseFile(target)) |
| 256 return false; | 259 return false; |
| 257 | 260 |
| 258 target->DefaultAssignIndexes(); | 261 target->DefaultAssignIndexes(); |
| 259 | 262 |
| 260 return true; | 263 return true; |
| 261 } | 264 } |
| 262 | 265 |
| 263 //////////////////////////////////////////////////////////////////////////////// | 266 //////////////////////////////////////////////////////////////////////////////// |
| 264 | 267 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 | 353 |
| 351 std::string DisassemblerWin32X64::SectionName(const Section* section) { | 354 std::string DisassemblerWin32X64::SectionName(const Section* section) { |
| 352 if (section == nullptr) | 355 if (section == nullptr) |
| 353 return "<none>"; | 356 return "<none>"; |
| 354 char name[9]; | 357 char name[9]; |
| 355 memcpy(name, section->name, 8); | 358 memcpy(name, section->name, 8); |
| 356 name[8] = '\0'; // Ensure termination. | 359 name[8] = '\0'; // Ensure termination. |
| 357 return name; | 360 return name; |
| 358 } | 361 } |
| 359 | 362 |
| 363 RvaVisitor* DisassemblerWin32X64::CreateAbs32TargetRvaVisitor() { |
| 364 return new RvaVisitor_Abs32(abs32_locations_, *this); |
| 365 } |
| 366 |
| 367 RvaVisitor* DisassemblerWin32X64::CreateRel32TargetRvaVisitor() { |
| 368 return new RvaVisitor_Rel32(rel32_locations_, *this); |
| 369 } |
| 370 |
| 371 void DisassemblerWin32X64::RemoveUnusedRel32Locations( |
| 372 AssemblyProgram* program) { |
| 373 auto cond = [this, program](RVA rva) -> bool { |
| 374 // + 4 since offset is relative to start of next instruction. |
| 375 RVA target_rva = rva + 4 + Read32LittleEndian(RVAToPointer(rva)); |
| 376 return program->FindRel32Label(target_rva) == nullptr; |
| 377 }; |
| 378 rel32_locations_.erase( |
| 379 std::remove_if(rel32_locations_.begin(), rel32_locations_.end(), cond), |
| 380 rel32_locations_.end()); |
| 381 } |
| 382 |
| 360 CheckBool DisassemblerWin32X64::ParseFile(AssemblyProgram* program) { | 383 CheckBool DisassemblerWin32X64::ParseFile(AssemblyProgram* program) { |
| 361 // Walk all the bytes in the file, whether or not in a section. | 384 // Walk all the bytes in the file, whether or not in a section. |
| 362 FileOffset file_offset = 0; | 385 FileOffset file_offset = 0; |
| 363 while (file_offset < length()) { | 386 while (file_offset < length()) { |
| 364 const Section* section = FindNextSection(file_offset); | 387 const Section* section = FindNextSection(file_offset); |
| 365 if (section == nullptr) { | 388 if (section == nullptr) { |
| 366 // No more sections. There should not be extra stuff following last | 389 // No more sections. There should not be extra stuff following last |
| 367 // section. | 390 // section. |
| 368 // ParseNonSectionFileRegion(file_offset, pe_info().length(), program); | 391 // ParseNonSectionFileRegion(file_offset, pe_info().length(), program); |
| 369 break; | 392 break; |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 RVA rel32_rva = static_cast<RVA>(rel32 - adjust_pointer_to_rva); | 541 RVA rel32_rva = static_cast<RVA>(rel32 - adjust_pointer_to_rva); |
| 519 | 542 |
| 520 // Is there an abs32 reloc overlapping the candidate? | 543 // Is there an abs32 reloc overlapping the candidate? |
| 521 while (abs32_pos != abs32_locations_.end() && *abs32_pos < rel32_rva - 3) | 544 while (abs32_pos != abs32_locations_.end() && *abs32_pos < rel32_rva - 3) |
| 522 ++abs32_pos; | 545 ++abs32_pos; |
| 523 // Now: (*abs32_pos > rel32_rva - 4) i.e. the lowest addressed 4-byte | 546 // Now: (*abs32_pos > rel32_rva - 4) i.e. the lowest addressed 4-byte |
| 524 // region that could overlap rel32_rva. | 547 // region that could overlap rel32_rva. |
| 525 if (abs32_pos != abs32_locations_.end()) { | 548 if (abs32_pos != abs32_locations_.end()) { |
| 526 if (*abs32_pos < rel32_rva + 4) { | 549 if (*abs32_pos < rel32_rva + 4) { |
| 527 // Beginning of abs32 reloc is before end of rel32 reloc so they | 550 // Beginning of abs32 reloc is before end of rel32 reloc so they |
| 528 // overlap. Skip four bytes past the abs32 reloc. | 551 // overlap. Skip four bytes past the abs32 reloc. |
| 529 p += (*abs32_pos + 4) - current_rva; | 552 p += (*abs32_pos + 4) - current_rva; |
| 530 continue; | 553 continue; |
| 531 } | 554 } |
| 532 } | 555 } |
| 533 | 556 |
| 557 // + 4 since offset is relative to start of next instruction. |
| 534 RVA target_rva = rel32_rva + 4 + Read32LittleEndian(rel32); | 558 RVA target_rva = rel32_rva + 4 + Read32LittleEndian(rel32); |
| 535 // To be valid, rel32 target must be within image, and within this | 559 // To be valid, rel32 target must be within image, and within this |
| 536 // section. | 560 // section. |
| 537 if (target_rva < size_of_image_ && // Subsumes rva != kUnassignedRVA. | 561 if (target_rva < size_of_image_ && // Subsumes rva != kUnassignedRVA. |
| 538 (is_rip_relative || | 562 (is_rip_relative || |
| 539 (start_rva <= target_rva && target_rva < end_rva))) { | 563 (start_rva <= target_rva && target_rva < end_rva))) { |
| 540 rel32_locations_.push_back(rel32_rva); | 564 rel32_locations_.push_back(rel32_rva); |
| 541 #if COURGETTE_HISTOGRAM_TARGETS | 565 #if COURGETTE_HISTOGRAM_TARGETS |
| 542 ++rel32_target_rvas_[target_rva]; | 566 ++rel32_target_rvas_[target_rva]; |
| 543 #endif | 567 #endif |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 } | 631 } |
| 608 | 632 |
| 609 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva) | 633 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva) |
| 610 ++abs32_pos; | 634 ++abs32_pos; |
| 611 | 635 |
| 612 if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) { | 636 if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) { |
| 613 RVA target_rva = PointerToTargetRVA(p); | 637 RVA target_rva = PointerToTargetRVA(p); |
| 614 DCHECK_NE(kNoRVA, target_rva); | 638 DCHECK_NE(kNoRVA, target_rva); |
| 615 // TODO(sra): target could be Label+offset. It is not clear how to guess | 639 // TODO(sra): target could be Label+offset. It is not clear how to guess |
| 616 // which it might be. We assume offset==0. | 640 // which it might be. We assume offset==0. |
| 617 if (!program->EmitAbs64(program->FindOrMakeAbs32Label(target_rva))) | 641 Label* label = program->FindAbs32Label(target_rva); |
| 642 DCHECK(label); |
| 643 if (!program->EmitAbs64(label)) |
| 618 return false; | 644 return false; |
| 619 p += 8; | 645 p += 8; |
| 620 continue; | 646 continue; |
| 621 } | 647 } |
| 622 | 648 |
| 623 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva) | 649 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva) |
| 624 ++rel32_pos; | 650 ++rel32_pos; |
| 625 | 651 |
| 626 if (rel32_pos != rel32_locations_.end() && *rel32_pos == current_rva) { | 652 if (rel32_pos != rel32_locations_.end() && *rel32_pos == current_rva) { |
| 653 // + 4 since offset is relative to start of next instruction. |
| 627 RVA target_rva = current_rva + 4 + Read32LittleEndian(p); | 654 RVA target_rva = current_rva + 4 + Read32LittleEndian(p); |
| 628 if (!program->EmitRel32(program->FindOrMakeRel32Label(target_rva))) | 655 Label* label = program->FindRel32Label(target_rva); |
| 656 DCHECK(label); |
| 657 if (!program->EmitRel32(label)) |
| 629 return false; | 658 return false; |
| 630 p += 4; | 659 p += 4; |
| 631 continue; | 660 continue; |
| 632 } | 661 } |
| 633 | 662 |
| 634 if (incomplete_disassembly_) { | 663 if (incomplete_disassembly_) { |
| 635 if ((abs32_pos == abs32_locations_.end() || end_rva <= *abs32_pos) && | 664 if ((abs32_pos == abs32_locations_.end() || end_rva <= *abs32_pos) && |
| 636 (rel32_pos == rel32_locations_.end() || end_rva <= *rel32_pos) && | 665 (rel32_pos == rel32_locations_.end() || end_rva <= *rel32_pos) && |
| 637 (end_rva <= relocs_start_rva || current_rva >= relocs_start_rva)) { | 666 (end_rva <= relocs_start_rva || current_rva >= relocs_start_rva)) { |
| 638 // No more relocs in this section, don't bother encoding bytes. | 667 // No more relocs in this section, don't bother encoding bytes. |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 750 directory->size_ = static_cast<uint32_t>(size); | 779 directory->size_ = static_cast<uint32_t>(size); |
| 751 return true; | 780 return true; |
| 752 } else { | 781 } else { |
| 753 directory->address_ = 0; | 782 directory->address_ = 0; |
| 754 directory->size_ = 0; | 783 directory->size_ = 0; |
| 755 return true; | 784 return true; |
| 756 } | 785 } |
| 757 } | 786 } |
| 758 | 787 |
| 759 } // namespace courgette | 788 } // namespace courgette |
| OLD | NEW |