Fri May 10 16:31:49 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / pedump.c
index 7a716cf3ea7f8c699e7ebdc3c65468112c5c13d9..64da6ea3a27f7299446aae6123332355f69dd579 100644 (file)
@@ -8,12 +8,21 @@
  */
 #include <config.h>
 #include <stdio.h>
-#include "assembly.h"
+#include <stdlib.h>
+#include <string.h>
+#include "image.h"
 #include <glib.h>
 #include "cil-coff.h"
+#include "private.h"
+#include "mono-endian.h"
+#include "verify.h"
+#include <mono/metadata/class.h>
+
+gboolean dump_data = TRUE;
+gboolean verify_pe = FALSE;
 
 static void
-hex_dump (char *buffer, int base, int count)
+hex_dump (const char *buffer, int base, int count)
 {
        int i;
        
@@ -26,25 +35,25 @@ hex_dump (char *buffer, int base, int count)
 }
 
 static void
-hex8 (char *label, unsigned char x)
+hex8 (const char *label, unsigned char x)
 {
        printf ("\t%s: 0x%02x\n", label, (unsigned char) x);
 }
 
 static void
-hex16 (char *label, guint16 x)
+hex16 (const char *label, guint16 x)
 {
        printf ("\t%s: 0x%04x\n", label, x);
 }
 
 static void
-hex32 (char *label, guint32 x)
+hex32 (const char *label, guint32 x)
 {
        printf ("\t%s: 0x%08x\n", label, x);
 }
 
 static void
-dump_coff_header (coff_header_t *coff)
+dump_coff_header (MonoCOFFHeader *coff)
 {
        printf ("\nCOFF Header:\n");
        hex16 ("                Machine", coff->coff_machine);
@@ -58,7 +67,7 @@ dump_coff_header (coff_header_t *coff)
 }
 
 static void
-dump_pe_header (pe_header_t *pe)
+dump_pe_header (MonoPEHeader *pe)
 {
        printf ("\nPE Header:\n");
        hex16 ("         Magic (0x010b)", pe->pe_magic);
@@ -74,7 +83,7 @@ dump_pe_header (pe_header_t *pe)
 }
 
 static void
-dump_nt_header (pe_header_nt_t *nt)
+dump_nt_header (MonoPEHeaderNT *nt)
 {
        printf ("\nNT Header:\n");
 
@@ -102,13 +111,13 @@ dump_nt_header (pe_header_nt_t *nt)
 }
 
 static void
-dent (const char *label, pe_dir_entry_t de)
+dent (const char *label, MonoPEDirEntry de)
 {
        printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
 }
 
 static void
-dump_datadir (pe_datadir_t *dd)
+dump_datadir (MonoPEDatadir *dd)
 {
        printf ("\nData directories:\n");
        dent ("     Export Table", dd->pe_export_table);
@@ -129,7 +138,7 @@ dump_datadir (pe_datadir_t *dd)
 }
 
 static void
-dump_dotnet_header (dotnet_header_t *header)
+dump_dotnet_header (MonoDotNetHeader *header)
 {
        dump_coff_header (&header->coff);
        dump_pe_header (&header->pe);
@@ -138,7 +147,7 @@ dump_dotnet_header (dotnet_header_t *header)
 }
 
 static void
-dump_section_table (section_table_t *st)
+dump_section_table (MonoSectionTable *st)
 {
        guint32 flags = st->st_flags;
                
@@ -166,17 +175,17 @@ dump_section_table (section_table_t *st)
 }
 
 static void
-dump_sections (dotnet_image_info_t *iinfo)
+dump_sections (MonoCLIImageInfo *iinfo)
 {
-       const int top = iinfo->dn_header.coff.coff_sections;
+       const int top = iinfo->cli_header.coff.coff_sections;
        int i;
        
        for (i = 0; i < top; i++)
-               dump_section_table (&iinfo->dn_section_tables [i]);
+               dump_section_table (&iinfo->cli_section_tables [i]);
 }
 
 static void
