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" },
299 { MONO_MT_TABLE_IDX, "DeprecatedConstraint" },
301 { MONO_MT_END, NULL }
304 const static MonoMetaTable MethodSpecSchema [] = {
305 { MONO_MT_MDOR_IDX, "Method" },
306 { MONO_MT_BLOB_IDX, "Signature" },
307 { MONO_MT_END, NULL }
310 const static MonoMetaTable GenericParamConstraintSchema [] = {
311 { MONO_MT_TABLE_IDX, "GenericParam" },
312 { MONO_MT_TDOR_IDX, "Constraint" },
313 { MONO_MT_END, NULL }
316 const static struct {
317 const MonoMetaTable *table;
320 /* 0 */ { ModuleSchema, "Module" },
321 /* 1 */ { TypeRefSchema, "TypeRef" },
322 /* 2 */ { TypeDefSchema, "TypeDef" },
323 /* 3 */ { NULL, NULL },
324 /* 4 */ { FieldSchema, "Field" },
325 /* 5 */ { NULL, NULL },
326 /* 6 */ { MethodSchema, "Method" },
327 /* 7 */ { NULL, NULL },
328 /* 8 */ { ParamSchema, "Param" },
329 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
330 /* A */ { MemberRefSchema, "MemberRef" },
331 /* B */ { ConstantSchema, "Constant" },
332 /* C */ { CustomAttributeSchema, "CustomAttribute" },
333 /* D */ { FieldMarshalSchema, "FieldMarshal" },
334 /* E */ { DeclSecuritySchema, "DeclSecurity" },
335 /* F */ { ClassLayoutSchema, "ClassLayout" },
336 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
337 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
338 /* 12 */ { EventMapSchema, "EventMap" },
339 /* 13 */ { NULL, NULL },
340 /* 14 */ { EventSchema, "Event" },
341 /* 15 */ { PropertyMapSchema, "PropertyMap" },
342 /* 16 */ { NULL, NULL },
343 /* 17 */ { PropertySchema, "PropertyTable" },
344 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
345 /* 19 */ { MethodImplSchema, "MethodImpl" },
346 /* 1A */ { ModuleRefSchema, "ModuleRef" },
347 /* 1B */ { TypeSpecSchema, "TypeSpec" },
348 /* 1C */ { ImplMapSchema, "ImplMap" },
349 /* 1D */ { FieldRVASchema, "FieldRVA" },
350 /* 1E */ { NULL, NULL },
351 /* 1F */ { NULL, NULL },
352 /* 20 */ { AssemblySchema, "Assembly" },
353 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
354 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
355 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
356 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
357 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
358 /* 26 */ { FileSchema, "File" },
359 /* 27 */ { ExportedTypeSchema, "ExportedType" },
360 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
361 /* 29 */ { NestedClassSchema, "NestedClass" },
362 /* 2A */ { GenericParamSchema, "GenericParam" },
363 /* 2B */ { MethodSpecSchema, "MethodSpec" },
364 /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
368 * mono_meta_table_name:
369 * @table: table index
371 * Returns the name for the @table index
374 mono_meta_table_name (int table)
376 if ((table < 0) || (table > 0x2c))
379 return tables [table].name;
382 /* The guy who wrote the spec for this should not be allowed near a
385 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
386 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
387 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
388 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
389 inverse of this mapping.
392 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
393 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
395 /* Reference: Partition II - 23.2.6 */
397 * mono_metadata_compute_size:
398 * @meta: metadata context
399 * @tableindex: metadata table number
400 * @result_bitfield: pointer to guint32 where to store additional info
402 * mono_metadata_compute_size() computes the lenght in bytes of a single
403 * row in a metadata table. The size of each column is encoded in the
404 * @result_bitfield return value along with the number of columns in the table.
405 * the resulting bitfield should be handed to the mono_metadata_table_size()
406 * and mono_metadata_table_count() macros.
409 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
411 guint32 bitfield = 0;
412 int size = 0, field_size;
415 const MonoMetaTable *table = tables [tableindex].table;
417 for (i = 0; (code = table [i].code) != MONO_MT_END; i++){
420 field_size = 4; break;
423 field_size = 2; break;
426 field_size = 1; break;
428 case MONO_MT_BLOB_IDX:
429 field_size = meta->idx_blob_wide ? 4 : 2; break;
431 case MONO_MT_STRING_IDX:
432 field_size = meta->idx_string_wide ? 4 : 2; break;
434 case MONO_MT_GUID_IDX:
435 field_size = meta->idx_guid_wide ? 4 : 2; break;
437 case MONO_MT_TABLE_IDX:
438 /* Uhm, a table index can point to other tables besides the current one
439 * so, it's not correct to use the rowcount of the current table to
440 * get the size for this column - lupus
442 switch (tableindex) {
443 case MONO_TABLE_ASSEMBLYREFOS:
445 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
446 case MONO_TABLE_ASSEMBLYPROCESSOR:
448 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
449 case MONO_TABLE_CLASSLAYOUT:
451 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
452 case MONO_TABLE_EVENTMAP:
453 g_assert (i == 0 || i == 1);
454 field_size = i ? idx_size (MONO_TABLE_EVENT):
455 idx_size(MONO_TABLE_TYPEDEF);
457 case MONO_TABLE_EVENT:
459 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
460 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
462 case MONO_TABLE_EXPORTEDTYPE:
464 /* the index is in another metadata file, so it must be 4 */
465 field_size = 4; break;
466 case MONO_TABLE_FIELDLAYOUT:
468 field_size = idx_size (MONO_TABLE_FIELD); break;
469 case MONO_TABLE_FIELDRVA:
471 field_size = idx_size (MONO_TABLE_FIELD); break;
472 case MONO_TABLE_IMPLMAP:
474 field_size = idx_size (MONO_TABLE_MODULEREF); break;
475 case MONO_TABLE_INTERFACEIMPL:
477 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
478 case MONO_TABLE_METHOD:
480 field_size = idx_size (MONO_TABLE_PARAM); break;
481 case MONO_TABLE_METHODIMPL:
483 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
484 case MONO_TABLE_METHODSEMANTICS:
486 field_size = idx_size (MONO_TABLE_METHOD); break;
487 case MONO_TABLE_NESTEDCLASS:
488 g_assert (i == 0 || i == 1);
489 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
490 case MONO_TABLE_PROPERTYMAP:
491 g_assert (i == 0 || i == 1);
492 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
493 idx_size(MONO_TABLE_TYPEDEF);
495 case MONO_TABLE_TYPEDEF:
496 g_assert (i == 4 || i == 5);
497 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
498 idx_size(MONO_TABLE_METHOD);
499 case MONO_TABLE_GENERICPARAM:
500 g_assert (i == 2 || i == 4 || i == 5);
502 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
504 field_size = idx_size (MONO_TABLE_TYPEDEF);
506 field_size = idx_size (MONO_TABLE_TYPEDEF);
509 case MONO_TABLE_GENERICPARAMCONSTRAINT:
511 field_size = idx_size (MONO_TABLE_GENERICPARAM);
515 g_assert_not_reached ();
517 if (tableindex != MONO_TABLE_EXPORTEDTYPE && field_size != idx_size (tableindex))
518 g_warning ("size changed (%d to %d)", idx_size (tableindex), field_size);
523 * HasConstant: ParamDef, FieldDef, Property
525 case MONO_MT_CONST_IDX:
526 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
527 meta->tables [MONO_TABLE_FIELD].rows);
528 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
530 /* 2 bits to encode tag */
531 field_size = rtsize (n, 16-2);
535 * HasCustomAttribute: points to any table but
538 case MONO_MT_HASCAT_IDX:
540 * We believe that since the signature and
541 * permission are indexing the Blob heap,
542 * we should consider the blob size first
544 /* I'm not a believer - lupus
545 if (meta->idx_blob_wide){
550 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
551 meta->tables [MONO_TABLE_FIELD].rows);
552 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
553 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
554 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
555 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
556 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
557 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
558 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
559 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
560 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
561 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
562 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
563 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
564 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
565 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
566 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
567 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
568 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
570 /* 5 bits to encode */
571 field_size = rtsize (n, 16-5);
575 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
576 * MemberRef and String.
578 case MONO_MT_CAT_IDX:
579 /* String is a heap, if it is wide, we know the size */
581 if (meta->idx_string_wide){
586 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
587 meta->tables [MONO_TABLE_TYPEDEF].rows);
588 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
589 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
591 /* 3 bits to encode */
592 field_size = rtsize (n, 16-3);
596 * HasDeclSecurity: Typedef, MethodDef, Assembly
598 case MONO_MT_HASDEC_IDX:
599 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
600 meta->tables [MONO_TABLE_METHOD].rows);
601 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
603 /* 2 bits to encode */
604 field_size = rtsize (n, 16-2);
608 * Implementation: File, AssemblyRef, ExportedType
610 case MONO_MT_IMPL_IDX:
611 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
612 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
613 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
615 /* 2 bits to encode tag */
616 field_size = rtsize (n, 16-2);
620 * HasFieldMarshall: FieldDef, ParamDef
622 case MONO_MT_HFM_IDX:
623 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
624 meta->tables [MONO_TABLE_PARAM].rows);
626 /* 1 bit used to encode tag */
627 field_size = rtsize (n, 16-1);
631 * MemberForwarded: FieldDef, MethodDef
634 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
635 meta->tables [MONO_TABLE_METHOD].rows);
637 /* 1 bit used to encode tag */
638 field_size = rtsize (n, 16-1);
642 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
644 * It is TypeDef, _TypeRef_, TypeSpec, instead.
646 case MONO_MT_TDOR_IDX:
647 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
648 meta->tables [MONO_TABLE_TYPEREF].rows);
649 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
651 /* 2 bits to encode */
652 field_size = rtsize (n, 16-2);
656 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
658 case MONO_MT_MRP_IDX:
659 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
660 meta->tables [MONO_TABLE_TYPEREF].rows);
661 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
662 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
663 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
664 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
666 /* 3 bits to encode */
667 field_size = rtsize (n, 16 - 3);
670 case MONO_MT_MDOR_IDX:
673 * MethodDefOrRef: MethodDef, MemberRef
676 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
677 meta->tables [MONO_TABLE_MEMBERREF].rows);
679 /* 1 bit used to encode tag */
680 field_size = rtsize (n, 16-1);
684 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
687 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
688 meta->tables [MONO_TABLE_MODULEREF].rows);
689 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
690 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
692 /* 2 bits used to encode tag (ECMA spec claims 3) */
693 field_size = rtsize (n, 16 - 2);
698 * encode field size as follows (we just need to
705 bitfield |= (field_size-1) << shift;
708 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
711 *result_bitfield = (i << 24) | bitfield;
716 * mono_metadata_compute_table_bases:
717 * @meta: metadata context to compute table values
719 * Computes the table bases for the metadata structure.
720 * This is an internal function used by the image loader code.
723 mono_metadata_compute_table_bases (MonoImage *meta)
726 const char *base = meta->tables_base;
728 for (i = 0; i < 64; i++){
729 if (meta->tables [i].rows == 0)
732 meta->tables [i].row_size = mono_metadata_compute_size (
733 meta, i, &meta->tables [i].size_bitfield);
734 meta->tables [i].base = base;
735 base += meta->tables [i].rows * meta->tables [i].row_size;
740 * mono_metadata_locate:
741 * @meta: metadata context
742 * @table: table code.
743 * @idx: index of element to retrieve from @table.
745 * Returns a pointer to the @idx element in the metadata table
746 * whose code is @table.
749 mono_metadata_locate (MonoImage *meta, int table, int idx)
751 /* idx == 0 refers always to NULL */
752 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
754 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
758 * mono_metadata_locate_token:
759 * @meta: metadata context
760 * @token: metadata token
762 * Returns a pointer to the data in the metadata represented by the
766 mono_metadata_locate_token (MonoImage *meta, guint32 token)
768 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
772 * mono_metadata_get_table:
773 * @table: table to retrieve
775 * Returns the MonoMetaTable structure for table @table
777 const MonoMetaTable *
778 mono_metadata_get_table (MonoMetaTableEnum table)
782 g_return_val_if_fail ((x > 0) && (x <= MONO_TABLE_LAST), NULL);
784 return tables [table].table;
788 * mono_metadata_string_heap:
789 * @meta: metadata context
790 * @index: index into the string heap.
792 * Returns: an in-memory pointer to the @index in the string heap.
795 mono_metadata_string_heap (MonoImage *meta, guint32 index)
797 g_return_val_if_fail (index < meta->heap_strings.size, "");
798 return meta->heap_strings.data + index;
802 * mono_metadata_user_string:
803 * @meta: metadata context
804 * @index: index into the user string heap.
806 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
809 mono_metadata_user_string (MonoImage *meta, guint32 index)
811 g_return_val_if_fail (index < meta->heap_us.size, "");
812 return meta->heap_us.data + index;
816 * mono_metadata_blob_heap:
817 * @meta: metadata context
818 * @index: index into the blob.
820 * Returns: an in-memory pointer to the @index in the Blob heap.
823 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
825 g_return_val_if_fail (index < meta->heap_blob.size, "");
826 return meta->heap_blob.data + index;
830 * mono_metadata_guid_heap:
831 * @meta: metadata context
832 * @index: index into the guid heap.
834 * Returns: an in-memory pointer to the @index in the guid heap.
837 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
840 index *= 16; /* adjust for guid size and 1-based index */
841 g_return_val_if_fail (index < meta->heap_guid.size, "");
842 return meta->heap_guid.data + index;
846 dword_align (const char *ptr)
848 #if SIZEOF_VOID_P == 8
849 return (const char *) (((guint64) (ptr + 3)) & ~3);
851 return (const char *) (((guint32) (ptr + 3)) & ~3);
856 * mono_metadata_decode_row:
857 * @t: table to extract information from.
858 * @idx: index in table.
859 * @res: array of @res_size cols to store the results in
861 * This decompresses the metadata element @idx in table @t
862 * into the guint32 @res array that has res_size elements
865 mono_metadata_decode_row (MonoTableInfo *t, int idx, guint32 *res, int res_size)
867 guint32 bitfield = t->size_bitfield;
868 int i, count = mono_metadata_table_count (bitfield);
869 const char *data = t->base + idx * t->row_size;
871 g_assert (res_size == count);
873 for (i = 0; i < count; i++){
874 int n = mono_metadata_table_size (bitfield, i);
878 res [i] = *data; break;
880 res [i] = read16 (data); break;
883 res [i] = read32 (data); break;
886 g_assert_not_reached ();
893 * mono_metadata_decode_row_col:
894 * @t: table to extract information from.
895 * @idx: index for row in table.
896 * @col: column in the row.
898 * This function returns the value of column @col from the @idx
899 * row in the table @t.
902 mono_metadata_decode_row_col (MonoTableInfo *t, int idx, guint col)
904 guint32 bitfield = t->size_bitfield;
906 register const char *data = t->base + idx * t->row_size;
909 g_assert (col < mono_metadata_table_count (bitfield));
911 n = mono_metadata_table_size (bitfield, 0);
912 for (i = 0; i < col; ++i) {
914 n = mono_metadata_table_size (bitfield, i + 1);
920 return read16 (data);
922 return read32 (data);
924 g_assert_not_reached ();
929 * mono_metadata_decode_blob_size:
930 * @ptr: pointer to a blob object
931 * @rptr: the new position of the pointer
933 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
935 * Returns: the size of the blob object
938 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
940 const unsigned char *ptr = (const unsigned char *)xptr;
943 if ((*ptr & 0x80) == 0){
944 size = ptr [0] & 0x7f;
946 } else if ((*ptr & 0x40) == 0){
947 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
950 size = ((ptr [0] & 0x1f) << 24) +
963 * mono_metadata_decode_value:
964 * @ptr: pointer to decode from
965 * @rptr: the new position of the pointer
967 * This routine decompresses 32-bit values as specified in the "Blob and
968 * Signature" section (22.2)
970 * Returns: the decoded value
973 mono_metadata_decode_value (const char *_ptr, const char **rptr)
975 const unsigned char *ptr = (const unsigned char *) _ptr;
976 unsigned char b = *ptr;
979 if ((b & 0x80) == 0){
982 } else if ((b & 0x40) == 0){
983 len = ((b & 0x3f) << 8 | ptr [1]);
986 len = ((b & 0x1f) << 24) |
999 * mono_metadata_parse_typedef_or_ref:
1000 * @m: a metadata context.
1001 * @ptr: a pointer to an encoded TypedefOrRef in @m
1002 * @rptr: pointer updated to match the end of the decoded stream
1004 * Returns: a token valid in the @m metadata decoded from
1005 * the compressed representation.
1008 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1011 token = mono_metadata_decode_value (ptr, &ptr);
1014 return mono_metadata_token_from_dor (token);
1018 * mono_metadata_parse_custom_mod:
1019 * @m: a metadata context.
1020 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1021 * @ptr: a pointer to (possibly) the start of a custom modifier list
1022 * @rptr: pointer updated to match the end of the decoded stream
1024 * Checks if @ptr points to a type custom modifier compressed representation.
1026 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1029 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1031 MonoCustomMod local;
1032 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
1033 (*ptr == MONO_TYPE_CMOD_REQD)) {
1036 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1037 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1044 * mono_metadata_parse_array:
1045 * @m: a metadata context.
1046 * @ptr: a pointer to an encoded array description.
1047 * @rptr: pointer updated to match the end of the decoded stream
1049 * Decodes the compressed array description found in the metadata @m at @ptr.
1051 * Returns: a #MonoArrayType structure describing the array type
1055 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1058 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1061 etype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1062 array->eklass = mono_class_from_mono_type (etype);
1063 array->rank = mono_metadata_decode_value (ptr, &ptr);
1065 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1066 if (array->numsizes)
1067 array->sizes = g_new0 (int, array->numsizes);
1068 for (i = 0; i < array->numsizes; ++i)
1069 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1071 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1072 if (array->numlobounds)
1073 array->lobounds = g_new0 (int, array->numlobounds);
1074 for (i = 0; i < array->numlobounds; ++i)
1075 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1083 * mono_metadata_free_array:
1084 * @array: array description
1086 * Frees the array description returned from mono_metadata_parse_array().
1089 mono_metadata_free_array (MonoArrayType *array)
1091 g_free (array->sizes);
1092 g_free (array->lobounds);
1097 * need to add common field and param attributes combinations:
1100 * public static literal
1103 * private static literal
1107 /* data, attrs, type, nmods, byref, pinned */
1108 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1109 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1110 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1111 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1112 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1113 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1114 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1115 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1116 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1117 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1118 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1119 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1120 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1121 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1122 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1123 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1124 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1125 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1126 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1127 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1128 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1129 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1130 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1131 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1132 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1133 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1134 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1135 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1136 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1137 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1138 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1139 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1142 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1144 static GHashTable *type_cache = NULL;
1147 * MonoTypes with modifies are never cached, so we never check or use that field.
1150 mono_type_hash (gconstpointer data)
1152 const MonoType *type = (const MonoType *) data;
1153 return type->type | (type->byref << 8) | (type->attrs << 9);
1157 mono_type_equal (gconstpointer ka, gconstpointer kb)
1159 const MonoType *a = (const MonoType *) ka;
1160 const MonoType *b = (const MonoType *) kb;
1162 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1164 /* need other checks */
1169 * mono_metadata_init:
1171 * Initialize the global variables of this module.
1174 mono_metadata_init (void)
1178 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1180 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1181 g_hash_table_insert (type_cache, &builtin_types [i], &builtin_types [i]);
1185 * mono_metadata_parse_type:
1186 * @m: metadata context
1187 * @mode: king of type that may be found at @ptr
1188 * @opt_attrs: optional attributes to store in the returned type
1189 * @ptr: pointer to the type representation
1190 * @rptr: pointer updated to match the end of the decoded stream
1192 * Decode a compressed type description found at @ptr in @m.
1193 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1194 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1195 * This function can be used to decode type descriptions in method signatures,
1196 * field signatures, locals signatures etc.
1198 * Returns: a #MonoType structure representing the decoded type.
1201 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs, const char *ptr, const char **rptr)
1203 MonoType *type, *cached;
1204 gboolean byref = FALSE;
1207 * According to the spec, custom modifiers should come before the byref
1208 * flag, but the IL produced by ilasm from the following signature:
1209 * object modopt(...) &
1210 * starts with a byref flag, followed by the modifiers. (bug #49802)
1211 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1212 * it would be better to treat byref as real type constructor instead of
1215 if (*ptr == MONO_TYPE_BYREF) {
1221 case MONO_PARSE_MOD_TYPE:
1222 case MONO_PARSE_PARAM:
1223 case MONO_PARSE_RET:
1224 case MONO_PARSE_LOCAL: /* should not have modifiers according to the spec, but ms tools disagree */
1225 case MONO_PARSE_FIELD: {
1226 /* count the modifiers */
1227 const char *tmp_ptr = ptr;
1229 while (mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr))
1232 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1233 type->num_mods = count;
1235 g_warning ("got more than 64 modifiers in type");
1236 /* save them this time */
1238 while (mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr))
1241 } /* fall through */
1243 case MONO_PARSE_TYPE:
1245 * Later we can avoid doing this allocation.
1247 type = g_new0 (MonoType, 1);
1250 g_assert_not_reached ();
1253 type->attrs = opt_attrs;
1254 type->byref = byref;
1255 if (mode == MONO_PARSE_LOCAL) {
1257 * check for pinned flag
1259 if (*ptr == MONO_TYPE_PINNED) {
1266 case MONO_TYPE_BYREF:
1267 if (mode == MONO_PARSE_FIELD)
1268 g_warning ("A field type cannot be byref");
1271 /* follow through */
1273 /*if (*ptr == MONO_TYPE_VOID && mode != MONO_PARSE_RET)
1274 g_error ("void not allowed in param");*/
1275 do_mono_metadata_parse_type (type, m, ptr, &ptr);
1281 /* No need to use locking since nobody is modifying the hash table */
1282 if (mode != MONO_PARSE_PARAM && !type->num_mods && (cached = g_hash_table_lookup (type_cache, type))) {
1283 mono_metadata_free_type (type);
1291 * mono_metadata_parse_signature:
1292 * @image: metadata context
1293 * @toke: metadata token
1295 * Decode a method signature stored in the STANDALONESIG table
1297 * Returns: a MonoMethodSignature describing the signature.
1299 MonoMethodSignature *
1300 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1302 MonoTableInfo *tables = image->tables;
1303 guint32 idx = mono_metadata_token_index (token);
1307 if (image->assembly->dynamic)
1308 return mono_lookup_dynamic_token (image, token);
1310 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1312 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1314 ptr = mono_metadata_blob_heap (image, sig);
1315 mono_metadata_decode_blob_size (ptr, &ptr);
1317 return mono_metadata_parse_method_signature (image, FALSE, ptr, NULL);
1320 MonoMethodSignature*
1321 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1323 MonoMethodSignature *sig;
1325 /* later we want to allocate signatures with mempools */
1326 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1327 sig->param_count = nparams;
1333 * mono_metadata_parse_method_signature:
1334 * @m: metadata context
1335 * @def: the MethodDef index or 0 for Ref signatures.
1336 * @ptr: pointer to the signature metadata representation
1337 * @rptr: pointer updated to match the end of the decoded stream
1339 * Decode a method signature stored at @ptr.
1341 * Returns: a MonoMethodSignature describing the signature.
1343 MonoMethodSignature *
1344 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1346 MonoMethodSignature *method;
1347 int i, ret_attrs = 0, *pattrs = NULL;
1348 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1349 guint32 gen_param_count = 0;
1352 gen_param_count = 1;
1357 call_convention = *ptr & 0x0F;
1359 if (gen_param_count)
1360 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1361 param_count = mono_metadata_decode_value (ptr, &ptr);
1362 pattrs = g_new0 (int, param_count);
1365 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1366 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1367 guint32 cols [MONO_PARAM_SIZE];
1368 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1370 if (def < methodt->rows)
1371 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1373 lastp = paramt->rows + 1;
1374 for (i = param_index; i < lastp; ++i) {
1375 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1376 if (!cols [MONO_PARAM_SEQUENCE])
1377 ret_attrs = cols [MONO_PARAM_FLAGS];
1379 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1382 method = mono_metadata_signature_alloc (m, param_count);
1383 method->hasthis = hasthis;
1384 method->explicit_this = explicit_this;
1385 method->call_convention = call_convention;
1386 method->generic_param_count = gen_param_count;
1387 if (call_convention != 0xa)
1388 method->ret = mono_metadata_parse_type (m, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1390 if (method->param_count) {
1391 method->sentinelpos = -1;
1393 for (i = 0; i < method->param_count; ++i) {
1394 if (*ptr == MONO_TYPE_SENTINEL) {
1395 if (method->call_convention != MONO_CALL_VARARG || def)
1396 g_error ("found sentinel for methoddef or no vararg method");
1397 method->sentinelpos = i;
1400 method->params [i] = mono_metadata_parse_type (m, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1408 * Add signature to a cache and increase ref count...
1414 * mono_metadata_free_method_signature:
1415 * @sig: signature to destroy
1417 * Free the memory allocated in the signature @sig.
1420 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1423 mono_metadata_free_type (sig->ret);
1424 for (i = 0; i < sig->param_count; ++i)
1425 mono_metadata_free_type (sig->params [i]);
1431 do_mono_metadata_parse_generic_inst (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1433 MonoGenericInst *generic_inst = g_new0 (MonoGenericInst, 1);
1434 MonoClass *klass, *gklass;
1437 type->data.generic_inst = generic_inst;
1439 generic_inst->klass = klass = g_new0 (MonoClass, 1);
1441 generic_inst->generic_type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1442 generic_inst->type_argc = count = mono_metadata_decode_value (ptr, &ptr);
1443 generic_inst->type_argv = g_new0 (MonoType*, count);
1446 * Create the klass before parsing the type arguments.
1447 * This is required to support "recursive" definitions.
1448 * See mcs/tests/gen-23.cs for an example.
1451 gklass = mono_class_from_mono_type (generic_inst->generic_type);
1452 mono_class_init (gklass);
1454 klass->name_space = gklass->name_space;
1456 klass->flags = gklass->flags;
1458 klass->generic_inst = type;
1460 klass->cast_class = klass->element_class = klass;
1462 for (i = 0; i < generic_inst->type_argc; i++)
1463 generic_inst->type_argv [i] = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1465 mono_class_initialize_generic (generic_inst->klass, TRUE);
1471 static MonoGenericParam *
1472 mono_metadata_parse_generic_param (MonoImage *m, const char *ptr, const char **rptr)
1474 MonoGenericParam *generic_param = g_new0 (MonoGenericParam, 1);
1476 generic_param->num = mono_metadata_decode_value (ptr, &ptr);
1481 return generic_param;
1485 * do_mono_metadata_parse_type:
1486 * @type: MonoType to be filled in with the return value
1488 * Internal routine used to "fill" the contents of @type from an
1489 * allocated pointer. This is done this way to avoid doing too
1490 * many mini-allocations (particularly for the MonoFieldType which
1491 * most of the time is just a MonoType, but sometimes might be augmented).
1493 * This routine is used by mono_metadata_parse_type and
1494 * mono_metadata_parse_field_type
1496 * This extracts a Type as specified in Partition II (22.2.12)
1499 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1501 type->type = mono_metadata_decode_value (ptr, &ptr);
1503 switch (type->type){
1504 case MONO_TYPE_VOID:
1505 case MONO_TYPE_BOOLEAN:
1506 case MONO_TYPE_CHAR:
1519 case MONO_TYPE_STRING:
1520 case MONO_TYPE_OBJECT:
1521 case MONO_TYPE_TYPEDBYREF:
1523 case MONO_TYPE_VALUETYPE:
1524 case MONO_TYPE_CLASS: {
1526 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1527 type->data.klass = mono_class_get (m, token);
1530 case MONO_TYPE_SZARRAY: {
1531 MonoType *etype = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1532 type->data.klass = mono_class_from_mono_type (etype);
1536 type->data.type = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1538 case MONO_TYPE_FNPTR:
1539 type->data.method = mono_metadata_parse_method_signature (m, 0, ptr, &ptr);
1541 case MONO_TYPE_ARRAY:
1542 type->data.array = mono_metadata_parse_array (m, ptr, &ptr);
1545 case MONO_TYPE_MVAR:
1547 type->data.generic_param = mono_metadata_parse_generic_param (m, ptr, &ptr);
1550 case MONO_TYPE_GENERICINST:
1551 do_mono_metadata_parse_generic_inst (type, m, ptr, &ptr);
1555 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1563 * mono_metadata_free_type:
1564 * @type: type to free
1566 * Free the memory allocated for type @type.
1569 mono_metadata_free_type (MonoType *type)
1571 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1573 switch (type->type){
1575 mono_metadata_free_type (type->data.type);
1577 case MONO_TYPE_FNPTR:
1578 mono_metadata_free_method_signature (type->data.method);
1580 case MONO_TYPE_ARRAY:
1581 mono_metadata_free_array (type->data.array);
1589 hex_dump (const char *buffer, int base, int count)
1591 int show_header = 1;
1599 for (i = 0; i < count; i++){
1602 printf ("\n0x%08x: ", (unsigned char) base + i);
1604 printf ("%02x ", (unsigned char) (buffer [i]));
1611 * @mh: The Method header
1612 * @ptr: Points to the beginning of the Section Data (25.3)
1615 parse_section_data (MonoMethodHeader *mh, const unsigned char *ptr)
1617 unsigned char sect_data_flags;
1618 const unsigned char *sptr;
1620 guint32 sect_data_len;
1623 /* align on 32-bit boundary */
1624 /* FIXME: not 64-bit clean code */
1625 sptr = ptr = dword_align (ptr);
1626 sect_data_flags = *ptr;
1629 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1631 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1634 sect_data_len = ptr [0];
1638 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1639 hex_dump (sptr, 0, sect_data_len+8);
1640 g_print ("\nheader: ");
1641 hex_dump (sptr-4, 0, 4);
1645 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1646 const unsigned char *p = dword_align (ptr);
1648 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1649 /* we could just store a pointer if we don't need to byteswap */
1650 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1651 for (i = 0; i < mh->num_clauses; ++i) {
1652 MonoExceptionClause *ec = &mh->clauses [i];
1655 /* we could memcpy and byteswap */
1656 ec->flags = read32 (p);
1658 ec->try_offset = read32 (p);
1660 ec->try_len = read32 (p);
1662 ec->handler_offset = read32 (p);
1664 ec->handler_len = read32 (p);
1666 ec->token_or_filter = read32 (p);
1669 ec->flags = read16 (p);
1671 ec->try_offset = read16 (p);
1675 ec->handler_offset = read16 (p);
1677 ec->handler_len = *p;
1679 ec->token_or_filter = read32 (p);
1682 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
1686 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
1687 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
1694 * mono_metadata_parse_mh:
1695 * @m: metadata context
1696 * @ptr: pointer to the method header.
1698 * Decode the method header at @ptr, including pointer to the IL code,
1699 * info about local variables and optional exception tables.
1701 * Returns: a MonoMethodHeader.
1704 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
1706 MonoMethodHeader *mh;
1707 unsigned char flags = *(const unsigned char *) ptr;
1708 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
1710 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
1711 const unsigned char *code;
1714 g_return_val_if_fail (ptr != NULL, NULL);
1717 case METHOD_HEADER_TINY_FORMAT:
1718 mh = g_new0 (MonoMethodHeader, 1);
1721 local_var_sig_tok = 0;
1722 mh->code_size = flags >> 2;
1725 case METHOD_HEADER_TINY_FORMAT1:
1726 mh = g_new0 (MonoMethodHeader, 1);
1729 local_var_sig_tok = 0;
1732 * The spec claims 3 bits, but the Beta2 is
1735 mh->code_size = flags >> 2;
1738 case METHOD_HEADER_FAT_FORMAT:
1739 fat_flags = read16 (ptr);
1741 hsize = (fat_flags >> 12) & 0xf;
1742 max_stack = read16 (ptr);
1744 code_size = read32 (ptr);
1746 local_var_sig_tok = read32 (ptr);
1749 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
1756 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
1760 * There are more sections
1762 ptr = code + code_size;
1770 if (local_var_sig_tok) {
1771 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1772 const char *locals_ptr;
1773 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1774 int len=0, i, bsize;
1776 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
1777 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1778 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1779 if (*locals_ptr != 0x07)
1780 g_warning ("wrong signature for locals blob");
1782 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
1783 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1784 mh->num_locals = len;
1785 for (i = 0; i < len; ++i)
1786 mh->locals [i] = mono_metadata_parse_type (m, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
1788 mh = g_new0 (MonoMethodHeader, 1);
1791 mh->code_size = code_size;
1792 mh->max_stack = max_stack;
1793 mh->init_locals = init_locals;
1794 if (fat_flags & METHOD_HEADER_MORE_SECTS)
1795 parse_section_data (mh, (const unsigned char*)ptr);
1800 * mono_metadata_free_mh:
1801 * @mh: a method header
1803 * Free the memory allocated for the method header.
1806 mono_metadata_free_mh (MonoMethodHeader *mh)
1809 for (i = 0; i < mh->num_locals; ++i)
1810 mono_metadata_free_type (mh->locals[i]);
1811 g_free (mh->clauses);
1816 * mono_metadata_parse_field_type:
1817 * @m: metadata context to extract information from
1818 * @ptr: pointer to the field signature
1819 * @rptr: pointer updated to match the end of the decoded stream
1821 * Parses the field signature, and returns the type information for it.
1823 * Returns: The MonoType that was extracted from @ptr.
1826 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
1828 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
1832 * mono_metadata_parse_param:
1833 * @m: metadata context to extract information from
1834 * @ptr: pointer to the param signature
1835 * @rptr: pointer updated to match the end of the decoded stream
1837 * Parses the param signature, and returns the type information for it.
1839 * Returns: The MonoType that was extracted from @ptr.
1842 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
1844 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
1848 * mono_metadata_token_from_dor:
1849 * @dor_token: A TypeDefOrRef coded index
1851 * dor_token is a TypeDefOrRef coded index: it contains either
1852 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
1853 * bits contain an index into the table.
1855 * Returns: an expanded token
1858 mono_metadata_token_from_dor (guint32 dor_index)
1862 table = dor_index & 0x03;
1863 idx = dor_index >> 2;
1866 case 0: /* TypeDef */
1867 return MONO_TOKEN_TYPE_DEF | idx;
1869 case 1: /* TypeRef */
1870 return MONO_TOKEN_TYPE_REF | idx;
1872 case 2: /* TypeSpec */
1873 return MONO_TOKEN_TYPE_SPEC | idx;
1876 g_assert_not_reached ();
1883 * We use this to pass context information to the row locator
1886 int idx; /* The index that we are trying to locate */
1887 int col_idx; /* The index in the row where idx may be stored */
1888 MonoTableInfo *t; /* pointer to the table */
1892 #define CSIZE(x) (sizeof (x) / 4)
1895 * How the row locator works.
1900 * ___|___------> _______
1903 * A column in the rows of table A references an index in table B.
1904 * For example A may be the TYPEDEF table and B the METHODDEF table.
1906 * Given an index in table B we want to get the row in table A
1907 * where the column n references our index in B.
1909 * In the locator_t structure:
1911 * col_idx is the column number
1912 * index is the index in table B
1913 * result will be the index in table A
1916 * Table A Table B column (in table A)
1917 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
1918 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
1919 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
1920 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
1921 * METHODSEM PROPERTY ASSOCIATION (encoded index)
1923 * Note that we still don't support encoded indexes.
1927 typedef_locator (const void *a, const void *b)
1929 locator_t *loc = (locator_t *) a;
1930 const char *bb = (const char *) b;
1931 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
1932 guint32 col, col_next;
1934 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
1940 * Need to check that the next row is valid.
1942 if (typedef_index + 1 < loc->t->rows) {
1943 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
1944 if (loc->idx >= col_next)
1947 if (col == col_next)
1951 loc->result = typedef_index;
1957 table_locator (const void *a, const void *b)
1959 locator_t *loc = (locator_t *) a;
1960 const char *bb = (const char *) b;
1961 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
1964 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
1966 if (loc->idx == col) {
1967 loc->result = table_index;
1977 * mono_metadata_typedef_from_field:
1978 * @meta: metadata context
1979 * @index: FieldDef token
1981 * Returns the 1-based index into the TypeDef table of the type that
1982 * declared the field described by @index.
1983 * Returns 0 if not found.
1986 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
1988 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
1994 loc.idx = mono_metadata_token_index (index);
1995 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
1998 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
1999 g_assert_not_reached ();
2001 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2002 return loc.result + 1;
2006 * mono_metadata_typedef_from_method:
2007 * @meta: metadata context
2008 * @index: MethodDef token
2010 * Returns the 1-based index into the TypeDef table of the type that
2011 * declared the method described by @index.
2012 * Returns 0 if not found.
2015 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2017 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2023 loc.idx = mono_metadata_token_index (index);
2024 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2027 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2028 g_assert_not_reached ();
2030 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2031 return loc.result + 1;
2035 * mono_metadata_interfaces_from_typedef:
2036 * @meta: metadata context
2037 * @index: typedef token
2039 * Returns and array of interfaces that the @index typedef token implements.
2040 * The number of elemnts in the array is returned in @count.
2043 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2045 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2048 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2056 loc.idx = mono_metadata_token_index (index);
2057 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2060 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2065 * We may end up in the middle of the rows...
2068 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2075 while (start < tdef->rows) {
2076 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2077 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2079 result = g_renew (MonoClass*, result, i + 1);
2080 result [i] = mono_class_get (meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]));
2088 * mono_metadata_nested_in_typedef:
2089 * @meta: metadata context
2090 * @index: typedef token
2092 * Returns: the 1-based index into the TypeDef table of the type
2093 * where the type described by @index is nested.
2094 * Retruns 0 if @index describes a non-nested type.
2097 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2099 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2105 loc.idx = mono_metadata_token_index (index);
2106 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2109 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2112 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2113 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2117 * mono_metadata_nesting_typedef:
2118 * @meta: metadata context
2119 * @index: typedef token
2121 * Returns: the 1-based index into the TypeDef table of the first type
2122 * that is nested inside the type described by @index. The search starts at
2124 * Returns 0 if no such type is found.
2127 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2129 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2131 guint32 class_index = mono_metadata_token_index (index);
2136 start = start_index;
2138 while (start <= tdef->rows) {
2139 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2145 if (start > tdef->rows)
2152 * mono_metadata_packing_from_typedef:
2153 * @meta: metadata context
2154 * @index: token representing a type
2156 * Returns the info stored in the ClassLAyout table for the given typedef token
2157 * into the @packing and @size pointers.
2158 * Returns 0 if the info is not found.
2161 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2163 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2165 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2170 loc.idx = mono_metadata_token_index (index);
2171 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2174 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2177 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2179 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2181 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2183 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2184 return loc.result + 1;
2188 * mono_metadata_custom_attrs_from_index:
2189 * @meta: metadata context
2190 * @index: token representing the parent
2192 * Returns: the 1-based index into the CustomAttribute table of the first
2193 * attribute which belongs to the metadata object described by @index.
2194 * Returns 0 if no such attribute is found.
2197 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2199 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2206 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2209 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2212 /* Find the first entry by searching backwards */
2213 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2216 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2217 return loc.result + 1;
2222 mono_backtrace (int limit)
2227 backtrace (array, limit);
2228 names = backtrace_symbols (array, limit);
2229 for (i =0; i < limit; ++i) {
2230 g_print ("\t%s\n", names [i]);
2237 /*#define __alignof__(a) sizeof(a)*/
2238 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2243 * @t: the type to return the size of
2245 * Returns: the number of bytes required to hold an instance of this
2249 mono_type_size (MonoType *t, gint *align)
2256 *align = __alignof__(gpointer);
2257 return sizeof (gpointer);
2261 case MONO_TYPE_VOID:
2264 case MONO_TYPE_BOOLEAN:
2265 *align = __alignof__(gint8);
2269 *align = __alignof__(gint8);
2271 case MONO_TYPE_CHAR:
2274 *align = __alignof__(gint16);
2278 *align = __alignof__(gint32);
2281 *align = __alignof__(float);
2285 *align = __alignof__(gint64);
2288 *align = __alignof__(double);
2292 *align = __alignof__(gpointer);
2293 return sizeof (gpointer);
2294 case MONO_TYPE_STRING:
2295 *align = __alignof__(gpointer);
2296 return sizeof (gpointer);
2297 case MONO_TYPE_OBJECT:
2298 *align = __alignof__(gpointer);
2299 return sizeof (gpointer);
2300 case MONO_TYPE_VALUETYPE: {
2301 if (t->data.klass->enumtype)
2302 return mono_type_size (t->data.klass->enum_basetype, align);
2304 return mono_class_value_size (t->data.klass, align);
2306 case MONO_TYPE_CLASS:
2307 case MONO_TYPE_SZARRAY:
2309 case MONO_TYPE_FNPTR:
2310 case MONO_TYPE_ARRAY:
2311 *align = __alignof__(gpointer);
2312 return sizeof (gpointer);
2313 case MONO_TYPE_TYPEDBYREF:
2314 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2315 case MONO_TYPE_GENERICINST: {
2316 MonoClass *iclass = mono_class_from_mono_type (t);
2317 return mono_type_size (&iclass->byval_arg, align);
2320 case MONO_TYPE_MVAR:
2321 *align = __alignof__(gpointer);
2322 return sizeof (gpointer);
2324 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2330 * mono_type_stack_size:
2331 * @t: the type to return the size it uses on the stack
2333 * Returns: the number of bytes required to hold an instance of this
2334 * type on the runtime stack
2337 mono_type_stack_size (MonoType *t, gint *align)
2341 g_assert (t != NULL);
2347 *align = __alignof__(gpointer);
2348 return sizeof (gpointer);
2352 case MONO_TYPE_BOOLEAN:
2353 case MONO_TYPE_CHAR:
2362 case MONO_TYPE_STRING:
2363 case MONO_TYPE_OBJECT:
2364 case MONO_TYPE_CLASS:
2365 case MONO_TYPE_SZARRAY:
2367 case MONO_TYPE_FNPTR:
2368 case MONO_TYPE_ARRAY:
2369 *align = __alignof__(gpointer);
2370 return sizeof (gpointer);
2371 case MONO_TYPE_TYPEDBYREF:
2372 *align = __alignof__(gpointer);
2373 return sizeof (gpointer) * 2;
2375 *align = __alignof__(float);
2376 return sizeof (float);
2379 *align = __alignof__(gint64);
2380 return sizeof (gint64);
2382 *align = __alignof__(double);
2383 return sizeof (double);
2384 case MONO_TYPE_VALUETYPE: {
2387 if (t->data.klass->enumtype)
2388 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2390 size = mono_class_value_size (t->data.klass, align);
2392 *align = *align + __alignof__(gpointer) - 1;
2393 *align &= ~(__alignof__(gpointer) - 1);
2395 size += sizeof (gpointer) - 1;
2396 size &= ~(sizeof (gpointer) - 1);
2401 case MONO_TYPE_GENERICINST: {
2402 MonoClass *iclass = mono_class_from_mono_type (t);
2403 return mono_type_stack_size (&iclass->byval_arg, align);
2406 g_error ("type 0x%02x unknown", t->type);
2412 * mono_metadata_type_hash:
2415 * Computes an hash value for @t1 to be used in GHashTable.
2418 mono_metadata_type_hash (MonoType *t1)
2420 guint hash = t1->type;
2422 hash |= t1->byref << 6; /* do not collide with t1->type values */
2424 case MONO_TYPE_VALUETYPE:
2425 case MONO_TYPE_CLASS:
2426 case MONO_TYPE_SZARRAY:
2427 /* check if the distribution is good enough */
2428 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2430 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2431 case MONO_TYPE_ARRAY:
2432 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2433 case MONO_TYPE_GENERICINST:
2434 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.generic_inst->generic_type);
2440 * mono_metadata_type_equal:
2444 * Determine if @t1 and @t2 represent the same type.
2445 * Returns: #TRUE if @t1 and @t2 are equal.
2448 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2450 if (t1->type != t2->type ||
2451 t1->byref != t2->byref)
2455 case MONO_TYPE_VOID:
2456 case MONO_TYPE_BOOLEAN:
2457 case MONO_TYPE_CHAR:
2468 case MONO_TYPE_STRING:
2471 case MONO_TYPE_OBJECT:
2472 case MONO_TYPE_TYPEDBYREF:
2474 case MONO_TYPE_VALUETYPE:
2475 case MONO_TYPE_CLASS:
2476 case MONO_TYPE_SZARRAY:
2477 return t1->data.klass == t2->data.klass;
2479 return mono_metadata_type_equal (t1->data.type, t2->data.type);
2480 case MONO_TYPE_ARRAY:
2481 if (t1->data.array->rank != t2->data.array->rank)
2483 return t1->data.array->eklass == t2->data.array->eklass;
2484 case MONO_TYPE_GENERICINST: {
2486 if (t1->data.generic_inst->type_argc != t2->data.generic_inst->type_argc)
2488 if (!mono_metadata_type_equal (t1->data.generic_inst->generic_type, t2->data.generic_inst->generic_type))
2490 for (i = 0; i < t1->data.generic_inst->type_argc; ++i) {
2491 if (!mono_metadata_type_equal (t1->data.generic_inst->type_argv [i], t2->data.generic_inst->type_argv [i]))
2497 case MONO_TYPE_MVAR:
2498 return t1->data.generic_param->num == t2->data.generic_param->num;
2500 g_error ("implement type compare for %0x!", t1->type);
2508 * mono_metadata_signature_equal:
2509 * @sig1: a signature
2510 * @sig2: another signature
2512 * Determine if @sig1 and @sig2 represent the same signature, with the
2513 * same number of arguments and the same types.
2514 * Returns: #TRUE if @sig1 and @sig2 are equal.
2517 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2521 if (sig1->hasthis != sig2->hasthis ||
2522 sig1->param_count != sig2->param_count)
2525 for (i = 0; i < sig1->param_count; i++) {
2526 MonoType *p1 = sig1->params[i];
2527 MonoType *p2 = sig2->params[i];
2529 //if (p1->attrs != p2->attrs)
2532 if (!mono_metadata_type_equal (p1, p2))
2536 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
2542 mono_signature_hash (MonoMethodSignature *sig)
2544 guint i, res = sig->ret->type;
2546 for (i = 0; i < sig->param_count; i++)
2547 res = (res << 5) - res + sig->params[i]->type;
2553 * mono_metadata_encode_value:
2554 * @value: value to encode
2555 * @buf: buffer where to write the compressed representation
2556 * @endbuf: pointer updated to point at the end of the encoded output
2558 * Encodes the value @value in the compressed representation used
2559 * in metadata and stores the result in @buf. @buf needs to be big
2560 * enough to hold the data (4 bytes).
2563 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
2569 else if (value <= 16384) {
2570 p [0] = 0x80 | (value >> 8);
2571 p [1] = value & 0xff;
2574 p [0] = (value >> 24) | 0xc0;
2575 p [1] = (value >> 16) & 0xff;
2576 p [2] = (value >> 8) & 0xff;
2577 p [3] = value & 0xff;
2585 * mono_metadata_field_info:
2586 * @meta: the Image the field is defined in
2587 * @index: the index in the field table representing the field
2588 * @offset: a pointer to an integer where to store the offset that
2589 * may have been specified for the field in a FieldLayout table
2590 * @rva: a pointer to the RVA of the field data in the image that
2591 * may have been defined in a FieldRVA table
2592 * @marshal_spec: a pointer to the marshal spec that may have been
2593 * defined for the field in a FieldMarshal table.
2595 * Gather info for field @index that may have been defined in the FieldLayout,
2596 * FieldRVA and FieldMarshal tables.
2597 * Either of offset, rva and marshal_spec can be NULL if you're not interested
2601 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
2602 MonoMarshalSpec **marshal_spec)
2604 MonoTableInfo *tdef;
2607 loc.idx = index + 1;
2609 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
2611 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
2614 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2615 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
2617 *offset = (guint32)-1;
2621 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
2623 loc.col_idx = MONO_FIELD_RVA_FIELD;
2626 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2628 * LAMESPEC: There is no signature, no nothing, just the raw data.
2630 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
2638 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
2639 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
2646 * mono_metadata_get_constant_index:
2647 * @meta: the Image the field is defined in
2648 * @index: the token that may have a row defined in the constants table
2649 * @hint: possible position for the row
2651 * @token must be a FieldDef, ParamDef or PropertyDef token.
2653 * Returns: the index into the Constants table or 0 if not found.
2656 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
2658 MonoTableInfo *tdef;
2660 guint32 index = mono_metadata_token_index (token);
2662 tdef = &meta->tables [MONO_TABLE_CONSTANT];
2663 index <<= HASCONSTANT_BITS;
2664 switch (mono_metadata_token_table (token)) {
2665 case MONO_TABLE_FIELD:
2666 index |= HASCONSTANT_FIEDDEF;
2668 case MONO_TABLE_PARAM:
2669 index |= HASCONSTANT_PARAM;
2671 case MONO_TABLE_PROPERTY:
2672 index |= HASCONSTANT_PROPERTY;
2675 g_warning ("Not a valid token for the constant table: 0x%08x", token);
2679 loc.col_idx = MONO_CONSTANT_PARENT;
2682 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
2685 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2686 return loc.result + 1;
2692 * mono_metadata_events_from_typedef:
2693 * @meta: metadata context
2694 * @index: 0-based index (in the TypeDef table) describing a type
2696 * Returns: the 0-based index in the Event table for the events in the
2697 * type. The last event that belongs to the type (plus 1) is stored
2698 * in the @end_idx pointer.
2701 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2705 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
2713 loc.col_idx = MONO_EVENT_MAP_PARENT;
2714 loc.idx = index + 1;
2716 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2719 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
2720 if (loc.result + 1 < tdef->rows) {
2721 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
2723 end = meta->tables [MONO_TABLE_EVENT].rows;
2731 * mono_metadata_methods_from_event:
2732 * @meta: metadata context
2733 * @index: 0-based index (in the Event table) describing a event
2735 * Returns: the 0-based index in the MethodDef table for the methods in the
2736 * event. The last method that belongs to the event (plus 1) is stored
2737 * in the @end_idx pointer.
2740 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
2744 guint32 cols [MONO_METHOD_SEMA_SIZE];
2745 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2752 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2753 loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_EVENT; /* Method association coded index */
2755 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2760 * We may end up in the middle of the rows...
2763 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2769 while (end < msemt->rows) {
2770 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2771 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2780 * mono_metadata_properties_from_typedef:
2781 * @meta: metadata context
2782 * @index: 0-based index (in the TypeDef table) describing a type
2784 * Returns: the 0-based index in the Property table for the properties in the
2785 * type. The last property that belongs to the type (plus 1) is stored
2786 * in the @end_idx pointer.
2789 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2793 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
2801 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
2802 loc.idx = index + 1;
2804 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2807 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
2808 if (loc.result + 1 < tdef->rows) {
2809 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
2811 end = meta->tables [MONO_TABLE_PROPERTY].rows;
2819 * mono_metadata_methods_from_property:
2820 * @meta: metadata context
2821 * @index: 0-based index (in the PropertyDef table) describing a property
2823 * Returns: the 0-based index in the MethodDef table for the methods in the
2824 * property. The last method that belongs to the property (plus 1) is stored
2825 * in the @end_idx pointer.
2828 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
2832 guint32 cols [MONO_METHOD_SEMA_SIZE];
2833 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2840 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2841 loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_PROPERTY; /* Method association coded index */
2843 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2848 * We may end up in the middle of the rows...
2851 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2857 while (end < msemt->rows) {
2858 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2859 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2868 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
2871 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
2877 loc.col_idx = MONO_IMPLMAP_MEMBER;
2878 loc.idx = ((method_idx + 1) << MEMBERFORWD_BITS) | MEMBERFORWD_METHODDEF;
2880 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2883 return loc.result + 1;
2887 * @image: context where the image is created
2888 * @type_spec: typespec token
2890 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
2894 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
2896 guint32 idx = mono_metadata_token_index (type_spec);
2898 guint32 cols [MONO_TYPESPEC_SIZE];
2903 mono_loader_lock ();
2905 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
2906 mono_loader_unlock ();
2910 t = &image->tables [MONO_TABLE_TYPESPEC];
2912 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
2913 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
2914 len = mono_metadata_decode_value (ptr, &ptr);
2916 type = g_new0 (MonoType, 1);
2918 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
2920 if (*ptr == MONO_TYPE_BYREF) {
2925 do_mono_metadata_parse_type (type, image, ptr, &ptr);
2927 mono_loader_unlock ();
2933 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
2935 MonoMarshalSpec *res;
2937 const char *start = ptr;
2939 /* fixme: this is incomplete, but I cant find more infos in the specs */
2941 res = g_new0 (MonoMarshalSpec, 1);
2943 len = mono_metadata_decode_value (ptr, &ptr);
2944 res->native = *ptr++;
2946 if (res->native == MONO_NATIVE_LPARRAY) {
2947 if (ptr - start <= len)
2948 res->data.array_data.elem_type = *ptr++;
2949 if (ptr - start <= len)
2950 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
2951 if (ptr - start <= len)
2952 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
2955 if (res->native == MONO_NATIVE_BYVALTSTR) {
2956 if (ptr - start <= len)
2957 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
2960 if (res->native == MONO_NATIVE_BYVALARRAY) {
2961 if (ptr - start <= len)
2962 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
2965 if (res->native == MONO_NATIVE_CUSTOM) {
2966 /* skip unused type guid */
2967 len = mono_metadata_decode_value (ptr, &ptr);
2969 /* skip unused native type name */
2970 len = mono_metadata_decode_value (ptr, &ptr);
2972 /* read custom marshaler type name */
2973 len = mono_metadata_decode_value (ptr, &ptr);
2974 res->data.custom_data.custom_name = g_strndup (ptr, len);
2976 /* read cookie string */
2977 len = mono_metadata_decode_value (ptr, &ptr);
2978 res->data.custom_data.cookie = g_strndup (ptr, len);
2985 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
2986 gboolean unicode, MonoMarshalConv *conv)
2988 MonoMarshalConv dummy_conv;
2994 *conv = MONO_MARSHAL_CONV_NONE;
2997 return MONO_NATIVE_UINT;
3001 case MONO_TYPE_BOOLEAN:
3003 switch (mspec->native) {
3004 case MONO_NATIVE_VARIANTBOOL:
3005 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3006 return MONO_NATIVE_VARIANTBOOL;
3007 case MONO_NATIVE_BOOLEAN:
3008 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3009 return MONO_NATIVE_BOOLEAN;
3010 case MONO_NATIVE_I1:
3011 case MONO_NATIVE_U1:
3012 return mspec->native;
3014 g_error ("cant marshal bool to native type %02x", mspec->native);
3017 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3018 return MONO_NATIVE_BOOLEAN;
3019 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3020 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3021 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3022 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3023 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3024 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3025 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3026 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3027 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3028 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3029 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3030 case MONO_TYPE_STRING:
3032 switch (mspec->native) {
3033 case MONO_NATIVE_BSTR:
3034 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3035 return MONO_NATIVE_BSTR;
3036 case MONO_NATIVE_LPSTR:
3037 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3038 return MONO_NATIVE_LPSTR;
3039 case MONO_NATIVE_LPWSTR:
3040 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3041 return MONO_NATIVE_LPWSTR;
3042 case MONO_NATIVE_LPTSTR:
3043 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3044 return MONO_NATIVE_LPTSTR;
3045 case MONO_NATIVE_ANSIBSTR:
3046 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3047 return MONO_NATIVE_ANSIBSTR;
3048 case MONO_NATIVE_TBSTR:
3049 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3050 return MONO_NATIVE_TBSTR;
3051 case MONO_NATIVE_BYVALTSTR:
3053 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3055 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3056 return MONO_NATIVE_BYVALTSTR;
3058 g_error ("cant marshal string to native type %02x", mspec->native);
3061 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3062 return MONO_NATIVE_LPTSTR;
3063 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3064 case MONO_TYPE_VALUETYPE: /*FIXME*/
3065 if (type->data.klass->enumtype) {
3066 t = type->data.klass->enum_basetype->type;
3069 return MONO_NATIVE_STRUCT;
3070 case MONO_TYPE_SZARRAY:
3071 case MONO_TYPE_ARRAY:
3073 switch (mspec->native) {
3074 case MONO_NATIVE_BYVALARRAY:
3075 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3076 return MONO_NATIVE_BYVALARRAY;
3077 case MONO_NATIVE_SAFEARRAY:
3078 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3079 return MONO_NATIVE_SAFEARRAY;
3080 case MONO_NATIVE_LPARRAY:
3081 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3082 return MONO_NATIVE_LPARRAY;
3084 g_error ("cant marshal array as native type %02x", mspec->native);
3088 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3089 return MONO_NATIVE_LPARRAY;
3090 case MONO_TYPE_I: return MONO_NATIVE_INT;
3091 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3092 case MONO_TYPE_CLASS:
3093 case MONO_TYPE_OBJECT: {
3094 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3096 switch (mspec->native) {
3097 case MONO_NATIVE_STRUCT:
3098 return MONO_NATIVE_STRUCT;
3099 case MONO_NATIVE_INTERFACE:
3100 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3101 return MONO_NATIVE_INTERFACE;
3102 case MONO_NATIVE_IDISPATCH:
3103 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3104 return MONO_NATIVE_IDISPATCH;
3105 case MONO_NATIVE_IUNKNOWN:
3106 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3107 return MONO_NATIVE_IUNKNOWN;
3109 g_error ("cant marshal object as native type %02x", mspec->native);
3112 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3113 type->data.klass == mono_defaults.delegate_class ||
3114 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3115 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3116 return MONO_NATIVE_FUNC;
3118 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3119 return MONO_NATIVE_STRUCT;
3121 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3122 case MONO_TYPE_TYPEDBYREF:
3124 g_error ("type 0x%02x not handled in marshal", t);
3126 return MONO_NATIVE_MAX;
3130 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3133 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3139 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3140 loc.idx = ((idx + 1) << HAS_FIELD_MARSHAL_BITS) | (is_field? HAS_FIELD_MARSHAL_FIELDSREF: HAS_FIELD_MARSHAL_PARAMDEF);
3142 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3145 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3149 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3151 guint32 idx = tok >> METHODDEFORREF_BITS;
3152 switch (tok & METHODDEFORREF_MASK) {
3153 case METHODDEFORREF_METHODDEF:
3154 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3155 case METHODDEFORREF_METHODREF:
3156 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3158 g_assert_not_reached ();
3163 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3166 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3169 guint32 cols [MONO_METHODIMPL_SIZE];
3170 MonoMethod **result;
3179 loc.col_idx = MONO_METHODIMPL_CLASS;
3180 loc.idx = mono_metadata_token_index (type_token);
3182 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3188 * We may end up in the middle of the rows...
3191 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3196 while (end < tdef->rows) {
3197 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3203 result = g_new (MonoMethod*, num * 2);
3204 for (i = 0; i < num; ++i) {
3205 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3206 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3207 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3211 *num_overrides = num;
3216 * mono_guid_to_string:
3218 * Converts a 16 byte Microsoft GUID to the standard string representation.
3221 mono_guid_to_string (const guint8 *guid)
3223 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3224 guid[3], guid[2], guid[1], guid[0],
3228 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3232 get_constraints (MonoImage *image, int owner)
3234 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3235 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3236 guint32 i, token, found;
3237 MonoClass *klass, **res;
3238 GList *cons = NULL, *tmp;
3242 for (i = 0; i < tdef->rows; ++i) {
3243 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3244 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3245 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3246 klass = mono_class_get (image, token);
3247 cons = g_list_append (cons, klass);
3250 /* contiguous list finished */
3257 res = g_new0 (MonoClass*, found + 1);
3258 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3259 res [i] = tmp->data;
3266 mono_metadata_load_generic_params (MonoImage *image, guint32 token, guint32 *num)
3268 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3269 guint32 cols [MONO_GENERICPARAM_SIZE];
3270 guint32 i, owner, last_num, n;
3271 MonoGenericParam *params;
3273 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3274 owner = MONO_TYPEORMETHOD_TYPE;
3275 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3276 owner = MONO_TYPEORMETHOD_METHOD;
3278 g_error ("wrong token %x to load_generics_params", token);
3280 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3286 for (i = 0; i < tdef->rows; ++i) {
3287 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3288 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3292 if (i >= tdef->rows)
3298 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3299 params [n - 1].pklass = NULL;
3300 params [n - 1].method = NULL;
3301 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3302 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3303 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3304 params [n - 1].constraints = get_constraints (image, i + 1);
3305 if (++i >= tdef->rows)
3307 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3308 } while (cols [MONO_GENERICPARAM_OWNER] == owner);