2009-11-23 Andreas Faerber <andreas.faerber@web.de>
[mono.git] / mono / metadata / image.c
blob1302af85dbe63d98e0d11a3f38ab7d8648532ce9
1 /*
2 * image.c: Routines for manipulating an image stored in an
3 * extended PE/COFF file.
4 *
5 * Authors:
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)
13 #include <config.h>
14 #include <stdio.h>
15 #include <glib.h>
16 #include <errno.h>
17 #include <time.h>
18 #include <string.h>
19 #include "image.h"
20 #include "cil-coff.h"
21 #include "mono-endian.h"
22 #include "tabledefs.h"
23 #include "tokentype.h"
24 #include "metadata-internals.h"
25 #include "profiler-private.h"
26 #include "loader.h"
27 #include "marshal.h"
28 #include "coree.h"
29 #include <mono/io-layer/io-layer.h>
30 #include <mono/utils/mono-logger.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/metadata/class-internals.h>
35 #include <mono/metadata/assembly.h>
36 #include <mono/metadata/object-internals.h>
37 #include <mono/metadata/security-core-clr.h>
38 #include <mono/metadata/verify-internals.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
45 #define INVALID_ADDRESS 0xffffffff
48 * Keeps track of the various assemblies loaded
50 static GHashTable *loaded_images_hash;
51 static GHashTable *loaded_images_refonly_hash;
53 static gboolean debug_assembly_unload = FALSE;
55 #define mono_images_lock() if (mutex_inited) EnterCriticalSection (&images_mutex)
56 #define mono_images_unlock() if (mutex_inited) LeaveCriticalSection (&images_mutex)
57 static gboolean mutex_inited;
58 static CRITICAL_SECTION images_mutex;
60 /* returns offset relative to image->raw_data */
61 guint32
62 mono_cli_rva_image_map (MonoImage *image, guint32 addr)
64 MonoCLIImageInfo *iinfo = image->image_info;
65 const int top = iinfo->cli_section_count;
66 MonoSectionTable *tables = iinfo->cli_section_tables;
67 int i;
69 for (i = 0; i < top; i++){
70 if ((addr >= tables->st_virtual_address) &&
71 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
72 #ifdef HOST_WIN32
73 if (image->is_module_handle)
74 return addr;
75 #endif
76 return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
78 tables++;
80 return INVALID_ADDRESS;
83 /**
84 * mono_images_rva_map:
85 * @image: a MonoImage
86 * @addr: relative virtual address (RVA)
88 * This is a low-level routine used by the runtime to map relative
89 * virtual address (RVA) into their location in memory.
91 * Returns: the address in memory for the given RVA, or NULL if the
92 * RVA is not valid for this image.
94 char *
95 mono_image_rva_map (MonoImage *image, guint32 addr)
97 MonoCLIImageInfo *iinfo = image->image_info;
98 const int top = iinfo->cli_section_count;
99 MonoSectionTable *tables = iinfo->cli_section_tables;
100 int i;
102 for (i = 0; i < top; i++){
103 if ((addr >= tables->st_virtual_address) &&
104 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
105 if (!iinfo->cli_sections [i]) {
106 if (!mono_image_ensure_section_idx (image, i))
107 return NULL;
109 #ifdef HOST_WIN32
110 if (image->is_module_handle)
111 return image->raw_data + addr;
112 #endif
113 return (char*)iinfo->cli_sections [i] +
114 (addr - tables->st_virtual_address);
116 tables++;
118 return NULL;
122 * mono_images_init:
124 * Initialize the global variables used by this module.
126 void
127 mono_images_init (void)
129 InitializeCriticalSection (&images_mutex);
131 loaded_images_hash = g_hash_table_new (g_str_hash, g_str_equal);
132 loaded_images_refonly_hash = g_hash_table_new (g_str_hash, g_str_equal);
134 debug_assembly_unload = g_getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
136 mutex_inited = TRUE;
140 * mono_images_cleanup:
142 * Free all resources used by this module.
144 void
145 mono_images_cleanup (void)
147 DeleteCriticalSection (&images_mutex);
149 g_hash_table_destroy (loaded_images_hash);
150 g_hash_table_destroy (loaded_images_refonly_hash);
152 mutex_inited = FALSE;
156 * mono_image_ensure_section_idx:
157 * @image: The image we are operating on
158 * @section: section number that we will load/map into memory
160 * This routine makes sure that we have an in-memory copy of
161 * an image section (.text, .rsrc, .data).
163 * Returns: TRUE on success
166 mono_image_ensure_section_idx (MonoImage *image, int section)
168 MonoCLIImageInfo *iinfo = image->image_info;
169 MonoSectionTable *sect;
170 gboolean writable;
172 g_return_val_if_fail (section < iinfo->cli_section_count, FALSE);
174 if (iinfo->cli_sections [section] != NULL)
175 return TRUE;
177 sect = &iinfo->cli_section_tables [section];
179 writable = sect->st_flags & SECT_FLAGS_MEM_WRITE;
181 if (sect->st_raw_data_ptr + sect->st_raw_data_size > image->raw_data_len)
182 return FALSE;
183 #ifdef HOST_WIN32
184 if (image->is_module_handle)
185 iinfo->cli_sections [section] = image->raw_data + sect->st_virtual_address;
186 else
187 #endif
188 /* FIXME: we ignore the writable flag since we don't patch the binary */
189 iinfo->cli_sections [section] = image->raw_data + sect->st_raw_data_ptr;
190 return TRUE;
194 * mono_image_ensure_section:
195 * @image: The image we are operating on
196 * @section: section name that we will load/map into memory
198 * This routine makes sure that we have an in-memory copy of
199 * an image section (.text, .rsrc, .data).
201 * Returns: TRUE on success
204 mono_image_ensure_section (MonoImage *image, const char *section)
206 MonoCLIImageInfo *ii = image->image_info;
207 int i;
209 for (i = 0; i < ii->cli_section_count; i++){
210 if (strncmp (ii->cli_section_tables [i].st_name, section, 8) != 0)
211 continue;
213 return mono_image_ensure_section_idx (image, i);
215 return FALSE;
218 static int
219 load_section_tables (MonoImage *image, MonoCLIImageInfo *iinfo, guint32 offset)
221 const int top = iinfo->cli_header.coff.coff_sections;
222 int i;
224 iinfo->cli_section_count = top;
225 iinfo->cli_section_tables = g_new0 (MonoSectionTable, top);
226 iinfo->cli_sections = g_new0 (void *, top);
228 for (i = 0; i < top; i++){
229 MonoSectionTable *t = &iinfo->cli_section_tables [i];
231 if (offset + sizeof (MonoSectionTable) > image->raw_data_len)
232 return FALSE;
233 memcpy (t, image->raw_data + offset, sizeof (MonoSectionTable));
234 offset += sizeof (MonoSectionTable);
236 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
237 t->st_virtual_size = GUINT32_FROM_LE (t->st_virtual_size);
238 t->st_virtual_address = GUINT32_FROM_LE (t->st_virtual_address);
239 t->st_raw_data_size = GUINT32_FROM_LE (t->st_raw_data_size);
240 t->st_raw_data_ptr = GUINT32_FROM_LE (t->st_raw_data_ptr);
241 t->st_reloc_ptr = GUINT32_FROM_LE (t->st_reloc_ptr);
242 t->st_lineno_ptr = GUINT32_FROM_LE (t->st_lineno_ptr);
243 t->st_reloc_count = GUINT16_FROM_LE (t->st_reloc_count);
244 t->st_line_count = GUINT16_FROM_LE (t->st_line_count);
245 t->st_flags = GUINT32_FROM_LE (t->st_flags);
246 #endif
247 /* consistency checks here */
250 return TRUE;
253 static gboolean
254 load_cli_header (MonoImage *image, MonoCLIImageInfo *iinfo)
256 guint32 offset;
258 offset = mono_cli_rva_image_map (image, iinfo->cli_header.datadir.pe_cli_header.rva);
259 if (offset == INVALID_ADDRESS)
260 return FALSE;
262 if (offset + sizeof (MonoCLIHeader) > image->raw_data_len)
263 return FALSE;
264 memcpy (&iinfo->cli_cli_header, image->raw_data + offset, sizeof (MonoCLIHeader));
266 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
267 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
268 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
269 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
270 SWAP32 (iinfo->cli_cli_header.ch_size);
271 SWAP32 (iinfo->cli_cli_header.ch_flags);
272 SWAP32 (iinfo->cli_cli_header.ch_entry_point);
273 SWAP16 (iinfo->cli_cli_header.ch_runtime_major);
274 SWAP16 (iinfo->cli_cli_header.ch_runtime_minor);
275 SWAPPDE (iinfo->cli_cli_header.ch_metadata);
276 SWAPPDE (iinfo->cli_cli_header.ch_resources);
277 SWAPPDE (iinfo->cli_cli_header.ch_strong_name);
278 SWAPPDE (iinfo->cli_cli_header.ch_code_manager_table);
279 SWAPPDE (iinfo->cli_cli_header.ch_vtable_fixups);
280 SWAPPDE (iinfo->cli_cli_header.ch_export_address_table_jumps);
281 SWAPPDE (iinfo->cli_cli_header.ch_eeinfo_table);
282 SWAPPDE (iinfo->cli_cli_header.ch_helper_table);
283 SWAPPDE (iinfo->cli_cli_header.ch_dynamic_info);
284 SWAPPDE (iinfo->cli_cli_header.ch_delay_load_info);
285 SWAPPDE (iinfo->cli_cli_header.ch_module_image);
286 SWAPPDE (iinfo->cli_cli_header.ch_external_fixups);
287 SWAPPDE (iinfo->cli_cli_header.ch_ridmap);
288 SWAPPDE (iinfo->cli_cli_header.ch_debug_map);
289 SWAPPDE (iinfo->cli_cli_header.ch_ip_map);
290 #undef SWAP32
291 #undef SWAP16
292 #undef SWAPPDE
293 #endif
294 /* Catch new uses of the fields that are supposed to be zero */
296 if ((iinfo->cli_cli_header.ch_eeinfo_table.rva != 0) ||
297 (iinfo->cli_cli_header.ch_helper_table.rva != 0) ||
298 (iinfo->cli_cli_header.ch_dynamic_info.rva != 0) ||
299 (iinfo->cli_cli_header.ch_delay_load_info.rva != 0) ||
300 (iinfo->cli_cli_header.ch_module_image.rva != 0) ||
301 (iinfo->cli_cli_header.ch_external_fixups.rva != 0) ||
302 (iinfo->cli_cli_header.ch_ridmap.rva != 0) ||
303 (iinfo->cli_cli_header.ch_debug_map.rva != 0) ||
304 (iinfo->cli_cli_header.ch_ip_map.rva != 0)){
307 * No need to scare people who are testing this, I am just
308 * labelling this as a LAMESPEC
310 /* g_warning ("Some fields in the CLI header which should have been zero are not zero"); */
314 return TRUE;
317 static gboolean
318 load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
320 guint32 offset, size;
321 guint16 streams;
322 int i;
323 guint32 pad;
324 char *ptr;
326 offset = mono_cli_rva_image_map (image, iinfo->cli_cli_header.ch_metadata.rva);
327 if (offset == INVALID_ADDRESS)
328 return FALSE;
330 size = iinfo->cli_cli_header.ch_metadata.size;
332 if (offset + size > image->raw_data_len)
333 return FALSE;
334 image->raw_metadata = image->raw_data + offset;
336 /* 24.2.1: Metadata root starts here */
337 ptr = image->raw_metadata;
339 if (strncmp (ptr, "BSJB", 4) == 0){
340 guint32 version_string_len;
342 ptr += 4;
343 image->md_version_major = read16 (ptr);
344 ptr += 2;
345 image->md_version_minor = read16 (ptr);
346 ptr += 6;
348 version_string_len = read32 (ptr);
349 ptr += 4;
350 image->version = g_strndup (ptr, version_string_len);
351 ptr += version_string_len;
352 pad = ptr - image->raw_metadata;
353 if (pad % 4)
354 ptr += 4 - (pad % 4);
355 } else
356 return FALSE;
358 /* skip over flags */
359 ptr += 2;
361 streams = read16 (ptr);
362 ptr += 2;
364 for (i = 0; i < streams; i++){
365 if (strncmp (ptr + 8, "#~", 3) == 0){
366 image->heap_tables.data = image->raw_metadata + read32 (ptr);
367 image->heap_tables.size = read32 (ptr + 4);
368 ptr += 8 + 3;
369 } else if (strncmp (ptr + 8, "#Strings", 9) == 0){
370 image->heap_strings.data = image->raw_metadata + read32 (ptr);
371 image->heap_strings.size = read32 (ptr + 4);
372 ptr += 8 + 9;
373 } else if (strncmp (ptr + 8, "#US", 4) == 0){
374 image->heap_us.data = image->raw_metadata + read32 (ptr);
375 image->heap_us.size = read32 (ptr + 4);
376 ptr += 8 + 4;
377 } else if (strncmp (ptr + 8, "#Blob", 6) == 0){
378 image->heap_blob.data = image->raw_metadata + read32 (ptr);
379 image->heap_blob.size = read32 (ptr + 4);
380 ptr += 8 + 6;
381 } else if (strncmp (ptr + 8, "#GUID", 6) == 0){
382 image->heap_guid.data = image->raw_metadata + read32 (ptr);
383 image->heap_guid.size = read32 (ptr + 4);
384 ptr += 8 + 6;
385 } else if (strncmp (ptr + 8, "#-", 3) == 0) {
386 image->heap_tables.data = image->raw_metadata + read32 (ptr);
387 image->heap_tables.size = read32 (ptr + 4);
388 ptr += 8 + 3;
389 image->uncompressed_metadata = TRUE;
390 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);
391 } else {
392 g_message ("Unknown heap type: %s\n", ptr + 8);
393 ptr += 8 + strlen (ptr + 8) + 1;
395 pad = ptr - image->raw_metadata;
396 if (pad % 4)
397 ptr += 4 - (pad % 4);
400 g_assert (image->heap_guid.data);
401 g_assert (image->heap_guid.size >= 16);
403 image->guid = mono_guid_to_string ((guint8*)image->heap_guid.data);
405 return TRUE;
409 * Load representation of logical metadata tables, from the "#~" stream
411 static gboolean
412 load_tables (MonoImage *image)
414 const char *heap_tables = image->heap_tables.data;
415 const guint32 *rows;
416 guint64 valid_mask, sorted_mask;
417 int valid = 0, table;
418 int heap_sizes;
420 heap_sizes = heap_tables [6];
421 image->idx_string_wide = ((heap_sizes & 0x01) == 1);
422 image->idx_guid_wide = ((heap_sizes & 0x02) == 2);
423 image->idx_blob_wide = ((heap_sizes & 0x04) == 4);
425 valid_mask = read64 (heap_tables + 8);
426 sorted_mask = read64 (heap_tables + 16);
427 rows = (const guint32 *) (heap_tables + 24);
429 for (table = 0; table < 64; table++){
430 if ((valid_mask & ((guint64) 1 << table)) == 0){
431 if (table > MONO_TABLE_LAST)
432 continue;
433 image->tables [table].rows = 0;
434 continue;
436 if (table > MONO_TABLE_LAST) {
437 g_warning("bits in valid must be zero above 0x2d (II - 23.1.6)");
438 } else {
439 image->tables [table].rows = read32 (rows);
441 /*if ((sorted_mask & ((guint64) 1 << table)) == 0){
442 g_print ("table %s (0x%02x) is sorted\n", mono_meta_table_name (table), table);
444 rows++;
445 valid++;
448 image->tables_base = (heap_tables + 24) + (4 * valid);
450 /* They must be the same */
451 g_assert ((const void *) image->tables_base == (const void *) rows);
453 mono_metadata_compute_table_bases (image);
454 return TRUE;
457 static gboolean
458 load_metadata (MonoImage *image, MonoCLIImageInfo *iinfo)
460 if (!load_metadata_ptrs (image, iinfo))
461 return FALSE;
463 return load_tables (image);
466 void
467 mono_image_check_for_module_cctor (MonoImage *image)
469 MonoTableInfo *t, *mt;
470 t = &image->tables [MONO_TABLE_TYPEDEF];
471 mt = &image->tables [MONO_TABLE_METHOD];
472 if (mono_framework_version () == 1) {
473 image->checked_module_cctor = TRUE;
474 return;
476 if (image->dynamic) {
477 /* FIXME: */
478 image->checked_module_cctor = TRUE;
479 return;
481 if (t->rows >= 1) {
482 guint32 nameidx = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_NAME);
483 const char *name = mono_metadata_string_heap (image, nameidx);
484 if (strcmp (name, "<Module>") == 0) {
485 guint32 first_method = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_METHOD_LIST) - 1;
486 guint32 last_method;
487 if (t->rows > 1)
488 last_method = mono_metadata_decode_row_col (t, 1, MONO_TYPEDEF_METHOD_LIST) - 1;
489 else
490 last_method = mt->rows;
491 for (; first_method < last_method; first_method++) {
492 nameidx = mono_metadata_decode_row_col (mt, first_method, MONO_METHOD_NAME);
493 name = mono_metadata_string_heap (image, nameidx);
494 if (strcmp (name, ".cctor") == 0) {
495 image->has_module_cctor = TRUE;
496 image->checked_module_cctor = TRUE;
497 return;
502 image->has_module_cctor = FALSE;
503 image->checked_module_cctor = TRUE;
506 static void
507 load_modules (MonoImage *image)
509 MonoTableInfo *t;
511 if (image->modules)
512 return;
514 t = &image->tables [MONO_TABLE_MODULEREF];
515 image->modules = g_new0 (MonoImage *, t->rows);
516 image->modules_loaded = g_new0 (gboolean, t->rows);
517 image->module_count = t->rows;
521 * mono_image_load_module:
523 * Load the module with the one-based index IDX from IMAGE and return it. Return NULL if
524 * it cannot be loaded.
526 MonoImage*
527 mono_image_load_module (MonoImage *image, int idx)
529 MonoTableInfo *t;
530 MonoTableInfo *file_table;
531 int i;
532 char *base_dir;
533 gboolean refonly = image->ref_only;
534 GList *list_iter, *valid_modules = NULL;
535 MonoImageOpenStatus status;
537 g_assert (idx <= image->module_count);
538 if (image->modules_loaded [idx - 1])
539 return image->modules [idx - 1];
541 file_table = &image->tables [MONO_TABLE_FILE];
542 for (i = 0; i < file_table->rows; i++) {
543 guint32 cols [MONO_FILE_SIZE];
544 mono_metadata_decode_row (file_table, i, cols, MONO_FILE_SIZE);
545 if (cols [MONO_FILE_FLAGS] == FILE_CONTAINS_NO_METADATA)
546 continue;
547 valid_modules = g_list_prepend (valid_modules, (char*)mono_metadata_string_heap (image, cols [MONO_FILE_NAME]));
550 t = &image->tables [MONO_TABLE_MODULEREF];
551 base_dir = g_path_get_dirname (image->name);
554 char *module_ref;
555 const char *name;
556 guint32 cols [MONO_MODULEREF_SIZE];
557 /* if there is no file table, we try to load the module... */
558 int valid = file_table->rows == 0;
560 mono_metadata_decode_row (t, idx - 1, cols, MONO_MODULEREF_SIZE);
561 name = mono_metadata_string_heap (image, cols [MONO_MODULEREF_NAME]);
562 for (list_iter = valid_modules; list_iter; list_iter = list_iter->next) {
563 /* be safe with string dups, but we could just compare string indexes */
564 if (strcmp (list_iter->data, name) == 0) {
565 valid = TRUE;
566 break;
569 if (valid) {
570 module_ref = g_build_filename (base_dir, name, NULL);
571 image->modules [idx - 1] = mono_image_open_full (module_ref, &status, refonly);
572 if (image->modules [idx - 1]) {
573 mono_image_addref (image->modules [idx - 1]);
574 image->modules [idx - 1]->assembly = image->assembly;
575 #ifdef HOST_WIN32
576 if (image->modules [idx - 1]->is_module_handle)
577 mono_image_fixup_vtable (image->modules [idx - 1]);
578 #endif
579 /* g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly); */
581 g_free (module_ref);
585 image->modules_loaded [idx - 1] = TRUE;
587 g_free (base_dir);
588 g_list_free (valid_modules);
590 return image->modules [idx - 1];
593 static gpointer
594 class_key_extract (gpointer value)
596 MonoClass *class = value;
598 return GUINT_TO_POINTER (class->type_token);
601 static gpointer*
602 class_next_value (gpointer value)
604 MonoClass *class = value;
606 return (gpointer*)&class->next_class_cache;
609 void
610 mono_image_init (MonoImage *image)
612 image->mempool = mono_mempool_new_size (512);
613 mono_internal_hash_table_init (&image->class_cache,
614 g_direct_hash,
615 class_key_extract,
616 class_next_value);
617 image->field_cache = g_hash_table_new (NULL, NULL);
619 image->typespec_cache = g_hash_table_new (NULL, NULL);
620 image->memberref_signatures = g_hash_table_new (NULL, NULL);
621 image->helper_signatures = g_hash_table_new (g_str_hash, g_str_equal);
622 image->method_signatures = g_hash_table_new (NULL, NULL);
624 image->property_hash = mono_property_hash_new ();
625 InitializeCriticalSection (&image->lock);
626 InitializeCriticalSection (&image->szarray_cache_lock);
629 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
630 #define SWAP64(x) (x) = GUINT64_FROM_LE ((x))
631 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
632 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
633 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
634 #else
635 #define SWAP64(x)
636 #define SWAP32(x)
637 #define SWAP16(x)
638 #define SWAPPDE(x)
639 #endif
642 * Returns < 0 to indicate an error.
644 static int
645 do_load_header (MonoImage *image, MonoDotNetHeader *header, int offset)
647 MonoDotNetHeader64 header64;
649 #ifdef HOST_WIN32
650 if (!image->is_module_handle)
651 #endif
652 if (offset + sizeof (MonoDotNetHeader32) > image->raw_data_len)
653 return -1;
655 memcpy (header, image->raw_data + offset, sizeof (MonoDotNetHeader));
657 if (header->pesig [0] != 'P' || header->pesig [1] != 'E')
658 return -1;
660 /* endian swap the fields common between PE and PE+ */
661 SWAP32 (header->coff.coff_time);
662 SWAP32 (header->coff.coff_symptr);
663 SWAP32 (header->coff.coff_symcount);
664 SWAP16 (header->coff.coff_machine);
665 SWAP16 (header->coff.coff_sections);
666 SWAP16 (header->coff.coff_opt_header_size);
667 SWAP16 (header->coff.coff_attributes);
668 /* MonoPEHeader */
669 SWAP32 (header->pe.pe_code_size);
670 SWAP32 (header->pe.pe_uninit_data_size);
671 SWAP32 (header->pe.pe_rva_entry_point);
672 SWAP32 (header->pe.pe_rva_code_base);
673 SWAP32 (header->pe.pe_rva_data_base);
674 SWAP16 (header->pe.pe_magic);
676 /* now we are ready for the basic tests */
678 if (header->pe.pe_magic == 0x10B) {
679 offset += sizeof (MonoDotNetHeader);
680 SWAP32 (header->pe.pe_data_size);
681 if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader) - sizeof (MonoCOFFHeader) - 4))
682 return -1;
684 SWAP32 (header->nt.pe_image_base); /* must be 0x400000 */
685 SWAP32 (header->nt.pe_stack_reserve);
686 SWAP32 (header->nt.pe_stack_commit);
687 SWAP32 (header->nt.pe_heap_reserve);
688 SWAP32 (header->nt.pe_heap_commit);
689 } else if (header->pe.pe_magic == 0x20B) {
690 /* PE32+ file format */
691 if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader64) - sizeof (MonoCOFFHeader) - 4))
692 return -1;
693 memcpy (&header64, image->raw_data + offset, sizeof (MonoDotNetHeader64));
694 offset += sizeof (MonoDotNetHeader64);
695 /* copy the fields already swapped. the last field, pe_data_size, is missing */
696 memcpy (&header64, header, sizeof (MonoDotNetHeader) - 4);
697 /* FIXME: we lose bits here, but we don't use this stuff internally, so we don't care much.
698 * will be fixed when we change MonoDotNetHeader to not match the 32 bit variant
700 SWAP64 (header64.nt.pe_image_base);
701 header->nt.pe_image_base = header64.nt.pe_image_base;
702 SWAP64 (header64.nt.pe_stack_reserve);
703 header->nt.pe_stack_reserve = header64.nt.pe_stack_reserve;
704 SWAP64 (header64.nt.pe_stack_commit);
705 header->nt.pe_stack_commit = header64.nt.pe_stack_commit;
706 SWAP64 (header64.nt.pe_heap_reserve);
707 header->nt.pe_heap_reserve = header64.nt.pe_heap_reserve;
708 SWAP64 (header64.nt.pe_heap_commit);
709 header->nt.pe_heap_commit = header64.nt.pe_heap_commit;
711 header->nt.pe_section_align = header64.nt.pe_section_align;
712 header->nt.pe_file_alignment = header64.nt.pe_file_alignment;
713 header->nt.pe_os_major = header64.nt.pe_os_major;
714 header->nt.pe_os_minor = header64.nt.pe_os_minor;
715 header->nt.pe_user_major = header64.nt.pe_user_major;
716 header->nt.pe_user_minor = header64.nt.pe_user_minor;
717 header->nt.pe_subsys_major = header64.nt.pe_subsys_major;
718 header->nt.pe_subsys_minor = header64.nt.pe_subsys_minor;
719 header->nt.pe_reserved_1 = header64.nt.pe_reserved_1;
720 header->nt.pe_image_size = header64.nt.pe_image_size;
721 header->nt.pe_header_size = header64.nt.pe_header_size;
722 header->nt.pe_checksum = header64.nt.pe_checksum;
723 header->nt.pe_subsys_required = header64.nt.pe_subsys_required;
724 header->nt.pe_dll_flags = header64.nt.pe_dll_flags;
725 header->nt.pe_loader_flags = header64.nt.pe_loader_flags;
726 header->nt.pe_data_dir_count = header64.nt.pe_data_dir_count;
728 /* copy the datadir */
729 memcpy (&header->datadir, &header64.datadir, sizeof (MonoPEDatadir));
730 } else {
731 return -1;
734 /* MonoPEHeaderNT: not used yet */
735 SWAP32 (header->nt.pe_section_align); /* must be 8192 */
736 SWAP32 (header->nt.pe_file_alignment); /* must be 512 or 4096 */
737 SWAP16 (header->nt.pe_os_major); /* must be 4 */
738 SWAP16 (header->nt.pe_os_minor); /* must be 0 */
739 SWAP16 (header->nt.pe_user_major);
740 SWAP16 (header->nt.pe_user_minor);
741 SWAP16 (header->nt.pe_subsys_major);
742 SWAP16 (header->nt.pe_subsys_minor);
743 SWAP32 (header->nt.pe_reserved_1);
744 SWAP32 (header->nt.pe_image_size);
745 SWAP32 (header->nt.pe_header_size);
746 SWAP32 (header->nt.pe_checksum);
747 SWAP16 (header->nt.pe_subsys_required);
748 SWAP16 (header->nt.pe_dll_flags);
749 SWAP32 (header->nt.pe_loader_flags);
750 SWAP32 (header->nt.pe_data_dir_count);
752 /* MonoDotNetHeader: mostly unused */
753 SWAPPDE (header->datadir.pe_export_table);
754 SWAPPDE (header->datadir.pe_import_table);
755 SWAPPDE (header->datadir.pe_resource_table);
756 SWAPPDE (header->datadir.pe_exception_table);
757 SWAPPDE (header->datadir.pe_certificate_table);
758 SWAPPDE (header->datadir.pe_reloc_table);
759 SWAPPDE (header->datadir.pe_debug);
760 SWAPPDE (header->datadir.pe_copyright);
761 SWAPPDE (header->datadir.pe_global_ptr);
762 SWAPPDE (header->datadir.pe_tls_table);
763 SWAPPDE (header->datadir.pe_load_config_table);
764 SWAPPDE (header->datadir.pe_bound_import);
765 SWAPPDE (header->datadir.pe_iat);
766 SWAPPDE (header->datadir.pe_delay_import_desc);
767 SWAPPDE (header->datadir.pe_cli_header);
768 SWAPPDE (header->datadir.pe_reserved);
770 #ifdef HOST_WIN32
771 if (image->is_module_handle)
772 image->raw_data_len = header->nt.pe_image_size;
773 #endif
775 return offset;
778 gboolean
779 mono_image_load_pe_data (MonoImage *image)
781 MonoCLIImageInfo *iinfo;
782 MonoDotNetHeader *header;
783 MonoMSDOSHeader msdos;
784 gint32 offset = 0;
786 iinfo = image->image_info;
787 header = &iinfo->cli_header;
789 #ifdef HOST_WIN32
790 if (!image->is_module_handle)
791 #endif
792 if (offset + sizeof (msdos) > image->raw_data_len)
793 goto invalid_image;
794 memcpy (&msdos, image->raw_data + offset, sizeof (msdos));
796 if (!(msdos.msdos_sig [0] == 'M' && msdos.msdos_sig [1] == 'Z'))
797 goto invalid_image;
799 msdos.pe_offset = GUINT32_FROM_LE (msdos.pe_offset);
801 offset = msdos.pe_offset;
803 offset = do_load_header (image, header, offset);
804 if (offset < 0)
805 goto invalid_image;
808 * this tests for a x86 machine type, but itanium, amd64 and others could be used, too.
809 * we skip this test.
810 if (header->coff.coff_machine != 0x14c)
811 goto invalid_image;
814 #if 0
816 * The spec says that this field should contain 6.0, but Visual Studio includes a new compiler,
817 * which produces binaries with 7.0. From Sergey:
819 * The reason is that MSVC7 uses traditional compile/link
820 * sequence for CIL executables, and VS.NET (and Framework
821 * SDK) includes linker version 7, that puts 7.0 in this
822 * field. That's why it's currently not possible to load VC
823 * binaries with Mono. This field is pretty much meaningless
824 * anyway (what linker?).
826 if (header->pe.pe_major != 6 || header->pe.pe_minor != 0)
827 goto invalid_image;
828 #endif
831 * FIXME: byte swap all addresses here for header.
834 if (!load_section_tables (image, iinfo, offset))
835 goto invalid_image;
837 return TRUE;
839 invalid_image:
840 return FALSE;
843 gboolean
844 mono_image_load_cli_data (MonoImage *image)
846 MonoCLIImageInfo *iinfo;
847 MonoDotNetHeader *header;
849 iinfo = image->image_info;
850 header = &iinfo->cli_header;
852 /* Load the CLI header */
853 if (!load_cli_header (image, iinfo))
854 return FALSE;
856 if (!load_metadata (image, iinfo))
857 return FALSE;
859 return TRUE;
862 void
863 mono_image_load_names (MonoImage *image)
865 /* modules don't have an assembly table row */
866 if (image->tables [MONO_TABLE_ASSEMBLY].rows) {
867 image->assembly_name = mono_metadata_string_heap (image,
868 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY],
869 0, MONO_ASSEMBLY_NAME));
872 image->module_name = mono_metadata_string_heap (image,
873 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_MODULE],
874 0, MONO_MODULE_NAME));
877 static MonoImage *
878 do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
879 gboolean care_about_cli, gboolean care_about_pecoff)
881 MonoCLIImageInfo *iinfo;
882 MonoDotNetHeader *header;
884 mono_profiler_module_event (image, MONO_PROFILE_START_LOAD);
886 mono_image_init (image);
888 iinfo = image->image_info;
889 header = &iinfo->cli_header;
891 if (status)
892 *status = MONO_IMAGE_IMAGE_INVALID;
894 if (care_about_pecoff == FALSE)
895 goto done;
897 if (!mono_verifier_verify_pe_data (image, NULL))
898 goto invalid_image;
900 if (!mono_image_load_pe_data (image))
901 goto invalid_image;
903 if (care_about_cli == FALSE) {
904 goto done;
907 if (!mono_verifier_verify_cli_data (image, NULL))
908 goto invalid_image;
910 if (!mono_image_load_cli_data (image))
911 goto invalid_image;
913 if (!mono_verifier_verify_table_data (image, NULL))
914 goto invalid_image;
916 mono_image_load_names (image);
918 load_modules (image);
920 done:
921 mono_profiler_module_loaded (image, MONO_PROFILE_OK);
922 if (status)
923 *status = MONO_IMAGE_OK;
925 return image;
927 invalid_image:
928 mono_profiler_module_loaded (image, MONO_PROFILE_FAILED);
929 mono_image_close (image);
930 return NULL;
933 static MonoImage *
934 do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
935 gboolean care_about_cli, gboolean care_about_pecoff, gboolean refonly)
937 MonoCLIImageInfo *iinfo;
938 MonoImage *image;
939 MonoFileMap *filed;
941 if ((filed = mono_file_map_open (fname)) == NULL){
942 if (IS_PORTABILITY_SET) {
943 gchar *ffname = mono_portability_find_file (fname, TRUE);
944 if (ffname) {
945 filed = mono_file_map_open (ffname);
946 g_free (ffname);
950 if (filed == NULL) {
951 if (status)
952 *status = MONO_IMAGE_ERROR_ERRNO;
953 return NULL;
957 image = g_new0 (MonoImage, 1);
958 image->raw_buffer_used = TRUE;
959 image->raw_data_len = mono_file_map_size (filed);
960 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);
961 if (!image->raw_data) {
962 mono_file_map_close (filed);
963 g_free (image);
964 if (status)
965 *status = MONO_IMAGE_IMAGE_INVALID;
966 return NULL;
968 iinfo = g_new0 (MonoCLIImageInfo, 1);
969 image->image_info = iinfo;
970 image->name = mono_path_resolve_symlinks (fname);
971 image->ref_only = refonly;
972 image->ref_count = 1;
973 /* if MONO_SECURITY_MODE_CORE_CLR is set then determine if this image is platform code */
974 image->core_clr_platform_code = mono_security_core_clr_determine_platform_image (image);
976 mono_file_map_close (filed);
977 return do_mono_image_load (image, status, care_about_cli, care_about_pecoff);
980 MonoImage *
981 mono_image_loaded_full (const char *name, gboolean refonly)
983 MonoImage *res;
984 GHashTable *loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
986 mono_images_lock ();
987 res = g_hash_table_lookup (loaded_images, name);
988 mono_images_unlock ();
989 return res;
993 * mono_image_loaded:
994 * @name: name of the image to load
996 * This routine ensures that the given image is loaded.
998 * Returns: the loaded MonoImage, or NULL on failure.
1000 MonoImage *
1001 mono_image_loaded (const char *name)
1003 return mono_image_loaded_full (name, FALSE);
1006 typedef struct {
1007 MonoImage *res;
1008 const char* guid;
1009 } GuidData;
1011 static void
1012 find_by_guid (gpointer key, gpointer val, gpointer user_data)
1014 GuidData *data = user_data;
1015 MonoImage *image;
1017 if (data->res)
1018 return;
1019 image = val;
1020 if (strcmp (data->guid, mono_image_get_guid (image)) == 0)
1021 data->res = image;
1024 MonoImage *
1025 mono_image_loaded_by_guid_full (const char *guid, gboolean refonly)
1027 GuidData data;
1028 GHashTable *loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
1029 data.res = NULL;
1030 data.guid = guid;
1032 mono_images_lock ();
1033 g_hash_table_foreach (loaded_images, find_by_guid, &data);
1034 mono_images_unlock ();
1035 return data.res;
1038 MonoImage *
1039 mono_image_loaded_by_guid (const char *guid)
1041 return mono_image_loaded_by_guid_full (guid, FALSE);
1044 static MonoImage *
1045 register_image (MonoImage *image)
1047 MonoImage *image2;
1048 GHashTable *loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
1050 mono_images_lock ();
1051 image2 = g_hash_table_lookup (loaded_images, image->name);
1053 if (image2) {
1054 /* Somebody else beat us to it */
1055 mono_image_addref (image2);
1056 mono_images_unlock ();
1057 mono_image_close (image);
1058 return image2;
1060 g_hash_table_insert (loaded_images, image->name, image);
1061 if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == NULL))
1062 g_hash_table_insert (loaded_images, (char *) image->assembly_name, image);
1063 mono_images_unlock ();
1065 return image;
1068 MonoImage *
1069 mono_image_open_from_data_full (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status, gboolean refonly)
1071 MonoCLIImageInfo *iinfo;
1072 MonoImage *image;
1073 char *datac;
1075 if (!data || !data_len) {
1076 if (status)
1077 *status = MONO_IMAGE_IMAGE_INVALID;
1078 return NULL;
1080 datac = data;
1081 if (need_copy) {
1082 datac = g_try_malloc (data_len);
1083 if (!datac) {
1084 if (status)
1085 *status = MONO_IMAGE_ERROR_ERRNO;
1086 return NULL;
1088 memcpy (datac, data, data_len);
1091 image = g_new0 (MonoImage, 1);
1092 image->raw_data = datac;
1093 image->raw_data_len = data_len;
1094 image->raw_data_allocated = need_copy;
1095 image->name = g_strdup_printf ("data-%p", datac);
1096 iinfo = g_new0 (MonoCLIImageInfo, 1);
1097 image->image_info = iinfo;
1098 image->ref_only = refonly;
1100 image = do_mono_image_load (image, status, TRUE, TRUE);
1101 if (image == NULL)
1102 return NULL;
1104 return register_image (image);
1107 MonoImage *
1108 mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status)
1110 return mono_image_open_from_data_full (data, data_len, need_copy, status, FALSE);
1113 #ifdef HOST_WIN32
1114 /* fname is not duplicated. */
1115 MonoImage*
1116 mono_image_open_from_module_handle (HMODULE module_handle, char* fname, gboolean has_entry_point, MonoImageOpenStatus* status)
1118 MonoImage* image;
1119 MonoCLIImageInfo* iinfo;
1121 image = g_new0 (MonoImage, 1);
1122 image->raw_data = (char*) module_handle;
1123 image->is_module_handle = TRUE;
1124 iinfo = g_new0 (MonoCLIImageInfo, 1);
1125 image->image_info = iinfo;
1126 image->name = fname;
1127 image->ref_count = has_entry_point ? 0 : 1;
1128 image->has_entry_point = has_entry_point;
1130 image = do_mono_image_load (image, status, TRUE, TRUE);
1131 if (image == NULL)
1132 return NULL;
1134 return register_image (image);
1136 #endif
1138 MonoImage *
1139 mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean refonly)
1141 MonoImage *image;
1142 GHashTable *loaded_images;
1143 char *absfname;
1145 g_return_val_if_fail (fname != NULL, NULL);
1147 #ifdef HOST_WIN32
1148 /* Load modules using LoadLibrary. */
1149 if (!refonly && coree_module_handle) {
1150 HMODULE module_handle;
1151 guint16 *fname_utf16;
1152 DWORD last_error;
1154 absfname = mono_path_resolve_symlinks (fname);
1155 fname_utf16 = NULL;
1157 /* There is little overhead because the OS loader lock is held by LoadLibrary. */
1158 mono_images_lock ();
1159 image = g_hash_table_lookup (loaded_images_hash, absfname);
1160 if (image) {
1161 g_assert (image->is_module_handle);
1162 if (image->has_entry_point && image->ref_count == 0) {
1163 /* Increment reference count on images loaded outside of the runtime. */
1164 fname_utf16 = g_utf8_to_utf16 (absfname, -1, NULL, NULL, NULL);
1165 /* The image is already loaded because _CorDllMain removes images from the hash. */
1166 module_handle = LoadLibrary (fname_utf16);
1167 g_assert (module_handle == (HMODULE) image->raw_data);
1169 mono_image_addref (image);
1170 mono_images_unlock ();
1171 if (fname_utf16)
1172 g_free (fname_utf16);
1173 g_free (absfname);
1174 return image;
1177 fname_utf16 = g_utf8_to_utf16 (absfname, -1, NULL, NULL, NULL);
1178 module_handle = MonoLoadImage (fname_utf16);
1179 if (status && module_handle == NULL)
1180 last_error = GetLastError ();
1182 /* mono_image_open_from_module_handle is called by _CorDllMain. */
1183 image = g_hash_table_lookup (loaded_images_hash, absfname);
1184 if (image)
1185 mono_image_addref (image);
1186 mono_images_unlock ();
1188 g_free (fname_utf16);
1190 if (module_handle == NULL) {
1191 g_assert (!image);
1192 g_free (absfname);
1193 if (status) {
1194 if (last_error == ERROR_BAD_EXE_FORMAT || last_error == STATUS_INVALID_IMAGE_FORMAT)
1195 *status = MONO_IMAGE_IMAGE_INVALID;
1196 else
1197 *status = MONO_IMAGE_ERROR_ERRNO;
1199 return NULL;
1202 if (image) {
1203 g_assert (image->is_module_handle);
1204 g_assert (image->has_entry_point);
1205 g_free (absfname);
1206 return image;
1209 return mono_image_open_from_module_handle (module_handle, absfname, FALSE, status);
1211 #endif
1213 absfname = mono_path_canonicalize (fname);
1216 * The easiest solution would be to do all the loading inside the mutex,
1217 * but that would lead to scalability problems. So we let the loading
1218 * happen outside the mutex, and if multiple threads happen to load
1219 * the same image, we discard all but the first copy.
1221 mono_images_lock ();
1222 loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
1223 image = g_hash_table_lookup (loaded_images, absfname);
1224 g_free (absfname);
1226 if (image){
1227 mono_image_addref (image);
1228 mono_images_unlock ();
1229 return image;
1231 mono_images_unlock ();
1233 image = do_mono_image_open (fname, status, TRUE, TRUE, refonly);
1234 if (image == NULL)
1235 return NULL;
1237 return register_image (image);
1241 * mono_image_open:
1242 * @fname: filename that points to the module we want to open
1243 * @status: An error condition is returned in this field
1245 * Returns: An open image of type %MonoImage or NULL on error.
1246 * The caller holds a temporary reference to the returned image which should be cleared
1247 * when no longer needed by calling mono_image_close ().
1248 * if NULL, then check the value of @status for details on the error
1250 MonoImage *
1251 mono_image_open (const char *fname, MonoImageOpenStatus *status)
1253 return mono_image_open_full (fname, status, FALSE);
1257 * mono_pe_file_open:
1258 * @fname: filename that points to the module we want to open
1259 * @status: An error condition is returned in this field
1261 * Returns: An open image of type %MonoImage or NULL on error. if
1262 * NULL, then check the value of @status for details on the error.
1263 * This variant for mono_image_open DOES NOT SET UP CLI METADATA.
1264 * It's just a PE file loader, used for FileVersionInfo. It also does
1265 * not use the image cache.
1267 MonoImage *
1268 mono_pe_file_open (const char *fname, MonoImageOpenStatus *status)
1270 g_return_val_if_fail (fname != NULL, NULL);
1272 return(do_mono_image_open (fname, status, FALSE, TRUE, FALSE));
1276 * mono_image_open_raw
1277 * @fname: filename that points to the module we want to open
1278 * @status: An error condition is returned in this field
1280 * Returns an image without loading neither pe or cli data.
1282 * Use mono_image_load_pe_data and mono_image_load_cli_data to load them.
1284 MonoImage *
1285 mono_image_open_raw (const char *fname, MonoImageOpenStatus *status)
1287 g_return_val_if_fail (fname != NULL, NULL);
1289 return(do_mono_image_open (fname, status, FALSE, FALSE, FALSE));
1292 void
1293 mono_image_fixup_vtable (MonoImage *image)
1295 #ifdef HOST_WIN32
1296 MonoCLIImageInfo *iinfo;
1297 MonoPEDirEntry *de;
1298 MonoVTableFixup *vtfixup;
1299 int count;
1300 gpointer slot;
1301 guint16 slot_type;
1302 int slot_count;
1304 g_assert (image->is_module_handle);
1306 iinfo = image->image_info;
1307 de = &iinfo->cli_cli_header.ch_vtable_fixups;
1308 if (!de->rva || !de->size)
1309 return;
1310 vtfixup = (MonoVTableFixup*) mono_image_rva_map (image, de->rva);
1311 if (!vtfixup)
1312 return;
1314 count = de->size / sizeof (MonoVTableFixup);
1315 while (count--) {
1316 if (!vtfixup->rva || !vtfixup->count)
1317 continue;
1319 slot = mono_image_rva_map (image, vtfixup->rva);
1320 g_assert (slot);
1321 slot_type = vtfixup->type;
1322 slot_count = vtfixup->count;
1323 if (slot_type & VTFIXUP_TYPE_32BIT)
1324 while (slot_count--) {
1325 *((guint32*) slot) = (guint32) mono_marshal_get_vtfixup_ftnptr (image, *((guint32*) slot), slot_type);
1326 slot = ((guint32*) slot) + 1;
1328 else if (slot_type & VTFIXUP_TYPE_64BIT)
1329 while (slot_count--) {
1330 *((guint64*) slot) = (guint64) mono_marshal_get_vtfixup_ftnptr (image, *((guint64*) slot), slot_type);
1331 slot = ((guint32*) slot) + 1;
1333 else
1334 g_assert_not_reached();
1336 vtfixup++;
1338 #else
1339 g_assert_not_reached();
1340 #endif
1343 static void
1344 free_hash_table (gpointer key, gpointer val, gpointer user_data)
1346 g_hash_table_destroy ((GHashTable*)val);
1350 static void
1351 free_mr_signatures (gpointer key, gpointer val, gpointer user_data)
1353 mono_metadata_free_method_signature ((MonoMethodSignature*)val);
1357 static void
1358 free_remoting_wrappers (gpointer key, gpointer val, gpointer user_data)
1360 g_free (val);
1363 static void
1364 free_array_cache_entry (gpointer key, gpointer val, gpointer user_data)
1366 g_slist_free ((GSList*)val);
1370 * mono_image_addref:
1371 * @image: The image file we wish to add a reference to
1373 * Increases the reference count of an image.
1375 void
1376 mono_image_addref (MonoImage *image)
1378 InterlockedIncrement (&image->ref_count);
1381 void
1382 mono_dynamic_stream_reset (MonoDynamicStream* stream)
1384 stream->alloc_size = stream->index = stream->offset = 0;
1385 g_free (stream->data);
1386 stream->data = NULL;
1387 if (stream->hash) {
1388 g_hash_table_destroy (stream->hash);
1389 stream->hash = NULL;
1393 static inline void
1394 free_hash (GHashTable *hash)
1396 if (hash)
1397 g_hash_table_destroy (hash);
1401 * Returns whether mono_image_close_finish() must be called as well.
1402 * We must unload images in two steps because clearing the domain in
1403 * SGen requires the class metadata to be intact, but we need to free
1404 * the mono_g_hash_tables in case a collection occurs during domain
1405 * unloading and the roots would trip up the GC.
1407 gboolean
1408 mono_image_close_except_pools (MonoImage *image)
1410 MonoImage *image2;
1411 GHashTable *loaded_images;
1412 int i;
1414 g_return_val_if_fail (image != NULL, FALSE);
1417 * Atomically decrement the refcount and remove ourselves from the hash tables, so
1418 * register_image () can't grab an image which is being closed.
1420 mono_images_lock ();
1422 if (InterlockedDecrement (&image->ref_count) > 0) {
1423 mono_images_unlock ();
1424 return FALSE;
1427 loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
1428 image2 = g_hash_table_lookup (loaded_images, image->name);
1429 if (image == image2) {
1430 /* This is not true if we are called from mono_image_open () */
1431 g_hash_table_remove (loaded_images, image->name);
1433 if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == image))
1434 g_hash_table_remove (loaded_images, (char *) image->assembly_name);
1436 mono_images_unlock ();
1438 #ifdef HOST_WIN32
1439 if (image->is_module_handle && image->has_entry_point) {
1440 mono_images_lock ();
1441 if (image->ref_count == 0) {
1442 /* Image will be closed by _CorDllMain. */
1443 FreeLibrary ((HMODULE) image->raw_data);
1444 mono_images_unlock ();
1445 return FALSE;
1447 mono_images_unlock ();
1449 #endif
1451 mono_profiler_module_event (image, MONO_PROFILE_START_UNLOAD);
1453 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading image %s [%p].", image->name, image);
1455 mono_metadata_clean_for_image (image);
1458 * The caches inside a MonoImage might refer to metadata which is stored in referenced
1459 * assemblies, so we can't release these references in mono_assembly_close () since the
1460 * MonoImage might outlive its associated MonoAssembly.
1462 if (image->references && !image->dynamic) {
1463 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
1464 int i;
1466 for (i = 0; i < t->rows; i++) {
1467 if (image->references [i]) {
1468 if (!mono_assembly_close_except_image_pools (image->references [i]))
1469 image->references [i] = NULL;
1472 } else {
1473 if (image->references) {
1474 g_free (image->references);
1475 image->references = NULL;
1479 #ifdef HOST_WIN32
1480 mono_images_lock ();
1481 if (image->is_module_handle && !image->has_entry_point)
1482 FreeLibrary ((HMODULE) image->raw_data);
1483 mono_images_unlock ();
1484 #endif
1486 if (image->raw_buffer_used) {
1487 if (image->raw_data != NULL)
1488 mono_file_unmap (image->raw_data, image->raw_data_handle);
1491 if (image->raw_data_allocated) {
1492 /* FIXME: do we need this? (image is disposed anyway) */
1493 /* image->raw_metadata and cli_sections might lie inside image->raw_data */
1494 MonoCLIImageInfo *ii = image->image_info;
1496 if ((image->raw_metadata > image->raw_data) &&
1497 (image->raw_metadata <= (image->raw_data + image->raw_data_len)))
1498 image->raw_metadata = NULL;
1500 for (i = 0; i < ii->cli_section_count; i++)
1501 if (((char*)(ii->cli_sections [i]) > image->raw_data) &&
1502 ((char*)(ii->cli_sections [i]) <= ((char*)image->raw_data + image->raw_data_len)))
1503 ii->cli_sections [i] = NULL;
1505 g_free (image->raw_data);
1508 if (debug_assembly_unload) {
1509 image->name = g_strdup_printf ("%s - UNLOADED", image->name);
1510 } else {
1511 g_free (image->name);
1512 g_free (image->guid);
1513 g_free (image->version);
1514 g_free (image->files);
1517 if (image->method_cache)
1518 mono_value_hash_table_destroy (image->method_cache);
1519 if (image->methodref_cache)
1520 g_hash_table_destroy (image->methodref_cache);
1521 mono_internal_hash_table_destroy (&image->class_cache);
1522 g_hash_table_destroy (image->field_cache);
1523 if (image->array_cache) {
1524 g_hash_table_foreach (image->array_cache, free_array_cache_entry, NULL);
1525 g_hash_table_destroy (image->array_cache);
1527 if (image->szarray_cache)
1528 g_hash_table_destroy (image->szarray_cache);
1529 if (image->ptr_cache)
1530 g_hash_table_destroy (image->ptr_cache);
1531 if (image->name_cache) {
1532 g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
1533 g_hash_table_destroy (image->name_cache);
1536 free_hash (image->native_wrapper_cache);
1537 free_hash (image->managed_wrapper_cache);
1538 free_hash (image->delegate_begin_invoke_cache);
1539 free_hash (image->delegate_end_invoke_cache);
1540 free_hash (image->delegate_invoke_cache);
1541 free_hash (image->delegate_abstract_invoke_cache);
1542 if (image->remoting_invoke_cache)
1543 g_hash_table_foreach (image->remoting_invoke_cache, free_remoting_wrappers, NULL);
1544 free_hash (image->remoting_invoke_cache);
1545 free_hash (image->runtime_invoke_cache);
1546 free_hash (image->runtime_invoke_direct_cache);
1547 free_hash (image->runtime_invoke_vcall_cache);
1548 free_hash (image->synchronized_cache);
1549 free_hash (image->unbox_wrapper_cache);
1550 free_hash (image->cominterop_invoke_cache);
1551 free_hash (image->cominterop_wrapper_cache);
1552 free_hash (image->typespec_cache);
1553 free_hash (image->ldfld_wrapper_cache);
1554 free_hash (image->ldflda_wrapper_cache);
1555 free_hash (image->stfld_wrapper_cache);
1556 free_hash (image->isinst_cache);
1557 free_hash (image->castclass_cache);
1558 free_hash (image->proxy_isinst_cache);
1559 free_hash (image->thunk_invoke_cache);
1561 /* The ownership of signatures is not well defined */
1562 //g_hash_table_foreach (image->memberref_signatures, free_mr_signatures, NULL);
1563 g_hash_table_destroy (image->memberref_signatures);
1564 //g_hash_table_foreach (image->helper_signatures, free_mr_signatures, NULL);
1565 g_hash_table_destroy (image->helper_signatures);
1566 g_hash_table_destroy (image->method_signatures);
1568 if (image->generic_class_cache)
1569 g_hash_table_destroy (image->generic_class_cache);
1571 if (image->rgctx_template_hash)
1572 g_hash_table_destroy (image->rgctx_template_hash);
1574 if (image->property_hash)
1575 mono_property_hash_destroy (image->property_hash);
1577 g_slist_free (image->reflection_info_unregister_classes);
1579 if (image->interface_bitset) {
1580 mono_unload_interface_ids (image->interface_bitset);
1581 mono_bitset_free (image->interface_bitset);
1583 if (image->image_info){
1584 MonoCLIImageInfo *ii = image->image_info;
1586 if (ii->cli_section_tables)
1587 g_free (ii->cli_section_tables);
1588 if (ii->cli_sections)
1589 g_free (ii->cli_sections);
1590 g_free (image->image_info);
1593 for (i = 0; i < image->module_count; ++i) {
1594 if (image->modules [i]) {
1595 if (!mono_image_close_except_pools (image->modules [i]))
1596 image->modules [i] = NULL;
1599 if (image->modules_loaded)
1600 g_free (image->modules_loaded);
1602 DeleteCriticalSection (&image->szarray_cache_lock);
1603 DeleteCriticalSection (&image->lock);
1605 /*g_print ("destroy image %p (dynamic: %d)\n", image, image->dynamic);*/
1606 if (image->dynamic) {
1607 /* Dynamic images are GC_MALLOCed */
1608 g_free ((char*)image->module_name);
1609 mono_dynamic_image_free ((MonoDynamicImage*)image);
1612 mono_profiler_module_event (image, MONO_PROFILE_END_UNLOAD);
1614 return TRUE;
1617 void
1618 mono_image_close_finish (MonoImage *image)
1620 int i;
1622 if (image->references && !image->dynamic) {
1623 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
1624 int i;
1626 for (i = 0; i < t->rows; i++) {
1627 if (image->references [i])
1628 mono_assembly_close_finish (image->references [i]);
1631 g_free (image->references);
1632 image->references = NULL;
1635 for (i = 0; i < image->module_count; ++i) {
1636 if (image->modules [i])
1637 mono_image_close_finish (image->modules [i]);
1639 if (image->modules)
1640 g_free (image->modules);
1642 mono_perfcounters->loader_bytes -= mono_mempool_get_allocated (image->mempool);
1644 if (!image->dynamic) {
1645 if (debug_assembly_unload)
1646 mono_mempool_invalidate (image->mempool);
1647 else {
1648 mono_mempool_destroy (image->mempool);
1649 g_free (image);
1651 } else {
1652 if (debug_assembly_unload)
1653 mono_mempool_invalidate (image->mempool);
1654 else
1655 mono_mempool_destroy (image->mempool);
1660 * mono_image_close:
1661 * @image: The image file we wish to close
1663 * Closes an image file, deallocates all memory consumed and
1664 * unmaps all possible sections of the file
1666 void
1667 mono_image_close (MonoImage *image)
1669 if (mono_image_close_except_pools (image))
1670 mono_image_close_finish (image);
1673 /**
1674 * mono_image_strerror:
1675 * @status: an code indicating the result from a recent operation
1677 * Returns: a string describing the error
1679 const char *
1680 mono_image_strerror (MonoImageOpenStatus status)
1682 switch (status){
1683 case MONO_IMAGE_OK:
1684 return "success";
1685 case MONO_IMAGE_ERROR_ERRNO:
1686 return strerror (errno);
1687 case MONO_IMAGE_IMAGE_INVALID:
1688 return "File does not contain a valid CIL image";
1689 case MONO_IMAGE_MISSING_ASSEMBLYREF:
1690 return "An assembly was referenced, but could not be found";
1692 return "Internal error";
1695 static gpointer
1696 mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
1697 guint32 lang_id, gunichar2 *name,
1698 MonoPEResourceDirEntry *entry,
1699 MonoPEResourceDir *root, guint32 level)
1701 gboolean is_string, is_dir;
1702 guint32 name_offset, dir_offset;
1704 /* Level 0 holds a directory entry for each type of resource
1705 * (identified by ID or name).
1707 * Level 1 holds a directory entry for each named resource
1708 * item, and each "anonymous" item of a particular type of
1709 * resource.
1711 * Level 2 holds a directory entry for each language pointing to
1712 * the actual data.
1714 is_string = MONO_PE_RES_DIR_ENTRY_NAME_IS_STRING (*entry);
1715 name_offset = MONO_PE_RES_DIR_ENTRY_NAME_OFFSET (*entry);
1717 is_dir = MONO_PE_RES_DIR_ENTRY_IS_DIR (*entry);
1718 dir_offset = MONO_PE_RES_DIR_ENTRY_DIR_OFFSET (*entry);
1720 if(level==0) {
1721 if (is_string)
1722 return NULL;
1723 } else if (level==1) {
1724 if (res_id != name_offset)
1725 return NULL;
1726 #if 0
1727 if(name!=NULL &&
1728 is_string==TRUE && name!=lookup (name_offset)) {
1729 return(NULL);
1731 #endif
1732 } else if (level==2) {
1733 if (is_string == TRUE || (is_string == FALSE && lang_id != 0 && name_offset != lang_id))
1734 return NULL;
1735 } else {
1736 g_assert_not_reached ();
1739 if(is_dir==TRUE) {
1740 MonoPEResourceDir *res_dir=(MonoPEResourceDir *)(((char *)root)+dir_offset);
1741 MonoPEResourceDirEntry *sub_entries=(MonoPEResourceDirEntry *)(res_dir+1);
1742 guint32 entries, i;
1744 entries = GUINT16_FROM_LE (res_dir->res_named_entries) + GUINT16_FROM_LE (res_dir->res_id_entries);
1746 for(i=0; i<entries; i++) {
1747 MonoPEResourceDirEntry *sub_entry=&sub_entries[i];
1748 gpointer ret;
1750 ret=mono_image_walk_resource_tree (info, res_id,
1751 lang_id, name,
1752 sub_entry, root,
1753 level+1);
1754 if(ret!=NULL) {
1755 return(ret);
1759 return(NULL);
1760 } else {
1761 MonoPEResourceDataEntry *data_entry=(MonoPEResourceDataEntry *)((char *)(root)+dir_offset);
1762 MonoPEResourceDataEntry *res;
1764 res = g_new0 (MonoPEResourceDataEntry, 1);
1766 res->rde_data_offset = GUINT32_TO_LE (data_entry->rde_data_offset);
1767 res->rde_size = GUINT32_TO_LE (data_entry->rde_size);
1768 res->rde_codepage = GUINT32_TO_LE (data_entry->rde_codepage);
1769 res->rde_reserved = GUINT32_TO_LE (data_entry->rde_reserved);
1771 return (res);
1776 * mono_image_lookup_resource:
1777 * @image: the image to look up the resource in
1778 * @res_id: A MONO_PE_RESOURCE_ID_ that represents the resource ID to lookup.
1779 * @lang_id: The language id.
1780 * @name: the resource name to lookup.
1782 * Returns: NULL if not found, otherwise a pointer to the in-memory representation
1783 * of the given resource. The caller should free it using g_free () when no longer
1784 * needed.
1786 gpointer
1787 mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name)
1789 MonoCLIImageInfo *info;
1790 MonoDotNetHeader *header;
1791 MonoPEDatadir *datadir;
1792 MonoPEDirEntry *rsrc;
1793 MonoPEResourceDir *resource_dir;
1794 MonoPEResourceDirEntry *res_entries;
1795 guint32 entries, i;
1797 if(image==NULL) {
1798 return(NULL);
1801 mono_image_ensure_section_idx (image, MONO_SECTION_RSRC);
1803 info=image->image_info;
1804 if(info==NULL) {
1805 return(NULL);
1808 header=&info->cli_header;
1809 if(header==NULL) {
1810 return(NULL);
1813 datadir=&header->datadir;
1814 if(datadir==NULL) {
1815 return(NULL);
1818 rsrc=&datadir->pe_resource_table;
1819 if(rsrc==NULL) {
1820 return(NULL);
1823 resource_dir=(MonoPEResourceDir *)mono_image_rva_map (image, rsrc->rva);
1824 if(resource_dir==NULL) {
1825 return(NULL);
1828 entries = GUINT16_FROM_LE (resource_dir->res_named_entries) + GUINT16_FROM_LE (resource_dir->res_id_entries);
1829 res_entries=(MonoPEResourceDirEntry *)(resource_dir+1);
1831 for(i=0; i<entries; i++) {
1832 MonoPEResourceDirEntry *entry=&res_entries[i];
1833 gpointer ret;
1835 ret=mono_image_walk_resource_tree (info, res_id, lang_id,
1836 name, entry, resource_dir,
1838 if(ret!=NULL) {
1839 return(ret);
1843 return(NULL);
1846 /**
1847 * mono_image_get_entry_point:
1848 * @image: the image where the entry point will be looked up.
1850 * Use this routine to determine the metadata token for method that
1851 * has been flagged as the entry point.
1853 * Returns: the token for the entry point method in the image
1855 guint32
1856 mono_image_get_entry_point (MonoImage *image)
1858 return ((MonoCLIImageInfo*)image->image_info)->cli_cli_header.ch_entry_point;
1862 * mono_image_get_resource:
1863 * @image: the image where the resource will be looked up.
1864 * @offset: The offset to add to the resource
1865 * @size: a pointer to an int where the size of the resource will be stored
1867 * This is a low-level routine that fetches a resource from the
1868 * metadata that starts at a given @offset. The @size parameter is
1869 * filled with the data field as encoded in the metadata.
1871 * Returns: the pointer to the resource whose offset is @offset.
1873 const char*
1874 mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)
1876 MonoCLIImageInfo *iinfo = image->image_info;
1877 MonoCLIHeader *ch = &iinfo->cli_cli_header;
1878 const char* data;
1880 if (!ch->ch_resources.rva || offset + 4 > ch->ch_resources.size)
1881 return NULL;
1883 data = mono_image_rva_map (image, ch->ch_resources.rva);
1884 if (!data)
1885 return NULL;
1886 data += offset;
1887 if (size)
1888 *size = read32 (data);
1889 data += 4;
1890 return data;
1893 MonoImage*
1894 mono_image_load_file_for_image (MonoImage *image, int fileidx)
1896 char *base_dir, *name;
1897 MonoImage *res;
1898 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
1899 const char *fname;
1900 guint32 fname_id;
1902 if (fileidx < 1 || fileidx > t->rows)
1903 return NULL;
1905 mono_loader_lock ();
1906 if (image->files && image->files [fileidx - 1]) {
1907 mono_loader_unlock ();
1908 return image->files [fileidx - 1];
1911 if (!image->files)
1912 image->files = g_new0 (MonoImage*, t->rows);
1914 fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
1915 fname = mono_metadata_string_heap (image, fname_id);
1916 base_dir = g_path_get_dirname (image->name);
1917 name = g_build_filename (base_dir, fname, NULL);
1918 res = mono_image_open (name, NULL);
1919 if (res) {
1920 int i;
1921 /* g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly); */
1922 res->assembly = image->assembly;
1923 for (i = 0; i < res->module_count; ++i) {
1924 if (res->modules [i] && !res->modules [i]->assembly)
1925 res->modules [i]->assembly = image->assembly;
1928 image->files [fileidx - 1] = res;
1929 #ifdef HOST_WIN32
1930 if (res->is_module_handle)
1931 mono_image_fixup_vtable (res);
1932 #endif
1934 mono_loader_unlock ();
1935 g_free (name);
1936 g_free (base_dir);
1937 return res;
1941 * mono_image_get_strong_name:
1942 * @image: a MonoImage
1943 * @size: a guint32 pointer, or NULL.
1945 * If the image has a strong name, and @size is not NULL, the value
1946 * pointed to by size will have the size of the strong name.
1948 * Returns: NULL if the image does not have a strong name, or a
1949 * pointer to the public key.
1951 const char*
1952 mono_image_get_strong_name (MonoImage *image, guint32 *size)
1954 MonoCLIImageInfo *iinfo = image->image_info;
1955 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
1956 const char* data;
1958 if (!de->size || !de->rva)
1959 return NULL;
1960 data = mono_image_rva_map (image, de->rva);
1961 if (!data)
1962 return NULL;
1963 if (size)
1964 *size = de->size;
1965 return data;
1969 * mono_image_strong_name_position:
1970 * @image: a MonoImage
1971 * @size: a guint32 pointer, or NULL.
1973 * If the image has a strong name, and @size is not NULL, the value
1974 * pointed to by size will have the size of the strong name.
1976 * Returns: the position within the image file where the strong name
1977 * is stored.
1979 guint32
1980 mono_image_strong_name_position (MonoImage *image, guint32 *size)
1982 MonoCLIImageInfo *iinfo = image->image_info;
1983 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
1984 guint32 pos;
1986 if (size)
1987 *size = de->size;
1988 if (!de->size || !de->rva)
1989 return 0;
1990 pos = mono_cli_rva_image_map (image, de->rva);
1991 return pos == INVALID_ADDRESS ? 0 : pos;
1995 * mono_image_get_public_key:
1996 * @image: a MonoImage
1997 * @size: a guint32 pointer, or NULL.
1999 * This is used to obtain the public key in the @image.
2001 * If the image has a public key, and @size is not NULL, the value
2002 * pointed to by size will have the size of the public key.
2004 * Returns: NULL if the image does not have a public key, or a pointer
2005 * to the public key.
2007 const char*
2008 mono_image_get_public_key (MonoImage *image, guint32 *size)
2010 const char *pubkey;
2011 guint32 len, tok;
2013 if (image->dynamic) {
2014 if (size)
2015 *size = ((MonoDynamicImage*)image)->public_key_len;
2016 return (char*)((MonoDynamicImage*)image)->public_key;
2018 if (image->tables [MONO_TABLE_ASSEMBLY].rows != 1)
2019 return NULL;
2020 tok = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY], 0, MONO_ASSEMBLY_PUBLIC_KEY);
2021 if (!tok)
2022 return NULL;
2023 pubkey = mono_metadata_blob_heap (image, tok);
2024 len = mono_metadata_decode_blob_size (pubkey, &pubkey);
2025 if (size)
2026 *size = len;
2027 return pubkey;
2031 * mono_image_get_name:
2032 * @name: a MonoImage
2034 * Returns: the name of the assembly.
2036 const char*
2037 mono_image_get_name (MonoImage *image)
2039 return image->assembly_name;
2043 * mono_image_get_filename:
2044 * @image: a MonoImage
2046 * Used to get the filename that hold the actual MonoImage
2048 * Returns: the filename.
2050 const char*
2051 mono_image_get_filename (MonoImage *image)
2053 return image->name;
2056 const char*
2057 mono_image_get_guid (MonoImage *image)
2059 return image->guid;
2062 const MonoTableInfo*
2063 mono_image_get_table_info (MonoImage *image, int table_id)
2065 if (table_id < 0 || table_id >= MONO_TABLE_NUM)
2066 return NULL;
2067 return &image->tables [table_id];
2071 mono_image_get_table_rows (MonoImage *image, int table_id)
2073 if (table_id < 0 || table_id >= MONO_TABLE_NUM)
2074 return 0;
2075 return image->tables [table_id].rows;
2079 mono_table_info_get_rows (const MonoTableInfo *table)
2081 return table->rows;
2085 * mono_image_get_assembly:
2086 * @image: the MonoImage.
2088 * Use this routine to get the assembly that owns this image.
2090 * Returns: the assembly that holds this image.
2092 MonoAssembly*
2093 mono_image_get_assembly (MonoImage *image)
2095 return image->assembly;
2099 * mono_image_is_dynamic:
2100 * @image: the MonoImage
2102 * Determines if the given image was created dynamically through the
2103 * System.Reflection.Emit API
2105 * Returns: TRUE if the image was created dynamically, FALSE if not.
2107 gboolean
2108 mono_image_is_dynamic (MonoImage *image)
2110 return image->dynamic;
2114 * mono_image_has_authenticode_entry:
2115 * @image: the MonoImage
2117 * Use this routine to determine if the image has a Authenticode
2118 * Certificate Table.
2120 * Returns: TRUE if the image contains an authenticode entry in the PE
2121 * directory.
2123 gboolean
2124 mono_image_has_authenticode_entry (MonoImage *image)
2126 MonoCLIImageInfo *iinfo = image->image_info;
2127 MonoDotNetHeader *header = &iinfo->cli_header;
2128 MonoPEDirEntry *de = &header->datadir.pe_certificate_table;
2129 // the Authenticode "pre" (non ASN.1) header is 8 bytes long
2130 return ((de->rva != 0) && (de->size > 8));
2133 gpointer
2134 mono_image_alloc (MonoImage *image, guint size)
2136 gpointer res;
2138 mono_perfcounters->loader_bytes += size;
2139 mono_image_lock (image);
2140 res = mono_mempool_alloc (image->mempool, size);
2141 mono_image_unlock (image);
2143 return res;
2146 gpointer
2147 mono_image_alloc0 (MonoImage *image, guint size)
2149 gpointer res;
2151 mono_perfcounters->loader_bytes += size;
2152 mono_image_lock (image);
2153 res = mono_mempool_alloc0 (image->mempool, size);
2154 mono_image_unlock (image);
2156 return res;
2159 char*
2160 mono_image_strdup (MonoImage *image, const char *s)
2162 char *res;
2164 mono_perfcounters->loader_bytes += strlen (s);
2165 mono_image_lock (image);
2166 res = mono_mempool_strdup (image->mempool, s);
2167 mono_image_unlock (image);
2169 return res;
2172 GList*
2173 g_list_prepend_image (MonoImage *image, GList *list, gpointer data)
2175 GList *new_list;
2177 new_list = mono_image_alloc (image, sizeof (GList));
2178 new_list->data = data;
2179 new_list->prev = list ? list->prev : NULL;
2180 new_list->next = list;
2182 if (new_list->prev)
2183 new_list->prev->next = new_list;
2184 if (list)
2185 list->prev = new_list;
2187 return new_list;
2190 GSList*
2191 g_slist_append_image (MonoImage *image, GSList *list, gpointer data)
2193 GSList *new_list;
2195 new_list = mono_image_alloc (image, sizeof (GSList));
2196 new_list->data = data;
2197 new_list->next = NULL;
2199 return g_slist_concat (list, new_list);
2202 void
2203 mono_image_lock (MonoImage *image)
2205 mono_locks_acquire (&image->lock, ImageDataLock);
2208 void
2209 mono_image_unlock (MonoImage *image)
2211 mono_locks_release (&image->lock, ImageDataLock);
2216 * mono_image_property_lookup:
2218 * Lookup a property on @image. Used to store very rare fields of MonoClass and MonoMethod.
2220 * LOCKING: Takes the image lock
2222 gpointer
2223 mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property)
2225 gpointer res;
2227 mono_image_lock (image);
2228 res = mono_property_hash_lookup (image->property_hash, subject, property);
2229 mono_image_unlock (image);
2231 return res;
2235 * mono_image_property_insert:
2237 * Insert a new property @property with value @value on @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
2239 * LOCKING: Takes the image lock
2241 void
2242 mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value)
2244 mono_image_lock (image);
2245 mono_property_hash_insert (image->property_hash, subject, property, value);
2246 mono_image_unlock (image);
2250 * mono_image_property_remove:
2252 * Remove all properties associated with @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
2254 * LOCKING: Takes the image lock
2256 void
2257 mono_image_property_remove (MonoImage *image, gpointer subject)
2259 mono_image_lock (image);
2260 mono_property_hash_remove_object (image->property_hash, subject);
2261 mono_image_unlock (image);