2001-09-21 Dietmar Maurer <dietmar@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 <string.h>
12 #include "image.h"
13 #include <glib.h>
14 #include "cil-coff.h"
15 #include "private.h"
16 #include "endian.h"
17
18 gboolean dump_data = TRUE;
19 gboolean dump_tables = FALSE;
20
21 static void
22 hex_dump (char *buffer, int base, int count)
23 {
24         int i;
25         
26         for (i = 0; i < count; i++){
27                 if ((i % 16) == 0)
28                         printf ("\n0x%08x: ", (unsigned char) base + i);
29
30                 printf ("%02x ", (unsigned char) (buffer [i]));
31         }
32 }
33
34 static void
35 hex8 (char *label, unsigned char x)
36 {
37         printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
38 }
39
40 static void
41 hex16 (char *label, guint16 x)
42 {
43         printf ("\t%s: 0x%04x\n", label, x);
44 }
45
46 static void
47 hex32 (char *label, guint32 x)
48 {
49         printf ("\t%s: 0x%08x\n", label, x);
50 }
51
52 static void
53 dump_coff_header (MonoCOFFHeader *coff)
54 {
55         printf ("\nCOFF Header:\n");
56         hex16 ("                Machine", coff->coff_machine);
57         hex16 ("               Sections", coff->coff_sections);
58         hex32 ("             Time stamp", coff->coff_time);
59         hex32 ("Pointer to Symbol Table", coff->coff_symptr);
60         hex32 ("           Symbol Count", coff->coff_symcount);
61         hex16 ("   Optional Header Size", coff->coff_opt_header_size);
62         hex16 ("        Characteristics", coff->coff_attributes);
63
64 }
65
66 static void
67 dump_pe_header (MonoPEHeader *pe)
68 {
69         printf ("\nPE Header:\n");
70         hex16 ("         Magic (0x010b)", pe->pe_magic);
71         hex8  ("             LMajor (6)", pe->pe_major);
72         hex8  ("             LMinor (0)", pe->pe_minor);
73         hex32 ("              Code Size", pe->pe_code_size);
74         hex32 ("  Initialized Data Size", pe->pe_data_size);
75         hex32 ("Uninitialized Data Size", pe->pe_uninit_data_size);
76         hex32 ("        Entry Point RVA", pe->pe_rva_entry_point);
77         hex32 ("          Code Base RVA", pe->pe_rva_code_base);
78         hex32 ("          Data Base RVA", pe->pe_rva_data_base);
79         printf ("\n");
80 }
81
82 static void
83 dump_nt_header (MonoPEHeaderNT *nt)
84 {
85         printf ("\nNT Header:\n");
86
87         hex32 ("   Image Base (0x400000)", nt->pe_image_base);
88         hex32 ("Section Alignment (8192)", nt->pe_section_align);
89         hex32 ("   File Align (512/4096)", nt->pe_file_alignment);
90         hex16 ("            OS Major (4)", nt->pe_os_major);
91         hex16 ("            OS Minor (0)", nt->pe_os_minor);
92         hex16 ("          User Major (0)", nt->pe_user_major);
93         hex16 ("          User Minor (0)", nt->pe_user_minor);
94         hex16 ("        Subsys major (4)", nt->pe_subsys_major);
95         hex16 ("        Subsys minor (0)", nt->pe_subsys_minor);
96         hex32 ("               Reserverd", nt->pe_reserved_1);
97         hex32 ("              Image Size", nt->pe_image_size);
98         hex32 ("             Header Size", nt->pe_header_size);
99         hex32 ("            Checksum (0)", nt->pe_checksum);
100         hex16 ("               Subsystem", nt->pe_subsys_required);
101         hex16 ("           DLL Flags (0)", nt->pe_dll_flags);
102         hex32 (" Stack Reserve Size (1M)", nt->pe_stack_reserve);
103         hex32 ("Stack commit Size (4096)", nt->pe_stack_commit);
104         hex32 ("  Heap Reserve Size (1M)", nt->pe_heap_reserve);
105         hex32 (" Heap Commit Size (4096)", nt->pe_heap_commit);
106         hex32 ("      Loader flags (0x1)", nt->pe_loader_flags);
107         hex32 ("   Data Directories (16)", nt->pe_data_dir_count);
108 }
109
110 static void
111 dent (const char *label, MonoPEDirEntry de)
112 {
113         printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
114 }
115
116 static void
117 dump_datadir (MonoPEDatadir *dd)
118 {
119         printf ("\nData directories:\n");
120         dent ("     Export Table", dd->pe_export_table);
121         dent ("     Import Table", dd->pe_import_table);
122         dent ("   Resource Table", dd->pe_resource_table);
123         dent ("  Exception Table", dd->pe_exception_table);
124         dent ("Certificate Table", dd->pe_certificate_table);
125         dent ("      Reloc Table", dd->pe_reloc_table);
126         dent ("            Debug", dd->pe_debug);
127         dent ("        Copyright", dd->pe_copyright);
128         dent ("       Global Ptr", dd->pe_global_ptr);
129         dent ("        TLS Table", dd->pe_tls_table);
130         dent ("Load Config Table", dd->pe_load_config_table);
131         dent ("     Bound Import", dd->pe_bound_import);
132         dent ("              IAT", dd->pe_iat);
133         dent ("Delay Import Desc", dd->pe_delay_import_desc);
134         dent ("       CLI Header", dd->pe_cli_header);
135 }
136
137 static void
138 dump_dotnet_header (MonoDotNetHeader *header)
139 {
140         dump_coff_header (&header->coff);
141         dump_pe_header (&header->pe);
142         dump_nt_header (&header->nt);
143         dump_datadir (&header->datadir);
144 }
145
146 static void
147 dump_section_table (MonoSectionTable *st)
148 {
149         guint32 flags = st->st_flags;
150                 
151         printf ("\n\tName: %s\n", st->st_name);
152         hex32 ("   Virtual Size", st->st_virtual_size);
153         hex32 ("Virtual Address", st->st_virtual_address);
154         hex32 ("  Raw Data Size", st->st_raw_data_size);
155         hex32 ("   Raw Data Ptr", st->st_raw_data_ptr);
156         hex32 ("      Reloc Ptr", st->st_reloc_ptr);
157         hex32 ("     LineNo Ptr", st->st_lineno_ptr);
158         hex16 ("    Reloc Count", st->st_reloc_count);
159         hex16 ("     Line Count", st->st_line_count);
160
161         printf ("\tFlags: %s%s%s%s%s%s%s%s%s%s\n",
162                 (flags & SECT_FLAGS_HAS_CODE) ? "code, " : "",
163                 (flags & SECT_FLAGS_HAS_INITIALIZED_DATA) ? "data, " : "",
164                 (flags & SECT_FLAGS_HAS_UNINITIALIZED_DATA) ? "bss, " : "",
165                 (flags & SECT_FLAGS_MEM_DISCARDABLE) ? "discard, " : "",
166                 (flags & SECT_FLAGS_MEM_NOT_CACHED) ? "nocache, " : "",
167                 (flags & SECT_FLAGS_MEM_NOT_PAGED) ? "nopage, " : "",
168                 (flags & SECT_FLAGS_MEM_SHARED) ? "shared, " : "",
169                 (flags & SECT_FLAGS_MEM_EXECUTE) ? "exec, " : "",
170                 (flags & SECT_FLAGS_MEM_READ) ? "read, " : "",
171                 (flags & SECT_FLAGS_MEM_WRITE) ? "write" : "");
172 }
173
174 static void
175 dump_sections (MonoCLIImageInfo *iinfo)
176 {
177         const int top = iinfo->cli_header.coff.coff_sections;
178         int i;
179         
180         for (i = 0; i < top; i++)
181                 dump_section_table (&iinfo->cli_section_tables [i]);
182 }
183
184 static void
185 dump_cli_header (MonoCLIHeader *ch)
186 {
187         printf ("\n");
188         printf ("          CLI header size: %d\n", ch->ch_size);
189         printf ("         Runtime required: %d.%d\n", ch->ch_runtime_major, ch->ch_runtime_minor);
190         printf ("                    Flags: %s, %s, %s\n",
191                 (ch->ch_flags & CLI_FLAGS_ILONLY ? "ilonly" : "contains native"),
192                 (ch->ch_flags & CLI_FLAGS_32BITREQUIRED ? "32bits" : "32/64"),
193                 (ch->ch_flags & CLI_FLAGS_ILONLY ? "trackdebug" : "no-trackdebug"));
194         dent   ("         Metadata", ch->ch_metadata);
195         hex32  ("Entry Point Token", ch->ch_entry_point);
196         dent   ("     Resources at", ch->ch_resources);
197         dent   ("   Strong Name at", ch->ch_strong_name);
198         dent   ("  Code Manager at", ch->ch_code_manager_table);
199         dent   ("  VTableFixups at", ch->ch_vtable_fixups);
200         dent   ("     EAT jumps at", ch->ch_export_address_table_jumps);
201 }       
202
203 static void
204 dsh (char *label, MonoStreamHeader *sh)
205 {
206         printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
207                 label,
208                 sh->offset, sh->offset + sh->size,
209                 sh->size, sh->size);
210 }
211
212 static void
213 dump_metadata_ptrs (MonoMetadata *meta)
214 {
215         printf ("\nMetadata pointers:\n");
216         dsh ("\tTables (#~)", &meta->heap_tables);
217         dsh ("\t    Strings", &meta->heap_strings);
218         dsh ("\t       Blob", &meta->heap_blob);
219         dsh ("\tUser string", &meta->heap_us);
220         dsh ("\t       GUID", &meta->heap_guid);
221 }
222
223 static void
224 dump_table (MonoMetadata *meta, int table)
225 {
226         
227 }
228
229 static void
230 dump_metadata (MonoMetadata *meta)
231 {
232         int table;
233         
234         dump_metadata_ptrs (meta);
235
236         printf ("Rows:\n");
237         for (table = 0; table < 64; table++){
238                 if (meta->tables [table].rows == 0)
239                         continue;
240                 printf ("Table %s: %d records (%d bytes, at %p)\n",
241                         mono_meta_table_name (table),
242                         meta->tables [table].rows,
243                         meta->tables [table].row_size,
244                         meta->tables [table].base
245                         );
246                 if (dump_tables)
247                         dump_table (meta, table);
248         }
249 }
250
251 static void
252 dump_methoddef (MonoMetadata *metadata, guint32 token)
253 {
254         char *loc;
255
256         loc = mono_metadata_locate_token (metadata, token);
257
258         printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
259 }
260
261 static void
262 dump_dotnet_iinfo (MonoImage *image)
263 {
264         MonoCLIImageInfo *iinfo = image->image_info;
265
266         dump_dotnet_header (&iinfo->cli_header);
267         dump_sections (iinfo);
268         dump_cli_header (&iinfo->cli_cli_header);
269         dump_metadata (image);
270
271         dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
272 }
273
274 static void
275 usage (void)
276 {
277         printf ("Usage is: pedump [--tables] file.exe\n");
278         exit (1);
279 }
280
281 int
282 main (int argc, char *argv [])
283 {
284         MonoImage *image;
285         char *file = NULL;
286         int i;
287         
288         for (i = 1; i < argc; i++){
289                 if (argv [i][0] != '-'){
290                         file = argv [1];
291                         continue;
292                 }
293
294                 if (strcmp (argv [i], "--help") == 0)
295                         usage ();
296                 if (strcmp (argv [i], "--tables") == 0)
297                         dump_tables = 1;
298         }
299         
300         if (!file)
301                 usage ();
302
303         image = mono_image_open (file, NULL);
304         if (!image){
305                 fprintf (stderr, "Can not open image %s\n", file);
306                 exit (1);
307         }
308
309         if (dump_data)
310                 dump_dotnet_iinfo (image);
311         
312         mono_image_close (image);
313         
314         return 0;
315 }
316