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 RVA target_rva = rva + 4 + Read32LittleEndian(RVAToPointer(rva)); |
| 375 return program->FindRel32Label(target_rva) == nullptr; |
| 376 }; |
| 377 rel32_locations_.erase( |
| 378 std::remove_if(rel32_locations_.begin(), rel32_locations_.end(), cond), |
| 379 rel32_locations_.end()); |
| 380 } |
| 381 |
360 CheckBool DisassemblerWin32X64::ParseFile(AssemblyProgram* program) { | 382 CheckBool DisassemblerWin32X64::ParseFile(AssemblyProgram* program) { |
361 // 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. |
362 FileOffset file_offset = 0; | 384 FileOffset file_offset = 0; |
363 while (file_offset < length()) { | 385 while (file_offset < length()) { |
364 const Section* section = FindNextSection(file_offset); | 386 const Section* section = FindNextSection(file_offset); |
365 if (section == nullptr) { | 387 if (section == nullptr) { |
366 // No more sections. There should not be extra stuff following last | 388 // No more sections. There should not be extra stuff following last |
367 // section. | 389 // section. |
368 // ParseNonSectionFileRegion(file_offset, pe_info().length(), program); | 390 // ParseNonSectionFileRegion(file_offset, pe_info().length(), program); |
369 break; | 391 break; |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 } | 629 } |
608 | 630 |
609 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva) | 631 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva) |
610 ++abs32_pos; | 632 ++abs32_pos; |
611 | 633 |
612 if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) { | 634 if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) { |
613 RVA target_rva = PointerToTargetRVA(p); | 635 RVA target_rva = PointerToTargetRVA(p); |
614 DCHECK_NE(kNoRVA, target_rva); | 636 DCHECK_NE(kNoRVA, target_rva); |
615 // TODO(sra): target could be Label+offset. It is not clear how to guess | 637 // TODO(sra): target could be Label+offset. It is not clear how to guess |
616 // which it might be. We assume offset==0. | 638 // which it might be. We assume offset==0. |
617 if (!program->EmitAbs64(program->FindOrMakeAbs32Label(target_rva))) | 639 Label* label = program->FindAbs32Label(target_rva); |
| 640 DCHECK(label); |
| 641 if (!program->EmitAbs64(label)) |
618 return false; | 642 return false; |
619 p += 8; | 643 p += 8; |
620 continue; | 644 continue; |
621 } | 645 } |
622 | 646 |
623 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva) | 647 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva) |
624 ++rel32_pos; | 648 ++rel32_pos; |
625 | 649 |
626 if (rel32_pos != rel32_locations_.end() && *rel32_pos == current_rva) { | 650 if (rel32_pos != rel32_locations_.end() && *rel32_pos == current_rva) { |
627 RVA target_rva = current_rva + 4 + Read32LittleEndian(p); | 651 RVA target_rva = current_rva + 4 + Read32LittleEndian(p); |
628 if (!program->EmitRel32(program->FindOrMakeRel32Label(target_rva))) | 652 Label* label = program->FindRel32Label(target_rva); |
| 653 DCHECK(label); |
| 654 if (!program->EmitRel32(label)) |
629 return false; | 655 return false; |
630 p += 4; | 656 p += 4; |
631 continue; | 657 continue; |
632 } | 658 } |
633 | 659 |
634 if (incomplete_disassembly_) { | 660 if (incomplete_disassembly_) { |
635 if ((abs32_pos == abs32_locations_.end() || end_rva <= *abs32_pos) && | 661 if ((abs32_pos == abs32_locations_.end() || end_rva <= *abs32_pos) && |
636 (rel32_pos == rel32_locations_.end() || end_rva <= *rel32_pos) && | 662 (rel32_pos == rel32_locations_.end() || end_rva <= *rel32_pos) && |
637 (end_rva <= relocs_start_rva || current_rva >= relocs_start_rva)) { | 663 (end_rva <= relocs_start_rva || current_rva >= relocs_start_rva)) { |
638 // No more relocs in this section, don't bother encoding bytes. | 664 // 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); | 776 directory->size_ = static_cast<uint32_t>(size); |
751 return true; | 777 return true; |
752 } else { | 778 } else { |
753 directory->address_ = 0; | 779 directory->address_ = 0; |
754 directory->size_ = 0; | 780 directory->size_ = 0; |
755 return true; | 781 return true; |
756 } | 782 } |
757 } | 783 } |
758 | 784 |
759 } // namespace courgette | 785 } // namespace courgette |
OLD | NEW |