[netcore] Make the load hook ALC-aware (#16012)
[mono-project.git] / tools / pedump / pedump.c
blob8414ae36e209fca2881924095ac1546d67a3eaf3
1 /*
2 * pedump.c: Dumps the contents of an extended PE/COFF file
4 * Author:
5 * Miguel de Icaza (miguel@ximian.com)
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.
11 #include <config.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <mono/metadata/image.h>
16 #include <glib.h>
17 #include <mono/metadata/cil-coff.h>
18 #include <mono/metadata/mono-endian.h>
19 #include <mono/metadata/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-internals.h>
25 #include <mono/metadata/metadata-internals.h>
26 #include <mono/metadata/class-internals.h>
27 #include <mono/metadata/class-init.h>
28 #include <mono/metadata/verify-internals.h>
29 #include <mono/metadata/marshal.h>
30 #include <mono/metadata/w32handle.h>
31 #include "mono/utils/mono-digest.h"
32 #include <mono/utils/mono-mmap.h>
33 #include <mono/utils/mono-counters.h>
34 #include <mono/utils/mono-error-internals.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
41 gboolean dump_data = TRUE;
42 gboolean verify_pe = FALSE;
43 gboolean verify_metadata = FALSE;
44 gboolean verify_code = FALSE;
45 gboolean verify_partial_md = FALSE;
47 static char *assembly_directory[2];
49 static MonoAssembly *pedump_preload (MonoAssemblyName *aname, gchar **assemblies_path, gpointer user_data);
50 static void pedump_assembly_load_hook (MonoAssemblyLoadContext *alc, MonoAssembly *assembly, gpointer user_data, MonoError *error);
51 static MonoAssembly *pedump_assembly_search_hook (MonoAssemblyLoadContext *alc, MonoAssembly *requesting, MonoAssemblyName *aname, gboolean refonly, gboolean postload, gpointer user_data, MonoError *error);
53 /* unused
54 static void
55 hex_dump (const char *buffer, int base, int count)
57 int i;
59 for (i = 0; i < count; i++){
60 if ((i % 16) == 0)
61 printf ("\n0x%08x: ", (unsigned char) base + i);
63 printf ("%02x ", (unsigned char) (buffer [i]));
68 static void
69 hex8 (const char *label, unsigned char x)
71 printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
74 static void
75 hex16 (const char *label, guint16 x)
77 printf ("\t%s: 0x%04x\n", label, x);
80 static void
81 hex32 (const char *label, guint32 x)
83 printf ("\t%s: 0x%08x\n", label, x);
86 static void
87 dump_coff_header (MonoCOFFHeader *coff)
89 printf ("\nCOFF Header:\n");
90 hex16 (" Machine", coff->coff_machine);
91 hex16 (" Sections", coff->coff_sections);
92 hex32 (" Time stamp", coff->coff_time);
93 hex32 ("Pointer to Symbol Table", coff->coff_symptr);
94 hex32 (" Symbol Count", coff->coff_symcount);
95 hex16 (" Optional Header Size", coff->coff_opt_header_size);
96 hex16 (" Characteristics", coff->coff_attributes);
100 static void
101 dump_pe_header (MonoPEHeader *pe)
103 printf ("\nPE Header:\n");
104 hex16 (" Magic (0x010b)", pe->pe_magic);
105 hex8 (" LMajor (6)", pe->pe_major);
106 hex8 (" LMinor (0)", pe->pe_minor);
107 hex32 (" Code Size", pe->pe_code_size);
108 hex32 (" Initialized Data Size", pe->pe_data_size);
109 hex32 ("Uninitialized Data Size", pe->pe_uninit_data_size);
110 hex32 (" Entry Point RVA", pe->pe_rva_entry_point);
111 hex32 (" Code Base RVA", pe->pe_rva_code_base);
112 hex32 (" Data Base RVA", pe->pe_rva_data_base);
113 printf ("\n");
116 static void
117 dump_nt_header (MonoPEHeaderNT *nt)
119 printf ("\nNT Header:\n");
121 hex32 (" Image Base (0x400000)", nt->pe_image_base);
122 hex32 ("Section Alignment (8192)", nt->pe_section_align);
123 hex32 (" File Align (512/4096)", nt->pe_file_alignment);
124 hex16 (" OS Major (4)", nt->pe_os_major);
125 hex16 (" OS Minor (0)", nt->pe_os_minor);
126 hex16 (" User Major (0)", nt->pe_user_major);
127 hex16 (" User Minor (0)", nt->pe_user_minor);
128 hex16 (" Subsys major (4)", nt->pe_subsys_major);
129 hex16 (" Subsys minor (0)", nt->pe_subsys_minor);
130 hex32 (" Reserverd", nt->pe_reserved_1);
131 hex32 (" Image Size", nt->pe_image_size);
132 hex32 (" Header Size", nt->pe_header_size);
133 hex32 (" Checksum (0)", nt->pe_checksum);
134 hex16 (" Subsystem", nt->pe_subsys_required);
135 hex16 (" DLL Flags (0)", nt->pe_dll_flags);
136 hex32 (" Stack Reserve Size (1M)", nt->pe_stack_reserve);
137 hex32 ("Stack commit Size (4096)", nt->pe_stack_commit);
138 hex32 (" Heap Reserve Size (1M)", nt->pe_heap_reserve);
139 hex32 (" Heap Commit Size (4096)", nt->pe_heap_commit);
140 hex32 (" Loader flags (0x1)", nt->pe_loader_flags);
141 hex32 (" Data Directories (16)", nt->pe_data_dir_count);
144 static void
145 dent (const char *label, MonoPEDirEntry de)
147 printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
150 static void
151 dump_blob (const char *desc, const char* p, guint32 size)
153 int i;
155 printf ("%s", desc);
156 if (!p) {
157 printf (" none\n");
158 return;
161 for (i = 0; i < size; ++i) {
162 if (!(i % 16))
163 printf ("\n\t");
164 printf (" %02X", p [i] & 0xFF);
166 printf ("\n");
169 static void
170 dump_public_key (MonoImage *m)
172 guint32 size;
173 const char *p;
175 p = mono_image_get_public_key (m, &size);
176 dump_blob ("\nPublic key:", p, size);
179 static void
180 dump_strong_name (MonoImage *m)
182 guint32 size;
183 const char *p;
185 p = mono_image_get_strong_name (m, &size);
186 dump_blob ("\nStrong name:", p, size);
189 static void
190 dump_datadir (MonoPEDatadir *dd)
192 printf ("\nData directories:\n");
193 dent (" Export Table", dd->pe_export_table);
194 dent (" Import Table", dd->pe_import_table);
195 dent (" Resource Table", dd->pe_resource_table);
196 dent (" Exception Table", dd->pe_exception_table);
197 dent ("Certificate Table", dd->pe_certificate_table);
198 dent (" Reloc Table", dd->pe_reloc_table);
199 dent (" Debug", dd->pe_debug);
200 dent (" Copyright", dd->pe_copyright);
201 dent (" Global Ptr", dd->pe_global_ptr);
202 dent (" TLS Table", dd->pe_tls_table);
203 dent ("Load Config Table", dd->pe_load_config_table);
204 dent (" Bound Import", dd->pe_bound_import);
205 dent (" IAT", dd->pe_iat);
206 dent ("Delay Import Desc", dd->pe_delay_import_desc);
207 dent (" CLI Header", dd->pe_cli_header);
210 static void
211 dump_dotnet_header (MonoDotNetHeader *header)
213 dump_coff_header (&header->coff);
214 dump_pe_header (&header->pe);
215 dump_nt_header (&header->nt);
216 dump_datadir (&header->datadir);
219 static void
220 dump_section_table (MonoSectionTable *st)
222 guint32 flags = st->st_flags;
224 printf ("\n\tName: %s\n", st->st_name);
225 hex32 (" Virtual Size", st->st_virtual_size);
226 hex32 ("Virtual Address", st->st_virtual_address);
227 hex32 (" Raw Data Size", st->st_raw_data_size);
228 hex32 (" Raw Data Ptr", st->st_raw_data_ptr);
229 hex32 (" Reloc Ptr", st->st_reloc_ptr);
230 hex32 (" LineNo Ptr", st->st_lineno_ptr);
231 hex16 (" Reloc Count", st->st_reloc_count);
232 hex16 (" Line Count", st->st_line_count);
234 printf ("\tFlags: %s%s%s%s%s%s%s%s%s%s\n",
235 (flags & SECT_FLAGS_HAS_CODE) ? "code, " : "",
236 (flags & SECT_FLAGS_HAS_INITIALIZED_DATA) ? "data, " : "",
237 (flags & SECT_FLAGS_HAS_UNINITIALIZED_DATA) ? "bss, " : "",
238 (flags & SECT_FLAGS_MEM_DISCARDABLE) ? "discard, " : "",
239 (flags & SECT_FLAGS_MEM_NOT_CACHED) ? "nocache, " : "",
240 (flags & SECT_FLAGS_MEM_NOT_PAGED) ? "nopage, " : "",
241 (flags & SECT_FLAGS_MEM_SHARED) ? "shared, " : "",
242 (flags & SECT_FLAGS_MEM_EXECUTE) ? "exec, " : "",
243 (flags & SECT_FLAGS_MEM_READ) ? "read, " : "",
244 (flags & SECT_FLAGS_MEM_WRITE) ? "write" : "");
247 static void
248 dump_sections (MonoCLIImageInfo *iinfo)
250 const int top = iinfo->cli_header.coff.coff_sections;
251 int i;
253 for (i = 0; i < top; i++)
254 dump_section_table (&iinfo->cli_section_tables [i]);
257 static void
258 dump_cli_header (MonoCLIHeader *ch)
260 printf ("\n");
261 printf (" CLI header size: %d\n", ch->ch_size);
262 printf (" Runtime required: %d.%d\n", ch->ch_runtime_major, ch->ch_runtime_minor);
263 printf (" Flags: %s, %s, %s, %s\n",
264 (ch->ch_flags & CLI_FLAGS_ILONLY ? "ilonly" : "contains native"),
265 (ch->ch_flags & CLI_FLAGS_32BITREQUIRED ? "32bits" : "32/64"),
266 (ch->ch_flags & CLI_FLAGS_TRACKDEBUGDATA ? "trackdebug" : "no-trackdebug"),
267 (ch->ch_flags & CLI_FLAGS_STRONGNAMESIGNED ? "strongnamesigned" : "notsigned"));
268 dent (" Metadata", ch->ch_metadata);
269 hex32 ("Entry Point Token", ch->ch_entry_point);
270 dent (" Resources at", ch->ch_resources);
271 dent (" Strong Name at", ch->ch_strong_name);
272 dent (" Code Manager at", ch->ch_code_manager_table);
273 dent (" VTableFixups at", ch->ch_vtable_fixups);
274 dent (" EAT jumps at", ch->ch_export_address_table_jumps);
277 static void
278 dsh (const char *label, MonoImage *meta, MonoStreamHeader *sh)
280 printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
281 label,
282 (int)(sh->data - meta->raw_metadata), (int)(sh->data + sh->size - meta->raw_metadata),
283 sh->size, sh->size);
286 static void
287 dump_metadata_header (MonoImage *meta)
289 printf ("\nMetadata header:\n");
290 printf (" Version: %d.%d\n", meta->md_version_major, meta->md_version_minor);
291 printf (" Version string: %s\n", meta->version);
294 static void
295 dump_metadata_ptrs (MonoImage *meta)
297 printf ("\nMetadata pointers:\n");
298 dsh ("\tTables (#~)", meta, &meta->heap_tables);
299 dsh ("\t Strings", meta, &meta->heap_strings);
300 dsh ("\t Blob", meta, &meta->heap_blob);
301 dsh ("\tUser string", meta, &meta->heap_us);
302 dsh ("\t GUID", meta, &meta->heap_guid);
305 static void
306 dump_metadata (MonoImage *meta)
308 int table;
310 dump_metadata_header (meta);
312 dump_metadata_ptrs (meta);
314 printf ("Rows:\n");
315 for (table = 0; table < MONO_TABLE_NUM; table++){
316 if (meta->tables [table].rows == 0)
317 continue;
318 printf ("Table %s: %d records (%d bytes, at %x)\n",
319 mono_meta_table_name (table),
320 meta->tables [table].rows,
321 meta->tables [table].row_size,
322 (unsigned int)(meta->tables [table].base - meta->raw_data)
327 static void
328 dump_methoddef (MonoImage *metadata, guint32 token)
330 const char *loc;
332 if (!token)
333 return;
334 loc = mono_metadata_locate_token (metadata, token);
336 printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
339 static void
340 dump_dotnet_iinfo (MonoImage *image)
342 MonoCLIImageInfo *iinfo = (MonoCLIImageInfo *)image->image_info;
344 dump_dotnet_header (&iinfo->cli_header);
345 dump_sections (iinfo);
346 dump_cli_header (&iinfo->cli_cli_header);
347 dump_strong_name (image);
348 dump_public_key (image);
349 dump_metadata (image);
351 dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
354 static int
355 dump_verify_info (MonoImage *image, int flags, gboolean valid_only)
357 GSList *errors, *tmp;
358 int count = 0, verifiable = 0;
359 const char* desc [] = {
360 "Ok", "Error", "Warning", NULL, "CLS", NULL, NULL, NULL, "Not Verifiable"
363 if (verify_code) { /* verify code */
364 int i;
365 MonoTableInfo *m = &image->tables [MONO_TABLE_METHOD];
367 for (i = 0; i < m->rows; ++i) {
368 MonoMethod *method;
369 ERROR_DECL (error);
371 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i+1), NULL, NULL, error);
372 if (!method) {
373 g_print ("Warning: Cannot lookup method with token 0x%08x due to %s\n", i + 1, mono_error_get_message (error));
374 mono_error_cleanup (error);
375 continue;
377 errors = mono_method_verify (method, flags);
378 if (errors) {
379 MonoClass *klass = mono_method_get_class (method);
380 char *name = mono_type_full_name (m_class_get_byval_arg (klass));
381 if (mono_method_signature_internal (method) == NULL) {
382 g_print ("In method: %s::%s(ERROR)\n", name, mono_method_get_name (method));
383 } else {
384 char *sig;
385 sig = mono_signature_get_desc (mono_method_signature_internal (method), FALSE);
386 g_print ("In method: %s::%s(%s)\n", name, mono_method_get_name (method), sig);
387 g_free (sig);
389 g_free (name);
392 for (tmp = errors; tmp; tmp = tmp->next) {
393 MonoVerifyInfo *info = (MonoVerifyInfo *)tmp->data;
394 if (info->status == MONO_VERIFY_NOT_VERIFIABLE && valid_only)
395 continue;
397 g_print ("%s: %s\n", desc [info->status], info->message);
398 if (info->status == MONO_VERIFY_ERROR) {
399 count++;
400 verifiable = 3;
402 if(info->status == MONO_VERIFY_NOT_VERIFIABLE) {
403 if (verifiable < 2)
404 verifiable = 2;
407 mono_free_verify_list (errors);
411 if (count)
412 g_print ("Error count: %d\n", count);
413 return verifiable;
416 static void
417 usage (void)
419 printf ("Usage is: pedump [--verify error,warn,cls,all,code,fail-on-verifiable,non-strict,valid-only,metadata] file.exe\n");
420 exit (1);
423 static int
424 verify_image_file (const char *fname)
426 ERROR_DECL (error);
428 MonoImage *image;
429 MonoTableInfo *table;
430 MonoAssembly *assembly;
431 MonoImageOpenStatus status;
432 int i, count = 0;
434 if (!strstr (fname, "mscorlib.dll")) {
435 image = mono_image_open_raw (mono_domain_default_alc (mono_get_root_domain ()), fname, &status);
436 if (!image) {
437 printf ("Could not open %s\n", fname);
438 return 1;
441 if (!mono_verifier_verify_pe_data (image, error))
442 goto invalid_image;
444 if (!mono_image_load_pe_data (image)) {
445 printf ("Could not load pe data for assembly %s\n", fname);
446 return 1;
449 if (!mono_verifier_verify_cli_data (image, error))
450 goto invalid_image;
452 if (!mono_image_load_cli_data (image)) {
453 printf ("Could not load cli data for assembly %s\n", fname);
454 return 1;
457 if (!mono_verifier_verify_table_data (image, error))
458 goto invalid_image;
460 mono_image_load_names (image);
462 /*fake an assembly for class loading to work*/
463 assembly = g_new0 (MonoAssembly, 1);
464 assembly->in_gac = FALSE;
465 assembly->image = image;
466 image->assembly = assembly;
467 mono_assembly_fill_assembly_name (image, &assembly->aname);
469 /*Finish initializing the runtime*/
470 mono_install_assembly_load_hook_v2 (pedump_assembly_load_hook, NULL);
471 mono_install_assembly_search_hook_v2 (pedump_assembly_search_hook, NULL, FALSE, FALSE);
473 mono_init_version ("pedump", image->version);
475 mono_install_assembly_preload_hook (pedump_preload, GUINT_TO_POINTER (FALSE));
477 mono_icall_init ();
478 mono_marshal_init ();
479 } else {
480 /*Finish initializing the runtime*/
481 mono_install_assembly_load_hook_v2 (pedump_assembly_load_hook, NULL);
482 mono_install_assembly_search_hook_v2 (pedump_assembly_search_hook, NULL, FALSE, FALSE);
484 mono_init_version ("pedump", NULL);
486 mono_install_assembly_preload_hook (pedump_preload, GUINT_TO_POINTER (FALSE));
488 mono_icall_init ();
489 mono_marshal_init ();
490 image = mono_get_corlib ();
492 if (!mono_verifier_verify_pe_data (image, error))
493 goto invalid_image;
495 if (!mono_image_load_pe_data (image)) {
496 printf ("Could not load pe data for assembly %s\n", fname);
497 return 1;
500 if (!mono_verifier_verify_cli_data (image, error))
501 goto invalid_image;
503 if (!mono_image_load_cli_data (image)) {
504 printf ("Could not load cli data for assembly %s\n", fname);
505 return 1;
508 if (!mono_verifier_verify_table_data (image, error))
509 goto invalid_image;
512 if (!verify_partial_md && !mono_verifier_verify_full_table_data (image, error))
513 goto invalid_image;
516 table = &image->tables [MONO_TABLE_TYPEDEF];
517 for (i = 1; i <= table->rows; ++i) {
518 ERROR_DECL (error);
519 guint32 token = i | MONO_TOKEN_TYPE_DEF;
520 MonoClass *klass = mono_class_get_checked (image, token, error);
521 if (!klass) {
522 printf ("Could not load class with token %x due to %s\n", token, mono_error_get_message (error));
523 mono_error_cleanup (error);
524 continue;
526 mono_class_init_internal (klass);
527 if (mono_class_has_failure (klass)) {
528 ERROR_DECL (type_load_error);
529 mono_error_set_for_class_failure (type_load_error, klass);
530 printf ("Could not initialize class(0x%08x) %s.%s due to %s\n", token, m_class_get_name_space (klass), m_class_get_name (klass), mono_error_get_message (type_load_error));
531 mono_error_cleanup (type_load_error);
532 ++count;
535 mono_class_setup_vtable (klass);
536 if (mono_class_has_failure (klass)) {
537 ERROR_DECL (type_load_error);
538 mono_error_set_for_class_failure (type_load_error, klass);
539 printf ("Could not initialize vtable of class(0x%08x) %s.%s due to %s\n", token, m_class_get_name_space (klass), m_class_get_name (klass), mono_error_get_message (type_load_error));
540 mono_error_cleanup (type_load_error);
541 ++count;
544 if (count)
545 return 5;
546 return 0;
548 invalid_image:
549 if (!is_ok (error)) {
550 g_print ("FAIL: %s\n", mono_error_get_message (error));
551 mono_error_cleanup (error);
552 ++count;
554 if (count)
555 g_print ("Error count: %d\n", count);
556 return 1;
559 static gboolean
560 try_load_from (MonoAssembly **assembly, const gchar *path1, const gchar *path2,
561 const gchar *path3, const gchar *path4,
562 const MonoAssemblyOpenRequest *req)
564 gchar *fullpath;
566 *assembly = NULL;
567 fullpath = g_build_filename (path1, path2, path3, path4, NULL);
568 if (g_file_test (fullpath, G_FILE_TEST_IS_REGULAR))
569 *assembly = mono_assembly_request_open (fullpath, req, NULL);
571 g_free (fullpath);
572 return (*assembly != NULL);
575 static MonoAssembly *
576 real_load (gchar **search_path, const gchar *culture, const gchar *name, const MonoAssemblyOpenRequest *req)
578 MonoAssembly *result = NULL;
579 gchar **path;
580 gchar *filename;
581 const gchar *local_culture;
582 gint len;
584 if (!culture || *culture == '\0') {
585 local_culture = "";
586 } else {
587 local_culture = culture;
590 filename = g_strconcat (name, ".dll", NULL);
591 len = strlen (filename);
593 for (path = search_path; *path; path++) {
594 if (**path == '\0')
595 continue; /* Ignore empty ApplicationBase */
597 /* See test cases in bug #58992 and bug #57710 */
598 /* 1st try: [culture]/[name].dll (culture may be empty) */
599 strcpy (filename + len - 4, ".dll");
600 if (try_load_from (&result, *path, local_culture, "", filename, req))
601 break;
603 /* 2nd try: [culture]/[name].exe (culture may be empty) */
604 strcpy (filename + len - 4, ".exe");
605 if (try_load_from (&result, *path, local_culture, "", filename, req))
606 break;
608 /* 3rd try: [culture]/[name]/[name].dll (culture may be empty) */
609 strcpy (filename + len - 4, ".dll");
610 if (try_load_from (&result, *path, local_culture, name, filename, req))
611 break;
613 /* 4th try: [culture]/[name]/[name].exe (culture may be empty) */
614 strcpy (filename + len - 4, ".exe");
615 if (try_load_from (&result, *path, local_culture, name, filename, req))
616 break;
619 g_free (filename);
620 return result;
624 * Try to load referenced assemblies from assemblies_path.
626 static MonoAssembly *
627 pedump_preload (MonoAssemblyName *aname,
628 gchar **assemblies_path,
629 gpointer user_data)
631 MonoAssembly *result = NULL;
632 gboolean refonly = GPOINTER_TO_UINT (user_data);
633 MonoAssemblyOpenRequest req;
634 mono_assembly_request_prepare (&req.request, sizeof (req), refonly ? MONO_ASMCTX_REFONLY : MONO_ASMCTX_DEFAULT);
637 if (assemblies_path && assemblies_path [0] != NULL) {
638 result = real_load (assemblies_path, aname->culture, aname->name, &req);
640 if (!result)
641 result = real_load (assembly_directory, aname->culture, aname->name, &req);
643 return result;
646 static GList *loaded_assemblies = NULL;
648 static void
649 pedump_assembly_load_hook (MonoAssemblyLoadContext *alc, MonoAssembly *assembly, gpointer user_data, MonoError *error)
651 loaded_assemblies = g_list_prepend (loaded_assemblies, assembly);
654 static MonoAssembly *
655 pedump_assembly_search_hook (MonoAssemblyLoadContext *alc, MonoAssembly *requesting, MonoAssemblyName *aname,
656 gboolean refonly, gboolean postload, gpointer user_data, MonoError *error)
658 GList *tmp;
660 for (tmp = loaded_assemblies; tmp; tmp = tmp->next) {
661 MonoAssembly *ass = (MonoAssembly *)tmp->data;
662 if (mono_assembly_names_equal (aname, &ass->aname))
663 return ass;
665 return NULL;
668 static void
669 thread_state_init (MonoThreadUnwindState *ctx)
673 #define VALID_ONLY_FLAG 0x08000000
674 #define VERIFY_CODE_ONLY MONO_VERIFY_ALL + 1
675 #define VERIFY_METADATA_ONLY VERIFY_CODE_ONLY + 1
676 #define VERIFY_PARTIAL_METADATA VERIFY_CODE_ONLY + 2
679 main (int argc, char *argv [])
681 int image_result = 0;
682 MonoImage *image;
683 char *file = NULL;
684 char *flags = NULL;
685 MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_VERIFIABLE;
686 const char *flag_desc [] = {"error", "warn", "cls", "all", "code", "fail-on-verifiable", "non-strict", "valid-only", "metadata", "partial-md", NULL};
687 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};
688 int i, verify_flags = MONO_VERIFY_REPORT_ALL_ERRORS, run_new_metadata_verifier = 0;
689 MonoThreadInfoRuntimeCallbacks ticallbacks;
691 for (i = 1; i < argc; i++){
692 if (argv [i][0] != '-'){
693 file = argv [i];
694 continue;
697 if (strcmp (argv [i], "--help") == 0)
698 usage ();
699 else if (strcmp (argv [i], "--verify") == 0) {
700 verify_pe = 1;
701 dump_data = 0;
702 ++i;
703 flags = argv [i];
704 } else {
705 usage ();
709 if (!file)
710 usage ();
712 //We have to force the runtime to load the corlib under verification as its own corlib so core types are properly populated in mono_defaults.
713 if (strstr (file, "mscorlib.dll"))
714 g_setenv ("MONO_PATH", g_path_get_dirname (file), 1);
715 assembly_directory [0] = g_path_get_dirname (file);
716 assembly_directory [1] = NULL;
718 #ifndef DISABLE_PERFCOUNTERS
719 mono_perfcounters_init ();
720 #endif
721 mono_counters_init ();
722 mono_tls_init_runtime_keys ();
723 memset (&ticallbacks, 0, sizeof (ticallbacks));
724 ticallbacks.thread_state_init = thread_state_init;
725 #ifndef HOST_WIN32
726 mono_w32handle_init ();
727 #endif
728 mono_thread_info_runtime_init (&ticallbacks);
730 mono_metadata_init ();
731 mono_images_init ();
732 mono_assemblies_init ();
733 mono_loader_init ();
736 if (verify_pe) {
737 char *tok = strtok (flags, ",");
739 verify_metadata = 1;
740 verify_code = 0;
741 while (tok) {
742 for (i = 0; flag_desc [i]; ++i) {
743 if (strcmp (tok, flag_desc [i]) == 0) {
744 if (flag_vals [i] == VERIFY_CODE_ONLY) {
745 verify_metadata = 0;
746 verify_code = 1;
747 } else if(flag_vals [i] == MONO_VERIFY_ALL) {
748 verify_code = 1;
749 } else if(flag_vals [i] == VERIFY_METADATA_ONLY) {
750 verify_metadata = 0;
751 run_new_metadata_verifier = 1;
752 } else if(flag_vals [i] == VERIFY_PARTIAL_METADATA) {
753 verify_partial_md = 1;
755 if (flag_vals [i] == VALID_ONLY_FLAG)
756 verifier_mode = MONO_VERIFIER_MODE_VALID;
757 else
758 verify_flags |= flag_vals [i];
759 break;
762 if (!flag_desc [i])
763 g_print ("Unknown verify flag %s\n", tok);
764 tok = strtok (NULL, ",");
767 mono_verifier_set_mode (verifier_mode);
768 /**/
771 if (verify_pe || run_new_metadata_verifier) {
772 run_new_metadata_verifier = 1;
775 if (run_new_metadata_verifier) {
776 mono_verifier_set_mode (verifier_mode);
778 image_result = verify_image_file (file);
779 if (image_result == 1 || !verify_code)
780 return image_result;
783 image = mono_image_open (file, NULL);
784 if (!image){
785 fprintf (stderr, "Cannot open image %s\n", file);
786 exit (1);
789 if (dump_data)
790 dump_dotnet_iinfo (image);
791 if (verify_pe) {
792 MonoAssemblyOpenRequest req;
793 MonoAssembly *assembly;
794 MonoImage *image;
795 MonoImageOpenStatus status;
796 int code_result;
798 mono_verifier_set_mode (verifier_mode);
800 mono_assembly_request_prepare (&req.request, sizeof (req), MONO_ASMCTX_DEFAULT);
801 assembly = mono_assembly_request_open (file, &req, NULL);
802 /*fake an assembly for netmodules so the verifier works*/
803 if (!assembly && (image = mono_image_open (file, &status)) && image->tables [MONO_TABLE_ASSEMBLY].rows == 0) {
804 assembly = g_new0 (MonoAssembly, 1);
805 assembly->in_gac = FALSE;
806 assembly->image = image;
807 image->assembly = assembly;
810 if (!assembly) {
811 g_print ("Could not open assembly %s\n", file);
812 return 4;
815 code_result = dump_verify_info (assembly->image, verify_flags, verifier_mode == MONO_VERIFIER_MODE_VALID);
816 return code_result ? code_result : image_result;
817 } else
818 mono_image_close (image);
820 return 0;