1 /* Copyright (c) 2009, 2010, 2011, 2013 Chris Faylor
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
15 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cygwin.h>
36 #include <cygwin/version.h>
40 #define _WIN32_WINNT 0x0a00
46 struct option longopts
[] =
48 {"help", no_argument
, NULL
, 'h'},
49 {"verbose", no_argument
, NULL
, 'v'},
50 {"version", no_argument
, NULL
, 'V'},
51 {"data-relocs", no_argument
, NULL
, 'd'},
52 {"function-relocs", no_argument
, NULL
, 'r'},
53 {"unused", no_argument
, NULL
, 'u'},
54 {0, no_argument
, NULL
, 0}
56 const char *opts
= "dhruvV";
58 static int process_file (const wchar_t *);
59 static void *drive_map
;
62 error (const char *fmt
, ...)
66 fprintf (stderr
, "ldd: ");
67 vfprintf (stderr
, fmt
, ap
);
68 fprintf (stderr
, "\nTry `ldd --help' for more information.\n");
72 static void __attribute__ ((__noreturn__
))
75 printf ("Usage: %s [OPTION]... FILE...\n\
77 Print shared library dependencies\n\
79 -h, --help print this help and exit\n\
80 -V, --version print version information and exit\n\
81 -r, --function-relocs process data and function relocations\n\
82 (currently unimplemented)\n\
83 -u, --unused print unused direct dependencies\n\
84 (currently unimplemented)\n\
85 -v, --verbose print all information\n\
86 (currently unimplemented)\n",
87 program_invocation_short_name
);
94 printf ("ldd (cygwin) %d.%d.%d\n"
95 "Print shared library dependencies\n"
96 "Copyright (C) 2009 - %s Chris Faylor\n"
97 "This is free software; see the source for copying conditions. There is NO\n"
98 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
99 CYGWIN_VERSION_DLL_MAJOR
/ 1000,
100 CYGWIN_VERSION_DLL_MAJOR
% 1000,
101 CYGWIN_VERSION_DLL_MINOR
,
102 strrchr (__DATE__
, ' ') + 1);
105 #define print_errno_error_and_return(__fn) \
107 fprintf (stderr, "ldd: %s: %s\n", (__fn), strerror (errno));\
111 #define set_errno_and_return(x) \
113 cygwin_internal (CW_SETERRNO, __FILE__, __LINE__ - 2);\
118 static HANDLE hProcess
;
120 static struct filelist
122 struct filelist
*next
;
127 saw_file (char *name
)
131 for (p
= head
; p
; p
= p
->next
)
132 if (strcasecmp (name
, p
->name
) == 0)
135 p
= (filelist
*) malloc(sizeof (struct filelist
));
137 p
->name
= strdup (name
);
143 get_module_filename (HANDLE hp
, HMODULE hm
)
148 for (len
= 1024; (res
= GetModuleFileNameExW (hp
, hm
, (buf
= (wchar_t *) realloc (buf
, len
* sizeof (wchar_t))), len
)) == len
; len
+= 1024)
159 GetFileNameFromHandle(HANDLE hFile
, WCHAR pszFilename
[MAX_PATH
+1])
163 OBJECT_NAME_INFORMATION
*ntfn
= (OBJECT_NAME_INFORMATION
*) alloca (65536);
164 NTSTATUS status
= NtQueryObject (hFile
, ObjectNameInformation
,
166 if (NT_SUCCESS (status
))
168 PWCHAR win32path
= ntfn
->Name
.Buffer
;
169 win32path
[ntfn
->Name
.Length
/ sizeof (WCHAR
)] = L
'\0';
171 /* NtQueryObject returns a native NT path. (Try to) convert to Win32. */
173 drive_map
= (void *) cygwin_internal (CW_ALLOC_DRIVE_MAP
);
175 win32path
= (PWCHAR
) cygwin_internal (CW_MAP_DRIVE_MAP
, drive_map
,
177 pszFilename
[0] = L
'\0';
178 wcsncat (pszFilename
, win32path
, MAX_PATH
);
185 load_dll (const wchar_t *fn
)
187 wchar_t *buf
= get_module_filename (GetCurrentProcess (), NULL
);
190 printf ("ldd: GetModuleFileName returned an error %u\n",
191 (unsigned int) GetLastError ());
192 exit (1); /* FIXME */
195 wchar_t *newbuf
= (wchar_t *) malloc ((sizeof (L
"\"\" -- ") + wcslen (buf
) + wcslen (fn
)) * sizeof (wchar_t));
197 wcscpy (newbuf
+ 1, buf
);
198 wchar_t *p
= wcsstr (newbuf
, L
"\\ldd");
201 printf ("ldd: can't parse my own filename \"%ls\"\n", buf
);
205 wcscat (newbuf
, L
"\" -- ");
212 start_process (const wchar_t *fn
, bool& isdll
)
214 STARTUPINFOW si
= {};
215 PROCESS_INFORMATION pi
;
218 /* OCaml natdynlink plugins (.cmxs) cannot be handled by ldd because they
219 can only be loaded by flexdll_dlopen() */
220 if (wcslen (fn
) < 4 || (wcscasecmp (wcschr (fn
, L
'\0') - 4, L
".dll") != 0
221 && wcscasecmp (wcschr (fn
, L
'\0') - 4, L
".oct") != 0
222 && wcscasecmp (wcschr (fn
, L
'\0') - 3, L
".so") != 0))
232 if (CreateProcessW (NULL
, cmd
, NULL
, NULL
, FALSE
, DEBUG_ONLY_THIS_PROCESS
, NULL
, NULL
, &si
, &pi
))
235 hProcess
= pi
.hProcess
;
236 DebugSetProcessKillOnExit (true);
241 set_errno_and_return (1);
251 #define SLOP strlen (" (?)")
253 tocyg (wchar_t *win_fn
)
255 ssize_t cwlen
= cygwin_conv_path (CCP_WIN_W_TO_POSIX
, win_fn
, NULL
, 0);
259 int len
= wcstombs (NULL
, win_fn
, 0) + 1;
260 if ((fn
= (char *) malloc (len
)))
261 wcstombs (fn
, win_fn
, len
);
265 char *fn_cyg
= (char *) malloc (cwlen
+ SLOP
+ 1);
266 if (cygwin_conv_path (CCP_WIN_W_TO_POSIX
, win_fn
, fn_cyg
, cwlen
) == 0)
271 int len
= wcstombs (NULL
, win_fn
, 0);
272 fn
= (char *) malloc (len
+ SLOP
+ 1);
273 wcstombs (fn
, win_fn
, len
+ SLOP
+ 1);
279 #define CYGWIN_DLL_LEN (wcslen (L"\\cygwin1.dll"))
281 print_dlls (dlls
*dll
, const wchar_t *dllfn
, const wchar_t *process_fn
)
283 head
= NULL
; /* FIXME: memory leak */
284 while ((dll
= dll
->next
))
287 wchar_t *fullpath
= get_module_filename (hProcess
, (HMODULE
) dll
->lpBaseOfDll
);
290 // if no path found yet, try getting it from an open handle to the DLL
291 wchar_t dllname
[MAX_PATH
+1];
292 if (GetFileNameFromHandle (dll
->hFile
, dllname
))
294 fn
= tocyg (dllname
);
295 saw_file (basename (fn
));
300 else if (dllfn
&& wcscmp (fullpath
, dllfn
) == 0)
307 fn
= tocyg (fullpath
);
308 saw_file (basename (fn
));
311 printf ("\t%s => %s (%p)\n", basename (fn
), fn
, dll
->lpBaseOfDll
);
315 return process_file (process_fn
);
320 report (const char *in_fn
, bool multiple
)
323 printf ("%s:\n", in_fn
);
324 char *fn
= realpath (in_fn
, NULL
);
326 print_errno_error_and_return (in_fn
);
328 ssize_t len
= cygwin_conv_path (CCP_POSIX_TO_WIN_W
, fn
, NULL
, 0);
330 print_errno_error_and_return (fn
);
333 wchar_t fn_win
[len
+ 1];
334 if (cygwin_conv_path (CCP_POSIX_TO_WIN_W
, fn
, fn_win
, len
))
335 print_errno_error_and_return (fn
);
337 if (!fn
|| start_process (fn_win
, isdll
))
338 print_errno_error_and_return (in_fn
);
343 dlls
*dll_last
= &dll_list
;
344 const wchar_t *process_fn
= NULL
;
350 bool exitnow
= false;
351 DWORD cont
= DBG_CONTINUE
;
352 if (!WaitForDebugEvent (&ev
, INFINITE
))
354 switch (ev
.dwDebugEventCode
)
356 case CREATE_PROCESS_DEBUG_EVENT
:
359 PIMAGE_DOS_HEADER dos_header
= (PIMAGE_DOS_HEADER
) alloca (4096);
360 PIMAGE_NT_HEADERS nt_header
;
362 static const unsigned char int3
= 0xcc;
365 if (!ReadProcessMemory (hProcess
,
366 ev
.u
.CreateProcessInfo
.lpBaseOfImage
,
367 dos_header
, 4096, &bytes
))
368 print_errno_error_and_return (in_fn
);
370 nt_header
= PIMAGE_NT_HEADERS (PBYTE (dos_header
)
371 + dos_header
->e_lfanew
);
372 entry_point
= (PVOID
)
373 ((caddr_t
) ev
.u
.CreateProcessInfo
.lpBaseOfImage
374 + nt_header
->OptionalHeader
.AddressOfEntryPoint
);
376 if (!WriteProcessMemory (hProcess
, entry_point
, &int3
, 1, &bytes
))
377 print_errno_error_and_return (in_fn
);
380 case LOAD_DLL_DEBUG_EVENT
:
381 dll_last
->next
= (dlls
*) malloc (sizeof (dlls
));
382 dll_last
->next
->lpBaseOfDll
= ev
.u
.LoadDll
.lpBaseOfDll
;
383 dll_last
->next
->hFile
= ev
.u
.LoadDll
.hFile
;
384 dll_last
->next
->next
= NULL
;
385 dll_last
= dll_last
->next
;
387 case EXCEPTION_DEBUG_EVENT
:
388 switch (ev
.u
.Exception
.ExceptionRecord
.ExceptionCode
)
390 case STATUS_ENTRYPOINT_NOT_FOUND
:
391 /* A STATUS_ENTRYPOINT_NOT_FOUND might be encountered right after
392 loading all DLLs. We have to handle it here, otherwise ldd
393 runs into an endless loop. */
395 case STATUS_DLL_NOT_FOUND
:
398 case STATUS_BREAKPOINT
:
400 TerminateProcess (hProcess
, 0);
403 if (ev
.u
.Exception
.ExceptionRecord
.ExceptionFlags
&
404 EXCEPTION_NONCONTINUABLE
) {
409 case EXIT_PROCESS_DEBUG_EVENT
:
410 if (ev
.u
.ExitProcess
.dwExitCode
!= 0)
413 print_dlls (&dll_list
, isdll
? fn_win
: NULL
, process_fn
);
419 if (!ContinueDebugEvent (ev
.dwProcessId
, ev
.dwThreadId
, cont
))
421 cygwin_internal (CW_SETERRNO
, __FILE__
, __LINE__
- 2);
422 print_errno_error_and_return (in_fn
);
432 main (int argc
, char **argv
)
436 /* Use locale from environment. If not set or set to "C", use UTF-8. */
437 setlocale (LC_CTYPE
, "");
438 if (!strcmp (setlocale (LC_CTYPE
, NULL
), "C"))
439 setlocale (LC_CTYPE
, "en_US.UTF-8");
440 while ((optch
= getopt_long (argc
, argv
, opts
, longopts
, NULL
)) != -1)
446 error ("option not implemented `-%c'", optch
);
454 fprintf (stderr
, "Try `%s --help' for more information.\n",
455 program_invocation_short_name
);
460 error ("missing file arguments");
463 bool multiple
= !!argv
[1];
465 while ((fn
= *argv
++))
466 if (report (fn
, multiple
))
469 cygwin_internal (CW_FREE_DRIVE_MAP
, drive_map
);
473 static bool printing
= false;
476 /* dump of import directory
477 section begins at pointer 'section base'
478 section RVA is 'section_rva'
479 import directory begins at pointer 'imp' */
481 dump_import_directory (const void *const section_base
,
482 const DWORD section_rva
,
483 const IMAGE_IMPORT_DESCRIPTOR
*imp
)
485 /* get memory address given the RVA */
486 #define adr(rva) ((const void*) ((char*) section_base+((DWORD) (rva))-section_rva))
488 /* continue until address inaccessible or there's no DLL name */
489 for (; !IsBadReadPtr (imp
, sizeof (*imp
)) && imp
->Name
; imp
++)
491 wchar_t full_path
[PATH_MAX
];
493 char *fn
= (char *) adr (imp
->Name
);
498 int len
= mbstowcs (NULL
, fn
, 0);
501 wchar_t fnw
[len
+ 1];
502 mbstowcs (fnw
, fn
, len
+ 1);
503 /* output DLL's name */
505 if (!SearchPathW (NULL
, fnw
, NULL
, PATH_MAX
, full_path
, &dummy
))
507 print_fn
= strdup ("not found");
514 print_fn
= tocyg (full_path
);
515 strcat (print_fn
, " (?)");
518 printf ("\t%s => %s\n", (char *) fn
, print_fn
);
526 /* load a file in RAM (memory-mapped)
527 return pointer to loaded file
530 map_file (const wchar_t *filename
)
532 HANDLE hFile
, hMapping
;
534 if ((hFile
= CreateFileW (filename
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
535 0, OPEN_EXISTING
, FILE_FLAG_SEQUENTIAL_SCAN
, 0)) == INVALID_HANDLE_VALUE
)
537 fprintf (stderr
, "couldn't open %ls\n", filename
);
540 if (!(hMapping
= CreateFileMapping (hFile
, 0, PAGE_READONLY
| SEC_COMMIT
, 0, 0, 0)))
542 fprintf (stderr
, "CreateFileMapping failed with windows error %u\n",
543 (unsigned int) GetLastError ());
547 if (!(basepointer
= MapViewOfFile (hMapping
, FILE_MAP_READ
, 0, 0, 0)))
549 fprintf (stderr
, "MapViewOfFile failed with windows error %u\n",
550 (unsigned int) GetLastError ());
551 CloseHandle (hMapping
);
556 CloseHandle (hMapping
);
563 /* this will return a pointer immediatly behind the DOS-header
566 skip_dos_stub (const IMAGE_DOS_HEADER
*dos_ptr
)
568 /* look there's enough space for a DOS-header */
569 if (IsBadReadPtr (dos_ptr
, sizeof (*dos_ptr
)))
571 fprintf (stderr
, "not enough space for DOS-header\n");
576 if (dos_ptr
->e_magic
!= IMAGE_DOS_SIGNATURE
)
578 fprintf (stderr
, "not a DOS-stub\n");
582 /* ok, then, go get it */
583 return (char*) dos_ptr
+ dos_ptr
->e_lfanew
;
587 /* find the directory's section index given the RVA
588 Returns -1 if impossible */
590 get_directory_index (const unsigned dir_rva
,
591 const unsigned dir_length
,
592 const int number_of_sections
,
593 const IMAGE_SECTION_HEADER
*sections
)
596 for (sect
= 0; sect
< number_of_sections
; sect
++)
598 /* compare directory RVA to section RVA */
599 if (sections
[sect
].VirtualAddress
<= dir_rva
600 && dir_rva
< sections
[sect
].VirtualAddress
+sections
[sect
].SizeOfRawData
)
607 /* dump imports of a single file
608 Returns 0 if successful, !=0 else */
610 process_file (const wchar_t *filename
)
612 void *basepointer
; /* Points to loaded PE file
613 * This is memory mapped stuff
615 int number_of_sections
;
616 DWORD import_rva
; /* RVA of import directory */
617 DWORD import_length
; /* length of import directory */
618 int import_index
; /* index of section with import directory */
620 /* ensure byte-alignment for struct tag_header */
621 #include <pshpack1.h>
623 const struct tag_header
626 IMAGE_FILE_HEADER file_head
;
627 IMAGE_OPTIONAL_HEADER opt_head
;
628 IMAGE_SECTION_HEADER section_header
[1]; /* an array of unknown length */
631 /* revert to regular alignment */
636 /* first, load file */
637 basepointer
= map_file (filename
);
640 puts ("cannot load file");
644 /* get header pointer; validate a little bit */
645 header
= (tag_header
*) skip_dos_stub ((IMAGE_DOS_HEADER
*) basepointer
);
648 puts ("cannot skip DOS stub");
649 UnmapViewOfFile (basepointer
);
653 /* look there's enough space for PE headers */
654 if (IsBadReadPtr (header
, sizeof (*header
)))
656 puts ("not enough space for PE headers");
657 UnmapViewOfFile (basepointer
);
661 /* validate PE signature */
662 if (header
->signature
!= IMAGE_NT_SIGNATURE
)
664 puts ("not a PE file");
665 UnmapViewOfFile (basepointer
);
669 /* get number of sections */
670 number_of_sections
= header
->file_head
.NumberOfSections
;
672 /* check there are sections... */
673 if (number_of_sections
< 1)
675 UnmapViewOfFile (basepointer
);
679 /* validate there's enough space for section headers */
680 if (IsBadReadPtr (header
->section_header
, number_of_sections
*sizeof (IMAGE_SECTION_HEADER
)))
682 puts ("not enough space for section headers");
683 UnmapViewOfFile (basepointer
);
687 /* get RVA and length of import directory */
688 import_rva
= header
->opt_head
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
].VirtualAddress
;
689 import_length
= header
->opt_head
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
].Size
;
691 /* check there's stuff to care about */
692 if (!import_rva
|| !import_length
)
694 UnmapViewOfFile (basepointer
);
695 return 0; /* success! */
698 /* get import directory pointer */
699 import_index
= get_directory_index (import_rva
,import_length
,number_of_sections
,header
->section_header
);
701 /* check directory was found */
702 if (import_index
< 0)
704 puts ("couldn't find import directory in sections");
705 UnmapViewOfFile (basepointer
);
709 /* The pointer to the start of the import directory's section */
710 const void *section_address
= (char*) basepointer
+ header
->section_header
[import_index
].PointerToRawData
;
711 if (dump_import_directory (section_address
,
712 header
->section_header
[import_index
].VirtualAddress
,
713 /* the last parameter is the pointer to the import directory:
714 section address + (import RVA - section RVA)
715 The difference is the offset of the import directory in the section */
716 (const IMAGE_IMPORT_DESCRIPTOR
*) ((char *) section_address
+import_rva
-header
->section_header
[import_index
].VirtualAddress
)))
718 UnmapViewOfFile (basepointer
);
722 UnmapViewOfFile (basepointer
);