2 * image.c: Routines for manipulating an image stored in an
3 * extended PE/COFF file.
6 * Miguel de Icaza (miguel@ximian.com)
8 * (C) 2001 Ximian, Inc. http://www.ximian.com
11 * Implement big-endian versions of the reading routines.
21 #include "rawbuffer.h"
22 #include "mono-endian.h"
24 #include "tabledefs.h"
25 #include "tokentype.h"
27 #define INVALID_ADDRESS 0xffffffff
30 * Keeps track of the various assemblies loaded
32 static GHashTable *loaded_images_hash;
33 static GHashTable *loaded_images_guid_hash;
36 mono_cli_rva_image_map (MonoCLIImageInfo *iinfo, guint32 addr)
38 const int top = iinfo->cli_section_count;
39 MonoSectionTable *tables = iinfo->cli_section_tables;
42 for (i = 0; i < top; i++){
43 if ((addr >= tables->st_virtual_address) &&
44 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
45 return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
49 return INVALID_ADDRESS;
53 mono_cli_rva_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 (char*)iinfo->cli_sections [i] +
63 (addr - tables->st_virtual_address);
71 * mono_image_ensure_section_idx:
72 * @image: The image we are operating on
73 * @section: section number that we will load/map into memory
75 * This routine makes sure that we have an in-memory copy of
76 * an image section (.text, .rsrc, .data).
78 * Returns: TRUE on success
81 mono_image_ensure_section_idx (MonoImage *image, int section)
83 MonoCLIImageInfo *iinfo = image->image_info;
84 MonoSectionTable *sect;
87 g_return_val_if_fail (section < iinfo->cli_section_count, FALSE);
89 if (iinfo->cli_sections [section] != NULL)
92 sect = &iinfo->cli_section_tables [section];
94 writable = sect->st_flags & SECT_FLAGS_MEM_WRITE;
96 iinfo->cli_sections [section] = mono_raw_buffer_load (
97 fileno (image->f), writable,
98 sect->st_raw_data_ptr, sect->st_raw_data_size);
100 if (iinfo->cli_sections [section] == NULL)
107 * mono_image_ensure_section:
108 * @image: The image we are operating on
109 * @section: section name that we will load/map into memory
111 * This routine makes sure that we have an in-memory copy of
112 * an image section (.text, .rsrc, .data).
114 * Returns: TRUE on success
117 mono_image_ensure_section (MonoImage *image, const char *section)
119 MonoCLIImageInfo *ii = image->image_info;
122 for (i = 0; i < ii->cli_section_count; i++){
123 if (strncmp (ii->cli_section_tables [i].st_name, section, 8) != 0)
126 return mono_image_ensure_section_idx (image, i);
132 load_section_tables (MonoImage *image, MonoCLIImageInfo *iinfo)
134 const int top = iinfo->cli_header.coff.coff_sections;
137 iinfo->cli_section_count = top;
138 iinfo->cli_section_tables = g_new0 (MonoSectionTable, top);
139 iinfo->cli_sections = g_new0 (void *, top);
141 for (i = 0; i < top; i++){
142 MonoSectionTable *t = &iinfo->cli_section_tables [i];
144 if (fread (t, sizeof (MonoSectionTable), 1, image->f) != 1)
147 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
148 t->st_virtual_size = GUINT32_FROM_LE (t->st_virtual_size);
149 t->st_virtual_address = GUINT32_FROM_LE (t->st_virtual_address);
150 t->st_raw_data_size = GUINT32_FROM_LE (t->st_raw_data_size);
151 t->st_raw_data_ptr = GUINT32_FROM_LE (t->st_raw_data_ptr);
152 t->st_reloc_ptr = GUINT32_FROM_LE (t->st_reloc_ptr);
153 t->st_lineno_ptr = GUINT32_FROM_LE (t->st_lineno_ptr);
154 t->st_reloc_count = GUINT16_FROM_LE (t->st_reloc_count);
155 t->st_line_count = GUINT16_FROM_LE (t->st_line_count);
156 t->st_flags = GUINT32_FROM_LE (t->st_flags);
158 /* consistency checks here */
161 for (i = 0; i < top; i++)
162 if (!mono_image_ensure_section_idx (image, i))
169 load_cli_header (MonoImage *image, MonoCLIImageInfo *iinfo)
174 offset = mono_cli_rva_image_map (iinfo, iinfo->cli_header.datadir.pe_cli_header.rva);
175 if (offset == INVALID_ADDRESS)
178 if (fseek (image->f, offset, SEEK_SET) != 0)
181 if ((n = fread (&iinfo->cli_cli_header, sizeof (MonoCLIHeader), 1, image->f)) != 1)
184 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
185 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
186 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
187 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
188 SWAP32 (iinfo->cli_cli_header.ch_size);
189 SWAP32 (iinfo->cli_cli_header.ch_flags);
190 SWAP32 (iinfo->cli_cli_header.ch_entry_point);
191 SWAP16 (iinfo->cli_cli_header.ch_runtime_major);
192 SWAP16 (iinfo->cli_cli_header.ch_runtime_minor);
193 SWAPPDE (iinfo->cli_cli_header.ch_metadata);
194 SWAPPDE (iinfo->cli_cli_header.ch_resources);
195 SWAPPDE (iinfo->cli_cli_header.ch_strong_name);
196 SWAPPDE (iinfo->cli_cli_header.ch_code_manager_table);
197 SWAPPDE (iinfo->cli_cli_header.ch_vtable_fixups);
198 SWAPPDE (iinfo->cli_cli_header.ch_export_address_table_jumps);
199 SWAPPDE (iinfo->cli_cli_header.ch_eeinfo_table);
200 SWAPPDE (iinfo->cli_cli_header.ch_helper_table);
201 SWAPPDE (iinfo->cli_cli_header.ch_dynamic_info);
202 SWAPPDE (iinfo->cli_cli_header.ch_delay_load_info);
203 SWAPPDE (iinfo->cli_cli_header.ch_module_image);
204 SWAPPDE (iinfo->cli_cli_header.ch_external_fixups);
205 SWAPPDE (iinfo->cli_cli_header.ch_ridmap);
206 SWAPPDE (iinfo->cli_cli_header.ch_debug_map);
207 SWAPPDE (iinfo->cli_cli_header.ch_ip_map);
212 /* Catch new uses of the fields that are supposed to be zero */
214 if ((iinfo->cli_cli_header.ch_eeinfo_table.rva != 0) ||
215 (iinfo->cli_cli_header.ch_helper_table.rva != 0) ||
216 (iinfo->cli_cli_header.ch_dynamic_info.rva != 0) ||
217 (iinfo->cli_cli_header.ch_delay_load_info.rva != 0) ||
218 (iinfo->cli_cli_header.ch_module_image.rva != 0) ||
219 (iinfo->cli_cli_header.ch_external_fixups.rva != 0) ||
220 (iinfo->cli_cli_header.ch_ridmap.rva != 0) ||
221 (iinfo->cli_cli_header.ch_debug_map.rva != 0) ||
222 (iinfo->cli_cli_header.ch_ip_map.rva != 0)){
225 * No need to scare people who are testing this, I am just
226 * labelling this as a LAMESPEC
228 /* g_warning ("Some fields in the CLI header which should have been zero are not zero"); */
236 load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
238 guint32 offset, size;
244 offset = mono_cli_rva_image_map (iinfo, iinfo->cli_cli_header.ch_metadata.rva);
245 size = iinfo->cli_cli_header.ch_metadata.size;
247 image->raw_metadata = mono_raw_buffer_load (fileno (image->f), FALSE, offset, size);
248 if (image->raw_metadata == NULL)
251 ptr = image->raw_metadata;
253 if (strncmp (ptr, "BSJB", 4) == 0){
254 guint32 version_string_len;
257 version_string_len = read32 (ptr);
259 ptr += version_string_len;
260 pad = ptr - image->raw_metadata;
262 ptr += 4 - (pad % 4);
266 /* skip over flags */
269 streams = read16 (ptr);
272 for (i = 0; i < streams; i++){
273 if (strncmp (ptr + 8, "#~", 3) == 0){
274 image->heap_tables.data = image->raw_metadata + read32 (ptr);
275 image->heap_tables.size = read32 (ptr + 4);
277 } else if (strncmp (ptr + 8, "#Strings", 9) == 0){
278 image->heap_strings.data = image->raw_metadata + read32 (ptr);
279 image->heap_strings.size = read32 (ptr + 4);
281 } else if (strncmp (ptr + 8, "#US", 4) == 0){
282 image->heap_us.data = image->raw_metadata + read32 (ptr);
283 image->heap_us.size = read32 (ptr + 4);
285 } else if (strncmp (ptr + 8, "#Blob", 6) == 0){
286 image->heap_blob.data = image->raw_metadata + read32 (ptr);
287 image->heap_blob.size = read32 (ptr + 4);
289 } else if (strncmp (ptr + 8, "#GUID", 6) == 0){
290 image->heap_guid.data = image->raw_metadata + read32 (ptr);
291 image->heap_guid.size = read32 (ptr + 4);
293 } else if (strncmp (ptr + 8, "#-", 3) == 0) {
294 g_print ("Assembly '%s' has the non-standard metadata heap #-.\nRecompile it correctly (without the /incremental switch or in Release mode).", image->name);
297 g_message ("Unknown heap type: %s\n", ptr + 8);
298 ptr += 8 + strlen (ptr) + 1;
300 pad = ptr - image->raw_metadata;
302 ptr += 4 - (pad % 4);
305 g_assert (image->heap_guid.data);
306 g_assert (image->heap_guid.size >= 16);
308 image->guid = mono_guid_to_string (image->heap_guid.data);
314 * Load representation of logical metadata tables, from the "#~" stream
317 load_tables (MonoImage *image)
319 const char *heap_tables = image->heap_tables.data;
321 guint64 valid_mask, sorted_mask;
322 int valid = 0, table;
325 heap_sizes = heap_tables [6];
326 image->idx_string_wide = ((heap_sizes & 0x01) == 1);
327 image->idx_guid_wide = ((heap_sizes & 0x02) == 2);
328 image->idx_blob_wide = ((heap_sizes & 0x04) == 4);
330 valid_mask = read64 (heap_tables + 8);
331 sorted_mask = read64 (heap_tables + 16);
332 rows = (const guint32 *) (heap_tables + 24);
334 for (table = 0; table < 64; table++){
335 if ((valid_mask & ((guint64) 1 << table)) == 0){
336 image->tables [table].rows = 0;
340 g_warning("bits in valid must be zero above 0x2b (II - 23.1.6)");
342 /*if ((sorted_mask & ((guint64) 1 << table)) == 0){
343 g_print ("table %s (0x%02x) is sorted\n", mono_meta_table_name (table), table);
345 image->tables [table].rows = read32 (rows);
350 image->tables_base = (heap_tables + 24) + (4 * valid);
352 /* They must be the same */
353 g_assert ((const void *) image->tables_base == (const void *) rows);
355 mono_metadata_compute_table_bases (image);
360 load_metadata (MonoImage *image, MonoCLIImageInfo *iinfo)
362 if (!load_metadata_ptrs (image, iinfo))
365 return load_tables (image);
369 mono_image_add_to_name_cache (MonoImage *image, const char *nspace,
370 const char *name, guint32 index)
372 GHashTable *nspace_table;
373 GHashTable *name_cache = image->name_cache;
375 if (!(nspace_table = g_hash_table_lookup (name_cache, nspace))) {
376 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
377 g_hash_table_insert (name_cache, (char *)nspace, (char *)nspace_table);
379 g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (index));
383 load_modules (MonoImage *image, MonoImageOpenStatus *status)
392 t = &image->tables [MONO_TABLE_MODULEREF];
393 image->modules = g_new0 (MonoImage *, t->rows);
394 base_dir = g_path_get_dirname (image->name);
395 for (i = 0; i < t->rows; i++){
398 guint32 cols [MONO_MODULEREF_SIZE];
400 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
401 name = mono_metadata_string_heap (image, cols [MONO_MODULEREF_NAME]);
402 module_ref = g_build_filename (base_dir, name, NULL);
403 image->modules [i] = mono_image_open (module_ref, status);
404 if (image->modules [i]) {
405 image->modules [i]->assembly = image->assembly;
406 //g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly);
409 * FIXME: what do we do here? it could be a native dll...
410 * We should probably do lazy-loading of modules.
413 *status = MONO_IMAGE_OK;
420 load_class_names (MonoImage *image)
422 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
423 guint32 cols [MONO_TYPEDEF_SIZE];
428 for (i = 1; i <= t->rows; ++i) {
429 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
430 /* nested types are accessed from the nesting name */
431 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
432 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
434 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
435 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
436 mono_image_add_to_name_cache (image, nspace, name, i);
441 do_mono_image_open (const char *fname, MonoImageOpenStatus *status)
443 MonoCLIImageInfo *iinfo;
444 MonoDotNetHeader *header;
445 MonoMSDOSHeader msdos;
449 image = g_new0 (MonoImage, 1);
450 image->ref_count = 1;
451 image->f = fopen (fname, "rb");
452 image->name = g_strdup (fname);
453 iinfo = g_new0 (MonoCLIImageInfo, 1);
454 image->image_info = iinfo;
456 image->method_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
457 image->class_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
458 image->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
459 image->array_cache = g_hash_table_new ((GHashFunc)mono_metadata_type_hash,
460 (GCompareFunc)mono_metadata_type_equal);
462 image->delegate_begin_invoke_cache =
463 g_hash_table_new ((GHashFunc)mono_signature_hash,
464 (GCompareFunc)mono_metadata_signature_equal);
465 image->delegate_end_invoke_cache =
466 g_hash_table_new ((GHashFunc)mono_signature_hash,
467 (GCompareFunc)mono_metadata_signature_equal);
468 image->delegate_invoke_cache =
469 g_hash_table_new ((GHashFunc)mono_signature_hash,
470 (GCompareFunc)mono_metadata_signature_equal);
472 image->runtime_invoke_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
473 image->managed_wrapper_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
474 image->native_wrapper_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
475 image->remoting_invoke_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
477 header = &iinfo->cli_header;
479 if (image->f == NULL){
481 *status = MONO_IMAGE_ERROR_ERRNO;
482 mono_image_close (image);
487 *status = MONO_IMAGE_IMAGE_INVALID;
489 if (fread (&msdos, sizeof (msdos), 1, image->f) != 1)
492 if (!(msdos.msdos_sig [0] == 'M' && msdos.msdos_sig [1] == 'Z'))
495 msdos.pe_offset = GUINT32_FROM_LE (msdos.pe_offset);
497 if (msdos.pe_offset != sizeof (msdos))
498 fseek (image->f, msdos.pe_offset, SEEK_SET);
500 if ((n = fread (header, sizeof (MonoDotNetHeader), 1, image->f)) != 1)
503 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
504 #define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
505 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
506 #define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
507 SWAP32 (header->coff.coff_time);
508 SWAP32 (header->coff.coff_symptr);
509 SWAP32 (header->coff.coff_symcount);
510 SWAP16 (header->coff.coff_machine);
511 SWAP16 (header->coff.coff_sections);
512 SWAP16 (header->coff.coff_opt_header_size);
513 SWAP16 (header->coff.coff_attributes);
515 SWAP32 (header->pe.pe_code_size);
516 SWAP32 (header->pe.pe_data_size);
517 SWAP32 (header->pe.pe_uninit_data_size);
518 SWAP32 (header->pe.pe_rva_entry_point);
519 SWAP32 (header->pe.pe_rva_code_base);
520 SWAP32 (header->pe.pe_rva_data_base);
521 SWAP16 (header->pe.pe_magic);
523 /* MonoPEHeaderNT: not used yet */
524 SWAP32 (header->nt.pe_image_base); /* must be 0x400000 */
525 SWAP32 (header->nt.pe_section_align); /* must be 8192 */
526 SWAP32 (header->nt.pe_file_alignment); /* must be 512 or 4096 */
527 SWAP16 (header->nt.pe_os_major); /* must be 4 */
528 SWAP16 (header->nt.pe_os_minor); /* must be 0 */
529 SWAP16 (header->nt.pe_user_major);
530 SWAP16 (header->nt.pe_user_minor);
531 SWAP16 (header->nt.pe_subsys_major);
532 SWAP16 (header->nt.pe_subsys_minor);
533 SWAP32 (header->nt.pe_reserved_1);
534 SWAP32 (header->nt.pe_image_size);
535 SWAP32 (header->nt.pe_header_size);
536 SWAP32 (header->nt.pe_checksum);
537 SWAP16 (header->nt.pe_subsys_required);
538 SWAP16 (header->nt.pe_dll_flags);
539 SWAP32 (header->nt.pe_stack_reserve);
540 SWAP32 (header->nt.pe_stack_commit);
541 SWAP32 (header->nt.pe_heap_reserve);
542 SWAP32 (header->nt.pe_heap_commit);
543 SWAP32 (header->nt.pe_loader_flags);
544 SWAP32 (header->nt.pe_data_dir_count);
546 /* MonoDotNetHeader: mostly unused */
547 SWAPPDE (header->datadir.pe_export_table);
548 SWAPPDE (header->datadir.pe_import_table);
549 SWAPPDE (header->datadir.pe_resource_table);
550 SWAPPDE (header->datadir.pe_exception_table);
551 SWAPPDE (header->datadir.pe_certificate_table);
552 SWAPPDE (header->datadir.pe_reloc_table);
553 SWAPPDE (header->datadir.pe_debug);
554 SWAPPDE (header->datadir.pe_copyright);
555 SWAPPDE (header->datadir.pe_global_ptr);
556 SWAPPDE (header->datadir.pe_tls_table);
557 SWAPPDE (header->datadir.pe_load_config_table);
558 SWAPPDE (header->datadir.pe_bound_import);
559 SWAPPDE (header->datadir.pe_iat);
560 SWAPPDE (header->datadir.pe_delay_import_desc);
561 SWAPPDE (header->datadir.pe_cli_header);
562 SWAPPDE (header->datadir.pe_reserved);
569 if (header->coff.coff_machine != 0x14c)
572 if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader) - sizeof (MonoCOFFHeader) - 4))
575 if (header->pesig[0] != 'P' || header->pesig[1] != 'E' || header->pe.pe_magic != 0x10B)
580 * The spec says that this field should contain 6.0, but Visual Studio includes a new compiler,
581 * which produces binaries with 7.0. From Sergey:
583 * The reason is that MSVC7 uses traditional compile/link
584 * sequence for CIL executables, and VS.NET (and Framework
585 * SDK) includes linker version 7, that puts 7.0 in this
586 * field. That's why it's currently not possible to load VC
587 * binaries with Mono. This field is pretty much meaningless
588 * anyway (what linker?).
590 if (header->pe.pe_major != 6 || header->pe.pe_minor != 0)
595 * FIXME: byte swap all addresses here for header.
598 if (!load_section_tables (image, iinfo))
601 /* Load the CLI header */
602 if (!load_cli_header (image, iinfo))
605 if (!load_metadata (image, iinfo))
608 load_class_names (image);
610 /* modules don't have an assembly table row */
611 if (image->tables [MONO_TABLE_ASSEMBLY].rows)
612 image->assembly_name = mono_metadata_string_heap (image,
613 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY],
614 0, MONO_ASSEMBLY_NAME));
616 image->module_name = mono_metadata_string_heap (image,
617 mono_metadata_decode_row_col (&image->tables [MONO_TABLE_MODULE],
618 0, MONO_MODULE_NAME));
620 load_modules (image, status);
623 *status = MONO_IMAGE_OK;
630 mono_image_close (image);
635 mono_image_loaded (const char *name) {
636 if (strcmp (name, "mscorlib") == 0)
638 if (loaded_images_hash)
639 return g_hash_table_lookup (loaded_images_hash, name);
644 mono_image_loaded_by_guid (const char *guid) {
645 if (loaded_images_guid_hash)
646 return g_hash_table_lookup (loaded_images_guid_hash, guid);
652 * @fname: filename that points to the module we want to open
653 * @status: An error condition is returned in this field
655 * Retuns: An open image of type %MonoImage or NULL on error.
656 * if NULL, then check the value of @status for details on the error
659 mono_image_open (const char *fname, MonoImageOpenStatus *status)
663 g_return_val_if_fail (fname != NULL, NULL);
665 if (loaded_images_hash){
666 image = g_hash_table_lookup (loaded_images_hash, fname);
673 image = do_mono_image_open (fname, status);
677 if (!loaded_images_hash)
678 loaded_images_hash = g_hash_table_new (g_str_hash, g_str_equal);
679 g_hash_table_insert (loaded_images_hash, image->name, image);
680 if (image->assembly_name)
681 g_hash_table_insert (loaded_images_hash, (char *) image->assembly_name, image);
683 if (!loaded_images_guid_hash)
684 loaded_images_guid_hash = g_hash_table_new (g_str_hash, g_str_equal);
685 g_hash_table_insert (loaded_images_guid_hash, image->guid, image);
691 free_hash_table(gpointer key, gpointer val, gpointer user_data)
693 g_hash_table_destroy ((GHashTable*)val);
698 * @image: The image file we wish to close
700 * Closes an image file, deallocates all memory consumed and
701 * unmaps all possible sections of the file
704 mono_image_close (MonoImage *image)
706 g_return_if_fail (image != NULL);
708 if (--image->ref_count)
711 if (!loaded_images_hash)
712 loaded_images_hash = g_hash_table_new (g_str_hash, g_str_equal);
713 g_hash_table_remove (loaded_images_hash, image->name);
718 g_free (image->name);
720 g_hash_table_destroy (image->method_cache);
721 g_hash_table_destroy (image->class_cache);
722 g_hash_table_destroy (image->array_cache);
723 g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
724 g_hash_table_destroy (image->name_cache);
725 g_hash_table_destroy (image->native_wrapper_cache);
726 g_hash_table_destroy (image->managed_wrapper_cache);
727 g_hash_table_destroy (image->delegate_begin_invoke_cache);
728 g_hash_table_destroy (image->delegate_end_invoke_cache);
729 g_hash_table_destroy (image->delegate_invoke_cache);
730 g_hash_table_destroy (image->remoting_invoke_cache);
731 g_hash_table_destroy (image->runtime_invoke_cache);
733 if (image->raw_metadata != NULL)
734 mono_raw_buffer_free (image->raw_metadata);
736 if (image->image_info){
737 MonoCLIImageInfo *ii = image->image_info;
740 for (i = 0; i < ii->cli_section_count; i++){
741 if (!ii->cli_sections [i])
743 mono_raw_buffer_free (ii->cli_sections [i]);
745 if (ii->cli_section_tables)
746 g_free (ii->cli_section_tables);
747 if (ii->cli_sections)
748 g_free (ii->cli_sections);
749 g_free (image->image_info);
756 * mono_image_strerror:
757 * @status: an code indicating the result from a recent operation
759 * Returns: a string describing the error
762 mono_image_strerror (MonoImageOpenStatus status)
767 case MONO_IMAGE_ERROR_ERRNO:
768 return strerror (errno);
769 case MONO_IMAGE_IMAGE_INVALID:
770 return "File does not contain a valid CIL image";
771 case MONO_IMAGE_MISSING_ASSEMBLYREF:
772 return "An assembly was referenced, but could not be found";
774 return "Internal error";
778 mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
779 guint32 lang_id, gunichar2 *name,
780 MonoPEResourceDirEntry *entry,
781 MonoPEResourceDir *root, guint32 level)
783 gboolean is_string=entry->name_is_string;
785 /* Level 0 holds a directory entry for each type of resource
786 * (identified by ID or name).
788 * Level 1 holds a directory entry for each named resource
789 * item, and each "anonymous" item of a particular type of
792 * Level 2 holds a directory entry for each language pointing to
797 if((is_string==FALSE && entry->name_offset!=res_id) ||
801 } else if (level==1) {
804 is_string==TRUE && name!=lookup (entry->name_offset)) {
808 } else if (level==2) {
809 if((is_string==FALSE && entry->name_offset!=lang_id) ||
814 g_assert_not_reached ();
817 if(entry->is_dir==TRUE) {
818 MonoPEResourceDir *res_dir=(MonoPEResourceDir *)(((char *)root)+entry->dir_offset);
819 MonoPEResourceDirEntry *sub_entries=(MonoPEResourceDirEntry *)(res_dir+1);
822 entries=res_dir->res_named_entries + res_dir->res_id_entries;
824 for(i=0; i<entries; i++) {
825 MonoPEResourceDirEntry *sub_entry=&sub_entries[i];
828 ret=mono_image_walk_resource_tree (info, res_id,
839 MonoPEResourceDataEntry *data_entry=(MonoPEResourceDataEntry *)((char *)(root)+entry->dir_offset);
846 mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name)
848 MonoCLIImageInfo *info;
849 MonoDotNetHeader *header;
850 MonoPEDatadir *datadir;
851 MonoPEDirEntry *rsrc;
852 MonoPEResourceDir *resource_dir;
853 MonoPEResourceDirEntry *res_entries;
860 info=image->image_info;
865 header=&info->cli_header;
870 datadir=&header->datadir;
875 rsrc=&datadir->pe_resource_table;
880 resource_dir=(MonoPEResourceDir *)mono_cli_rva_map (info, rsrc->rva);
881 if(resource_dir==NULL) {
885 entries=resource_dir->res_named_entries + resource_dir->res_id_entries;
886 res_entries=(MonoPEResourceDirEntry *)(resource_dir+1);
888 for(i=0; i<entries; i++) {
889 MonoPEResourceDirEntry *entry=&res_entries[i];
892 ret=mono_image_walk_resource_tree (info, res_id, lang_id,
893 name, entry, resource_dir,
904 mono_image_get_entry_point (MonoImage *image)
906 return ((MonoCLIImageInfo*)image->image_info)->cli_cli_header.ch_entry_point;
910 mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)
912 MonoCLIImageInfo *iinfo = image->image_info;
913 MonoCLIHeader *ch = &iinfo->cli_cli_header;
916 if (!ch->ch_resources.rva || offset + 4 > ch->ch_resources.size)
919 data = mono_cli_rva_map (iinfo, ch->ch_resources.rva);
924 *size = read32 (data);
930 mono_image_get_strong_name (MonoImage *image, guint32 *size)
932 MonoCLIImageInfo *iinfo = image->image_info;
933 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
936 if (!de->size || !de->rva)
938 data = mono_cli_rva_map (iinfo, de->rva);
947 mono_image_strong_name_position (MonoImage *image, guint32 *size)
949 MonoCLIImageInfo *iinfo = image->image_info;
950 MonoPEDirEntry *de = &iinfo->cli_cli_header.ch_strong_name;
951 const int top = iinfo->cli_section_count;
952 MonoSectionTable *tables = iinfo->cli_section_tables;
954 guint32 addr = de->rva;
958 if (!de->size || !de->rva)
960 for (i = 0; i < top; i++){
961 if ((addr >= tables->st_virtual_address) &&
962 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
963 return tables->st_raw_data_ptr +
964 (addr - tables->st_virtual_address);
973 mono_image_get_public_key (MonoImage *image, guint32 *size)
977 if (image->tables [MONO_TABLE_ASSEMBLY].rows != 1)
979 tok = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY], 0, MONO_ASSEMBLY_PUBLIC_KEY);
982 pubkey = mono_metadata_blob_heap (image, tok);
983 len = mono_metadata_decode_blob_size (pubkey, &pubkey);