2009-06-12 Bill Holmes <billholmes54@gmail.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  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9  */
10 #include <config.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include "image.h"
15 #include <glib.h>
16 #include "cil-coff.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 #include <mono/metadata/metadata-internals.h>
25 #include <mono/metadata/class-internals.h>
26 #include <mono/metadata/verify-internals.h>
27 #include <mono/metadata/marshal.h>
28 #include "mono/utils/mono-digest.h"
29 #include <mono/utils/mono-mmap.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36 gboolean dump_data = TRUE;
37 gboolean verify_pe = FALSE;
38 gboolean verify_metadata = FALSE;
39 gboolean verify_code = FALSE;
40
41 /* unused
42 static void
43 hex_dump (const char *buffer, int base, int count)
44 {
45         int i;
46         
47         for (i = 0; i < count; i++){
48                 if ((i % 16) == 0)
49                         printf ("\n0x%08x: ", (unsigned char) base + i);
50
51                 printf ("%02x ", (unsigned char) (buffer [i]));
52         }
53 }
54 */
55
56 static void
57 hex8 (const char *label, unsigned char x)
58 {
59         printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
60 }
61
62 static void
63 hex16 (const char *label, guint16 x)
64 {
65         printf ("\t%s: 0x%04x\n", label, x);
66 }
67
68 static void
69 hex32 (const char *label, guint32 x)
70 {
71         printf ("\t%s: 0x%08x\n", label, x);
72 }
73
74 static void
75 dump_coff_header (MonoCOFFHeader *coff)
76 {
77         printf ("\nCOFF Header:\n");
78         hex16 ("                Machine", coff->coff_machine);
79         hex16 ("               Sections", coff->coff_sections);
80         hex32 ("             Time stamp", coff->coff_time);
81         hex32 ("Pointer to Symbol Table", coff->coff_symptr);
82         hex32 ("           Symbol Count", coff->coff_symcount);
83         hex16 ("   Optional Header Size", coff->coff_opt_header_size);
84         hex16 ("        Characteristics", coff->coff_attributes);
85
86 }
87
88 static void
89 dump_pe_header (MonoPEHeader *pe)
90 {
91         printf ("\nPE Header:\n");
92         hex16 ("         Magic (0x010b)", pe->pe_magic);
93         hex8  ("             LMajor (6)", pe->pe_major);
94         hex8  ("             LMinor (0)", pe->pe_minor);
95         hex32 ("              Code Size", pe->pe_code_size);
96         hex32 ("  Initialized Data Size", pe->pe_data_size);
97         hex32 ("Uninitialized Data Size", pe->pe_uninit_data_size);
98         hex32 ("        Entry Point RVA", pe->pe_rva_entry_point);
99         hex32 ("          Code Base RVA", pe->pe_rva_code_base);
100         hex32 ("          Data Base RVA", pe->pe_rva_data_base);
101         printf ("\n");
102 }
103
104 static void
105 dump_nt_header (MonoPEHeaderNT *nt)
106 {
107         printf ("\nNT Header:\n");
108
109         hex32 ("   Image Base (0x400000)", nt->pe_image_base);
110         hex32 ("Section Alignment (8192)", nt->pe_section_align);
111         hex32 ("   File Align (512/4096)", nt->pe_file_alignment);
112         hex16 ("            OS Major (4)", nt->pe_os_major);
113         hex16 ("            OS Minor (0)", nt->pe_os_minor);
114         hex16 ("          User Major (0)", nt->pe_user_major);
115         hex16 ("          User Minor (0)", nt->pe_user_minor);
116         hex16 ("        Subsys major (4)", nt->pe_subsys_major);
117         hex16 ("        Subsys minor (0)", nt->pe_subsys_minor);
118         hex32 ("               Reserverd", nt->pe_reserved_1);
119         hex32 ("              Image Size", nt->pe_image_size);
120         hex32 ("             Header Size", nt->pe_header_size);
121         hex32 ("            Checksum (0)", nt->pe_checksum);
122         hex16 ("               Subsystem", nt->pe_subsys_required);
123         hex16 ("           DLL Flags (0)", nt->pe_dll_flags);
124         hex32 (" Stack Reserve Size (1M)", nt->pe_stack_reserve);
125         hex32 ("Stack commit Size (4096)", nt->pe_stack_commit);
126         hex32 ("  Heap Reserve Size (1M)", nt->pe_heap_reserve);
127         hex32 (" Heap Commit Size (4096)", nt->pe_heap_commit);
128         hex32 ("      Loader flags (0x1)", nt->pe_loader_flags);
129         hex32 ("   Data Directories (16)", nt->pe_data_dir_count);
130 }
131
132 static void
133 dent (const char *label, MonoPEDirEntry de)
134 {
135         printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
136 }
137
138 static void
139 dump_blob (const char *desc, const char* p, guint32 size)
140 {
141         int i;
142
143         printf ("%s", desc);
144         if (!p) {
145                 printf (" none\n");
146                 return;
147         }
148
149         for (i = 0; i < size; ++i) {
150                 if (!(i % 16))
151                         printf ("\n\t");
152                 printf (" %02X", p [i] & 0xFF);
153         }
154         printf ("\n");
155 }
156
157 static void
158 dump_public_key (MonoImage *m)
159 {
160         guint32 size;
161         const char *p;
162
163         p = mono_image_get_public_key (m, &size);
164         dump_blob ("\nPublic key:", p, size);
165 }
166
167 static void
168 dump_strong_name (MonoImage *m)
169 {
170         guint32 size;
171         const char *p;
172
173         p = mono_image_get_strong_name (m, &size);
174         dump_blob ("\nStrong name:", p, size);
175 }
176
177 static void
178 dump_datadir (MonoPEDatadir *dd)
179 {
180         printf ("\nData directories:\n");
181         dent ("     Export Table", dd->pe_export_table);
182         dent ("     Import Table", dd->pe_import_table);
183         dent ("   Resource Table", dd->pe_resource_table);
184         dent ("  Exception Table", dd->pe_exception_table);
185         dent ("Certificate Table", dd->pe_certificate_table);
186         dent ("      Reloc Table", dd->pe_reloc_table);
187         dent ("            Debug", dd->pe_debug);
188         dent ("        Copyright", dd->pe_copyright);
189         dent ("       Global Ptr", dd->pe_global_ptr);
190         dent ("        TLS Table", dd->pe_tls_table);
191         dent ("Load Config Table", dd->pe_load_config_table);
192         dent ("     Bound Import", dd->pe_bound_import);
193         dent ("              IAT", dd->pe_iat);
194         dent ("Delay Import Desc", dd->pe_delay_import_desc);
195         dent ("       CLI Header", dd->pe_cli_header);
196 }
197
198 static void
199 dump_dotnet_header (MonoDotNetHeader *header)
200 {
201         dump_coff_header (&header->coff);
202         dump_pe_header (&header->pe);
203         dump_nt_header (&header->nt);
204         dump_datadir (&header->datadir);
205 }
206
207 static void
208 dump_section_table (MonoSectionTable *st)
209 {
210         guint32 flags = st->st_flags;
211                 
212         printf ("\n\tName: %s\n", st->st_name);
213         hex32 ("   Virtual Size", st->st_virtual_size);
214         hex32 ("Virtual Address", st->st_virtual_address);
215         hex32 ("  Raw Data Size", st->st_raw_data_size);
216         hex32 ("   Raw Data Ptr", st->st_raw_data_ptr);
217         hex32 ("      Reloc Ptr", st->st_reloc_ptr);
218         hex32 ("     LineNo Ptr", st->st_lineno_ptr);
219         hex16 ("    Reloc Count", st->st_reloc_count);
220         hex16 ("     Line Count", st->st_line_count);
221
222         printf ("\tFlags: %s%s%s%s%s%s%s%s%s%s\n",
223                 (flags & SECT_FLAGS_HAS_CODE) ? "code, " : "",
224                 (flags & SECT_FLAGS_HAS_INITIALIZED_DATA) ? "data, " : "",
225                 (flags & SECT_FLAGS_HAS_UNINITIALIZED_DATA) ? "bss, " : "",
226                 (flags & SECT_FLAGS_MEM_DISCARDABLE) ? "discard, " : "",
227                 (flags & SECT_FLAGS_MEM_NOT_CACHED) ? "nocache, " : "",
228                 (flags & SECT_FLAGS_MEM_NOT_PAGED) ? "nopage, " : "",
229                 (flags & SECT_FLAGS_MEM_SHARED) ? "shared, " : "",
230                 (flags & SECT_FLAGS_MEM_EXECUTE) ? "exec, " : "",
231                 (flags & SECT_FLAGS_MEM_READ) ? "read, " : "",
232                 (flags & SECT_FLAGS_MEM_WRITE) ? "write" : "");
233 }
234
235 static void
236 dump_sections (MonoCLIImageInfo *iinfo)
237 {
238         const int top = iinfo->cli_header.coff.coff_sections;
239         int i;
240         
241         for (i = 0; i < top; i++)
242                 dump_section_table (&iinfo->cli_section_tables [i]);
243 }
244
245 static void
246 dump_cli_header (MonoCLIHeader *ch)
247 {
248         printf ("\n");
249         printf ("          CLI header size: %d\n", ch->ch_size);
250         printf ("         Runtime required: %d.%d\n", ch->ch_runtime_major, ch->ch_runtime_minor);
251         printf ("                    Flags: %s, %s, %s, %s\n",
252                 (ch->ch_flags & CLI_FLAGS_ILONLY ? "ilonly" : "contains native"),
253                 (ch->ch_flags & CLI_FLAGS_32BITREQUIRED ? "32bits" : "32/64"),
254                 (ch->ch_flags & CLI_FLAGS_TRACKDEBUGDATA ? "trackdebug" : "no-trackdebug"),
255                 (ch->ch_flags & CLI_FLAGS_STRONGNAMESIGNED ? "strongnamesigned" : "notsigned"));
256         dent   ("         Metadata", ch->ch_metadata);
257         hex32  ("Entry Point Token", ch->ch_entry_point);
258         dent   ("     Resources at", ch->ch_resources);
259         dent   ("   Strong Name at", ch->ch_strong_name);
260         dent   ("  Code Manager at", ch->ch_code_manager_table);
261         dent   ("  VTableFixups at", ch->ch_vtable_fixups);
262         dent   ("     EAT jumps at", ch->ch_export_address_table_jumps);
263 }       
264
265 static void
266 dsh (const char *label, MonoImage *meta, MonoStreamHeader *sh)
267 {
268         printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
269                 label,
270                 (int)(sh->data - meta->raw_metadata), (int)(sh->data + sh->size - meta->raw_metadata),
271                 sh->size, sh->size);
272 }
273
274 static void
275 dump_metadata_header (MonoImage *meta)
276 {
277         printf ("\nMetadata header:\n");
278         printf ("           Version: %d.%d\n", meta->md_version_major, meta->md_version_minor);
279         printf ("    Version string: %s\n", meta->version);
280 }
281
282 static void
283 dump_metadata_ptrs (MonoImage *meta)
284 {
285         printf ("\nMetadata pointers:\n");
286         dsh ("\tTables (#~)", meta, &meta->heap_tables);
287         dsh ("\t    Strings", meta, &meta->heap_strings);
288         dsh ("\t       Blob", meta, &meta->heap_blob);
289         dsh ("\tUser string", meta, &meta->heap_us);
290         dsh ("\t       GUID", meta, &meta->heap_guid);
291 }
292
293 static void
294 dump_metadata (MonoImage *meta)
295 {
296         int table;
297
298         dump_metadata_header (meta);
299
300         dump_metadata_ptrs (meta);
301
302         printf ("Rows:\n");
303         for (table = 0; table < MONO_TABLE_NUM; table++){
304                 if (meta->tables [table].rows == 0)
305                         continue;
306                 printf ("Table %s: %d records (%d bytes, at %x)\n",
307                         mono_meta_table_name (table),
308                         meta->tables [table].rows,
309                         meta->tables [table].row_size,
310                         (unsigned int)(meta->tables [table].base - meta->raw_data)
311                         );
312         }
313 }
314
315 static void
316 dump_methoddef (MonoImage *metadata, guint32 token)
317 {
318         const char *loc;
319
320         if (!token)
321                 return;
322         loc = mono_metadata_locate_token (metadata, token);
323
324         printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
325 }
326
327 static void
328 dump_dotnet_iinfo (MonoImage *image)
329 {
330         MonoCLIImageInfo *iinfo = image->image_info;
331
332         dump_dotnet_header (&iinfo->cli_header);
333         dump_sections (iinfo);
334         dump_cli_header (&iinfo->cli_cli_header);
335         dump_strong_name (image);
336         dump_public_key (image);
337         dump_metadata (image);
338
339         dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
340 }
341
342 static int
343 dump_verify_info (MonoImage *image, int flags)
344 {
345         GSList *errors, *tmp;
346         int count = 0, verifiable = 0;
347         const char* desc [] = {
348                 "Ok", "Error", "Warning", NULL, "CLS", NULL, NULL, NULL, "Not Verifiable"
349         };
350
351         if (verify_metadata) {
352                 errors = mono_image_verify_tables (image, flags);
353         
354                 for (tmp = errors; tmp; tmp = tmp->next) {
355                         MonoVerifyInfo *info = tmp->data;
356                         g_print ("%s: %s\n", desc [info->status], info->message);
357                         if (info->status == MONO_VERIFY_ERROR)
358                                 count++;
359                 }
360                 mono_free_verify_list (errors);
361         }
362
363         if (verify_code) { /* verify code */
364                 int i;
365                 MonoTableInfo *m = &image->tables [MONO_TABLE_METHOD];
366
367                 for (i = 0; i < m->rows; ++i) {
368                         MonoMethod *method;
369                         method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i+1), NULL);
370                         if (!method) {
371                                 g_print ("Warning: Cannot lookup method with token 0x%08x\n", i + 1);
372                                 continue;
373                         }
374                         errors = mono_method_verify (method, flags);
375                         if (errors) {
376                                 MonoClass *klass = mono_method_get_class (method);
377                                 char *name = mono_type_full_name (&klass->byval_arg);
378                                 if (mono_method_signature (method) == NULL) {
379                                         g_print ("In method: %s::%s(ERROR)\n", name, mono_method_get_name (method));
380                                 } else {
381                                         char *sig;
382                                         sig = mono_signature_get_desc (mono_method_signature (method), FALSE);  
383                                         g_print ("In method: %s::%s(%s)\n", name, mono_method_get_name (method), sig);
384                                         g_free (sig);
385                                 }
386                                 g_free (name);
387                         }
388
389                         for (tmp = errors; tmp; tmp = tmp->next) {
390                                 MonoVerifyInfo *info = tmp->data;
391                                 g_print ("%s: %s\n", desc [info->status], info->message);
392                                 if (info->status == MONO_VERIFY_ERROR) {
393                                         count++;
394                                         verifiable = 3;
395                                 }
396                                 if(info->status == MONO_VERIFY_NOT_VERIFIABLE) {
397                                         if (verifiable < 2)
398                                                 verifiable = 2; 
399                                 }
400                         }
401                         mono_free_verify_list (errors);
402                 }
403         }
404
405         if (count)
406                 g_print ("Error count: %d\n", count);
407         return verifiable;
408 }
409
410 static void
411 usage (void)
412 {
413         printf ("Usage is: pedump [--verify error,warn,cls,all,code,fail-on-verifiable,non-strict,valid-only,metadata] file.exe\n");
414         exit (1);
415 }
416
417 static int
418 verify_image_file (const char *fname)
419 {
420         GSList *errors = NULL, *tmp;
421         MonoImage *image;
422         MonoImageOpenStatus status;
423         int count = 0;
424         const char* desc [] = {
425                 "Ok", "Error", "Warning", NULL, "CLS", NULL, NULL, NULL, "Not Verifiable"
426         };
427
428         image = mono_image_open_raw (fname, &status);
429         if (!image) {
430                 printf ("Could not open %s\n", fname);
431                 return 1;
432         }
433
434         if (!mono_verifier_verify_pe_data (image, &errors))
435                 goto invalid_image;
436
437         if (!mono_image_load_pe_data (image)) {
438                 printf ("Could not load pe data for assembly %s\n", fname);
439                 return 1;
440         }
441
442         if (!mono_verifier_verify_cli_data (image, &errors))
443                 goto invalid_image;
444
445         if (!mono_image_load_cli_data (image)) {
446                 printf ("Could not load cli data for assembly %s\n", fname);
447                 return 1;
448         }
449
450         if (!mono_verifier_verify_table_data (image, &errors))
451                 goto invalid_image;
452
453         return 0;
454
455 invalid_image:
456         for (tmp = errors; tmp; tmp = tmp->next) {
457                 MonoVerifyInfo *info = tmp->data;
458                 g_print ("%s: %s\n", desc [info->status], info->message);
459                 if (info->status == MONO_VERIFY_ERROR)
460                         count++;
461         }
462         mono_free_verify_list (errors);
463         if (count)
464                 g_print ("Error count: %d\n", count);
465         return 1;
466 }
467
468 static gboolean
469 try_load_from (MonoAssembly **assembly, const gchar *path1, const gchar *path2,
470                                         const gchar *path3, const gchar *path4, gboolean refonly)
471 {
472         gchar *fullpath;
473
474         *assembly = NULL;
475         fullpath = g_build_filename (path1, path2, path3, path4, NULL);
476         if (g_file_test (fullpath, G_FILE_TEST_IS_REGULAR))
477                 *assembly = mono_assembly_open_full (fullpath, NULL, refonly);
478
479         g_free (fullpath);
480         return (*assembly != NULL);
481 }
482
483 static MonoAssembly *
484 real_load (gchar **search_path, const gchar *culture, const gchar *name, gboolean refonly)
485 {
486         MonoAssembly *result = NULL;
487         gchar **path;
488         gchar *filename;
489         const gchar *local_culture;
490         gint len;
491
492         if (!culture || *culture == '\0') {
493                 local_culture = "";
494         } else {
495                 local_culture = culture;
496         }
497
498         filename =  g_strconcat (name, ".dll", NULL);
499         len = strlen (filename);
500
501         for (path = search_path; *path; path++) {
502                 if (**path == '\0')
503                         continue; /* Ignore empty ApplicationBase */
504
505                 /* See test cases in bug #58992 and bug #57710 */
506                 /* 1st try: [culture]/[name].dll (culture may be empty) */
507                 strcpy (filename + len - 4, ".dll");
508                 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
509                         break;
510
511                 /* 2nd try: [culture]/[name].exe (culture may be empty) */
512                 strcpy (filename + len - 4, ".exe");
513                 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
514                         break;
515
516                 /* 3rd try: [culture]/[name]/[name].dll (culture may be empty) */
517                 strcpy (filename + len - 4, ".dll");
518                 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
519                         break;
520
521                 /* 4th try: [culture]/[name]/[name].exe (culture may be empty) */
522                 strcpy (filename + len - 4, ".exe");
523                 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
524                         break;
525         }
526
527         g_free (filename);
528         return result;
529 }
530
531 /*
532  * Try to load referenced assemblies from assemblies_path.
533  */
534 static MonoAssembly *
535 pedump_preload (MonoAssemblyName *aname,
536                                  gchar **assemblies_path,
537                                  gpointer user_data)
538 {
539         MonoAssembly *result = NULL;
540         gboolean refonly = GPOINTER_TO_UINT (user_data);
541
542         if (assemblies_path && assemblies_path [0] != NULL) {
543                 result = real_load (assemblies_path, aname->culture, aname->name, refonly);
544         }
545
546         return result;
547 }
548
549 static GList *loaded_assemblies = NULL;
550
551 static void
552 pedump_assembly_load_hook (MonoAssembly *assembly, gpointer user_data)
553 {
554         loaded_assemblies = g_list_prepend (loaded_assemblies, assembly);
555 }
556
557 static MonoAssembly *
558 pedump_assembly_search_hook (MonoAssemblyName *aname, gpointer user_data)
559 {
560         GList *tmp;
561
562        for (tmp = loaded_assemblies; tmp; tmp = tmp->next) {
563                MonoAssembly *ass = tmp->data;
564                if (mono_assembly_names_equal (aname, &ass->aname))
565                        return ass;
566        }
567        return NULL;
568 }
569
570 #define VALID_ONLY_FLAG 0x08000000
571 #define VERIFY_CODE_ONLY MONO_VERIFY_ALL + 1 
572 #define VERIFY_METADATA_ONLY VERIFY_CODE_ONLY + 1
573
574 int
575 main (int argc, char *argv [])
576 {
577         MonoImage *image;
578         char *file = NULL;
579         char *flags = NULL;
580         MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_VERIFIABLE;
581         const char *flag_desc [] = {"error", "warn", "cls", "all", "code", "fail-on-verifiable", "non-strict", "valid-only", "metadata", NULL};
582         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, VERIFY_METADATA_ONLY, 0};
583         int i, verify_flags = MONO_VERIFY_REPORT_ALL_ERRORS, run_new_metadata_verifier = 0;
584         
585         for (i = 1; i < argc; i++){
586                 if (argv [i][0] != '-'){
587                         file = argv [i];
588                         continue;
589                 }
590
591                 if (strcmp (argv [i], "--help") == 0)
592                         usage ();
593                 else if (strcmp (argv [i], "--verify") == 0) {
594                         verify_pe = 1;
595                         dump_data = 0;
596                         ++i;
597                         flags = argv [i];
598                 } else {
599                         usage ();
600                 }
601         }
602         
603         if (!file)
604                 usage ();
605
606         mono_perfcounters_init ();
607         mono_metadata_init ();
608         mono_images_init ();
609         mono_assemblies_init ();
610         mono_loader_init ();
611  
612         if (verify_pe) {
613                 char *tok = strtok (flags, ",");
614
615                 verify_metadata = 1;
616                 verify_code = 0;
617                 while (tok) {
618                         for (i = 0; flag_desc [i]; ++i) {
619                                 if (strcmp (tok, flag_desc [i]) == 0) {
620                                         if (flag_vals [i] == VERIFY_CODE_ONLY) {
621                                                 verify_metadata = 0;
622                                                 verify_code = 1;
623                                         } else if(flag_vals [i] == MONO_VERIFY_ALL) {
624                                                 verify_code = 1;
625                                         } else if(flag_vals [i] == VERIFY_METADATA_ONLY) {
626                                                 verify_metadata = 0;
627                                                 run_new_metadata_verifier = 1;
628                                         }
629                                         if (flag_vals [i] == VALID_ONLY_FLAG)
630                                                 verifier_mode = MONO_VERIFIER_MODE_VALID;
631                                         else
632                                                 verify_flags |= flag_vals [i];
633                                         break;
634                                 }
635                         }
636                         if (!flag_desc [i])
637                                 g_print ("Unknown verify flag %s\n", tok);
638                         tok = strtok (NULL, ",");
639                 }
640
641                 mono_verifier_set_mode (verifier_mode);
642                 /**/
643         }
644
645         if (verify_pe || run_new_metadata_verifier) {
646                 mono_install_assembly_load_hook (pedump_assembly_load_hook, NULL);
647                 mono_install_assembly_search_hook (pedump_assembly_search_hook, NULL);
648
649                 mono_init_version ("pedump", "v2.0.50727");
650
651                 mono_install_assembly_preload_hook (pedump_preload, GUINT_TO_POINTER (FALSE));
652
653                 mono_marshal_init ();
654                 run_new_metadata_verifier = 1;
655         }
656         
657         if (run_new_metadata_verifier) {
658                 int res;
659                 mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
660
661                 res = verify_image_file (file);
662                 if (res || !verify_code)
663                         return res;
664         }
665
666         image = mono_image_open (file, NULL);
667         if (!image){
668                 fprintf (stderr, "Cannot open image %s\n", file);
669                 exit (1);
670         }
671
672         if (dump_data)
673                 dump_dotnet_iinfo (image);
674         if (verify_pe) {
675                 MonoAssembly *assembly;
676                 MonoImage *image;
677                 MonoImageOpenStatus status;
678
679                 mono_verifier_set_mode (verifier_mode);
680
681                 assembly = mono_assembly_open (file, NULL);
682                 /*fake an assembly for netmodules so the verifier works*/
683                 if (!assembly && (image = mono_image_open (file, &status)) && image->tables [MONO_TABLE_ASSEMBLY].rows == 0) {
684                         assembly = g_new0 (MonoAssembly, 1);
685                         assembly->in_gac = FALSE;
686                         assembly->image = image;
687                         image->assembly = assembly;
688                 }
689
690                 if (!assembly) {
691                         g_print ("Could not open assembly %s\n", file);
692                         return 4;
693                 }
694
695                 return dump_verify_info (assembly->image, verify_flags);
696         } else
697                 mono_image_close (image);
698         
699         return 0;
700 }
701