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