Tue May 14 16:59:44 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / pedump.c
index cafa802906d7812e4c0bc465fb0bb32b54625cef..64da6ea3a27f7299446aae6123332355f69dd579 100644 (file)
@@ -8,16 +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;
+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;
        
@@ -30,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);
@@ -62,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);
@@ -78,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");
 
@@ -106,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);
@@ -133,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);
@@ -142,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;
                
@@ -170,7 +175,7 @@ dump_section_table (section_table_t *st)
 }
 
 static void
-dump_sections (cli_image_info_t *iinfo)
+dump_sections (MonoCLIImageInfo *iinfo)
 {
        const int top = iinfo->cli_header.coff.coff_sections;
        int i;
@@ -180,7 +185,7 @@ dump_sections (cli_image_info_t *iinfo)
 }
 
 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);
@@ -199,33 +204,27 @@ dump_cli_header (cli_header_t *ch)
 }      
 
 static void
-dsh (char *label, 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 (metadata_t *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 (metadata_t *meta, int table)
-{
-       
-}
-
-static void
-dump_metadata (metadata_t *meta)
+dump_metadata (MonoImage *meta)
 {
        int table;
        
@@ -241,38 +240,55 @@ dump_metadata (metadata_t *meta)
                        meta->tables [table].row_size,
                        meta->tables [table].base
                        );
-               if (dump_tables)
-                       dump_table (meta, table);
        }
 }
 
 static void
-dump_methoddef (metadata_t *metadata, guint32 token)
+dump_methoddef (MonoImage *metadata, guint32 token)
 {
-       char *loc;
+       const char *loc;
 
+       if (!token)
+               return;
        loc = mono_metadata_locate_token (metadata, token);
 
-       printf ("RVA for Entry Point: 0x%08x\n", (*(guint32 *)loc));
+       printf ("RVA for Entry Point: 0x%08x\n", read32 (loc));
 }
 
 static void
 dump_dotnet_iinfo (MonoImage *image)
 {
-       cli_image_info_t *iinfo = image->image_info;
+       MonoCLIImageInfo *iinfo = image->image_info;
 
        dump_dotnet_header (&iinfo->cli_header);
        dump_sections (iinfo);
        dump_cli_header (&iinfo->cli_cli_header);
-       dump_metadata (&image->metadata);
+       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"
+       };
 
-       dump_methoddef (&image->metadata, iinfo->cli_cli_header.ch_entry_point);
+       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);
 }
 
@@ -281,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)
@@ -306,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);