Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Faylor <me@cgf.cx>2005-09-07 07:10:17 +0400
committerChristopher Faylor <me@cgf.cx>2005-09-07 07:10:17 +0400
commit41ff9d8c7d4f1224b594eee921b6a966a0585d72 (patch)
tree9a5f0e4545d151129fb93e3125addca8e0e6a8d9 /winsup/cygwin/hookapi.cc
parente92fdf016331c18f76a2ccac28a58edf0e2f2685 (diff)
* dcrt0.cc (initial_env): Don't attempt stracing if dynamically loaded.
(dll_crt0_0): Move console initialization earlier. * init.cc (dll_entry): Move console initialization here. * exceptions.cc (init_console_handler): Fully remove any old console handler. * spawn.cc (spawn_guts): Don't fill out windows argv if we've deduced that this is a cygwin-using program. (av::fixup): Always check executables to see if they are using cygwin1.dll. Don't consider .com files to be scripts. * hookapi.cc (rvadelta): New function. (PEHeaderFromHModule): Simplify slightly. (hook_or_detect_cygwin): Use passed in name argument for "HMODULE" rather than incorrectly reading current program. Calculate delta needed to read image data and file names if this isn't a real "HMODULE".
Diffstat (limited to 'winsup/cygwin/hookapi.cc')
-rw-r--r--winsup/cygwin/hookapi.cc28
1 files changed, 22 insertions, 6 deletions
diff --git a/winsup/cygwin/hookapi.cc b/winsup/cygwin/hookapi.cc
index 97e3330ee..8857b3f0e 100644
--- a/winsup/cygwin/hookapi.cc
+++ b/winsup/cygwin/hookapi.cc
@@ -23,10 +23,10 @@ struct function_hook
static PIMAGE_NT_HEADERS
PEHeaderFromHModule (HMODULE hModule)
{
- PIMAGE_NT_HEADERS pNTHeader = NULL;
+ PIMAGE_NT_HEADERS pNTHeader;
if (PIMAGE_DOS_HEADER(hModule) ->e_magic != IMAGE_DOS_SIGNATURE)
- /* nothing */;
+ pNTHeader = NULL;
else
{
pNTHeader = PIMAGE_NT_HEADERS (PBYTE (hModule)
@@ -38,6 +38,18 @@ PEHeaderFromHModule (HMODULE hModule)
return pNTHeader;
}
+long
+rvadelta (PIMAGE_NT_HEADERS pnt, long import_rva)
+{
+ PIMAGE_SECTION_HEADER section = (PIMAGE_SECTION_HEADER) (pnt + 1);
+ for (int i = 0; i < pnt->FileHeader.NumberOfSections; i++)
+ if (section[i].VirtualAddress <= import_rva
+ && (section[i].VirtualAddress + section[i].Misc.VirtualSize) >= import_rva)
+ // if (strncasematch ((char *) section[i].Name, ".idata", IMAGE_SIZEOF_SHORT_NAME))
+ return section[i].VirtualAddress - section[i].PointerToRawData;
+ return -1;
+}
+
void *
putmem (PIMAGE_THUNK_DATA pi, const void *hookfn)
{
@@ -152,7 +164,7 @@ makename (const char *name, char *&buf, int& i, int inc)
void *
hook_or_detect_cygwin (const char *name, const void *fn)
{
- HMODULE hm = GetModuleHandle (NULL);
+ HMODULE hm = fn ? GetModuleHandle (NULL) : (HMODULE) name;
PIMAGE_NT_HEADERS pExeNTHdr = PEHeaderFromHModule (hm);
if (!pExeNTHdr)
@@ -163,18 +175,22 @@ hook_or_detect_cygwin (const char *name, const void *fn)
if (!importRVA)
return false;
+ long delta = fn ? 0 : rvadelta (pExeNTHdr, importRVA);
+ if (delta < 0)
+ return false;
+
// Convert imports RVA to a usable pointer
- PIMAGE_IMPORT_DESCRIPTOR pdfirst = rva (PIMAGE_IMPORT_DESCRIPTOR, hm, importRVA);
+ PIMAGE_IMPORT_DESCRIPTOR pdfirst = rva (PIMAGE_IMPORT_DESCRIPTOR, hm, importRVA - delta);
function_hook fh;
fh.origfn = NULL;
fh.hookfn = fn;
- char *buf = (char *) alloca (strlen (name) + strlen ("64") + sizeof ("_"));
+ char *buf = fn ? NULL : (char *) alloca (strlen (name) + strlen ("64") + sizeof ("_"));
int i;
// Iterate through each import descriptor, and redirect if appropriate
for (PIMAGE_IMPORT_DESCRIPTOR pd = pdfirst; pd->FirstThunk; pd++)
{
- if (!strcasematch (rva (PSTR, hm, pd->Name), "cygwin1.dll"))
+ if (!strcasematch (rva (PSTR, hm, pd->Name - delta), "cygwin1.dll"))
continue;
if (!fn)
return (void *) "found it"; // just checking if executable used cygwin1.dll