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.
16 #include "tabledefs.h"
17 #include "mono-endian.h"
19 #include "tokentype.h"
23 static void do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr);
26 * Encoding of the "description" argument:
28 * identifier [CODE ARG]
30 * If CODE is ':', then a lookup on table ARG is performed
31 * If CODE is '=', then a lookup in the aliased-table ARG is performed
32 * If CODE is '#', then this encodes a flag, ARG is the flag name.
34 * Aliased table for example is `CustomAttributeType' which depending on the
35 * information might refer to different tables.
38 const static MonoMetaTable AssemblySchema [] = {
39 { MONO_MT_UINT32, "HashId" },
40 { MONO_MT_UINT16, "Major" },
41 { MONO_MT_UINT16, "Minor" },
42 { MONO_MT_UINT16, "BuildNumber" },
43 { MONO_MT_UINT16, "RevisionNumber" },
44 { MONO_MT_UINT32, "Flags" },
45 { MONO_MT_BLOB_IDX, "PublicKey" },
46 { MONO_MT_STRING_IDX, "Name" },
47 { MONO_MT_STRING_IDX, "Culture" },
51 const static MonoMetaTable AssemblyOSSchema [] = {
52 { MONO_MT_UINT32, "OSPlatformID" },
53 { MONO_MT_UINT32, "OSMajor" },
54 { MONO_MT_UINT32, "OSMinor" },
58 const static MonoMetaTable AssemblyProcessorSchema [] = {
59 { MONO_MT_UINT32, "Processor" },
63 const static MonoMetaTable AssemblyRefSchema [] = {
64 { MONO_MT_UINT16, "Major" },
65 { MONO_MT_UINT16, "Minor" },
66 { MONO_MT_UINT16, "Build" },
67 { MONO_MT_UINT16, "Revision" },
68 { MONO_MT_UINT32, "Flags" },
69 { MONO_MT_BLOB_IDX, "PublicKeyOrToken" },
70 { MONO_MT_STRING_IDX, "Name" },
71 { MONO_MT_STRING_IDX, "Culture" },
72 { MONO_MT_BLOB_IDX, "HashValue" },
76 const static MonoMetaTable AssemblyRefOSSchema [] = {
77 { MONO_MT_UINT32, "OSPlatformID" },
78 { MONO_MT_UINT32, "OSMajorVersion" },
79 { MONO_MT_UINT32, "OSMinorVersion" },
80 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
84 const static MonoMetaTable AssemblyRefProcessorSchema [] = {
85 { MONO_MT_UINT32, "Processor" },
86 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
90 const static MonoMetaTable ClassLayoutSchema [] = {
91 { MONO_MT_UINT16, "PackingSize" },
92 { MONO_MT_UINT32, "ClassSize" },
93 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
97 const static MonoMetaTable ConstantSchema [] = {
98 { MONO_MT_UINT8, "Type" },
99 { MONO_MT_UINT8, "PaddingZero" },
100 { MONO_MT_CONST_IDX, "Parent" },
101 { MONO_MT_BLOB_IDX, "Value" },
102 { MONO_MT_END, NULL }
105 const static MonoMetaTable CustomAttributeSchema [] = {
106 { MONO_MT_HASCAT_IDX, "Parent" },
107 { MONO_MT_CAT_IDX, "Type" },
108 { MONO_MT_BLOB_IDX, "Value" },
109 { MONO_MT_END, NULL }
112 const static MonoMetaTable DeclSecuritySchema [] = {
113 { MONO_MT_UINT16, "Action" },
114 { MONO_MT_HASDEC_IDX, "Parent" },
115 { MONO_MT_BLOB_IDX, "PermissionSet" },
116 { MONO_MT_END, NULL }
119 const static MonoMetaTable EventMapSchema [] = {
120 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
121 { MONO_MT_TABLE_IDX, "EventList:Event" },
122 { MONO_MT_END, NULL }
125 const static MonoMetaTable EventSchema [] = {
126 { MONO_MT_UINT16, "EventFlags#EventAttribute" },
127 { MONO_MT_STRING_IDX, "Name" },
128 { MONO_MT_TABLE_IDX, "EventType" }, /* TypeDef or TypeRef */
129 { MONO_MT_END, NULL }
132 const static MonoMetaTable ExportedTypeSchema [] = {
133 { MONO_MT_UINT32, "Flags" },
134 { MONO_MT_TABLE_IDX, "TypeDefId" },
135 { MONO_MT_STRING_IDX, "TypeName" },
136 { MONO_MT_STRING_IDX, "TypeNameSpace" },
137 { MONO_MT_IMPL_IDX, "Implementation" },
138 { MONO_MT_END, NULL }
141 const static MonoMetaTable FieldSchema [] = {
142 { MONO_MT_UINT16, "Flags" },
143 { MONO_MT_STRING_IDX, "Name" },
144 { MONO_MT_BLOB_IDX, "Signature" },
145 { MONO_MT_END, NULL }
147 const static MonoMetaTable FieldLayoutSchema [] = {
148 { MONO_MT_UINT32, "Offset" },
149 { MONO_MT_TABLE_IDX, "Field:Field" },
150 { MONO_MT_END, NULL }
153 const static MonoMetaTable FieldMarshalSchema [] = {
154 { MONO_MT_HFM_IDX, "Parent" },
155 { MONO_MT_BLOB_IDX, "NativeType" },
156 { MONO_MT_END, NULL }
158 const static MonoMetaTable FieldRVASchema [] = {
159 { MONO_MT_UINT32, "RVA" },
160 { MONO_MT_TABLE_IDX, "Field:Field" },
161 { MONO_MT_END, NULL }
164 const static MonoMetaTable FileSchema [] = {
165 { MONO_MT_UINT32, "Flags" },
166 { MONO_MT_STRING_IDX, "Name" },
167 { MONO_MT_BLOB_IDX, "Value" },
168 { MONO_MT_END, NULL }
171 const static MonoMetaTable ImplMapSchema [] = {
172 { MONO_MT_UINT16, "MappingFlag" },
173 { MONO_MT_MF_IDX, "MemberForwarded" },
174 { MONO_MT_STRING_IDX, "ImportName" },
175 { MONO_MT_TABLE_IDX, "ImportScope:ModuleRef" },
176 { MONO_MT_END, NULL }
179 const static MonoMetaTable InterfaceImplSchema [] = {
180 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
181 { MONO_MT_TDOR_IDX, "Interface=TypeDefOrRef" },
182 { MONO_MT_END, NULL }
185 const static MonoMetaTable ManifestResourceSchema [] = {
186 { MONO_MT_UINT32, "Offset" },
187 { MONO_MT_UINT32, "Flags" },
188 { MONO_MT_STRING_IDX, "Name" },
189 { MONO_MT_IMPL_IDX, "Implementation" },
190 { MONO_MT_END, NULL }
193 const static MonoMetaTable MemberRefSchema [] = {
194 { MONO_MT_MRP_IDX, "Class" },
195 { MONO_MT_STRING_IDX, "Name" },
196 { MONO_MT_BLOB_IDX, "Signature" },
197 { MONO_MT_END, NULL }
200 const static MonoMetaTable MethodSchema [] = {
201 { MONO_MT_UINT32, "RVA" },
202 { MONO_MT_UINT16, "ImplFlags#MethodImplAttributes" },
203 { MONO_MT_UINT16, "Flags#MethodAttribute" },
204 { MONO_MT_STRING_IDX, "Name" },
205 { MONO_MT_BLOB_IDX, "Signature" },
206 { MONO_MT_TABLE_IDX, "ParamList:Param" },
207 { MONO_MT_END, NULL }
210 const static MonoMetaTable MethodImplSchema [] = {
211 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
212 { MONO_MT_MDOR_IDX, "MethodBody" },
213 { MONO_MT_MDOR_IDX, "MethodDeclaration" },
214 { MONO_MT_END, NULL }
217 const static MonoMetaTable MethodSemanticsSchema [] = {
218 { MONO_MT_UINT16, "MethodSemantic" },
219 { MONO_MT_TABLE_IDX, "Method:Method" },
220 { MONO_MT_HS_IDX, "Association" },
221 { MONO_MT_END, NULL }
224 const static MonoMetaTable ModuleSchema [] = {
225 { MONO_MT_UINT16, "Generation" },
226 { MONO_MT_STRING_IDX, "Name" },
227 { MONO_MT_GUID_IDX, "MVID" },
228 { MONO_MT_GUID_IDX, "EncID" },
229 { MONO_MT_GUID_IDX, "EncBaseID" },
230 { MONO_MT_END, NULL }
233 const static MonoMetaTable ModuleRefSchema [] = {
234 { MONO_MT_STRING_IDX, "Name" },
235 { MONO_MT_END, NULL }
238 const static MonoMetaTable NestedClassSchema [] = {
239 { MONO_MT_TABLE_IDX, "NestedClass:TypeDef" },
240 { MONO_MT_TABLE_IDX, "EnclosingClass:TypeDef" },
241 { MONO_MT_END, NULL }
244 const static MonoMetaTable ParamSchema [] = {
245 { MONO_MT_UINT16, "Flags" },
246 { MONO_MT_UINT16, "Sequence" },
247 { MONO_MT_STRING_IDX, "Name" },
248 { MONO_MT_END, NULL }
251 const static MonoMetaTable PropertySchema [] = {
252 { MONO_MT_UINT16, "Flags" },
253 { MONO_MT_STRING_IDX, "Name" },
254 { MONO_MT_BLOB_IDX, "Type" },
255 { MONO_MT_END, NULL }
258 const static MonoMetaTable PropertyMapSchema [] = {
259 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
260 { MONO_MT_TABLE_IDX, "PropertyList:Property" },
261 { MONO_MT_END, NULL }
264 const static MonoMetaTable StandaloneSigSchema [] = {
265 { MONO_MT_BLOB_IDX, "Signature" },
266 { MONO_MT_END, NULL }
269 const static MonoMetaTable TypeDefSchema [] = {
270 { MONO_MT_UINT32, "Flags" },
271 { MONO_MT_STRING_IDX, "Name" },
272 { MONO_MT_STRING_IDX, "Namespace" },
273 { MONO_MT_TDOR_IDX, "Extends" },
274 { MONO_MT_TABLE_IDX, "FieldList:Field" },
275 { MONO_MT_TABLE_IDX, "MethodList:Method" },
276 { MONO_MT_END, NULL }
279 const static MonoMetaTable TypeRefSchema [] = {
280 { MONO_MT_RS_IDX, "ResolutionScope=ResolutionScope" },
281 { MONO_MT_STRING_IDX, "Name" },
282 { MONO_MT_STRING_IDX, "Namespace" },
283 { MONO_MT_END, NULL }
286 const static MonoMetaTable TypeSpecSchema [] = {
287 { MONO_MT_BLOB_IDX, "Signature" },
288 { MONO_MT_END, NULL }
291 const static MonoMetaTable GenericParamSchema [] = {
292 { MONO_MT_UINT16, "Number" },
293 { MONO_MT_UINT16, "Flags" },
294 { MONO_MT_TABLE_IDX, "Owner" }, /* TypeDef or MethodDef */
295 { MONO_MT_STRING_IDX, "Name" },
297 /* soon to be removed */
298 { MONO_MT_TABLE_IDX, "Kind" },
300 { MONO_MT_END, NULL }
303 const static MonoMetaTable MethodSpecSchema [] = {
304 { MONO_MT_MDOR_IDX, "Method" },
305 { MONO_MT_BLOB_IDX, "Signature" },
306 { MONO_MT_END, NULL }
309 const static MonoMetaTable GenericParamConstraintSchema [] = {
310 { MONO_MT_TABLE_IDX, "GenericParam" },
311 { MONO_MT_TDOR_IDX, "Constraint" },
312 { MONO_MT_END, NULL }
315 const static struct {
316 const MonoMetaTable *table;
319 /* 0 */ { ModuleSchema, "Module" },
320 /* 1 */ { TypeRefSchema, "TypeRef" },
321 /* 2 */ { TypeDefSchema, "TypeDef" },
322 /* 3 */ { NULL, NULL },
323 /* 4 */ { FieldSchema, "Field" },
324 /* 5 */ { NULL, NULL },
325 /* 6 */ { MethodSchema, "Method" },
326 /* 7 */ { NULL, NULL },
327 /* 8 */ { ParamSchema, "Param" },
328 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
329 /* A */ { MemberRefSchema, "MemberRef" },
330 /* B */ { ConstantSchema, "Constant" },
331 /* C */ { CustomAttributeSchema, "CustomAttribute" },
332 /* D */ { FieldMarshalSchema, "FieldMarshal" },
333 /* E */ { DeclSecuritySchema, "DeclSecurity" },
334 /* F */ { ClassLayoutSchema, "ClassLayout" },
335 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
336 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
337 /* 12 */ { EventMapSchema, "EventMap" },
338 /* 13 */ { NULL, NULL },
339 /* 14 */ { EventSchema, "Event" },
340 /* 15 */ { PropertyMapSchema, "PropertyMap" },
341 /* 16 */ { NULL, NULL },
342 /* 17 */ { PropertySchema, "PropertyTable" },
343 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
344 /* 19 */ { MethodImplSchema, "MethodImpl" },
345 /* 1A */ { ModuleRefSchema, "ModuleRef" },
346 /* 1B */ { TypeSpecSchema, "TypeSpec" },
347 /* 1C */ { ImplMapSchema, "ImplMap" },
348 /* 1D */ { FieldRVASchema, "FieldRVA" },
349 /* 1E */ { NULL, NULL },
350 /* 1F */ { NULL, NULL },
351 /* 20 */ { AssemblySchema, "Assembly" },
352 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
353 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
354 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
355 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
356 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
357 /* 26 */ { FileSchema, "File" },
358 /* 27 */ { ExportedTypeSchema, "ExportedType" },
359 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
360 /* 29 */ { NestedClassSchema, "NestedClass" },
361 /* 2A */ { GenericParamSchema, "GenericParam" },
362 /* 2B */ { MethodSpecSchema, "MethodSpec" },
363 /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
367 * mono_meta_table_name:
368 * @table: table index
370 * Returns the name for the @table index
373 mono_meta_table_name (int table)
375 if ((table < 0) || (table > 0x2c))
378 return tables [table].name;
381 /* The guy who wrote the spec for this should not be allowed near a
384 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
385 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
386 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
387 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
388 inverse of this mapping.
391 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
392 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
394 /* Reference: Partition II - 23.2.6 */
396 * mono_metadata_compute_size:
397 * @meta: metadata context
398 * @tableindex: metadata table number
399 * @result_bitfield: pointer to guint32 where to store additional info
401 * mono_metadata_compute_size() computes the lenght in bytes of a single
402 * row in a metadata table. The size of each column is encoded in the
403 * @result_bitfield return value along with the number of columns in the table.
404 * the resulting bitfield should be handed to the mono_metadata_table_size()
405 * and mono_metadata_table_count() macros.
408 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
410 guint32 bitfield = 0;
411 int size = 0, field_size;
414 const MonoMetaTable *table = tables [tableindex].table;
416 for (i = 0; (code = table [i].code) != MONO_MT_END; i++){
419 field_size = 4; break;
422 field_size = 2; break;
425 field_size = 1; break;
427 case MONO_MT_BLOB_IDX:
428 field_size = meta->idx_blob_wide ? 4 : 2; break;
430 case MONO_MT_STRING_IDX:
431 field_size = meta->idx_string_wide ? 4 : 2; break;
433 case MONO_MT_GUID_IDX:
434 field_size = meta->idx_guid_wide ? 4 : 2; break;
436 case MONO_MT_TABLE_IDX:
437 /* Uhm, a table index can point to other tables besides the current one
438 * so, it's not correct to use the rowcount of the current table to
439 * get the size for this column - lupus
441 switch (tableindex) {
442 case MONO_TABLE_ASSEMBLYREFOS:
444 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
445 case MONO_TABLE_ASSEMBLYPROCESSOR:
447 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
448 case MONO_TABLE_CLASSLAYOUT:
450 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
451 case MONO_TABLE_EVENTMAP:
452 g_assert (i == 0 || i == 1);
453 field_size = i ? idx_size (MONO_TABLE_EVENT):
454 idx_size(MONO_TABLE_TYPEDEF);
456 case MONO_TABLE_EVENT:
458 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
459 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
461 case MONO_TABLE_EXPORTEDTYPE:
463 /* the index is in another metadata file, so it must be 4 */
464 field_size = 4; break;
465 case MONO_TABLE_FIELDLAYOUT:
467 field_size = idx_size (MONO_TABLE_FIELD); break;
468 case MONO_TABLE_FIELDRVA:
470 field_size = idx_size (MONO_TABLE_FIELD); break;
471 case MONO_TABLE_IMPLMAP:
473 field_size = idx_size (MONO_TABLE_MODULEREF); break;
474 case MONO_TABLE_INTERFACEIMPL:
476 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
477 case MONO_TABLE_METHOD:
479 field_size = idx_size (MONO_TABLE_PARAM); break;
480 case MONO_TABLE_METHODIMPL:
482 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
483 case MONO_TABLE_METHODSEMANTICS:
485 field_size = idx_size (MONO_TABLE_METHOD); break;
486 case MONO_TABLE_NESTEDCLASS:
487 g_assert (i == 0 || i == 1);
488 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
489 case MONO_TABLE_PROPERTYMAP:
490 g_assert (i == 0 || i == 1);
491 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
492 idx_size(MONO_TABLE_TYPEDEF);
494 case MONO_TABLE_TYPEDEF:
495 g_assert (i == 4 || i == 5);
496 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
497 idx_size(MONO_TABLE_METHOD);
498 case MONO_TABLE_GENERICPARAM:
499 g_assert (i == 2 || i == 4 || i == 5);
501 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
503 field_size = idx_size (MONO_TABLE_TYPEDEF);
505 field_size = idx_size (MONO_TABLE_TYPEDEF);
508 case MONO_TABLE_GENERICPARAMCONSTRAINT:
510 field_size = idx_size (MONO_TABLE_GENERICPARAM);
514 g_assert_not_reached ();
516 if (tableindex != MONO_TABLE_EXPORTEDTYPE && field_size != idx_size (tableindex))
517 g_warning ("size changed (%d to %d)", idx_size (tableindex), field_size);
522 * HasConstant: ParamDef, FieldDef, Property
524 case MONO_MT_CONST_IDX:
525 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
526 meta->tables [MONO_TABLE_FIELD].rows);
527 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
529 /* 2 bits to encode tag */
530 field_size = rtsize (n, 16-2);
534 * HasCustomAttribute: points to any table but
537 case MONO_MT_HASCAT_IDX:
539 * We believe that since the signature and
540 * permission are indexing the Blob heap,
541 * we should consider the blob size first
543 /* I'm not a believer - lupus
544 if (meta->idx_blob_wide){
549 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
550 meta->tables [MONO_TABLE_FIELD].rows);
551 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
552 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
553 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
554 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
555 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
556 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
557 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
558 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
559 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
560 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
561 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
562 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
563 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
564 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
565 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
566 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
567 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
569 /* 5 bits to encode */
570 field_size = rtsize (n, 16-5);
574 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
575 * MemberRef and String.
577 case MONO_MT_CAT_IDX:
578 /* String is a heap, if it is wide, we know the size */
580 if (meta->idx_string_wide){
585 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
586 meta->tables [MONO_TABLE_TYPEDEF].rows);
587 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
588 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
590 /* 3 bits to encode */
591 field_size = rtsize (n, 16-3);
595 * HasDeclSecurity: Typedef, MethodDef, Assembly
597 case MONO_MT_HASDEC_IDX:
598 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
599 meta->tables [MONO_TABLE_METHOD].rows);
600 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
602 /* 2 bits to encode */
603 field_size = rtsize (n, 16-2);
607 * Implementation: File, AssemblyRef, ExportedType
609 case MONO_MT_IMPL_IDX:
610 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
611 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
612 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
614 /* 2 bits to encode tag */
615 field_size = rtsize (n, 16-2);
619 * HasFieldMarshall: FieldDef, ParamDef
621 case MONO_MT_HFM_IDX:
622 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
623 meta->tables [MONO_TABLE_PARAM].rows);
625 /* 1 bit used to encode tag */
626 field_size = rtsize (n, 16-1);
630 * MemberForwarded: FieldDef, MethodDef
633 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
634 meta->tables [MONO_TABLE_METHOD].rows);
636 /* 1 bit used to encode tag */
637 field_size = rtsize (n, 16-1);
641 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
643 * It is TypeDef, _TypeRef_, TypeSpec, instead.
645 case MONO_MT_TDOR_IDX:
646 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
647 meta->tables [MONO_TABLE_TYPEREF].rows);
648 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
650 /* 2 bits to encode */
651 field_size = rtsize (n, 16-2);
655 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
657 case MONO_MT_MRP_IDX:
658 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
659 meta->tables [MONO_TABLE_TYPEREF].rows);
660 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
661 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
662 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
663 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
665 /* 3 bits to encode */
666 field_size = rtsize (n, 16 - 3);
669 case MONO_MT_MDOR_IDX:
672 * MethodDefOrRef: MethodDef, MemberRef
675 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
676 meta->tables [MONO_TABLE_MEMBERREF].rows);
678 /* 1 bit used to encode tag */
679 field_size = rtsize (n, 16-1);
683 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
686 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
687 meta->tables [MONO_TABLE_MODULEREF].rows);
688 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
689 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
691 /* 2 bits used to encode tag (ECMA spec claims 3) */
692 field_size = rtsize (n, 16 - 2);
697 * encode field size as follows (we just need to
704 bitfield |= (field_size-1) << shift;
707 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
710 *result_bitfield = (i << 24) | bitfield;
715 * mono_metadata_compute_table_bases:
716 * @meta: metadata context to compute table values
718 * Computes the table bases for the metadata structure.
719 * This is an internal function used by the image loader code.
722 mono_metadata_compute_table_bases (MonoImage *meta)
725 const char *base = meta->tables_base;
727 for (i = 0; i < 64; i++){
728 if (meta->tables [i].rows == 0)
731 meta->tables [i].row_size = mono_metadata_compute_size (
732 meta, i, &meta->tables [i].size_bitfield);
733 meta->tables [i].base = base;
734 base += meta->tables [i].rows * meta->tables [i].row_size;
739 * mono_metadata_locate:
740 * @meta: metadata context
741 * @table: table code.
742 * @idx: index of element to retrieve from @table.
744 * Returns a pointer to the @idx element in the metadata table
745 * whose code is @table.
748 mono_metadata_locate (MonoImage *meta, int table, int idx)
750 /* idx == 0 refers always to NULL */
751 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
753 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
757 * mono_metadata_locate_token:
758 * @meta: metadata context
759 * @token: metadata token
761 * Returns a pointer to the data in the metadata represented by the
765 mono_metadata_locate_token (MonoImage *meta, guint32 token)
767 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
771 * mono_metadata_get_table:
772 * @table: table to retrieve
774 * Returns the MonoMetaTable structure for table @table
776 const MonoMetaTable *
777 mono_metadata_get_table (MonoMetaTableEnum table)
781 g_return_val_if_fail ((x > 0) && (x <= MONO_TABLE_LAST), NULL);
783 return tables [table].table;
787 * mono_metadata_string_heap:
788 * @meta: metadata context
789 * @index: index into the string heap.
791 * Returns: an in-memory pointer to the @index in the string heap.
794 mono_metadata_string_heap (MonoImage *meta, guint32 index)
796 g_return_val_if_fail (index < meta->heap_strings.size, "");
797 return meta->heap_strings.data + index;
801 * mono_metadata_user_string:
802 * @meta: metadata context
803 * @index: index into the user string heap.
805 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
808 mono_metadata_user_string (MonoImage *meta, guint32 index)
810 g_return_val_if_fail (index < meta->heap_us.size, "");
811 return meta->heap_us.data + index;
815 * mono_metadata_blob_heap:
816 * @meta: metadata context
817 * @index: index into the blob.
819 * Returns: an in-memory pointer to the @index in the Blob heap.
822 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
824 g_return_val_if_fail (index < meta->heap_blob.size, "");
825 return meta->heap_blob.data + index;
829 * mono_metadata_guid_heap:
830 * @meta: metadata context
831 * @index: index into the guid heap.
833 * Returns: an in-memory pointer to the @index in the guid heap.
836 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
839 index *= 16; /* adjust for guid size and 1-based index */
840 g_return_val_if_fail (index < meta->heap_guid.size, "");
841 return meta->heap_guid.data + index;
845 dword_align (const char *ptr)
847 #if SIZEOF_VOID_P == 8
848 return (const char *) (((guint64) (ptr + 3)) & ~3);
850 return (const char *) (((guint32) (ptr + 3)) & ~3);
855 * mono_metadata_decode_row:
856 * @t: table to extract information from.
857 * @idx: index in table.
858 * @res: array of @res_size cols to store the results in
860 * This decompresses the metadata element @idx in table @t
861 * into the guint32 @res array that has res_size elements
864 mono_metadata_decode_row (MonoTableInfo *t, int idx, guint32 *res, int res_size)
866 guint32 bitfield = t->size_bitfield;
867 int i, count = mono_metadata_table_count (bitfield);
868 const char *data = t->base + idx * t->row_size;
870 g_assert (res_size == count);
872 for (i = 0; i < count; i++){
873 int n = mono_metadata_table_size (bitfield, i);
877 res [i] = *data; break;
879 res [i] = read16 (data); break;
882 res [i] = read32 (data); break;
885 g_assert_not_reached ();
892 * mono_metadata_decode_row_col:
893 * @t: table to extract information from.
894 * @idx: index for row in table.
895 * @col: column in the row.
897 * This function returns the value of column @col from the @idx
898 * row in the table @t.
901 mono_metadata_decode_row_col (MonoTableInfo *t, int idx, guint col)
903 guint32 bitfield = t->size_bitfield;
905 register const char *data = t->base + idx * t->row_size;
908 g_assert (col < mono_metadata_table_count (bitfield));
910 n = mono_metadata_table_size (bitfield, 0);
911 for (i = 0; i < col; ++i) {
913 n = mono_metadata_table_size (bitfield, i + 1);
919 return read16 (data);
921 return read32 (data);
923 g_assert_not_reached ();
928 * mono_metadata_decode_blob_size:
929 * @ptr: pointer to a blob object
930 * @rptr: the new position of the pointer
932 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
934 * Returns: the size of the blob object
937 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
939 const unsigned char *ptr = (const unsigned char *)xptr;
942 if ((*ptr & 0x80) == 0){
943 size = ptr [0] & 0x7f;
945 } else if ((*ptr & 0x40) == 0){
946 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
949 size = ((ptr [0] & 0x1f) << 24) +
962 * mono_metadata_decode_value:
963 * @ptr: pointer to decode from
964 * @rptr: the new position of the pointer
966 * This routine decompresses 32-bit values as specified in the "Blob and
967 * Signature" section (22.2)
969 * Returns: the decoded value
972 mono_metadata_decode_value (const char *_ptr, const char **rptr)
974 const unsigned char *ptr = (const unsigned char *) _ptr;
975 unsigned char b = *ptr;
978 if ((b & 0x80) == 0){
981 } else if ((b & 0x40) == 0){
982 len = ((b & 0x3f) << 8 | ptr [1]);
985 len = ((b & 0x1f) << 24) |
998 * mono_metadata_parse_typedef_or_ref:
999 * @m: a metadata context.
1000 * @ptr: a pointer to an encoded TypedefOrRef in @m
1001 * @rptr: pointer updated to match the end of the decoded stream
1003 * Returns: a token valid in the @m metadata decoded from
1004 * the compressed representation.
1007 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1010 token = mono_metadata_decode_value (ptr, &ptr);
1013 return mono_metadata_token_from_dor (token);
1017 * mono_metadata_parse_custom_mod:
1018 * @m: a metadata context.
1019 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1020 * @ptr: a pointer to (possibly) the start of a custom modifier list
1021 * @rptr: pointer updated to match the end of the decoded stream
1023 * Checks if @ptr points to a type custom modifier compressed representation.
1025 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1028 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1030 MonoCustomMod local;
1031 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
1032 (*ptr == MONO_TYPE_CMOD_REQD)) {
1035 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1036 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1043 * mono_metadata_parse_array:
1044 * @m: a metadata context.
1045 * @ptr: a pointer to an encoded array description.
1046 * @rptr: pointer updated to match the end of the decoded stream
1048 * Decodes the compressed array description found in the metadata @m at @ptr.
1050 * Returns: a #MonoArrayType structure describing the array type
1054 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1057 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1060 etype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1061 array->eklass = mono_class_from_mono_type (etype);
1062 array->rank = mono_metadata_decode_value (ptr, &ptr);
1064 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1065 if (array->numsizes)
1066 array->sizes = g_new0 (int, array->numsizes);
1067 for (i = 0; i < array->numsizes; ++i)
1068 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1070 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1071 if (array->numlobounds)
1072 array->lobounds = g_new0 (int, array->numlobounds);
1073 for (i = 0; i < array->numlobounds; ++i)
1074 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1082 * mono_metadata_free_array:
1083 * @array: array description
1085 * Frees the array description returned from mono_metadata_parse_array().
1088 mono_metadata_free_array (MonoArrayType *array)
1090 g_free (array->sizes);
1091 g_free (array->lobounds);
1096 * need to add common field and param attributes combinations:
1099 * public static literal
1102 * private static literal
1106 /* data, attrs, type, nmods, byref, pinned */
1107 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1108 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1109 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1110 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1111 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1112 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1113 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1114 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1115 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1116 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1117 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1118 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1119 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1120 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1121 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1122 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1123 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1124 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1125 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1126 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1127 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1128 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1129 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1130 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1131 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1132 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1133 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1134 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1135 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1136 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1137 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1138 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1141 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1143 static GHashTable *type_cache = NULL;
1146 * MonoTypes with modifies are never cached, so we never check or use that field.
1149 mono_type_hash (gconstpointer data)
1151 const MonoType *type = (const MonoType *) data;
1152 return type->type | (type->byref << 8) | (type->attrs << 9);
1156 mono_type_equal (gconstpointer ka, gconstpointer kb)
1158 const MonoType *a = (const MonoType *) ka;
1159 const MonoType *b = (const MonoType *) kb;
1161 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1163 /* need other checks */
1168 * mono_metadata_init:
1170 * Initialize the global variables of this module.
1173 mono_metadata_init (void)
1177 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1179 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1180 g_hash_table_insert (type_cache, &builtin_types [i], &builtin_types [i]);
1184 * mono_metadata_parse_type:
1185 * @m: metadata context
1186 * @mode: king of type that may be found at @ptr
1187 * @opt_attrs: optional attributes to store in the returned type
1188 * @ptr: pointer to the type representation
1189 * @rptr: pointer updated to match the end of the decoded stream
1191 * Decode a compressed type description found at @ptr in @m.
1192 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1193 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1194 * This function can be used to decode type descriptions in method signatures,
1195 * field signatures, locals signatures etc.
1197 * Returns: a #MonoType structure representing the decoded type.
1200 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs, const char *ptr, const char **rptr)
1202 MonoType *type, *cached;
1203 gboolean byref = FALSE;
1206 * According to the spec, custom modifiers should come before the byref
1207 * flag, but the IL produced by ilasm from the following signature:
1208 * object modopt(...) &
1209 * starts with a byref flag, followed by the modifiers. (bug #49802)
1210 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1211 * it would be better to treat byref as real type constructor instead of
1214 if (*ptr == MONO_TYPE_BYREF) {
1220 case MONO_PARSE_MOD_TYPE:
1221 case MONO_PARSE_PARAM:
1222 case MONO_PARSE_RET:
1223 case MONO_PARSE_LOCAL: /* should not have modifiers according to the spec, but ms tools disagree */
1224 case MONO_PARSE_FIELD: {
1225 /* count the modifiers */
1226 const char *tmp_ptr = ptr;
1228 while (mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr))
1231 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1232 type->num_mods = count;
1234 g_warning ("got more than 64 modifiers in type");
1235 /* save them this time */
1237 while (mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr))
1240 } /* fall through */
1242 case MONO_PARSE_TYPE:
1244 * Later we can avoid doing this allocation.
1246 type = g_new0 (MonoType, 1);
1249 g_assert_not_reached ();
1252 type->attrs = opt_attrs;
1253 type->byref = byref;
1254 if (mode == MONO_PARSE_LOCAL) {
1256 * check for pinned flag
1258 if (*ptr == MONO_TYPE_PINNED) {
1265 case MONO_TYPE_BYREF:
1266 if (mode == MONO_PARSE_FIELD)
1267 g_warning ("A field type cannot be byref");
1270 /* follow through */
1272 /*if (*ptr == MONO_TYPE_VOID && mode != MONO_PARSE_RET)
1273 g_error ("void not allowed in param");*/
1274 do_mono_metadata_parse_type (type, m, ptr, &ptr);
1280 /* No need to use locking since nobody is modifying the hash table */
1281 if (mode != MONO_PARSE_PARAM && !type->num_mods && (cached = g_hash_table_lookup (type_cache, type))) {
1282 mono_metadata_free_type (type);
1290 * mono_metadata_parse_signature:
1291 * @image: metadata context
1292 * @toke: metadata token
1294 * Decode a method signature stored in the STANDALONESIG table
1296 * Returns: a MonoMethodSignature describing the signature.
1298 MonoMethodSignature *
1299 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1301 MonoTableInfo *tables = image->tables;
1302 guint32 idx = mono_metadata_token_index (token);
1307 return mono_lookup_dynamic_token (image, token);
1309 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1311 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1313 ptr = mono_metadata_blob_heap (image, sig);
1314 mono_metadata_decode_blob_size (ptr, &ptr);
1316 return mono_metadata_parse_method_signature (image, FALSE, ptr, NULL);
1319 MonoMethodSignature*
1320 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1322 MonoMethodSignature *sig;
1324 /* later we want to allocate signatures with mempools */
1325 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1326 sig->param_count = nparams;
1331 MonoMethodSignature*
1332 mono_metadata_signature_dup (MonoMethodSignature *sig)
1336 sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
1337 return g_memdup (sig, sigsize);
1341 * mono_metadata_parse_method_signature:
1342 * @m: metadata context
1343 * @def: the MethodDef index or 0 for Ref signatures.
1344 * @ptr: pointer to the signature metadata representation
1345 * @rptr: pointer updated to match the end of the decoded stream
1347 * Decode a method signature stored at @ptr.
1349 * Returns: a MonoMethodSignature describing the signature.
1351 MonoMethodSignature *
1352 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1354 MonoMethodSignature *method;
1355 int i, ret_attrs = 0, *pattrs = NULL;
1356 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1357 guint32 gen_param_count = 0;
1360 gen_param_count = 1;
1365 call_convention = *ptr & 0x0F;
1367 if (gen_param_count)
1368 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1369 param_count = mono_metadata_decode_value (ptr, &ptr);
1370 pattrs = g_new0 (int, param_count);
1373 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1374 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1375 guint32 cols [MONO_PARAM_SIZE];
1376 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1378 if (def < methodt->rows)
1379 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1381 lastp = paramt->rows + 1;
1382 for (i = param_index; i < lastp; ++i) {
1383 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1384 if (!cols [MONO_PARAM_SEQUENCE])
1385 ret_attrs = cols [MONO_PARAM_FLAGS];
1387 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1390 method = mono_metadata_signature_alloc (m, param_count);
1391 method->hasthis = hasthis;
1392 method->explicit_this = explicit_this;
1393 method->call_convention = call_convention;
1394 method->generic_param_count = gen_param_count;
1395 if (call_convention != 0xa)
1396 method->ret = mono_metadata_parse_type (m, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1398 if (method->param_count) {
1399 method->sentinelpos = -1;
1401 for (i = 0; i < method->param_count; ++i) {
1402 if (*ptr == MONO_TYPE_SENTINEL) {
1403 if (method->call_convention != MONO_CALL_VARARG || def)
1404 g_error ("found sentinel for methoddef or no vararg method");
1405 method->sentinelpos = i;
1408 method->params [i] = mono_metadata_parse_type (m, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1416 * Add signature to a cache and increase ref count...
1422 * mono_metadata_free_method_signature:
1423 * @sig: signature to destroy
1425 * Free the memory allocated in the signature @sig.
1428 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1431 mono_metadata_free_type (sig->ret);
1432 for (i = 0; i < sig->param_count; ++i)
1433 mono_metadata_free_type (sig->params [i]);
1439 do_mono_metadata_parse_generic_inst (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1441 MonoGenericInst *ginst = g_new0 (MonoGenericInst, 1);
1445 type->data.generic_inst = ginst;
1447 ginst->context = g_new0 (MonoGenericContext, 1);
1448 ginst->context->ginst = ginst;
1450 ginst->klass = g_new0 (MonoClass, 1);
1452 ginst->generic_type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1453 ginst->type_argc = count = mono_metadata_decode_value (ptr, &ptr);
1454 ginst->type_argv = g_new0 (MonoType*, count);
1457 * Create the klass before parsing the type arguments.
1458 * This is required to support "recursive" definitions.
1459 * See mcs/tests/gen-23.cs for an example.
1462 ginst->init_pending = TRUE;
1464 mono_class_create_generic (ginst);
1466 for (i = 0; i < ginst->type_argc; i++) {
1467 MonoType *t = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1469 ginst->type_argv [i] = t;
1470 if (!ginst->is_open)
1471 ginst->is_open = mono_class_is_open_constructed_type (t);
1479 * We may be called multiple times on different metadata to create the same
1480 * instantiated type. This happens for instance if we're part of a method or
1481 * local variable signature.
1483 * It's important to return the same MonoGenericInst * for each particualar
1484 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1487 * According to the spec ($26.1.5), a static variable in a generic class
1488 * declaration is shared amongst all instances of the same closed constructed
1492 cached = g_hash_table_lookup (m->generic_inst_cache, ginst);
1494 g_free (ginst->klass);
1495 g_free (ginst->type_argv);
1498 type->data.generic_inst = cached->data.generic_inst;
1501 cached = g_new0 (MonoType, 1);
1502 cached->type = MONO_TYPE_GENERICINST;
1503 cached->data.generic_inst = ginst;
1505 g_hash_table_insert (m->generic_inst_cache, ginst, cached);
1507 mono_stats.generic_instance_count++;
1508 mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
1509 sizeof (MonoGenericContext) +
1510 ginst->type_argc * sizeof (MonoType);
1513 ginst->init_pending = FALSE;
1516 static MonoGenericParam *
1517 mono_metadata_parse_generic_param (MonoImage *m, const char *ptr, const char **rptr)
1519 MonoGenericParam *generic_param = g_new0 (MonoGenericParam, 1);
1521 generic_param->num = mono_metadata_decode_value (ptr, &ptr);
1526 return generic_param;
1530 * do_mono_metadata_parse_type:
1531 * @type: MonoType to be filled in with the return value
1533 * Internal routine used to "fill" the contents of @type from an
1534 * allocated pointer. This is done this way to avoid doing too
1535 * many mini-allocations (particularly for the MonoFieldType which
1536 * most of the time is just a MonoType, but sometimes might be augmented).
1538 * This routine is used by mono_metadata_parse_type and
1539 * mono_metadata_parse_field_type
1541 * This extracts a Type as specified in Partition II (22.2.12)
1544 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1546 type->type = mono_metadata_decode_value (ptr, &ptr);
1548 switch (type->type){
1549 case MONO_TYPE_VOID:
1550 case MONO_TYPE_BOOLEAN:
1551 case MONO_TYPE_CHAR:
1564 case MONO_TYPE_STRING:
1565 case MONO_TYPE_OBJECT:
1566 case MONO_TYPE_TYPEDBYREF:
1568 case MONO_TYPE_VALUETYPE:
1569 case MONO_TYPE_CLASS: {
1571 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1572 type->data.klass = mono_class_get (m, token);
1575 case MONO_TYPE_SZARRAY: {
1576 MonoType *etype = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1577 type->data.klass = mono_class_from_mono_type (etype);
1581 type->data.type = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1583 case MONO_TYPE_FNPTR:
1584 type->data.method = mono_metadata_parse_method_signature (m, 0, ptr, &ptr);
1586 case MONO_TYPE_ARRAY:
1587 type->data.array = mono_metadata_parse_array (m, ptr, &ptr);
1590 case MONO_TYPE_MVAR:
1592 type->data.generic_param = mono_metadata_parse_generic_param (m, ptr, &ptr);
1595 case MONO_TYPE_GENERICINST:
1596 do_mono_metadata_parse_generic_inst (type, m, ptr, &ptr);
1600 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1608 * mono_metadata_free_type:
1609 * @type: type to free
1611 * Free the memory allocated for type @type.
1614 mono_metadata_free_type (MonoType *type)
1616 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1618 switch (type->type){
1620 mono_metadata_free_type (type->data.type);
1622 case MONO_TYPE_FNPTR:
1623 mono_metadata_free_method_signature (type->data.method);
1625 case MONO_TYPE_ARRAY:
1626 mono_metadata_free_array (type->data.array);
1634 hex_dump (const char *buffer, int base, int count)
1636 int show_header = 1;
1644 for (i = 0; i < count; i++){
1647 printf ("\n0x%08x: ", (unsigned char) base + i);
1649 printf ("%02x ", (unsigned char) (buffer [i]));
1656 * @mh: The Method header
1657 * @ptr: Points to the beginning of the Section Data (25.3)
1660 parse_section_data (MonoMethodHeader *mh, const unsigned char *ptr)
1662 unsigned char sect_data_flags;
1663 const unsigned char *sptr;
1665 guint32 sect_data_len;
1668 /* align on 32-bit boundary */
1669 /* FIXME: not 64-bit clean code */
1670 sptr = ptr = dword_align (ptr);
1671 sect_data_flags = *ptr;
1674 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1676 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1679 sect_data_len = ptr [0];
1683 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1684 hex_dump (sptr, 0, sect_data_len+8);
1685 g_print ("\nheader: ");
1686 hex_dump (sptr-4, 0, 4);
1690 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1691 const unsigned char *p = dword_align (ptr);
1693 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1694 /* we could just store a pointer if we don't need to byteswap */
1695 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1696 for (i = 0; i < mh->num_clauses; ++i) {
1697 MonoExceptionClause *ec = &mh->clauses [i];
1700 /* we could memcpy and byteswap */
1701 ec->flags = read32 (p);
1703 ec->try_offset = read32 (p);
1705 ec->try_len = read32 (p);
1707 ec->handler_offset = read32 (p);
1709 ec->handler_len = read32 (p);
1711 ec->token_or_filter = read32 (p);
1714 ec->flags = read16 (p);
1716 ec->try_offset = read16 (p);
1720 ec->handler_offset = read16 (p);
1722 ec->handler_len = *p;
1724 ec->token_or_filter = read32 (p);
1727 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
1731 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
1732 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
1739 * mono_metadata_parse_mh:
1740 * @m: metadata context
1741 * @ptr: pointer to the method header.
1743 * Decode the method header at @ptr, including pointer to the IL code,
1744 * info about local variables and optional exception tables.
1746 * Returns: a MonoMethodHeader.
1749 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
1751 MonoMethodHeader *mh;
1752 unsigned char flags = *(const unsigned char *) ptr;
1753 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
1755 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
1756 const unsigned char *code;
1759 g_return_val_if_fail (ptr != NULL, NULL);
1762 case METHOD_HEADER_TINY_FORMAT:
1763 mh = g_new0 (MonoMethodHeader, 1);
1766 local_var_sig_tok = 0;
1767 mh->code_size = flags >> 2;
1770 case METHOD_HEADER_TINY_FORMAT1:
1771 mh = g_new0 (MonoMethodHeader, 1);
1774 local_var_sig_tok = 0;
1777 * The spec claims 3 bits, but the Beta2 is
1780 mh->code_size = flags >> 2;
1783 case METHOD_HEADER_FAT_FORMAT:
1784 fat_flags = read16 (ptr);
1786 hsize = (fat_flags >> 12) & 0xf;
1787 max_stack = read16 (ptr);
1789 code_size = read32 (ptr);
1791 local_var_sig_tok = read32 (ptr);
1794 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
1801 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
1805 * There are more sections
1807 ptr = code + code_size;
1815 if (local_var_sig_tok) {
1816 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1817 const char *locals_ptr;
1818 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1819 int len=0, i, bsize;
1821 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
1822 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1823 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1824 if (*locals_ptr != 0x07)
1825 g_warning ("wrong signature for locals blob");
1827 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
1828 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1829 mh->num_locals = len;
1830 for (i = 0; i < len; ++i)
1831 mh->locals [i] = mono_metadata_parse_type (m, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
1833 mh = g_new0 (MonoMethodHeader, 1);
1836 mh->code_size = code_size;
1837 mh->max_stack = max_stack;
1838 mh->init_locals = init_locals;
1839 if (fat_flags & METHOD_HEADER_MORE_SECTS)
1840 parse_section_data (mh, (const unsigned char*)ptr);
1845 * mono_metadata_free_mh:
1846 * @mh: a method header
1848 * Free the memory allocated for the method header.
1851 mono_metadata_free_mh (MonoMethodHeader *mh)
1854 for (i = 0; i < mh->num_locals; ++i)
1855 mono_metadata_free_type (mh->locals[i]);
1856 g_free (mh->clauses);
1861 * mono_metadata_parse_field_type:
1862 * @m: metadata context to extract information from
1863 * @ptr: pointer to the field signature
1864 * @rptr: pointer updated to match the end of the decoded stream
1866 * Parses the field signature, and returns the type information for it.
1868 * Returns: The MonoType that was extracted from @ptr.
1871 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
1873 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
1877 * mono_metadata_parse_param:
1878 * @m: metadata context to extract information from
1879 * @ptr: pointer to the param signature
1880 * @rptr: pointer updated to match the end of the decoded stream
1882 * Parses the param signature, and returns the type information for it.
1884 * Returns: The MonoType that was extracted from @ptr.
1887 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
1889 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
1893 * mono_metadata_token_from_dor:
1894 * @dor_token: A TypeDefOrRef coded index
1896 * dor_token is a TypeDefOrRef coded index: it contains either
1897 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
1898 * bits contain an index into the table.
1900 * Returns: an expanded token
1903 mono_metadata_token_from_dor (guint32 dor_index)
1907 table = dor_index & 0x03;
1908 idx = dor_index >> 2;
1911 case 0: /* TypeDef */
1912 return MONO_TOKEN_TYPE_DEF | idx;
1914 case 1: /* TypeRef */
1915 return MONO_TOKEN_TYPE_REF | idx;
1917 case 2: /* TypeSpec */
1918 return MONO_TOKEN_TYPE_SPEC | idx;
1921 g_assert_not_reached ();
1928 * We use this to pass context information to the row locator
1931 int idx; /* The index that we are trying to locate */
1932 int col_idx; /* The index in the row where idx may be stored */
1933 MonoTableInfo *t; /* pointer to the table */
1938 * How the row locator works.
1943 * ___|___------> _______
1946 * A column in the rows of table A references an index in table B.
1947 * For example A may be the TYPEDEF table and B the METHODDEF table.
1949 * Given an index in table B we want to get the row in table A
1950 * where the column n references our index in B.
1952 * In the locator_t structure:
1954 * col_idx is the column number
1955 * index is the index in table B
1956 * result will be the index in table A
1959 * Table A Table B column (in table A)
1960 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
1961 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
1962 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
1963 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
1964 * METHODSEM PROPERTY ASSOCIATION (encoded index)
1966 * Note that we still don't support encoded indexes.
1970 typedef_locator (const void *a, const void *b)
1972 locator_t *loc = (locator_t *) a;
1973 const char *bb = (const char *) b;
1974 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
1975 guint32 col, col_next;
1977 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
1983 * Need to check that the next row is valid.
1985 if (typedef_index + 1 < loc->t->rows) {
1986 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
1987 if (loc->idx >= col_next)
1990 if (col == col_next)
1994 loc->result = typedef_index;
2000 table_locator (const void *a, const void *b)
2002 locator_t *loc = (locator_t *) a;
2003 const char *bb = (const char *) b;
2004 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2007 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2009 if (loc->idx == col) {
2010 loc->result = table_index;
2020 * mono_metadata_typedef_from_field:
2021 * @meta: metadata context
2022 * @index: FieldDef token
2024 * Returns the 1-based index into the TypeDef table of the type that
2025 * declared the field described by @index.
2026 * Returns 0 if not found.
2029 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2031 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2037 loc.idx = mono_metadata_token_index (index);
2038 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2041 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2042 g_assert_not_reached ();
2044 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2045 return loc.result + 1;
2049 * mono_metadata_typedef_from_method:
2050 * @meta: metadata context
2051 * @index: MethodDef token
2053 * Returns the 1-based index into the TypeDef table of the type that
2054 * declared the method described by @index.
2055 * Returns 0 if not found.
2058 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2060 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2066 loc.idx = mono_metadata_token_index (index);
2067 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2070 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2071 g_assert_not_reached ();
2073 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2074 return loc.result + 1;
2078 * mono_metadata_interfaces_from_typedef:
2079 * @meta: metadata context
2080 * @index: typedef token
2082 * Returns and array of interfaces that the @index typedef token implements.
2083 * The number of elemnts in the array is returned in @count.
2086 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2088 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2091 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2099 loc.idx = mono_metadata_token_index (index);
2100 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2103 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2108 * We may end up in the middle of the rows...
2111 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2118 while (start < tdef->rows) {
2119 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2120 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2122 result = g_renew (MonoClass*, result, i + 1);
2123 result [i] = mono_class_get (meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]));
2131 * mono_metadata_nested_in_typedef:
2132 * @meta: metadata context
2133 * @index: typedef token
2135 * Returns: the 1-based index into the TypeDef table of the type
2136 * where the type described by @index is nested.
2137 * Retruns 0 if @index describes a non-nested type.
2140 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2142 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2148 loc.idx = mono_metadata_token_index (index);
2149 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2152 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2155 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2156 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2160 * mono_metadata_nesting_typedef:
2161 * @meta: metadata context
2162 * @index: typedef token
2164 * Returns: the 1-based index into the TypeDef table of the first type
2165 * that is nested inside the type described by @index. The search starts at
2167 * Returns 0 if no such type is found.
2170 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2172 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2174 guint32 class_index = mono_metadata_token_index (index);
2179 start = start_index;
2181 while (start <= tdef->rows) {
2182 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2188 if (start > tdef->rows)
2195 * mono_metadata_packing_from_typedef:
2196 * @meta: metadata context
2197 * @index: token representing a type
2199 * Returns the info stored in the ClassLAyout table for the given typedef token
2200 * into the @packing and @size pointers.
2201 * Returns 0 if the info is not found.
2204 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2206 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2208 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2213 loc.idx = mono_metadata_token_index (index);
2214 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2217 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2220 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2222 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2224 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2226 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2227 return loc.result + 1;
2231 * mono_metadata_custom_attrs_from_index:
2232 * @meta: metadata context
2233 * @index: token representing the parent
2235 * Returns: the 1-based index into the CustomAttribute table of the first
2236 * attribute which belongs to the metadata object described by @index.
2237 * Returns 0 if no such attribute is found.
2240 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2242 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2249 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2252 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2255 /* Find the first entry by searching backwards */
2256 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2259 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2260 return loc.result + 1;
2265 mono_backtrace (int limit)
2270 backtrace (array, limit);
2271 names = backtrace_symbols (array, limit);
2272 for (i =0; i < limit; ++i) {
2273 g_print ("\t%s\n", names [i]);
2280 /*#define __alignof__(a) sizeof(a)*/
2281 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2286 * @t: the type to return the size of
2288 * Returns: the number of bytes required to hold an instance of this
2292 mono_type_size (MonoType *t, gint *align)
2299 *align = __alignof__(gpointer);
2300 return sizeof (gpointer);
2304 case MONO_TYPE_VOID:
2307 case MONO_TYPE_BOOLEAN:
2308 *align = __alignof__(gint8);
2312 *align = __alignof__(gint8);
2314 case MONO_TYPE_CHAR:
2317 *align = __alignof__(gint16);
2321 *align = __alignof__(gint32);
2324 *align = __alignof__(float);
2328 *align = __alignof__(gint64);
2331 *align = __alignof__(double);
2335 *align = __alignof__(gpointer);
2336 return sizeof (gpointer);
2337 case MONO_TYPE_STRING:
2338 *align = __alignof__(gpointer);
2339 return sizeof (gpointer);
2340 case MONO_TYPE_OBJECT:
2341 *align = __alignof__(gpointer);
2342 return sizeof (gpointer);
2343 case MONO_TYPE_VALUETYPE: {
2344 if (t->data.klass->enumtype)
2345 return mono_type_size (t->data.klass->enum_basetype, align);
2347 return mono_class_value_size (t->data.klass, align);
2349 case MONO_TYPE_CLASS:
2350 case MONO_TYPE_SZARRAY:
2352 case MONO_TYPE_FNPTR:
2353 case MONO_TYPE_ARRAY:
2354 *align = __alignof__(gpointer);
2355 return sizeof (gpointer);
2356 case MONO_TYPE_TYPEDBYREF:
2357 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2358 case MONO_TYPE_GENERICINST: {
2359 MonoGenericInst *ginst = t->data.generic_inst;
2360 MonoClass *iclass = mono_class_from_mono_type (ginst->generic_type);
2361 return mono_type_size (&iclass->byval_arg, align);
2364 case MONO_TYPE_MVAR:
2365 *align = __alignof__(gpointer);
2366 return sizeof (gpointer);
2368 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2374 * mono_type_stack_size:
2375 * @t: the type to return the size it uses on the stack
2377 * Returns: the number of bytes required to hold an instance of this
2378 * type on the runtime stack
2381 mono_type_stack_size (MonoType *t, gint *align)
2385 g_assert (t != NULL);
2391 *align = __alignof__(gpointer);
2392 return sizeof (gpointer);
2396 case MONO_TYPE_BOOLEAN:
2397 case MONO_TYPE_CHAR:
2406 case MONO_TYPE_STRING:
2407 case MONO_TYPE_OBJECT:
2408 case MONO_TYPE_CLASS:
2409 case MONO_TYPE_SZARRAY:
2411 case MONO_TYPE_FNPTR:
2412 case MONO_TYPE_ARRAY:
2413 *align = __alignof__(gpointer);
2414 return sizeof (gpointer);
2415 case MONO_TYPE_TYPEDBYREF:
2416 *align = __alignof__(gpointer);
2417 return sizeof (gpointer) * 3;
2419 *align = __alignof__(float);
2420 return sizeof (float);
2423 *align = __alignof__(gint64);
2424 return sizeof (gint64);
2426 *align = __alignof__(double);
2427 return sizeof (double);
2428 case MONO_TYPE_VALUETYPE: {
2431 if (t->data.klass->enumtype)
2432 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2434 size = mono_class_value_size (t->data.klass, align);
2436 *align = *align + __alignof__(gpointer) - 1;
2437 *align &= ~(__alignof__(gpointer) - 1);
2439 size += sizeof (gpointer) - 1;
2440 size &= ~(sizeof (gpointer) - 1);
2445 case MONO_TYPE_GENERICINST: {
2446 MonoGenericInst *ginst = t->data.generic_inst;
2447 MonoClass *iclass = mono_class_from_mono_type (ginst->generic_type);
2448 return mono_type_stack_size (&iclass->byval_arg, align);
2451 g_error ("type 0x%02x unknown", t->type);
2457 mono_metadata_generic_inst_hash (MonoGenericInst *ginst)
2459 return mono_metadata_type_hash (ginst->generic_type);
2463 mono_metadata_generic_inst_equal (MonoGenericInst *g1, MonoGenericInst *g2)
2467 if (g1->type_argc != g2->type_argc)
2469 if (!mono_metadata_type_equal (g1->generic_type, g2->generic_type))
2471 for (i = 0; i < g1->type_argc; ++i) {
2472 if (!mono_metadata_type_equal (g1->type_argv [i], g2->type_argv [i]))
2479 * mono_metadata_type_hash:
2482 * Computes an hash value for @t1 to be used in GHashTable.
2485 mono_metadata_type_hash (MonoType *t1)
2487 guint hash = t1->type;
2489 hash |= t1->byref << 6; /* do not collide with t1->type values */
2491 case MONO_TYPE_VALUETYPE:
2492 case MONO_TYPE_CLASS:
2493 case MONO_TYPE_SZARRAY:
2494 /* check if the distribution is good enough */
2495 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2497 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2498 case MONO_TYPE_ARRAY:
2499 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2500 case MONO_TYPE_GENERICINST:
2501 return ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (t1->data.generic_inst);
2507 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2)
2511 if (c1->generic_inst && c2->generic_inst)
2512 return mono_metadata_generic_inst_equal (c1->generic_inst, c2->generic_inst);
2513 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
2514 return c1->byval_arg.data.generic_param->num == c2->byval_arg.data.generic_param->num;
2515 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
2516 return c1->byval_arg.data.generic_param->num == c2->byval_arg.data.generic_param->num;
2521 * mono_metadata_type_equal:
2525 * Determine if @t1 and @t2 represent the same type.
2526 * Returns: #TRUE if @t1 and @t2 are equal.
2529 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2531 if (t1->type != t2->type ||
2532 t1->byref != t2->byref)
2536 case MONO_TYPE_VOID:
2537 case MONO_TYPE_BOOLEAN:
2538 case MONO_TYPE_CHAR:
2549 case MONO_TYPE_STRING:
2552 case MONO_TYPE_OBJECT:
2553 case MONO_TYPE_TYPEDBYREF:
2555 case MONO_TYPE_VALUETYPE:
2556 case MONO_TYPE_CLASS:
2557 case MONO_TYPE_SZARRAY:
2558 return mono_metadata_class_equal (t1->data.klass, t2->data.klass);
2560 return mono_metadata_type_equal (t1->data.type, t2->data.type);
2561 case MONO_TYPE_ARRAY:
2562 if (t1->data.array->rank != t2->data.array->rank)
2564 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass);
2565 case MONO_TYPE_GENERICINST:
2566 return mono_metadata_generic_inst_equal (t1->data.generic_inst,
2567 t2->data.generic_inst);
2569 case MONO_TYPE_MVAR:
2570 return t1->data.generic_param->num == t2->data.generic_param->num;
2572 g_error ("implement type compare for %0x!", t1->type);
2580 * mono_metadata_signature_equal:
2581 * @sig1: a signature
2582 * @sig2: another signature
2584 * Determine if @sig1 and @sig2 represent the same signature, with the
2585 * same number of arguments and the same types.
2586 * Returns: #TRUE if @sig1 and @sig2 are equal.
2589 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2593 if (sig1->hasthis != sig2->hasthis ||
2594 sig1->param_count != sig2->param_count)
2597 for (i = 0; i < sig1->param_count; i++) {
2598 MonoType *p1 = sig1->params[i];
2599 MonoType *p2 = sig2->params[i];
2601 //if (p1->attrs != p2->attrs)
2604 if (!mono_metadata_type_equal (p1, p2))
2608 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
2614 mono_signature_hash (MonoMethodSignature *sig)
2616 guint i, res = sig->ret->type;
2618 for (i = 0; i < sig->param_count; i++)
2619 res = (res << 5) - res + sig->params[i]->type;
2625 * mono_metadata_encode_value:
2626 * @value: value to encode
2627 * @buf: buffer where to write the compressed representation
2628 * @endbuf: pointer updated to point at the end of the encoded output
2630 * Encodes the value @value in the compressed representation used
2631 * in metadata and stores the result in @buf. @buf needs to be big
2632 * enough to hold the data (4 bytes).
2635 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
2641 else if (value < 0x4000) {
2642 p [0] = 0x80 | (value >> 8);
2643 p [1] = value & 0xff;
2646 p [0] = (value >> 24) | 0xc0;
2647 p [1] = (value >> 16) & 0xff;
2648 p [2] = (value >> 8) & 0xff;
2649 p [3] = value & 0xff;
2657 * mono_metadata_field_info:
2658 * @meta: the Image the field is defined in
2659 * @index: the index in the field table representing the field
2660 * @offset: a pointer to an integer where to store the offset that
2661 * may have been specified for the field in a FieldLayout table
2662 * @rva: a pointer to the RVA of the field data in the image that
2663 * may have been defined in a FieldRVA table
2664 * @marshal_spec: a pointer to the marshal spec that may have been
2665 * defined for the field in a FieldMarshal table.
2667 * Gather info for field @index that may have been defined in the FieldLayout,
2668 * FieldRVA and FieldMarshal tables.
2669 * Either of offset, rva and marshal_spec can be NULL if you're not interested
2673 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
2674 MonoMarshalSpec **marshal_spec)
2676 MonoTableInfo *tdef;
2679 loc.idx = index + 1;
2681 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
2683 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
2686 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2687 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
2689 *offset = (guint32)-1;
2693 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
2695 loc.col_idx = MONO_FIELD_RVA_FIELD;
2698 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2700 * LAMESPEC: There is no signature, no nothing, just the raw data.
2702 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
2710 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
2711 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
2718 * mono_metadata_get_constant_index:
2719 * @meta: the Image the field is defined in
2720 * @index: the token that may have a row defined in the constants table
2721 * @hint: possible position for the row
2723 * @token must be a FieldDef, ParamDef or PropertyDef token.
2725 * Returns: the index into the Constants table or 0 if not found.
2728 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
2730 MonoTableInfo *tdef;
2732 guint32 index = mono_metadata_token_index (token);
2734 tdef = &meta->tables [MONO_TABLE_CONSTANT];
2735 index <<= HASCONSTANT_BITS;
2736 switch (mono_metadata_token_table (token)) {
2737 case MONO_TABLE_FIELD:
2738 index |= HASCONSTANT_FIEDDEF;
2740 case MONO_TABLE_PARAM:
2741 index |= HASCONSTANT_PARAM;
2743 case MONO_TABLE_PROPERTY:
2744 index |= HASCONSTANT_PROPERTY;
2747 g_warning ("Not a valid token for the constant table: 0x%08x", token);
2751 loc.col_idx = MONO_CONSTANT_PARENT;
2754 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
2757 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2758 return loc.result + 1;
2764 * mono_metadata_events_from_typedef:
2765 * @meta: metadata context
2766 * @index: 0-based index (in the TypeDef table) describing a type
2768 * Returns: the 0-based index in the Event table for the events in the
2769 * type. The last event that belongs to the type (plus 1) is stored
2770 * in the @end_idx pointer.
2773 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2777 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
2785 loc.col_idx = MONO_EVENT_MAP_PARENT;
2786 loc.idx = index + 1;
2788 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2791 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
2792 if (loc.result + 1 < tdef->rows) {
2793 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
2795 end = meta->tables [MONO_TABLE_EVENT].rows;
2803 * mono_metadata_methods_from_event:
2804 * @meta: metadata context
2805 * @index: 0-based index (in the Event table) describing a event
2807 * Returns: the 0-based index in the MethodDef table for the methods in the
2808 * event. The last method that belongs to the event (plus 1) is stored
2809 * in the @end_idx pointer.
2812 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
2816 guint32 cols [MONO_METHOD_SEMA_SIZE];
2817 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2824 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2825 loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_EVENT; /* Method association coded index */
2827 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2832 * We may end up in the middle of the rows...
2835 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2841 while (end < msemt->rows) {
2842 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2843 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2852 * mono_metadata_properties_from_typedef:
2853 * @meta: metadata context
2854 * @index: 0-based index (in the TypeDef table) describing a type
2856 * Returns: the 0-based index in the Property table for the properties in the
2857 * type. The last property that belongs to the type (plus 1) is stored
2858 * in the @end_idx pointer.
2861 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2865 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
2873 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
2874 loc.idx = index + 1;
2876 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2879 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
2880 if (loc.result + 1 < tdef->rows) {
2881 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
2883 end = meta->tables [MONO_TABLE_PROPERTY].rows;
2891 * mono_metadata_methods_from_property:
2892 * @meta: metadata context
2893 * @index: 0-based index (in the PropertyDef table) describing a property
2895 * Returns: the 0-based index in the MethodDef table for the methods in the
2896 * property. The last method that belongs to the property (plus 1) is stored
2897 * in the @end_idx pointer.
2900 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
2904 guint32 cols [MONO_METHOD_SEMA_SIZE];
2905 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2912 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2913 loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_PROPERTY; /* Method association coded index */
2915 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2920 * We may end up in the middle of the rows...
2923 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2929 while (end < msemt->rows) {
2930 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2931 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2940 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
2943 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
2949 loc.col_idx = MONO_IMPLMAP_MEMBER;
2950 loc.idx = ((method_idx + 1) << MEMBERFORWD_BITS) | MEMBERFORWD_METHODDEF;
2952 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2955 return loc.result + 1;
2959 * @image: context where the image is created
2960 * @type_spec: typespec token
2962 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
2966 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
2968 guint32 idx = mono_metadata_token_index (type_spec);
2970 guint32 cols [MONO_TYPESPEC_SIZE];
2975 mono_loader_lock ();
2977 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
2978 mono_loader_unlock ();
2982 t = &image->tables [MONO_TABLE_TYPESPEC];
2984 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
2985 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
2986 len = mono_metadata_decode_value (ptr, &ptr);
2988 type = g_new0 (MonoType, 1);
2990 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
2992 if (*ptr == MONO_TYPE_BYREF) {
2997 do_mono_metadata_parse_type (type, image, ptr, &ptr);
2999 mono_loader_unlock ();
3005 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3007 MonoMarshalSpec *res;
3009 const char *start = ptr;
3011 /* fixme: this is incomplete, but I cant find more infos in the specs */
3013 res = g_new0 (MonoMarshalSpec, 1);
3015 len = mono_metadata_decode_value (ptr, &ptr);
3016 res->native = *ptr++;
3018 if (res->native == MONO_NATIVE_LPARRAY) {
3019 if (ptr - start <= len)
3020 res->data.array_data.elem_type = *ptr++;
3021 if (ptr - start <= len)
3022 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3023 if (ptr - start <= len)
3024 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3027 if (res->native == MONO_NATIVE_BYVALTSTR) {
3028 if (ptr - start <= len)
3029 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3032 if (res->native == MONO_NATIVE_BYVALARRAY) {
3033 if (ptr - start <= len)
3034 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3037 if (res->native == MONO_NATIVE_CUSTOM) {
3038 /* skip unused type guid */
3039 len = mono_metadata_decode_value (ptr, &ptr);
3041 /* skip unused native type name */
3042 len = mono_metadata_decode_value (ptr, &ptr);
3044 /* read custom marshaler type name */
3045 len = mono_metadata_decode_value (ptr, &ptr);
3046 res->data.custom_data.custom_name = g_strndup (ptr, len);
3048 /* read cookie string */
3049 len = mono_metadata_decode_value (ptr, &ptr);
3050 res->data.custom_data.cookie = g_strndup (ptr, len);
3057 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3058 gboolean unicode, MonoMarshalConv *conv)
3060 MonoMarshalConv dummy_conv;
3066 *conv = MONO_MARSHAL_CONV_NONE;
3069 return MONO_NATIVE_UINT;
3073 case MONO_TYPE_BOOLEAN:
3075 switch (mspec->native) {
3076 case MONO_NATIVE_VARIANTBOOL:
3077 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3078 return MONO_NATIVE_VARIANTBOOL;
3079 case MONO_NATIVE_BOOLEAN:
3080 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3081 return MONO_NATIVE_BOOLEAN;
3082 case MONO_NATIVE_I1:
3083 case MONO_NATIVE_U1:
3084 return mspec->native;
3086 g_error ("cant marshal bool to native type %02x", mspec->native);
3089 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3090 return MONO_NATIVE_BOOLEAN;
3091 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3092 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3093 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3094 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3095 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3096 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3097 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3098 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3099 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3100 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3101 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3102 case MONO_TYPE_STRING:
3104 switch (mspec->native) {
3105 case MONO_NATIVE_BSTR:
3106 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3107 return MONO_NATIVE_BSTR;
3108 case MONO_NATIVE_LPSTR:
3109 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3110 return MONO_NATIVE_LPSTR;
3111 case MONO_NATIVE_LPWSTR:
3112 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3113 return MONO_NATIVE_LPWSTR;
3114 case MONO_NATIVE_LPTSTR:
3115 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3116 return MONO_NATIVE_LPTSTR;
3117 case MONO_NATIVE_ANSIBSTR:
3118 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3119 return MONO_NATIVE_ANSIBSTR;
3120 case MONO_NATIVE_TBSTR:
3121 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3122 return MONO_NATIVE_TBSTR;
3123 case MONO_NATIVE_BYVALTSTR:
3125 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3127 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3128 return MONO_NATIVE_BYVALTSTR;
3130 g_error ("cant marshal string to native type %02x", mspec->native);
3133 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3134 return MONO_NATIVE_LPTSTR;
3135 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3136 case MONO_TYPE_VALUETYPE: /*FIXME*/
3137 if (type->data.klass->enumtype) {
3138 t = type->data.klass->enum_basetype->type;
3141 return MONO_NATIVE_STRUCT;
3142 case MONO_TYPE_SZARRAY:
3143 case MONO_TYPE_ARRAY:
3145 switch (mspec->native) {
3146 case MONO_NATIVE_BYVALARRAY:
3147 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3148 return MONO_NATIVE_BYVALARRAY;
3149 case MONO_NATIVE_SAFEARRAY:
3150 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3151 return MONO_NATIVE_SAFEARRAY;
3152 case MONO_NATIVE_LPARRAY:
3153 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3154 return MONO_NATIVE_LPARRAY;
3156 g_error ("cant marshal array as native type %02x", mspec->native);
3160 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3161 return MONO_NATIVE_LPARRAY;
3162 case MONO_TYPE_I: return MONO_NATIVE_INT;
3163 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3164 case MONO_TYPE_CLASS:
3165 case MONO_TYPE_OBJECT: {
3166 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3168 switch (mspec->native) {
3169 case MONO_NATIVE_STRUCT:
3170 return MONO_NATIVE_STRUCT;
3171 case MONO_NATIVE_INTERFACE:
3172 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3173 return MONO_NATIVE_INTERFACE;
3174 case MONO_NATIVE_IDISPATCH:
3175 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3176 return MONO_NATIVE_IDISPATCH;
3177 case MONO_NATIVE_IUNKNOWN:
3178 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3179 return MONO_NATIVE_IUNKNOWN;
3180 case MONO_NATIVE_FUNC:
3181 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3182 type->data.klass == mono_defaults.delegate_class ||
3183 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3184 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3185 return MONO_NATIVE_FUNC;
3191 g_error ("cant marshal object as native type %02x", mspec->native);
3194 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3195 type->data.klass == mono_defaults.delegate_class ||
3196 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3197 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3198 return MONO_NATIVE_FUNC;
3200 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3201 return MONO_NATIVE_STRUCT;
3203 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3204 case MONO_TYPE_TYPEDBYREF:
3206 g_error ("type 0x%02x not handled in marshal", t);
3208 return MONO_NATIVE_MAX;
3212 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3215 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3221 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3222 loc.idx = ((idx + 1) << HAS_FIELD_MARSHAL_BITS) | (is_field? HAS_FIELD_MARSHAL_FIELDSREF: HAS_FIELD_MARSHAL_PARAMDEF);
3224 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3227 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3231 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3233 guint32 idx = tok >> METHODDEFORREF_BITS;
3234 switch (tok & METHODDEFORREF_MASK) {
3235 case METHODDEFORREF_METHODDEF:
3236 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3237 case METHODDEFORREF_METHODREF:
3238 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3240 g_assert_not_reached ();
3245 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3248 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3251 guint32 cols [MONO_METHODIMPL_SIZE];
3252 MonoMethod **result;
3261 loc.col_idx = MONO_METHODIMPL_CLASS;
3262 loc.idx = mono_metadata_token_index (type_token);
3264 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3270 * We may end up in the middle of the rows...
3273 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3278 while (end < tdef->rows) {
3279 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3285 result = g_new (MonoMethod*, num * 2);
3286 for (i = 0; i < num; ++i) {
3287 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3288 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3289 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3293 *num_overrides = num;
3298 * mono_guid_to_string:
3300 * Converts a 16 byte Microsoft GUID to the standard string representation.
3303 mono_guid_to_string (const guint8 *guid)
3305 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3306 guid[3], guid[2], guid[1], guid[0],
3310 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3314 get_constraints (MonoImage *image, int owner)
3316 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3317 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3318 guint32 i, token, found;
3319 MonoClass *klass, **res;
3320 GList *cons = NULL, *tmp;
3324 for (i = 0; i < tdef->rows; ++i) {
3325 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3326 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3327 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3328 klass = mono_class_get (image, token);
3329 cons = g_list_append (cons, klass);
3332 /* contiguous list finished */
3339 res = g_new0 (MonoClass*, found + 1);
3340 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3341 res [i] = tmp->data;
3348 mono_metadata_load_generic_params (MonoImage *image, guint32 token, guint32 *num)
3350 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3351 guint32 cols [MONO_GENERICPARAM_SIZE];
3352 guint32 i, owner, last_num, n;
3353 MonoGenericParam *params;
3355 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3356 owner = MONO_TYPEORMETHOD_TYPE;
3357 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3358 owner = MONO_TYPEORMETHOD_METHOD;
3360 g_error ("wrong token %x to load_generics_params", token);
3362 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3368 for (i = 0; i < tdef->rows; ++i) {
3369 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3370 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3374 if (i >= tdef->rows)
3380 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3381 params [n - 1].pklass = NULL;
3382 params [n - 1].method = NULL;
3383 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3384 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3385 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3386 params [n - 1].constraints = get_constraints (image, i + 1);
3387 if (++i >= tdef->rows)
3389 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3390 } while (cols [MONO_GENERICPARAM_OWNER] == owner);