2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
21 #include "tabledefs.h"
22 #include "mono-endian.h"
24 #include "tokentype.h"
25 #include "metadata-internals.h"
26 #include "class-internals.h"
29 static gboolean do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
30 const char *ptr, const char **rptr);
32 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
33 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
34 static gboolean mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only);
35 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
36 gboolean signature_only);
37 static void free_generic_inst (MonoGenericInst *ginst);
38 static void free_generic_class (MonoGenericClass *ginst);
39 static void free_inflated_method (MonoMethodInflated *method);
40 static void mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva, MonoMarshalSpec **marshal_spec, gboolean alloc_from_image);
43 * This enumeration is used to describe the data types in the metadata
54 /* Index into Blob heap */
57 /* Index into String heap */
63 /* Pointer into a table */
66 /* HasConstant:Parent pointer (Param, Field or Property) */
69 /* HasCustomAttribute index. Indexes any table except CustomAttribute */
72 /* CustomAttributeType encoded index */
75 /* HasDeclSecurity index: TypeDef Method or Assembly */
78 /* Implementation coded index: File, Export AssemblyRef */
81 /* HasFieldMarshal coded index: Field or Param table */
84 /* MemberForwardedIndex: Field or Method */
87 /* TypeDefOrRef coded index: typedef, typeref, typespec */
90 /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
93 /* MethodDefOrRef coded index: Method or Member Ref table */
96 /* HasSemantic coded index: Event or Property */
99 /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
103 const static unsigned char TableSchemas [] = {
104 #define ASSEMBLY_SCHEMA_OFFSET 0
105 MONO_MT_UINT32, /* "HashId" }, */
106 MONO_MT_UINT16, /* "Major" }, */
107 MONO_MT_UINT16, /* "Minor" }, */
108 MONO_MT_UINT16, /* "BuildNumber" }, */
109 MONO_MT_UINT16, /* "RevisionNumber" }, */
110 MONO_MT_UINT32, /* "Flags" }, */
111 MONO_MT_BLOB_IDX, /* "PublicKey" }, */
112 MONO_MT_STRING_IDX, /* "Name" }, */
113 MONO_MT_STRING_IDX, /* "Culture" }, */
116 #define ASSEMBLYOS_SCHEMA_OFFSET ASSEMBLY_SCHEMA_OFFSET + 10
117 MONO_MT_UINT32, /* "OSPlatformID" }, */
118 MONO_MT_UINT32, /* "OSMajor" }, */
119 MONO_MT_UINT32, /* "OSMinor" }, */
122 #define ASSEMBLYPROC_SCHEMA_OFFSET ASSEMBLYOS_SCHEMA_OFFSET + 4
123 MONO_MT_UINT32, /* "Processor" }, */
126 #define ASSEMBLYREF_SCHEMA_OFFSET ASSEMBLYPROC_SCHEMA_OFFSET + 2
127 MONO_MT_UINT16, /* "Major" }, */
128 MONO_MT_UINT16, /* "Minor" }, */
129 MONO_MT_UINT16, /* "Build" }, */
130 MONO_MT_UINT16, /* "Revision" }, */
131 MONO_MT_UINT32, /* "Flags" }, */
132 MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
133 MONO_MT_STRING_IDX, /* "Name" }, */
134 MONO_MT_STRING_IDX, /* "Culture" }, */
135 MONO_MT_BLOB_IDX, /* "HashValue" }, */
138 #define ASSEMBLYREFOS_SCHEMA_OFFSET ASSEMBLYREF_SCHEMA_OFFSET + 10
139 MONO_MT_UINT32, /* "OSPlatformID" }, */
140 MONO_MT_UINT32, /* "OSMajorVersion" }, */
141 MONO_MT_UINT32, /* "OSMinorVersion" }, */
142 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
145 #define ASSEMBLYREFPROC_SCHEMA_OFFSET ASSEMBLYREFOS_SCHEMA_OFFSET + 5
146 MONO_MT_UINT32, /* "Processor" }, */
147 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
150 #define CLASS_LAYOUT_SCHEMA_OFFSET ASSEMBLYREFPROC_SCHEMA_OFFSET + 3
151 MONO_MT_UINT16, /* "PackingSize" }, */
152 MONO_MT_UINT32, /* "ClassSize" }, */
153 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
156 #define CONSTANT_SCHEMA_OFFSET CLASS_LAYOUT_SCHEMA_OFFSET + 4
157 MONO_MT_UINT8, /* "Type" }, */
158 MONO_MT_UINT8, /* "PaddingZero" }, */
159 MONO_MT_CONST_IDX, /* "Parent" }, */
160 MONO_MT_BLOB_IDX, /* "Value" }, */
163 #define CUSTOM_ATTR_SCHEMA_OFFSET CONSTANT_SCHEMA_OFFSET + 5
164 MONO_MT_HASCAT_IDX, /* "Parent" }, */
165 MONO_MT_CAT_IDX, /* "Type" }, */
166 MONO_MT_BLOB_IDX, /* "Value" }, */
169 #define DECL_SEC_SCHEMA_OFFSET CUSTOM_ATTR_SCHEMA_OFFSET + 4
170 MONO_MT_UINT16, /* "Action" }, */
171 MONO_MT_HASDEC_IDX, /* "Parent" }, */
172 MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
175 #define EVENTMAP_SCHEMA_OFFSET DECL_SEC_SCHEMA_OFFSET + 4
176 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
177 MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
180 #define EVENT_SCHEMA_OFFSET EVENTMAP_SCHEMA_OFFSET + 3
181 MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
182 MONO_MT_STRING_IDX, /* "Name" }, */
183 MONO_MT_TABLE_IDX, /* "EventType" }, TypeDef or TypeRef */
186 #define EVENT_POINTER_SCHEMA_OFFSET EVENT_SCHEMA_OFFSET + 4
187 MONO_MT_TABLE_IDX, /* "Event" }, */
190 #define EXPORTED_TYPE_SCHEMA_OFFSET EVENT_POINTER_SCHEMA_OFFSET + 2
191 MONO_MT_UINT32, /* "Flags" }, */
192 MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
193 MONO_MT_STRING_IDX, /* "TypeName" }, */
194 MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
195 MONO_MT_IMPL_IDX, /* "Implementation" }, */
198 #define FIELD_SCHEMA_OFFSET EXPORTED_TYPE_SCHEMA_OFFSET + 6
199 MONO_MT_UINT16, /* "Flags" }, */
200 MONO_MT_STRING_IDX, /* "Name" }, */
201 MONO_MT_BLOB_IDX, /* "Signature" }, */
204 #define FIELD_LAYOUT_SCHEMA_OFFSET FIELD_SCHEMA_OFFSET + 4
205 MONO_MT_UINT32, /* "Offset" }, */
206 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
209 #define FIELD_MARSHAL_SCHEMA_OFFSET FIELD_LAYOUT_SCHEMA_OFFSET + 3
210 MONO_MT_HFM_IDX, /* "Parent" }, */
211 MONO_MT_BLOB_IDX, /* "NativeType" }, */
214 #define FIELD_RVA_SCHEMA_OFFSET FIELD_MARSHAL_SCHEMA_OFFSET + 3
215 MONO_MT_UINT32, /* "RVA" }, */
216 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
219 #define FIELD_POINTER_SCHEMA_OFFSET FIELD_RVA_SCHEMA_OFFSET + 3
220 MONO_MT_TABLE_IDX, /* "Field" }, */
223 #define FILE_SCHEMA_OFFSET FIELD_POINTER_SCHEMA_OFFSET + 2
224 MONO_MT_UINT32, /* "Flags" }, */
225 MONO_MT_STRING_IDX, /* "Name" }, */
226 MONO_MT_BLOB_IDX, /* "Value" }, */
229 #define IMPLMAP_SCHEMA_OFFSET FILE_SCHEMA_OFFSET + 4
230 MONO_MT_UINT16, /* "MappingFlag" }, */
231 MONO_MT_MF_IDX, /* "MemberForwarded" }, */
232 MONO_MT_STRING_IDX, /* "ImportName" }, */
233 MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
236 #define IFACEMAP_SCHEMA_OFFSET IMPLMAP_SCHEMA_OFFSET + 5
237 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
238 MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
241 #define MANIFEST_SCHEMA_OFFSET IFACEMAP_SCHEMA_OFFSET + 3
242 MONO_MT_UINT32, /* "Offset" }, */
243 MONO_MT_UINT32, /* "Flags" }, */
244 MONO_MT_STRING_IDX, /* "Name" }, */
245 MONO_MT_IMPL_IDX, /* "Implementation" }, */
248 #define MEMBERREF_SCHEMA_OFFSET MANIFEST_SCHEMA_OFFSET + 5
249 MONO_MT_MRP_IDX, /* "Class" }, */
250 MONO_MT_STRING_IDX, /* "Name" }, */
251 MONO_MT_BLOB_IDX, /* "Signature" }, */
254 #define METHOD_SCHEMA_OFFSET MEMBERREF_SCHEMA_OFFSET + 4
255 MONO_MT_UINT32, /* "RVA" }, */
256 MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
257 MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
258 MONO_MT_STRING_IDX, /* "Name" }, */
259 MONO_MT_BLOB_IDX, /* "Signature" }, */
260 MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
263 #define METHOD_IMPL_SCHEMA_OFFSET METHOD_SCHEMA_OFFSET + 7
264 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
265 MONO_MT_MDOR_IDX, /* "MethodBody" }, */
266 MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
269 #define METHOD_SEMA_SCHEMA_OFFSET METHOD_IMPL_SCHEMA_OFFSET + 4
270 MONO_MT_UINT16, /* "MethodSemantic" }, */
271 MONO_MT_TABLE_IDX, /* "Method:Method" }, */
272 MONO_MT_HS_IDX, /* "Association" }, */
275 #define METHOD_POINTER_SCHEMA_OFFSET METHOD_SEMA_SCHEMA_OFFSET + 4
276 MONO_MT_TABLE_IDX, /* "Method" }, */
279 #define MODULE_SCHEMA_OFFSET METHOD_POINTER_SCHEMA_OFFSET + 2
280 MONO_MT_UINT16, /* "Generation" }, */
281 MONO_MT_STRING_IDX, /* "Name" }, */
282 MONO_MT_GUID_IDX, /* "MVID" }, */
283 MONO_MT_GUID_IDX, /* "EncID" }, */
284 MONO_MT_GUID_IDX, /* "EncBaseID" }, */
287 #define MODULEREF_SCHEMA_OFFSET MODULE_SCHEMA_OFFSET + 6
288 MONO_MT_STRING_IDX, /* "Name" }, */
291 #define NESTED_CLASS_SCHEMA_OFFSET MODULEREF_SCHEMA_OFFSET + 2
292 MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
293 MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
296 #define PARAM_SCHEMA_OFFSET NESTED_CLASS_SCHEMA_OFFSET + 3
297 MONO_MT_UINT16, /* "Flags" }, */
298 MONO_MT_UINT16, /* "Sequence" }, */
299 MONO_MT_STRING_IDX, /* "Name" }, */
302 #define PARAM_POINTER_SCHEMA_OFFSET PARAM_SCHEMA_OFFSET + 4
303 MONO_MT_TABLE_IDX, /* "Param" }, */
306 #define PROPERTY_SCHEMA_OFFSET PARAM_POINTER_SCHEMA_OFFSET + 2
307 MONO_MT_UINT16, /* "Flags" }, */
308 MONO_MT_STRING_IDX, /* "Name" }, */
309 MONO_MT_BLOB_IDX, /* "Type" }, */
312 #define PROPERTY_POINTER_SCHEMA_OFFSET PROPERTY_SCHEMA_OFFSET + 4
313 MONO_MT_TABLE_IDX, /* "Property" }, */
316 #define PROPERTY_MAP_SCHEMA_OFFSET PROPERTY_POINTER_SCHEMA_OFFSET + 2
317 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
318 MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
321 #define STDALON_SIG_SCHEMA_OFFSET PROPERTY_MAP_SCHEMA_OFFSET + 3
322 MONO_MT_BLOB_IDX, /* "Signature" }, */
325 #define TYPEDEF_SCHEMA_OFFSET STDALON_SIG_SCHEMA_OFFSET + 2
326 MONO_MT_UINT32, /* "Flags" }, */
327 MONO_MT_STRING_IDX, /* "Name" }, */
328 MONO_MT_STRING_IDX, /* "Namespace" }, */
329 MONO_MT_TDOR_IDX, /* "Extends" }, */
330 MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
331 MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
334 #define TYPEREF_SCHEMA_OFFSET TYPEDEF_SCHEMA_OFFSET + 7
335 MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
336 MONO_MT_STRING_IDX, /* "Name" }, */
337 MONO_MT_STRING_IDX, /* "Namespace" }, */
340 #define TYPESPEC_SCHEMA_OFFSET TYPEREF_SCHEMA_OFFSET + 4
341 MONO_MT_BLOB_IDX, /* "Signature" }, */
344 #define GENPARAM_SCHEMA_OFFSET TYPESPEC_SCHEMA_OFFSET + 2
345 MONO_MT_UINT16, /* "Number" }, */
346 MONO_MT_UINT16, /* "Flags" }, */
347 MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
348 MONO_MT_STRING_IDX, /* "Name" }, */
351 #define METHOD_SPEC_SCHEMA_OFFSET GENPARAM_SCHEMA_OFFSET + 5
352 MONO_MT_MDOR_IDX, /* "Method" }, */
353 MONO_MT_BLOB_IDX, /* "Signature" }, */
356 #define GEN_CONSTRAINT_SCHEMA_OFFSET METHOD_SPEC_SCHEMA_OFFSET + 3
357 MONO_MT_TABLE_IDX, /* "GenericParam" }, */
358 MONO_MT_TDOR_IDX, /* "Constraint" }, */
361 #define NULL_SCHEMA_OFFSET GEN_CONSTRAINT_SCHEMA_OFFSET + 3
365 /* Must be the same order as MONO_TABLE_* */
366 const static unsigned char
367 table_description [] = {
368 MODULE_SCHEMA_OFFSET,
369 TYPEREF_SCHEMA_OFFSET,
370 TYPEDEF_SCHEMA_OFFSET,
371 FIELD_POINTER_SCHEMA_OFFSET,
373 METHOD_POINTER_SCHEMA_OFFSET,
374 METHOD_SCHEMA_OFFSET,
375 PARAM_POINTER_SCHEMA_OFFSET,
377 IFACEMAP_SCHEMA_OFFSET,
378 MEMBERREF_SCHEMA_OFFSET, /* 0xa */
379 CONSTANT_SCHEMA_OFFSET,
380 CUSTOM_ATTR_SCHEMA_OFFSET,
381 FIELD_MARSHAL_SCHEMA_OFFSET,
382 DECL_SEC_SCHEMA_OFFSET,
383 CLASS_LAYOUT_SCHEMA_OFFSET,
384 FIELD_LAYOUT_SCHEMA_OFFSET, /* 0x10 */
385 STDALON_SIG_SCHEMA_OFFSET,
386 EVENTMAP_SCHEMA_OFFSET,
387 EVENT_POINTER_SCHEMA_OFFSET,
389 PROPERTY_MAP_SCHEMA_OFFSET,
390 PROPERTY_POINTER_SCHEMA_OFFSET,
391 PROPERTY_SCHEMA_OFFSET,
392 METHOD_SEMA_SCHEMA_OFFSET,
393 METHOD_IMPL_SCHEMA_OFFSET,
394 MODULEREF_SCHEMA_OFFSET, /* 0x1a */
395 TYPESPEC_SCHEMA_OFFSET,
396 IMPLMAP_SCHEMA_OFFSET,
397 FIELD_RVA_SCHEMA_OFFSET,
400 ASSEMBLY_SCHEMA_OFFSET, /* 0x20 */
401 ASSEMBLYPROC_SCHEMA_OFFSET,
402 ASSEMBLYOS_SCHEMA_OFFSET,
403 ASSEMBLYREF_SCHEMA_OFFSET,
404 ASSEMBLYREFPROC_SCHEMA_OFFSET,
405 ASSEMBLYREFOS_SCHEMA_OFFSET,
407 EXPORTED_TYPE_SCHEMA_OFFSET,
408 MANIFEST_SCHEMA_OFFSET,
409 NESTED_CLASS_SCHEMA_OFFSET,
410 GENPARAM_SCHEMA_OFFSET, /* 0x2a */
411 METHOD_SPEC_SCHEMA_OFFSET,
412 GEN_CONSTRAINT_SCHEMA_OFFSET
415 #ifdef HAVE_ARRAY_ELEM_INIT
416 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
417 #define MSGSTRFIELD1(line) str##line
418 static const struct msgstr_t {
419 #define TABLEDEF(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
420 #include "mono/cil/tables.def"
423 #define TABLEDEF(a,b) b,
424 #include "mono/cil/tables.def"
427 static const gint16 tableidx [] = {
428 #define TABLEDEF(a,b) [a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
429 #include "mono/cil/tables.def"
434 #define TABLEDEF(a,b) b,
435 static const char* const
436 mono_tables_names [] = {
437 #include "mono/cil/tables.def"
443 /* Auxiliary structure used for caching inflated signatures */
445 MonoMethodSignature *sig;
446 MonoGenericContext context;
447 } MonoInflatedMethodSignature;
450 * mono_meta_table_name:
451 * @table: table index
453 * Returns the name of the given ECMA metadata logical format table
454 * as described in ECMA 335, Partition II, Section 22.
456 * Returns: the name for the @table index
459 mono_meta_table_name (int table)
461 if ((table < 0) || (table > MONO_TABLE_LAST))
464 #ifdef HAVE_ARRAY_ELEM_INIT
465 return (const char*)&tablestr + tableidx [table];
467 return mono_tables_names [table];
471 /* The guy who wrote the spec for this should not be allowed near a
474 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
475 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
476 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
477 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
478 inverse of this mapping.
481 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
482 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
484 /* Reference: Partition II - 23.2.6 */
486 * mono_metadata_compute_size:
487 * @meta: metadata context
488 * @tableindex: metadata table number
489 * @result_bitfield: pointer to guint32 where to store additional info
491 * mono_metadata_compute_size() computes the lenght in bytes of a single
492 * row in a metadata table. The size of each column is encoded in the
493 * @result_bitfield return value along with the number of columns in the table.
494 * the resulting bitfield should be handed to the mono_metadata_table_size()
495 * and mono_metadata_table_count() macros.
496 * This is a Mono runtime internal only function.
499 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
501 guint32 bitfield = 0;
502 int size = 0, field_size = 0;
505 const unsigned char *description = TableSchemas + table_description [tableindex];
507 for (i = 0; (code = description [i]) != MONO_MT_END; i++){
510 field_size = 4; break;
513 field_size = 2; break;
516 field_size = 1; break;
518 case MONO_MT_BLOB_IDX:
519 field_size = meta->idx_blob_wide ? 4 : 2; break;
521 case MONO_MT_STRING_IDX:
522 field_size = meta->idx_string_wide ? 4 : 2; break;
524 case MONO_MT_GUID_IDX:
525 field_size = meta->idx_guid_wide ? 4 : 2; break;
527 case MONO_MT_TABLE_IDX:
528 /* Uhm, a table index can point to other tables besides the current one
529 * so, it's not correct to use the rowcount of the current table to
530 * get the size for this column - lupus
532 switch (tableindex) {
533 case MONO_TABLE_ASSEMBLYREFOS:
535 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
536 case MONO_TABLE_ASSEMBLYPROCESSOR:
538 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
539 case MONO_TABLE_CLASSLAYOUT:
541 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
542 case MONO_TABLE_EVENTMAP:
543 g_assert (i == 0 || i == 1);
544 field_size = i ? idx_size (MONO_TABLE_EVENT):
545 idx_size(MONO_TABLE_TYPEDEF);
547 case MONO_TABLE_EVENT:
549 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows, meta->tables [MONO_TABLE_TYPEREF].rows);
550 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
551 /*This is a coded token for 3 tables, so takes 2 bits */
552 field_size = rtsize (n, 16 - MONO_TYPEDEFORREF_BITS);
554 case MONO_TABLE_EVENT_POINTER:
556 field_size = idx_size (MONO_TABLE_EVENT); break;
557 case MONO_TABLE_EXPORTEDTYPE:
559 /* the index is in another metadata file, so it must be 4 */
560 field_size = 4; break;
561 case MONO_TABLE_FIELDLAYOUT:
563 field_size = idx_size (MONO_TABLE_FIELD); break;
564 case MONO_TABLE_FIELDRVA:
566 field_size = idx_size (MONO_TABLE_FIELD); break;
567 case MONO_TABLE_FIELD_POINTER:
569 field_size = idx_size (MONO_TABLE_FIELD); break;
570 case MONO_TABLE_IMPLMAP:
572 field_size = idx_size (MONO_TABLE_MODULEREF); break;
573 case MONO_TABLE_INTERFACEIMPL:
575 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
576 case MONO_TABLE_METHOD:
578 field_size = idx_size (MONO_TABLE_PARAM); break;
579 case MONO_TABLE_METHODIMPL:
581 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
582 case MONO_TABLE_METHODSEMANTICS:
584 field_size = idx_size (MONO_TABLE_METHOD); break;
585 case MONO_TABLE_METHOD_POINTER:
587 field_size = idx_size (MONO_TABLE_METHOD); break;
588 case MONO_TABLE_NESTEDCLASS:
589 g_assert (i == 0 || i == 1);
590 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
591 case MONO_TABLE_PARAM_POINTER:
593 field_size = idx_size (MONO_TABLE_PARAM); break;
594 case MONO_TABLE_PROPERTYMAP:
595 g_assert (i == 0 || i == 1);
596 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
597 idx_size(MONO_TABLE_TYPEDEF);
599 case MONO_TABLE_PROPERTY_POINTER:
601 field_size = idx_size (MONO_TABLE_PROPERTY); break;
602 case MONO_TABLE_TYPEDEF:
603 g_assert (i == 4 || i == 5);
604 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
605 idx_size(MONO_TABLE_METHOD);
607 case MONO_TABLE_GENERICPARAM:
608 g_assert (i == 2 || i == 4 || i == 5);
610 n = MAX (meta->tables [MONO_TABLE_METHOD].rows, meta->tables [MONO_TABLE_TYPEDEF].rows);
611 /*This is a coded token for 2 tables, so takes 1 bit */
612 field_size = rtsize (n, 16 - MONO_TYPEORMETHOD_BITS);
614 field_size = idx_size (MONO_TABLE_TYPEDEF);
616 field_size = idx_size (MONO_TABLE_TYPEDEF);
619 case MONO_TABLE_GENERICPARAMCONSTRAINT:
621 field_size = idx_size (MONO_TABLE_GENERICPARAM);
625 g_assert_not_reached ();
630 * HasConstant: ParamDef, FieldDef, Property
632 case MONO_MT_CONST_IDX:
633 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
634 meta->tables [MONO_TABLE_FIELD].rows);
635 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
637 /* 2 bits to encode tag */
638 field_size = rtsize (n, 16-2);
642 * HasCustomAttribute: points to any table but
645 case MONO_MT_HASCAT_IDX:
647 * We believe that since the signature and
648 * permission are indexing the Blob heap,
649 * we should consider the blob size first
651 /* I'm not a believer - lupus
652 if (meta->idx_blob_wide){
657 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
658 meta->tables [MONO_TABLE_FIELD].rows);
659 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
660 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
661 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
662 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
663 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
664 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
665 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
666 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
667 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
668 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
669 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
670 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
671 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
672 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
673 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
674 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
675 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
677 /* 5 bits to encode */
678 field_size = rtsize (n, 16-5);
682 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
683 * MemberRef and String.
685 case MONO_MT_CAT_IDX:
686 /* String is a heap, if it is wide, we know the size */
688 if (meta->idx_string_wide){
693 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
694 meta->tables [MONO_TABLE_TYPEDEF].rows);
695 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
696 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
698 /* 3 bits to encode */
699 field_size = rtsize (n, 16-3);
703 * HasDeclSecurity: Typedef, MethodDef, Assembly
705 case MONO_MT_HASDEC_IDX:
706 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
707 meta->tables [MONO_TABLE_METHOD].rows);
708 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
710 /* 2 bits to encode */
711 field_size = rtsize (n, 16-2);
715 * Implementation: File, AssemblyRef, ExportedType
717 case MONO_MT_IMPL_IDX:
718 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
719 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
720 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
722 /* 2 bits to encode tag */
723 field_size = rtsize (n, 16-2);
727 * HasFieldMarshall: FieldDef, ParamDef
729 case MONO_MT_HFM_IDX:
730 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
731 meta->tables [MONO_TABLE_PARAM].rows);
733 /* 1 bit used to encode tag */
734 field_size = rtsize (n, 16-1);
738 * MemberForwarded: FieldDef, MethodDef
741 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
742 meta->tables [MONO_TABLE_METHOD].rows);
744 /* 1 bit used to encode tag */
745 field_size = rtsize (n, 16-1);
749 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
751 * It is TypeDef, _TypeRef_, TypeSpec, instead.
753 case MONO_MT_TDOR_IDX:
754 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
755 meta->tables [MONO_TABLE_TYPEREF].rows);
756 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
758 /* 2 bits to encode */
759 field_size = rtsize (n, 16-2);
763 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
765 case MONO_MT_MRP_IDX:
766 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
767 meta->tables [MONO_TABLE_TYPEREF].rows);
768 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
769 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
770 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
771 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
773 /* 3 bits to encode */
774 field_size = rtsize (n, 16 - 3);
778 * MethodDefOrRef: MethodDef, MemberRef
780 case MONO_MT_MDOR_IDX:
781 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
782 meta->tables [MONO_TABLE_MEMBERREF].rows);
784 /* 1 bit used to encode tag */
785 field_size = rtsize (n, 16-1);
789 * HasSemantics: Property, Event
792 n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
793 meta->tables [MONO_TABLE_EVENT].rows);
795 /* 1 bit used to encode tag */
796 field_size = rtsize (n, 16-1);
800 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
803 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
804 meta->tables [MONO_TABLE_MODULEREF].rows);
805 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
806 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
808 /* 2 bits used to encode tag (ECMA spec claims 3) */
809 field_size = rtsize (n, 16 - 2);
814 * encode field size as follows (we just need to
821 bitfield |= (field_size-1) << shift;
824 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
827 *result_bitfield = (i << 24) | bitfield;
832 * mono_metadata_compute_table_bases:
833 * @meta: metadata context to compute table values
835 * Computes the table bases for the metadata structure.
836 * This is an internal function used by the image loader code.
839 mono_metadata_compute_table_bases (MonoImage *meta)
842 const char *base = meta->tables_base;
844 for (i = 0; i < MONO_TABLE_NUM; i++) {
845 MonoTableInfo *table = &meta->tables [i];
846 if (table->rows == 0)
849 table->row_size = mono_metadata_compute_size (meta, i, &table->size_bitfield);
851 base += table->rows * table->row_size;
856 * mono_metadata_locate:
857 * @meta: metadata context
858 * @table: table code.
859 * @idx: index of element to retrieve from @table.
861 * Returns: a pointer to the @idx element in the metadata table
862 * whose code is @table.
865 mono_metadata_locate (MonoImage *meta, int table, int idx)
867 /* idx == 0 refers always to NULL */
868 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
870 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
874 * mono_metadata_locate_token:
875 * @meta: metadata context
876 * @token: metadata token
878 * Returns: a pointer to the data in the metadata represented by the
882 mono_metadata_locate_token (MonoImage *meta, guint32 token)
884 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
888 * mono_metadata_string_heap:
889 * @meta: metadata context
890 * @index: index into the string heap.
892 * Returns: an in-memory pointer to the @index in the string heap.
895 mono_metadata_string_heap (MonoImage *meta, guint32 index)
897 g_return_val_if_fail (index < meta->heap_strings.size, "");
898 return meta->heap_strings.data + index;
902 * mono_metadata_user_string:
903 * @meta: metadata context
904 * @index: index into the user string heap.
906 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
909 mono_metadata_user_string (MonoImage *meta, guint32 index)
911 g_return_val_if_fail (index < meta->heap_us.size, "");
912 return meta->heap_us.data + index;
916 * mono_metadata_blob_heap:
917 * @meta: metadata context
918 * @index: index into the blob.
920 * Returns: an in-memory pointer to the @index in the Blob heap.
923 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
925 g_return_val_if_fail (index < meta->heap_blob.size, "");
926 return meta->heap_blob.data + index;
930 * mono_metadata_guid_heap:
931 * @meta: metadata context
932 * @index: index into the guid heap.
934 * Returns: an in-memory pointer to the @index in the guid heap.
937 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
940 index *= 16; /* adjust for guid size and 1-based index */
941 g_return_val_if_fail (index < meta->heap_guid.size, "");
942 return meta->heap_guid.data + index;
945 static const unsigned char *
946 dword_align (const unsigned char *ptr)
948 #if SIZEOF_VOID_P == 8
949 return (const unsigned char *) (((guint64) (ptr + 3)) & ~3);
951 return (const unsigned char *) (((guint32) (ptr + 3)) & ~3);
956 * mono_metadata_decode_row:
957 * @t: table to extract information from.
958 * @idx: index in table.
959 * @res: array of @res_size cols to store the results in
961 * This decompresses the metadata element @idx in table @t
962 * into the guint32 @res array that has res_size elements
965 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
967 guint32 bitfield = t->size_bitfield;
968 int i, count = mono_metadata_table_count (bitfield);
971 g_assert (idx < t->rows);
972 data = t->base + idx * t->row_size;
974 g_assert (res_size == count);
976 for (i = 0; i < count; i++) {
977 int n = mono_metadata_table_size (bitfield, i);
981 res [i] = *data; break;
983 res [i] = read16 (data); break;
985 res [i] = read32 (data); break;
987 g_assert_not_reached ();
994 * mono_metadata_decode_row_col:
995 * @t: table to extract information from.
996 * @idx: index for row in table.
997 * @col: column in the row.
999 * This function returns the value of column @col from the @idx
1000 * row in the table @t.
1003 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
1005 guint32 bitfield = t->size_bitfield;
1007 register const char *data;
1010 g_assert (idx < t->rows);
1011 g_assert (col < mono_metadata_table_count (bitfield));
1012 data = t->base + idx * t->row_size;
1014 n = mono_metadata_table_size (bitfield, 0);
1015 for (i = 0; i < col; ++i) {
1017 n = mono_metadata_table_size (bitfield, i + 1);
1023 return read16 (data);
1025 return read32 (data);
1027 g_assert_not_reached ();
1033 * mono_metadata_decode_blob_size:
1034 * @ptr: pointer to a blob object
1035 * @rptr: the new position of the pointer
1037 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
1039 * Returns: the size of the blob object
1042 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
1044 const unsigned char *ptr = (const unsigned char *)xptr;
1047 if ((*ptr & 0x80) == 0){
1048 size = ptr [0] & 0x7f;
1050 } else if ((*ptr & 0x40) == 0){
1051 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
1054 size = ((ptr [0] & 0x1f) << 24) +
1066 * mono_metadata_decode_value:
1067 * @ptr: pointer to decode from
1068 * @rptr: the new position of the pointer
1070 * This routine decompresses 32-bit values as specified in the "Blob and
1071 * Signature" section (22.2)
1073 * Returns: the decoded value
1076 mono_metadata_decode_value (const char *_ptr, const char **rptr)
1078 const unsigned char *ptr = (const unsigned char *) _ptr;
1079 unsigned char b = *ptr;
1082 if ((b & 0x80) == 0){
1085 } else if ((b & 0x40) == 0){
1086 len = ((b & 0x3f) << 8 | ptr [1]);
1089 len = ((b & 0x1f) << 24) |
1102 * mono_metadata_decode_signed_value:
1103 * @ptr: pointer to decode from
1104 * @rptr: the new position of the pointer
1106 * This routine decompresses 32-bit signed values
1107 * (not specified in the spec)
1109 * Returns: the decoded value
1112 mono_metadata_decode_signed_value (const char *ptr, const char **rptr)
1114 guint32 uval = mono_metadata_decode_value (ptr, rptr);
1115 gint32 ival = uval >> 1;
1118 /* ival is a truncated 2's complement negative number. */
1120 /* 6 bits = 7 bits for compressed representation (top bit is '0') - 1 sign bit */
1123 /* 13 bits = 14 bits for compressed representation (top bits are '10') - 1 sign bit */
1124 return ival - 0x2000;
1125 if (ival < 0x10000000)
1126 /* 28 bits = 29 bits for compressed representation (top bits are '110') - 1 sign bit */
1127 return ival - 0x10000000;
1128 g_assert (ival < 0x20000000);
1129 g_warning ("compressed signed value appears to use 29 bits for compressed representation: %x (raw: %8x)", ival, uval);
1130 return ival - 0x20000000;
1134 * Translates the given 1-based index into the Method, Field, Event, or Param tables
1135 * using the *Ptr tables in uncompressed metadata, if they are available.
1137 * FIXME: The caller is not forced to call this function, which is error-prone, since
1138 * forgetting to call it would only show up as a bug on uncompressed metadata.
1141 mono_metadata_translate_token_index (MonoImage *image, int table, guint32 idx)
1143 if (!image->uncompressed_metadata)
1147 case MONO_TABLE_METHOD:
1148 if (image->tables [MONO_TABLE_METHOD_POINTER].rows)
1149 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD_POINTER], idx - 1, MONO_METHOD_POINTER_METHOD);
1152 case MONO_TABLE_FIELD:
1153 if (image->tables [MONO_TABLE_FIELD_POINTER].rows)
1154 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_FIELD_POINTER], idx - 1, MONO_FIELD_POINTER_FIELD);
1157 case MONO_TABLE_EVENT:
1158 if (image->tables [MONO_TABLE_EVENT_POINTER].rows)
1159 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_EVENT_POINTER], idx - 1, MONO_EVENT_POINTER_EVENT);
1162 case MONO_TABLE_PROPERTY:
1163 if (image->tables [MONO_TABLE_PROPERTY_POINTER].rows)
1164 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PROPERTY_POINTER], idx - 1, MONO_PROPERTY_POINTER_PROPERTY);
1167 case MONO_TABLE_PARAM:
1168 if (image->tables [MONO_TABLE_PARAM_POINTER].rows)
1169 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PARAM_POINTER], idx - 1, MONO_PARAM_POINTER_PARAM);
1178 * mono_metadata_decode_table_row:
1180 * Same as mono_metadata_decode_row, but takes an IMAGE+TABLE ID pair, and takes
1181 * uncompressed metadata into account, so it should be used to access the
1182 * Method, Field, Param and Event tables when the access is made from metadata, i.e.
1183 * IDX is retrieved from a metadata table, like MONO_TYPEDEF_FIELD_LIST.
1186 mono_metadata_decode_table_row (MonoImage *image, int table, int idx, guint32 *res, int res_size)
1188 if (image->uncompressed_metadata)
1189 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1191 mono_metadata_decode_row (&image->tables [table], idx, res, res_size);
1195 * mono_metadata_decode_table_row_col:
1197 * Same as mono_metadata_decode_row_col, but takes an IMAGE+TABLE ID pair, and takes
1198 * uncompressed metadata into account, so it should be used to access the
1199 * Method, Field, Param and Event tables.
1201 guint32 mono_metadata_decode_table_row_col (MonoImage *image, int table, int idx, guint col)
1203 if (image->uncompressed_metadata)
1204 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1206 return mono_metadata_decode_row_col (&image->tables [table], idx, col);
1210 * mono_metadata_parse_typedef_or_ref:
1211 * @m: a metadata context.
1212 * @ptr: a pointer to an encoded TypedefOrRef in @m
1213 * @rptr: pointer updated to match the end of the decoded stream
1215 * Returns: a token valid in the @m metadata decoded from
1216 * the compressed representation.
1219 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1222 token = mono_metadata_decode_value (ptr, &ptr);
1225 return mono_metadata_token_from_dor (token);
1229 * mono_metadata_parse_custom_mod:
1230 * @m: a metadata context.
1231 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1232 * @ptr: a pointer to (possibly) the start of a custom modifier list
1233 * @rptr: pointer updated to match the end of the decoded stream
1235 * Checks if @ptr points to a type custom modifier compressed representation.
1237 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1240 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1242 MonoCustomMod local;
1243 if ((*ptr == MONO_TYPE_CMOD_OPT) || (*ptr == MONO_TYPE_CMOD_REQD)) {
1246 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1247 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1254 * mono_metadata_parse_array_full:
1255 * @m: a metadata context.
1256 * @ptr: a pointer to an encoded array description.
1257 * @rptr: pointer updated to match the end of the decoded stream
1259 * Decodes the compressed array description found in the metadata @m at @ptr.
1261 * Returns: a #MonoArrayType structure describing the array type
1262 * and dimensions. Memory is allocated from the image mempool.
1264 * LOCKING: Acquires the loader lock
1267 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContainer *container,
1268 const char *ptr, const char **rptr)
1271 MonoArrayType *array;
1274 array = mono_image_alloc0 (m, sizeof (MonoArrayType));
1275 etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1278 array->eklass = mono_class_from_mono_type (etype);
1279 array->rank = mono_metadata_decode_value (ptr, &ptr);
1281 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1282 if (array->numsizes)
1283 array->sizes = g_new0 (int, array->numsizes);
1284 for (i = 0; i < array->numsizes; ++i)
1285 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1287 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1288 if (array->numlobounds)
1289 array->lobounds = g_new0 (int, array->numlobounds);
1290 for (i = 0; i < array->numlobounds; ++i)
1291 array->lobounds [i] = mono_metadata_decode_signed_value (ptr, &ptr);
1299 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1301 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1305 * mono_metadata_free_array:
1306 * @array: array description
1308 * Frees the array description returned from mono_metadata_parse_array().
1311 mono_metadata_free_array (MonoArrayType *array)
1313 g_free (array->sizes);
1314 g_free (array->lobounds);
1319 * need to add common field and param attributes combinations:
1322 * public static literal
1325 * private static literal
1327 static const MonoType
1329 /* data, attrs, type, nmods, byref, pinned */
1330 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1331 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1332 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1333 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1334 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1335 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1336 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1337 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1338 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1339 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1340 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1341 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1342 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1343 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1344 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1345 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1346 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1347 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1348 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1349 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1350 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1351 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1352 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1353 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1354 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1355 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1356 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1357 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1358 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1359 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1360 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1361 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1362 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1363 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1366 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1368 static GHashTable *type_cache = NULL;
1369 static GHashTable *generic_inst_cache = NULL;
1370 static GHashTable *generic_class_cache = NULL;
1371 static int next_generic_inst_id = 0;
1374 * Protected by the loader lock.
1375 * It has a MonoMethodInflated* as key and value.
1376 * The key lookup will just access the declaring and context fields
1378 static GHashTable *generic_method_cache = NULL;
1381 * Protected by the loader lock.
1382 * It has a MonoInflatedMethodSignature* as key and value.
1384 static GHashTable *generic_signature_cache = NULL;
1386 static guint mono_generic_class_hash (gconstpointer data);
1389 * MonoTypes with modifies are never cached, so we never check or use that field.
1392 mono_type_hash (gconstpointer data)
1394 const MonoType *type = (const MonoType *) data;
1395 if (type->type == MONO_TYPE_GENERICINST)
1396 return mono_generic_class_hash (type->data.generic_class);
1398 return type->type | (type->byref << 8) | (type->attrs << 9);
1402 mono_type_equal (gconstpointer ka, gconstpointer kb)
1404 const MonoType *a = (const MonoType *) ka;
1405 const MonoType *b = (const MonoType *) kb;
1407 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1409 /* need other checks */
1414 mono_metadata_generic_inst_hash (gconstpointer data)
1416 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1420 for (i = 0; i < ginst->type_argc; ++i) {
1422 hash += mono_metadata_type_hash (ginst->type_argv [i]);
1425 return hash ^ (ginst->is_open << 8);
1429 mono_generic_inst_equal_full (const MonoGenericInst *a, const MonoGenericInst *b, gboolean signature_only)
1433 if (a->id && b->id) {
1436 if (!signature_only)
1440 if (a->is_open != b->is_open || a->type_argc != b->type_argc)
1442 for (i = 0; i < a->type_argc; ++i) {
1443 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], signature_only))
1450 mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1452 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1453 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1455 return mono_generic_inst_equal_full (a, b, FALSE);
1459 mono_generic_class_hash (gconstpointer data)
1461 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1462 guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
1465 hash += gclass->is_tb_open;
1466 hash += mono_metadata_generic_context_hash (&gclass->context);
1472 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1474 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1475 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1477 return _mono_metadata_generic_class_equal (a, b, FALSE);
1481 * mono_metadata_init:
1483 * Initialize the global variables of this module.
1484 * This is a Mono runtime internal function.
1487 mono_metadata_init (void)
1491 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1492 generic_inst_cache = g_hash_table_new_full (mono_metadata_generic_inst_hash, mono_metadata_generic_inst_equal, NULL, (GDestroyNotify)free_generic_inst);
1493 generic_class_cache = g_hash_table_new_full (mono_generic_class_hash, mono_generic_class_equal, NULL, (GDestroyNotify)free_generic_class);
1495 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1496 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1500 * mono_metadata_cleanup:
1502 * Free all resources used by this module.
1503 * This is a Mono runtime internal function.
1506 mono_metadata_cleanup (void)
1508 g_hash_table_destroy (type_cache);
1509 g_hash_table_destroy (generic_inst_cache);
1510 g_hash_table_destroy (generic_class_cache);
1511 if (generic_method_cache)
1512 g_hash_table_destroy (generic_method_cache);
1513 if (generic_signature_cache)
1514 g_hash_table_destroy (generic_signature_cache);
1516 generic_inst_cache = NULL;
1517 generic_class_cache = NULL;
1518 generic_method_cache = NULL;
1519 generic_signature_cache = NULL;
1523 * mono_metadata_parse_type:
1524 * @m: metadata context
1525 * @mode: king of type that may be found at @ptr
1526 * @opt_attrs: optional attributes to store in the returned type
1527 * @ptr: pointer to the type representation
1528 * @rptr: pointer updated to match the end of the decoded stream
1530 * Decode a compressed type description found at @ptr in @m.
1531 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1532 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1533 * This function can be used to decode type descriptions in method signatures,
1534 * field signatures, locals signatures etc.
1536 * To parse a generic type, `generic_container' points to the current class'es
1537 * (the `generic_container' field in the MonoClass) or the current generic method's
1538 * (the `generic_container' field in the MonoMethodNormal) generic container.
1539 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1540 * this MonoGenericContainer.
1541 * This is a Mono runtime internal function.
1543 * LOCKING: Acquires the loader lock.
1545 * Returns: a #MonoType structure representing the decoded type.
1548 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, MonoParseTypeMode mode,
1549 short opt_attrs, const char *ptr, const char **rptr)
1551 MonoType *type, *cached;
1553 gboolean byref = FALSE;
1554 gboolean pinned = FALSE;
1555 const char *tmp_ptr;
1560 * According to the spec, custom modifiers should come before the byref
1561 * flag, but the IL produced by ilasm from the following signature:
1562 * object modopt(...) &
1563 * starts with a byref flag, followed by the modifiers. (bug #49802)
1564 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1565 * it would be better to treat byref as real type constructor instead of
1567 * Also, pinned should come before anything else, but some MSV++ produced
1568 * assemblies violate this (#bug 61990).
1571 /* Count the modifiers first */
1576 case MONO_TYPE_PINNED:
1577 case MONO_TYPE_BYREF:
1580 case MONO_TYPE_CMOD_REQD:
1581 case MONO_TYPE_CMOD_OPT:
1583 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1591 type = mono_image_alloc0 (m, sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1592 type->num_mods = count;
1594 g_warning ("got more than 64 modifiers in type");
1597 memset (type, 0, sizeof (MonoType));
1600 /* Parse pinned, byref and custom modifiers */
1605 case MONO_TYPE_PINNED:
1609 case MONO_TYPE_BYREF:
1613 case MONO_TYPE_CMOD_REQD:
1614 case MONO_TYPE_CMOD_OPT:
1615 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1623 type->attrs = opt_attrs;
1624 type->byref = byref;
1625 type->pinned = pinned ? 1 : 0;
1627 if (!do_mono_metadata_parse_type (type, m, container, ptr, &ptr)) {
1634 if (!type->num_mods) {
1635 /* no need to free type here, because it is on the stack */
1636 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1637 MonoType *ret = type->byref ? &type->data.klass->this_arg : &type->data.klass->byval_arg;
1639 /* Consider the case:
1641 class Foo<T> { class Bar {} }
1642 class Test : Foo<Test>.Bar {}
1644 When Foo<Test> is being expanded, 'Test' isn't yet initialized. It's actually in
1645 a really pristine state: it doesn't even know whether 'Test' is a reference or a value type.
1647 We ensure that the MonoClass is in a state that we can canonicalize to:
1649 klass->byval_arg.data.klass == klass
1650 klass->this_arg.data.klass == klass
1652 If we can't canonicalize 'type', it doesn't matter, since later users of 'type' will do it.
1654 LOCKING: even though we don't explicitly hold a lock, in the problematic case 'ret' is a field
1655 of a MonoClass which currently holds the loader lock. 'type' is local.
1657 if (ret->data.klass == type->data.klass) {
1661 /* No need to use locking since nobody is modifying the hash table */
1662 if ((cached = g_hash_table_lookup (type_cache, type))) {
1667 /* printf ("%x %x %c %s\n", type->attrs, type->num_mods, type->pinned ? 'p' : ' ', mono_type_full_name (type)); */
1669 if (type == &stype) {
1670 type = mono_image_alloc (m, sizeof (MonoType));
1671 memcpy (type, &stype, sizeof (MonoType));
1677 * LOCKING: Acquires the loader lock.
1680 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1681 const char *ptr, const char **rptr)
1683 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1687 * mono_metadata_get_param_attrs:
1689 * Return the parameter attributes for the method whose MethodDef index is DEF. The
1690 * returned memory needs to be freed by the caller. If all the param attributes are
1691 * 0, then NULL is returned.
1694 mono_metadata_get_param_attrs (MonoImage *m, int def)
1696 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1697 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1698 guint32 cols [MONO_PARAM_SIZE];
1699 guint lastp, i, param_index = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_METHOD], def - 1, MONO_METHOD_PARAMLIST);
1702 if (def < methodt->rows)
1703 lastp = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_METHOD], def, MONO_METHOD_PARAMLIST);
1705 lastp = paramt->rows + 1;
1707 for (i = param_index; i < lastp; ++i) {
1708 mono_metadata_decode_row (&m->tables [MONO_TABLE_PARAM], i - 1, cols, MONO_PARAM_SIZE);
1709 if (cols [MONO_PARAM_FLAGS]) {
1711 pattrs = g_new0 (int, 1 + (lastp - param_index));
1712 pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
1720 * mono_metadata_parse_signature_full:
1721 * @image: metadata context
1722 * @generic_container: generic container
1723 * @toke: metadata token
1725 * Decode a method signature stored in the STANDALONESIG table
1727 * LOCKING: Assumes the loader lock is held.
1729 * Returns: a MonoMethodSignature describing the signature.
1731 MonoMethodSignature*
1732 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContainer *generic_container, guint32 token)
1734 MonoTableInfo *tables = image->tables;
1735 guint32 idx = mono_metadata_token_index (token);
1740 return mono_lookup_dynamic_token (image, token, NULL);
1742 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1744 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1746 ptr = mono_metadata_blob_heap (image, sig);
1747 mono_metadata_decode_blob_size (ptr, &ptr);
1749 return mono_metadata_parse_method_signature_full (image, generic_container, 0, ptr, NULL);
1753 * mono_metadata_parse_signature:
1754 * @image: metadata context
1755 * @toke: metadata token
1757 * Decode a method signature stored in the STANDALONESIG table
1759 * Returns: a MonoMethodSignature describing the signature.
1761 MonoMethodSignature*
1762 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1764 return mono_metadata_parse_signature_full (image, NULL, token);
1768 * mono_metadata_signature_alloc:
1769 * @image: metadata context
1770 * @nparmas: number of parameters in the signature
1772 * Allocate a MonoMethodSignature structure with the specified number of params.
1773 * The return type and the params types need to be filled later.
1774 * This is a Mono runtime internal function.
1776 * LOCKING: Assumes the loader lock is held.
1778 * Returns: the new MonoMethodSignature structure.
1780 MonoMethodSignature*
1781 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1783 MonoMethodSignature *sig;
1785 sig = mono_image_alloc0 (m, sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1786 sig->param_count = nparams;
1787 sig->sentinelpos = -1;
1792 static MonoMethodSignature*
1793 mono_metadata_signature_dup_internal (MonoImage *image, MonoMemPool *mp, MonoMethodSignature *sig)
1796 MonoMethodSignature *ret;
1797 sigsize = sizeof (MonoMethodSignature) + (sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *);
1800 ret = mono_image_alloc (image, sigsize);
1802 ret = mono_mempool_alloc (mp, sigsize);
1804 ret = g_malloc (sigsize);
1806 memcpy (ret, sig, sigsize);
1810 MonoMethodSignature*
1811 mono_metadata_signature_dup_full (MonoImage *image, MonoMethodSignature *sig)
1813 return mono_metadata_signature_dup_internal (image, NULL, sig);
1816 /*The mempool is accessed without synchronization*/
1817 MonoMethodSignature*
1818 mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig)
1820 return mono_metadata_signature_dup_internal (NULL, mp, sig);
1824 * mono_metadata_signature_dup:
1825 * @sig: method signature
1827 * Duplicate an existing MonoMethodSignature so it can be modified.
1828 * This is a Mono runtime internal function.
1830 * Returns: the new MonoMethodSignature structure.
1832 MonoMethodSignature*
1833 mono_metadata_signature_dup (MonoMethodSignature *sig)
1835 return mono_metadata_signature_dup_full (NULL, sig);
1839 * mono_metadata_signature_size:
1841 * Return the amount of memory allocated to SIG.
1844 mono_metadata_signature_size (MonoMethodSignature *sig)
1846 return sizeof (MonoMethodSignature) + (sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *);
1850 * mono_metadata_parse_method_signature:
1851 * @m: metadata context
1852 * @generic_container: generics container
1853 * @def: the MethodDef index or 0 for Ref signatures.
1854 * @ptr: pointer to the signature metadata representation
1855 * @rptr: pointer updated to match the end of the decoded stream
1857 * Decode a method signature stored at @ptr.
1858 * This is a Mono runtime internal function.
1860 * LOCKING: Assumes the loader lock is held.
1862 * Returns: a MonoMethodSignature describing the signature.
1864 MonoMethodSignature *
1865 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *container,
1866 int def, const char *ptr, const char **rptr)
1868 MonoMethodSignature *method;
1869 int i, *pattrs = NULL;
1870 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1871 guint32 gen_param_count = 0;
1872 gboolean is_open = FALSE;
1875 gen_param_count = 1;
1880 call_convention = *ptr & 0x0F;
1882 if (gen_param_count)
1883 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1884 param_count = mono_metadata_decode_value (ptr, &ptr);
1887 pattrs = mono_metadata_get_param_attrs (m, def);
1888 method = mono_metadata_signature_alloc (m, param_count);
1889 method->hasthis = hasthis;
1890 method->explicit_this = explicit_this;
1891 method->call_convention = call_convention;
1892 method->generic_param_count = gen_param_count;
1894 if (call_convention != 0xa) {
1895 method->ret = mono_metadata_parse_type_full (m, container, MONO_PARSE_RET, pattrs ? pattrs [0] : 0, ptr, &ptr);
1897 mono_metadata_free_method_signature (method);
1901 is_open = mono_class_is_open_constructed_type (method->ret);
1904 for (i = 0; i < method->param_count; ++i) {
1905 if (*ptr == MONO_TYPE_SENTINEL) {
1906 if (method->call_convention != MONO_CALL_VARARG || def)
1907 g_error ("found sentinel for methoddef or no vararg method");
1908 if (method->sentinelpos >= 0)
1909 g_error ("found sentinel twice in the same signature");
1910 method->sentinelpos = i;
1913 method->params [i] = mono_metadata_parse_type_full (m, container, MONO_PARSE_PARAM, pattrs ? pattrs [i+1] : 0, ptr, &ptr);
1914 if (!method->params [i]) {
1915 mono_metadata_free_method_signature (method);
1920 is_open = mono_class_is_open_constructed_type (method->params [i]);
1923 /* The sentinel could be missing if the caller does not pass any additional arguments */
1924 if (!def && method->call_convention == MONO_CALL_VARARG && method->sentinelpos < 0)
1925 method->sentinelpos = method->param_count;
1927 method->has_type_parameters = is_open;
1929 if (def && (method->call_convention == MONO_CALL_VARARG))
1930 method->sentinelpos = method->param_count;
1937 * Add signature to a cache and increase ref count...
1944 * mono_metadata_parse_method_signature:
1945 * @m: metadata context
1946 * @def: the MethodDef index or 0 for Ref signatures.
1947 * @ptr: pointer to the signature metadata representation
1948 * @rptr: pointer updated to match the end of the decoded stream
1950 * Decode a method signature stored at @ptr.
1951 * This is a Mono runtime internal function.
1953 * LOCKING: Assumes the loader lock is held.
1955 * Returns: a MonoMethodSignature describing the signature.
1957 MonoMethodSignature *
1958 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1960 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1964 * mono_metadata_free_method_signature:
1965 * @sig: signature to destroy
1967 * Free the memory allocated in the signature @sig.
1968 * This method needs to be robust and work also on partially-built
1969 * signatures, so it does extra checks.
1972 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1974 /* Everything is allocated from mempools */
1978 mono_metadata_free_type (sig->ret);
1979 for (i = 0; i < sig->param_count; ++i) {
1980 if (sig->params [i])
1981 mono_metadata_free_type (sig->params [i]);
1987 mono_metadata_free_inflated_signature (MonoMethodSignature *sig)
1991 /* Allocated in inflate_generic_signature () */
1993 mono_metadata_free_type (sig->ret);
1994 for (i = 0; i < sig->param_count; ++i) {
1995 if (sig->params [i])
1996 mono_metadata_free_type (sig->params [i]);
2002 inflated_method_equal (gconstpointer a, gconstpointer b)
2004 const MonoMethodInflated *ma = a;
2005 const MonoMethodInflated *mb = b;
2006 if (ma->declaring != mb->declaring)
2008 if (ma->method.method.is_mb_open != mb->method.method.is_mb_open)
2010 return mono_metadata_generic_context_equal (&ma->context, &mb->context);
2014 inflated_method_hash (gconstpointer a)
2016 const MonoMethodInflated *ma = a;
2017 return (mono_metadata_generic_context_hash (&ma->context) ^ mono_aligned_addr_hash (ma->declaring)) + ma->method.method.is_mb_open;
2021 inflated_signature_equal (gconstpointer a, gconstpointer b)
2023 const MonoInflatedMethodSignature *sig1 = a;
2024 const MonoInflatedMethodSignature *sig2 = b;
2026 /* sig->sig is assumed to be canonized */
2027 if (sig1->sig != sig2->sig)
2029 /* The generic instances are canonized */
2030 return mono_metadata_generic_context_equal (&sig1->context, &sig2->context);
2034 inflated_signature_hash (gconstpointer a)
2036 const MonoInflatedMethodSignature *sig = a;
2038 /* sig->sig is assumed to be canonized */
2039 return mono_metadata_generic_context_hash (&sig->context) ^ mono_aligned_addr_hash (sig->sig);
2043 dump_ginst (MonoGenericInst *ginst)
2048 g_print ("Ginst: <");
2049 for (i = 0; i < ginst->type_argc; ++i) {
2052 name = mono_type_get_name (ginst->type_argv [i]);
2053 g_print ("%s", name);
2059 static gboolean type_in_image (MonoType *type, MonoImage *image);
2062 signature_in_image (MonoMethodSignature *sig, MonoImage *image)
2064 gpointer iter = NULL;
2067 while ((p = mono_signature_get_params (sig, &iter)) != NULL)
2068 if (type_in_image (p, image))
2071 return type_in_image (mono_signature_get_return_type (sig), image);
2075 ginst_in_image (MonoGenericInst *ginst, MonoImage *image)
2079 for (i = 0; i < ginst->type_argc; ++i) {
2080 if (type_in_image (ginst->type_argv [i], image))
2088 gclass_in_image (MonoGenericClass *gclass, MonoImage *image)
2090 return gclass->container_class->image == image ||
2091 ginst_in_image (gclass->context.class_inst, image);
2095 type_in_image (MonoType *type, MonoImage *image)
2098 switch (type->type) {
2099 case MONO_TYPE_GENERICINST:
2100 return gclass_in_image (type->data.generic_class, image);
2102 type = type->data.type;
2104 case MONO_TYPE_SZARRAY:
2105 type = &type->data.klass->byval_arg;
2107 case MONO_TYPE_ARRAY:
2108 type = &type->data.array->eklass->byval_arg;
2110 case MONO_TYPE_FNPTR:
2111 return signature_in_image (type->data.method, image);
2113 if (type->data.generic_param->owner) {
2114 g_assert (!type->data.generic_param->owner->is_method);
2116 * FIXME: The following check is here solely
2117 * for monodis, which uses the internal
2119 * mono_metadata_load_generic_params(). The
2120 * caller of that function needs to fill in
2121 * owner->klass or owner->method of the
2122 * returned struct, but monodis doesn't do
2123 * that. The image unloading depends on that,
2124 * however, so a crash results without this
2127 if (!type->data.generic_param->owner->owner.klass)
2129 return type->data.generic_param->owner->owner.klass->image == image;
2131 return type->data.generic_param->image == image;
2133 case MONO_TYPE_MVAR:
2134 if (type->data.generic_param->owner) {
2135 g_assert (type->data.generic_param->owner->is_method);
2136 if (!type->data.generic_param->owner->owner.method)
2137 /* RefEmit created generic param whose method is not finished */
2139 return type->data.generic_param->owner->owner.method->klass->image == image;
2141 return type->data.generic_param->image == image;
2144 /* At this point, we should've avoided all potential allocations in mono_class_from_mono_type () */
2145 return image == mono_class_from_mono_type (type)->image;
2152 } CleanForImageUserData;
2155 steal_gclass_in_image (gpointer key, gpointer value, gpointer data)
2157 MonoGenericClass *gclass = key;
2158 CleanForImageUserData *user_data = data;
2160 if (!gclass_in_image (gclass, user_data->image))
2163 user_data->list = g_slist_prepend (user_data->list, gclass);
2168 steal_ginst_in_image (gpointer key, gpointer value, gpointer data)
2170 MonoGenericInst *ginst = key;
2171 CleanForImageUserData *user_data = data;
2173 if (!ginst_in_image (ginst, user_data->image))
2176 user_data->list = g_slist_prepend (user_data->list, ginst);
2181 inflated_method_in_image (gpointer key, gpointer value, gpointer data)
2183 MonoImage *image = data;
2184 MonoMethodInflated *method = key;
2187 // https://bugzilla.novell.com/show_bug.cgi?id=458168
2188 return method->declaring->klass->image == image ||
2189 (method->context.class_inst && ginst_in_image (method->context.class_inst, image)) ||
2190 (method->context.method_inst && ginst_in_image (method->context.method_inst, image)) || (((MonoMethod*)method)->signature && signature_in_image (mono_method_signature ((MonoMethod*)method), image));
2194 inflated_signature_in_image (gpointer key, gpointer value, gpointer data)
2196 MonoImage *image = data;
2197 MonoInflatedMethodSignature *sig = key;
2199 return signature_in_image (sig->sig, image) ||
2200 (sig->context.class_inst && ginst_in_image (sig->context.class_inst, image)) ||
2201 (sig->context.method_inst && ginst_in_image (sig->context.method_inst, image));
2205 mono_metadata_clean_for_image (MonoImage *image)
2207 CleanForImageUserData ginst_data, gclass_data;
2210 /* The data structures could reference each other so we delete them in two phases */
2211 ginst_data.image = gclass_data.image = image;
2212 ginst_data.list = gclass_data.list = NULL;
2214 mono_loader_lock ();
2215 /* Collect the items to delete and remove them from the hash table */
2216 g_hash_table_foreach_steal (generic_inst_cache, steal_ginst_in_image, &ginst_data);
2217 g_hash_table_foreach_steal (generic_class_cache, steal_gclass_in_image, &gclass_data);
2218 if (generic_method_cache)
2219 g_hash_table_foreach_remove (generic_method_cache, inflated_method_in_image, image);
2220 if (generic_signature_cache)
2221 g_hash_table_foreach_remove (generic_signature_cache, inflated_signature_in_image, image);
2222 /* Delete the removed items */
2223 for (l = ginst_data.list; l; l = l->next)
2224 free_generic_inst (l->data);
2225 for (l = gclass_data.list; l; l = l->next)
2226 free_generic_class (l->data);
2227 g_slist_free (ginst_data.list);
2228 g_slist_free (gclass_data.list);
2229 mono_class_unregister_image_generic_subclasses (image);
2230 mono_loader_unlock ();
2234 free_inflated_method (MonoMethodInflated *imethod)
2237 MonoMethod *method = (MonoMethod*)imethod;
2239 if (method->signature)
2240 mono_metadata_free_inflated_signature (method->signature);
2242 if (!((method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))) {
2243 MonoMethodNormal* mn = (MonoMethodNormal*) method;
2244 MonoMethodHeader *header = mn->header;
2247 /* Allocated in inflate_generic_header () */
2248 for (i = 0; i < header->num_locals; ++i)
2249 mono_metadata_free_type (header->locals [i]);
2250 g_free (header->clauses);
2259 free_generic_inst (MonoGenericInst *ginst)
2263 for (i = 0; i < ginst->type_argc; ++i)
2264 mono_metadata_free_type (ginst->type_argv [i]);
2270 free_generic_class (MonoGenericClass *gclass)
2274 /* FIXME: The dynamic case */
2275 if (gclass->cached_class && !gclass->cached_class->image->dynamic && !mono_generic_class_is_generic_type_definition (gclass)) {
2276 MonoClass *class = gclass->cached_class;
2278 /* Allocated in mono_class_init () */
2279 g_free (class->methods);
2281 g_free (class->ext->properties);
2282 /* Allocated in mono_generic_class_get_class () */
2283 g_free (class->interfaces);
2285 } else if (gclass->is_dynamic) {
2286 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *)gclass;
2288 for (i = 0; i < dgclass->count_fields; ++i) {
2289 MonoClassField *field = dgclass->fields + i;
2290 mono_metadata_free_type (field->type);
2291 g_free ((char*)field->name);
2293 for (i = 0; i < dgclass->count_properties; ++i) {
2294 MonoProperty *property = dgclass->properties + i;
2295 g_free ((char*)property->name);
2297 for (i = 0; i < dgclass->count_events; ++i) {
2298 MonoEvent *event = dgclass->events + i;
2299 g_free ((char*)event->name);
2302 g_free (dgclass->methods);
2303 g_free (dgclass->ctors);
2304 g_free (dgclass->fields);
2305 g_free (dgclass->properties);
2306 g_free (dgclass->events);
2307 g_free (dgclass->field_objects);
2308 g_free (dgclass->field_generic_types);
2309 if (!mono_generic_class_is_generic_type_definition (gclass))
2310 g_free (gclass->cached_class);
2316 free_inflated_signature (MonoInflatedMethodSignature *sig)
2318 mono_metadata_free_inflated_signature (sig->sig);
2323 * LOCKING: assumes the loader lock is held.
2326 mono_method_inflated_lookup (MonoMethodInflated* method, gboolean cache)
2329 if (!generic_method_cache)
2330 generic_method_cache = g_hash_table_new_full (inflated_method_hash, inflated_method_equal, NULL, (GDestroyNotify)free_inflated_method);
2331 g_hash_table_insert (generic_method_cache, method, method);
2334 if (generic_method_cache)
2335 return g_hash_table_lookup (generic_method_cache, method);
2341 * mono_metadata_get_inflated_signature:
2343 * Given an inflated signature and a generic context, return a canonical copy of the
2344 * signature. The returned signature might be equal to SIG or it might be a cached copy.
2346 MonoMethodSignature *
2347 mono_metadata_get_inflated_signature (MonoMethodSignature *sig, MonoGenericContext *context)
2349 MonoInflatedMethodSignature helper;
2350 MonoInflatedMethodSignature *res;
2352 mono_loader_lock ();
2353 if (!generic_signature_cache)
2354 generic_signature_cache = g_hash_table_new_full (inflated_signature_hash, inflated_signature_equal, NULL, (GDestroyNotify)free_inflated_signature);
2357 helper.context.class_inst = context->class_inst;
2358 helper.context.method_inst = context->method_inst;
2359 res = g_hash_table_lookup (generic_signature_cache, &helper);
2361 res = g_new0 (MonoInflatedMethodSignature, 1);
2363 res->context.class_inst = context->class_inst;
2364 res->context.method_inst = context->method_inst;
2365 g_hash_table_insert (generic_signature_cache, res, res);
2368 mono_loader_unlock ();
2373 * mono_metadata_get_generic_inst:
2375 * Given a list of types, return a MonoGenericInst that represents that list.
2376 * The returned MonoGenericInst has its own copy of the list of types. The list
2377 * passed in the argument can be freed, modified or disposed of.
2381 mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv)
2383 MonoGenericInst *ginst;
2386 int size = sizeof (MonoGenericInst) + (type_argc - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *);
2388 for (i = 0; i < type_argc; ++i)
2389 if (mono_class_is_open_constructed_type (type_argv [i]))
2391 is_open = (i < type_argc);
2393 ginst = alloca (size);
2395 ginst->is_open = is_open;
2396 ginst->type_argc = type_argc;
2397 memcpy (ginst->type_argv, type_argv, type_argc * sizeof (MonoType *));
2399 mono_loader_lock ();
2401 ginst = g_hash_table_lookup (generic_inst_cache, ginst);
2403 ginst = g_malloc (size);
2404 ginst->id = ++next_generic_inst_id;
2405 ginst->is_open = is_open;
2406 ginst->type_argc = type_argc;
2408 for (i = 0; i < type_argc; ++i)
2409 ginst->type_argv [i] = mono_metadata_type_dup (NULL, type_argv [i]);
2411 g_hash_table_insert (generic_inst_cache, ginst, ginst);
2414 mono_loader_unlock ();
2419 mono_metadata_is_type_builder_generic_type_definition (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
2421 MonoGenericContainer *container = container_class->generic_container;
2423 if (!is_dynamic || container_class->wastypebuilder || container->type_argc != inst->type_argc)
2425 return inst == container->context.class_inst;
2429 * mono_metadata_lookup_generic_class:
2431 * Returns a MonoGenericClass with the given properties.
2435 mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
2437 MonoGenericClass *gclass;
2438 MonoGenericClass helper;
2439 gboolean is_tb_open = mono_metadata_is_type_builder_generic_type_definition (container_class, inst, is_dynamic);
2441 helper.container_class = container_class;
2442 helper.context.class_inst = inst;
2443 helper.context.method_inst = NULL;
2444 helper.is_dynamic = is_dynamic; /* We use this in a hash lookup, which does not attempt to downcast the pointer */
2445 helper.is_tb_open = is_tb_open;
2446 helper.cached_class = NULL;
2448 mono_loader_lock ();
2450 gclass = g_hash_table_lookup (generic_class_cache, &helper);
2452 /* A tripwire just to keep us honest */
2453 g_assert (!helper.cached_class);
2456 mono_loader_unlock ();
2461 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2462 gclass = &dgclass->generic_class;
2463 gclass->is_dynamic = 1;
2465 gclass = g_new0 (MonoGenericClass, 1);
2468 gclass->is_tb_open = is_tb_open;
2469 gclass->container_class = container_class;
2470 gclass->context.class_inst = inst;
2471 gclass->context.method_inst = NULL;
2472 if (inst == container_class->generic_container->context.class_inst && !is_tb_open)
2473 gclass->cached_class = container_class;
2475 g_hash_table_insert (generic_class_cache, gclass, gclass);
2477 mono_loader_unlock ();
2483 * mono_metadata_inflate_generic_inst:
2485 * Instantiate the generic instance @ginst with the context @context.
2489 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
2491 MonoType **type_argv;
2492 MonoGenericInst *nginst;
2495 if (!ginst->is_open)
2498 type_argv = g_new0 (MonoType*, ginst->type_argc);
2500 for (i = 0; i < ginst->type_argc; i++)
2501 type_argv [i] = mono_class_inflate_generic_type (ginst->type_argv [i], context);
2503 nginst = mono_metadata_get_generic_inst (ginst->type_argc, type_argv);
2505 for (i = 0; i < ginst->type_argc; i++)
2506 mono_metadata_free_type (type_argv [i]);
2513 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
2514 int count, const char *ptr, const char **rptr)
2516 MonoType **type_argv;
2517 MonoGenericInst *ginst;
2520 type_argv = g_new0 (MonoType*, count);
2522 for (i = 0; i < count; i++) {
2523 MonoType *t = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
2534 ginst = mono_metadata_get_generic_inst (count, type_argv);
2542 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
2543 const char *ptr, const char **rptr)
2545 MonoGenericInst *inst;
2550 gtype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
2554 gklass = mono_class_from_mono_type (gtype);
2555 if (!gklass->generic_container)
2558 count = mono_metadata_decode_value (ptr, &ptr);
2559 inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr);
2566 type->data.generic_class = mono_metadata_lookup_generic_class (gklass, inst, FALSE);
2572 * @gc: The generic container to normalize
2573 * @type: The kind of generic parameters the resulting generic-container should contain
2576 static MonoGenericContainer *
2577 select_container (MonoGenericContainer *gc, MonoTypeEnum type)
2579 gboolean is_var = (type == MONO_TYPE_VAR);
2583 g_assert (is_var || type == MONO_TYPE_MVAR);
2586 if (gc->is_method || gc->parent)
2588 * The current MonoGenericContainer is a generic method -> its `parent'
2589 * points to the containing class'es container.
2598 * mono_metadata_parse_generic_param:
2599 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
2600 * see mono_metadata_parse_type_full() for details.
2601 * Internal routine to parse a generic type parameter.
2602 * LOCKING: Acquires the loader lock
2604 static MonoGenericParam *
2605 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
2606 MonoTypeEnum type, const char *ptr, const char **rptr)
2608 int index = mono_metadata_decode_value (ptr, &ptr);
2612 generic_container = select_container (generic_container, type);
2613 if (!generic_container) {
2614 /* Create dummy MonoGenericParam */
2615 MonoGenericParam *param;
2617 param = mono_image_alloc0 (m, sizeof (MonoGenericParam));
2618 param->name = mono_image_alloc0 (m, 8);
2619 sprintf ((char*)param->name, "%d", index);
2626 g_assert (index < generic_container->type_argc);
2627 return &generic_container->type_params [index];
2631 * mono_metadata_get_shared_type:
2633 * Return a shared instance of TYPE, if available, NULL otherwise.
2634 * Shared MonoType instances help save memory. Their contents should not be modified
2635 * by the caller. They do not need to be freed as their lifetime is bound by either
2636 * the lifetime of the runtime (builtin types), or the lifetime of the MonoClass
2637 * instance they are embedded in. If they are freed, they should be freed using
2638 * mono_metadata_free_type () instead of g_free ().
2641 mono_metadata_get_shared_type (MonoType *type)
2645 /* No need to use locking since nobody is modifying the hash table */
2646 if ((cached = g_hash_table_lookup (type_cache, type)))
2649 switch (type->type){
2650 case MONO_TYPE_CLASS:
2651 case MONO_TYPE_VALUETYPE:
2652 if (type == &type->data.klass->byval_arg)
2654 if (type == &type->data.klass->this_arg)
2663 * do_mono_metadata_parse_type:
2664 * @type: MonoType to be filled in with the return value
2666 * @generic_context: generics_context
2667 * @ptr: pointer to the encoded type
2668 * @rptr: pointer where the end of the encoded type is saved
2670 * Internal routine used to "fill" the contents of @type from an
2671 * allocated pointer. This is done this way to avoid doing too
2672 * many mini-allocations (particularly for the MonoFieldType which
2673 * most of the time is just a MonoType, but sometimes might be augmented).
2675 * This routine is used by mono_metadata_parse_type and
2676 * mono_metadata_parse_field_type
2678 * This extracts a Type as specified in Partition II (22.2.12)
2680 * Returns: FALSE if the type could not be loaded
2683 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
2684 const char *ptr, const char **rptr)
2687 type->type = mono_metadata_decode_value (ptr, &ptr);
2689 switch (type->type){
2690 case MONO_TYPE_VOID:
2691 case MONO_TYPE_BOOLEAN:
2692 case MONO_TYPE_CHAR:
2705 case MONO_TYPE_STRING:
2706 case MONO_TYPE_OBJECT:
2707 case MONO_TYPE_TYPEDBYREF:
2709 case MONO_TYPE_VALUETYPE:
2710 case MONO_TYPE_CLASS: {
2712 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
2713 type->data.klass = mono_class_get (m, token);
2714 if (!type->data.klass)
2718 case MONO_TYPE_SZARRAY: {
2719 MonoType *etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
2722 type->data.klass = mono_class_from_mono_type (etype);
2726 type->data.type = mono_metadata_parse_type_full (m, container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
2727 if (!type->data.type)
2730 case MONO_TYPE_FNPTR:
2731 type->data.method = mono_metadata_parse_method_signature_full (m, container, 0, ptr, &ptr);
2733 case MONO_TYPE_ARRAY:
2734 type->data.array = mono_metadata_parse_array_full (m, container, ptr, &ptr);
2736 case MONO_TYPE_MVAR:
2738 type->data.generic_param = mono_metadata_parse_generic_param (m, container, type->type, ptr, &ptr);
2740 case MONO_TYPE_GENERICINST:
2741 ok = do_mono_metadata_parse_generic_class (type, m, container, ptr, &ptr);
2744 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
2753 * mono_metadata_free_type:
2754 * @type: type to free
2756 * Free the memory allocated for type @type which is allocated on the heap.
2759 mono_metadata_free_type (MonoType *type)
2761 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
2764 switch (type->type){
2765 case MONO_TYPE_OBJECT:
2766 case MONO_TYPE_STRING:
2767 if (!type->data.klass)
2770 case MONO_TYPE_CLASS:
2771 case MONO_TYPE_VALUETYPE:
2772 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
2776 mono_metadata_free_type (type->data.type);
2778 case MONO_TYPE_FNPTR:
2779 mono_metadata_free_method_signature (type->data.method);
2781 case MONO_TYPE_ARRAY:
2782 mono_metadata_free_array (type->data.array);
2791 hex_dump (const char *buffer, int base, int count)
2793 int show_header = 1;
2801 for (i = 0; i < count; i++){
2804 printf ("\n0x%08x: ", (unsigned char) base + i);
2806 printf ("%02x ", (unsigned char) (buffer [i]));
2813 * @mh: The Method header
2814 * @ptr: Points to the beginning of the Section Data (25.3)
2817 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
2819 unsigned char sect_data_flags;
2820 const unsigned char *sptr;
2822 guint32 sect_data_len;
2825 /* align on 32-bit boundary */
2826 sptr = ptr = dword_align (ptr);
2827 sect_data_flags = *ptr;
2830 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
2832 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
2835 sect_data_len = ptr [0];
2839 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
2840 hex_dump (sptr, 0, sect_data_len+8);
2841 g_print ("\nheader: ");
2842 hex_dump (sptr-4, 0, 4);
2846 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
2847 const unsigned char *p = dword_align (ptr);
2849 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
2850 /* we could just store a pointer if we don't need to byteswap */
2851 mh->clauses = mono_image_alloc0 (m, sizeof (MonoExceptionClause) * mh->num_clauses);
2852 for (i = 0; i < mh->num_clauses; ++i) {
2853 MonoExceptionClause *ec = &mh->clauses [i];
2856 ec->flags = read32 (p);
2857 ec->try_offset = read32 (p + 4);
2858 ec->try_len = read32 (p + 8);
2859 ec->handler_offset = read32 (p + 12);
2860 ec->handler_len = read32 (p + 16);
2861 tof_value = read32 (p + 20);
2864 ec->flags = read16 (p);
2865 ec->try_offset = read16 (p + 2);
2866 ec->try_len = *(p + 4);
2867 ec->handler_offset = read16 (p + 5);
2868 ec->handler_len = *(p + 7);
2869 tof_value = read32 (p + 8);
2872 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2873 ec->data.filter_offset = tof_value;
2874 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2875 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
2877 ec->data.catch_class = NULL;
2879 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
2883 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
2884 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
2891 * mono_metadata_parse_mh_full:
2892 * @m: metadata context
2893 * @generic_context: generics context
2894 * @ptr: pointer to the method header.
2896 * Decode the method header at @ptr, including pointer to the IL code,
2897 * info about local variables and optional exception tables.
2898 * This is a Mono runtime internal function.
2900 * LOCKING: Acquires the loader lock.
2902 * Returns: a MonoMethodHeader allocated from the image mempool.
2905 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, const char *ptr)
2907 MonoMethodHeader *mh;
2908 unsigned char flags = *(const unsigned char *) ptr;
2909 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2911 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2912 const unsigned char *code;
2915 g_return_val_if_fail (ptr != NULL, NULL);
2918 case METHOD_HEADER_TINY_FORMAT:
2919 mh = mono_image_alloc0 (m, sizeof (MonoMethodHeader));
2922 local_var_sig_tok = 0;
2923 mh->code_size = flags >> 2;
2924 mh->code = (unsigned char*)ptr;
2926 case METHOD_HEADER_TINY_FORMAT1:
2927 mh = mono_image_alloc0 (m, sizeof (MonoMethodHeader));
2930 local_var_sig_tok = 0;
2933 * The spec claims 3 bits, but the Beta2 is
2936 mh->code_size = flags >> 2;
2937 mh->code = (unsigned char*)ptr;
2939 case METHOD_HEADER_FAT_FORMAT:
2940 fat_flags = read16 (ptr);
2942 hsize = (fat_flags >> 12) & 0xf;
2943 max_stack = read16 (ptr);
2945 code_size = read32 (ptr);
2947 local_var_sig_tok = read32 (ptr);
2950 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2955 code = (unsigned char*)ptr;
2957 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2961 * There are more sections
2963 ptr = (char*)code + code_size;
2969 if (local_var_sig_tok) {
2970 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2971 const char *locals_ptr;
2972 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2973 int len=0, i, bsize;
2975 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2976 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2977 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2978 if (*locals_ptr != 0x07)
2979 g_warning ("wrong signature for locals blob");
2981 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2982 mh = mono_image_alloc0 (m, sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
2983 mh->num_locals = len;
2984 for (i = 0; i < len; ++i) {
2985 mh->locals [i] = mono_metadata_parse_type_full (
2986 m, container, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
2987 if (!mh->locals [i]) {
2992 mh = mono_image_alloc0 (m, sizeof (MonoMethodHeader));
2995 mh->code_size = code_size;
2996 mh->max_stack = max_stack;
2997 mh->init_locals = init_locals;
2998 if (fat_flags & METHOD_HEADER_MORE_SECTS)
2999 parse_section_data (m, mh, (const unsigned char*)ptr);
3004 * mono_metadata_parse_mh:
3005 * @generic_context: generics context
3006 * @ptr: pointer to the method header.
3008 * Decode the method header at @ptr, including pointer to the IL code,
3009 * info about local variables and optional exception tables.
3010 * This is a Mono runtime internal function.
3012 * Returns: a MonoMethodHeader.
3015 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
3017 MonoMethodHeader *res;
3019 mono_loader_lock ();
3021 res = mono_metadata_parse_mh_full (m, NULL, ptr);
3023 mono_loader_unlock ();
3029 * mono_metadata_free_mh:
3030 * @mh: a method header
3032 * Free the memory allocated for the method header.
3033 * This is a Mono runtime internal function.
3036 mono_metadata_free_mh (MonoMethodHeader *mh)
3038 /* Allocated from the mempool */
3042 * mono_method_header_get_code:
3043 * @header: a MonoMethodHeader pointer
3044 * @code_size: memory location for returning the code size
3045 * @max_stack: memory location for returning the max stack
3047 * Method header accessor to retreive info about the IL code properties:
3048 * a pointer to the IL code itself, the size of the code and the max number
3049 * of stack slots used by the code.
3051 * Returns: pointer to the IL code represented by the method header.
3053 const unsigned char*
3054 mono_method_header_get_code (MonoMethodHeader *header, guint32* code_size, guint32* max_stack)
3057 *code_size = header->code_size;
3059 *max_stack = header->max_stack;
3060 return header->code;
3064 * mono_method_header_get_locals:
3065 * @header: a MonoMethodHeader pointer
3066 * @num_locals: memory location for returning the number of local variables
3067 * @init_locals: memory location for returning the init_locals flag
3069 * Method header accessor to retreive info about the local variables:
3070 * an array of local types, the number of locals and whether the locals
3071 * are supposed to be initialized to 0 on method entry
3073 * Returns: pointer to an array of types of the local variables
3076 mono_method_header_get_locals (MonoMethodHeader *header, guint32* num_locals, gboolean *init_locals)
3079 *num_locals = header->num_locals;
3081 *init_locals = header->init_locals;
3082 return header->locals;
3086 * mono_method_header_get_num_clauses:
3087 * @header: a MonoMethodHeader pointer
3089 * Method header accessor to retreive the number of exception clauses.
3091 * Returns: the number of exception clauses present
3094 mono_method_header_get_num_clauses (MonoMethodHeader *header)
3096 return header->num_clauses;
3100 * mono_method_header_get_clauses:
3101 * @header: a MonoMethodHeader pointer
3102 * @method: MonoMethod the header belongs to
3103 * @iter: pointer to a iterator
3104 * @clause: pointer to a MonoExceptionClause structure which will be filled with the info
3106 * Get the info about the exception clauses in the method. Set *iter to NULL to
3107 * initiate the iteration, then call the method repeatedly until it returns FALSE.
3108 * At each iteration, the structure pointed to by clause if filled with the
3109 * exception clause information.
3111 * Returns: TRUE if clause was filled with info, FALSE if there are no more exception
3115 mono_method_header_get_clauses (MonoMethodHeader *header, MonoMethod *method, gpointer *iter, MonoExceptionClause *clause)
3117 MonoExceptionClause *sc;
3118 /* later we'll be able to use this interface to parse the clause info on demand,
3119 * without allocating anything.
3121 if (!iter || !header->num_clauses)
3124 *iter = sc = header->clauses;
3130 if (sc < header->clauses + header->num_clauses) {
3139 * mono_metadata_parse_field_type:
3140 * @m: metadata context to extract information from
3141 * @ptr: pointer to the field signature
3142 * @rptr: pointer updated to match the end of the decoded stream
3144 * Parses the field signature, and returns the type information for it.
3146 * Returns: The MonoType that was extracted from @ptr.
3149 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
3151 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
3155 * mono_metadata_parse_param:
3156 * @m: metadata context to extract information from
3157 * @ptr: pointer to the param signature
3158 * @rptr: pointer updated to match the end of the decoded stream
3160 * Parses the param signature, and returns the type information for it.
3162 * Returns: The MonoType that was extracted from @ptr.
3165 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
3167 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
3171 * mono_metadata_token_from_dor:
3172 * @dor_token: A TypeDefOrRef coded index
3174 * dor_token is a TypeDefOrRef coded index: it contains either
3175 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
3176 * bits contain an index into the table.
3178 * Returns: an expanded token
3181 mono_metadata_token_from_dor (guint32 dor_index)
3185 table = dor_index & 0x03;
3186 idx = dor_index >> 2;
3189 case 0: /* TypeDef */
3190 return MONO_TOKEN_TYPE_DEF | idx;
3191 case 1: /* TypeRef */
3192 return MONO_TOKEN_TYPE_REF | idx;
3193 case 2: /* TypeSpec */
3194 return MONO_TOKEN_TYPE_SPEC | idx;
3196 g_assert_not_reached ();
3203 * We use this to pass context information to the row locator
3206 int idx; /* The index that we are trying to locate */
3207 int col_idx; /* The index in the row where idx may be stored */
3208 MonoTableInfo *t; /* pointer to the table */
3213 * How the row locator works.
3218 * ___|___------> _______
3221 * A column in the rows of table A references an index in table B.
3222 * For example A may be the TYPEDEF table and B the METHODDEF table.
3224 * Given an index in table B we want to get the row in table A
3225 * where the column n references our index in B.
3227 * In the locator_t structure:
3229 * col_idx is the column number
3230 * index is the index in table B
3231 * result will be the index in table A
3234 * Table A Table B column (in table A)
3235 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
3236 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
3237 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
3238 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
3239 * METHODSEM PROPERTY ASSOCIATION (encoded index)
3241 * Note that we still don't support encoded indexes.
3245 typedef_locator (const void *a, const void *b)
3247 locator_t *loc = (locator_t *) a;
3248 const char *bb = (const char *) b;
3249 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
3250 guint32 col, col_next;
3252 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
3258 * Need to check that the next row is valid.
3260 if (typedef_index + 1 < loc->t->rows) {
3261 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
3262 if (loc->idx >= col_next)
3265 if (col == col_next)
3269 loc->result = typedef_index;
3275 table_locator (const void *a, const void *b)
3277 locator_t *loc = (locator_t *) a;
3278 const char *bb = (const char *) b;
3279 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
3282 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
3284 if (loc->idx == col) {
3285 loc->result = table_index;
3295 declsec_locator (const void *a, const void *b)
3297 locator_t *loc = (locator_t *) a;
3298 const char *bb = (const char *) b;
3299 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
3302 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
3304 if (loc->idx == col) {
3305 loc->result = table_index;
3317 * Return the 1-based row index in TABLE, which must be one of the *Ptr tables,
3318 * which contains IDX.
3321 search_ptr_table (MonoImage *image, int table, int idx)
3323 MonoTableInfo *ptrdef = &image->tables [table];
3326 /* Use a linear search to find our index in the table */
3327 for (i = 0; i < ptrdef->rows; i ++)
3328 /* All the Ptr tables have the same structure */
3329 if (mono_metadata_decode_row_col (ptrdef, i, 0) == idx)
3332 if (i < ptrdef->rows)
3339 * mono_metadata_typedef_from_field:
3340 * @meta: metadata context
3341 * @index: FieldDef token
3343 * Returns: the 1-based index into the TypeDef table of the type that
3344 * declared the field described by @index, or 0 if not found.
3347 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
3349 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
3355 loc.idx = mono_metadata_token_index (index);
3356 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
3359 if (meta->uncompressed_metadata)
3360 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
3362 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
3363 g_assert_not_reached ();
3365 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3366 return loc.result + 1;
3370 * mono_metadata_typedef_from_method:
3371 * @meta: metadata context
3372 * @index: MethodDef token
3374 * Returns: the 1-based index into the TypeDef table of the type that
3375 * declared the method described by @index. 0 if not found.
3378 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
3380 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
3386 loc.idx = mono_metadata_token_index (index);
3387 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
3390 if (meta->uncompressed_metadata)
3391 loc.idx = search_ptr_table (meta, MONO_TABLE_METHOD_POINTER, loc.idx);
3393 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
3394 g_assert_not_reached ();
3396 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3397 return loc.result + 1;
3401 * mono_metadata_interfaces_from_typedef_full:
3402 * @meta: metadata context
3403 * @index: typedef token
3405 * The array of interfaces that the @index typedef token implements is returned in
3406 * @interfaces. The number of elemnts in the array is returned in @count.
3408 * LOCKING: Assumes the loader lock is held.
3410 * Returns: TRUE on success, FALSE on failure.
3413 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, MonoClass ***interfaces, guint *count, MonoGenericContext *context)
3415 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
3418 guint32 cols [MONO_INTERFACEIMPL_SIZE];
3427 loc.idx = mono_metadata_token_index (index);
3428 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
3431 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3436 * We may end up in the middle of the rows...
3439 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
3445 while (pos < tdef->rows) {
3446 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
3447 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
3452 result = mono_image_alloc0 (meta, sizeof (MonoClass*) * (pos - start));
3455 while (pos < tdef->rows) {
3458 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
3459 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
3461 iface = mono_class_get_full (
3462 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
3465 result [pos - start] = iface;
3468 *count = pos - start;
3469 *interfaces = result;
3474 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
3476 MonoClass **interfaces;
3479 mono_loader_lock ();
3480 rv = mono_metadata_interfaces_from_typedef_full (meta, index, &interfaces, count, NULL);
3481 mono_loader_unlock ();
3489 * mono_metadata_nested_in_typedef:
3490 * @meta: metadata context
3491 * @index: typedef token
3493 * Returns: the 1-based index into the TypeDef table of the type
3494 * where the type described by @index is nested.
3495 * Retruns 0 if @index describes a non-nested type.
3498 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
3500 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
3506 loc.idx = mono_metadata_token_index (index);
3507 loc.col_idx = MONO_NESTED_CLASS_NESTED;
3510 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3513 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3514 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
3518 * mono_metadata_nesting_typedef:
3519 * @meta: metadata context
3520 * @index: typedef token
3522 * Returns: the 1-based index into the TypeDef table of the first type
3523 * that is nested inside the type described by @index. The search starts at
3524 * @start_index. returns 0 if no such type is found.
3527 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
3529 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
3531 guint32 class_index = mono_metadata_token_index (index);
3536 start = start_index;
3538 while (start <= tdef->rows) {
3539 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
3545 if (start > tdef->rows)
3552 * mono_metadata_packing_from_typedef:
3553 * @meta: metadata context
3554 * @index: token representing a type
3556 * Returns: the info stored in the ClassLAyout table for the given typedef token
3557 * into the @packing and @size pointers.
3558 * Returns 0 if the info is not found.
3561 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
3563 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
3565 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
3570 loc.idx = mono_metadata_token_index (index);
3571 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
3574 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3577 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
3579 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
3581 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
3583 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3584 return loc.result + 1;
3588 * mono_metadata_custom_attrs_from_index:
3589 * @meta: metadata context
3590 * @index: token representing the parent
3592 * Returns: the 1-based index into the CustomAttribute table of the first
3593 * attribute which belongs to the metadata object described by @index.
3594 * Returns 0 if no such attribute is found.
3597 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
3599 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
3606 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
3609 /* FIXME: Index translation */
3611 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3614 /* Find the first entry by searching backwards */
3615 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
3618 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3619 return loc.result + 1;
3623 * mono_metadata_declsec_from_index:
3624 * @meta: metadata context
3625 * @index: token representing the parent
3627 * Returns: the 0-based index into the DeclarativeSecurity table of the first
3628 * attribute which belongs to the metadata object described by @index.
3629 * Returns -1 if no such attribute is found.
3632 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
3634 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
3641 loc.col_idx = MONO_DECL_SECURITY_PARENT;
3644 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
3647 /* Find the first entry by searching backwards */
3648 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
3656 mono_backtrace (int limit)
3661 backtrace (array, limit);
3662 names = backtrace_symbols (array, limit);
3663 for (i =0; i < limit; ++i) {
3664 g_print ("\t%s\n", names [i]);
3671 /*#define __alignof__(a) sizeof(a)*/
3672 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
3677 * @t: the type to return the size of
3679 * Returns: the number of bytes required to hold an instance of this
3683 mono_type_size (MonoType *t, int *align)
3690 *align = __alignof__(gpointer);
3691 return sizeof (gpointer);
3695 case MONO_TYPE_VOID:
3698 case MONO_TYPE_BOOLEAN:
3699 *align = __alignof__(gint8);
3703 *align = __alignof__(gint8);
3705 case MONO_TYPE_CHAR:
3708 *align = __alignof__(gint16);
3712 *align = __alignof__(gint32);
3715 *align = __alignof__(float);
3719 *align = __alignof__(gint64);
3722 *align = __alignof__(double);
3726 *align = __alignof__(gpointer);
3727 return sizeof (gpointer);
3728 case MONO_TYPE_STRING:
3729 *align = __alignof__(gpointer);
3730 return sizeof (gpointer);
3731 case MONO_TYPE_OBJECT:
3732 *align = __alignof__(gpointer);
3733 return sizeof (gpointer);
3734 case MONO_TYPE_VALUETYPE: {
3735 if (t->data.klass->enumtype)
3736 return mono_type_size (mono_class_enum_basetype (t->data.klass), align);
3738 return mono_class_value_size (t->data.klass, (guint32*)align);
3740 case MONO_TYPE_CLASS:
3741 case MONO_TYPE_SZARRAY:
3743 case MONO_TYPE_FNPTR:
3744 case MONO_TYPE_ARRAY:
3745 *align = __alignof__(gpointer);
3746 return sizeof (gpointer);
3747 case MONO_TYPE_TYPEDBYREF:
3748 return mono_class_value_size (mono_defaults.typed_reference_class, (guint32*)align);
3749 case MONO_TYPE_GENERICINST: {
3750 MonoGenericClass *gclass = t->data.generic_class;
3751 MonoClass *container_class = gclass->container_class;
3753 // g_assert (!gclass->inst->is_open);
3755 if (container_class->valuetype) {
3756 if (container_class->enumtype)
3757 return mono_type_size (mono_class_enum_basetype (container_class), align);
3759 return mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
3761 *align = __alignof__(gpointer);
3762 return sizeof (gpointer);
3766 case MONO_TYPE_MVAR:
3767 /* FIXME: Martin, this is wrong. */
3768 *align = __alignof__(gpointer);
3769 return sizeof (gpointer);
3771 g_error ("mono_type_size: type 0x%02x unknown", t->type);
3777 * mono_type_stack_size:
3778 * @t: the type to return the size it uses on the stack
3780 * Returns: the number of bytes required to hold an instance of this
3781 * type on the runtime stack
3784 mono_type_stack_size (MonoType *t, int *align)
3786 return mono_type_stack_size_internal (t, align, FALSE);
3790 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open)
3793 #if SIZEOF_VOID_P == SIZEOF_REGISTER
3794 int stack_slot_size = sizeof (gpointer);
3795 int stack_slot_align = __alignof__ (gpointer);
3796 #elif SIZEOF_VOID_P < SIZEOF_REGISTER
3797 int stack_slot_size = SIZEOF_REGISTER;
3798 int stack_slot_align = SIZEOF_REGISTER;
3801 g_assert (t != NULL);
3807 *align = stack_slot_align;
3808 return stack_slot_size;
3812 case MONO_TYPE_BOOLEAN:
3813 case MONO_TYPE_CHAR:
3822 case MONO_TYPE_STRING:
3823 case MONO_TYPE_OBJECT:
3824 case MONO_TYPE_CLASS:
3825 case MONO_TYPE_SZARRAY:
3827 case MONO_TYPE_FNPTR:
3828 case MONO_TYPE_ARRAY:
3829 *align = stack_slot_align;
3830 return stack_slot_size;
3832 case MONO_TYPE_MVAR:
3833 g_assert (allow_open);
3834 *align = stack_slot_align;
3835 return stack_slot_size;
3836 case MONO_TYPE_TYPEDBYREF:
3837 *align = stack_slot_align;
3838 return stack_slot_size * 3;
3840 *align = __alignof__(float);
3841 return sizeof (float);
3844 *align = __alignof__(gint64);
3845 return sizeof (gint64);
3847 *align = __alignof__(double);
3848 return sizeof (double);
3849 case MONO_TYPE_VALUETYPE: {
3852 if (t->data.klass->enumtype)
3853 return mono_type_stack_size_internal (mono_class_enum_basetype (t->data.klass), align, allow_open);
3855 size = mono_class_value_size (t->data.klass, (guint32*)align);
3857 *align = *align + stack_slot_align - 1;
3858 *align &= ~(stack_slot_align - 1);
3860 size += stack_slot_size - 1;
3861 size &= ~(stack_slot_size - 1);
3866 case MONO_TYPE_GENERICINST: {
3867 MonoGenericClass *gclass = t->data.generic_class;
3868 MonoClass *container_class = gclass->container_class;
3871 g_assert (!gclass->context.class_inst->is_open);
3873 if (container_class->valuetype) {
3874 if (container_class->enumtype)
3875 return mono_type_stack_size_internal (mono_class_enum_basetype (container_class), align, allow_open);
3877 guint32 size = mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
3879 *align = *align + stack_slot_align - 1;
3880 *align &= ~(stack_slot_align - 1);
3882 size += stack_slot_size - 1;
3883 size &= ~(stack_slot_size - 1);
3888 *align = stack_slot_align;
3889 return stack_slot_size;
3893 g_error ("type 0x%02x unknown", t->type);
3899 mono_type_generic_inst_is_valuetype (MonoType *type)
3901 g_assert (type->type == MONO_TYPE_GENERICINST);
3902 return type->data.generic_class->container_class->valuetype;
3906 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
3908 return gclass->container_class->valuetype;
3912 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
3914 MonoGenericInst *i1 = g1->context.class_inst;
3915 MonoGenericInst *i2 = g2->context.class_inst;
3917 if (g1->is_dynamic != g2->is_dynamic)
3919 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
3921 if (!mono_generic_inst_equal_full (i1, i2, signature_only))
3923 return g1->is_tb_open == g2->is_tb_open;
3927 _mono_metadata_generic_class_container_equal (const MonoGenericClass *g1, MonoClass *c2, gboolean signature_only)
3929 MonoGenericInst *i1 = g1->context.class_inst;
3930 MonoGenericInst *i2 = c2->generic_container->context.class_inst;
3932 if (!mono_metadata_class_equal (g1->container_class, c2, signature_only))
3934 if (!mono_generic_inst_equal_full (i1, i2, signature_only))
3936 return !g1->is_tb_open;
3940 mono_metadata_generic_context_hash (const MonoGenericContext *context)
3942 /* FIXME: check if this seed is good enough */
3943 guint hash = 0xc01dfee7;
3944 if (context->class_inst)
3945 hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->class_inst);
3946 if (context->method_inst)
3947 hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->method_inst);
3952 mono_metadata_generic_context_equal (const MonoGenericContext *g1, const MonoGenericContext *g2)
3954 return g1->class_inst == g2->class_inst && g1->method_inst == g2->method_inst;
3958 * mono_metadata_type_hash:
3961 * Computes an hash value for @t1 to be used in GHashTable.
3964 mono_metadata_type_hash (MonoType *t1)
3966 guint hash = t1->type;
3968 hash |= t1->byref << 6; /* do not collide with t1->type values */
3970 case MONO_TYPE_VALUETYPE:
3971 case MONO_TYPE_CLASS:
3972 case MONO_TYPE_SZARRAY:
3973 /* check if the distribution is good enough */
3974 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
3976 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
3977 case MONO_TYPE_ARRAY:
3978 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
3979 case MONO_TYPE_GENERICINST:
3980 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
3986 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
3990 if (p1->num != p2->num)
3994 * We have to compare the image as well because if we didn't,
3995 * the generic_inst_cache lookup wouldn't care about the image
3996 * of generic params, so what could happen is that a generic
3997 * inst with params from image A is put into the cache, then
3998 * image B gets that generic inst from the cache, image A is
3999 * unloaded, so the inst is deleted, but image B still retains
4002 * The AOT runtime doesn't set the image when it's decoding
4003 * types, so we only compare it when the owner is NULL.
4005 if (p1->owner == p2->owner && (p1->owner || p1->image == p2->image))
4009 * If `signature_only' is true, we're comparing two (method) signatures.
4010 * In this case, the owner of two type parameters doesn't need to match.
4013 return signature_only;
4017 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
4021 if (c1->generic_class && c2->generic_class)
4022 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
4023 if (c1->generic_class && c2->generic_container)
4024 return _mono_metadata_generic_class_container_equal (c1->generic_class, c2, signature_only);
4025 if (c1->generic_container && c2->generic_class)
4026 return _mono_metadata_generic_class_container_equal (c2->generic_class, c1, signature_only);
4027 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
4028 return mono_metadata_generic_param_equal (
4029 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
4030 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
4031 return mono_metadata_generic_param_equal (
4032 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
4033 if (signature_only &&
4034 (c1->byval_arg.type == MONO_TYPE_SZARRAY) && (c2->byval_arg.type == MONO_TYPE_SZARRAY))
4035 return mono_metadata_class_equal (c1->byval_arg.data.klass, c2->byval_arg.data.klass, signature_only);
4040 mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only)
4042 gpointer iter1 = 0, iter2 = 0;
4046 if (s1->call_convention != s2->call_convention)
4048 if (s1->sentinelpos != s2->sentinelpos)
4050 if (s1->hasthis != s2->hasthis)
4052 if (s1->explicit_this != s2->explicit_this)
4054 if (! do_mono_metadata_type_equal (s1->ret, s2->ret, signature_only))
4056 if (s1->param_count != s2->param_count)
4060 MonoType *t1 = mono_signature_get_params (s1, &iter1);
4061 MonoType *t2 = mono_signature_get_params (s2, &iter2);
4063 if (t1 == NULL || t2 == NULL)
4065 if (! do_mono_metadata_type_equal (t1, t2, signature_only))
4071 * mono_metadata_type_equal:
4075 * Determine if @t1 and @t2 represent the same type.
4076 * Returns: #TRUE if @t1 and @t2 are equal.
4079 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
4081 if (t1->type != t2->type || t1->byref != t2->byref)
4085 case MONO_TYPE_VOID:
4086 case MONO_TYPE_BOOLEAN:
4087 case MONO_TYPE_CHAR:
4098 case MONO_TYPE_STRING:
4101 case MONO_TYPE_OBJECT:
4102 case MONO_TYPE_TYPEDBYREF:
4104 case MONO_TYPE_VALUETYPE:
4105 case MONO_TYPE_CLASS:
4106 case MONO_TYPE_SZARRAY:
4107 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
4109 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
4110 case MONO_TYPE_ARRAY:
4111 if (t1->data.array->rank != t2->data.array->rank)
4113 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
4114 case MONO_TYPE_GENERICINST:
4115 return _mono_metadata_generic_class_equal (
4116 t1->data.generic_class, t2->data.generic_class, signature_only);
4118 return mono_metadata_generic_param_equal (
4119 t1->data.generic_param, t2->data.generic_param, signature_only);
4120 case MONO_TYPE_MVAR:
4121 return mono_metadata_generic_param_equal (
4122 t1->data.generic_param, t2->data.generic_param, signature_only);
4123 case MONO_TYPE_FNPTR:
4124 return mono_metadata_fnptr_equal (t1->data.method, t2->data.method, signature_only);
4126 g_error ("implement type compare for %0x!", t1->type);
4134 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
4136 return do_mono_metadata_type_equal (t1, t2, FALSE);
4140 * mono_metadata_type_equal_full:
4143 * @signature_only: if signature only comparison should be made
4145 * Determine if @t1 and @t2 are signature compatible if @signature_only is #TRUE, otherwise
4146 * behaves the same way as mono_metadata_type_equal.
4147 * The function mono_metadata_type_equal(a, b) is just a shortcut for mono_metadata_type_equal_full(a, b, FALSE).
4148 * Returns: #TRUE if @t1 and @t2 are equal taking @signature_only into account.
4151 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only)
4153 return do_mono_metadata_type_equal (t1, t2, signature_only);
4157 * mono_metadata_signature_equal:
4158 * @sig1: a signature
4159 * @sig2: another signature
4161 * Determine if @sig1 and @sig2 represent the same signature, with the
4162 * same number of arguments and the same types.
4163 * Returns: #TRUE if @sig1 and @sig2 are equal.
4166 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
4170 if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
4173 if (sig1->generic_param_count != sig2->generic_param_count)
4177 * We're just comparing the signatures of two methods here:
4179 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
4180 * U and V are equal here.
4182 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
4185 for (i = 0; i < sig1->param_count; i++) {
4186 MonoType *p1 = sig1->params[i];
4187 MonoType *p2 = sig2->params[i];
4189 /* if (p1->attrs != p2->attrs)
4192 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
4196 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
4202 * mono_metadata_type_dup:
4203 * @image: image to alloc memory from
4204 * @original: type to duplicate
4206 * Returns: copy of type allocated from the image's mempool (or from the heap, if @image is null).
4209 mono_metadata_type_dup (MonoImage *image, const MonoType *o)
4212 int sizeof_o = sizeof (MonoType);
4214 sizeof_o += (o->num_mods - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod);
4216 r = image ? mono_image_alloc0 (image, sizeof_o) : g_malloc (sizeof_o);
4218 memcpy (r, o, sizeof_o);
4220 if (o->type == MONO_TYPE_PTR) {
4221 r->data.type = mono_metadata_type_dup (image, o->data.type);
4222 } else if (o->type == MONO_TYPE_ARRAY) {
4223 r->data.array = mono_dup_array_type (image, o->data.array);
4224 } else if (o->type == MONO_TYPE_FNPTR) {
4225 /*FIXME the dup'ed signature is leaked mono_metadata_free_type*/
4226 r->data.method = mono_metadata_signature_deep_dup (image, o->data.method);
4232 mono_signature_hash (MonoMethodSignature *sig)
4234 guint i, res = sig->ret->type;
4236 for (i = 0; i < sig->param_count; i++)
4237 res = (res << 5) - res + mono_type_hash (sig->params[i]);
4243 * mono_metadata_encode_value:
4244 * @value: value to encode
4245 * @buf: buffer where to write the compressed representation
4246 * @endbuf: pointer updated to point at the end of the encoded output
4248 * Encodes the value @value in the compressed representation used
4249 * in metadata and stores the result in @buf. @buf needs to be big
4250 * enough to hold the data (4 bytes).
4253 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
4259 else if (value < 0x4000) {
4260 p [0] = 0x80 | (value >> 8);
4261 p [1] = value & 0xff;
4264 p [0] = (value >> 24) | 0xc0;
4265 p [1] = (value >> 16) & 0xff;
4266 p [2] = (value >> 8) & 0xff;
4267 p [3] = value & 0xff;
4275 * mono_metadata_field_info:
4276 * @meta: the Image the field is defined in
4277 * @index: the index in the field table representing the field
4278 * @offset: a pointer to an integer where to store the offset that
4279 * may have been specified for the field in a FieldLayout table
4280 * @rva: a pointer to the RVA of the field data in the image that
4281 * may have been defined in a FieldRVA table
4282 * @marshal_spec: a pointer to the marshal spec that may have been
4283 * defined for the field in a FieldMarshal table.
4285 * Gather info for field @index that may have been defined in the FieldLayout,
4286 * FieldRVA and FieldMarshal tables.
4287 * Either of offset, rva and marshal_spec can be NULL if you're not interested
4291 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
4292 MonoMarshalSpec **marshal_spec)
4294 mono_metadata_field_info_full (meta, index, offset, rva, marshal_spec, FALSE);
4298 mono_metadata_field_info_with_mempool (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
4299 MonoMarshalSpec **marshal_spec)
4301 mono_metadata_field_info_full (meta, index, offset, rva, marshal_spec, TRUE);
4305 mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
4306 MonoMarshalSpec **marshal_spec, gboolean alloc_from_image)
4308 MonoTableInfo *tdef;
4311 loc.idx = index + 1;
4312 if (meta->uncompressed_metadata)
4313 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
4316 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
4318 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
4321 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
4322 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
4324 *offset = (guint32)-1;
4328 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
4330 loc.col_idx = MONO_FIELD_RVA_FIELD;
4333 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
4335 * LAMESPEC: There is no signature, no nothing, just the raw data.
4337 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
4345 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
4346 *marshal_spec = mono_metadata_parse_marshal_spec_full (alloc_from_image ? meta : NULL, p);
4353 * mono_metadata_get_constant_index:
4354 * @meta: the Image the field is defined in
4355 * @index: the token that may have a row defined in the constants table
4356 * @hint: possible position for the row
4358 * @token must be a FieldDef, ParamDef or PropertyDef token.
4360 * Returns: the index into the Constants table or 0 if not found.
4363 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
4365 MonoTableInfo *tdef;
4367 guint32 index = mono_metadata_token_index (token);
4369 tdef = &meta->tables [MONO_TABLE_CONSTANT];
4370 index <<= MONO_HASCONSTANT_BITS;
4371 switch (mono_metadata_token_table (token)) {
4372 case MONO_TABLE_FIELD:
4373 index |= MONO_HASCONSTANT_FIEDDEF;
4375 case MONO_TABLE_PARAM:
4376 index |= MONO_HASCONSTANT_PARAM;
4378 case MONO_TABLE_PROPERTY:
4379 index |= MONO_HASCONSTANT_PROPERTY;
4382 g_warning ("Not a valid token for the constant table: 0x%08x", token);
4386 loc.col_idx = MONO_CONSTANT_PARENT;
4389 /* FIXME: Index translation */
4391 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
4394 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
4395 return loc.result + 1;
4401 * mono_metadata_events_from_typedef:
4402 * @meta: metadata context
4403 * @index: 0-based index (in the TypeDef table) describing a type
4405 * Returns: the 0-based index in the Event table for the events in the
4406 * type. The last event that belongs to the type (plus 1) is stored
4407 * in the @end_idx pointer.
4410 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
4414 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
4422 loc.col_idx = MONO_EVENT_MAP_PARENT;
4423 loc.idx = index + 1;
4425 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4428 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
4429 if (loc.result + 1 < tdef->rows) {
4430 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
4432 end = meta->tables [MONO_TABLE_EVENT].rows;
4440 * mono_metadata_methods_from_event:
4441 * @meta: metadata context
4442 * @index: 0-based index (in the Event table) describing a event
4444 * Returns: the 0-based index in the MethodDef table for the methods in the
4445 * event. The last method that belongs to the event (plus 1) is stored
4446 * in the @end_idx pointer.
4449 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
4453 guint32 cols [MONO_METHOD_SEMA_SIZE];
4454 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
4460 if (meta->uncompressed_metadata)
4461 index = search_ptr_table (meta, MONO_TABLE_EVENT_POINTER, index + 1) - 1;
4464 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
4465 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
4467 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
4472 * We may end up in the middle of the rows...
4475 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
4481 while (end < msemt->rows) {
4482 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
4483 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
4492 * mono_metadata_properties_from_typedef:
4493 * @meta: metadata context
4494 * @index: 0-based index (in the TypeDef table) describing a type
4496 * Returns: the 0-based index in the Property table for the properties in the
4497 * type. The last property that belongs to the type (plus 1) is stored
4498 * in the @end_idx pointer.
4501 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
4505 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
4513 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
4514 loc.idx = index + 1;
4516 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4519 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
4520 if (loc.result + 1 < tdef->rows) {
4521 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
4523 end = meta->tables [MONO_TABLE_PROPERTY].rows;
4531 * mono_metadata_methods_from_property:
4532 * @meta: metadata context
4533 * @index: 0-based index (in the PropertyDef table) describing a property
4535 * Returns: the 0-based index in the MethodDef table for the methods in the
4536 * property. The last method that belongs to the property (plus 1) is stored
4537 * in the @end_idx pointer.
4540 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
4544 guint32 cols [MONO_METHOD_SEMA_SIZE];
4545 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
4551 if (meta->uncompressed_metadata)
4552 index = search_ptr_table (meta, MONO_TABLE_PROPERTY_POINTER, index + 1) - 1;
4555 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
4556 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
4558 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
4563 * We may end up in the middle of the rows...
4566 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
4572 while (end < msemt->rows) {
4573 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
4574 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
4583 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
4586 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
4591 /* No index translation seems to be needed */
4594 loc.col_idx = MONO_IMPLMAP_MEMBER;
4595 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
4597 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4600 return loc.result + 1;
4604 * @image: context where the image is created
4605 * @type_spec: typespec token
4607 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
4611 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
4613 guint32 idx = mono_metadata_token_index (type_spec);
4615 guint32 cols [MONO_TYPESPEC_SIZE];
4618 MonoType *type, *type2;
4620 mono_loader_lock ();
4622 type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
4624 mono_loader_unlock ();
4628 t = &image->tables [MONO_TABLE_TYPESPEC];
4630 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
4631 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
4632 len = mono_metadata_decode_value (ptr, &ptr);
4634 type = mono_image_alloc0 (image, sizeof (MonoType));
4636 if (*ptr == MONO_TYPE_BYREF) {
4641 if (!do_mono_metadata_parse_type (type, image, NULL, ptr, &ptr)) {
4642 mono_loader_unlock ();
4646 type2 = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
4649 mono_loader_unlock ();
4653 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
4655 mono_loader_unlock ();
4662 mono_image_strndup (MonoImage *image, const char *data, guint len)
4666 return g_strndup (data, len);
4667 res = mono_image_alloc (image, len + 1);
4668 memcpy (res, data, len);
4674 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
4676 return mono_metadata_parse_marshal_spec_full (NULL, ptr);
4680 mono_metadata_parse_marshal_spec_full (MonoImage *image, const char *ptr)
4682 MonoMarshalSpec *res;
4684 const char *start = ptr;
4686 /* fixme: this is incomplete, but I cant find more infos in the specs */
4689 res = mono_image_alloc0 (image, sizeof (MonoMarshalSpec));
4691 res = g_new0 (MonoMarshalSpec, 1);
4693 len = mono_metadata_decode_value (ptr, &ptr);
4694 res->native = *ptr++;
4696 if (res->native == MONO_NATIVE_LPARRAY) {
4697 res->data.array_data.param_num = -1;
4698 res->data.array_data.num_elem = -1;
4699 res->data.array_data.elem_mult = -1;
4701 if (ptr - start <= len)
4702 res->data.array_data.elem_type = *ptr++;
4703 if (ptr - start <= len)
4704 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
4705 if (ptr - start <= len)
4706 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4707 if (ptr - start <= len) {
4709 * LAMESPEC: Older spec versions say this parameter comes before
4710 * num_elem. Never spec versions don't talk about elem_mult at
4711 * all, but csc still emits it, and it is used to distinguish
4712 * between param_num being 0, and param_num being omitted.
4713 * So if (param_num == 0) && (num_elem > 0), then
4714 * elem_mult == 0 -> the array size is num_elem
4715 * elem_mult == 1 -> the array size is @param_num + num_elem
4717 res->data.array_data.elem_mult = mono_metadata_decode_value (ptr, &ptr);
4721 if (res->native == MONO_NATIVE_BYVALTSTR) {
4722 if (ptr - start <= len)
4723 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4726 if (res->native == MONO_NATIVE_BYVALARRAY) {
4727 if (ptr - start <= len)
4728 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4731 if (res->native == MONO_NATIVE_CUSTOM) {
4732 /* skip unused type guid */
4733 len = mono_metadata_decode_value (ptr, &ptr);
4735 /* skip unused native type name */
4736 len = mono_metadata_decode_value (ptr, &ptr);
4738 /* read custom marshaler type name */
4739 len = mono_metadata_decode_value (ptr, &ptr);
4740 res->data.custom_data.custom_name = mono_image_strndup (image, ptr, len);
4742 /* read cookie string */
4743 len = mono_metadata_decode_value (ptr, &ptr);
4744 res->data.custom_data.cookie = mono_image_strndup (image, ptr, len);
4747 if (res->native == MONO_NATIVE_SAFEARRAY) {
4748 res->data.safearray_data.elem_type = 0;
4749 res->data.safearray_data.num_elem = 0;
4750 if (ptr - start <= len)
4751 res->data.safearray_data.elem_type = *ptr++;
4752 if (ptr - start <= len)
4753 res->data.safearray_data.num_elem = *ptr++;
4759 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
4761 if (spec->native == MONO_NATIVE_CUSTOM) {
4762 g_free (spec->data.custom_data.custom_name);
4763 g_free (spec->data.custom_data.cookie);
4769 * mono_type_to_unmanaged:
4771 * Returns: A MonoMarshalNative enumeration value (MONO_NATIVE_) value
4772 * describing the underlying native reprensetation of the type.
4774 * In addition the value pointed by
4775 * "conv" will contain the kind of marshalling required for this
4776 * particular type one of the MONO_MARSHAL_CONV_ enumeration values.
4779 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
4780 gboolean unicode, MonoMarshalConv *conv)
4782 MonoMarshalConv dummy_conv;
4788 *conv = MONO_MARSHAL_CONV_NONE;
4791 return MONO_NATIVE_UINT;
4795 case MONO_TYPE_BOOLEAN:
4797 switch (mspec->native) {
4798 case MONO_NATIVE_VARIANTBOOL:
4799 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
4800 return MONO_NATIVE_VARIANTBOOL;
4801 case MONO_NATIVE_BOOLEAN:
4802 *conv = MONO_MARSHAL_CONV_BOOL_I4;
4803 return MONO_NATIVE_BOOLEAN;
4804 case MONO_NATIVE_I1:
4805 case MONO_NATIVE_U1:
4806 return mspec->native;
4808 g_error ("cant marshal bool to native type %02x", mspec->native);
4811 *conv = MONO_MARSHAL_CONV_BOOL_I4;
4812 return MONO_NATIVE_BOOLEAN;
4813 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
4814 case MONO_TYPE_I1: return MONO_NATIVE_I1;
4815 case MONO_TYPE_U1: return MONO_NATIVE_U1;
4816 case MONO_TYPE_I2: return MONO_NATIVE_I2;
4817 case MONO_TYPE_U2: return MONO_NATIVE_U2;
4818 case MONO_TYPE_I4: return MONO_NATIVE_I4;
4819 case MONO_TYPE_U4: return MONO_NATIVE_U4;
4820 case MONO_TYPE_I8: return MONO_NATIVE_I8;
4821 case MONO_TYPE_U8: return MONO_NATIVE_U8;
4822 case MONO_TYPE_R4: return MONO_NATIVE_R4;
4823 case MONO_TYPE_R8: return MONO_NATIVE_R8;
4824 case MONO_TYPE_STRING:
4826 switch (mspec->native) {
4827 case MONO_NATIVE_BSTR:
4828 *conv = MONO_MARSHAL_CONV_STR_BSTR;
4829 return MONO_NATIVE_BSTR;
4830 case MONO_NATIVE_LPSTR:
4831 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
4832 return MONO_NATIVE_LPSTR;
4833 case MONO_NATIVE_LPWSTR:
4834 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
4835 return MONO_NATIVE_LPWSTR;
4836 case MONO_NATIVE_LPTSTR:
4837 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
4838 return MONO_NATIVE_LPTSTR;
4839 case MONO_NATIVE_ANSIBSTR:
4840 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
4841 return MONO_NATIVE_ANSIBSTR;
4842 case MONO_NATIVE_TBSTR:
4843 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
4844 return MONO_NATIVE_TBSTR;
4845 case MONO_NATIVE_BYVALTSTR:
4847 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
4849 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
4850 return MONO_NATIVE_BYVALTSTR;
4852 g_error ("Can not marshal string to native type '%02x': Invalid managed/unmanaged type combination (String fields must be paired with LPStr, LPWStr, BStr or ByValTStr).", mspec->native);
4856 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
4857 return MONO_NATIVE_LPWSTR;
4860 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
4861 return MONO_NATIVE_LPSTR;
4863 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
4864 case MONO_TYPE_VALUETYPE: /*FIXME*/
4865 if (type->data.klass->enumtype) {
4866 t = mono_class_enum_basetype (type->data.klass)->type;
4869 if (type->data.klass == mono_defaults.handleref_class){
4870 *conv = MONO_MARSHAL_CONV_HANDLEREF;
4871 return MONO_NATIVE_INT;
4873 return MONO_NATIVE_STRUCT;
4874 case MONO_TYPE_SZARRAY:
4875 case MONO_TYPE_ARRAY:
4877 switch (mspec->native) {
4878 case MONO_NATIVE_BYVALARRAY:
4879 if ((type->data.klass->element_class == mono_defaults.char_class) && !unicode)
4880 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY;
4882 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
4883 return MONO_NATIVE_BYVALARRAY;
4884 case MONO_NATIVE_SAFEARRAY:
4885 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
4886 return MONO_NATIVE_SAFEARRAY;
4887 case MONO_NATIVE_LPARRAY:
4888 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
4889 return MONO_NATIVE_LPARRAY;
4891 g_error ("cant marshal array as native type %02x", mspec->native);
4895 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
4896 return MONO_NATIVE_LPARRAY;
4897 case MONO_TYPE_I: return MONO_NATIVE_INT;
4898 case MONO_TYPE_U: return MONO_NATIVE_UINT;
4899 case MONO_TYPE_CLASS:
4900 case MONO_TYPE_OBJECT: {
4901 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
4903 switch (mspec->native) {
4904 case MONO_NATIVE_STRUCT:
4905 return MONO_NATIVE_STRUCT;
4906 case MONO_NATIVE_CUSTOM:
4907 return MONO_NATIVE_CUSTOM;
4908 case MONO_NATIVE_INTERFACE:
4909 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
4910 return MONO_NATIVE_INTERFACE;
4911 case MONO_NATIVE_IDISPATCH:
4912 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
4913 return MONO_NATIVE_IDISPATCH;
4914 case MONO_NATIVE_IUNKNOWN:
4915 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
4916 return MONO_NATIVE_IUNKNOWN;
4917 case MONO_NATIVE_FUNC:
4918 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
4919 type->data.klass == mono_defaults.delegate_class ||
4920 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
4921 *conv = MONO_MARSHAL_CONV_DEL_FTN;
4922 return MONO_NATIVE_FUNC;
4926 g_error ("cant marshal object as native type %02x", mspec->native);
4929 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
4930 type->data.klass == mono_defaults.delegate_class ||
4931 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
4932 *conv = MONO_MARSHAL_CONV_DEL_FTN;
4933 return MONO_NATIVE_FUNC;
4935 if (mono_defaults.safehandle_class && type->data.klass == mono_defaults.safehandle_class){
4936 *conv = MONO_MARSHAL_CONV_SAFEHANDLE;
4937 return MONO_NATIVE_INT;
4939 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
4940 return MONO_NATIVE_STRUCT;
4942 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
4943 case MONO_TYPE_GENERICINST:
4944 type = &type->data.generic_class->container_class->byval_arg;
4947 case MONO_TYPE_TYPEDBYREF:
4949 g_error ("type 0x%02x not handled in marshal", t);
4951 return MONO_NATIVE_MAX;
4955 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
4958 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
4964 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
4965 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
4967 /* FIXME: Index translation */
4969 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4972 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
4976 method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context)
4978 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
4980 switch (tok & MONO_METHODDEFORREF_MASK) {
4981 case MONO_METHODDEFORREF_METHODDEF:
4982 return mono_get_method_full (m, MONO_TOKEN_METHOD_DEF | idx, NULL, context);
4983 case MONO_METHODDEFORREF_METHODREF:
4984 return mono_get_method_full (m, MONO_TOKEN_MEMBER_REF | idx, NULL, context);
4986 g_assert_not_reached ();
4991 * mono_class_get_overrides_full:
4993 * Return the method overrides belonging to class @type_token in @overrides, and
4994 * the number of overrides in @num_overrides.
4996 * Returns: TRUE on success, FALSE on failure.
4999 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
5000 MonoGenericContext *generic_context)
5003 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
5006 guint32 cols [MONO_METHODIMPL_SIZE];
5007 MonoMethod **result;
5018 loc.col_idx = MONO_METHODIMPL_CLASS;
5019 loc.idx = mono_metadata_token_index (type_token);
5021 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5027 * We may end up in the middle of the rows...
5030 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
5035 while (end < tdef->rows) {
5036 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
5042 result = g_new (MonoMethod*, num * 2);
5043 for (i = 0; i < num; ++i) {
5046 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
5047 method = method_from_method_def_or_ref (
5048 image, cols [MONO_METHODIMPL_DECLARATION], generic_context);
5051 result [i * 2] = method;
5052 method = method_from_method_def_or_ref (
5053 image, cols [MONO_METHODIMPL_BODY], generic_context);
5056 result [i * 2 + 1] = method;
5059 *overrides = result;
5061 *num_overrides = num;
5066 * mono_guid_to_string:
5068 * Converts a 16 byte Microsoft GUID to the standard string representation.
5071 mono_guid_to_string (const guint8 *guid)
5073 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
5074 guid[3], guid[2], guid[1], guid[0],
5078 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
5082 get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContainer *container)
5084 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
5085 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
5086 guint32 i, token, found;
5087 MonoClass *klass, **res;
5088 GList *cons = NULL, *tmp;
5089 MonoGenericContext *context = &container->context;
5091 *constraints = NULL;
5093 for (i = 0; i < tdef->rows; ++i) {
5094 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
5095 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
5096 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
5097 klass = mono_class_get_full (image, token, context);
5098 cons = g_list_append (cons, klass);
5101 /* contiguous list finished */
5108 res = g_new0 (MonoClass*, found + 1);
5109 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
5110 res [i] = tmp->data;
5118 * mono_metadata_get_generic_param_row:
5121 * @token: TypeOrMethodDef token, owner for GenericParam
5122 * @owner: coded token, set on return
5124 * Returns: 1-based row-id in the GenericParam table whose
5125 * owner is @token. 0 if not found.
5128 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner)
5130 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
5137 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
5138 *owner = MONO_TYPEORMETHOD_TYPE;
5139 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
5140 *owner = MONO_TYPEORMETHOD_METHOD;
5142 g_error ("wrong token %x to get_generic_param_row", token);
5145 *owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
5148 loc.col_idx = MONO_GENERICPARAM_OWNER;
5151 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5154 /* Find the first entry by searching backwards */
5155 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_GENERICPARAM_OWNER) == loc.idx))
5158 return loc.result + 1;
5162 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
5165 return mono_metadata_get_generic_param_row (image, token, &owner);
5169 * mono_metadata_load_generic_param_constraints:
5171 * Load the generic parameter constraints for the newly created generic type or method
5172 * represented by @token and @container. The @container is the new container which has
5173 * been returned by a call to mono_metadata_load_generic_params() with this @token.
5176 mono_metadata_load_generic_param_constraints (MonoImage *image, guint32 token,
5177 MonoGenericContainer *container)
5179 guint32 start_row, i, owner;
5180 if (! (start_row = mono_metadata_get_generic_param_row (image, token, &owner)))
5182 for (i = 0; i < container->type_argc; i++)
5183 get_constraints (image, start_row + i, &container->type_params [i].constraints, container);
5187 * mono_metadata_load_generic_params:
5189 * Load the type parameters from the type or method definition @token.
5191 * Use this method after parsing a type or method definition to figure out whether it's a generic
5192 * type / method. When parsing a method definition, @parent_container points to the generic container
5193 * of the current class, if any.
5195 * Note: This method does not load the constraints: for typedefs, this has to be done after fully
5196 * creating the type.
5198 * Returns: NULL if @token is not a generic type or method definition or the new generic container.
5200 * LOCKING: Acquires the loader lock
5203 MonoGenericContainer *
5204 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
5206 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
5207 guint32 cols [MONO_GENERICPARAM_SIZE];
5208 guint32 i, owner = 0, n;
5209 MonoGenericContainer *container;
5210 MonoGenericParam *params;
5211 MonoGenericContext *context;
5213 if (!(i = mono_metadata_get_generic_param_row (image, token, &owner)))
5215 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
5218 container = mono_image_alloc0 (image, sizeof (MonoGenericContainer));
5221 params = g_realloc (params, sizeof (MonoGenericParam) * n);
5222 memset (¶ms [n - 1], 0, sizeof (MonoGenericParam));
5223 params [n - 1].owner = container;
5224 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
5225 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
5226 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
5227 if (++i > tdef->rows)
5229 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
5230 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
5232 container->type_argc = n;
5233 container->type_params = mono_image_alloc0 (image, sizeof (MonoGenericParam) * n);
5234 memcpy (container->type_params, params, sizeof (MonoGenericParam) * n);
5236 container->parent = parent_container;
5238 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
5239 container->is_method = 1;
5241 g_assert (container->parent == NULL || container->is_method);
5243 context = &container->context;
5244 if (container->is_method) {
5245 context->class_inst = container->parent ? container->parent->context.class_inst : NULL;
5246 context->method_inst = mono_get_shared_generic_inst (container);
5248 context->class_inst = mono_get_shared_generic_inst (container);
5255 mono_get_shared_generic_inst (MonoGenericContainer *container)
5257 MonoType **type_argv;
5259 MonoGenericInst *nginst;
5262 type_argv = g_new0 (MonoType *, container->type_argc);
5263 helper = g_new0 (MonoType, container->type_argc);
5265 for (i = 0; i < container->type_argc; i++) {
5266 MonoType *t = &helper [i];
5268 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
5269 t->data.generic_param = &container->type_params [i];
5274 nginst = mono_metadata_get_generic_inst (container->type_argc, type_argv);
5283 mono_type_is_byref (MonoType *type)
5289 mono_type_get_type (MonoType *type)
5294 /* For MONO_TYPE_FNPTR */
5295 MonoMethodSignature*
5296 mono_type_get_signature (MonoType *type)
5298 return type->data.method;
5301 /* For MONO_TYPE_CLASS, VALUETYPE */
5303 mono_type_get_class (MonoType *type)
5305 return type->data.klass;
5308 /* For MONO_TYPE_ARRAY */
5310 mono_type_get_array_type (MonoType *type)
5312 return type->data.array;
5315 /* For MONO_TYPE_PTR */
5317 mono_type_get_ptr_type (MonoType *type)
5319 return type->data.type;
5323 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
5325 /* FIXME: implement */
5330 mono_signature_get_return_type (MonoMethodSignature *sig)
5336 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
5342 /* start from the first */
5343 if (sig->param_count) {
5344 *iter = &sig->params [0];
5345 return sig->params [0];
5353 if (type < &sig->params [sig->param_count]) {
5361 mono_signature_get_param_count (MonoMethodSignature *sig)
5363 return sig->param_count;
5367 mono_signature_get_call_conv (MonoMethodSignature *sig)
5369 return sig->call_convention;
5373 mono_signature_vararg_start (MonoMethodSignature *sig)
5375 return sig->sentinelpos;
5379 mono_signature_is_instance (MonoMethodSignature *sig)
5381 return sig->hasthis;
5385 mono_signature_explicit_this (MonoMethodSignature *sig)
5387 return sig->explicit_this;
5390 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
5392 mono_aligned_addr_hash (gconstpointer ptr)
5394 return GPOINTER_TO_UINT (ptr) >> 3;
5398 * If @field belongs to an inflated generic class, return the corresponding field of the
5399 * generic type definition class.
5402 mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field)
5407 if (!field->parent->generic_class)
5410 gtd = field->parent->generic_class->container_class;
5411 offset = field - field->parent->fields;
5412 return gtd->fields + offset;
5416 * If @event belongs to an inflated generic class, return the corresponding event of the
5417 * generic type definition class.
5420 mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event)
5425 if (!event->parent->generic_class)
5428 gtd = event->parent->generic_class->container_class;
5429 offset = event - event->parent->ext->events;
5430 return gtd->ext->events + offset;
5434 * If @property belongs to an inflated generic class, return the corresponding property of the
5435 * generic type definition class.
5438 mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property)
5443 if (!property->parent->generic_class)
5446 gtd = property->parent->generic_class->container_class;
5447 offset = property - property->parent->ext->properties;
5448 return gtd->ext->properties + offset;