2013-04-23 17:44:36 +08:00
|
|
|
/* Copyright (c) 2009, 2010, 2011, 2013 Chris Faylor
|
2009-01-09 13:11:57 +08:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Neither the name of the owner nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
|
|
|
|
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-03-18 12:19:05 +08:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <locale.h>
|
2009-01-09 13:11:57 +08:00
|
|
|
#include <sys/cygwin.h>
|
2011-10-10 22:57:48 +08:00
|
|
|
#include <cygwin/version.h>
|
2009-01-09 13:11:57 +08:00
|
|
|
#include <unistd.h>
|
2009-03-14 14:46:00 +08:00
|
|
|
#include <libgen.h>
|
2009-01-09 13:11:57 +08:00
|
|
|
|
|
|
|
#define _WIN32_WINNT 0x0501
|
|
|
|
#include <windows.h>
|
|
|
|
#include <imagehlp.h>
|
|
|
|
#include <psapi.h>
|
|
|
|
|
|
|
|
struct option longopts[] =
|
|
|
|
{
|
2011-10-10 22:57:48 +08:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"verbose", no_argument, NULL, 'v'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
2009-01-09 13:11:57 +08:00
|
|
|
{"data-relocs", no_argument, NULL, 'd'},
|
|
|
|
{"function-relocs", no_argument, NULL, 'r'},
|
|
|
|
{"unused", no_argument, NULL, 'u'},
|
|
|
|
{0, no_argument, NULL, 0}
|
|
|
|
};
|
2011-10-10 22:57:48 +08:00
|
|
|
const char *opts = "dhruvV";
|
2009-01-09 13:11:57 +08:00
|
|
|
|
2009-03-18 12:19:05 +08:00
|
|
|
static int process_file (const wchar_t *);
|
2009-03-14 14:46:00 +08:00
|
|
|
|
2009-01-09 13:11:57 +08:00
|
|
|
static int
|
2011-10-10 22:57:48 +08:00
|
|
|
error (const char *fmt, ...)
|
2009-01-09 13:11:57 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
fprintf (stderr, "ldd: ");
|
|
|
|
vfprintf (stderr, fmt, ap);
|
|
|
|
fprintf (stderr, "\nTry `ldd --help' for more information.\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2011-10-10 22:57:48 +08:00
|
|
|
static void
|
|
|
|
usage ()
|
|
|
|
{
|
|
|
|
printf ("Usage: %s [OPTION]... FILE...\n\
|
|
|
|
\n\
|
|
|
|
Print shared library dependencies\n\
|
|
|
|
\n\
|
|
|
|
-h, --help print this help and exit\n\
|
|
|
|
-V, --version print version information and exit\n\
|
|
|
|
-r, --function-relocs process data and function relocations\n\
|
|
|
|
(currently unimplemented)\n\
|
|
|
|
-u, --unused print unused direct dependencies\n\
|
|
|
|
(currently unimplemented)\n\
|
|
|
|
-v, --verbose print all information\n\
|
|
|
|
(currently unimplemented)\n",
|
|
|
|
program_invocation_short_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_version ()
|
|
|
|
{
|
|
|
|
printf ("ldd (cygwin) %d.%d.%d\n"
|
2011-12-18 07:39:47 +08:00
|
|
|
"Print shared library dependencies\n"
|
|
|
|
"Copyright (C) 2009 - %s Chris Faylor\n"
|
|
|
|
"This is free software; see the source for copying conditions. There is NO\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
|
2011-12-18 07:39:47 +08:00
|
|
|
CYGWIN_VERSION_DLL_MAJOR / 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MAJOR % 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MINOR,
|
|
|
|
strrchr (__DATE__, ' ') + 1);
|
2011-10-10 22:57:48 +08:00
|
|
|
}
|
|
|
|
|
2009-01-09 13:11:57 +08:00
|
|
|
#define print_errno_error_and_return(__fn) \
|
|
|
|
do {\
|
|
|
|
fprintf (stderr, "ldd: %s: %s\n", (__fn), strerror (errno));\
|
|
|
|
return 1;\
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define set_errno_and_return(x) \
|
|
|
|
do {\
|
|
|
|
cygwin_internal (CW_SETERRNO, __FILE__, __LINE__ - 2);\
|
|
|
|
return (x);\
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
static HANDLE hProcess;
|
|
|
|
|
2010-07-06 00:24:24 +08:00
|
|
|
static struct filelist
|
|
|
|
{
|
|
|
|
struct filelist *next;
|
|
|
|
char *name;
|
|
|
|
} *head;
|
|
|
|
|
|
|
|
static bool
|
|
|
|
saw_file (char *name)
|
|
|
|
{
|
|
|
|
filelist *p;
|
|
|
|
|
|
|
|
for (p = head; p; p = p->next)
|
|
|
|
if (strcasecmp (name, p->name) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
p = (filelist *) malloc(sizeof (struct filelist));
|
|
|
|
p->next = head;
|
|
|
|
p->name = strdup (name);
|
|
|
|
head = p;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-18 12:19:05 +08:00
|
|
|
static wchar_t *
|
2009-03-15 02:40:59 +08:00
|
|
|
get_module_filename (HANDLE hp, HMODULE hm)
|
|
|
|
{
|
|
|
|
size_t len;
|
2009-03-18 12:19:05 +08:00
|
|
|
wchar_t *buf = NULL;
|
2009-03-15 02:40:59 +08:00
|
|
|
DWORD res;
|
2009-03-18 12:19:05 +08:00
|
|
|
for (len = 1024; (res = GetModuleFileNameExW (hp, hm, (buf = (wchar_t *) realloc (buf, len * sizeof (wchar_t))), len)) == len; len += 1024)
|
2009-03-15 02:40:59 +08:00
|
|
|
continue;
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
free (buf);
|
|
|
|
buf = NULL;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2009-03-18 12:19:05 +08:00
|
|
|
static wchar_t *
|
|
|
|
load_dll (const wchar_t *fn)
|
2009-03-15 02:40:59 +08:00
|
|
|
{
|
2009-03-18 12:19:05 +08:00
|
|
|
wchar_t *buf = get_module_filename (GetCurrentProcess (), NULL);
|
2009-03-15 02:40:59 +08:00
|
|
|
if (!buf)
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
printf ("ldd: GetModuleFileName returned an error %u\n",
|
|
|
|
(unsigned int) GetLastError ());
|
2009-03-15 02:40:59 +08:00
|
|
|
exit (1); /* FIXME */
|
|
|
|
}
|
|
|
|
|
2009-03-18 12:19:05 +08:00
|
|
|
wchar_t *newbuf = (wchar_t *) malloc ((sizeof (L"\"\" -- ") + wcslen (buf) + wcslen (fn)) * sizeof (wchar_t));
|
|
|
|
newbuf[0] = L'"';
|
|
|
|
wcscpy (newbuf + 1, buf);
|
|
|
|
wchar_t *p = wcsstr (newbuf, L"\\ldd");
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
printf ("ldd: can't parse my own filename \"%ls\"\n", buf);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
p[3] = L'h';
|
|
|
|
wcscat (newbuf, L"\" -- ");
|
|
|
|
wcscat (newbuf, fn);
|
|
|
|
free (buf);
|
|
|
|
return newbuf;
|
|
|
|
}
|
2009-03-15 02:40:59 +08:00
|
|
|
|
2009-01-09 13:11:57 +08:00
|
|
|
static int
|
2009-03-18 12:19:05 +08:00
|
|
|
start_process (const wchar_t *fn, bool& isdll)
|
2009-01-09 13:11:57 +08:00
|
|
|
{
|
2009-03-18 12:19:05 +08:00
|
|
|
STARTUPINFOW si = {};
|
2009-01-09 13:11:57 +08:00
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
si.cb = sizeof (si);
|
2009-03-18 12:19:05 +08:00
|
|
|
wchar_t *cmd;
|
2012-01-24 14:29:01 +08:00
|
|
|
/* OCaml natdynlink plugins (.cmxs) cannot be handled by ldd because they
|
|
|
|
can only be loaded by flexdll_dlopen() */
|
|
|
|
if (wcslen (fn) < 4 || (wcscasecmp (wcschr (fn, L'\0') - 4, L".dll") != 0
|
|
|
|
&& wcscasecmp (wcschr (fn, L'\0') - 4, L".oct") != 0
|
|
|
|
&& wcscasecmp (wcschr (fn, L'\0') - 3, L".so") != 0))
|
2009-01-09 13:11:57 +08:00
|
|
|
{
|
2009-03-18 12:19:05 +08:00
|
|
|
cmd = wcsdup (fn);
|
2009-03-15 02:40:59 +08:00
|
|
|
isdll = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmd = load_dll (fn);
|
|
|
|
isdll = true;
|
|
|
|
}
|
2009-03-18 12:19:05 +08:00
|
|
|
if (CreateProcessW (NULL, cmd, NULL, NULL, FALSE, DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi))
|
2009-03-15 02:40:59 +08:00
|
|
|
{
|
|
|
|
free (cmd);
|
2009-01-09 13:11:57 +08:00
|
|
|
hProcess = pi.hProcess;
|
|
|
|
DebugSetProcessKillOnExit (true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-15 02:40:59 +08:00
|
|
|
free (cmd);
|
2009-01-09 13:11:57 +08:00
|
|
|
set_errno_and_return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-03-18 12:19:05 +08:00
|
|
|
set_entry_point_break ()
|
2009-01-09 13:11:57 +08:00
|
|
|
{
|
|
|
|
HMODULE hm;
|
2013-04-23 17:44:36 +08:00
|
|
|
DWORD cbe;
|
|
|
|
SIZE_T cbw;
|
|
|
|
if (!EnumProcessModules (hProcess, &hm, sizeof (hm), &cbe) || !cbe)
|
2009-01-09 13:11:57 +08:00
|
|
|
set_errno_and_return (1);
|
|
|
|
|
|
|
|
MODULEINFO mi = {};
|
|
|
|
if (!GetModuleInformation (hProcess, hm, &mi, sizeof (mi)) || !mi.EntryPoint)
|
|
|
|
set_errno_and_return (1);
|
|
|
|
|
|
|
|
static const unsigned char int3 = 0xcc;
|
2013-04-23 17:44:36 +08:00
|
|
|
if (!WriteProcessMemory (hProcess, mi.EntryPoint, &int3, 1, &cbw) || cbw != 1)
|
2009-01-09 13:11:57 +08:00
|
|
|
set_errno_and_return (1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dlls
|
|
|
|
{
|
|
|
|
LPVOID lpBaseOfDll;
|
|
|
|
struct dlls *next;
|
|
|
|
};
|
|
|
|
|
2009-03-14 14:46:00 +08:00
|
|
|
#define SLOP strlen (" (?)")
|
|
|
|
char *
|
2009-03-18 12:19:05 +08:00
|
|
|
tocyg (wchar_t *win_fn)
|
2009-03-14 14:46:00 +08:00
|
|
|
{
|
2009-03-18 12:19:05 +08:00
|
|
|
ssize_t cwlen = cygwin_conv_path (CCP_WIN_W_TO_POSIX, win_fn, NULL, 0);
|
2009-03-14 14:46:00 +08:00
|
|
|
char *fn;
|
|
|
|
if (cwlen <= 0)
|
2009-03-18 12:19:05 +08:00
|
|
|
{
|
|
|
|
int len = wcstombs (NULL, win_fn, 0) + 1;
|
|
|
|
if ((fn = (char *) malloc (len)))
|
|
|
|
wcstombs (fn, win_fn, len);
|
|
|
|
}
|
2009-03-14 14:46:00 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char *fn_cyg = (char *) malloc (cwlen + SLOP + 1);
|
2009-03-18 12:19:05 +08:00
|
|
|
if (cygwin_conv_path (CCP_WIN_W_TO_POSIX, win_fn, fn_cyg, cwlen) == 0)
|
2009-03-14 14:46:00 +08:00
|
|
|
fn = fn_cyg;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free (fn_cyg);
|
2009-03-18 12:19:05 +08:00
|
|
|
int len = wcstombs (NULL, win_fn, 0);
|
|
|
|
fn = (char *) malloc (len + SLOP + 1);
|
|
|
|
wcstombs (fn, win_fn, len + SLOP + 1);
|
2009-03-14 14:46:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
2009-03-18 12:19:05 +08:00
|
|
|
#define CYGWIN_DLL_LEN (wcslen (L"\\cygwin1.dll"))
|
2009-01-09 13:11:57 +08:00
|
|
|
static int
|
2009-03-18 12:19:05 +08:00
|
|
|
print_dlls (dlls *dll, const wchar_t *dllfn, const wchar_t *process_fn)
|
2009-01-09 13:11:57 +08:00
|
|
|
{
|
2010-07-06 00:24:24 +08:00
|
|
|
head = NULL; /* FIXME: memory leak */
|
2009-01-09 13:11:57 +08:00
|
|
|
while ((dll = dll->next))
|
|
|
|
{
|
|
|
|
char *fn;
|
2009-03-18 12:19:05 +08:00
|
|
|
wchar_t *fullpath = get_module_filename (hProcess, (HMODULE) dll->lpBaseOfDll);
|
2009-03-15 02:40:59 +08:00
|
|
|
if (!fullpath)
|
2009-01-09 13:11:57 +08:00
|
|
|
fn = strdup ("???");
|
2009-03-18 12:19:05 +08:00
|
|
|
else if (dllfn && wcscmp (fullpath, dllfn) == 0)
|
|
|
|
{
|
|
|
|
free (fullpath);
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-09 13:11:57 +08:00
|
|
|
else
|
2009-03-15 02:40:59 +08:00
|
|
|
{
|
2009-03-18 12:19:05 +08:00
|
|
|
fn = tocyg (fullpath);
|
2010-07-06 00:24:24 +08:00
|
|
|
saw_file (basename (fn));
|
2009-03-15 02:40:59 +08:00
|
|
|
free (fullpath);
|
|
|
|
}
|
2009-03-14 14:46:00 +08:00
|
|
|
printf ("\t%s => %s (%p)\n", basename (fn), fn, dll->lpBaseOfDll);
|
2009-01-09 13:11:57 +08:00
|
|
|
free (fn);
|
|
|
|
}
|
2009-03-14 14:46:00 +08:00
|
|
|
if (process_fn)
|
|
|
|
return process_file (process_fn);
|
2009-01-09 13:11:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
report (const char *in_fn, bool multiple)
|
|
|
|
{
|
|
|
|
if (multiple)
|
|
|
|
printf ("%s:\n", in_fn);
|
|
|
|
char *fn = realpath (in_fn, NULL);
|
2009-03-14 14:46:00 +08:00
|
|
|
if (!fn)
|
|
|
|
print_errno_error_and_return (in_fn);
|
|
|
|
|
2009-03-18 12:19:05 +08:00
|
|
|
ssize_t len = cygwin_conv_path (CCP_POSIX_TO_WIN_W, fn, NULL, 0);
|
2009-03-14 14:46:00 +08:00
|
|
|
if (len <= 0)
|
|
|
|
print_errno_error_and_return (fn);
|
|
|
|
|
2009-03-15 02:40:59 +08:00
|
|
|
bool isdll;
|
2010-08-23 19:04:04 +08:00
|
|
|
wchar_t fn_win[len + 1];
|
|
|
|
if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, fn, fn_win, len))
|
2009-03-14 14:46:00 +08:00
|
|
|
print_errno_error_and_return (fn);
|
|
|
|
|
2009-03-15 02:40:59 +08:00
|
|
|
if (!fn || start_process (fn_win, isdll))
|
2009-01-09 13:11:57 +08:00
|
|
|
print_errno_error_and_return (in_fn);
|
|
|
|
|
|
|
|
DEBUG_EVENT ev;
|
|
|
|
|
|
|
|
unsigned dll_count = 0;
|
|
|
|
|
|
|
|
dlls dll_list = {};
|
|
|
|
dlls *dll_last = &dll_list;
|
2009-03-18 12:19:05 +08:00
|
|
|
const wchar_t *process_fn = NULL;
|
2009-01-09 13:11:57 +08:00
|
|
|
while (1)
|
|
|
|
{
|
2009-03-18 12:19:05 +08:00
|
|
|
bool exitnow = false;
|
|
|
|
DWORD cont = DBG_CONTINUE;
|
|
|
|
if (!WaitForDebugEvent (&ev, INFINITE))
|
|
|
|
break;
|
2009-01-09 13:11:57 +08:00
|
|
|
switch (ev.dwDebugEventCode)
|
|
|
|
{
|
|
|
|
case LOAD_DLL_DEBUG_EVENT:
|
2009-03-15 02:40:59 +08:00
|
|
|
if (!isdll && ++dll_count == 2)
|
2009-03-18 12:19:05 +08:00
|
|
|
set_entry_point_break ();
|
2009-01-09 13:11:57 +08:00
|
|
|
dll_last->next = (dlls *) malloc (sizeof (dlls));
|
|
|
|
dll_last->next->lpBaseOfDll = ev.u.LoadDll.lpBaseOfDll;
|
|
|
|
dll_last->next->next = NULL;
|
|
|
|
dll_last = dll_last->next;
|
|
|
|
break;
|
|
|
|
case EXCEPTION_DEBUG_EVENT:
|
2009-03-15 02:40:59 +08:00
|
|
|
switch (ev.u.Exception.ExceptionRecord.ExceptionCode)
|
|
|
|
{
|
2015-07-21 00:32:24 +08:00
|
|
|
case STATUS_ENTRYPOINT_NOT_FOUND:
|
|
|
|
/* A STATUS_ENTRYPOINT_NOT_FOUND might be encountered right after
|
|
|
|
loading all DLLs. We have to handle it here, otherwise ldd
|
|
|
|
runs into an endless loop. */
|
|
|
|
goto print_and_exit;
|
2009-03-15 02:40:59 +08:00
|
|
|
case STATUS_DLL_NOT_FOUND:
|
|
|
|
process_fn = fn_win;
|
|
|
|
break;
|
|
|
|
case STATUS_BREAKPOINT:
|
|
|
|
if (!isdll)
|
2009-03-18 12:19:05 +08:00
|
|
|
cont = DBG_EXCEPTION_NOT_HANDLED;
|
2009-03-15 02:40:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2009-03-18 12:19:05 +08:00
|
|
|
case CREATE_THREAD_DEBUG_EVENT:
|
|
|
|
TerminateProcess (hProcess, 0);
|
|
|
|
break;
|
2009-03-15 02:40:59 +08:00
|
|
|
case EXIT_PROCESS_DEBUG_EVENT:
|
2015-07-21 00:32:24 +08:00
|
|
|
print_and_exit:
|
2009-03-18 12:19:05 +08:00
|
|
|
print_dlls (&dll_list, isdll ? fn_win : NULL, process_fn);
|
|
|
|
exitnow = true;
|
2009-01-09 13:11:57 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-03-18 12:19:05 +08:00
|
|
|
if (!ContinueDebugEvent (ev.dwProcessId, ev.dwThreadId, cont))
|
2009-01-09 13:11:57 +08:00
|
|
|
{
|
|
|
|
cygwin_internal (CW_SETERRNO, __FILE__, __LINE__ - 2);
|
|
|
|
print_errno_error_and_return (in_fn);
|
|
|
|
}
|
2009-03-18 12:19:05 +08:00
|
|
|
if (exitnow)
|
|
|
|
break;
|
2009-01-09 13:11:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int optch;
|
2009-05-15 19:30:18 +08:00
|
|
|
|
|
|
|
/* Use locale from environment. If not set or set to "C", use UTF-8. */
|
|
|
|
setlocale (LC_CTYPE, "");
|
|
|
|
if (!strcmp (setlocale (LC_CTYPE, NULL), "C"))
|
|
|
|
setlocale (LC_CTYPE, "en_US.UTF-8");
|
2011-10-10 22:57:48 +08:00
|
|
|
while ((optch = getopt_long (argc, argv, opts, longopts, NULL)) != -1)
|
2009-01-09 13:11:57 +08:00
|
|
|
switch (optch)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
case 'r':
|
|
|
|
case 'u':
|
2011-10-10 22:57:48 +08:00
|
|
|
error ("option not implemented `-%c'", optch);
|
2009-01-09 13:11:57 +08:00
|
|
|
exit (1);
|
2011-10-10 22:57:48 +08:00
|
|
|
case 'h':
|
|
|
|
usage ();
|
|
|
|
exit (0);
|
|
|
|
case 'V':
|
|
|
|
print_version ();
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
fprintf (stderr, "Try `%s --help' for more information.\n",
|
|
|
|
program_invocation_short_name);
|
|
|
|
return 1;
|
2009-01-09 13:11:57 +08:00
|
|
|
}
|
|
|
|
argv += optind;
|
|
|
|
if (!*argv)
|
2011-10-10 22:57:48 +08:00
|
|
|
error ("missing file arguments");
|
2009-01-09 13:11:57 +08:00
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
bool multiple = !!argv[1];
|
|
|
|
char *fn;
|
|
|
|
while ((fn = *argv++))
|
|
|
|
if (report (fn, multiple))
|
|
|
|
ret = 1;
|
|
|
|
exit (ret);
|
|
|
|
}
|
2009-03-14 14:46:00 +08:00
|
|
|
|
|
|
|
static bool printing = false;
|
|
|
|
|
|
|
|
|
|
|
|
/* dump of import directory
|
|
|
|
section begins at pointer 'section base'
|
|
|
|
section RVA is 'section_rva'
|
|
|
|
import directory begins at pointer 'imp' */
|
|
|
|
static int
|
|
|
|
dump_import_directory (const void *const section_base,
|
|
|
|
const DWORD section_rva,
|
|
|
|
const IMAGE_IMPORT_DESCRIPTOR *imp)
|
|
|
|
{
|
|
|
|
/* get memory address given the RVA */
|
|
|
|
#define adr(rva) ((const void*) ((char*) section_base+((DWORD) (rva))-section_rva))
|
|
|
|
|
|
|
|
/* continue until address inaccessible or there's no DLL name */
|
|
|
|
for (; !IsBadReadPtr (imp, sizeof (*imp)) && imp->Name; imp++)
|
|
|
|
{
|
2009-03-18 12:19:05 +08:00
|
|
|
wchar_t full_path[PATH_MAX];
|
|
|
|
wchar_t *dummy;
|
2009-03-14 14:46:00 +08:00
|
|
|
char *fn = (char *) adr (imp->Name);
|
|
|
|
|
|
|
|
if (saw_file (fn))
|
|
|
|
continue;
|
|
|
|
|
2009-03-18 12:19:05 +08:00
|
|
|
int len = mbstowcs (NULL, fn, 0);
|
|
|
|
if (len <= 0)
|
2011-12-18 07:39:47 +08:00
|
|
|
continue;
|
2009-03-18 12:19:05 +08:00
|
|
|
wchar_t fnw[len + 1];
|
|
|
|
mbstowcs (fnw, fn, len + 1);
|
2009-03-14 14:46:00 +08:00
|
|
|
/* output DLL's name */
|
|
|
|
char *print_fn;
|
2009-03-18 12:19:05 +08:00
|
|
|
if (!SearchPathW (NULL, fnw, NULL, PATH_MAX, full_path, &dummy))
|
2009-03-14 14:46:00 +08:00
|
|
|
{
|
|
|
|
print_fn = strdup ("not found");
|
|
|
|
printing = true;
|
|
|
|
}
|
|
|
|
else if (!printing)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print_fn = tocyg (full_path);
|
|
|
|
strcat (print_fn, " (?)");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("\t%s => %s\n", (char *) fn, print_fn);
|
|
|
|
free (print_fn);
|
|
|
|
}
|
|
|
|
#undef adr
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load a file in RAM (memory-mapped)
|
|
|
|
return pointer to loaded file
|
|
|
|
0 if no success */
|
|
|
|
static void *
|
2009-03-18 12:19:05 +08:00
|
|
|
map_file (const wchar_t *filename)
|
2009-03-14 14:46:00 +08:00
|
|
|
{
|
|
|
|
HANDLE hFile, hMapping;
|
|
|
|
void *basepointer;
|
2009-03-18 12:19:05 +08:00
|
|
|
if ((hFile = CreateFileW (filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
2009-03-14 14:46:00 +08:00
|
|
|
0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0)) == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2009-03-18 12:19:05 +08:00
|
|
|
fprintf (stderr, "couldn't open %ls\n", filename);
|
2009-03-14 14:46:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!(hMapping = CreateFileMapping (hFile, 0, PAGE_READONLY | SEC_COMMIT, 0, 0, 0)))
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
fprintf (stderr, "CreateFileMapping failed with windows error %u\n",
|
|
|
|
(unsigned int) GetLastError ());
|
2009-03-14 14:46:00 +08:00
|
|
|
CloseHandle (hFile);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!(basepointer = MapViewOfFile (hMapping, FILE_MAP_READ, 0, 0, 0)))
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
fprintf (stderr, "MapViewOfFile failed with windows error %u\n",
|
|
|
|
(unsigned int) GetLastError ());
|
2009-03-14 14:46:00 +08:00
|
|
|
CloseHandle (hMapping);
|
|
|
|
CloseHandle (hFile);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-18 07:39:47 +08:00
|
|
|
|
2009-03-14 14:46:00 +08:00
|
|
|
CloseHandle (hMapping);
|
|
|
|
CloseHandle (hFile);
|
|
|
|
|
|
|
|
return basepointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* this will return a pointer immediatly behind the DOS-header
|
|
|
|
0 if error */
|
|
|
|
static void *
|
|
|
|
skip_dos_stub (const IMAGE_DOS_HEADER *dos_ptr)
|
|
|
|
{
|
|
|
|
/* look there's enough space for a DOS-header */
|
|
|
|
if (IsBadReadPtr (dos_ptr, sizeof (*dos_ptr)))
|
|
|
|
{
|
|
|
|
fprintf (stderr, "not enough space for DOS-header\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* validate MZ */
|
|
|
|
if (dos_ptr->e_magic != IMAGE_DOS_SIGNATURE)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "not a DOS-stub\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ok, then, go get it */
|
|
|
|
return (char*) dos_ptr + dos_ptr->e_lfanew;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* find the directory's section index given the RVA
|
|
|
|
Returns -1 if impossible */
|
|
|
|
static int
|
|
|
|
get_directory_index (const unsigned dir_rva,
|
|
|
|
const unsigned dir_length,
|
|
|
|
const int number_of_sections,
|
|
|
|
const IMAGE_SECTION_HEADER *sections)
|
|
|
|
{
|
|
|
|
int sect;
|
|
|
|
for (sect = 0; sect < number_of_sections; sect++)
|
|
|
|
{
|
|
|
|
/* compare directory RVA to section RVA */
|
|
|
|
if (sections[sect].VirtualAddress <= dir_rva
|
|
|
|
&& dir_rva < sections[sect].VirtualAddress+sections[sect].SizeOfRawData)
|
|
|
|
return sect;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dump imports of a single file
|
|
|
|
Returns 0 if successful, !=0 else */
|
|
|
|
static int
|
2009-03-18 12:19:05 +08:00
|
|
|
process_file (const wchar_t *filename)
|
2009-03-14 14:46:00 +08:00
|
|
|
{
|
|
|
|
void *basepointer; /* Points to loaded PE file
|
|
|
|
* This is memory mapped stuff
|
|
|
|
*/
|
|
|
|
int number_of_sections;
|
|
|
|
DWORD import_rva; /* RVA of import directory */
|
|
|
|
DWORD import_length; /* length of import directory */
|
|
|
|
int import_index; /* index of section with import directory */
|
|
|
|
|
|
|
|
/* ensure byte-alignment for struct tag_header */
|
|
|
|
#include <pshpack1.h>
|
|
|
|
|
|
|
|
const struct tag_header
|
|
|
|
{
|
|
|
|
DWORD signature;
|
|
|
|
IMAGE_FILE_HEADER file_head;
|
|
|
|
IMAGE_OPTIONAL_HEADER opt_head;
|
2010-07-06 00:24:24 +08:00
|
|
|
IMAGE_SECTION_HEADER section_header[1]; /* an array of unknown length */
|
2009-03-14 14:46:00 +08:00
|
|
|
} *header;
|
|
|
|
|
|
|
|
/* revert to regular alignment */
|
|
|
|
#include <poppack.h>
|
|
|
|
|
|
|
|
printing = false;
|
|
|
|
|
|
|
|
/* first, load file */
|
|
|
|
basepointer = map_file (filename);
|
|
|
|
if (!basepointer)
|
|
|
|
{
|
|
|
|
puts ("cannot load file");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get header pointer; validate a little bit */
|
2010-07-06 00:24:24 +08:00
|
|
|
header = (tag_header *) skip_dos_stub ((IMAGE_DOS_HEADER *) basepointer);
|
2009-03-14 14:46:00 +08:00
|
|
|
if (!header)
|
|
|
|
{
|
|
|
|
puts ("cannot skip DOS stub");
|
|
|
|
UnmapViewOfFile (basepointer);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look there's enough space for PE headers */
|
|
|
|
if (IsBadReadPtr (header, sizeof (*header)))
|
|
|
|
{
|
|
|
|
puts ("not enough space for PE headers");
|
|
|
|
UnmapViewOfFile (basepointer);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* validate PE signature */
|
2010-07-06 00:24:24 +08:00
|
|
|
if (header->signature != IMAGE_NT_SIGNATURE)
|
2009-03-14 14:46:00 +08:00
|
|
|
{
|
|
|
|
puts ("not a PE file");
|
|
|
|
UnmapViewOfFile (basepointer);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get number of sections */
|
|
|
|
number_of_sections = header->file_head.NumberOfSections;
|
|
|
|
|
|
|
|
/* check there are sections... */
|
2010-07-06 00:24:24 +08:00
|
|
|
if (number_of_sections < 1)
|
2009-03-14 14:46:00 +08:00
|
|
|
{
|
|
|
|
UnmapViewOfFile (basepointer);
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* validate there's enough space for section headers */
|
|
|
|
if (IsBadReadPtr (header->section_header, number_of_sections*sizeof (IMAGE_SECTION_HEADER)))
|
|
|
|
{
|
|
|
|
puts ("not enough space for section headers");
|
|
|
|
UnmapViewOfFile (basepointer);
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get RVA and length of import directory */
|
|
|
|
import_rva = header->opt_head.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
|
|
|
|
import_length = header->opt_head.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
|
|
|
|
|
|
|
|
/* check there's stuff to care about */
|
|
|
|
if (!import_rva || !import_length)
|
|
|
|
{
|
|
|
|
UnmapViewOfFile (basepointer);
|
|
|
|
return 0; /* success! */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get import directory pointer */
|
|
|
|
import_index = get_directory_index (import_rva,import_length,number_of_sections,header->section_header);
|
|
|
|
|
|
|
|
/* check directory was found */
|
2010-07-06 00:24:24 +08:00
|
|
|
if (import_index < 0)
|
2009-03-14 14:46:00 +08:00
|
|
|
{
|
|
|
|
puts ("couldn't find import directory in sections");
|
|
|
|
UnmapViewOfFile (basepointer);
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
|
2010-07-06 00:24:24 +08:00
|
|
|
/* The pointer to the start of the import directory's section */
|
|
|
|
const void *section_address = (char*) basepointer + header->section_header[import_index].PointerToRawData;
|
|
|
|
if (dump_import_directory (section_address,
|
|
|
|
header->section_header[import_index].VirtualAddress,
|
|
|
|
/* the last parameter is the pointer to the import directory:
|
|
|
|
section address + (import RVA - section RVA)
|
|
|
|
The difference is the offset of the import directory in the section */
|
|
|
|
(const IMAGE_IMPORT_DESCRIPTOR *) ((char *) section_address+import_rva-header->section_header[import_index].VirtualAddress)))
|
|
|
|
{
|
|
|
|
UnmapViewOfFile (basepointer);
|
|
|
|
return 8;
|
|
|
|
}
|
2011-12-18 07:39:47 +08:00
|
|
|
|
2009-03-14 14:46:00 +08:00
|
|
|
UnmapViewOfFile (basepointer);
|
|
|
|
return 0;
|
|
|
|
}
|