* list :
[mono.git] / mono / metadata / pedump.c
1 /*
2  * pedump.c: Dumps the contents of an extended PE/COFF file
3  *
4  * Author:
5  *   Miguel de Icaza (miguel@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9 #include <config.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "image.h"
14 #include <glib.h>
15 #include "cil-coff.h"
16 #include "private.h"
17 #include "mono-endian.h"
18 #include "verify.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/utils/mono-digest.h"
25
26 gboolean dump_data = TRUE;
27 gboolean verify_pe = FALSE;
28
29 /* unused
30 static void
31 hex_dump (const char *buffer, int base, int count)
32 {
33         int i;
34         
35         for (i = 0; i < count; i++){
36                 if ((i % 16) == 0)
37                         printf ("\n0x%08x: ", (unsigned char) base + i);
38
39                 printf ("%02x ", (unsigned char) (buffer [i]));
40         }
41 }
42 */
43
44 static void
45 hex8 (const char *label, unsigned char x)
46 {
47         printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
48 }
49
50 static void
51 hex16 (const char *label, guint16 x)
52 {
53         printf ("\t%s: 0x%04x\n", label, x);
54 }
55
56 static void
57 hex32 (const char *label, guint32 x)
58 {
59         printf ("\t%s: 0x%08x\n", label, x);
60 }
61
62 static void
63 dump_coff_header (MonoCOFFHeader *coff)
64 {
65         printf ("\nCOFF Header:\n");
66         hex16 ("                Machine", coff->coff_machine);
67         hex16 ("               Sections", coff->coff_sections);
68         hex32 ("             Time stamp", coff->coff_time);
69         hex32 ("Pointer to Symbol Table", coff->coff_symptr);
70         hex32 ("           Symbol Count", coff->coff_symcount);
71         hex16 ("   Optional Header Size", coff->coff_opt_header_size);
72         hex16 ("        Characteristics", coff->coff_attributes);
73
74 }
75
76 static void
77 dump_pe_header (MonoPEHeader *pe)
78 {
79         printf ("\nPE Header:\n");
80         hex16 ("         Magic (0x010b)", pe->pe_magic);
81         hex8  ("             LMajor (6)", pe->pe_major);
82         hex8  ("             LMinor (0)", pe->pe_minor);
83         hex32 ("              Code Size", pe->pe_code_size);
84         hex32 ("  Initialized Data Size", pe->pe_data_size);
85         hex32 ("Uninitialized Data Size", pe->pe_uninit_data_size);
86         hex32 ("        Entry Point RVA", pe->pe_rva_entry_point);
87         hex32 ("          Code Base RVA", pe->pe_rva_code_base);
88         hex32 ("          Data Base RVA", pe->pe_rva_data_base);
89         printf ("\n");
90 }
91
92 static void
93 dump_nt_header (MonoPEHeaderNT *nt)
94 {
95         printf ("\nNT Header:\n");
96
97         hex32 ("   Image Base (0x400000)", nt->pe_image_base);
98         hex32 ("Section Alignment (8192)", nt->pe_section_align);
99         hex32 ("   File Align (512/4096)", nt->pe_file_alignment);
100         hex16 ("            OS Major (4)", nt->pe_os_major);
101         hex16 ("            OS Minor (0)", nt->pe_os_minor);
102         hex16 ("          User Major (0)", nt->pe_user_major);
103         hex16 ("          User Minor (0)", nt->pe_user_minor);
104         hex16 ("        Subsys major (4)", nt->pe_subsys_major);
105         hex16 ("        Subsys minor (0)", nt->pe_subsys_minor);
106         hex32 ("               Reserverd", nt->pe_reserved_1);
107         hex32 ("              Image Size", nt->pe_image_size);
108         hex32 ("             Header Size", nt->pe_header_size);
109         hex32 ("            Checksum (0)", nt->pe_checksum);
110         hex16 ("               Subsystem", nt->pe_subsys_required);
111         hex16 ("           DLL Flags (0)", nt->pe_dll_flags);
112         hex32 (" Stack Reserve Size (1M)", nt->pe_stack_reserve);
113         hex32 ("Stack commit Size (4096)", nt->pe_stack_commit);
114         hex32 ("  Heap Reserve Size (1M)", nt->pe_heap_reserve);
115         hex32 (" Heap Commit Size (4096)", nt->pe_heap_commit);
116         hex32 ("      Loader flags (0x1)", nt->pe_loader_flags);
117         hex32 ("   Data Directories (16)", nt->pe_data_dir_count);
118 }
119
120 static void
121 dent (const char *label, MonoPEDirEntry de)
122 {
123         printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
124 }
125
126 static void
127 dump_blob (const char *desc, const char* p, guint32 size)
128 {
129         int i;
130
131         printf ("%s", desc);
132         if (!p) {
133                 printf (" none\n");
134                 return;
135         }
136
137         for (i = 0; i < size; ++i) {
138                 if (!(i % 16))
139                         printf ("\n\t");
140                 printf (" %02X", p [i] & 0xFF);
141         }
142         printf ("\n");
143 }
144
145 static void
146 dump_public_key (MonoImage *m)
147 {
148         guint32 size;
149         const char *p;
150
151         p = mono_image_get_public_key (m, &size);
152         dump_blob ("\nPublic key:", p, size);
153 }
154
155 static void
156 dump_strong_name (MonoImage *m)
157 {
158         guint32 size;
159         const char *p;
160
161         p = mono_image_get_strong_name (m, &size);
162         dump_blob ("\nStrong name:", p, size);
163 }
164
165 static void
166 dump_datadir (MonoPEDatadir *dd)
167 {
168         printf ("\nData directories:\n");
169         dent ("     Export Table", dd->pe_export_table);
170         dent ("     Import Table", dd->pe_import_table);
171         dent ("   Resource Table", dd->pe_resource_table);
172         dent ("  Exception Table", dd->pe_exception_table);
173         dent ("Certificate Table", dd->pe_certificate_table);
174         dent ("      Reloc Table", dd->pe_reloc_table);
175         dent ("            Debug", dd->pe_debug);
176         dent ("        Copyright", dd->pe_copyright);
177         dent ("       Global Ptr", dd->pe_global_ptr);
178         dent ("        TLS Table", dd->pe_tls_table);
179         dent ("Load Config Table", dd->pe_load_config_table);
180         dent ("     Bound Import", dd->pe_bound_import);
181         dent ("              IAT", dd->pe_iat);
182         dent ("Delay Import Desc", dd->pe_delay_import_desc);
183         dent ("       CLI Header", dd->pe_cli_header);
184 }
185
186 static void
187 dump_dotnet_header (MonoDotNetHeader *header)
188 {
189         dump_coff_header (&header->coff);
190         dump_pe_header (&header->pe);
191         dump_nt_header (&header->nt);
192         dump_datadir (&header->datadir);
193 }
194
195 static void
196 dump_section_table (MonoSectionTable *st)
197 {
198         guint32 flags = st->st_flags;
199                 
200         printf ("\n\tName: %s\n", st->st_name);
201         hex32 ("   Virtual Size", st->st_virtual_size);
202         hex32 ("Virtual Address", st->st_virtual_address);
203         hex32 ("  Raw Data Size", st->st_raw_data_size);
204         hex32 ("   Raw Data Ptr", st->st_raw_data_ptr);
205         hex32 ("      Reloc Ptr", st->st_reloc_ptr);
206         hex32 ("     LineNo Ptr", st->st_lineno_ptr);
207         hex16 ("    Reloc Count", st->st_reloc_count);
208         hex16 ("     Line Count", st->st_line_count);
209
210         printf ("\tFlags: %s%s%s%s%s%s%s%s%s%s\n",
211                 (flags & SECT_FLAGS_HAS_CODE) ? "code, " : "",
212                 (flags & SECT_FLAGS_HAS_INITIALIZED_DATA) ? "data, " : "",
213                 (flags & SECT_FLAGS_HAS_UNINITIALIZED_DATA) ? "bss, " : "",
214                 (flags & SECT_FLAGS_MEM_DISCARDABLE) ? "discard, " : "",
215                 (flags & SECT_FLAGS_MEM_NOT_CACHED) ? "nocache, " : "",
216                 (flags & SECT_FLAGS_MEM_NOT_PAGED) ? "nopage, " : "",
217                 (flags & SECT_FLAGS_MEM_SHARED) ? "shared, " : "",
218                 (flags & SECT_FLAGS_MEM_EXECUTE) ? "exec, " : "",
219                 (flags & SECT_FLAGS_MEM_READ) ? "read, " : "",
220                 (flags & SECT_FLAGS_MEM_WRITE) ? "write" : "");
221 }
222
223 static void
224 dump_sections (MonoCLIImageInfo *iinfo)
225 {
226         const int top = iinfo->cli_header.coff.coff_sections;
227         int i;
228         
229         for (i = 0; i < top; i++)
230                 dump_section_table (&iinfo->cli_section_tables [i]);
231 }
232
233 static void
234 dump_cli_header (MonoCLIHeader *ch)
235 {
236         printf ("\n");
237         printf ("          CLI header size: %d\n", ch->ch_size);
238         printf ("         Runtime required: %d.%d\n", ch->ch_runtime_major, ch->ch_runtime_minor);
239         printf ("                    Flags: %s, %s, %s\n",
240                 (ch->ch_flags & CLI_FLAGS_ILONLY ? "ilonly" : "contains native"),
241                 (ch->ch_flags & CLI_FLAGS_32BITREQUIRED ? "32bits" : "32/64"),
242                 (ch->ch_flags & CLI_FLAGS_ILONLY ? "trackdebug" : "no-trackdebug"));
243         dent   ("         Metadata", ch->ch_metadata);
244         hex32  ("Entry Point Token", ch->ch_entry_point);
245         dent   ("     Resources at", ch->ch_resources);
246         dent   ("   Strong Name at", ch->ch_strong_name);
247         dent   ("  Code Manager at", ch->ch_code_manager_table);
248         dent   ("  VTableFixups at", ch->ch_vtable_fixups);
249         dent   ("     EAT jumps at", ch->ch_export_address_table_jumps);
250 }       
251
252 static void
253 dsh (const char *label, MonoImage *meta, MonoStreamHeader *sh)
254 {
255         printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
256                 label,
257                 sh->data - meta->raw_metadata, sh->data + sh->size - meta->raw_metadata,
258                 sh->size, sh->size);
259 }
260
261 static void
262 dump_metadata_ptrs (MonoImage *meta)
263 {
264         printf ("\nMetadata pointers:\n");
265         dsh ("\tTables (#~)", meta, &meta->heap_tables);
266         dsh ("\t    Strings", meta, &meta->heap_strings);
267         dsh ("\t       Blob", meta, &meta->heap_blob);
268         dsh ("\tUser string", meta, &meta->heap_us);
269         dsh ("\t       GUID", meta, &meta->heap_guid);
270 }
271
272 static void
273 dump_metadata (MonoImage *meta)
274 {
275         int table;
276         
277         dump_metadata_ptrs (meta);
278
279         printf ("Rows:\n");
280         for (table = 0; table < 64; table++){
281                 if (meta->tables [table].rows == 0)
282                         continue;
283                 printf ("Table %s: %d records (%d bytes, at %p)\n",
284                         mono_meta_table_name (table),
285                         meta->tables [table].rows,
286                         meta->tables [table].row_size,
287                         meta->tables [table].base
288                         );
289         }
290 }
291
292 static void
293 dump_methoddef (MonoImage *metadata, guint32 token)
294 {
295         const char *loc;
296
297         if (!token)
298                 return;
299         loc = mono_metadata_locate_token (metadata, token);
300
301         printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
302 }
303
304 static void
305 dump_dotnet_iinfo (MonoImage *image)
306 {
307         MonoCLIImageInfo *iinfo = image->image_info;
308
309         dump_dotnet_header (&iinfo->cli_header);
310         dump_sections (iinfo);
311         dump_cli_header (&iinfo->cli_cli_header);
312         dump_strong_name (image);
313         dump_public_key (image);
314         dump_metadata (image);
315
316         dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
317 }
318
319 static void
320 dump_verify_info (MonoImage *image, int flags)
321 {
322         GSList *errors, *tmp;
323         int count = 0;
324         const char* desc [] = {
325                 "Ok", "Error", "Warning", NULL, "CLS"
326         };
327
328         errors = mono_image_verify_tables (image, flags);
329
330         for (tmp = errors; tmp; tmp = tmp->next) {
331                 MonoVerifyInfo *info = tmp->data;
332                 g_print ("%s: %s\n", desc [info->status], info->message);
333                 if (info->status == MONO_VERIFY_ERROR)
334                         count++;
335         }
336         mono_free_verify_list (errors);
337
338         if (flags & (MONO_VERIFY_ALL + 1)) { /* verify code */
339                 int i;
340                 MonoTableInfo *m = &image->tables [MONO_TABLE_METHOD];
341
342                 for (i = 0; i < m->rows; ++i) {
343                         MonoMethod *method;
344
345                         method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i+1), NULL);
346                         errors = mono_method_verify (method, flags);
347                         if (errors) {
348                                 char *sig;
349                                 sig = mono_signature_get_desc (method->signature, FALSE);
350                                 g_print ("In method: %s.%s::%s(%s)\n", method->klass->name_space, method->klass->name, method->name, sig);
351                                 g_free (sig);
352                         }
353
354                         for (tmp = errors; tmp; tmp = tmp->next) {
355                                 MonoVerifyInfo *info = tmp->data;
356                                 g_print ("%s: %s\n", desc [info->status], info->message);
357                                 if (info->status == MONO_VERIFY_ERROR)
358                                         count++;
359                         }
360                         mono_free_verify_list (errors);
361                 }
362         }
363
364         if (count)
365                 g_print ("Error count: %d\n", count);
366 }
367
368 static void
369 usage (void)
370 {
371         printf ("Usage is: pedump [--verify error,warn,cls,all,code] file.exe\n");
372         exit (1);
373 }
374
375 int
376 main (int argc, char *argv [])
377 {
378         MonoImage *image;
379         char *file = NULL;
380         char *flags = NULL;
381         const char *flag_desc [] = {"error", "warn", "cls", "all", "code", NULL};
382         guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL, MONO_VERIFY_ALL + 1};
383         int i;
384         
385         for (i = 1; i < argc; i++){
386                 if (argv [i][0] != '-'){
387                         file = argv [i];
388                         continue;
389                 }
390
391                 if (strcmp (argv [i], "--help") == 0)
392                         usage ();
393                 else if (strcmp (argv [i], "--verify") == 0) {
394                         verify_pe = 1;
395                         dump_data = 0;
396                         ++i;
397                         flags = argv [i];
398                 } else {
399                         usage ();
400                 }
401         }
402         
403         if (!file)
404                 usage ();
405
406         image = mono_image_open (file, NULL);
407         if (!image){
408                 fprintf (stderr, "Can not open image %s\n", file);
409                 exit (1);
410         }
411
412         if (dump_data)
413                 dump_dotnet_iinfo (image);
414         if (verify_pe) {
415                 int f = 0;
416                 char *tok = strtok (flags, ",");
417                 MonoAssembly *assembly;
418                 while (tok) {
419                         for (i = 0; flag_desc [i]; ++i) {
420                                 if (strcmp (tok, flag_desc [i]) == 0) {
421                                         f |= flag_vals [i];
422                                         break;
423                                 }
424                         }
425                         if (!flag_desc [i])
426                                 g_print ("Unknown verify flag %s\n", tok);
427                         tok = strtok (NULL, ",");
428                 }
429                 mono_init (file);
430                 assembly = mono_assembly_open (file, NULL);
431                 dump_verify_info (assembly->image, f);
432         }
433         
434         mono_image_close (image);
435         
436         return 0;
437 }
438