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

commit 15dda9ebafbf0f9e6c07962e59d1afcd8df13291
Author:     Amine Khaldi <[email protected]>
AuthorDate: Thu Mar 15 12:28:19 2018 +0100
Commit:     Amine Khaldi <[email protected]>
CommitDate: Thu Mar 15 12:28:19 2018 +0100

    [IMAGEHLP] Sync with Wine Staging 3.3. CORE-14434
---
 dll/win32/imagehlp/access.c        |  99 +++++++++-------
 dll/win32/imagehlp/imagehlp_main.c |   7 +-
 dll/win32/imagehlp/integrity.c     |  12 +-
 dll/win32/imagehlp/modify.c        | 227 ++++++++++++++++++++++++++++---------
 dll/win32/imagehlp/precomp.h       |   4 +-
 media/doc/README.WINE              |   2 +-
 6 files changed, 255 insertions(+), 96 deletions(-)

diff --git a/dll/win32/imagehlp/access.c b/dll/win32/imagehlp/access.c
index 367efac5d5..897f2d553b 100644
--- a/dll/win32/imagehlp/access.c
+++ b/dll/win32/imagehlp/access.c
@@ -18,15 +18,22 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#include "precomp.h"
-
-#include <winternl.h>
+#include <stdarg.h>
+#include <string.h>
+#include "windef.h"
+#include "winbase.h"
+#include "winnt.h"
+#include "winternl.h"
+#include "winerror.h"
+#include "wine/debug.h"
+#include "imagehlp.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
 
 /***********************************************************************
  *           Data
  */
-
-static PLOADED_IMAGE IMAGEHLP_pFirstLoadedImage=NULL;
+LIST_ENTRY image_list = { &image_list, &image_list };
 
 DECLSPEC_HIDDEN extern HANDLE IMAGEHLP_hHeap;
 
@@ -61,57 +68,69 @@ DWORD WINAPI GetImageUnusedHeaderBytes(
 /***********************************************************************
  *             ImageLoad (IMAGEHLP.@)
  */
-PLOADED_IMAGE WINAPI ImageLoad(PCSTR DllName, PCSTR DllPath)
+PLOADED_IMAGE WINAPI ImageLoad(PCSTR dll_name, PCSTR dll_path)
 {
-  PLOADED_IMAGE pLoadedImage;
-
-  FIXME("(%s, %s): stub\n", DllName, DllPath);
-         
-  pLoadedImage = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(LOADED_IMAGE));
-  if (pLoadedImage)
-    pLoadedImage->FileHeader = HeapAlloc(IMAGEHLP_hHeap, 0, 
sizeof(IMAGE_NT_HEADERS));
-  
-  return pLoadedImage;
+    LOADED_IMAGE *image;
+
+    TRACE("(%s, %s)\n", dll_name, dll_path);
+
+    image = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(*image));
+    if (!image) return NULL;
+
+    if (!MapAndLoad(dll_name, dll_path, image, TRUE, TRUE))
+    {
+        HeapFree(IMAGEHLP_hHeap, 0, image);
+        return NULL;
+    }
+
+    image->Links.Flink = image_list.Flink;
+    image->Links.Blink = &image_list;
+    image_list.Flink->Blink = &image->Links;
+    image_list.Flink = &image->Links;
+
+    return image;
 }
 
 /***********************************************************************
  *             ImageUnload (IMAGEHLP.@)
  */
