| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.h" | 5 #include "courgette/disassembler_win32.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 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 target->set_image_base(image_base()); | 230 target->set_image_base(image_base()); |
| 231 | 231 |
| 232 if (!ParseAbs32Relocs()) | 232 if (!ParseAbs32Relocs()) |
| 233 return false; | 233 return false; |
| 234 | 234 |
| 235 ParseRel32RelocsFromSections(); | 235 ParseRel32RelocsFromSections(); |
| 236 | 236 |
| 237 PrecomputeLabels(target); | 237 PrecomputeLabels(target); |
| 238 RemoveUnusedRel32Locations(target); | 238 RemoveUnusedRel32Locations(target); |
| 239 | 239 |
| 240 if (!ParseFile(target)) | 240 // Pass 1: Count the space needed to store instructions. |
| 241 InstructionCountReceptor* count_receptor = nullptr; |
| 242 if (!target->CreateInstructionCountReceptor(&count_receptor) || |
| 243 !ParseFile(target, count_receptor)) { |
| 241 return false; | 244 return false; |
| 245 } |
| 246 // Pass 2: Emit all instructions to preallocated buffer (uses Phase 1 count). |
| 247 InstructionStoreReceptor* store_receptor = nullptr; |
| 248 if (!target->CreateInstructionStoreReceptor(&store_receptor) || |
| 249 !ParseFile(target, store_receptor)) { |
| 250 return false; |
| 251 } |
| 242 | 252 |
| 243 target->DefaultAssignIndexes(); | 253 target->DefaultAssignIndexes(); |
| 244 | |
| 245 return true; | 254 return true; |
| 246 } | 255 } |
| 247 | 256 |
| 248 //////////////////////////////////////////////////////////////////////////////// | 257 //////////////////////////////////////////////////////////////////////////////// |
| 249 | 258 |
| 250 bool DisassemblerWin32::ParseRelocs(std::vector<RVA>* relocs) { | 259 bool DisassemblerWin32::ParseRelocs(std::vector<RVA>* relocs) { |
| 251 relocs->clear(); | 260 relocs->clear(); |
| 252 | 261 |
| 253 size_t relocs_size = base_relocation_table_.size_; | 262 size_t relocs_size = base_relocation_table_.size_; |
| 254 if (relocs_size == 0) | 263 if (relocs_size == 0) |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 auto cond = [this, program](RVA rva) -> bool { | 390 auto cond = [this, program](RVA rva) -> bool { |
| 382 // + 4 since offset is relative to start of next instruction. | 391 // + 4 since offset is relative to start of next instruction. |
| 383 RVA target_rva = rva + 4 + Read32LittleEndian(RVAToPointer(rva)); | 392 RVA target_rva = rva + 4 + Read32LittleEndian(RVAToPointer(rva)); |
| 384 return program->FindRel32Label(target_rva) == nullptr; | 393 return program->FindRel32Label(target_rva) == nullptr; |
| 385 }; | 394 }; |
| 386 rel32_locations_.erase( | 395 rel32_locations_.erase( |
| 387 std::remove_if(rel32_locations_.begin(), rel32_locations_.end(), cond), | 396 std::remove_if(rel32_locations_.begin(), rel32_locations_.end(), cond), |
| 388 rel32_locations_.end()); | 397 rel32_locations_.end()); |
| 389 } | 398 } |
| 390 | 399 |
| 391 CheckBool DisassemblerWin32::ParseFile(AssemblyProgram* program) { | 400 CheckBool DisassemblerWin32::ParseFile(AssemblyProgram* program, |
| 401 InstructionReceptor* receptor) const { |
| 392 // Walk all the bytes in the file, whether or not in a section. | 402 // Walk all the bytes in the file, whether or not in a section. |
| 393 FileOffset file_offset = 0; | 403 FileOffset file_offset = 0; |
| 394 while (file_offset < length()) { | 404 while (file_offset < length()) { |
| 395 const Section* section = FindNextSection(file_offset); | 405 const Section* section = FindNextSection(file_offset); |
| 396 if (section == nullptr) { | 406 if (section == nullptr) { |
| 397 // No more sections. There should not be extra stuff following last | 407 // No more sections. There should not be extra stuff following last |
| 398 // section. | 408 // section. |
| 399 // ParseNonSectionFileRegion(file_offset, pe_info().length(), program); | 409 // ParseNonSectionFileRegion(file_offset, pe_info().length(), receptor); |
| 400 break; | 410 break; |
| 401 } | 411 } |
| 402 if (file_offset < section->file_offset_of_raw_data) { | 412 if (file_offset < section->file_offset_of_raw_data) { |
| 403 FileOffset section_start_offset = section->file_offset_of_raw_data; | 413 FileOffset section_start_offset = section->file_offset_of_raw_data; |
| 404 if (!ParseNonSectionFileRegion(file_offset, section_start_offset, | 414 if (!ParseNonSectionFileRegion(file_offset, section_start_offset, |
| 405 program)) { | 415 receptor)) { |
| 406 return false; | 416 return false; |
| 407 } | 417 } |
| 408 | 418 |
| 409 file_offset = section_start_offset; | 419 file_offset = section_start_offset; |
| 410 } | 420 } |
| 411 FileOffset end = file_offset + section->size_of_raw_data; | 421 FileOffset end = file_offset + section->size_of_raw_data; |
| 412 if (!ParseFileRegion(section, file_offset, end, program)) | 422 if (!ParseFileRegion(section, file_offset, end, program, receptor)) |
| 413 return false; | 423 return false; |
| 414 file_offset = end; | 424 file_offset = end; |
| 415 } | 425 } |
| 416 | 426 |
| 417 #if COURGETTE_HISTOGRAM_TARGETS | 427 #if COURGETTE_HISTOGRAM_TARGETS |
| 418 HistogramTargets("abs32 relocs", abs32_target_rvas_); | 428 HistogramTargets("abs32 relocs", abs32_target_rvas_); |
| 419 HistogramTargets("rel32 relocs", rel32_target_rvas_); | 429 HistogramTargets("rel32 relocs", rel32_target_rvas_); |
| 420 #endif | 430 #endif |
| 421 | 431 |
| 422 return true; | 432 return true; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 ++rel32_iter; | 482 ++rel32_iter; |
| 473 } | 483 } |
| 474 } | 484 } |
| 475 VLOG(1) << "common " << common; | 485 VLOG(1) << "common " << common; |
| 476 #endif | 486 #endif |
| 477 } | 487 } |
| 478 | 488 |
| 479 CheckBool DisassemblerWin32::ParseNonSectionFileRegion( | 489 CheckBool DisassemblerWin32::ParseNonSectionFileRegion( |
| 480 FileOffset start_file_offset, | 490 FileOffset start_file_offset, |
| 481 FileOffset end_file_offset, | 491 FileOffset end_file_offset, |
| 482 AssemblyProgram* program) { | 492 InstructionReceptor* receptor) const { |
| 483 if (incomplete_disassembly_) | 493 if (incomplete_disassembly_) |
| 484 return true; | 494 return true; |
| 485 | 495 |
| 486 if (end_file_offset > start_file_offset) { | 496 if (end_file_offset > start_file_offset) { |
| 487 if (!program->EmitBytesInstruction(FileOffsetToPointer(start_file_offset), | 497 if (!receptor->EmitMultipleBytes(FileOffsetToPointer(start_file_offset), |
| 488 end_file_offset - start_file_offset)) { | 498 end_file_offset - start_file_offset)) { |
| 489 return false; | 499 return false; |
| 490 } | 500 } |
| 491 } | 501 } |
| 492 | 502 |
| 493 return true; | 503 return true; |
| 494 } | 504 } |
| 495 | 505 |
| 496 CheckBool DisassemblerWin32::ParseFileRegion(const Section* section, | 506 CheckBool DisassemblerWin32::ParseFileRegion( |
| 497 FileOffset start_file_offset, | 507 const Section* section, |
| 498 FileOffset end_file_offset, | 508 FileOffset start_file_offset, |
| 499 AssemblyProgram* program) { | 509 FileOffset end_file_offset, |
| 510 AssemblyProgram* program, |
| 511 InstructionReceptor* receptor) const { |
| 500 RVA relocs_start_rva = base_relocation_table().address_; | 512 RVA relocs_start_rva = base_relocation_table().address_; |
| 501 | 513 |
| 502 const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset); | 514 const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset); |
| 503 const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset); | 515 const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset); |
| 504 | 516 |
| 505 RVA start_rva = FileOffsetToRVA(start_file_offset); | 517 RVA start_rva = FileOffsetToRVA(start_file_offset); |
| 506 RVA end_rva = start_rva + section->virtual_size; | 518 RVA end_rva = start_rva + section->virtual_size; |
| 507 const int kVAWidth = AbsVAWidth(); | 519 const int kVAWidth = AbsVAWidth(); |
| 508 | 520 |
| 509 // Quick way to convert from Pointer to RVA within a single Section is to | 521 // Quick way to convert from Pointer to RVA within a single Section is to |
| 510 // subtract 'pointer_to_rva'. | 522 // subtract 'pointer_to_rva'. |
| 511 const uint8_t* const adjust_pointer_to_rva = start_pointer - start_rva; | 523 const uint8_t* const adjust_pointer_to_rva = start_pointer - start_rva; |
| 512 | 524 |
| 513 std::vector<RVA>::iterator rel32_pos = rel32_locations_.begin(); | 525 std::vector<RVA>::const_iterator rel32_pos = rel32_locations_.begin(); |
| 514 std::vector<RVA>::iterator abs32_pos = abs32_locations_.begin(); | 526 std::vector<RVA>::const_iterator abs32_pos = abs32_locations_.begin(); |
| 515 | 527 |
| 516 if (!program->EmitOriginInstruction(start_rva)) | 528 if (!receptor->EmitOrigin(start_rva)) |
| 517 return false; | 529 return false; |
| 518 | 530 |
| 519 const uint8_t* p = start_pointer; | 531 const uint8_t* p = start_pointer; |
| 520 | 532 |
| 521 while (p < end_pointer) { | 533 while (p < end_pointer) { |
| 522 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); | 534 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); |
| 523 | 535 |
| 524 // The base relocation table is usually in the .relocs section, but it could | 536 // The base relocation table is usually in the .relocs section, but it could |
| 525 // actually be anywhere. Make sure we skip it because we will regenerate it | 537 // actually be anywhere. Make sure we skip it because we will regenerate it |
| 526 // during assembly. | 538 // during assembly. |
| 527 if (current_rva == relocs_start_rva) { | 539 if (current_rva == relocs_start_rva) { |
| 528 if (!program->EmitPeRelocsInstruction()) | 540 if (!receptor->EmitPeRelocs()) |
| 529 return false; | 541 return false; |
| 530 uint32_t relocs_size = base_relocation_table().size_; | 542 uint32_t relocs_size = base_relocation_table().size_; |
| 531 if (relocs_size) { | 543 if (relocs_size) { |
| 532 p += relocs_size; | 544 p += relocs_size; |
| 533 continue; | 545 continue; |
| 534 } | 546 } |
| 535 } | 547 } |
| 536 | 548 |
| 537 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva) | 549 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva) |
| 538 ++abs32_pos; | 550 ++abs32_pos; |
| 539 | 551 |
| 540 if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) { | 552 if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) { |
| 541 RVA target_rva = PointerToTargetRVA(p); | 553 RVA target_rva = PointerToTargetRVA(p); |
| 542 DCHECK_NE(kNoRVA, target_rva); | 554 DCHECK_NE(kNoRVA, target_rva); |
| 543 // TODO(sra): target could be Label+offset. It is not clear how to guess | 555 // TODO(sra): target could be Label+offset. It is not clear how to guess |
| 544 // which it might be. We assume offset==0. | 556 // which it might be. We assume offset==0. |
| 545 Label* label = program->FindAbs32Label(target_rva); | 557 Label* label = program->FindAbs32Label(target_rva); |
| 546 DCHECK(label); | 558 DCHECK(label); |
| 547 if (!EmitAbs(label, program)) | 559 if (!EmitAbs(label, receptor)) |
| 548 return false; | 560 return false; |
| 549 p += kVAWidth; | 561 p += kVAWidth; |
| 550 continue; | 562 continue; |
| 551 } | 563 } |
| 552 | 564 |
| 553 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva) | 565 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva) |
| 554 ++rel32_pos; | 566 ++rel32_pos; |
| 555 | 567 |
| 556 if (rel32_pos != rel32_locations_.end() && *rel32_pos == current_rva) { | 568 if (rel32_pos != rel32_locations_.end() && *rel32_pos == current_rva) { |
| 557 // + 4 since offset is relative to start of next instruction. | 569 // + 4 since offset is relative to start of next instruction. |
| 558 RVA target_rva = current_rva + 4 + Read32LittleEndian(p); | 570 RVA target_rva = current_rva + 4 + Read32LittleEndian(p); |
| 559 Label* label = program->FindRel32Label(target_rva); | 571 Label* label = program->FindRel32Label(target_rva); |
| 560 DCHECK(label); | 572 DCHECK(label); |
| 561 if (!program->EmitRel32(label)) | 573 if (!receptor->EmitRel32(label)) |
| 562 return false; | 574 return false; |
| 563 p += 4; | 575 p += 4; |
| 564 continue; | 576 continue; |
| 565 } | 577 } |
| 566 | 578 |
| 567 if (incomplete_disassembly_) { | 579 if (incomplete_disassembly_) { |
| 568 if ((abs32_pos == abs32_locations_.end() || end_rva <= *abs32_pos) && | 580 if ((abs32_pos == abs32_locations_.end() || end_rva <= *abs32_pos) && |
| 569 (rel32_pos == rel32_locations_.end() || end_rva <= *rel32_pos) && | 581 (rel32_pos == rel32_locations_.end() || end_rva <= *rel32_pos) && |
| 570 (end_rva <= relocs_start_rva || current_rva >= relocs_start_rva)) { | 582 (end_rva <= relocs_start_rva || current_rva >= relocs_start_rva)) { |
| 571 // No more relocs in this section, don't bother encoding bytes. | 583 // No more relocs in this section, don't bother encoding bytes. |
| 572 break; | 584 break; |
| 573 } | 585 } |
| 574 } | 586 } |
| 575 | 587 |
| 576 if (!program->EmitByteInstruction(*p)) | 588 if (!receptor->EmitSingleByte(*p)) |
| 577 return false; | 589 return false; |
| 578 p += 1; | 590 p += 1; |
| 579 } | 591 } |
| 580 | 592 |
| 581 return true; | 593 return true; |
| 582 } | 594 } |
| 583 | 595 |
| 584 #if COURGETTE_HISTOGRAM_TARGETS | 596 #if COURGETTE_HISTOGRAM_TARGETS |
| 585 // Histogram is printed to std::cout. It is purely for debugging the algorithm | 597 // Histogram is printed to std::cout. It is purely for debugging the algorithm |
| 586 // and is only enabled manually in 'exploration' builds. I don't want to add | 598 // and is only enabled manually in 'exploration' builds. I don't want to add |
| 587 // command-line configuration for this feature because this code has to be | 599 // command-line configuration for this feature because this code has to be |
| 588 // small, which means compiled-out. | 600 // small, which means compiled-out. |
| 589 void DisassemblerWin32::HistogramTargets(const char* kind, | 601 void DisassemblerWin32::HistogramTargets(const char* kind, |
| 590 const std::map<RVA, int>& map) { | 602 const std::map<RVA, int>& map) const { |
| 591 int total = 0; | 603 int total = 0; |
| 592 std::map<int, std::vector<RVA>> h; | 604 std::map<int, std::vector<RVA>> h; |
| 593 for (std::map<RVA, int>::const_iterator p = map.begin(); p != map.end(); | 605 for (std::map<RVA, int>::const_iterator p = map.begin(); p != map.end(); |
| 594 ++p) { | 606 ++p) { |
| 595 h[p->second].push_back(p->first); | 607 h[p->second].push_back(p->first); |
| 596 total += p->second; | 608 total += p->second; |
| 597 } | 609 } |
| 598 | 610 |
| 599 std::cout << total << " " << kind << " to " << map.size() << " unique targets" | 611 std::cout << total << " " << kind << " to " << map.size() << " unique targets" |
| 600 << std::endl; | 612 << std::endl; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 directory->size_ = static_cast<uint32_t>(size); | 689 directory->size_ = static_cast<uint32_t>(size); |
| 678 return true; | 690 return true; |
| 679 } else { | 691 } else { |
| 680 directory->address_ = 0; | 692 directory->address_ = 0; |
| 681 directory->size_ = 0; | 693 directory->size_ = 0; |
| 682 return true; | 694 return true; |
| 683 } | 695 } |
| 684 } | 696 } |
| 685 | 697 |
| 686 } // namespace courgette | 698 } // namespace courgette |
| OLD | NEW |