2 * image.c: Routines for manipulating an image stored in an
3 * extended PE/COFF file.
6 * Miguel de Icaza (miguel@ximian.com)
7 * Paolo Molaro (lupus@ximian.com)
9 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
21 #include "mono-endian.h"
22 #include "tabledefs.h"
23 #include "tokentype.h"
24 #include "metadata-internals.h"
25 #include "profiler-private.h"
29 #include <mono/io-layer/io-layer.h>
30 #include <mono/utils/mono-logger-internal.h>
31 #include <mono/utils/mono-path.h>
32 #include <mono/utils/mono-mmap.h>
33 #include <mono/utils/mono-io-portability.h>
34 #include <mono/utils/atomic.h>
35 #include <mono/metadata/class-internals.h>
36 #include <mono/metadata/assembly.h>
37 #include <mono/metadata/object-internals.h>
38 #include <mono/metadata/security-core-clr.h>
39 #include <mono/metadata/verify-internals.h>
40 #include <mono/metadata/verify.h>
41 #include <sys/types.h>
47 #define INVALID_ADDRESS 0xffffffff
49 // Amount initially reserved in each image's mempool.
50 // FIXME: This number is arbitrary, a more practical number should be found
51 #define INITIAL_IMAGE_SIZE 512
54 * The "loaded images" hashes keep track of the various assemblies and netmodules loaded
55 * There are four, for all combinations of [look up by path or assembly name?]
56 * and [normal or reflection-only load?, as in Assembly.ReflectionOnlyLoad]
60 IMAGES_HASH_PATH_REFONLY = 1,
62 IMAGES_HASH_NAME_REFONLY = 3,
65 static GHashTable *loaded_images_hashes [4] = {NULL, NULL, NULL, NULL};
67 static GHashTable *get_loaded_images_hash (gboolean refonly)
69 int idx = refonly ? IMAGES_HASH_PATH_REFONLY : IMAGES_HASH_PATH;
70 return loaded_images_hashes [idx];
73 static GHashTable *get_loaded_images_by_name_hash (gboolean refonly)
75 int idx = refonly ? IMAGES_HASH_NAME_REFONLY : IMAGES_HASH_NAME;
76 return loaded_images_hashes [idx];
79 static gboolean debug_assembly_unload = FALSE;
81 #define mono_images_lock() if (mutex_inited) mono_mutex_lock (&images_mutex)
82 #define mono_images_unlock() if (mutex_inited) mono_mutex_unlock (&images_mutex)
83 static gboolean mutex_inited;
84 static mono_mutex_t images_mutex;
86 static void install_pe_loader (void);
88 typedef struct ImageUnloadHook ImageUnloadHook;
89 struct ImageUnloadHook {
90 MonoImageUnloadFunc func;
94 static GSList *image_unload_hooks;
97 mono_install_image_unload_hook (MonoImageUnloadFunc func, gpointer user_data)
99 ImageUnloadHook *hook;
101 g_return_if_fail (func != NULL);
103 hook = g_new0 (ImageUnloadHook, 1);
105 hook->user_data = user_data;
106 image_unload_hooks = g_slist_prepend (image_unload_hooks, hook);
110 mono_remove_image_unload_hook (MonoImageUnloadFunc func, gpointer user_data)
113 ImageUnloadHook *hook;
115 for (l = image_unload_hooks; l; l = l->next) {
118 if (hook->func == func && hook->user_data == user_data) {
120 image_unload_hooks = g_slist_delete_link (image_unload_hooks, l);
127 mono_image_invoke_unload_hook (MonoImage *image)
130 ImageUnloadHook *hook;
132 for (l = image_unload_hooks; l; l = l->next) {
135 hook->func (image, hook->user_data);
139 static GSList *image_loaders;
142 mono_install_image_loader (const MonoImageLoader *loader)
144 image_loaders = g_slist_prepend (image_loaders, (MonoImageLoader*)loader);
147 /* returns offset relative to image->raw_data */
149 mono_cli_rva_image_map (MonoImage *image, guint32 addr)
151 MonoCLIImageInfo *iinfo = image->image_info;
152 const int top = iinfo->cli_section_count;
153 MonoSectionTable *tables = iinfo->cli_section_tables;
156 if (image->metadata_only)
159 for (i = 0; i < top; i++){
160 if ((addr >= tables->st_virtual_address) &&
161 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
163 if (image->is_module_handle)
166 return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
170 return INVALID_ADDRESS;
174 * mono_images_rva_map:
175 * @image: a MonoImage
176 * @addr: relative virtual address (RVA)
178 * This is a low-level routine used by the runtime to map relative
179 * virtual address (RVA) into their location in memory.
181 * Returns: the address in memory for the given RVA, or NULL if the
182 * RVA is not valid for this image.
185 mono_image_rva_map (MonoImage *image, guint32 addr)
187 MonoCLIImageInfo *iinfo = image->image_info;
188 const int top = iinfo->cli_section_count;
189 MonoSectionTable *tables = iinfo->cli_section_tables;
193 if (image->is_module_handle) {
194 if (addr && addr < image->raw_data_len)
195 return image->raw_data + addr;
201 for (i = 0; i < top; i++){
202 if ((addr >= tables->st_virtual_address) &&
203 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
204 if (!iinfo->cli_sections [i]) {
205 if (!mono_image_ensure_section_idx (image, i))
208 return (char*)iinfo->cli_sections [i] +
209 (addr - tables->st_virtual_address);
219 * Initialize the global variables used by this module.
222 mono_images_init (void)
224 mono_mutex_init_recursive (&images_mutex);
227 for(hash_idx = 0; hash_idx < IMAGES_HASH_COUNT; hash_idx++)
228 loaded_images_hashes [hash_idx] = g_hash_table_new (g_str_hash, g_str_equal);
230 debug_assembly_unload = g_getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
232 install_pe_loader ();
238 * mono_images_cleanup:
240 * Free all resources used by this module.
243 mono_images_cleanup (void)
248 mono_mutex_destroy (&images_mutex);
250 // If an assembly image is still loaded at shutdown, this could indicate managed code is still running.
251 // Reflection-only images being still loaded doesn't indicate anything as harmful, so we don't check for it.
252 g_hash_table_iter_init (&iter, get_loaded_images_hash (FALSE));
253 while (g_hash_table_iter_next (&iter, NULL, (void**)&image))
254 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly image '%s' still loaded at shutdown.", image->name);
257 for(hash_idx = 0; hash_idx < IMAGES_HASH_COUNT; hash_idx++)
258 g_hash_table_destroy (loaded_images_hashes [hash_idx]);
260 mutex_inited = FALSE;
264 * mono_image_ensure_section_idx:
265 * @image: The image we are operating on
266 * @section: section number that we will load/map into memory
268 * This routine makes sure that we have an in-memory copy of
269 * an image section (.text, .rsrc, .data).
271 * Returns: TRUE on success
274 mono_image_ensure_section_idx (MonoImage *image, int section)
276 MonoCLIImageInfo *iinfo = image->image_info;
277 MonoSectionTable *sect;
279 g_return_val_if_fail (section < iinfo->cli_section_count, FALSE);
281 if (iinfo->cli_sections [section] != NULL)
284 sect = &iinfo->cli_section_tables [section];
286 if (sect->st_raw_data_ptr + sect->st_raw_data_size > image->raw_data_len)
289 if (image->is_module_handle)
290 iinfo->cli_sections [section] = image->raw_data + sect->st_virtual_address;
293 /* FIXME: we ignore the writable flag since we don't patch the binary */
294 iinfo->cli_sections [section] = image->raw_data + sect->st_raw_data_ptr;
299 * mono_image_ensure_section:
300 * @image: The image we are operating on
301 * @section: section name that we will load/map into memory
303 * This routine makes sure that we have an in-memory copy of
304 * an image section (.text, .rsrc, .data).
306 * Returns: TRUE on success
309 mono_image_ensure_section (MonoImage *image, const char *section)
311 MonoCLIImageInfo *ii = image->image_info;
314 for (i = 0; i < ii->cli_section_count; i++){
315 if (strncmp (ii->cli_section_tables [i].st_name, section, 8) != 0)
318 return mono_image_ensure_section_idx (image, i);
324 load_section_tables (MonoImage *image, MonoCLIImageInfo *iinfo, guint32 offset)
326 const int top = iinfo->cli_header.coff.coff_sections;
329 iinfo->cli_section_count = top;
330 iinfo->cli_section_tables = g_new0 (MonoSectionTable, top);
331 iinfo->cli_sections = g_new0 (void *, top);
333 for (i = 0; i < top; i++){
334 MonoSectionTable *t = &iinfo->cli_section_tables [i];
336 if (offset + sizeof (MonoSectionTable) > image->raw_data_len)
338 memcpy (t, image->raw_data + offset, sizeof (MonoSectionTable));
339 offset += sizeof (MonoSectionTable);
341 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
342 t->st_virtual_size = GUINT32_FROM_LE (t->st_virtual_size);
343 t->st_virtual_address = GUINT32_FROM_LE (t->st_virtual_address);
344 t->st_raw_data_size = GUINT32_FROM_LE (t->st_raw_data_size);
345 t->st_raw_data_ptr = GUINT32_FROM_LE (t->st_raw_data_ptr);
346 t->st_reloc_ptr = GUINT32_FROM_LE (t->st_reloc_ptr);
347 t->st_lineno_ptr = GUINT32_FROM_LE (t->st_lineno_ptr);
348 t->st_reloc_count = GUINT16_FROM_LE (t->st_reloc_count);
349 t->st_line_count = GUINT16_FROM_LE (t->st_line_count);
350 t->st_flags = GUINT32_FROM_LE (t->st_flags);
352 /* consistency checks here */
359 mono_image_load_cli_header (MonoImage *image, MonoCLIImageInfo *iinfo)
363 offset = mono_cli_rva_image_map (image, iinfo->cli_header.datadir.pe_cli_header.rva);
364 if (offset == INVALID_ADDRESS)
367 if (offset + sizeof (MonoCLIHeader) > image->raw_data_len)
369 memcpy (&iinfo->cli_cli_header, image->raw_data + offset, sizeof (MonoCLIHeader));
371 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
372 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
373 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
374 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
375 SWAP32 (iinfo->cli_cli_header.ch_size);
376 SWAP32 (iinfo->cli_cli_header.ch_flags);
377 SWAP32 (iinfo->cli_cli_header.ch_entry_point);
378 SWAP16 (iinfo->cli_cli_header.ch_runtime_major);
379 SWAP16 (iinfo->cli_cli_header.ch_runtime_minor);
380 SWAPPDE (iinfo->cli_cli_header.ch_metadata);
381 SWAPPDE (iinfo->cli_cli_header.ch_resources);
382 SWAPPDE (iinfo->cli_cli_header.ch_strong_name);
383 SWAPPDE (iinfo->cli_cli_header.ch_code_manager_table);
384 SWAPPDE (iinfo->cli_cli_header.ch_vtable_fixups);
385 SWAPPDE (iinfo->cli_cli_header.ch_export_address_table_jumps);
386 SWAPPDE (iinfo->cli_cli_header.ch_eeinfo_table);
387 SWAPPDE (iinfo->cli_cli_header.ch_helper_table);
388 SWAPPDE (iinfo->cli_cli_header.ch_dynamic_info);
389 SWAPPDE (iinfo->cli_cli_header.ch_delay_load_info);
390 SWAPPDE (iinfo->cli_cli_header.ch_module_image);
391 SWAPPDE (iinfo->cli_cli_header.ch_external_fixups);
392 SWAPPDE (iinfo->cli_cli_header.ch_ridmap);
393 SWAPPDE (iinfo->cli_cli_header.ch_debug_map);
394 SWAPPDE (iinfo->cli_cli_header.ch_ip_map);
399 /* Catch new uses of the fields that are supposed to be zero */
401 if ((iinfo->cli_cli_header.ch_eeinfo_table.rva != 0) ||
402 (iinfo->cli_cli_header.ch_helper_table.rva != 0) ||
403 (iinfo->cli_cli_header.ch_dynamic_info.rva != 0) ||
404 (iinfo->cli_cli_header.ch_delay_load_info.rva != 0) ||
405 (iinfo->cli_cli_header.ch_module_image.rva != 0) ||
406 (iinfo->cli_cli_header.ch_external_fixups.rva != 0) ||
407 (iinfo->cli_cli_header.ch_ridmap.rva != 0) ||
408 (iinfo->cli_cli_header.ch_debug_map.rva != 0) ||
409 (iinfo->cli_cli_header.ch_ip_map.rva != 0)){
412 * No need to scare people who are testing this, I am just
413 * labelling this as a LAMESPEC
415 /* g_warning ("Some fields in the CLI header which should have been zero are not zero"); */
423 load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
425 guint32 offset, size;
431 offset = mono_cli_rva_image_map (image, iinfo->cli_cli_header.ch_metadata.rva);
432 if (offset == INVALID_ADDRESS)
435 size = iinfo->cli_cli_header.ch_metadata.size;
437 if (offset + size > image->raw_data_len)
439 image->raw_metadata = image->raw_data + offset;
441 /* 24.2.1: Metadata root starts here */
442 ptr = image->raw_metadata;
444 if (strncmp (ptr, "BSJB", 4) == 0){
445 guint32 version_string_len;
448 image->md_version_major = read16 (ptr);
450 image->md_version_minor = read16 (ptr);
453 version_string_len = read32 (ptr);
455 image->version = g_strndup (ptr, version_string_len);
456 ptr += version_string_len;
457 pad = ptr - image->raw_metadata;
459 ptr += 4 - (pad % 4);
463 /* skip over flags */
466 streams = read16 (ptr);
469 for (i = 0; i < streams; i++){
470 if (strncmp (ptr + 8, "#~", 3) == 0){
471 image->heap_tables.data = image->raw_metadata + read32 (ptr);
472 image->heap_tables.size = read32 (ptr + 4);
474 } else if (strncmp (ptr + 8, "#Strings", 9) == 0){
475 image->heap_strings.data = image->raw_metadata + read32 (ptr);
476 image->heap_strings.size = read32 (ptr + 4);
478 } else if (strncmp (ptr + 8, "#US", 4) == 0){
479 image->heap_us.data = image->raw_metadata + read32 (ptr);
480 image->heap_us.size = read32 (ptr + 4);
482 } else if (strncmp (ptr + 8, "#Blob", 6) == 0){
483 image->heap_blob.data = image->raw_metadata + read32 (ptr);
484 image->heap_blob.size = read32 (ptr + 4);
486 } else if (strncmp (ptr + 8, "#GUID", 6) == 0){
487 image->heap_guid.data = image->raw_metadata + read32 (ptr);
488 image->heap_guid.size = read32 (ptr + 4);
490 } else if (strncmp (ptr + 8, "#-", 3) == 0) {
491 image->heap_tables.data = image->raw_metadata + read32 (ptr);
492 image->heap_tables.size = read32 (ptr + 4);
494 image->uncompressed_metadata = TRUE;
495 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly '%s' has the non-standard metadata heap #-.\nRecompile it correctly (without the /incremental switch or in Release mode).\n", image->name);
496 } else if (strncmp (ptr + 8, "#Pdb", 5) == 0) {
497 image->heap_pdb.data = image->raw_metadata + read32 (ptr);
498 image->heap_pdb.size = read32 (ptr + 4);
501 g_message ("Unknown heap type: %s\n", ptr + 8);
502 ptr += 8 + strlen (ptr + 8) + 1;
504 pad = ptr - image->raw_metadata;
506 ptr += 4 - (pad % 4);
509 i = ((MonoImageLoader*)image->loader)->load_tables (image);
510 g_assert (image->heap_guid.data);
511 g_assert (image->heap_guid.size >= 16);
513 image->guid = mono_guid_to_string ((guint8*)image->heap_guid.data);
519 * Load representation of logical metadata tables, from the "#~" stream
522 load_tables (MonoImage *image)
524 const char *heap_tables = image->heap_tables.data;
527 int valid = 0, table;
530 heap_sizes = heap_tables [6];
531 image->idx_string_wide = ((heap_sizes & 0x01) == 1);
532 image->idx_guid_wide = ((heap_sizes & 0x02) == 2);
533 image->idx_blob_wide = ((heap_sizes & 0x04) == 4);
535 valid_mask = read64 (heap_tables + 8);
536 rows = (const guint32 *) (heap_tables + 24);
538 for (table = 0; table < 64; table++){
539 if ((valid_mask & ((guint64) 1 << table)) == 0){
540 if (table > MONO_TABLE_LAST)
542 image->tables [table].rows = 0;
545 if (table > MONO_TABLE_LAST) {
546 g_warning("bits in valid must be zero above 0x37 (II - 23.1.6)");
548 image->tables [table].rows = read32 (rows);
554 image->tables_base = (heap_tables + 24) + (4 * valid);
556 /* They must be the same */
557 g_assert ((const void *) image->tables_base == (const void *) rows);
559 mono_metadata_compute_table_bases (image);
564 mono_image_load_metadata (MonoImage *image, MonoCLIImageInfo *iinfo)
566 if (!load_metadata_ptrs (image, iinfo))
569 return load_tables (image);
573 mono_image_check_for_module_cctor (MonoImage *image)
575 MonoTableInfo *t, *mt;
576 t = &image->tables [MONO_TABLE_TYPEDEF];
577 mt = &image->tables [MONO_TABLE_METHOD];
578 if (image_is_dynamic (image)) {
580 image->checked_module_cctor = TRUE;
584 guint32 nameidx = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_NAME);
585 const char *name = mono_metadata_string_heap (image, nameidx);
586 if (strcmp (name, "<Module>") == 0) {
587 guint32 first_method = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_METHOD_LIST) - 1;
590 last_method = mono_metadata_decode_row_col (t, 1, MONO_TYPEDEF_METHOD_LIST) - 1;
592 last_method = mt->rows;
593 for (; first_method < last_method; first_method++) {
594 nameidx = mono_metadata_decode_row_col (mt, first_method, MONO_METHOD_NAME);
595 name = mono_metadata_string_heap (image, nameidx);
596 if (strcmp (name, ".cctor") == 0) {
597 image->has_module_cctor = TRUE;
598 image->checked_module_cctor = TRUE;
604 image->has_module_cctor = FALSE;
605 image->checked_module_cctor = TRUE;
609 load_modules (MonoImage *image)
616 t = &image->tables [MONO_TABLE_MODULEREF];
617 image->modules = g_new0 (MonoImage *, t->rows);
618 image->modules_loaded = g_new0 (gboolean, t->rows);
619 image->module_count = t->rows;
623 * mono_image_load_module:
625 * Load the module with the one-based index IDX from IMAGE and return it. Return NULL if
626 * it cannot be loaded.
629 mono_image_load_module (MonoImage *image, int idx)
632 MonoTableInfo *file_table;
635 gboolean refonly = image->ref_only;
636 GList *list_iter, *valid_modules = NULL;
637 MonoImageOpenStatus status;
639 if ((image->module_count == 0) || (idx > image->module_count || idx <= 0))
641 if (image->modules_loaded [idx - 1])
642 return image->modules [idx - 1];
644 file_table = &image->tables [MONO_TABLE_FILE];
645 for (i = 0; i < file_table->rows; i++) {
646 guint32 cols [MONO_FILE_SIZE];
647 mono_metadata_decode_row (file_table, i, cols, MONO_FILE_SIZE);
648 if (cols [MONO_FILE_FLAGS] == FILE_CONTAINS_NO_METADATA)
650 valid_modules = g_list_prepend (valid_modules, (char*)mono_metadata_string_heap (image, cols [MONO_FILE_NAME]));
653 t = &image->tables [MONO_TABLE_MODULEREF];
654 base_dir = g_path_get_dirname (image->name);
659 guint32 cols [MONO_MODULEREF_SIZE];
660 /* if there is no file table, we try to load the module... */
661 int valid = file_table->rows == 0;
663 mono_metadata_decode_row (t, idx - 1, cols, MONO_MODULEREF_SIZE);
664 name = mono_metadata_string_heap (image, cols [MONO_MODULEREF_NAME]);
665 for (list_iter = valid_modules; list_iter; list_iter = list_iter->next) {
666 /* be safe with string dups, but we could just compare string indexes */
667 if (strcmp (list_iter->data, name) == 0) {
673 module_ref = g_build_filename (base_dir, name, NULL);
674 image->modules [idx - 1] = mono_image_open_full (module_ref, &status, refonly);
675 if (image->modules [idx - 1]) {
676 mono_image_addref (image->modules [idx - 1]);
677 image->modules [idx - 1]->assembly = image->assembly;
679 if (image->modules [idx - 1]->is_module_handle)
680 mono_image_fixup_vtable (image->modules [idx - 1]);
682 /* g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly); */
688 image->modules_loaded [idx - 1] = TRUE;
691 g_list_free (valid_modules);
693 return image->modules [idx - 1];
697 class_key_extract (gpointer value)
699 MonoClass *class = value;
701 return GUINT_TO_POINTER (class->type_token);
705 class_next_value (gpointer value)
707 MonoClass *class = value;
709 return (gpointer*)&class->next_class_cache;
713 mono_image_init (MonoImage *image)
715 mono_mutex_init_recursive (&image->lock);
716 mono_mutex_init_recursive (&image->szarray_cache_lock);
718 image->mempool = mono_mempool_new_size (INITIAL_IMAGE_SIZE);
719 mono_internal_hash_table_init (&image->class_cache,
723 image->field_cache = mono_conc_hashtable_new (NULL, NULL);
725 image->typespec_cache = g_hash_table_new (NULL, NULL);
726 image->memberref_signatures = g_hash_table_new (NULL, NULL);
727 image->helper_signatures = g_hash_table_new (g_str_hash, g_str_equal);
728 image->method_signatures = g_hash_table_new (NULL, NULL);
730 image->property_hash = mono_property_hash_new ();
733 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
734 #define SWAP64(x) (x) = GUINT64_FROM_LE ((x))
735 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
736 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
737 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
746 * Returns < 0 to indicate an error.
749 do_load_header (MonoImage *image, MonoDotNetHeader *header, int offset)
751 MonoDotNetHeader64 header64;
754 if (!image->is_module_handle)
756 if (offset + sizeof (MonoDotNetHeader32) > image->raw_data_len)
759 memcpy (header, image->raw_data + offset, sizeof (MonoDotNetHeader));
761 if (header->pesig [0] != 'P' || header->pesig [1] != 'E')
764 /* endian swap the fields common between PE and PE+ */
765 SWAP32 (header->coff.coff_time);
766 SWAP32 (header->coff.coff_symptr);
767 SWAP32 (header->coff.coff_symcount);
768 SWAP16 (header->coff.coff_machine);
769 SWAP16 (header->coff.coff_sections);
770 SWAP16 (header->coff.coff_opt_header_size);
771 SWAP16 (header->coff.coff_attributes);
773 SWAP32 (header->pe.pe_code_size);
774 SWAP32 (header->pe.pe_uninit_data_size);
775 SWAP32 (header->pe.pe_rva_entry_point);
776 SWAP32 (header->pe.pe_rva_code_base);
777 SWAP32 (header->pe.pe_rva_data_base);
778 SWAP16 (header->pe.pe_magic);
780 /* now we are ready for the basic tests */
782 if (header->pe.pe_magic == 0x10B) {
783 offset += sizeof (MonoDotNetHeader);
784 SWAP32 (header->pe.pe_data_size);
785 if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader) - sizeof (MonoCOFFHeader) - 4))
788 SWAP32 (header->nt.pe_image_base); /* must be 0x400000 */
789 SWAP32 (header->nt.pe_stack_reserve);
790 SWAP32 (header->nt.pe_stack_commit);
791 SWAP32 (header->nt.pe_heap_reserve);
792 SWAP32 (header->nt.pe_heap_commit);
793 } else if (header->pe.pe_magic == 0x20B) {
794 /* PE32+ file format */
795 if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader64) - sizeof (MonoCOFFHeader) - 4))
797 memcpy (&header64, image->raw_data + offset, sizeof (MonoDotNetHeader64));
798 offset += sizeof (MonoDotNetHeader64);
799 /* copy the fields already swapped. the last field, pe_data_size, is missing */
800 memcpy (&header64, header, sizeof (MonoDotNetHeader) - 4);
801 /* FIXME: we lose bits here, but we don't use this stuff internally, so we don't care much.
802 * will be fixed when we change MonoDotNetHeader to not match the 32 bit variant
804 SWAP64 (header64.nt.pe_image_base);
805 header->nt.pe_image_base = header64.nt.pe_image_base;
806 SWAP64 (header64.nt.pe_stack_reserve);
807 header->nt.pe_stack_reserve = header64.nt.pe_stack_reserve;
808 SWAP64 (header64.nt.pe_stack_commit);
809 header->nt.pe_stack_commit = header64.nt.pe_stack_commit;
810 SWAP64 (header64.nt.pe_heap_reserve);
811 header->nt.pe_heap_reserve = header64.nt.pe_heap_reserve;
812 SWAP64 (header64.nt.pe_heap_commit);
813 header->nt.pe_heap_commit = header64.nt.pe_heap_commit;
815 header->nt.pe_section_align = header64.nt.pe_section_align;
816 header->nt.pe_file_alignment = header64.nt.pe_file_alignment;
817 header->nt.pe_os_major = header64.nt.pe_os_major;
818 header->nt.pe_os_minor = header64.nt.pe_os_minor;
819 header->nt.pe_user_major = header64.nt.pe_user_major;
820 header->nt.pe_user_minor = header64.nt.pe_user_minor;
821 header->nt.pe_subsys_major = header64.nt.pe_subsys_major;
822 header->nt.pe_subsys_minor = header64.nt.pe_subsys_minor;
823 header->nt.pe_reserved_1 = header64.nt.pe_reserved_1;
824 header->nt.pe_image_size = header64.nt.pe_image_size;
825 header->nt.pe_header_size = header64.nt.pe_header_size;
826 header->nt.pe_checksum = header64.nt.pe_checksum;
827 header->nt.pe_subsys_required = header64.nt.pe_subsys_required;
828 header->nt.pe_dll_flags = header64.nt.pe_dll_flags;
829 header->nt.pe_loader_flags = header64.nt.pe_loader_flags;
830 header->nt.pe_data_dir_count = header64.nt.pe_data_dir_count;
832 /* copy the datadir */
833 memcpy (&header->datadir, &header64.datadir, sizeof (MonoPEDatadir));
838 /* MonoPEHeaderNT: not used yet */
839 SWAP32 (header->nt.pe_section_align); /* must be 8192 */
840 SWAP32 (header->nt.pe_file_alignment); /* must be 512 or 4096 */
841 SWAP16 (header->nt.pe_os_major); /* must be 4 */
842 SWAP16 (header->nt.pe_os_minor); /* must be 0 */
843 SWAP16 (header->nt.pe_user_major);
844 SWAP16 (header->nt.pe_user_minor);
845 SWAP16 (header->nt.pe_subsys_major);
846 SWAP16 (header->nt.pe_subsys_minor);
847 SWAP32 (header->nt.pe_reserved_1);
848 SWAP32 (header->nt.pe_image_size);
849 SWAP32 (header->nt.pe_header_size);
850 SWAP32 (header->nt.pe_checksum);
851 SWAP16 (header->nt.pe_subsys_required);
852 SWAP16 (header->nt.pe_dll_flags);
853 SWAP32 (header->nt.pe_loader_flags);
854 SWAP32 (header->nt.pe_data_dir_count);
856 /* MonoDotNetHeader: mostly unused */
857 SWAPPDE (header->datadir.pe_export_table);
858 SWAPPDE (header->datadir.pe_import_table);
859 SWAPPDE (header->datadir.pe_resource_table);
860 SWAPPDE (header->datadir.pe_exception_table);
861 SWAPPDE (header->datadir.pe_certificate_table);
862 SWAPPDE (header->datadir.pe_reloc_table);
863 SWAPPDE (header->datadir.pe_debug);
864 SWAPPDE (header->datadir.pe_copyright);
865 SWAPPDE (header->datadir.pe_global_ptr);
866 SWAPPDE (header->datadir.pe_tls_table);
867 SWAPPDE (header->datadir.pe_load_config_table);
868 SWAPPDE (header->datadir.pe_bound_import);
869 SWAPPDE (header->datadir.pe_iat);
870 SWAPPDE (header->datadir.pe_delay_import_desc);
871 SWAPPDE (header->datadir.pe_cli_header);
872 SWAPPDE (header->datadir.pe_reserved);
875 if (image->is_module_handle)
876 image->raw_data_len = header->nt.pe_image_size;
883 mono_image_load_pe_data (MonoImage *image)
885 return ((MonoImageLoader*)image->loader)->load_pe_data (image);
889 pe_image_load_pe_data (MonoImage *image)
891 MonoCLIImageInfo *iinfo;
892 MonoDotNetHeader *header;
893 MonoMSDOSHeader msdos;
896 iinfo = image->image_info;
897 header = &iinfo->cli_header;
900 if (!image->is_module_handle)
902 if (offset + sizeof (msdos) > image->raw_data_len)
904 memcpy (&msdos, image->raw_data + offset, sizeof (msdos));
906 if (!(msdos.msdos_sig [0] == 'M' && msdos.msdos_sig [1] == 'Z'))
909 msdos.pe_offset = GUINT32_FROM_LE (msdos.pe_offset);
911 offset = msdos.pe_offset;
913 offset = do_load_header (image, header, offset);
918 * this tests for a x86 machine type, but itanium, amd64 and others could be used, too.
920 if (header->coff.coff_machine != 0x14c)
926 * The spec says that this field should contain 6.0, but Visual Studio includes a new compiler,
927 * which produces binaries with 7.0. From Sergey:
929 * The reason is that MSVC7 uses traditional compile/link
930 * sequence for CIL executables, and VS.NET (and Framework
931 * SDK) includes linker version 7, that puts 7.0 in this
932 * field. That's why it's currently not possible to load VC
933 * binaries with Mono. This field is pretty much meaningless
934 * anyway (what linker?).
936 if (header->pe.pe_major != 6 || header->pe.pe_minor != 0)
941 * FIXME: byte swap all addresses here for header.
944 if (!load_section_tables (image, iinfo, offset))
954 mono_image_load_cli_data (MonoImage *image)
956 return ((MonoImageLoader*)image->loader)->load_cli_data (image);
960 pe_image_load_cli_data (MonoImage *image)
962 MonoCLIImageInfo *iinfo;
963 MonoDotNetHeader *header;
965 iinfo = image->image_info;
966 header = &iinfo->cli_header;
968 /* Load the CLI header */
969 if (!mono_image_load_cli_header (image, iinfo))
972 if (!mono_image_load_metadata (image, iinfo))
979 mono_image_load_names (MonoImage *image)
981 /* modules don't have an assembly table row */
982 if (image->tables [MONO_TABLE_ASSEMBLY].rows) {
983 image->assembly_name = mono_metadata_string_heap (image,
984 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY],
985 0, MONO_ASSEMBLY_NAME));
988 /* Portable pdb images don't have a MODULE row */
989 if (image->tables [MONO_TABLE_MODULE].rows) {
990 image->module_name = mono_metadata_string_heap (image,
991 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_MODULE],
992 0, MONO_MODULE_NAME));
997 pe_image_load_tables (MonoImage *image)
1003 pe_image_match (MonoImage *image)
1005 if (image->raw_data [0] == 'M' && image->raw_data [1] == 'Z')
1010 static const MonoImageLoader pe_loader = {
1012 pe_image_load_pe_data,
1013 pe_image_load_cli_data,
1014 pe_image_load_tables,
1018 install_pe_loader (void)
1020 mono_install_image_loader (&pe_loader);
1024 do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
1025 gboolean care_about_cli, gboolean care_about_pecoff)
1027 MonoCLIImageInfo *iinfo;
1028 MonoDotNetHeader *header;
1029 GSList *errors = NULL;
1032 mono_profiler_module_event (image, MONO_PROFILE_START_LOAD);
1034 mono_image_init (image);
1036 iinfo = image->image_info;
1037 header = &iinfo->cli_header;
1039 for (l = image_loaders; l; l = l->next) {
1040 MonoImageLoader *loader = l->data;
1041 if (loader->match (image)) {
1042 image->loader = loader;
1046 if (!image->loader) {
1047 *status = MONO_IMAGE_IMAGE_INVALID;
1052 *status = MONO_IMAGE_IMAGE_INVALID;
1054 if (care_about_pecoff == FALSE)
1057 if (!image->metadata_only) {
1058 if (image->loader == &pe_loader && !mono_verifier_verify_pe_data (image, &errors))
1061 if (!mono_image_load_pe_data (image))
1065 if (care_about_cli == FALSE) {
1069 if (image->loader == &pe_loader && !mono_verifier_verify_cli_data (image, &errors))
1072 if (!mono_image_load_cli_data (image))
1075 if (image->loader == &pe_loader && !mono_verifier_verify_table_data (image, &errors))
1078 mono_image_load_names (image);
1080 load_modules (image);
1083 mono_profiler_module_loaded (image, MONO_PROFILE_OK);
1085 *status = MONO_IMAGE_OK;
1091 MonoVerifyInfo *info = errors->data;
1092 g_warning ("Could not load image %s due to %s", image->name, info->message);
1093 mono_free_verify_list (errors);
1095 mono_profiler_module_loaded (image, MONO_PROFILE_FAILED);
1096 mono_image_close (image);
1101 do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
1102 gboolean care_about_cli, gboolean care_about_pecoff, gboolean refonly, gboolean metadata_only)
1104 MonoCLIImageInfo *iinfo;
1108 if ((filed = mono_file_map_open (fname)) == NULL){
1109 if (IS_PORTABILITY_SET) {
1110 gchar *ffname = mono_portability_find_file (fname, TRUE);
1112 filed = mono_file_map_open (ffname);
1117 if (filed == NULL) {
1119 *status = MONO_IMAGE_ERROR_ERRNO;
1124 image = g_new0 (MonoImage, 1);
1125 image->raw_buffer_used = TRUE;
1126 image->raw_data_len = mono_file_map_size (filed);
1127 image->raw_data = mono_file_map (image->raw_data_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (filed), 0, &image->raw_data_handle);
1128 #if defined(HAVE_MMAP) && !defined (HOST_WIN32)
1129 if (!image->raw_data) {
1130 image->fileio_used = TRUE;
1131 image->raw_data = mono_file_map_fileio (image->raw_data_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (filed), 0, &image->raw_data_handle);
1134 if (!image->raw_data) {
1135 mono_file_map_close (filed);
1138 *status = MONO_IMAGE_IMAGE_INVALID;
1141 iinfo = g_new0 (MonoCLIImageInfo, 1);
1142 image->image_info = iinfo;
1143 image->name = mono_path_resolve_symlinks (fname);
1144 image->ref_only = refonly;
1145 image->metadata_only = metadata_only;
1146 image->ref_count = 1;
1147 /* if MONO_SECURITY_MODE_CORE_CLR is set then determine if this image is platform code */
1148 image->core_clr_platform_code = mono_security_core_clr_determine_platform_image (image);
1150 mono_file_map_close (filed);
1151 return do_mono_image_load (image, status, care_about_cli, care_about_pecoff);
1155 * mono_image_loaded:
1156 * @name: path or assembly name of the image to load
1157 * @refonly: Check with respect to reflection-only loads?
1159 * This routine verifies that the given image is loaded.
1160 * It checks either reflection-only loads only, or normal loads only, as specified by parameter.
1162 * Returns: the loaded MonoImage, or NULL on failure.
1165 mono_image_loaded_full (const char *name, gboolean refonly)
1169 mono_images_lock ();
1170 res = g_hash_table_lookup (get_loaded_images_hash (refonly), name);
1172 res = g_hash_table_lookup (get_loaded_images_by_name_hash (refonly), name);
1173 mono_images_unlock ();
1179 * mono_image_loaded:
1180 * @name: path or assembly name of the image to load
1182 * This routine verifies that the given image is loaded. Reflection-only loads do not count.
1184 * Returns: the loaded MonoImage, or NULL on failure.
1187 mono_image_loaded (const char *name)
1189 return mono_image_loaded_full (name, FALSE);
1198 find_by_guid (gpointer key, gpointer val, gpointer user_data)
1200 GuidData *data = user_data;
1206 if (strcmp (data->guid, mono_image_get_guid (image)) == 0)
1211 mono_image_loaded_by_guid_full (const char *guid, gboolean refonly)
1214 GHashTable *loaded_images = get_loaded_images_hash (refonly);
1218 mono_images_lock ();
1219 g_hash_table_foreach (loaded_images, find_by_guid, &data);
1220 mono_images_unlock ();
1225 mono_image_loaded_by_guid (const char *guid)
1227 return mono_image_loaded_by_guid_full (guid, FALSE);
1231 register_image (MonoImage *image)
1234 GHashTable *loaded_images = get_loaded_images_hash (image->ref_only);
1236 mono_images_lock ();
1237 image2 = g_hash_table_lookup (loaded_images, image->name);
1240 /* Somebody else beat us to it */
1241 mono_image_addref (image2);
1242 mono_images_unlock ();
1243 mono_image_close (image);
1247 GHashTable *loaded_images_by_name = get_loaded_images_by_name_hash (image->ref_only);
1248 g_hash_table_insert (loaded_images, image->name, image);
1249 if (image->assembly_name && (g_hash_table_lookup (loaded_images_by_name, image->assembly_name) == NULL))
1250 g_hash_table_insert (loaded_images_by_name, (char *) image->assembly_name, image);
1251 mono_images_unlock ();
1257 mono_image_open_from_data_internal (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status, gboolean refonly, gboolean metadata_only, const char *name)
1259 MonoCLIImageInfo *iinfo;
1263 if (!data || !data_len) {
1265 *status = MONO_IMAGE_IMAGE_INVALID;
1270 datac = g_try_malloc (data_len);
1273 *status = MONO_IMAGE_ERROR_ERRNO;
1276 memcpy (datac, data, data_len);
1279 image = g_new0 (MonoImage, 1);
1280 image->raw_data = datac;
1281 image->raw_data_len = data_len;
1282 image->raw_data_allocated = need_copy;
1283 image->name = (name == NULL) ? g_strdup_printf ("data-%p", datac) : g_strdup(name);
1284 iinfo = g_new0 (MonoCLIImageInfo, 1);
1285 image->image_info = iinfo;
1286 image->ref_only = refonly;
1287 image->metadata_only = metadata_only;
1289 image = do_mono_image_load (image, status, TRUE, TRUE);
1293 return register_image (image);
1297 mono_image_open_from_data_with_name (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status, gboolean refonly, const char *name)
1299 return mono_image_open_from_data_internal (data, data_len, need_copy, status, refonly, FALSE, name);
1303 mono_image_open_from_data_full (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status, gboolean refonly)
1305 return mono_image_open_from_data_with_name (data, data_len, need_copy, status, refonly, NULL);
1309 mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status)
1311 return mono_image_open_from_data_full (data, data_len, need_copy, status, FALSE);
1315 /* fname is not duplicated. */
1317 mono_image_open_from_module_handle (HMODULE module_handle, char* fname, gboolean has_entry_point, MonoImageOpenStatus* status)
1320 MonoCLIImageInfo* iinfo;
1322 image = g_new0 (MonoImage, 1);
1323 image->raw_data = (char*) module_handle;
1324 image->is_module_handle = TRUE;
1325 iinfo = g_new0 (MonoCLIImageInfo, 1);
1326 image->image_info = iinfo;
1327 image->name = fname;
1328 image->ref_count = has_entry_point ? 0 : 1;
1329 image->has_entry_point = has_entry_point;
1331 image = do_mono_image_load (image, status, TRUE, TRUE);
1335 return register_image (image);
1340 mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean refonly)
1343 GHashTable *loaded_images = get_loaded_images_hash (refonly);
1346 g_return_val_if_fail (fname != NULL, NULL);
1349 // Win32 path: If we are running with mixed-mode assemblies enabled (ie have loaded mscoree.dll),
1350 // then assemblies need to be loaded with LoadLibrary:
1351 if (!refonly && coree_module_handle) {
1352 HMODULE module_handle;
1353 guint16 *fname_utf16;
1356 absfname = mono_path_resolve_symlinks (fname);
1359 /* There is little overhead because the OS loader lock is held by LoadLibrary. */
1360 mono_images_lock ();
1361 image = g_hash_table_lookup (loaded_images, absfname);
1362 if (image) { // Image already loaded
1363 g_assert (image->is_module_handle);
1364 if (image->has_entry_point && image->ref_count == 0) {
1365 /* Increment reference count on images loaded outside of the runtime. */
1366 fname_utf16 = g_utf8_to_utf16 (absfname, -1, NULL, NULL, NULL);
1367 /* The image is already loaded because _CorDllMain removes images from the hash. */
1368 module_handle = LoadLibrary (fname_utf16);
1369 g_assert (module_handle == (HMODULE) image->raw_data);
1371 mono_image_addref (image);
1372 mono_images_unlock ();
1374 g_free (fname_utf16);
1379 // Image not loaded, load it now
1380 fname_utf16 = g_utf8_to_utf16 (absfname, -1, NULL, NULL, NULL);
1381 module_handle = MonoLoadImage (fname_utf16);
1382 if (status && module_handle == NULL)
1383 last_error = GetLastError ();
1385 /* mono_image_open_from_module_handle is called by _CorDllMain. */
1386 image = g_hash_table_lookup (loaded_images, absfname);
1388 mono_image_addref (image);
1389 mono_images_unlock ();
1391 g_free (fname_utf16);
1393 if (module_handle == NULL) {
1397 if (last_error == ERROR_BAD_EXE_FORMAT || last_error == STATUS_INVALID_IMAGE_FORMAT)
1398 *status = MONO_IMAGE_IMAGE_INVALID;
1400 if (last_error == ERROR_FILE_NOT_FOUND || last_error == ERROR_PATH_NOT_FOUND)
1410 g_assert (image->is_module_handle);
1411 g_assert (image->has_entry_point);
1416 return mono_image_open_from_module_handle (module_handle, absfname, FALSE, status);
1420 absfname = mono_path_canonicalize (fname);
1423 * The easiest solution would be to do all the loading inside the mutex,
1424 * but that would lead to scalability problems. So we let the loading
1425 * happen outside the mutex, and if multiple threads happen to load
1426 * the same image, we discard all but the first copy.
1428 mono_images_lock ();
1429 image = g_hash_table_lookup (loaded_images, absfname);
1432 if (image) { // Image already loaded
1433 mono_image_addref (image);
1434 mono_images_unlock ();
1437 mono_images_unlock ();
1439 // Image not loaded, load it now
1440 image = do_mono_image_open (fname, status, TRUE, TRUE, refonly, FALSE);
1444 return register_image (image);
1449 * @fname: filename that points to the module we want to open
1450 * @status: An error condition is returned in this field
1452 * Returns: An open image of type %MonoImage or NULL on error.
1453 * The caller holds a temporary reference to the returned image which should be cleared
1454 * when no longer needed by calling mono_image_close ().
1455 * if NULL, then check the value of @status for details on the error
1458 mono_image_open (const char *fname, MonoImageOpenStatus *status)
1460 return mono_image_open_full (fname, status, FALSE);
1464 * mono_pe_file_open:
1465 * @fname: filename that points to the module we want to open
1466 * @status: An error condition is returned in this field
1468 * Returns: An open image of type %MonoImage or NULL on error. if
1469 * NULL, then check the value of @status for details on the error.
1470 * This variant for mono_image_open DOES NOT SET UP CLI METADATA.
1471 * It's just a PE file loader, used for FileVersionInfo. It also does
1472 * not use the image cache.
1475 mono_pe_file_open (const char *fname, MonoImageOpenStatus *status)
1477 g_return_val_if_fail (fname != NULL, NULL);
1479 return do_mono_image_open (fname, status, FALSE, TRUE, FALSE, FALSE);
1483 * mono_image_open_raw
1484 * @fname: filename that points to the module we want to open
1485 * @status: An error condition is returned in this field
1487 * Returns an image without loading neither pe or cli data.
1489 * Use mono_image_load_pe_data and mono_image_load_cli_data to load them.
1492 mono_image_open_raw (const char *fname, MonoImageOpenStatus *status)
1494 g_return_val_if_fail (fname != NULL, NULL);
1496 return do_mono_image_open (fname, status, FALSE, FALSE, FALSE, FALSE);
1500 * mono_image_open_metadata_only:
1502 * Open an image which contains metadata only without a PE header.
1505 mono_image_open_metadata_only (const char *fname, MonoImageOpenStatus *status)
1507 return do_mono_image_open (fname, status, TRUE, TRUE, FALSE, TRUE);
1511 mono_image_fixup_vtable (MonoImage *image)
1514 MonoCLIImageInfo *iinfo;
1516 MonoVTableFixup *vtfixup;
1522 g_assert (image->is_module_handle);
1524 iinfo = image->image_info;
1525 de = &iinfo->cli_cli_header.ch_vtable_fixups;
1526 if (!de->rva || !de->size)
1528 vtfixup = (MonoVTableFixup*) mono_image_rva_map (image, de->rva);
1532 count = de->size / sizeof (MonoVTableFixup);
1534 if (!vtfixup->rva || !vtfixup->count)
1537 slot = mono_image_rva_map (image, vtfixup->rva);
1539 slot_type = vtfixup->type;
1540 slot_count = vtfixup->count;
1541 if (slot_type & VTFIXUP_TYPE_32BIT)
1542 while (slot_count--) {
1543 *((guint32*) slot) = (guint32) mono_marshal_get_vtfixup_ftnptr (image, *((guint32*) slot), slot_type);
1544 slot = ((guint32*) slot) + 1;
1546 else if (slot_type & VTFIXUP_TYPE_64BIT)
1547 while (slot_count--) {
1548 *((guint64*) slot) = (guint64) mono_marshal_get_vtfixup_ftnptr (image, *((guint64*) slot), slot_type);
1549 slot = ((guint32*) slot) + 1;
1552 g_assert_not_reached();
1557 g_assert_not_reached();
1562 free_hash_table (gpointer key, gpointer val, gpointer user_data)
1564 g_hash_table_destroy ((GHashTable*)val);
1569 free_mr_signatures (gpointer key, gpointer val, gpointer user_data)
1571 mono_metadata_free_method_signature ((MonoMethodSignature*)val);
1576 free_array_cache_entry (gpointer key, gpointer val, gpointer user_data)
1578 g_slist_free ((GSList*)val);
1582 * mono_image_addref:
1583 * @image: The image file we wish to add a reference to
1585 * Increases the reference count of an image.
1588 mono_image_addref (MonoImage *image)
1590 InterlockedIncrement (&image->ref_count);
1594 mono_dynamic_stream_reset (MonoDynamicStream* stream)
1596 stream->alloc_size = stream->index = stream->offset = 0;
1597 g_free (stream->data);
1598 stream->data = NULL;
1600 g_hash_table_destroy (stream->hash);
1601 stream->hash = NULL;
1606 free_hash (GHashTable *hash)
1609 g_hash_table_destroy (hash);
1613 mono_wrapper_caches_free (MonoWrapperCaches *cache)
1615 free_hash (cache->delegate_invoke_cache);
1616 free_hash (cache->delegate_begin_invoke_cache);
1617 free_hash (cache->delegate_end_invoke_cache);
1618 free_hash (cache->runtime_invoke_cache);
1619 free_hash (cache->runtime_invoke_vtype_cache);
1621 free_hash (cache->delegate_abstract_invoke_cache);
1623 free_hash (cache->runtime_invoke_direct_cache);
1624 free_hash (cache->managed_wrapper_cache);
1626 free_hash (cache->native_wrapper_cache);
1627 free_hash (cache->native_wrapper_aot_cache);
1628 free_hash (cache->native_wrapper_check_cache);
1629 free_hash (cache->native_wrapper_aot_check_cache);
1631 free_hash (cache->native_func_wrapper_aot_cache);
1632 free_hash (cache->remoting_invoke_cache);
1633 free_hash (cache->synchronized_cache);
1634 free_hash (cache->unbox_wrapper_cache);
1635 free_hash (cache->cominterop_invoke_cache);
1636 free_hash (cache->cominterop_wrapper_cache);
1637 free_hash (cache->thunk_invoke_cache);
1641 * Returns whether mono_image_close_finish() must be called as well.
1642 * We must unload images in two steps because clearing the domain in
1643 * SGen requires the class metadata to be intact, but we need to free
1644 * the mono_g_hash_tables in case a collection occurs during domain
1645 * unloading and the roots would trip up the GC.
1648 mono_image_close_except_pools (MonoImage *image)
1651 GHashTable *loaded_images, *loaded_images_by_name;
1654 g_return_val_if_fail (image != NULL, FALSE);
1657 * Atomically decrement the refcount and remove ourselves from the hash tables, so
1658 * register_image () can't grab an image which is being closed.
1660 mono_images_lock ();
1662 if (InterlockedDecrement (&image->ref_count) > 0) {
1663 mono_images_unlock ();
1667 loaded_images = get_loaded_images_hash (image->ref_only);
1668 loaded_images_by_name = get_loaded_images_by_name_hash (image->ref_only);
1669 image2 = g_hash_table_lookup (loaded_images, image->name);
1670 if (image == image2) {
1671 /* This is not true if we are called from mono_image_open () */
1672 g_hash_table_remove (loaded_images, image->name);
1674 if (image->assembly_name && (g_hash_table_lookup (loaded_images_by_name, image->assembly_name) == image))
1675 g_hash_table_remove (loaded_images_by_name, (char *) image->assembly_name);
1677 mono_images_unlock ();
1680 if (image->is_module_handle && image->has_entry_point) {
1681 mono_images_lock ();
1682 if (image->ref_count == 0) {
1683 /* Image will be closed by _CorDllMain. */
1684 FreeLibrary ((HMODULE) image->raw_data);
1685 mono_images_unlock ();
1688 mono_images_unlock ();
1692 mono_profiler_module_event (image, MONO_PROFILE_START_UNLOAD);
1694 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading image %s [%p].", image->name, image);
1696 mono_image_invoke_unload_hook (image);
1698 mono_metadata_clean_for_image (image);
1701 * The caches inside a MonoImage might refer to metadata which is stored in referenced
1702 * assemblies, so we can't release these references in mono_assembly_close () since the
1703 * MonoImage might outlive its associated MonoAssembly.
1705 if (image->references && !image_is_dynamic (image)) {
1706 for (i = 0; i < image->nreferences; i++) {
1707 if (image->references [i] && image->references [i] != REFERENCE_MISSING) {
1708 if (!mono_assembly_close_except_image_pools (image->references [i]))
1709 image->references [i] = NULL;
1713 if (image->references) {
1714 g_free (image->references);
1715 image->references = NULL;
1720 mono_images_lock ();
1721 if (image->is_module_handle && !image->has_entry_point)
1722 FreeLibrary ((HMODULE) image->raw_data);
1723 mono_images_unlock ();
1726 if (image->raw_buffer_used) {
1727 if (image->raw_data != NULL) {
1729 if (image->fileio_used)
1730 mono_file_unmap_fileio (image->raw_data, image->raw_data_handle);
1733 mono_file_unmap (image->raw_data, image->raw_data_handle);
1737 if (image->raw_data_allocated) {
1738 /* FIXME: do we need this? (image is disposed anyway) */
1739 /* image->raw_metadata and cli_sections might lie inside image->raw_data */
1740 MonoCLIImageInfo *ii = image->image_info;
1742 if ((image->raw_metadata > image->raw_data) &&
1743 (image->raw_metadata <= (image->raw_data + image->raw_data_len)))
1744 image->raw_metadata = NULL;
1746 for (i = 0; i < ii->cli_section_count; i++)
1747 if (((char*)(ii->cli_sections [i]) > image->raw_data) &&
1748 ((char*)(ii->cli_sections [i]) <= ((char*)image->raw_data + image->raw_data_len)))
1749 ii->cli_sections [i] = NULL;
1751 g_free (image->raw_data);
1754 if (debug_assembly_unload) {
1755 image->name = g_strdup_printf ("%s - UNLOADED", image->name);
1757 g_free (image->name);
1758 g_free (image->guid);
1759 g_free (image->version);
1760 g_free (image->files);
1763 if (image->method_cache)
1764 g_hash_table_destroy (image->method_cache);
1765 if (image->methodref_cache)
1766 g_hash_table_destroy (image->methodref_cache);
1767 mono_internal_hash_table_destroy (&image->class_cache);
1768 mono_conc_hashtable_destroy (image->field_cache);
1769 if (image->array_cache) {
1770 g_hash_table_foreach (image->array_cache, free_array_cache_entry, NULL);
1771 g_hash_table_destroy (image->array_cache);
1773 if (image->szarray_cache)
1774 g_hash_table_destroy (image->szarray_cache);
1775 if (image->ptr_cache)
1776 g_hash_table_destroy (image->ptr_cache);
1777 if (image->name_cache) {
1778 g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
1779 g_hash_table_destroy (image->name_cache);
1782 free_hash (image->delegate_bound_static_invoke_cache);
1783 free_hash (image->runtime_invoke_vcall_cache);
1784 free_hash (image->ldfld_wrapper_cache);
1785 free_hash (image->ldflda_wrapper_cache);
1786 free_hash (image->stfld_wrapper_cache);
1787 free_hash (image->isinst_cache);
1788 free_hash (image->castclass_cache);
1789 free_hash (image->proxy_isinst_cache);
1790 free_hash (image->var_cache_slow);
1791 free_hash (image->mvar_cache_slow);
1792 free_hash (image->var_cache_constrained);
1793 free_hash (image->mvar_cache_constrained);
1794 free_hash (image->wrapper_param_names);
1795 free_hash (image->pinvoke_scopes);
1796 free_hash (image->pinvoke_scope_filenames);
1797 free_hash (image->native_func_wrapper_cache);
1798 free_hash (image->typespec_cache);
1800 mono_wrapper_caches_free (&image->wrapper_caches);
1802 for (i = 0; i < image->gshared_types_len; ++i)
1803 free_hash (image->gshared_types [i]);
1804 g_free (image->gshared_types);
1806 /* The ownership of signatures is not well defined */
1807 g_hash_table_destroy (image->memberref_signatures);
1808 g_hash_table_destroy (image->helper_signatures);
1809 g_hash_table_destroy (image->method_signatures);
1811 if (image->rgctx_template_hash)
1812 g_hash_table_destroy (image->rgctx_template_hash);
1814 if (image->property_hash)
1815 mono_property_hash_destroy (image->property_hash);
1818 reflection_info_unregister_classes is only required by dynamic images, which will not be properly
1819 cleared during shutdown as we don't perform regular appdomain unload for the root one.
1821 g_assert (!image->reflection_info_unregister_classes || mono_runtime_is_shutting_down ());
1822 image->reflection_info_unregister_classes = NULL;
1824 if (image->interface_bitset) {
1825 mono_unload_interface_ids (image->interface_bitset);
1826 mono_bitset_free (image->interface_bitset);
1828 if (image->image_info){
1829 MonoCLIImageInfo *ii = image->image_info;
1831 if (ii->cli_section_tables)
1832 g_free (ii->cli_section_tables);
1833 if (ii->cli_sections)
1834 g_free (ii->cli_sections);
1835 g_free (image->image_info);
1838 for (i = 0; i < image->module_count; ++i) {
1839 if (image->modules [i]) {
1840 if (!mono_image_close_except_pools (image->modules [i]))
1841 image->modules [i] = NULL;
1844 if (image->modules_loaded)
1845 g_free (image->modules_loaded);
1847 mono_mutex_destroy (&image->szarray_cache_lock);
1848 mono_mutex_destroy (&image->lock);
1850 /*g_print ("destroy image %p (dynamic: %d)\n", image, image->dynamic);*/
1851 if (image_is_dynamic (image)) {
1852 /* Dynamic images are GC_MALLOCed */
1853 g_free ((char*)image->module_name);
1854 mono_dynamic_image_free ((MonoDynamicImage*)image);
1857 mono_profiler_module_event (image, MONO_PROFILE_END_UNLOAD);
1863 mono_image_close_finish (MonoImage *image)
1867 if (image->references && !image_is_dynamic (image)) {
1868 for (i = 0; i < image->nreferences; i++) {
1869 if (image->references [i] && image->references [i] != REFERENCE_MISSING)
1870 mono_assembly_close_finish (image->references [i]);
1873 g_free (image->references);
1874 image->references = NULL;
1877 for (i = 0; i < image->module_count; ++i) {
1878 if (image->modules [i])
1879 mono_image_close_finish (image->modules [i]);
1882 g_free (image->modules);
1884 #ifndef DISABLE_PERFCOUNTERS
1885 mono_perfcounters->loader_bytes -= mono_mempool_get_allocated (image->mempool);
1888 if (!image_is_dynamic (image)) {
1889 if (debug_assembly_unload)
1890 mono_mempool_invalidate (image->mempool);
1892 mono_mempool_destroy (image->mempool);
1896 if (debug_assembly_unload)
1897 mono_mempool_invalidate (image->mempool);
1899 mono_mempool_destroy (image->mempool);
1900 mono_dynamic_image_free_image ((MonoDynamicImage*)image);
1907 * @image: The image file we wish to close
1909 * Closes an image file, deallocates all memory consumed and
1910 * unmaps all possible sections of the file
1913 mono_image_close (MonoImage *image)
1915 if (mono_image_close_except_pools (image))
1916 mono_image_close_finish (image);
1920 * mono_image_strerror:
1921 * @status: an code indicating the result from a recent operation
1923 * Returns: a string describing the error
1926 mono_image_strerror (MonoImageOpenStatus status)
1931 case MONO_IMAGE_ERROR_ERRNO:
1932 return strerror (errno);
1933 case MONO_IMAGE_IMAGE_INVALID:
1934 return "File does not contain a valid CIL image";
1935 case MONO_IMAGE_MISSING_ASSEMBLYREF:
1936 return "An assembly was referenced, but could not be found";
1938 return "Internal error";
1942 mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
1943 guint32 lang_id, gunichar2 *name,
1944 MonoPEResourceDirEntry *entry,
1945 MonoPEResourceDir *root, guint32 level)
1947 gboolean is_string, is_dir;
1948 guint32 name_offset, dir_offset;
1950 /* Level 0 holds a directory entry for each type of resource
1951 * (identified by ID or name).
1953 * Level 1 holds a directory entry for each named resource
1954 * item, and each "anonymous" item of a particular type of
1957 * Level 2 holds a directory entry for each language pointing to
1960 is_string = MONO_PE_RES_DIR_ENTRY_NAME_IS_STRING (*entry);
1961 name_offset = MONO_PE_RES_DIR_ENTRY_NAME_OFFSET (*entry);
1963 is_dir = MONO_PE_RES_DIR_ENTRY_IS_DIR (*entry);
1964 dir_offset = MONO_PE_RES_DIR_ENTRY_DIR_OFFSET (*entry);
1969 } else if (level==1) {
1970 if (res_id != name_offset)
1974 is_string==TRUE && name!=lookup (name_offset)) {
1978 } else if (level==2) {
1979 if (is_string == TRUE || (is_string == FALSE && lang_id != 0 && name_offset != lang_id))
1982 g_assert_not_reached ();
1986 MonoPEResourceDir *res_dir=(MonoPEResourceDir *)(((char *)root)+dir_offset);
1987 MonoPEResourceDirEntry *sub_entries=(MonoPEResourceDirEntry *)(res_dir+1);
1990 entries = GUINT16_FROM_LE (res_dir->res_named_entries) + GUINT16_FROM_LE (res_dir->res_id_entries);
1992 for(i=0; i<entries; i++) {
1993 MonoPEResourceDirEntry *sub_entry=&sub_entries[i];
1996 ret=mono_image_walk_resource_tree (info, res_id,
2007 MonoPEResourceDataEntry *data_entry=(MonoPEResourceDataEntry *)((char *)(root)+dir_offset);
2008 MonoPEResourceDataEntry *res;
2010 res = g_new0 (MonoPEResourceDataEntry, 1);
2012 res->rde_data_offset = GUINT32_TO_LE (data_entry->rde_data_offset);
2013 res->rde_size = GUINT32_TO_LE (data_entry->rde_size);
2014 res->rde_codepage = GUINT32_TO_LE (data_entry->rde_codepage);
2015 res->rde_reserved = GUINT32_TO_LE (data_entry->rde_reserved);
2022 * mono_image_lookup_resource:
2023 * @image: the image to look up the resource in
2024 * @res_id: A MONO_PE_RESOURCE_ID_ that represents the resource ID to lookup.
2025 * @lang_id: The language id.
2026 * @name: the resource name to lookup.
2028 * Returns: NULL if not found, otherwise a pointer to the in-memory representation
2029 * of the given resource. The caller should free it using g_free () when no longer
2033 mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name)
2035 MonoCLIImageInfo *info;
2036 MonoDotNetHeader *header;
2037 MonoPEDatadir *datadir;
2038 MonoPEDirEntry *rsrc;
2039 MonoPEResourceDir *resource_dir;
2040 MonoPEResourceDirEntry *res_entries;
2047 mono_image_ensure_section_idx (image, MONO_SECTION_RSRC);
2049 info=image->image_info;
2054 header=&info->cli_header;
2059 datadir=&header->datadir;
2064 rsrc=&datadir->pe_resource_table;
2069 resource_dir=(MonoPEResourceDir *)mono_image_rva_map (image, rsrc->rva);
2070 if(resource_dir==NULL) {
2074 entries = GUINT16_FROM_LE (resource_dir->res_named_entries) + GUINT16_FROM_LE (resource_dir->res_id_entries);
2075 res_entries=(MonoPEResourceDirEntry *)(resource_dir+1);
2077 for(i=0; i<entries; i++) {
2078 MonoPEResourceDirEntry *entry=&res_entries[i];
2081 ret=mono_image_walk_resource_tree (info, res_id, lang_id,
2082 name, entry, resource_dir,
2093 * mono_image_get_entry_point:
2094 * @image: the image where the entry point will be looked up.
2096 * Use this routine to determine the metadata token for method that
2097 * has been flagged as the entry point.
2099 * Returns: the token for the entry point method in the image
2102 mono_image_get_entry_point (MonoImage *image)
2104 return ((MonoCLIImageInfo*)image->image_info)->cli_cli_header.ch_entry_point;
2108 * mono_image_get_resource:
2109 * @image: the image where the resource will be looked up.
2110 * @offset: The offset to add to the resource
2111 * @size: a pointer to an int where the size of the resource will be stored
2113 * This is a low-level routine that fetches a resource from the
2114 * metadata that starts at a given @offset. The @size parameter is
2115 * filled with the data field as encoded in the metadata.
2117 * Returns: the pointer to the resource whose offset is @offset.
2120 mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)
2122 MonoCLIImageInfo *iinfo = image->image_info;
2123 MonoCLIHeader *ch = &iinfo->cli_cli_header;
2126 if (!ch->ch_resources.rva || offset + 4 > ch->ch_resources.size)
2129 data = mono_image_rva_map (image, ch->ch_resources.rva);
2134 *size = read32 (data);
2140 mono_image_load_file_for_image (MonoImage *image, int fileidx)
2142 char *base_dir, *name;
2144 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
2148 if (fileidx < 1 || fileidx > t->rows)
2151 mono_image_lock (image);
2152 if (image->files && image->files [fileidx - 1]) {
2153 mono_image_unlock (image);
2154 return image->files [fileidx - 1];
2156 mono_image_unlock (image);
2158 fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
2159 fname = mono_metadata_string_heap (image, fname_id);
2160 base_dir = g_path_get_dirname (image->name);
2161 name = g_build_filename (base_dir, fname, NULL);
2162 res = mono_image_open (name, NULL);
2166 mono_image_lock (image);
2167 if (image->files && image->files [fileidx - 1]) {
2168 MonoImage *old = res;
2169 res = image->files [fileidx - 1];
2170 mono_image_unlock (image);
2171 mono_image_close (old);
2174 /* g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly); */
2175 res->assembly = image->assembly;
2176 for (i = 0; i < res->module_count; ++i) {
2177 if (res->modules [i] && !res->modules [i]->assembly)
2178 res->modules [i]->assembly = image->assembly;
2182 image->files = g_new0 (MonoImage*, t->rows);
2183 image->files [fileidx - 1] = res;
2184 mono_image_unlock (image);
2185 /* vtable fixup can't happen with the image lock held */
2187 if (res->is_module_handle)
2188 mono_image_fixup_vtable (res);
2199 * mono_image_get_strong_name:
2200 * @image: a MonoImage
2201 * @size: a guint32 pointer, or NULL.
2203 * If the image has a strong name, and @size is not NULL, the value
2204 * pointed to by size will have the size of the strong name.
2206 * Returns: NULL if the image does not have a strong name, or a
2207 * pointer to the public key.
2210 mono_image_get_strong_name (MonoImage *image, guint32 *size)
2212 MonoCLIImageInfo *iinfo = image->image_info;
2213 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
2216 if (!de->size || !de->rva)
2218 data = mono_image_rva_map (image, de->rva);
2227 * mono_image_strong_name_position:
2228 * @image: a MonoImage
2229 * @size: a guint32 pointer, or NULL.
2231 * If the image has a strong name, and @size is not NULL, the value
2232 * pointed to by size will have the size of the strong name.
2234 * Returns: the position within the image file where the strong name
2238 mono_image_strong_name_position (MonoImage *image, guint32 *size)
2240 MonoCLIImageInfo *iinfo = image->image_info;
2241 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
2246 if (!de->size || !de->rva)
2248 pos = mono_cli_rva_image_map (image, de->rva);
2249 return pos == INVALID_ADDRESS ? 0 : pos;
2253 * mono_image_get_public_key:
2254 * @image: a MonoImage
2255 * @size: a guint32 pointer, or NULL.
2257 * This is used to obtain the public key in the @image.
2259 * If the image has a public key, and @size is not NULL, the value
2260 * pointed to by size will have the size of the public key.
2262 * Returns: NULL if the image does not have a public key, or a pointer
2263 * to the public key.
2266 mono_image_get_public_key (MonoImage *image, guint32 *size)
2271 if (image_is_dynamic (image)) {
2273 *size = ((MonoDynamicImage*)image)->public_key_len;
2274 return (char*)((MonoDynamicImage*)image)->public_key;
2276 if (image->tables [MONO_TABLE_ASSEMBLY].rows != 1)
2278 tok = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY], 0, MONO_ASSEMBLY_PUBLIC_KEY);
2281 pubkey = mono_metadata_blob_heap (image, tok);
2282 len = mono_metadata_decode_blob_size (pubkey, &pubkey);
2289 * mono_image_get_name:
2290 * @name: a MonoImage
2292 * Returns: the name of the assembly.
2295 mono_image_get_name (MonoImage *image)
2297 return image->assembly_name;
2301 * mono_image_get_filename:
2302 * @image: a MonoImage
2304 * Used to get the filename that hold the actual MonoImage
2306 * Returns: the filename.
2309 mono_image_get_filename (MonoImage *image)
2315 mono_image_get_guid (MonoImage *image)
2320 const MonoTableInfo*
2321 mono_image_get_table_info (MonoImage *image, int table_id)
2323 if (table_id < 0 || table_id >= MONO_TABLE_NUM)
2325 return &image->tables [table_id];
2329 mono_image_get_table_rows (MonoImage *image, int table_id)
2331 if (table_id < 0 || table_id >= MONO_TABLE_NUM)
2333 return image->tables [table_id].rows;
2337 mono_table_info_get_rows (const MonoTableInfo *table)
2343 * mono_image_get_assembly:
2344 * @image: the MonoImage.
2346 * Use this routine to get the assembly that owns this image.
2348 * Returns: the assembly that holds this image.
2351 mono_image_get_assembly (MonoImage *image)
2353 return image->assembly;
2357 * mono_image_is_dynamic:
2358 * @image: the MonoImage
2360 * Determines if the given image was created dynamically through the
2361 * System.Reflection.Emit API
2363 * Returns: TRUE if the image was created dynamically, FALSE if not.
2366 mono_image_is_dynamic (MonoImage *image)
2368 return image_is_dynamic (image);
2372 * mono_image_has_authenticode_entry:
2373 * @image: the MonoImage
2375 * Use this routine to determine if the image has a Authenticode
2376 * Certificate Table.
2378 * Returns: TRUE if the image contains an authenticode entry in the PE
2382 mono_image_has_authenticode_entry (MonoImage *image)
2384 MonoCLIImageInfo *iinfo = image->image_info;
2385 MonoDotNetHeader *header = &iinfo->cli_header;
2386 MonoPEDirEntry *de = &header->datadir.pe_certificate_table;
2387 // the Authenticode "pre" (non ASN.1) header is 8 bytes long
2388 return ((de->rva != 0) && (de->size > 8));
2392 mono_image_alloc (MonoImage *image, guint size)
2396 #ifndef DISABLE_PERFCOUNTERS
2397 mono_perfcounters->loader_bytes += size;
2399 mono_image_lock (image);
2400 res = mono_mempool_alloc (image->mempool, size);
2401 mono_image_unlock (image);
2407 mono_image_alloc0 (MonoImage *image, guint size)
2411 #ifndef DISABLE_PERFCOUNTERS
2412 mono_perfcounters->loader_bytes += size;
2414 mono_image_lock (image);
2415 res = mono_mempool_alloc0 (image->mempool, size);
2416 mono_image_unlock (image);
2422 mono_image_strdup (MonoImage *image, const char *s)
2426 #ifndef DISABLE_PERFCOUNTERS
2427 mono_perfcounters->loader_bytes += strlen (s);
2429 mono_image_lock (image);
2430 res = mono_mempool_strdup (image->mempool, s);
2431 mono_image_unlock (image);
2437 g_list_prepend_image (MonoImage *image, GList *list, gpointer data)
2441 new_list = mono_image_alloc (image, sizeof (GList));
2442 new_list->data = data;
2443 new_list->prev = list ? list->prev : NULL;
2444 new_list->next = list;
2447 new_list->prev->next = new_list;
2449 list->prev = new_list;
2455 g_slist_append_image (MonoImage *image, GSList *list, gpointer data)
2459 new_list = mono_image_alloc (image, sizeof (GSList));
2460 new_list->data = data;
2461 new_list->next = NULL;
2463 return g_slist_concat (list, new_list);
2467 mono_image_lock (MonoImage *image)
2469 mono_locks_acquire (&image->lock, ImageDataLock);
2473 mono_image_unlock (MonoImage *image)
2475 mono_locks_release (&image->lock, ImageDataLock);
2480 * mono_image_property_lookup:
2482 * Lookup a property on @image. Used to store very rare fields of MonoClass and MonoMethod.
2484 * LOCKING: Takes the image lock
2487 mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property)
2491 mono_image_lock (image);
2492 res = mono_property_hash_lookup (image->property_hash, subject, property);
2493 mono_image_unlock (image);
2499 * mono_image_property_insert:
2501 * Insert a new property @property with value @value on @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
2503 * LOCKING: Takes the image lock
2506 mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value)
2508 mono_image_lock (image);
2509 mono_property_hash_insert (image->property_hash, subject, property, value);
2510 mono_image_unlock (image);
2514 * mono_image_property_remove:
2516 * Remove all properties associated with @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
2518 * LOCKING: Takes the image lock
2521 mono_image_property_remove (MonoImage *image, gpointer subject)
2523 mono_image_lock (image);
2524 mono_property_hash_remove_object (image->property_hash, subject);
2525 mono_image_unlock (image);
2529 mono_image_append_class_to_reflection_info_set (MonoClass *class)
2531 MonoImage *image = class->image;
2532 g_assert (image_is_dynamic (image));
2533 mono_image_lock (image);
2534 image->reflection_info_unregister_classes = g_slist_prepend_mempool (image->mempool, image->reflection_info_unregister_classes, class);
2535 mono_image_unlock (image);