https://git.reactos.org/?p=reactos.git;a=commitdiff;h=ebdb2b35d9ee6d285581a1a05a4dd94eb52fe78e

commit ebdb2b35d9ee6d285581a1a05a4dd94eb52fe78e
Author:     winesync <[email protected]>
AuthorDate: Fri Sep 11 17:05:18 2020 +0200
Commit:     Jérôme Gardou <[email protected]>
CommitDate: Wed Sep 16 10:35:46 2020 +0200

    [WINESYNC] dbghelp: Move elf_check_alternate implementation to module.c.
    
    Signed-off-by: Jacek Caban <[email protected]>
    Signed-off-by: Alexandre Julliard <[email protected]>
    
    wine commit id 4694801e4f58f0e856327d251ac98cfb573fd83b by Jacek Caban 
<[email protected]>
---
 dll/win32/dbghelp/CMakeLists.txt  |   1 +
 dll/win32/dbghelp/compat.c        | 110 ++++++++++++++++
 dll/win32/dbghelp/compat.h        |   8 ++
 dll/win32/dbghelp/dbghelp.c       |   3 -
 dll/win32/dbghelp/elf_module.c    | 257 +++-----------------------------------
 dll/win32/dbghelp/image_private.h |  11 ++
 dll/win32/dbghelp/module.c        | 249 +++++++++++++++++++++++++++++++++++-
 dll/win32/dbghelp/path.c          |   2 -
 sdk/tools/winesync/dbghelp.cfg    |   2 +-
 9 files changed, 395 insertions(+), 248 deletions(-)

diff --git a/dll/win32/dbghelp/CMakeLists.txt b/dll/win32/dbghelp/CMakeLists.txt
index c4e3929e7b9..59e160b21ee 100644
--- a/dll/win32/dbghelp/CMakeLists.txt
+++ b/dll/win32/dbghelp/CMakeLists.txt
@@ -15,6 +15,7 @@ if(NOT CMAKE_CROSSCOMPILING)
         compat.c
         dbghelp.c
         dwarf.c
+        elf_module.c
         module.c
         pe_module.c
         source.c
diff --git a/dll/win32/dbghelp/compat.c b/dll/win32/dbghelp/compat.c
index dfd9ec13d29..8ef4d551e65 100644
--- a/dll/win32/dbghelp/compat.c
+++ b/dll/win32/dbghelp/compat.c
@@ -166,6 +166,31 @@ HANDLE __CreateFileW(
     return res;
 }
 
+BOOL __ReadFile(HANDLE file, PVOID buffer, DWORD len, PDWORD outlen, PVOID 
overlap)
+{
+    size_t read;
+
+    assert(overlap == NULL);
+
+    read = fread(buffer, 1, len, file);
+
+    if (ferror(file) != 0)
+        return FALSE;
+
+    *outlen = (DWORD)read;
+    return TRUE;
+}
+
+DWORD __SetFilePointer(HANDLE file,LONG low, PLONG high, DWORD move)
+{
+    assert(move == FILE_BEGIN);
+    assert(high == NULL);
+
+    if (fseek(file, low, SEEK_SET))
+        return INVALID_SET_FILE_POINTER;
+    return low;
+}
+
 void* __MapViewOfFile(HANDLE file,DWORD d1,DWORD d2,DWORD d3,SIZE_T s)
 {
     FILE *f = (FILE*)file;
@@ -400,3 +425,88 @@ BOOL __IsWow64Process(HANDLE Process, BOOL* is_wow64)
        *is_wow64 = FALSE;
        return TRUE;
 }
