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