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 <mono/metadata/assembly.h>
31 #include <sys/types.h>
37 #define INVALID_ADDRESS 0xffffffff
40 * Keeps track of the various assemblies loaded
42 static GHashTable *loaded_images_hash;
43 static GHashTable *loaded_images_guid_hash;
44 static GHashTable *loaded_images_refonly_hash;
45 static GHashTable *loaded_images_refonly_guid_hash;
47 static gboolean debug_assembly_unload = FALSE;
49 #define mono_images_lock() EnterCriticalSection (&images_mutex)
50 #define mono_images_unlock() LeaveCriticalSection (&images_mutex)
51 static CRITICAL_SECTION images_mutex;
54 mono_cli_rva_image_map (MonoCLIImageInfo *iinfo, guint32 addr)
56 const int top = iinfo->cli_section_count;
57 MonoSectionTable *tables = iinfo->cli_section_tables;
60 for (i = 0; i < top; i++){
61 if ((addr >= tables->st_virtual_address) &&
62 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
63 return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
67 return INVALID_ADDRESS;
71 * mono_images_rva_map:
73 * @addr: relative virtual address (RVA)
75 * This is a low-level routine used by the runtime to map relative
76 * virtual address (RVA) into their location in memory.
78 * Returns: the address in memory for the given RVA, or NULL if the
79 * RVA is not valid for this image.
82 mono_image_rva_map (MonoImage *image, guint32 addr)
84 MonoCLIImageInfo *iinfo = image->image_info;
85 const int top = iinfo->cli_section_count;
86 MonoSectionTable *tables = iinfo->cli_section_tables;
89 for (i = 0; i < top; i++){
90 if ((addr >= tables->st_virtual_address) &&
91 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
92 if (!iinfo->cli_sections [i]) {
93 if (!mono_image_ensure_section_idx (image, i))
96 return (char*)iinfo->cli_sections [i] +
97 (addr - tables->st_virtual_address);
107 * Initialize the global variables used by this module.
110 mono_images_init (void)
112 InitializeCriticalSection (&images_mutex);
114 loaded_images_hash = g_hash_table_new (g_str_hash, g_str_equal);
115 loaded_images_guid_hash = g_hash_table_new (g_str_hash, g_str_equal);
116 loaded_images_refonly_hash = g_hash_table_new (g_str_hash, g_str_equal);
117 loaded_images_refonly_guid_hash = g_hash_table_new (g_str_hash, g_str_equal);
119 debug_assembly_unload = getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
123 * mono_images_cleanup:
125 * Free all resources used by this module.
128 mono_images_cleanup (void)
130 DeleteCriticalSection (&images_mutex);
132 g_hash_table_destroy (loaded_images_hash);
133 g_hash_table_destroy (loaded_images_guid_hash);
134 g_hash_table_destroy (loaded_images_refonly_hash);
135 g_hash_table_destroy (loaded_images_refonly_guid_hash);
139 * mono_image_ensure_section_idx:
140 * @image: The image we are operating on
141 * @section: section number that we will load/map into memory
143 * This routine makes sure that we have an in-memory copy of
144 * an image section (.text, .rsrc, .data).
146 * Returns: TRUE on success
149 mono_image_ensure_section_idx (MonoImage *image, int section)
151 MonoCLIImageInfo *iinfo = image->image_info;
152 MonoSectionTable *sect;
155 g_return_val_if_fail (section < iinfo->cli_section_count, FALSE);
157 if (iinfo->cli_sections [section] != NULL)
160 sect = &iinfo->cli_section_tables [section];
162 writable = sect->st_flags & SECT_FLAGS_MEM_WRITE;
164 if (sect->st_raw_data_ptr + sect->st_raw_data_size > image->raw_data_len)
166 /* FIXME: we ignore the writable flag since we don't patch the binary */
167 iinfo->cli_sections [section] = image->raw_data + sect->st_raw_data_ptr;
172 * mono_image_ensure_section:
173 * @image: The image we are operating on
174 * @section: section name that we will load/map into memory
176 * This routine makes sure that we have an in-memory copy of
177 * an image section (.text, .rsrc, .data).
179 * Returns: TRUE on success
182 mono_image_ensure_section (MonoImage *image, const char *section)
184 MonoCLIImageInfo *ii = image->image_info;
187 for (i = 0; i < ii->cli_section_count; i++){
188 if (strncmp (ii->cli_section_tables [i].st_name, section, 8) != 0)
191 return mono_image_ensure_section_idx (image, i);
197 load_section_tables (MonoImage *image, MonoCLIImageInfo *iinfo, guint32 offset)
199 const int top = iinfo->cli_header.coff.coff_sections;
202 iinfo->cli_section_count = top;
203 iinfo->cli_section_tables = g_new0 (MonoSectionTable, top);
204 iinfo->cli_sections = g_new0 (void *, top);
206 for (i = 0; i < top; i++){
207 MonoSectionTable *t = &iinfo->cli_section_tables [i];
209 if (offset + sizeof (MonoSectionTable) > image->raw_data_len)
211 memcpy (t, image->raw_data + offset, sizeof (MonoSectionTable));
212 offset += sizeof (MonoSectionTable);
214 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
215 t->st_virtual_size = GUINT32_FROM_LE (t->st_virtual_size);
216 t->st_virtual_address = GUINT32_FROM_LE (t->st_virtual_address);
217 t->st_raw_data_size = GUINT32_FROM_LE (t->st_raw_data_size);
218 t->st_raw_data_ptr = GUINT32_FROM_LE (t->st_raw_data_ptr);
219 t->st_reloc_ptr = GUINT32_FROM_LE (t->st_reloc_ptr);
220 t->st_lineno_ptr = GUINT32_FROM_LE (t->st_lineno_ptr);
221 t->st_reloc_count = GUINT16_FROM_LE (t->st_reloc_count);
222 t->st_line_count = GUINT16_FROM_LE (t->st_line_count);
223 t->st_flags = GUINT32_FROM_LE (t->st_flags);
225 /* consistency checks here */
232 load_cli_header (MonoImage *image, MonoCLIImageInfo *iinfo)
236 offset = mono_cli_rva_image_map (iinfo, iinfo->cli_header.datadir.pe_cli_header.rva);
237 if (offset == INVALID_ADDRESS)
240 if (offset + sizeof (MonoCLIHeader) > image->raw_data_len)
242 memcpy (&iinfo->cli_cli_header, image->raw_data + offset, sizeof (MonoCLIHeader));
244 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
245 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
246 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
247 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
248 SWAP32 (iinfo->cli_cli_header.ch_size);
249 SWAP32 (iinfo->cli_cli_header.ch_flags);
250 SWAP32 (iinfo->cli_cli_header.ch_entry_point);
251 SWAP16 (iinfo->cli_cli_header.ch_runtime_major);
252 SWAP16 (iinfo->cli_cli_header.ch_runtime_minor);
253 SWAPPDE (iinfo->cli_cli_header.ch_metadata);
254 SWAPPDE (iinfo->cli_cli_header.ch_resources);
255 SWAPPDE (iinfo->cli_cli_header.ch_strong_name);
256 SWAPPDE (iinfo->cli_cli_header.ch_code_manager_table);
257 SWAPPDE (iinfo->cli_cli_header.ch_vtable_fixups);
258 SWAPPDE (iinfo->cli_cli_header.ch_export_address_table_jumps);
259 SWAPPDE (iinfo->cli_cli_header.ch_eeinfo_table);
260 SWAPPDE (iinfo->cli_cli_header.ch_helper_table);
261 SWAPPDE (iinfo->cli_cli_header.ch_dynamic_info);
262 SWAPPDE (iinfo->cli_cli_header.ch_delay_load_info);
263 SWAPPDE (iinfo->cli_cli_header.ch_module_image);
264 SWAPPDE (iinfo->cli_cli_header.ch_external_fixups);
265 SWAPPDE (iinfo->cli_cli_header.ch_ridmap);
266 SWAPPDE (iinfo->cli_cli_header.ch_debug_map);
267 SWAPPDE (iinfo->cli_cli_header.ch_ip_map);
272 /* Catch new uses of the fields that are supposed to be zero */
274 if ((iinfo->cli_cli_header.ch_eeinfo_table.rva != 0) ||
275 (iinfo->cli_cli_header.ch_helper_table.rva != 0) ||
276 (iinfo->cli_cli_header.ch_dynamic_info.rva != 0) ||
277 (iinfo->cli_cli_header.ch_delay_load_info.rva != 0) ||
278 (iinfo->cli_cli_header.ch_module_image.rva != 0) ||
279 (iinfo->cli_cli_header.ch_external_fixups.rva != 0) ||
280 (iinfo->cli_cli_header.ch_ridmap.rva != 0) ||
281 (iinfo->cli_cli_header.ch_debug_map.rva != 0) ||
282 (iinfo->cli_cli_header.ch_ip_map.rva != 0)){
285 * No need to scare people who are testing this, I am just
286 * labelling this as a LAMESPEC
288 /* g_warning ("Some fields in the CLI header which should have been zero are not zero"); */
296 load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
298 guint32 offset, size;
304 offset = mono_cli_rva_image_map (iinfo, iinfo->cli_cli_header.ch_metadata.rva);
305 if (offset == INVALID_ADDRESS)
308 size = iinfo->cli_cli_header.ch_metadata.size;
310 if (offset + size > image->raw_data_len)
312 image->raw_metadata = image->raw_data + offset;
314 ptr = image->raw_metadata;
316 if (strncmp (ptr, "BSJB", 4) == 0){
317 guint32 version_string_len;
320 image->md_version_major = read16 (ptr);
322 image->md_version_minor = read16 (ptr);
325 version_string_len = read32 (ptr);
327 image->version = g_strndup (ptr, version_string_len);
328 ptr += version_string_len;
329 pad = ptr - image->raw_metadata;
331 ptr += 4 - (pad % 4);
335 /* skip over flags */
338 streams = read16 (ptr);
341 for (i = 0; i < streams; i++){
342 if (strncmp (ptr + 8, "#~", 3) == 0){
343 image->heap_tables.data = image->raw_metadata + read32 (ptr);
344 image->heap_tables.size = read32 (ptr + 4);
346 } else if (strncmp (ptr + 8, "#Strings", 9) == 0){
347 image->heap_strings.data = image->raw_metadata + read32 (ptr);
348 image->heap_strings.size = read32 (ptr + 4);
350 } else if (strncmp (ptr + 8, "#US", 4) == 0){
351 image->heap_us.data = image->raw_metadata + read32 (ptr);
352 image->heap_us.size = read32 (ptr + 4);
354 } else if (strncmp (ptr + 8, "#Blob", 6) == 0){
355 image->heap_blob.data = image->raw_metadata + read32 (ptr);
356 image->heap_blob.size = read32 (ptr + 4);
358 } else if (strncmp (ptr + 8, "#GUID", 6) == 0){
359 image->heap_guid.data = image->raw_metadata + read32 (ptr);
360 image->heap_guid.size = read32 (ptr + 4);
362 } else if (strncmp (ptr + 8, "#-", 3) == 0) {
363 image->heap_tables.data = image->raw_metadata + read32 (ptr);
364 image->heap_tables.size = read32 (ptr + 4);
366 image->uncompressed_metadata = TRUE;
367 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);
369 g_message ("Unknown heap type: %s\n", ptr + 8);
370 ptr += 8 + strlen (ptr + 8) + 1;
372 pad = ptr - image->raw_metadata;
374 ptr += 4 - (pad % 4);
377 g_assert (image->heap_guid.data);
378 g_assert (image->heap_guid.size >= 16);
380 image->guid = mono_guid_to_string ((guint8*)image->heap_guid.data);
386 * Load representation of logical metadata tables, from the "#~" stream
389 load_tables (MonoImage *image)
391 const char *heap_tables = image->heap_tables.data;
393 guint64 valid_mask, sorted_mask;
394 int valid = 0, table;
397 heap_sizes = heap_tables [6];
398 image->idx_string_wide = ((heap_sizes & 0x01) == 1);
399 image->idx_guid_wide = ((heap_sizes & 0x02) == 2);
400 image->idx_blob_wide = ((heap_sizes & 0x04) == 4);
402 valid_mask = read64 (heap_tables + 8);
403 sorted_mask = read64 (heap_tables + 16);
404 rows = (const guint32 *) (heap_tables + 24);
406 for (table = 0; table < 64; table++){
407 if ((valid_mask & ((guint64) 1 << table)) == 0){
408 if (table > MONO_TABLE_LAST)
410 image->tables [table].rows = 0;
413 if (table > MONO_TABLE_LAST) {
414 g_warning("bits in valid must be zero above 0x2d (II - 23.1.6)");
416 image->tables [table].rows = read32 (rows);
418 /*if ((sorted_mask & ((guint64) 1 << table)) == 0){
419 g_print ("table %s (0x%02x) is sorted\n", mono_meta_table_name (table), table);
425 image->tables_base = (heap_tables + 24) + (4 * valid);
427 /* They must be the same */
428 g_assert ((const void *) image->tables_base == (const void *) rows);
430 mono_metadata_compute_table_bases (image);
435 load_metadata (MonoImage *image, MonoCLIImageInfo *iinfo)
437 if (!load_metadata_ptrs (image, iinfo))
440 return load_tables (image);
444 mono_image_check_for_module_cctor (MonoImage *image)
446 MonoTableInfo *t, *mt;
447 t = &image->tables [MONO_TABLE_TYPEDEF];
448 mt = &image->tables [MONO_TABLE_METHOD];
449 if (mono_get_runtime_info ()->framework_version [0] == '1') {
450 image->checked_module_cctor = TRUE;
453 if (image->dynamic) {
455 image->checked_module_cctor = TRUE;
459 guint32 nameidx = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_NAME);
460 const char *name = mono_metadata_string_heap (image, nameidx);
461 if (strcmp (name, "<Module>") == 0) {
462 guint32 first_method = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_METHOD_LIST) - 1;
465 last_method = mono_metadata_decode_row_col (t, 1, MONO_TYPEDEF_METHOD_LIST) - 1;
467 last_method = mt->rows;
468 for (; first_method < last_method; first_method++) {
469 nameidx = mono_metadata_decode_row_col (mt, first_method, MONO_METHOD_NAME);
470 name = mono_metadata_string_heap (image, nameidx);
471 if (strcmp (name, ".cctor") == 0) {
472 image->has_module_cctor = TRUE;
473 image->checked_module_cctor = TRUE;
479 image->has_module_cctor = FALSE;
480 image->checked_module_cctor = TRUE;
484 load_modules (MonoImage *image)
491 t = &image->tables [MONO_TABLE_MODULEREF];
492 image->modules = g_new0 (MonoImage *, t->rows);
493 image->modules_loaded = g_new0 (gboolean, t->rows);
494 image->module_count = t->rows;
498 * mono_image_load_module:
500 * Load the module with the one-based index IDX from IMAGE and return it. Return NULL if
501 * it cannot be loaded.
504 mono_image_load_module (MonoImage *image, int idx)
507 MonoTableInfo *file_table;
510 gboolean refonly = image->ref_only;
511 GList *list_iter, *valid_modules = NULL;
512 MonoImageOpenStatus status;
514 g_assert (idx <= image->module_count);
515 if (image->modules_loaded [idx - 1])
516 return image->modules [idx - 1];
518 file_table = &image->tables [MONO_TABLE_FILE];
519 for (i = 0; i < file_table->rows; i++) {
520 guint32 cols [MONO_FILE_SIZE];
521 mono_metadata_decode_row (file_table, i, cols, MONO_FILE_SIZE);
522 if (cols [MONO_FILE_FLAGS] == FILE_CONTAINS_NO_METADATA)
524 valid_modules = g_list_prepend (valid_modules, (char*)mono_metadata_string_heap (image, cols [MONO_FILE_NAME]));
527 t = &image->tables [MONO_TABLE_MODULEREF];
528 base_dir = g_path_get_dirname (image->name);
533 guint32 cols [MONO_MODULEREF_SIZE];
534 /* if there is no file table, we try to load the module... */
535 int valid = file_table->rows == 0;
537 mono_metadata_decode_row (t, idx - 1, cols, MONO_MODULEREF_SIZE);
538 name = mono_metadata_string_heap (image, cols [MONO_MODULEREF_NAME]);
539 for (list_iter = valid_modules; list_iter; list_iter = list_iter->next) {
540 /* be safe with string dups, but we could just compare string indexes */
541 if (strcmp (list_iter->data, name) == 0) {
547 module_ref = g_build_filename (base_dir, name, NULL);
548 image->modules [idx - 1] = mono_image_open_full (module_ref, &status, refonly);
549 if (image->modules [idx - 1]) {
550 mono_image_addref (image->modules [idx - 1]);
551 image->modules [idx - 1]->assembly = image->assembly;
552 /* g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly); */
558 image->modules_loaded [idx - 1] = TRUE;
561 g_list_free (valid_modules);
563 return image->modules [idx - 1];
567 register_guid (gpointer key, gpointer value, gpointer user_data)
569 MonoImage *image = (MonoImage*)value;
571 if (!g_hash_table_lookup (loaded_images_guid_hash, image))
572 g_hash_table_insert (loaded_images_guid_hash, image->guid, image);
576 build_guid_table (gboolean refonly)
578 GHashTable *loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
579 g_hash_table_foreach (loaded_images, register_guid, NULL);
583 class_key_extract (gpointer value)
585 MonoClass *class = value;
587 return GUINT_TO_POINTER (class->type_token);
591 class_next_value (gpointer value)
593 MonoClass *class = value;
595 return (gpointer*)&class->next_class_cache;
599 mono_image_init (MonoImage *image)
601 image->mempool = mono_mempool_new ();
602 image->method_cache = g_hash_table_new (NULL, NULL);
603 mono_internal_hash_table_init (&image->class_cache,
607 image->field_cache = g_hash_table_new (NULL, NULL);
609 image->delegate_begin_invoke_cache =
610 g_hash_table_new ((GHashFunc)mono_signature_hash,
611 (GCompareFunc)mono_metadata_signature_equal);
612 image->delegate_end_invoke_cache =
613 g_hash_table_new ((GHashFunc)mono_signature_hash,
614 (GCompareFunc)mono_metadata_signature_equal);
615 image->delegate_invoke_cache =
616 g_hash_table_new ((GHashFunc)mono_signature_hash,
617 (GCompareFunc)mono_metadata_signature_equal);
618 image->runtime_invoke_cache =
619 g_hash_table_new ((GHashFunc)mono_signature_hash,
620 (GCompareFunc)mono_metadata_signature_equal);
622 image->managed_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
623 image->native_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
624 image->remoting_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
625 image->cominterop_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
626 image->cominterop_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
627 image->synchronized_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
628 image->unbox_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
630 image->ldfld_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
631 image->ldflda_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
632 image->ldfld_remote_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
633 image->stfld_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
634 image->stfld_remote_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
635 image->isinst_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
636 image->castclass_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
637 image->proxy_isinst_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
639 image->typespec_cache = g_hash_table_new (NULL, NULL);
640 image->memberref_signatures = g_hash_table_new (NULL, NULL);
641 image->helper_signatures = g_hash_table_new (g_str_hash, g_str_equal);
642 image->method_signatures = g_hash_table_new (NULL, NULL);
646 do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
647 gboolean care_about_cli)
649 MonoCLIImageInfo *iinfo;
650 MonoDotNetHeader *header;
651 MonoMSDOSHeader msdos;
654 mono_image_init (image);
656 iinfo = image->image_info;
657 header = &iinfo->cli_header;
660 *status = MONO_IMAGE_IMAGE_INVALID;
662 if (offset + sizeof (msdos) > image->raw_data_len)
664 memcpy (&msdos, image->raw_data + offset, sizeof (msdos));
666 if (!(msdos.msdos_sig [0] == 'M' && msdos.msdos_sig [1] == 'Z'))
669 msdos.pe_offset = GUINT32_FROM_LE (msdos.pe_offset);
671 offset = msdos.pe_offset;
673 if (offset + sizeof (MonoDotNetHeader) > image->raw_data_len)
675 memcpy (header, image->raw_data + offset, sizeof (MonoDotNetHeader));
676 offset += sizeof (MonoDotNetHeader);
678 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
679 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
680 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
681 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
682 SWAP32 (header->coff.coff_time);
683 SWAP32 (header->coff.coff_symptr);
684 SWAP32 (header->coff.coff_symcount);
685 SWAP16 (header->coff.coff_machine);
686 SWAP16 (header->coff.coff_sections);
687 SWAP16 (header->coff.coff_opt_header_size);
688 SWAP16 (header->coff.coff_attributes);
690 SWAP32 (header->pe.pe_code_size);
691 SWAP32 (header->pe.pe_data_size);
692 SWAP32 (header->pe.pe_uninit_data_size);
693 SWAP32 (header->pe.pe_rva_entry_point);
694 SWAP32 (header->pe.pe_rva_code_base);
695 SWAP32 (header->pe.pe_rva_data_base);
696 SWAP16 (header->pe.pe_magic);
698 /* MonoPEHeaderNT: not used yet */
699 SWAP32 (header->nt.pe_image_base); /* must be 0x400000 */
700 SWAP32 (header->nt.pe_section_align); /* must be 8192 */
701 SWAP32 (header->nt.pe_file_alignment); /* must be 512 or 4096 */
702 SWAP16 (header->nt.pe_os_major); /* must be 4 */
703 SWAP16 (header->nt.pe_os_minor); /* must be 0 */
704 SWAP16 (header->nt.pe_user_major);
705 SWAP16 (header->nt.pe_user_minor);
706 SWAP16 (header->nt.pe_subsys_major);
707 SWAP16 (header->nt.pe_subsys_minor);
708 SWAP32 (header->nt.pe_reserved_1);
709 SWAP32 (header->nt.pe_image_size);
710 SWAP32 (header->nt.pe_header_size);
711 SWAP32 (header->nt.pe_checksum);
712 SWAP16 (header->nt.pe_subsys_required);
713 SWAP16 (header->nt.pe_dll_flags);
714 SWAP32 (header->nt.pe_stack_reserve);
715 SWAP32 (header->nt.pe_stack_commit);
716 SWAP32 (header->nt.pe_heap_reserve);
717 SWAP32 (header->nt.pe_heap_commit);
718 SWAP32 (header->nt.pe_loader_flags);
719 SWAP32 (header->nt.pe_data_dir_count);
721 /* MonoDotNetHeader: mostly unused */
722 SWAPPDE (header->datadir.pe_export_table);
723 SWAPPDE (header->datadir.pe_import_table);
724 SWAPPDE (header->datadir.pe_resource_table);
725 SWAPPDE (header->datadir.pe_exception_table);
726 SWAPPDE (header->datadir.pe_certificate_table);
727 SWAPPDE (header->datadir.pe_reloc_table);
728 SWAPPDE (header->datadir.pe_debug);
729 SWAPPDE (header->datadir.pe_copyright);
730 SWAPPDE (header->datadir.pe_global_ptr);
731 SWAPPDE (header->datadir.pe_tls_table);
732 SWAPPDE (header->datadir.pe_load_config_table);
733 SWAPPDE (header->datadir.pe_bound_import);
734 SWAPPDE (header->datadir.pe_iat);
735 SWAPPDE (header->datadir.pe_delay_import_desc);
736 SWAPPDE (header->datadir.pe_cli_header);
737 SWAPPDE (header->datadir.pe_reserved);
744 if (header->coff.coff_machine != 0x14c)
747 if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader) - sizeof (MonoCOFFHeader) - 4))
750 if (header->pesig[0] != 'P' || header->pesig[1] != 'E' || header->pe.pe_magic != 0x10B)
755 * The spec says that this field should contain 6.0, but Visual Studio includes a new compiler,
756 * which produces binaries with 7.0. From Sergey:
758 * The reason is that MSVC7 uses traditional compile/link
759 * sequence for CIL executables, and VS.NET (and Framework
760 * SDK) includes linker version 7, that puts 7.0 in this
761 * field. That's why it's currently not possible to load VC
762 * binaries with Mono. This field is pretty much meaningless
763 * anyway (what linker?).
765 if (header->pe.pe_major != 6 || header->pe.pe_minor != 0)
770 * FIXME: byte swap all addresses here for header.
773 if (!load_section_tables (image, iinfo, offset))
776 if (care_about_cli == FALSE) {
780 /* Load the CLI header */
781 if (!load_cli_header (image, iinfo))
784 if (!load_metadata (image, iinfo))
787 /* modules don't have an assembly table row */
788 if (image->tables [MONO_TABLE_ASSEMBLY].rows)
789 image->assembly_name = mono_metadata_string_heap (image,
790 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY],
791 0, MONO_ASSEMBLY_NAME));
793 image->module_name = mono_metadata_string_heap (image,
794 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_MODULE],
795 0, MONO_MODULE_NAME));
797 load_modules (image);
801 *status = MONO_IMAGE_OK;
806 mono_image_close (image);
811 do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
812 gboolean care_about_cli, gboolean refonly)
814 MonoCLIImageInfo *iinfo;
817 struct stat stat_buf;
819 if ((filed = fopen (fname, "rb")) == NULL){
821 *status = MONO_IMAGE_ERROR_ERRNO;
825 if (fstat (fileno (filed), &stat_buf)) {
828 *status = MONO_IMAGE_ERROR_ERRNO;
831 image = g_new0 (MonoImage, 1);
832 image->file_descr = filed;
833 image->raw_data_len = stat_buf.st_size;
834 image->raw_data = mono_raw_buffer_load (fileno (filed), FALSE, 0, stat_buf.st_size);
835 iinfo = g_new0 (MonoCLIImageInfo, 1);
836 image->image_info = iinfo;
837 image->name = mono_path_resolve_symlinks (fname);
838 image->ref_only = refonly;
839 image->ref_count = 1;
841 return do_mono_image_load (image, status, care_about_cli);
845 mono_image_loaded_full (const char *name, gboolean refonly)
848 GHashTable *loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
851 res = g_hash_table_lookup (loaded_images, name);
852 mono_images_unlock ();
858 * @name: name of the image to load
860 * This routine ensures that the given image is loaded.
862 * Returns: the loaded MonoImage, or NULL on failure.
865 mono_image_loaded (const char *name)
867 return mono_image_loaded_full (name, FALSE);
871 mono_image_loaded_by_guid_full (const char *guid, gboolean refonly)
874 GHashTable *loaded_images = refonly ? loaded_images_refonly_guid_hash : loaded_images_guid_hash;
877 res = g_hash_table_lookup (loaded_images, guid);
878 mono_images_unlock ();
883 mono_image_loaded_by_guid (const char *guid)
885 return mono_image_loaded_by_guid_full (guid, FALSE);
889 register_image (MonoImage *image)
892 GHashTable *loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
895 image2 = g_hash_table_lookup (loaded_images, image->name);
898 /* Somebody else beat us to it */
899 mono_image_addref (image2);
900 mono_images_unlock ();
901 mono_image_close (image);
904 g_hash_table_insert (loaded_images, image->name, image);
905 if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == NULL))
906 g_hash_table_insert (loaded_images, (char *) image->assembly_name, image);
907 g_hash_table_insert (image->ref_only ? loaded_images_refonly_guid_hash : loaded_images_guid_hash, image->guid, image);
908 mono_images_unlock ();
914 mono_image_open_from_data_full (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status, gboolean refonly)
916 MonoCLIImageInfo *iinfo;
920 if (!data || !data_len) {
922 *status = MONO_IMAGE_IMAGE_INVALID;
927 datac = g_try_malloc (data_len);
930 *status = MONO_IMAGE_ERROR_ERRNO;
933 memcpy (datac, data, data_len);
936 image = g_new0 (MonoImage, 1);
937 image->raw_data = datac;
938 image->raw_data_len = data_len;
939 image->raw_data_allocated = need_copy;
940 image->name = g_strdup_printf ("data-%p", datac);
941 iinfo = g_new0 (MonoCLIImageInfo, 1);
942 image->image_info = iinfo;
943 image->ref_only = refonly;
945 image = do_mono_image_load (image, status, TRUE);
949 return register_image (image);
953 mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status)
955 return mono_image_open_from_data_full (data, data_len, need_copy, status, FALSE);
959 mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean refonly)
962 GHashTable *loaded_images;
965 g_return_val_if_fail (fname != NULL, NULL);
967 absfname = mono_path_canonicalize (fname);
970 * The easiest solution would be to do all the loading inside the mutex,
971 * but that would lead to scalability problems. So we let the loading
972 * happen outside the mutex, and if multiple threads happen to load
973 * the same image, we discard all but the first copy.
976 loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
977 image = g_hash_table_lookup (loaded_images, absfname);
981 mono_image_addref (image);
982 mono_images_unlock ();
985 mono_images_unlock ();
987 image = do_mono_image_open (fname, status, TRUE, refonly);
991 return register_image (image);
996 * @fname: filename that points to the module we want to open
997 * @status: An error condition is returned in this field
999 * Returns: An open image of type %MonoImage or NULL on error.
1000 * The caller holds a temporary reference to the returned image which should be cleared
1001 * when no longer needed by calling mono_image_close ().
1002 * if NULL, then check the value of @status for details on the error
1005 mono_image_open (const char *fname, MonoImageOpenStatus *status)
1007 return mono_image_open_full (fname, status, FALSE);
1011 * mono_pe_file_open:
1012 * @fname: filename that points to the module we want to open
1013 * @status: An error condition is returned in this field
1015 * Returns: An open image of type %MonoImage or NULL on error. if
1016 * NULL, then check the value of @status for details on the error.
1017 * This variant for mono_image_open DOES NOT SET UP CLI METADATA.
1018 * It's just a PE file loader, used for FileVersionInfo. It also does
1019 * not use the image cache.
1022 mono_pe_file_open (const char *fname, MonoImageOpenStatus *status)
1024 g_return_val_if_fail (fname != NULL, NULL);
1026 return(do_mono_image_open (fname, status, FALSE, FALSE));
1030 free_hash_table (gpointer key, gpointer val, gpointer user_data)
1032 g_hash_table_destroy ((GHashTable*)val);
1037 free_mr_signatures (gpointer key, gpointer val, gpointer user_data)
1039 mono_metadata_free_method_signature ((MonoMethodSignature*)val);
1044 free_blob_cache_entry (gpointer key, gpointer val, gpointer user_data)
1050 free_remoting_wrappers (gpointer key, gpointer val, gpointer user_data)
1056 free_array_cache_entry (gpointer key, gpointer val, gpointer user_data)
1058 g_slist_free ((GSList*)val);
1062 * mono_image_addref:
1063 * @image: The image file we wish to add a reference to
1065 * Increases the reference count of an image.
1068 mono_image_addref (MonoImage *image)
1070 InterlockedIncrement (&image->ref_count);
1074 mono_dynamic_stream_reset (MonoDynamicStream* stream)
1076 stream->alloc_size = stream->index = stream->offset = 0;
1077 g_free (stream->data);
1078 stream->data = NULL;
1080 g_hash_table_destroy (stream->hash);
1081 stream->hash = NULL;
1087 * @image: The image file we wish to close
1089 * Closes an image file, deallocates all memory consumed and
1090 * unmaps all possible sections of the file
1093 mono_image_close (MonoImage *image)
1096 GHashTable *loaded_images, *loaded_images_guid;
1099 g_return_if_fail (image != NULL);
1101 if (InterlockedDecrement (&image->ref_count) > 0)
1104 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading image %s [%p].", image->name, image);
1106 mono_metadata_clean_for_image (image);
1109 * The caches inside a MonoImage might refer to metadata which is stored in referenced
1110 * assemblies, so we can't release these references in mono_assembly_close () since the
1111 * MonoImage might outlive its associated MonoAssembly.
1113 if (image->references) {
1116 for (i = 0; image->references [i]; i++) {
1117 if (image->references [i])
1118 mono_assembly_close (image->references [i]);
1121 g_free (image->references);
1122 image->references = NULL;
1125 mono_images_lock ();
1126 loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
1127 loaded_images_guid = image->ref_only ? loaded_images_refonly_guid_hash : loaded_images_guid_hash;
1128 image2 = g_hash_table_lookup (loaded_images, image->name);
1129 if (image == image2) {
1130 /* This is not true if we are called from mono_image_open () */
1131 g_hash_table_remove (loaded_images, image->name);
1132 g_hash_table_remove (loaded_images_guid, image->guid);
1134 if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == image))
1135 g_hash_table_remove (loaded_images, (char *) image->assembly_name);
1137 /* Multiple images might have the same guid */
1138 build_guid_table (image->ref_only);
1140 mono_images_unlock ();
1142 if (image->file_descr) {
1143 fclose (image->file_descr);
1144 image->file_descr = NULL;
1145 if (image->raw_data != NULL)
1146 mono_raw_buffer_free (image->raw_data);
1149 if (image->raw_data_allocated) {
1150 /* image->raw_metadata and cli_sections might lie inside image->raw_data */
1151 MonoCLIImageInfo *ii = image->image_info;
1153 if ((image->raw_metadata > image->raw_data) &&
1154 (image->raw_metadata <= (image->raw_data + image->raw_data_len)))
1155 image->raw_metadata = NULL;
1157 for (i = 0; i < ii->cli_section_count; i++)
1158 if (((char*)(ii->cli_sections [i]) > image->raw_data) &&
1159 ((char*)(ii->cli_sections [i]) <= ((char*)image->raw_data + image->raw_data_len)))
1160 ii->cli_sections [i] = NULL;
1162 g_free (image->raw_data);
1165 if (debug_assembly_unload) {
1166 image->name = g_strdup_printf ("%s - UNLOADED", image->name);
1168 g_free (image->name);
1169 g_free (image->guid);
1170 g_free (image->version);
1171 g_free (image->files);
1174 g_hash_table_destroy (image->method_cache);
1175 mono_internal_hash_table_destroy (&image->class_cache);
1176 g_hash_table_destroy (image->field_cache);
1177 if (image->array_cache) {
1178 g_hash_table_foreach (image->array_cache, free_array_cache_entry, NULL);
1179 g_hash_table_destroy (image->array_cache);
1181 if (image->ptr_cache)
1182 g_hash_table_destroy (image->ptr_cache);
1183 if (image->name_cache) {
1184 g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
1185 g_hash_table_destroy (image->name_cache);
1187 g_hash_table_destroy (image->native_wrapper_cache);
1188 g_hash_table_destroy (image->managed_wrapper_cache);
1189 g_hash_table_destroy (image->delegate_begin_invoke_cache);
1190 g_hash_table_destroy (image->delegate_end_invoke_cache);
1191 g_hash_table_destroy (image->delegate_invoke_cache);
1192 g_hash_table_foreach (image->remoting_invoke_cache, free_remoting_wrappers, NULL);
1193 g_hash_table_destroy (image->remoting_invoke_cache);
1194 g_hash_table_destroy (image->runtime_invoke_cache);
1195 g_hash_table_destroy (image->synchronized_cache);
1196 g_hash_table_destroy (image->unbox_wrapper_cache);
1197 g_hash_table_destroy (image->cominterop_invoke_cache);
1198 g_hash_table_destroy (image->cominterop_wrapper_cache);
1199 g_hash_table_destroy (image->typespec_cache);
1200 g_hash_table_destroy (image->ldfld_wrapper_cache);
1201 g_hash_table_destroy (image->ldflda_wrapper_cache);
1202 g_hash_table_destroy (image->ldfld_remote_wrapper_cache);
1203 g_hash_table_destroy (image->stfld_wrapper_cache);
1204 g_hash_table_destroy (image->stfld_remote_wrapper_cache);
1205 g_hash_table_destroy (image->isinst_cache);
1206 g_hash_table_destroy (image->castclass_cache);
1207 g_hash_table_destroy (image->proxy_isinst_cache);
1209 /* The ownership of signatures is not well defined */
1210 //g_hash_table_foreach (image->memberref_signatures, free_mr_signatures, NULL);
1211 g_hash_table_destroy (image->memberref_signatures);
1212 //g_hash_table_foreach (image->helper_signatures, free_mr_signatures, NULL);
1213 g_hash_table_destroy (image->helper_signatures);
1214 g_hash_table_destroy (image->method_signatures);
1216 if (image->interface_bitset) {
1217 mono_unload_interface_ids (image->interface_bitset);
1218 mono_bitset_free (image->interface_bitset);
1220 if (image->image_info){
1221 MonoCLIImageInfo *ii = image->image_info;
1223 if (ii->cli_section_tables)
1224 g_free (ii->cli_section_tables);
1225 if (ii->cli_sections)
1226 g_free (ii->cli_sections);
1227 g_free (image->image_info);
1230 for (i = 0; i < image->module_count; ++i) {
1231 if (image->modules [i])
1232 mono_image_close (image->modules [i]);
1235 g_free (image->modules);
1236 if (image->modules_loaded)
1237 g_free (image->modules_loaded);
1238 if (image->references)
1239 g_free (image->references);
1240 /*g_print ("destroy image %p (dynamic: %d)\n", image, image->dynamic);*/
1241 if (!image->dynamic) {
1242 if (debug_assembly_unload)
1243 mono_mempool_invalidate (image->mempool);
1245 mono_mempool_destroy (image->mempool);
1249 /* Dynamic images are GC_MALLOCed */
1250 struct _MonoDynamicImage *di = (struct _MonoDynamicImage*)image;
1252 g_free ((char*)image->module_name);
1254 g_hash_table_destroy (di->typespec);
1256 g_hash_table_destroy (di->typeref);
1258 g_hash_table_destroy (di->handleref);
1260 mono_g_hash_table_destroy (di->tokens);
1261 if (di->blob_cache) {
1262 g_hash_table_foreach (di->blob_cache, free_blob_cache_entry, NULL);
1263 g_hash_table_destroy (di->blob_cache);
1265 g_list_free (di->array_methods);
1267 g_ptr_array_free (di->gen_params, TRUE);
1268 if (di->token_fixups)
1269 mono_g_hash_table_destroy (di->token_fixups);
1270 if (di->method_to_table_idx)
1271 g_hash_table_destroy (di->method_to_table_idx);
1272 if (di->field_to_table_idx)
1273 g_hash_table_destroy (di->field_to_table_idx);
1274 if (di->method_aux_hash)
1275 g_hash_table_destroy (di->method_aux_hash);
1276 g_free (di->strong_name);
1277 g_free (di->win32_res);
1278 /*g_print ("string heap destroy for image %p\n", di);*/
1279 mono_dynamic_stream_reset (&di->sheap);
1280 mono_dynamic_stream_reset (&di->code);
1281 mono_dynamic_stream_reset (&di->resources);
1282 mono_dynamic_stream_reset (&di->us);
1283 mono_dynamic_stream_reset (&di->blob);
1284 mono_dynamic_stream_reset (&di->tstream);
1285 mono_dynamic_stream_reset (&di->guid);
1286 for (i = 0; i < MONO_TABLE_NUM; ++i) {
1287 g_free (di->tables [i].values);
1289 mono_mempool_destroy (image->mempool);
1294 * mono_image_strerror:
1295 * @status: an code indicating the result from a recent operation
1297 * Returns: a string describing the error
1300 mono_image_strerror (MonoImageOpenStatus status)
1305 case MONO_IMAGE_ERROR_ERRNO:
1306 return strerror (errno);
1307 case MONO_IMAGE_IMAGE_INVALID:
1308 return "File does not contain a valid CIL image";
1309 case MONO_IMAGE_MISSING_ASSEMBLYREF:
1310 return "An assembly was referenced, but could not be found";
1312 return "Internal error";
1316 mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
1317 guint32 lang_id, gunichar2 *name,
1318 MonoPEResourceDirEntry *entry,
1319 MonoPEResourceDir *root, guint32 level)
1321 gboolean is_string, is_dir;
1322 guint32 name_offset, dir_offset;
1324 /* Level 0 holds a directory entry for each type of resource
1325 * (identified by ID or name).
1327 * Level 1 holds a directory entry for each named resource
1328 * item, and each "anonymous" item of a particular type of
1331 * Level 2 holds a directory entry for each language pointing to
1334 name_offset = GUINT32_FROM_LE (entry->name_offset) & 0x7fffffff;
1335 dir_offset = GUINT32_FROM_LE (entry->dir_offset) & 0x7fffffff;
1337 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
1338 is_string = (GUINT32_FROM_LE (entry->name_offset) & 0x80000000) != 0;
1339 is_dir = (GUINT32_FROM_LE (entry->dir_offset) & 0x80000000) != 0;
1341 is_string = entry->name_is_string;
1342 is_dir = entry->is_dir;
1346 if((is_string==FALSE && name_offset!=res_id) ||
1347 (is_string==TRUE)) {
1350 } else if (level==1) {
1353 is_string==TRUE && name!=lookup (name_offset)) {
1357 } else if (level==2) {
1358 if ((is_string == FALSE &&
1359 name_offset != lang_id &&
1361 (is_string == TRUE)) {
1365 g_assert_not_reached ();
1369 MonoPEResourceDir *res_dir=(MonoPEResourceDir *)(((char *)root)+dir_offset);
1370 MonoPEResourceDirEntry *sub_entries=(MonoPEResourceDirEntry *)(res_dir+1);
1373 entries = GUINT16_FROM_LE (res_dir->res_named_entries) + GUINT16_FROM_LE (res_dir->res_id_entries);
1375 for(i=0; i<entries; i++) {
1376 MonoPEResourceDirEntry *sub_entry=&sub_entries[i];
1379 ret=mono_image_walk_resource_tree (info, res_id,
1390 MonoPEResourceDataEntry *data_entry=(MonoPEResourceDataEntry *)((char *)(root)+dir_offset);
1391 MonoPEResourceDataEntry *res;
1393 res = g_new0 (MonoPEResourceDataEntry, 1);
1395 res->rde_data_offset = GUINT32_TO_LE (data_entry->rde_data_offset);
1396 res->rde_size = GUINT32_TO_LE (data_entry->rde_size);
1397 res->rde_codepage = GUINT32_TO_LE (data_entry->rde_codepage);
1398 res->rde_reserved = GUINT32_TO_LE (data_entry->rde_reserved);
1405 * mono_image_lookup_resource:
1406 * @image: the image to look up the resource in
1407 * @res_id: A MONO_PE_RESOURCE_ID_ that represents the resource ID to lookup.
1408 * @lang_id: The language id.
1409 * @name: the resource name to lookup.
1411 * Returns: NULL if not found, otherwise a pointer to the in-memory representation
1412 * of the given resource. The caller should free it using g_free () when no longer
1416 mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name)
1418 MonoCLIImageInfo *info;
1419 MonoDotNetHeader *header;
1420 MonoPEDatadir *datadir;
1421 MonoPEDirEntry *rsrc;
1422 MonoPEResourceDir *resource_dir;
1423 MonoPEResourceDirEntry *res_entries;
1430 info=image->image_info;
1435 header=&info->cli_header;
1440 datadir=&header->datadir;
1445 rsrc=&datadir->pe_resource_table;
1450 resource_dir=(MonoPEResourceDir *)mono_image_rva_map (image, rsrc->rva);
1451 if(resource_dir==NULL) {
1455 entries = GUINT16_FROM_LE (resource_dir->res_named_entries) + GUINT16_FROM_LE (resource_dir->res_id_entries);
1456 res_entries=(MonoPEResourceDirEntry *)(resource_dir+1);
1458 for(i=0; i<entries; i++) {
1459 MonoPEResourceDirEntry *entry=&res_entries[i];
1462 ret=mono_image_walk_resource_tree (info, res_id, lang_id,
1463 name, entry, resource_dir,
1474 * mono_image_get_entry_point:
1475 * @image: the image where the entry point will be looked up.
1477 * Use this routine to determine the metadata token for method that
1478 * has been flagged as the entry point.
1480 * Returns: the token for the entry point method in the image
1483 mono_image_get_entry_point (MonoImage *image)
1485 return ((MonoCLIImageInfo*)image->image_info)->cli_cli_header.ch_entry_point;
1489 * mono_image_get_resource:
1490 * @image: the image where the resource will be looked up.
1491 * @offset: The offset to add to the resource
1492 * @size: a pointer to an int where the size of the resource will be stored
1494 * This is a low-level routine that fetches a resource from the
1495 * metadata that starts at a given @offset. The @size parameter is
1496 * filled with the data field as encoded in the metadata.
1498 * Returns: the pointer to the resource whose offset is @offset.
1501 mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)
1503 MonoCLIImageInfo *iinfo = image->image_info;
1504 MonoCLIHeader *ch = &iinfo->cli_cli_header;
1507 if (!ch->ch_resources.rva || offset + 4 > ch->ch_resources.size)
1510 data = mono_image_rva_map (image, ch->ch_resources.rva);
1515 *size = read32 (data);
1521 mono_image_load_file_for_image (MonoImage *image, int fileidx)
1523 char *base_dir, *name;
1525 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
1529 if (fileidx < 1 || fileidx > t->rows)
1532 if (image->files && image->files [fileidx - 1])
1533 return image->files [fileidx - 1];
1536 image->files = g_new0 (MonoImage*, t->rows);
1538 fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
1539 fname = mono_metadata_string_heap (image, fname_id);
1540 base_dir = g_path_get_dirname (image->name);
1541 name = g_build_filename (base_dir, fname, NULL);
1542 res = mono_image_open (name, NULL);
1545 /* g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly); */
1546 res->assembly = image->assembly;
1547 for (i = 0; i < res->module_count; ++i) {
1548 if (res->modules [i] && !res->modules [i]->assembly)
1549 res->modules [i]->assembly = image->assembly;
1552 image->files [fileidx - 1] = res;
1560 * mono_image_get_strong_name:
1561 * @image: a MonoImage
1562 * @size: a guint32 pointer, or NULL.
1564 * If the image has a strong name, and @size is not NULL, the value
1565 * pointed to by size will have the size of the strong name.
1567 * Returns: NULL if the image does not have a strong name, or a
1568 * pointer to the public key.
1571 mono_image_get_strong_name (MonoImage *image, guint32 *size)
1573 MonoCLIImageInfo *iinfo = image->image_info;
1574 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
1577 if (!de->size || !de->rva)
1579 data = mono_image_rva_map (image, de->rva);
1588 * mono_image_strong_name_position:
1589 * @image: a MonoImage
1590 * @size: a guint32 pointer, or NULL.
1592 * If the image has a strong name, and @size is not NULL, the value
1593 * pointed to by size will have the size of the strong name.
1595 * Returns: the position within the image file where the strong name
1599 mono_image_strong_name_position (MonoImage *image, guint32 *size)
1601 MonoCLIImageInfo *iinfo = image->image_info;
1602 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
1603 const int top = iinfo->cli_section_count;
1604 MonoSectionTable *tables = iinfo->cli_section_tables;
1606 guint32 addr = de->rva;
1610 if (!de->size || !de->rva)
1612 for (i = 0; i < top; i++){
1613 if ((addr >= tables->st_virtual_address) &&
1614 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
1615 return tables->st_raw_data_ptr +
1616 (addr - tables->st_virtual_address);
1625 * mono_image_get_public_key:
1626 * @image: a MonoImage
1627 * @size: a guint32 pointer, or NULL.
1629 * This is used to obtain the public key in the @image.
1631 * If the image has a public key, and @size is not NULL, the value
1632 * pointed to by size will have the size of the public key.
1634 * Returns: NULL if the image does not have a public key, or a pointer
1635 * to the public key.
1638 mono_image_get_public_key (MonoImage *image, guint32 *size)
1642 if (image->tables [MONO_TABLE_ASSEMBLY].rows != 1)
1644 tok = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY], 0, MONO_ASSEMBLY_PUBLIC_KEY);
1647 pubkey = mono_metadata_blob_heap (image, tok);
1648 len = mono_metadata_decode_blob_size (pubkey, &pubkey);
1655 * mono_image_get_name:
1656 * @name: a MonoImage
1658 * Returns: the name of the assembly.
1661 mono_image_get_name (MonoImage *image)
1663 return image->assembly_name;
1667 * mono_image_get_filename:
1668 * @image: a MonoImage
1670 * Used to get the filename that hold the actual MonoImage
1672 * Returns: the filename.
1675 mono_image_get_filename (MonoImage *image)
1681 mono_image_get_guid (MonoImage *image)
1686 const MonoTableInfo*
1687 mono_image_get_table_info (MonoImage *image, int table_id)
1689 if (table_id < 0 || table_id >= MONO_TABLE_NUM)
1691 return &image->tables [table_id];
1695 mono_image_get_table_rows (MonoImage *image, int table_id)
1697 if (table_id < 0 || table_id >= MONO_TABLE_NUM)
1699 return image->tables [table_id].rows;
1703 mono_table_info_get_rows (const MonoTableInfo *table)
1709 * mono_image_get_assembly:
1710 * @image: the MonoImage.
1712 * Use this routine to get the assembly that owns this image.
1714 * Returns: the assembly that holds this image.
1717 mono_image_get_assembly (MonoImage *image)
1719 return image->assembly;
1723 * mono_image_is_dynamic:
1724 * @image: the MonoImage
1726 * Determines if the given image was created dynamically through the
1727 * System.Reflection.Emit API
1729 * Returns: TRUE if the image was created dynamically, FALSE if not.
1732 mono_image_is_dynamic (MonoImage *image)
1734 return image->dynamic;
1738 * mono_image_has_authenticode_entry:
1739 * @image: the MonoImage
1741 * Use this routine to determine if the image has a Authenticode
1742 * Certificate Table.
1744 * Returns: TRUE if the image contains an authenticode entry in the PE
1748 mono_image_has_authenticode_entry (MonoImage *image)
1750 MonoCLIImageInfo *iinfo = image->image_info;
1751 MonoDotNetHeader *header = &iinfo->cli_header;
1752 MonoPEDirEntry *de = &header->datadir.pe_certificate_table;
1753 // the Authenticode "pre" (non ASN.1) header is 8 bytes long
1754 return ((de->rva != 0) && (de->size > 8));