| 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 #include <DelayIMP.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 | |
| 13 // So long as these symbols are supplied to the final binary through an | |
| 14 // object file (as opposed to indirectly through a library), these pointers | |
| 15 // will override the CRT's symbols and direct the notifications to our hook. | |
| 16 // Alternatively referencing the ChromeDelayLoadHook function somehow will | |
| 17 // cause this declaration of these variables to take preference to the delay | |
| 18 // load runtime's defaults (in delayimp.lib). | |
| 19 #if _MSC_FULL_VER >= 190024112 | |
| 20 // Prior to Visual Studio 2015 Update 3, these hooks were non-const. They were | |
| 21 // made const to improve security (global, writable function pointers are bad). | |
| 22 // This #ifdef is needed for testing of VS 2015 Update 3 pre-release and can be | |
| 23 // removed when we formally switch to Update 3 or higher. | |
| 24 // TODO(612313): remove the #if when we update toolchains. | |
| 25 const PfnDliHook __pfnDliNotifyHook2 = ChromeDelayLoadHook; | |
| 26 const PfnDliHook __pfnDliFailureHook2 = ChromeDelayLoadHook; | |
| 27 #else | |
| 28 PfnDliHook __pfnDliNotifyHook2 = ChromeDelayLoadHook; | |
| 29 PfnDliHook __pfnDliFailureHook2 = ChromeDelayLoadHook; | |
| 30 #endif | |
| 31 | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 FARPROC OnPreLoadLibrary(DelayLoadInfo* info) { | |
| 36 // If the DLL name ends with "-delay.dll", this call is about one of our | |
| 37 // custom import libraries. In this case we need to snip the suffix off, | |
| 38 // and bind to the real DLL. | |
| 39 std::string dll_name(info->szDll); | |
| 40 const char kDelaySuffix[] = "-delay.dll"; | |
| 41 if (base::EndsWith(dll_name, kDelaySuffix, | |
| 42 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 43 // Trim the "-delay.dll" suffix from the string. | |
| 44 dll_name.resize(dll_name.length() - (sizeof(kDelaySuffix) - 1)); | |
| 45 dll_name.append(".dll"); | |
| 46 | |
| 47 return reinterpret_cast<FARPROC>(::LoadLibraryA(dll_name.c_str())); | |
| 48 } | |
| 49 | |
| 50 return NULL; | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 // This function is a delay load notification hook. It is invoked by the | |
| 56 // delay load support in the visual studio runtime. | |
| 57 // See http://msdn.microsoft.com/en-us/library/z9h1h6ty(v=vs.100).aspx for | |
| 58 // details. | |
| 59 extern "C" FARPROC WINAPI ChromeDelayLoadHook(unsigned reason, | |
| 60 DelayLoadInfo* info) { | |
| 61 switch (reason) { | |
| 62 case dliNoteStartProcessing: | |
| 63 case dliNoteEndProcessing: | |
| 64 // Nothing to do here. | |
| 65 break; | |
| 66 | |
| 67 case dliNotePreLoadLibrary: | |
| 68 return OnPreLoadLibrary(info); | |
| 69 break; | |
| 70 | |
| 71 case dliNotePreGetProcAddress: | |
| 72 // Nothing to do here. | |
| 73 break; | |
| 74 | |
| 75 case dliFailLoadLib: | |
| 76 case dliFailGetProc: | |
| 77 // Returning NULL from error notifications will cause the delay load | |
| 78 // runtime to raise a VcppException structured exception, that some code | |
| 79 // might want to handle. | |
| 80 return NULL; | |
| 81 break; | |
| 82 | |
| 83 default: | |
| 84 NOTREACHED() << "Impossible delay load notification."; | |
| 85 break; | |
| 86 } | |
| 87 | |
| 88 // Returning NULL causes the delay load machinery to perform default | |
| 89 // processing for this notification. | |
| 90 return NULL; | |
| 91 } | |
| OLD | NEW |