#include <stdio.h>
#include <glib.h>
#include <errno.h>
+#include <time.h>
#include <string.h>
#include "image.h"
#include "cil-coff.h"
#include "rawbuffer.h"
-#include "endian.h"
+#include "mono-endian.h"
#include "private.h"
+#include "tabledefs.h"
#define INVALID_ADDRESS 0xffffffff
for (i = 0; i < top; i++){
if ((addr >= tables->st_virtual_address) &&
(addr < tables->st_virtual_address + tables->st_raw_data_size)){
- return iinfo->cli_sections [i] +
+ return (char*)iinfo->cli_sections [i] +
(addr - tables->st_virtual_address);
}
tables++;
if (fread (t, sizeof (MonoSectionTable), 1, image->f) != 1)
return FALSE;
- t->st_virtual_size = le32_to_cpu (t->st_virtual_size);
- t->st_virtual_address = le32_to_cpu (t->st_virtual_address);
- t->st_raw_data_size = le32_to_cpu (t->st_raw_data_size);
- t->st_raw_data_ptr = le32_to_cpu (t->st_raw_data_ptr);
- t->st_reloc_ptr = le32_to_cpu (t->st_reloc_ptr);
- t->st_lineno_ptr = le32_to_cpu (t->st_lineno_ptr);
- t->st_reloc_count = le16_to_cpu (t->st_reloc_count);
- t->st_line_count = le16_to_cpu (t->st_line_count);
-
+#if G_BYTE_ORDER != G_LITTLE_ENDIAN
+ t->st_virtual_size = GUINT32_FROM_LE (t->st_virtual_size);
+ t->st_virtual_address = GUINT32_FROM_LE (t->st_virtual_address);
+ t->st_raw_data_size = GUINT32_FROM_LE (t->st_raw_data_size);
+ t->st_raw_data_ptr = GUINT32_FROM_LE (t->st_raw_data_ptr);
+ t->st_reloc_ptr = GUINT32_FROM_LE (t->st_reloc_ptr);
+ t->st_lineno_ptr = GUINT32_FROM_LE (t->st_lineno_ptr);
+ t->st_reloc_count = GUINT16_FROM_LE (t->st_reloc_count);
+ t->st_line_count = GUINT16_FROM_LE (t->st_line_count);
+ t->st_flags = GUINT32_FROM_LE (t->st_flags);
+#endif
/* consistency checks here */
}
if (offset == INVALID_ADDRESS)
return FALSE;
- if (fseek (image->f, offset, 0) != 0)
+ if (fseek (image->f, offset, SEEK_SET) != 0)
return FALSE;
if ((n = fread (&iinfo->cli_cli_header, sizeof (MonoCLIHeader), 1, image->f)) != 1)
return FALSE;
+#if G_BYTE_ORDER != G_LITTLE_ENDIAN
+#define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
+#define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
+#define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
+ SWAP32 (iinfo->cli_cli_header.ch_size);
+ SWAP32 (iinfo->cli_cli_header.ch_flags);
+ SWAP32 (iinfo->cli_cli_header.ch_entry_point);
+ SWAP16 (iinfo->cli_cli_header.ch_runtime_major);
+ SWAP16 (iinfo->cli_cli_header.ch_runtime_minor);
+ SWAPPDE (iinfo->cli_cli_header.ch_metadata);
+ SWAPPDE (iinfo->cli_cli_header.ch_resources);
+ SWAPPDE (iinfo->cli_cli_header.ch_strong_name);
+ SWAPPDE (iinfo->cli_cli_header.ch_code_manager_table);
+ SWAPPDE (iinfo->cli_cli_header.ch_vtable_fixups);
+ SWAPPDE (iinfo->cli_cli_header.ch_export_address_table_jumps);
+ SWAPPDE (iinfo->cli_cli_header.ch_eeinfo_table);
+ SWAPPDE (iinfo->cli_cli_header.ch_helper_table);
+ SWAPPDE (iinfo->cli_cli_header.ch_dynamic_info);
+ SWAPPDE (iinfo->cli_cli_header.ch_delay_load_info);
+ SWAPPDE (iinfo->cli_cli_header.ch_module_image);
+ SWAPPDE (iinfo->cli_cli_header.ch_external_fixups);
+ SWAPPDE (iinfo->cli_cli_header.ch_ridmap);
+ SWAPPDE (iinfo->cli_cli_header.ch_debug_map);
+ SWAPPDE (iinfo->cli_cli_header.ch_ip_map);
+#undef SWAP32
+#undef SWAP16
+#undef SWAPPDE
+#endif
/* Catch new uses of the fields that are supposed to be zero */
if ((iinfo->cli_cli_header.ch_eeinfo_table.rva != 0) ||
image->heap_guid.offset = read32 (ptr);
image->heap_guid.size = read32 (ptr + 4);
ptr += 8 + 6;
- } else
+ } else {
g_message ("Unknown heap type: %s\n", ptr + 8);
+ ptr += 8 + strlen (ptr) + 1;
+ }
if (((guint32)ptr) % 4){
ptr += 4 - (((guint32)ptr) % 4);
}
return load_tables (image);
}
+static void
+load_class_names (MonoImage *image) {
+ MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
+ guint32 cols [MONO_TYPEDEF_SIZE];
+ const char* name;
+ const char *nspace;
+ GHashTable *nspace_table;
+ GHashTable *name_cache = image->name_cache;
+ guint32 i, visib;
+
+ for (i = 1; i <= t->rows; ++i) {
+ mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
+ /* nested types are accessed from the nesting name */
+ visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
+ if (visib > TYPE_ATTRIBUTE_PUBLIC && visib < TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
+ continue;
+ name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
+ nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
+ if (!(nspace_table = g_hash_table_lookup (name_cache, nspace))) {
+ nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
+ g_hash_table_insert (name_cache, nspace, nspace_table);
+ }
+ g_hash_table_insert (nspace_table, name, GUINT_TO_POINTER (i));
+ }
+}
+
static MonoImage *
do_mono_image_open (const char *fname, enum MonoImageOpenStatus *status)
{
int n;
image = g_new0 (MonoImage, 1);
- image->f = fopen (fname, "r");
+ image->f = fopen (fname, "rb");
image->name = g_strdup (fname);
iinfo = g_new0 (MonoCLIImageInfo, 1);
image->image_info = iinfo;
image->method_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
image->class_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
+ image->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
image->array_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
header = &iinfo->cli_header;
if (!(msdos.msdos_header [0] == 'M' && msdos.msdos_header [1] == 'Z'))
goto invalid_image;
+ msdos.pe_offset = GUINT32_FROM_LE (msdos.pe_offset);
+
if (msdos.pe_offset != sizeof (msdos))
fseek (image->f, msdos.pe_offset, SEEK_SET);
if ((n = fread (header, sizeof (MonoDotNetHeader), 1, image->f)) != 1)
goto invalid_image;
- if (header->coff.coff_machine != 0x14c) /* FIXME: ENOENDIAN */
+#if G_BYTE_ORDER != G_LITTLE_ENDIAN
+#define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
+#define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
+#define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
+ SWAP32 (header->coff.coff_time);
+ SWAP32 (header->coff.coff_symptr);
+ SWAP32 (header->coff.coff_symcount);
+ SWAP16 (header->coff.coff_machine);
+ SWAP16 (header->coff.coff_sections);
+ SWAP16 (header->coff.coff_opt_header_size);
+ SWAP16 (header->coff.coff_attributes);
+ /* MonoPEHeader */
+ SWAP32 (header->pe.pe_code_size);
+ SWAP32 (header->pe.pe_data_size);
+ SWAP32 (header->pe.pe_uninit_data_size);
+ SWAP32 (header->pe.pe_rva_entry_point);
+ SWAP32 (header->pe.pe_rva_code_base);
+ SWAP32 (header->pe.pe_rva_data_base);
+ SWAP16 (header->pe.pe_magic);
+
+ /* MonoPEHeaderNT: not used yet */
+ SWAP32 (header->nt.pe_image_base); /* must be 0x400000 */
+ SWAP32 (header->nt.pe_section_align); /* must be 8192 */
+ SWAP32 (header->nt.pe_file_alignment); /* must be 512 or 4096 */
+ SWAP16 (header->nt.pe_os_major); /* must be 4 */
+ SWAP16 (header->nt.pe_os_minor); /* must be 0 */
+ SWAP16 (header->nt.pe_user_major);
+ SWAP16 (header->nt.pe_user_minor);
+ SWAP16 (header->nt.pe_subsys_major);
+ SWAP16 (header->nt.pe_subsys_minor);
+ SWAP32 (header->nt.pe_reserved_1);
+ SWAP32 (header->nt.pe_image_size);
+ SWAP32 (header->nt.pe_header_size);
+ SWAP32 (header->nt.pe_checksum);
+ SWAP16 (header->nt.pe_subsys_required);
+ SWAP16 (header->nt.pe_dll_flags);
+ SWAP32 (header->nt.pe_stack_reserve);
+ SWAP32 (header->nt.pe_stack_commit);
+ SWAP32 (header->nt.pe_heap_reserve);
+ SWAP32 (header->nt.pe_heap_commit);
+ SWAP32 (header->nt.pe_loader_flags);
+ SWAP32 (header->nt.pe_data_dir_count);
+
+ /* MonoDotNetHeader: mostly unused */
+ SWAPPDE (header->datadir.pe_export_table);
+ SWAPPDE (header->datadir.pe_import_table);
+ SWAPPDE (header->datadir.pe_resource_table);
+ SWAPPDE (header->datadir.pe_exception_table);
+ SWAPPDE (header->datadir.pe_certificate_table);
+ SWAPPDE (header->datadir.pe_reloc_table);
+ SWAPPDE (header->datadir.pe_debug);
+ SWAPPDE (header->datadir.pe_copyright);
+ SWAPPDE (header->datadir.pe_global_ptr);
+ SWAPPDE (header->datadir.pe_tls_table);
+ SWAPPDE (header->datadir.pe_load_config_table);
+ SWAPPDE (header->datadir.pe_bound_import);
+ SWAPPDE (header->datadir.pe_iat);
+ SWAPPDE (header->datadir.pe_delay_import_desc);
+ SWAPPDE (header->datadir.pe_cli_header);
+ SWAPPDE (header->datadir.pe_reserved);
+
+#undef SWAP32
+#undef SWAP16
+#undef SWAPPDE
+#endif
+
+ if (header->coff.coff_machine != 0x14c)
goto invalid_image;
if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader) - sizeof (MonoCOFFHeader) - 4))
goto invalid_image;
- if (header->pe.pe_magic != 0x10B) /* FIXME: ENOENDIAN */
+ if (header->pesig[0] != 'P' || header->pesig[1] != 'E' || header->pe.pe_magic != 0x10B)
goto invalid_image;
if (header->pe.pe_major != 6 || header->pe.pe_minor != 0)
if (!load_metadata (image, iinfo))
goto invalid_image;
-
+
+ load_class_names (image);
+
+ image->assembly_name = mono_metadata_string_heap (image,
+ mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY],
+ 0, MONO_ASSEMBLY_NAME));
+
if (status)
*status = MONO_IMAGE_OK;
return image;
}
+static void
+free_hash_table(gpointer key, gpointer val, gpointer user_data)
+{
+ g_hash_table_destroy ((GHashTable*)val);
+}
+
/**
* mono_image_close:
* @image: The image file we wish to close
fclose (image->f);
g_free (image->name);
+
+ g_hash_table_destroy (image->method_cache);
+ g_hash_table_destroy (image->class_cache);
+ g_hash_table_destroy (image->array_cache);
+ g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
+ g_hash_table_destroy (image->name_cache);
if (image->raw_metadata != NULL)
mono_raw_buffer_free (image->raw_metadata);