1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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_x86.h"
11 #include "base/basictypes.h"
12 #include "base/logging.h"
14 #include "courgette/assembly_program.h"
15 #include "courgette/courgette.h"
16 #include "courgette/encoded_program.h"
20 DisassemblerWin32X86::DisassemblerWin32X86(const void* start
, size_t length
)
21 : Disassembler(start
, length
),
22 incomplete_disassembly_(false),
24 optional_header_(NULL
),
25 size_of_optional_header_(0),
26 offset_of_data_directories_(0),
28 number_of_sections_(0),
30 has_text_section_(false),
32 size_of_initialized_data_(0),
33 size_of_uninitialized_data_(0),
38 number_of_data_directories_(0) {
41 // ParseHeader attempts to match up the buffer with the Windows data
42 // structures that exist within a Windows 'Portable Executable' format file.
43 // Returns 'true' if the buffer matches, and 'false' if the data looks
44 // suspicious. Rather than try to 'map' the buffer to the numerous windows
45 // structures, we extract the information we need into the courgette::PEInfo
48 bool DisassemblerWin32X86::ParseHeader() {
49 if (length() < kOffsetOfFileAddressOfNewExeHeader
+ 4 /*size*/)
50 return Bad("Too small");
52 // Have 'MZ' magic for a DOS header?
53 if (start()[0] != 'M' || start()[1] != 'Z')
56 // offset from DOS header to PE header is stored in DOS header.
57 uint32 offset
= ReadU32(start(),
58 kOffsetOfFileAddressOfNewExeHeader
);
60 if (offset
>= length())
61 return Bad("Bad offset to PE header");
63 const uint8
* const pe_header
= OffsetToPointer(offset
);
64 const size_t kMinPEHeaderSize
= 4 /*signature*/ + kSizeOfCoffHeader
;
65 if (pe_header
<= start() ||
66 pe_header
>= end() - kMinPEHeaderSize
)
67 return Bad("Bad offset to PE header");
70 return Bad("Misaligned PE header");
72 // The 'PE' header is an IMAGE_NT_HEADERS structure as defined in WINNT.H.
73 // See http://msdn.microsoft.com/en-us/library/ms680336(VS.85).aspx
75 // The first field of the IMAGE_NT_HEADERS is the signature.
76 if (!(pe_header
[0] == 'P' &&
77 pe_header
[1] == 'E' &&
80 return Bad("no PE signature");
82 // The second field of the IMAGE_NT_HEADERS is the COFF header.
83 // The COFF header is also called an IMAGE_FILE_HEADER
84 // http://msdn.microsoft.com/en-us/library/ms680313(VS.85).aspx
85 const uint8
* const coff_header
= pe_header
+ 4;
86 machine_type_
= ReadU16(coff_header
, 0);
87 number_of_sections_
= ReadU16(coff_header
, 2);
88 size_of_optional_header_
= ReadU16(coff_header
, 16);
90 // The rest of the IMAGE_NT_HEADERS is the IMAGE_OPTIONAL_HEADER(32|64)
91 const uint8
* const optional_header
= coff_header
+ kSizeOfCoffHeader
;
92 optional_header_
= optional_header
;
94 if (optional_header
+ size_of_optional_header_
>= end())
95 return Bad("optional header past end of file");
97 // Check we can read the magic.
98 if (size_of_optional_header_
< 2)
99 return Bad("optional header no magic");
101 uint16 magic
= ReadU16(optional_header
, 0);
103 if (magic
== kImageNtOptionalHdr32Magic
) {
104 is_PE32_plus_
= false;
105 offset_of_data_directories_
=
106 kOffsetOfDataDirectoryFromImageOptionalHeader32
;
107 } else if (magic
== kImageNtOptionalHdr64Magic
) {
108 is_PE32_plus_
= true;
109 offset_of_data_directories_
=
110 kOffsetOfDataDirectoryFromImageOptionalHeader64
;
112 return Bad("unrecognized magic");
115 // Check that we can read the rest of the the fixed fields. Data directories
116 // directly follow the fixed fields of the IMAGE_OPTIONAL_HEADER.
117 if (size_of_optional_header_
< offset_of_data_directories_
)
118 return Bad("optional header too short");
120 // The optional header is either an IMAGE_OPTIONAL_HEADER32 or
121 // IMAGE_OPTIONAL_HEADER64
122 // http://msdn.microsoft.com/en-us/library/ms680339(VS.85).aspx
124 // Copy the fields we care about.
125 size_of_code_
= ReadU32(optional_header
, 4);
126 size_of_initialized_data_
= ReadU32(optional_header
, 8);
127 size_of_uninitialized_data_
= ReadU32(optional_header
, 12);
128 base_of_code_
= ReadU32(optional_header
, 20);
131 image_base_
= ReadU64(optional_header
, 24);
133 base_of_data_
= ReadU32(optional_header
, 24);
134 image_base_
= ReadU32(optional_header
, 28);
136 size_of_image_
= ReadU32(optional_header
, 56);
137 number_of_data_directories_
=
138 ReadU32(optional_header
, (is_PE32_plus_
? 108 : 92));
140 if (size_of_code_
>= length() ||
141 size_of_initialized_data_
>= length() ||
142 size_of_code_
+ size_of_initialized_data_
>= length()) {
143 // This validation fires on some perfectly fine executables.
144 // return Bad("code or initialized data too big");
147 // TODO(sra): we can probably get rid of most of the data directories.
149 // 'b &= ...' could be short circuit 'b = b && ...' but it is not necessary
150 // for correctness and it compiles smaller this way.
151 b
&= ReadDataDirectory(0, &export_table_
);
152 b
&= ReadDataDirectory(1, &import_table_
);
153 b
&= ReadDataDirectory(2, &resource_table_
);
154 b
&= ReadDataDirectory(3, &exception_table_
);
155 b
&= ReadDataDirectory(5, &base_relocation_table_
);
156 b
&= ReadDataDirectory(11, &bound_import_table_
);
157 b
&= ReadDataDirectory(12, &import_address_table_
);
158 b
&= ReadDataDirectory(13, &delay_import_descriptor_
);
159 b
&= ReadDataDirectory(14, &clr_runtime_header_
);
161 return Bad("malformed data directory");
164 // Sections follow the optional header.
166 reinterpret_cast<const Section
*>(optional_header
+
167 size_of_optional_header_
);
168 size_t detected_length
= 0;
170 for (int i
= 0; i
< number_of_sections_
; ++i
) {
171 const Section
* section
= §ions_
[i
];
173 // TODO(sra): consider using the 'characteristics' field of the section
174 // header to see if the section contains instructions.
175 if (memcmp(section
->name
, ".text", 6) == 0)
176 has_text_section_
= true;
179 section
->file_offset_of_raw_data
+ section
->size_of_raw_data
;
180 if (section_end
> detected_length
)
181 detected_length
= section_end
;
184 // Pretend our in-memory copy is only as long as our detected length.
185 ReduceLength(detected_length
);
188 return Bad("64 bit executables are not supported by this disassembler");
191 if (!has_text_section()) {
192 return Bad("Resource-only executables are not yet supported");
198 bool DisassemblerWin32X86::Disassemble(AssemblyProgram
* target
) {
202 target
->set_image_base(image_base());
204 if (!ParseAbs32Relocs())
207 ParseRel32RelocsFromSections();
209 if (!ParseFile(target
))
212 target
->DefaultAssignIndexes();
217 ////////////////////////////////////////////////////////////////////////////////
219 bool DisassemblerWin32X86::ParseRelocs(std::vector
<RVA
> *relocs
) {
222 size_t relocs_size
= base_relocation_table_
.size_
;
223 if (relocs_size
== 0)
226 // The format of the base relocation table is a sequence of variable sized
227 // IMAGE_BASE_RELOCATION blocks. Search for
228 // "The format of the base relocation data is somewhat quirky"
229 // at http://msdn.microsoft.com/en-us/library/ms809762.aspx
231 const uint8
* relocs_start
= RVAToPointer(base_relocation_table_
.address_
);
232 const uint8
* relocs_end
= relocs_start
+ relocs_size
;
234 // Make sure entire base relocation table is within the buffer.
235 if (relocs_start
< start() ||
236 relocs_start
>= end() ||
237 relocs_end
<= start() ||
238 relocs_end
> end()) {
239 return Bad(".relocs outside image");
242 const uint8
* block
= relocs_start
;
244 // Walk the variable sized blocks.
245 while (block
+ 8 < relocs_end
) {
246 RVA page_rva
= ReadU32(block
, 0);
247 uint32 size
= ReadU32(block
, 4);
248 if (size
< 8 || // Size includes header ...
249 size
% 4 != 0) // ... and is word aligned.
250 return Bad("unreasonable relocs block");
252 const uint8
* end_entries
= block
+ size
;
254 if (end_entries
<= block
||
255 end_entries
<= start() ||
257 return Bad(".relocs block outside image");
259 // Walk through the two-byte entries.
260 for (const uint8
* p
= block
+ 8; p
< end_entries
; p
+= 2) {
261 uint16 entry
= ReadU16(p
, 0);
262 int type
= entry
>> 12;
263 int offset
= entry
& 0xFFF;
265 RVA rva
= page_rva
+ offset
;
266 // Skip the relocs that live outside of the image. It might be the case
267 // if a reloc is relative to a register, e.g.:
268 // mov ecx,dword ptr [eax+044D5888h]
269 uint32 target_address
= Read32LittleEndian(RVAToPointer(rva
));
270 if (target_address
< image_base_
||
271 target_address
> (image_base_
+ size_of_image_
)) {
274 if (type
== 3) { // IMAGE_REL_BASED_HIGHLOW
275 relocs
->push_back(rva
);
276 } else if (type
== 0) { // IMAGE_REL_BASED_ABSOLUTE
277 // Ignore, used as padding.
279 // Does not occur in Windows x86 executables.
280 return Bad("unknown type of reloc");
287 std::sort(relocs
->begin(), relocs
->end());
292 const Section
* DisassemblerWin32X86::RVAToSection(RVA rva
) const {
293 for (int i
= 0; i
< number_of_sections_
; i
++) {
294 const Section
* section
= §ions_
[i
];
295 uint32 offset
= rva
- section
->virtual_address
;
296 if (offset
< section
->virtual_size
) {
303 int DisassemblerWin32X86::RVAToFileOffset(RVA rva
) const {
304 const Section
* section
= RVAToSection(rva
);
306 uint32 offset
= rva
- section
->virtual_address
;
307 if (offset
< section
->size_of_raw_data
) {
308 return section
->file_offset_of_raw_data
+ offset
;
310 return kNoOffset
; // In section but not in file (e.g. uninit data).
314 // Small RVA values point into the file header in the loaded image.
315 // RVA 0 is the module load address which Windows uses as the module handle.
316 // RVA 2 sometimes occurs, I'm not sure what it is, but it would map into the
318 if (rva
== 0 || rva
== 2)
325 const uint8
* DisassemblerWin32X86::RVAToPointer(RVA rva
) const {
326 int file_offset
= RVAToFileOffset(rva
);
327 if (file_offset
== kNoOffset
)
330 return OffsetToPointer(file_offset
);
333 std::string
DisassemblerWin32X86::SectionName(const Section
* section
) {
337 memcpy(name
, section
->name
, 8);
338 name
[8] = '\0'; // Ensure termination.
342 CheckBool
DisassemblerWin32X86::ParseFile(AssemblyProgram
* program
) {
343 // Walk all the bytes in the file, whether or not in a section.
344 uint32 file_offset
= 0;
345 while (file_offset
< length()) {
346 const Section
* section
= FindNextSection(file_offset
);
347 if (section
== NULL
) {
348 // No more sections. There should not be extra stuff following last
350 // ParseNonSectionFileRegion(file_offset, pe_info().length(), program);
353 if (file_offset
< section
->file_offset_of_raw_data
) {
354 uint32 section_start_offset
= section
->file_offset_of_raw_data
;
355 if(!ParseNonSectionFileRegion(file_offset
, section_start_offset
,
359 file_offset
= section_start_offset
;
361 uint32 end
= file_offset
+ section
->size_of_raw_data
;
362 if (!ParseFileRegion(section
, file_offset
, end
, program
))
367 #if COURGETTE_HISTOGRAM_TARGETS
368 HistogramTargets("abs32 relocs", abs32_target_rvas_
);
369 HistogramTargets("rel32 relocs", rel32_target_rvas_
);
375 bool DisassemblerWin32X86::ParseAbs32Relocs() {
376 abs32_locations_
.clear();
377 if (!ParseRelocs(&abs32_locations_
))
380 #if COURGETTE_HISTOGRAM_TARGETS
381 for (size_t i
= 0; i
< abs32_locations_
.size(); ++i
) {
382 RVA rva
= abs32_locations_
[i
];
383 // The 4 bytes at the relocation are a reference to some address.
384 uint32 target_address
= Read32LittleEndian(RVAToPointer(rva
));
385 ++abs32_target_rvas_
[target_address
- image_base()];
391 void DisassemblerWin32X86::ParseRel32RelocsFromSections() {
392 uint32 file_offset
= 0;
393 while (file_offset
< length()) {
394 const Section
* section
= FindNextSection(file_offset
);
397 if (file_offset
< section
->file_offset_of_raw_data
)
398 file_offset
= section
->file_offset_of_raw_data
;
399 ParseRel32RelocsFromSection(section
);
400 file_offset
+= section
->size_of_raw_data
;
402 std::sort(rel32_locations_
.begin(), rel32_locations_
.end());
404 #if COURGETTE_HISTOGRAM_TARGETS
405 VLOG(1) << "abs32_locations_ " << abs32_locations_
.size()
406 << "\nrel32_locations_ " << rel32_locations_
.size()
407 << "\nabs32_target_rvas_ " << abs32_target_rvas_
.size()
408 << "\nrel32_target_rvas_ " << rel32_target_rvas_
.size();
411 std::map
<RVA
, int>::iterator abs32_iter
= abs32_target_rvas_
.begin();
412 std::map
<RVA
, int>::iterator rel32_iter
= rel32_target_rvas_
.begin();
413 while (abs32_iter
!= abs32_target_rvas_
.end() &&
414 rel32_iter
!= rel32_target_rvas_
.end()) {
415 if (abs32_iter
->first
< rel32_iter
->first
)
417 else if (rel32_iter
->first
< abs32_iter
->first
)
425 VLOG(1) << "common " << common
;
429 void DisassemblerWin32X86::ParseRel32RelocsFromSection(const Section
* section
) {
430 // TODO(sra): use characteristic.
431 bool isCode
= strcmp(section
->name
, ".text") == 0;
435 uint32 start_file_offset
= section
->file_offset_of_raw_data
;
436 uint32 end_file_offset
= start_file_offset
+ section
->size_of_raw_data
;
437 RVA relocs_start_rva
= base_relocation_table().address_
;
439 const uint8
* start_pointer
= OffsetToPointer(start_file_offset
);
440 const uint8
* end_pointer
= OffsetToPointer(end_file_offset
);
442 RVA start_rva
= FileOffsetToRVA(start_file_offset
);
443 RVA end_rva
= start_rva
+ section
->virtual_size
;
445 // Quick way to convert from Pointer to RVA within a single Section is to
446 // subtract 'pointer_to_rva'.
447 const uint8
* const adjust_pointer_to_rva
= start_pointer
- start_rva
;
449 std::vector
<RVA
>::iterator abs32_pos
= abs32_locations_
.begin();
451 // Find the rel32 relocations.
452 const uint8
* p
= start_pointer
;
453 while (p
< end_pointer
) {
454 RVA current_rva
= static_cast<RVA
>(p
- adjust_pointer_to_rva
);
455 if (current_rva
== relocs_start_rva
) {
456 uint32 relocs_size
= base_relocation_table().size_
;
463 //while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva)
466 // Heuristic discovery of rel32 locations in instruction stream: are the
467 // next few bytes the start of an instruction containing a rel32
469 const uint8
* rel32
= NULL
;
471 if (p
+ 5 <= end_pointer
) {
472 if (*p
== 0xE8 || *p
== 0xE9) { // jmp rel32 and call rel32
476 if (p
+ 6 <= end_pointer
) {
477 if (*p
== 0x0F && (*(p
+1) & 0xF0) == 0x80) { // Jcc long form
478 if (p
[1] != 0x8A && p
[1] != 0x8B) // JPE/JPO unlikely
483 RVA rel32_rva
= static_cast<RVA
>(rel32
- adjust_pointer_to_rva
);
485 // Is there an abs32 reloc overlapping the candidate?
486 while (abs32_pos
!= abs32_locations_
.end() && *abs32_pos
< rel32_rva
- 3)
488 // Now: (*abs32_pos > rel32_rva - 4) i.e. the lowest addressed 4-byte
489 // region that could overlap rel32_rva.
490 if (abs32_pos
!= abs32_locations_
.end()) {
491 if (*abs32_pos
< rel32_rva
+ 4) {
492 // Beginning of abs32 reloc is before end of rel32 reloc so they
493 // overlap. Skip four bytes past the abs32 reloc.
494 p
+= (*abs32_pos
+ 4) - current_rva
;
499 RVA target_rva
= rel32_rva
+ 4 + Read32LittleEndian(rel32
);
500 // To be valid, rel32 target must be within image, and within this
502 if (IsValidRVA(target_rva
) &&
503 start_rva
<= target_rva
&& target_rva
< end_rva
) {
504 rel32_locations_
.push_back(rel32_rva
);
505 #if COURGETTE_HISTOGRAM_TARGETS
506 ++rel32_target_rvas_
[target_rva
];
516 CheckBool
DisassemblerWin32X86::ParseNonSectionFileRegion(
517 uint32 start_file_offset
,
518 uint32 end_file_offset
,
519 AssemblyProgram
* program
) {
520 if (incomplete_disassembly_
)
523 if (end_file_offset
> start_file_offset
) {
524 if (!program
->EmitBytesInstruction(OffsetToPointer(start_file_offset
),
525 end_file_offset
- start_file_offset
)) {
533 CheckBool
DisassemblerWin32X86::ParseFileRegion(
534 const Section
* section
,
535 uint32 start_file_offset
, uint32 end_file_offset
,
536 AssemblyProgram
* program
) {
537 RVA relocs_start_rva
= base_relocation_table().address_
;
539 const uint8
* start_pointer
= OffsetToPointer(start_file_offset
);
540 const uint8
* end_pointer
= OffsetToPointer(end_file_offset
);
542 RVA start_rva
= FileOffsetToRVA(start_file_offset
);
543 RVA end_rva
= start_rva
+ section
->virtual_size
;
545 // Quick way to convert from Pointer to RVA within a single Section is to
546 // subtract 'pointer_to_rva'.
547 const uint8
* const adjust_pointer_to_rva
= start_pointer
- start_rva
;
549 std::vector
<RVA
>::iterator rel32_pos
= rel32_locations_
.begin();
550 std::vector
<RVA
>::iterator abs32_pos
= abs32_locations_
.begin();
552 if (!program
->EmitOriginInstruction(start_rva
))
555 const uint8
* p
= start_pointer
;
557 while (p
< end_pointer
) {
558 RVA current_rva
= static_cast<RVA
>(p
- adjust_pointer_to_rva
);
560 // The base relocation table is usually in the .relocs section, but it could
561 // actually be anywhere. Make sure we skip it because we will regenerate it
563 if (current_rva
== relocs_start_rva
) {
564 if (!program
->EmitPeRelocsInstruction())
566 uint32 relocs_size
= base_relocation_table().size_
;
573 while (abs32_pos
!= abs32_locations_
.end() && *abs32_pos
< current_rva
)
576 if (abs32_pos
!= abs32_locations_
.end() && *abs32_pos
== current_rva
) {
577 uint32 target_address
= Read32LittleEndian(p
);
578 RVA target_rva
= target_address
- image_base();
579 // TODO(sra): target could be Label+offset. It is not clear how to guess
580 // which it might be. We assume offset==0.
581 if (!program
->EmitAbs32(program
->FindOrMakeAbs32Label(target_rva
)))
587 while (rel32_pos
!= rel32_locations_
.end() && *rel32_pos
< current_rva
)
590 if (rel32_pos
!= rel32_locations_
.end() && *rel32_pos
== current_rva
) {
591 RVA target_rva
= current_rva
+ 4 + Read32LittleEndian(p
);
592 if (!program
->EmitRel32(program
->FindOrMakeRel32Label(target_rva
)))
598 if (incomplete_disassembly_
) {
599 if ((abs32_pos
== abs32_locations_
.end() || end_rva
<= *abs32_pos
) &&
600 (rel32_pos
== rel32_locations_
.end() || end_rva
<= *rel32_pos
) &&
601 (end_rva
<= relocs_start_rva
|| current_rva
>= relocs_start_rva
)) {
602 // No more relocs in this section, don't bother encoding bytes.
607 if (!program
->EmitByteInstruction(*p
))
615 #if COURGETTE_HISTOGRAM_TARGETS
616 // Histogram is printed to std::cout. It is purely for debugging the algorithm
617 // and is only enabled manually in 'exploration' builds. I don't want to add
618 // command-line configuration for this feature because this code has to be
619 // small, which means compiled-out.
620 void DisassemblerWin32X86::HistogramTargets(const char* kind
,
621 const std::map
<RVA
, int>& map
) {
623 std::map
<int, std::vector
<RVA
> > h
;
624 for (std::map
<RVA
, int>::const_iterator p
= map
.begin();
627 h
[p
->second
].push_back(p
->first
);
631 std::cout
<< total
<< " " << kind
<< " to "
632 << map
.size() << " unique targets" << std::endl
;
634 std::cout
<< "indegree: #targets-with-indegree (example)" << std::endl
;
635 const int kFirstN
= 15;
636 bool someSkipped
= false;
638 for (std::map
<int, std::vector
<RVA
> >::reverse_iterator p
= h
.rbegin();
642 if (index
<= kFirstN
|| p
->first
<= 3) {
644 std::cout
<< "..." << std::endl
;
646 size_t count
= p
->second
.size();
647 std::cout
<< std::dec
<< p
->first
<< ": " << count
;
649 for (size_t i
= 0; i
< count
; ++i
)
650 std::cout
<< " " << DescribeRVA(p
->second
[i
]);
652 std::cout
<< std::endl
;
659 #endif // COURGETTE_HISTOGRAM_TARGETS
662 // DescribeRVA is for debugging only. I would put it under #ifdef DEBUG except
663 // that during development I'm finding I need to call it when compiled in
664 // Release mode. Hence:
665 // TODO(sra): make this compile only for debug mode.
666 std::string
DisassemblerWin32X86::DescribeRVA(RVA rva
) const {
667 const Section
* section
= RVAToSection(rva
);
668 std::ostringstream s
;
669 s
<< std::hex
<< rva
;
672 s
<< SectionName(section
) << "+"
673 << std::hex
<< (rva
- section
->virtual_address
)
679 const Section
* DisassemblerWin32X86::FindNextSection(uint32 fileOffset
) const {
680 const Section
* best
= 0;
681 for (int i
= 0; i
< number_of_sections_
; i
++) {
682 const Section
* section
= §ions_
[i
];
683 if (section
->size_of_raw_data
> 0) { // i.e. has data in file.
684 if (fileOffset
<= section
->file_offset_of_raw_data
) {
686 section
->file_offset_of_raw_data
< best
->file_offset_of_raw_data
) {
695 RVA
DisassemblerWin32X86::FileOffsetToRVA(uint32 file_offset
) const {
696 for (int i
= 0; i
< number_of_sections_
; i
++) {
697 const Section
* section
= §ions_
[i
];
698 uint32 offset
= file_offset
- section
->file_offset_of_raw_data
;
699 if (offset
< section
->size_of_raw_data
) {
700 return section
->virtual_address
+ offset
;
706 bool DisassemblerWin32X86::ReadDataDirectory(
708 ImageDataDirectory
* directory
) {
710 if (index
< number_of_data_directories_
) {
711 size_t offset
= index
* 8 + offset_of_data_directories_
;
712 if (offset
>= size_of_optional_header_
)
713 return Bad("number of data directories inconsistent");
714 const uint8
* data_directory
= optional_header_
+ offset
;
715 if (data_directory
< start() ||
716 data_directory
+ 8 >= end())
717 return Bad("data directory outside image");
718 RVA rva
= ReadU32(data_directory
, 0);
719 size_t size
= ReadU32(data_directory
, 4);
720 if (size
> size_of_image_
)
721 return Bad("data directory size too big");
723 // TODO(sra): validate RVA.
724 directory
->address_
= rva
;
725 directory
->size_
= static_cast<uint32
>(size
);
728 directory
->address_
= 0;
729 directory
->size_
= 0;
734 } // namespace courgette