From 27c718aa1a4674587925adb543362cb8e60814c4 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 2 Jun 2020 07:53:10 +0100 Subject: win32: use lazy loading for certain DLLs Only a handful of functions are used from shell32.dll, userenv.dll and psapi.dll. Mostly these functions are in out of the way places. By loading the functions only when required we can avoid the startup cost of linking the three DLLs in the common case that they aren't needed. --- win32/lazyload.h | 16 ++-------------- win32/mingw.c | 27 +++++++++++++++++++++------ win32/process.c | 15 +++++++++++++-- 3 files changed, 36 insertions(+), 22 deletions(-) (limited to 'win32') diff --git a/win32/lazyload.h b/win32/lazyload.h index a9fcff209..034bc7e45 100644 --- a/win32/lazyload.h +++ b/win32/lazyload.h @@ -21,19 +21,7 @@ struct proc_addr { #define INIT_PROC_ADDR(dll, function) \ (function = get_proc_addr(#dll, #function, &proc_addr_##function)) -static inline void *get_proc_addr(const char *dll, const char *function, struct proc_addr *proc) -{ - /* only do this once */ - if (!proc->initialized) { - HANDLE hnd = LoadLibraryExA(dll, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); - if (hnd) - proc->pfunction = GetProcAddress(hnd, function); - proc->initialized = 1; - } - /* set ENOSYS if DLL or function was not found */ - if (!proc->pfunction) - errno = ENOSYS; - return proc->pfunction; -} +void *get_proc_addr(const char *dll, const char *function, + struct proc_addr *proc); #endif diff --git a/win32/mingw.c b/win32/mingw.c index 5c4c39b9d..105b7864d 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -837,17 +837,17 @@ static char *gethomedir(void) static char *buf = NULL; DWORD len = PATH_MAX; HANDLE h; + DECLARE_PROC_ADDR(BOOL, GetUserProfileDirectoryA, HANDLE, LPSTR, LPDWORD); - if (!buf) - buf = xzalloc(PATH_MAX); - - if (buf[0]) + if (buf) return buf; + buf = xzalloc(PATH_MAX); if ( !OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &h) ) return buf; - GetUserProfileDirectory(h, buf, &len); + if (INIT_PROC_ADDR(userenv.dll, GetUserProfileDirectoryA)) + GetUserProfileDirectoryA(h, buf, &len); CloseHandle(h); return bs_to_slash(buf); } @@ -1027,7 +1027,6 @@ int link(const char *oldpath, const char *newpath) LPSECURITY_ATTRIBUTES); if (!INIT_PROC_ADDR(kernel32.dll, CreateHardLinkA)) { - errno = ENOSYS; return -1; } if (!CreateHardLinkA(newpath, oldpath, NULL)) { @@ -1767,3 +1766,19 @@ void make_sparse(int fd, off_t start, off_t end) DeviceIoControl(fh, FSCTL_SET_ZERO_DATA, &fzdi, sizeof(fzdi), NULL, 0, &dwTemp, NULL); } + +void *get_proc_addr(const char *dll, const char *function, + struct proc_addr *proc) +{ + /* only do this once */ + if (!proc->initialized) { + HANDLE hnd = LoadLibraryExA(dll, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); + if (hnd) + proc->pfunction = GetProcAddress(hnd, function); + proc->initialized = 1; + } + /* set ENOSYS if DLL or function was not found */ + if (!proc->pfunction) + errno = ENOSYS; + return proc->pfunction; +} diff --git a/win32/process.c b/win32/process.c index 04775100b..5833a0323 100644 --- a/win32/process.c +++ b/win32/process.c @@ -446,6 +446,17 @@ static char *get_bb_string(DWORD pid, const char *exe, char *string) char exepath[PATH_MAX]; char *name = NULL; int i; + DECLARE_PROC_ADDR(DWORD, GetProcessImageFileNameA, HANDLE, + LPSTR, DWORD); + DECLARE_PROC_ADDR(BOOL, EnumProcessModules, HANDLE, HMODULE *, + DWORD, LPDWORD); + DECLARE_PROC_ADDR(DWORD, GetModuleFileNameExA, HANDLE, HMODULE, + LPSTR, DWORD); + + if (!INIT_PROC_ADDR(psapi.dll, GetProcessImageFileNameA) || + !INIT_PROC_ADDR(psapi.dll, EnumProcessModules) || + !INIT_PROC_ADDR(psapi.dll, GetModuleFileNameExA)) + return NULL; if (!(proc=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, pid))) { @@ -453,7 +464,7 @@ static char *get_bb_string(DWORD pid, const char *exe, char *string) } if (exe == NULL) { - if (GetProcessImageFileName(proc, exepath, PATH_MAX) != 0) { + if (GetProcessImageFileNameA(proc, exepath, PATH_MAX) != 0) { exe = bb_basename(exepath); } } @@ -470,7 +481,7 @@ static char *get_bb_string(DWORD pid, const char *exe, char *string) for (i=0; exe != NULL && i