Wed Sep 11 12:53:28 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / pedump.c
1 /*
2  * pedump.c: Dumps the contents of an extended PE/COFF file
3  *
4  * Author:
5  *   Miguel de Icaza (miguel@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9 #include <config.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "image.h"
14 #include <glib.h>
15 #include "cil-coff.h"
16 #include "private.h"
17 #include "mono-endian.h"
18 #include "verify.h"
19 #include <mono/metadata/class.h>
20 #include <mono/metadata/debug-helpers.h>
21 #include <mono/metadata/tokentype.h>
22 #include <mono/metadata/appdomain.h>
23 #include <mono/metadata/assembly.h>
24
25 gboolean dump_data = TRUE;
26 gboolean verify_pe = FALSE;
27
28 /* unused
29 static void
30 hex_dump (const char *buffer, int base, int count)
31 {
32         int i;
33         
34         for (i = 0; i < count; i++){
35                 if ((i % 16) == 0)
36                         printf ("\n0x%08x: ", (unsigned char) base + i);
37
38                 printf ("%02x ", (unsigned char) (buffer [i]));
39         }
40 }
41 */
42
43 static void
44 hex8 (const char *label, unsigned char x)
45 {
46         printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
47 }
48
49 static void
50 hex16 (const char *label, guint16 x)
51 {
52         printf ("\t%s: 0x%04x\n", label, x);
53 }
54
55 static void
56 hex32 (const char *label, guint32 x)
57 {
58         printf ("\t%s: 0x%08x\n", label, x);
59 }
60
61 static void
62 dump_coff_header (MonoCOFFHeader *coff)
63 {
64         printf ("\nCOFF Header:\n");
65         hex16 ("                Machine", coff->coff_machine);
66         hex16 ("               Sections", coff->coff_sections);
67         hex32 ("             Time stamp", coff->coff_time);
68         hex32 ("Pointer to Symbol Table", coff->coff_symptr);
69         hex32 ("           Symbol Count", coff->coff_symcount);
70         hex16 ("   Optional Header Size", coff->coff_opt_header_size);
71         hex16 ("        Characteristics", coff->coff_attributes);
72
73 }
74
75 static void
76 dump_pe_header (MonoPEHeader *pe)
77 {
78         printf ("\nPE Header:\n");
79         hex16 ("         Magic (0x010b)", pe->pe_magic);
80         hex8  ("             LMajor (6)", pe->pe_major);
81         hex8  ("             LMinor (0)", pe->pe_minor);
82         hex32 ("              Code Size", pe->pe_code_size);
83         hex32 ("  Initialized Data Size", pe->pe_data_size);
84         hex32 ("Uninitialized Data Size", pe->pe_uninit_data_size);
85         hex32 ("        Entry Point RVA", pe->pe_rva_entry_point);
86         hex32 ("          Code Base RVA", pe->pe_rva_code_base);
87         hex32 ("          Data Base RVA", pe->pe_rva_data_base);
88         printf ("\n");
89 }
90
91 static void
92 dump_nt_header (MonoPEHeaderNT *nt)
93 {
94         printf ("\nNT Header:\n");
95
96         hex32 ("   Image Base (0x400000)", nt->pe_image_base);
97         hex32 ("Section Alignment (8192)", nt->pe_section_align);
98         hex32 ("   File Align (512/4096)", nt->pe_file_alignment);
99         hex16 ("            OS Major (4)", nt->pe_os_major);
100         hex16 ("            OS Minor (0)", nt->pe_os_minor);
101         hex16 ("          User Major (0)", nt->pe_user_major);
102         hex16 ("          User Minor (0)", nt->pe_user_minor);
103         hex16 ("        Subsys major (4)", nt->pe_subsys_major);
104         hex16 ("        Subsys minor (0)", nt->pe_subsys_minor);
105         hex32 ("               Reserverd", nt->pe_reserved_1);
106         hex32 ("              Image Size", nt->pe_image_size);
107         hex32 ("             Header Size", nt->pe_header_size);
108         hex32 ("            Checksum (0)", nt->pe_checksum);
109         hex16 ("               Subsystem", nt->pe_subsys_required);
110         hex16 ("           DLL Flags (0)", nt->pe_dll_flags);
111         hex32 (" Stack Reserve Size (1M)", nt->pe_stack_reserve);
112         hex32 ("Stack commit Size (4096)", nt->pe_stack_commit);
113         hex32 ("  Heap Reserve Size (1M)", nt->pe_heap_reserve);
114         hex32 (" Heap Commit Size (4096)", nt->pe_heap_commit);
115         hex32 ("      Loader flags (0x1)", nt->pe_loader_flags);
116         hex32 ("   Data Directories (16)", nt->pe_data_dir_count);
117 }
118
119 static void
120 dent (const char *label, MonoPEDirEntry de)
121 {
122         printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
123 }
124
125 static void
126 dump_datadir (MonoPEDatadir *dd)
127 {
128         printf ("\nData directories:\n");
129         dent ("     Export Table", dd->pe_export_table);
130         dent ("     Import Table", dd->pe_import_table);
131         dent ("   Resource Table", dd->pe_resource_table);
132         dent ("  Exception Table", dd->pe_exception_table);
133         dent ("Certificate Table", dd->pe_certificate_table);
134         dent ("      Reloc Table", dd->pe_reloc_table);
135         dent ("            Debug", dd->pe_debug);
136         dent ("        Copyright", dd->pe_copyright);
137         dent ("       Global Ptr", dd->pe_global_ptr);
138         dent ("        TLS Table", dd->pe_tls_table);
139         dent ("Load Config Table", dd->pe_load_config_table);
140         dent ("     Bound Import", dd->pe_bound_import);
141         dent ("              IAT", dd->pe_iat);
142         dent ("Delay Import Desc", dd->pe_delay_import_desc);
143         dent ("       CLI Header", dd->pe_cli_header);
144 }
145
146 static void
147 dump_dotnet_header (MonoDotNetHeader *header)
148 {
149         dump_coff_header (&header->coff);
150         dump_pe_header (&header->pe);
151         dump_nt_header (&header->nt);
152         dump_datadir (&header->datadir);
153 }
154
155 static void
156 dump_section_table (MonoSectionTable *st)
157 {
158         guint32 flags = st->st_flags;
159                 
160         printf ("\n\tName: %s\n", st->st_name);
161         hex32 ("   Virtual Size", st->st_virtual_size);
162         hex32 ("Virtual Address", st->st_virtual_address);
163         hex32 ("  Raw Data Size", st->st_raw_data_size);
164         hex32 ("   Raw Data Ptr", st->st_raw_data_ptr);
165         hex32 ("      Reloc Ptr", st->st_reloc_ptr);
166         hex32 ("     LineNo Ptr", st->st_lineno_ptr);
167         hex16 ("    Reloc Count", st->st_reloc_count);
168         hex16 ("     Line Count", st->st_line_count);
169
170         printf ("\tFlags: %s%s%s%s%s%s%s%s%s%s\n",
171                 (flags & SECT_FLAGS_HAS_CODE) ? "code, " : "",
172                 (flags & SECT_FLAGS_HAS_INITIALIZED_DATA) ? "data, " : "",
173                 (flags & SECT_FLAGS_HAS_UNINITIALIZED_DATA) ? "bss, " : "",
174                 (flags & SECT_FLAGS_MEM_DISCARDABLE) ? "discard, " : "",
175                 (flags & SECT_FLAGS_MEM_NOT_CACHED) ? "nocache, " : "",
176                 (flags & SECT_FLAGS_MEM_NOT_PAGED) ? "nopage, " : "",
177                 (flags & SECT_FLAGS_MEM_SHARED) ? "shared, " : "",
178                 (flags & SECT_FLAGS_MEM_EXECUTE) ? "exec, " : "",
179                 (flags & SECT_FLAGS_MEM_READ) ? "read, " : "",
180                 (flags & SECT_FLAGS_MEM_WRITE) ? "write" : "");
181 }
182
183 static void
184 dump_sections (MonoCLIImageInfo *iinfo)
185 {
186         const int top = iinfo->cli_header.coff.coff_sections;
187         int i;
188         
189         for (i = 0; i < top; i++)
190                 dump_section_table (&iinfo->cli_section_tables [i]);
191 }
192
193 static void
194 dump_cli_header (MonoCLIHeader *ch)
195 {
196         printf ("\n");
197         printf ("          CLI header size: %d\n", ch->ch_size);
198         printf ("         Runtime required: %d.%d\n", ch->ch_runtime_major, ch->ch_runtime_minor);
199         printf ("                    Flags: %s, %s, %s\n",
200                 (ch->ch_flags & CLI_FLAGS_ILONLY ? "ilonly" : "contains native"),
201                 (ch->ch_flags & CLI_FLAGS_32BITREQUIRED ? "32bits" : "32/64"),
202                 (ch->ch_flags & CLI_FLAGS_ILONLY ? "trackdebug" : "no-trackdebug"));
203         dent   ("         Metadata", ch->ch_metadata);
204         hex32  ("Entry Point Token", ch->ch_entry_point);
205         dent   ("     Resources at", ch->ch_resources);
206         dent   ("   Strong Name at", ch->ch_strong_name);
207         dent   ("  Code Manager at", ch->ch_code_manager_table);
208         dent   ("  VTableFixups at", ch->ch_vtable_fixups);
209         dent   ("     EAT jumps at", ch->ch_export_address_table_jumps);
210 }       
211
212 static void
213 dsh (const char *label, MonoImage *meta, MonoStreamHeader *sh)
214 {
215         printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
216                 label,
217                 sh->data - meta->raw_metadata, sh->data + sh->size - meta->raw_metadata,
218                 sh->size, sh->size);
219 }
220
221 static void
222 dump_metadata_ptrs (MonoImage *meta)
223 {
224         printf ("\nMetadata pointers:\n");
225         dsh ("\tTables (#~)", meta, &meta->heap_tables);
226         dsh ("\t    Strings", meta, &meta->heap_strings);
227         dsh ("\t       Blob", meta, &meta->heap_blob);
228         dsh ("\tUser string", meta, &meta->heap_us);
229         dsh ("\t       GUID", meta, &meta->heap_guid);
230 }
231
232 static void
233 dump_metadata (MonoImage *meta)
234 {
235         int table;
236         
237         dump_metadata_ptrs (meta);
238
239         printf ("Rows:\n");
240         for (table = 0; table < 64; table++){
241                 if (meta->tables [table].rows == 0)
242                         continue;
243                 printf ("Table %s: %d records (%d bytes, at %p)\n",
244                         mono_meta_table_name (table),
245                         meta->tables [table].rows,
246                         meta->tables [table].row_size,
247                         meta->tables [table].base
248                         );
249         }
250 }
251
252 static void
253 dump_methoddef (MonoImage *metadata, guint32 token)
254 {
255         const char *loc;
256
257         if (!token)
258                 return;
259         loc = mono_metadata_locate_token (metadata, token);
260
261         printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
262 }
263
264 static void
265 dump_dotnet_iinfo (MonoImage *image)
266 {
267         MonoCLIImageInfo *iinfo = image->image_info;
268
269         dump_dotnet_header (&iinfo->cli_header);
270         dump_sections (iinfo);
271         dump_cli_header (&iinfo->cli_cli_header);
272         dump_metadata (image);
273
274         dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
275 }
276
277 static void
278 dump_verify_info (MonoImage *image, int flags)
279 {
280         GSList *errors, *tmp;
281         int count = 0;
282         const char* desc [] = {
283                 "Ok", "Error", "Warning", NULL, "CLS"
284         };
285
286         errors = mono_image_verify_tables (image, flags);
287
288         for (tmp = errors; tmp; tmp = tmp->next) {
289                 MonoVerifyInfo *info = tmp->data;
290                 g_print ("%s: %s\n", desc [info->status], info->message);
291                 if (info->status == MONO_VERIFY_ERROR)
292                         count++;
293         }
294         mono_free_verify_list (errors);
295
296         if (flags & (MONO_VERIFY_ALL + 1)) { /* verify code */
297                 int i;
298                 MonoTableInfo *m = &image->tables [MONO_TABLE_METHOD];
299
300                 for (i = 0; i < m->rows; ++i) {
301                         MonoMethod *method;
302
303                         method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i+1), NULL);
304                         errors = mono_method_verify (method, flags);
305                         if (errors) {
306                                 char *sig;
307                                 sig = mono_signature_get_desc (method->signature, FALSE);
308                                 g_print ("In method: %s.%s::%s(%s)\n", method->klass->name_space, method->klass->name, method->name, sig);
309                                 g_free (sig);
310                         }
311
312                         for (tmp = errors; tmp; tmp = tmp->next) {
313                                 MonoVerifyInfo *info = tmp->data;
314                                 g_print ("%s: %s\n", desc [info->status], info->message);
315                                 if (info->status == MONO_VERIFY_ERROR)
316                                         count++;
317                         }
318                         mono_free_verify_list (errors);
319                 }
320         }
321
322         if (count)
323                 g_print ("Error count: %d\n", count);
324 }
325
326 static void
327 usage (void)
328 {
329         printf ("Usage is: pedump [--verify error,warn,cls,all,code] file.exe\n");
330         exit (1);
331 }
332
333 int
334 main (int argc, char *argv [])
335 {
336         MonoImage *image;
337         char *file = NULL;
338         char *flags = NULL;
339         const char *flag_desc [] = {"error", "warn", "cls", "all", "code", NULL};
340         guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL, MONO_VERIFY_ALL + 1};
341         int i;
342         
343         for (i = 1; i < argc; i++){
344                 if (argv [i][0] != '-'){
345                         file = argv [i];
346                         continue;
347                 }
348
349                 if (strcmp (argv [i], "--help") == 0)
350                         usage ();
351                 else if (strcmp (argv [i], "--verify") == 0) {
352                         verify_pe = 1;
353                         dump_data = 0;
354                         ++i;
355                         flags = argv [i];
356                 } else {
357                         usage ();
358                 }
359         }
360         
361         if (!file)
362                 usage ();
363
364         image = mono_image_open (file, NULL);
365         if (!image){
366                 fprintf (stderr, "Can not open image %s\n", file);
367                 exit (1);
368         }
369
370         if (dump_data)
371                 dump_dotnet_iinfo (image);
372         if (verify_pe) {
373                 int f = 0;
374                 char *tok = strtok (flags, ",");
375                 MonoAssembly *assembly;
376                 while (tok) {
377                         for (i = 0; flag_desc [i]; ++i) {
378                                 if (strcmp (tok, flag_desc [i]) == 0) {
379                                         f |= flag_vals [i];
380                                         break;
381                                 }
382                         }
383                         if (!flag_desc [i])
384                                 g_print ("Unknown verify flag %s\n", tok);
385                         tok = strtok (NULL, ",");
386                 }
387                 mono_init (file);
388                 assembly = mono_assembly_open (file, NULL);
389                 dump_verify_info (assembly->image, f);
390         }
391         
392         mono_image_close (image);
393         
394         return 0;
395 }
396