2 * assembly.c: Routines for manipulating and assembly 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"
23 #define INVALID_ADDRESS 0xffffffff
26 cli_rva_image_map (cli_image_info_t *iinfo, guint32 addr)
28 const int top = iinfo->cli_section_count;
29 section_table_t *tables = iinfo->cli_section_tables;
32 for (i = 0; i < top; i++){
33 if ((addr >= tables->st_virtual_address) &&
34 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
35 return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
39 return INVALID_ADDRESS;
43 cli_rva_map (cli_image_info_t *iinfo, guint32 addr)
45 const int top = iinfo->cli_section_count;
46 section_table_t *tables = iinfo->cli_section_tables;
49 for (i = 0; i < top; i++){
50 if ((addr >= tables->st_virtual_address) &&
51 (addr < tables->st_virtual_address + tables->st_raw_data_size)){
52 return iinfo->cli_sections [i] +
53 (addr - tables->st_virtual_address);
61 * mono_assembly_ensure_section_idx:
62 * @assembly: The image we are operating on
63 * @section: section number that we will load/map into memory
65 * This routine makes sure that we have an in-memory copy of
66 * an image section (.text, .rsrc, .data).
68 * Returns: TRUE on success
71 mono_assembly_ensure_section_idx (MonoAssembly *assembly, int section)
73 cli_image_info_t *iinfo = assembly->image_info;
74 section_table_t *sect;
77 g_return_val_if_fail (section < iinfo->cli_section_count, FALSE);
79 if (iinfo->cli_sections [section] != NULL)
82 sect = &iinfo->cli_section_tables [section];
84 writable = sect->st_flags & SECT_FLAGS_MEM_WRITE;
86 iinfo->cli_sections [section] = raw_buffer_load (
87 fileno (assembly->f), writable,
88 sect->st_raw_data_ptr, sect->st_raw_data_size);
90 if (iinfo->cli_sections [section] == NULL)
97 * mono_assembly_ensure_section:
98 * @assembly: The image we are operating on
99 * @section: section name that we will load/map into memory
101 * This routine makes sure that we have an in-memory copy of
102 * an image section (.text, .rsrc, .data).
104 * Returns: TRUE on success
107 mono_assembly_ensure_section (MonoAssembly *assembly, const char *section)
109 cli_image_info_t *ii = assembly->image_info;
112 for (i = 0; i < ii->cli_section_count; i++){
113 if (strncmp (ii->cli_section_tables [i].st_name, section, 8) != 0)
116 return mono_assembly_ensure_section_idx (assembly, i);
122 load_section_tables (MonoAssembly *assembly, cli_image_info_t *iinfo)
124 const int top = iinfo->cli_header.coff.coff_sections;
127 iinfo->cli_section_count = top;
128 iinfo->cli_section_tables = g_new (section_table_t, top);
129 iinfo->cli_sections = g_new0 (void *, top);
131 for (i = 0; i < top; i++){
132 section_table_t *t = &iinfo->cli_section_tables [i];
134 if (fread (t, sizeof (section_table_t), 1, assembly->f) != 1)
137 t->st_virtual_size = le32_to_cpu (t->st_virtual_size);
138 t->st_virtual_address = le32_to_cpu (t->st_virtual_address);
139 t->st_raw_data_size = le32_to_cpu (t->st_raw_data_size);
140 t->st_raw_data_ptr = le32_to_cpu (t->st_raw_data_ptr);
141 t->st_reloc_ptr = le32_to_cpu (t->st_reloc_ptr);
142 t->st_lineno_ptr = le32_to_cpu (t->st_lineno_ptr);
143 t->st_reloc_count = le16_to_cpu (t->st_reloc_count);
144 t->st_line_count = le16_to_cpu (t->st_line_count);
147 for (i = 0; i < top; i++)
148 if (!mono_assembly_ensure_section_idx (assembly, i))
155 load_cli_header (MonoAssembly *assembly, cli_image_info_t *iinfo)
160 offset = cli_rva_image_map (iinfo, iinfo->cli_header.datadir.pe_cli_header.rva);
161 if (offset == INVALID_ADDRESS)
164 if (fseek (assembly->f, offset, 0) != 0)
167 if ((n = fread (&iinfo->cli_cli_header, sizeof (cli_header_t), 1, assembly->f)) != 1)
170 /* Catch new uses of the fields that are supposed to be zero */
172 if ((iinfo->cli_cli_header.ch_eeinfo_table.rva != 0) ||
173 (iinfo->cli_cli_header.ch_helper_table.rva != 0) ||
174 (iinfo->cli_cli_header.ch_dynamic_info.rva != 0) ||
175 (iinfo->cli_cli_header.ch_delay_load_info.rva != 0) ||
176 (iinfo->cli_cli_header.ch_module_image.rva != 0) ||
177 (iinfo->cli_cli_header.ch_external_fixups.rva != 0) ||
178 (iinfo->cli_cli_header.ch_ridmap.rva != 0) ||
179 (iinfo->cli_cli_header.ch_debug_map.rva != 0) ||
180 (iinfo->cli_cli_header.ch_ip_map.rva != 0)){
181 g_message ("Some fields in the CLI header which should have been zero are not zero");
188 load_metadata_ptrs (MonoAssembly *assembly, cli_image_info_t *iinfo)
190 metadata_t *metadata = &iinfo->cli_metadata;
191 guint32 offset, size;
196 offset = cli_rva_image_map (iinfo, iinfo->cli_cli_header.ch_metadata.rva);
197 size = iinfo->cli_cli_header.ch_metadata.size;
199 metadata->raw_metadata = raw_buffer_load (fileno (assembly->f), FALSE, offset, size);
200 if (metadata->raw_metadata == NULL)
203 ptr = metadata->raw_metadata;
205 if (strncmp (ptr, "BSJB", 4) == 0){
206 guint32 version_string_len;
209 version_string_len = read32 (ptr);
211 ptr += version_string_len;
212 if (((guint32) ptr) % 4)
213 ptr += 4 - (((guint32) ptr) %4);
217 /* skip over flags */
220 streams = read16 (ptr);
223 for (i = 0; i < streams; i++){
224 if (strncmp (ptr + 8, "#~", 3) == 0){
225 metadata->heap_tables.sh_offset = read32 (ptr);
226 metadata->heap_tables.sh_size = read32 (ptr + 4);
228 } else if (strncmp (ptr + 8, "#Strings", 9) == 0){
229 metadata->heap_strings.sh_offset = read32 (ptr);
230 metadata->heap_strings.sh_size = read32 (ptr + 4);
232 } else if (strncmp (ptr + 8, "#US", 4) == 0){
233 metadata->heap_us.sh_offset = read32 (ptr);
234 metadata->heap_us.sh_size = read32 (ptr + 4);
236 } else if (strncmp (ptr + 8, "#Blob", 6) == 0){
237 metadata->heap_blob.sh_offset = read32 (ptr);
238 metadata->heap_blob.sh_size = read32 (ptr + 4);
240 } else if (strncmp (ptr + 8, "#GUID", 6) == 0){
241 metadata->heap_guid.sh_offset = read32 (ptr);
242 metadata->heap_guid.sh_size = read32 (ptr + 4);
245 g_message ("Unknown heap type: %s\n", ptr + 8);
246 if (((guint32)ptr) % 4){
247 ptr += 4 - (((guint32)ptr) % 4);
254 * Load representation of logical metadata tables, from the "#~" stream
257 load_tables (MonoAssembly *assembly, metadata_t *meta)
259 char *heap_tables = meta->raw_metadata + meta->heap_tables.sh_offset;
262 int valid = 0, table;
265 heap_sizes = heap_tables [6];
266 meta->idx_string_wide = ((heap_sizes & 0x01) == 1);
267 meta->idx_guid_wide = ((heap_sizes & 0x02) == 2);
268 meta->idx_blob_wide = ((heap_sizes & 0x04) == 4);
270 valid_mask = read64 (heap_tables + 8);
271 rows = (guint32 *) (heap_tables + 24);
273 for (table = 0; table < 64; table++){
274 if ((valid_mask & ((guint64) 1 << table)) == 0){
275 meta->tables [table].rows = 0;
278 meta->tables [table].rows = read32 (rows);
283 meta->tables_base = (heap_tables + 24) + (4 * valid);
285 /* They must be the same */
286 g_assert ((void *) meta->tables_base == (void *) rows);
288 mono_metadata_compute_table_bases (meta);
293 load_metadata (MonoAssembly *assembly, cli_image_info_t *iinfo)
295 if (!load_metadata_ptrs (assembly, iinfo))
298 return load_tables (assembly, &iinfo->cli_metadata);
302 * mono_assembly_open:
303 * @fname: filename that points to the module we want to open
304 * @status: An error condition is returned in this field
306 * Retuns: An open assembly of type %MonoAssembly or NULL on error.
307 * if NULL, then check the value of @status for details on the error
310 mono_assembly_open (const char *fname, enum MonoAssemblyOpenStatus *status)
312 cli_image_info_t *iinfo;
313 dotnet_header_t *header;
314 msdos_header_t msdos;
315 MonoAssembly *assembly;
318 assembly = g_new (MonoAssembly, 1);
319 assembly->f = fopen (fname, "r");
320 iinfo = g_new (cli_image_info_t, 1);
321 assembly->image_info = iinfo;
323 header = &iinfo->cli_header;
325 if (assembly->f == NULL){
327 *status = MONO_ASSEMBLY_ERROR_ERRNO;
328 mono_assembly_close (assembly);
333 *status = MONO_ASSEMBLY_IMAGE_INVALID;
335 if (fread (&msdos, sizeof (msdos), 1, assembly->f) != 1)
338 if (!(msdos.msdos_header [0] == 0x4d && msdos.msdos_header [1] == 0x5a))
341 if ((n = fread (header, sizeof (dotnet_header_t), 1, assembly->f)) != 1)
345 * FIXME: byte swap all addresses here for header.
348 if (!load_section_tables (assembly, iinfo))
351 /* Load the CLI header */
352 if (!load_cli_header (assembly, iinfo))
355 if (!load_metadata (assembly, iinfo))
359 *status = MONO_ASSEMBLY_OK;
364 mono_assembly_close (assembly);
369 * mono_assembly_close:
370 * @assembly: The image file we wish to close
372 * Closes an image file, deallocates all memory consumed and
373 * unmaps all possible sections of the file
376 mono_assembly_close (MonoAssembly *assembly)
378 g_return_if_fail (assembly != NULL);
381 fclose (assembly->f);
383 if (assembly->image_info){
384 cli_image_info_t *ii = assembly->image_info;
387 if (ii->cli_metadata.raw_metadata != NULL)
388 raw_buffer_free (ii->cli_metadata.raw_metadata);
390 for (i = 0; i < ii->cli_section_count; i++){
391 if (!ii->cli_sections [i])
393 raw_buffer_free (ii->cli_sections [i]);
395 if (ii->cli_section_tables)
396 g_free (ii->cli_section_tables);
397 if (ii->cli_sections)
398 g_free (ii->cli_section_tables);
399 g_free (assembly->image_info);
406 * mono_assembly_strerror:
407 * @status: an code indicating the result from a recent operation
409 * Returns: a string describing the error
412 mono_assembly_strerror (enum MonoAssemblyOpenStatus status)
415 case MONO_ASSEMBLY_OK:
417 case MONO_ASSEMBLY_ERROR_ERRNO:
418 return strerror (errno);
419 case MONO_ASSEMBLY_IMAGE_INVALID:
420 return "File does not contain a valid CIL image";
422 return "Internal error";