-dump_cli_header (cli_header_t *ch)
+dump_cli_header (MonoCLIHeader *ch)
 {
        printf ("\n");
        printf ("          CLI header size: %d\n", ch->ch_size);
@@ -195,114 +204,151 @@ dump_cli_header (cli_header_t *ch)
 }      
 
 static void
-dsh (char *label, dotnet_image_info_t *iinfo, stream_header_t *sh)
+dsh (const char *label, MonoImage *meta, MonoStreamHeader *sh)
 {
        printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
                label,
-               sh->sh_offset, sh->sh_offset + sh->sh_size,
-               sh->sh_size, sh->sh_size);
+               sh->data - meta->raw_metadata, sh->data + sh->size - meta->raw_metadata,
+               sh->size, sh->size);
 }
 
 static void
-dump_metadata_ptrs (dotnet_image_info_t *iinfo)
+dump_metadata_ptrs (MonoImage *meta)
 {
-       metadata_t *meta = &iinfo->dn_metadata;
-       
        printf ("\nMetadata pointers:\n");
-       dsh ("\tTables (#~)", iinfo, &meta->heap_tables);
-       dsh ("\t    Strings", iinfo, &meta->heap_strings);
-       dsh ("\t       Blob", iinfo, &meta->heap_blob);
-       dsh ("\tUser string", iinfo, &meta->heap_us);
-       dsh ("\t       GUID", iinfo, &meta->heap_guid);
-}
-
-static const char *
-table_to_string (int table)
-{
-       char *map_table_to_string [] = {
-               /* 0 */  "Module",       "TypeRef",       "TypeDef",     "FieldPtr",
-               /* 4 */  "FieldDef",     "MethodPtr",     "MethodDef",   "ParamPtr",
-               /* 8 */  "ParamDef",     "InterfaceImpl", "MemberRef",   "FieldInit",
-               /* 12 */ "CustomAttr",   "MarshalDef",    "Permission",  "LayoutDef",
-               /* 16 */ "FieldOffset",  "Signature",     "EventAssoc",  "EventPtr",
-               /* 20 */ "Event",        "PropertyAssoc", "PropertyPtr", "Property",
-               /* 24 */ "MethodAssoc",  "MethodImpl",    "ModuleRef",   "TypeSpec",
-               /* 28 */ "PInvoke",      "Data",          "EncLog",      "EncAssoc",
-               /* 32 */ "Assembly",     "ProcessorDef",  "OSDef",       "AssemblyRef",
-               /* 36 */ "ProcessorRef", "OSRef",         "File",        "ComType",
-               /* 40 */ "ManifestResource", "ExeLocation", "42",        "43",
-               /* 44 */ "44",           "45",            "NestedClass", "47"
-               /* 48 */ "48",           "49",            "50",          "51",
-               /* 52 */ "52",           "53",            "54",          "55"
-               /* 56 */ "56",           "57",            "58",          "59"
-               /* 60 */ "60",           "61",            "62",          "63"
-       };
-       
-       if (table < 0 || table > 63)
-               return "Unknown table name";
-       
-       return map_table_to_string [table];
+       dsh ("\tTables (#~)", meta, &meta->heap_tables);
+       dsh ("\t    Strings", meta, &meta->heap_strings);
+       dsh ("\t       Blob", meta, &meta->heap_blob);
+       dsh ("\tUser string", meta, &meta->heap_us);
+       dsh ("\t       GUID", meta, &meta->heap_guid);
 }
 
 static void
-dump_metadata (dotnet_image_info_t *iinfo)
+dump_metadata (MonoImage *meta)
 {
-       metadata_t *meta = &iinfo->dn_metadata;
        int table;
        
-       dump_metadata_ptrs (iinfo);
+       dump_metadata_ptrs (meta);
 
        printf ("Rows:\n");
        for (table = 0; table < 64; table++){
-               if (meta->rows [table] == 0)
+               if (meta->tables [table].rows == 0)
                        continue;
-               printf ("Table %s (%d): %d rows\n", table_to_string (table), table, meta->rows [table]);
+               printf ("Table %s: %d records (%d bytes, at %p)\n",
+                       mono_meta_table_name (table),
+                       meta->tables [table].rows,
+                       meta->tables [table].row_size,
+                       meta->tables [table].base
+                       );
        }
 }
 
 static void
