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 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 static MonoMetaTable AssemblyOSSchema [] = {
52 { MONO_MT_UINT32, "OSPlatformID" },
53 { MONO_MT_UINT32, "OSMajor" },
54 { MONO_MT_UINT32, "OSMinor" },
58 static MonoMetaTable AssemblyProcessorSchema [] = {
59 { MONO_MT_UINT32, "Processor" },
63 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 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 static MonoMetaTable AssemblyRefProcessorSchema [] = {
85 { MONO_MT_UINT32, "Processor" },
86 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
90 static MonoMetaTable ClassLayoutSchema [] = {
91 { MONO_MT_UINT16, "PackingSize" },
92 { MONO_MT_UINT32, "ClassSize" },
93 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
97 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 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 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 static MonoMetaTable EventMapSchema [] = {
120 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
121 { MONO_MT_TABLE_IDX, "EventList:Event" },
122 { MONO_MT_END, NULL }
125 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 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 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 static MonoMetaTable FieldLayoutSchema [] = {
148 { MONO_MT_UINT32, "Offset" },
149 { MONO_MT_TABLE_IDX, "Field:Field" },
150 { MONO_MT_END, NULL }
153 static MonoMetaTable FieldMarshalSchema [] = {
154 { MONO_MT_HFM_IDX, "Parent" },
155 { MONO_MT_BLOB_IDX, "NativeType" },
156 { MONO_MT_END, NULL }
158 static MonoMetaTable FieldRVASchema [] = {
159 { MONO_MT_UINT32, "RVA" },
160 { MONO_MT_TABLE_IDX, "Field:Field" },
161 { MONO_MT_END, NULL }
164 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 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 static MonoMetaTable InterfaceImplSchema [] = {
180 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
181 { MONO_MT_TDOR_IDX, "Interface=TypeDefOrRef" },
182 { MONO_MT_END, NULL }
185 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 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 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 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 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 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 static MonoMetaTable ModuleRefSchema [] = {
234 { MONO_MT_STRING_IDX, "Name" },
235 { MONO_MT_END, NULL }
238 static MonoMetaTable NestedClassSchema [] = {
239 { MONO_MT_TABLE_IDX, "NestedClass:TypeDef" },
240 { MONO_MT_TABLE_IDX, "EnclosingClass:TypeDef" },
241 { MONO_MT_END, NULL }
244 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 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 static MonoMetaTable PropertyMapSchema [] = {
259 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
260 { MONO_MT_TABLE_IDX, "PropertyList:Property" },
261 { MONO_MT_END, NULL }
264 static MonoMetaTable StandaloneSigSchema [] = {
265 { MONO_MT_BLOB_IDX, "Signature" },
266 { MONO_MT_END, NULL }
269 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 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 static MonoMetaTable TypeSpecSchema [] = {
287 { MONO_MT_BLOB_IDX, "Signature" },
288 { MONO_MT_END, NULL }
292 MonoMetaTable *table;
295 /* 0 */ { ModuleSchema, "Module" },
296 /* 1 */ { TypeRefSchema, "TypeRef" },
297 /* 2 */ { TypeDefSchema, "TypeDef" },
298 /* 3 */ { NULL, NULL },
299 /* 4 */ { FieldSchema, "Field" },
300 /* 5 */ { NULL, NULL },
301 /* 6 */ { MethodSchema, "Method" },
302 /* 7 */ { NULL, NULL },
303 /* 8 */ { ParamSchema, "Param" },
304 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
305 /* A */ { MemberRefSchema, "MemberRef" },
306 /* B */ { ConstantSchema, "Constant" },
307 /* C */ { CustomAttributeSchema, "CustomAttribute" },
308 /* D */ { FieldMarshalSchema, "FieldMarshal" },
309 /* E */ { DeclSecuritySchema, "DeclSecurity" },
310 /* F */ { ClassLayoutSchema, "ClassLayout" },
311 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
312 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
313 /* 12 */ { EventMapSchema, "EventMap" },
314 /* 13 */ { NULL, NULL },
315 /* 14 */ { EventSchema, "Event" },
316 /* 15 */ { PropertyMapSchema, "PropertyMap" },
317 /* 16 */ { NULL, NULL },
318 /* 17 */ { PropertySchema, "PropertyTable" },
319 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
320 /* 19 */ { MethodImplSchema, "MethodImpl" },
321 /* 1A */ { ModuleRefSchema, "ModuleRef" },
322 /* 1B */ { TypeSpecSchema, "TypeSpec" },
323 /* 1C */ { ImplMapSchema, "ImplMap" },
324 /* 1D */ { FieldRVASchema, "FieldRVA" },
325 /* 1E */ { NULL, NULL },
326 /* 1F */ { NULL, NULL },
327 /* 20 */ { AssemblySchema, "Assembly" },
328 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
329 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
330 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
331 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
332 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
333 /* 26 */ { FileSchema, "File" },
334 /* 27 */ { ExportedTypeSchema, "ExportedType" },
335 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
336 /* 29 */ { NestedClassSchema, "NestedClass" },
337 /* 2A */ { NULL, NULL },
338 /* 2B */ { NULL, NULL },
342 * mono_meta_table_name:
343 * @table: table index
345 * Returns the name for the @table index
348 mono_meta_table_name (int table)
350 if ((table < 0) || (table > 0x29))
353 return tables [table].name;
356 /* The guy who wrote the spec for this should not be allowed near a
359 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
360 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
361 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
362 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
363 inverse of this mapping.
366 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
367 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
369 /* Reference: Partition II - 23.2.6 */
371 * mono_metadata_compute_size:
372 * @meta: metadata context
373 * @tableindex: metadata table number
374 * @result_bitfield: pointer to guint32 where to store additional info
376 * mono_metadata_compute_size() computes the lenght in bytes of a single
377 * row in a metadata table. The size of each column is encoded in the
378 * @result_bitfield return value along with the number of columns in the table.
379 * the resulting bitfield should be handed to the mono_metadata_table_size()
380 * and mono_metadata_table_count() macros.
383 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
385 guint32 bitfield = 0;
386 int size = 0, field_size;
389 MonoMetaTable *table = tables [tableindex].table;
391 for (i = 0; (code = table [i].code) != MONO_MT_END; i++){
394 field_size = 4; break;
397 field_size = 2; break;
400 field_size = 1; break;
402 case MONO_MT_BLOB_IDX:
403 field_size = meta->idx_blob_wide ? 4 : 2; break;
405 case MONO_MT_STRING_IDX:
406 field_size = meta->idx_string_wide ? 4 : 2; break;
408 case MONO_MT_GUID_IDX:
409 field_size = meta->idx_guid_wide ? 4 : 2; break;
411 case MONO_MT_TABLE_IDX:
412 /* Uhm, a table index can point to other tables besides the current one
413 * so, it's not correct to use the rowcount of the current table to
414 * get the size for this column - lupus
416 switch (tableindex) {
417 case MONO_TABLE_ASSEMBLYREFOS:
419 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
420 case MONO_TABLE_ASSEMBLYPROCESSOR:
422 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
423 case MONO_TABLE_CLASSLAYOUT:
425 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
426 case MONO_TABLE_EVENTMAP:
427 g_assert (i == 0 || i == 1);
428 field_size = i ? idx_size (MONO_TABLE_EVENT):
429 idx_size(MONO_TABLE_TYPEDEF);
431 case MONO_TABLE_EVENT:
433 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
434 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
436 case MONO_TABLE_EXPORTEDTYPE:
438 /* the index is in another metadata file, so it must be 4 */
439 field_size = 4; break;
440 case MONO_TABLE_FIELDLAYOUT:
442 field_size = idx_size (MONO_TABLE_FIELD); break;
443 case MONO_TABLE_FIELDRVA:
445 field_size = idx_size (MONO_TABLE_FIELD); break;
446 case MONO_TABLE_IMPLMAP:
448 field_size = idx_size (MONO_TABLE_MODULEREF); break;
449 case MONO_TABLE_INTERFACEIMPL:
451 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
452 case MONO_TABLE_METHOD:
454 field_size = idx_size (MONO_TABLE_PARAM); break;
455 case MONO_TABLE_METHODIMPL:
457 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
458 case MONO_TABLE_METHODSEMANTICS:
460 field_size = idx_size (MONO_TABLE_METHOD); break;
461 case MONO_TABLE_NESTEDCLASS:
462 g_assert (i == 0 || i == 1);
463 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
464 case MONO_TABLE_PROPERTYMAP:
465 g_assert (i == 0 || i == 1);
466 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
467 idx_size(MONO_TABLE_TYPEDEF);
469 case MONO_TABLE_TYPEDEF:
470 g_assert (i == 4 || i == 5);
471 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
472 idx_size(MONO_TABLE_METHOD);
475 g_assert_not_reached ();
477 if (tableindex != MONO_TABLE_EXPORTEDTYPE && field_size != idx_size (tableindex))
478 g_warning ("size changed (%d to %d)", idx_size (tableindex), field_size);
483 * HasConstant: ParamDef, FieldDef, Property
485 case MONO_MT_CONST_IDX:
486 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
487 meta->tables [MONO_TABLE_FIELD].rows);
488 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
490 /* 2 bits to encode tag */
491 field_size = rtsize (n, 16-2);
495 * HasCustomAttribute: points to any table but
498 case MONO_MT_HASCAT_IDX:
500 * We believe that since the signature and
501 * permission are indexing the Blob heap,
502 * we should consider the blob size first
504 /* I'm not a believer - lupus
505 if (meta->idx_blob_wide){
510 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
511 meta->tables [MONO_TABLE_FIELD].rows);
512 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
513 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
514 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
515 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
516 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
517 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
518 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
519 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
520 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
521 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
522 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
523 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
524 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
525 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
526 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
527 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
528 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
530 /* 5 bits to encode */
531 field_size = rtsize (n, 16-5);
535 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
536 * MemberRef and String.
538 case MONO_MT_CAT_IDX:
539 /* String is a heap, if it is wide, we know the size */
541 if (meta->idx_string_wide){
546 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
547 meta->tables [MONO_TABLE_TYPEDEF].rows);
548 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
549 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
551 /* 3 bits to encode */
552 field_size = rtsize (n, 16-3);
556 * HasDeclSecurity: Typedef, MethodDef, Assembly
558 case MONO_MT_HASDEC_IDX:
559 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
560 meta->tables [MONO_TABLE_METHOD].rows);
561 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
563 /* 2 bits to encode */
564 field_size = rtsize (n, 16-2);
568 * Implementation: File, AssemblyRef, ExportedType
570 case MONO_MT_IMPL_IDX:
571 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
572 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
573 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
575 /* 2 bits to encode tag */
576 field_size = rtsize (n, 16-2);
580 * HasFieldMarshall: FieldDef, ParamDef
582 case MONO_MT_HFM_IDX:
583 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
584 meta->tables [MONO_TABLE_PARAM].rows);
586 /* 1 bit used to encode tag */
587 field_size = rtsize (n, 16-1);
591 * MemberForwarded: FieldDef, MethodDef
594 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
595 meta->tables [MONO_TABLE_METHOD].rows);
597 /* 1 bit used to encode tag */
598 field_size = rtsize (n, 16-1);
602 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
604 * It is TypeDef, _TypeRef_, TypeSpec, instead.
606 case MONO_MT_TDOR_IDX:
607 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
608 meta->tables [MONO_TABLE_TYPEREF].rows);
609 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
611 /* 2 bits to encode */
612 field_size = rtsize (n, 16-2);
616 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
618 case MONO_MT_MRP_IDX:
619 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
620 meta->tables [MONO_TABLE_TYPEREF].rows);
621 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
622 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
623 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
624 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
626 /* 3 bits to encode */
627 field_size = rtsize (n, 16 - 3);
630 case MONO_MT_MDOR_IDX:
633 * MethodDefOrRef: MethodDef, MemberRef
636 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
637 meta->tables [MONO_TABLE_MEMBERREF].rows);
639 /* 1 bit used to encode tag */
640 field_size = rtsize (n, 16-1);
644 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
647 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
648 meta->tables [MONO_TABLE_MODULEREF].rows);
649 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
650 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
652 /* 2 bits used to encode tag (ECMA spec claims 3) */
653 field_size = rtsize (n, 16 - 2);
658 * encode field size as follows (we just need to
665 bitfield |= (field_size-1) << shift;
668 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
671 *result_bitfield = (i << 24) | bitfield;
676 * mono_metadata_compute_table_bases:
677 * @meta: metadata context to compute table values
679 * Computes the table bases for the metadata structure.
680 * This is an internal function used by the image loader code.
683 mono_metadata_compute_table_bases (MonoImage *meta)
686 const char *base = meta->tables_base;
688 for (i = 0; i < 64; i++){
689 if (meta->tables [i].rows == 0)
692 meta->tables [i].row_size = mono_metadata_compute_size (
693 meta, i, &meta->tables [i].size_bitfield);
694 meta->tables [i].base = base;
695 base += meta->tables [i].rows * meta->tables [i].row_size;
700 * mono_metadata_locate:
701 * @meta: metadata context
702 * @table: table code.
703 * @idx: index of element to retrieve from @table.
705 * Returns a pointer to the @idx element in the metadata table
706 * whose code is @table.
709 mono_metadata_locate (MonoImage *meta, int table, int idx)
711 /* idx == 0 refers always to NULL */
712 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
714 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
718 * mono_metadata_locate_token:
719 * @meta: metadata context
720 * @token: metadata token
722 * Returns a pointer to the data in the metadata represented by the
726 mono_metadata_locate_token (MonoImage *meta, guint32 token)
728 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
732 * mono_metadata_get_table:
733 * @table: table to retrieve
735 * Returns the MonoMetaTable structure for table @table
738 mono_metadata_get_table (MonoMetaTableEnum table)
742 g_return_val_if_fail ((x > 0) && (x <= MONO_TABLE_LAST), NULL);
744 return tables [table].table;
748 * mono_metadata_string_heap:
749 * @meta: metadata context
750 * @index: index into the string heap.
752 * Returns: an in-memory pointer to the @index in the string heap.
755 mono_metadata_string_heap (MonoImage *meta, guint32 index)
757 g_return_val_if_fail (index < meta->heap_strings.size, "");
758 return meta->heap_strings.data + index;
762 * mono_metadata_user_string:
763 * @meta: metadata context
764 * @index: index into the user string heap.
766 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
769 mono_metadata_user_string (MonoImage *meta, guint32 index)
771 g_return_val_if_fail (index < meta->heap_us.size, "");
772 return meta->heap_us.data + index;
776 * mono_metadata_blob_heap:
777 * @meta: metadata context
778 * @index: index into the blob.
780 * Returns: an in-memory pointer to the @index in the Blob heap.
783 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
785 g_return_val_if_fail (index < meta->heap_blob.size, "");
786 return meta->heap_blob.data + index;
790 * mono_metadata_guid_heap:
791 * @meta: metadata context
792 * @index: index into the guid heap.
794 * Returns: an in-memory pointer to the @index in the guid heap.
797 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
800 index *= 16; /* adjust for guid size and 1-based index */
801 g_return_val_if_fail (index < meta->heap_guid.size, "");
802 return meta->heap_guid.data + index;
806 dword_align (const char *ptr)
808 return (const char *) (((guint32) (ptr + 3)) & ~3);
812 * mono_metadata_decode_row:
813 * @t: table to extract information from.
814 * @idx: index in table.
815 * @res: array of @res_size cols to store the results in
817 * This decompresses the metadata element @idx in table @t
818 * into the guint32 @res array that has res_size elements
821 mono_metadata_decode_row (MonoTableInfo *t, int idx, guint32 *res, int res_size)
823 guint32 bitfield = t->size_bitfield;
824 int i, count = mono_metadata_table_count (bitfield);
825 const char *data = t->base + idx * t->row_size;
827 g_assert (res_size == count);
829 for (i = 0; i < count; i++){
830 int n = mono_metadata_table_size (bitfield, i);
834 res [i] = *data; break;
836 res [i] = read16 (data); break;
839 res [i] = read32 (data); break;
842 g_assert_not_reached ();
849 * mono_metadata_decode_row_col:
850 * @t: table to extract information from.
851 * @idx: index for row in table.
852 * @col: column in the row.
854 * This function returns the value of column @col from the @idx
855 * row in the table @t.
858 mono_metadata_decode_row_col (MonoTableInfo *t, int idx, guint col)
860 guint32 bitfield = t->size_bitfield;
862 register const char *data = t->base + idx * t->row_size;
865 g_assert (col < mono_metadata_table_count (bitfield));
867 n = mono_metadata_table_size (bitfield, 0);
868 for (i = 0; i < col; ++i) {
870 n = mono_metadata_table_size (bitfield, i + 1);
876 return read16 (data);
878 return read32 (data);
880 g_assert_not_reached ();
885 * mono_metadata_decode_blob_size:
886 * @ptr: pointer to a blob object
887 * @rptr: the new position of the pointer
889 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
891 * Returns: the size of the blob object
894 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
896 const unsigned char *ptr = (const unsigned char *)xptr;
899 if ((*ptr & 0x80) == 0){
900 size = ptr [0] & 0x7f;
902 } else if ((*ptr & 0x40) == 0){
903 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
906 size = ((ptr [0] & 0x1f) << 24) +
919 * mono_metadata_decode_value:
920 * @ptr: pointer to decode from
921 * @rptr: the new position of the pointer
923 * This routine decompresses 32-bit values as specified in the "Blob and
924 * Signature" section (22.2)
926 * Returns: the decoded value
929 mono_metadata_decode_value (const char *_ptr, const char **rptr)
931 const unsigned char *ptr = (const unsigned char *) _ptr;
932 unsigned char b = *ptr;
935 if ((b & 0x80) == 0){
938 } else if ((b & 0x40) == 0){
939 len = ((b & 0x3f) << 8 | ptr [1]);
942 len = ((b & 0x1f) << 24) |
955 * mono_metadata_parse_typedef_or_ref:
956 * @m: a metadata context.
957 * @ptr: a pointer to an encoded TypedefOrRef in @m
958 * @rptr: pointer updated to match the end of the decoded stream
960 * Returns: a token valid in the @m metadata decoded from
961 * the compressed representation.
964 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
967 token = mono_metadata_decode_value (ptr, &ptr);
970 return mono_metadata_token_from_dor (token);
974 * mono_metadata_parse_custom_mod:
975 * @m: a metadata context.
976 * @dest: storage where the info about the custom modifier is stored (may be NULL)
977 * @ptr: a pointer to (possibly) the start of a custom modifier list
978 * @rptr: pointer updated to match the end of the decoded stream
980 * Checks if @ptr points to a type custom modifier compressed representation.
982 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
985 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
988 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
989 (*ptr == MONO_TYPE_CMOD_REQD)) {
992 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
993 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1000 * mono_metadata_parse_array:
1001 * @m: a metadata context.
1002 * @ptr: a pointer to an encoded array description.
1003 * @rptr: pointer updated to match the end of the decoded stream
1005 * Decodes the compressed array description found in the metadata @m at @ptr.
1007 * Returns: a #MonoArrayType structure describing the array type
1011 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1014 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1016 array->type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1017 array->rank = mono_metadata_decode_value (ptr, &ptr);
1019 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1020 if (array->numsizes)
1021 array->sizes = g_new0 (int, array->numsizes);
1022 for (i = 0; i < array->numsizes; ++i)
1023 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1025 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1026 if (array->numlobounds)
1027 array->lobounds = g_new0 (int, array->numlobounds);
1028 for (i = 0; i < array->numlobounds; ++i)
1029 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1037 * mono_metadata_free_array:
1038 * @array: array description
1040 * Frees the array description returned from mono_metadata_parse_array().
1043 mono_metadata_free_array (MonoArrayType *array)
1045 mono_metadata_free_type (array->type);
1046 g_free (array->sizes);
1047 g_free (array->lobounds);
1052 * need to add common field and param attributes combinations:
1055 * public static literal
1058 * private static literal
1062 /* data, attrs, type, nmods, byref, pinned */
1063 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1064 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1065 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1066 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1067 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1068 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1069 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1070 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1071 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1072 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1073 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1074 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1075 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1076 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1077 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1078 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1079 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1080 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1081 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1082 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1083 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1084 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1085 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1086 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1087 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1088 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1089 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1090 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1091 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1092 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1093 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1094 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1097 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1099 static GHashTable *type_cache = NULL;
1102 * MonoTypes with modifies are never cached, so we never check or use that field.
1105 mono_type_hash (gconstpointer data)
1107 const MonoType *type = (const MonoType *) data;
1108 return type->type | (type->byref << 8) | (type->attrs << 9);
1112 mono_type_equal (gconstpointer ka, gconstpointer kb)
1114 const MonoType *a = (const MonoType *) ka;
1115 const MonoType *b = (const MonoType *) kb;
1117 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1119 /* need other checks */
1124 * mono_metadata_parse_type:
1125 * @m: metadata context
1126 * @mode: king of type that may be found at @ptr
1127 * @opt_attrs: optional attributes to store in the returned type
1128 * @ptr: pointer to the type representation
1129 * @rptr: pointer updated to match the end of the decoded stream
1131 * Decode a compressed type description found at @ptr in @m.
1132 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1133 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1134 * This function can be used to decode type descriptions in method signatures,
1135 * field signatures, locals signatures etc.
1137 * Returns: a #MonoType structure representing the decoded type.
1140 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs, const char *ptr, const char **rptr)
1142 MonoType *type, *cached;
1146 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1148 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1149 g_hash_table_insert (type_cache, &builtin_types [i], &builtin_types [i]);
1153 case MONO_PARSE_MOD_TYPE:
1154 case MONO_PARSE_PARAM:
1155 case MONO_PARSE_RET:
1156 case MONO_PARSE_LOCAL: /* should not have modifiers according to the spec, but ms tools disagree */
1157 case MONO_PARSE_FIELD: {
1158 /* count the modifiers */
1159 const char *tmp_ptr = ptr;
1161 while (mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr))
1164 type = g_malloc0 (sizeof (MonoType) + (count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1165 type->num_mods = count;
1167 g_warning ("got more than 64 modifiers in type");
1168 /* save them this time */
1170 while (mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr))
1173 } /* fall through */
1175 case MONO_PARSE_TYPE:
1177 * Later we can avoid doing this allocation.
1179 type = g_new0 (MonoType, 1);
1182 g_assert_not_reached ();
1185 type->attrs = opt_attrs;
1186 if (mode == MONO_PARSE_LOCAL) {
1188 * check for pinned flag
1190 if (*ptr == MONO_TYPE_PINNED) {
1197 case MONO_TYPE_BYREF:
1198 if (mode == MONO_PARSE_FIELD)
1199 g_warning ("A field type cannot be byref");
1202 /* follow through */
1204 /*if (*ptr == MONO_TYPE_VOID && mode != MONO_PARSE_RET)
1205 g_error ("void not allowed in param");*/
1206 do_mono_metadata_parse_type (type, m, ptr, &ptr);
1211 if (mode != MONO_PARSE_PARAM && !type->num_mods && (cached = g_hash_table_lookup (type_cache, type))) {
1212 mono_metadata_free_type (type);
1220 * mono_metadata_parse_signature:
1221 * @image: metadata context
1222 * @toke: metadata token
1224 * Decode a method signature stored in the STANDALONESIG table
1226 * Returns: a MonoMethodSignature describing the signature.
1228 MonoMethodSignature *
1229 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1231 MonoTableInfo *tables = image->tables;
1232 guint32 idx = mono_metadata_token_index (token);
1236 if (image->assembly->dynamic)
1237 return mono_lookup_dynamic_token (image, token);
1239 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1241 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1243 ptr = mono_metadata_blob_heap (image, sig);
1244 mono_metadata_decode_blob_size (ptr, &ptr);
1246 return mono_metadata_parse_method_signature (image, FALSE, ptr, NULL);
1249 MonoMethodSignature*
1250 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1252 MonoMethodSignature *sig;
1254 /* later we want to allocate signatures with mempools */
1255 sig = g_malloc0 (sizeof (MonoMethodSignature) + (nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1256 sig->param_count = nparams;
1262 * mono_metadata_parse_method_signature:
1263 * @m: metadata context
1264 * @def: the MethodDef index or 0 for Ref signatures.
1265 * @ptr: pointer to the signature metadata representation
1266 * @rptr: pointer updated to match the end of the decoded stream
1268 * Decode a method signature stored at @ptr.
1270 * Returns: a MonoMethodSignature describing the signature.
1272 MonoMethodSignature *
1273 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1275 MonoMethodSignature *method;
1276 int i, ret_attrs = 0, *pattrs = NULL;
1277 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1283 call_convention = *ptr & 0x0F;
1285 param_count = mono_metadata_decode_value (ptr, &ptr);
1286 pattrs = g_new0 (int, param_count);
1289 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1290 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1291 guint32 cols [MONO_PARAM_SIZE];
1292 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1294 if (def < methodt->rows)
1295 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1297 lastp = paramt->rows;
1298 for (i = param_index; i < lastp; ++i) {
1299 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1300 if (!cols [MONO_PARAM_SEQUENCE])
1301 ret_attrs = cols [MONO_PARAM_FLAGS];
1303 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1306 method = mono_metadata_signature_alloc (m, param_count);
1307 method->hasthis = hasthis;
1308 method->explicit_this = explicit_this;
1309 method->call_convention = call_convention;
1310 method->ret = mono_metadata_parse_type (m, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1312 if (method->param_count) {
1313 method->sentinelpos = -1;
1315 for (i = 0; i < method->param_count; ++i) {
1316 if (*ptr == MONO_TYPE_SENTINEL) {
1317 if (method->call_convention != MONO_CALL_VARARG || def)
1318 g_error ("found sentinel for methoddef or no vararg method");
1319 method->sentinelpos = i;
1322 method->params [i] = mono_metadata_parse_type (m, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1330 * Add signature to a cache and increase ref count...
1336 * mono_metadata_free_method_signature:
1337 * @sig: signature to destroy
1339 * Free the memory allocated in the signature @sig.
1342 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1345 mono_metadata_free_type (sig->ret);
1346 for (i = 0; i < sig->param_count; ++i)
1347 mono_metadata_free_type (sig->params [i]);
1353 * do_mono_metadata_parse_type:
1354 * @type: MonoType to be filled in with the return value
1356 * Internal routine used to "fill" the contents of @type from an
1357 * allocated pointer. This is done this way to avoid doing too
1358 * many mini-allocations (particularly for the MonoFieldType which
1359 * most of the time is just a MonoType, but sometimes might be augmented).
1361 * This routine is used by mono_metadata_parse_type and
1362 * mono_metadata_parse_field_type
1364 * This extracts a Type as specified in Partition II (22.2.12)
1367 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1369 type->type = mono_metadata_decode_value (ptr, &ptr);
1371 switch (type->type){
1372 case MONO_TYPE_VOID:
1373 case MONO_TYPE_BOOLEAN:
1374 case MONO_TYPE_CHAR:
1387 case MONO_TYPE_STRING:
1388 case MONO_TYPE_OBJECT:
1389 case MONO_TYPE_TYPEDBYREF:
1391 case MONO_TYPE_VALUETYPE:
1392 case MONO_TYPE_CLASS: {
1394 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1395 type->data.klass = mono_class_get (m, token);
1398 case MONO_TYPE_SZARRAY:
1400 type->data.type = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1402 case MONO_TYPE_FNPTR:
1403 type->data.method = mono_metadata_parse_method_signature (m, 0, ptr, &ptr);
1405 case MONO_TYPE_ARRAY:
1406 type->data.array = mono_metadata_parse_array (m, ptr, &ptr);
1409 g_error ("type 0x%02x not handled in mono_metadata_parse_type", type->type);
1417 * mono_metadata_free_type:
1418 * @type: type to free
1420 * Free the memory allocated for type @type.
1423 mono_metadata_free_type (MonoType *type)
1425 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1427 switch (type->type){
1428 case MONO_TYPE_SZARRAY:
1430 mono_metadata_free_type (type->data.type);
1432 case MONO_TYPE_FNPTR:
1433 mono_metadata_free_method_signature (type->data.method);
1435 case MONO_TYPE_ARRAY:
1436 mono_metadata_free_array (type->data.array);
1444 hex_dump (const char *buffer, int base, int count)
1446 int show_header = 1;
1454 for (i = 0; i < count; i++){
1457 printf ("\n0x%08x: ", (unsigned char) base + i);
1459 printf ("%02x ", (unsigned char) (buffer [i]));
1466 * @mh: The Method header
1467 * @ptr: Points to the beginning of the Section Data (25.3)
1470 parse_section_data (MonoMethodHeader *mh, const unsigned char *ptr)
1472 unsigned char sect_data_flags;
1473 const unsigned char *sptr;
1475 guint32 sect_data_len;
1478 /* align on 32-bit boundary */
1479 /* FIXME: not 64-bit clean code */
1480 sptr = ptr = dword_align (ptr);
1481 sect_data_flags = *ptr;
1484 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1486 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1489 sect_data_len = ptr [0];
1493 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1494 hex_dump (sptr, 0, sect_data_len+8);
1495 g_print ("\nheader: ");
1496 hex_dump (sptr-4, 0, 4);
1500 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1501 const unsigned char *p = dword_align (ptr);
1503 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1504 /* we could just store a pointer if we don't need to byteswap */
1505 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1506 for (i = 0; i < mh->num_clauses; ++i) {
1507 MonoExceptionClause *ec = &mh->clauses [i];
1510 /* we could memcpy and byteswap */
1511 ec->flags = read32 (p);
1513 ec->try_offset = read32 (p);
1515 ec->try_len = read32 (p);
1517 ec->handler_offset = read32 (p);
1519 ec->handler_len = read32 (p);
1521 ec->token_or_filter = read32 (p);
1524 ec->flags = read16 (p);
1526 ec->try_offset = read16 (p);
1530 ec->handler_offset = read16 (p);
1532 ec->handler_len = *p;
1534 ec->token_or_filter = read32 (p);
1537 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
1541 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
1542 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
1549 * mono_metadata_parse_mh:
1550 * @m: metadata context
1551 * @ptr: pointer to the method header.
1553 * Decode the method header at @ptr, including pointer to the IL code,
1554 * info about local variables and optional exception tables.
1556 * Returns: a MonoMethodHeader.
1559 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
1561 MonoMethodHeader *mh;
1562 unsigned char flags = *(const unsigned char *) ptr;
1563 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
1565 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
1566 const unsigned char *code;
1569 g_return_val_if_fail (ptr != NULL, NULL);
1572 case METHOD_HEADER_TINY_FORMAT:
1573 mh = g_new0 (MonoMethodHeader, 1);
1576 local_var_sig_tok = 0;
1577 mh->code_size = flags >> 2;
1580 case METHOD_HEADER_TINY_FORMAT1:
1581 mh = g_new0 (MonoMethodHeader, 1);
1584 local_var_sig_tok = 0;
1587 * The spec claims 3 bits, but the Beta2 is
1590 mh->code_size = flags >> 2;
1593 case METHOD_HEADER_FAT_FORMAT:
1594 fat_flags = read16 (ptr);
1596 hsize = (fat_flags >> 12) & 0xf;
1597 max_stack = read16 (ptr);
1599 code_size = read32 (ptr);
1601 local_var_sig_tok = read32 (ptr);
1604 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
1611 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
1615 * There are more sections
1617 ptr = code + code_size;
1625 if (local_var_sig_tok) {
1626 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1627 const char *locals_ptr;
1628 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1629 int len=0, i, bsize;
1631 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
1632 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1633 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1634 if (*locals_ptr != 0x07)
1635 g_warning ("wrong signature for locals blob");
1637 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
1638 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1639 mh->num_locals = len;
1640 for (i = 0; i < len; ++i)
1641 mh->locals [i] = mono_metadata_parse_type (m, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
1643 mh = g_new0 (MonoMethodHeader, 1);
1646 mh->code_size = code_size;
1647 mh->max_stack = max_stack;
1648 mh->init_locals = init_locals;
1649 if (fat_flags & METHOD_HEADER_MORE_SECTS)
1650 parse_section_data (mh, (const unsigned char*)ptr);
1655 * mono_metadata_free_mh:
1656 * @mh: a method header
1658 * Free the memory allocated for the method header.
1661 mono_metadata_free_mh (MonoMethodHeader *mh)
1664 for (i = 0; i < mh->num_locals; ++i)
1665 mono_metadata_free_type (mh->locals[i]);
1666 g_free (mh->clauses);
1671 * mono_metadata_parse_field_type:
1672 * @m: metadata context to extract information from
1673 * @ptr: pointer to the field signature
1674 * @rptr: pointer updated to match the end of the decoded stream
1676 * Parses the field signature, and returns the type information for it.
1678 * Returns: The MonoType that was extracted from @ptr.
1681 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
1683 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
1687 * mono_metadata_parse_param:
1688 * @m: metadata context to extract information from
1689 * @ptr: pointer to the param signature
1690 * @rptr: pointer updated to match the end of the decoded stream
1692 * Parses the param signature, and returns the type information for it.
1694 * Returns: The MonoType that was extracted from @ptr.
1697 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
1699 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
1703 * mono_metadata_token_from_dor:
1704 * @dor_token: A TypeDefOrRef coded index
1706 * dor_token is a TypeDefOrRef coded index: it contains either
1707 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
1708 * bits contain an index into the table.
1710 * Returns: an expanded token
1713 mono_metadata_token_from_dor (guint32 dor_index)
1717 table = dor_index & 0x03;
1718 idx = dor_index >> 2;
1721 case 0: /* TypeDef */
1722 return MONO_TOKEN_TYPE_DEF | idx;
1724 case 1: /* TypeRef */
1725 return MONO_TOKEN_TYPE_REF | idx;
1727 case 2: /* TypeSpec */
1728 return MONO_TOKEN_TYPE_SPEC | idx;
1731 g_assert_not_reached ();
1738 * We use this to pass context information to the row locator
1741 int idx; /* The index that we are trying to locate */
1742 int col_idx; /* The index in the row where idx may be stored */
1743 MonoTableInfo *t; /* pointer to the table */
1747 #define CSIZE(x) (sizeof (x) / 4)
1750 * How the row locator works.
1755 * ___|___------> _______
1758 * A column in the rows of table A references an index in table B.
1759 * For example A may be the TYPEDEF table and B the METHODDEF table.
1761 * Given an index in table B we want to get the row in table A
1762 * where the column n references our index in B.
1764 * In the locator_t structure:
1766 * col_idx is the column number
1767 * index is the index in table B
1768 * result will be the index in table A
1771 * Table A Table B column (in table A)
1772 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
1773 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
1774 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
1775 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
1776 * METHODSEM PROPERTY ASSOCIATION (encoded index)
1778 * Note that we still don't support encoded indexes.
1782 typedef_locator (const void *a, const void *b)
1784 locator_t *loc = (locator_t *) a;
1785 const char *bb = (const char *) b;
1786 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
1787 guint32 col, col_next;
1789 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
1795 * Need to check that the next row is valid.
1797 if (typedef_index + 1 < loc->t->rows) {
1798 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
1799 if (loc->idx >= col_next)
1802 if (col == col_next)
1806 loc->result = typedef_index;
1812 table_locator (const void *a, const void *b)
1814 locator_t *loc = (locator_t *) a;
1815 const char *bb = (const char *) b;
1816 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
1819 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
1821 if (loc->idx == col) {
1822 loc->result = table_index;
1832 * mono_metadata_typedef_from_field:
1833 * @meta: metadata context
1834 * @index: FieldDef token
1836 * Returns the 1-based index into the TypeDef table of the type that
1837 * declared the field described by @index.
1838 * Returns 0 if not found.
1841 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
1843 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
1849 loc.idx = mono_metadata_token_index (index);
1850 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
1853 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
1854 g_assert_not_reached ();
1856 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
1857 return loc.result + 1;
1861 * mono_metadata_typedef_from_method:
1862 * @meta: metadata context
1863 * @index: MethodDef token
1865 * Returns the 1-based index into the TypeDef table of the type that
1866 * declared the method described by @index.
1867 * Returns 0 if not found.
1870 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
1872 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
1878 loc.idx = mono_metadata_token_index (index);
1879 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
1882 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
1883 g_assert_not_reached ();
1885 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
1886 return loc.result + 1;
1890 * mono_metadata_interfaces_from_typedef:
1891 * @meta: metadata context
1892 * @index: typedef token
1894 * Returns and array of interfaces that the @index typedef token implements.
1895 * The number of elemnts in the array is returned in @count.
1898 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
1900 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
1903 guint32 cols [MONO_INTERFACEIMPL_SIZE];
1911 loc.idx = mono_metadata_token_index (index);
1912 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
1915 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
1920 * We may end up in the middle of the rows...
1923 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
1930 while (start < tdef->rows) {
1931 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
1932 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
1934 result = g_renew (MonoClass*, result, i + 1);
1935 result [i] = mono_class_get (meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]));
1943 * mono_metadata_nested_in_typedef:
1944 * @meta: metadata context
1945 * @index: typedef token
1947 * Returns: the 1-based index into the TypeDef table of the type
1948 * where the type described by @index is nested.
1949 * Retruns 0 if @index describes a non-nested type.
1952 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
1954 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
1960 loc.idx = mono_metadata_token_index (index);
1961 loc.col_idx = MONO_NESTED_CLASS_NESTED;
1964 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
1967 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
1968 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
1972 * mono_metadata_nesting_typedef:
1973 * @meta: metadata context
1974 * @index: typedef token
1976 * Returns: the 1-based index into the TypeDef table of the first type
1977 * that is nested inside the type described by @index.
1978 * Retruns 0 if @index doesn't have nested types.
1981 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index)
1983 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
1990 loc.idx = mono_metadata_token_index (index);
1991 loc.col_idx = MONO_NESTED_CLASS_ENCLOSING;
1994 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2000 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2006 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2011 * mono_metadata_packing_from_typedef:
2012 * @meta: metadata context
2013 * @index: token representing a type
2015 * Returns the info stored in the ClassLAyout table for the given typedef token
2016 * into the @packing and @size pointers.
2017 * Returns 0 if the info is not found.
2020 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2022 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2024 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2029 loc.idx = mono_metadata_token_index (index);
2030 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2033 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2036 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2038 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2040 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2042 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2043 return loc.result + 1;
2047 #define __alignof__(a) sizeof(a)
2052 * @t: the type to return the size of
2054 * Returns: the number of bytes required to hold an instance of this
2058 mono_type_size (MonoType *t, gint *align)
2065 *align = __alignof__(gpointer);
2066 return sizeof (gpointer);
2070 case MONO_TYPE_VOID:
2073 case MONO_TYPE_BOOLEAN:
2074 *align = __alignof__(gint8);
2078 *align = __alignof__(gint8);
2080 case MONO_TYPE_CHAR:
2083 *align = __alignof__(gint16);
2087 *align = __alignof__(gint32);
2090 *align = __alignof__(float);
2094 *align = __alignof__(gint64);
2097 *align = __alignof__(double);
2101 *align = __alignof__(gpointer);
2102 return sizeof (gpointer);
2103 case MONO_TYPE_STRING:
2104 *align = __alignof__(gpointer);
2105 return sizeof (gpointer);
2106 case MONO_TYPE_OBJECT:
2107 *align = __alignof__(gpointer);
2108 return sizeof (gpointer);
2109 case MONO_TYPE_VALUETYPE: {
2110 if (t->data.klass->enumtype)
2111 return mono_type_size (t->data.klass->enum_basetype, align);
2113 return mono_class_value_size (t->data.klass, align);
2115 case MONO_TYPE_CLASS:
2116 case MONO_TYPE_SZARRAY:
2118 case MONO_TYPE_FNPTR:
2119 case MONO_TYPE_ARRAY:
2120 case MONO_TYPE_TYPEDBYREF: /* we may want to use a struct {MonoType* type, void *data } instead ...*/
2121 *align = __alignof__(gpointer);
2122 return sizeof (gpointer);
2124 g_error ("type 0x%02x unknown", t->type);
2130 * mono_type_stack_size:
2131 * @t: the type to return the size it uses on the stack
2133 * Returns: the number of bytes required to hold an instance of this
2134 * type on the runtime stack
2137 mono_type_stack_size (MonoType *t, gint *align)
2141 g_assert (t != NULL);
2147 *align = __alignof__(gpointer);
2148 return sizeof (gpointer);
2152 case MONO_TYPE_BOOLEAN:
2153 case MONO_TYPE_CHAR:
2162 case MONO_TYPE_STRING:
2163 case MONO_TYPE_OBJECT:
2164 case MONO_TYPE_CLASS:
2165 case MONO_TYPE_SZARRAY:
2167 case MONO_TYPE_FNPTR:
2168 case MONO_TYPE_ARRAY:
2169 case MONO_TYPE_TYPEDBYREF:
2170 *align = __alignof__(gpointer);
2171 return sizeof (gpointer);
2173 *align = __alignof__(float);
2174 return sizeof (float);
2177 *align = __alignof__(gint64);
2178 return sizeof (gint64);
2180 *align = __alignof__(double);
2181 return sizeof (double);
2182 case MONO_TYPE_VALUETYPE: {
2185 if (t->data.klass->enumtype)
2186 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2188 size = mono_class_value_size (t->data.klass, align);
2190 *align = *align + __alignof__(gpointer) - 1;
2191 *align &= ~(__alignof__(gpointer) - 1);
2197 g_error ("type 0x%02x unknown", t->type);
2203 * mono_type_native_stack_size:
2204 * @t: the type to return the size it uses on the stack
2206 * Returns: the number of bytes required to hold an instance of this
2207 * type on the native stack
2210 mono_type_native_stack_size (MonoType *t, gint *align)
2214 g_assert (t != NULL);
2225 case MONO_TYPE_BOOLEAN:
2226 case MONO_TYPE_CHAR:
2235 case MONO_TYPE_STRING:
2236 case MONO_TYPE_OBJECT:
2237 case MONO_TYPE_CLASS:
2238 case MONO_TYPE_SZARRAY:
2240 case MONO_TYPE_FNPTR:
2241 case MONO_TYPE_ARRAY:
2242 case MONO_TYPE_TYPEDBYREF:
2253 case MONO_TYPE_VALUETYPE: {
2256 if (t->data.klass->enumtype)
2257 return mono_type_native_stack_size (t->data.klass->enum_basetype, align);
2259 size = mono_class_native_size (t->data.klass, align);
2260 *align = *align + 3;
2267 g_error ("type 0x%02x unknown", t->type);
2273 * mono_metadata_type_hash:
2276 * Computes an hash value for @t1 to be used in GHashTable.
2279 mono_metadata_type_hash (MonoType *t1)
2281 guint hash = t1->type;
2283 hash |= t1->byref << 6; /* do not collide with t1->type values */
2285 case MONO_TYPE_VALUETYPE:
2286 case MONO_TYPE_CLASS:
2287 /* check if the distribution is good enough */
2288 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2290 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2291 case MONO_TYPE_SZARRAY:
2292 if (t1->data.type->type == MONO_TYPE_OBJECT)
2293 return ((hash << 5) - hash);
2294 return ((hash << 5) - hash) ^ (gint32)(t1->data.type->data.klass);
2295 case MONO_TYPE_ARRAY:
2296 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.array->type);
2302 * mono_metadata_type_equal:
2306 * Determine if @t1 and @t2 represent the same type.
2307 * Returns: #TRUE if @t1 and @t2 are equal.
2310 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2312 if (t1->type != t2->type ||
2313 t1->byref != t2->byref)
2317 case MONO_TYPE_VOID:
2318 case MONO_TYPE_BOOLEAN:
2319 case MONO_TYPE_CHAR:
2330 case MONO_TYPE_STRING:
2333 case MONO_TYPE_OBJECT:
2334 case MONO_TYPE_TYPEDBYREF:
2336 case MONO_TYPE_VALUETYPE:
2337 case MONO_TYPE_CLASS:
2338 return t1->data.klass == t2->data.klass;
2340 return mono_metadata_type_equal (t1->data.type, t2->data.type);
2341 case MONO_TYPE_SZARRAY:
2343 if (t1->data.type->type != t2->data.type->type)
2345 if (t1->data.type->type == MONO_TYPE_CLASS || t1->data.type->type == MONO_TYPE_VALUETYPE)
2346 return t1->data.type->data.klass == t2->data.type->data.klass;
2347 if (t1->data.type->type == MONO_TYPE_SZARRAY) {
2352 return mono_metadata_type_equal (t1->data.type, t2->data.type);
2353 case MONO_TYPE_ARRAY:
2354 if (t1->data.array->rank != t2->data.array->rank)
2356 return mono_metadata_type_equal (t1->data.array->type, t2->data.array->type);
2358 g_error ("implement type compare for %0x!", t1->type);
2366 * mono_metadata_signature_equal:
2367 * @sig1: a signature
2368 * @sig2: another signature
2370 * Determine if @sig1 and @sig2 represent the same signature, with the
2371 * same number of arguments and the same types.
2372 * Returns: #TRUE if @sig1 and @sig2 are equal.
2375 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2379 if (sig1->hasthis != sig2->hasthis ||
2380 sig1->param_count != sig2->param_count)
2383 for (i = 0; i < sig1->param_count; i++) {
2384 MonoType *p1 = sig1->params[i];
2385 MonoType *p2 = sig2->params[i];
2387 //if (p1->attrs != p2->attrs)
2390 if (!mono_metadata_type_equal (p1, p2))
2394 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
2400 mono_signature_hash (MonoMethodSignature *sig)
2402 guint i, res = sig->ret->type;
2404 for (i = 0; i < sig->param_count; i++)
2405 res = (res << 5) - res + sig->params[i]->type;
2411 * mono_metadata_encode_value:
2412 * @value: value to encode
2413 * @buf: buffer where to write the compressed representation
2414 * @endbuf: pointer updated to point at the end of the encoded output
2416 * Encodes the value @value in the compressed representation used
2417 * in metadata and stores the result in @buf. @buf needs to be big
2418 * enough to hold the data (4 bytes).
2421 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
2427 else if (value <= 16384) {
2428 p [0] = 0x80 | (value >> 8);
2429 p [1] = value & 0xff;
2432 p [0] = (value >> 24) | 0xc0;
2433 p [1] = (value >> 16) & 0xff;
2434 p [2] = (value >> 8) & 0xff;
2435 p [3] = value & 0xff;
2443 * mono_metadata_field_info:
2444 * @meta: the Image the field is defined in
2445 * @index: the index in the field table representing the field
2446 * @offset: a pointer to an integer where to store the offset that
2447 * may have been specified for the field in a FieldLayout table
2448 * @rva: a pointer to the RVA of the field data in the image that
2449 * may have been defined in a FieldRVA table
2450 * @marshal_spec: a pointer to the marshal spec that may have been
2451 * defined for the field in a FieldMarshal table.
2453 * Gather info for field @index that may have been defined in the FieldLayout,
2454 * FieldRVA and FieldMarshal tables.
2455 * Either of offset, rva and marshal_spec can be NULL if you're not interested
2459 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
2460 MonoMarshalSpec **marshal_spec)
2462 MonoTableInfo *tdef;
2465 loc.idx = index + 1;
2467 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
2469 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
2472 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2473 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
2475 *offset = (guint32)-1;
2479 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
2481 loc.col_idx = MONO_FIELD_RVA_FIELD;
2484 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2486 * LAMESPEC: There is no signature, no nothing, just the raw data.
2488 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
2496 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
2497 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
2504 * mono_metadata_get_constant_index:
2505 * @meta: the Image the field is defined in
2506 * @index: the token that may have a row defined in the constants table
2508 * @token must be a FieldDef, ParamDef or PropertyDef token.
2510 * Returns: the index into the Constsnts table or 0 if not found.
2513 mono_metadata_get_constant_index (MonoImage *meta, guint32 token)
2515 MonoTableInfo *tdef;
2517 guint32 index = mono_metadata_token_index (token);
2519 tdef = &meta->tables [MONO_TABLE_CONSTANT];
2520 index <<= HASCONSTANT_BITS;
2521 switch (mono_metadata_token_table (token)) {
2522 case MONO_TABLE_FIELD:
2523 index |= HASCONSTANT_FIEDDEF;
2525 case MONO_TABLE_PARAM:
2526 index |= HASCONSTANT_PARAM;
2528 case MONO_TABLE_PROPERTY:
2529 index |= HASCONSTANT_PROPERTY;
2532 g_warning ("Not a valid token for the constant table: 0x%08x", token);
2536 loc.col_idx = MONO_CONSTANT_PARENT;
2539 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2540 return loc.result + 1;
2546 * mono_metadata_events_from_typedef:
2547 * @meta: metadata context
2548 * @index: 0-based index (in the TypeDef table) describing a type
2550 * Returns: the 0-based index in the Event table for the events in the
2551 * type. The last event that belongs to the type (plus 1) is stored
2552 * in the @end_idx pointer.
2555 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2559 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
2567 loc.col_idx = MONO_EVENT_MAP_PARENT;
2568 loc.idx = index + 1;
2570 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2573 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
2574 if (loc.result + 1 < tdef->rows) {
2575 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
2577 end = meta->tables [MONO_TABLE_EVENT].rows;
2585 * mono_metadata_methods_from_event:
2586 * @meta: metadata context
2587 * @index: 0-based index (in the Event table) describing a event
2589 * Returns: the 0-based index in the MethodDef table for the methods in the
2590 * event. The last method that belongs to the event (plus 1) is stored
2591 * in the @end_idx pointer.
2594 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
2598 guint32 cols [MONO_METHOD_SEMA_SIZE];
2599 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2606 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2607 loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_EVENT; /* Method association coded index */
2609 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2614 * We may end up in the middle of the rows...
2617 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2623 while (end < msemt->rows) {
2624 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2625 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2634 * mono_metadata_properties_from_typedef:
2635 * @meta: metadata context
2636 * @index: 0-based index (in the TypeDef table) describing a type
2638 * Returns: the 0-based index in the Property table for the properties in the
2639 * type. The last property that belongs to the type (plus 1) is stored
2640 * in the @end_idx pointer.
2643 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2647 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
2655 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
2656 loc.idx = index + 1;
2658 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2661 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
2662 if (loc.result + 1 < tdef->rows) {
2663 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
2665 end = meta->tables [MONO_TABLE_PROPERTY].rows;
2673 * mono_metadata_methods_from_property:
2674 * @meta: metadata context
2675 * @index: 0-based index (in the PropertyDef table) describing a property
2677 * Returns: the 0-based index in the MethodDef table for the methods in the
2678 * property. The last method that belongs to the property (plus 1) is stored
2679 * in the @end_idx pointer.
2682 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
2686 guint32 cols [MONO_METHOD_SEMA_SIZE];
2687 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2694 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2695 loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_PROPERTY; /* Method association coded index */
2697 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2702 * We may end up in the middle of the rows...
2705 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2711 while (end < msemt->rows) {
2712 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2713 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2722 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
2725 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
2731 loc.col_idx = MONO_IMPLMAP_MEMBER;
2732 loc.idx = ((method_idx + 1) << MEMBERFORWD_BITS) | MEMBERFORWD_METHODDEF;
2734 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2737 return loc.result + 1;
2741 * @image: context where the image is created
2742 * @type_spec: typespec token
2744 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
2748 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
2750 guint32 idx = mono_metadata_token_index (type_spec);
2752 guint32 cols [MONO_TYPESPEC_SIZE];
2757 t = &image->tables [MONO_TABLE_TYPESPEC];
2759 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
2760 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
2761 len = mono_metadata_decode_value (ptr, &ptr);
2762 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
2768 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
2770 MonoMarshalSpec *res;
2772 const char *start = ptr;
2774 /* fixme: this is incomplete, but I cant find more infos in the specs */
2776 res = g_new0 (MonoMarshalSpec, 1);
2778 len = mono_metadata_decode_value (ptr, &ptr);
2779 res->native = *ptr++;
2781 if (res->native == MONO_NATIVE_LPARRAY) {
2782 if (ptr - start <= len)
2783 res->elem_type = *ptr++;
2784 if (ptr - start <= len)
2785 res->param_num = mono_metadata_decode_value (ptr, &ptr);
2786 if (ptr - start <= len)
2787 res->num_elem = mono_metadata_decode_value (ptr, &ptr);
2790 if (res->native == MONO_NATIVE_BYVALTSTR) {
2791 if (ptr - start <= len)
2792 res->num_elem = mono_metadata_decode_value (ptr, &ptr);
2795 if (res->native == MONO_NATIVE_BYVALARRAY) {
2796 if (ptr - start <= len)
2797 res->num_elem = mono_metadata_decode_value (ptr, &ptr);
2804 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
2805 gboolean unicode, MonoMarshalConv *conv)
2807 MonoMarshalConv dummy_conv;
2813 *conv = MONO_MARSHAL_CONV_NONE;
2816 return MONO_NATIVE_UINT;
2820 case MONO_TYPE_BOOLEAN:
2822 switch (mspec->native) {
2823 case MONO_NATIVE_VARIANTBOOL:
2824 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
2825 return MONO_NATIVE_VARIANTBOOL;
2826 case MONO_NATIVE_BOOLEAN:
2827 *conv = MONO_MARSHAL_CONV_BOOL_I4;
2828 return MONO_NATIVE_BOOLEAN;
2829 case MONO_NATIVE_U1:
2830 return MONO_NATIVE_U1;
2832 g_error ("cant marshal bool to native type %02x", mspec->native);
2835 *conv = MONO_MARSHAL_CONV_BOOL_I4;
2836 return MONO_NATIVE_BOOLEAN;
2837 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
2838 case MONO_TYPE_I1: return MONO_NATIVE_I1;
2839 case MONO_TYPE_U1: return MONO_NATIVE_U1;
2840 case MONO_TYPE_I2: return MONO_NATIVE_I2;
2841 case MONO_TYPE_U2: return MONO_NATIVE_U2;
2842 case MONO_TYPE_I4: return MONO_NATIVE_I4;
2843 case MONO_TYPE_U4: return MONO_NATIVE_U4;
2844 case MONO_TYPE_I8: return MONO_NATIVE_I8;
2845 case MONO_TYPE_U8: return MONO_NATIVE_U8;
2846 case MONO_TYPE_R4: return MONO_NATIVE_R4;
2847 case MONO_TYPE_R8: return MONO_NATIVE_R8;
2848 case MONO_TYPE_STRING:
2850 switch (mspec->native) {
2851 case MONO_NATIVE_BSTR:
2852 *conv = MONO_MARSHAL_CONV_STR_BSTR;
2853 return MONO_NATIVE_BSTR;
2854 case MONO_NATIVE_LPSTR:
2855 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
2856 return MONO_NATIVE_LPSTR;
2857 case MONO_NATIVE_LPWSTR:
2858 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
2859 return MONO_NATIVE_LPWSTR;
2860 case MONO_NATIVE_LPTSTR:
2861 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
2862 return MONO_NATIVE_LPTSTR;
2863 case MONO_NATIVE_ANSIBSTR:
2864 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
2865 return MONO_NATIVE_ANSIBSTR;
2866 case MONO_NATIVE_TBSTR:
2867 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
2868 return MONO_NATIVE_TBSTR;
2869 case MONO_NATIVE_BYVALTSTR:
2871 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
2873 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
2874 return MONO_NATIVE_BYVALTSTR;
2876 g_error ("cant marshal string to native type %02x", mspec->native);
2879 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
2880 return MONO_NATIVE_LPTSTR;
2881 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
2882 case MONO_TYPE_VALUETYPE: /*FIXME*/
2883 if (type->data.klass->enumtype) {
2884 t = type->data.klass->enum_basetype->type;
2887 return MONO_NATIVE_STRUCT;
2888 case MONO_TYPE_SZARRAY:
2889 case MONO_TYPE_ARRAY:
2891 switch (mspec->native) {
2892 case MONO_NATIVE_BYVALARRAY:
2893 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
2894 return MONO_NATIVE_BYVALARRAY;
2895 case MONO_NATIVE_SAFEARRAY:
2896 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
2897 return MONO_NATIVE_SAFEARRAY;
2898 case MONO_NATIVE_LPARRAY:
2899 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
2900 return MONO_NATIVE_LPARRAY;
2902 g_error ("cant marshal array as native type %02x", mspec->native);
2906 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
2907 return MONO_NATIVE_LPARRAY;
2908 case MONO_TYPE_I: return MONO_NATIVE_INT;
2909 case MONO_TYPE_U: return MONO_NATIVE_UINT;
2910 case MONO_TYPE_CLASS:
2911 case MONO_TYPE_OBJECT: {
2912 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
2914 switch (mspec->native) {
2915 case MONO_NATIVE_STRUCT:
2916 return MONO_NATIVE_STRUCT;
2917 case MONO_NATIVE_INTERFACE:
2918 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
2919 return MONO_NATIVE_INTERFACE;
2920 case MONO_NATIVE_IDISPATCH:
2921 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
2922 return MONO_NATIVE_IDISPATCH;
2923 case MONO_NATIVE_IUNKNOWN:
2924 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
2925 return MONO_NATIVE_IUNKNOWN;
2927 g_error ("cant marshal object as native type %02x", mspec->native);
2930 if (type->data.klass->parent == mono_defaults.multicastdelegate_class) {
2931 *conv = MONO_MARSHAL_CONV_DEL_FTN;
2932 return MONO_NATIVE_FUNC;
2934 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
2935 return MONO_NATIVE_STRUCT;
2937 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
2938 case MONO_TYPE_TYPEDBYREF:
2940 g_error ("type 0x%02x not handled in marshal", t);
2942 return MONO_NATIVE_MAX;
2946 mono_marshal_type_size (MonoType *type, MonoMarshalSpec *mspec, gint32 *align,
2947 gboolean as_field, gboolean unicode)
2949 MonoMarshalNative native_type = mono_type_to_unmanaged (type, mspec, as_field, unicode, NULL);
2952 switch (native_type) {
2953 case MONO_NATIVE_BOOLEAN:
2956 case MONO_NATIVE_I1:
2957 case MONO_NATIVE_U1:
2960 case MONO_NATIVE_I2:
2961 case MONO_NATIVE_U2:
2964 case MONO_NATIVE_I4:
2965 case MONO_NATIVE_U4:
2966 case MONO_NATIVE_ERROR:
2969 case MONO_NATIVE_I8:
2970 case MONO_NATIVE_U8:
2973 case MONO_NATIVE_R4:
2976 case MONO_NATIVE_R8:
2979 case MONO_NATIVE_INT:
2980 case MONO_NATIVE_UINT:
2981 case MONO_NATIVE_LPSTR:
2982 case MONO_NATIVE_LPWSTR:
2983 case MONO_NATIVE_LPTSTR:
2984 case MONO_NATIVE_BSTR:
2985 case MONO_NATIVE_ANSIBSTR:
2986 case MONO_NATIVE_TBSTR:
2987 case MONO_NATIVE_LPARRAY:
2988 case MONO_NATIVE_SAFEARRAY:
2989 case MONO_NATIVE_IUNKNOWN:
2990 case MONO_NATIVE_IDISPATCH:
2991 case MONO_NATIVE_INTERFACE:
2992 case MONO_NATIVE_ASANY:
2993 case MONO_NATIVE_VARIANTBOOL:
2994 case MONO_NATIVE_FUNC:
2995 case MONO_NATIVE_LPSTRUCT:
2997 return sizeof (gpointer);
2998 case MONO_NATIVE_STRUCT:
2999 klass = mono_class_from_mono_type (type);
3000 return mono_class_native_size (klass, align);
3001 case MONO_NATIVE_BYVALTSTR: {
3002 int esize = unicode ? 2: 1;
3005 return mspec->num_elem * esize;
3007 case MONO_NATIVE_BYVALARRAY: {
3009 klass = mono_class_from_mono_type (type);
3010 esize = mono_class_native_size (klass->element_class, align);
3012 return mspec->num_elem * esize;
3014 case MONO_NATIVE_CURRENCY:
3015 case MONO_NATIVE_VBBYREFSTR:
3016 case MONO_NATIVE_CUSTOM:
3018 g_error ("native type %02x not implemented", native_type);
3021 g_assert_not_reached ();
3026 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3029 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3035 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3036 loc.idx = ((idx + 1) << HAS_FIELD_MARSHAL_BITS) | (is_field? HAS_FIELD_MARSHAL_FIELDSREF: HAS_FIELD_MARSHAL_PARAMDEF);
3038 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3041 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3045 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3047 guint32 idx = tok >> METHODDEFORREF_BITS;
3048 switch (tok & METHODDEFORREF_MASK) {
3049 case METHODDEFORREF_METHODDEF:
3050 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3051 case METHODDEFORREF_METHODREF:
3052 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3054 g_assert_not_reached ();
3059 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3062 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3065 guint32 cols [MONO_METHODIMPL_SIZE];
3066 MonoMethod **result;
3075 loc.col_idx = MONO_METHODIMPL_CLASS;
3076 loc.idx = mono_metadata_token_index (type_token);
3078 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3084 * We may end up in the middle of the rows...
3087 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3092 while (end < tdef->rows) {
3093 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3099 result = g_new (MonoMethod*, num * 2);
3100 for (i = 0; i < num; ++i) {
3101 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3102 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3103 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3107 *num_overrides = num;
3112 * mono_guid_to_string:
3114 * Converts a 16 byte Microsoft GUID to the standard string representation.
3117 mono_guid_to_string (const guint8 *guid)
3119 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3120 guid[3], guid[2], guid[1], guid[0],
3124 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);