| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_x86.h" | 5 #include "courgette/disassembler_win32_x86.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 | 13 |
| 14 #include "courgette/assembly_program.h" | 14 #include "courgette/assembly_program.h" |
| 15 #include "courgette/courgette.h" | 15 #include "courgette/courgette.h" |
| 16 #include "courgette/encoded_program.h" | 16 #include "courgette/encoded_program.h" |
| 17 #include "courgette/image_info.h" | |
| 18 | 17 |
| 19 // COURGETTE_HISTOGRAM_TARGETS prints out a histogram of how frequently | 18 // COURGETTE_HISTOGRAM_TARGETS prints out a histogram of how frequently |
| 20 // different target addresses are referenced. Purely for debugging. | 19 // different target addresses are referenced. Purely for debugging. |
| 21 #define COURGETTE_HISTOGRAM_TARGETS 0 | 20 #define COURGETTE_HISTOGRAM_TARGETS 0 |
| 22 | 21 |
| 23 namespace courgette { | 22 namespace courgette { |
| 24 | 23 |
| 25 DisassemblerWin32X86::DisassemblerWin32X86(PEInfo* pe_info) | 24 DisassemblerWin32X86::DisassemblerWin32X86(const void* start, size_t length) |
| 26 : pe_info_(pe_info), | 25 : Disassembler(start, length), |
| 27 incomplete_disassembly_(false) { | 26 incomplete_disassembly_(false), |
| 27 is_PE32_plus_(false), |
| 28 optional_header_(NULL), |
| 29 size_of_optional_header_(0), |
| 30 offset_of_data_directories_(0), |
| 31 machine_type_(0), |
| 32 number_of_sections_(0), |
| 33 sections_(NULL), |
| 34 has_text_section_(false), |
| 35 size_of_code_(0), |
| 36 size_of_initialized_data_(0), |
| 37 size_of_uninitialized_data_(0), |
| 38 base_of_code_(0), |
| 39 base_of_data_(0), |
| 40 image_base_(0), |
| 41 size_of_image_(0), |
| 42 number_of_data_directories_(0) { |
| 43 } |
| 44 |
| 45 // ParseHeader attempts to match up the buffer with the Windows data |
| 46 // structures that exist within a Windows 'Portable Executable' format file. |
| 47 // Returns 'true' if the buffer matches, and 'false' if the data looks |
| 48 // suspicious. Rather than try to 'map' the buffer to the numerous windows |
| 49 // structures, we extract the information we need into the courgette::PEInfo |
| 50 // structure. |
| 51 // |
| 52 bool DisassemblerWin32X86::ParseHeader() { |
| 53 if (length() < kOffsetOfFileAddressOfNewExeHeader + 4 /*size*/) |
| 54 return Bad("Too small"); |
| 55 |
| 56 // Have 'MZ' magic for a DOS header? |
| 57 if (start()[0] != 'M' || start()[1] != 'Z') |
| 58 return Bad("Not MZ"); |
| 59 |
| 60 // offset from DOS header to PE header is stored in DOS header. |
| 61 uint32 offset = ReadU32(start(), |
| 62 kOffsetOfFileAddressOfNewExeHeader); |
| 63 |
| 64 const uint8* const pe_header = OffsetToPointer(offset); |
| 65 const size_t kMinPEHeaderSize = 4 /*signature*/ + kSizeOfCoffHeader; |
| 66 if (pe_header <= start() || |
| 67 pe_header >= end() - kMinPEHeaderSize) |
| 68 return Bad("Bad offset to PE header"); |
| 69 |
| 70 if (offset % 8 != 0) |
| 71 return Bad("Misaligned PE header"); |
| 72 |
| 73 // The 'PE' header is an IMAGE_NT_HEADERS structure as defined in WINNT.H. |
| 74 // See http://msdn.microsoft.com/en-us/library/ms680336(VS.85).aspx |
| 75 // |
| 76 // The first field of the IMAGE_NT_HEADERS is the signature. |
| 77 if (!(pe_header[0] == 'P' && |
| 78 pe_header[1] == 'E' && |
| 79 pe_header[2] == 0 && |
| 80 pe_header[3] == 0)) |
| 81 return Bad("no PE signature"); |
| 82 |
| 83 // The second field of the IMAGE_NT_HEADERS is the COFF header. |
| 84 // The COFF header is also called an IMAGE_FILE_HEADER |
| 85 // http://msdn.microsoft.com/en-us/library/ms680313(VS.85).aspx |
| 86 const uint8* const coff_header = pe_header + 4; |
| 87 machine_type_ = ReadU16(coff_header, 0); |
| 88 number_of_sections_ = ReadU16(coff_header, 2); |
| 89 size_of_optional_header_ = ReadU16(coff_header, 16); |
| 90 |
| 91 // The rest of the IMAGE_NT_HEADERS is the IMAGE_OPTIONAL_HEADER(32|64) |
| 92 const uint8* const optional_header = coff_header + kSizeOfCoffHeader; |
| 93 optional_header_ = optional_header; |
| 94 |
| 95 if (optional_header + size_of_optional_header_ >= end()) |
| 96 return Bad("optional header past end of file"); |
| 97 |
| 98 // Check we can read the magic. |
| 99 if (size_of_optional_header_ < 2) |
| 100 return Bad("optional header no magic"); |
| 101 |
| 102 uint16 magic = ReadU16(optional_header, 0); |
| 103 |
| 104 if (magic == kImageNtOptionalHdr32Magic) { |
| 105 is_PE32_plus_ = false; |
| 106 offset_of_data_directories_ = |
| 107 kOffsetOfDataDirectoryFromImageOptionalHeader32; |
| 108 } else if (magic == kImageNtOptionalHdr64Magic) { |
| 109 is_PE32_plus_ = true; |
| 110 offset_of_data_directories_ = |
| 111 kOffsetOfDataDirectoryFromImageOptionalHeader64; |
| 112 } else { |
| 113 return Bad("unrecognized magic"); |
| 114 } |
| 115 |
| 116 // Check that we can read the rest of the the fixed fields. Data directories |
| 117 // directly follow the fixed fields of the IMAGE_OPTIONAL_HEADER. |
| 118 if (size_of_optional_header_ < offset_of_data_directories_) |
| 119 return Bad("optional header too short"); |
| 120 |
| 121 // The optional header is either an IMAGE_OPTIONAL_HEADER32 or |
| 122 // IMAGE_OPTIONAL_HEADER64 |
| 123 // http://msdn.microsoft.com/en-us/library/ms680339(VS.85).aspx |
| 124 // |
| 125 // Copy the fields we care about. |
| 126 size_of_code_ = ReadU32(optional_header, 4); |
| 127 size_of_initialized_data_ = ReadU32(optional_header, 8); |
| 128 size_of_uninitialized_data_ = ReadU32(optional_header, 12); |
| 129 base_of_code_ = ReadU32(optional_header, 20); |
| 130 if (is_PE32_plus_) { |
| 131 base_of_data_ = 0; |
| 132 image_base_ = ReadU64(optional_header, 24); |
| 133 } else { |
| 134 base_of_data_ = ReadU32(optional_header, 24); |
| 135 image_base_ = ReadU32(optional_header, 28); |
| 136 } |
| 137 size_of_image_ = ReadU32(optional_header, 56); |
| 138 number_of_data_directories_ = |
| 139 ReadU32(optional_header, (is_PE32_plus_ ? 108 : 92)); |
| 140 |
| 141 if (size_of_code_ >= length() || |
| 142 size_of_initialized_data_ >= length() || |
| 143 size_of_code_ + size_of_initialized_data_ >= length()) { |
| 144 // This validation fires on some perfectly fine executables. |
| 145 // return Bad("code or initialized data too big"); |
| 146 } |
| 147 |
| 148 // TODO(sra): we can probably get rid of most of the data directories. |
| 149 bool b = true; |
| 150 // 'b &= ...' could be short circuit 'b = b && ...' but it is not necessary |
| 151 // for correctness and it compiles smaller this way. |
| 152 b &= ReadDataDirectory(0, &export_table_); |
| 153 b &= ReadDataDirectory(1, &import_table_); |
| 154 b &= ReadDataDirectory(2, &resource_table_); |
| 155 b &= ReadDataDirectory(3, &exception_table_); |
| 156 b &= ReadDataDirectory(5, &base_relocation_table_); |
| 157 b &= ReadDataDirectory(11, &bound_import_table_); |
| 158 b &= ReadDataDirectory(12, &import_address_table_); |
| 159 b &= ReadDataDirectory(13, &delay_import_descriptor_); |
| 160 b &= ReadDataDirectory(14, &clr_runtime_header_); |
| 161 if (!b) { |
| 162 return Bad("malformed data directory"); |
| 163 } |
| 164 |
| 165 // Sections follow the optional header. |
| 166 sections_ = |
| 167 reinterpret_cast<const Section*>(optional_header + |
| 168 size_of_optional_header_); |
| 169 size_t detected_length = 0; |
| 170 |
| 171 for (int i = 0; i < number_of_sections_; ++i) { |
| 172 const Section* section = §ions_[i]; |
| 173 |
| 174 // TODO(sra): consider using the 'characteristics' field of the section |
| 175 // header to see if the section contains instructions. |
| 176 if (memcmp(section->name, ".text", 6) == 0) |
| 177 has_text_section_ = true; |
| 178 |
| 179 uint32 section_end = |
| 180 section->file_offset_of_raw_data + section->size_of_raw_data; |
| 181 if (section_end > detected_length) |
| 182 detected_length = section_end; |
| 183 } |
| 184 |
| 185 // Pretend our in-memory copy is only as long as our detected length. |
| 186 ReduceLength(detected_length); |
| 187 return Good(); |
| 28 } | 188 } |
| 29 | 189 |
| 30 bool DisassemblerWin32X86::Disassemble(AssemblyProgram* target) { | 190 bool DisassemblerWin32X86::Disassemble(AssemblyProgram* target) { |
| 31 if (!pe_info().ok()) | 191 if (!ok()) |
| 32 return false; | 192 return false; |
| 33 | 193 |
| 34 target->set_image_base(pe_info().image_base()); | 194 target->set_image_base(image_base()); |
| 35 | 195 |
| 36 if (!ParseAbs32Relocs()) | 196 if (!ParseAbs32Relocs()) |
| 37 return false; | 197 return false; |
| 38 | 198 |
| 39 ParseRel32RelocsFromSections(); | 199 ParseRel32RelocsFromSections(); |
| 40 | 200 |
| 41 if (!ParseFile(target)) | 201 if (!ParseFile(target)) |
| 42 return false; | 202 return false; |
| 43 | 203 |
| 44 target->DefaultAssignIndexes(); | 204 target->DefaultAssignIndexes(); |
| 45 | 205 |
| 46 return true; | 206 return true; |
| 47 } | 207 } |
| 48 | 208 |
| 49 static uint32 Read32LittleEndian(const void* address) { | 209 //////////////////////////////////////////////////////////////////////////////// |
| 50 return *reinterpret_cast<const uint32*>(address); | 210 |
| 211 bool DisassemblerWin32X86::ParseRelocs(std::vector<RVA> *relocs) { |
| 212 relocs->clear(); |
| 213 |
| 214 size_t relocs_size = base_relocation_table_.size_; |
| 215 if (relocs_size == 0) |
| 216 return true; |
| 217 |
| 218 // The format of the base relocation table is a sequence of variable sized |
| 219 // IMAGE_BASE_RELOCATION blocks. Search for |
| 220 // "The format of the base relocation data is somewhat quirky" |
| 221 // at http://msdn.microsoft.com/en-us/library/ms809762.aspx |
| 222 |
| 223 const uint8* relocs_start = RVAToPointer(base_relocation_table_.address_); |
| 224 const uint8* relocs_end = relocs_start + relocs_size; |
| 225 |
| 226 // Make sure entire base relocation table is within the buffer. |
| 227 if (relocs_start < start() || |
| 228 relocs_start >= end() || |
| 229 relocs_end <= start() || |
| 230 relocs_end > end()) { |
| 231 return Bad(".relocs outside image"); |
| 232 } |
| 233 |
| 234 const uint8* block = relocs_start; |
| 235 |
| 236 // Walk the variable sized blocks. |
| 237 while (block + 8 < relocs_end) { |
| 238 RVA page_rva = ReadU32(block, 0); |
| 239 uint32 size = ReadU32(block, 4); |
| 240 if (size < 8 || // Size includes header ... |
| 241 size % 4 != 0) // ... and is word aligned. |
| 242 return Bad("unreasonable relocs block"); |
| 243 |
| 244 const uint8* end_entries = block + size; |
| 245 |
| 246 if (end_entries <= block || |
| 247 end_entries <= start() || |
| 248 end_entries > end()) |
| 249 return Bad(".relocs block outside image"); |
| 250 |
| 251 // Walk through the two-byte entries. |
| 252 for (const uint8* p = block + 8; p < end_entries; p += 2) { |
| 253 uint16 entry = ReadU16(p, 0); |
| 254 int type = entry >> 12; |
| 255 int offset = entry & 0xFFF; |
| 256 |
| 257 RVA rva = page_rva + offset; |
| 258 if (type == 3) { // IMAGE_REL_BASED_HIGHLOW |
| 259 relocs->push_back(rva); |
| 260 } else if (type == 0) { // IMAGE_REL_BASED_ABSOLUTE |
| 261 // Ignore, used as padding. |
| 262 } else { |
| 263 // Does not occur in Windows x86 executables. |
| 264 return Bad("unknown type of reloc"); |
| 265 } |
| 266 } |
| 267 |
| 268 block += size; |
| 269 } |
| 270 |
| 271 std::sort(relocs->begin(), relocs->end()); |
| 272 |
| 273 return true; |
| 274 } |
| 275 |
| 276 const Section* DisassemblerWin32X86::RVAToSection(RVA rva) const { |
| 277 for (int i = 0; i < number_of_sections_; i++) { |
| 278 const Section* section = §ions_[i]; |
| 279 uint32 offset = rva - section->virtual_address; |
| 280 if (offset < section->virtual_size) { |
| 281 return section; |
| 282 } |
| 283 } |
| 284 return NULL; |
| 285 } |
| 286 |
| 287 int DisassemblerWin32X86::RVAToFileOffset(RVA rva) const { |
| 288 const Section* section = RVAToSection(rva); |
| 289 if (section) { |
| 290 uint32 offset = rva - section->virtual_address; |
| 291 if (offset < section->size_of_raw_data) { |
| 292 return section->file_offset_of_raw_data + offset; |
| 293 } else { |
| 294 return kNoOffset; // In section but not in file (e.g. uninit data). |
| 295 } |
| 296 } |
| 297 |
| 298 // Small RVA values point into the file header in the loaded image. |
| 299 // RVA 0 is the module load address which Windows uses as the module handle. |
| 300 // RVA 2 sometimes occurs, I'm not sure what it is, but it would map into the |
| 301 // DOS header. |
| 302 if (rva == 0 || rva == 2) |
| 303 return rva; |
| 304 |
| 305 NOTREACHED(); |
| 306 return kNoOffset; |
| 307 } |
| 308 |
| 309 const uint8* DisassemblerWin32X86::RVAToPointer(RVA rva) const { |
| 310 int file_offset = RVAToFileOffset(rva); |
| 311 if (file_offset == kNoOffset) |
| 312 return NULL; |
| 313 else |
| 314 return OffsetToPointer(file_offset); |
| 315 } |
| 316 |
| 317 std::string DisassemblerWin32X86::SectionName(const Section* section) { |
| 318 if (section == NULL) |
| 319 return "<none>"; |
| 320 char name[9]; |
| 321 memcpy(name, section->name, 8); |
| 322 name[8] = '\0'; // Ensure termination. |
| 323 return name; |
| 324 } |
| 325 |
| 326 CheckBool DisassemblerWin32X86::ParseFile(AssemblyProgram* program) { |
| 327 bool ok = true; |
| 328 // Walk all the bytes in the file, whether or not in a section. |
| 329 uint32 file_offset = 0; |
| 330 while (ok && file_offset < length()) { |
| 331 const Section* section = FindNextSection(file_offset); |
| 332 if (section == NULL) { |
| 333 // No more sections. There should not be extra stuff following last |
| 334 // section. |
| 335 // ParseNonSectionFileRegion(file_offset, pe_info().length(), program); |
| 336 break; |
| 337 } |
| 338 if (file_offset < section->file_offset_of_raw_data) { |
| 339 uint32 section_start_offset = section->file_offset_of_raw_data; |
| 340 ok = ParseNonSectionFileRegion(file_offset, section_start_offset, |
| 341 program); |
| 342 file_offset = section_start_offset; |
| 343 } |
| 344 if (ok) { |
| 345 uint32 end = file_offset + section->size_of_raw_data; |
| 346 ok = ParseFileRegion(section, file_offset, end, program); |
| 347 file_offset = end; |
| 348 } |
| 349 } |
| 350 |
| 351 #if COURGETTE_HISTOGRAM_TARGETS |
| 352 HistogramTargets("abs32 relocs", abs32_target_rvas_); |
| 353 HistogramTargets("rel32 relocs", rel32_target_rvas_); |
| 354 #endif |
| 355 |
| 356 return ok; |
| 51 } | 357 } |
| 52 | 358 |
| 53 bool DisassemblerWin32X86::ParseAbs32Relocs() { | 359 bool DisassemblerWin32X86::ParseAbs32Relocs() { |
| 54 abs32_locations_.clear(); | 360 abs32_locations_.clear(); |
| 55 if (!pe_info().ParseRelocs(&abs32_locations_)) | 361 if (!ParseRelocs(&abs32_locations_)) |
| 56 return false; | 362 return false; |
| 57 | 363 |
| 58 std::sort(abs32_locations_.begin(), abs32_locations_.end()); | 364 std::sort(abs32_locations_.begin(), abs32_locations_.end()); |
| 59 | 365 |
| 60 #if COURGETTE_HISTOGRAM_TARGETS | 366 #if COURGETTE_HISTOGRAM_TARGETS |
| 61 for (size_t i = 0; i < abs32_locations_.size(); ++i) { | 367 for (size_t i = 0; i < abs32_locations_.size(); ++i) { |
| 62 RVA rva = abs32_locations_[i]; | 368 RVA rva = abs32_locations_[i]; |
| 63 // The 4 bytes at the relocation are a reference to some address. | 369 // The 4 bytes at the relocation are a reference to some address. |
| 64 uint32 target_address = Read32LittleEndian(pe_info().RVAToPointer(rva)); | 370 uint32 target_address = Read32LittleEndian(RVAToPointer(rva)); |
| 65 ++abs32_target_rvas_[target_address - pe_info().image_base()]; | 371 ++abs32_target_rvas_[target_address - image_base()]; |
| 66 } | 372 } |
| 67 #endif | 373 #endif |
| 68 return true; | 374 return true; |
| 69 } | 375 } |
| 70 | 376 |
| 71 void DisassemblerWin32X86::ParseRel32RelocsFromSections() { | 377 void DisassemblerWin32X86::ParseRel32RelocsFromSections() { |
| 72 uint32 file_offset = 0; | 378 uint32 file_offset = 0; |
| 73 while (file_offset < pe_info().length()) { | 379 while (file_offset < length()) { |
| 74 const Section* section = pe_info().FindNextSection(file_offset); | 380 const Section* section = FindNextSection(file_offset); |
| 75 if (section == NULL) | 381 if (section == NULL) |
| 76 break; | 382 break; |
| 77 if (file_offset < section->file_offset_of_raw_data) | 383 if (file_offset < section->file_offset_of_raw_data) |
| 78 file_offset = section->file_offset_of_raw_data; | 384 file_offset = section->file_offset_of_raw_data; |
| 79 ParseRel32RelocsFromSection(section); | 385 ParseRel32RelocsFromSection(section); |
| 80 file_offset += section->size_of_raw_data; | 386 file_offset += section->size_of_raw_data; |
| 81 } | 387 } |
| 82 std::sort(rel32_locations_.begin(), rel32_locations_.end()); | 388 std::sort(rel32_locations_.begin(), rel32_locations_.end()); |
| 83 | 389 |
| 84 #if COURGETTE_HISTOGRAM_TARGETS | 390 #if COURGETTE_HISTOGRAM_TARGETS |
| (...skipping 22 matching lines...) Expand all Loading... |
| 107 } | 413 } |
| 108 | 414 |
| 109 void DisassemblerWin32X86::ParseRel32RelocsFromSection(const Section* section) { | 415 void DisassemblerWin32X86::ParseRel32RelocsFromSection(const Section* section) { |
| 110 // TODO(sra): use characteristic. | 416 // TODO(sra): use characteristic. |
| 111 bool isCode = strcmp(section->name, ".text") == 0; | 417 bool isCode = strcmp(section->name, ".text") == 0; |
| 112 if (!isCode) | 418 if (!isCode) |
| 113 return; | 419 return; |
| 114 | 420 |
| 115 uint32 start_file_offset = section->file_offset_of_raw_data; | 421 uint32 start_file_offset = section->file_offset_of_raw_data; |
| 116 uint32 end_file_offset = start_file_offset + section->size_of_raw_data; | 422 uint32 end_file_offset = start_file_offset + section->size_of_raw_data; |
| 117 RVA relocs_start_rva = pe_info().base_relocation_table().address_; | 423 RVA relocs_start_rva = base_relocation_table().address_; |
| 118 | 424 |
| 119 const uint8* start_pointer = pe_info().FileOffsetToPointer(start_file_offset); | 425 const uint8* start_pointer = OffsetToPointer(start_file_offset); |
| 120 const uint8* end_pointer = pe_info().FileOffsetToPointer(end_file_offset); | 426 const uint8* end_pointer = OffsetToPointer(end_file_offset); |
| 121 | 427 |
| 122 RVA start_rva = pe_info().FileOffsetToRVA(start_file_offset); | 428 RVA start_rva = FileOffsetToRVA(start_file_offset); |
| 123 RVA end_rva = start_rva + section->virtual_size; | 429 RVA end_rva = start_rva + section->virtual_size; |
| 124 | 430 |
| 125 // Quick way to convert from Pointer to RVA within a single Section is to | 431 // Quick way to convert from Pointer to RVA within a single Section is to |
| 126 // subtract 'pointer_to_rva'. | 432 // subtract 'pointer_to_rva'. |
| 127 const uint8* const adjust_pointer_to_rva = start_pointer - start_rva; | 433 const uint8* const adjust_pointer_to_rva = start_pointer - start_rva; |
| 128 | 434 |
| 129 std::vector<RVA>::iterator abs32_pos = abs32_locations_.begin(); | 435 std::vector<RVA>::iterator abs32_pos = abs32_locations_.begin(); |
| 130 | 436 |
| 131 // Find the rel32 relocations. | 437 // Find the rel32 relocations. |
| 132 const uint8* p = start_pointer; | 438 const uint8* p = start_pointer; |
| 133 while (p < end_pointer) { | 439 while (p < end_pointer) { |
| 134 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); | 440 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); |
| 135 if (current_rva == relocs_start_rva) { | 441 if (current_rva == relocs_start_rva) { |
| 136 uint32 relocs_size = pe_info().base_relocation_table().size_; | 442 uint32 relocs_size = base_relocation_table().size_; |
| 137 if (relocs_size) { | 443 if (relocs_size) { |
| 138 p += relocs_size; | 444 p += relocs_size; |
| 139 continue; | 445 continue; |
| 140 } | 446 } |
| 141 } | 447 } |
| 142 | 448 |
| 143 //while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva) | 449 //while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva) |
| 144 // ++abs32_pos; | 450 // ++abs32_pos; |
| 145 | 451 |
| 146 // Heuristic discovery of rel32 locations in instruction stream: are the | 452 // Heuristic discovery of rel32 locations in instruction stream: are the |
| (...skipping 25 matching lines...) Expand all Loading... |
| 172 // Beginning of abs32 reloc is before end of rel32 reloc so they | 478 // Beginning of abs32 reloc is before end of rel32 reloc so they |
| 173 // overlap. Skip four bytes past the abs32 reloc. | 479 // overlap. Skip four bytes past the abs32 reloc. |
| 174 p += (*abs32_pos + 4) - current_rva; | 480 p += (*abs32_pos + 4) - current_rva; |
| 175 continue; | 481 continue; |
| 176 } | 482 } |
| 177 } | 483 } |
| 178 | 484 |
| 179 RVA target_rva = rel32_rva + 4 + Read32LittleEndian(rel32); | 485 RVA target_rva = rel32_rva + 4 + Read32LittleEndian(rel32); |
| 180 // To be valid, rel32 target must be within image, and within this | 486 // To be valid, rel32 target must be within image, and within this |
| 181 // section. | 487 // section. |
| 182 if (pe_info().IsValidRVA(target_rva) && | 488 if (IsValidRVA(target_rva) && |
| 183 start_rva <= target_rva && target_rva < end_rva) { | 489 start_rva <= target_rva && target_rva < end_rva) { |
| 184 rel32_locations_.push_back(rel32_rva); | 490 rel32_locations_.push_back(rel32_rva); |
| 185 #if COURGETTE_HISTOGRAM_TARGETS | 491 #if COURGETTE_HISTOGRAM_TARGETS |
| 186 ++rel32_target_rvas_[target_rva]; | 492 ++rel32_target_rvas_[target_rva]; |
| 187 #endif | 493 #endif |
| 188 p += 4; | 494 p += 4; |
| 189 continue; | 495 continue; |
| 190 } | 496 } |
| 191 } | 497 } |
| 192 p += 1; | 498 p += 1; |
| 193 } | 499 } |
| 194 } | 500 } |
| 195 | 501 |
| 196 CheckBool DisassemblerWin32X86::ParseFile(AssemblyProgram* program) { | |
| 197 bool ok = true; | |
| 198 // Walk all the bytes in the file, whether or not in a section. | |
| 199 uint32 file_offset = 0; | |
| 200 while (ok && file_offset < pe_info().length()) { | |
| 201 const Section* section = pe_info().FindNextSection(file_offset); | |
| 202 if (section == NULL) { | |
| 203 // No more sections. There should not be extra stuff following last | |
| 204 // section. | |
| 205 // ParseNonSectionFileRegion(file_offset, pe_info().length(), program); | |
| 206 break; | |
| 207 } | |
| 208 if (file_offset < section->file_offset_of_raw_data) { | |
| 209 uint32 section_start_offset = section->file_offset_of_raw_data; | |
| 210 ok = ParseNonSectionFileRegion(file_offset, section_start_offset, | |
| 211 program); | |
| 212 file_offset = section_start_offset; | |
| 213 } | |
| 214 if (ok) { | |
| 215 uint32 end = file_offset + section->size_of_raw_data; | |
| 216 ok = ParseFileRegion(section, file_offset, end, program); | |
| 217 file_offset = end; | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 #if COURGETTE_HISTOGRAM_TARGETS | |
| 222 HistogramTargets("abs32 relocs", abs32_target_rvas_); | |
| 223 HistogramTargets("rel32 relocs", rel32_target_rvas_); | |
| 224 #endif | |
| 225 | |
| 226 return ok; | |
| 227 } | |
| 228 | |
| 229 CheckBool DisassemblerWin32X86::ParseNonSectionFileRegion( | 502 CheckBool DisassemblerWin32X86::ParseNonSectionFileRegion( |
| 230 uint32 start_file_offset, | 503 uint32 start_file_offset, |
| 231 uint32 end_file_offset, | 504 uint32 end_file_offset, |
| 232 AssemblyProgram* program) { | 505 AssemblyProgram* program) { |
| 233 if (incomplete_disassembly_) | 506 if (incomplete_disassembly_) |
| 234 return true; | 507 return true; |
| 235 | 508 |
| 236 const uint8* start = pe_info().FileOffsetToPointer(start_file_offset); | 509 const uint8* start = OffsetToPointer(start_file_offset); |
| 237 const uint8* end = pe_info().FileOffsetToPointer(end_file_offset); | 510 const uint8* end = OffsetToPointer(end_file_offset); |
| 238 | 511 |
| 239 const uint8* p = start; | 512 const uint8* p = start; |
| 240 | 513 |
| 241 bool ok = true; | 514 bool ok = true; |
| 242 while (p < end && ok) { | 515 while (p < end && ok) { |
| 243 ok = program->EmitByteInstruction(*p); | 516 ok = program->EmitByteInstruction(*p); |
| 244 ++p; | 517 ++p; |
| 245 } | 518 } |
| 246 | 519 |
| 247 return ok; | 520 return ok; |
| 248 } | 521 } |
| 249 | 522 |
| 250 CheckBool DisassemblerWin32X86::ParseFileRegion( | 523 CheckBool DisassemblerWin32X86::ParseFileRegion( |
| 251 const Section* section, | 524 const Section* section, |
| 252 uint32 start_file_offset, uint32 end_file_offset, | 525 uint32 start_file_offset, uint32 end_file_offset, |
| 253 AssemblyProgram* program) { | 526 AssemblyProgram* program) { |
| 254 RVA relocs_start_rva = pe_info().base_relocation_table().address_; | 527 RVA relocs_start_rva = base_relocation_table().address_; |
| 255 | 528 |
| 256 const uint8* start_pointer = pe_info().FileOffsetToPointer(start_file_offset); | 529 const uint8* start_pointer = OffsetToPointer(start_file_offset); |
| 257 const uint8* end_pointer = pe_info().FileOffsetToPointer(end_file_offset); | 530 const uint8* end_pointer = OffsetToPointer(end_file_offset); |
| 258 | 531 |
| 259 RVA start_rva = pe_info().FileOffsetToRVA(start_file_offset); | 532 RVA start_rva = FileOffsetToRVA(start_file_offset); |
| 260 RVA end_rva = start_rva + section->virtual_size; | 533 RVA end_rva = start_rva + section->virtual_size; |
| 261 | 534 |
| 262 // Quick way to convert from Pointer to RVA within a single Section is to | 535 // Quick way to convert from Pointer to RVA within a single Section is to |
| 263 // subtract 'pointer_to_rva'. | 536 // subtract 'pointer_to_rva'. |
| 264 const uint8* const adjust_pointer_to_rva = start_pointer - start_rva; | 537 const uint8* const adjust_pointer_to_rva = start_pointer - start_rva; |
| 265 | 538 |
| 266 std::vector<RVA>::iterator rel32_pos = rel32_locations_.begin(); | 539 std::vector<RVA>::iterator rel32_pos = rel32_locations_.begin(); |
| 267 std::vector<RVA>::iterator abs32_pos = abs32_locations_.begin(); | 540 std::vector<RVA>::iterator abs32_pos = abs32_locations_.begin(); |
| 268 | 541 |
| 269 bool ok = program->EmitOriginInstruction(start_rva); | 542 bool ok = program->EmitOriginInstruction(start_rva); |
| 270 | 543 |
| 271 const uint8* p = start_pointer; | 544 const uint8* p = start_pointer; |
| 272 | 545 |
| 273 while (ok && p < end_pointer) { | 546 while (ok && p < end_pointer) { |
| 274 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); | 547 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); |
| 275 | 548 |
| 276 // The base relocation table is usually in the .relocs section, but it could | 549 // The base relocation table is usually in the .relocs section, but it could |
| 277 // actually be anywhere. Make sure we skip it because we will regenerate it | 550 // actually be anywhere. Make sure we skip it because we will regenerate it |
| 278 // during assembly. | 551 // during assembly. |
| 279 if (current_rva == relocs_start_rva) { | 552 if (current_rva == relocs_start_rva) { |
| 280 ok = program->EmitMakeRelocsInstruction(); | 553 ok = program->EmitMakeRelocsInstruction(); |
| 281 if (!ok) | 554 if (!ok) |
| 282 break; | 555 break; |
| 283 uint32 relocs_size = pe_info().base_relocation_table().size_; | 556 uint32 relocs_size = base_relocation_table().size_; |
| 284 if (relocs_size) { | 557 if (relocs_size) { |
| 285 p += relocs_size; | 558 p += relocs_size; |
| 286 continue; | 559 continue; |
| 287 } | 560 } |
| 288 } | 561 } |
| 289 | 562 |
| 290 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva) | 563 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva) |
| 291 ++abs32_pos; | 564 ++abs32_pos; |
| 292 | 565 |
| 293 if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) { | 566 if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) { |
| 294 uint32 target_address = Read32LittleEndian(p); | 567 uint32 target_address = Read32LittleEndian(p); |
| 295 RVA target_rva = target_address - pe_info().image_base(); | 568 RVA target_rva = target_address - image_base(); |
| 296 // TODO(sra): target could be Label+offset. It is not clear how to guess | 569 // TODO(sra): target could be Label+offset. It is not clear how to guess |
| 297 // which it might be. We assume offset==0. | 570 // which it might be. We assume offset==0. |
| 298 ok = program->EmitAbs32(program->FindOrMakeAbs32Label(target_rva)); | 571 ok = program->EmitAbs32(program->FindOrMakeAbs32Label(target_rva)); |
| 299 if (!ok) | 572 if (!ok) |
| 300 break; | 573 break; |
| 301 p += 4; | 574 p += 4; |
| 302 continue; | 575 continue; |
| 303 } | 576 } |
| 304 | 577 |
| 305 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva) | 578 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 ++p) { | 629 ++p) { |
| 357 ++index; | 630 ++index; |
| 358 if (index <= kFirstN || p->first <= 3) { | 631 if (index <= kFirstN || p->first <= 3) { |
| 359 if (someSkipped) { | 632 if (someSkipped) { |
| 360 std::cout << "..." << std::endl; | 633 std::cout << "..." << std::endl; |
| 361 } | 634 } |
| 362 size_t count = p->second.size(); | 635 size_t count = p->second.size(); |
| 363 std::cout << std::dec << p->first << ": " << count; | 636 std::cout << std::dec << p->first << ": " << count; |
| 364 if (count <= 2) { | 637 if (count <= 2) { |
| 365 for (size_t i = 0; i < count; ++i) | 638 for (size_t i = 0; i < count; ++i) |
| 366 std::cout << " " << pe_info().DescribeRVA(p->second[i]); | 639 std::cout << " " << DescribeRVA(p->second[i]); |
| 367 } | 640 } |
| 368 std::cout << std::endl; | 641 std::cout << std::endl; |
| 369 someSkipped = false; | 642 someSkipped = false; |
| 370 } else { | 643 } else { |
| 371 someSkipped = true; | 644 someSkipped = true; |
| 372 } | 645 } |
| 373 } | 646 } |
| 374 } | 647 } |
| 375 #endif // COURGETTE_HISTOGRAM_TARGETS | 648 #endif // COURGETTE_HISTOGRAM_TARGETS |
| 376 | 649 |
| 650 |
| 651 // DescribeRVA is for debugging only. I would put it under #ifdef DEBUG except |
| 652 // that during development I'm finding I need to call it when compiled in |
| 653 // Release mode. Hence: |
| 654 // TODO(sra): make this compile only for debug mode. |
| 655 std::string DisassemblerWin32X86::DescribeRVA(RVA rva) const { |
| 656 const Section* section = RVAToSection(rva); |
| 657 std::ostringstream s; |
| 658 s << std::hex << rva; |
| 659 if (section) { |
| 660 s << " ("; |
| 661 s << SectionName(section) << "+" |
| 662 << std::hex << (rva - section->virtual_address) |
| 663 << ")"; |
| 664 } |
| 665 return s.str(); |
| 666 } |
| 667 |
| 668 const Section* DisassemblerWin32X86::FindNextSection(uint32 fileOffset) const { |
| 669 const Section* best = 0; |
| 670 for (int i = 0; i < number_of_sections_; i++) { |
| 671 const Section* section = §ions_[i]; |
| 672 if (section->size_of_raw_data > 0) { // i.e. has data in file. |
| 673 if (fileOffset <= section->file_offset_of_raw_data) { |
| 674 if (best == 0 || |
| 675 section->file_offset_of_raw_data < best->file_offset_of_raw_data) { |
| 676 best = section; |
| 677 } |
| 678 } |
| 679 } |
| 680 } |
| 681 return best; |
| 682 } |
| 683 |
| 684 RVA DisassemblerWin32X86::FileOffsetToRVA(uint32 file_offset) const { |
| 685 for (int i = 0; i < number_of_sections_; i++) { |
| 686 const Section* section = §ions_[i]; |
| 687 uint32 offset = file_offset - section->file_offset_of_raw_data; |
| 688 if (offset < section->size_of_raw_data) { |
| 689 return section->virtual_address + offset; |
| 690 } |
| 691 } |
| 692 return 0; |
| 693 } |
| 694 |
| 695 bool DisassemblerWin32X86::ReadDataDirectory( |
| 696 int index, |
| 697 ImageDataDirectory* directory) { |
| 698 |
| 699 if (index < number_of_data_directories_) { |
| 700 size_t offset = index * 8 + offset_of_data_directories_; |
| 701 if (offset >= size_of_optional_header_) |
| 702 return Bad("number of data directories inconsistent"); |
| 703 const uint8* data_directory = optional_header_ + offset; |
| 704 if (data_directory < start() || |
| 705 data_directory + 8 >= end()) |
| 706 return Bad("data directory outside image"); |
| 707 RVA rva = ReadU32(data_directory, 0); |
| 708 size_t size = ReadU32(data_directory, 4); |
| 709 if (size > size_of_image_) |
| 710 return Bad("data directory size too big"); |
| 711 |
| 712 // TODO(sra): validate RVA. |
| 713 directory->address_ = rva; |
| 714 directory->size_ = static_cast<uint32>(size); |
| 715 return true; |
| 716 } else { |
| 717 directory->address_ = 0; |
| 718 directory->size_ = 0; |
| 719 return true; |
| 720 } |
| 721 } |
| 722 |
| 377 } // namespace courgette | 723 } // namespace courgette |
| OLD | NEW |