/
image.c
2014 lines (1752 loc) · 54.8 KB
/
image.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* image.c: Routines for manipulating an image stored in an
* extended PE/COFF file.
*
* Authors:
* Miguel de Icaza (miguel@ximian.com)
* Paolo Molaro (lupus@ximian.com)
*
* (C) 2001-2003 Ximian, Inc. http://www.ximian.com
*
*/
#include <config.h>
#include <stdio.h>
#include <glib.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include "image.h"
#include "cil-coff.h"
#include "mono-endian.h"
#include "tabledefs.h"
#include "tokentype.h"
#include "metadata-internals.h"
#include "profiler-private.h"
#include "loader.h"
#include "marshal.h"
#include "coree.h"
#include <mono/io-layer/io-layer.h>
#include <mono/utils/mono-logger.h>
#include <mono/utils/mono-path.h>
#include <mono/utils/mono-mmap.h>
#include <mono/utils/mono-io-portability.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/object-internals.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#define INVALID_ADDRESS 0xffffffff
/*
* Keeps track of the various assemblies loaded
*/
static GHashTable *loaded_images_hash;
static GHashTable *loaded_images_refonly_hash;
static gboolean debug_assembly_unload = FALSE;
#define mono_images_lock() EnterCriticalSection (&images_mutex)
#define mono_images_unlock() LeaveCriticalSection (&images_mutex)
static CRITICAL_SECTION images_mutex;
/* returns offset relative to image->raw_data */
guint32
mono_cli_rva_image_map (MonoImage *image, guint32 addr)
{
MonoCLIImageInfo *iinfo = image->image_info;
const int top = iinfo->cli_section_count;
MonoSectionTable *tables = iinfo->cli_section_tables;
int i;
for (i = 0; i < top; i++){
if ((addr >= tables->st_virtual_address) &&
(addr < tables->st_virtual_address + tables->st_raw_data_size)){
#ifdef PLATFORM_WIN32
if (image->is_module_handle)
return addr;
#endif
return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
}
tables++;
}
return INVALID_ADDRESS;
}
/**
* mono_images_rva_map:
* @image: a MonoImage
* @addr: relative virtual address (RVA)
*
* This is a low-level routine used by the runtime to map relative
* virtual address (RVA) into their location in memory.
*
* Returns: the address in memory for the given RVA, or NULL if the
* RVA is not valid for this image.
*/
char *
mono_image_rva_map (MonoImage *image, guint32 addr)
{
MonoCLIImageInfo *iinfo = image->image_info;
const int top = iinfo->cli_section_count;
MonoSectionTable *tables = iinfo->cli_section_tables;
int i;
for (i = 0; i < top; i++){
if ((addr >= tables->st_virtual_address) &&
(addr < tables->st_virtual_address + tables->st_raw_data_size)){
if (!iinfo->cli_sections [i]) {
if (!mono_image_ensure_section_idx (image, i))
return NULL;
}
#ifdef PLATFORM_WIN32
if (image->is_module_handle)
return image->raw_data + addr;
#endif
return (char*)iinfo->cli_sections [i] +
(addr - tables->st_virtual_address);
}
tables++;
}
return NULL;
}
/**
* mono_images_init:
*
* Initialize the global variables used by this module.
*/
void
mono_images_init (void)
{
InitializeCriticalSection (&images_mutex);
loaded_images_hash = g_hash_table_new (g_str_hash, g_str_equal);
loaded_images_refonly_hash = g_hash_table_new (g_str_hash, g_str_equal);
debug_assembly_unload = getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
}
/**
* mono_images_cleanup:
*
* Free all resources used by this module.
*/
void
mono_images_cleanup (void)
{
DeleteCriticalSection (&images_mutex);
g_hash_table_destroy (loaded_images_hash);
g_hash_table_destroy (loaded_images_refonly_hash);
}
/**
* mono_image_ensure_section_idx:
* @image: The image we are operating on
* @section: section number that we will load/map into memory
*
* This routine makes sure that we have an in-memory copy of
* an image section (.text, .rsrc, .data).
*
* Returns: TRUE on success
*/
int
mono_image_ensure_section_idx (MonoImage *image, int section)
{
MonoCLIImageInfo *iinfo = image->image_info;
MonoSectionTable *sect;
gboolean writable;
g_return_val_if_fail (section < iinfo->cli_section_count, FALSE);
if (iinfo->cli_sections [section] != NULL)
return TRUE;
sect = &iinfo->cli_section_tables [section];
writable = sect->st_flags & SECT_FLAGS_MEM_WRITE;
if (sect->st_raw_data_ptr + sect->st_raw_data_size > image->raw_data_len)
return FALSE;
#ifdef PLATFORM_WIN32
if (image->is_module_handle)
iinfo->cli_sections [section] = image->raw_data + sect->st_virtual_address;
else
#endif
/* FIXME: we ignore the writable flag since we don't patch the binary */
iinfo->cli_sections [section] = image->raw_data + sect->st_raw_data_ptr;
return TRUE;
}
/**
* mono_image_ensure_section:
* @image: The image we are operating on
* @section: section name that we will load/map into memory
*
* This routine makes sure that we have an in-memory copy of
* an image section (.text, .rsrc, .data).
*
* Returns: TRUE on success
*/
int
mono_image_ensure_section (MonoImage *image, const char *section)
{
MonoCLIImageInfo *ii = image->image_info;
int i;
for (i = 0; i < ii->cli_section_count; i++){
if (strncmp (ii->cli_section_tables [i].st_name, section, 8) != 0)
continue;
return mono_image_ensure_section_idx (image, i);
}
return FALSE;
}
static int
load_section_tables (MonoImage *image, MonoCLIImageInfo *iinfo, guint32 offset)
{
const int top = iinfo->cli_header.coff.coff_sections;
int i;
iinfo->cli_section_count = top;
iinfo->cli_section_tables = g_new0 (MonoSectionTable, top);
iinfo->cli_sections = g_new0 (void *, top);
for (i = 0; i < top; i++){
MonoSectionTable *t = &iinfo->cli_section_tables [i];
if (offset + sizeof (MonoSectionTable) > image->raw_data_len)
return FALSE;
memcpy (t, image->raw_data + offset, sizeof (MonoSectionTable));
offset += sizeof (MonoSectionTable);
#if G_BYTE_ORDER != G_LITTLE_ENDIAN
t->st_virtual_size = GUINT32_FROM_LE (t->st_virtual_size);
t->st_virtual_address = GUINT32_FROM_LE (t->st_virtual_address);
t->st_raw_data_size = GUINT32_FROM_LE (t->st_raw_data_size);
t->st_raw_data_ptr = GUINT32_FROM_LE (t->st_raw_data_ptr);
t->st_reloc_ptr = GUINT32_FROM_LE (t->st_reloc_ptr);
t->st_lineno_ptr = GUINT32_FROM_LE (t->st_lineno_ptr);
t->st_reloc_count = GUINT16_FROM_LE (t->st_reloc_count);
t->st_line_count = GUINT16_FROM_LE (t->st_line_count);
t->st_flags = GUINT32_FROM_LE (t->st_flags);
#endif
/* consistency checks here */
}
return TRUE;
}
static gboolean
load_cli_header (MonoImage *image, MonoCLIImageInfo *iinfo)
{
guint32 offset;
offset = mono_cli_rva_image_map (image, iinfo->cli_header.datadir.pe_cli_header.rva);
if (offset == INVALID_ADDRESS)
return FALSE;
if (offset + sizeof (MonoCLIHeader) > image->raw_data_len)
return FALSE;
memcpy (&iinfo->cli_cli_header, image->raw_data + offset, sizeof (MonoCLIHeader));
#if G_BYTE_ORDER != G_LITTLE_ENDIAN
#define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
#define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
#define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
SWAP32 (iinfo->cli_cli_header.ch_size);
SWAP32 (iinfo->cli_cli_header.ch_flags);
SWAP32 (iinfo->cli_cli_header.ch_entry_point);
SWAP16 (iinfo->cli_cli_header.ch_runtime_major);
SWAP16 (iinfo->cli_cli_header.ch_runtime_minor);
SWAPPDE (iinfo->cli_cli_header.ch_metadata);
SWAPPDE (iinfo->cli_cli_header.ch_resources);
SWAPPDE (iinfo->cli_cli_header.ch_strong_name);
SWAPPDE (iinfo->cli_cli_header.ch_code_manager_table);
SWAPPDE (iinfo->cli_cli_header.ch_vtable_fixups);
SWAPPDE (iinfo->cli_cli_header.ch_export_address_table_jumps);
SWAPPDE (iinfo->cli_cli_header.ch_eeinfo_table);
SWAPPDE (iinfo->cli_cli_header.ch_helper_table);
SWAPPDE (iinfo->cli_cli_header.ch_dynamic_info);
SWAPPDE (iinfo->cli_cli_header.ch_delay_load_info);
SWAPPDE (iinfo->cli_cli_header.ch_module_image);
SWAPPDE (iinfo->cli_cli_header.ch_external_fixups);
SWAPPDE (iinfo->cli_cli_header.ch_ridmap);
SWAPPDE (iinfo->cli_cli_header.ch_debug_map);
SWAPPDE (iinfo->cli_cli_header.ch_ip_map);
#undef SWAP32
#undef SWAP16
#undef SWAPPDE
#endif
/* Catch new uses of the fields that are supposed to be zero */
if ((iinfo->cli_cli_header.ch_eeinfo_table.rva != 0) ||
(iinfo->cli_cli_header.ch_helper_table.rva != 0) ||
(iinfo->cli_cli_header.ch_dynamic_info.rva != 0) ||
(iinfo->cli_cli_header.ch_delay_load_info.rva != 0) ||
(iinfo->cli_cli_header.ch_module_image.rva != 0) ||
(iinfo->cli_cli_header.ch_external_fixups.rva != 0) ||
(iinfo->cli_cli_header.ch_ridmap.rva != 0) ||
(iinfo->cli_cli_header.ch_debug_map.rva != 0) ||
(iinfo->cli_cli_header.ch_ip_map.rva != 0)){
/*
* No need to scare people who are testing this, I am just
* labelling this as a LAMESPEC
*/
/* g_warning ("Some fields in the CLI header which should have been zero are not zero"); */
}
return TRUE;
}
static gboolean
load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
{
guint32 offset, size;
guint16 streams;
int i;
guint32 pad;
char *ptr;
offset = mono_cli_rva_image_map (image, iinfo->cli_cli_header.ch_metadata.rva);
if (offset == INVALID_ADDRESS)
return FALSE;
size = iinfo->cli_cli_header.ch_metadata.size;
if (offset + size > image->raw_data_len)
return FALSE;
image->raw_metadata = image->raw_data + offset;
ptr = image->raw_metadata;
if (strncmp (ptr, "BSJB", 4) == 0){
guint32 version_string_len;
ptr += 4;
image->md_version_major = read16 (ptr);
ptr += 4;
image->md_version_minor = read16 (ptr);
ptr += 4;
version_string_len = read32 (ptr);
ptr += 4;
image->version = g_strndup (ptr, version_string_len);
ptr += version_string_len;
pad = ptr - image->raw_metadata;
if (pad % 4)
ptr += 4 - (pad % 4);
} else
return FALSE;
/* skip over flags */
ptr += 2;
streams = read16 (ptr);
ptr += 2;
for (i = 0; i < streams; i++){
if (strncmp (ptr + 8, "#~", 3) == 0){
image->heap_tables.data = image->raw_metadata + read32 (ptr);
image->heap_tables.size = read32 (ptr + 4);
ptr += 8 + 3;
} else if (strncmp (ptr + 8, "#Strings", 9) == 0){
image->heap_strings.data = image->raw_metadata + read32 (ptr);
image->heap_strings.size = read32 (ptr + 4);
ptr += 8 + 9;
} else if (strncmp (ptr + 8, "#US", 4) == 0){
image->heap_us.data = image->raw_metadata + read32 (ptr);
image->heap_us.size = read32 (ptr + 4);
ptr += 8 + 4;
} else if (strncmp (ptr + 8, "#Blob", 6) == 0){
image->heap_blob.data = image->raw_metadata + read32 (ptr);
image->heap_blob.size = read32 (ptr + 4);
ptr += 8 + 6;
} else if (strncmp (ptr + 8, "#GUID", 6) == 0){
image->heap_guid.data = image->raw_metadata + read32 (ptr);
image->heap_guid.size = read32 (ptr + 4);
ptr += 8 + 6;
} else if (strncmp (ptr + 8, "#-", 3) == 0) {
image->heap_tables.data = image->raw_metadata + read32 (ptr);
image->heap_tables.size = read32 (ptr + 4);
ptr += 8 + 3;
image->uncompressed_metadata = TRUE;
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);
} else {
g_message ("Unknown heap type: %s\n", ptr + 8);
ptr += 8 + strlen (ptr + 8) + 1;
}
pad = ptr - image->raw_metadata;
if (pad % 4)
ptr += 4 - (pad % 4);
}
g_assert (image->heap_guid.data);
g_assert (image->heap_guid.size >= 16);
image->guid = mono_guid_to_string ((guint8*)image->heap_guid.data);
return TRUE;
}
/*
* Load representation of logical metadata tables, from the "#~" stream
*/
static gboolean
load_tables (MonoImage *image)
{
const char *heap_tables = image->heap_tables.data;
const guint32 *rows;
guint64 valid_mask, sorted_mask;
int valid = 0, table;
int heap_sizes;
heap_sizes = heap_tables [6];
image->idx_string_wide = ((heap_sizes & 0x01) == 1);
image->idx_guid_wide = ((heap_sizes & 0x02) == 2);
image->idx_blob_wide = ((heap_sizes & 0x04) == 4);
valid_mask = read64 (heap_tables + 8);
sorted_mask = read64 (heap_tables + 16);
rows = (const guint32 *) (heap_tables + 24);
for (table = 0; table < 64; table++){
if ((valid_mask & ((guint64) 1 << table)) == 0){
if (table > MONO_TABLE_LAST)
continue;
image->tables [table].rows = 0;
continue;
}
if (table > MONO_TABLE_LAST) {
g_warning("bits in valid must be zero above 0x2d (II - 23.1.6)");
} else {
image->tables [table].rows = read32 (rows);
}
/*if ((sorted_mask & ((guint64) 1 << table)) == 0){
g_print ("table %s (0x%02x) is sorted\n", mono_meta_table_name (table), table);
}*/
rows++;
valid++;
}
image->tables_base = (heap_tables + 24) + (4 * valid);
/* They must be the same */
g_assert ((const void *) image->tables_base == (const void *) rows);
mono_metadata_compute_table_bases (image);
return TRUE;
}
static gboolean
load_metadata (MonoImage *image, MonoCLIImageInfo *iinfo)
{
if (!load_metadata_ptrs (image, iinfo))
return FALSE;
return load_tables (image);
}
void
mono_image_check_for_module_cctor (MonoImage *image)
{
MonoTableInfo *t, *mt;
t = &image->tables [MONO_TABLE_TYPEDEF];
mt = &image->tables [MONO_TABLE_METHOD];
if (mono_framework_version () == 1) {
image->checked_module_cctor = TRUE;
return;
}
if (image->dynamic) {
/* FIXME: */
image->checked_module_cctor = TRUE;
return;
}
if (t->rows >= 1) {
guint32 nameidx = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_NAME);
const char *name = mono_metadata_string_heap (image, nameidx);
if (strcmp (name, "<Module>") == 0) {
guint32 first_method = mono_metadata_decode_row_col (t, 0, MONO_TYPEDEF_METHOD_LIST) - 1;
guint32 last_method;
if (t->rows > 1)
last_method = mono_metadata_decode_row_col (t, 1, MONO_TYPEDEF_METHOD_LIST) - 1;
else
last_method = mt->rows;
for (; first_method < last_method; first_method++) {
nameidx = mono_metadata_decode_row_col (mt, first_method, MONO_METHOD_NAME);
name = mono_metadata_string_heap (image, nameidx);
if (strcmp (name, ".cctor") == 0) {
image->has_module_cctor = TRUE;
image->checked_module_cctor = TRUE;
return;
}
}
}
}
image->has_module_cctor = FALSE;
image->checked_module_cctor = TRUE;
}
static void
load_modules (MonoImage *image)
{
MonoTableInfo *t;
if (image->modules)
return;
t = &image->tables [MONO_TABLE_MODULEREF];
image->modules = g_new0 (MonoImage *, t->rows);
image->modules_loaded = g_new0 (gboolean, t->rows);
image->module_count = t->rows;
}
/**
* mono_image_load_module:
*
* Load the module with the one-based index IDX from IMAGE and return it. Return NULL if
* it cannot be loaded.
*/
MonoImage*
mono_image_load_module (MonoImage *image, int idx)
{
MonoTableInfo *t;
MonoTableInfo *file_table;
int i;
char *base_dir;
gboolean refonly = image->ref_only;
GList *list_iter, *valid_modules = NULL;
MonoImageOpenStatus status;
g_assert (idx <= image->module_count);
if (image->modules_loaded [idx - 1])
return image->modules [idx - 1];
file_table = &image->tables [MONO_TABLE_FILE];
for (i = 0; i < file_table->rows; i++) {
guint32 cols [MONO_FILE_SIZE];
mono_metadata_decode_row (file_table, i, cols, MONO_FILE_SIZE);
if (cols [MONO_FILE_FLAGS] == FILE_CONTAINS_NO_METADATA)
continue;
valid_modules = g_list_prepend (valid_modules, (char*)mono_metadata_string_heap (image, cols [MONO_FILE_NAME]));
}
t = &image->tables [MONO_TABLE_MODULEREF];
base_dir = g_path_get_dirname (image->name);
{
char *module_ref;
const char *name;
guint32 cols [MONO_MODULEREF_SIZE];
/* if there is no file table, we try to load the module... */
int valid = file_table->rows == 0;
mono_metadata_decode_row (t, idx - 1, cols, MONO_MODULEREF_SIZE);
name = mono_metadata_string_heap (image, cols [MONO_MODULEREF_NAME]);
for (list_iter = valid_modules; list_iter; list_iter = list_iter->next) {
/* be safe with string dups, but we could just compare string indexes */
if (strcmp (list_iter->data, name) == 0) {
valid = TRUE;
break;
}
}
if (valid) {
module_ref = g_build_filename (base_dir, name, NULL);
image->modules [idx - 1] = mono_image_open_full (module_ref, &status, refonly);
if (image->modules [idx - 1]) {
mono_image_addref (image->modules [idx - 1]);
image->modules [idx - 1]->assembly = image->assembly;
#ifdef PLATFORM_WIN32
if (image->modules [idx - 1]->is_module_handle)
mono_image_fixup_vtable (image->modules [idx - 1]);
#endif
/* g_print ("loaded module %s from %s (%p)\n", module_ref, image->name, image->assembly); */
}
g_free (module_ref);
}
}
image->modules_loaded [idx - 1] = TRUE;
g_free (base_dir);
g_list_free (valid_modules);
return image->modules [idx - 1];
}
static gpointer
class_key_extract (gpointer value)
{
MonoClass *class = value;
return GUINT_TO_POINTER (class->type_token);
}
static gpointer*
class_next_value (gpointer value)
{
MonoClass *class = value;
return (gpointer*)&class->next_class_cache;
}
void
mono_image_init (MonoImage *image)
{
image->mempool = mono_mempool_new_size (512);
mono_internal_hash_table_init (&image->class_cache,
g_direct_hash,
class_key_extract,
class_next_value);
image->field_cache = g_hash_table_new (NULL, NULL);
image->native_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
image->typespec_cache = g_hash_table_new (NULL, NULL);
image->memberref_signatures = g_hash_table_new (NULL, NULL);
image->helper_signatures = g_hash_table_new (g_str_hash, g_str_equal);
image->method_signatures = g_hash_table_new (NULL, NULL);
image->property_hash = mono_property_hash_new ();
}
#if G_BYTE_ORDER != G_LITTLE_ENDIAN
#define SWAP64(x) (x) = GUINT64_FROM_LE ((x))
#define SWAP32(x) (x) = GUINT32_FROM_LE ((x))
#define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
#define SWAPPDE(x) do { (x).rva = GUINT32_FROM_LE ((x).rva); (x).size = GUINT32_FROM_LE ((x).size);} while (0)
#else
#define SWAP64(x)
#define SWAP32(x)
#define SWAP16(x)
#define SWAPPDE(x)
#endif
/*
* Returns < 0 to indicate an error.
*/
static int
do_load_header (MonoImage *image, MonoDotNetHeader *header, int offset)
{
MonoDotNetHeader64 header64;
#ifdef PLATFORM_WIN32
if (!image->is_module_handle)
#endif
if (offset + sizeof (MonoDotNetHeader32) > image->raw_data_len)
return -1;
memcpy (header, image->raw_data + offset, sizeof (MonoDotNetHeader));
if (header->pesig [0] != 'P' || header->pesig [1] != 'E')
return -1;
/* endian swap the fields common between PE and PE+ */
SWAP32 (header->coff.coff_time);
SWAP32 (header->coff.coff_symptr);
SWAP32 (header->coff.coff_symcount);
SWAP16 (header->coff.coff_machine);
SWAP16 (header->coff.coff_sections);
SWAP16 (header->coff.coff_opt_header_size);
SWAP16 (header->coff.coff_attributes);
/* MonoPEHeader */
SWAP32 (header->pe.pe_code_size);
SWAP32 (header->pe.pe_uninit_data_size);
SWAP32 (header->pe.pe_rva_entry_point);
SWAP32 (header->pe.pe_rva_code_base);
SWAP32 (header->pe.pe_rva_data_base);
SWAP16 (header->pe.pe_magic);
/* now we are ready for the basic tests */
if (header->pe.pe_magic == 0x10B) {
offset += sizeof (MonoDotNetHeader);
SWAP32 (header->pe.pe_data_size);
if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader) - sizeof (MonoCOFFHeader) - 4))
return -1;
SWAP32 (header->nt.pe_image_base); /* must be 0x400000 */
SWAP32 (header->nt.pe_stack_reserve);
SWAP32 (header->nt.pe_stack_commit);
SWAP32 (header->nt.pe_heap_reserve);
SWAP32 (header->nt.pe_heap_commit);
} else if (header->pe.pe_magic == 0x20B) {
/* PE32+ file format */
if (header->coff.coff_opt_header_size != (sizeof (MonoDotNetHeader64) - sizeof (MonoCOFFHeader) - 4))
return -1;
memcpy (&header64, image->raw_data + offset, sizeof (MonoDotNetHeader64));
offset += sizeof (MonoDotNetHeader64);
/* copy the fields already swapped. the last field, pe_data_size, is missing */
memcpy (&header64, header, sizeof (MonoDotNetHeader) - 4);
/* FIXME: we lose bits here, but we don't use this stuff internally, so we don't care much.
* will be fixed when we change MonoDotNetHeader to not match the 32 bit variant
*/
SWAP64 (header64.nt.pe_image_base);
header->nt.pe_image_base = header64.nt.pe_image_base;
SWAP64 (header64.nt.pe_stack_reserve);
header->nt.pe_stack_reserve = header64.nt.pe_stack_reserve;
SWAP64 (header64.nt.pe_stack_commit);
header->nt.pe_stack_commit = header64.nt.pe_stack_commit;
SWAP64 (header64.nt.pe_heap_reserve);
header->nt.pe_heap_reserve = header64.nt.pe_heap_reserve;
SWAP64 (header64.nt.pe_heap_commit);
header->nt.pe_heap_commit = header64.nt.pe_heap_commit;
header->nt.pe_section_align = header64.nt.pe_section_align;
header->nt.pe_file_alignment = header64.nt.pe_file_alignment;
header->nt.pe_os_major = header64.nt.pe_os_major;
header->nt.pe_os_minor = header64.nt.pe_os_minor;
header->nt.pe_user_major = header64.nt.pe_user_major;
header->nt.pe_user_minor = header64.nt.pe_user_minor;
header->nt.pe_subsys_major = header64.nt.pe_subsys_major;
header->nt.pe_subsys_minor = header64.nt.pe_subsys_minor;
header->nt.pe_reserved_1 = header64.nt.pe_reserved_1;
header->nt.pe_image_size = header64.nt.pe_image_size;
header->nt.pe_header_size = header64.nt.pe_header_size;
header->nt.pe_checksum = header64.nt.pe_checksum;
header->nt.pe_subsys_required = header64.nt.pe_subsys_required;
header->nt.pe_dll_flags = header64.nt.pe_dll_flags;
header->nt.pe_loader_flags = header64.nt.pe_loader_flags;
header->nt.pe_data_dir_count = header64.nt.pe_data_dir_count;
/* copy the datadir */
memcpy (&header->datadir, &header64.datadir, sizeof (MonoPEDatadir));
} else {
return -1;
}
/* MonoPEHeaderNT: not used yet */
SWAP32 (header->nt.pe_section_align); /* must be 8192 */
SWAP32 (header->nt.pe_file_alignment); /* must be 512 or 4096 */
SWAP16 (header->nt.pe_os_major); /* must be 4 */
SWAP16 (header->nt.pe_os_minor); /* must be 0 */
SWAP16 (header->nt.pe_user_major);
SWAP16 (header->nt.pe_user_minor);
SWAP16 (header->nt.pe_subsys_major);
SWAP16 (header->nt.pe_subsys_minor);
SWAP32 (header->nt.pe_reserved_1);
SWAP32 (header->nt.pe_image_size);
SWAP32 (header->nt.pe_header_size);
SWAP32 (header->nt.pe_checksum);
SWAP16 (header->nt.pe_subsys_required);
SWAP16 (header->nt.pe_dll_flags);
SWAP32 (header->nt.pe_loader_flags);
SWAP32 (header->nt.pe_data_dir_count);
/* MonoDotNetHeader: mostly unused */
SWAPPDE (header->datadir.pe_export_table);
SWAPPDE (header->datadir.pe_import_table);
SWAPPDE (header->datadir.pe_resource_table);
SWAPPDE (header->datadir.pe_exception_table);
SWAPPDE (header->datadir.pe_certificate_table);
SWAPPDE (header->datadir.pe_reloc_table);
SWAPPDE (header->datadir.pe_debug);
SWAPPDE (header->datadir.pe_copyright);
SWAPPDE (header->datadir.pe_global_ptr);
SWAPPDE (header->datadir.pe_tls_table);
SWAPPDE (header->datadir.pe_load_config_table);
SWAPPDE (header->datadir.pe_bound_import);
SWAPPDE (header->datadir.pe_iat);
SWAPPDE (header->datadir.pe_delay_import_desc);
SWAPPDE (header->datadir.pe_cli_header);
SWAPPDE (header->datadir.pe_reserved);
#ifdef PLATFORM_WIN32
if (image->is_module_handle)
image->raw_data_len = header->nt.pe_image_size;
#endif
return offset;
}
static MonoImage *
do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
gboolean care_about_cli)
{
MonoCLIImageInfo *iinfo;
MonoDotNetHeader *header;
MonoMSDOSHeader msdos;
gint32 offset = 0;
mono_profiler_module_event (image, MONO_PROFILE_START_LOAD);
mono_image_init (image);
iinfo = image->image_info;
header = &iinfo->cli_header;
if (status)
*status = MONO_IMAGE_IMAGE_INVALID;
#ifdef PLATFORM_WIN32
if (!image->is_module_handle)
#endif
if (offset + sizeof (msdos) > image->raw_data_len)
goto invalid_image;
memcpy (&msdos, image->raw_data + offset, sizeof (msdos));
if (!(msdos.msdos_sig [0] == 'M' && msdos.msdos_sig [1] == 'Z'))
goto invalid_image;
msdos.pe_offset = GUINT32_FROM_LE (msdos.pe_offset);
offset = msdos.pe_offset;
offset = do_load_header (image, header, offset);
if (offset < 0)
goto invalid_image;
/*
* this tests for a x86 machine type, but itanium, amd64 and others could be used, too.
* we skip this test.
if (header->coff.coff_machine != 0x14c)
goto invalid_image;
*/
#if 0
/*
* The spec says that this field should contain 6.0, but Visual Studio includes a new compiler,
* which produces binaries with 7.0. From Sergey:
*
* The reason is that MSVC7 uses traditional compile/link
* sequence for CIL executables, and VS.NET (and Framework
* SDK) includes linker version 7, that puts 7.0 in this
* field. That's why it's currently not possible to load VC
* binaries with Mono. This field is pretty much meaningless
* anyway (what linker?).
*/
if (header->pe.pe_major != 6 || header->pe.pe_minor != 0)
goto invalid_image;
#endif
/*
* FIXME: byte swap all addresses here for header.
*/
if (!load_section_tables (image, iinfo, offset))
goto invalid_image;
if (care_about_cli == FALSE) {
goto done;
}
/* Load the CLI header */
if (!load_cli_header (image, iinfo))
goto invalid_image;
if (!load_metadata (image, iinfo))
goto invalid_image;
/* modules don't have an assembly table row */
if (image->tables [MONO_TABLE_ASSEMBLY].rows) {
image->assembly_name = mono_metadata_string_heap (image,
mono_metadata_decode_row_col (&image->tables [MONO_TABLE_ASSEMBLY],
0, MONO_ASSEMBLY_NAME));
}
image->module_name = mono_metadata_string_heap (image,
mono_metadata_decode_row_col (&image->tables [MONO_TABLE_MODULE],
0, MONO_MODULE_NAME));
load_modules (image);
done:
mono_profiler_module_loaded (image, MONO_PROFILE_OK);
if (status)
*status = MONO_IMAGE_OK;
return image;
invalid_image:
mono_profiler_module_loaded (image, MONO_PROFILE_FAILED);
mono_image_close (image);
return NULL;
}
static MonoImage *
do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
gboolean care_about_cli, gboolean refonly)
{
MonoCLIImageInfo *iinfo;
MonoImage *image;
FILE *filed;
struct stat stat_buf;
if ((filed = fopen (fname, "rb")) == NULL){
if (IS_PORTABILITY_SET) {
gchar *ffname = mono_portability_find_file (fname, TRUE);
if (ffname) {
filed = fopen (ffname, "rb");
g_free (ffname);
}
}
if (filed == NULL) {
if (status)
*status = MONO_IMAGE_ERROR_ERRNO;
return NULL;
}
}
if (fstat (fileno (filed), &stat_buf)) {
fclose (filed);
if (status)
*status = MONO_IMAGE_ERROR_ERRNO;
return NULL;
}
image = g_new0 (MonoImage, 1);
image->raw_buffer_used = TRUE;
image->raw_data_len = stat_buf.st_size;
image->raw_data = mono_file_map (stat_buf.st_size, MONO_MMAP_READ|MONO_MMAP_PRIVATE, fileno (filed), 0, &image->raw_data_handle);
if (!image->raw_data) {
fclose (filed);
g_free (image);
if (status)
*status = MONO_IMAGE_IMAGE_INVALID;
return NULL;
}
iinfo = g_new0 (MonoCLIImageInfo, 1);
image->image_info = iinfo;
image->name = mono_path_resolve_symlinks (fname);
image->ref_only = refonly;
image->ref_count = 1;
fclose (filed);
return do_mono_image_load (image, status, care_about_cli);
}
MonoImage *
mono_image_loaded_full (const char *name, gboolean refonly)
{
MonoImage *res;
GHashTable *loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
mono_images_lock ();
res = g_hash_table_lookup (loaded_images, name);
mono_images_unlock ();
return res;
}
/**
* mono_image_loaded:
* @name: name of the image to load
*
* This routine ensures that the given image is loaded.
*
* Returns: the loaded MonoImage, or NULL on failure.
*/
MonoImage *
mono_image_loaded (const char *name)
{
return mono_image_loaded_full (name, FALSE);
}
typedef struct {
MonoImage *res;
const char* guid;
} GuidData;
static void
find_by_guid (gpointer key, gpointer val, gpointer user_data)
{
GuidData *data = user_data;
MonoImage *image;
if (data->res)
return;
image = val;
if (strcmp (data->guid, mono_image_get_guid (image)) == 0)
data->res = image;
}
MonoImage *
mono_image_loaded_by_guid_full (const char *guid, gboolean refonly)
{
GuidData data;
GHashTable *loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
data.res = NULL;
data.guid = guid;
mono_images_lock ();
g_hash_table_foreach (loaded_images, find_by_guid, &data);
mono_images_unlock ();
return data.res;
}
MonoImage *
mono_image_loaded_by_guid (const char *guid)
{
return mono_image_loaded_by_guid_full (guid, FALSE);
}
static MonoImage *
register_image (MonoImage *image)
{
MonoImage *image2;
GHashTable *loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
mono_images_lock ();
image2 = g_hash_table_lookup (loaded_images, image->name);
if (image2) {
/* Somebody else beat us to it */