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