Chromium Code Reviews| 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/logging.h" | 12 #include "base/logging.h" |
| 13 #include "courgette/assembly_program.h" | 13 #include "courgette/assembly_program.h" |
| 14 #include "courgette/courgette.h" | 14 #include "courgette/courgette.h" |
| 15 | 15 |
| 16 #if COURGETTE_HISTOGRAM_TARGETS | 16 #if COURGETTE_HISTOGRAM_TARGETS |
| 17 #include <iostream> | 17 #include <iostream> |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 namespace courgette { | 20 namespace courgette { |
| 21 | 21 |
| 22 DisassemblerWin32::DisassemblerWin32(const void* start, size_t length) | 22 DisassemblerWin32::DisassemblerWin32(const uint8_t* start, size_t length) |
| 23 : Disassembler(start, length) {} | 23 : Disassembler(start, length) {} |
| 24 | 24 |
| 25 RVA DisassemblerWin32::FileOffsetToRVA(FileOffset file_offset) const { | 25 RVA DisassemblerWin32::FileOffsetToRVA(FileOffset file_offset) const { |
| 26 for (int i = 0; i < number_of_sections_; ++i) { | 26 for (int i = 0; i < number_of_sections_; ++i) { |
| 27 const Section* section = §ions_[i]; | 27 const Section* section = §ions_[i]; |
| 28 if (file_offset >= section->file_offset_of_raw_data) { | 28 if (file_offset >= section->file_offset_of_raw_data) { |
| 29 FileOffset offset_in_section = | 29 FileOffset offset_in_section = |
| 30 file_offset - section->file_offset_of_raw_data; | 30 file_offset - section->file_offset_of_raw_data; |
| 31 if (offset_in_section < section->size_of_raw_data) | 31 if (offset_in_section < section->size_of_raw_data) |
| 32 return static_cast<RVA>(section->virtual_address + offset_in_section); | 32 return static_cast<RVA>(section->virtual_address + offset_in_section); |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 | 331 |
| 332 std::string DisassemblerWin32::SectionName(const Section* section) { | 332 std::string DisassemblerWin32::SectionName(const Section* section) { |
| 333 if (section == nullptr) | 333 if (section == nullptr) |
| 334 return "<none>"; | 334 return "<none>"; |
| 335 char name[9]; | 335 char name[9]; |
| 336 memcpy(name, section->name, 8); | 336 memcpy(name, section->name, 8); |
| 337 name[8] = '\0'; // Ensure termination. | 337 name[8] = '\0'; // Ensure termination. |
| 338 return name; | 338 return name; |
| 339 } | 339 } |
| 340 | 340 |
| 341 bool DisassemblerWin32::QuickDetect(const uint8_t* start, | |
|
huangs
2016/06/13 18:30:28
NIT:
// static
etiennep
2016/06/14 21:16:37
Done.
| |
| 342 size_t length, | |
| 343 uint16_t magic) { | |
| 344 if (length < kOffsetOfFileAddressOfNewExeHeader + 4 /* size */) | |
| 345 return false; | |
| 346 | |
| 347 // Have 'MZ' magic for a DOS header? | |
| 348 if (start[0] != 'M' || start[1] != 'Z') | |
| 349 return false; | |
| 350 | |
| 351 FileOffset file_offset = static_cast<FileOffset>( | |
| 352 ReadU32(start, kOffsetOfFileAddressOfNewExeHeader)); | |
| 353 if (file_offset >= length || file_offset % 8 != 0) | |
| 354 return false; | |
| 355 const uint8_t* const pe_header = start + file_offset; | |
| 356 const size_t kMinPEHeaderSize = 4 /*signature*/ + kSizeOfCoffHeader; | |
| 357 if (pe_header <= start || pe_header + kMinPEHeaderSize >= start + length) | |
| 358 return false; | |
| 359 | |
| 360 const uint8_t* optional_header = pe_header + 4 + kSizeOfCoffHeader; | |
| 361 | |
| 362 // Check we can read the magic. | |
| 363 if (optional_header + 2 >= start + length) | |
| 364 return false; | |
| 365 if (magic != ReadU16(optional_header, 0)) | |
| 366 return false; | |
| 367 | |
| 368 return true; | |
| 369 } | |
| 370 | |
| 341 RvaVisitor* DisassemblerWin32::CreateAbs32TargetRvaVisitor() { | 371 RvaVisitor* DisassemblerWin32::CreateAbs32TargetRvaVisitor() { |
| 342 return new RvaVisitor_Abs32(abs32_locations_, *this); | 372 return new RvaVisitor_Abs32(abs32_locations_, *this); |
| 343 } | 373 } |
| 344 | 374 |
| 345 RvaVisitor* DisassemblerWin32::CreateRel32TargetRvaVisitor() { | 375 RvaVisitor* DisassemblerWin32::CreateRel32TargetRvaVisitor() { |
| 346 return new RvaVisitor_Rel32(rel32_locations_, *this); | 376 return new RvaVisitor_Rel32(rel32_locations_, *this); |
| 347 } | 377 } |
| 348 | 378 |
| 349 void DisassemblerWin32::RemoveUnusedRel32Locations( | 379 void DisassemblerWin32::RemoveUnusedRel32Locations( |
| 350 AssemblyProgram* program) { | 380 AssemblyProgram* program) { |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 647 directory->size_ = static_cast<uint32_t>(size); | 677 directory->size_ = static_cast<uint32_t>(size); |
| 648 return true; | 678 return true; |
| 649 } else { | 679 } else { |
| 650 directory->address_ = 0; | 680 directory->address_ = 0; |
| 651 directory->size_ = 0; | 681 directory->size_ = 0; |
| 652 return true; | 682 return true; |
| 653 } | 683 } |
| 654 } | 684 } |
| 655 | 685 |
| 656 } // namespace courgette | 686 } // namespace courgette |
| OLD | NEW |