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>
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>
25 gboolean dump_data = TRUE;
26 gboolean verify_pe = FALSE;
30 hex_dump (const char *buffer, int base, int count)
34 for (i = 0; i < count; i++){
36 printf ("\n0x%08x: ", (unsigned char) base + i);
38 printf ("%02x ", (unsigned char) (buffer [i]));
44 hex8 (const char *label, unsigned char x)
46 printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
50 hex16 (const char *label, guint16 x)
52 printf ("\t%s: 0x%04x\n", label, x);
56 hex32 (const char *label, guint32 x)
58 printf ("\t%s: 0x%08x\n", label, x);
62 dump_coff_header (MonoCOFFHeader *coff)
64 printf ("\nCOFF Header:\n");
65 hex16 (" Machine", coff->coff_machine);
66 hex16 (" Sections", coff->coff_sections);
67 hex32 (" Time stamp", coff->coff_time);
68 hex32 ("Pointer to Symbol Table", coff->coff_symptr);
69 hex32 (" Symbol Count", coff->coff_symcount);
70 hex16 (" Optional Header Size", coff->coff_opt_header_size);
71 hex16 (" Characteristics", coff->coff_attributes);
76 dump_pe_header (MonoPEHeader *pe)
78 printf ("\nPE Header:\n");
79 hex16 (" Magic (0x010b)", pe->pe_magic);
80 hex8 (" LMajor (6)", pe->pe_major);
81 hex8 (" LMinor (0)", pe->pe_minor);
82 hex32 (" Code Size", pe->pe_code_size);
83 hex32 (" Initialized Data Size", pe->pe_data_size);
84 hex32 ("Uninitialized Data Size", pe->pe_uninit_data_size);
85 hex32 (" Entry Point RVA", pe->pe_rva_entry_point);
86 hex32 (" Code Base RVA", pe->pe_rva_code_base);
87 hex32 (" Data Base RVA", pe->pe_rva_data_base);
92 dump_nt_header (MonoPEHeaderNT *nt)
94 printf ("\nNT Header:\n");
96 hex32 (" Image Base (0x400000)", nt->pe_image_base);
97 hex32 ("Section Alignment (8192)", nt->pe_section_align);
98 hex32 (" File Align (512/4096)", nt->pe_file_alignment);
99 hex16 (" OS Major (4)", nt->pe_os_major);
100 hex16 (" OS Minor (0)", nt->pe_os_minor);
101 hex16 (" User Major (0)", nt->pe_user_major);
102 hex16 (" User Minor (0)", nt->pe_user_minor);
103 hex16 (" Subsys major (4)", nt->pe_subsys_major);
104 hex16 (" Subsys minor (0)", nt->pe_subsys_minor);
105 hex32 (" Reserverd", nt->pe_reserved_1);
106 hex32 (" Image Size", nt->pe_image_size);
107 hex32 (" Header Size", nt->pe_header_size);
108 hex32 (" Checksum (0)", nt->pe_checksum);
109 hex16 (" Subsystem", nt->pe_subsys_required);
110 hex16 (" DLL Flags (0)", nt->pe_dll_flags);
111 hex32 (" Stack Reserve Size (1M)", nt->pe_stack_reserve);
112 hex32 ("Stack commit Size (4096)", nt->pe_stack_commit);
113 hex32 (" Heap Reserve Size (1M)", nt->pe_heap_reserve);
114 hex32 (" Heap Commit Size (4096)", nt->pe_heap_commit);
115 hex32 (" Loader flags (0x1)", nt->pe_loader_flags);
116 hex32 (" Data Directories (16)", nt->pe_data_dir_count);
120 dent (const char *label, MonoPEDirEntry de)
122 printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
126 dump_datadir (MonoPEDatadir *dd)
128 printf ("\nData directories:\n");
129 dent (" Export Table", dd->pe_export_table);
130 dent (" Import Table", dd->pe_import_table);
131 dent (" Resource Table", dd->pe_resource_table);
132 dent (" Exception Table", dd->pe_exception_table);
133 dent ("Certificate Table", dd->pe_certificate_table);
134 dent (" Reloc Table", dd->pe_reloc_table);
135 dent (" Debug", dd->pe_debug);
136 dent (" Copyright", dd->pe_copyright);
137 dent (" Global Ptr", dd->pe_global_ptr);
138 dent (" TLS Table", dd->pe_tls_table);
139 dent ("Load Config Table", dd->pe_load_config_table);
140 dent (" Bound Import", dd->pe_bound_import);
141 dent (" IAT", dd->pe_iat);
142 dent ("Delay Import Desc", dd->pe_delay_import_desc);
143 dent (" CLI Header", dd->pe_cli_header);
147 dump_dotnet_header (MonoDotNetHeader *header)
149 dump_coff_header (&header->coff);
150 dump_pe_header (&header->pe);
151 dump_nt_header (&header->nt);
152 dump_datadir (&header->datadir);
156 dump_section_table (MonoSectionTable *st)
158 guint32 flags = st->st_flags;
160 printf ("\n\tName: %s\n", st->st_name);
161 hex32 (" Virtual Size", st->st_virtual_size);
162 hex32 ("Virtual Address", st->st_virtual_address);
163 hex32 (" Raw Data Size", st->st_raw_data_size);
164 hex32 (" Raw Data Ptr", st->st_raw_data_ptr);
165 hex32 (" Reloc Ptr", st->st_reloc_ptr);
166 hex32 (" LineNo Ptr", st->st_lineno_ptr);
167 hex16 (" Reloc Count", st->st_reloc_count);
168 hex16 (" Line Count", st->st_line_count);
170 printf ("\tFlags: %s%s%s%s%s%s%s%s%s%s\n",
171 (flags & SECT_FLAGS_HAS_CODE) ? "code, " : "",
172 (flags & SECT_FLAGS_HAS_INITIALIZED_DATA) ? "data, " : "",
173 (flags & SECT_FLAGS_HAS_UNINITIALIZED_DATA) ? "bss, " : "",
174 (flags & SECT_FLAGS_MEM_DISCARDABLE) ? "discard, " : "",
175 (flags & SECT_FLAGS_MEM_NOT_CACHED) ? "nocache, " : "",
176 (flags & SECT_FLAGS_MEM_NOT_PAGED) ? "nopage, " : "",
177 (flags & SECT_FLAGS_MEM_SHARED) ? "shared, " : "",
178 (flags & SECT_FLAGS_MEM_EXECUTE) ? "exec, " : "",
179 (flags & SECT_FLAGS_MEM_READ) ? "read, " : "",
180 (flags & SECT_FLAGS_MEM_WRITE) ? "write" : "");
184 dump_sections (MonoCLIImageInfo *iinfo)
186 const int top = iinfo->cli_header.coff.coff_sections;
189 for (i = 0; i < top; i++)
190 dump_section_table (&iinfo->cli_section_tables [i]);
194 dump_cli_header (MonoCLIHeader *ch)
197 printf (" CLI header size: %d\n", ch->ch_size);
198 printf (" Runtime required: %d.%d\n", ch->ch_runtime_major, ch->ch_runtime_minor);
199 printf (" Flags: %s, %s, %s\n",
200 (ch->ch_flags & CLI_FLAGS_ILONLY ? "ilonly" : "contains native"),
201 (ch->ch_flags & CLI_FLAGS_32BITREQUIRED ? "32bits" : "32/64"),
202 (ch->ch_flags & CLI_FLAGS_ILONLY ? "trackdebug" : "no-trackdebug"));
203 dent (" Metadata", ch->ch_metadata);
204 hex32 ("Entry Point Token", ch->ch_entry_point);
205 dent (" Resources at", ch->ch_resources);
206 dent (" Strong Name at", ch->ch_strong_name);
207 dent (" Code Manager at", ch->ch_code_manager_table);
208 dent (" VTableFixups at", ch->ch_vtable_fixups);
209 dent (" EAT jumps at", ch->ch_export_address_table_jumps);
213 dsh (const char *label, MonoImage *meta, MonoStreamHeader *sh)
215 printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
217 sh->data - meta->raw_metadata, sh->data + sh->size - meta->raw_metadata,
222 dump_metadata_ptrs (MonoImage *meta)
224 printf ("\nMetadata pointers:\n");
225 dsh ("\tTables (#~)", meta, &meta->heap_tables);
226 dsh ("\t Strings", meta, &meta->heap_strings);
227 dsh ("\t Blob", meta, &meta->heap_blob);
228 dsh ("\tUser string", meta, &meta->heap_us);
229 dsh ("\t GUID", meta, &meta->heap_guid);
233 dump_metadata (MonoImage *meta)
237 dump_metadata_ptrs (meta);
240 for (table = 0; table < 64; table++){
241 if (meta->tables [table].rows == 0)
243 printf ("Table %s: %d records (%d bytes, at %p)\n",
244 mono_meta_table_name (table),
245 meta->tables [table].rows,
246 meta->tables [table].row_size,
247 meta->tables [table].base
253 dump_methoddef (MonoImage *metadata, guint32 token)
259 loc = mono_metadata_locate_token (metadata, token);
261 printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
265 dump_dotnet_iinfo (MonoImage *image)
267 MonoCLIImageInfo *iinfo = image->image_info;
269 dump_dotnet_header (&iinfo->cli_header);
270 dump_sections (iinfo);
271 dump_cli_header (&iinfo->cli_cli_header);
272 dump_metadata (image);
274 dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
278 dump_verify_info (MonoImage *image, int flags)
280 GSList *errors, *tmp;
282 const char* desc [] = {
283 "Ok", "Error", "Warning", NULL, "CLS"
286 errors = mono_image_verify_tables (image, flags);
288 for (tmp = errors; tmp; tmp = tmp->next) {
289 MonoVerifyInfo *info = tmp->data;
290 g_print ("%s: %s\n", desc [info->status], info->message);
291 if (info->status == MONO_VERIFY_ERROR)
294 mono_free_verify_list (errors);
296 if (flags & (MONO_VERIFY_ALL + 1)) { /* verify code */
298 MonoTableInfo *m = &image->tables [MONO_TABLE_METHOD];
300 for (i = 0; i < m->rows; ++i) {
303 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i+1), NULL);
304 errors = mono_method_verify (method, flags);
307 sig = mono_signature_get_desc (method->signature, FALSE);
308 g_print ("In method: %s.%s::%s(%s)\n", method->klass->name_space, method->klass->name, method->name, sig);
312 for (tmp = errors; tmp; tmp = tmp->next) {
313 MonoVerifyInfo *info = tmp->data;
314 g_print ("%s: %s\n", desc [info->status], info->message);
315 if (info->status == MONO_VERIFY_ERROR)
318 mono_free_verify_list (errors);
323 g_print ("Error count: %d\n", count);
329 printf ("Usage is: pedump [--verify error,warn,cls,all,code] file.exe\n");
334 main (int argc, char *argv [])
339 const char *flag_desc [] = {"error", "warn", "cls", "all", "code", NULL};
340 guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL, MONO_VERIFY_ALL + 1};
343 for (i = 1; i < argc; i++){
344 if (argv [i][0] != '-'){
349 if (strcmp (argv [i], "--help") == 0)
351 else if (strcmp (argv [i], "--verify") == 0) {
364 image = mono_image_open (file, NULL);
366 fprintf (stderr, "Can not open image %s\n", file);
371 dump_dotnet_iinfo (image);
374 char *tok = strtok (flags, ",");
375 MonoAssembly *assembly;
377 for (i = 0; flag_desc [i]; ++i) {
378 if (strcmp (tok, flag_desc [i]) == 0) {
384 g_print ("Unknown verify flag %s\n", tok);
385 tok = strtok (NULL, ",");
388 assembly = mono_assembly_open (file, NULL);
389 dump_verify_info (assembly->image, f);
392 mono_image_close (image);