+
+/* from sdk/lib/rtl/crc2.c */
+/* This work is based off of rtl.c in Wine.
+ * Please give credit where credit is due:
+ *
+ * Copyright 2003      Thomas Mertes
+ * Crc32 code Copyright 1986 Gary S. Brown (Public domain)
+ */
+
+/* CRC polynomial 0xedb88320 */
+static const ULONG CrcTable[256] =
+{
+    0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
+    0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
+    0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
+    0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
+    0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
+    0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
+    0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
+    0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
+    0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
+    0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
+    0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
+    0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
+    0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
+    0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
+    0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
+    0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
+    0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
+    0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
+    0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
+    0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+    0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
+    0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
+    0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
+    0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
+    0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
+    0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
+    0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
+    0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
+    0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
+    0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
+    0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
+    0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
+    0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
+    0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
+    0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
+    0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
+    0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
+    0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
+    0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
+    0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+    0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
+    0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
+    0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
+};
+
+/*********************************************************************
+ *                  RtlComputeCrc32   [NTDLL.@]
+ *
+ * Calculate the CRC32 checksum of a block of bytes
+ *
+ * PARAMS
+ *  Initial [I] Initial CRC value
+ *  Data    [I] Data block
+ *  Length  [I] Length of the byte block
+ *
+ * RETURNS
+ *  The cumulative CRC32 of Initial and Length bytes of the Data block.
+ *
+ * @implemented
+ */
+ULONG
+__RtlComputeCrc32(ULONG Initial, PUCHAR Data, ULONG Length)
+{
+  ULONG CrcValue = ~Initial;
+
+  while (Length > 0)
+  {
+    CrcValue = CrcTable[(CrcValue ^ *Data) & 0xff] ^ (CrcValue >> 8);
+    Data++;
+    Length--;
+  }
+  return ~CrcValue;
+}
diff --git a/dll/win32/dbghelp/compat.h b/dll/win32/dbghelp/compat.h
index b249fe1ef79..1485a78e024 100644
--- a/dll/win32/dbghelp/compat.h
+++ b/dll/win32/dbghelp/compat.h
@@ -470,6 +470,7 @@ typedef struct _EXCEPTION_REGISTRATION_RECORD
 
 // winbase.h
 #define INVALID_HANDLE_VALUE (HANDLE)(-1)
+#define INVALID_SET_FILE_POINTER       ((DWORD)-1)
 #define HeapAlloc __HeapAlloc
 #define HeapReAlloc __HeapReAlloc
 #define HeapFree(x,y,z) free(z)
@@ -479,6 +480,8 @@ typedef struct _EXCEPTION_REGISTRATION_RECORD
 #define CloseHandle __CloseHandle
 #define CreateFileA(a,b,c,d,e,f,g) fopen(a, "rb")
 #define CreateFileW __CreateFileW
+#define ReadFile(a,b,c,d,e) __ReadFile
+#define SetFilePointer __SetFilePointer
 #define CreateFileMappingW(a,b,c,d,e,f) a
 #define MapViewOfFile __MapViewOfFile
 #define UnmapViewOfFile __UnmapViewOfFile
@@ -496,12 +499,15 @@ typedef struct _EXCEPTION_REGISTRATION_RECORD
 #define ReadProcessMemory(a,b,c,d,e) 0
 #define GetCurrentProcess() (HANDLE)1
 #define IsWow64Process __IsWow64Process
+#define FILE_BEGIN     0
 
 void* __HeapAlloc(int heap, int flags, size_t size);
 void* __HeapReAlloc(int heap, DWORD d2, void *slab, SIZE_T newsize);
 WCHAR* __lstrcpynW(WCHAR* lpString1, const WCHAR* lpString2, int iMaxLength);
 BOOL __CloseHandle(HANDLE handle);
 HANDLE __CreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD 
dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD 
dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
+BOOL __ReadFile(HANDLE,PVOID,DWORD,PDWORD,/*LPOVERLAPPED*/ PVOID);
+DWORD __SetFilePointer(HANDLE,LONG,PLONG,DWORD);
 void* __MapViewOfFile(HANDLE file,DWORD d1,DWORD d2,DWORD d3,SIZE_T s);
 BOOL __UnmapViewOfFile(const void*);
 LPSTR __lstrcpynA(LPSTR,LPCSTR,int);
@@ -542,6 +548,7 @@ typedef LONG KPRIORITY;
 #define RtlImageRvaToVa __RtlImageRvaToVa
 #define RtlImageRvaToSection __RtlImageRvaToSection
 #define RtlImageDirectoryEntryToData __RtlImageDirectoryEntryToData
+#define RtlComputeCrc32 __RtlComputeCrc32
 
 #ifdef _MSC_VER
 #define RtlUlongByteSwap(_x) _byteswap_ulong((_x))