-dump_dotnet_iinfo (dotnet_image_info_t *iinfo)
+dump_methoddef (MonoImage *metadata, guint32 token)
 {
-       dump_dotnet_header (&iinfo->dn_header);
+       const char *loc;
+
+       if (!token)
+               return;
+       loc = mono_metadata_locate_token (metadata, token);
+
+       printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
+}
+
+static void
+dump_dotnet_iinfo (MonoImage *image)
+{
+       MonoCLIImageInfo *iinfo = image->image_info;
+
+       dump_dotnet_header (&iinfo->cli_header);
        dump_sections (iinfo);
-       dump_cli_header (&iinfo->dn_cli_header);
-       dump_metadata (iinfo);
+       dump_cli_header (&iinfo->cli_cli_header);
+       dump_metadata (image);
+
+       dump_methoddef (image, iinfo->cli_cli_header.ch_entry_point);
+}
+
+static void
+dump_verify_info (MonoImage *image, int flags)
+{
+       GSList *errors, *tmp;
+       const char* desc [] = {
+               "Ok", "Error", "Warning", NULL, "CLS"
+       };
+
+       errors = mono_image_verify_tables (image, flags);
+
+       for (tmp = errors; tmp; tmp = tmp->next) {
+               MonoVerifyInfo *info = tmp->data;
+               g_print ("%s: %s\n", desc [info->status], info->message);
+       }
+       mono_free_verify_list (errors);
 }
 
 static void
 usage (void)
 {
-       printf ("Usage is: pedump [-m] file.exe\n");
+       printf ("Usage is: pedump [--verify error,warn,cls,all] file.exe\n");
        exit (1);
 }
 
 int
 main (int argc, char *argv [])
 {
-       dotnet_image_info_t *iinfo;
-       MonoAssembly *assembly;
+       MonoImage *image;
        char *file = NULL;
+       char *flags = NULL;
+       const char *flag_desc [] = {"error", "warn", "cls", "all", NULL};
+       guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL};
        int i;
        
        for (i = 1; i < argc; i++){
                if (argv [i][0] != '-'){
-                       file = argv [1];
+                       file = argv [i];
                        continue;
                }
 
-               if (argv [i][1] == 'h')
+               if (strcmp (argv [i], "--help") == 0)
                        usage ();
+               else if (strcmp (argv [i], "--verify") == 0) {
+                       verify_pe = 1;
+                       dump_data = 0;
+                       ++i;
+                       flags = argv [i];
+               } else {
+                       usage ();
+               }
        }
        
        if (!file)
                usage ();
 
-       assembly = mono_assembly_open (file, NULL);
-       iinfo = assembly->image_info;
-
-       dump_dotnet_iinfo (iinfo);
+       image = mono_image_open (file, NULL);
+       if (!image){
+               fprintf (stderr, "Can not open image %s\n", file);
+               exit (1);
+       }
 
-       mono_assembly_close (assembly);
+       if (dump_data)
+               dump_dotnet_iinfo (image);
+       if (verify_pe) {
+               int f = 0;
+               char *tok = strtok (flags, ",");
+               while (tok) {
+                       for (i = 0; flag_desc [i]; ++i) {
+                               if (strcmp (tok, flag_desc [i]) == 0) {
+                                       f |= flag_vals [i];
+                                       break;
+                               }
+                       }
+                       if (!flag_desc [i])
+                               g_print ("Unknown verify flag %s\n", tok);
+                       tok = strtok (NULL, ",");
+               }
+               dump_verify_info (image, f);
+       }
+       
+       mono_image_close (image);
        
        return 0;
 }