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