This is the mail archive of the cygwin-patches mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 3/4] Cygwin: pty: Move function hook_api() into hookapi.cc.


- PTY uses Win32 API hook for pseudo console suppot. The function
  hook_api() is used for this purpose and defined in fhandler_tty.cc
  previously. This patch moves it into hookapi.cc.
---
 winsup/cygwin/fhandler_tty.cc | 44 -----------------------------------
 winsup/cygwin/hookapi.cc      | 34 +++++++++++++++++++++++++++
 winsup/cygwin/winsup.h        |  1 +
 3 files changed, 35 insertions(+), 44 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 94ef2f8d4..f76f7b262 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -75,50 +75,6 @@ static bool pcon_attached[NTTYS];
 static bool isHybrid;
 
 #if USE_API_HOOK
-/* Hook WIN32 API */
-static
-void *hook_api (const char *mname, const char *name, const void *fn)
-{
-  HMODULE hm = GetModuleHandle (mname);
-  PIMAGE_NT_HEADERS pExeNTHdr = PIMAGE_NT_HEADERS (PBYTE (hm)
-				   + PIMAGE_DOS_HEADER (hm)->e_lfanew);
-  DWORD importRVA = pExeNTHdr->OptionalHeader.DataDirectory
-    [IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
-  PIMAGE_IMPORT_DESCRIPTOR pdfirst =
-    (PIMAGE_IMPORT_DESCRIPTOR) ((char *) hm + importRVA);
-  for (PIMAGE_IMPORT_DESCRIPTOR pd = pdfirst; pd->FirstThunk; pd++)
-    {
-      if (pd->OriginalFirstThunk == 0)
-	continue;
-      PIMAGE_THUNK_DATA pt =
-	(PIMAGE_THUNK_DATA) ((char *) hm + pd->FirstThunk);
-      PIMAGE_THUNK_DATA pn =
-	(PIMAGE_THUNK_DATA) ((char *) hm + pd->OriginalFirstThunk);
-      for (PIMAGE_THUNK_DATA pi = pt; pn->u1.Ordinal; pi++, pn++)
-	{
-	  if (IMAGE_SNAP_BY_ORDINAL (pn->u1.Ordinal))
-	    continue;
-	  PIMAGE_IMPORT_BY_NAME pimp =
-	    (PIMAGE_IMPORT_BY_NAME) ((char *) hm + pn->u1.AddressOfData);
-	  if (strcmp (name, (char *) pimp->Name) != 0)
-	    continue;
-#ifdef __x86_64__
-#define THUNK_FUNC_TYPE ULONGLONG
-#else
-#define THUNK_FUNC_TYPE DWORD
-#endif
-	  DWORD ofl = PAGE_READWRITE;
-	  if (!VirtualProtect (pi, sizeof (THUNK_FUNC_TYPE), ofl, &ofl))
-	    return NULL;
-	  void *origfn = (void *) pi->u1.Function;
-	  pi->u1.Function = (THUNK_FUNC_TYPE) fn;
-	  VirtualProtect (pi, sizeof (THUNK_FUNC_TYPE), ofl, &ofl);
-	  return origfn;
-	}
-    }
-  return NULL;
-}
-
 static void
 set_switch_to_pcon (void)
 {
diff --git a/winsup/cygwin/hookapi.cc b/winsup/cygwin/hookapi.cc
index 4078e65bd..dcd9b1df8 100644
--- a/winsup/cygwin/hookapi.cc
+++ b/winsup/cygwin/hookapi.cc
@@ -428,6 +428,40 @@ hook_or_detect_cygwin (const char *name, const void *fn, WORD& subsys, HANDLE h)
   return fh.origfn;
 }
 
+/* Hook a function in any DLL such as kernel32.dll */
+/* The DLL must be loaded in advance. */
+/* Used in fhandler_tty.cc */
+void *hook_api (const char *mname, const char *name, const void *fn)
+{
+  HMODULE hm = GetModuleHandle (mname);
+  PIMAGE_NT_HEADERS pExeNTHdr =
+    rva (PIMAGE_NT_HEADERS, hm, PIMAGE_DOS_HEADER (hm)->e_lfanew);
+  DWORD importRVA = pExeNTHdr->OptionalHeader.DataDirectory
+    [IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
+  PIMAGE_IMPORT_DESCRIPTOR pdfirst =
+    rva (PIMAGE_IMPORT_DESCRIPTOR, hm, importRVA);
+  for (PIMAGE_IMPORT_DESCRIPTOR pd = pdfirst; pd->FirstThunk; pd++)
+    {
+      if (pd->OriginalFirstThunk == 0)
+	continue;
+      PIMAGE_THUNK_DATA pt = rva (PIMAGE_THUNK_DATA, hm, pd->FirstThunk);
+      PIMAGE_THUNK_DATA pn =
+	rva (PIMAGE_THUNK_DATA, hm, pd->OriginalFirstThunk);
+      for (PIMAGE_THUNK_DATA pi = pt; pn->u1.Ordinal; pi++, pn++)
+	{
+	  if (IMAGE_SNAP_BY_ORDINAL (pn->u1.Ordinal))
+	    continue;
+	  PIMAGE_IMPORT_BY_NAME pimp =
+	    rva (PIMAGE_IMPORT_BY_NAME, hm, pn->u1.AddressOfData);
+	  if (strcmp (name, (char *) pimp->Name) != 0)
+	    continue;
+	  void *origfn = putmem (pi, fn);
+	  return origfn;
+	}
+    }
+  return NULL;
+}
+
 void
 ld_preload ()
 {
diff --git a/winsup/cygwin/winsup.h b/winsup/cygwin/winsup.h
index 95ab41e6b..ab7b3bbdc 100644
--- a/winsup/cygwin/winsup.h
+++ b/winsup/cygwin/winsup.h
@@ -199,6 +199,7 @@ ino_t __reg2 hash_path_name (ino_t hash, const char *name);
 void __reg2 nofinalslash (const char *src, char *dst);
 
 void __reg3 *hook_or_detect_cygwin (const char *, const void *, WORD&, HANDLE h = NULL);
+void __reg3 *hook_api (const char *mname, const char *name, const void *fn);
 
 /* Time related */
 void __stdcall totimeval (struct timeval *, PLARGE_INTEGER, int, int);
-- 
2.21.0


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]