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 & 0x03;
270 idx = dor_token >> 2;
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 & 0x01) /* MethodDef */
312 return get_methoddef (m, dor_token >> 1);
314 return get_typedef (m, dor_token >> 1);
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);
475 dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int methoddef_row, gboolean fully_qualified)
477 guint32 cols [MONO_METHOD_SIZE];
478 guint32 pcols [MONO_PARAM_SIZE];
479 guint32 param_index = 0;
480 const char *name = "";
484 GString *result = g_string_new ("");
487 g_assert (method || methoddef_row);
490 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD], methoddef_row -1, cols, MONO_METHOD_SIZE);
492 type = get_typedef (m, mono_metadata_typedef_from_method (m, methoddef_row));
493 name = mono_metadata_string_heap (m, cols [MONO_METHOD_NAME]);
494 param_index = cols [MONO_METHOD_PARAMLIST];
496 const char *sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
497 mono_metadata_decode_blob_size (sig, &sig);
498 method = mono_metadata_parse_method_signature (m, methoddef_row, sig, &sig);
503 retval = dis_stringify_param (m, method->ret);
505 g_string_append (result, "instance ");
506 g_string_append (result, map (method->call_convention, call_conv_type_map));
507 g_string_sprintfa (result, " %s ", retval);
509 g_string_sprintfa (result, "%s::", type);
510 g_string_sprintfa (result, "%s(", name);
512 for (i = 0; i < method->param_count; ++i) {
513 if (param_index && param_index <= m->tables [MONO_TABLE_PARAM].rows) {
514 mono_metadata_decode_row (&m->tables [MONO_TABLE_PARAM], param_index - 1, pcols, MONO_PARAM_SIZE);
515 name = mono_metadata_string_heap (m, pcols [MONO_PARAM_NAME]);
516 method->params [i]->attrs = pcols [MONO_PARAM_FLAGS];
522 g_string_append (result, ", ");
523 retval = dis_stringify_param (m, method->params [i]);
524 g_string_sprintfa (result, "%s '%s'", retval, name);
527 g_string_append (result, ") ");
530 mono_metadata_free_method_signature (method);
531 retval = result->str;
532 g_string_free (result, FALSE);
538 dis_stringify_object_with_class (MonoImage *m, MonoClass *c)
540 /* FIXME: handle MONO_TYPE_OBJECT ... */
541 const char *otype = c->byval_arg.type == MONO_TYPE_CLASS? "class" : "valuetype";
542 char *assemblyref = NULL, *result;
544 if (c->image->assembly_name) {
546 if (substitute_with_mscorlib_p && !strcmp ("corlib", c->image->assembly_name))
547 assemblyref = g_strdup_printf ("[%s]", "mscorlib");
549 assemblyref = g_strdup_printf ("[%s]", c->image->assembly->aname.name);
551 assemblyref = g_strdup_printf ("[.module %s]", c->image->module_name);
554 result = g_strdup_printf ("%s %s%s%s%s", otype, assemblyref?assemblyref:"", c->name_space,
555 *c->name_space?".":"", c->name);
556 g_free (assemblyref);
561 dis_stringify_object (MonoImage *m, MonoType *type)
563 MonoClass *c = mono_class_from_mono_type (type);
564 return dis_stringify_object_with_class (m, c);
568 dis_stringify_type (MonoImage *m, MonoType *type)
570 const char *pinned = "", *byref = "";
571 char *bare = NULL, *mods = NULL;
575 mods = dis_stringify_modifiers (m, type->num_mods, type->modifiers);
578 case MONO_TYPE_BOOLEAN:
592 case MONO_TYPE_STRING:
593 case MONO_TYPE_OBJECT:
594 case MONO_TYPE_TYPEDBYREF:
595 bare = g_strdup (map (type->type, element_type_map));
598 case MONO_TYPE_VALUETYPE:
599 case MONO_TYPE_CLASS:
600 bare = dis_stringify_object (m, type);
603 case MONO_TYPE_FNPTR:
604 bare = dis_stringify_method_signature (m, type->data.method, 0, FALSE);
606 case MONO_TYPE_PTR: {
608 child_type = dis_stringify_type (m, type->data.type);
610 bare = g_strdup_printf ("%s*", child_type);
614 case MONO_TYPE_SZARRAY: {
616 child_type = dis_stringify_type (m, &type->data.klass->byval_arg);
618 bare = g_strdup_printf ("%s[]", child_type);
622 case MONO_TYPE_ARRAY:
623 bare = dis_stringify_array (m, type->data.array);
626 bare = g_strdup ("void");
629 bare = g_strdup_printf ("!!%d", type->data.generic_param->num);
632 bare = g_strdup_printf ("!%d", type->data.generic_param->num);
634 case MONO_TYPE_GENERICINST: {
635 GString *str = g_string_new ("");
637 char *generic_type = dis_stringify_type (m, type->data.generic_inst->generic_type);
639 for (i = 0; i < type->data.generic_inst->type_argc; i++){
640 char *t = dis_stringify_type (m, type->data.generic_inst->type_argv [i]);
642 g_string_append (str, t);
643 if (i+1 != type->data.generic_inst->type_argc)
644 g_string_append (str, ", ");
647 bare = g_strdup_printf ("%s<%s>", generic_type, str->str);
648 g_string_free (str, TRUE);
653 g_error ("Do not know how to stringify type 0x%x", type->type);
662 result = g_strconcat (mods ? mods : "", bare, byref, pinned, NULL);
671 * @m: metadata context
672 * @ptr: location to decode from.
673 * @result: pointer to string where resulting decoded string is stored
675 * This routine returs in @result the stringified type pointed by @ptr.
678 * Returns: the new ptr to continue decoding
681 get_type (MonoImage *m, const char *ptr, char **result)
683 MonoType *type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
684 *result = dis_stringify_type (m, type);
685 mono_metadata_free_type (type);
691 * Returns a stringified representation of a FieldSig (22.2.4)
694 get_field_signature (MonoImage *m, guint32 blob_signature)
696 char *allocated_modifier_string, *allocated_type_string;
697 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
702 len = mono_metadata_decode_value (ptr, &ptr);
705 g_assert (*ptr == 0x06);
706 /* hex_dump (ptr, 0, len); */
709 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
710 ptr = get_type (m, ptr, &allocated_type_string);
712 res = g_strdup_printf (
714 allocated_modifier_string ? allocated_modifier_string : "",
715 allocated_type_string);
717 if (allocated_modifier_string)
718 g_free (allocated_modifier_string);
719 if (allocated_type_string)
720 g_free (allocated_type_string);
726 get_field_literal_type (MonoImage *m, guint32 blob_signature)
728 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
730 char *allocated_modifier_string;
732 len = mono_metadata_decode_value (ptr, &ptr);
735 g_assert (*ptr == 0x06);
738 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
739 if (allocated_modifier_string)
740 g_free (allocated_modifier_string);
742 return (MonoTypeEnum) *ptr;
748 * @m: metadata context
749 * @token: token to decode
751 * decodes the literal indexed by @token.
754 decode_literal (MonoImage *m, guint32 token)
756 return g_strdup ("LITERAL_VALUE");
761 * @m: metadata context
762 * @ptr: location to decode from.
763 * @result: pointer to string where resulting decoded string is stored
765 * This routine returns in @result the stringified RetType (22.2.11)
767 * Returns: the new ptr to continue decoding.
770 get_ret_type (MonoImage *m, const char *ptr, char **ret_type)
772 GString *str = g_string_new ("");
774 char *allocated_type_string;
776 ptr = get_custom_mod (m, ptr, &mod);
778 g_string_append (str, mod);
779 g_string_append_c (str, ' ');
783 if (*ptr == MONO_TYPE_TYPEDBYREF){
784 /* TODO: what does `typedbyref' mean? */
785 g_string_append (str, "/* FIXME: What does this mean? */ typedbyref ");
787 } else if (*ptr == MONO_TYPE_VOID){
788 g_string_append (str, "void");
791 if (*ptr == MONO_TYPE_BYREF){
792 g_string_append (str, "[out] ");
796 ptr = get_type (m, ptr, &allocated_type_string);
797 g_string_append (str, allocated_type_string);
798 g_free (allocated_type_string);
801 *ret_type = str->str;
802 g_string_free (str, FALSE);
809 * @m: metadata context
810 * @ptr: location to decode from.
811 * @result: pointer to string where resulting decoded string is stored
813 * This routine returns in @result the stringified Param (22.2.10)
815 * Returns: the new ptr to continue decoding.
818 get_param (MonoImage *m, const char *ptr, char **retval)
820 GString *str = g_string_new ("");
821 char *allocated_mod_string, *allocated_type_string;
823 ptr = get_custom_mod (m, ptr, &allocated_mod_string);
824 if (allocated_mod_string){
825 g_string_append (str, allocated_mod_string);
826 g_string_append_c (str, ' ');
827 g_free (allocated_mod_string);
830 if (*ptr == MONO_TYPE_TYPEDBYREF){
831 g_string_append (str, " typedbyref ");
834 if (*ptr == MONO_TYPE_BYREF){
835 g_string_append (str, "[out] ");
838 ptr = get_type (m, ptr, &allocated_type_string);
839 g_string_append (str, allocated_type_string);
840 g_free (allocated_type_string);
844 g_string_free (str, FALSE);
848 static map_t param_map [] = {
849 { PARAM_ATTRIBUTE_IN, "[in] " },
850 { PARAM_ATTRIBUTE_OUT, "[out] " },
851 { PARAM_ATTRIBUTE_OPTIONAL, "optional " },
852 { PARAM_ATTRIBUTE_HAS_DEFAULT, "hasdefault " },
853 { PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL, "fieldmarshal " },
858 param_flags (guint32 f)
860 return g_strdup (flags (f, param_map));
863 static map_t field_access_map [] = {
864 { FIELD_ATTRIBUTE_COMPILER_CONTROLLED, "compilercontrolled " },
865 { FIELD_ATTRIBUTE_PRIVATE, "private " },
866 { FIELD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
867 { FIELD_ATTRIBUTE_ASSEMBLY, "assembly " },
868 { FIELD_ATTRIBUTE_FAMILY, "family " },
869 { FIELD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
870 { FIELD_ATTRIBUTE_PUBLIC, "public " },
874 static map_t field_flags_map [] = {
875 { FIELD_ATTRIBUTE_STATIC, "static " },
876 { FIELD_ATTRIBUTE_INIT_ONLY, "initonly " },
877 { FIELD_ATTRIBUTE_LITERAL, "literal " },
878 { FIELD_ATTRIBUTE_NOT_SERIALIZED, "notserialized " },
879 { FIELD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
880 { FIELD_ATTRIBUTE_PINVOKE_IMPL, "FIXME:pinvokeimpl " },
881 { FIELD_ATTRIBUTE_RT_SPECIAL_NAME, "rtspecialname " },
882 { FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL, "hasfieldmarshal " },
883 { FIELD_ATTRIBUTE_HAS_DEFAULT, "hasdefault " },
884 { FIELD_ATTRIBUTE_HAS_FIELD_RVA, "hasfieldrva " },
891 * Returns a stringified version of a Field's flags
894 field_flags (guint32 f)
897 int access = f & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK;
901 strcat (buffer, map (access, field_access_map));
902 strcat (buffer, flags (f, field_flags_map));
903 return g_strdup (buffer);
907 * Returns a stringifed representation of a MethodRefSig (22.2.2)
910 get_methodref_signature (MonoImage *m, guint32 blob_signature, const char *fancy_name)
912 GString *res = g_string_new ("");
913 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
914 char *allocated_ret_type, *s;
915 gboolean seen_vararg = 0;
916 int param_count, signature_len;
917 int i, gen_count = 0;
920 signature_len = mono_metadata_decode_value (ptr, &ptr);
924 g_string_append (res, "explicit-this ");
926 g_string_append (res, "instance "); /* has-this */
936 gen_count = mono_metadata_decode_value (ptr, &ptr);
937 param_count = mono_metadata_decode_value (ptr, &ptr);
939 ptr = get_ret_type (m, ptr, &allocated_ret_type);
940 g_string_append (res, allocated_ret_type);
941 g_free (allocated_ret_type);
945 g_string_append_c (res, ' ');
946 g_string_append (res, fancy_name);
949 g_string_append (res, "(");
952 * param_count describes parameters *before* and *after*
953 * the vararg sentinel
955 for (i = 0; i < param_count; i++){
959 * If ptr is a SENTINEL
962 g_string_append (res, " varargs ");
966 ptr = get_param (m, ptr, ¶m);
967 g_string_append (res, param);
968 if (i+1 != param_count)
969 g_string_append (res, ", ");
972 g_string_append (res, ")");
978 g_string_free (res, FALSE);
983 * Returns a stringifed representation of a field ref
986 get_fieldref_signature (MonoImage *m, int idx)
988 guint32 cols [MONO_MEMBERREF_SIZE];
992 mono_metadata_decode_row (&m->tables [MONO_TABLE_MEMBERREF],
993 idx - 1, cols, MONO_MEMBERREF_SIZE);
995 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
996 full_sig = g_strdup_printf ("%s %s::%s", sig,
997 get_memberref_parent (m, cols [MONO_MEMBERREF_CLASS]),
998 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));
1006 * @m: metadata context
1007 * @token: a FIELD_DEF token
1009 * This routine has to locate the TypeDef that "owns" this Field.
1010 * Since there is no backpointer in the Field table, we have to scan
1011 * the TypeDef table and locate the actual "owner" of the field
1014 get_field (MonoImage *m, guint32 token)
1016 int idx = mono_metadata_token_index (token);
1017 guint32 cols [MONO_FIELD_SIZE];
1018 char *sig, *res, *type;
1022 * We can get here also with a MenberRef token (for a field
1023 * defined in another module/assembly, just like in get_method ()
1025 if (mono_metadata_token_code (token) == MONO_TOKEN_MEMBER_REF) {
1026 return get_fieldref_signature (m, idx);
1028 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
1030 mono_metadata_decode_row (&m->tables [MONO_TABLE_FIELD], idx - 1, cols, MONO_FIELD_SIZE);
1031 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
1034 * To locate the actual "container" for this field, we have to scan
1035 * the TypeDef table. LAME!
1037 type_idx = mono_metadata_typedef_from_field (m, idx);
1039 type = get_typedef (m, type_idx);
1040 res = g_strdup_printf ("%s %s::%s",
1042 mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
1050 get_memberref_parent (MonoImage *m, guint32 mrp_token)
1053 * mrp_index is a MemberRefParent coded index
1055 guint32 table = mrp_token & 7;
1056 guint32 idx = mrp_token >> 3;
1059 case 0: /* TypeDef */
1060 return get_typedef (m, idx);
1062 case 1: /* TypeRef */
1063 return get_typeref (m, idx);
1065 case 2: /* ModuleRef */
1066 return g_strdup_printf ("TODO:MemberRefParent-ModuleRef");
1068 case 3: /* MethodDef */
1069 return g_strdup ("TODO:MethodDef");
1071 case 4: /* TypeSpec */
1072 return get_typespec (m, idx);
1074 g_assert_not_reached ();
1080 * @m: metadata context
1081 * @token: a METHOD_DEF or MEMBER_REF token
1083 * This routine has to locate the TypeDef that "owns" this Field.
1084 * Since there is no backpointer in the Field table, we have to scan
1085 * the TypeDef table and locate the actual "owner" of the field
1088 get_method (MonoImage *m, guint32 token)
1090 int idx = mono_metadata_token_index (token);
1091 guint32 member_cols [MONO_MEMBERREF_SIZE], method_cols [MONO_METHOD_SIZE];
1097 mh = mono_get_method (m, token, NULL);
1099 sig = dis_stringify_object_with_class (m, mh->klass);
1100 name = g_strdup_printf ("%s::%s", sig, mh->name);
1105 switch (mono_metadata_token_code (token)){
1106 case MONO_TOKEN_METHOD_DEF:
1107 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD],
1108 idx - 1, method_cols, MONO_METHOD_SIZE);
1110 sig = get_methodref_signature (m, method_cols [MONO_METHOD_SIGNATURE], name);
1113 case MONO_TOKEN_MEMBER_REF: {
1114 mono_metadata_decode_row (&m->tables [MONO_TABLE_MEMBERREF],
1115 idx - 1, member_cols, MONO_MEMBERREF_SIZE);
1117 name = g_strdup_printf ("%s::%s",
1118 get_memberref_parent (m, member_cols [MONO_MEMBERREF_CLASS]),
1119 mono_metadata_string_heap (m, member_cols [MONO_MEMBERREF_NAME]));
1120 sig = get_methodref_signature (
1121 m, member_cols [MONO_MEMBERREF_SIGNATURE], name);
1124 case MONO_TOKEN_METHOD_SPEC: {
1125 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHODSPEC], idx - 1, member_cols, MONO_METHODSPEC_SIZE);
1126 token = member_cols [MONO_METHODSPEC_METHOD];
1127 if ((token & METHODDEFORREF_MASK) == METHODDEFORREF_METHODDEF)
1128 token = MONO_TOKEN_METHOD_DEF | (token >> METHODDEFORREF_BITS);
1130 token = MONO_TOKEN_MEMBER_REF | (token >> METHODDEFORREF_BITS);
1131 return get_method (m, token);
1136 g_assert_not_reached ();
1138 g_assert_not_reached ();
1144 * @m: metadata context
1145 * @idx: index into the method table
1147 * Returns: A stringified version of the method signature.
1150 get_methoddef (MonoImage *m, guint32 idx)
1152 guint32 cols [MONO_METHOD_SIZE];
1158 mh = mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
1160 sig = dis_stringify_object_with_class (m, mh->klass);
1161 name = g_strdup_printf ("%s::%s", sig, mh->name);
1165 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD],
1166 idx - 1, cols, MONO_METHOD_SIZE);
1167 sig = get_methodref_signature (m, cols [MONO_METHOD_SIGNATURE], name);
1174 * @m: metadata context
1175 * @blob_index: index into the blob where the constant is stored
1177 * Returns: An allocated value representing a stringified version of the
1181 get_constant (MonoImage *m, MonoTypeEnum t, guint32 blob_index)
1183 const char *ptr = mono_metadata_blob_heap (m, blob_index);
1186 len = mono_metadata_decode_value (ptr, &ptr);
1189 case MONO_TYPE_BOOLEAN:
1190 return g_strdup_printf ("%s", *ptr ? "true" : "false");
1192 case MONO_TYPE_CHAR:
1193 return g_strdup_printf ("%c", *ptr); /* FIXME: unicode char */
1197 return g_strdup_printf ("int8(0x%02x)", (int) (*ptr));
1202 return g_strdup_printf ("int16(0x%08x)", (int) read16 (ptr));
1206 return g_strdup_printf ("int32(%d)", read32 (ptr));
1208 case MONO_TYPE_I8: {
1211 high = read32 (ptr + 4);
1212 return g_strdup_printf ("0x%08x%08x", high, low);
1214 case MONO_TYPE_U8: {
1217 high = read32 (ptr + 4);
1218 return g_strdup_printf ("0x%08x%08x", high, low);
1220 case MONO_TYPE_R4: {
1223 return g_strdup_printf ("%g", (double) r);
1225 case MONO_TYPE_R8: {
1228 return g_strdup_printf ("%g", r);
1230 case MONO_TYPE_STRING: {
1234 for (i = 0; !ptr [i+1]; i += 2){
1239 case '\n': /* add more */
1243 res = g_malloc (len + e + 3);
1247 for (i = 0; i < len; i += 2){
1260 res[j++] = isprint (ptr [i]) ? ptr [i] : '.';
1269 case MONO_TYPE_CLASS:
1270 return g_strdup ("CLASS CONSTANT. MUST BE ZERO");
1273 g_error ("Unknown MONO_TYPE (%d) on constant at Blob index (0x%08x)\n",
1274 (int) *ptr, blob_index);
1275 return g_strdup_printf ("Unknown");
1282 * @m: metadata context
1283 * @token: token that we want to decode.
1285 * Returns: An allocated value representing a stringified version of the
1289 get_token (MonoImage *m, guint32 token)
1291 guint32 idx = mono_metadata_token_index (token);
1293 switch (mono_metadata_token_code (token)){
1294 case MONO_TOKEN_FIELD_DEF:
1295 return (get_field (m, token));
1296 case MONO_TOKEN_TYPE_DEF:
1297 return get_typedef (m, idx);
1298 case MONO_TOKEN_TYPE_REF:
1299 return get_typeref (m, idx);
1300 case MONO_TOKEN_TYPE_SPEC:
1301 return get_typespec (m, idx);
1303 g_error ("Do not know how to decode tokens of type 0x%08x", token);
1306 g_assert_not_reached ();
1307 return g_strdup ("ERROR");
1312 * @m: metadata context
1313 * @token: the token can belong to any of the following tables:
1314 * MONO_TOKEN_TYPE_REF, MONO_TOKEN_TYPE_DEF, MONO_TOKEN_TYPE_SPEC
1316 * Returns: a stringified version of the MethodDef or MethodRef or TypeSpecn
1317 * at (token & 0xffffff)
1320 get_token_type (MonoImage *m, guint32 token)
1322 char *temp = NULL, *s;
1325 idx = mono_metadata_token_index (token);
1327 switch (mono_metadata_token_code (token)){
1328 case MONO_TOKEN_TYPE_DEF:
1329 temp = get_typedef (m, idx);
1330 s = g_strdup_printf ("%s", temp);
1333 case MONO_TOKEN_TYPE_REF:
1334 temp = get_typeref (m, idx);
1335 s = g_strdup_printf ("%s", temp);
1338 case MONO_TOKEN_TYPE_SPEC:
1339 s = get_typespec (m, idx);
1343 g_error ("Unhandled encoding for token 0x%08x", token);
1354 get_guid (MonoImage *m, guint32 guid_index)
1356 const unsigned char *guid;
1359 guid = mono_metadata_guid_heap (m, guid_index);
1361 result = g_strdup_printf ("{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
1362 guid [3], guid [2], guid [1], guid [0], guid [5], guid [4], guid [7], guid [6],
1363 guid [8], guid [9], guid [10], guid [11], guid [12], guid [13], guid [14], guid [15]);
1368 dis_get_custom_attrs (MonoImage *m, guint32 token)
1371 guint32 idx, i, len, mtoken;
1372 guint32 cols [MONO_CUSTOM_ATTR_SIZE];
1378 idx = mono_metadata_token_index (token);
1379 idx <<= CUSTOM_ATTR_BITS;
1381 switch (mono_metadata_token_table (token)) {
1382 case MONO_TABLE_TYPEDEF:
1383 idx |= CUSTOM_ATTR_TYPEDEF;
1385 case MONO_TABLE_ASSEMBLY:
1386 idx |= CUSTOM_ATTR_ASSEMBLY;
1388 case MONO_TABLE_PROPERTY:
1389 idx |= CUSTOM_ATTR_PROPERTY;
1391 case MONO_TABLE_EVENT:
1392 idx |= CUSTOM_ATTR_EVENT;
1394 case MONO_TABLE_FIELD:
1395 idx |= CUSTOM_ATTR_FIELDDEF;
1397 case MONO_TABLE_METHOD:
1398 idx |= CUSTOM_ATTR_METHODDEF;
1400 case MONO_TABLE_PARAM:
1401 idx |= CUSTOM_ATTR_PARAMDEF;
1404 g_print ("Missing custom attr get support for token 0x%08x\n", token);
1408 ca = &m->tables [MONO_TABLE_CUSTOMATTRIBUTE];
1409 /* the table is not sorted */
1410 for (i = 0; i < ca->rows; ++i) {
1412 mono_metadata_decode_row (ca, i, cols, MONO_CUSTOM_ATTR_SIZE);
1413 if (cols [MONO_CUSTOM_ATTR_PARENT] != idx)
1415 mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> CUSTOM_ATTR_TYPE_BITS;
1416 switch (cols [MONO_CUSTOM_ATTR_TYPE] & CUSTOM_ATTR_TYPE_MASK) {
1417 case CUSTOM_ATTR_TYPE_METHODDEF:
1418 mtoken |= MONO_TOKEN_METHOD_DEF;
1420 case CUSTOM_ATTR_TYPE_MEMBERREF:
1421 mtoken |= MONO_TOKEN_MEMBER_REF;
1424 g_error ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
1427 method = get_method (m, mtoken);
1428 val = mono_metadata_blob_heap (m, cols [MONO_CUSTOM_ATTR_VALUE]);
1429 len = mono_metadata_decode_value (val, &val);
1430 attr = g_string_new (".custom ");
1431 dump = data_dump (val, len, "\t\t");
1432 g_string_sprintfa (attr, "%s = %s", method, dump);
1434 list = g_list_append (list, attr->str);
1435 g_string_free (attr, FALSE);
1442 get_marshal_info (MonoImage *m, const char *blob) {
1445 len = mono_metadata_decode_blob_size (blob, &blob);
1448 case MONO_NATIVE_BOOLEAN:
1449 return g_strdup ("bool");
1450 case MONO_NATIVE_I1:
1451 return g_strdup ("int8");
1452 case MONO_NATIVE_U1:
1453 return g_strdup ("unsigned int8");
1454 case MONO_NATIVE_I2:
1455 return g_strdup ("int16");
1456 case MONO_NATIVE_U2:
1457 return g_strdup ("unsigned int16");
1458 case MONO_NATIVE_I4:
1459 return g_strdup ("int32");
1460 case MONO_NATIVE_U4:
1461 return g_strdup ("unsigned int32");
1462 case MONO_NATIVE_I8:
1463 return g_strdup ("int64");
1464 case MONO_NATIVE_U8:
1465 return g_strdup ("unsigned int64");
1466 case MONO_NATIVE_R4:
1467 return g_strdup ("float32");
1468 case MONO_NATIVE_R8:
1469 return g_strdup ("float64");
1470 case MONO_NATIVE_CURRENCY:
1471 return g_strdup ("currency");
1472 case MONO_NATIVE_BSTR:
1473 return g_strdup ("bstr");
1474 case MONO_NATIVE_LPSTR:
1475 return g_strdup ("lpstr");
1476 case MONO_NATIVE_LPWSTR:
1477 return g_strdup ("lpwstr");
1478 case MONO_NATIVE_LPTSTR:
1479 return g_strdup ("lptstr");
1480 case MONO_NATIVE_BYVALTSTR:
1481 size = mono_metadata_decode_value (blob + 1, &blob);
1482 return g_strdup_printf ("fixed sysstring [%d]", size);
1483 case MONO_NATIVE_IUNKNOWN:
1484 return g_strdup ("iunknown");
1485 case MONO_NATIVE_IDISPATCH:
1486 return g_strdup ("idispatch");
1487 case MONO_NATIVE_STRUCT:
1488 return g_strdup ("struct");
1489 case MONO_NATIVE_INTERFACE:
1490 return g_strdup ("interface");
1491 case MONO_NATIVE_SAFEARRAY:
1492 return g_strdup ("safearray");
1493 case MONO_NATIVE_BYVALARRAY:
1494 size = mono_metadata_decode_value (blob + 1, &blob);
1495 return g_strdup_printf ("fixed array [%d]", size);
1496 case MONO_NATIVE_INT:
1497 return g_strdup ("int");
1498 case MONO_NATIVE_UINT:
1499 return g_strdup ("unsigned int");
1500 case MONO_NATIVE_VBBYREFSTR:
1501 return g_strdup ("vbbyrefstr");
1502 case MONO_NATIVE_ANSIBSTR:
1503 return g_strdup ("ansi bstr");
1504 case MONO_NATIVE_TBSTR:
1505 return g_strdup ("tbstr");
1506 case MONO_NATIVE_VARIANTBOOL:
1507 return g_strdup ("variant bool");
1508 case MONO_NATIVE_FUNC:
1509 return g_strdup ("method");
1510 case MONO_NATIVE_ASANY:
1511 return g_strdup ("as any");
1512 case MONO_NATIVE_LPARRAY:
1513 return g_strdup ("[]");
1514 case MONO_NATIVE_LPSTRUCT:
1515 return g_strdup ("lpstruct");
1516 case MONO_NATIVE_CUSTOM:
1517 return g_strdup ("custom");
1518 case MONO_NATIVE_ERROR:
1519 return g_strdup ("error");
1521 return g_strdup ("unknown");