1 /*
2  * dump.c: Dumping routines for the disassembler.
3  *
4  * Author:
5  *   Miguel de Icaza (miguel@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9 #include <config.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <glib.h>
13 #include <math.h>
14 #include "meta.h"
15 #include "util.h"
16 #include "dump.h"
17 #include "get.h"
18 #include "declsec.h"
19 #include "mono/metadata/loader.h"
20 #include "mono/metadata/class.h"
21 #include "mono/metadata/class-internals.h"
22 #include "mono/utils/mono-compiler.h"
23 
24 void
dump_table_assembly(MonoImage * m)25 dump_table_assembly (MonoImage *m)
26 {
27 	MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLY];
28 	guint32 cols [MONO_ASSEMBLY_SIZE];
29 	const char *ptr;
30 	int len;
31 
32 	fprintf (output, "Assembly Table\n");
33 
34 	if (!t->rows)
35 		return;
36 
37 	mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
38 
39 	fprintf (output, "Name:          %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_NAME]));
40 	fprintf (output, "Hash Algoritm: 0x%08x\n", cols [MONO_ASSEMBLY_HASH_ALG]);
41 	fprintf (output, "Version:       %d.%d.%d.%d\n", cols [MONO_ASSEMBLY_MAJOR_VERSION],
42 					cols [MONO_ASSEMBLY_MINOR_VERSION],
43 					cols [MONO_ASSEMBLY_BUILD_NUMBER],
44 					cols [MONO_ASSEMBLY_REV_NUMBER]);
45 	fprintf (output, "Flags:         0x%08x\n", cols [MONO_ASSEMBLY_FLAGS]);
46 	fprintf (output, "PublicKey:     BlobPtr (0x%08x)\n", cols [MONO_ASSEMBLY_PUBLIC_KEY]);
47 
48 	ptr = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
49 	len = mono_metadata_decode_value (ptr, &ptr);
50 	if (len > 0){
51 		fprintf (output, "\tDump:");
52 		hex_dump (ptr, 0, len);
53 		fprintf (output, "\n");
54 	} else
55 		fprintf (output, "\tZero sized public key\n");
56 
57 	fprintf (output, "Culture:       %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_CULTURE]));
58 	fprintf (output, "\n");
59 }
60 
61 void
dump_table_typeref(MonoImage * m)62 dump_table_typeref (MonoImage *m)
63 {
64 	MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEREF];
65 	int i;
66 
67 	fprintf (output, "Typeref Table\n");
68 
69 	for (i = 1; i <= t->rows; i++){
70 		char *s = get_typeref (m, i);
71 
72 		fprintf (output, "%d: %s\n", i, s);
73 		g_free (s);
74 	}
75 	fprintf (output, "\n");
76 }
77 
78 void
dump_table_typedef(MonoImage * m)79 dump_table_typedef (MonoImage *m)
80 {
81 	MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
82 	int i;
83 
84 	fprintf (output, "Typedef Table\n");
85 
86 	for (i = 1; i <= t->rows; i++){
87 		char *s = get_typedef (m, i);
88 		guint32 cols [MONO_TYPEDEF_SIZE];
89 
90 		mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEDEF], i - 1, cols, MONO_TYPEDEF_SIZE);
91 
92 		fprintf (output, "%d: %s (flist=%d, mlist=%d, flags=0x%x, extends=0x%x)\n", i, s,
93 					cols [MONO_TYPEDEF_FIELD_LIST], cols [MONO_TYPEDEF_METHOD_LIST],
94 					cols [MONO_TYPEDEF_FLAGS], cols [MONO_TYPEDEF_EXTENDS]);
95 		g_free (s);
96 	}
97 	fprintf (output, "\n");
98 }
99 
100 void
dump_table_typespec(MonoImage * m)101 dump_table_typespec (MonoImage *m)
102 {
103 	MonoTableInfo *t = &m->tables [MONO_TABLE_TYPESPEC];
104 	int i;
105 
106 	fprintf (output, "Typespec Table\n");
107 
108 	for (i = 1; i <= t->rows; i++){
109 		char *typespec = get_typespec (m, i, TRUE, NULL);
110 
111 		fprintf (output, "%d: %s\n", i, typespec);
112 		g_free (typespec);
113 	}
114 	fprintf (output, "\n");
115 }
116 
117 void
dump_table_assemblyref(MonoImage * m)118 dump_table_assemblyref (MonoImage *m)
119 {
120 	MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLYREF];
121 	int i;
122 
123 	fprintf (output, "AssemblyRef Table\n");
124 
125 	for (i = 0; i < t->rows; i++){
126 		const char *ptr;
127 		int len;
128 		guint32 cols [MONO_ASSEMBLYREF_SIZE];
129 
130 		mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
131 		fprintf (output, "%d: Version=%d.%d.%d.%d\n\tName=%s\n", i + 1,
132 			 cols [MONO_ASSEMBLYREF_MAJOR_VERSION],
133 			 cols [MONO_ASSEMBLYREF_MINOR_VERSION],
134 			 cols [MONO_ASSEMBLYREF_BUILD_NUMBER],
135 			 cols [MONO_ASSEMBLYREF_REV_NUMBER],
136 			 mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
137 		fprintf (output, "\tFlags=0x%08x\n", cols [MONO_ASSEMBLYREF_FLAGS]);
138 		ptr = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLYREF_PUBLIC_KEY]);
139 		len = mono_metadata_decode_value (ptr, &ptr);
140 		if (len > 0){
141 			fprintf (output, "\tPublic Key:");
142 			hex_dump (ptr, 0, len);
143 			fprintf (output, "\n");
144 		} else
145 			fprintf (output, "\tZero sized public key\n");
146 
147 	}
148 	fprintf (output, "\n");
149 }
150 
151 void
dump_table_param(MonoImage * m)152 dump_table_param (MonoImage *m)
153 {
154 	MonoTableInfo *t = &m->tables [MONO_TABLE_PARAM];
155 	int i;
156 
157 	fprintf (output, "Param Table\n");
158 
159 	for (i = 0; i < t->rows; i++){
160 		guint32 cols [MONO_PARAM_SIZE];
161 
162 		mono_metadata_decode_row (t, i, cols, MONO_PARAM_SIZE);
163 		fprintf (output, "%d: 0x%04x %d %s\n",
164 			 i + 1,
165 			 cols [MONO_PARAM_FLAGS], cols [MONO_PARAM_SEQUENCE],
166 			 mono_metadata_string_heap (m, cols [MONO_PARAM_NAME]));
167 	}
168 	fprintf (output, "\n");
169 }
170 
171 void
dump_table_field(MonoImage * m)172 dump_table_field (MonoImage *m)
173 {
174 	MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
175 	MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
176 	MonoTableInfo *fl = &m->tables [MONO_TABLE_FIELDLAYOUT];
177 	MonoTableInfo *rva = &m->tables [MONO_TABLE_FIELDRVA];
178 	int i, current_type, offset_row, rva_row;
179 	guint32 first_m, last_m;
180 
181 	fprintf (output, "Field Table (1..%d)\n", t->rows);
182 
183 	rva_row = offset_row = current_type = 1;
184 	last_m = first_m = 1;
185 	for (i = 1; i <= t->rows; i++){
186 		guint32 cols [MONO_FIELD_SIZE];
187 		char *sig, *flags;
188 
189 		/*
190 		 * Find the next type.
191 		 */
192 		while (current_type <= td->rows && i >= (last_m = mono_metadata_decode_row_col (td, current_type - 1, MONO_TYPEDEF_FIELD_LIST))) {
193 			current_type++;
194 		}
195 		if (i == first_m) {
196 			fprintf (output, "########## %s.%s\n",
197 				mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAMESPACE)),
198 				mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAME)));
199 			first_m = last_m;
200 		}
201 		mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_SIZE);
202 		sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE], NULL);
203 		flags = field_flags (cols [MONO_FIELD_FLAGS]);
204 		fprintf (output, "%d: %s %s: %s\n",
205 			 i,
206 			 sig,
207 			 mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]),
208 			 flags);
209 		g_free (sig);
210 		g_free (flags);
211 		if (offset_row <= fl->rows && (mono_metadata_decode_row_col (fl, offset_row - 1, MONO_FIELD_LAYOUT_FIELD) == i)) {
212 			fprintf (output, "\texplicit offset: %d\n", mono_metadata_decode_row_col (fl, offset_row - 1, MONO_FIELD_LAYOUT_OFFSET));
213 			offset_row ++;
214 		}
215 		if (rva_row <= rva->rows && (mono_metadata_decode_row_col (rva, rva_row - 1, MONO_FIELD_RVA_FIELD) == i)) {
216 			fprintf (output, "\trva: %d\n", mono_metadata_decode_row_col (rva, rva_row - 1, MONO_FIELD_RVA_RVA));
217 			rva_row ++;
218 		}
219 	}
220 	fprintf (output, "\n");
221 }
222 
223 void
dump_table_memberref(MonoImage * m)224 dump_table_memberref (MonoImage *m)
225 {
226 	MonoTableInfo *t = &m->tables [MONO_TABLE_MEMBERREF];
227 	int i, kind, idx;
228 	char *x, *xx;
229 	char *sig;
230 	const char *blob, *ks = NULL;
231 
232 	fprintf (output, "MemberRef Table (1..%d)\n", t->rows);
233 
234 	for (i = 0; i < t->rows; i++){
235 		guint32 cols [MONO_MEMBERREF_SIZE];
236 
237 		mono_metadata_decode_row (t, i, cols, MONO_MEMBERREF_SIZE);
238 
239 		kind = cols [MONO_MEMBERREF_CLASS] & 7;
240 		idx = cols [MONO_MEMBERREF_CLASS] >> 3;
241 
242 		x = g_strdup ("UNHANDLED CASE");
243 
244 		switch (kind){
245 		case 0:
246 			ks = "TypeDef";
247 			xx = get_typedef (m, idx);
248 			x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
249 			g_free (xx);
250 			break;
251 		case 1:
252 			ks = "TypeRef";
253 			xx = get_typeref (m, idx);
254 			x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
255 			g_free (xx);
256 			break;
257 		case 2:
258 			ks = "ModuleRef"; break;
259 		case 3:
260 			ks = "MethodDef";
261 			x = get_methoddef (m, idx);
262 			break;
263 		case 4:
264 			ks = "TypeSpec";
265 			xx = get_typespec (m, idx, FALSE, NULL);
266 			x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
267 			g_free (xx);
268 			break;
269 		default:
270 			g_error ("Unknown tag: %d\n", kind);
271 		}
272 		blob = mono_metadata_blob_heap (m, cols [MONO_MEMBERREF_SIGNATURE]);
273 		mono_metadata_decode_blob_size (blob, &blob);
274 		if (*blob == 0x6) { /* it's a field */
275 			sig = get_field_signature (m, cols [MONO_MEMBERREF_SIGNATURE], NULL);
276 		} else {
277 			sig = get_methodref_signature (m, cols [MONO_MEMBERREF_SIGNATURE], NULL);
278 		}
279 		fprintf (output, "%d: %s[%d] %s\n\tResolved: %s\n\tSignature: %s\n\t\n",
280 			 i + 1,
281 			 ks, idx,
282 			 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]),
283 			 x ? x : "",
284 			 sig);
285 
286 		if (x)
287 			g_free (x);
288 		g_free (sig);
289 	}
290 }
291 
292 void
dump_table_class_layout(MonoImage * m)293 dump_table_class_layout (MonoImage *m)
294 {
295 	MonoTableInfo *t = &m->tables [MONO_TABLE_CLASSLAYOUT];
296 	int i;
297 	fprintf (output, "ClassLayout Table (1..%d)\n", t->rows);
298 
299 	for (i = 0; i < t->rows; i++){
300 		guint32 cols [MONO_CLASS_LAYOUT_SIZE];
301 
302 		mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
303 
304 		fprintf (output, "%d: PackingSize=%d  ClassSize=%d  Parent=%s\n",
305 			 i + 1, cols [MONO_CLASS_LAYOUT_PACKING_SIZE],
306 			 cols [MONO_CLASS_LAYOUT_CLASS_SIZE],
307 			 get_typedef (m, cols [MONO_CLASS_LAYOUT_PARENT]));
308 	}
309 }
310 
311 void
dump_table_constant(MonoImage * m)312 dump_table_constant (MonoImage *m)
313 {
314 	MonoTableInfo *t = &m->tables [MONO_TABLE_CONSTANT];
315 	int i;
316 	const char *desc [] = {
317 		"Field",
318 		"Param",
319 		"Property",
320 		""
321 	};
322 	fprintf (output, "Constant Table (1..%d)\n", t->rows);
323 
324 	for (i = 0; i < t->rows; i++){
325 		guint32 cols [MONO_CONSTANT_SIZE];
326 		const char *parent;
327 		mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
328 		parent = desc [cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK];
329 
330 		fprintf (output, "%d: Parent= %s: %d %s\n",
331 			 i + 1, parent, cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS,
332 			 get_constant (m, (MonoTypeEnum) cols [MONO_CONSTANT_TYPE], cols [MONO_CONSTANT_VALUE]));
333 	}
334 
335 }
336 
337 void
dump_table_property_map(MonoImage * m)338 dump_table_property_map (MonoImage *m)
339 {
340 	MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTYMAP];
341 	int i;
342 	char *s;
343 
344 	fprintf (output, "Property Map Table (1..%d)\n", t->rows);
345 
346 	for (i = 0; i < t->rows; i++){
347 		guint32 cols [MONO_PROPERTY_MAP_SIZE];
348 
349 		mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_MAP_SIZE);
350 		s = get_typedef (m, cols [MONO_PROPERTY_MAP_PARENT]);
351 		fprintf (output, "%d: %s (%d) %d\n", i + 1, s, cols [MONO_PROPERTY_MAP_PARENT], cols [MONO_PROPERTY_MAP_PROPERTY_LIST]);
352 		g_free (s);
353 	}
354 }
355 
356 void
dump_table_property(MonoImage * m)357 dump_table_property (MonoImage *m)
358 {
359 	MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTY];
360 	int i, j, pcount;
361 	const char *ptr;
362 	char flags[128];
363 
364 	fprintf (output, "Property Table (1..%d)\n", t->rows);
365 
366 	for (i = 0; i < t->rows; i++){
367 		guint32 cols [MONO_PROPERTY_SIZE];
368 		char *type;
369 		int prop_flags;
370 
371 		mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_SIZE);
372 		flags [0] = 0;
373 		prop_flags = cols [MONO_PROPERTY_FLAGS];
374 		if (prop_flags & 0x0200)
375 			strcat (flags, "special ");
376 		if (prop_flags & 0x0400)
377 			strcat (flags, "runtime ");
378 		if (prop_flags & 0x1000)
379 			strcat (flags, "hasdefault ");
380 
381 		ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
382 		/* bsize = */ mono_metadata_decode_blob_size (ptr, &ptr);
383 		/* ECMA claims 0x08 ... */
384 		if (*ptr != 0x28 && *ptr != 0x08)
385 			g_warning("incorrect signature in propert blob: 0x%x", *ptr);
386 		ptr++;
387 		pcount = mono_metadata_decode_value (ptr, &ptr);
388 		ptr = get_type (m, ptr, &type, FALSE, NULL);
389 		fprintf (output, "%d: %s %s (",
390 			 i + 1, type, mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]));
391 		g_free (type);
392 
393 		for (j = 0; j < pcount; j++){
394 			ptr = get_param (m, ptr, &type, NULL);
395 			fprintf (output, "%s%s", j > 0? ", " : "",type);
396 			g_free (type);
397 		}
398 		fprintf (output, ") %s\n", flags);
399 	}
400 }
401 
402 void
dump_table_event(MonoImage * m)403 dump_table_event (MonoImage *m)
404 {
405 	MonoTableInfo *t = &m->tables [MONO_TABLE_EVENT];
406 	int i;
407 	fprintf (output, "Event Table (1..%d)\n", t->rows);
408 
409 	for (i = 0; i < t->rows; i++){
410 		guint32 cols [MONO_EVENT_SIZE];
411 		const char *name;
412 		char *type;
413 
414 		mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
415 
416 		name = mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]);
417 		type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE], NULL);
418 		fprintf (output, "%d: %s %s %s\n", i + 1, type, name,
419 			 cols [MONO_EVENT_FLAGS] & 0x200 ? "specialname " : "");
420 		g_free (type);
421 	}
422 
423 }
424 
425 void
dump_table_file(MonoImage * m)426 dump_table_file (MonoImage *m)
427 {
428 	MonoTableInfo *t = &m->tables [MONO_TABLE_FILE];
429 	int i, j, len;
430 	fprintf (output, "File Table (1..%d)\n", t->rows);
431 
432 	for (i = 0; i < t->rows; i++){
433 		guint32 cols [MONO_FILE_SIZE];
434 		const char *name, *hash;
435 
436 		mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
437 
438 		name = mono_metadata_string_heap (m, cols [MONO_FILE_NAME]);
439 		fprintf (output, "%d: %s %s [", i + 1, name,
440 				cols [MONO_FILE_FLAGS] & 0x1 ? "nometadata" : "containsmetadata");
441 		hash = mono_metadata_blob_heap (m, cols [MONO_FILE_HASH_VALUE]);
442 		len = mono_metadata_decode_blob_size (hash, &hash);
443 		for (j = 0; j < len; ++j)
444 			fprintf (output, "%s%02X", j? " ": "", hash [j] & 0xff);
445 		fprintf (output, "]\n");
446 	}
447 
448 }
449 
450 static char*
get_manifest_implementation(MonoImage * m,guint32 idx)451 get_manifest_implementation (MonoImage *m, guint32 idx)
452 {
453 	guint32 row;
454 	const char* table = "";
455 	if (!idx)
456 		return g_strdup ("current module");
457 	row = idx >> MONO_IMPLEMENTATION_BITS;
458 	switch (idx & MONO_IMPLEMENTATION_MASK) {
459 	case MONO_IMPLEMENTATION_FILE:
460 		table = "file";
461 		break;
462 	case MONO_IMPLEMENTATION_ASSEMBLYREF:
463 		table = "assemblyref";
464 		break;
465 	case MONO_IMPLEMENTATION_EXP_TYPE:
466 		table = "exportedtype";
467 		break;
468 	default:
469 		g_assert_not_reached ();
470 	}
471 	return g_strdup_printf ("%s %d", table, row);
472 }
473 
474 static const char*
get_manifest_flags(guint32 mf)475 get_manifest_flags (guint32 mf)
476 {
477 	mf &= 3;
478 	switch (mf) {
479 	case 1: return "public";
480 	case 2: return "private";
481 	default:
482 		return "";
483 	}
484 }
485 
486 void
dump_table_manifest(MonoImage * m)487 dump_table_manifest (MonoImage *m)
488 {
489 	MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
490 	int i;
491 	fprintf (output, "Manifestresource Table (1..%d)\n", t->rows);
492 
493 	for (i = 0; i < t->rows; i++){
494 		guint32 cols [MONO_MANIFEST_SIZE];
495 		const char *name, *mf;
496 		char *impl;
497 
498 		mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
499 
500 		name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
501 		mf = get_manifest_flags (cols [MONO_MANIFEST_FLAGS]);
502 		impl = get_manifest_implementation (m, cols [MONO_MANIFEST_IMPLEMENTATION]);
503 		fprintf (output, "%d: %s '%s' at offset %u in %s\n", i + 1, mf, name, cols [MONO_MANIFEST_OFFSET], impl);
504 		g_free (impl);
505 	}
506 
507 }
508 
509 void
dump_table_moduleref(MonoImage * m)510 dump_table_moduleref (MonoImage *m)
511 {
512 	MonoTableInfo *t = &m->tables [MONO_TABLE_MODULEREF];
513 	int i;
514 	fprintf (output, "ModuleRef Table (1..%d)\n", t->rows);
515 
516 	for (i = 0; i < t->rows; i++){
517 		guint32 cols [MONO_MODULEREF_SIZE];
518 		const char *name;
519 
520 		mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
521 
522 		name = mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]);
523 		fprintf (output, "%d: %s\n", i + 1, name);
524 	}
525 
526 }
527 
528 void
dump_table_module(MonoImage * m)529 dump_table_module (MonoImage *m)
530 {
531 	MonoTableInfo *t = &m->tables [MONO_TABLE_MODULE];
532 	int i;
533 	fprintf (output, "Module Table (1..%d)\n", t->rows);
534 
535 	for (i = 0; i < t->rows; i++){
536 		guint32 cols [MONO_MODULE_SIZE];
537 		const char *name;
538 		char *guid;
539 
540 		mono_metadata_decode_row (t, i, cols, MONO_MODULE_SIZE);
541 
542 		name = mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]);
543 		guid = get_guid (m, cols [MONO_MODULE_MVID]);
544 		fprintf (output, "%d: %s %d %s\n", i + 1, name, cols [MONO_MODULE_MVID], guid);
545 	}
546 }
547 
548 void
dump_table_method(MonoImage * m)549 dump_table_method (MonoImage *m)
550 {
551 	MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
552 	MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
553 	int i, current_type;
554 	guint32 first_m, last_m;
555 	/* Generic container for Type & method */
556 	MonoGenericContainer *type_container = NULL, *method_container = NULL;
557 
558 	fprintf (output, "Method Table (1..%d)\n", t->rows);
559 
560 	current_type = 1;
561 	last_m = first_m = 1;
562 	for (i = 1; i <= t->rows; i++){
563 		MonoError error;
564 		guint32 cols [MONO_METHOD_SIZE];
565 		char *sig, *impl_flags;
566 		const char *sigblob;
567 		MonoMethodSignature *method;
568 
569 		/*
570 		 * Find the next type.
571 		 */
572 		while (current_type <= td->rows && i >= (last_m = mono_metadata_decode_row_col (td, current_type - 1, MONO_TYPEDEF_METHOD_LIST))) {
573 			current_type++;
574 		}
575 		if (i == first_m) {
576 			fprintf (output, "########## %s.%s\n",
577 				mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAMESPACE)),
578 				mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAME)));
579 			first_m = last_m;
580 			type_container = mono_metadata_load_generic_params (m, MONO_TOKEN_TYPE_DEF | (current_type - 1), NULL);
581 			if (type_container) {
582 				mono_metadata_load_generic_param_constraints_checked (m, MONO_TOKEN_TYPE_DEF | (current_type - 1), type_container, &error);
583 				g_assert (mono_error_ok (&error)); /*FIXME don't swallow the error message*/
584 			}
585 		}
586 
587 		method_container = mono_metadata_load_generic_params (m, MONO_TOKEN_METHOD_DEF | i, type_container);
588 		if (method_container) {
589 			mono_metadata_load_generic_param_constraints_checked (m, MONO_TOKEN_METHOD_DEF | i, method_container, &error);
590 			g_assert (mono_error_ok (&error)); /*FIXME don't swallow the error message*/
591 		}
592 		mono_metadata_decode_table_row (m, MONO_TABLE_METHOD, i - 1, cols, MONO_METHOD_SIZE);
593 		sigblob = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
594 		mono_metadata_decode_blob_size (sigblob, &sigblob);
595 		method = mono_metadata_parse_method_signature_full (m, method_container ? method_container : type_container, i, sigblob, &sigblob, &error);
596 		if (!mono_error_ok (&error)) {
597 			fprintf (output,"%d: failed to parse due to %s\n", i, mono_error_get_message (&error));
598 			mono_error_cleanup (&error);
599 			continue;
600 		}
601 
602 		g_assert (mono_error_ok (&error)); /*FIXME don't swallow the error message*/
603 		sig = dis_stringify_method_signature (m, method, i, method_container ? method_container : type_container, FALSE);
604                 impl_flags = get_method_impl_flags (cols [MONO_METHOD_IMPLFLAGS]);
605 		fprintf (output, "%d: %s (param: %d impl_flags: %s)\n", i, sig, cols [MONO_METHOD_PARAMLIST], impl_flags);
606 		g_free (sig);
607 		g_free (impl_flags);
608 		mono_metadata_free_method_signature (method);
609 	}
610 
611 }
612 
613 void
dump_table_implmap(MonoImage * m)614 dump_table_implmap (MonoImage *m)
615 {
616 	MonoTableInfo *t = &m->tables [MONO_TABLE_IMPLMAP];
617 	MonoTableInfo *td = &m->tables [MONO_TABLE_MODULEREF];
618 	int i;
619 
620 	fprintf (output, "ImplMap Table (1..%d)\n", t->rows);
621 
622 	for (i = 1; i <= t->rows; i++){
623 		guint32 cols [MONO_IMPLMAP_SIZE];
624 		char *method;
625 
626 		mono_metadata_decode_row (t, i - 1, cols, MONO_IMPLMAP_SIZE);
627 
628 		method = get_method (m, MONO_TOKEN_METHOD_DEF | (cols [MONO_IMPLMAP_MEMBER] >> MONO_MEMBERFORWD_BITS), NULL);
629 
630 		fprintf (output, "%d: %s %d (%s %s)\n", i,
631 				 method,
632 				 cols [MONO_IMPLMAP_FLAGS],
633 				 mono_metadata_string_heap (m, cols [MONO_IMPLMAP_NAME]),
634 				 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME)));
635 	}
636 }
637 
638 void
dump_table_fieldrva(MonoImage * m)639 dump_table_fieldrva  (MonoImage *m)
640 {
641 	MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDRVA];
642 	int i;
643 
644 	fprintf (output, "FieldRVA Table (1..%d)\n", t->rows);
645 
646 	for (i = 1; i <= t->rows; i++){
647 		guint32 cols [MONO_FIELD_RVA_SIZE];
648 
649 		mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_RVA_SIZE);
650 		fprintf (output, "%d: Field %d: %x\n", i, cols [MONO_FIELD_RVA_FIELD], cols [MONO_FIELD_RVA_RVA]);
651 	}
652 }
653 
654 void
dump_table_methodimpl(MonoImage * m)655 dump_table_methodimpl (MonoImage *m)
656 {
657 	MonoTableInfo *t = &m->tables [MONO_TABLE_METHODIMPL];
658 	/*MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];*/
659 	int i;
660 
661 	fprintf (output, "MethodImpl Table (1..%d)\n", t->rows);
662 
663 	for (i = 1; i <= t->rows; i++){
664 		guint32 cols [MONO_METHODIMPL_SIZE];
665 		char *klass, *impl, *decl;
666 
667 		mono_metadata_decode_row (t, i - 1, cols, MONO_METHODIMPL_SIZE);
668 		klass = get_typedef (m, cols [MONO_METHODIMPL_CLASS]);
669 		impl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_BODY]), NULL);
670 		decl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_DECLARATION]), NULL);
671 		fprintf (output, "%d: %s\n\tdecl: %s\n\timpl: %s\n", i, klass, decl, impl);
672 		g_free (klass);
673 		g_free (impl);
674 		g_free (decl);
675 	}
676 
677 }
678 
679 static dis_map_t semantics_map [] = {
680 		{1, "setter"},
681 		{2, "getter"},
682 		{4, "other"},
683 		{8, "add-on"},
684 		{0x10, "remove-on"},
685 		{0x20, "fire"},
686 		{0, NULL},
687 };
688 
689 void
dump_table_methodsem(MonoImage * m)690 dump_table_methodsem (MonoImage *m)
691 {
692 	MonoTableInfo *t = &m->tables [MONO_TABLE_METHODSEMANTICS];
693 	int i, is_property, index;
694 	const char *semantics;
695 
696 	fprintf (output, "Method Semantics Table (1..%d)\n", t->rows);
697 	for (i = 1; i <= t->rows; i++){
698 		guint32 cols [MONO_METHOD_SEMA_SIZE];
699 
700 		mono_metadata_decode_row (t, i - 1, cols, MONO_METHOD_SEMA_SIZE);
701 		semantics = flags (cols [MONO_METHOD_SEMA_SEMANTICS], semantics_map);
702 		is_property = cols [MONO_METHOD_SEMA_ASSOCIATION] & MONO_HAS_SEMANTICS_MASK;
703 		index = cols [MONO_METHOD_SEMA_ASSOCIATION] >> MONO_HAS_SEMANTICS_BITS;
704 		fprintf (output, "%d: [%d] %s method: %d %s %d\n", i, cols [MONO_METHOD_SEMA_ASSOCIATION], semantics,
705 						cols [MONO_METHOD_SEMA_METHOD] - 1,
706 						is_property? "property" : "event",
707 						index);
708 	}
709 }
710 
711 void
dump_table_interfaceimpl(MonoImage * m)712 dump_table_interfaceimpl (MonoImage *m)
713 {
714 	MonoTableInfo *t = &m->tables [MONO_TABLE_INTERFACEIMPL];
715 	int i;
716 
717 	fprintf (output, "Interface Implementation Table (1..%d)\n", t->rows);
718 	for (i = 1; i <= t->rows; i++) {
719 		guint32 cols [MONO_INTERFACEIMPL_SIZE];
720 
721 		mono_metadata_decode_row (t, i - 1, cols, MONO_INTERFACEIMPL_SIZE);
722 		fprintf (output, "%d: %s implements %s\n", i,
723 			 get_typedef (m, cols [MONO_INTERFACEIMPL_CLASS]),
724 			 get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE], NULL));
725 	}
726 }
727 
728 static char*
has_cattr_get_table(MonoImage * m,guint32 val)729 has_cattr_get_table (MonoImage *m, guint32 val)
730 {
731 	guint32 t = val & MONO_CUSTOM_ATTR_MASK;
732 	guint32 index = val >> MONO_CUSTOM_ATTR_BITS;
733 	const char *table;
734 
735 	switch (t) {
736 	case MONO_CUSTOM_ATTR_METHODDEF:
737 		table = "MethodDef";
738 		break;
739 	case MONO_CUSTOM_ATTR_FIELDDEF:
740 		table = "FieldDef";
741 		break;
742 	case MONO_CUSTOM_ATTR_TYPEREF:
743 		table = "TypeRef";
744 		break;
745 	case MONO_CUSTOM_ATTR_TYPEDEF:
746 		table = "TypeDef";
747 		break;
748 	case MONO_CUSTOM_ATTR_PARAMDEF:
749 		table = "Param";
750 		break;
751 	case MONO_CUSTOM_ATTR_INTERFACE:
752 		table = "InterfaceImpl";
753 		break;
754 	case MONO_CUSTOM_ATTR_MEMBERREF:
755 		table = "MemberRef";
756 		break;
757 	case MONO_CUSTOM_ATTR_MODULE:
758 		table = "Module";
759 		break;
760 	case MONO_CUSTOM_ATTR_PERMISSION:
761 		table = "DeclSecurity?";
762 		break;
763 	case MONO_CUSTOM_ATTR_PROPERTY:
764 		table = "Property";
765 		break;
766 	case MONO_CUSTOM_ATTR_EVENT:
767 		table = "Event";
768 		break;
769 	case MONO_CUSTOM_ATTR_SIGNATURE:
770 		table = "StandAloneSignature";
771 		break;
772 	case MONO_CUSTOM_ATTR_MODULEREF:
773 		table = "ModuleRef";
774 		break;
775 	case MONO_CUSTOM_ATTR_TYPESPEC:
776 		table = "TypeSpec";
777 		break;
778 	case MONO_CUSTOM_ATTR_ASSEMBLY:
779 		table = "Assembly";
780 		break;
781 	case MONO_CUSTOM_ATTR_ASSEMBLYREF:
782 		table = "AssemblyRef";
783 		break;
784 	case MONO_CUSTOM_ATTR_FILE:
785 		table = "File";
786 		break;
787 	case MONO_CUSTOM_ATTR_EXP_TYPE:
788 		table = "ExportedType";
789 		break;
790 	case MONO_CUSTOM_ATTR_MANIFEST:
791 		table = "Manifest";
792 		break;
793 	case MONO_CUSTOM_ATTR_GENERICPAR:
794 		table = "GenericParam";
795 		break;
796 	default:
797 		table = "Unknown";
798 		break;
799 	}
800 	/*
801 	 * FIXME: we should decode the index into something more uman-friendly.
802 	 */
803 	return g_strdup_printf ("%s: %d", table, index);
804 }
805 
806 static char*
custom_attr_params(MonoImage * m,MonoMethodSignature * sig,const char * value)807 custom_attr_params (MonoImage *m, MonoMethodSignature* sig, const char* value)
808 {
809 	int len, i, slen, type;
810 	GString *res;
811 	char *s;
812 	const char *p = value;
813 
814 	len = mono_metadata_decode_value (p, &p);
815 	if (len < 2 || read16 (p) != 0x0001) /* Prolog */
816 		return g_strdup ("");
817 
818 	/* skip prolog */
819 	p += 2;
820 	res = g_string_new ("");
821 	for (i = 0; i < sig->param_count; ++i) {
822 		if (i != 0)
823 			g_string_append (res, ", ");
824 		type = sig->params [i]->type;
825 handle_enum:
826 		switch (type) {
827 		case MONO_TYPE_U1:
828 			g_string_append_printf (res, "%d", (unsigned int)*p);
829 			++p;
830 			break;
831 		case MONO_TYPE_I1:
832 			g_string_append_printf (res, "%d", *p);
833 			++p;
834 			break;
835 		case MONO_TYPE_BOOLEAN:
836 			g_string_append_printf (res, "%s", *p?"true":"false");
837 			++p;
838 			break;
839 		case MONO_TYPE_CHAR:
840 			g_string_append_printf (res, "'%c'", read16 (p));
841 			p += 2;
842 			break;
843 		case MONO_TYPE_U2:
844 			g_string_append_printf (res, "%d", read16 (p));
845 			p += 2;
846 			break;
847 		case MONO_TYPE_I2:
848 			g_string_append_printf (res, "%d", (gint16)read16 (p));
849 			p += 2;
850 			break;
851 		case MONO_TYPE_U4:
852 			g_string_append_printf (res, "%d", read32 (p));
853 			p += 4;
854 			break;
855 		case MONO_TYPE_I4:
856 			g_string_append_printf (res, "%d", (gint32)read32 (p));
857 			p += 4;
858 			break;
859 		case MONO_TYPE_U8:
860 			g_string_append_printf (res, "%lld", (long long)read64 (p));
861 			p += 8;
862 			break;
863 		case MONO_TYPE_I8:
864 			g_string_append_printf (res, "%lld", (long long)read64 (p));
865 			p += 8;
866 			break;
867 		case MONO_TYPE_R4: {
868 			float val;
869 			int inf;
870 			readr4 (p, &val);
871 			inf = dis_isinf (val);
872 			if (inf == -1)
873 				g_string_append_printf (res, "(00 00 80 ff)"); /* negative infinity */
874 			else if (inf == 1)
875 				g_string_append_printf (res, "(00 00 80 7f)"); /* positive infinity */
876 			else if (dis_isnan (val))
877 				g_string_append_printf (res, "(00 00 c0 ff)"); /* NaN */
878 			else
879 				g_string_append_printf (res, "%g", val);
880 			p += 4;
881 			break;
882 		}
883 		case MONO_TYPE_R8: {
884 			double val;
885 			int inf;
886 
887 			readr8 (p, &val);
888 			inf = dis_isinf (val);
889 			if (inf == -1)
890 				g_string_append_printf (res, "(00 00 00 00 00 00 f0 ff)"); /* negative infinity */
891 			else if (inf == 1)
892 				g_string_append_printf (res, "(00 00 00 00 00 00 f0 7f)"); /* positive infinity */
893 			else if (isnan (val))
894 				g_string_append_printf (res, "(00 00 00 00 00 00 f8 ff)"); /* NaN */
895 			else
896 				g_string_append_printf (res, "%g", val);
897 			p += 8;
898 			break;
899 		}
900 		case MONO_TYPE_VALUETYPE:
901 			if (mono_class_is_enum (sig->params [i]->data.klass)) {
902 				type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
903 				goto handle_enum;
904 			} else {
905 				g_warning ("generic valutype not handled in custom attr value decoding");
906 			}
907 			break;
908 		case MONO_TYPE_CLASS: /* It must be a Type: check? */
909 		case MONO_TYPE_STRING:
910 			if (*p == (char)0xff) {
911 				g_string_append (res, "null");
912 				p++;
913 				break;
914 			}
915 			slen = mono_metadata_decode_value (p, &p);
916 			g_string_append_c (res, '"');
917 			g_string_append (res, p);
918 			g_string_append_c (res, '"');
919 			p += slen;
920 			break;
921 		default:
922 			g_warning ("Type %02x not handled in custom attr value decoding", sig->params [i]->type);
923 			break;
924 		}
925 	}
926 	slen = read16 (p);
927 	if (slen) {
928 		g_string_append_printf (res, " %d named args: (", slen);
929 		slen = len - (p - value) + 1;
930 		for (i = 0; i < slen; ++i) {
931 			g_string_append_printf (res, " %02X", (p [i] & 0xff));
932 		}
933 		g_string_append_c (res, ')');
934 	}
935 	s = res->str;
936 	g_string_free (res, FALSE);
937 	return s;
938 }
939 
940 void
dump_table_customattr(MonoImage * m)941 dump_table_customattr (MonoImage *m)
942 {
943 	MonoTableInfo *t = &m->tables [MONO_TABLE_CUSTOMATTRIBUTE];
944 	int i;
945 
946 	fprintf (output, "Custom Attributes Table (1..%d)\n", t->rows);
947 	for (i = 1; i <= t->rows; i++) {
948 		MonoError error;
949 		guint32 cols [MONO_CUSTOM_ATTR_SIZE];
950 		guint32 mtoken;
951 		char * desc;
952 		char *method;
953 		char *params;
954 		MonoMethod *meth;
955 
956 		mono_metadata_decode_row (t, i - 1, cols, MONO_CUSTOM_ATTR_SIZE);
957 		desc = has_cattr_get_table (m, cols [MONO_CUSTOM_ATTR_PARENT]);
958 		mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> MONO_CUSTOM_ATTR_TYPE_BITS;
959 		switch (cols [MONO_CUSTOM_ATTR_TYPE] & MONO_CUSTOM_ATTR_TYPE_MASK) {
960 		case MONO_CUSTOM_ATTR_TYPE_METHODDEF:
961 			mtoken |= MONO_TOKEN_METHOD_DEF;
962 			break;
963 		case MONO_CUSTOM_ATTR_TYPE_MEMBERREF:
964 			mtoken |= MONO_TOKEN_MEMBER_REF;
965 			break;
966 		default:
967 			g_warning ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
968 			break;
969 		}
970 		method = get_method (m, mtoken, NULL);
971 		meth = mono_get_method_checked (m, mtoken, NULL, NULL, &error);
972 		if (meth) {
973 			params = custom_attr_params (m, mono_method_signature (meth), mono_metadata_blob_heap (m, cols [MONO_CUSTOM_ATTR_VALUE]));
974 			fprintf (output, "%d: %s: %s [%s]\n", i, desc, method, params);
975 			g_free (params);
976 		} else {
977 			fprintf (output, "Could not decode method due to %s", mono_error_get_message (&error));
978 			mono_error_cleanup (&error);
979 		}
980 
981 		g_free (desc);
982 		g_free (method);
983 	}
984 }
985 
986 void
dump_table_nestedclass(MonoImage * m)987 dump_table_nestedclass (MonoImage *m)
988 {
989 	MonoTableInfo *t = &m->tables [MONO_TABLE_NESTEDCLASS];
990 	guint32 cols [MONO_NESTED_CLASS_SIZE];
991 	int i;
992 	char *nested, *nesting;
993 	fprintf (output, "NestedClass Table (1..%d)\n", t->rows);
994 
995 	for (i = 1; i <= t->rows; i++){
996 		mono_metadata_decode_row (t, i - 1, cols, MONO_NESTED_CLASS_SIZE);
997 		nested = get_typedef (m, cols [MONO_NESTED_CLASS_NESTED]);
998 		nesting = get_typedef (m, cols [MONO_NESTED_CLASS_ENCLOSING]);
999 		fprintf (output, "%d: %d %d: %s in %s\n", i,
1000 				cols [MONO_NESTED_CLASS_NESTED],
1001 				cols [MONO_NESTED_CLASS_ENCLOSING], nested, nesting);
1002 		g_free (nested);
1003 		g_free (nesting);
1004 	}
1005 
1006 }
1007 
1008 void
dump_table_exported(MonoImage * m)1009 dump_table_exported (MonoImage *m)
1010 {
1011 	MonoTableInfo *t = &m->tables [MONO_TABLE_EXPORTEDTYPE];
1012 	guint32 cols [MONO_EXP_TYPE_SIZE];
1013 	int i;
1014 	const char *name, *nspace;
1015 	char *impl;
1016 	guint32 index, flags;
1017 	fprintf (output, "ExportedType Table (1..%d)\n", t->rows);
1018 
1019 	for (i = 1; i <= t->rows; i++) {
1020 		mono_metadata_decode_row (t, i - 1, cols, MONO_EXP_TYPE_SIZE);
1021 		name = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAME]);
1022 		nspace = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAMESPACE]);
1023 		impl = get_manifest_implementation (m, cols [MONO_EXP_TYPE_IMPLEMENTATION]);
1024 		index = cols [MONO_EXP_TYPE_TYPEDEF];
1025 		flags = cols [MONO_EXP_TYPE_FLAGS];
1026 		fprintf (output, "%d: %s%s%s is in %s, index=%x, flags=0x%x\n", i, nspace, *nspace ? "." : "", name, impl, index, flags);
1027 		g_free (impl);
1028 	}
1029 
1030 }
1031 
1032 static void
dump_blob(MonoImage * m,const char * blob)1033 dump_blob (MonoImage *m, const char* blob)
1034 {
1035 	int j, bsize;
1036 
1037 	bsize = mono_metadata_decode_blob_size (blob, &blob);
1038 
1039 	for (j = 0; j < bsize; j++) {
1040 		fprintf (output, "%02x ", blob [j] & 0xff);
1041 	}
1042 }
1043 
1044 void
dump_table_field_marshal(MonoImage * m)1045 dump_table_field_marshal (MonoImage *m)
1046 {
1047 	MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDMARSHAL];
1048 	guint32 cols [MONO_FIELD_MARSHAL_SIZE];
1049 	int i, is_field, idx;
1050 	const char *blob;
1051 	char *native;
1052 
1053 	fprintf (output, "FieldMarshal Table (1..%d)\n", t->rows);
1054 
1055 	for (i = 1; i <= t->rows; i++) {
1056 		mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_MARSHAL_SIZE);
1057 		blob = mono_metadata_blob_heap (m, cols [MONO_FIELD_MARSHAL_NATIVE_TYPE]);
1058 		native = get_marshal_info (m, blob);
1059 		is_field = (cols [MONO_FIELD_MARSHAL_PARENT] & MONO_HAS_FIELD_MARSHAL_MASK) == MONO_HAS_FIELD_MARSHAL_FIELDSREF;
1060 		idx = cols [MONO_FIELD_MARSHAL_PARENT] >> MONO_HAS_FIELD_MARSHAL_BITS;
1061 		fprintf (output, "%d: (0x%04x) %s %d: %s\n", i, cols [MONO_FIELD_MARSHAL_PARENT], is_field? "Field" : "Param", idx, native);
1062 		fprintf (output, "\tblob encoding: ");
1063 		dump_blob (m, blob);
1064 		fprintf (output, "\n");
1065 		g_free (native);
1066 	}
1067 
1068 }
1069 
1070 static const char*
get_security_action(int val)1071 get_security_action (int val) {
1072 	static char buf [32];
1073 
1074 	switch (val) {
1075 	case SECURITY_ACTION_DEMAND:
1076 		return "Demand";
1077 	case SECURITY_ACTION_ASSERT:
1078 		return "Assert";
1079 	case SECURITY_ACTION_DENY:
1080 		return "Deny";
1081 	case SECURITY_ACTION_PERMITONLY:
1082 		return "PermitOnly";
1083 	case SECURITY_ACTION_LINKDEMAND:
1084 		return "LinkDemand";
1085 	case SECURITY_ACTION_INHERITDEMAND:
1086 		return "InheritanceDemand";
1087 	case SECURITY_ACTION_REQMIN:
1088 		return "RequestMinimum";
1089 	case SECURITY_ACTION_REQOPT:
1090 		return "RequestOptional";
1091 	case SECURITY_ACTION_REQREFUSE:
1092 		return "RequestRefuse";
1093 	/* Special actions (for non CAS permissions) */
1094 	case SECURITY_ACTION_NONCASDEMAND:
1095 		return "NonCasDemand";
1096 	case SECURITY_ACTION_NONCASLINKDEMAND:
1097 		return "NonCasLinkDemand";
1098 	case SECURITY_ACTION_NONCASINHERITANCE:
1099 		return "NonCasInheritance";
1100 	/* Fx 2.0 actions (for both CAS and non-CAS permissions) */
1101 	case SECURITY_ACTION_LINKDEMANDCHOICE:
1102 		return "LinkDemandChoice";
1103 	case SECURITY_ACTION_INHERITDEMANDCHOICE:
1104 		return "InheritanceDemandChoice";
1105 	case SECURITY_ACTION_DEMANDCHOICE:
1106 		return "DemandChoice";
1107 	default:
1108 		g_snprintf (buf, sizeof (buf), "0x%04X", val);
1109 		return buf;
1110 	}
1111 }
1112 
1113 void
dump_table_declsec(MonoImage * m)1114 dump_table_declsec (MonoImage *m)
1115 {
1116 	MonoTableInfo *t = &m->tables [MONO_TABLE_DECLSECURITY];
1117 	guint32 cols [MONO_DECL_SECURITY_SIZE];
1118 	int i, len;
1119 	guint32 idx;
1120 	const char *blob, *action;
1121 	const char* parent[] = {
1122 		"TypeDef", "MethodDef", "Assembly", ""
1123 	};
1124 
1125 	fprintf (output, "DeclSecurity Table (1..%d)\n", t->rows);
1126 
1127 	for (i = 1; i <= t->rows; i++) {
1128 		mono_metadata_decode_row (t, i - 1, cols, MONO_DECL_SECURITY_SIZE);
1129 		blob = mono_metadata_blob_heap (m, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
1130 		len = mono_metadata_decode_blob_size (blob, &blob);
1131 		action = get_security_action (cols [MONO_DECL_SECURITY_ACTION]);
1132 		idx = cols [MONO_DECL_SECURITY_PARENT];
1133 		fprintf (output, "%d: %s on %s %d%s", i, action, parent [idx & MONO_HAS_DECL_SECURITY_MASK], idx >> MONO_HAS_DECL_SECURITY_BITS, len? ":\n\t":"\n");
1134 		if (!len)
1135 			continue;
1136 		if (blob [0] == MONO_DECLSEC_FORMAT_20) {
1137 			/* 2.0 declarative security format */
1138 			char *declsec = dump_declsec_entry20 (m, blob, "\t");
1139 			fprintf (output, "%s", declsec);
1140 			g_free (declsec);
1141 		} else {
1142 			/* 1.0 declarative security format - Unicode XML */
1143 			for (idx = 0; idx < len; ++idx)
1144 				fprintf (output, "%c", blob [idx]);
1145 		}
1146 		fprintf (output, "\n");
1147 	}
1148 }
1149 
1150 void
dump_table_genericpar(MonoImage * m)1151 dump_table_genericpar (MonoImage *m)
1152 {
1153 	MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAM];
1154 	guint32 cols [MONO_GENERICPARAM_SIZE];
1155 	int i;
1156 
1157 	fprintf (output, "GenericParameters (1..%d)\n", t->rows);
1158 
1159 	for (i = 1; i <= t->rows; i++) {
1160                 char *sig;
1161 		mono_metadata_decode_row (t, i - 1, cols, MONO_GENERICPARAM_SIZE);
1162 
1163                 // sig = get_type_or_methdef (m, cols [MONO_GENERICPARAM_OWNER]);
1164 		sig = g_strdup_printf ("%x", cols [MONO_GENERICPARAM_OWNER]);
1165 		fprintf (output, "%d: %d, flags=%d, owner=%s %s\n", i,
1166 			 cols [MONO_GENERICPARAM_NUMBER],
1167 			 cols [MONO_GENERICPARAM_FLAGS], sig,
1168 			 mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
1169                 g_free (sig);
1170 	}
1171 }
1172 
1173 void
dump_table_methodspec(MonoImage * m)1174 dump_table_methodspec (MonoImage *m)
1175 {
1176 	MonoTableInfo *t = &m->tables [MONO_TABLE_METHODSPEC];
1177 	guint32 cols [MONO_METHODSPEC_SIZE];
1178 	int i;
1179 
1180 	fprintf (output, "MethodSpec (1..%d)\n", t->rows);
1181 
1182 	for (i = 1; i <= t->rows; i++) {
1183 		char *sig;
1184 		char *method;
1185                 guint32 token;
1186 
1187 		mono_metadata_decode_row (t, i - 1, cols, MONO_METHODSPEC_SIZE);
1188 
1189                 /* build a methodspec token to get the method */
1190                 token = MONO_TOKEN_METHOD_SPEC | i;
1191                 method = get_method (m, token, NULL);
1192 
1193                 sig = get_method_type_param (m, cols [MONO_METHODSPEC_SIGNATURE], NULL);
1194 		fprintf (output, "%d: %s, %s\n", i, method, sig);
1195 		g_free (sig);
1196 		g_free (method);
1197 	}
1198 }
1199 
1200 void
dump_table_parconstraint(MonoImage * m)1201 dump_table_parconstraint (MonoImage *m)
1202 {
1203 	MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
1204 	guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
1205 	int i;
1206 
1207 	fprintf (output, "Generic Param Constraint (1..%d)\n", t->rows);
1208 
1209 	for (i = 1; i <= t->rows; i++) {
1210                 char *sig;
1211 		mono_metadata_decode_row (t, i - 1, cols, MONO_GENPARCONSTRAINT_SIZE);
1212 
1213                 // sig = get_typedef_or_ref (m, cols [MONO_GENPARCONSTRAINT_CONSTRAINT], NULL);
1214 		sig = g_strdup_printf ("%x", cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
1215 		fprintf (output, "%d: gen-par=%d, Constraint=%s\n", i,
1216 			 cols [MONO_GENPARCONSTRAINT_GENERICPAR], sig);
1217                 g_free (sig);
1218 	}
1219 }
1220 
1221 void
dump_stream_blob(MonoImage * m)1222 dump_stream_blob (MonoImage *m)
1223 {
1224 	int i;
1225 
1226 	fprintf (output, "Blob heap contents\n");
1227 
1228 	for (i = 0; i < m->heap_blob.size; i++) {
1229 		if (i > 0) {
1230 			if ((i % 16) == 0)
1231 				fprintf (output, "\n");
1232 			else if ((i % 8) == 0)
1233 				fprintf (output, "- ");
1234 		}
1235 		fprintf (output, "%02x ", m->heap_blob.data [i] & 0xff);
1236 	}
1237 
1238 	fprintf (output, "\n");
1239 }
1240 
1241 void
dump_stream_strings(MonoImage * m)1242 dump_stream_strings (MonoImage *m)
1243 {
1244 	guint32 i;
1245 
1246 	fprintf (output, "Strings heap contents\n");
1247 
1248 	for (i = 0; i < m->heap_strings.size; ) {
1249 		const char *str = mono_metadata_string_heap (m, i);
1250 		fprintf (output, "%02x: \"%s\"\n", i, str);
1251 		i += strlen (str) + 1;
1252 	}
1253 }
1254 
1255 void
dump_stream_us(MonoImage * m)1256 dump_stream_us (MonoImage *m)
1257 {
1258 	guint32 i;
1259 
1260 	fprintf (output, "User Strings heap contents\n");
1261 
1262 	for (i = 0; i < m->heap_us.size; ) {
1263 		const char *us_ptr = mono_metadata_user_string (m, i);
1264 		int len = mono_metadata_decode_blob_size (us_ptr, (const char**)&us_ptr);
1265 
1266 		char *str = get_encoded_user_string_or_bytearray ((const guchar*)us_ptr, len);
1267 		fprintf (output, "%02x: %s\n", i, str);
1268 		g_free (str);
1269 		i += len + 1;
1270 	}
1271 }
1272 
1273 void
dump_table_standalonesig(MonoImage * m)1274 dump_table_standalonesig (MonoImage *m)
1275 {
1276 	MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1277 	guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1278 	int i;
1279 
1280 	fprintf (output, "Stand alone signature (1..%d)\n", t->rows);
1281 
1282 	for (i = 1; i <= t->rows; i++) {
1283                 const char *locals_ptr;
1284 		int j, bsize;
1285 
1286 		mono_metadata_decode_row (t, i - 1, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
1287 
1288 		locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1289 		bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1290 
1291 		fprintf (output, "%d: blob[0x%x] = ", i, cols [MONO_STAND_ALONE_SIGNATURE]);
1292 
1293 		for (j = 0; j < bsize; j++) {
1294 			fprintf (output, "%02x ", locals_ptr [j] & 0xff);
1295 		}
1296 		fprintf (output, "\n");
1297 	}
1298 }
1299 
1300 static void
dump_table_ptr(MonoImage * m,int table,const char * name)1301 dump_table_ptr (MonoImage *m, int table, const char *name)
1302 {
1303 	MonoTableInfo *t = &m->tables [table];
1304 	guint32 cols [1];
1305 	int i;
1306 
1307 	fprintf (output, "%s (1..%d)\n", name, t->rows);
1308 
1309 	for (i = 1; i <= t->rows; i++) {
1310 		mono_metadata_decode_row (t, i - 1, cols, 1);
1311 
1312 		fprintf (output, "%d: %d\n", i, cols [0]);
1313 	}
1314 }
1315 
1316 void
dump_table_methodptr(MonoImage * m)1317 dump_table_methodptr (MonoImage *m)
1318 {
1319 	dump_table_ptr (m, MONO_TABLE_METHOD_POINTER, "Method Ptr");
1320 }
1321 
1322 void
dump_table_fieldptr(MonoImage * m)1323 dump_table_fieldptr (MonoImage *m)
1324 {
1325 	dump_table_ptr (m, MONO_TABLE_FIELD_POINTER, "Field Ptr");
1326 }
1327 
1328 void
dump_table_paramptr(MonoImage * m)1329 dump_table_paramptr (MonoImage *m)
1330 {
1331 	dump_table_ptr (m, MONO_TABLE_PARAM_POINTER, "Param Ptr");
1332 }
1333 
1334 void
dump_table_eventptr(MonoImage * m)1335 dump_table_eventptr (MonoImage *m)
1336 {
1337 	dump_table_ptr (m, MONO_TABLE_EVENT_POINTER, "Event Ptr");
1338 }
1339 
1340 void
dump_table_propertyptr(MonoImage * m)1341 dump_table_propertyptr (MonoImage *m)
1342 {
1343 	dump_table_ptr (m, MONO_TABLE_PROPERTY_POINTER, "Property Ptr");
1344 }
1345