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