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->type);
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 ? "opt": "reqd", 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)
455 guint32 cols [MONO_METHOD_SIZE];
456 guint32 pcols [MONO_PARAM_SIZE];
457 guint32 param_index = 0;
458 const char *name = "";
461 GString *result = g_string_new ("");
464 g_assert (method || methoddef_row);
467 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD], methoddef_row -1, cols, MONO_METHOD_SIZE);
468 name = mono_metadata_string_heap (m, cols [MONO_METHOD_NAME]);
469 param_index = cols [MONO_METHOD_PARAMLIST];
471 const char *sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
472 mono_metadata_decode_blob_size (sig, &sig);
473 method = mono_metadata_parse_method_signature (m, methoddef_row, sig, &sig);
478 retval = dis_stringify_param (m, method->ret);
480 g_string_append (result, "instance ");
481 g_string_append (result, map (method->call_convention, call_conv_type_map));
482 g_string_sprintfa (result, " %s %s(", retval, name);
484 for (i = 0; i < method->param_count; ++i) {
485 if (param_index && param_index <= m->tables [MONO_TABLE_PARAM].rows) {
486 mono_metadata_decode_row (&m->tables [MONO_TABLE_PARAM], param_index - 1, pcols, MONO_PARAM_SIZE);
487 name = mono_metadata_string_heap (m, pcols [MONO_PARAM_NAME]);
488 method->params [i]->attrs = pcols [MONO_PARAM_FLAGS];
494 g_string_append (result, ", ");
495 retval = dis_stringify_param (m, method->params [i]);
496 g_string_sprintfa (result, "%s %s", retval, name);
499 g_string_append (result, ") ");
502 mono_metadata_free_method_signature (method);
503 retval = result->str;
504 g_string_free (result, FALSE);
510 dis_stringify_object_with_class (MonoImage *m, MonoClass *c)
512 /* FIXME: handle MONO_TYPE_OBJECT ... */
513 const char *otype = c->byval_arg.type == MONO_TYPE_CLASS? "class" : "valuetype";
514 char *assemblyref = NULL, *result;
516 if (c->image->assembly_name) {
518 if (substitute_with_mscorlib_p && !strcmp ("corlib", c->image->assembly_name))
519 assemblyref = g_strdup_printf ("[%s]", "mscorlib");
521 assemblyref = g_strdup_printf ("[%s]", c->image->assembly->aname.name);
523 assemblyref = g_strdup_printf ("[.module %s]", c->image->module_name);
526 result = g_strdup_printf ("%s %s%s%s%s", otype, assemblyref?assemblyref:"", c->name_space,
527 *c->name_space?".":"", c->name);
528 g_free (assemblyref);
533 dis_stringify_object (MonoImage *m, MonoType *type)
535 MonoClass *c = mono_class_from_mono_type (type);
536 return dis_stringify_object_with_class (m, c);
540 dis_stringify_type (MonoImage *m, MonoType *type)
542 const char *pinned = "", *byref = "";
543 char *bare = NULL, *mods = NULL;
547 mods = dis_stringify_modifiers (m, type->num_mods, type->modifiers);
550 case MONO_TYPE_BOOLEAN:
564 case MONO_TYPE_STRING:
565 case MONO_TYPE_OBJECT:
566 case MONO_TYPE_TYPEDBYREF:
567 bare = g_strdup (map (type->type, element_type_map));
570 case MONO_TYPE_VALUETYPE:
571 case MONO_TYPE_CLASS:
572 bare = dis_stringify_object (m, type);
575 case MONO_TYPE_FNPTR:
576 bare = dis_stringify_method_signature (m, type->data.method, 0);
579 case MONO_TYPE_SZARRAY: {
581 child_type = dis_stringify_type (m, type->data.type);
583 bare = g_strdup_printf (type->type == MONO_TYPE_PTR ? "%s*" : "%s[]", child_type);
587 case MONO_TYPE_ARRAY:
588 bare = dis_stringify_array (m, type->data.array);
591 bare = g_strdup ("void");
594 bare = g_strdup_printf ("!!%d", type->data.type_param);
597 bare = g_strdup_printf ("!%d", type->data.type_param);
599 case MONO_TYPE_GENERICINST: {
600 GString *str = g_string_new ("");
602 char *generic_type = dis_stringify_type (m, type->data.generic_inst->generic_type);
604 for (i = 0; i < type->data.generic_inst->type_argc; i++){
605 char *t = dis_stringify_type (m, type->data.generic_inst->type_argv [i]);
607 g_string_append (str, t);
608 if (i+1 != type->data.generic_inst->type_argc)
609 g_string_append (str, ", ");
612 bare = g_strdup_printf ("%s<%s>", generic_type, str->str);
613 g_string_free (str, TRUE);
618 g_error ("Do not know how to stringify type 0x%x", type->type);
627 result = g_strconcat (mods ? mods : "", bare, byref, pinned, NULL);
636 * @m: metadata context
637 * @ptr: location to decode from.
638 * @result: pointer to string where resulting decoded string is stored
640 * This routine returs in @result the stringified type pointed by @ptr.
643 * Returns: the new ptr to continue decoding
646 get_type (MonoImage *m, const char *ptr, char **result)
648 MonoType *type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
649 *result = dis_stringify_type (m, type);
650 mono_metadata_free_type (type);
656 * Returns a stringified representation of a FieldSig (22.2.4)
659 get_field_signature (MonoImage *m, guint32 blob_signature)
661 char *allocated_modifier_string, *allocated_type_string;
662 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
667 len = mono_metadata_decode_value (ptr, &ptr);
670 g_assert (*ptr == 0x06);
671 /* hex_dump (ptr, 0, len); */
674 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
675 ptr = get_type (m, ptr, &allocated_type_string);
677 res = g_strdup_printf (
679 allocated_modifier_string ? allocated_modifier_string : "",
680 allocated_type_string);
682 if (allocated_modifier_string)
683 g_free (allocated_modifier_string);
684 if (allocated_type_string)
685 g_free (allocated_type_string);
691 get_field_literal_type (MonoImage *m, guint32 blob_signature)
693 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
695 char *allocated_modifier_string;
697 len = mono_metadata_decode_value (ptr, &ptr);
700 g_assert (*ptr == 0x06);
703 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
704 if (allocated_modifier_string)
705 g_free (allocated_modifier_string);
707 return (MonoTypeEnum) *ptr;
713 * @m: metadata context
714 * @token: token to decode
716 * decodes the literal indexed by @token.
719 decode_literal (MonoImage *m, guint32 token)
721 return g_strdup ("LITERAL_VALUE");
726 * @m: metadata context
727 * @ptr: location to decode from.
728 * @result: pointer to string where resulting decoded string is stored
730 * This routine returns in @result the stringified RetType (22.2.11)
732 * Returns: the new ptr to continue decoding.
735 get_ret_type (MonoImage *m, const char *ptr, char **ret_type)
737 GString *str = g_string_new ("");
739 char *allocated_type_string;
741 ptr = get_custom_mod (m, ptr, &mod);
743 g_string_append (str, mod);
744 g_string_append_c (str, ' ');
748 if (*ptr == MONO_TYPE_TYPEDBYREF){
749 /* TODO: what does `typedbyref' mean? */
750 g_string_append (str, "/* FIXME: What does this mean? */ typedbyref ");
752 } else if (*ptr == MONO_TYPE_VOID){
753 g_string_append (str, "void");
756 if (*ptr == MONO_TYPE_BYREF){
757 g_string_append (str, "[out] ");
761 ptr = get_type (m, ptr, &allocated_type_string);
762 g_string_append (str, allocated_type_string);
763 g_free (allocated_type_string);
766 *ret_type = str->str;
767 g_string_free (str, FALSE);
774 * @m: metadata context
775 * @ptr: location to decode from.
776 * @result: pointer to string where resulting decoded string is stored
778 * This routine returns in @result the stringified Param (22.2.10)
780 * Returns: the new ptr to continue decoding.
783 get_param (MonoImage *m, const char *ptr, char **retval)
785 GString *str = g_string_new ("");
786 char *allocated_mod_string, *allocated_type_string;
788 ptr = get_custom_mod (m, ptr, &allocated_mod_string);
789 if (allocated_mod_string){
790 g_string_append (str, allocated_mod_string);
791 g_string_append_c (str, ' ');
792 g_free (allocated_mod_string);
795 if (*ptr == MONO_TYPE_TYPEDBYREF){
796 g_string_append (str, "/*FIXME: what does typedbyref mean? */ typedbyref ");
799 if (*ptr == MONO_TYPE_BYREF){
800 g_string_append (str, "[out] ");
803 ptr = get_type (m, ptr, &allocated_type_string);
804 g_string_append (str, allocated_type_string);
805 g_free (allocated_type_string);
809 g_string_free (str, FALSE);
813 static map_t param_map [] = {
814 { PARAM_ATTRIBUTE_IN, "[in] " },
815 { PARAM_ATTRIBUTE_OUT, "[out] " },
816 { PARAM_ATTRIBUTE_OPTIONAL, "optional " },
817 { PARAM_ATTRIBUTE_HAS_DEFAULT, "hasdefault " },
818 { PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL, "fieldmarshal " },
823 param_flags (guint32 f)
825 return g_strdup (flags (f, param_map));
828 static map_t field_access_map [] = {
829 { FIELD_ATTRIBUTE_COMPILER_CONTROLLED, "compilercontrolled " },
830 { FIELD_ATTRIBUTE_PRIVATE, "private " },
831 { FIELD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
832 { FIELD_ATTRIBUTE_ASSEMBLY, "assembly " },
833 { FIELD_ATTRIBUTE_FAMILY, "family " },
834 { FIELD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
835 { FIELD_ATTRIBUTE_PUBLIC, "public " },
839 static map_t field_flags_map [] = {
840 { FIELD_ATTRIBUTE_STATIC, "static " },
841 { FIELD_ATTRIBUTE_INIT_ONLY, "initonly " },
842 { FIELD_ATTRIBUTE_LITERAL, "literal " },
843 { FIELD_ATTRIBUTE_NOT_SERIALIZED, "notserialized " },
844 { FIELD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
845 { FIELD_ATTRIBUTE_PINVOKE_IMPL, "FIXME:pinvokeimpl " },
846 { FIELD_ATTRIBUTE_RT_SPECIAL_NAME, "rtspecialname " },
847 { FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL, "hasfieldmarshal " },
848 { FIELD_ATTRIBUTE_HAS_DEFAULT, "hasdefault " },
849 { FIELD_ATTRIBUTE_HAS_FIELD_RVA, "hasfieldrva " },
856 * Returns a stringified version of a Field's flags
859 field_flags (guint32 f)
862 int access = f & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK;
866 strcat (buffer, map (access, field_access_map));
867 strcat (buffer, flags (f, field_flags_map));
868 return g_strdup (buffer);
872 * Returns a stringifed representation of a MethodRefSig (22.2.2)
875 get_methodref_signature (MonoImage *m, guint32 blob_signature, const char *fancy_name)
877 GString *res = g_string_new ("");
878 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
879 char *allocated_ret_type, *s;
880 gboolean seen_vararg = 0;
881 int param_count, signature_len;
884 signature_len = mono_metadata_decode_value (ptr, &ptr);
888 g_string_append (res, "explicit-this ");
890 g_string_append (res, "instance "); /* has-this */
897 param_count = mono_metadata_decode_value (ptr, &ptr);
898 ptr = get_ret_type (m, ptr, &allocated_ret_type);
900 g_string_append (res, allocated_ret_type);
903 g_string_append_c (res, ' ');
904 g_string_append (res, fancy_name);
907 g_string_append (res, "(");
910 * param_count describes parameters *before* and *after*
911 * the vararg sentinel
913 for (i = 0; i < param_count; i++){
917 * If ptr is a SENTINEL
920 g_string_append (res, " varargs ");
924 ptr = get_param (m, ptr, ¶m);
925 g_string_append (res, param);
926 if (i+1 != param_count)
927 g_string_append (res, ", ");
930 g_string_append (res, ")");
935 g_free (allocated_ret_type);
937 g_string_free (res, FALSE);
943 * @m: metadata context
944 * @token: a FIELD_DEF token
946 * This routine has to locate the TypeDef that "owns" this Field.
947 * Since there is no backpointer in the Field table, we have to scan
948 * the TypeDef table and locate the actual "owner" of the field
951 get_field (MonoImage *m, guint32 token)
953 int idx = mono_metadata_token_index (token);
954 guint32 cols [MONO_FIELD_SIZE];
955 char *sig, *res, *type;
959 * We can get here also with a MenberRef token (for a field
960 * defined in another module/assembly, just like in get_method ()
962 if (mono_metadata_token_code (token) == MONO_TOKEN_MEMBER_REF) {
963 return g_strdup_printf ("fieldref-0x%08x", token);
965 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
967 mono_metadata_decode_row (&m->tables [MONO_TABLE_FIELD], idx - 1, cols, MONO_FIELD_SIZE);
968 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
971 * To locate the actual "container" for this field, we have to scan
972 * the TypeDef table. LAME!
974 type_idx = mono_metadata_typedef_from_field (m, idx);
976 type = get_typedef (m, type_idx);
977 res = g_strdup_printf ("%s %s::%s",
979 mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
987 get_memberref_parent (MonoImage *m, guint32 mrp_token)
990 * mrp_index is a MemberRefParent coded index
992 guint32 table = mrp_token & 7;
993 guint32 idx = mrp_token >> 3;
996 case 0: /* TypeDef */
997 return get_typedef (m, idx);
999 case 1: /* TypeRef */
1000 return get_typeref (m, idx);
1002 case 2: /* ModuleRef */
1003 return g_strdup_printf ("TODO:MemberRefParent-ModuleRef");
1005 case 3: /* MethodDef */
1006 return g_strdup ("TODO:MethodDef");
1008 case 4: /* TypeSpec */
1009 return get_typespec (m, idx);
1011 g_assert_not_reached ();
1017 * @m: metadata context
1018 * @token: a METHOD_DEF or MEMBER_REF token
1020 * This routine has to locate the TypeDef that "owns" this Field.
1021 * Since there is no backpointer in the Field table, we have to scan
1022 * the TypeDef table and locate the actual "owner" of the field
1025 get_method (MonoImage *m, guint32 token)
1027 int idx = mono_metadata_token_index (token);
1028 guint32 member_cols [MONO_MEMBERREF_SIZE], method_cols [MONO_METHOD_SIZE];
1034 mh = mono_get_method (m, token, NULL);
1036 sig = dis_stringify_object_with_class (m, mh->klass);
1037 name = g_strdup_printf ("%s::%s", sig, mh->name);
1042 switch (mono_metadata_token_code (token)){
1043 case MONO_TOKEN_METHOD_DEF:
1044 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD],
1045 idx - 1, method_cols, MONO_METHOD_SIZE);
1047 sig = get_methodref_signature (m, method_cols [MONO_METHOD_SIGNATURE], name);
1050 case MONO_TOKEN_MEMBER_REF: {
1052 mono_metadata_decode_row (&m->tables [MONO_TABLE_MEMBERREF],
1053 idx - 1, member_cols, MONO_MEMBERREF_SIZE);
1055 name = g_strdup_printf ("%s::%s",
1056 get_memberref_parent (m, member_cols [MONO_MEMBERREF_CLASS]),
1057 mono_metadata_string_heap (m, member_cols [MONO_MEMBERREF_NAME]));
1058 sig = get_methodref_signature (
1059 m, member_cols [MONO_MEMBERREF_SIGNATURE], name);
1064 g_assert_not_reached ();
1066 g_assert_not_reached ();
1072 * @m: metadata context
1073 * @blob_index: index into the blob where the constant is stored
1075 * Returns: An allocated value representing a stringified version of the
1079 get_constant (MonoImage *m, MonoTypeEnum t, guint32 blob_index)
1081 const char *ptr = mono_metadata_blob_heap (m, blob_index);
1084 len = mono_metadata_decode_value (ptr, &ptr);
1087 case MONO_TYPE_BOOLEAN:
1088 return g_strdup_printf ("%s", *ptr ? "true" : "false");
1090 case MONO_TYPE_CHAR:
1091 return g_strdup_printf ("%c", *ptr); /* FIXME: unicode char */
1095 return g_strdup_printf ("int8(0x%02x)", (int) (*ptr));
1100 return g_strdup_printf ("int16(0x%08x)", (int) read16 (ptr));
1104 return g_strdup_printf ("int32(%d)", read32 (ptr));
1106 case MONO_TYPE_I8: {
1109 high = read32 (ptr + 4);
1110 return g_strdup_printf ("0x%08x%08x", high, low);
1112 case MONO_TYPE_U8: {
1115 high = read32 (ptr + 4);
1116 return g_strdup_printf ("0x%08x%08x", high, low);
1118 case MONO_TYPE_R4: {
1121 return g_strdup_printf ("%g", (double) r);
1123 case MONO_TYPE_R8: {
1126 return g_strdup_printf ("%g", r);
1128 case MONO_TYPE_STRING: {
1132 for (i = 0; !ptr [i+1]; i += 2){
1137 case '\n': /* add more */
1141 res = g_malloc (len + e + 3);
1145 for (i = 0; i < len; i += 2){
1158 res[j++] = isprint (ptr [i]) ? ptr [i] : '.';
1167 case MONO_TYPE_CLASS:
1168 return g_strdup ("CLASS CONSTANT. MUST BE ZERO");
1171 g_error ("Unknown MONO_TYPE (%d) on constant at Blob index (0x%08x)\n",
1172 (int) *ptr, blob_index);
1173 return g_strdup_printf ("Unknown");
1180 * @m: metadata context
1181 * @token: token that we want to decode.
1183 * Returns: An allocated value representing a stringified version of the
1187 get_token (MonoImage *m, guint32 token)
1189 guint32 idx = mono_metadata_token_index (token);
1191 switch (mono_metadata_token_code (token)){
1192 case MONO_TOKEN_FIELD_DEF:
1193 return (get_field (m, token));
1194 case MONO_TOKEN_TYPE_DEF:
1195 return get_typedef (m, idx);
1196 case MONO_TOKEN_TYPE_REF:
1197 return get_typeref (m, idx);
1198 case MONO_TOKEN_TYPE_SPEC:
1199 return get_typespec (m, idx);
1201 g_error ("Do not know how to decode tokens of type 0x%08x", token);
1204 g_assert_not_reached ();
1205 return g_strdup ("ERROR");
1210 * @m: metadata context
1211 * @token: the token can belong to any of the following tables:
1212 * MONO_TOKEN_TYPE_REF, MONO_TOKEN_TYPE_DEF, MONO_TOKEN_TYPE_SPEC
1214 * Returns: a stringified version of the MethodDef or MethodRef or TypeSpecn
1215 * at (token & 0xffffff)
1218 get_token_type (MonoImage *m, guint32 token)
1220 char *temp = NULL, *s;
1223 idx = mono_metadata_token_index (token);
1225 switch (mono_metadata_token_code (token)){
1226 case MONO_TOKEN_TYPE_DEF:
1227 temp = get_typedef (m, idx);
1228 s = g_strdup_printf ("%s", temp);
1231 case MONO_TOKEN_TYPE_REF:
1232 temp = get_typeref (m, idx);
1233 s = g_strdup_printf ("%s", temp);
1236 case MONO_TOKEN_TYPE_SPEC:
1237 s = get_typespec (m, idx);
1241 g_error ("Unhandled encoding for token 0x%08x", token);
1252 get_guid (MonoImage *m, guint32 guid_index)
1254 const unsigned char *guid;
1257 guid = mono_metadata_guid_heap (m, guid_index);
1259 result = g_strdup_printf ("{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
1260 guid [3], guid [2], guid [1], guid [0], guid [5], guid [4], guid [7], guid [6],
1261 guid [8], guid [9], guid [10], guid [11], guid [12], guid [13], guid [14], guid [15]);
1266 dis_get_custom_attrs (MonoImage *m, guint32 token)
1269 guint32 idx, i, len, mtoken;
1270 guint32 cols [MONO_CUSTOM_ATTR_SIZE];
1276 idx = mono_metadata_token_index (token);
1277 idx <<= CUSTOM_ATTR_BITS;
1279 switch (mono_metadata_token_table (token)) {
1280 case MONO_TABLE_TYPEDEF:
1281 idx |= CUSTOM_ATTR_TYPEDEF;
1283 case MONO_TABLE_ASSEMBLY:
1284 idx |= CUSTOM_ATTR_ASSEMBLY;
1286 case MONO_TABLE_PROPERTY:
1287 idx |= CUSTOM_ATTR_PROPERTY;
1289 case MONO_TABLE_EVENT:
1290 idx |= CUSTOM_ATTR_EVENT;
1292 case MONO_TABLE_FIELD:
1293 idx |= CUSTOM_ATTR_FIELDDEF;
1295 case MONO_TABLE_METHOD:
1296 idx |= CUSTOM_ATTR_METHODDEF;
1298 case MONO_TABLE_PARAM:
1299 idx |= CUSTOM_ATTR_PARAMDEF;
1302 g_print ("Missing custom attr get support for token 0x%08x\n", token);
1306 ca = &m->tables [MONO_TABLE_CUSTOMATTRIBUTE];
1307 /* the table is not sorted */
1308 for (i = 0; i < ca->rows; ++i) {
1310 mono_metadata_decode_row (ca, i, cols, MONO_CUSTOM_ATTR_SIZE);
1311 if (cols [MONO_CUSTOM_ATTR_PARENT] != idx)
1313 mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> CUSTOM_ATTR_TYPE_BITS;
1314 switch (cols [MONO_CUSTOM_ATTR_TYPE] & CUSTOM_ATTR_TYPE_MASK) {
1315 case CUSTOM_ATTR_TYPE_METHODDEF:
1316 mtoken |= MONO_TOKEN_METHOD_DEF;
1318 case CUSTOM_ATTR_TYPE_MEMBERREF:
1319 mtoken |= MONO_TOKEN_MEMBER_REF;
1322 g_error ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
1325 method = get_method (m, mtoken);
1326 val = mono_metadata_blob_heap (m, cols [MONO_CUSTOM_ATTR_VALUE]);
1327 len = mono_metadata_decode_value (val, &val);
1328 attr = g_string_new (".custom ");
1329 dump = data_dump (val, len, "\t\t");
1330 g_string_sprintfa (attr, "%s = %s", method, dump);
1332 list = g_list_append (list, attr->str);
1333 g_string_free (attr, FALSE);
1340 get_marshal_info (MonoImage *m, const char *blob) {
1343 len = mono_metadata_decode_blob_size (blob, &blob);
1346 case MONO_NATIVE_BOOLEAN:
1347 return g_strdup ("bool");
1348 case MONO_NATIVE_I1:
1349 return g_strdup ("int8");
1350 case MONO_NATIVE_U1:
1351 return g_strdup ("unsigned int8");
1352 case MONO_NATIVE_I2:
1353 return g_strdup ("int16");
1354 case MONO_NATIVE_U2:
1355 return g_strdup ("unsigned int16");
1356 case MONO_NATIVE_I4:
1357 return g_strdup ("int32");
1358 case MONO_NATIVE_U4:
1359 return g_strdup ("unsigned int32");
1360 case MONO_NATIVE_I8:
1361 return g_strdup ("int64");
1362 case MONO_NATIVE_U8:
1363 return g_strdup ("unsigned int64");
1364 case MONO_NATIVE_R4:
1365 return g_strdup ("float32");
1366 case MONO_NATIVE_R8:
1367 return g_strdup ("float64");
1368 case MONO_NATIVE_CURRENCY:
1369 return g_strdup ("currency");
1370 case MONO_NATIVE_BSTR:
1371 return g_strdup ("bstr");
1372 case MONO_NATIVE_LPSTR:
1373 return g_strdup ("lpstr");
1374 case MONO_NATIVE_LPWSTR:
1375 return g_strdup ("lpwstr");
1376 case MONO_NATIVE_LPTSTR:
1377 return g_strdup ("lptstr");
1378 case MONO_NATIVE_BYVALTSTR:
1379 size = mono_metadata_decode_value (blob + 1, &blob);
1380 return g_strdup_printf ("fixed sysstring [%d]", size);
1381 case MONO_NATIVE_IUNKNOWN:
1382 return g_strdup ("iunknown");
1383 case MONO_NATIVE_IDISPATCH:
1384 return g_strdup ("idispatch");
1385 case MONO_NATIVE_STRUCT:
1386 return g_strdup ("struct");
1387 case MONO_NATIVE_INTERFACE:
1388 return g_strdup ("interface");
1389 case MONO_NATIVE_SAFEARRAY:
1390 return g_strdup ("safearray");
1391 case MONO_NATIVE_BYVALARRAY:
1392 size = mono_metadata_decode_value (blob + 1, &blob);
1393 return g_strdup_printf ("fixed array [%d]", size);
1394 case MONO_NATIVE_INT:
1395 return g_strdup ("int");
1396 case MONO_NATIVE_UINT:
1397 return g_strdup ("unsigned int");
1398 case MONO_NATIVE_VBBYREFSTR:
1399 return g_strdup ("vbbyrefstr");
1400 case MONO_NATIVE_ANSIBSTR:
1401 return g_strdup ("ansi bstr");
1402 case MONO_NATIVE_TBSTR:
1403 return g_strdup ("tbstr");
1404 case MONO_NATIVE_VARIANTBOOL:
1405 return g_strdup ("variant bool");
1406 case MONO_NATIVE_FUNC:
1407 return g_strdup ("method");
1408 case MONO_NATIVE_ASANY:
1409 return g_strdup ("as any");
1410 case MONO_NATIVE_LPARRAY:
1411 return g_strdup ("[]");
1412 case MONO_NATIVE_LPSTRUCT:
1413 return g_strdup ("lpstruct");
1414 case MONO_NATIVE_CUSTOM:
1415 return g_strdup ("custom");
1416 case MONO_NATIVE_ERROR:
1417 return g_strdup ("error");
1419 return g_strdup ("unknown");