#include "meta.h"
#include "util.h"
#include "get.h"
-
-/**
- * expand:
- * @t: table to extract information from.
- * @idx: index in table.
- * @res: array of @res_size cols to store the results in
- *
- * This decompresses the metadata element @idx in table @t
- * into the guint32 @res array that has res_size elements
- */
-void
-expand (metadata_tableinfo_t *t, int idx, guint32 *res, int res_size)
-
-{
- guint32 bitfield = t->size_bitfield;
- int i, count = meta_table_count (bitfield);
- char *data = t->base + idx * t->row_size;
-
- g_assert (res_size == count);
-
- for (i = 0; i < count; i++){
- int n = meta_table_size (bitfield, i);
-
- switch (n){
- case 1:
- res [i] = *data; break;
- case 2:
- res [i] = read16 (data); break;
-
- case 4:
- res [i] = read32 (data); break;
-
- default:
- g_assert_not_reached ();
- }
- data += n;
- }
-}
-
-/**
- * get_encoded_value:
- * @ptr: pointer to decode from
- * @len: result value is stored here.
- *
- * This routine decompresses 32-bit values as specified in the "Blob and
- * Signature" section (22.2)
- *
- * Returns: updated pointer location
- */
-const char *
-get_encoded_value (const char *_ptr, guint32 *len)
-{
- const unsigned char *ptr = (unsigned char *) _ptr;
- unsigned char b = *ptr;
-
- if ((b & 0x80) == 0){
- *len = b;
- return ptr+1;
- } else if ((b & 0x40) == 0){
- *len = ((b & 0x3f) << 8 | ptr [1]);
- return ptr + 2;
- }
- *len = ((b & 0x1f) << 24) |
- (ptr [1] << 16) |
- (ptr [2] << 8) |
- ptr [3];
-
- return ptr + 4;
-}
+#include <mono/metadata/class.h>
char *
-get_typedef (metadata_t *m, int idx)
+get_typedef (MonoMetadata *m, int idx)
{
- guint32 cols [6];
+ guint32 cols [MONO_TYPEDEF_SIZE];
- expand (&m->tables [META_TABLE_TYPEDEF], idx - 1, cols, CSIZE (cols));
+ mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEDEF], idx - 1, cols, MONO_TYPEDEF_SIZE);
return g_strdup_printf (
"%s.%s",
- mono_metadata_string_heap (m, cols [2]),
- mono_metadata_string_heap (m, cols [1]));
+ mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]),
+ mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME]));
}
char *
-get_module (metadata_t *m, int idx)
+get_module (MonoMetadata *m, int idx)
{
- guint32 cols [5];
+ guint32 cols [MONO_MODULE_SIZE];
/*
* There MUST BE only one module in the Module table
*/
g_assert (idx == 1);
- expand (&m->tables [META_TABLE_MODULEREF], idx - 1, cols, CSIZE (cols));
+ mono_metadata_decode_row (&m->tables [MONO_TABLE_MODULEREF], idx - 1, cols, MONO_MODULE_SIZE);
- return g_strdup (mono_metadata_string_heap (m, cols [6]));
+ return g_strdup (mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]));
}
char *
-get_assemblyref (metadata_t *m, int idx)
+get_assemblyref (MonoMetadata *m, int idx)
{
- guint32 cols [9];
+ guint32 cols [MONO_ASSEMBLYREF_SIZE];
- expand (&m->tables [META_TABLE_ASSEMBLYREF], idx - 1, cols, CSIZE (cols));
+ mono_metadata_decode_row (&m->tables [MONO_TABLE_ASSEMBLYREF], idx - 1, cols, MONO_ASSEMBLYREF_SIZE);
- return g_strdup (mono_metadata_string_heap (m, cols [6]));
+ return g_strdup (mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
}
/*
* Returns a string representing the ArrayShape (22.2.16).
*/
static const char *
-get_array_shape (metadata_t *m, const char *ptr, char **result)
+get_array_shape (MonoMetadata *m, const char *ptr, char **result)
{
GString *res = g_string_new ("[");
guint32 rank, num_sizes, num_lo_bounds;
int i, r;
char buffer [80];
- ptr = get_encoded_value (ptr, &rank);
- ptr = get_encoded_value (ptr, &num_sizes);
+ rank = mono_metadata_decode_value (ptr, &ptr);
+ num_sizes = mono_metadata_decode_value (ptr, &ptr);
if (num_sizes > 0)
sizes = g_new (guint32, num_sizes);
for (i = 0; i < num_sizes; i++)
- ptr = get_encoded_value (ptr, &(sizes [i]));
+ sizes [i] = mono_metadata_decode_value (ptr, &ptr);
- ptr = get_encoded_value (ptr, &num_lo_bounds);
+ num_lo_bounds = mono_metadata_decode_value (ptr, &ptr);
if (num_lo_bounds > 0)
lo_bounds = g_new (guint32, num_lo_bounds);
for (i = 0; i < num_lo_bounds; i++)
- ptr = get_encoded_value (ptr, &(lo_bounds [i]));
+ lo_bounds [i] = mono_metadata_decode_value (ptr, &ptr);
for (r = 0; r < rank; r++){
if (r < num_sizes){
* Returns the stringified representation of a TypeSpec signature (22.2.17)
*/
char *
-get_typespec (metadata_t *m, guint32 idx)
+get_typespec (MonoMetadata *m, guint32 idx)
{
- guint32 cols [1];
+ guint32 cols [MONO_TYPESPEC_SIZE];
const char *ptr;
char *s, *result;
GString *res = g_string_new ("");
int len;
- expand (&m->tables [META_TABLE_TYPESPEC], idx-1, cols, CSIZE (cols));
- ptr = mono_metadata_blob_heap (m, cols [0]);
- ptr = get_encoded_value (ptr, &len);
+ mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPESPEC], idx-1, cols, MONO_TYPESPEC_SIZE);
+ ptr = mono_metadata_blob_heap (m, cols [MONO_TYPESPEC_SIGNATURE]);
+ len = mono_metadata_decode_value (ptr, &ptr);
switch (*ptr++){
- case ELEMENT_TYPE_PTR:
+ case MONO_TYPE_PTR:
ptr = get_custom_mod (m, ptr, &s);
if (s){
g_string_append (res, s);
g_free (s);
}
- if (*ptr == ELEMENT_TYPE_VOID)
+ if (*ptr == MONO_TYPE_VOID)
g_string_append (res, "void");
else {
ptr = get_type (m, ptr, &s);
}
break;
- case ELEMENT_TYPE_FNPTR:
+ case MONO_TYPE_FNPTR:
g_string_append (res, "FNPTR ");
/*
* we assume MethodRefSig, as we do not know
hex_dump (ptr, 0, 40);
break;
- case ELEMENT_TYPE_ARRAY:
+ case MONO_TYPE_ARRAY:
ptr = get_type (m, ptr, &s);
g_string_append (res, s);
g_free (s);
g_free (s);
break;
- case ELEMENT_TYPE_SZARRAY:
+ case MONO_TYPE_SZARRAY:
ptr = get_custom_mod (m, ptr, &s);
if (s){
g_string_append (res, s);
}
char *
-get_typeref (metadata_t *m, int idx)
+get_typeref (MonoMetadata *m, int idx)
{
- guint32 cols [3];
+ guint32 cols [MONO_TYPEREF_SIZE];
const char *s, *t;
char *x, *ret;
guint32 rs_idx, table;
- expand (&m->tables [META_TABLE_TYPEREF], idx - 1, cols, CSIZE (cols));
+ mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEREF], idx - 1, cols, MONO_TYPEREF_SIZE);
- t = mono_metadata_string_heap (m, cols [1]);
- s = mono_metadata_string_heap (m, cols [2]);
+ t = mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAME]);
+ s = mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAMESPACE]);
- rs_idx = cols [0] >> 2;
+ rs_idx = cols [MONO_TYPEREF_SCOPE] >> 2;
/*
* Two bits in Beta2.
* ECMA spec claims 3 bits
*/
- table = cols [0] & 3;
+ table = cols [MONO_TYPEREF_SCOPE] & 3;
switch (table){
case 0: /* Module */
* me like it is 2 (tokens are prefixed with 0x23)
*/
x = get_assemblyref (m, rs_idx);
- ret = g_strdup_printf ("[%s] %s.%s", x, s, t);
+ ret = g_strdup_printf ("[%s]%s.%s", x, s, t);
g_free (x);
break;
* at (dor_token >> 2)
*/
char *
-get_typedef_or_ref (metadata_t *m, guint32 dor_token)
+get_typedef_or_ref (MonoMetadata *m, guint32 dor_token)
{
char *temp = NULL, *s;
int table, idx;
* Returns: the new ptr to continue decoding
*/
const char *
-get_encoded_typedef_or_ref (metadata_t *m, const char *ptr, char **result)
+get_encoded_typedef_or_ref (MonoMetadata *m, const char *ptr, char **result)
{
guint32 token;
- ptr = get_encoded_value (ptr, &token);
+ token = mono_metadata_decode_value (ptr, &ptr);
*result = get_typedef_or_ref (m, token);
* Returns: updated pointer location
*/
const char *
-get_custom_mod (metadata_t *m, const char *ptr, char **return_value)
+get_custom_mod (MonoMetadata *m, const char *ptr, char **return_value)
{
char *s;
- if ((*ptr == ELEMENT_TYPE_CMOD_OPT) ||
- (*ptr == ELEMENT_TYPE_CMOD_REQD)){
+ if ((*ptr == MONO_TYPE_CMOD_OPT) ||
+ (*ptr == MONO_TYPE_CMOD_REQD)){
ptr++;
ptr = get_encoded_typedef_or_ref (m, ptr, &s);
}
-/**
- * methoddefref_signature:
- * @m: metadata context
- * @ptr: location to decode from.
- * @result: pointer to string where resulting decoded string is stored
- *
- * This routine decodes into a string a MethodDef or a MethodRef.
- *
- * result will point to a g_malloc()ed string.
- *
- * Returns: the new ptr to continue decoding
- */
-static const char *
-methoddefref_signature (metadata_t *m, const char *ptr, char **result)
-{
- *result = g_strdup ("method-def-or-ref");
-
- return ptr;
-}
-
static map_t element_type_map [] = {
- { ELEMENT_TYPE_END , "end" },
- { ELEMENT_TYPE_VOID , "void" },
- { ELEMENT_TYPE_BOOLEAN , "bool" },
- { ELEMENT_TYPE_CHAR , "char" },
- { ELEMENT_TYPE_I1 , "sbyte" },
- { ELEMENT_TYPE_U1 , "byte" },
- { ELEMENT_TYPE_I2 , "int16" },
- { ELEMENT_TYPE_U2 , "uint16" },
- { ELEMENT_TYPE_I4 , "int32" },
- { ELEMENT_TYPE_U4 , "uint32" },
- { ELEMENT_TYPE_I8 , "int64" },
- { ELEMENT_TYPE_U8 , "uint64" },
- { ELEMENT_TYPE_R4 , "float32" },
- { ELEMENT_TYPE_R8 , "float64" },
- { ELEMENT_TYPE_STRING , "string" },
- { ELEMENT_TYPE_TYPEDBYREF , "TypedByRef" },
- { ELEMENT_TYPE_I , "native int" },
- { ELEMENT_TYPE_U , "native unsigned int" },
- { ELEMENT_TYPE_OBJECT , "object" },
+ { MONO_TYPE_END , "end" },
+ { MONO_TYPE_VOID , "void" },
+ { MONO_TYPE_BOOLEAN , "bool" },
+ { MONO_TYPE_CHAR , "char" },
+ { MONO_TYPE_I1 , "int8" },
+ { MONO_TYPE_U1 , "unsigned int8" },
+ { MONO_TYPE_I2 , "int16" },
+ { MONO_TYPE_U2 , "uint16" },
+ { MONO_TYPE_I4 , "int32" },
+ { MONO_TYPE_U4 , "uint32" },
+ { MONO_TYPE_I8 , "int64" },
+ { MONO_TYPE_U8 , "uint64" },
+ { MONO_TYPE_R4 , "float32" },
+ { MONO_TYPE_R8 , "float64" },
+ { MONO_TYPE_STRING , "string" },
+ { MONO_TYPE_TYPEDBYREF , "TypedByRef" },
+ { MONO_TYPE_I , "native int" },
+ { MONO_TYPE_U , "native unsigned int" },
+ { MONO_TYPE_OBJECT , "object" },
{ 0, NULL }
};
-/**
- * get_type:
- * @m: metadata context
- * @ptr: location to decode from.
- * @result: pointer to string where resulting decoded string is stored
- *
- * This routine returs in @result the stringified type pointed by @ptr.
- * (22.2.12)
- *
- * Returns: the new ptr to continue decoding
- */
-const char *
-get_type (metadata_t *m, const char *ptr, char **result)
+static map_t call_conv_type_map [] = {
+ { MONO_CALL_DEFAULT , "default" },
+ { MONO_CALL_C , "c" },
+ { MONO_CALL_STDCALL , "stdcall" },
+ { MONO_CALL_THISCALL , "thiscall" },
+ { MONO_CALL_FASTCALL , "fastcall" },
+ { MONO_CALL_VARARG , "vararg" },
+ { 0, NULL }
+};
+
+char*
+dis_stringify_token (MonoMetadata *m, guint32 token)
+{
+ guint idx = token & 0xffffff;
+ switch (token >> 24) {
+ case MONO_TABLE_TYPEDEF: return get_typedef (m, idx);
+ case MONO_TABLE_TYPEREF: return get_typeref (m, idx);
+ case MONO_TABLE_TYPESPEC: return get_typespec (m, idx);
+ default:
+ break;
+ }
+ return g_strdup_printf("0x%08x", token);
+}
+
+char*
+dis_stringify_array (MonoMetadata *m, MonoArray *array)
{
- char c;
+ char *type;
+ GString *s = g_string_new("");
+ int i;
- c = *ptr++;
+ type = dis_stringify_type (m, array->type);
+ g_string_append (s, type);
+ g_free (type);
+ g_string_append_c (s, '[');
+ for (i = 0; i < array->rank; ++i) {
+ if (i)
+ g_string_append_c (s, ',');
+ if (i < array->numsizes) {
+ if (i < array->numlobounds && array->lobounds[i] != 0)
+ g_string_sprintfa (s, "%d..%d", array->lobounds[i], array->sizes[i]);
+ else
+ g_string_sprintfa (s, "%d", array->sizes[i]);
+ }
+ }
+ g_string_append_c (s, ']');
+ type = s->str;
+ g_string_free (s, FALSE);
+ return type;
+}
+
+char*
+dis_stringify_modifiers (MonoMetadata *m, int n, MonoCustomMod *mod)
+{
+ GString *s = g_string_new("");
+ char *result;
+ int i;
+ for (i = 0; i < n; ++i) {
+ char *tok = dis_stringify_token (m, mod[i].token);
+ g_string_sprintfa (s, "%s %s", mod[i].required ? "opt": "reqd", tok);
+ g_free (tok);
+ }
+ g_string_append_c (s, ' ');
+ result = s->str;
+ g_string_free (s, FALSE);
+ return result;
+}
+
+char*
+dis_stringify_param (MonoMetadata *m, MonoType *param)
+{
+ char *t;
+ char *result;
+ char *out = param->attrs & 2 ? "[out] ": "";
+ t = dis_stringify_type (m, param);
+ result = g_strconcat (out, t, NULL);
+ g_free (t);
+ return result;
+}
+
+char*
+dis_stringify_method_signature (MonoMetadata *m, MonoMethodSignature *method, int methoddef_row)
+{
+ guint32 cols [MONO_METHOD_SIZE];
+ guint32 pcols [MONO_PARAM_SIZE];
+ guint32 param_index = 0;
+ const char *name = "";
+ int free_method = 0;
+ char *retval;
+ GString *result = g_string_new ("");
+ int i;
+
+ g_assert (method || methoddef_row);
+
+ if (methoddef_row) {
+ mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD], methoddef_row -1, cols, MONO_METHOD_SIZE);
+ name = mono_metadata_string_heap (m, cols [MONO_METHOD_NAME]);
+ param_index = cols [MONO_METHOD_PARAMLIST];
+ if (!method) {
+ const char *sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
+ mono_metadata_decode_blob_size (sig, &sig);
+ method = mono_metadata_parse_method_signature (m, 1, sig, &sig);
+ free_method = 1;
+ }
+ }
- switch (c){
- case ELEMENT_TYPE_BOOLEAN:
- case ELEMENT_TYPE_CHAR:
- case ELEMENT_TYPE_I1:
- case ELEMENT_TYPE_U1:
- case ELEMENT_TYPE_I2:
- case ELEMENT_TYPE_U2:
- case ELEMENT_TYPE_I4:
- case ELEMENT_TYPE_U4:
- case ELEMENT_TYPE_I8:
- case ELEMENT_TYPE_U8:
- case ELEMENT_TYPE_R4:
- case ELEMENT_TYPE_R8:
- case ELEMENT_TYPE_I:
- case ELEMENT_TYPE_STRING:
- case ELEMENT_TYPE_OBJECT:
- *result = g_strdup (map (c, element_type_map));
+ retval = dis_stringify_param (m, method->ret);
+ if (method->hasthis)
+ g_string_append (result, "instance ");
+ g_string_append (result, map (method->call_convention, call_conv_type_map));
+ g_string_sprintfa (result, " %s %s(", retval, name);
+ g_free (retval);
+ for (i = 0; i < method->param_count; ++i) {
+ if (param_index) {
+ mono_metadata_decode_row (&m->tables [MONO_TABLE_PARAM], param_index - 1, pcols, MONO_PARAM_SIZE);
+ name = mono_metadata_string_heap (m, pcols [MONO_PARAM_NAME]);
+ method->params [i]->attrs = pcols [MONO_PARAM_FLAGS];
+ param_index++;
+ } else {
+ name = "";
+ }
+ if (i)
+ g_string_append (result, ", ");
+ retval = dis_stringify_param (m, method->params [i]);
+ g_string_sprintfa (result, "%s %s", retval, name);
+ g_free (retval);
+ }
+ g_string_append (result, ") ");
+
+ if (free_method)
+ mono_metadata_free_method_signature (method);
+ retval = result->str;
+ g_string_free (result, FALSE);
+
+ return retval;
+}
+
+char *
+dis_stringify_object (MonoMetadata *m, MonoType *type)
+{
+ char *otype = type->type == MONO_TYPE_CLASS? "class" : "valuetype";
+ char *assemblyref = NULL;
+ MonoClass *c = type->data.klass;
+ if (m != c->image) {
+ /* we cheat */
+ if (!strcmp ("corlib", c->image->assembly_name))
+ assemblyref = g_strdup_printf ("[%s]", "mscorlib");
+ else
+ assemblyref = g_strdup_printf ("[%s]", c->image->assembly_name);
+ }
+ otype = g_strdup_printf ("%s %s%s%s%s", otype, assemblyref?assemblyref:"", c->name_space,
+ *c->name_space?".":"", c->name);
+ g_free (assemblyref);
+ return otype;
+}
+
+char*
+dis_stringify_type (MonoMetadata *m, MonoType *type)
+{
+ char *bare = NULL, *pinned = "", *byref = "";
+ char *mods = NULL;
+ char *result;
+
+ if (type->num_mods)
+ mods = dis_stringify_modifiers (m, type->num_mods, type->modifiers);
+
+ switch (type->type){
+ case MONO_TYPE_BOOLEAN:
+ case MONO_TYPE_CHAR:
+ case MONO_TYPE_I1:
+ case MONO_TYPE_U1:
+ case MONO_TYPE_I2:
+ case MONO_TYPE_U2:
+ case MONO_TYPE_I4:
+ case MONO_TYPE_U4:
+ case MONO_TYPE_I8:
+ case MONO_TYPE_U8:
+ case MONO_TYPE_R4:
+ case MONO_TYPE_R8:
+ case MONO_TYPE_I:
+ case MONO_TYPE_U:
+ case MONO_TYPE_STRING:
+ case MONO_TYPE_OBJECT:
+ case MONO_TYPE_TYPEDBYREF:
+ bare = g_strdup (map (type->type, element_type_map));
break;
- case ELEMENT_TYPE_VALUETYPE:
- case ELEMENT_TYPE_CLASS:
- ptr = get_encoded_typedef_or_ref (m, ptr, result);
+ case MONO_TYPE_VALUETYPE:
+ case MONO_TYPE_CLASS:
+ bare = dis_stringify_object (m, type);
break;
- case ELEMENT_TYPE_FNPTR:
- ptr = methoddefref_signature (m, ptr, result);
+ case MONO_TYPE_FNPTR:
+ bare = dis_stringify_method_signature (m, type->data.method, 0);
break;
-
- case ELEMENT_TYPE_SZARRAY: {
+ case MONO_TYPE_PTR:
+ case MONO_TYPE_SZARRAY: {
char *child_type;
+ child_type = dis_stringify_type (m, type->data.type);
- ptr = get_type (m, ptr, &child_type);
- *result = g_strdup_printf ("%s[]", child_type);
+ bare = g_strdup_printf (type->type == MONO_TYPE_PTR ? "%s*" : "%s[]", child_type);
g_free (child_type);
break;
}
-
- case ELEMENT_TYPE_ARRAY:
-
- *result = g_strdup ("ARRAY:TODO");
+ case MONO_TYPE_ARRAY:
+ bare = dis_stringify_array (m, type->data.array);
+ break;
+ case MONO_TYPE_VOID:
+ bare = g_strdup ("void");
+ break;
+ default:
+ g_error ("Do not know how to stringify type 0x%x", type->type);
}
+ if (type->pinned)
+ pinned = " pinned";
+
+ if (type->byref)
+ byref = "&";
+
+ result = g_strconcat (mods ? mods : "", bare, byref, pinned, NULL);
+
+ g_free (bare);
+
+ return result;
+}
+
+/**
+ * get_type:
+ * @m: metadata context
+ * @ptr: location to decode from.
+ * @result: pointer to string where resulting decoded string is stored
+ *
+ * This routine returs in @result the stringified type pointed by @ptr.
+ * (22.2.12)
+ *
+ * Returns: the new ptr to continue decoding
+ */
+const char *
+get_type (MonoMetadata *m, const char *ptr, char **result)
+{
+ MonoType *type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
+ *result = dis_stringify_type (m, type);
+ mono_metadata_free_type (type);
return ptr;
}
* Returns a stringified representation of a FieldSig (22.2.4)
*/
char *
-get_field_signature (metadata_t *m, guint32 blob_signature)
+get_field_signature (MonoMetadata *m, guint32 blob_signature)
{
char *allocated_modifier_string, *allocated_type_string;
const char *ptr = mono_metadata_blob_heap (m, blob_signature);
char *res;
int len;
- ptr = get_encoded_value (ptr, &len);
+ len = mono_metadata_decode_value (ptr, &ptr);
base = ptr;
/* FIELD is 0x06 */
g_assert (*ptr == 0x06);
return res;
}
-ElementTypeEnum
-get_field_literal_type (metadata_t *m, guint32 blob_signature)
+MonoTypeEnum
+get_field_literal_type (MonoMetadata *m, guint32 blob_signature)
{
const char *ptr = mono_metadata_blob_heap (m, blob_signature);
int len;
char *allocated_modifier_string;
- ptr = get_encoded_value (ptr, &len);
+ len = mono_metadata_decode_value (ptr, &ptr);
/* FIELD is 0x06 */
g_assert (*ptr == 0x06);
if (allocated_modifier_string)
g_free (allocated_modifier_string);
- return (ElementTypeEnum) *ptr;
+ return (MonoTypeEnum) *ptr;
}
* decodes the literal indexed by @token.
*/
char *
-decode_literal (metadata_t *m, guint32 token)
+decode_literal (MonoMetadata *m, guint32 token)
{
return g_strdup ("LITERAL_VALUE");
}
* Returns: the new ptr to continue decoding.
*/
const char *
-get_ret_type (metadata_t *m, const char *ptr, char **ret_type)
+get_ret_type (MonoMetadata *m, const char *ptr, char **ret_type)
{
GString *str = g_string_new ("");
char *mod = NULL;
g_free (mod);
}
- if (*ptr == ELEMENT_TYPE_TYPEDBYREF){
+ if (*ptr == MONO_TYPE_TYPEDBYREF){
/* TODO: what does `typedbyref' mean? */
g_string_append (str, "/* FIXME: What does this mean? */ typedbyref ");
ptr++;
- } else if (*ptr == ELEMENT_TYPE_VOID){
+ } else if (*ptr == MONO_TYPE_VOID){
g_string_append (str, "void");
ptr++;
} else {
- if (*ptr == ELEMENT_TYPE_BYREF){
+ if (*ptr == MONO_TYPE_BYREF){
g_string_append (str, "[out] ");
ptr++;
}
* Returns: the new ptr to continue decoding.
*/
const char *
-get_param (metadata_t *m, const char *ptr, char **retval)
+get_param (MonoMetadata *m, const char *ptr, char **retval)
{
GString *str = g_string_new ("");
char *allocated_mod_string, *allocated_type_string;
g_free (allocated_mod_string);
}
- if (*ptr == ELEMENT_TYPE_TYPEDBYREF){
+ if (*ptr == MONO_TYPE_TYPEDBYREF){
g_string_append (str, "/*FIXME: what does typedbyref mean? */ typedbyref ");
ptr++;
} else {
- if (*ptr == ELEMENT_TYPE_BYREF){
+ if (*ptr == MONO_TYPE_BYREF){
g_string_append (str, "[out] ");
ptr++;
}
char *
field_flags (guint32 f)
{
- static char buffer [1024];
+ char buffer [1024];
int access = f & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK;
buffer [0] = 0;
return g_strdup (buffer);
}
-/**
- * get_blob_encoded_size:
- * @ptr: pointer to a blob object
- * @size: where we return the size of the object
- *
- * This decodes a compressed size as described by 23.1.4
- *
- * Returns: the position to start decoding a blob or user string object
- * from.
- */
-const char *
-get_blob_encoded_size (const char *xptr, int *size)
-{
- const unsigned char *ptr = xptr;
-
- if ((*ptr & 0x80) == 0){
- *size = ptr [0] & 0x7f;
- ptr++;
- } else if ((*ptr & 0x40) == 0){
- *size = ((ptr [0] & 0x3f) << 8) + ptr [1];
- ptr += 2;
- } else {
- *size = ((ptr [0] & 0x1f) << 24) +
- (ptr [1] << 16) +
- (ptr [2] << 8) +
- ptr [3];
- ptr += 4;
- }
-
- return (char *) ptr;
-}
-
/**
* Returns a stringifed representation of a MethodRefSig (22.2.2)
*/
char *
-get_methodref_signature (metadata_t *m, guint32 blob_signature, const char *fancy_name)
+get_methodref_signature (MonoMetadata *m, guint32 blob_signature, const char *fancy_name)
{
GString *res = g_string_new ("");
const char *ptr = mono_metadata_blob_heap (m, blob_signature);
int param_count, signature_len;
int i;
- ptr = get_encoded_value (ptr, &signature_len);
+ signature_len = mono_metadata_decode_value (ptr, &ptr);
if (*ptr & 0x20){
if (*ptr & 0x40)
seen_vararg = 1;
ptr++;
- ptr = get_encoded_value (ptr, ¶m_count);
+ param_count = mono_metadata_decode_value (ptr, &ptr);
ptr = get_ret_type (m, ptr, &allocated_ret_type);
g_string_append (res, allocated_ret_type);
g_string_append (res, fancy_name);
}
- g_string_append (res, " (");
+ g_string_append (res, "(");
/*
* param_count describes parameters *before* and *after*
return s;
}
-/*
- * We use this to pass context information to the typedef locator
- */
-typedef struct {
- int idx; /* The field index that we are trying to locate */
- metadata_t *m; /* the metadata context */
- metadata_tableinfo_t *t; /* pointer to the typedef table */
- guint32 result;
-} locator_t;
-
-static int
-typedef_locator (const void *a, const void *b)
-{
- locator_t *loc = (locator_t *) a;
- char *bb = (char *) b;
- int typedef_index = (bb - loc->t->base) / loc->t->row_size;
- guint32 cols [6], cols_next [6];
-
- expand (loc->t, typedef_index, cols, CSIZE (cols));
- expand (loc->t, typedef_index + 1, cols_next, CSIZE (cols_next));
-
- if (loc->idx < cols [4])
- return -1;
-
- if (loc->idx >= cols_next [4])
- return 1;
-
- if (cols [5] == cols_next [4])
- return 1;
-
- loc->result = typedef_index;
-
- return 0;
-}
-
/**
* get_field:
* @m: metadata context
* the TypeDef table and locate the actual "owner" of the field
*/
char *
-get_field (metadata_t *m, guint32 token)
+get_field (MonoMetadata *m, guint32 token)
{
int idx = mono_metadata_token_index (token);
- metadata_tableinfo_t *tdef = &m->tables [META_TABLE_TYPEDEF];
- guint32 cols [3];
+ guint32 cols [MONO_FIELD_SIZE];
char *sig, *res, *type;
- locator_t loc;
-
- g_assert (mono_metadata_token_code (token) == TOKEN_TYPE_FIELD_DEF);
+ guint32 type_idx;
- expand (&m->tables [META_TABLE_FIELD], idx - 1, cols, CSIZE (cols));
- sig = get_field_signature (m, cols [2]);
+ /*
+ * We can get here also with a MenberRef token (for a field
+ * defined in another module/assembly, just like in get_method ()
+ */
+ if (mono_metadata_token_code (token) == MONO_TOKEN_MEMBER_REF) {
+ return g_strdup_printf ("fieldref-0x%08x", token);
+ }
+ g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
+
+ mono_metadata_decode_row (&m->tables [MONO_TABLE_FIELD], idx - 1, cols, MONO_FIELD_SIZE);
+ sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
/*
* To locate the actual "container" for this field, we have to scan
* the TypeDef table. LAME!
*/
- loc.idx = idx;
- loc.m = m;
- loc.t = tdef;
+ type_idx = mono_metadata_typedef_from_field (m, idx);
- if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
- g_assert_not_reached ();
-
- /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
- type = get_typedef (m, loc.result + 1);
- res = g_strdup_printf ("%s %s %s",
+ type = get_typedef (m, type_idx);
+ res = g_strdup_printf ("%s %s::%s",
sig, type,
- mono_metadata_string_heap (m, cols [1]));
+ mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
g_free (type);
g_free (sig);
}
static char *
-get_memberref_parent (metadata_t *m, guint32 mrp_token)
+get_memberref_parent (MonoMetadata *m, guint32 mrp_token)
{
/*
* mrp_index is a MemberRefParent coded index
* the TypeDef table and locate the actual "owner" of the field
*/
char *
-get_method (metadata_t *m, guint32 token)
+get_method (MonoMetadata *m, guint32 token)
{
int idx = mono_metadata_token_index (token);
- guint32 member_cols [3];
- char *res, *class, *fancy_name;
-
+ guint32 member_cols [MONO_MEMBERREF_SIZE], method_cols [MONO_METHOD_SIZE];
+ char *sig;
+ const char *name;
+
+ MonoMethod *mh;
+
+ mh = mono_get_method (m, token, NULL);
+ g_assert (mh);
+
+ name = g_strdup_printf ("%s.%s::%s", mh->klass->name_space,
+ mh->klass->name, mh->name);
+
switch (mono_metadata_token_code (token)){
- case TOKEN_TYPE_METHOD_DEF:
- return g_strdup_printf ("TODO:MethodDef call [%x]", token);
-
- case TOKEN_TYPE_MEMBER_REF: {
- char *sig;
+ case MONO_TOKEN_METHOD_DEF:
+ mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD],
+ idx - 1, method_cols, MONO_METHOD_SIZE);
+
+ sig = get_methodref_signature (m, method_cols [MONO_METHOD_SIGNATURE], name);
+ return sig;
- expand (&m->tables [META_TABLE_MEMBERREF], idx - 1, member_cols, CSIZE (member_cols));
- class = get_memberref_parent (m, member_cols [0]);
- fancy_name = g_strconcat (class, "::",
- mono_metadata_string_heap (m, member_cols [1]),
- NULL);
+ case MONO_TOKEN_MEMBER_REF: {
+ mono_metadata_decode_row (&m->tables [MONO_TABLE_MEMBERREF],
+ idx - 1, member_cols, MONO_MEMBERREF_SIZE);
sig = get_methodref_signature (
- m, member_cols [2], fancy_name);
- res = g_strdup_printf ("%s", sig);
- g_free (sig);
-
- return res;
+ m, member_cols [MONO_MEMBERREF_SIGNATURE], name);
+ return sig;
}
-
+
default:
g_assert_not_reached ();
}
* constant.
*/
char *
-get_constant (metadata_t *m, ElementTypeEnum t, guint32 blob_index)
+get_constant (MonoMetadata *m, MonoTypeEnum t, guint32 blob_index)
{
const char *ptr = mono_metadata_blob_heap (m, blob_index);
int len;
- ptr = get_encoded_value (ptr, &len);
+ len = mono_metadata_decode_value (ptr, &ptr);
switch (t){
- case ELEMENT_TYPE_BOOLEAN:
+ case MONO_TYPE_BOOLEAN:
return g_strdup_printf ("%s", *ptr ? "true" : "false");
- case ELEMENT_TYPE_CHAR:
+ case MONO_TYPE_CHAR:
return g_strdup_printf ("%c", *ptr);
- case ELEMENT_TYPE_U1:
+ case MONO_TYPE_U1:
return g_strdup_printf ("0x%02x", (int) (*ptr));
break;
- case ELEMENT_TYPE_I2:
- return g_strdup_printf ("%d", (int) (*(gint16 *) ptr));
+ case MONO_TYPE_I2:
+ return g_strdup_printf ("%d", (int) read16 (ptr));
- case ELEMENT_TYPE_I4:
- return g_strdup_printf ("%d", *(gint32 *) ptr);
+ case MONO_TYPE_I4:
+ return g_strdup_printf ("%d", read32 (ptr));
- case ELEMENT_TYPE_I8:
+ case MONO_TYPE_I8:
/*
* FIXME: This is not endian portable, does only
* matter for debugging, but still.
*/
return g_strdup_printf ("0x%08x%08x", *(guint32 *) ptr, *(guint32 *) (ptr + 4));
- case ELEMENT_TYPE_U8:
+ case MONO_TYPE_U8:
return g_strdup_printf ("0x%08x%08x", *(guint32 *) ptr, *(guint32 *) (ptr + 4));
- case ELEMENT_TYPE_R4:
+ case MONO_TYPE_R4:
return g_strdup_printf ("%g", (double) (* (float *) ptr));
- case ELEMENT_TYPE_R8:
+ case MONO_TYPE_R8:
return g_strdup_printf ("%g", * (double *) ptr);
- case ELEMENT_TYPE_STRING: {
+ case MONO_TYPE_STRING: {
int len, i, j, e;
char *res;
e = len = 0;
return res;
}
- case ELEMENT_TYPE_CLASS:
+ case MONO_TYPE_CLASS:
return g_strdup ("CLASS CONSTANT. MUST BE ZERO");
/*
* These are non CLS compliant:
*/
- case ELEMENT_TYPE_I1:
+ case MONO_TYPE_I1:
return g_strdup_printf ("%d", (int) *ptr);
- case ELEMENT_TYPE_U2:
- return g_strdup_printf ("0x%04x", (unsigned int) (*(guint16 *) ptr));
+ case MONO_TYPE_U2:
+ return g_strdup_printf ("0x%04x", (unsigned int) read16 (ptr));
- case ELEMENT_TYPE_U4:
- return g_strdup_printf ("0x%04x", (unsigned int) (*(guint32 *) ptr));
+ case MONO_TYPE_U4:
+ return g_strdup_printf ("0x%04x", (unsigned int) read32 (ptr));
default:
- g_error ("Unknown ELEMENT_TYPE (%d) on constant at Blob index (0x%08x)\n",
+ g_error ("Unknown MONO_TYPE (%d) on constant at Blob index (0x%08x)\n",
(int) *ptr, blob_index);
return g_strdup_printf ("Unknown");
}
* constant.
*/
char *
-get_token (metadata_t *m, guint32 token)
+get_token (MonoMetadata *m, guint32 token)
{
+ guint32 idx = mono_metadata_token_index (token);
+
switch (mono_metadata_token_code (token)){
- case TOKEN_TYPE_FIELD_DEF:
+ case MONO_TOKEN_FIELD_DEF:
return (get_field (m, token));
-
- default:
+ case MONO_TOKEN_TYPE_DEF:
+ return get_typedef (m, idx);
+ case MONO_TOKEN_TYPE_REF:
+ return get_typeref (m, idx);
+ case MONO_TOKEN_TYPE_SPEC:
+ return get_typespec (m, idx);
+ default:
g_error ("Do not know how to decode tokens of type 0x%08x", token);
}
* get_token_type:
* @m: metadata context
* @token: the token can belong to any of the following tables:
- * TOKEN_TYPE_TYPE_REF, TOKEN_TYPE_TYPE_DEF, TOKEN_TYPE_TYPE_SPEC
+ * MONO_TOKEN_TYPE_REF, MONO_TOKEN_TYPE_DEF, MONO_TOKEN_TYPE_SPEC
*
* Returns: a stringified version of the MethodDef or MethodRef or TypeSpecn
* at (token & 0xffffff)
*/
char *
-get_token_type (metadata_t *m, guint32 token)
+get_token_type (MonoMetadata *m, guint32 token)
{
char *temp = NULL, *s;
int idx;
idx = mono_metadata_token_index (token);
switch (mono_metadata_token_code (token)){
- case TOKEN_TYPE_TYPE_DEF:
+ case MONO_TOKEN_TYPE_DEF:
temp = get_typedef (m, idx);
s = g_strdup_printf ("%s", temp);
break;
- case TOKEN_TYPE_TYPE_REF:
+ case MONO_TOKEN_TYPE_REF:
temp = get_typeref (m, idx);
s = g_strdup_printf ("%s", temp);
break;
- case TOKEN_TYPE_TYPE_SPEC:
+ case MONO_TOKEN_TYPE_SPEC:
s = get_typespec (m, idx);
break;