2002-03-14 Duncan Mak <duncan@ximian.com>
[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
21 gboolean dump_data = TRUE;
22 gboolean verify_pe = FALSE;
23
24 static void
25 hex_dump (char *buffer, int base, int count)
26 {
27         int i;
28         
29         for (i = 0; i < count; i++){
30                 if ((i % 16) == 0)
31                         printf ("\n0x%08x: ", (unsigned char) base + i);
32
33                 printf ("%02x ", (unsigned char) (buffer [i]));
34         }
35 }
36
37 static void
38 hex8 (char *label, unsigned char x)
39 {
40         printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
41 }
42
43 static void
44 hex16 (char *label, guint16 x)
45 {
46         printf ("\t%s: 0x%04x\n", label, x);
47 }
48
49 static void
50 hex32 (char *label, guint32 x)
51 {
52         printf ("\t%s: 0x%08x\n", label, x);
53 }
54
55 static void
56 dump_coff_header (MonoCOFFHeader *coff)
57 {
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);
66
67 }
68
69 static void
70 dump_pe_header (MonoPEHeader *pe)
71 {
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);
82         printf ("\n");
83 }
84
85 static void
86 dump_nt_header (MonoPEHeaderNT *nt)
87 {
88         printf ("\nNT Header:\n");
89
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);
111 }
112
113 static void
114 dent (const char *label, MonoPEDirEntry de)
115 {
116         printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
117 }
118
119 static void
120 dump_datadir (MonoPEDatadir *dd)
121 {
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);
138 }
139
140 static void
141 dump_dotnet_header (MonoDotNetHeader *header)
142 {
143         dump_coff_header (&header->coff);
144         dump_pe_header (&header->pe);
145         dump_nt_header (&header->nt);
146         dump_datadir (&header->datadir);
147 }
148
149 static void
150 dump_section_table (MonoSectionTable *st)
151 {
152         guint32 flags = st->st_flags;
153                 
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);
163
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" : "");
175 }
176
177 static void
178 dump_sections (MonoCLIImageInfo *iinfo)
179 {
180         const int top = iinfo->cli_header.coff.coff_sections;
181         int i;
182         
183         for (i = 0; i < top; i++)
184                 dump_section_table (&iinfo->cli_section_tables [i]);
185 }
186
187 static void
188 dump_cli_header (MonoCLIHeader *ch)
189 {
190         printf ("\n");
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);
204 }       
205
206 static void
207 dsh (char *label, MonoImage *meta, MonoStreamHeader *sh)
208 {
209         printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
210                 label,
211                 sh->data - meta->raw_metadata, sh->data + sh->size - meta->raw_metadata,
212                 sh->size, sh->size);
213 }
214
215 static void
216 dump_metadata_ptrs (MonoImage *meta)
217 {
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);
224 }
225
226 static void
227 dump_metadata (MonoImage *meta)
228 {
229         int table;
230         
231         dump_metadata_ptrs (meta);
232
233         printf ("Rows:\n");
234         for (table = 0; table < 64; table++){
235                 if (meta->tables [table].rows == 0)
236                         continue;
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
242                         );
243         }
244 }
245
246 static void
247 dump_methoddef (MonoImage *metadata, guint32 token)
248 {
249         char *loc;
250
251         if (!token)
252                 return;
253         loc = mono_metadata_locate_token (metadata, token);
254
255         printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
256 }
257
258 static void
259 dump_dotnet_iinfo (MonoImage *image)
260 {
261         MonoCLIImageInfo *iinfo = image->image_info;
262
263         dump_dotnet_header (&iinfo->cli_header);
264         dump_sections (iinfo);
265         dump_cli_header (&iinfo->cli_cli_header);
266         dump_metadata (image);
267
268         dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
269 }
270
271 static void
272 dump_verify_info (MonoImage *image, int flags)
273 {
274         GSList *errors, *tmp;
275         const char* desc [] = {
276                 "Ok", "Error", "Warning", NULL, "CLS"
277         };
278
279         errors = mono_image_verify_tables (image, flags);
280
281         for (tmp = errors; tmp; tmp = tmp->next) {
282                 MonoVerifyInfo *info = tmp->data;
283                 g_print ("%s: %s\n", desc [info->status], info->message);
284         }
285         mono_free_verify_list (errors);
286 }
287
288 static void
289 usage (void)
290 {
291         printf ("Usage is: pedump [--verify error,warn,cls,all] file.exe\n");
292         exit (1);
293 }
294
295 int
296 main (int argc, char *argv [])
297 {
298         MonoImage *image;
299         char *file = NULL;
300         char *flags = NULL;
301         char *flag_desc [] = {"error", "warn", "cls", "all", NULL};
302         guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL};
303         int i;
304         
305         for (i = 1; i < argc; i++){
306                 if (argv [i][0] != '-'){
307                         file = argv [i];
308                         continue;
309                 }
310
311                 if (strcmp (argv [i], "--help") == 0)
312                         usage ();
313                 else if (strcmp (argv [i], "--verify") == 0) {
314                         verify_pe = 1;
315                         dump_data = 0;
316                         ++i;
317                         flags = argv [i];
318                 } else {
319                         usage ();
320                 }
321         }
322         
323         if (!file)
324                 usage ();
325
326         image = mono_image_open (file, NULL);
327         if (!image){
328                 fprintf (stderr, "Can not open image %s\n", file);
329                 exit (1);
330         }
331
332         if (dump_data)
333                 dump_dotnet_iinfo (image);
334         if (verify_pe) {
335                 int f = 0;
336                 char *tok = strtok (flags, ",");
337                 while (tok) {
338                         for (i = 0; flag_desc [i]; ++i) {
339                                 if (strcmp (tok, flag_desc [i]) == 0) {
340                                         f |= flag_vals [i];
341                                         break;
342                                 }
343                         }
344                         if (!flag_desc [i])
345                                 g_print ("Unknown verify flag %s\n", tok);
346                         tok = strtok (NULL, ",");
347                 }
348                 dump_verify_info (image, f);
349         }
350         
351         mono_image_close (image);
352         
353         return 0;
354 }
355