2006-07-07 01:16:37 +08:00
|
|
|
/* hookapi.cc
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2005-03-23 03:00:31 +08:00
|
|
|
#include "winsup.h"
|
2008-03-27 09:50:40 +08:00
|
|
|
#include <stdlib.h>
|
2011-10-26 00:35:58 +08:00
|
|
|
#include <sys/param.h>
|
2008-03-27 09:50:40 +08:00
|
|
|
#include "ntdll.h"
|
2005-03-23 03:00:31 +08:00
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
|
|
|
|
|
|
|
#define rva(coerce, base, addr) (coerce) ((char *) (base) + (addr))
|
|
|
|
#define rvacyg(coerce, addr) rva (coerce, cygwin_hmodule, addr)
|
|
|
|
|
|
|
|
struct function_hook
|
|
|
|
{
|
|
|
|
const char *name; // Function name, e.g. "DirectDrawCreateEx".
|
|
|
|
const void *hookfn; // Address of your function.
|
|
|
|
void *origfn; // Stored by HookAPICalls, the address of the original function.
|
|
|
|
};
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
/* Given an HMODULE, returns a pointer to the PE header. */
|
2005-03-23 03:00:31 +08:00
|
|
|
static PIMAGE_NT_HEADERS
|
2013-04-23 17:44:36 +08:00
|
|
|
PEHeaderFromHModule (HMODULE hModule, bool &is_64bit)
|
2005-03-23 03:00:31 +08:00
|
|
|
{
|
2005-09-07 11:10:17 +08:00
|
|
|
PIMAGE_NT_HEADERS pNTHeader;
|
2005-03-23 03:00:31 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
if (PIMAGE_DOS_HEADER (hModule) ->e_magic != IMAGE_DOS_SIGNATURE)
|
2005-09-07 11:10:17 +08:00
|
|
|
pNTHeader = NULL;
|
2005-03-23 03:00:31 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pNTHeader = PIMAGE_NT_HEADERS (PBYTE (hModule)
|
|
|
|
+ PIMAGE_DOS_HEADER (hModule) ->e_lfanew);
|
|
|
|
if (pNTHeader->Signature != IMAGE_NT_SIGNATURE)
|
|
|
|
pNTHeader = NULL;
|
2013-04-23 17:44:36 +08:00
|
|
|
else if (pNTHeader->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64)
|
|
|
|
is_64bit = true;
|
|
|
|
else if (pNTHeader->FileHeader.Machine == IMAGE_FILE_MACHINE_I386)
|
|
|
|
is_64bit = false;
|
|
|
|
else
|
|
|
|
pNTHeader = NULL;
|
2005-03-23 03:00:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return pNTHeader;
|
|
|
|
}
|
|
|
|
|
2006-07-07 01:16:37 +08:00
|
|
|
static long
|
2012-03-09 20:37:37 +08:00
|
|
|
rvadelta (PIMAGE_NT_HEADERS pnt, DWORD import_rva, DWORD &max_size)
|
2005-09-07 11:10:17 +08:00
|
|
|
{
|
|
|
|
PIMAGE_SECTION_HEADER section = (PIMAGE_SECTION_HEADER) (pnt + 1);
|
|
|
|
for (int i = 0; i < pnt->FileHeader.NumberOfSections; i++)
|
|
|
|
if (section[i].VirtualAddress <= import_rva
|
2006-07-07 01:16:37 +08:00
|
|
|
&& (section[i].VirtualAddress + section[i].Misc.VirtualSize) > import_rva)
|
2012-03-09 20:37:37 +08:00
|
|
|
{
|
2012-04-04 19:09:44 +08:00
|
|
|
max_size = section[i].SizeOfRawData
|
2012-03-09 20:37:37 +08:00
|
|
|
- (import_rva - section[i].VirtualAddress);
|
|
|
|
return section[i].VirtualAddress - section[i].PointerToRawData;
|
|
|
|
}
|
2005-09-07 11:10:17 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
/* This function is only used for the current architecture.
|
|
|
|
Just the size of the IMAGE_THUNK_DATA Function member differs. */
|
2006-07-07 01:16:37 +08:00
|
|
|
static void *
|
2005-03-23 03:00:31 +08:00
|
|
|
putmem (PIMAGE_THUNK_DATA pi, const void *hookfn)
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
#ifdef __x86_64__
|
|
|
|
#define THUNK_FUNC_TYPE ULONGLONG
|
|
|
|
#else
|
|
|
|
#define THUNK_FUNC_TYPE DWORD
|
|
|
|
#endif
|
|
|
|
|
2005-12-17 02:10:10 +08:00
|
|
|
DWORD ofl;
|
2013-04-23 17:44:36 +08:00
|
|
|
if (!VirtualProtect (pi, sizeof (THUNK_FUNC_TYPE), PAGE_READWRITE, &ofl) )
|
2005-03-23 03:00:31 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
void *origfn = (void *) pi->u1.Function;
|
2013-04-23 17:44:36 +08:00
|
|
|
pi->u1.Function = (THUNK_FUNC_TYPE) hookfn;
|
2005-03-23 03:00:31 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
VirtualProtect (pi, sizeof (THUNK_FUNC_TYPE), ofl, &ofl);
|
2005-03-23 03:00:31 +08:00
|
|
|
return origfn;
|
|
|
|
}
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
/* Builds stubs for and redirects the IAT for one DLL (pImportDesc)
|
|
|
|
This function is only used for the current architecture. */
|
2005-03-23 03:00:31 +08:00
|
|
|
|
|
|
|
static bool
|
|
|
|
RedirectIAT (function_hook& fh, PIMAGE_IMPORT_DESCRIPTOR pImportDesc,
|
|
|
|
HMODULE hm)
|
|
|
|
{
|
|
|
|
// If no import names table, we can't redirect this, so bail
|
|
|
|
if (pImportDesc->OriginalFirstThunk == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* import address table */
|
|
|
|
PIMAGE_THUNK_DATA pt = rva (PIMAGE_THUNK_DATA, hm, pImportDesc->FirstThunk);
|
|
|
|
/* import names table */
|
|
|
|
PIMAGE_THUNK_DATA pn = rva (PIMAGE_THUNK_DATA, hm, pImportDesc->OriginalFirstThunk);
|
|
|
|
|
|
|
|
/* Scan through the IAT, completing the stubs and redirecting the IAT
|
|
|
|
entries to point to the stubs. */
|
|
|
|
for (PIMAGE_THUNK_DATA pi = pt; pn->u1.Ordinal; pi++, pn++)
|
|
|
|
{
|
|
|
|
if (IMAGE_SNAP_BY_ORDINAL (pn->u1.Ordinal) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* import by name */
|
|
|
|
PIMAGE_IMPORT_BY_NAME pimp = rva (PIMAGE_IMPORT_BY_NAME, hm, pn->u1.AddressOfData);
|
|
|
|
|
|
|
|
if (strcmp (fh.name, (char *) pimp->Name) == 0)
|
|
|
|
{
|
|
|
|
fh.origfn = putmem (pi, fh.hookfn);
|
|
|
|
if (!fh.origfn)
|
|
|
|
return false;
|
|
|
|
hook_chain *hc;
|
|
|
|
for (hc = &cygheap->hooks; hc->next; hc = hc->next)
|
|
|
|
continue;
|
2007-11-27 05:30:49 +08:00
|
|
|
hc->next = (hook_chain *) cmalloc_abort (HEAP_1_HOOK, sizeof (hook_chain));
|
2005-03-23 03:00:31 +08:00
|
|
|
hc->next->loc = (void **) pi;
|
|
|
|
hc->next->func = fh.hookfn;
|
|
|
|
hc->next->next = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
/* This function is only used for the current architecture. */
|
2006-07-07 01:16:37 +08:00
|
|
|
static void
|
2005-03-23 03:00:31 +08:00
|
|
|
get_export (function_hook& fh)
|
|
|
|
{
|
|
|
|
PIMAGE_DOS_HEADER pdh = (PIMAGE_DOS_HEADER) cygwin_hmodule;
|
|
|
|
if (pdh->e_magic != IMAGE_DOS_SIGNATURE)
|
|
|
|
return;
|
|
|
|
PIMAGE_NT_HEADERS pnt = (PIMAGE_NT_HEADERS) ((char *) pdh + pdh->e_lfanew);
|
|
|
|
if (pnt->Signature != IMAGE_NT_SIGNATURE || pnt->FileHeader.SizeOfOptionalHeader == 0)
|
|
|
|
return;
|
|
|
|
PIMAGE_EXPORT_DIRECTORY pexp =
|
|
|
|
rvacyg (PIMAGE_EXPORT_DIRECTORY,
|
|
|
|
pnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
|
|
|
|
if (!pexp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PDWORD pfuncs = rvacyg (PDWORD, pexp->AddressOfFunctions);
|
|
|
|
PDWORD pnames = rvacyg (PDWORD, pexp->AddressOfNames);
|
|
|
|
for (DWORD i = 0; i < pexp->NumberOfNames; i++)
|
|
|
|
if (strcmp (fh.name, rvacyg (char *, pnames[i])) == 0)
|
|
|
|
{
|
|
|
|
fh.origfn = rvacyg (void *, pfuncs[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
makename (const char *name, char *&buf, int& i, int inc)
|
|
|
|
{
|
|
|
|
i += inc;
|
|
|
|
static const char *testers[] = {"NOTUSED", "64", "32"};
|
|
|
|
if (i < 0 || i >= (int) (sizeof (testers) / sizeof (testers[0])))
|
|
|
|
return NULL;
|
|
|
|
if (i)
|
|
|
|
{
|
|
|
|
__small_sprintf (buf, "_%s%s", name, testers[i]);
|
|
|
|
name = buf;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
static HMODULE
|
|
|
|
remap (PIMAGE_IMPORT_DESCRIPTOR &pdfirst, long &delta, HANDLE hc,
|
|
|
|
DWORD importRVA, DWORD importRVASize, DWORD importRVAMaxSize)
|
|
|
|
{
|
|
|
|
/* If h is not NULL, the calling function only mapped at most the first
|
|
|
|
64K of the image. The IAT is usually at the end of the image, so
|
|
|
|
what we do here is to map the IAT into our address space if it doesn't
|
|
|
|
reside in the first 64K anyway. The offset must be a multiple of the
|
|
|
|
allocation granularity, though, so we have to map a bit more. */
|
|
|
|
HMODULE map;
|
|
|
|
DWORD offset = rounddown (importRVA, wincap.allocation_granularity ());
|
|
|
|
/* But that's not all, unfortunately. Apparently there's a difference
|
|
|
|
between the importRVASize of applications built with gcc and those
|
|
|
|
built with Visual Studio. When built with gcc, importRVASize contains
|
|
|
|
the size of the import RVA table plus the size of the referenced
|
|
|
|
string table with the DLL names. When built with VS, it only contains
|
|
|
|
the size of the naked import RVA table. The following code handles
|
|
|
|
the situation. importRVAMaxSize contains the size of the remainder
|
|
|
|
of the section. If the difference between importRVAMaxSize and
|
|
|
|
importRVASize is less than 64K, we just use importRVAMaxSize to
|
|
|
|
compute the size of the memory map. Otherwise the executable may be
|
|
|
|
very big. In that case we only map the import RVA table and ... */
|
|
|
|
DWORD size = importRVA - offset + ((importRVAMaxSize - importRVASize
|
|
|
|
<= wincap.allocation_granularity ())
|
|
|
|
? importRVAMaxSize : importRVASize);
|
|
|
|
map = (HMODULE) MapViewOfFile (hc, FILE_MAP_READ, 0, offset, size);
|
|
|
|
if (!map)
|
|
|
|
return NULL;
|
|
|
|
pdfirst = rva (PIMAGE_IMPORT_DESCRIPTOR, map, importRVA - offset);
|
|
|
|
/* ... carefully check the required size to fit the string table into
|
|
|
|
the map as well. Allow NAME_MAX bytes for the DLL name, but don't
|
|
|
|
go beyond the remainder of the section. */
|
|
|
|
if (importRVAMaxSize - importRVASize > wincap.allocation_granularity ())
|
|
|
|
{
|
|
|
|
DWORD newsize = size;
|
|
|
|
for (PIMAGE_IMPORT_DESCRIPTOR pd = pdfirst; pd->FirstThunk; pd++)
|
|
|
|
if (pd->Name - delta - offset + (NAME_MAX + 1) > newsize)
|
|
|
|
newsize = pd->Name - delta - offset + (NAME_MAX + 1);
|
|
|
|
if (newsize > size)
|
|
|
|
{
|
|
|
|
if (newsize > importRVA - offset + importRVAMaxSize)
|
|
|
|
newsize = importRVA - offset + importRVAMaxSize;
|
|
|
|
UnmapViewOfFile (map);
|
|
|
|
map = (HMODULE) MapViewOfFile (hc, FILE_MAP_READ, 0, offset, newsize);
|
|
|
|
if (!map)
|
|
|
|
return NULL;
|
|
|
|
pdfirst = rva (PIMAGE_IMPORT_DESCRIPTOR, map, importRVA - offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delta += offset;
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2008-03-27 09:50:40 +08:00
|
|
|
/* Find first missing dll in a given executable.
|
|
|
|
FIXME: This is not foolproof since it doesn't look for dlls in the
|
|
|
|
same directory as the given executable, like Windows. Instead it
|
2012-05-30 22:37:53 +08:00
|
|
|
searches for dlls in the context of the current executable.
|
|
|
|
It also only finds direct dependencies, not indirect ones. */
|
2008-03-27 09:50:40 +08:00
|
|
|
const char *
|
|
|
|
find_first_notloaded_dll (path_conv& pc)
|
|
|
|
{
|
|
|
|
const char *res = "?";
|
|
|
|
HANDLE hc = NULL;
|
|
|
|
HMODULE hm = NULL;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
HANDLE h;
|
|
|
|
NTSTATUS status;
|
2012-03-09 20:37:37 +08:00
|
|
|
LARGE_INTEGER size;
|
2013-04-23 17:44:36 +08:00
|
|
|
PIMAGE_NT_HEADERS pExeNTHdr;
|
|
|
|
DWORD importRVA, importRVASize, importRVAMaxSize;
|
|
|
|
HMODULE map;
|
|
|
|
long delta;
|
2008-03-27 09:50:40 +08:00
|
|
|
|
|
|
|
status = NtOpenFile (&h, SYNCHRONIZE | GENERIC_READ,
|
|
|
|
pc.get_object_attr (attr, sec_none_nih),
|
2011-07-04 23:25:36 +08:00
|
|
|
&io, FILE_SHARE_VALID_FLAGS,
|
2008-03-27 09:50:40 +08:00
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT
|
|
|
|
| FILE_OPEN_FOR_BACKUP_INTENT
|
|
|
|
| FILE_NON_DIRECTORY_FILE);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
goto out;
|
2012-03-09 20:37:37 +08:00
|
|
|
/* Just as in hook_or_detect_cygwin below, we have to take big executables
|
|
|
|
into account. That means, we must not try to map the entire file, since
|
|
|
|
there's no guarantee that the current process has enough VM in one block
|
|
|
|
left for this mapping. The offset computation below ignores very big
|
|
|
|
executables for now. In theory, since the import RVA table appears to
|
|
|
|
be more or less at the end of the data section, independent of the used
|
|
|
|
compiler, that shouldn't matter. */
|
|
|
|
if (!GetFileSizeEx (h, &size))
|
|
|
|
{
|
|
|
|
NtClose (h);
|
|
|
|
goto out;
|
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
if (size.QuadPart > (LONGLONG) wincap.allocation_granularity ())
|
2012-03-09 20:37:37 +08:00
|
|
|
size.LowPart = wincap.allocation_granularity ();
|
2008-03-27 09:50:40 +08:00
|
|
|
hc = CreateFileMapping (h, &sec_none_nih, PAGE_READONLY, 0, 0, NULL);
|
|
|
|
NtClose (h);
|
|
|
|
if (!hc)
|
|
|
|
goto out;
|
2012-03-09 20:37:37 +08:00
|
|
|
hm = (HMODULE) MapViewOfFile(hc, FILE_MAP_READ, 0, 0, size.LowPart);
|
|
|
|
if (!hm)
|
|
|
|
goto out;
|
2008-03-27 09:50:40 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
bool is_64bit;
|
|
|
|
pExeNTHdr = PEHeaderFromHModule (hm, is_64bit);
|
|
|
|
|
|
|
|
if (!pExeNTHdr)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
#ifdef __x86_64__
|
|
|
|
if (!is_64bit)
|
|
|
|
#else
|
|
|
|
if (is_64bit)
|
|
|
|
#endif
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
importRVA = pExeNTHdr->OptionalHeader.DataDirectory
|
|
|
|
[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
|
|
|
|
importRVASize = pExeNTHdr->OptionalHeader.DataDirectory
|
|
|
|
[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
|
|
|
|
if (!importRVA)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
delta = rvadelta (pExeNTHdr, importRVA, importRVAMaxSize);
|
|
|
|
if (delta < 0)
|
|
|
|
goto out;
|
|
|
|
importRVA -= delta;
|
|
|
|
map = NULL;
|
|
|
|
|
|
|
|
PIMAGE_IMPORT_DESCRIPTOR pdfirst;
|
|
|
|
|
|
|
|
if (importRVA + importRVAMaxSize > wincap.allocation_granularity ())
|
|
|
|
{
|
|
|
|
map = remap (pdfirst, delta, hc, importRVA, importRVASize,
|
|
|
|
importRVAMaxSize);
|
|
|
|
if (!map)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pdfirst = rva (PIMAGE_IMPORT_DESCRIPTOR, hm, importRVA);
|
2008-03-27 09:50:40 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
/* Iterate through each import descriptor, and check if DLL can be loaded. */
|
|
|
|
for (PIMAGE_IMPORT_DESCRIPTOR pd = pdfirst; pd->FirstThunk; pd++)
|
2008-03-27 09:50:40 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
const char *lib = rva (PSTR, map ?: hm, pd->Name - delta);
|
|
|
|
if (!LoadLibraryEx (lib, NULL, DONT_RESOLVE_DLL_REFERENCES
|
|
|
|
| LOAD_LIBRARY_AS_DATAFILE))
|
2008-03-27 09:50:40 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
static char buf[MAX_PATH];
|
|
|
|
strlcpy (buf, lib, MAX_PATH);
|
|
|
|
res = buf;
|
2008-03-27 09:50:40 +08:00
|
|
|
}
|
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
if (map)
|
|
|
|
UnmapViewOfFile (map);
|
2008-03-27 09:50:40 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
if (hm)
|
|
|
|
UnmapViewOfFile (hm);
|
2012-03-09 20:37:37 +08:00
|
|
|
if (hc)
|
|
|
|
CloseHandle (hc);
|
2008-03-27 09:50:40 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Top level routine to find the EXE's imports and redirect them
|
2005-03-23 03:00:31 +08:00
|
|
|
void *
|
2011-10-26 00:35:58 +08:00
|
|
|
hook_or_detect_cygwin (const char *name, const void *fn, WORD& subsys, HANDLE h)
|
2005-03-23 03:00:31 +08:00
|
|
|
{
|
2005-09-07 11:10:17 +08:00
|
|
|
HMODULE hm = fn ? GetModuleHandle (NULL) : (HMODULE) name;
|
2013-04-23 17:44:36 +08:00
|
|
|
bool is_64bit;
|
|
|
|
PIMAGE_NT_HEADERS pExeNTHdr = PEHeaderFromHModule (hm, is_64bit);
|
2005-03-23 03:00:31 +08:00
|
|
|
|
|
|
|
if (!pExeNTHdr)
|
2012-03-09 20:37:37 +08:00
|
|
|
return NULL;
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
/* Shortcut. We don't have to do anything further from here, if the
|
|
|
|
executable's architecture doesn't match, unless we want to support
|
|
|
|
a mix of 32 and 64 bit Cygwin at one point. */
|
|
|
|
#ifdef __x86_64__
|
|
|
|
if (!is_64bit)
|
|
|
|
#else
|
|
|
|
if (is_64bit)
|
|
|
|
#endif
|
2012-03-09 20:37:37 +08:00
|
|
|
return NULL;
|
2005-03-23 03:00:31 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
DWORD importRVA, importRVASize;
|
|
|
|
subsys = pExeNTHdr->OptionalHeader.Subsystem;
|
|
|
|
importRVA = pExeNTHdr->OptionalHeader.DataDirectory
|
|
|
|
[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
|
|
|
|
importRVASize = pExeNTHdr->OptionalHeader.DataDirectory
|
2011-10-26 00:35:58 +08:00
|
|
|
[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
|
2005-03-23 03:00:31 +08:00
|
|
|
if (!importRVA)
|
2012-03-09 20:37:37 +08:00
|
|
|
return NULL;
|
2005-03-23 03:00:31 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
DWORD importRVAMaxSize = 0;
|
2012-03-09 20:37:37 +08:00
|
|
|
long delta = fn ? 0 : rvadelta (pExeNTHdr, importRVA, importRVAMaxSize);
|
2005-09-07 11:10:17 +08:00
|
|
|
if (delta < 0)
|
2012-03-09 20:37:37 +08:00
|
|
|
return NULL;
|
2011-10-26 00:35:58 +08:00
|
|
|
importRVA -= delta;
|
2005-09-07 11:10:17 +08:00
|
|
|
|
2005-03-23 03:00:31 +08:00
|
|
|
// Convert imports RVA to a usable pointer
|
2011-10-26 00:35:58 +08:00
|
|
|
PIMAGE_IMPORT_DESCRIPTOR pdfirst;
|
|
|
|
char *map = NULL;
|
2012-03-09 20:37:37 +08:00
|
|
|
if (h && importRVA + importRVAMaxSize > wincap.allocation_granularity ())
|
2011-10-26 00:35:58 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
map = (char *) remap (pdfirst, delta, h, importRVA, importRVASize,
|
|
|
|
importRVAMaxSize);
|
2011-10-26 00:35:58 +08:00
|
|
|
if (!map)
|
2012-03-09 20:37:37 +08:00
|
|
|
return NULL;
|
2011-10-26 00:35:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
pdfirst = rva (PIMAGE_IMPORT_DESCRIPTOR, hm, importRVA);
|
2005-03-23 03:00:31 +08:00
|
|
|
|
|
|
|
function_hook fh;
|
|
|
|
fh.origfn = NULL;
|
|
|
|
fh.hookfn = fn;
|
2012-03-09 20:37:37 +08:00
|
|
|
char *buf = NULL;
|
|
|
|
if (fn)
|
|
|
|
buf = (char *) alloca (strlen (name) + sizeof ("_64"));
|
2011-02-09 23:46:00 +08:00
|
|
|
int i = 0;
|
2005-07-17 08:51:03 +08:00
|
|
|
// Iterate through each import descriptor, and redirect if appropriate
|
|
|
|
for (PIMAGE_IMPORT_DESCRIPTOR pd = pdfirst; pd->FirstThunk; pd++)
|
2005-03-23 03:00:31 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
if (!ascii_strcasematch (rva (PSTR, map ?: (char *) hm, pd->Name - delta),
|
|
|
|
"cygwin1.dll"))
|
|
|
|
continue;
|
2005-07-17 08:51:03 +08:00
|
|
|
if (!fn)
|
2011-10-26 00:35:58 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
/* Just checking if executable used cygwin1.dll. */
|
2011-10-26 00:35:58 +08:00
|
|
|
if (map)
|
|
|
|
UnmapViewOfFile (map);
|
2013-04-23 17:44:36 +08:00
|
|
|
return (void *) "found it";
|
2011-10-26 00:35:58 +08:00
|
|
|
}
|
2005-07-17 08:51:03 +08:00
|
|
|
i = -1;
|
|
|
|
while (!fh.origfn && (fh.name = makename (name, buf, i, 1)))
|
|
|
|
RedirectIAT (fh, pd, hm);
|
|
|
|
if (fh.origfn)
|
|
|
|
break;
|
2005-03-23 03:00:31 +08:00
|
|
|
}
|
|
|
|
|
2011-10-26 00:35:58 +08:00
|
|
|
if (map)
|
|
|
|
UnmapViewOfFile (map);
|
2012-03-09 20:37:37 +08:00
|
|
|
if (!fn)
|
|
|
|
return NULL;
|
2011-10-26 00:35:58 +08:00
|
|
|
|
2005-03-23 03:00:31 +08:00
|
|
|
while (!fh.origfn && (fh.name = makename (name, buf, i, -1)))
|
|
|
|
get_export (fh);
|
|
|
|
|
|
|
|
return fh.origfn;
|
|
|
|
}
|
|
|
|
|
2019-09-04 09:44:25 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2005-03-23 03:00:31 +08:00
|
|
|
void
|
|
|
|
ld_preload ()
|
|
|
|
{
|
|
|
|
char *p = getenv ("LD_PRELOAD");
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
char *s = (char *) alloca (strlen (p) + 1);
|
|
|
|
strcpy (s, p);
|
|
|
|
char *here = NULL;
|
2005-04-15 01:34:03 +08:00
|
|
|
for (p = strtok_r (s, ":\t\n", &here); p; p = strtok_r (NULL, ":\t\n", &here))
|
2005-03-23 03:00:31 +08:00
|
|
|
{
|
|
|
|
path_conv lib (p);
|
2007-08-20 23:41:08 +08:00
|
|
|
WCHAR libname[lib.get_wide_win32_path_len () + 1];
|
|
|
|
if (!LoadLibraryW (lib.get_wide_win32_path (libname)))
|
2005-03-23 03:00:31 +08:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
api_fatal ("error while loading shared libraries: %s: "
|
|
|
|
"cannot open shared object file: %s", p,
|
|
|
|
strerror (get_errno ()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fixup_hooks_after_fork ()
|
|
|
|
{
|
|
|
|
for (hook_chain *hc = &cygheap->hooks; (hc = hc->next); )
|
|
|
|
putmem ((PIMAGE_THUNK_DATA) hc->loc, hc->func);
|
|
|
|
}
|