2 * pedump.c: Dumps the contents of an extended PE/COFF file
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
17 #include "mono-endian.h"
19 #include <mono/metadata/class.h>
21 gboolean dump_data = TRUE;
22 gboolean verify_pe = FALSE;
25 hex_dump (const char *buffer, int base, int count)
29 for (i = 0; i < count; i++){
31 printf ("\n0x%08x: ", (unsigned char) base + i);
33 printf ("%02x ", (unsigned char) (buffer [i]));
38 hex8 (const char *label, unsigned char x)
40 printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
44 hex16 (const char *label, guint16 x)
46 printf ("\t%s: 0x%04x\n", label, x);
50 hex32 (const char *label, guint32 x)
52 printf ("\t%s: 0x%08x\n", label, x);
56 dump_coff_header (MonoCOFFHeader *coff)
58 printf ("\nCOFF Header:\n");
59 hex16 (" Machine", coff->coff_machine);
60 hex16 (" Sections", coff->coff_sections);
61 hex32 (" Time stamp", coff->coff_time);
62 hex32 ("Pointer to Symbol Table", coff->coff_symptr);
63 hex32 (" Symbol Count", coff->coff_symcount);
64 hex16 (" Optional Header Size", coff->coff_opt_header_size);
65 hex16 (" Characteristics", coff->coff_attributes);
70 dump_pe_header (MonoPEHeader *pe)
72 printf ("\nPE Header:\n");
73 hex16 (" Magic (0x010b)", pe->pe_magic);
74 hex8 (" LMajor (6)", pe->pe_major);
75 hex8 (" LMinor (0)", pe->pe_minor);
76 hex32 (" Code Size", pe->pe_code_size);
77 hex32 (" Initialized Data Size", pe->pe_data_size);
78 hex32 ("Uninitialized Data Size", pe->pe_uninit_data_size);
79 hex32 (" Entry Point RVA", pe->pe_rva_entry_point);
80 hex32 (" Code Base RVA", pe->pe_rva_code_base);
81 hex32 (" Data Base RVA", pe->pe_rva_data_base);
86 dump_nt_header (MonoPEHeaderNT *nt)
88 printf ("\nNT Header:\n");
90 hex32 (" Image Base (0x400000)", nt->pe_image_base);
91 hex32 ("Section Alignment (8192)", nt->pe_section_align);
92 hex32 (" File Align (512/4096)", nt->pe_file_alignment);
93 hex16 (" OS Major (4)", nt->pe_os_major);
94 hex16 (" OS Minor (0)", nt->pe_os_minor);
95 hex16 (" User Major (0)", nt->pe_user_major);
96 hex16 (" User Minor (0)", nt->pe_user_minor);
97 hex16 (" Subsys major (4)", nt->pe_subsys_major);
98 hex16 (" Subsys minor (0)", nt->pe_subsys_minor);
99 hex32 (" Reserverd", nt->pe_reserved_1);
100 hex32 (" Image Size", nt->pe_image_size);
101 hex32 (" Header Size", nt->pe_header_size);
102 hex32 (" Checksum (0)", nt->pe_checksum);
103 hex16 (" Subsystem", nt->pe_subsys_required);
104 hex16 (" DLL Flags (0)", nt->pe_dll_flags);
105 hex32 (" Stack Reserve Size (1M)", nt->pe_stack_reserve);
106 hex32 ("Stack commit Size (4096)", nt->pe_stack_commit);
107 hex32 (" Heap Reserve Size (1M)", nt->pe_heap_reserve);
108 hex32 (" Heap Commit Size (4096)", nt->pe_heap_commit);
109 hex32 (" Loader flags (0x1)", nt->pe_loader_flags);
110 hex32 (" Data Directories (16)", nt->pe_data_dir_count);
114 dent (const char *label, MonoPEDirEntry de)
116 printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
120 dump_datadir (MonoPEDatadir *dd)
122 printf ("\nData directories:\n");
123 dent (" Export Table", dd->pe_export_table);
124 dent (" Import Table", dd->pe_import_table);
125 dent (" Resource Table", dd->pe_resource_table);
126 dent (" Exception Table", dd->pe_exception_table);
127 dent ("Certificate Table", dd->pe_certificate_table);
128 dent (" Reloc Table", dd->pe_reloc_table);
129 dent (" Debug", dd->pe_debug);
130 dent (" Copyright", dd->pe_copyright);
131 dent (" Global Ptr", dd->pe_global_ptr);
132 dent (" TLS Table", dd->pe_tls_table);
133 dent ("Load Config Table", dd->pe_load_config_table);
134 dent (" Bound Import", dd->pe_bound_import);
135 dent (" IAT", dd->pe_iat);
136 dent ("Delay Import Desc", dd->pe_delay_import_desc);
137 dent (" CLI Header", dd->pe_cli_header);
141 dump_dotnet_header (MonoDotNetHeader *header)
143 dump_coff_header (&header->coff);
144 dump_pe_header (&header->pe);
145 dump_nt_header (&header->nt);
146 dump_datadir (&header->datadir);
150 dump_section_table (MonoSectionTable *st)
152 guint32 flags = st->st_flags;
154 printf ("\n\tName: %s\n", st->st_name);
155 hex32 (" Virtual Size", st->st_virtual_size);
156 hex32 ("Virtual Address", st->st_virtual_address);
157 hex32 (" Raw Data Size", st->st_raw_data_size);
158 hex32 (" Raw Data Ptr", st->st_raw_data_ptr);
159 hex32 (" Reloc Ptr", st->st_reloc_ptr);
160 hex32 (" LineNo Ptr", st->st_lineno_ptr);
161 hex16 (" Reloc Count", st->st_reloc_count);
162 hex16 (" Line Count", st->st_line_count);
164 printf ("\tFlags: %s%s%s%s%s%s%s%s%s%s\n",
165 (flags & SECT_FLAGS_HAS_CODE) ? "code, " : "",
166 (flags & SECT_FLAGS_HAS_INITIALIZED_DATA) ? "data, " : "",
167 (flags & SECT_FLAGS_HAS_UNINITIALIZED_DATA) ? "bss, " : "",
168 (flags & SECT_FLAGS_MEM_DISCARDABLE) ? "discard, " : "",
169 (flags & SECT_FLAGS_MEM_NOT_CACHED) ? "nocache, " : "",
170 (flags & SECT_FLAGS_MEM_NOT_PAGED) ? "nopage, " : "",
171 (flags & SECT_FLAGS_MEM_SHARED) ? "shared, " : "",
172 (flags & SECT_FLAGS_MEM_EXECUTE) ? "exec, " : "",
173 (flags & SECT_FLAGS_MEM_READ) ? "read, " : "",
174 (flags & SECT_FLAGS_MEM_WRITE) ? "write" : "");
178 dump_sections (MonoCLIImageInfo *iinfo)
180 const int top = iinfo->cli_header.coff.coff_sections;
183 for (i = 0; i < top; i++)
184 dump_section_table (&iinfo->cli_section_tables [i]);
188 dump_cli_header (MonoCLIHeader *ch)
191 printf (" CLI header size: %d\n", ch->ch_size);
192 printf (" Runtime required: %d.%d\n", ch->ch_runtime_major, ch->ch_runtime_minor);
193 printf (" Flags: %s, %s, %s\n",
194 (ch->ch_flags & CLI_FLAGS_ILONLY ? "ilonly" : "contains native"),
195 (ch->ch_flags & CLI_FLAGS_32BITREQUIRED ? "32bits" : "32/64"),
196 (ch->ch_flags & CLI_FLAGS_ILONLY ? "trackdebug" : "no-trackdebug"));
197 dent (" Metadata", ch->ch_metadata);
198 hex32 ("Entry Point Token", ch->ch_entry_point);
199 dent (" Resources at", ch->ch_resources);
200 dent (" Strong Name at", ch->ch_strong_name);
201 dent (" Code Manager at", ch->ch_code_manager_table);
202 dent (" VTableFixups at", ch->ch_vtable_fixups);
203 dent (" EAT jumps at", ch->ch_export_address_table_jumps);
207 dsh (const char *label, MonoImage *meta, MonoStreamHeader *sh)
209 printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
211 sh->data - meta->raw_metadata, sh->data + sh->size - meta->raw_metadata,
216 dump_metadata_ptrs (MonoImage *meta)
218 printf ("\nMetadata pointers:\n");
219 dsh ("\tTables (#~)", meta, &meta->heap_tables);
220 dsh ("\t Strings", meta, &meta->heap_strings);
221 dsh ("\t Blob", meta, &meta->heap_blob);
222 dsh ("\tUser string", meta, &meta->heap_us);
223 dsh ("\t GUID", meta, &meta->heap_guid);
227 dump_metadata (MonoImage *meta)
231 dump_metadata_ptrs (meta);
234 for (table = 0; table < 64; table++){
235 if (meta->tables [table].rows == 0)
237 printf ("Table %s: %d records (%d bytes, at %p)\n",
238 mono_meta_table_name (table),
239 meta->tables [table].rows,
240 meta->tables [table].row_size,
241 meta->tables [table].base
247 dump_methoddef (MonoImage *metadata, guint32 token)
253 loc = mono_metadata_locate_token (metadata, token);
255 printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
259 dump_dotnet_iinfo (MonoImage *image)
261 MonoCLIImageInfo *iinfo = image->image_info;
263 dump_dotnet_header (&iinfo->cli_header);
264 dump_sections (iinfo);
265 dump_cli_header (&iinfo->cli_cli_header);
266 dump_metadata (image);
268 dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
272 dump_verify_info (MonoImage *image, int flags)
274 GSList *errors, *tmp;
275 const char* desc [] = {
276 "Ok", "Error", "Warning", NULL, "CLS"
279 errors = mono_image_verify_tables (image, flags);
281 for (tmp = errors; tmp; tmp = tmp->next) {
282 MonoVerifyInfo *info = tmp->data;
283 g_print ("%s: %s\n", desc [info->status], info->message);
285 mono_free_verify_list (errors);
291 printf ("Usage is: pedump [--verify error,warn,cls,all] file.exe\n");
296 main (int argc, char *argv [])
301 const char *flag_desc [] = {"error", "warn", "cls", "all", NULL};
302 guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL};
305 for (i = 1; i < argc; i++){
306 if (argv [i][0] != '-'){
311 if (strcmp (argv [i], "--help") == 0)
313 else if (strcmp (argv [i], "--verify") == 0) {
326 image = mono_image_open (file, NULL);
328 fprintf (stderr, "Can not open image %s\n", file);
333 dump_dotnet_iinfo (image);
336 char *tok = strtok (flags, ",");
338 for (i = 0; flag_desc [i]; ++i) {
339 if (strcmp (tok, flag_desc [i]) == 0) {
345 g_print ("Unknown verify flag %s\n", tok);
346 tok = strtok (NULL, ",");
348 dump_verify_info (image, f);
351 mono_image_close (image);