svn path=/trunk/mcs/; revision=104772
[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 "mono-endian.h"
17 #include "verify.h"
18 #include <mono/metadata/class.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/tokentype.h>
21 #include <mono/metadata/appdomain.h>
22 #include <mono/metadata/assembly.h>
23 #include <mono/metadata/metadata-internals.h>
24 #include <mono/metadata/rawbuffer.h>
25 #include <mono/metadata/class-internals.h>
26 #include <mono/metadata/verify-internals.h>
27 #include "mono/utils/mono-digest.h"
28
29 gboolean dump_data = TRUE;
30 gboolean verify_pe = FALSE;
31 gboolean verify_metadata = FALSE;
32 gboolean verify_code = FALSE;
33
34 /* unused
35 static void
36 hex_dump (const char *buffer, int base, int count)
37 {
38         int i;
39         
40         for (i = 0; i < count; i++){
41                 if ((i % 16) == 0)
42                         printf ("\n0x%08x: ", (unsigned char) base + i);
43
44                 printf ("%02x ", (unsigned char) (buffer [i]));
45         }
46 }
47 */
48
49 static void
50 hex8 (const char *label, unsigned char x)
51 {
52         printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
53 }
54
55 static void
56 hex16 (const char *label, guint16 x)
57 {
58         printf ("\t%s: 0x%04x\n", label, x);
59 }
60
61 static void
62 hex32 (const char *label, guint32 x)
63 {
64         printf ("\t%s: 0x%08x\n", label, x);
65 }
66
67 static void
68 dump_coff_header (MonoCOFFHeader *coff)
69 {
70         printf ("\nCOFF Header:\n");
71         hex16 ("                Machine", coff->coff_machine);
72         hex16 ("               Sections", coff->coff_sections);
73         hex32 ("             Time stamp", coff->coff_time);
74         hex32 ("Pointer to Symbol Table", coff->coff_symptr);
75         hex32 ("           Symbol Count", coff->coff_symcount);
76         hex16 ("   Optional Header Size", coff->coff_opt_header_size);
77         hex16 ("        Characteristics", coff->coff_attributes);
78
79 }
80
81 static void
82 dump_pe_header (MonoPEHeader *pe)
83 {
84         printf ("\nPE Header:\n");
85         hex16 ("         Magic (0x010b)", pe->pe_magic);
86         hex8  ("             LMajor (6)", pe->pe_major);
87         hex8  ("             LMinor (0)", pe->pe_minor);
88         hex32 ("              Code Size", pe->pe_code_size);
89         hex32 ("  Initialized Data Size", pe->pe_data_size);
90         hex32 ("Uninitialized Data Size", pe->pe_uninit_data_size);
91         hex32 ("        Entry Point RVA", pe->pe_rva_entry_point);
92         hex32 ("          Code Base RVA", pe->pe_rva_code_base);
93         hex32 ("          Data Base RVA", pe->pe_rva_data_base);
94         printf ("\n");
95 }
96
97 static void
98 dump_nt_header (MonoPEHeaderNT *nt)
99 {
100         printf ("\nNT Header:\n");
101
102         hex32 ("   Image Base (0x400000)", nt->pe_image_base);
103         hex32 ("Section Alignment (8192)", nt->pe_section_align);
104         hex32 ("   File Align (512/4096)", nt->pe_file_alignment);
105         hex16 ("            OS Major (4)", nt->pe_os_major);
106         hex16 ("            OS Minor (0)", nt->pe_os_minor);
107         hex16 ("          User Major (0)", nt->pe_user_major);
108         hex16 ("          User Minor (0)", nt->pe_user_minor);
109         hex16 ("        Subsys major (4)", nt->pe_subsys_major);
110         hex16 ("        Subsys minor (0)", nt->pe_subsys_minor);
111         hex32 ("               Reserverd", nt->pe_reserved_1);
112         hex32 ("              Image Size", nt->pe_image_size);
113         hex32 ("             Header Size", nt->pe_header_size);
114         hex32 ("            Checksum (0)", nt->pe_checksum);
115         hex16 ("               Subsystem", nt->pe_subsys_required);
116         hex16 ("           DLL Flags (0)", nt->pe_dll_flags);
117         hex32 (" Stack Reserve Size (1M)", nt->pe_stack_reserve);
118         hex32 ("Stack commit Size (4096)", nt->pe_stack_commit);
119         hex32 ("  Heap Reserve Size (1M)", nt->pe_heap_reserve);
120         hex32 (" Heap Commit Size (4096)", nt->pe_heap_commit);
121         hex32 ("      Loader flags (0x1)", nt->pe_loader_flags);
122         hex32 ("   Data Directories (16)", nt->pe_data_dir_count);
123 }
124
125 static void
126 dent (const char *label, MonoPEDirEntry de)
127 {
128         printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
129 }
130
131 static void
132 dump_blob (const char *desc, const char* p, guint32 size)
133 {
134         int i;
135
136         printf ("%s", desc);
137         if (!p) {
138                 printf (" none\n");
139                 return;
140         }
141
142         for (i = 0; i < size; ++i) {
143                 if (!(i % 16))
144                         printf ("\n\t");
145                 printf (" %02X", p [i] & 0xFF);
146         }
147         printf ("\n");
148 }
149
150 static void
151 dump_public_key (MonoImage *m)
152 {
153         guint32 size;
154         const char *p;
155
156         p = mono_image_get_public_key (m, &size);
157         dump_blob ("\nPublic key:", p, size);
158 }
159
160 static void
161 dump_strong_name (MonoImage *m)
162 {
163         guint32 size;
164         const char *p;
165
166         p = mono_image_get_strong_name (m, &size);
167         dump_blob ("\nStrong name:", p, size);
168 }
169
170 static void
171 dump_datadir (MonoPEDatadir *dd)
172 {
173         printf ("\nData directories:\n");
174         dent ("     Export Table", dd->pe_export_table);
175         dent ("     Import Table", dd->pe_import_table);
176         dent ("   Resource Table", dd->pe_resource_table);
177         dent ("  Exception Table", dd->pe_exception_table);
178         dent ("Certificate Table", dd->pe_certificate_table);
179         dent ("      Reloc Table", dd->pe_reloc_table);
180         dent ("            Debug", dd->pe_debug);
181         dent ("        Copyright", dd->pe_copyright);
182         dent ("       Global Ptr", dd->pe_global_ptr);
183         dent ("        TLS Table", dd->pe_tls_table);
184         dent ("Load Config Table", dd->pe_load_config_table);
185         dent ("     Bound Import", dd->pe_bound_import);
186         dent ("              IAT", dd->pe_iat);
187         dent ("Delay Import Desc", dd->pe_delay_import_desc);
188         dent ("       CLI Header", dd->pe_cli_header);
189 }
190
191 static void
192 dump_dotnet_header (MonoDotNetHeader *header)
193 {
194         dump_coff_header (&header->coff);
195         dump_pe_header (&header->pe);
196         dump_nt_header (&header->nt);
197         dump_datadir (&header->datadir);
198 }
199
200 static void
201 dump_section_table (MonoSectionTable *st)
202 {
203         guint32 flags = st->st_flags;
204                 
205         printf ("\n\tName: %s\n", st->st_name);
206         hex32 ("   Virtual Size", st->st_virtual_size);
207         hex32 ("Virtual Address", st->st_virtual_address);
208         hex32 ("  Raw Data Size", st->st_raw_data_size);
209         hex32 ("   Raw Data Ptr", st->st_raw_data_ptr);
210         hex32 ("      Reloc Ptr", st->st_reloc_ptr);
211         hex32 ("     LineNo Ptr", st->st_lineno_ptr);
212         hex16 ("    Reloc Count", st->st_reloc_count);
213         hex16 ("     Line Count", st->st_line_count);
214
215         printf ("\tFlags: %s%s%s%s%s%s%s%s%s%s\n",
216                 (flags & SECT_FLAGS_HAS_CODE) ? "code, " : "",
217                 (flags & SECT_FLAGS_HAS_INITIALIZED_DATA) ? "data, " : "",
218                 (flags & SECT_FLAGS_HAS_UNINITIALIZED_DATA) ? "bss, " : "",
219                 (flags & SECT_FLAGS_MEM_DISCARDABLE) ? "discard, " : "",
220                 (flags & SECT_FLAGS_MEM_NOT_CACHED) ? "nocache, " : "",
221                 (flags & SECT_FLAGS_MEM_NOT_PAGED) ? "nopage, " : "",
222                 (flags & SECT_FLAGS_MEM_SHARED) ? "shared, " : "",
223                 (flags & SECT_FLAGS_MEM_EXECUTE) ? "exec, " : "",
224                 (flags & SECT_FLAGS_MEM_READ) ? "read, " : "",
225                 (flags & SECT_FLAGS_MEM_WRITE) ? "write" : "");
226 }
227
228 static void
229 dump_sections (MonoCLIImageInfo *iinfo)
230 {
231         const int top = iinfo->cli_header.coff.coff_sections;
232         int i;
233         
234         for (i = 0; i < top; i++)
235                 dump_section_table (&iinfo->cli_section_tables [i]);
236 }
237
238 static void
239 dump_cli_header (MonoCLIHeader *ch)
240 {
241         printf ("\n");
242         printf ("          CLI header size: %d\n", ch->ch_size);
243         printf ("         Runtime required: %d.%d\n", ch->ch_runtime_major, ch->ch_runtime_minor);
244         printf ("                    Flags: %s, %s, %s, %s\n",
245                 (ch->ch_flags & CLI_FLAGS_ILONLY ? "ilonly" : "contains native"),
246                 (ch->ch_flags & CLI_FLAGS_32BITREQUIRED ? "32bits" : "32/64"),
247                 (ch->ch_flags & CLI_FLAGS_ILONLY ? "trackdebug" : "no-trackdebug"),
248                 (ch->ch_flags & CLI_FLAGS_STRONGNAMESIGNED ? "strongnamesigned" : "notsigned"));
249         dent   ("         Metadata", ch->ch_metadata);
250         hex32  ("Entry Point Token", ch->ch_entry_point);
251         dent   ("     Resources at", ch->ch_resources);
252         dent   ("   Strong Name at", ch->ch_strong_name);
253         dent   ("  Code Manager at", ch->ch_code_manager_table);
254         dent   ("  VTableFixups at", ch->ch_vtable_fixups);
255         dent   ("     EAT jumps at", ch->ch_export_address_table_jumps);
256 }       
257
258 static void
259 dsh (const char *label, MonoImage *meta, MonoStreamHeader *sh)
260 {
261         printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
262                 label,
263                 (int)(sh->data - meta->raw_metadata), (int)(sh->data + sh->size - meta->raw_metadata),
264                 sh->size, sh->size);
265 }
266
267 static void
268 dump_metadata_header (MonoImage *meta)
269 {
270         printf ("\nMetadata header:\n");
271         printf ("           Version: %d.%d\n", meta->md_version_major, meta->md_version_minor);
272         printf ("    Version string: %s\n", meta->version);
273 }
274
275 static void
276 dump_metadata_ptrs (MonoImage *meta)
277 {
278         printf ("\nMetadata pointers:\n");
279         dsh ("\tTables (#~)", meta, &meta->heap_tables);
280         dsh ("\t    Strings", meta, &meta->heap_strings);
281         dsh ("\t       Blob", meta, &meta->heap_blob);
282         dsh ("\tUser string", meta, &meta->heap_us);
283         dsh ("\t       GUID", meta, &meta->heap_guid);
284 }
285
286 static void
287 dump_metadata (MonoImage *meta)
288 {
289         int table;
290
291         dump_metadata_header (meta);
292
293         dump_metadata_ptrs (meta);
294
295         printf ("Rows:\n");
296         for (table = 0; table < MONO_TABLE_NUM; table++){
297                 if (meta->tables [table].rows == 0)
298                         continue;
299                 printf ("Table %s: %d records (%d bytes, at %p)\n",
300                         mono_meta_table_name (table),
301                         meta->tables [table].rows,
302                         meta->tables [table].row_size,
303                         meta->tables [table].base
304                         );
305         }
306 }
307
308 static void
309 dump_methoddef (MonoImage *metadata, guint32 token)
310 {
311         const char *loc;
312
313         if (!token)
314                 return;
315         loc = mono_metadata_locate_token (metadata, token);
316
317         printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
318 }
319
320 static void
321 dump_dotnet_iinfo (MonoImage *image)
322 {
323         MonoCLIImageInfo *iinfo = image->image_info;
324
325         dump_dotnet_header (&iinfo->cli_header);
326         dump_sections (iinfo);
327         dump_cli_header (&iinfo->cli_cli_header);
328         dump_strong_name (image);
329         dump_public_key (image);
330         dump_metadata (image);
331
332         dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
333 }
334
335 static int
336 dump_verify_info (MonoImage *image, int flags)
337 {
338         GSList *errors, *tmp;
339         int count = 0, verifiable = 0;
340         const char* desc [] = {
341                 "Ok", "Error", "Warning", NULL, "CLS", NULL, NULL, NULL, "Not Verifiable"
342         };
343
344         if (verify_metadata) {
345                 errors = mono_image_verify_tables (image, flags);
346         
347                 for (tmp = errors; tmp; tmp = tmp->next) {
348                         MonoVerifyInfo *info = tmp->data;
349                         g_print ("%s: %s\n", desc [info->status], info->message);
350                         if (info->status == MONO_VERIFY_ERROR)
351                                 count++;
352                 }
353                 mono_free_verify_list (errors);
354         }
355
356         if (verify_code) { /* verify code */
357                 int i;
358                 MonoTableInfo *m = &image->tables [MONO_TABLE_METHOD];
359
360                 for (i = 0; i < m->rows; ++i) {
361                         MonoMethod *method;
362                         method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i+1), NULL);
363                         errors = mono_method_verify (method, flags);
364                         if (errors) {
365                                 char *sig;
366                                 MonoClass *klass = mono_method_get_class (method);
367                                 sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
368                                 //FIXME report the class name taking nesting into account
369                                 g_print ("In method: %s.%s::%s(%s)\n", mono_class_get_namespace (klass), mono_class_get_name (klass), mono_method_get_name (method), sig);
370                                 g_free (sig);
371                         }
372
373                         for (tmp = errors; tmp; tmp = tmp->next) {
374                                 MonoVerifyInfo *info = tmp->data;
375                                 g_print ("%s: %s\n", desc [info->status], info->message);
376                                 if (info->status == MONO_VERIFY_ERROR) {
377                                         count++;
378                                         verifiable = 3;
379                                 }
380                                 if(info->status == MONO_VERIFY_NOT_VERIFIABLE) {
381                                         if (verifiable < 2)
382                                                 verifiable = 2; 
383                                 }
384                         }
385                         mono_free_verify_list (errors);
386                 }
387         }
388
389         if (count)
390                 g_print ("Error count: %d\n", count);
391         return verifiable;
392 }
393
394 static void
395 usage (void)
396 {
397         printf ("Usage is: pedump [--verify error,warn,cls,all,code,fail-on-verifiable,non-strict,valid-only] file.exe\n");
398         exit (1);
399 }
400
401 #define VALID_ONLY_FLAG 0x08000000
402 #define VERIFY_CODE_ONLY MONO_VERIFY_ALL + 1 
403 int
404 main (int argc, char *argv [])
405 {
406         MonoImage *image;
407         char *file = NULL;
408         char *flags = NULL;
409         MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_VERIFIABLE;
410         const char *flag_desc [] = {"error", "warn", "cls", "all", "code", "fail-on-verifiable", "non-strict", "valid-only", NULL};
411         guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL, VERIFY_CODE_ONLY, MONO_VERIFY_FAIL_FAST, MONO_VERIFY_NON_STRICT, VALID_ONLY_FLAG, 0};
412         int i;
413         
414         for (i = 1; i < argc; i++){
415                 if (argv [i][0] != '-'){
416                         file = argv [i];
417                         continue;
418                 }
419
420                 if (strcmp (argv [i], "--help") == 0)
421                         usage ();
422                 else if (strcmp (argv [i], "--verify") == 0) {
423                         verify_pe = 1;
424                         dump_data = 0;
425                         ++i;
426                         flags = argv [i];
427                 } else {
428                         usage ();
429                 }
430         }
431         
432         if (!file)
433                 usage ();
434
435         mono_metadata_init ();
436         mono_raw_buffer_init ();
437         mono_images_init ();
438         mono_assemblies_init ();
439         mono_loader_init ();
440  
441         image = mono_image_open (file, NULL);
442         if (!image){
443                 fprintf (stderr, "Can not open image %s\n", file);
444                 exit (1);
445         }
446
447         if (dump_data)
448                 dump_dotnet_iinfo (image);
449         if (verify_pe) {
450                 int f = MONO_VERIFY_REPORT_ALL_ERRORS;
451                 char *tok = strtok (flags, ",");
452                 MonoAssembly *assembly;
453                 verify_metadata = 1;
454                 verify_code = 0;
455                 while (tok) {
456                         for (i = 0; flag_desc [i]; ++i) {
457                                 if (strcmp (tok, flag_desc [i]) == 0) {
458                                         if (flag_vals [i] == VERIFY_CODE_ONLY) {
459                                                 verify_metadata = 0;
460                                                 verify_code = 1;
461                                         } else if(flag_vals [i] == MONO_VERIFY_ALL)
462                                                 verify_code = 1;
463                                         if (flag_vals [i] == VALID_ONLY_FLAG)
464                                                 verifier_mode = MONO_VERIFIER_MODE_VALID;
465                                         else
466                                                 f |= flag_vals [i];
467                                         break;
468                                 }
469                         }
470                         if (!flag_desc [i])
471                                 g_print ("Unknown verify flag %s\n", tok);
472                         tok = strtok (NULL, ",");
473                 }
474
475                 mono_verifier_set_mode (verifier_mode);
476                 mono_init_from_assembly (file, file);
477                 assembly = mono_assembly_open (file, NULL);
478
479                 if (!assembly) {
480                         g_print ("Could not open assembly %s\n", file);
481                         return 4;
482                 }
483
484                 return dump_verify_info (assembly->image, f);
485         } else
486                 mono_image_close (image);
487         
488         return 0;
489 }
490