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);
1411 if (def && (method->call_convention == MONO_CALL_VARARG))
1412 method->sentinelpos = method->param_count;
1419 * Add signature to a cache and increase ref count...
1425 * mono_metadata_free_method_signature:
1426 * @sig: signature to destroy
1428 * Free the memory allocated in the signature @sig.
1431 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1434 mono_metadata_free_type (sig->ret);
1435 for (i = 0; i < sig->param_count; ++i)
1436 mono_metadata_free_type (sig->params [i]);
1442 do_mono_metadata_parse_generic_inst (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1444 MonoGenericInst *ginst = g_new0 (MonoGenericInst, 1);
1448 type->data.generic_inst = ginst;
1450 ginst->context = g_new0 (MonoGenericContext, 1);
1451 ginst->context->ginst = ginst;
1453 ginst->klass = g_new0 (MonoClass, 1);
1455 ginst->generic_type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1456 ginst->type_argc = count = mono_metadata_decode_value (ptr, &ptr);
1457 ginst->type_argv = g_new0 (MonoType*, count);
1460 * Create the klass before parsing the type arguments.
1461 * This is required to support "recursive" definitions.
1462 * See mcs/tests/gen-23.cs for an example.
1465 ginst->init_pending = TRUE;
1467 mono_class_create_generic (ginst);
1469 for (i = 0; i < ginst->type_argc; i++) {
1470 MonoType *t = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1472 ginst->type_argv [i] = t;
1473 if (!ginst->is_open)
1474 ginst->is_open = mono_class_is_open_constructed_type (t);
1482 * We may be called multiple times on different metadata to create the same
1483 * instantiated type. This happens for instance if we're part of a method or
1484 * local variable signature.
1486 * It's important to return the same MonoGenericInst * for each particualar
1487 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1490 * According to the spec ($26.1.5), a static variable in a generic class
1491 * declaration is shared amongst all instances of the same closed constructed
1495 cached = g_hash_table_lookup (m->generic_inst_cache, ginst);
1497 g_free (ginst->klass);
1498 g_free (ginst->type_argv);
1501 type->data.generic_inst = cached->data.generic_inst;
1504 cached = g_new0 (MonoType, 1);
1505 cached->type = MONO_TYPE_GENERICINST;
1506 cached->data.generic_inst = ginst;
1508 g_hash_table_insert (m->generic_inst_cache, ginst, cached);
1510 mono_stats.generic_instance_count++;
1511 mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
1512 sizeof (MonoGenericContext) +
1513 ginst->type_argc * sizeof (MonoType);
1516 ginst->init_pending = FALSE;
1519 static MonoGenericParam *
1520 mono_metadata_parse_generic_param (MonoImage *m, const char *ptr, const char **rptr)
1522 MonoGenericParam *generic_param = g_new0 (MonoGenericParam, 1);
1524 generic_param->num = mono_metadata_decode_value (ptr, &ptr);
1529 return generic_param;
1533 * do_mono_metadata_parse_type:
1534 * @type: MonoType to be filled in with the return value
1536 * Internal routine used to "fill" the contents of @type from an
1537 * allocated pointer. This is done this way to avoid doing too
1538 * many mini-allocations (particularly for the MonoFieldType which
1539 * most of the time is just a MonoType, but sometimes might be augmented).
1541 * This routine is used by mono_metadata_parse_type and
1542 * mono_metadata_parse_field_type
1544 * This extracts a Type as specified in Partition II (22.2.12)
1547 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1549 type->type = mono_metadata_decode_value (ptr, &ptr);
1551 switch (type->type){
1552 case MONO_TYPE_VOID:
1553 case MONO_TYPE_BOOLEAN:
1554 case MONO_TYPE_CHAR:
1567 case MONO_TYPE_STRING:
1568 case MONO_TYPE_OBJECT:
1569 case MONO_TYPE_TYPEDBYREF:
1571 case MONO_TYPE_VALUETYPE:
1572 case MONO_TYPE_CLASS: {
1574 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1575 type->data.klass = mono_class_get (m, token);
1578 case MONO_TYPE_SZARRAY: {
1579 MonoType *etype = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1580 type->data.klass = mono_class_from_mono_type (etype);
1584 type->data.type = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1586 case MONO_TYPE_FNPTR:
1587 type->data.method = mono_metadata_parse_method_signature (m, 0, ptr, &ptr);
1589 case MONO_TYPE_ARRAY:
1590 type->data.array = mono_metadata_parse_array (m, ptr, &ptr);
1593 case MONO_TYPE_MVAR:
1595 type->data.generic_param = mono_metadata_parse_generic_param (m, ptr, &ptr);
1598 case MONO_TYPE_GENERICINST:
1599 do_mono_metadata_parse_generic_inst (type, m, ptr, &ptr);
1603 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1611 * mono_metadata_free_type:
1612 * @type: type to free
1614 * Free the memory allocated for type @type.
1617 mono_metadata_free_type (MonoType *type)
1619 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1621 switch (type->type){
1623 mono_metadata_free_type (type->data.type);
1625 case MONO_TYPE_FNPTR:
1626 mono_metadata_free_method_signature (type->data.method);
1628 case MONO_TYPE_ARRAY:
1629 mono_metadata_free_array (type->data.array);
1637 hex_dump (const char *buffer, int base, int count)
1639 int show_header = 1;
1647 for (i = 0; i < count; i++){
1650 printf ("\n0x%08x: ", (unsigned char) base + i);
1652 printf ("%02x ", (unsigned char) (buffer [i]));
1659 * @mh: The Method header
1660 * @ptr: Points to the beginning of the Section Data (25.3)
1663 parse_section_data (MonoMethodHeader *mh, const unsigned char *ptr)
1665 unsigned char sect_data_flags;
1666 const unsigned char *sptr;
1668 guint32 sect_data_len;
1671 /* align on 32-bit boundary */
1672 /* FIXME: not 64-bit clean code */
1673 sptr = ptr = dword_align (ptr);
1674 sect_data_flags = *ptr;
1677 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1679 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1682 sect_data_len = ptr [0];
1686 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1687 hex_dump (sptr, 0, sect_data_len+8);
1688 g_print ("\nheader: ");
1689 hex_dump (sptr-4, 0, 4);
1693 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1694 const unsigned char *p = dword_align (ptr);
1696 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1697 /* we could just store a pointer if we don't need to byteswap */
1698 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1699 for (i = 0; i < mh->num_clauses; ++i) {
1700 MonoExceptionClause *ec = &mh->clauses [i];
1703 /* we could memcpy and byteswap */
1704 ec->flags = read32 (p);
1706 ec->try_offset = read32 (p);
1708 ec->try_len = read32 (p);
1710 ec->handler_offset = read32 (p);
1712 ec->handler_len = read32 (p);
1714 ec->token_or_filter = read32 (p);
1717 ec->flags = read16 (p);
1719 ec->try_offset = read16 (p);
1723 ec->handler_offset = read16 (p);
1725 ec->handler_len = *p;
1727 ec->token_or_filter = read32 (p);
1730 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
1734 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
1735 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
1742 * mono_metadata_parse_mh:
1743 * @m: metadata context
1744 * @ptr: pointer to the method header.
1746 * Decode the method header at @ptr, including pointer to the IL code,
1747 * info about local variables and optional exception tables.
1749 * Returns: a MonoMethodHeader.
1752 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
1754 MonoMethodHeader *mh;
1755 unsigned char flags = *(const unsigned char *) ptr;
1756 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
1758 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
1759 const unsigned char *code;
1762 g_return_val_if_fail (ptr != NULL, NULL);
1765 case METHOD_HEADER_TINY_FORMAT:
1766 mh = g_new0 (MonoMethodHeader, 1);
1769 local_var_sig_tok = 0;
1770 mh->code_size = flags >> 2;
1773 case METHOD_HEADER_TINY_FORMAT1:
1774 mh = g_new0 (MonoMethodHeader, 1);
1777 local_var_sig_tok = 0;
1780 * The spec claims 3 bits, but the Beta2 is
1783 mh->code_size = flags >> 2;
1786 case METHOD_HEADER_FAT_FORMAT:
1787 fat_flags = read16 (ptr);
1789 hsize = (fat_flags >> 12) & 0xf;
1790 max_stack = read16 (ptr);
1792 code_size = read32 (ptr);
1794 local_var_sig_tok = read32 (ptr);
1797 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
1804 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
1808 * There are more sections
1810 ptr = code + code_size;
1818 if (local_var_sig_tok) {
1819 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1820 const char *locals_ptr;
1821 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1822 int len=0, i, bsize;
1824 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
1825 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1826 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1827 if (*locals_ptr != 0x07)
1828 g_warning ("wrong signature for locals blob");
1830 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
1831 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1832 mh->num_locals = len;
1833 for (i = 0; i < len; ++i)
1834 mh->locals [i] = mono_metadata_parse_type (m, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
1836 mh = g_new0 (MonoMethodHeader, 1);
1839 mh->code_size = code_size;
1840 mh->max_stack = max_stack;
1841 mh->init_locals = init_locals;
1842 if (fat_flags & METHOD_HEADER_MORE_SECTS)
1843 parse_section_data (mh, (const unsigned char*)ptr);
1848 * mono_metadata_free_mh:
1849 * @mh: a method header
1851 * Free the memory allocated for the method header.
1854 mono_metadata_free_mh (MonoMethodHeader *mh)
1857 for (i = 0; i < mh->num_locals; ++i)
1858 mono_metadata_free_type (mh->locals[i]);
1859 g_free (mh->clauses);
1864 * mono_metadata_parse_field_type:
1865 * @m: metadata context to extract information from
1866 * @ptr: pointer to the field signature
1867 * @rptr: pointer updated to match the end of the decoded stream
1869 * Parses the field signature, and returns the type information for it.
1871 * Returns: The MonoType that was extracted from @ptr.
1874 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
1876 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
1880 * mono_metadata_parse_param:
1881 * @m: metadata context to extract information from
1882 * @ptr: pointer to the param signature
1883 * @rptr: pointer updated to match the end of the decoded stream
1885 * Parses the param signature, and returns the type information for it.
1887 * Returns: The MonoType that was extracted from @ptr.
1890 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
1892 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
1896 * mono_metadata_token_from_dor:
1897 * @dor_token: A TypeDefOrRef coded index
1899 * dor_token is a TypeDefOrRef coded index: it contains either
1900 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
1901 * bits contain an index into the table.
1903 * Returns: an expanded token
1906 mono_metadata_token_from_dor (guint32 dor_index)
1910 table = dor_index & 0x03;
1911 idx = dor_index >> 2;
1914 case 0: /* TypeDef */
1915 return MONO_TOKEN_TYPE_DEF | idx;
1917 case 1: /* TypeRef */
1918 return MONO_TOKEN_TYPE_REF | idx;
1920 case 2: /* TypeSpec */
1921 return MONO_TOKEN_TYPE_SPEC | idx;
1924 g_assert_not_reached ();
1931 * We use this to pass context information to the row locator
1934 int idx; /* The index that we are trying to locate */
1935 int col_idx; /* The index in the row where idx may be stored */
1936 MonoTableInfo *t; /* pointer to the table */
1941 * How the row locator works.
1946 * ___|___------> _______
1949 * A column in the rows of table A references an index in table B.
1950 * For example A may be the TYPEDEF table and B the METHODDEF table.
1952 * Given an index in table B we want to get the row in table A
1953 * where the column n references our index in B.
1955 * In the locator_t structure:
1957 * col_idx is the column number
1958 * index is the index in table B
1959 * result will be the index in table A
1962 * Table A Table B column (in table A)
1963 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
1964 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
1965 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
1966 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
1967 * METHODSEM PROPERTY ASSOCIATION (encoded index)
1969 * Note that we still don't support encoded indexes.
1973 typedef_locator (const void *a, const void *b)
1975 locator_t *loc = (locator_t *) a;
1976 const char *bb = (const char *) b;
1977 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
1978 guint32 col, col_next;
1980 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
1986 * Need to check that the next row is valid.
1988 if (typedef_index + 1 < loc->t->rows) {
1989 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
1990 if (loc->idx >= col_next)
1993 if (col == col_next)
1997 loc->result = typedef_index;
2003 table_locator (const void *a, const void *b)
2005 locator_t *loc = (locator_t *) a;
2006 const char *bb = (const char *) b;
2007 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2010 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2012 if (loc->idx == col) {
2013 loc->result = table_index;
2023 * mono_metadata_typedef_from_field:
2024 * @meta: metadata context
2025 * @index: FieldDef token
2027 * Returns the 1-based index into the TypeDef table of the type that
2028 * declared the field described by @index.
2029 * Returns 0 if not found.
2032 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2034 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2040 loc.idx = mono_metadata_token_index (index);
2041 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2044 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2045 g_assert_not_reached ();
2047 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2048 return loc.result + 1;
2052 * mono_metadata_typedef_from_method:
2053 * @meta: metadata context
2054 * @index: MethodDef token
2056 * Returns the 1-based index into the TypeDef table of the type that
2057 * declared the method described by @index.
2058 * Returns 0 if not found.
2061 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2063 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2069 loc.idx = mono_metadata_token_index (index);
2070 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2073 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2074 g_assert_not_reached ();
2076 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2077 return loc.result + 1;
2081 * mono_metadata_interfaces_from_typedef:
2082 * @meta: metadata context
2083 * @index: typedef token
2085 * Returns and array of interfaces that the @index typedef token implements.
2086 * The number of elemnts in the array is returned in @count.
2089 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2091 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2094 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2102 loc.idx = mono_metadata_token_index (index);
2103 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2106 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2111 * We may end up in the middle of the rows...
2114 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2121 while (start < tdef->rows) {
2122 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2123 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2125 result = g_renew (MonoClass*, result, i + 1);
2126 result [i] = mono_class_get (meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]));
2134 * mono_metadata_nested_in_typedef:
2135 * @meta: metadata context
2136 * @index: typedef token
2138 * Returns: the 1-based index into the TypeDef table of the type
2139 * where the type described by @index is nested.
2140 * Retruns 0 if @index describes a non-nested type.
2143 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2145 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2151 loc.idx = mono_metadata_token_index (index);
2152 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2155 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2158 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2159 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2163 * mono_metadata_nesting_typedef:
2164 * @meta: metadata context
2165 * @index: typedef token
2167 * Returns: the 1-based index into the TypeDef table of the first type
2168 * that is nested inside the type described by @index. The search starts at
2170 * Returns 0 if no such type is found.
2173 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2175 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2177 guint32 class_index = mono_metadata_token_index (index);
2182 start = start_index;
2184 while (start <= tdef->rows) {
2185 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2191 if (start > tdef->rows)
2198 * mono_metadata_packing_from_typedef:
2199 * @meta: metadata context
2200 * @index: token representing a type
2202 * Returns the info stored in the ClassLAyout table for the given typedef token
2203 * into the @packing and @size pointers.
2204 * Returns 0 if the info is not found.
2207 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2209 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2211 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2216 loc.idx = mono_metadata_token_index (index);
2217 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2220 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2223 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2225 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2227 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2229 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2230 return loc.result + 1;
2234 * mono_metadata_custom_attrs_from_index:
2235 * @meta: metadata context
2236 * @index: token representing the parent
2238 * Returns: the 1-based index into the CustomAttribute table of the first
2239 * attribute which belongs to the metadata object described by @index.
2240 * Returns 0 if no such attribute is found.
2243 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2245 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2252 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2255 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2258 /* Find the first entry by searching backwards */
2259 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2262 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2263 return loc.result + 1;
2268 mono_backtrace (int limit)
2273 backtrace (array, limit);
2274 names = backtrace_symbols (array, limit);
2275 for (i =0; i < limit; ++i) {
2276 g_print ("\t%s\n", names [i]);
2283 /*#define __alignof__(a) sizeof(a)*/
2284 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2289 * @t: the type to return the size of
2291 * Returns: the number of bytes required to hold an instance of this
2295 mono_type_size (MonoType *t, gint *align)
2302 *align = __alignof__(gpointer);
2303 return sizeof (gpointer);
2307 case MONO_TYPE_VOID:
2310 case MONO_TYPE_BOOLEAN:
2311 *align = __alignof__(gint8);
2315 *align = __alignof__(gint8);
2317 case MONO_TYPE_CHAR:
2320 *align = __alignof__(gint16);
2324 *align = __alignof__(gint32);
2327 *align = __alignof__(float);
2331 *align = __alignof__(gint64);
2334 *align = __alignof__(double);
2338 *align = __alignof__(gpointer);
2339 return sizeof (gpointer);
2340 case MONO_TYPE_STRING:
2341 *align = __alignof__(gpointer);
2342 return sizeof (gpointer);
2343 case MONO_TYPE_OBJECT:
2344 *align = __alignof__(gpointer);
2345 return sizeof (gpointer);
2346 case MONO_TYPE_VALUETYPE: {
2347 if (t->data.klass->enumtype)
2348 return mono_type_size (t->data.klass->enum_basetype, align);
2350 return mono_class_value_size (t->data.klass, align);
2352 case MONO_TYPE_CLASS:
2353 case MONO_TYPE_SZARRAY:
2355 case MONO_TYPE_FNPTR:
2356 case MONO_TYPE_ARRAY:
2357 *align = __alignof__(gpointer);
2358 return sizeof (gpointer);
2359 case MONO_TYPE_TYPEDBYREF:
2360 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2361 case MONO_TYPE_GENERICINST: {
2362 MonoGenericInst *ginst = t->data.generic_inst;
2363 MonoClass *iclass = mono_class_from_mono_type (ginst->generic_type);
2364 return mono_type_size (&iclass->byval_arg, align);
2367 case MONO_TYPE_MVAR:
2368 *align = __alignof__(gpointer);
2369 return sizeof (gpointer);
2371 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2377 * mono_type_stack_size:
2378 * @t: the type to return the size it uses on the stack
2380 * Returns: the number of bytes required to hold an instance of this
2381 * type on the runtime stack
2384 mono_type_stack_size (MonoType *t, gint *align)
2388 g_assert (t != NULL);
2394 *align = __alignof__(gpointer);
2395 return sizeof (gpointer);
2399 case MONO_TYPE_BOOLEAN:
2400 case MONO_TYPE_CHAR:
2409 case MONO_TYPE_STRING:
2410 case MONO_TYPE_OBJECT:
2411 case MONO_TYPE_CLASS:
2412 case MONO_TYPE_SZARRAY:
2414 case MONO_TYPE_FNPTR:
2415 case MONO_TYPE_ARRAY:
2416 *align = __alignof__(gpointer);
2417 return sizeof (gpointer);
2418 case MONO_TYPE_TYPEDBYREF:
2419 *align = __alignof__(gpointer);
2420 return sizeof (gpointer) * 3;
2422 *align = __alignof__(float);
2423 return sizeof (float);
2426 *align = __alignof__(gint64);
2427 return sizeof (gint64);
2429 *align = __alignof__(double);
2430 return sizeof (double);
2431 case MONO_TYPE_VALUETYPE: {
2434 if (t->data.klass->enumtype)
2435 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2437 size = mono_class_value_size (t->data.klass, align);
2439 *align = *align + __alignof__(gpointer) - 1;
2440 *align &= ~(__alignof__(gpointer) - 1);
2442 size += sizeof (gpointer) - 1;
2443 size &= ~(sizeof (gpointer) - 1);
2448 case MONO_TYPE_GENERICINST: {
2449 MonoGenericInst *ginst = t->data.generic_inst;
2450 MonoClass *iclass = mono_class_from_mono_type (ginst->generic_type);
2451 return mono_type_stack_size (&iclass->byval_arg, align);
2454 g_error ("type 0x%02x unknown", t->type);
2460 mono_metadata_generic_inst_hash (MonoGenericInst *ginst)
2462 return mono_metadata_type_hash (ginst->generic_type);
2466 mono_metadata_generic_inst_equal (MonoGenericInst *g1, MonoGenericInst *g2)
2470 if (g1->type_argc != g2->type_argc)
2472 if (!mono_metadata_type_equal (g1->generic_type, g2->generic_type))
2474 for (i = 0; i < g1->type_argc; ++i) {
2475 if (!mono_metadata_type_equal (g1->type_argv [i], g2->type_argv [i]))
2482 * mono_metadata_type_hash:
2485 * Computes an hash value for @t1 to be used in GHashTable.
2488 mono_metadata_type_hash (MonoType *t1)
2490 guint hash = t1->type;
2492 hash |= t1->byref << 6; /* do not collide with t1->type values */
2494 case MONO_TYPE_VALUETYPE:
2495 case MONO_TYPE_CLASS:
2496 case MONO_TYPE_SZARRAY:
2497 /* check if the distribution is good enough */
2498 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2500 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2501 case MONO_TYPE_ARRAY:
2502 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2503 case MONO_TYPE_GENERICINST:
2504 return ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (t1->data.generic_inst);
2510 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2)
2514 if (c1->generic_inst && c2->generic_inst)
2515 return mono_metadata_generic_inst_equal (c1->generic_inst, c2->generic_inst);
2516 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
2517 return c1->byval_arg.data.generic_param->num == c2->byval_arg.data.generic_param->num;
2518 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
2519 return c1->byval_arg.data.generic_param->num == c2->byval_arg.data.generic_param->num;
2524 * mono_metadata_type_equal:
2528 * Determine if @t1 and @t2 represent the same type.
2529 * Returns: #TRUE if @t1 and @t2 are equal.
2532 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2534 if (t1->type != t2->type ||
2535 t1->byref != t2->byref)
2539 case MONO_TYPE_VOID:
2540 case MONO_TYPE_BOOLEAN:
2541 case MONO_TYPE_CHAR:
2552 case MONO_TYPE_STRING:
2555 case MONO_TYPE_OBJECT:
2556 case MONO_TYPE_TYPEDBYREF:
2558 case MONO_TYPE_VALUETYPE:
2559 case MONO_TYPE_CLASS:
2560 case MONO_TYPE_SZARRAY:
2561 return mono_metadata_class_equal (t1->data.klass, t2->data.klass);
2563 return mono_metadata_type_equal (t1->data.type, t2->data.type);
2564 case MONO_TYPE_ARRAY:
2565 if (t1->data.array->rank != t2->data.array->rank)
2567 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass);
2568 case MONO_TYPE_GENERICINST:
2569 return mono_metadata_generic_inst_equal (t1->data.generic_inst,
2570 t2->data.generic_inst);
2572 case MONO_TYPE_MVAR:
2573 return t1->data.generic_param->num == t2->data.generic_param->num;
2575 g_error ("implement type compare for %0x!", t1->type);
2583 * mono_metadata_signature_equal:
2584 * @sig1: a signature
2585 * @sig2: another signature
2587 * Determine if @sig1 and @sig2 represent the same signature, with the
2588 * same number of arguments and the same types.
2589 * Returns: #TRUE if @sig1 and @sig2 are equal.
2592 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2596 if (sig1->hasthis != sig2->hasthis ||
2597 sig1->param_count != sig2->param_count)
2600 for (i = 0; i < sig1->param_count; i++) {
2601 MonoType *p1 = sig1->params[i];
2602 MonoType *p2 = sig2->params[i];
2604 //if (p1->attrs != p2->attrs)
2607 if (!mono_metadata_type_equal (p1, p2))
2611 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
2617 mono_signature_hash (MonoMethodSignature *sig)
2619 guint i, res = sig->ret->type;
2621 for (i = 0; i < sig->param_count; i++)
2622 res = (res << 5) - res + sig->params[i]->type;
2628 * mono_metadata_encode_value:
2629 * @value: value to encode
2630 * @buf: buffer where to write the compressed representation
2631 * @endbuf: pointer updated to point at the end of the encoded output
2633 * Encodes the value @value in the compressed representation used
2634 * in metadata and stores the result in @buf. @buf needs to be big
2635 * enough to hold the data (4 bytes).
2638 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
2644 else if (value < 0x4000) {
2645 p [0] = 0x80 | (value >> 8);
2646 p [1] = value & 0xff;
2649 p [0] = (value >> 24) | 0xc0;
2650 p [1] = (value >> 16) & 0xff;
2651 p [2] = (value >> 8) & 0xff;
2652 p [3] = value & 0xff;
2660 * mono_metadata_field_info:
2661 * @meta: the Image the field is defined in
2662 * @index: the index in the field table representing the field
2663 * @offset: a pointer to an integer where to store the offset that
2664 * may have been specified for the field in a FieldLayout table
2665 * @rva: a pointer to the RVA of the field data in the image that
2666 * may have been defined in a FieldRVA table
2667 * @marshal_spec: a pointer to the marshal spec that may have been
2668 * defined for the field in a FieldMarshal table.
2670 * Gather info for field @index that may have been defined in the FieldLayout,
2671 * FieldRVA and FieldMarshal tables.
2672 * Either of offset, rva and marshal_spec can be NULL if you're not interested
2676 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
2677 MonoMarshalSpec **marshal_spec)
2679 MonoTableInfo *tdef;
2682 loc.idx = index + 1;
2684 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
2686 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
2689 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2690 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
2692 *offset = (guint32)-1;
2696 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
2698 loc.col_idx = MONO_FIELD_RVA_FIELD;
2701 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2703 * LAMESPEC: There is no signature, no nothing, just the raw data.
2705 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
2713 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
2714 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
2721 * mono_metadata_get_constant_index:
2722 * @meta: the Image the field is defined in
2723 * @index: the token that may have a row defined in the constants table
2724 * @hint: possible position for the row
2726 * @token must be a FieldDef, ParamDef or PropertyDef token.
2728 * Returns: the index into the Constants table or 0 if not found.
2731 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
2733 MonoTableInfo *tdef;
2735 guint32 index = mono_metadata_token_index (token);
2737 tdef = &meta->tables [MONO_TABLE_CONSTANT];
2738 index <<= HASCONSTANT_BITS;
2739 switch (mono_metadata_token_table (token)) {
2740 case MONO_TABLE_FIELD:
2741 index |= HASCONSTANT_FIEDDEF;
2743 case MONO_TABLE_PARAM:
2744 index |= HASCONSTANT_PARAM;
2746 case MONO_TABLE_PROPERTY:
2747 index |= HASCONSTANT_PROPERTY;
2750 g_warning ("Not a valid token for the constant table: 0x%08x", token);
2754 loc.col_idx = MONO_CONSTANT_PARENT;
2757 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
2760 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2761 return loc.result + 1;
2767 * mono_metadata_events_from_typedef:
2768 * @meta: metadata context
2769 * @index: 0-based index (in the TypeDef table) describing a type
2771 * Returns: the 0-based index in the Event table for the events in the
2772 * type. The last event that belongs to the type (plus 1) is stored
2773 * in the @end_idx pointer.
2776 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2780 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
2788 loc.col_idx = MONO_EVENT_MAP_PARENT;
2789 loc.idx = index + 1;
2791 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2794 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
2795 if (loc.result + 1 < tdef->rows) {
2796 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
2798 end = meta->tables [MONO_TABLE_EVENT].rows;
2806 * mono_metadata_methods_from_event:
2807 * @meta: metadata context
2808 * @index: 0-based index (in the Event table) describing a event
2810 * Returns: the 0-based index in the MethodDef table for the methods in the
2811 * event. The last method that belongs to the event (plus 1) is stored
2812 * in the @end_idx pointer.
2815 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
2819 guint32 cols [MONO_METHOD_SEMA_SIZE];
2820 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2827 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2828 loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_EVENT; /* Method association coded index */
2830 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2835 * We may end up in the middle of the rows...
2838 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2844 while (end < msemt->rows) {
2845 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2846 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2855 * mono_metadata_properties_from_typedef:
2856 * @meta: metadata context
2857 * @index: 0-based index (in the TypeDef table) describing a type
2859 * Returns: the 0-based index in the Property table for the properties in the
2860 * type. The last property that belongs to the type (plus 1) is stored
2861 * in the @end_idx pointer.
2864 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2868 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
2876 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
2877 loc.idx = index + 1;
2879 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2882 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
2883 if (loc.result + 1 < tdef->rows) {
2884 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
2886 end = meta->tables [MONO_TABLE_PROPERTY].rows;
2894 * mono_metadata_methods_from_property:
2895 * @meta: metadata context
2896 * @index: 0-based index (in the PropertyDef table) describing a property
2898 * Returns: the 0-based index in the MethodDef table for the methods in the
2899 * property. The last method that belongs to the property (plus 1) is stored
2900 * in the @end_idx pointer.
2903 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
2907 guint32 cols [MONO_METHOD_SEMA_SIZE];
2908 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2915 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2916 loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_PROPERTY; /* Method association coded index */
2918 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2923 * We may end up in the middle of the rows...
2926 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2932 while (end < msemt->rows) {
2933 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2934 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2943 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
2946 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
2952 loc.col_idx = MONO_IMPLMAP_MEMBER;
2953 loc.idx = ((method_idx + 1) << MEMBERFORWD_BITS) | MEMBERFORWD_METHODDEF;
2955 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2958 return loc.result + 1;
2962 * @image: context where the image is created
2963 * @type_spec: typespec token
2965 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
2969 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
2971 guint32 idx = mono_metadata_token_index (type_spec);
2973 guint32 cols [MONO_TYPESPEC_SIZE];
2978 mono_loader_lock ();
2980 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
2981 mono_loader_unlock ();
2985 t = &image->tables [MONO_TABLE_TYPESPEC];
2987 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
2988 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
2989 len = mono_metadata_decode_value (ptr, &ptr);
2991 type = g_new0 (MonoType, 1);
2993 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
2995 if (*ptr == MONO_TYPE_BYREF) {
3000 do_mono_metadata_parse_type (type, image, ptr, &ptr);
3002 mono_loader_unlock ();
3008 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3010 MonoMarshalSpec *res;
3012 const char *start = ptr;
3014 /* fixme: this is incomplete, but I cant find more infos in the specs */
3016 res = g_new0 (MonoMarshalSpec, 1);
3018 len = mono_metadata_decode_value (ptr, &ptr);
3019 res->native = *ptr++;
3021 if (res->native == MONO_NATIVE_LPARRAY) {
3022 if (ptr - start <= len)
3023 res->data.array_data.elem_type = *ptr++;
3024 if (ptr - start <= len)
3025 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3026 if (ptr - start <= len)
3027 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3030 if (res->native == MONO_NATIVE_BYVALTSTR) {
3031 if (ptr - start <= len)
3032 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3035 if (res->native == MONO_NATIVE_BYVALARRAY) {
3036 if (ptr - start <= len)
3037 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3040 if (res->native == MONO_NATIVE_CUSTOM) {
3041 /* skip unused type guid */
3042 len = mono_metadata_decode_value (ptr, &ptr);
3044 /* skip unused native type name */
3045 len = mono_metadata_decode_value (ptr, &ptr);
3047 /* read custom marshaler type name */
3048 len = mono_metadata_decode_value (ptr, &ptr);
3049 res->data.custom_data.custom_name = g_strndup (ptr, len);
3051 /* read cookie string */
3052 len = mono_metadata_decode_value (ptr, &ptr);
3053 res->data.custom_data.cookie = g_strndup (ptr, len);
3060 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3061 gboolean unicode, MonoMarshalConv *conv)
3063 MonoMarshalConv dummy_conv;
3069 *conv = MONO_MARSHAL_CONV_NONE;
3072 return MONO_NATIVE_UINT;
3076 case MONO_TYPE_BOOLEAN:
3078 switch (mspec->native) {
3079 case MONO_NATIVE_VARIANTBOOL:
3080 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3081 return MONO_NATIVE_VARIANTBOOL;
3082 case MONO_NATIVE_BOOLEAN:
3083 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3084 return MONO_NATIVE_BOOLEAN;
3085 case MONO_NATIVE_I1:
3086 case MONO_NATIVE_U1:
3087 return mspec->native;
3089 g_error ("cant marshal bool to native type %02x", mspec->native);
3092 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3093 return MONO_NATIVE_BOOLEAN;
3094 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3095 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3096 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3097 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3098 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3099 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3100 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3101 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3102 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3103 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3104 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3105 case MONO_TYPE_STRING:
3107 switch (mspec->native) {
3108 case MONO_NATIVE_BSTR:
3109 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3110 return MONO_NATIVE_BSTR;
3111 case MONO_NATIVE_LPSTR:
3112 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3113 return MONO_NATIVE_LPSTR;
3114 case MONO_NATIVE_LPWSTR:
3115 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3116 return MONO_NATIVE_LPWSTR;
3117 case MONO_NATIVE_LPTSTR:
3118 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3119 return MONO_NATIVE_LPTSTR;
3120 case MONO_NATIVE_ANSIBSTR:
3121 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3122 return MONO_NATIVE_ANSIBSTR;
3123 case MONO_NATIVE_TBSTR:
3124 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3125 return MONO_NATIVE_TBSTR;
3126 case MONO_NATIVE_BYVALTSTR:
3128 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3130 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3131 return MONO_NATIVE_BYVALTSTR;
3133 g_error ("cant marshal string to native type %02x", mspec->native);
3136 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3137 return MONO_NATIVE_LPTSTR;
3138 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3139 case MONO_TYPE_VALUETYPE: /*FIXME*/
3140 if (type->data.klass->enumtype) {
3141 t = type->data.klass->enum_basetype->type;
3144 return MONO_NATIVE_STRUCT;
3145 case MONO_TYPE_SZARRAY:
3146 case MONO_TYPE_ARRAY:
3148 switch (mspec->native) {
3149 case MONO_NATIVE_BYVALARRAY:
3150 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3151 return MONO_NATIVE_BYVALARRAY;
3152 case MONO_NATIVE_SAFEARRAY:
3153 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3154 return MONO_NATIVE_SAFEARRAY;
3155 case MONO_NATIVE_LPARRAY:
3156 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3157 return MONO_NATIVE_LPARRAY;
3159 g_error ("cant marshal array as native type %02x", mspec->native);
3163 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3164 return MONO_NATIVE_LPARRAY;
3165 case MONO_TYPE_I: return MONO_NATIVE_INT;
3166 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3167 case MONO_TYPE_CLASS:
3168 case MONO_TYPE_OBJECT: {
3169 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3171 switch (mspec->native) {
3172 case MONO_NATIVE_STRUCT:
3173 return MONO_NATIVE_STRUCT;
3174 case MONO_NATIVE_INTERFACE:
3175 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3176 return MONO_NATIVE_INTERFACE;
3177 case MONO_NATIVE_IDISPATCH:
3178 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3179 return MONO_NATIVE_IDISPATCH;
3180 case MONO_NATIVE_IUNKNOWN:
3181 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3182 return MONO_NATIVE_IUNKNOWN;
3183 case MONO_NATIVE_FUNC:
3184 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3185 type->data.klass == mono_defaults.delegate_class ||
3186 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3187 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3188 return MONO_NATIVE_FUNC;
3194 g_error ("cant marshal object as native type %02x", mspec->native);
3197 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3198 type->data.klass == mono_defaults.delegate_class ||
3199 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3200 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3201 return MONO_NATIVE_FUNC;
3203 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3204 return MONO_NATIVE_STRUCT;
3206 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3207 case MONO_TYPE_TYPEDBYREF:
3209 g_error ("type 0x%02x not handled in marshal", t);
3211 return MONO_NATIVE_MAX;
3215 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3218 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3224 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3225 loc.idx = ((idx + 1) << HAS_FIELD_MARSHAL_BITS) | (is_field? HAS_FIELD_MARSHAL_FIELDSREF: HAS_FIELD_MARSHAL_PARAMDEF);
3227 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3230 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3234 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3236 guint32 idx = tok >> METHODDEFORREF_BITS;
3237 switch (tok & METHODDEFORREF_MASK) {
3238 case METHODDEFORREF_METHODDEF:
3239 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3240 case METHODDEFORREF_METHODREF:
3241 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3243 g_assert_not_reached ();
3248 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3251 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3254 guint32 cols [MONO_METHODIMPL_SIZE];
3255 MonoMethod **result;
3264 loc.col_idx = MONO_METHODIMPL_CLASS;
3265 loc.idx = mono_metadata_token_index (type_token);
3267 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3273 * We may end up in the middle of the rows...
3276 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3281 while (end < tdef->rows) {
3282 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3288 result = g_new (MonoMethod*, num * 2);
3289 for (i = 0; i < num; ++i) {
3290 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3291 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3292 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3296 *num_overrides = num;
3301 * mono_guid_to_string:
3303 * Converts a 16 byte Microsoft GUID to the standard string representation.
3306 mono_guid_to_string (const guint8 *guid)
3308 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3309 guid[3], guid[2], guid[1], guid[0],
3313 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3317 get_constraints (MonoImage *image, int owner)
3319 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3320 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3321 guint32 i, token, found;
3322 MonoClass *klass, **res;
3323 GList *cons = NULL, *tmp;
3327 for (i = 0; i < tdef->rows; ++i) {
3328 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3329 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3330 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3331 klass = mono_class_get (image, token);
3332 cons = g_list_append (cons, klass);
3335 /* contiguous list finished */
3342 res = g_new0 (MonoClass*, found + 1);
3343 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3344 res [i] = tmp->data;
3351 mono_metadata_load_generic_params (MonoImage *image, guint32 token, guint32 *num)
3353 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3354 guint32 cols [MONO_GENERICPARAM_SIZE];
3355 guint32 i, owner, last_num, n;
3356 MonoGenericParam *params;
3358 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3359 owner = MONO_TYPEORMETHOD_TYPE;
3360 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3361 owner = MONO_TYPEORMETHOD_METHOD;
3363 g_error ("wrong token %x to load_generics_params", token);
3365 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3371 for (i = 0; i < tdef->rows; ++i) {
3372 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3373 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3377 if (i >= tdef->rows)
3383 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3384 params [n - 1].pklass = NULL;
3385 params [n - 1].method = NULL;
3386 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3387 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3388 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3389 params [n - 1].constraints = get_constraints (image, i + 1);
3390 if (++i >= tdef->rows)
3392 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3393 } while (cols [MONO_GENERICPARAM_OWNER] == owner);