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},
1095 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1096 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0}
1099 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1101 static GHashTable *type_cache = NULL;
1104 * MonoTypes with modifies are never cached, so we never check or use that field.
1107 mono_type_hash (gconstpointer data)
1109 const MonoType *type = (const MonoType *) data;
1110 return type->type | (type->byref << 8) | (type->attrs << 9);
1114 mono_type_equal (gconstpointer ka, gconstpointer kb)
1116 const MonoType *a = (const MonoType *) ka;
1117 const MonoType *b = (const MonoType *) kb;
1119 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1121 /* need other checks */
1126 * mono_metadata_parse_type:
1127 * @m: metadata context
1128 * @mode: king of type that may be found at @ptr
1129 * @opt_attrs: optional attributes to store in the returned type
1130 * @ptr: pointer to the type representation
1131 * @rptr: pointer updated to match the end of the decoded stream
1133 * Decode a compressed type description found at @ptr in @m.
1134 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1135 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1136 * This function can be used to decode type descriptions in method signatures,
1137 * field signatures, locals signatures etc.
1139 * Returns: a #MonoType structure representing the decoded type.
1142 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs, const char *ptr, const char **rptr)
1144 MonoType *type, *cached;
1148 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1150 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1151 g_hash_table_insert (type_cache, &builtin_types [i], &builtin_types [i]);
1155 case MONO_PARSE_MOD_TYPE:
1156 case MONO_PARSE_PARAM:
1157 case MONO_PARSE_RET:
1158 case MONO_PARSE_LOCAL: /* should not have modifiers according to the spec, but ms tools disagree */
1159 case MONO_PARSE_FIELD: {
1160 /* count the modifiers */
1161 const char *tmp_ptr = ptr;
1163 while (mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr))
1166 type = g_malloc0 (sizeof (MonoType) + (count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1167 type->num_mods = count;
1169 g_warning ("got more than 64 modifiers in type");
1170 /* save them this time */
1172 while (mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr))
1175 } /* fall through */
1177 case MONO_PARSE_TYPE:
1179 * Later we can avoid doing this allocation.
1181 type = g_new0 (MonoType, 1);
1184 g_assert_not_reached ();
1187 type->attrs = opt_attrs;
1188 if (mode == MONO_PARSE_LOCAL) {
1190 * check for pinned flag
1192 if (*ptr == MONO_TYPE_PINNED) {
1199 case MONO_TYPE_BYREF:
1200 if (mode == MONO_PARSE_FIELD)
1201 g_warning ("A field type cannot be byref");
1204 /* follow through */
1206 /*if (*ptr == MONO_TYPE_VOID && mode != MONO_PARSE_RET)
1207 g_error ("void not allowed in param");*/
1208 do_mono_metadata_parse_type (type, m, ptr, &ptr);
1213 if (mode != MONO_PARSE_PARAM && !type->num_mods && (cached = g_hash_table_lookup (type_cache, type))) {
1214 mono_metadata_free_type (type);
1222 * mono_metadata_parse_signature:
1223 * @image: metadata context
1224 * @toke: metadata token
1226 * Decode a method signature stored in the STANDALONESIG table
1228 * Returns: a MonoMethodSignature describing the signature.
1230 MonoMethodSignature *
1231 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1233 MonoTableInfo *tables = image->tables;
1234 guint32 idx = mono_metadata_token_index (token);
1238 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1240 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1242 ptr = mono_metadata_blob_heap (image, sig);
1243 mono_metadata_decode_blob_size (ptr, &ptr);
1245 return mono_metadata_parse_method_signature (image, FALSE, ptr, NULL);
1248 MonoMethodSignature*
1249 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1251 MonoMethodSignature *sig;
1253 /* later we want to allocate signatures with mempools */
1254 sig = g_malloc0 (sizeof (MonoMethodSignature) + (nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1255 sig->param_count = nparams;
1261 * mono_metadata_parse_method_signature:
1262 * @m: metadata context
1263 * @def: the MethodDef index or 0 for Ref signatures.
1264 * @ptr: pointer to the signature metadata representation
1265 * @rptr: pointer updated to match the end of the decoded stream
1267 * Decode a method signature stored at @ptr.
1269 * Returns: a MonoMethodSignature describing the signature.
1271 MonoMethodSignature *
1272 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1274 MonoMethodSignature *method;
1275 int i, ret_attrs = 0, *pattrs = NULL;
1276 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1282 call_convention = *ptr & 0x0F;
1284 param_count = mono_metadata_decode_value (ptr, &ptr);
1285 pattrs = g_new0 (int, param_count);
1288 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1289 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1290 guint32 cols [MONO_PARAM_SIZE];
1291 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1293 if (def < methodt->rows)
1294 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1296 lastp = paramt->rows;
1297 for (i = param_index; i < lastp; ++i) {
1298 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1299 if (!cols [MONO_PARAM_SEQUENCE])
1300 ret_attrs = cols [MONO_PARAM_FLAGS];
1302 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1305 method = mono_metadata_signature_alloc (m, param_count);
1306 method->hasthis = hasthis;
1307 method->explicit_this = explicit_this;
1308 method->call_convention = call_convention;
1309 method->ret = mono_metadata_parse_type (m, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1311 if (method->param_count) {
1312 method->sentinelpos = -1;
1314 for (i = 0; i < method->param_count; ++i) {
1315 if (*ptr == MONO_TYPE_SENTINEL) {
1316 if (method->call_convention != MONO_CALL_VARARG || def)
1317 g_error ("found sentinel for methoddef or no vararg method");
1318 method->sentinelpos = i;
1321 method->params [i] = mono_metadata_parse_type (m, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1329 * Add signature to a cache and increase ref count...
1335 * mono_metadata_free_method_signature:
1336 * @sig: signature to destroy
1338 * Free the memory allocated in the signature @sig.
1341 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1344 mono_metadata_free_type (sig->ret);
1345 for (i = 0; i < sig->param_count; ++i)
1346 mono_metadata_free_type (sig->params [i]);
1352 * do_mono_metadata_parse_type:
1353 * @type: MonoType to be filled in with the return value
1355 * Internal routine used to "fill" the contents of @type from an
1356 * allocated pointer. This is done this way to avoid doing too
1357 * many mini-allocations (particularly for the MonoFieldType which
1358 * most of the time is just a MonoType, but sometimes might be augmented).
1360 * This routine is used by mono_metadata_parse_type and
1361 * mono_metadata_parse_field_type
1363 * This extracts a Type as specified in Partition II (22.2.12)
1366 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1368 type->type = mono_metadata_decode_value (ptr, &ptr);
1370 switch (type->type){
1371 case MONO_TYPE_VOID:
1372 case MONO_TYPE_BOOLEAN:
1373 case MONO_TYPE_CHAR:
1386 case MONO_TYPE_STRING:
1387 case MONO_TYPE_OBJECT:
1388 case MONO_TYPE_TYPEDBYREF:
1390 case MONO_TYPE_VALUETYPE:
1391 case MONO_TYPE_CLASS: {
1393 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1394 type->data.klass = mono_class_get (m, token);
1397 case MONO_TYPE_SZARRAY:
1399 type->data.type = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1401 case MONO_TYPE_FNPTR:
1402 type->data.method = mono_metadata_parse_method_signature (m, 0, ptr, &ptr);
1404 case MONO_TYPE_ARRAY:
1405 type->data.array = mono_metadata_parse_array (m, ptr, &ptr);
1408 g_error ("type 0x%02x not handled in mono_metadata_parse_type", type->type);
1416 * mono_metadata_free_type:
1417 * @type: type to free
1419 * Free the memory allocated for type @type.
1422 mono_metadata_free_type (MonoType *type)
1424 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1426 switch (type->type){
1427 case MONO_TYPE_SZARRAY:
1429 mono_metadata_free_type (type->data.type);
1431 case MONO_TYPE_FNPTR:
1432 mono_metadata_free_method_signature (type->data.method);
1434 case MONO_TYPE_ARRAY:
1435 mono_metadata_free_array (type->data.array);
1443 hex_dump (const char *buffer, int base, int count)
1445 int show_header = 1;
1453 for (i = 0; i < count; i++){
1456 printf ("\n0x%08x: ", (unsigned char) base + i);
1458 printf ("%02x ", (unsigned char) (buffer [i]));
1465 * @mh: The Method header
1466 * @ptr: Points to the beginning of the Section Data (25.3)
1469 parse_section_data (MonoMethodHeader *mh, const unsigned char *ptr)
1471 unsigned char sect_data_flags;
1472 const unsigned char *sptr;
1474 guint32 sect_data_len;
1477 /* align on 32-bit boundary */
1478 /* FIXME: not 64-bit clean code */
1479 sptr = ptr = dword_align (ptr);
1480 sect_data_flags = *ptr;
1483 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1485 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1488 sect_data_len = ptr [0];
1492 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1493 hex_dump (sptr, 0, sect_data_len+8);
1494 g_print ("\nheader: ");
1495 hex_dump (sptr-4, 0, 4);
1499 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1500 const unsigned char *p = dword_align (ptr);
1502 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1503 /* we could just store a pointer if we don't need to byteswap */
1504 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1505 for (i = 0; i < mh->num_clauses; ++i) {
1506 MonoExceptionClause *ec = &mh->clauses [i];
1509 /* we could memcpy and byteswap */
1510 ec->flags = read32 (p);
1512 ec->try_offset = read32 (p);
1514 ec->try_len = read32 (p);
1516 ec->handler_offset = read32 (p);
1518 ec->handler_len = read32 (p);
1520 ec->token_or_filter = read32 (p);
1523 ec->flags = read16 (p);
1525 ec->try_offset = read16 (p);
1529 ec->handler_offset = read16 (p);
1531 ec->handler_len = *p;
1533 ec->token_or_filter = read32 (p);
1536 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
1540 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
1541 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
1548 * mono_metadata_parse_mh:
1549 * @m: metadata context
1550 * @ptr: pointer to the method header.
1552 * Decode the method header at @ptr, including pointer to the IL code,
1553 * info about local variables and optional exception tables.
1555 * Returns: a MonoMethodHeader.
1558 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
1560 MonoMethodHeader *mh;
1561 unsigned char flags = *(const unsigned char *) ptr;
1562 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
1564 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
1565 const unsigned char *code;
1568 g_return_val_if_fail (ptr != NULL, NULL);
1571 case METHOD_HEADER_TINY_FORMAT:
1572 mh = g_new0 (MonoMethodHeader, 1);
1575 local_var_sig_tok = 0;
1576 mh->code_size = flags >> 2;
1579 case METHOD_HEADER_TINY_FORMAT1:
1580 mh = g_new0 (MonoMethodHeader, 1);
1583 local_var_sig_tok = 0;
1586 * The spec claims 3 bits, but the Beta2 is
1589 mh->code_size = flags >> 2;
1592 case METHOD_HEADER_FAT_FORMAT:
1593 fat_flags = read16 (ptr);
1595 hsize = (fat_flags >> 12) & 0xf;
1596 max_stack = read16 (ptr);
1598 code_size = read32 (ptr);
1600 local_var_sig_tok = read32 (ptr);
1603 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
1610 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
1614 * There are more sections
1616 ptr = code + code_size;
1624 if (local_var_sig_tok) {
1625 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1626 const char *locals_ptr;
1627 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1628 int len=0, i, bsize;
1630 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
1631 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1632 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1633 if (*locals_ptr != 0x07)
1634 g_warning ("wrong signature for locals blob");
1636 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
1637 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1638 mh->num_locals = len;
1639 for (i = 0; i < len; ++i)
1640 mh->locals [i] = mono_metadata_parse_type (m, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
1642 mh = g_new0 (MonoMethodHeader, 1);
1645 mh->code_size = code_size;
1646 mh->max_stack = max_stack;
1647 mh->init_locals = init_locals;
1648 if (fat_flags & METHOD_HEADER_MORE_SECTS)
1649 parse_section_data (mh, (const unsigned char*)ptr);
1654 * mono_metadata_free_mh:
1655 * @mh: a method header
1657 * Free the memory allocated for the method header.
1660 mono_metadata_free_mh (MonoMethodHeader *mh)
1663 for (i = 0; i < mh->num_locals; ++i)
1664 mono_metadata_free_type (mh->locals[i]);
1665 g_free (mh->clauses);
1670 * mono_metadata_parse_field_type:
1671 * @m: metadata context to extract information from
1672 * @ptr: pointer to the field signature
1673 * @rptr: pointer updated to match the end of the decoded stream
1675 * Parses the field signature, and returns the type information for it.
1677 * Returns: The MonoType that was extracted from @ptr.
1680 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
1682 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
1686 * mono_metadata_parse_param:
1687 * @m: metadata context to extract information from
1688 * @ptr: pointer to the param signature
1689 * @rptr: pointer updated to match the end of the decoded stream
1691 * Parses the param signature, and returns the type information for it.
1693 * Returns: The MonoType that was extracted from @ptr.
1696 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
1698 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
1702 * mono_metadata_token_from_dor:
1703 * @dor_token: A TypeDefOrRef coded index
1705 * dor_token is a TypeDefOrRef coded index: it contains either
1706 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
1707 * bits contain an index into the table.
1709 * Returns: an expanded token
1712 mono_metadata_token_from_dor (guint32 dor_index)
1716 table = dor_index & 0x03;
1717 idx = dor_index >> 2;
1720 case 0: /* TypeDef */
1721 return MONO_TOKEN_TYPE_DEF | idx;
1723 case 1: /* TypeRef */
1724 return MONO_TOKEN_TYPE_REF | idx;
1726 case 2: /* TypeSpec */
1727 return MONO_TOKEN_TYPE_SPEC | idx;
1730 g_assert_not_reached ();
1737 * We use this to pass context information to the row locator
1740 int idx; /* The index that we are trying to locate */
1741 int col_idx; /* The index in the row where idx may be stored */
1742 MonoTableInfo *t; /* pointer to the table */
1746 #define CSIZE(x) (sizeof (x) / 4)
1749 * How the row locator works.
1754 * ___|___------> _______
1757 * A column in the rows of table A references an index in table B.
1758 * For example A may be the TYPEDEF table and B the METHODDEF table.
1760 * Given an index in table B we want to get the row in table A
1761 * where the column n references our index in B.
1763 * In the locator_t structure:
1765 * col_idx is the column number
1766 * index is the index in table B
1767 * result will be the index in table A
1770 * Table A Table B column (in table A)
1771 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
1772 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
1773 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
1774 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
1775 * METHODSEM PROPERTY ASSOCIATION (encoded index)
1777 * Note that we still don't support encoded indexes.
1781 typedef_locator (const void *a, const void *b)
1783 locator_t *loc = (locator_t *) a;
1784 const char *bb = (const char *) b;
1785 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
1786 guint32 col, col_next;
1788 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
1794 * Need to check that the next row is valid.
1796 if (typedef_index + 1 < loc->t->rows) {
1797 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
1798 if (loc->idx >= col_next)
1801 if (col == col_next)
1805 loc->result = typedef_index;
1811 table_locator (const void *a, const void *b)
1813 locator_t *loc = (locator_t *) a;
1814 const char *bb = (const char *) b;
1815 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
1818 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
1820 if (loc->idx == col) {
1821 loc->result = table_index;
1831 * mono_metadata_typedef_from_field:
1832 * @meta: metadata context
1833 * @index: FieldDef token
1835 * Returns the 1-based index into the TypeDef table of the type that
1836 * declared the field described by @index.
1837 * Returns 0 if not found.
1840 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
1842 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
1848 loc.idx = mono_metadata_token_index (index);
1849 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
1852 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
1853 g_assert_not_reached ();
1855 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
1856 return loc.result + 1;
1860 * mono_metadata_typedef_from_method:
1861 * @meta: metadata context
1862 * @index: MethodDef token
1864 * Returns the 1-based index into the TypeDef table of the type that
1865 * declared the method described by @index.
1866 * Returns 0 if not found.
1869 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
1871 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
1877 loc.idx = mono_metadata_token_index (index);
1878 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
1881 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
1882 g_assert_not_reached ();
1884 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
1885 return loc.result + 1;
1889 * mono_metadata_interfaces_from_typedef:
1890 * @meta: metadata context
1891 * @index: typedef token
1893 * Returns and array of interfaces that the @index typedef token implements.
1894 * The number of elemnts in the array is returned in @count.
1897 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
1899 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
1902 guint32 cols [MONO_INTERFACEIMPL_SIZE];
1910 loc.idx = mono_metadata_token_index (index);
1911 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
1914 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
1919 * We may end up in the middle of the rows...
1922 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
1929 while (start < tdef->rows) {
1930 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
1931 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
1933 result = g_renew (MonoClass*, result, i + 1);
1934 result [i] = mono_class_get (meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]));
1942 * mono_metadata_nested_in_typedef:
1943 * @meta: metadata context
1944 * @index: typedef token
1946 * Returns: the 1-based index into the TypeDef table of the type
1947 * where the type described by @index is nested.
1948 * Retruns 0 if @index describes a non-nested type.
1951 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
1953 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
1959 loc.idx = mono_metadata_token_index (index);
1960 loc.col_idx = MONO_NESTED_CLASS_NESTED;
1963 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
1966 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
1967 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
1971 * mono_metadata_nesting_typedef:
1972 * @meta: metadata context
1973 * @index: typedef token
1975 * Returns: the 1-based index into the TypeDef table of the first type
1976 * that is nested inside the type described by @index.
1977 * Retruns 0 if @index doesn't have nested types.
1980 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index)
1982 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
1989 loc.idx = mono_metadata_token_index (index);
1990 loc.col_idx = MONO_NESTED_CLASS_ENCLOSING;
1993 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
1999 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2005 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2010 * mono_metadata_packing_from_typedef:
2011 * @meta: metadata context
2012 * @index: token representing a type
2014 * Returns the info stored in the ClassLAyout table for the given typedef token
2015 * into the @packing and @size pointers.
2016 * Returns 0 if the info is not found.
2019 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2021 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2023 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2028 loc.idx = mono_metadata_token_index (index);
2029 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2032 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2035 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2037 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2039 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2041 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2042 return loc.result + 1;
2046 #define __alignof__(a) sizeof(a)
2051 * @t: the type to return the size of
2053 * Returns: the number of bytes required to hold an instance of this
2057 mono_type_size (MonoType *t, gint *align)
2064 *align = __alignof__(gpointer);
2065 return sizeof (gpointer);
2069 case MONO_TYPE_VOID:
2072 case MONO_TYPE_BOOLEAN:
2073 *align = __alignof__(gint8);
2077 *align = __alignof__(gint8);
2079 case MONO_TYPE_CHAR:
2082 *align = __alignof__(gint16);
2086 *align = __alignof__(gint32);
2089 *align = __alignof__(float);
2093 *align = __alignof__(gint64);
2096 *align = __alignof__(double);
2100 *align = __alignof__(gpointer);
2101 return sizeof (gpointer);
2102 case MONO_TYPE_STRING:
2103 *align = __alignof__(gpointer);
2104 return sizeof (gpointer);
2105 case MONO_TYPE_OBJECT:
2106 *align = __alignof__(gpointer);
2107 return sizeof (gpointer);
2108 case MONO_TYPE_VALUETYPE: {
2109 if (t->data.klass->enumtype)
2110 return mono_type_size (t->data.klass->enum_basetype, align);
2112 return mono_class_value_size (t->data.klass, align);
2114 case MONO_TYPE_CLASS:
2115 case MONO_TYPE_SZARRAY:
2117 case MONO_TYPE_FNPTR:
2118 case MONO_TYPE_ARRAY:
2119 case MONO_TYPE_TYPEDBYREF: /* we may want to use a struct {MonoType* type, void *data } instead ...*/
2120 *align = __alignof__(gpointer);
2121 return sizeof (gpointer);
2123 g_error ("type 0x%02x unknown", t->type);
2129 * mono_type_stack_size:
2130 * @t: the type to return the size it uses on the stack
2132 * Returns: the number of bytes required to hold an instance of this
2133 * type on the runtime stack
2136 mono_type_stack_size (MonoType *t, gint *align)
2140 g_assert (t != NULL);
2146 *align = __alignof__(gpointer);
2147 return sizeof (gpointer);
2151 case MONO_TYPE_BOOLEAN:
2152 case MONO_TYPE_CHAR:
2161 case MONO_TYPE_STRING:
2162 case MONO_TYPE_OBJECT:
2163 case MONO_TYPE_CLASS:
2164 case MONO_TYPE_SZARRAY:
2166 case MONO_TYPE_FNPTR:
2167 case MONO_TYPE_ARRAY:
2168 case MONO_TYPE_TYPEDBYREF:
2169 *align = __alignof__(gpointer);
2170 return sizeof (gpointer);
2172 *align = __alignof__(float);
2173 return sizeof (float);
2176 *align = __alignof__(gint64);
2177 return sizeof (gint64);
2179 *align = __alignof__(double);
2180 return sizeof (double);
2181 case MONO_TYPE_VALUETYPE: {
2184 if (t->data.klass->enumtype)
2185 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2187 size = mono_class_value_size (t->data.klass, align);
2189 *align = *align + __alignof__(gpointer) - 1;
2190 *align &= ~(__alignof__(gpointer) - 1);
2196 g_error ("type 0x%02x unknown", t->type);
2202 * mono_type_native_stack_size:
2203 * @t: the type to return the size it uses on the stack
2205 * Returns: the number of bytes required to hold an instance of this
2206 * type on the native stack
2209 mono_type_native_stack_size (MonoType *t, gint *align)
2213 g_assert (t != NULL);
2224 case MONO_TYPE_BOOLEAN:
2225 case MONO_TYPE_CHAR:
2234 case MONO_TYPE_STRING:
2235 case MONO_TYPE_OBJECT:
2236 case MONO_TYPE_CLASS:
2237 case MONO_TYPE_SZARRAY:
2239 case MONO_TYPE_FNPTR:
2240 case MONO_TYPE_ARRAY:
2241 case MONO_TYPE_TYPEDBYREF:
2252 case MONO_TYPE_VALUETYPE: {
2255 if (t->data.klass->enumtype)
2256 return mono_type_native_stack_size (t->data.klass->enum_basetype, align);
2258 size = mono_class_native_size (t->data.klass, align);
2259 *align = *align + 3;
2266 g_error ("type 0x%02x unknown", t->type);
2272 * mono_metadata_type_hash:
2275 * Computes an hash value for @t1 to be used in GHashTable.
2278 mono_metadata_type_hash (MonoType *t1)
2280 guint hash = t1->type;
2282 hash |= t1->byref << 6; /* do not collide with t1->type values */
2284 case MONO_TYPE_VALUETYPE:
2285 case MONO_TYPE_CLASS:
2286 /* check if the distribution is good enough */
2287 return hash << 7 | g_str_hash (t1->data.klass->name);
2289 case MONO_TYPE_SZARRAY:
2290 return hash << 7 | mono_metadata_type_hash (t1->data.type);
2296 * mono_metadata_type_equal:
2300 * Determine if @t1 and @t2 represent the same type.
2301 * Returns: #TRUE if @t1 and @t2 are equal.
2304 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2306 if (t1->type != t2->type ||
2307 t1->byref != t2->byref)
2311 case MONO_TYPE_VOID:
2312 case MONO_TYPE_BOOLEAN:
2313 case MONO_TYPE_CHAR:
2324 case MONO_TYPE_STRING:
2327 case MONO_TYPE_OBJECT:
2329 case MONO_TYPE_VALUETYPE:
2330 case MONO_TYPE_CLASS:
2331 return t1->data.klass == t2->data.klass;
2333 case MONO_TYPE_SZARRAY:
2334 return mono_metadata_type_equal (t1->data.type, t2->data.type);
2335 case MONO_TYPE_ARRAY:
2336 if (t1->data.array->rank != t2->data.array->rank)
2338 return mono_metadata_type_equal (t1->data.array->type, t2->data.array->type);
2340 g_error ("implement type compare for %0x!", t1->type);
2348 * mono_metadata_signature_equal:
2349 * @sig1: a signature
2350 * @sig2: another signature
2352 * Determine if @sig1 and @sig2 represent the same signature, with the
2353 * same number of arguments and the same types.
2354 * Returns: #TRUE if @sig1 and @sig2 are equal.
2357 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2361 if (sig1->hasthis != sig2->hasthis ||
2362 sig1->param_count != sig2->param_count)
2365 for (i = 0; i < sig1->param_count; i++) {
2366 MonoType *p1 = sig1->params[i];
2367 MonoType *p2 = sig2->params[i];
2369 //if (p1->attrs != p2->attrs)
2372 if (!mono_metadata_type_equal (p1, p2))
2376 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
2382 mono_signature_hash (MonoMethodSignature *sig)
2384 guint i, res = sig->ret->type;
2386 for (i = 0; i < sig->param_count; i++)
2387 res = (res << 5) - res + sig->params[i]->type;
2393 * mono_metadata_encode_value:
2394 * @value: value to encode
2395 * @buf: buffer where to write the compressed representation
2396 * @endbuf: pointer updated to point at the end of the encoded output
2398 * Encodes the value @value in the compressed representation used
2399 * in metadata and stores the result in @buf. @buf needs to be big
2400 * enough to hold the data (4 bytes).
2403 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
2409 else if (value <= 16384) {
2410 p [0] = 0x80 | (value >> 8);
2411 p [1] = value & 0xff;
2414 p [0] = (value >> 24) | 0xc0;
2415 p [1] = (value >> 16) & 0xff;
2416 p [2] = (value >> 8) & 0xff;
2417 p [3] = value & 0xff;
2425 * mono_metadata_field_info:
2426 * @meta: the Image the field is defined in
2427 * @index: the index in the field table representing the field
2428 * @offset: a pointer to an integer where to store the offset that
2429 * may have been specified for the field in a FieldLayout table
2430 * @rva: a pointer to the RVA of the field data in the image that
2431 * may have been defined in a FieldRVA table
2432 * @marshal_spec: a pointer to the marshal spec that may have been
2433 * defined for the field in a FieldMarshal table.
2435 * Gather info for field @index that may have been defined in the FieldLayout,
2436 * FieldRVA and FieldMarshal tables.
2437 * Either of offset, rva and marshal_spec can be NULL if you're not interested
2441 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
2442 MonoMarshalSpec **marshal_spec)
2444 MonoTableInfo *tdef;
2447 loc.idx = index + 1;
2449 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
2451 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
2454 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2455 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
2457 *offset = (guint32)-1;
2461 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
2463 loc.col_idx = MONO_FIELD_RVA_FIELD;
2466 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2468 * LAMESPEC: There is no signature, no nothing, just the raw data.
2470 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
2478 tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
2480 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
2482 loc.idx = (loc.idx << HAS_FIELD_MARSHAL_BITS) | HAS_FIELD_MARSHAL_FIELDSREF;
2484 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2485 p = mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
2486 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
2488 *marshal_spec = NULL;
2495 * mono_metadata_get_constant_index:
2496 * @meta: the Image the field is defined in
2497 * @index: the token that may have a row defined in the constants table
2499 * @token must be a FieldDef, ParamDef or PropertyDef token.
2501 * Returns: the index into the Constsnts table or 0 if not found.
2504 mono_metadata_get_constant_index (MonoImage *meta, guint32 token)
2506 MonoTableInfo *tdef;
2508 guint32 index = mono_metadata_token_index (token);
2510 tdef = &meta->tables [MONO_TABLE_CONSTANT];
2511 index <<= HASCONSTANT_BITS;
2512 switch (mono_metadata_token_table (token)) {
2513 case MONO_TABLE_FIELD:
2514 index |= HASCONSTANT_FIEDDEF;
2516 case MONO_TABLE_PARAM:
2517 index |= HASCONSTANT_PARAM;
2519 case MONO_TABLE_PROPERTY:
2520 index |= HASCONSTANT_PROPERTY;
2523 g_warning ("Not a valid token for the constant table: 0x%08x", token);
2527 loc.col_idx = MONO_CONSTANT_PARENT;
2530 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2531 return loc.result + 1;
2537 * mono_metadata_events_from_typedef:
2538 * @meta: metadata context
2539 * @index: 0-based index (in the TypeDef table) describing a type
2541 * Returns: the 0-based index in the Event table for the events in the
2542 * type. The last event that belongs to the type (plus 1) is stored
2543 * in the @end_idx pointer.
2546 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2550 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
2558 loc.col_idx = MONO_EVENT_MAP_PARENT;
2559 loc.idx = index + 1;
2561 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2564 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
2565 if (loc.result + 1 < tdef->rows) {
2566 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
2568 end = meta->tables [MONO_TABLE_EVENT].rows;
2576 * mono_metadata_methods_from_event:
2577 * @meta: metadata context
2578 * @index: 0-based index (in the Event table) describing a event
2580 * Returns: the 0-based index in the MethodDef table for the methods in the
2581 * event. The last method that belongs to the event (plus 1) is stored
2582 * in the @end_idx pointer.
2585 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
2589 guint32 cols [MONO_METHOD_SEMA_SIZE];
2590 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2597 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2598 loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_EVENT; /* Method association coded index */
2600 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2605 * We may end up in the middle of the rows...
2608 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2614 while (end < msemt->rows) {
2615 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2616 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2625 * mono_metadata_properties_from_typedef:
2626 * @meta: metadata context
2627 * @index: 0-based index (in the TypeDef table) describing a type
2629 * Returns: the 0-based index in the Property table for the properties in the
2630 * type. The last property that belongs to the type (plus 1) is stored
2631 * in the @end_idx pointer.
2634 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2638 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
2646 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
2647 loc.idx = index + 1;
2649 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2652 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
2653 if (loc.result + 1 < tdef->rows) {
2654 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
2656 end = meta->tables [MONO_TABLE_PROPERTY].rows;
2664 * mono_metadata_methods_from_property:
2665 * @meta: metadata context
2666 * @index: 0-based index (in the PropertyDef table) describing a property
2668 * Returns: the 0-based index in the MethodDef table for the methods in the
2669 * property. The last method that belongs to the property (plus 1) is stored
2670 * in the @end_idx pointer.
2673 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
2677 guint32 cols [MONO_METHOD_SEMA_SIZE];
2678 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2685 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2686 loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_PROPERTY; /* Method association coded index */
2688 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2693 * We may end up in the middle of the rows...
2696 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2702 while (end < msemt->rows) {
2703 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2704 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2713 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
2716 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
2722 loc.col_idx = MONO_IMPLMAP_MEMBER;
2723 loc.idx = ((method_idx + 1) << MEMBERFORWD_BITS) | MEMBERFORWD_METHODDEF;
2725 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2728 return loc.result + 1;
2732 * @image: context where the image is created
2733 * @type_spec: typespec token
2735 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
2739 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
2741 guint32 idx = mono_metadata_token_index (type_spec);
2743 guint32 cols [MONO_TYPESPEC_SIZE];
2748 t = &image->tables [MONO_TABLE_TYPESPEC];
2750 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
2751 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
2752 len = mono_metadata_decode_value (ptr, &ptr);
2753 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
2759 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
2761 MonoMarshalSpec *res;
2763 const char *start = ptr;
2765 /* fixme: this is incomplete, but I cant find more infos in the specs */
2767 res = g_new0 (MonoMarshalSpec, 1);
2769 len = mono_metadata_decode_value (ptr, &ptr);
2770 res->native = *ptr++;
2772 if (res->native == MONO_NATIVE_LPARRAY) {
2773 if (ptr - start <= len)
2774 res->elem_type = *ptr++;
2775 if (ptr - start <= len)
2776 res->param_num = mono_metadata_decode_value (ptr, &ptr);
2777 if (ptr - start <= len)
2778 res->num_elem = mono_metadata_decode_value (ptr, &ptr);
2781 if (res->native == MONO_NATIVE_BYVALTSTR) {
2782 if (ptr - start <= len)
2783 res->num_elem = mono_metadata_decode_value (ptr, &ptr);
2786 if (res->native == MONO_NATIVE_BYVALARRAY) {
2787 if (ptr - start <= len)
2788 res->num_elem = mono_metadata_decode_value (ptr, &ptr);
2795 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
2796 gboolean unicode, MonoMarshalConv *conv)
2798 MonoMarshalConv dummy_conv;
2804 *conv = MONO_MARSHAL_CONV_NONE;
2807 return MONO_NATIVE_UINT;
2811 case MONO_TYPE_BOOLEAN:
2813 switch (mspec->native) {
2814 case MONO_NATIVE_VARIANTBOOL:
2815 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
2816 return MONO_NATIVE_VARIANTBOOL;
2817 case MONO_NATIVE_BOOLEAN:
2818 *conv = MONO_MARSHAL_CONV_BOOL_I4;
2819 return MONO_NATIVE_BOOLEAN;
2820 case MONO_NATIVE_U1:
2821 return MONO_NATIVE_U1;
2823 g_error ("cant marshal bool to native type %02x", mspec->native);
2826 *conv = MONO_MARSHAL_CONV_BOOL_I4;
2827 return MONO_NATIVE_BOOLEAN;
2828 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
2829 case MONO_TYPE_I1: return MONO_NATIVE_I1;
2830 case MONO_TYPE_U1: return MONO_NATIVE_U1;
2831 case MONO_TYPE_I2: return MONO_NATIVE_I2;
2832 case MONO_TYPE_U2: return MONO_NATIVE_U2;
2833 case MONO_TYPE_I4: return MONO_NATIVE_I4;
2834 case MONO_TYPE_U4: return MONO_NATIVE_U4;
2835 case MONO_TYPE_I8: return MONO_NATIVE_I8;
2836 case MONO_TYPE_U8: return MONO_NATIVE_U8;
2837 case MONO_TYPE_R4: return MONO_NATIVE_R4;
2838 case MONO_TYPE_R8: return MONO_NATIVE_R8;
2839 case MONO_TYPE_STRING:
2841 switch (mspec->native) {
2842 case MONO_NATIVE_BSTR:
2843 *conv = MONO_MARSHAL_CONV_STR_BSTR;
2844 return MONO_NATIVE_BSTR;
2845 case MONO_NATIVE_LPSTR:
2846 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
2847 return MONO_NATIVE_LPSTR;
2848 case MONO_NATIVE_LPWSTR:
2849 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
2850 return MONO_NATIVE_LPWSTR;
2851 case MONO_NATIVE_LPTSTR:
2852 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
2853 return MONO_NATIVE_LPTSTR;
2854 case MONO_NATIVE_ANSIBSTR:
2855 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
2856 return MONO_NATIVE_ANSIBSTR;
2857 case MONO_NATIVE_TBSTR:
2858 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
2859 return MONO_NATIVE_TBSTR;
2860 case MONO_NATIVE_BYVALTSTR:
2862 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
2864 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
2865 return MONO_NATIVE_BYVALTSTR;
2867 g_error ("cant marshal string to native type %02x", mspec->native);
2870 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
2871 return MONO_NATIVE_LPTSTR;
2872 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
2873 case MONO_TYPE_VALUETYPE: /*FIXME*/
2874 if (type->data.klass->enumtype) {
2875 t = type->data.klass->enum_basetype->type;
2878 return MONO_NATIVE_STRUCT;
2879 case MONO_TYPE_SZARRAY:
2880 case MONO_TYPE_ARRAY:
2882 switch (mspec->native) {
2883 case MONO_NATIVE_BYVALARRAY:
2884 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
2885 return MONO_NATIVE_BYVALARRAY;
2886 case MONO_NATIVE_SAFEARRAY:
2887 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
2888 return MONO_NATIVE_SAFEARRAY;
2889 case MONO_NATIVE_LPARRAY:
2890 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
2891 return MONO_NATIVE_LPARRAY;
2893 g_error ("cant marshal array as native type %02x", mspec->native);
2897 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
2898 return MONO_NATIVE_LPARRAY;
2899 case MONO_TYPE_I: return MONO_NATIVE_INT;
2900 case MONO_TYPE_U: return MONO_NATIVE_UINT;
2901 case MONO_TYPE_CLASS:
2902 case MONO_TYPE_OBJECT: {
2903 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
2905 switch (mspec->native) {
2906 case MONO_NATIVE_STRUCT:
2907 return MONO_NATIVE_STRUCT;
2908 case MONO_NATIVE_INTERFACE:
2909 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
2910 return MONO_NATIVE_INTERFACE;
2911 case MONO_NATIVE_IDISPATCH:
2912 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
2913 return MONO_NATIVE_IDISPATCH;
2914 case MONO_NATIVE_IUNKNOWN:
2915 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
2916 return MONO_NATIVE_IUNKNOWN;
2918 g_error ("cant marshal object as native type %02x", mspec->native);
2921 if (type->data.klass->parent == mono_defaults.multicastdelegate_class) {
2922 *conv = MONO_MARSHAL_CONV_DEL_FTN;
2923 return MONO_NATIVE_FUNC;
2925 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
2926 return MONO_NATIVE_STRUCT;
2928 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
2929 case MONO_TYPE_TYPEDBYREF:
2931 g_error ("type 0x%02x not handled in marshal", t);
2933 return MONO_NATIVE_MAX;
2937 mono_marshal_type_size (MonoType *type, MonoMarshalSpec *mspec, gint32 *align,
2938 gboolean as_field, gboolean unicode)
2940 MonoMarshalNative native_type = mono_type_to_unmanaged (type, mspec, as_field, unicode, NULL);
2943 switch (native_type) {
2944 case MONO_NATIVE_BOOLEAN:
2947 case MONO_NATIVE_I1:
2948 case MONO_NATIVE_U1:
2951 case MONO_NATIVE_I2:
2952 case MONO_NATIVE_U2:
2955 case MONO_NATIVE_I4:
2956 case MONO_NATIVE_U4:
2957 case MONO_NATIVE_ERROR:
2960 case MONO_NATIVE_I8:
2961 case MONO_NATIVE_U8:
2964 case MONO_NATIVE_R4:
2967 case MONO_NATIVE_R8:
2970 case MONO_NATIVE_INT:
2971 case MONO_NATIVE_UINT:
2972 case MONO_NATIVE_LPSTR:
2973 case MONO_NATIVE_LPWSTR:
2974 case MONO_NATIVE_LPTSTR:
2975 case MONO_NATIVE_BSTR:
2976 case MONO_NATIVE_ANSIBSTR:
2977 case MONO_NATIVE_TBSTR:
2978 case MONO_NATIVE_LPARRAY:
2979 case MONO_NATIVE_SAFEARRAY:
2980 case MONO_NATIVE_IUNKNOWN:
2981 case MONO_NATIVE_IDISPATCH:
2982 case MONO_NATIVE_INTERFACE:
2983 case MONO_NATIVE_ASANY:
2984 case MONO_NATIVE_VARIANTBOOL:
2985 case MONO_NATIVE_FUNC:
2986 case MONO_NATIVE_LPSTRUCT:
2988 return sizeof (gpointer);
2989 case MONO_NATIVE_STRUCT:
2990 klass = mono_class_from_mono_type (type);
2991 return mono_class_native_size (klass, align);
2992 case MONO_NATIVE_BYVALTSTR: {
2993 int esize = unicode ? 2: 1;
2996 return mspec->num_elem * esize;
2998 case MONO_NATIVE_BYVALARRAY: {
3000 klass = mono_class_from_mono_type (type);
3001 esize = mono_class_native_size (klass->element_class, align);
3003 return mspec->num_elem * esize;
3005 case MONO_NATIVE_CURRENCY:
3006 case MONO_NATIVE_VBBYREFSTR:
3007 case MONO_NATIVE_CUSTOM:
3009 g_error ("native type %02x not implemented", native_type);
3012 g_assert_not_reached ();
3017 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3020 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3026 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3027 loc.idx = ((idx + 1) << HAS_FIELD_MARSHAL_BITS) | (is_field? HAS_FIELD_MARSHAL_FIELDSREF: HAS_FIELD_MARSHAL_PARAMDEF);
3029 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3032 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3036 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3038 guint32 idx = tok >> METHODDEFORREF_BITS;
3039 switch (tok & METHODDEFORREF_MASK) {
3040 case METHODDEFORREF_METHODDEF:
3041 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3042 case METHODDEFORREF_METHODREF:
3043 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3045 g_assert_not_reached ();
3050 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3053 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3056 guint32 cols [MONO_METHODIMPL_SIZE];
3057 MonoMethod **result;
3066 loc.col_idx = MONO_METHODIMPL_CLASS;
3067 loc.idx = mono_metadata_token_index (type_token);
3069 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3075 * We may end up in the middle of the rows...
3078 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3083 while (end < tdef->rows) {
3084 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3090 result = g_new (MonoMethod*, num * 2);
3091 for (i = 0; i < num; ++i) {
3092 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3093 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3094 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3098 *num_overrides = num;
3103 * mono_guid_to_string:
3105 * Converts a 16 byte Microsoft GUID to the standard string representation.
3108 mono_guid_to_string (const guint8 *guid)
3110 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3111 guid[3], guid[2], guid[1], guid[0],
3115 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);