winedump: Larger usage of symbol demangling while dumping.
[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 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
52 #include "windef.h"
53 #include "winbase.h"
54
55 /* Argument type constants */
56 #define MAX_FUNCTION_ARGS   32
57
58 #define ARG_VOID            0x0
59 #define ARG_STRING          0x1
60 #define ARG_WIDE_STRING     0x2
61 #define ARG_POINTER         0x3
62 #define ARG_LONG            0x4
63 #define ARG_DOUBLE          0x5
64 #define ARG_STRUCT          0x6 /* By value */
65 #define ARG_FLOAT           0x7
66 #define ARG_VARARGS         0x8
67
68 /* Compound type flags */
69 #define CT_BY_REFERENCE     0x1
70 #define CT_VOLATILE         0x2
71 #define CT_CONST            0x4
72 #define CT_EXTENDED         0x8
73
74 /* symbol flags */
75 #define SYM_CDECL           0x1
76 #define SYM_STDCALL         0x2
77 #define SYM_THISCALL        0x4
78 #define SYM_DATA            0x8 /* Data, not a function */
79
80 typedef enum {NONE, DMGL, SPEC, DUMP} Mode;
81
82 /* Structure holding a parsed symbol */
83 typedef struct __parsed_symbol
84 {
85   char *symbol;
86   int   ordinal;
87   char *return_text;
88   char  return_type;
89   char *function_name;
90   int varargs;
91   unsigned int argc;
92   unsigned int flags;
93   char  arg_type [MAX_FUNCTION_ARGS];
94   char  arg_flag [MAX_FUNCTION_ARGS];
95   char *arg_text [MAX_FUNCTION_ARGS];
96   char *arg_name [MAX_FUNCTION_ARGS];
97   unsigned int n_u_refs;
98   char *u_ref    [MAX_FUNCTION_ARGS];
99 } parsed_symbol;
100
101 /* FIXME: Replace with some hash such as GHashTable */
102 typedef struct __search_symbol
103 {
104   struct __search_symbol *next;
105   int found;
106   char symbolname[1];    /* static string, be ANSI C compliant by [1] */
107 } search_symbol;
108
109 /* All globals */
110 typedef struct __globals
111 {
112   Mode  mode;              /* SPEC, DEMANGLE or DUMP */
113
114   /* Options: generic */
115   int   do_quiet;          /* -q */
116   int   do_verbose;        /* -v */
117
118   /* Option arguments: generic */
119   const char *input_name;  /* */
120   const char *input_module; /* input module name generated after input_name according mode */
121
122   /* Options: spec mode */
123   int   do_code;           /* -c, -t, -f */
124   int   do_trace;          /* -t, -f */
125   int   do_cdecl;          /* -C */
126   int   do_documentation;  /* -D */
127
128   /* Options: dump mode */
129   int   do_demangle;        /* -d */
130   int   do_dumpheader;      /* -f */
131   int   do_dump_rawdata;    /* -x */
132   int   do_debug;           /* -G == 1, -g == 2 */
133
134   /* Option arguments: spec mode */
135   int   start_ordinal;     /* -s */
136   int   end_ordinal;       /* -e */
137   search_symbol *search_symbol; /* -S */
138   char *directory;         /* -I */
139   const char *forward_dll; /* -f */
140   const char *dll_name;    /* -o */
141   const char *uc_dll_name;       /* -o */
142
143   /* Option arguments: dump mode */
144   const char *dumpsect;    /* -j */
145
146   /* internal options */
147   int   do_ordinals;
148 } _globals;
149
150 extern _globals globals;
151
152 /* Names to use for output DLL */
153 #define OUTPUT_DLL_NAME \
154           (globals.dll_name ? globals.dll_name : (globals.input_module ? globals.input_module : globals.input_name))
155 #define OUTPUT_UC_DLL_NAME globals.uc_dll_name
156
157 /* Verbosity levels */
158 #define QUIET   (globals.do_quiet)
159 #define NORMAL  (!QUIET)
160 #define VERBOSE (globals.do_verbose)
161
162 /* Default calling convention */
163 #define CALLING_CONVENTION (globals.do_cdecl ? SYM_CDECL : SYM_STDCALL)
164
165 /* Image functions */
166 void    dump_file(const char* name);
167
168 /* DLL functions */
169 int   dll_open (const char *dll_name);
170
171 int   dll_next_symbol (parsed_symbol * sym);
172
173 /* Symbol functions */
174 int   symbol_init(parsed_symbol* symbol, const char* name);
175
176 int   symbol_demangle (parsed_symbol *symbol);
177
178 int   symbol_search (parsed_symbol *symbol);
179
180 void  symbol_clear(parsed_symbol *sym);
181
182 int   symbol_is_valid_c(const parsed_symbol *sym);
183
184 const char *symbol_get_call_convention(const parsed_symbol *sym);
185
186 const char *symbol_get_spec_type (const parsed_symbol *sym, size_t arg);
187
188 void  symbol_clean_string (const char *string);
189
190 int   symbol_get_type (const char *string);
191
192 /* Output functions */
193 void  output_spec_preamble (void);
194
195 void  output_spec_symbol (const parsed_symbol *sym);
196
197 void  output_header_preamble (void);
198
199 void  output_header_symbol (const parsed_symbol *sym);
200
201 void  output_c_preamble (void);
202
203 void  output_c_symbol (const parsed_symbol *sym);
204
205 void  output_prototype (FILE *file, const parsed_symbol *sym);
206
207 void  output_makefile (void);
208
209 /* Misc functions */
210 char *str_create (size_t num_str, ...);
211
212 char *str_create_num (size_t num_str, int num, ...);
213
214 char *str_substring(const char *start, const char *end);
215
216 char *str_replace (char *str, const char *oldstr, const char *newstr);
217
218 const char *str_match (const char *str, const char *match, int *found);
219
220 const char *str_find_set (const char *str, const char *findset);
221
222 char *str_toupper (char *str);
223
224 const char *get_machine_str(int mach);
225
226 /* file dumping functions */
227 enum FileSig {SIG_UNKNOWN, SIG_DOS, SIG_PE, SIG_DBG, SIG_PDB, SIG_NE, SIG_LE, SIG_MDMP, SIG_COFFLIB, SIG_LNK, SIG_EMF};
228
229 const void*     PRD(unsigned long prd, unsigned long len);
230 unsigned long   Offset(const void* ptr);
231
232 typedef void (*file_dumper)(void);
233 int             dump_analysis(const char*, file_dumper, enum FileSig);
234
235 void            dump_data( const unsigned char *ptr, unsigned int size, const char *prefix );
236 const char*     get_time_str( unsigned long );
237 unsigned int    strlenW( const unsigned short *str );
238 void            dump_unicode_str( const unsigned short *str, int len );
239 const char*     get_symbol_str(const char* symname);
240 void            dump_file_header(const IMAGE_FILE_HEADER *);
241 void            dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *, UINT);
242 void            dump_section(const IMAGE_SECTION_HEADER *);
243 char*           guid_to_string(const GUID* guid, char *str, size_t sz);
244
245 enum FileSig    get_kind_exec(void);
246 void            dos_dump( void );
247 void            pe_dump( void );
248 void            ne_dump( void );
249 void            le_dump( void );
250 enum FileSig    get_kind_mdmp(void);
251 void            mdmp_dump( void );
252 enum FileSig    get_kind_lib(void);
253 void            lib_dump( void );
254 enum FileSig    get_kind_dbg(void);
255 void            dbg_dump( void );
256 enum FileSig    get_kind_lnk(void);
257 void            lnk_dump( void );
258 enum FileSig    get_kind_emf(void);
259 void            emf_dump( void );
260 enum FileSig    get_kind_pdb(void);
261 void            pdb_dump(void);
262 int             codeview_dump_symbols(const void* root, unsigned long size);
263 int             codeview_dump_types_from_offsets(const void* table, const DWORD* offsets, unsigned num_types);
264 int             codeview_dump_types_from_block(const void* table, unsigned long len);
265
266 void            dump_stabs(const void* pv_stabs, unsigned szstabs, const char* stabstr, unsigned szstr);
267 void            dump_codeview(unsigned long ptr, unsigned long len);
268 void            dump_coff(unsigned long coffbase, unsigned long len, const void* sect_map);
269 void            dump_frame_pointer_omission(unsigned long base, unsigned long len);
270
271 FILE *open_file (const char *name, const char *ext, const char *mode);
272
273 #ifdef __GNUC__
274 void  do_usage (void) __attribute__ ((noreturn));
275 void  fatal (const char *message)  __attribute__ ((noreturn));
276 #else
277 void  do_usage (void);
278 void  fatal (const char *message);
279 #endif
280
281
282
283 #endif /* __WINE_WINEDUMP_H */