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