2 * get.c: Functions to get stringified values from the metadata tables.
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
18 #include <mono/metadata/class.h>
19 #include <mono/metadata/marshal.h>
21 extern gboolean substitute_with_mscorlib_p;
24 get_memberref_parent (MonoImage *m, guint32 mrp_token);
27 get_typedef (MonoImage *m, int idx)
29 guint32 cols [MONO_TYPEDEF_SIZE];
32 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEDEF], idx - 1, cols, MONO_TYPEDEF_SIZE);
34 ns = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]);
35 return g_strdup_printf (
36 "%s%s%s", ns, *ns?".":"",
37 mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME]));
41 get_module (MonoImage *m, int idx)
43 guint32 cols [MONO_MODULE_SIZE];
46 * There MUST BE only one module in the Module table
50 mono_metadata_decode_row (&m->tables [MONO_TABLE_MODULE], idx - 1, cols, MONO_MODULE_SIZE);
52 return g_strdup (mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]));
56 get_assemblyref (MonoImage *m, int idx)
58 guint32 cols [MONO_ASSEMBLYREF_SIZE];
60 mono_metadata_decode_row (&m->tables [MONO_TABLE_ASSEMBLYREF], idx - 1, cols, MONO_ASSEMBLYREF_SIZE);
62 return g_strdup (mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
67 * Returns a string representing the ArrayShape (22.2.16).
70 get_array_shape (MonoImage *m, const char *ptr, char **result)
72 GString *res = g_string_new ("[");
73 guint32 rank, num_sizes, num_lo_bounds;
74 guint32 *sizes = NULL, *lo_bounds = NULL;
78 rank = mono_metadata_decode_value (ptr, &ptr);
79 num_sizes = mono_metadata_decode_value (ptr, &ptr);
82 sizes = g_new (guint32, num_sizes);
84 for (i = 0; i < num_sizes; i++)
85 sizes [i] = mono_metadata_decode_value (ptr, &ptr);
87 num_lo_bounds = mono_metadata_decode_value (ptr, &ptr);
88 if (num_lo_bounds > 0)
89 lo_bounds = g_new (guint32, num_lo_bounds);
91 for (i = 0; i < num_lo_bounds; i++)
92 lo_bounds [i] = mono_metadata_decode_value (ptr, &ptr);
94 for (r = 0; r < rank; r++){
96 if (r < num_lo_bounds){
97 sprintf (buffer, "%d..%d", lo_bounds [r], lo_bounds [r] + sizes [r] - 1);
99 sprintf (buffer, "0..%d", sizes [r] - 1);
104 g_string_append (res, buffer);
106 g_string_append (res, ", ");
108 g_string_append (res, "]");
117 g_string_free (res, FALSE);
124 * @m: metadata context
125 * @blob_idx: index into the blob heap
127 * Returns the stringified representation of a TypeSpec signature (22.2.17)
130 get_typespec (MonoImage *m, guint32 idx)
132 guint32 cols [MONO_TYPESPEC_SIZE];
135 GString *res = g_string_new ("");
138 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPESPEC], idx-1, cols, MONO_TYPESPEC_SIZE);
139 ptr = mono_metadata_blob_heap (m, cols [MONO_TYPESPEC_SIGNATURE]);
140 len = mono_metadata_decode_value (ptr, &ptr);
144 ptr = get_custom_mod (m, ptr, &s);
146 g_string_append (res, s);
147 g_string_append_c (res, ' ');
151 if (*ptr == MONO_TYPE_VOID)
152 g_string_append (res, "void");
154 ptr = get_type (m, ptr, &s);
156 g_string_append (res, s);
160 case MONO_TYPE_FNPTR:
161 g_string_append (res, "FNPTR ");
163 * we assume MethodRefSig, as we do not know
164 * whether it is a MethodDefSig or a MethodRefSig.
166 printf ("\n FNPTR:\n");
168 hex_dump (ptr, 0, 40);
171 case MONO_TYPE_ARRAY:
172 ptr = get_type (m, ptr, &s);
173 g_string_append (res, s);
175 g_string_append_c (res, ' ');
176 ptr = get_array_shape (m, ptr, &s);
177 g_string_append (res, s);
181 case MONO_TYPE_SZARRAY:
182 ptr = get_custom_mod (m, ptr, &s);
184 g_string_append (res, s);
185 g_string_append_c (res, ' ');
188 ptr = get_type (m, ptr, &s);
189 g_string_append (res, s);
190 g_string_append (res, "[]");
195 g_string_free (res, FALSE);
201 get_typeref (MonoImage *m, int idx)
203 guint32 cols [MONO_TYPEREF_SIZE];
206 guint32 rs_idx, table;
208 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEREF], idx - 1, cols, MONO_TYPEREF_SIZE);
210 t = mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAME]);
211 s = mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAMESPACE]);
213 rs_idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
214 table = cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK;
217 case RESOLTION_SCOPE_MODULE: /* Module */
218 x = get_module (m, rs_idx);
219 ret = g_strdup_printf ("[%s] %s%s%s", x, s, *s?".":"", t);
223 case RESOLTION_SCOPE_MODULEREF: /* ModuleRef */
224 ret = g_strdup_printf ("TODO:TypeRef-ModuleRef (%s.%s)", s, t);
227 case RESOLTION_SCOPE_ASSEMBLYREF: /*
228 * AssemblyRef (ECMA docs claim it is 3, but it looks to
229 * me like it is 2 (tokens are prefixed with 0x23)
231 x = get_assemblyref (m, rs_idx);
232 ret = g_strdup_printf ("[%s]%s%s%s", x, s, *s?".":"", t);
236 case RESOLTION_SCOPE_TYPEREF: /* TypeRef */
237 x = get_typeref (m, rs_idx);
238 ret = g_strdup_printf ("%s/%s", x, t);
243 ret = g_strdup_printf ("Unknown table in TypeRef %d", table);
250 * get_typedef_or_ref:
251 * @m: metadata context
252 * @dor_token: def or ref encoded index
254 * Low two bits contain table to lookup from
255 * high bits contain the index into the def or ref table
257 * Returns: a stringified version of the MethodDef or MethodRef
258 * at (dor_token >> 2)
261 get_typedef_or_ref (MonoImage *m, guint32 dor_token)
263 char *temp = NULL, *s;
267 * low 2 bits contain encoding
269 table = dor_token & TYPEDEFORREF_MASK;
270 idx = dor_token >> TYPEDEFORREF_BITS;
273 case 0: /* TypeDef */
274 temp = get_typedef (m, idx);
275 s = g_strdup_printf ("%s", temp);
278 case 1: /* TypeRef */
279 temp = get_typeref (m, idx);
280 s = g_strdup_printf ("%s", temp);
283 case 2: /* TypeSpec */
284 s = get_typespec (m, idx);
288 g_error ("Unhandled encoding for typedef-or-ref coded index 0x%08x", dor_token);
299 * get_type_or_methdef
300 * @m: metadata context
301 * @dor_token: type or method def encoded index
303 * Low bit contains the table to lookup from
304 * high bits contain the index into the type def or method def table
306 * Returns: a stringified version of the TypeOrMethodDef token
309 get_type_or_methdef (MonoImage *m, guint32 dor_token)
311 if (dor_token & MONO_TYPEORMETHOD_METHOD) /* MethodDef */
312 return get_methoddef (m, dor_token >> MONO_TYPEORMETHOD_BITS);
314 return get_typedef (m, dor_token >> MONO_TYPEORMETHOD_BITS);
318 * get_encoded_typedef_or_ref:
319 * @m: metadata context
320 * @ptr: location to decode from.
321 * @result: pointer to string where resulting decoded string is stored
323 * result will point to a g_malloc()ed string.
325 * Returns: the new ptr to continue decoding
328 get_encoded_typedef_or_ref (MonoImage *m, const char *ptr, char **result)
332 token = mono_metadata_decode_value (ptr, &ptr);
334 *result = get_typedef_or_ref (m, token);
342 * Decodes a CustomMod (22.2.7)
344 * Returns: updated pointer location
347 get_custom_mod (MonoImage *m, const char *ptr, char **return_value)
352 *return_value = NULL;
353 while ((*ptr == MONO_TYPE_CMOD_OPT) ||
354 (*ptr == MONO_TYPE_CMOD_REQD)) {
355 reqd = (*ptr == MONO_TYPE_CMOD_REQD) ? "reqd" : "opt";
357 ptr = get_encoded_typedef_or_ref (m, ptr, &s);
359 if (*return_value == NULL)
360 *return_value = g_strconcat (reqd, " ", s, NULL);
362 *return_value = g_strconcat (*return_value, " ", reqd, " ", s, NULL);
369 static map_t element_type_map [] = {
370 { MONO_TYPE_END , "end" },
371 { MONO_TYPE_VOID , "void" },
372 { MONO_TYPE_BOOLEAN , "bool" },
373 { MONO_TYPE_CHAR , "char" },
374 { MONO_TYPE_I1 , "int8" },
375 { MONO_TYPE_U1 , "unsigned int8" },
376 { MONO_TYPE_I2 , "int16" },
377 { MONO_TYPE_U2 , "unsigned int16" },
378 { MONO_TYPE_I4 , "int32" },
379 { MONO_TYPE_U4 , "unsigned int32" },
380 { MONO_TYPE_I8 , "int64" },
381 { MONO_TYPE_U8 , "unsigned int64" },
382 { MONO_TYPE_R4 , "float32" },
383 { MONO_TYPE_R8 , "float64" },
384 { MONO_TYPE_STRING , "string" },
385 { MONO_TYPE_TYPEDBYREF , "TypedByRef" },
386 { MONO_TYPE_I , "native int" },
387 { MONO_TYPE_U , "native unsigned int" },
388 { MONO_TYPE_OBJECT , "object" },
392 static map_t call_conv_type_map [] = {
393 { MONO_CALL_DEFAULT , "default" },
394 { MONO_CALL_C , "c" },
395 { MONO_CALL_STDCALL , "stdcall" },
396 { MONO_CALL_THISCALL , "thiscall" },
397 { MONO_CALL_FASTCALL , "fastcall" },
398 { MONO_CALL_VARARG , "vararg" },
403 dis_stringify_token (MonoImage *m, guint32 token)
405 guint idx = token & 0xffffff;
406 switch (token >> 24) {
407 case MONO_TABLE_TYPEDEF: return get_typedef (m, idx);
408 case MONO_TABLE_TYPEREF: return get_typeref (m, idx);
409 case MONO_TABLE_TYPESPEC: return get_typespec (m, idx);
413 return g_strdup_printf("0x%08x", token);
417 dis_stringify_array (MonoImage *m, MonoArrayType *array)
420 GString *s = g_string_new("");
423 type = dis_stringify_type (m, &array->eklass->byval_arg);
424 g_string_append (s, type);
426 g_string_append_c (s, '[');
427 for (i = 0; i < array->rank; ++i) {
429 g_string_append_c (s, ',');
430 if (i < array->numsizes) {
431 if (i < array->numlobounds && array->lobounds[i] != 0)
432 g_string_sprintfa (s, "%d..%d", array->lobounds[i], array->sizes[i]);
434 g_string_sprintfa (s, "%d", array->sizes[i]);
437 g_string_append_c (s, ']');
439 g_string_free (s, FALSE);
444 dis_stringify_modifiers (MonoImage *m, int n, MonoCustomMod *mod)
446 GString *s = g_string_new("");
449 for (i = 0; i < n; ++i) {
450 char *tok = dis_stringify_token (m, mod[i].token);
452 g_string_sprintfa (s, " ");
453 g_string_sprintfa (s, "%s (%s)", mod[i].required ? "modopt": "modreq", tok);
456 g_string_append_c (s, ' ');
458 g_string_free (s, FALSE);
463 dis_stringify_param (MonoImage *m, MonoType *param)
467 const char *out = param->attrs & 2 ? "[out] ": "";
468 t = dis_stringify_type (m, param);
469 result = g_strconcat (out, t, NULL);
476 * @m: metadata context
477 * @table_type: The type of table we are getting generics for (0 for typedef, 1 for method)
478 * @row: The row in the table
480 * Returns: Allocated stringified generic parameters
483 get_generic_param (MonoImage *m, int table_type, guint32 row)
485 MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAM];
486 GString *result = g_string_new ("");
488 guint32 cols [MONO_GENERICPARAM_SIZE];
489 int i, own_tok, table, idx, found_count;
491 g_assert (table_type != MONO_TYPEORMETHOD_TYPE || table_type != MONO_TYPEORMETHOD_METHOD);
494 for (i = 1; i <= t->rows; i++) {
495 mono_metadata_decode_row (t, i-1, cols, MONO_GENERICPARAM_SIZE);
496 own_tok = cols [MONO_GENERICPARAM_OWNER];
497 table = own_tok & MONO_TYPEORMETHOD_MASK;
498 idx = own_tok >> MONO_TYPEORMETHOD_BITS;
500 if (table != table_type || idx != row)
503 if (found_count == 0)
504 g_string_append_printf (result, "<%s",
505 mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
507 g_string_append_printf (result, ", %s",
508 mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
513 g_string_append_c (result, '>');
514 retval = result->str;
515 g_string_free (result, FALSE);
520 dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int methoddef_row, gboolean fully_qualified)
522 guint32 cols [MONO_METHOD_SIZE];
523 guint32 pcols [MONO_PARAM_SIZE];
524 guint32 param_index = 0;
525 const char *name = "";
530 GString *result = g_string_new ("");
533 g_assert (method || methoddef_row);
536 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD], methoddef_row -1, cols, MONO_METHOD_SIZE);
538 type = get_typedef (m, mono_metadata_typedef_from_method (m, methoddef_row));
539 name = mono_metadata_string_heap (m, cols [MONO_METHOD_NAME]);
540 param_index = cols [MONO_METHOD_PARAMLIST];
542 const char *sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
543 mono_metadata_decode_blob_size (sig, &sig);
544 method = mono_metadata_parse_method_signature (m, methoddef_row, sig, &sig);
547 gen_param = get_generic_param (m, 1, methoddef_row);
550 retval = dis_stringify_param (m, method->ret);
552 g_string_append (result, "instance ");
553 g_string_append (result, map (method->call_convention, call_conv_type_map));
554 g_string_sprintfa (result, " %s ", retval);
556 g_string_sprintfa (result, "%s::", type);
557 g_string_append (result, name);
559 g_string_append (result, gen_param);
562 g_string_append (result, " (");
564 for (i = 0; i < method->param_count; ++i) {
565 if (param_index && param_index <= m->tables [MONO_TABLE_PARAM].rows) {
566 mono_metadata_decode_row (&m->tables [MONO_TABLE_PARAM], param_index - 1, pcols, MONO_PARAM_SIZE);
567 name = mono_metadata_string_heap (m, pcols [MONO_PARAM_NAME]);
568 method->params [i]->attrs = pcols [MONO_PARAM_FLAGS];
574 g_string_append (result, ", ");
575 retval = dis_stringify_param (m, method->params [i]);
576 g_string_sprintfa (result, "%s '%s'", retval, name);
579 g_string_append (result, ") ");
582 mono_metadata_free_method_signature (method);
583 retval = result->str;
584 g_string_free (result, FALSE);
590 dis_stringify_object_with_class (MonoImage *m, MonoClass *c)
592 /* FIXME: handle MONO_TYPE_OBJECT ... */
593 const char *otype = c->byval_arg.type == MONO_TYPE_CLASS? "class" : "valuetype";
594 char *assemblyref = NULL, *result;
596 if (c->image->assembly_name) {
598 if (substitute_with_mscorlib_p && !strcmp ("corlib", c->image->assembly_name))
599 assemblyref = g_strdup_printf ("[%s]", "mscorlib");
601 assemblyref = g_strdup_printf ("[%s]", c->image->assembly->aname.name);
603 assemblyref = g_strdup_printf ("[.module %s]", c->image->module_name);
606 result = g_strdup_printf ("%s %s%s%s%s", otype, assemblyref?assemblyref:"", c->name_space,
607 *c->name_space?".":"", c->name);
608 g_free (assemblyref);
613 dis_stringify_object (MonoImage *m, MonoType *type)
615 MonoClass *c = mono_class_from_mono_type (type);
616 return dis_stringify_object_with_class (m, c);
620 dis_stringify_type (MonoImage *m, MonoType *type)
622 const char *pinned = "", *byref = "";
623 char *bare = NULL, *mods = NULL;
627 mods = dis_stringify_modifiers (m, type->num_mods, type->modifiers);
630 case MONO_TYPE_BOOLEAN:
644 case MONO_TYPE_STRING:
645 case MONO_TYPE_OBJECT:
646 case MONO_TYPE_TYPEDBYREF:
647 bare = g_strdup (map (type->type, element_type_map));
650 case MONO_TYPE_VALUETYPE:
651 case MONO_TYPE_CLASS:
652 bare = dis_stringify_object (m, type);
655 case MONO_TYPE_FNPTR:
656 bare = dis_stringify_method_signature (m, type->data.method, 0, FALSE);
658 case MONO_TYPE_PTR: {
660 child_type = dis_stringify_type (m, type->data.type);
662 bare = g_strdup_printf ("%s*", child_type);
666 case MONO_TYPE_SZARRAY: {
668 child_type = dis_stringify_type (m, &type->data.klass->byval_arg);
670 bare = g_strdup_printf ("%s[]", child_type);
674 case MONO_TYPE_ARRAY:
675 bare = dis_stringify_array (m, type->data.array);
678 bare = g_strdup ("void");
681 bare = g_strdup_printf ("!!%d", type->data.generic_param->num);
684 bare = g_strdup_printf ("!%d", type->data.generic_param->num);
686 case MONO_TYPE_GENERICINST: {
687 GString *str = g_string_new ("");
689 char *generic_type = dis_stringify_type (m, type->data.generic_inst->generic_type);
691 for (i = 0; i < type->data.generic_inst->type_argc; i++){
692 char *t = dis_stringify_type (m, type->data.generic_inst->type_argv [i]);
694 g_string_append (str, t);
695 if (i+1 != type->data.generic_inst->type_argc)
696 g_string_append (str, ", ");
699 bare = g_strdup_printf ("%s<%s>", generic_type, str->str);
700 g_string_free (str, TRUE);
705 g_error ("Do not know how to stringify type 0x%x", type->type);
714 result = g_strconcat (mods ? mods : "", bare, byref, pinned, NULL);
723 * @m: metadata context
724 * @ptr: location to decode from.
725 * @result: pointer to string where resulting decoded string is stored
727 * This routine returs in @result the stringified type pointed by @ptr.
730 * Returns: the new ptr to continue decoding
733 get_type (MonoImage *m, const char *ptr, char **result)
735 MonoType *type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
736 *result = dis_stringify_type (m, type);
737 mono_metadata_free_type (type);
743 * Returns a stringified representation of a FieldSig (22.2.4)
746 get_field_signature (MonoImage *m, guint32 blob_signature)
748 char *allocated_modifier_string, *allocated_type_string;
749 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
754 len = mono_metadata_decode_value (ptr, &ptr);
757 g_assert (*ptr == 0x06);
758 /* hex_dump (ptr, 0, len); */
761 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
762 ptr = get_type (m, ptr, &allocated_type_string);
764 res = g_strdup_printf (
766 allocated_modifier_string ? allocated_modifier_string : "",
767 allocated_type_string);
769 if (allocated_modifier_string)
770 g_free (allocated_modifier_string);
771 if (allocated_type_string)
772 g_free (allocated_type_string);
778 get_field_literal_type (MonoImage *m, guint32 blob_signature)
780 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
782 char *allocated_modifier_string;
784 len = mono_metadata_decode_value (ptr, &ptr);
787 g_assert (*ptr == 0x06);
790 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
791 if (allocated_modifier_string)
792 g_free (allocated_modifier_string);
794 return (MonoTypeEnum) *ptr;
800 * @m: metadata context
801 * @token: token to decode
803 * decodes the literal indexed by @token.
806 decode_literal (MonoImage *m, guint32 token)
808 return g_strdup ("LITERAL_VALUE");
813 * @m: metadata context
814 * @ptr: location to decode from.
815 * @result: pointer to string where resulting decoded string is stored
817 * This routine returns in @result the stringified RetType (22.2.11)
819 * Returns: the new ptr to continue decoding.
822 get_ret_type (MonoImage *m, const char *ptr, char **ret_type)
824 GString *str = g_string_new ("");
826 char *allocated_type_string;
828 ptr = get_custom_mod (m, ptr, &mod);
830 g_string_append (str, mod);
831 g_string_append_c (str, ' ');
835 if (*ptr == MONO_TYPE_TYPEDBYREF){
836 /* TODO: what does `typedbyref' mean? */
837 g_string_append (str, "/* FIXME: What does this mean? */ typedbyref ");
839 } else if (*ptr == MONO_TYPE_VOID){
840 g_string_append (str, "void");
843 if (*ptr == MONO_TYPE_BYREF){
844 g_string_append (str, "[out] ");
848 ptr = get_type (m, ptr, &allocated_type_string);
849 g_string_append (str, allocated_type_string);
850 g_free (allocated_type_string);
853 *ret_type = str->str;
854 g_string_free (str, FALSE);
861 * @m: metadata context
862 * @ptr: location to decode from.
863 * @result: pointer to string where resulting decoded string is stored
865 * This routine returns in @result the stringified Param (22.2.10)
867 * Returns: the new ptr to continue decoding.
870 get_param (MonoImage *m, const char *ptr, char **retval)
872 GString *str = g_string_new ("");
873 char *allocated_mod_string, *allocated_type_string;
875 ptr = get_custom_mod (m, ptr, &allocated_mod_string);
876 if (allocated_mod_string){
877 g_string_append (str, allocated_mod_string);
878 g_string_append_c (str, ' ');
879 g_free (allocated_mod_string);
882 if (*ptr == MONO_TYPE_TYPEDBYREF){
883 g_string_append (str, " typedbyref ");
886 if (*ptr == MONO_TYPE_BYREF){
887 g_string_append (str, "[out] ");
890 ptr = get_type (m, ptr, &allocated_type_string);
891 g_string_append (str, allocated_type_string);
892 g_free (allocated_type_string);
896 g_string_free (str, FALSE);
900 static map_t param_map [] = {
901 { PARAM_ATTRIBUTE_IN, "[in] " },
902 { PARAM_ATTRIBUTE_OUT, "[out] " },
903 { PARAM_ATTRIBUTE_OPTIONAL, "optional " },
904 { PARAM_ATTRIBUTE_HAS_DEFAULT, "hasdefault " },
905 { PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL, "fieldmarshal " },
910 param_flags (guint32 f)
912 return g_strdup (flags (f, param_map));
915 static map_t field_access_map [] = {
916 { FIELD_ATTRIBUTE_COMPILER_CONTROLLED, "compilercontrolled " },
917 { FIELD_ATTRIBUTE_PRIVATE, "private " },
918 { FIELD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
919 { FIELD_ATTRIBUTE_ASSEMBLY, "assembly " },
920 { FIELD_ATTRIBUTE_FAMILY, "family " },
921 { FIELD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
922 { FIELD_ATTRIBUTE_PUBLIC, "public " },
926 static map_t field_flags_map [] = {
927 { FIELD_ATTRIBUTE_STATIC, "static " },
928 { FIELD_ATTRIBUTE_INIT_ONLY, "initonly " },
929 { FIELD_ATTRIBUTE_LITERAL, "literal " },
930 { FIELD_ATTRIBUTE_NOT_SERIALIZED, "notserialized " },
931 { FIELD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
932 { FIELD_ATTRIBUTE_PINVOKE_IMPL, "FIXME:pinvokeimpl " },
933 { FIELD_ATTRIBUTE_RT_SPECIAL_NAME, "rtspecialname " },
934 { FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL, "hasfieldmarshal " },
935 { FIELD_ATTRIBUTE_HAS_DEFAULT, "hasdefault " },
936 { FIELD_ATTRIBUTE_HAS_FIELD_RVA, "hasfieldrva " },
943 * Returns a stringified version of a Field's flags
946 field_flags (guint32 f)
949 int access = f & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK;
953 strcat (buffer, map (access, field_access_map));
954 strcat (buffer, flags (f, field_flags_map));
955 return g_strdup (buffer);
959 * Returns a stringifed representation of a MethodRefSig (22.2.2)
962 get_methodref_signature (MonoImage *m, guint32 blob_signature, const char *fancy_name)
964 GString *res = g_string_new ("");
965 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
966 char *allocated_ret_type, *s;
967 gboolean seen_vararg = 0;
968 int param_count, signature_len;
969 int i, gen_count = 0;
972 signature_len = mono_metadata_decode_value (ptr, &ptr);
976 g_string_append (res, "explicit-this ");
978 g_string_append (res, "instance "); /* has-this */
988 gen_count = mono_metadata_decode_value (ptr, &ptr);
989 param_count = mono_metadata_decode_value (ptr, &ptr);
991 ptr = get_ret_type (m, ptr, &allocated_ret_type);
992 g_string_append (res, allocated_ret_type);
993 g_free (allocated_ret_type);
997 g_string_append_c (res, ' ');
998 g_string_append (res, fancy_name);
1001 g_string_append (res, "(");
1004 * param_count describes parameters *before* and *after*
1005 * the vararg sentinel
1007 for (i = 0; i < param_count; i++){
1011 * If ptr is a SENTINEL
1014 g_string_append (res, " varargs ");
1018 ptr = get_param (m, ptr, ¶m);
1019 g_string_append (res, param);
1020 if (i+1 != param_count)
1021 g_string_append (res, ", ");
1024 g_string_append (res, ")");
1027 * cleanup and return
1030 g_string_free (res, FALSE);
1035 * Returns a stringifed representation of a field ref
1038 get_fieldref_signature (MonoImage *m, int idx)
1040 guint32 cols [MONO_MEMBERREF_SIZE];
1044 mono_metadata_decode_row (&m->tables [MONO_TABLE_MEMBERREF],
1045 idx - 1, cols, MONO_MEMBERREF_SIZE);
1047 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
1048 full_sig = g_strdup_printf ("%s %s::%s", sig,
1049 get_memberref_parent (m, cols [MONO_MEMBERREF_CLASS]),
1050 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));
1058 * @m: metadata context
1059 * @token: a FIELD_DEF token
1061 * This routine has to locate the TypeDef that "owns" this Field.
1062 * Since there is no backpointer in the Field table, we have to scan
1063 * the TypeDef table and locate the actual "owner" of the field
1066 get_field (MonoImage *m, guint32 token)
1068 int idx = mono_metadata_token_index (token);
1069 guint32 cols [MONO_FIELD_SIZE];
1070 char *sig, *res, *type;
1074 * We can get here also with a MenberRef token (for a field
1075 * defined in another module/assembly, just like in get_method ()
1077 if (mono_metadata_token_code (token) == MONO_TOKEN_MEMBER_REF) {
1078 return get_fieldref_signature (m, idx);
1080 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
1082 mono_metadata_decode_row (&m->tables [MONO_TABLE_FIELD], idx - 1, cols, MONO_FIELD_SIZE);
1083 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
1086 * To locate the actual "container" for this field, we have to scan
1087 * the TypeDef table. LAME!
1089 type_idx = mono_metadata_typedef_from_field (m, idx);
1091 type = get_typedef (m, type_idx);
1092 res = g_strdup_printf ("%s %s::%s",
1094 mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
1102 get_memberref_parent (MonoImage *m, guint32 mrp_token)
1105 * mrp_index is a MemberRefParent coded index
1107 guint32 table = mrp_token & 7;
1108 guint32 idx = mrp_token >> 3;
1111 case 0: /* TypeDef */
1112 return get_typedef (m, idx);
1114 case 1: /* TypeRef */
1115 return get_typeref (m, idx);
1117 case 2: /* ModuleRef */
1118 return g_strdup_printf ("TODO:MemberRefParent-ModuleRef");
1120 case 3: /* MethodDef */
1121 return g_strdup ("TODO:MethodDef");
1123 case 4: /* TypeSpec */
1124 return get_typespec (m, idx);
1126 g_assert_not_reached ();
1132 * @m: metadata context
1133 * @token: a METHOD_DEF or MEMBER_REF token
1135 * This routine has to locate the TypeDef that "owns" this Field.
1136 * Since there is no backpointer in the Field table, we have to scan
1137 * the TypeDef table and locate the actual "owner" of the field
1140 get_method (MonoImage *m, guint32 token)
1142 int idx = mono_metadata_token_index (token);
1143 guint32 member_cols [MONO_MEMBERREF_SIZE], method_cols [MONO_METHOD_SIZE];
1149 mh = mono_get_method (m, token, NULL);
1151 sig = dis_stringify_object_with_class (m, mh->klass);
1152 name = g_strdup_printf ("%s::%s", sig, mh->name);
1157 switch (mono_metadata_token_code (token)){
1158 case MONO_TOKEN_METHOD_DEF:
1159 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD],
1160 idx - 1, method_cols, MONO_METHOD_SIZE);
1162 sig = get_methodref_signature (m, method_cols [MONO_METHOD_SIGNATURE], name);
1165 case MONO_TOKEN_MEMBER_REF: {
1166 mono_metadata_decode_row (&m->tables [MONO_TABLE_MEMBERREF],
1167 idx - 1, member_cols, MONO_MEMBERREF_SIZE);
1169 name = g_strdup_printf ("%s::%s",
1170 get_memberref_parent (m, member_cols [MONO_MEMBERREF_CLASS]),
1171 mono_metadata_string_heap (m, member_cols [MONO_MEMBERREF_NAME]));
1172 sig = get_methodref_signature (
1173 m, member_cols [MONO_MEMBERREF_SIGNATURE], name);
1176 case MONO_TOKEN_METHOD_SPEC: {
1177 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHODSPEC], idx - 1, member_cols, MONO_METHODSPEC_SIZE);
1178 token = member_cols [MONO_METHODSPEC_METHOD];
1179 if ((token & METHODDEFORREF_MASK) == METHODDEFORREF_METHODDEF)
1180 token = MONO_TOKEN_METHOD_DEF | (token >> METHODDEFORREF_BITS);
1182 token = MONO_TOKEN_MEMBER_REF | (token >> METHODDEFORREF_BITS);
1183 return get_method (m, token);
1188 g_assert_not_reached ();
1190 g_assert_not_reached ();
1196 * @m: metadata context
1197 * @idx: index into the method table
1199 * Returns: A stringified version of the method signature.
1202 get_methoddef (MonoImage *m, guint32 idx)
1204 guint32 cols [MONO_METHOD_SIZE];
1210 mh = mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
1212 sig = dis_stringify_object_with_class (m, mh->klass);
1213 name = g_strdup_printf ("%s::%s", sig, mh->name);
1217 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD],
1218 idx - 1, cols, MONO_METHOD_SIZE);
1219 sig = get_methodref_signature (m, cols [MONO_METHOD_SIGNATURE], name);
1226 * @m: metadata context
1227 * @blob_index: index into the blob where the constant is stored
1229 * Returns: An allocated value representing a stringified version of the
1233 get_constant (MonoImage *m, MonoTypeEnum t, guint32 blob_index)
1235 const char *ptr = mono_metadata_blob_heap (m, blob_index);
1238 len = mono_metadata_decode_value (ptr, &ptr);
1241 case MONO_TYPE_BOOLEAN:
1242 return g_strdup_printf ("%s", *ptr ? "true" : "false");
1244 case MONO_TYPE_CHAR:
1245 return g_strdup_printf ("%c", *ptr); /* FIXME: unicode char */
1249 return g_strdup_printf ("int8(0x%02x)", (int) (*ptr));
1254 return g_strdup_printf ("int16(0x%08x)", (int) read16 (ptr));
1258 return g_strdup_printf ("int32(%d)", read32 (ptr));
1260 case MONO_TYPE_I8: {
1263 high = read32 (ptr + 4);
1264 return g_strdup_printf ("0x%08x%08x", high, low);
1266 case MONO_TYPE_U8: {
1269 high = read32 (ptr + 4);
1270 return g_strdup_printf ("0x%08x%08x", high, low);
1272 case MONO_TYPE_R4: {
1275 return g_strdup_printf ("%g", (double) r);
1277 case MONO_TYPE_R8: {
1280 return g_strdup_printf ("%g", r);
1282 case MONO_TYPE_STRING: {
1286 for (i = 0; !ptr [i+1]; i += 2){
1291 case '\n': /* add more */
1295 res = g_malloc (len + e + 3);
1299 for (i = 0; i < len; i += 2){
1312 res[j++] = isprint (ptr [i]) ? ptr [i] : '.';
1321 case MONO_TYPE_CLASS:
1322 return g_strdup ("CLASS CONSTANT. MUST BE ZERO");
1325 g_error ("Unknown MONO_TYPE (%d) on constant at Blob index (0x%08x)\n",
1326 (int) *ptr, blob_index);
1327 return g_strdup_printf ("Unknown");
1334 * @m: metadata context
1335 * @token: token that we want to decode.
1337 * Returns: An allocated value representing a stringified version of the
1341 get_token (MonoImage *m, guint32 token)
1343 guint32 idx = mono_metadata_token_index (token);
1345 switch (mono_metadata_token_code (token)){
1346 case MONO_TOKEN_FIELD_DEF:
1347 return (get_field (m, token));
1348 case MONO_TOKEN_TYPE_DEF:
1349 return get_typedef (m, idx);
1350 case MONO_TOKEN_TYPE_REF:
1351 return get_typeref (m, idx);
1352 case MONO_TOKEN_TYPE_SPEC:
1353 return get_typespec (m, idx);
1355 g_error ("Do not know how to decode tokens of type 0x%08x", token);
1358 g_assert_not_reached ();
1359 return g_strdup ("ERROR");
1364 * @m: metadata context
1365 * @token: the token can belong to any of the following tables:
1366 * MONO_TOKEN_TYPE_REF, MONO_TOKEN_TYPE_DEF, MONO_TOKEN_TYPE_SPEC
1368 * Returns: a stringified version of the MethodDef or MethodRef or TypeSpecn
1369 * at (token & 0xffffff)
1372 get_token_type (MonoImage *m, guint32 token)
1374 char *temp = NULL, *s;
1377 idx = mono_metadata_token_index (token);
1379 switch (mono_metadata_token_code (token)){
1380 case MONO_TOKEN_TYPE_DEF:
1381 temp = get_typedef (m, idx);
1382 s = g_strdup_printf ("%s", temp);
1385 case MONO_TOKEN_TYPE_REF:
1386 temp = get_typeref (m, idx);
1387 s = g_strdup_printf ("%s", temp);
1390 case MONO_TOKEN_TYPE_SPEC:
1391 s = get_typespec (m, idx);
1395 g_error ("Unhandled encoding for token 0x%08x", token);
1406 get_guid (MonoImage *m, guint32 guid_index)
1408 const unsigned char *guid;
1411 guid = mono_metadata_guid_heap (m, guid_index);
1413 result = g_strdup_printf ("{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
1414 guid [3], guid [2], guid [1], guid [0], guid [5], guid [4], guid [7], guid [6],
1415 guid [8], guid [9], guid [10], guid [11], guid [12], guid [13], guid [14], guid [15]);
1420 dis_get_custom_attrs (MonoImage *m, guint32 token)
1423 guint32 idx, i, len, mtoken;
1424 guint32 cols [MONO_CUSTOM_ATTR_SIZE];
1430 idx = mono_metadata_token_index (token);
1431 idx <<= CUSTOM_ATTR_BITS;
1433 switch (mono_metadata_token_table (token)) {
1434 case MONO_TABLE_TYPEDEF:
1435 idx |= CUSTOM_ATTR_TYPEDEF;
1437 case MONO_TABLE_ASSEMBLY:
1438 idx |= CUSTOM_ATTR_ASSEMBLY;
1440 case MONO_TABLE_PROPERTY:
1441 idx |= CUSTOM_ATTR_PROPERTY;
1443 case MONO_TABLE_EVENT:
1444 idx |= CUSTOM_ATTR_EVENT;
1446 case MONO_TABLE_FIELD:
1447 idx |= CUSTOM_ATTR_FIELDDEF;
1449 case MONO_TABLE_METHOD:
1450 idx |= CUSTOM_ATTR_METHODDEF;
1452 case MONO_TABLE_PARAM:
1453 idx |= CUSTOM_ATTR_PARAMDEF;
1456 g_print ("Missing custom attr get support for token 0x%08x\n", token);
1460 ca = &m->tables [MONO_TABLE_CUSTOMATTRIBUTE];
1461 /* the table is not sorted */
1462 for (i = 0; i < ca->rows; ++i) {
1464 mono_metadata_decode_row (ca, i, cols, MONO_CUSTOM_ATTR_SIZE);
1465 if (cols [MONO_CUSTOM_ATTR_PARENT] != idx)
1467 mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> CUSTOM_ATTR_TYPE_BITS;
1468 switch (cols [MONO_CUSTOM_ATTR_TYPE] & CUSTOM_ATTR_TYPE_MASK) {
1469 case CUSTOM_ATTR_TYPE_METHODDEF:
1470 mtoken |= MONO_TOKEN_METHOD_DEF;
1472 case CUSTOM_ATTR_TYPE_MEMBERREF:
1473 mtoken |= MONO_TOKEN_MEMBER_REF;
1476 g_error ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
1479 method = get_method (m, mtoken);
1480 val = mono_metadata_blob_heap (m, cols [MONO_CUSTOM_ATTR_VALUE]);
1481 len = mono_metadata_decode_value (val, &val);
1482 attr = g_string_new (".custom ");
1483 dump = data_dump (val, len, "\t\t");
1484 g_string_sprintfa (attr, "%s = %s", method, dump);
1486 list = g_list_append (list, attr->str);
1487 g_string_free (attr, FALSE);
1494 get_marshal_info (MonoImage *m, const char *blob) {
1497 len = mono_metadata_decode_blob_size (blob, &blob);
1500 case MONO_NATIVE_BOOLEAN:
1501 return g_strdup ("bool");
1502 case MONO_NATIVE_I1:
1503 return g_strdup ("int8");
1504 case MONO_NATIVE_U1:
1505 return g_strdup ("unsigned int8");
1506 case MONO_NATIVE_I2:
1507 return g_strdup ("int16");
1508 case MONO_NATIVE_U2:
1509 return g_strdup ("unsigned int16");
1510 case MONO_NATIVE_I4:
1511 return g_strdup ("int32");
1512 case MONO_NATIVE_U4:
1513 return g_strdup ("unsigned int32");
1514 case MONO_NATIVE_I8:
1515 return g_strdup ("int64");
1516 case MONO_NATIVE_U8:
1517 return g_strdup ("unsigned int64");
1518 case MONO_NATIVE_R4:
1519 return g_strdup ("float32");
1520 case MONO_NATIVE_R8:
1521 return g_strdup ("float64");
1522 case MONO_NATIVE_CURRENCY:
1523 return g_strdup ("currency");
1524 case MONO_NATIVE_BSTR:
1525 return g_strdup ("bstr");
1526 case MONO_NATIVE_LPSTR:
1527 return g_strdup ("lpstr");
1528 case MONO_NATIVE_LPWSTR:
1529 return g_strdup ("lpwstr");
1530 case MONO_NATIVE_LPTSTR:
1531 return g_strdup ("lptstr");
1532 case MONO_NATIVE_BYVALTSTR:
1533 size = mono_metadata_decode_value (blob + 1, &blob);
1534 return g_strdup_printf ("fixed sysstring [%d]", size);
1535 case MONO_NATIVE_IUNKNOWN:
1536 return g_strdup ("iunknown");
1537 case MONO_NATIVE_IDISPATCH:
1538 return g_strdup ("idispatch");
1539 case MONO_NATIVE_STRUCT:
1540 return g_strdup ("struct");
1541 case MONO_NATIVE_INTERFACE:
1542 return g_strdup ("interface");
1543 case MONO_NATIVE_SAFEARRAY:
1544 return g_strdup ("safearray");
1545 case MONO_NATIVE_BYVALARRAY:
1546 size = mono_metadata_decode_value (blob + 1, &blob);
1547 return g_strdup_printf ("fixed array [%d]", size);
1548 case MONO_NATIVE_INT:
1549 return g_strdup ("int");
1550 case MONO_NATIVE_UINT:
1551 return g_strdup ("unsigned int");
1552 case MONO_NATIVE_VBBYREFSTR:
1553 return g_strdup ("vbbyrefstr");
1554 case MONO_NATIVE_ANSIBSTR:
1555 return g_strdup ("ansi bstr");
1556 case MONO_NATIVE_TBSTR:
1557 return g_strdup ("tbstr");
1558 case MONO_NATIVE_VARIANTBOOL:
1559 return g_strdup ("variant bool");
1560 case MONO_NATIVE_FUNC:
1561 return g_strdup ("method");
1562 case MONO_NATIVE_ASANY:
1563 return g_strdup ("as any");
1564 case MONO_NATIVE_LPARRAY:
1565 return g_strdup ("[]");
1566 case MONO_NATIVE_LPSTRUCT:
1567 return g_strdup ("lpstruct");
1568 case MONO_NATIVE_CUSTOM:
1569 return g_strdup ("custom");
1570 case MONO_NATIVE_ERROR:
1571 return g_strdup ("error");
1573 return g_strdup ("unknown");