OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/app/delay_load_hook.h" | |
6 | |
7 #if defined(_WIN32_WINNT_WIN8) && _MSC_VER < 1700 | |
8 // The Windows 8 SDK defines FACILITY_VISUALCPP in winerror.h, and in | |
9 // delayimp.h previous to VS2012. | |
10 #undef FACILITY_VISUALCPP | |
11 #endif | |
12 #include <DelayIMP.h> | |
13 | |
14 #include "base/logging.h" | |
15 #include "base/string_util.h" | |
16 | |
17 // So long as these symbols are supplied to the final binary through an | |
18 // object file (as opposed to indirectly thruogh a library), these pointers | |
M-A Ruel
2013/02/07 21:14:30
through
| |
19 // will override the CRT's symbols and direct the notifications to our hook. | |
20 PfnDliHook __pfnDliNotifyHook2 = ChromeDelayLoadHook; | |
21 PfnDliHook __pfnDliFailureHook2 = ChromeDelayLoadHook; | |
22 | |
23 | |
24 namespace { | |
25 | |
26 FARPROC OnPreLoadLibrary(DelayLoadInfo* info) { | |
27 // If the DLL name ends with "-delay.dll", this call is about one of our | |
28 // custom import libraries. In this case we need to snip the suffix off, | |
29 // and bind to the real DLL. | |
30 std::string dll_name(info->szDll); | |
31 const char kDelaySuffix[] = "-delay.dll"; | |
32 if (EndsWith(dll_name, kDelaySuffix, false)) { | |
33 // Trim the "-delay.dll" suffix from the string. | |
34 dll_name.resize(dll_name.length() - (sizeof(kDelaySuffix) - 1)); | |
35 dll_name.append(".dll"); | |
36 | |
37 HMODULE dll = ::LoadLibraryA(dll_name.c_str()); | |
M-A Ruel
2013/02/07 21:14:30
return reinterpret_cast<FARPROC>(::LoadLibraryA(dl
| |
38 | |
39 return reinterpret_cast<FARPROC>(dll); | |
40 } | |
41 | |
42 return NULL; | |
43 } | |
44 | |
45 FARPROC OnFailLoadLib(DelayLoadInfo* info) { | |
46 LOG(FATAL) << "Failed to delay load DLL: " << info->szDll; | |
47 return NULL; | |
48 } | |
49 | |
50 FARPROC OnFailGetProc(DelayLoadInfo* info) { | |
51 if (info->dlp.fImportByName) { | |
52 LOG(FATAL) << "Failed to delay load function: '" << info->dlp.szProcName | |
53 << "' from DLL: " << info->szDll; | |
54 } else { | |
55 LOG(FATAL) << "Failed to delay load ordinal: '" << info->dlp.dwOrdinal | |
56 << "' from DLL: " << info->szDll; | |
57 } | |
58 | |
59 return NULL; | |
60 } | |
61 | |
62 } // namespace | |
63 | |
64 FARPROC WINAPI ChromeDelayLoadHook(unsigned reason, DelayLoadInfo* info) { | |
65 switch (reason) { | |
66 case dliNoteStartProcessing: | |
67 LOG(INFO) << "Delay load dliNoteStartProcessing notification."; | |
68 break; | |
69 case dliNotePreLoadLibrary: | |
70 return OnPreLoadLibrary(info); | |
71 | |
72 break; | |
73 case dliNotePreGetProcAddress: | |
74 LOG(INFO) << "Delay load dliNotePreGetProcAddress notification."; | |
75 break; | |
76 | |
77 case dliFailLoadLib: | |
78 return OnFailLoadLib(info); | |
79 break; | |
80 | |
81 case dliFailGetProc: | |
82 return OnFailGetProc(info); | |
83 break; | |
84 | |
85 case dliNoteEndProcessing: | |
86 LOG(INFO) << "Delay load dliNoteEndProcessing notification."; | |
87 break; | |
88 default: | |
89 NOTREACHED() << "Impossible delay load notification."; | |
90 break; | |
91 } | |
92 | |
93 return NULL; | |
94 } | |
OLD | NEW |