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_typedef (MonoImage *m, int idx)
26 guint32 cols [MONO_TYPEDEF_SIZE];
29 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEDEF], idx - 1, cols, MONO_TYPEDEF_SIZE);
31 ns = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]);
32 return g_strdup_printf (
33 "%s%s%s", ns, *ns?".":"",
34 mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME]));
38 get_module (MonoImage *m, int idx)
40 guint32 cols [MONO_MODULE_SIZE];
43 * There MUST BE only one module in the Module table
47 mono_metadata_decode_row (&m->tables [MONO_TABLE_MODULE], idx - 1, cols, MONO_MODULE_SIZE);
49 return g_strdup (mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]));
53 get_assemblyref (MonoImage *m, int idx)
55 guint32 cols [MONO_ASSEMBLYREF_SIZE];
57 mono_metadata_decode_row (&m->tables [MONO_TABLE_ASSEMBLYREF], idx - 1, cols, MONO_ASSEMBLYREF_SIZE);
59 return g_strdup (mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
64 * Returns a string representing the ArrayShape (22.2.16).
67 get_array_shape (MonoImage *m, const char *ptr, char **result)
69 GString *res = g_string_new ("[");
70 guint32 rank, num_sizes, num_lo_bounds;
71 guint32 *sizes = NULL, *lo_bounds = NULL;
75 rank = mono_metadata_decode_value (ptr, &ptr);
76 num_sizes = mono_metadata_decode_value (ptr, &ptr);
79 sizes = g_new (guint32, num_sizes);
81 for (i = 0; i < num_sizes; i++)
82 sizes [i] = mono_metadata_decode_value (ptr, &ptr);
84 num_lo_bounds = mono_metadata_decode_value (ptr, &ptr);
85 if (num_lo_bounds > 0)
86 lo_bounds = g_new (guint32, num_lo_bounds);
88 for (i = 0; i < num_lo_bounds; i++)
89 lo_bounds [i] = mono_metadata_decode_value (ptr, &ptr);
91 for (r = 0; r < rank; r++){
93 if (r < num_lo_bounds){
94 sprintf (buffer, "%d..%d", lo_bounds [r], lo_bounds [r] + sizes [r] - 1);
96 sprintf (buffer, "0..%d", sizes [r] - 1);
101 g_string_append (res, buffer);
103 g_string_append (res, ", ");
105 g_string_append (res, "]");
114 g_string_free (res, FALSE);
121 * @m: metadata context
122 * @blob_idx: index into the blob heap
124 * Returns the stringified representation of a TypeSpec signature (22.2.17)
127 get_typespec (MonoImage *m, guint32 idx)
129 guint32 cols [MONO_TYPESPEC_SIZE];
132 GString *res = g_string_new ("");
135 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPESPEC], idx-1, cols, MONO_TYPESPEC_SIZE);
136 ptr = mono_metadata_blob_heap (m, cols [MONO_TYPESPEC_SIGNATURE]);
137 len = mono_metadata_decode_value (ptr, &ptr);
141 ptr = get_custom_mod (m, ptr, &s);
143 g_string_append (res, s);
144 g_string_append_c (res, ' ');
148 if (*ptr == MONO_TYPE_VOID)
149 g_string_append (res, "void");
151 ptr = get_type (m, ptr, &s);
153 g_string_append (res, s);
157 case MONO_TYPE_FNPTR:
158 g_string_append (res, "FNPTR ");
160 * we assume MethodRefSig, as we do not know
161 * whether it is a MethodDefSig or a MethodRefSig.
163 printf ("\n FNPTR:\n");
165 hex_dump (ptr, 0, 40);
168 case MONO_TYPE_ARRAY:
169 ptr = get_type (m, ptr, &s);
170 g_string_append (res, s);
172 g_string_append_c (res, ' ');
173 ptr = get_array_shape (m, ptr, &s);
174 g_string_append (res, s);
178 case MONO_TYPE_SZARRAY:
179 ptr = get_custom_mod (m, ptr, &s);
181 g_string_append (res, s);
182 g_string_append_c (res, ' ');
185 ptr = get_type (m, ptr, &s);
186 g_string_append (res, s);
187 g_string_append (res, "[]");
192 g_string_free (res, FALSE);
198 get_typeref (MonoImage *m, int idx)
200 guint32 cols [MONO_TYPEREF_SIZE];
203 guint32 rs_idx, table;
205 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEREF], idx - 1, cols, MONO_TYPEREF_SIZE);
207 t = mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAME]);
208 s = mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAMESPACE]);
210 rs_idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
211 table = cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK;
214 case RESOLTION_SCOPE_MODULE: /* Module */
215 x = get_module (m, rs_idx);
216 ret = g_strdup_printf ("[%s] %s%s%s", x, s, *s?".":"", t);
220 case RESOLTION_SCOPE_MODULEREF: /* ModuleRef */
221 ret = g_strdup_printf ("TODO:TypeRef-ModuleRef (%s.%s)", s, t);
224 case RESOLTION_SCOPE_ASSEMBLYREF: /*
225 * AssemblyRef (ECMA docs claim it is 3, but it looks to
226 * me like it is 2 (tokens are prefixed with 0x23)
228 x = get_assemblyref (m, rs_idx);
229 ret = g_strdup_printf ("[%s]%s%s%s", x, s, *s?".":"", t);
233 case RESOLTION_SCOPE_TYPEREF: /* TypeRef */
234 x = get_typeref (m, rs_idx);
235 ret = g_strdup_printf ("%s/%s", x, t);
240 ret = g_strdup_printf ("Unknown table in TypeRef %d", table);
247 * get_typedef_or_ref:
248 * @m: metadata context
249 * @dor_token: def or ref encoded index
251 * Low two bits contain table to lookup from
252 * high bits contain the index into the def or ref table
254 * Returns: a stringified version of the MethodDef or MethodRef
255 * at (dor_token >> 2)
258 get_typedef_or_ref (MonoImage *m, guint32 dor_token)
260 char *temp = NULL, *s;
264 * low 2 bits contain encoding
266 table = dor_token & 0x03;
267 idx = dor_token >> 2;
270 case 0: /* TypeDef */
271 temp = get_typedef (m, idx);
272 s = g_strdup_printf ("%s", temp);
275 case 1: /* TypeRef */
276 temp = get_typeref (m, idx);
277 s = g_strdup_printf ("%s", temp);
280 case 2: /* TypeSpec */
281 s = get_typespec (m, idx);
285 g_error ("Unhandled encoding for typedef-or-ref coded index 0x%08x", dor_token);
296 * get_encoded_typedef_or_ref:
297 * @m: metadata context
298 * @ptr: location to decode from.
299 * @result: pointer to string where resulting decoded string is stored
301 * result will point to a g_malloc()ed string.
303 * Returns: the new ptr to continue decoding
306 get_encoded_typedef_or_ref (MonoImage *m, const char *ptr, char **result)
310 token = mono_metadata_decode_value (ptr, &ptr);
312 *result = get_typedef_or_ref (m, token);
320 * Decodes a CustomMod (22.2.7)
322 * Returns: updated pointer location
325 get_custom_mod (MonoImage *m, const char *ptr, char **return_value)
330 *return_value = NULL;
331 while ((*ptr == MONO_TYPE_CMOD_OPT) ||
332 (*ptr == MONO_TYPE_CMOD_REQD)) {
333 reqd = (*ptr == MONO_TYPE_CMOD_REQD) ? "reqd" : "opt";
335 ptr = get_encoded_typedef_or_ref (m, ptr, &s);
337 if (*return_value == NULL)
338 *return_value = g_strconcat (reqd, " ", s, NULL);
340 *return_value = g_strconcat (*return_value, " ", reqd, " ", s, NULL);
347 static map_t element_type_map [] = {
348 { MONO_TYPE_END , "end" },
349 { MONO_TYPE_VOID , "void" },
350 { MONO_TYPE_BOOLEAN , "bool" },
351 { MONO_TYPE_CHAR , "char" },
352 { MONO_TYPE_I1 , "int8" },
353 { MONO_TYPE_U1 , "unsigned int8" },
354 { MONO_TYPE_I2 , "int16" },
355 { MONO_TYPE_U2 , "uint16" },
356 { MONO_TYPE_I4 , "int32" },
357 { MONO_TYPE_U4 , "uint32" },
358 { MONO_TYPE_I8 , "int64" },
359 { MONO_TYPE_U8 , "uint64" },
360 { MONO_TYPE_R4 , "float32" },
361 { MONO_TYPE_R8 , "float64" },
362 { MONO_TYPE_STRING , "string" },
363 { MONO_TYPE_TYPEDBYREF , "TypedByRef" },
364 { MONO_TYPE_I , "native int" },
365 { MONO_TYPE_U , "native unsigned int" },
366 { MONO_TYPE_OBJECT , "object" },
370 static map_t call_conv_type_map [] = {
371 { MONO_CALL_DEFAULT , "default" },
372 { MONO_CALL_C , "c" },
373 { MONO_CALL_STDCALL , "stdcall" },
374 { MONO_CALL_THISCALL , "thiscall" },
375 { MONO_CALL_FASTCALL , "fastcall" },
376 { MONO_CALL_VARARG , "vararg" },
381 dis_stringify_token (MonoImage *m, guint32 token)
383 guint idx = token & 0xffffff;
384 switch (token >> 24) {
385 case MONO_TABLE_TYPEDEF: return get_typedef (m, idx);
386 case MONO_TABLE_TYPEREF: return get_typeref (m, idx);
387 case MONO_TABLE_TYPESPEC: return get_typespec (m, idx);
391 return g_strdup_printf("0x%08x", token);
395 dis_stringify_array (MonoImage *m, MonoArrayType *array)
398 GString *s = g_string_new("");
401 type = dis_stringify_type (m, &array->eklass->byval_arg);
402 g_string_append (s, type);
404 g_string_append_c (s, '[');
405 for (i = 0; i < array->rank; ++i) {
407 g_string_append_c (s, ',');
408 if (i < array->numsizes) {
409 if (i < array->numlobounds && array->lobounds[i] != 0)
410 g_string_sprintfa (s, "%d..%d", array->lobounds[i], array->sizes[i]);
412 g_string_sprintfa (s, "%d", array->sizes[i]);
415 g_string_append_c (s, ']');
417 g_string_free (s, FALSE);
422 dis_stringify_modifiers (MonoImage *m, int n, MonoCustomMod *mod)
424 GString *s = g_string_new("");
427 for (i = 0; i < n; ++i) {
428 char *tok = dis_stringify_token (m, mod[i].token);
430 g_string_sprintfa (s, " ");
431 g_string_sprintfa (s, "%s (%s)", mod[i].required ? "modopt": "modreq", tok);
434 g_string_append_c (s, ' ');
436 g_string_free (s, FALSE);
441 dis_stringify_param (MonoImage *m, MonoType *param)
445 const char *out = param->attrs & 2 ? "[out] ": "";
446 t = dis_stringify_type (m, param);
447 result = g_strconcat (out, t, NULL);
453 dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int methoddef_row, gboolean fully_qualified)
455 guint32 cols [MONO_METHOD_SIZE];
456 guint32 pcols [MONO_PARAM_SIZE];
457 guint32 param_index = 0;
458 const char *name = "";
462 GString *result = g_string_new ("");
465 g_assert (method || methoddef_row);
468 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD], methoddef_row -1, cols, MONO_METHOD_SIZE);
470 type = get_typedef (m, mono_metadata_typedef_from_method (m, methoddef_row));
471 name = mono_metadata_string_heap (m, cols [MONO_METHOD_NAME]);
472 param_index = cols [MONO_METHOD_PARAMLIST];
474 const char *sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
475 mono_metadata_decode_blob_size (sig, &sig);
476 method = mono_metadata_parse_method_signature (m, methoddef_row, sig, &sig);
481 retval = dis_stringify_param (m, method->ret);
483 g_string_append (result, "instance ");
484 g_string_append (result, map (method->call_convention, call_conv_type_map));
485 g_string_sprintfa (result, " %s ", retval);
487 g_string_sprintfa (result, "%s::", type);
488 g_string_sprintfa (result, "%s(", name);
490 for (i = 0; i < method->param_count; ++i) {
491 if (param_index && param_index <= m->tables [MONO_TABLE_PARAM].rows) {
492 mono_metadata_decode_row (&m->tables [MONO_TABLE_PARAM], param_index - 1, pcols, MONO_PARAM_SIZE);
493 name = mono_metadata_string_heap (m, pcols [MONO_PARAM_NAME]);
494 method->params [i]->attrs = pcols [MONO_PARAM_FLAGS];
500 g_string_append (result, ", ");
501 retval = dis_stringify_param (m, method->params [i]);
502 g_string_sprintfa (result, "%s %s", retval, name);
505 g_string_append (result, ") ");
508 mono_metadata_free_method_signature (method);
509 retval = result->str;
510 g_string_free (result, FALSE);
516 dis_stringify_object_with_class (MonoImage *m, MonoClass *c)
518 /* FIXME: handle MONO_TYPE_OBJECT ... */
519 const char *otype = c->byval_arg.type == MONO_TYPE_CLASS? "class" : "valuetype";
520 char *assemblyref = NULL, *result;
522 if (c->image->assembly_name) {
524 if (substitute_with_mscorlib_p && !strcmp ("corlib", c->image->assembly_name))
525 assemblyref = g_strdup_printf ("[%s]", "mscorlib");
527 assemblyref = g_strdup_printf ("[%s]", c->image->assembly->aname.name);
529 assemblyref = g_strdup_printf ("[.module %s]", c->image->module_name);
532 result = g_strdup_printf ("%s %s%s%s%s", otype, assemblyref?assemblyref:"", c->name_space,
533 *c->name_space?".":"", c->name);
534 g_free (assemblyref);
539 dis_stringify_object (MonoImage *m, MonoType *type)
541 MonoClass *c = mono_class_from_mono_type (type);
542 return dis_stringify_object_with_class (m, c);
546 dis_stringify_type (MonoImage *m, MonoType *type)
548 const char *pinned = "", *byref = "";
549 char *bare = NULL, *mods = NULL;
553 mods = dis_stringify_modifiers (m, type->num_mods, type->modifiers);
556 case MONO_TYPE_BOOLEAN:
570 case MONO_TYPE_STRING:
571 case MONO_TYPE_OBJECT:
572 case MONO_TYPE_TYPEDBYREF:
573 bare = g_strdup (map (type->type, element_type_map));
576 case MONO_TYPE_VALUETYPE:
577 case MONO_TYPE_CLASS:
578 bare = dis_stringify_object (m, type);
581 case MONO_TYPE_FNPTR:
582 bare = dis_stringify_method_signature (m, type->data.method, 0, FALSE);
584 case MONO_TYPE_PTR: {
586 child_type = dis_stringify_type (m, type->data.type);
588 bare = g_strdup_printf ("%s*", child_type);
592 case MONO_TYPE_SZARRAY: {
594 child_type = dis_stringify_type (m, &type->data.klass->byval_arg);
596 bare = g_strdup_printf ("%s[]", child_type);
600 case MONO_TYPE_ARRAY:
601 bare = dis_stringify_array (m, type->data.array);
604 bare = g_strdup ("void");
607 bare = g_strdup_printf ("!!%d", type->data.type_param);
610 bare = g_strdup_printf ("!%d", type->data.type_param);
612 case MONO_TYPE_GENERICINST: {
613 GString *str = g_string_new ("");
615 char *generic_type = dis_stringify_type (m, type->data.generic_inst->generic_type);
617 for (i = 0; i < type->data.generic_inst->type_argc; i++){
618 char *t = dis_stringify_type (m, type->data.generic_inst->type_argv [i]);
620 g_string_append (str, t);
621 if (i+1 != type->data.generic_inst->type_argc)
622 g_string_append (str, ", ");
625 bare = g_strdup_printf ("%s<%s>", generic_type, str->str);
626 g_string_free (str, TRUE);
631 g_error ("Do not know how to stringify type 0x%x", type->type);
640 result = g_strconcat (mods ? mods : "", bare, byref, pinned, NULL);
649 * @m: metadata context
650 * @ptr: location to decode from.
651 * @result: pointer to string where resulting decoded string is stored
653 * This routine returs in @result the stringified type pointed by @ptr.
656 * Returns: the new ptr to continue decoding
659 get_type (MonoImage *m, const char *ptr, char **result)
661 MonoType *type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
662 *result = dis_stringify_type (m, type);
663 mono_metadata_free_type (type);
669 * Returns a stringified representation of a FieldSig (22.2.4)
672 get_field_signature (MonoImage *m, guint32 blob_signature)
674 char *allocated_modifier_string, *allocated_type_string;
675 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
680 len = mono_metadata_decode_value (ptr, &ptr);
683 g_assert (*ptr == 0x06);
684 /* hex_dump (ptr, 0, len); */
687 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
688 ptr = get_type (m, ptr, &allocated_type_string);
690 res = g_strdup_printf (
692 allocated_modifier_string ? allocated_modifier_string : "",
693 allocated_type_string);
695 if (allocated_modifier_string)
696 g_free (allocated_modifier_string);
697 if (allocated_type_string)
698 g_free (allocated_type_string);
704 get_field_literal_type (MonoImage *m, guint32 blob_signature)
706 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
708 char *allocated_modifier_string;
710 len = mono_metadata_decode_value (ptr, &ptr);
713 g_assert (*ptr == 0x06);
716 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
717 if (allocated_modifier_string)
718 g_free (allocated_modifier_string);
720 return (MonoTypeEnum) *ptr;
726 * @m: metadata context
727 * @token: token to decode
729 * decodes the literal indexed by @token.
732 decode_literal (MonoImage *m, guint32 token)
734 return g_strdup ("LITERAL_VALUE");
739 * @m: metadata context
740 * @ptr: location to decode from.
741 * @result: pointer to string where resulting decoded string is stored
743 * This routine returns in @result the stringified RetType (22.2.11)
745 * Returns: the new ptr to continue decoding.
748 get_ret_type (MonoImage *m, const char *ptr, char **ret_type)
750 GString *str = g_string_new ("");
752 char *allocated_type_string;
754 ptr = get_custom_mod (m, ptr, &mod);
756 g_string_append (str, mod);
757 g_string_append_c (str, ' ');
761 if (*ptr == MONO_TYPE_TYPEDBYREF){
762 /* TODO: what does `typedbyref' mean? */
763 g_string_append (str, "/* FIXME: What does this mean? */ typedbyref ");
765 } else if (*ptr == MONO_TYPE_VOID){
766 g_string_append (str, "void");
769 if (*ptr == MONO_TYPE_BYREF){
770 g_string_append (str, "[out] ");
774 ptr = get_type (m, ptr, &allocated_type_string);
775 g_string_append (str, allocated_type_string);
776 g_free (allocated_type_string);
779 *ret_type = str->str;
780 g_string_free (str, FALSE);
787 * @m: metadata context
788 * @ptr: location to decode from.
789 * @result: pointer to string where resulting decoded string is stored
791 * This routine returns in @result the stringified Param (22.2.10)
793 * Returns: the new ptr to continue decoding.
796 get_param (MonoImage *m, const char *ptr, char **retval)
798 GString *str = g_string_new ("");
799 char *allocated_mod_string, *allocated_type_string;
801 ptr = get_custom_mod (m, ptr, &allocated_mod_string);
802 if (allocated_mod_string){
803 g_string_append (str, allocated_mod_string);
804 g_string_append_c (str, ' ');
805 g_free (allocated_mod_string);
808 if (*ptr == MONO_TYPE_TYPEDBYREF){
809 g_string_append (str, "/*FIXME: what does typedbyref mean? */ typedbyref ");
812 if (*ptr == MONO_TYPE_BYREF){
813 g_string_append (str, "[out] ");
816 ptr = get_type (m, ptr, &allocated_type_string);
817 g_string_append (str, allocated_type_string);
818 g_free (allocated_type_string);
822 g_string_free (str, FALSE);
826 static map_t param_map [] = {
827 { PARAM_ATTRIBUTE_IN, "[in] " },
828 { PARAM_ATTRIBUTE_OUT, "[out] " },
829 { PARAM_ATTRIBUTE_OPTIONAL, "optional " },
830 { PARAM_ATTRIBUTE_HAS_DEFAULT, "hasdefault " },
831 { PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL, "fieldmarshal " },
836 param_flags (guint32 f)
838 return g_strdup (flags (f, param_map));
841 static map_t field_access_map [] = {
842 { FIELD_ATTRIBUTE_COMPILER_CONTROLLED, "compilercontrolled " },
843 { FIELD_ATTRIBUTE_PRIVATE, "private " },
844 { FIELD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
845 { FIELD_ATTRIBUTE_ASSEMBLY, "assembly " },
846 { FIELD_ATTRIBUTE_FAMILY, "family " },
847 { FIELD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
848 { FIELD_ATTRIBUTE_PUBLIC, "public " },
852 static map_t field_flags_map [] = {
853 { FIELD_ATTRIBUTE_STATIC, "static " },
854 { FIELD_ATTRIBUTE_INIT_ONLY, "initonly " },
855 { FIELD_ATTRIBUTE_LITERAL, "literal " },
856 { FIELD_ATTRIBUTE_NOT_SERIALIZED, "notserialized " },
857 { FIELD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
858 { FIELD_ATTRIBUTE_PINVOKE_IMPL, "FIXME:pinvokeimpl " },
859 { FIELD_ATTRIBUTE_RT_SPECIAL_NAME, "rtspecialname " },
860 { FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL, "hasfieldmarshal " },
861 { FIELD_ATTRIBUTE_HAS_DEFAULT, "hasdefault " },
862 { FIELD_ATTRIBUTE_HAS_FIELD_RVA, "hasfieldrva " },
869 * Returns a stringified version of a Field's flags
872 field_flags (guint32 f)
875 int access = f & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK;
879 strcat (buffer, map (access, field_access_map));
880 strcat (buffer, flags (f, field_flags_map));
881 return g_strdup (buffer);
885 * Returns a stringifed representation of a MethodRefSig (22.2.2)
888 get_methodref_signature (MonoImage *m, guint32 blob_signature, const char *fancy_name)
890 GString *res = g_string_new ("");
891 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
892 char *allocated_ret_type, *s;
893 gboolean seen_vararg = 0;
894 int param_count, signature_len;
895 int i, gen_count = 0;
898 signature_len = mono_metadata_decode_value (ptr, &ptr);
902 g_string_append (res, "explicit-this ");
904 g_string_append (res, "instance "); /* has-this */
914 gen_count = mono_metadata_decode_value (ptr, &ptr);
915 param_count = mono_metadata_decode_value (ptr, &ptr);
917 ptr = get_ret_type (m, ptr, &allocated_ret_type);
918 g_string_append (res, allocated_ret_type);
919 g_free (allocated_ret_type);
923 g_string_append_c (res, ' ');
924 g_string_append (res, fancy_name);
927 g_string_append (res, "(");
930 * param_count describes parameters *before* and *after*
931 * the vararg sentinel
933 for (i = 0; i < param_count; i++){
937 * If ptr is a SENTINEL
940 g_string_append (res, " varargs ");
944 ptr = get_param (m, ptr, ¶m);
945 g_string_append (res, param);
946 if (i+1 != param_count)
947 g_string_append (res, ", ");
950 g_string_append (res, ")");
956 g_string_free (res, FALSE);
962 * @m: metadata context
963 * @token: a FIELD_DEF token
965 * This routine has to locate the TypeDef that "owns" this Field.
966 * Since there is no backpointer in the Field table, we have to scan
967 * the TypeDef table and locate the actual "owner" of the field
970 get_field (MonoImage *m, guint32 token)
972 int idx = mono_metadata_token_index (token);
973 guint32 cols [MONO_FIELD_SIZE];
974 char *sig, *res, *type;
978 * We can get here also with a MenberRef token (for a field
979 * defined in another module/assembly, just like in get_method ()
981 if (mono_metadata_token_code (token) == MONO_TOKEN_MEMBER_REF) {
982 return g_strdup_printf ("fieldref-0x%08x", token);
984 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
986 mono_metadata_decode_row (&m->tables [MONO_TABLE_FIELD], idx - 1, cols, MONO_FIELD_SIZE);
987 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
990 * To locate the actual "container" for this field, we have to scan
991 * the TypeDef table. LAME!
993 type_idx = mono_metadata_typedef_from_field (m, idx);
995 type = get_typedef (m, type_idx);
996 res = g_strdup_printf ("%s %s::%s",
998 mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
1006 get_memberref_parent (MonoImage *m, guint32 mrp_token)
1009 * mrp_index is a MemberRefParent coded index
1011 guint32 table = mrp_token & 7;
1012 guint32 idx = mrp_token >> 3;
1015 case 0: /* TypeDef */
1016 return get_typedef (m, idx);
1018 case 1: /* TypeRef */
1019 return get_typeref (m, idx);
1021 case 2: /* ModuleRef */
1022 return g_strdup_printf ("TODO:MemberRefParent-ModuleRef");
1024 case 3: /* MethodDef */
1025 return g_strdup ("TODO:MethodDef");
1027 case 4: /* TypeSpec */
1028 return get_typespec (m, idx);
1030 g_assert_not_reached ();
1036 * @m: metadata context
1037 * @token: a METHOD_DEF or MEMBER_REF token
1039 * This routine has to locate the TypeDef that "owns" this Field.
1040 * Since there is no backpointer in the Field table, we have to scan
1041 * the TypeDef table and locate the actual "owner" of the field
1044 get_method (MonoImage *m, guint32 token)
1046 int idx = mono_metadata_token_index (token);
1047 guint32 member_cols [MONO_MEMBERREF_SIZE], method_cols [MONO_METHOD_SIZE];
1053 mh = mono_get_method (m, token, NULL);
1055 sig = dis_stringify_object_with_class (m, mh->klass);
1056 name = g_strdup_printf ("%s::%s", sig, mh->name);
1061 switch (mono_metadata_token_code (token)){
1062 case MONO_TOKEN_METHOD_DEF:
1063 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD],
1064 idx - 1, method_cols, MONO_METHOD_SIZE);
1066 sig = get_methodref_signature (m, method_cols [MONO_METHOD_SIGNATURE], name);
1069 case MONO_TOKEN_MEMBER_REF: {
1070 mono_metadata_decode_row (&m->tables [MONO_TABLE_MEMBERREF],
1071 idx - 1, member_cols, MONO_MEMBERREF_SIZE);
1073 name = g_strdup_printf ("%s::%s",
1074 get_memberref_parent (m, member_cols [MONO_MEMBERREF_CLASS]),
1075 mono_metadata_string_heap (m, member_cols [MONO_MEMBERREF_NAME]));
1076 sig = get_methodref_signature (
1077 m, member_cols [MONO_MEMBERREF_SIGNATURE], name);
1080 case MONO_TOKEN_METHOD_SPEC: {
1081 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHODSPEC], idx - 1, member_cols, MONO_METHODSPEC_SIZE);
1082 token = member_cols [MONO_METHODSPEC_METHOD];
1083 if ((token & METHODDEFORREF_MASK) == METHODDEFORREF_METHODDEF)
1084 token = MONO_TOKEN_METHOD_DEF | (token >> METHODDEFORREF_BITS);
1086 token = MONO_TOKEN_MEMBER_REF | (token >> METHODDEFORREF_BITS);
1087 return get_method (m, token);
1092 g_assert_not_reached ();
1094 g_assert_not_reached ();
1100 * @m: metadata context
1101 * @blob_index: index into the blob where the constant is stored
1103 * Returns: An allocated value representing a stringified version of the
1107 get_constant (MonoImage *m, MonoTypeEnum t, guint32 blob_index)
1109 const char *ptr = mono_metadata_blob_heap (m, blob_index);
1112 len = mono_metadata_decode_value (ptr, &ptr);
1115 case MONO_TYPE_BOOLEAN:
1116 return g_strdup_printf ("%s", *ptr ? "true" : "false");
1118 case MONO_TYPE_CHAR:
1119 return g_strdup_printf ("%c", *ptr); /* FIXME: unicode char */
1123 return g_strdup_printf ("int8(0x%02x)", (int) (*ptr));
1128 return g_strdup_printf ("int16(0x%08x)", (int) read16 (ptr));
1132 return g_strdup_printf ("int32(%d)", read32 (ptr));
1134 case MONO_TYPE_I8: {
1137 high = read32 (ptr + 4);
1138 return g_strdup_printf ("0x%08x%08x", high, low);
1140 case MONO_TYPE_U8: {
1143 high = read32 (ptr + 4);
1144 return g_strdup_printf ("0x%08x%08x", high, low);
1146 case MONO_TYPE_R4: {
1149 return g_strdup_printf ("%g", (double) r);
1151 case MONO_TYPE_R8: {
1154 return g_strdup_printf ("%g", r);
1156 case MONO_TYPE_STRING: {
1160 for (i = 0; !ptr [i+1]; i += 2){
1165 case '\n': /* add more */
1169 res = g_malloc (len + e + 3);
1173 for (i = 0; i < len; i += 2){
1186 res[j++] = isprint (ptr [i]) ? ptr [i] : '.';
1195 case MONO_TYPE_CLASS:
1196 return g_strdup ("CLASS CONSTANT. MUST BE ZERO");
1199 g_error ("Unknown MONO_TYPE (%d) on constant at Blob index (0x%08x)\n",
1200 (int) *ptr, blob_index);
1201 return g_strdup_printf ("Unknown");
1208 * @m: metadata context
1209 * @token: token that we want to decode.
1211 * Returns: An allocated value representing a stringified version of the
1215 get_token (MonoImage *m, guint32 token)
1217 guint32 idx = mono_metadata_token_index (token);
1219 switch (mono_metadata_token_code (token)){
1220 case MONO_TOKEN_FIELD_DEF:
1221 return (get_field (m, token));
1222 case MONO_TOKEN_TYPE_DEF:
1223 return get_typedef (m, idx);
1224 case MONO_TOKEN_TYPE_REF:
1225 return get_typeref (m, idx);
1226 case MONO_TOKEN_TYPE_SPEC:
1227 return get_typespec (m, idx);
1229 g_error ("Do not know how to decode tokens of type 0x%08x", token);
1232 g_assert_not_reached ();
1233 return g_strdup ("ERROR");
1238 * @m: metadata context
1239 * @token: the token can belong to any of the following tables:
1240 * MONO_TOKEN_TYPE_REF, MONO_TOKEN_TYPE_DEF, MONO_TOKEN_TYPE_SPEC
1242 * Returns: a stringified version of the MethodDef or MethodRef or TypeSpecn
1243 * at (token & 0xffffff)
1246 get_token_type (MonoImage *m, guint32 token)
1248 char *temp = NULL, *s;
1251 idx = mono_metadata_token_index (token);
1253 switch (mono_metadata_token_code (token)){
1254 case MONO_TOKEN_TYPE_DEF:
1255 temp = get_typedef (m, idx);
1256 s = g_strdup_printf ("%s", temp);
1259 case MONO_TOKEN_TYPE_REF:
1260 temp = get_typeref (m, idx);
1261 s = g_strdup_printf ("%s", temp);
1264 case MONO_TOKEN_TYPE_SPEC:
1265 s = get_typespec (m, idx);
1269 g_error ("Unhandled encoding for token 0x%08x", token);
1280 get_guid (MonoImage *m, guint32 guid_index)
1282 const unsigned char *guid;
1285 guid = mono_metadata_guid_heap (m, guid_index);
1287 result = g_strdup_printf ("{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
1288 guid [3], guid [2], guid [1], guid [0], guid [5], guid [4], guid [7], guid [6],
1289 guid [8], guid [9], guid [10], guid [11], guid [12], guid [13], guid [14], guid [15]);
1294 dis_get_custom_attrs (MonoImage *m, guint32 token)
1297 guint32 idx, i, len, mtoken;
1298 guint32 cols [MONO_CUSTOM_ATTR_SIZE];
1304 idx = mono_metadata_token_index (token);
1305 idx <<= CUSTOM_ATTR_BITS;
1307 switch (mono_metadata_token_table (token)) {
1308 case MONO_TABLE_TYPEDEF:
1309 idx |= CUSTOM_ATTR_TYPEDEF;
1311 case MONO_TABLE_ASSEMBLY:
1312 idx |= CUSTOM_ATTR_ASSEMBLY;
1314 case MONO_TABLE_PROPERTY:
1315 idx |= CUSTOM_ATTR_PROPERTY;
1317 case MONO_TABLE_EVENT:
1318 idx |= CUSTOM_ATTR_EVENT;
1320 case MONO_TABLE_FIELD:
1321 idx |= CUSTOM_ATTR_FIELDDEF;
1323 case MONO_TABLE_METHOD:
1324 idx |= CUSTOM_ATTR_METHODDEF;
1326 case MONO_TABLE_PARAM:
1327 idx |= CUSTOM_ATTR_PARAMDEF;
1330 g_print ("Missing custom attr get support for token 0x%08x\n", token);
1334 ca = &m->tables [MONO_TABLE_CUSTOMATTRIBUTE];
1335 /* the table is not sorted */
1336 for (i = 0; i < ca->rows; ++i) {
1338 mono_metadata_decode_row (ca, i, cols, MONO_CUSTOM_ATTR_SIZE);
1339 if (cols [MONO_CUSTOM_ATTR_PARENT] != idx)
1341 mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> CUSTOM_ATTR_TYPE_BITS;
1342 switch (cols [MONO_CUSTOM_ATTR_TYPE] & CUSTOM_ATTR_TYPE_MASK) {
1343 case CUSTOM_ATTR_TYPE_METHODDEF:
1344 mtoken |= MONO_TOKEN_METHOD_DEF;
1346 case CUSTOM_ATTR_TYPE_MEMBERREF:
1347 mtoken |= MONO_TOKEN_MEMBER_REF;
1350 g_error ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
1353 method = get_method (m, mtoken);
1354 val = mono_metadata_blob_heap (m, cols [MONO_CUSTOM_ATTR_VALUE]);
1355 len = mono_metadata_decode_value (val, &val);
1356 attr = g_string_new (".custom ");
1357 dump = data_dump (val, len, "\t\t");
1358 g_string_sprintfa (attr, "%s = %s", method, dump);
1360 list = g_list_append (list, attr->str);
1361 g_string_free (attr, FALSE);
1368 get_marshal_info (MonoImage *m, const char *blob) {
1371 len = mono_metadata_decode_blob_size (blob, &blob);
1374 case MONO_NATIVE_BOOLEAN:
1375 return g_strdup ("bool");
1376 case MONO_NATIVE_I1:
1377 return g_strdup ("int8");
1378 case MONO_NATIVE_U1:
1379 return g_strdup ("unsigned int8");
1380 case MONO_NATIVE_I2:
1381 return g_strdup ("int16");
1382 case MONO_NATIVE_U2:
1383 return g_strdup ("unsigned int16");
1384 case MONO_NATIVE_I4:
1385 return g_strdup ("int32");
1386 case MONO_NATIVE_U4:
1387 return g_strdup ("unsigned int32");
1388 case MONO_NATIVE_I8:
1389 return g_strdup ("int64");
1390 case MONO_NATIVE_U8:
1391 return g_strdup ("unsigned int64");
1392 case MONO_NATIVE_R4:
1393 return g_strdup ("float32");
1394 case MONO_NATIVE_R8:
1395 return g_strdup ("float64");
1396 case MONO_NATIVE_CURRENCY:
1397 return g_strdup ("currency");
1398 case MONO_NATIVE_BSTR:
1399 return g_strdup ("bstr");
1400 case MONO_NATIVE_LPSTR:
1401 return g_strdup ("lpstr");
1402 case MONO_NATIVE_LPWSTR:
1403 return g_strdup ("lpwstr");
1404 case MONO_NATIVE_LPTSTR:
1405 return g_strdup ("lptstr");
1406 case MONO_NATIVE_BYVALTSTR:
1407 size = mono_metadata_decode_value (blob + 1, &blob);
1408 return g_strdup_printf ("fixed sysstring [%d]", size);
1409 case MONO_NATIVE_IUNKNOWN:
1410 return g_strdup ("iunknown");
1411 case MONO_NATIVE_IDISPATCH:
1412 return g_strdup ("idispatch");
1413 case MONO_NATIVE_STRUCT:
1414 return g_strdup ("struct");
1415 case MONO_NATIVE_INTERFACE:
1416 return g_strdup ("interface");
1417 case MONO_NATIVE_SAFEARRAY:
1418 return g_strdup ("safearray");
1419 case MONO_NATIVE_BYVALARRAY:
1420 size = mono_metadata_decode_value (blob + 1, &blob);
1421 return g_strdup_printf ("fixed array [%d]", size);
1422 case MONO_NATIVE_INT:
1423 return g_strdup ("int");
1424 case MONO_NATIVE_UINT:
1425 return g_strdup ("unsigned int");
1426 case MONO_NATIVE_VBBYREFSTR:
1427 return g_strdup ("vbbyrefstr");
1428 case MONO_NATIVE_ANSIBSTR:
1429 return g_strdup ("ansi bstr");
1430 case MONO_NATIVE_TBSTR:
1431 return g_strdup ("tbstr");
1432 case MONO_NATIVE_VARIANTBOOL:
1433 return g_strdup ("variant bool");
1434 case MONO_NATIVE_FUNC:
1435 return g_strdup ("method");
1436 case MONO_NATIVE_ASANY:
1437 return g_strdup ("as any");
1438 case MONO_NATIVE_LPARRAY:
1439 return g_strdup ("[]");
1440 case MONO_NATIVE_LPSTRUCT:
1441 return g_strdup ("lpstruct");
1442 case MONO_NATIVE_CUSTOM:
1443 return g_strdup ("custom");
1444 case MONO_NATIVE_ERROR:
1445 return g_strdup ("error");
1447 return g_strdup ("unknown");