1 /**
2  * \file
3  */
4 
5 #include <stdio.h>
6 
7 #include "config.h"
8 #include <mono/metadata/abi-details.h>
9 
10 #include <mono/metadata/class-internals.h>
11 #include <mono/metadata/object-internals.h>
12 #include <mono/metadata/profiler-private.h>
13 #include <mono/metadata/monitor.h>
14 #include <mono/metadata/handle.h>
15 #ifdef HAVE_SGEN_GC
16 #include <mono/sgen/sgen-gc.h>
17 #endif
18 
19 static int
dump_arch(void)20 dump_arch (void)
21 {
22 #if defined (TARGET_X86)
23 	g_print ("#ifdef TARGET_X86\n");
24 #elif defined (TARGET_AMD64)
25 	g_print ("#ifdef TARGET_AMD64\n");
26 #elif defined (TARGET_ARM)
27 	g_print ("#ifdef TARGET_ARM\n");
28 #elif defined (TARGET_ARM64)
29 	g_print ("#ifdef TARGET_ARM64\n");
30 #else
31 	return 0;
32 #endif
33 	return 1;
34 }
35 
36 static int
dump_os(void)37 dump_os (void)
38 {
39 #if defined (HOST_WIN32)
40 	g_print ("#ifdef TARGET_WIN32\n");
41 #elif defined (HOST_ANDROID)
42 	g_print ("#ifdef TARGET_ANDROID\n");
43 #elif defined (HOST_DARWIN)
44 	g_print ("#ifdef TARGET_OSX\n");
45 #elif defined (PLATFORM_IOS)
46 	g_print ("#ifdef TARGET_IOS\n");
47 #else
48 	return 0;
49 #endif
50 	return 1;
51 }
52 
53 void
54 mono_dump_metadata_offsets (void);
55 
56 void
mono_dump_metadata_offsets(void)57 mono_dump_metadata_offsets (void)
58 {
59 #ifdef USED_CROSS_COMPILER_OFFSETS
60 	g_print ("not using native offsets\n");
61 #else
62 	g_print ("#ifndef USED_CROSS_COMPILER_OFFSETS\n");
63 
64 	if (!dump_arch ()) {
65 		g_print ("#error failed to figure out the current arch\n");
66 		return;
67 	}
68 
69 	if (!dump_os ()) {
70 		g_print ("#error failed to figure out the current OS\n");
71 		return;
72 	}
73 
74 #ifdef HAVE_SGEN_GC
75 	g_print ("#ifndef HAVE_BOEHM_GC\n");
76 #elif HAVE_BOEHM_GC
77 	g_print ("#ifndef HAVE_SGEN_GC\n");
78 #else
79 	g_print ("#error no gc conf not supported\n");
80 	return;
81 #endif
82 
83 	g_print ("#define HAS_CROSS_COMPILER_OFFSETS\n");
84 	g_print ("#if defined (USE_CROSS_COMPILE_OFFSETS) || defined (MONO_CROSS_COMPILE)\n");
85 	g_print ("#if !defined (DISABLE_METADATA_OFFSETS)\n");
86 	g_print ("#define USED_CROSS_COMPILER_OFFSETS\n");
87 
88 #define DISABLE_JIT_OFFSETS
89 #define DECL_OFFSET2(struct,field,offset) this_should_not_happen
90 #define DECL_ALIGN2(type,size) this_should_not_happen
91 
92 #define DECL_OFFSET(struct,field) g_print ("DECL_OFFSET2(%s,%s,%d)\n", #struct, #field, (int)MONO_STRUCT_OFFSET (struct, field));
93 #define DECL_ALIGN(type) g_print ("DECL_ALIGN2(%s,%d)\n", #type, (int)MONO_ABI_ALIGNOF (type));
94 #define DECL_SIZE(type) g_print ("DECL_SIZE2(%s,%d)\n", #type, (int)MONO_ABI_SIZEOF (type));
95 #include <mono/metadata/object-offsets.h>
96 
97 	g_print ("#endif //disable metadata check\n");
98 	g_print ("#endif //gc check\n");
99 #endif
100 }
101 
102 void
103 mono_metadata_cross_helpers_run (void);
104 
105 /*
106  * mono_metadata_cross_helpers_run:
107  *
108  *   Check that the offsets given by object-offsets.h match the offsets
109  * on the host. This only checks the metadata offsets.
110  */
111 void
mono_metadata_cross_helpers_run(void)112 mono_metadata_cross_helpers_run (void)
113 {
114 #if defined (HAS_CROSS_COMPILER_OFFSETS) && !defined (MONO_CROSS_COMPILE)
115 	gboolean is_broken = FALSE;
116 
117 #define DISABLE_JIT_OFFSETS
118 #define USE_CROSS_COMPILE_OFFSETS
119 #define DECL_OFFSET(struct,field) this_should_not_happen_for_cross_fields
120 #define DECL_OFFSET2(struct,field,offset) \
121 	 if ((int)G_STRUCT_OFFSET (struct, field) != offset) { \
122 		g_print (#struct ":" #field " invalid struct offset %d (expected %d)\n",	\
123 			offset,	\
124 			(int)G_STRUCT_OFFSET (struct, field));	\
125 		is_broken = TRUE;	\
126 	}
127 #define DECL_ALIGN(type) this_should_not_happen_for_cross_align
128 #define DECL_ALIGN2(name,size) \
129 	 if (MONO_ALIGN_ ## name != size) { \
130 		g_print (#name ": invalid alignment %d (expected %d)\n",	\
131 		size,	\
132 		MONO_ALIGN_ ## name);	\
133 		is_broken = TRUE;	\
134 	}
135 #define DECL_SIZE(type) this_should_not_happen_for_cross_size
136 #define DECL_SIZE2(name,size) \
137 	 if (MONO_SIZEOF_ ## name != size) { \
138 		g_print (#name ": invalid size %d (expected %d)\n",	\
139 		size,	\
140 		MONO_SIZEOF_ ## name);	\
141 		is_broken = TRUE;	\
142 	}
143 
144 #include <mono/metadata/object-offsets.h>
145 
146 	g_assert (!is_broken);
147 #endif
148 }
149 
150