Tue May 14 16:59:44 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / pedump.c
index bf9d031297f9f5921daaa73991ee180055d628f7..64da6ea3a27f7299446aae6123332355f69dd579 100644 (file)
@@ -8,27 +8,21 @@
  */
 #include <config.h>
 #include <stdio.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 dump_tables = FALSE;
-
-gpointer arch_create_jit_trampoline (MonoMethod *method, gboolean virtual);
-
-gpointer 
-arch_create_jit_trampoline (MonoMethod *method, gboolean virtual)
-{
-       return method;
-}
+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;
        
@@ -41,19 +35,19 @@ 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);
 }
@@ -210,33 +204,27 @@ dump_cli_header (MonoCLIHeader *ch)
 }      
 
 static void
-dsh (char *label, MonoStreamHeader *sh)
+dsh (const char *label, MonoImage *meta, MonoStreamHeader *sh)
 {
        printf ("%s: 0x%08x - 0x%08x [%d == 0x%08x]\n",
                label,
-               sh->offset, sh->offset + sh->size,
+               sh->data - meta->raw_metadata, sh->data + sh->size - meta->raw_metadata,
                sh->size, sh->size);
 }
 
 static void
-dump_metadata_ptrs (MonoMetadata *meta)
+dump_metadata_ptrs (MonoImage *meta)
 {
        printf ("\nMetadata pointers:\n");
-       dsh ("\tTables (#~)", &meta->heap_tables);
-       dsh ("\t    Strings", &meta->heap_strings);
-       dsh ("\t       Blob", &meta->heap_blob);
-       dsh ("\tUser string", &meta->heap_us);
-       dsh ("\t       GUID", &meta->heap_guid);
-}
-
-static void
-dump_table (MonoMetadata *meta, int 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 (MonoMetadata *meta)
+dump_metadata (MonoImage *meta)
 {
        int table;
        
@@ -252,15 +240,13 @@ dump_metadata (MonoMetadata *meta)
                        meta->tables [table].row_size,
                        meta->tables [table].base
                        );
-               if (dump_tables)
-                       dump_table (meta, table);
        }
 }
 
 static void
-dump_methoddef (MonoMetadata *metadata, guint32 token)
+dump_methoddef (MonoImage *metadata, guint32 token)
 {
-       char *loc;
+       const char *loc;
 
        if (!token)
                return;
@@ -282,10 +268,27 @@ dump_dotnet_iinfo (MonoImage *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 [--tables] file.exe\n");
+       printf ("Usage is: pedump [--verify error,warn,cls,all] file.exe\n");
        exit (1);
 }
 
@@ -294,18 +297,27 @@ main (int argc, char *argv [])
 {
        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 (strcmp (argv [i], "--help") == 0)
                        usage ();
-               if (strcmp (argv [i], "--tables") == 0)
-                       dump_tables = 1;
+               else if (strcmp (argv [i], "--verify") == 0) {
+                       verify_pe = 1;
+                       dump_data = 0;
+                       ++i;
+                       flags = argv [i];
+               } else {
+                       usage ();
+               }
        }
        
        if (!file)
@@ -319,6 +331,22 @@ main (int argc, char *argv [])
 
        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);