1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "courgette/disassembler_win32.h"
10 #include "base/logging.h"
11 #include "courgette/assembly_program.h"
12 #include "courgette/courgette.h"
14 #if COURGETTE_HISTOGRAM_TARGETS
20 DisassemblerWin32::DisassemblerWin32(const uint8_t* start, size_t length)
21 : Disassembler(start, length) {}
23 RVA DisassemblerWin32::FileOffsetToRVA(FileOffset file_offset) const {
24 for (int i = 0; i < number_of_sections_; ++i) {
25 const Section* section = §ions_[i];
26 if (file_offset >= section->file_offset_of_raw_data) {
27 FileOffset offset_in_section =
28 file_offset - section->file_offset_of_raw_data;
29 if (offset_in_section < section->size_of_raw_data)
30 return static_cast<RVA>(section->virtual_address + offset_in_section);
38 FileOffset DisassemblerWin32::RVAToFileOffset(RVA rva) const {
39 const Section* section = RVAToSection(rva);
40 if (section != nullptr) {
41 FileOffset offset_in_section = rva - section->virtual_address;
42 // Need this extra check, since an |rva| may be valid for a section, but is
43 // non-existent in an image (e.g. uninit data).
44 if (offset_in_section >= section->size_of_raw_data)
47 return static_cast<FileOffset>(section->file_offset_of_raw_data +
51 // Small RVA values point into the file header in the loaded image.
52 // RVA 0 is the module load address which Windows uses as the module handle.
53 // RVA 2 sometimes occurs, I'm not sure what it is, but it would map into the
55 if (rva == 0 || rva == 2)
56 return static_cast<FileOffset>(rva);
62 // ParseHeader attempts to match up the buffer with the Windows data
63 // structures that exist within a Windows 'Portable Executable' format file.
64 // Returns 'true' if the buffer matches, and 'false' if the data looks
65 // suspicious. Rather than try to 'map' the buffer to the numerous windows
66 // structures, we extract the information we need into the courgette::PEInfo
69 bool DisassemblerWin32::ParseHeader() {
70 if (!IsRangeInBounds(kOffsetOfFileAddressOfNewExeHeader, 4))
71 return Bad("Too small");
73 // Have 'MZ' magic for a DOS header?
74 if (start()[0] != 'M' || start()[1] != 'Z')
77 // offset from DOS header to PE header is stored in DOS header.
78 FileOffset pe_header_offset = static_cast<FileOffset>(
79 ReadU32(start(), kOffsetOfFileAddressOfNewExeHeader));
80 if (pe_header_offset % 8 != 0)
81 return Bad("Misaligned PE header");
82 if (pe_header_offset < kOffsetOfFileAddressOfNewExeHeader + 4)
83 return Bad("PE header pathological overlap");
84 if (!IsRangeInBounds(pe_header_offset, kMinPeHeaderSize))
85 return Bad("PE header past end of file");
87 const uint8_t* const pe_header = FileOffsetToPointer(pe_header_offset);
89 // The 'PE' header is an IMAGE_NT_HEADERS structure as defined in WINNT.H.
90 // See http://msdn.microsoft.com/en-us/library/ms680336(VS.85).aspx
92 // The first field of the IMAGE_NT_HEADERS is the signature.
93 if (!(pe_header[0] == 'P' && pe_header[1] == 'E' && pe_header[2] == 0 &&
95 return Bad("No PE signature");
98 // The second field of the IMAGE_NT_HEADERS is the COFF header.
99 // The COFF header is also called an IMAGE_FILE_HEADER
100 // http://msdn.microsoft.com/en-us/library/ms680313(VS.85).aspx
101 FileOffset coff_header_offset = pe_header_offset + 4;
102 if (!IsRangeInBounds(coff_header_offset, kSizeOfCoffHeader))
103 return Bad("COFF header past end of file");
104 const uint8_t* const coff_header = start() + coff_header_offset;
105 machine_type_ = ReadU16(coff_header, 0);
106 number_of_sections_ = ReadU16(coff_header, 2);
107 size_of_optional_header_ = ReadU16(coff_header, 16);
108 // Check we can read the magic.
109 if (size_of_optional_header_ < 2)
110 return Bad("Optional header no magic");
111 // Check that we can read the rest of the the fixed fields. Data directories
112 // directly follow the fixed fields of the IMAGE_OPTIONAL_HEADER.
113 if (size_of_optional_header_ < RelativeOffsetOfDataDirectories())
114 return Bad("Optional header too short");
116 // The rest of the IMAGE_NT_HEADERS is the IMAGE_OPTIONAL_HEADER(32|64)
117 FileOffset optional_header_offset = pe_header_offset + kMinPeHeaderSize;
118 if (!IsRangeInBounds(optional_header_offset, size_of_optional_header_))
119 return Bad("Optional header past end of file");
120 optional_header_ = start() + optional_header_offset;
122 uint16_t magic = ReadU16(optional_header_, 0);
125 if (magic != kImageNtOptionalHdr32Magic)
126 return Bad("64 bit executables are not supported by this disassembler");
130 if (magic != kImageNtOptionalHdr64Magic)
131 return Bad("32 bit executables are not supported by this disassembler");
135 return Bad("Unrecognized magic");
138 // The optional header is either an IMAGE_OPTIONAL_HEADER32 or
139 // IMAGE_OPTIONAL_HEADER64
140 // http://msdn.microsoft.com/en-us/library/ms680339(VS.85).aspx
142 // Copy the fields we care about.
143 size_of_code_ = ReadU32(optional_header_, 4);
144 size_of_initialized_data_ = ReadU32(optional_header_, 8);
145 size_of_uninitialized_data_ = ReadU32(optional_header_, 12);
146 base_of_code_ = ReadU32(optional_header_, 20);
150 base_of_data_ = ReadU32(optional_header_, 24);
151 image_base_ = ReadU32(optional_header_, 28);
152 size_of_image_ = ReadU32(optional_header_, 56);
153 number_of_data_directories_ = ReadU32(optional_header_, 92);
158 image_base_ = ReadU64(optional_header_, 24);
159 size_of_image_ = ReadU32(optional_header_, 56);
160 number_of_data_directories_ = ReadU32(optional_header_, 108);
167 if (size_of_image_ >= 0x80000000U)
168 return Bad("Invalid SizeOfImage");
170 if (size_of_code_ >= length() || size_of_initialized_data_ >= length() ||
171 size_of_code_ + size_of_initialized_data_ >= length()) {
172 // This validation fires on some perfectly fine executables.
173 // return Bad("code or initialized data too big");
176 // TODO(sra): we can probably get rid of most of the data directories.
178 // 'b &= ...' could be short circuit 'b = b && ...' but it is not necessary
179 // for correctness and it compiles smaller this way.
180 b &= ReadDataDirectory(0, &export_table_);
181 b &= ReadDataDirectory(1, &import_table_);
182 b &= ReadDataDirectory(2, &resource_table_);
183 b &= ReadDataDirectory(3, &exception_table_);
184 b &= ReadDataDirectory(5, &base_relocation_table_);
185 b &= ReadDataDirectory(11, &bound_import_table_);
186 b &= ReadDataDirectory(12, &import_address_table_);
187 b &= ReadDataDirectory(13, &delay_import_descriptor_);
188 b &= ReadDataDirectory(14, &clr_runtime_header_);
190 return Bad("Malformed data directory");
192 // Sections follow the optional header.
193 FileOffset sections_offset =
194 optional_header_offset + size_of_optional_header_;
195 if (!IsArrayInBounds(sections_offset, number_of_sections_, sizeof(Section)))
196 return Bad("Sections past end of file");
197 sections_ = reinterpret_cast<const Section*>(start() + sections_offset);
198 if (!CheckSectionRanges())
199 return Bad("Out of bound section");
201 size_t detected_length = 0;
202 for (int i = 0; i < number_of_sections_; ++i) {
203 const Section* section = §ions_[i];
205 // TODO(sra): consider using the 'characteristics' field of the section
206 // header to see if the section contains instructions.
207 if (memcmp(section->name, ".text", 6) == 0)
208 has_text_section_ = true;
210 uint32_t section_end =
211 section->file_offset_of_raw_data + section->size_of_raw_data;
212 if (section_end > detected_length)
213 detected_length = section_end;
216 // Pretend our in-memory copy is only as long as our detected length.
217 ReduceLength(detected_length);
219 if (!has_text_section()) {
220 return Bad("Resource-only executables are not yet supported");
226 ////////////////////////////////////////////////////////////////////////////////
228 bool DisassemblerWin32::ParseRelocs(std::vector<RVA>* relocs) {
231 size_t relocs_size = base_relocation_table_.size_;
232 if (relocs_size == 0)
235 // The format of the base relocation table is a sequence of variable sized
236 // IMAGE_BASE_RELOCATION blocks. Search for
237 // "The format of the base relocation data is somewhat quirky"
238 // at http://msdn.microsoft.com/en-us/library/ms809762.aspx
240 const uint8_t* relocs_start = RVAToPointer(base_relocation_table_.address_);
241 if (relocs_start == nullptr || relocs_start < start() ||
242 relocs_start >= end())
243 return Bad(".relocs outside image");
245 // Make sure entire base relocation table is within the buffer.
246 if (relocs_size > static_cast<size_t>(end() - relocs_start))
247 return Bad(".relocs outside image");
248 const uint8_t* relocs_end = relocs_start + relocs_size;
250 const uint8_t* block = relocs_start;
252 // Walk the variable sized blocks.
253 while (block + 8 < relocs_end) {
254 RVA page_rva = ReadU32(block, 0);
255 uint32_t size = ReadU32(block, 4);
256 if (size < 8 || // Size includes header ...
257 size % 4 != 0) // ... and is word aligned.
258 return Bad("Unreasonable relocs block");
260 const uint8_t* end_entries = block + size;
262 if (end_entries <= block || end_entries <= start() || end_entries > end())
263 return Bad(".relocs block outside image");
265 // Walk through the two-byte entries.
266 for (const uint8_t* p = block + 8; p < end_entries; p += 2) {
267 uint16_t entry = ReadU16(p, 0);
268 int type = entry >> 12;
269 int offset = entry & 0xFFF;
271 RVA rva = page_rva + offset;
272 // Skip the relocs that live outside of the image. It might be the case
273 // if a reloc is relative to a register, e.g.:
274 // mov ecx,dword ptr [eax+044D5888h]
275 RVA target_rva = PointerToTargetRVA(RVAToPointer(rva));
276 if (target_rva == kNoRVA) {
280 if (SupportsRelTableType(type)) {
281 relocs->push_back(rva);
282 } else if (type == 0) { // IMAGE_REL_BASED_ABSOLUTE
283 // Ignore, used as padding.
285 // Does not occur in Windows x86/x64 executables.
286 return Bad("Unknown type of reloc");
293 std::sort(relocs->begin(), relocs->end());
294 DCHECK(relocs->empty() || relocs->back() != kUnassignedRVA);
299 const Section* DisassemblerWin32::RVAToSection(RVA rva) const {
300 for (int i = 0; i < number_of_sections_; ++i) {
301 const Section* section = §ions_[i];
302 if (rva >= section->virtual_address) {
303 FileOffset offset_in_section = rva - section->virtual_address;
304 if (offset_in_section < section->virtual_size)
311 std::string DisassemblerWin32::SectionName(const Section* section) {
312 if (section == nullptr)
315 memcpy(name, section->name, 8);
316 name[8] = '\0'; // Ensure termination.
321 bool DisassemblerWin32::QuickDetect(const uint8_t* start,
324 if (length < kOffsetOfFileAddressOfNewExeHeader + 4)
327 // Have 'MZ' magic for a DOS header?
328 if (start[0] != 'M' || start[1] != 'Z')
331 FileOffset pe_header_offset = static_cast<FileOffset>(
332 ReadU32(start, kOffsetOfFileAddressOfNewExeHeader));
333 if (pe_header_offset % 8 != 0 ||
334 pe_header_offset < kOffsetOfFileAddressOfNewExeHeader + 4 ||
335 pe_header_offset >= length ||
336 length - pe_header_offset < kMinPeHeaderSize) {
339 const uint8_t* pe_header = start + pe_header_offset;
340 if (!(pe_header[0] == 'P' && pe_header[1] == 'E' && pe_header[2] == 0 &&
341 pe_header[3] == 0)) {
345 FileOffset optional_header_offset = pe_header_offset + kMinPeHeaderSize;
346 if (optional_header_offset >= length || length - optional_header_offset < 2)
348 const uint8_t* optional_header = start + optional_header_offset;
349 return magic == ReadU16(optional_header, 0);
352 bool DisassemblerWin32::IsRvaRangeInBounds(size_t start, size_t length) {
353 return start < size_of_image_ && length <= size_of_image_ - start;
356 bool DisassemblerWin32::CheckSectionRanges() {
357 for (int i = 0; i < number_of_sections_; ++i) {
358 const Section* section = §ions_[i];
359 if (!IsRangeInBounds(section->file_offset_of_raw_data,
360 section->size_of_raw_data) ||
361 !IsRvaRangeInBounds(section->virtual_address, section->virtual_size)) {
368 bool DisassemblerWin32::ExtractAbs32Locations() {
369 abs32_locations_.clear();
370 if (!ParseRelocs(&abs32_locations_))
373 #if COURGETTE_HISTOGRAM_TARGETS
374 for (size_t i = 0; i < abs32_locations_.size(); ++i) {
375 RVA rva = abs32_locations_[i];
376 // The 4 bytes at the relocation are a reference to some address.
377 ++abs32_target_rvas_[PointerToTargetRVA(RVAToPointer(rva))];
383 bool DisassemblerWin32::ExtractRel32Locations() {
384 FileOffset file_offset = 0;
385 while (file_offset < length()) {
386 const Section* section = FindNextSection(file_offset);
387 if (section == nullptr)
389 if (file_offset < section->file_offset_of_raw_data)
390 file_offset = section->file_offset_of_raw_data;
391 ParseRel32RelocsFromSection(section);
392 file_offset += section->size_of_raw_data;
394 std::sort(rel32_locations_.begin(), rel32_locations_.end());
395 DCHECK(rel32_locations_.empty() || rel32_locations_.back() != kUnassignedRVA);
397 #if COURGETTE_HISTOGRAM_TARGETS
398 VLOG(1) << "abs32_locations_ " << abs32_locations_.size()
399 << "\nrel32_locations_ " << rel32_locations_.size()
400 << "\nabs32_target_rvas_ " << abs32_target_rvas_.size()
401 << "\nrel32_target_rvas_ " << rel32_target_rvas_.size();
404 std::map<RVA, int>::iterator abs32_iter = abs32_target_rvas_.begin();
405 std::map<RVA, int>::iterator rel32_iter = rel32_target_rvas_.begin();
406 while (abs32_iter != abs32_target_rvas_.end() &&
407 rel32_iter != rel32_target_rvas_.end()) {
408 if (abs32_iter->first < rel32_iter->first) {
410 } else if (rel32_iter->first < abs32_iter->first) {
418 VLOG(1) << "common " << common;
423 RvaVisitor* DisassemblerWin32::CreateAbs32TargetRvaVisitor() {
424 return new RvaVisitor_Abs32(abs32_locations_, *this);
427 RvaVisitor* DisassemblerWin32::CreateRel32TargetRvaVisitor() {
428 return new RvaVisitor_Rel32(rel32_locations_, *this);
431 void DisassemblerWin32::RemoveUnusedRel32Locations(
432 AssemblyProgram* program) {
433 auto cond = [this, program](RVA rva) -> bool {
434 // + 4 since offset is relative to start of next instruction.
435 RVA target_rva = rva + 4 + Read32LittleEndian(RVAToPointer(rva));
436 return program->FindRel32Label(target_rva) == nullptr;
438 rel32_locations_.erase(
439 std::remove_if(rel32_locations_.begin(), rel32_locations_.end(), cond),
440 rel32_locations_.end());
443 InstructionGenerator DisassemblerWin32::GetInstructionGenerator(
444 AssemblyProgram* program) {
445 return base::BindRepeating(&DisassemblerWin32::ParseFile,
446 base::Unretained(this), program);
449 CheckBool DisassemblerWin32::ParseFile(AssemblyProgram* program,
450 InstructionReceptor* receptor) const {
451 // Walk all the bytes in the file, whether or not in a section.
452 FileOffset file_offset = 0;
453 while (file_offset < length()) {
454 const Section* section = FindNextSection(file_offset);
455 if (section == nullptr) {
456 // No more sections. There should not be extra stuff following last
458 // ParseNonSectionFileRegion(file_offset, pe_info().length(), receptor);
461 if (file_offset < section->file_offset_of_raw_data) {
462 FileOffset section_start_offset = section->file_offset_of_raw_data;
463 if (!ParseNonSectionFileRegion(file_offset, section_start_offset,
468 file_offset = section_start_offset;
470 FileOffset end = file_offset + section->size_of_raw_data;
471 if (!ParseFileRegion(section, file_offset, end, program, receptor))
476 #if COURGETTE_HISTOGRAM_TARGETS
477 HistogramTargets("abs32 relocs", abs32_target_rvas_);
478 HistogramTargets("rel32 relocs", rel32_target_rvas_);
484 CheckBool DisassemblerWin32::ParseNonSectionFileRegion(
485 FileOffset start_file_offset,
486 FileOffset end_file_offset,
487 InstructionReceptor* receptor) const {
488 if (incomplete_disassembly_)
491 if (end_file_offset > start_file_offset) {
492 if (!receptor->EmitMultipleBytes(FileOffsetToPointer(start_file_offset),
493 end_file_offset - start_file_offset)) {
501 CheckBool DisassemblerWin32::ParseFileRegion(
502 const Section* section,
503 FileOffset start_file_offset,
504 FileOffset end_file_offset,
505 AssemblyProgram* program,
506 InstructionReceptor* receptor) const {
507 RVA relocs_start_rva = base_relocation_table().address_;
509 const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
510 const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
512 RVA start_rva = FileOffsetToRVA(start_file_offset);
513 RVA end_rva = start_rva + section->virtual_size;
514 const int kVAWidth = AbsVAWidth();
516 // Quick way to convert from Pointer to RVA within a single Section is to
517 // subtract 'pointer_to_rva'.
518 const uint8_t* const adjust_pointer_to_rva = start_pointer - start_rva;
520 std::vector<RVA>::const_iterator rel32_pos = rel32_locations_.begin();
521 std::vector<RVA>::const_iterator abs32_pos = abs32_locations_.begin();
523 if (!receptor->EmitOrigin(start_rva))
526 const uint8_t* p = start_pointer;
528 while (p < end_pointer) {
529 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva);
531 // The base relocation table is usually in the .relocs section, but it could
532 // actually be anywhere. Make sure we skip it because we will regenerate it
534 if (current_rva == relocs_start_rva) {
535 if (!receptor->EmitPeRelocs())
537 uint32_t relocs_size = base_relocation_table().size_;
544 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva)
547 if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) {
548 RVA target_rva = PointerToTargetRVA(p);
549 DCHECK_NE(kNoRVA, target_rva);
550 // TODO(sra): target could be Label+offset. It is not clear how to guess
551 // which it might be. We assume offset==0.
552 Label* label = program->FindAbs32Label(target_rva);
554 if (!EmitAbs(label, receptor))
560 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva)
563 if (rel32_pos != rel32_locations_.end() && *rel32_pos == current_rva) {
564 // + 4 since offset is relative to start of next instruction.
565 RVA target_rva = current_rva + 4 + Read32LittleEndian(p);
566 Label* label = program->FindRel32Label(target_rva);
568 if (!receptor->EmitRel32(label))
574 if (incomplete_disassembly_) {
575 if ((abs32_pos == abs32_locations_.end() || end_rva <= *abs32_pos) &&
576 (rel32_pos == rel32_locations_.end() || end_rva <= *rel32_pos) &&
577 (end_rva <= relocs_start_rva || current_rva >= relocs_start_rva)) {
578 // No more relocs in this section, don't bother encoding bytes.
583 if (!receptor->EmitSingleByte(*p))
591 #if COURGETTE_HISTOGRAM_TARGETS
592 // Histogram is printed to std::cout. It is purely for debugging the algorithm
593 // and is only enabled manually in 'exploration' builds. I don't want to add
594 // command-line configuration for this feature because this code has to be
595 // small, which means compiled-out.
596 void DisassemblerWin32::HistogramTargets(const char* kind,
597 const std::map<RVA, int>& map) const {
599 std::map<int, std::vector<RVA>> h;
600 for (std::map<RVA, int>::const_iterator p = map.begin(); p != map.end();
602 h[p->second].push_back(p->first);
606 std::cout << total << " " << kind << " to " << map.size() << " unique targets"
609 std::cout << "indegree: #targets-with-indegree (example)" << std::endl;
610 const int kFirstN = 15;
611 bool someSkipped = false;
613 for (std::map<int, std::vector<RVA>>::reverse_iterator p = h.rbegin();
614 p != h.rend(); ++p) {
616 if (index <= kFirstN || p->first <= 3) {
618 std::cout << "..." << std::endl;
620 size_t count = p->second.size();
621 std::cout << std::dec << p->first << ": " << count;
623 for (size_t i = 0; i < count; ++i)
624 std::cout << " " << DescribeRVA(p->second[i]);
626 std::cout << std::endl;
633 #endif // COURGETTE_HISTOGRAM_TARGETS
635 // DescribeRVA is for debugging only. I would put it under #ifdef DEBUG except
636 // that during development I'm finding I need to call it when compiled in
637 // Release mode. Hence:
638 // TODO(sra): make this compile only for debug mode.
639 std::string DisassemblerWin32::DescribeRVA(RVA rva) const {
640 const Section* section = RVAToSection(rva);
641 std::ostringstream s;
642 s << std::hex << rva;
645 s << SectionName(section) << "+" << std::hex
646 << (rva - section->virtual_address) << ")";
651 const Section* DisassemblerWin32::FindNextSection(
652 FileOffset file_offset) const {
653 const Section* best = nullptr;
654 for (int i = 0; i < number_of_sections_; ++i) {
655 const Section* section = §ions_[i];
656 if (section->size_of_raw_data > 0) { // i.e. has data in file.
657 if (file_offset <= section->file_offset_of_raw_data) {
658 if (best == nullptr ||
659 section->file_offset_of_raw_data < best->file_offset_of_raw_data) {
668 bool DisassemblerWin32::ReadDataDirectory(int index,
669 ImageDataDirectory* directory) {
670 if (index < number_of_data_directories_) {
671 FileOffset file_offset = index * 8 + RelativeOffsetOfDataDirectories();
672 if (file_offset >= size_of_optional_header_)
673 return Bad("Number of data directories inconsistent");
674 const uint8_t* data_directory = optional_header_ + file_offset;
675 if (data_directory < start() || data_directory + 8 >= end())
676 return Bad("Data directory outside image");
677 RVA rva = ReadU32(data_directory, 0);
678 size_t size = ReadU32(data_directory, 4);
679 if (size > size_of_image_)
680 return Bad("Data directory size too big");
682 // TODO(sra): validate RVA.
683 directory->address_ = rva;
684 directory->size_ = static_cast<uint32_t>(size);
687 directory->address_ = 0;
688 directory->size_ = 0;
693 } // namespace courgette