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)
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
22 #include "mono-endian.h"
23 #include "tabledefs.h"
24 #include "tokentype.h"
25 #include "metadata-internals.h"
26 #include "profiler-private.h"
30 #include <mono/io-layer/io-layer.h>
31 #include <mono/utils/checked-build.h>
32 #include <mono/utils/mono-logger-internals.h>
33 #include <mono/utils/mono-path.h>
34 #include <mono/utils/mono-mmap.h>
35 #include <mono/utils/mono-io-portability.h>
36 #include <mono/utils/atomic.h>
37 #include <mono/metadata/class-internals.h>
38 #include <mono/metadata/assembly.h>
39 #include <mono/metadata/object-internals.h>
40 #include <mono/metadata/security-core-clr.h>
41 #include <mono/metadata/verify-internals.h>
42 #include <mono/metadata/verify.h>
43 #include <mono/metadata/image-internals.h>
44 #include <sys/types.h>
50 #define INVALID_ADDRESS 0xffffffff
52 // Amount initially reserved in each image's mempool.
53 // FIXME: This number is arbitrary, a more practical number should be found
54 #define INITIAL_IMAGE_SIZE 512
57 * The "loaded images" hashes keep track of the various assemblies and netmodules loaded
58 * There are four, for all combinations of [look up by path or assembly name?]
59 * and [normal or reflection-only load?, as in Assembly.ReflectionOnlyLoad]
63 IMAGES_HASH_PATH_REFONLY = 1,
65 IMAGES_HASH_NAME_REFONLY = 3,
68 static GHashTable *loaded_images_hashes [4] = {NULL, NULL, NULL, NULL};
71 get_loaded_images_hash (gboolean refonly)
73 int idx = refonly ? IMAGES_HASH_PATH_REFONLY : IMAGES_HASH_PATH;
74 return loaded_images_hashes [idx];
78 get_loaded_images_by_name_hash (gboolean refonly)
80 int idx = refonly ? IMAGES_HASH_NAME_REFONLY : IMAGES_HASH_NAME;
81 return loaded_images_hashes [idx];
84 // Change the assembly set in `image` to the assembly set in `assemblyImage`. Halt if overwriting is attempted.
85 // Can be used on modules loaded through either the "file" or "module" mechanism
87 assign_assembly_parent_for_netmodule (MonoImage *image, MonoImage *assemblyImage, MonoError *error)
90 MonoAssembly *assembly = assemblyImage->assembly;
93 // Assembly currently assigned
94 MonoAssembly *assemblyOld = image->assembly;
96 if (assemblyOld == assembly)
98 mono_error_set_bad_image (error, assemblyImage, "Attempted to load module %s which has already been loaded by assembly %s. This is not supported in Mono.", image->name, assemblyOld->image->name);
101 gpointer result = InterlockedExchangePointer((gpointer *)&image->assembly, assembly);
102 if (result == assembly)
107 static gboolean debug_assembly_unload = FALSE;
109 #define mono_images_lock() if (mutex_inited) mono_os_mutex_lock (&images_mutex)
110 #define mono_images_unlock() if (mutex_inited) mono_os_mutex_unlock (&images_mutex)
111 static gboolean mutex_inited;
112 static mono_mutex_t images_mutex;
114 static void install_pe_loader (void);
116 typedef struct ImageUnloadHook ImageUnloadHook;
117 struct ImageUnloadHook {
118 MonoImageUnloadFunc func;
122 static GSList *image_unload_hooks;
125 mono_install_image_unload_hook (MonoImageUnloadFunc func, gpointer user_data)
127 ImageUnloadHook *hook;
129 g_return_if_fail (func != NULL);
131 hook = g_new0 (ImageUnloadHook, 1);
133 hook->user_data = user_data;
134 image_unload_hooks = g_slist_prepend (image_unload_hooks, hook);
138 mono_remove_image_unload_hook (MonoImageUnloadFunc func, gpointer user_data)
141 ImageUnloadHook *hook;
143 for (l = image_unload_hooks; l; l = l->next) {
144 hook = (ImageUnloadHook *)l->data;
146 if (hook->func == func && hook->user_data == user_data) {
148 image_unload_hooks = g_slist_delete_link (image_unload_hooks, l);
155 mono_image_invoke_unload_hook (MonoImage *image)
158 ImageUnloadHook *hook;
160 for (l = image_unload_hooks; l; l = l->next) {
161 hook = (ImageUnloadHook *)l->data;
163 hook->func (image, hook->user_data);
167 static GSList *image_loaders;
170 mono_install_image_loader (const MonoImageLoader *loader)
172 image_loaders = g_slist_prepend (image_loaders, (MonoImageLoader*)loader);
175 /* returns offset relative to image->raw_data */
177 mono_cli_rva_image_map (MonoImage *image, guint32 addr)
179 MonoCLIImageInfo *iinfo = (MonoCLIImageInfo *)image->image_info;
180 const int top = iinfo->cli_section_count;
181 MonoSectionTable *tables = iinfo->cli_section_tables;
184 if (image->metadata_only)
187 for (i = 0; i < top; i++){
188 if ((addr >= tables->st_virtual_address) &&
189 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
191 if (image->is_module_handle)
194 return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
198 return INVALID_ADDRESS;
202 * mono_images_rva_map:
203 * @image: a MonoImage
204 * @addr: relative virtual address (RVA)
206 * This is a low-level routine used by the runtime to map relative
207 * virtual address (RVA) into their location in memory.
209 * Returns: the address in memory for the given RVA, or NULL if the
210 * RVA is not valid for this image.
213 mono_image_rva_map (MonoImage *image, guint32 addr)
215 MonoCLIImageInfo *iinfo = (MonoCLIImageInfo *)image->image_info;
216 const int top = iinfo->cli_section_count;
217 MonoSectionTable *tables = iinfo->cli_section_tables;
221 if (image->is_module_handle) {
222 if (addr && addr < image->raw_data_len)
223 return image->raw_data + addr;
229 for (i = 0; i < top; i++){
230 if ((addr >= tables->st_virtual_address) &&
231 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
232 if (!iinfo->cli_sections [i]) {
233 if (!mono_image_ensure_section_idx (image, i))
236 return (char*)iinfo->cli_sections [i] +
237 (addr - tables->st_virtual_address);
247 * Initialize the global variables used by this module.
250 mono_images_init (void)
252 mono_os_mutex_init_recursive (&images_mutex);
255 for(hash_idx = 0; hash_idx < IMAGES_HASH_COUNT; hash_idx++)
256 loaded_images_hashes [hash_idx] = g_hash_table_new (g_str_hash, g_str_equal);
258 debug_assembly_unload = g_getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
260 install_pe_loader ();
266 * mono_images_cleanup:
268 * Free all resources used by this module.
271 mono_images_cleanup (void)
276 mono_os_mutex_destroy (&images_mutex);
278 // If an assembly image is still loaded at shutdown, this could indicate managed code is still running.
279 // Reflection-only images being still loaded doesn't indicate anything as harmful, so we don't check for it.
280 g_hash_table_iter_init (&iter, get_loaded_images_hash (FALSE));
281 while (g_hash_table_iter_next (&iter, NULL, (void**)&image))
282 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly image '%s' still loaded at shutdown.", image->name);
285 for(hash_idx = 0; hash_idx < IMAGES_HASH_COUNT; hash_idx++)
286 g_hash_table_destroy (loaded_images_hashes [hash_idx]);
288 mutex_inited = FALSE;
292 * mono_image_ensure_section_idx:
293 * @image: The image we are operating on
294 * @section: section number that we will load/map into memory
296 * This routine makes sure that we have an in-memory copy of
297 * an image section (.text, .rsrc, .data).
299 * Returns: TRUE on success
302 mono_image_ensure_section_idx (MonoImage *image, int section)
304 MonoCLIImageInfo *iinfo = (MonoCLIImageInfo *)image->image_info;
305 MonoSectionTable *sect;
307 g_return_val_if_fail (section < iinfo->cli_section_count, FALSE);
309 if (iinfo->cli_sections [section] != NULL)
312 sect = &iinfo->cli_section_tables [section];
314 if (sect->st_raw_data_ptr + sect->st_raw_data_size > image->raw_data_len)
317 if (image->is_module_handle)
318 iinfo->cli_sections [section] = image->raw_data + sect->st_virtual_address;
321 /* FIXME: we ignore the writable flag since we don't patch the binary */
322 iinfo->cli_sections [section] = image->raw_data + sect->st_raw_data_ptr;
327 * mono_image_ensure_section:
328 * @image: The image we are operating on
329 * @section: section name that we will load/map into memory
331 * This routine makes sure that we have an in-memory copy of
332 * an image section (.text, .rsrc, .data).
334 * Returns: TRUE on success
337 mono_image_ensure_section (MonoImage *image, const char *section)
339 MonoCLIImageInfo *ii = (MonoCLIImageInfo *)image->image_info;
342 for (i = 0; i < ii->cli_section_count; i++){
343 if (strncmp (ii->cli_section_tables [i].st_name, section, 8) != 0)
346 return mono_image_ensure_section_idx (image, i);
352 load_section_tables (MonoImage *image, MonoCLIImageInfo *iinfo, guint32 offset)
354 const int top = iinfo->cli_header.coff.coff_sections;
357 iinfo->cli_section_count = top;
358 iinfo->cli_section_tables = g_new0 (MonoSectionTable, top);
359 iinfo->cli_sections = g_new0 (void *, top);
361 for (i = 0; i < top; i++){
362 MonoSectionTable *t = &iinfo->cli_section_tables [i];
364 if (offset + sizeof (MonoSectionTable) > image->raw_data_len)
366 memcpy (t, image->raw_data + offset, sizeof (MonoSectionTable));
367 offset += sizeof (MonoSectionTable);
369 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
370 t->st_virtual_size = GUINT32_FROM_LE (t->st_virtual_size);
371 t->st_virtual_address = GUINT32_FROM_LE (t->st_virtual_address);
372 t->st_raw_data_size = GUINT32_FROM_LE (t->st_raw_data_size);
373 t->st_raw_data_ptr = GUINT32_FROM_LE (t->st_raw_data_ptr);
374 t->st_reloc_ptr = GUINT32_FROM_LE (t->st_reloc_ptr);
375 t->st_lineno_ptr = GUINT32_FROM_LE (t->st_lineno_ptr);
376 t->st_reloc_count = GUINT16_FROM_LE (t->st_reloc_count);
377 t->st_line_count = GUINT16_FROM_LE (t->st_line_count);
378 t->st_flags = GUINT32_FROM_LE (t->st_flags);
380 /* consistency checks here */
387 mono_image_load_cli_header (MonoImage *image, MonoCLIImageInfo *iinfo)
391 offset = mono_cli_rva_image_map (image, iinfo->cli_header.datadir.pe_cli_header.rva);
392 if (offset == INVALID_ADDRESS)
395 if (offset + sizeof (MonoCLIHeader) > image->raw_data_len)
397 memcpy (&iinfo->cli_cli_header, image->raw_data + offset, sizeof (MonoCLIHeader));
399 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
400 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
401 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
402 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
403 SWAP32 (iinfo->cli_cli_header.ch_size);
404 SWAP32 (iinfo->cli_cli_header.ch_flags);
405 SWAP32 (iinfo->cli_cli_header.ch_entry_point);
406 SWAP16 (iinfo->cli_cli_header.ch_runtime_major);
407 SWAP16 (iinfo->cli_cli_header.ch_runtime_minor);
408 SWAPPDE (iinfo->cli_cli_header.ch_metadata);
409 SWAPPDE (iinfo->cli_cli_header.ch_resources);
410 SWAPPDE (iinfo->cli_cli_header.ch_strong_name);
411 SWAPPDE (iinfo->cli_cli_header.ch_code_manager_table);
412 SWAPPDE (iinfo->cli_cli_header.ch_vtable_fixups);
413 SWAPPDE (iinfo->cli_cli_header.ch_export_address_table_jumps);
414 SWAPPDE (iinfo->cli_cli_header.ch_eeinfo_table);
415 SWAPPDE (iinfo->cli_cli_header.ch_helper_table);
416 SWAPPDE (iinfo->cli_cli_header.ch_dynamic_info);
417 SWAPPDE (iinfo->cli_cli_header.ch_delay_load_info);
418 SWAPPDE (iinfo->cli_cli_header.ch_module_image);
419 SWAPPDE (iinfo->cli_cli_header.ch_external_fixups);
420 SWAPPDE (iinfo->cli_cli_header.ch_ridmap);
421 SWAPPDE (iinfo->cli_cli_header.ch_debug_map);
422 SWAPPDE (iinfo->cli_cli_header.ch_ip_map);
427 /* Catch new uses of the fields that are supposed to be zero */
429 if ((iinfo->cli_cli_header.ch_eeinfo_table.rva != 0) ||
430 (iinfo->cli_cli_header.ch_helper_table.rva != 0) ||
431 (iinfo->cli_cli_header.ch_dynamic_info.rva != 0) ||
432 (iinfo->cli_cli_header.ch_delay_load_info.rva != 0) ||
433 (iinfo->cli_cli_header.ch_module_image.rva != 0) ||
434 (iinfo->cli_cli_header.ch_external_fixups.rva != 0) ||
435 (iinfo->cli_cli_header.ch_ridmap.rva != 0) ||
436 (iinfo->cli_cli_header.ch_debug_map.rva != 0) ||
437 (iinfo->cli_cli_header.ch_ip_map.rva != 0)){
440 * No need to scare people who are testing this, I am just
441 * labelling this as a LAMESPEC
443 /* g_warning ("Some fields in the CLI header which should have been zero are not zero"); */
451 load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
453 guint32 offset, size;
459 offset = mono_cli_rva_image_map (image, iinfo->cli_cli_header.ch_metadata.rva);
460 if (offset == INVALID_ADDRESS)
463 size = iinfo->cli_cli_header.ch_metadata.size;
465 if (offset + size > image->raw_data_len)
467 image->raw_metadata = image->raw_data + offset;
469 /* 24.2.1: Metadata root starts here */
470 ptr = image->raw_metadata;
472 if (strncmp (ptr, "BSJB", 4) == 0){
473 guint32 version_string_len;
476 image->md_version_major = read16 (ptr);
478 image->md_version_minor = read16 (ptr);
481 version_string_len = read32 (ptr);
483 image->version = g_strndup (ptr, version_string_len);
484 ptr += version_string_len;
485 pad = ptr - image->raw_metadata;
487 ptr += 4 - (pad % 4);
491 /* skip over flags */
494 streams = read16 (ptr);
497 for (i = 0; i < streams; i++){
498 if (strncmp (ptr + 8, "#~", 3) == 0){
499 image->heap_tables.data = image->raw_metadata + read32 (ptr);
500 image->heap_tables.size = read32 (ptr + 4);
502 } else if (strncmp (ptr + 8, "#Strings", 9) == 0){
503 image->heap_strings.data = image->raw_metadata + read32 (ptr);
504 image->heap_strings.size = read32 (ptr + 4);
506 } else if (strncmp (ptr + 8, "#US", 4) == 0){
507 image->heap_us.data = image->raw_metadata + read32 (ptr);
508 image->heap_us.size = read32 (ptr + 4);
510 } else if (strncmp (ptr + 8, "#Blob", 6) == 0){
511 image->heap_blob.data = image->raw_metadata + read32 (ptr);
512 image->heap_blob.size = read32 (ptr + 4);
514 } else if (strncmp (ptr + 8, "#GUID", 6) == 0){
515 image->heap_guid.data = image->raw_metadata + read32 (ptr);
516 image->heap_guid.size = read32 (ptr + 4);
518 } else if (strncmp (ptr + 8, "#-", 3) == 0) {
519 image->heap_tables.data = image->raw_metadata + read32 (ptr);
520 image->heap_tables.size = read32 (ptr + 4);
522 image->uncompressed_metadata = TRUE;
523 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);
524 } else if (strncmp (ptr + 8, "#Pdb", 5) == 0) {
525 image->heap_pdb.data = image->raw_metadata + read32 (ptr);
526 image->heap_pdb.size = read32 (ptr + 4);
529 g_message ("Unknown heap type: %s\n", ptr + 8);
530 ptr += 8 + strlen (ptr + 8) + 1;
532 pad = ptr - image->raw_metadata;
534 ptr += 4 - (pad % 4);
537 i = ((MonoImageLoader*)image->loader)->load_tables (image);
538 g_assert (image->heap_guid.data);
540 if (!image->metadata_only) {
541 g_assert (image->heap_guid.size >= 16);
543 image->guid = mono_guid_to_string ((guint8*)image->heap_guid.data);
545 /* PPDB files have no guid */
546 guint8 empty_guid [16];
548 memset (empty_guid, 0, sizeof (empty_guid));
550 image->guid = mono_guid_to_string (empty_guid);
557 * Load representation of logical metadata tables, from the "#~" stream
560 load_tables (MonoImage *image)
562 const char *heap_tables = image->heap_tables.data;
565 int valid = 0, table;
568 heap_sizes = heap_tables [6];
569 image->idx_string_wide = ((heap_sizes & 0x01) == 1);
570 image->idx_guid_wide = ((heap_sizes & 0x02) == 2);
571 image->idx_blob_wide = ((heap_sizes & 0x04) == 4);
573 valid_mask = read64 (heap_tables + 8);
574 rows = (const guint32 *) (heap_tables + 24);
576 for (table = 0; table < 64; table++){
577 if ((valid_mask & ((guint64) 1 << table)) == 0){
578 if (table > MONO_TABLE_LAST)
580 image->tables [table].rows = 0;
583 if (table > MONO_TABLE_LAST) {
584 g_warning("bits in valid must be zero above 0x37 (II - 23.1.6)");
586 image->tables [table].rows = read32 (rows);
592 image->tables_base = (heap_tables + 24) + (4 * valid);
594 /* They must be the same */
595 g_assert ((const void *) image->tables_base == (const void *) rows);
597 mono_metadata_compute_table_bases (image);
602 mono_image_load_metadata (MonoImage *image, MonoCLIImageInfo *iinfo)
604 if (!load_metadata_ptrs (image, iinfo))
607 return load_tables (image);
611 mono_image_check_for_module_cctor (MonoImage *image)
613 MonoTableInfo *t, *mt;
614 t = &image->tables [MONO_TABLE_TYPEDEF];
615 mt = &image->tables [MONO_TABLE_METHOD];
616 if (image_is_dynamic (image)) {
618 image->checked_module_cctor = TRUE;
622 guint32 nameidx = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_NAME);
623 const char *name = mono_metadata_string_heap (image, nameidx);
624 if (strcmp (name, "<Module>") == 0) {
625 guint32 first_method = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_METHOD_LIST) - 1;
628 last_method = mono_metadata_decode_row_col (t, 1, MONO_TYPEDEF_METHOD_LIST) - 1;
630 last_method = mt->rows;
631 for (; first_method < last_method; first_method++) {
632 nameidx = mono_metadata_decode_row_col (mt, first_method, MONO_METHOD_NAME);
633 name = mono_metadata_string_heap (image, nameidx);
634 if (strcmp (name, ".cctor") == 0) {
635 image->has_module_cctor = TRUE;
636 image->checked_module_cctor = TRUE;
642 image->has_module_cctor = FALSE;
643 image->checked_module_cctor = TRUE;
647 load_modules (MonoImage *image)
654 t = &image->tables [MONO_TABLE_MODULEREF];
655 image->modules = g_new0 (MonoImage *, t->rows);
656 image->modules_loaded = g_new0 (gboolean, t->rows);
657 image->module_count = t->rows;
661 * mono_image_load_module_checked:
663 * Load the module with the one-based index IDX from IMAGE and return it. Return NULL if
664 * it cannot be loaded. NULL without MonoError being set will be interpreted as "not found".
667 mono_image_load_module_checked (MonoImage *image, int idx, MonoError *error)
670 MonoTableInfo *file_table;
673 gboolean refonly = image->ref_only;
674 GList *list_iter, *valid_modules = NULL;
675 MonoImageOpenStatus status;
677 mono_error_init (error);
679 if ((image->module_count == 0) || (idx > image->module_count || idx <= 0))
681 if (image->modules_loaded [idx - 1])
682 return image->modules [idx - 1];
684 file_table = &image->tables [MONO_TABLE_FILE];
685 for (i = 0; i < file_table->rows; i++) {
686 guint32 cols [MONO_FILE_SIZE];
687 mono_metadata_decode_row (file_table, i, cols, MONO_FILE_SIZE);
688 if (cols [MONO_FILE_FLAGS] == FILE_CONTAINS_NO_METADATA)
690 valid_modules = g_list_prepend (valid_modules, (char*)mono_metadata_string_heap (image, cols [MONO_FILE_NAME]));
693 t = &image->tables [MONO_TABLE_MODULEREF];
694 base_dir = g_path_get_dirname (image->name);
699 guint32 cols [MONO_MODULEREF_SIZE];
700 /* if there is no file table, we try to load the module... */
701 int valid = file_table->rows == 0;
703 mono_metadata_decode_row (t, idx - 1, cols, MONO_MODULEREF_SIZE);
704 name = mono_metadata_string_heap (image, cols [MONO_MODULEREF_NAME]);
705 for (list_iter = valid_modules; list_iter; list_iter = list_iter->next) {
706 /* be safe with string dups, but we could just compare string indexes */
707 if (strcmp (list_iter->data, name) == 0) {
713 module_ref = g_build_filename (base_dir, name, NULL);
714 MonoImage *moduleImage = mono_image_open_full (module_ref, &status, refonly);
716 if (!assign_assembly_parent_for_netmodule (moduleImage, image, error)) {
717 mono_image_close (moduleImage);
720 g_list_free (valid_modules);
724 image->modules [idx - 1] = image;
727 if (image->modules [idx - 1]->is_module_handle)
728 mono_image_fixup_vtable (image->modules [idx - 1]);
730 /* g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly); */
736 image->modules_loaded [idx - 1] = TRUE;
739 g_list_free (valid_modules);
741 return image->modules [idx - 1];
745 mono_image_load_module (MonoImage *image, int idx)
748 MonoImage *result = mono_image_load_module_checked (image, idx, &error);
749 mono_error_assert_ok (&error);
754 class_key_extract (gpointer value)
756 MonoClass *klass = (MonoClass *)value;
758 return GUINT_TO_POINTER (klass->type_token);
762 class_next_value (gpointer value)
764 MonoClassDef *klass = (MonoClassDef *)value;
766 return (gpointer*)&klass->next_class_cache;
770 mono_image_init (MonoImage *image)
772 mono_os_mutex_init_recursive (&image->lock);
773 mono_os_mutex_init_recursive (&image->szarray_cache_lock);
775 image->mempool = mono_mempool_new_size (INITIAL_IMAGE_SIZE);
776 mono_internal_hash_table_init (&image->class_cache,
780 image->field_cache = mono_conc_hashtable_new (NULL, NULL);
782 image->typespec_cache = g_hash_table_new (NULL, NULL);
783 image->memberref_signatures = g_hash_table_new (NULL, NULL);
784 image->helper_signatures = g_hash_table_new (g_str_hash, g_str_equal);
785 image->method_signatures = g_hash_table_new (NULL, NULL);
787 image->property_hash = mono_property_hash_new ();
790 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
791 #define SWAP64(x) (x) = GUINT64_FROM_LE ((x))
792 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
793 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
794 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
803 * Returns < 0 to indicate an error.
806 do_load_header (MonoImage *image, MonoDotNetHeader *header, int offset)
808 MonoDotNetHeader64 header64;
811 if (!image->is_module_handle)
813 if (offset + sizeof (MonoDotNetHeader32) > image->raw_data_len)
816 memcpy (header, image->raw_data + offset, sizeof (MonoDotNetHeader));
818 if (header->pesig [0] != 'P' || header->pesig [1] != 'E')
821 /* endian swap the fields common between PE and PE+ */
822 SWAP32 (header->coff.coff_time);
823 SWAP32 (header->coff.coff_symptr);
824 SWAP32 (header->coff.coff_symcount);
825 SWAP16 (header->coff.coff_machine);
826 SWAP16 (header->coff.coff_sections);
827 SWAP16 (header->coff.coff_opt_header_size);
828 SWAP16 (header->coff.coff_attributes);
830 SWAP32 (header->pe.pe_code_size);
831 SWAP32 (header->pe.pe_uninit_data_size);
832 SWAP32 (header->pe.pe_rva_entry_point);
833 SWAP32 (header->pe.pe_rva_code_base);
834 SWAP32 (header->pe.pe_rva_data_base);
835 SWAP16 (header->pe.pe_magic);
837 /* now we are ready for the basic tests */
839 if (header->pe.pe_magic == 0x10B) {
840 offset += sizeof (MonoDotNetHeader);
841 SWAP32 (header->pe.pe_data_size);
842 if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader) - sizeof (MonoCOFFHeader) - 4))
845 SWAP32 (header->nt.pe_image_base); /* must be 0x400000 */
846 SWAP32 (header->nt.pe_stack_reserve);
847 SWAP32 (header->nt.pe_stack_commit);
848 SWAP32 (header->nt.pe_heap_reserve);
849 SWAP32 (header->nt.pe_heap_commit);
850 } else if (header->pe.pe_magic == 0x20B) {
851 /* PE32+ file format */
852 if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader64) - sizeof (MonoCOFFHeader) - 4))
854 memcpy (&header64, image->raw_data + offset, sizeof (MonoDotNetHeader64));
855 offset += sizeof (MonoDotNetHeader64);
856 /* copy the fields already swapped. the last field, pe_data_size, is missing */
857 memcpy (&header64, header, sizeof (MonoDotNetHeader) - 4);
858 /* FIXME: we lose bits here, but we don't use this stuff internally, so we don't care much.
859 * will be fixed when we change MonoDotNetHeader to not match the 32 bit variant
861 SWAP64 (header64.nt.pe_image_base);
862 header->nt.pe_image_base = header64.nt.pe_image_base;
863 SWAP64 (header64.nt.pe_stack_reserve);
864 header->nt.pe_stack_reserve = header64.nt.pe_stack_reserve;
865 SWAP64 (header64.nt.pe_stack_commit);
866 header->nt.pe_stack_commit = header64.nt.pe_stack_commit;
867 SWAP64 (header64.nt.pe_heap_reserve);
868 header->nt.pe_heap_reserve = header64.nt.pe_heap_reserve;
869 SWAP64 (header64.nt.pe_heap_commit);
870 header->nt.pe_heap_commit = header64.nt.pe_heap_commit;
872 header->nt.pe_section_align = header64.nt.pe_section_align;
873 header->nt.pe_file_alignment = header64.nt.pe_file_alignment;
874 header->nt.pe_os_major = header64.nt.pe_os_major;
875 header->nt.pe_os_minor = header64.nt.pe_os_minor;
876 header->nt.pe_user_major = header64.nt.pe_user_major;
877 header->nt.pe_user_minor = header64.nt.pe_user_minor;
878 header->nt.pe_subsys_major = header64.nt.pe_subsys_major;
879 header->nt.pe_subsys_minor = header64.nt.pe_subsys_minor;
880 header->nt.pe_reserved_1 = header64.nt.pe_reserved_1;
881 header->nt.pe_image_size = header64.nt.pe_image_size;
882 header->nt.pe_header_size = header64.nt.pe_header_size;
883 header->nt.pe_checksum = header64.nt.pe_checksum;
884 header->nt.pe_subsys_required = header64.nt.pe_subsys_required;
885 header->nt.pe_dll_flags = header64.nt.pe_dll_flags;
886 header->nt.pe_loader_flags = header64.nt.pe_loader_flags;
887 header->nt.pe_data_dir_count = header64.nt.pe_data_dir_count;
889 /* copy the datadir */
890 memcpy (&header->datadir, &header64.datadir, sizeof (MonoPEDatadir));
895 /* MonoPEHeaderNT: not used yet */
896 SWAP32 (header->nt.pe_section_align); /* must be 8192 */
897 SWAP32 (header->nt.pe_file_alignment); /* must be 512 or 4096 */
898 SWAP16 (header->nt.pe_os_major); /* must be 4 */
899 SWAP16 (header->nt.pe_os_minor); /* must be 0 */
900 SWAP16 (header->nt.pe_user_major);
901 SWAP16 (header->nt.pe_user_minor);
902 SWAP16 (header->nt.pe_subsys_major);
903 SWAP16 (header->nt.pe_subsys_minor);
904 SWAP32 (header->nt.pe_reserved_1);
905 SWAP32 (header->nt.pe_image_size);
906 SWAP32 (header->nt.pe_header_size);
907 SWAP32 (header->nt.pe_checksum);
908 SWAP16 (header->nt.pe_subsys_required);
909 SWAP16 (header->nt.pe_dll_flags);
910 SWAP32 (header->nt.pe_loader_flags);
911 SWAP32 (header->nt.pe_data_dir_count);
913 /* MonoDotNetHeader: mostly unused */
914 SWAPPDE (header->datadir.pe_export_table);
915 SWAPPDE (header->datadir.pe_import_table);
916 SWAPPDE (header->datadir.pe_resource_table);
917 SWAPPDE (header->datadir.pe_exception_table);
918 SWAPPDE (header->datadir.pe_certificate_table);
919 SWAPPDE (header->datadir.pe_reloc_table);
920 SWAPPDE (header->datadir.pe_debug);
921 SWAPPDE (header->datadir.pe_copyright);
922 SWAPPDE (header->datadir.pe_global_ptr);
923 SWAPPDE (header->datadir.pe_tls_table);
924 SWAPPDE (header->datadir.pe_load_config_table);
925 SWAPPDE (header->datadir.pe_bound_import);
926 SWAPPDE (header->datadir.pe_iat);
927 SWAPPDE (header->datadir.pe_delay_import_desc);
928 SWAPPDE (header->datadir.pe_cli_header);
929 SWAPPDE (header->datadir.pe_reserved);
932 if (image->is_module_handle)
933 image->raw_data_len = header->nt.pe_image_size;
940 mono_image_load_pe_data (MonoImage *image)
942 return ((MonoImageLoader*)image->loader)->load_pe_data (image);
946 pe_image_load_pe_data (MonoImage *image)
948 MonoCLIImageInfo *iinfo;
949 MonoDotNetHeader *header;
950 MonoMSDOSHeader msdos;
953 iinfo = (MonoCLIImageInfo *)image->image_info;
954 header = &iinfo->cli_header;
957 if (!image->is_module_handle)
959 if (offset + sizeof (msdos) > image->raw_data_len)
961 memcpy (&msdos, image->raw_data + offset, sizeof (msdos));
963 if (!(msdos.msdos_sig [0] == 'M' && msdos.msdos_sig [1] == 'Z'))
966 msdos.pe_offset = GUINT32_FROM_LE (msdos.pe_offset);
968 offset = msdos.pe_offset;
970 offset = do_load_header (image, header, offset);
975 * this tests for a x86 machine type, but itanium, amd64 and others could be used, too.
977 if (header->coff.coff_machine != 0x14c)
983 * The spec says that this field should contain 6.0, but Visual Studio includes a new compiler,
984 * which produces binaries with 7.0. From Sergey:
986 * The reason is that MSVC7 uses traditional compile/link
987 * sequence for CIL executables, and VS.NET (and Framework
988 * SDK) includes linker version 7, that puts 7.0 in this
989 * field. That's why it's currently not possible to load VC
990 * binaries with Mono. This field is pretty much meaningless
991 * anyway (what linker?).
993 if (header->pe.pe_major != 6 || header->pe.pe_minor != 0)
998 * FIXME: byte swap all addresses here for header.
1001 if (!load_section_tables (image, iinfo, offset))
1011 mono_image_load_cli_data (MonoImage *image)
1013 return ((MonoImageLoader*)image->loader)->load_cli_data (image);
1017 pe_image_load_cli_data (MonoImage *image)
1019 MonoCLIImageInfo *iinfo;
1020 MonoDotNetHeader *header;
1022 iinfo = (MonoCLIImageInfo *)image->image_info;
1023 header = &iinfo->cli_header;
1025 /* Load the CLI header */
1026 if (!mono_image_load_cli_header (image, iinfo))
1029 if (!mono_image_load_metadata (image, iinfo))
1036 mono_image_load_names (MonoImage *image)
1038 /* modules don't have an assembly table row */
1039 if (image->tables [MONO_TABLE_ASSEMBLY].rows) {
1040 image->assembly_name = mono_metadata_string_heap (image,
1041 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY],
1042 0, MONO_ASSEMBLY_NAME));
1045 /* Portable pdb images don't have a MODULE row */
1046 if (image->tables [MONO_TABLE_MODULE].rows) {
1047 image->module_name = mono_metadata_string_heap (image,
1048 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_MODULE],
1049 0, MONO_MODULE_NAME));
1054 pe_image_load_tables (MonoImage *image)
1060 pe_image_match (MonoImage *image)
1062 if (image->raw_data [0] == 'M' && image->raw_data [1] == 'Z')
1067 static const MonoImageLoader pe_loader = {
1069 pe_image_load_pe_data,
1070 pe_image_load_cli_data,
1071 pe_image_load_tables,
1075 install_pe_loader (void)
1077 mono_install_image_loader (&pe_loader);
1081 do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
1082 gboolean care_about_cli, gboolean care_about_pecoff)
1084 MonoCLIImageInfo *iinfo;
1085 MonoDotNetHeader *header;
1086 GSList *errors = NULL;
1089 mono_profiler_module_event (image, MONO_PROFILE_START_LOAD);
1091 mono_image_init (image);
1093 iinfo = (MonoCLIImageInfo *)image->image_info;
1094 header = &iinfo->cli_header;
1096 if (!image->metadata_only) {
1097 for (l = image_loaders; l; l = l->next) {
1098 MonoImageLoader *loader = (MonoImageLoader *)l->data;
1099 if (loader->match (image)) {
1100 image->loader = loader;
1104 if (!image->loader) {
1106 *status = MONO_IMAGE_IMAGE_INVALID;
1111 *status = MONO_IMAGE_IMAGE_INVALID;
1113 if (care_about_pecoff == FALSE)
1116 if (image->loader == &pe_loader && !mono_verifier_verify_pe_data (image, &errors))
1119 if (!mono_image_load_pe_data (image))
1122 image->loader = (MonoImageLoader*)&pe_loader;
1125 if (care_about_cli == FALSE) {
1129 if (image->loader == &pe_loader && !image->metadata_only && !mono_verifier_verify_cli_data (image, &errors))
1132 if (!mono_image_load_cli_data (image))
1135 if (image->loader == &pe_loader && !image->metadata_only && !mono_verifier_verify_table_data (image, &errors))
1138 mono_image_load_names (image);
1140 load_modules (image);
1143 mono_profiler_module_loaded (image, MONO_PROFILE_OK);
1145 *status = MONO_IMAGE_OK;
1151 MonoVerifyInfo *info = (MonoVerifyInfo *)errors->data;
1152 g_warning ("Could not load image %s due to %s", image->name, info->message);
1153 mono_free_verify_list (errors);
1155 mono_profiler_module_loaded (image, MONO_PROFILE_FAILED);
1156 mono_image_close (image);
1161 do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
1162 gboolean care_about_cli, gboolean care_about_pecoff, gboolean refonly, gboolean metadata_only)
1164 MonoCLIImageInfo *iinfo;
1168 if ((filed = mono_file_map_open (fname)) == NULL){
1169 if (IS_PORTABILITY_SET) {
1170 gchar *ffname = mono_portability_find_file (fname, TRUE);
1172 filed = mono_file_map_open (ffname);
1177 if (filed == NULL) {
1179 *status = MONO_IMAGE_ERROR_ERRNO;
1184 image = g_new0 (MonoImage, 1);
1185 image->raw_buffer_used = TRUE;
1186 image->raw_data_len = mono_file_map_size (filed);
1187 image->raw_data = (char *)mono_file_map (image->raw_data_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (filed), 0, &image->raw_data_handle);
1188 #if defined(HAVE_MMAP) && !defined (HOST_WIN32)
1189 if (!image->raw_data) {
1190 image->fileio_used = TRUE;
1191 image->raw_data = (char *)mono_file_map_fileio (image->raw_data_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (filed), 0, &image->raw_data_handle);
1194 if (!image->raw_data) {
1195 mono_file_map_close (filed);
1198 *status = MONO_IMAGE_IMAGE_INVALID;
1201 iinfo = g_new0 (MonoCLIImageInfo, 1);
1202 image->image_info = iinfo;
1203 image->name = mono_path_resolve_symlinks (fname);
1204 image->ref_only = refonly;
1205 image->metadata_only = metadata_only;
1206 image->ref_count = 1;
1207 /* if MONO_SECURITY_MODE_CORE_CLR is set then determine if this image is platform code */
1208 image->core_clr_platform_code = mono_security_core_clr_determine_platform_image (image);
1210 mono_file_map_close (filed);
1211 return do_mono_image_load (image, status, care_about_cli, care_about_pecoff);
1215 * mono_image_loaded:
1216 * @name: path or assembly name of the image to load
1217 * @refonly: Check with respect to reflection-only loads?
1219 * This routine verifies that the given image is loaded.
1220 * It checks either reflection-only loads only, or normal loads only, as specified by parameter.
1222 * Returns: the loaded MonoImage, or NULL on failure.
1225 mono_image_loaded_full (const char *name, gboolean refonly)
1229 mono_images_lock ();
1230 res = (MonoImage *)g_hash_table_lookup (get_loaded_images_hash (refonly), name);
1232 res = (MonoImage *)g_hash_table_lookup (get_loaded_images_by_name_hash (refonly), name);
1233 mono_images_unlock ();
1239 * mono_image_loaded:
1240 * @name: path or assembly name of the image to load
1242 * This routine verifies that the given image is loaded. Reflection-only loads do not count.
1244 * Returns: the loaded MonoImage, or NULL on failure.
1247 mono_image_loaded (const char *name)
1249 return mono_image_loaded_full (name, FALSE);
1258 find_by_guid (gpointer key, gpointer val, gpointer user_data)
1260 GuidData *data = (GuidData *)user_data;
1265 image = (MonoImage *)val;
1266 if (strcmp (data->guid, mono_image_get_guid (image)) == 0)
1271 mono_image_loaded_by_guid_full (const char *guid, gboolean refonly)
1274 GHashTable *loaded_images = get_loaded_images_hash (refonly);
1278 mono_images_lock ();
1279 g_hash_table_foreach (loaded_images, find_by_guid, &data);
1280 mono_images_unlock ();
1285 mono_image_loaded_by_guid (const char *guid)
1287 return mono_image_loaded_by_guid_full (guid, FALSE);
1291 register_image (MonoImage *image)
1294 GHashTable *loaded_images = get_loaded_images_hash (image->ref_only);
1296 mono_images_lock ();
1297 image2 = (MonoImage *)g_hash_table_lookup (loaded_images, image->name);
1300 /* Somebody else beat us to it */
1301 mono_image_addref (image2);
1302 mono_images_unlock ();
1303 mono_image_close (image);
1307 GHashTable *loaded_images_by_name = get_loaded_images_by_name_hash (image->ref_only);
1308 g_hash_table_insert (loaded_images, image->name, image);
1309 if (image->assembly_name && (g_hash_table_lookup (loaded_images_by_name, image->assembly_name) == NULL))
1310 g_hash_table_insert (loaded_images_by_name, (char *) image->assembly_name, image);
1311 mono_images_unlock ();
1317 mono_image_open_from_data_internal (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status, gboolean refonly, gboolean metadata_only, const char *name)
1319 MonoCLIImageInfo *iinfo;
1323 if (!data || !data_len) {
1325 *status = MONO_IMAGE_IMAGE_INVALID;
1330 datac = (char *)g_try_malloc (data_len);
1333 *status = MONO_IMAGE_ERROR_ERRNO;
1336 memcpy (datac, data, data_len);
1339 image = g_new0 (MonoImage, 1);
1340 image->raw_data = datac;
1341 image->raw_data_len = data_len;
1342 image->raw_data_allocated = need_copy;
1343 image->name = (name == NULL) ? g_strdup_printf ("data-%p", datac) : g_strdup(name);
1344 iinfo = g_new0 (MonoCLIImageInfo, 1);
1345 image->image_info = iinfo;
1346 image->ref_only = refonly;
1347 image->metadata_only = metadata_only;
1348 image->ref_count = 1;
1350 image = do_mono_image_load (image, status, TRUE, TRUE);
1354 return register_image (image);
1358 mono_image_open_from_data_with_name (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status, gboolean refonly, const char *name)
1360 return mono_image_open_from_data_internal (data, data_len, need_copy, status, refonly, FALSE, name);
1364 mono_image_open_from_data_full (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status, gboolean refonly)
1366 return mono_image_open_from_data_with_name (data, data_len, need_copy, status, refonly, NULL);
1370 mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status)
1372 return mono_image_open_from_data_full (data, data_len, need_copy, status, FALSE);
1376 /* fname is not duplicated. */
1378 mono_image_open_from_module_handle (HMODULE module_handle, char* fname, gboolean has_entry_point, MonoImageOpenStatus* status)
1381 MonoCLIImageInfo* iinfo;
1383 image = g_new0 (MonoImage, 1);
1384 image->raw_data = (char*) module_handle;
1385 image->is_module_handle = TRUE;
1386 iinfo = g_new0 (MonoCLIImageInfo, 1);
1387 image->image_info = iinfo;
1388 image->name = fname;
1389 image->ref_count = has_entry_point ? 0 : 1;
1390 image->has_entry_point = has_entry_point;
1392 image = do_mono_image_load (image, status, TRUE, TRUE);
1396 return register_image (image);
1401 mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean refonly)
1404 GHashTable *loaded_images = get_loaded_images_hash (refonly);
1407 g_return_val_if_fail (fname != NULL, NULL);
1410 // Win32 path: If we are running with mixed-mode assemblies enabled (ie have loaded mscoree.dll),
1411 // then assemblies need to be loaded with LoadLibrary:
1412 if (!refonly && coree_module_handle) {
1413 HMODULE module_handle;
1414 guint16 *fname_utf16;
1417 absfname = mono_path_resolve_symlinks (fname);
1420 /* There is little overhead because the OS loader lock is held by LoadLibrary. */
1421 mono_images_lock ();
1422 image = g_hash_table_lookup (loaded_images, absfname);
1423 if (image) { // Image already loaded
1424 g_assert (image->is_module_handle);
1425 if (image->has_entry_point && image->ref_count == 0) {
1426 /* Increment reference count on images loaded outside of the runtime. */
1427 fname_utf16 = g_utf8_to_utf16 (absfname, -1, NULL, NULL, NULL);
1428 /* The image is already loaded because _CorDllMain removes images from the hash. */
1429 module_handle = LoadLibrary (fname_utf16);
1430 g_assert (module_handle == (HMODULE) image->raw_data);
1432 mono_image_addref (image);
1433 mono_images_unlock ();
1435 g_free (fname_utf16);
1440 // Image not loaded, load it now
1441 fname_utf16 = g_utf8_to_utf16 (absfname, -1, NULL, NULL, NULL);
1442 module_handle = MonoLoadImage (fname_utf16);
1443 if (status && module_handle == NULL)
1444 last_error = GetLastError ();
1446 /* mono_image_open_from_module_handle is called by _CorDllMain. */
1447 image = g_hash_table_lookup (loaded_images, absfname);
1449 mono_image_addref (image);
1450 mono_images_unlock ();
1452 g_free (fname_utf16);
1454 if (module_handle == NULL) {
1458 if (last_error == ERROR_BAD_EXE_FORMAT || last_error == STATUS_INVALID_IMAGE_FORMAT)
1459 *status = MONO_IMAGE_IMAGE_INVALID;
1461 if (last_error == ERROR_FILE_NOT_FOUND || last_error == ERROR_PATH_NOT_FOUND)
1471 g_assert (image->is_module_handle);
1472 g_assert (image->has_entry_point);
1477 return mono_image_open_from_module_handle (module_handle, absfname, FALSE, status);
1481 absfname = mono_path_canonicalize (fname);
1484 * The easiest solution would be to do all the loading inside the mutex,
1485 * but that would lead to scalability problems. So we let the loading
1486 * happen outside the mutex, and if multiple threads happen to load
1487 * the same image, we discard all but the first copy.
1489 mono_images_lock ();
1490 image = (MonoImage *)g_hash_table_lookup (loaded_images, absfname);
1493 if (image) { // Image already loaded
1494 mono_image_addref (image);
1495 mono_images_unlock ();
1498 mono_images_unlock ();
1500 // Image not loaded, load it now
1501 image = do_mono_image_open (fname, status, TRUE, TRUE, refonly, FALSE);
1505 return register_image (image);
1510 * @fname: filename that points to the module we want to open
1511 * @status: An error condition is returned in this field
1513 * Returns: An open image of type %MonoImage or NULL on error.
1514 * The caller holds a temporary reference to the returned image which should be cleared
1515 * when no longer needed by calling mono_image_close ().
1516 * if NULL, then check the value of @status for details on the error
1519 mono_image_open (const char *fname, MonoImageOpenStatus *status)
1521 return mono_image_open_full (fname, status, FALSE);
1525 * mono_pe_file_open:
1526 * @fname: filename that points to the module we want to open
1527 * @status: An error condition is returned in this field
1529 * Returns: An open image of type %MonoImage or NULL on error. if
1530 * NULL, then check the value of @status for details on the error.
1531 * This variant for mono_image_open DOES NOT SET UP CLI METADATA.
1532 * It's just a PE file loader, used for FileVersionInfo. It also does
1533 * not use the image cache.
1536 mono_pe_file_open (const char *fname, MonoImageOpenStatus *status)
1538 g_return_val_if_fail (fname != NULL, NULL);
1540 return do_mono_image_open (fname, status, FALSE, TRUE, FALSE, FALSE);
1544 * mono_image_open_raw
1545 * @fname: filename that points to the module we want to open
1546 * @status: An error condition is returned in this field
1548 * Returns an image without loading neither pe or cli data.
1550 * Use mono_image_load_pe_data and mono_image_load_cli_data to load them.
1553 mono_image_open_raw (const char *fname, MonoImageOpenStatus *status)
1555 g_return_val_if_fail (fname != NULL, NULL);
1557 return do_mono_image_open (fname, status, FALSE, FALSE, FALSE, FALSE);
1561 * mono_image_open_metadata_only:
1563 * Open an image which contains metadata only without a PE header.
1566 mono_image_open_metadata_only (const char *fname, MonoImageOpenStatus *status)
1568 return do_mono_image_open (fname, status, TRUE, TRUE, FALSE, TRUE);
1572 mono_image_fixup_vtable (MonoImage *image)
1575 MonoCLIImageInfo *iinfo;
1577 MonoVTableFixup *vtfixup;
1583 g_assert (image->is_module_handle);
1585 iinfo = image->image_info;
1586 de = &iinfo->cli_cli_header.ch_vtable_fixups;
1587 if (!de->rva || !de->size)
1589 vtfixup = (MonoVTableFixup*) mono_image_rva_map (image, de->rva);
1593 count = de->size / sizeof (MonoVTableFixup);
1595 if (!vtfixup->rva || !vtfixup->count)
1598 slot = mono_image_rva_map (image, vtfixup->rva);
1600 slot_type = vtfixup->type;
1601 slot_count = vtfixup->count;
1602 if (slot_type & VTFIXUP_TYPE_32BIT)
1603 while (slot_count--) {
1604 *((guint32*) slot) = (guint32) mono_marshal_get_vtfixup_ftnptr (image, *((guint32*) slot), slot_type);
1605 slot = ((guint32*) slot) + 1;
1607 else if (slot_type & VTFIXUP_TYPE_64BIT)
1608 while (slot_count--) {
1609 *((guint64*) slot) = (guint64) mono_marshal_get_vtfixup_ftnptr (image, *((guint64*) slot), slot_type);
1610 slot = ((guint32*) slot) + 1;
1613 g_assert_not_reached();
1618 g_assert_not_reached();
1623 free_hash_table (gpointer key, gpointer val, gpointer user_data)
1625 g_hash_table_destroy ((GHashTable*)val);
1630 free_mr_signatures (gpointer key, gpointer val, gpointer user_data)
1632 mono_metadata_free_method_signature ((MonoMethodSignature*)val);
1637 free_array_cache_entry (gpointer key, gpointer val, gpointer user_data)
1639 g_slist_free ((GSList*)val);
1643 * mono_image_addref:
1644 * @image: The image file we wish to add a reference to
1646 * Increases the reference count of an image.
1649 mono_image_addref (MonoImage *image)
1651 InterlockedIncrement (&image->ref_count);
1655 mono_dynamic_stream_reset (MonoDynamicStream* stream)
1657 stream->alloc_size = stream->index = stream->offset = 0;
1658 g_free (stream->data);
1659 stream->data = NULL;
1661 g_hash_table_destroy (stream->hash);
1662 stream->hash = NULL;
1667 free_hash (GHashTable *hash)
1670 g_hash_table_destroy (hash);
1674 mono_wrapper_caches_free (MonoWrapperCaches *cache)
1676 free_hash (cache->delegate_invoke_cache);
1677 free_hash (cache->delegate_begin_invoke_cache);
1678 free_hash (cache->delegate_end_invoke_cache);
1679 free_hash (cache->runtime_invoke_cache);
1680 free_hash (cache->runtime_invoke_vtype_cache);
1682 free_hash (cache->delegate_abstract_invoke_cache);
1684 free_hash (cache->runtime_invoke_direct_cache);
1685 free_hash (cache->managed_wrapper_cache);
1687 free_hash (cache->native_wrapper_cache);
1688 free_hash (cache->native_wrapper_aot_cache);
1689 free_hash (cache->native_wrapper_check_cache);
1690 free_hash (cache->native_wrapper_aot_check_cache);
1692 free_hash (cache->native_func_wrapper_aot_cache);
1693 free_hash (cache->remoting_invoke_cache);
1694 free_hash (cache->synchronized_cache);
1695 free_hash (cache->unbox_wrapper_cache);
1696 free_hash (cache->cominterop_invoke_cache);
1697 free_hash (cache->cominterop_wrapper_cache);
1698 free_hash (cache->thunk_invoke_cache);
1702 mono_image_close_except_pools_all (MonoImage**images, int image_count)
1704 for (int i = 0; i < image_count; ++i) {
1706 if (!mono_image_close_except_pools (images [i]))
1713 * Returns whether mono_image_close_finish() must be called as well.
1714 * We must unload images in two steps because clearing the domain in
1715 * SGen requires the class metadata to be intact, but we need to free
1716 * the mono_g_hash_tables in case a collection occurs during domain
1717 * unloading and the roots would trip up the GC.
1720 mono_image_close_except_pools (MonoImage *image)
1723 GHashTable *loaded_images, *loaded_images_by_name;
1726 g_return_val_if_fail (image != NULL, FALSE);
1729 * Atomically decrement the refcount and remove ourselves from the hash tables, so
1730 * register_image () can't grab an image which is being closed.
1732 mono_images_lock ();
1734 if (InterlockedDecrement (&image->ref_count) > 0) {
1735 mono_images_unlock ();
1739 loaded_images = get_loaded_images_hash (image->ref_only);
1740 loaded_images_by_name = get_loaded_images_by_name_hash (image->ref_only);
1741 image2 = (MonoImage *)g_hash_table_lookup (loaded_images, image->name);
1742 if (image == image2) {
1743 /* This is not true if we are called from mono_image_open () */
1744 g_hash_table_remove (loaded_images, image->name);
1746 if (image->assembly_name && (g_hash_table_lookup (loaded_images_by_name, image->assembly_name) == image))
1747 g_hash_table_remove (loaded_images_by_name, (char *) image->assembly_name);
1749 mono_images_unlock ();
1752 if (image->is_module_handle && image->has_entry_point) {
1753 mono_images_lock ();
1754 if (image->ref_count == 0) {
1755 /* Image will be closed by _CorDllMain. */
1756 FreeLibrary ((HMODULE) image->raw_data);
1757 mono_images_unlock ();
1760 mono_images_unlock ();
1764 mono_profiler_module_event (image, MONO_PROFILE_START_UNLOAD);
1766 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading image %s [%p].", image->name, image);
1768 mono_image_invoke_unload_hook (image);
1770 mono_metadata_clean_for_image (image);
1773 * The caches inside a MonoImage might refer to metadata which is stored in referenced
1774 * assemblies, so we can't release these references in mono_assembly_close () since the
1775 * MonoImage might outlive its associated MonoAssembly.
1777 if (image->references && !image_is_dynamic (image)) {
1778 for (i = 0; i < image->nreferences; i++) {
1779 if (image->references [i] && image->references [i] != REFERENCE_MISSING) {
1780 if (!mono_assembly_close_except_image_pools (image->references [i]))
1781 image->references [i] = NULL;
1785 if (image->references) {
1786 g_free (image->references);
1787 image->references = NULL;
1792 mono_images_lock ();
1793 if (image->is_module_handle && !image->has_entry_point)
1794 FreeLibrary ((HMODULE) image->raw_data);
1795 mono_images_unlock ();
1798 if (image->raw_buffer_used) {
1799 if (image->raw_data != NULL) {
1801 if (image->fileio_used)
1802 mono_file_unmap_fileio (image->raw_data, image->raw_data_handle);
1805 mono_file_unmap (image->raw_data, image->raw_data_handle);
1809 if (image->raw_data_allocated) {
1810 /* FIXME: do we need this? (image is disposed anyway) */
1811 /* image->raw_metadata and cli_sections might lie inside image->raw_data */
1812 MonoCLIImageInfo *ii = (MonoCLIImageInfo *)image->image_info;
1814 if ((image->raw_metadata > image->raw_data) &&
1815 (image->raw_metadata <= (image->raw_data + image->raw_data_len)))
1816 image->raw_metadata = NULL;
1818 for (i = 0; i < ii->cli_section_count; i++)
1819 if (((char*)(ii->cli_sections [i]) > image->raw_data) &&
1820 ((char*)(ii->cli_sections [i]) <= ((char*)image->raw_data + image->raw_data_len)))
1821 ii->cli_sections [i] = NULL;
1823 g_free (image->raw_data);
1826 if (debug_assembly_unload) {
1827 image->name = g_strdup_printf ("%s - UNLOADED", image->name);
1829 g_free (image->name);
1830 g_free (image->guid);
1831 g_free (image->version);
1834 if (image->method_cache)
1835 g_hash_table_destroy (image->method_cache);
1836 if (image->methodref_cache)
1837 g_hash_table_destroy (image->methodref_cache);
1838 mono_internal_hash_table_destroy (&image->class_cache);
1839 mono_conc_hashtable_destroy (image->field_cache);
1840 if (image->array_cache) {
1841 g_hash_table_foreach (image->array_cache, free_array_cache_entry, NULL);
1842 g_hash_table_destroy (image->array_cache);
1844 if (image->szarray_cache)
1845 g_hash_table_destroy (image->szarray_cache);
1846 if (image->ptr_cache)
1847 g_hash_table_destroy (image->ptr_cache);
1848 if (image->name_cache) {
1849 g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
1850 g_hash_table_destroy (image->name_cache);
1853 free_hash (image->delegate_bound_static_invoke_cache);
1854 free_hash (image->runtime_invoke_vcall_cache);
1855 free_hash (image->ldfld_wrapper_cache);
1856 free_hash (image->ldflda_wrapper_cache);
1857 free_hash (image->stfld_wrapper_cache);
1858 free_hash (image->isinst_cache);
1859 free_hash (image->castclass_cache);
1860 free_hash (image->icall_wrapper_cache);
1861 free_hash (image->proxy_isinst_cache);
1862 free_hash (image->var_cache_slow);
1863 free_hash (image->mvar_cache_slow);
1864 free_hash (image->var_cache_constrained);
1865 free_hash (image->mvar_cache_constrained);
1866 free_hash (image->wrapper_param_names);
1867 free_hash (image->pinvoke_scopes);
1868 free_hash (image->pinvoke_scope_filenames);
1869 free_hash (image->native_func_wrapper_cache);
1870 free_hash (image->typespec_cache);
1872 mono_wrapper_caches_free (&image->wrapper_caches);
1874 for (i = 0; i < image->gshared_types_len; ++i)
1875 free_hash (image->gshared_types [i]);
1876 g_free (image->gshared_types);
1878 /* The ownership of signatures is not well defined */
1879 g_hash_table_destroy (image->memberref_signatures);
1880 g_hash_table_destroy (image->helper_signatures);
1881 g_hash_table_destroy (image->method_signatures);
1883 if (image->rgctx_template_hash)
1884 g_hash_table_destroy (image->rgctx_template_hash);
1886 if (image->property_hash)
1887 mono_property_hash_destroy (image->property_hash);
1890 reflection_info_unregister_classes is only required by dynamic images, which will not be properly
1891 cleared during shutdown as we don't perform regular appdomain unload for the root one.
1893 g_assert (!image->reflection_info_unregister_classes || mono_runtime_is_shutting_down ());
1894 image->reflection_info_unregister_classes = NULL;
1896 if (image->interface_bitset) {
1897 mono_unload_interface_ids (image->interface_bitset);
1898 mono_bitset_free (image->interface_bitset);
1900 if (image->image_info){
1901 MonoCLIImageInfo *ii = (MonoCLIImageInfo *)image->image_info;
1903 if (ii->cli_section_tables)
1904 g_free (ii->cli_section_tables);
1905 if (ii->cli_sections)
1906 g_free (ii->cli_sections);
1907 g_free (image->image_info);
1910 mono_image_close_except_pools_all (image->files, image->file_count);
1911 mono_image_close_except_pools_all (image->modules, image->module_count);
1912 if (image->modules_loaded)
1913 g_free (image->modules_loaded);
1915 mono_os_mutex_destroy (&image->szarray_cache_lock);
1916 mono_os_mutex_destroy (&image->lock);
1918 /*g_print ("destroy image %p (dynamic: %d)\n", image, image->dynamic);*/
1919 if (image_is_dynamic (image)) {
1920 /* Dynamic images are GC_MALLOCed */
1921 g_free ((char*)image->module_name);
1922 mono_dynamic_image_free ((MonoDynamicImage*)image);
1925 mono_profiler_module_event (image, MONO_PROFILE_END_UNLOAD);
1931 mono_image_close_all (MonoImage**images, int image_count)
1933 for (int i = 0; i < image_count; ++i) {
1935 mono_image_close_finish (images [i]);
1942 mono_image_close_finish (MonoImage *image)
1946 if (image->references && !image_is_dynamic (image)) {
1947 for (i = 0; i < image->nreferences; i++) {
1948 if (image->references [i] && image->references [i] != REFERENCE_MISSING)
1949 mono_assembly_close_finish (image->references [i]);
1952 g_free (image->references);
1953 image->references = NULL;
1956 mono_image_close_all (image->files, image->file_count);
1957 mono_image_close_all (image->modules, image->module_count);
1959 #ifndef DISABLE_PERFCOUNTERS
1960 mono_perfcounters->loader_bytes -= mono_mempool_get_allocated (image->mempool);
1963 if (!image_is_dynamic (image)) {
1964 if (debug_assembly_unload)
1965 mono_mempool_invalidate (image->mempool);
1967 mono_mempool_destroy (image->mempool);
1971 if (debug_assembly_unload)
1972 mono_mempool_invalidate (image->mempool);
1974 mono_mempool_destroy (image->mempool);
1975 mono_dynamic_image_free_image ((MonoDynamicImage*)image);
1982 * @image: The image file we wish to close
1984 * Closes an image file, deallocates all memory consumed and
1985 * unmaps all possible sections of the file
1988 mono_image_close (MonoImage *image)
1990 if (mono_image_close_except_pools (image))
1991 mono_image_close_finish (image);
1995 * mono_image_strerror:
1996 * @status: an code indicating the result from a recent operation
1998 * Returns: a string describing the error
2001 mono_image_strerror (MonoImageOpenStatus status)
2006 case MONO_IMAGE_ERROR_ERRNO:
2007 return strerror (errno);
2008 case MONO_IMAGE_IMAGE_INVALID:
2009 return "File does not contain a valid CIL image";
2010 case MONO_IMAGE_MISSING_ASSEMBLYREF:
2011 return "An assembly was referenced, but could not be found";
2013 return "Internal error";
2017 mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
2018 guint32 lang_id, gunichar2 *name,
2019 MonoPEResourceDirEntry *entry,
2020 MonoPEResourceDir *root, guint32 level)
2022 gboolean is_string, is_dir;
2023 guint32 name_offset, dir_offset;
2025 /* Level 0 holds a directory entry for each type of resource
2026 * (identified by ID or name).
2028 * Level 1 holds a directory entry for each named resource
2029 * item, and each "anonymous" item of a particular type of
2032 * Level 2 holds a directory entry for each language pointing to
2035 is_string = MONO_PE_RES_DIR_ENTRY_NAME_IS_STRING (*entry);
2036 name_offset = MONO_PE_RES_DIR_ENTRY_NAME_OFFSET (*entry);
2038 is_dir = MONO_PE_RES_DIR_ENTRY_IS_DIR (*entry);
2039 dir_offset = MONO_PE_RES_DIR_ENTRY_DIR_OFFSET (*entry);
2044 } else if (level==1) {
2045 if (res_id != name_offset)
2049 is_string==TRUE && name!=lookup (name_offset)) {
2053 } else if (level==2) {
2054 if (is_string || (lang_id != 0 && name_offset != lang_id))
2057 g_assert_not_reached ();
2061 MonoPEResourceDir *res_dir=(MonoPEResourceDir *)(((char *)root)+dir_offset);
2062 MonoPEResourceDirEntry *sub_entries=(MonoPEResourceDirEntry *)(res_dir+1);
2065 entries = GUINT16_FROM_LE (res_dir->res_named_entries) + GUINT16_FROM_LE (res_dir->res_id_entries);
2067 for(i=0; i<entries; i++) {
2068 MonoPEResourceDirEntry *sub_entry=&sub_entries[i];
2071 ret=mono_image_walk_resource_tree (info, res_id,
2082 MonoPEResourceDataEntry *data_entry=(MonoPEResourceDataEntry *)((char *)(root)+dir_offset);
2083 MonoPEResourceDataEntry *res;
2085 res = g_new0 (MonoPEResourceDataEntry, 1);
2087 res->rde_data_offset = GUINT32_TO_LE (data_entry->rde_data_offset);
2088 res->rde_size = GUINT32_TO_LE (data_entry->rde_size);
2089 res->rde_codepage = GUINT32_TO_LE (data_entry->rde_codepage);
2090 res->rde_reserved = GUINT32_TO_LE (data_entry->rde_reserved);
2097 * mono_image_lookup_resource:
2098 * @image: the image to look up the resource in
2099 * @res_id: A MONO_PE_RESOURCE_ID_ that represents the resource ID to lookup.
2100 * @lang_id: The language id.
2101 * @name: the resource name to lookup.
2103 * Returns: NULL if not found, otherwise a pointer to the in-memory representation
2104 * of the given resource. The caller should free it using g_free () when no longer
2108 mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name)
2110 MonoCLIImageInfo *info;
2111 MonoDotNetHeader *header;
2112 MonoPEDatadir *datadir;
2113 MonoPEDirEntry *rsrc;
2114 MonoPEResourceDir *resource_dir;
2115 MonoPEResourceDirEntry *res_entries;
2122 mono_image_ensure_section_idx (image, MONO_SECTION_RSRC);
2124 info = (MonoCLIImageInfo *)image->image_info;
2129 header=&info->cli_header;
2134 datadir=&header->datadir;
2139 rsrc=&datadir->pe_resource_table;
2144 resource_dir=(MonoPEResourceDir *)mono_image_rva_map (image, rsrc->rva);
2145 if(resource_dir==NULL) {
2149 entries = GUINT16_FROM_LE (resource_dir->res_named_entries) + GUINT16_FROM_LE (resource_dir->res_id_entries);
2150 res_entries=(MonoPEResourceDirEntry *)(resource_dir+1);
2152 for(i=0; i<entries; i++) {
2153 MonoPEResourceDirEntry *entry=&res_entries[i];
2156 ret=mono_image_walk_resource_tree (info, res_id, lang_id,
2157 name, entry, resource_dir,
2168 * mono_image_get_entry_point:
2169 * @image: the image where the entry point will be looked up.
2171 * Use this routine to determine the metadata token for method that
2172 * has been flagged as the entry point.
2174 * Returns: the token for the entry point method in the image
2177 mono_image_get_entry_point (MonoImage *image)
2179 return ((MonoCLIImageInfo*)image->image_info)->cli_cli_header.ch_entry_point;
2183 * mono_image_get_resource:
2184 * @image: the image where the resource will be looked up.
2185 * @offset: The offset to add to the resource
2186 * @size: a pointer to an int where the size of the resource will be stored
2188 * This is a low-level routine that fetches a resource from the
2189 * metadata that starts at a given @offset. The @size parameter is
2190 * filled with the data field as encoded in the metadata.
2192 * Returns: the pointer to the resource whose offset is @offset.
2195 mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)
2197 MonoCLIImageInfo *iinfo = (MonoCLIImageInfo *)image->image_info;
2198 MonoCLIHeader *ch = &iinfo->cli_cli_header;
2201 if (!ch->ch_resources.rva || offset + 4 > ch->ch_resources.size)
2204 data = mono_image_rva_map (image, ch->ch_resources.rva);
2209 *size = read32 (data);
2214 // Returning NULL with no error set will be interpeted as "not found"
2216 mono_image_load_file_for_image_checked (MonoImage *image, int fileidx, MonoError *error)
2218 char *base_dir, *name;
2220 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
2224 mono_error_init (error);
2226 if (fileidx < 1 || fileidx > t->rows)
2229 mono_image_lock (image);
2230 if (image->files && image->files [fileidx - 1]) {
2231 mono_image_unlock (image);
2232 return image->files [fileidx - 1];
2234 mono_image_unlock (image);
2236 fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
2237 fname = mono_metadata_string_heap (image, fname_id);
2238 base_dir = g_path_get_dirname (image->name);
2239 name = g_build_filename (base_dir, fname, NULL);
2240 res = mono_image_open (name, NULL);
2244 mono_image_lock (image);
2245 if (image->files && image->files [fileidx - 1]) {
2246 MonoImage *old = res;
2247 res = image->files [fileidx - 1];
2248 mono_image_unlock (image);
2249 mono_image_close (old);
2252 /* g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly); */
2253 if (!assign_assembly_parent_for_netmodule (res, image, error)) {
2254 mono_image_unlock (image);
2255 mono_image_close (res);
2259 for (i = 0; i < res->module_count; ++i) {
2260 if (res->modules [i] && !res->modules [i]->assembly)
2261 res->modules [i]->assembly = image->assembly;
2264 if (!image->files) {
2265 image->files = g_new0 (MonoImage*, t->rows);
2266 image->file_count = t->rows;
2268 image->files [fileidx - 1] = res;
2269 mono_image_unlock (image);
2270 /* vtable fixup can't happen with the image lock held */
2272 if (res->is_module_handle)
2273 mono_image_fixup_vtable (res);
2284 mono_image_load_file_for_image (MonoImage *image, int fileidx)
2287 MonoImage *result = mono_image_load_file_for_image_checked (image, fileidx, &error);
2288 mono_error_assert_ok (&error);
2293 * mono_image_get_strong_name:
2294 * @image: a MonoImage
2295 * @size: a guint32 pointer, or NULL.
2297 * If the image has a strong name, and @size is not NULL, the value
2298 * pointed to by size will have the size of the strong name.
2300 * Returns: NULL if the image does not have a strong name, or a
2301 * pointer to the public key.
2304 mono_image_get_strong_name (MonoImage *image, guint32 *size)
2306 MonoCLIImageInfo *iinfo = (MonoCLIImageInfo *)image->image_info;
2307 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
2310 if (!de->size || !de->rva)
2312 data = mono_image_rva_map (image, de->rva);
2321 * mono_image_strong_name_position:
2322 * @image: a MonoImage
2323 * @size: a guint32 pointer, or NULL.
2325 * If the image has a strong name, and @size is not NULL, the value
2326 * pointed to by size will have the size of the strong name.
2328 * Returns: the position within the image file where the strong name
2332 mono_image_strong_name_position (MonoImage *image, guint32 *size)
2334 MonoCLIImageInfo *iinfo = (MonoCLIImageInfo *)image->image_info;
2335 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
2340 if (!de->size || !de->rva)
2342 pos = mono_cli_rva_image_map (image, de->rva);
2343 return pos == INVALID_ADDRESS ? 0 : pos;
2347 * mono_image_get_public_key:
2348 * @image: a MonoImage
2349 * @size: a guint32 pointer, or NULL.
2351 * This is used to obtain the public key in the @image.
2353 * If the image has a public key, and @size is not NULL, the value
2354 * pointed to by size will have the size of the public key.
2356 * Returns: NULL if the image does not have a public key, or a pointer
2357 * to the public key.
2360 mono_image_get_public_key (MonoImage *image, guint32 *size)
2365 if (image_is_dynamic (image)) {
2367 *size = ((MonoDynamicImage*)image)->public_key_len;
2368 return (char*)((MonoDynamicImage*)image)->public_key;
2370 if (image->tables [MONO_TABLE_ASSEMBLY].rows != 1)
2372 tok = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY], 0, MONO_ASSEMBLY_PUBLIC_KEY);
2375 pubkey = mono_metadata_blob_heap (image, tok);
2376 len = mono_metadata_decode_blob_size (pubkey, &pubkey);
2383 * mono_image_get_name:
2384 * @name: a MonoImage
2386 * Returns: the name of the assembly.
2389 mono_image_get_name (MonoImage *image)
2391 return image->assembly_name;
2395 * mono_image_get_filename:
2396 * @image: a MonoImage
2398 * Used to get the filename that hold the actual MonoImage
2400 * Returns: the filename.
2403 mono_image_get_filename (MonoImage *image)
2409 mono_image_get_guid (MonoImage *image)
2414 const MonoTableInfo*
2415 mono_image_get_table_info (MonoImage *image, int table_id)
2417 if (table_id < 0 || table_id >= MONO_TABLE_NUM)
2419 return &image->tables [table_id];
2423 mono_image_get_table_rows (MonoImage *image, int table_id)
2425 if (table_id < 0 || table_id >= MONO_TABLE_NUM)
2427 return image->tables [table_id].rows;
2431 mono_table_info_get_rows (const MonoTableInfo *table)
2437 * mono_image_get_assembly:
2438 * @image: the MonoImage.
2440 * Use this routine to get the assembly that owns this image.
2442 * Returns: the assembly that holds this image.
2445 mono_image_get_assembly (MonoImage *image)
2447 return image->assembly;
2451 * mono_image_is_dynamic:
2452 * @image: the MonoImage
2454 * Determines if the given image was created dynamically through the
2455 * System.Reflection.Emit API
2457 * Returns: TRUE if the image was created dynamically, FALSE if not.
2460 mono_image_is_dynamic (MonoImage *image)
2462 return image_is_dynamic (image);
2466 * mono_image_has_authenticode_entry:
2467 * @image: the MonoImage
2469 * Use this routine to determine if the image has a Authenticode
2470 * Certificate Table.
2472 * Returns: TRUE if the image contains an authenticode entry in the PE
2476 mono_image_has_authenticode_entry (MonoImage *image)
2478 MonoCLIImageInfo *iinfo = (MonoCLIImageInfo *)image->image_info;
2479 MonoDotNetHeader *header = &iinfo->cli_header;
2480 MonoPEDirEntry *de = &header->datadir.pe_certificate_table;
2481 // the Authenticode "pre" (non ASN.1) header is 8 bytes long
2482 return ((de->rva != 0) && (de->size > 8));
2486 mono_image_alloc (MonoImage *image, guint size)
2490 #ifndef DISABLE_PERFCOUNTERS
2491 mono_perfcounters->loader_bytes += size;
2493 mono_image_lock (image);
2494 res = mono_mempool_alloc (image->mempool, size);
2495 mono_image_unlock (image);
2501 mono_image_alloc0 (MonoImage *image, guint size)
2505 #ifndef DISABLE_PERFCOUNTERS
2506 mono_perfcounters->loader_bytes += size;
2508 mono_image_lock (image);
2509 res = mono_mempool_alloc0 (image->mempool, size);
2510 mono_image_unlock (image);
2516 mono_image_strdup (MonoImage *image, const char *s)
2520 #ifndef DISABLE_PERFCOUNTERS
2521 mono_perfcounters->loader_bytes += strlen (s);
2523 mono_image_lock (image);
2524 res = mono_mempool_strdup (image->mempool, s);
2525 mono_image_unlock (image);
2531 mono_image_strdup_vprintf (MonoImage *image, const char *format, va_list args)
2534 mono_image_lock (image);
2535 buf = mono_mempool_strdup_vprintf (image->mempool, format, args);
2536 mono_image_unlock (image);
2537 #ifndef DISABLE_PERFCOUNTERS
2538 mono_perfcounters->loader_bytes += strlen (buf);
2544 mono_image_strdup_printf (MonoImage *image, const char *format, ...)
2549 va_start (args, format);
2550 buf = mono_image_strdup_vprintf (image, format, args);
2556 g_list_prepend_image (MonoImage *image, GList *list, gpointer data)
2560 new_list = (GList *)mono_image_alloc (image, sizeof (GList));
2561 new_list->data = data;
2562 new_list->prev = list ? list->prev : NULL;
2563 new_list->next = list;
2566 new_list->prev->next = new_list;
2568 list->prev = new_list;
2574 g_slist_append_image (MonoImage *image, GSList *list, gpointer data)
2578 new_list = (GSList *)mono_image_alloc (image, sizeof (GSList));
2579 new_list->data = data;
2580 new_list->next = NULL;
2582 return g_slist_concat (list, new_list);
2586 mono_image_lock (MonoImage *image)
2588 mono_locks_os_acquire (&image->lock, ImageDataLock);
2592 mono_image_unlock (MonoImage *image)
2594 mono_locks_os_release (&image->lock, ImageDataLock);
2599 * mono_image_property_lookup:
2601 * Lookup a property on @image. Used to store very rare fields of MonoClass and MonoMethod.
2603 * LOCKING: Takes the image lock
2606 mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property)
2610 mono_image_lock (image);
2611 res = mono_property_hash_lookup (image->property_hash, subject, property);
2612 mono_image_unlock (image);
2618 * mono_image_property_insert:
2620 * Insert a new property @property with value @value on @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
2622 * LOCKING: Takes the image lock
2625 mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value)
2627 CHECKED_METADATA_STORE_LOCAL (image->mempool, value);
2628 mono_image_lock (image);
2629 mono_property_hash_insert (image->property_hash, subject, property, value);
2630 mono_image_unlock (image);
2634 * mono_image_property_remove:
2636 * Remove all properties associated with @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
2638 * LOCKING: Takes the image lock
2641 mono_image_property_remove (MonoImage *image, gpointer subject)
2643 mono_image_lock (image);
2644 mono_property_hash_remove_object (image->property_hash, subject);
2645 mono_image_unlock (image);
2649 mono_image_append_class_to_reflection_info_set (MonoClass *klass)
2651 MonoImage *image = klass->image;
2652 g_assert (image_is_dynamic (image));
2653 mono_image_lock (image);
2654 image->reflection_info_unregister_classes = g_slist_prepend_mempool (image->mempool, image->reflection_info_unregister_classes, klass);
2655 mono_image_unlock (image);
2658 // This is support for the mempool reference tracking feature in checked-build, but lives in image.c due to use of static variables of this file.
2661 * mono_find_image_owner:
2663 * Find the image, if any, which a given pointer is located in the memory of.
2666 mono_find_image_owner (void *ptr)
2668 mono_images_lock ();
2670 MonoImage *owner = NULL;
2672 // Iterate over both by-path image hashes
2673 const int hash_candidates[] = {IMAGES_HASH_PATH, IMAGES_HASH_PATH_REFONLY};
2675 for (hash_idx = 0; !owner && hash_idx < G_N_ELEMENTS (hash_candidates); hash_idx++)
2677 GHashTable *target = loaded_images_hashes [hash_candidates [hash_idx]];
2678 GHashTableIter iter;
2681 // Iterate over images within a hash
2682 g_hash_table_iter_init (&iter, target);
2683 while (!owner && g_hash_table_iter_next(&iter, NULL, (gpointer *)&image))
2685 mono_image_lock (image);
2686 if (mono_mempool_contains_addr (image->mempool, ptr))
2688 mono_image_unlock (image);
2692 mono_images_unlock ();