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