2 * pedump.c: Dumps the contents of an extended PE/COFF file
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)
17 #include "mono-endian.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/utils/mono-digest.h"
28 #include <mono/utils/mono-mmap.h>
29 #include <sys/types.h>
35 gboolean dump_data = TRUE;
36 gboolean verify_pe = FALSE;
37 gboolean verify_metadata = FALSE;
38 gboolean verify_code = FALSE;
42 hex_dump (const char *buffer, int base, int count)
46 for (i = 0; i < count; i++){
48 printf ("\n0x%08x: ", (unsigned char) base + i);
50 printf ("%02x ", (unsigned char) (buffer [i]));
56 hex8 (const char *label, unsigned char x)
58 printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
62 hex16 (const char *label, guint16 x)
64 printf ("\t%s: 0x%04x\n", label, x);
68 hex32 (const char *label, guint32 x)
70 printf ("\t%s: 0x%08x\n", label, x);
74 dump_coff_header (MonoCOFFHeader *coff)
76 printf ("\nCOFF Header:\n");
77 hex16 (" Machine", coff->coff_machine);
78 hex16 (" Sections", coff->coff_sections);
79 hex32 (" Time stamp", coff->coff_time);
80 hex32 ("Pointer to Symbol Table", coff->coff_symptr);
81 hex32 (" Symbol Count", coff->coff_symcount);
82 hex16 (" Optional Header Size", coff->coff_opt_header_size);
83 hex16 (" Characteristics", coff->coff_attributes);
88 dump_pe_header (MonoPEHeader *pe)
90 printf ("\nPE Header:\n");
91 hex16 (" Magic (0x010b)", pe->pe_magic);
92 hex8 (" LMajor (6)", pe->pe_major);
93 hex8 (" LMinor (0)", pe->pe_minor);
94 hex32 (" Code Size", pe->pe_code_size);
95 hex32 (" Initialized Data Size", pe->pe_data_size);
96 hex32 ("Uninitialized Data Size", pe->pe_uninit_data_size);
97 hex32 (" Entry Point RVA", pe->pe_rva_entry_point);
98 hex32 (" Code Base RVA", pe->pe_rva_code_base);
99 hex32 (" Data Base RVA", pe->pe_rva_data_base);
104 dump_nt_header (MonoPEHeaderNT *nt)
106 printf ("\nNT Header:\n");
108 hex32 (" Image Base (0x400000)", nt->pe_image_base);
109 hex32 ("Section Alignment (8192)", nt->pe_section_align);
110 hex32 (" File Align (512/4096)", nt->pe_file_alignment);
111 hex16 (" OS Major (4)", nt->pe_os_major);
112 hex16 (" OS Minor (0)", nt->pe_os_minor);
113 hex16 (" User Major (0)", nt->pe_user_major);
114 hex16 (" User Minor (0)", nt->pe_user_minor);
115 hex16 (" Subsys major (4)", nt->pe_subsys_major);
116 hex16 (" Subsys minor (0)", nt->pe_subsys_minor);
117 hex32 (" Reserverd", nt->pe_reserved_1);
118 hex32 (" Image Size", nt->pe_image_size);
119 hex32 (" Header Size", nt->pe_header_size);
120 hex32 (" Checksum (0)", nt->pe_checksum);
121 hex16 (" Subsystem", nt->pe_subsys_required);
122 hex16 (" DLL Flags (0)", nt->pe_dll_flags);
123 hex32 (" Stack Reserve Size (1M)", nt->pe_stack_reserve);
124 hex32 ("Stack commit Size (4096)", nt->pe_stack_commit);
125 hex32 (" Heap Reserve Size (1M)", nt->pe_heap_reserve);
126 hex32 (" Heap Commit Size (4096)", nt->pe_heap_commit);
127 hex32 (" Loader flags (0x1)", nt->pe_loader_flags);
128 hex32 (" Data Directories (16)", nt->pe_data_dir_count);
132 dent (const char *label, MonoPEDirEntry de)
134 printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
138 dump_blob (const char *desc, const char* p, guint32 size)
148 for (i = 0; i < size; ++i) {
151 printf (" %02X", p [i] & 0xFF);
157 dump_public_key (MonoImage *m)
162 p = mono_image_get_public_key (m, &size);
163 dump_blob ("\nPublic key:", p, size);
167 dump_strong_name (MonoImage *m)
172 p = mono_image_get_strong_name (m, &size);
173 dump_blob ("\nStrong name:", p, size);
177 dump_datadir (MonoPEDatadir *dd)
179 printf ("\nData directories:\n");
180 dent (" Export Table", dd->pe_export_table);
181 dent (" Import Table", dd->pe_import_table);
182 dent (" Resource Table", dd->pe_resource_table);
183 dent (" Exception Table", dd->pe_exception_table);
184 dent ("Certificate Table", dd->pe_certificate_table);
185 dent (" Reloc Table", dd->pe_reloc_table);
186 dent (" Debug", dd->pe_debug);
187 dent (" Copyright", dd->pe_copyright);
188 dent (" Global Ptr", dd->pe_global_ptr);
189 dent (" TLS Table", dd->pe_tls_table);
190 dent ("Load Config Table", dd->pe_load_config_table);
191 dent (" Bound Import", dd->pe_bound_import);
192 dent (" IAT", dd->pe_iat);
193 dent ("Delay Import Desc", dd->pe_delay_import_desc);
194 dent (" CLI Header", dd->pe_cli_header);
198 dump_dotnet_header (MonoDotNetHeader *header)
200 dump_coff_header (&header->coff);
201 dump_pe_header (&header->pe);
202 dump_nt_header (&header->nt);
203 dump_datadir (&header->datadir);
207 dump_section_table (MonoSectionTable *st)
209 guint32 flags = st->st_flags;
211 printf ("\n\tName: %s\n", st->st_name);
212 hex32 (" Virtual Size", st->st_virtual_size);
213 hex32 ("Virtual Address", st->st_virtual_address);
214 hex32 (" Raw Data Size", st->st_raw_data_size);
215 hex32 (" Raw Data Ptr", st->st_raw_data_ptr);
216 hex32 (" Reloc Ptr", st->st_reloc_ptr);
217 hex32 (" LineNo Ptr", st->st_lineno_ptr);
218 hex16 (" Reloc Count", st->st_reloc_count);
219 hex16 (" Line Count", st->st_line_count);
221 printf ("\tFlags: %s%s%s%s%s%s%s%s%s%s\n",
222 (flags & SECT_FLAGS_HAS_CODE) ? "code, " : "",
223 (flags & SECT_FLAGS_HAS_INITIALIZED_DATA) ? "data, " : "",
224 (flags & SECT_FLAGS_HAS_UNINITIALIZED_DATA) ? "bss, " : "",
225 (flags & SECT_FLAGS_MEM_DISCARDABLE) ? "discard, " : "",
226 (flags & SECT_FLAGS_MEM_NOT_CACHED) ? "nocache, " : "",
227 (flags & SECT_FLAGS_MEM_NOT_PAGED) ? "nopage, " : "",
228 (flags & SECT_FLAGS_MEM_SHARED) ? "shared, " : "",
229 (flags & SECT_FLAGS_MEM_EXECUTE) ? "exec, " : "",
230 (flags & SECT_FLAGS_MEM_READ) ? "read, " : "",
231 (flags & SECT_FLAGS_MEM_WRITE) ? "write" : "");
235 dump_sections (MonoCLIImageInfo *iinfo)
237 const int top = iinfo->cli_header.coff.coff_sections;
240 for (i = 0; i < top; i++)
241 dump_section_table (&iinfo->cli_section_tables [i]);
245 dump_cli_header (MonoCLIHeader *ch)
248 printf (" CLI header size: %d\n", ch->ch_size);
249 printf (" Runtime required: %d.%d\n", ch->ch_runtime_major, ch->ch_runtime_minor);
250 printf (" Flags: %s, %s, %s, %s\n",
251 (ch->ch_flags & CLI_FLAGS_ILONLY ? "ilonly" : "contains native"),
252 (ch->ch_flags & CLI_FLAGS_32BITREQUIRED ? "32bits" : "32/64"),
253 (ch->ch_flags & CLI_FLAGS_ILONLY ? "trackdebug" : "no-trackdebug"),
254 (ch->ch_flags & CLI_FLAGS_STRONGNAMESIGNED ? "strongnamesigned" : "notsigned"));
255 dent (" Metadata", ch->ch_metadata);
256 hex32 ("Entry Point Token", ch->ch_entry_point);
257 dent (" Resources at", ch->ch_resources);
258 dent (" Strong Name at", ch->ch_strong_name);
259 dent (" Code Manager at", ch->ch_code_manager_table);
260 dent (" VTableFixups at", ch->ch_vtable_fixups);
261 dent (" EAT jumps at", ch->ch_export_address_table_jumps);
265 dsh (const char *label, MonoImage *meta, MonoStreamHeader *sh)
267 printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
269 (int)(sh->data - meta->raw_metadata), (int)(sh->data + sh->size - meta->raw_metadata),
274 dump_metadata_header (MonoImage *meta)
276 printf ("\nMetadata header:\n");
277 printf (" Version: %d.%d\n", meta->md_version_major, meta->md_version_minor);
278 printf (" Version string: %s\n", meta->version);
282 dump_metadata_ptrs (MonoImage *meta)
284 printf ("\nMetadata pointers:\n");
285 dsh ("\tTables (#~)", meta, &meta->heap_tables);
286 dsh ("\t Strings", meta, &meta->heap_strings);
287 dsh ("\t Blob", meta, &meta->heap_blob);
288 dsh ("\tUser string", meta, &meta->heap_us);
289 dsh ("\t GUID", meta, &meta->heap_guid);
293 dump_metadata (MonoImage *meta)
297 dump_metadata_header (meta);
299 dump_metadata_ptrs (meta);
302 for (table = 0; table < MONO_TABLE_NUM; table++){
303 if (meta->tables [table].rows == 0)
305 printf ("Table %s: %d records (%d bytes, at %p)\n",
306 mono_meta_table_name (table),
307 meta->tables [table].rows,
308 meta->tables [table].row_size,
309 meta->tables [table].base
315 dump_methoddef (MonoImage *metadata, guint32 token)
321 loc = mono_metadata_locate_token (metadata, token);
323 printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
327 dump_dotnet_iinfo (MonoImage *image)
329 MonoCLIImageInfo *iinfo = image->image_info;
331 dump_dotnet_header (&iinfo->cli_header);
332 dump_sections (iinfo);
333 dump_cli_header (&iinfo->cli_cli_header);
334 dump_strong_name (image);
335 dump_public_key (image);
336 dump_metadata (image);
338 dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
342 dump_verify_info (MonoImage *image, int flags)
344 GSList *errors, *tmp;
345 int count = 0, verifiable = 0;
346 const char* desc [] = {
347 "Ok", "Error", "Warning", NULL, "CLS", NULL, NULL, NULL, "Not Verifiable"
350 if (verify_metadata) {
351 errors = mono_image_verify_tables (image, flags);
353 for (tmp = errors; tmp; tmp = tmp->next) {
354 MonoVerifyInfo *info = tmp->data;
355 g_print ("%s: %s\n", desc [info->status], info->message);
356 if (info->status == MONO_VERIFY_ERROR)
359 mono_free_verify_list (errors);
362 if (verify_code) { /* verify code */
364 MonoTableInfo *m = &image->tables [MONO_TABLE_METHOD];
366 for (i = 0; i < m->rows; ++i) {
368 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i+1), NULL);
370 g_print ("Warning: Cannot lookup method with token 0x%08x\n", i + 1);
373 errors = mono_method_verify (method, flags);
376 MonoClass *klass = mono_method_get_class (method);
377 sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
378 name = mono_type_full_name (&klass->byval_arg);
379 g_print ("In method: %s::%s(%s)\n", name, mono_method_get_name (method), sig);
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) {
391 if(info->status == MONO_VERIFY_NOT_VERIFIABLE) {
396 mono_free_verify_list (errors);
401 g_print ("Error count: %d\n", count);
408 printf ("Usage is: pedump [--verify error,warn,cls,all,code,fail-on-verifiable,non-strict,valid-only,metadata] file.exe\n");
413 verify_image_file (const char *fname)
416 struct stat stat_buf;
417 void *raw_data_handle;
419 GSList *errors, *tmp;
421 const char* desc [] = {
422 "Ok", "Error", "Warning", NULL, "CLS", NULL, NULL, NULL, "Not Verifiable"
425 if ((filed = fopen (fname, "rb")) == NULL) {
426 fprintf (stderr, "Cannot open file %s\n", fname);
430 if (fstat (fileno (filed), &stat_buf)) {
432 fprintf (stderr, "Cannot stat file %s\n", fname);
436 raw_data = mono_file_map (stat_buf.st_size, MONO_MMAP_READ|MONO_MMAP_PRIVATE, fileno (filed), 0, &raw_data_handle);
439 fprintf (stderr, "Could not mmap file %s\n", fname);
444 errors = mono_image_verify (raw_data, stat_buf.st_size);
445 mono_file_unmap (raw_data, raw_data_handle);
450 for (tmp = errors; tmp; tmp = tmp->next) {
451 MonoVerifyInfo *info = tmp->data;
452 g_print ("%s: %s\n", desc [info->status], info->message);
453 if (info->status == MONO_VERIFY_ERROR)
456 mono_free_verify_list (errors);
458 g_print ("Error count: %d\n", count);
460 return count > 0 ? 1 : 0;
464 try_load_from (MonoAssembly **assembly, const gchar *path1, const gchar *path2,
465 const gchar *path3, const gchar *path4, gboolean refonly)
470 fullpath = g_build_filename (path1, path2, path3, path4, NULL);
471 if (g_file_test (fullpath, G_FILE_TEST_IS_REGULAR))
472 *assembly = mono_assembly_open_full (fullpath, NULL, refonly);
475 return (*assembly != NULL);
478 static MonoAssembly *
479 real_load (gchar **search_path, const gchar *culture, const gchar *name, gboolean refonly)
481 MonoAssembly *result = NULL;
484 const gchar *local_culture;
487 if (!culture || *culture == '\0') {
490 local_culture = culture;
493 filename = g_strconcat (name, ".dll", NULL);
494 len = strlen (filename);
496 for (path = search_path; *path; path++) {
498 continue; /* Ignore empty ApplicationBase */
500 /* See test cases in bug #58992 and bug #57710 */
501 /* 1st try: [culture]/[name].dll (culture may be empty) */
502 strcpy (filename + len - 4, ".dll");
503 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
506 /* 2nd try: [culture]/[name].exe (culture may be empty) */
507 strcpy (filename + len - 4, ".exe");
508 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
511 /* 3rd try: [culture]/[name]/[name].dll (culture may be empty) */
512 strcpy (filename + len - 4, ".dll");
513 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
516 /* 4th try: [culture]/[name]/[name].exe (culture may be empty) */
517 strcpy (filename + len - 4, ".exe");
518 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
527 * Try to load referenced assemblies from assemblies_path.
529 static MonoAssembly *
530 pedump_preload (MonoAssemblyName *aname,
531 gchar **assemblies_path,
534 MonoAssembly *result = NULL;
535 gboolean refonly = GPOINTER_TO_UINT (user_data);
537 if (assemblies_path && assemblies_path [0] != NULL) {
538 result = real_load (assemblies_path, aname->culture, aname->name, refonly);
544 static GList *loaded_assemblies = NULL;
547 pedump_assembly_load_hook (MonoAssembly *assembly, gpointer user_data)
549 loaded_assemblies = g_list_prepend (loaded_assemblies, assembly);
552 static MonoAssembly *
553 pedump_assembly_search_hook (MonoAssemblyName *aname, gpointer user_data)
557 for (tmp = loaded_assemblies; tmp; tmp = tmp->next) {
558 MonoAssembly *ass = tmp->data;
559 if (mono_assembly_names_equal (aname, &ass->aname))
565 #define VALID_ONLY_FLAG 0x08000000
566 #define VERIFY_CODE_ONLY MONO_VERIFY_ALL + 1
567 #define VERIFY_METADATA_ONLY VERIFY_CODE_ONLY + 1
570 main (int argc, char *argv [])
575 MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_VERIFIABLE;
576 const char *flag_desc [] = {"error", "warn", "cls", "all", "code", "fail-on-verifiable", "non-strict", "valid-only", "metadata", NULL};
577 guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL, VERIFY_CODE_ONLY, MONO_VERIFY_FAIL_FAST, MONO_VERIFY_NON_STRICT, VALID_ONLY_FLAG, VERIFY_METADATA_ONLY, 0};
578 int i, verify_flags = MONO_VERIFY_REPORT_ALL_ERRORS, run_new_metadata_verifier = 0;
580 for (i = 1; i < argc; i++){
581 if (argv [i][0] != '-'){
586 if (strcmp (argv [i], "--help") == 0)
588 else if (strcmp (argv [i], "--verify") == 0) {
601 mono_perfcounters_init ();
602 mono_metadata_init ();
604 mono_assemblies_init ();
608 char *tok = strtok (flags, ",");
613 for (i = 0; flag_desc [i]; ++i) {
614 if (strcmp (tok, flag_desc [i]) == 0) {
615 if (flag_vals [i] == VERIFY_CODE_ONLY) {
618 } else if(flag_vals [i] == MONO_VERIFY_ALL) {
620 } else if(flag_vals [i] == VERIFY_METADATA_ONLY) {
622 run_new_metadata_verifier = 1;
624 if (flag_vals [i] == VALID_ONLY_FLAG)
625 verifier_mode = MONO_VERIFIER_MODE_VALID;
627 verify_flags |= flag_vals [i];
632 g_print ("Unknown verify flag %s\n", tok);
633 tok = strtok (NULL, ",");
636 mono_verifier_set_mode (verifier_mode);
640 if (run_new_metadata_verifier)
641 return verify_image_file (file);
643 image = mono_image_open (file, NULL);
645 fprintf (stderr, "Cannot open image %s\n", file);
650 dump_dotnet_iinfo (image);
652 MonoAssembly *assembly;
654 mono_install_assembly_load_hook (pedump_assembly_load_hook, NULL);
655 mono_install_assembly_search_hook (pedump_assembly_search_hook, NULL);
657 mono_init_from_assembly (file, file);
659 mono_install_assembly_preload_hook (pedump_preload, GUINT_TO_POINTER (FALSE));
661 mono_marshal_init ();
663 assembly = mono_assembly_open (file, NULL);
666 g_print ("Could not open assembly %s\n", file);
670 return dump_verify_info (assembly->image, verify_flags);
672 mono_image_close (image);