2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
19 #include "tabledefs.h"
20 #include "mono-endian.h"
22 #include "tokentype.h"
23 #include "class-internals.h"
24 #include "metadata-internals.h"
25 #include "verify-internals.h"
28 #include "debug-helpers.h"
29 #include "abi-details.h"
30 #include <mono/utils/mono-error-internals.h>
31 #include <mono/utils/bsearch.h>
32 #include <mono/utils/atomic.h>
34 /* Auxiliary structure used for caching inflated signatures */
36 MonoMethodSignature *sig;
37 MonoGenericContext context;
38 } MonoInflatedMethodSignature;
40 static gboolean do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container, gboolean transient,
41 const char *ptr, const char **rptr, MonoError *error);
43 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
44 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
45 static gboolean mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only);
46 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
47 gboolean signature_only);
48 static void free_generic_inst (MonoGenericInst *ginst);
49 static void free_generic_class (MonoGenericClass *ginst);
50 static void free_inflated_method (MonoMethodInflated *method);
51 static void free_inflated_signature (MonoInflatedMethodSignature *sig);
52 static void mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva, MonoMarshalSpec **marshal_spec, gboolean alloc_from_image);
55 * This enumeration is used to describe the data types in the metadata
66 /* Index into Blob heap */
69 /* Index into String heap */
75 /* Pointer into a table */
78 /* HasConstant:Parent pointer (Param, Field or Property) */
81 /* HasCustomAttribute index. Indexes any table except CustomAttribute */
84 /* CustomAttributeType encoded index */
87 /* HasDeclSecurity index: TypeDef Method or Assembly */
90 /* Implementation coded index: File, Export AssemblyRef */
93 /* HasFieldMarshal coded index: Field or Param table */
96 /* MemberForwardedIndex: Field or Method */
99 /* TypeDefOrRef coded index: typedef, typeref, typespec */
102 /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
105 /* MethodDefOrRef coded index: Method or Member Ref table */
108 /* HasSemantic coded index: Event or Property */
111 /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
114 /* CustomDebugInformation parent encoded index */
115 MONO_MT_HASCUSTDEBUG_IDX
118 const static unsigned char TableSchemas [] = {
119 #define ASSEMBLY_SCHEMA_OFFSET 0
120 MONO_MT_UINT32, /* "HashId" }, */
121 MONO_MT_UINT16, /* "Major" }, */
122 MONO_MT_UINT16, /* "Minor" }, */
123 MONO_MT_UINT16, /* "BuildNumber" }, */
124 MONO_MT_UINT16, /* "RevisionNumber" }, */
125 MONO_MT_UINT32, /* "Flags" }, */
126 MONO_MT_BLOB_IDX, /* "PublicKey" }, */
127 MONO_MT_STRING_IDX, /* "Name" }, */
128 MONO_MT_STRING_IDX, /* "Culture" }, */
131 #define ASSEMBLYOS_SCHEMA_OFFSET ASSEMBLY_SCHEMA_OFFSET + 10
132 MONO_MT_UINT32, /* "OSPlatformID" }, */
133 MONO_MT_UINT32, /* "OSMajor" }, */
134 MONO_MT_UINT32, /* "OSMinor" }, */
137 #define ASSEMBLYPROC_SCHEMA_OFFSET ASSEMBLYOS_SCHEMA_OFFSET + 4
138 MONO_MT_UINT32, /* "Processor" }, */
141 #define ASSEMBLYREF_SCHEMA_OFFSET ASSEMBLYPROC_SCHEMA_OFFSET + 2
142 MONO_MT_UINT16, /* "Major" }, */
143 MONO_MT_UINT16, /* "Minor" }, */
144 MONO_MT_UINT16, /* "Build" }, */
145 MONO_MT_UINT16, /* "Revision" }, */
146 MONO_MT_UINT32, /* "Flags" }, */
147 MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
148 MONO_MT_STRING_IDX, /* "Name" }, */
149 MONO_MT_STRING_IDX, /* "Culture" }, */
150 MONO_MT_BLOB_IDX, /* "HashValue" }, */
153 #define ASSEMBLYREFOS_SCHEMA_OFFSET ASSEMBLYREF_SCHEMA_OFFSET + 10
154 MONO_MT_UINT32, /* "OSPlatformID" }, */
155 MONO_MT_UINT32, /* "OSMajorVersion" }, */
156 MONO_MT_UINT32, /* "OSMinorVersion" }, */
157 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
160 #define ASSEMBLYREFPROC_SCHEMA_OFFSET ASSEMBLYREFOS_SCHEMA_OFFSET + 5
161 MONO_MT_UINT32, /* "Processor" }, */
162 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
165 #define CLASS_LAYOUT_SCHEMA_OFFSET ASSEMBLYREFPROC_SCHEMA_OFFSET + 3
166 MONO_MT_UINT16, /* "PackingSize" }, */
167 MONO_MT_UINT32, /* "ClassSize" }, */
168 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
171 #define CONSTANT_SCHEMA_OFFSET CLASS_LAYOUT_SCHEMA_OFFSET + 4
172 MONO_MT_UINT8, /* "Type" }, */
173 MONO_MT_UINT8, /* "PaddingZero" }, */
174 MONO_MT_CONST_IDX, /* "Parent" }, */
175 MONO_MT_BLOB_IDX, /* "Value" }, */
178 #define CUSTOM_ATTR_SCHEMA_OFFSET CONSTANT_SCHEMA_OFFSET + 5
179 MONO_MT_HASCAT_IDX, /* "Parent" }, */
180 MONO_MT_CAT_IDX, /* "Type" }, */
181 MONO_MT_BLOB_IDX, /* "Value" }, */
184 #define DECL_SEC_SCHEMA_OFFSET CUSTOM_ATTR_SCHEMA_OFFSET + 4
185 MONO_MT_UINT16, /* "Action" }, */
186 MONO_MT_HASDEC_IDX, /* "Parent" }, */
187 MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
190 #define EVENTMAP_SCHEMA_OFFSET DECL_SEC_SCHEMA_OFFSET + 4
191 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
192 MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
195 #define EVENT_SCHEMA_OFFSET EVENTMAP_SCHEMA_OFFSET + 3
196 MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
197 MONO_MT_STRING_IDX, /* "Name" }, */
198 MONO_MT_TDOR_IDX, /* "EventType" }, TypeDef or TypeRef or TypeSpec */
201 #define EVENT_POINTER_SCHEMA_OFFSET EVENT_SCHEMA_OFFSET + 4
202 MONO_MT_TABLE_IDX, /* "Event" }, */
205 #define EXPORTED_TYPE_SCHEMA_OFFSET EVENT_POINTER_SCHEMA_OFFSET + 2
206 MONO_MT_UINT32, /* "Flags" }, */
207 MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
208 MONO_MT_STRING_IDX, /* "TypeName" }, */
209 MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
210 MONO_MT_IMPL_IDX, /* "Implementation" }, */
213 #define FIELD_SCHEMA_OFFSET EXPORTED_TYPE_SCHEMA_OFFSET + 6
214 MONO_MT_UINT16, /* "Flags" }, */
215 MONO_MT_STRING_IDX, /* "Name" }, */
216 MONO_MT_BLOB_IDX, /* "Signature" }, */
219 #define FIELD_LAYOUT_SCHEMA_OFFSET FIELD_SCHEMA_OFFSET + 4
220 MONO_MT_UINT32, /* "Offset" }, */
221 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
224 #define FIELD_MARSHAL_SCHEMA_OFFSET FIELD_LAYOUT_SCHEMA_OFFSET + 3
225 MONO_MT_HFM_IDX, /* "Parent" }, */
226 MONO_MT_BLOB_IDX, /* "NativeType" }, */
229 #define FIELD_RVA_SCHEMA_OFFSET FIELD_MARSHAL_SCHEMA_OFFSET + 3
230 MONO_MT_UINT32, /* "RVA" }, */
231 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
234 #define FIELD_POINTER_SCHEMA_OFFSET FIELD_RVA_SCHEMA_OFFSET + 3
235 MONO_MT_TABLE_IDX, /* "Field" }, */
238 #define FILE_SCHEMA_OFFSET FIELD_POINTER_SCHEMA_OFFSET + 2
239 MONO_MT_UINT32, /* "Flags" }, */
240 MONO_MT_STRING_IDX, /* "Name" }, */
241 MONO_MT_BLOB_IDX, /* "Value" }, */
244 #define IMPLMAP_SCHEMA_OFFSET FILE_SCHEMA_OFFSET + 4
245 MONO_MT_UINT16, /* "MappingFlag" }, */
246 MONO_MT_MF_IDX, /* "MemberForwarded" }, */
247 MONO_MT_STRING_IDX, /* "ImportName" }, */
248 MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
251 #define IFACEMAP_SCHEMA_OFFSET IMPLMAP_SCHEMA_OFFSET + 5
252 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
253 MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
256 #define MANIFEST_SCHEMA_OFFSET IFACEMAP_SCHEMA_OFFSET + 3
257 MONO_MT_UINT32, /* "Offset" }, */
258 MONO_MT_UINT32, /* "Flags" }, */
259 MONO_MT_STRING_IDX, /* "Name" }, */
260 MONO_MT_IMPL_IDX, /* "Implementation" }, */
263 #define MEMBERREF_SCHEMA_OFFSET MANIFEST_SCHEMA_OFFSET + 5
264 MONO_MT_MRP_IDX, /* "Class" }, */
265 MONO_MT_STRING_IDX, /* "Name" }, */
266 MONO_MT_BLOB_IDX, /* "Signature" }, */
269 #define METHOD_SCHEMA_OFFSET MEMBERREF_SCHEMA_OFFSET + 4
270 MONO_MT_UINT32, /* "RVA" }, */
271 MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
272 MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
273 MONO_MT_STRING_IDX, /* "Name" }, */
274 MONO_MT_BLOB_IDX, /* "Signature" }, */
275 MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
278 #define METHOD_IMPL_SCHEMA_OFFSET METHOD_SCHEMA_OFFSET + 7
279 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
280 MONO_MT_MDOR_IDX, /* "MethodBody" }, */
281 MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
284 #define METHOD_SEMA_SCHEMA_OFFSET METHOD_IMPL_SCHEMA_OFFSET + 4
285 MONO_MT_UINT16, /* "MethodSemantic" }, */
286 MONO_MT_TABLE_IDX, /* "Method:Method" }, */
287 MONO_MT_HS_IDX, /* "Association" }, */
290 #define METHOD_POINTER_SCHEMA_OFFSET METHOD_SEMA_SCHEMA_OFFSET + 4
291 MONO_MT_TABLE_IDX, /* "Method" }, */
294 #define MODULE_SCHEMA_OFFSET METHOD_POINTER_SCHEMA_OFFSET + 2
295 MONO_MT_UINT16, /* "Generation" }, */
296 MONO_MT_STRING_IDX, /* "Name" }, */
297 MONO_MT_GUID_IDX, /* "MVID" }, */
298 MONO_MT_GUID_IDX, /* "EncID" }, */
299 MONO_MT_GUID_IDX, /* "EncBaseID" }, */
302 #define MODULEREF_SCHEMA_OFFSET MODULE_SCHEMA_OFFSET + 6
303 MONO_MT_STRING_IDX, /* "Name" }, */
306 #define NESTED_CLASS_SCHEMA_OFFSET MODULEREF_SCHEMA_OFFSET + 2
307 MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
308 MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
311 #define PARAM_SCHEMA_OFFSET NESTED_CLASS_SCHEMA_OFFSET + 3
312 MONO_MT_UINT16, /* "Flags" }, */
313 MONO_MT_UINT16, /* "Sequence" }, */
314 MONO_MT_STRING_IDX, /* "Name" }, */
317 #define PARAM_POINTER_SCHEMA_OFFSET PARAM_SCHEMA_OFFSET + 4
318 MONO_MT_TABLE_IDX, /* "Param" }, */
321 #define PROPERTY_SCHEMA_OFFSET PARAM_POINTER_SCHEMA_OFFSET + 2
322 MONO_MT_UINT16, /* "Flags" }, */
323 MONO_MT_STRING_IDX, /* "Name" }, */
324 MONO_MT_BLOB_IDX, /* "Type" }, */
327 #define PROPERTY_POINTER_SCHEMA_OFFSET PROPERTY_SCHEMA_OFFSET + 4
328 MONO_MT_TABLE_IDX, /* "Property" }, */
331 #define PROPERTY_MAP_SCHEMA_OFFSET PROPERTY_POINTER_SCHEMA_OFFSET + 2
332 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
333 MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
336 #define STDALON_SIG_SCHEMA_OFFSET PROPERTY_MAP_SCHEMA_OFFSET + 3
337 MONO_MT_BLOB_IDX, /* "Signature" }, */
340 #define TYPEDEF_SCHEMA_OFFSET STDALON_SIG_SCHEMA_OFFSET + 2
341 MONO_MT_UINT32, /* "Flags" }, */
342 MONO_MT_STRING_IDX, /* "Name" }, */
343 MONO_MT_STRING_IDX, /* "Namespace" }, */
344 MONO_MT_TDOR_IDX, /* "Extends" }, */
345 MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
346 MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
349 #define TYPEREF_SCHEMA_OFFSET TYPEDEF_SCHEMA_OFFSET + 7
350 MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
351 MONO_MT_STRING_IDX, /* "Name" }, */
352 MONO_MT_STRING_IDX, /* "Namespace" }, */
355 #define TYPESPEC_SCHEMA_OFFSET TYPEREF_SCHEMA_OFFSET + 4
356 MONO_MT_BLOB_IDX, /* "Signature" }, */
359 #define GENPARAM_SCHEMA_OFFSET TYPESPEC_SCHEMA_OFFSET + 2
360 MONO_MT_UINT16, /* "Number" }, */
361 MONO_MT_UINT16, /* "Flags" }, */
362 MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
363 MONO_MT_STRING_IDX, /* "Name" }, */
366 #define METHOD_SPEC_SCHEMA_OFFSET GENPARAM_SCHEMA_OFFSET + 5
367 MONO_MT_MDOR_IDX, /* "Method" }, */
368 MONO_MT_BLOB_IDX, /* "Signature" }, */
371 #define GEN_CONSTRAINT_SCHEMA_OFFSET METHOD_SPEC_SCHEMA_OFFSET + 3
372 MONO_MT_TABLE_IDX, /* "GenericParam" }, */
373 MONO_MT_TDOR_IDX, /* "Constraint" }, */
376 #define DOCUMENT_SCHEMA_OFFSET GEN_CONSTRAINT_SCHEMA_OFFSET + 3
377 MONO_MT_BLOB_IDX, /* Name */
378 MONO_MT_GUID_IDX, /* HashAlgorithm */
379 MONO_MT_BLOB_IDX, /* Hash */
380 MONO_MT_GUID_IDX, /* Language */
383 #define METHODBODY_SCHEMA_OFFSET DOCUMENT_SCHEMA_OFFSET + 5
384 MONO_MT_TABLE_IDX, /* Document */
385 MONO_MT_BLOB_IDX, /* SequencePoints */
388 #define LOCALSCOPE_SCHEMA_OFFSET METHODBODY_SCHEMA_OFFSET + 3
389 MONO_MT_TABLE_IDX, /* Method */
390 MONO_MT_TABLE_IDX, /* ImportScope */
391 MONO_MT_TABLE_IDX, /* VariableList */
392 MONO_MT_TABLE_IDX, /* ConstantList */
393 MONO_MT_UINT32, /* StartOffset */
394 MONO_MT_UINT32, /* Length */
397 #define LOCALVARIABLE_SCHEMA_OFFSET LOCALSCOPE_SCHEMA_OFFSET + 7
398 MONO_MT_UINT16, /* Attributes */
399 MONO_MT_UINT16, /* Index */
400 MONO_MT_STRING_IDX, /* Name */
403 #define LOCALCONSTANT_SCHEMA_OFFSET LOCALVARIABLE_SCHEMA_OFFSET + 4
404 MONO_MT_STRING_IDX, /* Name (String heap index) */
405 MONO_MT_BLOB_IDX, /* Signature (Blob heap index, LocalConstantSig blob) */
408 #define IMPORTSCOPE_SCHEMA_OFFSET LOCALCONSTANT_SCHEMA_OFFSET + 3
409 MONO_MT_TABLE_IDX, /* Parent (ImportScope row id or nil) */
410 MONO_MT_BLOB_IDX, /* Imports (Blob index, encoding: Imports blob) */
413 #define ASYNCMETHOD_SCHEMA_OFFSET IMPORTSCOPE_SCHEMA_OFFSET + 3
414 MONO_MT_TABLE_IDX, /* MoveNextMethod (MethodDef row id) */
415 MONO_MT_TABLE_IDX, /* KickoffMethod (MethodDef row id) */
418 #define CUSTOMDEBUGINFORMATION_SCHEMA_OFFSET ASYNCMETHOD_SCHEMA_OFFSET + 3
419 MONO_MT_HASCUSTDEBUG_IDX, /* Parent (HasCustomDebugInformation coded index) */
420 MONO_MT_GUID_IDX, /* Kind (Guid heap index) */
421 MONO_MT_BLOB_IDX, /* Value (Blob heap index) */
424 #define NULL_SCHEMA_OFFSET CUSTOMDEBUGINFORMATION_SCHEMA_OFFSET + 4
428 /* Must be the same order as MONO_TABLE_* */
429 const static unsigned char
430 table_description [] = {
431 MODULE_SCHEMA_OFFSET,
432 TYPEREF_SCHEMA_OFFSET,
433 TYPEDEF_SCHEMA_OFFSET,
434 FIELD_POINTER_SCHEMA_OFFSET,
436 METHOD_POINTER_SCHEMA_OFFSET,
437 METHOD_SCHEMA_OFFSET,
438 PARAM_POINTER_SCHEMA_OFFSET,
440 IFACEMAP_SCHEMA_OFFSET,
441 MEMBERREF_SCHEMA_OFFSET, /* 0xa */
442 CONSTANT_SCHEMA_OFFSET,
443 CUSTOM_ATTR_SCHEMA_OFFSET,
444 FIELD_MARSHAL_SCHEMA_OFFSET,
445 DECL_SEC_SCHEMA_OFFSET,
446 CLASS_LAYOUT_SCHEMA_OFFSET,
447 FIELD_LAYOUT_SCHEMA_OFFSET, /* 0x10 */
448 STDALON_SIG_SCHEMA_OFFSET,
449 EVENTMAP_SCHEMA_OFFSET,
450 EVENT_POINTER_SCHEMA_OFFSET,
452 PROPERTY_MAP_SCHEMA_OFFSET,
453 PROPERTY_POINTER_SCHEMA_OFFSET,
454 PROPERTY_SCHEMA_OFFSET,
455 METHOD_SEMA_SCHEMA_OFFSET,
456 METHOD_IMPL_SCHEMA_OFFSET,
457 MODULEREF_SCHEMA_OFFSET, /* 0x1a */
458 TYPESPEC_SCHEMA_OFFSET,
459 IMPLMAP_SCHEMA_OFFSET,
460 FIELD_RVA_SCHEMA_OFFSET,
463 ASSEMBLY_SCHEMA_OFFSET, /* 0x20 */
464 ASSEMBLYPROC_SCHEMA_OFFSET,
465 ASSEMBLYOS_SCHEMA_OFFSET,
466 ASSEMBLYREF_SCHEMA_OFFSET,
467 ASSEMBLYREFPROC_SCHEMA_OFFSET,
468 ASSEMBLYREFOS_SCHEMA_OFFSET,
470 EXPORTED_TYPE_SCHEMA_OFFSET,
471 MANIFEST_SCHEMA_OFFSET,
472 NESTED_CLASS_SCHEMA_OFFSET,
473 GENPARAM_SCHEMA_OFFSET, /* 0x2a */
474 METHOD_SPEC_SCHEMA_OFFSET,
475 GEN_CONSTRAINT_SCHEMA_OFFSET,
479 DOCUMENT_SCHEMA_OFFSET, /* 0x30 */
480 METHODBODY_SCHEMA_OFFSET,
481 LOCALSCOPE_SCHEMA_OFFSET,
482 LOCALVARIABLE_SCHEMA_OFFSET,
483 LOCALCONSTANT_SCHEMA_OFFSET,
484 IMPORTSCOPE_SCHEMA_OFFSET,
485 ASYNCMETHOD_SCHEMA_OFFSET,
486 CUSTOMDEBUGINFORMATION_SCHEMA_OFFSET
489 #ifdef HAVE_ARRAY_ELEM_INIT
490 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
491 #define MSGSTRFIELD1(line) str##line
492 static const struct msgstr_t {
493 #define TABLEDEF(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
494 #include "mono/cil/tables.def"
497 #define TABLEDEF(a,b) b,
498 #include "mono/cil/tables.def"
501 static const gint16 tableidx [] = {
502 #define TABLEDEF(a,b) [a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
503 #include "mono/cil/tables.def"
508 #define TABLEDEF(a,b) b,
509 static const char* const
510 mono_tables_names [] = {
511 #include "mono/cil/tables.def"
517 // Amount initially reserved in each imageset's mempool.
518 // FIXME: This number is arbitrary, a more practical number should be found
519 #define INITIAL_IMAGE_SET_SIZE 1024
522 * mono_meta_table_name:
523 * @table: table index
525 * Returns the name of the given ECMA metadata logical format table
526 * as described in ECMA 335, Partition II, Section 22.
528 * Returns: the name for the @table index
531 mono_meta_table_name (int table)
533 if ((table < 0) || (table > MONO_TABLE_LAST))
536 #ifdef HAVE_ARRAY_ELEM_INIT
537 return (const char*)&tablestr + tableidx [table];
539 return mono_tables_names [table];
543 /* The guy who wrote the spec for this should not be allowed near a
546 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
547 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
548 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
549 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
550 inverse of this mapping.
553 #define rtsize(meta,s,b) (((s) < (1 << (b)) ? 2 : 4))
556 idx_size (MonoImage *meta, int tableidx)
558 if (meta->referenced_tables && (meta->referenced_tables & ((guint64)1 << tableidx)))
559 return meta->referenced_table_rows [tableidx] < 65536 ? 2 : 4;
561 return meta->tables [tableidx].rows < 65536 ? 2 : 4;
565 get_nrows (MonoImage *meta, int tableidx)
567 if (meta->referenced_tables && (meta->referenced_tables & ((guint64)1 << tableidx)))
568 return meta->referenced_table_rows [tableidx];
570 return meta->tables [tableidx].rows;
573 /* Reference: Partition II - 23.2.6 */
575 * mono_metadata_compute_size:
576 * @meta: metadata context
577 * @tableindex: metadata table number
578 * @result_bitfield: pointer to guint32 where to store additional info
580 * mono_metadata_compute_size() computes the lenght in bytes of a single
581 * row in a metadata table. The size of each column is encoded in the
582 * @result_bitfield return value along with the number of columns in the table.
583 * the resulting bitfield should be handed to the mono_metadata_table_size()
584 * and mono_metadata_table_count() macros.
585 * This is a Mono runtime internal only function.
588 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
590 guint32 bitfield = 0;
591 int size = 0, field_size = 0;
594 const unsigned char *description = TableSchemas + table_description [tableindex];
596 for (i = 0; (code = description [i]) != MONO_MT_END; i++){
599 field_size = 4; break;
602 field_size = 2; break;
605 field_size = 1; break;
607 case MONO_MT_BLOB_IDX:
608 field_size = meta->idx_blob_wide ? 4 : 2; break;
610 case MONO_MT_STRING_IDX:
611 field_size = meta->idx_string_wide ? 4 : 2; break;
613 case MONO_MT_GUID_IDX:
614 field_size = meta->idx_guid_wide ? 4 : 2; break;
616 case MONO_MT_TABLE_IDX:
617 /* Uhm, a table index can point to other tables besides the current one
618 * so, it's not correct to use the rowcount of the current table to
619 * get the size for this column - lupus
621 switch (tableindex) {
622 case MONO_TABLE_ASSEMBLYREFOS:
624 field_size = idx_size (meta, MONO_TABLE_ASSEMBLYREF); break;
625 case MONO_TABLE_ASSEMBLYREFPROCESSOR:
627 field_size = idx_size (meta, MONO_TABLE_ASSEMBLYREF); break;
628 case MONO_TABLE_CLASSLAYOUT:
630 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
631 case MONO_TABLE_EVENTMAP:
632 g_assert (i == 0 || i == 1);
633 field_size = i ? idx_size (meta, MONO_TABLE_EVENT):
634 idx_size (meta, MONO_TABLE_TYPEDEF);
636 case MONO_TABLE_EVENT_POINTER:
638 field_size = idx_size (meta, MONO_TABLE_EVENT); break;
639 case MONO_TABLE_EXPORTEDTYPE:
641 /* the index is in another metadata file, so it must be 4 */
642 field_size = 4; break;
643 case MONO_TABLE_FIELDLAYOUT:
645 field_size = idx_size (meta, MONO_TABLE_FIELD); break;
646 case MONO_TABLE_FIELDRVA:
648 field_size = idx_size (meta, MONO_TABLE_FIELD); break;
649 case MONO_TABLE_FIELD_POINTER:
651 field_size = idx_size (meta, MONO_TABLE_FIELD); break;
652 case MONO_TABLE_IMPLMAP:
654 field_size = idx_size (meta, MONO_TABLE_MODULEREF); break;
655 case MONO_TABLE_INTERFACEIMPL:
657 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
658 case MONO_TABLE_METHOD:
660 field_size = idx_size (meta, MONO_TABLE_PARAM); break;
661 case MONO_TABLE_METHODIMPL:
663 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
664 case MONO_TABLE_METHODSEMANTICS:
666 field_size = idx_size (meta, MONO_TABLE_METHOD); break;
667 case MONO_TABLE_METHOD_POINTER:
669 field_size = idx_size (meta, MONO_TABLE_METHOD); break;
670 case MONO_TABLE_NESTEDCLASS:
671 g_assert (i == 0 || i == 1);
672 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
673 case MONO_TABLE_PARAM_POINTER:
675 field_size = idx_size (meta, MONO_TABLE_PARAM); break;
676 case MONO_TABLE_PROPERTYMAP:
677 g_assert (i == 0 || i == 1);
678 field_size = i ? idx_size (meta, MONO_TABLE_PROPERTY):
679 idx_size (meta, MONO_TABLE_TYPEDEF);
681 case MONO_TABLE_PROPERTY_POINTER:
683 field_size = idx_size (meta, MONO_TABLE_PROPERTY); break;
684 case MONO_TABLE_TYPEDEF:
685 g_assert (i == 4 || i == 5);
686 field_size = i == 4 ? idx_size (meta, MONO_TABLE_FIELD):
687 idx_size (meta, MONO_TABLE_METHOD);
689 case MONO_TABLE_GENERICPARAM:
691 n = MAX (get_nrows (meta, MONO_TABLE_METHOD), get_nrows (meta, MONO_TABLE_TYPEDEF));
692 /*This is a coded token for 2 tables, so takes 1 bit */
693 field_size = rtsize (meta, n, 16 - MONO_TYPEORMETHOD_BITS);
695 case MONO_TABLE_GENERICPARAMCONSTRAINT:
697 field_size = idx_size (meta, MONO_TABLE_GENERICPARAM);
699 case MONO_TABLE_LOCALSCOPE:
702 // FIXME: This table is in another file
703 field_size = idx_size (meta, MONO_TABLE_METHOD);
706 field_size = idx_size (meta, MONO_TABLE_IMPORTSCOPE);
709 field_size = idx_size (meta, MONO_TABLE_LOCALVARIABLE);
712 field_size = idx_size (meta, MONO_TABLE_LOCALCONSTANT);
715 g_assert_not_reached ();
719 case MONO_TABLE_METHODBODY:
721 field_size = idx_size (meta, MONO_TABLE_DOCUMENT); break;
722 case MONO_TABLE_IMPORTSCOPE:
724 field_size = idx_size (meta, MONO_TABLE_IMPORTSCOPE); break;
725 case MONO_TABLE_STATEMACHINEMETHOD:
726 g_assert(i == 0 || i == 1);
727 field_size = idx_size(meta, MONO_TABLE_METHOD); break;
729 g_error ("Can't handle MONO_MT_TABLE_IDX for table %d element %d", tableindex, i);
734 * HasConstant: ParamDef, FieldDef, Property
736 case MONO_MT_CONST_IDX:
737 n = MAX (get_nrows (meta, MONO_TABLE_PARAM),
738 get_nrows (meta, MONO_TABLE_FIELD));
739 n = MAX (n, get_nrows (meta, MONO_TABLE_PROPERTY));
741 /* 2 bits to encode tag */
742 field_size = rtsize (meta, n, 16-2);
746 * HasCustomAttribute: points to any table but
749 case MONO_MT_HASCAT_IDX:
751 * We believe that since the signature and
752 * permission are indexing the Blob heap,
753 * we should consider the blob size first
755 /* I'm not a believer - lupus
756 if (meta->idx_blob_wide){
761 n = MAX (get_nrows (meta, MONO_TABLE_METHOD),
762 get_nrows (meta, MONO_TABLE_FIELD));
763 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPEREF));
764 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPEDEF));
765 n = MAX (n, get_nrows (meta, MONO_TABLE_PARAM));
766 n = MAX (n, get_nrows (meta, MONO_TABLE_INTERFACEIMPL));
767 n = MAX (n, get_nrows (meta, MONO_TABLE_MEMBERREF));
768 n = MAX (n, get_nrows (meta, MONO_TABLE_MODULE));
769 n = MAX (n, get_nrows (meta, MONO_TABLE_DECLSECURITY));
770 n = MAX (n, get_nrows (meta, MONO_TABLE_PROPERTY));
771 n = MAX (n, get_nrows (meta, MONO_TABLE_EVENT));
772 n = MAX (n, get_nrows (meta, MONO_TABLE_STANDALONESIG));
773 n = MAX (n, get_nrows (meta, MONO_TABLE_MODULEREF));
774 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPESPEC));
775 n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLY));
776 n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
777 n = MAX (n, get_nrows (meta, MONO_TABLE_FILE));
778 n = MAX (n, get_nrows (meta, MONO_TABLE_EXPORTEDTYPE));
779 n = MAX (n, get_nrows (meta, MONO_TABLE_MANIFESTRESOURCE));
780 n = MAX (n, get_nrows (meta, MONO_TABLE_GENERICPARAM));
781 n = MAX (n, get_nrows (meta, MONO_TABLE_GENERICPARAMCONSTRAINT));
782 n = MAX (n, get_nrows (meta, MONO_TABLE_METHODSPEC));
784 /* 5 bits to encode */
785 field_size = rtsize (meta, n, 16-5);
789 * HasCustomAttribute: points to any table but
793 case MONO_MT_HASCUSTDEBUG_IDX:
794 n = MAX(get_nrows (meta, MONO_TABLE_METHOD),
795 get_nrows (meta, MONO_TABLE_FIELD));
796 n = MAX(n, get_nrows (meta, MONO_TABLE_TYPEREF));
797 n = MAX(n, get_nrows (meta, MONO_TABLE_TYPEDEF));
798 n = MAX(n, get_nrows (meta, MONO_TABLE_PARAM));
799 n = MAX(n, get_nrows (meta, MONO_TABLE_INTERFACEIMPL));
800 n = MAX(n, get_nrows (meta, MONO_TABLE_MEMBERREF));
801 n = MAX(n, get_nrows (meta, MONO_TABLE_MODULE));
802 n = MAX(n, get_nrows (meta, MONO_TABLE_DECLSECURITY));
803 n = MAX(n, get_nrows (meta, MONO_TABLE_PROPERTY));
804 n = MAX(n, get_nrows (meta, MONO_TABLE_EVENT));
805 n = MAX(n, get_nrows (meta, MONO_TABLE_STANDALONESIG));
806 n = MAX(n, get_nrows (meta, MONO_TABLE_MODULEREF));
807 n = MAX(n, get_nrows (meta, MONO_TABLE_TYPESPEC));
808 n = MAX(n, get_nrows (meta, MONO_TABLE_ASSEMBLY));
809 n = MAX(n, get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
810 n = MAX(n, get_nrows (meta, MONO_TABLE_FILE));
811 n = MAX(n, get_nrows (meta, MONO_TABLE_EXPORTEDTYPE));
812 n = MAX(n, get_nrows (meta, MONO_TABLE_MANIFESTRESOURCE));
813 n = MAX(n, get_nrows (meta, MONO_TABLE_GENERICPARAM));
814 n = MAX(n, get_nrows (meta, MONO_TABLE_GENERICPARAMCONSTRAINT));
815 n = MAX(n, get_nrows (meta, MONO_TABLE_METHODSPEC));
816 n = MAX(n, get_nrows (meta, MONO_TABLE_DOCUMENT));
817 n = MAX(n, get_nrows (meta, MONO_TABLE_LOCALSCOPE));
818 n = MAX(n, get_nrows (meta, MONO_TABLE_LOCALVARIABLE));
819 n = MAX(n, get_nrows (meta, MONO_TABLE_LOCALCONSTANT));
820 n = MAX(n, get_nrows (meta, MONO_TABLE_IMPORTSCOPE));
822 /* 5 bits to encode */
823 field_size = rtsize(meta, n, 16 - 5);
827 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
828 * MemberRef and String.
830 case MONO_MT_CAT_IDX:
831 /* String is a heap, if it is wide, we know the size */
833 if (meta->idx_string_wide){
838 n = MAX (get_nrows (meta, MONO_TABLE_TYPEREF),
839 get_nrows (meta, MONO_TABLE_TYPEDEF));
840 n = MAX (n, get_nrows (meta, MONO_TABLE_METHOD));
841 n = MAX (n, get_nrows (meta, MONO_TABLE_MEMBERREF));
843 /* 3 bits to encode */
844 field_size = rtsize (meta, n, 16-3);
848 * HasDeclSecurity: Typedef, MethodDef, Assembly
850 case MONO_MT_HASDEC_IDX:
851 n = MAX (get_nrows (meta, MONO_TABLE_TYPEDEF),
852 get_nrows (meta, MONO_TABLE_METHOD));
853 n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLY));
855 /* 2 bits to encode */
856 field_size = rtsize (meta, n, 16-2);
860 * Implementation: File, AssemblyRef, ExportedType
862 case MONO_MT_IMPL_IDX:
863 n = MAX (get_nrows (meta, MONO_TABLE_FILE),
864 get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
865 n = MAX (n, get_nrows (meta, MONO_TABLE_EXPORTEDTYPE));
867 /* 2 bits to encode tag */
868 field_size = rtsize (meta, n, 16-2);
872 * HasFieldMarshall: FieldDef, ParamDef
874 case MONO_MT_HFM_IDX:
875 n = MAX (get_nrows (meta, MONO_TABLE_FIELD),
876 get_nrows (meta, MONO_TABLE_PARAM));
878 /* 1 bit used to encode tag */
879 field_size = rtsize (meta, n, 16-1);
883 * MemberForwarded: FieldDef, MethodDef
886 n = MAX (get_nrows (meta, MONO_TABLE_FIELD),
887 get_nrows (meta, MONO_TABLE_METHOD));
889 /* 1 bit used to encode tag */
890 field_size = rtsize (meta, n, 16-1);
894 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
896 * It is TypeDef, _TypeRef_, TypeSpec, instead.
898 case MONO_MT_TDOR_IDX:
899 n = MAX (get_nrows (meta, MONO_TABLE_TYPEDEF),
900 get_nrows (meta, MONO_TABLE_TYPEREF));
901 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPESPEC));
903 /* 2 bits to encode */
904 field_size = rtsize (meta, n, 16-2);
908 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
910 case MONO_MT_MRP_IDX:
911 n = MAX (get_nrows (meta, MONO_TABLE_TYPEDEF),
912 get_nrows (meta, MONO_TABLE_TYPEREF));
913 n = MAX (n, get_nrows (meta, MONO_TABLE_METHOD));
914 n = MAX (n, get_nrows (meta, MONO_TABLE_MODULEREF));
915 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPESPEC));
917 /* 3 bits to encode */
918 field_size = rtsize (meta, n, 16 - 3);
922 * MethodDefOrRef: MethodDef, MemberRef
924 case MONO_MT_MDOR_IDX:
925 n = MAX (get_nrows (meta, MONO_TABLE_METHOD),
926 get_nrows (meta, MONO_TABLE_MEMBERREF));
928 /* 1 bit used to encode tag */
929 field_size = rtsize (meta, n, 16-1);
933 * HasSemantics: Property, Event
936 n = MAX (get_nrows (meta, MONO_TABLE_PROPERTY),
937 get_nrows (meta, MONO_TABLE_EVENT));
939 /* 1 bit used to encode tag */
940 field_size = rtsize (meta, n, 16-1);
944 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
947 n = MAX (get_nrows (meta, MONO_TABLE_MODULE),
948 get_nrows (meta, MONO_TABLE_MODULEREF));
949 n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
950 n = MAX (n, get_nrows (meta, MONO_TABLE_TYPEREF));
952 /* 2 bits used to encode tag (ECMA spec claims 3) */
953 field_size = rtsize (meta, n, 16 - 2);
958 * encode field size as follows (we just need to
965 bitfield |= (field_size-1) << shift;
968 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
971 *result_bitfield = (i << 24) | bitfield;
976 * mono_metadata_compute_table_bases:
977 * @meta: metadata context to compute table values
979 * Computes the table bases for the metadata structure.
980 * This is an internal function used by the image loader code.
983 mono_metadata_compute_table_bases (MonoImage *meta)
986 const char *base = meta->tables_base;
988 for (i = 0; i < MONO_TABLE_NUM; i++) {
989 MonoTableInfo *table = &meta->tables [i];
990 if (table->rows == 0)
993 table->row_size = mono_metadata_compute_size (meta, i, &table->size_bitfield);
995 base += table->rows * table->row_size;
1000 * mono_metadata_locate:
1001 * @meta: metadata context
1002 * @table: table code.
1003 * @idx: index of element to retrieve from @table.
1005 * Returns: a pointer to the @idx element in the metadata table
1006 * whose code is @table.
1009 mono_metadata_locate (MonoImage *meta, int table, int idx)
1011 /* idx == 0 refers always to NULL */
1012 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, ""); /*FIXME shouldn't we return NULL here?*/
1014 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
1018 * mono_metadata_locate_token:
1019 * @meta: metadata context
1020 * @token: metadata token
1022 * Returns: a pointer to the data in the metadata represented by the
1026 mono_metadata_locate_token (MonoImage *meta, guint32 token)
1028 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
1032 * mono_metadata_string_heap:
1033 * @meta: metadata context
1034 * @index: index into the string heap.
1036 * Returns: an in-memory pointer to the @index in the string heap.
1039 mono_metadata_string_heap (MonoImage *meta, guint32 index)
1041 g_assert (index < meta->heap_strings.size);
1042 g_return_val_if_fail (index < meta->heap_strings.size, "");
1043 return meta->heap_strings.data + index;
1047 * mono_metadata_user_string:
1048 * @meta: metadata context
1049 * @index: index into the user string heap.
1051 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
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 * @meta: metadata context
1064 * @index: index into the blob.
1066 * Returns: an in-memory pointer to the @index in the Blob heap.
1069 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
1071 g_assert (index < meta->heap_blob.size);
1072 g_return_val_if_fail (index < meta->heap_blob.size, "");/*FIXME shouldn't we return NULL and check for index == 0?*/
1073 return meta->heap_blob.data + index;
1077 * mono_metadata_guid_heap:
1078 * @meta: metadata context
1079 * @index: index into the guid heap.
1081 * Returns: an in-memory pointer to the @index in the guid heap.
1084 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
1087 index *= 16; /* adjust for guid size and 1-based index */
1088 g_return_val_if_fail (index < meta->heap_guid.size, "");
1089 return meta->heap_guid.data + index;
1092 static const unsigned char *
1093 dword_align (const unsigned char *ptr)
1095 #if SIZEOF_VOID_P == 8
1096 return (const unsigned char *) (((guint64) (ptr + 3)) & ~3);
1098 return (const unsigned char *) (((guint32) (ptr + 3)) & ~3);
1103 * mono_metadata_decode_row:
1104 * @t: table to extract information from.
1105 * @idx: index in table.
1106 * @res: array of @res_size cols to store the results in
1108 * This decompresses the metadata element @idx in table @t
1109 * into the guint32 @res array that has res_size elements
1112 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
1114 guint32 bitfield = t->size_bitfield;
1115 int i, count = mono_metadata_table_count (bitfield);
1118 g_assert (idx < t->rows);
1119 g_assert (idx >= 0);
1120 data = t->base + idx * t->row_size;
1122 g_assert (res_size == count);
1124 for (i = 0; i < count; i++) {
1125 int n = mono_metadata_table_size (bitfield, i);
1129 res [i] = *data; break;
1131 res [i] = read16 (data); break;
1133 res [i] = read32 (data); break;
1135 g_assert_not_reached ();
1142 * mono_metadata_decode_row_col:
1143 * @t: table to extract information from.
1144 * @idx: index for row in table.
1145 * @col: column in the row.
1147 * This function returns the value of column @col from the @idx
1148 * row in the table @t.
1151 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
1153 guint32 bitfield = t->size_bitfield;
1155 register const char *data;
1158 g_assert (idx < t->rows);
1159 g_assert (col < mono_metadata_table_count (bitfield));
1160 data = t->base + idx * t->row_size;
1162 n = mono_metadata_table_size (bitfield, 0);
1163 for (i = 0; i < col; ++i) {
1165 n = mono_metadata_table_size (bitfield, i + 1);
1171 return read16 (data);
1173 return read32 (data);
1175 g_assert_not_reached ();
1181 * mono_metadata_decode_blob_size:
1182 * @ptr: pointer to a blob object
1183 * @rptr: the new position of the pointer
1185 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
1187 * Returns: the size of the blob object
1190 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
1192 const unsigned char *ptr = (const unsigned char *)xptr;
1195 if ((*ptr & 0x80) == 0){
1196 size = ptr [0] & 0x7f;
1198 } else if ((*ptr & 0x40) == 0){
1199 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
1202 size = ((ptr [0] & 0x1f) << 24) +
1214 * mono_metadata_decode_value:
1215 * @ptr: pointer to decode from
1216 * @rptr: the new position of the pointer
1218 * This routine decompresses 32-bit values as specified in the "Blob and
1219 * Signature" section (23.2)
1221 * Returns: the decoded value
1224 mono_metadata_decode_value (const char *_ptr, const char **rptr)
1226 const unsigned char *ptr = (const unsigned char *) _ptr;
1227 unsigned char b = *ptr;
1230 if ((b & 0x80) == 0){
1233 } else if ((b & 0x40) == 0){
1234 len = ((b & 0x3f) << 8 | ptr [1]);
1237 len = ((b & 0x1f) << 24) |
1250 * mono_metadata_decode_signed_value:
1251 * @ptr: pointer to decode from
1252 * @rptr: the new position of the pointer
1254 * This routine decompresses 32-bit signed values
1255 * (not specified in the spec)
1257 * Returns: the decoded value
1260 mono_metadata_decode_signed_value (const char *ptr, const char **rptr)
1262 guint32 uval = mono_metadata_decode_value (ptr, rptr);
1263 gint32 ival = uval >> 1;
1266 /* ival is a truncated 2's complement negative number. */
1268 /* 6 bits = 7 bits for compressed representation (top bit is '0') - 1 sign bit */
1271 /* 13 bits = 14 bits for compressed representation (top bits are '10') - 1 sign bit */
1272 return ival - 0x2000;
1273 if (ival < 0x10000000)
1274 /* 28 bits = 29 bits for compressed representation (top bits are '110') - 1 sign bit */
1275 return ival - 0x10000000;
1276 g_assert (ival < 0x20000000);
1277 g_warning ("compressed signed value appears to use 29 bits for compressed representation: %x (raw: %8x)", ival, uval);
1278 return ival - 0x20000000;
1282 * Translates the given 1-based index into the Method, Field, Event, or Param tables
1283 * using the *Ptr tables in uncompressed metadata, if they are available.
1285 * FIXME: The caller is not forced to call this function, which is error-prone, since
1286 * forgetting to call it would only show up as a bug on uncompressed metadata.
1289 mono_metadata_translate_token_index (MonoImage *image, int table, guint32 idx)
1291 if (!image->uncompressed_metadata)
1295 case MONO_TABLE_METHOD:
1296 if (image->tables [MONO_TABLE_METHOD_POINTER].rows)
1297 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD_POINTER], idx - 1, MONO_METHOD_POINTER_METHOD);
1300 case MONO_TABLE_FIELD:
1301 if (image->tables [MONO_TABLE_FIELD_POINTER].rows)
1302 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_FIELD_POINTER], idx - 1, MONO_FIELD_POINTER_FIELD);
1305 case MONO_TABLE_EVENT:
1306 if (image->tables [MONO_TABLE_EVENT_POINTER].rows)
1307 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_EVENT_POINTER], idx - 1, MONO_EVENT_POINTER_EVENT);
1310 case MONO_TABLE_PROPERTY:
1311 if (image->tables [MONO_TABLE_PROPERTY_POINTER].rows)
1312 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PROPERTY_POINTER], idx - 1, MONO_PROPERTY_POINTER_PROPERTY);
1315 case MONO_TABLE_PARAM:
1316 if (image->tables [MONO_TABLE_PARAM_POINTER].rows)
1317 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PARAM_POINTER], idx - 1, MONO_PARAM_POINTER_PARAM);
1326 * mono_metadata_decode_table_row:
1328 * Same as mono_metadata_decode_row, but takes an IMAGE+TABLE ID pair, and takes
1329 * uncompressed metadata into account, so it should be used to access the
1330 * Method, Field, Param and Event tables when the access is made from metadata, i.e.
1331 * IDX is retrieved from a metadata table, like MONO_TYPEDEF_FIELD_LIST.
1334 mono_metadata_decode_table_row (MonoImage *image, int table, int idx, guint32 *res, int res_size)
1336 if (image->uncompressed_metadata)
1337 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1339 mono_metadata_decode_row (&image->tables [table], idx, res, res_size);
1343 * mono_metadata_decode_table_row_col:
1345 * Same as mono_metadata_decode_row_col, but takes an IMAGE+TABLE ID pair, and takes
1346 * uncompressed metadata into account, so it should be used to access the
1347 * Method, Field, Param and Event tables.
1349 guint32 mono_metadata_decode_table_row_col (MonoImage *image, int table, int idx, guint col)
1351 if (image->uncompressed_metadata)
1352 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1354 return mono_metadata_decode_row_col (&image->tables [table], idx, col);
1358 * mono_metadata_parse_typedef_or_ref:
1359 * @m: a metadata context.
1360 * @ptr: a pointer to an encoded TypedefOrRef in @m
1361 * @rptr: pointer updated to match the end of the decoded stream
1363 * Returns: a token valid in the @m metadata decoded from
1364 * the compressed representation.
1367 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1370 token = mono_metadata_decode_value (ptr, &ptr);
1373 return mono_metadata_token_from_dor (token);
1377 * mono_metadata_parse_custom_mod:
1378 * @m: a metadata context.
1379 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1380 * @ptr: a pointer to (possibly) the start of a custom modifier list
1381 * @rptr: pointer updated to match the end of the decoded stream
1383 * Checks if @ptr points to a type custom modifier compressed representation.
1385 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1388 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1390 MonoCustomMod local;
1391 if ((*ptr == MONO_TYPE_CMOD_OPT) || (*ptr == MONO_TYPE_CMOD_REQD)) {
1394 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1395 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1402 * mono_metadata_parse_array_internal:
1403 * @m: a metadata context.
1404 * @transient: whenever to allocate data from the heap
1405 * @ptr: a pointer to an encoded array description.
1406 * @rptr: pointer updated to match the end of the decoded stream
1408 * Decodes the compressed array description found in the metadata @m at @ptr.
1410 * Returns: a #MonoArrayType structure describing the array type
1411 * and dimensions. Memory is allocated from the heap or from the image mempool, depending
1412 * on the value of @transient.
1414 * LOCKING: Acquires the loader lock
1416 static MonoArrayType *
1417 mono_metadata_parse_array_internal (MonoImage *m, MonoGenericContainer *container,
1418 gboolean transient, const char *ptr, const char **rptr, MonoError *error)
1421 MonoArrayType *array;
1424 etype = mono_metadata_parse_type_checked (m, container, 0, FALSE, ptr, &ptr, error); //FIXME this doesn't respect @transient
1428 array = transient ? (MonoArrayType *)g_malloc0 (sizeof (MonoArrayType)) : (MonoArrayType *)mono_image_alloc0 (m, sizeof (MonoArrayType));
1429 array->eklass = mono_class_from_mono_type (etype);
1430 array->rank = mono_metadata_decode_value (ptr, &ptr);
1432 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1433 if (array->numsizes)
1434 array->sizes = transient ? (int *)g_malloc0 (sizeof (int) * array->numsizes) : (int *)mono_image_alloc0 (m, sizeof (int) * array->numsizes);
1435 for (i = 0; i < array->numsizes; ++i)
1436 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1438 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1439 if (array->numlobounds)
1440 array->lobounds = transient ? (int *)g_malloc0 (sizeof (int) * array->numlobounds) : (int *)mono_image_alloc0 (m, sizeof (int) * array->numlobounds);
1441 for (i = 0; i < array->numlobounds; ++i)
1442 array->lobounds [i] = mono_metadata_decode_signed_value (ptr, &ptr);
1450 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1453 MonoArrayType *ret = mono_metadata_parse_array_internal (m, NULL, FALSE, ptr, rptr, &error);
1454 mono_error_cleanup (&error);
1460 * mono_metadata_free_array:
1461 * @array: array description
1463 * Frees the array description returned from mono_metadata_parse_array().
1466 mono_metadata_free_array (MonoArrayType *array)
1468 g_free (array->sizes);
1469 g_free (array->lobounds);
1474 * need to add common field and param attributes combinations:
1477 * public static literal
1480 * private static literal
1482 static const MonoType
1484 /* data, attrs, type, nmods, byref, pinned */
1485 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1486 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1487 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1488 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1489 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1490 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1491 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1492 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1493 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1494 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1495 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1496 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1497 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1498 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1499 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1500 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1501 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1502 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1503 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1504 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1505 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1506 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1507 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1508 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1509 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1510 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1511 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1512 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1513 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1514 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1515 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1516 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1517 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1518 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1521 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1523 static GHashTable *type_cache = NULL;
1524 static int next_generic_inst_id = 0;
1526 /* Protected by image_sets_mutex */
1527 static MonoImageSet *mscorlib_image_set;
1528 /* Protected by image_sets_mutex */
1529 static GPtrArray *image_sets;
1530 static mono_mutex_t image_sets_mutex;
1532 static guint mono_generic_class_hash (gconstpointer data);
1535 * MonoTypes with modifies are never cached, so we never check or use that field.
1538 mono_type_hash (gconstpointer data)
1540 const MonoType *type = (const MonoType *) data;
1541 if (type->type == MONO_TYPE_GENERICINST)
1542 return mono_generic_class_hash (type->data.generic_class);
1544 return type->type | (type->byref << 8) | (type->attrs << 9);
1548 mono_type_equal (gconstpointer ka, gconstpointer kb)
1550 const MonoType *a = (const MonoType *) ka;
1551 const MonoType *b = (const MonoType *) kb;
1553 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1555 /* need other checks */
1560 mono_metadata_generic_inst_hash (gconstpointer data)
1562 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1566 for (i = 0; i < ginst->type_argc; ++i) {
1568 hash += mono_metadata_type_hash (ginst->type_argv [i]);
1571 return hash ^ (ginst->is_open << 8);
1575 mono_generic_inst_equal_full (const MonoGenericInst *a, const MonoGenericInst *b, gboolean signature_only)
1579 // An optimization: if the ids of two insts are the same, we know they are the same inst and don't check contents.
1580 // Furthermore, because we perform early de-duping, if the ids differ, we know the contents differ.
1581 #ifndef MONO_SMALL_CONFIG // Optimization does not work in MONO_SMALL_CONFIG: There are no IDs
1582 if (a->id && b->id) { // "id 0" means "object has no id"-- de-duping hasn't been performed yet, must check contents.
1585 // In signature-comparison mode id equality implies object equality, but this is not true for inequality.
1586 // Two separate objects could have signature-equavalent contents.
1587 if (!signature_only)
1592 if (a->is_open != b->is_open || a->type_argc != b->type_argc)
1594 for (i = 0; i < a->type_argc; ++i) {
1595 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], signature_only))
1602 mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1604 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1605 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1607 return mono_generic_inst_equal_full (a, b, FALSE);
1611 mono_generic_class_hash (gconstpointer data)
1613 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1614 guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
1617 hash += gclass->is_tb_open;
1618 hash += mono_metadata_generic_context_hash (&gclass->context);
1624 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1626 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1627 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1629 return _mono_metadata_generic_class_equal (a, b, FALSE);
1633 * mono_metadata_init:
1635 * Initialize the global variables of this module.
1636 * This is a Mono runtime internal function.
1639 mono_metadata_init (void)
1643 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1645 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1646 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1648 mono_os_mutex_init_recursive (&image_sets_mutex);
1652 * mono_metadata_cleanup:
1654 * Free all resources used by this module.
1655 * This is a Mono runtime internal function.
1658 mono_metadata_cleanup (void)
1660 g_hash_table_destroy (type_cache);
1662 g_ptr_array_free (image_sets, TRUE);
1664 mono_os_mutex_destroy (&image_sets_mutex);
1668 * mono_metadata_parse_type:
1669 * @m: metadata context
1670 * @mode: king of type that may be found at @ptr
1671 * @opt_attrs: optional attributes to store in the returned type
1672 * @ptr: pointer to the type representation
1673 * @rptr: pointer updated to match the end of the decoded stream
1674 * @transient: whenever to allocate the result from the heap or from a mempool
1676 * Decode a compressed type description found at @ptr in @m.
1677 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1678 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1679 * This function can be used to decode type descriptions in method signatures,
1680 * field signatures, locals signatures etc.
1682 * To parse a generic type, `generic_container' points to the current class'es
1683 * (the `generic_container' field in the MonoClass) or the current generic method's
1684 * (stored in image->property_hash) generic container.
1685 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1686 * this MonoGenericContainer.
1688 * LOCKING: Acquires the loader lock.
1690 * Returns: a #MonoType structure representing the decoded type.
1693 mono_metadata_parse_type_internal (MonoImage *m, MonoGenericContainer *container,
1694 short opt_attrs, gboolean transient, const char *ptr, const char **rptr, MonoError *error)
1696 MonoType *type, *cached;
1698 gboolean byref = FALSE;
1699 gboolean pinned = FALSE;
1700 const char *tmp_ptr;
1701 int count = 0; // Number of mod arguments
1707 * According to the spec, custom modifiers should come before the byref
1708 * flag, but the IL produced by ilasm from the following signature:
1709 * object modopt(...) &
1710 * starts with a byref flag, followed by the modifiers. (bug #49802)
1711 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1712 * it would be better to treat byref as real type constructor instead of
1714 * Also, pinned should come before anything else, but some MSV++ produced
1715 * assemblies violate this (#bug 61990).
1718 /* Count the modifiers first */
1723 case MONO_TYPE_PINNED:
1724 case MONO_TYPE_BYREF:
1727 case MONO_TYPE_CMOD_REQD:
1728 case MONO_TYPE_CMOD_OPT:
1730 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1737 if (count) { // There are mods, so the MonoType will be of nonstandard size.
1740 size = MONO_SIZEOF_TYPE + ((gint32)count) * sizeof (MonoCustomMod);
1741 type = transient ? (MonoType *)g_malloc0 (size) : (MonoType *)mono_image_alloc0 (m, size);
1742 type->num_mods = count;
1744 mono_error_set_bad_image (error, m, "Invalid type with more than 64 modifiers");
1747 } else { // The type is of standard size, so we can allocate it on the stack.
1749 memset (type, 0, MONO_SIZEOF_TYPE);
1752 /* Iterate again, but now parse pinned, byref and custom modifiers */
1757 case MONO_TYPE_PINNED:
1761 case MONO_TYPE_BYREF:
1765 case MONO_TYPE_CMOD_REQD:
1766 case MONO_TYPE_CMOD_OPT:
1767 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1775 type->attrs = opt_attrs;
1776 type->byref = byref;
1777 type->pinned = pinned ? 1 : 0;
1779 if (!do_mono_metadata_parse_type (type, m, container, transient, ptr, &ptr, error))
1785 // Possibly we can return an already-allocated type instead of the one we decoded
1786 if (!type->num_mods && !transient) {
1787 /* no need to free type here, because it is on the stack */
1788 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1789 MonoType *ret = type->byref ? &type->data.klass->this_arg : &type->data.klass->byval_arg;
1791 /* Consider the case:
1793 class Foo<T> { class Bar {} }
1794 class Test : Foo<Test>.Bar {}
1796 When Foo<Test> is being expanded, 'Test' isn't yet initialized. It's actually in
1797 a really pristine state: it doesn't even know whether 'Test' is a reference or a value type.
1799 We ensure that the MonoClass is in a state that we can canonicalize to:
1801 klass->byval_arg.data.klass == klass
1802 klass->this_arg.data.klass == klass
1804 If we can't canonicalize 'type', it doesn't matter, since later users of 'type' will do it.
1806 LOCKING: even though we don't explicitly hold a lock, in the problematic case 'ret' is a field
1807 of a MonoClass which currently holds the loader lock. 'type' is local.
1809 if (ret->data.klass == type->data.klass) {
1813 /* No need to use locking since nobody is modifying the hash table */
1814 if ((cached = (MonoType *)g_hash_table_lookup (type_cache, type))) {
1819 /* printf ("%x %x %c %s\n", type->attrs, type->num_mods, type->pinned ? 'p' : ' ', mono_type_full_name (type)); */
1821 if (type == &stype) { // Type was allocated on the stack, so we need to copy it to safety
1822 type = transient ? (MonoType *)g_malloc (MONO_SIZEOF_TYPE) : (MonoType *)mono_image_alloc (m, MONO_SIZEOF_TYPE);
1823 memcpy (type, &stype, MONO_SIZEOF_TYPE);
1830 mono_metadata_parse_type_checked (MonoImage *m, MonoGenericContainer *container,
1831 short opt_attrs, gboolean transient, const char *ptr, const char **rptr, MonoError *error)
1833 return mono_metadata_parse_type_internal (m, container, opt_attrs, transient, ptr, rptr, error);
1837 * LOCKING: Acquires the loader lock.
1840 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1841 const char *ptr, const char **rptr)
1844 MonoType * type = mono_metadata_parse_type_internal (m, NULL, opt_attrs, FALSE, ptr, rptr, &error);
1845 mono_error_cleanup (&error);
1850 mono_metadata_method_has_param_attrs (MonoImage *m, int def)
1852 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1853 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1854 guint lastp, i, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1856 if (def < methodt->rows)
1857 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1859 lastp = m->tables [MONO_TABLE_PARAM].rows + 1;
1861 for (i = param_index; i < lastp; ++i) {
1862 guint32 flags = mono_metadata_decode_row_col (paramt, i - 1, MONO_PARAM_FLAGS);
1871 * mono_metadata_get_param_attrs:
1873 * @m The image to loader parameter attributes from
1874 * @def method def token (one based)
1875 * @param_count number of params to decode including the return value
1877 * Return the parameter attributes for the method whose MethodDef index is DEF. The
1878 * returned memory needs to be freed by the caller. If all the param attributes are
1879 * 0, then NULL is returned.
1882 mono_metadata_get_param_attrs (MonoImage *m, int def, int param_count)
1884 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1885 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1886 guint32 cols [MONO_PARAM_SIZE];
1887 guint lastp, i, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1890 if (def < methodt->rows)
1891 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1893 lastp = paramt->rows + 1;
1895 for (i = param_index; i < lastp; ++i) {
1896 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1897 if (cols [MONO_PARAM_FLAGS]) {
1899 pattrs = g_new0 (int, param_count);
1900 /* at runtime we just ignore this kind of malformed file:
1901 * the verifier can signal the error to the user
1903 if (cols [MONO_PARAM_SEQUENCE] >= param_count)
1905 pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
1914 * mono_metadata_parse_signature:
1915 * @image: metadata context
1916 * @token: metadata token
1918 * Decode a method signature stored in the STANDALONESIG table
1920 * Returns: a MonoMethodSignature describing the signature.
1922 MonoMethodSignature*
1923 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1926 MonoMethodSignature *ret;
1927 ret = mono_metadata_parse_signature_checked (image, token, &error);
1928 mono_error_cleanup (&error);
1933 * mono_metadata_parse_signature_checked:
1934 * @image: metadata context
1935 * @token: metadata token
1936 * @error: set on error
1938 * Decode a method signature stored in the STANDALONESIG table
1940 * Returns: a MonoMethodSignature describing the signature. On failure
1941 * returns NULL and sets @error.
1943 MonoMethodSignature*
1944 mono_metadata_parse_signature_checked (MonoImage *image, guint32 token, MonoError *error)
1948 MonoTableInfo *tables = image->tables;
1949 guint32 idx = mono_metadata_token_index (token);
1953 if (image_is_dynamic (image)) {
1954 return (MonoMethodSignature *)mono_lookup_dynamic_token (image, token, NULL, error);
1957 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1959 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1961 ptr = mono_metadata_blob_heap (image, sig);
1962 mono_metadata_decode_blob_size (ptr, &ptr);
1964 return mono_metadata_parse_method_signature_full (image, NULL, 0, ptr, NULL, error);
1968 * mono_metadata_signature_alloc:
1969 * @image: metadata context
1970 * @nparmas: number of parameters in the signature
1972 * Allocate a MonoMethodSignature structure with the specified number of params.
1973 * The return type and the params types need to be filled later.
1974 * This is a Mono runtime internal function.
1976 * LOCKING: Assumes the loader lock is held.
1978 * Returns: the new MonoMethodSignature structure.
1980 MonoMethodSignature*
1981 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1983 MonoMethodSignature *sig;
1985 sig = (MonoMethodSignature *)mono_image_alloc0 (m, MONO_SIZEOF_METHOD_SIGNATURE + ((gint32)nparams) * sizeof (MonoType*));
1986 sig->param_count = nparams;
1987 sig->sentinelpos = -1;
1992 static MonoMethodSignature*
1993 mono_metadata_signature_dup_internal_with_padding (MonoImage *image, MonoMemPool *mp, MonoMethodSignature *sig, size_t padding)
1995 int sigsize, sig_header_size;
1996 MonoMethodSignature *ret;
1997 sigsize = sig_header_size = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *) + padding;
1999 sigsize += MONO_SIZEOF_TYPE;
2002 ret = (MonoMethodSignature *)mono_image_alloc (image, sigsize);
2004 ret = (MonoMethodSignature *)mono_mempool_alloc (mp, sigsize);
2006 ret = (MonoMethodSignature *)g_malloc (sigsize);
2009 memcpy (ret, sig, sig_header_size - padding);
2011 // Copy return value because of ownership semantics.
2013 // Danger! Do not alter padding use without changing the dup_add_this below
2014 intptr_t end_of_header = (intptr_t)( (char*)(ret) + sig_header_size);
2015 ret->ret = (MonoType *)end_of_header;
2016 memcpy (ret->ret, sig->ret, MONO_SIZEOF_TYPE);
2022 static MonoMethodSignature*
2023 mono_metadata_signature_dup_internal (MonoImage *image, MonoMemPool *mp, MonoMethodSignature *sig)
2025 return mono_metadata_signature_dup_internal_with_padding (image, mp, sig, 0);
2028 * signature_dup_add_this:
2030 * Make a copy of @sig, adding an explicit this argument.
2032 MonoMethodSignature*
2033 mono_metadata_signature_dup_add_this (MonoImage *image, MonoMethodSignature *sig, MonoClass *klass)
2035 MonoMethodSignature *ret;
2036 ret = mono_metadata_signature_dup_internal_with_padding (image, NULL, sig, sizeof (MonoType *));
2038 ret->param_count = sig->param_count + 1;
2039 ret->hasthis = FALSE;
2041 for (int i = sig->param_count - 1; i >= 0; i --)
2042 ret->params [i + 1] = sig->params [i];
2043 ret->params [0] = klass->valuetype ? &klass->this_arg : &klass->byval_arg;
2045 for (int i = sig->param_count - 1; i >= 0; i --)
2046 g_assert(ret->params [i + 1]->type == sig->params [i]->type && ret->params [i+1]->type != MONO_TYPE_END);
2047 g_assert (ret->ret->type == sig->ret->type && ret->ret->type != MONO_TYPE_END);
2054 MonoMethodSignature*
2055 mono_metadata_signature_dup_full (MonoImage *image, MonoMethodSignature *sig)
2057 MonoMethodSignature *ret = mono_metadata_signature_dup_internal (image, NULL, sig);
2059 for (int i = 0 ; i < sig->param_count; i ++)
2060 g_assert(ret->params [i]->type == sig->params [i]->type);
2061 g_assert (ret->ret->type == sig->ret->type);
2066 /*The mempool is accessed without synchronization*/
2067 MonoMethodSignature*
2068 mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig)
2070 return mono_metadata_signature_dup_internal (NULL, mp, sig);
2074 * mono_metadata_signature_dup:
2075 * @sig: method signature
2077 * Duplicate an existing MonoMethodSignature so it can be modified.
2078 * This is a Mono runtime internal function.
2080 * Returns: the new MonoMethodSignature structure.
2082 MonoMethodSignature*
2083 mono_metadata_signature_dup (MonoMethodSignature *sig)
2085 return mono_metadata_signature_dup_full (NULL, sig);
2089 * mono_metadata_signature_size:
2091 * Return the amount of memory allocated to SIG.
2094 mono_metadata_signature_size (MonoMethodSignature *sig)
2096 return MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
2100 * mono_metadata_parse_method_signature:
2101 * @m: metadata context
2102 * @generic_container: generics container
2103 * @def: the MethodDef index or 0 for Ref signatures.
2104 * @ptr: pointer to the signature metadata representation
2105 * @rptr: pointer updated to match the end of the decoded stream
2107 * Decode a method signature stored at @ptr.
2108 * This is a Mono runtime internal function.
2110 * LOCKING: Assumes the loader lock is held.
2112 * Returns: a MonoMethodSignature describing the signature.
2114 MonoMethodSignature *
2115 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *container,
2116 int def, const char *ptr, const char **rptr, MonoError *error)
2118 MonoMethodSignature *method;
2119 int i, *pattrs = NULL;
2120 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
2121 guint32 gen_param_count = 0;
2122 gboolean is_open = FALSE;
2127 gen_param_count = 1;
2132 call_convention = *ptr & 0x0F;
2134 if (gen_param_count)
2135 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
2136 param_count = mono_metadata_decode_value (ptr, &ptr);
2139 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 */
2141 method = mono_metadata_signature_alloc (m, param_count);
2142 method->hasthis = hasthis;
2143 method->explicit_this = explicit_this;
2144 method->call_convention = call_convention;
2145 method->generic_param_count = gen_param_count;
2147 if (call_convention != 0xa) {
2148 method->ret = mono_metadata_parse_type_checked (m, container, pattrs ? pattrs [0] : 0, FALSE, ptr, &ptr, error);
2150 mono_metadata_free_method_signature (method);
2154 is_open = mono_class_is_open_constructed_type (method->ret);
2157 for (i = 0; i < method->param_count; ++i) {
2158 if (*ptr == MONO_TYPE_SENTINEL) {
2159 if (method->call_convention != MONO_CALL_VARARG || def) {
2160 mono_error_set_bad_image (error, m, "Found sentinel for methoddef or no vararg");
2164 if (method->sentinelpos >= 0) {
2165 mono_error_set_bad_image (error, m, "Found sentinel twice in the same signature.");
2169 method->sentinelpos = i;
2172 method->params [i] = mono_metadata_parse_type_checked (m, container, pattrs ? pattrs [i+1] : 0, FALSE, ptr, &ptr, error);
2173 if (!method->params [i]) {
2174 mono_metadata_free_method_signature (method);
2179 is_open = mono_class_is_open_constructed_type (method->params [i]);
2182 /* The sentinel could be missing if the caller does not pass any additional arguments */
2183 if (!def && method->call_convention == MONO_CALL_VARARG && method->sentinelpos < 0)
2184 method->sentinelpos = method->param_count;
2186 method->has_type_parameters = is_open;
2188 if (def && (method->call_convention == MONO_CALL_VARARG))
2189 method->sentinelpos = method->param_count;
2196 * Add signature to a cache and increase ref count...
2203 * mono_metadata_parse_method_signature:
2204 * @m: metadata context
2205 * @def: the MethodDef index or 0 for Ref signatures.
2206 * @ptr: pointer to the signature metadata representation
2207 * @rptr: pointer updated to match the end of the decoded stream
2209 * Decode a method signature stored at @ptr.
2210 * This is a Mono runtime internal function.
2212 * LOCKING: Assumes the loader lock is held.
2214 * Returns: a MonoMethodSignature describing the signature.
2216 MonoMethodSignature *
2217 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
2220 * This function MUST NOT be called by runtime code as it does error handling incorrectly.
2221 * Use mono_metadata_parse_method_signature_full instead.
2222 * It's ok to asser on failure as we no longer use it.
2225 MonoMethodSignature *ret;
2226 ret = mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr, &error);
2227 g_assert (mono_error_ok (&error));
2233 * mono_metadata_free_method_signature:
2234 * @sig: signature to destroy
2236 * Free the memory allocated in the signature @sig.
2237 * This method needs to be robust and work also on partially-built
2238 * signatures, so it does extra checks.
2241 mono_metadata_free_method_signature (MonoMethodSignature *sig)
2243 /* Everything is allocated from mempools */
2247 mono_metadata_free_type (sig->ret);
2248 for (i = 0; i < sig->param_count; ++i) {
2249 if (sig->params [i])
2250 mono_metadata_free_type (sig->params [i]);
2256 mono_metadata_free_inflated_signature (MonoMethodSignature *sig)
2260 /* Allocated in inflate_generic_signature () */
2262 mono_metadata_free_type (sig->ret);
2263 for (i = 0; i < sig->param_count; ++i) {
2264 if (sig->params [i])
2265 mono_metadata_free_type (sig->params [i]);
2271 inflated_method_equal (gconstpointer a, gconstpointer b)
2273 const MonoMethodInflated *ma = (const MonoMethodInflated *)a;
2274 const MonoMethodInflated *mb = (const MonoMethodInflated *)b;
2275 if (ma->declaring != mb->declaring)
2277 return mono_metadata_generic_context_equal (&ma->context, &mb->context);
2281 inflated_method_hash (gconstpointer a)
2283 const MonoMethodInflated *ma = (const MonoMethodInflated *)a;
2284 return (mono_metadata_generic_context_hash (&ma->context) ^ mono_aligned_addr_hash (ma->declaring));
2288 inflated_signature_equal (gconstpointer a, gconstpointer b)
2290 const MonoInflatedMethodSignature *sig1 = (const MonoInflatedMethodSignature *)a;
2291 const MonoInflatedMethodSignature *sig2 = (const MonoInflatedMethodSignature *)b;
2293 /* sig->sig is assumed to be canonized */
2294 if (sig1->sig != sig2->sig)
2296 /* The generic instances are canonized */
2297 return mono_metadata_generic_context_equal (&sig1->context, &sig2->context);
2301 inflated_signature_hash (gconstpointer a)
2303 const MonoInflatedMethodSignature *sig = (const MonoInflatedMethodSignature *)a;
2305 /* sig->sig is assumed to be canonized */
2306 return mono_metadata_generic_context_hash (&sig->context) ^ mono_aligned_addr_hash (sig->sig);
2310 dump_ginst (MonoGenericInst *ginst)
2315 g_print ("Ginst: <");
2316 for (i = 0; i < ginst->type_argc; ++i) {
2319 name = mono_type_get_name (ginst->type_argv [i]);
2320 g_print ("%s", name);
2326 static gboolean type_in_image (MonoType *type, MonoImage *image);
2329 signature_in_image (MonoMethodSignature *sig, MonoImage *image)
2331 gpointer iter = NULL;
2334 while ((p = mono_signature_get_params (sig, &iter)) != NULL)
2335 if (type_in_image (p, image))
2338 return type_in_image (mono_signature_get_return_type (sig), image);
2342 ginst_in_image (MonoGenericInst *ginst, MonoImage *image)
2346 for (i = 0; i < ginst->type_argc; ++i) {
2347 if (type_in_image (ginst->type_argv [i], image))
2355 gclass_in_image (MonoGenericClass *gclass, MonoImage *image)
2357 return gclass->container_class->image == image ||
2358 ginst_in_image (gclass->context.class_inst, image);
2362 type_in_image (MonoType *type, MonoImage *image)
2365 switch (type->type) {
2366 case MONO_TYPE_GENERICINST:
2367 return gclass_in_image (type->data.generic_class, image);
2369 type = type->data.type;
2371 case MONO_TYPE_SZARRAY:
2372 type = &type->data.klass->byval_arg;
2374 case MONO_TYPE_ARRAY:
2375 type = &type->data.array->eklass->byval_arg;
2377 case MONO_TYPE_FNPTR:
2378 return signature_in_image (type->data.method, image);
2380 case MONO_TYPE_MVAR:
2381 return image == get_image_for_generic_param (type->data.generic_param);
2383 /* At this point, we should've avoided all potential allocations in mono_class_from_mono_type () */
2384 return image == mono_class_from_mono_type (type)->image;
2389 image_sets_lock (void)
2391 mono_os_mutex_lock (&image_sets_mutex);
2395 image_sets_unlock (void)
2397 mono_os_mutex_unlock (&image_sets_mutex);
2403 * Return a MonoImageSet representing the set of images in IMAGES.
2405 static MonoImageSet*
2406 get_image_set (MonoImage **images, int nimages)
2412 /* Common case: Image set contains corlib only. If we've seen that case before, we cached the set. */
2413 if (nimages == 1 && images [0] == mono_defaults.corlib && mscorlib_image_set)
2414 return mscorlib_image_set;
2416 /* Happens with empty generic instances */
2417 // 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.
2419 return mscorlib_image_set;
2424 image_sets = g_ptr_array_new ();
2426 // Before we go on, we should check to see whether a MonoImageSet with these images already exists.
2427 // We can search the referred-by imagesets of any one of our images to do this. Arbitrarily pick one here:
2428 if (images [0] == mono_defaults.corlib && nimages > 1)
2429 l = images [1]->image_sets; // Prefer not to search the imagesets of corlib-- that will be a long list.
2431 l = images [0]->image_sets;
2434 while (l) // Iterate over selected list, looking for an imageset with members equal to our target one
2436 set = (MonoImageSet *)l->data;
2438 if (set->nimages == nimages) { // Member count differs, this can't be it
2439 // Compare all members to all members-- order might be different
2440 for (j = 0; j < nimages; ++j) {
2441 for (k = 0; k < nimages; ++k)
2442 if (set->images [k] == images [j])
2443 break; // Break on match
2445 // If we iterated all the way through set->images, images[j] was *not* found.
2447 break; // Break on "image not found"
2450 // If we iterated all the way through images without breaking, all items in images were found in set->images
2452 break; // Break on "found a set with equal members"
2458 // If we iterated all the way through l without breaking, the imageset does not already exist and we shuold create it
2460 set = g_new0 (MonoImageSet, 1);
2461 set->nimages = nimages;
2462 set->images = g_new0 (MonoImage*, nimages);
2463 mono_os_mutex_init_recursive (&set->lock);
2464 for (i = 0; i < nimages; ++i)
2465 set->images [i] = images [i];
2466 set->gclass_cache = g_hash_table_new_full (mono_generic_class_hash, mono_generic_class_equal, NULL, (GDestroyNotify)free_generic_class);
2467 set->ginst_cache = g_hash_table_new_full (mono_metadata_generic_inst_hash, mono_metadata_generic_inst_equal, NULL, (GDestroyNotify)free_generic_inst);
2468 set->gmethod_cache = g_hash_table_new_full (inflated_method_hash, inflated_method_equal, NULL, (GDestroyNotify)free_inflated_method);
2469 set->gsignature_cache = g_hash_table_new_full (inflated_signature_hash, inflated_signature_equal, NULL, (GDestroyNotify)free_inflated_signature);
2471 for (i = 0; i < nimages; ++i)
2472 set->images [i]->image_sets = g_slist_prepend (set->images [i]->image_sets, set);
2474 g_ptr_array_add (image_sets, set);
2477 if (nimages == 1 && images [0] == mono_defaults.corlib) {
2478 mono_memory_barrier ();
2479 mscorlib_image_set = set;
2482 image_sets_unlock ();
2488 delete_image_set (MonoImageSet *set)
2492 g_hash_table_destroy (set->gclass_cache);
2493 g_hash_table_destroy (set->ginst_cache);
2494 g_hash_table_destroy (set->gmethod_cache);
2495 g_hash_table_destroy (set->gsignature_cache);
2497 mono_wrapper_caches_free (&set->wrapper_caches);
2501 for (i = 0; i < set->nimages; ++i)
2502 set->images [i]->image_sets = g_slist_remove (set->images [i]->image_sets, set);
2504 g_ptr_array_remove (image_sets, set);
2506 image_sets_unlock ();
2509 mono_mempool_destroy (set->mempool);
2510 g_free (set->images);
2511 mono_os_mutex_destroy (&set->lock);
2516 mono_image_set_lock (MonoImageSet *set)
2518 mono_os_mutex_lock (&set->lock);
2522 mono_image_set_unlock (MonoImageSet *set)
2524 mono_os_mutex_unlock (&set->lock);
2528 mono_image_set_alloc (MonoImageSet *set, guint size)
2532 mono_image_set_lock (set);
2534 set->mempool = mono_mempool_new_size (INITIAL_IMAGE_SET_SIZE);
2535 res = mono_mempool_alloc (set->mempool, size);
2536 mono_image_set_unlock (set);
2542 mono_image_set_alloc0 (MonoImageSet *set, guint size)
2546 mono_image_set_lock (set);
2548 set->mempool = mono_mempool_new_size (INITIAL_IMAGE_SET_SIZE);
2549 res = mono_mempool_alloc0 (set->mempool, size);
2550 mono_image_set_unlock (set);
2556 mono_image_set_strdup (MonoImageSet *set, const char *s)
2560 mono_image_set_lock (set);
2562 set->mempool = mono_mempool_new_size (INITIAL_IMAGE_SET_SIZE);
2563 res = mono_mempool_strdup (set->mempool, s);
2564 mono_image_set_unlock (set);
2569 // Get a descriptive string for a MonoImageSet
2570 // Callers are obligated to free buffer with g_free after use
2572 mono_image_set_description (MonoImageSet *set)
2574 GString *result = g_string_new (NULL);
2576 g_string_append (result, "[");
2577 for (img = 0; img < set->nimages; img++)
2580 g_string_append (result, ", ");
2581 g_string_append (result, set->images[img]->name);
2583 g_string_append (result, "]");
2584 return g_string_free (result, FALSE);
2588 * Structure used by the collect_..._images functions to store the image list.
2591 MonoImage *image_buf [64];
2593 int nimages, images_len;
2597 collect_data_init (CollectData *data)
2599 data->images = data->image_buf;
2600 data->images_len = 64;
2605 collect_data_free (CollectData *data)
2607 if (data->images != data->image_buf)
2608 g_free (data->images);
2612 enlarge_data (CollectData *data)
2614 int new_len = data->images_len < 16 ? 16 : data->images_len * 2;
2615 MonoImage **d = g_new (MonoImage *, new_len);
2618 g_assert_not_reached ();
2619 memcpy (d, data->images, data->images_len);
2620 if (data->images != data->image_buf)
2621 g_free (data->images);
2623 data->images_len = new_len;
2627 add_image (MonoImage *image, CollectData *data)
2631 /* The arrays are small, so use a linear search instead of a hash table */
2632 for (i = 0; i < data->nimages; ++i)
2633 if (data->images [i] == image)
2636 if (data->nimages == data->images_len)
2637 enlarge_data (data);
2639 data->images [data->nimages ++] = image;
2643 collect_type_images (MonoType *type, CollectData *data);
2646 collect_ginst_images (MonoGenericInst *ginst, CollectData *data)
2650 for (i = 0; i < ginst->type_argc; ++i) {
2651 collect_type_images (ginst->type_argv [i], data);
2656 collect_gclass_images (MonoGenericClass *gclass, CollectData *data)
2658 add_image (gclass->container_class->image, data);
2659 if (gclass->context.class_inst)
2660 collect_ginst_images (gclass->context.class_inst, data);
2664 collect_signature_images (MonoMethodSignature *sig, CollectData *data)
2666 gpointer iter = NULL;
2669 collect_type_images (mono_signature_get_return_type (sig), data);
2670 while ((p = mono_signature_get_params (sig, &iter)) != NULL)
2671 collect_type_images (p, data);
2675 collect_inflated_signature_images (MonoInflatedMethodSignature *sig, CollectData *data)
2677 collect_signature_images (sig->sig, data);
2678 if (sig->context.class_inst)
2679 collect_ginst_images (sig->context.class_inst, data);
2680 if (sig->context.method_inst)
2681 collect_ginst_images (sig->context.method_inst, data);
2685 collect_method_images (MonoMethodInflated *method, CollectData *data)
2687 MonoMethod *m = method->declaring;
2689 add_image (method->declaring->klass->image, data);
2690 if (method->context.class_inst)
2691 collect_ginst_images (method->context.class_inst, data);
2692 if (method->context.method_inst)
2693 collect_ginst_images (method->context.method_inst, data);
2695 * Dynamic assemblies have no references, so the images they depend on can be unloaded before them.
2697 if (image_is_dynamic (m->klass->image))
2698 collect_signature_images (mono_method_signature (m), data);
2702 collect_type_images (MonoType *type, CollectData *data)
2705 switch (type->type) {
2706 case MONO_TYPE_GENERICINST:
2707 collect_gclass_images (type->data.generic_class, data);
2710 type = type->data.type;
2712 case MONO_TYPE_SZARRAY:
2713 type = &type->data.klass->byval_arg;
2715 case MONO_TYPE_ARRAY:
2716 type = &type->data.array->eklass->byval_arg;
2718 case MONO_TYPE_FNPTR:
2719 //return signature_in_image (type->data.method, image);
2720 g_assert_not_reached ();
2722 case MONO_TYPE_MVAR:
2724 MonoImage *image = get_image_for_generic_param (type->data.generic_param);
2725 add_image (image, data);
2728 case MONO_TYPE_CLASS:
2729 case MONO_TYPE_VALUETYPE:
2730 add_image (mono_class_from_mono_type (type)->image, data);
2733 add_image (mono_defaults.corlib, data);
2740 } CleanForImageUserData;
2743 steal_gclass_in_image (gpointer key, gpointer value, gpointer data)
2745 MonoGenericClass *gclass = (MonoGenericClass *)key;
2746 CleanForImageUserData *user_data = (CleanForImageUserData *)data;
2748 g_assert (gclass_in_image (gclass, user_data->image));
2750 user_data->list = g_slist_prepend (user_data->list, gclass);
2755 steal_ginst_in_image (gpointer key, gpointer value, gpointer data)
2757 MonoGenericInst *ginst = (MonoGenericInst *)key;
2758 CleanForImageUserData *user_data = (CleanForImageUserData *)data;
2760 // This doesn't work during corlib compilation
2761 //g_assert (ginst_in_image (ginst, user_data->image));
2763 user_data->list = g_slist_prepend (user_data->list, ginst);
2768 inflated_method_in_image (gpointer key, gpointer value, gpointer data)
2770 MonoImage *image = (MonoImage *)data;
2771 MonoMethodInflated *method = (MonoMethodInflated *)key;
2774 // https://bugzilla.novell.com/show_bug.cgi?id=458168
2775 g_assert (method->declaring->klass->image == image ||
2776 (method->context.class_inst && ginst_in_image (method->context.class_inst, image)) ||
2777 (method->context.method_inst && ginst_in_image (method->context.method_inst, image)) || (((MonoMethod*)method)->signature && signature_in_image (mono_method_signature ((MonoMethod*)method), image)));
2783 inflated_signature_in_image (gpointer key, gpointer value, gpointer data)
2785 MonoImage *image = (MonoImage *)data;
2786 MonoInflatedMethodSignature *sig = (MonoInflatedMethodSignature *)key;
2788 return signature_in_image (sig->sig, image) ||
2789 (sig->context.class_inst && ginst_in_image (sig->context.class_inst, image)) ||
2790 (sig->context.method_inst && ginst_in_image (sig->context.method_inst, image));
2794 check_gmethod (gpointer key, gpointer value, gpointer data)
2796 MonoMethodInflated *method = (MonoMethodInflated *)key;
2797 MonoImage *image = (MonoImage *)data;
2799 if (method->context.class_inst)
2800 g_assert (!ginst_in_image (method->context.class_inst, image));
2801 if (method->context.method_inst)
2802 g_assert (!ginst_in_image (method->context.method_inst, image));
2803 if (((MonoMethod*)method)->signature)
2804 g_assert (!signature_in_image (mono_method_signature ((MonoMethod*)method), image));
2810 * Run a consistency check on the image set data structures.
2812 static G_GNUC_UNUSED void
2813 check_image_sets (MonoImage *image)
2816 GSList *l = image->image_sets;
2821 for (i = 0; i < image_sets->len; ++i) {
2822 MonoImageSet *set = (MonoImageSet *)g_ptr_array_index (image_sets, i);
2824 if (!g_slist_find (l, set)) {
2825 g_hash_table_foreach (set->gmethod_cache, check_gmethod, image);
2831 mono_metadata_clean_for_image (MonoImage *image)
2833 CleanForImageUserData ginst_data, gclass_data;
2834 GSList *l, *set_list;
2836 //check_image_sets (image);
2839 * The data structures could reference each other so we delete them in two phases.
2840 * This is required because of the hashing functions in gclass/ginst_cache.
2842 ginst_data.image = gclass_data.image = image;
2843 ginst_data.list = gclass_data.list = NULL;
2845 /* Collect the items to delete */
2846 /* delete_image_set () modifies the lists so make a copy */
2847 for (l = image->image_sets; l; l = l->next) {
2848 MonoImageSet *set = (MonoImageSet *)l->data;
2850 mono_image_set_lock (set);
2851 g_hash_table_foreach_steal (set->gclass_cache, steal_gclass_in_image, &gclass_data);
2852 g_hash_table_foreach_steal (set->ginst_cache, steal_ginst_in_image, &ginst_data);
2853 g_hash_table_foreach_remove (set->gmethod_cache, inflated_method_in_image, image);
2854 g_hash_table_foreach_remove (set->gsignature_cache, inflated_signature_in_image, image);
2855 mono_image_set_unlock (set);
2858 /* Delete the removed items */
2859 for (l = ginst_data.list; l; l = l->next)
2860 free_generic_inst ((MonoGenericInst *)l->data);
2861 for (l = gclass_data.list; l; l = l->next)
2862 free_generic_class ((MonoGenericClass *)l->data);
2863 g_slist_free (ginst_data.list);
2864 g_slist_free (gclass_data.list);
2865 /* delete_image_set () modifies the lists so make a copy */
2866 set_list = g_slist_copy (image->image_sets);
2867 for (l = set_list; l; l = l->next) {
2868 MonoImageSet *set = (MonoImageSet *)l->data;
2870 delete_image_set (set);
2872 g_slist_free (set_list);
2876 free_inflated_method (MonoMethodInflated *imethod)
2878 MonoMethod *method = (MonoMethod*)imethod;
2880 if (method->signature)
2881 mono_metadata_free_inflated_signature (method->signature);
2883 if (method->wrapper_type)
2884 g_free (((MonoMethodWrapper*)method)->method_data);
2890 free_generic_inst (MonoGenericInst *ginst)
2894 /* The ginst itself is allocated from the image set mempool */
2895 for (i = 0; i < ginst->type_argc; ++i)
2896 mono_metadata_free_type (ginst->type_argv [i]);
2900 free_generic_class (MonoGenericClass *gclass)
2902 /* The gclass itself is allocated from the image set mempool */
2903 if (gclass->cached_class && gclass->cached_class->interface_id)
2904 mono_unload_interface_id (gclass->cached_class);
2908 free_inflated_signature (MonoInflatedMethodSignature *sig)
2910 mono_metadata_free_inflated_signature (sig->sig);
2915 * mono_metadata_get_inflated_signature:
2917 * Given an inflated signature and a generic context, return a canonical copy of the
2918 * signature. The returned signature might be equal to SIG or it might be a cached copy.
2920 MonoMethodSignature *
2921 mono_metadata_get_inflated_signature (MonoMethodSignature *sig, MonoGenericContext *context)
2923 MonoInflatedMethodSignature helper;
2924 MonoInflatedMethodSignature *res;
2929 helper.context.class_inst = context->class_inst;
2930 helper.context.method_inst = context->method_inst;
2932 collect_data_init (&data);
2934 collect_inflated_signature_images (&helper, &data);
2936 set = get_image_set (data.images, data.nimages);
2938 collect_data_free (&data);
2940 mono_image_set_lock (set);
2942 res = (MonoInflatedMethodSignature *)g_hash_table_lookup (set->gsignature_cache, &helper);
2944 res = g_new0 (MonoInflatedMethodSignature, 1);
2946 res->context.class_inst = context->class_inst;
2947 res->context.method_inst = context->method_inst;
2948 g_hash_table_insert (set->gsignature_cache, res, res);
2951 mono_image_set_unlock (set);
2957 mono_metadata_get_image_set_for_method (MonoMethodInflated *method)
2960 CollectData image_set_data;
2962 collect_data_init (&image_set_data);
2963 collect_method_images (method, &image_set_data);
2964 set = get_image_set (image_set_data.images, image_set_data.nimages);
2965 collect_data_free (&image_set_data);
2971 * mono_metadata_get_generic_inst:
2973 * Given a list of types, return a MonoGenericInst that represents that list.
2974 * The returned MonoGenericInst has its own copy of the list of types. The list
2975 * passed in the argument can be freed, modified or disposed of.
2979 mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv)
2981 MonoGenericInst *ginst;
2984 int size = MONO_SIZEOF_GENERIC_INST + type_argc * sizeof (MonoType *);
2986 for (i = 0; i < type_argc; ++i)
2987 if (mono_class_is_open_constructed_type (type_argv [i]))
2989 is_open = (i < type_argc);
2991 ginst = (MonoGenericInst *)g_alloca (size);
2992 memset (ginst, 0, sizeof (MonoGenericInst));
2993 ginst->is_open = is_open;
2994 ginst->type_argc = type_argc;
2995 memcpy (ginst->type_argv, type_argv, type_argc * sizeof (MonoType *));
2997 return mono_metadata_get_canonical_generic_inst (ginst);
3002 * mono_metadata_get_canonical_generic_inst:
3003 * @candidate: an arbitrary generic instantiation
3005 * Returns the canonical generic instantiation that represents the given
3006 * candidate by identifying the image set for the candidate instantiation and
3007 * finding the instance in the image set or adding a copy of the given instance
3010 * The returned MonoGenericInst has its own copy of the list of types. The list
3011 * passed in the argument can be freed, modified or disposed of.
3015 mono_metadata_get_canonical_generic_inst (MonoGenericInst *candidate)
3018 int type_argc = candidate->type_argc;
3019 gboolean is_open = candidate->is_open;
3022 collect_data_init (&data);
3024 collect_ginst_images (candidate, &data);
3026 set = get_image_set (data.images, data.nimages);
3028 collect_data_free (&data);
3030 mono_image_set_lock (set);
3032 MonoGenericInst *ginst = (MonoGenericInst *)g_hash_table_lookup (set->ginst_cache, candidate);
3034 int size = MONO_SIZEOF_GENERIC_INST + type_argc * sizeof (MonoType *);
3035 ginst = (MonoGenericInst *)mono_image_set_alloc0 (set, size);
3036 #ifndef MONO_SMALL_CONFIG
3037 ginst->id = ++next_generic_inst_id;
3039 ginst->is_open = is_open;
3040 ginst->type_argc = type_argc;
3042 for (int i = 0; i < type_argc; ++i)
3043 ginst->type_argv [i] = mono_metadata_type_dup (NULL, candidate->type_argv [i]);
3045 g_hash_table_insert (set->ginst_cache, ginst, ginst);
3048 mono_image_set_unlock (set);
3053 mono_metadata_is_type_builder_generic_type_definition (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
3055 MonoGenericContainer *container = mono_class_get_generic_container (container_class);
3057 if (!is_dynamic || container_class->wastypebuilder || container->type_argc != inst->type_argc)
3059 return inst == container->context.class_inst;
3063 * mono_metadata_lookup_generic_class:
3065 * Returns a MonoGenericClass with the given properties.
3069 mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
3071 MonoGenericClass *gclass;
3072 MonoGenericClass helper;
3073 gboolean is_tb_open = mono_metadata_is_type_builder_generic_type_definition (container_class, inst, is_dynamic);
3077 g_assert (mono_class_get_generic_container (container_class)->type_argc == inst->type_argc);
3079 memset (&helper, 0, sizeof(helper)); // act like g_new0
3080 helper.container_class = container_class;
3081 helper.context.class_inst = inst;
3082 helper.is_dynamic = is_dynamic; /* We use this in a hash lookup, which does not attempt to downcast the pointer */
3083 helper.is_tb_open = is_tb_open;
3085 collect_data_init (&data);
3087 collect_gclass_images (&helper, &data);
3089 set = get_image_set (data.images, data.nimages);
3091 collect_data_free (&data);
3093 mono_image_set_lock (set);
3095 gclass = (MonoGenericClass *)g_hash_table_lookup (set->gclass_cache, &helper);
3097 /* A tripwire just to keep us honest */
3098 g_assert (!helper.cached_class);
3101 mono_image_set_unlock (set);
3105 gclass = mono_image_set_new0 (set, MonoGenericClass, 1);
3107 gclass->is_dynamic = 1;
3109 gclass->is_tb_open = is_tb_open;
3110 gclass->container_class = container_class;
3111 gclass->context.class_inst = inst;
3112 gclass->context.method_inst = NULL;
3113 gclass->owner = set;
3114 if (inst == mono_class_get_generic_container (container_class)->context.class_inst && !is_tb_open)
3115 gclass->cached_class = container_class;
3117 g_hash_table_insert (set->gclass_cache, gclass, gclass);
3119 mono_image_set_unlock (set);
3125 * mono_metadata_inflate_generic_inst:
3127 * Instantiate the generic instance @ginst with the context @context.
3128 * Check @error for success.
3132 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context, MonoError *error)
3134 MonoType **type_argv;
3135 MonoGenericInst *nginst = NULL;
3140 if (!ginst->is_open)
3143 type_argv = g_new0 (MonoType*, ginst->type_argc);
3145 for (i = 0; i < ginst->type_argc; i++) {
3146 type_argv [i] = mono_class_inflate_generic_type_checked (ginst->type_argv [i], context, error);
3147 if (!mono_error_ok (error))
3152 nginst = mono_metadata_get_generic_inst (ginst->type_argc, type_argv);
3155 for (i = 0; i < count; i++)
3156 mono_metadata_free_type (type_argv [i]);
3163 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
3164 int count, const char *ptr, const char **rptr, MonoError *error)
3166 MonoType **type_argv;
3167 MonoGenericInst *ginst;
3171 type_argv = g_new0 (MonoType*, count);
3173 for (i = 0; i < count; i++) {
3174 MonoType *t = mono_metadata_parse_type_checked (m, container, 0, FALSE, ptr, &ptr, error);
3185 ginst = mono_metadata_get_generic_inst (count, type_argv);
3193 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
3194 const char *ptr, const char **rptr, MonoError *error)
3196 MonoGenericInst *inst;
3203 // XXX how about transient?
3204 gtype = mono_metadata_parse_type_checked (m, NULL, 0, FALSE, ptr, &ptr, error);
3208 gklass = mono_class_from_mono_type (gtype);
3209 if (!mono_class_is_gtd (gklass)) {
3210 mono_error_set_bad_image (error, m, "Generic instance with non-generic definition");
3214 count = mono_metadata_decode_value (ptr, &ptr);
3215 inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr, error);
3222 type->data.generic_class = mono_metadata_lookup_generic_class (gklass, inst, FALSE);
3228 * @gc: The generic container to normalize
3229 * @type: The kind of generic parameters the resulting generic-container should contain
3232 static MonoGenericContainer *
3233 select_container (MonoGenericContainer *gc, MonoTypeEnum type)
3235 gboolean is_var = (type == MONO_TYPE_VAR);
3239 g_assert (is_var || type == MONO_TYPE_MVAR);
3242 if (gc->is_method || gc->parent)
3244 * The current MonoGenericContainer is a generic method -> its `parent'
3245 * points to the containing class'es container.
3253 MonoGenericContainer *
3254 get_anonymous_container_for_image (MonoImage *image, gboolean is_mvar)
3256 MonoGenericContainer **container_pointer;
3258 container_pointer = &image->anonymous_generic_method_container;
3260 container_pointer = &image->anonymous_generic_class_container;
3261 MonoGenericContainer *result = *container_pointer;
3263 // This container has never been created; make it now.
3266 // Note this is never deallocated anywhere-- it exists for the lifetime of the image it's allocated from
3267 result = (MonoGenericContainer *)mono_image_alloc0 (image, sizeof (MonoGenericContainer));
3268 result->owner.image = image;
3269 result->is_anonymous = TRUE;
3270 result->is_small_param = TRUE;
3271 result->is_method = is_mvar;
3273 // If another thread already made a container, use that and leak this new one.
3274 // (Technically it would currently be safe to just assign instead of CASing.)
3275 MonoGenericContainer *exchange = (MonoGenericContainer *)InterlockedCompareExchangePointer ((volatile gpointer *)container_pointer, result, NULL);
3283 * mono_metadata_parse_generic_param:
3284 * @generic_container: Our MonoClass's or MonoMethod's MonoGenericContainer;
3285 * see mono_metadata_parse_type_checked() for details.
3286 * Internal routine to parse a generic type parameter.
3287 * LOCKING: Acquires the loader lock
3289 static MonoGenericParam *
3290 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
3291 MonoTypeEnum type, const char *ptr, const char **rptr, MonoError *error)
3293 int index = mono_metadata_decode_value (ptr, &ptr);
3299 generic_container = select_container (generic_container, type);
3300 if (!generic_container) {
3301 gboolean is_mvar = FALSE;
3306 case MONO_TYPE_MVAR:
3310 g_error ("Cerating generic param object with invalid MonoType"); // This is not a generic param
3313 /* Create dummy MonoGenericParam */
3314 MonoGenericParam *param;
3316 param = (MonoGenericParam *)mono_image_alloc0 (m, sizeof (MonoGenericParam));
3318 param->owner = get_anonymous_container_for_image (m, is_mvar);
3323 if (index >= generic_container->type_argc) {
3324 mono_error_set_bad_image (error, m, "Invalid generic %s parameter index %d, max index is %d",
3325 generic_container->is_method ? "method" : "type",
3326 index, generic_container->type_argc);
3330 //This can't return NULL
3331 return mono_generic_container_get_param (generic_container, index);
3335 * mono_metadata_get_shared_type:
3337 * Return a shared instance of TYPE, if available, NULL otherwise.
3338 * Shared MonoType instances help save memory. Their contents should not be modified
3339 * by the caller. They do not need to be freed as their lifetime is bound by either
3340 * the lifetime of the runtime (builtin types), or the lifetime of the MonoClass
3341 * instance they are embedded in. If they are freed, they should be freed using
3342 * mono_metadata_free_type () instead of g_free ().
3345 mono_metadata_get_shared_type (MonoType *type)
3349 /* No need to use locking since nobody is modifying the hash table */
3350 if ((cached = (MonoType *)g_hash_table_lookup (type_cache, type)))
3353 switch (type->type){
3354 case MONO_TYPE_CLASS:
3355 case MONO_TYPE_VALUETYPE:
3356 if (type == &type->data.klass->byval_arg)
3358 if (type == &type->data.klass->this_arg)
3369 compare_type_literals (MonoImage *image, int class_type, int type_type, MonoError *error)
3373 /* byval_arg.type can be zero if we're decoding a type that references a class been loading.
3374 * See mcs/test/gtest-440. and #650936.
3375 * FIXME This better be moved to the metadata verifier as it can catch more cases.
3379 /* NET 1.1 assemblies might encode string and object in a denormalized way.
3382 if (class_type == type_type)
3385 if (type_type == MONO_TYPE_CLASS) {
3386 if (class_type == MONO_TYPE_STRING || class_type == MONO_TYPE_OBJECT)
3388 //XXX stringify this argument
3389 mono_error_set_bad_image (error, image, "Expected reference type but got type kind %d", class_type);
3393 g_assert (type_type == MONO_TYPE_VALUETYPE);
3394 switch (class_type) {
3395 case MONO_TYPE_BOOLEAN:
3396 case MONO_TYPE_CHAR:
3409 case MONO_TYPE_CLASS:
3412 //XXX stringify this argument
3413 mono_error_set_bad_image (error, image, "Expected value type but got type kind %d", class_type);
3419 verify_var_type_and_container (MonoImage *image, int var_type, MonoGenericContainer *container, MonoError *error)
3422 if (var_type == MONO_TYPE_MVAR) {
3423 if (!container->is_method) { //MVAR and a method container
3424 mono_error_set_bad_image (error, image, "MVAR parsed in a context without a method container");
3428 if (!(!container->is_method || //VAR and class container
3429 (container->is_method && container->parent))) { //VAR and method container with parent
3430 mono_error_set_bad_image (error, image, "VAR parsed in a context without a class container");
3438 * do_mono_metadata_parse_type:
3439 * @type: MonoType to be filled in with the return value
3441 * @generic_context: generics_context
3442 * @transient: whenever to allocate data from the heap
3443 * @ptr: pointer to the encoded type
3444 * @rptr: pointer where the end of the encoded type is saved
3446 * Internal routine used to "fill" the contents of @type from an
3447 * allocated pointer. This is done this way to avoid doing too
3448 * many mini-allocations (particularly for the MonoFieldType which
3449 * most of the time is just a MonoType, but sometimes might be augmented).
3451 * This routine is used by mono_metadata_parse_type and
3452 * mono_metadata_parse_field_type
3454 * This extracts a Type as specified in Partition II (22.2.12)
3456 * Returns: FALSE if the type could not be loaded
3459 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
3460 gboolean transient, const char *ptr, const char **rptr, MonoError *error)
3464 type->type = (MonoTypeEnum)mono_metadata_decode_value (ptr, &ptr);
3466 switch (type->type){
3467 case MONO_TYPE_VOID:
3468 case MONO_TYPE_BOOLEAN:
3469 case MONO_TYPE_CHAR:
3482 case MONO_TYPE_STRING:
3483 case MONO_TYPE_OBJECT:
3484 case MONO_TYPE_TYPEDBYREF:
3486 case MONO_TYPE_VALUETYPE:
3487 case MONO_TYPE_CLASS: {
3490 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
3491 klass = mono_class_get_checked (m, token, error);
3492 type->data.klass = klass;
3496 if (!compare_type_literals (m, klass->byval_arg.type, type->type, error))
3501 case MONO_TYPE_SZARRAY: {
3502 MonoType *etype = mono_metadata_parse_type_checked (m, container, 0, transient, ptr, &ptr, error);
3506 type->data.klass = mono_class_from_mono_type (etype);
3509 mono_metadata_free_type (etype);
3511 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.
3514 case MONO_TYPE_PTR: {
3515 type->data.type = mono_metadata_parse_type_checked (m, container, 0, transient, ptr, &ptr, error);
3516 if (!type->data.type)
3520 case MONO_TYPE_FNPTR: {
3521 type->data.method = mono_metadata_parse_method_signature_full (m, container, 0, ptr, &ptr, error);
3522 if (!type->data.method)
3526 case MONO_TYPE_ARRAY: {
3527 type->data.array = mono_metadata_parse_array_internal (m, container, transient, ptr, &ptr, error);
3528 if (!type->data.array)
3532 case MONO_TYPE_MVAR:
3533 case MONO_TYPE_VAR: {
3534 if (container && !verify_var_type_and_container (m, type->type, container, error))
3537 type->data.generic_param = mono_metadata_parse_generic_param (m, container, type->type, ptr, &ptr, error);
3538 if (!type->data.generic_param)
3543 case MONO_TYPE_GENERICINST: {
3544 if (!do_mono_metadata_parse_generic_class (type, m, container, ptr, &ptr, error))
3549 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);
3559 * mono_metadata_free_type:
3560 * @type: type to free
3562 * Free the memory allocated for type @type which is allocated on the heap.
3565 mono_metadata_free_type (MonoType *type)
3567 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
3570 switch (type->type){
3571 case MONO_TYPE_OBJECT:
3572 case MONO_TYPE_STRING:
3573 if (!type->data.klass)
3576 case MONO_TYPE_CLASS:
3577 case MONO_TYPE_VALUETYPE:
3578 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
3582 mono_metadata_free_type (type->data.type);
3584 case MONO_TYPE_FNPTR:
3585 mono_metadata_free_method_signature (type->data.method);
3587 case MONO_TYPE_ARRAY:
3588 mono_metadata_free_array (type->data.array);
3599 hex_dump (const char *buffer, int base, int count)
3601 int show_header = 1;
3609 for (i = 0; i < count; i++){
3612 printf ("\n0x%08x: ", (unsigned char) base + i);
3614 printf ("%02x ", (unsigned char) (buffer [i]));
3621 * @ptr: Points to the beginning of the Section Data (25.3)
3623 static MonoExceptionClause*
3624 parse_section_data (MonoImage *m, int *num_clauses, const unsigned char *ptr, MonoError *error)
3626 unsigned char sect_data_flags;
3628 guint32 sect_data_len;
3629 MonoExceptionClause* clauses = NULL;
3634 /* align on 32-bit boundary */
3635 ptr = dword_align (ptr);
3636 sect_data_flags = *ptr;
3639 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
3641 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
3644 sect_data_len = ptr [0];
3648 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
3649 const unsigned char *p = dword_align (ptr);
3651 *num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
3652 /* we could just store a pointer if we don't need to byteswap */
3653 clauses = (MonoExceptionClause *)g_malloc0 (sizeof (MonoExceptionClause) * (*num_clauses));
3654 for (i = 0; i < *num_clauses; ++i) {
3655 MonoExceptionClause *ec = &clauses [i];
3658 ec->flags = read32 (p);
3659 ec->try_offset = read32 (p + 4);
3660 ec->try_len = read32 (p + 8);
3661 ec->handler_offset = read32 (p + 12);
3662 ec->handler_len = read32 (p + 16);
3663 tof_value = read32 (p + 20);
3666 ec->flags = read16 (p);
3667 ec->try_offset = read16 (p + 2);
3668 ec->try_len = *(p + 4);
3669 ec->handler_offset = read16 (p + 5);
3670 ec->handler_len = *(p + 7);
3671 tof_value = read32 (p + 8);
3674 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3675 ec->data.filter_offset = tof_value;
3676 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
3677 ec->data.catch_class = NULL;
3679 ec->data.catch_class = mono_class_get_checked (m, tof_value, error);
3680 if (!is_ok (error)) {
3686 ec->data.catch_class = NULL;
3688 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
3692 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
3693 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
3700 * mono_method_get_header_summary:
3701 * @method: The method to get the header.
3702 * @summary: Where to store the header
3705 * Returns: TRUE if the header was properly decoded.
3708 mono_method_get_header_summary (MonoMethod *method, MonoMethodHeaderSummary *summary)
3714 unsigned char flags, format;
3717 /*Only the GMD has a pointer to the metadata.*/
3718 while (method->is_inflated)
3719 method = ((MonoMethodInflated*)method)->declaring;
3721 summary->code_size = 0;
3722 summary->has_clauses = FALSE;
3724 /*FIXME extract this into a MACRO and share it with mono_method_get_header*/
3725 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))
3728 if (method->wrapper_type != MONO_WRAPPER_NONE || method->sre_method) {
3729 MonoMethodHeader *header = ((MonoMethodWrapper *)method)->header;
3732 summary->code_size = header->code_size;
3733 summary->has_clauses = header->num_clauses > 0;
3738 idx = mono_metadata_token_index (method->token);
3739 img = method->klass->image;
3740 rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
3742 /*We must run the verifier since we'll be decoding it.*/
3743 if (!mono_verifier_verify_method_header (img, rva, NULL))
3746 ptr = mono_image_rva_map (img, rva);
3750 flags = *(const unsigned char *)ptr;
3751 format = flags & METHOD_HEADER_FORMAT_MASK;
3754 case METHOD_HEADER_TINY_FORMAT:
3756 summary->code_size = flags >> 2;
3758 case METHOD_HEADER_FAT_FORMAT:
3759 fat_flags = read16 (ptr);
3761 summary->code_size = read32 (ptr);
3762 if (fat_flags & METHOD_HEADER_MORE_SECTS)
3763 summary->has_clauses = TRUE;
3772 * mono_metadata_parse_mh_full:
3773 * @m: metadata context
3774 * @generic_context: generics context
3775 * @ptr: pointer to the method header.
3777 * Decode the method header at @ptr, including pointer to the IL code,
3778 * info about local variables and optional exception tables.
3779 * This is a Mono runtime internal function.
3781 * LOCKING: Acquires the loader lock.
3783 * Returns: a transient MonoMethodHeader allocated from the heap.
3786 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, const char *ptr, MonoError *error)
3788 MonoMethodHeader *mh = NULL;
3789 unsigned char flags = *(const unsigned char *) ptr;
3790 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
3792 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
3793 const unsigned char *code;
3794 MonoExceptionClause* clauses = NULL;
3795 int num_clauses = 0;
3796 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
3797 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
3802 mono_error_set_bad_image (error, m, "Method header with null pointer");
3807 case METHOD_HEADER_TINY_FORMAT:
3808 mh = (MonoMethodHeader *)g_malloc0 (MONO_SIZEOF_METHOD_HEADER);
3811 mh->is_transient = TRUE;
3812 local_var_sig_tok = 0;
3813 mh->code_size = flags >> 2;
3814 mh->code = (unsigned char*)ptr;
3816 case METHOD_HEADER_FAT_FORMAT:
3817 fat_flags = read16 (ptr);
3819 max_stack = read16 (ptr);
3821 code_size = read32 (ptr);
3823 local_var_sig_tok = read32 (ptr);
3826 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
3831 code = (unsigned char*)ptr;
3833 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
3837 * There are more sections
3839 ptr = (char*)code + code_size;
3842 mono_error_set_bad_image (error, m, "Invalid method header format %d", format);
3846 if (local_var_sig_tok) {
3847 int idx = (local_var_sig_tok & 0xffffff)-1;
3848 if (idx >= t->rows || idx < 0) {
3849 mono_error_set_bad_image (error, m, "Invalid method header local vars signature token 0x%8x", idx);
3852 mono_metadata_decode_row (t, idx, cols, 1);
3854 if (!mono_verifier_verify_standalone_signature (m, cols [MONO_STAND_ALONE_SIGNATURE], NULL)) {
3855 mono_error_set_bad_image (error, m, "Method header locals signature 0x%8x verification failed", idx);
3859 if (fat_flags & METHOD_HEADER_MORE_SECTS) {
3860 clauses = parse_section_data (m, &num_clauses, (const unsigned char*)ptr, error);
3864 if (local_var_sig_tok) {
3865 const char *locals_ptr;
3868 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
3869 mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
3870 if (*locals_ptr != 0x07)
3871 g_warning ("wrong signature for locals blob");
3873 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
3874 mh = (MonoMethodHeader *)g_malloc0 (MONO_SIZEOF_METHOD_HEADER + len * sizeof (MonoType*) + num_clauses * sizeof (MonoExceptionClause));
3875 mh->num_locals = len;
3876 for (i = 0; i < len; ++i) {
3877 mh->locals [i] = mono_metadata_parse_type_internal (m, container, 0, TRUE, locals_ptr, &locals_ptr, error);
3882 mh = (MonoMethodHeader *)g_malloc0 (MONO_SIZEOF_METHOD_HEADER + num_clauses * sizeof (MonoExceptionClause));
3885 mh->code_size = code_size;
3886 mh->max_stack = max_stack;
3887 mh->is_transient = TRUE;
3888 mh->init_locals = init_locals;
3890 MonoExceptionClause* clausesp = (MonoExceptionClause*)&mh->locals [mh->num_locals];
3891 memcpy (clausesp, clauses, num_clauses * sizeof (MonoExceptionClause));
3893 mh->clauses = clausesp;
3894 mh->num_clauses = num_clauses;
3905 * mono_metadata_parse_mh:
3906 * @generic_context: generics context
3907 * @ptr: pointer to the method header.
3909 * Decode the method header at @ptr, including pointer to the IL code,
3910 * info about local variables and optional exception tables.
3912 * Returns: a transient MonoMethodHeader allocated from the heap.
3915 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
3918 MonoMethodHeader *header = mono_metadata_parse_mh_full (m, NULL, ptr, &error);
3919 mono_error_cleanup (&error);
3924 * mono_metadata_free_mh:
3925 * @mh: a method header
3927 * Free the memory allocated for the method header.
3930 mono_metadata_free_mh (MonoMethodHeader *mh)
3934 /* If it is not transient it means it's part of a wrapper method,
3935 * or a SRE-generated method, so the lifetime in that case is
3936 * dictated by the method's own lifetime
3938 if (mh->is_transient) {
3939 for (i = 0; i < mh->num_locals; ++i)
3940 mono_metadata_free_type (mh->locals [i]);
3946 * mono_method_header_get_code:
3947 * @header: a MonoMethodHeader pointer
3948 * @code_size: memory location for returning the code size
3949 * @max_stack: memory location for returning the max stack
3951 * Method header accessor to retreive info about the IL code properties:
3952 * a pointer to the IL code itself, the size of the code and the max number
3953 * of stack slots used by the code.
3955 * Returns: pointer to the IL code represented by the method header.
3957 const unsigned char*
3958 mono_method_header_get_code (MonoMethodHeader *header, guint32* code_size, guint32* max_stack)
3961 *code_size = header->code_size;
3963 *max_stack = header->max_stack;
3964 return header->code;
3968 * mono_method_header_get_locals:
3969 * @header: a MonoMethodHeader pointer
3970 * @num_locals: memory location for returning the number of local variables
3971 * @init_locals: memory location for returning the init_locals flag
3973 * Method header accessor to retreive info about the local variables:
3974 * an array of local types, the number of locals and whether the locals
3975 * are supposed to be initialized to 0 on method entry
3977 * Returns: pointer to an array of types of the local variables
3980 mono_method_header_get_locals (MonoMethodHeader *header, guint32* num_locals, gboolean *init_locals)
3983 *num_locals = header->num_locals;
3985 *init_locals = header->init_locals;
3986 return header->locals;
3990 * mono_method_header_get_num_clauses:
3991 * @header: a MonoMethodHeader pointer
3993 * Method header accessor to retreive the number of exception clauses.
3995 * Returns: the number of exception clauses present
3998 mono_method_header_get_num_clauses (MonoMethodHeader *header)
4000 return header->num_clauses;
4004 * mono_method_header_get_clauses:
4005 * @header: a MonoMethodHeader pointer
4006 * @method: MonoMethod the header belongs to
4007 * @iter: pointer to a iterator
4008 * @clause: pointer to a MonoExceptionClause structure which will be filled with the info
4010 * Get the info about the exception clauses in the method. Set *iter to NULL to
4011 * initiate the iteration, then call the method repeatedly until it returns FALSE.
4012 * At each iteration, the structure pointed to by clause if filled with the
4013 * exception clause information.
4015 * Returns: TRUE if clause was filled with info, FALSE if there are no more exception
4019 mono_method_header_get_clauses (MonoMethodHeader *header, MonoMethod *method, gpointer *iter, MonoExceptionClause *clause)
4021 MonoExceptionClause *sc;
4022 /* later we'll be able to use this interface to parse the clause info on demand,
4023 * without allocating anything.
4025 if (!iter || !header->num_clauses)
4028 *iter = sc = header->clauses;
4032 sc = (MonoExceptionClause *)*iter;
4034 if (sc < header->clauses + header->num_clauses) {
4043 * mono_metadata_parse_field_type:
4044 * @m: metadata context to extract information from
4045 * @ptr: pointer to the field signature
4046 * @rptr: pointer updated to match the end of the decoded stream
4048 * Parses the field signature, and returns the type information for it.
4050 * Returns: The MonoType that was extracted from @ptr.
4053 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
4056 MonoType * type = mono_metadata_parse_type_internal (m, NULL, field_flags, FALSE, ptr, rptr, &error);
4057 mono_error_cleanup (&error);
4062 * mono_metadata_parse_param:
4063 * @m: metadata context to extract information from
4064 * @ptr: pointer to the param signature
4065 * @rptr: pointer updated to match the end of the decoded stream
4067 * Parses the param signature, and returns the type information for it.
4069 * Returns: The MonoType that was extracted from @ptr.
4072 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
4075 MonoType * type = mono_metadata_parse_type_internal (m, NULL, 0, FALSE, ptr, rptr, &error);
4076 mono_error_cleanup (&error);
4081 * mono_metadata_token_from_dor:
4082 * @dor_token: A TypeDefOrRef coded index
4084 * dor_token is a TypeDefOrRef coded index: it contains either
4085 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
4086 * bits contain an index into the table.
4088 * Returns: an expanded token
4091 mono_metadata_token_from_dor (guint32 dor_index)
4095 table = dor_index & 0x03;
4096 idx = dor_index >> 2;
4099 case 0: /* TypeDef */
4100 return MONO_TOKEN_TYPE_DEF | idx;
4101 case 1: /* TypeRef */
4102 return MONO_TOKEN_TYPE_REF | idx;
4103 case 2: /* TypeSpec */
4104 return MONO_TOKEN_TYPE_SPEC | idx;
4106 g_assert_not_reached ();
4113 * We use this to pass context information to the row locator
4116 int idx; /* The index that we are trying to locate */
4117 int col_idx; /* The index in the row where idx may be stored */
4118 MonoTableInfo *t; /* pointer to the table */
4123 * How the row locator works.
4128 * ___|___------> _______
4131 * A column in the rows of table A references an index in table B.
4132 * For example A may be the TYPEDEF table and B the METHODDEF table.
4134 * Given an index in table B we want to get the row in table A
4135 * where the column n references our index in B.
4137 * In the locator_t structure:
4139 * col_idx is the column number
4140 * index is the index in table B
4141 * result will be the index in table A
4144 * Table A Table B column (in table A)
4145 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
4146 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
4147 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
4148 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
4149 * METHODSEM PROPERTY ASSOCIATION (encoded index)
4151 * Note that we still don't support encoded indexes.
4155 typedef_locator (const void *a, const void *b)
4157 locator_t *loc = (locator_t *) a;
4158 const char *bb = (const char *) b;
4159 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
4160 guint32 col, col_next;
4162 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
4168 * Need to check that the next row is valid.
4170 if (typedef_index + 1 < loc->t->rows) {
4171 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
4172 if (loc->idx >= col_next)
4175 if (col == col_next)
4179 loc->result = typedef_index;
4185 table_locator (const void *a, const void *b)
4187 locator_t *loc = (locator_t *) a;
4188 const char *bb = (const char *) b;
4189 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
4192 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
4194 if (loc->idx == col) {
4195 loc->result = table_index;
4205 declsec_locator (const void *a, const void *b)
4207 locator_t *loc = (locator_t *) a;
4208 const char *bb = (const char *) b;
4209 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
4212 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
4214 if (loc->idx == col) {
4215 loc->result = table_index;
4227 * Return the 1-based row index in TABLE, which must be one of the *Ptr tables,
4228 * which contains IDX.
4231 search_ptr_table (MonoImage *image, int table, int idx)
4233 MonoTableInfo *ptrdef = &image->tables [table];
4236 /* Use a linear search to find our index in the table */
4237 for (i = 0; i < ptrdef->rows; i ++)
4238 /* All the Ptr tables have the same structure */
4239 if (mono_metadata_decode_row_col (ptrdef, i, 0) == idx)
4242 if (i < ptrdef->rows)
4249 * mono_metadata_typedef_from_field:
4250 * @meta: metadata context
4251 * @index: FieldDef token
4253 * Returns: the 1-based index into the TypeDef table of the type that
4254 * declared the field described by @index, or 0 if not found.
4257 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
4259 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
4265 loc.idx = mono_metadata_token_index (index);
4266 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
4269 if (meta->uncompressed_metadata)
4270 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
4272 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
4275 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4276 return loc.result + 1;
4280 * mono_metadata_typedef_from_method:
4281 * @meta: metadata context
4282 * @index: MethodDef token
4284 * Returns: the 1-based index into the TypeDef table of the type that
4285 * declared the method described by @index. 0 if not found.
4288 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
4290 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
4296 loc.idx = mono_metadata_token_index (index);
4297 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
4300 if (meta->uncompressed_metadata)
4301 loc.idx = search_ptr_table (meta, MONO_TABLE_METHOD_POINTER, loc.idx);
4303 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
4306 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4307 return loc.result + 1;
4311 * mono_metadata_interfaces_from_typedef_full:
4312 * @meta: metadata context
4313 * @index: typedef token
4314 * @interfaces: Out parameter used to store the interface array
4315 * @count: Out parameter used to store the number of interfaces
4316 * @heap_alloc_result: if TRUE the result array will be g_malloc'd
4317 * @context: The generic context
4319 * The array of interfaces that the @index typedef token implements is returned in
4320 * @interfaces. The number of elements in the array is returned in @count.
4323 * Returns: TRUE on success, FALSE on failure.
4326 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, MonoClass ***interfaces, guint *count, gboolean heap_alloc_result, MonoGenericContext *context, MonoError *error)
4328 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
4331 guint32 cols [MONO_INTERFACEIMPL_SIZE];
4342 loc.idx = mono_metadata_token_index (index);
4343 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
4346 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4351 * We may end up in the middle of the rows...
4354 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
4360 while (pos < tdef->rows) {
4361 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
4362 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
4367 if (heap_alloc_result)
4368 result = g_new0 (MonoClass*, pos - start);
4370 result = (MonoClass **)mono_image_alloc0 (meta, sizeof (MonoClass*) * (pos - start));
4373 while (pos < tdef->rows) {
4376 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
4377 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
4379 iface = mono_class_get_and_inflate_typespec_checked (
4380 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context, error);
4383 result [pos - start] = iface;
4386 *count = pos - start;
4387 *interfaces = result;
4392 * @meta: metadata context
4393 * @index: typedef token
4394 * @count: Out parameter used to store the number of interfaces
4396 * The array of interfaces that the @index typedef token implements is returned in
4397 * @interfaces. The number of elements in the array is returned in @count. The returned
4398 * array is g_malloc'd and the caller must free it.
4400 * LOCKING: Acquires the loader lock .
4402 * Returns: the interface array on success, NULL on failure.
4406 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
4409 MonoClass **interfaces = NULL;
4412 rv = mono_metadata_interfaces_from_typedef_full (meta, index, &interfaces, count, TRUE, NULL, &error);
4413 g_assert (mono_error_ok (&error)); /* FIXME dont swallow the error */
4421 * mono_metadata_nested_in_typedef:
4422 * @meta: metadata context
4423 * @index: typedef token
4425 * Returns: the 1-based index into the TypeDef table of the type
4426 * where the type described by @index is nested.
4427 * Returns 0 if @index describes a non-nested type.
4430 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
4432 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
4438 loc.idx = mono_metadata_token_index (index);
4439 loc.col_idx = MONO_NESTED_CLASS_NESTED;
4442 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4445 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4446 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
4450 * mono_metadata_nesting_typedef:
4451 * @meta: metadata context
4452 * @index: typedef token
4454 * Returns: the 1-based index into the TypeDef table of the first type
4455 * that is nested inside the type described by @index. The search starts at
4456 * @start_index. returns 0 if no such type is found.
4459 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
4461 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
4463 guint32 class_index = mono_metadata_token_index (index);
4468 start = start_index;
4470 while (start <= tdef->rows) {
4471 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
4477 if (start > tdef->rows)
4484 * mono_metadata_packing_from_typedef:
4485 * @meta: metadata context
4486 * @index: token representing a type
4488 * Returns: the info stored in the ClassLAyout table for the given typedef token
4489 * into the @packing and @size pointers.
4490 * Returns 0 if the info is not found.
4493 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
4495 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
4497 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
4502 loc.idx = mono_metadata_token_index (index);
4503 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
4506 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4509 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
4511 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
4513 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
4515 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4516 return loc.result + 1;
4520 * mono_metadata_custom_attrs_from_index:
4521 * @meta: metadata context
4522 * @index: token representing the parent
4524 * Returns: the 1-based index into the CustomAttribute table of the first
4525 * attribute which belongs to the metadata object described by @index.
4526 * Returns 0 if no such attribute is found.
4529 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
4531 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
4538 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
4541 /* FIXME: Index translation */
4543 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4546 /* Find the first entry by searching backwards */
4547 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
4550 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4551 return loc.result + 1;
4555 * mono_metadata_declsec_from_index:
4556 * @meta: metadata context
4557 * @index: token representing the parent
4559 * Returns: the 0-based index into the DeclarativeSecurity table of the first
4560 * attribute which belongs to the metadata object described by @index.
4561 * Returns -1 if no such attribute is found.
4564 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
4566 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
4573 loc.col_idx = MONO_DECL_SECURITY_PARENT;
4576 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
4579 /* Find the first entry by searching backwards */
4580 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
4587 * mono_metadata_localscope_from_methoddef:
4588 * @meta: metadata context
4589 * @index: methoddef index
4591 * Returns: the 1-based index into the LocalScope table of the first
4592 * scope which belongs to the method described by @index.
4593 * Returns 0 if no such row is found.
4596 mono_metadata_localscope_from_methoddef (MonoImage *meta, guint32 index)
4598 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_LOCALSCOPE];
4605 loc.col_idx = MONO_LOCALSCOPE_METHOD;
4608 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4611 /* Find the first entry by searching backwards */
4612 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_LOCALSCOPE_METHOD) == index))
4615 return loc.result + 1;
4620 mono_backtrace (int limit)
4625 backtrace (array, limit);
4626 names = backtrace_symbols (array, limit);
4627 for (i =0; i < limit; ++i) {
4628 g_print ("\t%s\n", names [i]);
4634 static int i8_align;
4637 * mono_type_set_alignment:
4639 * Set the alignment used by runtime to layout fields etc. of type TYPE to ALIGN.
4640 * This should only be used in AOT mode since the resulting layout will not match the
4644 mono_type_set_alignment (MonoTypeEnum type, int align)
4646 /* Support only a few types whose alignment is abi dependent */
4652 g_assert_not_reached ();
4659 * @t: the type to return the size of
4661 * Returns: the number of bytes required to hold an instance of this
4665 mono_type_size (MonoType *t, int *align)
4667 MonoTypeEnum simple_type;
4674 *align = MONO_ABI_ALIGNOF (gpointer);
4675 return MONO_ABI_SIZEOF (gpointer);
4678 simple_type = t->type;
4680 switch (simple_type) {
4681 case MONO_TYPE_VOID:
4684 case MONO_TYPE_BOOLEAN:
4685 *align = MONO_ABI_ALIGNOF (gint8);
4689 *align = MONO_ABI_ALIGNOF (gint8);
4691 case MONO_TYPE_CHAR:
4694 *align = MONO_ABI_ALIGNOF (gint16);
4698 *align = MONO_ABI_ALIGNOF (gint32);
4701 *align = MONO_ABI_ALIGNOF (float);
4705 *align = MONO_ABI_ALIGNOF (gint64);
4708 *align = MONO_ABI_ALIGNOF (double);
4712 *align = MONO_ABI_ALIGNOF (gpointer);
4713 return MONO_ABI_SIZEOF (gpointer);
4714 case MONO_TYPE_VALUETYPE: {
4715 if (t->data.klass->enumtype)
4716 return mono_type_size (mono_class_enum_basetype (t->data.klass), align);
4718 return mono_class_value_size (t->data.klass, (guint32*)align);
4720 case MONO_TYPE_STRING:
4721 case MONO_TYPE_OBJECT:
4722 case MONO_TYPE_CLASS:
4723 case MONO_TYPE_SZARRAY:
4725 case MONO_TYPE_FNPTR:
4726 case MONO_TYPE_ARRAY:
4727 *align = MONO_ABI_ALIGNOF (gpointer);
4728 return MONO_ABI_SIZEOF (gpointer);
4729 case MONO_TYPE_TYPEDBYREF:
4730 return mono_class_value_size (mono_defaults.typed_reference_class, (guint32*)align);
4731 case MONO_TYPE_GENERICINST: {
4732 MonoGenericClass *gclass = t->data.generic_class;
4733 MonoClass *container_class = gclass->container_class;
4735 // g_assert (!gclass->inst->is_open);
4737 if (container_class->valuetype) {
4738 if (container_class->enumtype)
4739 return mono_type_size (mono_class_enum_basetype (container_class), align);
4741 return mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
4743 *align = MONO_ABI_ALIGNOF (gpointer);
4744 return MONO_ABI_SIZEOF (gpointer);
4748 case MONO_TYPE_MVAR:
4749 if (!t->data.generic_param->gshared_constraint || t->data.generic_param->gshared_constraint->type == MONO_TYPE_VALUETYPE) {
4750 *align = MONO_ABI_ALIGNOF (gpointer);
4751 return MONO_ABI_SIZEOF (gpointer);
4753 /* The gparam can only match types given by gshared_constraint */
4754 return mono_type_size (t->data.generic_param->gshared_constraint, align);
4758 g_error ("mono_type_size: type 0x%02x unknown", t->type);
4764 * mono_type_stack_size:
4765 * @t: the type to return the size it uses on the stack
4767 * Returns: the number of bytes required to hold an instance of this
4768 * type on the runtime stack
4771 mono_type_stack_size (MonoType *t, int *align)
4773 return mono_type_stack_size_internal (t, align, FALSE);
4777 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open)
4780 MonoTypeEnum simple_type;
4781 #if SIZEOF_VOID_P == SIZEOF_REGISTER
4782 int stack_slot_size = MONO_ABI_SIZEOF (gpointer);
4783 int stack_slot_align = MONO_ABI_ALIGNOF (gpointer);
4784 #elif SIZEOF_VOID_P < SIZEOF_REGISTER
4785 int stack_slot_size = SIZEOF_REGISTER;
4786 int stack_slot_align = SIZEOF_REGISTER;
4789 g_assert (t != NULL);
4795 *align = stack_slot_align;
4796 return stack_slot_size;
4799 simple_type = t->type;
4800 switch (simple_type) {
4801 case MONO_TYPE_BOOLEAN:
4802 case MONO_TYPE_CHAR:
4811 case MONO_TYPE_STRING:
4812 case MONO_TYPE_OBJECT:
4813 case MONO_TYPE_CLASS:
4814 case MONO_TYPE_SZARRAY:
4816 case MONO_TYPE_FNPTR:
4817 case MONO_TYPE_ARRAY:
4818 *align = stack_slot_align;
4819 return stack_slot_size;
4821 case MONO_TYPE_MVAR:
4822 g_assert (allow_open);
4823 if (!t->data.generic_param->gshared_constraint || t->data.generic_param->gshared_constraint->type == MONO_TYPE_VALUETYPE) {
4824 *align = stack_slot_align;
4825 return stack_slot_size;
4827 /* The gparam can only match types given by gshared_constraint */
4828 return mono_type_stack_size_internal (t->data.generic_param->gshared_constraint, align, allow_open);
4830 case MONO_TYPE_TYPEDBYREF:
4831 *align = stack_slot_align;
4832 return stack_slot_size * 3;
4834 *align = MONO_ABI_ALIGNOF (float);
4835 return sizeof (float);
4838 *align = MONO_ABI_ALIGNOF (gint64);
4839 return sizeof (gint64);
4841 *align = MONO_ABI_ALIGNOF (double);
4842 return sizeof (double);
4843 case MONO_TYPE_VALUETYPE: {
4846 if (t->data.klass->enumtype)
4847 return mono_type_stack_size_internal (mono_class_enum_basetype (t->data.klass), align, allow_open);
4849 size = mono_class_value_size (t->data.klass, (guint32*)align);
4851 *align = *align + stack_slot_align - 1;
4852 *align &= ~(stack_slot_align - 1);
4854 size += stack_slot_size - 1;
4855 size &= ~(stack_slot_size - 1);
4860 case MONO_TYPE_GENERICINST: {
4861 MonoGenericClass *gclass = t->data.generic_class;
4862 MonoClass *container_class = gclass->container_class;
4865 g_assert (!gclass->context.class_inst->is_open);
4867 if (container_class->valuetype) {
4868 if (container_class->enumtype)
4869 return mono_type_stack_size_internal (mono_class_enum_basetype (container_class), align, allow_open);
4871 guint32 size = mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
4873 *align = *align + stack_slot_align - 1;
4874 *align &= ~(stack_slot_align - 1);
4876 size += stack_slot_size - 1;
4877 size &= ~(stack_slot_size - 1);
4882 *align = stack_slot_align;
4883 return stack_slot_size;
4887 g_error ("type 0x%02x unknown", t->type);
4893 mono_type_generic_inst_is_valuetype (MonoType *type)
4895 g_assert (type->type == MONO_TYPE_GENERICINST);
4896 return type->data.generic_class->container_class->valuetype;
4900 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
4902 return gclass->container_class->valuetype;
4906 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
4908 MonoGenericInst *i1 = g1->context.class_inst;
4909 MonoGenericInst *i2 = g2->context.class_inst;
4911 if (g1->is_dynamic != g2->is_dynamic)
4913 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
4915 if (!mono_generic_inst_equal_full (i1, i2, signature_only))
4917 return g1->is_tb_open == g2->is_tb_open;
4921 _mono_metadata_generic_class_container_equal (const MonoGenericClass *g1, MonoClass *c2, gboolean signature_only)
4923 MonoGenericInst *i1 = g1->context.class_inst;
4924 MonoGenericInst *i2 = mono_class_get_generic_container (c2)->context.class_inst;
4926 if (!mono_metadata_class_equal (g1->container_class, c2, signature_only))
4928 if (!mono_generic_inst_equal_full (i1, i2, signature_only))
4930 return !g1->is_tb_open;
4934 mono_metadata_generic_context_hash (const MonoGenericContext *context)
4936 /* FIXME: check if this seed is good enough */
4937 guint hash = 0xc01dfee7;
4938 if (context->class_inst)
4939 hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->class_inst);
4940 if (context->method_inst)
4941 hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->method_inst);
4946 mono_metadata_generic_context_equal (const MonoGenericContext *g1, const MonoGenericContext *g2)
4948 return g1->class_inst == g2->class_inst && g1->method_inst == g2->method_inst;
4952 * mono_metadata_str_hash:
4954 * This should be used instead of g_str_hash for computing hash codes visible
4955 * outside this module, since g_str_hash () is not guaranteed to be stable
4956 * (its not the same in eglib for example).
4959 mono_metadata_str_hash (gconstpointer v1)
4961 /* Same as g_str_hash () in glib */
4962 char *p = (char *) v1;
4967 hash = (hash << 5) - hash + *p;
4974 * mono_metadata_type_hash:
4977 * Computes an hash value for @t1 to be used in GHashTable.
4978 * The returned hash is guaranteed to be the same across executions.
4981 mono_metadata_type_hash (MonoType *t1)
4983 guint hash = t1->type;
4985 hash |= t1->byref << 6; /* do not collide with t1->type values */
4987 case MONO_TYPE_VALUETYPE:
4988 case MONO_TYPE_CLASS:
4989 case MONO_TYPE_SZARRAY: {
4990 MonoClass *klass = t1->data.klass;
4992 * Dynamic classes must not be hashed on their type since it can change
4993 * during runtime. For example, if we hash a reference type that is
4994 * later made into a valuetype.
4996 * This is specially problematic with generic instances since they are
4997 * inserted in a bunch of hash tables before been finished.
4999 if (image_is_dynamic (klass->image))
5000 return (t1->byref << 6) | mono_metadata_str_hash (klass->name);
5001 return ((hash << 5) - hash) ^ mono_metadata_str_hash (klass->name);
5004 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
5005 case MONO_TYPE_ARRAY:
5006 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
5007 case MONO_TYPE_GENERICINST:
5008 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
5010 case MONO_TYPE_MVAR:
5011 return ((hash << 5) - hash) ^ mono_metadata_generic_param_hash (t1->data.generic_param);
5018 mono_metadata_generic_param_hash (MonoGenericParam *p)
5021 MonoGenericParamInfo *info;
5023 hash = (mono_generic_param_num (p) << 2);
5024 if (p->gshared_constraint)
5025 hash = ((hash << 5) - hash) ^ mono_metadata_type_hash (p->gshared_constraint);
5026 info = mono_generic_param_info (p);
5027 /* Can't hash on the owner klass/method, since those might not be set when this is called */
5029 hash = ((hash << 5) - hash) ^ info->token;
5034 mono_metadata_generic_param_equal_internal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
5038 if (mono_generic_param_num (p1) != mono_generic_param_num (p2))
5040 if (p1->gshared_constraint && p2->gshared_constraint) {
5041 if (!mono_metadata_type_equal (p1->gshared_constraint, p2->gshared_constraint))
5044 if (p1->gshared_constraint != p2->gshared_constraint)
5049 * We have to compare the image as well because if we didn't,
5050 * the generic_inst_cache lookup wouldn't care about the image
5051 * of generic params, so what could happen is that a generic
5052 * inst with params from image A is put into the cache, then
5053 * image B gets that generic inst from the cache, image A is
5054 * unloaded, so the inst is deleted, but image B still retains
5057 if (mono_generic_param_owner (p1) == mono_generic_param_owner (p2))
5061 * If `signature_only' is true, we're comparing two (method) signatures.
5062 * In this case, the owner of two type parameters doesn't need to match.
5065 return signature_only;
5069 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2)
5071 return mono_metadata_generic_param_equal_internal (p1, p2, TRUE);
5075 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
5079 if (mono_class_is_ginst (c1) && mono_class_is_ginst (c2))
5080 return _mono_metadata_generic_class_equal (mono_class_get_generic_class (c1), mono_class_get_generic_class (c2), signature_only);
5081 if (mono_class_is_ginst (c1) && mono_class_is_gtd (c2))
5082 return _mono_metadata_generic_class_container_equal (mono_class_get_generic_class (c1), c2, signature_only);
5083 if (mono_class_is_gtd (c1) && mono_class_is_ginst (c2))
5084 return _mono_metadata_generic_class_container_equal (mono_class_get_generic_class (c2), c1, signature_only);
5085 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
5086 return mono_metadata_generic_param_equal_internal (
5087 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
5088 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
5089 return mono_metadata_generic_param_equal_internal (
5090 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
5091 if (signature_only &&
5092 (c1->byval_arg.type == MONO_TYPE_SZARRAY) && (c2->byval_arg.type == MONO_TYPE_SZARRAY))
5093 return mono_metadata_class_equal (c1->byval_arg.data.klass, c2->byval_arg.data.klass, signature_only);
5094 if (signature_only &&
5095 (c1->byval_arg.type == MONO_TYPE_ARRAY) && (c2->byval_arg.type == MONO_TYPE_ARRAY))
5096 return do_mono_metadata_type_equal (&c1->byval_arg, &c2->byval_arg, signature_only);
5101 mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only)
5103 gpointer iter1 = 0, iter2 = 0;
5107 if (s1->call_convention != s2->call_convention)
5109 if (s1->sentinelpos != s2->sentinelpos)
5111 if (s1->hasthis != s2->hasthis)
5113 if (s1->explicit_this != s2->explicit_this)
5115 if (! do_mono_metadata_type_equal (s1->ret, s2->ret, signature_only))
5117 if (s1->param_count != s2->param_count)
5121 MonoType *t1 = mono_signature_get_params (s1, &iter1);
5122 MonoType *t2 = mono_signature_get_params (s2, &iter2);
5124 if (t1 == NULL || t2 == NULL)
5126 if (! do_mono_metadata_type_equal (t1, t2, signature_only))
5132 * mono_metadata_type_equal:
5135 * @signature_only: If true, treat ginsts as equal which are instantiated separately but have equal positional value
5137 * Determine if @t1 and @t2 represent the same type.
5138 * Returns: #TRUE if @t1 and @t2 are equal.
5141 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
5143 if (t1->type != t2->type || t1->byref != t2->byref)
5147 case MONO_TYPE_VOID:
5148 case MONO_TYPE_BOOLEAN:
5149 case MONO_TYPE_CHAR:
5160 case MONO_TYPE_STRING:
5163 case MONO_TYPE_OBJECT:
5164 case MONO_TYPE_TYPEDBYREF:
5166 case MONO_TYPE_VALUETYPE:
5167 case MONO_TYPE_CLASS:
5168 case MONO_TYPE_SZARRAY:
5169 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
5171 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
5172 case MONO_TYPE_ARRAY:
5173 if (t1->data.array->rank != t2->data.array->rank)
5175 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
5176 case MONO_TYPE_GENERICINST:
5177 return _mono_metadata_generic_class_equal (
5178 t1->data.generic_class, t2->data.generic_class, signature_only);
5180 return mono_metadata_generic_param_equal_internal (
5181 t1->data.generic_param, t2->data.generic_param, signature_only);
5182 case MONO_TYPE_MVAR:
5183 return mono_metadata_generic_param_equal_internal (
5184 t1->data.generic_param, t2->data.generic_param, signature_only);
5185 case MONO_TYPE_FNPTR:
5186 return mono_metadata_fnptr_equal (t1->data.method, t2->data.method, signature_only);
5188 g_error ("implement type compare for %0x!", t1->type);
5196 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
5198 return do_mono_metadata_type_equal (t1, t2, FALSE);
5202 * mono_metadata_type_equal_full:
5205 * @signature_only: if signature only comparison should be made
5207 * Determine if @t1 and @t2 are signature compatible if @signature_only is #TRUE, otherwise
5208 * behaves the same way as mono_metadata_type_equal.
5209 * The function mono_metadata_type_equal(a, b) is just a shortcut for mono_metadata_type_equal_full(a, b, FALSE).
5210 * Returns: #TRUE if @t1 and @t2 are equal taking @signature_only into account.
5213 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only)
5215 return do_mono_metadata_type_equal (t1, t2, signature_only);
5219 * mono_metadata_signature_equal:
5220 * @sig1: a signature
5221 * @sig2: another signature
5223 * Determine if @sig1 and @sig2 represent the same signature, with the
5224 * same number of arguments and the same types.
5225 * Returns: #TRUE if @sig1 and @sig2 are equal.
5228 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
5232 if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
5235 if (sig1->generic_param_count != sig2->generic_param_count)
5239 * We're just comparing the signatures of two methods here:
5241 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
5242 * U and V are equal here.
5244 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
5247 for (i = 0; i < sig1->param_count; i++) {
5248 MonoType *p1 = sig1->params[i];
5249 MonoType *p2 = sig2->params[i];
5251 /* if (p1->attrs != p2->attrs)
5254 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
5258 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
5264 * mono_metadata_type_dup:
5265 * @image: image to alloc memory from
5266 * @original: type to duplicate
5268 * Returns: copy of type allocated from the image's mempool (or from the heap, if @image is null).
5271 mono_metadata_type_dup (MonoImage *image, const MonoType *o)
5274 int sizeof_o = MONO_SIZEOF_TYPE;
5276 sizeof_o += o->num_mods * sizeof (MonoCustomMod);
5278 r = image ? (MonoType *)mono_image_alloc0 (image, sizeof_o) : (MonoType *)g_malloc (sizeof_o);
5280 memcpy (r, o, sizeof_o);
5282 if (o->type == MONO_TYPE_PTR) {
5283 r->data.type = mono_metadata_type_dup (image, o->data.type);
5284 } else if (o->type == MONO_TYPE_ARRAY) {
5285 r->data.array = mono_dup_array_type (image, o->data.array);
5286 } else if (o->type == MONO_TYPE_FNPTR) {
5287 /*FIXME the dup'ed signature is leaked mono_metadata_free_type*/
5288 r->data.method = mono_metadata_signature_deep_dup (image, o->data.method);
5294 mono_signature_hash (MonoMethodSignature *sig)
5296 guint i, res = sig->ret->type;
5298 for (i = 0; i < sig->param_count; i++)
5299 res = (res << 5) - res + mono_type_hash (sig->params[i]);
5305 * mono_metadata_encode_value:
5306 * @value: value to encode
5307 * @buf: buffer where to write the compressed representation
5308 * @endbuf: pointer updated to point at the end of the encoded output
5310 * Encodes the value @value in the compressed representation used
5311 * in metadata and stores the result in @buf. @buf needs to be big
5312 * enough to hold the data (4 bytes).
5315 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
5321 else if (value < 0x4000) {
5322 p [0] = 0x80 | (value >> 8);
5323 p [1] = value & 0xff;
5326 p [0] = (value >> 24) | 0xc0;
5327 p [1] = (value >> 16) & 0xff;
5328 p [2] = (value >> 8) & 0xff;
5329 p [3] = value & 0xff;
5337 * mono_metadata_field_info:
5338 * @meta: the Image the field is defined in
5339 * @index: the index in the field table representing the field
5340 * @offset: a pointer to an integer where to store the offset that
5341 * may have been specified for the field in a FieldLayout table
5342 * @rva: a pointer to the RVA of the field data in the image that
5343 * may have been defined in a FieldRVA table
5344 * @marshal_spec: a pointer to the marshal spec that may have been
5345 * defined for the field in a FieldMarshal table.
5347 * Gather info for field @index that may have been defined in the FieldLayout,
5348 * FieldRVA and FieldMarshal tables.
5349 * Either of offset, rva and marshal_spec can be NULL if you're not interested
5353 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
5354 MonoMarshalSpec **marshal_spec)
5356 mono_metadata_field_info_full (meta, index, offset, rva, marshal_spec, FALSE);
5360 mono_metadata_field_info_with_mempool (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
5361 MonoMarshalSpec **marshal_spec)
5363 mono_metadata_field_info_full (meta, index, offset, rva, marshal_spec, TRUE);
5367 mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
5368 MonoMarshalSpec **marshal_spec, gboolean alloc_from_image)
5370 MonoTableInfo *tdef;
5373 loc.idx = index + 1;
5374 if (meta->uncompressed_metadata)
5375 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
5378 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
5380 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
5383 if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
5384 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
5386 *offset = (guint32)-1;
5390 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
5392 loc.col_idx = MONO_FIELD_RVA_FIELD;
5395 if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
5397 * LAMESPEC: There is no signature, no nothing, just the raw data.
5399 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
5407 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
5408 *marshal_spec = mono_metadata_parse_marshal_spec_full (alloc_from_image ? meta : NULL, meta, p);
5415 * mono_metadata_get_constant_index:
5416 * @meta: the Image the field is defined in
5417 * @index: the token that may have a row defined in the constants table
5418 * @hint: possible position for the row
5420 * @token must be a FieldDef, ParamDef or PropertyDef token.
5422 * Returns: the index into the Constants table or 0 if not found.
5425 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
5427 MonoTableInfo *tdef;
5429 guint32 index = mono_metadata_token_index (token);
5431 tdef = &meta->tables [MONO_TABLE_CONSTANT];
5432 index <<= MONO_HASCONSTANT_BITS;
5433 switch (mono_metadata_token_table (token)) {
5434 case MONO_TABLE_FIELD:
5435 index |= MONO_HASCONSTANT_FIEDDEF;
5437 case MONO_TABLE_PARAM:
5438 index |= MONO_HASCONSTANT_PARAM;
5440 case MONO_TABLE_PROPERTY:
5441 index |= MONO_HASCONSTANT_PROPERTY;
5444 g_warning ("Not a valid token for the constant table: 0x%08x", token);
5448 loc.col_idx = MONO_CONSTANT_PARENT;
5451 /* FIXME: Index translation */
5453 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
5456 if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
5457 return loc.result + 1;
5463 * mono_metadata_events_from_typedef:
5464 * @meta: metadata context
5465 * @index: 0-based index (in the TypeDef table) describing a type
5467 * Returns: the 0-based index in the Event table for the events in the
5468 * type. The last event that belongs to the type (plus 1) is stored
5469 * in the @end_idx pointer.
5472 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
5476 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
5484 loc.col_idx = MONO_EVENT_MAP_PARENT;
5485 loc.idx = index + 1;
5487 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5490 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
5491 if (loc.result + 1 < tdef->rows) {
5492 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
5494 end = meta->tables [MONO_TABLE_EVENT].rows;
5502 * mono_metadata_methods_from_event:
5503 * @meta: metadata context
5504 * @index: 0-based index (in the Event table) describing a event
5506 * Returns: the 0-based index in the MethodDef table for the methods in the
5507 * event. The last method that belongs to the event (plus 1) is stored
5508 * in the @end_idx pointer.
5511 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
5515 guint32 cols [MONO_METHOD_SEMA_SIZE];
5516 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
5522 if (meta->uncompressed_metadata)
5523 index = search_ptr_table (meta, MONO_TABLE_EVENT_POINTER, index + 1) - 1;
5526 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
5527 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
5529 if (!mono_binary_search (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
5534 * We may end up in the middle of the rows...
5537 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
5543 while (end < msemt->rows) {
5544 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
5545 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
5554 * mono_metadata_properties_from_typedef:
5555 * @meta: metadata context
5556 * @index: 0-based index (in the TypeDef table) describing a type
5558 * Returns: the 0-based index in the Property table for the properties in the
5559 * type. The last property that belongs to the type (plus 1) is stored
5560 * in the @end_idx pointer.
5563 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
5567 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
5575 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
5576 loc.idx = index + 1;
5578 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5581 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
5582 if (loc.result + 1 < tdef->rows) {
5583 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
5585 end = meta->tables [MONO_TABLE_PROPERTY].rows;
5593 * mono_metadata_methods_from_property:
5594 * @meta: metadata context
5595 * @index: 0-based index (in the PropertyDef table) describing a property
5597 * Returns: the 0-based index in the MethodDef table for the methods in the
5598 * property. The last method that belongs to the property (plus 1) is stored
5599 * in the @end_idx pointer.
5602 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
5606 guint32 cols [MONO_METHOD_SEMA_SIZE];
5607 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
5613 if (meta->uncompressed_metadata)
5614 index = search_ptr_table (meta, MONO_TABLE_PROPERTY_POINTER, index + 1) - 1;
5617 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
5618 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
5620 if (!mono_binary_search (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
5625 * We may end up in the middle of the rows...
5628 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
5634 while (end < msemt->rows) {
5635 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
5636 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
5645 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
5648 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
5653 /* No index translation seems to be needed */
5656 loc.col_idx = MONO_IMPLMAP_MEMBER;
5657 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
5659 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5662 return loc.result + 1;
5666 * @image: context where the image is created
5667 * @type_spec: typespec token
5668 * @deprecated use mono_type_create_from_typespec_checked that has proper error handling
5670 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
5674 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
5677 MonoType *type = mono_type_create_from_typespec_checked (image, type_spec, &error);
5679 g_error ("Could not create typespec %x due to %s", type_spec, mono_error_get_message (&error));
5684 mono_type_create_from_typespec_checked (MonoImage *image, guint32 type_spec, MonoError *error)
5687 guint32 idx = mono_metadata_token_index (type_spec);
5689 guint32 cols [MONO_TYPESPEC_SIZE];
5691 MonoType *type, *type2;
5695 mono_image_lock (image);
5696 type = (MonoType *)g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
5697 mono_image_unlock (image);
5701 t = &image->tables [MONO_TABLE_TYPESPEC];
5703 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
5704 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
5706 if (!mono_verifier_verify_typespec_signature (image, cols [MONO_TYPESPEC_SIGNATURE], type_spec, NULL)) {
5707 mono_error_set_bad_image (error, image, "Could not verify type spec %08x.", type_spec);
5711 mono_metadata_decode_value (ptr, &ptr);
5713 type = mono_metadata_parse_type_checked (image, NULL, 0, TRUE, ptr, &ptr, error);
5717 type2 = mono_metadata_type_dup (image, type);
5718 mono_metadata_free_type (type);
5720 mono_image_lock (image);
5721 type = (MonoType *)g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
5722 /* We might leak some data in the image mempool if found */
5724 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type2);
5727 mono_image_unlock (image);
5734 mono_image_strndup (MonoImage *image, const char *data, guint len)
5738 return g_strndup (data, len);
5739 res = (char *)mono_image_alloc (image, len + 1);
5740 memcpy (res, data, len);
5746 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
5748 return mono_metadata_parse_marshal_spec_full (NULL, image, ptr);
5752 * If IMAGE is non-null, memory will be allocated from its mempool, otherwise it will be allocated using malloc.
5753 * PARENT_IMAGE is the image containing the marshal spec.
5756 mono_metadata_parse_marshal_spec_full (MonoImage *image, MonoImage *parent_image, const char *ptr)
5758 MonoMarshalSpec *res;
5760 const char *start = ptr;
5762 /* fixme: this is incomplete, but I cant find more infos in the specs */
5765 res = (MonoMarshalSpec *)mono_image_alloc0 (image, sizeof (MonoMarshalSpec));
5767 res = g_new0 (MonoMarshalSpec, 1);
5769 len = mono_metadata_decode_value (ptr, &ptr);
5770 res->native = (MonoMarshalNative)*ptr++;
5772 if (res->native == MONO_NATIVE_LPARRAY) {
5773 res->data.array_data.param_num = -1;
5774 res->data.array_data.num_elem = -1;
5775 res->data.array_data.elem_mult = -1;
5777 if (ptr - start <= len)
5778 res->data.array_data.elem_type = (MonoMarshalNative)*ptr++;
5779 if (ptr - start <= len)
5780 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
5781 if (ptr - start <= len)
5782 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
5783 if (ptr - start <= len) {
5785 * LAMESPEC: Older spec versions say this parameter comes before
5786 * num_elem. Never spec versions don't talk about elem_mult at
5787 * all, but csc still emits it, and it is used to distinguish
5788 * between param_num being 0, and param_num being omitted.
5789 * So if (param_num == 0) && (num_elem > 0), then
5790 * elem_mult == 0 -> the array size is num_elem
5791 * elem_mult == 1 -> the array size is @param_num + num_elem
5793 res->data.array_data.elem_mult = mono_metadata_decode_value (ptr, &ptr);
5797 if (res->native == MONO_NATIVE_BYVALTSTR) {
5798 if (ptr - start <= len)
5799 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
5802 if (res->native == MONO_NATIVE_BYVALARRAY) {
5803 if (ptr - start <= len)
5804 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
5807 if (res->native == MONO_NATIVE_CUSTOM) {
5808 /* skip unused type guid */
5809 len = mono_metadata_decode_value (ptr, &ptr);
5811 /* skip unused native type name */
5812 len = mono_metadata_decode_value (ptr, &ptr);
5814 /* read custom marshaler type name */
5815 len = mono_metadata_decode_value (ptr, &ptr);
5816 res->data.custom_data.custom_name = mono_image_strndup (image, ptr, len);
5818 /* read cookie string */
5819 len = mono_metadata_decode_value (ptr, &ptr);
5820 res->data.custom_data.cookie = mono_image_strndup (image, ptr, len);
5821 res->data.custom_data.image = parent_image;
5824 if (res->native == MONO_NATIVE_SAFEARRAY) {
5825 res->data.safearray_data.elem_type = (MonoMarshalVariant)0;
5826 res->data.safearray_data.num_elem = 0;
5827 if (ptr - start <= len)
5828 res->data.safearray_data.elem_type = (MonoMarshalVariant)*ptr++;
5829 if (ptr - start <= len)
5830 res->data.safearray_data.num_elem = *ptr++;
5836 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
5838 if (spec->native == MONO_NATIVE_CUSTOM) {
5839 g_free (spec->data.custom_data.custom_name);
5840 g_free (spec->data.custom_data.cookie);
5846 * mono_type_to_unmanaged:
5848 * Returns: A MonoMarshalNative enumeration value (MONO_NATIVE_) value
5849 * describing the underlying native reprensetation of the type.
5851 * In addition the value pointed by
5852 * "conv" will contain the kind of marshalling required for this
5853 * particular type one of the MONO_MARSHAL_CONV_ enumeration values.
5856 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
5857 gboolean unicode, MonoMarshalConv *conv)
5859 MonoMarshalConv dummy_conv;
5865 *conv = MONO_MARSHAL_CONV_NONE;
5868 return MONO_NATIVE_UINT;
5872 case MONO_TYPE_BOOLEAN:
5874 switch (mspec->native) {
5875 case MONO_NATIVE_VARIANTBOOL:
5876 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
5877 return MONO_NATIVE_VARIANTBOOL;
5878 case MONO_NATIVE_BOOLEAN:
5879 *conv = MONO_MARSHAL_CONV_BOOL_I4;
5880 return MONO_NATIVE_BOOLEAN;
5881 case MONO_NATIVE_I1:
5882 case MONO_NATIVE_U1:
5883 return mspec->native;
5885 g_error ("cant marshal bool to native type %02x", mspec->native);
5888 *conv = MONO_MARSHAL_CONV_BOOL_I4;
5889 return MONO_NATIVE_BOOLEAN;
5890 case MONO_TYPE_CHAR:
5892 switch (mspec->native) {
5893 case MONO_NATIVE_U2:
5894 case MONO_NATIVE_U1:
5895 return mspec->native;
5897 g_error ("cant marshal char to native type %02x", mspec->native);
5900 return unicode ? MONO_NATIVE_U2 : MONO_NATIVE_U1;
5901 case MONO_TYPE_I1: return MONO_NATIVE_I1;
5902 case MONO_TYPE_U1: return MONO_NATIVE_U1;
5903 case MONO_TYPE_I2: return MONO_NATIVE_I2;
5904 case MONO_TYPE_U2: return MONO_NATIVE_U2;
5905 case MONO_TYPE_I4: return MONO_NATIVE_I4;
5906 case MONO_TYPE_U4: return MONO_NATIVE_U4;
5907 case MONO_TYPE_I8: return MONO_NATIVE_I8;
5908 case MONO_TYPE_U8: return MONO_NATIVE_U8;
5909 case MONO_TYPE_R4: return MONO_NATIVE_R4;
5910 case MONO_TYPE_R8: return MONO_NATIVE_R8;
5911 case MONO_TYPE_STRING:
5913 switch (mspec->native) {
5914 case MONO_NATIVE_BSTR:
5915 *conv = MONO_MARSHAL_CONV_STR_BSTR;
5916 return MONO_NATIVE_BSTR;
5917 case MONO_NATIVE_LPSTR:
5918 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
5919 return MONO_NATIVE_LPSTR;
5920 case MONO_NATIVE_LPWSTR:
5921 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
5922 return MONO_NATIVE_LPWSTR;
5923 case MONO_NATIVE_LPTSTR:
5924 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
5925 return MONO_NATIVE_LPTSTR;
5926 case MONO_NATIVE_ANSIBSTR:
5927 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
5928 return MONO_NATIVE_ANSIBSTR;
5929 case MONO_NATIVE_TBSTR:
5930 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
5931 return MONO_NATIVE_TBSTR;
5932 case MONO_NATIVE_UTF8STR:
5933 *conv = MONO_MARSHAL_CONV_STR_UTF8STR;
5934 return MONO_NATIVE_UTF8STR;
5935 case MONO_NATIVE_BYVALTSTR:
5937 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
5939 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
5940 return MONO_NATIVE_BYVALTSTR;
5942 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);
5946 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
5947 return MONO_NATIVE_LPWSTR;
5950 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
5951 return MONO_NATIVE_LPSTR;
5953 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
5954 case MONO_TYPE_VALUETYPE: /*FIXME*/
5955 if (type->data.klass->enumtype) {
5956 t = mono_class_enum_basetype (type->data.klass)->type;
5959 if (type->data.klass == mono_defaults.handleref_class){
5960 *conv = MONO_MARSHAL_CONV_HANDLEREF;
5961 return MONO_NATIVE_INT;
5963 return MONO_NATIVE_STRUCT;
5964 case MONO_TYPE_SZARRAY:
5965 case MONO_TYPE_ARRAY:
5967 switch (mspec->native) {
5968 case MONO_NATIVE_BYVALARRAY:
5969 if ((type->data.klass->element_class == mono_defaults.char_class) && !unicode)
5970 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY;
5972 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
5973 return MONO_NATIVE_BYVALARRAY;
5974 case MONO_NATIVE_SAFEARRAY:
5975 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
5976 return MONO_NATIVE_SAFEARRAY;
5977 case MONO_NATIVE_LPARRAY:
5978 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
5979 return MONO_NATIVE_LPARRAY;
5981 g_error ("cant marshal array as native type %02x", mspec->native);
5985 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
5986 return MONO_NATIVE_LPARRAY;
5987 case MONO_TYPE_I: return MONO_NATIVE_INT;
5988 case MONO_TYPE_U: return MONO_NATIVE_UINT;
5989 case MONO_TYPE_CLASS:
5990 case MONO_TYPE_OBJECT: {
5991 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
5993 switch (mspec->native) {
5994 case MONO_NATIVE_STRUCT:
5995 return MONO_NATIVE_STRUCT;
5996 case MONO_NATIVE_CUSTOM:
5997 return MONO_NATIVE_CUSTOM;
5998 case MONO_NATIVE_INTERFACE:
5999 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
6000 return MONO_NATIVE_INTERFACE;
6001 case MONO_NATIVE_IDISPATCH:
6002 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
6003 return MONO_NATIVE_IDISPATCH;
6004 case MONO_NATIVE_IUNKNOWN:
6005 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
6006 return MONO_NATIVE_IUNKNOWN;
6007 case MONO_NATIVE_FUNC:
6008 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
6009 type->data.klass == mono_defaults.delegate_class ||
6010 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
6011 *conv = MONO_MARSHAL_CONV_DEL_FTN;
6012 return MONO_NATIVE_FUNC;
6016 g_error ("cant marshal object as native type %02x", mspec->native);
6019 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
6020 type->data.klass == mono_defaults.delegate_class ||
6021 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
6022 *conv = MONO_MARSHAL_CONV_DEL_FTN;
6023 return MONO_NATIVE_FUNC;
6025 if (mono_class_try_get_safehandle_class () && type->data.klass == mono_class_try_get_safehandle_class ()){
6026 *conv = MONO_MARSHAL_CONV_SAFEHANDLE;
6027 return MONO_NATIVE_INT;
6029 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
6030 return MONO_NATIVE_STRUCT;
6032 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
6033 case MONO_TYPE_GENERICINST:
6034 type = &type->data.generic_class->container_class->byval_arg;
6037 case MONO_TYPE_TYPEDBYREF:
6039 g_error ("type 0x%02x not handled in marshal", t);
6041 return MONO_NATIVE_MAX;
6045 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
6048 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
6054 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
6055 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
6057 /* FIXME: Index translation */
6059 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
6062 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
6066 method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context, MonoError *error)
6068 MonoMethod *result = NULL;
6069 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
6073 switch (tok & MONO_METHODDEFORREF_MASK) {
6074 case MONO_METHODDEFORREF_METHODDEF:
6075 result = mono_get_method_checked (m, MONO_TOKEN_METHOD_DEF | idx, NULL, context, error);
6077 case MONO_METHODDEFORREF_METHODREF:
6078 result = mono_get_method_checked (m, MONO_TOKEN_MEMBER_REF | idx, NULL, context, error);
6081 mono_error_set_bad_image (error, m, "Invalid MethodDefOfRef token %x", tok);
6088 * mono_class_get_overrides_full:
6090 * Return the method overrides belonging to class @type_token in @overrides, and
6091 * the number of overrides in @num_overrides.
6093 * Returns: TRUE on success, FALSE on failure.
6096 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
6097 MonoGenericContext *generic_context)
6101 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
6104 guint32 cols [MONO_METHODIMPL_SIZE];
6105 MonoMethod **result;
6116 loc.col_idx = MONO_METHODIMPL_CLASS;
6117 loc.idx = mono_metadata_token_index (type_token);
6119 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
6125 * We may end up in the middle of the rows...
6128 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
6133 while (end < tdef->rows) {
6134 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
6140 result = g_new (MonoMethod*, num * 2);
6141 for (i = 0; i < num; ++i) {
6144 if (!mono_verifier_verify_methodimpl_row (image, start + i, &error)) {
6145 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6150 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
6151 method = method_from_method_def_or_ref (
6152 image, cols [MONO_METHODIMPL_DECLARATION], generic_context, &error);
6153 if (method == NULL) {
6154 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6157 result [i * 2] = method;
6158 method = method_from_method_def_or_ref (
6159 image, cols [MONO_METHODIMPL_BODY], generic_context, &error);
6160 if (method == NULL) {
6161 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6164 result [i * 2 + 1] = method;
6167 *overrides = result;
6169 *num_overrides = num;
6174 * mono_guid_to_string:
6176 * Converts a 16 byte Microsoft GUID to the standard string representation.
6179 mono_guid_to_string (const guint8 *guid)
6181 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
6182 guid[3], guid[2], guid[1], guid[0],
6186 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
6190 * mono_guid_to_string_minimal:
6192 * Converts a 16 byte Microsoft GUID to lower case no '-' representation..
6195 mono_guid_to_string_minimal (const guint8 *guid)
6197 return g_strdup_printf ("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
6198 guid[3], guid[2], guid[1], guid[0],
6202 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
6205 get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContainer *container, MonoError *error)
6207 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
6208 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
6209 guint32 i, token, found;
6210 MonoClass *klass, **res;
6211 GSList *cons = NULL, *tmp;
6212 MonoGenericContext *context = &container->context;
6216 *constraints = NULL;
6218 for (i = 0; i < tdef->rows; ++i) {
6219 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
6220 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
6221 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
6222 klass = mono_class_get_and_inflate_typespec_checked (image, token, context, error);
6224 g_slist_free (cons);
6227 cons = g_slist_append (cons, klass);
6230 /* contiguous list finished */
6237 res = (MonoClass **)mono_image_alloc0 (image, sizeof (MonoClass*) * (found + 1));
6238 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
6239 res [i] = (MonoClass *)tmp->data;
6241 g_slist_free (cons);
6247 * mono_metadata_get_generic_param_row:
6250 * @token: TypeOrMethodDef token, owner for GenericParam
6251 * @owner: coded token, set on return
6253 * Returns: 1-based row-id in the GenericParam table whose
6254 * owner is @token. 0 if not found.
6257 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner)
6259 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
6266 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
6267 *owner = MONO_TYPEORMETHOD_TYPE;
6268 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
6269 *owner = MONO_TYPEORMETHOD_METHOD;
6271 g_error ("wrong token %x to get_generic_param_row", token);
6274 *owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
6277 loc.col_idx = MONO_GENERICPARAM_OWNER;
6280 if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
6283 /* Find the first entry by searching backwards */
6284 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_GENERICPARAM_OWNER) == loc.idx))
6287 return loc.result + 1;
6291 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
6294 return mono_metadata_get_generic_param_row (image, token, &owner);
6298 * Memory is allocated from IMAGE's mempool.
6301 mono_metadata_load_generic_param_constraints_checked (MonoImage *image, guint32 token,
6302 MonoGenericContainer *container, MonoError *error)
6305 guint32 start_row, i, owner;
6308 if (! (start_row = mono_metadata_get_generic_param_row (image, token, &owner)))
6310 for (i = 0; i < container->type_argc; i++) {
6311 if (!get_constraints (image, start_row + i, &mono_generic_container_get_param_info (container, i)->constraints, container, error)) {
6319 * mono_metadata_load_generic_params:
6321 * Load the type parameters from the type or method definition @token.
6323 * Use this method after parsing a type or method definition to figure out whether it's a generic
6324 * type / method. When parsing a method definition, @parent_container points to the generic container
6325 * of the current class, if any.
6327 * Note: This method does not load the constraints: for typedefs, this has to be done after fully
6328 * creating the type.
6330 * Returns: NULL if @token is not a generic type or method definition or the new generic container.
6332 * LOCKING: Acquires the loader lock
6335 MonoGenericContainer *
6336 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
6338 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
6339 guint32 cols [MONO_GENERICPARAM_SIZE];
6340 guint32 i, owner = 0, n;
6341 MonoGenericContainer *container;
6342 MonoGenericParamFull *params;
6343 MonoGenericContext *context;
6345 if (!(i = mono_metadata_get_generic_param_row (image, token, &owner)))
6347 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
6350 container = (MonoGenericContainer *)mono_image_alloc0 (image, sizeof (MonoGenericContainer));
6351 container->owner.image = image; // Temporarily mark as anonymous, but this will be overriden by caller
6352 container->is_anonymous = TRUE;
6355 params = (MonoGenericParamFull *)g_realloc (params, sizeof (MonoGenericParamFull) * n);
6356 memset (¶ms [n - 1], 0, sizeof (MonoGenericParamFull));
6357 params [n - 1].param.owner = container;
6358 params [n - 1].param.num = cols [MONO_GENERICPARAM_NUMBER];
6359 params [n - 1].info.token = i | MONO_TOKEN_GENERIC_PARAM;
6360 params [n - 1].info.flags = cols [MONO_GENERICPARAM_FLAGS];
6361 params [n - 1].info.name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
6362 if (params [n - 1].param.num != n - 1)
6363 g_warning ("GenericParam table unsorted or hole in generic param sequence: token %d", i);
6364 if (++i > tdef->rows)
6366 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
6367 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
6369 container->type_argc = n;
6370 container->type_params = (MonoGenericParamFull *)mono_image_alloc0 (image, sizeof (MonoGenericParamFull) * n);
6371 memcpy (container->type_params, params, sizeof (MonoGenericParamFull) * n);
6373 container->parent = parent_container;
6375 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
6376 container->is_method = 1;
6378 g_assert (container->parent == NULL || container->is_method);
6380 context = &container->context;
6381 if (container->is_method) {
6382 context->class_inst = container->parent ? container->parent->context.class_inst : NULL;
6383 context->method_inst = mono_get_shared_generic_inst (container);
6385 context->class_inst = mono_get_shared_generic_inst (container);
6392 mono_get_shared_generic_inst (MonoGenericContainer *container)
6394 MonoType **type_argv;
6396 MonoGenericInst *nginst;
6399 type_argv = g_new0 (MonoType *, container->type_argc);
6400 helper = g_new0 (MonoType, container->type_argc);
6402 for (i = 0; i < container->type_argc; i++) {
6403 MonoType *t = &helper [i];
6405 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
6406 t->data.generic_param = mono_generic_container_get_param (container, i);
6411 nginst = mono_metadata_get_generic_inst (container->type_argc, type_argv);
6420 * mono_type_is_byref:
6421 * @type: the MonoType operated on
6423 * Returns: #TRUE if @type represents a type passed by reference,
6427 mono_type_is_byref (MonoType *type)
6433 * mono_type_get_type:
6434 * @type: the MonoType operated on
6436 * Returns: the IL type value for @type. This is one of the MonoTypeEnum
6437 * enum members like MONO_TYPE_I4 or MONO_TYPE_STRING.
6440 mono_type_get_type (MonoType *type)
6446 * mono_type_get_signature:
6447 * @type: the MonoType operated on
6449 * It is only valid to call this function if @type is a MONO_TYPE_FNPTR.
6451 * Returns: the MonoMethodSignature pointer that describes the signature
6452 * of the function pointer @type represents.
6454 MonoMethodSignature*
6455 mono_type_get_signature (MonoType *type)
6457 g_assert (type->type == MONO_TYPE_FNPTR);
6458 return type->data.method;
6462 * mono_type_get_class:
6463 * @type: the MonoType operated on
6465 * It is only valid to call this function if @type is a MONO_TYPE_CLASS or a
6466 * MONO_TYPE_VALUETYPE. For more general functionality, use mono_class_from_mono_type (),
6469 * Returns: the MonoClass pointer that describes the class that @type represents.
6472 mono_type_get_class (MonoType *type)
6474 /* FIXME: review the runtime users before adding the assert here */
6475 return type->data.klass;
6479 * mono_type_get_array_type:
6480 * @type: the MonoType operated on
6482 * It is only valid to call this function if @type is a MONO_TYPE_ARRAY.
6484 * Returns: a MonoArrayType struct describing the array type that @type
6485 * represents. The info includes details such as rank, array element type
6486 * and the sizes and bounds of multidimensional arrays.
6489 mono_type_get_array_type (MonoType *type)
6491 return type->data.array;
6495 * mono_type_get_ptr_type:
6496 * @type: the MonoType operated on
6498 * It is only valid to call this function if @type is a MONO_TYPE_PTR.
6501 * Returns: the MonoType pointer that describes the type that @type
6502 * represents a pointer to.
6505 mono_type_get_ptr_type (MonoType *type)
6507 g_assert (type->type == MONO_TYPE_PTR);
6508 return type->data.type;
6512 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
6514 /* FIXME: implement */
6519 * mono_type_is_struct:
6520 * @type: the MonoType operated on
6522 * Returns: #TRUE is @type is a struct, that is a ValueType but not en enum
6523 * or a basic type like System.Int32. #FALSE otherwise.
6526 mono_type_is_struct (MonoType *type)
6528 return (!type->byref && ((type->type == MONO_TYPE_VALUETYPE &&
6529 !type->data.klass->enumtype) || (type->type == MONO_TYPE_TYPEDBYREF) ||
6530 ((type->type == MONO_TYPE_GENERICINST) &&
6531 mono_metadata_generic_class_is_valuetype (type->data.generic_class) &&
6532 !type->data.generic_class->container_class->enumtype)));
6536 * mono_type_is_void:
6537 * @type: the MonoType operated on
6539 * Returns: #TRUE is @type is System.Void. #FALSE otherwise.
6542 mono_type_is_void (MonoType *type)
6544 return (type && (type->type == MONO_TYPE_VOID) && !type->byref);
6548 * mono_type_is_pointer:
6549 * @type: the MonoType operated on
6551 * Returns: #TRUE is @type is a managed or unmanaged pointer type. #FALSE otherwise.
6554 mono_type_is_pointer (MonoType *type)
6556 return (type && ((type->byref || (type->type == MONO_TYPE_I) || type->type == MONO_TYPE_STRING)
6557 || (type->type == MONO_TYPE_SZARRAY) || (type->type == MONO_TYPE_CLASS) ||
6558 (type->type == MONO_TYPE_U) || (type->type == MONO_TYPE_OBJECT) ||
6559 (type->type == MONO_TYPE_ARRAY) || (type->type == MONO_TYPE_PTR) ||
6560 (type->type == MONO_TYPE_FNPTR)));
6564 * mono_type_is_reference:
6565 * @type: the MonoType operated on
6567 * Returns: #TRUE is @type represents an object reference . #FALSE otherwise.
6570 mono_type_is_reference (MonoType *type)
6572 return (type && (((type->type == MONO_TYPE_STRING) ||
6573 (type->type == MONO_TYPE_SZARRAY) || (type->type == MONO_TYPE_CLASS) ||
6574 (type->type == MONO_TYPE_OBJECT) || (type->type == MONO_TYPE_ARRAY)) ||
6575 ((type->type == MONO_TYPE_GENERICINST) &&
6576 !mono_metadata_generic_class_is_valuetype (type->data.generic_class))));
6580 mono_type_is_generic_parameter (MonoType *type)
6582 return !type->byref && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR);
6586 * mono_signature_get_return_type:
6587 * @sig: the method signature inspected
6589 * Returns: the return type of the method signature @sig
6592 mono_signature_get_return_type (MonoMethodSignature *sig)
6598 * mono_signature_get_params:
6599 * @sig: the method signature inspected
6600 * #iter: pointer to an iterator
6602 * Iterates over the parameters for the method signature @sig.
6603 * A void* pointer must be initualized to #NULL to start the iteration
6604 * and it's address is passed to this function repeteadly until it returns
6607 * Returns: the next parameter type of the method signature @sig,
6608 * #NULL when finished.
6611 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
6617 /* start from the first */
6618 if (sig->param_count) {
6619 *iter = &sig->params [0];
6620 return sig->params [0];
6626 type = (MonoType **)*iter;
6628 if (type < &sig->params [sig->param_count]) {
6636 * mono_signature_get_param_count:
6637 * @sig: the method signature inspected
6639 * Returns: the number of parameters in the method signature @sig.
6642 mono_signature_get_param_count (MonoMethodSignature *sig)
6644 return sig->param_count;
6648 * mono_signature_get_call_conv:
6649 * @sig: the method signature inspected
6651 * Returns: the call convention of the method signature @sig.
6654 mono_signature_get_call_conv (MonoMethodSignature *sig)
6656 return sig->call_convention;
6660 * mono_signature_vararg_start:
6661 * @sig: the method signature inspected
6663 * Returns: the number of the first vararg parameter in the
6664 * method signature @sig. -1 if this is not a vararg signature.
6667 mono_signature_vararg_start (MonoMethodSignature *sig)
6669 return sig->sentinelpos;
6673 * mono_signature_is_instance:
6674 * @sig: the method signature inspected
6676 * Returns: #TRUE if this the method signature @sig has an implicit
6677 * first instance argument. #FALSE otherwise.
6680 mono_signature_is_instance (MonoMethodSignature *sig)
6682 return sig->hasthis;
6686 * mono_signature_param_is_out
6687 * @sig: the method signature inspected
6688 * @param_num: the 0-based index of the inspected parameter
6690 * Returns: #TRUE if the parameter is an out parameter, #FALSE
6694 mono_signature_param_is_out (MonoMethodSignature *sig, int param_num)
6696 g_assert (param_num >= 0 && param_num < sig->param_count);
6697 return (sig->params [param_num]->attrs & PARAM_ATTRIBUTE_OUT) != 0;
6701 * mono_signature_explicit_this:
6702 * @sig: the method signature inspected
6704 * Returns: #TRUE if this the method signature @sig has an explicit
6705 * instance argument. #FALSE otherwise.
6708 mono_signature_explicit_this (MonoMethodSignature *sig)
6710 return sig->explicit_this;
6713 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
6715 mono_aligned_addr_hash (gconstpointer ptr)
6717 /* Same hashing we use for objects */
6718 return (GPOINTER_TO_UINT (ptr) >> 3) * 2654435761u;
6722 * If @field belongs to an inflated generic class, return the corresponding field of the
6723 * generic type definition class.
6726 mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field)
6731 if (!mono_class_is_ginst (field->parent))
6734 gtd = mono_class_get_generic_class (field->parent)->container_class;
6735 offset = field - field->parent->fields;
6736 return gtd->fields + offset;
6740 * If @event belongs to an inflated generic class, return the corresponding event of the
6741 * generic type definition class.
6744 mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event)
6749 if (!mono_class_is_ginst (event->parent))
6752 gtd = mono_class_get_generic_class (event->parent)->container_class;
6753 offset = event - mono_class_get_event_info (event->parent)->events;
6754 return mono_class_get_event_info (gtd)->events + offset;
6758 * If @property belongs to an inflated generic class, return the corresponding property of the
6759 * generic type definition class.
6762 mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property)
6764 MonoClassPropertyInfo *info;
6768 if (!mono_class_is_ginst (property->parent))
6771 info = mono_class_get_property_info (property->parent);
6772 gtd = mono_class_get_generic_class (property->parent)->container_class;
6773 offset = property - info->properties;
6774 return mono_class_get_property_info (gtd)->properties + offset;
6778 mono_method_get_wrapper_cache (MonoMethod *method)
6780 if (method->is_inflated) {
6781 MonoMethodInflated *imethod = (MonoMethodInflated *)method;
6782 return &imethod->owner->wrapper_caches;
6784 return &method->klass->image->wrapper_caches;
6788 // 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.
6791 * mono_find_image_set_owner:
6793 * Find the imageset, if any, which a given pointer is located in the memory of.
6796 mono_find_image_set_owner (void *ptr)
6798 MonoImageSet *owner = NULL;
6805 for (i = 0; !owner && i < image_sets->len; ++i) {
6806 MonoImageSet *set = (MonoImageSet *)g_ptr_array_index (image_sets, i);
6807 if (mono_mempool_contains_addr (set->mempool, ptr))
6812 image_sets_unlock ();