Fix tests/bug79956.2.il
[mono.git] / mono / metadata / pedump.c
index f6f3ee555b5209f6ccc7d9de272de1550188fbba..b52f5d79dbcc50893b9980dadc5a27b7f3bf29e0 100644 (file)
@@ -8,27 +8,29 @@
  */
 #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>
+#include <mono/metadata/debug-helpers.h>
+#include <mono/metadata/tokentype.h>
+#include <mono/metadata/appdomain.h>
+#include <mono/metadata/assembly.h>
+#include <mono/metadata/metadata-internals.h>
+#include <mono/metadata/rawbuffer.h>
+#include <mono/metadata/class-internals.h>
+#include "mono/utils/mono-digest.h"
 
 gboolean dump_data = TRUE;
-gboolean dump_tables = FALSE;
-
-gpointer arch_create_jit_trampoline (MonoMethod *method);
-
-gpointer 
-arch_create_jit_trampoline (MonoMethod *method)
-{
-       return method;
-}
+gboolean verify_pe = FALSE;
 
+/* unused
 static void
-hex_dump (char *buffer, int base, int count)
+hex_dump (const char *buffer, int base, int count)
 {
        int i;
        
@@ -39,21 +41,22 @@ hex_dump (char *buffer, int base, int count)
                printf ("%02x ", (unsigned char) (buffer [i]));
        }
 }
+*/
 
 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);
 }
@@ -122,6 +125,45 @@ dent (const char *label, MonoPEDirEntry de)
        printf ("\t%s: 0x%08x [0x%08x]\n", label, de.rva, de.size);
 }
 
+static void
+dump_blob (const char *desc, const char* p, guint32 size)
+{
+       int i;
+
+       printf ("%s", desc);
+       if (!p) {
+               printf (" none\n");
+               return;
+       }
+
+       for (i = 0; i < size; ++i) {
+               if (!(i % 16))
+                       printf ("\n\t");
+               printf (" %02X", p [i] & 0xFF);
+       }
+       printf ("\n");
+}
+
+static void
+dump_public_key (MonoImage *m)
+{
+       guint32 size;
+       const char *p;
+
+       p = mono_image_get_public_key (m, &size);
+       dump_blob ("\nPublic key:", p, size);
+}
+
+static void
+dump_strong_name (MonoImage *m)
+{
+       guint32 size;
+       const char *p;
+
+       p = mono_image_get_strong_name (m, &size);
+       dump_blob ("\nStrong name:", p, size);
+}
+
 static void
 dump_datadir (MonoPEDatadir *dd)
 {
@@ -196,10 +238,11 @@ dump_cli_header (MonoCLIHeader *ch)
        printf ("\n");
        printf ("          CLI header size: %d\n", ch->ch_size);
        printf ("         Runtime required: %d.%d\n", ch->ch_runtime_major, ch->ch_runtime_minor);
-       printf ("                    Flags: %s, %s, %s\n",
+       printf ("                    Flags: %s, %s, %s, %s\n",
                (ch->ch_flags & CLI_FLAGS_ILONLY ? "ilonly" : "contains native"),
                (ch->ch_flags & CLI_FLAGS_32BITREQUIRED ? "32bits" : "32/64"),
-               (ch->ch_flags & CLI_FLAGS_ILONLY ? "trackdebug" : "no-trackdebug"));
+               (ch->ch_flags & CLI_FLAGS_ILONLY ? "trackdebug" : "no-trackdebug"),
+               (ch->ch_flags & CLI_FLAGS_STRONGNAMESIGNED ? "strongnamesigned" : "notsigned"));
        dent   ("         Metadata", ch->ch_metadata);
        hex32  ("Entry Point Token", ch->ch_entry_point);
        dent   ("     Resources at", ch->ch_resources);
@@ -210,40 +253,34 @@ 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,
+               (int)(sh->data - meta->raw_metadata), (int)(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);
+       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_table (MonoMetadata *meta, int table)
-{
-       
-}
-
-static void
-dump_metadata (MonoMetadata *meta)
+dump_metadata (MonoImage *meta)
 {
        int table;
        
        dump_metadata_ptrs (meta);
 
        printf ("Rows:\n");
-       for (table = 0; table < 64; table++){
+       for (table = 0; table < MONO_TABLE_NUM; table++){
                if (meta->tables [table].rows == 0)
                        continue;
                printf ("Table %s: %d records (%d bytes, at %p)\n",
@@ -252,15 +289,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;
@@ -277,15 +312,67 @@ dump_dotnet_iinfo (MonoImage *image)
        dump_dotnet_header (&iinfo->cli_header);
        dump_sections (iinfo);
        dump_cli_header (&iinfo->cli_cli_header);
+       dump_strong_name (image);
+       dump_public_key (image);
        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;
+       int count = 0;
+       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);
+               if (info->status == MONO_VERIFY_ERROR)
+                       count++;
+       }
+       mono_free_verify_list (errors);
+
+       if (flags & (MONO_VERIFY_ALL + 1)) { /* verify code */
+               int i;
+               MonoTableInfo *m = &image->tables [MONO_TABLE_METHOD];
+
+               for (i = 0; i < m->rows; ++i) {
+                       MonoMethod *method;
+
+                       method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i+1), NULL);
+                       errors = mono_method_verify (method, flags);
+                       if (errors) {
+                               char *sig;
+                               MonoClass *klass = mono_method_get_class (method);
+                               sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
+                               g_print ("In method: %s.%s::%s(%s)\n", mono_class_get_namespace (klass), mono_class_get_name (klass), mono_method_get_name (method), sig);
+                               g_free (sig);
+                       }
+
+                       for (tmp = errors; tmp; tmp = tmp->next) {
+                               MonoVerifyInfo *info = tmp->data;
+                               g_print ("%s: %s\n", desc [info->status], info->message);
+                               if (info->status == MONO_VERIFY_ERROR)
+                                       count++;
+                       }
+                       mono_free_verify_list (errors);
+               }
+       }
+
+       if (count)
+               g_print ("Error count: %d\n", count);
+}
+
 static void
 usage (void)
 {
-       printf ("Usage is: pedump [--tables] file.exe\n");
+       printf ("Usage is: pedump [--verify error,warn,cls,all,code] file.exe\n");
        exit (1);
 }
 
@@ -294,23 +381,38 @@ main (int argc, char *argv [])
 {
        MonoImage *image;
        char *file = NULL;
+       char *flags = NULL;
+       const char *flag_desc [] = {"error", "warn", "cls", "all", "code", NULL};
+       guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL, MONO_VERIFY_ALL + 1};
        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)
                usage ();
 
+       mono_metadata_init ();
+        mono_raw_buffer_init ();
+        mono_images_init ();
+        mono_assemblies_init ();
+        mono_loader_init ();
        image = mono_image_open (file, NULL);
        if (!image){
                fprintf (stderr, "Can not open image %s\n", file);
@@ -319,8 +421,26 @@ main (int argc, char *argv [])
 
        if (dump_data)
                dump_dotnet_iinfo (image);
-       
-       mono_image_close (image);
+       if (verify_pe) {
+               int f = 0;
+               char *tok = strtok (flags, ",");
+               MonoAssembly *assembly;
+               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, ",");
+               }
+               mono_init (file);
+               assembly = mono_assembly_open (file, NULL);
+               dump_verify_info (assembly->image, f);
+       } else
+               mono_image_close (image);
        
        return 0;
 }