2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2002 Ximian, Inc.
17 #include "tabledefs.h"
18 #include "mono-endian.h"
20 #include "tokentype.h"
21 #include "metadata-internals.h"
22 #include "class-internals.h"
26 static void do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *generic_container,
27 const char *ptr, const char **rptr);
29 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
30 static gboolean _mono_metadata_generic_class_equal (MonoGenericClass *g1, MonoGenericClass *g2,
31 gboolean signature_only);
34 * Encoding of the "description" argument:
36 * identifier [CODE ARG]
38 * If CODE is ':', then a lookup on table ARG is performed
39 * If CODE is '=', then a lookup in the aliased-table ARG is performed
40 * If CODE is '#', then this encodes a flag, ARG is the flag name.
42 * Aliased table for example is `CustomAttributeType' which depending on the
43 * information might refer to different tables.
46 const static MonoMetaTable AssemblySchema [] = {
47 { MONO_MT_UINT32, "HashId" },
48 { MONO_MT_UINT16, "Major" },
49 { MONO_MT_UINT16, "Minor" },
50 { MONO_MT_UINT16, "BuildNumber" },
51 { MONO_MT_UINT16, "RevisionNumber" },
52 { MONO_MT_UINT32, "Flags" },
53 { MONO_MT_BLOB_IDX, "PublicKey" },
54 { MONO_MT_STRING_IDX, "Name" },
55 { MONO_MT_STRING_IDX, "Culture" },
59 const static MonoMetaTable AssemblyOSSchema [] = {
60 { MONO_MT_UINT32, "OSPlatformID" },
61 { MONO_MT_UINT32, "OSMajor" },
62 { MONO_MT_UINT32, "OSMinor" },
66 const static MonoMetaTable AssemblyProcessorSchema [] = {
67 { MONO_MT_UINT32, "Processor" },
71 const static MonoMetaTable AssemblyRefSchema [] = {
72 { MONO_MT_UINT16, "Major" },
73 { MONO_MT_UINT16, "Minor" },
74 { MONO_MT_UINT16, "Build" },
75 { MONO_MT_UINT16, "Revision" },
76 { MONO_MT_UINT32, "Flags" },
77 { MONO_MT_BLOB_IDX, "PublicKeyOrToken" },
78 { MONO_MT_STRING_IDX, "Name" },
79 { MONO_MT_STRING_IDX, "Culture" },
80 { MONO_MT_BLOB_IDX, "HashValue" },
84 const static MonoMetaTable AssemblyRefOSSchema [] = {
85 { MONO_MT_UINT32, "OSPlatformID" },
86 { MONO_MT_UINT32, "OSMajorVersion" },
87 { MONO_MT_UINT32, "OSMinorVersion" },
88 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
92 const static MonoMetaTable AssemblyRefProcessorSchema [] = {
93 { MONO_MT_UINT32, "Processor" },
94 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
98 const static MonoMetaTable ClassLayoutSchema [] = {
99 { MONO_MT_UINT16, "PackingSize" },
100 { MONO_MT_UINT32, "ClassSize" },
101 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
102 { MONO_MT_END, NULL }
105 const static MonoMetaTable ConstantSchema [] = {
106 { MONO_MT_UINT8, "Type" },
107 { MONO_MT_UINT8, "PaddingZero" },
108 { MONO_MT_CONST_IDX, "Parent" },
109 { MONO_MT_BLOB_IDX, "Value" },
110 { MONO_MT_END, NULL }
113 const static MonoMetaTable CustomAttributeSchema [] = {
114 { MONO_MT_HASCAT_IDX, "Parent" },
115 { MONO_MT_CAT_IDX, "Type" },
116 { MONO_MT_BLOB_IDX, "Value" },
117 { MONO_MT_END, NULL }
120 const static MonoMetaTable DeclSecuritySchema [] = {
121 { MONO_MT_UINT16, "Action" },
122 { MONO_MT_HASDEC_IDX, "Parent" },
123 { MONO_MT_BLOB_IDX, "PermissionSet" },
124 { MONO_MT_END, NULL }
127 const static MonoMetaTable EventMapSchema [] = {
128 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
129 { MONO_MT_TABLE_IDX, "EventList:Event" },
130 { MONO_MT_END, NULL }
133 const static MonoMetaTable EventSchema [] = {
134 { MONO_MT_UINT16, "EventFlags#EventAttribute" },
135 { MONO_MT_STRING_IDX, "Name" },
136 { MONO_MT_TABLE_IDX, "EventType" }, /* TypeDef or TypeRef */
137 { MONO_MT_END, NULL }
140 const static MonoMetaTable ExportedTypeSchema [] = {
141 { MONO_MT_UINT32, "Flags" },
142 { MONO_MT_TABLE_IDX, "TypeDefId" },
143 { MONO_MT_STRING_IDX, "TypeName" },
144 { MONO_MT_STRING_IDX, "TypeNameSpace" },
145 { MONO_MT_IMPL_IDX, "Implementation" },
146 { MONO_MT_END, NULL }
149 const static MonoMetaTable FieldSchema [] = {
150 { MONO_MT_UINT16, "Flags" },
151 { MONO_MT_STRING_IDX, "Name" },
152 { MONO_MT_BLOB_IDX, "Signature" },
153 { MONO_MT_END, NULL }
155 const static MonoMetaTable FieldLayoutSchema [] = {
156 { MONO_MT_UINT32, "Offset" },
157 { MONO_MT_TABLE_IDX, "Field:Field" },
158 { MONO_MT_END, NULL }
161 const static MonoMetaTable FieldMarshalSchema [] = {
162 { MONO_MT_HFM_IDX, "Parent" },
163 { MONO_MT_BLOB_IDX, "NativeType" },
164 { MONO_MT_END, NULL }
166 const static MonoMetaTable FieldRVASchema [] = {
167 { MONO_MT_UINT32, "RVA" },
168 { MONO_MT_TABLE_IDX, "Field:Field" },
169 { MONO_MT_END, NULL }
172 const static MonoMetaTable FileSchema [] = {
173 { MONO_MT_UINT32, "Flags" },
174 { MONO_MT_STRING_IDX, "Name" },
175 { MONO_MT_BLOB_IDX, "Value" },
176 { MONO_MT_END, NULL }
179 const static MonoMetaTable ImplMapSchema [] = {
180 { MONO_MT_UINT16, "MappingFlag" },
181 { MONO_MT_MF_IDX, "MemberForwarded" },
182 { MONO_MT_STRING_IDX, "ImportName" },
183 { MONO_MT_TABLE_IDX, "ImportScope:ModuleRef" },
184 { MONO_MT_END, NULL }
187 const static MonoMetaTable InterfaceImplSchema [] = {
188 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
189 { MONO_MT_TDOR_IDX, "Interface=TypeDefOrRef" },
190 { MONO_MT_END, NULL }
193 const static MonoMetaTable ManifestResourceSchema [] = {
194 { MONO_MT_UINT32, "Offset" },
195 { MONO_MT_UINT32, "Flags" },
196 { MONO_MT_STRING_IDX, "Name" },
197 { MONO_MT_IMPL_IDX, "Implementation" },
198 { MONO_MT_END, NULL }
201 const static MonoMetaTable MemberRefSchema [] = {
202 { MONO_MT_MRP_IDX, "Class" },
203 { MONO_MT_STRING_IDX, "Name" },
204 { MONO_MT_BLOB_IDX, "Signature" },
205 { MONO_MT_END, NULL }
208 const static MonoMetaTable MethodSchema [] = {
209 { MONO_MT_UINT32, "RVA" },
210 { MONO_MT_UINT16, "ImplFlags#MethodImplAttributes" },
211 { MONO_MT_UINT16, "Flags#MethodAttribute" },
212 { MONO_MT_STRING_IDX, "Name" },
213 { MONO_MT_BLOB_IDX, "Signature" },
214 { MONO_MT_TABLE_IDX, "ParamList:Param" },
215 { MONO_MT_END, NULL }
218 const static MonoMetaTable MethodImplSchema [] = {
219 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
220 { MONO_MT_MDOR_IDX, "MethodBody" },
221 { MONO_MT_MDOR_IDX, "MethodDeclaration" },
222 { MONO_MT_END, NULL }
225 const static MonoMetaTable MethodSemanticsSchema [] = {
226 { MONO_MT_UINT16, "MethodSemantic" },
227 { MONO_MT_TABLE_IDX, "Method:Method" },
228 { MONO_MT_HS_IDX, "Association" },
229 { MONO_MT_END, NULL }
232 const static MonoMetaTable ModuleSchema [] = {
233 { MONO_MT_UINT16, "Generation" },
234 { MONO_MT_STRING_IDX, "Name" },
235 { MONO_MT_GUID_IDX, "MVID" },
236 { MONO_MT_GUID_IDX, "EncID" },
237 { MONO_MT_GUID_IDX, "EncBaseID" },
238 { MONO_MT_END, NULL }
241 const static MonoMetaTable ModuleRefSchema [] = {
242 { MONO_MT_STRING_IDX, "Name" },
243 { MONO_MT_END, NULL }
246 const static MonoMetaTable NestedClassSchema [] = {
247 { MONO_MT_TABLE_IDX, "NestedClass:TypeDef" },
248 { MONO_MT_TABLE_IDX, "EnclosingClass:TypeDef" },
249 { MONO_MT_END, NULL }
252 const static MonoMetaTable ParamSchema [] = {
253 { MONO_MT_UINT16, "Flags" },
254 { MONO_MT_UINT16, "Sequence" },
255 { MONO_MT_STRING_IDX, "Name" },
256 { MONO_MT_END, NULL }
259 const static MonoMetaTable PropertySchema [] = {
260 { MONO_MT_UINT16, "Flags" },
261 { MONO_MT_STRING_IDX, "Name" },
262 { MONO_MT_BLOB_IDX, "Type" },
263 { MONO_MT_END, NULL }
266 const static MonoMetaTable PropertyMapSchema [] = {
267 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
268 { MONO_MT_TABLE_IDX, "PropertyList:Property" },
269 { MONO_MT_END, NULL }
272 const static MonoMetaTable StandaloneSigSchema [] = {
273 { MONO_MT_BLOB_IDX, "Signature" },
274 { MONO_MT_END, NULL }
277 const static MonoMetaTable TypeDefSchema [] = {
278 { MONO_MT_UINT32, "Flags" },
279 { MONO_MT_STRING_IDX, "Name" },
280 { MONO_MT_STRING_IDX, "Namespace" },
281 { MONO_MT_TDOR_IDX, "Extends" },
282 { MONO_MT_TABLE_IDX, "FieldList:Field" },
283 { MONO_MT_TABLE_IDX, "MethodList:Method" },
284 { MONO_MT_END, NULL }
287 const static MonoMetaTable TypeRefSchema [] = {
288 { MONO_MT_RS_IDX, "ResolutionScope=ResolutionScope" },
289 { MONO_MT_STRING_IDX, "Name" },
290 { MONO_MT_STRING_IDX, "Namespace" },
291 { MONO_MT_END, NULL }
294 const static MonoMetaTable TypeSpecSchema [] = {
295 { MONO_MT_BLOB_IDX, "Signature" },
296 { MONO_MT_END, NULL }
299 const static MonoMetaTable GenericParamSchema [] = {
300 { MONO_MT_UINT16, "Number" },
301 { MONO_MT_UINT16, "Flags" },
302 { MONO_MT_TABLE_IDX, "Owner" }, /* TypeDef or MethodDef */
303 { MONO_MT_STRING_IDX, "Name" },
305 /* soon to be removed */
306 { MONO_MT_TABLE_IDX, "Kind" },
308 { MONO_MT_END, NULL }
311 const static MonoMetaTable MethodSpecSchema [] = {
312 { MONO_MT_MDOR_IDX, "Method" },
313 { MONO_MT_BLOB_IDX, "Signature" },
314 { MONO_MT_END, NULL }
317 const static MonoMetaTable GenericParamConstraintSchema [] = {
318 { MONO_MT_TABLE_IDX, "GenericParam" },
319 { MONO_MT_TDOR_IDX, "Constraint" },
320 { MONO_MT_END, NULL }
323 const static struct {
324 const MonoMetaTable *table;
327 /* 0 */ { ModuleSchema, "Module" },
328 /* 1 */ { TypeRefSchema, "TypeRef" },
329 /* 2 */ { TypeDefSchema, "TypeDef" },
330 /* 3 */ { NULL, NULL },
331 /* 4 */ { FieldSchema, "Field" },
332 /* 5 */ { NULL, NULL },
333 /* 6 */ { MethodSchema, "Method" },
334 /* 7 */ { NULL, NULL },
335 /* 8 */ { ParamSchema, "Param" },
336 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
337 /* A */ { MemberRefSchema, "MemberRef" },
338 /* B */ { ConstantSchema, "Constant" },
339 /* C */ { CustomAttributeSchema, "CustomAttribute" },
340 /* D */ { FieldMarshalSchema, "FieldMarshal" },
341 /* E */ { DeclSecuritySchema, "DeclSecurity" },
342 /* F */ { ClassLayoutSchema, "ClassLayout" },
343 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
344 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
345 /* 12 */ { EventMapSchema, "EventMap" },
346 /* 13 */ { NULL, NULL },
347 /* 14 */ { EventSchema, "Event" },
348 /* 15 */ { PropertyMapSchema, "PropertyMap" },
349 /* 16 */ { NULL, NULL },
350 /* 17 */ { PropertySchema, "PropertyTable" },
351 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
352 /* 19 */ { MethodImplSchema, "MethodImpl" },
353 /* 1A */ { ModuleRefSchema, "ModuleRef" },
354 /* 1B */ { TypeSpecSchema, "TypeSpec" },
355 /* 1C */ { ImplMapSchema, "ImplMap" },
356 /* 1D */ { FieldRVASchema, "FieldRVA" },
357 /* 1E */ { NULL, NULL },
358 /* 1F */ { NULL, NULL },
359 /* 20 */ { AssemblySchema, "Assembly" },
360 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
361 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
362 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
363 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
364 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
365 /* 26 */ { FileSchema, "File" },
366 /* 27 */ { ExportedTypeSchema, "ExportedType" },
367 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
368 /* 29 */ { NestedClassSchema, "NestedClass" },
369 /* 2A */ { GenericParamSchema, "GenericParam" },
370 /* 2B */ { MethodSpecSchema, "MethodSpec" },
371 /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
375 * mono_meta_table_name:
376 * @table: table index
378 * Returns: the name for the @table index
381 mono_meta_table_name (int table)
383 if ((table < 0) || (table > 0x2c))
386 return tables [table].name;
389 /* The guy who wrote the spec for this should not be allowed near a
392 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
393 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
394 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
395 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
396 inverse of this mapping.
399 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
400 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
402 /* Reference: Partition II - 23.2.6 */
404 * mono_metadata_compute_size:
405 * @meta: metadata context
406 * @tableindex: metadata table number
407 * @result_bitfield: pointer to guint32 where to store additional info
409 * mono_metadata_compute_size() computes the lenght in bytes of a single
410 * row in a metadata table. The size of each column is encoded in the
411 * @result_bitfield return value along with the number of columns in the table.
412 * the resulting bitfield should be handed to the mono_metadata_table_size()
413 * and mono_metadata_table_count() macros.
416 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
418 guint32 bitfield = 0;
419 int size = 0, field_size = 0;
422 const MonoMetaTable *table = tables [tableindex].table;
424 for (i = 0; (code = table [i].code) != MONO_MT_END; i++){
427 field_size = 4; break;
430 field_size = 2; break;
433 field_size = 1; break;
435 case MONO_MT_BLOB_IDX:
436 field_size = meta->idx_blob_wide ? 4 : 2; break;
438 case MONO_MT_STRING_IDX:
439 field_size = meta->idx_string_wide ? 4 : 2; break;
441 case MONO_MT_GUID_IDX:
442 field_size = meta->idx_guid_wide ? 4 : 2; break;
444 case MONO_MT_TABLE_IDX:
445 /* Uhm, a table index can point to other tables besides the current one
446 * so, it's not correct to use the rowcount of the current table to
447 * get the size for this column - lupus
449 switch (tableindex) {
450 case MONO_TABLE_ASSEMBLYREFOS:
452 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
453 case MONO_TABLE_ASSEMBLYPROCESSOR:
455 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
456 case MONO_TABLE_CLASSLAYOUT:
458 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
459 case MONO_TABLE_EVENTMAP:
460 g_assert (i == 0 || i == 1);
461 field_size = i ? idx_size (MONO_TABLE_EVENT):
462 idx_size(MONO_TABLE_TYPEDEF);
464 case MONO_TABLE_EVENT:
466 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
467 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
469 case MONO_TABLE_EXPORTEDTYPE:
471 /* the index is in another metadata file, so it must be 4 */
472 field_size = 4; break;
473 case MONO_TABLE_FIELDLAYOUT:
475 field_size = idx_size (MONO_TABLE_FIELD); break;
476 case MONO_TABLE_FIELDRVA:
478 field_size = idx_size (MONO_TABLE_FIELD); break;
479 case MONO_TABLE_IMPLMAP:
481 field_size = idx_size (MONO_TABLE_MODULEREF); break;
482 case MONO_TABLE_INTERFACEIMPL:
484 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
485 case MONO_TABLE_METHOD:
487 field_size = idx_size (MONO_TABLE_PARAM); break;
488 case MONO_TABLE_METHODIMPL:
490 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
491 case MONO_TABLE_METHODSEMANTICS:
493 field_size = idx_size (MONO_TABLE_METHOD); break;
494 case MONO_TABLE_NESTEDCLASS:
495 g_assert (i == 0 || i == 1);
496 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
497 case MONO_TABLE_PROPERTYMAP:
498 g_assert (i == 0 || i == 1);
499 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
500 idx_size(MONO_TABLE_TYPEDEF);
502 case MONO_TABLE_TYPEDEF:
503 g_assert (i == 4 || i == 5);
504 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
505 idx_size(MONO_TABLE_METHOD);
506 case MONO_TABLE_GENERICPARAM:
507 g_assert (i == 2 || i == 4 || i == 5);
509 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
511 field_size = idx_size (MONO_TABLE_TYPEDEF);
513 field_size = idx_size (MONO_TABLE_TYPEDEF);
516 case MONO_TABLE_GENERICPARAMCONSTRAINT:
518 field_size = idx_size (MONO_TABLE_GENERICPARAM);
522 g_assert_not_reached ();
524 if (tableindex != MONO_TABLE_EXPORTEDTYPE && field_size != idx_size (tableindex))
525 g_warning ("size changed (%d to %d)", idx_size (tableindex), field_size);
530 * HasConstant: ParamDef, FieldDef, Property
532 case MONO_MT_CONST_IDX:
533 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
534 meta->tables [MONO_TABLE_FIELD].rows);
535 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
537 /* 2 bits to encode tag */
538 field_size = rtsize (n, 16-2);
542 * HasCustomAttribute: points to any table but
545 case MONO_MT_HASCAT_IDX:
547 * We believe that since the signature and
548 * permission are indexing the Blob heap,
549 * we should consider the blob size first
551 /* I'm not a believer - lupus
552 if (meta->idx_blob_wide){
557 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
558 meta->tables [MONO_TABLE_FIELD].rows);
559 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
560 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
561 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
562 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
563 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
564 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
565 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
566 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
567 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
568 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
569 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
570 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
571 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
572 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
573 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
574 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
575 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
577 /* 5 bits to encode */
578 field_size = rtsize (n, 16-5);
582 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
583 * MemberRef and String.
585 case MONO_MT_CAT_IDX:
586 /* String is a heap, if it is wide, we know the size */
588 if (meta->idx_string_wide){
593 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
594 meta->tables [MONO_TABLE_TYPEDEF].rows);
595 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
596 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
598 /* 3 bits to encode */
599 field_size = rtsize (n, 16-3);
603 * HasDeclSecurity: Typedef, MethodDef, Assembly
605 case MONO_MT_HASDEC_IDX:
606 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
607 meta->tables [MONO_TABLE_METHOD].rows);
608 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
610 /* 2 bits to encode */
611 field_size = rtsize (n, 16-2);
615 * Implementation: File, AssemblyRef, ExportedType
617 case MONO_MT_IMPL_IDX:
618 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
619 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
620 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
622 /* 2 bits to encode tag */
623 field_size = rtsize (n, 16-2);
627 * HasFieldMarshall: FieldDef, ParamDef
629 case MONO_MT_HFM_IDX:
630 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
631 meta->tables [MONO_TABLE_PARAM].rows);
633 /* 1 bit used to encode tag */
634 field_size = rtsize (n, 16-1);
638 * MemberForwarded: FieldDef, MethodDef
641 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
642 meta->tables [MONO_TABLE_METHOD].rows);
644 /* 1 bit used to encode tag */
645 field_size = rtsize (n, 16-1);
649 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
651 * It is TypeDef, _TypeRef_, TypeSpec, instead.
653 case MONO_MT_TDOR_IDX:
654 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
655 meta->tables [MONO_TABLE_TYPEREF].rows);
656 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
658 /* 2 bits to encode */
659 field_size = rtsize (n, 16-2);
663 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
665 case MONO_MT_MRP_IDX:
666 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
667 meta->tables [MONO_TABLE_TYPEREF].rows);
668 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
669 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
670 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
671 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
673 /* 3 bits to encode */
674 field_size = rtsize (n, 16 - 3);
677 case MONO_MT_MDOR_IDX:
680 * MethodDefOrRef: MethodDef, MemberRef
683 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
684 meta->tables [MONO_TABLE_MEMBERREF].rows);
686 /* 1 bit used to encode tag */
687 field_size = rtsize (n, 16-1);
691 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
694 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
695 meta->tables [MONO_TABLE_MODULEREF].rows);
696 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
697 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
699 /* 2 bits used to encode tag (ECMA spec claims 3) */
700 field_size = rtsize (n, 16 - 2);
705 * encode field size as follows (we just need to
712 bitfield |= (field_size-1) << shift;
715 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
718 *result_bitfield = (i << 24) | bitfield;
723 * mono_metadata_compute_table_bases:
724 * @meta: metadata context to compute table values
726 * Computes the table bases for the metadata structure.
727 * This is an internal function used by the image loader code.
730 mono_metadata_compute_table_bases (MonoImage *meta)
733 const char *base = meta->tables_base;
735 for (i = 0; i < 64; i++){
736 if (meta->tables [i].rows == 0)
739 meta->tables [i].row_size = mono_metadata_compute_size (
740 meta, i, &meta->tables [i].size_bitfield);
741 meta->tables [i].base = base;
742 base += meta->tables [i].rows * meta->tables [i].row_size;
747 * mono_metadata_locate:
748 * @meta: metadata context
749 * @table: table code.
750 * @idx: index of element to retrieve from @table.
752 * Returns: a pointer to the @idx element in the metadata table
753 * whose code is @table.
756 mono_metadata_locate (MonoImage *meta, int table, int idx)
758 /* idx == 0 refers always to NULL */
759 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
761 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
765 * mono_metadata_locate_token:
766 * @meta: metadata context
767 * @token: metadata token
769 * Returns: a pointer to the data in the metadata represented by the
773 mono_metadata_locate_token (MonoImage *meta, guint32 token)
775 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
779 * mono_metadata_get_table:
780 * @table: table to retrieve
782 * Returns: the MonoMetaTable structure for table @table
784 const MonoMetaTable *
785 mono_metadata_get_table (MonoMetaTableEnum table)
789 g_return_val_if_fail ((x > 0) && (x <= MONO_TABLE_LAST), NULL);
791 return tables [table].table;
795 * mono_metadata_string_heap:
796 * @meta: metadata context
797 * @index: index into the string heap.
799 * Returns: an in-memory pointer to the @index in the string heap.
802 mono_metadata_string_heap (MonoImage *meta, guint32 index)
804 g_return_val_if_fail (index < meta->heap_strings.size, "");
805 return meta->heap_strings.data + index;
809 * mono_metadata_user_string:
810 * @meta: metadata context
811 * @index: index into the user string heap.
813 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
816 mono_metadata_user_string (MonoImage *meta, guint32 index)
818 g_return_val_if_fail (index < meta->heap_us.size, "");
819 return meta->heap_us.data + index;
823 * mono_metadata_blob_heap:
824 * @meta: metadata context
825 * @index: index into the blob.
827 * Returns: an in-memory pointer to the @index in the Blob heap.
830 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
832 g_return_val_if_fail (index < meta->heap_blob.size, "");
833 return meta->heap_blob.data + index;
837 * mono_metadata_guid_heap:
838 * @meta: metadata context
839 * @index: index into the guid heap.
841 * Returns: an in-memory pointer to the @index in the guid heap.
844 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
847 index *= 16; /* adjust for guid size and 1-based index */
848 g_return_val_if_fail (index < meta->heap_guid.size, "");
849 return meta->heap_guid.data + index;
853 dword_align (const char *ptr)
855 #if SIZEOF_VOID_P == 8
856 return (const char *) (((guint64) (ptr + 3)) & ~3);
858 return (const char *) (((guint32) (ptr + 3)) & ~3);
863 * mono_metadata_decode_row:
864 * @t: table to extract information from.
865 * @idx: index in table.
866 * @res: array of @res_size cols to store the results in
868 * This decompresses the metadata element @idx in table @t
869 * into the guint32 @res array that has res_size elements
872 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
874 guint32 bitfield = t->size_bitfield;
875 int i, count = mono_metadata_table_count (bitfield);
876 const char *data = t->base + idx * t->row_size;
878 g_assert (res_size == count);
880 for (i = 0; i < count; i++){
881 int n = mono_metadata_table_size (bitfield, i);
885 res [i] = *data; break;
887 res [i] = read16 (data); break;
890 res [i] = read32 (data); break;
893 g_assert_not_reached ();
900 * mono_metadata_decode_row_col:
901 * @t: table to extract information from.
902 * @idx: index for row in table.
903 * @col: column in the row.
905 * This function returns the value of column @col from the @idx
906 * row in the table @t.
909 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
911 guint32 bitfield = t->size_bitfield;
913 register const char *data = t->base + idx * t->row_size;
916 g_assert (col < mono_metadata_table_count (bitfield));
918 n = mono_metadata_table_size (bitfield, 0);
919 for (i = 0; i < col; ++i) {
921 n = mono_metadata_table_size (bitfield, i + 1);
927 return read16 (data);
929 return read32 (data);
931 g_assert_not_reached ();
936 * mono_metadata_decode_blob_size:
937 * @ptr: pointer to a blob object
938 * @rptr: the new position of the pointer
940 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
942 * Returns: the size of the blob object
945 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
947 const unsigned char *ptr = (const unsigned char *)xptr;
950 if ((*ptr & 0x80) == 0){
951 size = ptr [0] & 0x7f;
953 } else if ((*ptr & 0x40) == 0){
954 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
957 size = ((ptr [0] & 0x1f) << 24) +
970 * mono_metadata_decode_value:
971 * @ptr: pointer to decode from
972 * @rptr: the new position of the pointer
974 * This routine decompresses 32-bit values as specified in the "Blob and
975 * Signature" section (22.2)
977 * Returns: the decoded value
980 mono_metadata_decode_value (const char *_ptr, const char **rptr)
982 const unsigned char *ptr = (const unsigned char *) _ptr;
983 unsigned char b = *ptr;
986 if ((b & 0x80) == 0){
989 } else if ((b & 0x40) == 0){
990 len = ((b & 0x3f) << 8 | ptr [1]);
993 len = ((b & 0x1f) << 24) |
1006 * mono_metadata_parse_typedef_or_ref:
1007 * @m: a metadata context.
1008 * @ptr: a pointer to an encoded TypedefOrRef in @m
1009 * @rptr: pointer updated to match the end of the decoded stream
1011 * Returns: a token valid in the @m metadata decoded from
1012 * the compressed representation.
1015 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1018 token = mono_metadata_decode_value (ptr, &ptr);
1021 return mono_metadata_token_from_dor (token);
1025 * mono_metadata_parse_custom_mod:
1026 * @m: a metadata context.
1027 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1028 * @ptr: a pointer to (possibly) the start of a custom modifier list
1029 * @rptr: pointer updated to match the end of the decoded stream
1031 * Checks if @ptr points to a type custom modifier compressed representation.
1033 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1036 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1038 MonoCustomMod local;
1039 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
1040 (*ptr == MONO_TYPE_CMOD_REQD)) {
1043 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1044 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1051 * mono_metadata_parse_array:
1052 * @m: a metadata context.
1053 * @ptr: a pointer to an encoded array description.
1054 * @rptr: pointer updated to match the end of the decoded stream
1056 * Decodes the compressed array description found in the metadata @m at @ptr.
1058 * Returns: a #MonoArrayType structure describing the array type
1062 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContainer *generic_container,
1063 const char *ptr, const char **rptr)
1066 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1069 etype = mono_metadata_parse_type_full (m, generic_container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1070 array->eklass = mono_class_from_mono_type (etype);
1071 array->rank = mono_metadata_decode_value (ptr, &ptr);
1073 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1074 if (array->numsizes)
1075 array->sizes = g_new0 (int, array->numsizes);
1076 for (i = 0; i < array->numsizes; ++i)
1077 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1079 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1080 if (array->numlobounds)
1081 array->lobounds = g_new0 (int, array->numlobounds);
1082 for (i = 0; i < array->numlobounds; ++i)
1083 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1091 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1093 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1097 * mono_metadata_free_array:
1098 * @array: array description
1100 * Frees the array description returned from mono_metadata_parse_array().
1103 mono_metadata_free_array (MonoArrayType *array)
1105 g_free (array->sizes);
1106 g_free (array->lobounds);
1111 * need to add common field and param attributes combinations:
1114 * public static literal
1117 * private static literal
1121 /* data, attrs, type, nmods, byref, pinned */
1122 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1123 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1124 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1125 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1126 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1127 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1128 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1129 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1130 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1131 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1132 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1133 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1134 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1135 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1136 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1137 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1138 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1139 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1140 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1141 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1142 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1143 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1144 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1145 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1146 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1147 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1148 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1149 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1150 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1151 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1152 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1153 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1154 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1155 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1158 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1160 static GHashTable *type_cache = NULL;
1161 static GHashTable *generic_inst_cache = NULL;
1162 static GHashTable *generic_class_cache = NULL;
1163 static int next_generic_inst_id = 0;
1166 * MonoTypes with modifies are never cached, so we never check or use that field.
1169 mono_type_hash (gconstpointer data)
1171 const MonoType *type = (const MonoType *) data;
1172 return type->type | (type->byref << 8) | (type->attrs << 9);
1176 mono_type_equal (gconstpointer ka, gconstpointer kb)
1178 const MonoType *a = (const MonoType *) ka;
1179 const MonoType *b = (const MonoType *) kb;
1181 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1183 /* need other checks */
1188 mono_generic_inst_hash (gconstpointer data)
1190 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1191 return ginst->type_argc | (ginst->is_open << 8);
1195 mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1197 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1198 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1201 if ((a->is_open != b->is_open) || (a->type_argc != b->type_argc))
1203 for (i = 0; i < a->type_argc; ++i) {
1204 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], FALSE))
1211 mono_generic_class_hash (gconstpointer data)
1213 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1214 return mono_metadata_type_hash (gclass->generic_type);
1218 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1220 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1221 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1223 return _mono_metadata_generic_class_equal (a, b, FALSE);
1227 * mono_metadata_init:
1229 * Initialize the global variables of this module.
1232 mono_metadata_init (void)
1236 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1237 generic_inst_cache = g_hash_table_new (mono_generic_inst_hash, mono_generic_inst_equal);
1238 generic_class_cache = g_hash_table_new (mono_generic_class_hash, mono_generic_class_equal);
1240 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1241 g_hash_table_insert (type_cache, &builtin_types [i], &builtin_types [i]);
1245 * mono_metadata_parse_type:
1246 * @m: metadata context
1247 * @mode: king of type that may be found at @ptr
1248 * @opt_attrs: optional attributes to store in the returned type
1249 * @ptr: pointer to the type representation
1250 * @rptr: pointer updated to match the end of the decoded stream
1252 * Decode a compressed type description found at @ptr in @m.
1253 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1254 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1255 * This function can be used to decode type descriptions in method signatures,
1256 * field signatures, locals signatures etc.
1258 * To parse a generic type, `generic_container' points to the current class'es
1259 * (the `generic_container' field in the MonoClass) or the current generic method's
1260 * (the `generic_container' field in the MonoMethodNormal) generic container.
1261 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1262 * this MonoGenericContainer.
1264 * Returns: a #MonoType structure representing the decoded type.
1267 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *generic_container, MonoParseTypeMode mode,
1268 short opt_attrs, const char *ptr, const char **rptr)
1270 MonoType *type, *cached;
1272 gboolean byref = FALSE;
1273 gboolean pinned = FALSE;
1274 const char *tmp_ptr;
1279 * According to the spec, custom modifiers should come before the byref
1280 * flag, but the IL produced by ilasm from the following signature:
1281 * object modopt(...) &
1282 * starts with a byref flag, followed by the modifiers. (bug #49802)
1283 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1284 * it would be better to treat byref as real type constructor instead of
1286 * Also, pinned should come before anything else, but some MSV++ produced
1287 * assemblies violate this (#bug 61990).
1290 /* Count the modifiers first */
1295 case MONO_TYPE_PINNED:
1296 case MONO_TYPE_BYREF:
1299 case MONO_TYPE_CMOD_REQD:
1300 case MONO_TYPE_CMOD_OPT:
1302 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1310 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1311 type->num_mods = count;
1313 g_warning ("got more than 64 modifiers in type");
1317 memset (type, 0, sizeof (MonoType));
1320 /* Parse pinned, byref and custom modifiers */
1325 case MONO_TYPE_PINNED:
1329 case MONO_TYPE_BYREF:
1333 case MONO_TYPE_CMOD_REQD:
1334 case MONO_TYPE_CMOD_OPT:
1335 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1343 type->attrs = opt_attrs;
1344 type->byref = byref;
1345 type->pinned = pinned ? 1 : 0;
1347 do_mono_metadata_parse_type (type, m, generic_container, ptr, &ptr);
1353 /* FIXME: remove the != MONO_PARSE_PARAM condition, this accounts for
1354 * almost 10k (about 2/3rds) of all MonoType's we create.
1356 if (mode != MONO_PARSE_PARAM && !type->num_mods) {
1357 /* no need to free type here, because it is on the stack */
1358 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1360 return &type->data.klass->this_arg;
1362 return &type->data.klass->byval_arg;
1364 /* No need to use locking since nobody is modifying the hash table */
1365 if ((cached = g_hash_table_lookup (type_cache, type)))
1369 /*printf ("%x%c %s\n", type->attrs, type->pinned ? 'p' : ' ', mono_type_full_name (type));*/
1372 type = g_memdup (&stype, sizeof (MonoType));
1377 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1378 const char *ptr, const char **rptr)
1380 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1384 * mono_metadata_parse_signature:
1385 * @image: metadata context
1386 * @toke: metadata token
1388 * Decode a method signature stored in the STANDALONESIG table
1390 * Returns: a MonoMethodSignature describing the signature.
1392 MonoMethodSignature *
1393 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContainer *generic_container, guint32 token)
1395 MonoTableInfo *tables = image->tables;
1396 guint32 idx = mono_metadata_token_index (token);
1401 return mono_lookup_dynamic_token (image, token);
1403 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1405 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1407 ptr = mono_metadata_blob_heap (image, sig);
1408 mono_metadata_decode_blob_size (ptr, &ptr);
1410 return mono_metadata_parse_method_signature_full (image, generic_container, FALSE, ptr, NULL);
1413 MonoMethodSignature *
1414 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1416 return mono_metadata_parse_signature_full (image, NULL, token);
1419 MonoMethodSignature*
1420 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1422 MonoMethodSignature *sig;
1424 /* later we want to allocate signatures with mempools */
1425 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1426 sig->param_count = nparams;
1427 sig->sentinelpos = -1;
1432 MonoMethodSignature*
1433 mono_metadata_signature_dup (MonoMethodSignature *sig)
1437 sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
1438 return g_memdup (sig, sigsize);
1442 * mono_metadata_parse_method_signature:
1443 * @m: metadata context
1444 * @def: the MethodDef index or 0 for Ref signatures.
1445 * @ptr: pointer to the signature metadata representation
1446 * @rptr: pointer updated to match the end of the decoded stream
1448 * Decode a method signature stored at @ptr.
1450 * Returns: a MonoMethodSignature describing the signature.
1452 MonoMethodSignature *
1453 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *generic_container,
1454 int def, const char *ptr, const char **rptr)
1456 MonoMethodSignature *method;
1457 int i, ret_attrs = 0, *pattrs = NULL;
1458 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1459 guint32 gen_param_count = 0;
1460 gboolean is_open = FALSE;
1461 MonoGenericContainer *container = NULL;
1464 gen_param_count = 1;
1469 call_convention = *ptr & 0x0F;
1471 if (gen_param_count)
1472 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1473 param_count = mono_metadata_decode_value (ptr, &ptr);
1474 pattrs = g_new0 (int, param_count);
1477 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1478 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1479 guint32 cols [MONO_PARAM_SIZE];
1480 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1482 if (def < methodt->rows)
1483 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1485 lastp = paramt->rows + 1;
1486 for (i = param_index; i < lastp; ++i) {
1487 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1488 if (!cols [MONO_PARAM_SEQUENCE])
1489 ret_attrs = cols [MONO_PARAM_FLAGS];
1491 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1494 method = mono_metadata_signature_alloc (m, param_count);
1495 method->hasthis = hasthis;
1496 method->explicit_this = explicit_this;
1497 method->call_convention = call_convention;
1498 method->generic_param_count = gen_param_count;
1500 if (gen_param_count)
1501 method->has_type_parameters = 1;
1503 if (gen_param_count && (!generic_container || !generic_container->is_method)) {
1504 container = g_new0 (MonoGenericContainer, 1);
1505 container->parent = generic_container;
1506 container->is_signature = 1;
1508 container->context.container = container;
1510 container->type_argc = gen_param_count;
1511 container->type_params = g_new0 (MonoGenericParam, gen_param_count);
1513 for (i = 0; i < gen_param_count; i++) {
1514 container->type_params [i].owner = container;
1515 container->type_params [i].num = i;
1518 container = generic_container;
1520 if (call_convention != 0xa) {
1521 method->ret = mono_metadata_parse_type_full (m, container, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1522 is_open = mono_class_is_open_constructed_type (method->ret);
1525 if (method->param_count) {
1526 method->sentinelpos = -1;
1528 for (i = 0; i < method->param_count; ++i) {
1529 if (*ptr == MONO_TYPE_SENTINEL) {
1530 if (method->call_convention != MONO_CALL_VARARG || def)
1531 g_error ("found sentinel for methoddef or no vararg method");
1532 method->sentinelpos = i;
1535 method->params [i] = mono_metadata_parse_type_full (
1536 m, container, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1538 is_open = mono_class_is_open_constructed_type (method->params [i]);
1542 method->has_type_parameters = is_open;
1544 if (def && (method->call_convention == MONO_CALL_VARARG))
1545 method->sentinelpos = method->param_count;
1552 * Add signature to a cache and increase ref count...
1557 MonoMethodSignature *
1558 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1560 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1564 * mono_metadata_free_method_signature:
1565 * @sig: signature to destroy
1567 * Free the memory allocated in the signature @sig.
1570 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1573 mono_metadata_free_type (sig->ret);
1574 for (i = 0; i < sig->param_count; ++i)
1575 mono_metadata_free_type (sig->params [i]);
1581 mono_metadata_lookup_generic_inst (MonoGenericInst *ginst)
1583 MonoGenericInst *cached;
1586 cached = g_hash_table_lookup (generic_inst_cache, ginst);
1588 for (i = 0; i < ginst->type_argc; i++)
1589 mono_metadata_free_type (ginst->type_argv [i]);
1590 g_free (ginst->type_argv);
1595 ginst->id = ++next_generic_inst_id;
1596 g_hash_table_insert (generic_inst_cache, ginst, ginst);
1602 mono_metadata_lookup_generic_class (MonoGenericClass *gclass)
1604 MonoGenericClass *cached;
1607 cached = g_hash_table_lookup (generic_class_cache, gclass);
1611 g_hash_table_insert (generic_class_cache, gclass, gclass);
1616 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
1618 MonoGenericInst *nginst, *cached;
1621 nginst = g_new0 (MonoGenericInst, 1);
1622 nginst->type_argc = ginst->type_argc;
1623 nginst->type_argv = g_new0 (MonoType*, nginst->type_argc);
1625 for (i = 0; i < nginst->type_argc; i++) {
1626 MonoType *t = ginst->type_argv [i];
1628 nginst->type_argv [i] = mono_class_inflate_generic_type (t, context);
1630 if (!nginst->is_open)
1631 nginst->is_open = mono_class_is_open_constructed_type (nginst->type_argv [i]);
1634 return mono_metadata_lookup_generic_inst (nginst);
1638 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *generic_container,
1639 int count, const char *ptr, const char **rptr)
1641 MonoGenericInst *ginst;
1644 ginst = g_new0 (MonoGenericInst, 1);
1645 ginst->type_argc = count;
1646 ginst->type_argv = g_new0 (MonoType*, count);
1648 for (i = 0; i < ginst->type_argc; i++) {
1649 MonoType *t = mono_metadata_parse_type_full (m, generic_container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1651 ginst->type_argv [i] = t;
1652 if (!ginst->is_open)
1653 ginst->is_open = mono_class_is_open_constructed_type (t);
1659 return mono_metadata_lookup_generic_inst (ginst);
1663 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *generic_container,
1664 const char *ptr, const char **rptr)
1666 MonoGenericClass *gclass = g_new0 (MonoGenericClass, 1);
1667 MonoGenericClass *cached;
1668 MonoGenericInst *ginst;
1672 type->data.generic_class = gclass;
1674 gclass->context = g_new0 (MonoGenericContext, 1);
1675 gclass->context->gclass = gclass;
1677 gclass->klass = g_new0 (MonoClass, 1);
1679 gclass->generic_type = mono_metadata_parse_type_full (m, generic_container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1681 gklass = mono_class_from_mono_type (gclass->generic_type);
1682 g_assert ((gclass->context->container = gclass->container = gklass->generic_container) != NULL);
1684 count = mono_metadata_decode_value (ptr, &ptr);
1687 * Create the klass before parsing the type arguments.
1688 * This is required to support "recursive" definitions.
1689 * See mcs/tests/gen-23.cs for an example.
1692 gclass->init_pending = TRUE;
1694 mono_class_create_generic (gclass);
1696 gclass->inst = mono_metadata_parse_generic_inst (m, generic_container, count, ptr, &ptr);
1698 mono_class_create_generic_2 (gclass);
1704 * We may be called multiple times on different metadata to create the same
1705 * instantiated type. This happens for instance if we're part of a method or
1706 * local variable signature.
1708 * It's important to return the same MonoGenericClass * for each particualar
1709 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1712 * According to the spec ($26.1.5), a static variable in a generic class
1713 * declaration is shared amongst all instances of the same closed constructed
1717 cached = g_hash_table_lookup (generic_class_cache, gclass);
1719 g_free (gclass->klass);
1722 type->data.generic_class = cached;
1725 g_hash_table_insert (generic_class_cache, gclass, gclass);
1727 mono_stats.generic_instance_count++;
1728 mono_stats.generics_metadata_size += sizeof (MonoGenericClass) +
1729 sizeof (MonoGenericContext) +
1730 gclass->inst->type_argc * sizeof (MonoType);
1733 gclass->init_pending = FALSE;
1737 * do_mono_metadata_parse_generic_param:
1738 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
1739 * see mono_metadata_parse_type_full() for details.
1740 * Internal routine to parse a generic type parameter.
1742 static MonoGenericParam *
1743 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
1744 gboolean is_mvar, const char *ptr, const char **rptr)
1748 index = mono_metadata_decode_value (ptr, &ptr);
1753 g_assert (generic_container);
1754 if (generic_container->parent) {
1756 * The current MonoGenericContainer is a generic method -> its `parent'
1757 * points to the containing class'es container.
1759 generic_container = generic_container->parent;
1761 g_assert (generic_container && !generic_container->is_method);
1762 g_assert (index < generic_container->type_argc);
1764 return &generic_container->type_params [index];
1766 g_assert (generic_container && (generic_container->is_method || generic_container->is_signature));
1767 g_assert (index < generic_container->type_argc);
1769 return &generic_container->type_params [index];
1774 * do_mono_metadata_parse_type:
1775 * @type: MonoType to be filled in with the return value
1777 * Internal routine used to "fill" the contents of @type from an
1778 * allocated pointer. This is done this way to avoid doing too
1779 * many mini-allocations (particularly for the MonoFieldType which
1780 * most of the time is just a MonoType, but sometimes might be augmented).
1782 * This routine is used by mono_metadata_parse_type and
1783 * mono_metadata_parse_field_type
1785 * This extracts a Type as specified in Partition II (22.2.12)
1788 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *generic_container,
1789 const char *ptr, const char **rptr)
1791 type->type = mono_metadata_decode_value (ptr, &ptr);
1793 switch (type->type){
1794 case MONO_TYPE_VOID:
1795 case MONO_TYPE_BOOLEAN:
1796 case MONO_TYPE_CHAR:
1809 case MONO_TYPE_STRING:
1810 case MONO_TYPE_OBJECT:
1811 case MONO_TYPE_TYPEDBYREF:
1813 case MONO_TYPE_VALUETYPE:
1814 case MONO_TYPE_CLASS: {
1816 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1817 type->data.klass = mono_class_get (m, token);
1820 case MONO_TYPE_SZARRAY: {
1821 MonoType *etype = mono_metadata_parse_type_full (m, generic_container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1822 type->data.klass = mono_class_from_mono_type (etype);
1823 mono_metadata_free_type (etype);
1827 type->data.type = mono_metadata_parse_type_full (m, generic_container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1829 case MONO_TYPE_FNPTR:
1830 type->data.method = mono_metadata_parse_method_signature_full (m, generic_container, 0, ptr, &ptr);
1832 case MONO_TYPE_ARRAY:
1833 type->data.array = mono_metadata_parse_array_full (m, generic_container, ptr, &ptr);
1836 case MONO_TYPE_MVAR:
1837 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_container, TRUE, ptr, &ptr);
1841 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_container, FALSE, ptr, &ptr);
1844 case MONO_TYPE_GENERICINST:
1845 do_mono_metadata_parse_generic_class (type, m, generic_container, ptr, &ptr);
1849 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1857 * mono_metadata_free_type:
1858 * @type: type to free
1860 * Free the memory allocated for type @type.
1863 mono_metadata_free_type (MonoType *type)
1865 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1868 switch (type->type){
1869 case MONO_TYPE_OBJECT:
1870 case MONO_TYPE_STRING:
1871 if (!type->data.klass)
1874 case MONO_TYPE_CLASS:
1875 case MONO_TYPE_VALUETYPE:
1876 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
1880 mono_metadata_free_type (type->data.type);
1882 case MONO_TYPE_FNPTR:
1883 mono_metadata_free_method_signature (type->data.method);
1885 case MONO_TYPE_ARRAY:
1886 mono_metadata_free_array (type->data.array);
1894 hex_dump (const char *buffer, int base, int count)
1896 int show_header = 1;
1904 for (i = 0; i < count; i++){
1907 printf ("\n0x%08x: ", (unsigned char) base + i);
1909 printf ("%02x ", (unsigned char) (buffer [i]));
1916 * @mh: The Method header
1917 * @ptr: Points to the beginning of the Section Data (25.3)
1920 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
1922 unsigned char sect_data_flags;
1923 const unsigned char *sptr;
1925 guint32 sect_data_len;
1928 /* align on 32-bit boundary */
1929 /* FIXME: not 64-bit clean code */
1930 sptr = ptr = dword_align (ptr);
1931 sect_data_flags = *ptr;
1934 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1936 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1939 sect_data_len = ptr [0];
1943 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1944 hex_dump (sptr, 0, sect_data_len+8);
1945 g_print ("\nheader: ");
1946 hex_dump (sptr-4, 0, 4);
1950 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1951 const unsigned char *p = dword_align (ptr);
1953 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1954 /* we could just store a pointer if we don't need to byteswap */
1955 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1956 for (i = 0; i < mh->num_clauses; ++i) {
1957 MonoExceptionClause *ec = &mh->clauses [i];
1960 ec->flags = read32 (p);
1962 ec->try_offset = read32 (p);
1964 ec->try_len = read32 (p);
1966 ec->handler_offset = read32 (p);
1968 ec->handler_len = read32 (p);
1970 tof_value = read32 (p);
1973 ec->flags = read16 (p);
1975 ec->try_offset = read16 (p);
1979 ec->handler_offset = read16 (p);
1981 ec->handler_len = *p;
1983 tof_value = read32 (p);
1986 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1987 ec->data.filter_offset = tof_value;
1988 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
1989 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
1991 ec->data.catch_class = NULL;
1993 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
1997 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
1998 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
2005 * mono_metadata_parse_mh:
2006 * @m: metadata context
2007 * @ptr: pointer to the method header.
2009 * Decode the method header at @ptr, including pointer to the IL code,
2010 * info about local variables and optional exception tables.
2012 * Returns: a MonoMethodHeader.
2015 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *generic_container, const char *ptr)
2017 MonoMethodHeader *mh;
2018 unsigned char flags = *(const unsigned char *) ptr;
2019 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2021 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2022 const unsigned char *code;
2025 g_return_val_if_fail (ptr != NULL, NULL);
2028 case METHOD_HEADER_TINY_FORMAT:
2029 mh = g_new0 (MonoMethodHeader, 1);
2032 local_var_sig_tok = 0;
2033 mh->code_size = flags >> 2;
2036 case METHOD_HEADER_TINY_FORMAT1:
2037 mh = g_new0 (MonoMethodHeader, 1);
2040 local_var_sig_tok = 0;
2043 * The spec claims 3 bits, but the Beta2 is
2046 mh->code_size = flags >> 2;
2049 case METHOD_HEADER_FAT_FORMAT:
2050 fat_flags = read16 (ptr);
2052 hsize = (fat_flags >> 12) & 0xf;
2053 max_stack = read16 (ptr);
2055 code_size = read32 (ptr);
2057 local_var_sig_tok = read32 (ptr);
2060 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2067 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2071 * There are more sections
2073 ptr = code + code_size;
2081 if (local_var_sig_tok) {
2082 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2083 const char *locals_ptr;
2084 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2085 int len=0, i, bsize;
2087 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2088 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2089 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2090 if (*locals_ptr != 0x07)
2091 g_warning ("wrong signature for locals blob");
2093 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2094 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
2095 mh->num_locals = len;
2096 for (i = 0; i < len; ++i)
2097 mh->locals [i] = mono_metadata_parse_type_full (
2098 m, generic_container, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
2100 mh = g_new0 (MonoMethodHeader, 1);
2103 mh->code_size = code_size;
2104 mh->max_stack = max_stack;
2105 mh->init_locals = init_locals;
2106 if (fat_flags & METHOD_HEADER_MORE_SECTS)
2107 parse_section_data (m, mh, (const unsigned char*)ptr);
2112 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
2114 return mono_metadata_parse_mh_full (m, NULL, ptr);
2118 * mono_metadata_free_mh:
2119 * @mh: a method header
2121 * Free the memory allocated for the method header.
2124 mono_metadata_free_mh (MonoMethodHeader *mh)
2127 for (i = 0; i < mh->num_locals; ++i)
2128 mono_metadata_free_type (mh->locals[i]);
2129 g_free (mh->clauses);
2134 * mono_metadata_parse_field_type:
2135 * @m: metadata context to extract information from
2136 * @ptr: pointer to the field signature
2137 * @rptr: pointer updated to match the end of the decoded stream
2139 * Parses the field signature, and returns the type information for it.
2141 * Returns: The MonoType that was extracted from @ptr.
2144 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
2146 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
2150 * mono_metadata_parse_param:
2151 * @m: metadata context to extract information from
2152 * @ptr: pointer to the param signature
2153 * @rptr: pointer updated to match the end of the decoded stream
2155 * Parses the param signature, and returns the type information for it.
2157 * Returns: The MonoType that was extracted from @ptr.
2160 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
2162 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
2166 * mono_metadata_token_from_dor:
2167 * @dor_token: A TypeDefOrRef coded index
2169 * dor_token is a TypeDefOrRef coded index: it contains either
2170 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
2171 * bits contain an index into the table.
2173 * Returns: an expanded token
2176 mono_metadata_token_from_dor (guint32 dor_index)
2180 table = dor_index & 0x03;
2181 idx = dor_index >> 2;
2184 case 0: /* TypeDef */
2185 return MONO_TOKEN_TYPE_DEF | idx;
2187 case 1: /* TypeRef */
2188 return MONO_TOKEN_TYPE_REF | idx;
2190 case 2: /* TypeSpec */
2191 return MONO_TOKEN_TYPE_SPEC | idx;
2194 g_assert_not_reached ();
2201 * We use this to pass context information to the row locator
2204 int idx; /* The index that we are trying to locate */
2205 int col_idx; /* The index in the row where idx may be stored */
2206 MonoTableInfo *t; /* pointer to the table */
2211 * How the row locator works.
2216 * ___|___------> _______
2219 * A column in the rows of table A references an index in table B.
2220 * For example A may be the TYPEDEF table and B the METHODDEF table.
2222 * Given an index in table B we want to get the row in table A
2223 * where the column n references our index in B.
2225 * In the locator_t structure:
2227 * col_idx is the column number
2228 * index is the index in table B
2229 * result will be the index in table A
2232 * Table A Table B column (in table A)
2233 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
2234 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
2235 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
2236 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
2237 * METHODSEM PROPERTY ASSOCIATION (encoded index)
2239 * Note that we still don't support encoded indexes.
2243 typedef_locator (const void *a, const void *b)
2245 locator_t *loc = (locator_t *) a;
2246 const char *bb = (const char *) b;
2247 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
2248 guint32 col, col_next;
2250 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
2256 * Need to check that the next row is valid.
2258 if (typedef_index + 1 < loc->t->rows) {
2259 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
2260 if (loc->idx >= col_next)
2263 if (col == col_next)
2267 loc->result = typedef_index;
2273 table_locator (const void *a, const void *b)
2275 locator_t *loc = (locator_t *) a;
2276 const char *bb = (const char *) b;
2277 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2280 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2282 if (loc->idx == col) {
2283 loc->result = table_index;
2293 declsec_locator (const void *a, const void *b)
2295 locator_t *loc = (locator_t *) a;
2296 const char *bb = (const char *) b;
2297 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2300 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2302 if (loc->idx == col) {
2303 loc->result = table_index;
2313 * mono_metadata_typedef_from_field:
2314 * @meta: metadata context
2315 * @index: FieldDef token
2317 * Returns: the 1-based index into the TypeDef table of the type that
2318 * declared the field described by @index, or 0 if not found.
2321 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2323 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2329 loc.idx = mono_metadata_token_index (index);
2330 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2333 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2334 g_assert_not_reached ();
2336 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2337 return loc.result + 1;
2341 * mono_metadata_typedef_from_method:
2342 * @meta: metadata context
2343 * @index: MethodDef token
2345 * Returns: the 1-based index into the TypeDef table of the type that
2346 * declared the method described by @index. 0 if not found.
2349 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2351 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2357 loc.idx = mono_metadata_token_index (index);
2358 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2361 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2362 g_assert_not_reached ();
2364 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2365 return loc.result + 1;
2369 * mono_metadata_interfaces_from_typedef:
2370 * @meta: metadata context
2371 * @index: typedef token
2373 * Returns: and array of interfaces that the @index typedef token implements.
2374 * The number of elemnts in the array is returned in @count.
2377 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, guint *count, MonoGenericContext *context)
2379 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2382 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2390 loc.idx = mono_metadata_token_index (index);
2391 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2394 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2399 * We may end up in the middle of the rows...
2402 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2409 while (start < tdef->rows) {
2410 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2411 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2413 result = g_renew (MonoClass*, result, i + 1);
2414 result [i] = mono_class_get_full (
2415 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
2423 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2425 return mono_metadata_interfaces_from_typedef_full (meta, index, count, NULL);
2429 * mono_metadata_nested_in_typedef:
2430 * @meta: metadata context
2431 * @index: typedef token
2433 * Returns: the 1-based index into the TypeDef table of the type
2434 * where the type described by @index is nested.
2435 * Retruns 0 if @index describes a non-nested type.
2438 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2440 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2446 loc.idx = mono_metadata_token_index (index);
2447 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2450 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2453 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2454 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2458 * mono_metadata_nesting_typedef:
2459 * @meta: metadata context
2460 * @index: typedef token
2462 * Returns: the 1-based index into the TypeDef table of the first type
2463 * that is nested inside the type described by @index. The search starts at
2464 * @start_index. returns 0 if no such type is found.
2467 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2469 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2471 guint32 class_index = mono_metadata_token_index (index);
2476 start = start_index;
2478 while (start <= tdef->rows) {
2479 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2485 if (start > tdef->rows)
2492 * mono_metadata_packing_from_typedef:
2493 * @meta: metadata context
2494 * @index: token representing a type
2496 * Returns: the info stored in the ClassLAyout table for the given typedef token
2497 * into the @packing and @size pointers.
2498 * Returns 0 if the info is not found.
2501 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2503 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2505 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2510 loc.idx = mono_metadata_token_index (index);
2511 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2514 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2517 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2519 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2521 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2523 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2524 return loc.result + 1;
2528 * mono_metadata_custom_attrs_from_index:
2529 * @meta: metadata context
2530 * @index: token representing the parent
2532 * Returns: the 1-based index into the CustomAttribute table of the first
2533 * attribute which belongs to the metadata object described by @index.
2534 * Returns 0 if no such attribute is found.
2537 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2539 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2546 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2549 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2552 /* Find the first entry by searching backwards */
2553 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2556 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2557 return loc.result + 1;
2561 * mono_metadata_declsec_from_index:
2562 * @meta: metadata context
2563 * @index: token representing the parent
2565 * Returns: the 0-based index into the DeclarativeSecurity table of the first
2566 * attribute which belongs to the metadata object described by @index.
2567 * Returns -1 if no such attribute is found.
2570 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
2572 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
2579 loc.col_idx = MONO_DECL_SECURITY_PARENT;
2582 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
2585 /* Find the first entry by searching backwards */
2586 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
2594 mono_backtrace (int limit)
2599 backtrace (array, limit);
2600 names = backtrace_symbols (array, limit);
2601 for (i =0; i < limit; ++i) {
2602 g_print ("\t%s\n", names [i]);
2609 /*#define __alignof__(a) sizeof(a)*/
2610 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2615 * @t: the type to return the size of
2617 * Returns: the number of bytes required to hold an instance of this
2621 mono_type_size (MonoType *t, gint *align)
2628 *align = __alignof__(gpointer);
2629 return sizeof (gpointer);
2633 case MONO_TYPE_VOID:
2636 case MONO_TYPE_BOOLEAN:
2637 *align = __alignof__(gint8);
2641 *align = __alignof__(gint8);
2643 case MONO_TYPE_CHAR:
2646 *align = __alignof__(gint16);
2650 *align = __alignof__(gint32);
2653 *align = __alignof__(float);
2657 *align = __alignof__(gint64);
2660 *align = __alignof__(double);
2664 *align = __alignof__(gpointer);
2665 return sizeof (gpointer);
2666 case MONO_TYPE_STRING:
2667 *align = __alignof__(gpointer);
2668 return sizeof (gpointer);
2669 case MONO_TYPE_OBJECT:
2670 *align = __alignof__(gpointer);
2671 return sizeof (gpointer);
2672 case MONO_TYPE_VALUETYPE: {
2673 if (t->data.klass->enumtype)
2674 return mono_type_size (t->data.klass->enum_basetype, align);
2676 return mono_class_value_size (t->data.klass, align);
2678 case MONO_TYPE_CLASS:
2679 case MONO_TYPE_SZARRAY:
2681 case MONO_TYPE_FNPTR:
2682 case MONO_TYPE_ARRAY:
2683 *align = __alignof__(gpointer);
2684 return sizeof (gpointer);
2685 case MONO_TYPE_TYPEDBYREF:
2686 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2687 case MONO_TYPE_GENERICINST: {
2688 MonoGenericClass *gclass = t->data.generic_class;
2690 g_assert (!gclass->inst->is_open && !gclass->klass->generic_container);
2692 if (MONO_TYPE_ISSTRUCT (gclass->generic_type)) {
2693 MonoClass *gklass = mono_class_from_mono_type (gclass->generic_type);
2695 if (gklass->enumtype)
2696 return mono_type_size (gklass->enum_basetype, align);
2698 return mono_class_value_size (gclass->klass, align);
2700 *align = __alignof__(gpointer);
2701 return sizeof (gpointer);
2705 case MONO_TYPE_MVAR:
2706 *align = __alignof__(gpointer);
2707 return sizeof (gpointer);
2709 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2715 * mono_type_stack_size:
2716 * @t: the type to return the size it uses on the stack
2718 * Returns: the number of bytes required to hold an instance of this
2719 * type on the runtime stack
2722 mono_type_stack_size (MonoType *t, gint *align)
2726 g_assert (t != NULL);
2732 *align = __alignof__(gpointer);
2733 return sizeof (gpointer);
2737 case MONO_TYPE_BOOLEAN:
2738 case MONO_TYPE_CHAR:
2747 case MONO_TYPE_STRING:
2748 case MONO_TYPE_OBJECT:
2749 case MONO_TYPE_CLASS:
2750 case MONO_TYPE_SZARRAY:
2752 case MONO_TYPE_FNPTR:
2753 case MONO_TYPE_ARRAY:
2754 *align = __alignof__(gpointer);
2755 return sizeof (gpointer);
2756 case MONO_TYPE_TYPEDBYREF:
2757 *align = __alignof__(gpointer);
2758 return sizeof (gpointer) * 3;
2760 *align = __alignof__(float);
2761 return sizeof (float);
2764 *align = __alignof__(gint64);
2765 return sizeof (gint64);
2767 *align = __alignof__(double);
2768 return sizeof (double);
2769 case MONO_TYPE_VALUETYPE: {
2772 if (t->data.klass->enumtype)
2773 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2775 size = mono_class_value_size (t->data.klass, align);
2777 *align = *align + __alignof__(gpointer) - 1;
2778 *align &= ~(__alignof__(gpointer) - 1);
2780 size += sizeof (gpointer) - 1;
2781 size &= ~(sizeof (gpointer) - 1);
2786 case MONO_TYPE_GENERICINST: {
2787 MonoGenericClass *gclass = t->data.generic_class;
2789 g_assert (!gclass->inst->is_open && !gclass->klass->generic_container);
2791 if (MONO_TYPE_ISSTRUCT (gclass->generic_type)) {
2792 MonoClass *gklass = mono_class_from_mono_type (gclass->generic_type);
2794 if (gklass->enumtype)
2795 return mono_type_stack_size (gklass->enum_basetype, align);
2797 guint32 size = mono_class_value_size (gclass->klass, align);
2799 *align = *align + __alignof__(gpointer) - 1;
2800 *align &= ~(__alignof__(gpointer) - 1);
2802 size += sizeof (gpointer) - 1;
2803 size &= ~(sizeof (gpointer) - 1);
2808 *align = __alignof__(gpointer);
2809 return sizeof (gpointer);
2813 g_error ("type 0x%02x unknown", t->type);
2819 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
2821 return MONO_TYPE_ISSTRUCT (gclass->generic_type);
2825 _mono_metadata_generic_class_equal (MonoGenericClass *g1, MonoGenericClass *g2, gboolean signature_only)
2829 if (g1->inst->type_argc != g2->inst->type_argc)
2831 if (!do_mono_metadata_type_equal (g1->generic_type, g2->generic_type, signature_only))
2833 for (i = 0; i < g1->inst->type_argc; ++i) {
2834 if (!do_mono_metadata_type_equal (g1->inst->type_argv [i], g2->inst->type_argv [i], signature_only))
2841 mono_metadata_generic_method_hash (MonoGenericMethod *gmethod)
2843 return gmethod->inst->id;
2847 mono_metadata_generic_method_equal (MonoGenericMethod *g1, MonoGenericMethod *g2)
2849 return (g1->container == g2->container) && (g1->inst == g2->inst);
2854 * mono_metadata_type_hash:
2857 * Computes an hash value for @t1 to be used in GHashTable.
2860 mono_metadata_type_hash (MonoType *t1)
2862 guint hash = t1->type;
2864 hash |= t1->byref << 6; /* do not collide with t1->type values */
2866 case MONO_TYPE_VALUETYPE:
2867 case MONO_TYPE_CLASS:
2868 case MONO_TYPE_SZARRAY:
2869 /* check if the distribution is good enough */
2870 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2872 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2873 case MONO_TYPE_ARRAY:
2874 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2875 case MONO_TYPE_GENERICINST:
2876 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
2882 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
2886 if (p1->num != p2->num)
2889 g_assert (p1->owner && p2->owner);
2890 if (p1->owner == p2->owner)
2894 * If `signature_only' is true, we're comparing two (method) signatures.
2895 * In this case, the owner of two type parameters doesn't need to match.
2898 return signature_only;
2902 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
2906 if (c1->generic_class && c2->generic_class)
2907 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
2908 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
2909 return mono_metadata_generic_param_equal (
2910 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, FALSE);
2911 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
2912 return mono_metadata_generic_param_equal (
2913 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
2918 * mono_metadata_type_equal:
2922 * Determine if @t1 and @t2 represent the same type.
2923 * Returns: #TRUE if @t1 and @t2 are equal.
2926 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
2928 if (t1->type != t2->type ||
2929 t1->byref != t2->byref)
2933 case MONO_TYPE_VOID:
2934 case MONO_TYPE_BOOLEAN:
2935 case MONO_TYPE_CHAR:
2946 case MONO_TYPE_STRING:
2949 case MONO_TYPE_OBJECT:
2950 case MONO_TYPE_TYPEDBYREF:
2952 case MONO_TYPE_VALUETYPE:
2953 case MONO_TYPE_CLASS:
2954 case MONO_TYPE_SZARRAY:
2955 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
2957 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
2958 case MONO_TYPE_ARRAY:
2959 if (t1->data.array->rank != t2->data.array->rank)
2961 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
2962 case MONO_TYPE_GENERICINST:
2963 return _mono_metadata_generic_class_equal (
2964 t1->data.generic_class, t2->data.generic_class, signature_only);
2966 return mono_metadata_generic_param_equal (
2967 t1->data.generic_param, t2->data.generic_param, FALSE);
2968 case MONO_TYPE_MVAR:
2969 return mono_metadata_generic_param_equal (
2970 t1->data.generic_param, t2->data.generic_param, signature_only);
2972 g_error ("implement type compare for %0x!", t1->type);
2980 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2982 return do_mono_metadata_type_equal (t1, t2, FALSE);
2986 * mono_metadata_signature_equal:
2987 * @sig1: a signature
2988 * @sig2: another signature
2990 * Determine if @sig1 and @sig2 represent the same signature, with the
2991 * same number of arguments and the same types.
2992 * Returns: #TRUE if @sig1 and @sig2 are equal.
2995 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2999 if (sig1->hasthis != sig2->hasthis ||
3000 sig1->param_count != sig2->param_count)
3004 * We're just comparing the signatures of two methods here:
3006 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
3007 * U and V are equal here.
3009 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
3012 for (i = 0; i < sig1->param_count; i++) {
3013 MonoType *p1 = sig1->params[i];
3014 MonoType *p2 = sig2->params[i];
3016 /* if (p1->attrs != p2->attrs)
3019 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
3023 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
3029 mono_signature_hash (MonoMethodSignature *sig)
3031 guint i, res = sig->ret->type;
3033 for (i = 0; i < sig->param_count; i++)
3034 res = (res << 5) - res + sig->params[i]->type;
3040 * mono_metadata_encode_value:
3041 * @value: value to encode
3042 * @buf: buffer where to write the compressed representation
3043 * @endbuf: pointer updated to point at the end of the encoded output
3045 * Encodes the value @value in the compressed representation used
3046 * in metadata and stores the result in @buf. @buf needs to be big
3047 * enough to hold the data (4 bytes).
3050 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
3056 else if (value < 0x4000) {
3057 p [0] = 0x80 | (value >> 8);
3058 p [1] = value & 0xff;
3061 p [0] = (value >> 24) | 0xc0;
3062 p [1] = (value >> 16) & 0xff;
3063 p [2] = (value >> 8) & 0xff;
3064 p [3] = value & 0xff;
3072 * mono_metadata_field_info:
3073 * @meta: the Image the field is defined in
3074 * @index: the index in the field table representing the field
3075 * @offset: a pointer to an integer where to store the offset that
3076 * may have been specified for the field in a FieldLayout table
3077 * @rva: a pointer to the RVA of the field data in the image that
3078 * may have been defined in a FieldRVA table
3079 * @marshal_spec: a pointer to the marshal spec that may have been
3080 * defined for the field in a FieldMarshal table.
3082 * Gather info for field @index that may have been defined in the FieldLayout,
3083 * FieldRVA and FieldMarshal tables.
3084 * Either of offset, rva and marshal_spec can be NULL if you're not interested
3088 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
3089 MonoMarshalSpec **marshal_spec)
3091 MonoTableInfo *tdef;
3094 loc.idx = index + 1;
3096 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
3098 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
3101 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3102 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
3104 *offset = (guint32)-1;
3108 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
3110 loc.col_idx = MONO_FIELD_RVA_FIELD;
3113 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3115 * LAMESPEC: There is no signature, no nothing, just the raw data.
3117 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
3125 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
3126 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
3133 * mono_metadata_get_constant_index:
3134 * @meta: the Image the field is defined in
3135 * @index: the token that may have a row defined in the constants table
3136 * @hint: possible position for the row
3138 * @token must be a FieldDef, ParamDef or PropertyDef token.
3140 * Returns: the index into the Constants table or 0 if not found.
3143 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
3145 MonoTableInfo *tdef;
3147 guint32 index = mono_metadata_token_index (token);
3149 tdef = &meta->tables [MONO_TABLE_CONSTANT];
3150 index <<= MONO_HASCONSTANT_BITS;
3151 switch (mono_metadata_token_table (token)) {
3152 case MONO_TABLE_FIELD:
3153 index |= MONO_HASCONSTANT_FIEDDEF;
3155 case MONO_TABLE_PARAM:
3156 index |= MONO_HASCONSTANT_PARAM;
3158 case MONO_TABLE_PROPERTY:
3159 index |= MONO_HASCONSTANT_PROPERTY;
3162 g_warning ("Not a valid token for the constant table: 0x%08x", token);
3166 loc.col_idx = MONO_CONSTANT_PARENT;
3169 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
3172 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3173 return loc.result + 1;
3179 * mono_metadata_events_from_typedef:
3180 * @meta: metadata context
3181 * @index: 0-based index (in the TypeDef table) describing a type
3183 * Returns: the 0-based index in the Event table for the events in the
3184 * type. The last event that belongs to the type (plus 1) is stored
3185 * in the @end_idx pointer.
3188 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3192 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
3200 loc.col_idx = MONO_EVENT_MAP_PARENT;
3201 loc.idx = index + 1;
3203 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3206 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
3207 if (loc.result + 1 < tdef->rows) {
3208 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
3210 end = meta->tables [MONO_TABLE_EVENT].rows;
3218 * mono_metadata_methods_from_event:
3219 * @meta: metadata context
3220 * @index: 0-based index (in the Event table) describing a event
3222 * Returns: the 0-based index in the MethodDef table for the methods in the
3223 * event. The last method that belongs to the event (plus 1) is stored
3224 * in the @end_idx pointer.
3227 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
3231 guint32 cols [MONO_METHOD_SEMA_SIZE];
3232 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3239 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3240 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
3242 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3247 * We may end up in the middle of the rows...
3250 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3256 while (end < msemt->rows) {
3257 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3258 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3267 * mono_metadata_properties_from_typedef:
3268 * @meta: metadata context
3269 * @index: 0-based index (in the TypeDef table) describing a type
3271 * Returns: the 0-based index in the Property table for the properties in the
3272 * type. The last property that belongs to the type (plus 1) is stored
3273 * in the @end_idx pointer.
3276 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3280 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
3288 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
3289 loc.idx = index + 1;
3291 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3294 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
3295 if (loc.result + 1 < tdef->rows) {
3296 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
3298 end = meta->tables [MONO_TABLE_PROPERTY].rows;
3306 * mono_metadata_methods_from_property:
3307 * @meta: metadata context
3308 * @index: 0-based index (in the PropertyDef table) describing a property
3310 * Returns: the 0-based index in the MethodDef table for the methods in the
3311 * property. The last method that belongs to the property (plus 1) is stored
3312 * in the @end_idx pointer.
3315 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
3319 guint32 cols [MONO_METHOD_SEMA_SIZE];
3320 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3327 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3328 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
3330 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3335 * We may end up in the middle of the rows...
3338 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3344 while (end < msemt->rows) {
3345 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3346 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3355 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
3358 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
3364 loc.col_idx = MONO_IMPLMAP_MEMBER;
3365 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
3367 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3370 return loc.result + 1;
3374 * @image: context where the image is created
3375 * @type_spec: typespec token
3377 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
3381 mono_type_create_from_typespec_full (MonoImage *image, MonoGenericContainer *generic_container, guint32 type_spec)
3383 guint32 idx = mono_metadata_token_index (type_spec);
3385 guint32 cols [MONO_TYPESPEC_SIZE];
3390 mono_loader_lock ();
3392 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
3393 mono_loader_unlock ();
3397 t = &image->tables [MONO_TABLE_TYPESPEC];
3399 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
3400 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
3401 len = mono_metadata_decode_value (ptr, &ptr);
3403 type = g_new0 (MonoType, 1);
3405 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3407 if (*ptr == MONO_TYPE_BYREF) {
3412 do_mono_metadata_parse_type (type, image, generic_container, ptr, &ptr);
3414 mono_loader_unlock ();
3420 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
3422 return mono_type_create_from_typespec_full (image, NULL, type_spec);
3426 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3428 MonoMarshalSpec *res;
3430 const char *start = ptr;
3432 /* fixme: this is incomplete, but I cant find more infos in the specs */
3434 res = g_new0 (MonoMarshalSpec, 1);
3436 len = mono_metadata_decode_value (ptr, &ptr);
3437 res->native = *ptr++;
3439 if (res->native == MONO_NATIVE_LPARRAY) {
3440 if (ptr - start <= len)
3441 res->data.array_data.elem_type = *ptr++;
3442 if (ptr - start <= len)
3443 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3444 if (ptr - start <= len)
3445 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3448 if (res->native == MONO_NATIVE_BYVALTSTR) {
3449 if (ptr - start <= len)
3450 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3453 if (res->native == MONO_NATIVE_BYVALARRAY) {
3454 if (ptr - start <= len)
3455 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3458 if (res->native == MONO_NATIVE_CUSTOM) {
3459 /* skip unused type guid */
3460 len = mono_metadata_decode_value (ptr, &ptr);
3462 /* skip unused native type name */
3463 len = mono_metadata_decode_value (ptr, &ptr);
3465 /* read custom marshaler type name */
3466 len = mono_metadata_decode_value (ptr, &ptr);
3467 res->data.custom_data.custom_name = g_strndup (ptr, len);
3469 /* read cookie string */
3470 len = mono_metadata_decode_value (ptr, &ptr);
3471 res->data.custom_data.cookie = g_strndup (ptr, len);
3478 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
3480 if (spec->native == MONO_NATIVE_CUSTOM) {
3481 g_free (spec->data.custom_data.custom_name);
3482 g_free (spec->data.custom_data.cookie);
3488 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3489 gboolean unicode, MonoMarshalConv *conv)
3491 MonoMarshalConv dummy_conv;
3497 *conv = MONO_MARSHAL_CONV_NONE;
3500 return MONO_NATIVE_UINT;
3504 case MONO_TYPE_BOOLEAN:
3506 switch (mspec->native) {
3507 case MONO_NATIVE_VARIANTBOOL:
3508 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3509 return MONO_NATIVE_VARIANTBOOL;
3510 case MONO_NATIVE_BOOLEAN:
3511 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3512 return MONO_NATIVE_BOOLEAN;
3513 case MONO_NATIVE_I1:
3514 case MONO_NATIVE_U1:
3515 return mspec->native;
3517 g_error ("cant marshal bool to native type %02x", mspec->native);
3520 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3521 return MONO_NATIVE_BOOLEAN;
3522 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3523 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3524 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3525 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3526 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3527 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3528 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3529 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3530 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3531 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3532 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3533 case MONO_TYPE_STRING:
3535 switch (mspec->native) {
3536 case MONO_NATIVE_BSTR:
3537 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3538 return MONO_NATIVE_BSTR;
3539 case MONO_NATIVE_LPSTR:
3540 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3541 return MONO_NATIVE_LPSTR;
3542 case MONO_NATIVE_LPWSTR:
3543 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3544 return MONO_NATIVE_LPWSTR;
3545 case MONO_NATIVE_LPTSTR:
3546 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3547 return MONO_NATIVE_LPTSTR;
3548 case MONO_NATIVE_ANSIBSTR:
3549 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3550 return MONO_NATIVE_ANSIBSTR;
3551 case MONO_NATIVE_TBSTR:
3552 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3553 return MONO_NATIVE_TBSTR;
3554 case MONO_NATIVE_BYVALTSTR:
3556 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3558 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3559 return MONO_NATIVE_BYVALTSTR;
3561 g_error ("cant marshal string to native type %02x", mspec->native);
3564 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3565 return MONO_NATIVE_LPTSTR;
3566 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3567 case MONO_TYPE_VALUETYPE: /*FIXME*/
3568 if (type->data.klass->enumtype) {
3569 t = type->data.klass->enum_basetype->type;
3572 return MONO_NATIVE_STRUCT;
3573 case MONO_TYPE_SZARRAY:
3574 case MONO_TYPE_ARRAY:
3576 switch (mspec->native) {
3577 case MONO_NATIVE_BYVALARRAY:
3578 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3579 return MONO_NATIVE_BYVALARRAY;
3580 case MONO_NATIVE_SAFEARRAY:
3581 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3582 return MONO_NATIVE_SAFEARRAY;
3583 case MONO_NATIVE_LPARRAY:
3584 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3585 return MONO_NATIVE_LPARRAY;
3587 g_error ("cant marshal array as native type %02x", mspec->native);
3591 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3592 return MONO_NATIVE_LPARRAY;
3593 case MONO_TYPE_I: return MONO_NATIVE_INT;
3594 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3595 case MONO_TYPE_CLASS:
3596 case MONO_TYPE_OBJECT: {
3597 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3599 switch (mspec->native) {
3600 case MONO_NATIVE_STRUCT:
3601 return MONO_NATIVE_STRUCT;
3602 case MONO_NATIVE_INTERFACE:
3603 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3604 return MONO_NATIVE_INTERFACE;
3605 case MONO_NATIVE_IDISPATCH:
3606 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3607 return MONO_NATIVE_IDISPATCH;
3608 case MONO_NATIVE_IUNKNOWN:
3609 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3610 return MONO_NATIVE_IUNKNOWN;
3611 case MONO_NATIVE_FUNC:
3612 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3613 type->data.klass == mono_defaults.delegate_class ||
3614 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3615 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3616 return MONO_NATIVE_FUNC;
3622 g_error ("cant marshal object as native type %02x", mspec->native);
3625 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3626 type->data.klass == mono_defaults.delegate_class ||
3627 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3628 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3629 return MONO_NATIVE_FUNC;
3631 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3632 return MONO_NATIVE_STRUCT;
3634 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3635 case MONO_TYPE_TYPEDBYREF:
3637 g_error ("type 0x%02x not handled in marshal", t);
3639 return MONO_NATIVE_MAX;
3643 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3646 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3652 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3653 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
3655 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3658 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3662 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3664 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
3665 switch (tok & MONO_METHODDEFORREF_MASK) {
3666 case MONO_METHODDEFORREF_METHODDEF:
3667 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3668 case MONO_METHODDEFORREF_METHODREF:
3669 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3671 g_assert_not_reached ();
3676 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3679 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3682 guint32 cols [MONO_METHODIMPL_SIZE];
3683 MonoMethod **result;
3692 loc.col_idx = MONO_METHODIMPL_CLASS;
3693 loc.idx = mono_metadata_token_index (type_token);
3695 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3701 * We may end up in the middle of the rows...
3704 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3709 while (end < tdef->rows) {
3710 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3716 result = g_new (MonoMethod*, num * 2);
3717 for (i = 0; i < num; ++i) {
3718 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3719 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3720 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3724 *num_overrides = num;
3729 * mono_guid_to_string:
3731 * Converts a 16 byte Microsoft GUID to the standard string representation.
3734 mono_guid_to_string (const guint8 *guid)
3736 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3737 guid[3], guid[2], guid[1], guid[0],
3741 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3745 get_constraints (MonoImage *image, int owner, MonoGenericContext *context)
3747 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3748 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3749 guint32 i, token, found;
3750 MonoClass *klass, **res;
3751 GList *cons = NULL, *tmp;
3755 for (i = 0; i < tdef->rows; ++i) {
3756 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3757 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3758 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3759 klass = mono_class_get_full (image, token, context);
3760 cons = g_list_append (cons, klass);
3763 /* contiguous list finished */
3770 res = g_new0 (MonoClass*, found + 1);
3771 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3772 res [i] = tmp->data;
3778 MonoGenericContainer *
3779 mono_metadata_load_generic_params (MonoImage *image, guint32 token)
3781 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3782 guint32 cols [MONO_GENERICPARAM_SIZE];
3783 guint32 i, owner = 0, last_num, n;
3784 MonoGenericContainer *container;
3785 MonoGenericParam *params;
3787 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3788 owner = MONO_TYPEORMETHOD_TYPE;
3789 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3790 owner = MONO_TYPEORMETHOD_METHOD;
3792 g_error ("wrong token %x to load_generics_params", token);
3795 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3799 for (i = 0; i < tdef->rows; ++i) {
3800 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3801 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3805 if (i >= tdef->rows)
3809 container = g_new0 (MonoGenericContainer, 1);
3812 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3813 params [n - 1].owner = container;
3814 params [n - 1].pklass = NULL;
3815 params [n - 1].method = NULL;
3816 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3817 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3818 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3819 if (++i >= tdef->rows)
3821 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3822 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
3824 container->type_argc = n;
3825 container->type_params = params;
3827 container->context.container = container;
3829 for (i = 0; i < n; i++)
3830 params [i].constraints = get_constraints (image, i + 1, &container->context);
3836 mono_type_is_byref (MonoType *type)
3842 mono_type_get_type (MonoType *type)
3847 /* For MONO_TYPE_FNPTR */
3848 MonoMethodSignature*
3849 mono_type_get_signature (MonoType *type)
3851 return type->data.method;
3854 /* For MONO_TYPE_CLASS, VALUETYPE */
3856 mono_type_get_class (MonoType *type)
3858 return type->data.klass;
3861 /* For MONO_TYPE_ARRAY */
3863 mono_type_get_array_type (MonoType *type)
3865 return type->data.array;
3869 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
3871 /* FIXME: implement */
3876 mono_signature_get_return_type (MonoMethodSignature *sig)
3882 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
3888 /* start from the first */
3889 if (sig->param_count) {
3890 *iter = &sig->params [0];
3891 return sig->params [0];
3899 if (type < &sig->params [sig->param_count]) {
3907 mono_signature_get_param_count (MonoMethodSignature *sig)
3909 return sig->param_count;
3913 mono_signature_get_call_conv (MonoMethodSignature *sig)
3915 return sig->call_convention;
3919 mono_signature_vararg_start (MonoMethodSignature *sig)
3921 return sig->sentinelpos;
3925 mono_signature_is_instance (MonoMethodSignature *sig)
3927 return sig->hasthis;
3931 mono_signature_explicit_this (MonoMethodSignature *sig)
3933 return sig->explicit_this;