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