dbghelp: Introduce search_unix_path helper and use it in elf_search_and_load_file.

Signed-off-by: Jacek Caban <jacek@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Jacek Caban 2020-03-23 16:16:27 +01:00 committed by Alexandre Julliard
parent ee5d29b7aa
commit ca49552f64
3 changed files with 47 additions and 46 deletions

View File

@ -671,6 +671,7 @@ extern BOOL path_find_symbol_file(const struct process* pcs, const struc
WCHAR *buffer, BOOL* is_unmatched) DECLSPEC_HIDDEN;
extern WCHAR *get_dos_file_name(const WCHAR *filename) DECLSPEC_HIDDEN;
extern BOOL search_dll_path(const WCHAR *name, BOOL (*match)(void*, HANDLE, const WCHAR*), void *param) DECLSPEC_HIDDEN;
extern BOOL search_unix_path(const WCHAR *name, const char *path, BOOL (*match)(void*, HANDLE, const WCHAR*), void *param) DECLSPEC_HIDDEN;
extern const WCHAR* file_name(const WCHAR* str) DECLSPEC_HIDDEN;
extern const char* file_nameA(const char* str) DECLSPEC_HIDDEN;

View File

@ -1274,47 +1274,6 @@ static BOOL elf_load_file_cb(void *param, HANDLE handle, const WCHAR *filename)
return elf_load_file(load_file->process, filename, load_file->load_offset, load_file->dyn_addr, load_file->elf_info);
}
/******************************************************************
* elf_load_file_from_path
* tries to load an ELF file from a set of paths (separated by ':')
*/
static BOOL elf_load_file_from_path(HANDLE hProcess,
const WCHAR* filename,
unsigned long load_offset,
unsigned long dyn_addr,
const char* path,
struct elf_info* elf_info)
{
BOOL ret = FALSE;
WCHAR *s, *t, *fn;
WCHAR* pathW = NULL;
unsigned len;
if (!path) return FALSE;
len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (!pathW) return FALSE;
MultiByteToWideChar(CP_UNIXCP, 0, path, -1, pathW, len);
for (s = pathW; s && *s; s = (t) ? (t+1) : NULL)
{
t = strchrW(s, ':');
if (t) *t = '\0';
fn = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1 + lstrlenW(s) + 1) * sizeof(WCHAR));
if (!fn) break;
strcpyW(fn, s);
strcatW(fn, S_SlashW);
strcatW(fn, filename);
ret = elf_load_file(hProcess, fn, load_offset, dyn_addr, elf_info);
HeapFree(GetProcessHeap(), 0, fn);
if (ret) break;
}
HeapFree(GetProcessHeap(), 0, pathW);
return ret;
}
#ifdef AT_SYSINFO_EHDR
/******************************************************************
* elf_search_auxv
@ -1418,11 +1377,9 @@ static BOOL elf_search_and_load_file(struct process* pcs, const WCHAR* filename,
load_elf.dyn_addr = dyn_addr;
load_elf.elf_info = elf_info;
ret = elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
getenv("PATH"), elf_info) ||
elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
getenv("LD_LIBRARY_PATH"), elf_info);
if (!ret) ret = search_dll_path(filename, elf_load_file_cb, &load_elf);
ret = search_unix_path(filename, getenv("PATH"), elf_load_file_cb, &load_elf)
|| search_unix_path(filename, getenv("LD_LIBRARY_PATH"), elf_load_file_cb, &load_elf)
|| search_dll_path(filename, elf_load_file_cb, &load_elf);
}
return ret;

View File

@ -806,3 +806,46 @@ found:
heap_free(buf);
return TRUE;
}
BOOL search_unix_path(const WCHAR *name, const char *path, BOOL (*match)(void*, HANDLE, const WCHAR*), void *param)
{
const char *iter, *next;
size_t size, len;
WCHAR *dos_path;
char *buf;
BOOL ret = FALSE;
if (!path) return FALSE;
name = file_name(name);
size = WideCharToMultiByte(CP_UNIXCP, 0, name, -1, NULL, 0, NULL, NULL) + strlen(path) + 1;
if (!(buf = heap_alloc(size))) return FALSE;
for (iter = path;; iter = next + 1)
{
if (!(next = strchr(iter, ':'))) next = iter + strlen(iter);
if (*iter == '/')
{
len = next - iter;
memcpy(buf, iter, len);
if (buf[len - 1] != '/') buf[len++] = '/';
WideCharToMultiByte(CP_UNIXCP, 0, name, -1, buf + len, size - len, NULL, NULL);
if ((dos_path = wine_get_dos_file_name(buf)))
{
HANDLE file = CreateFileW(dos_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file != INVALID_HANDLE_VALUE)
{
ret = match(param, file, dos_path);
CloseHandle(file);
if (ret) TRACE("found %s\n", debugstr_w(dos_path));
}
heap_free(dos_path);
if (ret) break;
}
}
if (*next != ':') break;
}
heap_free(buf);
return ret;
}