3 * Routines for accessing the metadata
6 * Miguel de Icaza (miguel@ximian.com)
7 * Paolo Molaro (lupus@ximian.com)
9 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
20 #include "tabledefs.h"
21 #include "mono-endian.h"
23 #include "tokentype.h"
24 #include "class-internals.h"
25 #include "metadata-internals.h"
26 #include "verify-internals.h"
29 #include "debug-helpers.h"
30 #include "abi-details.h"
31 #include <mono/utils/mono-error-internals.h>
32 #include <mono/utils/bsearch.h>
33 #include <mono/utils/atomic.h>
34 #include <mono/utils/mono-counters.h>
36 static int img_set_cache_hit, img_set_cache_miss, img_set_count;
39 /* Auxiliary structure used for caching inflated signatures */
41 MonoMethodSignature *sig;
42 MonoGenericContext context;
43 } MonoInflatedMethodSignature;
45 static gboolean do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container, gboolean transient,
46 const char *ptr, const char **rptr, MonoError *error);
48 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
49 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
50 static gboolean mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only);
51 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
52 gboolean signature_only);
53 static void free_generic_inst (MonoGenericInst *ginst);
54 static void free_generic_class (MonoGenericClass *ginst);
55 static void free_inflated_method (MonoMethodInflated *method);
56 static void free_inflated_signature (MonoInflatedMethodSignature *sig);
57 static void mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva, MonoMarshalSpec **marshal_spec, gboolean alloc_from_image);
60 * This enumeration is used to describe the data types in the metadata
71 /* Index into Blob heap */
74 /* Index into String heap */
80 /* Pointer into a table */
83 /* HasConstant:Parent pointer (Param, Field or Property) */
86 /* HasCustomAttribute index. Indexes any table except CustomAttribute */
89 /* CustomAttributeType encoded index */
92 /* HasDeclSecurity index: TypeDef Method or Assembly */
95 /* Implementation coded index: File, Export AssemblyRef */
98 /* HasFieldMarshal coded index: Field or Param table */
101 /* MemberForwardedIndex: Field or Method */
104 /* TypeDefOrRef coded index: typedef, typeref, typespec */
107 /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
110 /* MethodDefOrRef coded index: Method or Member Ref table */
113 /* HasSemantic coded index: Event or Property */
116 /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
119 /* CustomDebugInformation parent encoded index */
120 MONO_MT_HASCUSTDEBUG_IDX
123 const static unsigned char TableSchemas [] = {
124 #define ASSEMBLY_SCHEMA_OFFSET 0
125 MONO_MT_UINT32, /* "HashId" }, */
126 MONO_MT_UINT16, /* "Major" }, */
127 MONO_MT_UINT16, /* "Minor" }, */
128 MONO_MT_UINT16, /* "BuildNumber" }, */
129 MONO_MT_UINT16, /* "RevisionNumber" }, */
130 MONO_MT_UINT32, /* "Flags" }, */
131 MONO_MT_BLOB_IDX, /* "PublicKey" }, */
132 MONO_MT_STRING_IDX, /* "Name" }, */
133 MONO_MT_STRING_IDX, /* "Culture" }, */
136 #define ASSEMBLYOS_SCHEMA_OFFSET ASSEMBLY_SCHEMA_OFFSET + 10
137 MONO_MT_UINT32, /* "OSPlatformID" }, */
138 MONO_MT_UINT32, /* "OSMajor" }, */
139 MONO_MT_UINT32, /* "OSMinor" }, */
142 #define ASSEMBLYPROC_SCHEMA_OFFSET ASSEMBLYOS_SCHEMA_OFFSET + 4
143 MONO_MT_UINT32, /* "Processor" }, */
146 #define ASSEMBLYREF_SCHEMA_OFFSET ASSEMBLYPROC_SCHEMA_OFFSET + 2
147 MONO_MT_UINT16, /* "Major" }, */
148 MONO_MT_UINT16, /* "Minor" }, */
149 MONO_MT_UINT16, /* "Build" }, */
150 MONO_MT_UINT16, /* "Revision" }, */
151 MONO_MT_UINT32, /* "Flags" }, */
152 MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
153 MONO_MT_STRING_IDX, /* "Name" }, */
154 MONO_MT_STRING_IDX, /* "Culture" }, */
155 MONO_MT_BLOB_IDX, /* "HashValue" }, */
158 #define ASSEMBLYREFOS_SCHEMA_OFFSET ASSEMBLYREF_SCHEMA_OFFSET + 10
159 MONO_MT_UINT32, /* "OSPlatformID" }, */
160 MONO_MT_UINT32, /* "OSMajorVersion" }, */
161 MONO_MT_UINT32, /* "OSMinorVersion" }, */
162 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
165 #define ASSEMBLYREFPROC_SCHEMA_OFFSET ASSEMBLYREFOS_SCHEMA_OFFSET + 5
166 MONO_MT_UINT32, /* "Processor" }, */
167 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
170 #define CLASS_LAYOUT_SCHEMA_OFFSET ASSEMBLYREFPROC_SCHEMA_OFFSET + 3
171 MONO_MT_UINT16, /* "PackingSize" }, */
172 MONO_MT_UINT32, /* "ClassSize" }, */
173 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
176 #define CONSTANT_SCHEMA_OFFSET CLASS_LAYOUT_SCHEMA_OFFSET + 4
177 MONO_MT_UINT8, /* "Type" }, */
178 MONO_MT_UINT8, /* "PaddingZero" }, */
179 MONO_MT_CONST_IDX, /* "Parent" }, */
180 MONO_MT_BLOB_IDX, /* "Value" }, */
183 #define CUSTOM_ATTR_SCHEMA_OFFSET CONSTANT_SCHEMA_OFFSET + 5
184 MONO_MT_HASCAT_IDX, /* "Parent" }, */
185 MONO_MT_CAT_IDX, /* "Type" }, */
186 MONO_MT_BLOB_IDX, /* "Value" }, */
189 #define DECL_SEC_SCHEMA_OFFSET CUSTOM_ATTR_SCHEMA_OFFSET + 4
190 MONO_MT_UINT16, /* "Action" }, */
191 MONO_MT_HASDEC_IDX, /* "Parent" }, */
192 MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
195 #define EVENTMAP_SCHEMA_OFFSET DECL_SEC_SCHEMA_OFFSET + 4
196 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
197 MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
200 #define EVENT_SCHEMA_OFFSET EVENTMAP_SCHEMA_OFFSET + 3
201 MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
202 MONO_MT_STRING_IDX, /* "Name" }, */
203 MONO_MT_TDOR_IDX, /* "EventType" }, TypeDef or TypeRef or TypeSpec */
206 #define EVENT_POINTER_SCHEMA_OFFSET EVENT_SCHEMA_OFFSET + 4
207 MONO_MT_TABLE_IDX, /* "Event" }, */
210 #define EXPORTED_TYPE_SCHEMA_OFFSET EVENT_POINTER_SCHEMA_OFFSET + 2
211 MONO_MT_UINT32, /* "Flags" }, */
212 MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
213 MONO_MT_STRING_IDX, /* "TypeName" }, */
214 MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
215 MONO_MT_IMPL_IDX, /* "Implementation" }, */
218 #define FIELD_SCHEMA_OFFSET EXPORTED_TYPE_SCHEMA_OFFSET + 6
219 MONO_MT_UINT16, /* "Flags" }, */
220 MONO_MT_STRING_IDX, /* "Name" }, */
221 MONO_MT_BLOB_IDX, /* "Signature" }, */
224 #define FIELD_LAYOUT_SCHEMA_OFFSET FIELD_SCHEMA_OFFSET + 4
225 MONO_MT_UINT32, /* "Offset" }, */
226 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
229 #define FIELD_MARSHAL_SCHEMA_OFFSET FIELD_LAYOUT_SCHEMA_OFFSET + 3
230 MONO_MT_HFM_IDX, /* "Parent" }, */
231 MONO_MT_BLOB_IDX, /* "NativeType" }, */
234 #define FIELD_RVA_SCHEMA_OFFSET FIELD_MARSHAL_SCHEMA_OFFSET + 3
235 MONO_MT_UINT32, /* "RVA" }, */
236 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
239 #define FIELD_POINTER_SCHEMA_OFFSET FIELD_RVA_SCHEMA_OFFSET + 3
240 MONO_MT_TABLE_IDX, /* "Field" }, */
243 #define FILE_SCHEMA_OFFSET FIELD_POINTER_SCHEMA_OFFSET + 2
244 MONO_MT_UINT32, /* "Flags" }, */
245 MONO_MT_STRING_IDX, /* "Name" }, */
246 MONO_MT_BLOB_IDX, /* "Value" }, */
249 #define IMPLMAP_SCHEMA_OFFSET FILE_SCHEMA_OFFSET + 4
250 MONO_MT_UINT16, /* "MappingFlag" }, */
251 MONO_MT_MF_IDX, /* "MemberForwarded" }, */
252 MONO_MT_STRING_IDX, /* "ImportName" }, */
253 MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
256 #define IFACEMAP_SCHEMA_OFFSET IMPLMAP_SCHEMA_OFFSET + 5
257 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
258 MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
261 #define MANIFEST_SCHEMA_OFFSET IFACEMAP_SCHEMA_OFFSET + 3
262 MONO_MT_UINT32, /* "Offset" }, */
263 MONO_MT_UINT32, /* "Flags" }, */
264 MONO_MT_STRING_IDX, /* "Name" }, */
265 MONO_MT_IMPL_IDX, /* "Implementation" }, */
268 #define MEMBERREF_SCHEMA_OFFSET MANIFEST_SCHEMA_OFFSET + 5
269 MONO_MT_MRP_IDX, /* "Class" }, */
270 MONO_MT_STRING_IDX, /* "Name" }, */
271 MONO_MT_BLOB_IDX, /* "Signature" }, */
274 #define METHOD_SCHEMA_OFFSET MEMBERREF_SCHEMA_OFFSET + 4
275 MONO_MT_UINT32, /* "RVA" }, */
276 MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
277 MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
278 MONO_MT_STRING_IDX, /* "Name" }, */
279 MONO_MT_BLOB_IDX, /* "Signature" }, */
280 MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
283 #define METHOD_IMPL_SCHEMA_OFFSET METHOD_SCHEMA_OFFSET + 7
284 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
285 MONO_MT_MDOR_IDX, /* "MethodBody" }, */
286 MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
289 #define METHOD_SEMA_SCHEMA_OFFSET METHOD_IMPL_SCHEMA_OFFSET + 4
290 MONO_MT_UINT16, /* "MethodSemantic" }, */
291 MONO_MT_TABLE_IDX, /* "Method:Method" }, */
292 MONO_MT_HS_IDX, /* "Association" }, */
295 #define METHOD_POINTER_SCHEMA_OFFSET METHOD_SEMA_SCHEMA_OFFSET + 4
296 MONO_MT_TABLE_IDX, /* "Method" }, */
299 #define MODULE_SCHEMA_OFFSET METHOD_POINTER_SCHEMA_OFFSET + 2
300 MONO_MT_UINT16, /* "Generation" }, */
301 MONO_MT_STRING_IDX, /* "Name" }, */
302 MONO_MT_GUID_IDX, /* "MVID" }, */
303 MONO_MT_GUID_IDX, /* "EncID" }, */
304 MONO_MT_GUID_IDX, /* "EncBaseID" }, */
307 #define MODULEREF_SCHEMA_OFFSET MODULE_SCHEMA_OFFSET + 6
308 MONO_MT_STRING_IDX, /* "Name" }, */
311 #define NESTED_CLASS_SCHEMA_OFFSET MODULEREF_SCHEMA_OFFSET + 2
312 MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
313 MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
316 #define PARAM_SCHEMA_OFFSET NESTED_CLASS_SCHEMA_OFFSET + 3
317 MONO_MT_UINT16, /* "Flags" }, */
318 MONO_MT_UINT16, /* "Sequence" }, */
319 MONO_MT_STRING_IDX, /* "Name" }, */
322 #define PARAM_POINTER_SCHEMA_OFFSET PARAM_SCHEMA_OFFSET + 4
323 MONO_MT_TABLE_IDX, /* "Param" }, */
326 #define PROPERTY_SCHEMA_OFFSET PARAM_POINTER_SCHEMA_OFFSET + 2
327 MONO_MT_UINT16, /* "Flags" }, */
328 MONO_MT_STRING_IDX, /* "Name" }, */
329 MONO_MT_BLOB_IDX, /* "Type" }, */
332 #define PROPERTY_POINTER_SCHEMA_OFFSET PROPERTY_SCHEMA_OFFSET + 4
333 MONO_MT_TABLE_IDX, /* "Property" }, */
336 #define PROPERTY_MAP_SCHEMA_OFFSET PROPERTY_POINTER_SCHEMA_OFFSET + 2
337 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
338 MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
341 #define STDALON_SIG_SCHEMA_OFFSET PROPERTY_MAP_SCHEMA_OFFSET + 3
342 MONO_MT_BLOB_IDX, /* "Signature" }, */
345 #define TYPEDEF_SCHEMA_OFFSET STDALON_SIG_SCHEMA_OFFSET + 2
346 MONO_MT_UINT32, /* "Flags" }, */
347 MONO_MT_STRING_IDX, /* "Name" }, */
348 MONO_MT_STRING_IDX, /* "Namespace" }, */
349 MONO_MT_TDOR_IDX, /* "Extends" }, */
350 MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
351 MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
354 #define TYPEREF_SCHEMA_OFFSET TYPEDEF_SCHEMA_OFFSET + 7
355 MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
356 MONO_MT_STRING_IDX, /* "Name" }, */
357 MONO_MT_STRING_IDX, /* "Namespace" }, */
360 #define TYPESPEC_SCHEMA_OFFSET TYPEREF_SCHEMA_OFFSET + 4
361 MONO_MT_BLOB_IDX, /* "Signature" }, */
364 #define GENPARAM_SCHEMA_OFFSET TYPESPEC_SCHEMA_OFFSET + 2
365 MONO_MT_UINT16, /* "Number" }, */
366 MONO_MT_UINT16, /* "Flags" }, */
367 MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
368 MONO_MT_STRING_IDX, /* "Name" }, */
371 #define METHOD_SPEC_SCHEMA_OFFSET GENPARAM_SCHEMA_OFFSET + 5
372 MONO_MT_MDOR_IDX, /* "Method" }, */
373 MONO_MT_BLOB_IDX, /* "Signature" }, */
376 #define GEN_CONSTRAINT_SCHEMA_OFFSET METHOD_SPEC_SCHEMA_OFFSET + 3
377 MONO_MT_TABLE_IDX, /* "GenericParam" }, */
378 MONO_MT_TDOR_IDX, /* "Constraint" }, */
381 #define DOCUMENT_SCHEMA_OFFSET GEN_CONSTRAINT_SCHEMA_OFFSET + 3
382 MONO_MT_BLOB_IDX, /* Name */
383 MONO_MT_GUID_IDX, /* HashAlgorithm */
384 MONO_MT_BLOB_IDX, /* Hash */
385 MONO_MT_GUID_IDX, /* Language */
388 #define METHODBODY_SCHEMA_OFFSET DOCUMENT_SCHEMA_OFFSET + 5
389 MONO_MT_TABLE_IDX, /* Document */
390 MONO_MT_BLOB_IDX, /* SequencePoints */
393 #define LOCALSCOPE_SCHEMA_OFFSET METHODBODY_SCHEMA_OFFSET + 3
394 MONO_MT_TABLE_IDX, /* Method */
395 MONO_MT_TABLE_IDX, /* ImportScope */
396 MONO_MT_TABLE_IDX, /* VariableList */
397 MONO_MT_TABLE_IDX, /* ConstantList */
398 MONO_MT_UINT32, /* StartOffset */
399 MONO_MT_UINT32, /* Length */
402 #define LOCALVARIABLE_SCHEMA_OFFSET LOCALSCOPE_SCHEMA_OFFSET + 7
403 MONO_MT_UINT16, /* Attributes */
404 MONO_MT_UINT16, /* Index */
405 MONO_MT_STRING_IDX, /* Name */
408 #define LOCALCONSTANT_SCHEMA_OFFSET LOCALVARIABLE_SCHEMA_OFFSET + 4
409 MONO_MT_STRING_IDX, /* Name (String heap index) */
410 MONO_MT_BLOB_IDX, /* Signature (Blob heap index, LocalConstantSig blob) */
413 #define IMPORTSCOPE_SCHEMA_OFFSET LOCALCONSTANT_SCHEMA_OFFSET + 3
414 MONO_MT_TABLE_IDX, /* Parent (ImportScope row id or nil) */
415 MONO_MT_BLOB_IDX, /* Imports (Blob index, encoding: Imports blob) */
418 #define ASYNCMETHOD_SCHEMA_OFFSET IMPORTSCOPE_SCHEMA_OFFSET + 3
419 MONO_MT_TABLE_IDX, /* MoveNextMethod (MethodDef row id) */
420 MONO_MT_TABLE_IDX, /* KickoffMethod (MethodDef row id) */
423 #define CUSTOMDEBUGINFORMATION_SCHEMA_OFFSET ASYNCMETHOD_SCHEMA_OFFSET + 3
424 MONO_MT_HASCUSTDEBUG_IDX, /* Parent (HasCustomDebugInformation coded index) */
425 MONO_MT_GUID_IDX, /* Kind (Guid heap index) */
426 MONO_MT_BLOB_IDX, /* Value (Blob heap index) */
429 #define NULL_SCHEMA_OFFSET CUSTOMDEBUGINFORMATION_SCHEMA_OFFSET + 4
433 /* Must be the same order as MONO_TABLE_* */
434 const static unsigned char
435 table_description [] = {
436 MODULE_SCHEMA_OFFSET,
437 TYPEREF_SCHEMA_OFFSET,
438 TYPEDEF_SCHEMA_OFFSET,
439 FIELD_POINTER_SCHEMA_OFFSET,
441 METHOD_POINTER_SCHEMA_OFFSET,
442 METHOD_SCHEMA_OFFSET,
443 PARAM_POINTER_SCHEMA_OFFSET,
445 IFACEMAP_SCHEMA_OFFSET,
446 MEMBERREF_SCHEMA_OFFSET, /* 0xa */
447 CONSTANT_SCHEMA_OFFSET,
448 CUSTOM_ATTR_SCHEMA_OFFSET,
449 FIELD_MARSHAL_SCHEMA_OFFSET,
450 DECL_SEC_SCHEMA_OFFSET,
451 CLASS_LAYOUT_SCHEMA_OFFSET,
452 FIELD_LAYOUT_SCHEMA_OFFSET, /* 0x10 */
453 STDALON_SIG_SCHEMA_OFFSET,
454 EVENTMAP_SCHEMA_OFFSET,
455 EVENT_POINTER_SCHEMA_OFFSET,
457 PROPERTY_MAP_SCHEMA_OFFSET,
458 PROPERTY_POINTER_SCHEMA_OFFSET,
459 PROPERTY_SCHEMA_OFFSET,
460 METHOD_SEMA_SCHEMA_OFFSET,
461 METHOD_IMPL_SCHEMA_OFFSET,
462 MODULEREF_SCHEMA_OFFSET, /* 0x1a */
463 TYPESPEC_SCHEMA_OFFSET,
464 IMPLMAP_SCHEMA_OFFSET,
465 FIELD_RVA_SCHEMA_OFFSET,
468 ASSEMBLY_SCHEMA_OFFSET, /* 0x20 */
469 ASSEMBLYPROC_SCHEMA_OFFSET,
470 ASSEMBLYOS_SCHEMA_OFFSET,
471 ASSEMBLYREF_SCHEMA_OFFSET,
472 ASSEMBLYREFPROC_SCHEMA_OFFSET,
473 ASSEMBLYREFOS_SCHEMA_OFFSET,
475 EXPORTED_TYPE_SCHEMA_OFFSET,
476 MANIFEST_SCHEMA_OFFSET,
477 NESTED_CLASS_SCHEMA_OFFSET,
478 GENPARAM_SCHEMA_OFFSET, /* 0x2a */
479 METHOD_SPEC_SCHEMA_OFFSET,
480 GEN_CONSTRAINT_SCHEMA_OFFSET,
484 DOCUMENT_SCHEMA_OFFSET, /* 0x30 */
485 METHODBODY_SCHEMA_OFFSET,
486 LOCALSCOPE_SCHEMA_OFFSET,
487 LOCALVARIABLE_SCHEMA_OFFSET,
488 LOCALCONSTANT_SCHEMA_OFFSET,
489 IMPORTSCOPE_SCHEMA_OFFSET,
490 ASYNCMETHOD_SCHEMA_OFFSET,
491 CUSTOMDEBUGINFORMATION_SCHEMA_OFFSET
494 #ifdef HAVE_ARRAY_ELEM_INIT
495 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
496 #define MSGSTRFIELD1(line) str##line
497 static const struct msgstr_t {
498 #define TABLEDEF(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
499 #include "mono/cil/tables.def"
502 #define TABLEDEF(a,b) b,
503 #include "mono/cil/tables.def"
506 static const gint16 tableidx [] = {
507 #define TABLEDEF(a,b) [a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
508 #include "mono/cil/tables.def"
513 #define TABLEDEF(a,b) b,
514 static const char* const
515 mono_tables_names [] = {
516 #include "mono/cil/tables.def"
522 /* If TRUE (but also see DISABLE_STICT_STRONG_NAMES #define), Mono will check
523 * that the public key token, culture and version of a candidate assembly matches
524 * the requested strong name. If FALSE, as long as the name matches, the candidate
527 static gboolean check_strong_names_strictly = FALSE;
529 // Amount initially reserved in each imageset's mempool.
530 // FIXME: This number is arbitrary, a more practical number should be found
531 #define INITIAL_IMAGE_SET_SIZE 1024
534 * mono_meta_table_name:
535 * \param table table index
537 * Returns the name of the given ECMA metadata logical format table
538 * as described in ECMA 335, Partition II, Section 22.
540 * \returns the name for the \p table index
543 mono_meta_table_name (int table)
545 if ((table < 0) || (table > MONO_TABLE_LAST))
548 #ifdef HAVE_ARRAY_ELEM_INIT
549 return (const char*)&tablestr + tableidx [table];
551 return mono_tables_names [table];
555 /* The guy who wrote the spec for this should not be allowed near a
558 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
559 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
560 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
561 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
562 inverse of this mapping.
565 #define rtsize(meta,s,b) (((s) < (1 << (b)) ? 2 : 4))
568 idx_size (MonoImage *meta, int tableidx)
570 if (meta->referenced_tables && (meta->referenced_tables & ((guint64)1 << tableidx)))
571 return meta->referenced_table_rows [tableidx] < 65536 ? 2 : 4;
573 return meta->tables [tableidx].rows < 65536 ? 2 : 4;
577 get_nrows (MonoImage *meta, int tableidx)
579 if (meta->referenced_tables && (meta->referenced_tables & ((guint64)1 << tableidx)))
580 return meta->referenced_table_rows [tableidx];
582 return meta->tables [tableidx].rows;
585 /* Reference: Partition II - 23.2.6 */
587 * mono_metadata_compute_size:
588 * \param meta metadata context
589 * \param tableindex metadata table number
590 * \param result_bitfield pointer to \c guint32 where to store additional info
592 * \c mono_metadata_compute_size computes the length in bytes of a single
593 * row in a metadata table. The size of each column is encoded in the
594 * \p result_bitfield return value along with the number of columns in the table.
595 * the resulting bitfield should be handed to the \c mono_metadata_table_size
596 * and \c mono_metadata_table_count macros.
597 * This is a Mono runtime internal only function.
600 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
602 guint32 bitfield = 0;
603 int size = 0, field_size = 0;
606 const unsigned char *description = TableSchemas + table_description [tableindex];
608 for (i = 0; (code = description [i]) != MONO_MT_END; i++){
611 field_size = 4; break;
614 field_size = 2; break;
617 field_size = 1; break;
619 case MONO_MT_BLOB_IDX:
620 field_size = meta->idx_blob_wide ? 4 : 2; break;
622 case MONO_MT_STRING_IDX:
623 field_size = meta->idx_string_wide ? 4 : 2; break;
625 case MONO_MT_GUID_IDX:
626 field_size = meta->idx_guid_wide ? 4 : 2; break;
628 case MONO_MT_TABLE_IDX:
629 /* Uhm, a table index can point to other tables besides the current one
630 * so, it's not correct to use the rowcount of the current table to
631 * get the size for this column - lupus
633 switch (tableindex) {
634 case MONO_TABLE_ASSEMBLYREFOS:
636 field_size = idx_size (meta, MONO_TABLE_ASSEMBLYREF); break;
637 case MONO_TABLE_ASSEMBLYREFPROCESSOR:
639 field_size = idx_size (meta, MONO_TABLE_ASSEMBLYREF); break;
640 case MONO_TABLE_CLASSLAYOUT:
642 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
643 case MONO_TABLE_EVENTMAP:
644 g_assert (i == 0 || i == 1);
645 field_size = i ? idx_size (meta, MONO_TABLE_EVENT):
646 idx_size (meta, MONO_TABLE_TYPEDEF);
648 case MONO_TABLE_EVENT_POINTER:
650 field_size = idx_size (meta, MONO_TABLE_EVENT); break;
651 case MONO_TABLE_EXPORTEDTYPE:
653 /* the index is in another metadata file, so it must be 4 */
654 field_size = 4; break;
655 case MONO_TABLE_FIELDLAYOUT:
657 field_size = idx_size (meta, MONO_TABLE_FIELD); break;
658 case MONO_TABLE_FIELDRVA:
660 field_size = idx_size (meta, MONO_TABLE_FIELD); break;
661 case MONO_TABLE_FIELD_POINTER:
663 field_size = idx_size (meta, MONO_TABLE_FIELD); break;
664 case MONO_TABLE_IMPLMAP:
666 field_size = idx_size (meta, MONO_TABLE_MODULEREF); break;
667 case MONO_TABLE_INTERFACEIMPL:
669 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
670 case MONO_TABLE_METHOD:
672 field_size = idx_size (meta, MONO_TABLE_PARAM); break;
673 case MONO_TABLE_METHODIMPL:
675 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
676 case MONO_TABLE_METHODSEMANTICS:
678 field_size = idx_size (meta, MONO_TABLE_METHOD); break;
679 case MONO_TABLE_METHOD_POINTER:
681 field_size = idx_size (meta, MONO_TABLE_METHOD); break;
682 case MONO_TABLE_NESTEDCLASS:
683 g_assert (i == 0 || i == 1);
684 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
685 case MONO_TABLE_PARAM_POINTER:
687 field_size = idx_size (meta, MONO_TABLE_PARAM); break;
688 case MONO_TABLE_PROPERTYMAP:
689 g_assert (i == 0 || i == 1);
690 field_size = i ? idx_size (meta, MONO_TABLE_PROPERTY):
691 idx_size (meta, MONO_TABLE_TYPEDEF);
693 case MONO_TABLE_PROPERTY_POINTER:
695 field_size = idx_size (meta, MONO_TABLE_PROPERTY); break;
696 case MONO_TABLE_TYPEDEF:
697 g_assert (i == 4 || i == 5);
698 field_size = i == 4 ? idx_size (meta, MONO_TABLE_FIELD):
699 idx_size (meta, MONO_TABLE_METHOD);
701 case MONO_TABLE_GENERICPARAM:
703 n = MAX (get_nrows (meta, MONO_TABLE_METHOD), get_nrows (meta, MONO_TABLE_TYPEDEF));
704 /*This is a coded token for 2 tables, so takes 1 bit */
705 field_size = rtsize (meta, n, 16 - MONO_TYPEORMETHOD_BITS);
707 case MONO_TABLE_GENERICPARAMCONSTRAINT:
709 field_size = idx_size (meta, MONO_TABLE_GENERICPARAM);
711 case MONO_TABLE_LOCALSCOPE:
714 // FIXME: This table is in another file
715 field_size = idx_size (meta, MONO_TABLE_METHOD);
718 field_size = idx_size (meta, MONO_TABLE_IMPORTSCOPE);
721 field_size = idx_size (meta, MONO_TABLE_LOCALVARIABLE);
724 field_size = idx_size (meta, MONO_TABLE_LOCALCONSTANT);
727 g_assert_not_reached ();
731 case MONO_TABLE_METHODBODY:
733 field_size = idx_size (meta, MONO_TABLE_DOCUMENT); break;
734 case MONO_TABLE_IMPORTSCOPE:
736 field_size = idx_size (meta, MONO_TABLE_IMPORTSCOPE); break;
737 case MONO_TABLE_STATEMACHINEMETHOD:
738 g_assert(i == 0 || i == 1);
739 field_size = idx_size(meta, MONO_TABLE_METHOD); break;
741 g_error ("Can't handle MONO_MT_TABLE_IDX for table %d element %d", tableindex, i);
746 * HasConstant: ParamDef, FieldDef, Property
748 case MONO_MT_CONST_IDX:
749 n = MAX (get_nrows (meta, MONO_TABLE_PARAM),
750 get_nrows (meta, MONO_TABLE_FIELD));
751 n = MAX (n, get_nrows (meta, MONO_TABLE_PROPERTY));
753 /* 2 bits to encode tag */
754 field_size = rtsize (meta, n, 16-2);
758 * HasCustomAttribute: points to any table but
761 case MONO_MT_HASCAT_IDX:
763 * We believe that since the signature and
764 * permission are indexing the Blob heap,
765 * we should consider the blob size first
767 /* I'm not a believer - lupus
768 if (meta->idx_blob_wide){
773 n = MAX (get_nrows (meta, MONO_TABLE_METHOD),
774 get_nrows (meta, MONO_TABLE_FIELD));
775 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPEREF));
776 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPEDEF));
777 n = MAX (n, get_nrows (meta, MONO_TABLE_PARAM));
778 n = MAX (n, get_nrows (meta, MONO_TABLE_INTERFACEIMPL));
779 n = MAX (n, get_nrows (meta, MONO_TABLE_MEMBERREF));
780 n = MAX (n, get_nrows (meta, MONO_TABLE_MODULE));
781 n = MAX (n, get_nrows (meta, MONO_TABLE_DECLSECURITY));
782 n = MAX (n, get_nrows (meta, MONO_TABLE_PROPERTY));
783 n = MAX (n, get_nrows (meta, MONO_TABLE_EVENT));
784 n = MAX (n, get_nrows (meta, MONO_TABLE_STANDALONESIG));
785 n = MAX (n, get_nrows (meta, MONO_TABLE_MODULEREF));
786 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPESPEC));
787 n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLY));
788 n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
789 n = MAX (n, get_nrows (meta, MONO_TABLE_FILE));
790 n = MAX (n, get_nrows (meta, MONO_TABLE_EXPORTEDTYPE));
791 n = MAX (n, get_nrows (meta, MONO_TABLE_MANIFESTRESOURCE));
792 n = MAX (n, get_nrows (meta, MONO_TABLE_GENERICPARAM));
793 n = MAX (n, get_nrows (meta, MONO_TABLE_GENERICPARAMCONSTRAINT));
794 n = MAX (n, get_nrows (meta, MONO_TABLE_METHODSPEC));
796 /* 5 bits to encode */
797 field_size = rtsize (meta, n, 16-5);
801 * HasCustomAttribute: points to any table but
805 case MONO_MT_HASCUSTDEBUG_IDX:
806 n = MAX(get_nrows (meta, MONO_TABLE_METHOD),
807 get_nrows (meta, MONO_TABLE_FIELD));
808 n = MAX(n, get_nrows (meta, MONO_TABLE_TYPEREF));
809 n = MAX(n, get_nrows (meta, MONO_TABLE_TYPEDEF));
810 n = MAX(n, get_nrows (meta, MONO_TABLE_PARAM));
811 n = MAX(n, get_nrows (meta, MONO_TABLE_INTERFACEIMPL));
812 n = MAX(n, get_nrows (meta, MONO_TABLE_MEMBERREF));
813 n = MAX(n, get_nrows (meta, MONO_TABLE_MODULE));
814 n = MAX(n, get_nrows (meta, MONO_TABLE_DECLSECURITY));
815 n = MAX(n, get_nrows (meta, MONO_TABLE_PROPERTY));
816 n = MAX(n, get_nrows (meta, MONO_TABLE_EVENT));
817 n = MAX(n, get_nrows (meta, MONO_TABLE_STANDALONESIG));
818 n = MAX(n, get_nrows (meta, MONO_TABLE_MODULEREF));
819 n = MAX(n, get_nrows (meta, MONO_TABLE_TYPESPEC));
820 n = MAX(n, get_nrows (meta, MONO_TABLE_ASSEMBLY));
821 n = MAX(n, get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
822 n = MAX(n, get_nrows (meta, MONO_TABLE_FILE));
823 n = MAX(n, get_nrows (meta, MONO_TABLE_EXPORTEDTYPE));
824 n = MAX(n, get_nrows (meta, MONO_TABLE_MANIFESTRESOURCE));
825 n = MAX(n, get_nrows (meta, MONO_TABLE_GENERICPARAM));
826 n = MAX(n, get_nrows (meta, MONO_TABLE_GENERICPARAMCONSTRAINT));
827 n = MAX(n, get_nrows (meta, MONO_TABLE_METHODSPEC));
828 n = MAX(n, get_nrows (meta, MONO_TABLE_DOCUMENT));
829 n = MAX(n, get_nrows (meta, MONO_TABLE_LOCALSCOPE));
830 n = MAX(n, get_nrows (meta, MONO_TABLE_LOCALVARIABLE));
831 n = MAX(n, get_nrows (meta, MONO_TABLE_LOCALCONSTANT));
832 n = MAX(n, get_nrows (meta, MONO_TABLE_IMPORTSCOPE));
834 /* 5 bits to encode */
835 field_size = rtsize(meta, n, 16 - 5);
839 * CustomAttributeType: MethodDef, MemberRef.
841 case MONO_MT_CAT_IDX:
842 n = MAX (get_nrows (meta, MONO_TABLE_METHOD),
843 get_nrows (meta, MONO_TABLE_MEMBERREF));
845 /* 3 bits to encode */
846 field_size = rtsize (meta, n, 16-3);
850 * HasDeclSecurity: Typedef, MethodDef, Assembly
852 case MONO_MT_HASDEC_IDX:
853 n = MAX (get_nrows (meta, MONO_TABLE_TYPEDEF),
854 get_nrows (meta, MONO_TABLE_METHOD));
855 n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLY));
857 /* 2 bits to encode */
858 field_size = rtsize (meta, n, 16-2);
862 * Implementation: File, AssemblyRef, ExportedType
864 case MONO_MT_IMPL_IDX:
865 n = MAX (get_nrows (meta, MONO_TABLE_FILE),
866 get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
867 n = MAX (n, get_nrows (meta, MONO_TABLE_EXPORTEDTYPE));
869 /* 2 bits to encode tag */
870 field_size = rtsize (meta, n, 16-2);
874 * HasFieldMarshall: FieldDef, ParamDef
876 case MONO_MT_HFM_IDX:
877 n = MAX (get_nrows (meta, MONO_TABLE_FIELD),
878 get_nrows (meta, MONO_TABLE_PARAM));
880 /* 1 bit used to encode tag */
881 field_size = rtsize (meta, n, 16-1);
885 * MemberForwarded: FieldDef, MethodDef
888 n = MAX (get_nrows (meta, MONO_TABLE_FIELD),
889 get_nrows (meta, MONO_TABLE_METHOD));
891 /* 1 bit used to encode tag */
892 field_size = rtsize (meta, n, 16-1);
896 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
898 * It is TypeDef, _TypeRef_, TypeSpec, instead.
900 case MONO_MT_TDOR_IDX:
901 n = MAX (get_nrows (meta, MONO_TABLE_TYPEDEF),
902 get_nrows (meta, MONO_TABLE_TYPEREF));
903 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPESPEC));
905 /* 2 bits to encode */
906 field_size = rtsize (meta, n, 16-2);
910 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
912 case MONO_MT_MRP_IDX:
913 n = MAX (get_nrows (meta, MONO_TABLE_TYPEDEF),
914 get_nrows (meta, MONO_TABLE_TYPEREF));
915 n = MAX (n, get_nrows (meta, MONO_TABLE_METHOD));
916 n = MAX (n, get_nrows (meta, MONO_TABLE_MODULEREF));
917 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPESPEC));
919 /* 3 bits to encode */
920 field_size = rtsize (meta, n, 16 - 3);
924 * MethodDefOrRef: MethodDef, MemberRef
926 case MONO_MT_MDOR_IDX:
927 n = MAX (get_nrows (meta, MONO_TABLE_METHOD),
928 get_nrows (meta, MONO_TABLE_MEMBERREF));
930 /* 1 bit used to encode tag */
931 field_size = rtsize (meta, n, 16-1);
935 * HasSemantics: Property, Event
938 n = MAX (get_nrows (meta, MONO_TABLE_PROPERTY),
939 get_nrows (meta, MONO_TABLE_EVENT));
941 /* 1 bit used to encode tag */
942 field_size = rtsize (meta, n, 16-1);
946 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
949 n = MAX (get_nrows (meta, MONO_TABLE_MODULE),
950 get_nrows (meta, MONO_TABLE_MODULEREF));
951 n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
952 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPEREF));
954 /* 2 bits used to encode tag (ECMA spec claims 3) */
955 field_size = rtsize (meta, n, 16 - 2);
960 * encode field size as follows (we just need to
967 bitfield |= (field_size-1) << shift;
970 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
973 *result_bitfield = (i << 24) | bitfield;
978 * mono_metadata_compute_table_bases:
979 * \param meta metadata context to compute table values
981 * Computes the table bases for the metadata structure.
982 * This is an internal function used by the image loader code.
985 mono_metadata_compute_table_bases (MonoImage *meta)
988 const char *base = meta->tables_base;
990 for (i = 0; i < MONO_TABLE_NUM; i++) {
991 MonoTableInfo *table = &meta->tables [i];
992 if (table->rows == 0)
995 table->row_size = mono_metadata_compute_size (meta, i, &table->size_bitfield);
997 base += table->rows * table->row_size;
1002 * mono_metadata_locate:
1003 * \param meta metadata context
1004 * \param table table code.
1005 * \param idx index of element to retrieve from \p table.
1007 * \returns a pointer to the \p idx element in the metadata table
1008 * whose code is \p table.
1011 mono_metadata_locate (MonoImage *meta, int table, int idx)
1013 /* idx == 0 refers always to NULL */
1014 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, ""); /*FIXME shouldn't we return NULL here?*/
1016 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
1020 * mono_metadata_locate_token:
1021 * \param meta metadata context
1022 * \param token metadata token
1024 * \returns a pointer to the data in the metadata represented by the
1028 mono_metadata_locate_token (MonoImage *meta, guint32 token)
1030 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
1034 * mono_metadata_string_heap:
1035 * \param meta metadata context
1036 * \param index index into the string heap.
1037 * \returns an in-memory pointer to the \p index in the string heap.
1040 mono_metadata_string_heap (MonoImage *meta, guint32 index)
1042 g_assert (index < meta->heap_strings.size);
1043 g_return_val_if_fail (index < meta->heap_strings.size, "");
1044 return meta->heap_strings.data + index;
1048 * mono_metadata_user_string:
1049 * \param meta metadata context
1050 * \param index index into the user string heap.
1051 * \returns an in-memory pointer to the \p index in the user string heap (<code>#US</code>).
1054 mono_metadata_user_string (MonoImage *meta, guint32 index)
1056 g_assert (index < meta->heap_us.size);
1057 g_return_val_if_fail (index < meta->heap_us.size, "");
1058 return meta->heap_us.data + index;
1062 * mono_metadata_blob_heap:
1063 * \param meta metadata context
1064 * \param index index into the blob.
1065 * \returns an in-memory pointer to the \p index in the Blob heap.
1068 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
1070 g_assert (index < meta->heap_blob.size);
1071 g_return_val_if_fail (index < meta->heap_blob.size, "");/*FIXME shouldn't we return NULL and check for index == 0?*/
1072 return meta->heap_blob.data + index;
1076 * mono_metadata_guid_heap:
1077 * \param meta metadata context
1078 * \param index index into the guid heap.
1079 * \returns an in-memory pointer to the \p index in the guid heap.
1082 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
1085 index *= 16; /* adjust for guid size and 1-based index */
1086 g_return_val_if_fail (index < meta->heap_guid.size, "");
1087 return meta->heap_guid.data + index;
1090 static const unsigned char *
1091 dword_align (const unsigned char *ptr)
1093 #if SIZEOF_VOID_P == 8
1094 return (const unsigned char *) (((guint64) (ptr + 3)) & ~3);
1096 return (const unsigned char *) (((guint32) (ptr + 3)) & ~3);
1101 * mono_metadata_decode_row:
1102 * \param t table to extract information from.
1103 * \param idx index in table.
1104 * \param res array of \p res_size cols to store the results in
1106 * This decompresses the metadata element \p idx in table \p t
1107 * into the \c guint32 \p res array that has \p res_size elements
1110 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
1112 guint32 bitfield = t->size_bitfield;
1113 int i, count = mono_metadata_table_count (bitfield);
1116 g_assert (idx < t->rows);
1117 g_assert (idx >= 0);
1118 data = t->base + idx * t->row_size;
1120 g_assert (res_size == count);
1122 for (i = 0; i < count; i++) {
1123 int n = mono_metadata_table_size (bitfield, i);
1127 res [i] = *data; break;
1129 res [i] = read16 (data); break;
1131 res [i] = read32 (data); break;
1133 g_assert_not_reached ();
1140 * mono_metadata_decode_row_col:
1141 * \param t table to extract information from.
1142 * \param idx index for row in table.
1143 * \param col column in the row.
1145 * This function returns the value of column \p col from the \p idx
1146 * row in the table \p t .
1149 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
1151 guint32 bitfield = t->size_bitfield;
1153 register const char *data;
1156 g_assert (idx < t->rows);
1157 g_assert (col < mono_metadata_table_count (bitfield));
1158 data = t->base + idx * t->row_size;
1160 n = mono_metadata_table_size (bitfield, 0);
1161 for (i = 0; i < col; ++i) {
1163 n = mono_metadata_table_size (bitfield, i + 1);
1169 return read16 (data);
1171 return read32 (data);
1173 g_assert_not_reached ();
1179 * mono_metadata_decode_blob_size:
1180 * \param ptr pointer to a blob object
1181 * \param rptr the new position of the pointer
1183 * This decodes a compressed size as described by 24.2.4 (#US and #Blob a blob or user string object)
1185 * \returns the size of the blob object
1188 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
1190 const unsigned char *ptr = (const unsigned char *)xptr;
1193 if ((*ptr & 0x80) == 0){
1194 size = ptr [0] & 0x7f;
1196 } else if ((*ptr & 0x40) == 0){
1197 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
1200 size = ((ptr [0] & 0x1f) << 24) +
1212 * mono_metadata_decode_value:
1213 * \param ptr pointer to decode from
1214 * \param rptr the new position of the pointer
1216 * This routine decompresses 32-bit values as specified in the "Blob and
1217 * Signature" section (23.2)
1219 * \returns the decoded value
1222 mono_metadata_decode_value (const char *_ptr, const char **rptr)
1224 const unsigned char *ptr = (const unsigned char *) _ptr;
1225 unsigned char b = *ptr;
1228 if ((b & 0x80) == 0){
1231 } else if ((b & 0x40) == 0){
1232 len = ((b & 0x3f) << 8 | ptr [1]);
1235 len = ((b & 0x1f) << 24) |
1248 * mono_metadata_decode_signed_value:
1249 * \param ptr pointer to decode from
1250 * \param rptr the new position of the pointer
1252 * This routine decompresses 32-bit signed values
1253 * (not specified in the spec)
1255 * \returns the decoded value
1258 mono_metadata_decode_signed_value (const char *ptr, const char **rptr)
1260 guint32 uval = mono_metadata_decode_value (ptr, rptr);
1261 gint32 ival = uval >> 1;
1264 /* ival is a truncated 2's complement negative number. */
1266 /* 6 bits = 7 bits for compressed representation (top bit is '0') - 1 sign bit */
1269 /* 13 bits = 14 bits for compressed representation (top bits are '10') - 1 sign bit */
1270 return ival - 0x2000;
1271 if (ival < 0x10000000)
1272 /* 28 bits = 29 bits for compressed representation (top bits are '110') - 1 sign bit */
1273 return ival - 0x10000000;
1274 g_assert (ival < 0x20000000);
1275 g_warning ("compressed signed value appears to use 29 bits for compressed representation: %x (raw: %8x)", ival, uval);
1276 return ival - 0x20000000;
1280 * mono_metadata_translate_token_index:
1281 * Translates the given 1-based index into the \c Method, \c Field, \c Event, or \c Param tables
1282 * using the \c *Ptr tables in uncompressed metadata, if they are available.
1284 * FIXME: The caller is not forced to call this function, which is error-prone, since
1285 * forgetting to call it would only show up as a bug on uncompressed metadata.
1288 mono_metadata_translate_token_index (MonoImage *image, int table, guint32 idx)
1290 if (!image->uncompressed_metadata)
1294 case MONO_TABLE_METHOD:
1295 if (image->tables [MONO_TABLE_METHOD_POINTER].rows)
1296 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD_POINTER], idx - 1, MONO_METHOD_POINTER_METHOD);
1299 case MONO_TABLE_FIELD:
1300 if (image->tables [MONO_TABLE_FIELD_POINTER].rows)
1301 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_FIELD_POINTER], idx - 1, MONO_FIELD_POINTER_FIELD);
1304 case MONO_TABLE_EVENT:
1305 if (image->tables [MONO_TABLE_EVENT_POINTER].rows)
1306 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_EVENT_POINTER], idx - 1, MONO_EVENT_POINTER_EVENT);
1309 case MONO_TABLE_PROPERTY:
1310 if (image->tables [MONO_TABLE_PROPERTY_POINTER].rows)
1311 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PROPERTY_POINTER], idx - 1, MONO_PROPERTY_POINTER_PROPERTY);
1314 case MONO_TABLE_PARAM:
1315 if (image->tables [MONO_TABLE_PARAM_POINTER].rows)
1316 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PARAM_POINTER], idx - 1, MONO_PARAM_POINTER_PARAM);
1325 * mono_metadata_decode_table_row:
1327 * Same as \c mono_metadata_decode_row, but takes an \p image + \p table ID pair, and takes
1328 * uncompressed metadata into account, so it should be used to access the
1329 * \c Method, \c Field, \c Param and \c Event tables when the access is made from metadata, i.e.
1330 * \p idx is retrieved from a metadata table, like \c MONO_TYPEDEF_FIELD_LIST.
1333 mono_metadata_decode_table_row (MonoImage *image, int table, int idx, guint32 *res, int res_size)
1335 if (image->uncompressed_metadata)
1336 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1338 mono_metadata_decode_row (&image->tables [table], idx, res, res_size);
1342 * mono_metadata_decode_table_row_col:
1344 * Same as \c mono_metadata_decode_row_col, but takes an \p image + \p table ID pair, and takes
1345 * uncompressed metadata into account, so it should be used to access the
1346 * \c Method, \c Field, \c Param and \c Event tables.
1348 guint32 mono_metadata_decode_table_row_col (MonoImage *image, int table, int idx, guint col)
1350 if (image->uncompressed_metadata)
1351 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1353 return mono_metadata_decode_row_col (&image->tables [table], idx, col);
1357 * mono_metadata_parse_typedef_or_ref:
1358 * \param m a metadata context.
1359 * \param ptr a pointer to an encoded TypedefOrRef in \p m
1360 * \param rptr pointer updated to match the end of the decoded stream
1361 * \returns a token valid in the \p m metadata decoded from
1362 * the compressed representation.
1365 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1368 token = mono_metadata_decode_value (ptr, &ptr);
1371 return mono_metadata_token_from_dor (token);
1375 * mono_metadata_parse_custom_mod:
1376 * \param m a metadata context.
1377 * \param dest storage where the info about the custom modifier is stored (may be NULL)
1378 * \param ptr a pointer to (possibly) the start of a custom modifier list
1379 * \param rptr pointer updated to match the end of the decoded stream
1381 * Checks if \p ptr points to a type custom modifier compressed representation.
1383 * \returns TRUE if a custom modifier was found, FALSE if not.
1386 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1388 MonoCustomMod local;
1389 if ((*ptr == MONO_TYPE_CMOD_OPT) || (*ptr == MONO_TYPE_CMOD_REQD)) {
1392 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1393 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1400 * mono_metadata_parse_array_internal:
1401 * @m: a metadata context.
1402 * @transient: whenever to allocate data from the heap
1403 * @ptr: a pointer to an encoded array description.
1404 * @rptr: pointer updated to match the end of the decoded stream
1406 * Decodes the compressed array description found in the metadata @m at @ptr.
1408 * Returns: a #MonoArrayType structure describing the array type
1409 * and dimensions. Memory is allocated from the heap or from the image mempool, depending
1410 * on the value of @transient.
1412 * LOCKING: Acquires the loader lock
1414 static MonoArrayType *
1415 mono_metadata_parse_array_internal (MonoImage *m, MonoGenericContainer *container,
1416 gboolean transient, const char *ptr, const char **rptr, MonoError *error)
1419 MonoArrayType *array;
1422 etype = mono_metadata_parse_type_checked (m, container, 0, FALSE, ptr, &ptr, error); //FIXME this doesn't respect @transient
1426 array = transient ? (MonoArrayType *)g_malloc0 (sizeof (MonoArrayType)) : (MonoArrayType *)mono_image_alloc0 (m, sizeof (MonoArrayType));
1427 array->eklass = mono_class_from_mono_type (etype);
1428 array->rank = mono_metadata_decode_value (ptr, &ptr);
1430 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1431 if (array->numsizes)
1432 array->sizes = transient ? (int *)g_malloc0 (sizeof (int) * array->numsizes) : (int *)mono_image_alloc0 (m, sizeof (int) * array->numsizes);
1433 for (i = 0; i < array->numsizes; ++i)
1434 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1436 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1437 if (array->numlobounds)
1438 array->lobounds = transient ? (int *)g_malloc0 (sizeof (int) * array->numlobounds) : (int *)mono_image_alloc0 (m, sizeof (int) * array->numlobounds);
1439 for (i = 0; i < array->numlobounds; ++i)
1440 array->lobounds [i] = mono_metadata_decode_signed_value (ptr, &ptr);
1448 * mono_metadata_parse_array:
1451 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1454 MonoArrayType *ret = mono_metadata_parse_array_internal (m, NULL, FALSE, ptr, rptr, &error);
1455 mono_error_cleanup (&error);
1461 * mono_metadata_free_array:
1462 * \param array array description
1464 * Frees the array description returned from \c mono_metadata_parse_array.
1467 mono_metadata_free_array (MonoArrayType *array)
1469 g_free (array->sizes);
1470 g_free (array->lobounds);
1475 * need to add common field and param attributes combinations:
1478 * public static literal
1481 * private static literal
1483 static const MonoType
1485 /* data, attrs, type, nmods, byref, pinned */
1486 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1487 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1488 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1489 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1490 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1491 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1492 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1493 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1494 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1495 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1496 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1497 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1498 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1499 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1500 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1501 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1502 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1503 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1504 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1505 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1506 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1507 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1508 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1509 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1510 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1511 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1512 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1513 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1514 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1515 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1516 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1517 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1518 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1519 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1522 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1524 static GHashTable *type_cache = NULL;
1525 static gint32 next_generic_inst_id = 0;
1527 /* Protected by image_sets_mutex */
1528 static MonoImageSet *mscorlib_image_set;
1529 /* Protected by image_sets_mutex */
1530 static GPtrArray *image_sets;
1531 static mono_mutex_t image_sets_mutex;
1533 static guint mono_generic_class_hash (gconstpointer data);
1536 * MonoTypes with modifies are never cached, so we never check or use that field.
1539 mono_type_hash (gconstpointer data)
1541 const MonoType *type = (const MonoType *) data;
1542 if (type->type == MONO_TYPE_GENERICINST)
1543 return mono_generic_class_hash (type->data.generic_class);
1545 return type->type | (type->byref << 8) | (type->attrs << 9);
1549 mono_type_equal (gconstpointer ka, gconstpointer kb)
1551 const MonoType *a = (const MonoType *) ka;
1552 const MonoType *b = (const MonoType *) kb;
1554 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1556 /* need other checks */
1561 mono_metadata_generic_inst_hash (gconstpointer data)
1563 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1567 for (i = 0; i < ginst->type_argc; ++i) {
1569 hash += mono_metadata_type_hash (ginst->type_argv [i]);
1572 return hash ^ (ginst->is_open << 8);
1576 mono_generic_inst_equal_full (const MonoGenericInst *a, const MonoGenericInst *b, gboolean signature_only)
1580 // An optimization: if the ids of two insts are the same, we know they are the same inst and don't check contents.
1581 // Furthermore, because we perform early de-duping, if the ids differ, we know the contents differ.
1582 #ifndef MONO_SMALL_CONFIG // Optimization does not work in MONO_SMALL_CONFIG: There are no IDs
1583 if (a->id && b->id) { // "id 0" means "object has no id"-- de-duping hasn't been performed yet, must check contents.
1586 // In signature-comparison mode id equality implies object equality, but this is not true for inequality.
1587 // Two separate objects could have signature-equavalent contents.
1588 if (!signature_only)
1593 if (a->is_open != b->is_open || a->type_argc != b->type_argc)
1595 for (i = 0; i < a->type_argc; ++i) {
1596 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], signature_only))
1603 mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1605 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1606 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1608 return mono_generic_inst_equal_full (a, b, FALSE);
1612 mono_generic_class_hash (gconstpointer data)
1614 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1615 guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
1618 hash += gclass->is_tb_open;
1619 hash += mono_metadata_generic_context_hash (&gclass->context);
1625 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1627 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1628 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1630 return _mono_metadata_generic_class_equal (a, b, FALSE);
1634 * mono_metadata_init:
1636 * Initialize the global variables of this module.
1637 * This is a Mono runtime internal function.
1640 mono_metadata_init (void)
1644 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1646 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1647 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1649 mono_os_mutex_init_recursive (&image_sets_mutex);
1651 mono_counters_register ("ImgSet Cache Hit", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &img_set_cache_hit);
1652 mono_counters_register ("ImgSet Cache Miss", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &img_set_cache_miss);
1653 mono_counters_register ("ImgSet Count", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &img_set_count);
1657 * mono_metadata_cleanup:
1659 * Free all resources used by this module.
1660 * This is a Mono runtime internal function.
1663 mono_metadata_cleanup (void)
1665 g_hash_table_destroy (type_cache);
1667 g_ptr_array_free (image_sets, TRUE);
1669 mono_os_mutex_destroy (&image_sets_mutex);
1673 * mono_metadata_parse_type:
1674 * \param m metadata context
1675 * \param mode kind of type that may be found at \p ptr
1676 * \param opt_attrs optional attributes to store in the returned type
1677 * \param ptr pointer to the type representation
1678 * \param rptr pointer updated to match the end of the decoded stream
1679 * \param transient whenever to allocate the result from the heap or from a mempool
1681 * Decode a compressed type description found at \p ptr in \p m .
1682 * \p mode can be one of \c MONO_PARSE_MOD_TYPE, \c MONO_PARSE_PARAM, \c MONO_PARSE_RET,
1683 * \c MONO_PARSE_FIELD, \c MONO_PARSE_LOCAL, \c MONO_PARSE_TYPE.
1684 * This function can be used to decode type descriptions in method signatures,
1685 * field signatures, locals signatures etc.
1687 * To parse a generic type, \c generic_container points to the current class'es
1688 * (the \c generic_container field in the <code>MonoClass</code>) or the current generic method's
1689 * (stored in <code>image->property_hash</code>) generic container.
1690 * When we encounter a \c MONO_TYPE_VAR or \c MONO_TYPE_MVAR, it's looked up in
1691 * this \c MonoGenericContainer.
1693 * LOCKING: Acquires the loader lock.
1695 * \returns a \c MonoType structure representing the decoded type.
1698 mono_metadata_parse_type_internal (MonoImage *m, MonoGenericContainer *container,
1699 short opt_attrs, gboolean transient, const char *ptr, const char **rptr, MonoError *error)
1701 MonoType *type, *cached;
1703 gboolean byref = FALSE;
1704 gboolean pinned = FALSE;
1705 const char *tmp_ptr;
1706 int count = 0; // Number of mod arguments
1712 * According to the spec, custom modifiers should come before the byref
1713 * flag, but the IL produced by ilasm from the following signature:
1714 * object modopt(...) &
1715 * starts with a byref flag, followed by the modifiers. (bug #49802)
1716 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1717 * it would be better to treat byref as real type constructor instead of
1719 * Also, pinned should come before anything else, but some MSV++ produced
1720 * assemblies violate this (#bug 61990).
1723 /* Count the modifiers first */
1728 case MONO_TYPE_PINNED:
1729 case MONO_TYPE_BYREF:
1732 case MONO_TYPE_CMOD_REQD:
1733 case MONO_TYPE_CMOD_OPT:
1735 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1742 if (count) { // There are mods, so the MonoType will be of nonstandard size.
1745 size = MONO_SIZEOF_TYPE + ((gint32)count) * sizeof (MonoCustomMod);
1746 type = transient ? (MonoType *)g_malloc0 (size) : (MonoType *)mono_image_alloc0 (m, size);
1747 type->num_mods = count;
1749 mono_error_set_bad_image (error, m, "Invalid type with more than 64 modifiers");
1752 } else { // The type is of standard size, so we can allocate it on the stack.
1754 memset (type, 0, MONO_SIZEOF_TYPE);
1757 /* Iterate again, but now parse pinned, byref and custom modifiers */
1762 case MONO_TYPE_PINNED:
1766 case MONO_TYPE_BYREF:
1770 case MONO_TYPE_CMOD_REQD:
1771 case MONO_TYPE_CMOD_OPT:
1772 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1780 type->attrs = opt_attrs;
1781 type->byref = byref;
1782 type->pinned = pinned ? 1 : 0;
1784 if (!do_mono_metadata_parse_type (type, m, container, transient, ptr, &ptr, error))
1790 // Possibly we can return an already-allocated type instead of the one we decoded
1791 if (!type->num_mods && !transient) {
1792 /* no need to free type here, because it is on the stack */
1793 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1794 MonoType *ret = type->byref ? &type->data.klass->this_arg : &type->data.klass->byval_arg;
1796 /* Consider the case:
1798 class Foo<T> { class Bar {} }
1799 class Test : Foo<Test>.Bar {}
1801 When Foo<Test> is being expanded, 'Test' isn't yet initialized. It's actually in
1802 a really pristine state: it doesn't even know whether 'Test' is a reference or a value type.
1804 We ensure that the MonoClass is in a state that we can canonicalize to:
1806 klass->byval_arg.data.klass == klass
1807 klass->this_arg.data.klass == klass
1809 If we can't canonicalize 'type', it doesn't matter, since later users of 'type' will do it.
1811 LOCKING: even though we don't explicitly hold a lock, in the problematic case 'ret' is a field
1812 of a MonoClass which currently holds the loader lock. 'type' is local.
1814 if (ret->data.klass == type->data.klass) {
1818 /* No need to use locking since nobody is modifying the hash table */
1819 if ((cached = (MonoType *)g_hash_table_lookup (type_cache, type))) {
1824 /* printf ("%x %x %c %s\n", type->attrs, type->num_mods, type->pinned ? 'p' : ' ', mono_type_full_name (type)); */
1826 if (type == &stype) { // Type was allocated on the stack, so we need to copy it to safety
1827 type = transient ? (MonoType *)g_malloc (MONO_SIZEOF_TYPE) : (MonoType *)mono_image_alloc (m, MONO_SIZEOF_TYPE);
1828 memcpy (type, &stype, MONO_SIZEOF_TYPE);
1835 mono_metadata_parse_type_checked (MonoImage *m, MonoGenericContainer *container,
1836 short opt_attrs, gboolean transient, const char *ptr, const char **rptr, MonoError *error)
1838 return mono_metadata_parse_type_internal (m, container, opt_attrs, transient, ptr, rptr, error);
1842 * LOCKING: Acquires the loader lock.
1845 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1846 const char *ptr, const char **rptr)
1849 MonoType * type = mono_metadata_parse_type_internal (m, NULL, opt_attrs, FALSE, ptr, rptr, &error);
1850 mono_error_cleanup (&error);
1855 mono_metadata_method_has_param_attrs (MonoImage *m, int def)
1857 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1858 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1859 guint lastp, i, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1861 if (def < methodt->rows)
1862 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1864 lastp = m->tables [MONO_TABLE_PARAM].rows + 1;
1866 for (i = param_index; i < lastp; ++i) {
1867 guint32 flags = mono_metadata_decode_row_col (paramt, i - 1, MONO_PARAM_FLAGS);
1876 * mono_metadata_get_param_attrs:
1878 * @m The image to loader parameter attributes from
1879 * @def method def token (one based)
1880 * @param_count number of params to decode including the return value
1882 * Return the parameter attributes for the method whose MethodDef index is DEF. The
1883 * returned memory needs to be freed by the caller. If all the param attributes are
1884 * 0, then NULL is returned.
1887 mono_metadata_get_param_attrs (MonoImage *m, int def, int param_count)
1889 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1890 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1891 guint32 cols [MONO_PARAM_SIZE];
1892 guint lastp, i, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1895 if (def < methodt->rows)
1896 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1898 lastp = paramt->rows + 1;
1900 for (i = param_index; i < lastp; ++i) {
1901 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1902 if (cols [MONO_PARAM_FLAGS]) {
1904 pattrs = g_new0 (int, param_count);
1905 /* at runtime we just ignore this kind of malformed file:
1906 * the verifier can signal the error to the user
1908 if (cols [MONO_PARAM_SEQUENCE] >= param_count)
1910 pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
1919 * mono_metadata_parse_signature:
1920 * \param image metadata context
1921 * \param token metadata token
1923 * Decode a method signature stored in the \c StandAloneSig table
1925 * \returns a \c MonoMethodSignature describing the signature.
1927 MonoMethodSignature*
1928 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1931 MonoMethodSignature *ret;
1932 ret = mono_metadata_parse_signature_checked (image, token, &error);
1933 mono_error_cleanup (&error);
1938 * mono_metadata_parse_signature_checked:
1939 * @image: metadata context
1940 * @token: metadata token
1941 * @error: set on error
1943 * Decode a method signature stored in the STANDALONESIG table
1945 * Returns: a MonoMethodSignature describing the signature. On failure
1946 * returns NULL and sets @error.
1948 MonoMethodSignature*
1949 mono_metadata_parse_signature_checked (MonoImage *image, guint32 token, MonoError *error)
1953 MonoTableInfo *tables = image->tables;
1954 guint32 idx = mono_metadata_token_index (token);
1958 if (image_is_dynamic (image)) {
1959 return (MonoMethodSignature *)mono_lookup_dynamic_token (image, token, NULL, error);
1962 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1964 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1966 ptr = mono_metadata_blob_heap (image, sig);
1967 mono_metadata_decode_blob_size (ptr, &ptr);
1969 return mono_metadata_parse_method_signature_full (image, NULL, 0, ptr, NULL, error);
1973 * mono_metadata_signature_alloc:
1974 * \param image metadata context
1975 * \param nparams number of parameters in the signature
1977 * Allocate a \c MonoMethodSignature structure with the specified number of params.
1978 * The return type and the params types need to be filled later.
1979 * This is a Mono runtime internal function.
1981 * LOCKING: Assumes the loader lock is held.
1983 * \returns the new \c MonoMethodSignature structure.
1985 MonoMethodSignature*
1986 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1988 MonoMethodSignature *sig;
1990 sig = (MonoMethodSignature *)mono_image_alloc0 (m, MONO_SIZEOF_METHOD_SIGNATURE + ((gint32)nparams) * sizeof (MonoType*));
1991 sig->param_count = nparams;
1992 sig->sentinelpos = -1;
1997 static MonoMethodSignature*
1998 mono_metadata_signature_dup_internal_with_padding (MonoImage *image, MonoMemPool *mp, MonoMethodSignature *sig, size_t padding)
2000 int sigsize, sig_header_size;
2001 MonoMethodSignature *ret;
2002 sigsize = sig_header_size = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *) + padding;
2004 sigsize += MONO_SIZEOF_TYPE;
2007 ret = (MonoMethodSignature *)mono_image_alloc (image, sigsize);
2009 ret = (MonoMethodSignature *)mono_mempool_alloc (mp, sigsize);
2011 ret = (MonoMethodSignature *)g_malloc (sigsize);
2014 memcpy (ret, sig, sig_header_size - padding);
2016 // Copy return value because of ownership semantics.
2018 // Danger! Do not alter padding use without changing the dup_add_this below
2019 intptr_t end_of_header = (intptr_t)( (char*)(ret) + sig_header_size);
2020 ret->ret = (MonoType *)end_of_header;
2021 memcpy (ret->ret, sig->ret, MONO_SIZEOF_TYPE);
2027 static MonoMethodSignature*
2028 mono_metadata_signature_dup_internal (MonoImage *image, MonoMemPool *mp, MonoMethodSignature *sig)
2030 return mono_metadata_signature_dup_internal_with_padding (image, mp, sig, 0);
2033 * signature_dup_add_this:
2035 * Make a copy of @sig, adding an explicit this argument.
2037 MonoMethodSignature*
2038 mono_metadata_signature_dup_add_this (MonoImage *image, MonoMethodSignature *sig, MonoClass *klass)
2040 MonoMethodSignature *ret;
2041 ret = mono_metadata_signature_dup_internal_with_padding (image, NULL, sig, sizeof (MonoType *));
2043 ret->param_count = sig->param_count + 1;
2044 ret->hasthis = FALSE;
2046 for (int i = sig->param_count - 1; i >= 0; i --)
2047 ret->params [i + 1] = sig->params [i];
2048 ret->params [0] = klass->valuetype ? &klass->this_arg : &klass->byval_arg;
2050 for (int i = sig->param_count - 1; i >= 0; i --)
2051 g_assert(ret->params [i + 1]->type == sig->params [i]->type && ret->params [i+1]->type != MONO_TYPE_END);
2052 g_assert (ret->ret->type == sig->ret->type && ret->ret->type != MONO_TYPE_END);
2059 MonoMethodSignature*
2060 mono_metadata_signature_dup_full (MonoImage *image, MonoMethodSignature *sig)
2062 MonoMethodSignature *ret = mono_metadata_signature_dup_internal (image, NULL, sig);
2064 for (int i = 0 ; i < sig->param_count; i ++)
2065 g_assert(ret->params [i]->type == sig->params [i]->type);
2066 g_assert (ret->ret->type == sig->ret->type);
2071 /*The mempool is accessed without synchronization*/
2072 MonoMethodSignature*
2073 mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig)
2075 return mono_metadata_signature_dup_internal (NULL, mp, sig);
2079 * mono_metadata_signature_dup:
2080 * \param sig method signature
2082 * Duplicate an existing \c MonoMethodSignature so it can be modified.
2083 * This is a Mono runtime internal function.
2085 * \returns the new \c MonoMethodSignature structure.
2087 MonoMethodSignature*
2088 mono_metadata_signature_dup (MonoMethodSignature *sig)
2090 return mono_metadata_signature_dup_full (NULL, sig);
2094 * mono_metadata_signature_size:
2096 * Return the amount of memory allocated to SIG.
2099 mono_metadata_signature_size (MonoMethodSignature *sig)
2101 return MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
2105 * mono_metadata_parse_method_signature:
2106 * @m: metadata context
2107 * @generic_container: generics container
2108 * @def: the MethodDef index or 0 for Ref signatures.
2109 * @ptr: pointer to the signature metadata representation
2110 * @rptr: pointer updated to match the end of the decoded stream
2112 * Decode a method signature stored at @ptr.
2113 * This is a Mono runtime internal function.
2115 * LOCKING: Assumes the loader lock is held.
2117 * Returns: a MonoMethodSignature describing the signature.
2119 MonoMethodSignature *
2120 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *container,
2121 int def, const char *ptr, const char **rptr, MonoError *error)
2123 MonoMethodSignature *method;
2124 int i, *pattrs = NULL;
2125 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
2126 guint32 gen_param_count = 0;
2127 gboolean is_open = FALSE;
2132 gen_param_count = 1;
2137 call_convention = *ptr & 0x0F;
2139 if (gen_param_count)
2140 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
2141 param_count = mono_metadata_decode_value (ptr, &ptr);
2144 pattrs = mono_metadata_get_param_attrs (m, def, param_count + 1); /*Must be + 1 since signature's param count doesn't account for the return value */
2146 method = mono_metadata_signature_alloc (m, param_count);
2147 method->hasthis = hasthis;
2148 method->explicit_this = explicit_this;
2149 method->call_convention = call_convention;
2150 method->generic_param_count = gen_param_count;
2152 if (call_convention != 0xa) {
2153 method->ret = mono_metadata_parse_type_checked (m, container, pattrs ? pattrs [0] : 0, FALSE, ptr, &ptr, error);
2155 mono_metadata_free_method_signature (method);
2159 is_open = mono_class_is_open_constructed_type (method->ret);
2162 for (i = 0; i < method->param_count; ++i) {
2163 if (*ptr == MONO_TYPE_SENTINEL) {
2164 if (method->call_convention != MONO_CALL_VARARG || def) {
2165 mono_error_set_bad_image (error, m, "Found sentinel for methoddef or no vararg");
2169 if (method->sentinelpos >= 0) {
2170 mono_error_set_bad_image (error, m, "Found sentinel twice in the same signature.");
2174 method->sentinelpos = i;
2177 method->params [i] = mono_metadata_parse_type_checked (m, container, pattrs ? pattrs [i+1] : 0, FALSE, ptr, &ptr, error);
2178 if (!method->params [i]) {
2179 mono_metadata_free_method_signature (method);
2184 is_open = mono_class_is_open_constructed_type (method->params [i]);
2187 /* The sentinel could be missing if the caller does not pass any additional arguments */
2188 if (!def && method->call_convention == MONO_CALL_VARARG && method->sentinelpos < 0)
2189 method->sentinelpos = method->param_count;
2191 method->has_type_parameters = is_open;
2193 if (def && (method->call_convention == MONO_CALL_VARARG))
2194 method->sentinelpos = method->param_count;
2201 * Add signature to a cache and increase ref count...
2208 * mono_metadata_parse_method_signature:
2209 * \param m metadata context
2210 * \param def the \c MethodDef index or 0 for \c Ref signatures.
2211 * \param ptr pointer to the signature metadata representation
2212 * \param rptr pointer updated to match the end of the decoded stream
2214 * Decode a method signature stored at \p ptr.
2215 * This is a Mono runtime internal function.
2217 * LOCKING: Assumes the loader lock is held.
2219 * \returns a \c MonoMethodSignature describing the signature.
2221 MonoMethodSignature *
2222 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
2225 * This function MUST NOT be called by runtime code as it does error handling incorrectly.
2226 * Use mono_metadata_parse_method_signature_full instead.
2227 * It's ok to asser on failure as we no longer use it.
2230 MonoMethodSignature *ret;
2231 ret = mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr, &error);
2232 g_assert (mono_error_ok (&error));
2238 * mono_metadata_free_method_signature:
2239 * \param sig signature to destroy
2241 * Free the memory allocated in the signature \p sig.
2242 * This method needs to be robust and work also on partially-built
2243 * signatures, so it does extra checks.
2246 mono_metadata_free_method_signature (MonoMethodSignature *sig)
2248 /* Everything is allocated from mempools */
2252 mono_metadata_free_type (sig->ret);
2253 for (i = 0; i < sig->param_count; ++i) {
2254 if (sig->params [i])
2255 mono_metadata_free_type (sig->params [i]);
2261 mono_metadata_free_inflated_signature (MonoMethodSignature *sig)
2265 /* Allocated in inflate_generic_signature () */
2267 mono_metadata_free_type (sig->ret);
2268 for (i = 0; i < sig->param_count; ++i) {
2269 if (sig->params [i])
2270 mono_metadata_free_type (sig->params [i]);
2276 inflated_method_equal (gconstpointer a, gconstpointer b)
2278 const MonoMethodInflated *ma = (const MonoMethodInflated *)a;
2279 const MonoMethodInflated *mb = (const MonoMethodInflated *)b;
2280 if (ma->declaring != mb->declaring)
2282 return mono_metadata_generic_context_equal (&ma->context, &mb->context);
2286 inflated_method_hash (gconstpointer a)
2288 const MonoMethodInflated *ma = (const MonoMethodInflated *)a;
2289 return (mono_metadata_generic_context_hash (&ma->context) ^ mono_aligned_addr_hash (ma->declaring));
2293 inflated_signature_equal (gconstpointer a, gconstpointer b)
2295 const MonoInflatedMethodSignature *sig1 = (const MonoInflatedMethodSignature *)a;
2296 const MonoInflatedMethodSignature *sig2 = (const MonoInflatedMethodSignature *)b;
2298 /* sig->sig is assumed to be canonized */
2299 if (sig1->sig != sig2->sig)
2301 /* The generic instances are canonized */
2302 return mono_metadata_generic_context_equal (&sig1->context, &sig2->context);
2306 inflated_signature_hash (gconstpointer a)
2308 const MonoInflatedMethodSignature *sig = (const MonoInflatedMethodSignature *)a;
2310 /* sig->sig is assumed to be canonized */
2311 return mono_metadata_generic_context_hash (&sig->context) ^ mono_aligned_addr_hash (sig->sig);
2315 dump_ginst (MonoGenericInst *ginst)
2320 g_print ("Ginst: <");
2321 for (i = 0; i < ginst->type_argc; ++i) {
2324 name = mono_type_get_name (ginst->type_argv [i]);
2325 g_print ("%s", name);
2331 static gboolean type_in_image (MonoType *type, MonoImage *image);
2334 signature_in_image (MonoMethodSignature *sig, MonoImage *image)
2336 gpointer iter = NULL;
2339 while ((p = mono_signature_get_params (sig, &iter)) != NULL)
2340 if (type_in_image (p, image))
2343 return type_in_image (mono_signature_get_return_type (sig), image);
2347 ginst_in_image (MonoGenericInst *ginst, MonoImage *image)
2351 for (i = 0; i < ginst->type_argc; ++i) {
2352 if (type_in_image (ginst->type_argv [i], image))
2360 gclass_in_image (MonoGenericClass *gclass, MonoImage *image)
2362 return gclass->container_class->image == image ||
2363 ginst_in_image (gclass->context.class_inst, image);
2367 type_in_image (MonoType *type, MonoImage *image)
2370 switch (type->type) {
2371 case MONO_TYPE_GENERICINST:
2372 return gclass_in_image (type->data.generic_class, image);
2374 type = type->data.type;
2376 case MONO_TYPE_SZARRAY:
2377 type = &type->data.klass->byval_arg;
2379 case MONO_TYPE_ARRAY:
2380 type = &type->data.array->eklass->byval_arg;
2382 case MONO_TYPE_FNPTR:
2383 return signature_in_image (type->data.method, image);
2385 case MONO_TYPE_MVAR:
2386 return image == get_image_for_generic_param (type->data.generic_param);
2388 /* At this point, we should've avoided all potential allocations in mono_class_from_mono_type () */
2389 return image == mono_class_from_mono_type (type)->image;
2394 image_sets_lock (void)
2396 mono_os_mutex_lock (&image_sets_mutex);
2400 image_sets_unlock (void)
2402 mono_os_mutex_unlock (&image_sets_mutex);
2406 compare_pointers (const void *a, const void *b)
2408 return (size_t)a - (size_t)b;
2412 #define HASH_TABLE_SIZE 1103
2413 static MonoImageSet *img_set_cache [HASH_TABLE_SIZE];
2416 mix_hash (uintptr_t source)
2418 unsigned int hash = source;
2421 hash = (((hash * 215497) >> 16) ^ ((hash * 1823231) + hash));
2423 // Mix in highest bits on 64-bit systems only
2424 if (sizeof (source) > 4)
2425 hash = hash ^ (source >> 32);
2431 hash_images (MonoImage **images, int nimages)
2435 for (i = 0; i < nimages; ++i)
2436 res += mix_hash ((size_t)images [i]);
2442 compare_img_set (MonoImageSet *set, MonoImage **images, int nimages)
2446 if (set->nimages != nimages)
2449 for (j = 0; j < nimages; ++j) {
2450 for (k = 0; k < nimages; ++k)
2451 if (set->images [k] == images [j])
2452 break; // Break on match
2454 // If we iterated all the way through set->images, images[j] was *not* found.
2456 break; // Break on "image not found"
2459 // If we iterated all the way through images without breaking, all items in images were found in set->images
2460 return j == nimages;
2464 static MonoImageSet*
2465 img_set_cache_get (MonoImage **images, int nimages)
2467 guint32 hash_code = hash_images (images, nimages);
2468 int index = hash_code % HASH_TABLE_SIZE;
2469 MonoImageSet *img = img_set_cache [index];
2470 if (!img || !compare_img_set (img, images, nimages)) {
2471 ++img_set_cache_miss;
2474 ++img_set_cache_hit;
2479 img_set_cache_add (MonoImageSet *set)
2481 guint32 hash_code = hash_images (set->images, set->nimages);
2482 int index = hash_code % HASH_TABLE_SIZE;
2483 img_set_cache [index] = set;
2487 img_set_cache_remove (MonoImageSet *is)
2489 guint32 hash_code = hash_images (is->images, is->nimages);
2490 int index = hash_code % HASH_TABLE_SIZE;
2491 if (img_set_cache [index] == is)
2492 img_set_cache [index] = NULL;
2497 * Return a MonoImageSet representing the set of images in IMAGES.
2499 static MonoImageSet*
2500 get_image_set (MonoImage **images, int nimages)
2506 /* Common case: Image set contains corlib only. If we've seen that case before, we cached the set. */
2507 if (nimages == 1 && images [0] == mono_defaults.corlib && mscorlib_image_set)
2508 return mscorlib_image_set;
2510 /* Happens with empty generic instances */
2511 // FIXME: Is corlib the correct thing to return here? If so, why? This may be an artifact of generic instances previously defaulting to allocating from corlib.
2513 return mscorlib_image_set;
2515 set = img_set_cache_get (images, nimages);
2522 image_sets = g_ptr_array_new ();
2524 // Before we go on, we should check to see whether a MonoImageSet with these images already exists.
2525 // We can search the referred-by imagesets of any one of our images to do this. Arbitrarily pick one here:
2526 if (images [0] == mono_defaults.corlib && nimages > 1)
2527 l = images [1]->image_sets; // Prefer not to search the imagesets of corlib-- that will be a long list.
2529 l = images [0]->image_sets;
2532 while (l) // Iterate over selected list, looking for an imageset with members equal to our target one
2534 set = (MonoImageSet *)l->data;
2536 if (set->nimages == nimages) { // Member count differs, this can't be it
2537 // Compare all members to all members-- order might be different
2538 for (j = 0; j < nimages; ++j) {
2539 for (k = 0; k < nimages; ++k)
2540 if (set->images [k] == images [j])
2541 break; // Break on match
2543 // If we iterated all the way through set->images, images[j] was *not* found.
2545 break; // Break on "image not found"
2548 // If we iterated all the way through images without breaking, all items in images were found in set->images
2550 // Break on "found a set with equal members".
2551 // This happens in case of a hash collision with a previously cached set.
2559 // If we iterated all the way through l without breaking, the imageset does not already exist and we shuold create it
2561 set = g_new0 (MonoImageSet, 1);
2562 set->nimages = nimages;
2563 set->images = g_new0 (MonoImage*, nimages);
2564 mono_os_mutex_init_recursive (&set->lock);
2565 for (i = 0; i < nimages; ++i)
2566 set->images [i] = images [i];
2567 set->gclass_cache = mono_conc_hashtable_new_full (mono_generic_class_hash, mono_generic_class_equal, NULL, (GDestroyNotify)free_generic_class);
2568 set->ginst_cache = g_hash_table_new_full (mono_metadata_generic_inst_hash, mono_metadata_generic_inst_equal, NULL, (GDestroyNotify)free_generic_inst);
2569 set->gmethod_cache = g_hash_table_new_full (inflated_method_hash, inflated_method_equal, NULL, (GDestroyNotify)free_inflated_method);
2570 set->gsignature_cache = g_hash_table_new_full (inflated_signature_hash, inflated_signature_equal, NULL, (GDestroyNotify)free_inflated_signature);
2572 for (i = 0; i < nimages; ++i)
2573 set->images [i]->image_sets = g_slist_prepend (set->images [i]->image_sets, set);
2575 g_ptr_array_add (image_sets, set);
2579 /* Cache the set. If there was a cache collision, the previously cached value will be replaced. */
2580 img_set_cache_add (set);
2582 if (nimages == 1 && images [0] == mono_defaults.corlib) {
2583 mono_memory_barrier ();
2584 mscorlib_image_set = set;
2587 image_sets_unlock ();
2593 delete_image_set (MonoImageSet *set)
2597 mono_conc_hashtable_destroy (set->gclass_cache);
2598 g_hash_table_destroy (set->ginst_cache);
2599 g_hash_table_destroy (set->gmethod_cache);
2600 g_hash_table_destroy (set->gsignature_cache);
2602 mono_wrapper_caches_free (&set->wrapper_caches);
2606 for (i = 0; i < set->nimages; ++i)
2607 set->images [i]->image_sets = g_slist_remove (set->images [i]->image_sets, set);
2609 g_ptr_array_remove (image_sets, set);
2611 image_sets_unlock ();
2613 img_set_cache_remove (set);
2616 mono_mempool_destroy (set->mempool);
2617 g_free (set->images);
2618 mono_os_mutex_destroy (&set->lock);
2623 mono_image_set_lock (MonoImageSet *set)
2625 mono_os_mutex_lock (&set->lock);
2629 mono_image_set_unlock (MonoImageSet *set)
2631 mono_os_mutex_unlock (&set->lock);
2635 mono_image_set_alloc (MonoImageSet *set, guint size)
2639 mono_image_set_lock (set);
2641 set->mempool = mono_mempool_new_size (INITIAL_IMAGE_SET_SIZE);
2642 res = mono_mempool_alloc (set->mempool, size);
2643 mono_image_set_unlock (set);
2649 mono_image_set_alloc0 (MonoImageSet *set, guint size)
2653 mono_image_set_lock (set);
2655 set->mempool = mono_mempool_new_size (INITIAL_IMAGE_SET_SIZE);
2656 res = mono_mempool_alloc0 (set->mempool, size);
2657 mono_image_set_unlock (set);
2663 mono_image_set_strdup (MonoImageSet *set, const char *s)
2667 mono_image_set_lock (set);
2669 set->mempool = mono_mempool_new_size (INITIAL_IMAGE_SET_SIZE);
2670 res = mono_mempool_strdup (set->mempool, s);
2671 mono_image_set_unlock (set);
2676 // Get a descriptive string for a MonoImageSet
2677 // Callers are obligated to free buffer with g_free after use
2679 mono_image_set_description (MonoImageSet *set)
2681 GString *result = g_string_new (NULL);
2683 g_string_append (result, "[");
2684 for (img = 0; img < set->nimages; img++)
2687 g_string_append (result, ", ");
2688 g_string_append (result, set->images[img]->name);
2690 g_string_append (result, "]");
2691 return g_string_free (result, FALSE);
2695 * Structure used by the collect_..._images functions to store the image list.
2698 MonoImage *image_buf [64];
2700 int nimages, images_len;
2704 collect_data_init (CollectData *data)
2706 data->images = data->image_buf;
2707 data->images_len = 64;
2712 collect_data_free (CollectData *data)
2714 if (data->images != data->image_buf)
2715 g_free (data->images);
2719 enlarge_data (CollectData *data)
2721 int new_len = data->images_len < 16 ? 16 : data->images_len * 2;
2722 MonoImage **d = g_new (MonoImage *, new_len);
2725 g_assert_not_reached ();
2726 memcpy (d, data->images, data->images_len);
2727 if (data->images != data->image_buf)
2728 g_free (data->images);
2730 data->images_len = new_len;
2734 add_image (MonoImage *image, CollectData *data)
2738 /* The arrays are small, so use a linear search instead of a hash table */
2739 for (i = 0; i < data->nimages; ++i)
2740 if (data->images [i] == image)
2743 if (data->nimages == data->images_len)
2744 enlarge_data (data);
2746 data->images [data->nimages ++] = image;
2750 collect_type_images (MonoType *type, CollectData *data);
2753 collect_ginst_images (MonoGenericInst *ginst, CollectData *data)
2757 for (i = 0; i < ginst->type_argc; ++i) {
2758 collect_type_images (ginst->type_argv [i], data);
2763 collect_gclass_images (MonoGenericClass *gclass, CollectData *data)
2765 add_image (gclass->container_class->image, data);
2766 if (gclass->context.class_inst)
2767 collect_ginst_images (gclass->context.class_inst, data);
2771 collect_signature_images (MonoMethodSignature *sig, CollectData *data)
2773 gpointer iter = NULL;
2776 collect_type_images (mono_signature_get_return_type (sig), data);
2777 while ((p = mono_signature_get_params (sig, &iter)) != NULL)
2778 collect_type_images (p, data);
2782 collect_inflated_signature_images (MonoInflatedMethodSignature *sig, CollectData *data)
2784 collect_signature_images (sig->sig, data);
2785 if (sig->context.class_inst)
2786 collect_ginst_images (sig->context.class_inst, data);
2787 if (sig->context.method_inst)
2788 collect_ginst_images (sig->context.method_inst, data);
2792 collect_method_images (MonoMethodInflated *method, CollectData *data)
2794 MonoMethod *m = method->declaring;
2796 add_image (method->declaring->klass->image, data);
2797 if (method->context.class_inst)
2798 collect_ginst_images (method->context.class_inst, data);
2799 if (method->context.method_inst)
2800 collect_ginst_images (method->context.method_inst, data);
2802 * Dynamic assemblies have no references, so the images they depend on can be unloaded before them.
2804 if (image_is_dynamic (m->klass->image))
2805 collect_signature_images (mono_method_signature (m), data);
2809 collect_type_images (MonoType *type, CollectData *data)
2812 switch (type->type) {
2813 case MONO_TYPE_GENERICINST:
2814 collect_gclass_images (type->data.generic_class, data);
2817 type = type->data.type;
2819 case MONO_TYPE_SZARRAY:
2820 type = &type->data.klass->byval_arg;
2822 case MONO_TYPE_ARRAY:
2823 type = &type->data.array->eklass->byval_arg;
2825 case MONO_TYPE_FNPTR:
2826 //return signature_in_image (type->data.method, image);
2827 g_assert_not_reached ();
2829 case MONO_TYPE_MVAR:
2831 MonoImage *image = get_image_for_generic_param (type->data.generic_param);
2832 add_image (image, data);
2835 case MONO_TYPE_CLASS:
2836 case MONO_TYPE_VALUETYPE:
2837 add_image (mono_class_from_mono_type (type)->image, data);
2840 add_image (mono_defaults.corlib, data);
2847 } CleanForImageUserData;
2850 steal_gclass_in_image (gpointer key, gpointer value, gpointer data)
2852 MonoGenericClass *gclass = (MonoGenericClass *)key;
2853 CleanForImageUserData *user_data = (CleanForImageUserData *)data;
2855 g_assert (gclass_in_image (gclass, user_data->image));
2857 user_data->list = g_slist_prepend (user_data->list, gclass);
2862 steal_ginst_in_image (gpointer key, gpointer value, gpointer data)
2864 MonoGenericInst *ginst = (MonoGenericInst *)key;
2865 CleanForImageUserData *user_data = (CleanForImageUserData *)data;
2867 // This doesn't work during corlib compilation
2868 //g_assert (ginst_in_image (ginst, user_data->image));
2870 user_data->list = g_slist_prepend (user_data->list, ginst);
2875 inflated_method_in_image (gpointer key, gpointer value, gpointer data)
2877 MonoImage *image = (MonoImage *)data;
2878 MonoMethodInflated *method = (MonoMethodInflated *)key;
2881 // https://bugzilla.novell.com/show_bug.cgi?id=458168
2882 g_assert (method->declaring->klass->image == image ||
2883 (method->context.class_inst && ginst_in_image (method->context.class_inst, image)) ||
2884 (method->context.method_inst && ginst_in_image (method->context.method_inst, image)) || (((MonoMethod*)method)->signature && signature_in_image (mono_method_signature ((MonoMethod*)method), image)));
2890 inflated_signature_in_image (gpointer key, gpointer value, gpointer data)
2892 MonoImage *image = (MonoImage *)data;
2893 MonoInflatedMethodSignature *sig = (MonoInflatedMethodSignature *)key;
2895 return signature_in_image (sig->sig, image) ||
2896 (sig->context.class_inst && ginst_in_image (sig->context.class_inst, image)) ||
2897 (sig->context.method_inst && ginst_in_image (sig->context.method_inst, image));
2901 check_gmethod (gpointer key, gpointer value, gpointer data)
2903 MonoMethodInflated *method = (MonoMethodInflated *)key;
2904 MonoImage *image = (MonoImage *)data;
2906 if (method->context.class_inst)
2907 g_assert (!ginst_in_image (method->context.class_inst, image));
2908 if (method->context.method_inst)
2909 g_assert (!ginst_in_image (method->context.method_inst, image));
2910 if (((MonoMethod*)method)->signature)
2911 g_assert (!signature_in_image (mono_method_signature ((MonoMethod*)method), image));
2917 * Run a consistency check on the image set data structures.
2919 static G_GNUC_UNUSED void
2920 check_image_sets (MonoImage *image)
2923 GSList *l = image->image_sets;
2928 for (i = 0; i < image_sets->len; ++i) {
2929 MonoImageSet *set = (MonoImageSet *)g_ptr_array_index (image_sets, i);
2931 if (!g_slist_find (l, set)) {
2932 g_hash_table_foreach (set->gmethod_cache, check_gmethod, image);
2938 mono_metadata_clean_for_image (MonoImage *image)
2940 CleanForImageUserData ginst_data, gclass_data;
2941 GSList *l, *set_list;
2943 //check_image_sets (image);
2946 * The data structures could reference each other so we delete them in two phases.
2947 * This is required because of the hashing functions in gclass/ginst_cache.
2949 ginst_data.image = gclass_data.image = image;
2950 ginst_data.list = gclass_data.list = NULL;
2952 /* Collect the items to delete */
2953 /* delete_image_set () modifies the lists so make a copy */
2954 for (l = image->image_sets; l; l = l->next) {
2955 MonoImageSet *set = (MonoImageSet *)l->data;
2957 mono_image_set_lock (set);
2958 mono_conc_hashtable_foreach_steal (set->gclass_cache, steal_gclass_in_image, &gclass_data);
2959 g_hash_table_foreach_steal (set->ginst_cache, steal_ginst_in_image, &ginst_data);
2960 g_hash_table_foreach_remove (set->gmethod_cache, inflated_method_in_image, image);
2961 g_hash_table_foreach_remove (set->gsignature_cache, inflated_signature_in_image, image);
2962 mono_image_set_unlock (set);
2965 /* Delete the removed items */
2966 for (l = ginst_data.list; l; l = l->next)
2967 free_generic_inst ((MonoGenericInst *)l->data);
2968 for (l = gclass_data.list; l; l = l->next)
2969 free_generic_class ((MonoGenericClass *)l->data);
2970 g_slist_free (ginst_data.list);
2971 g_slist_free (gclass_data.list);
2972 /* delete_image_set () modifies the lists so make a copy */
2973 set_list = g_slist_copy (image->image_sets);
2974 for (l = set_list; l; l = l->next) {
2975 MonoImageSet *set = (MonoImageSet *)l->data;
2977 delete_image_set (set);
2979 g_slist_free (set_list);
2983 free_inflated_method (MonoMethodInflated *imethod)
2985 MonoMethod *method = (MonoMethod*)imethod;
2987 if (method->signature)
2988 mono_metadata_free_inflated_signature (method->signature);
2990 if (method->wrapper_type)
2991 g_free (((MonoMethodWrapper*)method)->method_data);
2997 free_generic_inst (MonoGenericInst *ginst)
3001 /* The ginst itself is allocated from the image set mempool */
3002 for (i = 0; i < ginst->type_argc; ++i)
3003 mono_metadata_free_type (ginst->type_argv [i]);
3007 free_generic_class (MonoGenericClass *gclass)
3009 /* The gclass itself is allocated from the image set mempool */
3010 if (gclass->cached_class && gclass->cached_class->interface_id)
3011 mono_unload_interface_id (gclass->cached_class);
3015 free_inflated_signature (MonoInflatedMethodSignature *sig)
3017 mono_metadata_free_inflated_signature (sig->sig);
3022 * mono_metadata_get_inflated_signature:
3024 * Given an inflated signature and a generic context, return a canonical copy of the
3025 * signature. The returned signature might be equal to SIG or it might be a cached copy.
3027 MonoMethodSignature *
3028 mono_metadata_get_inflated_signature (MonoMethodSignature *sig, MonoGenericContext *context)
3030 MonoInflatedMethodSignature helper;
3031 MonoInflatedMethodSignature *res;
3036 helper.context.class_inst = context->class_inst;
3037 helper.context.method_inst = context->method_inst;
3039 collect_data_init (&data);
3041 collect_inflated_signature_images (&helper, &data);
3043 set = get_image_set (data.images, data.nimages);
3045 collect_data_free (&data);
3047 mono_image_set_lock (set);
3049 res = (MonoInflatedMethodSignature *)g_hash_table_lookup (set->gsignature_cache, &helper);
3051 res = g_new0 (MonoInflatedMethodSignature, 1);
3053 res->context.class_inst = context->class_inst;
3054 res->context.method_inst = context->method_inst;
3055 g_hash_table_insert (set->gsignature_cache, res, res);
3058 mono_image_set_unlock (set);
3064 mono_metadata_get_image_set_for_method (MonoMethodInflated *method)
3067 CollectData image_set_data;
3069 collect_data_init (&image_set_data);
3070 collect_method_images (method, &image_set_data);
3071 set = get_image_set (image_set_data.images, image_set_data.nimages);
3072 collect_data_free (&image_set_data);
3078 type_is_gtd (MonoType *type)
3080 switch (type->type) {
3081 case MONO_TYPE_CLASS:
3082 case MONO_TYPE_VALUETYPE:
3083 return mono_class_is_gtd (type->data.klass);
3090 * mono_metadata_get_generic_inst:
3092 * Given a list of types, return a MonoGenericInst that represents that list.
3093 * The returned MonoGenericInst has its own copy of the list of types. The list
3094 * passed in the argument can be freed, modified or disposed of.
3098 mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv)
3100 MonoGenericInst *ginst;
3103 int size = MONO_SIZEOF_GENERIC_INST + type_argc * sizeof (MonoType *);
3105 for (i = 0; i < type_argc; ++i)
3106 if (mono_class_is_open_constructed_type (type_argv [i]))
3108 is_open = (i < type_argc);
3110 ginst = (MonoGenericInst *)g_alloca (size);
3111 memset (ginst, 0, sizeof (MonoGenericInst));
3112 ginst->is_open = is_open;
3113 ginst->type_argc = type_argc;
3114 memcpy (ginst->type_argv, type_argv, type_argc * sizeof (MonoType *));
3116 for (i = 0; i < type_argc; ++i) {
3117 MonoType *t = ginst->type_argv [i];
3118 if (type_is_gtd (t)) {
3119 ginst->type_argv [i] = mono_class_gtd_get_canonical_inst (t->data.klass);
3123 return mono_metadata_get_canonical_generic_inst (ginst);
3128 * mono_metadata_get_canonical_generic_inst:
3129 * \param candidate an arbitrary generic instantiation
3131 * \returns the canonical generic instantiation that represents the given
3132 * candidate by identifying the image set for the candidate instantiation and
3133 * finding the instance in the image set or adding a copy of the given instance
3136 * The returned MonoGenericInst has its own copy of the list of types. The list
3137 * passed in the argument can be freed, modified or disposed of.
3141 mono_metadata_get_canonical_generic_inst (MonoGenericInst *candidate)
3144 int type_argc = candidate->type_argc;
3145 gboolean is_open = candidate->is_open;
3148 collect_data_init (&data);
3150 collect_ginst_images (candidate, &data);
3152 set = get_image_set (data.images, data.nimages);
3154 collect_data_free (&data);
3156 mono_image_set_lock (set);
3158 MonoGenericInst *ginst = (MonoGenericInst *)g_hash_table_lookup (set->ginst_cache, candidate);
3160 int size = MONO_SIZEOF_GENERIC_INST + type_argc * sizeof (MonoType *);
3161 ginst = (MonoGenericInst *)mono_image_set_alloc0 (set, size);
3162 #ifndef MONO_SMALL_CONFIG
3163 ginst->id = InterlockedIncrement (&next_generic_inst_id);
3165 ginst->is_open = is_open;
3166 ginst->type_argc = type_argc;
3168 for (int i = 0; i < type_argc; ++i)
3169 ginst->type_argv [i] = mono_metadata_type_dup (NULL, candidate->type_argv [i]);
3171 g_hash_table_insert (set->ginst_cache, ginst, ginst);
3174 mono_image_set_unlock (set);
3179 mono_metadata_is_type_builder_generic_type_definition (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
3181 MonoGenericContainer *container = mono_class_get_generic_container (container_class);
3183 if (!is_dynamic || container_class->wastypebuilder || container->type_argc != inst->type_argc)
3185 return inst == container->context.class_inst;
3189 * mono_metadata_lookup_generic_class:
3191 * Returns a MonoGenericClass with the given properties.
3195 mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
3197 MonoGenericClass *gclass;
3198 MonoGenericClass helper;
3199 gboolean is_tb_open = mono_metadata_is_type_builder_generic_type_definition (container_class, inst, is_dynamic);
3203 g_assert (mono_class_get_generic_container (container_class)->type_argc == inst->type_argc);
3205 memset (&helper, 0, sizeof(helper)); // act like g_new0
3206 helper.container_class = container_class;
3207 helper.context.class_inst = inst;
3208 helper.is_dynamic = is_dynamic; /* We use this in a hash lookup, which does not attempt to downcast the pointer */
3209 helper.is_tb_open = is_tb_open;
3211 collect_data_init (&data);
3213 collect_gclass_images (&helper, &data);
3215 set = get_image_set (data.images, data.nimages);
3217 collect_data_free (&data);
3219 gclass = (MonoGenericClass *)mono_conc_hashtable_lookup (set->gclass_cache, &helper);
3221 /* A tripwire just to keep us honest */
3222 g_assert (!helper.cached_class);
3227 gclass = mono_image_set_new0 (set, MonoGenericClass, 1);
3229 gclass->is_dynamic = 1;
3231 gclass->is_tb_open = is_tb_open;
3232 gclass->container_class = container_class;
3233 gclass->context.class_inst = inst;
3234 gclass->context.method_inst = NULL;
3235 gclass->owner = set;
3236 if (inst == mono_class_get_generic_container (container_class)->context.class_inst && !is_tb_open)
3237 gclass->cached_class = container_class;
3239 mono_image_set_lock (set);
3241 MonoGenericClass *gclass2 = mono_conc_hashtable_insert (set->gclass_cache, gclass, gclass);
3245 // g_hash_table_insert (set->gclass_cache, gclass, gclass);
3247 mono_image_set_unlock (set);
3253 * mono_metadata_inflate_generic_inst:
3255 * Instantiate the generic instance @ginst with the context @context.
3256 * Check @error for success.
3260 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context, MonoError *error)
3262 MonoType **type_argv;
3263 MonoGenericInst *nginst = NULL;
3268 if (!ginst->is_open)
3271 type_argv = g_new0 (MonoType*, ginst->type_argc);
3273 for (i = 0; i < ginst->type_argc; i++) {
3274 type_argv [i] = mono_class_inflate_generic_type_checked (ginst->type_argv [i], context, error);
3275 if (!mono_error_ok (error))
3280 nginst = mono_metadata_get_generic_inst (ginst->type_argc, type_argv);
3283 for (i = 0; i < count; i++)
3284 mono_metadata_free_type (type_argv [i]);
3291 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
3292 int count, const char *ptr, const char **rptr, MonoError *error)
3294 MonoType **type_argv;
3295 MonoGenericInst *ginst;
3299 type_argv = g_new0 (MonoType*, count);
3301 for (i = 0; i < count; i++) {
3302 MonoType *t = mono_metadata_parse_type_checked (m, container, 0, FALSE, ptr, &ptr, error);
3313 ginst = mono_metadata_get_generic_inst (count, type_argv);
3321 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
3322 const char *ptr, const char **rptr, MonoError *error)
3324 MonoGenericInst *inst;
3331 // XXX how about transient?
3332 gtype = mono_metadata_parse_type_checked (m, NULL, 0, FALSE, ptr, &ptr, error);
3336 gklass = mono_class_from_mono_type (gtype);
3337 if (!mono_class_is_gtd (gklass)) {
3338 mono_error_set_bad_image (error, m, "Generic instance with non-generic definition");
3342 count = mono_metadata_decode_value (ptr, &ptr);
3343 inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr, error);
3350 type->data.generic_class = mono_metadata_lookup_generic_class (gklass, inst, FALSE);
3356 * @gc: The generic container to normalize
3357 * @type: The kind of generic parameters the resulting generic-container should contain
3360 static MonoGenericContainer *
3361 select_container (MonoGenericContainer *gc, MonoTypeEnum type)
3363 gboolean is_var = (type == MONO_TYPE_VAR);
3367 g_assert (is_var || type == MONO_TYPE_MVAR);
3370 if (gc->is_method || gc->parent)
3372 * The current MonoGenericContainer is a generic method -> its `parent'
3373 * points to the containing class'es container.
3381 MonoGenericContainer *
3382 get_anonymous_container_for_image (MonoImage *image, gboolean is_mvar)
3384 MonoGenericContainer **container_pointer;
3386 container_pointer = &image->anonymous_generic_method_container;
3388 container_pointer = &image->anonymous_generic_class_container;
3389 MonoGenericContainer *result = *container_pointer;
3391 // This container has never been created; make it now.
3394 // Note this is never deallocated anywhere-- it exists for the lifetime of the image it's allocated from
3395 result = (MonoGenericContainer *)mono_image_alloc0 (image, sizeof (MonoGenericContainer));
3396 result->owner.image = image;
3397 result->is_anonymous = TRUE;
3398 result->is_small_param = TRUE;
3399 result->is_method = is_mvar;
3401 // If another thread already made a container, use that and leak this new one.
3402 // (Technically it would currently be safe to just assign instead of CASing.)
3403 MonoGenericContainer *exchange = (MonoGenericContainer *)InterlockedCompareExchangePointer ((volatile gpointer *)container_pointer, result, NULL);
3411 * mono_metadata_parse_generic_param:
3412 * @generic_container: Our MonoClass's or MonoMethod's MonoGenericContainer;
3413 * see mono_metadata_parse_type_checked() for details.
3414 * Internal routine to parse a generic type parameter.
3415 * LOCKING: Acquires the loader lock
3417 static MonoGenericParam *
3418 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
3419 MonoTypeEnum type, const char *ptr, const char **rptr, MonoError *error)
3421 int index = mono_metadata_decode_value (ptr, &ptr);
3427 generic_container = select_container (generic_container, type);
3428 if (!generic_container) {
3429 gboolean is_mvar = FALSE;
3434 case MONO_TYPE_MVAR:
3438 g_error ("Cerating generic param object with invalid MonoType"); // This is not a generic param
3441 /* Create dummy MonoGenericParam */
3442 MonoGenericParam *param;
3444 param = (MonoGenericParam *)mono_image_alloc0 (m, sizeof (MonoGenericParam));
3446 param->owner = get_anonymous_container_for_image (m, is_mvar);
3451 if (index >= generic_container->type_argc) {
3452 mono_error_set_bad_image (error, m, "Invalid generic %s parameter index %d, max index is %d",
3453 generic_container->is_method ? "method" : "type",
3454 index, generic_container->type_argc);
3458 //This can't return NULL
3459 return mono_generic_container_get_param (generic_container, index);
3463 * mono_metadata_get_shared_type:
3465 * Return a shared instance of TYPE, if available, NULL otherwise.
3466 * Shared MonoType instances help save memory. Their contents should not be modified
3467 * by the caller. They do not need to be freed as their lifetime is bound by either
3468 * the lifetime of the runtime (builtin types), or the lifetime of the MonoClass
3469 * instance they are embedded in. If they are freed, they should be freed using
3470 * mono_metadata_free_type () instead of g_free ().
3473 mono_metadata_get_shared_type (MonoType *type)
3477 /* No need to use locking since nobody is modifying the hash table */
3478 if ((cached = (MonoType *)g_hash_table_lookup (type_cache, type)))
3481 switch (type->type){
3482 case MONO_TYPE_CLASS:
3483 case MONO_TYPE_VALUETYPE:
3484 if (type == &type->data.klass->byval_arg)
3486 if (type == &type->data.klass->this_arg)
3497 compare_type_literals (MonoImage *image, int class_type, int type_type, MonoError *error)
3501 /* byval_arg.type can be zero if we're decoding a type that references a class been loading.
3502 * See mcs/test/gtest-440. and #650936.
3503 * FIXME This better be moved to the metadata verifier as it can catch more cases.
3507 /* NET 1.1 assemblies might encode string and object in a denormalized way.
3510 if (class_type == type_type)
3513 if (type_type == MONO_TYPE_CLASS) {
3514 if (class_type == MONO_TYPE_STRING || class_type == MONO_TYPE_OBJECT)
3516 //XXX stringify this argument
3517 mono_error_set_bad_image (error, image, "Expected reference type but got type kind %d", class_type);
3521 g_assert (type_type == MONO_TYPE_VALUETYPE);
3522 switch (class_type) {
3523 case MONO_TYPE_BOOLEAN:
3524 case MONO_TYPE_CHAR:
3537 case MONO_TYPE_CLASS:
3540 //XXX stringify this argument
3541 mono_error_set_bad_image (error, image, "Expected value type but got type kind %d", class_type);
3547 verify_var_type_and_container (MonoImage *image, int var_type, MonoGenericContainer *container, MonoError *error)
3550 if (var_type == MONO_TYPE_MVAR) {
3551 if (!container->is_method) { //MVAR and a method container
3552 mono_error_set_bad_image (error, image, "MVAR parsed in a context without a method container");
3556 if (!(!container->is_method || //VAR and class container
3557 (container->is_method && container->parent))) { //VAR and method container with parent
3558 mono_error_set_bad_image (error, image, "VAR parsed in a context without a class container");
3566 * do_mono_metadata_parse_type:
3567 * @type: MonoType to be filled in with the return value
3569 * @generic_context: generics_context
3570 * @transient: whenever to allocate data from the heap
3571 * @ptr: pointer to the encoded type
3572 * @rptr: pointer where the end of the encoded type is saved
3574 * Internal routine used to "fill" the contents of @type from an
3575 * allocated pointer. This is done this way to avoid doing too
3576 * many mini-allocations (particularly for the MonoFieldType which
3577 * most of the time is just a MonoType, but sometimes might be augmented).
3579 * This routine is used by mono_metadata_parse_type and
3580 * mono_metadata_parse_field_type
3582 * This extracts a Type as specified in Partition II (22.2.12)
3584 * Returns: FALSE if the type could not be loaded
3587 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
3588 gboolean transient, const char *ptr, const char **rptr, MonoError *error)
3592 type->type = (MonoTypeEnum)mono_metadata_decode_value (ptr, &ptr);
3594 switch (type->type){
3595 case MONO_TYPE_VOID:
3596 case MONO_TYPE_BOOLEAN:
3597 case MONO_TYPE_CHAR:
3610 case MONO_TYPE_STRING:
3611 case MONO_TYPE_OBJECT:
3612 case MONO_TYPE_TYPEDBYREF:
3614 case MONO_TYPE_VALUETYPE:
3615 case MONO_TYPE_CLASS: {
3618 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
3619 klass = mono_class_get_checked (m, token, error);
3620 type->data.klass = klass;
3624 if (!compare_type_literals (m, klass->byval_arg.type, type->type, error))
3629 case MONO_TYPE_SZARRAY: {
3630 MonoType *etype = mono_metadata_parse_type_checked (m, container, 0, transient, ptr, &ptr, error);
3634 type->data.klass = mono_class_from_mono_type (etype);
3637 mono_metadata_free_type (etype);
3639 g_assert (type->data.klass); //This was previously a check for NULL, but mcfmt should never fail. It can return a borken MonoClass, but should return at least something.
3642 case MONO_TYPE_PTR: {
3643 type->data.type = mono_metadata_parse_type_checked (m, container, 0, transient, ptr, &ptr, error);
3644 if (!type->data.type)
3648 case MONO_TYPE_FNPTR: {
3649 type->data.method = mono_metadata_parse_method_signature_full (m, container, 0, ptr, &ptr, error);
3650 if (!type->data.method)
3654 case MONO_TYPE_ARRAY: {
3655 type->data.array = mono_metadata_parse_array_internal (m, container, transient, ptr, &ptr, error);
3656 if (!type->data.array)
3660 case MONO_TYPE_MVAR:
3661 case MONO_TYPE_VAR: {
3662 if (container && !verify_var_type_and_container (m, type->type, container, error))
3665 type->data.generic_param = mono_metadata_parse_generic_param (m, container, type->type, ptr, &ptr, error);
3666 if (!type->data.generic_param)
3671 case MONO_TYPE_GENERICINST: {
3672 if (!do_mono_metadata_parse_generic_class (type, m, container, ptr, &ptr, error))
3677 mono_error_set_bad_image (error, m, "type 0x%02x not handled in do_mono_metadata_parse_type on image %s", type->type, m->name);
3687 * mono_metadata_free_type:
3688 * \param type type to free
3690 * Free the memory allocated for type \p type which is allocated on the heap.
3693 mono_metadata_free_type (MonoType *type)
3695 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
3698 switch (type->type){
3699 case MONO_TYPE_OBJECT:
3700 case MONO_TYPE_STRING:
3701 if (!type->data.klass)
3704 case MONO_TYPE_CLASS:
3705 case MONO_TYPE_VALUETYPE:
3706 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
3710 mono_metadata_free_type (type->data.type);
3712 case MONO_TYPE_FNPTR:
3713 mono_metadata_free_method_signature (type->data.method);
3715 case MONO_TYPE_ARRAY:
3716 mono_metadata_free_array (type->data.array);
3727 hex_dump (const char *buffer, int base, int count)
3729 int show_header = 1;
3737 for (i = 0; i < count; i++){
3740 printf ("\n0x%08x: ", (unsigned char) base + i);
3742 printf ("%02x ", (unsigned char) (buffer [i]));
3749 * @ptr: Points to the beginning of the Section Data (25.3)
3751 static MonoExceptionClause*
3752 parse_section_data (MonoImage *m, int *num_clauses, const unsigned char *ptr, MonoError *error)
3754 unsigned char sect_data_flags;
3756 guint32 sect_data_len;
3757 MonoExceptionClause* clauses = NULL;
3762 /* align on 32-bit boundary */
3763 ptr = dword_align (ptr);
3764 sect_data_flags = *ptr;
3767 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
3769 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
3772 sect_data_len = ptr [0];
3776 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
3777 const unsigned char *p = dword_align (ptr);
3779 *num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
3780 /* we could just store a pointer if we don't need to byteswap */
3781 clauses = (MonoExceptionClause *)g_malloc0 (sizeof (MonoExceptionClause) * (*num_clauses));
3782 for (i = 0; i < *num_clauses; ++i) {
3783 MonoExceptionClause *ec = &clauses [i];
3786 ec->flags = read32 (p);
3787 ec->try_offset = read32 (p + 4);
3788 ec->try_len = read32 (p + 8);
3789 ec->handler_offset = read32 (p + 12);
3790 ec->handler_len = read32 (p + 16);
3791 tof_value = read32 (p + 20);
3794 ec->flags = read16 (p);
3795 ec->try_offset = read16 (p + 2);
3796 ec->try_len = *(p + 4);
3797 ec->handler_offset = read16 (p + 5);
3798 ec->handler_len = *(p + 7);
3799 tof_value = read32 (p + 8);
3802 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3803 ec->data.filter_offset = tof_value;
3804 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
3805 ec->data.catch_class = NULL;
3807 ec->data.catch_class = mono_class_get_checked (m, tof_value, error);
3808 if (!is_ok (error)) {
3814 ec->data.catch_class = NULL;
3816 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
3820 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
3821 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
3828 * mono_method_get_header_summary:
3829 * @method: The method to get the header.
3830 * @summary: Where to store the header
3833 * Returns: TRUE if the header was properly decoded.
3836 mono_method_get_header_summary (MonoMethod *method, MonoMethodHeaderSummary *summary)
3842 unsigned char flags, format;
3845 /*Only the GMD has a pointer to the metadata.*/
3846 while (method->is_inflated)
3847 method = ((MonoMethodInflated*)method)->declaring;
3849 summary->code_size = 0;
3850 summary->has_clauses = FALSE;
3852 /*FIXME extract this into a MACRO and share it with mono_method_get_header*/
3853 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))
3856 if (method->wrapper_type != MONO_WRAPPER_NONE || method->sre_method) {
3857 MonoMethodHeader *header = ((MonoMethodWrapper *)method)->header;
3860 summary->code_size = header->code_size;
3861 summary->has_clauses = header->num_clauses > 0;
3866 idx = mono_metadata_token_index (method->token);
3867 img = method->klass->image;
3868 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
3870 /*We must run the verifier since we'll be decoding it.*/
3871 if (!mono_verifier_verify_method_header (img, rva, NULL))
3874 ptr = mono_image_rva_map (img, rva);
3878 flags = *(const unsigned char *)ptr;
3879 format = flags & METHOD_HEADER_FORMAT_MASK;
3882 case METHOD_HEADER_TINY_FORMAT:
3884 summary->code_size = flags >> 2;
3886 case METHOD_HEADER_FAT_FORMAT:
3887 fat_flags = read16 (ptr);
3889 summary->code_size = read32 (ptr);
3890 if (fat_flags & METHOD_HEADER_MORE_SECTS)
3891 summary->has_clauses = TRUE;
3900 * mono_metadata_parse_mh_full:
3901 * @m: metadata context
3902 * @generic_context: generics context
3903 * @ptr: pointer to the method header.
3905 * Decode the method header at @ptr, including pointer to the IL code,
3906 * info about local variables and optional exception tables.
3907 * This is a Mono runtime internal function.
3909 * LOCKING: Acquires the loader lock.
3911 * Returns: a transient MonoMethodHeader allocated from the heap.
3914 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, const char *ptr, MonoError *error)
3916 MonoMethodHeader *mh = NULL;
3917 unsigned char flags = *(const unsigned char *) ptr;
3918 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
3920 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
3921 const unsigned char *code;
3922 MonoExceptionClause* clauses = NULL;
3923 int num_clauses = 0;
3924 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
3925 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
3930 mono_error_set_bad_image (error, m, "Method header with null pointer");
3935 case METHOD_HEADER_TINY_FORMAT:
3936 mh = (MonoMethodHeader *)g_malloc0 (MONO_SIZEOF_METHOD_HEADER);
3939 mh->is_transient = TRUE;
3940 local_var_sig_tok = 0;
3941 mh->code_size = flags >> 2;
3942 mh->code = (unsigned char*)ptr;
3944 case METHOD_HEADER_FAT_FORMAT:
3945 fat_flags = read16 (ptr);
3947 max_stack = read16 (ptr);
3949 code_size = read32 (ptr);
3951 local_var_sig_tok = read32 (ptr);
3954 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
3959 code = (unsigned char*)ptr;
3961 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
3965 * There are more sections
3967 ptr = (char*)code + code_size;
3970 mono_error_set_bad_image (error, m, "Invalid method header format %d", format);
3974 if (local_var_sig_tok) {
3975 int idx = (local_var_sig_tok & 0xffffff)-1;
3976 if (idx >= t->rows || idx < 0) {
3977 mono_error_set_bad_image (error, m, "Invalid method header local vars signature token 0x%8x", idx);
3980 mono_metadata_decode_row (t, idx, cols, 1);
3982 if (!mono_verifier_verify_standalone_signature (m, cols [MONO_STAND_ALONE_SIGNATURE], NULL)) {
3983 mono_error_set_bad_image (error, m, "Method header locals signature 0x%8x verification failed", idx);
3987 if (fat_flags & METHOD_HEADER_MORE_SECTS) {
3988 clauses = parse_section_data (m, &num_clauses, (const unsigned char*)ptr, error);
3992 if (local_var_sig_tok) {
3993 const char *locals_ptr;
3996 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
3997 mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
3998 if (*locals_ptr != 0x07)
3999 g_warning ("wrong signature for locals blob");
4001 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
4002 mh = (MonoMethodHeader *)g_malloc0 (MONO_SIZEOF_METHOD_HEADER + len * sizeof (MonoType*) + num_clauses * sizeof (MonoExceptionClause));
4003 mh->num_locals = len;
4004 for (i = 0; i < len; ++i) {
4005 mh->locals [i] = mono_metadata_parse_type_internal (m, container, 0, TRUE, locals_ptr, &locals_ptr, error);
4010 mh = (MonoMethodHeader *)g_malloc0 (MONO_SIZEOF_METHOD_HEADER + num_clauses * sizeof (MonoExceptionClause));
4013 mh->code_size = code_size;
4014 mh->max_stack = max_stack;
4015 mh->is_transient = TRUE;
4016 mh->init_locals = init_locals;
4018 MonoExceptionClause* clausesp = (MonoExceptionClause*)&mh->locals [mh->num_locals];
4019 memcpy (clausesp, clauses, num_clauses * sizeof (MonoExceptionClause));
4021 mh->clauses = clausesp;
4022 mh->num_clauses = num_clauses;
4033 * mono_metadata_parse_mh:
4034 * \param generic_context generics context
4035 * \param ptr pointer to the method header.
4037 * Decode the method header at \p ptr, including pointer to the IL code,
4038 * info about local variables and optional exception tables.
4040 * \returns a transient \c MonoMethodHeader allocated from the heap.
4043 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
4046 MonoMethodHeader *header = mono_metadata_parse_mh_full (m, NULL, ptr, &error);
4047 mono_error_cleanup (&error);
4052 * mono_metadata_free_mh:
4053 * \param mh a method header
4055 * Free the memory allocated for the method header.
4058 mono_metadata_free_mh (MonoMethodHeader *mh)
4062 /* If it is not transient it means it's part of a wrapper method,
4063 * or a SRE-generated method, so the lifetime in that case is
4064 * dictated by the method's own lifetime
4066 if (mh->is_transient) {
4067 for (i = 0; i < mh->num_locals; ++i)
4068 mono_metadata_free_type (mh->locals [i]);
4074 * mono_method_header_get_code:
4075 * \param header a \c MonoMethodHeader pointer
4076 * \param code_size memory location for returning the code size
4077 * \param max_stack memory location for returning the max stack
4079 * Method header accessor to retreive info about the IL code properties:
4080 * a pointer to the IL code itself, the size of the code and the max number
4081 * of stack slots used by the code.
4083 * \returns pointer to the IL code represented by the method header.
4085 const unsigned char*
4086 mono_method_header_get_code (MonoMethodHeader *header, guint32* code_size, guint32* max_stack)
4089 *code_size = header->code_size;
4091 *max_stack = header->max_stack;
4092 return header->code;
4096 * mono_method_header_get_locals:
4097 * \param header a \c MonoMethodHeader pointer
4098 * \param num_locals memory location for returning the number of local variables
4099 * \param init_locals memory location for returning the init_locals flag
4101 * Method header accessor to retreive info about the local variables:
4102 * an array of local types, the number of locals and whether the locals
4103 * are supposed to be initialized to 0 on method entry
4105 * \returns pointer to an array of types of the local variables
4108 mono_method_header_get_locals (MonoMethodHeader *header, guint32* num_locals, gboolean *init_locals)
4111 *num_locals = header->num_locals;
4113 *init_locals = header->init_locals;
4114 return header->locals;
4118 * mono_method_header_get_num_clauses:
4119 * @header: a MonoMethodHeader pointer
4121 * Method header accessor to retreive the number of exception clauses.
4123 * Returns: the number of exception clauses present
4126 mono_method_header_get_num_clauses (MonoMethodHeader *header)
4128 return header->num_clauses;
4132 * mono_method_header_get_clauses:
4133 * \param header a \c MonoMethodHeader pointer
4134 * \param method \c MonoMethod the header belongs to
4135 * \param iter pointer to a iterator
4136 * \param clause pointer to a \c MonoExceptionClause structure which will be filled with the info
4138 * Get the info about the exception clauses in the method. Set \c *iter to NULL to
4139 * initiate the iteration, then call the method repeatedly until it returns FALSE.
4140 * At each iteration, the structure pointed to by clause if filled with the
4141 * exception clause information.
4143 * \returns TRUE if clause was filled with info, FALSE if there are no more exception
4147 mono_method_header_get_clauses (MonoMethodHeader *header, MonoMethod *method, gpointer *iter, MonoExceptionClause *clause)
4149 MonoExceptionClause *sc;
4150 /* later we'll be able to use this interface to parse the clause info on demand,
4151 * without allocating anything.
4153 if (!iter || !header->num_clauses)
4156 *iter = sc = header->clauses;
4160 sc = (MonoExceptionClause *)*iter;
4162 if (sc < header->clauses + header->num_clauses) {
4171 * mono_metadata_parse_field_type:
4172 * \param m metadata context to extract information from
4173 * \param ptr pointer to the field signature
4174 * \param rptr pointer updated to match the end of the decoded stream
4176 * Parses the field signature, and returns the type information for it.
4178 * \returns The \c MonoType that was extracted from \p ptr .
4181 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
4184 MonoType * type = mono_metadata_parse_type_internal (m, NULL, field_flags, FALSE, ptr, rptr, &error);
4185 mono_error_cleanup (&error);
4190 * mono_metadata_parse_param:
4191 * \param m metadata context to extract information from
4192 * \param ptr pointer to the param signature
4193 * \param rptr pointer updated to match the end of the decoded stream
4195 * Parses the param signature, and returns the type information for it.
4197 * \returns The \c MonoType that was extracted from \p ptr .
4200 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
4203 MonoType * type = mono_metadata_parse_type_internal (m, NULL, 0, FALSE, ptr, rptr, &error);
4204 mono_error_cleanup (&error);
4209 * mono_metadata_token_from_dor:
4210 * \param dor_token A \c TypeDefOrRef coded index
4212 * \p dor_token is a \c TypeDefOrRef coded index: it contains either
4213 * a \c TypeDef, \c TypeRef or \c TypeSpec in the lower bits, and the upper
4214 * bits contain an index into the table.
4216 * \returns an expanded token
4219 mono_metadata_token_from_dor (guint32 dor_index)
4223 table = dor_index & 0x03;
4224 idx = dor_index >> 2;
4227 case 0: /* TypeDef */
4228 return MONO_TOKEN_TYPE_DEF | idx;
4229 case 1: /* TypeRef */
4230 return MONO_TOKEN_TYPE_REF | idx;
4231 case 2: /* TypeSpec */
4232 return MONO_TOKEN_TYPE_SPEC | idx;
4234 g_assert_not_reached ();
4241 * We use this to pass context information to the row locator
4244 int idx; /* The index that we are trying to locate */
4245 int col_idx; /* The index in the row where idx may be stored */
4246 MonoTableInfo *t; /* pointer to the table */
4251 * How the row locator works.
4256 * ___|___------> _______
4259 * A column in the rows of table A references an index in table B.
4260 * For example A may be the TYPEDEF table and B the METHODDEF table.
4262 * Given an index in table B we want to get the row in table A
4263 * where the column n references our index in B.
4265 * In the locator_t structure:
4267 * col_idx is the column number
4268 * index is the index in table B
4269 * result will be the index in table A
4272 * Table A Table B column (in table A)
4273 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
4274 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
4275 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
4276 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
4277 * METHODSEM PROPERTY ASSOCIATION (encoded index)
4279 * Note that we still don't support encoded indexes.
4283 typedef_locator (const void *a, const void *b)
4285 locator_t *loc = (locator_t *) a;
4286 const char *bb = (const char *) b;
4287 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
4288 guint32 col, col_next;
4290 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
4296 * Need to check that the next row is valid.
4298 if (typedef_index + 1 < loc->t->rows) {
4299 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
4300 if (loc->idx >= col_next)
4303 if (col == col_next)
4307 loc->result = typedef_index;
4313 table_locator (const void *a, const void *b)
4315 locator_t *loc = (locator_t *) a;
4316 const char *bb = (const char *) b;
4317 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
4320 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
4322 if (loc->idx == col) {
4323 loc->result = table_index;
4333 declsec_locator (const void *a, const void *b)
4335 locator_t *loc = (locator_t *) a;
4336 const char *bb = (const char *) b;
4337 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
4340 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
4342 if (loc->idx == col) {
4343 loc->result = table_index;
4355 * Return the 1-based row index in TABLE, which must be one of the *Ptr tables,
4356 * which contains IDX.
4359 search_ptr_table (MonoImage *image, int table, int idx)
4361 MonoTableInfo *ptrdef = &image->tables [table];
4364 /* Use a linear search to find our index in the table */
4365 for (i = 0; i < ptrdef->rows; i ++)
4366 /* All the Ptr tables have the same structure */
4367 if (mono_metadata_decode_row_col (ptrdef, i, 0) == idx)
4370 if (i < ptrdef->rows)
4377 * mono_metadata_typedef_from_field:
4378 * \param meta metadata context
4379 * \param index FieldDef token
4381 * \returns the 1-based index into the \c TypeDef table of the type that
4382 * declared the field described by \p index, or 0 if not found.
4385 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
4387 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
4393 loc.idx = mono_metadata_token_index (index);
4394 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
4397 if (meta->uncompressed_metadata)
4398 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
4400 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
4403 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4404 return loc.result + 1;
4408 * mono_metadata_typedef_from_method:
4409 * \param meta metadata context
4410 * \param index \c MethodDef token
4411 * \returns the 1-based index into the \c TypeDef table of the type that
4412 * declared the method described by \p index. 0 if not found.
4415 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
4417 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
4423 loc.idx = mono_metadata_token_index (index);
4424 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
4427 if (meta->uncompressed_metadata)
4428 loc.idx = search_ptr_table (meta, MONO_TABLE_METHOD_POINTER, loc.idx);
4430 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
4433 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4434 return loc.result + 1;
4438 * mono_metadata_interfaces_from_typedef_full:
4439 * @meta: metadata context
4440 * @index: typedef token
4441 * @interfaces: Out parameter used to store the interface array
4442 * @count: Out parameter used to store the number of interfaces
4443 * @heap_alloc_result: if TRUE the result array will be g_malloc'd
4444 * @context: The generic context
4446 * The array of interfaces that the @index typedef token implements is returned in
4447 * @interfaces. The number of elements in the array is returned in @count.
4450 * Returns: TRUE on success, FALSE on failure.
4453 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, MonoClass ***interfaces, guint *count, gboolean heap_alloc_result, MonoGenericContext *context, MonoError *error)
4455 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
4458 guint32 cols [MONO_INTERFACEIMPL_SIZE];
4469 loc.idx = mono_metadata_token_index (index);
4470 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
4473 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4478 * We may end up in the middle of the rows...
4481 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
4487 while (pos < tdef->rows) {
4488 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
4489 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
4494 if (heap_alloc_result)
4495 result = g_new0 (MonoClass*, pos - start);
4497 result = (MonoClass **)mono_image_alloc0 (meta, sizeof (MonoClass*) * (pos - start));
4500 while (pos < tdef->rows) {
4503 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
4504 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
4506 iface = mono_class_get_and_inflate_typespec_checked (
4507 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context, error);
4510 result [pos - start] = iface;
4513 *count = pos - start;
4514 *interfaces = result;
4519 * mono_metadata_interfaces_from_typedef:
4520 * \param meta metadata context
4521 * \param index typedef token
4522 * \param count Out parameter used to store the number of interfaces
4524 * The array of interfaces that the \p index typedef token implements is returned in
4525 * \p interfaces. The number of elements in the array is returned in \p count. The returned
4526 * array is allocated with \c g_malloc and the caller must free it.
4528 * LOCKING: Acquires the loader lock .
4530 * \returns the interface array on success, NULL on failure.
4533 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
4536 MonoClass **interfaces = NULL;
4539 rv = mono_metadata_interfaces_from_typedef_full (meta, index, &interfaces, count, TRUE, NULL, &error);
4540 g_assert (mono_error_ok (&error)); /* FIXME dont swallow the error */
4548 * mono_metadata_nested_in_typedef:
4549 * \param meta metadata context
4550 * \param index typedef token
4551 * \returns the 1-based index into the TypeDef table of the type
4552 * where the type described by \p index is nested.
4553 * Returns 0 if \p index describes a non-nested type.
4556 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
4558 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
4564 loc.idx = mono_metadata_token_index (index);
4565 loc.col_idx = MONO_NESTED_CLASS_NESTED;
4568 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4571 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4572 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
4576 * mono_metadata_nesting_typedef:
4577 * \param meta metadata context
4578 * \param index typedef token
4579 * \returns the 1-based index into the \c TypeDef table of the first type
4580 * that is nested inside the type described by \p index. The search starts at
4581 * \p start_index. Returns 0 if no such type is found.
4584 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
4586 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
4588 guint32 class_index = mono_metadata_token_index (index);
4593 start = start_index;
4595 while (start <= tdef->rows) {
4596 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
4602 if (start > tdef->rows)
4609 * mono_metadata_packing_from_typedef:
4610 * \param meta metadata context
4611 * \param index token representing a type
4612 * \returns the info stored in the \c ClassLayout table for the given typedef token
4613 * into the \p packing and \p size pointers.
4614 * Returns 0 if the info is not found.
4617 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
4619 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
4621 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
4626 loc.idx = mono_metadata_token_index (index);
4627 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
4630 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4633 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
4635 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
4637 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
4639 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4640 return loc.result + 1;
4644 * mono_metadata_custom_attrs_from_index:
4645 * \param meta metadata context
4646 * \param index token representing the parent
4647 * \returns: the 1-based index into the \c CustomAttribute table of the first
4648 * attribute which belongs to the metadata object described by \p index.
4649 * Returns 0 if no such attribute is found.
4652 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
4654 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
4661 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
4664 /* FIXME: Index translation */
4666 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4669 /* Find the first entry by searching backwards */
4670 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
4673 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4674 return loc.result + 1;
4678 * mono_metadata_declsec_from_index:
4679 * \param meta metadata context
4680 * \param index token representing the parent
4681 * \returns the 0-based index into the \c DeclarativeSecurity table of the first
4682 * attribute which belongs to the metadata object described by \p index.
4683 * Returns \c -1 if no such attribute is found.
4686 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
4688 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
4695 loc.col_idx = MONO_DECL_SECURITY_PARENT;
4698 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
4701 /* Find the first entry by searching backwards */
4702 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
4709 * mono_metadata_localscope_from_methoddef:
4710 * @meta: metadata context
4711 * @index: methoddef index
4713 * Returns: the 1-based index into the LocalScope table of the first
4714 * scope which belongs to the method described by @index.
4715 * Returns 0 if no such row is found.
4718 mono_metadata_localscope_from_methoddef (MonoImage *meta, guint32 index)
4720 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_LOCALSCOPE];
4727 loc.col_idx = MONO_LOCALSCOPE_METHOD;
4730 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4733 /* Find the first entry by searching backwards */
4734 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_LOCALSCOPE_METHOD) == index))
4737 return loc.result + 1;
4742 mono_backtrace (int limit)
4747 backtrace (array, limit);
4748 names = backtrace_symbols (array, limit);
4749 for (i =0; i < limit; ++i) {
4750 g_print ("\t%s\n", names [i]);
4756 static int i8_align;
4759 * mono_type_set_alignment:
4761 * Set the alignment used by runtime to layout fields etc. of type TYPE to ALIGN.
4762 * This should only be used in AOT mode since the resulting layout will not match the
4766 mono_type_set_alignment (MonoTypeEnum type, int align)
4768 /* Support only a few types whose alignment is abi dependent */
4774 g_assert_not_reached ();
4781 * \param t the type to return the size of
4782 * \returns The number of bytes required to hold an instance of this
4786 mono_type_size (MonoType *t, int *align)
4788 MonoTypeEnum simple_type;
4795 *align = MONO_ABI_ALIGNOF (gpointer);
4796 return MONO_ABI_SIZEOF (gpointer);
4799 simple_type = t->type;
4801 switch (simple_type) {
4802 case MONO_TYPE_VOID:
4805 case MONO_TYPE_BOOLEAN:
4806 *align = MONO_ABI_ALIGNOF (gint8);
4810 *align = MONO_ABI_ALIGNOF (gint8);
4812 case MONO_TYPE_CHAR:
4815 *align = MONO_ABI_ALIGNOF (gint16);
4819 *align = MONO_ABI_ALIGNOF (gint32);
4822 *align = MONO_ABI_ALIGNOF (float);
4826 *align = MONO_ABI_ALIGNOF (gint64);
4829 *align = MONO_ABI_ALIGNOF (double);
4833 *align = MONO_ABI_ALIGNOF (gpointer);
4834 return MONO_ABI_SIZEOF (gpointer);
4835 case MONO_TYPE_VALUETYPE: {
4836 if (t->data.klass->enumtype)
4837 return mono_type_size (mono_class_enum_basetype (t->data.klass), align);
4839 return mono_class_value_size (t->data.klass, (guint32*)align);
4841 case MONO_TYPE_STRING:
4842 case MONO_TYPE_OBJECT:
4843 case MONO_TYPE_CLASS:
4844 case MONO_TYPE_SZARRAY:
4846 case MONO_TYPE_FNPTR:
4847 case MONO_TYPE_ARRAY:
4848 *align = MONO_ABI_ALIGNOF (gpointer);
4849 return MONO_ABI_SIZEOF (gpointer);
4850 case MONO_TYPE_TYPEDBYREF:
4851 return mono_class_value_size (mono_defaults.typed_reference_class, (guint32*)align);
4852 case MONO_TYPE_GENERICINST: {
4853 MonoGenericClass *gclass = t->data.generic_class;
4854 MonoClass *container_class = gclass->container_class;
4856 // g_assert (!gclass->inst->is_open);
4858 if (container_class->valuetype) {
4859 if (container_class->enumtype)
4860 return mono_type_size (mono_class_enum_basetype (container_class), align);
4862 return mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
4864 *align = MONO_ABI_ALIGNOF (gpointer);
4865 return MONO_ABI_SIZEOF (gpointer);
4869 case MONO_TYPE_MVAR:
4870 if (!t->data.generic_param->gshared_constraint || t->data.generic_param->gshared_constraint->type == MONO_TYPE_VALUETYPE) {
4871 *align = MONO_ABI_ALIGNOF (gpointer);
4872 return MONO_ABI_SIZEOF (gpointer);
4874 /* The gparam can only match types given by gshared_constraint */
4875 return mono_type_size (t->data.generic_param->gshared_constraint, align);
4879 g_error ("mono_type_size: type 0x%02x unknown", t->type);
4885 * mono_type_stack_size:
4886 * \param t the type to return the size it uses on the stack
4887 * \returns The number of bytes required to hold an instance of this
4888 * type on the runtime stack
4891 mono_type_stack_size (MonoType *t, int *align)
4893 return mono_type_stack_size_internal (t, align, FALSE);
4897 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open)
4900 MonoTypeEnum simple_type;
4901 #if SIZEOF_VOID_P == SIZEOF_REGISTER
4902 int stack_slot_size = MONO_ABI_SIZEOF (gpointer);
4903 int stack_slot_align = MONO_ABI_ALIGNOF (gpointer);
4904 #elif SIZEOF_VOID_P < SIZEOF_REGISTER
4905 int stack_slot_size = SIZEOF_REGISTER;
4906 int stack_slot_align = SIZEOF_REGISTER;
4909 g_assert (t != NULL);
4915 *align = stack_slot_align;
4916 return stack_slot_size;
4919 simple_type = t->type;
4920 switch (simple_type) {
4921 case MONO_TYPE_BOOLEAN:
4922 case MONO_TYPE_CHAR:
4931 case MONO_TYPE_STRING:
4932 case MONO_TYPE_OBJECT:
4933 case MONO_TYPE_CLASS:
4934 case MONO_TYPE_SZARRAY:
4936 case MONO_TYPE_FNPTR:
4937 case MONO_TYPE_ARRAY:
4938 *align = stack_slot_align;
4939 return stack_slot_size;
4941 case MONO_TYPE_MVAR:
4942 g_assert (allow_open);
4943 if (!t->data.generic_param->gshared_constraint || t->data.generic_param->gshared_constraint->type == MONO_TYPE_VALUETYPE) {
4944 *align = stack_slot_align;
4945 return stack_slot_size;
4947 /* The gparam can only match types given by gshared_constraint */
4948 return mono_type_stack_size_internal (t->data.generic_param->gshared_constraint, align, allow_open);
4950 case MONO_TYPE_TYPEDBYREF:
4951 *align = stack_slot_align;
4952 return stack_slot_size * 3;
4954 *align = MONO_ABI_ALIGNOF (float);
4955 return sizeof (float);
4958 *align = MONO_ABI_ALIGNOF (gint64);
4959 return sizeof (gint64);
4961 *align = MONO_ABI_ALIGNOF (double);
4962 return sizeof (double);
4963 case MONO_TYPE_VALUETYPE: {
4966 if (t->data.klass->enumtype)
4967 return mono_type_stack_size_internal (mono_class_enum_basetype (t->data.klass), align, allow_open);
4969 size = mono_class_value_size (t->data.klass, (guint32*)align);
4971 *align = *align + stack_slot_align - 1;
4972 *align &= ~(stack_slot_align - 1);
4974 size += stack_slot_size - 1;
4975 size &= ~(stack_slot_size - 1);
4980 case MONO_TYPE_GENERICINST: {
4981 MonoGenericClass *gclass = t->data.generic_class;
4982 MonoClass *container_class = gclass->container_class;
4985 g_assert (!gclass->context.class_inst->is_open);
4987 if (container_class->valuetype) {
4988 if (container_class->enumtype)
4989 return mono_type_stack_size_internal (mono_class_enum_basetype (container_class), align, allow_open);
4991 guint32 size = mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
4993 *align = *align + stack_slot_align - 1;
4994 *align &= ~(stack_slot_align - 1);
4996 size += stack_slot_size - 1;
4997 size &= ~(stack_slot_size - 1);
5002 *align = stack_slot_align;
5003 return stack_slot_size;
5007 g_error ("type 0x%02x unknown", t->type);
5013 mono_type_generic_inst_is_valuetype (MonoType *type)
5015 g_assert (type->type == MONO_TYPE_GENERICINST);
5016 return type->data.generic_class->container_class->valuetype;
5020 * mono_metadata_generic_class_is_valuetype:
5023 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
5025 return gclass->container_class->valuetype;
5029 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
5031 MonoGenericInst *i1 = g1->context.class_inst;
5032 MonoGenericInst *i2 = g2->context.class_inst;
5034 if (g1->is_dynamic != g2->is_dynamic)
5036 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
5038 if (!mono_generic_inst_equal_full (i1, i2, signature_only))
5040 return g1->is_tb_open == g2->is_tb_open;
5044 _mono_metadata_generic_class_container_equal (const MonoGenericClass *g1, MonoClass *c2, gboolean signature_only)
5046 MonoGenericInst *i1 = g1->context.class_inst;
5047 MonoGenericInst *i2 = mono_class_get_generic_container (c2)->context.class_inst;
5049 if (!mono_metadata_class_equal (g1->container_class, c2, signature_only))
5051 if (!mono_generic_inst_equal_full (i1, i2, signature_only))
5053 return !g1->is_tb_open;
5057 mono_metadata_generic_context_hash (const MonoGenericContext *context)
5059 /* FIXME: check if this seed is good enough */
5060 guint hash = 0xc01dfee7;
5061 if (context->class_inst)
5062 hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->class_inst);
5063 if (context->method_inst)
5064 hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->method_inst);
5069 mono_metadata_generic_context_equal (const MonoGenericContext *g1, const MonoGenericContext *g2)
5071 return g1->class_inst == g2->class_inst && g1->method_inst == g2->method_inst;
5075 * mono_metadata_str_hash:
5077 * This should be used instead of g_str_hash for computing hash codes visible
5078 * outside this module, since g_str_hash () is not guaranteed to be stable
5079 * (its not the same in eglib for example).
5082 mono_metadata_str_hash (gconstpointer v1)
5084 /* Same as g_str_hash () in glib */
5085 char *p = (char *) v1;
5090 hash = (hash << 5) - hash + *p;
5097 * mono_metadata_type_hash:
5099 * Computes a hash value for \p t1 to be used in \c GHashTable.
5100 * The returned hash is guaranteed to be the same across executions.
5103 mono_metadata_type_hash (MonoType *t1)
5105 guint hash = t1->type;
5107 hash |= t1->byref << 6; /* do not collide with t1->type values */
5109 case MONO_TYPE_VALUETYPE:
5110 case MONO_TYPE_CLASS:
5111 case MONO_TYPE_SZARRAY: {
5112 MonoClass *klass = t1->data.klass;
5114 * Dynamic classes must not be hashed on their type since it can change
5115 * during runtime. For example, if we hash a reference type that is
5116 * later made into a valuetype.
5118 * This is specially problematic with generic instances since they are
5119 * inserted in a bunch of hash tables before been finished.
5121 if (image_is_dynamic (klass->image))
5122 return (t1->byref << 6) | mono_metadata_str_hash (klass->name);
5123 return ((hash << 5) - hash) ^ mono_metadata_str_hash (klass->name);
5126 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
5127 case MONO_TYPE_ARRAY:
5128 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
5129 case MONO_TYPE_GENERICINST:
5130 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
5132 case MONO_TYPE_MVAR:
5133 return ((hash << 5) - hash) ^ mono_metadata_generic_param_hash (t1->data.generic_param);
5140 mono_metadata_generic_param_hash (MonoGenericParam *p)
5143 MonoGenericParamInfo *info;
5145 hash = (mono_generic_param_num (p) << 2);
5146 if (p->gshared_constraint)
5147 hash = ((hash << 5) - hash) ^ mono_metadata_type_hash (p->gshared_constraint);
5148 info = mono_generic_param_info (p);
5149 /* Can't hash on the owner klass/method, since those might not be set when this is called */
5151 hash = ((hash << 5) - hash) ^ info->token;
5156 mono_metadata_generic_param_equal_internal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
5160 if (mono_generic_param_num (p1) != mono_generic_param_num (p2))
5162 if (p1->gshared_constraint && p2->gshared_constraint) {
5163 if (!mono_metadata_type_equal (p1->gshared_constraint, p2->gshared_constraint))
5166 if (p1->gshared_constraint != p2->gshared_constraint)
5171 * We have to compare the image as well because if we didn't,
5172 * the generic_inst_cache lookup wouldn't care about the image
5173 * of generic params, so what could happen is that a generic
5174 * inst with params from image A is put into the cache, then
5175 * image B gets that generic inst from the cache, image A is
5176 * unloaded, so the inst is deleted, but image B still retains
5179 if (mono_generic_param_owner (p1) == mono_generic_param_owner (p2))
5183 * If `signature_only' is true, we're comparing two (method) signatures.
5184 * In this case, the owner of two type parameters doesn't need to match.
5187 return signature_only;
5191 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2)
5193 return mono_metadata_generic_param_equal_internal (p1, p2, TRUE);
5197 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
5201 if (mono_class_is_ginst (c1) && mono_class_is_ginst (c2))
5202 return _mono_metadata_generic_class_equal (mono_class_get_generic_class (c1), mono_class_get_generic_class (c2), signature_only);
5203 if (mono_class_is_ginst (c1) && mono_class_is_gtd (c2))
5204 return _mono_metadata_generic_class_container_equal (mono_class_get_generic_class (c1), c2, signature_only);
5205 if (mono_class_is_gtd (c1) && mono_class_is_ginst (c2))
5206 return _mono_metadata_generic_class_container_equal (mono_class_get_generic_class (c2), c1, signature_only);
5207 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
5208 return mono_metadata_generic_param_equal_internal (
5209 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
5210 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
5211 return mono_metadata_generic_param_equal_internal (
5212 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
5213 if (signature_only &&
5214 (c1->byval_arg.type == MONO_TYPE_SZARRAY) && (c2->byval_arg.type == MONO_TYPE_SZARRAY))
5215 return mono_metadata_class_equal (c1->byval_arg.data.klass, c2->byval_arg.data.klass, signature_only);
5216 if (signature_only &&
5217 (c1->byval_arg.type == MONO_TYPE_ARRAY) && (c2->byval_arg.type == MONO_TYPE_ARRAY))
5218 return do_mono_metadata_type_equal (&c1->byval_arg, &c2->byval_arg, signature_only);
5223 mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only)
5225 gpointer iter1 = 0, iter2 = 0;
5229 if (s1->call_convention != s2->call_convention)
5231 if (s1->sentinelpos != s2->sentinelpos)
5233 if (s1->hasthis != s2->hasthis)
5235 if (s1->explicit_this != s2->explicit_this)
5237 if (! do_mono_metadata_type_equal (s1->ret, s2->ret, signature_only))
5239 if (s1->param_count != s2->param_count)
5243 MonoType *t1 = mono_signature_get_params (s1, &iter1);
5244 MonoType *t2 = mono_signature_get_params (s2, &iter2);
5246 if (t1 == NULL || t2 == NULL)
5248 if (! do_mono_metadata_type_equal (t1, t2, signature_only))
5254 * mono_metadata_type_equal:
5257 * @signature_only: If true, treat ginsts as equal which are instantiated separately but have equal positional value
5259 * Determine if @t1 and @t2 represent the same type.
5260 * Returns: #TRUE if @t1 and @t2 are equal.
5263 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
5265 if (t1->type != t2->type || t1->byref != t2->byref)
5269 case MONO_TYPE_VOID:
5270 case MONO_TYPE_BOOLEAN:
5271 case MONO_TYPE_CHAR:
5282 case MONO_TYPE_STRING:
5285 case MONO_TYPE_OBJECT:
5286 case MONO_TYPE_TYPEDBYREF:
5288 case MONO_TYPE_VALUETYPE:
5289 case MONO_TYPE_CLASS:
5290 case MONO_TYPE_SZARRAY:
5291 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
5293 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
5294 case MONO_TYPE_ARRAY:
5295 if (t1->data.array->rank != t2->data.array->rank)
5297 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
5298 case MONO_TYPE_GENERICINST:
5299 return _mono_metadata_generic_class_equal (
5300 t1->data.generic_class, t2->data.generic_class, signature_only);
5302 return mono_metadata_generic_param_equal_internal (
5303 t1->data.generic_param, t2->data.generic_param, signature_only);
5304 case MONO_TYPE_MVAR:
5305 return mono_metadata_generic_param_equal_internal (
5306 t1->data.generic_param, t2->data.generic_param, signature_only);
5307 case MONO_TYPE_FNPTR:
5308 return mono_metadata_fnptr_equal (t1->data.method, t2->data.method, signature_only);
5310 g_error ("implement type compare for %0x!", t1->type);
5318 * mono_metadata_type_equal:
5321 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
5323 return do_mono_metadata_type_equal (t1, t2, FALSE);
5327 * mono_metadata_type_equal_full:
5329 * \param t2 another type
5330 * \param signature_only if signature only comparison should be made
5332 * Determine if \p t1 and \p t2 are signature compatible if \p signature_only is TRUE, otherwise
5333 * behaves the same way as mono_metadata_type_equal.
5334 * The function mono_metadata_type_equal(a, b) is just a shortcut for mono_metadata_type_equal_full(a, b, FALSE).
5335 * \returns TRUE if \p t1 and \p t2 are equal taking \p signature_only into account.
5338 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only)
5340 return do_mono_metadata_type_equal (t1, t2, signature_only);
5344 * mono_metadata_signature_equal:
5345 * \param sig1 a signature
5346 * \param sig2 another signature
5348 * Determine if \p sig1 and \p sig2 represent the same signature, with the
5349 * same number of arguments and the same types.
5350 * \returns TRUE if \p sig1 and \p sig2 are equal.
5353 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
5357 if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
5360 if (sig1->generic_param_count != sig2->generic_param_count)
5364 * We're just comparing the signatures of two methods here:
5366 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
5367 * U and V are equal here.
5369 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
5372 for (i = 0; i < sig1->param_count; i++) {
5373 MonoType *p1 = sig1->params[i];
5374 MonoType *p2 = sig2->params[i];
5376 /* if (p1->attrs != p2->attrs)
5379 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
5383 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
5389 * mono_metadata_type_dup:
5390 * \param image image to alloc memory from
5391 * \param original type to duplicate
5392 * \returns copy of type allocated from the image's mempool (or from the heap, if \p image is null).
5395 mono_metadata_type_dup (MonoImage *image, const MonoType *o)
5398 int sizeof_o = MONO_SIZEOF_TYPE;
5400 sizeof_o += o->num_mods * sizeof (MonoCustomMod);
5402 r = image ? (MonoType *)mono_image_alloc0 (image, sizeof_o) : (MonoType *)g_malloc (sizeof_o);
5404 memcpy (r, o, sizeof_o);
5406 if (o->type == MONO_TYPE_PTR) {
5407 r->data.type = mono_metadata_type_dup (image, o->data.type);
5408 } else if (o->type == MONO_TYPE_ARRAY) {
5409 r->data.array = mono_dup_array_type (image, o->data.array);
5410 } else if (o->type == MONO_TYPE_FNPTR) {
5411 /*FIXME the dup'ed signature is leaked mono_metadata_free_type*/
5412 r->data.method = mono_metadata_signature_deep_dup (image, o->data.method);
5418 * mono_signature_hash:
5421 mono_signature_hash (MonoMethodSignature *sig)
5423 guint i, res = sig->ret->type;
5425 for (i = 0; i < sig->param_count; i++)
5426 res = (res << 5) - res + mono_type_hash (sig->params[i]);
5432 * mono_metadata_encode_value:
5433 * @value: value to encode
5434 * @buf: buffer where to write the compressed representation
5435 * @endbuf: pointer updated to point at the end of the encoded output
5437 * Encodes the value @value in the compressed representation used
5438 * in metadata and stores the result in @buf. @buf needs to be big
5439 * enough to hold the data (4 bytes).
5442 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
5448 else if (value < 0x4000) {
5449 p [0] = 0x80 | (value >> 8);
5450 p [1] = value & 0xff;
5453 p [0] = (value >> 24) | 0xc0;
5454 p [1] = (value >> 16) & 0xff;
5455 p [2] = (value >> 8) & 0xff;
5456 p [3] = value & 0xff;
5464 * mono_metadata_field_info:
5465 * \param meta the Image the field is defined in
5466 * \param index the index in the field table representing the field
5467 * \param offset a pointer to an integer where to store the offset that may have been specified for the field in a FieldLayout table
5468 * \param rva a pointer to the RVA of the field data in the image that may have been defined in a \c FieldRVA table
5469 * \param marshal_spec a pointer to the marshal spec that may have been defined for the field in a \c FieldMarshal table.
5471 * Gather info for field \p index that may have been defined in the \c FieldLayout,
5472 * \c FieldRVA and \c FieldMarshal tables.
5473 * Either of \p offset, \p rva and \p marshal_spec can be NULL if you're not interested
5477 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
5478 MonoMarshalSpec **marshal_spec)
5480 mono_metadata_field_info_full (meta, index, offset, rva, marshal_spec, FALSE);
5484 mono_metadata_field_info_with_mempool (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
5485 MonoMarshalSpec **marshal_spec)
5487 mono_metadata_field_info_full (meta, index, offset, rva, marshal_spec, TRUE);
5491 mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
5492 MonoMarshalSpec **marshal_spec, gboolean alloc_from_image)
5494 MonoTableInfo *tdef;
5497 loc.idx = index + 1;
5498 if (meta->uncompressed_metadata)
5499 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
5502 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
5504 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
5507 if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
5508 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
5510 *offset = (guint32)-1;
5514 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
5516 loc.col_idx = MONO_FIELD_RVA_FIELD;
5519 if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
5521 * LAMESPEC: There is no signature, no nothing, just the raw data.
5523 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
5531 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
5532 *marshal_spec = mono_metadata_parse_marshal_spec_full (alloc_from_image ? meta : NULL, meta, p);
5539 * mono_metadata_get_constant_index:
5540 * \param meta the Image the field is defined in
5541 * \param index the token that may have a row defined in the constants table
5542 * \param hint possible position for the row
5544 * \p token must be a \c FieldDef, \c ParamDef or \c PropertyDef token.
5546 * \returns the index into the \c Constants table or 0 if not found.
5549 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
5551 MonoTableInfo *tdef;
5553 guint32 index = mono_metadata_token_index (token);
5555 tdef = &meta->tables [MONO_TABLE_CONSTANT];
5556 index <<= MONO_HASCONSTANT_BITS;
5557 switch (mono_metadata_token_table (token)) {
5558 case MONO_TABLE_FIELD:
5559 index |= MONO_HASCONSTANT_FIEDDEF;
5561 case MONO_TABLE_PARAM:
5562 index |= MONO_HASCONSTANT_PARAM;
5564 case MONO_TABLE_PROPERTY:
5565 index |= MONO_HASCONSTANT_PROPERTY;
5568 g_warning ("Not a valid token for the constant table: 0x%08x", token);
5572 loc.col_idx = MONO_CONSTANT_PARENT;
5575 /* FIXME: Index translation */
5577 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
5580 if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
5581 return loc.result + 1;
5587 * mono_metadata_events_from_typedef:
5588 * \param meta metadata context
5589 * \param index 0-based index (in the \c TypeDef table) describing a type
5590 * \returns the 0-based index in the \c Event table for the events in the
5591 * type. The last event that belongs to the type (plus 1) is stored
5592 * in the \p end_idx pointer.
5595 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
5599 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
5607 loc.col_idx = MONO_EVENT_MAP_PARENT;
5608 loc.idx = index + 1;
5610 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5613 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
5614 if (loc.result + 1 < tdef->rows) {
5615 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
5617 end = meta->tables [MONO_TABLE_EVENT].rows;
5625 * mono_metadata_methods_from_event:
5626 * \param meta metadata context
5627 * \param index 0-based index (in the \c Event table) describing a event
5628 * \returns the 0-based index in the \c MethodDef table for the methods in the
5629 * event. The last method that belongs to the event (plus 1) is stored
5630 * in the \p end_idx pointer.
5633 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
5637 guint32 cols [MONO_METHOD_SEMA_SIZE];
5638 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
5644 if (meta->uncompressed_metadata)
5645 index = search_ptr_table (meta, MONO_TABLE_EVENT_POINTER, index + 1) - 1;
5648 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
5649 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
5651 if (!mono_binary_search (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
5656 * We may end up in the middle of the rows...
5659 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
5665 while (end < msemt->rows) {
5666 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
5667 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
5676 * mono_metadata_properties_from_typedef:
5677 * \param meta metadata context
5678 * \param index 0-based index (in the \c TypeDef table) describing a type
5679 * \returns the 0-based index in the \c Property table for the properties in the
5680 * type. The last property that belongs to the type (plus 1) is stored
5681 * in the \p end_idx pointer.
5684 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
5688 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
5696 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
5697 loc.idx = index + 1;
5699 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5702 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
5703 if (loc.result + 1 < tdef->rows) {
5704 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
5706 end = meta->tables [MONO_TABLE_PROPERTY].rows;
5714 * mono_metadata_methods_from_property:
5715 * \param meta metadata context
5716 * \param index 0-based index (in the \c PropertyDef table) describing a property
5717 * \returns the 0-based index in the \c MethodDef table for the methods in the
5718 * property. The last method that belongs to the property (plus 1) is stored
5719 * in the \p end_idx pointer.
5722 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
5726 guint32 cols [MONO_METHOD_SEMA_SIZE];
5727 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
5733 if (meta->uncompressed_metadata)
5734 index = search_ptr_table (meta, MONO_TABLE_PROPERTY_POINTER, index + 1) - 1;
5737 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
5738 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
5740 if (!mono_binary_search (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
5745 * We may end up in the middle of the rows...
5748 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
5754 while (end < msemt->rows) {
5755 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
5756 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
5765 * mono_metadata_implmap_from_method:
5768 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
5771 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
5776 /* No index translation seems to be needed */
5779 loc.col_idx = MONO_IMPLMAP_MEMBER;
5780 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
5782 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5785 return loc.result + 1;
5789 * mono_type_create_from_typespec:
5790 * \param image context where the image is created
5791 * \param type_spec typespec token
5792 * \deprecated use \c mono_type_create_from_typespec_checked that has proper error handling
5794 * Creates a \c MonoType representing the \c TypeSpec indexed by the \p type_spec
5798 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
5801 MonoType *type = mono_type_create_from_typespec_checked (image, type_spec, &error);
5803 g_error ("Could not create typespec %x due to %s", type_spec, mono_error_get_message (&error));
5808 mono_type_create_from_typespec_checked (MonoImage *image, guint32 type_spec, MonoError *error)
5811 guint32 idx = mono_metadata_token_index (type_spec);
5813 guint32 cols [MONO_TYPESPEC_SIZE];
5815 MonoType *type, *type2;
5819 type = (MonoType *)mono_conc_hashtable_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
5823 t = &image->tables [MONO_TABLE_TYPESPEC];
5825 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
5826 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
5828 if (!mono_verifier_verify_typespec_signature (image, cols [MONO_TYPESPEC_SIGNATURE], type_spec, NULL)) {
5829 mono_error_set_bad_image (error, image, "Could not verify type spec %08x.", type_spec);
5833 mono_metadata_decode_value (ptr, &ptr);
5835 type = mono_metadata_parse_type_checked (image, NULL, 0, TRUE, ptr, &ptr, error);
5839 type2 = mono_metadata_type_dup (image, type);
5840 mono_metadata_free_type (type);
5842 mono_image_lock (image);
5844 /* We might leak some data in the image mempool if found */
5845 type = mono_conc_hashtable_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type2);
5849 mono_image_unlock (image);
5856 mono_image_strndup (MonoImage *image, const char *data, guint len)
5860 return g_strndup (data, len);
5861 res = (char *)mono_image_alloc (image, len + 1);
5862 memcpy (res, data, len);
5868 * mono_metadata_parse_marshal_spec:
5871 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
5873 return mono_metadata_parse_marshal_spec_full (NULL, image, ptr);
5877 * If IMAGE is non-null, memory will be allocated from its mempool, otherwise it will be allocated using malloc.
5878 * PARENT_IMAGE is the image containing the marshal spec.
5881 mono_metadata_parse_marshal_spec_full (MonoImage *image, MonoImage *parent_image, const char *ptr)
5883 MonoMarshalSpec *res;
5885 const char *start = ptr;
5887 /* fixme: this is incomplete, but I cant find more infos in the specs */
5890 res = (MonoMarshalSpec *)mono_image_alloc0 (image, sizeof (MonoMarshalSpec));
5892 res = g_new0 (MonoMarshalSpec, 1);
5894 len = mono_metadata_decode_value (ptr, &ptr);
5895 res->native = (MonoMarshalNative)*ptr++;
5897 if (res->native == MONO_NATIVE_LPARRAY) {
5898 res->data.array_data.param_num = -1;
5899 res->data.array_data.num_elem = -1;
5900 res->data.array_data.elem_mult = -1;
5902 if (ptr - start <= len)
5903 res->data.array_data.elem_type = (MonoMarshalNative)*ptr++;
5904 if (ptr - start <= len)
5905 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
5906 if (ptr - start <= len)
5907 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
5908 if (ptr - start <= len) {
5910 * LAMESPEC: Older spec versions say this parameter comes before
5911 * num_elem. Never spec versions don't talk about elem_mult at
5912 * all, but csc still emits it, and it is used to distinguish
5913 * between param_num being 0, and param_num being omitted.
5914 * So if (param_num == 0) && (num_elem > 0), then
5915 * elem_mult == 0 -> the array size is num_elem
5916 * elem_mult == 1 -> the array size is @param_num + num_elem
5918 res->data.array_data.elem_mult = mono_metadata_decode_value (ptr, &ptr);
5922 if (res->native == MONO_NATIVE_BYVALTSTR) {
5923 if (ptr - start <= len)
5924 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
5927 if (res->native == MONO_NATIVE_BYVALARRAY) {
5928 if (ptr - start <= len)
5929 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
5932 if (res->native == MONO_NATIVE_CUSTOM) {
5933 /* skip unused type guid */
5934 len = mono_metadata_decode_value (ptr, &ptr);
5936 /* skip unused native type name */
5937 len = mono_metadata_decode_value (ptr, &ptr);
5939 /* read custom marshaler type name */
5940 len = mono_metadata_decode_value (ptr, &ptr);
5941 res->data.custom_data.custom_name = mono_image_strndup (image, ptr, len);
5943 /* read cookie string */
5944 len = mono_metadata_decode_value (ptr, &ptr);
5945 res->data.custom_data.cookie = mono_image_strndup (image, ptr, len);
5946 res->data.custom_data.image = parent_image;
5949 if (res->native == MONO_NATIVE_SAFEARRAY) {
5950 res->data.safearray_data.elem_type = (MonoMarshalVariant)0;
5951 res->data.safearray_data.num_elem = 0;
5952 if (ptr - start <= len)
5953 res->data.safearray_data.elem_type = (MonoMarshalVariant)*ptr++;
5954 if (ptr - start <= len)
5955 res->data.safearray_data.num_elem = *ptr++;
5961 * mono_metadata_free_marshal_spec:
5964 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
5966 if (spec->native == MONO_NATIVE_CUSTOM) {
5967 g_free (spec->data.custom_data.custom_name);
5968 g_free (spec->data.custom_data.cookie);
5974 * mono_type_to_unmanaged:
5975 * The value pointed to by \p conv will contain the kind of marshalling required for this
5976 * particular type one of the \c MONO_MARSHAL_CONV_ enumeration values.
5977 * \returns A \c MonoMarshalNative enumeration value (<code>MONO_NATIVE_</code>) value
5978 * describing the underlying native reprensetation of the type.
5981 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
5982 gboolean unicode, MonoMarshalConv *conv)
5984 MonoMarshalConv dummy_conv;
5990 *conv = MONO_MARSHAL_CONV_NONE;
5993 return MONO_NATIVE_UINT;
5997 case MONO_TYPE_BOOLEAN:
5999 switch (mspec->native) {
6000 case MONO_NATIVE_VARIANTBOOL:
6001 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
6002 return MONO_NATIVE_VARIANTBOOL;
6003 case MONO_NATIVE_BOOLEAN:
6004 *conv = MONO_MARSHAL_CONV_BOOL_I4;
6005 return MONO_NATIVE_BOOLEAN;
6006 case MONO_NATIVE_I1:
6007 case MONO_NATIVE_U1:
6008 return mspec->native;
6010 g_error ("cant marshal bool to native type %02x", mspec->native);
6013 *conv = MONO_MARSHAL_CONV_BOOL_I4;
6014 return MONO_NATIVE_BOOLEAN;
6015 case MONO_TYPE_CHAR:
6017 switch (mspec->native) {
6018 case MONO_NATIVE_U2:
6019 case MONO_NATIVE_U1:
6020 return mspec->native;
6022 g_error ("cant marshal char to native type %02x", mspec->native);
6025 return unicode ? MONO_NATIVE_U2 : MONO_NATIVE_U1;
6026 case MONO_TYPE_I1: return MONO_NATIVE_I1;
6027 case MONO_TYPE_U1: return MONO_NATIVE_U1;
6028 case MONO_TYPE_I2: return MONO_NATIVE_I2;
6029 case MONO_TYPE_U2: return MONO_NATIVE_U2;
6030 case MONO_TYPE_I4: return MONO_NATIVE_I4;
6031 case MONO_TYPE_U4: return MONO_NATIVE_U4;
6032 case MONO_TYPE_I8: return MONO_NATIVE_I8;
6033 case MONO_TYPE_U8: return MONO_NATIVE_U8;
6034 case MONO_TYPE_R4: return MONO_NATIVE_R4;
6035 case MONO_TYPE_R8: return MONO_NATIVE_R8;
6036 case MONO_TYPE_STRING:
6038 switch (mspec->native) {
6039 case MONO_NATIVE_BSTR:
6040 *conv = MONO_MARSHAL_CONV_STR_BSTR;
6041 return MONO_NATIVE_BSTR;
6042 case MONO_NATIVE_LPSTR:
6043 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
6044 return MONO_NATIVE_LPSTR;
6045 case MONO_NATIVE_LPWSTR:
6046 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
6047 return MONO_NATIVE_LPWSTR;
6048 case MONO_NATIVE_LPTSTR:
6049 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
6050 return MONO_NATIVE_LPTSTR;
6051 case MONO_NATIVE_ANSIBSTR:
6052 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
6053 return MONO_NATIVE_ANSIBSTR;
6054 case MONO_NATIVE_TBSTR:
6055 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
6056 return MONO_NATIVE_TBSTR;
6057 case MONO_NATIVE_UTF8STR:
6058 *conv = MONO_MARSHAL_CONV_STR_UTF8STR;
6059 return MONO_NATIVE_UTF8STR;
6060 case MONO_NATIVE_BYVALTSTR:
6062 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
6064 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
6065 return MONO_NATIVE_BYVALTSTR;
6067 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);
6071 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
6072 return MONO_NATIVE_LPWSTR;
6075 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
6076 return MONO_NATIVE_LPSTR;
6078 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
6079 case MONO_TYPE_VALUETYPE: /*FIXME*/
6080 if (type->data.klass->enumtype) {
6081 t = mono_class_enum_basetype (type->data.klass)->type;
6084 if (type->data.klass == mono_defaults.handleref_class){
6085 *conv = MONO_MARSHAL_CONV_HANDLEREF;
6086 return MONO_NATIVE_INT;
6088 return MONO_NATIVE_STRUCT;
6089 case MONO_TYPE_SZARRAY:
6090 case MONO_TYPE_ARRAY:
6092 switch (mspec->native) {
6093 case MONO_NATIVE_BYVALARRAY:
6094 if ((type->data.klass->element_class == mono_defaults.char_class) && !unicode)
6095 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY;
6097 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
6098 return MONO_NATIVE_BYVALARRAY;
6099 case MONO_NATIVE_SAFEARRAY:
6100 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
6101 return MONO_NATIVE_SAFEARRAY;
6102 case MONO_NATIVE_LPARRAY:
6103 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
6104 return MONO_NATIVE_LPARRAY;
6106 g_error ("cant marshal array as native type %02x", mspec->native);
6110 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
6111 return MONO_NATIVE_LPARRAY;
6112 case MONO_TYPE_I: return MONO_NATIVE_INT;
6113 case MONO_TYPE_U: return MONO_NATIVE_UINT;
6114 case MONO_TYPE_CLASS:
6115 case MONO_TYPE_OBJECT: {
6116 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
6118 switch (mspec->native) {
6119 case MONO_NATIVE_STRUCT:
6120 return MONO_NATIVE_STRUCT;
6121 case MONO_NATIVE_CUSTOM:
6122 return MONO_NATIVE_CUSTOM;
6123 case MONO_NATIVE_INTERFACE:
6124 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
6125 return MONO_NATIVE_INTERFACE;
6126 case MONO_NATIVE_IDISPATCH:
6127 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
6128 return MONO_NATIVE_IDISPATCH;
6129 case MONO_NATIVE_IUNKNOWN:
6130 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
6131 return MONO_NATIVE_IUNKNOWN;
6132 case MONO_NATIVE_FUNC:
6133 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
6134 type->data.klass == mono_defaults.delegate_class ||
6135 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
6136 *conv = MONO_MARSHAL_CONV_DEL_FTN;
6137 return MONO_NATIVE_FUNC;
6141 g_error ("cant marshal object as native type %02x", mspec->native);
6144 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
6145 type->data.klass == mono_defaults.delegate_class ||
6146 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
6147 *conv = MONO_MARSHAL_CONV_DEL_FTN;
6148 return MONO_NATIVE_FUNC;
6150 if (mono_class_try_get_safehandle_class () && type->data.klass == mono_class_try_get_safehandle_class ()){
6151 *conv = MONO_MARSHAL_CONV_SAFEHANDLE;
6152 return MONO_NATIVE_INT;
6154 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
6155 return MONO_NATIVE_STRUCT;
6157 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
6158 case MONO_TYPE_GENERICINST:
6159 type = &type->data.generic_class->container_class->byval_arg;
6162 case MONO_TYPE_TYPEDBYREF:
6164 g_error ("type 0x%02x not handled in marshal", t);
6166 return MONO_NATIVE_MAX;
6170 * mono_metadata_get_marshal_info:
6173 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
6176 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
6182 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
6183 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
6185 /* FIXME: Index translation */
6187 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
6190 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
6194 method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context, MonoError *error)
6196 MonoMethod *result = NULL;
6197 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
6201 switch (tok & MONO_METHODDEFORREF_MASK) {
6202 case MONO_METHODDEFORREF_METHODDEF:
6203 result = mono_get_method_checked (m, MONO_TOKEN_METHOD_DEF | idx, NULL, context, error);
6205 case MONO_METHODDEFORREF_METHODREF:
6206 result = mono_get_method_checked (m, MONO_TOKEN_MEMBER_REF | idx, NULL, context, error);
6209 mono_error_set_bad_image (error, m, "Invalid MethodDefOfRef token %x", tok);
6216 * mono_class_get_overrides_full:
6218 * Return the method overrides belonging to class @type_token in @overrides, and
6219 * the number of overrides in @num_overrides.
6221 * Returns: TRUE on success, FALSE on failure.
6224 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
6225 MonoGenericContext *generic_context)
6229 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
6232 guint32 cols [MONO_METHODIMPL_SIZE];
6233 MonoMethod **result;
6244 loc.col_idx = MONO_METHODIMPL_CLASS;
6245 loc.idx = mono_metadata_token_index (type_token);
6247 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
6253 * We may end up in the middle of the rows...
6256 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
6261 while (end < tdef->rows) {
6262 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
6268 result = g_new (MonoMethod*, num * 2);
6269 for (i = 0; i < num; ++i) {
6272 if (!mono_verifier_verify_methodimpl_row (image, start + i, &error)) {
6273 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6278 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
6279 method = method_from_method_def_or_ref (
6280 image, cols [MONO_METHODIMPL_DECLARATION], generic_context, &error);
6281 if (method == NULL) {
6282 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6285 result [i * 2] = method;
6286 method = method_from_method_def_or_ref (
6287 image, cols [MONO_METHODIMPL_BODY], generic_context, &error);
6288 if (method == NULL) {
6289 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6292 result [i * 2 + 1] = method;
6295 *overrides = result;
6297 *num_overrides = num;
6302 * mono_guid_to_string:
6304 * Converts a 16 byte Microsoft GUID to the standard string representation.
6307 mono_guid_to_string (const guint8 *guid)
6309 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
6310 guid[3], guid[2], guid[1], guid[0],
6314 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
6318 * mono_guid_to_string_minimal:
6320 * Converts a 16 byte Microsoft GUID to lower case no '-' representation..
6323 mono_guid_to_string_minimal (const guint8 *guid)
6325 return g_strdup_printf ("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
6326 guid[3], guid[2], guid[1], guid[0],
6330 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
6333 get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContainer *container, MonoError *error)
6335 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
6336 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
6337 guint32 i, token, found;
6338 MonoClass *klass, **res;
6339 GSList *cons = NULL, *tmp;
6340 MonoGenericContext *context = &container->context;
6344 *constraints = NULL;
6346 for (i = 0; i < tdef->rows; ++i) {
6347 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
6348 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
6349 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
6350 klass = mono_class_get_and_inflate_typespec_checked (image, token, context, error);
6352 g_slist_free (cons);
6355 cons = g_slist_append (cons, klass);
6358 /* contiguous list finished */
6365 res = (MonoClass **)mono_image_alloc0 (image, sizeof (MonoClass*) * (found + 1));
6366 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
6367 res [i] = (MonoClass *)tmp->data;
6369 g_slist_free (cons);
6375 * mono_metadata_get_generic_param_row:
6378 * @token: TypeOrMethodDef token, owner for GenericParam
6379 * @owner: coded token, set on return
6381 * Returns: 1-based row-id in the GenericParam table whose
6382 * owner is @token. 0 if not found.
6385 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner)
6387 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
6394 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
6395 *owner = MONO_TYPEORMETHOD_TYPE;
6396 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
6397 *owner = MONO_TYPEORMETHOD_METHOD;
6399 g_error ("wrong token %x to get_generic_param_row", token);
6402 *owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
6405 loc.col_idx = MONO_GENERICPARAM_OWNER;
6408 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
6411 /* Find the first entry by searching backwards */
6412 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_GENERICPARAM_OWNER) == loc.idx))
6415 return loc.result + 1;
6419 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
6422 return mono_metadata_get_generic_param_row (image, token, &owner);
6426 * Memory is allocated from IMAGE's mempool.
6429 mono_metadata_load_generic_param_constraints_checked (MonoImage *image, guint32 token,
6430 MonoGenericContainer *container, MonoError *error)
6433 guint32 start_row, i, owner;
6436 if (! (start_row = mono_metadata_get_generic_param_row (image, token, &owner)))
6438 for (i = 0; i < container->type_argc; i++) {
6439 if (!get_constraints (image, start_row + i, &mono_generic_container_get_param_info (container, i)->constraints, container, error)) {
6447 * mono_metadata_load_generic_params:
6449 * Load the type parameters from the type or method definition @token.
6451 * Use this method after parsing a type or method definition to figure out whether it's a generic
6452 * type / method. When parsing a method definition, @parent_container points to the generic container
6453 * of the current class, if any.
6455 * Note: This method does not load the constraints: for typedefs, this has to be done after fully
6456 * creating the type.
6458 * Returns: NULL if @token is not a generic type or method definition or the new generic container.
6460 * LOCKING: Acquires the loader lock
6463 MonoGenericContainer *
6464 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
6466 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
6467 guint32 cols [MONO_GENERICPARAM_SIZE];
6468 guint32 i, owner = 0, n;
6469 MonoGenericContainer *container;
6470 MonoGenericParamFull *params;
6471 MonoGenericContext *context;
6473 if (!(i = mono_metadata_get_generic_param_row (image, token, &owner)))
6475 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
6478 container = (MonoGenericContainer *)mono_image_alloc0 (image, sizeof (MonoGenericContainer));
6479 container->owner.image = image; // Temporarily mark as anonymous, but this will be overriden by caller
6480 container->is_anonymous = TRUE;
6483 params = (MonoGenericParamFull *)g_realloc (params, sizeof (MonoGenericParamFull) * n);
6484 memset (¶ms [n - 1], 0, sizeof (MonoGenericParamFull));
6485 params [n - 1].param.owner = container;
6486 params [n - 1].param.num = cols [MONO_GENERICPARAM_NUMBER];
6487 params [n - 1].info.token = i | MONO_TOKEN_GENERIC_PARAM;
6488 params [n - 1].info.flags = cols [MONO_GENERICPARAM_FLAGS];
6489 params [n - 1].info.name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
6490 if (params [n - 1].param.num != n - 1)
6491 g_warning ("GenericParam table unsorted or hole in generic param sequence: token %d", i);
6492 if (++i > tdef->rows)
6494 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
6495 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
6497 container->type_argc = n;
6498 container->type_params = (MonoGenericParamFull *)mono_image_alloc0 (image, sizeof (MonoGenericParamFull) * n);
6499 memcpy (container->type_params, params, sizeof (MonoGenericParamFull) * n);
6501 container->parent = parent_container;
6503 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
6504 container->is_method = 1;
6506 g_assert (container->parent == NULL || container->is_method);
6508 context = &container->context;
6509 if (container->is_method) {
6510 context->class_inst = container->parent ? container->parent->context.class_inst : NULL;
6511 context->method_inst = mono_get_shared_generic_inst (container);
6513 context->class_inst = mono_get_shared_generic_inst (container);
6520 mono_get_shared_generic_inst (MonoGenericContainer *container)
6522 MonoType **type_argv;
6524 MonoGenericInst *nginst;
6527 type_argv = g_new0 (MonoType *, container->type_argc);
6528 helper = g_new0 (MonoType, container->type_argc);
6530 for (i = 0; i < container->type_argc; i++) {
6531 MonoType *t = &helper [i];
6533 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
6534 t->data.generic_param = mono_generic_container_get_param (container, i);
6539 nginst = mono_metadata_get_generic_inst (container->type_argc, type_argv);
6548 * mono_type_is_byref:
6549 * \param type the \c MonoType operated on
6550 * \returns TRUE if \p type represents a type passed by reference,
6554 mono_type_is_byref (MonoType *type)
6560 * mono_type_get_type:
6561 * \param type the \c MonoType operated on
6562 * \returns the IL type value for \p type. This is one of the \c MonoTypeEnum
6563 * enum members like \c MONO_TYPE_I4 or \c MONO_TYPE_STRING.
6566 mono_type_get_type (MonoType *type)
6572 * mono_type_get_signature:
6573 * \param type the \c MonoType operated on
6574 * It is only valid to call this function if \p type is a \c MONO_TYPE_FNPTR .
6575 * \returns the \c MonoMethodSignature pointer that describes the signature
6576 * of the function pointer \p type represents.
6578 MonoMethodSignature*
6579 mono_type_get_signature (MonoType *type)
6581 g_assert (type->type == MONO_TYPE_FNPTR);
6582 return type->data.method;
6586 * mono_type_get_class:
6587 * \param type the \c MonoType operated on
6588 * It is only valid to call this function if \p type is a \c MONO_TYPE_CLASS or a
6589 * \c MONO_TYPE_VALUETYPE . For more general functionality, use \c mono_class_from_mono_type,
6591 * \returns the \c MonoClass pointer that describes the class that \p type represents.
6594 mono_type_get_class (MonoType *type)
6596 /* FIXME: review the runtime users before adding the assert here */
6597 return type->data.klass;
6601 * mono_type_get_array_type:
6602 * \param type the \c MonoType operated on
6603 * It is only valid to call this function if \p type is a \c MONO_TYPE_ARRAY .
6604 * \returns a \c MonoArrayType struct describing the array type that \p type
6605 * represents. The info includes details such as rank, array element type
6606 * and the sizes and bounds of multidimensional arrays.
6609 mono_type_get_array_type (MonoType *type)
6611 return type->data.array;
6615 * mono_type_get_ptr_type:
6616 * \pararm type the \c MonoType operated on
6617 * It is only valid to call this function if \p type is a \c MONO_TYPE_PTR .
6618 * \returns the \c MonoType pointer that describes the type that \p type
6619 * represents a pointer to.
6622 mono_type_get_ptr_type (MonoType *type)
6624 g_assert (type->type == MONO_TYPE_PTR);
6625 return type->data.type;
6629 * mono_type_get_modifiers:
6632 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
6634 /* FIXME: implement */
6639 * mono_type_is_struct:
6640 * \param type the \c MonoType operated on
6641 * \returns TRUE if \p type is a struct, that is a \c ValueType but not an enum
6642 * or a basic type like \c System.Int32 . FALSE otherwise.
6645 mono_type_is_struct (MonoType *type)
6647 return (!type->byref && ((type->type == MONO_TYPE_VALUETYPE &&
6648 !type->data.klass->enumtype) || (type->type == MONO_TYPE_TYPEDBYREF) ||
6649 ((type->type == MONO_TYPE_GENERICINST) &&
6650 mono_metadata_generic_class_is_valuetype (type->data.generic_class) &&
6651 !type->data.generic_class->container_class->enumtype)));
6655 * mono_type_is_void:
6656 * \param type the \c MonoType operated on
6657 * \returns TRUE if \p type is \c System.Void . FALSE otherwise.
6660 mono_type_is_void (MonoType *type)
6662 return (type && (type->type == MONO_TYPE_VOID) && !type->byref);
6666 * mono_type_is_pointer:
6667 * \param type the \c MonoType operated on
6668 * \returns TRUE if \p type is a managed or unmanaged pointer type. FALSE otherwise.
6671 mono_type_is_pointer (MonoType *type)
6673 return (type && ((type->byref || (type->type == MONO_TYPE_I) || type->type == MONO_TYPE_STRING)
6674 || (type->type == MONO_TYPE_SZARRAY) || (type->type == MONO_TYPE_CLASS) ||
6675 (type->type == MONO_TYPE_U) || (type->type == MONO_TYPE_OBJECT) ||
6676 (type->type == MONO_TYPE_ARRAY) || (type->type == MONO_TYPE_PTR) ||
6677 (type->type == MONO_TYPE_FNPTR)));
6681 * mono_type_is_reference:
6682 * \param type the \c MonoType operated on
6683 * \returns TRUE if \p type represents an object reference. FALSE otherwise.
6686 mono_type_is_reference (MonoType *type)
6688 return (type && (((type->type == MONO_TYPE_STRING) ||
6689 (type->type == MONO_TYPE_SZARRAY) || (type->type == MONO_TYPE_CLASS) ||
6690 (type->type == MONO_TYPE_OBJECT) || (type->type == MONO_TYPE_ARRAY)) ||
6691 ((type->type == MONO_TYPE_GENERICINST) &&
6692 !mono_metadata_generic_class_is_valuetype (type->data.generic_class))));
6696 mono_type_is_generic_parameter (MonoType *type)
6698 return !type->byref && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR);
6702 * mono_signature_get_return_type:
6703 * \param sig the method signature inspected
6704 * \returns the return type of the method signature \p sig
6707 mono_signature_get_return_type (MonoMethodSignature *sig)
6713 * mono_signature_get_params:
6714 * \param sig the method signature inspected
6715 * \param iter pointer to an iterator
6716 * Iterates over the parameters for the method signature \p sig.
6717 * A \c void* pointer must be initialized to NULL to start the iteration
6718 * and its address is passed to this function repeteadly until it returns
6720 * \returns the next parameter type of the method signature \p sig,
6721 * NULL when finished.
6724 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
6730 /* start from the first */
6731 if (sig->param_count) {
6732 *iter = &sig->params [0];
6733 return sig->params [0];
6739 type = (MonoType **)*iter;
6741 if (type < &sig->params [sig->param_count]) {
6749 * mono_signature_get_param_count:
6750 * \param sig the method signature inspected
6751 * \returns the number of parameters in the method signature \p sig.
6754 mono_signature_get_param_count (MonoMethodSignature *sig)
6756 return sig->param_count;
6760 * mono_signature_get_call_conv:
6761 * \param sig the method signature inspected
6762 * \returns the call convention of the method signature \p sig.
6765 mono_signature_get_call_conv (MonoMethodSignature *sig)
6767 return sig->call_convention;
6771 * mono_signature_vararg_start:
6772 * \param sig the method signature inspected
6773 * \returns the number of the first vararg parameter in the
6774 * method signature \param sig. \c -1 if this is not a vararg signature.
6777 mono_signature_vararg_start (MonoMethodSignature *sig)
6779 return sig->sentinelpos;
6783 * mono_signature_is_instance:
6784 * \param sig the method signature inspected
6785 * \returns TRUE if this the method signature \p sig has an implicit
6786 * first instance argument. FALSE otherwise.
6789 mono_signature_is_instance (MonoMethodSignature *sig)
6791 return sig->hasthis;
6795 * mono_signature_param_is_out
6796 * \param sig the method signature inspected
6797 * \param param_num the 0-based index of the inspected parameter
6798 * \returns TRUE if the parameter is an out parameter, FALSE
6802 mono_signature_param_is_out (MonoMethodSignature *sig, int param_num)
6804 g_assert (param_num >= 0 && param_num < sig->param_count);
6805 return (sig->params [param_num]->attrs & PARAM_ATTRIBUTE_OUT) != 0;
6809 * mono_signature_explicit_this:
6810 * \param sig the method signature inspected
6811 * \returns TRUE if this the method signature \p sig has an explicit
6812 * instance argument. FALSE otherwise.
6815 mono_signature_explicit_this (MonoMethodSignature *sig)
6817 return sig->explicit_this;
6820 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
6822 mono_aligned_addr_hash (gconstpointer ptr)
6824 /* Same hashing we use for objects */
6825 return (GPOINTER_TO_UINT (ptr) >> 3) * 2654435761u;
6829 * If @field belongs to an inflated generic class, return the corresponding field of the
6830 * generic type definition class.
6833 mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field)
6838 if (!mono_class_is_ginst (field->parent))
6841 gtd = mono_class_get_generic_class (field->parent)->container_class;
6842 offset = field - field->parent->fields;
6843 return gtd->fields + offset;
6847 * If @event belongs to an inflated generic class, return the corresponding event of the
6848 * generic type definition class.
6851 mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event)
6856 if (!mono_class_is_ginst (event->parent))
6859 gtd = mono_class_get_generic_class (event->parent)->container_class;
6860 offset = event - mono_class_get_event_info (event->parent)->events;
6861 return mono_class_get_event_info (gtd)->events + offset;
6865 * If @property belongs to an inflated generic class, return the corresponding property of the
6866 * generic type definition class.
6869 mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property)
6871 MonoClassPropertyInfo *info;
6875 if (!mono_class_is_ginst (property->parent))
6878 info = mono_class_get_property_info (property->parent);
6879 gtd = mono_class_get_generic_class (property->parent)->container_class;
6880 offset = property - info->properties;
6881 return mono_class_get_property_info (gtd)->properties + offset;
6885 mono_method_get_wrapper_cache (MonoMethod *method)
6887 if (method->is_inflated) {
6888 MonoMethodInflated *imethod = (MonoMethodInflated *)method;
6889 return &imethod->owner->wrapper_caches;
6891 return &method->klass->image->wrapper_caches;
6895 // This is support for the mempool reference tracking feature in checked-build, but lives in metadata.c due to use of static variables of this file.
6898 * mono_find_image_set_owner:
6900 * Find the imageset, if any, which a given pointer is located in the memory of.
6903 mono_find_image_set_owner (void *ptr)
6905 MonoImageSet *owner = NULL;
6912 for (i = 0; !owner && i < image_sets->len; ++i) {
6913 MonoImageSet *set = (MonoImageSet *)g_ptr_array_index (image_sets, i);
6914 if (mono_mempool_contains_addr (set->mempool, ptr))
6919 image_sets_unlock ();
6925 mono_loader_set_strict_strong_names (gboolean enabled)
6927 check_strong_names_strictly = enabled;
6931 mono_loader_get_strict_strong_names (void)
6933 return check_strong_names_strictly;