dbghelp: Simplify code for searching alternate debug info files.

Signed-off-by: Eric Pouech <eric.pouech@gmail.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Eric Pouech 2021-11-22 15:21:07 +01:00 committed by Alexandre Julliard
parent 789512beee
commit 9f1766193e

View file

@ -634,6 +634,17 @@ found:
return TRUE; return TRUE;
} }
static WCHAR* append_hex(WCHAR* dst, const BYTE* id, const BYTE* end)
{
while (id < end)
{
*dst++ = "0123456789abcdef"[*id >> 4 ];
*dst++ = "0123456789abcdef"[*id & 0x0F];
id++;
}
return dst;
}
/****************************************************************** /******************************************************************
* image_load_debugaltlink * image_load_debugaltlink
* *
@ -683,16 +694,14 @@ struct image_file_map* image_load_debugaltlink(struct image_file_map* fmap, stru
if (dst) if (dst)
{ {
MultiByteToWideChar(CP_UNIXCP, 0, data, -1, dst, filename_len); MultiByteToWideChar(CP_UNIXCP, 0, data, -1, dst, filename_len);
ret = image_check_debug_link_gnu_id(dst, fmap_link, id, data + sect_len - (const char*)id); ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
HeapFree(GetProcessHeap(), 0, dst); HeapFree(GetProcessHeap(), 0, dst);
} }
/* Trying relative path to build-id directory */ /* Trying relative path to build-id directory */
if (!ret) if (!ret)
{ {
static const WCHAR globalDebugDirW[] =
{'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/','.','b','u','i','l','d','-','i','d','/'};
dst = HeapAlloc(GetProcessHeap(), 0, dst = HeapAlloc(GetProcessHeap(), 0,
sizeof(globalDebugDirW) + (3 + filename_len + idlen * 2) * sizeof(WCHAR)); sizeof(L"/usr/lib/debug/.build-id/") + (3 + filename_len + idlen * 2) * sizeof(WCHAR));
if (dst) if (dst)
{ {
WCHAR* p; WCHAR* p;
@ -702,21 +711,16 @@ struct image_file_map* image_load_debugaltlink(struct image_file_map* fmap, stru
* where the alternate file is... * where the alternate file is...
* so try both * so try both
*/ */
p = memcpy(dst, globalDebugDirW, sizeof(globalDebugDirW)); p = memcpy(dst, L"/usr/lib/debug/.build-id/", sizeof(L"/usr/lib/debug/.build-id/"));
p += ARRAY_SIZE(globalDebugDirW); p += wcslen(dst);
MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len); MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
ret = image_check_debug_link_gnu_id(dst, fmap_link, id, data + sect_len - (const char*)id); ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
if (!ret) if (!ret)
{ {
p = dst + ARRAY_SIZE(globalDebugDirW); p = append_hex(p, id, id + idlen);
if ((const char*)id < data + sect_len)
{
*p++ = "0123456789abcdef"[*id >> 4 ];
*p++ = "0123456789abcdef"[*id & 0x0F];
}
*p++ = '/'; *p++ = '/';
MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len); MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
ret = image_check_debug_link_gnu_id(dst, fmap_link, id, data + sect_len - (const char*)id); ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
} }
HeapFree(GetProcessHeap(), 0, dst); HeapFree(GetProcessHeap(), 0, dst);
} }
@ -742,51 +746,38 @@ struct image_file_map* image_load_debugaltlink(struct image_file_map* fmap, stru
*/ */
static BOOL image_locate_build_id_target(struct image_file_map* fmap, const BYTE* id, unsigned idlen) static BOOL image_locate_build_id_target(struct image_file_map* fmap, const BYTE* id, unsigned idlen)
{ {
static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
static const WCHAR buildidW[] = {'.','b','u','i','l','d','-','i','d','/'};
struct image_file_map* fmap_link = NULL; struct image_file_map* fmap_link = NULL;
WCHAR* p; WCHAR* p;
WCHAR* z; WCHAR* z;
const BYTE* idend = id + idlen;
fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link)); fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
if (!fmap_link) return FALSE; if (!fmap_link) return FALSE;
p = HeapAlloc(GetProcessHeap(), 0, p = malloc(sizeof(L"/usr/lib/debug/.build-id/") +
sizeof(globalDebugDirW) + sizeof(buildidW) + (idlen * 2 + 1) * sizeof(WCHAR) + sizeof(L".debug"));
(idlen * 2 + 1) * sizeof(WCHAR) + sizeof(L".debug")); wcscpy(p, L"/usr/lib/debug/.build-id/");
z = p; z = p + wcslen(p);
memcpy(z, globalDebugDirW, sizeof(globalDebugDirW)); if (idlen)
z += ARRAY_SIZE(globalDebugDirW);
memcpy(z, buildidW, sizeof(buildidW));
z += ARRAY_SIZE(buildidW);
if (id < idend)
{ {
*z++ = "0123456789abcdef"[*id >> 4 ]; z = append_hex(z, id, id + 1);
*z++ = "0123456789abcdef"[*id & 0x0F]; if (idlen > 1)
id++; {
} *z++ = L'/';
if (id < idend) z = append_hex(z, id + 1, id + idlen);
*z++ = '/'; }
while (id < idend)
{
*z++ = "0123456789abcdef"[*id >> 4 ];
*z++ = "0123456789abcdef"[*id & 0x0F];
id++;
} }
memcpy(z, L".debug", sizeof(L".debug")); memcpy(z, L".debug", sizeof(L".debug"));
TRACE("checking %s\n", wine_dbgstr_w(p)); TRACE("checking %s\n", wine_dbgstr_w(p));
if (image_check_debug_link_gnu_id(p, fmap_link, idend - idlen, idlen)) if (image_check_debug_link_gnu_id(p, fmap_link, id, idlen))
{ {
HeapFree(GetProcessHeap(), 0, p); free(p);
fmap->alternate = fmap_link; fmap->alternate = fmap_link;
return TRUE; return TRUE;
} }
TRACE("not found\n"); TRACE("not found\n");
HeapFree(GetProcessHeap(), 0, p); free(p);
HeapFree(GetProcessHeap(), 0, fmap_link); HeapFree(GetProcessHeap(), 0, fmap_link);
return FALSE; return FALSE;
} }