| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // This file implements PEImage, a generic class to manipulate PE files. | 5 // This file implements PEImage, a generic class to manipulate PE files. |
| 6 // This file was adapted from GreenBorder's Code. | 6 // This file was adapted from GreenBorder's Code. |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include "base/win/pe_image.h" | 10 #include "base/win/pe_image.h" |
| 11 | 11 |
| 12 namespace base { | 12 namespace base { |
| 13 namespace win { | 13 namespace win { |
| 14 | 14 |
| 15 // TODO(jschuh): crbug.com/167707 Make sure this code works on 64-bit. | |
| 16 | |
| 17 // Structure to perform imports enumerations. | 15 // Structure to perform imports enumerations. |
| 18 struct EnumAllImportsStorage { | 16 struct EnumAllImportsStorage { |
| 19 PEImage::EnumImportsFunction callback; | 17 PEImage::EnumImportsFunction callback; |
| 20 PVOID cookie; | 18 PVOID cookie; |
| 21 }; | 19 }; |
| 22 | 20 |
| 23 namespace { | 21 namespace { |
| 24 | 22 |
| 25 // PdbInfo Signature | 23 // PdbInfo Signature |
| 26 const DWORD kPdbInfoSignature = 'SDSR'; | 24 const DWORD kPdbInfoSignature = 'SDSR'; |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 return NULL; | 204 return NULL; |
| 207 | 205 |
| 208 PBYTE function = reinterpret_cast<PBYTE>(RVAToAddr(*export_entry)); | 206 PBYTE function = reinterpret_cast<PBYTE>(RVAToAddr(*export_entry)); |
| 209 | 207 |
| 210 PBYTE exports = reinterpret_cast<PBYTE>( | 208 PBYTE exports = reinterpret_cast<PBYTE>( |
| 211 GetImageDirectoryEntryAddr(IMAGE_DIRECTORY_ENTRY_EXPORT)); | 209 GetImageDirectoryEntryAddr(IMAGE_DIRECTORY_ENTRY_EXPORT)); |
| 212 DWORD size = GetImageDirectoryEntrySize(IMAGE_DIRECTORY_ENTRY_EXPORT); | 210 DWORD size = GetImageDirectoryEntrySize(IMAGE_DIRECTORY_ENTRY_EXPORT); |
| 213 | 211 |
| 214 // Check for forwarded exports as a special case. | 212 // Check for forwarded exports as a special case. |
| 215 if (exports <= function && exports + size > function) | 213 if (exports <= function && exports + size > function) |
| 216 #pragma warning(push) | 214 return reinterpret_cast<FARPROC>(-1); |
| 217 #pragma warning(disable: 4312) | |
| 218 // This cast generates a warning because it is 32 bit specific. | |
| 219 return reinterpret_cast<FARPROC>(0xFFFFFFFF); | |
| 220 #pragma warning(pop) | |
| 221 | 215 |
| 222 return reinterpret_cast<FARPROC>(function); | 216 return reinterpret_cast<FARPROC>(function); |
| 223 } | 217 } |
| 224 | 218 |
| 225 bool PEImage::GetProcOrdinal(LPCSTR function_name, WORD *ordinal) const { | 219 bool PEImage::GetProcOrdinal(LPCSTR function_name, WORD *ordinal) const { |
| 226 if (NULL == ordinal) | 220 if (NULL == ordinal) |
| 227 return false; | 221 return false; |
| 228 | 222 |
| 229 PIMAGE_EXPORT_DIRECTORY exports = GetExportDirectory(); | 223 PIMAGE_EXPORT_DIRECTORY exports = GetExportDirectory(); |
| 230 | 224 |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 bool rvas = (delay_descriptor->grAttrs & dlattrRva) != 0; | 443 bool rvas = (delay_descriptor->grAttrs & dlattrRva) != 0; |
| 450 | 444 |
| 451 if (rvas) { | 445 if (rvas) { |
| 452 module_name = | 446 module_name = |
| 453 reinterpret_cast<LPCSTR>(RVAToAddr(delay_descriptor->rvaDLLName)); | 447 reinterpret_cast<LPCSTR>(RVAToAddr(delay_descriptor->rvaDLLName)); |
| 454 name_table = reinterpret_cast<PIMAGE_THUNK_DATA>( | 448 name_table = reinterpret_cast<PIMAGE_THUNK_DATA>( |
| 455 RVAToAddr(delay_descriptor->rvaINT)); | 449 RVAToAddr(delay_descriptor->rvaINT)); |
| 456 iat = reinterpret_cast<PIMAGE_THUNK_DATA>( | 450 iat = reinterpret_cast<PIMAGE_THUNK_DATA>( |
| 457 RVAToAddr(delay_descriptor->rvaIAT)); | 451 RVAToAddr(delay_descriptor->rvaIAT)); |
| 458 } else { | 452 } else { |
| 459 #pragma warning(push) | 453 // Values in IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT are 32-bit, even on 64-bit |
| 460 #pragma warning(disable: 4312) | 454 // platforms. See section 4.8 of PECOFF image spec rev 8.3. |
| 461 // These casts generate warnings because they are 32 bit specific. | 455 module_name = reinterpret_cast<LPCSTR>( |
| 462 module_name = reinterpret_cast<LPCSTR>(delay_descriptor->rvaDLLName); | 456 static_cast<uintptr_t>(delay_descriptor->rvaDLLName)); |
| 463 name_table = | 457 name_table = reinterpret_cast<PIMAGE_THUNK_DATA>( |
| 464 reinterpret_cast<PIMAGE_THUNK_DATA>(delay_descriptor->rvaINT); | 458 static_cast<uintptr_t>(delay_descriptor->rvaINT)); |
| 465 iat = reinterpret_cast<PIMAGE_THUNK_DATA>(delay_descriptor->rvaIAT); | 459 iat = reinterpret_cast<PIMAGE_THUNK_DATA>( |
| 466 #pragma warning(pop) | 460 static_cast<uintptr_t>(delay_descriptor->rvaIAT)); |
| 467 } | 461 } |
| 468 | 462 |
| 469 if (!callback(*this, delay_descriptor, module_name, name_table, iat, | 463 if (!callback(*this, delay_descriptor, module_name, name_table, iat, |
| 470 cookie)) | 464 cookie)) |
| 471 return false; | 465 return false; |
| 472 } | 466 } |
| 473 | 467 |
| 474 return true; | 468 return true; |
| 475 } | 469 } |
| 476 | 470 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 488 if (IMAGE_SNAP_BY_ORDINAL(name_table->u1.Ordinal)) { | 482 if (IMAGE_SNAP_BY_ORDINAL(name_table->u1.Ordinal)) { |
| 489 ordinal = static_cast<WORD>(IMAGE_ORDINAL32(name_table->u1.Ordinal)); | 483 ordinal = static_cast<WORD>(IMAGE_ORDINAL32(name_table->u1.Ordinal)); |
| 490 } else { | 484 } else { |
| 491 PIMAGE_IMPORT_BY_NAME import; | 485 PIMAGE_IMPORT_BY_NAME import; |
| 492 bool rvas = (delay_descriptor->grAttrs & dlattrRva) != 0; | 486 bool rvas = (delay_descriptor->grAttrs & dlattrRva) != 0; |
| 493 | 487 |
| 494 if (rvas) { | 488 if (rvas) { |
| 495 import = reinterpret_cast<PIMAGE_IMPORT_BY_NAME>( | 489 import = reinterpret_cast<PIMAGE_IMPORT_BY_NAME>( |
| 496 RVAToAddr(name_table->u1.ForwarderString)); | 490 RVAToAddr(name_table->u1.ForwarderString)); |
| 497 } else { | 491 } else { |
| 498 #pragma warning(push) | |
| 499 #pragma warning(disable: 4312) | |
| 500 // This cast generates a warning because it is 32 bit specific. | |
| 501 import = reinterpret_cast<PIMAGE_IMPORT_BY_NAME>( | 492 import = reinterpret_cast<PIMAGE_IMPORT_BY_NAME>( |
| 502 name_table->u1.ForwarderString); | 493 name_table->u1.ForwarderString); |
| 503 #pragma warning(pop) | |
| 504 } | 494 } |
| 505 | 495 |
| 506 hint = import->Hint; | 496 hint = import->Hint; |
| 507 name = reinterpret_cast<LPCSTR>(&import->Name); | 497 name = reinterpret_cast<LPCSTR>(&import->Name); |
| 508 } | 498 } |
| 509 | 499 |
| 510 if (!callback(*this, module_name, ordinal, name, hint, iat, cookie)) | 500 if (!callback(*this, module_name, ordinal, name, hint, iat, cookie)) |
| 511 return false; | 501 return false; |
| 512 } | 502 } |
| 513 | 503 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 DWORD disk_offset; | 571 DWORD disk_offset; |
| 582 | 572 |
| 583 if (!ImageAddrToOnDiskOffset(in_memory, &disk_offset)) | 573 if (!ImageAddrToOnDiskOffset(in_memory, &disk_offset)) |
| 584 return NULL; | 574 return NULL; |
| 585 | 575 |
| 586 return PEImage::RVAToAddr(disk_offset); | 576 return PEImage::RVAToAddr(disk_offset); |
| 587 } | 577 } |
| 588 | 578 |
| 589 } // namespace win | 579 } // namespace win |
| 590 } // namespace base | 580 } // namespace base |
| OLD | NEW |