diff options
author | csharp@chromium.org <csharp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-12 04:47:05 +0000 |
---|---|---|
committer | csharp@chromium.org <csharp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-12 04:47:05 +0000 |
commit | b2de31ca8b5029ad22d54cf9d6614bd1c3b61ce7 (patch) | |
tree | 6ab080b9899c7291427ea1571b7770b75fef419f /chrome_elf | |
parent | d5681e25a5f783677c981d72db0f89509eb836af (diff) | |
download | chromium_src-b2de31ca8b5029ad22d54cf9d6614bd1c3b61ce7.zip chromium_src-b2de31ca8b5029ad22d54cf9d6614bd1c3b61ce7.tar.gz chromium_src-b2de31ca8b5029ad22d54cf9d6614bd1c3b61ce7.tar.bz2 |
Use PEImage in ntdll_cache.cc
Use the exported functions iterator in PEImage instead of writing our
own.
BUG=
Review URL: https://codereview.chromium.org/192933003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@256424 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome_elf')
-rw-r--r-- | chrome_elf/chrome_elf.gyp | 1 | ||||
-rw-r--r-- | chrome_elf/ntdll_cache.cc | 59 |
2 files changed, 24 insertions, 36 deletions
diff --git a/chrome_elf/chrome_elf.gyp b/chrome_elf/chrome_elf.gyp index 165507d..21ab568 100644 --- a/chrome_elf/chrome_elf.gyp +++ b/chrome_elf/chrome_elf.gyp @@ -124,6 +124,7 @@ ], 'dependencies': [ 'chrome_elf_common', + '../base/base.gyp:base_static', ], }, { diff --git a/chrome_elf/ntdll_cache.cc b/chrome_elf/ntdll_cache.cc index e550442..2f4dbdf 100644 --- a/chrome_elf/ntdll_cache.cc +++ b/chrome_elf/ntdll_cache.cc @@ -5,47 +5,34 @@ #include <stdint.h> #include <windows.h> +#include "base/win/pe_image.h" #include "chrome_elf/ntdll_cache.h" FunctionLookupTable g_ntdll_lookup; +namespace { + +bool EnumExportsCallback(const base::win::PEImage& image, + DWORD ordinal, + DWORD hint, + LPCSTR name, + PVOID function_addr, + LPCSTR forward, + PVOID cookie) { + // Our lookup only cares about named functions that are in ntdll, so skip + // unnamed or forwarded exports. + if (name && function_addr) + g_ntdll_lookup[std::string(name)] = function_addr; + + return true; +} + +} // namespace + void InitCache() { HMODULE ntdll_handle = ::GetModuleHandle(L"ntdll.dll"); - // To find the Export Address Table address, we start from the DOS header. - // The module handle is actually the address of the header. - IMAGE_DOS_HEADER* dos_header = - reinterpret_cast<IMAGE_DOS_HEADER*>(ntdll_handle); - // The e_lfanew is an offset from the DOS header to the NT header. It should - // never be 0. - IMAGE_NT_HEADERS* nt_headers = reinterpret_cast<IMAGE_NT_HEADERS*>( - ntdll_handle + dos_header->e_lfanew / sizeof(uint32_t)); - // For modules that have an import address table, its offset from the - // DOS header is stored in the second data directory's VirtualAddress. - if (!nt_headers->OptionalHeader.DataDirectory[0].VirtualAddress) - return; - - BYTE* base_addr = reinterpret_cast<BYTE*>(ntdll_handle); - - IMAGE_DATA_DIRECTORY* exports_data_dir = - &nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; - - IMAGE_EXPORT_DIRECTORY* exports = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>( - base_addr + exports_data_dir->VirtualAddress); - - WORD* ordinals = reinterpret_cast<WORD*>( - base_addr + exports->AddressOfNameOrdinals); - DWORD* names = reinterpret_cast<DWORD*>( - base_addr + exports->AddressOfNames); - DWORD* funcs = reinterpret_cast<DWORD*>( - base_addr + exports->AddressOfFunctions); - int num_entries = exports->NumberOfNames; - - for (int i = 0; i < num_entries; i++) { - char* name = reinterpret_cast<char*>(base_addr + names[i]); - WORD ord = ordinals[i]; - DWORD func = funcs[ord]; - FARPROC func_addr = reinterpret_cast<FARPROC>(func + base_addr); - g_ntdll_lookup[std::string(name)] = func_addr; - } + base::win::PEImage ntdll_image(ntdll_handle); + + ntdll_image.EnumExports(EnumExportsCallback, NULL); } |