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 * (C) 2001-2003 Ximian, Inc. http://www.ximian.com
20 #include "rawbuffer.h"
21 #include "mono-endian.h"
22 #include "tabledefs.h"
23 #include "tokentype.h"
24 #include "metadata-internals.h"
26 #include <mono/io-layer/io-layer.h>
27 #include <mono/utils/mono-logger.h>
28 #include <mono/utils/mono-path.h>
29 #include <mono/metadata/class-internals.h>
30 #include <sys/types.h>
36 #define INVALID_ADDRESS 0xffffffff
39 * Keeps track of the various assemblies loaded
41 static GHashTable *loaded_images_hash;
42 static GHashTable *loaded_images_guid_hash;
43 static GHashTable *loaded_images_refonly_hash;
44 static GHashTable *loaded_images_refonly_guid_hash;
46 static gboolean debug_assembly_unload = FALSE;
48 #define mono_images_lock() EnterCriticalSection (&images_mutex)
49 #define mono_images_unlock() LeaveCriticalSection (&images_mutex)
50 static CRITICAL_SECTION images_mutex;
53 mono_cli_rva_image_map (MonoCLIImageInfo *iinfo, guint32 addr)
55 const int top = iinfo->cli_section_count;
56 MonoSectionTable *tables = iinfo->cli_section_tables;
59 for (i = 0; i < top; i++){
60 if ((addr >= tables->st_virtual_address) &&
61 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
62 return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
66 return INVALID_ADDRESS;
70 * mono_images_rva_map:
72 * @addr: relative virtual address (RVA)
74 * This is a low-level routine used by the runtime to map relative
75 * virtual address (RVA) into their location in memory.
77 * Returns: the address in memory for the given RVA, or NULL if the
78 * RVA is not valid for this image.
81 mono_image_rva_map (MonoImage *image, guint32 addr)
83 MonoCLIImageInfo *iinfo = image->image_info;
84 const int top = iinfo->cli_section_count;
85 MonoSectionTable *tables = iinfo->cli_section_tables;
88 for (i = 0; i < top; i++){
89 if ((addr >= tables->st_virtual_address) &&
90 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
91 if (!iinfo->cli_sections [i]) {
92 if (!mono_image_ensure_section_idx (image, i))
95 return (char*)iinfo->cli_sections [i] +
96 (addr - tables->st_virtual_address);
106 * Initialize the global variables used by this module.
109 mono_images_init (void)
111 InitializeCriticalSection (&images_mutex);
113 loaded_images_hash = g_hash_table_new (g_str_hash, g_str_equal);
114 loaded_images_guid_hash = g_hash_table_new (g_str_hash, g_str_equal);
115 loaded_images_refonly_hash = g_hash_table_new (g_str_hash, g_str_equal);
116 loaded_images_refonly_guid_hash = g_hash_table_new (g_str_hash, g_str_equal);
118 debug_assembly_unload = getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
122 * mono_images_cleanup:
124 * Free all resources used by this module.
127 mono_images_cleanup (void)
129 DeleteCriticalSection (&images_mutex);
131 g_hash_table_destroy (loaded_images_hash);
132 g_hash_table_destroy (loaded_images_guid_hash);
133 g_hash_table_destroy (loaded_images_refonly_hash);
134 g_hash_table_destroy (loaded_images_refonly_guid_hash);
138 * mono_image_ensure_section_idx:
139 * @image: The image we are operating on
140 * @section: section number that we will load/map into memory
142 * This routine makes sure that we have an in-memory copy of
143 * an image section (.text, .rsrc, .data).
145 * Returns: TRUE on success
148 mono_image_ensure_section_idx (MonoImage *image, int section)
150 MonoCLIImageInfo *iinfo = image->image_info;
151 MonoSectionTable *sect;
154 g_return_val_if_fail (section < iinfo->cli_section_count, FALSE);
156 if (iinfo->cli_sections [section] != NULL)
159 sect = &iinfo->cli_section_tables [section];
161 writable = sect->st_flags & SECT_FLAGS_MEM_WRITE;
163 if (sect->st_raw_data_ptr + sect->st_raw_data_size > image->raw_data_len)
165 /* FIXME: we ignore the writable flag since we don't patch the binary */
166 iinfo->cli_sections [section] = image->raw_data + sect->st_raw_data_ptr;
171 * mono_image_ensure_section:
172 * @image: The image we are operating on
173 * @section: section name that we will load/map into memory
175 * This routine makes sure that we have an in-memory copy of
176 * an image section (.text, .rsrc, .data).
178 * Returns: TRUE on success
181 mono_image_ensure_section (MonoImage *image, const char *section)
183 MonoCLIImageInfo *ii = image->image_info;
186 for (i = 0; i < ii->cli_section_count; i++){
187 if (strncmp (ii->cli_section_tables [i].st_name, section, 8) != 0)
190 return mono_image_ensure_section_idx (image, i);
196 load_section_tables (MonoImage *image, MonoCLIImageInfo *iinfo, guint32 offset)
198 const int top = iinfo->cli_header.coff.coff_sections;
201 iinfo->cli_section_count = top;
202 iinfo->cli_section_tables = g_new0 (MonoSectionTable, top);
203 iinfo->cli_sections = g_new0 (void *, top);
205 for (i = 0; i < top; i++){
206 MonoSectionTable *t = &iinfo->cli_section_tables [i];
208 if (offset + sizeof (MonoSectionTable) > image->raw_data_len)
210 memcpy (t, image->raw_data + offset, sizeof (MonoSectionTable));
211 offset += sizeof (MonoSectionTable);
213 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
214 t->st_virtual_size = GUINT32_FROM_LE (t->st_virtual_size);
215 t->st_virtual_address = GUINT32_FROM_LE (t->st_virtual_address);
216 t->st_raw_data_size = GUINT32_FROM_LE (t->st_raw_data_size);
217 t->st_raw_data_ptr = GUINT32_FROM_LE (t->st_raw_data_ptr);
218 t->st_reloc_ptr = GUINT32_FROM_LE (t->st_reloc_ptr);
219 t->st_lineno_ptr = GUINT32_FROM_LE (t->st_lineno_ptr);
220 t->st_reloc_count = GUINT16_FROM_LE (t->st_reloc_count);
221 t->st_line_count = GUINT16_FROM_LE (t->st_line_count);
222 t->st_flags = GUINT32_FROM_LE (t->st_flags);
224 /* consistency checks here */
231 load_cli_header (MonoImage *image, MonoCLIImageInfo *iinfo)
235 offset = mono_cli_rva_image_map (iinfo, iinfo->cli_header.datadir.pe_cli_header.rva);
236 if (offset == INVALID_ADDRESS)
239 if (offset + sizeof (MonoCLIHeader) > image->raw_data_len)
241 memcpy (&iinfo->cli_cli_header, image->raw_data + offset, sizeof (MonoCLIHeader));
243 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
244 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
245 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
246 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
247 SWAP32 (iinfo->cli_cli_header.ch_size);
248 SWAP32 (iinfo->cli_cli_header.ch_flags);
249 SWAP32 (iinfo->cli_cli_header.ch_entry_point);
250 SWAP16 (iinfo->cli_cli_header.ch_runtime_major);
251 SWAP16 (iinfo->cli_cli_header.ch_runtime_minor);
252 SWAPPDE (iinfo->cli_cli_header.ch_metadata);
253 SWAPPDE (iinfo->cli_cli_header.ch_resources);
254 SWAPPDE (iinfo->cli_cli_header.ch_strong_name);
255 SWAPPDE (iinfo->cli_cli_header.ch_code_manager_table);
256 SWAPPDE (iinfo->cli_cli_header.ch_vtable_fixups);
257 SWAPPDE (iinfo->cli_cli_header.ch_export_address_table_jumps);
258 SWAPPDE (iinfo->cli_cli_header.ch_eeinfo_table);
259 SWAPPDE (iinfo->cli_cli_header.ch_helper_table);
260 SWAPPDE (iinfo->cli_cli_header.ch_dynamic_info);
261 SWAPPDE (iinfo->cli_cli_header.ch_delay_load_info);
262 SWAPPDE (iinfo->cli_cli_header.ch_module_image);
263 SWAPPDE (iinfo->cli_cli_header.ch_external_fixups);
264 SWAPPDE (iinfo->cli_cli_header.ch_ridmap);
265 SWAPPDE (iinfo->cli_cli_header.ch_debug_map);
266 SWAPPDE (iinfo->cli_cli_header.ch_ip_map);
271 /* Catch new uses of the fields that are supposed to be zero */
273 if ((iinfo->cli_cli_header.ch_eeinfo_table.rva != 0) ||
274 (iinfo->cli_cli_header.ch_helper_table.rva != 0) ||
275 (iinfo->cli_cli_header.ch_dynamic_info.rva != 0) ||
276 (iinfo->cli_cli_header.ch_delay_load_info.rva != 0) ||
277 (iinfo->cli_cli_header.ch_module_image.rva != 0) ||
278 (iinfo->cli_cli_header.ch_external_fixups.rva != 0) ||
279 (iinfo->cli_cli_header.ch_ridmap.rva != 0) ||
280 (iinfo->cli_cli_header.ch_debug_map.rva != 0) ||
281 (iinfo->cli_cli_header.ch_ip_map.rva != 0)){
284 * No need to scare people who are testing this, I am just
285 * labelling this as a LAMESPEC
287 /* g_warning ("Some fields in the CLI header which should have been zero are not zero"); */
295 load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
297 guint32 offset, size;
303 offset = mono_cli_rva_image_map (iinfo, iinfo->cli_cli_header.ch_metadata.rva);
304 if (offset == INVALID_ADDRESS)
307 size = iinfo->cli_cli_header.ch_metadata.size;
309 if (offset + size > image->raw_data_len)
311 image->raw_metadata = image->raw_data + offset;
313 ptr = image->raw_metadata;
315 if (strncmp (ptr, "BSJB", 4) == 0){
316 guint32 version_string_len;
319 image->md_version_major = read16 (ptr);
321 image->md_version_minor = read16 (ptr);
324 version_string_len = read32 (ptr);
326 image->version = g_strndup (ptr, version_string_len);
327 ptr += version_string_len;
328 pad = ptr - image->raw_metadata;
330 ptr += 4 - (pad % 4);
334 /* skip over flags */
337 streams = read16 (ptr);
340 for (i = 0; i < streams; i++){
341 if (strncmp (ptr + 8, "#~", 3) == 0){
342 image->heap_tables.data = image->raw_metadata + read32 (ptr);
343 image->heap_tables.size = read32 (ptr + 4);
345 } else if (strncmp (ptr + 8, "#Strings", 9) == 0){
346 image->heap_strings.data = image->raw_metadata + read32 (ptr);
347 image->heap_strings.size = read32 (ptr + 4);
349 } else if (strncmp (ptr + 8, "#US", 4) == 0){
350 image->heap_us.data = image->raw_metadata + read32 (ptr);
351 image->heap_us.size = read32 (ptr + 4);
353 } else if (strncmp (ptr + 8, "#Blob", 6) == 0){
354 image->heap_blob.data = image->raw_metadata + read32 (ptr);
355 image->heap_blob.size = read32 (ptr + 4);
357 } else if (strncmp (ptr + 8, "#GUID", 6) == 0){
358 image->heap_guid.data = image->raw_metadata + read32 (ptr);
359 image->heap_guid.size = read32 (ptr + 4);
361 } else if (strncmp (ptr + 8, "#-", 3) == 0) {
362 image->heap_tables.data = image->raw_metadata + read32 (ptr);
363 image->heap_tables.size = read32 (ptr + 4);
365 image->uncompressed_metadata = TRUE;
366 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);
368 g_message ("Unknown heap type: %s\n", ptr + 8);
369 ptr += 8 + strlen (ptr + 8) + 1;
371 pad = ptr - image->raw_metadata;
373 ptr += 4 - (pad % 4);
376 g_assert (image->heap_guid.data);
377 g_assert (image->heap_guid.size >= 16);
379 image->guid = mono_guid_to_string ((guint8*)image->heap_guid.data);
385 * Load representation of logical metadata tables, from the "#~" stream
388 load_tables (MonoImage *image)
390 const char *heap_tables = image->heap_tables.data;
392 guint64 valid_mask, sorted_mask;
393 int valid = 0, table;
396 heap_sizes = heap_tables [6];
397 image->idx_string_wide = ((heap_sizes & 0x01) == 1);
398 image->idx_guid_wide = ((heap_sizes & 0x02) == 2);
399 image->idx_blob_wide = ((heap_sizes & 0x04) == 4);
401 valid_mask = read64 (heap_tables + 8);
402 sorted_mask = read64 (heap_tables + 16);
403 rows = (const guint32 *) (heap_tables + 24);
405 for (table = 0; table < 64; table++){
406 if ((valid_mask & ((guint64) 1 << table)) == 0){
407 if (table > MONO_TABLE_LAST)
409 image->tables [table].rows = 0;
412 if (table > MONO_TABLE_LAST) {
413 g_warning("bits in valid must be zero above 0x2d (II - 23.1.6)");
415 image->tables [table].rows = read32 (rows);
417 /*if ((sorted_mask & ((guint64) 1 << table)) == 0){
418 g_print ("table %s (0x%02x) is sorted\n", mono_meta_table_name (table), table);
424 image->tables_base = (heap_tables + 24) + (4 * valid);
426 /* They must be the same */
427 g_assert ((const void *) image->tables_base == (const void *) rows);
429 mono_metadata_compute_table_bases (image);
434 load_metadata (MonoImage *image, MonoCLIImageInfo *iinfo)
436 if (!load_metadata_ptrs (image, iinfo))
439 return load_tables (image);
443 mono_image_check_for_module_cctor (MonoImage *image)
445 MonoTableInfo *t, *mt;
446 t = &image->tables [MONO_TABLE_TYPEDEF];
447 mt = &image->tables [MONO_TABLE_METHOD];
448 if (mono_get_runtime_info ()->framework_version [0] == '1') {
449 image->checked_module_cctor = TRUE;
452 if (image->dynamic) {
454 image->checked_module_cctor = TRUE;
458 guint32 nameidx = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_NAME);
459 const char *name = mono_metadata_string_heap (image, nameidx);
460 if (strcmp (name, "<Module>") == 0) {
461 guint32 first_method = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_METHOD_LIST) - 1;
464 last_method = mono_metadata_decode_row_col (t, 1, MONO_TYPEDEF_METHOD_LIST) - 1;
466 last_method = mt->rows;
467 for (; first_method < last_method; first_method++) {
468 nameidx = mono_metadata_decode_row_col (mt, first_method, MONO_METHOD_NAME);
469 name = mono_metadata_string_heap (image, nameidx);
470 if (strcmp (name, ".cctor") == 0) {
471 image->has_module_cctor = TRUE;
472 image->checked_module_cctor = TRUE;
478 image->has_module_cctor = FALSE;
479 image->checked_module_cctor = TRUE;
483 load_modules (MonoImage *image)
490 t = &image->tables [MONO_TABLE_MODULEREF];
491 image->modules = g_new0 (MonoImage *, t->rows);
492 image->modules_loaded = g_new0 (gboolean, t->rows);
493 image->module_count = t->rows;
497 * mono_image_load_module:
499 * Load the module with the one-based index IDX from IMAGE and return it. Return NULL if
500 * it cannot be loaded.
503 mono_image_load_module (MonoImage *image, int idx)
506 MonoTableInfo *file_table;
509 gboolean refonly = image->ref_only;
510 GList *list_iter, *valid_modules = NULL;
511 MonoImageOpenStatus status;
513 g_assert (idx <= image->module_count);
514 if (image->modules_loaded [idx - 1])
515 return image->modules [idx - 1];
517 file_table = &image->tables [MONO_TABLE_FILE];
518 for (i = 0; i < file_table->rows; i++) {
519 guint32 cols [MONO_FILE_SIZE];
520 mono_metadata_decode_row (file_table, i, cols, MONO_FILE_SIZE);
521 if (cols [MONO_FILE_FLAGS] == FILE_CONTAINS_NO_METADATA)
523 valid_modules = g_list_prepend (valid_modules, (char*)mono_metadata_string_heap (image, cols [MONO_FILE_NAME]));
526 t = &image->tables [MONO_TABLE_MODULEREF];
527 base_dir = g_path_get_dirname (image->name);
532 guint32 cols [MONO_MODULEREF_SIZE];
533 /* if there is no file table, we try to load the module... */
534 int valid = file_table->rows == 0;
536 mono_metadata_decode_row (t, idx - 1, cols, MONO_MODULEREF_SIZE);
537 name = mono_metadata_string_heap (image, cols [MONO_MODULEREF_NAME]);
538 for (list_iter = valid_modules; list_iter; list_iter = list_iter->next) {
539 /* be safe with string dups, but we could just compare string indexes */
540 if (strcmp (list_iter->data, name) == 0) {
546 module_ref = g_build_filename (base_dir, name, NULL);
547 image->modules [idx - 1] = mono_image_open_full (module_ref, &status, refonly);
548 if (image->modules [idx - 1]) {
549 mono_image_addref (image->modules [idx - 1]);
550 image->modules [idx - 1]->assembly = image->assembly;
551 /* g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly); */
557 image->modules_loaded [idx - 1] = TRUE;
560 g_list_free (valid_modules);
562 return image->modules [idx - 1];
566 register_guid (gpointer key, gpointer value, gpointer user_data)
568 MonoImage *image = (MonoImage*)value;
570 if (!g_hash_table_lookup (loaded_images_guid_hash, image))
571 g_hash_table_insert (loaded_images_guid_hash, image->guid, image);
575 build_guid_table (gboolean refonly)
577 GHashTable *loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
578 g_hash_table_foreach (loaded_images, register_guid, NULL);
582 class_key_extract (gpointer value)
584 MonoClass *class = value;
586 return GUINT_TO_POINTER (class->type_token);
590 class_next_value (gpointer value)
592 MonoClass *class = value;
594 return (gpointer*)&class->next_class_cache;
598 mono_image_init (MonoImage *image)
600 image->mempool = mono_mempool_new ();
601 image->method_cache = g_hash_table_new (NULL, NULL);
602 mono_internal_hash_table_init (&image->class_cache,
606 image->field_cache = g_hash_table_new (NULL, NULL);
608 image->delegate_begin_invoke_cache =
609 g_hash_table_new ((GHashFunc)mono_signature_hash,
610 (GCompareFunc)mono_metadata_signature_equal);
611 image->delegate_end_invoke_cache =
612 g_hash_table_new ((GHashFunc)mono_signature_hash,
613 (GCompareFunc)mono_metadata_signature_equal);
614 image->delegate_invoke_cache =
615 g_hash_table_new ((GHashFunc)mono_signature_hash,
616 (GCompareFunc)mono_metadata_signature_equal);
617 image->runtime_invoke_cache =
618 g_hash_table_new ((GHashFunc)mono_signature_hash,
619 (GCompareFunc)mono_metadata_signature_equal);
621 image->managed_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
622 image->native_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
623 image->remoting_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
624 image->cominterop_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
625 image->cominterop_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
626 image->synchronized_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
627 image->unbox_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
629 image->ldfld_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
630 image->ldflda_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
631 image->ldfld_remote_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
632 image->stfld_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
633 image->stfld_remote_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
634 image->isinst_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
635 image->castclass_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
636 image->proxy_isinst_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
638 image->typespec_cache = g_hash_table_new (NULL, NULL);
639 image->memberref_signatures = g_hash_table_new (NULL, NULL);
640 image->helper_signatures = g_hash_table_new (g_str_hash, g_str_equal);
641 image->method_signatures = g_hash_table_new (NULL, NULL);
645 do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
646 gboolean care_about_cli)
648 MonoCLIImageInfo *iinfo;
649 MonoDotNetHeader *header;
650 MonoMSDOSHeader msdos;
653 mono_image_init (image);
655 iinfo = image->image_info;
656 header = &iinfo->cli_header;
659 *status = MONO_IMAGE_IMAGE_INVALID;
661 if (offset + sizeof (msdos) > image->raw_data_len)
663 memcpy (&msdos, image->raw_data + offset, sizeof (msdos));
665 if (!(msdos.msdos_sig [0] == 'M' && msdos.msdos_sig [1] == 'Z'))
668 msdos.pe_offset = GUINT32_FROM_LE (msdos.pe_offset);
670 offset = msdos.pe_offset;
672 if (offset + sizeof (MonoDotNetHeader) > image->raw_data_len)
674 memcpy (header, image->raw_data + offset, sizeof (MonoDotNetHeader));
675 offset += sizeof (MonoDotNetHeader);
677 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
678 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
679 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
680 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
681 SWAP32 (header->coff.coff_time);
682 SWAP32 (header->coff.coff_symptr);
683 SWAP32 (header->coff.coff_symcount);
684 SWAP16 (header->coff.coff_machine);
685 SWAP16 (header->coff.coff_sections);
686 SWAP16 (header->coff.coff_opt_header_size);
687 SWAP16 (header->coff.coff_attributes);
689 SWAP32 (header->pe.pe_code_size);
690 SWAP32 (header->pe.pe_data_size);
691 SWAP32 (header->pe.pe_uninit_data_size);
692 SWAP32 (header->pe.pe_rva_entry_point);
693 SWAP32 (header->pe.pe_rva_code_base);
694 SWAP32 (header->pe.pe_rva_data_base);
695 SWAP16 (header->pe.pe_magic);
697 /* MonoPEHeaderNT: not used yet */
698 SWAP32 (header->nt.pe_image_base); /* must be 0x400000 */
699 SWAP32 (header->nt.pe_section_align); /* must be 8192 */
700 SWAP32 (header->nt.pe_file_alignment); /* must be 512 or 4096 */
701 SWAP16 (header->nt.pe_os_major); /* must be 4 */
702 SWAP16 (header->nt.pe_os_minor); /* must be 0 */
703 SWAP16 (header->nt.pe_user_major);
704 SWAP16 (header->nt.pe_user_minor);
705 SWAP16 (header->nt.pe_subsys_major);
706 SWAP16 (header->nt.pe_subsys_minor);
707 SWAP32 (header->nt.pe_reserved_1);
708 SWAP32 (header->nt.pe_image_size);
709 SWAP32 (header->nt.pe_header_size);
710 SWAP32 (header->nt.pe_checksum);
711 SWAP16 (header->nt.pe_subsys_required);
712 SWAP16 (header->nt.pe_dll_flags);
713 SWAP32 (header->nt.pe_stack_reserve);
714 SWAP32 (header->nt.pe_stack_commit);
715 SWAP32 (header->nt.pe_heap_reserve);
716 SWAP32 (header->nt.pe_heap_commit);
717 SWAP32 (header->nt.pe_loader_flags);
718 SWAP32 (header->nt.pe_data_dir_count);
720 /* MonoDotNetHeader: mostly unused */
721 SWAPPDE (header->datadir.pe_export_table);
722 SWAPPDE (header->datadir.pe_import_table);
723 SWAPPDE (header->datadir.pe_resource_table);
724 SWAPPDE (header->datadir.pe_exception_table);
725 SWAPPDE (header->datadir.pe_certificate_table);
726 SWAPPDE (header->datadir.pe_reloc_table);
727 SWAPPDE (header->datadir.pe_debug);
728 SWAPPDE (header->datadir.pe_copyright);
729 SWAPPDE (header->datadir.pe_global_ptr);
730 SWAPPDE (header->datadir.pe_tls_table);
731 SWAPPDE (header->datadir.pe_load_config_table);
732 SWAPPDE (header->datadir.pe_bound_import);
733 SWAPPDE (header->datadir.pe_iat);
734 SWAPPDE (header->datadir.pe_delay_import_desc);
735 SWAPPDE (header->datadir.pe_cli_header);
736 SWAPPDE (header->datadir.pe_reserved);
743 if (header->coff.coff_machine != 0x14c)
746 if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader) - sizeof (MonoCOFFHeader) - 4))
749 if (header->pesig[0] != 'P' || header->pesig[1] != 'E' || header->pe.pe_magic != 0x10B)
754 * The spec says that this field should contain 6.0, but Visual Studio includes a new compiler,
755 * which produces binaries with 7.0. From Sergey:
757 * The reason is that MSVC7 uses traditional compile/link
758 * sequence for CIL executables, and VS.NET (and Framework
759 * SDK) includes linker version 7, that puts 7.0 in this
760 * field. That's why it's currently not possible to load VC
761 * binaries with Mono. This field is pretty much meaningless
762 * anyway (what linker?).
764 if (header->pe.pe_major != 6 || header->pe.pe_minor != 0)
769 * FIXME: byte swap all addresses here for header.
772 if (!load_section_tables (image, iinfo, offset))
775 if (care_about_cli == FALSE) {
779 /* Load the CLI header */
780 if (!load_cli_header (image, iinfo))
783 if (!load_metadata (image, iinfo))
786 /* modules don't have an assembly table row */
787 if (image->tables [MONO_TABLE_ASSEMBLY].rows)
788 image->assembly_name = mono_metadata_string_heap (image,
789 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY],
790 0, MONO_ASSEMBLY_NAME));
792 image->module_name = mono_metadata_string_heap (image,
793 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_MODULE],
794 0, MONO_MODULE_NAME));
796 load_modules (image);
800 *status = MONO_IMAGE_OK;
805 mono_image_close (image);
810 do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
811 gboolean care_about_cli, gboolean refonly)
813 MonoCLIImageInfo *iinfo;
816 struct stat stat_buf;
818 if ((filed = fopen (fname, "rb")) == NULL){
820 *status = MONO_IMAGE_ERROR_ERRNO;
824 if (fstat (fileno (filed), &stat_buf)) {
827 *status = MONO_IMAGE_ERROR_ERRNO;
830 image = g_new0 (MonoImage, 1);
831 image->file_descr = filed;
832 image->raw_data_len = stat_buf.st_size;
833 image->raw_data = mono_raw_buffer_load (fileno (filed), FALSE, 0, stat_buf.st_size);
834 iinfo = g_new0 (MonoCLIImageInfo, 1);
835 image->image_info = iinfo;
836 image->name = mono_path_resolve_symlinks (fname);
837 image->ref_only = refonly;
838 image->ref_count = 1;
840 return do_mono_image_load (image, status, care_about_cli);
844 mono_image_loaded_full (const char *name, gboolean refonly)
847 GHashTable *loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
850 res = g_hash_table_lookup (loaded_images, name);
851 mono_images_unlock ();
857 * @name: name of the image to load
859 * This routine ensures that the given image is loaded.
861 * Returns: the loaded MonoImage, or NULL on failure.
864 mono_image_loaded (const char *name)
866 return mono_image_loaded_full (name, FALSE);
870 mono_image_loaded_by_guid_full (const char *guid, gboolean refonly)
873 GHashTable *loaded_images = refonly ? loaded_images_refonly_guid_hash : loaded_images_guid_hash;
876 res = g_hash_table_lookup (loaded_images, guid);
877 mono_images_unlock ();
882 mono_image_loaded_by_guid (const char *guid)
884 return mono_image_loaded_by_guid_full (guid, FALSE);
888 register_image (MonoImage *image)
891 GHashTable *loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
894 image2 = g_hash_table_lookup (loaded_images, image->name);
897 /* Somebody else beat us to it */
898 mono_image_addref (image2);
899 mono_images_unlock ();
900 mono_image_close (image);
903 g_hash_table_insert (loaded_images, image->name, image);
904 if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == NULL))
905 g_hash_table_insert (loaded_images, (char *) image->assembly_name, image);
906 g_hash_table_insert (image->ref_only ? loaded_images_refonly_guid_hash : loaded_images_guid_hash, image->guid, image);
907 mono_images_unlock ();
913 mono_image_open_from_data_full (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status, gboolean refonly)
915 MonoCLIImageInfo *iinfo;
919 if (!data || !data_len) {
921 *status = MONO_IMAGE_IMAGE_INVALID;
926 datac = g_try_malloc (data_len);
929 *status = MONO_IMAGE_ERROR_ERRNO;
932 memcpy (datac, data, data_len);
935 image = g_new0 (MonoImage, 1);
936 image->raw_data = datac;
937 image->raw_data_len = data_len;
938 image->raw_data_allocated = need_copy;
939 image->name = g_strdup_printf ("data-%p", datac);
940 iinfo = g_new0 (MonoCLIImageInfo, 1);
941 image->image_info = iinfo;
942 image->ref_only = refonly;
944 image = do_mono_image_load (image, status, TRUE);
948 return register_image (image);
952 mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status)
954 return mono_image_open_from_data_full (data, data_len, need_copy, status, FALSE);
958 mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean refonly)
961 GHashTable *loaded_images;
964 g_return_val_if_fail (fname != NULL, NULL);
966 absfname = mono_path_canonicalize (fname);
969 * The easiest solution would be to do all the loading inside the mutex,
970 * but that would lead to scalability problems. So we let the loading
971 * happen outside the mutex, and if multiple threads happen to load
972 * the same image, we discard all but the first copy.
975 loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
976 image = g_hash_table_lookup (loaded_images, absfname);
980 mono_image_addref (image);
981 mono_images_unlock ();
984 mono_images_unlock ();
986 image = do_mono_image_open (fname, status, TRUE, refonly);
990 return register_image (image);
995 * @fname: filename that points to the module we want to open
996 * @status: An error condition is returned in this field
998 * Returns: An open image of type %MonoImage or NULL on error.
999 * The caller holds a temporary reference to the returned image which should be cleared
1000 * when no longer needed by calling mono_image_close ().
1001 * if NULL, then check the value of @status for details on the error
1004 mono_image_open (const char *fname, MonoImageOpenStatus *status)
1006 return mono_image_open_full (fname, status, FALSE);
1010 * mono_pe_file_open:
1011 * @fname: filename that points to the module we want to open
1012 * @status: An error condition is returned in this field
1014 * Returns: An open image of type %MonoImage or NULL on error. if
1015 * NULL, then check the value of @status for details on the error.
1016 * This variant for mono_image_open DOES NOT SET UP CLI METADATA.
1017 * It's just a PE file loader, used for FileVersionInfo. It also does
1018 * not use the image cache.
1021 mono_pe_file_open (const char *fname, MonoImageOpenStatus *status)
1023 g_return_val_if_fail (fname != NULL, NULL);
1025 return(do_mono_image_open (fname, status, FALSE, FALSE));
1029 free_hash_table (gpointer key, gpointer val, gpointer user_data)
1031 g_hash_table_destroy ((GHashTable*)val);
1036 free_mr_signatures (gpointer key, gpointer val, gpointer user_data)
1038 mono_metadata_free_method_signature ((MonoMethodSignature*)val);
1043 free_blob_cache_entry (gpointer key, gpointer val, gpointer user_data)
1049 free_remoting_wrappers (gpointer key, gpointer val, gpointer user_data)
1055 free_array_cache_entry (gpointer key, gpointer val, gpointer user_data)
1057 g_slist_free ((GSList*)val);
1061 * mono_image_addref:
1062 * @image: The image file we wish to add a reference to
1064 * Increases the reference count of an image.
1067 mono_image_addref (MonoImage *image)
1069 InterlockedIncrement (&image->ref_count);
1073 mono_dynamic_stream_reset (MonoDynamicStream* stream)
1075 stream->alloc_size = stream->index = stream->offset = 0;
1076 g_free (stream->data);
1077 stream->data = NULL;
1079 g_hash_table_destroy (stream->hash);
1080 stream->hash = NULL;
1086 * @image: The image file we wish to close
1088 * Closes an image file, deallocates all memory consumed and
1089 * unmaps all possible sections of the file
1092 mono_image_close (MonoImage *image)
1095 GHashTable *loaded_images, *loaded_images_guid;
1098 g_return_if_fail (image != NULL);
1100 if (InterlockedDecrement (&image->ref_count) > 0)
1103 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading image %s [%p].", image->name, image);
1105 mono_metadata_clean_for_image (image);
1107 mono_images_lock ();
1108 loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
1109 loaded_images_guid = image->ref_only ? loaded_images_refonly_guid_hash : loaded_images_guid_hash;
1110 image2 = g_hash_table_lookup (loaded_images, image->name);
1111 if (image == image2) {
1112 /* This is not true if we are called from mono_image_open () */
1113 g_hash_table_remove (loaded_images, image->name);
1114 g_hash_table_remove (loaded_images_guid, image->guid);
1116 if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == image))
1117 g_hash_table_remove (loaded_images, (char *) image->assembly_name);
1119 /* Multiple images might have the same guid */
1120 build_guid_table (image->ref_only);
1122 mono_images_unlock ();
1124 if (image->file_descr) {
1125 fclose (image->file_descr);
1126 image->file_descr = NULL;
1127 if (image->raw_data != NULL)
1128 mono_raw_buffer_free (image->raw_data);
1131 if (image->raw_data_allocated) {
1132 /* image->raw_metadata and cli_sections might lie inside image->raw_data */
1133 MonoCLIImageInfo *ii = image->image_info;
1135 if ((image->raw_metadata > image->raw_data) &&
1136 (image->raw_metadata <= (image->raw_data + image->raw_data_len)))
1137 image->raw_metadata = NULL;
1139 for (i = 0; i < ii->cli_section_count; i++)
1140 if (((char*)(ii->cli_sections [i]) > image->raw_data) &&
1141 ((char*)(ii->cli_sections [i]) <= ((char*)image->raw_data + image->raw_data_len)))
1142 ii->cli_sections [i] = NULL;
1144 g_free (image->raw_data);
1147 if (debug_assembly_unload) {
1148 image->name = g_strdup_printf ("%s - UNLOADED", image->name);
1150 g_free (image->name);
1151 g_free (image->guid);
1152 g_free (image->version);
1153 g_free (image->files);
1156 g_hash_table_destroy (image->method_cache);
1157 mono_internal_hash_table_destroy (&image->class_cache);
1158 g_hash_table_destroy (image->field_cache);
1159 if (image->array_cache) {
1160 g_hash_table_foreach (image->array_cache, free_array_cache_entry, NULL);
1161 g_hash_table_destroy (image->array_cache);
1163 if (image->ptr_cache)
1164 g_hash_table_destroy (image->ptr_cache);
1165 if (image->name_cache) {
1166 g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
1167 g_hash_table_destroy (image->name_cache);
1169 g_hash_table_destroy (image->native_wrapper_cache);
1170 g_hash_table_destroy (image->managed_wrapper_cache);
1171 g_hash_table_destroy (image->delegate_begin_invoke_cache);
1172 g_hash_table_destroy (image->delegate_end_invoke_cache);
1173 g_hash_table_destroy (image->delegate_invoke_cache);
1174 g_hash_table_foreach (image->remoting_invoke_cache, free_remoting_wrappers, NULL);
1175 g_hash_table_destroy (image->remoting_invoke_cache);
1176 g_hash_table_destroy (image->runtime_invoke_cache);
1177 g_hash_table_destroy (image->synchronized_cache);
1178 g_hash_table_destroy (image->unbox_wrapper_cache);
1179 g_hash_table_destroy (image->cominterop_invoke_cache);
1180 g_hash_table_destroy (image->cominterop_wrapper_cache);
1181 g_hash_table_destroy (image->typespec_cache);
1182 g_hash_table_destroy (image->ldfld_wrapper_cache);
1183 g_hash_table_destroy (image->ldflda_wrapper_cache);
1184 g_hash_table_destroy (image->ldfld_remote_wrapper_cache);
1185 g_hash_table_destroy (image->stfld_wrapper_cache);
1186 g_hash_table_destroy (image->stfld_remote_wrapper_cache);
1187 g_hash_table_destroy (image->isinst_cache);
1188 g_hash_table_destroy (image->castclass_cache);
1189 g_hash_table_destroy (image->proxy_isinst_cache);
1191 /* The ownership of signatures is not well defined */
1192 //g_hash_table_foreach (image->memberref_signatures, free_mr_signatures, NULL);
1193 g_hash_table_destroy (image->memberref_signatures);
1194 //g_hash_table_foreach (image->helper_signatures, free_mr_signatures, NULL);
1195 g_hash_table_destroy (image->helper_signatures);
1196 g_hash_table_destroy (image->method_signatures);
1198 if (image->interface_bitset) {
1199 mono_unload_interface_ids (image->interface_bitset);
1200 mono_bitset_free (image->interface_bitset);
1202 if (image->image_info){
1203 MonoCLIImageInfo *ii = image->image_info;
1205 if (ii->cli_section_tables)
1206 g_free (ii->cli_section_tables);
1207 if (ii->cli_sections)
1208 g_free (ii->cli_sections);
1209 g_free (image->image_info);
1212 for (i = 0; i < image->module_count; ++i) {
1213 if (image->modules [i])
1214 mono_image_close (image->modules [i]);
1217 g_free (image->modules);
1218 if (image->modules_loaded)
1219 g_free (image->modules_loaded);
1220 if (image->references)
1221 g_free (image->references);
1222 /*g_print ("destroy image %p (dynamic: %d)\n", image, image->dynamic);*/
1223 if (!image->dynamic) {
1224 if (debug_assembly_unload)
1225 mono_mempool_invalidate (image->mempool);
1227 mono_mempool_destroy (image->mempool);
1231 /* Dynamic images are GC_MALLOCed */
1232 struct _MonoDynamicImage *di = (struct _MonoDynamicImage*)image;
1234 g_free ((char*)image->module_name);
1236 g_hash_table_destroy (di->typespec);
1238 g_hash_table_destroy (di->typeref);
1240 g_hash_table_destroy (di->handleref);
1242 mono_g_hash_table_destroy (di->tokens);
1243 if (di->blob_cache) {
1244 g_hash_table_foreach (di->blob_cache, free_blob_cache_entry, NULL);
1245 g_hash_table_destroy (di->blob_cache);
1247 g_list_free (di->array_methods);
1249 g_ptr_array_free (di->gen_params, TRUE);
1250 if (di->token_fixups)
1251 mono_g_hash_table_destroy (di->token_fixups);
1252 if (di->method_to_table_idx)
1253 g_hash_table_destroy (di->method_to_table_idx);
1254 if (di->field_to_table_idx)
1255 g_hash_table_destroy (di->field_to_table_idx);
1256 if (di->method_aux_hash)
1257 g_hash_table_destroy (di->method_aux_hash);
1258 g_free (di->strong_name);
1259 g_free (di->win32_res);
1260 /*g_print ("string heap destroy for image %p\n", di);*/
1261 mono_dynamic_stream_reset (&di->sheap);
1262 mono_dynamic_stream_reset (&di->code);
1263 mono_dynamic_stream_reset (&di->resources);
1264 mono_dynamic_stream_reset (&di->us);
1265 mono_dynamic_stream_reset (&di->blob);
1266 mono_dynamic_stream_reset (&di->tstream);
1267 mono_dynamic_stream_reset (&di->guid);
1268 for (i = 0; i < MONO_TABLE_NUM; ++i) {
1269 g_free (di->tables [i].values);
1271 mono_mempool_destroy (image->mempool);
1276 * mono_image_strerror:
1277 * @status: an code indicating the result from a recent operation
1279 * Returns: a string describing the error
1282 mono_image_strerror (MonoImageOpenStatus status)
1287 case MONO_IMAGE_ERROR_ERRNO:
1288 return strerror (errno);
1289 case MONO_IMAGE_IMAGE_INVALID:
1290 return "File does not contain a valid CIL image";
1291 case MONO_IMAGE_MISSING_ASSEMBLYREF:
1292 return "An assembly was referenced, but could not be found";
1294 return "Internal error";
1298 mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
1299 guint32 lang_id, gunichar2 *name,
1300 MonoPEResourceDirEntry *entry,
1301 MonoPEResourceDir *root, guint32 level)
1303 gboolean is_string, is_dir;
1304 guint32 name_offset, dir_offset;
1306 /* Level 0 holds a directory entry for each type of resource
1307 * (identified by ID or name).
1309 * Level 1 holds a directory entry for each named resource
1310 * item, and each "anonymous" item of a particular type of
1313 * Level 2 holds a directory entry for each language pointing to
1316 name_offset = GUINT32_FROM_LE (entry->name_offset) & 0x7fffffff;
1317 dir_offset = GUINT32_FROM_LE (entry->dir_offset) & 0x7fffffff;
1319 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
1320 is_string = (GUINT32_FROM_LE (entry->name_offset) & 0x80000000) != 0;
1321 is_dir = (GUINT32_FROM_LE (entry->dir_offset) & 0x80000000) != 0;
1323 is_string = entry->name_is_string;
1324 is_dir = entry->is_dir;
1328 if((is_string==FALSE && name_offset!=res_id) ||
1329 (is_string==TRUE)) {
1332 } else if (level==1) {
1335 is_string==TRUE && name!=lookup (name_offset)) {
1339 } else if (level==2) {
1340 if ((is_string == FALSE &&
1341 name_offset != lang_id &&
1343 (is_string == TRUE)) {
1347 g_assert_not_reached ();
1351 MonoPEResourceDir *res_dir=(MonoPEResourceDir *)(((char *)root)+dir_offset);
1352 MonoPEResourceDirEntry *sub_entries=(MonoPEResourceDirEntry *)(res_dir+1);
1355 entries = GUINT16_FROM_LE (res_dir->res_named_entries) + GUINT16_FROM_LE (res_dir->res_id_entries);
1357 for(i=0; i<entries; i++) {
1358 MonoPEResourceDirEntry *sub_entry=&sub_entries[i];
1361 ret=mono_image_walk_resource_tree (info, res_id,
1372 MonoPEResourceDataEntry *data_entry=(MonoPEResourceDataEntry *)((char *)(root)+dir_offset);
1373 MonoPEResourceDataEntry *res;
1375 res = g_new0 (MonoPEResourceDataEntry, 1);
1377 res->rde_data_offset = GUINT32_TO_LE (data_entry->rde_data_offset);
1378 res->rde_size = GUINT32_TO_LE (data_entry->rde_size);
1379 res->rde_codepage = GUINT32_TO_LE (data_entry->rde_codepage);
1380 res->rde_reserved = GUINT32_TO_LE (data_entry->rde_reserved);
1387 * mono_image_lookup_resource:
1388 * @image: the image to look up the resource in
1389 * @res_id: A MONO_PE_RESOURCE_ID_ that represents the resource ID to lookup.
1390 * @lang_id: The language id.
1391 * @name: the resource name to lookup.
1393 * Returns: NULL if not found, otherwise a pointer to the in-memory representation
1394 * of the given resource. The caller should free it using g_free () when no longer
1398 mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name)
1400 MonoCLIImageInfo *info;
1401 MonoDotNetHeader *header;
1402 MonoPEDatadir *datadir;
1403 MonoPEDirEntry *rsrc;
1404 MonoPEResourceDir *resource_dir;
1405 MonoPEResourceDirEntry *res_entries;
1412 info=image->image_info;
1417 header=&info->cli_header;
1422 datadir=&header->datadir;
1427 rsrc=&datadir->pe_resource_table;
1432 resource_dir=(MonoPEResourceDir *)mono_image_rva_map (image, rsrc->rva);
1433 if(resource_dir==NULL) {
1437 entries = GUINT16_FROM_LE (resource_dir->res_named_entries) + GUINT16_FROM_LE (resource_dir->res_id_entries);
1438 res_entries=(MonoPEResourceDirEntry *)(resource_dir+1);
1440 for(i=0; i<entries; i++) {
1441 MonoPEResourceDirEntry *entry=&res_entries[i];
1444 ret=mono_image_walk_resource_tree (info, res_id, lang_id,
1445 name, entry, resource_dir,
1456 * mono_image_get_entry_point:
1457 * @image: the image where the entry point will be looked up.
1459 * Use this routine to determine the metadata token for method that
1460 * has been flagged as the entry point.
1462 * Returns: the token for the entry point method in the image
1465 mono_image_get_entry_point (MonoImage *image)
1467 return ((MonoCLIImageInfo*)image->image_info)->cli_cli_header.ch_entry_point;
1471 * mono_image_get_resource:
1472 * @image: the image where the resource will be looked up.
1473 * @offset: The offset to add to the resource
1474 * @size: a pointer to an int where the size of the resource will be stored
1476 * This is a low-level routine that fetches a resource from the
1477 * metadata that starts at a given @offset. The @size parameter is
1478 * filled with the data field as encoded in the metadata.
1480 * Returns: the pointer to the resource whose offset is @offset.
1483 mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)
1485 MonoCLIImageInfo *iinfo = image->image_info;
1486 MonoCLIHeader *ch = &iinfo->cli_cli_header;
1489 if (!ch->ch_resources.rva || offset + 4 > ch->ch_resources.size)
1492 data = mono_image_rva_map (image, ch->ch_resources.rva);
1497 *size = read32 (data);
1503 mono_image_load_file_for_image (MonoImage *image, int fileidx)
1505 char *base_dir, *name;
1507 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
1511 if (fileidx < 1 || fileidx > t->rows)
1514 if (image->files && image->files [fileidx - 1])
1515 return image->files [fileidx - 1];
1518 image->files = g_new0 (MonoImage*, t->rows);
1520 fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
1521 fname = mono_metadata_string_heap (image, fname_id);
1522 base_dir = g_path_get_dirname (image->name);
1523 name = g_build_filename (base_dir, fname, NULL);
1524 res = mono_image_open (name, NULL);
1527 /* g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly); */
1528 res->assembly = image->assembly;
1529 for (i = 0; i < res->module_count; ++i) {
1530 if (res->modules [i] && !res->modules [i]->assembly)
1531 res->modules [i]->assembly = image->assembly;
1534 image->files [fileidx - 1] = res;
1542 * mono_image_get_strong_name:
1543 * @image: a MonoImage
1544 * @size: a guint32 pointer, or NULL.
1546 * If the image has a strong name, and @size is not NULL, the value
1547 * pointed to by size will have the size of the strong name.
1549 * Returns: NULL if the image does not have a strong name, or a
1550 * pointer to the public key.
1553 mono_image_get_strong_name (MonoImage *image, guint32 *size)
1555 MonoCLIImageInfo *iinfo = image->image_info;
1556 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
1559 if (!de->size || !de->rva)
1561 data = mono_image_rva_map (image, de->rva);
1570 * mono_image_strong_name_position:
1571 * @image: a MonoImage
1572 * @size: a guint32 pointer, or NULL.
1574 * If the image has a strong name, and @size is not NULL, the value
1575 * pointed to by size will have the size of the strong name.
1577 * Returns: the position within the image file where the strong name
1581 mono_image_strong_name_position (MonoImage *image, guint32 *size)
1583 MonoCLIImageInfo *iinfo = image->image_info;
1584 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
1585 const int top = iinfo->cli_section_count;
1586 MonoSectionTable *tables = iinfo->cli_section_tables;
1588 guint32 addr = de->rva;
1592 if (!de->size || !de->rva)
1594 for (i = 0; i < top; i++){
1595 if ((addr >= tables->st_virtual_address) &&
1596 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
1597 return tables->st_raw_data_ptr +
1598 (addr - tables->st_virtual_address);
1607 * mono_image_get_public_key:
1608 * @image: a MonoImage
1609 * @size: a guint32 pointer, or NULL.
1611 * This is used to obtain the public key in the @image.
1613 * If the image has a public key, and @size is not NULL, the value
1614 * pointed to by size will have the size of the public key.
1616 * Returns: NULL if the image does not have a public key, or a pointer
1617 * to the public key.
1620 mono_image_get_public_key (MonoImage *image, guint32 *size)
1624 if (image->tables [MONO_TABLE_ASSEMBLY].rows != 1)
1626 tok = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY], 0, MONO_ASSEMBLY_PUBLIC_KEY);
1629 pubkey = mono_metadata_blob_heap (image, tok);
1630 len = mono_metadata_decode_blob_size (pubkey, &pubkey);
1637 * mono_image_get_name:
1638 * @name: a MonoImage
1640 * Returns: the name of the assembly.
1643 mono_image_get_name (MonoImage *image)
1645 return image->assembly_name;
1649 * mono_image_get_filename:
1650 * @image: a MonoImage
1652 * Used to get the filename that hold the actual MonoImage
1654 * Returns: the filename.
1657 mono_image_get_filename (MonoImage *image)
1663 mono_image_get_guid (MonoImage *image)
1668 const MonoTableInfo*
1669 mono_image_get_table_info (MonoImage *image, int table_id)
1671 if (table_id < 0 || table_id >= MONO_TABLE_NUM)
1673 return &image->tables [table_id];
1677 mono_image_get_table_rows (MonoImage *image, int table_id)
1679 if (table_id < 0 || table_id >= MONO_TABLE_NUM)
1681 return image->tables [table_id].rows;
1685 mono_table_info_get_rows (const MonoTableInfo *table)
1691 * mono_image_get_assembly:
1692 * @image: the MonoImage.
1694 * Use this routine to get the assembly that owns this image.
1696 * Returns: the assembly that holds this image.
1699 mono_image_get_assembly (MonoImage *image)
1701 return image->assembly;
1705 * mono_image_is_dynamic:
1706 * @image: the MonoImage
1708 * Determines if the given image was created dynamically through the
1709 * System.Reflection.Emit API
1711 * Returns: TRUE if the image was created dynamically, FALSE if not.
1714 mono_image_is_dynamic (MonoImage *image)
1716 return image->dynamic;
1720 * mono_image_has_authenticode_entry:
1721 * @image: the MonoImage
1723 * Use this routine to determine if the image has a Authenticode
1724 * Certificate Table.
1726 * Returns: TRUE if the image contains an authenticode entry in the PE
1730 mono_image_has_authenticode_entry (MonoImage *image)
1732 MonoCLIImageInfo *iinfo = image->image_info;
1733 MonoDotNetHeader *header = &iinfo->cli_header;
1734 MonoPEDirEntry *de = &header->datadir.pe_certificate_table;
1735 // the Authenticode "pre" (non ASN.1) header is 8 bytes long
1736 return ((de->rva != 0) && (de->size > 8));