2 * loader.c: Image Loader
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
9 * This file is used by the interpreter and the JIT engine to locate
10 * assemblies. Used to load AssemblyRef and later to resolve various
14 * This should keep track of the assembly versions that we are loading.
20 #include <mono/metadata/metadata.h>
21 #include <mono/metadata/image.h>
22 #include <mono/metadata/cil-coff.h>
31 static GHashTable *image_hash;
33 #define CSIZE(x) (sizeof (x) / 4)
36 register_image (MonoImage *img)
38 cli_image_info_t *iinfo = img->image_info;
39 metadata_t *t = &iinfo->cli_metadata;
40 ImageInfo *ii = g_new (ImageInfo, 1);
41 metadata_tableinfo_t *assembly_table = &t->tables [META_TABLE_ASSEMBLY];
44 if (assembly_table->base == NULL) {
51 mono_metadata_decode_row (assembly_table, 0, cols, CSIZE (cols));
52 ii->name = mono_metadata_string_heap (t, cols [7]);
53 ii->ver [0] = cols [1];
54 ii->ver [1] = cols [2];
55 ii->ver [2] = cols [3];
56 ii->ver [3] = cols [4];
59 if (image_hash == NULL)
60 image_hash = g_hash_table_new (g_str_hash, g_str_equal);
62 g_hash_table_insert (image_hash, (void *) ii->name, ii);
66 unregister_image (MonoImage *img)
72 * @fname: file that contains the image
73 * @status: pointer to the status that can be returned.
75 * Returns: on success a pointer to a MonoImage.
77 * Side Effects: This uses mono_image_open to load the image
78 * and later registers this image. The image can later be located
79 * with mono_locate_image.
82 mono_load_image (const char *fname, enum MonoImageOpenStatus *status)
86 img = mono_image_open (fname, status);
97 * @name: assembly name to locate.
100 mono_locate_image (const char *name)
102 ImageInfo *ii = g_hash_table_lookup (image_hash, name);
111 mono_get_method (cli_image_info_t *iinfo, guint32 token)
113 MonoMethod *result = g_new0 (MonoMethod, 1);
114 int table = mono_metadata_token_table (token);
115 int index = mono_metadata_token_index (token);
116 metadata_tableinfo_t *tables = iinfo->cli_metadata.tables;
118 const char *sig = NULL;
123 * We need a context with cli_image_info_t for this module and the assemblies
124 * loaded later to support method refs...
126 if (table != META_TABLE_METHOD) {
127 g_assert (table == META_TABLE_MEMBERREF);
128 mono_metadata_decode_row (&tables [table], index, cols, 3);
129 g_assert ((cols [0] & 0x07) != 3);
130 table = META_TABLE_METHOD;
131 index = cols [0] >> 3;
132 sig = mono_metadata_blob_heap (&iinfo->cli_metadata, cols [2]);
133 result->name_idx = cols [1];
136 mono_metadata_decode_row (&tables [table], index - 1, cols, 6);
137 result->name_idx = cols [3];
138 /* if this is a methodref from another module/assembly, this fails */
139 loc = cli_rva_map (iinfo, cols [0]);
141 result->header = mono_metadata_parse_mh (&iinfo->cli_metadata, loc);
142 if (!sig) /* already taken from the methodref */
143 sig = mono_metadata_blob_heap (&iinfo->cli_metadata, cols [4]);
144 sig = mono_metadata_decode_blob_size (sig, &size);
145 result->signature = mono_metadata_parse_method_signature (&iinfo->cli_metadata, 0, sig, NULL);
151 mono_free_method (MonoMethod *method)
153 mono_metadata_free_method_signature (method->signature);
154 mono_metadata_free_mh (method->header);