winedump: Use same scheme for dumping lnk files as the executables (through the PRD...
[wine] / tools / winedump / winedump.h
1 /*
2  *  Winedump - A Wine DLL tool
3  *
4  *  Copyright 2000 Jon Griffiths
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  *  References:
21  *  DLL symbol extraction based on file format from alib (anthonyw.cjb.net).
22  *
23  *  Option processing shamelessly cadged from winebuild.
24  *
25  *  All the cool functionality (prototyping, call tracing, forwarding)
26  *  relies on Patrik Stridvall's 'function_grep.pl' script to work.
27  *
28  *  http://msdn.microsoft.com/library/periodic/period96/msj/S330.htm
29  *  This article provides both a description and freely downloadable
30  *  implementation, in source code form, of how to extract symbols
31  *  from Win32 PE executables/DLLs.
32  *
33  *  http://www.kegel.com/mangle.html
34  *  Gives information on the name mangling scheme used by MS compilers,
35  *  used as the starting point for the code here. Contains a few
36  *  mistakes and some incorrect assumptions, but the lists of types
37  *  are pure gold.
38  */
39 #ifndef __WINE_WINEDUMP_H
40 #define __WINE_WINEDUMP_H
41
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <assert.h>
48 #include <stdarg.h>
49
50 /* Argument type constants */
51 #define MAX_FUNCTION_ARGS   32
52
53 #define ARG_VOID            0x0
54 #define ARG_STRING          0x1
55 #define ARG_WIDE_STRING     0x2
56 #define ARG_POINTER         0x3
57 #define ARG_LONG            0x4
58 #define ARG_DOUBLE          0x5
59 #define ARG_STRUCT          0x6 /* By value */
60 #define ARG_FLOAT           0x7
61 #define ARG_VARARGS         0x8
62
63 /* Compound type flags */
64 #define CT_BY_REFERENCE     0x1
65 #define CT_VOLATILE         0x2
66 #define CT_CONST            0x4
67 #define CT_EXTENDED         0x8
68
69 /* symbol flags */
70 #define SYM_CDECL           0x1
71 #define SYM_STDCALL         0x2
72 #define SYM_THISCALL        0x4
73 #define SYM_DATA            0x8 /* Data, not a function */
74
75 typedef enum {NONE, DMGL, SPEC, DUMP, EMF} Mode;
76
77 /* Structure holding a parsed symbol */
78 typedef struct __parsed_symbol
79 {
80   char *symbol;
81   int   ordinal;
82   char *return_text;
83   char  return_type;
84   char *function_name;
85   int varargs;
86   unsigned int argc;
87   unsigned int flags;
88   char  arg_type [MAX_FUNCTION_ARGS];
89   char  arg_flag [MAX_FUNCTION_ARGS];
90   char *arg_text [MAX_FUNCTION_ARGS];
91   char *arg_name [MAX_FUNCTION_ARGS];
92   unsigned int n_u_refs;
93   char *u_ref    [MAX_FUNCTION_ARGS];
94 } parsed_symbol;
95
96 /* FIXME: Replace with some hash such as GHashTable */
97 typedef struct __search_symbol
98 {
99   struct __search_symbol *next;
100   int found;
101   char symbolname[1];    /* static string, be ANSI C compliant by [1] */
102 } search_symbol;
103
104 /* All globals */
105 typedef struct __globals
106 {
107   Mode  mode;              /* SPEC, DEMANGLE or DUMP */
108
109   /* Options: generic */
110   int   do_quiet;          /* -q */
111   int   do_verbose;        /* -v */
112
113   /* Option arguments: generic */
114   const char *input_name;  /* */
115   const char *input_module; /* input module name generated after input_name according mode */
116
117   /* Options: spec mode */
118   int   do_code;           /* -c, -t, -f */
119   int   do_trace;          /* -t, -f */
120   int   do_cdecl;          /* -C */
121   int   do_documentation;  /* -D */
122
123   /* Options: dump mode */
124   int   do_demangle;        /* -d */
125   int   do_dumpheader;      /* -f */
126   int   do_debug;           /* -G == 1, -g == 2 */
127
128   /* Option arguments: spec mode */
129   int   start_ordinal;     /* -s */
130   int   end_ordinal;       /* -e */
131   search_symbol *search_symbol; /* -S */
132   char *directory;         /* -I */
133   const char *forward_dll; /* -f */
134   const char *dll_name;    /* -o */
135   const char *uc_dll_name;       /* -o */
136
137   /* Option arguments: dump mode */
138   const char *dumpsect;    /* -j */
139
140   /* internal options */
141   int   do_ordinals;
142 } _globals;
143
144 extern _globals globals;
145
146 /* Names to use for output DLL */
147 #define OUTPUT_DLL_NAME \
148           (globals.dll_name ? globals.dll_name : (globals.input_module ? globals.input_module : globals.input_name))
149 #define OUTPUT_UC_DLL_NAME globals.uc_dll_name
150
151 /* Verbosity levels */
152 #define QUIET   (globals.do_quiet)
153 #define NORMAL  (!QUIET)
154 #define VERBOSE (globals.do_verbose)
155
156 /* Default calling convention */
157 #define CALLING_CONVENTION (globals.do_cdecl ? SYM_CDECL : SYM_STDCALL)
158
159 /* EMF functions */
160 int   dump_emf (const char *emf);
161
162 /* Image functions */
163 void    dump_file(const char* name);
164
165 /* DLL functions */
166 int   dll_open (const char *dll_name);
167
168 int   dll_next_symbol (parsed_symbol * sym);
169
170 /* Symbol functions */
171 int   symbol_init(parsed_symbol* symbol, const char* name);
172
173 int   symbol_demangle (parsed_symbol *symbol);
174
175 int   symbol_search (parsed_symbol *symbol);
176
177 void  symbol_clear(parsed_symbol *sym);
178
179 int   symbol_is_valid_c(const parsed_symbol *sym);
180
181 const char *symbol_get_call_convention(const parsed_symbol *sym);
182
183 const char *symbol_get_spec_type (const parsed_symbol *sym, size_t arg);
184
185 void  symbol_clean_string (const char *string);
186
187 int   symbol_get_type (const char *string);
188
189 /* Output functions */
190 void  output_spec_preamble (void);
191
192 void  output_spec_symbol (const parsed_symbol *sym);
193
194 void  output_header_preamble (void);
195
196 void  output_header_symbol (const parsed_symbol *sym);
197
198 void  output_c_preamble (void);
199
200 void  output_c_symbol (const parsed_symbol *sym);
201
202 void  output_prototype (FILE *file, const parsed_symbol *sym);
203
204 void  output_makefile (void);
205
206 /* Misc functions */
207 char *str_create (size_t num_str, ...);
208
209 char *str_create_num (size_t num_str, int num, ...);
210
211 char *str_substring(const char *start, const char *end);
212
213 char *str_replace (char *str, const char *oldstr, const char *newstr);
214
215 const char *str_match (const char *str, const char *match, int *found);
216
217 const char *str_find_set (const char *str, const char *findset);
218
219 char *str_toupper (char *str);
220
221 const char *get_machine_str(int mach);
222
223 /* file dumping functions */
224 enum FileSig {SIG_UNKNOWN, SIG_DOS, SIG_PE, SIG_DBG, SIG_NE, SIG_LE, SIG_MDMP, SIG_COFFLIB, SIG_LNK};
225
226 const void*     PRD(unsigned long prd, unsigned long len);
227 unsigned long   Offset(const void* ptr);
228
229 typedef void (*file_dumper)(void);
230 int             dump_analysis(const char*, file_dumper, enum FileSig);
231
232 void            dump_data( const unsigned char *ptr, unsigned int size, const char *prefix );
233 const char*     get_time_str( unsigned long );
234 unsigned int    strlenW( const unsigned short *str );
235 void            dump_unicode_str( const unsigned short *str, int len );
236
237 enum FileSig    get_kind_exec(void);
238 void            pe_dump( void );
239 void            ne_dump( void );
240 void            le_dump( void );
241 enum FileSig    get_kind_mdmp(void);
242 void            mdmp_dump( void );
243 enum FileSig    get_kind_lib(void);
244 void            lib_dump( void );
245 enum FileSig    get_kind_dbg(void);
246 void            dbg_dump( void );
247 enum FileSig    get_kind_lnk(void);
248 void            lnk_dump( void );
249
250 void            dump_stabs(const void* pv_stabs, unsigned szstabs, const char* stabstr, unsigned szstr);
251 void            dump_codeview(unsigned long ptr, unsigned long len);
252 void            dump_coff(unsigned long coffbase, unsigned long len, const void* sect_map);
253 void            dump_frame_pointer_omission(unsigned long base, unsigned long len);
254
255 FILE *open_file (const char *name, const char *ext, const char *mode);
256
257 #ifdef __GNUC__
258 void  do_usage (void) __attribute__ ((noreturn));
259 void  fatal (const char *message)  __attribute__ ((noreturn));
260 #else
261 void  do_usage (void);
262 void  fatal (const char *message);
263 #endif
264
265
266
267 #endif /* __WINE_WINEDUMP_H */