OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <windows.h> | 5 #include <windows.h> |
6 | 6 |
7 #include "chrome_elf/chrome_elf_main.h" | 7 #include "chrome_elf/chrome_elf_main.h" |
8 | 8 |
9 FunctionLookupTable g_ntdll_lookup = FunctionLookupTable(); | |
robertshield
2013/12/03 03:26:26
I don't think you need the extra assignment here,
Cait (Slow)
2013/12/03 19:59:40
Done.
| |
10 | |
9 void InitChromeElf() { | 11 void InitChromeElf() { |
10 // This method is a no-op which may be called to force a load-time dependency | 12 // This method is a no-op which may be called to force a load-time dependency |
11 // on chrome_elf.dll. | 13 // on chrome_elf.dll. |
12 } | 14 } |
13 | 15 |
14 BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) { | 16 BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) { |
17 if (reason == DLL_PROCESS_ATTACH) | |
18 Init(); | |
15 return TRUE; | 19 return TRUE; |
16 } | 20 } |
21 | |
22 void Init() { | |
23 HMODULE ntdll_handle = ::GetModuleHandle(L"ntdll.dll"); | |
24 | |
25 // To find the Export Address Table address, we start from the DOS header. | |
26 // The module handle is actually the base address where the header is. | |
robertshield
2013/12/03 03:26:26
s/base address where the header is/address of the
| |
27 IMAGE_DOS_HEADER* dos_header = | |
28 reinterpret_cast<IMAGE_DOS_HEADER*>(ntdll_handle); | |
29 // The e_lfanew is an offset from the DOS header to the NT header. It should | |
30 // never be 0. | |
31 IMAGE_NT_HEADERS* nt_headers = reinterpret_cast<IMAGE_NT_HEADERS*>( | |
32 ntdll_handle + dos_header->e_lfanew / sizeof(uintptr_t)); | |
33 // For modules that have an import address table, its offset from the | |
34 // DOS header is stored in the second data directory's VirtualAddress. | |
35 if (!nt_headers->OptionalHeader.DataDirectory[0].VirtualAddress) | |
36 return; | |
37 | |
38 PBYTE base_addr = reinterpret_cast<PBYTE>(ntdll_handle); | |
robertshield
2013/12/03 03:26:26
nit: use BYTE* rather than PBYTE.
Cait (Slow)
2013/12/03 19:59:40
Done.
| |
39 | |
40 IMAGE_DATA_DIRECTORY* exports_data_dir = | |
41 &nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; | |
42 | |
43 IMAGE_EXPORT_DIRECTORY* exports = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>( | |
44 base_addr + exports_data_dir->VirtualAddress); | |
45 | |
46 WORD* ordinals = reinterpret_cast<WORD*>( | |
47 base_addr + exports->AddressOfNameOrdinals); | |
48 DWORD* names = reinterpret_cast<DWORD*>( | |
49 base_addr + exports->AddressOfNames); | |
50 DWORD* funcs = reinterpret_cast<DWORD*>( | |
51 base_addr + exports->AddressOfFunctions); | |
52 int num_entries = exports->NumberOfNames; | |
53 | |
54 for (int i = 0; i < num_entries; i++) { | |
55 char* name = reinterpret_cast<char*>(base_addr + names[i]); | |
56 WORD ord = ordinals[i]; | |
57 DWORD func = funcs[ord]; | |
58 FARPROC func_addr = reinterpret_cast<FARPROC>(func + base_addr); | |
59 g_ntdll_lookup[std::string(name)] = func_addr; | |
60 } | |
61 } | |
OLD | NEW |