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.
20 #include "rawbuffer.h"
24 #define INVALID_ADDRESS 0xffffffff
27 * Keeps track of the various assemblies loaded
29 static GHashTable *loaded_images_hash;
32 mono_cli_rva_image_map (MonoCLIImageInfo *iinfo, guint32 addr)
34 const int top = iinfo->cli_section_count;
35 MonoSectionTable *tables = iinfo->cli_section_tables;
38 for (i = 0; i < top; i++){
39 if ((addr >= tables->st_virtual_address) &&
40 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
41 return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
45 return INVALID_ADDRESS;
49 mono_cli_rva_map (MonoCLIImageInfo *iinfo, guint32 addr)
51 const int top = iinfo->cli_section_count;
52 MonoSectionTable *tables = iinfo->cli_section_tables;
55 for (i = 0; i < top; i++){
56 if ((addr >= tables->st_virtual_address) &&
57 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
58 return iinfo->cli_sections [i] +
59 (addr - tables->st_virtual_address);
67 * mono_image_ensure_section_idx:
68 * @image: The image we are operating on
69 * @section: section number that we will load/map into memory
71 * This routine makes sure that we have an in-memory copy of
72 * an image section (.text, .rsrc, .data).
74 * Returns: TRUE on success
77 mono_image_ensure_section_idx (MonoImage *image, int section)
79 MonoCLIImageInfo *iinfo = image->image_info;
80 MonoSectionTable *sect;
83 g_return_val_if_fail (section < iinfo->cli_section_count, FALSE);
85 if (iinfo->cli_sections [section] != NULL)
88 sect = &iinfo->cli_section_tables [section];
90 writable = sect->st_flags & SECT_FLAGS_MEM_WRITE;
92 iinfo->cli_sections [section] = mono_raw_buffer_load (
93 fileno (image->f), writable,
94 sect->st_raw_data_ptr, sect->st_raw_data_size);
96 if (iinfo->cli_sections [section] == NULL)
103 * mono_image_ensure_section:
104 * @image: The image we are operating on
105 * @section: section name that we will load/map into memory
107 * This routine makes sure that we have an in-memory copy of
108 * an image section (.text, .rsrc, .data).
110 * Returns: TRUE on success
113 mono_image_ensure_section (MonoImage *image, const char *section)
115 MonoCLIImageInfo *ii = image->image_info;
118 for (i = 0; i < ii->cli_section_count; i++){
119 if (strncmp (ii->cli_section_tables [i].st_name, section, 8) != 0)
122 return mono_image_ensure_section_idx (image, i);
128 load_section_tables (MonoImage *image, MonoCLIImageInfo *iinfo)
130 const int top = iinfo->cli_header.coff.coff_sections;
133 iinfo->cli_section_count = top;
134 iinfo->cli_section_tables = g_new0 (MonoSectionTable, top);
135 iinfo->cli_sections = g_new0 (void *, top);
137 for (i = 0; i < top; i++){
138 MonoSectionTable *t = &iinfo->cli_section_tables [i];
140 if (fread (t, sizeof (MonoSectionTable), 1, image->f) != 1)
143 t->st_virtual_size = le32_to_cpu (t->st_virtual_size);
144 t->st_virtual_address = le32_to_cpu (t->st_virtual_address);
145 t->st_raw_data_size = le32_to_cpu (t->st_raw_data_size);
146 t->st_raw_data_ptr = le32_to_cpu (t->st_raw_data_ptr);
147 t->st_reloc_ptr = le32_to_cpu (t->st_reloc_ptr);
148 t->st_lineno_ptr = le32_to_cpu (t->st_lineno_ptr);
149 t->st_reloc_count = le16_to_cpu (t->st_reloc_count);
150 t->st_line_count = le16_to_cpu (t->st_line_count);
152 /* consistency checks here */
155 for (i = 0; i < top; i++)
156 if (!mono_image_ensure_section_idx (image, i))
163 load_cli_header (MonoImage *image, MonoCLIImageInfo *iinfo)
168 offset = mono_cli_rva_image_map (iinfo, iinfo->cli_header.datadir.pe_cli_header.rva);
169 if (offset == INVALID_ADDRESS)
172 if (fseek (image->f, offset, 0) != 0)
175 if ((n = fread (&iinfo->cli_cli_header, sizeof (MonoCLIHeader), 1, image->f)) != 1)
178 /* Catch new uses of the fields that are supposed to be zero */
180 if ((iinfo->cli_cli_header.ch_eeinfo_table.rva != 0) ||
181 (iinfo->cli_cli_header.ch_helper_table.rva != 0) ||
182 (iinfo->cli_cli_header.ch_dynamic_info.rva != 0) ||
183 (iinfo->cli_cli_header.ch_delay_load_info.rva != 0) ||
184 (iinfo->cli_cli_header.ch_module_image.rva != 0) ||
185 (iinfo->cli_cli_header.ch_external_fixups.rva != 0) ||
186 (iinfo->cli_cli_header.ch_ridmap.rva != 0) ||
187 (iinfo->cli_cli_header.ch_debug_map.rva != 0) ||
188 (iinfo->cli_cli_header.ch_ip_map.rva != 0)){
191 * No need to scare people who are testing this, I am just
192 * labelling this as a LAMESPEC
194 /* g_warning ("Some fields in the CLI header which should have been zero are not zero"); */
202 load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
204 MonoMetadata *metadata = &image->metadata;
205 guint32 offset, size;
210 offset = mono_cli_rva_image_map (iinfo, iinfo->cli_cli_header.ch_metadata.rva);
211 size = iinfo->cli_cli_header.ch_metadata.size;
213 metadata->raw_metadata = mono_raw_buffer_load (fileno (image->f), FALSE, offset, size);
214 if (metadata->raw_metadata == NULL)
217 ptr = metadata->raw_metadata;
219 if (strncmp (ptr, "BSJB", 4) == 0){
220 guint32 version_string_len;
223 version_string_len = read32 (ptr);
225 ptr += version_string_len;
226 if (((guint32) ptr) % 4)
227 ptr += 4 - (((guint32) ptr) %4);
231 /* skip over flags */
234 streams = read16 (ptr);
237 for (i = 0; i < streams; i++){
238 if (strncmp (ptr + 8, "#~", 3) == 0){
239 metadata->heap_tables.offset = read32 (ptr);
240 metadata->heap_tables.size = read32 (ptr + 4);
242 } else if (strncmp (ptr + 8, "#Strings", 9) == 0){
243 metadata->heap_strings.offset = read32 (ptr);
244 metadata->heap_strings.size = read32 (ptr + 4);
246 } else if (strncmp (ptr + 8, "#US", 4) == 0){
247 metadata->heap_us.offset = read32 (ptr);
248 metadata->heap_us.size = read32 (ptr + 4);
250 } else if (strncmp (ptr + 8, "#Blob", 6) == 0){
251 metadata->heap_blob.offset = read32 (ptr);
252 metadata->heap_blob.size = read32 (ptr + 4);
254 } else if (strncmp (ptr + 8, "#GUID", 6) == 0){
255 metadata->heap_guid.offset = read32 (ptr);
256 metadata->heap_guid.size = read32 (ptr + 4);
259 g_message ("Unknown heap type: %s\n", ptr + 8);
260 if (((guint32)ptr) % 4){
261 ptr += 4 - (((guint32)ptr) % 4);
268 * Load representation of logical metadata tables, from the "#~" stream
271 load_tables (MonoImage *image, MonoMetadata *meta)
273 char *heap_tables = meta->raw_metadata + meta->heap_tables.offset;
276 int valid = 0, table;
279 heap_sizes = heap_tables [6];
280 meta->idx_string_wide = ((heap_sizes & 0x01) == 1);
281 meta->idx_guid_wide = ((heap_sizes & 0x02) == 2);
282 meta->idx_blob_wide = ((heap_sizes & 0x04) == 4);
284 valid_mask = read64 (heap_tables + 8);
285 rows = (guint32 *) (heap_tables + 24);
287 for (table = 0; table < 64; table++){
288 if ((valid_mask & ((guint64) 1 << table)) == 0){
289 meta->tables [table].rows = 0;
293 g_warning("bits in valid must be zero above 0x2b (II - 23.1.6)");
295 meta->tables [table].rows = read32 (rows);
300 meta->tables_base = (heap_tables + 24) + (4 * valid);
302 /* They must be the same */
303 g_assert ((void *) meta->tables_base == (void *) rows);
305 mono_metadata_compute_table_bases (meta);
310 load_metadata (MonoImage *image, MonoCLIImageInfo *iinfo)
312 if (!load_metadata_ptrs (image, iinfo))
315 return load_tables (image, &image->metadata);
319 do_mono_image_open (const char *fname, enum MonoImageOpenStatus *status)
321 MonoCLIImageInfo *iinfo;
322 MonoDotNetHeader *header;
323 MonoMSDOSHeader msdos;
327 image = g_new0 (MonoImage, 1);
328 image->f = fopen (fname, "r");
329 image->name = g_strdup (fname);
330 iinfo = g_new0 (MonoCLIImageInfo, 1);
331 image->image_info = iinfo;
333 image->method_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
334 image->class_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
335 image->array_cache = g_hash_table_new (g_direct_hash, g_direct_equal);
337 header = &iinfo->cli_header;
339 if (image->f == NULL){
341 *status = MONO_IMAGE_ERROR_ERRNO;
342 mono_image_close (image);
347 *status = MONO_IMAGE_IMAGE_INVALID;
349 if (fread (&msdos, sizeof (msdos), 1, image->f) != 1)
352 if (!(msdos.msdos_header [0] == 'M' && msdos.msdos_header [1] == 'Z'))
355 if (msdos.pe_offset != sizeof (msdos))
356 fseek (image->f, msdos.pe_offset, SEEK_SET);
358 if ((n = fread (header, sizeof (MonoDotNetHeader), 1, image->f)) != 1)
361 if (header->coff.coff_machine != 0x14c) /* FIXME: ENOENDIAN */
364 if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader) - sizeof (MonoCOFFHeader) - 4))
367 if (header->pe.pe_magic != 0x10B) /* FIXME: ENOENDIAN */
370 if (header->pe.pe_major != 6 || header->pe.pe_minor != 0)
374 * FIXME: byte swap all addresses here for header.
377 if (!load_section_tables (image, iinfo))
380 /* Load the CLI header */
381 if (!load_cli_header (image, iinfo))
384 if (!load_metadata (image, iinfo))
388 *status = MONO_IMAGE_OK;
393 mono_image_close (image);
399 * @fname: filename that points to the module we want to open
400 * @status: An error condition is returned in this field
402 * Retuns: An open image of type %MonoImage or NULL on error.
403 * if NULL, then check the value of @status for details on the error
406 mono_image_open (const char *fname, enum MonoImageOpenStatus *status)
410 g_return_val_if_fail (fname != NULL, NULL);
412 if (loaded_images_hash){
413 image = g_hash_table_lookup (loaded_images_hash, fname);
420 image = do_mono_image_open (fname, status);
424 if (!loaded_images_hash)
425 loaded_images_hash = g_hash_table_new (g_str_hash, g_str_equal);
426 g_hash_table_insert (loaded_images_hash, image->name, image);
433 * @image: The image file we wish to close
435 * Closes an image file, deallocates all memory consumed and
436 * unmaps all possible sections of the file
439 mono_image_close (MonoImage *image)
441 g_return_if_fail (image != NULL);
443 if (--image->ref_count)
446 g_hash_table_remove (loaded_images_hash, image->name);
451 g_free (image->name);
453 if (image->metadata.raw_metadata != NULL)
454 mono_raw_buffer_free (image->metadata.raw_metadata);
456 if (image->image_info){
457 MonoCLIImageInfo *ii = image->image_info;
460 for (i = 0; i < ii->cli_section_count; i++){
461 if (!ii->cli_sections [i])
463 mono_raw_buffer_free (ii->cli_sections [i]);
465 if (ii->cli_section_tables)
466 g_free (ii->cli_section_tables);
467 if (ii->cli_sections)
468 g_free (ii->cli_sections);
469 g_free (image->image_info);
476 * mono_image_strerror:
477 * @status: an code indicating the result from a recent operation
479 * Returns: a string describing the error
482 mono_image_strerror (enum MonoImageOpenStatus status)
487 case MONO_IMAGE_ERROR_ERRNO:
488 return strerror (errno);
489 case MONO_IMAGE_IMAGE_INVALID:
490 return "File does not contain a valid CIL image";
491 case MONO_IMAGE_MISSING_ASSEMBLYREF:
492 return "An assembly was referenced, but could not be found";
494 return "Internal error";