[go: nahoru, domu]

blob: b9133f2735a25803f6754beeecf55460b8501ccc [file] [log] [blame]
etiennep5ac34c92016-05-20 15:44:461// Copyright 2016 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.
4
5#include "courgette/disassembler_win32.h"
6
etiennep5ac34c92016-05-20 15:44:467#include <algorithm>
8
huangs7b221a52016-11-09 22:28:239#include "base/bind.h"
etiennep5ac34c92016-05-20 15:44:4610#include "base/logging.h"
11#include "courgette/assembly_program.h"
12#include "courgette/courgette.h"
13
14#if COURGETTE_HISTOGRAM_TARGETS
15#include <iostream>
16#endif
17
18namespace courgette {
19
etiennep5059bca2016-07-08 17:55:2020DisassemblerWin32::DisassemblerWin32(const uint8_t* start, size_t length)
etiennep5ac34c92016-05-20 15:44:4621 : Disassembler(start, length) {}
22
23RVA DisassemblerWin32::FileOffsetToRVA(FileOffset file_offset) const {
24 for (int i = 0; i < number_of_sections_; ++i) {
25 const Section* section = &sections_[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);
31 }
32 }
33
34 NOTREACHED();
35 return kNoRVA;
36}
37
38FileOffset 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)
45 return kNoFileOffset;
46
47 return static_cast<FileOffset>(section->file_offset_of_raw_data +
48 offset_in_section);
49 }
50
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
54 // DOS header.
55 if (rva == 0 || rva == 2)
56 return static_cast<FileOffset>(rva);
57
58 NOTREACHED();
59 return kNoFileOffset;
60}
61
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
67// structure.
68//
69bool DisassemblerWin32::ParseHeader() {
Samuel Huanga34dce52019-04-03 15:53:1370 if (!IsRangeInBounds(kOffsetOfFileAddressOfNewExeHeader, 4))
etiennep5ac34c92016-05-20 15:44:4671 return Bad("Too small");
72
73 // Have 'MZ' magic for a DOS header?
74 if (start()[0] != 'M' || start()[1] != 'Z')
75 return Bad("Not MZ");
76
77 // offset from DOS header to PE header is stored in DOS header.
Samuel Huanga34dce52019-04-03 15:53:1378 FileOffset pe_header_offset = static_cast<FileOffset>(
etiennep5ac34c92016-05-20 15:44:4679 ReadU32(start(), kOffsetOfFileAddressOfNewExeHeader));
Samuel Huanga34dce52019-04-03 15:53:1380 if (pe_header_offset % 8 != 0)
etiennep5ac34c92016-05-20 15:44:4681 return Bad("Misaligned PE header");
Samuel Huanga34dce52019-04-03 15:53:1382 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");
86
87 const uint8_t* const pe_header = FileOffsetToPointer(pe_header_offset);
etiennep5ac34c92016-05-20 15:44:4688
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
91 //
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 &&
Samuel Huanga34dce52019-04-03 15:53:1394 pe_header[3] == 0)) {
etiennep5ac34c92016-05-20 15:44:4695 return Bad("No PE signature");
Samuel Huanga34dce52019-04-03 15:53:1396 }
etiennep5ac34c92016-05-20 15:44:4697
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
Samuel Huanga34dce52019-04-03 15:53:13101 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;
etiennep5ac34c92016-05-20 15:44:46105 machine_type_ = ReadU16(coff_header, 0);
106 number_of_sections_ = ReadU16(coff_header, 2);
107 size_of_optional_header_ = ReadU16(coff_header, 16);
etiennep5ac34c92016-05-20 15:44:46108 // Check we can read the magic.
109 if (size_of_optional_header_ < 2)
110 return Bad("Optional header no magic");
Samuel Huanga34dce52019-04-03 15:53:13111 // 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");
etiennep5ac34c92016-05-20 15:44:46115
Samuel Huanga34dce52019-04-03 15:53:13116 // 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;
etiennep5ac34c92016-05-20 15:44:46121
Samuel Huanga34dce52019-04-03 15:53:13122 uint16_t magic = ReadU16(optional_header_, 0);
etiennep5ac34c92016-05-20 15:44:46123 switch (kind()) {
124 case EXE_WIN_32_X86:
Samuel Huanga34dce52019-04-03 15:53:13125 if (magic != kImageNtOptionalHdr32Magic)
etiennep5ac34c92016-05-20 15:44:46126 return Bad("64 bit executables are not supported by this disassembler");
etiennep5ac34c92016-05-20 15:44:46127 break;
128
129 case EXE_WIN_32_X64:
Samuel Huanga34dce52019-04-03 15:53:13130 if (magic != kImageNtOptionalHdr64Magic)
etiennep5ac34c92016-05-20 15:44:46131 return Bad("32 bit executables are not supported by this disassembler");
etiennep5ac34c92016-05-20 15:44:46132 break;
133
134 default:
135 return Bad("Unrecognized magic");
136 }
137
etiennep5ac34c92016-05-20 15:44:46138 // 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
141 //
142 // Copy the fields we care about.
Samuel Huanga34dce52019-04-03 15:53:13143 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);
etiennep5ac34c92016-05-20 15:44:46147
148 switch (kind()) {
149 case EXE_WIN_32_X86:
Samuel Huanga34dce52019-04-03 15:53:13150 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);
etiennep5ac34c92016-05-20 15:44:46154 break;
155
156 case EXE_WIN_32_X64:
157 base_of_data_ = 0;
Samuel Huanga34dce52019-04-03 15:53:13158 image_base_ = ReadU64(optional_header_, 24);
159 size_of_image_ = ReadU32(optional_header_, 56);
160 number_of_data_directories_ = ReadU32(optional_header_, 108);
etiennep5ac34c92016-05-20 15:44:46161 break;
162
163 default:
164 NOTREACHED();
165 }
166
Samuel Huanga34dce52019-04-03 15:53:13167 if (size_of_image_ >= 0x80000000U)
168 return Bad("Invalid SizeOfImage");
169
etiennep5ac34c92016-05-20 15:44:46170 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");
174 }
175
176 // TODO(sra): we can probably get rid of most of the data directories.
177 bool b = true;
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_);
Samuel Huanga34dce52019-04-03 15:53:13189 if (!b)
etiennep5ac34c92016-05-20 15:44:46190 return Bad("Malformed data directory");
etiennep5ac34c92016-05-20 15:44:46191
192 // Sections follow the optional header.
Samuel Huanga34dce52019-04-03 15:53:13193 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");
etiennep5ac34c92016-05-20 15:44:46200
Samuel Huanga34dce52019-04-03 15:53:13201 size_t detected_length = 0;
etiennep5ac34c92016-05-20 15:44:46202 for (int i = 0; i < number_of_sections_; ++i) {
203 const Section* section = &sections_[i];
204
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;
209
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;
214 }
215
216 // Pretend our in-memory copy is only as long as our detected length.
217 ReduceLength(detected_length);
218
219 if (!has_text_section()) {
220 return Bad("Resource-only executables are not yet supported");
221 }
222
223 return Good();
224}
225
etiennep5ac34c92016-05-20 15:44:46226////////////////////////////////////////////////////////////////////////////////
227
228bool DisassemblerWin32::ParseRelocs(std::vector<RVA>* relocs) {
229 relocs->clear();
230
231 size_t relocs_size = base_relocation_table_.size_;
232 if (relocs_size == 0)
233 return true;
234
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
239
240 const uint8_t* relocs_start = RVAToPointer(base_relocation_table_.address_);
Etienne Pierre-doraye0586292019-12-10 23:05:25241 if (relocs_start == nullptr || relocs_start < start() ||
242 relocs_start >= end())
243 return Bad(".relocs outside image");
etiennep5ac34c92016-05-20 15:44:46244
245 // Make sure entire base relocation table is within the buffer.
Etienne Pierre-doraye0586292019-12-10 23:05:25246 if (relocs_size > static_cast<size_t>(end() - relocs_start))
etiennep5ac34c92016-05-20 15:44:46247 return Bad(".relocs outside image");
Etienne Pierre-doraye0586292019-12-10 23:05:25248 const uint8_t* relocs_end = relocs_start + relocs_size;
etiennep5ac34c92016-05-20 15:44:46249
250 const uint8_t* block = relocs_start;
251
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");
259
260 const uint8_t* end_entries = block + size;
261
262 if (end_entries <= block || end_entries <= start() || end_entries > end())
263 return Bad(".relocs block outside image");
264
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;
270
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) {
277 continue;
278 }
279
280 if (SupportsRelTableType(type)) {
281 relocs->push_back(rva);
282 } else if (type == 0) { // IMAGE_REL_BASED_ABSOLUTE
283 // Ignore, used as padding.
284 } else {
285 // Does not occur in Windows x86/x64 executables.
286 return Bad("Unknown type of reloc");
287 }
288 }
289
290 block += size;
291 }
292
293 std::sort(relocs->begin(), relocs->end());
294 DCHECK(relocs->empty() || relocs->back() != kUnassignedRVA);
295
296 return true;
297}
298
299const Section* DisassemblerWin32::RVAToSection(RVA rva) const {
300 for (int i = 0; i < number_of_sections_; ++i) {
301 const Section* section = &sections_[i];
302 if (rva >= section->virtual_address) {
303 FileOffset offset_in_section = rva - section->virtual_address;
304 if (offset_in_section < section->virtual_size)
305 return section;
306 }
307 }
308 return nullptr;
309}
310
311std::string DisassemblerWin32::SectionName(const Section* section) {
312 if (section == nullptr)
313 return "<none>";
314 char name[9];
315 memcpy(name, section->name, 8);
316 name[8] = '\0'; // Ensure termination.
317 return name;
318}
319
etiennep5059bca2016-07-08 17:55:20320// static
321bool DisassemblerWin32::QuickDetect(const uint8_t* start,
322 size_t length,
323 uint16_t magic) {
Samuel Huanga34dce52019-04-03 15:53:13324 if (length < kOffsetOfFileAddressOfNewExeHeader + 4)
etiennep5059bca2016-07-08 17:55:20325 return false;
326
327 // Have 'MZ' magic for a DOS header?
328 if (start[0] != 'M' || start[1] != 'Z')
329 return false;
330
Samuel Huanga34dce52019-04-03 15:53:13331 FileOffset pe_header_offset = static_cast<FileOffset>(
etiennep5059bca2016-07-08 17:55:20332 ReadU32(start, kOffsetOfFileAddressOfNewExeHeader));
Samuel Huanga34dce52019-04-03 15:53:13333 if (pe_header_offset % 8 != 0 ||
334 pe_header_offset < kOffsetOfFileAddressOfNewExeHeader + 4 ||
335 pe_header_offset >= length ||
336 length - pe_header_offset < kMinPeHeaderSize) {
etiennep5059bca2016-07-08 17:55:20337 return false;
Samuel Huanga34dce52019-04-03 15:53:13338 }
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)) {
etiennep5059bca2016-07-08 17:55:20342 return false;
Samuel Huanga34dce52019-04-03 15:53:13343 }
etiennep5059bca2016-07-08 17:55:20344
Samuel Huanga34dce52019-04-03 15:53:13345 FileOffset optional_header_offset = pe_header_offset + kMinPeHeaderSize;
346 if (optional_header_offset >= length || length - optional_header_offset < 2)
etiennep5059bca2016-07-08 17:55:20347 return false;
Samuel Huanga34dce52019-04-03 15:53:13348 const uint8_t* optional_header = start + optional_header_offset;
349 return magic == ReadU16(optional_header, 0);
350}
etiennep5059bca2016-07-08 17:55:20351
Samuel Huanga34dce52019-04-03 15:53:13352bool DisassemblerWin32::IsRvaRangeInBounds(size_t start, size_t length) {
353 return start < size_of_image_ && length <= size_of_image_ - start;
354}
355
356bool DisassemblerWin32::CheckSectionRanges() {
357 for (int i = 0; i < number_of_sections_; ++i) {
358 const Section* section = &sections_[i];
359 if (!IsRangeInBounds(section->file_offset_of_raw_data,
360 section->size_of_raw_data) ||
361 !IsRvaRangeInBounds(section->virtual_address, section->virtual_size)) {
362 return false;
363 }
364 }
etiennep5059bca2016-07-08 17:55:20365 return true;
366}
367
huangs257f9fb2017-03-23 23:17:50368bool DisassemblerWin32::ExtractAbs32Locations() {
huangsdc779d92017-03-01 22:22:38369 abs32_locations_.clear();
370 if (!ParseRelocs(&abs32_locations_))
371 return false;
372
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))];
378 }
379#endif
380 return true;
381}
382
huangs257f9fb2017-03-23 23:17:50383bool DisassemblerWin32::ExtractRel32Locations() {
huangsdc779d92017-03-01 22:22:38384 FileOffset file_offset = 0;
385 while (file_offset < length()) {
386 const Section* section = FindNextSection(file_offset);
387 if (section == nullptr)
388 break;
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;
393 }
394 std::sort(rel32_locations_.begin(), rel32_locations_.end());
395 DCHECK(rel32_locations_.empty() || rel32_locations_.back() != kUnassignedRVA);
396
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();
402
403 int common = 0;
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) {
409 ++abs32_iter;
410 } else if (rel32_iter->first < abs32_iter->first) {
411 ++rel32_iter;
412 } else {
413 ++common;
414 ++abs32_iter;
415 ++rel32_iter;
416 }
417 }
418 VLOG(1) << "common " << common;
419#endif
huangs257f9fb2017-03-23 23:17:50420 return true;
huangsdc779d92017-03-01 22:22:38421}
422
etiennep5ac34c92016-05-20 15:44:46423RvaVisitor* DisassemblerWin32::CreateAbs32TargetRvaVisitor() {
424 return new RvaVisitor_Abs32(abs32_locations_, *this);
425}
426
427RvaVisitor* DisassemblerWin32::CreateRel32TargetRvaVisitor() {
428 return new RvaVisitor_Rel32(rel32_locations_, *this);
429}
430
431void 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;
437 };
438 rel32_locations_.erase(
439 std::remove_if(rel32_locations_.begin(), rel32_locations_.end(), cond),
440 rel32_locations_.end());
441}
442
huangs257f9fb2017-03-23 23:17:50443InstructionGenerator DisassemblerWin32::GetInstructionGenerator(
444 AssemblyProgram* program) {
tzik0881a4002018-03-08 06:19:49445 return base::BindRepeating(&DisassemblerWin32::ParseFile,
446 base::Unretained(this), program);
huangs257f9fb2017-03-23 23:17:50447}
448
huangs7b221a52016-11-09 22:28:23449CheckBool DisassemblerWin32::ParseFile(AssemblyProgram* program,
450 InstructionReceptor* receptor) const {
etiennep5ac34c92016-05-20 15:44:46451 // 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
457 // section.
huangs7b221a52016-11-09 22:28:23458 // ParseNonSectionFileRegion(file_offset, pe_info().length(), receptor);
etiennep5ac34c92016-05-20 15:44:46459 break;
460 }
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,
huangs7b221a52016-11-09 22:28:23464 receptor)) {
etiennep5ac34c92016-05-20 15:44:46465 return false;
466 }
467
468 file_offset = section_start_offset;
469 }
470 FileOffset end = file_offset + section->size_of_raw_data;
huangs7b221a52016-11-09 22:28:23471 if (!ParseFileRegion(section, file_offset, end, program, receptor))
etiennep5ac34c92016-05-20 15:44:46472 return false;
473 file_offset = end;
474 }
475
476#if COURGETTE_HISTOGRAM_TARGETS
477 HistogramTargets("abs32 relocs", abs32_target_rvas_);
478 HistogramTargets("rel32 relocs", rel32_target_rvas_);
479#endif
480
481 return true;
482}
483
etiennep5ac34c92016-05-20 15:44:46484CheckBool DisassemblerWin32::ParseNonSectionFileRegion(
485 FileOffset start_file_offset,
486 FileOffset end_file_offset,
huangs7b221a52016-11-09 22:28:23487 InstructionReceptor* receptor) const {
etiennep5ac34c92016-05-20 15:44:46488 if (incomplete_disassembly_)
489 return true;
490
491 if (end_file_offset > start_file_offset) {
huangs7b221a52016-11-09 22:28:23492 if (!receptor->EmitMultipleBytes(FileOffsetToPointer(start_file_offset),
493 end_file_offset - start_file_offset)) {
etiennep5ac34c92016-05-20 15:44:46494 return false;
495 }
496 }
497
498 return true;
499}
500
huangs7b221a52016-11-09 22:28:23501CheckBool DisassemblerWin32::ParseFileRegion(
502 const Section* section,
503 FileOffset start_file_offset,
504 FileOffset end_file_offset,
505 AssemblyProgram* program,
506 InstructionReceptor* receptor) const {
etiennep5ac34c92016-05-20 15:44:46507 RVA relocs_start_rva = base_relocation_table().address_;
508
509 const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
510 const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
511
512 RVA start_rva = FileOffsetToRVA(start_file_offset);
513 RVA end_rva = start_rva + section->virtual_size;
514 const int kVAWidth = AbsVAWidth();
515
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;
519
huangs7b221a52016-11-09 22:28:23520 std::vector<RVA>::const_iterator rel32_pos = rel32_locations_.begin();
521 std::vector<RVA>::const_iterator abs32_pos = abs32_locations_.begin();
etiennep5ac34c92016-05-20 15:44:46522
huangs7b221a52016-11-09 22:28:23523 if (!receptor->EmitOrigin(start_rva))
etiennep5ac34c92016-05-20 15:44:46524 return false;
525
526 const uint8_t* p = start_pointer;
527
528 while (p < end_pointer) {
529 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva);
530
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
533 // during assembly.
534 if (current_rva == relocs_start_rva) {
huangs7b221a52016-11-09 22:28:23535 if (!receptor->EmitPeRelocs())
etiennep5ac34c92016-05-20 15:44:46536 return false;
537 uint32_t relocs_size = base_relocation_table().size_;
538 if (relocs_size) {
539 p += relocs_size;
540 continue;
541 }
542 }
543
544 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva)
545 ++abs32_pos;
546
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);
553 DCHECK(label);
huangs7b221a52016-11-09 22:28:23554 if (!EmitAbs(label, receptor))
etiennep5ac34c92016-05-20 15:44:46555 return false;
556 p += kVAWidth;
557 continue;
558 }
559
560 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva)
561 ++rel32_pos;
562
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);
567 DCHECK(label);
huangs7b221a52016-11-09 22:28:23568 if (!receptor->EmitRel32(label))
etiennep5ac34c92016-05-20 15:44:46569 return false;
570 p += 4;
571 continue;
572 }
573
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.
579 break;
580 }
581 }
582
huangs7b221a52016-11-09 22:28:23583 if (!receptor->EmitSingleByte(*p))
etiennep5ac34c92016-05-20 15:44:46584 return false;
585 p += 1;
586 }
587
588 return true;
589}
590
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.
596void DisassemblerWin32::HistogramTargets(const char* kind,
huangs7b221a52016-11-09 22:28:23597 const std::map<RVA, int>& map) const {
etiennep5ac34c92016-05-20 15:44:46598 int total = 0;
599 std::map<int, std::vector<RVA>> h;
600 for (std::map<RVA, int>::const_iterator p = map.begin(); p != map.end();
601 ++p) {
602 h[p->second].push_back(p->first);
603 total += p->second;
604 }
605
606 std::cout << total << " " << kind << " to " << map.size() << " unique targets"
607 << std::endl;
608
609 std::cout << "indegree: #targets-with-indegree (example)" << std::endl;
610 const int kFirstN = 15;
611 bool someSkipped = false;
612 int index = 0;
613 for (std::map<int, std::vector<RVA>>::reverse_iterator p = h.rbegin();
614 p != h.rend(); ++p) {
615 ++index;
616 if (index <= kFirstN || p->first <= 3) {
617 if (someSkipped) {
618 std::cout << "..." << std::endl;
619 }
620 size_t count = p->second.size();
621 std::cout << std::dec << p->first << ": " << count;
622 if (count <= 2) {
623 for (size_t i = 0; i < count; ++i)
624 std::cout << " " << DescribeRVA(p->second[i]);
625 }
626 std::cout << std::endl;
627 someSkipped = false;
628 } else {
629 someSkipped = true;
630 }
631 }
632}
633#endif // COURGETTE_HISTOGRAM_TARGETS
634
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.
639std::string DisassemblerWin32::DescribeRVA(RVA rva) const {
640 const Section* section = RVAToSection(rva);
641 std::ostringstream s;
642 s << std::hex << rva;
643 if (section) {
644 s << " (";
645 s << SectionName(section) << "+" << std::hex
646 << (rva - section->virtual_address) << ")";
647 }
648 return s.str();
649}
650
651const Section* DisassemblerWin32::FindNextSection(
652 FileOffset file_offset) const {
Raul Tambre5bd92982019-03-28 06:21:55653 const Section* best = nullptr;
etiennep5ac34c92016-05-20 15:44:46654 for (int i = 0; i < number_of_sections_; ++i) {
655 const Section* section = &sections_[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) {
Raul Tambre5bd92982019-03-28 06:21:55658 if (best == nullptr ||
etiennep5ac34c92016-05-20 15:44:46659 section->file_offset_of_raw_data < best->file_offset_of_raw_data) {
660 best = section;
661 }
662 }
663 }
664 }
665 return best;
666}
667
668bool DisassemblerWin32::ReadDataDirectory(int index,
669 ImageDataDirectory* directory) {
670 if (index < number_of_data_directories_) {
Samuel Huanga34dce52019-04-03 15:53:13671 FileOffset file_offset = index * 8 + RelativeOffsetOfDataDirectories();
etiennep5ac34c92016-05-20 15:44:46672 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");
681
682 // TODO(sra): validate RVA.
683 directory->address_ = rva;
684 directory->size_ = static_cast<uint32_t>(size);
685 return true;
686 } else {
687 directory->address_ = 0;
688 directory->size_ = 0;
689 return true;
690 }
691}
692
693} // namespace courgette