Chromium Code Reviews| 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_win.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 #include "base/stringprintf.h" | |
| 17 | |
| 18 // So long as these symbols are supplied to the final binary through an | |
| 19 // object file (as opposed to indirectly through a library), these pointers | |
| 20 // will override the CRT's symbols and direct the notifications to our hook. | |
| 21 // Alternatively referencing the ChromeDelayLoadHook function somehow will | |
| 22 // cause this declaration of these variables to take preference to the delay | |
| 23 // load runtime's defaults (in delayimp.lib). | |
| 24 PfnDliHook __pfnDliNotifyHook2 = ChromeDelayLoadHook; | |
| 25 PfnDliHook __pfnDliFailureHook2 = ChromeDelayLoadHook; | |
| 26 | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 FARPROC OnPreLoadLibrary(DelayLoadInfo* info) { | |
| 31 // If the DLL name ends with "-delay.dll", this call is about one of our | |
| 32 // custom import libraries. In this case we need to snip the suffix off, | |
| 33 // and bind to the real DLL. | |
| 34 std::string dll_name(info->szDll); | |
| 35 const char kDelaySuffix[] = "-delay.dll"; | |
| 36 if (EndsWith(dll_name, kDelaySuffix, false)) { | |
| 37 // Trim the "-delay.dll" suffix from the string. | |
| 38 dll_name.resize(dll_name.length() - (sizeof(kDelaySuffix) - 1)); | |
| 39 dll_name.append(".dll"); | |
| 40 | |
| 41 return reinterpret_cast<FARPROC>(::LoadLibraryA(dll_name.c_str())); | |
| 42 } | |
| 43 | |
| 44 return NULL; | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 // This function is a delay load notification hook. It is invoked by the | |
| 50 // delay load support in the visual studio runtime. | |
| 51 // See http://msdn.microsoft.com/en-us/library/z9h1h6ty(v=vs.100).aspx for | |
| 52 // details. | |
| 53 FARPROC WINAPI ChromeDelayLoadHook(unsigned reason, DelayLoadInfo* info) { | |
| 54 switch (reason) { | |
| 55 case dliNoteStartProcessing: | |
| 56 case dliNoteEndProcessing: | |
| 57 // Nothing to do here. | |
| 58 break; | |
| 59 | |
| 60 case dliNotePreLoadLibrary: | |
| 61 return OnPreLoadLibrary(info); | |
| 62 break; | |
| 63 | |
| 64 case dliNotePreGetProcAddress: | |
| 65 // Nothing to do here. | |
| 66 break; | |
| 67 | |
| 68 case dliFailLoadLib: | |
| 69 case dliFailGetProc: | |
| 70 // Returning NULL from error notifications will cause the delay load | |
| 71 // runtime to raise a VcppException structured exception, that some code | |
| 72 // might want to handle. | |
| 73 return NULL; | |
| 74 break; | |
| 75 | |
| 76 default: | |
| 77 NOTREACHED() << "Impossible delay load notification."; | |
|
M-A Ruel
2013/03/26 12:59:14
It's tricky, you could get into a loop here becau
Sigurður Ásgeirsson
2013/03/26 15:35:35
That's true but unlikely now that we're not delay
M-A Ruel
2013/03/26 15:44:37
Indeed, I just guess there's no way to ensure that
| |
| 78 break; | |
| 79 } | |
| 80 | |
| 81 // Returning NULL causes the delay load machinery to perform default | |
| 82 // processing for this notification. | |
| 83 return NULL; | |
| 84 } | |
| OLD | NEW |