-BOOL WINAPI ImageUnload(PLOADED_IMAGE pLoadedImage)
+BOOL WINAPI ImageUnload(PLOADED_IMAGE loaded_image)
 {
-  LIST_ENTRY *pCurrent, *pFind;
+    LIST_ENTRY *entry, *mark;
+    PLOADED_IMAGE image;
+
+    FIXME("(%p)\n", loaded_image);
 
-  TRACE("(%p)\n", pLoadedImage);
-  
-  if(!IMAGEHLP_pFirstLoadedImage || !pLoadedImage)
+    if (!loaded_image)
     {
-      /* No image loaded or null pointer */
-      SetLastError(ERROR_INVALID_PARAMETER);
-      return FALSE;
+        /* No image loaded or null pointer */
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
     }
 
-  pFind=&pLoadedImage->Links;
-  pCurrent=&IMAGEHLP_pFirstLoadedImage->Links;
-  while((pCurrent != pFind) &&
-    (pCurrent != NULL))
-      pCurrent = pCurrent->Flink;
-  if(!pCurrent)
+    /* FIXME: do we really need to check this? */
+    mark = &image_list;
+    for (entry = mark->Flink; entry != mark; entry = entry->Flink)
     {
-      /* Not found */
-      SetLastError(ERROR_INVALID_PARAMETER);
-      return FALSE;
+        image = CONTAINING_RECORD(entry, LOADED_IMAGE, Links);
+        if (image == loaded_image)
+            break;
     }
 
-  if(pCurrent->Blink)
-    pCurrent->Blink->Flink = pCurrent->Flink;
-  else
-    IMAGEHLP_pFirstLoadedImage = pCurrent->Flink?CONTAINING_RECORD(
-      pCurrent->Flink, LOADED_IMAGE, Links):NULL;
+    if (entry == mark)
+    {
+        /* Not found */
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
 
-  if(pCurrent->Flink)
-    pCurrent->Flink->Blink = pCurrent->Blink;
+    entry->Blink->Flink = entry->Flink;
+    entry->Flink->Blink = entry->Blink;
 
-  return FALSE;
+    UnMapAndLoad(loaded_image);
+    HeapFree(IMAGEHLP_hHeap, 0, loaded_image);
+
+    return TRUE;
 }
 
 /***********************************************************************
diff --git a/dll/win32/imagehlp/imagehlp_main.c 
b/dll/win32/imagehlp/imagehlp_main.c
index a2b79d49b8..e505126381 100644
--- a/dll/win32/imagehlp/imagehlp_main.c
+++ b/dll/win32/imagehlp/imagehlp_main.c
@@ -18,7 +18,12 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#include "precomp.h"
+#include <stdarg.h>
+
+#include "windef.h"
+#include "winbase.h"
+#include "imagehlp.h"
+#include "wine/debug.h"
 
 /**********************************************************************/
 DECLSPEC_HIDDEN HANDLE IMAGEHLP_hHeap = NULL;
diff --git a/dll/win32/imagehlp/integrity.c b/dll/win32/imagehlp/integrity.c
index 9d79fa8f61..d86ec11bd5 100644
--- a/dll/win32/imagehlp/integrity.c
+++ b/dll/win32/imagehlp/integrity.c
@@ -22,7 +22,17 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#include "precomp.h"
+#include <stdarg.h>
+
+#include "windef.h"
+#include "winbase.h"
+#include "winerror.h"
+#include "winternl.h"
+#include "winnt.h"
+#include "imagehlp.h"
+#include "wine/debug.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
 
 /*
  * These functions are partially documented at:
diff --git a/dll/win32/imagehlp/modify.c b/dll/win32/imagehlp/modify.c
index 6adabb6771..ee68a4ef85 100644
--- a/dll/win32/imagehlp/modify.c
+++ b/dll/win32/imagehlp/modify.c
@@ -18,16 +18,25 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#include "precomp.h"
+#include <stdarg.h>
 
-#include <wine/exception.h>
-#include <wine/winternl.h>
+#include "windef.h"
+#include "winbase.h"
+#include "winternl.h"
+#include "winerror.h"
+#include "wine/debug.h"
+#include "wine/exception.h"
+#include "imagehlp.h"
 
-static WORD CalcCheckSum(DWORD StartValue, LPVOID BaseAddress, DWORD 
WordCount);
+WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
 
+static WORD CalcCheckSum(DWORD StartValue, LPVOID BaseAddress, DWORD 
WordCount);
 
 /***********************************************************************
  *             BindImage (IMAGEHLP.@)
+ *
+ * NOTES
+ *   See BindImageEx
  */
 BOOL WINAPI BindImage(
   PCSTR ImageName, PCSTR DllPath, PCSTR SymbolPath)
@@ -37,42 +46,138 @@ BOOL WINAPI BindImage(
 
 /***********************************************************************
  *             BindImageEx (IMAGEHLP.@)
+ *
+ * Compute the virtual address of each function imported by a PE image
+ *
+ * PARAMS
+ *
+ *   Flags         [in] Bind options
+ *   ImageName     [in] File name of the image to be bound
+ *   DllPath       [in] Root of the fallback search path in case the ImageName 
file cannot be opened
+ *   SymbolPath    [in] Symbol file root search path
+ *   StatusRoutine [in] Pointer to a status routine which will be called 
during the binding process
+ *
+ * RETURNS
+ *   Success: TRUE
+ *   Failure: FALSE
+ *
+ * NOTES
+ *  Binding is not implemented yet, so far this function only enumerates
+ *  all imported dlls/functions and returns TRUE.
  */
 BOOL WINAPI BindImageEx(
   DWORD Flags, PCSTR ImageName, PCSTR DllPath, PCSTR SymbolPath,
   PIMAGEHLP_STATUS_ROUTINE StatusRoutine)
 {
-  FIXME("(%d, %s, %s, %s, %p): stub\n",
-    Flags, debugstr_a(ImageName), debugstr_a(DllPath),
-    debugstr_a(SymbolPath), StatusRoutine
-  );
-  return TRUE;
+    LOADED_IMAGE loaded_image;
+    const IMAGE_IMPORT_DESCRIPTOR *import_desc;
+    ULONG size;
+
+    FIXME("(%d, %s, %s, %s, %p): semi-stub\n",
+        Flags, debugstr_a(ImageName), debugstr_a(DllPath),
+        debugstr_a(SymbolPath), StatusRoutine
+    );
+
+    if (!(MapAndLoad(ImageName, DllPath, &loaded_image, TRUE, TRUE))) return 
FALSE;
+
+    if (!(import_desc = 
RtlImageDirectoryEntryToData((HMODULE)loaded_image.MappedAddress, FALSE,
+                                                     
IMAGE_DIRECTORY_ENTRY_IMPORT, &size)))
+    {
+        UnMapAndLoad(&loaded_image);
+        return TRUE; /* No imported modules means nothing to bind, so we're 
done. */
+    }
+
+    /* FIXME: Does native imagehlp support both 32-bit and 64-bit PE 
executables? */
+#ifdef _WIN64
+    if (loaded_image.FileHeader->OptionalHeader.Magic != 
IMAGE_NT_OPTIONAL_HDR64_MAGIC)
+#else
+    if (loaded_image.FileHeader->OptionalHeader.Magic != 
IMAGE_NT_OPTIONAL_HDR32_MAGIC)
+#endif
+    {
+        FIXME("Wrong architecture in PE header, unable to enumerate 
imports\n");
+        UnMapAndLoad(&loaded_image);
+        return TRUE;
+    }
+
+    for (; import_desc->Name && import_desc->FirstThunk; ++import_desc)
+    {
+        IMAGE_THUNK_DATA *thunk;
+        char dll_fullname[MAX_PATH];
+        const char *dll_name;
+
+        if (!(dll_name = ImageRvaToVa(loaded_image.FileHeader, 
loaded_image.MappedAddress,
+                                      import_desc->Name, 0)))
+        {
+            UnMapAndLoad(&loaded_image);
+            SetLastError(ERROR_INVALID_ACCESS); /* FIXME */
+            return FALSE;
+        }
+
+        if (StatusRoutine)
+            StatusRoutine(BindImportModule, ImageName, dll_name, 0, 0);
+
+        if (!SearchPathA(DllPath, dll_name, 0, sizeof(dll_fullname), 
dll_fullname, 0))
+        {
+            UnMapAndLoad(&loaded_image);
+            SetLastError(ERROR_FILE_NOT_FOUND);
+            return FALSE;
+        }
+
+        if (!(thunk = ImageRvaToVa(loaded_image.FileHeader, 
loaded_image.MappedAddress,
+                                   import_desc->OriginalFirstThunk ? 
import_desc->OriginalFirstThunk :
+                                   import_desc->FirstThunk, 0)))
+        {
+            ERR("Can't grab thunk data of %s, going to next imported DLL\n", 
dll_name);
+            continue;
+        }
+
+        for (; thunk->u1.Ordinal; ++thunk)
+        {
+            /* Ignoring ordinal imports for now */
+            if(!IMAGE_SNAP_BY_ORDINAL(thunk->u1.Ordinal))
+            {
+                IMAGE_IMPORT_BY_NAME *iibn;
+
+                if (!(iibn = ImageRvaToVa(loaded_image.FileHeader, 
loaded_image.MappedAddress,
+                                          thunk->u1.AddressOfData, 0)))
+                {
+                    ERR("Can't grab import by name info, skipping to next 
ordinal\n");
+                    continue;
+                }
+
+                if (StatusRoutine)
+                    StatusRoutine(BindImportProcedure, ImageName, 
dll_fullname, 0, (ULONG_PTR)iibn->Name);
+            }
+        }
+    }
+
+    UnMapAndLoad(&loaded_image);
+    return TRUE;
 }
 
 
 /***********************************************************************
  *             CheckSum (internal)
  */
-static WORD CalcCheckSum(
-  DWORD StartValue, LPVOID BaseAddress, DWORD WordCount)
+static WORD CalcCheckSum(DWORD StartValue, LPVOID BaseAddress, DWORD ByteCount)
 {
-   LPWORD Ptr;
-   DWORD Sum;
-   DWORD i;
-
-   Sum = StartValue;
-   Ptr = (LPWORD)BaseAddress;
-   for (i = 0; i < WordCount; i++)
-     {
-       Sum += *Ptr;
-       if (HIWORD(Sum) != 0)
-         {
-            Sum = LOWORD(Sum) + HIWORD(Sum);
-         }
-       Ptr++;
-     }
-
-   return (WORD)(LOWORD(Sum) + HIWORD(Sum));
+    LPWORD Ptr;
+    DWORD Sum, i;
+
+    Sum = StartValue;
+    Ptr = (LPWORD)BaseAddress;
+    for (i = ByteCount; i > 1; i -= 2)
+    {
+        Sum += *Ptr;
+        if (HIWORD(Sum) != 0)
+            Sum = LOWORD(Sum) + HIWORD(Sum);
+        Ptr++;
+    }
+
+    if (i == 1)
+        Sum += *(BYTE *)Ptr;
+
+    return (WORD)(LOWORD(Sum) + HIWORD(Sum));
 }
 
 
@@ -86,37 +191,57 @@ PIMAGE_NT_HEADERS WINAPI CheckSumMappedFile(
   IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *) BaseAddress;
   PIMAGE_NT_HEADERS32 Header32;
   PIMAGE_NT_HEADERS64 Header64;
+  PIMAGE_NT_HEADERS ret = NULL;
   DWORD *ChecksumFile;
   DWORD CalcSum;
-  DWORD HdrSum;
+  DWORD HdrSum = 0;
 
   TRACE("(%p, %d, %p, %p)\n",
     BaseAddress, FileLength, HeaderSum, CheckSum
   );
 
-  CalcSum = (DWORD)CalcCheckSum(0,
-                               BaseAddress,
-                               (FileLength + 1) / sizeof(WORD));
-
-  if (dos->e_magic != IMAGE_DOS_SIGNATURE)
-    return NULL;
-
-  Header32 = (IMAGE_NT_HEADERS32 *)((char *)dos + dos->e_lfanew);
-
-  if (Header32->Signature != IMAGE_NT_SIGNATURE)
-    return NULL;
+  CalcSum = (DWORD)CalcCheckSum(0, BaseAddress, FileLength);
 
-  if (Header32->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
-    ChecksumFile = &Header32->OptionalHeader.CheckSum;
-  else if (Header32->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
+  __TRY
   {
-    Header64 = (IMAGE_NT_HEADERS64 *)Header32;
-    ChecksumFile = &Header64->OptionalHeader.CheckSum;
+    if (dos->e_magic != IMAGE_DOS_SIGNATURE)
+#ifdef __REACTOS__
+      _SEH2_LEAVE;
+#else
+      break;
+#endif
+
+    Header32 = (IMAGE_NT_HEADERS32 *)((char *)dos + dos->e_lfanew);
+    if (Header32->Signature != IMAGE_NT_SIGNATURE)
+#ifdef __REACTOS__
+      _SEH2_LEAVE;
+#else
+      break;
+#endif
+
+    ret = (PIMAGE_NT_HEADERS)Header32;
+
+    if (Header32->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
+      ChecksumFile = &Header32->OptionalHeader.CheckSum;
+    else if (Header32->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
+    {
+      Header64 = (IMAGE_NT_HEADERS64 *)Header32;
+      ChecksumFile = &Header64->OptionalHeader.CheckSum;
+    }
+    else
+#ifdef __REACTOS__
+      _SEH2_LEAVE;
+#else
+      break;
+#endif
+
+    HdrSum = *ChecksumFile;
   }
-  else
-    return NULL;
-
-  HdrSum = *ChecksumFile;
+  __EXCEPT_PAGE_FAULT
+  {
+    /* nothing */
+  }
+  __ENDTRY
 
   /* Subtract image checksum from calculated checksum. */
   /* fix low word of checksum */
@@ -143,9 +268,9 @@ PIMAGE_NT_HEADERS WINAPI CheckSumMappedFile(
   CalcSum += FileLength;
 
   *CheckSum = CalcSum;
-  *HeaderSum = *ChecksumFile;
+  *HeaderSum = HdrSum;
 
-  return (PIMAGE_NT_HEADERS) Header32;
+  return ret;
 }
 
 /***********************************************************************
diff --git a/dll/win32/imagehlp/precomp.h b/dll/win32/imagehlp/precomp.h
index e9f8eef0a8..4195b3095e 100644
--- a/dll/win32/imagehlp/precomp.h
+++ b/dll/win32/imagehlp/precomp.h
@@ -1,3 +1,4 @@
+
 #ifndef _IMAGEHLP_PCH_
 #define _IMAGEHLP_PCH_
 
@@ -11,6 +12,5 @@
 #include <imagehlp.h>
 
 #include <wine/debug.h>
-WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
 
-#endif /* _IMAGEHLP_PCH_ */
+#endif /* !_IMAGEHLP_PCH_ */
diff --git a/media/doc/README.WINE b/media/doc/README.WINE
index b235854fff..cd8730679d 100644
--- a/media/doc/README.WINE
+++ b/media/doc/README.WINE
@@ -76,7 +76,7 @@ reactos/dll/win32/httpapi             # Synced to 
WineStaging-3.3
 reactos/dll/win32/iccvid              # Synced to WineStaging-3.3
 reactos/dll/win32/ieframe             # Synced to WineStaging-3.3
 reactos/dll/win32/imaadp32.acm        # Synced to WineStaging-3.3
-reactos/dll/win32/imagehlp            # Synced to Wine-3.0
+reactos/dll/win32/imagehlp            # Synced to WineStaging-3.3
 reactos/dll/win32/imm32               # Synced to Wine-2.22
 reactos/dll/win32/inetcomm            # Synced to Wine-3.0
 reactos/dll/win32/inetmib1            # Synced to WineStaging-2.9

Reply via email to