2 * pedump.c: Dumps the contents of an extended PE/COFF file
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
16 #include "mono-endian.h"
18 #include <mono/metadata/class.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/tokentype.h>
21 #include <mono/metadata/appdomain.h>
22 #include <mono/metadata/assembly.h>
23 #include <mono/metadata/metadata-internals.h>
24 #include <mono/metadata/class-internals.h>
25 #include <mono/metadata/verify-internals.h>
26 #include "mono/utils/mono-digest.h"
27 #include <mono/utils/mono-mmap.h>
28 #include <sys/types.h>
34 gboolean dump_data = TRUE;
35 gboolean verify_pe = FALSE;
36 gboolean verify_metadata = FALSE;
37 gboolean verify_code = FALSE;
41 hex_dump (const char *buffer, int base, int count)
45 for (i = 0; i < count; i++){
47 printf ("\n0x%08x: ", (unsigned char) base + i);
49 printf ("%02x ", (unsigned char) (buffer [i]));
55 hex8 (const char *label, unsigned char x)
57 printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
61 hex16 (const char *label, guint16 x)
63 printf ("\t%s: 0x%04x\n", label, x);
67 hex32 (const char *label, guint32 x)
69 printf ("\t%s: 0x%08x\n", label, x);
73 dump_coff_header (MonoCOFFHeader *coff)
75 printf ("\nCOFF Header:\n");
76 hex16 (" Machine", coff->coff_machine);
77 hex16 (" Sections", coff->coff_sections);
78 hex32 (" Time stamp", coff->coff_time);
79 hex32 ("Pointer to Symbol Table", coff->coff_symptr);
80 hex32 (" Symbol Count", coff->coff_symcount);
81 hex16 (" Optional Header Size", coff->coff_opt_header_size);
82 hex16 (" Characteristics", coff->coff_attributes);
87 dump_pe_header (MonoPEHeader *pe)
89 printf ("\nPE Header:\n");
90 hex16 (" Magic (0x010b)", pe->pe_magic);
91 hex8 (" LMajor (6)", pe->pe_major);
92 hex8 (" LMinor (0)", pe->pe_minor);
93 hex32 (" Code Size", pe->pe_code_size);
94 hex32 (" Initialized Data Size", pe->pe_data_size);
95 hex32 ("Uninitialized Data Size", pe->pe_uninit_data_size);
96 hex32 (" Entry Point RVA", pe->pe_rva_entry_point);
97 hex32 (" Code Base RVA", pe->pe_rva_code_base);
98 hex32 (" Data Base RVA", pe->pe_rva_data_base);
103 dump_nt_header (MonoPEHeaderNT *nt)
105 printf ("\nNT Header:\n");
107 hex32 (" Image Base (0x400000)", nt->pe_image_base);
108 hex32 ("Section Alignment (8192)", nt->pe_section_align);
109 hex32 (" File Align (512/4096)", nt->pe_file_alignment);
110 hex16 (" OS Major (4)", nt->pe_os_major);
111 hex16 (" OS Minor (0)", nt->pe_os_minor);
112 hex16 (" User Major (0)", nt->pe_user_major);
113 hex16 (" User Minor (0)", nt->pe_user_minor);
114 hex16 (" Subsys major (4)", nt->pe_subsys_major);
115 hex16 (" Subsys minor (0)", nt->pe_subsys_minor);
116 hex32 (" Reserverd", nt->pe_reserved_1);
117 hex32 (" Image Size", nt->pe_image_size);
118 hex32 (" Header Size", nt->pe_header_size);
119 hex32 (" Checksum (0)", nt->pe_checksum);
120 hex16 (" Subsystem", nt->pe_subsys_required);
121 hex16 (" DLL Flags (0)", nt->pe_dll_flags);
122 hex32 (" Stack Reserve Size (1M)", nt->pe_stack_reserve);
123 hex32 ("Stack commit Size (4096)", nt->pe_stack_commit);
124 hex32 (" Heap Reserve Size (1M)", nt->pe_heap_reserve);
125 hex32 (" Heap Commit Size (4096)", nt->pe_heap_commit);
126 hex32 (" Loader flags (0x1)", nt->pe_loader_flags);
127 hex32 (" Data Directories (16)", nt->pe_data_dir_count);
131 dent (const char *label, MonoPEDirEntry de)
133 printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
137 dump_blob (const char *desc, const char* p, guint32 size)
147 for (i = 0; i < size; ++i) {
150 printf (" %02X", p [i] & 0xFF);
156 dump_public_key (MonoImage *m)
161 p = mono_image_get_public_key (m, &size);
162 dump_blob ("\nPublic key:", p, size);
166 dump_strong_name (MonoImage *m)
171 p = mono_image_get_strong_name (m, &size);
172 dump_blob ("\nStrong name:", p, size);
176 dump_datadir (MonoPEDatadir *dd)
178 printf ("\nData directories:\n");
179 dent (" Export Table", dd->pe_export_table);
180 dent (" Import Table", dd->pe_import_table);
181 dent (" Resource Table", dd->pe_resource_table);
182 dent (" Exception Table", dd->pe_exception_table);
183 dent ("Certificate Table", dd->pe_certificate_table);
184 dent (" Reloc Table", dd->pe_reloc_table);
185 dent (" Debug", dd->pe_debug);
186 dent (" Copyright", dd->pe_copyright);
187 dent (" Global Ptr", dd->pe_global_ptr);
188 dent (" TLS Table", dd->pe_tls_table);
189 dent ("Load Config Table", dd->pe_load_config_table);
190 dent (" Bound Import", dd->pe_bound_import);
191 dent (" IAT", dd->pe_iat);
192 dent ("Delay Import Desc", dd->pe_delay_import_desc);
193 dent (" CLI Header", dd->pe_cli_header);
197 dump_dotnet_header (MonoDotNetHeader *header)
199 dump_coff_header (&header->coff);
200 dump_pe_header (&header->pe);
201 dump_nt_header (&header->nt);
202 dump_datadir (&header->datadir);
206 dump_section_table (MonoSectionTable *st)
208 guint32 flags = st->st_flags;
210 printf ("\n\tName: %s\n", st->st_name);
211 hex32 (" Virtual Size", st->st_virtual_size);
212 hex32 ("Virtual Address", st->st_virtual_address);
213 hex32 (" Raw Data Size", st->st_raw_data_size);
214 hex32 (" Raw Data Ptr", st->st_raw_data_ptr);
215 hex32 (" Reloc Ptr", st->st_reloc_ptr);
216 hex32 (" LineNo Ptr", st->st_lineno_ptr);
217 hex16 (" Reloc Count", st->st_reloc_count);
218 hex16 (" Line Count", st->st_line_count);
220 printf ("\tFlags: %s%s%s%s%s%s%s%s%s%s\n",
221 (flags & SECT_FLAGS_HAS_CODE) ? "code, " : "",
222 (flags & SECT_FLAGS_HAS_INITIALIZED_DATA) ? "data, " : "",
223 (flags & SECT_FLAGS_HAS_UNINITIALIZED_DATA) ? "bss, " : "",
224 (flags & SECT_FLAGS_MEM_DISCARDABLE) ? "discard, " : "",
225 (flags & SECT_FLAGS_MEM_NOT_CACHED) ? "nocache, " : "",
226 (flags & SECT_FLAGS_MEM_NOT_PAGED) ? "nopage, " : "",
227 (flags & SECT_FLAGS_MEM_SHARED) ? "shared, " : "",
228 (flags & SECT_FLAGS_MEM_EXECUTE) ? "exec, " : "",
229 (flags & SECT_FLAGS_MEM_READ) ? "read, " : "",
230 (flags & SECT_FLAGS_MEM_WRITE) ? "write" : "");
234 dump_sections (MonoCLIImageInfo *iinfo)
236 const int top = iinfo->cli_header.coff.coff_sections;
239 for (i = 0; i < top; i++)
240 dump_section_table (&iinfo->cli_section_tables [i]);
244 dump_cli_header (MonoCLIHeader *ch)
247 printf (" CLI header size: %d\n", ch->ch_size);
248 printf (" Runtime required: %d.%d\n", ch->ch_runtime_major, ch->ch_runtime_minor);
249 printf (" Flags: %s, %s, %s, %s\n",
250 (ch->ch_flags & CLI_FLAGS_ILONLY ? "ilonly" : "contains native"),
251 (ch->ch_flags & CLI_FLAGS_32BITREQUIRED ? "32bits" : "32/64"),
252 (ch->ch_flags & CLI_FLAGS_ILONLY ? "trackdebug" : "no-trackdebug"),
253 (ch->ch_flags & CLI_FLAGS_STRONGNAMESIGNED ? "strongnamesigned" : "notsigned"));
254 dent (" Metadata", ch->ch_metadata);
255 hex32 ("Entry Point Token", ch->ch_entry_point);
256 dent (" Resources at", ch->ch_resources);
257 dent (" Strong Name at", ch->ch_strong_name);
258 dent (" Code Manager at", ch->ch_code_manager_table);
259 dent (" VTableFixups at", ch->ch_vtable_fixups);
260 dent (" EAT jumps at", ch->ch_export_address_table_jumps);
264 dsh (const char *label, MonoImage *meta, MonoStreamHeader *sh)
266 printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
268 (int)(sh->data - meta->raw_metadata), (int)(sh->data + sh->size - meta->raw_metadata),
273 dump_metadata_header (MonoImage *meta)
275 printf ("\nMetadata header:\n");
276 printf (" Version: %d.%d\n", meta->md_version_major, meta->md_version_minor);
277 printf (" Version string: %s\n", meta->version);
281 dump_metadata_ptrs (MonoImage *meta)
283 printf ("\nMetadata pointers:\n");
284 dsh ("\tTables (#~)", meta, &meta->heap_tables);
285 dsh ("\t Strings", meta, &meta->heap_strings);
286 dsh ("\t Blob", meta, &meta->heap_blob);
287 dsh ("\tUser string", meta, &meta->heap_us);
288 dsh ("\t GUID", meta, &meta->heap_guid);
292 dump_metadata (MonoImage *meta)
296 dump_metadata_header (meta);
298 dump_metadata_ptrs (meta);
301 for (table = 0; table < MONO_TABLE_NUM; table++){
302 if (meta->tables [table].rows == 0)
304 printf ("Table %s: %d records (%d bytes, at %p)\n",
305 mono_meta_table_name (table),
306 meta->tables [table].rows,
307 meta->tables [table].row_size,
308 meta->tables [table].base
314 dump_methoddef (MonoImage *metadata, guint32 token)
320 loc = mono_metadata_locate_token (metadata, token);
322 printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
326 dump_dotnet_iinfo (MonoImage *image)
328 MonoCLIImageInfo *iinfo = image->image_info;
330 dump_dotnet_header (&iinfo->cli_header);
331 dump_sections (iinfo);
332 dump_cli_header (&iinfo->cli_cli_header);
333 dump_strong_name (image);
334 dump_public_key (image);
335 dump_metadata (image);
337 dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
341 dump_verify_info (MonoImage *image, int flags)
343 GSList *errors, *tmp;
344 int count = 0, verifiable = 0;
345 const char* desc [] = {
346 "Ok", "Error", "Warning", NULL, "CLS", NULL, NULL, NULL, "Not Verifiable"
349 if (verify_metadata) {
350 errors = mono_image_verify_tables (image, flags);
352 for (tmp = errors; tmp; tmp = tmp->next) {
353 MonoVerifyInfo *info = tmp->data;
354 g_print ("%s: %s\n", desc [info->status], info->message);
355 if (info->status == MONO_VERIFY_ERROR)
358 mono_free_verify_list (errors);
361 if (verify_code) { /* verify code */
363 MonoTableInfo *m = &image->tables [MONO_TABLE_METHOD];
365 for (i = 0; i < m->rows; ++i) {
367 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i+1), NULL);
369 g_print ("Warning: Cannot lookup method with token 0x%08x\n", i + 1);
372 errors = mono_method_verify (method, flags);
375 MonoClass *klass = mono_method_get_class (method);
376 sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
377 name = mono_type_full_name (&klass->byval_arg);
378 g_print ("In method: %s::%s(%s)\n", name, mono_method_get_name (method), sig);
383 for (tmp = errors; tmp; tmp = tmp->next) {
384 MonoVerifyInfo *info = tmp->data;
385 g_print ("%s: %s\n", desc [info->status], info->message);
386 if (info->status == MONO_VERIFY_ERROR) {
390 if(info->status == MONO_VERIFY_NOT_VERIFIABLE) {
395 mono_free_verify_list (errors);
400 g_print ("Error count: %d\n", count);
407 printf ("Usage is: pedump [--verify error,warn,cls,all,code,fail-on-verifiable,non-strict,valid-only,metadata] file.exe\n");
412 verify_image_file (const char *fname)
415 struct stat stat_buf;
416 void *raw_data_handle;
418 GSList *errors, *tmp;
420 const char* desc [] = {
421 "Ok", "Error", "Warning", NULL, "CLS", NULL, NULL, NULL, "Not Verifiable"
424 if ((filed = fopen (fname, "rb")) == NULL) {
425 fprintf (stderr, "Cannot open file %s\n", fname);
429 if (fstat (fileno (filed), &stat_buf)) {
431 fprintf (stderr, "Cannot stat file %s\n", fname);
435 raw_data = mono_file_map (stat_buf.st_size, MONO_MMAP_READ|MONO_MMAP_PRIVATE, fileno (filed), 0, &raw_data_handle);
438 fprintf (stderr, "Could not mmap file %s\n", fname);
443 errors = mono_image_verify (raw_data, stat_buf.st_size);
444 mono_file_unmap (raw_data, raw_data_handle);
449 for (tmp = errors; tmp; tmp = tmp->next) {
450 MonoVerifyInfo *info = tmp->data;
451 g_print ("%s: %s\n", desc [info->status], info->message);
452 if (info->status == MONO_VERIFY_ERROR)
455 mono_free_verify_list (errors);
457 g_print ("Error count: %d\n", count);
459 return count > 0 ? 1 : 0;
463 try_load_from (MonoAssembly **assembly, const gchar *path1, const gchar *path2,
464 const gchar *path3, const gchar *path4, gboolean refonly)
469 fullpath = g_build_filename (path1, path2, path3, path4, NULL);
470 if (g_file_test (fullpath, G_FILE_TEST_IS_REGULAR))
471 *assembly = mono_assembly_open_full (fullpath, NULL, refonly);
474 return (*assembly != NULL);
477 static MonoAssembly *
478 real_load (gchar **search_path, const gchar *culture, const gchar *name, gboolean refonly)
480 MonoAssembly *result = NULL;
483 const gchar *local_culture;
486 if (!culture || *culture == '\0') {
489 local_culture = culture;
492 filename = g_strconcat (name, ".dll", NULL);
493 len = strlen (filename);
495 for (path = search_path; *path; path++) {
497 continue; /* Ignore empty ApplicationBase */
499 /* See test cases in bug #58992 and bug #57710 */
500 /* 1st try: [culture]/[name].dll (culture may be empty) */
501 strcpy (filename + len - 4, ".dll");
502 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
505 /* 2nd try: [culture]/[name].exe (culture may be empty) */
506 strcpy (filename + len - 4, ".exe");
507 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
510 /* 3rd try: [culture]/[name]/[name].dll (culture may be empty) */
511 strcpy (filename + len - 4, ".dll");
512 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
515 /* 4th try: [culture]/[name]/[name].exe (culture may be empty) */
516 strcpy (filename + len - 4, ".exe");
517 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
526 * Try to load referenced assemblies from assemblies_path.
528 static MonoAssembly *
529 pedump_preload (MonoAssemblyName *aname,
530 gchar **assemblies_path,
533 MonoAssembly *result = NULL;
534 gboolean refonly = GPOINTER_TO_UINT (user_data);
536 if (assemblies_path && assemblies_path [0] != NULL) {
537 result = real_load (assemblies_path, aname->culture, aname->name, refonly);
543 static GList *loaded_assemblies = NULL;
546 pedump_assembly_load_hook (MonoAssembly *assembly, gpointer user_data)
548 loaded_assemblies = g_list_prepend (loaded_assemblies, assembly);
551 static MonoAssembly *
552 pedump_assembly_search_hook (MonoAssemblyName *aname, gpointer user_data)
556 for (tmp = loaded_assemblies; tmp; tmp = tmp->next) {
557 MonoAssembly *ass = tmp->data;
558 if (mono_assembly_names_equal (aname, &ass->aname))
564 #define VALID_ONLY_FLAG 0x08000000
565 #define VERIFY_CODE_ONLY MONO_VERIFY_ALL + 1
566 #define VERIFY_METADATA_ONLY VERIFY_CODE_ONLY + 1
569 main (int argc, char *argv [])
574 MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_VERIFIABLE;
575 const char *flag_desc [] = {"error", "warn", "cls", "all", "code", "fail-on-verifiable", "non-strict", "valid-only", "metadata", NULL};
576 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};
577 int i, verify_flags = MONO_VERIFY_REPORT_ALL_ERRORS, run_new_metadata_verifier = 0;
579 for (i = 1; i < argc; i++){
580 if (argv [i][0] != '-'){
585 if (strcmp (argv [i], "--help") == 0)
587 else if (strcmp (argv [i], "--verify") == 0) {
600 mono_perfcounters_init ();
601 mono_metadata_init ();
603 mono_assemblies_init ();
607 char *tok = strtok (flags, ",");
612 for (i = 0; flag_desc [i]; ++i) {
613 if (strcmp (tok, flag_desc [i]) == 0) {
614 if (flag_vals [i] == VERIFY_CODE_ONLY) {
617 } else if(flag_vals [i] == MONO_VERIFY_ALL) {
619 } else if(flag_vals [i] == VERIFY_METADATA_ONLY) {
621 run_new_metadata_verifier = 1;
623 if (flag_vals [i] == VALID_ONLY_FLAG)
624 verifier_mode = MONO_VERIFIER_MODE_VALID;
626 verify_flags |= flag_vals [i];
631 g_print ("Unknown verify flag %s\n", tok);
632 tok = strtok (NULL, ",");
635 mono_verifier_set_mode (verifier_mode);
639 if (run_new_metadata_verifier)
640 return verify_image_file (file);
642 image = mono_image_open (file, NULL);
644 fprintf (stderr, "Cannot open image %s\n", file);
649 dump_dotnet_iinfo (image);
651 MonoAssembly *assembly;
653 mono_install_assembly_load_hook (pedump_assembly_load_hook, NULL);
654 mono_install_assembly_search_hook (pedump_assembly_search_hook, NULL);
656 mono_init_from_assembly (file, file);
658 mono_install_assembly_preload_hook (pedump_preload, GUINT_TO_POINTER (FALSE));
660 assembly = mono_assembly_open (file, NULL);
663 g_print ("Could not open assembly %s\n", file);
667 return dump_verify_info (assembly->image, verify_flags);
669 mono_image_close (image);