Fixed copyright date.
[wine/testsucceed.git] / tools / winedump / README
blob99a26d709886704f521df2ccfe69711a9f99d16d
1 Winedump - A Wine DLL tool
2 --------------------------
4 Background
5 ----------
7 Most of the functions available in Windows, and in Windows applications, are
8 made available to applications from DLL's. Wine implements the Win32 API by
9 providing replacement's for the essential Windows DLLs in the form of Unix
10 shared library (.so) files, and provides a tool, winebuild, to allow Winelib
11 applications to link to functions exported from shared libraries/DLLs.
13 The first thing to note is that there are many DLLs that aren't yet
14 implemented in Wine. Mostly this doesn't present a problem because the native
15 Win32 versions of lots of DLLs can be used without problems, at least on
16 x86 platforms. However, one of Wine's goals is the eventual replacement of
17 every essential O/S DLL so that the whole API is implemented. This not only
18 means that a copy of the real O/S is not needed, but also that non-x86
19 platforms can run most Win32 programs after recompiling.
21 The second thing to note is that applications commonly use their own or 3rd
22 party DLLs to provide functionality. In order to call these functions with
23 a Winelib program, some 'glue' is needed. This 'glue' comes in the form of
24 a .spec file. The .spec file, along with some dummy code, is used to create
25 a Wine .so corresponding to the Windows DLL. The winebuild program can then
26 resolve calls made to DLL functions to call your dummy DLL. You then tell
27 Wine to only use the native Win32 version of the DLL, and at runtime your
28 calls will be made to the Win32 DLL. If you want to reimplement the dll,
29 you simply add the code for the DLL calls to your stub .so, and then tell
30 Wine to use the .so version instead [1].
32 These two factors mean that if you are:
34 A: Reimplementing a Win32 DLL for use within Wine, or
35 B: Compiling a Win32 application with Winelib that uses x86 DLLs
37 Then you will need to create a .spec file (amongst other things). If you
38 won't be doing either of the above, then you won't need winedump.
40 Creating a .spec file is a labour intensive task during which it is easy
41 to make a mistake. The idea of winedump is to automate this task and create
42 the majority of the support code needed for your DLL. In addition you can
43 have winedump create code to help you reimplement a DLL, by providing
44 tracing of calls to the DLL, and (in some cases) automatically determining
45 the parameters, calling conventions, and return values of the DLLs functions.
47 You can think of winedump as somewhat similar to the IMPLIB tool when
48 only its basic functionality is used.
51 Usage
52 -----
54 Winedump is a command line tool. Running it with no arguments or passing
55 it '-h' on the command line lists the available options:
57 Winedump can be used for different usages:
58 - generating default source files (.spec, .c...) for using a native DLL in Wine
59 - demangling MSVC C++ symbol names
60 - dumping the 'PE' files contents
62 Usage: winedump [-h sym <sym> spec <dll> dump <dll>] [mode options]
63 When used in -h mode
64    -h           Display this help message
65 When used in sym mode
66    sym <sym>    Demangle C++ symbol <sym>' and exit
67 When used in spec mode
68    spec <dll>   Use dll for input file and generate implementation code
69    -I dir       Look for prototypes in 'dir' (implies -c)
70    -c           Generate skeleton code (requires -I)
71    -t           TRACE arguments (implies -c)
72    -f dll       Forward calls to 'dll' (implies -t)
73    -D           Generate documentation
74    -o name      Set the output dll name (default: dll)
75    -C           Assume __cdecl calls (default: __stdcall)
76    -s num       Start prototype search after symbol 'num'
77    -e num       End prototype search after symbol 'num'
78    -q           Don't show progress (quiet).
79    -v           Show lots of detail while working (verbose).
80 When used in dump mode
81    dump <dll>   Dumps the content of the dll named <dll>
82    -C           Turns on symbol demangling
83    -f           Dumps file header information
84    -j sect_name Dumps only the content of section sect_name (import, export, debug)
85    -x           Dumps everything
87 Basic options
88 -------------
90 OPTION: -o name  Set the output dll name (default: dll)
92 By default, if winedump is run on DLL 'foo', it creates files called
93 'foo.spec', 'foo_main.c' etc, and prefixes any functions generated
94 with 'FOO_'. If '-o bar' is given, these will become 'bar.spec',
95 'bar_main.c' and 'BAR_' respectively.
97 This option is mostly useful when generating a forwarding DLL. See below
98 for more information.
100 OPTION: -q       Don't show progress (quiet).
101         -v       Show lots of detail while working (verbose).
103 There are 3 levels of output while winedump is running. The default level,
104 when neither -q or -v are given, prints the number of exported functions
105 found in the dll, followed by the name of each function as it is processed,
106 and a status indication of whether it was processed OK. With -v given, a
107 lot of information is dumped while winedump works: this is intended to help
108 debug any problems. Giving -q means nothing will be printed unless a fatal
109 error occurs, and could be used when calling winedump from a script.
112 OPTION: -C       Assume __cdecl calls (default: __stdcall)
114 This option determines the default calling convention used by the functions
115 in the DLL. If specbuild cannot determine the convention, __stdcall is
116 used by default, unless this option has been given.
118 Unless -q is given, a warning will be printed for every function that
119 winedump determines the calling convention for and which does not match
120 the assumed calling convention.
123 Generating stub DLLS
124 --------------------
126 If all you want to do is generate a stub DLL to allow you to link your
127 Winelib application to an x86 DLL, the above options are all you need.
129 As an example, lets assume the application you are porting uses functions
130 from a 3rd party dll called 'zipextra.dll', and the functions in the DLL
131 use the __stdcall calling convention. Copy zipextra.dll to an empty directory,
132 change to it, and run winedump as follows:
134 winedump spec zipextra  (Note: this assumes winedump is in your path)
136 The output will look something like the following:
138 22 named symbols in DLL, 22 in total ...
139 Export    1 - '_OpenZipFile' ... [Ignoring]
140 Export    2 - '_UnZipFile' ... [Ignoring]
143 "[Ignoring]" Just tells you that winedump isn't trying to determine the
144 parameters or return types of the functions, its just creating stubs.
146 The following files are created:
148 zipextra.spec
149 This is the .spec file. Each exported function is listed as a stub:
151 @ stub _OpenZipFile
152 @ stub _UnZipFile
155 This means that winebuild will generate dummy code for this function. That
156 doesn't concern us, because all we want is for winebuild to allow the
157 symbols to be resolved. At run-time, the functions in the native DLL will
158 be called; this just allows us to link.
160 zipextra_dll.h zipextra_main.c
161 These are source code files containing the minimum set of code to build
162 a stub DLL. The C file contains one function, ZIPEXTRA_Init, which does
163 nothing.
165 Makefile.in
166 This is a template for 'configure' to produce a makefile. It is designed
167 for a DLL that will be inserted into the Wine source tree. If your DLL
168 will not be part of Wine, or you don't wish to build it this way,
169 you should look at the Wine tool 'winemaker' to generate a DLL project.
171 FIXME: winemaker could run this tool automatically when generating projects
172 that use extra DLL's (*.lib in the "ADD LINK32" line in .dsp) ....
174 zipextra_install
175 A shell script for adding zipextra to the Wine source tree (see below).
178 Inserting a stub DLL into the Wine tree
179 ---------------------------------------
181 To build your stub DLL as part of Wine, do the following:
183  chmod a+x ./zipextra_install
184  ./zipextra_install <wine-path>
185  cd <wine-path>
186  autoconf
187  ./configure
188  make depend && make
189  make install
191 Your application can now link with the DLL.
193 NOTE: **DO NOT** submit patches to Wine for 3rd party DLLs! Building DLLs
194       into your copy of the tree is just a simple way for you to link. When
195       you release your application you won't be distributing the Unix .so
196       anyway, just the Win32 DLL. As you update your version of Wine
197       you can simply re-run the procedure above (Since no patches are
198       involved, it should be pretty resiliant to changes).
201 Advanced Options
202 ----------------
204 This section discusses features of winedump that are useful to Wine Hackers
205 or developers looking to reimplement a Win32 DLL for Unix. Using these
206 features means you will need to be able to resolve compilation problems and
207 have a general understanding of Wine programming.
210 OPTION: -I dir   Look for prototypes in 'dir' (implies -c)
212 For all advanced functionality, you must give winedump a directoryor file that
213 contains prototypes for the DLL. In the case of Windows DLLs, this could be
214 either the standard include directory from your compiler, or an SDK include
215 directory. If you have a text document with prototypes (such as documentation)
216 that can be used also, however you may need to delete some non-code lines to
217 ensure that prototypes are parsed correctly.
219 The 'dir' argument can also be a file specification (e.g. "include/*"). If
220 it contains wildcards you must quote it to prevent the shell from expanding it.
222 If you have no prototypes, specify /dev/null for 'dir'. Winedump may still
223 be able to generate some working stub code for you.
225 Once you have created your DLL, if you generated code (see below), you can
226 backup the DLL header file created and use it for rebuilding the DLL (you
227 should remove the DLLNAME_ prefix from the prototypes to make this work). This
228 allows you to add names to the function arguments, for example, so that the
229 comments and prototype in the regenerated DLL will be clearer.
231 Winedump searches for prototypes using 'grep', and then retrieves each
232 prototype by calling 'function_grep.pl', a Perl script. When you pass the -v
233 option on the command line, the calls to both of these programs are logged.
234 This allows you to see where each function definition has come from. Should
235 winedump take an excessively long time to locate a prototype, you can check
236 that it is searching the right files; you may want to limit the number of files
237 searched if locating the prototype takes too long.
239 You can compile function_grep.pl for a slight increase in performance; see
240 'man perlcc' for details.
243 OPTION: -s num   Start prototype search after symbol 'num'
244         -e num   End prototype search after symbol 'num'
246 By passing the -s or -e options you can have winedump try to generate code
247 for only some functions in your DLL. This may be used to generate a single
248 function, for example, if you wanted to add functionality to an existing DLL.
250 They is also useful for debugging problems, in conjunction with -v.
253 OPTION: -D       Generate documentation
255 By default, winedump generates a standard comment at the header of each
256 function it generates. Passing this option makes winedump output a full
257 header template for standard Wine documentation, listing the parameters
258 and return value of the function.
261 OPTION: -c       Generate skeleton code (requires -I)
263 This option tells winedump that you want to create function stubs for
264 each function in the DLL. This is the most basic level of code generation.
265 As winedump reads each exported symbol from the source DLL, it first tries
266 to demangle the name. If the name is a C++ symbol, the arguments, class and
267 return value are all encoded into the symbol name. Winedump converts this
268 information into a C function prototype. If this fails, the file(s) specified
269 in the -I argument are scanned for a function prototype. If one is found it
270 is used for the next step of the process, code generation.
272 Note: C++ name demangling is currently under development. Since the algorithm
273 used is not documented, it must be decoded. Many simple prototypes are already
274 working however.
276 If winedump does not find a prototype, it emits code like the following:
278 In the .spec file:
280 @stub _OpenZipFile
282 in the header file:
284 /* __cdecl ZIPEXTRA__OpenZipFile() */
286 in the C source file:
288 /*********************************************************************
289  *      _OpenZipFile     (ZIPEXTRA.@)
291  */
292 #if 0
293 __stdcall ZIPEXTRA__OpenZipFile()
295     /* '@Stubbed'ed in .spec */
297 #endif
299 If a prototype is found, or correctly demangled, the following is emitted:
301 .spec:
302 @ stdcall _OpenZipFile ZIPEXTRA__OpenZipFile
305 BOOL __stdcall ZIPEXTRA__OpenZipFile(LPCSTR pszFileName);
308 BOOL __stdcall ZIPEXTRA__OpenZipFile(LPCSTR pszFileName)
310   TRACE("stub");
311   return 0;
314 Note that if the prototype does not contain argument names, winedump will
315 add them following the convention arg0, arg1 ... argN. If the function is
316 demangled C++, the first argument will be called '_this' if an implicit this
317 pointer is passed (i.e. the function is a non-static class member function).
320 OPTION: -t       TRACE arguments (implies -c)
322 This option produces the same code as -c, except that arguments are printed
323 out when the function is called, so the FIXME in the above example becomes:
325   FIXME("(%s) stub", pszFileName);
327 Structs that are passed by value are printed as "struct", and functions
328 that take variable argument lists print "...".
331 OPTION: -f dll   Forward calls to 'dll' (implies -t)
333 This is the most complicated level of code generation. The same code is
334 generated as -t, however support is added for forwarding calls to another
335 DLL. The DLL to forward to is given as 'dll'. Lets suppose we built the
336 examples above using "-f real_zipextra". The code generated will look like
337 the following:
339 .spec
340 As for -c, except if a function prototype was not found:
342 @ forward _OpenZipFile real_zipextra._OpenZipFile
344 In this case the function is forwarded to the destination DLL rather
345 than stubbed.
348 As for -c.
352 A variable "hDLL" is added to hold a pointer to the DLL to forward to, and
353 the initialisation code in ZIPEXTRA_Init is changed to load and free the
354 forward DLL automatically:
356 HMODULE hDLL = 0; /* DLL to call through to */
358 BOOL WINAPI ZIPEXTRA_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
360     TRACE("(0x%08x, %ld, %p)\n", hinstDLL, fdwReason, lpvReserved);
362     if (fdwReason == DLL_PROCESS_ATTACH)
363     {
364         hDLL = LoadLibraryA( "real_zipextra" );
365         TRACE ("Forwarding DLL (real_zipextra) loaded\n" );
366     }
367     else if (fdwReason == DLL_PROCESS_DETACH)
368     {
369         FreeLibrary( hDLL );
370         TRACE ("Forwarding DLL (real_zipextra) freed\n" );
371     }
373     return TRUE;
376 The stub function is changed to call the forwarding DLL and return that value.
378 BOOL __stdcall ZIPEXTRA__OpenZipFile(LPCSTR pszFileName)
380   BOOL (__stdcall *pFunc)(LPCSTR) = (void*)GetProcAddress(hDLL,"_OpenZipFile");
381   BOOL retVal;
382   TRACE("((LPCSTR)%s) stub", pszFileName);
383   retVal = pFunc(pszFileName);
384   TRACE("returned (%ld)\n",(LONG)retVal));
385   return retVal;
388 This allows you to investigate the workings of a DLL without interfering in
389 its operation in any way (unless you want to).
391 In the example I have been using, we probably should have used the -o option
392 to change the ouput name of our DLL to something else, and used the -f
393 option to forward to the real zipextra DLL:
395 winedump spec zipextra -f zipextra -o myzipextra -I "~/zipextra/include/*h"
397 Then in the .spec file for our Winelib application, we add the line:
399 import myzipextra
401 When we build our application, winebuild resolves the calls to our Unix .so.
402 As our application runs we can see the values of all parameters passed to
403 the DLL, and any values returned, without having to write code to dump
404 them ourselves (see below for a better way to wrap a DLL for forwarding).
406 This isn't a very realistic example of the usefulness of this feature,
407 however, since we could print out the results anyway, because it is our
408 application making the calls to the DLL. Where DLL forwarding is most useful
409 is where an application or DLL we didn't write calls functions in the DLL.
410 In this case we can capture the sequence of calls made, and the values passed
411 around. This is an aid in reimplementing the DLL, since we can add code for a
412 function, print the results, and then call the real DLL and compare. Only
413 when our code is the same do we need to remove the function pointer and the
414 call to the real DLL. A similar feature in wine is +relay debugging. Using a
415 fowarding DLL allows more granular reporting of arguments, because you can
416 write code to dump out the contents of types/structures rather than just
417 their address in memory. A future version of winedump may generate this
418 code automatically for common Win32 types.
420 See below for more information on setting up a forwarding DLL.
423 Problems compiling a DLL containing generated code
424 --------------------------------------------------
426 Unless you are very lucky, you will need to do a small amount of work to
427 get a DLL generated with -c, -t or -f to compile. The reason for this is
428 that most DLLs will use custom types such as structs whose definition
429 is not known to the code in the DLL.
431 Heres an example prototype from crtdll:
433 double __cdecl _cabs(struct _complex arg0)
435 The definition for the _complex struct needs to be given. Since it is passed
436 by value, its size also needs to be correct in order to forward the call
437 correctly to a native DLL. In this case the structure is 8 bytes in size, which
438 means that the gcc compile flag -freg-struct-return must be given when
439 compiling the function in order to be compatable with the native DLL. (In 
440 general this is not an issue, but you need to be aware of such issues if you
441 encounter problems with your forwarding DLL).
443 For third party (non C++) DLL's, the header(s) supplied with the DLL  can
444 normally be added as an include to the generated DLL header. For other DLLs
445 I suggest creating a seperate header in the DLL directory and adding any
446 needed types to that. This allows you to rebuild the DLL at whim, for example
447 if a new version of winedump brings increased functionality, then you
448 only have to overwrite the generated files and re-include the header to take
449 advantage of it.
451 Usually there isn't much work to do to get the DLL to compile if you have
452 headers. As an example, building a forwarded crtdll, which contains 520
453 functions, required 20 types to be defined before it compiled. Of these,
454 about half were structures, so about 35 lines of code were needed. The only
455 change to the generated code was one line in the header to include the type
456 definitions.
458 To save some typing in case you don't have headers for your DLL type, winedump
459 will dump dummy declarations for unknown classes and types it encounters,
460 if you use the -v option. These can be piped directly into a fix-up header
461 file for use in compiling your DLL. For example, if winedump encounters the
462 (C++ ) symbol:
464 ??0foobar@@QAE@ABV0@@Z   (Which is a constructor for a foobar object)
466 It will emit the following with -v set:
468 struct foobar { int _FIXME; };
470 (Classes are mapped to C structs when generating code).
472 The output should be piped through 'sort' and 'uniq' to remove multiple
473 declarations, e.g:
475 winedump -d foo -c -I "inc/*.h" -v | grep FIXME | sort | uniq > fixup.h
477 By adding '#include "fixup.h"' to foobar_dll.h your compile errors will be
478 greatly reduced.
480 If winedump encounters a type it doesnt know that is passed by value (as in
481 the _cabs example above), it also prints a FIXME message like:
483 /* FIXME: By value type: Assumed 'int' */ typedef int ldiv_t;
485 If the type is not an int, you will need to change the code and possibly
486 the .spec entry in order to forward correctly. Otherwise, include the typedef
487 in your fixup header to avoid compile errors.
490 Using a forwarding DLL
491 ----------------------
493 To create and use a forwarding DLL to trace DLL calls, you need to first
494 create a DLL using the -f option as outlined above, and get it to compile.
495 In order to forward calls the following procedure can be used (for this
496 example we are going to build a forwarding msvcrt.dll for the purpose
497 of reimplementing it).
499 First we create the forwarding DLL. We will rename the real msvcrt.dll on our
500 system to ms_msvcrt.dll, and our msvcrt implementation will call it:
502 winedump spec msvcrt -C -f ms_msvcrt -I "inc/*.h"
504 We then install this DLL into the Wine tree and add the types we need to
505 make it compile. Once the DLL compiles, we create a dummy ms_msvcrt DLL so
506 winebuild will resolve our forward calls to it (for the cases where winedump
507 couldn't generate code and has placed an '@forward' line in the .spec file):
509 winedump spec msvcrt -C -o ms_msvcrt
511 Install this DLL into the wine tree (since its a stub DLL, no changes are
512 needed to the code).
514 Now uncomment the line that winedump inserted into msvcrt.spec:
516 #inport ms_msvcrt.dll
518 And recompile Wine.
520 Finally, we must tell Wine to only use the builtin msvcrt.dll and to only use
521 the native (Win32) ms_msvcrt.dll. Add the following two lines to ~/.wine/config
522 under the [DllOverrides] section:
524 ;Use our implmentation of msvcrt
525 "msvcrt" = "builtin, so"
526 ;Use only the Win32 ms_msvcrt
527 "ms_msvcrt" = "native"
529 At this point, when any call is made to msvcrt.dll, Our libmsvcrt.so recieves
530 the call. It then forwards or calls ms_msvcrt.dll, which is the native dll. We
531 recieve a return value and pass it back to our caller, having TRACEd the
532 arguments on the way.
534 At this point you are ready to start reimplementing the calls.
537 Final comments
538 --------------
540 If you have any suggestions for improving this tool, please let me know.
541 If anyone can help answer the FIXME questions in msmangle.c or can fill me in
542 on any aspect of the C++ mangling scheme, I would appreciate it. In particular
543 I want to know what _E and _G represent.
545 If you encounter a C++ symbol that doesn't demangle **AND** you have the
546 prototype for it, please send me the symbol as reported by winedump and the
547 prototype. The more examples I have the easier it is to decypher the scheme,
548 and generating them myself is very slow.
550 Finally, although it is easy to generate a DLL, I _very strongly_ suggest that
551 you dont submit a generated DLL for inclusion into Wine unless you have
552 actually implemented a fairly reasonable portion of it. Even then, you should
553 only send the portions of the DLL you have implemented. Thousands of lines of
554 stub code don't help the project at all.
556 Please send questions and bug reports to jon_p_griffiths@yahoo.com.
559 References
560 ----------
562 [1] See the Wine and Wine.conf man pages for details on how to tell Wine
563     whether to use native (Win32) or internal DLLs.
566 Demangling
567 ----------
569 If you need to demangle a single C++ symbol, you can use the demangling mode
570 of winedump. This is useful for testing the demangler or implementing
571 C++ functions in partially implemented wine DLLS. As an example:
573 winedump sym "??3@YAXPAX@Z"
575 Gives:
577 void __cdecl _global_operator_delete_1(void * arg0)
579 Which is enough information to begin implementing the function.
582 Dumping
583 -------
585 Another tool might be helpful digging into a 32bit DLL (and any PE image file):
586 pedump.
588 Usage:
589    -h           Display this help message
590    -d <dll>     Use dll for input file and generate implementation code
591    -C           Turns on symbol demangling
592    -f           Dumps file header information
593    -j dir_name  Dumps only the content of directory dir_name (import, export, debug)
594    -x           Dumps everything
596 The basic usage, to look everything in a file is:
597 winedump dump -d mydll.dll -x
599 It'll print any available information on the file. This information can be splitted
600 into sub-categories:
601 - file hedaers (request by -f or -x) are made of the standard PE header structures, 
602   plus the COFF sections
603 - directories: you can print them one after the other using the -j switch. Currently, 
604   only the import, export and debug directories are implemented.
605 - -x displays the file headers and any available directory.