From 88938669e1649714bde0c2e057964ab497419c6f Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Thu, 30 Jul 2020 10:51:23 -0700 Subject: [PATCH] devops(windows): add tool for printing library dependencies on Windows (#3224) --- .../tools/PrintDepsWindows/.gitignore | 2 + .../tools/PrintDepsWindows/PrintDeps.cpp | 109 +++++++++++++ .../tools/PrintDepsWindows/PrintDeps.vcxproj | 151 ++++++++++++++++++ .../PrintDeps.vcxproj.filters | 22 +++ .../PrintDepsWindows/PrintDeps.vcxproj.user | 4 + 5 files changed, 288 insertions(+) create mode 100644 browser_patches/tools/PrintDepsWindows/.gitignore create mode 100644 browser_patches/tools/PrintDepsWindows/PrintDeps.cpp create mode 100644 browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj create mode 100644 browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj.filters create mode 100644 browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj.user diff --git a/browser_patches/tools/PrintDepsWindows/.gitignore b/browser_patches/tools/PrintDepsWindows/.gitignore new file mode 100644 index 0000000000000..c98f18fff9ecf --- /dev/null +++ b/browser_patches/tools/PrintDepsWindows/.gitignore @@ -0,0 +1,2 @@ +.vs +x64 diff --git a/browser_patches/tools/PrintDepsWindows/PrintDeps.cpp b/browser_patches/tools/PrintDepsWindows/PrintDeps.cpp new file mode 100644 index 0000000000000..eb2a950ea0786 --- /dev/null +++ b/browser_patches/tools/PrintDepsWindows/PrintDeps.cpp @@ -0,0 +1,109 @@ +/** + * Copyright Microsoft Corporation. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include +#include + +using DepsMap = std::map; + +std::string getLastErrorString() +{ + LPTSTR lpMsgBuf; + DWORD dw = GetLastError(); + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + dw, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)&lpMsgBuf, + 0, NULL); + std::string result(lpMsgBuf); + LocalFree(lpMsgBuf); + return result; +} + +const DepsMap getDependencies(const HMODULE hMod) +{ + // See https://docs.microsoft.com/en-us/archive/msdn-magazine/2002/february/inside-windows-win32-portable-executable-file-format-in-detail + // for PE format description. + ULONG size; + PIMAGE_IMPORT_DESCRIPTOR pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToData(hMod, true, IMAGE_DIRECTORY_ENTRY_IMPORT, &size); + DepsMap deps; + // According to https://docs.microsoft.com/en-us/archive/msdn-magazine/2002/march/inside-windows-an-in-depth-look-into-the-win32-portable-executable-file-format-part-2 + // "The end of the IMAGE_IMPORT_DESCRIPTOR array is indicated by an entry with fields all set to 0." + while (pImportDesc->Name) + { + LPCSTR dllName = (LPCSTR)((BYTE*)hMod + pImportDesc->Name); + std::string dllPath = "not found"; + HMODULE hModDep = LoadLibraryEx(dllName, NULL, DONT_RESOLVE_DLL_REFERENCES); + if (hModDep != NULL) + { + TCHAR pathBuffer[_MAX_PATH]; + DWORD result = GetModuleFileName(hModDep, pathBuffer, _MAX_PATH); + if (result == 0) { + std::cerr << "Failed to get library file name: " << dllName << " Error: " << getLastErrorString() << std::endl; + } + dllPath = std::string(pathBuffer); + FreeLibrary(hModDep); + } + deps[std::string(dllName)] = dllPath; + pImportDesc++; + } + + return deps; +} + +int printDependencies(const char* library) +{ + HMODULE hMod = LoadLibraryEx(library, NULL, DONT_RESOLVE_DLL_REFERENCES); + if (hMod == NULL) + { + std::cerr << "Failed to load " << library << " Error: " << getLastErrorString() << std::endl; + return -1; + } + const DepsMap& deps = getDependencies(hMod); + for (const auto iter : deps) + { + std::cout << " " << iter.first << " => " << iter.second << std::endl; + } + FreeLibrary(hMod); + return 0; +} + +int printUsage() +{ + std::cout << "Usage:\n PrintDeps FILE..." << std::endl; + return -1; +} + +int main(int argc, char* argv[]) +{ + if (argc <= 1) + { + return printUsage(); + } + int res = 0; + for (int i = 1; i < argc; ++i) + { + std::cout << argv[i] << std::endl; + res = printDependencies(argv[i]); + } + return res; +} diff --git a/browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj b/browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj new file mode 100644 index 0000000000000..a72bde9dbbbfa --- /dev/null +++ b/browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj @@ -0,0 +1,151 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {90c6cf9b-bed7-41e9-904d-50bd303bacc8} + PrintDeps + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + NotSet + + + Application + true + v142 + NotSet + + + Application + false + v142 + true + NotSet + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + MultiThreadedDLL + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + MultiThreadedDLL + + + Console + true + true + true + %(AddModuleNamesToAssembly) + dbghelp.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj.filters b/browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj.filters new file mode 100644 index 0000000000000..023892c53064e --- /dev/null +++ b/browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj.user b/browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj.user new file mode 100644 index 0000000000000..0f14913f3c720 --- /dev/null +++ b/browser_patches/tools/PrintDepsWindows/PrintDeps.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file