2 * image.c: Routines for manipulating an image stored in an
3 * extended PE/COFF file.
6 * Miguel de Icaza (miguel@ximian.com)
7 * Paolo Molaro (lupus@ximian.com)
9 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
21 #include "mono-endian.h"
22 #include "tabledefs.h"
23 #include "tokentype.h"
24 #include "metadata-internals.h"
25 #include "profiler-private.h"
29 #include <mono/io-layer/io-layer.h>
30 #include <mono/utils/mono-logger.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>
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 */
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
;
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
)){
73 if (image
->is_module_handle
)
76 return addr
- tables
->st_virtual_address
+ tables
->st_raw_data_ptr
;
80 return INVALID_ADDRESS
;
84 * mono_images_rva_map:
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.
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
;
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
))
110 if (image
->is_module_handle
)
111 return image
->raw_data
+ addr
;
113 return (char*)iinfo
->cli_sections
[i
] +
114 (addr
- tables
->st_virtual_address
);
124 * Initialize the global variables used by this module.
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
;
140 * mono_images_cleanup:
142 * Free all resources used by this module.
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
;
172 g_return_val_if_fail (section
< iinfo
->cli_section_count
, FALSE
);
174 if (iinfo
->cli_sections
[section
] != NULL
)
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
)
184 if (image
->is_module_handle
)
185 iinfo
->cli_sections
[section
] = image
->raw_data
+ sect
->st_virtual_address
;
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
;
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
;
209 for (i
= 0; i
< ii
->cli_section_count
; i
++){
210 if (strncmp (ii
->cli_section_tables
[i
].st_name
, section
, 8) != 0)
213 return mono_image_ensure_section_idx (image
, i
);
219 load_section_tables (MonoImage
*image
, MonoCLIImageInfo
*iinfo
, guint32 offset
)
221 const int top
= iinfo
->cli_header
.coff
.coff_sections
;
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
)
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
);
247 /* consistency checks here */
254 load_cli_header (MonoImage
*image
, MonoCLIImageInfo
*iinfo
)
258 offset
= mono_cli_rva_image_map (image
, iinfo
->cli_header
.datadir
.pe_cli_header
.rva
);
259 if (offset
== INVALID_ADDRESS
)
262 if (offset
+ sizeof (MonoCLIHeader
) > image
->raw_data_len
)
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
);
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"); */
318 load_metadata_ptrs (MonoImage
*image
, MonoCLIImageInfo
*iinfo
)
320 guint32 offset
, size
;
326 offset
= mono_cli_rva_image_map (image
, iinfo
->cli_cli_header
.ch_metadata
.rva
);
327 if (offset
== INVALID_ADDRESS
)
330 size
= iinfo
->cli_cli_header
.ch_metadata
.size
;
332 if (offset
+ size
> image
->raw_data_len
)
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
;
343 image
->md_version_major
= read16 (ptr
);
345 image
->md_version_minor
= read16 (ptr
);
348 version_string_len
= read32 (ptr
);
350 image
->version
= g_strndup (ptr
, version_string_len
);
351 ptr
+= version_string_len
;
352 pad
= ptr
- image
->raw_metadata
;
354 ptr
+= 4 - (pad
% 4);
358 /* skip over flags */
361 streams
= read16 (ptr
);
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);
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);
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);
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);
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);
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);
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
);
392 g_message ("Unknown heap type: %s\n", ptr
+ 8);
393 ptr
+= 8 + strlen (ptr
+ 8) + 1;
395 pad
= ptr
- image
->raw_metadata
;
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
);
409 * Load representation of logical metadata tables, from the "#~" stream
412 load_tables (MonoImage
*image
)
414 const char *heap_tables
= image
->heap_tables
.data
;
416 guint64 valid_mask
, sorted_mask
;
417 int valid
= 0, table
;
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
)
433 image
->tables
[table
].rows
= 0;
436 if (table
> MONO_TABLE_LAST
) {
437 g_warning("bits in valid must be zero above 0x2d (II - 23.1.6)");
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);
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
);
458 load_metadata (MonoImage
*image
, MonoCLIImageInfo
*iinfo
)
460 if (!load_metadata_ptrs (image
, iinfo
))
463 return load_tables (image
);
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
;
476 if (image
->dynamic
) {
478 image
->checked_module_cctor
= TRUE
;
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;
488 last_method
= mono_metadata_decode_row_col (t
, 1, MONO_TYPEDEF_METHOD_LIST
) - 1;
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
;
502 image
->has_module_cctor
= FALSE
;
503 image
->checked_module_cctor
= TRUE
;
507 load_modules (MonoImage
*image
)
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.
527 mono_image_load_module (MonoImage
*image
, int idx
)
530 MonoTableInfo
*file_table
;
533 gboolean refonly
= image
->ref_only
;
534 GList
*list_iter
, *valid_modules
= NULL
;
535 MonoImageOpenStatus status
;
537 if ((image
->module_count
== 0) || (idx
> image
->module_count
))
539 if (image
->modules_loaded
[idx
- 1])
540 return image
->modules
[idx
- 1];
542 file_table
= &image
->tables
[MONO_TABLE_FILE
];
543 for (i
= 0; i
< file_table
->rows
; i
++) {
544 guint32 cols
[MONO_FILE_SIZE
];
545 mono_metadata_decode_row (file_table
, i
, cols
, MONO_FILE_SIZE
);
546 if (cols
[MONO_FILE_FLAGS
] == FILE_CONTAINS_NO_METADATA
)
548 valid_modules
= g_list_prepend (valid_modules
, (char*)mono_metadata_string_heap (image
, cols
[MONO_FILE_NAME
]));
551 t
= &image
->tables
[MONO_TABLE_MODULEREF
];
552 base_dir
= g_path_get_dirname (image
->name
);
557 guint32 cols
[MONO_MODULEREF_SIZE
];
558 /* if there is no file table, we try to load the module... */
559 int valid
= file_table
->rows
== 0;
561 mono_metadata_decode_row (t
, idx
- 1, cols
, MONO_MODULEREF_SIZE
);
562 name
= mono_metadata_string_heap (image
, cols
[MONO_MODULEREF_NAME
]);
563 for (list_iter
= valid_modules
; list_iter
; list_iter
= list_iter
->next
) {
564 /* be safe with string dups, but we could just compare string indexes */
565 if (strcmp (list_iter
->data
, name
) == 0) {
571 module_ref
= g_build_filename (base_dir
, name
, NULL
);
572 image
->modules
[idx
- 1] = mono_image_open_full (module_ref
, &status
, refonly
);
573 if (image
->modules
[idx
- 1]) {
574 mono_image_addref (image
->modules
[idx
- 1]);
575 image
->modules
[idx
- 1]->assembly
= image
->assembly
;
577 if (image
->modules
[idx
- 1]->is_module_handle
)
578 mono_image_fixup_vtable (image
->modules
[idx
- 1]);
580 /* g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly); */
586 image
->modules_loaded
[idx
- 1] = TRUE
;
589 g_list_free (valid_modules
);
591 return image
->modules
[idx
- 1];
595 class_key_extract (gpointer value
)
597 MonoClass
*class = value
;
599 return GUINT_TO_POINTER (class->type_token
);
603 class_next_value (gpointer value
)
605 MonoClass
*class = value
;
607 return (gpointer
*)&class->next_class_cache
;
611 mono_image_init (MonoImage
*image
)
613 image
->mempool
= mono_mempool_new_size (512);
614 mono_internal_hash_table_init (&image
->class_cache
,
618 image
->field_cache
= g_hash_table_new (NULL
, NULL
);
620 image
->typespec_cache
= g_hash_table_new (NULL
, NULL
);
621 image
->memberref_signatures
= g_hash_table_new (NULL
, NULL
);
622 image
->helper_signatures
= g_hash_table_new (g_str_hash
, g_str_equal
);
623 image
->method_signatures
= g_hash_table_new (NULL
, NULL
);
625 image
->property_hash
= mono_property_hash_new ();
626 InitializeCriticalSection (&image
->lock
);
627 InitializeCriticalSection (&image
->szarray_cache_lock
);
630 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
631 #define SWAP64(x) (x) = GUINT64_FROM_LE ((x))
632 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
633 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
634 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
643 * Returns < 0 to indicate an error.
646 do_load_header (MonoImage
*image
, MonoDotNetHeader
*header
, int offset
)
648 MonoDotNetHeader64 header64
;
651 if (!image
->is_module_handle
)
653 if (offset
+ sizeof (MonoDotNetHeader32
) > image
->raw_data_len
)
656 memcpy (header
, image
->raw_data
+ offset
, sizeof (MonoDotNetHeader
));
658 if (header
->pesig
[0] != 'P' || header
->pesig
[1] != 'E')
661 /* endian swap the fields common between PE and PE+ */
662 SWAP32 (header
->coff
.coff_time
);
663 SWAP32 (header
->coff
.coff_symptr
);
664 SWAP32 (header
->coff
.coff_symcount
);
665 SWAP16 (header
->coff
.coff_machine
);
666 SWAP16 (header
->coff
.coff_sections
);
667 SWAP16 (header
->coff
.coff_opt_header_size
);
668 SWAP16 (header
->coff
.coff_attributes
);
670 SWAP32 (header
->pe
.pe_code_size
);
671 SWAP32 (header
->pe
.pe_uninit_data_size
);
672 SWAP32 (header
->pe
.pe_rva_entry_point
);
673 SWAP32 (header
->pe
.pe_rva_code_base
);
674 SWAP32 (header
->pe
.pe_rva_data_base
);
675 SWAP16 (header
->pe
.pe_magic
);
677 /* now we are ready for the basic tests */
679 if (header
->pe
.pe_magic
== 0x10B) {
680 offset
+= sizeof (MonoDotNetHeader
);
681 SWAP32 (header
->pe
.pe_data_size
);
682 if (header
->coff
.coff_opt_header_size
!= (sizeof (MonoDotNetHeader
) - sizeof (MonoCOFFHeader
) - 4))
685 SWAP32 (header
->nt
.pe_image_base
); /* must be 0x400000 */
686 SWAP32 (header
->nt
.pe_stack_reserve
);
687 SWAP32 (header
->nt
.pe_stack_commit
);
688 SWAP32 (header
->nt
.pe_heap_reserve
);
689 SWAP32 (header
->nt
.pe_heap_commit
);
690 } else if (header
->pe
.pe_magic
== 0x20B) {
691 /* PE32+ file format */
692 if (header
->coff
.coff_opt_header_size
!= (sizeof (MonoDotNetHeader64
) - sizeof (MonoCOFFHeader
) - 4))
694 memcpy (&header64
, image
->raw_data
+ offset
, sizeof (MonoDotNetHeader64
));
695 offset
+= sizeof (MonoDotNetHeader64
);
696 /* copy the fields already swapped. the last field, pe_data_size, is missing */
697 memcpy (&header64
, header
, sizeof (MonoDotNetHeader
) - 4);
698 /* FIXME: we lose bits here, but we don't use this stuff internally, so we don't care much.
699 * will be fixed when we change MonoDotNetHeader to not match the 32 bit variant
701 SWAP64 (header64
.nt
.pe_image_base
);
702 header
->nt
.pe_image_base
= header64
.nt
.pe_image_base
;
703 SWAP64 (header64
.nt
.pe_stack_reserve
);
704 header
->nt
.pe_stack_reserve
= header64
.nt
.pe_stack_reserve
;
705 SWAP64 (header64
.nt
.pe_stack_commit
);
706 header
->nt
.pe_stack_commit
= header64
.nt
.pe_stack_commit
;
707 SWAP64 (header64
.nt
.pe_heap_reserve
);
708 header
->nt
.pe_heap_reserve
= header64
.nt
.pe_heap_reserve
;
709 SWAP64 (header64
.nt
.pe_heap_commit
);
710 header
->nt
.pe_heap_commit
= header64
.nt
.pe_heap_commit
;
712 header
->nt
.pe_section_align
= header64
.nt
.pe_section_align
;
713 header
->nt
.pe_file_alignment
= header64
.nt
.pe_file_alignment
;
714 header
->nt
.pe_os_major
= header64
.nt
.pe_os_major
;
715 header
->nt
.pe_os_minor
= header64
.nt
.pe_os_minor
;
716 header
->nt
.pe_user_major
= header64
.nt
.pe_user_major
;
717 header
->nt
.pe_user_minor
= header64
.nt
.pe_user_minor
;
718 header
->nt
.pe_subsys_major
= header64
.nt
.pe_subsys_major
;
719 header
->nt
.pe_subsys_minor
= header64
.nt
.pe_subsys_minor
;
720 header
->nt
.pe_reserved_1
= header64
.nt
.pe_reserved_1
;
721 header
->nt
.pe_image_size
= header64
.nt
.pe_image_size
;
722 header
->nt
.pe_header_size
= header64
.nt
.pe_header_size
;
723 header
->nt
.pe_checksum
= header64
.nt
.pe_checksum
;
724 header
->nt
.pe_subsys_required
= header64
.nt
.pe_subsys_required
;
725 header
->nt
.pe_dll_flags
= header64
.nt
.pe_dll_flags
;
726 header
->nt
.pe_loader_flags
= header64
.nt
.pe_loader_flags
;
727 header
->nt
.pe_data_dir_count
= header64
.nt
.pe_data_dir_count
;
729 /* copy the datadir */
730 memcpy (&header
->datadir
, &header64
.datadir
, sizeof (MonoPEDatadir
));
735 /* MonoPEHeaderNT: not used yet */
736 SWAP32 (header
->nt
.pe_section_align
); /* must be 8192 */
737 SWAP32 (header
->nt
.pe_file_alignment
); /* must be 512 or 4096 */
738 SWAP16 (header
->nt
.pe_os_major
); /* must be 4 */
739 SWAP16 (header
->nt
.pe_os_minor
); /* must be 0 */
740 SWAP16 (header
->nt
.pe_user_major
);
741 SWAP16 (header
->nt
.pe_user_minor
);
742 SWAP16 (header
->nt
.pe_subsys_major
);
743 SWAP16 (header
->nt
.pe_subsys_minor
);
744 SWAP32 (header
->nt
.pe_reserved_1
);
745 SWAP32 (header
->nt
.pe_image_size
);
746 SWAP32 (header
->nt
.pe_header_size
);
747 SWAP32 (header
->nt
.pe_checksum
);
748 SWAP16 (header
->nt
.pe_subsys_required
);
749 SWAP16 (header
->nt
.pe_dll_flags
);
750 SWAP32 (header
->nt
.pe_loader_flags
);
751 SWAP32 (header
->nt
.pe_data_dir_count
);
753 /* MonoDotNetHeader: mostly unused */
754 SWAPPDE (header
->datadir
.pe_export_table
);
755 SWAPPDE (header
->datadir
.pe_import_table
);
756 SWAPPDE (header
->datadir
.pe_resource_table
);
757 SWAPPDE (header
->datadir
.pe_exception_table
);
758 SWAPPDE (header
->datadir
.pe_certificate_table
);
759 SWAPPDE (header
->datadir
.pe_reloc_table
);
760 SWAPPDE (header
->datadir
.pe_debug
);
761 SWAPPDE (header
->datadir
.pe_copyright
);
762 SWAPPDE (header
->datadir
.pe_global_ptr
);
763 SWAPPDE (header
->datadir
.pe_tls_table
);
764 SWAPPDE (header
->datadir
.pe_load_config_table
);
765 SWAPPDE (header
->datadir
.pe_bound_import
);
766 SWAPPDE (header
->datadir
.pe_iat
);
767 SWAPPDE (header
->datadir
.pe_delay_import_desc
);
768 SWAPPDE (header
->datadir
.pe_cli_header
);
769 SWAPPDE (header
->datadir
.pe_reserved
);
772 if (image
->is_module_handle
)
773 image
->raw_data_len
= header
->nt
.pe_image_size
;
780 mono_image_load_pe_data (MonoImage
*image
)
782 MonoCLIImageInfo
*iinfo
;
783 MonoDotNetHeader
*header
;
784 MonoMSDOSHeader msdos
;
787 iinfo
= image
->image_info
;
788 header
= &iinfo
->cli_header
;
791 if (!image
->is_module_handle
)
793 if (offset
+ sizeof (msdos
) > image
->raw_data_len
)
795 memcpy (&msdos
, image
->raw_data
+ offset
, sizeof (msdos
));
797 if (!(msdos
.msdos_sig
[0] == 'M' && msdos
.msdos_sig
[1] == 'Z'))
800 msdos
.pe_offset
= GUINT32_FROM_LE (msdos
.pe_offset
);
802 offset
= msdos
.pe_offset
;
804 offset
= do_load_header (image
, header
, offset
);
809 * this tests for a x86 machine type, but itanium, amd64 and others could be used, too.
811 if (header->coff.coff_machine != 0x14c)
817 * The spec says that this field should contain 6.0, but Visual Studio includes a new compiler,
818 * which produces binaries with 7.0. From Sergey:
820 * The reason is that MSVC7 uses traditional compile/link
821 * sequence for CIL executables, and VS.NET (and Framework
822 * SDK) includes linker version 7, that puts 7.0 in this
823 * field. That's why it's currently not possible to load VC
824 * binaries with Mono. This field is pretty much meaningless
825 * anyway (what linker?).
827 if (header
->pe
.pe_major
!= 6 || header
->pe
.pe_minor
!= 0)
832 * FIXME: byte swap all addresses here for header.
835 if (!load_section_tables (image
, iinfo
, offset
))
845 mono_image_load_cli_data (MonoImage
*image
)
847 MonoCLIImageInfo
*iinfo
;
848 MonoDotNetHeader
*header
;
850 iinfo
= image
->image_info
;
851 header
= &iinfo
->cli_header
;
853 /* Load the CLI header */
854 if (!load_cli_header (image
, iinfo
))
857 if (!load_metadata (image
, iinfo
))
864 mono_image_load_names (MonoImage
*image
)
866 /* modules don't have an assembly table row */
867 if (image
->tables
[MONO_TABLE_ASSEMBLY
].rows
) {
868 image
->assembly_name
= mono_metadata_string_heap (image
,
869 mono_metadata_decode_row_col (&image
->tables
[MONO_TABLE_ASSEMBLY
],
870 0, MONO_ASSEMBLY_NAME
));
873 image
->module_name
= mono_metadata_string_heap (image
,
874 mono_metadata_decode_row_col (&image
->tables
[MONO_TABLE_MODULE
],
875 0, MONO_MODULE_NAME
));
879 do_mono_image_load (MonoImage
*image
, MonoImageOpenStatus
*status
,
880 gboolean care_about_cli
, gboolean care_about_pecoff
)
882 MonoCLIImageInfo
*iinfo
;
883 MonoDotNetHeader
*header
;
885 mono_profiler_module_event (image
, MONO_PROFILE_START_LOAD
);
887 mono_image_init (image
);
889 iinfo
= image
->image_info
;
890 header
= &iinfo
->cli_header
;
893 *status
= MONO_IMAGE_IMAGE_INVALID
;
895 if (care_about_pecoff
== FALSE
)
898 if (!mono_verifier_verify_pe_data (image
, NULL
))
901 if (!mono_image_load_pe_data (image
))
904 if (care_about_cli
== FALSE
) {
908 if (!mono_verifier_verify_cli_data (image
, NULL
))
911 if (!mono_image_load_cli_data (image
))
914 if (!mono_verifier_verify_table_data (image
, NULL
))
917 mono_image_load_names (image
);
919 load_modules (image
);
922 mono_profiler_module_loaded (image
, MONO_PROFILE_OK
);
924 *status
= MONO_IMAGE_OK
;
929 mono_profiler_module_loaded (image
, MONO_PROFILE_FAILED
);
930 mono_image_close (image
);
935 do_mono_image_open (const char *fname
, MonoImageOpenStatus
*status
,
936 gboolean care_about_cli
, gboolean care_about_pecoff
, gboolean refonly
)
938 MonoCLIImageInfo
*iinfo
;
942 if ((filed
= mono_file_map_open (fname
)) == NULL
){
943 if (IS_PORTABILITY_SET
) {
944 gchar
*ffname
= mono_portability_find_file (fname
, TRUE
);
946 filed
= mono_file_map_open (ffname
);
953 *status
= MONO_IMAGE_ERROR_ERRNO
;
958 image
= g_new0 (MonoImage
, 1);
959 image
->raw_buffer_used
= TRUE
;
960 image
->raw_data_len
= mono_file_map_size (filed
);
961 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
);
962 if (!image
->raw_data
) {
963 mono_file_map_close (filed
);
966 *status
= MONO_IMAGE_IMAGE_INVALID
;
969 iinfo
= g_new0 (MonoCLIImageInfo
, 1);
970 image
->image_info
= iinfo
;
971 image
->name
= mono_path_resolve_symlinks (fname
);
972 image
->ref_only
= refonly
;
973 image
->ref_count
= 1;
974 /* if MONO_SECURITY_MODE_CORE_CLR is set then determine if this image is platform code */
975 image
->core_clr_platform_code
= mono_security_core_clr_determine_platform_image (image
);
977 mono_file_map_close (filed
);
978 return do_mono_image_load (image
, status
, care_about_cli
, care_about_pecoff
);
982 mono_image_loaded_full (const char *name
, gboolean refonly
)
985 GHashTable
*loaded_images
= refonly
? loaded_images_refonly_hash
: loaded_images_hash
;
988 res
= g_hash_table_lookup (loaded_images
, name
);
989 mono_images_unlock ();
995 * @name: name of the image to load
997 * This routine ensures that the given image is loaded.
999 * Returns: the loaded MonoImage, or NULL on failure.
1002 mono_image_loaded (const char *name
)
1004 return mono_image_loaded_full (name
, FALSE
);
1013 find_by_guid (gpointer key
, gpointer val
, gpointer user_data
)
1015 GuidData
*data
= user_data
;
1021 if (strcmp (data
->guid
, mono_image_get_guid (image
)) == 0)
1026 mono_image_loaded_by_guid_full (const char *guid
, gboolean refonly
)
1029 GHashTable
*loaded_images
= refonly
? loaded_images_refonly_hash
: loaded_images_hash
;
1033 mono_images_lock ();
1034 g_hash_table_foreach (loaded_images
, find_by_guid
, &data
);
1035 mono_images_unlock ();
1040 mono_image_loaded_by_guid (const char *guid
)
1042 return mono_image_loaded_by_guid_full (guid
, FALSE
);
1046 register_image (MonoImage
*image
)
1049 GHashTable
*loaded_images
= image
->ref_only
? loaded_images_refonly_hash
: loaded_images_hash
;
1051 mono_images_lock ();
1052 image2
= g_hash_table_lookup (loaded_images
, image
->name
);
1055 /* Somebody else beat us to it */
1056 mono_image_addref (image2
);
1057 mono_images_unlock ();
1058 mono_image_close (image
);
1061 g_hash_table_insert (loaded_images
, image
->name
, image
);
1062 if (image
->assembly_name
&& (g_hash_table_lookup (loaded_images
, image
->assembly_name
) == NULL
))
1063 g_hash_table_insert (loaded_images
, (char *) image
->assembly_name
, image
);
1064 mono_images_unlock ();
1070 mono_image_open_from_data_full (char *data
, guint32 data_len
, gboolean need_copy
, MonoImageOpenStatus
*status
, gboolean refonly
)
1072 MonoCLIImageInfo
*iinfo
;
1076 if (!data
|| !data_len
) {
1078 *status
= MONO_IMAGE_IMAGE_INVALID
;
1083 datac
= g_try_malloc (data_len
);
1086 *status
= MONO_IMAGE_ERROR_ERRNO
;
1089 memcpy (datac
, data
, data_len
);
1092 image
= g_new0 (MonoImage
, 1);
1093 image
->raw_data
= datac
;
1094 image
->raw_data_len
= data_len
;
1095 image
->raw_data_allocated
= need_copy
;
1096 image
->name
= g_strdup_printf ("data-%p", datac
);
1097 iinfo
= g_new0 (MonoCLIImageInfo
, 1);
1098 image
->image_info
= iinfo
;
1099 image
->ref_only
= refonly
;
1101 image
= do_mono_image_load (image
, status
, TRUE
, TRUE
);
1105 return register_image (image
);
1109 mono_image_open_from_data (char *data
, guint32 data_len
, gboolean need_copy
, MonoImageOpenStatus
*status
)
1111 return mono_image_open_from_data_full (data
, data_len
, need_copy
, status
, FALSE
);
1115 /* fname is not duplicated. */
1117 mono_image_open_from_module_handle (HMODULE module_handle
, char* fname
, gboolean has_entry_point
, MonoImageOpenStatus
* status
)
1120 MonoCLIImageInfo
* iinfo
;
1122 image
= g_new0 (MonoImage
, 1);
1123 image
->raw_data
= (char*) module_handle
;
1124 image
->is_module_handle
= TRUE
;
1125 iinfo
= g_new0 (MonoCLIImageInfo
, 1);
1126 image
->image_info
= iinfo
;
1127 image
->name
= fname
;
1128 image
->ref_count
= has_entry_point
? 0 : 1;
1129 image
->has_entry_point
= has_entry_point
;
1131 image
= do_mono_image_load (image
, status
, TRUE
, TRUE
);
1135 return register_image (image
);
1140 mono_image_open_full (const char *fname
, MonoImageOpenStatus
*status
, gboolean refonly
)
1143 GHashTable
*loaded_images
;
1146 g_return_val_if_fail (fname
!= NULL
, NULL
);
1149 /* Load modules using LoadLibrary. */
1150 if (!refonly
&& coree_module_handle
) {
1151 HMODULE module_handle
;
1152 guint16
*fname_utf16
;
1155 absfname
= mono_path_resolve_symlinks (fname
);
1158 /* There is little overhead because the OS loader lock is held by LoadLibrary. */
1159 mono_images_lock ();
1160 image
= g_hash_table_lookup (loaded_images_hash
, absfname
);
1162 g_assert (image
->is_module_handle
);
1163 if (image
->has_entry_point
&& image
->ref_count
== 0) {
1164 /* Increment reference count on images loaded outside of the runtime. */
1165 fname_utf16
= g_utf8_to_utf16 (absfname
, -1, NULL
, NULL
, NULL
);
1166 /* The image is already loaded because _CorDllMain removes images from the hash. */
1167 module_handle
= LoadLibrary (fname_utf16
);
1168 g_assert (module_handle
== (HMODULE
) image
->raw_data
);
1170 mono_image_addref (image
);
1171 mono_images_unlock ();
1173 g_free (fname_utf16
);
1178 fname_utf16
= g_utf8_to_utf16 (absfname
, -1, NULL
, NULL
, NULL
);
1179 module_handle
= MonoLoadImage (fname_utf16
);
1180 if (status
&& module_handle
== NULL
)
1181 last_error
= GetLastError ();
1183 /* mono_image_open_from_module_handle is called by _CorDllMain. */
1184 image
= g_hash_table_lookup (loaded_images_hash
, absfname
);
1186 mono_image_addref (image
);
1187 mono_images_unlock ();
1189 g_free (fname_utf16
);
1191 if (module_handle
== NULL
) {
1195 if (last_error
== ERROR_BAD_EXE_FORMAT
|| last_error
== STATUS_INVALID_IMAGE_FORMAT
)
1196 *status
= MONO_IMAGE_IMAGE_INVALID
;
1198 *status
= MONO_IMAGE_ERROR_ERRNO
;
1204 g_assert (image
->is_module_handle
);
1205 g_assert (image
->has_entry_point
);
1210 return mono_image_open_from_module_handle (module_handle
, absfname
, FALSE
, status
);
1214 absfname
= mono_path_canonicalize (fname
);
1217 * The easiest solution would be to do all the loading inside the mutex,
1218 * but that would lead to scalability problems. So we let the loading
1219 * happen outside the mutex, and if multiple threads happen to load
1220 * the same image, we discard all but the first copy.
1222 mono_images_lock ();
1223 loaded_images
= refonly
? loaded_images_refonly_hash
: loaded_images_hash
;
1224 image
= g_hash_table_lookup (loaded_images
, absfname
);
1228 mono_image_addref (image
);
1229 mono_images_unlock ();
1232 mono_images_unlock ();
1234 image
= do_mono_image_open (fname
, status
, TRUE
, TRUE
, refonly
);
1238 return register_image (image
);
1243 * @fname: filename that points to the module we want to open
1244 * @status: An error condition is returned in this field
1246 * Returns: An open image of type %MonoImage or NULL on error.
1247 * The caller holds a temporary reference to the returned image which should be cleared
1248 * when no longer needed by calling mono_image_close ().
1249 * if NULL, then check the value of @status for details on the error
1252 mono_image_open (const char *fname
, MonoImageOpenStatus
*status
)
1254 return mono_image_open_full (fname
, status
, FALSE
);
1258 * mono_pe_file_open:
1259 * @fname: filename that points to the module we want to open
1260 * @status: An error condition is returned in this field
1262 * Returns: An open image of type %MonoImage or NULL on error. if
1263 * NULL, then check the value of @status for details on the error.
1264 * This variant for mono_image_open DOES NOT SET UP CLI METADATA.
1265 * It's just a PE file loader, used for FileVersionInfo. It also does
1266 * not use the image cache.
1269 mono_pe_file_open (const char *fname
, MonoImageOpenStatus
*status
)
1271 g_return_val_if_fail (fname
!= NULL
, NULL
);
1273 return(do_mono_image_open (fname
, status
, FALSE
, TRUE
, FALSE
));
1277 * mono_image_open_raw
1278 * @fname: filename that points to the module we want to open
1279 * @status: An error condition is returned in this field
1281 * Returns an image without loading neither pe or cli data.
1283 * Use mono_image_load_pe_data and mono_image_load_cli_data to load them.
1286 mono_image_open_raw (const char *fname
, MonoImageOpenStatus
*status
)
1288 g_return_val_if_fail (fname
!= NULL
, NULL
);
1290 return(do_mono_image_open (fname
, status
, FALSE
, FALSE
, FALSE
));
1294 mono_image_fixup_vtable (MonoImage
*image
)
1297 MonoCLIImageInfo
*iinfo
;
1299 MonoVTableFixup
*vtfixup
;
1305 g_assert (image
->is_module_handle
);
1307 iinfo
= image
->image_info
;
1308 de
= &iinfo
->cli_cli_header
.ch_vtable_fixups
;
1309 if (!de
->rva
|| !de
->size
)
1311 vtfixup
= (MonoVTableFixup
*) mono_image_rva_map (image
, de
->rva
);
1315 count
= de
->size
/ sizeof (MonoVTableFixup
);
1317 if (!vtfixup
->rva
|| !vtfixup
->count
)
1320 slot
= mono_image_rva_map (image
, vtfixup
->rva
);
1322 slot_type
= vtfixup
->type
;
1323 slot_count
= vtfixup
->count
;
1324 if (slot_type
& VTFIXUP_TYPE_32BIT
)
1325 while (slot_count
--) {
1326 *((guint32
*) slot
) = (guint32
) mono_marshal_get_vtfixup_ftnptr (image
, *((guint32
*) slot
), slot_type
);
1327 slot
= ((guint32
*) slot
) + 1;
1329 else if (slot_type
& VTFIXUP_TYPE_64BIT
)
1330 while (slot_count
--) {
1331 *((guint64
*) slot
) = (guint64
) mono_marshal_get_vtfixup_ftnptr (image
, *((guint64
*) slot
), slot_type
);
1332 slot
= ((guint32
*) slot
) + 1;
1335 g_assert_not_reached();
1340 g_assert_not_reached();
1345 free_hash_table (gpointer key
, gpointer val
, gpointer user_data
)
1347 g_hash_table_destroy ((GHashTable
*)val
);
1352 free_mr_signatures (gpointer key, gpointer val, gpointer user_data)
1354 mono_metadata_free_method_signature ((MonoMethodSignature*)val);
1359 free_remoting_wrappers (gpointer key
, gpointer val
, gpointer user_data
)
1365 free_array_cache_entry (gpointer key
, gpointer val
, gpointer user_data
)
1367 g_slist_free ((GSList
*)val
);
1371 * mono_image_addref:
1372 * @image: The image file we wish to add a reference to
1374 * Increases the reference count of an image.
1377 mono_image_addref (MonoImage
*image
)
1379 InterlockedIncrement (&image
->ref_count
);
1383 mono_dynamic_stream_reset (MonoDynamicStream
* stream
)
1385 stream
->alloc_size
= stream
->index
= stream
->offset
= 0;
1386 g_free (stream
->data
);
1387 stream
->data
= NULL
;
1389 g_hash_table_destroy (stream
->hash
);
1390 stream
->hash
= NULL
;
1395 free_hash (GHashTable
*hash
)
1398 g_hash_table_destroy (hash
);
1402 * Returns whether mono_image_close_finish() must be called as well.
1403 * We must unload images in two steps because clearing the domain in
1404 * SGen requires the class metadata to be intact, but we need to free
1405 * the mono_g_hash_tables in case a collection occurs during domain
1406 * unloading and the roots would trip up the GC.
1409 mono_image_close_except_pools (MonoImage
*image
)
1412 GHashTable
*loaded_images
;
1415 g_return_val_if_fail (image
!= NULL
, FALSE
);
1418 * Atomically decrement the refcount and remove ourselves from the hash tables, so
1419 * register_image () can't grab an image which is being closed.
1421 mono_images_lock ();
1423 if (InterlockedDecrement (&image
->ref_count
) > 0) {
1424 mono_images_unlock ();
1428 loaded_images
= image
->ref_only
? loaded_images_refonly_hash
: loaded_images_hash
;
1429 image2
= g_hash_table_lookup (loaded_images
, image
->name
);
1430 if (image
== image2
) {
1431 /* This is not true if we are called from mono_image_open () */
1432 g_hash_table_remove (loaded_images
, image
->name
);
1434 if (image
->assembly_name
&& (g_hash_table_lookup (loaded_images
, image
->assembly_name
) == image
))
1435 g_hash_table_remove (loaded_images
, (char *) image
->assembly_name
);
1437 mono_images_unlock ();
1440 if (image
->is_module_handle
&& image
->has_entry_point
) {
1441 mono_images_lock ();
1442 if (image
->ref_count
== 0) {
1443 /* Image will be closed by _CorDllMain. */
1444 FreeLibrary ((HMODULE
) image
->raw_data
);
1445 mono_images_unlock ();
1448 mono_images_unlock ();
1452 mono_profiler_module_event (image
, MONO_PROFILE_START_UNLOAD
);
1454 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_ASSEMBLY
, "Unloading image %s [%p].", image
->name
, image
);
1456 mono_metadata_clean_for_image (image
);
1459 * The caches inside a MonoImage might refer to metadata which is stored in referenced
1460 * assemblies, so we can't release these references in mono_assembly_close () since the
1461 * MonoImage might outlive its associated MonoAssembly.
1463 if (image
->references
&& !image
->dynamic
) {
1464 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_ASSEMBLYREF
];
1467 for (i
= 0; i
< t
->rows
; i
++) {
1468 if (image
->references
[i
]) {
1469 if (!mono_assembly_close_except_image_pools (image
->references
[i
]))
1470 image
->references
[i
] = NULL
;
1474 if (image
->references
) {
1475 g_free (image
->references
);
1476 image
->references
= NULL
;
1481 mono_images_lock ();
1482 if (image
->is_module_handle
&& !image
->has_entry_point
)
1483 FreeLibrary ((HMODULE
) image
->raw_data
);
1484 mono_images_unlock ();
1487 if (image
->raw_buffer_used
) {
1488 if (image
->raw_data
!= NULL
)
1489 mono_file_unmap (image
->raw_data
, image
->raw_data_handle
);
1492 if (image
->raw_data_allocated
) {
1493 /* FIXME: do we need this? (image is disposed anyway) */
1494 /* image->raw_metadata and cli_sections might lie inside image->raw_data */
1495 MonoCLIImageInfo
*ii
= image
->image_info
;
1497 if ((image
->raw_metadata
> image
->raw_data
) &&
1498 (image
->raw_metadata
<= (image
->raw_data
+ image
->raw_data_len
)))
1499 image
->raw_metadata
= NULL
;
1501 for (i
= 0; i
< ii
->cli_section_count
; i
++)
1502 if (((char*)(ii
->cli_sections
[i
]) > image
->raw_data
) &&
1503 ((char*)(ii
->cli_sections
[i
]) <= ((char*)image
->raw_data
+ image
->raw_data_len
)))
1504 ii
->cli_sections
[i
] = NULL
;
1506 g_free (image
->raw_data
);
1509 if (debug_assembly_unload
) {
1510 image
->name
= g_strdup_printf ("%s - UNLOADED", image
->name
);
1512 g_free (image
->name
);
1513 g_free (image
->guid
);
1514 g_free (image
->version
);
1515 g_free (image
->files
);
1518 if (image
->method_cache
)
1519 mono_value_hash_table_destroy (image
->method_cache
);
1520 if (image
->methodref_cache
)
1521 g_hash_table_destroy (image
->methodref_cache
);
1522 mono_internal_hash_table_destroy (&image
->class_cache
);
1523 g_hash_table_destroy (image
->field_cache
);
1524 if (image
->array_cache
) {
1525 g_hash_table_foreach (image
->array_cache
, free_array_cache_entry
, NULL
);
1526 g_hash_table_destroy (image
->array_cache
);
1528 if (image
->szarray_cache
)
1529 g_hash_table_destroy (image
->szarray_cache
);
1530 if (image
->ptr_cache
)
1531 g_hash_table_destroy (image
->ptr_cache
);
1532 if (image
->name_cache
) {
1533 g_hash_table_foreach (image
->name_cache
, free_hash_table
, NULL
);
1534 g_hash_table_destroy (image
->name_cache
);
1537 free_hash (image
->native_wrapper_cache
);
1538 free_hash (image
->managed_wrapper_cache
);
1539 free_hash (image
->delegate_begin_invoke_cache
);
1540 free_hash (image
->delegate_end_invoke_cache
);
1541 free_hash (image
->delegate_invoke_cache
);
1542 free_hash (image
->delegate_abstract_invoke_cache
);
1543 if (image
->remoting_invoke_cache
)
1544 g_hash_table_foreach (image
->remoting_invoke_cache
, free_remoting_wrappers
, NULL
);
1545 free_hash (image
->remoting_invoke_cache
);
1546 free_hash (image
->runtime_invoke_cache
);
1547 free_hash (image
->runtime_invoke_direct_cache
);
1548 free_hash (image
->runtime_invoke_vcall_cache
);
1549 free_hash (image
->synchronized_cache
);
1550 free_hash (image
->unbox_wrapper_cache
);
1551 free_hash (image
->cominterop_invoke_cache
);
1552 free_hash (image
->cominterop_wrapper_cache
);
1553 free_hash (image
->typespec_cache
);
1554 free_hash (image
->ldfld_wrapper_cache
);
1555 free_hash (image
->ldflda_wrapper_cache
);
1556 free_hash (image
->stfld_wrapper_cache
);
1557 free_hash (image
->isinst_cache
);
1558 free_hash (image
->castclass_cache
);
1559 free_hash (image
->proxy_isinst_cache
);
1560 free_hash (image
->thunk_invoke_cache
);
1562 /* The ownership of signatures is not well defined */
1563 //g_hash_table_foreach (image->memberref_signatures, free_mr_signatures, NULL);
1564 g_hash_table_destroy (image
->memberref_signatures
);
1565 //g_hash_table_foreach (image->helper_signatures, free_mr_signatures, NULL);
1566 g_hash_table_destroy (image
->helper_signatures
);
1567 g_hash_table_destroy (image
->method_signatures
);
1569 if (image
->generic_class_cache
)
1570 g_hash_table_destroy (image
->generic_class_cache
);
1572 if (image
->rgctx_template_hash
)
1573 g_hash_table_destroy (image
->rgctx_template_hash
);
1575 if (image
->property_hash
)
1576 mono_property_hash_destroy (image
->property_hash
);
1578 g_slist_free (image
->reflection_info_unregister_classes
);
1580 if (image
->interface_bitset
) {
1581 mono_unload_interface_ids (image
->interface_bitset
);
1582 mono_bitset_free (image
->interface_bitset
);
1584 if (image
->image_info
){
1585 MonoCLIImageInfo
*ii
= image
->image_info
;
1587 if (ii
->cli_section_tables
)
1588 g_free (ii
->cli_section_tables
);
1589 if (ii
->cli_sections
)
1590 g_free (ii
->cli_sections
);
1591 g_free (image
->image_info
);
1594 for (i
= 0; i
< image
->module_count
; ++i
) {
1595 if (image
->modules
[i
]) {
1596 if (!mono_image_close_except_pools (image
->modules
[i
]))
1597 image
->modules
[i
] = NULL
;
1600 if (image
->modules_loaded
)
1601 g_free (image
->modules_loaded
);
1603 DeleteCriticalSection (&image
->szarray_cache_lock
);
1604 DeleteCriticalSection (&image
->lock
);
1606 /*g_print ("destroy image %p (dynamic: %d)\n", image, image->dynamic);*/
1607 if (image
->dynamic
) {
1608 /* Dynamic images are GC_MALLOCed */
1609 g_free ((char*)image
->module_name
);
1610 mono_dynamic_image_free ((MonoDynamicImage
*)image
);
1613 mono_profiler_module_event (image
, MONO_PROFILE_END_UNLOAD
);
1619 mono_image_close_finish (MonoImage
*image
)
1623 if (image
->references
&& !image
->dynamic
) {
1624 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_ASSEMBLYREF
];
1627 for (i
= 0; i
< t
->rows
; i
++) {
1628 if (image
->references
[i
])
1629 mono_assembly_close_finish (image
->references
[i
]);
1632 g_free (image
->references
);
1633 image
->references
= NULL
;
1636 for (i
= 0; i
< image
->module_count
; ++i
) {
1637 if (image
->modules
[i
])
1638 mono_image_close_finish (image
->modules
[i
]);
1641 g_free (image
->modules
);
1643 mono_perfcounters
->loader_bytes
-= mono_mempool_get_allocated (image
->mempool
);
1645 if (!image
->dynamic
) {
1646 if (debug_assembly_unload
)
1647 mono_mempool_invalidate (image
->mempool
);
1649 mono_mempool_destroy (image
->mempool
);
1653 if (debug_assembly_unload
)
1654 mono_mempool_invalidate (image
->mempool
);
1656 mono_mempool_destroy (image
->mempool
);
1662 * @image: The image file we wish to close
1664 * Closes an image file, deallocates all memory consumed and
1665 * unmaps all possible sections of the file
1668 mono_image_close (MonoImage
*image
)
1670 if (mono_image_close_except_pools (image
))
1671 mono_image_close_finish (image
);
1675 * mono_image_strerror:
1676 * @status: an code indicating the result from a recent operation
1678 * Returns: a string describing the error
1681 mono_image_strerror (MonoImageOpenStatus status
)
1686 case MONO_IMAGE_ERROR_ERRNO
:
1687 return strerror (errno
);
1688 case MONO_IMAGE_IMAGE_INVALID
:
1689 return "File does not contain a valid CIL image";
1690 case MONO_IMAGE_MISSING_ASSEMBLYREF
:
1691 return "An assembly was referenced, but could not be found";
1693 return "Internal error";
1697 mono_image_walk_resource_tree (MonoCLIImageInfo
*info
, guint32 res_id
,
1698 guint32 lang_id
, gunichar2
*name
,
1699 MonoPEResourceDirEntry
*entry
,
1700 MonoPEResourceDir
*root
, guint32 level
)
1702 gboolean is_string
, is_dir
;
1703 guint32 name_offset
, dir_offset
;
1705 /* Level 0 holds a directory entry for each type of resource
1706 * (identified by ID or name).
1708 * Level 1 holds a directory entry for each named resource
1709 * item, and each "anonymous" item of a particular type of
1712 * Level 2 holds a directory entry for each language pointing to
1715 is_string
= MONO_PE_RES_DIR_ENTRY_NAME_IS_STRING (*entry
);
1716 name_offset
= MONO_PE_RES_DIR_ENTRY_NAME_OFFSET (*entry
);
1718 is_dir
= MONO_PE_RES_DIR_ENTRY_IS_DIR (*entry
);
1719 dir_offset
= MONO_PE_RES_DIR_ENTRY_DIR_OFFSET (*entry
);
1724 } else if (level
==1) {
1725 if (res_id
!= name_offset
)
1729 is_string
==TRUE
&& name
!=lookup (name_offset
)) {
1733 } else if (level
==2) {
1734 if (is_string
== TRUE
|| (is_string
== FALSE
&& lang_id
!= 0 && name_offset
!= lang_id
))
1737 g_assert_not_reached ();
1741 MonoPEResourceDir
*res_dir
=(MonoPEResourceDir
*)(((char *)root
)+dir_offset
);
1742 MonoPEResourceDirEntry
*sub_entries
=(MonoPEResourceDirEntry
*)(res_dir
+1);
1745 entries
= GUINT16_FROM_LE (res_dir
->res_named_entries
) + GUINT16_FROM_LE (res_dir
->res_id_entries
);
1747 for(i
=0; i
<entries
; i
++) {
1748 MonoPEResourceDirEntry
*sub_entry
=&sub_entries
[i
];
1751 ret
=mono_image_walk_resource_tree (info
, res_id
,
1762 MonoPEResourceDataEntry
*data_entry
=(MonoPEResourceDataEntry
*)((char *)(root
)+dir_offset
);
1763 MonoPEResourceDataEntry
*res
;
1765 res
= g_new0 (MonoPEResourceDataEntry
, 1);
1767 res
->rde_data_offset
= GUINT32_TO_LE (data_entry
->rde_data_offset
);
1768 res
->rde_size
= GUINT32_TO_LE (data_entry
->rde_size
);
1769 res
->rde_codepage
= GUINT32_TO_LE (data_entry
->rde_codepage
);
1770 res
->rde_reserved
= GUINT32_TO_LE (data_entry
->rde_reserved
);
1777 * mono_image_lookup_resource:
1778 * @image: the image to look up the resource in
1779 * @res_id: A MONO_PE_RESOURCE_ID_ that represents the resource ID to lookup.
1780 * @lang_id: The language id.
1781 * @name: the resource name to lookup.
1783 * Returns: NULL if not found, otherwise a pointer to the in-memory representation
1784 * of the given resource. The caller should free it using g_free () when no longer
1788 mono_image_lookup_resource (MonoImage
*image
, guint32 res_id
, guint32 lang_id
, gunichar2
*name
)
1790 MonoCLIImageInfo
*info
;
1791 MonoDotNetHeader
*header
;
1792 MonoPEDatadir
*datadir
;
1793 MonoPEDirEntry
*rsrc
;
1794 MonoPEResourceDir
*resource_dir
;
1795 MonoPEResourceDirEntry
*res_entries
;
1802 mono_image_ensure_section_idx (image
, MONO_SECTION_RSRC
);
1804 info
=image
->image_info
;
1809 header
=&info
->cli_header
;
1814 datadir
=&header
->datadir
;
1819 rsrc
=&datadir
->pe_resource_table
;
1824 resource_dir
=(MonoPEResourceDir
*)mono_image_rva_map (image
, rsrc
->rva
);
1825 if(resource_dir
==NULL
) {
1829 entries
= GUINT16_FROM_LE (resource_dir
->res_named_entries
) + GUINT16_FROM_LE (resource_dir
->res_id_entries
);
1830 res_entries
=(MonoPEResourceDirEntry
*)(resource_dir
+1);
1832 for(i
=0; i
<entries
; i
++) {
1833 MonoPEResourceDirEntry
*entry
=&res_entries
[i
];
1836 ret
=mono_image_walk_resource_tree (info
, res_id
, lang_id
,
1837 name
, entry
, resource_dir
,
1848 * mono_image_get_entry_point:
1849 * @image: the image where the entry point will be looked up.
1851 * Use this routine to determine the metadata token for method that
1852 * has been flagged as the entry point.
1854 * Returns: the token for the entry point method in the image
1857 mono_image_get_entry_point (MonoImage
*image
)
1859 return ((MonoCLIImageInfo
*)image
->image_info
)->cli_cli_header
.ch_entry_point
;
1863 * mono_image_get_resource:
1864 * @image: the image where the resource will be looked up.
1865 * @offset: The offset to add to the resource
1866 * @size: a pointer to an int where the size of the resource will be stored
1868 * This is a low-level routine that fetches a resource from the
1869 * metadata that starts at a given @offset. The @size parameter is
1870 * filled with the data field as encoded in the metadata.
1872 * Returns: the pointer to the resource whose offset is @offset.
1875 mono_image_get_resource (MonoImage
*image
, guint32 offset
, guint32
*size
)
1877 MonoCLIImageInfo
*iinfo
= image
->image_info
;
1878 MonoCLIHeader
*ch
= &iinfo
->cli_cli_header
;
1881 if (!ch
->ch_resources
.rva
|| offset
+ 4 > ch
->ch_resources
.size
)
1884 data
= mono_image_rva_map (image
, ch
->ch_resources
.rva
);
1889 *size
= read32 (data
);
1895 mono_image_load_file_for_image (MonoImage
*image
, int fileidx
)
1897 char *base_dir
, *name
;
1899 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_FILE
];
1903 if (fileidx
< 1 || fileidx
> t
->rows
)
1906 mono_loader_lock ();
1907 if (image
->files
&& image
->files
[fileidx
- 1]) {
1908 mono_loader_unlock ();
1909 return image
->files
[fileidx
- 1];
1913 image
->files
= g_new0 (MonoImage
*, t
->rows
);
1915 fname_id
= mono_metadata_decode_row_col (t
, fileidx
- 1, MONO_FILE_NAME
);
1916 fname
= mono_metadata_string_heap (image
, fname_id
);
1917 base_dir
= g_path_get_dirname (image
->name
);
1918 name
= g_build_filename (base_dir
, fname
, NULL
);
1919 res
= mono_image_open (name
, NULL
);
1922 /* g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly); */
1923 res
->assembly
= image
->assembly
;
1924 for (i
= 0; i
< res
->module_count
; ++i
) {
1925 if (res
->modules
[i
] && !res
->modules
[i
]->assembly
)
1926 res
->modules
[i
]->assembly
= image
->assembly
;
1929 image
->files
[fileidx
- 1] = res
;
1931 if (res
->is_module_handle
)
1932 mono_image_fixup_vtable (res
);
1935 mono_loader_unlock ();
1942 * mono_image_get_strong_name:
1943 * @image: a MonoImage
1944 * @size: a guint32 pointer, or NULL.
1946 * If the image has a strong name, and @size is not NULL, the value
1947 * pointed to by size will have the size of the strong name.
1949 * Returns: NULL if the image does not have a strong name, or a
1950 * pointer to the public key.
1953 mono_image_get_strong_name (MonoImage
*image
, guint32
*size
)
1955 MonoCLIImageInfo
*iinfo
= image
->image_info
;
1956 MonoPEDirEntry
*de
= &iinfo
->cli_cli_header
.ch_strong_name
;
1959 if (!de
->size
|| !de
->rva
)
1961 data
= mono_image_rva_map (image
, de
->rva
);
1970 * mono_image_strong_name_position:
1971 * @image: a MonoImage
1972 * @size: a guint32 pointer, or NULL.
1974 * If the image has a strong name, and @size is not NULL, the value
1975 * pointed to by size will have the size of the strong name.
1977 * Returns: the position within the image file where the strong name
1981 mono_image_strong_name_position (MonoImage
*image
, guint32
*size
)
1983 MonoCLIImageInfo
*iinfo
= image
->image_info
;
1984 MonoPEDirEntry
*de
= &iinfo
->cli_cli_header
.ch_strong_name
;
1989 if (!de
->size
|| !de
->rva
)
1991 pos
= mono_cli_rva_image_map (image
, de
->rva
);
1992 return pos
== INVALID_ADDRESS
? 0 : pos
;
1996 * mono_image_get_public_key:
1997 * @image: a MonoImage
1998 * @size: a guint32 pointer, or NULL.
2000 * This is used to obtain the public key in the @image.
2002 * If the image has a public key, and @size is not NULL, the value
2003 * pointed to by size will have the size of the public key.
2005 * Returns: NULL if the image does not have a public key, or a pointer
2006 * to the public key.
2009 mono_image_get_public_key (MonoImage
*image
, guint32
*size
)
2014 if (image
->dynamic
) {
2016 *size
= ((MonoDynamicImage
*)image
)->public_key_len
;
2017 return (char*)((MonoDynamicImage
*)image
)->public_key
;
2019 if (image
->tables
[MONO_TABLE_ASSEMBLY
].rows
!= 1)
2021 tok
= mono_metadata_decode_row_col (&image
->tables
[MONO_TABLE_ASSEMBLY
], 0, MONO_ASSEMBLY_PUBLIC_KEY
);
2024 pubkey
= mono_metadata_blob_heap (image
, tok
);
2025 len
= mono_metadata_decode_blob_size (pubkey
, &pubkey
);
2032 * mono_image_get_name:
2033 * @name: a MonoImage
2035 * Returns: the name of the assembly.
2038 mono_image_get_name (MonoImage
*image
)
2040 return image
->assembly_name
;
2044 * mono_image_get_filename:
2045 * @image: a MonoImage
2047 * Used to get the filename that hold the actual MonoImage
2049 * Returns: the filename.
2052 mono_image_get_filename (MonoImage
*image
)
2058 mono_image_get_guid (MonoImage
*image
)
2063 const MonoTableInfo
*
2064 mono_image_get_table_info (MonoImage
*image
, int table_id
)
2066 if (table_id
< 0 || table_id
>= MONO_TABLE_NUM
)
2068 return &image
->tables
[table_id
];
2072 mono_image_get_table_rows (MonoImage
*image
, int table_id
)
2074 if (table_id
< 0 || table_id
>= MONO_TABLE_NUM
)
2076 return image
->tables
[table_id
].rows
;
2080 mono_table_info_get_rows (const MonoTableInfo
*table
)
2086 * mono_image_get_assembly:
2087 * @image: the MonoImage.
2089 * Use this routine to get the assembly that owns this image.
2091 * Returns: the assembly that holds this image.
2094 mono_image_get_assembly (MonoImage
*image
)
2096 return image
->assembly
;
2100 * mono_image_is_dynamic:
2101 * @image: the MonoImage
2103 * Determines if the given image was created dynamically through the
2104 * System.Reflection.Emit API
2106 * Returns: TRUE if the image was created dynamically, FALSE if not.
2109 mono_image_is_dynamic (MonoImage
*image
)
2111 return image
->dynamic
;
2115 * mono_image_has_authenticode_entry:
2116 * @image: the MonoImage
2118 * Use this routine to determine if the image has a Authenticode
2119 * Certificate Table.
2121 * Returns: TRUE if the image contains an authenticode entry in the PE
2125 mono_image_has_authenticode_entry (MonoImage
*image
)
2127 MonoCLIImageInfo
*iinfo
= image
->image_info
;
2128 MonoDotNetHeader
*header
= &iinfo
->cli_header
;
2129 MonoPEDirEntry
*de
= &header
->datadir
.pe_certificate_table
;
2130 // the Authenticode "pre" (non ASN.1) header is 8 bytes long
2131 return ((de
->rva
!= 0) && (de
->size
> 8));
2135 mono_image_alloc (MonoImage
*image
, guint size
)
2139 mono_perfcounters
->loader_bytes
+= size
;
2140 mono_image_lock (image
);
2141 res
= mono_mempool_alloc (image
->mempool
, size
);
2142 mono_image_unlock (image
);
2148 mono_image_alloc0 (MonoImage
*image
, guint size
)
2152 mono_perfcounters
->loader_bytes
+= size
;
2153 mono_image_lock (image
);
2154 res
= mono_mempool_alloc0 (image
->mempool
, size
);
2155 mono_image_unlock (image
);
2161 mono_image_strdup (MonoImage
*image
, const char *s
)
2165 mono_perfcounters
->loader_bytes
+= strlen (s
);
2166 mono_image_lock (image
);
2167 res
= mono_mempool_strdup (image
->mempool
, s
);
2168 mono_image_unlock (image
);
2174 g_list_prepend_image (MonoImage
*image
, GList
*list
, gpointer data
)
2178 new_list
= mono_image_alloc (image
, sizeof (GList
));
2179 new_list
->data
= data
;
2180 new_list
->prev
= list
? list
->prev
: NULL
;
2181 new_list
->next
= list
;
2184 new_list
->prev
->next
= new_list
;
2186 list
->prev
= new_list
;
2192 g_slist_append_image (MonoImage
*image
, GSList
*list
, gpointer data
)
2196 new_list
= mono_image_alloc (image
, sizeof (GSList
));
2197 new_list
->data
= data
;
2198 new_list
->next
= NULL
;
2200 return g_slist_concat (list
, new_list
);
2204 mono_image_lock (MonoImage
*image
)
2206 mono_locks_acquire (&image
->lock
, ImageDataLock
);
2210 mono_image_unlock (MonoImage
*image
)
2212 mono_locks_release (&image
->lock
, ImageDataLock
);
2217 * mono_image_property_lookup:
2219 * Lookup a property on @image. Used to store very rare fields of MonoClass and MonoMethod.
2221 * LOCKING: Takes the image lock
2224 mono_image_property_lookup (MonoImage
*image
, gpointer subject
, guint32 property
)
2228 mono_image_lock (image
);
2229 res
= mono_property_hash_lookup (image
->property_hash
, subject
, property
);
2230 mono_image_unlock (image
);
2236 * mono_image_property_insert:
2238 * Insert a new property @property with value @value on @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
2240 * LOCKING: Takes the image lock
2243 mono_image_property_insert (MonoImage
*image
, gpointer subject
, guint32 property
, gpointer value
)
2245 mono_image_lock (image
);
2246 mono_property_hash_insert (image
->property_hash
, subject
, property
, value
);
2247 mono_image_unlock (image
);
2251 * mono_image_property_remove:
2253 * Remove all properties associated with @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
2255 * LOCKING: Takes the image lock
2258 mono_image_property_remove (MonoImage
*image
, gpointer subject
)
2260 mono_image_lock (image
);
2261 mono_property_hash_remove_object (image
->property_hash
, subject
);
2262 mono_image_unlock (image
);