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(); | |
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) | |
robertshield
2013/11/28 13:57:46
nit: indent
Cait (Slow)
2013/12/03 03:06:14
Done.
| |
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. | |
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 = (PBYTE) ntdll_handle; | |
39 | |
40 IMAGE_DATA_DIRECTORY* exports_data_dir = | |
robertshield
2013/11/28 13:57:46
nit: extra space
Cait (Slow)
2013/12/03 03:06:14
Done.
| |
41 &nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; | |
42 | |
43 IMAGE_EXPORT_DIRECTORY* exports = | |
robertshield
2013/11/28 13:57:46
nit: extra space
Cait (Slow)
2013/12/03 03:06:14
Done.
| |
44 (IMAGE_EXPORT_DIRECTORY*) (base_addr + exports_data_dir->VirtualAddress); | |
45 | |
46 WORD* ordinals = (WORD*)(base_addr + exports->AddressOfNameOrdinals); | |
robertshield
2013/11/28 13:57:46
use C++ style casts (reinterpret_cast) here and be
Cait (Slow)
2013/12/03 03:06:14
Done.
| |
47 DWORD* names = (DWORD*)(base_addr + exports->AddressOfNames); | |
48 DWORD* funcs = (DWORD*)(base_addr + exports->AddressOfFunctions); | |
49 int num_entries = exports->NumberOfNames; | |
50 | |
51 for (int i = 0; i < num_entries; i++) { | |
52 char* name = (char*) (base_addr + names[i]); | |
53 WORD ord = ordinals[i]; | |
54 DWORD func = funcs[ord]; | |
55 FARPROC func_addr = (FARPROC) (func + base_addr); | |
robertshield
2013/11/28 13:57:46
nit: extra spaces after = and func, also use a C++
Cait (Slow)
2013/12/03 03:06:14
Done.
| |
56 g_ntdll_lookup[std::string(name)] = func_addr; | |
57 } | |
58 } | |
OLD | NEW |