@@ -552,6 +559,7 @@ typedef LONG KPRIORITY;
 PIMAGE_NT_HEADERS __RtlImageNtHeader(void *data);
 PVOID __RtlImageRvaToVa (const IMAGE_NT_HEADERS* NtHeader, PVOID BaseAddress, 
ULONG Rva, PIMAGE_SECTION_HEADER *SectionHeader);
 PVOID __RtlImageDirectoryEntryToData(PVOID BaseAddress, BOOLEAN MappedAsImage, 
USHORT Directory, PULONG Size);
+ULONG __RtlComputeCrc32(ULONG Initial, PUCHAR Data, ULONG Length);
 
 typedef struct _CLIENT_ID
 {
diff --git a/dll/win32/dbghelp/dbghelp.c b/dll/win32/dbghelp/dbghelp.c
index bb69d671961..b59ee6a269c 100644
--- a/dll/win32/dbghelp/dbghelp.c
+++ b/dll/win32/dbghelp/dbghelp.c
@@ -739,7 +739,6 @@ void WINAPI WinDbgExtensionDllInit(PWINDBG_EXTENSION_APIS 
lpExtensionApis,
 {
 }
 
-#ifndef DBGHELP_STATIC_LIB
 DWORD calc_crc32(HANDLE handle)
 {
     BYTE buffer[8192];
@@ -751,5 +750,3 @@ DWORD calc_crc32(HANDLE handle)
         crc = RtlComputeCrc32(crc, buffer, len);
     return crc;
 }
-#endif
-
diff --git a/dll/win32/dbghelp/elf_module.c b/dll/win32/dbghelp/elf_module.c
index 75f046105c7..7a6d9bed125 100644
--- a/dll/win32/dbghelp/elf_module.c
+++ b/dll/win32/dbghelp/elf_module.c
@@ -1,3 +1,12 @@
+#ifdef __REACTOS__
+#ifdef DBGHELP_STATIC_LIB
+#undef __ELF__
+#include "compat.h"
+#else
+#endif
+#include "dbghelp_private.h"
+#include "image_private.h"
+#else
 /*
  * File elf.c - processing of ELF files
  *
@@ -41,6 +50,7 @@
 #include "wine/library.h"
 #include "wine/debug.h"
 #include "wine/heap.h"
+#endif /* __REACTOS__ */
 
 #ifdef __ELF__
 
@@ -542,7 +552,7 @@ static BOOL elf_map_file(struct elf_map_file_data* emfd, 
struct image_file_map*
     return TRUE;
 }
 
-static BOOL elf_map_handle(HANDLE handle, struct image_file_map* fmap)
+BOOL elf_map_handle(HANDLE handle, struct image_file_map* fmap)
 {
     struct elf_map_file_data emfd;
     emfd.kind = from_handle;
@@ -958,244 +968,6 @@ static int elf_new_public_symbols(struct module* module, 
const struct hash_table
     return TRUE;
 }
 
-static BOOL elf_check_debug_link(const WCHAR* file, struct image_file_map* 
fmap, DWORD link_crc)
-{
-    HANDLE handle;
-    WCHAR *path;
-    BOOL ret;
-
-    path = get_dos_file_name(file);
-    handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, 
OPEN_EXISTING, 0, NULL);
-    heap_free(path);
-    if (handle == INVALID_HANDLE_VALUE) return FALSE;
-
-    if (link_crc)
-    {
-        DWORD crc = calc_crc32(handle);
-        if (crc != link_crc)
-        {
-            WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",  
debugstr_w(file), crc, link_crc);
-            CloseHandle(handle);
-            return FALSE;
-        }
-    }
-
-    ret = elf_map_handle(handle, fmap);
-    CloseHandle(handle);
-    return ret;
-}
-
-/******************************************************************
- *             elf_locate_debug_link
- *
- * Locate a filename from a .gnu_debuglink section, using the same
- * strategy as gdb:
- * "If the full name of the directory containing the executable is
- * execdir, and the executable has a debug link that specifies the
- * name debugfile, then GDB will automatically search for the
- * debugging information file in three places:
- *  - the directory containing the executable file (that is, it
- *    will look for a file named `execdir/debugfile',
- *  - a subdirectory of that directory named `.debug' (that is, the
- *    file `execdir/.debug/debugfile', and
- *  - a subdirectory of the global debug file directory that includes
- *    the executable's full path, and the name from the link (that is,
- *    the file `globaldebugdir/execdir/debugfile', where globaldebugdir
- *    is the global debug file directory, and execdir has been turned
- *    into a relative path)." (from GDB manual)
- */
-static BOOL elf_locate_debug_link(const struct module* module, struct 
image_file_map* fmap, const char* filename, DWORD crc)
-{
-    static const WCHAR globalDebugDirW[] = 
{'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
-    static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
-    const size_t globalDebugDirLen = ARRAY_SIZE(globalDebugDirW);
-    size_t filename_len, path_len;
-    WCHAR* p = NULL;
-    WCHAR* slash;
-    WCHAR* slash2;
-    struct image_file_map* fmap_link = NULL;
-
-    fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
-    if (!fmap_link) return FALSE;
-
-    filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
-    path_len = strlenW(module->module.LoadedImageName);
-    p = HeapAlloc(GetProcessHeap(), 0,
-                  (globalDebugDirLen + path_len + 6 + 1 + filename_len + 1) * 
sizeof(WCHAR));
-    if (!p) goto found;
-
-    /* we prebuild the string with "execdir" */
-    strcpyW(p, module->module.LoadedImageName);
-    slash = p;
-    if ((slash2 = strrchrW(slash, '/'))) slash = slash2 + 1;
-    if ((slash2 = strrchrW(slash, '\\'))) slash = slash2 + 1;
-
-    /* testing execdir/filename */
-    MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
-    if (elf_check_debug_link(p, fmap_link, crc)) goto found;
-
-    /* testing execdir/.debug/filename */
-    memcpy(slash, dotDebugW, sizeof(dotDebugW));
-    MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + 
ARRAY_SIZE(dotDebugW), filename_len);
-    if (elf_check_debug_link(p, fmap_link, crc)) goto found;
-
-    /* testing globaldebugdir/execdir/filename */
-    memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
-    memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
-    slash += globalDebugDirLen;
-    MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
-    if (elf_check_debug_link(p, fmap_link, crc)) goto found;
-
-    /* finally testing filename */
-    if (elf_check_debug_link(slash, fmap_link, crc)) goto found;
-
-
-    WARN("Couldn't locate or map %s\n", filename);
-    HeapFree(GetProcessHeap(), 0, p);
-    HeapFree(GetProcessHeap(), 0, fmap_link);
-    return FALSE;
-
-found:
-    TRACE("Located debug information file %s at %s\n", filename, 
debugstr_w(p));
-    HeapFree(GetProcessHeap(), 0, p);
-    fmap->alternate = fmap_link;
-    return TRUE;
-}
-
-/******************************************************************
- *             elf_locate_build_id_target
- *
- * Try to find the .so file containing the debug info out of the build-id note 
information
- */
-static BOOL elf_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','/'};
-    static const WCHAR dotDebug0W[] = {'.','d','e','b','u','g',0};
-    struct image_file_map* fmap_link = NULL;
-    WCHAR* p;
-    WCHAR* z;
-    const BYTE* idend = id + idlen;
-
-    fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
-    if (!fmap_link) return FALSE;
-
-    p = HeapAlloc(GetProcessHeap(), 0,
-                  sizeof(globalDebugDirW) + sizeof(buildidW) +
-                  (idlen * 2 + 1) * sizeof(WCHAR) + sizeof(dotDebug0W));
-    z = p;
-    memcpy(z, globalDebugDirW, sizeof(globalDebugDirW));
-    z += ARRAY_SIZE(globalDebugDirW);
-    memcpy(z, buildidW, sizeof(buildidW));
-    z += ARRAY_SIZE(buildidW);
-
-    if (id < idend)
-    {
-        *z++ = "0123456789abcdef"[*id >> 4  ];
-        *z++ = "0123456789abcdef"[*id & 0x0F];
-        id++;
-    }
-    if (id < idend)
-        *z++ = '/';
-    while (id < idend)
-    {
-        *z++ = "0123456789abcdef"[*id >> 4  ];
-        *z++ = "0123456789abcdef"[*id & 0x0F];
-        id++;
-    }
-    memcpy(z, dotDebug0W, sizeof(dotDebug0W));
-    TRACE("checking %s\n", wine_dbgstr_w(p));
-
-    if (elf_check_debug_link(p, fmap_link, 0))
-    {
-        struct image_section_map buildid_sect;
-        if (image_find_section(fmap_link, ".note.gnu.build-id", &buildid_sect))
-        {
-            const uint32_t* note;
-
-            note = (const uint32_t*)image_map_section(&buildid_sect);
-            if (note != IMAGE_NO_MAP)
-            {
-                /* the usual ELF note structure: name-size desc-size type 
<name> <desc> */
-                if (note[2] == NT_GNU_BUILD_ID)
-                {
-                    if (note[1] == idlen &&
-                        !memcmp(note + 3 + ((note[0] + 3) >> 2), idend - 
idlen, idlen))
-                    {
-                        TRACE("Located debug information file at %s\n", 
debugstr_w(p));
-                        HeapFree(GetProcessHeap(), 0, p);
-                        fmap->alternate = fmap_link;
-                        return TRUE;
-                    }
-                    WARN("mismatch in buildid information for %s\n", 
wine_dbgstr_w(p));
-                }
-            }
-            image_unmap_section(&buildid_sect);
-        }
-        image_unmap_file(fmap_link);
-    }
-
-    TRACE("not found\n");
-    HeapFree(GetProcessHeap(), 0, p);
-    HeapFree(GetProcessHeap(), 0, fmap_link);
-    return FALSE;
-}
-
-/******************************************************************
- *             elf_check_alternate
- *
- * Load alternate files for a given ELF file, looking at either 
.note.gnu_build-id
- * or .gnu_debuglink sections.
- */
-static BOOL elf_check_alternate(struct image_file_map* fmap, const struct 
module* module)
-{
-    BOOL ret = FALSE;
-    BOOL found = FALSE;
-    struct image_section_map buildid_sect, debuglink_sect;
-
-    /* if present, add the .gnu_debuglink file as an alternate to current one 
*/
-    if (image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
-    {
-        const uint32_t* note;
-
-        found = TRUE;
-        note = (const uint32_t*)image_map_section(&buildid_sect);
-        if (note != IMAGE_NO_MAP)
-        {
-            /* the usual ELF note structure: name-size desc-size type <name> 
<desc> */
-            if (note[2] == NT_GNU_BUILD_ID)
-            {
-                ret = elf_locate_build_id_target(fmap, (const BYTE*)(note + 3 
+ ((note[0] + 3) >> 2)), note[1]);
-            }
-        }
-        image_unmap_section(&buildid_sect);
-    }
-    /* if present, add the .gnu_debuglink file as an alternate to current one 
*/
-    if (!ret && image_find_section(fmap, ".gnu_debuglink", &debuglink_sect))
-    {
-        const char* dbg_link;
-
-        found = TRUE;
-        dbg_link = (const char*)image_map_section(&debuglink_sect);
-        if (dbg_link != IMAGE_NO_MAP)
-        {
-            /* The content of a debug link section is:
-             * 1/ a NULL terminated string, containing the file name for the
-             *    debug info
-             * 2/ padding on 4 byte boundary
-             * 3/ CRC of the linked ELF file
-             */
-            DWORD crc = *(const DWORD*)(dbg_link + 
((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
-            ret = elf_locate_debug_link(module, fmap, dbg_link, crc);
-            if (!ret)
-                WARN("Couldn't load linked debug file for %s\n",
-                     debugstr_w(module->module.ModuleName));
-        }
-        image_unmap_section(&debuglink_sect);
-    }
-    return found ? ret : TRUE;
-}
-
 /******************************************************************
  *             elf_load_debug_info_from_map
  *
@@ -1236,7 +1008,7 @@ static BOOL elf_load_debug_info_from_map(struct module* 
module,
         struct image_section_map stab_sect, stabstr_sect;
 
         /* check if we need an alternate file (from debuglink or build-id) */
-        ret = elf_check_alternate(fmap, module);
+        ret = image_check_alternate(fmap, module);
 
         if (image_find_section(fmap, ".stab", &stab_sect) &&
             image_find_section(fmap, ".stabstr", &stabstr_sect))
@@ -2030,6 +1802,11 @@ BOOL     elf_synchronize_module_list(struct process* pcs)
 
 #else  /* !__ELF__ */
 
+BOOL elf_map_handle(HANDLE handle, struct image_file_map* fmap)
+{
+    return FALSE;
+}
+
 BOOL   elf_synchronize_module_list(struct process* pcs)
 {
     return FALSE;
diff --git a/dll/win32/dbghelp/image_private.h 
b/dll/win32/dbghelp/image_private.h
index 8a8cd9a4454..579d7af37cf 100644
--- a/dll/win32/dbghelp/image_private.h
+++ b/dll/win32/dbghelp/image_private.h
@@ -54,6 +54,13 @@
 #ifndef SHT_NULL
 #define SHT_NULL        0
 #endif
+#ifndef NT_GNU_BUILD_ID
+#define NT_GNU_BUILD_ID 3
+#endif
+#elif defined(__REACTOS__)
+#ifndef NT_GNU_BUILD_ID
+#define NT_GNU_BUILD_ID 3
+#endif
 #endif
 
 /* structure holding information while handling an ELF image
@@ -132,6 +139,10 @@ struct image_section_map
     long                        sidx;
 };
 
+BOOL image_check_alternate(struct image_file_map* fmap, const struct module* 
module) DECLSPEC_HIDDEN;
+
+BOOL elf_map_handle(HANDLE handle, struct image_file_map* fmap) 
DECLSPEC_HIDDEN;
+
 struct image_file_map_ops
 {
     const char* (*map_section)(struct image_section_map* ism);
diff --git a/dll/win32/dbghelp/module.c b/dll/win32/dbghelp/module.c
index 5243b53a829..16d6670a737 100644
--- a/dll/win32/dbghelp/module.c
+++ b/dll/win32/dbghelp/module.c
@@ -26,14 +26,13 @@
 #include <assert.h>
 
 #include "dbghelp_private.h"
+#include "image_private.h"
 #ifndef DBGHELP_STATIC_LIB
 #include "psapi.h"
 #include "winternl.h"
 #include "wine/debug.h"
-#ifndef __REACTOS__
 #include "wine/heap.h"
 #endif
-#endif
 
 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
 
@@ -560,6 +559,252 @@ static BOOL refresh_module_list(struct process* pcs)
 }
 #endif
 
+static BOOL image_check_debug_link(const WCHAR* file, struct image_file_map* 
fmap, DWORD link_crc)
+{
+    HANDLE handle;
+#ifndef DBGHELP_STATIC_LIB
+    WCHAR *path;
+#endif
+    BOOL ret;
+
+#ifndef DBGHELP_STATIC_LIB
+    path = get_dos_file_name(file);
+    handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, 
OPEN_EXISTING, 0, NULL);
+    heap_free(path);
+#else
+    handle = CreateFileW(file, GENERIC_READ, FILE_SHARE_READ, NULL, 
OPEN_EXISTING, 0, NULL);
+#endif
+    if (handle == INVALID_HANDLE_VALUE) return FALSE;
+
+    if (link_crc)
+    {
+        DWORD crc = calc_crc32(handle);
+        if (crc != link_crc)
+        {
+            WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",  
debugstr_w(file), crc, link_crc);
+            CloseHandle(handle);
+            return FALSE;
+        }
+    }
+
+    ret = elf_map_handle(handle, fmap);
+    CloseHandle(handle);
+    return ret;
+}
+
+/******************************************************************
+ *             image_locate_debug_link
+ *
+ * Locate a filename from a .gnu_debuglink section, using the same
+ * strategy as gdb:
+ * "If the full name of the directory containing the executable is
+ * execdir, and the executable has a debug link that specifies the
+ * name debugfile, then GDB will automatically search for the
+ * debugging information file in three places:
+ *  - the directory containing the executable file (that is, it
+ *    will look for a file named `execdir/debugfile',
+ *  - a subdirectory of that directory named `.debug' (that is, the
+ *    file `execdir/.debug/debugfile', and
+ *  - a subdirectory of the global debug file directory that includes
+ *    the executable's full path, and the name from the link (that is,
+ *    the file `globaldebugdir/execdir/debugfile', where globaldebugdir
+ *    is the global debug file directory, and execdir has been turned
+ *    into a relative path)." (from GDB manual)
+ */
+static BOOL image_locate_debug_link(const struct module* module, struct 
image_file_map* fmap, const char* filename, DWORD crc)
+{
+    static const WCHAR globalDebugDirW[] = 
{'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
+    static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
+    const size_t globalDebugDirLen = ARRAY_SIZE(globalDebugDirW);
+    size_t filename_len, path_len;
+    WCHAR* p = NULL;
+    WCHAR* slash;
+    WCHAR* slash2;
+    struct image_file_map* fmap_link = NULL;
+
+    fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
+    if (!fmap_link) return FALSE;
+
+    filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
+    path_len = strlenW(module->module.LoadedImageName);
+    p = HeapAlloc(GetProcessHeap(), 0,
+                  (globalDebugDirLen + path_len + 6 + 1 + filename_len + 1) * 
sizeof(WCHAR));
+    if (!p) goto found;
+
+    /* we prebuild the string with "execdir" */
+    strcpyW(p, module->module.LoadedImageName);
+    slash = p;
+    if ((slash2 = strrchrW(slash, '/'))) slash = slash2 + 1;
+    if ((slash2 = strrchrW(slash, '\\'))) slash = slash2 + 1;
+
+    /* testing execdir/filename */
+    MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
+    if (image_check_debug_link(p, fmap_link, crc)) goto found;
+
+    /* testing execdir/.debug/filename */
+    memcpy(slash, dotDebugW, sizeof(dotDebugW));
+    MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + 
ARRAY_SIZE(dotDebugW), filename_len);
+    if (image_check_debug_link(p, fmap_link, crc)) goto found;
+
+#ifndef __REACTOS__
+    /* testing globaldebugdir/execdir/filename */
+    memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
+    memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
+    slash += globalDebugDirLen;
+    MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
+    if (image_check_debug_link(p, fmap_link, crc)) goto found;
+#endif
+
+    /* finally testing filename */
+    if (image_check_debug_link(slash, fmap_link, crc)) goto found;
+
+
+    WARN("Couldn't locate or map %s\n", filename);
+    HeapFree(GetProcessHeap(), 0, p);
+    HeapFree(GetProcessHeap(), 0, fmap_link);
+    return FALSE;
+
+found:
+    TRACE("Located debug information file %s at %s\n", filename, 
debugstr_w(p));
+    HeapFree(GetProcessHeap(), 0, p);
+    fmap->alternate = fmap_link;
+    return TRUE;
+}
+
+/******************************************************************
+ *             image_locate_build_id_target
+ *
+ * Try to find the .so file containing the debug info out of the build-id note 
information
+ */
+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','/'};
+    static const WCHAR dotDebug0W[] = {'.','d','e','b','u','g',0};
+    struct image_file_map* fmap_link = NULL;
+    WCHAR* p;
+    WCHAR* z;
+    const BYTE* idend = id + idlen;
+
+    fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
+    if (!fmap_link) return FALSE;
+
+    p = HeapAlloc(GetProcessHeap(), 0,
+                  sizeof(globalDebugDirW) + sizeof(buildidW) +
+                  (idlen * 2 + 1) * sizeof(WCHAR) + sizeof(dotDebug0W));
+    z = p;
+    memcpy(z, globalDebugDirW, sizeof(globalDebugDirW));
+    z += ARRAY_SIZE(globalDebugDirW);
+    memcpy(z, buildidW, sizeof(buildidW));
+    z += ARRAY_SIZE(buildidW);
+
+    if (id < idend)
+    {
+        *z++ = "0123456789abcdef"[*id >> 4  ];
+        *z++ = "0123456789abcdef"[*id & 0x0F];
+        id++;
+    }
+    if (id < idend)
+        *z++ = '/';
+    while (id < idend)
+    {
+        *z++ = "0123456789abcdef"[*id >> 4  ];
+        *z++ = "0123456789abcdef"[*id & 0x0F];
+        id++;
+    }
+    memcpy(z, dotDebug0W, sizeof(dotDebug0W));
+    TRACE("checking %s\n", wine_dbgstr_w(p));
+
+    if (image_check_debug_link(p, fmap_link, 0))
+    {
+        struct image_section_map buildid_sect;
+        if (image_find_section(fmap_link, ".note.gnu.build-id", &buildid_sect))
+        {
+            const UINT32* note;
+
+            note = (const UINT32*)image_map_section(&buildid_sect);
+            if (note != IMAGE_NO_MAP)
+            {
+                /* the usual ELF note structure: name-size desc-size type 
<name> <desc> */
+                if (note[2] == NT_GNU_BUILD_ID)
+                {
+                    if (note[1] == idlen &&
+                        !memcmp(note + 3 + ((note[0] + 3) >> 2), idend - 
idlen, idlen))
+                    {
+                        TRACE("Located debug information file at %s\n", 
debugstr_w(p));
+                        HeapFree(GetProcessHeap(), 0, p);
+                        fmap->alternate = fmap_link;
+                        return TRUE;
+                    }
+                    WARN("mismatch in buildid information for %s\n", 
wine_dbgstr_w(p));
+                }
+            }
+            image_unmap_section(&buildid_sect);
+        }
+        image_unmap_file(fmap_link);
+    }
+
+    TRACE("not found\n");
+    HeapFree(GetProcessHeap(), 0, p);
+    HeapFree(GetProcessHeap(), 0, fmap_link);
+    return FALSE;
+}
+
+/******************************************************************
+ *             image_check_alternate
+ *
+ * Load alternate files for a given image file, looking at either 
.note.gnu_build-id
+ * or .gnu_debuglink sections.
+ */
+BOOL image_check_alternate(struct image_file_map* fmap, const struct module* 
module)
+{
+    BOOL ret = FALSE;
+    BOOL found = FALSE;
+    struct image_section_map buildid_sect, debuglink_sect;
+
+    /* if present, add the .gnu_debuglink file as an alternate to current one 
*/
+    if (image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
+    {
+        const UINT32* note;
+
+        found = TRUE;
+        note = (const UINT32*)image_map_section(&buildid_sect);
+        if (note != IMAGE_NO_MAP)
+        {
+            /* the usual ELF note structure: name-size desc-size type <name> 
<desc> */
+            if (note[2] == NT_GNU_BUILD_ID)
+            {
+                ret = image_locate_build_id_target(fmap, (const BYTE*)(note + 
3 + ((note[0] + 3) >> 2)), note[1]);
+            }
+        }
+        image_unmap_section(&buildid_sect);
+    }
+    /* if present, add the .gnu_debuglink file as an alternate to current one 
*/
+    if (!ret && image_find_section(fmap, ".gnu_debuglink", &debuglink_sect))
+    {
+        const char* dbg_link;
+
+        found = TRUE;
+        dbg_link = (const char*)image_map_section(&debuglink_sect);
+        if (dbg_link != IMAGE_NO_MAP)
+        {
+            /* The content of a debug link section is:
+             * 1/ a NULL terminated string, containing the file name for the
+             *    debug info
+             * 2/ padding on 4 byte boundary
+             * 3/ CRC of the linked file
+             */
+            DWORD crc = *(const DWORD*)(dbg_link + 
((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
+            ret = image_locate_debug_link(module, fmap, dbg_link, crc);
+            if (!ret)
+                WARN("Couldn't load linked debug file for %s\n",
+                     debugstr_w(module->module.ModuleName));
+        }
+        image_unmap_section(&debuglink_sect);
+    }
+    return found ? ret : TRUE;
+}
+
 /***********************************************************************
  *                     SymLoadModule (DBGHELP.@)
  */
diff --git a/dll/win32/dbghelp/path.c b/dll/win32/dbghelp/path.c
index 21ff3ac5dfc..feb951ad55d 100644
--- a/dll/win32/dbghelp/path.c
+++ b/dll/win32/dbghelp/path.c
@@ -698,7 +698,6 @@ BOOL path_find_symbol_file(const struct process* pcs, const 
struct module* modul
     return FALSE;
 }
 
-#ifndef __REACTOS__
 WCHAR *get_dos_file_name(const WCHAR *filename)
 {
     WCHAR *dos_path;
@@ -721,4 +720,3 @@ WCHAR *get_dos_file_name(const WCHAR *filename)
     }
     return dos_path;
 }
-#endif
diff --git a/sdk/tools/winesync/dbghelp.cfg b/sdk/tools/winesync/dbghelp.cfg
index edd9c2dc5d4..50838ce5e31 100644
--- a/sdk/tools/winesync/dbghelp.cfg
+++ b/sdk/tools/winesync/dbghelp.cfg
@@ -4,4 +4,4 @@ files:
   include/dbghelp.h: sdk/include/psdk/dbghelp.h
   include/wine/mscvpdb.h: sdk/include/reactos/wine/mscvpdb.h
 tags:
-  wine: d38682397997c56359f6d161467a56ffe5bfa9fa
+  wine: 4694801e4f58f0e856327d251ac98cfb573fd83b

Reply via email to