Reputation: 121
I'm using MinGW on Windows 10 to build Windows executables (programmed in Rust, though I don't think that part matters). I don't like having to manually keep track of which of the libraries I use have DLL files that need to be included with the release builds of my applications, so I'd like to be able to write a script that can automatically find and copy them next to the app after I build it. To that end, I'm trying to find a command-line program that will list (to stdout or a file) the DLL dependencies of a Windows executable I pass it - bonus points if it filters out DLLs that I can assume are included with Windows in the first place.
I know about Dependency Walker already, but it's old and doesn't seem to like Windows 10 very much (it yields a lot of false negatives); I know about dumpbin, but from the sound of it I can't use that outside of a Visual Studio context anyway. Cygwin apparently has a working ldd, but I'd rather not install Cygwin in its entirety just for this one purpose if I can help it.
Are there any other tools available for this purpose? If not, how could I program one myself? I've seen a script that scrapes the output of objdump -p
(a MinGW utility) for this purpose; I can always fall back on that if there's no better way.
Upvotes: 2
Views: 3998
Reputation: 447
I interpret you question as follows. Given a .exe
how can I write a program which lists the external dependencies to dynamic link libraries? Given this list how can I sort for dll's which aren't included with windows?
You have to work out how the dynamic linker in windows get's this information. See the .exe
or portable executable file format, specifically the Import table.
"PE File Imports Earlier, I described how function calls to outside DLLs don't call the DLL directly. Instead, the CALL instruction goes to a JMP DWORD PTR [XXXXXXXX] instruction somewhere in the executable's .text section (or .icode section if you're using Borland C++). The address that the JMP instruction looks up and transfers control to is the real target address. The PE file's .idata section contains the information necessary for the loader to determine the addresses of the target functions and patch them into the executable image. The .idata section (or import table, as I prefer to call it) begins with an array of IMAGE_IMPORT_DESCRIPTORs. There is one IMAGE_IMPORT_DESCRIPTOR for each DLL that the PE file implicitly links to. There's no field indicating the number of structures in this array. Instead, the last element of the array is indicated by an IMAGE_IMPORT_DESCRIPTOR that has fields filled with NULLs. The format of an IMAGE_IMPORT_DESCRIPTOR is shown in Figure 10."
"- Matt Pietrek March 1994 Matt Pietrek is the author of Windows Internals (Addison-Wesley, 1993). He works at Nu-Mega Technologies Inc., and can be reached via CompuServe: 71774,362 This article is reproduced from the March 1994 issue of Microsoft Systems Journal. Copyright © 1994 by Miller Freeman, Inc. All rights are reserved. No part of this article may be reproduced in any fashion (except in brief quotations used in critical articles and reviews) without the prior consent of Miller Freeman." - https://msdn.microsoft.com/en-us/library/ms809762.aspx
The .exe
should have an import table which a structure defined in WINNT.H containing a DWORD name
for each implicitly linked dll. Note you will have to see how Windows 10 has changed and if there are changes to the executable format. Hope this helps if you wanted to program something to do this.
Also obtaining a list of windows standard user mode driver's and dll's should be easy to find and check against.
Snippet of .exe
I wrote in c++, I can find
_CxxThrowException w _CorExeMain mscoree.dll
8 ! 0 1 2 3 9 : ; < ) * + , - . / 4 5 6 7 " # $ % & ' ( = > ? N O R T V P Q S U @ B C D E G H I J K M A F L Y Z \ [ ] ^ _ ` a b c d e f ÿÿÿÿ ±¿DNæ@»
Ès@ .?AVbad_alloc@std@@ Ès@ .?AVexception@std@@ Ès@
.?AVbad_array_new_length@std@@ Ès@ .?AVtype_info@@
So it appears searching for string ".dll"
would work, then filter it with a list of windows standard dll.
Alternatively I have written a very very crude forms application which minimally does what you ask. It's on github both the source and the executable. I'm don't work with forms applications so forgive the bugs. https://github.com/marshalcraft/CheapoDllDependencyTool
Upvotes: 1